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/MRegisterInfo.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 << "Register Defined by: ";
51 cerr << " Alive in blocks: ";
52 for (unsigned i = 0, e = AliveBlocks.size(); i != e; ++i)
53 if (AliveBlocks[i]) cerr << i << ", ";
54 cerr << " Used in blocks: ";
55 for (unsigned i = 0, e = UsedBlocks.size(); i != e; ++i)
56 if (UsedBlocks[i]) cerr << i << ", ";
57 cerr << "\n Killed by:";
59 cerr << " No instructions.\n";
61 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
62 cerr << "\n #" << i << ": " << *Kills[i];
67 LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
68 assert(MRegisterInfo::isVirtualRegister(RegIdx) &&
69 "getVarInfo: not a virtual register!");
70 RegIdx -= MRegisterInfo::FirstVirtualRegister;
71 if (RegIdx >= VirtRegInfo.size()) {
72 if (RegIdx >= 2*VirtRegInfo.size())
73 VirtRegInfo.resize(RegIdx*2);
75 VirtRegInfo.resize(2*VirtRegInfo.size());
77 VarInfo &VI = VirtRegInfo[RegIdx];
78 VI.AliveBlocks.resize(MF->getNumBlockIDs());
79 VI.UsedBlocks.resize(MF->getNumBlockIDs());
83 bool LiveVariables::KillsRegister(MachineInstr *MI, unsigned Reg) const {
84 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
85 MachineOperand &MO = MI->getOperand(i);
86 if (MO.isRegister() && MO.isKill()) {
87 if ((MO.getReg() == Reg) ||
88 (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
89 MRegisterInfo::isPhysicalRegister(Reg) &&
90 RegInfo->isSubRegister(MO.getReg(), Reg)))
97 bool LiveVariables::RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
98 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
99 MachineOperand &MO = MI->getOperand(i);
100 if (MO.isRegister() && MO.isDead()) {
101 if ((MO.getReg() == Reg) ||
102 (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
103 MRegisterInfo::isPhysicalRegister(Reg) &&
104 RegInfo->isSubRegister(MO.getReg(), Reg)))
111 bool LiveVariables::ModifiesRegister(MachineInstr *MI, unsigned Reg) const {
112 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
113 MachineOperand &MO = MI->getOperand(i);
114 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
120 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
121 MachineBasicBlock *MBB,
122 std::vector<MachineBasicBlock*> &WorkList) {
123 unsigned BBNum = MBB->getNumber();
125 // Check to see if this basic block is one of the killing blocks. If so,
127 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
128 if (VRInfo.Kills[i]->getParent() == MBB) {
129 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
133 if (MBB == VRInfo.DefInst->getParent()) return; // Terminate recursion
135 if (VRInfo.AliveBlocks[BBNum])
136 return; // We already know the block is live
138 // Mark the variable known alive in this bb
139 VRInfo.AliveBlocks[BBNum] = true;
141 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(),
142 E = MBB->pred_rend(); PI != E; ++PI)
143 WorkList.push_back(*PI);
146 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
147 MachineBasicBlock *MBB) {
148 std::vector<MachineBasicBlock*> WorkList;
149 MarkVirtRegAliveInBlock(VRInfo, MBB, WorkList);
150 while (!WorkList.empty()) {
151 MachineBasicBlock *Pred = WorkList.back();
153 MarkVirtRegAliveInBlock(VRInfo, Pred, WorkList);
158 void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
160 assert(VRInfo.DefInst && "Register use before def!");
162 unsigned BBNum = MBB->getNumber();
164 VRInfo.UsedBlocks[BBNum] = true;
167 // Check to see if this basic block is already a kill block...
168 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
169 // Yes, this register is killed in this basic block already. Increase the
170 // live range by updating the kill instruction.
171 VRInfo.Kills.back() = MI;
176 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
177 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
180 assert(MBB != VRInfo.DefInst->getParent() &&
181 "Should have kill for defblock!");
183 // Add a new kill entry for this basic block.
184 // If this virtual register is already marked as alive in this basic block,
185 // that means it is alive in at least one of the successor block, it's not
187 if (!VRInfo.AliveBlocks[BBNum])
188 VRInfo.Kills.push_back(MI);
190 // Update all dominating blocks to mark them known live.
191 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
192 E = MBB->pred_end(); PI != E; ++PI)
193 MarkVirtRegAliveInBlock(VRInfo, *PI);
196 bool LiveVariables::addRegisterKilled(unsigned IncomingReg, MachineInstr *MI,
197 const MRegisterInfo *RegInfo,
198 bool AddIfNotFound) {
200 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
201 MachineOperand &MO = MI->getOperand(i);
202 if (MO.isRegister() && MO.isUse()) {
203 unsigned Reg = MO.getReg();
206 if (Reg == IncomingReg) {
210 } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
211 MRegisterInfo::isPhysicalRegister(IncomingReg) &&
212 RegInfo->isSuperRegister(IncomingReg, Reg) &&
214 // A super-register kill already exists.
219 // If not found, this means an alias of one of the operand is killed. Add a
220 // new implicit operand if required.
221 if (!Found && AddIfNotFound) {
222 MI->addOperand(MachineOperand::CreateReg(IncomingReg, false/*IsDef*/,
223 true/*IsImp*/,true/*IsKill*/));
229 bool LiveVariables::addRegisterDead(unsigned IncomingReg, MachineInstr *MI,
230 const MRegisterInfo *RegInfo,
231 bool AddIfNotFound) {
233 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
234 MachineOperand &MO = MI->getOperand(i);
235 if (MO.isRegister() && MO.isDef()) {
236 unsigned Reg = MO.getReg();
239 if (Reg == IncomingReg) {
243 } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
244 MRegisterInfo::isPhysicalRegister(IncomingReg) &&
245 RegInfo->isSuperRegister(IncomingReg, Reg) &&
247 // There exists a super-register that's marked dead.
252 // If not found, this means an alias of one of the operand is dead. Add a
253 // new implicit operand.
254 if (!Found && AddIfNotFound) {
255 MI->addOperand(MachineOperand::CreateReg(IncomingReg, true/*IsDef*/,
256 true/*IsImp*/,false/*IsKill*/,
263 void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
264 // Turn previous partial def's into read/mod/write.
265 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) {
266 MachineInstr *Def = PhysRegPartDef[Reg][i];
267 // First one is just a def. This means the use is reading some undef bits.
269 Def->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
270 true/*IsImp*/,true/*IsKill*/));
271 Def->addOperand(MachineOperand::CreateReg(Reg,true/*IsDef*/,true/*IsImp*/));
273 PhysRegPartDef[Reg].clear();
275 // There was an earlier def of a super-register. Add implicit def to that MI.
278 // Add implicit def to A.
279 if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] &&
281 MachineInstr *Def = PhysRegInfo[Reg];
282 if (!Def->findRegisterDefOperand(Reg))
283 Def->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
287 // There is a now a proper use, forget about the last partial use.
288 PhysRegPartUse[Reg] = NULL;
289 PhysRegInfo[Reg] = MI;
290 PhysRegUsed[Reg] = true;
292 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
293 unsigned SubReg = *SubRegs; ++SubRegs) {
294 PhysRegInfo[SubReg] = MI;
295 PhysRegUsed[SubReg] = true;
298 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
299 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
300 // Remember the partial use of this superreg if it was previously defined.
301 bool HasPrevDef = PhysRegInfo[SuperReg] != NULL;
303 for (const unsigned *SSRegs = RegInfo->getSuperRegisters(SuperReg);
304 unsigned SSReg = *SSRegs; ++SSRegs) {
305 if (PhysRegInfo[SSReg] != NULL) {
312 PhysRegInfo[SuperReg] = MI;
313 PhysRegPartUse[SuperReg] = MI;
318 bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI,
319 SmallSet<unsigned, 4> &SubKills) {
320 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
321 unsigned SubReg = *SubRegs; ++SubRegs) {
322 MachineInstr *LastRef = PhysRegInfo[SubReg];
323 if (LastRef != RefMI ||
324 !HandlePhysRegKill(SubReg, RefMI, SubKills))
325 SubKills.insert(SubReg);
328 if (*RegInfo->getImmediateSubRegisters(Reg) == 0) {
329 // No sub-registers, just check if reg is killed by RefMI.
330 if (PhysRegInfo[Reg] == RefMI)
332 } else if (SubKills.empty())
333 // None of the sub-registers are killed elsewhere...
338 void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
339 SmallSet<unsigned, 4> &SubKills) {
340 if (SubKills.count(Reg) == 0)
341 addRegisterKilled(Reg, MI, RegInfo, true);
343 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
344 unsigned SubReg = *SubRegs; ++SubRegs)
345 addRegisterKills(SubReg, MI, SubKills);
349 bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
350 SmallSet<unsigned, 4> SubKills;
351 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
352 addRegisterKilled(Reg, RefMI, RegInfo, true);
355 // Some sub-registers are killed by another MI.
356 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
357 unsigned SubReg = *SubRegs; ++SubRegs)
358 addRegisterKills(SubReg, RefMI, SubKills);
363 void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
364 // Does this kill a previous version of this register?
365 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
366 if (PhysRegUsed[Reg]) {
367 if (!HandlePhysRegKill(Reg, LastRef)) {
368 if (PhysRegPartUse[Reg])
369 addRegisterKilled(Reg, PhysRegPartUse[Reg], RegInfo, true);
371 } else if (PhysRegPartUse[Reg])
372 // Add implicit use / kill to last partial use.
373 addRegisterKilled(Reg, PhysRegPartUse[Reg], RegInfo, true);
374 else if (LastRef != MI)
375 // Defined, but not used. However, watch out for cases where a super-reg
376 // is also defined on the same MI.
377 addRegisterDead(Reg, LastRef, RegInfo);
380 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
381 unsigned SubReg = *SubRegs; ++SubRegs) {
382 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
383 if (PhysRegUsed[SubReg]) {
384 if (!HandlePhysRegKill(SubReg, LastRef)) {
385 if (PhysRegPartUse[SubReg])
386 addRegisterKilled(SubReg, PhysRegPartUse[SubReg], RegInfo, true);
388 } else if (PhysRegPartUse[SubReg])
389 // Add implicit use / kill to last use of a sub-register.
390 addRegisterKilled(SubReg, PhysRegPartUse[SubReg], RegInfo, true);
391 else if (LastRef != MI)
392 // This must be a def of the subreg on the same MI.
393 addRegisterDead(SubReg, LastRef, RegInfo);
398 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
399 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
400 if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) {
401 // The larger register is previously defined. Now a smaller part is
402 // being re-defined. Treat it as read/mod/write.
404 // AX = EAX<imp-use,kill>, EAX<imp-def>
405 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/,
406 true/*IsImp*/,true/*IsKill*/));
407 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/,
409 PhysRegInfo[SuperReg] = MI;
410 PhysRegUsed[SuperReg] = false;
411 PhysRegPartUse[SuperReg] = NULL;
413 // Remember this partial def.
414 PhysRegPartDef[SuperReg].push_back(MI);
418 PhysRegInfo[Reg] = MI;
419 PhysRegUsed[Reg] = false;
420 PhysRegPartDef[Reg].clear();
421 PhysRegPartUse[Reg] = NULL;
422 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
423 unsigned SubReg = *SubRegs; ++SubRegs) {
424 PhysRegInfo[SubReg] = MI;
425 PhysRegUsed[SubReg] = false;
426 PhysRegPartDef[SubReg].clear();
427 PhysRegPartUse[SubReg] = NULL;
432 bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
434 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
435 RegInfo = MF->getTarget().getRegisterInfo();
436 assert(RegInfo && "Target doesn't have register information?");
438 ReservedRegisters = RegInfo->getReservedRegs(mf);
440 unsigned NumRegs = RegInfo->getNumRegs();
441 PhysRegInfo = new MachineInstr*[NumRegs];
442 PhysRegUsed = new bool[NumRegs];
443 PhysRegPartUse = new MachineInstr*[NumRegs];
444 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs];
445 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()];
446 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
447 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
448 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
450 /// Get some space for a respectable number of registers...
451 VirtRegInfo.resize(64);
455 // Calculate live variable information in depth first order on the CFG of the
456 // function. This guarantees that we will see the definition of a virtual
457 // register before its uses due to dominance properties of SSA (except for PHI
458 // nodes, which are treated as a special case).
460 MachineBasicBlock *Entry = MF->begin();
461 SmallPtrSet<MachineBasicBlock*,16> Visited;
462 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
463 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
465 MachineBasicBlock *MBB = *DFI;
467 // Mark live-in registers as live-in.
468 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(),
469 EE = MBB->livein_end(); II != EE; ++II) {
470 assert(MRegisterInfo::isPhysicalRegister(*II) &&
471 "Cannot have a live-in virtual register!");
472 HandlePhysRegDef(*II, 0);
475 // Loop over all of the instructions, processing them.
476 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
478 MachineInstr *MI = I;
480 // Process all of the operands of the instruction...
481 unsigned NumOperandsToProcess = MI->getNumOperands();
483 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
484 // of the uses. They will be handled in other basic blocks.
485 if (MI->getOpcode() == TargetInstrInfo::PHI)
486 NumOperandsToProcess = 1;
488 // Process all uses...
489 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
490 MachineOperand &MO = MI->getOperand(i);
491 if (MO.isRegister() && MO.isUse() && MO.getReg()) {
492 if (MRegisterInfo::isVirtualRegister(MO.getReg())){
493 HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
494 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
495 !ReservedRegisters[MO.getReg()]) {
496 HandlePhysRegUse(MO.getReg(), MI);
501 // Process all defs...
502 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
503 MachineOperand &MO = MI->getOperand(i);
504 if (MO.isRegister() && MO.isDef() && MO.getReg()) {
505 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
506 VarInfo &VRInfo = getVarInfo(MO.getReg());
508 assert(VRInfo.DefInst == 0 && "Variable multiply defined!");
511 VRInfo.Kills.push_back(MI);
512 } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
513 !ReservedRegisters[MO.getReg()]) {
514 HandlePhysRegDef(MO.getReg(), MI);
520 // Handle any virtual assignments from PHI nodes which might be at the
521 // bottom of this basic block. We check all of our successor blocks to see
522 // if they have PHI nodes, and if so, we simulate an assignment at the end
523 // of the current block.
524 if (!PHIVarInfo[MBB->getNumber()].empty()) {
525 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()];
527 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(),
528 E = VarInfoVec.end(); I != E; ++I) {
529 VarInfo& VRInfo = getVarInfo(*I);
530 assert(VRInfo.DefInst && "Register use before def (or no def)!");
532 // Only mark it alive only in the block we are representing.
533 MarkVirtRegAliveInBlock(VRInfo, MBB);
537 // Finally, if the last instruction in the block is a return, make sure to mark
538 // it as using all of the live-out values in the function.
539 if (!MBB->empty() && TII.isReturn(MBB->back().getOpcode())) {
540 MachineInstr *Ret = &MBB->back();
541 for (MachineRegisterInfo::liveout_iterator
542 I = MF->getRegInfo().liveout_begin(),
543 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
544 assert(MRegisterInfo::isPhysicalRegister(*I) &&
545 "Cannot have a live-in virtual register!");
546 HandlePhysRegUse(*I, Ret);
547 // Add live-out registers as implicit uses.
548 if (Ret->findRegisterUseOperandIdx(*I) == -1)
549 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
553 // Loop over PhysRegInfo, killing any registers that are available at the
554 // end of the basic block. This also resets the PhysRegInfo map.
555 for (unsigned i = 0; i != NumRegs; ++i)
557 HandlePhysRegDef(i, 0);
559 // Clear some states between BB's. These are purely local information.
560 for (unsigned i = 0; i != NumRegs; ++i)
561 PhysRegPartDef[i].clear();
562 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
563 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
564 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
567 // Convert and transfer the dead / killed information we have gathered into
568 // VirtRegInfo onto MI's.
570 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i)
571 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j) {
572 if (VirtRegInfo[i].Kills[j] == VirtRegInfo[i].DefInst)
573 addRegisterDead(i + MRegisterInfo::FirstVirtualRegister,
574 VirtRegInfo[i].Kills[j], RegInfo);
576 addRegisterKilled(i + MRegisterInfo::FirstVirtualRegister,
577 VirtRegInfo[i].Kills[j], RegInfo);
580 // Check to make sure there are no unreachable blocks in the MC CFG for the
581 // function. If so, it is due to a bug in the instruction selector or some
582 // other part of the code generator if this happens.
584 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
585 assert(Visited.count(&*i) != 0 && "unreachable basic block found");
588 delete[] PhysRegInfo;
589 delete[] PhysRegUsed;
590 delete[] PhysRegPartUse;
591 delete[] PhysRegPartDef;
597 /// instructionChanged - When the address of an instruction changes, this
598 /// method should be called so that live variables can update its internal
599 /// data structures. This removes the records for OldMI, transfering them to
600 /// the records for NewMI.
601 void LiveVariables::instructionChanged(MachineInstr *OldMI,
602 MachineInstr *NewMI) {
603 // If the instruction defines any virtual registers, update the VarInfo,
604 // kill and dead information for the instruction.
605 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
606 MachineOperand &MO = OldMI->getOperand(i);
607 if (MO.isRegister() && MO.getReg() &&
608 MRegisterInfo::isVirtualRegister(MO.getReg())) {
609 unsigned Reg = MO.getReg();
610 VarInfo &VI = getVarInfo(Reg);
614 addVirtualRegisterDead(Reg, NewMI);
616 // Update the defining instruction.
617 if (VI.DefInst == OldMI)
622 addVirtualRegisterKilled(Reg, NewMI);
624 // If this is a kill of the value, update the VI kills list.
625 if (VI.removeKill(OldMI))
626 VI.Kills.push_back(NewMI); // Yes, there was a kill of it
631 /// transferKillDeadInfo - Similar to instructionChanged except it does not
632 /// update live variables internal data structures.
633 void LiveVariables::transferKillDeadInfo(MachineInstr *OldMI,
635 const MRegisterInfo *RegInfo) {
636 // If the instruction defines any virtual registers, update the VarInfo,
637 // kill and dead information for the instruction.
638 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
639 MachineOperand &MO = OldMI->getOperand(i);
640 if (MO.isRegister() && MO.getReg() &&
641 MRegisterInfo::isVirtualRegister(MO.getReg())) {
642 unsigned Reg = MO.getReg();
646 addRegisterDead(Reg, NewMI, RegInfo);
651 addRegisterKilled(Reg, NewMI, RegInfo);
658 /// removeVirtualRegistersKilled - Remove all killed info for the specified
660 void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
661 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
662 MachineOperand &MO = MI->getOperand(i);
663 if (MO.isRegister() && MO.isKill()) {
665 unsigned Reg = MO.getReg();
666 if (MRegisterInfo::isVirtualRegister(Reg)) {
667 bool removed = getVarInfo(Reg).removeKill(MI);
668 assert(removed && "kill not in register's VarInfo?");
674 /// removeVirtualRegistersDead - Remove all of the dead registers for the
675 /// specified instruction from the live variable information.
676 void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) {
677 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
678 MachineOperand &MO = MI->getOperand(i);
679 if (MO.isRegister() && MO.isDead()) {
681 unsigned Reg = MO.getReg();
682 if (MRegisterInfo::isVirtualRegister(Reg)) {
683 bool removed = getVarInfo(Reg).removeKill(MI);
684 assert(removed && "kill not in register's VarInfo?");
690 /// analyzePHINodes - Gather information about the PHI nodes in here. In
691 /// particular, we want to map the variable information of a virtual
692 /// register which is used in a PHI node. We map that to the BB the vreg is
695 void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
696 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end();
698 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
699 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI)
700 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
701 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()].
702 push_back(BBI->getOperand(i).getReg());