LLVM API Documentation
00001 //===- LiveDebugVariables.cpp - Tracking debug info variables -------------===// 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 LiveDebugVariables analysis. 00011 // 00012 // Remove all DBG_VALUE instructions referencing virtual registers and replace 00013 // them with a data structure tracking where live user variables are kept - in a 00014 // virtual register or in a stack slot. 00015 // 00016 // Allow the data structure to be updated during register allocation when values 00017 // are moved between registers and stack slots. Finally emit new DBG_VALUE 00018 // instructions after register allocation is complete. 00019 // 00020 //===----------------------------------------------------------------------===// 00021 00022 #define DEBUG_TYPE "livedebug" 00023 #include "LiveDebugVariables.h" 00024 #include "llvm/ADT/IntervalMap.h" 00025 #include "llvm/ADT/Statistic.h" 00026 #include "llvm/CodeGen/LexicalScopes.h" 00027 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 00028 #include "llvm/CodeGen/MachineDominators.h" 00029 #include "llvm/CodeGen/MachineFunction.h" 00030 #include "llvm/CodeGen/MachineInstrBuilder.h" 00031 #include "llvm/CodeGen/MachineRegisterInfo.h" 00032 #include "llvm/CodeGen/Passes.h" 00033 #include "llvm/CodeGen/VirtRegMap.h" 00034 #include "llvm/DebugInfo.h" 00035 #include "llvm/IR/Constants.h" 00036 #include "llvm/IR/Metadata.h" 00037 #include "llvm/IR/Value.h" 00038 #include "llvm/Support/CommandLine.h" 00039 #include "llvm/Support/Debug.h" 00040 #include "llvm/Target/TargetInstrInfo.h" 00041 #include "llvm/Target/TargetMachine.h" 00042 #include "llvm/Target/TargetRegisterInfo.h" 00043 00044 using namespace llvm; 00045 00046 static cl::opt<bool> 00047 EnableLDV("live-debug-variables", cl::init(true), 00048 cl::desc("Enable the live debug variables pass"), cl::Hidden); 00049 00050 STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted"); 00051 char LiveDebugVariables::ID = 0; 00052 00053 INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars", 00054 "Debug Variable Analysis", false, false) 00055 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 00056 INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 00057 INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars", 00058 "Debug Variable Analysis", false, false) 00059 00060 void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const { 00061 AU.addRequired<MachineDominatorTree>(); 00062 AU.addRequiredTransitive<LiveIntervals>(); 00063 AU.setPreservesAll(); 00064 MachineFunctionPass::getAnalysisUsage(AU); 00065 } 00066 00067 LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(0) { 00068 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 00069 } 00070 00071 /// LocMap - Map of where a user value is live, and its location. 00072 typedef IntervalMap<SlotIndex, unsigned, 4> LocMap; 00073 00074 namespace { 00075 /// UserValueScopes - Keeps track of lexical scopes associated with an 00076 /// user value's source location. 00077 class UserValueScopes { 00078 DebugLoc DL; 00079 LexicalScopes &LS; 00080 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks; 00081 00082 public: 00083 UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(D), LS(L) {} 00084 00085 /// dominates - Return true if current scope dominates at least one machine 00086 /// instruction in a given machine basic block. 00087 bool dominates(MachineBasicBlock *MBB) { 00088 if (LBlocks.empty()) 00089 LS.getMachineBasicBlocks(DL, LBlocks); 00090 if (LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB)) 00091 return true; 00092 return false; 00093 } 00094 }; 00095 } // end anonymous namespace 00096 00097 /// UserValue - A user value is a part of a debug info user variable. 00098 /// 00099 /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register 00100 /// holds part of a user variable. The part is identified by a byte offset. 00101 /// 00102 /// UserValues are grouped into equivalence classes for easier searching. Two 00103 /// user values are related if they refer to the same variable, or if they are 00104 /// held by the same virtual register. The equivalence class is the transitive 00105 /// closure of that relation. 00106 namespace { 00107 class LDVImpl; 00108 class UserValue { 00109 const MDNode *variable; ///< The debug info variable we are part of. 00110 unsigned offset; ///< Byte offset into variable. 00111 DebugLoc dl; ///< The debug location for the variable. This is 00112 ///< used by dwarf writer to find lexical scope. 00113 UserValue *leader; ///< Equivalence class leader. 00114 UserValue *next; ///< Next value in equivalence class, or null. 00115 00116 /// Numbered locations referenced by locmap. 00117 SmallVector<MachineOperand, 4> locations; 00118 00119 /// Map of slot indices where this value is live. 00120 LocMap locInts; 00121 00122 /// coalesceLocation - After LocNo was changed, check if it has become 00123 /// identical to another location, and coalesce them. This may cause LocNo or 00124 /// a later location to be erased, but no earlier location will be erased. 00125 void coalesceLocation(unsigned LocNo); 00126 00127 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo. 00128 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo, 00129 LiveIntervals &LIS, const TargetInstrInfo &TII); 00130 00131 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs 00132 /// is live. Returns true if any changes were made. 00133 bool splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs); 00134 00135 public: 00136 /// UserValue - Create a new UserValue. 00137 UserValue(const MDNode *var, unsigned o, DebugLoc L, 00138 LocMap::Allocator &alloc) 00139 : variable(var), offset(o), dl(L), leader(this), next(0), locInts(alloc) 00140 {} 00141 00142 /// getLeader - Get the leader of this value's equivalence class. 00143 UserValue *getLeader() { 00144 UserValue *l = leader; 00145 while (l != l->leader) 00146 l = l->leader; 00147 return leader = l; 00148 } 00149 00150 /// getNext - Return the next UserValue in the equivalence class. 00151 UserValue *getNext() const { return next; } 00152 00153 /// match - Does this UserValue match the parameters? 00154 bool match(const MDNode *Var, unsigned Offset) const { 00155 return Var == variable && Offset == offset; 00156 } 00157 00158 /// merge - Merge equivalence classes. 00159 static UserValue *merge(UserValue *L1, UserValue *L2) { 00160 L2 = L2->getLeader(); 00161 if (!L1) 00162 return L2; 00163 L1 = L1->getLeader(); 00164 if (L1 == L2) 00165 return L1; 00166 // Splice L2 before L1's members. 00167 UserValue *End = L2; 00168 while (End->next) 00169 End->leader = L1, End = End->next; 00170 End->leader = L1; 00171 End->next = L1->next; 00172 L1->next = L2; 00173 return L1; 00174 } 00175 00176 /// getLocationNo - Return the location number that matches Loc. 00177 unsigned getLocationNo(const MachineOperand &LocMO) { 00178 if (LocMO.isReg()) { 00179 if (LocMO.getReg() == 0) 00180 return ~0u; 00181 // For register locations we dont care about use/def and other flags. 00182 for (unsigned i = 0, e = locations.size(); i != e; ++i) 00183 if (locations[i].isReg() && 00184 locations[i].getReg() == LocMO.getReg() && 00185 locations[i].getSubReg() == LocMO.getSubReg()) 00186 return i; 00187 } else 00188 for (unsigned i = 0, e = locations.size(); i != e; ++i) 00189 if (LocMO.isIdenticalTo(locations[i])) 00190 return i; 00191 locations.push_back(LocMO); 00192 // We are storing a MachineOperand outside a MachineInstr. 00193 locations.back().clearParent(); 00194 // Don't store def operands. 00195 if (locations.back().isReg()) 00196 locations.back().setIsUse(); 00197 return locations.size() - 1; 00198 } 00199 00200 /// mapVirtRegs - Ensure that all virtual register locations are mapped. 00201 void mapVirtRegs(LDVImpl *LDV); 00202 00203 /// addDef - Add a definition point to this value. 00204 void addDef(SlotIndex Idx, const MachineOperand &LocMO) { 00205 // Add a singular (Idx,Idx) -> Loc mapping. 00206 LocMap::iterator I = locInts.find(Idx); 00207 if (!I.valid() || I.start() != Idx) 00208 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO)); 00209 else 00210 // A later DBG_VALUE at the same SlotIndex overrides the old location. 00211 I.setValue(getLocationNo(LocMO)); 00212 } 00213 00214 /// extendDef - Extend the current definition as far as possible down the 00215 /// dominator tree. Stop when meeting an existing def or when leaving the live 00216 /// range of VNI. 00217 /// End points where VNI is no longer live are added to Kills. 00218 /// @param Idx Starting point for the definition. 00219 /// @param LocNo Location number to propagate. 00220 /// @param LI Restrict liveness to where LI has the value VNI. May be null. 00221 /// @param VNI When LI is not null, this is the value to restrict to. 00222 /// @param Kills Append end points of VNI's live range to Kills. 00223 /// @param LIS Live intervals analysis. 00224 /// @param MDT Dominator tree. 00225 void extendDef(SlotIndex Idx, unsigned LocNo, 00226 LiveInterval *LI, const VNInfo *VNI, 00227 SmallVectorImpl<SlotIndex> *Kills, 00228 LiveIntervals &LIS, MachineDominatorTree &MDT, 00229 UserValueScopes &UVS); 00230 00231 /// addDefsFromCopies - The value in LI/LocNo may be copies to other 00232 /// registers. Determine if any of the copies are available at the kill 00233 /// points, and add defs if possible. 00234 /// @param LI Scan for copies of the value in LI->reg. 00235 /// @param LocNo Location number of LI->reg. 00236 /// @param Kills Points where the range of LocNo could be extended. 00237 /// @param NewDefs Append (Idx, LocNo) of inserted defs here. 00238 void addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 00239 const SmallVectorImpl<SlotIndex> &Kills, 00240 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 00241 MachineRegisterInfo &MRI, 00242 LiveIntervals &LIS); 00243 00244 /// computeIntervals - Compute the live intervals of all locations after 00245 /// collecting all their def points. 00246 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 00247 LiveIntervals &LIS, MachineDominatorTree &MDT, 00248 UserValueScopes &UVS); 00249 00250 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is 00251 /// live. Returns true if any changes were made. 00252 bool splitRegister(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs); 00253 00254 /// rewriteLocations - Rewrite virtual register locations according to the 00255 /// provided virtual register map. 00256 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI); 00257 00258 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 00259 void emitDebugValues(VirtRegMap *VRM, 00260 LiveIntervals &LIS, const TargetInstrInfo &TRI); 00261 00262 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A 00263 /// variable may have more than one corresponding DBG_VALUE instructions. 00264 /// Only first one needs DebugLoc to identify variable's lexical scope 00265 /// in source file. 00266 DebugLoc findDebugLoc(); 00267 00268 /// getDebugLoc - Return DebugLoc of this UserValue. 00269 DebugLoc getDebugLoc() { return dl;} 00270 void print(raw_ostream&, const TargetMachine*); 00271 }; 00272 } // namespace 00273 00274 /// LDVImpl - Implementation of the LiveDebugVariables pass. 00275 namespace { 00276 class LDVImpl { 00277 LiveDebugVariables &pass; 00278 LocMap::Allocator allocator; 00279 MachineFunction *MF; 00280 LiveIntervals *LIS; 00281 LexicalScopes LS; 00282 MachineDominatorTree *MDT; 00283 const TargetRegisterInfo *TRI; 00284 00285 /// Whether emitDebugValues is called. 00286 bool EmitDone; 00287 /// Whether the machine function is modified during the pass. 00288 bool ModifiedMF; 00289 00290 /// userValues - All allocated UserValue instances. 00291 SmallVector<UserValue*, 8> userValues; 00292 00293 /// Map virtual register to eq class leader. 00294 typedef DenseMap<unsigned, UserValue*> VRMap; 00295 VRMap virtRegToEqClass; 00296 00297 /// Map user variable to eq class leader. 00298 typedef DenseMap<const MDNode *, UserValue*> UVMap; 00299 UVMap userVarMap; 00300 00301 /// getUserValue - Find or create a UserValue. 00302 UserValue *getUserValue(const MDNode *Var, unsigned Offset, DebugLoc DL); 00303 00304 /// lookupVirtReg - Find the EC leader for VirtReg or null. 00305 UserValue *lookupVirtReg(unsigned VirtReg); 00306 00307 /// handleDebugValue - Add DBG_VALUE instruction to our maps. 00308 /// @param MI DBG_VALUE instruction 00309 /// @param Idx Last valid SLotIndex before instruction. 00310 /// @return True if the DBG_VALUE instruction should be deleted. 00311 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx); 00312 00313 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding 00314 /// a UserValue def for each instruction. 00315 /// @param mf MachineFunction to be scanned. 00316 /// @return True if any debug values were found. 00317 bool collectDebugValues(MachineFunction &mf); 00318 00319 /// computeIntervals - Compute the live intervals of all user values after 00320 /// collecting all their def points. 00321 void computeIntervals(); 00322 00323 public: 00324 LDVImpl(LiveDebugVariables *ps) : pass(*ps), EmitDone(false), 00325 ModifiedMF(false) {} 00326 bool runOnMachineFunction(MachineFunction &mf); 00327 00328 /// clear - Release all memory. 00329 void clear() { 00330 DeleteContainerPointers(userValues); 00331 userValues.clear(); 00332 virtRegToEqClass.clear(); 00333 userVarMap.clear(); 00334 // Make sure we call emitDebugValues if the machine function was modified. 00335 assert((!ModifiedMF || EmitDone) && 00336 "Dbg values are not emitted in LDV"); 00337 EmitDone = false; 00338 ModifiedMF = false; 00339 } 00340 00341 /// mapVirtReg - Map virtual register to an equivalence class. 00342 void mapVirtReg(unsigned VirtReg, UserValue *EC); 00343 00344 /// splitRegister - Replace all references to OldReg with NewRegs. 00345 void splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs); 00346 00347 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 00348 void emitDebugValues(VirtRegMap *VRM); 00349 00350 void print(raw_ostream&); 00351 }; 00352 } // namespace 00353 00354 void UserValue::print(raw_ostream &OS, const TargetMachine *TM) { 00355 DIVariable DV(variable); 00356 OS << "!\""; 00357 DV.printExtendedName(OS); 00358 OS << "\"\t"; 00359 if (offset) 00360 OS << '+' << offset; 00361 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { 00362 OS << " [" << I.start() << ';' << I.stop() << "):"; 00363 if (I.value() == ~0u) 00364 OS << "undef"; 00365 else 00366 OS << I.value(); 00367 } 00368 for (unsigned i = 0, e = locations.size(); i != e; ++i) { 00369 OS << " Loc" << i << '='; 00370 locations[i].print(OS, TM); 00371 } 00372 OS << '\n'; 00373 } 00374 00375 void LDVImpl::print(raw_ostream &OS) { 00376 OS << "********** DEBUG VARIABLES **********\n"; 00377 for (unsigned i = 0, e = userValues.size(); i != e; ++i) 00378 userValues[i]->print(OS, &MF->getTarget()); 00379 } 00380 00381 void UserValue::coalesceLocation(unsigned LocNo) { 00382 unsigned KeepLoc = 0; 00383 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) { 00384 if (KeepLoc == LocNo) 00385 continue; 00386 if (locations[KeepLoc].isIdenticalTo(locations[LocNo])) 00387 break; 00388 } 00389 // No matches. 00390 if (KeepLoc == locations.size()) 00391 return; 00392 00393 // Keep the smaller location, erase the larger one. 00394 unsigned EraseLoc = LocNo; 00395 if (KeepLoc > EraseLoc) 00396 std::swap(KeepLoc, EraseLoc); 00397 locations.erase(locations.begin() + EraseLoc); 00398 00399 // Rewrite values. 00400 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { 00401 unsigned v = I.value(); 00402 if (v == EraseLoc) 00403 I.setValue(KeepLoc); // Coalesce when possible. 00404 else if (v > EraseLoc) 00405 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values. 00406 } 00407 } 00408 00409 void UserValue::mapVirtRegs(LDVImpl *LDV) { 00410 for (unsigned i = 0, e = locations.size(); i != e; ++i) 00411 if (locations[i].isReg() && 00412 TargetRegisterInfo::isVirtualRegister(locations[i].getReg())) 00413 LDV->mapVirtReg(locations[i].getReg(), this); 00414 } 00415 00416 UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset, 00417 DebugLoc DL) { 00418 UserValue *&Leader = userVarMap[Var]; 00419 if (Leader) { 00420 UserValue *UV = Leader->getLeader(); 00421 Leader = UV; 00422 for (; UV; UV = UV->getNext()) 00423 if (UV->match(Var, Offset)) 00424 return UV; 00425 } 00426 00427 UserValue *UV = new UserValue(Var, Offset, DL, allocator); 00428 userValues.push_back(UV); 00429 Leader = UserValue::merge(Leader, UV); 00430 return UV; 00431 } 00432 00433 void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) { 00434 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs"); 00435 UserValue *&Leader = virtRegToEqClass[VirtReg]; 00436 Leader = UserValue::merge(Leader, EC); 00437 } 00438 00439 UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) { 00440 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg)) 00441 return UV->getLeader(); 00442 return 0; 00443 } 00444 00445 bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) { 00446 // DBG_VALUE loc, offset, variable 00447 if (MI->getNumOperands() != 3 || 00448 !MI->getOperand(1).isImm() || !MI->getOperand(2).isMetadata()) { 00449 DEBUG(dbgs() << "Can't handle " << *MI); 00450 return false; 00451 } 00452 00453 // Get or create the UserValue for (variable,offset). 00454 unsigned Offset = MI->getOperand(1).getImm(); 00455 const MDNode *Var = MI->getOperand(2).getMetadata(); 00456 UserValue *UV = getUserValue(Var, Offset, MI->getDebugLoc()); 00457 UV->addDef(Idx, MI->getOperand(0)); 00458 return true; 00459 } 00460 00461 bool LDVImpl::collectDebugValues(MachineFunction &mf) { 00462 bool Changed = false; 00463 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE; 00464 ++MFI) { 00465 MachineBasicBlock *MBB = MFI; 00466 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); 00467 MBBI != MBBE;) { 00468 if (!MBBI->isDebugValue()) { 00469 ++MBBI; 00470 continue; 00471 } 00472 // DBG_VALUE has no slot index, use the previous instruction instead. 00473 SlotIndex Idx = MBBI == MBB->begin() ? 00474 LIS->getMBBStartIdx(MBB) : 00475 LIS->getInstructionIndex(llvm::prior(MBBI)).getRegSlot(); 00476 // Handle consecutive DBG_VALUE instructions with the same slot index. 00477 do { 00478 if (handleDebugValue(MBBI, Idx)) { 00479 MBBI = MBB->erase(MBBI); 00480 Changed = true; 00481 } else 00482 ++MBBI; 00483 } while (MBBI != MBBE && MBBI->isDebugValue()); 00484 } 00485 } 00486 return Changed; 00487 } 00488 00489 void UserValue::extendDef(SlotIndex Idx, unsigned LocNo, 00490 LiveInterval *LI, const VNInfo *VNI, 00491 SmallVectorImpl<SlotIndex> *Kills, 00492 LiveIntervals &LIS, MachineDominatorTree &MDT, 00493 UserValueScopes &UVS) { 00494 SmallVector<SlotIndex, 16> Todo; 00495 Todo.push_back(Idx); 00496 do { 00497 SlotIndex Start = Todo.pop_back_val(); 00498 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start); 00499 SlotIndex Stop = LIS.getMBBEndIdx(MBB); 00500 LocMap::iterator I = locInts.find(Start); 00501 00502 // Limit to VNI's live range. 00503 bool ToEnd = true; 00504 if (LI && VNI) { 00505 LiveRange *Range = LI->getLiveRangeContaining(Start); 00506 if (!Range || Range->valno != VNI) { 00507 if (Kills) 00508 Kills->push_back(Start); 00509 continue; 00510 } 00511 if (Range->end < Stop) 00512 Stop = Range->end, ToEnd = false; 00513 } 00514 00515 // There could already be a short def at Start. 00516 if (I.valid() && I.start() <= Start) { 00517 // Stop when meeting a different location or an already extended interval. 00518 Start = Start.getNextSlot(); 00519 if (I.value() != LocNo || I.stop() != Start) 00520 continue; 00521 // This is a one-slot placeholder. Just skip it. 00522 ++I; 00523 } 00524 00525 // Limited by the next def. 00526 if (I.valid() && I.start() < Stop) 00527 Stop = I.start(), ToEnd = false; 00528 // Limited by VNI's live range. 00529 else if (!ToEnd && Kills) 00530 Kills->push_back(Stop); 00531 00532 if (Start >= Stop) 00533 continue; 00534 00535 I.insert(Start, Stop, LocNo); 00536 00537 // If we extended to the MBB end, propagate down the dominator tree. 00538 if (!ToEnd) 00539 continue; 00540 const std::vector<MachineDomTreeNode*> &Children = 00541 MDT.getNode(MBB)->getChildren(); 00542 for (unsigned i = 0, e = Children.size(); i != e; ++i) { 00543 MachineBasicBlock *MBB = Children[i]->getBlock(); 00544 if (UVS.dominates(MBB)) 00545 Todo.push_back(LIS.getMBBStartIdx(MBB)); 00546 } 00547 } while (!Todo.empty()); 00548 } 00549 00550 void 00551 UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 00552 const SmallVectorImpl<SlotIndex> &Kills, 00553 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 00554 MachineRegisterInfo &MRI, LiveIntervals &LIS) { 00555 if (Kills.empty()) 00556 return; 00557 // Don't track copies from physregs, there are too many uses. 00558 if (!TargetRegisterInfo::isVirtualRegister(LI->reg)) 00559 return; 00560 00561 // Collect all the (vreg, valno) pairs that are copies of LI. 00562 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues; 00563 for (MachineRegisterInfo::use_nodbg_iterator 00564 UI = MRI.use_nodbg_begin(LI->reg), 00565 UE = MRI.use_nodbg_end(); UI != UE; ++UI) { 00566 // Copies of the full value. 00567 if (UI.getOperand().getSubReg() || !UI->isCopy()) 00568 continue; 00569 MachineInstr *MI = &*UI; 00570 unsigned DstReg = MI->getOperand(0).getReg(); 00571 00572 // Don't follow copies to physregs. These are usually setting up call 00573 // arguments, and the argument registers are always call clobbered. We are 00574 // better off in the source register which could be a callee-saved register, 00575 // or it could be spilled. 00576 if (!TargetRegisterInfo::isVirtualRegister(DstReg)) 00577 continue; 00578 00579 // Is LocNo extended to reach this copy? If not, another def may be blocking 00580 // it, or we are looking at a wrong value of LI. 00581 SlotIndex Idx = LIS.getInstructionIndex(MI); 00582 LocMap::iterator I = locInts.find(Idx.getRegSlot(true)); 00583 if (!I.valid() || I.value() != LocNo) 00584 continue; 00585 00586 if (!LIS.hasInterval(DstReg)) 00587 continue; 00588 LiveInterval *DstLI = &LIS.getInterval(DstReg); 00589 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot()); 00590 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value"); 00591 CopyValues.push_back(std::make_pair(DstLI, DstVNI)); 00592 } 00593 00594 if (CopyValues.empty()) 00595 return; 00596 00597 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n'); 00598 00599 // Try to add defs of the copied values for each kill point. 00600 for (unsigned i = 0, e = Kills.size(); i != e; ++i) { 00601 SlotIndex Idx = Kills[i]; 00602 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) { 00603 LiveInterval *DstLI = CopyValues[j].first; 00604 const VNInfo *DstVNI = CopyValues[j].second; 00605 if (DstLI->getVNInfoAt(Idx) != DstVNI) 00606 continue; 00607 // Check that there isn't already a def at Idx 00608 LocMap::iterator I = locInts.find(Idx); 00609 if (I.valid() && I.start() <= Idx) 00610 continue; 00611 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" 00612 << DstVNI->id << " in " << *DstLI << '\n'); 00613 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); 00614 assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); 00615 unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); 00616 I.insert(Idx, Idx.getNextSlot(), LocNo); 00617 NewDefs.push_back(std::make_pair(Idx, LocNo)); 00618 break; 00619 } 00620 } 00621 } 00622 00623 void 00624 UserValue::computeIntervals(MachineRegisterInfo &MRI, 00625 const TargetRegisterInfo &TRI, 00626 LiveIntervals &LIS, 00627 MachineDominatorTree &MDT, 00628 UserValueScopes &UVS) { 00629 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs; 00630 00631 // Collect all defs to be extended (Skipping undefs). 00632 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) 00633 if (I.value() != ~0u) 00634 Defs.push_back(std::make_pair(I.start(), I.value())); 00635 00636 // Extend all defs, and possibly add new ones along the way. 00637 for (unsigned i = 0; i != Defs.size(); ++i) { 00638 SlotIndex Idx = Defs[i].first; 00639 unsigned LocNo = Defs[i].second; 00640 const MachineOperand &Loc = locations[LocNo]; 00641 00642 if (!Loc.isReg()) { 00643 extendDef(Idx, LocNo, 0, 0, 0, LIS, MDT, UVS); 00644 continue; 00645 } 00646 00647 // Register locations are constrained to where the register value is live. 00648 if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) { 00649 LiveInterval *LI = 0; 00650 const VNInfo *VNI = 0; 00651 if (LIS.hasInterval(Loc.getReg())) { 00652 LI = &LIS.getInterval(Loc.getReg()); 00653 VNI = LI->getVNInfoAt(Idx); 00654 } 00655 SmallVector<SlotIndex, 16> Kills; 00656 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS); 00657 if (LI) 00658 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS); 00659 continue; 00660 } 00661 00662 // For physregs, use the live range of the first regunit as a guide. 00663 unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI); 00664 LiveInterval *LI = &LIS.getRegUnit(Unit); 00665 const VNInfo *VNI = LI->getVNInfoAt(Idx); 00666 // Don't track copies from physregs, it is too expensive. 00667 extendDef(Idx, LocNo, LI, VNI, 0, LIS, MDT, UVS); 00668 } 00669 00670 // Finally, erase all the undefs. 00671 for (LocMap::iterator I = locInts.begin(); I.valid();) 00672 if (I.value() == ~0u) 00673 I.erase(); 00674 else 00675 ++I; 00676 } 00677 00678 void LDVImpl::computeIntervals() { 00679 for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 00680 UserValueScopes UVS(userValues[i]->getDebugLoc(), LS); 00681 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS); 00682 userValues[i]->mapVirtRegs(this); 00683 } 00684 } 00685 00686 bool LDVImpl::runOnMachineFunction(MachineFunction &mf) { 00687 MF = &mf; 00688 LIS = &pass.getAnalysis<LiveIntervals>(); 00689 MDT = &pass.getAnalysis<MachineDominatorTree>(); 00690 TRI = mf.getTarget().getRegisterInfo(); 00691 clear(); 00692 LS.initialize(mf); 00693 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " 00694 << mf.getName() << " **********\n"); 00695 00696 bool Changed = collectDebugValues(mf); 00697 computeIntervals(); 00698 DEBUG(print(dbgs())); 00699 LS.releaseMemory(); 00700 ModifiedMF = Changed; 00701 return Changed; 00702 } 00703 00704 bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) { 00705 if (!EnableLDV) 00706 return false; 00707 if (!pImpl) 00708 pImpl = new LDVImpl(this); 00709 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf); 00710 } 00711 00712 void LiveDebugVariables::releaseMemory() { 00713 if (pImpl) 00714 static_cast<LDVImpl*>(pImpl)->clear(); 00715 } 00716 00717 LiveDebugVariables::~LiveDebugVariables() { 00718 if (pImpl) 00719 delete static_cast<LDVImpl*>(pImpl); 00720 } 00721 00722 //===----------------------------------------------------------------------===// 00723 // Live Range Splitting 00724 //===----------------------------------------------------------------------===// 00725 00726 bool 00727 UserValue::splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs) { 00728 DEBUG({ 00729 dbgs() << "Splitting Loc" << OldLocNo << '\t'; 00730 print(dbgs(), 0); 00731 }); 00732 bool DidChange = false; 00733 LocMap::iterator LocMapI; 00734 LocMapI.setMap(locInts); 00735 for (unsigned i = 0; i != NewRegs.size(); ++i) { 00736 LiveInterval *LI = NewRegs[i]; 00737 if (LI->empty()) 00738 continue; 00739 00740 // Don't allocate the new LocNo until it is needed. 00741 unsigned NewLocNo = ~0u; 00742 00743 // Iterate over the overlaps between locInts and LI. 00744 LocMapI.find(LI->beginIndex()); 00745 if (!LocMapI.valid()) 00746 continue; 00747 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start()); 00748 LiveInterval::iterator LIE = LI->end(); 00749 while (LocMapI.valid() && LII != LIE) { 00750 // At this point, we know that LocMapI.stop() > LII->start. 00751 LII = LI->advanceTo(LII, LocMapI.start()); 00752 if (LII == LIE) 00753 break; 00754 00755 // Now LII->end > LocMapI.start(). Do we have an overlap? 00756 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) { 00757 // Overlapping correct location. Allocate NewLocNo now. 00758 if (NewLocNo == ~0u) { 00759 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false); 00760 MO.setSubReg(locations[OldLocNo].getSubReg()); 00761 NewLocNo = getLocationNo(MO); 00762 DidChange = true; 00763 } 00764 00765 SlotIndex LStart = LocMapI.start(); 00766 SlotIndex LStop = LocMapI.stop(); 00767 00768 // Trim LocMapI down to the LII overlap. 00769 if (LStart < LII->start) 00770 LocMapI.setStartUnchecked(LII->start); 00771 if (LStop > LII->end) 00772 LocMapI.setStopUnchecked(LII->end); 00773 00774 // Change the value in the overlap. This may trigger coalescing. 00775 LocMapI.setValue(NewLocNo); 00776 00777 // Re-insert any removed OldLocNo ranges. 00778 if (LStart < LocMapI.start()) { 00779 LocMapI.insert(LStart, LocMapI.start(), OldLocNo); 00780 ++LocMapI; 00781 assert(LocMapI.valid() && "Unexpected coalescing"); 00782 } 00783 if (LStop > LocMapI.stop()) { 00784 ++LocMapI; 00785 LocMapI.insert(LII->end, LStop, OldLocNo); 00786 --LocMapI; 00787 } 00788 } 00789 00790 // Advance to the next overlap. 00791 if (LII->end < LocMapI.stop()) { 00792 if (++LII == LIE) 00793 break; 00794 LocMapI.advanceTo(LII->start); 00795 } else { 00796 ++LocMapI; 00797 if (!LocMapI.valid()) 00798 break; 00799 LII = LI->advanceTo(LII, LocMapI.start()); 00800 } 00801 } 00802 } 00803 00804 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself. 00805 locations.erase(locations.begin() + OldLocNo); 00806 LocMapI.goToBegin(); 00807 while (LocMapI.valid()) { 00808 unsigned v = LocMapI.value(); 00809 if (v == OldLocNo) { 00810 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';' 00811 << LocMapI.stop() << ")\n"); 00812 LocMapI.erase(); 00813 } else { 00814 if (v > OldLocNo) 00815 LocMapI.setValueUnchecked(v-1); 00816 ++LocMapI; 00817 } 00818 } 00819 00820 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), 0);}); 00821 return DidChange; 00822 } 00823 00824 bool 00825 UserValue::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) { 00826 bool DidChange = false; 00827 // Split locations referring to OldReg. Iterate backwards so splitLocation can 00828 // safely erase unused locations. 00829 for (unsigned i = locations.size(); i ; --i) { 00830 unsigned LocNo = i-1; 00831 const MachineOperand *Loc = &locations[LocNo]; 00832 if (!Loc->isReg() || Loc->getReg() != OldReg) 00833 continue; 00834 DidChange |= splitLocation(LocNo, NewRegs); 00835 } 00836 return DidChange; 00837 } 00838 00839 void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) { 00840 bool DidChange = false; 00841 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext()) 00842 DidChange |= UV->splitRegister(OldReg, NewRegs); 00843 00844 if (!DidChange) 00845 return; 00846 00847 // Map all of the new virtual registers. 00848 UserValue *UV = lookupVirtReg(OldReg); 00849 for (unsigned i = 0; i != NewRegs.size(); ++i) 00850 mapVirtReg(NewRegs[i]->reg, UV); 00851 } 00852 00853 void LiveDebugVariables:: 00854 splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) { 00855 if (pImpl) 00856 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs); 00857 } 00858 00859 void 00860 UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) { 00861 // Iterate over locations in reverse makes it easier to handle coalescing. 00862 for (unsigned i = locations.size(); i ; --i) { 00863 unsigned LocNo = i-1; 00864 MachineOperand &Loc = locations[LocNo]; 00865 // Only virtual registers are rewritten. 00866 if (!Loc.isReg() || !Loc.getReg() || 00867 !TargetRegisterInfo::isVirtualRegister(Loc.getReg())) 00868 continue; 00869 unsigned VirtReg = Loc.getReg(); 00870 if (VRM.isAssignedReg(VirtReg) && 00871 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) { 00872 // This can create a %noreg operand in rare cases when the sub-register 00873 // index is no longer available. That means the user value is in a 00874 // non-existent sub-register, and %noreg is exactly what we want. 00875 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI); 00876 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) { 00877 // FIXME: Translate SubIdx to a stackslot offset. 00878 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg)); 00879 } else { 00880 Loc.setReg(0); 00881 Loc.setSubReg(0); 00882 } 00883 coalesceLocation(LocNo); 00884 } 00885 } 00886 00887 /// findInsertLocation - Find an iterator for inserting a DBG_VALUE 00888 /// instruction. 00889 static MachineBasicBlock::iterator 00890 findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, 00891 LiveIntervals &LIS) { 00892 SlotIndex Start = LIS.getMBBStartIdx(MBB); 00893 Idx = Idx.getBaseIndex(); 00894 00895 // Try to find an insert location by going backwards from Idx. 00896 MachineInstr *MI; 00897 while (!(MI = LIS.getInstructionFromIndex(Idx))) { 00898 // We've reached the beginning of MBB. 00899 if (Idx == Start) { 00900 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin()); 00901 return I; 00902 } 00903 Idx = Idx.getPrevIndex(); 00904 } 00905 00906 // Don't insert anything after the first terminator, though. 00907 return MI->isTerminator() ? MBB->getFirstTerminator() : 00908 llvm::next(MachineBasicBlock::iterator(MI)); 00909 } 00910 00911 DebugLoc UserValue::findDebugLoc() { 00912 DebugLoc D = dl; 00913 dl = DebugLoc(); 00914 return D; 00915 } 00916 void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, 00917 unsigned LocNo, 00918 LiveIntervals &LIS, 00919 const TargetInstrInfo &TII) { 00920 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS); 00921 MachineOperand &Loc = locations[LocNo]; 00922 ++NumInsertedDebugValues; 00923 00924 // Frame index locations may require a target callback. 00925 if (Loc.isFI()) { 00926 MachineInstr *MI = TII.emitFrameIndexDebugValue(*MBB->getParent(), 00927 Loc.getIndex(), offset, variable, 00928 findDebugLoc()); 00929 if (MI) { 00930 MBB->insert(I, MI); 00931 return; 00932 } 00933 } 00934 // This is not a frame index, or the target is happy with a standard FI. 00935 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE)) 00936 .addOperand(Loc).addImm(offset).addMetadata(variable); 00937 } 00938 00939 void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, 00940 const TargetInstrInfo &TII) { 00941 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end(); 00942 00943 for (LocMap::const_iterator I = locInts.begin(); I.valid();) { 00944 SlotIndex Start = I.start(); 00945 SlotIndex Stop = I.stop(); 00946 unsigned LocNo = I.value(); 00947 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo); 00948 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); 00949 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB); 00950 00951 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 00952 insertDebugValue(MBB, Start, LocNo, LIS, TII); 00953 // This interval may span multiple basic blocks. 00954 // Insert a DBG_VALUE into each one. 00955 while(Stop > MBBEnd) { 00956 // Move to the next block. 00957 Start = MBBEnd; 00958 if (++MBB == MFEnd) 00959 break; 00960 MBBEnd = LIS.getMBBEndIdx(MBB); 00961 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 00962 insertDebugValue(MBB, Start, LocNo, LIS, TII); 00963 } 00964 DEBUG(dbgs() << '\n'); 00965 if (MBB == MFEnd) 00966 break; 00967 00968 ++I; 00969 } 00970 } 00971 00972 void LDVImpl::emitDebugValues(VirtRegMap *VRM) { 00973 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); 00974 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 00975 for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 00976 DEBUG(userValues[i]->print(dbgs(), &MF->getTarget())); 00977 userValues[i]->rewriteLocations(*VRM, *TRI); 00978 userValues[i]->emitDebugValues(VRM, *LIS, *TII); 00979 } 00980 EmitDone = true; 00981 } 00982 00983 void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) { 00984 if (pImpl) 00985 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM); 00986 } 00987 00988 00989 #ifndef NDEBUG 00990 void LiveDebugVariables::dump() { 00991 if (pImpl) 00992 static_cast<LDVImpl*>(pImpl)->print(dbgs()); 00993 } 00994 #endif 00995