Commit Graph

221 Commits

Author SHA1 Message Date
Kazu Hirata a7938c74f1 [llvm] Don't use Optional::hasValue (NFC)
This patch replaces Optional::hasValue with the implicit cast to bool
in conditionals only.
2022-06-25 21:42:52 -07:00
Kazu Hirata 3b7c3a654c Revert "Don't use Optional::hasValue (NFC)"
This reverts commit aa8feeefd3.
2022-06-25 11:56:50 -07:00
Kazu Hirata aa8feeefd3 Don't use Optional::hasValue (NFC) 2022-06-25 11:55:57 -07:00
Nikita Popov 871197d0a3 [MemoryBuiltins] Accept any value in getInitialValueOfAllocation() (NFC)
Drop the requirement that getInitialValueOfAllocation() must be
passed an allocator function, shifting the responsibility for
checking that into the function (which it does anyway). The
motivation is to avoid some calls to isAllocationFn(), which has
somewhat ill-defined semantics (given the number of
allocator-related attributes we have floating around...)

(For this function, all we eventually need is an allockind of
zeroed or uninitialized.)

Differential Revision: https://reviews.llvm.org/D127274
2022-06-24 16:08:07 +02:00
Kazu Hirata e0e687a615 [llvm] Don't use Optional::hasValue (NFC) 2022-06-20 10:38:12 -07:00
Guillaume Chatelet d3cf49e984 [Alignment] Remove alignTo version taking a MaybeAlign 2022-06-20 15:15:53 +00:00
Kazu Hirata 129b531c9c [llvm] Use value_or instead of getValueOr (NFC) 2022-06-18 23:07:11 -07:00
Jay Foad 6bec3e9303 [APInt] Remove all uses of zextOrSelf, sextOrSelf and truncOrSelf
Most clients only used these methods because they wanted to be able to
extend or truncate to the same bit width (which is a no-op). Now that
the standard zext, sext and trunc allow this, there is no reason to use
the OrSelf versions.

The OrSelf versions additionally have the strange behaviour of allowing
extending to a *smaller* width, or truncating to a *larger* width, which
are also treated as no-ops. A small amount of client code relied on this
(ConstantRange::castOp and MicrosoftCXXNameMangler::mangleNumber) and
needed rewriting.

Differential Revision: https://reviews.llvm.org/D125557
2022-05-19 11:23:13 +01:00
Augie Fackler 1deea714b3 BuildLibCalls: simplify switch statement slightly
Per feedback on D123086 after submit.

Also added a test for vec_malloc et al attribute inference to show it's
doing the right thing.

The new tests exposed a defect, corrected by adding vec_free to the list of
free functions in MemoryBuiltins.cpp, which had been overlooked all the
way back in D94710, over a year ago.

Differential Revision: https://reviews.llvm.org/D124859
2022-05-03 13:17:33 -04:00
serge-sans-paille 262eba01b3 Revert "[ValueTracking] Make getStringLenth aware of strdup"
This reverts commit e810d55809.

The commit was not taken into account the fact that strduped string could be
modified. Checking if such modification happens would make the function very
costly, without a test case in mind it's not worth the effort.
2022-04-13 19:17:28 +02:00
serge-sans-paille e810d55809 [ValueTracking] Make getStringLenth aware of strdup
During strlen compile-time evaluation, make it possible to track size of
strduped strings.

Differential Revision: https://reviews.llvm.org/D123497
2022-04-12 14:47:29 +02:00
Nikita Popov 4e85b427dd [MemoryBuiltins] Remove unnecessary lambda capture (NFC) 2022-04-08 10:13:37 +02:00
serge-sans-paille aa15ea47e2 [builtin_object_size] Basic support for posix_memalign
It actually implements support for seeing through loads, using alias analysis to
refine the result.

This is rather limited, but I didn't want to rely on more than available
analysis at that point (to be gentle with compilation time), and it does seem to
catch common scenario, as showcased by the included tests.

