Teach InstructionSimplify the trick of skipping incoming phi
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
diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp
index 282e0d3..a8288bf 100644
--- a/lib/Analysis/InstructionSimplify.cpp
+++ b/lib/Analysis/InstructionSimplify.cpp
@@ -142,9 +142,12 @@
   // 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 @@
   // 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))