800 Commits

Author SHA1 Message Date
Nikita Popov
7c38ee26d4
[FunctionAttrs][IR] Fix memory attr inference for volatile mem intrinsics (#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-25 09:29:37 +02:00
Nikita Popov
bc7fafbeea
[AA] Take read-only provenance captures into account (#143097)
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-12 14:13:15 +02:00
Nikita Popov
4e441665cc [BasicAA][ValueTracking] Use MaxLookupSearchDepth constant (NFC)
Use MaxLookupSearchDepth in all places limiting an underlying
object walk, instead of hardcoding 6 in various places.
2025-06-11 16:32:06 +02:00
Nikita Popov
10dd83d274
[AA] Merge isNonEscapingLocalObject() into SimpleCaptureAnalysis (NFC) (#142971)
isNonEscapingLocalObject() is only used by SimpleCaptureAnalysis and
tightly integrated with its implementation (in particular its cache), so
inline and simplify the implementation.
2025-06-06 09:40:57 +02:00
Ramkumar Ramachandra
b40e4ceaa6
[ValueTracking] Make Depth last default arg (NFC) (#142384)
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-06-03 17:12:24 +01:00
Kazu Hirata
89308de4b0
[llvm] Value-initialize values with *Map::try_emplace (NFC) (#141522)
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-26 15:13:02 -07:00
Nikita Popov
027b203814
[BasicAA] Gracefully handle large LocationSize (#138528)
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-06 14:19:47 +02:00
Nikita Popov
3416d4fcee
[AA] Assert that alias() arguments are pointers (#138242)
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-05-05 12:08:55 +02:00
Craig Topper
fff622fbf7
[BasicAA] Account for wrapping when using abs(Scale*V0 + (-Scale)*V1) >= 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-29 14:10:37 -07:00
Rahul Joshi
99e4b3927c
[LLVM] Cleanup pass initialization for Analysis passes (#135858)
- 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-21 12:36:34 -07:00
David Green
c23e1cb936
[BasicAA] Treat ExtractValue(Argument) similar to Argument in relation to 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-04-08 10:05:58 +01:00
Nikita Popov
38e8dff84b
[AA][BasicAA] Move more call logic to BasicAA (#131144)
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-19 15:44:52 +01:00
Nikita Popov
de895751d2
[CaptureTracking][AA] Only consider provenance captures (#130777)
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-03-13 09:54:36 +01:00
Nikita Popov
9cbdcfcafd [CaptureTracking] Remove StoreCaptures parameter (NFC)
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-24 12:00:57 +01:00
Yingwei Zheng
626c23112f
[ValueTracking] Use SimplifyQuery in isKnownNonEqual (#124942)
It is needed by https://github.com/llvm/llvm-project/pull/117442.
2025-02-01 15:13:11 +08:00
Nikita Popov
a30e50fcb3
[BasicAA] Do not decompose past casts with different index width (#119365)
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-13 12:58:59 +01:00
Nikita Popov
cc569a3702 [AA] Export the isBaseOfObject() API (NFC)
This is also useful outside BasicAA.
2024-12-09 16:19:35 +01:00
Nikita Popov
5b0f4f2cb0
[BasicAA] Treat returns_twice functions as clobbering unescaped objects (#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-12-03 09:55:12 +01:00
Nikita Popov
e636434bdf
[BasicAA][LAA] Don't use same-block phis in cross iteration mode (#116802)
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-27 09:38:51 +01:00
Nikita Popov
1e32a7d42c
[AA] Rename CaptureInfo -> CaptureAnalysis (NFC) (#116842)
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-11-20 09:42:28 +01:00
Kazu Hirata
0614b3cfac
[Analysis] Simplify code with DenseMap::operator[] (NFC) (#111331) 2024-10-07 07:00:45 -07:00
Ramkumar Ramachandra
091dc23a2f
BasicAA: update comments in a routine (NFC) (#110492)
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-10-01 10:42:55 +01:00
Nikita Popov
b9bba6ca9f
[BasicAA] Track nuw through decomposed expressions (#106512)
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-09-02 12:11:03 +02:00
Hari Limaye
ba84cfbe0c
[BasicAA] Use nuw attribute of GEPs (#98608)
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-20 11:00:03 +01:00
Matt Arsenault
9c7c3f94ef
BasicAA: Fix assert when indexing address spaces with different sizes (#103713)
Fixes #103500
2024-08-14 14:53:06 +04:00
Nikita Popov
228753290b [BasicAA] Remove unused variables (NFC)
Split out from #98608.
2024-08-14 12:43:50 +02:00
Nikita Popov
3c87f66b7e
[BasicAA] Make use of nusw+nuw -> nneg implication (#102141)
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-07 12:47:21 +02:00
Nikita Popov
d56d808fdc [BasicAA] Check nusw instead of inbounds
For the offset scaling, this is sufficient to guarantee nsw. The
other checks for inbounds in this file do need proper inbounds.
2024-08-06 14:38:49 +02:00
Nikita Popov
91073380ac
[BasicAA] Fix handling of indirect assumption based results (#100130)
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-07-25 12:25:19 +02:00
Nikita Popov
9df71d7673
[IR] Add getDataLayout() helpers to Function and GlobalValue (#96919)
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-28 08:36:49 +02:00
Alex MacLean
d881bac6fa
[BasicAA] Consider 'nneg' flag when comparing CastedValues (#94129)
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-06-04 08:32:57 -07:00
Nikita Popov
f8a19a8f74 [SimplifyQuery] Avoid PatternMatch.h include (NFC)
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-23 12:18:07 +09:00
Harald van Dijk
60de56c743
[ValueTracking] Restore isKnownNonZero parameter order. (#88873)
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-16 15:21:09 +01:00
Yingwei Zheng
e0a628715a
[ValueTracking] Convert isKnownNonZero to use SimplifyQuery (#85863)
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-04-12 23:47:20 +08:00
David Green
5e6b4be5cb
[BasicAA] Treat different VScale intrinsics as the same value. (#81152)
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 11:27:49 +00:00
David Green
9d8a236164
[BasicAA] Check for Overflow using vscale_range (#81144)
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-12 10:21:20 +00:00
David Green
0079136f7d
[BasicAA] Fix Scale check in vscale aliasing. (#81174)
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-09 07:48:43 +00:00
David Green
878234b320
[BasicAA] Scalable offset with scalable typesize. (#80818)
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-08 11:07:33 +00:00
David Green
84ea236af9
[BasicAA] Handle scalable type sizes with constant offsets (#80445)
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-02-05 11:20:50 +00:00
Nikita Popov
4f32f5d572
[AA][JumpThreading] Don't use DomTree for AA in JumpThreading (#79294)
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-31 15:23:53 +01:00
Nikita Popov
5f57ad85a1
[BasicAA] Remove incorrect rule about constant pointers (#76815)
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-17 09:31:00 +01:00
David Green
d69efa4015
[BasicAA] Handle disjoint or as add in DecomposeGEP. (#78209)
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-16 09:22:20 +00:00
David Goldblatt
852596d804
[BasicAA] Guess reasonable contexts for separate storage hints (#76770)
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 11:29:00 -08:00
Jannik Silvanus
7954c57124
[IR] Fix GEP offset computations for vector GEPs (#75448)
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-04 10:08:21 +01:00
David Goldblatt
92e211ab33
[BasicAA] Enable separate storage hints by default (#76864)
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 12:51:40 -08:00
Nikita Popov
b07bf16a6f
[AssumptionCache] Add affected values for separate_storage (#76806)
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.
2024-01-03 17:05:08 +01:00
Nikita Popov
d9e8ae7d2f [ValueTracking] Convert MaskedValueIsZero() to use SimplifyQuery (NFC) 2023-11-29 11:18:42 +01:00
Sander de Smalen
81b7f115fb
[llvm][TypeSize] Fix addition/subtraction in TypeSize. (#72979)
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-22 08:52:53 +00:00
Nikita Popov
a3908d33b1 [BasicAA] Optimize index size adjustment (NFC)
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 16:32:27 +01:00
Florian Hahn
2d39cb4983
[BasicAA] Don't use MinAbsVarIndex = 1. (#72993)
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 14:27:50 +00:00