Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 1 | //===- StrongPhiElimination.cpp - Eliminate PHI nodes by inserting copies -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #define DEBUG_TYPE "strongphielim" |
Cameron Zwarich | 47bce43 | 2010-12-21 06:54:43 +0000 | [diff] [blame] | 14 | #include "PHIEliminationUtils.h" |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/Passes.h" |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineDominators.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Cameron Zwarich | 47bce43 | 2010-12-21 06:54:43 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 21 | #include "llvm/Target/TargetInstrInfo.h" |
| 22 | #include "llvm/Support/Debug.h" |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 25 | namespace { |
Cameron Zwarich | 9eaf49b | 2010-12-05 22:34:08 +0000 | [diff] [blame] | 26 | class StrongPHIElimination : public MachineFunctionPass { |
| 27 | public: |
| 28 | static char ID; // Pass identification, replacement for typeid |
| 29 | StrongPHIElimination() : MachineFunctionPass(ID) { |
| 30 | initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); |
| 31 | } |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 32 | |
Cameron Zwarich | 9eaf49b | 2010-12-05 22:34:08 +0000 | [diff] [blame] | 33 | virtual void getAnalysisUsage(AnalysisUsage&) const; |
| 34 | bool runOnMachineFunction(MachineFunction&); |
Cameron Zwarich | 47bce43 | 2010-12-21 06:54:43 +0000 | [diff] [blame] | 35 | |
| 36 | private: |
| 37 | void InsertCopiesForPHI(MachineInstr*, MachineBasicBlock*); |
| 38 | |
| 39 | MachineRegisterInfo* MRI; |
| 40 | const TargetInstrInfo* TII; |
| 41 | LiveIntervals* LI; |
| 42 | |
| 43 | typedef DenseSet<std::pair<MachineBasicBlock*, unsigned> > CopySet; |
| 44 | CopySet InsertedCopies; |
Cameron Zwarich | 9eaf49b | 2010-12-05 22:34:08 +0000 | [diff] [blame] | 45 | }; |
Jakob Stoklund Olesen | 68be956 | 2010-12-03 19:21:53 +0000 | [diff] [blame] | 46 | } // namespace |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 47 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 48 | char StrongPHIElimination::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 49 | INITIALIZE_PASS_BEGIN(StrongPHIElimination, "strong-phi-node-elimination", |
| 50 | "Eliminate PHI nodes for register allocation, intelligently", false, false) |
| 51 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 52 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 53 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 54 | INITIALIZE_PASS_END(StrongPHIElimination, "strong-phi-node-elimination", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 55 | "Eliminate PHI nodes for register allocation, intelligently", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 56 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 57 | char &llvm::StrongPHIEliminationID = StrongPHIElimination::ID; |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 58 | |
Cameron Zwarich | 9eaf49b | 2010-12-05 22:34:08 +0000 | [diff] [blame] | 59 | void StrongPHIElimination::getAnalysisUsage(AnalysisUsage& AU) const { |
| 60 | AU.setPreservesCFG(); |
| 61 | AU.addRequired<MachineDominatorTree>(); |
| 62 | AU.addRequired<SlotIndexes>(); |
| 63 | AU.addPreserved<SlotIndexes>(); |
| 64 | AU.addRequired<LiveIntervals>(); |
| 65 | AU.addPreserved<LiveIntervals>(); |
| 66 | MachineFunctionPass::getAnalysisUsage(AU); |
| 67 | } |
| 68 | |
Cameron Zwarich | 47bce43 | 2010-12-21 06:54:43 +0000 | [diff] [blame] | 69 | static MachineOperand* findLastUse(MachineBasicBlock* MBB, unsigned Reg) { |
| 70 | // FIXME: This only needs to check from the first terminator, as only the |
| 71 | // first terminator can use a virtual register. |
| 72 | for (MachineBasicBlock::reverse_iterator RI = MBB->rbegin(); ; ++RI) { |
| 73 | assert (RI != MBB->rend()); |
| 74 | MachineInstr* MI = &*RI; |
| 75 | |
| 76 | for (MachineInstr::mop_iterator OI = MI->operands_begin(), |
| 77 | OE = MI->operands_end(); OI != OE; ++OI) { |
| 78 | MachineOperand& MO = *OI; |
| 79 | if (MO.isReg() && MO.isUse() && MO.getReg() == Reg) |
| 80 | return &MO; |
| 81 | } |
| 82 | } |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | bool StrongPHIElimination::runOnMachineFunction(MachineFunction& MF) { |
| 87 | MRI = &MF.getRegInfo(); |
| 88 | TII = MF.getTarget().getInstrInfo(); |
| 89 | LI = &getAnalysis<LiveIntervals>(); |
| 90 | |
| 91 | // Insert copies for all PHI source and destination registers. |
| 92 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); |
| 93 | I != E; ++I) { |
| 94 | for (MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end(); |
| 95 | BBI != BBE && BBI->isPHI(); ++BBI) { |
| 96 | InsertCopiesForPHI(BBI, I); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Adjust the live intervals of all PHI source registers to handle the case |
| 101 | // where the PHIs in successor blocks were the only later uses of the source |
| 102 | // register. |
| 103 | for (CopySet::iterator I = InsertedCopies.begin(), E = InsertedCopies.end(); |
| 104 | I != E; ++I) { |
| 105 | MachineBasicBlock* MBB = I->first; |
| 106 | unsigned SrcReg = I->second; |
| 107 | LiveInterval& SrcLI = LI->getInterval(SrcReg); |
| 108 | |
| 109 | bool isLiveOut = false; |
| 110 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 111 | SE = MBB->succ_end(); SI != SE; ++SI) { |
| 112 | if (SrcLI.liveAt(LI->getMBBStartIdx(*SI))) { |
| 113 | isLiveOut = true; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (isLiveOut) |
| 119 | continue; |
| 120 | |
| 121 | MachineOperand* LastUse = findLastUse(MBB, SrcReg); |
| 122 | assert(LastUse); |
| 123 | SrcLI.removeRange(LI->getInstructionIndex(LastUse->getParent()).getDefIndex(), |
| 124 | LI->getMBBEndIdx(MBB)); |
| 125 | LastUse->setIsKill(true); |
| 126 | } |
| 127 | |
| 128 | // Remove all PHI instructions from the function. |
| 129 | bool Changed = false; |
| 130 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); |
| 131 | I != E; ++I) { |
| 132 | MachineBasicBlock::iterator BBI = I->begin(), BBE = I->end(); |
| 133 | while (BBI != BBE && BBI->isPHI()) { |
| 134 | MachineInstr* PHI = BBI; |
| 135 | ++BBI; |
| 136 | LI->RemoveMachineInstrFromMaps(PHI); |
| 137 | PHI->eraseFromParent(); |
| 138 | Changed = true; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | LI->renumber(); |
| 143 | MF.verify(this); |
| 144 | |
| 145 | InsertedCopies.clear(); |
| 146 | |
| 147 | return Changed; |
| 148 | } |
| 149 | |
| 150 | void StrongPHIElimination::InsertCopiesForPHI(MachineInstr* PHI, |
| 151 | MachineBasicBlock* MBB) { |
| 152 | assert(PHI->isPHI()); |
| 153 | unsigned DestReg = PHI->getOperand(0).getReg(); |
| 154 | |
| 155 | const TargetRegisterClass* RC = MRI->getRegClass(DestReg); |
| 156 | unsigned CopyReg = MRI->createVirtualRegister(RC); |
| 157 | |
| 158 | MachineInstr* CopyInstr = BuildMI(*MBB, |
| 159 | MBB->SkipPHIsAndLabels(MBB->begin()), |
| 160 | PHI->getDebugLoc(), |
| 161 | TII->get(TargetOpcode::COPY), |
| 162 | DestReg).addReg(CopyReg); |
| 163 | LI->InsertMachineInstrInMaps(CopyInstr); |
| 164 | CopyInstr->getOperand(1).setIsKill(true); |
| 165 | |
| 166 | // Add the region from the beginning of MBB to the copy instruction to |
| 167 | // CopyReg's live interval, and give the VNInfo the phidef flag. |
| 168 | LiveInterval& CopyLI = LI->getOrCreateInterval(CopyReg); |
| 169 | SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB); |
| 170 | SlotIndex DestCopyIndex = LI->getInstructionIndex(CopyInstr); |
| 171 | VNInfo* CopyVNI = CopyLI.getNextValue(MBBStartIndex, |
| 172 | CopyInstr, |
| 173 | LI->getVNInfoAllocator()); |
| 174 | CopyVNI->setIsPHIDef(true); |
| 175 | CopyLI.addRange(LiveRange(MBBStartIndex, |
| 176 | DestCopyIndex.getDefIndex(), |
| 177 | CopyVNI)); |
| 178 | |
| 179 | // Adjust DestReg's live interval to adjust for its new definition at |
| 180 | // CopyInstr. |
| 181 | LiveInterval& DestLI = LI->getOrCreateInterval(DestReg); |
| 182 | SlotIndex PHIIndex = LI->getInstructionIndex(PHI); |
| 183 | DestLI.removeRange(PHIIndex.getDefIndex(), DestCopyIndex.getDefIndex()); |
| 184 | |
| 185 | VNInfo* DestVNI = DestLI.getVNInfoAt(DestCopyIndex.getDefIndex()); |
| 186 | assert(DestVNI); |
| 187 | DestVNI->def = DestCopyIndex.getDefIndex(); |
| 188 | |
| 189 | SmallPtrSet<MachineBasicBlock*, 8> MBBsInsertedInto; |
| 190 | for (unsigned i = 1; i < PHI->getNumOperands(); i += 2) { |
| 191 | MachineOperand& SrcMO = PHI->getOperand(i); |
Cameron Zwarich | ef485d8 | 2010-12-24 03:09:36 +0000 | [diff] [blame] | 192 | |
| 193 | // If a source is defined by an implicit def, there is no need to insert a |
| 194 | // copy in the predecessor. |
| 195 | if (SrcMO.isUndef()) |
| 196 | continue; |
| 197 | |
Cameron Zwarich | 47bce43 | 2010-12-21 06:54:43 +0000 | [diff] [blame] | 198 | unsigned SrcReg = SrcMO.getReg(); |
| 199 | unsigned SrcSubReg = SrcMO.getSubReg(); |
| 200 | |
| 201 | assert(TargetRegisterInfo::isVirtualRegister(SrcReg) && |
| 202 | "Machine PHI Operands must all be virtual registers!"); |
| 203 | |
Cameron Zwarich | 47bce43 | 2010-12-21 06:54:43 +0000 | [diff] [blame] | 204 | MachineBasicBlock* PredBB = PHI->getOperand(i + 1).getMBB(); |
| 205 | |
| 206 | // A copy may have already been inserted in the predecessor in the case of a |
| 207 | // block with multiple incoming edges. |
| 208 | if (!MBBsInsertedInto.insert(PredBB)) |
| 209 | continue; |
| 210 | |
| 211 | MachineBasicBlock::iterator |
| 212 | CopyInsertPoint = findPHICopyInsertPoint(PredBB, MBB, SrcReg); |
| 213 | MachineInstr* CopyInstr = BuildMI(*PredBB, |
| 214 | CopyInsertPoint, |
| 215 | PHI->getDebugLoc(), |
| 216 | TII->get(TargetOpcode::COPY), |
| 217 | CopyReg).addReg(SrcReg, 0, SrcSubReg); |
| 218 | LI->InsertMachineInstrInMaps(CopyInstr); |
| 219 | |
| 220 | // addLiveRangeToEndOfBlock() also adds the phikill flag to the VNInfo for |
| 221 | // the newly added range. |
| 222 | LI->addLiveRangeToEndOfBlock(CopyReg, CopyInstr); |
| 223 | InsertedCopies.insert(std::make_pair(PredBB, SrcReg)); |
| 224 | |
| 225 | // If SrcReg is not live beyond the PHI, trim its interval so that it is no |
| 226 | // longer live-in to MBB. Note that SrcReg may appear in other PHIs that are |
| 227 | // processed later, but this is still correct to do at this point because we |
| 228 | // never rely on LiveIntervals being correct while inserting copies. |
| 229 | // FIXME: Should this just count uses at PHIs like the normal PHIElimination |
| 230 | // pass does? |
| 231 | LiveInterval& SrcLI = LI->getInterval(SrcReg); |
| 232 | SlotIndex MBBStartIndex = LI->getMBBStartIdx(MBB); |
| 233 | SlotIndex PHIIndex = LI->getInstructionIndex(PHI); |
| 234 | SlotIndex NextInstrIndex = PHIIndex.getNextIndex(); |
| 235 | if (SrcLI.liveAt(MBBStartIndex) && SrcLI.expiredAt(NextInstrIndex)) |
| 236 | SrcLI.removeRange(MBBStartIndex, PHIIndex, true); |
| 237 | } |
Cameron Zwarich | 9eaf49b | 2010-12-05 22:34:08 +0000 | [diff] [blame] | 238 | } |