aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/BasicAliasAnalysis.cpp
AgeCommit message (Collapse)AuthorFilesLines
12 days[BasicAA] Handle scalable vectors in new errno aliasing checks. (#159248)David Green1-1/+2
This is a minor fixup for scalable vectors after f9f62ef4ae555a. It handles them in the same way as other memory locations that are larger than errno, preventing the failure on implicit conversion from a scalable location.
13 days[AA] Refine ModRefInfo taking into account `errnomem` locationAntonio Frighetto1-1/+24
Ensure alias analyses mask out `errnomem` location, refining the resulting modref info, when the given access/location does not alias errno. This may occur either when TBAA proves there is no alias with errno (e.g., float TBAA for the same root would be disjoint with the int-only compatible TBAA node for errno); or if the memory access size is larger than the integer size, or when the underlying object is a potentially-escaping alloca. Previous discussion: https://discourse.llvm.org/t/rfc-modelling-errno-memory-effects/82972.
2025-09-01[MemoryBuiltins] Add getBaseObjectSize() (NFCI) (#155911)Nikita Popov1-3/+6
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-08-29[Analysis] Remove an unnecessary cast (NFC) (#155977)Kazu Hirata1-3/+3
getParent() already returns Function *.
2025-06-25[FunctionAttrs][IR] Fix memory attr inference for volatile mem intrinsics ↵Nikita Popov1-0/+4
(#122926) Per LangRef volatile operations can read and write inaccessible memory: > any volatile operation can read and/or modify state which is not > accessible via a regular load or store in this module Model this by adding inaccessible memory effects in getMemoryEffects() if the operation is volatile. In the future, we should model volatile using operand bundles instead. Fixes https://github.com/llvm/llvm-project/issues/120932.
2025-06-12[AA] Take read-only provenance captures into account (#143097)Nikita Popov1-35/+51
Update the AA CaptureAnalysis providers to return CaptureComponents, so we can distinguish between full provenance and read-only provenance captures. Use this to restrict "other" memory effects on call from ModRef to Ref. Ideally we would also apply the same reasoning for escape sources, but the current API cannot actually convey the necessary information (we can only say NoAlias or MayAlias, not MayAlias but only via Ref).
2025-06-11[BasicAA][ValueTracking] Use MaxLookupSearchDepth constant (NFC)Nikita Popov1-4/+0
Use MaxLookupSearchDepth in all places limiting an underlying object walk, instead of hardcoding 6 in various places.
2025-06-06[AA] Merge isNonEscapingLocalObject() into SimpleCaptureAnalysis (NFC) (#142971)Nikita Popov1-1/+11
isNonEscapingLocalObject() is only used by SimpleCaptureAnalysis and tightly integrated with its implementation (in particular its cache), so inline and simplify the implementation.
2025-06-03[ValueTracking] Make Depth last default arg (NFC) (#142384)Ramkumar Ramachandra1-2/+1
Having a finite Depth (or recursion limit) for computeKnownBits is very limiting, but is currently a load-bearing necessity, as all KnownBits are recomputed on each call and there is no caching. As a prerequisite for an effort to remove the recursion limit altogether, either using a clever caching technique, or writing a easily-invalidable KnownBits analysis, make the Depth argument in APIs in ValueTracking uniformly the last argument with a default value. This would aid in removing the argument when the time comes, as many callers that currently pass 0 explicitly are now updated to omit the argument altogether.
2025-05-26[llvm] Value-initialize values with *Map::try_emplace (NFC) (#141522)Kazu Hirata1-1/+1
try_emplace value-initializes values, so we do not need to pass nullptr to try_emplace when the value types are raw pointers or std::unique_ptr<T>.
2025-05-06[BasicAA] Gracefully handle large LocationSize (#138528)Nikita Popov1-3/+5
If the LocationSize is larger than the index space of the pointer type, bail out instead of triggering an APInt assertion. Fixes the issue reported at https://github.com/llvm/llvm-project/pull/119365#issuecomment-2849874894.
2025-05-05[AA] Assert that alias() arguments are pointers (#138242)Nikita Popov1-3/+0
Assert instead of returning NoAlias for non-pointers. This makes sure that people don't confuse alias (working on locations) with getModRefInfo (working on instructions).
2025-04-29[BasicAA] Account for wrapping when using abs(Scale*V0 + (-Scale)*V1) >= ↵Craig Topper1-16/+18
abs(Scale) (#137755) Similar to 1b7ef6aac8a3cad245c0ed14fe21725e31261f73, add a check to only set MinAbsVarIndex if abs(Scale*V0) and abs((-Scale)*V1) won't wrap. In the absence of IsNSW, try to use the bitwidths of the original V and Scale to rule out wrapping
2025-04-21[LLVM] Cleanup pass initialization for Analysis passes (#135858)Rahul Joshi1-3/+1
- Do not call pass initialization from pass constructors. - Instead, pass initialization should happen in the `initializeAnalysis` function. - https://github.com/llvm/llvm-project/issues/111767
2025-04-08[BasicAA] Treat ExtractValue(Argument) similar to Argument in relation to ↵David Green1-2/+12
function-local objects. (#134716) This is a much smaller, technically orthogonal patch similar to #134505. It states that a extractvalue(Argument) can be treated like an Argument for alias analysis, where the extractelement acts like a phi / copy. No inttoptr here.
2025-03-19[AA][BasicAA] Move more call logic to BasicAA (#131144)Nikita Popov1-56/+48
Currently, the handling for calls is split between AA and BasicAA in an awkward way. BasicAA does argument alias analysis for non-escaping objects (but without considering MemoryEffects), while AA handles the generic case using MemoryEffects. However, fundamentally, both of these are really trying to do the same thing. The new merged logic first tries to remove the OtherMR component of the memory effects, which includes accesses to escaped memory. If a function-local object does not escape, OtherMR can be set to NoModRef. Then we perform the argument scan in basically the same way as AA previously did. However, we also need to look at the operand bundles. To support that, I've adjusted getArgModRefInfo to accept operand bundle arguments.
2025-03-13[CaptureTracking][AA] Only consider provenance captures (#130777)Nikita Popov1-1/+1
For the purposes of alias analysis, we should only consider provenance captures, not address captures. To support this, change (or add) CaptureTracking APIs to accept a Mask and StopFn argument. The Mask determines which components we are interested in (for AA that would be Provenance). The StopFn determines when we can abort the walk early. Currently, we want to do this as soon as any of the components in the Mask is captured. The purpose of making this a separate predicate is that in the future we will also want to distinguish between capturing full provenance and read-only provenance. In that case, we can only stop early once full provenance is captured. The earliest escape analysis does not get a StopFn, because it must always inspect all captures.
2025-02-24[CaptureTracking] Remove StoreCaptures parameter (NFC)Nikita Popov1-1/+1
The implementation doesn't use it, and is unlikely to use it in the future. The places that do set StoreCaptures=false, do so incorrectly and would be broken if the parameter actually did anything.
2025-02-01[ValueTracking] Use `SimplifyQuery` in `isKnownNonEqual` (#124942)Yingwei Zheng1-2/+4
It is needed by https://github.com/llvm/llvm-project/pull/117442.
2024-12-13[BasicAA] Do not decompose past casts with different index width (#119365)Nikita Popov1-39/+26
BasicAA currently tries to support addrspacecasts that change the index width by performing the decomposition in the maximum of all index widths and then trying to fix this up with in-place sign extends to get correct overflow behavior if the actual index width is smaller. However, even in the case where we don't mix different index widths and just have an index width that is smaller than the maximum, the behavior is incorrect (see test), because we only perform the index width adjustment during decomposition and not any of the later logic -- and we don't do anything at all for variable offsets. I'm sure that the case where we actually mix different index widths is even more broken than that. Fix this by not allowing decomposition through index width changes. If the pointers have different index widths, fall back to a base object comparison, ignoring the offsets.
2024-12-09[AA] Export the isBaseOfObject() API (NFC)Nikita Popov1-13/+0
This is also useful outside BasicAA.
2024-12-03[BasicAA] Treat returns_twice functions as clobbering unescaped objects ↵Nikita Popov1-1/+7
(#117902) Effectively this models all the accesses that occur between the first and second return as happening at the point of the call. Fixes https://github.com/llvm/llvm-project/issues/116668.
2024-11-27[BasicAA][LAA] Don't use same-block phis in cross iteration mode (#116802)Nikita Popov1-2/+3
In 4de3184f07fd8c548125d315dd306d4afa7c9698 we exposed BasicAA's cross-iteration mode for use in LAA, so we can handle selects with equal conditions correctly (where the select condition is not actually equal across iterations). However, if we replace the selects with equivalent phis, the issue still exists. In the phi case, we effectively still have an assumption that the condition(s) that control which phi arg is used will be the same across iterations. Fix this by disabling this phi handling in cross-iteration mode. (I'm not entirely sure whether this is also needed when BasicAA enables cross-iteration mode during internal phi recursion, but I wouldn't be surprised if that's the case.)
2024-11-20[AA] Rename CaptureInfo -> CaptureAnalysis (NFC) (#116842)Nikita Popov1-10/+12
I'd like to use the name CaptureInfo to represent the new attribute proposed at https://discourse.llvm.org/t/rfc-improvements-to-capture-tracking/81420, but it's already taken by AA, and I can't think of great alternatives (CaptureEffects would be something of a stretch). As such, I'd like to rename CaptureInfo -> CaptureAnalysis in AA, which also seems like the more accurate terminology.
2024-10-07[Analysis] Simplify code with DenseMap::operator[] (NFC) (#111331)Kazu Hirata1-4/+2
2024-10-01BasicAA: update comments in a routine (NFC) (#110492)Ramkumar Ramachandra1-13/+8
The comments in isObjectSmallerThan are outdated, as it is only ever called with the underlying object as the first argument. Update the comments to reflect this.
2024-09-02[BasicAA] Track nuw through decomposed expressions (#106512)Nikita Popov1-10/+21
When we decompose the GEP offset expression, and the arithmetic is not performed using nuw operations, we cannot retain the nuw flag on the decomposed GEP. For example, if we have `gep nuw p, (a-1)`, this is not at all the same as `gep nuw (gep nuw p, a), -1`. Fix this by tracking NUW through linear expression decomposition, similarly to what we already do for the NSW flag. This fixes the miscompilation reported in https://github.com/llvm/llvm-project/pull/105496#issuecomment-2315322220.
2024-08-20[BasicAA] Use nuw attribute of GEPs (#98608)Hari Limaye1-20/+49
Use the nuw attribute of GEPs to prove that pointers do not alias, in cases matching the following: + + + | BaseOffset | +<nuw> Indices | ---------------->|-------------------->| |-->V2Size | |-------> V1Size LHS RHS If the difference between pointers is Offset +<nuw> Indices then we know that the addition does not wrap the pointer index type (add nuw) and the constant Offset is a lower bound on the distance between the pointers. We can then prove NoAlias via Offset u>= V2Size.
2024-08-14BasicAA: Fix assert when indexing address spaces with different sizes (#103713)Matt Arsenault1-0/+3
Fixes #103500
2024-08-14[BasicAA] Remove unused variables (NFC)Nikita Popov1-3/+0
Split out from #98608.
2024-08-07[BasicAA] Make use of nusw+nuw -> nneg implication (#102141)Nikita Popov1-2/+8
If the GEP is both nuw and inbounds/nusw, the offset is non-negative. Pass this information to CastedValue and make use of it when determining the value range. Proof for nusw+nuw->nneg: https://alive2.llvm.org/ce/z/a_CKAw Proof for the test case: https://alive2.llvm.org/ce/z/yJ3ymP
2024-08-06[BasicAA] Check nusw instead of inboundsNikita Popov1-1/+1
For the offset scaling, this is sufficient to guarantee nsw. The other checks for inbounds in this file do need proper inbounds.
2024-07-25[BasicAA] Fix handling of indirect assumption based results (#100130)Nikita Popov1-4/+24
If a result is potentially based on a not yet proven assumption, BasicAA will remember it inside AssumptionBasedResults and remove the cache entry if an assumption higher up is later disproved. However, we currently miss the case where another cache entry ends up depending on such an AssumptionBased result. Fix this by introducing an additional AssumptionBased state for cache entries. If such a result is used, we'll still increment AAQI.NumAssumptionUses, which means that the using entry will also become AssumptionBased and be cleared if the assumption is disproved. At the end of the root query, convert remaining AssumptionBased results into definitive results. Fixes https://github.com/llvm/llvm-project/issues/98978.
2024-06-28[IR] Add getDataLayout() helpers to Function and GlobalValue (#96919)Nikita Popov1-2/+2
Similar to https://github.com/llvm/llvm-project/pull/96902, this adds `getDataLayout()` helpers to Function and GlobalValue, replacing the current `getParent()->getDataLayout()` pattern.
2024-06-04[BasicAA] Consider 'nneg' flag when comparing CastedValues (#94129)Alex MacLean1-19/+43
Any of the `zext` bits in a `zext nneg` can be converted to `sext` but when checking if casts are compatible `BasicAA` fails to take into account `nneg`. This change adds tracking of `nneg` to the `CastedValue` struct and ensures that `sext` and `zext` bits are treated as interchangeable when either `CastedValue` has a `nneg`. When distributing casted values in `GetLinearExpression` we conservatively discard the `nneg` from the `CastedValue`, except in the case of `shl nsw`, where we know the sign has not changed to negative.
2024-04-23[SimplifyQuery] Avoid PatternMatch.h include (NFC)Nikita Popov1-0/+1
Move the one method that uses it out of line. This is primarily to reduce the number of files to rebuild when changing PatternMatch.h.
2024-04-16[ValueTracking] Restore isKnownNonZero parameter order. (#88873)Harald van Dijk1-2/+1
Prior to #85863, the required parameters of llvm::isKnownNonZero were Value and DataLayout. After, they are Value, Depth, and SimplifyQuery, where SimplifyQuery is implicitly constructible from DataLayout. The change to move Depth before SimplifyQuery needed callers to be updated unnecessarily, and as commented in #85863, we actually want Depth to be after SimplifyQuery anyway so that it can be defaulted and the caller does not need to specify it.
2024-04-12[ValueTracking] Convert `isKnownNonZero` to use SimplifyQuery (#85863)Yingwei Zheng1-1/+2
This patch converts `isKnownNonZero` to use SimplifyQuery. Then we can use the context information from `DomCondCache`. Fixes https://github.com/llvm/llvm-project/issues/85823. Alive2: https://alive2.llvm.org/ce/z/QUvHVj
2024-02-12[BasicAA] Treat different VScale intrinsics as the same value. (#81152)David Green1-2/+10
The IR may contain multiple llvm.vscale intrinsics that have not been CSEd. This patch ensures that multiple vscales can be treated the same, either in the decomposition of geps and when we subtract one decomposition from another.
2024-02-12[BasicAA] Check for Overflow using vscale_range (#81144)David Green1-7/+17
This extends #80818 when IsNSW is lost (possibly due to looking through multiple GEPs), to check the vscale_range for an access that will not overflow even with the maximum range.
2024-02-09[BasicAA] Fix Scale check in vscale aliasing. (#81174)David Green1-1/+1
This is a fix for #80818, as pointed out in #81144 it should be checking the abs of Scale. The added test changes from NoAlias to MayAlias.
2024-02-08[BasicAA] Scalable offset with scalable typesize. (#80818)David Green1-0/+21
This patch adds a simple alias analysis check for accesses that are scalable with a offset between them that is also trivially scalable (there are no other constant/variable offsets). We essentially divide each side by vscale and are left needing to check that the offset >= typesize.
2024-02-05[BasicAA] Handle scalable type sizes with constant offsets (#80445)David Green1-18/+28
This is a separate, but related issue to #69152 that was attempting to improve AA with scalable dependency distances. This patch attempts to improve when there are scalable accesses with a constant offset between them. We happen to get a report of such a thing recently, where so long as the vscale_range is known, the maximum size of the access can be assessed and better aliasing results can be returned. The Upper range of the vscale_range, along with known part of the typesize are used to prove that Off >= CR.upper * LSize. It does not try to produce PartialAlias results at the moment from the lower vscale_range. It also enables the added benefit of allowing better alias analysis when the RHS of the two values is scalable, but the LHS is normal and can be treated like any other aliasing query.
2024-01-31[AA][JumpThreading] Don't use DomTree for AA in JumpThreading (#79294)Nikita Popov1-2/+4
JumpThreading may perform AA queries while the dominator tree is not up to date, which may result in miscompilations. Fix this by adding a new AAQI option to disable the use of the dominator tree in BasicAA. Fixes https://github.com/llvm/llvm-project/issues/79175.
2024-01-17[BasicAA] Remove incorrect rule about constant pointers (#76815)Nikita Popov1-10/+9
BasicAA currently says that any Constant cannot alias an identified local object. This is not correct if the local object escaped, as it's possible to create a pointer to the escaped object using an inttoptr constant expression base. To compensate for this, make sure that inttoptr constant expressions are treated as escape sources, just like inttoptr instructions. This ensures that the optimization can still be applied if the local object is non-escaping. This is sufficient to still optimize the original motivating case from c53e2ecf0296a55d3c33c19fb70a3aa7f81f2732. Fixes https://github.com/llvm/llvm-project/issues/76789.
2024-01-16[BasicAA] Handle disjoint or as add in DecomposeGEP. (#78209)David Green1-4/+2
This removes the MaskedValueIsZero check in decomposing geps in BasicAA, using the isDisjoint flags instead. This relies on the disjoint flags being present when AA is ran. The alternative would be to keep the old MaskedValueIsZero check too if this causes issues.
2024-01-04[BasicAA] Guess reasonable contexts for separate storage hints (#76770)David Goldblatt1-5/+25
The definition of the pointer of the memory location being queried is always one such context. Even this conservative guess can be better than no guess at all in some cases. Fixes #64666 Co-authored-by: David Goldblatt <davidgoldblatt@meta.com>
2024-01-04[IR] Fix GEP offset computations for vector GEPs (#75448)Jannik Silvanus1-2/+2
Vectors are always bit-packed and don't respect the elements' alignment requirements. This is different from arrays. This means offsets of vector GEPs need to be computed differently than offsets of array GEPs. This PR fixes many places that rely on an incorrect pattern that always relies on `DL.getTypeAllocSize(GTI.getIndexedType())`. We replace these by usages of `GTI.getSequentialElementStride(DL)`, which is a new helper function added in this PR. This changes behavior for GEPs into vectors with element types for which the (bit) size and alloc size is different. This includes two cases: * Types with a bit size that is not a multiple of a byte, e.g. i1. GEPs into such vectors are questionable to begin with, as some elements are not even addressable. * Overaligned types, e.g. i16 with 32-bit alignment. Existing tests are unaffected, but a miscompilation of a new test is fixed. --------- Co-authored-by: Nikita Popov <github@npopov.com>
2024-01-03[BasicAA] Enable separate storage hints by default (#76864)David Goldblatt1-1/+1
As requested in https://github.com/llvm/llvm-project/pull/76770#pullrequestreview-1801649466 A few months of experimentation in a large codebase did not reveal any significant build speed regressions, and b07bf16 speeds up hint lookup even further. Co-authored-by: David Goldblatt <davidgoldblatt@meta.com>
2024-01-03[AssumptionCache] Add affected values for separate_storage (#76806)Nikita Popov1-20/+17
Add the underlying object of both separate_storage arguments as affected values. This allows us to use assumptionsFor() in BasicAA, which will be more efficient if there are many assumes in the function.