Differential Revision: https://reviews.llvm.org/D122431
2022-04-08 09:31:11 +02:00
Augie Fackler d6a7da5ae3 MemoryBuiltins: only claim an allocator family on builtin functions
This lines up with other parts of the codebase that only use special
knowledge about allocator functions if they're builtins.

Differential Revision: https://reviews.llvm.org/D123053
2022-04-07 12:38:45 -04:00
Augie Fackler 5f09498a11 MemoryBuiltins: also check function definition for allocalign
This got changed to use hasAttrSomewhere() during review, and I didn't
notice until today when I was writing some tests for another part of
this system that using hasAttrSomewhere only checked the callsite for
allocalign, rather than both the callsite and the definition. This fixes
that by introducing a helper method.

Differential Revision: https://reviews.llvm.org/D121641
2022-04-07 12:38:44 -04:00
Augie Fackler 33b1f41914 MemoryBuiltins: getAllocAlignment is now useful for non-allocator funcs
This has been true since dba73135c8, but
didn't matter until now because clang wasn't emitting allocalign
attributes.

Differential Revision: https://reviews.llvm.org/D121640
2022-04-06 09:51:38 -04:00
serge-sans-paille 39b02d49cc [instcombine] Support and test __builtin_object_size interaction with __strdup and __strndup
Differential Revision: https://reviews.llvm.org/D122005
2022-03-21 11:30:51 +01:00
serge-sans-paille d8e0a6d5e9 [LowerConstantIntrinsics] Support phi operand in __builtin_object_size folder
The implementation is just a generalization of the Select handler.
We're no trying to be smart and compute any kind of fixed point.

Differential Revision: https://reviews.llvm.org/D121897
2022-03-21 11:30:50 +01:00
Augie Fackler dba73135c8 getAllocAlignment: respect allocalign attribute if present
As with allocsize(), we prefer the table data to attributes.

Differential Revision: https://reviews.llvm.org/D118263
2022-03-04 15:57:54 -05:00
Augie Fackler 5e4c75db3b InstructionCombining: avoid eliding mismatched alloc/free pairs
Prior to this change LLVM would happily elide a call to any allocation
function and a call to any free function operating on the same unused
pointer. This can cause problems in some obscure cases, for example if
the body of operator::new can be inlined but the body of
operator::delete can't, as in this example from jyknight:

    #include <stdlib.h>
    #include <stdio.h>

    int allocs = 0;

    void *operator new(size_t n) {
        allocs++;
        void *mem = malloc(n);
        if (!mem) abort();
        return mem;
    }

    __attribute__((noinline)) void operator delete(void *mem) noexcept {
        allocs--;
        free(mem);
    }

    void deleteit(int*i) { delete i; }
    int main() {
        int*i = new int;
        deleteit(i);
        if (allocs != 0)
          printf("MEMORY LEAK! allocs: %d\n", allocs);
    }

This patch addresses the issue by introducing the concept of an
allocator function family and uses it to make sure that alloc/free
function pairs are only removed if they're in the same family.

Differential Revision: https://reviews.llvm.org/D117356
2022-03-04 10:41:10 -05:00
serge-sans-paille 71c3a5519d Cleanup includes: LLVMAnalysis
Number of lines output by preprocessor:
before: 1065940348
after:  1065307662

Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup
Differential Revision: https://reviews.llvm.org/D120659
2022-03-01 18:01:54 +01:00
Philip Reames cf5e88864b [instsimplify] When compare allocas, consider their minimal size
The code was using exact sizing only, but since what we really need is just to make sure the offsets are in bounds, a minimum bound on the object size is sufficient.

To demonstrate the difference, support computing minimum sizes from obects of scalable vector type.
2022-02-17 09:53:24 -08:00
Johannes Doerfert 29c8ebad10 [MemoryBuiltins][FIX] Adjust index type size properly wrt. AS casts
Use existing functionality to strip constant offsets that works well
with AS casts and avoids the code duplication.

Since we strip AS casts during the computation of the offset we also
need to adjust the APInt properly to avoid mismatches in the bit width.
This code ensures the caller of `compute` sees APInts that match the
index type size of the value passed to `compute`, not the value result
of the strip pointer cast.

