indvars -disable-iv-rewrite: handle an edge case involving identity phis.
authorAndrew Trick <atrick@apple.com>
Thu, 30 Jun 2011 01:27:23 +0000 (01:27 +0000)
committerAndrew Trick <atrick@apple.com>
Thu, 30 Jun 2011 01:27:23 +0000 (01:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134124 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/IndVarSimplify.cpp
test/Transforms/IndVarSimplify/no-iv-rewrite.ll

index 2a3e4725a1375c2d2f67aad46cbe0da922cc4556..00f8831eb2864dc15c094b9637adeacef36885d4 100644 (file)
@@ -1015,9 +1015,9 @@ bool IndVarSimplify::EliminateIVUser(Instruction *UseInst,
       (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
     return false;
 
-  UseInst->replaceAllUsesWith(IVOperand);
-
   DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
+
+  UseInst->replaceAllUsesWith(IVOperand);
   ++NumElimIdentity;
   Changed = true;
   DeadInsts.push_back(UseInst);
@@ -1037,7 +1037,9 @@ static void pushIVUsers(
 
     // Avoid infinite or exponential worklist processing.
     // Also ensure unique worklist users.
-    if (Simplified.insert(User))
+    // If Def is a LoopPhi, it may not be in the Simplified set, so check for
+    // self edges first.
+    if (User != Def && Simplified.insert(User))
       SimpleIVUsers.push_back(std::make_pair(User, Def));
   }
 }
@@ -1111,6 +1113,9 @@ void IndVarSimplify::SimplifyIVUsersNoRewrite(Loop *L, SCEVExpander &Rewriter) {
       // Use-def pairs if IVUsers waiting to be processed for CurrIV.
       SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
 
+      // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
+      // called multiple times for the same LoopPhi. This is the proper thing to
+      // do for loop header phis that use each other.
       pushIVUsers(CurrIV, Simplified, SimpleIVUsers);
 
       while (!SimpleIVUsers.empty()) {
index 3395be28361f2db0d0f19f0dda323a8fbe8c94de..01871707a3fa870386c0673b8bd7a5da750d58b2 100644 (file)
@@ -23,7 +23,7 @@ ph:
 ; sext should be eliminated while preserving gep inboundsness.
 ; CHECK-NOT: sext
 ; CHECK: getelementptr inbounds
-; CHECK: exit
+; CHECK: exit:
 loop:
   %i.02 = phi i32 [ 0, %ph ], [ %iinc, %loop ]
   %s.01 = phi i32 [ 0, %ph ], [ %sinc, %loop ]
@@ -64,7 +64,7 @@ ph:
 ; CHECK: getelementptr inbounds
 ; %vall sext should obviously not be eliminated
 ; CHECK: sext
-; CHECK: exit
+; CHECK: exit:
 loop:
   %i.02 = phi i32 [ 0, %ph ], [ %iinc, %loop ]
   %s.01 = phi i64 [ 0, %ph ], [ %sinc, %loop ]
@@ -108,7 +108,7 @@ ph:
 ; Preserve gep inboundsness, and don't factor it.
 ; CHECK: getelementptr inbounds i32* %ptriv, i32 1
 ; CHECK-NOT: add
-; CHECK: exit
+; CHECK: exit:
 loop:
   %ptriv = phi i32* [ %first, %ph ], [ %ptrpost, %loop ]
   %ofs = sext i32 %idx to i64
@@ -139,7 +139,7 @@ entry:
 ; CHECK: phi i32
 ; CHECK: bitcast
 ; CHECK: getelementptr
-; CHECK: exit
+; CHECK: exit:
 loop:
   %iv = phi i32 [%start, %entry], [%next, %loop]
   %p = phi %struct* [%base, %entry], [%pinc, %loop]
@@ -157,12 +157,13 @@ exit:
 }
 
 define void @maxvisitor(i32 %limit, i32* %base) nounwind {
-entry: br label %loop
+entry:
+ br label %loop
 
 ; CHECK: loop:
 ; CHECK: phi i64
 ; CHECK: trunc
-; CHECK: exit
+; CHECK: exit:
 loop:
   %idx = phi i32 [ 0, %entry ], [ %idx.next, %loop.inc ]
   %max = phi i32 [ 0, %entry ], [ %max.next, %loop.inc ]
@@ -186,4 +187,28 @@ loop.inc:
 
 exit:
   ret void
-}
\ No newline at end of file
+}
+
+; CHECK: loop:
+; CHECK: phi i32
+; CHECK-NOT: phi
+; CHECK: exit:
+define void @identityphi(i32 %limit) nounwind {
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i32 [ 0, %entry], [ %iv.next, %control ]
+  br i1 undef, label %if.then, label %control
+
+if.then:
+  br label %control
+
+control:
+  %iv.next = phi i32 [ %iv, %loop ], [ undef, %if.then ]
+  %cmp = icmp slt i32 %iv.next, %limit
+  br i1 %cmp, label %loop, label %exit
+
+exit:
+  ret void
+}