[llvm-commits] [llvm] r61082 - in /llvm/trunk: lib/Analysis/ScalarEvolution.cpp test/Analysis/ScalarEvolution/2008-12-08-FiniteSGE.ll test/Analysis/ScalarEvolution/2008-12-11-SMaxOverflow.ll test/Analysis/ScalarEvolution/2008-12-14-StrideAndSigned.ll test/Analysis/ScalarEvolution/2008-12-15-DontUseSDiv.ll

Nick Lewycky nicholas at mxc.ca
Tue Dec 16 00:30:09 PST 2008


Author: nicholas
Date: Tue Dec 16 02:30:01 2008
New Revision: 61082

URL: http://llvm.org/viewvc/llvm-project?rev=61082&view=rev
Log:
Generalize support for analyzing loops to include SLE/SGE loop exit conditions
and support for non-unit strides with signed exit conditions.

Added:
    llvm/trunk/test/Analysis/ScalarEvolution/2008-12-14-StrideAndSigned.ll
    llvm/trunk/test/Analysis/ScalarEvolution/2008-12-15-DontUseSDiv.ll
Modified:
    llvm/trunk/lib/Analysis/ScalarEvolution.cpp
    llvm/trunk/test/Analysis/ScalarEvolution/2008-12-08-FiniteSGE.ll
    llvm/trunk/test/Analysis/ScalarEvolution/2008-12-11-SMaxOverflow.ll

Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=61082&r1=61081&r2=61082&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Tue Dec 16 02:30:01 2008
@@ -2924,15 +2924,16 @@
   if (!R)
     return true;
 
-  if (isSigned)
-    return true;  // XXX: because we don't have an sdiv scev.
-
   // If negative, it wraps around every iteration, but we don't care about that.
   APInt S = SC->getValue()->getValue().abs();
 
-  APInt Dist = APInt::getMaxValue(R->getValue()->getBitWidth()) -
-               R->getValue()->getValue();
+  uint32_t Width = R->getValue()->getBitWidth();
+  APInt Dist = (isSigned ? APInt::getSignedMaxValue(Width)
+                         : APInt::getMaxValue(Width))
+               - R->getValue()->getValue();
 
+  // Because we're looking at distance, we perform an unsigned comparison,
+  // regardless of the sign of the computation.
   if (trueWhenEqual)
     return !S.ult(Dist);
   else
@@ -2961,24 +2962,15 @@
     // m.  So, we count the number of iterations in which {n,+,s} < m is true.
     // Note that we cannot simply return max(m-n,0)/s because it's not safe to
     // treat m-n as signed nor unsigned due to overflow possibility.
+    //
+    // Assuming that the loop will run at least once, we know that it will
+    // run (m-n)/s times.
 
     // First, we get the value of the LHS in the first iteration: n
     SCEVHandle Start = AddRec->getOperand(0);
 
     SCEVHandle One = SE.getIntegerSCEV(1, RHS->getType());
 
-    // Assuming that the loop will run at least once, we know that it will
-    // run (m-n)/s times.
-    SCEVHandle End = RHS;
-
-    if (!executesAtLeastOnce(L, isSigned, trueWhenEqual,
-                             SE.getMinusSCEV(Start, One), RHS)) {
-      // If not, we get the value of the LHS in the first iteration in which
-      // the above condition doesn't hold.  This equals to max(m,n).
-      End = isSigned ? SE.getSMaxExpr(RHS, Start)
-                     : SE.getUMaxExpr(RHS, Start);
-    }
-
     // If the expression is less-than-or-equal to, we need to extend the
     // loop by one iteration.
     //
@@ -2986,16 +2978,23 @@
     // might not divide cleanly. For example, if you have {2,+,5} u< 10 the
     // division would equal one, but the loop runs twice putting the
     // induction variable at 12.
-
+    SCEVHandle End = SE.getAddExpr(RHS, Stride);
     if (!trueWhenEqual)
-      // (Stride - 1) is correct only because we know it's unsigned.
-      // What we really want is to decrease the magnitude of Stride by one.
-      Start = SE.getMinusSCEV(Start, SE.getMinusSCEV(Stride, One));
-    else
-      Start = SE.getMinusSCEV(Start, Stride);
+      End = SE.getMinusSCEV(End, One);
+
+    if (!executesAtLeastOnce(L, isSigned, trueWhenEqual,
+                             SE.getMinusSCEV(Start, One), RHS)) {
+      // If not, we get the value of the LHS in the first iteration in which
+      // the above condition doesn't hold.  This equals to max(m,n).
+      End = isSigned ? SE.getSMaxExpr(End, Start)
+                     : SE.getUMaxExpr(End, Start);
+    }
 
     // Finally, we subtract these two values to get the number of times the
