[llvm-branch-commits] [llvm-branch] r310538 - Merging r309758:

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Aug 9 16:52:05 PDT 2017


Author: hans
Date: Wed Aug  9 16:52:05 2017
New Revision: 310538

URL: http://llvm.org/viewvc/llvm-project?rev=310538&view=rev
Log:
Merging r309758:
------------------------------------------------------------------------
r309758 | sanjoy | 2017-08-01 15:37:58 -0700 (Tue, 01 Aug 2017) | 6 lines

[SCEV/IndVars] Always compute loop exiting values if the backedge count is 0

If SCEV can prove that the backedge taken count for a loop is zero, it does not
need to "understand" a recursive PHI to compute its exiting value.

This should fix PR33885.
------------------------------------------------------------------------

Modified:
    llvm/branches/release_50/   (props changed)
    llvm/branches/release_50/lib/Analysis/ScalarEvolution.cpp
    llvm/branches/release_50/test/Transforms/IndVarSimplify/exit_value_test2.ll

Propchange: llvm/branches/release_50/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Aug  9 16:52:05 2017
@@ -1,3 +1,3 @@
 /llvm/branches/Apple/Pertwee:110850,110961
 /llvm/branches/type-system-rewrite:133420-134817
-/llvm/trunk:155241,308483-308484,308503,308808,308813,308891,308906,308950,308963,308978,308986,309113,309302,309321,309323,309325,309330,309343,309353,309355,309422,309481,309483,309495,309555,309561,309594,309651,309744,309849,309928,309930,310071,310190,310240-310242,310250,310253,310267
+/llvm/trunk:155241,308483-308484,308503,308808,308813,308891,308906,308950,308963,308978,308986,309113,309302,309321,309323,309325,309330,309343,309353,309355,309422,309481,309483,309495,309555,309561,309594,309651,309744,309758,309849,309928,309930,310071,310190,310240-310242,310250,310253,310267

Modified: llvm/branches/release_50/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_50/lib/Analysis/ScalarEvolution.cpp?rev=310538&r1=310537&r2=310538&view=diff
==============================================================================
--- llvm/branches/release_50/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/branches/release_50/lib/Analysis/ScalarEvolution.cpp Wed Aug  9 16:52:05 2017
@@ -7582,6 +7582,25 @@ const SCEV *ScalarEvolution::computeSCEV
             const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
             if (const SCEVConstant *BTCC =
                   dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
+
+              // This trivial case can show up in some degenerate cases where
+              // the incoming IR has not yet been fully simplified.
+              if (BTCC->getValue()->isZero()) {
+                Value *InitValue = nullptr;
+                bool MultipleInitValues = false;
+                for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
+                  if (!LI->contains(PN->getIncomingBlock(i))) {
+                    if (!InitValue)
+                      InitValue = PN->getIncomingValue(i);
+                    else if (InitValue != PN->getIncomingValue(i)) {
+                      MultipleInitValues = true;
+                      break;
+                    }
+                  }
+                  if (!MultipleInitValues && InitValue)
+                    return getSCEV(InitValue);
+                }
+              }
               // Okay, we know how many times the containing loop executes.  If
               // this is a constant evolving PHI node, get the final value at
               // the specified iteration number.

Modified: llvm/branches/release_50/test/Transforms/IndVarSimplify/exit_value_test2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_50/test/Transforms/IndVarSimplify/exit_value_test2.ll?rev=310538&r1=310537&r2=310538&view=diff
==============================================================================
--- llvm/branches/release_50/test/Transforms/IndVarSimplify/exit_value_test2.ll (original)
+++ llvm/branches/release_50/test/Transforms/IndVarSimplify/exit_value_test2.ll Wed Aug  9 16:52:05 2017
@@ -3,15 +3,14 @@
 
 ; Check IndVarSimplify should not replace exit value because or else
 ; udiv will be introduced by expand and the cost will be high.
-;
-; CHECK-LABEL: @_Z3fooPKcjj(
-; CHECK-NOT: udiv
 
 declare void @_Z3mixRjj(i32* dereferenceable(4), i32)
 declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)
 declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
 
 define i32 @_Z3fooPKcjj(i8* nocapture readonly %s, i32 %len, i32 %c) {
+; CHECK-LABEL: @_Z3fooPKcjj(
+; CHECK-NOT: udiv
 entry:
   %a = alloca i32, align 4
   %tmp = bitcast i32* %a to i8*
@@ -50,3 +49,26 @@ while.end:
   call void @llvm.lifetime.end.p0i8(i64 4, i8* %tmp)
   ret i32 %tmp4
 }
+
+define i32 @zero_backedge_count_test(i32 %unknown_init, i32* %unknown_mem) {
+; CHECK-LABEL: @zero_backedge_count_test(
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i32 [ 0, %entry], [ %iv.inc, %loop ]
+  %unknown_phi = phi i32 [ %unknown_init, %entry ], [ %unknown_next, %loop ]
+  %iv.inc = add i32 %iv, 1
+  %be_taken = icmp ne i32 %iv.inc, 1
+  %unknown_next = load volatile i32, i32* %unknown_mem
+  br i1 %be_taken, label %loop, label %leave
+
+leave:
+; We can fold %unknown_phi even though the backedge value for it is completely
+; unknown, since we can prove that the loop's backedge taken count is 0.
+
+; CHECK: leave:
+; CHECK: ret i32 %unknown_init
+  %exit_val = phi i32 [ %unknown_phi, %loop ]
+  ret i32 %exit_val
+}




More information about the llvm-branch-commits mailing list