Teach InstructionSimplify the trick of skipping incoming phi
authorDuncan Sands <baldrick@free.fr>
Mon, 15 Nov 2010 17:52:45 +0000 (17:52 +0000)
committerDuncan Sands <baldrick@free.fr>
Mon, 15 Nov 2010 17:52:45 +0000 (17:52 +0000)
values that are equal to the phi itself.

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

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstCombine/phi.ll

index 282e0d31bed77a9b190708bc6650f202464e9274..a8288bf67a97fb04418733c9b41912f5a8feb389 100644 (file)
@@ -142,9 +142,12 @@ static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS,
   // Evaluate the BinOp on the incoming phi values.
   Value *CommonValue = 0;
   for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
+    Value *Incoming = PI->getIncomingValue(i);
+    // If the incoming value is the phi node itself, it can be safely skipped.
+    if (Incoming == PI) continue;
     Value *V = PI == LHS ?
-      SimplifyBinOp(Opcode, PI->getIncomingValue(i), RHS, TD, MaxRecurse) :
-      SimplifyBinOp(Opcode, LHS, PI->getIncomingValue(i), TD, MaxRecurse);
+      SimplifyBinOp(Opcode, Incoming, RHS, TD, MaxRecurse) :
+      SimplifyBinOp(Opcode, LHS, Incoming, TD, MaxRecurse);
     // If the operation failed to simplify, or simplified to a different value
     // to previously, then give up.
     if (!V || (CommonValue && V != CommonValue))
@@ -172,8 +175,10 @@ static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
   // Evaluate the BinOp on the incoming phi values.
   Value *CommonValue = 0;
   for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
-    Value *V = SimplifyCmpInst(Pred, PI->getIncomingValue(i), RHS, TD,
-                               MaxRecurse);
+    Value *Incoming = PI->getIncomingValue(i);
+    // If the incoming value is the phi node itself, it can be safely skipped.
+    if (Incoming == PI) continue;
+    Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, MaxRecurse);
     // If the operation failed to simplify, or simplified to a different value
     // to previously, then give up.
     if (!V || (CommonValue && V != CommonValue))
index 1181aefdb385421acd40ffa59c87b1e669a95fcd..c3e034fb5c10bfd01c5c47a01fbc6d5704b7fa08 100644 (file)
@@ -469,3 +469,22 @@ ret:
 ; CHECK: @test20
 ; CHECK: ret i1 false
 }
+
+define i1 @test21(i1 %c1, i1 %c2) {
+  %a = alloca i32
+  %b = alloca i32
+  %c = alloca i32
+  br i1 %c1, label %true, label %false
+true:
+  br label %loop
+false:
+  br label %loop
+loop:
+  %p = phi i32* [ %a, %true ], [ %b, %false ], [ %p, %loop ]
+  %r = icmp eq i32* %p, %c
+  br i1 %c2, label %ret, label %loop
+ret:
+  ret i1 %r
+; CHECK: @test21
+; CHECK: ret i1 false
+}