Do a dominator walk when scheduling copies, rather than a DFS on the CFG. Also, fix a few problems when creating

live intervals for temporaries created by phi elimination.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54483 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson
2008-08-07 18:28:07 +00:00
parent c93346af8e
commit 724acf1c52

View File

@@ -139,7 +139,7 @@ namespace {
std::vector<StrongPHIElimination::DomForestNode*>& DF, std::vector<StrongPHIElimination::DomForestNode*>& DF,
std::vector<std::pair<unsigned, unsigned> >& locals); std::vector<std::pair<unsigned, unsigned> >& locals);
void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed); void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed);
void InsertCopies(MachineBasicBlock* MBB, void InsertCopies(MachineDomTreeNode* MBB,
SmallPtrSet<MachineBasicBlock*, 16>& v); SmallPtrSet<MachineBasicBlock*, 16>& v);
void mergeLiveIntervals(unsigned primary, unsigned secondary, void mergeLiveIntervals(unsigned primary, unsigned secondary,
MachineBasicBlock* pred); MachineBasicBlock* pred);
@@ -303,10 +303,9 @@ static bool isLiveIn(unsigned r, MachineBasicBlock* MBB,
static bool isLiveOut(unsigned r, MachineBasicBlock* MBB, static bool isLiveOut(unsigned r, MachineBasicBlock* MBB,
LiveIntervals& LI) { LiveIntervals& LI) {
for (MachineBasicBlock::succ_iterator PI = MBB->succ_begin(), for (MachineBasicBlock::succ_iterator PI = MBB->succ_begin(),
E = MBB->succ_end(); PI != E; ++PI) { E = MBB->succ_end(); PI != E; ++PI)
if (isLiveIn(r, *PI, LI)) if (isLiveIn(r, *PI, LI))
return true; return true;
}
return false; return false;
} }
@@ -691,6 +690,9 @@ void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB,
// Insert curr.second in pushed // Insert curr.second in pushed
pushed.insert(curr.second); pushed.insert(curr.second);
// Create a live interval for this temporary
InsertedPHIDests.push_back(std::make_pair(t, --PI));
} }
// Insert copy from map[curr.first] to curr.second // Insert copy from map[curr.first] to curr.second
@@ -748,34 +750,63 @@ void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB,
std::set<unsigned> RegHandled; std::set<unsigned> RegHandled;
for (SmallVector<std::pair<unsigned, MachineInstr*>, 4>::iterator I = for (SmallVector<std::pair<unsigned, MachineInstr*>, 4>::iterator I =
InsertedPHIDests.begin(), E = InsertedPHIDests.end(); I != E; ++I) InsertedPHIDests.begin(), E = InsertedPHIDests.end(); I != E; ++I)
if (RegHandled.insert(I->first).second) if (RegHandled.insert(I->first).second &&
!LI.getOrCreateInterval(I->first).liveAt(
LI.getMBBEndIdx(I->second->getParent())))
LI.addLiveRangeToEndOfBlock(I->first, I->second); LI.addLiveRangeToEndOfBlock(I->first, I->second);
} }
/// InsertCopies - insert copies into MBB and all of its successors /// InsertCopies - insert copies into MBB and all of its successors
void StrongPHIElimination::InsertCopies(MachineBasicBlock* MBB, void StrongPHIElimination::InsertCopies(MachineDomTreeNode* MDTN,
SmallPtrSet<MachineBasicBlock*, 16>& visited) { SmallPtrSet<MachineBasicBlock*, 16>& visited) {
MachineBasicBlock* MBB = MDTN->getBlock();
visited.insert(MBB); visited.insert(MBB);
std::set<unsigned> pushed; std::set<unsigned> pushed;
LiveIntervals& LI = getAnalysis<LiveIntervals>();
// Rewrite register uses from Stacks // Rewrite register uses from Stacks
for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
I != E; ++I) I != E; ++I) {
if (I->getOpcode() == TargetInstrInfo::PHI)
continue;
for (unsigned i = 0; i < I->getNumOperands(); ++i) for (unsigned i = 0; i < I->getNumOperands(); ++i)
if (I->getOperand(i).isRegister() && if (I->getOperand(i).isRegister() &&
Stacks[I->getOperand(i).getReg()].size()) { Stacks[I->getOperand(i).getReg()].size()) {
// Remove the live range for the old vreg.
LiveInterval& OldInt = LI.getInterval(I->getOperand(i).getReg());
LiveInterval::iterator OldLR = OldInt.FindLiveRangeContaining(
LiveIntervals::getUseIndex(LI.getInstructionIndex(I)));
if (OldLR != OldInt.end())
OldInt.removeRange(*OldLR, true);
// Change the register
I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back()); I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back());
// Add a live range for the new vreg
LiveInterval& Int = LI.getInterval(I->getOperand(i).getReg());
VNInfo* FirstVN = *Int.vni_begin();
FirstVN->hasPHIKill = false;
if (I->getOperand(i).isKill())
FirstVN->kills.push_back(
LiveIntervals::getUseIndex(LI.getInstructionIndex(I)));
LiveRange LR (LI.getMBBStartIdx(I->getParent()),
LiveIntervals::getUseIndex(LI.getInstructionIndex(I)),
FirstVN);
Int.addRange(LR);
} }
}
// Schedule the copies for this block // Schedule the copies for this block
ScheduleCopies(MBB, pushed); ScheduleCopies(MBB, pushed);
// Recur to our successors // Recur down the dominator tree.
for (GraphTraits<MachineBasicBlock*>::ChildIteratorType I = for (MachineDomTreeNode::iterator I = MDTN->begin(),
GraphTraits<MachineBasicBlock*>::child_begin(MBB), E = E = MDTN->end(); I != E; ++I)
GraphTraits<MachineBasicBlock*>::child_end(MBB); I != E; ++I) if (!visited.count((*I)->getBlock()))
if (!visited.count(*I))
InsertCopies(*I, visited); InsertCopies(*I, visited);
// As we exit this block, pop the names we pushed while processing it // As we exit this block, pop the names we pushed while processing it
@@ -884,7 +915,8 @@ bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
// Insert copies // Insert copies
// FIXME: This process should probably preserve LiveIntervals // FIXME: This process should probably preserve LiveIntervals
SmallPtrSet<MachineBasicBlock*, 16> visited; SmallPtrSet<MachineBasicBlock*, 16> visited;
InsertCopies(Fn.begin(), visited); MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
InsertCopies(MDT.getRootNode(), visited);
// Perform renaming // Perform renaming
for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end(); for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end();
@@ -901,8 +933,6 @@ bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) {
RenameSets[SI->first].end()); RenameSets[SI->first].end());
RenameSets.erase(SI->first); RenameSets.erase(SI->first);
} }
} }
I->second.erase(SI->first); I->second.erase(SI->first);