[PATCH] D144197: [SCEV] Also strengthen flags of expr for BEValue.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 16 08:40:12 PST 2023


fhahn created this revision.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144197

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Analysis/ScalarEvolution/trip-count-negative-stride.ll


Index: llvm/test/Analysis/ScalarEvolution/trip-count-negative-stride.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/trip-count-negative-stride.ll
+++ llvm/test/Analysis/ScalarEvolution/trip-count-negative-stride.ll
@@ -106,10 +106,10 @@
 define void @ult_129_unknown_start(i8 %start) mustprogress {
 ; CHECK-LABEL: 'ult_129_unknown_start'
 ; CHECK-NEXT:  Determining loop execution counts for: @ult_129_unknown_start
-; CHECK-NEXT:  Loop %for.body: backedge-taken count is 0
-; CHECK-NEXT:  Loop %for.body: constant max backedge-taken count is 0
-; CHECK-NEXT:  Loop %for.body: symbolic max backedge-taken count is 0
-; CHECK-NEXT:  Loop %for.body: Predicated backedge-taken count is 0
+; CHECK-NEXT:  Loop %for.body: backedge-taken count is (((127 + (-1 * (1 umin (127 + (-1 * %start) + (-128 umax (-127 + %start)<nuw>))))<nuw><nsw> + (-1 * %start) + (-128 umax (-127 + %start)<nuw>)) /u -127) + (1 umin (127 + (-1 * %start) + (-128 umax (-127 + %start)<nuw>))))
+; CHECK-NEXT:  Loop %for.body: constant max backedge-taken count is 1
+; CHECK-NEXT:  Loop %for.body: symbolic max backedge-taken count is (((127 + (-1 * (1 umin (127 + (-1 * %start) + (-128 umax (-127 + %start)<nuw>))))<nuw><nsw> + (-1 * %start) + (-128 umax (-127 + %start)<nuw>)) /u -127) + (1 umin (127 + (-1 * %start) + (-128 umax (-127 + %start)<nuw>))))
+; CHECK-NEXT:  Loop %for.body: Predicated backedge-taken count is (((127 + (-1 * (1 umin (127 + (-1 * %start) + (-128 umax (-127 + %start)<nuw>))))<nuw><nsw> + (-1 * %start) + (-128 umax (-127 + %start)<nuw>)) /u -127) + (1 umin (127 + (-1 * %start) + (-128 umax (-127 + %start)<nuw>))))
 ; CHECK-NEXT:   Predicates:
 ; CHECK:       Loop %for.body: Trip multiple is 1
 ;
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -5735,8 +5735,14 @@
   if (auto *BEInst = dyn_cast<Instruction>(BEValueV)) {
     assert(isLoopInvariant(Accum, L) &&
            "Accum is defined outside L, but is not invariant?");
-    if (isAddRecNeverPoison(BEInst, L))
-      (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
+    if (isAddRecNeverPoison(BEInst, L)) {
+      auto *S = getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
+      if (auto *AR = dyn_cast<SCEVAddRecExpr>(S)) {
+        setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),
+                       (SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
+                                           proveNoWrapViaConstantRanges(AR)));
+      }
+    }
   }
 
   return PHISCEV;
@@ -5864,8 +5870,16 @@
         // know that it is *undefined behavior* for BEValueV to
         // overflow.
         if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
-          if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
-            (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
+          if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L)) {
+            auto *S =
+                getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
+            if (auto *AR = dyn_cast<SCEVAddRecExpr>(S)) {
+              setNoWrapFlags(
+                  const_cast<SCEVAddRecExpr *>(AR),
+                  (SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
+                                      proveNoWrapViaConstantRanges(AR)));
+            }
+          }
 
         return PHISCEV;
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144197.498040.patch
Type: text/x-patch
Size: 3531 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230216/399b5720/attachment.bin>


More information about the llvm-commits mailing list