1218885Sdim//===- LiveDebugVariables.cpp - Tracking debug info variables -------------===// 2218885Sdim// 3218885Sdim// The LLVM Compiler Infrastructure 4218885Sdim// 5218885Sdim// This file is distributed under the University of Illinois Open Source 6218885Sdim// License. See LICENSE.TXT for details. 7218885Sdim// 8218885Sdim//===----------------------------------------------------------------------===// 9218885Sdim// 10218885Sdim// This file implements the LiveDebugVariables analysis. 11218885Sdim// 12218885Sdim// Remove all DBG_VALUE instructions referencing virtual registers and replace 13218885Sdim// them with a data structure tracking where live user variables are kept - in a 14218885Sdim// virtual register or in a stack slot. 15218885Sdim// 16218885Sdim// Allow the data structure to be updated during register allocation when values 17218885Sdim// are moved between registers and stack slots. Finally emit new DBG_VALUE 18218885Sdim// instructions after register allocation is complete. 19218885Sdim// 20218885Sdim//===----------------------------------------------------------------------===// 21218885Sdim 22218885Sdim#define DEBUG_TYPE "livedebug" 23218885Sdim#include "LiveDebugVariables.h" 24218885Sdim#include "llvm/ADT/IntervalMap.h" 25226633Sdim#include "llvm/ADT/Statistic.h" 26226633Sdim#include "llvm/CodeGen/LexicalScopes.h" 27218885Sdim#include "llvm/CodeGen/LiveIntervalAnalysis.h" 28218885Sdim#include "llvm/CodeGen/MachineDominators.h" 29218885Sdim#include "llvm/CodeGen/MachineFunction.h" 30218885Sdim#include "llvm/CodeGen/MachineInstrBuilder.h" 31221345Sdim#include "llvm/CodeGen/MachineRegisterInfo.h" 32218885Sdim#include "llvm/CodeGen/Passes.h" 33249423Sdim#include "llvm/CodeGen/VirtRegMap.h" 34249423Sdim#include "llvm/DebugInfo.h" 35249423Sdim#include "llvm/IR/Constants.h" 36249423Sdim#include "llvm/IR/Metadata.h" 37249423Sdim#include "llvm/IR/Value.h" 38218885Sdim#include "llvm/Support/CommandLine.h" 39218885Sdim#include "llvm/Support/Debug.h" 40218885Sdim#include "llvm/Target/TargetInstrInfo.h" 41218885Sdim#include "llvm/Target/TargetMachine.h" 42218885Sdim#include "llvm/Target/TargetRegisterInfo.h" 43218885Sdim 44218885Sdimusing namespace llvm; 45218885Sdim 46218885Sdimstatic cl::opt<bool> 47218885SdimEnableLDV("live-debug-variables", cl::init(true), 48218885Sdim cl::desc("Enable the live debug variables pass"), cl::Hidden); 49218885Sdim 50226633SdimSTATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted"); 51218885Sdimchar LiveDebugVariables::ID = 0; 52218885Sdim 53218885SdimINITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars", 54218885Sdim "Debug Variable Analysis", false, false) 55218885SdimINITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 56218885SdimINITIALIZE_PASS_DEPENDENCY(LiveIntervals) 57218885SdimINITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars", 58218885Sdim "Debug Variable Analysis", false, false) 59218885Sdim 60218885Sdimvoid LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const { 61218885Sdim AU.addRequired<MachineDominatorTree>(); 62218885Sdim AU.addRequiredTransitive<LiveIntervals>(); 63218885Sdim AU.setPreservesAll(); 64218885Sdim MachineFunctionPass::getAnalysisUsage(AU); 65218885Sdim} 66218885Sdim 67218885SdimLiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(0) { 68218885Sdim initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 69218885Sdim} 70218885Sdim 71218885Sdim/// LocMap - Map of where a user value is live, and its location. 72218885Sdimtypedef IntervalMap<SlotIndex, unsigned, 4> LocMap; 73218885Sdim 74226633Sdimnamespace { 75226633Sdim/// UserValueScopes - Keeps track of lexical scopes associated with an 76226633Sdim/// user value's source location. 77226633Sdimclass UserValueScopes { 78226633Sdim DebugLoc DL; 79226633Sdim LexicalScopes &LS; 80226633Sdim SmallPtrSet<const MachineBasicBlock *, 4> LBlocks; 81226633Sdim 82226633Sdimpublic: 83226633Sdim UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(D), LS(L) {} 84226633Sdim 85226633Sdim /// dominates - Return true if current scope dominates at least one machine 86226633Sdim /// instruction in a given machine basic block. 87226633Sdim bool dominates(MachineBasicBlock *MBB) { 88226633Sdim if (LBlocks.empty()) 89226633Sdim LS.getMachineBasicBlocks(DL, LBlocks); 90226633Sdim if (LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB)) 91226633Sdim return true; 92226633Sdim return false; 93226633Sdim } 94226633Sdim}; 95226633Sdim} // end anonymous namespace 96226633Sdim 97218885Sdim/// UserValue - A user value is a part of a debug info user variable. 98218885Sdim/// 99218885Sdim/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register 100218885Sdim/// holds part of a user variable. The part is identified by a byte offset. 101218885Sdim/// 102218885Sdim/// UserValues are grouped into equivalence classes for easier searching. Two 103218885Sdim/// user values are related if they refer to the same variable, or if they are 104218885Sdim/// held by the same virtual register. The equivalence class is the transitive 105218885Sdim/// closure of that relation. 106218885Sdimnamespace { 107221345Sdimclass LDVImpl; 108218885Sdimclass UserValue { 109218885Sdim const MDNode *variable; ///< The debug info variable we are part of. 110218885Sdim unsigned offset; ///< Byte offset into variable. 111263508Sdim bool IsIndirect; ///< true if this is a register-indirect+offset value. 112218885Sdim DebugLoc dl; ///< The debug location for the variable. This is 113218885Sdim ///< used by dwarf writer to find lexical scope. 114218885Sdim UserValue *leader; ///< Equivalence class leader. 115218885Sdim UserValue *next; ///< Next value in equivalence class, or null. 116218885Sdim 117218885Sdim /// Numbered locations referenced by locmap. 118218885Sdim SmallVector<MachineOperand, 4> locations; 119218885Sdim 120218885Sdim /// Map of slot indices where this value is live. 121218885Sdim LocMap locInts; 122218885Sdim 123218885Sdim /// coalesceLocation - After LocNo was changed, check if it has become 124218885Sdim /// identical to another location, and coalesce them. This may cause LocNo or 125218885Sdim /// a later location to be erased, but no earlier location will be erased. 126218885Sdim void coalesceLocation(unsigned LocNo); 127218885Sdim 128218885Sdim /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo. 129218885Sdim void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo, 130218885Sdim LiveIntervals &LIS, const TargetInstrInfo &TII); 131218885Sdim 132223017Sdim /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs 133223017Sdim /// is live. Returns true if any changes were made. 134263508Sdim bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 135263508Sdim LiveIntervals &LIS); 136223017Sdim 137218885Sdimpublic: 138218885Sdim /// UserValue - Create a new UserValue. 139263508Sdim UserValue(const MDNode *var, unsigned o, bool i, DebugLoc L, 140218885Sdim LocMap::Allocator &alloc) 141263508Sdim : variable(var), offset(o), IsIndirect(i), dl(L), leader(this), 142263508Sdim next(0), locInts(alloc) 143218885Sdim {} 144218885Sdim 145218885Sdim /// getLeader - Get the leader of this value's equivalence class. 146218885Sdim UserValue *getLeader() { 147218885Sdim UserValue *l = leader; 148218885Sdim while (l != l->leader) 149218885Sdim l = l->leader; 150218885Sdim return leader = l; 151218885Sdim } 152218885Sdim 153218885Sdim /// getNext - Return the next UserValue in the equivalence class. 154218885Sdim UserValue *getNext() const { return next; } 155218885Sdim 156224145Sdim /// match - Does this UserValue match the parameters? 157218885Sdim bool match(const MDNode *Var, unsigned Offset) const { 158218885Sdim return Var == variable && Offset == offset; 159218885Sdim } 160218885Sdim 161218885Sdim /// merge - Merge equivalence classes. 162218885Sdim static UserValue *merge(UserValue *L1, UserValue *L2) { 163218885Sdim L2 = L2->getLeader(); 164218885Sdim if (!L1) 165218885Sdim return L2; 166218885Sdim L1 = L1->getLeader(); 167218885Sdim if (L1 == L2) 168218885Sdim return L1; 169218885Sdim // Splice L2 before L1's members. 170218885Sdim UserValue *End = L2; 171218885Sdim while (End->next) 172218885Sdim End->leader = L1, End = End->next; 173218885Sdim End->leader = L1; 174218885Sdim End->next = L1->next; 175218885Sdim L1->next = L2; 176218885Sdim return L1; 177218885Sdim } 178218885Sdim 179218885Sdim /// getLocationNo - Return the location number that matches Loc. 180218885Sdim unsigned getLocationNo(const MachineOperand &LocMO) { 181221345Sdim if (LocMO.isReg()) { 182221345Sdim if (LocMO.getReg() == 0) 183221345Sdim return ~0u; 184221345Sdim // For register locations we dont care about use/def and other flags. 185221345Sdim for (unsigned i = 0, e = locations.size(); i != e; ++i) 186221345Sdim if (locations[i].isReg() && 187221345Sdim locations[i].getReg() == LocMO.getReg() && 188221345Sdim locations[i].getSubReg() == LocMO.getSubReg()) 189221345Sdim return i; 190221345Sdim } else 191221345Sdim for (unsigned i = 0, e = locations.size(); i != e; ++i) 192221345Sdim if (LocMO.isIdenticalTo(locations[i])) 193221345Sdim return i; 194218885Sdim locations.push_back(LocMO); 195218885Sdim // We are storing a MachineOperand outside a MachineInstr. 196218885Sdim locations.back().clearParent(); 197221345Sdim // Don't store def operands. 198221345Sdim if (locations.back().isReg()) 199221345Sdim locations.back().setIsUse(); 200218885Sdim return locations.size() - 1; 201218885Sdim } 202218885Sdim 203221345Sdim /// mapVirtRegs - Ensure that all virtual register locations are mapped. 204221345Sdim void mapVirtRegs(LDVImpl *LDV); 205221345Sdim 206218885Sdim /// addDef - Add a definition point to this value. 207218885Sdim void addDef(SlotIndex Idx, const MachineOperand &LocMO) { 208218885Sdim // Add a singular (Idx,Idx) -> Loc mapping. 209218885Sdim LocMap::iterator I = locInts.find(Idx); 210218885Sdim if (!I.valid() || I.start() != Idx) 211218885Sdim I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO)); 212226633Sdim else 213226633Sdim // A later DBG_VALUE at the same SlotIndex overrides the old location. 214226633Sdim I.setValue(getLocationNo(LocMO)); 215218885Sdim } 216218885Sdim 217218885Sdim /// extendDef - Extend the current definition as far as possible down the 218218885Sdim /// dominator tree. Stop when meeting an existing def or when leaving the live 219218885Sdim /// range of VNI. 220221345Sdim /// End points where VNI is no longer live are added to Kills. 221218885Sdim /// @param Idx Starting point for the definition. 222218885Sdim /// @param LocNo Location number to propagate. 223263508Sdim /// @param LR Restrict liveness to where LR has the value VNI. May be null. 224263508Sdim /// @param VNI When LR is not null, this is the value to restrict to. 225221345Sdim /// @param Kills Append end points of VNI's live range to Kills. 226218885Sdim /// @param LIS Live intervals analysis. 227218885Sdim /// @param MDT Dominator tree. 228218885Sdim void extendDef(SlotIndex Idx, unsigned LocNo, 229263508Sdim LiveRange *LR, const VNInfo *VNI, 230221345Sdim SmallVectorImpl<SlotIndex> *Kills, 231226633Sdim LiveIntervals &LIS, MachineDominatorTree &MDT, 232234353Sdim UserValueScopes &UVS); 233218885Sdim 234221345Sdim /// addDefsFromCopies - The value in LI/LocNo may be copies to other 235221345Sdim /// registers. Determine if any of the copies are available at the kill 236221345Sdim /// points, and add defs if possible. 237221345Sdim /// @param LI Scan for copies of the value in LI->reg. 238221345Sdim /// @param LocNo Location number of LI->reg. 239221345Sdim /// @param Kills Points where the range of LocNo could be extended. 240221345Sdim /// @param NewDefs Append (Idx, LocNo) of inserted defs here. 241221345Sdim void addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 242221345Sdim const SmallVectorImpl<SlotIndex> &Kills, 243221345Sdim SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 244221345Sdim MachineRegisterInfo &MRI, 245221345Sdim LiveIntervals &LIS); 246221345Sdim 247218885Sdim /// computeIntervals - Compute the live intervals of all locations after 248218885Sdim /// collecting all their def points. 249239462Sdim void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 250226633Sdim LiveIntervals &LIS, MachineDominatorTree &MDT, 251226633Sdim UserValueScopes &UVS); 252218885Sdim 253223017Sdim /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is 254223017Sdim /// live. Returns true if any changes were made. 255263508Sdim bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 256263508Sdim LiveIntervals &LIS); 257223017Sdim 258218885Sdim /// rewriteLocations - Rewrite virtual register locations according to the 259218885Sdim /// provided virtual register map. 260218885Sdim void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI); 261218885Sdim 262249423Sdim /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 263218885Sdim void emitDebugValues(VirtRegMap *VRM, 264218885Sdim LiveIntervals &LIS, const TargetInstrInfo &TRI); 265218885Sdim 266218885Sdim /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A 267218885Sdim /// variable may have more than one corresponding DBG_VALUE instructions. 268218885Sdim /// Only first one needs DebugLoc to identify variable's lexical scope 269218885Sdim /// in source file. 270218885Sdim DebugLoc findDebugLoc(); 271226633Sdim 272226633Sdim /// getDebugLoc - Return DebugLoc of this UserValue. 273226633Sdim DebugLoc getDebugLoc() { return dl;} 274223017Sdim void print(raw_ostream&, const TargetMachine*); 275218885Sdim}; 276218885Sdim} // namespace 277218885Sdim 278218885Sdim/// LDVImpl - Implementation of the LiveDebugVariables pass. 279218885Sdimnamespace { 280218885Sdimclass LDVImpl { 281218885Sdim LiveDebugVariables &pass; 282218885Sdim LocMap::Allocator allocator; 283218885Sdim MachineFunction *MF; 284218885Sdim LiveIntervals *LIS; 285226633Sdim LexicalScopes LS; 286218885Sdim MachineDominatorTree *MDT; 287218885Sdim const TargetRegisterInfo *TRI; 288218885Sdim 289249423Sdim /// Whether emitDebugValues is called. 290249423Sdim bool EmitDone; 291249423Sdim /// Whether the machine function is modified during the pass. 292249423Sdim bool ModifiedMF; 293249423Sdim 294218885Sdim /// userValues - All allocated UserValue instances. 295218885Sdim SmallVector<UserValue*, 8> userValues; 296218885Sdim 297218885Sdim /// Map virtual register to eq class leader. 298218885Sdim typedef DenseMap<unsigned, UserValue*> VRMap; 299218885Sdim VRMap virtRegToEqClass; 300218885Sdim 301218885Sdim /// Map user variable to eq class leader. 302218885Sdim typedef DenseMap<const MDNode *, UserValue*> UVMap; 303218885Sdim UVMap userVarMap; 304218885Sdim 305218885Sdim /// getUserValue - Find or create a UserValue. 306263508Sdim UserValue *getUserValue(const MDNode *Var, unsigned Offset, 307263508Sdim bool IsIndirect, DebugLoc DL); 308218885Sdim 309218885Sdim /// lookupVirtReg - Find the EC leader for VirtReg or null. 310218885Sdim UserValue *lookupVirtReg(unsigned VirtReg); 311218885Sdim 312218885Sdim /// handleDebugValue - Add DBG_VALUE instruction to our maps. 313218885Sdim /// @param MI DBG_VALUE instruction 314218885Sdim /// @param Idx Last valid SLotIndex before instruction. 315218885Sdim /// @return True if the DBG_VALUE instruction should be deleted. 316218885Sdim bool handleDebugValue(MachineInstr *MI, SlotIndex Idx); 317218885Sdim 318218885Sdim /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding 319218885Sdim /// a UserValue def for each instruction. 320218885Sdim /// @param mf MachineFunction to be scanned. 321218885Sdim /// @return True if any debug values were found. 322218885Sdim bool collectDebugValues(MachineFunction &mf); 323218885Sdim 324218885Sdim /// computeIntervals - Compute the live intervals of all user values after 325218885Sdim /// collecting all their def points. 326218885Sdim void computeIntervals(); 327218885Sdim 328218885Sdimpublic: 329249423Sdim LDVImpl(LiveDebugVariables *ps) : pass(*ps), EmitDone(false), 330249423Sdim ModifiedMF(false) {} 331218885Sdim bool runOnMachineFunction(MachineFunction &mf); 332218885Sdim 333249423Sdim /// clear - Release all memory. 334218885Sdim void clear() { 335218885Sdim DeleteContainerPointers(userValues); 336218885Sdim userValues.clear(); 337218885Sdim virtRegToEqClass.clear(); 338218885Sdim userVarMap.clear(); 339249423Sdim // Make sure we call emitDebugValues if the machine function was modified. 340249423Sdim assert((!ModifiedMF || EmitDone) && 341249423Sdim "Dbg values are not emitted in LDV"); 342249423Sdim EmitDone = false; 343249423Sdim ModifiedMF = false; 344218885Sdim } 345218885Sdim 346221345Sdim /// mapVirtReg - Map virtual register to an equivalence class. 347221345Sdim void mapVirtReg(unsigned VirtReg, UserValue *EC); 348221345Sdim 349223017Sdim /// splitRegister - Replace all references to OldReg with NewRegs. 350263508Sdim void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs); 351223017Sdim 352249423Sdim /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 353218885Sdim void emitDebugValues(VirtRegMap *VRM); 354218885Sdim 355218885Sdim void print(raw_ostream&); 356218885Sdim}; 357218885Sdim} // namespace 358218885Sdim 359223017Sdimvoid UserValue::print(raw_ostream &OS, const TargetMachine *TM) { 360226633Sdim DIVariable DV(variable); 361226633Sdim OS << "!\""; 362226633Sdim DV.printExtendedName(OS); 363226633Sdim OS << "\"\t"; 364218885Sdim if (offset) 365218885Sdim OS << '+' << offset; 366218885Sdim for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { 367218885Sdim OS << " [" << I.start() << ';' << I.stop() << "):"; 368218885Sdim if (I.value() == ~0u) 369218885Sdim OS << "undef"; 370218885Sdim else 371218885Sdim OS << I.value(); 372218885Sdim } 373223017Sdim for (unsigned i = 0, e = locations.size(); i != e; ++i) { 374223017Sdim OS << " Loc" << i << '='; 375223017Sdim locations[i].print(OS, TM); 376223017Sdim } 377218885Sdim OS << '\n'; 378218885Sdim} 379218885Sdim 380218885Sdimvoid LDVImpl::print(raw_ostream &OS) { 381218885Sdim OS << "********** DEBUG VARIABLES **********\n"; 382218885Sdim for (unsigned i = 0, e = userValues.size(); i != e; ++i) 383223017Sdim userValues[i]->print(OS, &MF->getTarget()); 384218885Sdim} 385218885Sdim 386218885Sdimvoid UserValue::coalesceLocation(unsigned LocNo) { 387218885Sdim unsigned KeepLoc = 0; 388218885Sdim for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) { 389218885Sdim if (KeepLoc == LocNo) 390218885Sdim continue; 391218885Sdim if (locations[KeepLoc].isIdenticalTo(locations[LocNo])) 392218885Sdim break; 393218885Sdim } 394218885Sdim // No matches. 395218885Sdim if (KeepLoc == locations.size()) 396218885Sdim return; 397218885Sdim 398218885Sdim // Keep the smaller location, erase the larger one. 399218885Sdim unsigned EraseLoc = LocNo; 400218885Sdim if (KeepLoc > EraseLoc) 401218885Sdim std::swap(KeepLoc, EraseLoc); 402218885Sdim locations.erase(locations.begin() + EraseLoc); 403218885Sdim 404218885Sdim // Rewrite values. 405218885Sdim for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { 406218885Sdim unsigned v = I.value(); 407218885Sdim if (v == EraseLoc) 408218885Sdim I.setValue(KeepLoc); // Coalesce when possible. 409218885Sdim else if (v > EraseLoc) 410218885Sdim I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values. 411218885Sdim } 412218885Sdim} 413218885Sdim 414221345Sdimvoid UserValue::mapVirtRegs(LDVImpl *LDV) { 415221345Sdim for (unsigned i = 0, e = locations.size(); i != e; ++i) 416221345Sdim if (locations[i].isReg() && 417221345Sdim TargetRegisterInfo::isVirtualRegister(locations[i].getReg())) 418221345Sdim LDV->mapVirtReg(locations[i].getReg(), this); 419221345Sdim} 420221345Sdim 421218885SdimUserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset, 422263508Sdim bool IsIndirect, DebugLoc DL) { 423218885Sdim UserValue *&Leader = userVarMap[Var]; 424218885Sdim if (Leader) { 425218885Sdim UserValue *UV = Leader->getLeader(); 426218885Sdim Leader = UV; 427218885Sdim for (; UV; UV = UV->getNext()) 428218885Sdim if (UV->match(Var, Offset)) 429218885Sdim return UV; 430218885Sdim } 431218885Sdim 432263508Sdim UserValue *UV = new UserValue(Var, Offset, IsIndirect, DL, allocator); 433218885Sdim userValues.push_back(UV); 434218885Sdim Leader = UserValue::merge(Leader, UV); 435218885Sdim return UV; 436218885Sdim} 437218885Sdim 438218885Sdimvoid LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) { 439218885Sdim assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs"); 440218885Sdim UserValue *&Leader = virtRegToEqClass[VirtReg]; 441218885Sdim Leader = UserValue::merge(Leader, EC); 442218885Sdim} 443218885Sdim 444218885SdimUserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) { 445218885Sdim if (UserValue *UV = virtRegToEqClass.lookup(VirtReg)) 446218885Sdim return UV->getLeader(); 447218885Sdim return 0; 448218885Sdim} 449218885Sdim 450218885Sdimbool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) { 451218885Sdim // DBG_VALUE loc, offset, variable 452218885Sdim if (MI->getNumOperands() != 3 || 453263508Sdim !(MI->getOperand(1).isReg() || MI->getOperand(1).isImm()) || 454263508Sdim !MI->getOperand(2).isMetadata()) { 455218885Sdim DEBUG(dbgs() << "Can't handle " << *MI); 456218885Sdim return false; 457218885Sdim } 458218885Sdim 459218885Sdim // Get or create the UserValue for (variable,offset). 460263508Sdim bool IsIndirect = MI->isIndirectDebugValue(); 461263508Sdim unsigned Offset = IsIndirect ? MI->getOperand(1).getImm() : 0; 462218885Sdim const MDNode *Var = MI->getOperand(2).getMetadata(); 463263508Sdim //here. 464263508Sdim UserValue *UV = getUserValue(Var, Offset, IsIndirect, MI->getDebugLoc()); 465218885Sdim UV->addDef(Idx, MI->getOperand(0)); 466218885Sdim return true; 467218885Sdim} 468218885Sdim 469218885Sdimbool LDVImpl::collectDebugValues(MachineFunction &mf) { 470218885Sdim bool Changed = false; 471218885Sdim for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE; 472218885Sdim ++MFI) { 473218885Sdim MachineBasicBlock *MBB = MFI; 474218885Sdim for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); 475218885Sdim MBBI != MBBE;) { 476218885Sdim if (!MBBI->isDebugValue()) { 477218885Sdim ++MBBI; 478218885Sdim continue; 479218885Sdim } 480218885Sdim // DBG_VALUE has no slot index, use the previous instruction instead. 481218885Sdim SlotIndex Idx = MBBI == MBB->begin() ? 482218885Sdim LIS->getMBBStartIdx(MBB) : 483234353Sdim LIS->getInstructionIndex(llvm::prior(MBBI)).getRegSlot(); 484218885Sdim // Handle consecutive DBG_VALUE instructions with the same slot index. 485218885Sdim do { 486218885Sdim if (handleDebugValue(MBBI, Idx)) { 487218885Sdim MBBI = MBB->erase(MBBI); 488218885Sdim Changed = true; 489218885Sdim } else 490218885Sdim ++MBBI; 491218885Sdim } while (MBBI != MBBE && MBBI->isDebugValue()); 492218885Sdim } 493218885Sdim } 494218885Sdim return Changed; 495218885Sdim} 496218885Sdim 497218885Sdimvoid UserValue::extendDef(SlotIndex Idx, unsigned LocNo, 498263508Sdim LiveRange *LR, const VNInfo *VNI, 499221345Sdim SmallVectorImpl<SlotIndex> *Kills, 500226633Sdim LiveIntervals &LIS, MachineDominatorTree &MDT, 501234353Sdim UserValueScopes &UVS) { 502218885Sdim SmallVector<SlotIndex, 16> Todo; 503218885Sdim Todo.push_back(Idx); 504218885Sdim do { 505218885Sdim SlotIndex Start = Todo.pop_back_val(); 506218885Sdim MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start); 507218885Sdim SlotIndex Stop = LIS.getMBBEndIdx(MBB); 508218885Sdim LocMap::iterator I = locInts.find(Start); 509218885Sdim 510218885Sdim // Limit to VNI's live range. 511218885Sdim bool ToEnd = true; 512263508Sdim if (LR && VNI) { 513263508Sdim LiveInterval::Segment *Segment = LR->getSegmentContaining(Start); 514263508Sdim if (!Segment || Segment->valno != VNI) { 515221345Sdim if (Kills) 516221345Sdim Kills->push_back(Start); 517218885Sdim continue; 518221345Sdim } 519263508Sdim if (Segment->end < Stop) 520263508Sdim Stop = Segment->end, ToEnd = false; 521218885Sdim } 522218885Sdim 523218885Sdim // There could already be a short def at Start. 524218885Sdim if (I.valid() && I.start() <= Start) { 525218885Sdim // Stop when meeting a different location or an already extended interval. 526218885Sdim Start = Start.getNextSlot(); 527218885Sdim if (I.value() != LocNo || I.stop() != Start) 528218885Sdim continue; 529218885Sdim // This is a one-slot placeholder. Just skip it. 530218885Sdim ++I; 531218885Sdim } 532218885Sdim 533218885Sdim // Limited by the next def. 534218885Sdim if (I.valid() && I.start() < Stop) 535218885Sdim Stop = I.start(), ToEnd = false; 536221345Sdim // Limited by VNI's live range. 537221345Sdim else if (!ToEnd && Kills) 538221345Sdim Kills->push_back(Stop); 539218885Sdim 540218885Sdim if (Start >= Stop) 541218885Sdim continue; 542218885Sdim 543218885Sdim I.insert(Start, Stop, LocNo); 544218885Sdim 545218885Sdim // If we extended to the MBB end, propagate down the dominator tree. 546218885Sdim if (!ToEnd) 547218885Sdim continue; 548218885Sdim const std::vector<MachineDomTreeNode*> &Children = 549218885Sdim MDT.getNode(MBB)->getChildren(); 550226633Sdim for (unsigned i = 0, e = Children.size(); i != e; ++i) { 551226633Sdim MachineBasicBlock *MBB = Children[i]->getBlock(); 552226633Sdim if (UVS.dominates(MBB)) 553226633Sdim Todo.push_back(LIS.getMBBStartIdx(MBB)); 554226633Sdim } 555218885Sdim } while (!Todo.empty()); 556218885Sdim} 557218885Sdim 558218885Sdimvoid 559221345SdimUserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 560221345Sdim const SmallVectorImpl<SlotIndex> &Kills, 561221345Sdim SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 562221345Sdim MachineRegisterInfo &MRI, LiveIntervals &LIS) { 563221345Sdim if (Kills.empty()) 564221345Sdim return; 565221345Sdim // Don't track copies from physregs, there are too many uses. 566221345Sdim if (!TargetRegisterInfo::isVirtualRegister(LI->reg)) 567221345Sdim return; 568221345Sdim 569221345Sdim // Collect all the (vreg, valno) pairs that are copies of LI. 570221345Sdim SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues; 571221345Sdim for (MachineRegisterInfo::use_nodbg_iterator 572221345Sdim UI = MRI.use_nodbg_begin(LI->reg), 573221345Sdim UE = MRI.use_nodbg_end(); UI != UE; ++UI) { 574221345Sdim // Copies of the full value. 575221345Sdim if (UI.getOperand().getSubReg() || !UI->isCopy()) 576221345Sdim continue; 577221345Sdim MachineInstr *MI = &*UI; 578221345Sdim unsigned DstReg = MI->getOperand(0).getReg(); 579221345Sdim 580221345Sdim // Don't follow copies to physregs. These are usually setting up call 581221345Sdim // arguments, and the argument registers are always call clobbered. We are 582221345Sdim // better off in the source register which could be a callee-saved register, 583221345Sdim // or it could be spilled. 584221345Sdim if (!TargetRegisterInfo::isVirtualRegister(DstReg)) 585221345Sdim continue; 586221345Sdim 587221345Sdim // Is LocNo extended to reach this copy? If not, another def may be blocking 588221345Sdim // it, or we are looking at a wrong value of LI. 589221345Sdim SlotIndex Idx = LIS.getInstructionIndex(MI); 590234353Sdim LocMap::iterator I = locInts.find(Idx.getRegSlot(true)); 591221345Sdim if (!I.valid() || I.value() != LocNo) 592221345Sdim continue; 593221345Sdim 594221345Sdim if (!LIS.hasInterval(DstReg)) 595221345Sdim continue; 596221345Sdim LiveInterval *DstLI = &LIS.getInterval(DstReg); 597234353Sdim const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot()); 598234353Sdim assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value"); 599221345Sdim CopyValues.push_back(std::make_pair(DstLI, DstVNI)); 600221345Sdim } 601221345Sdim 602221345Sdim if (CopyValues.empty()) 603221345Sdim return; 604221345Sdim 605221345Sdim DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n'); 606221345Sdim 607221345Sdim // Try to add defs of the copied values for each kill point. 608221345Sdim for (unsigned i = 0, e = Kills.size(); i != e; ++i) { 609221345Sdim SlotIndex Idx = Kills[i]; 610221345Sdim for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) { 611221345Sdim LiveInterval *DstLI = CopyValues[j].first; 612221345Sdim const VNInfo *DstVNI = CopyValues[j].second; 613221345Sdim if (DstLI->getVNInfoAt(Idx) != DstVNI) 614221345Sdim continue; 615221345Sdim // Check that there isn't already a def at Idx 616221345Sdim LocMap::iterator I = locInts.find(Idx); 617221345Sdim if (I.valid() && I.start() <= Idx) 618221345Sdim continue; 619221345Sdim DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" 620221345Sdim << DstVNI->id << " in " << *DstLI << '\n'); 621221345Sdim MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); 622221345Sdim assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); 623221345Sdim unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); 624221345Sdim I.insert(Idx, Idx.getNextSlot(), LocNo); 625221345Sdim NewDefs.push_back(std::make_pair(Idx, LocNo)); 626221345Sdim break; 627221345Sdim } 628221345Sdim } 629221345Sdim} 630221345Sdim 631221345Sdimvoid 632221345SdimUserValue::computeIntervals(MachineRegisterInfo &MRI, 633239462Sdim const TargetRegisterInfo &TRI, 634221345Sdim LiveIntervals &LIS, 635226633Sdim MachineDominatorTree &MDT, 636234353Sdim UserValueScopes &UVS) { 637218885Sdim SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs; 638218885Sdim 639218885Sdim // Collect all defs to be extended (Skipping undefs). 640218885Sdim for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) 641218885Sdim if (I.value() != ~0u) 642218885Sdim Defs.push_back(std::make_pair(I.start(), I.value())); 643218885Sdim 644221345Sdim // Extend all defs, and possibly add new ones along the way. 645221345Sdim for (unsigned i = 0; i != Defs.size(); ++i) { 646218885Sdim SlotIndex Idx = Defs[i].first; 647218885Sdim unsigned LocNo = Defs[i].second; 648218885Sdim const MachineOperand &Loc = locations[LocNo]; 649218885Sdim 650239462Sdim if (!Loc.isReg()) { 651239462Sdim extendDef(Idx, LocNo, 0, 0, 0, LIS, MDT, UVS); 652239462Sdim continue; 653239462Sdim } 654239462Sdim 655218885Sdim // Register locations are constrained to where the register value is live. 656239462Sdim if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) { 657239462Sdim LiveInterval *LI = 0; 658239462Sdim const VNInfo *VNI = 0; 659239462Sdim if (LIS.hasInterval(Loc.getReg())) { 660239462Sdim LI = &LIS.getInterval(Loc.getReg()); 661239462Sdim VNI = LI->getVNInfoAt(Idx); 662239462Sdim } 663221345Sdim SmallVector<SlotIndex, 16> Kills; 664226633Sdim extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS); 665239462Sdim if (LI) 666239462Sdim addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS); 667239462Sdim continue; 668239462Sdim } 669239462Sdim 670239462Sdim // For physregs, use the live range of the first regunit as a guide. 671239462Sdim unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI); 672263508Sdim LiveRange *LR = &LIS.getRegUnit(Unit); 673263508Sdim const VNInfo *VNI = LR->getVNInfoAt(Idx); 674239462Sdim // Don't track copies from physregs, it is too expensive. 675263508Sdim extendDef(Idx, LocNo, LR, VNI, 0, LIS, MDT, UVS); 676218885Sdim } 677218885Sdim 678218885Sdim // Finally, erase all the undefs. 679218885Sdim for (LocMap::iterator I = locInts.begin(); I.valid();) 680218885Sdim if (I.value() == ~0u) 681218885Sdim I.erase(); 682218885Sdim else 683218885Sdim ++I; 684218885Sdim} 685218885Sdim 686218885Sdimvoid LDVImpl::computeIntervals() { 687221345Sdim for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 688226633Sdim UserValueScopes UVS(userValues[i]->getDebugLoc(), LS); 689239462Sdim userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS); 690221345Sdim userValues[i]->mapVirtRegs(this); 691221345Sdim } 692218885Sdim} 693218885Sdim 694218885Sdimbool LDVImpl::runOnMachineFunction(MachineFunction &mf) { 695218885Sdim MF = &mf; 696218885Sdim LIS = &pass.getAnalysis<LiveIntervals>(); 697218885Sdim MDT = &pass.getAnalysis<MachineDominatorTree>(); 698218885Sdim TRI = mf.getTarget().getRegisterInfo(); 699218885Sdim clear(); 700226633Sdim LS.initialize(mf); 701218885Sdim DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " 702243830Sdim << mf.getName() << " **********\n"); 703218885Sdim 704218885Sdim bool Changed = collectDebugValues(mf); 705218885Sdim computeIntervals(); 706218885Sdim DEBUG(print(dbgs())); 707226633Sdim LS.releaseMemory(); 708249423Sdim ModifiedMF = Changed; 709218885Sdim return Changed; 710218885Sdim} 711218885Sdim 712218885Sdimbool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) { 713218885Sdim if (!EnableLDV) 714218885Sdim return false; 715218885Sdim if (!pImpl) 716218885Sdim pImpl = new LDVImpl(this); 717218885Sdim return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf); 718218885Sdim} 719218885Sdim 720218885Sdimvoid LiveDebugVariables::releaseMemory() { 721218885Sdim if (pImpl) 722218885Sdim static_cast<LDVImpl*>(pImpl)->clear(); 723218885Sdim} 724218885Sdim 725218885SdimLiveDebugVariables::~LiveDebugVariables() { 726218885Sdim if (pImpl) 727218885Sdim delete static_cast<LDVImpl*>(pImpl); 728218885Sdim} 729218885Sdim 730223017Sdim//===----------------------------------------------------------------------===// 731223017Sdim// Live Range Splitting 732223017Sdim//===----------------------------------------------------------------------===// 733223017Sdim 734223017Sdimbool 735263508SdimUserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 736263508Sdim LiveIntervals& LIS) { 737223017Sdim DEBUG({ 738223017Sdim dbgs() << "Splitting Loc" << OldLocNo << '\t'; 739223017Sdim print(dbgs(), 0); 740223017Sdim }); 741223017Sdim bool DidChange = false; 742223017Sdim LocMap::iterator LocMapI; 743223017Sdim LocMapI.setMap(locInts); 744223017Sdim for (unsigned i = 0; i != NewRegs.size(); ++i) { 745263508Sdim LiveInterval *LI = &LIS.getInterval(NewRegs[i]); 746223017Sdim if (LI->empty()) 747223017Sdim continue; 748223017Sdim 749223017Sdim // Don't allocate the new LocNo until it is needed. 750223017Sdim unsigned NewLocNo = ~0u; 751223017Sdim 752223017Sdim // Iterate over the overlaps between locInts and LI. 753223017Sdim LocMapI.find(LI->beginIndex()); 754223017Sdim if (!LocMapI.valid()) 755223017Sdim continue; 756223017Sdim LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start()); 757223017Sdim LiveInterval::iterator LIE = LI->end(); 758223017Sdim while (LocMapI.valid() && LII != LIE) { 759223017Sdim // At this point, we know that LocMapI.stop() > LII->start. 760223017Sdim LII = LI->advanceTo(LII, LocMapI.start()); 761223017Sdim if (LII == LIE) 762223017Sdim break; 763223017Sdim 764223017Sdim // Now LII->end > LocMapI.start(). Do we have an overlap? 765223017Sdim if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) { 766223017Sdim // Overlapping correct location. Allocate NewLocNo now. 767223017Sdim if (NewLocNo == ~0u) { 768223017Sdim MachineOperand MO = MachineOperand::CreateReg(LI->reg, false); 769223017Sdim MO.setSubReg(locations[OldLocNo].getSubReg()); 770223017Sdim NewLocNo = getLocationNo(MO); 771223017Sdim DidChange = true; 772223017Sdim } 773223017Sdim 774223017Sdim SlotIndex LStart = LocMapI.start(); 775223017Sdim SlotIndex LStop = LocMapI.stop(); 776223017Sdim 777223017Sdim // Trim LocMapI down to the LII overlap. 778223017Sdim if (LStart < LII->start) 779223017Sdim LocMapI.setStartUnchecked(LII->start); 780223017Sdim if (LStop > LII->end) 781223017Sdim LocMapI.setStopUnchecked(LII->end); 782223017Sdim 783223017Sdim // Change the value in the overlap. This may trigger coalescing. 784223017Sdim LocMapI.setValue(NewLocNo); 785223017Sdim 786223017Sdim // Re-insert any removed OldLocNo ranges. 787223017Sdim if (LStart < LocMapI.start()) { 788223017Sdim LocMapI.insert(LStart, LocMapI.start(), OldLocNo); 789223017Sdim ++LocMapI; 790223017Sdim assert(LocMapI.valid() && "Unexpected coalescing"); 791223017Sdim } 792223017Sdim if (LStop > LocMapI.stop()) { 793223017Sdim ++LocMapI; 794223017Sdim LocMapI.insert(LII->end, LStop, OldLocNo); 795223017Sdim --LocMapI; 796223017Sdim } 797223017Sdim } 798223017Sdim 799223017Sdim // Advance to the next overlap. 800223017Sdim if (LII->end < LocMapI.stop()) { 801223017Sdim if (++LII == LIE) 802223017Sdim break; 803223017Sdim LocMapI.advanceTo(LII->start); 804223017Sdim } else { 805223017Sdim ++LocMapI; 806223017Sdim if (!LocMapI.valid()) 807223017Sdim break; 808223017Sdim LII = LI->advanceTo(LII, LocMapI.start()); 809223017Sdim } 810223017Sdim } 811223017Sdim } 812223017Sdim 813223017Sdim // Finally, remove any remaining OldLocNo intervals and OldLocNo itself. 814223017Sdim locations.erase(locations.begin() + OldLocNo); 815223017Sdim LocMapI.goToBegin(); 816223017Sdim while (LocMapI.valid()) { 817223017Sdim unsigned v = LocMapI.value(); 818223017Sdim if (v == OldLocNo) { 819223017Sdim DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';' 820223017Sdim << LocMapI.stop() << ")\n"); 821223017Sdim LocMapI.erase(); 822223017Sdim } else { 823223017Sdim if (v > OldLocNo) 824223017Sdim LocMapI.setValueUnchecked(v-1); 825223017Sdim ++LocMapI; 826223017Sdim } 827223017Sdim } 828223017Sdim 829223017Sdim DEBUG({dbgs() << "Split result: \t"; print(dbgs(), 0);}); 830223017Sdim return DidChange; 831223017Sdim} 832223017Sdim 833223017Sdimbool 834263508SdimUserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, 835263508Sdim LiveIntervals &LIS) { 836223017Sdim bool DidChange = false; 837223017Sdim // Split locations referring to OldReg. Iterate backwards so splitLocation can 838234353Sdim // safely erase unused locations. 839223017Sdim for (unsigned i = locations.size(); i ; --i) { 840223017Sdim unsigned LocNo = i-1; 841223017Sdim const MachineOperand *Loc = &locations[LocNo]; 842223017Sdim if (!Loc->isReg() || Loc->getReg() != OldReg) 843223017Sdim continue; 844263508Sdim DidChange |= splitLocation(LocNo, NewRegs, LIS); 845223017Sdim } 846223017Sdim return DidChange; 847223017Sdim} 848223017Sdim 849263508Sdimvoid LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) { 850223017Sdim bool DidChange = false; 851223017Sdim for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext()) 852263508Sdim DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS); 853223017Sdim 854223017Sdim if (!DidChange) 855223017Sdim return; 856223017Sdim 857223017Sdim // Map all of the new virtual registers. 858223017Sdim UserValue *UV = lookupVirtReg(OldReg); 859223017Sdim for (unsigned i = 0; i != NewRegs.size(); ++i) 860263508Sdim mapVirtReg(NewRegs[i], UV); 861223017Sdim} 862223017Sdim 863223017Sdimvoid LiveDebugVariables:: 864263508SdimsplitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) { 865223017Sdim if (pImpl) 866223017Sdim static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs); 867223017Sdim} 868223017Sdim 869218885Sdimvoid 870218885SdimUserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) { 871218885Sdim // Iterate over locations in reverse makes it easier to handle coalescing. 872218885Sdim for (unsigned i = locations.size(); i ; --i) { 873218885Sdim unsigned LocNo = i-1; 874218885Sdim MachineOperand &Loc = locations[LocNo]; 875218885Sdim // Only virtual registers are rewritten. 876218885Sdim if (!Loc.isReg() || !Loc.getReg() || 877218885Sdim !TargetRegisterInfo::isVirtualRegister(Loc.getReg())) 878218885Sdim continue; 879218885Sdim unsigned VirtReg = Loc.getReg(); 880218885Sdim if (VRM.isAssignedReg(VirtReg) && 881218885Sdim TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) { 882223017Sdim // This can create a %noreg operand in rare cases when the sub-register 883223017Sdim // index is no longer available. That means the user value is in a 884223017Sdim // non-existent sub-register, and %noreg is exactly what we want. 885218885Sdim Loc.substPhysReg(VRM.getPhys(VirtReg), TRI); 886234353Sdim } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) { 887218885Sdim // FIXME: Translate SubIdx to a stackslot offset. 888218885Sdim Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg)); 889218885Sdim } else { 890218885Sdim Loc.setReg(0); 891218885Sdim Loc.setSubReg(0); 892218885Sdim } 893218885Sdim coalesceLocation(LocNo); 894218885Sdim } 895218885Sdim} 896218885Sdim 897218885Sdim/// findInsertLocation - Find an iterator for inserting a DBG_VALUE 898218885Sdim/// instruction. 899218885Sdimstatic MachineBasicBlock::iterator 900218885SdimfindInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, 901218885Sdim LiveIntervals &LIS) { 902218885Sdim SlotIndex Start = LIS.getMBBStartIdx(MBB); 903218885Sdim Idx = Idx.getBaseIndex(); 904218885Sdim 905218885Sdim // Try to find an insert location by going backwards from Idx. 906218885Sdim MachineInstr *MI; 907218885Sdim while (!(MI = LIS.getInstructionFromIndex(Idx))) { 908218885Sdim // We've reached the beginning of MBB. 909218885Sdim if (Idx == Start) { 910218885Sdim MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin()); 911218885Sdim return I; 912218885Sdim } 913218885Sdim Idx = Idx.getPrevIndex(); 914218885Sdim } 915218885Sdim 916218885Sdim // Don't insert anything after the first terminator, though. 917234353Sdim return MI->isTerminator() ? MBB->getFirstTerminator() : 918234353Sdim llvm::next(MachineBasicBlock::iterator(MI)); 919218885Sdim} 920218885Sdim 921218885SdimDebugLoc UserValue::findDebugLoc() { 922218885Sdim DebugLoc D = dl; 923218885Sdim dl = DebugLoc(); 924218885Sdim return D; 925218885Sdim} 926218885Sdimvoid UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, 927218885Sdim unsigned LocNo, 928218885Sdim LiveIntervals &LIS, 929218885Sdim const TargetInstrInfo &TII) { 930218885Sdim MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS); 931218885Sdim MachineOperand &Loc = locations[LocNo]; 932226633Sdim ++NumInsertedDebugValues; 933218885Sdim 934263508Sdim if (Loc.isReg()) 935263508Sdim BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE), 936263508Sdim IsIndirect, Loc.getReg(), offset, variable); 937263508Sdim else 938263508Sdim BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE)) 939263508Sdim .addOperand(Loc).addImm(offset).addMetadata(variable); 940218885Sdim} 941218885Sdim 942218885Sdimvoid UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, 943218885Sdim const TargetInstrInfo &TII) { 944218885Sdim MachineFunction::iterator MFEnd = VRM->getMachineFunction().end(); 945218885Sdim 946218885Sdim for (LocMap::const_iterator I = locInts.begin(); I.valid();) { 947218885Sdim SlotIndex Start = I.start(); 948218885Sdim SlotIndex Stop = I.stop(); 949218885Sdim unsigned LocNo = I.value(); 950218885Sdim DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo); 951218885Sdim MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); 952218885Sdim SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB); 953218885Sdim 954218885Sdim DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 955218885Sdim insertDebugValue(MBB, Start, LocNo, LIS, TII); 956218885Sdim // This interval may span multiple basic blocks. 957218885Sdim // Insert a DBG_VALUE into each one. 958218885Sdim while(Stop > MBBEnd) { 959218885Sdim // Move to the next block. 960218885Sdim Start = MBBEnd; 961218885Sdim if (++MBB == MFEnd) 962218885Sdim break; 963218885Sdim MBBEnd = LIS.getMBBEndIdx(MBB); 964218885Sdim DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 965218885Sdim insertDebugValue(MBB, Start, LocNo, LIS, TII); 966218885Sdim } 967218885Sdim DEBUG(dbgs() << '\n'); 968218885Sdim if (MBB == MFEnd) 969218885Sdim break; 970218885Sdim 971218885Sdim ++I; 972218885Sdim } 973218885Sdim} 974218885Sdim 975218885Sdimvoid LDVImpl::emitDebugValues(VirtRegMap *VRM) { 976218885Sdim DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); 977218885Sdim const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 978218885Sdim for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 979223017Sdim DEBUG(userValues[i]->print(dbgs(), &MF->getTarget())); 980218885Sdim userValues[i]->rewriteLocations(*VRM, *TRI); 981218885Sdim userValues[i]->emitDebugValues(VRM, *LIS, *TII); 982218885Sdim } 983249423Sdim EmitDone = true; 984218885Sdim} 985218885Sdim 986218885Sdimvoid LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) { 987218885Sdim if (pImpl) 988218885Sdim static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM); 989218885Sdim} 990218885Sdim 991218885Sdim 992218885Sdim#ifndef NDEBUG 993218885Sdimvoid LiveDebugVariables::dump() { 994218885Sdim if (pImpl) 995218885Sdim static_cast<LDVImpl*>(pImpl)->print(dbgs()); 996218885Sdim} 997218885Sdim#endif 998