indvars -disable-iv-rewrite fix: derived GEP IVs
authorAndrew Trick <atrick@apple.com>
Wed, 20 Jul 2011 02:08:58 +0000 (02:08 +0000)
committerAndrew Trick <atrick@apple.com>
Wed, 20 Jul 2011 02:08:58 +0000 (02:08 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135558 91177308-0d34-0410-b5e6-96231b3b80d8

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

index b75db7d2babd9c1019422b63d1ede0ab77a7f51c..55094b9d8028e453bd539be3d5d938d3200bf851 100644 (file)
@@ -1419,6 +1419,11 @@ void IndVarSimplify::SimplifyCongruentIVs(Loop *L) {
     if (Inserted)
       continue;
     PHINode *OrigPhi = Pos->second;
+
+    // If one phi derives from the other via GEPs, types may differ.
+    if (OrigPhi->getType() != Phi->getType())
+      continue;
+
     // Replacing the congruent phi is sufficient because acyclic redundancy
     // elimination, CSE/GVN, should handle the rest. However, once SCEV proves
     // that a phi is congruent, it's almost certain to be the head of an IV
@@ -1430,6 +1435,7 @@ void IndVarSimplify::SimplifyCongruentIVs(Loop *L) {
       Instruction *IsomorphicInc =
         cast<Instruction>(Phi->getIncomingValueForBlock(LatchBlock));
       if (OrigInc != IsomorphicInc &&
+          OrigInc->getType() == IsomorphicInc->getType() &&
           SE->getSCEV(OrigInc) == SE->getSCEV(IsomorphicInc) &&
           HoistStep(OrigInc, IsomorphicInc, DT)) {
         DEBUG(dbgs() << "INDVARS: Eliminated congruent iv.inc: "
index 9605670b86e1f022f370dcc7a64d9058282b1217..bb951c6f182ad878b30683bba3aef9fff68e8834 100644 (file)
@@ -125,9 +125,9 @@ return:
   ret void
 }
 
-%struct = type { i32 }
+%structI = type { i32 }
 
-define void @bitcastiv(i32 %start, i32 %limit, i32 %step, %struct* %base)
+define void @bitcastiv(i32 %start, i32 %limit, i32 %step, %structI* %base)
 nounwind
 {
 entry:
@@ -142,12 +142,12 @@ entry:
 ; CHECK: exit:
 loop:
   %iv = phi i32 [%start, %entry], [%next, %loop]
-  %p = phi %struct* [%base, %entry], [%pinc, %loop]
-  %adr = getelementptr %struct* %p, i32 0, i32 0
+  %p = phi %structI* [%base, %entry], [%pinc, %loop]
+  %adr = getelementptr %structI* %p, i32 0, i32 0
   store i32 3, i32* %adr
-  %pp = bitcast %struct* %p to i32*
+  %pp = bitcast %structI* %p to i32*
   store i32 4, i32* %pp
-  %pinc = getelementptr %struct* %p, i32 1
+  %pinc = getelementptr %structI* %p, i32 1
   %next = add i32 %iv, 1
   %cond = icmp ne i32 %next, %limit
   br i1 %cond, label %loop, label %exit
@@ -320,3 +320,26 @@ return:
   %sum4 = add i32 %sum1, %l.next
   ret i32 %sum4
 }
+
+; Test a GEP IV that is derived from another GEP IV by a nop gep that
+; lowers the type without changing the expression.
+%structIF = type { i32, float }
+
+define void @congruentgepiv(%structIF* %base) nounwind uwtable ssp {
+entry:
+  %first = getelementptr inbounds %structIF* %base, i64 0, i32 0
+  br label %loop
+
+loop:
+  %ptr.iv = phi %structIF* [ %ptr.inc, %latch ], [ %base, %entry ]
+  %next = phi i32* [ %next.inc, %latch ], [ %first, %entry ]
+  br i1 undef, label %latch, label %exit
+
+latch:                         ; preds = %for.inc50.i
+  %ptr.inc = getelementptr inbounds %structIF* %ptr.iv, i64 1
+  %next.inc = getelementptr inbounds %structIF* %ptr.inc, i64 0, i32 0
+  br label %loop
+
+exit:
+  ret void
+}