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" 232754fe60SDimitry Andric #include "llvm/ADT/IntervalMap.h" 246122f3e6SDimitry Andric #include "llvm/ADT/Statistic.h" 256122f3e6SDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 262754fe60SDimitry Andric #include "llvm/CodeGen/LiveIntervalAnalysis.h" 272754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 282754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 292754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 303b0f4066SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 312754fe60SDimitry Andric #include "llvm/CodeGen/Passes.h" 32139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h" 33139f7f9bSDimitry Andric #include "llvm/IR/Constants.h" 3491bc56edSDimitry Andric #include "llvm/IR/DebugInfo.h" 35139f7f9bSDimitry Andric #include "llvm/IR/Metadata.h" 36139f7f9bSDimitry Andric #include "llvm/IR/Value.h" 372754fe60SDimitry Andric #include "llvm/Support/CommandLine.h" 382754fe60SDimitry Andric #include "llvm/Support/Debug.h" 392754fe60SDimitry Andric #include "llvm/Target/TargetInstrInfo.h" 402754fe60SDimitry Andric #include "llvm/Target/TargetMachine.h" 412754fe60SDimitry Andric #include "llvm/Target/TargetRegisterInfo.h" 422754fe60SDimitry Andric 4391bc56edSDimitry Andric #include <memory> 4491bc56edSDimitry Andric 452754fe60SDimitry Andric using namespace llvm; 462754fe60SDimitry Andric 4791bc56edSDimitry Andric #define DEBUG_TYPE "livedebug" 4891bc56edSDimitry Andric 492754fe60SDimitry Andric static cl::opt<bool> 502754fe60SDimitry Andric EnableLDV("live-debug-variables", cl::init(true), 512754fe60SDimitry Andric cl::desc("Enable the live debug variables pass"), cl::Hidden); 522754fe60SDimitry Andric 536122f3e6SDimitry Andric STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted"); 542754fe60SDimitry Andric char LiveDebugVariables::ID = 0; 552754fe60SDimitry Andric 562754fe60SDimitry Andric INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars", 572754fe60SDimitry Andric "Debug Variable Analysis", false, false) 582754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 592754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 602754fe60SDimitry Andric INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars", 612754fe60SDimitry Andric "Debug Variable Analysis", false, false) 622754fe60SDimitry Andric 632754fe60SDimitry Andric void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const { 642754fe60SDimitry Andric AU.addRequired<MachineDominatorTree>(); 652754fe60SDimitry Andric AU.addRequiredTransitive<LiveIntervals>(); 662754fe60SDimitry Andric AU.setPreservesAll(); 672754fe60SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 682754fe60SDimitry Andric } 692754fe60SDimitry Andric 7091bc56edSDimitry Andric LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(nullptr) { 712754fe60SDimitry Andric initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 722754fe60SDimitry Andric } 732754fe60SDimitry Andric 742754fe60SDimitry Andric /// LocMap - Map of where a user value is live, and its location. 752754fe60SDimitry Andric typedef IntervalMap<SlotIndex, unsigned, 4> LocMap; 762754fe60SDimitry Andric 776122f3e6SDimitry Andric namespace { 7891bc56edSDimitry Andric /// UserValueScopes - Keeps track of lexical scopes associated with a 796122f3e6SDimitry Andric /// user value's source location. 806122f3e6SDimitry Andric class UserValueScopes { 816122f3e6SDimitry Andric DebugLoc DL; 826122f3e6SDimitry Andric LexicalScopes &LS; 836122f3e6SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 4> LBlocks; 846122f3e6SDimitry Andric 856122f3e6SDimitry Andric public: 866122f3e6SDimitry Andric UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(D), LS(L) {} 876122f3e6SDimitry Andric 886122f3e6SDimitry Andric /// dominates - Return true if current scope dominates at least one machine 896122f3e6SDimitry Andric /// instruction in a given machine basic block. 906122f3e6SDimitry Andric bool dominates(MachineBasicBlock *MBB) { 916122f3e6SDimitry Andric if (LBlocks.empty()) 926122f3e6SDimitry Andric LS.getMachineBasicBlocks(DL, LBlocks); 936122f3e6SDimitry Andric if (LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB)) 946122f3e6SDimitry Andric return true; 956122f3e6SDimitry Andric return false; 966122f3e6SDimitry Andric } 976122f3e6SDimitry Andric }; 986122f3e6SDimitry Andric } // end anonymous namespace 996122f3e6SDimitry Andric 1002754fe60SDimitry Andric /// UserValue - A user value is a part of a debug info user variable. 1012754fe60SDimitry Andric /// 1022754fe60SDimitry Andric /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register 1032754fe60SDimitry Andric /// holds part of a user variable. The part is identified by a byte offset. 1042754fe60SDimitry Andric /// 1052754fe60SDimitry Andric /// UserValues are grouped into equivalence classes for easier searching. Two 1062754fe60SDimitry Andric /// user values are related if they refer to the same variable, or if they are 1072754fe60SDimitry Andric /// held by the same virtual register. The equivalence class is the transitive 1082754fe60SDimitry Andric /// closure of that relation. 1092754fe60SDimitry Andric namespace { 1103b0f4066SDimitry Andric class LDVImpl; 1112754fe60SDimitry Andric class UserValue { 1122754fe60SDimitry Andric const MDNode *variable; ///< The debug info variable we are part of. 1132754fe60SDimitry Andric unsigned offset; ///< Byte offset into variable. 114f785676fSDimitry Andric bool IsIndirect; ///< true if this is a register-indirect+offset value. 1152754fe60SDimitry Andric DebugLoc dl; ///< The debug location for the variable. This is 1162754fe60SDimitry Andric ///< used by dwarf writer to find lexical scope. 1172754fe60SDimitry Andric UserValue *leader; ///< Equivalence class leader. 1182754fe60SDimitry Andric UserValue *next; ///< Next value in equivalence class, or null. 1192754fe60SDimitry Andric 1202754fe60SDimitry Andric /// Numbered locations referenced by locmap. 1212754fe60SDimitry Andric SmallVector<MachineOperand, 4> locations; 1222754fe60SDimitry Andric 1232754fe60SDimitry Andric /// Map of slot indices where this value is live. 1242754fe60SDimitry Andric LocMap locInts; 1252754fe60SDimitry Andric 1262754fe60SDimitry Andric /// coalesceLocation - After LocNo was changed, check if it has become 1272754fe60SDimitry Andric /// identical to another location, and coalesce them. This may cause LocNo or 1282754fe60SDimitry Andric /// a later location to be erased, but no earlier location will be erased. 1292754fe60SDimitry Andric void coalesceLocation(unsigned LocNo); 1302754fe60SDimitry Andric 1312754fe60SDimitry Andric /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo. 1322754fe60SDimitry Andric void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo, 1332754fe60SDimitry Andric LiveIntervals &LIS, const TargetInstrInfo &TII); 1342754fe60SDimitry Andric 135bd5abe19SDimitry Andric /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs 136bd5abe19SDimitry Andric /// is live. Returns true if any changes were made. 137f785676fSDimitry Andric bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 138f785676fSDimitry Andric LiveIntervals &LIS); 139bd5abe19SDimitry Andric 1402754fe60SDimitry Andric public: 1412754fe60SDimitry Andric /// UserValue - Create a new UserValue. 142f785676fSDimitry Andric UserValue(const MDNode *var, unsigned o, bool i, DebugLoc L, 1432754fe60SDimitry Andric LocMap::Allocator &alloc) 144f785676fSDimitry Andric : variable(var), offset(o), IsIndirect(i), dl(L), leader(this), 14591bc56edSDimitry Andric next(nullptr), locInts(alloc) 1462754fe60SDimitry Andric {} 1472754fe60SDimitry Andric 1482754fe60SDimitry Andric /// getLeader - Get the leader of this value's equivalence class. 1492754fe60SDimitry Andric UserValue *getLeader() { 1502754fe60SDimitry Andric UserValue *l = leader; 1512754fe60SDimitry Andric while (l != l->leader) 1522754fe60SDimitry Andric l = l->leader; 1532754fe60SDimitry Andric return leader = l; 1542754fe60SDimitry Andric } 1552754fe60SDimitry Andric 1562754fe60SDimitry Andric /// getNext - Return the next UserValue in the equivalence class. 1572754fe60SDimitry Andric UserValue *getNext() const { return next; } 1582754fe60SDimitry Andric 15917a519f9SDimitry Andric /// match - Does this UserValue match the parameters? 16091bc56edSDimitry Andric bool match(const MDNode *Var, unsigned Offset, bool indirect) const { 16191bc56edSDimitry Andric return Var == variable && Offset == offset && indirect == IsIndirect; 1622754fe60SDimitry Andric } 1632754fe60SDimitry Andric 1642754fe60SDimitry Andric /// merge - Merge equivalence classes. 1652754fe60SDimitry Andric static UserValue *merge(UserValue *L1, UserValue *L2) { 1662754fe60SDimitry Andric L2 = L2->getLeader(); 1672754fe60SDimitry Andric if (!L1) 1682754fe60SDimitry Andric return L2; 1692754fe60SDimitry Andric L1 = L1->getLeader(); 1702754fe60SDimitry Andric if (L1 == L2) 1712754fe60SDimitry Andric return L1; 1722754fe60SDimitry Andric // Splice L2 before L1's members. 1732754fe60SDimitry Andric UserValue *End = L2; 1742754fe60SDimitry Andric while (End->next) 1752754fe60SDimitry Andric End->leader = L1, End = End->next; 1762754fe60SDimitry Andric End->leader = L1; 1772754fe60SDimitry Andric End->next = L1->next; 1782754fe60SDimitry Andric L1->next = L2; 1792754fe60SDimitry Andric return L1; 1802754fe60SDimitry Andric } 1812754fe60SDimitry Andric 1822754fe60SDimitry Andric /// getLocationNo - Return the location number that matches Loc. 1832754fe60SDimitry Andric unsigned getLocationNo(const MachineOperand &LocMO) { 1843b0f4066SDimitry Andric if (LocMO.isReg()) { 1853b0f4066SDimitry Andric if (LocMO.getReg() == 0) 1862754fe60SDimitry Andric return ~0u; 1873b0f4066SDimitry Andric // For register locations we dont care about use/def and other flags. 1883b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 1893b0f4066SDimitry Andric if (locations[i].isReg() && 1903b0f4066SDimitry Andric locations[i].getReg() == LocMO.getReg() && 1913b0f4066SDimitry Andric locations[i].getSubReg() == LocMO.getSubReg()) 1923b0f4066SDimitry Andric return i; 1933b0f4066SDimitry Andric } else 1942754fe60SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 1952754fe60SDimitry Andric if (LocMO.isIdenticalTo(locations[i])) 1962754fe60SDimitry Andric return i; 1972754fe60SDimitry Andric locations.push_back(LocMO); 1982754fe60SDimitry Andric // We are storing a MachineOperand outside a MachineInstr. 1992754fe60SDimitry Andric locations.back().clearParent(); 2003b0f4066SDimitry Andric // Don't store def operands. 2013b0f4066SDimitry Andric if (locations.back().isReg()) 2023b0f4066SDimitry Andric locations.back().setIsUse(); 2032754fe60SDimitry Andric return locations.size() - 1; 2042754fe60SDimitry Andric } 2052754fe60SDimitry Andric 2063b0f4066SDimitry Andric /// mapVirtRegs - Ensure that all virtual register locations are mapped. 2073b0f4066SDimitry Andric void mapVirtRegs(LDVImpl *LDV); 2083b0f4066SDimitry Andric 2092754fe60SDimitry Andric /// addDef - Add a definition point to this value. 2102754fe60SDimitry Andric void addDef(SlotIndex Idx, const MachineOperand &LocMO) { 2112754fe60SDimitry Andric // Add a singular (Idx,Idx) -> Loc mapping. 2122754fe60SDimitry Andric LocMap::iterator I = locInts.find(Idx); 2132754fe60SDimitry Andric if (!I.valid() || I.start() != Idx) 2142754fe60SDimitry Andric I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO)); 2156122f3e6SDimitry Andric else 2166122f3e6SDimitry Andric // A later DBG_VALUE at the same SlotIndex overrides the old location. 2176122f3e6SDimitry Andric I.setValue(getLocationNo(LocMO)); 2182754fe60SDimitry Andric } 2192754fe60SDimitry Andric 2202754fe60SDimitry Andric /// extendDef - Extend the current definition as far as possible down the 2212754fe60SDimitry Andric /// dominator tree. Stop when meeting an existing def or when leaving the live 2222754fe60SDimitry Andric /// range of VNI. 2233b0f4066SDimitry Andric /// End points where VNI is no longer live are added to Kills. 2242754fe60SDimitry Andric /// @param Idx Starting point for the definition. 2252754fe60SDimitry Andric /// @param LocNo Location number to propagate. 226f785676fSDimitry Andric /// @param LR Restrict liveness to where LR has the value VNI. May be null. 227f785676fSDimitry Andric /// @param VNI When LR is not null, this is the value to restrict to. 2283b0f4066SDimitry Andric /// @param Kills Append end points of VNI's live range to Kills. 2292754fe60SDimitry Andric /// @param LIS Live intervals analysis. 2302754fe60SDimitry Andric /// @param MDT Dominator tree. 2312754fe60SDimitry Andric void extendDef(SlotIndex Idx, unsigned LocNo, 232f785676fSDimitry Andric LiveRange *LR, const VNInfo *VNI, 2333b0f4066SDimitry Andric SmallVectorImpl<SlotIndex> *Kills, 2346122f3e6SDimitry Andric LiveIntervals &LIS, MachineDominatorTree &MDT, 2356122f3e6SDimitry Andric UserValueScopes &UVS); 2362754fe60SDimitry Andric 2373b0f4066SDimitry Andric /// addDefsFromCopies - The value in LI/LocNo may be copies to other 2383b0f4066SDimitry Andric /// registers. Determine if any of the copies are available at the kill 2393b0f4066SDimitry Andric /// points, and add defs if possible. 2403b0f4066SDimitry Andric /// @param LI Scan for copies of the value in LI->reg. 2413b0f4066SDimitry Andric /// @param LocNo Location number of LI->reg. 2423b0f4066SDimitry Andric /// @param Kills Points where the range of LocNo could be extended. 2433b0f4066SDimitry Andric /// @param NewDefs Append (Idx, LocNo) of inserted defs here. 2443b0f4066SDimitry Andric void addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 2453b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 2463b0f4066SDimitry Andric SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 2473b0f4066SDimitry Andric MachineRegisterInfo &MRI, 2483b0f4066SDimitry Andric LiveIntervals &LIS); 2493b0f4066SDimitry Andric 2502754fe60SDimitry Andric /// computeIntervals - Compute the live intervals of all locations after 2512754fe60SDimitry Andric /// collecting all their def points. 2527ae0e2c9SDimitry Andric void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 2536122f3e6SDimitry Andric LiveIntervals &LIS, MachineDominatorTree &MDT, 2546122f3e6SDimitry Andric UserValueScopes &UVS); 2552754fe60SDimitry Andric 256bd5abe19SDimitry Andric /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is 257bd5abe19SDimitry Andric /// live. Returns true if any changes were made. 258f785676fSDimitry Andric bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 259f785676fSDimitry Andric LiveIntervals &LIS); 260bd5abe19SDimitry Andric 2612754fe60SDimitry Andric /// rewriteLocations - Rewrite virtual register locations according to the 2622754fe60SDimitry Andric /// provided virtual register map. 2632754fe60SDimitry Andric void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI); 2642754fe60SDimitry Andric 265139f7f9bSDimitry Andric /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 2662754fe60SDimitry Andric void emitDebugValues(VirtRegMap *VRM, 2672754fe60SDimitry Andric LiveIntervals &LIS, const TargetInstrInfo &TRI); 2682754fe60SDimitry Andric 2692754fe60SDimitry Andric /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A 2702754fe60SDimitry Andric /// variable may have more than one corresponding DBG_VALUE instructions. 2712754fe60SDimitry Andric /// Only first one needs DebugLoc to identify variable's lexical scope 2722754fe60SDimitry Andric /// in source file. 2732754fe60SDimitry Andric DebugLoc findDebugLoc(); 2746122f3e6SDimitry Andric 2756122f3e6SDimitry Andric /// getDebugLoc - Return DebugLoc of this UserValue. 2766122f3e6SDimitry Andric DebugLoc getDebugLoc() { return dl;} 277bd5abe19SDimitry Andric void print(raw_ostream&, const TargetMachine*); 2782754fe60SDimitry Andric }; 2792754fe60SDimitry Andric } // namespace 2802754fe60SDimitry Andric 2812754fe60SDimitry Andric /// LDVImpl - Implementation of the LiveDebugVariables pass. 2822754fe60SDimitry Andric namespace { 2832754fe60SDimitry Andric class LDVImpl { 2842754fe60SDimitry Andric LiveDebugVariables &pass; 2852754fe60SDimitry Andric LocMap::Allocator allocator; 2862754fe60SDimitry Andric MachineFunction *MF; 2872754fe60SDimitry Andric LiveIntervals *LIS; 2886122f3e6SDimitry Andric LexicalScopes LS; 2892754fe60SDimitry Andric MachineDominatorTree *MDT; 2902754fe60SDimitry Andric const TargetRegisterInfo *TRI; 2912754fe60SDimitry Andric 292139f7f9bSDimitry Andric /// Whether emitDebugValues is called. 293139f7f9bSDimitry Andric bool EmitDone; 294139f7f9bSDimitry Andric /// Whether the machine function is modified during the pass. 295139f7f9bSDimitry Andric bool ModifiedMF; 296139f7f9bSDimitry Andric 2972754fe60SDimitry Andric /// userValues - All allocated UserValue instances. 29891bc56edSDimitry Andric SmallVector<std::unique_ptr<UserValue>, 8> userValues; 2992754fe60SDimitry Andric 3002754fe60SDimitry Andric /// Map virtual register to eq class leader. 3012754fe60SDimitry Andric typedef DenseMap<unsigned, UserValue*> VRMap; 3022754fe60SDimitry Andric VRMap virtRegToEqClass; 3032754fe60SDimitry Andric 3042754fe60SDimitry Andric /// Map user variable to eq class leader. 3052754fe60SDimitry Andric typedef DenseMap<const MDNode *, UserValue*> UVMap; 3062754fe60SDimitry Andric UVMap userVarMap; 3072754fe60SDimitry Andric 3082754fe60SDimitry Andric /// getUserValue - Find or create a UserValue. 309f785676fSDimitry Andric UserValue *getUserValue(const MDNode *Var, unsigned Offset, 310f785676fSDimitry Andric bool IsIndirect, DebugLoc DL); 3112754fe60SDimitry Andric 3122754fe60SDimitry Andric /// lookupVirtReg - Find the EC leader for VirtReg or null. 3132754fe60SDimitry Andric UserValue *lookupVirtReg(unsigned VirtReg); 3142754fe60SDimitry Andric 3152754fe60SDimitry Andric /// handleDebugValue - Add DBG_VALUE instruction to our maps. 3162754fe60SDimitry Andric /// @param MI DBG_VALUE instruction 3172754fe60SDimitry Andric /// @param Idx Last valid SLotIndex before instruction. 3182754fe60SDimitry Andric /// @return True if the DBG_VALUE instruction should be deleted. 3192754fe60SDimitry Andric bool handleDebugValue(MachineInstr *MI, SlotIndex Idx); 3202754fe60SDimitry Andric 3212754fe60SDimitry Andric /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding 3222754fe60SDimitry Andric /// a UserValue def for each instruction. 3232754fe60SDimitry Andric /// @param mf MachineFunction to be scanned. 3242754fe60SDimitry Andric /// @return True if any debug values were found. 3252754fe60SDimitry Andric bool collectDebugValues(MachineFunction &mf); 3262754fe60SDimitry Andric 3272754fe60SDimitry Andric /// computeIntervals - Compute the live intervals of all user values after 3282754fe60SDimitry Andric /// collecting all their def points. 3292754fe60SDimitry Andric void computeIntervals(); 3302754fe60SDimitry Andric 3312754fe60SDimitry Andric public: 332139f7f9bSDimitry Andric LDVImpl(LiveDebugVariables *ps) : pass(*ps), EmitDone(false), 333139f7f9bSDimitry Andric ModifiedMF(false) {} 3342754fe60SDimitry Andric bool runOnMachineFunction(MachineFunction &mf); 3352754fe60SDimitry Andric 336139f7f9bSDimitry Andric /// clear - Release all memory. 3372754fe60SDimitry Andric void clear() { 3382754fe60SDimitry Andric userValues.clear(); 3392754fe60SDimitry Andric virtRegToEqClass.clear(); 3402754fe60SDimitry Andric userVarMap.clear(); 341139f7f9bSDimitry Andric // Make sure we call emitDebugValues if the machine function was modified. 342139f7f9bSDimitry Andric assert((!ModifiedMF || EmitDone) && 343139f7f9bSDimitry Andric "Dbg values are not emitted in LDV"); 344139f7f9bSDimitry Andric EmitDone = false; 345139f7f9bSDimitry Andric ModifiedMF = false; 3462754fe60SDimitry Andric } 3472754fe60SDimitry Andric 3483b0f4066SDimitry Andric /// mapVirtReg - Map virtual register to an equivalence class. 3493b0f4066SDimitry Andric void mapVirtReg(unsigned VirtReg, UserValue *EC); 3503b0f4066SDimitry Andric 351bd5abe19SDimitry Andric /// splitRegister - Replace all references to OldReg with NewRegs. 352f785676fSDimitry Andric void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs); 353bd5abe19SDimitry Andric 354139f7f9bSDimitry Andric /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 3552754fe60SDimitry Andric void emitDebugValues(VirtRegMap *VRM); 3562754fe60SDimitry Andric 3572754fe60SDimitry Andric void print(raw_ostream&); 3582754fe60SDimitry Andric }; 3592754fe60SDimitry Andric } // namespace 3602754fe60SDimitry Andric 361bd5abe19SDimitry Andric void UserValue::print(raw_ostream &OS, const TargetMachine *TM) { 3626122f3e6SDimitry Andric DIVariable DV(variable); 3636122f3e6SDimitry Andric OS << "!\""; 3646122f3e6SDimitry Andric DV.printExtendedName(OS); 3656122f3e6SDimitry Andric OS << "\"\t"; 3662754fe60SDimitry Andric if (offset) 3672754fe60SDimitry Andric OS << '+' << offset; 3682754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { 3692754fe60SDimitry Andric OS << " [" << I.start() << ';' << I.stop() << "):"; 3702754fe60SDimitry Andric if (I.value() == ~0u) 3712754fe60SDimitry Andric OS << "undef"; 3722754fe60SDimitry Andric else 3732754fe60SDimitry Andric OS << I.value(); 3742754fe60SDimitry Andric } 375bd5abe19SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) { 376bd5abe19SDimitry Andric OS << " Loc" << i << '='; 377bd5abe19SDimitry Andric locations[i].print(OS, TM); 378bd5abe19SDimitry Andric } 3792754fe60SDimitry Andric OS << '\n'; 3802754fe60SDimitry Andric } 3812754fe60SDimitry Andric 3822754fe60SDimitry Andric void LDVImpl::print(raw_ostream &OS) { 3832754fe60SDimitry Andric OS << "********** DEBUG VARIABLES **********\n"; 3842754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) 385bd5abe19SDimitry Andric userValues[i]->print(OS, &MF->getTarget()); 3862754fe60SDimitry Andric } 3872754fe60SDimitry Andric 3882754fe60SDimitry Andric void UserValue::coalesceLocation(unsigned LocNo) { 3892754fe60SDimitry Andric unsigned KeepLoc = 0; 3902754fe60SDimitry Andric for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) { 3912754fe60SDimitry Andric if (KeepLoc == LocNo) 3922754fe60SDimitry Andric continue; 3932754fe60SDimitry Andric if (locations[KeepLoc].isIdenticalTo(locations[LocNo])) 3942754fe60SDimitry Andric break; 3952754fe60SDimitry Andric } 3962754fe60SDimitry Andric // No matches. 3972754fe60SDimitry Andric if (KeepLoc == locations.size()) 3982754fe60SDimitry Andric return; 3992754fe60SDimitry Andric 4002754fe60SDimitry Andric // Keep the smaller location, erase the larger one. 4012754fe60SDimitry Andric unsigned EraseLoc = LocNo; 4022754fe60SDimitry Andric if (KeepLoc > EraseLoc) 4032754fe60SDimitry Andric std::swap(KeepLoc, EraseLoc); 4042754fe60SDimitry Andric locations.erase(locations.begin() + EraseLoc); 4052754fe60SDimitry Andric 4062754fe60SDimitry Andric // Rewrite values. 4072754fe60SDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { 4082754fe60SDimitry Andric unsigned v = I.value(); 4092754fe60SDimitry Andric if (v == EraseLoc) 4102754fe60SDimitry Andric I.setValue(KeepLoc); // Coalesce when possible. 4112754fe60SDimitry Andric else if (v > EraseLoc) 4122754fe60SDimitry Andric I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values. 4132754fe60SDimitry Andric } 4142754fe60SDimitry Andric } 4152754fe60SDimitry Andric 4163b0f4066SDimitry Andric void UserValue::mapVirtRegs(LDVImpl *LDV) { 4173b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 4183b0f4066SDimitry Andric if (locations[i].isReg() && 4193b0f4066SDimitry Andric TargetRegisterInfo::isVirtualRegister(locations[i].getReg())) 4203b0f4066SDimitry Andric LDV->mapVirtReg(locations[i].getReg(), this); 4213b0f4066SDimitry Andric } 4223b0f4066SDimitry Andric 4232754fe60SDimitry Andric UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset, 424f785676fSDimitry Andric bool IsIndirect, DebugLoc DL) { 4252754fe60SDimitry Andric UserValue *&Leader = userVarMap[Var]; 4262754fe60SDimitry Andric if (Leader) { 4272754fe60SDimitry Andric UserValue *UV = Leader->getLeader(); 4282754fe60SDimitry Andric Leader = UV; 4292754fe60SDimitry Andric for (; UV; UV = UV->getNext()) 43091bc56edSDimitry Andric if (UV->match(Var, Offset, IsIndirect)) 4312754fe60SDimitry Andric return UV; 4322754fe60SDimitry Andric } 4332754fe60SDimitry Andric 43491bc56edSDimitry Andric userValues.push_back( 43591bc56edSDimitry Andric make_unique<UserValue>(Var, Offset, IsIndirect, DL, allocator)); 43691bc56edSDimitry Andric UserValue *UV = userValues.back().get(); 4372754fe60SDimitry Andric Leader = UserValue::merge(Leader, UV); 4382754fe60SDimitry Andric return UV; 4392754fe60SDimitry Andric } 4402754fe60SDimitry Andric 4412754fe60SDimitry Andric void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) { 4422754fe60SDimitry Andric assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs"); 4432754fe60SDimitry Andric UserValue *&Leader = virtRegToEqClass[VirtReg]; 4442754fe60SDimitry Andric Leader = UserValue::merge(Leader, EC); 4452754fe60SDimitry Andric } 4462754fe60SDimitry Andric 4472754fe60SDimitry Andric UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) { 4482754fe60SDimitry Andric if (UserValue *UV = virtRegToEqClass.lookup(VirtReg)) 4492754fe60SDimitry Andric return UV->getLeader(); 45091bc56edSDimitry Andric return nullptr; 4512754fe60SDimitry Andric } 4522754fe60SDimitry Andric 4532754fe60SDimitry Andric bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) { 4542754fe60SDimitry Andric // DBG_VALUE loc, offset, variable 4552754fe60SDimitry Andric if (MI->getNumOperands() != 3 || 456f785676fSDimitry Andric !(MI->getOperand(1).isReg() || MI->getOperand(1).isImm()) || 457f785676fSDimitry Andric !MI->getOperand(2).isMetadata()) { 4582754fe60SDimitry Andric DEBUG(dbgs() << "Can't handle " << *MI); 4592754fe60SDimitry Andric return false; 4602754fe60SDimitry Andric } 4612754fe60SDimitry Andric 4622754fe60SDimitry Andric // Get or create the UserValue for (variable,offset). 463f785676fSDimitry Andric bool IsIndirect = MI->isIndirectDebugValue(); 464f785676fSDimitry Andric unsigned Offset = IsIndirect ? MI->getOperand(1).getImm() : 0; 4652754fe60SDimitry Andric const MDNode *Var = MI->getOperand(2).getMetadata(); 466f785676fSDimitry Andric //here. 467f785676fSDimitry Andric UserValue *UV = getUserValue(Var, Offset, IsIndirect, MI->getDebugLoc()); 4682754fe60SDimitry Andric UV->addDef(Idx, MI->getOperand(0)); 4692754fe60SDimitry Andric return true; 4702754fe60SDimitry Andric } 4712754fe60SDimitry Andric 4722754fe60SDimitry Andric bool LDVImpl::collectDebugValues(MachineFunction &mf) { 4732754fe60SDimitry Andric bool Changed = false; 4742754fe60SDimitry Andric for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE; 4752754fe60SDimitry Andric ++MFI) { 4762754fe60SDimitry Andric MachineBasicBlock *MBB = MFI; 4772754fe60SDimitry Andric for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); 4782754fe60SDimitry Andric MBBI != MBBE;) { 4792754fe60SDimitry Andric if (!MBBI->isDebugValue()) { 4802754fe60SDimitry Andric ++MBBI; 4812754fe60SDimitry Andric continue; 4822754fe60SDimitry Andric } 4832754fe60SDimitry Andric // DBG_VALUE has no slot index, use the previous instruction instead. 4842754fe60SDimitry Andric SlotIndex Idx = MBBI == MBB->begin() ? 4852754fe60SDimitry Andric LIS->getMBBStartIdx(MBB) : 48691bc56edSDimitry Andric LIS->getInstructionIndex(std::prev(MBBI)).getRegSlot(); 4872754fe60SDimitry Andric // Handle consecutive DBG_VALUE instructions with the same slot index. 4882754fe60SDimitry Andric do { 4892754fe60SDimitry Andric if (handleDebugValue(MBBI, Idx)) { 4902754fe60SDimitry Andric MBBI = MBB->erase(MBBI); 4912754fe60SDimitry Andric Changed = true; 4922754fe60SDimitry Andric } else 4932754fe60SDimitry Andric ++MBBI; 4942754fe60SDimitry Andric } while (MBBI != MBBE && MBBI->isDebugValue()); 4952754fe60SDimitry Andric } 4962754fe60SDimitry Andric } 4972754fe60SDimitry Andric return Changed; 4982754fe60SDimitry Andric } 4992754fe60SDimitry Andric 5002754fe60SDimitry Andric void UserValue::extendDef(SlotIndex Idx, unsigned LocNo, 501f785676fSDimitry Andric LiveRange *LR, const VNInfo *VNI, 5023b0f4066SDimitry Andric SmallVectorImpl<SlotIndex> *Kills, 5036122f3e6SDimitry Andric LiveIntervals &LIS, MachineDominatorTree &MDT, 5046122f3e6SDimitry Andric UserValueScopes &UVS) { 5052754fe60SDimitry Andric SmallVector<SlotIndex, 16> Todo; 5062754fe60SDimitry Andric Todo.push_back(Idx); 5072754fe60SDimitry Andric do { 5082754fe60SDimitry Andric SlotIndex Start = Todo.pop_back_val(); 5092754fe60SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start); 5102754fe60SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(MBB); 5112754fe60SDimitry Andric LocMap::iterator I = locInts.find(Start); 5122754fe60SDimitry Andric 5132754fe60SDimitry Andric // Limit to VNI's live range. 5142754fe60SDimitry Andric bool ToEnd = true; 515f785676fSDimitry Andric if (LR && VNI) { 516f785676fSDimitry Andric LiveInterval::Segment *Segment = LR->getSegmentContaining(Start); 517f785676fSDimitry Andric if (!Segment || Segment->valno != VNI) { 5183b0f4066SDimitry Andric if (Kills) 5193b0f4066SDimitry Andric Kills->push_back(Start); 5202754fe60SDimitry Andric continue; 5213b0f4066SDimitry Andric } 522f785676fSDimitry Andric if (Segment->end < Stop) 523f785676fSDimitry Andric Stop = Segment->end, ToEnd = false; 5242754fe60SDimitry Andric } 5252754fe60SDimitry Andric 5262754fe60SDimitry Andric // There could already be a short def at Start. 5272754fe60SDimitry Andric if (I.valid() && I.start() <= Start) { 5282754fe60SDimitry Andric // Stop when meeting a different location or an already extended interval. 5292754fe60SDimitry Andric Start = Start.getNextSlot(); 5302754fe60SDimitry Andric if (I.value() != LocNo || I.stop() != Start) 5312754fe60SDimitry Andric continue; 5322754fe60SDimitry Andric // This is a one-slot placeholder. Just skip it. 5332754fe60SDimitry Andric ++I; 5342754fe60SDimitry Andric } 5352754fe60SDimitry Andric 5362754fe60SDimitry Andric // Limited by the next def. 5372754fe60SDimitry Andric if (I.valid() && I.start() < Stop) 5382754fe60SDimitry Andric Stop = I.start(), ToEnd = false; 5393b0f4066SDimitry Andric // Limited by VNI's live range. 5403b0f4066SDimitry Andric else if (!ToEnd && Kills) 5413b0f4066SDimitry Andric Kills->push_back(Stop); 5422754fe60SDimitry Andric 5432754fe60SDimitry Andric if (Start >= Stop) 5442754fe60SDimitry Andric continue; 5452754fe60SDimitry Andric 5462754fe60SDimitry Andric I.insert(Start, Stop, LocNo); 5472754fe60SDimitry Andric 5482754fe60SDimitry Andric // If we extended to the MBB end, propagate down the dominator tree. 5492754fe60SDimitry Andric if (!ToEnd) 5502754fe60SDimitry Andric continue; 5512754fe60SDimitry Andric const std::vector<MachineDomTreeNode*> &Children = 5522754fe60SDimitry Andric MDT.getNode(MBB)->getChildren(); 5536122f3e6SDimitry Andric for (unsigned i = 0, e = Children.size(); i != e; ++i) { 5546122f3e6SDimitry Andric MachineBasicBlock *MBB = Children[i]->getBlock(); 5556122f3e6SDimitry Andric if (UVS.dominates(MBB)) 5566122f3e6SDimitry Andric Todo.push_back(LIS.getMBBStartIdx(MBB)); 5576122f3e6SDimitry Andric } 5582754fe60SDimitry Andric } while (!Todo.empty()); 5592754fe60SDimitry Andric } 5602754fe60SDimitry Andric 5612754fe60SDimitry Andric void 5623b0f4066SDimitry Andric UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 5633b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 5643b0f4066SDimitry Andric SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 5653b0f4066SDimitry Andric MachineRegisterInfo &MRI, LiveIntervals &LIS) { 5663b0f4066SDimitry Andric if (Kills.empty()) 5673b0f4066SDimitry Andric return; 5683b0f4066SDimitry Andric // Don't track copies from physregs, there are too many uses. 5693b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(LI->reg)) 5703b0f4066SDimitry Andric return; 5713b0f4066SDimitry Andric 5723b0f4066SDimitry Andric // Collect all the (vreg, valno) pairs that are copies of LI. 5733b0f4066SDimitry Andric SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues; 57491bc56edSDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) { 57591bc56edSDimitry Andric MachineInstr *MI = MO.getParent(); 5763b0f4066SDimitry Andric // Copies of the full value. 57791bc56edSDimitry Andric if (MO.getSubReg() || !MI->isCopy()) 5783b0f4066SDimitry Andric continue; 5793b0f4066SDimitry Andric unsigned DstReg = MI->getOperand(0).getReg(); 5803b0f4066SDimitry Andric 5813b0f4066SDimitry Andric // Don't follow copies to physregs. These are usually setting up call 5823b0f4066SDimitry Andric // arguments, and the argument registers are always call clobbered. We are 5833b0f4066SDimitry Andric // better off in the source register which could be a callee-saved register, 5843b0f4066SDimitry Andric // or it could be spilled. 5853b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(DstReg)) 5863b0f4066SDimitry Andric continue; 5873b0f4066SDimitry Andric 5883b0f4066SDimitry Andric // Is LocNo extended to reach this copy? If not, another def may be blocking 5893b0f4066SDimitry Andric // it, or we are looking at a wrong value of LI. 5903b0f4066SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(MI); 591dff0c46cSDimitry Andric LocMap::iterator I = locInts.find(Idx.getRegSlot(true)); 5923b0f4066SDimitry Andric if (!I.valid() || I.value() != LocNo) 5933b0f4066SDimitry Andric continue; 5943b0f4066SDimitry Andric 5953b0f4066SDimitry Andric if (!LIS.hasInterval(DstReg)) 5963b0f4066SDimitry Andric continue; 5973b0f4066SDimitry Andric LiveInterval *DstLI = &LIS.getInterval(DstReg); 598dff0c46cSDimitry Andric const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot()); 599dff0c46cSDimitry Andric assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value"); 6003b0f4066SDimitry Andric CopyValues.push_back(std::make_pair(DstLI, DstVNI)); 6013b0f4066SDimitry Andric } 6023b0f4066SDimitry Andric 6033b0f4066SDimitry Andric if (CopyValues.empty()) 6043b0f4066SDimitry Andric return; 6053b0f4066SDimitry Andric 6063b0f4066SDimitry Andric DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n'); 6073b0f4066SDimitry Andric 6083b0f4066SDimitry Andric // Try to add defs of the copied values for each kill point. 6093b0f4066SDimitry Andric for (unsigned i = 0, e = Kills.size(); i != e; ++i) { 6103b0f4066SDimitry Andric SlotIndex Idx = Kills[i]; 6113b0f4066SDimitry Andric for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) { 6123b0f4066SDimitry Andric LiveInterval *DstLI = CopyValues[j].first; 6133b0f4066SDimitry Andric const VNInfo *DstVNI = CopyValues[j].second; 6143b0f4066SDimitry Andric if (DstLI->getVNInfoAt(Idx) != DstVNI) 6153b0f4066SDimitry Andric continue; 6163b0f4066SDimitry Andric // Check that there isn't already a def at Idx 6173b0f4066SDimitry Andric LocMap::iterator I = locInts.find(Idx); 6183b0f4066SDimitry Andric if (I.valid() && I.start() <= Idx) 6193b0f4066SDimitry Andric continue; 6203b0f4066SDimitry Andric DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" 6213b0f4066SDimitry Andric << DstVNI->id << " in " << *DstLI << '\n'); 6223b0f4066SDimitry Andric MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); 6233b0f4066SDimitry Andric assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); 6243b0f4066SDimitry Andric unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); 6253b0f4066SDimitry Andric I.insert(Idx, Idx.getNextSlot(), LocNo); 6263b0f4066SDimitry Andric NewDefs.push_back(std::make_pair(Idx, LocNo)); 6273b0f4066SDimitry Andric break; 6283b0f4066SDimitry Andric } 6293b0f4066SDimitry Andric } 6303b0f4066SDimitry Andric } 6313b0f4066SDimitry Andric 6323b0f4066SDimitry Andric void 6333b0f4066SDimitry Andric UserValue::computeIntervals(MachineRegisterInfo &MRI, 6347ae0e2c9SDimitry Andric const TargetRegisterInfo &TRI, 6353b0f4066SDimitry Andric LiveIntervals &LIS, 6366122f3e6SDimitry Andric MachineDominatorTree &MDT, 6376122f3e6SDimitry Andric UserValueScopes &UVS) { 6382754fe60SDimitry Andric SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs; 6392754fe60SDimitry Andric 6402754fe60SDimitry Andric // Collect all defs to be extended (Skipping undefs). 6412754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) 6422754fe60SDimitry Andric if (I.value() != ~0u) 6432754fe60SDimitry Andric Defs.push_back(std::make_pair(I.start(), I.value())); 6442754fe60SDimitry Andric 6453b0f4066SDimitry Andric // Extend all defs, and possibly add new ones along the way. 6463b0f4066SDimitry Andric for (unsigned i = 0; i != Defs.size(); ++i) { 6472754fe60SDimitry Andric SlotIndex Idx = Defs[i].first; 6482754fe60SDimitry Andric unsigned LocNo = Defs[i].second; 6492754fe60SDimitry Andric const MachineOperand &Loc = locations[LocNo]; 6502754fe60SDimitry Andric 6517ae0e2c9SDimitry Andric if (!Loc.isReg()) { 65291bc56edSDimitry Andric extendDef(Idx, LocNo, nullptr, nullptr, nullptr, LIS, MDT, UVS); 6537ae0e2c9SDimitry Andric continue; 6547ae0e2c9SDimitry Andric } 6557ae0e2c9SDimitry Andric 6562754fe60SDimitry Andric // Register locations are constrained to where the register value is live. 6577ae0e2c9SDimitry Andric if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) { 65891bc56edSDimitry Andric LiveInterval *LI = nullptr; 65991bc56edSDimitry Andric const VNInfo *VNI = nullptr; 6607ae0e2c9SDimitry Andric if (LIS.hasInterval(Loc.getReg())) { 6617ae0e2c9SDimitry Andric LI = &LIS.getInterval(Loc.getReg()); 6627ae0e2c9SDimitry Andric VNI = LI->getVNInfoAt(Idx); 6637ae0e2c9SDimitry Andric } 6643b0f4066SDimitry Andric SmallVector<SlotIndex, 16> Kills; 6656122f3e6SDimitry Andric extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS); 6667ae0e2c9SDimitry Andric if (LI) 6673b0f4066SDimitry Andric addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS); 6687ae0e2c9SDimitry Andric continue; 6697ae0e2c9SDimitry Andric } 6707ae0e2c9SDimitry Andric 6717ae0e2c9SDimitry Andric // For physregs, use the live range of the first regunit as a guide. 6727ae0e2c9SDimitry Andric unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI); 673f785676fSDimitry Andric LiveRange *LR = &LIS.getRegUnit(Unit); 674f785676fSDimitry Andric const VNInfo *VNI = LR->getVNInfoAt(Idx); 6757ae0e2c9SDimitry Andric // Don't track copies from physregs, it is too expensive. 67691bc56edSDimitry Andric extendDef(Idx, LocNo, LR, VNI, nullptr, LIS, MDT, UVS); 6772754fe60SDimitry Andric } 6782754fe60SDimitry Andric 6792754fe60SDimitry Andric // Finally, erase all the undefs. 6802754fe60SDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid();) 6812754fe60SDimitry Andric if (I.value() == ~0u) 6822754fe60SDimitry Andric I.erase(); 6832754fe60SDimitry Andric else 6842754fe60SDimitry Andric ++I; 6852754fe60SDimitry Andric } 6862754fe60SDimitry Andric 6872754fe60SDimitry Andric void LDVImpl::computeIntervals() { 6883b0f4066SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 6896122f3e6SDimitry Andric UserValueScopes UVS(userValues[i]->getDebugLoc(), LS); 6907ae0e2c9SDimitry Andric userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS); 6913b0f4066SDimitry Andric userValues[i]->mapVirtRegs(this); 6923b0f4066SDimitry Andric } 6932754fe60SDimitry Andric } 6942754fe60SDimitry Andric 6952754fe60SDimitry Andric bool LDVImpl::runOnMachineFunction(MachineFunction &mf) { 6962754fe60SDimitry Andric MF = &mf; 6972754fe60SDimitry Andric LIS = &pass.getAnalysis<LiveIntervals>(); 6982754fe60SDimitry Andric MDT = &pass.getAnalysis<MachineDominatorTree>(); 6992754fe60SDimitry Andric TRI = mf.getTarget().getRegisterInfo(); 7002754fe60SDimitry Andric clear(); 7016122f3e6SDimitry Andric LS.initialize(mf); 7022754fe60SDimitry Andric DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " 7033861d79fSDimitry Andric << mf.getName() << " **********\n"); 7042754fe60SDimitry Andric 7052754fe60SDimitry Andric bool Changed = collectDebugValues(mf); 7062754fe60SDimitry Andric computeIntervals(); 7072754fe60SDimitry Andric DEBUG(print(dbgs())); 708139f7f9bSDimitry Andric ModifiedMF = Changed; 7092754fe60SDimitry Andric return Changed; 7102754fe60SDimitry Andric } 7112754fe60SDimitry Andric 7122754fe60SDimitry Andric bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) { 7132754fe60SDimitry Andric if (!EnableLDV) 7142754fe60SDimitry Andric return false; 7152754fe60SDimitry Andric if (!pImpl) 7162754fe60SDimitry Andric pImpl = new LDVImpl(this); 7172754fe60SDimitry Andric return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf); 7182754fe60SDimitry Andric } 7192754fe60SDimitry Andric 7202754fe60SDimitry Andric void LiveDebugVariables::releaseMemory() { 7212754fe60SDimitry Andric if (pImpl) 7222754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->clear(); 7232754fe60SDimitry Andric } 7242754fe60SDimitry Andric 7252754fe60SDimitry Andric LiveDebugVariables::~LiveDebugVariables() { 7262754fe60SDimitry Andric if (pImpl) 7272754fe60SDimitry Andric delete static_cast<LDVImpl*>(pImpl); 7282754fe60SDimitry Andric } 7292754fe60SDimitry Andric 730bd5abe19SDimitry Andric //===----------------------------------------------------------------------===// 731bd5abe19SDimitry Andric // Live Range Splitting 732bd5abe19SDimitry Andric //===----------------------------------------------------------------------===// 733bd5abe19SDimitry Andric 734bd5abe19SDimitry Andric bool 735f785676fSDimitry Andric UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 736f785676fSDimitry Andric LiveIntervals& LIS) { 737bd5abe19SDimitry Andric DEBUG({ 738bd5abe19SDimitry Andric dbgs() << "Splitting Loc" << OldLocNo << '\t'; 73991bc56edSDimitry Andric print(dbgs(), nullptr); 740bd5abe19SDimitry Andric }); 741bd5abe19SDimitry Andric bool DidChange = false; 742bd5abe19SDimitry Andric LocMap::iterator LocMapI; 743bd5abe19SDimitry Andric LocMapI.setMap(locInts); 744bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) { 745f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(NewRegs[i]); 746bd5abe19SDimitry Andric if (LI->empty()) 747bd5abe19SDimitry Andric continue; 748bd5abe19SDimitry Andric 749bd5abe19SDimitry Andric // Don't allocate the new LocNo until it is needed. 750bd5abe19SDimitry Andric unsigned NewLocNo = ~0u; 751bd5abe19SDimitry Andric 752bd5abe19SDimitry Andric // Iterate over the overlaps between locInts and LI. 753bd5abe19SDimitry Andric LocMapI.find(LI->beginIndex()); 754bd5abe19SDimitry Andric if (!LocMapI.valid()) 755bd5abe19SDimitry Andric continue; 756bd5abe19SDimitry Andric LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start()); 757bd5abe19SDimitry Andric LiveInterval::iterator LIE = LI->end(); 758bd5abe19SDimitry Andric while (LocMapI.valid() && LII != LIE) { 759bd5abe19SDimitry Andric // At this point, we know that LocMapI.stop() > LII->start. 760bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 761bd5abe19SDimitry Andric if (LII == LIE) 762bd5abe19SDimitry Andric break; 763bd5abe19SDimitry Andric 764bd5abe19SDimitry Andric // Now LII->end > LocMapI.start(). Do we have an overlap? 765bd5abe19SDimitry Andric if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) { 766bd5abe19SDimitry Andric // Overlapping correct location. Allocate NewLocNo now. 767bd5abe19SDimitry Andric if (NewLocNo == ~0u) { 768bd5abe19SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(LI->reg, false); 769bd5abe19SDimitry Andric MO.setSubReg(locations[OldLocNo].getSubReg()); 770bd5abe19SDimitry Andric NewLocNo = getLocationNo(MO); 771bd5abe19SDimitry Andric DidChange = true; 772bd5abe19SDimitry Andric } 773bd5abe19SDimitry Andric 774bd5abe19SDimitry Andric SlotIndex LStart = LocMapI.start(); 775bd5abe19SDimitry Andric SlotIndex LStop = LocMapI.stop(); 776bd5abe19SDimitry Andric 777bd5abe19SDimitry Andric // Trim LocMapI down to the LII overlap. 778bd5abe19SDimitry Andric if (LStart < LII->start) 779bd5abe19SDimitry Andric LocMapI.setStartUnchecked(LII->start); 780bd5abe19SDimitry Andric if (LStop > LII->end) 781bd5abe19SDimitry Andric LocMapI.setStopUnchecked(LII->end); 782bd5abe19SDimitry Andric 783bd5abe19SDimitry Andric // Change the value in the overlap. This may trigger coalescing. 784bd5abe19SDimitry Andric LocMapI.setValue(NewLocNo); 785bd5abe19SDimitry Andric 786bd5abe19SDimitry Andric // Re-insert any removed OldLocNo ranges. 787bd5abe19SDimitry Andric if (LStart < LocMapI.start()) { 788bd5abe19SDimitry Andric LocMapI.insert(LStart, LocMapI.start(), OldLocNo); 789bd5abe19SDimitry Andric ++LocMapI; 790bd5abe19SDimitry Andric assert(LocMapI.valid() && "Unexpected coalescing"); 791bd5abe19SDimitry Andric } 792bd5abe19SDimitry Andric if (LStop > LocMapI.stop()) { 793bd5abe19SDimitry Andric ++LocMapI; 794bd5abe19SDimitry Andric LocMapI.insert(LII->end, LStop, OldLocNo); 795bd5abe19SDimitry Andric --LocMapI; 796bd5abe19SDimitry Andric } 797bd5abe19SDimitry Andric } 798bd5abe19SDimitry Andric 799bd5abe19SDimitry Andric // Advance to the next overlap. 800bd5abe19SDimitry Andric if (LII->end < LocMapI.stop()) { 801bd5abe19SDimitry Andric if (++LII == LIE) 802bd5abe19SDimitry Andric break; 803bd5abe19SDimitry Andric LocMapI.advanceTo(LII->start); 804bd5abe19SDimitry Andric } else { 805bd5abe19SDimitry Andric ++LocMapI; 806bd5abe19SDimitry Andric if (!LocMapI.valid()) 807bd5abe19SDimitry Andric break; 808bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 809bd5abe19SDimitry Andric } 810bd5abe19SDimitry Andric } 811bd5abe19SDimitry Andric } 812bd5abe19SDimitry Andric 813bd5abe19SDimitry Andric // Finally, remove any remaining OldLocNo intervals and OldLocNo itself. 814bd5abe19SDimitry Andric locations.erase(locations.begin() + OldLocNo); 815bd5abe19SDimitry Andric LocMapI.goToBegin(); 816bd5abe19SDimitry Andric while (LocMapI.valid()) { 817bd5abe19SDimitry Andric unsigned v = LocMapI.value(); 818bd5abe19SDimitry Andric if (v == OldLocNo) { 819bd5abe19SDimitry Andric DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';' 820bd5abe19SDimitry Andric << LocMapI.stop() << ")\n"); 821bd5abe19SDimitry Andric LocMapI.erase(); 822bd5abe19SDimitry Andric } else { 823bd5abe19SDimitry Andric if (v > OldLocNo) 824bd5abe19SDimitry Andric LocMapI.setValueUnchecked(v-1); 825bd5abe19SDimitry Andric ++LocMapI; 826bd5abe19SDimitry Andric } 827bd5abe19SDimitry Andric } 828bd5abe19SDimitry Andric 82991bc56edSDimitry Andric DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);}); 830bd5abe19SDimitry Andric return DidChange; 831bd5abe19SDimitry Andric } 832bd5abe19SDimitry Andric 833bd5abe19SDimitry Andric bool 834f785676fSDimitry Andric UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, 835f785676fSDimitry Andric LiveIntervals &LIS) { 836bd5abe19SDimitry Andric bool DidChange = false; 837bd5abe19SDimitry Andric // Split locations referring to OldReg. Iterate backwards so splitLocation can 838dff0c46cSDimitry Andric // safely erase unused locations. 839bd5abe19SDimitry Andric for (unsigned i = locations.size(); i ; --i) { 840bd5abe19SDimitry Andric unsigned LocNo = i-1; 841bd5abe19SDimitry Andric const MachineOperand *Loc = &locations[LocNo]; 842bd5abe19SDimitry Andric if (!Loc->isReg() || Loc->getReg() != OldReg) 843bd5abe19SDimitry Andric continue; 844f785676fSDimitry Andric DidChange |= splitLocation(LocNo, NewRegs, LIS); 845bd5abe19SDimitry Andric } 846bd5abe19SDimitry Andric return DidChange; 847bd5abe19SDimitry Andric } 848bd5abe19SDimitry Andric 849f785676fSDimitry Andric void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) { 850bd5abe19SDimitry Andric bool DidChange = false; 851bd5abe19SDimitry Andric for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext()) 852f785676fSDimitry Andric DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS); 853bd5abe19SDimitry Andric 854bd5abe19SDimitry Andric if (!DidChange) 855bd5abe19SDimitry Andric return; 856bd5abe19SDimitry Andric 857bd5abe19SDimitry Andric // Map all of the new virtual registers. 858bd5abe19SDimitry Andric UserValue *UV = lookupVirtReg(OldReg); 859bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) 860f785676fSDimitry Andric mapVirtReg(NewRegs[i], UV); 861bd5abe19SDimitry Andric } 862bd5abe19SDimitry Andric 863bd5abe19SDimitry Andric void LiveDebugVariables:: 864f785676fSDimitry Andric splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) { 865bd5abe19SDimitry Andric if (pImpl) 866bd5abe19SDimitry Andric static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs); 867bd5abe19SDimitry Andric } 868bd5abe19SDimitry Andric 8692754fe60SDimitry Andric void 8702754fe60SDimitry Andric UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) { 8712754fe60SDimitry Andric // Iterate over locations in reverse makes it easier to handle coalescing. 8722754fe60SDimitry Andric for (unsigned i = locations.size(); i ; --i) { 8732754fe60SDimitry Andric unsigned LocNo = i-1; 8742754fe60SDimitry Andric MachineOperand &Loc = locations[LocNo]; 8752754fe60SDimitry Andric // Only virtual registers are rewritten. 8762754fe60SDimitry Andric if (!Loc.isReg() || !Loc.getReg() || 8772754fe60SDimitry Andric !TargetRegisterInfo::isVirtualRegister(Loc.getReg())) 8782754fe60SDimitry Andric continue; 8792754fe60SDimitry Andric unsigned VirtReg = Loc.getReg(); 8802754fe60SDimitry Andric if (VRM.isAssignedReg(VirtReg) && 8812754fe60SDimitry Andric TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) { 882bd5abe19SDimitry Andric // This can create a %noreg operand in rare cases when the sub-register 883bd5abe19SDimitry Andric // index is no longer available. That means the user value is in a 884bd5abe19SDimitry Andric // non-existent sub-register, and %noreg is exactly what we want. 8852754fe60SDimitry Andric Loc.substPhysReg(VRM.getPhys(VirtReg), TRI); 886dff0c46cSDimitry Andric } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) { 8872754fe60SDimitry Andric // FIXME: Translate SubIdx to a stackslot offset. 8882754fe60SDimitry Andric Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg)); 8892754fe60SDimitry Andric } else { 8902754fe60SDimitry Andric Loc.setReg(0); 8912754fe60SDimitry Andric Loc.setSubReg(0); 8922754fe60SDimitry Andric } 8932754fe60SDimitry Andric coalesceLocation(LocNo); 8942754fe60SDimitry Andric } 8952754fe60SDimitry Andric } 8962754fe60SDimitry Andric 8972754fe60SDimitry Andric /// findInsertLocation - Find an iterator for inserting a DBG_VALUE 8982754fe60SDimitry Andric /// instruction. 8992754fe60SDimitry Andric static MachineBasicBlock::iterator 9002754fe60SDimitry Andric findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, 9012754fe60SDimitry Andric LiveIntervals &LIS) { 9022754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 9032754fe60SDimitry Andric Idx = Idx.getBaseIndex(); 9042754fe60SDimitry Andric 9052754fe60SDimitry Andric // Try to find an insert location by going backwards from Idx. 9062754fe60SDimitry Andric MachineInstr *MI; 9072754fe60SDimitry Andric while (!(MI = LIS.getInstructionFromIndex(Idx))) { 9082754fe60SDimitry Andric // We've reached the beginning of MBB. 9092754fe60SDimitry Andric if (Idx == Start) { 9102754fe60SDimitry Andric MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin()); 9112754fe60SDimitry Andric return I; 9122754fe60SDimitry Andric } 9132754fe60SDimitry Andric Idx = Idx.getPrevIndex(); 9142754fe60SDimitry Andric } 9152754fe60SDimitry Andric 9162754fe60SDimitry Andric // Don't insert anything after the first terminator, though. 917dff0c46cSDimitry Andric return MI->isTerminator() ? MBB->getFirstTerminator() : 91891bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI)); 9192754fe60SDimitry Andric } 9202754fe60SDimitry Andric 9212754fe60SDimitry Andric DebugLoc UserValue::findDebugLoc() { 9222754fe60SDimitry Andric DebugLoc D = dl; 9232754fe60SDimitry Andric dl = DebugLoc(); 9242754fe60SDimitry Andric return D; 9252754fe60SDimitry Andric } 9262754fe60SDimitry Andric void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, 9272754fe60SDimitry Andric unsigned LocNo, 9282754fe60SDimitry Andric LiveIntervals &LIS, 9292754fe60SDimitry Andric const TargetInstrInfo &TII) { 9302754fe60SDimitry Andric MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS); 9312754fe60SDimitry Andric MachineOperand &Loc = locations[LocNo]; 9326122f3e6SDimitry Andric ++NumInsertedDebugValues; 9332754fe60SDimitry Andric 934f785676fSDimitry Andric if (Loc.isReg()) 935f785676fSDimitry Andric BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE), 936f785676fSDimitry Andric IsIndirect, Loc.getReg(), offset, variable); 937f785676fSDimitry Andric else 9382754fe60SDimitry Andric BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE)) 9392754fe60SDimitry Andric .addOperand(Loc).addImm(offset).addMetadata(variable); 9402754fe60SDimitry Andric } 9412754fe60SDimitry Andric 9422754fe60SDimitry Andric void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, 9432754fe60SDimitry Andric const TargetInstrInfo &TII) { 9442754fe60SDimitry Andric MachineFunction::iterator MFEnd = VRM->getMachineFunction().end(); 9452754fe60SDimitry Andric 9462754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid();) { 9472754fe60SDimitry Andric SlotIndex Start = I.start(); 9482754fe60SDimitry Andric SlotIndex Stop = I.stop(); 9492754fe60SDimitry Andric unsigned LocNo = I.value(); 9502754fe60SDimitry Andric DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo); 9512754fe60SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start); 9522754fe60SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB); 9532754fe60SDimitry Andric 9542754fe60SDimitry Andric DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 9552754fe60SDimitry Andric insertDebugValue(MBB, Start, LocNo, LIS, TII); 9562754fe60SDimitry Andric // This interval may span multiple basic blocks. 9572754fe60SDimitry Andric // Insert a DBG_VALUE into each one. 9582754fe60SDimitry Andric while(Stop > MBBEnd) { 9592754fe60SDimitry Andric // Move to the next block. 9602754fe60SDimitry Andric Start = MBBEnd; 9612754fe60SDimitry Andric if (++MBB == MFEnd) 9622754fe60SDimitry Andric break; 9632754fe60SDimitry Andric MBBEnd = LIS.getMBBEndIdx(MBB); 9642754fe60SDimitry Andric DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 9652754fe60SDimitry Andric insertDebugValue(MBB, Start, LocNo, LIS, TII); 9662754fe60SDimitry Andric } 9672754fe60SDimitry Andric DEBUG(dbgs() << '\n'); 9682754fe60SDimitry Andric if (MBB == MFEnd) 9692754fe60SDimitry Andric break; 9702754fe60SDimitry Andric 9712754fe60SDimitry Andric ++I; 9722754fe60SDimitry Andric } 9732754fe60SDimitry Andric } 9742754fe60SDimitry Andric 9752754fe60SDimitry Andric void LDVImpl::emitDebugValues(VirtRegMap *VRM) { 9762754fe60SDimitry Andric DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); 9772754fe60SDimitry Andric const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 9782754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 979bd5abe19SDimitry Andric DEBUG(userValues[i]->print(dbgs(), &MF->getTarget())); 9802754fe60SDimitry Andric userValues[i]->rewriteLocations(*VRM, *TRI); 9812754fe60SDimitry Andric userValues[i]->emitDebugValues(VRM, *LIS, *TII); 9822754fe60SDimitry Andric } 983139f7f9bSDimitry Andric EmitDone = true; 9842754fe60SDimitry Andric } 9852754fe60SDimitry Andric 9862754fe60SDimitry Andric void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) { 9872754fe60SDimitry Andric if (pImpl) 9882754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM); 9892754fe60SDimitry Andric } 9902754fe60SDimitry Andric 9912754fe60SDimitry Andric 9922754fe60SDimitry Andric #ifndef NDEBUG 9932754fe60SDimitry Andric void LiveDebugVariables::dump() { 9942754fe60SDimitry Andric if (pImpl) 9952754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->print(dbgs()); 9962754fe60SDimitry Andric } 9972754fe60SDimitry Andric #endif 998