[llvm-commits] [llvm] r127342 - in /llvm/trunk: lib/Analysis/ScalarEvolution.cpp test/Analysis/ScalarEvolution/2011-03-09-ExactNoMaxBECount.ll

Andrew Trick atrick at apple.com
Wed Mar 9 09:29:58 PST 2011


Author: atrick
Date: Wed Mar  9 11:29:58 2011
New Revision: 127342

URL: http://llvm.org/viewvc/llvm-project?rev=127342&view=rev
Log:
When SCEV can determine the loop test is X < X, set ExactBECount=0.
When ExactBECount is a constant, use it for MaxBECount.
When MaxBECount cannot be computed, replace it with ExactBECount.
Fixes PR9424.

Added:
    llvm/trunk/test/Analysis/ScalarEvolution/2011-03-09-ExactNoMaxBECount.ll
Modified:
    llvm/trunk/lib/Analysis/ScalarEvolution.cpp

Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=127342&r1=127341&r2=127342&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Wed Mar  9 11:29:58 2011
@@ -5646,6 +5646,13 @@
          "This code doesn't handle negative strides yet!");
 
   const Type *Ty = Start->getType();
+
+  // When Start == End, we have an exact BECount == 0. Short-circuit this case
+  // here because SCEV may not be able to determine that the unsigned division
+  // after rounding is zero.
+  if (Start == End)
+    return getConstant(Ty, 0);
+
   const SCEV *NegOne = getConstant(Ty, (uint64_t)-1);
   const SCEV *Diff = getMinusSCEV(End, Start);
   const SCEV *RoundUp = getAddExpr(Step, NegOne);
@@ -5768,7 +5775,16 @@
 
     // The maximum backedge count is similar, except using the minimum start
     // value and the maximum end value.
-    const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step, NoWrap);
+    // If we already have an exact constant BECount, use it instead.
+    const SCEV *MaxBECount = isa<SCEVConstant>(BECount) ? BECount
+      : getBECount(MinStart, MaxEnd, Step, NoWrap);
+
+    // If the stride is nonconstant, and NoWrap == true, then
+    // getBECount(MinStart, MaxEnd) may not compute. This would result in an
+    // exact BECount and invalid MaxBECount, which should be avoided to catch
+    // more optimization opportunities.
+    if (isa<SCEVCouldNotCompute>(MaxBECount))
+      MaxBECount = BECount;
 
     return BackedgeTakenInfo(BECount, MaxBECount);
   }

Added: llvm/trunk/test/Analysis/ScalarEvolution/2011-03-09-ExactNoMaxBECount.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/2011-03-09-ExactNoMaxBECount.ll?rev=127342&view=auto
==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/2011-03-09-ExactNoMaxBECount.ll (added)
+++ llvm/trunk/test/Analysis/ScalarEvolution/2011-03-09-ExactNoMaxBECount.ll Wed Mar  9 11:29:58 2011
@@ -0,0 +1,34 @@
+; RUN: opt -indvars  %s
+; PR9424: Attempt to use a SCEVCouldNotCompute object!
+; The inner loop computes the Step and Start of the outer loop.
+; Call that Vexit. The outer End value is max(2,Vexit), because
+; the condition "icmp %4 < 2" does not guard the outer loop.
+; SCEV knows that Vexit has range [2,4], so End == Vexit == Start.
+; Now we have ExactBECount == 0. However, MinStart == 2 and MaxEnd == 4.
+; Since the stride is variable and may wrap, we cannot compute
+; MaxBECount. SCEV should override MaxBECount with ExactBECount.
+
+define void @bar() nounwind {
+entry:
+  %. = select i1 undef, i32 2, i32 1
+  br label %"5.preheader"
+
+"4":                                              ; preds = %"5.preheader", %"4"
+  %0 = phi i32 [ 0, %"5.preheader" ], [ %1, %"4" ]
+  %1 = add nsw i32 %0, 1
+  %2 = icmp sgt i32 %., %1
+  br i1 %2, label %"4", label %"9"
+
+"9":                                              ; preds = %"4"
+  %3 = add i32 %6, 1
+  %4 = add i32 %3, %1
+  %5 = icmp slt i32 %4, 2
+  br i1 %5, label %"5.preheader", label %return
+
+"5.preheader":                                    ; preds = %"9", %entry
+  %6 = phi i32 [ 0, %entry ], [ %4, %"9" ]
+  br label %"4"
+
+return:                                           ; preds = %"9"
+  ret void
+}





More information about the llvm-commits mailing list