[llvm] r272179 - Fix a bug in SCEV's poison value propagation

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 8 10:48:32 PDT 2016


Author: sanjoy
Date: Wed Jun  8 12:48:31 2016
New Revision: 272179

URL: http://llvm.org/viewvc/llvm-project?rev=272179&view=rev
Log:
Fix a bug in SCEV's poison value propagation

The worklist algorithm introduced in rL271151 didn't check to see if the
direct users of the post-inc add recurrence propagates poison.  This
change fixes the problem and makes the code structure more obvious.

Note for release managers: correctness wise, this bug wasn't a
regression introduced by rL271151 -- the behavior of SCEV around
post-inc add recurrences was strictly improved (in terms of correctness)
in rL271151.

Modified:
    llvm/trunk/lib/Analysis/ScalarEvolution.cpp
    llvm/trunk/test/Analysis/ScalarEvolution/nsw.ll

Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=272179&r1=272178&r2=272179&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Wed Jun  8 12:48:31 2016
@@ -4880,22 +4880,23 @@ bool ScalarEvolution::isAddRecNeverPoiso
     return false;
 
   SmallPtrSet<const Instruction *, 16> Pushed;
-  SmallVector<const Instruction *, 8> Stack;
+  SmallVector<const Instruction *, 8> PoisonStack;
 
+  // We start by assuming \c I, the post-inc add recurrence, is poison.  Only
+  // things that are known to be fully poison under that assumption go on the
+  // PoisonStack.
   Pushed.insert(I);
-  for (auto *U : I->users())
-    if (Pushed.insert(cast<Instruction>(U)).second)
-      Stack.push_back(cast<Instruction>(U));
+  PoisonStack.push_back(I);
 
   bool LatchControlDependentOnPoison = false;
-  while (!Stack.empty()) {
-    const Instruction *I = Stack.pop_back_val();
+  while (!PoisonStack.empty()) {
+    const Instruction *Poison = PoisonStack.pop_back_val();
 
-    for (auto *U : I->users()) {
-      if (propagatesFullPoison(cast<Instruction>(U))) {
-        if (Pushed.insert(cast<Instruction>(U)).second)
-          Stack.push_back(cast<Instruction>(U));
-      } else if (auto *BI = dyn_cast<BranchInst>(U)) {
+    for (auto *PoisonUser : Poison->users()) {
+      if (propagatesFullPoison(cast<Instruction>(PoisonUser))) {
+        if (Pushed.insert(cast<Instruction>(PoisonUser)).second)
+          PoisonStack.push_back(cast<Instruction>(PoisonUser));
+      } else if (auto *BI = dyn_cast<BranchInst>(PoisonUser)) {
         assert(BI->isConditional() && "Only possibility!");
         if (BI->getParent() == LatchBB) {
           LatchControlDependentOnPoison = true;

Modified: llvm/trunk/test/Analysis/ScalarEvolution/nsw.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/nsw.ll?rev=272179&r1=272178&r2=272179&view=diff
==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/nsw.ll (original)
+++ llvm/trunk/test/Analysis/ScalarEvolution/nsw.ll Wed Jun  8 12:48:31 2016
@@ -204,3 +204,39 @@ for.body:
 for.end:
   ret void
 }
+
+
+define void @bad_postinc_nsw_a(i32 %n) {
+; CHECK-LABEL: Classifying expressions for: @bad_postinc_nsw_a
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
+  %iv.inc = add nsw i32 %iv, 7
+; CHECK:    %iv.inc = add nsw i32 %iv, 7
+; CHECK-NEXT:  -->  {7,+,7}<nuw><%loop>
+  %becond = icmp ult i32 %iv, %n
+  br i1 %becond, label %loop, label %leave
+
+leave:
+  ret void
+}
+
+define void @bad_postinc_nsw_b(i32 %n) {
+; CHECK-LABEL: Classifying expressions for: @bad_postinc_nsw_b
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
+  %iv.inc = add nsw i32 %iv, 7
+  %iv.inc.and = and i32 %iv.inc, 0
+; CHECK:    %iv.inc = add nsw i32 %iv, 7
+; CHECK-NEXT:  -->  {7,+,7}<nuw><%loop>
+  %becond = icmp ult i32 %iv.inc.and, %n
+  br i1 %becond, label %loop, label %leave
+
+leave:
+  ret void
+}




More information about the llvm-commits mailing list