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" 252754fe60SDimitry Andric #include "llvm/CodeGen/LiveIntervalAnalysis.h" 262754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 272754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 282754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 293b0f4066SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 302754fe60SDimitry Andric #include "llvm/CodeGen/Passes.h" 31139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h" 32139f7f9bSDimitry Andric #include "llvm/IR/Constants.h" 3391bc56edSDimitry Andric #include "llvm/IR/DebugInfo.h" 34139f7f9bSDimitry Andric #include "llvm/IR/Metadata.h" 35139f7f9bSDimitry Andric #include "llvm/IR/Value.h" 362754fe60SDimitry Andric #include "llvm/Support/CommandLine.h" 372754fe60SDimitry Andric #include "llvm/Support/Debug.h" 38ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h" 392754fe60SDimitry Andric #include "llvm/Target/TargetInstrInfo.h" 402754fe60SDimitry Andric #include "llvm/Target/TargetMachine.h" 412754fe60SDimitry Andric #include "llvm/Target/TargetRegisterInfo.h" 4239d628a0SDimitry Andric #include "llvm/Target/TargetSubtargetInfo.h" 4391bc56edSDimitry Andric #include <memory> 443ca95b02SDimitry Andric #include <utility> 4591bc56edSDimitry Andric 462754fe60SDimitry Andric using namespace llvm; 472754fe60SDimitry Andric 48302affcbSDimitry Andric #define DEBUG_TYPE "livedebugvars" 4991bc56edSDimitry Andric 502754fe60SDimitry Andric static cl::opt<bool> 512754fe60SDimitry Andric EnableLDV("live-debug-variables", cl::init(true), 522754fe60SDimitry Andric cl::desc("Enable the live debug variables pass"), cl::Hidden); 532754fe60SDimitry Andric 546122f3e6SDimitry Andric STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted"); 552754fe60SDimitry Andric char LiveDebugVariables::ID = 0; 562754fe60SDimitry Andric 57302affcbSDimitry Andric INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE, 582754fe60SDimitry Andric "Debug Variable Analysis", false, false) 592754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 602754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 61302affcbSDimitry Andric INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE, 622754fe60SDimitry Andric "Debug Variable Analysis", false, false) 632754fe60SDimitry Andric 642754fe60SDimitry Andric void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const { 652754fe60SDimitry Andric AU.addRequired<MachineDominatorTree>(); 662754fe60SDimitry Andric AU.addRequiredTransitive<LiveIntervals>(); 672754fe60SDimitry Andric AU.setPreservesAll(); 682754fe60SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 692754fe60SDimitry Andric } 702754fe60SDimitry Andric 7191bc56edSDimitry Andric LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(nullptr) { 722754fe60SDimitry Andric initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 732754fe60SDimitry Andric } 742754fe60SDimitry Andric 752754fe60SDimitry Andric /// LocMap - Map of where a user value is live, and its location. 762754fe60SDimitry Andric typedef IntervalMap<SlotIndex, unsigned, 4> LocMap; 772754fe60SDimitry Andric 782754fe60SDimitry Andric /// UserValue - A user value is a part of a debug info user variable. 792754fe60SDimitry Andric /// 802754fe60SDimitry Andric /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register 812754fe60SDimitry Andric /// holds part of a user variable. The part is identified by a byte offset. 822754fe60SDimitry Andric /// 832754fe60SDimitry Andric /// UserValues are grouped into equivalence classes for easier searching. Two 842754fe60SDimitry Andric /// user values are related if they refer to the same variable, or if they are 852754fe60SDimitry Andric /// held by the same virtual register. The equivalence class is the transitive 862754fe60SDimitry Andric /// closure of that relation. 872754fe60SDimitry Andric namespace { 883b0f4066SDimitry Andric class LDVImpl; 892754fe60SDimitry Andric class UserValue { 9039d628a0SDimitry Andric const MDNode *Variable; ///< The debug info variable we are part of. 9139d628a0SDimitry Andric const MDNode *Expression; ///< Any complex address expression. 922754fe60SDimitry Andric unsigned offset; ///< Byte offset into variable. 93f785676fSDimitry Andric bool IsIndirect; ///< true if this is a register-indirect+offset value. 942754fe60SDimitry Andric DebugLoc dl; ///< The debug location for the variable. This is 952754fe60SDimitry Andric ///< used by dwarf writer to find lexical scope. 962754fe60SDimitry Andric UserValue *leader; ///< Equivalence class leader. 972754fe60SDimitry Andric UserValue *next; ///< Next value in equivalence class, or null. 982754fe60SDimitry Andric 992754fe60SDimitry Andric /// Numbered locations referenced by locmap. 1002754fe60SDimitry Andric SmallVector<MachineOperand, 4> locations; 1012754fe60SDimitry Andric 1022754fe60SDimitry Andric /// Map of slot indices where this value is live. 1032754fe60SDimitry Andric LocMap locInts; 1042754fe60SDimitry Andric 1052754fe60SDimitry Andric /// coalesceLocation - After LocNo was changed, check if it has become 1062754fe60SDimitry Andric /// identical to another location, and coalesce them. This may cause LocNo or 1072754fe60SDimitry Andric /// a later location to be erased, but no earlier location will be erased. 1082754fe60SDimitry Andric void coalesceLocation(unsigned LocNo); 1092754fe60SDimitry Andric 1102754fe60SDimitry Andric /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo. 1112754fe60SDimitry Andric void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo, 1122754fe60SDimitry Andric LiveIntervals &LIS, const TargetInstrInfo &TII); 1132754fe60SDimitry Andric 114bd5abe19SDimitry Andric /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs 115bd5abe19SDimitry Andric /// is live. Returns true if any changes were made. 116f785676fSDimitry Andric bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 117f785676fSDimitry Andric LiveIntervals &LIS); 118bd5abe19SDimitry Andric 1192754fe60SDimitry Andric public: 1202754fe60SDimitry Andric /// UserValue - Create a new UserValue. 12139d628a0SDimitry Andric UserValue(const MDNode *var, const MDNode *expr, unsigned o, bool i, 12239d628a0SDimitry Andric DebugLoc L, LocMap::Allocator &alloc) 1233ca95b02SDimitry Andric : Variable(var), Expression(expr), offset(o), IsIndirect(i), 1243ca95b02SDimitry Andric dl(std::move(L)), leader(this), next(nullptr), locInts(alloc) {} 1252754fe60SDimitry Andric 1262754fe60SDimitry Andric /// getLeader - Get the leader of this value's equivalence class. 1272754fe60SDimitry Andric UserValue *getLeader() { 1282754fe60SDimitry Andric UserValue *l = leader; 1292754fe60SDimitry Andric while (l != l->leader) 1302754fe60SDimitry Andric l = l->leader; 1312754fe60SDimitry Andric return leader = l; 1322754fe60SDimitry Andric } 1332754fe60SDimitry Andric 1342754fe60SDimitry Andric /// getNext - Return the next UserValue in the equivalence class. 1352754fe60SDimitry Andric UserValue *getNext() const { return next; } 1362754fe60SDimitry Andric 13717a519f9SDimitry Andric /// match - Does this UserValue match the parameters? 138ff0cc061SDimitry Andric bool match(const MDNode *Var, const MDNode *Expr, const DILocation *IA, 139ff0cc061SDimitry Andric unsigned Offset, bool indirect) const { 140ff0cc061SDimitry Andric return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA && 141ff0cc061SDimitry Andric Offset == offset && indirect == IsIndirect; 1422754fe60SDimitry Andric } 1432754fe60SDimitry Andric 1442754fe60SDimitry Andric /// merge - Merge equivalence classes. 1452754fe60SDimitry Andric static UserValue *merge(UserValue *L1, UserValue *L2) { 1462754fe60SDimitry Andric L2 = L2->getLeader(); 1472754fe60SDimitry Andric if (!L1) 1482754fe60SDimitry Andric return L2; 1492754fe60SDimitry Andric L1 = L1->getLeader(); 1502754fe60SDimitry Andric if (L1 == L2) 1512754fe60SDimitry Andric return L1; 1522754fe60SDimitry Andric // Splice L2 before L1's members. 1532754fe60SDimitry Andric UserValue *End = L2; 1543ca95b02SDimitry Andric while (End->next) { 1553ca95b02SDimitry Andric End->leader = L1; 1563ca95b02SDimitry Andric End = End->next; 1573ca95b02SDimitry Andric } 1582754fe60SDimitry Andric End->leader = L1; 1592754fe60SDimitry Andric End->next = L1->next; 1602754fe60SDimitry Andric L1->next = L2; 1612754fe60SDimitry Andric return L1; 1622754fe60SDimitry Andric } 1632754fe60SDimitry Andric 1642754fe60SDimitry Andric /// getLocationNo - Return the location number that matches Loc. 1652754fe60SDimitry Andric unsigned getLocationNo(const MachineOperand &LocMO) { 1663b0f4066SDimitry Andric if (LocMO.isReg()) { 1673b0f4066SDimitry Andric if (LocMO.getReg() == 0) 1682754fe60SDimitry Andric return ~0u; 1693b0f4066SDimitry Andric // For register locations we dont care about use/def and other flags. 1703b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 1713b0f4066SDimitry Andric if (locations[i].isReg() && 1723b0f4066SDimitry Andric locations[i].getReg() == LocMO.getReg() && 1733b0f4066SDimitry Andric locations[i].getSubReg() == LocMO.getSubReg()) 1743b0f4066SDimitry Andric return i; 1753b0f4066SDimitry Andric } else 1762754fe60SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 1772754fe60SDimitry Andric if (LocMO.isIdenticalTo(locations[i])) 1782754fe60SDimitry Andric return i; 1792754fe60SDimitry Andric locations.push_back(LocMO); 1802754fe60SDimitry Andric // We are storing a MachineOperand outside a MachineInstr. 1812754fe60SDimitry Andric locations.back().clearParent(); 1823b0f4066SDimitry Andric // Don't store def operands. 1833b0f4066SDimitry Andric if (locations.back().isReg()) 1843b0f4066SDimitry Andric locations.back().setIsUse(); 1852754fe60SDimitry Andric return locations.size() - 1; 1862754fe60SDimitry Andric } 1872754fe60SDimitry Andric 1883b0f4066SDimitry Andric /// mapVirtRegs - Ensure that all virtual register locations are mapped. 1893b0f4066SDimitry Andric void mapVirtRegs(LDVImpl *LDV); 1903b0f4066SDimitry Andric 1912754fe60SDimitry Andric /// addDef - Add a definition point to this value. 1922754fe60SDimitry Andric void addDef(SlotIndex Idx, const MachineOperand &LocMO) { 1932754fe60SDimitry Andric // Add a singular (Idx,Idx) -> Loc mapping. 1942754fe60SDimitry Andric LocMap::iterator I = locInts.find(Idx); 1952754fe60SDimitry Andric if (!I.valid() || I.start() != Idx) 1962754fe60SDimitry Andric I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO)); 1976122f3e6SDimitry Andric else 1986122f3e6SDimitry Andric // A later DBG_VALUE at the same SlotIndex overrides the old location. 1996122f3e6SDimitry Andric I.setValue(getLocationNo(LocMO)); 2002754fe60SDimitry Andric } 2012754fe60SDimitry Andric 202d88c1a5aSDimitry Andric /// extendDef - Extend the current definition as far as possible down. 203d88c1a5aSDimitry Andric /// Stop when meeting an existing def or when leaving the live 2042754fe60SDimitry Andric /// range of VNI. 2053b0f4066SDimitry Andric /// End points where VNI is no longer live are added to Kills. 2062754fe60SDimitry Andric /// @param Idx Starting point for the definition. 2072754fe60SDimitry Andric /// @param LocNo Location number to propagate. 208f785676fSDimitry Andric /// @param LR Restrict liveness to where LR has the value VNI. May be null. 209f785676fSDimitry Andric /// @param VNI When LR is not null, this is the value to restrict to. 2103b0f4066SDimitry Andric /// @param Kills Append end points of VNI's live range to Kills. 2112754fe60SDimitry Andric /// @param LIS Live intervals analysis. 2122754fe60SDimitry Andric void extendDef(SlotIndex Idx, unsigned LocNo, 213f785676fSDimitry Andric LiveRange *LR, const VNInfo *VNI, 2143b0f4066SDimitry Andric SmallVectorImpl<SlotIndex> *Kills, 215d88c1a5aSDimitry Andric LiveIntervals &LIS); 2162754fe60SDimitry Andric 2173b0f4066SDimitry Andric /// addDefsFromCopies - The value in LI/LocNo may be copies to other 2183b0f4066SDimitry Andric /// registers. Determine if any of the copies are available at the kill 2193b0f4066SDimitry Andric /// points, and add defs if possible. 2203b0f4066SDimitry Andric /// @param LI Scan for copies of the value in LI->reg. 2213b0f4066SDimitry Andric /// @param LocNo Location number of LI->reg. 2223b0f4066SDimitry Andric /// @param Kills Points where the range of LocNo could be extended. 2233b0f4066SDimitry Andric /// @param NewDefs Append (Idx, LocNo) of inserted defs here. 2243b0f4066SDimitry Andric void addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 2253b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 2263b0f4066SDimitry Andric SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 2273b0f4066SDimitry Andric MachineRegisterInfo &MRI, 2283b0f4066SDimitry Andric LiveIntervals &LIS); 2293b0f4066SDimitry Andric 2302754fe60SDimitry Andric /// computeIntervals - Compute the live intervals of all locations after 2312754fe60SDimitry Andric /// collecting all their def points. 2327ae0e2c9SDimitry Andric void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 233d88c1a5aSDimitry Andric LiveIntervals &LIS); 2342754fe60SDimitry Andric 235bd5abe19SDimitry Andric /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is 236bd5abe19SDimitry Andric /// live. Returns true if any changes were made. 237f785676fSDimitry Andric bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 238f785676fSDimitry Andric LiveIntervals &LIS); 239bd5abe19SDimitry Andric 2402754fe60SDimitry Andric /// rewriteLocations - Rewrite virtual register locations according to the 2412754fe60SDimitry Andric /// provided virtual register map. 2422754fe60SDimitry Andric void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI); 2432754fe60SDimitry Andric 244139f7f9bSDimitry Andric /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 2452754fe60SDimitry Andric void emitDebugValues(VirtRegMap *VRM, 2462754fe60SDimitry Andric LiveIntervals &LIS, const TargetInstrInfo &TRI); 2472754fe60SDimitry Andric 2486122f3e6SDimitry Andric /// getDebugLoc - Return DebugLoc of this UserValue. 2496122f3e6SDimitry Andric DebugLoc getDebugLoc() { return dl;} 250ff0cc061SDimitry Andric void print(raw_ostream &, const TargetRegisterInfo *); 2512754fe60SDimitry Andric }; 2522754fe60SDimitry Andric } // namespace 2532754fe60SDimitry Andric 2542754fe60SDimitry Andric /// LDVImpl - Implementation of the LiveDebugVariables pass. 2552754fe60SDimitry Andric namespace { 2562754fe60SDimitry Andric class LDVImpl { 2572754fe60SDimitry Andric LiveDebugVariables &pass; 2582754fe60SDimitry Andric LocMap::Allocator allocator; 2592754fe60SDimitry Andric MachineFunction *MF; 2602754fe60SDimitry Andric LiveIntervals *LIS; 2612754fe60SDimitry Andric const TargetRegisterInfo *TRI; 2622754fe60SDimitry Andric 263139f7f9bSDimitry Andric /// Whether emitDebugValues is called. 264139f7f9bSDimitry Andric bool EmitDone; 265139f7f9bSDimitry Andric /// Whether the machine function is modified during the pass. 266139f7f9bSDimitry Andric bool ModifiedMF; 267139f7f9bSDimitry Andric 2682754fe60SDimitry Andric /// userValues - All allocated UserValue instances. 26991bc56edSDimitry Andric SmallVector<std::unique_ptr<UserValue>, 8> userValues; 2702754fe60SDimitry Andric 2712754fe60SDimitry Andric /// Map virtual register to eq class leader. 2722754fe60SDimitry Andric typedef DenseMap<unsigned, UserValue*> VRMap; 2732754fe60SDimitry Andric VRMap virtRegToEqClass; 2742754fe60SDimitry Andric 2752754fe60SDimitry Andric /// Map user variable to eq class leader. 2762754fe60SDimitry Andric typedef DenseMap<const MDNode *, UserValue*> UVMap; 2772754fe60SDimitry Andric UVMap userVarMap; 2782754fe60SDimitry Andric 2792754fe60SDimitry Andric /// getUserValue - Find or create a UserValue. 28039d628a0SDimitry Andric UserValue *getUserValue(const MDNode *Var, const MDNode *Expr, 2813ca95b02SDimitry Andric unsigned Offset, bool IsIndirect, const DebugLoc &DL); 2822754fe60SDimitry Andric 2832754fe60SDimitry Andric /// lookupVirtReg - Find the EC leader for VirtReg or null. 2842754fe60SDimitry Andric UserValue *lookupVirtReg(unsigned VirtReg); 2852754fe60SDimitry Andric 2862754fe60SDimitry Andric /// handleDebugValue - Add DBG_VALUE instruction to our maps. 2872754fe60SDimitry Andric /// @param MI DBG_VALUE instruction 2882754fe60SDimitry Andric /// @param Idx Last valid SLotIndex before instruction. 2892754fe60SDimitry Andric /// @return True if the DBG_VALUE instruction should be deleted. 2903ca95b02SDimitry Andric bool handleDebugValue(MachineInstr &MI, SlotIndex Idx); 2912754fe60SDimitry Andric 2922754fe60SDimitry Andric /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding 2932754fe60SDimitry Andric /// a UserValue def for each instruction. 2942754fe60SDimitry Andric /// @param mf MachineFunction to be scanned. 2952754fe60SDimitry Andric /// @return True if any debug values were found. 2962754fe60SDimitry Andric bool collectDebugValues(MachineFunction &mf); 2972754fe60SDimitry Andric 2982754fe60SDimitry Andric /// computeIntervals - Compute the live intervals of all user values after 2992754fe60SDimitry Andric /// collecting all their def points. 3002754fe60SDimitry Andric void computeIntervals(); 3012754fe60SDimitry Andric 3022754fe60SDimitry Andric public: 30339d628a0SDimitry Andric LDVImpl(LiveDebugVariables *ps) 30439d628a0SDimitry Andric : pass(*ps), MF(nullptr), EmitDone(false), ModifiedMF(false) {} 3052754fe60SDimitry Andric bool runOnMachineFunction(MachineFunction &mf); 3062754fe60SDimitry Andric 307139f7f9bSDimitry Andric /// clear - Release all memory. 3082754fe60SDimitry Andric void clear() { 30939d628a0SDimitry Andric MF = nullptr; 3102754fe60SDimitry Andric userValues.clear(); 3112754fe60SDimitry Andric virtRegToEqClass.clear(); 3122754fe60SDimitry Andric userVarMap.clear(); 313139f7f9bSDimitry Andric // Make sure we call emitDebugValues if the machine function was modified. 314139f7f9bSDimitry Andric assert((!ModifiedMF || EmitDone) && 315139f7f9bSDimitry Andric "Dbg values are not emitted in LDV"); 316139f7f9bSDimitry Andric EmitDone = false; 317139f7f9bSDimitry Andric ModifiedMF = false; 3182754fe60SDimitry Andric } 3192754fe60SDimitry Andric 3203b0f4066SDimitry Andric /// mapVirtReg - Map virtual register to an equivalence class. 3213b0f4066SDimitry Andric void mapVirtReg(unsigned VirtReg, UserValue *EC); 3223b0f4066SDimitry Andric 323bd5abe19SDimitry Andric /// splitRegister - Replace all references to OldReg with NewRegs. 324f785676fSDimitry Andric void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs); 325bd5abe19SDimitry Andric 326139f7f9bSDimitry Andric /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. 3272754fe60SDimitry Andric void emitDebugValues(VirtRegMap *VRM); 3282754fe60SDimitry Andric 3292754fe60SDimitry Andric void print(raw_ostream&); 3302754fe60SDimitry Andric }; 3312754fe60SDimitry Andric } // namespace 3322754fe60SDimitry Andric 3333ca95b02SDimitry Andric static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS, 334ff0cc061SDimitry Andric const LLVMContext &Ctx) { 335ff0cc061SDimitry Andric if (!DL) 336ff0cc061SDimitry Andric return; 337ff0cc061SDimitry Andric 338ff0cc061SDimitry Andric auto *Scope = cast<DIScope>(DL.getScope()); 339ff0cc061SDimitry Andric // Omit the directory, because it's likely to be long and uninteresting. 340ff0cc061SDimitry Andric CommentOS << Scope->getFilename(); 341ff0cc061SDimitry Andric CommentOS << ':' << DL.getLine(); 342ff0cc061SDimitry Andric if (DL.getCol() != 0) 343ff0cc061SDimitry Andric CommentOS << ':' << DL.getCol(); 344ff0cc061SDimitry Andric 345ff0cc061SDimitry Andric DebugLoc InlinedAtDL = DL.getInlinedAt(); 346ff0cc061SDimitry Andric if (!InlinedAtDL) 347ff0cc061SDimitry Andric return; 348ff0cc061SDimitry Andric 349ff0cc061SDimitry Andric CommentOS << " @[ "; 350ff0cc061SDimitry Andric printDebugLoc(InlinedAtDL, CommentOS, Ctx); 351ff0cc061SDimitry Andric CommentOS << " ]"; 352ff0cc061SDimitry Andric } 353ff0cc061SDimitry Andric 354ff0cc061SDimitry Andric static void printExtendedName(raw_ostream &OS, const DILocalVariable *V, 355ff0cc061SDimitry Andric const DILocation *DL) { 356ff0cc061SDimitry Andric const LLVMContext &Ctx = V->getContext(); 357ff0cc061SDimitry Andric StringRef Res = V->getName(); 358ff0cc061SDimitry Andric if (!Res.empty()) 359ff0cc061SDimitry Andric OS << Res << "," << V->getLine(); 360ff0cc061SDimitry Andric if (auto *InlinedAt = DL->getInlinedAt()) { 361ff0cc061SDimitry Andric if (DebugLoc InlinedAtDL = InlinedAt) { 362ff0cc061SDimitry Andric OS << " @["; 363ff0cc061SDimitry Andric printDebugLoc(InlinedAtDL, OS, Ctx); 364ff0cc061SDimitry Andric OS << "]"; 365ff0cc061SDimitry Andric } 366ff0cc061SDimitry Andric } 367ff0cc061SDimitry Andric } 368ff0cc061SDimitry Andric 369ff0cc061SDimitry Andric void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) { 370ff0cc061SDimitry Andric auto *DV = cast<DILocalVariable>(Variable); 3716122f3e6SDimitry Andric OS << "!\""; 372ff0cc061SDimitry Andric printExtendedName(OS, DV, dl); 373ff0cc061SDimitry Andric 3746122f3e6SDimitry Andric OS << "\"\t"; 3752754fe60SDimitry Andric if (offset) 3762754fe60SDimitry Andric OS << '+' << offset; 3772754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { 3782754fe60SDimitry Andric OS << " [" << I.start() << ';' << I.stop() << "):"; 3792754fe60SDimitry Andric if (I.value() == ~0u) 3802754fe60SDimitry Andric OS << "undef"; 3812754fe60SDimitry Andric else 3822754fe60SDimitry Andric OS << I.value(); 3832754fe60SDimitry Andric } 384bd5abe19SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) { 385bd5abe19SDimitry Andric OS << " Loc" << i << '='; 386ff0cc061SDimitry Andric locations[i].print(OS, TRI); 387bd5abe19SDimitry Andric } 3882754fe60SDimitry Andric OS << '\n'; 3892754fe60SDimitry Andric } 3902754fe60SDimitry Andric 3912754fe60SDimitry Andric void LDVImpl::print(raw_ostream &OS) { 3922754fe60SDimitry Andric OS << "********** DEBUG VARIABLES **********\n"; 3932754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) 394ff0cc061SDimitry Andric userValues[i]->print(OS, TRI); 3952754fe60SDimitry Andric } 3962754fe60SDimitry Andric 3972754fe60SDimitry Andric void UserValue::coalesceLocation(unsigned LocNo) { 3982754fe60SDimitry Andric unsigned KeepLoc = 0; 3992754fe60SDimitry Andric for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) { 4002754fe60SDimitry Andric if (KeepLoc == LocNo) 4012754fe60SDimitry Andric continue; 4022754fe60SDimitry Andric if (locations[KeepLoc].isIdenticalTo(locations[LocNo])) 4032754fe60SDimitry Andric break; 4042754fe60SDimitry Andric } 4052754fe60SDimitry Andric // No matches. 4062754fe60SDimitry Andric if (KeepLoc == locations.size()) 4072754fe60SDimitry Andric return; 4082754fe60SDimitry Andric 4092754fe60SDimitry Andric // Keep the smaller location, erase the larger one. 4102754fe60SDimitry Andric unsigned EraseLoc = LocNo; 4112754fe60SDimitry Andric if (KeepLoc > EraseLoc) 4122754fe60SDimitry Andric std::swap(KeepLoc, EraseLoc); 4132754fe60SDimitry Andric locations.erase(locations.begin() + EraseLoc); 4142754fe60SDimitry Andric 4152754fe60SDimitry Andric // Rewrite values. 4162754fe60SDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { 4172754fe60SDimitry Andric unsigned v = I.value(); 4182754fe60SDimitry Andric if (v == EraseLoc) 4192754fe60SDimitry Andric I.setValue(KeepLoc); // Coalesce when possible. 4202754fe60SDimitry Andric else if (v > EraseLoc) 4212754fe60SDimitry Andric I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values. 4222754fe60SDimitry Andric } 4232754fe60SDimitry Andric } 4242754fe60SDimitry Andric 4253b0f4066SDimitry Andric void UserValue::mapVirtRegs(LDVImpl *LDV) { 4263b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 4273b0f4066SDimitry Andric if (locations[i].isReg() && 4283b0f4066SDimitry Andric TargetRegisterInfo::isVirtualRegister(locations[i].getReg())) 4293b0f4066SDimitry Andric LDV->mapVirtReg(locations[i].getReg(), this); 4303b0f4066SDimitry Andric } 4313b0f4066SDimitry Andric 43239d628a0SDimitry Andric UserValue *LDVImpl::getUserValue(const MDNode *Var, const MDNode *Expr, 43339d628a0SDimitry Andric unsigned Offset, bool IsIndirect, 4343ca95b02SDimitry Andric const DebugLoc &DL) { 4352754fe60SDimitry Andric UserValue *&Leader = userVarMap[Var]; 4362754fe60SDimitry Andric if (Leader) { 4372754fe60SDimitry Andric UserValue *UV = Leader->getLeader(); 4382754fe60SDimitry Andric Leader = UV; 4392754fe60SDimitry Andric for (; UV; UV = UV->getNext()) 440ff0cc061SDimitry Andric if (UV->match(Var, Expr, DL->getInlinedAt(), Offset, IsIndirect)) 4412754fe60SDimitry Andric return UV; 4422754fe60SDimitry Andric } 4432754fe60SDimitry Andric 44491bc56edSDimitry Andric userValues.push_back( 44539d628a0SDimitry Andric make_unique<UserValue>(Var, Expr, Offset, IsIndirect, DL, allocator)); 44691bc56edSDimitry Andric UserValue *UV = userValues.back().get(); 4472754fe60SDimitry Andric Leader = UserValue::merge(Leader, UV); 4482754fe60SDimitry Andric return UV; 4492754fe60SDimitry Andric } 4502754fe60SDimitry Andric 4512754fe60SDimitry Andric void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) { 4522754fe60SDimitry Andric assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs"); 4532754fe60SDimitry Andric UserValue *&Leader = virtRegToEqClass[VirtReg]; 4542754fe60SDimitry Andric Leader = UserValue::merge(Leader, EC); 4552754fe60SDimitry Andric } 4562754fe60SDimitry Andric 4572754fe60SDimitry Andric UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) { 4582754fe60SDimitry Andric if (UserValue *UV = virtRegToEqClass.lookup(VirtReg)) 4592754fe60SDimitry Andric return UV->getLeader(); 46091bc56edSDimitry Andric return nullptr; 4612754fe60SDimitry Andric } 4622754fe60SDimitry Andric 4633ca95b02SDimitry Andric bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) { 4642754fe60SDimitry Andric // DBG_VALUE loc, offset, variable 4653ca95b02SDimitry Andric if (MI.getNumOperands() != 4 || 4663ca95b02SDimitry Andric !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) || 4673ca95b02SDimitry Andric !MI.getOperand(2).isMetadata()) { 4683ca95b02SDimitry Andric DEBUG(dbgs() << "Can't handle " << MI); 4692754fe60SDimitry Andric return false; 4702754fe60SDimitry Andric } 4712754fe60SDimitry Andric 4722754fe60SDimitry Andric // Get or create the UserValue for (variable,offset). 4733ca95b02SDimitry Andric bool IsIndirect = MI.isIndirectDebugValue(); 4743ca95b02SDimitry Andric unsigned Offset = IsIndirect ? MI.getOperand(1).getImm() : 0; 4753ca95b02SDimitry Andric const MDNode *Var = MI.getDebugVariable(); 4763ca95b02SDimitry Andric const MDNode *Expr = MI.getDebugExpression(); 477f785676fSDimitry Andric //here. 4783ca95b02SDimitry Andric UserValue *UV = getUserValue(Var, Expr, Offset, IsIndirect, MI.getDebugLoc()); 4793ca95b02SDimitry Andric UV->addDef(Idx, MI.getOperand(0)); 4802754fe60SDimitry Andric return true; 4812754fe60SDimitry Andric } 4822754fe60SDimitry Andric 4832754fe60SDimitry Andric bool LDVImpl::collectDebugValues(MachineFunction &mf) { 4842754fe60SDimitry Andric bool Changed = false; 4852754fe60SDimitry Andric for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE; 4862754fe60SDimitry Andric ++MFI) { 4877d523365SDimitry Andric MachineBasicBlock *MBB = &*MFI; 4882754fe60SDimitry Andric for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); 4892754fe60SDimitry Andric MBBI != MBBE;) { 4902754fe60SDimitry Andric if (!MBBI->isDebugValue()) { 4912754fe60SDimitry Andric ++MBBI; 4922754fe60SDimitry Andric continue; 4932754fe60SDimitry Andric } 4942754fe60SDimitry Andric // DBG_VALUE has no slot index, use the previous instruction instead. 4953ca95b02SDimitry Andric SlotIndex Idx = 4963ca95b02SDimitry Andric MBBI == MBB->begin() 4973ca95b02SDimitry Andric ? LIS->getMBBStartIdx(MBB) 4983ca95b02SDimitry Andric : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot(); 4992754fe60SDimitry Andric // Handle consecutive DBG_VALUE instructions with the same slot index. 5002754fe60SDimitry Andric do { 5013ca95b02SDimitry Andric if (handleDebugValue(*MBBI, Idx)) { 5022754fe60SDimitry Andric MBBI = MBB->erase(MBBI); 5032754fe60SDimitry Andric Changed = true; 5042754fe60SDimitry Andric } else 5052754fe60SDimitry Andric ++MBBI; 5062754fe60SDimitry Andric } while (MBBI != MBBE && MBBI->isDebugValue()); 5072754fe60SDimitry Andric } 5082754fe60SDimitry Andric } 5092754fe60SDimitry Andric return Changed; 5102754fe60SDimitry Andric } 5112754fe60SDimitry Andric 5127d523365SDimitry Andric /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a 5137d523365SDimitry Andric /// data-flow analysis to propagate them beyond basic block boundaries. 5147d523365SDimitry Andric void UserValue::extendDef(SlotIndex Idx, unsigned LocNo, LiveRange *LR, 5157d523365SDimitry Andric const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills, 516d88c1a5aSDimitry Andric LiveIntervals &LIS) { 5177d523365SDimitry Andric SlotIndex Start = Idx; 5182754fe60SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start); 5192754fe60SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(MBB); 5202754fe60SDimitry Andric LocMap::iterator I = locInts.find(Start); 5212754fe60SDimitry Andric 5222754fe60SDimitry Andric // Limit to VNI's live range. 5232754fe60SDimitry Andric bool ToEnd = true; 524f785676fSDimitry Andric if (LR && VNI) { 525f785676fSDimitry Andric LiveInterval::Segment *Segment = LR->getSegmentContaining(Start); 526f785676fSDimitry Andric if (!Segment || Segment->valno != VNI) { 5273b0f4066SDimitry Andric if (Kills) 5283b0f4066SDimitry Andric Kills->push_back(Start); 5297d523365SDimitry Andric return; 5303b0f4066SDimitry Andric } 5313ca95b02SDimitry Andric if (Segment->end < Stop) { 5323ca95b02SDimitry Andric Stop = Segment->end; 5333ca95b02SDimitry Andric ToEnd = false; 5343ca95b02SDimitry Andric } 5352754fe60SDimitry Andric } 5362754fe60SDimitry Andric 5372754fe60SDimitry Andric // There could already be a short def at Start. 5382754fe60SDimitry Andric if (I.valid() && I.start() <= Start) { 5392754fe60SDimitry Andric // Stop when meeting a different location or an already extended interval. 5402754fe60SDimitry Andric Start = Start.getNextSlot(); 5412754fe60SDimitry Andric if (I.value() != LocNo || I.stop() != Start) 5427d523365SDimitry Andric return; 5432754fe60SDimitry Andric // This is a one-slot placeholder. Just skip it. 5442754fe60SDimitry Andric ++I; 5452754fe60SDimitry Andric } 5462754fe60SDimitry Andric 5472754fe60SDimitry Andric // Limited by the next def. 5483ca95b02SDimitry Andric if (I.valid() && I.start() < Stop) { 5493ca95b02SDimitry Andric Stop = I.start(); 5503ca95b02SDimitry Andric ToEnd = false; 5513ca95b02SDimitry Andric } 5523b0f4066SDimitry Andric // Limited by VNI's live range. 5533b0f4066SDimitry Andric else if (!ToEnd && Kills) 5543b0f4066SDimitry Andric Kills->push_back(Stop); 5552754fe60SDimitry Andric 5567d523365SDimitry Andric if (Start < Stop) 5572754fe60SDimitry Andric I.insert(Start, Stop, LocNo); 5582754fe60SDimitry Andric } 5592754fe60SDimitry Andric 5602754fe60SDimitry Andric void 5613b0f4066SDimitry Andric UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo, 5623b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 5633b0f4066SDimitry Andric SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs, 5643b0f4066SDimitry Andric MachineRegisterInfo &MRI, LiveIntervals &LIS) { 5653b0f4066SDimitry Andric if (Kills.empty()) 5663b0f4066SDimitry Andric return; 5673b0f4066SDimitry Andric // Don't track copies from physregs, there are too many uses. 5683b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(LI->reg)) 5693b0f4066SDimitry Andric return; 5703b0f4066SDimitry Andric 5713b0f4066SDimitry Andric // Collect all the (vreg, valno) pairs that are copies of LI. 5723b0f4066SDimitry Andric SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues; 57391bc56edSDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) { 57491bc56edSDimitry Andric MachineInstr *MI = MO.getParent(); 5753b0f4066SDimitry Andric // Copies of the full value. 57691bc56edSDimitry Andric if (MO.getSubReg() || !MI->isCopy()) 5773b0f4066SDimitry Andric continue; 5783b0f4066SDimitry Andric unsigned DstReg = MI->getOperand(0).getReg(); 5793b0f4066SDimitry Andric 5803b0f4066SDimitry Andric // Don't follow copies to physregs. These are usually setting up call 5813b0f4066SDimitry Andric // arguments, and the argument registers are always call clobbered. We are 5823b0f4066SDimitry Andric // better off in the source register which could be a callee-saved register, 5833b0f4066SDimitry Andric // or it could be spilled. 5843b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(DstReg)) 5853b0f4066SDimitry Andric continue; 5863b0f4066SDimitry Andric 5873b0f4066SDimitry Andric // Is LocNo extended to reach this copy? If not, another def may be blocking 5883b0f4066SDimitry Andric // it, or we are looking at a wrong value of LI. 5893ca95b02SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI); 590dff0c46cSDimitry Andric LocMap::iterator I = locInts.find(Idx.getRegSlot(true)); 5913b0f4066SDimitry Andric if (!I.valid() || I.value() != LocNo) 5923b0f4066SDimitry Andric continue; 5933b0f4066SDimitry Andric 5943b0f4066SDimitry Andric if (!LIS.hasInterval(DstReg)) 5953b0f4066SDimitry Andric continue; 5963b0f4066SDimitry Andric LiveInterval *DstLI = &LIS.getInterval(DstReg); 597dff0c46cSDimitry Andric const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot()); 598dff0c46cSDimitry Andric assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value"); 5993b0f4066SDimitry Andric CopyValues.push_back(std::make_pair(DstLI, DstVNI)); 6003b0f4066SDimitry Andric } 6013b0f4066SDimitry Andric 6023b0f4066SDimitry Andric if (CopyValues.empty()) 6033b0f4066SDimitry Andric return; 6043b0f4066SDimitry Andric 6053b0f4066SDimitry Andric DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n'); 6063b0f4066SDimitry Andric 6073b0f4066SDimitry Andric // Try to add defs of the copied values for each kill point. 6083b0f4066SDimitry Andric for (unsigned i = 0, e = Kills.size(); i != e; ++i) { 6093b0f4066SDimitry Andric SlotIndex Idx = Kills[i]; 6103b0f4066SDimitry Andric for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) { 6113b0f4066SDimitry Andric LiveInterval *DstLI = CopyValues[j].first; 6123b0f4066SDimitry Andric const VNInfo *DstVNI = CopyValues[j].second; 6133b0f4066SDimitry Andric if (DstLI->getVNInfoAt(Idx) != DstVNI) 6143b0f4066SDimitry Andric continue; 6153b0f4066SDimitry Andric // Check that there isn't already a def at Idx 6163b0f4066SDimitry Andric LocMap::iterator I = locInts.find(Idx); 6173b0f4066SDimitry Andric if (I.valid() && I.start() <= Idx) 6183b0f4066SDimitry Andric continue; 6193b0f4066SDimitry Andric DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" 6203b0f4066SDimitry Andric << DstVNI->id << " in " << *DstLI << '\n'); 6213b0f4066SDimitry Andric MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); 6223b0f4066SDimitry Andric assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); 6233b0f4066SDimitry Andric unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); 6243b0f4066SDimitry Andric I.insert(Idx, Idx.getNextSlot(), LocNo); 6253b0f4066SDimitry Andric NewDefs.push_back(std::make_pair(Idx, LocNo)); 6263b0f4066SDimitry Andric break; 6273b0f4066SDimitry Andric } 6283b0f4066SDimitry Andric } 6293b0f4066SDimitry Andric } 6303b0f4066SDimitry Andric 6313b0f4066SDimitry Andric void 6323b0f4066SDimitry Andric UserValue::computeIntervals(MachineRegisterInfo &MRI, 6337ae0e2c9SDimitry Andric const TargetRegisterInfo &TRI, 634d88c1a5aSDimitry Andric LiveIntervals &LIS) { 6352754fe60SDimitry Andric SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs; 6362754fe60SDimitry Andric 6372754fe60SDimitry Andric // Collect all defs to be extended (Skipping undefs). 6382754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) 6392754fe60SDimitry Andric if (I.value() != ~0u) 6402754fe60SDimitry Andric Defs.push_back(std::make_pair(I.start(), I.value())); 6412754fe60SDimitry Andric 6423b0f4066SDimitry Andric // Extend all defs, and possibly add new ones along the way. 6433b0f4066SDimitry Andric for (unsigned i = 0; i != Defs.size(); ++i) { 6442754fe60SDimitry Andric SlotIndex Idx = Defs[i].first; 6452754fe60SDimitry Andric unsigned LocNo = Defs[i].second; 6462754fe60SDimitry Andric const MachineOperand &Loc = locations[LocNo]; 6472754fe60SDimitry Andric 6487ae0e2c9SDimitry Andric if (!Loc.isReg()) { 649d88c1a5aSDimitry Andric extendDef(Idx, LocNo, nullptr, nullptr, nullptr, LIS); 6507ae0e2c9SDimitry Andric continue; 6517ae0e2c9SDimitry Andric } 6527ae0e2c9SDimitry Andric 6532754fe60SDimitry Andric // Register locations are constrained to where the register value is live. 6547ae0e2c9SDimitry Andric if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) { 65591bc56edSDimitry Andric LiveInterval *LI = nullptr; 65691bc56edSDimitry Andric const VNInfo *VNI = nullptr; 6577ae0e2c9SDimitry Andric if (LIS.hasInterval(Loc.getReg())) { 6587ae0e2c9SDimitry Andric LI = &LIS.getInterval(Loc.getReg()); 6597ae0e2c9SDimitry Andric VNI = LI->getVNInfoAt(Idx); 6607ae0e2c9SDimitry Andric } 6613b0f4066SDimitry Andric SmallVector<SlotIndex, 16> Kills; 662d88c1a5aSDimitry Andric extendDef(Idx, LocNo, LI, VNI, &Kills, LIS); 6637ae0e2c9SDimitry Andric if (LI) 6643b0f4066SDimitry Andric addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS); 6657ae0e2c9SDimitry Andric continue; 6667ae0e2c9SDimitry Andric } 6677ae0e2c9SDimitry Andric 6687ae0e2c9SDimitry Andric // For physregs, use the live range of the first regunit as a guide. 6697ae0e2c9SDimitry Andric unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI); 670f785676fSDimitry Andric LiveRange *LR = &LIS.getRegUnit(Unit); 671f785676fSDimitry Andric const VNInfo *VNI = LR->getVNInfoAt(Idx); 6727ae0e2c9SDimitry Andric // Don't track copies from physregs, it is too expensive. 673d88c1a5aSDimitry Andric extendDef(Idx, LocNo, LR, VNI, nullptr, LIS); 6742754fe60SDimitry Andric } 6752754fe60SDimitry Andric 6762754fe60SDimitry Andric // Finally, erase all the undefs. 6772754fe60SDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid();) 6782754fe60SDimitry Andric if (I.value() == ~0u) 6792754fe60SDimitry Andric I.erase(); 6802754fe60SDimitry Andric else 6812754fe60SDimitry Andric ++I; 6822754fe60SDimitry Andric } 6832754fe60SDimitry Andric 6842754fe60SDimitry Andric void LDVImpl::computeIntervals() { 6853b0f4066SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 686d88c1a5aSDimitry Andric userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS); 6873b0f4066SDimitry Andric userValues[i]->mapVirtRegs(this); 6883b0f4066SDimitry Andric } 6892754fe60SDimitry Andric } 6902754fe60SDimitry Andric 6912754fe60SDimitry Andric bool LDVImpl::runOnMachineFunction(MachineFunction &mf) { 69239d628a0SDimitry Andric clear(); 6932754fe60SDimitry Andric MF = &mf; 6942754fe60SDimitry Andric LIS = &pass.getAnalysis<LiveIntervals>(); 69539d628a0SDimitry Andric TRI = mf.getSubtarget().getRegisterInfo(); 6962754fe60SDimitry Andric DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " 6973861d79fSDimitry Andric << mf.getName() << " **********\n"); 6982754fe60SDimitry Andric 6992754fe60SDimitry Andric bool Changed = collectDebugValues(mf); 7002754fe60SDimitry Andric computeIntervals(); 7012754fe60SDimitry Andric DEBUG(print(dbgs())); 702139f7f9bSDimitry Andric ModifiedMF = Changed; 7032754fe60SDimitry Andric return Changed; 7042754fe60SDimitry Andric } 7052754fe60SDimitry Andric 70639d628a0SDimitry Andric static void removeDebugValues(MachineFunction &mf) { 70739d628a0SDimitry Andric for (MachineBasicBlock &MBB : mf) { 70839d628a0SDimitry Andric for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) { 70939d628a0SDimitry Andric if (!MBBI->isDebugValue()) { 71039d628a0SDimitry Andric ++MBBI; 71139d628a0SDimitry Andric continue; 71239d628a0SDimitry Andric } 71339d628a0SDimitry Andric MBBI = MBB.erase(MBBI); 71439d628a0SDimitry Andric } 71539d628a0SDimitry Andric } 71639d628a0SDimitry Andric } 71739d628a0SDimitry Andric 7182754fe60SDimitry Andric bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) { 7192754fe60SDimitry Andric if (!EnableLDV) 7202754fe60SDimitry Andric return false; 7217d523365SDimitry Andric if (!mf.getFunction()->getSubprogram()) { 72239d628a0SDimitry Andric removeDebugValues(mf); 72339d628a0SDimitry Andric return false; 72439d628a0SDimitry Andric } 7252754fe60SDimitry Andric if (!pImpl) 7262754fe60SDimitry Andric pImpl = new LDVImpl(this); 7272754fe60SDimitry Andric return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf); 7282754fe60SDimitry Andric } 7292754fe60SDimitry Andric 7302754fe60SDimitry Andric void LiveDebugVariables::releaseMemory() { 7312754fe60SDimitry Andric if (pImpl) 7322754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->clear(); 7332754fe60SDimitry Andric } 7342754fe60SDimitry Andric 7352754fe60SDimitry Andric LiveDebugVariables::~LiveDebugVariables() { 7362754fe60SDimitry Andric if (pImpl) 7372754fe60SDimitry Andric delete static_cast<LDVImpl*>(pImpl); 7382754fe60SDimitry Andric } 7392754fe60SDimitry Andric 740bd5abe19SDimitry Andric //===----------------------------------------------------------------------===// 741bd5abe19SDimitry Andric // Live Range Splitting 742bd5abe19SDimitry Andric //===----------------------------------------------------------------------===// 743bd5abe19SDimitry Andric 744bd5abe19SDimitry Andric bool 745f785676fSDimitry Andric UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, 746f785676fSDimitry Andric LiveIntervals& LIS) { 747bd5abe19SDimitry Andric DEBUG({ 748bd5abe19SDimitry Andric dbgs() << "Splitting Loc" << OldLocNo << '\t'; 74991bc56edSDimitry Andric print(dbgs(), nullptr); 750bd5abe19SDimitry Andric }); 751bd5abe19SDimitry Andric bool DidChange = false; 752bd5abe19SDimitry Andric LocMap::iterator LocMapI; 753bd5abe19SDimitry Andric LocMapI.setMap(locInts); 754bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) { 755f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(NewRegs[i]); 756bd5abe19SDimitry Andric if (LI->empty()) 757bd5abe19SDimitry Andric continue; 758bd5abe19SDimitry Andric 759bd5abe19SDimitry Andric // Don't allocate the new LocNo until it is needed. 760bd5abe19SDimitry Andric unsigned NewLocNo = ~0u; 761bd5abe19SDimitry Andric 762bd5abe19SDimitry Andric // Iterate over the overlaps between locInts and LI. 763bd5abe19SDimitry Andric LocMapI.find(LI->beginIndex()); 764bd5abe19SDimitry Andric if (!LocMapI.valid()) 765bd5abe19SDimitry Andric continue; 766bd5abe19SDimitry Andric LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start()); 767bd5abe19SDimitry Andric LiveInterval::iterator LIE = LI->end(); 768bd5abe19SDimitry Andric while (LocMapI.valid() && LII != LIE) { 769bd5abe19SDimitry Andric // At this point, we know that LocMapI.stop() > LII->start. 770bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 771bd5abe19SDimitry Andric if (LII == LIE) 772bd5abe19SDimitry Andric break; 773bd5abe19SDimitry Andric 774bd5abe19SDimitry Andric // Now LII->end > LocMapI.start(). Do we have an overlap? 775bd5abe19SDimitry Andric if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) { 776bd5abe19SDimitry Andric // Overlapping correct location. Allocate NewLocNo now. 777bd5abe19SDimitry Andric if (NewLocNo == ~0u) { 778bd5abe19SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(LI->reg, false); 779bd5abe19SDimitry Andric MO.setSubReg(locations[OldLocNo].getSubReg()); 780bd5abe19SDimitry Andric NewLocNo = getLocationNo(MO); 781bd5abe19SDimitry Andric DidChange = true; 782bd5abe19SDimitry Andric } 783bd5abe19SDimitry Andric 784bd5abe19SDimitry Andric SlotIndex LStart = LocMapI.start(); 785bd5abe19SDimitry Andric SlotIndex LStop = LocMapI.stop(); 786bd5abe19SDimitry Andric 787bd5abe19SDimitry Andric // Trim LocMapI down to the LII overlap. 788bd5abe19SDimitry Andric if (LStart < LII->start) 789bd5abe19SDimitry Andric LocMapI.setStartUnchecked(LII->start); 790bd5abe19SDimitry Andric if (LStop > LII->end) 791bd5abe19SDimitry Andric LocMapI.setStopUnchecked(LII->end); 792bd5abe19SDimitry Andric 793bd5abe19SDimitry Andric // Change the value in the overlap. This may trigger coalescing. 794bd5abe19SDimitry Andric LocMapI.setValue(NewLocNo); 795bd5abe19SDimitry Andric 796bd5abe19SDimitry Andric // Re-insert any removed OldLocNo ranges. 797bd5abe19SDimitry Andric if (LStart < LocMapI.start()) { 798bd5abe19SDimitry Andric LocMapI.insert(LStart, LocMapI.start(), OldLocNo); 799bd5abe19SDimitry Andric ++LocMapI; 800bd5abe19SDimitry Andric assert(LocMapI.valid() && "Unexpected coalescing"); 801bd5abe19SDimitry Andric } 802bd5abe19SDimitry Andric if (LStop > LocMapI.stop()) { 803bd5abe19SDimitry Andric ++LocMapI; 804bd5abe19SDimitry Andric LocMapI.insert(LII->end, LStop, OldLocNo); 805bd5abe19SDimitry Andric --LocMapI; 806bd5abe19SDimitry Andric } 807bd5abe19SDimitry Andric } 808bd5abe19SDimitry Andric 809bd5abe19SDimitry Andric // Advance to the next overlap. 810bd5abe19SDimitry Andric if (LII->end < LocMapI.stop()) { 811bd5abe19SDimitry Andric if (++LII == LIE) 812bd5abe19SDimitry Andric break; 813bd5abe19SDimitry Andric LocMapI.advanceTo(LII->start); 814bd5abe19SDimitry Andric } else { 815bd5abe19SDimitry Andric ++LocMapI; 816bd5abe19SDimitry Andric if (!LocMapI.valid()) 817bd5abe19SDimitry Andric break; 818bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 819bd5abe19SDimitry Andric } 820bd5abe19SDimitry Andric } 821bd5abe19SDimitry Andric } 822bd5abe19SDimitry Andric 823bd5abe19SDimitry Andric // Finally, remove any remaining OldLocNo intervals and OldLocNo itself. 824bd5abe19SDimitry Andric locations.erase(locations.begin() + OldLocNo); 825bd5abe19SDimitry Andric LocMapI.goToBegin(); 826bd5abe19SDimitry Andric while (LocMapI.valid()) { 827bd5abe19SDimitry Andric unsigned v = LocMapI.value(); 828bd5abe19SDimitry Andric if (v == OldLocNo) { 829bd5abe19SDimitry Andric DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';' 830bd5abe19SDimitry Andric << LocMapI.stop() << ")\n"); 831bd5abe19SDimitry Andric LocMapI.erase(); 832bd5abe19SDimitry Andric } else { 833bd5abe19SDimitry Andric if (v > OldLocNo) 834bd5abe19SDimitry Andric LocMapI.setValueUnchecked(v-1); 835bd5abe19SDimitry Andric ++LocMapI; 836bd5abe19SDimitry Andric } 837bd5abe19SDimitry Andric } 838bd5abe19SDimitry Andric 83991bc56edSDimitry Andric DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);}); 840bd5abe19SDimitry Andric return DidChange; 841bd5abe19SDimitry Andric } 842bd5abe19SDimitry Andric 843bd5abe19SDimitry Andric bool 844f785676fSDimitry Andric UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, 845f785676fSDimitry Andric LiveIntervals &LIS) { 846bd5abe19SDimitry Andric bool DidChange = false; 847bd5abe19SDimitry Andric // Split locations referring to OldReg. Iterate backwards so splitLocation can 848dff0c46cSDimitry Andric // safely erase unused locations. 849bd5abe19SDimitry Andric for (unsigned i = locations.size(); i ; --i) { 850bd5abe19SDimitry Andric unsigned LocNo = i-1; 851bd5abe19SDimitry Andric const MachineOperand *Loc = &locations[LocNo]; 852bd5abe19SDimitry Andric if (!Loc->isReg() || Loc->getReg() != OldReg) 853bd5abe19SDimitry Andric continue; 854f785676fSDimitry Andric DidChange |= splitLocation(LocNo, NewRegs, LIS); 855bd5abe19SDimitry Andric } 856bd5abe19SDimitry Andric return DidChange; 857bd5abe19SDimitry Andric } 858bd5abe19SDimitry Andric 859f785676fSDimitry Andric void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) { 860bd5abe19SDimitry Andric bool DidChange = false; 861bd5abe19SDimitry Andric for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext()) 862f785676fSDimitry Andric DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS); 863bd5abe19SDimitry Andric 864bd5abe19SDimitry Andric if (!DidChange) 865bd5abe19SDimitry Andric return; 866bd5abe19SDimitry Andric 867bd5abe19SDimitry Andric // Map all of the new virtual registers. 868bd5abe19SDimitry Andric UserValue *UV = lookupVirtReg(OldReg); 869bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) 870f785676fSDimitry Andric mapVirtReg(NewRegs[i], UV); 871bd5abe19SDimitry Andric } 872bd5abe19SDimitry Andric 873bd5abe19SDimitry Andric void LiveDebugVariables:: 874f785676fSDimitry Andric splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) { 875bd5abe19SDimitry Andric if (pImpl) 876bd5abe19SDimitry Andric static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs); 877bd5abe19SDimitry Andric } 878bd5abe19SDimitry Andric 8792754fe60SDimitry Andric void 8802754fe60SDimitry Andric UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) { 8812754fe60SDimitry Andric // Iterate over locations in reverse makes it easier to handle coalescing. 8822754fe60SDimitry Andric for (unsigned i = locations.size(); i ; --i) { 8832754fe60SDimitry Andric unsigned LocNo = i-1; 8842754fe60SDimitry Andric MachineOperand &Loc = locations[LocNo]; 8852754fe60SDimitry Andric // Only virtual registers are rewritten. 8862754fe60SDimitry Andric if (!Loc.isReg() || !Loc.getReg() || 8872754fe60SDimitry Andric !TargetRegisterInfo::isVirtualRegister(Loc.getReg())) 8882754fe60SDimitry Andric continue; 8892754fe60SDimitry Andric unsigned VirtReg = Loc.getReg(); 8902754fe60SDimitry Andric if (VRM.isAssignedReg(VirtReg) && 8912754fe60SDimitry Andric TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) { 892bd5abe19SDimitry Andric // This can create a %noreg operand in rare cases when the sub-register 893bd5abe19SDimitry Andric // index is no longer available. That means the user value is in a 894bd5abe19SDimitry Andric // non-existent sub-register, and %noreg is exactly what we want. 8952754fe60SDimitry Andric Loc.substPhysReg(VRM.getPhys(VirtReg), TRI); 896dff0c46cSDimitry Andric } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) { 8972754fe60SDimitry Andric // FIXME: Translate SubIdx to a stackslot offset. 8982754fe60SDimitry Andric Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg)); 8992754fe60SDimitry Andric } else { 9002754fe60SDimitry Andric Loc.setReg(0); 9012754fe60SDimitry Andric Loc.setSubReg(0); 9022754fe60SDimitry Andric } 9032754fe60SDimitry Andric coalesceLocation(LocNo); 9042754fe60SDimitry Andric } 9052754fe60SDimitry Andric } 9062754fe60SDimitry Andric 9072754fe60SDimitry Andric /// findInsertLocation - Find an iterator for inserting a DBG_VALUE 9082754fe60SDimitry Andric /// instruction. 9092754fe60SDimitry Andric static MachineBasicBlock::iterator 9102754fe60SDimitry Andric findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, 9112754fe60SDimitry Andric LiveIntervals &LIS) { 9122754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 9132754fe60SDimitry Andric Idx = Idx.getBaseIndex(); 9142754fe60SDimitry Andric 9152754fe60SDimitry Andric // Try to find an insert location by going backwards from Idx. 9162754fe60SDimitry Andric MachineInstr *MI; 9172754fe60SDimitry Andric while (!(MI = LIS.getInstructionFromIndex(Idx))) { 9182754fe60SDimitry Andric // We've reached the beginning of MBB. 9192754fe60SDimitry Andric if (Idx == Start) { 920d88c1a5aSDimitry Andric MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin()); 9212754fe60SDimitry Andric return I; 9222754fe60SDimitry Andric } 9232754fe60SDimitry Andric Idx = Idx.getPrevIndex(); 9242754fe60SDimitry Andric } 9252754fe60SDimitry Andric 9262754fe60SDimitry Andric // Don't insert anything after the first terminator, though. 927dff0c46cSDimitry Andric return MI->isTerminator() ? MBB->getFirstTerminator() : 92891bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI)); 9292754fe60SDimitry Andric } 9302754fe60SDimitry Andric 9312754fe60SDimitry Andric void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, 9322754fe60SDimitry Andric unsigned LocNo, 9332754fe60SDimitry Andric LiveIntervals &LIS, 9342754fe60SDimitry Andric const TargetInstrInfo &TII) { 9352754fe60SDimitry Andric MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS); 9362754fe60SDimitry Andric MachineOperand &Loc = locations[LocNo]; 9376122f3e6SDimitry Andric ++NumInsertedDebugValues; 9382754fe60SDimitry Andric 939ff0cc061SDimitry Andric assert(cast<DILocalVariable>(Variable) 940ff0cc061SDimitry Andric ->isValidLocationForIntrinsic(getDebugLoc()) && 941ff0cc061SDimitry Andric "Expected inlined-at fields to agree"); 942f785676fSDimitry Andric if (Loc.isReg()) 943ff0cc061SDimitry Andric BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE), 94439d628a0SDimitry Andric IsIndirect, Loc.getReg(), offset, Variable, Expression); 945f785676fSDimitry Andric else 946ff0cc061SDimitry Andric BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE)) 9477a7e6055SDimitry Andric .add(Loc) 94839d628a0SDimitry Andric .addImm(offset) 94939d628a0SDimitry Andric .addMetadata(Variable) 95039d628a0SDimitry Andric .addMetadata(Expression); 9512754fe60SDimitry Andric } 9522754fe60SDimitry Andric 9532754fe60SDimitry Andric void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, 9542754fe60SDimitry Andric const TargetInstrInfo &TII) { 9552754fe60SDimitry Andric MachineFunction::iterator MFEnd = VRM->getMachineFunction().end(); 9562754fe60SDimitry Andric 9572754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid();) { 9582754fe60SDimitry Andric SlotIndex Start = I.start(); 9592754fe60SDimitry Andric SlotIndex Stop = I.stop(); 9602754fe60SDimitry Andric unsigned LocNo = I.value(); 9612754fe60SDimitry Andric DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo); 9627d523365SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator(); 9637d523365SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB); 9642754fe60SDimitry Andric 9652754fe60SDimitry Andric DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 9667d523365SDimitry Andric insertDebugValue(&*MBB, Start, LocNo, LIS, TII); 9672754fe60SDimitry Andric // This interval may span multiple basic blocks. 9682754fe60SDimitry Andric // Insert a DBG_VALUE into each one. 9692754fe60SDimitry Andric while(Stop > MBBEnd) { 9702754fe60SDimitry Andric // Move to the next block. 9712754fe60SDimitry Andric Start = MBBEnd; 9722754fe60SDimitry Andric if (++MBB == MFEnd) 9732754fe60SDimitry Andric break; 9747d523365SDimitry Andric MBBEnd = LIS.getMBBEndIdx(&*MBB); 9752754fe60SDimitry Andric DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); 9767d523365SDimitry Andric insertDebugValue(&*MBB, Start, LocNo, LIS, TII); 9772754fe60SDimitry Andric } 9782754fe60SDimitry Andric DEBUG(dbgs() << '\n'); 9792754fe60SDimitry Andric if (MBB == MFEnd) 9802754fe60SDimitry Andric break; 9812754fe60SDimitry Andric 9822754fe60SDimitry Andric ++I; 9832754fe60SDimitry Andric } 9842754fe60SDimitry Andric } 9852754fe60SDimitry Andric 9862754fe60SDimitry Andric void LDVImpl::emitDebugValues(VirtRegMap *VRM) { 9872754fe60SDimitry Andric DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); 98839d628a0SDimitry Andric if (!MF) 98939d628a0SDimitry Andric return; 99039d628a0SDimitry Andric const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 9912754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 992ff0cc061SDimitry Andric DEBUG(userValues[i]->print(dbgs(), TRI)); 9932754fe60SDimitry Andric userValues[i]->rewriteLocations(*VRM, *TRI); 9942754fe60SDimitry Andric userValues[i]->emitDebugValues(VRM, *LIS, *TII); 9952754fe60SDimitry Andric } 996139f7f9bSDimitry Andric EmitDone = true; 9972754fe60SDimitry Andric } 9982754fe60SDimitry Andric 9992754fe60SDimitry Andric void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) { 10002754fe60SDimitry Andric if (pImpl) 10012754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM); 10022754fe60SDimitry Andric } 10032754fe60SDimitry Andric 100439d628a0SDimitry Andric bool LiveDebugVariables::doInitialization(Module &M) { 100539d628a0SDimitry Andric return Pass::doInitialization(M); 100639d628a0SDimitry Andric } 10072754fe60SDimitry Andric 10087a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 10093ca95b02SDimitry Andric LLVM_DUMP_METHOD void LiveDebugVariables::dump() { 10102754fe60SDimitry Andric if (pImpl) 10112754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->print(dbgs()); 10122754fe60SDimitry Andric } 10132754fe60SDimitry Andric #endif 1014