Fixes #53559.

Differential Revision: https://reviews.llvm.org/D118727
2022-02-07 20:19:19 -06:00
Augie Fackler b2d091aa5d [NFC] MemoryBuiltins: tease out a getFreeFunctionDataForFunction helper 2022-02-03 08:36:36 -08:00
Augie Fackler bad0301cc5 MemoryBuiltins: simplify isLibFreeFunction [NFC]
This is in anticipation of my next patch, where I need to store more information about free functions than just their argument count. It felt invasive enough on this function that it seemed worthwhile to just extract this as its own commit that makes no functional changes.

Differential Revision: https://reviews.llvm.org/D117350
2022-02-03 08:30:02 -08:00
Philip Reames 215bd46905 [MemoryBuiltins] Demote isMallocLikeFn to implementation routine since last use has been removed
Try 2, this time including the test.
2022-01-18 15:24:52 -08:00
Philip Reames fcab2d1309 Revert "[MemoryBuiltins] Demote isMallocLikeFn to implementation routine since last use has been removed"
This reverts commit 167af7bbfe. Buildbot breaks since I forgot to remove a unit test.
2022-01-18 15:16:12 -08:00
Philip Reames 167af7bbfe [MemoryBuiltins] Demote isMallocLikeFn to implementation routine since last use has been removed 2022-01-18 15:12:07 -08:00
Philip Reames dac82b53e2 Revert "[MemoryBuiltins] [NFC] Add missing section comments"
This reverts commit 83338d5032.  Comments in source are non-idiomatic and naming choice in head is unclear.
2022-01-14 08:34:21 -08:00
Bryce Wilson 83338d5032
[MemoryBuiltins] [NFC] Add missing section comments 2022-01-13 17:43:43 -08:00
Philip Reames ee02cf0797 [MemoryBuiltins] Demote isCallocLikeFn and isAlignedAllocLikeFn to local helpers after removal of last external use [NFC] 2022-01-13 15:51:17 -08:00
Philip Reames cf66f01ec1 [Attributor] Share code for abstract interpretation of allocation sizes with getObjectSize [NFC-ish]
The basic idea is that we can parameterize the getObjectSize implementation with a callback which lets us replace the operand before analysis if desired. This is what Attributor is doing during it's abstract interpretation, and allows us to have one copy of the code.

Note this is not NFC for two reasons:
* The existing attributor code is wrong. (Well, this is under-specified to be honest, but at least inconsistent.) The intermediate math needs to be done in the index type of the pointer space. Imagine e.g. i64 arguments in a 32 bit address space.
* I did not preserve the behavior in getAPInt where we return 0 for a partially analyzed value. This looks simply wrong in the original code, and nothing test wise contradicts that.

