[llvm-branch-commits] [llvm-branch] r223637 - Merging rr217102:

David Majnemer david.majnemer at gmail.com
Mon Dec 8 00:50:22 PST 2014


Author: majnemer
Date: Mon Dec  8 02:50:22 2014
New Revision: 223637

URL: http://llvm.org/viewvc/llvm-project?rev=223637&view=rev
Log:
Merging rr217102:
------------------------------------------------------------------------
r217102 | majnemer | 2014-09-03 16:03:18 -0700 (Wed, 03 Sep 2014) | 11 lines

IndVarSimplify: Don't let LFTR compare against a poison value

LinearFunctionTestReplace tries to use the *next* indvar to compare
against when possible.  However, it may be the case that the calculation
for the next indvar has NUW/NSW flags and that it may only be safely
used inside the loop.  Using it in a comparison to calculate the exit
condition could result in observing poison.

This fixes PR20680.

Differential Revision: http://reviews.llvm.org/D5174
------------------------------------------------------------------------

Added:
    llvm/branches/release_35/test/Transforms/IndVarSimplify/pr20680.ll
      - copied unchanged from r217102, llvm/trunk/test/Transforms/IndVarSimplify/pr20680.ll
Modified:
    llvm/branches/release_35/   (props changed)
    llvm/branches/release_35/lib/Transforms/Scalar/IndVarSimplify.cpp
    llvm/branches/release_35/test/Transforms/IndVarSimplify/2011-10-27-lftrnull.ll
    llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-address-space-pointers.ll
    llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-extend-const.ll
    llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-reuse.ll

Propchange: llvm/branches/release_35/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Dec  8 02:50:22 2014
@@ -1,3 +1,3 @@
 /llvm/branches/Apple/Pertwee:110850,110961
 /llvm/branches/type-system-rewrite:133420-134817
-/llvm/trunk:155241,213653,213665,213726,213749,213773,213793,213798-213799,213815,213847,213880,213883-213884,213894-213896,213899,213915,213966,213999,214060,214129,214180,214287,214331,214423,214429,214519,214670,214674,214679,215685,215711,215806,216064,216262,216531,216920,217257,218745,221009,221408,221453,221501,222338,222376,223163,223170-223171
+/llvm/trunk:155241,213653,213665,213726,213749,213773,213793,213798-213799,213815,213847,213880,213883-213884,213894-213896,213899,213915,213966,213999,214060,214129,214180,214287,214331,214423,214429,214519,214670,214674,214679,215685,215711,215806,216064,216262,216531,216920,217102,217257,218745,221009,221408,221453,221501,222338,222376,223163,223170-223171

