From fae86eddeb6453e5d27a19ba2a07a7bb4505fc4f Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Tue, 23 Sep 2008 04:37:10 +0000 Subject: Add initial support for inserting last minute copies. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@56485 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StrongPHIElimination.cpp | 81 +++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/lib/CodeGen/StrongPHIElimination.cpp b/lib/CodeGen/StrongPHIElimination.cpp index 7c30e26..8bda41d 100644 --- a/lib/CodeGen/StrongPHIElimination.cpp +++ b/lib/CodeGen/StrongPHIElimination.cpp @@ -142,7 +142,7 @@ namespace { void ScheduleCopies(MachineBasicBlock* MBB, std::set& pushed); void InsertCopies(MachineDomTreeNode* MBB, SmallPtrSet& v); - void mergeLiveIntervals(unsigned primary, unsigned secondary); + bool mergeLiveIntervals(unsigned primary, unsigned secondary); }; } @@ -844,7 +844,7 @@ void StrongPHIElimination::InsertCopies(MachineDomTreeNode* MDTN, Stacks[*I].pop_back(); } -void StrongPHIElimination::mergeLiveIntervals(unsigned primary, +bool StrongPHIElimination::mergeLiveIntervals(unsigned primary, unsigned secondary) { LiveIntervals& LI = getAnalysis(); @@ -859,32 +859,35 @@ void StrongPHIElimination::mergeLiveIntervals(unsigned primary, unsigned Start = R.start; unsigned End = R.end; - if (LHS.liveAt(Start)) - LHS.removeRange(Start, End, true); + if (LHS.getLiveRangeContaining(Start)) + return false; - if (const LiveRange* ER = LHS.getLiveRangeContaining(End)) - End = ER->start; + if (LHS.getLiveRangeContaining(End)) + return false; LiveInterval::iterator RI = std::upper_bound(LHS.begin(), LHS.end(), R); if (RI != LHS.end() && RI->start < End) - End = RI->start; - - if (Start != End) { - VNInfo* OldVN = R.valno; - VNInfo*& NewVN = VNMap[OldVN]; - if (!NewVN) { - NewVN = LHS.getNextValue(OldVN->def, - OldVN->copy, - LI.getVNInfoAllocator()); - NewVN->kills = OldVN->kills; - } - - LiveRange LR (Start, End, NewVN); - LHS.addRange(LR); + return false; + } + + for (LiveInterval::iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { + LiveRange R = *I; + VNInfo* OldVN = R.valno; + VNInfo*& NewVN = VNMap[OldVN]; + if (!NewVN) { + NewVN = LHS.getNextValue(OldVN->def, + OldVN->copy, + LI.getVNInfoAllocator()); + NewVN->kills = OldVN->kills; } + + LiveRange LR (R.start, R.end, NewVN); + LHS.addRange(LR); } LI.removeInterval(RHS.reg); + + return true; } bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) { @@ -936,21 +939,43 @@ bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) { DOUT << "Renaming: " << SI->first << " -> " << I->first << "\n"; if (SI->first != I->first) { - mergeLiveIntervals(I->first, SI->first); - Fn.getRegInfo().replaceRegWith(SI->first, I->first); + if (mergeLiveIntervals(I->first, SI->first)) { + Fn.getRegInfo().replaceRegWith(SI->first, I->first); - if (RenameSets.count(SI->first)) { - I->second.insert(RenameSets[SI->first].begin(), - RenameSets[SI->first].end()); - RenameSets.erase(SI->first); + if (RenameSets.count(SI->first)) { + I->second.insert(RenameSets[SI->first].begin(), + RenameSets[SI->first].end()); + RenameSets.erase(SI->first); + } + } else { + // Insert a last-minute copy if a conflict was detected. + const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo(); + const TargetRegisterClass *RC = Fn.getRegInfo().getRegClass(I->first); + TII->copyRegToReg(*SI->second, SI->second->getFirstTerminator(), + I->first, SI->first, RC, RC); + + LI.computeNumbering(); + + LiveInterval& Int = LI.getOrCreateInterval(I->first); + unsigned instrIdx = + LI.getInstructionIndex(--SI->second->getFirstTerminator()); + if (Int.liveAt(LiveIntervals::getDefIndex(instrIdx))) + Int.removeRange(LiveIntervals::getDefIndex(instrIdx), + LI.getMBBEndIdx(SI->second)+1, true); + + LiveRange R = LI.addLiveRangeToEndOfBlock(I->first, + --SI->second->getFirstTerminator()); + R.valno->copy = --SI->second->getFirstTerminator(); + R.valno->def = LiveIntervals::getDefIndex(instrIdx); + + DOUT << "Renaming failed: " << SI->first << " -> " + << I->first << "\n"; } } I->second.erase(SI->first); } - // FIXME: Insert last-minute copies - // Remove PHIs std::vector phis; for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { -- cgit v1.1