aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/MemoryBuiltins.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-09-01[MemoryBuiltins] Add getBaseObjectSize() (NFCI) (#155911)Nikita Popov1-0/+53
getObjectSize() is based on ObjectSizeOffsetVisitor, which has become very expensive over time. The implementation is geared towards computing as-good-as-possible results for the objectsize intrinsics and similar. However, we also use it in BasicAA, which is very hot, and really only cares about the base cases like alloca/malloc/global, not any of the analysis for GEPs, phis, or loads. Add a new getBaseObjectSize() API for this use case, which only handles the non-recursive cases. As a bonus, this API can easily return a TypeSize and thus support scalable vectors. For now, I'm explicitly discarding the scalable sizes in BasicAA just to avoid unnecessary behavior changes during this refactor.
2025-06-16[MemoryBuiltins] Support allocas in getInitialValueOfAllocation (NFC)Nikita Popov1-0/+3
2024-12-20[llvm] Bail out when meeting pointer with negative offset in approximated ↵serge-sans-paille1-3/+6
mode instead of … (#120424) …generating empty location Fix the regression detected by https://github.com/llvm/llvm-test-suite/pull/188
2024-12-10[llvm] Improve llvm.objectsize computation by computing GEP, alloca a… ↵serge-sans-paille1-4/+100
(#117849) …nd malloc parameters bound Using a naive expression walker, it is possible to compute valuable information for allocation functions, GEP and alloca, even in the presence of some dynamic information. We don't rely on computeConstantRange to avoid taking advantage of undefined behavior, which would be counter-productive wrt. usual llvm.objectsize usage. llvm.objectsize plays an important role in _FORTIFY_SOURCE definitions, so improving its diagnostic in turns improves the security of compiled application. As a side note, as a result of recent optimization improvements, clang no longer passes https://github.com/serge-sans-paille/builtin_object_size-test-suite This commit restores the situation and greatly improves the scope of code handled by the static version of __builtin_object_size. This is a recommit of https://github.com/llvm/llvm-project/pull/115522 with fix applied.
2024-11-23[llvm] Fix ObjectSizeOffsetVisitor behavior in exact mode upon negati… ↵serge-sans-paille1-7/+8
(#116955) …ve offset In Exact mode, the approximation of returning (0,0) is invalid. It only holds in min/max mode.
2024-11-20Revert "[llvm] Improve llvm.objectsize computation by computing GEP, alloca ↵Florian Mayer1-104/+4
and malloc parameters bound" (#117020) Reverts llvm/llvm-project#115522 This caused UBSan errors in multi-stage clang build: https://lab.llvm.org/buildbot/#/builders/25/builds/4241/steps/10/logs/stdio
2024-11-19[llvm] Improve llvm.objectsize computation by computing GEP, alloca and ↵serge-sans-paille1-4/+104
malloc parameters bound (#115522) Using a naive expression walker, it is possible to compute valuable information for allocation functions, GEP and alloca, even in the presence of some dynamic information. We don't rely on computeConstantRange to avoid taking advantage of undefined behavior, which would be counter-productive wrt. usual llvm.objectsize usage. llvm.objectsize plays an important role in _FORTIFY_SOURCE definitions, so improving its diagnostic in turns improves the security of compiled application. As a side note, as a result of recent optimization improvements, clang no longer passes https://github.com/serge-sans-paille/builtin_object_size-test-suite This commit restores the situation and greatly improves the scope of code handled by the static version of __builtin_object_size.
2024-11-18[llvm] Fix behavior of llvm.objectsize in presence of negative / large ↵serge-sans-paille1-8/+45
offset (#115504) The internal structure used to carry intermediate computations hold signed values. If an object size happens to overflow signed values, we can get invalid result, so make sure this situation never happens. This is not very limitative as static allocation of such large values should scarcely happen.
2024-11-07Revert "[llvm] Use computeConstantRange to improve llvm.objectsize ↵serge-sans-paille1-91/+4
computation (#114673)" This reverts commit 5f342816efe1854333f2be41a03fdd25fa0db433. This seems to break various builders, such as https://lab.llvm.org/buildbot/#/builders/41/builds/3259 https://lab.llvm.org/buildbot/#/builders/76/builds/4298
2024-11-07[llvm] Use computeConstantRange to improve llvm.objectsize computation (#114673)serge-sans-paille1-4/+91
Using LazyValueInfo, it is possible to compute valuable information for allocation functions, GEP and alloca, even in the presence of dynamic information. llvm.objectsize plays an important role in _FORTIFY_SOURCE definitions, so improving its diagnostic in turns improves the security of compiled application. As a side note, as a result of recent optimization improvements, clang no longer passes https://github.com/serge-sans-paille/builtin_object_size-test-suite This commit restores the situation and greatly improves the scope of code handled by the static version of __builtin_object_size.
2024-11-05[Analysis] Remove unused includes (NFC) (#114936)Kazu Hirata1-1/+0
Identified with misc-include-cleaner.
2024-11-02[llvm] Fix __builtin_object_size interaction between Negative Offset … ↵serge-sans-paille1-65/+80
(#111827) …and Select/Phi When picking a SizeOffsetAPInt through combineSizeOffset, the behavior differs if we're going to apply a constant offset that's positive or negative: If it's positive, then we need to compare the remaining bytes (i.e. Size - Offset), but if it's negative, we need to compare the preceding bytes (i.e. Offset). Fix #111709
2024-10-17[APInt] Fix APInt constructions where value does not fit bitwidth (NFCI) ↵Nikita Popov1-0/+2
(#80309) This fixes all the places that hit the new assertion added in https://github.com/llvm/llvm-project/pull/106524 in tests. That is, cases where the value passed to the APInt constructor is not an N-bit signed/unsigned integer, where N is the bit width and signedness is determined by the isSigned flag. The fixes either set the correct value for isSigned, set the implicitTrunc flag, or perform more calculations inside APInt. Note that the assertion is currently still disabled by default, so this patch is mostly NFC.
2024-08-13[MemoryBuiltins] Use getAllOnesValue()Nikita Popov1-1/+2
Split out from https://github.com/llvm/llvm-project/pull/80309.
2024-08-09[MemoryBuiltins] Simplify getCalledFunction() helper (NFC)Nikita Popov1-24/+12
If nobuiltin is set, directly return nullptr instead of using a separate out parameter and having all callers check this.
2024-08-09[MemoryBuiltins] Handle allocator attributes on call-siteNikita Popov1-31/+28
We should handle allocator attributes not only on function declarations, but also on the call-site. That way we can e.g. also optimize cases where the allocator function is a virtual function call. This was already supported in some of the MemoryBuiltins helpers, but not all of them. This adds support for allocsize, alloc-family and allockind("free").
2024-06-27[IR] Add getDataLayout() helpers to BasicBlock and Instruction (#96902)Nikita Popov1-1/+1
This is a helper to avoid writing `getModule()->getDataLayout()`. I regularly try to use this method only to remember it doesn't exist... `getModule()->getDataLayout()` is also a common (the most common?) reason why code has to include the Module.h header.
2024-06-24Revert "[IR][NFC] Update IRBuilder to use InsertPosition (#96497)"Stephen Tozer1-1/+1
Reverts the above commit, as it updates a common header function and did not update all callsites: https://lab.llvm.org/buildbot/#/builders/29/builds/382 This reverts commit 6481dc57612671ebe77fe9c34214fba94e1b3b27.
2024-06-24[IR][NFC] Update IRBuilder to use InsertPosition (#96497)Stephen Tozer1-1/+1
Uses the new InsertPosition class (added in #94226) to simplify some of the IRBuilder interface, and removes the need to pass a BasicBlock alongside a BasicBlock::iterator, using the fact that we can now get the parent basic block from the iterator even if it points to the sentinel. This patch removes the BasicBlock argument from each constructor or call to setInsertPoint. This has no functional effect, but later on as we look to remove the `Instruction *InsertBefore` argument from instruction-creation (discussed [here](https://discourse.llvm.org/t/psa-instruction-constructors-changing-to-iterator-only-insertion/77845)), this will simplify the process by allowing us to deprecate the InsertPosition constructor directly and catch all the cases where we use instructions rather than iterators.
2024-05-02[BoundsChecking] Handle vscale allocas (#90926)Vitaly Buka1-4/+4
2024-01-05[NFC][ObjectSizeOffset] Use classes instead of std::pair (#76882)Bill Wendling1-150/+158
The use of std::pair makes the values it holds opaque. Using classes improves this while keeping the POD aspect of a std::pair. As a nice addition, the "known" functions held inappropriately in the Visitor classes can now properly reside in the value classes. :-)
2023-10-19[MemoryBuiltins] Simplify getAllocFnKind() implementation (NFC)Nikita Popov1-4/+1
2023-09-27[ObjectSizeOffsetVisitor] Bail after visiting 100 instructions (#67479)Arthur Eubanks1-9/+25
We're running into stack overflows for huge functions with lots of phis. Even without the stack overflows, this is recursing >7000 in some auto-generated code. This fixes the stack overflow and brings down the compile time to something reasonable.
2023-09-22[NFC][ObjectSizeOffsetVisitor] Remove redundant equality checkArthur Eubanks1-1/+1
Originally suggested in https://reviews.llvm.org/D131001 but I accidentally took only have of the suggestion.
2023-09-15[MemoryBuiltins] Cache the result of ObjectOffsetSizeVisitor::visit. #64796 ↵Bevin Hansson1-4/+8
(#65326) visit will skip visiting instructions it already has visited to avoid issues with cycles in the data graph. However, the result of this skipping behavior is that if we encounter the same instruction twice, and that instruction has a well defined result and isn't part of a cycle, we will introduce unknowns into the analysis even though we knew the size and offset of the instruction's result. Instead of skipping such instructions, keep a cache of the result of visiting them. This result is initialized to unknown() before visiting, so if we happen to visit it again recursively (perhaps as the result of a cycle or a phi), we will get unknown as the cached result and exit out.
2023-09-11[NFC][RemoveDIs] Use iterators over inst-pointers when using IRBuilderJeremy Morse1-3/+4
This patch adds a two-argument SetInsertPoint method to IRBuilder that takes a block/iterator instead of an instruction, and updates many call sites to use it. The motivating reason for doing this is given here [0], we'd like to pass around more information about the position of debug-info in the iterator object. That necessitates passing iterators around most of the time. [0] https://discourse.llvm.org/t/rfc-instruction-api-changes-needed-to-eliminate-debug-intrinsics-from-ir/68939 Differential Revision: https://reviews.llvm.org/D152468
2023-07-03[InstSimplify] Fold all global variables with initializersAnshil Gandhi1-1/+3
Allow computing size of interposable or externally initializable global variables. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D152145
2023-06-23[InstCombine] Track inserted instructions when lowering objectsizeNikita Popov1-5/+9
The inserted instructions can usually be simplified. Make sure this happens in the same InstCombine iteration by adding them to the worklist. We happen to get some better optimization in two cases, but this is just a lucky accident. https://github.com/llvm/llvm-project/issues/63472 tracks implementing a fold for that case. This doesn't track all inserted instructions yet, for that we would also have to include those created by ObjectSizeOffsetEvaluator.
2023-06-14Revert "[InstSimplify] Fold all global variables with initializers"Alan Zhao1-3/+1
This reverts commit 17b7df3daee85c1a4d1d955e558d42b34ce17549. Reason: causes chrome builds to crash: https://crbug.com/1454861
2023-06-13[InstSimplify] Fold all global variables with initializersAnshil Gandhi1-1/+3
Allow computing size of interposable or externally initializable global variables. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D152145
2023-05-31[MemoryBuiltins] Handle phi nodes without operands (PR63013)Nikita Popov1-0/+2
Conservatively return unknown in this degenerate case. This is hard to hit in practice, because such phis are usually optimized away before they reach a getObjectSize() call. Fixes https://github.com/llvm/llvm-project/issues/63013.
2023-04-28[MemProf] Use updated version of hot/cold operator newTeresa Johnson1-8/+8
Switch to the just updated versions of the API in tcmalloc that change the name of the hot cold paramter to a reserved identifier __hot_cold_t. This was based on feedback from Richard Smith, as I also need to add some follow-on handling to clang so they are annotated properly. Differential Revision: https://reviews.llvm.org/D149475
2023-04-19[MemProf] Optionally pass hot/cold hints to operator newTeresa Johnson1-0/+8
Optionally (off by default) replace operator new() calls marked with a hot or cold memprof attribute with an operator new() call that takes a hot_cold_t parameter. Currently this is supported by the open source version of tcmalloc, see: https://github.com/google/tcmalloc/blob/master/tcmalloc/new_extension.h Differential Revision: https://reviews.llvm.org/D148718
2023-03-28[llvm] Use pointer index type for more GEP offsets (pre-codegen)Krzysztof Drewniak1-4/+5
Many uses of getIntPtrType() were using that type to calculate the neened type for GEP offset arguments. However, some time ago, DataLayout was extended to support pointers where the size of the pointer is not equal to the size of the values used to index it. Much code was already migrated to, for example, use getIndexSizeInBits instead of getPtrSizeInBits, but some rewrites still used getIntPtrType() to get the type for GEP offsets. This commit changes uses of getIntPtrType() to getIndexType() where they are involved in a GEP-related calculation. In at least one case (bounds check insertion) this resolves a compiler crash that the new test added here would previously trigger. This commit does not impact - C library-related rewriting (memcpy()), which are operating under the assumption that intptr_t == size_t. While all the mechanisms for breaking this assumption now exist, doing so is outside the scope of this commit. - Code generation and below. Note that the use of getIntPtrType() in CodeGenPrepare will be changed in a future commit. - Usage of getIntPtrType() in any backend Depends on D143435 Reviewed By: arichardson Differential Revision: https://reviews.llvm.org/D143437
2023-01-11[NFC] Use TypeSize::getKnownMinValue() instead of TypeSize::getKnownMinSize()Guillaume Chatelet1-1/+1
This change is one of a series to implement the discussion from https://reviews.llvm.org/D141134.
2022-12-29Detemplate llvm::EmitGEPOffset and move it into a cpp file. NFC.Benjamin Kramer1-1/+1
2022-12-16std::optional::value => operator*/operator->Fangrui Song1-2/+2
value() has undesired exception checking semantics and calls __throw_bad_optional_access in libc++. Moreover, the API is unavailable without _LIBCPP_NO_EXCEPTIONS on older Mach-O platforms (see _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS). This commit fixes LLVMAnalysis and its dependencies.
2022-12-14Don't include Optional.hKazu Hirata1-1/+0
These files no longer use llvm::Optional.
2022-12-14[Analysis] llvm::Optional => std::optionalFangrui Song1-1/+1
2022-12-11[Analysis] Use std::optional in MemoryBuiltins.cpp (NFC)Kazu Hirata1-17/+18
This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-09[MemoryBuiltins] Avoid comparing against Type::getInt8PtrTy(0)Alex Richardson1-2/+2
This does not make sense with opaque pointers, and also caused issues for CHERI/Morello where hardcoding address-space zero prevented optimization. Downstream change: https://git.morello-project.org/morello/llvm-project/-/merge_requests/180 Co-authored-by: Silviu Baranga <silviu.baranga@arm.com> Reviewed By: lebedev.ri Differential Revision: https://reviews.llvm.org/D139708
2022-12-09[MemoryBuiltins] Remove unused TLI parameters (NFC)Nikita Popov1-3/+2
2022-12-09[MemoryBuiltins] Remove CallocLike (NFC)Nikita Popov1-22/+5
All functions of this kind already use allocator attributes. This also highlights that isMallocOrCallocLikeFn() doesn't really do what the name says (notably, it does not check for allocator attributes). The places it is used in are also very dubious, so we'll want to remove it.
2022-12-09[MemoryBuiltins] Remove AlignedAllocLike (NFC)Nikita Popov1-12/+5
All functions that formerly had this AllocType now use allocator attributes, so drop it.
2022-12-09[MemoryBuiltins] Drop ReallocLike type (NFC)Nikita Popov1-9/+3
All realloc style functions have been migrated to use allocator attributes, so we no longer need to check for this.
2022-12-09[MemoryBuiltins] Remove some hardcoded builtinsNikita Popov1-5/+0
For all of these we already infer the new memory attributes, so they don't need to be explicitly listed.
2022-12-06[ADT] Don't including None.h (NFC)Kazu Hirata1-1/+0
These source files no longer use None, so they do not need to include None.h. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02[Analysis] Use std::nullopt instead of None (NFC)Kazu Hirata1-20/+20
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02Attributes: convert Optional to std::optionalKrzysztof Parzyszek1-1/+2
2022-11-16MemoryBuiltins: Don't check for unsized allocasMatt Arsenault1-3/+0
The verifier rejects these.