Sort the incoming values in PHI nodes to match the predecessor order.
authorDan Gohman <gohman@apple.com>
Fri, 30 Oct 2009 22:22:22 +0000 (22:22 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 30 Oct 2009 22:22:22 +0000 (22:22 +0000)
This helps expose duplicate PHIs, which will make it easier for them
to be eliminated.

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

lib/Transforms/Scalar/InstructionCombining.cpp

index 0ca3114c44d009dab17f3f13dbb942d61642c25a..e1741a006760e8b14f61b624aff8f3c98881912c 100644 (file)
@@ -10980,6 +10980,25 @@ Instruction *InstCombiner::visitPHINode(PHINode &PN) {
       }
     }
   }
+
+  // Sort the PHI node operands to match the pred iterator order. This will
+  // help identical PHIs be eliminated by other passes. Other passes shouldn't
+  // depend on this for correctness however.
+  unsigned i = 0;
+  for (pred_iterator PI = pred_begin(PN.getParent()),
+       PE = pred_end(PN.getParent()); PI != PE; ++PI, ++i)
+    if (PN.getIncomingBlock(i) != *PI) {
+      unsigned j = PN.getBasicBlockIndex(*PI);
+      Value *VA = PN.getIncomingValue(i);
+      BasicBlock *BBA = PN.getIncomingBlock(i);
+      Value *VB = PN.getIncomingValue(j);
+      BasicBlock *BBB = PN.getIncomingBlock(j);
+      PN.setIncomingBlock(i, BBB);
+      PN.setIncomingValue(i, VB);
+      PN.setIncomingBlock(j, BBA);
+      PN.setIncomingValue(j, VA);
+    }
+
   return 0;
 }