blob: e21869192d9f0f24817ddd33e8ac988eb8d1e4b1 [file] [log] [blame]
Chris Lattnerbc40e892003-01-13 20:01:16 +00001//===-- PhiElimination.cpp - Eliminate PHI nodes by inserting copies ------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbc40e892003-01-13 20:01:16 +00009//
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 Lattnercd3245a2006-12-19 22:41:21 +000016#define DEBUG_TYPE "phielim"
Misha Brukmand7a10c82005-05-05 23:45:17 +000017#include "llvm/CodeGen/LiveVariables.h"
Chris Lattner0742b592004-02-23 18:38:20 +000018#include "llvm/CodeGen/Passes.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000022#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerbc40e892003-01-13 20:01:16 +000023#include "llvm/Target/TargetMachine.h"
Evan Cheng576a2702008-04-03 16:38:20 +000024#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/ADT/STLExtras.h"
Chris Lattner6db07562005-10-03 07:22:07 +000026#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattner6db07562005-10-03 07:22:07 +000028#include <algorithm>
Evan Cheng10883172008-04-02 17:23:50 +000029#include <map>
Chris Lattner0742b592004-02-23 18:38:20 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattnercd3245a2006-12-19 22:41:21 +000032STATISTIC(NumAtomic, "Number of atomic phis lowered");
Chris Lattnercd3245a2006-12-19 22:41:21 +000033
Chris Lattnerbc40e892003-01-13 20:01:16 +000034namespace {
Evan Cheng576a2702008-04-03 16:38:20 +000035 class VISIBILITY_HIDDEN PNE : public MachineFunctionPass {
36 MachineRegisterInfo *MRI; // Machine register information
37
38 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000040 PNE() : MachineFunctionPass((intptr_t)&ID) {}
41
Evan Cheng576a2702008-04-03 16:38:20 +000042 virtual bool runOnMachineFunction(MachineFunction &Fn);
43
Chris Lattnerbc40e892003-01-13 20:01:16 +000044 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.addPreserved<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000046 AU.addPreservedID(MachineLoopInfoID);
47 AU.addPreservedID(MachineDominatorsID);
Chris Lattnerbc40e892003-01-13 20:01:16 +000048 MachineFunctionPass::getAnalysisUsage(AU);
49 }
50
51 private:
Evan Cheng576a2702008-04-03 16:38:20 +000052 /// findInsertionPoint - Find a safe location to insert a move to copy
53 /// source of a PHI instruction.
54 MachineBasicBlock::iterator
55 findInsertionPoint(MachineBasicBlock &MBB, MachineInstr *DefMI,
56 unsigned DstReg, unsigned SrcReg) const;
57
Chris Lattnerbc40e892003-01-13 20:01:16 +000058 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
59 /// in predecessor basic blocks.
60 ///
61 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
Chris Lattner53a79aa2005-10-03 04:47:08 +000062 void LowerAtomicPHINode(MachineBasicBlock &MBB,
Bill Wendlingca756d22006-09-28 07:10:24 +000063 MachineBasicBlock::iterator AfterPHIsIt);
64
65 /// analyzePHINodes - Gather information about the PHI nodes in
66 /// here. In particular, we want to map the number of uses of a virtual
67 /// register which is used in a PHI node. We map that to the BB the
68 /// vreg is coming from. This is used later to determine when the vreg
69 /// is killed in the BB.
70 ///
71 void analyzePHINodes(const MachineFunction& Fn);
72
73 typedef std::pair<const MachineBasicBlock*, unsigned> BBVRegPair;
74 typedef std::map<BBVRegPair, unsigned> VRegPHIUse;
75
76 VRegPHIUse VRegPHIUseCount;
Evan Cheng576a2702008-04-03 16:38:20 +000077
78 // Defs of PHI sources which are implicit_def.
79 SmallPtrSet<MachineInstr*, 4> ImpDefs;
Chris Lattnerbc40e892003-01-13 20:01:16 +000080 };
81
Devang Patel19974732007-05-03 01:11:54 +000082 char PNE::ID = 0;
Chris Lattnerbc40e892003-01-13 20:01:16 +000083 RegisterPass<PNE> X("phi-node-elimination",
Misha Brukmandedf2bd2005-04-22 04:01:18 +000084 "Eliminate PHI nodes for register allocation");
Chris Lattnerbc40e892003-01-13 20:01:16 +000085}
86
Chris Lattner0742b592004-02-23 18:38:20 +000087const PassInfo *llvm::PHIEliminationID = X.getPassInfo();
Chris Lattnerbc40e892003-01-13 20:01:16 +000088
Evan Cheng576a2702008-04-03 16:38:20 +000089bool PNE::runOnMachineFunction(MachineFunction &Fn) {
90 MRI = &Fn.getRegInfo();
91
92 analyzePHINodes(Fn);
93
94 bool Changed = false;
95
96 // Eliminate PHI instructions by inserting copies into predecessor blocks.
97 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
98 Changed |= EliminatePHINodes(Fn, *I);
99
100 // Remove dead IMPLICIT_DEF instructions.
101 for (SmallPtrSet<MachineInstr*,4>::iterator I = ImpDefs.begin(),
102 E = ImpDefs.end(); I != E; ++I) {
103 MachineInstr *DefMI = *I;
104 unsigned DefReg = DefMI->getOperand(0).getReg();
105 if (MRI->use_begin(DefReg) == MRI->use_end())
106 DefMI->eraseFromParent();
107 }
108
109 ImpDefs.clear();
110 VRegPHIUseCount.clear();
111 return Changed;
112}
113
114
Chris Lattnerbc40e892003-01-13 20:01:16 +0000115/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
116/// predecessor basic blocks.
117///
118bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000119 if (MBB.empty() || MBB.front().getOpcode() != TargetInstrInfo::PHI)
Chris Lattner53a79aa2005-10-03 04:47:08 +0000120 return false; // Quick exit for basic blocks without PHIs.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000121
Chris Lattner791f8962004-05-10 18:47:18 +0000122 // Get an iterator to the first instruction after the last PHI node (this may
Chris Lattner53a79aa2005-10-03 04:47:08 +0000123 // also be the end of the basic block).
Chris Lattner791f8962004-05-10 18:47:18 +0000124 MachineBasicBlock::iterator AfterPHIsIt = MBB.begin();
125 while (AfterPHIsIt != MBB.end() &&
Chris Lattnerbee88722004-05-12 21:47:57 +0000126 AfterPHIsIt->getOpcode() == TargetInstrInfo::PHI)
Chris Lattner791f8962004-05-10 18:47:18 +0000127 ++AfterPHIsIt; // Skip over all of the PHI nodes...
128
Bill Wendlingca756d22006-09-28 07:10:24 +0000129 while (MBB.front().getOpcode() == TargetInstrInfo::PHI)
130 LowerAtomicPHINode(MBB, AfterPHIsIt);
131
Chris Lattner53a79aa2005-10-03 04:47:08 +0000132 return true;
133}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000134
Evan Cheng576a2702008-04-03 16:38:20 +0000135/// findInsertionPoint - Find a safe location to insert a move to copy
136/// source of a PHI instruction.
137MachineBasicBlock::iterator
138PNE::findInsertionPoint(MachineBasicBlock &MBB, MachineInstr *DefMI,
139 unsigned DstReg, unsigned SrcReg) const {
140 if (DefMI->getOpcode() == TargetInstrInfo::PHI ||
141 DefMI->getParent() != &MBB)
142 return MBB.getFirstTerminator();
143
144 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(SrcReg),
145 E = MRI->use_end(); I != E; ++I)
146 if (I->getParent() == &MBB)
147 return MBB.getFirstTerminator();
148 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(DstReg),
149 E = MRI->use_end(); I != E; ++I)
150 if (I->getParent() == &MBB)
151 return MBB.getFirstTerminator();
152
153 MachineBasicBlock::iterator I = DefMI;
154 return ++I;
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000155}
156
Chris Lattner53a79aa2005-10-03 04:47:08 +0000157/// LowerAtomicPHINode - Lower the PHI node at the top of the specified block,
158/// under the assuption that it needs to be lowered in a way that supports
159/// atomic execution of PHIs. This lowering method is always correct all of the
160/// time.
161void PNE::LowerAtomicPHINode(MachineBasicBlock &MBB,
Bill Wendlingca756d22006-09-28 07:10:24 +0000162 MachineBasicBlock::iterator AfterPHIsIt) {
Chris Lattner53a79aa2005-10-03 04:47:08 +0000163 // Unlink the PHI node from the basic block, but don't delete the PHI yet.
164 MachineInstr *MPhi = MBB.remove(MBB.begin());
Chris Lattnerbc40e892003-01-13 20:01:16 +0000165
Chris Lattner53a79aa2005-10-03 04:47:08 +0000166 unsigned DestReg = MPhi->getOperand(0).getReg();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000167
Bill Wendlingca756d22006-09-28 07:10:24 +0000168 // Create a new register for the incoming PHI arguments.
Chris Lattner53a79aa2005-10-03 04:47:08 +0000169 MachineFunction &MF = *MBB.getParent();
Chris Lattner84bc5422007-12-31 04:13:23 +0000170 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(DestReg);
171 unsigned IncomingReg = MF.getRegInfo().createVirtualRegister(RC);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000172
Chris Lattner53a79aa2005-10-03 04:47:08 +0000173 // Insert a register to register copy in the top of the current block (but
174 // after any remaining phi nodes) which copies the new incoming register
175 // into the phi node destination.
176 //
Owen Andersond10fd972007-12-31 06:32:00 +0000177 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
178 TII->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC, RC);
Chris Lattner53a79aa2005-10-03 04:47:08 +0000179
180 // Update live variable information if there is any...
181 LiveVariables *LV = getAnalysisToUpdate<LiveVariables>();
182 if (LV) {
183 MachineInstr *PHICopy = prior(AfterPHIsIt);
184
Evan Cheng3fefc182007-04-18 00:36:11 +0000185 // Increment use count of the newly created virtual register.
186 LV->getVarInfo(IncomingReg).NumUses++;
187
Chris Lattner53a79aa2005-10-03 04:47:08 +0000188 // Add information to LiveVariables to know that the incoming value is
189 // killed. Note that because the value is defined in several places (once
190 // each for each incoming block), the "def" block and instruction fields
191 // for the VarInfo is not filled in.
Chris Lattnerbc40e892003-01-13 20:01:16 +0000192 //
Chris Lattner53a79aa2005-10-03 04:47:08 +0000193 LV->addVirtualRegisterKilled(IncomingReg, PHICopy);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000194
Chris Lattner53a79aa2005-10-03 04:47:08 +0000195 // Since we are going to be deleting the PHI node, if it is the last use
196 // of any registers, or if the value itself is dead, we need to move this
197 // information over to the new copy we just inserted.
198 //
199 LV->removeVirtualRegistersKilled(MPhi);
Chris Lattnerbc40e892003-01-13 20:01:16 +0000200
Chris Lattner6db07562005-10-03 07:22:07 +0000201 // If the result is dead, update LV.
Evan Cheng6130f662008-03-05 00:59:57 +0000202 if (MPhi->registerDefIsDead(DestReg)) {
Chris Lattner6db07562005-10-03 07:22:07 +0000203 LV->addVirtualRegisterDead(DestReg, PHICopy);
Chris Lattner53a79aa2005-10-03 04:47:08 +0000204 LV->removeVirtualRegistersDead(MPhi);
205 }
Owen Andersona0185402007-11-08 01:20:48 +0000206
207 LV->getVarInfo(IncomingReg).UsedBlocks[MBB.getNumber()] = true;
Chris Lattner53a79aa2005-10-03 04:47:08 +0000208 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000209
Chris Lattner53a79aa2005-10-03 04:47:08 +0000210 // Adjust the VRegPHIUseCount map to account for the removal of this PHI
211 // node.
212 for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2)
Chris Lattner8aa797a2007-12-30 23:10:15 +0000213 --VRegPHIUseCount[BBVRegPair(MPhi->getOperand(i + 1).getMBB(),
214 MPhi->getOperand(i).getReg())];
Chris Lattner572c7702003-05-12 14:28:28 +0000215
Chris Lattner53a79aa2005-10-03 04:47:08 +0000216 // Now loop over all of the incoming arguments, changing them to copy into
217 // the IncomingReg register in the corresponding predecessor basic block.
218 //
Evan Cheng576a2702008-04-03 16:38:20 +0000219 SmallPtrSet<MachineBasicBlock*, 8> MBBsInsertedInto;
Chris Lattner53a79aa2005-10-03 04:47:08 +0000220 for (int i = MPhi->getNumOperands() - 1; i >= 2; i-=2) {
Chris Lattner6db07562005-10-03 07:22:07 +0000221 unsigned SrcReg = MPhi->getOperand(i-1).getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000222 assert(TargetRegisterInfo::isVirtualRegister(SrcReg) &&
Chris Lattner6db07562005-10-03 07:22:07 +0000223 "Machine PHI Operands must all be virtual registers!");
Chris Lattner53a79aa2005-10-03 04:47:08 +0000224
Evan Cheng576a2702008-04-03 16:38:20 +0000225 // If source is defined by an implicit def, there is no need to insert
226 // a copy.
227 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
228 if (DefMI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
229 ImpDefs.insert(DefMI);
230 continue;
231 }
232
Chris Lattner53a79aa2005-10-03 04:47:08 +0000233 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
234 // source path the PHI.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000235 MachineBasicBlock &opBlock = *MPhi->getOperand(i).getMBB();
Chris Lattner53a79aa2005-10-03 04:47:08 +0000236
Chris Lattner53a79aa2005-10-03 04:47:08 +0000237 // Check to make sure we haven't already emitted the copy for this block.
238 // This can happen because PHI nodes may have multiple entries for the
Chris Lattner6db07562005-10-03 07:22:07 +0000239 // same basic block.
Evan Cheng576a2702008-04-03 16:38:20 +0000240 if (!MBBsInsertedInto.insert(&opBlock))
Chris Lattner6db07562005-10-03 07:22:07 +0000241 continue; // If the copy has already been emitted, we're done.
242
Evan Cheng576a2702008-04-03 16:38:20 +0000243 // Find a safe location to insert the copy, this may be the first
244 // terminator in the block (or end()).
245 MachineBasicBlock::iterator InsertPos =
246 findInsertionPoint(opBlock, DefMI, IncomingReg, SrcReg);
Chris Lattner6db07562005-10-03 07:22:07 +0000247
248 // Insert the copy.
Evan Cheng576a2702008-04-03 16:38:20 +0000249 TII->copyRegToReg(opBlock, InsertPos, IncomingReg, SrcReg, RC, RC);
Chris Lattner53a79aa2005-10-03 04:47:08 +0000250
Chris Lattner6db07562005-10-03 07:22:07 +0000251 // Now update live variable information if we have it. Otherwise we're done
252 if (!LV) continue;
253
254 // We want to be able to insert a kill of the register if this PHI
255 // (aka, the copy we just inserted) is the last use of the source
256 // value. Live variable analysis conservatively handles this by
257 // saying that the value is live until the end of the block the PHI
258 // entry lives in. If the value really is dead at the PHI copy, there
259 // will be no successor blocks which have the value live-in.
260 //
261 // Check to see if the copy is the last use, and if so, update the
262 // live variables information so that it knows the copy source
263 // instruction kills the incoming value.
264 //
265 LiveVariables::VarInfo &InRegVI = LV->getVarInfo(SrcReg);
Owen Andersona0185402007-11-08 01:20:48 +0000266 InRegVI.UsedBlocks[opBlock.getNumber()] = true;
Chris Lattner6db07562005-10-03 07:22:07 +0000267
268 // Loop over all of the successors of the basic block, checking to see
269 // if the value is either live in the block, or if it is killed in the
270 // block. Also check to see if this register is in use by another PHI
271 // node which has not yet been eliminated. If so, it will be killed
272 // at an appropriate point later.
273 //
274
275 // Is it used by any PHI instructions in this block?
Bill Wendlingca756d22006-09-28 07:10:24 +0000276 bool ValueIsLive = VRegPHIUseCount[BBVRegPair(&opBlock, SrcReg)] != 0;
Chris Lattner6db07562005-10-03 07:22:07 +0000277
278 std::vector<MachineBasicBlock*> OpSuccBlocks;
279
280 // Otherwise, scan successors, including the BB the PHI node lives in.
281 for (MachineBasicBlock::succ_iterator SI = opBlock.succ_begin(),
282 E = opBlock.succ_end(); SI != E && !ValueIsLive; ++SI) {
283 MachineBasicBlock *SuccMBB = *SI;
284
285 // Is it alive in this successor?
286 unsigned SuccIdx = SuccMBB->getNumber();
287 if (SuccIdx < InRegVI.AliveBlocks.size() &&
288 InRegVI.AliveBlocks[SuccIdx]) {
289 ValueIsLive = true;
290 break;
Chris Lattner927ce5d2003-05-12 03:55:21 +0000291 }
Chris Lattner6db07562005-10-03 07:22:07 +0000292
293 OpSuccBlocks.push_back(SuccMBB);
Chris Lattner927ce5d2003-05-12 03:55:21 +0000294 }
295
Chris Lattner6db07562005-10-03 07:22:07 +0000296 // Check to see if this value is live because there is a use in a successor
297 // that kills it.
298 if (!ValueIsLive) {
299 switch (OpSuccBlocks.size()) {
300 case 1: {
301 MachineBasicBlock *MBB = OpSuccBlocks[0];
302 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
303 if (InRegVI.Kills[i]->getParent() == MBB) {
Chris Lattner53a79aa2005-10-03 04:47:08 +0000304 ValueIsLive = true;
305 break;
306 }
Chris Lattner6db07562005-10-03 07:22:07 +0000307 break;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000308 }
Chris Lattner6db07562005-10-03 07:22:07 +0000309 case 2: {
310 MachineBasicBlock *MBB1 = OpSuccBlocks[0], *MBB2 = OpSuccBlocks[1];
311 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
312 if (InRegVI.Kills[i]->getParent() == MBB1 ||
313 InRegVI.Kills[i]->getParent() == MBB2) {
314 ValueIsLive = true;
315 break;
316 }
317 break;
318 }
319 default:
320 std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end());
321 for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
322 if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(),
323 InRegVI.Kills[i]->getParent())) {
324 ValueIsLive = true;
325 break;
326 }
327 }
328 }
329
330 // Okay, if we now know that the value is not live out of the block,
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000331 // we can add a kill marker in this block saying that it kills the incoming
332 // value!
Chris Lattner6db07562005-10-03 07:22:07 +0000333 if (!ValueIsLive) {
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000334 // In our final twist, we have to decide which instruction kills the
335 // register. In most cases this is the copy, however, the first
336 // terminator instruction at the end of the block may also use the value.
337 // In this case, we should mark *it* as being the killing block, not the
338 // copy.
Evan Cheng576a2702008-04-03 16:38:20 +0000339 MachineBasicBlock::iterator KillInst = prior(InsertPos);
340 MachineBasicBlock::iterator Term = opBlock.getFirstTerminator();
341 if (Term != opBlock.end()) {
342 if (Term->readsRegister(SrcReg))
343 KillInst = Term;
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000344
345 // Check that no other terminators use values.
346#ifndef NDEBUG
Evan Cheng576a2702008-04-03 16:38:20 +0000347 for (MachineBasicBlock::iterator TI = next(Term); TI != opBlock.end();
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000348 ++TI) {
Evan Cheng576a2702008-04-03 16:38:20 +0000349 assert(!TI->readsRegister(SrcReg) &&
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000350 "Terminator instructions cannot use virtual registers unless"
351 "they are the first terminator in a block!");
352 }
353#endif
354 }
355
Chris Lattner2adfa7e2006-01-04 07:12:21 +0000356 // Finally, mark it killed.
357 LV->addVirtualRegisterKilled(SrcReg, KillInst);
Chris Lattner6db07562005-10-03 07:22:07 +0000358
359 // This vreg no longer lives all of the way through opBlock.
360 unsigned opBlockNum = opBlock.getNumber();
361 if (opBlockNum < InRegVI.AliveBlocks.size())
362 InRegVI.AliveBlocks[opBlockNum] = false;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000363 }
Chris Lattnerbc40e892003-01-13 20:01:16 +0000364 }
Chris Lattner53a79aa2005-10-03 04:47:08 +0000365
366 // Really delete the PHI instruction now!
367 delete MPhi;
Chris Lattner6db07562005-10-03 07:22:07 +0000368 ++NumAtomic;
Chris Lattnerbc40e892003-01-13 20:01:16 +0000369}
Bill Wendlingca756d22006-09-28 07:10:24 +0000370
371/// analyzePHINodes - Gather information about the PHI nodes in here. In
372/// particular, we want to map the number of uses of a virtual register which is
373/// used in a PHI node. We map that to the BB the vreg is coming from. This is
374/// used later to determine when the vreg is killed in the BB.
375///
376void PNE::analyzePHINodes(const MachineFunction& Fn) {
377 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
378 I != E; ++I)
379 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
380 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
381 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
Chris Lattner8aa797a2007-12-30 23:10:15 +0000382 ++VRegPHIUseCount[BBVRegPair(BBI->getOperand(i + 1).getMBB(),
383 BBI->getOperand(i).getReg())];
Bill Wendlingca756d22006-09-28 07:10:24 +0000384}