215 Commits

Author SHA1 Message Date
Ryotaro Kasuga
34a16392fa
[DA] Use SmallVector instead of raw new/delete (NFC) (#190586)
Some functions used `new`/`delete` to allocate/free arrays. To avoid
memory leaks, it would be better to avoid using raw pointers. This patch
replaces the use of them with `SmallVector`.
2026-04-06 15:54:34 +00:00
Ryotaro Kasuga
85e2a36501
[DA] Remove dead code from the Weak Crossing SIV test (#190355)
The ConstantRange intersection check can now handle cases where the
condition of this branch is satisfied. The check is performed before
entering this function, so this part is no longer necessary.
2026-04-03 16:30:42 +00:00
Ehsan Amiri
0abef30c80
[DA] Fix warning introduced by #189740 (#190226)
The following warning is issued for llvm/lib/Analysis/DependenceAnalysis.cpp:2004

warning: suggest parentheses around ‘&&’ within ‘||’
2026-04-02 15:15:25 -04:00
Ryotaro Kasuga
75359e1e1f
[DA] Add static to analyzeCoefficientsForGCD (NFC) (#190172) 2026-04-02 14:04:31 +00:00
Ryotaro Kasuga
682a217d74
[DA] Extract the logic shared by the Exact SIV/RDIV test (#189951)
The Exact SIV test and the Exact RDIV test behave almost identically,
except that the Exact SIV test also explores the directions in the final
step. This patch consolidates the two duplicate implementations into a
single function that can be used by both tests. While this change
slightly affects things like debug output and metrics, it is not
intended to alter the actual test results.
2026-04-02 13:30:42 +00:00
Ehsan Amiri
c5f67a0128
[DA] Perform nsw check for SIV AddRecs earlier (NFCI) (#189740)
The repetitive code at the start of each SIV test can be factored out to
`testSIV` function
2026-04-01 16:16:41 -04:00
Ehsan Amiri
573935d7b3
[DA] Overflow check in WeakCrossing Delta calculation (#185046)
When subtracting the constant part of two addrecs, we need to ensure the
calculation won't overflow. If it may overflow, we conservatively stop
the analysis and return false.
2026-04-01 14:27:18 -04:00
Ehsan Amiri
26a5a494c3
[DA] factor out repetitive code in GCD test (NFCI) (#189461)
The logic for recursively investigating the source and destination
AddRecs in GCD test is the same and can be factored out.
2026-04-01 13:36:47 -04:00
Ruoyu Qiu
71cc59400c
[DA] add debug log in findGCD (#189537) 2026-04-01 09:43:26 +00:00
Ryotaro Kasuga
68a91f3d36
[DA] Remove ExtraGCD from GCD MIV (NFC) (#172004)
As some code was removed in #169927, `ExtraGCD` in `gcdMIVtest` is no
longer necessary. This patch removes it and also adjust the comments.
2026-03-31 11:51:05 +00:00
Ryotaro Kasuga
66c45a3b49
[DA] Fix overflow in the Exact RDIV test (#189578)
This patch adds an overflow check for the computation of `Delta` in the
Exact RDIV test. If an overflow is detected, the analysis bails out.
2026-03-31 11:19:59 +00:00
Ryotaro Kasuga
5d2d179575
[DA] Add overflow check in GCD MIV (#172003)
Add an overflow check in the GCD MIV test. Fix the test case added in #172002 .
2026-03-31 11:19:54 +00:00
Ruoyu Qiu
64eeafad70
[DA] Check nsw flags for addrecs in the Exact RDIV test (#189536)
This patch adds a check to ensure that the addrecs have nsw flags at the
beginning of the Exact SIV test. If either of them doesn't have, the
analysis bails out. This check is necessary because the subsequent
process in the Exact SIV test assumes that they don't wrap.
2026-03-31 09:12:57 +00:00
Ruoyu Qiu
6e2e06f98e
[DA] Refactor the signature of the Exact RDIV test (NFCI) (#189535) 2026-03-31 08:20:06 +00:00
Ehsan Amiri
804ece6a4f
[DA] Require nsw for AddRecs in the WeakCrossing SIV test (#185041)
Before the start of the algorithm in weak crossing SIV test, we need to
ensure both addrecs are `nsw`
2026-03-30 15:51:44 -04:00
Ryotaro Kasuga
f2a9b073ac
[DA] Consolidate the logic for checking overlap at the boundary (NFCI) (#189341)
In the Weak Crossing SIV test, there were two places where we checked
the dependency at the boundary, one is at the first iteration, and the
other is at the last iteration. Now the former can be merged into the
latter. There used to be an edge case when the coefficient is zero, and
we had an explicit check for that. This patch removes that check as
well, by moving the boundary check after the assertion that ensures the
(maybe negated) coefficient is positive.
2026-03-30 09:58:41 +00:00
Ryotaro Kasuga
be94bfd615
[DA] Stop negating Delta in the Weak Zero SIV test (#188212)
This patch removes the variable `NewDelta`, which was calculated as the
negation of `Delta`, along with its uses. `NewDelta` is now referenced
in only one place, and that code is effectively dead because more
general analysis with ConstantRange is performed at an earlier stage.
Also the test using `NewDelta` is not correct when `Delta` is a signed
minimum value, as negating it yields the same value as original. This
patch also fixes the correctness issue in such a situation.
2026-03-30 09:26:02 +00:00
Ruoyu Qiu
c3e7f98997
[DA] Fix overflow of calculation in weakCrossingSIVtest (#188450)
This patch fixes a correctness issue where integer overflow in the
upper bound calculation of weakCrossingSIVtest caused the pass to
incorrectly prove independence.

The previous logic used `SCEV::getMulExpr` to calculate
`2 * ConstCoeff * UpperBound` and compared it to `Delta` using
`isKnownPredicate`. In the presence of overflow, this could yield
unsafe results.

This change replaces the SCEV arithmetic with `ConstantRange` to   
work around calculation overflows, ensures we conservatively assume   
a dependence if the bounds cannot be proven safe.

---------

Signed-off-by: Ruoyu Qiu <cabbaken@outlook.com>
2026-03-30 08:45:44 +00:00
Ryotaro Kasuga
b6d7afe534
[DA] Remove calls to the GCD MIV test from testSIV (#187220)
This patch removes all invocations of the GCD MIV test in `testSIV`
function, specifically:

- If both the source and destination are addrecs, replace the call to
the GCD MIV test with the Exact SIV test. Generally, the Exact SIV test
has a narrower applicability than the GCD MIV test (both the source and
destination must be affine addrecs), while the former likely yields a
more precise result. Therefore, in this case, there's no reason to use
the GCD MIV test. This replacement also means that previously the Exact
SIV test was called conditionally (only when the Strong and Weak
Crossing SIV tests are not applicable), but now it will be called
unconditionally when both the source and destination are addrecs.
- If either the source or the destination is loop-invariant, simply
remove the call to the GCD MIV test without replacing it with anything.
In this case, the Weak Zero SIV test will be invoked, and part of that
test performs almost the same analysis as the GCD MIV test. Therefore,
the GCD MIV test should be redundant in nearly all cases.

This patch introduces two types of changes to the analysis results.
First, it fixes several correctness issues that were caused by defects
in the GCD MIV test. Second, it makes the analysis more precise in some
cases. This improvement comes from the fact that, when both the source
and destination are affine addrecs, the Exact SIV test is now invoked
unconditionally, whereas previously it was only invoked conditionally.
2026-03-30 00:58:59 +00:00
Walter Lee
eb2ff71013
[DA] Mark variable only used in assert as maybe_unused (#189100)
Fix 00aebbff71ff4e348538708064ba2e033ccd6b2a.
2026-03-27 20:38:07 +00:00
Ehsan Amiri
00aebbff71
[DA] Refactor signature of weakCrossingSIVtest and check inputs (NFCI) (#187117)
Passing SCEVAddRecExpr objects directly to weakCrossingSIVtest and
checking the validity of the input operands
2026-03-27 13:57:08 -04:00
Ryotaro Kasuga
f4c0404ffb
[DA] Remove absolute value calculations in the Weak Zero SIV tests (#185580)
In general, the following two points are potentially risky in DA:

- Computing the absolute value of a SCEV expression. Calling
`ScalarEvolution::getAbsExpr` on a signed minimum value will overflow
and return the same value, which is not the expected result in DA.
- Performing arithmetic operations involving a backedge-taken count. The
backedge-taken count is meaningful in an unsigned sense, whereas DA
currently treats all other values and operations in a signed sense.

For these reasons, I think it is better to avoid such operations in DA
whenever possible.

This patch addresses these issues in the Weak Zero SIV tests. I
attempted to craft a test case that exposes a defect caused by these
factors, but I couldn't find one. I'm not entirely sure whether the
current implementation is sound, but anyway, I think avoiding such
operations in DA is a good practice to prevent potential issues in the
future.
2026-03-27 10:49:14 +00:00
Ryotaro Kasuga
62fded6b0f
[DA] Remove redundant check in the Weak Zero SIV tests (NFCI) (#188922)
The trivial disjoint checks in several dependence tests were
consolidated into a single invocation in #187435. However, the
duplicated check still remains in the Weak Crossing SIV tests. This is
redundant and should be deleted as well.
2026-03-27 08:31:07 +00:00
Ryotaro Kasuga
929e072270
[DA] Add nsw check for addrecs in the Weak Zero SIV tests (#185579)
Check that the addrec has `nsw` in the Weak Zero SIV tests, as their
algorithm rely on the addrecs don't wrap.
Fix the test cases added in #185578 .
2026-03-27 07:39:54 +00:00
Ryotaro Kasuga
accf41ef78
[DA] Add precondition 0 <=s UB to function inferAffineDomain (#187218)
This patch fixes issues where the Exact SIV/RDIV tests can return
incorrect results when the BTC is negative in a signed sense. The root
cause is that BTCs should be interpreted in an unsigned sense, but
`inferAffineDomain` uses the BTC in inequalities that are interpreted in
a signed sense. These inequalities make sense only when the BTC is
non-negative. Thus we need to check it beforehand.
2026-03-27 06:14:27 +00:00
Aiden Grossman
c9c1520dee [DA] Fix -Wunused-variable
A couple of these variables are only used within LLVM_DEBUG statements
which get removed by the preprocessor in non-assertions builds which
will cause the variable to become unused. Mark them maybe_unused given
the names make the code more readable.
2026-03-26 15:53:19 +00:00
Ruoyu Qiu
e926dd7ce6
[DA] Consolidate ConstantRange intersection checks into a single early pre-test (#187435) 2026-03-26 22:36:12 +08:00
Ryotaro Kasuga
8e2a5e37ea
[DA] Consolidate the core logic of the Weak Zero SIV tests (NFCI) (#185577)
There have existed two functions `weakZeroSrcSIVtest` and
`weakZeroDstSIVtest`, which are almost identical, except for some minor
differences (e.g., the Direction to be updated). This patch consolidates
the shared core logic into a single function `weakZeroSIVtestImpl` then
calls it from both `weakZeroSrcSIVtest` and `weakZeroDstSIVtest`,
passing the appropriate parameters to handle the differences. This
reduces code duplication and improves maintainability.
2026-03-26 06:52:58 +00:00
Ruoyu Qiu
c84397a6b8
[DA] Optimize parity check in weakCrossingSIVtest (NFC) (#188437)
This patch simplifies the logic used to determine if the `Distance`
is divisible by 2. Previously, this was done by allocating an APInt
and performing a signed remainder (`srem`) operation.

Since `Distance` is an APInt, we can more efficiently check if it
is odd by directly inspecting the least significant bit (`Distance[0]`).
This avoids an expensive division operation and APInt allocation
while making the code more concise.

Signed-off-by: Ruoyu Qiu <cabbaken@outlook.com>
2026-03-25 20:52:15 +08:00
Ryotaro Kasuga
8b1f7f00b4
[DA] Fix the Weak Crossing SIV test when Coeff and Delta are zero (#188203)
The Weak Zero SIV test concluded that there is a dependency only in the
`=`-direction when `Delta` is zero. This is incorrect, because the
coefficients of the addrecs might be zero, in which case the dependency
should have all directions. This patch adds non-zero check for the
coefficient to address the issue.
2026-03-25 08:35:21 +00:00
Alireza Torabian
3124cb3fe7
[DA] Bug fix regarding the SameSD levels (#188098)
SCEV isKnownPredicate may crash if the expressions are involved with
different loops. To verify if two loops have the same iteration space,
we do not need to use the SCEV apis, and it can be done by the equality
check.

Moreover, no pass (not even loop fusion) requires to check SameSD levels
for more than one level. In this patch, we limit the analysis of SameSD
levels to only one level after the common levels.
2026-03-23 20:23:11 +00:00
Ryotaro Kasuga
4754d24f7b
[DA] Extract negating dependence logic (NFCI) (#185576)
Extract the logic to negate the dependence object from
`Dependence::normalize`. The extracted method will be used in the next
PR #185577 to refactor the Weak Zero SIV tests.
2026-03-23 07:33:56 +00:00
Ryotaro Kasuga
5ae5f9df42
[DA] Check nsw flags for addrecs in the Exact SIV test (#186387)
This patch adds a check to ensure that the addrecs have nsw flags at the
beginning of the Exact SIV test. If either of them doesn't have, the
analysis bails out. This check is necessary because the subsequent
process in the Exact SIV test assumes that they don't wrap.
2026-03-19 15:07:08 +00:00
Ryotaro Kasuga
038c8d3f4f
[DA] Rewrite formula in the Weak Zero SIV tests (#183738)
This patch rewrites the formula in the Weak Zero SIV tests to match the
one used in the Strong SIV test that was updated in #179665. In this
form, `ConstantRange` is used so we don't need to pay attention to any
corner cases such as overflow.

Fix some test cases that were added in the past PRs to represent the
edge cases.
2026-03-18 04:49:22 +00:00
Ruoyu Qiu
5f6cd9b923
[DA] Fix overflow in symbolic RDIV test (#185805)
The symbolic RDIV test relies on computing the extremes of affine
expressions (e.g., `A1*N1` and `A2*N2`) to disprove dependencies. These
calculations were previously done using `SE->getMulExpr` and
`SE->getMinusSCEV` without guarding against signed integer overflow. If
large coefficients or loop bounds cause a wrap, `isKnownPredicate`
evaluates the wrapped values, potentially disproving a valid dependence
and leading to miscompilations.

This patch reimplements symbolicRDIVtest using `ConstantRange` to work
around overflows.

---------

Signed-off-by: Ruoyu Qiu <cabbaken@outlook.com>
Co-authored-by: Ryotaro Kasuga <kasuga.ryotaro@fujitsu.com>
2026-03-18 04:39:53 +00:00
Ryotaro Kasuga
3d421d59ad
[DA] Refactor the signature of the Exact SIV test (NFCI) (#186386)
Change the signature of `exactSIVtest` to directly pass addrecs instead
of passing their operands separately. I *think* this change is not
mandatory, but it will simplify the code, especially because we will be
checking the presence of nsw flags on the addrecs.
2026-03-17 06:42:15 +00:00
Ryotaro Kasuga
8677f49147
[DA] Refactor the signature of the Weak Zero SIV tests (NFC) (#185825)
Change the signatures of the Weak Zero SIV tests, specifically by
passing an addrec directly instead of separating it into the coefficient
and constant. This form is more useful to address the several
correctness issues in those tests.
2026-03-12 13:11:08 +00:00
Ruoyu Qiu
cf38704f46
[DA] Fix debug log format (#185804)
The current debug log:
```
SrcSubscripts: {4611686018427387904,+,-1}<nsw><%loop.header>
DstSubscripts: {4611686018427387904,+,-1}<nsw><%loop.header>    delinearized
    common nesting levels = 1
    maximum nesting levels = 1
    SameSD nesting levels = 0
```
Missing a newline before "delinearized".

Signed-off-by: Ruoyu Qiu <cabbaken@outlook.com>
2026-03-11 08:52:54 +00:00
Ruoyu Qiu
8a9c6a346c
[DA] refactor bounds inference in exactSIVtest and exactRDIVtest (NFC) (#185719)
Replaces the `SmallVector`-based approach for computing the min/max of
affine domain bounds with `GetMaxOrMin` lambda returning `std::optional`
for better readability.
Previously, the code allocated a `SmallVector` to collect valid bounds
and relied on `smax(front(), back())` to handle the single-element case,
which may cause misunderstanding.

---------

Signed-off-by: Ruoyu Qiu <cabbaken@outlook.com>
2026-03-11 15:43:43 +08:00
Ryotaro Kasuga
c61f2be5c4
[DA] Remove outdated comments (NFC) (#185621)
Recently, the consistent flag and the peeling flags were removed from
the `Dependence` class (#181608, #183737). However, the related comments
were not deleted accordingly. This patch cleans them up.
2026-03-10 12:08:38 +00:00
Ryotaro Kasuga
eebc390eb0
[DA] Fix overflows when calculating Delta in the Weak Zero SIV tests (#184997)
Add overflow checks when computing `Delta` in the Weak Zero SIV tests.
The tests bail out if we cannot prove that the `Delta` computation does
not overflow. These calculations are also moved later so that some
analyses that do not require these checks can run first.

Fix part of the test cases added in #164246.
2026-03-10 01:18:50 +00:00
Ehsan Amiri
2e3bff6980
[DA] Test AddRecs are nsw before strong SIV test (#183421)
Currently Strong SIV test, does not check that the AddRecs involved do
not overflow. This is required for correctness of the tests. Strictly
speaking, the range-based independence check in Strong SIV relies on
SCEV which internally takes care of potential overflows, so this is
mainly needed for the divisibility test and distance/directions
calculations, but putting the test early in the function covers all the
cases anyways.
2026-03-09 12:39:37 -04:00
Ryotaro Kasuga
aab73769da
[DA] Remove isPeelFirst and isPeelLast (#183737)
`isPeelFirst` and `isPeelLast` are updated only in the Weak Zero SIV
tests, and no clients actually use them. Keeping these features while
fixing the existing defects in DA would add unnecessary complexity. If
they are unnecessary in the first place, it would be better to delete
them to mitigate maintenance burden.
2026-03-06 17:19:29 +09:00
Ryotaro Kasuga
7dda8ba375
[DA] Fix the Weak Zero SIV tests when the coeff may be zero (#183736)
In the Weak Zero SIV tests, given two subscripts `{c0,+,a}` and `c1`,
when `c0 == c1`, the tests conclude that a dependency exists from the
former subscript at the first iteration to the latter subscript at every
iteration. However, this conclusion is correct only when `a` is not
zero, which was not being checked.
This patch adds non-zero checks for `a` in the Weak Zero SIV tests.
Fix the test cases added in #183735 .
2026-03-06 07:28:21 +00:00
Ryotaro Kasuga
ae43867756
[DA] Remove consistent flag from Dependence class (#181608)
The `Dependence` class has a flag `Consistent`, but it's not clear what
it represents. This flag doesn't seem to be updated correctly during
analysis. There are no users of it, so I think this flag would be
unnecessary.
In addition, because of this flag, even minor code changes can alter
test results unintentionally (specifically, the presence/absence of a
string "consistent" in the test output). Such test changes can be
confusing and gradually add to the maintenance burden.
Given these points, it's better to remove the `Consistent` flag from the
`Dependence` class entirely.
2026-03-04 07:51:15 +00:00
Ryotaro Kasuga
37eee8a99f
[DA] Rewrite the formula in the Strong SIV test (#179665)
In the Strong SIV test, given two addrecs `{c0,+,a}` and `{c1,+,a}`, the
following inequality is evaluated:

`|c0 - c1| >s |a| * BTC`, where `BTC` is the backedge-taken count of the
loop.

To evaluate this correctly, at least the following checks are necessary.

- `c0 - c1` doesn't overflow
- For all absolute-value calculations `|x|`, `x` is not the signed
minimum value
- `|a| * BTC` doesn't overflow
- `0 <=s BTC`, which is currently missed
- The addrecs have `nsw`, which is also currently missed

Enumerating these conditions and inserting them one by one is risky, and
I believe it makes the software flaky, so it should be avoided. It's
also unclear if these conditions are sufficient.

This patch replaces the current implementation with one that uses
`ConstantRange` so that we don't need to check any condition by
ourselves.

Fixes the test case for the Strong SIV test added in #179664. I believe
a similar fix can be applied for other tests as well.
2026-02-24 11:39:14 +00:00
Ryotaro Kasuga
e01b867890
[DA] Remove "minor algebra" in the RDIV test (#179654)
As mentioned in #179653, the "minor algebra" in `testRDIV` is incorrect.
This patch deletes that part completely.

Fixes the test case added in #179653.
2026-02-20 17:29:22 +00:00
Ryotaro Kasuga
e503801ff4
[DA] Remove DependenceInfo::unifySubscriptType (#181607)
`DependenceInfo::unifySubscriptType` is a function that takes two
subscripts and casts them to the wider type. Using this function can
sometimes lead to correctness issues, especially when combined with
`DependenceInfo::removeMatchingExtensions`, as in #148435. These two
functions are intended to broaden the scope of DA, but they can also
introduce correctness issues, mainly due to mishandling of `sext`/`zext`
and integer overflows.

To avoid these issues, this patch removes the `unifySubscriptType`
function. Currently, it has only one caller, which is part of the
validation logic for delinearization. Instead of calling
`unifySubscriptType`, this patch adds a type check and bails out if the
types do not match. Note that I'm not entirely sure whether there are
real cases where the types differ and the check is actually necessary.
Also, this patch doesn't include new test cases, as I have not found
concrete examples where `unifySubscriptType` itself causes actual
issues. That is, this patch may be NFC.

Fix #169807
2026-02-19 19:19:03 +09:00
Ryotaro Kasuga
d74617b06f
[DA] Remove DependenceInfo::removeMatchingExtensions (#171809)
If two subscripts has same cast operation (`sext` or `zext`),
`removeMatchingExtensions` strips off them. Due to the following
reasons, remaining this function is not useful and risky:

- If the operand of the cast operation has proper nowrap properties,
SCEV usually strips off the cast operation automatically. It's not very
meaningful to do it on DA side.
- If the operand doesn't have nowrap properties, stripping off the cast
operation may lead to incorrect result. Especially, if it can be
negative, removing `zext` would change the interpretation of the value
in a signed sense.
- This function is part of the root cause of #148435.

Although I've not found any cases where incorrect results are produced
by this function, but I think it doesn't make sense to keep it. As far
as I have checked, there are no regressions after removing this
function.

Resolve #169809
2026-02-16 17:12:48 +00:00
Florian Hahn
3fb914d851
[SCEV] Add initial support for ptrtoaddr. (#158032)
Add initial support for PtrToAddr to SCEV, including a new
SCEVPtrToAddrExpr and SCEV expansion support for it.

PR: https://github.com/llvm/llvm-project/pull/158032
2026-01-16 11:58:04 +00:00