-    // backedge is executed: max(m,n)-n.
+    // backedge is executed: (max(m,n)-n)/s.
+    //
+    // Note that a trip count is always positive. Using SDiv here produces
+    // wrong answers when Start < End.
     return SE.getUDivExpr(SE.getMinusSCEV(End, Start), Stride);
   }
 

Modified: llvm/trunk/test/Analysis/ScalarEvolution/2008-12-08-FiniteSGE.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/2008-12-08-FiniteSGE.ll?rev=61082&r1=61081&r2=61082&view=diff

==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/2008-12-08-FiniteSGE.ll (original)
+++ llvm/trunk/test/Analysis/ScalarEvolution/2008-12-08-FiniteSGE.ll Tue Dec 16 02:30:01 2008
@@ -1,5 +1,4 @@
 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {255 iterations}
-; XFAIL: *
 
 define i32 @foo(i32 %x, i32 %y, i32* %lam, i32* %alp) nounwind {
 bb1.thread:

Modified: llvm/trunk/test/Analysis/ScalarEvolution/2008-12-11-SMaxOverflow.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/2008-12-11-SMaxOverflow.ll?rev=61082&r1=61081&r2=61082&view=diff

==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/2008-12-11-SMaxOverflow.ll (original)
+++ llvm/trunk/test/Analysis/ScalarEvolution/2008-12-11-SMaxOverflow.ll Tue Dec 16 02:30:01 2008
@@ -1,5 +1,4 @@
 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {0 smax}
-; XFAIL: *
 
 define i32 @f(i32 %c.idx.val) {
 

Added: llvm/trunk/test/Analysis/ScalarEvolution/2008-12-14-StrideAndSigned.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/2008-12-14-StrideAndSigned.ll?rev=61082&view=auto

==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/2008-12-14-StrideAndSigned.ll (added)
+++ llvm/trunk/test/Analysis/ScalarEvolution/2008-12-14-StrideAndSigned.ll Tue Dec 16 02:30:01 2008
@@ -0,0 +1,21 @@
+; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& \
+; RUN: grep {(((-1 \\* %i0) + (100005 smax %i0)) /u 5)}
+
+define i32 @foo0(i32 %i0) nounwind {
+entry:
+	br label %bb1
+
+bb:		; preds = %bb1
+	%0 = add i32 %j.0, 1		; <i32> [#uses=1]
+	%1 = add i32 %i.0, 5		; <i32> [#uses=1]
+	br label %bb1
+
+bb1:		; preds = %bb, %entry
+	%j.0 = phi i32 [ 0, %entry ], [ %0, %bb ]		; <i32> [#uses=2]
+	%i.0 = phi i32 [ %i0, %entry ], [ %1, %bb ]		; <i32> [#uses=2]
+	%2 = icmp sgt i32 %i.0, 100000		; <i1> [#uses=1]
+	br i1 %2, label %return, label %bb
+
+return:		; preds = %bb1
+	ret i32 %j.0
+}

Added: llvm/trunk/test/Analysis/ScalarEvolution/2008-12-15-DontUseSDiv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/2008-12-15-DontUseSDiv.ll?rev=61082&view=auto

==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/2008-12-15-DontUseSDiv.ll (added)
+++ llvm/trunk/test/Analysis/ScalarEvolution/2008-12-15-DontUseSDiv.ll Tue Dec 16 02:30:01 2008
@@ -0,0 +1,20 @@
+; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& grep {/u 5}
+
+define i8 @foo0(i8 %i0) nounwind {
+entry:
+	br label %bb1
+
+bb:		; preds = %bb1
+	%0 = add i8 %j.0, 1		; <i8> [#uses=1]
+	%1 = add i8 %i.0, 5		; <i8> [#uses=1]
+	br label %bb1
+
+bb1:		; preds = %bb, %entry
+	%j.0 = phi i8 [ 0, %entry ], [ %0, %bb ]		; <i8> [#uses=2]
+	%i.0 = phi i8 [ %i0, %entry ], [ %1, %bb ]		; <i8> [#uses=2]
+	%2 = icmp sgt i8 %i.0, 100		; <i1> [#uses=1]
+	br i1 %2, label %return, label %bb
+
+return:		; preds = %bb1
+	ret i8 %j.0
+}





More information about the llvm-commits mailing list