LiveVariables.cpp revision 90a3868fe5702caaa56082cde2edb6521de73e01
1//===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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/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" 39#include <algorithm> 40using namespace llvm; 41 42char LiveVariables::ID = 0; 43static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis"); 44 45void 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:"; 53 if (Kills.empty()) 54 cerr << " No instructions.\n"; 55 else { 56 for (unsigned i = 0, e = Kills.size(); i != e; ++i) 57 cerr << "\n #" << i << ": " << *Kills[i]; 58 cerr << "\n"; 59 } 60} 61 62/// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg. 63LiveVariables::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); 70 else 71 VirtRegInfo.resize(2*VirtRegInfo.size()); 72 } 73 VarInfo &VI = VirtRegInfo[RegIdx]; 74 VI.AliveBlocks.resize(MF->getNumBlockIDs()); 75 VI.UsedBlocks.resize(MF->getNumBlockIDs()); 76 return VI; 77} 78 79/// KillsRegister - Returns true if the machine instruction kills the specified 80/// register. 81bool 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(); 86 if (MOReg == Reg || 87 (TargetRegisterInfo::isPhysicalRegister(MOReg) && 88 TargetRegisterInfo::isPhysicalRegister(Reg) && 89 RegInfo->isSubRegister(MOReg, Reg))) 90 return true; 91 } 92 } 93 return false; 94} 95 96/// RegisterDefIsDead - Returns true if the register is dead in this machine 97/// instruction. 98bool 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))) 107 return true; 108 } 109 } 110 return false; 111} 112 113/// ModifiesRegister - Returns true if the machine instruction modifies the 114/// register. 115bool 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) 119 return true; 120 } 121 return false; 122} 123 124void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo, 125 MachineBasicBlock *DefBlock, 126 MachineBasicBlock *MBB, 127 std::vector<MachineBasicBlock*> &WorkList) { 128 unsigned BBNum = MBB->getNumber(); 129 130 // Check to see if this basic block is one of the killing blocks. If so, 131 // remove it. 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 135 break; 136 } 137 138 if (MBB == DefBlock) return; // Terminate recursion 139 140 if (VRInfo.AliveBlocks[BBNum]) 141 return; // We already know the block is live 142 143 // Mark the variable known alive in this bb 144 VRInfo.AliveBlocks[BBNum] = true; 145 146 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(), 147 E = MBB->pred_rend(); PI != E; ++PI) 148 WorkList.push_back(*PI); 149} 150 151void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo, 152 MachineBasicBlock *DefBlock, 153 MachineBasicBlock *MBB) { 154 std::vector<MachineBasicBlock*> WorkList; 155 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList); 156 while (!WorkList.empty()) { 157 MachineBasicBlock *Pred = WorkList.back(); 158 WorkList.pop_back(); 159 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList); 160 } 161} 162 163 164void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB, 165 MachineInstr *MI) { 166 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo(); 167 assert(MRI.getVRegDef(reg) && "Register use before def!"); 168 169 unsigned BBNum = MBB->getNumber(); 170 171 VarInfo& VRInfo = getVarInfo(reg); 172 VRInfo.UsedBlocks[BBNum] = true; 173 VRInfo.NumUses++; 174 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; 180 return; 181 } 182 183#ifndef NDEBUG 184 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) 185 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!"); 186#endif 187 188 assert(MBB != MRI.getVRegDef(reg)->getParent() && 189 "Should have kill for defblock!"); 190 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); 196 197 // Update all dominating blocks to mark them 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); 201} 202 203void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) { 204 // Turn previous partial def's into read/mod/write. 205 for (unsigned i = 0, e = PhysRegPartDef[Reg].size(); i != e; ++i) { 206 MachineInstr *Def = PhysRegPartDef[Reg][i]; 207 // First one is just a def. This means the use is reading some undef bits. 208 if (i != 0) 209 Def->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/, 210 true/*IsImp*/,true/*IsKill*/)); 211 Def->addOperand(MachineOperand::CreateReg(Reg,true/*IsDef*/,true/*IsImp*/)); 212 } 213 214 PhysRegPartDef[Reg].clear(); 215 216 // There was an earlier def of a super-register. Add implicit def to that MI. 217 // A: EAX = ... 218 // B: = AX 219 // Add implicit def to A. 220 if (PhysRegInfo[Reg] && PhysRegInfo[Reg] != PhysRegPartUse[Reg] && 221 !PhysRegUsed[Reg]) { 222 MachineInstr *Def = PhysRegInfo[Reg]; 223 if (!Def->findRegisterDefOperand(Reg)) 224 Def->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/, 225 true/*IsImp*/)); 226 } 227 228 // There is a now a proper use, forget about the last partial use. 229 PhysRegPartUse[Reg] = NULL; 230 PhysRegInfo[Reg] = MI; 231 PhysRegUsed[Reg] = true; 232 233 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg); 234 unsigned SubReg = *SubRegs; ++SubRegs) { 235 PhysRegInfo[SubReg] = MI; 236 PhysRegUsed[SubReg] = true; 237 } 238 239 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg); 240 unsigned SuperReg = *SuperRegs; ++SuperRegs) { 241 // Remember the partial use of this superreg if it was previously defined. 242 bool HasPrevDef = PhysRegInfo[SuperReg] != NULL; 243 if (!HasPrevDef) { 244 for (const unsigned *SSRegs = RegInfo->getSuperRegisters(SuperReg); 245 unsigned SSReg = *SSRegs; ++SSRegs) { 246 if (PhysRegInfo[SSReg] != NULL) { 247 HasPrevDef = true; 248 break; 249 } 250 } 251 } 252 if (HasPrevDef) { 253 PhysRegInfo[SuperReg] = MI; 254 PhysRegPartUse[SuperReg] = MI; 255 } 256 } 257} 258 259bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI, 260 SmallSet<unsigned, 4> &SubKills) { 261 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg); 262 unsigned SubReg = *SubRegs; ++SubRegs) { 263 MachineInstr *LastRef = PhysRegInfo[SubReg]; 264 if (LastRef != RefMI || 265 !HandlePhysRegKill(SubReg, RefMI, SubKills)) 266 SubKills.insert(SubReg); 267 } 268 269 if (*RegInfo->getImmediateSubRegisters(Reg) == 0) { 270 // No sub-registers, just check if reg is killed by RefMI. 271 if (PhysRegInfo[Reg] == RefMI) 272 return true; 273 } else if (SubKills.empty()) 274 // None of the sub-registers are killed elsewhere... 275 return true; 276 return false; 277} 278 279void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI, 280 SmallSet<unsigned, 4> &SubKills) { 281 if (SubKills.count(Reg) == 0) 282 MI->addRegisterKilled(Reg, RegInfo, true); 283 else { 284 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg); 285 unsigned SubReg = *SubRegs; ++SubRegs) 286 addRegisterKills(SubReg, MI, SubKills); 287 } 288} 289 290bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) { 291 SmallSet<unsigned, 4> SubKills; 292 if (HandlePhysRegKill(Reg, RefMI, SubKills)) { 293 RefMI->addRegisterKilled(Reg, RegInfo, true); 294 return true; 295 } else { 296 // Some sub-registers are killed by another MI. 297 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg); 298 unsigned SubReg = *SubRegs; ++SubRegs) 299 addRegisterKills(SubReg, RefMI, SubKills); 300 return false; 301 } 302} 303 304void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) { 305 // Does this kill a previous version of this register? 306 if (MachineInstr *LastRef = PhysRegInfo[Reg]) { 307 if (PhysRegUsed[Reg]) { 308 if (!HandlePhysRegKill(Reg, LastRef)) { 309 if (PhysRegPartUse[Reg]) 310 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true); 311 } 312 } else if (PhysRegPartUse[Reg]) 313 // Add implicit use / kill to last partial use. 314 PhysRegPartUse[Reg]->addRegisterKilled(Reg, RegInfo, true); 315 else if (LastRef != MI) 316 // Defined, but not used. However, watch out for cases where a super-reg 317 // is also defined on the same MI. 318 LastRef->addRegisterDead(Reg, RegInfo); 319 } 320 321 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg); 322 unsigned SubReg = *SubRegs; ++SubRegs) { 323 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) { 324 if (PhysRegUsed[SubReg]) { 325 if (!HandlePhysRegKill(SubReg, LastRef)) { 326 if (PhysRegPartUse[SubReg]) 327 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true); 328 } 329 } else if (PhysRegPartUse[SubReg]) 330 // Add implicit use / kill to last use of a sub-register. 331 PhysRegPartUse[SubReg]->addRegisterKilled(SubReg, RegInfo, true); 332 else if (LastRef != MI) 333 // This must be a def of the subreg on the same MI. 334 LastRef->addRegisterDead(SubReg, RegInfo); 335 } 336 } 337 338 if (MI) { 339 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg); 340 unsigned SuperReg = *SuperRegs; ++SuperRegs) { 341 if (PhysRegInfo[SuperReg] && PhysRegInfo[SuperReg] != MI) { 342 // The larger register is previously defined. Now a smaller part is 343 // being re-defined. Treat it as read/mod/write. 344 // EAX = 345 // AX = EAX<imp-use,kill>, EAX<imp-def> 346 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/, 347 true/*IsImp*/,true/*IsKill*/)); 348 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/, 349 true/*IsImp*/)); 350 PhysRegInfo[SuperReg] = MI; 351 PhysRegUsed[SuperReg] = false; 352 PhysRegPartUse[SuperReg] = NULL; 353 } else { 354 // Remember this partial def. 355 PhysRegPartDef[SuperReg].push_back(MI); 356 } 357 } 358 359 PhysRegInfo[Reg] = MI; 360 PhysRegUsed[Reg] = false; 361 PhysRegPartDef[Reg].clear(); 362 PhysRegPartUse[Reg] = NULL; 363 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg); 364 unsigned SubReg = *SubRegs; ++SubRegs) { 365 PhysRegInfo[SubReg] = MI; 366 PhysRegUsed[SubReg] = false; 367 PhysRegPartDef[SubReg].clear(); 368 PhysRegPartUse[SubReg] = NULL; 369 } 370 } 371} 372 373bool LiveVariables::runOnMachineFunction(MachineFunction &mf) { 374 MF = &mf; 375 RegInfo = MF->getTarget().getRegisterInfo(); 376 MachineRegisterInfo& MRI = mf.getRegInfo(); 377 assert(RegInfo && "Target doesn't have register information?"); 378 379 ReservedRegisters = RegInfo->getReservedRegs(mf); 380 381 unsigned NumRegs = RegInfo->getNumRegs(); 382 PhysRegInfo = new MachineInstr*[NumRegs]; 383 PhysRegUsed = new bool[NumRegs]; 384 PhysRegPartUse = new MachineInstr*[NumRegs]; 385 PhysRegPartDef = new SmallVector<MachineInstr*,4>[NumRegs]; 386 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()]; 387 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0); 388 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false); 389 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0); 390 391 /// Get some space for a respectable number of registers... 392 VirtRegInfo.resize(64); 393 394 analyzePHINodes(mf); 395 396 // Calculate live variable information in depth first order on the CFG of the 397 // function. This guarantees that we will see the definition of a virtual 398 // register before its uses due to dominance properties of SSA (except for PHI 399 // nodes, which are treated as a special case). 400 // 401 MachineBasicBlock *Entry = MF->begin(); 402 SmallPtrSet<MachineBasicBlock*,16> Visited; 403 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> > 404 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited); 405 DFI != E; ++DFI) { 406 MachineBasicBlock *MBB = *DFI; 407 408 // Mark live-in registers as live-in. 409 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(), 410 EE = MBB->livein_end(); II != EE; ++II) { 411 assert(TargetRegisterInfo::isPhysicalRegister(*II) && 412 "Cannot have a live-in virtual register!"); 413 HandlePhysRegDef(*II, 0); 414 } 415 416 // Loop over all of the instructions, processing them. 417 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); 418 I != E; ++I) { 419 MachineInstr *MI = I; 420 421 // Process all of the operands of the instruction... 422 unsigned NumOperandsToProcess = MI->getNumOperands(); 423 424 // Unless it is a PHI node. In this case, ONLY process the DEF, not any 425 // of the uses. They will be handled in other basic blocks. 426 if (MI->getOpcode() == TargetInstrInfo::PHI) 427 NumOperandsToProcess = 1; 428 429 // Process all uses... 430 for (unsigned i = 0; i != NumOperandsToProcess; ++i) { 431 const MachineOperand &MO = MI->getOperand(i); 432 if (MO.isRegister() && MO.isUse() && MO.getReg()) { 433 unsigned MOReg = MO.getReg(); 434 if (TargetRegisterInfo::isVirtualRegister(MOReg)) 435 HandleVirtRegUse(MOReg, MBB, MI); 436 else if (TargetRegisterInfo::isPhysicalRegister(MOReg) && 437 !ReservedRegisters[MOReg]) 438 HandlePhysRegUse(MOReg, MI); 439 } 440 } 441 442 // Process all defs... 443 for (unsigned i = 0; i != NumOperandsToProcess; ++i) { 444 const MachineOperand &MO = MI->getOperand(i); 445 if (MO.isRegister() && MO.isDef() && MO.getReg()) { 446 unsigned MOReg = MO.getReg(); 447 if (TargetRegisterInfo::isVirtualRegister(MOReg)) { 448 VarInfo &VRInfo = getVarInfo(MOReg); 449 450 if (VRInfo.AliveBlocks.none()) 451 // If vr is not alive in any block, then defaults to dead. 452 VRInfo.Kills.push_back(MI); 453 } else if (TargetRegisterInfo::isPhysicalRegister(MOReg) && 454 !ReservedRegisters[MOReg]) { 455 HandlePhysRegDef(MOReg, MI); 456 } 457 } 458 } 459 } 460 461 // Handle any virtual assignments from PHI nodes which might be at the 462 // bottom of this basic block. We check all of our successor blocks to see 463 // if they have PHI nodes, and if so, we simulate an assignment at the end 464 // of the current block. 465 if (!PHIVarInfo[MBB->getNumber()].empty()) { 466 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()]; 467 468 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(), 469 E = VarInfoVec.end(); I != E; ++I) { 470 // Only mark it alive only in the block we are representing. 471 MarkVirtRegAliveInBlock(getVarInfo(*I), MRI.getVRegDef(*I)->getParent(), 472 MBB); 473 } 474 } 475 476 // Finally, if the last instruction in the block is a return, make sure to mark 477 // it as using all of the live-out values in the function. 478 if (!MBB->empty() && MBB->back().getDesc().isReturn()) { 479 MachineInstr *Ret = &MBB->back(); 480 for (MachineRegisterInfo::liveout_iterator 481 I = MF->getRegInfo().liveout_begin(), 482 E = MF->getRegInfo().liveout_end(); I != E; ++I) { 483 assert(TargetRegisterInfo::isPhysicalRegister(*I) && 484 "Cannot have a live-in virtual register!"); 485 HandlePhysRegUse(*I, Ret); 486 // Add live-out registers as implicit uses. 487 if (Ret->findRegisterUseOperandIdx(*I) == -1) 488 Ret->addOperand(MachineOperand::CreateReg(*I, false, true)); 489 } 490 } 491 492 // Loop over PhysRegInfo, killing any registers that are available at the 493 // end of the basic block. This also resets the PhysRegInfo map. 494 for (unsigned i = 0; i != NumRegs; ++i) 495 if (PhysRegInfo[i]) 496 HandlePhysRegDef(i, 0); 497 498 // Clear some states between BB's. These are purely local information. 499 for (unsigned i = 0; i != NumRegs; ++i) 500 PhysRegPartDef[i].clear(); 501 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0); 502 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false); 503 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0); 504 } 505 506 // Convert and transfer the dead / killed information we have gathered into 507 // VirtRegInfo onto MI's. 508 // 509 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) 510 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j) { 511 if (VirtRegInfo[i].Kills[j] == MRI.getVRegDef(i + 512 TargetRegisterInfo::FirstVirtualRegister)) 513 VirtRegInfo[i].Kills[j]->addRegisterDead(i + 514 TargetRegisterInfo::FirstVirtualRegister, 515 RegInfo); 516 else 517 VirtRegInfo[i].Kills[j]->addRegisterKilled(i + 518 TargetRegisterInfo::FirstVirtualRegister, 519 RegInfo); 520 } 521 522 // Check to make sure there are no unreachable blocks in the MC CFG for the 523 // function. If so, it is due to a bug in the instruction selector or some 524 // other part of the code generator if this happens. 525#ifndef NDEBUG 526 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i) 527 assert(Visited.count(&*i) != 0 && "unreachable basic block found"); 528#endif 529 530 delete[] PhysRegInfo; 531 delete[] PhysRegUsed; 532 delete[] PhysRegPartUse; 533 delete[] PhysRegPartDef; 534 delete[] PHIVarInfo; 535 536 return false; 537} 538 539/// instructionChanged - When the address of an instruction changes, this 540/// method should be called so that live variables can update its internal 541/// data structures. This removes the records for OldMI, transfering them to 542/// the records for NewMI. 543void LiveVariables::instructionChanged(MachineInstr *OldMI, 544 MachineInstr *NewMI) { 545 // If the instruction defines any virtual registers, update the VarInfo, 546 // kill and dead information for the instruction. 547 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) { 548 MachineOperand &MO = OldMI->getOperand(i); 549 if (MO.isRegister() && MO.getReg() && 550 TargetRegisterInfo::isVirtualRegister(MO.getReg())) { 551 unsigned Reg = MO.getReg(); 552 VarInfo &VI = getVarInfo(Reg); 553 if (MO.isDef()) { 554 if (MO.isDead()) { 555 MO.setIsDead(false); 556 addVirtualRegisterDead(Reg, NewMI); 557 } 558 } 559 if (MO.isKill()) { 560 MO.setIsKill(false); 561 addVirtualRegisterKilled(Reg, NewMI); 562 } 563 // If this is a kill of the value, update the VI kills list. 564 if (VI.removeKill(OldMI)) 565 VI.Kills.push_back(NewMI); // Yes, there was a kill of it 566 } 567 } 568} 569 570/// removeVirtualRegistersKilled - Remove all killed info for the specified 571/// instruction. 572void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) { 573 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 574 MachineOperand &MO = MI->getOperand(i); 575 if (MO.isRegister() && MO.isKill()) { 576 MO.setIsKill(false); 577 unsigned Reg = MO.getReg(); 578 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 579 bool removed = getVarInfo(Reg).removeKill(MI); 580 assert(removed && "kill not in register's VarInfo?"); 581 } 582 } 583 } 584} 585 586/// removeVirtualRegistersDead - Remove all of the dead registers for the 587/// specified instruction from the live variable information. 588void LiveVariables::removeVirtualRegistersDead(MachineInstr *MI) { 589 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 590 MachineOperand &MO = MI->getOperand(i); 591 if (MO.isRegister() && MO.isDead()) { 592 MO.setIsDead(false); 593 unsigned Reg = MO.getReg(); 594 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 595 bool removed = getVarInfo(Reg).removeKill(MI); 596 assert(removed && "kill not in register's VarInfo?"); 597 } 598 } 599 } 600} 601 602/// analyzePHINodes - Gather information about the PHI nodes in here. In 603/// particular, we want to map the variable information of a virtual 604/// register which is used in a PHI node. We map that to the BB the vreg is 605/// coming from. 606/// 607void LiveVariables::analyzePHINodes(const MachineFunction& Fn) { 608 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end(); 609 I != E; ++I) 610 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end(); 611 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) 612 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) 613 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()] 614 .push_back(BBI->getOperand(i).getReg()); 615} 616