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