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 #include "LiveDebugVariables.h" 232cab237bSDimitry Andric #include "llvm/ADT/ArrayRef.h" 242cab237bSDimitry Andric #include "llvm/ADT/DenseMap.h" 252754fe60SDimitry Andric #include "llvm/ADT/IntervalMap.h" 262cab237bSDimitry Andric #include "llvm/ADT/STLExtras.h" 272cab237bSDimitry Andric #include "llvm/ADT/SmallSet.h" 282cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h" 296122f3e6SDimitry Andric #include "llvm/ADT/Statistic.h" 302cab237bSDimitry Andric #include "llvm/ADT/StringRef.h" 312cab237bSDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 322cab237bSDimitry Andric #include "llvm/CodeGen/LiveInterval.h" 332cab237bSDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 342cab237bSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 352754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 362754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 372cab237bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 382754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 392cab237bSDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 403b0f4066SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 412cab237bSDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 422cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 432cab237bSDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 442cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 452cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 46139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h" 472cab237bSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 482cab237bSDimitry Andric #include "llvm/IR/DebugLoc.h" 492cab237bSDimitry Andric #include "llvm/IR/Function.h" 50139f7f9bSDimitry Andric #include "llvm/IR/Metadata.h" 512cab237bSDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 522cab237bSDimitry Andric #include "llvm/Pass.h" 532cab237bSDimitry Andric #include "llvm/Support/Casting.h" 542754fe60SDimitry Andric #include "llvm/Support/CommandLine.h" 552cab237bSDimitry Andric #include "llvm/Support/Compiler.h" 562754fe60SDimitry Andric #include "llvm/Support/Debug.h" 57ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h" 582cab237bSDimitry Andric #include <algorithm> 592cab237bSDimitry Andric #include <cassert> 602cab237bSDimitry Andric #include <iterator> 6191bc56edSDimitry Andric #include <memory> 623ca95b02SDimitry Andric #include <utility> 6391bc56edSDimitry Andric 642754fe60SDimitry Andric using namespace llvm; 652754fe60SDimitry Andric 66302affcbSDimitry Andric #define DEBUG_TYPE "livedebugvars" 6791bc56edSDimitry Andric 682754fe60SDimitry Andric static cl::opt<bool> 692754fe60SDimitry Andric EnableLDV("live-debug-variables", cl::init(true), 702754fe60SDimitry Andric cl::desc("Enable the live debug variables pass"), cl::Hidden); 712754fe60SDimitry Andric 726122f3e6SDimitry Andric STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted"); 732cab237bSDimitry Andric 742754fe60SDimitry Andric char LiveDebugVariables::ID = 0; 752754fe60SDimitry Andric 76302affcbSDimitry Andric INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE, 772754fe60SDimitry Andric "Debug Variable Analysis", false, false) 782754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 792754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 80302affcbSDimitry Andric INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE, 812754fe60SDimitry Andric "Debug Variable Analysis", false, false) 822754fe60SDimitry Andric 832754fe60SDimitry Andric void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const { 842754fe60SDimitry Andric AU.addRequired<MachineDominatorTree>(); 852754fe60SDimitry Andric AU.addRequiredTransitive<LiveIntervals>(); 862754fe60SDimitry Andric AU.setPreservesAll(); 872754fe60SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 882754fe60SDimitry Andric } 892754fe60SDimitry Andric 902cab237bSDimitry Andric LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) { 912754fe60SDimitry Andric initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 922754fe60SDimitry Andric } 932754fe60SDimitry Andric 942cab237bSDimitry Andric enum : unsigned { UndefLocNo = ~0U }; 952cab237bSDimitry Andric 962cab237bSDimitry Andric /// Describes a location by number along with some flags about the original 972cab237bSDimitry Andric /// usage of the location. 982cab237bSDimitry Andric class DbgValueLocation { 992cab237bSDimitry Andric public: 1002cab237bSDimitry Andric DbgValueLocation(unsigned LocNo, bool WasIndirect) 1012cab237bSDimitry Andric : LocNo(LocNo), WasIndirect(WasIndirect) { 1022cab237bSDimitry Andric static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing"); 1032cab237bSDimitry Andric assert(locNo() == LocNo && "location truncation"); 1042cab237bSDimitry Andric } 1052cab237bSDimitry Andric 1062cab237bSDimitry Andric DbgValueLocation() : LocNo(0), WasIndirect(0) {} 1072cab237bSDimitry Andric 1082cab237bSDimitry Andric unsigned locNo() const { 1092cab237bSDimitry Andric // Fix up the undef location number, which gets truncated. 1102cab237bSDimitry Andric return LocNo == INT_MAX ? UndefLocNo : LocNo; 1112cab237bSDimitry Andric } 1122cab237bSDimitry Andric bool wasIndirect() const { return WasIndirect; } 1132cab237bSDimitry Andric bool isUndef() const { return locNo() == UndefLocNo; } 1142cab237bSDimitry Andric 1152cab237bSDimitry Andric DbgValueLocation changeLocNo(unsigned NewLocNo) const { 1162cab237bSDimitry Andric return DbgValueLocation(NewLocNo, WasIndirect); 1172cab237bSDimitry Andric } 1182cab237bSDimitry Andric 1192cab237bSDimitry Andric friend inline bool operator==(const DbgValueLocation &LHS, 1202cab237bSDimitry Andric const DbgValueLocation &RHS) { 1212cab237bSDimitry Andric return LHS.LocNo == RHS.LocNo && LHS.WasIndirect == RHS.WasIndirect; 1222cab237bSDimitry Andric } 1232cab237bSDimitry Andric 1242cab237bSDimitry Andric friend inline bool operator!=(const DbgValueLocation &LHS, 1252cab237bSDimitry Andric const DbgValueLocation &RHS) { 1262cab237bSDimitry Andric return !(LHS == RHS); 1272cab237bSDimitry Andric } 1282cab237bSDimitry Andric 1292cab237bSDimitry Andric private: 1302cab237bSDimitry Andric unsigned LocNo : 31; 1312cab237bSDimitry Andric unsigned WasIndirect : 1; 1322cab237bSDimitry Andric }; 1332cab237bSDimitry Andric 1342754fe60SDimitry Andric /// LocMap - Map of where a user value is live, and its location. 1352cab237bSDimitry Andric using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>; 1362cab237bSDimitry Andric 1372cab237bSDimitry Andric namespace { 1382cab237bSDimitry Andric 1392cab237bSDimitry Andric class LDVImpl; 1402754fe60SDimitry Andric 1412754fe60SDimitry Andric /// UserValue - A user value is a part of a debug info user variable. 1422754fe60SDimitry Andric /// 1432754fe60SDimitry Andric /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register 1442754fe60SDimitry Andric /// holds part of a user variable. The part is identified by a byte offset. 1452754fe60SDimitry Andric /// 1462754fe60SDimitry Andric /// UserValues are grouped into equivalence classes for easier searching. Two 1472754fe60SDimitry Andric /// user values are related if they refer to the same variable, or if they are 1482754fe60SDimitry Andric /// held by the same virtual register. The equivalence class is the transitive 1492754fe60SDimitry Andric /// closure of that relation. 1502754fe60SDimitry Andric class UserValue { 1512cab237bSDimitry Andric const DILocalVariable *Variable; ///< The debug info variable we are part of. 1522cab237bSDimitry Andric const DIExpression *Expression; ///< Any complex address expression. 1532754fe60SDimitry Andric DebugLoc dl; ///< The debug location for the variable. This is 1542754fe60SDimitry Andric ///< used by dwarf writer to find lexical scope. 1552754fe60SDimitry Andric UserValue *leader; ///< Equivalence class leader. 1562cab237bSDimitry Andric UserValue *next = nullptr; ///< Next value in equivalence class, or null. 1572754fe60SDimitry Andric 1582754fe60SDimitry Andric /// Numbered locations referenced by locmap. 1592754fe60SDimitry Andric SmallVector<MachineOperand, 4> locations; 1602754fe60SDimitry Andric 1612754fe60SDimitry Andric /// Map of slot indices where this value is live. 1622754fe60SDimitry Andric LocMap locInts; 1632754fe60SDimitry Andric 1642cab237bSDimitry Andric /// Set of interval start indexes that have been trimmed to the 1652cab237bSDimitry Andric /// lexical scope. 1662cab237bSDimitry Andric SmallSet<SlotIndex, 2> trimmedDefs; 1672754fe60SDimitry Andric 1682754fe60SDimitry Andric /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo. 1692cab237bSDimitry Andric void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx, 1702cab237bSDimitry Andric SlotIndex StopIdx, 1712cab237bSDimitry Andric DbgValueLocation Loc, bool Spilled, LiveIntervals &LIS, 1722cab237bSDimitry Andric const TargetInstrInfo &TII, 1732cab237bSDimitry Andric const TargetRegisterInfo &TRI); 1742754fe60SDimitry Andric 175bd5abe19SDimitry Andric /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs 176bd5abe19SDimitry Andric /// is live. Returns true if any changes were made. 177f785676fSDimitry Andric bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 178f785676fSDimitry Andric LiveIntervals &LIS); 179bd5abe19SDimitry Andric 1802754fe60SDimitry Andric public: 1812754fe60SDimitry Andric /// UserValue - Create a new UserValue. 1822cab237bSDimitry Andric UserValue(const DILocalVariable *var, const DIExpression *expr, DebugLoc L, 1832cab237bSDimitry Andric LocMap::Allocator &alloc) 1842cab237bSDimitry Andric : Variable(var), Expression(expr), dl(std::move(L)), leader(this), 1852cab237bSDimitry Andric locInts(alloc) {} 1862754fe60SDimitry Andric 1872754fe60SDimitry Andric /// getLeader - Get the leader of this value's equivalence class. 1882754fe60SDimitry Andric UserValue *getLeader() { 1892754fe60SDimitry Andric UserValue *l = leader; 1902754fe60SDimitry Andric while (l != l->leader) 1912754fe60SDimitry Andric l = l->leader; 1922754fe60SDimitry Andric return leader = l; 1932754fe60SDimitry Andric } 1942754fe60SDimitry Andric 1952754fe60SDimitry Andric /// getNext - Return the next UserValue in the equivalence class. 1962754fe60SDimitry Andric UserValue *getNext() const { return next; } 1972754fe60SDimitry Andric 19817a519f9SDimitry Andric /// match - Does this UserValue match the parameters? 1992cab237bSDimitry Andric bool match(const DILocalVariable *Var, const DIExpression *Expr, 2002cab237bSDimitry Andric const DILocation *IA) const { 2012cab237bSDimitry Andric // FIXME: The fragment should be part of the equivalence class, but not 2022cab237bSDimitry Andric // other things in the expression like stack values. 2032cab237bSDimitry Andric return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA; 2042754fe60SDimitry Andric } 2052754fe60SDimitry Andric 2062754fe60SDimitry Andric /// merge - Merge equivalence classes. 2072754fe60SDimitry Andric static UserValue *merge(UserValue *L1, UserValue *L2) { 2082754fe60SDimitry Andric L2 = L2->getLeader(); 2092754fe60SDimitry Andric if (!L1) 2102754fe60SDimitry Andric return L2; 2112754fe60SDimitry Andric L1 = L1->getLeader(); 2122754fe60SDimitry Andric if (L1 == L2) 2132754fe60SDimitry Andric return L1; 2142754fe60SDimitry Andric // Splice L2 before L1's members. 2152754fe60SDimitry Andric UserValue *End = L2; 2163ca95b02SDimitry Andric while (End->next) { 2173ca95b02SDimitry Andric End->leader = L1; 2183ca95b02SDimitry Andric End = End->next; 2193ca95b02SDimitry Andric } 2202754fe60SDimitry Andric End->leader = L1; 2212754fe60SDimitry Andric End->next = L1->next; 2222754fe60SDimitry Andric L1->next = L2; 2232754fe60SDimitry Andric return L1; 2242754fe60SDimitry Andric } 2252754fe60SDimitry Andric 2262754fe60SDimitry Andric /// getLocationNo - Return the location number that matches Loc. 2272754fe60SDimitry Andric unsigned getLocationNo(const MachineOperand &LocMO) { 2283b0f4066SDimitry Andric if (LocMO.isReg()) { 2293b0f4066SDimitry Andric if (LocMO.getReg() == 0) 2302cab237bSDimitry Andric return UndefLocNo; 2313b0f4066SDimitry Andric // For register locations we dont care about use/def and other flags. 2323b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 2333b0f4066SDimitry Andric if (locations[i].isReg() && 2343b0f4066SDimitry Andric locations[i].getReg() == LocMO.getReg() && 2353b0f4066SDimitry Andric locations[i].getSubReg() == LocMO.getSubReg()) 2363b0f4066SDimitry Andric return i; 2373b0f4066SDimitry Andric } else 2382754fe60SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 2392754fe60SDimitry Andric if (LocMO.isIdenticalTo(locations[i])) 2402754fe60SDimitry Andric return i; 2412754fe60SDimitry Andric locations.push_back(LocMO); 2422754fe60SDimitry Andric // We are storing a MachineOperand outside a MachineInstr. 2432754fe60SDimitry Andric locations.back().clearParent(); 2443b0f4066SDimitry Andric // Don't store def operands. 2453b0f4066SDimitry Andric if (locations.back().isReg()) 2463b0f4066SDimitry Andric locations.back().setIsUse(); 2472754fe60SDimitry Andric return locations.size() - 1; 2482754fe60SDimitry Andric } 2492754fe60SDimitry Andric 2503b0f4066SDimitry Andric /// mapVirtRegs - Ensure that all virtual register locations are mapped. 2513b0f4066SDimitry Andric void mapVirtRegs(LDVImpl *LDV); 2523b0f4066SDimitry Andric 2532754fe60SDimitry Andric /// addDef - Add a definition point to this value. 2542cab237bSDimitry Andric void addDef(SlotIndex Idx, const MachineOperand &LocMO, bool IsIndirect) { 2552cab237bSDimitry Andric DbgValueLocation Loc(getLocationNo(LocMO), IsIndirect); 2562754fe60SDimitry Andric // Add a singular (Idx,Idx) -> Loc mapping. 2572754fe60SDimitry Andric LocMap::iterator I = locInts.find(Idx); 2582754fe60SDimitry Andric if (!I.valid() || I.start() != Idx) 2592cab237bSDimitry Andric I.insert(Idx, Idx.getNextSlot(), Loc); 2606122f3e6SDimitry Andric else 2616122f3e6SDimitry Andric // A later DBG_VALUE at the same SlotIndex overrides the old location. 2622cab237bSDimitry Andric I.setValue(Loc); 2632754fe60SDimitry Andric } 2642754fe60SDimitry Andric 265d88c1a5aSDimitry Andric /// extendDef - Extend the current definition as far as possible down. 266d88c1a5aSDimitry Andric /// Stop when meeting an existing def or when leaving the live 2672754fe60SDimitry Andric /// range of VNI. 2683b0f4066SDimitry Andric /// End points where VNI is no longer live are added to Kills. 2692754fe60SDimitry Andric /// @param Idx Starting point for the definition. 2702cab237bSDimitry Andric /// @param Loc Location number to propagate. 271f785676fSDimitry Andric /// @param LR Restrict liveness to where LR has the value VNI. May be null. 272f785676fSDimitry Andric /// @param VNI When LR is not null, this is the value to restrict to. 2733b0f4066SDimitry Andric /// @param Kills Append end points of VNI's live range to Kills. 2742754fe60SDimitry Andric /// @param LIS Live intervals analysis. 2752cab237bSDimitry Andric void extendDef(SlotIndex Idx, DbgValueLocation Loc, 276f785676fSDimitry Andric LiveRange *LR, const VNInfo *VNI, 2773b0f4066SDimitry Andric SmallVectorImpl<SlotIndex> *Kills, 278d88c1a5aSDimitry Andric LiveIntervals &LIS); 2792754fe60SDimitry Andric 2803b0f4066SDimitry Andric /// addDefsFromCopies - The value in LI/LocNo may be copies to other 2813b0f4066SDimitry Andric /// registers. Determine if any of the copies are available at the kill 2823b0f4066SDimitry Andric /// points, and add defs if possible. 2833b0f4066SDimitry Andric /// @param LI Scan for copies of the value in LI->reg. 2843b0f4066SDimitry Andric /// @param LocNo Location number of LI->reg. 2852cab237bSDimitry Andric /// @param WasIndirect Indicates if the original use of LI->reg was indirect 2863b0f4066SDimitry Andric /// @param Kills Points where the range of LocNo could be extended. 2873b0f4066SDimitry Andric /// @param NewDefs Append (Idx, LocNo) of inserted defs here. 2882cab237bSDimitry Andric void addDefsFromCopies( 2892cab237bSDimitry Andric LiveInterval *LI, unsigned LocNo, bool WasIndirect, 2903b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 2912cab237bSDimitry Andric SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs, 2922cab237bSDimitry Andric MachineRegisterInfo &MRI, LiveIntervals &LIS); 2933b0f4066SDimitry Andric 2942754fe60SDimitry Andric /// computeIntervals - Compute the live intervals of all locations after 2952754fe60SDimitry Andric /// collecting all their def points. 2967ae0e2c9SDimitry Andric void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 2972cab237bSDimitry Andric LiveIntervals &LIS, LexicalScopes &LS); 2982754fe60SDimitry Andric 299bd5abe19SDimitry Andric /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is 300bd5abe19SDimitry Andric /// live. Returns true if any changes were made. 301f785676fSDimitry Andric bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 302f785676fSDimitry Andric LiveIntervals &LIS); 303bd5abe19SDimitry Andric 3042754fe60SDimitry Andric /// rewriteLocations - Rewrite virtual register locations according to the 3052cab237bSDimitry Andric /// provided virtual register map. Record which locations were spilled. 3062cab237bSDimitry Andric void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI, 3072cab237bSDimitry Andric BitVector &SpilledLocations); 3082754fe60SDimitry Andric 309139f7f9bSDimitry Andric /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 3102cab237bSDimitry Andric void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, 3112cab237bSDimitry Andric const TargetInstrInfo &TII, 3122cab237bSDimitry Andric const TargetRegisterInfo &TRI, 3132cab237bSDimitry Andric const BitVector &SpilledLocations); 3142754fe60SDimitry Andric 3156122f3e6SDimitry Andric /// getDebugLoc - Return DebugLoc of this UserValue. 3166122f3e6SDimitry Andric DebugLoc getDebugLoc() { return dl;} 3172cab237bSDimitry Andric 318ff0cc061SDimitry Andric void print(raw_ostream &, const TargetRegisterInfo *); 3192754fe60SDimitry Andric }; 3202754fe60SDimitry Andric 3212754fe60SDimitry Andric /// LDVImpl - Implementation of the LiveDebugVariables pass. 3222754fe60SDimitry Andric class LDVImpl { 3232754fe60SDimitry Andric LiveDebugVariables &pass; 3242754fe60SDimitry Andric LocMap::Allocator allocator; 3252cab237bSDimitry Andric MachineFunction *MF = nullptr; 3262754fe60SDimitry Andric LiveIntervals *LIS; 3272754fe60SDimitry Andric const TargetRegisterInfo *TRI; 3282754fe60SDimitry Andric 329139f7f9bSDimitry Andric /// Whether emitDebugValues is called. 3302cab237bSDimitry Andric bool EmitDone = false; 3312cab237bSDimitry Andric 332139f7f9bSDimitry Andric /// Whether the machine function is modified during the pass. 3332cab237bSDimitry Andric bool ModifiedMF = false; 334139f7f9bSDimitry Andric 3352754fe60SDimitry Andric /// userValues - All allocated UserValue instances. 33691bc56edSDimitry Andric SmallVector<std::unique_ptr<UserValue>, 8> userValues; 3372754fe60SDimitry Andric 3382754fe60SDimitry Andric /// Map virtual register to eq class leader. 3392cab237bSDimitry Andric using VRMap = DenseMap<unsigned, UserValue *>; 3402754fe60SDimitry Andric VRMap virtRegToEqClass; 3412754fe60SDimitry Andric 3422754fe60SDimitry Andric /// Map user variable to eq class leader. 3432cab237bSDimitry Andric using UVMap = DenseMap<const DILocalVariable *, UserValue *>; 3442754fe60SDimitry Andric UVMap userVarMap; 3452754fe60SDimitry Andric 3462754fe60SDimitry Andric /// getUserValue - Find or create a UserValue. 3472cab237bSDimitry Andric UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr, 3482cab237bSDimitry Andric const DebugLoc &DL); 3492754fe60SDimitry Andric 3502754fe60SDimitry Andric /// lookupVirtReg - Find the EC leader for VirtReg or null. 3512754fe60SDimitry Andric UserValue *lookupVirtReg(unsigned VirtReg); 3522754fe60SDimitry Andric 3532754fe60SDimitry Andric /// handleDebugValue - Add DBG_VALUE instruction to our maps. 3542754fe60SDimitry Andric /// @param MI DBG_VALUE instruction 3552754fe60SDimitry Andric /// @param Idx Last valid SLotIndex before instruction. 3562754fe60SDimitry Andric /// @return True if the DBG_VALUE instruction should be deleted. 3573ca95b02SDimitry Andric bool handleDebugValue(MachineInstr &MI, SlotIndex Idx); 3582754fe60SDimitry Andric 3592754fe60SDimitry Andric /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding 3602754fe60SDimitry Andric /// a UserValue def for each instruction. 3612754fe60SDimitry Andric /// @param mf MachineFunction to be scanned. 3622754fe60SDimitry Andric /// @return True if any debug values were found. 3632754fe60SDimitry Andric bool collectDebugValues(MachineFunction &mf); 3642754fe60SDimitry Andric 3652754fe60SDimitry Andric /// computeIntervals - Compute the live intervals of all user values after 3662754fe60SDimitry Andric /// collecting all their def points. 3672754fe60SDimitry Andric void computeIntervals(); 3682754fe60SDimitry Andric 3692754fe60SDimitry Andric public: 3702cab237bSDimitry Andric LDVImpl(LiveDebugVariables *ps) : pass(*ps) {} 3712cab237bSDimitry Andric 3722754fe60SDimitry Andric bool runOnMachineFunction(MachineFunction &mf); 3732754fe60SDimitry Andric 374139f7f9bSDimitry Andric /// clear - Release all memory. 3752754fe60SDimitry Andric void clear() { 37639d628a0SDimitry Andric MF = nullptr; 3772754fe60SDimitry Andric userValues.clear(); 3782754fe60SDimitry Andric virtRegToEqClass.clear(); 3792754fe60SDimitry Andric userVarMap.clear(); 380139f7f9bSDimitry Andric // Make sure we call emitDebugValues if the machine function was modified. 381139f7f9bSDimitry Andric assert((!ModifiedMF || EmitDone) && 382139f7f9bSDimitry Andric "Dbg values are not emitted in LDV"); 383139f7f9bSDimitry Andric EmitDone = false; 384139f7f9bSDimitry Andric ModifiedMF = false; 3852754fe60SDimitry Andric } 3862754fe60SDimitry Andric 3873b0f4066SDimitry Andric /// mapVirtReg - Map virtual register to an equivalence class. 3883b0f4066SDimitry Andric void mapVirtReg(unsigned VirtReg, UserValue *EC); 3893b0f4066SDimitry Andric 390bd5abe19SDimitry Andric /// splitRegister - Replace all references to OldReg with NewRegs. 391f785676fSDimitry Andric void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs); 392bd5abe19SDimitry Andric 393139f7f9bSDimitry Andric /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 3942754fe60SDimitry Andric void emitDebugValues(VirtRegMap *VRM); 3952754fe60SDimitry Andric 3962754fe60SDimitry Andric void print(raw_ostream&); 3972754fe60SDimitry Andric }; 3982754fe60SDimitry Andric 3992cab237bSDimitry Andric } // end anonymous namespace 4002cab237bSDimitry Andric 4012cab237bSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 4023ca95b02SDimitry Andric static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS, 403ff0cc061SDimitry Andric const LLVMContext &Ctx) { 404ff0cc061SDimitry Andric if (!DL) 405ff0cc061SDimitry Andric return; 406ff0cc061SDimitry Andric 407ff0cc061SDimitry Andric auto *Scope = cast<DIScope>(DL.getScope()); 408ff0cc061SDimitry Andric // Omit the directory, because it's likely to be long and uninteresting. 409ff0cc061SDimitry Andric CommentOS << Scope->getFilename(); 410ff0cc061SDimitry Andric CommentOS << ':' << DL.getLine(); 411ff0cc061SDimitry Andric if (DL.getCol() != 0) 412ff0cc061SDimitry Andric CommentOS << ':' << DL.getCol(); 413ff0cc061SDimitry Andric 414ff0cc061SDimitry Andric DebugLoc InlinedAtDL = DL.getInlinedAt(); 415ff0cc061SDimitry Andric if (!InlinedAtDL) 416ff0cc061SDimitry Andric return; 417ff0cc061SDimitry Andric 418ff0cc061SDimitry Andric CommentOS << " @[ "; 419ff0cc061SDimitry Andric printDebugLoc(InlinedAtDL, CommentOS, Ctx); 420ff0cc061SDimitry Andric CommentOS << " ]"; 421ff0cc061SDimitry Andric } 422ff0cc061SDimitry Andric 423ff0cc061SDimitry Andric static void printExtendedName(raw_ostream &OS, const DILocalVariable *V, 424ff0cc061SDimitry Andric const DILocation *DL) { 425ff0cc061SDimitry Andric const LLVMContext &Ctx = V->getContext(); 426ff0cc061SDimitry Andric StringRef Res = V->getName(); 427ff0cc061SDimitry Andric if (!Res.empty()) 428ff0cc061SDimitry Andric OS << Res << "," << V->getLine(); 429ff0cc061SDimitry Andric if (auto *InlinedAt = DL->getInlinedAt()) { 430ff0cc061SDimitry Andric if (DebugLoc InlinedAtDL = InlinedAt) { 431ff0cc061SDimitry Andric OS << " @["; 432ff0cc061SDimitry Andric printDebugLoc(InlinedAtDL, OS, Ctx); 433ff0cc061SDimitry Andric OS << "]"; 434ff0cc061SDimitry Andric } 435ff0cc061SDimitry Andric } 436ff0cc061SDimitry Andric } 437ff0cc061SDimitry Andric 438ff0cc061SDimitry Andric void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) { 439ff0cc061SDimitry Andric auto *DV = cast<DILocalVariable>(Variable); 4406122f3e6SDimitry Andric OS << "!\""; 441ff0cc061SDimitry Andric printExtendedName(OS, DV, dl); 442ff0cc061SDimitry Andric 4436122f3e6SDimitry Andric OS << "\"\t"; 4442754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { 4452754fe60SDimitry Andric OS << " [" << I.start() << ';' << I.stop() << "):"; 4462cab237bSDimitry Andric if (I.value().isUndef()) 4472754fe60SDimitry Andric OS << "undef"; 4482cab237bSDimitry Andric else { 4492cab237bSDimitry Andric OS << I.value().locNo(); 4502cab237bSDimitry Andric if (I.value().wasIndirect()) 4512cab237bSDimitry Andric OS << " ind"; 4522cab237bSDimitry Andric } 4532754fe60SDimitry Andric } 454bd5abe19SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) { 455bd5abe19SDimitry Andric OS << " Loc" << i << '='; 456ff0cc061SDimitry Andric locations[i].print(OS, TRI); 457bd5abe19SDimitry Andric } 4582754fe60SDimitry Andric OS << '\n'; 4592754fe60SDimitry Andric } 4602754fe60SDimitry Andric 4612754fe60SDimitry Andric void LDVImpl::print(raw_ostream &OS) { 4622754fe60SDimitry Andric OS << "********** DEBUG VARIABLES **********\n"; 4632754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) 464ff0cc061SDimitry Andric userValues[i]->print(OS, TRI); 4652754fe60SDimitry Andric } 4662cab237bSDimitry Andric #endif 4672754fe60SDimitry Andric 4683b0f4066SDimitry Andric void UserValue::mapVirtRegs(LDVImpl *LDV) { 4693b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 4703b0f4066SDimitry Andric if (locations[i].isReg() && 4713b0f4066SDimitry Andric TargetRegisterInfo::isVirtualRegister(locations[i].getReg())) 4723b0f4066SDimitry Andric LDV->mapVirtReg(locations[i].getReg(), this); 4733b0f4066SDimitry Andric } 4743b0f4066SDimitry Andric 4752cab237bSDimitry Andric UserValue *LDVImpl::getUserValue(const DILocalVariable *Var, 4762cab237bSDimitry Andric const DIExpression *Expr, const DebugLoc &DL) { 4772754fe60SDimitry Andric UserValue *&Leader = userVarMap[Var]; 4782754fe60SDimitry Andric if (Leader) { 4792754fe60SDimitry Andric UserValue *UV = Leader->getLeader(); 4802754fe60SDimitry Andric Leader = UV; 4812754fe60SDimitry Andric for (; UV; UV = UV->getNext()) 4822cab237bSDimitry Andric if (UV->match(Var, Expr, DL->getInlinedAt())) 4832754fe60SDimitry Andric return UV; 4842754fe60SDimitry Andric } 4852754fe60SDimitry Andric 48691bc56edSDimitry Andric userValues.push_back( 4872cab237bSDimitry Andric llvm::make_unique<UserValue>(Var, Expr, DL, allocator)); 48891bc56edSDimitry Andric UserValue *UV = userValues.back().get(); 4892754fe60SDimitry Andric Leader = UserValue::merge(Leader, UV); 4902754fe60SDimitry Andric return UV; 4912754fe60SDimitry Andric } 4922754fe60SDimitry Andric 4932754fe60SDimitry Andric void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) { 4942754fe60SDimitry Andric assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs"); 4952754fe60SDimitry Andric UserValue *&Leader = virtRegToEqClass[VirtReg]; 4962754fe60SDimitry Andric Leader = UserValue::merge(Leader, EC); 4972754fe60SDimitry Andric } 4982754fe60SDimitry Andric 4992754fe60SDimitry Andric UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) { 5002754fe60SDimitry Andric if (UserValue *UV = virtRegToEqClass.lookup(VirtReg)) 5012754fe60SDimitry Andric return UV->getLeader(); 50291bc56edSDimitry Andric return nullptr; 5032754fe60SDimitry Andric } 5042754fe60SDimitry Andric 5053ca95b02SDimitry Andric bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) { 5062754fe60SDimitry Andric // DBG_VALUE loc, offset, variable 5073ca95b02SDimitry Andric if (MI.getNumOperands() != 4 || 5083ca95b02SDimitry Andric !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) || 5093ca95b02SDimitry Andric !MI.getOperand(2).isMetadata()) { 5103ca95b02SDimitry Andric DEBUG(dbgs() << "Can't handle " << MI); 5112754fe60SDimitry Andric return false; 5122754fe60SDimitry Andric } 5132754fe60SDimitry Andric 5142cab237bSDimitry Andric // Get or create the UserValue for (variable,offset) here. 5152cab237bSDimitry Andric bool IsIndirect = MI.getOperand(1).isImm(); 5162cab237bSDimitry Andric if (IsIndirect) 5172cab237bSDimitry Andric assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset"); 5182cab237bSDimitry Andric const DILocalVariable *Var = MI.getDebugVariable(); 5192cab237bSDimitry Andric const DIExpression *Expr = MI.getDebugExpression(); 5202cab237bSDimitry Andric UserValue *UV = 5212cab237bSDimitry Andric getUserValue(Var, Expr, MI.getDebugLoc()); 5222cab237bSDimitry Andric UV->addDef(Idx, MI.getOperand(0), IsIndirect); 5232754fe60SDimitry Andric return true; 5242754fe60SDimitry Andric } 5252754fe60SDimitry Andric 5262754fe60SDimitry Andric bool LDVImpl::collectDebugValues(MachineFunction &mf) { 5272754fe60SDimitry Andric bool Changed = false; 5282754fe60SDimitry Andric for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE; 5292754fe60SDimitry Andric ++MFI) { 5307d523365SDimitry Andric MachineBasicBlock *MBB = &*MFI; 5312754fe60SDimitry Andric for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); 5322754fe60SDimitry Andric MBBI != MBBE;) { 5332754fe60SDimitry Andric if (!MBBI->isDebugValue()) { 5342754fe60SDimitry Andric ++MBBI; 5352754fe60SDimitry Andric continue; 5362754fe60SDimitry Andric } 5372754fe60SDimitry Andric // DBG_VALUE has no slot index, use the previous instruction instead. 5383ca95b02SDimitry Andric SlotIndex Idx = 5393ca95b02SDimitry Andric MBBI == MBB->begin() 5403ca95b02SDimitry Andric ? LIS->getMBBStartIdx(MBB) 5413ca95b02SDimitry Andric : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot(); 5422754fe60SDimitry Andric // Handle consecutive DBG_VALUE instructions with the same slot index. 5432754fe60SDimitry Andric do { 5443ca95b02SDimitry Andric if (handleDebugValue(*MBBI, Idx)) { 5452754fe60SDimitry Andric MBBI = MBB->erase(MBBI); 5462754fe60SDimitry Andric Changed = true; 5472754fe60SDimitry Andric } else 5482754fe60SDimitry Andric ++MBBI; 5492754fe60SDimitry Andric } while (MBBI != MBBE && MBBI->isDebugValue()); 5502754fe60SDimitry Andric } 5512754fe60SDimitry Andric } 5522754fe60SDimitry Andric return Changed; 5532754fe60SDimitry Andric } 5542754fe60SDimitry Andric 5557d523365SDimitry Andric /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a 5567d523365SDimitry Andric /// data-flow analysis to propagate them beyond basic block boundaries. 5572cab237bSDimitry Andric void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR, 5587d523365SDimitry Andric const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills, 559d88c1a5aSDimitry Andric LiveIntervals &LIS) { 5607d523365SDimitry Andric SlotIndex Start = Idx; 5612754fe60SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start); 5622754fe60SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(MBB); 5632754fe60SDimitry Andric LocMap::iterator I = locInts.find(Start); 5642754fe60SDimitry Andric 5652754fe60SDimitry Andric // Limit to VNI's live range. 5662754fe60SDimitry Andric bool ToEnd = true; 567f785676fSDimitry Andric if (LR && VNI) { 568f785676fSDimitry Andric LiveInterval::Segment *Segment = LR->getSegmentContaining(Start); 569f785676fSDimitry Andric if (!Segment || Segment->valno != VNI) { 5703b0f4066SDimitry Andric if (Kills) 5713b0f4066SDimitry Andric Kills->push_back(Start); 5727d523365SDimitry Andric return; 5733b0f4066SDimitry Andric } 5743ca95b02SDimitry Andric if (Segment->end < Stop) { 5753ca95b02SDimitry Andric Stop = Segment->end; 5763ca95b02SDimitry Andric ToEnd = false; 5773ca95b02SDimitry Andric } 5782754fe60SDimitry Andric } 5792754fe60SDimitry Andric 5802754fe60SDimitry Andric // There could already be a short def at Start. 5812754fe60SDimitry Andric if (I.valid() && I.start() <= Start) { 5822754fe60SDimitry Andric // Stop when meeting a different location or an already extended interval. 5832754fe60SDimitry Andric Start = Start.getNextSlot(); 5842cab237bSDimitry Andric if (I.value() != Loc || I.stop() != Start) 5857d523365SDimitry Andric return; 5862754fe60SDimitry Andric // This is a one-slot placeholder. Just skip it. 5872754fe60SDimitry Andric ++I; 5882754fe60SDimitry Andric } 5892754fe60SDimitry Andric 5902754fe60SDimitry Andric // Limited by the next def. 5913ca95b02SDimitry Andric if (I.valid() && I.start() < Stop) { 5923ca95b02SDimitry Andric Stop = I.start(); 5933ca95b02SDimitry Andric ToEnd = false; 5943ca95b02SDimitry Andric } 5953b0f4066SDimitry Andric // Limited by VNI's live range. 5963b0f4066SDimitry Andric else if (!ToEnd && Kills) 5973b0f4066SDimitry Andric Kills->push_back(Stop); 5982754fe60SDimitry Andric 5997d523365SDimitry Andric if (Start < Stop) 6002cab237bSDimitry Andric I.insert(Start, Stop, Loc); 6012754fe60SDimitry Andric } 6022754fe60SDimitry Andric 6032cab237bSDimitry Andric void UserValue::addDefsFromCopies( 6042cab237bSDimitry Andric LiveInterval *LI, unsigned LocNo, bool WasIndirect, 6053b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 6062cab237bSDimitry Andric SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs, 6073b0f4066SDimitry Andric MachineRegisterInfo &MRI, LiveIntervals &LIS) { 6083b0f4066SDimitry Andric if (Kills.empty()) 6093b0f4066SDimitry Andric return; 6103b0f4066SDimitry Andric // Don't track copies from physregs, there are too many uses. 6113b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(LI->reg)) 6123b0f4066SDimitry Andric return; 6133b0f4066SDimitry Andric 6143b0f4066SDimitry Andric // Collect all the (vreg, valno) pairs that are copies of LI. 6153b0f4066SDimitry Andric SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues; 61691bc56edSDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) { 61791bc56edSDimitry Andric MachineInstr *MI = MO.getParent(); 6183b0f4066SDimitry Andric // Copies of the full value. 61991bc56edSDimitry Andric if (MO.getSubReg() || !MI->isCopy()) 6203b0f4066SDimitry Andric continue; 6213b0f4066SDimitry Andric unsigned DstReg = MI->getOperand(0).getReg(); 6223b0f4066SDimitry Andric 6233b0f4066SDimitry Andric // Don't follow copies to physregs. These are usually setting up call 6243b0f4066SDimitry Andric // arguments, and the argument registers are always call clobbered. We are 6253b0f4066SDimitry Andric // better off in the source register which could be a callee-saved register, 6263b0f4066SDimitry Andric // or it could be spilled. 6273b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(DstReg)) 6283b0f4066SDimitry Andric continue; 6293b0f4066SDimitry Andric 6303b0f4066SDimitry Andric // Is LocNo extended to reach this copy? If not, another def may be blocking 6313b0f4066SDimitry Andric // it, or we are looking at a wrong value of LI. 6323ca95b02SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI); 633dff0c46cSDimitry Andric LocMap::iterator I = locInts.find(Idx.getRegSlot(true)); 6342cab237bSDimitry Andric if (!I.valid() || I.value().locNo() != LocNo) 6353b0f4066SDimitry Andric continue; 6363b0f4066SDimitry Andric 6373b0f4066SDimitry Andric if (!LIS.hasInterval(DstReg)) 6383b0f4066SDimitry Andric continue; 6393b0f4066SDimitry Andric LiveInterval *DstLI = &LIS.getInterval(DstReg); 640dff0c46cSDimitry Andric const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot()); 641dff0c46cSDimitry Andric assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value"); 6423b0f4066SDimitry Andric CopyValues.push_back(std::make_pair(DstLI, DstVNI)); 6433b0f4066SDimitry Andric } 6443b0f4066SDimitry Andric 6453b0f4066SDimitry Andric if (CopyValues.empty()) 6463b0f4066SDimitry Andric return; 6473b0f4066SDimitry Andric 6483b0f4066SDimitry Andric DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n'); 6493b0f4066SDimitry Andric 6503b0f4066SDimitry Andric // Try to add defs of the copied values for each kill point. 6513b0f4066SDimitry Andric for (unsigned i = 0, e = Kills.size(); i != e; ++i) { 6523b0f4066SDimitry Andric SlotIndex Idx = Kills[i]; 6533b0f4066SDimitry Andric for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) { 6543b0f4066SDimitry Andric LiveInterval *DstLI = CopyValues[j].first; 6553b0f4066SDimitry Andric const VNInfo *DstVNI = CopyValues[j].second; 6563b0f4066SDimitry Andric if (DstLI->getVNInfoAt(Idx) != DstVNI) 6573b0f4066SDimitry Andric continue; 6583b0f4066SDimitry Andric // Check that there isn't already a def at Idx 6593b0f4066SDimitry Andric LocMap::iterator I = locInts.find(Idx); 6603b0f4066SDimitry Andric if (I.valid() && I.start() <= Idx) 6613b0f4066SDimitry Andric continue; 6623b0f4066SDimitry Andric DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" 6633b0f4066SDimitry Andric << DstVNI->id << " in " << *DstLI << '\n'); 6643b0f4066SDimitry Andric MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); 6653b0f4066SDimitry Andric assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); 6663b0f4066SDimitry Andric unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); 6672cab237bSDimitry Andric DbgValueLocation NewLoc(LocNo, WasIndirect); 6682cab237bSDimitry Andric I.insert(Idx, Idx.getNextSlot(), NewLoc); 6692cab237bSDimitry Andric NewDefs.push_back(std::make_pair(Idx, NewLoc)); 6703b0f4066SDimitry Andric break; 6713b0f4066SDimitry Andric } 6723b0f4066SDimitry Andric } 6733b0f4066SDimitry Andric } 6743b0f4066SDimitry Andric 6752cab237bSDimitry Andric void UserValue::computeIntervals(MachineRegisterInfo &MRI, 6767ae0e2c9SDimitry Andric const TargetRegisterInfo &TRI, 6772cab237bSDimitry Andric LiveIntervals &LIS, LexicalScopes &LS) { 6782cab237bSDimitry Andric SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs; 6792754fe60SDimitry Andric 6802754fe60SDimitry Andric // Collect all defs to be extended (Skipping undefs). 6812754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) 6822cab237bSDimitry Andric if (!I.value().isUndef()) 6832754fe60SDimitry Andric Defs.push_back(std::make_pair(I.start(), I.value())); 6842754fe60SDimitry Andric 6853b0f4066SDimitry Andric // Extend all defs, and possibly add new ones along the way. 6863b0f4066SDimitry Andric for (unsigned i = 0; i != Defs.size(); ++i) { 6872754fe60SDimitry Andric SlotIndex Idx = Defs[i].first; 6882cab237bSDimitry Andric DbgValueLocation Loc = Defs[i].second; 6892cab237bSDimitry Andric const MachineOperand &LocMO = locations[Loc.locNo()]; 6902754fe60SDimitry Andric 6912cab237bSDimitry Andric if (!LocMO.isReg()) { 6922cab237bSDimitry Andric extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS); 6937ae0e2c9SDimitry Andric continue; 6947ae0e2c9SDimitry Andric } 6957ae0e2c9SDimitry Andric 6962754fe60SDimitry Andric // Register locations are constrained to where the register value is live. 6972cab237bSDimitry Andric if (TargetRegisterInfo::isVirtualRegister(LocMO.getReg())) { 69891bc56edSDimitry Andric LiveInterval *LI = nullptr; 69991bc56edSDimitry Andric const VNInfo *VNI = nullptr; 7002cab237bSDimitry Andric if (LIS.hasInterval(LocMO.getReg())) { 7012cab237bSDimitry Andric LI = &LIS.getInterval(LocMO.getReg()); 7027ae0e2c9SDimitry Andric VNI = LI->getVNInfoAt(Idx); 7037ae0e2c9SDimitry Andric } 7043b0f4066SDimitry Andric SmallVector<SlotIndex, 16> Kills; 7052cab237bSDimitry Andric extendDef(Idx, Loc, LI, VNI, &Kills, LIS); 7067ae0e2c9SDimitry Andric if (LI) 7072cab237bSDimitry Andric addDefsFromCopies(LI, Loc.locNo(), Loc.wasIndirect(), Kills, Defs, MRI, 7082cab237bSDimitry Andric LIS); 7097ae0e2c9SDimitry Andric continue; 7107ae0e2c9SDimitry Andric } 7117ae0e2c9SDimitry Andric 7122cab237bSDimitry Andric // For physregs, we only mark the start slot idx. DwarfDebug will see it 7132cab237bSDimitry Andric // as if the DBG_VALUE is valid up until the end of the basic block, or 7142cab237bSDimitry Andric // the next def of the physical register. So we do not need to extend the 7152cab237bSDimitry Andric // range. It might actually happen that the DBG_VALUE is the last use of 7162cab237bSDimitry Andric // the physical register (e.g. if this is an unused input argument to a 7172cab237bSDimitry Andric // function). 7182754fe60SDimitry Andric } 7192754fe60SDimitry Andric 7202cab237bSDimitry Andric // Erase all the undefs. 7212754fe60SDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid();) 7222cab237bSDimitry Andric if (I.value().isUndef()) 7232754fe60SDimitry Andric I.erase(); 7242754fe60SDimitry Andric else 7252754fe60SDimitry Andric ++I; 7262cab237bSDimitry Andric 7272cab237bSDimitry Andric // The computed intervals may extend beyond the range of the debug 7282cab237bSDimitry Andric // location's lexical scope. In this case, splitting of an interval 7292cab237bSDimitry Andric // can result in an interval outside of the scope being created, 7302cab237bSDimitry Andric // causing extra unnecessary DBG_VALUEs to be emitted. To prevent 7312cab237bSDimitry Andric // this, trim the intervals to the lexical scope. 7322cab237bSDimitry Andric 7332cab237bSDimitry Andric LexicalScope *Scope = LS.findLexicalScope(dl); 7342cab237bSDimitry Andric if (!Scope) 7352cab237bSDimitry Andric return; 7362cab237bSDimitry Andric 7372cab237bSDimitry Andric SlotIndex PrevEnd; 7382cab237bSDimitry Andric LocMap::iterator I = locInts.begin(); 7392cab237bSDimitry Andric 7402cab237bSDimitry Andric // Iterate over the lexical scope ranges. Each time round the loop 7412cab237bSDimitry Andric // we check the intervals for overlap with the end of the previous 7422cab237bSDimitry Andric // range and the start of the next. The first range is handled as 7432cab237bSDimitry Andric // a special case where there is no PrevEnd. 7442cab237bSDimitry Andric for (const InsnRange &Range : Scope->getRanges()) { 7452cab237bSDimitry Andric SlotIndex RStart = LIS.getInstructionIndex(*Range.first); 7462cab237bSDimitry Andric SlotIndex REnd = LIS.getInstructionIndex(*Range.second); 7472cab237bSDimitry Andric 7482cab237bSDimitry Andric // At the start of each iteration I has been advanced so that 7492cab237bSDimitry Andric // I.stop() >= PrevEnd. Check for overlap. 7502cab237bSDimitry Andric if (PrevEnd && I.start() < PrevEnd) { 7512cab237bSDimitry Andric SlotIndex IStop = I.stop(); 7522cab237bSDimitry Andric DbgValueLocation Loc = I.value(); 7532cab237bSDimitry Andric 7542cab237bSDimitry Andric // Stop overlaps previous end - trim the end of the interval to 7552cab237bSDimitry Andric // the scope range. 7562cab237bSDimitry Andric I.setStopUnchecked(PrevEnd); 7572cab237bSDimitry Andric ++I; 7582cab237bSDimitry Andric 7592cab237bSDimitry Andric // If the interval also overlaps the start of the "next" (i.e. 7602cab237bSDimitry Andric // current) range create a new interval for the remainder (which 7612cab237bSDimitry Andric // may be further trimmed). 7622cab237bSDimitry Andric if (RStart < IStop) 7632cab237bSDimitry Andric I.insert(RStart, IStop, Loc); 7642cab237bSDimitry Andric } 7652cab237bSDimitry Andric 7662cab237bSDimitry Andric // Advance I so that I.stop() >= RStart, and check for overlap. 7672cab237bSDimitry Andric I.advanceTo(RStart); 7682cab237bSDimitry Andric if (!I.valid()) 7692cab237bSDimitry Andric return; 7702cab237bSDimitry Andric 7712cab237bSDimitry Andric if (I.start() < RStart) { 7722cab237bSDimitry Andric // Interval start overlaps range - trim to the scope range. 7732cab237bSDimitry Andric I.setStartUnchecked(RStart); 7742cab237bSDimitry Andric // Remember that this interval was trimmed. 7752cab237bSDimitry Andric trimmedDefs.insert(RStart); 7762cab237bSDimitry Andric } 7772cab237bSDimitry Andric 7782cab237bSDimitry Andric // The end of a lexical scope range is the last instruction in the 7792cab237bSDimitry Andric // range. To convert to an interval we need the index of the 7802cab237bSDimitry Andric // instruction after it. 7812cab237bSDimitry Andric REnd = REnd.getNextIndex(); 7822cab237bSDimitry Andric 7832cab237bSDimitry Andric // Advance I to first interval outside current range. 7842cab237bSDimitry Andric I.advanceTo(REnd); 7852cab237bSDimitry Andric if (!I.valid()) 7862cab237bSDimitry Andric return; 7872cab237bSDimitry Andric 7882cab237bSDimitry Andric PrevEnd = REnd; 7892cab237bSDimitry Andric } 7902cab237bSDimitry Andric 7912cab237bSDimitry Andric // Check for overlap with end of final range. 7922cab237bSDimitry Andric if (PrevEnd && I.start() < PrevEnd) 7932cab237bSDimitry Andric I.setStopUnchecked(PrevEnd); 7942754fe60SDimitry Andric } 7952754fe60SDimitry Andric 7962754fe60SDimitry Andric void LDVImpl::computeIntervals() { 7972cab237bSDimitry Andric LexicalScopes LS; 7982cab237bSDimitry Andric LS.initialize(*MF); 7992cab237bSDimitry Andric 8003b0f4066SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 8012cab237bSDimitry Andric userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS); 8023b0f4066SDimitry Andric userValues[i]->mapVirtRegs(this); 8033b0f4066SDimitry Andric } 8042754fe60SDimitry Andric } 8052754fe60SDimitry Andric 8062754fe60SDimitry Andric bool LDVImpl::runOnMachineFunction(MachineFunction &mf) { 80739d628a0SDimitry Andric clear(); 8082754fe60SDimitry Andric MF = &mf; 8092754fe60SDimitry Andric LIS = &pass.getAnalysis<LiveIntervals>(); 81039d628a0SDimitry Andric TRI = mf.getSubtarget().getRegisterInfo(); 8112754fe60SDimitry Andric DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " 8123861d79fSDimitry Andric << mf.getName() << " **********\n"); 8132754fe60SDimitry Andric 8142754fe60SDimitry Andric bool Changed = collectDebugValues(mf); 8152754fe60SDimitry Andric computeIntervals(); 8162754fe60SDimitry Andric DEBUG(print(dbgs())); 817139f7f9bSDimitry Andric ModifiedMF = Changed; 8182754fe60SDimitry Andric return Changed; 8192754fe60SDimitry Andric } 8202754fe60SDimitry Andric 82139d628a0SDimitry Andric static void removeDebugValues(MachineFunction &mf) { 82239d628a0SDimitry Andric for (MachineBasicBlock &MBB : mf) { 82339d628a0SDimitry Andric for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) { 82439d628a0SDimitry Andric if (!MBBI->isDebugValue()) { 82539d628a0SDimitry Andric ++MBBI; 82639d628a0SDimitry Andric continue; 82739d628a0SDimitry Andric } 82839d628a0SDimitry Andric MBBI = MBB.erase(MBBI); 82939d628a0SDimitry Andric } 83039d628a0SDimitry Andric } 83139d628a0SDimitry Andric } 83239d628a0SDimitry Andric 8332754fe60SDimitry Andric bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) { 8342754fe60SDimitry Andric if (!EnableLDV) 8352754fe60SDimitry Andric return false; 8362cab237bSDimitry Andric if (!mf.getFunction().getSubprogram()) { 83739d628a0SDimitry Andric removeDebugValues(mf); 83839d628a0SDimitry Andric return false; 83939d628a0SDimitry Andric } 8402754fe60SDimitry Andric if (!pImpl) 8412754fe60SDimitry Andric pImpl = new LDVImpl(this); 8422754fe60SDimitry Andric return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf); 8432754fe60SDimitry Andric } 8442754fe60SDimitry Andric 8452754fe60SDimitry Andric void LiveDebugVariables::releaseMemory() { 8462754fe60SDimitry Andric if (pImpl) 8472754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->clear(); 8482754fe60SDimitry Andric } 8492754fe60SDimitry Andric 8502754fe60SDimitry Andric LiveDebugVariables::~LiveDebugVariables() { 8512754fe60SDimitry Andric if (pImpl) 8522754fe60SDimitry Andric delete static_cast<LDVImpl*>(pImpl); 8532754fe60SDimitry Andric } 8542754fe60SDimitry Andric 855bd5abe19SDimitry Andric //===----------------------------------------------------------------------===// 856bd5abe19SDimitry Andric // Live Range Splitting 857bd5abe19SDimitry Andric //===----------------------------------------------------------------------===// 858bd5abe19SDimitry Andric 859bd5abe19SDimitry Andric bool 860f785676fSDimitry Andric UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 861f785676fSDimitry Andric LiveIntervals& LIS) { 862bd5abe19SDimitry Andric DEBUG({ 863bd5abe19SDimitry Andric dbgs() << "Splitting Loc" << OldLocNo << '\t'; 86491bc56edSDimitry Andric print(dbgs(), nullptr); 865bd5abe19SDimitry Andric }); 866bd5abe19SDimitry Andric bool DidChange = false; 867bd5abe19SDimitry Andric LocMap::iterator LocMapI; 868bd5abe19SDimitry Andric LocMapI.setMap(locInts); 869bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) { 870f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(NewRegs[i]); 871bd5abe19SDimitry Andric if (LI->empty()) 872bd5abe19SDimitry Andric continue; 873bd5abe19SDimitry Andric 874bd5abe19SDimitry Andric // Don't allocate the new LocNo until it is needed. 8752cab237bSDimitry Andric unsigned NewLocNo = UndefLocNo; 876bd5abe19SDimitry Andric 877bd5abe19SDimitry Andric // Iterate over the overlaps between locInts and LI. 878bd5abe19SDimitry Andric LocMapI.find(LI->beginIndex()); 879bd5abe19SDimitry Andric if (!LocMapI.valid()) 880bd5abe19SDimitry Andric continue; 881bd5abe19SDimitry Andric LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start()); 882bd5abe19SDimitry Andric LiveInterval::iterator LIE = LI->end(); 883bd5abe19SDimitry Andric while (LocMapI.valid() && LII != LIE) { 884bd5abe19SDimitry Andric // At this point, we know that LocMapI.stop() > LII->start. 885bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 886bd5abe19SDimitry Andric if (LII == LIE) 887bd5abe19SDimitry Andric break; 888bd5abe19SDimitry Andric 889bd5abe19SDimitry Andric // Now LII->end > LocMapI.start(). Do we have an overlap? 8902cab237bSDimitry Andric if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) { 891bd5abe19SDimitry Andric // Overlapping correct location. Allocate NewLocNo now. 8922cab237bSDimitry Andric if (NewLocNo == UndefLocNo) { 893bd5abe19SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(LI->reg, false); 894bd5abe19SDimitry Andric MO.setSubReg(locations[OldLocNo].getSubReg()); 895bd5abe19SDimitry Andric NewLocNo = getLocationNo(MO); 896bd5abe19SDimitry Andric DidChange = true; 897bd5abe19SDimitry Andric } 898bd5abe19SDimitry Andric 899bd5abe19SDimitry Andric SlotIndex LStart = LocMapI.start(); 900bd5abe19SDimitry Andric SlotIndex LStop = LocMapI.stop(); 9012cab237bSDimitry Andric DbgValueLocation OldLoc = LocMapI.value(); 902bd5abe19SDimitry Andric 903bd5abe19SDimitry Andric // Trim LocMapI down to the LII overlap. 904bd5abe19SDimitry Andric if (LStart < LII->start) 905bd5abe19SDimitry Andric LocMapI.setStartUnchecked(LII->start); 906bd5abe19SDimitry Andric if (LStop > LII->end) 907bd5abe19SDimitry Andric LocMapI.setStopUnchecked(LII->end); 908bd5abe19SDimitry Andric 909bd5abe19SDimitry Andric // Change the value in the overlap. This may trigger coalescing. 9102cab237bSDimitry Andric LocMapI.setValue(OldLoc.changeLocNo(NewLocNo)); 911bd5abe19SDimitry Andric 912bd5abe19SDimitry Andric // Re-insert any removed OldLocNo ranges. 913bd5abe19SDimitry Andric if (LStart < LocMapI.start()) { 9142cab237bSDimitry Andric LocMapI.insert(LStart, LocMapI.start(), OldLoc); 915bd5abe19SDimitry Andric ++LocMapI; 916bd5abe19SDimitry Andric assert(LocMapI.valid() && "Unexpected coalescing"); 917bd5abe19SDimitry Andric } 918bd5abe19SDimitry Andric if (LStop > LocMapI.stop()) { 919bd5abe19SDimitry Andric ++LocMapI; 9202cab237bSDimitry Andric LocMapI.insert(LII->end, LStop, OldLoc); 921bd5abe19SDimitry Andric --LocMapI; 922bd5abe19SDimitry Andric } 923bd5abe19SDimitry Andric } 924bd5abe19SDimitry Andric 925bd5abe19SDimitry Andric // Advance to the next overlap. 926bd5abe19SDimitry Andric if (LII->end < LocMapI.stop()) { 927bd5abe19SDimitry Andric if (++LII == LIE) 928bd5abe19SDimitry Andric break; 929bd5abe19SDimitry Andric LocMapI.advanceTo(LII->start); 930bd5abe19SDimitry Andric } else { 931bd5abe19SDimitry Andric ++LocMapI; 932bd5abe19SDimitry Andric if (!LocMapI.valid()) 933bd5abe19SDimitry Andric break; 934bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 935bd5abe19SDimitry Andric } 936bd5abe19SDimitry Andric } 937bd5abe19SDimitry Andric } 938bd5abe19SDimitry Andric 939bd5abe19SDimitry Andric // Finally, remove any remaining OldLocNo intervals and OldLocNo itself. 940bd5abe19SDimitry Andric locations.erase(locations.begin() + OldLocNo); 941bd5abe19SDimitry Andric LocMapI.goToBegin(); 942bd5abe19SDimitry Andric while (LocMapI.valid()) { 9432cab237bSDimitry Andric DbgValueLocation v = LocMapI.value(); 9442cab237bSDimitry Andric if (v.locNo() == OldLocNo) { 945bd5abe19SDimitry Andric DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';' 946bd5abe19SDimitry Andric << LocMapI.stop() << ")\n"); 947bd5abe19SDimitry Andric LocMapI.erase(); 948bd5abe19SDimitry Andric } else { 9492cab237bSDimitry Andric if (v.locNo() > OldLocNo) 9502cab237bSDimitry Andric LocMapI.setValueUnchecked(v.changeLocNo(v.locNo() - 1)); 951bd5abe19SDimitry Andric ++LocMapI; 952bd5abe19SDimitry Andric } 953bd5abe19SDimitry Andric } 954bd5abe19SDimitry Andric 95591bc56edSDimitry Andric DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);}); 956bd5abe19SDimitry Andric return DidChange; 957bd5abe19SDimitry Andric } 958bd5abe19SDimitry Andric 959bd5abe19SDimitry Andric bool 960f785676fSDimitry Andric UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, 961f785676fSDimitry Andric LiveIntervals &LIS) { 962bd5abe19SDimitry Andric bool DidChange = false; 963bd5abe19SDimitry Andric // Split locations referring to OldReg. Iterate backwards so splitLocation can 964dff0c46cSDimitry Andric // safely erase unused locations. 965bd5abe19SDimitry Andric for (unsigned i = locations.size(); i ; --i) { 966bd5abe19SDimitry Andric unsigned LocNo = i-1; 967bd5abe19SDimitry Andric const MachineOperand *Loc = &locations[LocNo]; 968bd5abe19SDimitry Andric if (!Loc->isReg() || Loc->getReg() != OldReg) 969bd5abe19SDimitry Andric continue; 970f785676fSDimitry Andric DidChange |= splitLocation(LocNo, NewRegs, LIS); 971bd5abe19SDimitry Andric } 972bd5abe19SDimitry Andric return DidChange; 973bd5abe19SDimitry Andric } 974bd5abe19SDimitry Andric 975f785676fSDimitry Andric void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) { 976bd5abe19SDimitry Andric bool DidChange = false; 977bd5abe19SDimitry Andric for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext()) 978f785676fSDimitry Andric DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS); 979bd5abe19SDimitry Andric 980bd5abe19SDimitry Andric if (!DidChange) 981bd5abe19SDimitry Andric return; 982bd5abe19SDimitry Andric 983bd5abe19SDimitry Andric // Map all of the new virtual registers. 984bd5abe19SDimitry Andric UserValue *UV = lookupVirtReg(OldReg); 985bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) 986f785676fSDimitry Andric mapVirtReg(NewRegs[i], UV); 987bd5abe19SDimitry Andric } 988bd5abe19SDimitry Andric 989bd5abe19SDimitry Andric void LiveDebugVariables:: 990f785676fSDimitry Andric splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) { 991bd5abe19SDimitry Andric if (pImpl) 992bd5abe19SDimitry Andric static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs); 993bd5abe19SDimitry Andric } 994bd5abe19SDimitry Andric 9952cab237bSDimitry Andric void UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI, 9962cab237bSDimitry Andric BitVector &SpilledLocations) { 9972cab237bSDimitry Andric // Build a set of new locations with new numbers so we can coalesce our 9982cab237bSDimitry Andric // IntervalMap if two vreg intervals collapse to the same physical location. 9992cab237bSDimitry Andric // Use MapVector instead of SetVector because MapVector::insert returns the 10002cab237bSDimitry Andric // position of the previously or newly inserted element. The boolean value 10012cab237bSDimitry Andric // tracks if the location was produced by a spill. 10022cab237bSDimitry Andric // FIXME: This will be problematic if we ever support direct and indirect 10032cab237bSDimitry Andric // frame index locations, i.e. expressing both variables in memory and 10042cab237bSDimitry Andric // 'int x, *px = &x'. The "spilled" bit must become part of the location. 10052cab237bSDimitry Andric MapVector<MachineOperand, bool> NewLocations; 10062cab237bSDimitry Andric SmallVector<unsigned, 4> LocNoMap(locations.size()); 10072cab237bSDimitry Andric for (unsigned I = 0, E = locations.size(); I != E; ++I) { 10082cab237bSDimitry Andric bool Spilled = false; 10092cab237bSDimitry Andric MachineOperand Loc = locations[I]; 10102754fe60SDimitry Andric // Only virtual registers are rewritten. 10112cab237bSDimitry Andric if (Loc.isReg() && Loc.getReg() && 10122cab237bSDimitry Andric TargetRegisterInfo::isVirtualRegister(Loc.getReg())) { 10132754fe60SDimitry Andric unsigned VirtReg = Loc.getReg(); 10142754fe60SDimitry Andric if (VRM.isAssignedReg(VirtReg) && 10152754fe60SDimitry Andric TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) { 1016bd5abe19SDimitry Andric // This can create a %noreg operand in rare cases when the sub-register 1017bd5abe19SDimitry Andric // index is no longer available. That means the user value is in a 1018bd5abe19SDimitry Andric // non-existent sub-register, and %noreg is exactly what we want. 10192754fe60SDimitry Andric Loc.substPhysReg(VRM.getPhys(VirtReg), TRI); 1020dff0c46cSDimitry Andric } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) { 10212754fe60SDimitry Andric // FIXME: Translate SubIdx to a stackslot offset. 10222754fe60SDimitry Andric Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg)); 10232cab237bSDimitry Andric Spilled = true; 10242754fe60SDimitry Andric } else { 10252754fe60SDimitry Andric Loc.setReg(0); 10262754fe60SDimitry Andric Loc.setSubReg(0); 10272754fe60SDimitry Andric } 10282cab237bSDimitry Andric } 10292cab237bSDimitry Andric 10302cab237bSDimitry Andric // Insert this location if it doesn't already exist and record a mapping 10312cab237bSDimitry Andric // from the old number to the new number. 10322cab237bSDimitry Andric auto InsertResult = NewLocations.insert({Loc, Spilled}); 10332cab237bSDimitry Andric unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first); 10342cab237bSDimitry Andric LocNoMap[I] = NewLocNo; 10352cab237bSDimitry Andric } 10362cab237bSDimitry Andric 10372cab237bSDimitry Andric // Rewrite the locations and record which ones were spill slots. 10382cab237bSDimitry Andric locations.clear(); 10392cab237bSDimitry Andric SpilledLocations.clear(); 10402cab237bSDimitry Andric SpilledLocations.resize(NewLocations.size()); 10412cab237bSDimitry Andric for (auto &Pair : NewLocations) { 10422cab237bSDimitry Andric locations.push_back(Pair.first); 10432cab237bSDimitry Andric if (Pair.second) { 10442cab237bSDimitry Andric unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair); 10452cab237bSDimitry Andric SpilledLocations.set(NewLocNo); 10462754fe60SDimitry Andric } 10472754fe60SDimitry Andric } 10482754fe60SDimitry Andric 10492cab237bSDimitry Andric // Update the interval map, but only coalesce left, since intervals to the 10502cab237bSDimitry Andric // right use the old location numbers. This should merge two contiguous 10512cab237bSDimitry Andric // DBG_VALUE intervals with different vregs that were allocated to the same 10522cab237bSDimitry Andric // physical register. 10532cab237bSDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { 10542cab237bSDimitry Andric DbgValueLocation Loc = I.value(); 10552cab237bSDimitry Andric unsigned NewLocNo = LocNoMap[Loc.locNo()]; 10562cab237bSDimitry Andric I.setValueUnchecked(Loc.changeLocNo(NewLocNo)); 10572cab237bSDimitry Andric I.setStart(I.start()); 10582cab237bSDimitry Andric } 10592cab237bSDimitry Andric } 10602cab237bSDimitry Andric 10612cab237bSDimitry Andric /// Find an iterator for inserting a DBG_VALUE instruction. 10622754fe60SDimitry Andric static MachineBasicBlock::iterator 10632754fe60SDimitry Andric findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, 10642754fe60SDimitry Andric LiveIntervals &LIS) { 10652754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 10662754fe60SDimitry Andric Idx = Idx.getBaseIndex(); 10672754fe60SDimitry Andric 10682754fe60SDimitry Andric // Try to find an insert location by going backwards from Idx. 10692754fe60SDimitry Andric MachineInstr *MI; 10702754fe60SDimitry Andric while (!(MI = LIS.getInstructionFromIndex(Idx))) { 10712754fe60SDimitry Andric // We've reached the beginning of MBB. 10722754fe60SDimitry Andric if (Idx == Start) { 1073d88c1a5aSDimitry Andric MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin()); 10742754fe60SDimitry Andric return I; 10752754fe60SDimitry Andric } 10762754fe60SDimitry Andric Idx = Idx.getPrevIndex(); 10772754fe60SDimitry Andric } 10782754fe60SDimitry Andric 10792754fe60SDimitry Andric // Don't insert anything after the first terminator, though. 1080dff0c46cSDimitry Andric return MI->isTerminator() ? MBB->getFirstTerminator() : 108191bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI)); 10822754fe60SDimitry Andric } 10832754fe60SDimitry Andric 10842cab237bSDimitry Andric /// Find an iterator for inserting the next DBG_VALUE instruction 10852cab237bSDimitry Andric /// (or end if no more insert locations found). 10862cab237bSDimitry Andric static MachineBasicBlock::iterator 10872cab237bSDimitry Andric findNextInsertLocation(MachineBasicBlock *MBB, 10882cab237bSDimitry Andric MachineBasicBlock::iterator I, 10892cab237bSDimitry Andric SlotIndex StopIdx, MachineOperand &LocMO, 10902754fe60SDimitry Andric LiveIntervals &LIS, 10912cab237bSDimitry Andric const TargetRegisterInfo &TRI) { 10922cab237bSDimitry Andric if (!LocMO.isReg()) 10932cab237bSDimitry Andric return MBB->instr_end(); 10942cab237bSDimitry Andric unsigned Reg = LocMO.getReg(); 10952cab237bSDimitry Andric 10962cab237bSDimitry Andric // Find the next instruction in the MBB that define the register Reg. 10972cab237bSDimitry Andric while (I != MBB->end()) { 10982cab237bSDimitry Andric if (!LIS.isNotInMIMap(*I) && 10992cab237bSDimitry Andric SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I))) 11002cab237bSDimitry Andric break; 11012cab237bSDimitry Andric if (I->definesRegister(Reg, &TRI)) 11022cab237bSDimitry Andric // The insert location is directly after the instruction/bundle. 11032cab237bSDimitry Andric return std::next(I); 11042cab237bSDimitry Andric ++I; 11052cab237bSDimitry Andric } 11062cab237bSDimitry Andric return MBB->end(); 11072cab237bSDimitry Andric } 11082cab237bSDimitry Andric 11092cab237bSDimitry Andric void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx, 11102cab237bSDimitry Andric SlotIndex StopIdx, 11112cab237bSDimitry Andric DbgValueLocation Loc, bool Spilled, 11122cab237bSDimitry Andric LiveIntervals &LIS, 11132cab237bSDimitry Andric const TargetInstrInfo &TII, 11142cab237bSDimitry Andric const TargetRegisterInfo &TRI) { 11152cab237bSDimitry Andric SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB); 11162cab237bSDimitry Andric // Only search within the current MBB. 11172cab237bSDimitry Andric StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx; 11182cab237bSDimitry Andric MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS); 11192cab237bSDimitry Andric MachineOperand &MO = locations[Loc.locNo()]; 11206122f3e6SDimitry Andric ++NumInsertedDebugValues; 11212754fe60SDimitry Andric 1122ff0cc061SDimitry Andric assert(cast<DILocalVariable>(Variable) 1123ff0cc061SDimitry Andric ->isValidLocationForIntrinsic(getDebugLoc()) && 1124ff0cc061SDimitry Andric "Expected inlined-at fields to agree"); 11252cab237bSDimitry Andric 11262cab237bSDimitry Andric // If the location was spilled, the new DBG_VALUE will be indirect. If the 11272cab237bSDimitry Andric // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate 11282cab237bSDimitry Andric // that the original virtual register was a pointer. 11292cab237bSDimitry Andric const DIExpression *Expr = Expression; 11302cab237bSDimitry Andric bool IsIndirect = Loc.wasIndirect(); 11312cab237bSDimitry Andric if (Spilled) { 11322cab237bSDimitry Andric if (IsIndirect) 11332cab237bSDimitry Andric Expr = DIExpression::prepend(Expr, DIExpression::WithDeref); 11342cab237bSDimitry Andric IsIndirect = true; 11352cab237bSDimitry Andric } 11362cab237bSDimitry Andric 11372cab237bSDimitry Andric assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index"); 11382cab237bSDimitry Andric 11392cab237bSDimitry Andric do { 11402cab237bSDimitry Andric MachineInstrBuilder MIB = 1141ff0cc061SDimitry Andric BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE)) 11422cab237bSDimitry Andric .add(MO); 11432cab237bSDimitry Andric if (IsIndirect) 11442cab237bSDimitry Andric MIB.addImm(0U); 11452cab237bSDimitry Andric else 11462cab237bSDimitry Andric MIB.addReg(0U, RegState::Debug); 11472cab237bSDimitry Andric MIB.addMetadata(Variable).addMetadata(Expr); 11482cab237bSDimitry Andric 11492cab237bSDimitry Andric // Continue and insert DBG_VALUES after every redefinition of register 11502cab237bSDimitry Andric // associated with the debug value within the range 11512cab237bSDimitry Andric I = findNextInsertLocation(MBB, I, StopIdx, MO, LIS, TRI); 11522cab237bSDimitry Andric } while (I != MBB->end()); 11532754fe60SDimitry Andric } 11542754fe60SDimitry Andric 11552754fe60SDimitry Andric void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, 11562cab237bSDimitry Andric const TargetInstrInfo &TII, 11572cab237bSDimitry Andric const TargetRegisterInfo &TRI, 11582cab237bSDimitry Andric const BitVector &SpilledLocations) { 11592754fe60SDimitry Andric MachineFunction::iterator MFEnd = VRM->getMachineFunction().end(); 11602754fe60SDimitry Andric 11612754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid();) { 11622754fe60SDimitry Andric SlotIndex Start = I.start(); 11632754fe60SDimitry Andric SlotIndex Stop = I.stop(); 11642cab237bSDimitry Andric DbgValueLocation Loc = I.value(); 11652cab237bSDimitry Andric bool Spilled = !Loc.isUndef() ? SpilledLocations.test(Loc.locNo()) : false; 11662cab237bSDimitry Andric 11672cab237bSDimitry Andric // If the interval start was trimmed to the lexical scope insert the 11682cab237bSDimitry Andric // DBG_VALUE at the previous index (otherwise it appears after the 11692cab237bSDimitry Andric // first instruction in the range). 11702cab237bSDimitry Andric if (trimmedDefs.count(Start)) 11712cab237bSDimitry Andric Start = Start.getPrevIndex(); 11722cab237bSDimitry Andric 11732cab237bSDimitry Andric DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo()); 11747d523365SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator(); 11757d523365SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB); 11762754fe60SDimitry Andric 11772cab237bSDimitry Andric DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); 11782cab237bSDimitry Andric insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, LIS, TII, TRI); 11792754fe60SDimitry Andric // This interval may span multiple basic blocks. 11802754fe60SDimitry Andric // Insert a DBG_VALUE into each one. 11812754fe60SDimitry Andric while (Stop > MBBEnd) { 11822754fe60SDimitry Andric // Move to the next block. 11832754fe60SDimitry Andric Start = MBBEnd; 11842754fe60SDimitry Andric if (++MBB == MFEnd) 11852754fe60SDimitry Andric break; 11867d523365SDimitry Andric MBBEnd = LIS.getMBBEndIdx(&*MBB); 11872cab237bSDimitry Andric DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); 11882cab237bSDimitry Andric insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, LIS, TII, TRI); 11892754fe60SDimitry Andric } 11902754fe60SDimitry Andric DEBUG(dbgs() << '\n'); 11912754fe60SDimitry Andric if (MBB == MFEnd) 11922754fe60SDimitry Andric break; 11932754fe60SDimitry Andric 11942754fe60SDimitry Andric ++I; 11952754fe60SDimitry Andric } 11962754fe60SDimitry Andric } 11972754fe60SDimitry Andric 11982754fe60SDimitry Andric void LDVImpl::emitDebugValues(VirtRegMap *VRM) { 11992754fe60SDimitry Andric DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); 120039d628a0SDimitry Andric if (!MF) 120139d628a0SDimitry Andric return; 120239d628a0SDimitry Andric const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 12032cab237bSDimitry Andric BitVector SpilledLocations; 12042754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 1205ff0cc061SDimitry Andric DEBUG(userValues[i]->print(dbgs(), TRI)); 12062cab237bSDimitry Andric userValues[i]->rewriteLocations(*VRM, *TRI, SpilledLocations); 12072cab237bSDimitry Andric userValues[i]->emitDebugValues(VRM, *LIS, *TII, *TRI, SpilledLocations); 12082754fe60SDimitry Andric } 1209139f7f9bSDimitry Andric EmitDone = true; 12102754fe60SDimitry Andric } 12112754fe60SDimitry Andric 12122754fe60SDimitry Andric void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) { 12132754fe60SDimitry Andric if (pImpl) 12142754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM); 12152754fe60SDimitry Andric } 12162754fe60SDimitry Andric 121739d628a0SDimitry Andric bool LiveDebugVariables::doInitialization(Module &M) { 121839d628a0SDimitry Andric return Pass::doInitialization(M); 121939d628a0SDimitry Andric } 12202754fe60SDimitry Andric 12217a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1222edd7eaddSDimitry Andric LLVM_DUMP_METHOD void LiveDebugVariables::dump() const { 12232754fe60SDimitry Andric if (pImpl) 12242754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->print(dbgs()); 12252754fe60SDimitry Andric } 12262754fe60SDimitry Andric #endif 1227