Modified: llvm/branches/release_35/lib/Transforms/Scalar/IndVarSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/lib/Transforms/Scalar/IndVarSimplify.cpp?rev=223637&r1=223636&r2=223637&view=diff
==============================================================================
--- llvm/branches/release_35/lib/Transforms/Scalar/IndVarSimplify.cpp (original)
+++ llvm/branches/release_35/lib/Transforms/Scalar/IndVarSimplify.cpp Mon Dec  8 02:50:22 2014
@@ -1623,15 +1623,27 @@ LinearFunctionTestReplace(Loop *L,
   // compare against the post-incremented value, otherwise we must compare
   // against the preincremented value.
   if (L->getExitingBlock() == L->getLoopLatch()) {
-    // Add one to the "backedge-taken" count to get the trip count.
-    // This addition may overflow, which is valid as long as the comparison is
-    // truncated to BackedgeTakenCount->getType().
-    IVCount = SE->getAddExpr(BackedgeTakenCount,
-                             SE->getConstant(BackedgeTakenCount->getType(), 1));
     // The BackedgeTaken expression contains the number of times that the
     // backedge branches to the loop header.  This is one less than the
     // number of times the loop executes, so use the incremented indvar.
-    CmpIndVar = IndVar->getIncomingValueForBlock(L->getExitingBlock());
+    llvm::Value *IncrementedIndvar = IndVar->getIncomingValueForBlock(L->getExitingBlock());
+    const auto *IncrementedIndvarSCEV =
+        cast<SCEVAddRecExpr>(SE->getSCEV(IncrementedIndvar));
+    // It is unsafe to use the incremented indvar if it has a wrapping flag, we
+    // don't want to compare against a poison value.  Check the SCEV that
+    // corresponds to the incremented indvar, the SCEVExpander will only insert
+    // flags in the IR if the SCEV originally had wrapping flags.
+    if (ScalarEvolution::maskFlags(IncrementedIndvarSCEV->getNoWrapFlags(),
+                                   SCEV::FlagNUW | SCEV::FlagNSW) ==
+        SCEV::FlagAnyWrap) {
+      // Add one to the "backedge-taken" count to get the trip count.
+      // This addition may overflow, which is valid as long as the comparison is
+      // truncated to BackedgeTakenCount->getType().
+      IVCount =
+          SE->getAddExpr(BackedgeTakenCount,
+                         SE->getConstant(BackedgeTakenCount->getType(), 1));
+      CmpIndVar = IncrementedIndvar;
+    }
   }
 
   Value *ExitCnt = genLoopLimit(IndVar, IVCount, L, Rewriter, SE);

Modified: llvm/branches/release_35/test/Transforms/IndVarSimplify/2011-10-27-lftrnull.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/test/Transforms/IndVarSimplify/2011-10-27-lftrnull.ll?rev=223637&r1=223636&r2=223637&view=diff
==============================================================================
--- llvm/branches/release_35/test/Transforms/IndVarSimplify/2011-10-27-lftrnull.ll (original)
+++ llvm/branches/release_35/test/Transforms/IndVarSimplify/2011-10-27-lftrnull.ll Mon Dec  8 02:50:22 2014
@@ -6,7 +6,7 @@ target triple = "thumbv7-apple-darwin"
 
 ; CHECK-LABEL: @test(
 ; CHECK: if.end.i126:
-; CHECK: %exitcond = icmp ne i8* %incdec.ptr.i, getelementptr (i8* null, i32 undef)
+; CHECK: %exitcond = icmp ne i8* %destYPixelPtr.010.i, getelementptr (i8* null, i32 undef)
 define void @test() nounwind {
 entry:
   br label %while.cond

Modified: llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-address-space-pointers.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-address-space-pointers.ll?rev=223637&r1=223636&r2=223637&view=diff
==============================================================================
--- llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-address-space-pointers.ll (original)
+++ llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-address-space-pointers.ll Mon Dec  8 02:50:22 2014
@@ -11,7 +11,7 @@ entry:
   br i1 %cmp1, label %for.body, label %for.end
 
 ; Make sure the added GEP has the right index type
-; CHECK: %lftr.limit = getelementptr i8 addrspace(2)* %base, i8 %0
+; CHECK: %lftr.limit = getelementptr i8 addrspace(2)* %base, i8
 
 ; CHECK: for.body:
 ; CHECK: phi i8 addrspace(2)*
@@ -43,7 +43,7 @@ entry:
   br i1 %cmp1, label %for.body, label %for.end
 
 ; Make sure the added GEP has the right index type
-; CHECK: %lftr.limit = getelementptr i8 addrspace(3)* %base, i16 %0
+; CHECK: %lftr.limit = getelementptr i8 addrspace(3)* %base, i16
 
 ; CHECK: for.body:
 ; CHECK: phi i8 addrspace(3)*

Modified: llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-extend-const.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-extend-const.ll?rev=223637&r1=223636&r2=223637&view=diff
==============================================================================
--- llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-extend-const.ll (original)
+++ llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-extend-const.ll Mon Dec  8 02:50:22 2014
@@ -2,7 +2,7 @@
 
 ; CHECK-LABEL: @foo(
 ; CHECK-NOT: %lftr.wideiv = trunc i32 %indvars.iv.next to i16
-; CHECK: %exitcond = icmp ne i32 %indvars.iv.next, 512
+; CHECK: %exitcond = icmp ne i32 %indvars.iv, 511
 define void @foo() #0 {
 entry:
   br label %for.body
@@ -21,7 +21,7 @@ for.end:
 
 ; Check that post-incrementing the backedge taken count does not overflow.
 ; CHECK-LABEL: @postinc(
-; CHECK: icmp eq i32 %indvars.iv.next, 256
+; CHECK: icmp eq i32 %indvars.iv, 255
 define i32 @postinc() #0 {
 entry:
   br label %do.body

Modified: llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-reuse.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-reuse.ll?rev=223637&r1=223636&r2=223637&view=diff
==============================================================================
--- llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-reuse.ll (original)
+++ llvm/branches/release_35/test/Transforms/IndVarSimplify/lftr-reuse.ll Mon Dec  8 02:50:22 2014
@@ -82,15 +82,23 @@ exit:
 ; Perform LFTR without generating extra preheader code.
 define void @guardedloop([0 x double]* %matrix, [0 x double]* %vector,
                          i32 %irow, i32 %ilead) nounwind {
-; CHECK: entry:
-; CHECK-NOT: zext
-; CHECK-NOT: add
-; CHECK: loop:
-; CHECK: phi i64
-; CHECK: phi i64
+; CHECK-LABEL: @guardedloop(
+; CHECK-LABEL: entry:
+; CHECK-NEXT: %[[cmp:.*]] = icmp slt i32 1, %irow
+; CHECK-NEXT: br i1 %[[cmp]], label %[[loop_preheader:.*]], label %[[return:.*]]
+
+; CHECK: [[loop_preheader]]:
+; CHECK-NEXT: %[[sext:.*]] = sext i32 %ilead to i64
+; CHECK-NEXT: %[[add:.*]] = add i32 %irow, -1
+; CHECK-NEXT: br label %[[loop:.*]]
+
+; CHECK: [[loop]]:
+; CHECK-NEXT: %[[indvars_iv2:.*]] = phi i64
+; CHECK-NEXT: phi i64
 ; CHECK-NOT: phi
-; CHECK: icmp ne
-; CHECK: br i1
+; CHECK: %[[lftr_wideiv:.*]] = trunc i64 %[[indvars_iv2]] to i32
+; CHECK-NEXT: %[[exitcond:.*]] = icmp ne i32 %[[lftr_wideiv]], %[[add]]
+; CHECK-NEXT: br i1 %[[exitcond]], label %[[loop]], label
 entry:
   %cmp = icmp slt i32 1, %irow
   br i1 %cmp, label %loop, label %return





More information about the llvm-branch-commits mailing list