[PATCH] D64509: [SCEV] Compute exit count from overflow test

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 11 13:57:33 PDT 2019


nikic added inline comments.


================
Comment at: lib/Analysis/ScalarEvolution.cpp:7308
+        if (WO->getBinaryOp() == Instruction::Add &&
+            isKnownPositive(getSCEV(WO->getRHS()))) {
+          Value* LHS = WO->getLHS();
----------------
It's not wrong, but unnecessarily restrictive and a bit confusing to do the isKnownPositive check for an unsigned addition.

It might make sense to handle the negative stride case right away with a code structure like this:

```
const SCEV *MaxValue = nullptr, *MinValue = nullptr;
if (WO->getBinaryOp() == Instruction::Add) {
  if (!Signed)
    MaxValue = getMinusSCEV(
        getConstant(APInt::getNullValue(BitWidth)), RHSSCEV);
  else if (isKnownPositive(RHSSCEV))
    MaxValue = getMinusSCEV(
        getConstant(APInt::getSignedMinValue(BitWidth)), RHSSCEV);
  else if (isKnownNegative(RHSSCEV))
    MinValue = getAddExpr(
        getConstant(APInt::getSignedMaxValue(BitWidth)), RHSSCEV);
}
if (MaxValue) { ... howManyLessThans ... }
else if (MinValue) { ... howManyGreaterThans ... }
```

This should make it easy to extend to Sub as well.


================
Comment at: lib/Analysis/ScalarEvolution.cpp:7316
+                         APInt::getMaxValue(BitWidth)) + 1);
+          const SCEV *MaxValue = getMinusSCEV(OnOverflow, getSCEV(RHS));;
+          ExitLimit EL = howManyLessThans(getSCEV(LHS), MaxValue, L, Signed,
----------------
nit: Stray semicolon.


Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64509/new/

https://reviews.llvm.org/D64509





More information about the llvm-commits mailing list