Revert "[IndVarSimplify] Rewrite loop exit values with their initial values from...
authorTobias Grosser <tobias@grosser.es>
Tue, 3 Nov 2015 07:14:39 +0000 (07:14 +0000)
committerTobias Grosser <tobias@grosser.es>
Tue, 3 Nov 2015 07:14:39 +0000 (07:14 +0000)
Commit 251839 triggers miscompiles on some bots:

http://lab.llvm.org:8011/builders/perf-x86_64-penryn-O3-polly-fast/builds/13723

(The commit is listed in 13722, but due to an existing failure introduced in
13721 and reverted in 13723 the failure is only visible in 13723)

To verify r251839 is indeed the only change that triggered the buildbot failures
and to ensure the buildbots remain green while investigating I temporarily
revert this commit. At the current state it is unclear if this commit introduced
some miscompile or if it only exposed code to Polly that is subsequently
miscompiled by Polly.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251901 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/IndVarSimplify.cpp
test/Transforms/IndVarSimplify/rewrite-loop-exit-value.ll [deleted file]
test/Transforms/LoopUnroll/scevunroll.ll

index 5ce76c95832ffc4b90c1854787b8e5c044151ab7..3dd3cfd4187e3e95d27e000b0056b13318f4dba3 100644 (file)
@@ -132,7 +132,6 @@ private:
 
   bool canLoopBeDeleted(Loop *L, SmallVector<RewritePhi, 8> &RewritePhiSet);
   void rewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter);
-  void rewriteFirstIterationLoopExitValues(Loop *L);
 
   Value *linearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount,
                                    PHINode *IndVar, SCEVExpander &Rewriter);
@@ -701,73 +700,6 @@ void IndVarSimplify::rewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter) {
   Rewriter.clearInsertPoint();
 }
 
-//===---------------------------------------------------------------------===//
-// rewriteFirstIterationLoopExitValues: Rewrite loop exit values if we know
-// they will exit at the first iteration.
-//===---------------------------------------------------------------------===//
-
-/// Check to see if this loop has loop invariant conditions which lead to loop
-/// exits. If so, we know that if the exit path is taken, it is at the first
-/// loop iteration. This lets us predict exit values of PHI nodes that live in
-/// loop header.
-void IndVarSimplify::rewriteFirstIterationLoopExitValues(Loop *L) {
-  // Verify the input to the pass is already in LCSSA form.
-  assert(L->isLCSSAForm(*DT));
-
-  SmallVector<BasicBlock *, 8> ExitBlocks;
-  L->getUniqueExitBlocks(ExitBlocks);
-
-  for (auto *ExitBB : ExitBlocks) {
-    BasicBlock::iterator begin = ExitBB->begin();
-    // If there are no more PHI nodes in this exit block, then no more
-    // values defined inside the loop are used on this path.
-    while (auto *PN = dyn_cast<PHINode>(begin++)) {
-      for (unsigned IncomingValIdx = 0, e = PN->getNumIncomingValues();
-          IncomingValIdx != e; ++IncomingValIdx) {
-        auto *IncomingBB = PN->getIncomingBlock(IncomingValIdx);
-        if (!L->contains(IncomingBB))
-          continue;
-
-        // Get condition that leads to the exit path.
-        auto *TermInst = IncomingBB->getTerminator();
-
-        Value *Cond = nullptr;
-        if (auto *BI = dyn_cast<BranchInst>(TermInst)) {
-          // Must be a conditional branch, otherwise the block
-          // should not be in the loop.
-          Cond = BI->getCondition();
-        } else if (auto *SI = dyn_cast<SwitchInst>(TermInst))
-          Cond = SI->getCondition();
-        else
-          continue;
-
-        // All non-instructions are loop-invariant.
-        if (isa<Instruction>(Cond) && !L->isLoopInvariant(Cond))
-            continue;
-
-        auto *ExitVal =
-            dyn_cast<PHINode>(PN->getIncomingValue(IncomingValIdx));
-
-        // Only deal with PHIs.
-        if (!ExitVal)
-          continue;
-
-        // If ExitVal is a PHI on the loop header, then we know its
-        // value along this exit because the exit can only be taken
-        // on the first iteration.
-        auto *LoopPreheader = L->getLoopPreheader();
-        assert(LoopPreheader && "Invalid loop");
-        if (ExitVal->getBasicBlockIndex(LoopPreheader) != -1) {
-          assert(ExitVal->getParent() == L->getHeader() &&
-              "ExitVal must be in loop header");
-          PN->setIncomingValue(IncomingValIdx,
-              ExitVal->getIncomingValueForBlock(LoopPreheader));
-        }
-      }
-    }
-  }
-}
-
 /// Check whether it is possible to delete the loop after rewriting exit
 /// value. If it is possible, ignore ReplaceExitValue and do rewriting
 /// aggressively.
