LLVM API Documentation
00001 //===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the LiveVariable analysis pass. For each machine 00011 // instruction in the function, this pass calculates the set of registers that 00012 // are immediately dead after the instruction (i.e., the instruction calculates 00013 // the value, but it is never used) and the set of registers that are used by 00014 // the instruction, but are never used after the instruction (i.e., they are 00015 // killed). 00016 // 00017 // This class computes live variables using are sparse implementation based on 00018 // the machine code SSA form. This class computes live variable information for 00019 // each virtual and _register allocatable_ physical register in a function. It 00020 // uses the dominance properties of SSA form to efficiently compute live 00021 // variables for virtual registers, and assumes that physical registers are only 00022 // live within a single basic block (allowing it to do a single local analysis 00023 // to resolve physical register lifetimes in each basic block). If a physical 00024 // register is not register allocatable, it is not tracked. This is useful for 00025 // things like the stack pointer and condition codes. 00026 // 00027 //===----------------------------------------------------------------------===// 00028 00029 #include "llvm/CodeGen/LiveVariables.h" 00030 #include "llvm/CodeGen/MachineInstr.h" 00031 #include "llvm/CodeGen/MachineRegisterInfo.h" 00032 #include "llvm/CodeGen/Passes.h" 00033 #include "llvm/Target/TargetRegisterInfo.h" 00034 #include "llvm/Target/TargetInstrInfo.h" 00035 #include "llvm/Target/TargetMachine.h" 00036 #include "llvm/ADT/DepthFirstIterator.h" 00037 #include "llvm/ADT/SmallPtrSet.h" 00038 #include "llvm/ADT/SmallSet.h" 00039 #include "llvm/ADT/STLExtras.h" 00040 #include "llvm/Config/alloca.h" 00041 #include <algorithm> 00042 using namespace llvm; 00043 00044 char LiveVariables::ID = 0; 00045 static RegisterPass<LiveVariables> X("livevars", "Live Variable Analysis"); 00046 00047 00048 void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const { 00049 AU.addRequiredID(UnreachableMachineBlockElimID); 00050 AU.setPreservesAll(); 00051 } 00052 00053 void LiveVariables::VarInfo::dump() const { 00054 cerr << " Alive in blocks: "; 00055 for (int i = AliveBlocks.find_first(); i != -1; i = AliveBlocks.find_next(i)) 00056 cerr << i << ", "; 00057 cerr << " Used in blocks: "; 00058 for (int i = UsedBlocks.find_first(); i != -1; i = UsedBlocks.find_next(i)) 00059 cerr << i << ", "; 00060 cerr << "\n Killed by:"; 00061 if (Kills.empty()) 00062 cerr << " No instructions.\n"; 00063 else { 00064 for (unsigned i = 0, e = Kills.size(); i != e; ++i) 00065 cerr << "\n #" << i << ": " << *Kills[i]; 00066 cerr << "\n"; 00067 } 00068 } 00069 00070 /// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg. 00071 LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) { 00072 assert(TargetRegisterInfo::isVirtualRegister(RegIdx) && 00073 "getVarInfo: not a virtual register!"); 00074 RegIdx -= TargetRegisterInfo::FirstVirtualRegister; 00075 if (RegIdx >= VirtRegInfo.size()) { 00076 if (RegIdx >= 2*VirtRegInfo.size()) 00077 VirtRegInfo.resize(RegIdx*2); 00078 else 00079 VirtRegInfo.resize(2*VirtRegInfo.size()); 00080 } 00081 VarInfo &VI = VirtRegInfo[RegIdx]; 00082 VI.AliveBlocks.resize(MF->getNumBlockIDs()); 00083 VI.UsedBlocks.resize(MF->getNumBlockIDs()); 00084 return VI; 00085 } 00086 00087 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo, 00088 MachineBasicBlock *DefBlock, 00089 MachineBasicBlock *MBB, 00090 std::vector<MachineBasicBlock*> &WorkList) { 00091 unsigned BBNum = MBB->getNumber(); 00092 00093 // Check to see if this basic block is one of the killing blocks. If so, 00094 // remove it. 00095 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) 00096 if (VRInfo.Kills[i]->getParent() == MBB) { 00097 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry 00098 break; 00099 } 00100 00101 if (MBB == DefBlock) return; // Terminate recursion 00102 00103 if (VRInfo.AliveBlocks[BBNum]) 00104 return; // We already know the block is live 00105 00106 // Mark the variable known alive in this bb 00107 VRInfo.AliveBlocks[BBNum] = true; 00108 00109 for (MachineBasicBlock::const_pred_reverse_iterator PI = MBB->pred_rbegin(), 00110 E = MBB->pred_rend(); PI != E; ++PI) 00111 WorkList.push_back(*PI); 00112 } 00113 00114 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo, 00115 MachineBasicBlock *DefBlock, 00116 MachineBasicBlock *MBB) { 00117 std::vector<MachineBasicBlock*> WorkList; 00118 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList); 00119 00120 while (!WorkList.empty()) { 00121 MachineBasicBlock *Pred = WorkList.back(); 00122 WorkList.pop_back(); 00123 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList); 00124 } 00125 } 00126 00127 void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB, 00128 MachineInstr *MI) { 00129 assert(MRI->getVRegDef(reg) && "Register use before def!"); 00130 00131 unsigned BBNum = MBB->getNumber(); 00132 00133 VarInfo& VRInfo = getVarInfo(reg); 00134 VRInfo.UsedBlocks[BBNum] = true; 00135 VRInfo.NumUses++; 00136 00137 // Check to see if this basic block is already a kill block. 00138 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) { 00139 // Yes, this register is killed in this basic block already. Increase the 00140 // live range by updating the kill instruction. 00141 VRInfo.Kills.back() = MI; 00142 return; 00143 } 00144 00145 #ifndef NDEBUG 00146 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) 00147 assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!"); 00148 #endif 00149 00150 // This situation can occur: 00151 // 00152 // ,------. 00153 // | | 00154 // | v 00155 // | t2 = phi ... t1 ... 00156 // | | 00157 // | v 00158 // | t1 = ... 00159 // | ... = ... t1 ... 00160 // | | 00161 // `------' 00162 // 00163 // where there is a use in a PHI node that's a predecessor to the defining 00164 // block. We don't want to mark all predecessors as having the value "alive" 00165 // in this case. 00166 if (MBB == MRI->getVRegDef(reg)->getParent()) return; 00167 00168 // Add a new kill entry for this basic block. If this virtual register is 00169 // already marked as alive in this basic block, that means it is alive in at 00170 // least one of the successor blocks, it's not a kill. 00171 if (!VRInfo.AliveBlocks[BBNum]) 00172 VRInfo.Kills.push_back(MI); 00173 00174 // Update all dominating blocks to mark them as "known live". 00175 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), 00176 E = MBB->pred_end(); PI != E; ++PI) 00177 MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI); 00178 } 00179 00180 void LiveVariables::HandleVirtRegDef(unsigned Reg, MachineInstr *MI) { 00181 VarInfo &VRInfo = getVarInfo(Reg); 00182 00183 if (VRInfo.AliveBlocks.none()) 00184 // If vr is not alive in any block, then defaults to dead. 00185 VRInfo.Kills.push_back(MI); 00186 } 00187 00188 /// FindLastPartialDef - Return the last partial def of the specified register. 00189 /// Also returns the sub-register that's defined. 00190 MachineInstr *LiveVariables::FindLastPartialDef(unsigned Reg, 00191 unsigned &PartDefReg) { 00192 unsigned LastDefReg = 0; 00193 unsigned LastDefDist = 0; 00194 MachineInstr *LastDef = NULL; 00195 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); 00196 unsigned SubReg = *SubRegs; ++SubRegs) { 00197 MachineInstr *Def = PhysRegDef[SubReg]; 00198 if (!Def) 00199 continue; 00200 unsigned Dist = DistanceMap[Def]; 00201 if (Dist > LastDefDist) { 00202 LastDefReg = SubReg; 00203 LastDef = Def; 00204 LastDefDist = Dist; 00205 } 00206 } 00207 PartDefReg = LastDefReg; 00208 return LastDef; 00209 } 00210 00211 /// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add 00212 /// implicit defs to a machine instruction if there was an earlier def of its 00213 /// super-register. 00214 void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) { 00215 // If there was a previous use or a "full" def all is well. 00216 if (!PhysRegDef[Reg] && !PhysRegUse[Reg]) { 00217 // Otherwise, the last sub-register def implicitly defines this register. 00218 // e.g. 00219 // AH = 00220 // AL = ... <imp-def EAX>, <imp-kill AH> 00221 // = AH 00222 // ... 00223 // = EAX 00224 // All of the sub-registers must have been defined before the use of Reg! 00225 unsigned PartDefReg = 0; 00226 MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefReg); 00227 // If LastPartialDef is NULL, it must be using a livein register. 00228 if (LastPartialDef) { 00229 LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/, 00230 true/*IsImp*/)); 00231 PhysRegDef[Reg] = LastPartialDef; 00232 SmallSet<unsigned, 8> Processed; 00233 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); 00234 unsigned SubReg = *SubRegs; ++SubRegs) { 00235 if (Processed.count(SubReg)) 00236 continue; 00237 if (SubReg == PartDefReg || TRI->isSubRegister(PartDefReg, SubReg)) 00238 continue; 00239 // This part of Reg was defined before the last partial def. It's killed 00240 // here. 00241 LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg, 00242 false/*IsDef*/, 00243 true/*IsImp*/)); 00244 PhysRegDef[SubReg] = LastPartialDef; 00245 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS) 00246 Processed.insert(*SS); 00247 } 00248 } 00249 } 00250 00251 // There was an earlier def of a super-register. Add implicit def to that MI. 00252 // 00253 // A: EAX = ... 00254 // B: ... = AX 00255 // 00256 // Add implicit def to A if there isn't a use of AX (or EAX) before B. 00257 if (!PhysRegUse[Reg]) { 00258 MachineInstr *Def = PhysRegDef[Reg]; 00259 if (Def && !Def->modifiesRegister(Reg)) 00260 Def->addOperand(MachineOperand::CreateReg(Reg, 00261 true /*IsDef*/, 00262 true /*IsImp*/)); 00263 } 00264 00265 // Remember this use. 00266 PhysRegUse[Reg] = MI; 00267 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); 00268 unsigned SubReg = *SubRegs; ++SubRegs) 00269 PhysRegUse[SubReg] = MI; 00270 } 00271 00272 /// hasRegisterUseBelow - Return true if the specified register is used after 00273 /// the current instruction and before it's next definition. 00274 bool LiveVariables::hasRegisterUseBelow(unsigned Reg, 00275 MachineBasicBlock::iterator I, 00276 MachineBasicBlock *MBB) { 00277 if (I == MBB->end()) 00278 return false; 00279 00280 // First find out if there are any uses / defs below. 00281 bool hasDistInfo = true; 00282 unsigned CurDist = DistanceMap[I]; 00283 SmallVector<MachineInstr*, 4> Uses; 00284 SmallVector<MachineInstr*, 4> Defs; 00285 for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(Reg), 00286 RE = MRI->reg_end(); RI != RE; ++RI) { 00287 MachineOperand &UDO = RI.getOperand(); 00288 MachineInstr *UDMI = &*RI; 00289 if (UDMI->getParent() != MBB) 00290 continue; 00291 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI); 00292 bool isBelow = false; 00293 if (DI == DistanceMap.end()) { 00294 // Must be below if it hasn't been assigned a distance yet. 00295 isBelow = true; 00296 hasDistInfo = false; 00297 } else if (DI->second > CurDist) 00298 isBelow = true; 00299 if (isBelow) { 00300 if (UDO.isUse()) 00301 Uses.push_back(UDMI); 00302 if (UDO.isDef()) 00303 Defs.push_back(UDMI); 00304 } 00305 } 00306 00307 if (Uses.empty()) 00308 // No uses below. 00309 return false; 00310 else if (!Uses.empty() && Defs.empty()) 00311 // There are uses below but no defs below. 00312 return true; 00313 // There are both uses and defs below. We need to know which comes first. 00314 if (!hasDistInfo) { 00315 // Complete DistanceMap for this MBB. This information is computed only 00316 // once per MBB. 00317 ++I; 00318 ++CurDist; 00319 for (MachineBasicBlock::iterator E = MBB->end(); I != E; ++I, ++CurDist) 00320 DistanceMap.insert(std::make_pair(I, CurDist)); 00321 } 00322 00323 unsigned EarliestUse = DistanceMap[Uses[0]]; 00324 for (unsigned i = 1, e = Uses.size(); i != e; ++i) { 00325 unsigned Dist = DistanceMap[Uses[i]]; 00326 if (Dist < EarliestUse) 00327 EarliestUse = Dist; 00328 } 00329 for (unsigned i = 0, e = Defs.size(); i != e; ++i) { 00330 unsigned Dist = DistanceMap[Defs[i]]; 00331 if (Dist < EarliestUse) 00332 // The register is defined before its first use below. 00333 return false; 00334 } 00335 return true; 00336 } 00337 00338 bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *MI) { 00339 if (!PhysRegUse[Reg] && !PhysRegDef[Reg]) 00340 return false; 00341 00342 MachineInstr *LastRefOrPartRef = PhysRegUse[Reg] 00343 ? PhysRegUse[Reg] : PhysRegDef[Reg]; 00344 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef]; 00345 // The whole register is used. 00346 // AL = 00347 // AH = 00348 // 00349 // = AX 00350 // = AL, AX<imp-use, kill> 00351 // AX = 00352 // 00353 // Or whole register is defined, but not used at all. 00354 // AX<dead> = 00355 // ... 00356 // AX = 00357 // 00358 // Or whole register is defined, but only partly used. 00359 // AX<dead> = AL<imp-def> 00360 // = AL<kill> 00361 // AX = 00362 SmallSet<unsigned, 8> PartUses; 00363 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); 00364 unsigned SubReg = *SubRegs; ++SubRegs) { 00365 if (MachineInstr *Use = PhysRegUse[SubReg]) { 00366 PartUses.insert(SubReg); 00367 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS) 00368 PartUses.insert(*SS); 00369 unsigned Dist = DistanceMap[Use]; 00370 if (Dist > LastRefOrPartRefDist) { 00371 LastRefOrPartRefDist = Dist; 00372 LastRefOrPartRef = Use; 00373 } 00374 } 00375 } 00376 00377 if (LastRefOrPartRef == PhysRegDef[Reg] && LastRefOrPartRef != MI) 00378 // If the last reference is the last def, then it's not used at all. 00379 // That is, unless we are currently processing the last reference itself. 00380 LastRefOrPartRef->addRegisterDead(Reg, TRI, true); 00381 00382 /* Partial uses. Mark register def dead and add implicit def of 00383 sub-registers which are used. 00384 FIXME: LiveIntervalAnalysis can't handle this yet! 00385 EAX<dead> = op AL<imp-def> 00386 That is, EAX def is dead but AL def extends pass it. 00387 Enable this after live interval analysis is fixed to improve codegen! 00388 else if (!PhysRegUse[Reg]) { 00389 PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true); 00390 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); 00391 unsigned SubReg = *SubRegs; ++SubRegs) { 00392 if (PartUses.count(SubReg)) { 00393 PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg, 00394 true, true)); 00395 LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true); 00396 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS) 00397 PartUses.erase(*SS); 00398 } 00399 } 00400 } */ 00401 else 00402 LastRefOrPartRef->addRegisterKilled(Reg, TRI, true); 00403 return true; 00404 } 00405 00406 void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) { 00407 // What parts of the register are previously defined? 00408 SmallSet<unsigned, 32> Live; 00409 if (PhysRegDef[Reg] || PhysRegUse[Reg]) { 00410 Live.insert(Reg); 00411 for (const unsigned *SS = TRI->getSubRegisters(Reg); *SS; ++SS) 00412 Live.insert(*SS); 00413 } else { 00414 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); 00415 unsigned SubReg = *SubRegs; ++SubRegs) { 00416 // If a register isn't itself defined, but all parts that make up of it 00417 // are defined, then consider it also defined. 00418 // e.g. 00419 // AL = 00420 // AH = 00421 // = AX 00422 if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) { 00423 Live.insert(SubReg); 00424 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS) 00425 Live.insert(*SS); 00426 } 00427 } 00428 } 00429 00430 // Start from the largest piece, find the last time any part of the register 00431 // is referenced. 00432 if (!HandlePhysRegKill(Reg, MI)) { 00433 // Only some of the sub-registers are used. 00434 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); 00435 unsigned SubReg = *SubRegs; ++SubRegs) { 00436 if (!Live.count(SubReg)) 00437 // Skip if this sub-register isn't defined. 00438 continue; 00439 if (HandlePhysRegKill(SubReg, MI)) { 00440 Live.erase(SubReg); 00441 for (const unsigned *SS = TRI->getSubRegisters(SubReg); *SS; ++SS) 00442 Live.erase(*SS); 00443 } 00444 } 00445 assert(Live.empty() && "Not all defined registers are killed / dead?"); 00446 } 00447 00448 if (MI) { 00449 // Does this extend the live range of a super-register? 00450 SmallSet<unsigned, 8> Processed; 00451 for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg); 00452 unsigned SuperReg = *SuperRegs; ++SuperRegs) { 00453 if (Processed.count(SuperReg)) 00454 continue; 00455 MachineInstr *LastRef = PhysRegUse[SuperReg] 00456 ? PhysRegUse[SuperReg] : PhysRegDef[SuperReg]; 00457 if (LastRef && LastRef != MI) { 00458 // The larger register is previously defined. Now a smaller part is 00459 // being re-defined. Treat it as read/mod/write if there are uses 00460 // below. 00461 // EAX = 00462 // AX = EAX<imp-use,kill>, EAX<imp-def> 00463 // ... 00464 /// = EAX 00465 if (hasRegisterUseBelow(SuperReg, MI, MI->getParent())) { 00466 MI->addOperand(MachineOperand::CreateReg(SuperReg, false/*IsDef*/, 00467 true/*IsImp*/,true/*IsKill*/)); 00468 MI->addOperand(MachineOperand::CreateReg(SuperReg, true/*IsDef*/, 00469 true/*IsImp*/)); 00470 PhysRegDef[SuperReg] = MI; 00471 PhysRegUse[SuperReg] = NULL; 00472 Processed.insert(SuperReg); 00473 for (const unsigned *SS = TRI->getSubRegisters(SuperReg); *SS; ++SS) { 00474 PhysRegDef[*SS] = MI; 00475 PhysRegUse[*SS] = NULL; 00476 Processed.insert(*SS); 00477 } 00478 } else { 00479 // Otherwise, the super register is killed. 00480 if (HandlePhysRegKill(SuperReg, MI)) { 00481 PhysRegDef[SuperReg] = NULL; 00482 PhysRegUse[SuperReg] = NULL; 00483 for (const unsigned *SS = TRI->getSubRegisters(SuperReg); *SS; ++SS) { 00484 PhysRegDef[*SS] = NULL; 00485 PhysRegUse[*SS] = NULL; 00486 Processed.insert(*SS); 00487 } 00488 } 00489 } 00490 } 00491 } 00492 00493 // Remember this def. 00494 PhysRegDef[Reg] = MI; 00495 PhysRegUse[Reg] = NULL; 00496 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); 00497 unsigned SubReg = *SubRegs; ++SubRegs) { 00498 PhysRegDef[SubReg] = MI; 00499 PhysRegUse[SubReg] = NULL; 00500 } 00501 } 00502 } 00503 00504 bool LiveVariables::runOnMachineFunction(MachineFunction &mf) { 00505 MF = &mf; 00506 MRI = &mf.getRegInfo(); 00507 TRI = MF->getTarget().getRegisterInfo(); 00508 00509 ReservedRegisters = TRI->getReservedRegs(mf); 00510 00511 unsigned NumRegs = TRI->getNumRegs(); 00512 PhysRegDef = new MachineInstr*[NumRegs]; 00513 PhysRegUse = new MachineInstr*[NumRegs]; 00514 PHIVarInfo = new SmallVector<unsigned, 4>[MF->getNumBlockIDs()]; 00515 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0); 00516 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0); 00517 00518 /// Get some space for a respectable number of registers. 00519 VirtRegInfo.resize(64); 00520 00521 analyzePHINodes(mf); 00522 00523 // Calculate live variable information in depth first order on the CFG of the 00524 // function. This guarantees that we will see the definition of a virtual 00525 // register before its uses due to dominance properties of SSA (except for PHI 00526 // nodes, which are treated as a special case). 00527 MachineBasicBlock *Entry = MF->begin(); 00528 SmallPtrSet<MachineBasicBlock*,16> Visited; 00529 00530 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> > 00531 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited); 00532 DFI != E; ++DFI) { 00533 MachineBasicBlock *MBB = *DFI; 00534 00535 // Mark live-in registers as live-in. 00536 for (MachineBasicBlock::const_livein_iterator II = MBB->livein_begin(), 00537 EE = MBB->livein_end(); II != EE; ++II) { 00538 assert(TargetRegisterInfo::isPhysicalRegister(*II) && 00539 "Cannot have a live-in virtual register!"); 00540 HandlePhysRegDef(*II, 0); 00541 } 00542 00543 // Loop over all of the instructions, processing them. 00544 DistanceMap.clear(); 00545 unsigned Dist = 0; 00546 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); 00547 I != E; ++I) { 00548 MachineInstr *MI = I; 00549 DistanceMap.insert(std::make_pair(MI, Dist++)); 00550 00551 // Process all of the operands of the instruction... 00552 unsigned NumOperandsToProcess = MI->getNumOperands(); 00553 00554 // Unless it is a PHI node. In this case, ONLY process the DEF, not any 00555 // of the uses. They will be handled in other basic blocks. 00556 if (MI->getOpcode() == TargetInstrInfo::PHI) 00557 NumOperandsToProcess = 1; 00558 00559 SmallVector<unsigned, 4> UseRegs; 00560 SmallVector<unsigned, 4> DefRegs; 00561 for (unsigned i = 0; i != NumOperandsToProcess; ++i) { 00562 const MachineOperand &MO = MI->getOperand(i); 00563 if (!MO.isReg() || MO.getReg() == 0) 00564 continue; 00565 unsigned MOReg = MO.getReg(); 00566 if (MO.isUse()) 00567 UseRegs.push_back(MOReg); 00568 if (MO.isDef()) 00569 DefRegs.push_back(MOReg); 00570 } 00571 00572 // Process all uses. 00573 for (unsigned i = 0, e = UseRegs.size(); i != e; ++i) { 00574 unsigned MOReg = UseRegs[i]; 00575 if (TargetRegisterInfo::isVirtualRegister(MOReg)) 00576 HandleVirtRegUse(MOReg, MBB, MI); 00577 else if (!ReservedRegisters[MOReg]) 00578 HandlePhysRegUse(MOReg, MI); 00579 } 00580 00581 // Process all defs. 00582 for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) { 00583 unsigned MOReg = DefRegs[i]; 00584 if (TargetRegisterInfo::isVirtualRegister(MOReg)) 00585 HandleVirtRegDef(MOReg, MI); 00586 else if (!ReservedRegisters[MOReg]) 00587 HandlePhysRegDef(MOReg, MI); 00588 } 00589 } 00590 00591 // Handle any virtual assignments from PHI nodes which might be at the 00592 // bottom of this basic block. We check all of our successor blocks to see 00593 // if they have PHI nodes, and if so, we simulate an assignment at the end 00594 // of the current block. 00595 if (!PHIVarInfo[MBB->getNumber()].empty()) { 00596 SmallVector<unsigned, 4>& VarInfoVec = PHIVarInfo[MBB->getNumber()]; 00597 00598 for (SmallVector<unsigned, 4>::iterator I = VarInfoVec.begin(), 00599 E = VarInfoVec.end(); I != E; ++I) 00600 // Mark it alive only in the block we are representing. 00601 MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(), 00602 MBB); 00603 } 00604 00605 // Finally, if the last instruction in the block is a return, make sure to 00606 // mark it as using all of the live-out values in the function. 00607 if (!MBB->empty() && MBB->back().getDesc().isReturn()) { 00608 MachineInstr *Ret = &MBB->back(); 00609 00610 for (MachineRegisterInfo::liveout_iterator 00611 I = MF->getRegInfo().liveout_begin(), 00612 E = MF->getRegInfo().liveout_end(); I != E; ++I) { 00613 assert(TargetRegisterInfo::isPhysicalRegister(*I) && 00614 "Cannot have a live-out virtual register!"); 00615 HandlePhysRegUse(*I, Ret); 00616 00617 // Add live-out registers as implicit uses. 00618 if (!Ret->readsRegister(*I)) 00619 Ret->addOperand(MachineOperand::CreateReg(*I, false, true)); 00620 } 00621 } 00622 00623 // Loop over PhysRegDef / PhysRegUse, killing any registers that are 00624 // available at the end of the basic block. 00625 for (unsigned i = 0; i != NumRegs; ++i) 00626 if (PhysRegDef[i] || PhysRegUse[i]) 00627 HandlePhysRegDef(i, 0); 00628 00629 std::fill(PhysRegDef, PhysRegDef + NumRegs, (MachineInstr*)0); 00630 std::fill(PhysRegUse, PhysRegUse + NumRegs, (MachineInstr*)0); 00631 } 00632 00633 // Convert and transfer the dead / killed information we have gathered into 00634 // VirtRegInfo onto MI's. 00635 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) 00636 for (unsigned j = 0, e2 = VirtRegInfo[i].Kills.size(); j != e2; ++j) 00637 if (VirtRegInfo[i].Kills[j] == 00638 MRI->getVRegDef(i + TargetRegisterInfo::FirstVirtualRegister)) 00639 VirtRegInfo[i] 00640 .Kills[j]->addRegisterDead(i + 00641 TargetRegisterInfo::FirstVirtualRegister, 00642 TRI); 00643 else 00644 VirtRegInfo[i] 00645 .Kills[j]->addRegisterKilled(i + 00646 TargetRegisterInfo::FirstVirtualRegister, 00647 TRI); 00648 00649 // Check to make sure there are no unreachable blocks in the MC CFG for the 00650 // function. If so, it is due to a bug in the instruction selector or some 00651 // other part of the code generator if this happens. 00652 #ifndef NDEBUG 00653 for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i) 00654 assert(Visited.count(&*i) != 0 && "unreachable basic block found"); 00655 #endif 00656 00657 delete[] PhysRegDef; 00658 delete[] PhysRegUse; 00659 delete[] PHIVarInfo; 00660 00661 return false; 00662 } 00663 00664 /// replaceKillInstruction - Update register kill info by replacing a kill 00665 /// instruction with a new one. 00666 void LiveVariables::replaceKillInstruction(unsigned Reg, MachineInstr *OldMI, 00667 MachineInstr *NewMI) { 00668 VarInfo &VI = getVarInfo(Reg); 00669 std::replace(VI.Kills.begin(), VI.Kills.end(), OldMI, NewMI); 00670 } 00671 00672 /// removeVirtualRegistersKilled - Remove all killed info for the specified 00673 /// instruction. 00674 void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) { 00675 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00676 MachineOperand &MO = MI->getOperand(i); 00677 if (MO.isReg() && MO.isKill()) { 00678 MO.setIsKill(false); 00679 unsigned Reg = MO.getReg(); 00680 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 00681 bool removed = getVarInfo(Reg).removeKill(MI); 00682 assert(removed && "kill not in register's VarInfo?"); 00683 removed = true; 00684 } 00685 } 00686 } 00687 } 00688 00689 /// analyzePHINodes - Gather information about the PHI nodes in here. In 00690 /// particular, we want to map the variable information of a virtual register 00691 /// which is used in a PHI node. We map that to the BB the vreg is coming from. 00692 /// 00693 void LiveVariables::analyzePHINodes(const MachineFunction& Fn) { 00694 for (MachineFunction::const_iterator I = Fn.begin(), E = Fn.end(); 00695 I != E; ++I) 00696 for (MachineBasicBlock::const_iterator BBI = I->begin(), BBE = I->end(); 00697 BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) 00698 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) 00699 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()] 00700 .push_back(BBI->getOperand(i).getReg()); 00701 }