[PATCH] D152513: [SCEV] Use isKnownNonZero() for SCEV verification

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 9 03:19:54 PDT 2023


nikic created this revision.
nikic added reviewers: fhahn, mkazantsev, reames.
Herald added subscribers: StephenFan, javed.absar, hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Report all cases where we //can// prove that the expressions are //not// the same, rather than just the cases where we know the exact constant delta.

In part this is intended to clarify the difference with verify-scev-strict mode, which reports cases where we //cannot// prove that the expressions //are// the same.


https://reviews.llvm.org/D152513

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp


Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -14039,7 +14039,7 @@
   SmallPtrSet<BasicBlock *, 16> ReachableBlocks;
   SE2.getReachableBlocks(ReachableBlocks, F);
 
-  auto GetDelta = [&](const SCEV *Old, const SCEV *New) -> const SCEV * {
+  auto GetNonZeroDelta = [&](const SCEV *Old, const SCEV *New) -> const SCEV * {
     if (containsUndefs(Old) || containsUndefs(New)) {
       // SCEV treats "undef" as an unknown but consistent value (i.e. it does
       // not propagate undef aggressively).  This means we can (and do) fail
@@ -14049,12 +14049,13 @@
       return nullptr;
     }
 
-    // Unless VerifySCEVStrict is set, we only compare constant deltas.
     const SCEV *Delta = SE2.getMinusSCEV(Old, New);
-    if (!VerifySCEVStrict && !isa<SCEVConstant>(Delta))
-      return nullptr;
-
-    return Delta;
+    // By default, only report cases where we can prove the expressions are not
+    // the same. In VerifySCEVStrict mode, report cases where we cannot prove
+    // that the expressions are the same.
+    if (VerifySCEVStrict ? !Delta->isZero() : SE2.isKnownNonZero(Delta))
+      return Delta;
+    return nullptr;
   };
 
   while (!LoopStack.empty()) {
@@ -14093,8 +14094,8 @@
              SE.getTypeSizeInBits(NewBECount->getType()))
       CurBECount = SE2.getZeroExtendExpr(CurBECount, NewBECount->getType());
 
-    const SCEV *Delta = GetDelta(CurBECount, NewBECount);
-    if (Delta && !Delta->isZero()) {
+    const SCEV *Delta = GetNonZeroDelta(CurBECount, NewBECount);
+    if (Delta) {
       dbgs() << "Trip Count for " << *L << " Changed!\n";
       dbgs() << "Old: " << *CurBECount << "\n";
       dbgs() << "New: " << *NewBECount << "\n";
@@ -14133,8 +14134,8 @@
         continue;
       const SCEV *OldSCEV = SCM.visit(KV.second);
       const SCEV *NewSCEV = SE2.getSCEV(I);
-      const SCEV *Delta = GetDelta(OldSCEV, NewSCEV);
-      if (Delta && !Delta->isZero()) {
+      const SCEV *Delta = GetNonZeroDelta(OldSCEV, NewSCEV);
+      if (Delta) {
         dbgs() << "SCEV for value " << *I << " changed!\n"
                << "Old: " << *OldSCEV << "\n"
                << "New: " << *NewSCEV << "\n"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152513.529884.patch
Type: text/x-patch
Size: 2304 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230609/5183011a/attachment.bin>


More information about the llvm-commits mailing list