@@ -2241,11 +2173,6 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
   // loop may be sunk below the loop to reduce register pressure.
   sinkUnusedInvariants(L);
 
-  // rewriteFirstIterationLoopExitValues does not rely on the computation of
-  // trip count and therefore can further simplify exit values in addition to
-  // rewriteLoopExitValues.
-  rewriteFirstIterationLoopExitValues(L);
-
   // Clean up dead instructions.
   Changed |= DeleteDeadPHIs(L->getHeader(), TLI);
   // Check a post-condition.
diff --git a/test/Transforms/IndVarSimplify/rewrite-loop-exit-value.ll b/test/Transforms/IndVarSimplify/rewrite-loop-exit-value.ll
deleted file mode 100644 (file)
index 1302e86..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-; RUN: opt -indvars -instcombine -S < %s | FileCheck %s
-
-;; Test that loop's exit value is rewritten to its initial
-;; value from loop preheader
-define i32 @test1(i32* %var) {
-; CHECK-LABEL: @test1
-entry:
- %cond = icmp eq i32* %var, null 
- br label %header
-
-header:
- %phi_indvar = phi i32 [0, %entry], [%indvar, %loop]
- br i1 %cond, label %loop, label %exit
-
-loop:
- %indvar = add i32 %phi_indvar, 1
- br label %header
-
-exit:
-; CHECK: ret i32 0
- ret i32 %phi_indvar
-}
-
-
-;; Test that inner loop's exit value is first rewritten to outer
-;; loop's induction variable, and then further rewritten to a 
-;; constant when process outer loop.
-define i32 @test2(i32* %var1, i32* %var2) {
-; CHECK-LABEL: @test2
-entry:
- %cond1 = icmp eq i32* %var1, null 
- %cond2 = icmp eq i32* %var2, null 
- br label %outer_header
-
-outer_header:
- %phi_outer = phi i32 [0, %entry], [%indvar_outer, %inner_exit]
- br label %inner_header
-inner_header:
- %phi_inner = phi i32 [%phi_outer, %outer_header], [%indvar_inner, %loop]
- br i1 %cond1, label %loop, label %exit
-
-loop:
- %indvar_inner = add i32 %phi_inner, 1
- br i1 %cond2, label %inner_header, label %inner_exit
-inner_exit:
- %indvar_outer = add i32 %phi_outer, 1
- br label %outer_header
-
-exit:
-;; %phi_inner is first rewritten to %phi_outer
-;; and then %phi_outer is rewritten to 0
- %ret_val = add i32 %phi_inner, %phi_outer
-; CHECK: ret i32 0
- ret i32 %ret_val
-}
-
-;; Test that we can not rewrite loop exit value if it's not
-;; a phi node (%indvar is an add instruction in this test).
-define i32 @test3(i32* %var) {
-; CHECK-LABEL: @test3
-entry:
- %cond = icmp eq i32* %var, null 
- br label %header
-
-header:
- %phi_indvar = phi i32 [0, %entry], [%indvar, %header]
- %indvar = add i32 %phi_indvar, 1
- br i1 %cond, label %header, label %exit
-
-exit:
-; CHECK: ret i32 %indvar
- ret i32 %indvar
-}
\ No newline at end of file
index afee41e11aaac6c57b119f957492a85c4c4494f4..a5c9a6efacf38fdcad66c97144ff5ff3040e2b32 100644 (file)
@@ -184,7 +184,7 @@ for.body87:
 ; CHECK: for.body:
 ; CHECK: %b.03 = phi i32 [ 0, %entry ], [ %add, %for.cond ]
 ; CHECK: return:
-; CHECK: %b.03.lcssa = phi i32 [ %b.03, %for.body ], [ 0, %for.cond ]
+; CHECK: %b.03.lcssa = phi i32 [ %b.03, %for.body ], [ %b.03, %for.cond ]
 define void @nsw_latch(i32* %a) nounwind {
 entry:
   br label %for.body