12754fe60SDimitry Andric //===- LiveDebugVariables.cpp - Tracking debug info variables -------------===// 22754fe60SDimitry Andric // 32754fe60SDimitry Andric // The LLVM Compiler Infrastructure 42754fe60SDimitry Andric // 52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source 62754fe60SDimitry Andric // License. See LICENSE.TXT for details. 72754fe60SDimitry Andric // 82754fe60SDimitry Andric //===----------------------------------------------------------------------===// 92754fe60SDimitry Andric // 102754fe60SDimitry Andric // This file implements the LiveDebugVariables analysis. 112754fe60SDimitry Andric // 122754fe60SDimitry Andric // Remove all DBG_VALUE instructions referencing virtual registers and replace 132754fe60SDimitry Andric // them with a data structure tracking where live user variables are kept - in a 142754fe60SDimitry Andric // virtual register or in a stack slot. 152754fe60SDimitry Andric // 162754fe60SDimitry Andric // Allow the data structure to be updated during register allocation when values 172754fe60SDimitry Andric // are moved between registers and stack slots. Finally emit new DBG_VALUE 182754fe60SDimitry Andric // instructions after register allocation is complete. 192754fe60SDimitry Andric // 202754fe60SDimitry Andric //===----------------------------------------------------------------------===// 212754fe60SDimitry Andric 222754fe60SDimitry Andric #define DEBUG_TYPE "livedebug" 232754fe60SDimitry Andric #include "LiveDebugVariables.h" 242754fe60SDimitry Andric #include "VirtRegMap.h" 252754fe60SDimitry Andric #include "llvm/Constants.h" 262754fe60SDimitry Andric #include "llvm/Metadata.h" 272754fe60SDimitry Andric #include "llvm/Value.h" 282754fe60SDimitry Andric #include "llvm/ADT/IntervalMap.h" 292754fe60SDimitry Andric #include "llvm/CodeGen/LiveIntervalAnalysis.h" 302754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 312754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 322754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 333b0f4066SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 342754fe60SDimitry Andric #include "llvm/CodeGen/Passes.h" 352754fe60SDimitry Andric #include "llvm/Support/CommandLine.h" 362754fe60SDimitry Andric #include "llvm/Support/Debug.h" 372754fe60SDimitry Andric #include "llvm/Target/TargetInstrInfo.h" 382754fe60SDimitry Andric #include "llvm/Target/TargetMachine.h" 392754fe60SDimitry Andric #include "llvm/Target/TargetRegisterInfo.h" 402754fe60SDimitry Andric 412754fe60SDimitry Andric using namespace llvm; 422754fe60SDimitry Andric 432754fe60SDimitry Andric static cl::opt<bool> 442754fe60SDimitry Andric EnableLDV("live-debug-variables", cl::init(true), 452754fe60SDimitry Andric cl::desc("Enable the live debug variables pass"), cl::Hidden); 462754fe60SDimitry Andric 472754fe60SDimitry Andric char LiveDebugVariables::ID = 0; 482754fe60SDimitry Andric 492754fe60SDimitry Andric INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars", 502754fe60SDimitry Andric "Debug Variable Analysis", false, false) 512754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 522754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 532754fe60SDimitry Andric INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars", 542754fe60SDimitry Andric "Debug Variable Analysis", false, false) 552754fe60SDimitry Andric 562754fe60SDimitry Andric void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const { 572754fe60SDimitry Andric AU.addRequired<MachineDominatorTree>(); 582754fe60SDimitry Andric AU.addRequiredTransitive<LiveIntervals>(); 592754fe60SDimitry Andric AU.setPreservesAll(); 602754fe60SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 612754fe60SDimitry Andric } 622754fe60SDimitry Andric 632754fe60SDimitry Andric LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(0) { 642754fe60SDimitry Andric initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 652754fe60SDimitry Andric } 662754fe60SDimitry Andric 672754fe60SDimitry Andric /// LocMap - Map of where a user value is live, and its location. 682754fe60SDimitry Andric typedef IntervalMap<SlotIndex, unsigned, 4> LocMap; 692754fe60SDimitry Andric 702754fe60SDimitry Andric /// UserValue - A user value is a part of a debug info user variable. 712754fe60SDimitry Andric /// 722754fe60SDimitry Andric /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register 732754fe60SDimitry Andric /// holds part of a user variable. The part is identified by a byte offset. 742754fe60SDimitry Andric /// 752754fe60SDimitry Andric /// UserValues are grouped into equivalence classes for easier searching. Two 762754fe60SDimitry Andric /// user values are related if they refer to the same variable, or if they are 772754fe60SDimitry Andric /// held by the same virtual register. The equivalence class is the transitive 782754fe60SDimitry Andric /// closure of that relation. 792754fe60SDimitry Andric namespace { 803b0f4066SDimitry Andric class LDVImpl; 812754fe60SDimitry Andric class UserValue { 822754fe60SDimitry Andric const MDNode *variable; ///< The debug info variable we are part of. 832754fe60SDimitry Andric unsigned offset; ///< Byte offset into variable. 842754fe60SDimitry Andric DebugLoc dl; ///< The debug location for the variable. This is 852754fe60SDimitry Andric ///< used by dwarf writer to find lexical scope. 862754fe60SDimitry Andric UserValue *leader; ///< Equivalence class leader. 872754fe60SDimitry Andric UserValue *next; ///< Next value in equivalence class, or null. 882754fe60SDimitry Andric 892754fe60SDimitry Andric /// Numbered locations referenced by locmap. 902754fe60SDimitry Andric SmallVector<MachineOperand, 4> locations; 912754fe60SDimitry Andric 922754fe60SDimitry Andric /// Map of slot indices where this value is live. 932754fe60SDimitry Andric LocMap locInts; 942754fe60SDimitry Andric 952754fe60SDimitry Andric /// coalesceLocation - After LocNo was changed, check if it has become 962754fe60SDimitry Andric /// identical to another location, and coalesce them. This may cause LocNo or 972754fe60SDimitry Andric /// a later location to be erased, but no earlier location will be erased. 982754fe60SDimitry Andric void coalesceLocation(unsigned LocNo); 992754fe60SDimitry Andric 1002754fe60SDimitry Andric /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo. 1012754fe60SDimitry Andric void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo, 1022754fe60SDimitry Andric LiveIntervals &LIS, const TargetInstrInfo &TII); 1032754fe60SDimitry Andric 104bd5abe19SDimitry Andric /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs 105bd5abe19SDimitry Andric /// is live. Returns true if any changes were made. 106bd5abe19SDimitry Andric bool splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs); 107bd5abe19SDimitry Andric 1082754fe60SDimitry Andric public: 1092754fe60SDimitry Andric /// UserValue - Create a new UserValue. 1102754fe60SDimitry Andric UserValue(const MDNode *var, unsigned o, DebugLoc L, 1112754fe60SDimitry Andric LocMap::Allocator &alloc) 1122754fe60SDimitry Andric : variable(var), offset(o), dl(L), leader(this), next(0), locInts(alloc) 1132754fe60SDimitry Andric {} 1142754fe60SDimitry Andric 1152754fe60SDimitry Andric /// getLeader - Get the leader of this value's equivalence class. 1162754fe60SDimitry Andric UserValue *getLeader() { 1172754fe60SDimitry Andric UserValue *l = leader; 1182754fe60SDimitry Andric while (l != l->leader) 1192754fe60SDimitry Andric l = l->leader; 1202754fe60SDimitry Andric return leader = l; 1212754fe60SDimitry Andric } 1222754fe60SDimitry Andric 1232754fe60SDimitry Andric /// getNext - Return the next UserValue in the equivalence class. 1242754fe60SDimitry Andric UserValue *getNext() const { return next; } 1252754fe60SDimitry Andric 1262754fe60SDimitry Andric /// match - Does this UserValue match the aprameters? 1272754fe60SDimitry Andric bool match(const MDNode *Var, unsigned Offset) const { 1282754fe60SDimitry Andric return Var == variable && Offset == offset; 1292754fe60SDimitry Andric } 1302754fe60SDimitry Andric 1312754fe60SDimitry Andric /// merge - Merge equivalence classes. 1322754fe60SDimitry Andric static UserValue *merge(UserValue *L1, UserValue *L2) { 1332754fe60SDimitry Andric L2 = L2->getLeader(); 1342754fe60SDimitry Andric if (!L1) 1352754fe60SDimitry Andric return L2; 1362754fe60SDimitry Andric L1 = L1->getLeader(); 1372754fe60SDimitry Andric if (L1 == L2) 1382754fe60SDimitry Andric return L1; 1392754fe60SDimitry Andric // Splice L2 before L1's members. 1402754fe60SDimitry Andric UserValue *End = L2; 1412754fe60SDimitry Andric while (End->next) 1422754fe60SDimitry Andric End->leader = L1, End = End->next; 1432754fe60SDimitry Andric End->leader = L1; 1442754fe60SDimitry Andric End->next = L1->next; 1452754fe60SDimitry Andric L1->next = L2; 1462754fe60SDimitry Andric return L1; 1472754fe60SDimitry Andric } 1482754fe60SDimitry Andric 1492754fe60SDimitry Andric /// getLocationNo - Return the location number that matches Loc. 1502754fe60SDimitry Andric unsigned getLocationNo(const MachineOperand &LocMO) { 1513b0f4066SDimitry Andric if (LocMO.isReg()) { 1523b0f4066SDimitry Andric if (LocMO.getReg() == 0) 1532754fe60SDimitry Andric return ~0u; 1543b0f4066SDimitry Andric // For register locations we dont care about use/def and other flags. 1553b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 1563b0f4066SDimitry Andric if (locations[i].isReg() && 1573b0f4066SDimitry Andric locations[i].getReg() == LocMO.getReg() && 1583b0f4066SDimitry Andric locations[i].getSubReg() == LocMO.getSubReg()) 1593b0f4066SDimitry Andric return i; 1603b0f4066SDimitry Andric } else 1612754fe60SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 1622754fe60SDimitry Andric if (LocMO.isIdenticalTo(locations[i])) 1632754fe60SDimitry Andric return i; 1642754fe60SDimitry Andric locations.push_back(LocMO); 1652754fe60SDimitry Andric // We are storing a MachineOperand outside a MachineInstr. 1662754fe60SDimitry Andric locations.back().clearParent(); 1673b0f4066SDimitry Andric // Don't store def operands. 1683b0f4066SDimitry Andric if (locations.back().isReg()) 1693b0f4066SDimitry Andric locations.back().setIsUse(); 1702754fe60SDimitry Andric return locations.size() - 1; 1712754fe60SDimitry Andric } 1722754fe60SDimitry Andric 1733b0f4066SDimitry Andric /// mapVirtRegs - Ensure that all virtual register locations are mapped. 1743b0f4066SDimitry Andric void mapVirtRegs(LDVImpl *LDV); 1753b0f4066SDimitry Andric 1762754fe60SDimitry Andric /// addDef - Add a definition point to this value. 1772754fe60SDimitry Andric void addDef(SlotIndex Idx, const MachineOperand &LocMO) { 1782754fe60SDimitry Andric // Add a singular (Idx,Idx) -> Loc mapping. 1792754fe60SDimitry Andric LocMap::iterator I = locInts.find(Idx); 1802754fe60SDimitry Andric if (!I.valid() || I.start() != Idx) 1812754fe60SDimitry Andric I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO)); 1822754fe60SDimitry Andric } 1832754fe60SDimitry Andric 1842754fe60SDimitry Andric /// extendDef - Extend the current definition as far as possible down the 1852754fe60SDimitry Andric /// dominator tree. Stop when meeting an existing def or when leaving the live 1862754fe60SDimitry Andric /// range of VNI. 1873b0f4066SDimitry Andric /// End points where VNI is no longer live are added to Kills. 1882754fe60SDimitry Andric /// @param Idx Starting point for the definition. 1892754fe60SDimitry Andric /// @param LocNo Location number to propagate. 1902754fe60SDimitry Andric /// @param LI Restrict liveness to where LI has the value VNI. May be null. 1912754fe60SDimitry Andric /// @param VNI When LI is not null, this is the value to restrict to. 1923b0f4066SDimitry Andric /// @param Kills Append end points of VNI's live range to Kills. 1932754fe60SDimitry Andric /// @param LIS Live intervals analysis. 1942754fe60SDimitry Andric /// @param MDT Dominator tree. 1952754fe60SDimitry Andric void extendDef(SlotIndex Idx, unsigned LocNo, 1962754fe60SDimitry Andric LiveInterval *LI, const VNInfo *VNI, 1973b0f4066SDimitry Andric SmallVectorImpl<SlotIndex> *Kills, 1982754fe60SDimitry Andric LiveIntervals &LIS, MachineDominatorTree &MDT); 1992754fe60SDimitry Andric 2003b0f4066SDimitry Andric /// addDefsFromCopies - The value in LI/LocNo may be copies to other 2013b0f4066SDimitry Andric /// registers. Determine if any of the copies are available at the kill 2023b0f4066SDimitry Andric /// points, and add defs if possible. 2033b0f4066SDimitry Andric /// @param LI Scan for copies of the value in LI->reg. 2043b0f4066SDimitry Andric /// @param LocNo Location number of LI->reg. 2053b0f4066SDimitry Andric /// @param Kills Points where the range of LocNo could be extended. 2063b0f4066SDimitry Andric /// @param NewDefs Append (Idx, LocNo) of inserted defs here. 2073b0f4066SDimitry Andric void addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 2083b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 2093b0f4066SDimitry Andric SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 2103b0f4066SDimitry Andric MachineRegisterInfo &MRI, 2113b0f4066SDimitry Andric LiveIntervals &LIS); 2123b0f4066SDimitry Andric 2132754fe60SDimitry Andric /// computeIntervals - Compute the live intervals of all locations after 2142754fe60SDimitry Andric /// collecting all their def points. 2153b0f4066SDimitry Andric void computeIntervals(MachineRegisterInfo &MRI, 2163b0f4066SDimitry Andric LiveIntervals &LIS, MachineDominatorTree &MDT); 2172754fe60SDimitry Andric 2182754fe60SDimitry Andric /// renameRegister - Update locations to rewrite OldReg as NewReg:SubIdx. 2192754fe60SDimitry Andric void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx, 2202754fe60SDimitry Andric const TargetRegisterInfo *TRI); 2212754fe60SDimitry Andric 222bd5abe19SDimitry Andric /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is 223bd5abe19SDimitry Andric /// live. Returns true if any changes were made. 224bd5abe19SDimitry Andric bool splitRegister(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs); 225bd5abe19SDimitry Andric 2262754fe60SDimitry Andric /// rewriteLocations - Rewrite virtual register locations according to the 2272754fe60SDimitry Andric /// provided virtual register map. 2282754fe60SDimitry Andric void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI); 2292754fe60SDimitry Andric 2302754fe60SDimitry Andric /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures. 2312754fe60SDimitry Andric void emitDebugValues(VirtRegMap *VRM, 2322754fe60SDimitry Andric LiveIntervals &LIS, const TargetInstrInfo &TRI); 2332754fe60SDimitry Andric 2342754fe60SDimitry Andric /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A 2352754fe60SDimitry Andric /// variable may have more than one corresponding DBG_VALUE instructions. 2362754fe60SDimitry Andric /// Only first one needs DebugLoc to identify variable's lexical scope 2372754fe60SDimitry Andric /// in source file. 2382754fe60SDimitry Andric DebugLoc findDebugLoc(); 239bd5abe19SDimitry Andric void print(raw_ostream&, const TargetMachine*); 2402754fe60SDimitry Andric }; 2412754fe60SDimitry Andric } // namespace 2422754fe60SDimitry Andric 2432754fe60SDimitry Andric /// LDVImpl - Implementation of the LiveDebugVariables pass. 2442754fe60SDimitry Andric namespace { 2452754fe60SDimitry Andric class LDVImpl { 2462754fe60SDimitry Andric LiveDebugVariables &pass; 2472754fe60SDimitry Andric LocMap::Allocator allocator; 2482754fe60SDimitry Andric MachineFunction *MF; 2492754fe60SDimitry Andric LiveIntervals *LIS; 2502754fe60SDimitry Andric MachineDominatorTree *MDT; 2512754fe60SDimitry Andric const TargetRegisterInfo *TRI; 2522754fe60SDimitry Andric 2532754fe60SDimitry Andric /// userValues - All allocated UserValue instances. 2542754fe60SDimitry Andric SmallVector<UserValue*, 8> userValues; 2552754fe60SDimitry Andric 2562754fe60SDimitry Andric /// Map virtual register to eq class leader. 2572754fe60SDimitry Andric typedef DenseMap<unsigned, UserValue*> VRMap; 2582754fe60SDimitry Andric VRMap virtRegToEqClass; 2592754fe60SDimitry Andric 2602754fe60SDimitry Andric /// Map user variable to eq class leader. 2612754fe60SDimitry Andric typedef DenseMap<const MDNode *, UserValue*> UVMap; 2622754fe60SDimitry Andric UVMap userVarMap; 2632754fe60SDimitry Andric 2642754fe60SDimitry Andric /// getUserValue - Find or create a UserValue. 2652754fe60SDimitry Andric UserValue *getUserValue(const MDNode *Var, unsigned Offset, DebugLoc DL); 2662754fe60SDimitry Andric 2672754fe60SDimitry Andric /// lookupVirtReg - Find the EC leader for VirtReg or null. 2682754fe60SDimitry Andric UserValue *lookupVirtReg(unsigned VirtReg); 2692754fe60SDimitry Andric 2702754fe60SDimitry Andric /// handleDebugValue - Add DBG_VALUE instruction to our maps. 2712754fe60SDimitry Andric /// @param MI DBG_VALUE instruction 2722754fe60SDimitry Andric /// @param Idx Last valid SLotIndex before instruction. 2732754fe60SDimitry Andric /// @return True if the DBG_VALUE instruction should be deleted. 2742754fe60SDimitry Andric bool handleDebugValue(MachineInstr *MI, SlotIndex Idx); 2752754fe60SDimitry Andric 2762754fe60SDimitry Andric /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding 2772754fe60SDimitry Andric /// a UserValue def for each instruction. 2782754fe60SDimitry Andric /// @param mf MachineFunction to be scanned. 2792754fe60SDimitry Andric /// @return True if any debug values were found. 2802754fe60SDimitry Andric bool collectDebugValues(MachineFunction &mf); 2812754fe60SDimitry Andric 2822754fe60SDimitry Andric /// computeIntervals - Compute the live intervals of all user values after 2832754fe60SDimitry Andric /// collecting all their def points. 2842754fe60SDimitry Andric void computeIntervals(); 2852754fe60SDimitry Andric 2862754fe60SDimitry Andric public: 2872754fe60SDimitry Andric LDVImpl(LiveDebugVariables *ps) : pass(*ps) {} 2882754fe60SDimitry Andric bool runOnMachineFunction(MachineFunction &mf); 2892754fe60SDimitry Andric 2902754fe60SDimitry Andric /// clear - Relase all memory. 2912754fe60SDimitry Andric void clear() { 2922754fe60SDimitry Andric DeleteContainerPointers(userValues); 2932754fe60SDimitry Andric userValues.clear(); 2942754fe60SDimitry Andric virtRegToEqClass.clear(); 2952754fe60SDimitry Andric userVarMap.clear(); 2962754fe60SDimitry Andric } 2972754fe60SDimitry Andric 2983b0f4066SDimitry Andric /// mapVirtReg - Map virtual register to an equivalence class. 2993b0f4066SDimitry Andric void mapVirtReg(unsigned VirtReg, UserValue *EC); 3003b0f4066SDimitry Andric 3013b0f4066SDimitry Andric /// renameRegister - Replace all references to OldReg with NewReg:SubIdx. 3022754fe60SDimitry Andric void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx); 3032754fe60SDimitry Andric 304bd5abe19SDimitry Andric /// splitRegister - Replace all references to OldReg with NewRegs. 305bd5abe19SDimitry Andric void splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs); 306bd5abe19SDimitry Andric 3072754fe60SDimitry Andric /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures. 3082754fe60SDimitry Andric void emitDebugValues(VirtRegMap *VRM); 3092754fe60SDimitry Andric 3102754fe60SDimitry Andric void print(raw_ostream&); 3112754fe60SDimitry Andric }; 3122754fe60SDimitry Andric } // namespace 3132754fe60SDimitry Andric 314bd5abe19SDimitry Andric void UserValue::print(raw_ostream &OS, const TargetMachine *TM) { 3152754fe60SDimitry Andric if (const MDString *MDS = dyn_cast<MDString>(variable->getOperand(2))) 3162754fe60SDimitry Andric OS << "!\"" << MDS->getString() << "\"\t"; 3172754fe60SDimitry Andric if (offset) 3182754fe60SDimitry Andric OS << '+' << offset; 3192754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { 3202754fe60SDimitry Andric OS << " [" << I.start() << ';' << I.stop() << "):"; 3212754fe60SDimitry Andric if (I.value() == ~0u) 3222754fe60SDimitry Andric OS << "undef"; 3232754fe60SDimitry Andric else 3242754fe60SDimitry Andric OS << I.value(); 3252754fe60SDimitry Andric } 326bd5abe19SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) { 327bd5abe19SDimitry Andric OS << " Loc" << i << '='; 328bd5abe19SDimitry Andric locations[i].print(OS, TM); 329bd5abe19SDimitry Andric } 3302754fe60SDimitry Andric OS << '\n'; 3312754fe60SDimitry Andric } 3322754fe60SDimitry Andric 3332754fe60SDimitry Andric void LDVImpl::print(raw_ostream &OS) { 3342754fe60SDimitry Andric OS << "********** DEBUG VARIABLES **********\n"; 3352754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) 336bd5abe19SDimitry Andric userValues[i]->print(OS, &MF->getTarget()); 3372754fe60SDimitry Andric } 3382754fe60SDimitry Andric 3392754fe60SDimitry Andric void UserValue::coalesceLocation(unsigned LocNo) { 3402754fe60SDimitry Andric unsigned KeepLoc = 0; 3412754fe60SDimitry Andric for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) { 3422754fe60SDimitry Andric if (KeepLoc == LocNo) 3432754fe60SDimitry Andric continue; 3442754fe60SDimitry Andric if (locations[KeepLoc].isIdenticalTo(locations[LocNo])) 3452754fe60SDimitry Andric break; 3462754fe60SDimitry Andric } 3472754fe60SDimitry Andric // No matches. 3482754fe60SDimitry Andric if (KeepLoc == locations.size()) 3492754fe60SDimitry Andric return; 3502754fe60SDimitry Andric 3512754fe60SDimitry Andric // Keep the smaller location, erase the larger one. 3522754fe60SDimitry Andric unsigned EraseLoc = LocNo; 3532754fe60SDimitry Andric if (KeepLoc > EraseLoc) 3542754fe60SDimitry Andric std::swap(KeepLoc, EraseLoc); 3552754fe60SDimitry Andric locations.erase(locations.begin() + EraseLoc); 3562754fe60SDimitry Andric 3572754fe60SDimitry Andric // Rewrite values. 3582754fe60SDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { 3592754fe60SDimitry Andric unsigned v = I.value(); 3602754fe60SDimitry Andric if (v == EraseLoc) 3612754fe60SDimitry Andric I.setValue(KeepLoc); // Coalesce when possible. 3622754fe60SDimitry Andric else if (v > EraseLoc) 3632754fe60SDimitry Andric I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values. 3642754fe60SDimitry Andric } 3652754fe60SDimitry Andric } 3662754fe60SDimitry Andric 3673b0f4066SDimitry Andric void UserValue::mapVirtRegs(LDVImpl *LDV) { 3683b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 3693b0f4066SDimitry Andric if (locations[i].isReg() && 3703b0f4066SDimitry Andric TargetRegisterInfo::isVirtualRegister(locations[i].getReg())) 3713b0f4066SDimitry Andric LDV->mapVirtReg(locations[i].getReg(), this); 3723b0f4066SDimitry Andric } 3733b0f4066SDimitry Andric 3742754fe60SDimitry Andric UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset, 3752754fe60SDimitry Andric DebugLoc DL) { 3762754fe60SDimitry Andric UserValue *&Leader = userVarMap[Var]; 3772754fe60SDimitry Andric if (Leader) { 3782754fe60SDimitry Andric UserValue *UV = Leader->getLeader(); 3792754fe60SDimitry Andric Leader = UV; 3802754fe60SDimitry Andric for (; UV; UV = UV->getNext()) 3812754fe60SDimitry Andric if (UV->match(Var, Offset)) 3822754fe60SDimitry Andric return UV; 3832754fe60SDimitry Andric } 3842754fe60SDimitry Andric 3852754fe60SDimitry Andric UserValue *UV = new UserValue(Var, Offset, DL, allocator); 3862754fe60SDimitry Andric userValues.push_back(UV); 3872754fe60SDimitry Andric Leader = UserValue::merge(Leader, UV); 3882754fe60SDimitry Andric return UV; 3892754fe60SDimitry Andric } 3902754fe60SDimitry Andric 3912754fe60SDimitry Andric void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) { 3922754fe60SDimitry Andric assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs"); 3932754fe60SDimitry Andric UserValue *&Leader = virtRegToEqClass[VirtReg]; 3942754fe60SDimitry Andric Leader = UserValue::merge(Leader, EC); 3952754fe60SDimitry Andric } 3962754fe60SDimitry Andric 3972754fe60SDimitry Andric UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) { 3982754fe60SDimitry Andric if (UserValue *UV = virtRegToEqClass.lookup(VirtReg)) 3992754fe60SDimitry Andric return UV->getLeader(); 4002754fe60SDimitry Andric return 0; 4012754fe60SDimitry Andric } 4022754fe60SDimitry Andric 4032754fe60SDimitry Andric bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) { 4042754fe60SDimitry Andric // DBG_VALUE loc, offset, variable 4052754fe60SDimitry Andric if (MI->getNumOperands() != 3 || 4062754fe60SDimitry Andric !MI->getOperand(1).isImm() || !MI->getOperand(2).isMetadata()) { 4072754fe60SDimitry Andric DEBUG(dbgs() << "Can't handle " << *MI); 4082754fe60SDimitry Andric return false; 4092754fe60SDimitry Andric } 4102754fe60SDimitry Andric 4112754fe60SDimitry Andric // Get or create the UserValue for (variable,offset). 4122754fe60SDimitry Andric unsigned Offset = MI->getOperand(1).getImm(); 4132754fe60SDimitry Andric const MDNode *Var = MI->getOperand(2).getMetadata(); 4142754fe60SDimitry Andric UserValue *UV = getUserValue(Var, Offset, MI->getDebugLoc()); 4152754fe60SDimitry Andric UV->addDef(Idx, MI->getOperand(0)); 4162754fe60SDimitry Andric return true; 4172754fe60SDimitry Andric } 4182754fe60SDimitry Andric 4192754fe60SDimitry Andric bool LDVImpl::collectDebugValues(MachineFunction &mf) { 4202754fe60SDimitry Andric bool Changed = false; 4212754fe60SDimitry Andric for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE; 4222754fe60SDimitry Andric ++MFI) { 4232754fe60SDimitry Andric MachineBasicBlock *MBB = MFI; 4242754fe60SDimitry Andric for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); 4252754fe60SDimitry Andric MBBI != MBBE;) { 4262754fe60SDimitry Andric if (!MBBI->isDebugValue()) { 4272754fe60SDimitry Andric ++MBBI; 4282754fe60SDimitry Andric continue; 4292754fe60SDimitry Andric } 4302754fe60SDimitry Andric // DBG_VALUE has no slot index, use the previous instruction instead. 4312754fe60SDimitry Andric SlotIndex Idx = MBBI == MBB->begin() ? 4322754fe60SDimitry Andric LIS->getMBBStartIdx(MBB) : 4332754fe60SDimitry Andric LIS->getInstructionIndex(llvm::prior(MBBI)).getDefIndex(); 4342754fe60SDimitry Andric // Handle consecutive DBG_VALUE instructions with the same slot index. 4352754fe60SDimitry Andric do { 4362754fe60SDimitry Andric if (handleDebugValue(MBBI, Idx)) { 4372754fe60SDimitry Andric MBBI = MBB->erase(MBBI); 4382754fe60SDimitry Andric Changed = true; 4392754fe60SDimitry Andric } else 4402754fe60SDimitry Andric ++MBBI; 4412754fe60SDimitry Andric } while (MBBI != MBBE && MBBI->isDebugValue()); 4422754fe60SDimitry Andric } 4432754fe60SDimitry Andric } 4442754fe60SDimitry Andric return Changed; 4452754fe60SDimitry Andric } 4462754fe60SDimitry Andric 4472754fe60SDimitry Andric void UserValue::extendDef(SlotIndex Idx, unsigned LocNo, 4482754fe60SDimitry Andric LiveInterval *LI, const VNInfo *VNI, 4493b0f4066SDimitry Andric SmallVectorImpl<SlotIndex> *Kills, 4502754fe60SDimitry Andric LiveIntervals &LIS, MachineDominatorTree &MDT) { 4512754fe60SDimitry Andric SmallVector<SlotIndex, 16> Todo; 4522754fe60SDimitry Andric Todo.push_back(Idx); 4532754fe60SDimitry Andric 4542754fe60SDimitry Andric do { 4552754fe60SDimitry Andric SlotIndex Start = Todo.pop_back_val(); 4562754fe60SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start); 4572754fe60SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(MBB); 4582754fe60SDimitry Andric LocMap::iterator I = locInts.find(Start); 4592754fe60SDimitry Andric 4602754fe60SDimitry Andric // Limit to VNI's live range. 4612754fe60SDimitry Andric bool ToEnd = true; 4622754fe60SDimitry Andric if (LI && VNI) { 4632754fe60SDimitry Andric LiveRange *Range = LI->getLiveRangeContaining(Start); 4643b0f4066SDimitry Andric if (!Range || Range->valno != VNI) { 4653b0f4066SDimitry Andric if (Kills) 4663b0f4066SDimitry Andric Kills->push_back(Start); 4672754fe60SDimitry Andric continue; 4683b0f4066SDimitry Andric } 4692754fe60SDimitry Andric if (Range->end < Stop) 4702754fe60SDimitry Andric Stop = Range->end, ToEnd = false; 4712754fe60SDimitry Andric } 4722754fe60SDimitry Andric 4732754fe60SDimitry Andric // There could already be a short def at Start. 4742754fe60SDimitry Andric if (I.valid() && I.start() <= Start) { 4752754fe60SDimitry Andric // Stop when meeting a different location or an already extended interval. 4762754fe60SDimitry Andric Start = Start.getNextSlot(); 4772754fe60SDimitry Andric if (I.value() != LocNo || I.stop() != Start) 4782754fe60SDimitry Andric continue; 4792754fe60SDimitry Andric // This is a one-slot placeholder. Just skip it. 4802754fe60SDimitry Andric ++I; 4812754fe60SDimitry Andric } 4822754fe60SDimitry Andric 4832754fe60SDimitry Andric // Limited by the next def. 4842754fe60SDimitry Andric if (I.valid() && I.start() < Stop) 4852754fe60SDimitry Andric Stop = I.start(), ToEnd = false; 4863b0f4066SDimitry Andric // Limited by VNI's live range. 4873b0f4066SDimitry Andric else if (!ToEnd && Kills) 4883b0f4066SDimitry Andric Kills->push_back(Stop); 4892754fe60SDimitry Andric 4902754fe60SDimitry Andric if (Start >= Stop) 4912754fe60SDimitry Andric continue; 4922754fe60SDimitry Andric 4932754fe60SDimitry Andric I.insert(Start, Stop, LocNo); 4942754fe60SDimitry Andric 4952754fe60SDimitry Andric // If we extended to the MBB end, propagate down the dominator tree. 4962754fe60SDimitry Andric if (!ToEnd) 4972754fe60SDimitry Andric continue; 4982754fe60SDimitry Andric const std::vector<MachineDomTreeNode*> &Children = 4992754fe60SDimitry Andric MDT.getNode(MBB)->getChildren(); 5002754fe60SDimitry Andric for (unsigned i = 0, e = Children.size(); i != e; ++i) 5012754fe60SDimitry Andric Todo.push_back(LIS.getMBBStartIdx(Children[i]->getBlock())); 5022754fe60SDimitry Andric } while (!Todo.empty()); 5032754fe60SDimitry Andric } 5042754fe60SDimitry Andric 5052754fe60SDimitry Andric void 5063b0f4066SDimitry Andric UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 5073b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 5083b0f4066SDimitry Andric SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 5093b0f4066SDimitry Andric MachineRegisterInfo &MRI, LiveIntervals &LIS) { 5103b0f4066SDimitry Andric if (Kills.empty()) 5113b0f4066SDimitry Andric return; 5123b0f4066SDimitry Andric // Don't track copies from physregs, there are too many uses. 5133b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(LI->reg)) 5143b0f4066SDimitry Andric return; 5153b0f4066SDimitry Andric 5163b0f4066SDimitry Andric // Collect all the (vreg, valno) pairs that are copies of LI. 5173b0f4066SDimitry Andric SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues; 5183b0f4066SDimitry Andric for (MachineRegisterInfo::use_nodbg_iterator 5193b0f4066SDimitry Andric UI = MRI.use_nodbg_begin(LI->reg), 5203b0f4066SDimitry Andric UE = MRI.use_nodbg_end(); UI != UE; ++UI) { 5213b0f4066SDimitry Andric // Copies of the full value. 5223b0f4066SDimitry Andric if (UI.getOperand().getSubReg() || !UI->isCopy()) 5233b0f4066SDimitry Andric continue; 5243b0f4066SDimitry Andric MachineInstr *MI = &*UI; 5253b0f4066SDimitry Andric unsigned DstReg = MI->getOperand(0).getReg(); 5263b0f4066SDimitry Andric 5273b0f4066SDimitry Andric // Don't follow copies to physregs. These are usually setting up call 5283b0f4066SDimitry Andric // arguments, and the argument registers are always call clobbered. We are 5293b0f4066SDimitry Andric // better off in the source register which could be a callee-saved register, 5303b0f4066SDimitry Andric // or it could be spilled. 5313b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(DstReg)) 5323b0f4066SDimitry Andric continue; 5333b0f4066SDimitry Andric 5343b0f4066SDimitry Andric // Is LocNo extended to reach this copy? If not, another def may be blocking 5353b0f4066SDimitry Andric // it, or we are looking at a wrong value of LI. 5363b0f4066SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(MI); 5373b0f4066SDimitry Andric LocMap::iterator I = locInts.find(Idx.getUseIndex()); 5383b0f4066SDimitry Andric if (!I.valid() || I.value() != LocNo) 5393b0f4066SDimitry Andric continue; 5403b0f4066SDimitry Andric 5413b0f4066SDimitry Andric if (!LIS.hasInterval(DstReg)) 5423b0f4066SDimitry Andric continue; 5433b0f4066SDimitry Andric LiveInterval *DstLI = &LIS.getInterval(DstReg); 5443b0f4066SDimitry Andric const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getDefIndex()); 5453b0f4066SDimitry Andric assert(DstVNI && DstVNI->def == Idx.getDefIndex() && "Bad copy value"); 5463b0f4066SDimitry Andric CopyValues.push_back(std::make_pair(DstLI, DstVNI)); 5473b0f4066SDimitry Andric } 5483b0f4066SDimitry Andric 5493b0f4066SDimitry Andric if (CopyValues.empty()) 5503b0f4066SDimitry Andric return; 5513b0f4066SDimitry Andric 5523b0f4066SDimitry Andric DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n'); 5533b0f4066SDimitry Andric 5543b0f4066SDimitry Andric // Try to add defs of the copied values for each kill point. 5553b0f4066SDimitry Andric for (unsigned i = 0, e = Kills.size(); i != e; ++i) { 5563b0f4066SDimitry Andric SlotIndex Idx = Kills[i]; 5573b0f4066SDimitry Andric for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) { 5583b0f4066SDimitry Andric LiveInterval *DstLI = CopyValues[j].first; 5593b0f4066SDimitry Andric const VNInfo *DstVNI = CopyValues[j].second; 5603b0f4066SDimitry Andric if (DstLI->getVNInfoAt(Idx) != DstVNI) 5613b0f4066SDimitry Andric continue; 5623b0f4066SDimitry Andric // Check that there isn't already a def at Idx 5633b0f4066SDimitry Andric LocMap::iterator I = locInts.find(Idx); 5643b0f4066SDimitry Andric if (I.valid() && I.start() <= Idx) 5653b0f4066SDimitry Andric continue; 5663b0f4066SDimitry Andric DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" 5673b0f4066SDimitry Andric << DstVNI->id << " in " << *DstLI << '\n'); 5683b0f4066SDimitry Andric MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); 5693b0f4066SDimitry Andric assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); 5703b0f4066SDimitry Andric unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); 5713b0f4066SDimitry Andric I.insert(Idx, Idx.getNextSlot(), LocNo); 5723b0f4066SDimitry Andric NewDefs.push_back(std::make_pair(Idx, LocNo)); 5733b0f4066SDimitry Andric break; 5743b0f4066SDimitry Andric } 5753b0f4066SDimitry Andric } 5763b0f4066SDimitry Andric } 5773b0f4066SDimitry Andric 5783b0f4066SDimitry Andric void 5793b0f4066SDimitry Andric UserValue::computeIntervals(MachineRegisterInfo &MRI, 5803b0f4066SDimitry Andric LiveIntervals &LIS, 5813b0f4066SDimitry Andric MachineDominatorTree &MDT) { 5822754fe60SDimitry Andric SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs; 5832754fe60SDimitry Andric 5842754fe60SDimitry Andric // Collect all defs to be extended (Skipping undefs). 5852754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) 5862754fe60SDimitry Andric if (I.value() != ~0u) 5872754fe60SDimitry Andric Defs.push_back(std::make_pair(I.start(), I.value())); 5882754fe60SDimitry Andric 5893b0f4066SDimitry Andric // Extend all defs, and possibly add new ones along the way. 5903b0f4066SDimitry Andric for (unsigned i = 0; i != Defs.size(); ++i) { 5912754fe60SDimitry Andric SlotIndex Idx = Defs[i].first; 5922754fe60SDimitry Andric unsigned LocNo = Defs[i].second; 5932754fe60SDimitry Andric const MachineOperand &Loc = locations[LocNo]; 5942754fe60SDimitry Andric 5952754fe60SDimitry Andric // Register locations are constrained to where the register value is live. 5962754fe60SDimitry Andric if (Loc.isReg() && LIS.hasInterval(Loc.getReg())) { 5972754fe60SDimitry Andric LiveInterval *LI = &LIS.getInterval(Loc.getReg()); 5982754fe60SDimitry Andric const VNInfo *VNI = LI->getVNInfoAt(Idx); 5993b0f4066SDimitry Andric SmallVector<SlotIndex, 16> Kills; 6003b0f4066SDimitry Andric extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT); 6013b0f4066SDimitry Andric addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS); 6022754fe60SDimitry Andric } else 6033b0f4066SDimitry Andric extendDef(Idx, LocNo, 0, 0, 0, LIS, MDT); 6042754fe60SDimitry Andric } 6052754fe60SDimitry Andric 6062754fe60SDimitry Andric // Finally, erase all the undefs. 6072754fe60SDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid();) 6082754fe60SDimitry Andric if (I.value() == ~0u) 6092754fe60SDimitry Andric I.erase(); 6102754fe60SDimitry Andric else 6112754fe60SDimitry Andric ++I; 6122754fe60SDimitry Andric } 6132754fe60SDimitry Andric 6142754fe60SDimitry Andric void LDVImpl::computeIntervals() { 6153b0f4066SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 6163b0f4066SDimitry Andric userValues[i]->computeIntervals(MF->getRegInfo(), *LIS, *MDT); 6173b0f4066SDimitry Andric userValues[i]->mapVirtRegs(this); 6183b0f4066SDimitry Andric } 6192754fe60SDimitry Andric } 6202754fe60SDimitry Andric 6212754fe60SDimitry Andric bool LDVImpl::runOnMachineFunction(MachineFunction &mf) { 6222754fe60SDimitry Andric MF = &mf; 6232754fe60SDimitry Andric LIS = &pass.getAnalysis<LiveIntervals>(); 6242754fe60SDimitry Andric MDT = &pass.getAnalysis<MachineDominatorTree>(); 6252754fe60SDimitry Andric TRI = mf.getTarget().getRegisterInfo(); 6262754fe60SDimitry Andric clear(); 6272754fe60SDimitry Andric DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " 6282754fe60SDimitry Andric << ((Value*)mf.getFunction())->getName() 6292754fe60SDimitry Andric << " **********\n"); 6302754fe60SDimitry Andric 6312754fe60SDimitry Andric bool Changed = collectDebugValues(mf); 6322754fe60SDimitry Andric computeIntervals(); 6332754fe60SDimitry Andric DEBUG(print(dbgs())); 6342754fe60SDimitry Andric return Changed; 6352754fe60SDimitry Andric } 6362754fe60SDimitry Andric 6372754fe60SDimitry Andric bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) { 6382754fe60SDimitry Andric if (!EnableLDV) 6392754fe60SDimitry Andric return false; 6402754fe60SDimitry Andric if (!pImpl) 6412754fe60SDimitry Andric pImpl = new LDVImpl(this); 6422754fe60SDimitry Andric return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf); 6432754fe60SDimitry Andric } 6442754fe60SDimitry Andric 6452754fe60SDimitry Andric void LiveDebugVariables::releaseMemory() { 6462754fe60SDimitry Andric if (pImpl) 6472754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->clear(); 6482754fe60SDimitry Andric } 6492754fe60SDimitry Andric 6502754fe60SDimitry Andric LiveDebugVariables::~LiveDebugVariables() { 6512754fe60SDimitry Andric if (pImpl) 6522754fe60SDimitry Andric delete static_cast<LDVImpl*>(pImpl); 6532754fe60SDimitry Andric } 6542754fe60SDimitry Andric 6552754fe60SDimitry Andric void UserValue:: 6562754fe60SDimitry Andric renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx, 6572754fe60SDimitry Andric const TargetRegisterInfo *TRI) { 6582754fe60SDimitry Andric for (unsigned i = locations.size(); i; --i) { 6592754fe60SDimitry Andric unsigned LocNo = i - 1; 6602754fe60SDimitry Andric MachineOperand &Loc = locations[LocNo]; 6612754fe60SDimitry Andric if (!Loc.isReg() || Loc.getReg() != OldReg) 6622754fe60SDimitry Andric continue; 6632754fe60SDimitry Andric if (TargetRegisterInfo::isPhysicalRegister(NewReg)) 6642754fe60SDimitry Andric Loc.substPhysReg(NewReg, *TRI); 6652754fe60SDimitry Andric else 6662754fe60SDimitry Andric Loc.substVirtReg(NewReg, SubIdx, *TRI); 6672754fe60SDimitry Andric coalesceLocation(LocNo); 6682754fe60SDimitry Andric } 6692754fe60SDimitry Andric } 6702754fe60SDimitry Andric 6712754fe60SDimitry Andric void LDVImpl:: 6722754fe60SDimitry Andric renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) { 6732754fe60SDimitry Andric UserValue *UV = lookupVirtReg(OldReg); 6742754fe60SDimitry Andric if (!UV) 6752754fe60SDimitry Andric return; 6762754fe60SDimitry Andric 6772754fe60SDimitry Andric if (TargetRegisterInfo::isVirtualRegister(NewReg)) 6782754fe60SDimitry Andric mapVirtReg(NewReg, UV); 6792754fe60SDimitry Andric virtRegToEqClass.erase(OldReg); 6802754fe60SDimitry Andric 6812754fe60SDimitry Andric do { 6822754fe60SDimitry Andric UV->renameRegister(OldReg, NewReg, SubIdx, TRI); 6832754fe60SDimitry Andric UV = UV->getNext(); 6842754fe60SDimitry Andric } while (UV); 6852754fe60SDimitry Andric } 6862754fe60SDimitry Andric 6872754fe60SDimitry Andric void LiveDebugVariables:: 6882754fe60SDimitry Andric renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) { 6892754fe60SDimitry Andric if (pImpl) 6902754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->renameRegister(OldReg, NewReg, SubIdx); 6912754fe60SDimitry Andric } 6922754fe60SDimitry Andric 693bd5abe19SDimitry Andric //===----------------------------------------------------------------------===// 694bd5abe19SDimitry Andric // Live Range Splitting 695bd5abe19SDimitry Andric //===----------------------------------------------------------------------===// 696bd5abe19SDimitry Andric 697bd5abe19SDimitry Andric bool 698bd5abe19SDimitry Andric UserValue::splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs) { 699bd5abe19SDimitry Andric DEBUG({ 700bd5abe19SDimitry Andric dbgs() << "Splitting Loc" << OldLocNo << '\t'; 701bd5abe19SDimitry Andric print(dbgs(), 0); 702bd5abe19SDimitry Andric }); 703bd5abe19SDimitry Andric bool DidChange = false; 704bd5abe19SDimitry Andric LocMap::iterator LocMapI; 705bd5abe19SDimitry Andric LocMapI.setMap(locInts); 706bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) { 707bd5abe19SDimitry Andric LiveInterval *LI = NewRegs[i]; 708bd5abe19SDimitry Andric if (LI->empty()) 709bd5abe19SDimitry Andric continue; 710bd5abe19SDimitry Andric 711bd5abe19SDimitry Andric // Don't allocate the new LocNo until it is needed. 712bd5abe19SDimitry Andric unsigned NewLocNo = ~0u; 713bd5abe19SDimitry Andric 714bd5abe19SDimitry Andric // Iterate over the overlaps between locInts and LI. 715bd5abe19SDimitry Andric LocMapI.find(LI->beginIndex()); 716bd5abe19SDimitry Andric if (!LocMapI.valid()) 717bd5abe19SDimitry Andric continue; 718bd5abe19SDimitry Andric LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start()); 719bd5abe19SDimitry Andric LiveInterval::iterator LIE = LI->end(); 720bd5abe19SDimitry Andric while (LocMapI.valid() && LII != LIE) { 721bd5abe19SDimitry Andric // At this point, we know that LocMapI.stop() > LII->start. 722bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 723bd5abe19SDimitry Andric if (LII == LIE) 724bd5abe19SDimitry Andric break; 725bd5abe19SDimitry Andric 726bd5abe19SDimitry Andric // Now LII->end > LocMapI.start(). Do we have an overlap? 727bd5abe19SDimitry Andric if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) { 728bd5abe19SDimitry Andric // Overlapping correct location. Allocate NewLocNo now. 729bd5abe19SDimitry Andric if (NewLocNo == ~0u) { 730bd5abe19SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(LI->reg, false); 731bd5abe19SDimitry Andric MO.setSubReg(locations[OldLocNo].getSubReg()); 732bd5abe19SDimitry Andric NewLocNo = getLocationNo(MO); 733bd5abe19SDimitry Andric DidChange = true; 734bd5abe19SDimitry Andric } 735bd5abe19SDimitry Andric 736bd5abe19SDimitry Andric SlotIndex LStart = LocMapI.start(); 737bd5abe19SDimitry Andric SlotIndex LStop = LocMapI.stop(); 738bd5abe19SDimitry Andric 739bd5abe19SDimitry Andric // Trim LocMapI down to the LII overlap. 740bd5abe19SDimitry Andric if (LStart < LII->start) 741bd5abe19SDimitry Andric LocMapI.setStartUnchecked(LII->start); 742bd5abe19SDimitry Andric if (LStop > LII->end) 743bd5abe19SDimitry Andric LocMapI.setStopUnchecked(LII->end); 744bd5abe19SDimitry Andric 745bd5abe19SDimitry Andric // Change the value in the overlap. This may trigger coalescing. 746bd5abe19SDimitry Andric LocMapI.setValue(NewLocNo); 747bd5abe19SDimitry Andric 748bd5abe19SDimitry Andric // Re-insert any removed OldLocNo ranges. 749bd5abe19SDimitry Andric if (LStart < LocMapI.start()) { 750bd5abe19SDimitry Andric LocMapI.insert(LStart, LocMapI.start(), OldLocNo); 751bd5abe19SDimitry Andric ++LocMapI; 752bd5abe19SDimitry Andric assert(LocMapI.valid() && "Unexpected coalescing"); 753bd5abe19SDimitry Andric } 754bd5abe19SDimitry Andric if (LStop > LocMapI.stop()) { 755bd5abe19SDimitry Andric ++LocMapI; 756bd5abe19SDimitry Andric LocMapI.insert(LII->end, LStop, OldLocNo); 757bd5abe19SDimitry Andric --LocMapI; 758bd5abe19SDimitry Andric } 759bd5abe19SDimitry Andric } 760bd5abe19SDimitry Andric 761bd5abe19SDimitry Andric // Advance to the next overlap. 762bd5abe19SDimitry Andric if (LII->end < LocMapI.stop()) { 763bd5abe19SDimitry Andric if (++LII == LIE) 764bd5abe19SDimitry Andric break; 765bd5abe19SDimitry Andric LocMapI.advanceTo(LII->start); 766bd5abe19SDimitry Andric } else { 767bd5abe19SDimitry Andric ++LocMapI; 768bd5abe19SDimitry Andric if (!LocMapI.valid()) 769bd5abe19SDimitry Andric break; 770bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 771bd5abe19SDimitry Andric } 772bd5abe19SDimitry Andric } 773bd5abe19SDimitry Andric } 774bd5abe19SDimitry Andric 775bd5abe19SDimitry Andric // Finally, remove any remaining OldLocNo intervals and OldLocNo itself. 776bd5abe19SDimitry Andric locations.erase(locations.begin() + OldLocNo); 777bd5abe19SDimitry Andric LocMapI.goToBegin(); 778bd5abe19SDimitry Andric while (LocMapI.valid()) { 779bd5abe19SDimitry Andric unsigned v = LocMapI.value(); 780bd5abe19SDimitry Andric if (v == OldLocNo) { 781bd5abe19SDimitry Andric DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';' 782bd5abe19SDimitry Andric << LocMapI.stop() << ")\n"); 783bd5abe19SDimitry Andric LocMapI.erase(); 784bd5abe19SDimitry Andric } else { 785bd5abe19SDimitry Andric if (v > OldLocNo) 786bd5abe19SDimitry Andric LocMapI.setValueUnchecked(v-1); 787bd5abe19SDimitry Andric ++LocMapI; 788bd5abe19SDimitry Andric } 789bd5abe19SDimitry Andric } 790bd5abe19SDimitry Andric 791bd5abe19SDimitry Andric DEBUG({dbgs() << "Split result: \t"; print(dbgs(), 0);}); 792bd5abe19SDimitry Andric return DidChange; 793bd5abe19SDimitry Andric } 794bd5abe19SDimitry Andric 795bd5abe19SDimitry Andric bool 796bd5abe19SDimitry Andric UserValue::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) { 797bd5abe19SDimitry Andric bool DidChange = false; 798bd5abe19SDimitry Andric // Split locations referring to OldReg. Iterate backwards so splitLocation can 799bd5abe19SDimitry Andric // safely erase unuused locations. 800bd5abe19SDimitry Andric for (unsigned i = locations.size(); i ; --i) { 801bd5abe19SDimitry Andric unsigned LocNo = i-1; 802bd5abe19SDimitry Andric const MachineOperand *Loc = &locations[LocNo]; 803bd5abe19SDimitry Andric if (!Loc->isReg() || Loc->getReg() != OldReg) 804bd5abe19SDimitry Andric continue; 805bd5abe19SDimitry Andric DidChange |= splitLocation(LocNo, NewRegs); 806bd5abe19SDimitry Andric } 807bd5abe19SDimitry Andric return DidChange; 808bd5abe19SDimitry Andric } 809bd5abe19SDimitry Andric 810bd5abe19SDimitry Andric void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) { 811bd5abe19SDimitry Andric bool DidChange = false; 812bd5abe19SDimitry Andric for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext()) 813bd5abe19SDimitry Andric DidChange |= UV->splitRegister(OldReg, NewRegs); 814bd5abe19SDimitry Andric 815bd5abe19SDimitry Andric if (!DidChange) 816bd5abe19SDimitry Andric return; 817bd5abe19SDimitry Andric 818bd5abe19SDimitry Andric // Map all of the new virtual registers. 819bd5abe19SDimitry Andric UserValue *UV = lookupVirtReg(OldReg); 820bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) 821bd5abe19SDimitry Andric mapVirtReg(NewRegs[i]->reg, UV); 822bd5abe19SDimitry Andric } 823bd5abe19SDimitry Andric 824bd5abe19SDimitry Andric void LiveDebugVariables:: 825bd5abe19SDimitry Andric splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) { 826bd5abe19SDimitry Andric if (pImpl) 827bd5abe19SDimitry Andric static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs); 828bd5abe19SDimitry Andric } 829bd5abe19SDimitry Andric 8302754fe60SDimitry Andric void 8312754fe60SDimitry Andric UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) { 8322754fe60SDimitry Andric // Iterate over locations in reverse makes it easier to handle coalescing. 8332754fe60SDimitry Andric for (unsigned i = locations.size(); i ; --i) { 8342754fe60SDimitry Andric unsigned LocNo = i-1; 8352754fe60SDimitry Andric MachineOperand &Loc = locations[LocNo]; 8362754fe60SDimitry Andric // Only virtual registers are rewritten. 8372754fe60SDimitry Andric if (!Loc.isReg() || !Loc.getReg() || 8382754fe60SDimitry Andric !TargetRegisterInfo::isVirtualRegister(Loc.getReg())) 8392754fe60SDimitry Andric continue; 8402754fe60SDimitry Andric unsigned VirtReg = Loc.getReg(); 8412754fe60SDimitry Andric if (VRM.isAssignedReg(VirtReg) && 8422754fe60SDimitry Andric TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) { 843bd5abe19SDimitry Andric // This can create a %noreg operand in rare cases when the sub-register 844bd5abe19SDimitry Andric // index is no longer available. That means the user value is in a 845bd5abe19SDimitry Andric // non-existent sub-register, and %noreg is exactly what we want. 8462754fe60SDimitry Andric Loc.substPhysReg(VRM.getPhys(VirtReg), TRI); 8472754fe60SDimitry Andric } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT && 8482754fe60SDimitry Andric VRM.isSpillSlotUsed(VRM.getStackSlot(VirtReg))) { 8492754fe60SDimitry Andric // FIXME: Translate SubIdx to a stackslot offset. 8502754fe60SDimitry Andric Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg)); 8512754fe60SDimitry Andric } else { 8522754fe60SDimitry Andric Loc.setReg(0); 8532754fe60SDimitry Andric Loc.setSubReg(0); 8542754fe60SDimitry Andric } 8552754fe60SDimitry Andric coalesceLocation(LocNo); 8562754fe60SDimitry Andric } 8572754fe60SDimitry Andric } 8582754fe60SDimitry Andric 8592754fe60SDimitry Andric /// findInsertLocation - Find an iterator for inserting a DBG_VALUE 8602754fe60SDimitry Andric /// instruction. 8612754fe60SDimitry Andric static MachineBasicBlock::iterator 8622754fe60SDimitry Andric findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, 8632754fe60SDimitry Andric LiveIntervals &LIS) { 8642754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 8652754fe60SDimitry Andric Idx = Idx.getBaseIndex(); 8662754fe60SDimitry Andric 8672754fe60SDimitry Andric // Try to find an insert location by going backwards from Idx. 8682754fe60SDimitry Andric MachineInstr *MI; 8692754fe60SDimitry Andric while (!(MI = LIS.getInstructionFromIndex(Idx))) { 8702754fe60SDimitry Andric // We've reached the beginning of MBB. 8712754fe60SDimitry Andric if (Idx == Start) { 8722754fe60SDimitry Andric MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin()); 8732754fe60SDimitry Andric return I; 8742754fe60SDimitry Andric } 8752754fe60SDimitry Andric Idx = Idx.getPrevIndex(); 8762754fe60SDimitry Andric } 8772754fe60SDimitry Andric 8782754fe60SDimitry Andric // Don't insert anything after the first terminator, though. 8792754fe60SDimitry Andric return MI->getDesc().isTerminator() ? MBB->getFirstTerminator() : 8802754fe60SDimitry Andric llvm::next(MachineBasicBlock::iterator(MI)); 8812754fe60SDimitry Andric } 8822754fe60SDimitry Andric 8832754fe60SDimitry Andric DebugLoc UserValue::findDebugLoc() { 8842754fe60SDimitry Andric DebugLoc D = dl; 8852754fe60SDimitry Andric dl = DebugLoc(); 8862754fe60SDimitry Andric return D; 8872754fe60SDimitry Andric } 8882754fe60SDimitry Andric void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, 8892754fe60SDimitry Andric unsigned LocNo, 8902754fe60SDimitry Andric LiveIntervals &LIS, 8912754fe60SDimitry Andric const TargetInstrInfo &TII) { 8922754fe60SDimitry Andric MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS); 8932754fe60SDimitry Andric MachineOperand &Loc = locations[LocNo]; 8942754fe60SDimitry Andric 8952754fe60SDimitry Andric // Frame index locations may require a target callback. 8962754fe60SDimitry Andric if (Loc.isFI()) { 8972754fe60SDimitry Andric MachineInstr *MI = TII.emitFrameIndexDebugValue(*MBB->getParent(), 8982754fe60SDimitry Andric Loc.getIndex(), offset, variable, 8992754fe60SDimitry Andric findDebugLoc()); 9002754fe60SDimitry Andric if (MI) { 9012754fe60SDimitry Andric MBB->insert(I, MI); 9022754fe60SDimitry Andric return; 9032754fe60SDimitry Andric } 9042754fe60SDimitry Andric } 9052754fe60SDimitry Andric // This is not a frame index, or the target is happy with a standard FI. 9062754fe60SDimitry Andric BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE)) 9072754fe60SDimitry Andric .addOperand(Loc).addImm(offset).addMetadata(variable); 9082754fe60SDimitry Andric } 9092754fe60SDimitry Andric 9102754fe60SDimitry Andric void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, 9112754fe60SDimitry Andric const TargetInstrInfo &TII) { 9122754fe60SDimitry Andric MachineFunction::iterator MFEnd = VRM->getMachineFunction().end(); 9132754fe60SDimitry Andric 9142754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid();) { 9152754fe60SDimitry Andric SlotIndex Start = I.start(); 9162754fe60SDimitry Andric SlotIndex Stop = I.stop(); 9172754fe60SDimitry Andric unsigned LocNo = I.value(); 9182754fe60SDimitry Andric DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo); 9192754fe60SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); 9202754fe60SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB); 9212754fe60SDimitry Andric 9222754fe60SDimitry Andric DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 9232754fe60SDimitry Andric insertDebugValue(MBB, Start, LocNo, LIS, TII); 9242754fe60SDimitry Andric 9252754fe60SDimitry Andric // This interval may span multiple basic blocks. 9262754fe60SDimitry Andric // Insert a DBG_VALUE into each one. 9272754fe60SDimitry Andric while(Stop > MBBEnd) { 9282754fe60SDimitry Andric // Move to the next block. 9292754fe60SDimitry Andric Start = MBBEnd; 9302754fe60SDimitry Andric if (++MBB == MFEnd) 9312754fe60SDimitry Andric break; 9322754fe60SDimitry Andric MBBEnd = LIS.getMBBEndIdx(MBB); 9332754fe60SDimitry Andric DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 9342754fe60SDimitry Andric insertDebugValue(MBB, Start, LocNo, LIS, TII); 9352754fe60SDimitry Andric } 9362754fe60SDimitry Andric DEBUG(dbgs() << '\n'); 9372754fe60SDimitry Andric if (MBB == MFEnd) 9382754fe60SDimitry Andric break; 9392754fe60SDimitry Andric 9402754fe60SDimitry Andric ++I; 9412754fe60SDimitry Andric } 9422754fe60SDimitry Andric } 9432754fe60SDimitry Andric 9442754fe60SDimitry Andric void LDVImpl::emitDebugValues(VirtRegMap *VRM) { 9452754fe60SDimitry Andric DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); 9462754fe60SDimitry Andric const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 9472754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 948bd5abe19SDimitry Andric DEBUG(userValues[i]->print(dbgs(), &MF->getTarget())); 9492754fe60SDimitry Andric userValues[i]->rewriteLocations(*VRM, *TRI); 9502754fe60SDimitry Andric userValues[i]->emitDebugValues(VRM, *LIS, *TII); 9512754fe60SDimitry Andric } 9522754fe60SDimitry Andric } 9532754fe60SDimitry Andric 9542754fe60SDimitry Andric void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) { 9552754fe60SDimitry Andric if (pImpl) 9562754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM); 9572754fe60SDimitry Andric } 9582754fe60SDimitry Andric 9592754fe60SDimitry Andric 9602754fe60SDimitry Andric #ifndef NDEBUG 9612754fe60SDimitry Andric void LiveDebugVariables::dump() { 9622754fe60SDimitry Andric if (pImpl) 9632754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->print(dbgs()); 9642754fe60SDimitry Andric } 9652754fe60SDimitry Andric #endif 9662754fe60SDimitry Andric 967