From 5b097014f25f4ba918974651ad027b5981294666 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sat, 31 Oct 2009 14:22:52 +0000 Subject: [PATCH] Reapply r85634, with the bug fixed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85655 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index e1741a00676..c2194c460fd 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -10981,22 +10981,24 @@ 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); + // If there are multiple PHIs, sort their operands so that they all list + // the blocks in the same order. This will help identical PHIs be eliminated + // by other passes. Other passes shouldn't depend on this for correctness + // however. + PHINode *FirstPN = cast(PN.getParent()->begin()); + if (&PN != FirstPN) + for (unsigned i = 0, e = FirstPN->getNumIncomingValues(); i != e; ++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); + BasicBlock *BBB = FirstPN->getIncomingBlock(i); + if (BBA != BBB) { + Value *VA = PN.getIncomingValue(i); + unsigned j = PN.getBasicBlockIndex(BBB); + Value *VB = PN.getIncomingValue(j); + PN.setIncomingBlock(i, BBB); + PN.setIncomingValue(i, VB); + PN.setIncomingBlock(j, BBA); + PN.setIncomingValue(j, VA); + } } return 0;