[llvm] [DA] Remove absolute value calculations in the Weak Zero SIV tests (PR #185580)
Ryotaro Kasuga via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 27 01:49:59 PDT 2026
================
@@ -1802,19 +1822,19 @@ bool DependenceInfo::weakZeroSIVtestImpl(const SCEVAddRecExpr *AR,
if (!ConstCoeff)
return false;
- // Since ConstCoeff is constant, !isKnownNegative means it's non-negative.
- // TODO: Bail out if it's a signed minimum value.
- const SCEV *AbsCoeff = SE->isKnownNegative(ConstCoeff)
- ? SE->getNegativeSCEV(ConstCoeff)
- : ConstCoeff;
const SCEV *NewDelta =
SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
if (const SCEV *UpperBound =
collectUpperBound(AR->getLoop(), Delta->getType())) {
LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
- const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
- if (SE->isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
+ bool OverlapAtLast = [&] {
+ if (!SE->isKnownNonZero(ConstCoeff))
+ return false;
+ const SCEV *Last = AR->evaluateAtIteration(UpperBound, *SE);
+ return Last == Const;
+ }();
+ if (OverlapAtLast) {
----------------
kasuga-fj wrote:
I've rebased. The ConstantRange-based test you mentioned was moved to outside of the function, so we cannot use them unless we recompute it.
> that value of our Const is equal to one of the two ends of the `ARRange`
This is not the case. We need to check the sign of `ARCoeff` and compare the value of `Const` with the corresponding endpoint of `ARRange`. The code would look like this:
```cpp
bool OverlapAtLast = false;
OverlapAtLast |= SE->isKnownPositive(ARCoeff) && ConstCoeff->getAPInt().eq(ARRange.getSignedRangeMax());
OverlapAtLast |= SE->isKnownNegative(ARCoeff) && ConstCoeff->getAPInt().eq(ARRange.getSignedRangeMin());
```
I don't think there is any meaningful difference in terms of LoC or simplicity. However, I'd prefer the current one as I believe it is easier to read and understand.
> I think it is better if we avoid using `evaluateAtIteration` as it ignores overflow
Now that `AR` is guaranteed to have `nsw`, using `evaluateAtIteration` here is safe. If you are concerned about overflow, relying on ConstantRange does not necessarily solve the problem either. If `AR` can overflow, it may evaluate to the minimum or maximum value of the range multiple times, which would make this test unsound.
https://github.com/llvm/llvm-project/pull/185580
More information about the llvm-commits
mailing list