Be careful when to add implicit kill / dead operands. Don't add them during / post...
[oota-llvm.git] / lib / CodeGen / LiveVariables.cpp
1 //===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
2 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LiveVariable analysis pass.  For each machine
11 // instruction in the function, this pass calculates the set of registers that
12 // are immediately dead after the instruction (i.e., the instruction calculates
13 // the value, but it is never used) and the set of registers that are used by
14 // the instruction, but are never used after the instruction (i.e., they are
15 // killed).
16 //
17 // This class computes live variables using are sparse implementation based on
18 // the machine code SSA form.  This class computes live variable information for
19 // each virtual and _register allocatable_ physical register in a function.  It
20 // uses the dominance properties of SSA form to efficiently compute live
21 // variables for virtual registers, and assumes that physical registers are only
22 // live within a single basic block (allowing it to do a single local analysis
23 // to resolve physical register lifetimes in each basic block).  If a physical
24 // register is not register allocatable, it is not tracked.  This is useful for
25 // things like the stack pointer and condition codes.
26 //
27 //===----------------------------------------------------------------------===//
28
29 #include "llvm/CodeGen/LiveVariables.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/Target/MRegisterInfo.h"
32 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/ADT/DepthFirstIterator.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/Config/alloca.h"
37 #include <algorithm>
38 using namespace llvm;
39
40 static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis");
41
42 void LiveVariables::VarInfo::dump() const {
43   cerr << "Register Defined by: ";
44   if (DefInst) 
45     cerr << *DefInst;
46   else
47     cerr << "<null>\n";
48   cerr << "  Alive in blocks: ";
49   for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i)
50     if (AliveBlocks[i]) cerr << i << ", ";
51   cerr << "\n  Killed by:";
52   if (Kills.empty())
53     cerr << " No instructions.\n";
54   else {
55     for (unsigned i = 0, e = Kills.size(); i != e; ++i)
56       cerr << "\n    #" << i << ": " << *Kills[i];
57     cerr << "\n";
58   }
59 }
60
61 LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
62   assert(MRegisterInfo::isVirtualRegister(RegIdx) &&
63          "getVarInfo: not a virtual register!");
64   RegIdx -= MRegisterInfo::FirstVirtualRegister;
65   if (RegIdx >= VirtRegInfo.size()) {
66     if (RegIdx >= 2*VirtRegInfo.size())
67       VirtRegInfo.resize(RegIdx*2);
68     else
69       VirtRegInfo.resize(2*VirtRegInfo.size());
70   }
71   VarInfo &VI = VirtRegInfo[RegIdx];
72   VI.AliveBlocks.resize(MF->getNumBlockIDs());
73   return VI;
74 }
75
76 bool LiveVariables::KillsRegister(MachineInstr *MI, unsigned Reg) const {
77   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
78     MachineOperand &MO = MI->getOperand(i);
79     if (MO.isReg() && MO.isKill()) {
80       if ((MO.getReg() == Reg) ||
81           (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
82            MRegisterInfo::isPhysicalRegister(Reg) &&
83            RegInfo->isSubRegister(MO.getReg(), Reg)))
84         return true;
85     }
86   }
87   return false;
88 }
89
90 bool LiveVariables::RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
91   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
92     MachineOperand &MO = MI->getOperand(i);
93     if (MO.isReg() && MO.isDead()) {
94       if ((MO.getReg() == Reg) ||
95           (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
96            MRegisterInfo::isPhysicalRegister(Reg) &&
97            RegInfo->isSubRegister(MO.getReg(), Reg)))
98         return true;
99     }
100   }
101   return false;
102 }
103
104 bool LiveVariables::ModifiesRegister(MachineInstr *MI, unsigned Reg) const {
105   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
106     MachineOperand &MO = MI->getOperand(i);
107     if (MO.isReg() && MO.isDef() && MO.getReg() == Reg)
108       return true;
109   }
110   return false;
111 }
112
113 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
114                                             MachineBasicBlock *MBB) {
115   unsigned BBNum = MBB->getNumber();
116
117   // Check to see if this basic block is one of the killing blocks.  If so,
118   // remove it...
119   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
120     if (VRInfo.Kills[i]->getParent() == MBB) {
121       VRInfo.Kills.erase(VRInfo.Kills.begin()+i);  // Erase entry
122       break;
123     }
124
125   if (MBB == VRInfo.DefInst->getParent()) return;  // Terminate recursion
126
127   if (VRInfo.AliveBlocks[BBNum])
128     return;  // We already know the block is live
129
130   // Mark the variable known alive in this bb
131   VRInfo.AliveBlocks[BBNum] = true;
132
133   for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
134          E = MBB->pred_end(); PI != E; ++PI)
135     MarkVirtRegAliveInBlock(VRInfo, *PI);
136 }
137
138 void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
139                                      MachineInstr *MI) {
140   assert(VRInfo.DefInst && "Register use before def!");
141
142   VRInfo.NumUses++;
143
144   // Check to see if this basic block is already a kill block...
145   if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
146     // Yes, this register is killed in this basic block already.  Increase the
147     // live range by updating the kill instruction.
148     VRInfo.Kills.back() = MI;
149     return;
150   }
151
152 #ifndef NDEBUG
153   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
154     assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
155 #endif
156
157   assert(MBB != VRInfo.DefInst->getParent() &&
158          "Should have kill for defblock!");
159
160   // Add a new kill entry for this basic block.
161   // If this virtual register is already marked as alive in this basic block,
162   // that means it is alive in at least one of the successor block, it's not
163   // a kill.
164   if (!VRInfo.AliveBlocks[MBB->getNumber()])
165     VRInfo.Kills.push_back(MI);
166
167   // Update all dominating blocks to mark them known live.
168   for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
169          E = MBB->pred_end(); PI != E; ++PI)
170     MarkVirtRegAliveInBlock(VRInfo, *PI);
171 }
172
173 bool LiveVariables::addRegisterKilled(unsigned IncomingReg, MachineInstr *MI,
174                                       bool AddIfNotFound) {
175   bool Found = false;
176   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
177     MachineOperand &MO = MI->getOperand(i);
178     if (MO.isReg() && MO.isUse()) {
179       unsigned Reg = MO.getReg();
180       if (!Reg)
181         continue;
182       if (Reg == IncomingReg) {
183         MO.setIsKill();
184         Found = true;
185         break;
186       } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
187                  MRegisterInfo::isPhysicalRegister(IncomingReg) &&
188                  RegInfo->isSuperRegister(IncomingReg, Reg) &&
189                  MO.isKill())
190         // A super-register kill already exists.
191         return true;
192     }
193   }
194
195   // If not found, this means an alias of one of the operand is killed. Add a
196   // new implicit operand if required.
197   if (!Found && AddIfNotFound) {
198     MI->addRegOperand(IncomingReg, false/*IsDef*/,true/*IsImp*/,true/*IsKill*/);
199     return true;
200   }
201   return Found;
202 }
203
204 bool LiveVariables::addRegisterDead(unsigned IncomingReg, MachineInstr *MI,
205                                     bool AddIfNotFound) {
206   bool Found = false;
207   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
208     MachineOperand &MO = MI->getOperand(i);
209     if (MO.isReg() && MO.isDef()) {
210       unsigned Reg = MO.getReg();
211       if (!Reg)
212         continue;
213       if (Reg == IncomingReg) {
214         MO.setIsDead();
215         Found = true;
216         break;
217       } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
218                  MRegisterInfo::isPhysicalRegister(IncomingReg) &&
219                  RegInfo->isSuperRegister(IncomingReg, Reg) &&
220                  MO.isDead())
221         // There exists a super-register that's marked dead.
222         return true;
223     }
224   }
225
226   // If not found, this means an alias of one of the operand is dead. Add a
227   // new implicit operand.
228   if (!Found && AddIfNotFound) {
229     MI->addRegOperand(IncomingReg, true/*IsDef*/,true/*IsImp*/,false/*IsKill*/,
230                       true/*IsDead*/);
231     return true;
232   }
233   return Found;
234 }
235
236 void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
237   // There is a now a proper use, forget about the last partial use.
238   PhysRegPartUse[Reg] = NULL;
239
240   // Turn previous partial def's into read/mod/write.
241   for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
242     MachineInstr *Def = PhysRegPartDef[Reg][i];
243     // First one is just a def. This means the use is reading some undef bits.
244     if (i != 0)
245       Def->addRegOperand(Reg, false/*IsDef*/,true/*IsImp*/,true/*IsKill*/);
246     Def->addRegOperand(Reg, true/*IsDef*/,true/*IsImp*/);
247   }
248   PhysRegPartDef[Reg].clear();
249
250   // There was an earlier def of a super-register. Add implicit def to that MI.
251   // A: EAX = ...
252   // B:     = AX
253   // Add implicit def to A.
254   if (PhysRegInfo[Reg] && !PhysRegUsed[Reg]) {
255     MachineInstr *Def = PhysRegInfo[Reg];
256     if (!Def->findRegisterDefOperand(Reg))
257       Def->addRegOperand(Reg, true/*IsDef*/,true/*IsImp*/);
258   }
259
260   PhysRegInfo[Reg] = MI;
261   PhysRegUsed[Reg] = true;
262
263   for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
264        unsigned SubReg = *SubRegs; ++SubRegs) {
265     PhysRegInfo[SubReg] = MI;
266     PhysRegUsed[SubReg] = true;
267   }
268
269   // Remember the partial uses.
270   for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
271        unsigned SuperReg = *SuperRegs; ++SuperRegs)
272     PhysRegPartUse[SuperReg] = MI;
273 }
274
275 void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
276   // Does this kill a previous version of this register?
277   if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
278     if (PhysRegUsed[Reg])
279       addRegisterKilled(Reg, LastRef);
280     else if (PhysRegPartUse[Reg])
281       // Add implicit use / kill to last use of a sub-register.
282       addRegisterKilled(Reg, PhysRegPartUse[Reg], true);
283     else
284       addRegisterDead(Reg, LastRef, true);
285   }
286   PhysRegInfo[Reg] = MI;
287   PhysRegUsed[Reg] = false;
288   PhysRegPartUse[Reg] = NULL;
289
290   for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
291        unsigned SubReg = *SubRegs; ++SubRegs) {
292     if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
293       if (PhysRegUsed[SubReg])
294         addRegisterKilled(SubReg, LastRef);
295       else if (PhysRegPartUse[SubReg])
296         // Add implicit use / kill to last use of a sub-register.
297         addRegisterKilled(SubReg, PhysRegPartUse[SubReg]);
298       else
299         addRegisterDead(SubReg, LastRef);
300     }
301     PhysRegInfo[SubReg] = MI;
302     PhysRegUsed[SubReg] = false;
303   }
304
305   if (MI)
306     for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
307          unsigned SuperReg = *SuperRegs; ++SuperRegs) {
308       if (PhysRegInfo[SuperReg]) {
309         // The larger register is previously defined. Now a smaller part is
310         // being re-defined. Treat it as read/mod/write.
311         // EAX =
312         // AX  =        EAX<imp-use,kill>, EAX<imp-def>
313         MI->addRegOperand(SuperReg, false/*IsDef*/,true/*IsImp*/,true/*IsKill*/);
314         MI->addRegOperand(SuperReg, true/*IsDef*/,true/*IsImp*/);
315         PhysRegInfo[SuperReg] = MI;
316         PhysRegUsed[SuperReg] = false;
317       } else {
318         // Remember this partial def.
319         PhysRegPartDef[SuperReg].push_back(MI);
320       }
321   }
322 }
323
324 bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
325   MF = &mf;
326   const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
327   RegInfo = MF->getTarget().getRegisterInfo();
328   assert(RegInfo && "Target doesn't have register information?");
329
330   ReservedRegisters = RegInfo->getReservedRegs(mf);
331
332   unsigned NumRegs = RegInfo->getNumRegs();
333   PhysRegInfo = new MachineInstr*[NumRegs];
334   PhysRegUsed = new bool[NumRegs];
335   PhysRegPartUse = new MachineInstr*[NumRegs];
336   PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
337   PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
338   std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
339   std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
340   std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
341
342   /// Get some space for a respectable number of registers...
343   VirtRegInfo.resize(64);
344
345   analyzePHINodes(mf);
346
347   // Calculate live variable information in depth first order on the CFG of the
348   // function.  This guarantees that we will see the definition of a virtual
349   // register before its uses due to dominance properties of SSA (except for PHI
350   // nodes, which are treated as a special case).
351   //
352   MachineBasicBlock *Entry = MF->begin();
353   std::set<MachineBasicBlock*> Visited;
354   for (df_ext_iterator<MachineBasicBlock*> DFI = df_ext_begin(Entry, Visited),
355          E = df_ext_end(Entry, Visited); DFI != E; ++DFI) {
356     MachineBasicBlock *MBB = *DFI;
357
358     // Mark live-in registers as live-in.
359     for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
360            EE = MBB->livein_end(); II != EE; ++II) {
361       assert(MRegisterInfo::isPhysicalRegister(*II) &&
362              "Cannot have a live-in virtual register!");
363       HandlePhysRegDef(*II, 0);
364     }
365
366     // Loop over all of the instructions, processing them.
367     for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
368          I != E; ++I) {
369       MachineInstr *MI = I;
370
371       // Process all of the operands of the instruction...
372       unsigned NumOperandsToProcess = MI->getNumOperands();
373
374       // Unless it is a PHI node.  In this case, ONLY process the DEF, not any
375       // of the uses.  They will be handled in other basic blocks.
376       if (MI->getOpcode() == TargetInstrInfo::PHI)
377         NumOperandsToProcess = 1;
378
379       // Process all uses...
380       for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
381         MachineOperand &MO = MI->getOperand(i);
382         if (MO.isRegister() && MO.isUse() && MO.getReg()) {
383           if (MRegisterInfo::isVirtualRegister(MO.getReg())){
384             HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
385           } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
386                      !ReservedRegisters[MO.getReg()]) {
387             HandlePhysRegUse(MO.getReg(), MI);
388           }
389         }
390       }
391
392       // Process all defs...
393       for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
394         MachineOperand &MO = MI->getOperand(i);
395         if (MO.isRegister() && MO.isDef() && MO.getReg()) {
396           if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
397             VarInfo &VRInfo = getVarInfo(MO.getReg());
398
399             assert(VRInfo.DefInst == 0 && "Variable multiply defined!");
400             VRInfo.DefInst = MI;
401             // Defaults to dead
402             VRInfo.Kills.push_back(MI);
403           } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
404                      !ReservedRegisters[MO.getReg()]) {
405             HandlePhysRegDef(MO.getReg(), MI);
406           }
407         }
408       }
409     }
410
411     // Handle any virtual assignments from PHI nodes which might be at the
412     // bottom of this basic block.  We check all of our successor blocks to see
413     // if they have PHI nodes, and if so, we simulate an assignment at the end
414     // of the current block.
415     if (!PHIVarInfo[MBB->getNumber()].empty()) {
416       SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
417
418       for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
419              E = VarInfoVec.end(); I != E; ++I) {
420         VarInfo& VRInfo = getVarInfo(*I);
421         assert(VRInfo.DefInst && "Register use before def (or no def)!");
422
423         // Only mark it alive only in the block we are representing.
424         MarkVirtRegAliveInBlock(VRInfo, MBB);
425       }
426     }
427
428     // Finally, if the last instruction in the block is a return, make sure to mark
429     // it as using all of the live-out values in the function.
430     if (!MBB->empty() && TII.isReturn(MBB->back().getOpcode())) {
431       MachineInstr *Ret = &MBB->back();
432       for (MachineFunction::liveout_iterator I = MF->liveout_begin(),
433              E = MF->liveout_end(); I != E; ++I) {
434         assert(MRegisterInfo::isPhysicalRegister(*I) &&
435                "Cannot have a live-in virtual register!");
436         HandlePhysRegUse(*I, Ret);
437         // Add live-out registers as implicit uses.
438         Ret->addRegOperand(*I, false, true);
439       }
440     }
441
442     // Loop over PhysRegInfo, killing any registers that are available at the
443     // end of the basic block.  This also resets the PhysRegInfo map.
444     for (unsigned i = 0; i != NumRegs; ++i)
445       if (PhysRegInfo[i])
446         HandlePhysRegDef(i, 0);
447
448     // Clear some states between BB's. These are purely local information.
449     for (unsigned i = 0; i != NumRegs; ++i)
450       PhysRegPartDef[i].clear();
451     std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
452   }
453
454   // Convert and transfer the dead / killed information we have gathered into
455   // VirtRegInfo onto MI's.
456   //
457   for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
458     for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j) {
459       if (VirtRegInfo[i].Kills[j] == VirtRegInfo[i].DefInst)
460         addRegisterDead(i + MRegisterInfo::FirstVirtualRegister,
461                         VirtRegInfo[i].Kills[j]);
462       else
463         addRegisterKilled(i + MRegisterInfo::FirstVirtualRegister,
464                           VirtRegInfo[i].Kills[j]);
465     }
466
467   // Check to make sure there are no unreachable blocks in the MC CFG for the
468   // function.  If so, it is due to a bug in the instruction selector or some
469   // other part of the code generator if this happens.
470 #ifndef NDEBUG
471   for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
472     assert(Visited.count(&*i) != 0 && "unreachable basic block found");
473 #endif
474
475   delete[] PhysRegInfo;
476   delete[] PhysRegUsed;
477   delete[] PhysRegPartUse;
478   delete[] PhysRegPartDef;
479   delete[] PHIVarInfo;
480
481   return false;
482 }
483
484 /// instructionChanged - When the address of an instruction changes, this
485 /// method should be called so that live variables can update its internal
486 /// data structures.  This removes the records for OldMI, transfering them to
487 /// the records for NewMI.
488 void LiveVariables::instructionChanged(MachineInstr *OldMI,
489                                        MachineInstr *NewMI) {
490   // If the instruction defines any virtual registers, update the VarInfo,
491   // kill and dead information for the instruction.
492   for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
493     MachineOperand &MO = OldMI->getOperand(i);
494     if (MO.isRegister() && MO.getReg() &&
495         MRegisterInfo::isVirtualRegister(MO.getReg())) {
496       unsigned Reg = MO.getReg();
497       VarInfo &VI = getVarInfo(Reg);
498       if (MO.isDef()) {
499         if (MO.isDead()) {
500           MO.unsetIsDead();
501           addVirtualRegisterDead(Reg, NewMI);
502         }
503         // Update the defining instruction.
504         if (VI.DefInst == OldMI)
505           VI.DefInst = NewMI;
506       }
507       if (MO.isUse()) {
508         if (MO.isKill()) {
509           MO.unsetIsKill();
510           addVirtualRegisterKilled(Reg, NewMI);
511         }
512         // If this is a kill of the value, update the VI kills list.
513         if (VI.removeKill(OldMI))
514           VI.Kills.push_back(NewMI);   // Yes, there was a kill of it
515       }
516     }
517   }
518 }
519
520 /// removeVirtualRegistersKilled - Remove all killed info for the specified
521 /// instruction.
522 void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
523   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
524     MachineOperand &MO = MI->getOperand(i);
525     if (MO.isReg() && MO.isKill()) {
526       MO.unsetIsKill();
527       unsigned Reg = MO.getReg();
528       if (MRegisterInfo::isVirtualRegister(Reg)) {
529         bool removed = getVarInfo(Reg).removeKill(MI);
530         assert(removed && "kill not in register's VarInfo?");
531       }
532     }
533   }
534 }
535
536 /// removeVirtualRegistersDead - Remove all of the dead registers for the
537 /// specified instruction from the live variable information.
538 void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
539   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
540     MachineOperand &MO = MI->getOperand(i);
541     if (MO.isReg() && MO.isDead()) {
542       MO.unsetIsDead();
543       unsigned Reg = MO.getReg();
544       if (MRegisterInfo::isVirtualRegister(Reg)) {
545         bool removed = getVarInfo(Reg).removeKill(MI);
546         assert(removed && "kill not in register's VarInfo?");
547       }
548     }
549   }
550 }
551
552 /// analyzePHINodes - Gather information about the PHI nodes in here. In
553 /// particular, we want to map the variable information of a virtual
554 /// register which is used in a PHI node. We map that to the BB the vreg is
555 /// coming from.
556 ///
557 void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
558   for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
559        I != E; ++I)
560     for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
561          BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
562       for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
563         PHIVarInfo[BBI->getOperand(i + 1).getMachineBasicBlock()->getNumber()].
564           push_back(BBI->getOperand(i).getReg());
565 }