From: Chris Lattner Date: Mon, 12 May 2003 14:22:21 +0000 (+0000) Subject: Do not insert multiple initializations for the same value in a PHI node X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=a6e73f19560ee1dd4f5f4b8b645a117d9f93488e;p=oota-llvm.git Do not insert multiple initializations for the same value in a PHI node git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6113 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Target/X86/InstSelectSimple.cpp b/lib/Target/X86/InstSelectSimple.cpp index 1b5ab7a4ae6..4a247419384 100644 --- a/lib/Target/X86/InstSelectSimple.cpp +++ b/lib/Target/X86/InstSelectSimple.cpp @@ -493,18 +493,38 @@ void ISel::SelectPHINodes() { MBB->insert(MBB->begin()+NumPHIs++, LongPhiMI); } + // PHIValues - Map of blocks to incoming virtual registers. We use this + // so that we only initialize one incoming value for a particular block, + // even if the block has multiple entries in the PHI node. + // + std::map PHIValues; + for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)]; + unsigned ValReg; + std::map::iterator EntryIt = + PHIValues.lower_bound(PredMBB); + + if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) { + // We already inserted an initialization of the register for this + // predecessor. Recycle it. + ValReg = EntryIt->second; + + } else { + // Get the incoming value into a virtual register. If it is not + // already available in a virtual register, insert the computation + // code into PredMBB + // + MachineBasicBlock::iterator PI = PredMBB->end(); + while (PI != PredMBB->begin() && + TII.isTerminatorInstr((*(PI-1))->getOpcode())) + --PI; + ValReg = getReg(PN->getIncomingValue(i), PredMBB, PI); + + // Remember that we inserted a value for this PHI for this predecessor + PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg)); + } - // Get the incoming value into a virtual register. If it is not already - // available in a virtual register, insert the computation code into - // PredMBB - // - MachineBasicBlock::iterator PI = PredMBB->end(); - while (PI != PredMBB->begin() && - TII.isTerminatorInstr((*(PI-1))->getOpcode())) - --PI; - unsigned ValReg = getReg(PN->getIncomingValue(i), PredMBB, PI); PhiMI->addRegOperand(ValReg); PhiMI->addMachineBasicBlockOperand(PredMBB); if (LongPhiMI) { diff --git a/lib/Target/X86/X86ISelSimple.cpp b/lib/Target/X86/X86ISelSimple.cpp index 1b5ab7a4ae6..4a247419384 100644 --- a/lib/Target/X86/X86ISelSimple.cpp +++ b/lib/Target/X86/X86ISelSimple.cpp @@ -493,18 +493,38 @@ void ISel::SelectPHINodes() { MBB->insert(MBB->begin()+NumPHIs++, LongPhiMI); } + // PHIValues - Map of blocks to incoming virtual registers. We use this + // so that we only initialize one incoming value for a particular block, + // even if the block has multiple entries in the PHI node. + // + std::map PHIValues; + for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)]; + unsigned ValReg; + std::map::iterator EntryIt = + PHIValues.lower_bound(PredMBB); + + if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) { + // We already inserted an initialization of the register for this + // predecessor. Recycle it. + ValReg = EntryIt->second; + + } else { + // Get the incoming value into a virtual register. If it is not + // already available in a virtual register, insert the computation + // code into PredMBB + // + MachineBasicBlock::iterator PI = PredMBB->end(); + while (PI != PredMBB->begin() && + TII.isTerminatorInstr((*(PI-1))->getOpcode())) + --PI; + ValReg = getReg(PN->getIncomingValue(i), PredMBB, PI); + + // Remember that we inserted a value for this PHI for this predecessor + PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg)); + } - // Get the incoming value into a virtual register. If it is not already - // available in a virtual register, insert the computation code into - // PredMBB - // - MachineBasicBlock::iterator PI = PredMBB->end(); - while (PI != PredMBB->begin() && - TII.isTerminatorInstr((*(PI-1))->getOpcode())) - --PI; - unsigned ValReg = getReg(PN->getIncomingValue(i), PredMBB, PI); PhiMI->addRegOperand(ValReg); PhiMI->addMachineBasicBlockOperand(PredMBB); if (LongPhiMI) {