Differential Revision: https://reviews.llvm.org/D117241
2022-01-13 15:33:24 -08:00
Bryce Wilson 68874d8b5f
[MemoryBuiltins] [NFC] Remove unused overload of isAlignedAllocLikeFn
Differential Revision: https://reviews.llvm.org/D117245
2022-01-13 15:19:04 -08:00
Philip Reames cd36b29ec7 [MemoryBuiltins] (Slightly) clean up abuse of MallocLike bitmask [NFC] 2022-01-13 12:39:22 -08:00
Nikita Popov 3946095b88 [MemoryBuiltins] Remove unused isOpNewLikeFn() (NFC)
This function is no longer used since
2cafbcb560.
2022-01-11 12:27:23 +01:00
Nikita Popov b56f6f1913 [MemoryBuiltins] Remove unused isStrdupLikeFn() function (NFC)
This function is no longer used after
dcbc91f40c.
2022-01-11 12:26:20 +01:00
Philip Reames 5265ac72c6 [MemoryBuiltin] Add an API for checking if an unused allocation can be removed [NFC]
Not all allocation functions are removable if unused.  An example of a non-removable allocation would be a direct call to the replaceable global allocation function in C++.  An example of a removable one - at least according to historical practice - would be malloc.
2022-01-10 15:43:39 -08:00
Philip Reames 1d127315e7 Minor style tweaks following fb93659 2022-01-10 09:32:29 -08:00
Bryce Wilson 7febd60a90 [instcombine] Add align return attributes for operator new(..., align_val)
(Split from original patch to separate non-NFC part and add coverage.  I typoed when adding the new test, so this change includes the typo fix to let libfunc recongize the signature.  Didn't figure it was worth another separate commit.)

Differential Revision: https://reviews.llvm.org/D116851 (part 2 of 2)
2022-01-10 09:15:20 -08:00
Bryce Wilson fb936595fa [MemoryBuiltins] Add field for alignment argument [NFC]
There are a few places where the alignment argument for AlignedAllocLike functions was previously hardcoded. This patch adds an getAllocAlignment function and a change to the MemoryBuiltin table to allow alignment arguments to be found generically.

This will shortly allow alignment inference on operator new's with align_val params and an extension to Attributor's HeapToStack.  The former will follow shortly - I split Bryce's patch for purpose of having the large change be NFC.  The later will be reviewed separately.

Differential Revision: https://reviews.llvm.org/D116851 (part 1 of 2)
2022-01-10 09:15:20 -08:00
Nikita Popov 92d55e7336 [MemoryBuiltins] Remove isNoAliasFn() in favor of isNoAliasCall()
We currently have two similar implementations of this concept:
isNoAliasCall() only checks for the noalias return attribute.
isNoAliasFn() also checks for allocation functions.

We should switch to only checking the attribute. SLC is responsible
for inferring the noalias return attribute for non-new allocation
functions (with a missing case fixed in
348bc76e35).
For new, clang is responsible for setting the attribute,
if -fno-assume-sane-operator-new is not passed.

Differential Revision: https://reviews.llvm.org/D116800
2022-01-10 09:18:15 +01:00
Philip Reames f38873537b [MemoryBuiltin] Cleanup stale todo comments [NFC]
strdup/strndup are already partially implemented, move remaining comment to relevant place.  Remaining named routines are copy routines and mostly handled via intrinsics already - they do not allocate new memory.
2022-01-07 13:57:20 -08:00
Philip Reames 6b0ff0969d Extract utility function for checking initial value of allocation [NFC, try 2]
This is a reoccuring pattern, we can consolidate three copies into one.  The main motivation is to reduce usages of isMallocLike.

The original commit (which was quickly reverted) didn't account for the allocation function could be an invoke, test coverage for that case added in this commit.
2022-01-07 08:44:08 -08:00
Philip Reames c6a0c1585a Revert "Extract utility function for checking initial value of allocation [NFC]"
This reverts commit 9ce30fe86f.  Appears to be causing a problem on a buildbot, revert while investigating.

https://green.lab.llvm.org/green//job/clang-stage1-RA/26818/consoleFull#-1502953973d489585b-5106-414a-ac11-3ff90657619c
2022-01-06 19:05:51 -08:00
Philip Reames 9ce30fe86f Extract utility function for checking initial value of allocation [NFC]
This is a reoccuring pattern, we can consolidate three copies into one.  The main motivation is to reduce usages of isMallocLike.
2022-01-06 18:02:14 -08:00
Philip Reames 5d1cfd4348 Remove unused LookThroughBitCast param in isXAllocLike functions [NFC]
This parameter took the non-default value exactly twice, and neither had semantic effect.
2022-01-06 18:02:13 -08:00
Philip Reames 7052670e96 Move getMallocAllocatedType and getMallocArraySize to GlobalOpt [NFC]
These are implementation details of the global-opt transform and not easily reuseable, so remove them from the analysis header.
2022-01-06 18:02:13 -08:00
Philip Reames 67a3331e4f Inline extractMallocCall to sole use and delete [NFC] 2022-01-06 18:02:13 -08:00
Philip Reames 4b0fc924a9 Delete unused extractCallocCall routine [NFC] 2022-01-06 18:02:13 -08:00
Philip Reames cffd268316 Demote getMallocType to implementation routine in MemoryBuiltins [NFC] 2022-01-06 18:02:13 -08:00