Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 1 | //===-- PhiElimination.cpp - Eliminate PHI nodes by inserting copies ------===// |
| 2 | // |
| 3 | // This pass eliminates machine instruction PHI nodes by inserting copy |
| 4 | // instructions. This destroys SSA information, but is the desired input for |
| 5 | // some register allocators. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 10 | #include "llvm/CodeGen/MachineInstr.h" |
| 11 | #include "llvm/CodeGen/SSARegMap.h" |
| 12 | #include "llvm/CodeGen/LiveVariables.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 13 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 14 | #include "llvm/Target/TargetMachine.h" |
| 15 | |
| 16 | namespace { |
| 17 | struct PNE : public MachineFunctionPass { |
| 18 | bool runOnMachineFunction(MachineFunction &Fn) { |
| 19 | bool Changed = false; |
| 20 | |
| 21 | // Eliminate PHI instructions by inserting copies into predecessor blocks. |
| 22 | // |
| 23 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 24 | Changed |= EliminatePHINodes(Fn, *I); |
| 25 | |
| 26 | //std::cerr << "AFTER PHI NODE ELIM:\n"; |
| 27 | //Fn.dump(); |
| 28 | return Changed; |
| 29 | } |
| 30 | |
| 31 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 32 | AU.addPreserved<LiveVariables>(); |
| 33 | MachineFunctionPass::getAnalysisUsage(AU); |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions |
| 38 | /// in predecessor basic blocks. |
| 39 | /// |
| 40 | bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB); |
| 41 | }; |
| 42 | |
| 43 | RegisterPass<PNE> X("phi-node-elimination", |
| 44 | "Eliminate PHI nodes for register allocation"); |
| 45 | } |
| 46 | |
| 47 | const PassInfo *PHIEliminationID = X.getPassInfo(); |
| 48 | |
| 49 | /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in |
| 50 | /// predecessor basic blocks. |
| 51 | /// |
| 52 | bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) { |
Chris Lattner | 0416d2a | 2003-01-16 18:06:43 +0000 | [diff] [blame] | 53 | if (MBB.empty() || MBB.front()->getOpcode() != TargetInstrInfo::PHI) |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 54 | return false; // Quick exit for normal case... |
| 55 | |
| 56 | LiveVariables *LV = getAnalysisToUpdate<LiveVariables>(); |
| 57 | const TargetInstrInfo &MII = MF.getTarget().getInstrInfo(); |
| 58 | const MRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); |
| 59 | |
| 60 | while (MBB.front()->getOpcode() == TargetInstrInfo::PHI) { |
| 61 | MachineInstr *MI = MBB.front(); |
| 62 | // Unlink the PHI node from the basic block... but don't delete the PHI yet |
| 63 | MBB.erase(MBB.begin()); |
| 64 | |
| 65 | assert(MI->getOperand(0).isVirtualRegister() && |
| 66 | "PHI node doesn't write virt reg?"); |
| 67 | |
| 68 | unsigned DestReg = MI->getOperand(0).getAllocatedRegNum(); |
| 69 | |
| 70 | // Create a new register for the incoming PHI arguments |
| 71 | const TargetRegisterClass *RC = MF.getSSARegMap()->getRegClass(DestReg); |
| 72 | unsigned IncomingReg = MF.getSSARegMap()->createVirtualRegister(RC); |
| 73 | |
Chris Lattner | 927ce5d | 2003-05-12 03:55:21 +0000 | [diff] [blame] | 74 | // Insert a register to register copy in the top of the current block (but |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 75 | // after any remaining phi nodes) which copies the new incoming register |
| 76 | // into the phi node destination. |
| 77 | // |
| 78 | MachineBasicBlock::iterator AfterPHIsIt = MBB.begin(); |
Chris Lattner | a13f0d3 | 2003-05-12 14:26:38 +0000 | [diff] [blame^] | 79 | while (AfterPHIsIt != MBB.end() && |
| 80 | (*AfterPHIsIt)->getOpcode() == TargetInstrInfo::PHI) |
| 81 | ++AfterPHIsIt; // Skip over all of the PHI nodes... |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 82 | RegInfo->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC); |
Chris Lattner | 927ce5d | 2003-05-12 03:55:21 +0000 | [diff] [blame] | 83 | |
| 84 | // Update live variable information if there is any... |
| 85 | if (LV) { |
| 86 | MachineInstr *PHICopy = *(AfterPHIsIt-1); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 927ce5d | 2003-05-12 03:55:21 +0000 | [diff] [blame] | 88 | // Add information to LiveVariables to know that the incoming value is |
| 89 | // dead. This says that the register is dead, not killed, because we |
| 90 | // cannot use the live variable information to indicate that the variable |
| 91 | // is defined in multiple entry blocks. Instead, we pretend that this |
| 92 | // instruction defined it and killed it at the same time. |
| 93 | // |
| 94 | LV->addVirtualRegisterDead(IncomingReg, PHICopy); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 927ce5d | 2003-05-12 03:55:21 +0000 | [diff] [blame] | 96 | // Since we are going to be deleting the PHI node, if it is the last use |
| 97 | // of any registers, or if the value itself is dead, we need to move this |
| 98 | // information over to the new copy we just inserted... |
| 99 | // |
| 100 | std::pair<LiveVariables::killed_iterator, LiveVariables::killed_iterator> |
| 101 | RKs = LV->killed_range(MI); |
| 102 | if (RKs.first != RKs.second) { |
| 103 | for (LiveVariables::killed_iterator I = RKs.first; I != RKs.second; ++I) |
| 104 | LV->addVirtualRegisterKilled(I->second, PHICopy); |
| 105 | LV->removeVirtualRegistersKilled(RKs.first, RKs.second); |
| 106 | } |
| 107 | |
| 108 | RKs = LV->dead_range(MI); |
| 109 | if (RKs.first != RKs.second) { |
| 110 | for (LiveVariables::killed_iterator I = RKs.first; I != RKs.second; ++I) |
| 111 | LV->addVirtualRegisterDead(I->second, PHICopy); |
| 112 | LV->removeVirtualRegistersDead(RKs.first, RKs.second); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Now loop over all of the incoming arguments, changing them to copy into |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 117 | // the IncomingReg register in the corresponding predecessor basic block. |
| 118 | // |
| 119 | for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) { |
| 120 | MachineOperand &opVal = MI->getOperand(i-1); |
| 121 | |
| 122 | // Get the MachineBasicBlock equivalent of the BasicBlock that is the |
Chris Lattner | 927ce5d | 2003-05-12 03:55:21 +0000 | [diff] [blame] | 123 | // source path the PHI. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 124 | MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock(); |
| 125 | |
Chris Lattner | 98719d7 | 2003-05-12 04:08:54 +0000 | [diff] [blame] | 126 | // Figure out where to insert the copy, which is at the end of the |
| 127 | // predecessor basic block, but before any terminator/branch |
| 128 | // instructions... |
| 129 | MachineBasicBlock::iterator I = opBlock.end(); |
| 130 | if (I != opBlock.begin()) { // Handle empty blocks |
| 131 | --I; |
| 132 | // must backtrack over ALL the branches in the previous block |
| 133 | while (MII.isTerminatorInstr((*I)->getOpcode()) && |
| 134 | I != opBlock.begin()) |
| 135 | --I; |
| 136 | |
| 137 | // move back to the first branch instruction so new instructions |
| 138 | // are inserted right in front of it and not in front of a non-branch |
| 139 | if (!MII.isTerminatorInstr((*I)->getOpcode())) |
| 140 | ++I; |
| 141 | } |
| 142 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 143 | // Check to make sure we haven't already emitted the copy for this block. |
| 144 | // This can happen because PHI nodes may have multiple entries for the |
| 145 | // same basic block. It doesn't matter which entry we use though, because |
| 146 | // all incoming values are guaranteed to be the same for a particular bb. |
| 147 | // |
Chris Lattner | 98719d7 | 2003-05-12 04:08:54 +0000 | [diff] [blame] | 148 | // If we emitted a copy for this basic block already, it will be right |
| 149 | // where we want to insert one now. Just check for a definition of the |
| 150 | // register we are interested in! |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 151 | // |
| 152 | bool HaveNotEmitted = true; |
Chris Lattner | 98719d7 | 2003-05-12 04:08:54 +0000 | [diff] [blame] | 153 | |
| 154 | if (I != opBlock.begin()) { |
| 155 | MachineInstr *PrevInst = *(I-1); |
| 156 | for (unsigned i = 0, e = PrevInst->getNumOperands(); i != e; ++i) { |
| 157 | MachineOperand &MO = PrevInst->getOperand(i); |
| 158 | if (MO.isVirtualRegister() && MO.getReg() == IncomingReg) |
| 159 | if (MO.opIsDef() || MO.opIsDefAndUse()) { |
| 160 | HaveNotEmitted = false; |
| 161 | break; |
| 162 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 163 | } |
Chris Lattner | 98719d7 | 2003-05-12 04:08:54 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 165 | |
| 166 | if (HaveNotEmitted) { |
Chris Lattner | 98719d7 | 2003-05-12 04:08:54 +0000 | [diff] [blame] | 167 | assert(opVal.isVirtualRegister() && |
| 168 | "Machine PHI Operands must all be virtual registers!"); |
| 169 | RegInfo->copyRegToReg(opBlock, I, IncomingReg, opVal.getReg(), RC); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | // really delete the PHI instruction now! |
| 174 | delete MI; |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | } |