aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/BasicAliasAnalysis.cpp
AgeCommit message (Collapse)AuthorFilesLines
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.
2023-11-29[ValueTracking] Convert MaskedValueIsZero() to use SimplifyQuery (NFC)Nikita Popov1-2/+2
2023-11-22[llvm][TypeSize] Fix addition/subtraction in TypeSize. (#72979)Sander de Smalen1-2/+2
It seems TypeSize is currently broken in the sense that: TypeSize::Fixed(4) + TypeSize::Scalable(4) => TypeSize::Fixed(8) without failing its assert that explicitly tests for this case: assert(LHS.Scalable == RHS.Scalable && ...); The reason this fails is that `Scalable` is a static method of class TypeSize, and LHS and RHS are both objects of class TypeSize. So this is evaluating if the pointer to the function Scalable == the pointer to the function Scalable, which is always true because LHS and RHS have the same class. This patch fixes the issue by renaming `TypeSize::Scalable` -> `TypeSize::getScalable`, as well as `TypeSize::Fixed` to `TypeSize::getFixed`, so that it no longer clashes with the variable in FixedOrScalableQuantity. The new methods now also better match the coding standard, which specifies that: * Variable names should be nouns (as they represent state) * Function names should be verb phrases (as they represent actions)
2023-11-21[BasicAA] Optimize index size adjustment (NFC)Nikita Popov1-4/+7
In most cases we do not actually have to perform an index size adjustment. Don't perform any APInt operations in that case.
2023-11-21[BasicAA] Don't use MinAbsVarIndex = 1. (#72993)Florian Hahn1-3/+0
The current code incorrectly assumed that the absolute variable index needs to be at least 1, if the variable is != 0. This is incorrect, in case multiplying with Scale wraps. The code below already checks for wrapping properly, so just remove the incorrect assignment. Fixes https://github.com/llvm/llvm-project/issues/72831.
2023-11-21[BasicAA] Don't assume DT is nonnullNikita Popov1-4/+4
I thought DT is required in BasicAA, but apparently it can be null in unit tests at least. This should fix the ubsan bot failures.
2023-11-21[BasicAA] Make isNotCapturedBeforeOrAt() check for calls more precise (#69931)Nikita Popov1-15/+23
For calls, we are only interested in captures before the call, not captures by the call itself -- arguments that get passed to the call are checked explicitly. In particular, the current implementation is not optimal if the pointer is captured via a readonly argument -- in that case, we know that even if the argument is captured, the call will not modify the argument (at least not via that argument). Make this more precise by renaming to isCapturedBefore() and adding an OrAt argument that allows us to toggle whether to consider captures in the instruction itself or not.
2023-11-02Revert "[CaptureTracking] Ignore ephemeral values when determining po… ↵Florian Hahn1-1/+1
(#71066) Unfortunately the commit (D123162) introduced a mis-compile (https://github.com/llvm/llvm-project/issues/70547), which wasn't fixed by the alternative fix (c0de28b92e98acbeb73) I think as long as the call considered as ephemeral is not removed, we need to be conservative. To address the correctness issue quickly, I think we should revert the patch (as this patch does, it doens't revert cleanly) This reverts commit 17fdaccccfad9b143e4aadbcdda7f645de127153. Fixes https://github.com/llvm/llvm-project/issues/70547
2023-10-27Replace TypeSize::{getFixed,getScalable} with canonical ↵Fangrui Song1-2/+2
TypeSize::{Fixed,Scalable}. NFC
2023-10-24[Analysis] Add Scalable field in MemoryLocation.h (#69716)Harvin Iriawan1-13/+21
This is the first of a series of patch to improve Alias Analysis on Scalable quantities. Keep Scalable information from TypeSize which will be used in Alias Analysis.
2023-10-24[BasicAA] Update comment (NFC)Nikita Popov1-1/+1
2023-10-23[BasicAA] Return std::optional from getObjectSize() (NFC)Nikita Popov1-10/+12
Prefer this over UnknownSize, especially once the return value switches to something other than uint64_t.
2023-10-20[AA] Make LI and EphValues option in EarliestEscapeInfo (NFC)Nikita Popov1-1/+1
To allow using it in places where these may not be available.
2023-10-16[BasicAA] Remove NSW flags when merging scales (#69122)Yingwei Zheng1-0/+1
When merging scales of `LinearExpression` that have common index variables, we cannot guarantee the NSW flag still applies to the merged expression. Fixes #69096.
2023-07-28[AA] Skip the layer of indirection in returning conservative results.David Goldblatt1-9/+9
Historically, AA implementations chained to a following implementation to answer recursive queries. This is no longer the case, but the legacy lives on in a confusing phrasing of the return-a-conservative-value paths. Let's just return "don't know" directly, where appropriate; the current two-step way is confusing. Differential Revision: https://reviews.llvm.org/D149100
2023-06-27[BasicAA] Fix nsw handling for negated scales (PR63266)Nikita Popov1-7/+34
We currently preserve the nsw flag when negating scales, which is incorrect for INT_MIN. However, just dropping the NSW flag in this case makes BasicAA behavior unreliable and asymmetric, because we may or may not drop the NSW flag depending on which side gets subtracted. Instead, leave the Scale alone and add an additional IsNegated flag, which indicates that the whole VarIndex should be interpreted as a subtraction. This allows us to retain the NSW flag. When accumulating the offset range, we need to use subtraction instead of adding for IsNegated indices. Everything else works on the absolute value of the scale, so the negation does not matter there. Fixes https://github.com/llvm/llvm-project/issues/63266. Differential Revision: https://reviews.llvm.org/D153270
2023-06-26[BasicAA] Don't short-circuit non-capturing argumentsNikita Popov1-9/+6
This is an alternative to D153464. BasicAA currently assumes that an unescaped alloca cannot be read through non-nocapture arguments of a call, based on the argument that if the argument were based on the alloca, it would not be unescaped. This currently fails in the case where the call is an ephemeral value and as such does not count as a capture. It also happens for calls that are readonly+nounwind+void, though that case tends to not matter in practice, because such calls will get DCEd anyway. Differential Revision: https://reviews.llvm.org/D153511
2023-05-31[Analysis] Remove unused class LegacyAARGetterKazu Hirata1-7/+0
The last use was removed by: commit fa6ea7a419f37befbed04368bcb8af4c718facbb Author: Arthur Eubanks <aeubanks@google.com> Date: Mon Mar 20 11:18:35 2023 -0700 Once we remove it, createLegacyPMAAResults and createLegacyPMAAResults become unused, so this patch removes them as well. Differential Revision: https://reviews.llvm.org/D151787
2023-03-03[InstCombine] Simplify separate_storage assumptionsDavid Goldblatt1-0/+2
Before this change, we call getUnderlyingObject on each separate_storage operand on every alias() call (potentially requiring lots of pointer chasing). Instead, we rewrite the assumptions in instcombine to do this pointer-chasing once. We still leave the getUnderlyingObject calls in alias(), just expecting them to be no-ops much of the time. This is relatively fast (just a couple dyn_casts with no pointer chasing) and avoids making alias analysis results depend on whether or not instcombine has been run. Differential Revision: https://reviews.llvm.org/D144933
2023-02-19Use APInt::count{l,r}_{zero,one} (NFC)Kazu Hirata1-2/+2
2023-01-11[NFC] Use TypeSize::geFixedValue() instead of TypeSize::getFixedSize()Guillaume Chatelet1-2/+2
This change is one of a series to implement the discussion from https://reviews.llvm.org/D141134.
2022-12-16[AA][Intrinsics] Add separate_storage assumptions.David Goldblatt1-3/+32
This operand bundle on an assume informs alias analysis that the arguments point to regions of memory that were allocated separately (i.e. different heap allocations, different allocas, or different globals). As a safety measure, we leave the analysis flag-disabled by default. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D136514
2022-12-15[AA] Allow for flow-sensitive analyses.David Goldblatt1-2/+2
All current analyses ignore the context. We make the argument mandatory for analyses, but optional for the query interface. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D136512
2022-12-12[BasicAA] Remove support for PhiValues analysisNikita Popov1-54/+28
BasicAA currently has an optional dependency on the PhiValues analysis. However, at least with our current pipeline setup, we never actually make use of it. It's possible that this used to work with the legacy pass manager, but I'm not sure of that either. Given that this analysis has not actually been in use for a long time, and nobody noticed or complained, I think we should drop support for it and focus on one code path. It is worth noting that analysis quality for the non-PhiValues case has significantly improved in the meantime. If we really wanted to make use of PhiValues, the right way would probably be to pass it in via AAQI in places we want to use it, rather than using an optional pass manager dependency (which are an unpredictable PITA and should really only ever be used for analyses that are only preserved and not used). Differential Revision: https://reviews.llvm.org/D139719
2022-12-10[llvm] Use std::optional instead of None in comments (NFC)Kazu Hirata1-1/+1
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[BasicAA] Handle phi with itself as incoming valueNikita Popov1-0/+4
We can skip such incoming values. This was already done by PhiValues if present, but we can also do this without the additional analysis.
2022-12-06[BasicAA] Guard against empty successors list (PR59360)Nikita Popov1-1/+2
Succs can be empty here if a phi predecessor is unreachable. Fixes https://github.com/llvm/llvm-project/issues/59360
2022-12-02[Analysis] Use std::nullopt instead of None (NFC)Kazu Hirata1-1/+1
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-02Use CTAD on llvm::SaveAndRestoreJan Svoboda1-1/+1
Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D139229
2022-11-25[Analysis] Use std::optional in BasicAliasAnalysis.cpp (NFC)Kazu Hirata1-3/+4
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-11-14[AA] Move MayBeCrossIteration into AAQI (NFC)Nikita Popov1-17/+22
Move the MayBeCrossIteration flag from BasicAA into AAQI. This is in preparation for exposing it to users of the AA API.
2022-11-04[IR] Switch everything to use memory attributeNikita Popov1-22/+2
This switches everything to use the memory attribute proposed in https://discourse.llvm.org/t/rfc-unify-memory-effect-attributes/65579. The old argmemonly, inaccessiblememonly and inaccessiblemem_or_argmemonly attributes are dropped. The readnone, readonly and writeonly attributes are restricted to parameters only. The old attributes are auto-upgraded both in bitcode and IR. The bitcode upgrade is a policy requirement that has to be retained indefinitely. The IR upgrade is mainly there so it's not necessary to update all tests using memory attributes in this patch, which is already large enough. We could drop that part after migrating tests, or retain it longer term, to make it easier to import IR from older LLVM versions. High-level Function/CallBase APIs like doesNotAccessMemory() or setDoesNotAccessMemory() are mapped transparently to the memory attribute. Code that directly manipulates attributes (e.g. via AttributeList) on the other hand needs to switch to working with the memory attribute instead. Differential Revision: https://reviews.llvm.org/D135780
2022-10-31[AliasAnalysis] Introduce getModRefInfoMask() as a generalization of ↵Patrick Walton1-13/+30
pointsToConstantMemory(). The pointsToConstantMemory() method returns true only if the memory pointed to by the memory location is globally invariant. However, the LLVM memory model also has the semantic notion of *locally-invariant*: memory that is known to be invariant for the life of the SSA value representing that pointer. The most common example of this is a pointer argument that is marked readonly noalias, which the Rust compiler frequently emits. It'd be desirable for LLVM to treat locally-invariant memory the same way as globally-invariant memory when it's safe to do so. This patch implements that, by introducing the concept of a *ModRefInfo mask*. A ModRefInfo mask is a bound on the Mod/Ref behavior of an instruction that writes to a memory location, based on the knowledge that the memory is globally-constant memory (in which case the mask is NoModRef) or locally-constant memory (in which case the mask is Ref). ModRefInfo values for an instruction can be combined with the ModRefInfo mask by simply using the & operator. Where appropriate, this patch has modified uses of pointsToConstantMemory() to instead examine the mask. The most notable optimization change I noticed with this patch is that now redundant loads from readonly noalias pointers can be eliminated across calls, even when the pointer is captured. Internally, before this patch, AliasAnalysis was assigning Ref to reads from constant memory; now AA can assign NoModRef, which is a tighter bound. Differential Revision: https://reviews.llvm.org/D136659
2022-10-31[BasicAA] Include MayBeCrossIteration in cache keyNikita Popov1-10/+7
Rather than switching to a new AAQI instance with empty cache when MayBeCrossIteration is toggled, include the value in the cache key. The implementation redundantly include the information in both sides of the pair, but that seems simpler than trying to store it only on one side. Differential Revision: https://reviews.llvm.org/D136175
2022-10-27[BasicAA] Remove redundant libcall handlingNikita Popov1-28/+1
The writeonly attribute for memset_pattern16 (and other referenced libcalls) is being added by InferFunctionAttrs nowadays. No need to special-case it here.
2022-10-27Fix a -Wunused-const-variable warning.Haojian Wu1-6/+0