1 //===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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.
27 //===----------------------------------------------------------------------===//
29 #include "llvm/CodeGen/LiveVariables.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/Target/TargetRegisterInfo.h"
33 #include "llvm/Target/TargetInstrInfo.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/ADT/DepthFirstIterator.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/STLExtras.h"
38 #include "llvm/Config/alloca.h"
42 char LiveVariables::ID = 0;
43 static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis");
45 void LiveVariables::VarInfo::dump() const {
46 cerr << " Alive in blocks: ";
47 for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i)
48 if (AliveBlocks[i]) cerr << i << ", ";
49 cerr << " Used in blocks: ";
50 for (unsigned i = 0, e = UsedBlocks.size(); i != e; ++i)
51 if (UsedBlocks[i]) cerr << i << ", ";
52 cerr << "\n Killed by:";
54 cerr << " No instructions.\n";
56 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
57 cerr << "\n #" << i << ": " << *Kills[i];
62 /// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
63 LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
64 assert(TargetRegisterInfo::isVirtualRegister(RegIdx) &&
65 "getVarInfo: not a virtual register!");
66 RegIdx -= TargetRegisterInfo::FirstVirtualRegister;
67 if (RegIdx >= VirtRegInfo.size()) {
68 if (RegIdx >= 2*VirtRegInfo.size())
69 VirtRegInfo.resize(RegIdx*2);
71 VirtRegInfo.resize(2*VirtRegInfo.size());
73 VarInfo &VI = VirtRegInfo[RegIdx];
74 VI.AliveBlocks.resize(MF->getNumBlockIDs());
75 VI.UsedBlocks.resize(MF->getNumBlockIDs());
79 /// KillsRegister - Returns true if the machine instruction kills the specified
81 bool LiveVariables::KillsRegister(MachineInstr *MI, unsigned Reg) const {
82 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
83 const MachineOperand &MO = MI->getOperand(i);
84 if (MO.isRegister() && MO.isKill()) {
85 unsigned MOReg = MO.getReg();
87 (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
88 TargetRegisterInfo::isPhysicalRegister(Reg) &&
89 RegInfo->isSubRegister(MOReg, Reg)))
96 /// RegisterDefIsDead - Returns true if the register is dead in this machine
98 bool LiveVariables::RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
99 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
100 const MachineOperand &MO = MI->getOperand(i);
101 if (MO.isRegister() && MO.isDead()) {
102 unsigned MOReg = MO.getReg();
103 if ((MOReg == Reg) ||
104 (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
105 TargetRegisterInfo::isPhysicalRegister(Reg) &&
106 RegInfo->isSubRegister(MOReg, Reg)))
113 /// ModifiesRegister - Returns true if the machine instruction modifies the
115 bool LiveVariables::ModifiesRegister(MachineInstr *MI, unsigned Reg) const {
116 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
117 const MachineOperand &MO = MI->getOperand(i);
118 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
124 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
125 MachineBasicBlock *DefBlock,
126 MachineBasicBlock *MBB,
127 std::vector<MachineBasicBlock*> &WorkList) {
128 unsigned BBNum = MBB->getNumber();
130 // Check to see if this basic block is one of the killing blocks. If so,
132 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
133 if (VRInfo.Kills[i]->getParent() == MBB) {
134 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
138 if (MBB == DefBlock) return; // Terminate recursion
140 if (VRInfo.AliveBlocks[BBNum])
141 return; // We already know the block is live
143 // Mark the variable known alive in this bb
144 VRInfo.AliveBlocks[BBNum] = true;
146 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
147 E = MBB->pred_rend(); PI != E; ++PI)
148 WorkList.push_back(*PI);
151 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
152 MachineBasicBlock *DefBlock,
153 MachineBasicBlock *MBB) {
154 std::vector<MachineBasicBlock*> WorkList;
155 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
157 while (!WorkList.empty()) {
158 MachineBasicBlock *Pred = WorkList.back();
160 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
164 void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
166 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
167 assert(MRI.getVRegDef(reg) && "Register use before def!");
169 unsigned BBNum = MBB->getNumber();
171 VarInfo& VRInfo = getVarInfo(reg);
172 VRInfo.UsedBlocks[BBNum] = true;
175 // Check to see if this basic block is already a kill block.
176 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
177 // Yes, this register is killed in this basic block already. Increase the
178 // live range by updating the kill instruction.
179 VRInfo.Kills.back() = MI;
184 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
185 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
188 assert(MBB != MRI.getVRegDef(reg)->getParent() &&
189 "Should have kill for defblock!");
191 // Add a new kill entry for this basic block. If this virtual register is
192 // already marked as alive in this basic block, that means it is alive in at
193 // least one of the successor blocks, it's not a kill.
194 if (!VRInfo.AliveBlocks[BBNum])
195 VRInfo.Kills.push_back(MI);
197 // Update all dominating blocks to mark them as "known live".
198 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
199 E = MBB->pred_end(); PI != E; ++PI)
200 MarkVirtRegAliveInBlock(VRInfo, MRI.getVRegDef(reg)->getParent(), *PI);
203 /// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
204 /// implicit defs to a machine instruction if there was an earlier def of its
206 void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
207 // Turn previous partial def's into read/mod/write.
208 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
209 MachineInstr *Def = PhysRegPartDef[Reg][i];
211 // First one is just a def. This means the use is reading some undef bits.
213 Def->addOperand(MachineOperand::CreateReg(Reg,
218 Def->addOperand(MachineOperand::CreateReg(Reg,
223 PhysRegPartDef[Reg].clear();
225 // There was an earlier def of a super-register. Add implicit def to that MI.
230 // Add implicit def to A.
231 if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] &&
233 MachineInstr *Def = PhysRegInfo[Reg];
235 if (!Def->findRegisterDefOperand(Reg))
236 Def->addOperand(MachineOperand::CreateReg(Reg,
241 // There is a now a proper use, forget about the last partial use.
242 PhysRegPartUse[Reg] = NULL;
243 PhysRegInfo[Reg] = MI;
244 PhysRegUsed[Reg] = true;
246 // Now reset the use information for the sub-registers.
247 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
248 unsigned SubReg = *SubRegs; ++SubRegs) {
249 // FIXME: Should we do: "PhysRegPartUse[SubReg] = NULL;" here?
250 PhysRegInfo[SubReg] = MI;
251 PhysRegUsed[SubReg] = true;
254 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
255 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
256 // Remember the partial use of this super-register if it was previously
258 bool HasPrevDef = PhysRegInfo[SuperReg] != NULL;
261 // No need to go up more levels. A def of a register also sets its sub-
262 // registers. So if PhysRegInfo[SuperReg] is NULL, it means SuperReg's
263 // super-registers are not previously defined.
264 for (const unsigned *SSRegs = RegInfo->getSuperRegisters(SuperReg);
265 unsigned SSReg = *SSRegs; ++SSRegs)
266 if (PhysRegInfo[SSReg] != NULL) {
272 PhysRegInfo[SuperReg] = MI;
273 PhysRegPartUse[SuperReg] = MI;
278 /// addRegisterKills - For all of a register's sub-registers that are killed in
279 /// at this machine instruction, mark them as "killed". (If the machine operand
280 /// isn't found, add it first.)
281 void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
282 SmallSet<unsigned, 4> &SubKills) {
283 if (SubKills.count(Reg) == 0) {
284 MI->addRegisterKilled(Reg, RegInfo, true);
288 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
289 unsigned SubReg = *SubRegs; ++SubRegs)
290 addRegisterKills(SubReg, MI, SubKills);
293 /// HandlePhysRegKill - The recursive version of HandlePhysRegKill. Returns true
296 /// - The register has no sub-registers and the machine instruction is the
297 /// last def/use of the register, or
298 /// - The register has sub-registers and none of them are killed elsewhere.
300 /// SubKills is filled with the set of sub-registers that are killed elsewhere.
301 bool LiveVariables::HandlePhysRegKill(unsigned Reg, const MachineInstr *RefMI,
302 SmallSet<unsigned, 4> &SubKills) {
303 const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
305 for (; unsigned SubReg = *SubRegs; ++SubRegs) {
306 const MachineInstr *LastRef = PhysRegInfo[SubReg];
308 if (LastRef != RefMI ||
309 !HandlePhysRegKill(SubReg, RefMI, SubKills))
310 SubKills.insert(SubReg);
314 // No sub-registers, just check if reg is killed by RefMI.
315 if (PhysRegInfo[Reg] == RefMI)
317 } else if (SubKills.empty()) {
318 // None of the sub-registers are killed elsewhere.
325 /// HandlePhysRegKill - Returns true if the whole register is killed in the
326 /// machine instruction. If only some of its sub-registers are killed in this
327 /// machine instruction, then mark those as killed and return false.
328 bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
329 SmallSet<unsigned, 4> SubKills;
331 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
332 // This machine instruction kills this register.
333 RefMI->addRegisterKilled(Reg, RegInfo, true);
337 // Some sub-registers are killed by another machine instruction.
338 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
339 unsigned SubReg = *SubRegs; ++SubRegs)
340 addRegisterKills(SubReg, RefMI, SubKills);
345 void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
346 // Does this kill a previous version of this register?
347 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
348 if (PhysRegUsed[Reg]) {
349 if (!HandlePhysRegKill(Reg, LastRef)) {
350 if (PhysRegPartUse[Reg])
351 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true);
353 } else if (PhysRegPartUse[Reg]) {
354 // Add implicit use / kill to last partial use.
355 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true);
356 } else if (LastRef != MI) {
357 // Defined, but not used. However, watch out for cases where a super-reg
358 // is also defined on the same MI.
359 LastRef->addRegisterDead(Reg, RegInfo);
363 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
364 unsigned SubReg = *SubRegs; ++SubRegs) {
365 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
366 if (PhysRegUsed[SubReg]) {
367 if (!HandlePhysRegKill(SubReg, LastRef)) {
368 if (PhysRegPartUse[SubReg])
369 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true);
371 } else if (PhysRegPartUse[SubReg]) {
372 // Add implicit use / kill to last use of a sub-register.
373 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true);
374 } else if (LastRef != MI) {
375 // This must be a def of the subreg on the same MI.
376 LastRef->addRegisterDead(SubReg, RegInfo);
382 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
383 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
384 if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) {
385 // The larger register is previously defined. Now a smaller part is
386 // being re-defined. Treat it as read/mod/write.
388 // AX = EAX<imp-use,kill>, EAX<imp-def>
389 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
390 true/*IsImp*/,true/*IsKill*/));
391 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
393 PhysRegInfo[SuperReg] = MI;
394 PhysRegUsed[SuperReg] = false;
395 PhysRegPartUse[SuperReg] = NULL;
397 // Remember this partial def.
398 PhysRegPartDef[SuperReg].push_back(MI);
402 PhysRegInfo[Reg] = MI;
403 PhysRegUsed[Reg] = false;
404 PhysRegPartDef[Reg].clear();
405 PhysRegPartUse[Reg] = NULL;
407 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
408 unsigned SubReg = *SubRegs; ++SubRegs) {
409 PhysRegInfo[SubReg] = MI;
410 PhysRegUsed[SubReg] = false;
411 PhysRegPartDef[SubReg].clear();
412 PhysRegPartUse[SubReg] = NULL;
417 bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
419 RegInfo = MF->getTarget().getRegisterInfo();
420 MachineRegisterInfo& MRI = mf.getRegInfo();
421 assert(RegInfo && "Target doesn't have register information?");
423 ReservedRegisters = RegInfo->getReservedRegs(mf);
425 unsigned NumRegs = RegInfo->getNumRegs();
426 PhysRegInfo = new MachineInstr*[NumRegs];
427 PhysRegUsed = new bool[NumRegs];
428 PhysRegPartUse = new MachineInstr*[NumRegs];
429 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
430 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
431 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
432 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
433 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
435 /// Get some space for a respectable number of registers.
436 VirtRegInfo.resize(64);
440 // Calculate live variable information in depth first order on the CFG of the
441 // function. This guarantees that we will see the definition of a virtual
442 // register before its uses due to dominance properties of SSA (except for PHI
443 // nodes, which are treated as a special case).
444 MachineBasicBlock *Entry = MF->begin();
445 SmallPtrSet<MachineBasicBlock*,16> Visited;
447 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
448 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
450 MachineBasicBlock *MBB = *DFI;
452 // Mark live-in registers as live-in.
453 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
454 EE = MBB->livein_end(); II != EE; ++II) {
455 assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
456 "Cannot have a live-in virtual register!");
457 HandlePhysRegDef(*II, 0);
460 // Loop over all of the instructions, processing them.
461 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
463 MachineInstr *MI = I;
465 // Process all of the operands of the instruction...
466 unsigned NumOperandsToProcess = MI->getNumOperands();
468 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
469 // of the uses. They will be handled in other basic blocks.
470 if (MI->getOpcode() == TargetInstrInfo::PHI)
471 NumOperandsToProcess = 1;
474 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
475 const MachineOperand &MO = MI->getOperand(i);
477 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
478 unsigned MOReg = MO.getReg();
480 if (TargetRegisterInfo::isVirtualRegister(MOReg))
481 HandleVirtRegUse(MOReg, MBB, MI);
482 else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
483 !ReservedRegisters[MOReg])
484 HandlePhysRegUse(MOReg, MI);
489 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
490 const MachineOperand &MO = MI->getOperand(i);
492 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
493 unsigned MOReg = MO.getReg();
495 if (TargetRegisterInfo::isVirtualRegister(MOReg)) {
496 VarInfo &VRInfo = getVarInfo(MOReg);
498 if (VRInfo.AliveBlocks.none())
499 // If vr is not alive in any block, then defaults to dead.
500 VRInfo.Kills.push_back(MI);
501 } else if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
502 !ReservedRegisters[MOReg]) {
503 HandlePhysRegDef(MOReg, MI);
509 // Handle any virtual assignments from PHI nodes which might be at the
510 // bottom of this basic block. We check all of our successor blocks to see
511 // if they have PHI nodes, and if so, we simulate an assignment at the end
512 // of the current block.
513 if (!PHIVarInfo[MBB->getNumber()].empty()) {
514 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
516 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
517 E = VarInfoVec.end(); I != E; ++I)
518 // Mark it alive only in the block we are representing.
519 MarkVirtRegAliveInBlock(getVarInfo(*I), MRI.getVRegDef(*I)->getParent(),
523 // Finally, if the last instruction in the block is a return, make sure to
524 // mark it as using all of the live-out values in the function.
525 if (!MBB->empty() && MBB->back().getDesc().isReturn()) {
526 MachineInstr *Ret = &MBB->back();
528 for (MachineRegisterInfo::liveout_iterator
529 I = MF->getRegInfo().liveout_begin(),
530 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
531 assert(TargetRegisterInfo::isPhysicalRegister(*I) &&
532 "Cannot have a live-in virtual register!");
533 HandlePhysRegUse(*I, Ret);
535 // Add live-out registers as implicit uses.
536 if (Ret->findRegisterUseOperandIdx(*I) == -1)
537 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
541 // Loop over PhysRegInfo, killing any registers that are available at the
542 // end of the basic block. This also resets the PhysRegInfo map.
543 for (unsigned i = 0; i != NumRegs; ++i)
545 HandlePhysRegDef(i, 0);
547 // Clear some states between BB's. These are purely local information.
548 for (unsigned i = 0; i != NumRegs; ++i)
549 PhysRegPartDef[i].clear();
551 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
552 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
553 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
556 // Convert and transfer the dead / killed information we have gathered into
557 // VirtRegInfo onto MI's.
558 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
559 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j)
560 if (VirtRegInfo[i].Kills[j] ==
561 MRI.getVRegDef(i + TargetRegisterInfo::FirstVirtualRegister))
563 .Kills[j]->addRegisterDead(i +
564 TargetRegisterInfo::FirstVirtualRegister,
568 .Kills[j]->addRegisterKilled(i +
569 TargetRegisterInfo::FirstVirtualRegister,
572 // Check to make sure there are no unreachable blocks in the MC CFG for the
573 // function. If so, it is due to a bug in the instruction selector or some
574 // other part of the code generator if this happens.
576 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
577 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
580 delete[] PhysRegInfo;
581 delete[] PhysRegUsed;
582 delete[] PhysRegPartUse;
583 delete[] PhysRegPartDef;
589 /// instructionChanged - When the address of an instruction changes, this method
590 /// should be called so that live variables can update its internal data
591 /// structures. This removes the records for OldMI, transfering them to the
592 /// records for NewMI.
593 void LiveVariables::instructionChanged(MachineInstr *OldMI,
594 MachineInstr *NewMI) {
595 // If the instruction defines any virtual registers, update the VarInfo,
596 // kill and dead information for the instruction.
597 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
598 MachineOperand &MO = OldMI->getOperand(i);
599 if (MO.isRegister() && MO.getReg() &&
600 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
601 unsigned Reg = MO.getReg();
602 VarInfo &VI = getVarInfo(Reg);
606 addVirtualRegisterDead(Reg, NewMI);
611 addVirtualRegisterKilled(Reg, NewMI);
613 // If this is a kill of the value, update the VI kills list.
614 if (VI.removeKill(OldMI))
615 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
620 /// removeVirtualRegistersKilled - Remove all killed info for the specified
622 void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
623 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
624 MachineOperand &MO = MI->getOperand(i);
625 if (MO.isRegister() && MO.isKill()) {
627 unsigned Reg = MO.getReg();
628 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
629 bool removed = getVarInfo(Reg).removeKill(MI);
630 assert(removed && "kill not in register's VarInfo?");
636 /// removeVirtualRegistersDead - Remove all of the dead registers for the
637 /// specified instruction from the live variable information.
638 void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
639 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
640 MachineOperand &MO = MI->getOperand(i);
641 if (MO.isRegister() && MO.isDead()) {
643 unsigned Reg = MO.getReg();
644 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
645 bool removed = getVarInfo(Reg).removeKill(MI);
646 assert(removed && "kill not in register's VarInfo?");
652 /// analyzePHINodes - Gather information about the PHI nodes in here. In
653 /// particular, we want to map the variable information of a virtual register
654 /// which is used in a PHI node. We map that to the BB the vreg is coming from.
656 void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
657 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
659 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
660 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
661 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
662 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
663 .push_back(BBI->getOperand(i).getReg());