+ // Now loop over all of the incoming arguments, changing them to copy into
+ // the IncomingReg register in the corresponding predecessor basic block.
+ //
+ std::set<MachineBasicBlock*> MBBsInsertedInto;
+ for (int i = MPhi->getNumOperands() - 1; i >= 2; i-=2) {
+ unsigned SrcReg = MPhi->getOperand(i-1).getReg();
+ assert(MRegisterInfo::isVirtualRegister(SrcReg) &&
+ "Machine PHI Operands must all be virtual registers!");
+
+ // Get the MachineBasicBlock equivalent of the BasicBlock that is the
+ // source path the PHI.
+ MachineBasicBlock &opBlock = *MPhi->getOperand(i).getMachineBasicBlock();
+
+ // Check to make sure we haven't already emitted the copy for this block.
+ // This can happen because PHI nodes may have multiple entries for the
+ // same basic block.
+ if (!MBBsInsertedInto.insert(&opBlock).second)
+ continue; // If the copy has already been emitted, we're done.
+
+ // Get an iterator pointing to the first terminator in the block (or end()).
+ // This is the point where we can insert a copy if we'd like to.
+ MachineBasicBlock::iterator I = opBlock.getFirstTerminator();
+
+ // Insert the copy.
+ RegInfo->copyRegToReg(opBlock, I, IncomingReg, SrcReg, RC, RC);
+
+ // Now update live variable information if we have it. Otherwise we're done
+ if (!LV) continue;
+
+ // We want to be able to insert a kill of the register if this PHI
+ // (aka, the copy we just inserted) is the last use of the source
+ // value. Live variable analysis conservatively handles this by
+ // saying that the value is live until the end of the block the PHI
+ // entry lives in. If the value really is dead at the PHI copy, there
+ // will be no successor blocks which have the value live-in.
+ //
+ // Check to see if the copy is the last use, and if so, update the
+ // live variables information so that it knows the copy source
+ // instruction kills the incoming value.
+ //
+ LiveVariables::VarInfo &InRegVI = LV->getVarInfo(SrcReg);
+ InRegVI.UsedBlocks[opBlock.getNumber()] = true;
+
+ // Loop over all of the successors of the basic block, checking to see
+ // if the value is either live in the block, or if it is killed in the
+ // block. Also check to see if this register is in use by another PHI
+ // node which has not yet been eliminated. If so, it will be killed
+ // at an appropriate point later.