Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 1 | //===-- PhiElimination.cpp - Eliminate PHI nodes by inserting copies ------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass eliminates machine instruction PHI nodes by inserting copy |
| 11 | // instructions. This destroys SSA information, but is the desired input for |
| 12 | // some register allocators. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Misha Brukman | d7a10c8 | 2005-05-05 23:45:17 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/LiveVariables.h" |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/Passes.h" |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 19 | #include "llvm/CodeGen/MachineInstr.h" |
| 20 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseMap.h" |
| 24 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 26 | #include <set> |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 30 | namespace { |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 31 | Statistic<> NumAtomic("phielim", "Number of atomic phis lowered"); |
| 32 | Statistic<> NumSimple("phielim", "Number of simple phis lowered"); |
| 33 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 34 | struct PNE : public MachineFunctionPass { |
| 35 | bool runOnMachineFunction(MachineFunction &Fn) { |
| 36 | bool Changed = false; |
| 37 | |
| 38 | // Eliminate PHI instructions by inserting copies into predecessor blocks. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 39 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 40 | Changed |= EliminatePHINodes(Fn, *I); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 41 | |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 42 | return Changed; |
| 43 | } |
| 44 | |
| 45 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 46 | AU.addPreserved<LiveVariables>(); |
| 47 | MachineFunctionPass::getAnalysisUsage(AU); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions |
| 52 | /// in predecessor basic blocks. |
| 53 | /// |
| 54 | bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB); |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 55 | void LowerAtomicPHINode(MachineBasicBlock &MBB, |
| 56 | MachineBasicBlock::iterator AfterPHIsIt, |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 57 | DenseMap<unsigned, VirtReg2IndexFunctor> &VUC); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | RegisterPass<PNE> X("phi-node-elimination", |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 61 | "Eliminate PHI nodes for register allocation"); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 65 | const PassInfo *llvm::PHIEliminationID = X.getPassInfo(); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 66 | |
| 67 | /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in |
| 68 | /// predecessor basic blocks. |
| 69 | /// |
| 70 | bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 71 | if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI) |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 72 | return false; // Quick exit for basic blocks without PHIs. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 80e20eb | 2004-05-10 19:06:37 +0000 | [diff] [blame] | 74 | // VRegPHIUseCount - Keep track of the number of times each virtual register |
Chris Lattner | bee8872 | 2004-05-12 21:47:57 +0000 | [diff] [blame] | 75 | // is used by PHI nodes in successors of this block. |
Chris Lattner | 8abf693 | 2004-05-10 19:17:36 +0000 | [diff] [blame] | 76 | DenseMap<unsigned, VirtReg2IndexFunctor> VRegPHIUseCount; |
| 77 | VRegPHIUseCount.grow(MF.getSSARegMap()->getLastVirtReg()); |
Chris Lattner | 80e20eb | 2004-05-10 19:06:37 +0000 | [diff] [blame] | 78 | |
Chris Lattner | bee8872 | 2004-05-12 21:47:57 +0000 | [diff] [blame] | 79 | for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin(), |
| 80 | E = MBB.pred_end(); PI != E; ++PI) |
| 81 | for (MachineBasicBlock::succ_iterator SI = (*PI)->succ_begin(), |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 82 | E = (*PI)->succ_end(); SI != E; ++SI) |
| 83 | for (MachineBasicBlock::iterator BBI = (*SI)->begin(), E = (*SI)->end(); |
| 84 | BBI != E && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) |
| 85 | for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) |
| 86 | VRegPHIUseCount[BBI->getOperand(i).getReg()]++; |
| 87 | |
Chris Lattner | 791f896 | 2004-05-10 18:47:18 +0000 | [diff] [blame] | 88 | // Get an iterator to the first instruction after the last PHI node (this may |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 89 | // also be the end of the basic block). |
Chris Lattner | 791f896 | 2004-05-10 18:47:18 +0000 | [diff] [blame] | 90 | MachineBasicBlock::iterator AfterPHIsIt = MBB.begin(); |
| 91 | while (AfterPHIsIt != MBB.end() && |
Chris Lattner | bee8872 | 2004-05-12 21:47:57 +0000 | [diff] [blame] | 92 | AfterPHIsIt->getOpcode() == TargetInstrInfo::PHI) |
Chris Lattner | 791f896 | 2004-05-10 18:47:18 +0000 | [diff] [blame] | 93 | ++AfterPHIsIt; // Skip over all of the PHI nodes... |
| 94 | |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 95 | while (MBB.front().getOpcode() == TargetInstrInfo::PHI) { |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 96 | LowerAtomicPHINode(MBB, AfterPHIsIt, VRegPHIUseCount); |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 97 | } |
| 98 | return true; |
| 99 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 2adfa7e | 2006-01-04 07:12:21 +0000 | [diff] [blame^] | 101 | /// InstructionUsesRegister - Return true if the specified machine instr has a |
| 102 | /// use of the specified register. |
| 103 | static bool InstructionUsesRegister(MachineInstr *MI, unsigned SrcReg) { |
| 104 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) |
| 105 | if (MI->getOperand(0).isRegister() && |
| 106 | MI->getOperand(0).getReg() == SrcReg && |
| 107 | MI->getOperand(0).isUse()) |
| 108 | return true; |
| 109 | return false; |
| 110 | } |
| 111 | |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 112 | /// LowerAtomicPHINode - Lower the PHI node at the top of the specified block, |
| 113 | /// under the assuption that it needs to be lowered in a way that supports |
| 114 | /// atomic execution of PHIs. This lowering method is always correct all of the |
| 115 | /// time. |
| 116 | void PNE::LowerAtomicPHINode(MachineBasicBlock &MBB, |
| 117 | MachineBasicBlock::iterator AfterPHIsIt, |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 118 | DenseMap<unsigned, VirtReg2IndexFunctor> &VRegPHIUseCount) { |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 119 | // Unlink the PHI node from the basic block, but don't delete the PHI yet. |
| 120 | MachineInstr *MPhi = MBB.remove(MBB.begin()); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 122 | unsigned DestReg = MPhi->getOperand(0).getReg(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 124 | // Create a new register for the incoming PHI arguments/ |
| 125 | MachineFunction &MF = *MBB.getParent(); |
| 126 | const TargetRegisterClass *RC = MF.getSSARegMap()->getRegClass(DestReg); |
| 127 | unsigned IncomingReg = MF.getSSARegMap()->createVirtualRegister(RC); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 129 | // Insert a register to register copy in the top of the current block (but |
| 130 | // after any remaining phi nodes) which copies the new incoming register |
| 131 | // into the phi node destination. |
| 132 | // |
| 133 | const MRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); |
| 134 | RegInfo->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC); |
| 135 | |
| 136 | // Update live variable information if there is any... |
| 137 | LiveVariables *LV = getAnalysisToUpdate<LiveVariables>(); |
| 138 | if (LV) { |
| 139 | MachineInstr *PHICopy = prior(AfterPHIsIt); |
| 140 | |
| 141 | // Add information to LiveVariables to know that the incoming value is |
| 142 | // killed. Note that because the value is defined in several places (once |
| 143 | // each for each incoming block), the "def" block and instruction fields |
| 144 | // for the VarInfo is not filled in. |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 145 | // |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 146 | LV->addVirtualRegisterKilled(IncomingReg, PHICopy); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 148 | // Since we are going to be deleting the PHI node, if it is the last use |
| 149 | // of any registers, or if the value itself is dead, we need to move this |
| 150 | // information over to the new copy we just inserted. |
| 151 | // |
| 152 | LV->removeVirtualRegistersKilled(MPhi); |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 154 | // If the result is dead, update LV. |
| 155 | if (LV->RegisterDefIsDead(MPhi, DestReg)) { |
| 156 | LV->addVirtualRegisterDead(DestReg, PHICopy); |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 157 | LV->removeVirtualRegistersDead(MPhi); |
| 158 | } |
Chris Lattner | 172c362 | 2006-01-04 06:47:48 +0000 | [diff] [blame] | 159 | |
| 160 | // Realize that the destination register is defined by the PHI copy now, not |
| 161 | // the PHI itself. |
| 162 | LV->getVarInfo(DestReg).DefInst = PHICopy; |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 163 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 165 | // Adjust the VRegPHIUseCount map to account for the removal of this PHI |
| 166 | // node. |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 167 | unsigned NumPreds = (MPhi->getNumOperands()-1)/2; |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 168 | for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2) |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 169 | VRegPHIUseCount[MPhi->getOperand(i).getReg()] -= NumPreds; |
Chris Lattner | 572c770 | 2003-05-12 14:28:28 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 171 | // Now loop over all of the incoming arguments, changing them to copy into |
| 172 | // the IncomingReg register in the corresponding predecessor basic block. |
| 173 | // |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 174 | std::set<MachineBasicBlock*> MBBsInsertedInto; |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 175 | for (int i = MPhi->getNumOperands() - 1; i >= 2; i-=2) { |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 176 | unsigned SrcReg = MPhi->getOperand(i-1).getReg(); |
| 177 | assert(MRegisterInfo::isVirtualRegister(SrcReg) && |
| 178 | "Machine PHI Operands must all be virtual registers!"); |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 179 | |
| 180 | // Get the MachineBasicBlock equivalent of the BasicBlock that is the |
| 181 | // source path the PHI. |
| 182 | MachineBasicBlock &opBlock = *MPhi->getOperand(i).getMachineBasicBlock(); |
| 183 | |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 184 | // Check to make sure we haven't already emitted the copy for this block. |
| 185 | // This can happen because PHI nodes may have multiple entries for the |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 186 | // same basic block. |
| 187 | if (!MBBsInsertedInto.insert(&opBlock).second) |
| 188 | continue; // If the copy has already been emitted, we're done. |
| 189 | |
| 190 | // Get an iterator pointing to the first terminator in the block (or end()). |
| 191 | // This is the point where we can insert a copy if we'd like to. |
| 192 | MachineBasicBlock::iterator I = opBlock.getFirstTerminator(); |
| 193 | |
| 194 | // Insert the copy. |
| 195 | RegInfo->copyRegToReg(opBlock, I, IncomingReg, SrcReg, RC); |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 197 | // Now update live variable information if we have it. Otherwise we're done |
| 198 | if (!LV) continue; |
| 199 | |
| 200 | // We want to be able to insert a kill of the register if this PHI |
| 201 | // (aka, the copy we just inserted) is the last use of the source |
| 202 | // value. Live variable analysis conservatively handles this by |
| 203 | // saying that the value is live until the end of the block the PHI |
| 204 | // entry lives in. If the value really is dead at the PHI copy, there |
| 205 | // will be no successor blocks which have the value live-in. |
| 206 | // |
| 207 | // Check to see if the copy is the last use, and if so, update the |
| 208 | // live variables information so that it knows the copy source |
| 209 | // instruction kills the incoming value. |
| 210 | // |
| 211 | LiveVariables::VarInfo &InRegVI = LV->getVarInfo(SrcReg); |
| 212 | |
| 213 | // Loop over all of the successors of the basic block, checking to see |
| 214 | // if the value is either live in the block, or if it is killed in the |
| 215 | // block. Also check to see if this register is in use by another PHI |
| 216 | // node which has not yet been eliminated. If so, it will be killed |
| 217 | // at an appropriate point later. |
| 218 | // |
| 219 | |
| 220 | // Is it used by any PHI instructions in this block? |
| 221 | bool ValueIsLive = VRegPHIUseCount[SrcReg] != 0; |
| 222 | |
| 223 | std::vector<MachineBasicBlock*> OpSuccBlocks; |
| 224 | |
| 225 | // Otherwise, scan successors, including the BB the PHI node lives in. |
| 226 | for (MachineBasicBlock::succ_iterator SI = opBlock.succ_begin(), |
| 227 | E = opBlock.succ_end(); SI != E && !ValueIsLive; ++SI) { |
| 228 | MachineBasicBlock *SuccMBB = *SI; |
| 229 | |
| 230 | // Is it alive in this successor? |
| 231 | unsigned SuccIdx = SuccMBB->getNumber(); |
| 232 | if (SuccIdx < InRegVI.AliveBlocks.size() && |
| 233 | InRegVI.AliveBlocks[SuccIdx]) { |
| 234 | ValueIsLive = true; |
| 235 | break; |
Chris Lattner | 927ce5d | 2003-05-12 03:55:21 +0000 | [diff] [blame] | 236 | } |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 237 | |
| 238 | OpSuccBlocks.push_back(SuccMBB); |
Chris Lattner | 927ce5d | 2003-05-12 03:55:21 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 241 | // Check to see if this value is live because there is a use in a successor |
| 242 | // that kills it. |
| 243 | if (!ValueIsLive) { |
| 244 | switch (OpSuccBlocks.size()) { |
| 245 | case 1: { |
| 246 | MachineBasicBlock *MBB = OpSuccBlocks[0]; |
| 247 | for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i) |
| 248 | if (InRegVI.Kills[i]->getParent() == MBB) { |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 249 | ValueIsLive = true; |
| 250 | break; |
| 251 | } |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 252 | break; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 253 | } |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 254 | case 2: { |
| 255 | MachineBasicBlock *MBB1 = OpSuccBlocks[0], *MBB2 = OpSuccBlocks[1]; |
| 256 | for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i) |
| 257 | if (InRegVI.Kills[i]->getParent() == MBB1 || |
| 258 | InRegVI.Kills[i]->getParent() == MBB2) { |
| 259 | ValueIsLive = true; |
| 260 | break; |
| 261 | } |
| 262 | break; |
| 263 | } |
| 264 | default: |
| 265 | std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end()); |
| 266 | for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i) |
| 267 | if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(), |
| 268 | InRegVI.Kills[i]->getParent())) { |
| 269 | ValueIsLive = true; |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // Okay, if we now know that the value is not live out of the block, |
Chris Lattner | 2adfa7e | 2006-01-04 07:12:21 +0000 | [diff] [blame^] | 276 | // we can add a kill marker in this block saying that it kills the incoming |
| 277 | // value! |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 278 | if (!ValueIsLive) { |
Chris Lattner | 2adfa7e | 2006-01-04 07:12:21 +0000 | [diff] [blame^] | 279 | // In our final twist, we have to decide which instruction kills the |
| 280 | // register. In most cases this is the copy, however, the first |
| 281 | // terminator instruction at the end of the block may also use the value. |
| 282 | // In this case, we should mark *it* as being the killing block, not the |
| 283 | // copy. |
| 284 | bool FirstTerminatorUsesValue = false; |
| 285 | if (I != opBlock.end()) { |
| 286 | FirstTerminatorUsesValue = InstructionUsesRegister(I, SrcReg); |
| 287 | |
| 288 | // Check that no other terminators use values. |
| 289 | #ifndef NDEBUG |
| 290 | for (MachineBasicBlock::iterator TI = next(I); TI != opBlock.end(); |
| 291 | ++TI) { |
| 292 | assert(!InstructionUsesRegister(TI, SrcReg) && |
| 293 | "Terminator instructions cannot use virtual registers unless" |
| 294 | "they are the first terminator in a block!"); |
| 295 | } |
| 296 | #endif |
| 297 | } |
| 298 | |
| 299 | MachineBasicBlock::iterator KillInst; |
| 300 | if (!FirstTerminatorUsesValue) |
| 301 | KillInst = prior(I); |
| 302 | else |
| 303 | KillInst = I; |
| 304 | |
| 305 | // Finally, mark it killed. |
| 306 | LV->addVirtualRegisterKilled(SrcReg, KillInst); |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 307 | |
| 308 | // This vreg no longer lives all of the way through opBlock. |
| 309 | unsigned opBlockNum = opBlock.getNumber(); |
| 310 | if (opBlockNum < InRegVI.AliveBlocks.size()) |
| 311 | InRegVI.AliveBlocks[opBlockNum] = false; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 53a79aa | 2005-10-03 04:47:08 +0000 | [diff] [blame] | 314 | |
| 315 | // Really delete the PHI instruction now! |
| 316 | delete MPhi; |
Chris Lattner | 6db0756 | 2005-10-03 07:22:07 +0000 | [diff] [blame] | 317 | ++NumAtomic; |
Chris Lattner | bc40e89 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 318 | } |