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"
39ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h"
402754fe60SDimitry Andric #include "llvm/Target/TargetInstrInfo.h"
412754fe60SDimitry Andric #include "llvm/Target/TargetMachine.h"
422754fe60SDimitry Andric #include "llvm/Target/TargetRegisterInfo.h"
4339d628a0SDimitry Andric #include "llvm/Target/TargetSubtargetInfo.h"
4491bc56edSDimitry Andric #include <memory>
453ca95b02SDimitry Andric #include <utility>
4691bc56edSDimitry Andric 
472754fe60SDimitry Andric using namespace llvm;
482754fe60SDimitry Andric 
4991bc56edSDimitry Andric #define DEBUG_TYPE "livedebug"
5091bc56edSDimitry Andric 
512754fe60SDimitry Andric static cl::opt<bool>
522754fe60SDimitry Andric EnableLDV("live-debug-variables", cl::init(true),
532754fe60SDimitry Andric           cl::desc("Enable the live debug variables pass"), cl::Hidden);
542754fe60SDimitry Andric 
556122f3e6SDimitry Andric STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
562754fe60SDimitry Andric char LiveDebugVariables::ID = 0;
572754fe60SDimitry Andric 
582754fe60SDimitry Andric INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars",
592754fe60SDimitry Andric                 "Debug Variable Analysis", false, false)
602754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
612754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
622754fe60SDimitry Andric INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars",
632754fe60SDimitry Andric                 "Debug Variable Analysis", false, false)
642754fe60SDimitry Andric 
652754fe60SDimitry Andric void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
662754fe60SDimitry Andric   AU.addRequired<MachineDominatorTree>();
672754fe60SDimitry Andric   AU.addRequiredTransitive<LiveIntervals>();
682754fe60SDimitry Andric   AU.setPreservesAll();
692754fe60SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
702754fe60SDimitry Andric }
712754fe60SDimitry Andric 
7291bc56edSDimitry Andric LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(nullptr) {
732754fe60SDimitry Andric   initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
742754fe60SDimitry Andric }
752754fe60SDimitry Andric 
762754fe60SDimitry Andric /// LocMap - Map of where a user value is live, and its location.
772754fe60SDimitry Andric typedef IntervalMap<SlotIndex, unsigned, 4> LocMap;
782754fe60SDimitry Andric 
796122f3e6SDimitry Andric namespace {
8091bc56edSDimitry Andric /// UserValueScopes - Keeps track of lexical scopes associated with a
816122f3e6SDimitry Andric /// user value's source location.
826122f3e6SDimitry Andric class UserValueScopes {
836122f3e6SDimitry Andric   DebugLoc DL;
846122f3e6SDimitry Andric   LexicalScopes &LS;
856122f3e6SDimitry Andric   SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
866122f3e6SDimitry Andric 
876122f3e6SDimitry Andric public:
883ca95b02SDimitry Andric   UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {}
896122f3e6SDimitry Andric 
906122f3e6SDimitry Andric   /// dominates - Return true if current scope dominates at least one machine
916122f3e6SDimitry Andric   /// instruction in a given machine basic block.
926122f3e6SDimitry Andric   bool dominates(MachineBasicBlock *MBB) {
936122f3e6SDimitry Andric     if (LBlocks.empty())
946122f3e6SDimitry Andric       LS.getMachineBasicBlocks(DL, LBlocks);
957d523365SDimitry Andric     return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB);
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 {
11239d628a0SDimitry Andric   const MDNode *Variable;   ///< The debug info variable we are part of.
11339d628a0SDimitry Andric   const MDNode *Expression; ///< Any complex address expression.
1142754fe60SDimitry Andric   unsigned offset;        ///< Byte offset into variable.
115f785676fSDimitry Andric   bool IsIndirect;        ///< true if this is a register-indirect+offset value.
1162754fe60SDimitry Andric   DebugLoc dl;            ///< The debug location for the variable. This is
1172754fe60SDimitry Andric                           ///< used by dwarf writer to find lexical scope.
1182754fe60SDimitry Andric   UserValue *leader;      ///< Equivalence class leader.
1192754fe60SDimitry Andric   UserValue *next;        ///< Next value in equivalence class, or null.
1202754fe60SDimitry Andric 
1212754fe60SDimitry Andric   /// Numbered locations referenced by locmap.
1222754fe60SDimitry Andric   SmallVector<MachineOperand, 4> locations;
1232754fe60SDimitry Andric 
1242754fe60SDimitry Andric   /// Map of slot indices where this value is live.
1252754fe60SDimitry Andric   LocMap locInts;
1262754fe60SDimitry Andric 
1272754fe60SDimitry Andric   /// coalesceLocation - After LocNo was changed, check if it has become
1282754fe60SDimitry Andric   /// identical to another location, and coalesce them. This may cause LocNo or
1292754fe60SDimitry Andric   /// a later location to be erased, but no earlier location will be erased.
1302754fe60SDimitry Andric   void coalesceLocation(unsigned LocNo);
1312754fe60SDimitry Andric 
1322754fe60SDimitry Andric   /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
1332754fe60SDimitry Andric   void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
1342754fe60SDimitry Andric                         LiveIntervals &LIS, const TargetInstrInfo &TII);
1352754fe60SDimitry Andric 
136bd5abe19SDimitry Andric   /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
137bd5abe19SDimitry Andric   /// is live. Returns true if any changes were made.
138f785676fSDimitry Andric   bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
139f785676fSDimitry Andric                      LiveIntervals &LIS);
140bd5abe19SDimitry Andric 
1412754fe60SDimitry Andric public:
1422754fe60SDimitry Andric   /// UserValue - Create a new UserValue.
14339d628a0SDimitry Andric   UserValue(const MDNode *var, const MDNode *expr, unsigned o, bool i,
14439d628a0SDimitry Andric             DebugLoc L, LocMap::Allocator &alloc)
1453ca95b02SDimitry Andric       : Variable(var), Expression(expr), offset(o), IsIndirect(i),
1463ca95b02SDimitry Andric         dl(std::move(L)), leader(this), next(nullptr), locInts(alloc) {}
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?
160ff0cc061SDimitry Andric   bool match(const MDNode *Var, const MDNode *Expr, const DILocation *IA,
161ff0cc061SDimitry Andric              unsigned Offset, bool indirect) const {
162ff0cc061SDimitry Andric     return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA &&
163ff0cc061SDimitry Andric            Offset == offset && indirect == IsIndirect;
1642754fe60SDimitry Andric   }
1652754fe60SDimitry Andric 
1662754fe60SDimitry Andric   /// merge - Merge equivalence classes.
1672754fe60SDimitry Andric   static UserValue *merge(UserValue *L1, UserValue *L2) {
1682754fe60SDimitry Andric     L2 = L2->getLeader();
1692754fe60SDimitry Andric     if (!L1)
1702754fe60SDimitry Andric       return L2;
1712754fe60SDimitry Andric     L1 = L1->getLeader();
1722754fe60SDimitry Andric     if (L1 == L2)
1732754fe60SDimitry Andric       return L1;
1742754fe60SDimitry Andric     // Splice L2 before L1's members.
1752754fe60SDimitry Andric     UserValue *End = L2;
1763ca95b02SDimitry Andric     while (End->next) {
1773ca95b02SDimitry Andric       End->leader = L1;
1783ca95b02SDimitry Andric       End = End->next;
1793ca95b02SDimitry Andric     }
1802754fe60SDimitry Andric     End->leader = L1;
1812754fe60SDimitry Andric     End->next = L1->next;
1822754fe60SDimitry Andric     L1->next = L2;
1832754fe60SDimitry Andric     return L1;
1842754fe60SDimitry Andric   }
1852754fe60SDimitry Andric 
1862754fe60SDimitry Andric   /// getLocationNo - Return the location number that matches Loc.
1872754fe60SDimitry Andric   unsigned getLocationNo(const MachineOperand &LocMO) {
1883b0f4066SDimitry Andric     if (LocMO.isReg()) {
1893b0f4066SDimitry Andric       if (LocMO.getReg() == 0)
1902754fe60SDimitry Andric         return ~0u;
1913b0f4066SDimitry Andric       // For register locations we dont care about use/def and other flags.
1923b0f4066SDimitry Andric       for (unsigned i = 0, e = locations.size(); i != e; ++i)
1933b0f4066SDimitry Andric         if (locations[i].isReg() &&
1943b0f4066SDimitry Andric             locations[i].getReg() == LocMO.getReg() &&
1953b0f4066SDimitry Andric             locations[i].getSubReg() == LocMO.getSubReg())
1963b0f4066SDimitry Andric           return i;
1973b0f4066SDimitry Andric     } else
1982754fe60SDimitry Andric       for (unsigned i = 0, e = locations.size(); i != e; ++i)
1992754fe60SDimitry Andric         if (LocMO.isIdenticalTo(locations[i]))
2002754fe60SDimitry Andric           return i;
2012754fe60SDimitry Andric     locations.push_back(LocMO);
2022754fe60SDimitry Andric     // We are storing a MachineOperand outside a MachineInstr.
2032754fe60SDimitry Andric     locations.back().clearParent();
2043b0f4066SDimitry Andric     // Don't store def operands.
2053b0f4066SDimitry Andric     if (locations.back().isReg())
2063b0f4066SDimitry Andric       locations.back().setIsUse();
2072754fe60SDimitry Andric     return locations.size() - 1;
2082754fe60SDimitry Andric   }
2092754fe60SDimitry Andric 
2103b0f4066SDimitry Andric   /// mapVirtRegs - Ensure that all virtual register locations are mapped.
2113b0f4066SDimitry Andric   void mapVirtRegs(LDVImpl *LDV);
2123b0f4066SDimitry Andric 
2132754fe60SDimitry Andric   /// addDef - Add a definition point to this value.
2142754fe60SDimitry Andric   void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
2152754fe60SDimitry Andric     // Add a singular (Idx,Idx) -> Loc mapping.
2162754fe60SDimitry Andric     LocMap::iterator I = locInts.find(Idx);
2172754fe60SDimitry Andric     if (!I.valid() || I.start() != Idx)
2182754fe60SDimitry Andric       I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
2196122f3e6SDimitry Andric     else
2206122f3e6SDimitry Andric       // A later DBG_VALUE at the same SlotIndex overrides the old location.
2216122f3e6SDimitry Andric       I.setValue(getLocationNo(LocMO));
2222754fe60SDimitry Andric   }
2232754fe60SDimitry Andric 
2242754fe60SDimitry Andric   /// extendDef - Extend the current definition as far as possible down the
2252754fe60SDimitry Andric   /// dominator tree. Stop when meeting an existing def or when leaving the live
2262754fe60SDimitry Andric   /// range of VNI.
2273b0f4066SDimitry Andric   /// End points where VNI is no longer live are added to Kills.
2282754fe60SDimitry Andric   /// @param Idx   Starting point for the definition.
2292754fe60SDimitry Andric   /// @param LocNo Location number to propagate.
230f785676fSDimitry Andric   /// @param LR    Restrict liveness to where LR has the value VNI. May be null.
231f785676fSDimitry Andric   /// @param VNI   When LR is not null, this is the value to restrict to.
2323b0f4066SDimitry Andric   /// @param Kills Append end points of VNI's live range to Kills.
2332754fe60SDimitry Andric   /// @param LIS   Live intervals analysis.
2342754fe60SDimitry Andric   /// @param MDT   Dominator tree.
2352754fe60SDimitry Andric   void extendDef(SlotIndex Idx, unsigned LocNo,
236f785676fSDimitry Andric                  LiveRange *LR, const VNInfo *VNI,
2373b0f4066SDimitry Andric                  SmallVectorImpl<SlotIndex> *Kills,
2386122f3e6SDimitry Andric                  LiveIntervals &LIS, MachineDominatorTree &MDT,
2396122f3e6SDimitry Andric                  UserValueScopes &UVS);
2402754fe60SDimitry Andric 
2413b0f4066SDimitry Andric   /// addDefsFromCopies - The value in LI/LocNo may be copies to other
2423b0f4066SDimitry Andric   /// registers. Determine if any of the copies are available at the kill
2433b0f4066SDimitry Andric   /// points, and add defs if possible.
2443b0f4066SDimitry Andric   /// @param LI      Scan for copies of the value in LI->reg.
2453b0f4066SDimitry Andric   /// @param LocNo   Location number of LI->reg.
2463b0f4066SDimitry Andric   /// @param Kills   Points where the range of LocNo could be extended.
2473b0f4066SDimitry Andric   /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
2483b0f4066SDimitry Andric   void addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
2493b0f4066SDimitry Andric                       const SmallVectorImpl<SlotIndex> &Kills,
2503b0f4066SDimitry Andric                       SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
2513b0f4066SDimitry Andric                       MachineRegisterInfo &MRI,
2523b0f4066SDimitry Andric                       LiveIntervals &LIS);
2533b0f4066SDimitry Andric 
2542754fe60SDimitry Andric   /// computeIntervals - Compute the live intervals of all locations after
2552754fe60SDimitry Andric   /// collecting all their def points.
2567ae0e2c9SDimitry Andric   void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
2576122f3e6SDimitry Andric                         LiveIntervals &LIS, MachineDominatorTree &MDT,
2586122f3e6SDimitry Andric                         UserValueScopes &UVS);
2592754fe60SDimitry Andric 
260bd5abe19SDimitry Andric   /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
261bd5abe19SDimitry Andric   /// live. Returns true if any changes were made.
262f785676fSDimitry Andric   bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
263f785676fSDimitry Andric                      LiveIntervals &LIS);
264bd5abe19SDimitry Andric 
2652754fe60SDimitry Andric   /// rewriteLocations - Rewrite virtual register locations according to the
2662754fe60SDimitry Andric   /// provided virtual register map.
2672754fe60SDimitry Andric   void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
2682754fe60SDimitry Andric 
269139f7f9bSDimitry Andric   /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
2702754fe60SDimitry Andric   void emitDebugValues(VirtRegMap *VRM,
2712754fe60SDimitry Andric                        LiveIntervals &LIS, const TargetInstrInfo &TRI);
2722754fe60SDimitry Andric 
2736122f3e6SDimitry Andric   /// getDebugLoc - Return DebugLoc of this UserValue.
2746122f3e6SDimitry Andric   DebugLoc getDebugLoc() { return dl;}
275ff0cc061SDimitry Andric   void print(raw_ostream &, const TargetRegisterInfo *);
2762754fe60SDimitry Andric };
2772754fe60SDimitry Andric } // namespace
2782754fe60SDimitry Andric 
2792754fe60SDimitry Andric /// LDVImpl - Implementation of the LiveDebugVariables pass.
2802754fe60SDimitry Andric namespace {
2812754fe60SDimitry Andric class LDVImpl {
2822754fe60SDimitry Andric   LiveDebugVariables &pass;
2832754fe60SDimitry Andric   LocMap::Allocator allocator;
2842754fe60SDimitry Andric   MachineFunction *MF;
2852754fe60SDimitry Andric   LiveIntervals *LIS;
2866122f3e6SDimitry Andric   LexicalScopes LS;
2872754fe60SDimitry Andric   MachineDominatorTree *MDT;
2882754fe60SDimitry Andric   const TargetRegisterInfo *TRI;
2892754fe60SDimitry Andric 
290139f7f9bSDimitry Andric   /// Whether emitDebugValues is called.
291139f7f9bSDimitry Andric   bool EmitDone;
292139f7f9bSDimitry Andric   /// Whether the machine function is modified during the pass.
293139f7f9bSDimitry Andric   bool ModifiedMF;
294139f7f9bSDimitry Andric 
2952754fe60SDimitry Andric   /// userValues - All allocated UserValue instances.
29691bc56edSDimitry Andric   SmallVector<std::unique_ptr<UserValue>, 8> userValues;
2972754fe60SDimitry Andric 
2982754fe60SDimitry Andric   /// Map virtual register to eq class leader.
2992754fe60SDimitry Andric   typedef DenseMap<unsigned, UserValue*> VRMap;
3002754fe60SDimitry Andric   VRMap virtRegToEqClass;
3012754fe60SDimitry Andric 
3022754fe60SDimitry Andric   /// Map user variable to eq class leader.
3032754fe60SDimitry Andric   typedef DenseMap<const MDNode *, UserValue*> UVMap;
3042754fe60SDimitry Andric   UVMap userVarMap;
3052754fe60SDimitry Andric 
3062754fe60SDimitry Andric   /// getUserValue - Find or create a UserValue.
30739d628a0SDimitry Andric   UserValue *getUserValue(const MDNode *Var, const MDNode *Expr,
3083ca95b02SDimitry Andric                           unsigned Offset, bool IsIndirect, const DebugLoc &DL);
3092754fe60SDimitry Andric 
3102754fe60SDimitry Andric   /// lookupVirtReg - Find the EC leader for VirtReg or null.
3112754fe60SDimitry Andric   UserValue *lookupVirtReg(unsigned VirtReg);
3122754fe60SDimitry Andric 
3132754fe60SDimitry Andric   /// handleDebugValue - Add DBG_VALUE instruction to our maps.
3142754fe60SDimitry Andric   /// @param MI  DBG_VALUE instruction
3152754fe60SDimitry Andric   /// @param Idx Last valid SLotIndex before instruction.
3162754fe60SDimitry Andric   /// @return    True if the DBG_VALUE instruction should be deleted.
3173ca95b02SDimitry Andric   bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
3182754fe60SDimitry Andric 
3192754fe60SDimitry Andric   /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
3202754fe60SDimitry Andric   /// a UserValue def for each instruction.
3212754fe60SDimitry Andric   /// @param mf MachineFunction to be scanned.
3222754fe60SDimitry Andric   /// @return True if any debug values were found.
3232754fe60SDimitry Andric   bool collectDebugValues(MachineFunction &mf);
3242754fe60SDimitry Andric 
3252754fe60SDimitry Andric   /// computeIntervals - Compute the live intervals of all user values after
3262754fe60SDimitry Andric   /// collecting all their def points.
3272754fe60SDimitry Andric   void computeIntervals();
3282754fe60SDimitry Andric 
3292754fe60SDimitry Andric public:
33039d628a0SDimitry Andric   LDVImpl(LiveDebugVariables *ps)
33139d628a0SDimitry Andric       : pass(*ps), MF(nullptr), EmitDone(false), ModifiedMF(false) {}
3322754fe60SDimitry Andric   bool runOnMachineFunction(MachineFunction &mf);
3332754fe60SDimitry Andric 
334139f7f9bSDimitry Andric   /// clear - Release all memory.
3352754fe60SDimitry Andric   void clear() {
33639d628a0SDimitry Andric     MF = nullptr;
3372754fe60SDimitry Andric     userValues.clear();
3382754fe60SDimitry Andric     virtRegToEqClass.clear();
3392754fe60SDimitry Andric     userVarMap.clear();
340139f7f9bSDimitry Andric     // Make sure we call emitDebugValues if the machine function was modified.
341139f7f9bSDimitry Andric     assert((!ModifiedMF || EmitDone) &&
342139f7f9bSDimitry Andric            "Dbg values are not emitted in LDV");
343139f7f9bSDimitry Andric     EmitDone = false;
344139f7f9bSDimitry Andric     ModifiedMF = false;
34539d628a0SDimitry Andric     LS.reset();
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 
3613ca95b02SDimitry Andric static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
362ff0cc061SDimitry Andric                           const LLVMContext &Ctx) {
363ff0cc061SDimitry Andric   if (!DL)
364ff0cc061SDimitry Andric     return;
365ff0cc061SDimitry Andric 
366ff0cc061SDimitry Andric   auto *Scope = cast<DIScope>(DL.getScope());
367ff0cc061SDimitry Andric   // Omit the directory, because it's likely to be long and uninteresting.
368ff0cc061SDimitry Andric   CommentOS << Scope->getFilename();
369ff0cc061SDimitry Andric   CommentOS << ':' << DL.getLine();
370ff0cc061SDimitry Andric   if (DL.getCol() != 0)
371ff0cc061SDimitry Andric     CommentOS << ':' << DL.getCol();
372ff0cc061SDimitry Andric 
373ff0cc061SDimitry Andric   DebugLoc InlinedAtDL = DL.getInlinedAt();
374ff0cc061SDimitry Andric   if (!InlinedAtDL)
375ff0cc061SDimitry Andric     return;
376ff0cc061SDimitry Andric 
377ff0cc061SDimitry Andric   CommentOS << " @[ ";
378ff0cc061SDimitry Andric   printDebugLoc(InlinedAtDL, CommentOS, Ctx);
379ff0cc061SDimitry Andric   CommentOS << " ]";
380ff0cc061SDimitry Andric }
381ff0cc061SDimitry Andric 
382ff0cc061SDimitry Andric static void printExtendedName(raw_ostream &OS, const DILocalVariable *V,
383ff0cc061SDimitry Andric                               const DILocation *DL) {
384ff0cc061SDimitry Andric   const LLVMContext &Ctx = V->getContext();
385ff0cc061SDimitry Andric   StringRef Res = V->getName();
386ff0cc061SDimitry Andric   if (!Res.empty())
387ff0cc061SDimitry Andric     OS << Res << "," << V->getLine();
388ff0cc061SDimitry Andric   if (auto *InlinedAt = DL->getInlinedAt()) {
389ff0cc061SDimitry Andric     if (DebugLoc InlinedAtDL = InlinedAt) {
390ff0cc061SDimitry Andric       OS << " @[";
391ff0cc061SDimitry Andric       printDebugLoc(InlinedAtDL, OS, Ctx);
392ff0cc061SDimitry Andric       OS << "]";
393ff0cc061SDimitry Andric     }
394ff0cc061SDimitry Andric   }
395ff0cc061SDimitry Andric }
396ff0cc061SDimitry Andric 
397ff0cc061SDimitry Andric void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
398ff0cc061SDimitry Andric   auto *DV = cast<DILocalVariable>(Variable);
3996122f3e6SDimitry Andric   OS << "!\"";
400ff0cc061SDimitry Andric   printExtendedName(OS, DV, dl);
401ff0cc061SDimitry Andric 
4026122f3e6SDimitry Andric   OS << "\"\t";
4032754fe60SDimitry Andric   if (offset)
4042754fe60SDimitry Andric     OS << '+' << offset;
4052754fe60SDimitry Andric   for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
4062754fe60SDimitry Andric     OS << " [" << I.start() << ';' << I.stop() << "):";
4072754fe60SDimitry Andric     if (I.value() == ~0u)
4082754fe60SDimitry Andric       OS << "undef";
4092754fe60SDimitry Andric     else
4102754fe60SDimitry Andric       OS << I.value();
4112754fe60SDimitry Andric   }
412bd5abe19SDimitry Andric   for (unsigned i = 0, e = locations.size(); i != e; ++i) {
413bd5abe19SDimitry Andric     OS << " Loc" << i << '=';
414ff0cc061SDimitry Andric     locations[i].print(OS, TRI);
415bd5abe19SDimitry Andric   }
4162754fe60SDimitry Andric   OS << '\n';
4172754fe60SDimitry Andric }
4182754fe60SDimitry Andric 
4192754fe60SDimitry Andric void LDVImpl::print(raw_ostream &OS) {
4202754fe60SDimitry Andric   OS << "********** DEBUG VARIABLES **********\n";
4212754fe60SDimitry Andric   for (unsigned i = 0, e = userValues.size(); i != e; ++i)
422ff0cc061SDimitry Andric     userValues[i]->print(OS, TRI);
4232754fe60SDimitry Andric }
4242754fe60SDimitry Andric 
4252754fe60SDimitry Andric void UserValue::coalesceLocation(unsigned LocNo) {
4262754fe60SDimitry Andric   unsigned KeepLoc = 0;
4272754fe60SDimitry Andric   for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
4282754fe60SDimitry Andric     if (KeepLoc == LocNo)
4292754fe60SDimitry Andric       continue;
4302754fe60SDimitry Andric     if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
4312754fe60SDimitry Andric       break;
4322754fe60SDimitry Andric   }
4332754fe60SDimitry Andric   // No matches.
4342754fe60SDimitry Andric   if (KeepLoc == locations.size())
4352754fe60SDimitry Andric     return;
4362754fe60SDimitry Andric 
4372754fe60SDimitry Andric   // Keep the smaller location, erase the larger one.
4382754fe60SDimitry Andric   unsigned EraseLoc = LocNo;
4392754fe60SDimitry Andric   if (KeepLoc > EraseLoc)
4402754fe60SDimitry Andric     std::swap(KeepLoc, EraseLoc);
4412754fe60SDimitry Andric   locations.erase(locations.begin() + EraseLoc);
4422754fe60SDimitry Andric 
4432754fe60SDimitry Andric   // Rewrite values.
4442754fe60SDimitry Andric   for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
4452754fe60SDimitry Andric     unsigned v = I.value();
4462754fe60SDimitry Andric     if (v == EraseLoc)
4472754fe60SDimitry Andric       I.setValue(KeepLoc);      // Coalesce when possible.
4482754fe60SDimitry Andric     else if (v > EraseLoc)
4492754fe60SDimitry Andric       I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
4502754fe60SDimitry Andric   }
4512754fe60SDimitry Andric }
4522754fe60SDimitry Andric 
4533b0f4066SDimitry Andric void UserValue::mapVirtRegs(LDVImpl *LDV) {
4543b0f4066SDimitry Andric   for (unsigned i = 0, e = locations.size(); i != e; ++i)
4553b0f4066SDimitry Andric     if (locations[i].isReg() &&
4563b0f4066SDimitry Andric         TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
4573b0f4066SDimitry Andric       LDV->mapVirtReg(locations[i].getReg(), this);
4583b0f4066SDimitry Andric }
4593b0f4066SDimitry Andric 
46039d628a0SDimitry Andric UserValue *LDVImpl::getUserValue(const MDNode *Var, const MDNode *Expr,
46139d628a0SDimitry Andric                                  unsigned Offset, bool IsIndirect,
4623ca95b02SDimitry Andric                                  const DebugLoc &DL) {
4632754fe60SDimitry Andric   UserValue *&Leader = userVarMap[Var];
4642754fe60SDimitry Andric   if (Leader) {
4652754fe60SDimitry Andric     UserValue *UV = Leader->getLeader();
4662754fe60SDimitry Andric     Leader = UV;
4672754fe60SDimitry Andric     for (; UV; UV = UV->getNext())
468ff0cc061SDimitry Andric       if (UV->match(Var, Expr, DL->getInlinedAt(), Offset, IsIndirect))
4692754fe60SDimitry Andric         return UV;
4702754fe60SDimitry Andric   }
4712754fe60SDimitry Andric 
47291bc56edSDimitry Andric   userValues.push_back(
47339d628a0SDimitry Andric       make_unique<UserValue>(Var, Expr, Offset, IsIndirect, DL, allocator));
47491bc56edSDimitry Andric   UserValue *UV = userValues.back().get();
4752754fe60SDimitry Andric   Leader = UserValue::merge(Leader, UV);
4762754fe60SDimitry Andric   return UV;
4772754fe60SDimitry Andric }
4782754fe60SDimitry Andric 
4792754fe60SDimitry Andric void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
4802754fe60SDimitry Andric   assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
4812754fe60SDimitry Andric   UserValue *&Leader = virtRegToEqClass[VirtReg];
4822754fe60SDimitry Andric   Leader = UserValue::merge(Leader, EC);
4832754fe60SDimitry Andric }
4842754fe60SDimitry Andric 
4852754fe60SDimitry Andric UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
4862754fe60SDimitry Andric   if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
4872754fe60SDimitry Andric     return UV->getLeader();
48891bc56edSDimitry Andric   return nullptr;
4892754fe60SDimitry Andric }
4902754fe60SDimitry Andric 
4913ca95b02SDimitry Andric bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
4922754fe60SDimitry Andric   // DBG_VALUE loc, offset, variable
4933ca95b02SDimitry Andric   if (MI.getNumOperands() != 4 ||
4943ca95b02SDimitry Andric       !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
4953ca95b02SDimitry Andric       !MI.getOperand(2).isMetadata()) {
4963ca95b02SDimitry Andric     DEBUG(dbgs() << "Can't handle " << MI);
4972754fe60SDimitry Andric     return false;
4982754fe60SDimitry Andric   }
4992754fe60SDimitry Andric 
5002754fe60SDimitry Andric   // Get or create the UserValue for (variable,offset).
5013ca95b02SDimitry Andric   bool IsIndirect = MI.isIndirectDebugValue();
5023ca95b02SDimitry Andric   unsigned Offset = IsIndirect ? MI.getOperand(1).getImm() : 0;
5033ca95b02SDimitry Andric   const MDNode *Var = MI.getDebugVariable();
5043ca95b02SDimitry Andric   const MDNode *Expr = MI.getDebugExpression();
505f785676fSDimitry Andric   //here.
5063ca95b02SDimitry Andric   UserValue *UV = getUserValue(Var, Expr, Offset, IsIndirect, MI.getDebugLoc());
5073ca95b02SDimitry Andric   UV->addDef(Idx, MI.getOperand(0));
5082754fe60SDimitry Andric   return true;
5092754fe60SDimitry Andric }
5102754fe60SDimitry Andric 
5112754fe60SDimitry Andric bool LDVImpl::collectDebugValues(MachineFunction &mf) {
5122754fe60SDimitry Andric   bool Changed = false;
5132754fe60SDimitry Andric   for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
5142754fe60SDimitry Andric        ++MFI) {
5157d523365SDimitry Andric     MachineBasicBlock *MBB = &*MFI;
5162754fe60SDimitry Andric     for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
5172754fe60SDimitry Andric          MBBI != MBBE;) {
5182754fe60SDimitry Andric       if (!MBBI->isDebugValue()) {
5192754fe60SDimitry Andric         ++MBBI;
5202754fe60SDimitry Andric         continue;
5212754fe60SDimitry Andric       }
5222754fe60SDimitry Andric       // DBG_VALUE has no slot index, use the previous instruction instead.
5233ca95b02SDimitry Andric       SlotIndex Idx =
5243ca95b02SDimitry Andric           MBBI == MBB->begin()
5253ca95b02SDimitry Andric               ? LIS->getMBBStartIdx(MBB)
5263ca95b02SDimitry Andric               : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
5272754fe60SDimitry Andric       // Handle consecutive DBG_VALUE instructions with the same slot index.
5282754fe60SDimitry Andric       do {
5293ca95b02SDimitry Andric         if (handleDebugValue(*MBBI, Idx)) {
5302754fe60SDimitry Andric           MBBI = MBB->erase(MBBI);
5312754fe60SDimitry Andric           Changed = true;
5322754fe60SDimitry Andric         } else
5332754fe60SDimitry Andric           ++MBBI;
5342754fe60SDimitry Andric       } while (MBBI != MBBE && MBBI->isDebugValue());
5352754fe60SDimitry Andric     }
5362754fe60SDimitry Andric   }
5372754fe60SDimitry Andric   return Changed;
5382754fe60SDimitry Andric }
5392754fe60SDimitry Andric 
5407d523365SDimitry Andric /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
5417d523365SDimitry Andric /// data-flow analysis to propagate them beyond basic block boundaries.
5427d523365SDimitry Andric void UserValue::extendDef(SlotIndex Idx, unsigned LocNo, LiveRange *LR,
5437d523365SDimitry Andric                           const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
5446122f3e6SDimitry Andric                           LiveIntervals &LIS, MachineDominatorTree &MDT,
5456122f3e6SDimitry Andric                           UserValueScopes &UVS) {
5467d523365SDimitry Andric   SlotIndex Start = Idx;
5472754fe60SDimitry Andric   MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
5482754fe60SDimitry Andric   SlotIndex Stop = LIS.getMBBEndIdx(MBB);
5492754fe60SDimitry Andric   LocMap::iterator I = locInts.find(Start);
5502754fe60SDimitry Andric 
5512754fe60SDimitry Andric   // Limit to VNI's live range.
5522754fe60SDimitry Andric   bool ToEnd = true;
553f785676fSDimitry Andric   if (LR && VNI) {
554f785676fSDimitry Andric     LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
555f785676fSDimitry Andric     if (!Segment || Segment->valno != VNI) {
5563b0f4066SDimitry Andric       if (Kills)
5573b0f4066SDimitry Andric         Kills->push_back(Start);
5587d523365SDimitry Andric       return;
5593b0f4066SDimitry Andric     }
5603ca95b02SDimitry Andric     if (Segment->end < Stop) {
5613ca95b02SDimitry Andric       Stop = Segment->end;
5623ca95b02SDimitry Andric       ToEnd = false;
5633ca95b02SDimitry Andric     }
5642754fe60SDimitry Andric   }
5652754fe60SDimitry Andric 
5662754fe60SDimitry Andric   // There could already be a short def at Start.
5672754fe60SDimitry Andric   if (I.valid() && I.start() <= Start) {
5682754fe60SDimitry Andric     // Stop when meeting a different location or an already extended interval.
5692754fe60SDimitry Andric     Start = Start.getNextSlot();
5702754fe60SDimitry Andric     if (I.value() != LocNo || I.stop() != Start)
5717d523365SDimitry Andric       return;
5722754fe60SDimitry Andric     // This is a one-slot placeholder. Just skip it.
5732754fe60SDimitry Andric     ++I;
5742754fe60SDimitry Andric   }
5752754fe60SDimitry Andric 
5762754fe60SDimitry Andric   // Limited by the next def.
5773ca95b02SDimitry Andric   if (I.valid() && I.start() < Stop) {
5783ca95b02SDimitry Andric     Stop = I.start();
5793ca95b02SDimitry Andric     ToEnd = false;
5803ca95b02SDimitry Andric   }
5813b0f4066SDimitry Andric   // Limited by VNI's live range.
5823b0f4066SDimitry Andric   else if (!ToEnd && Kills)
5833b0f4066SDimitry Andric     Kills->push_back(Stop);
5842754fe60SDimitry Andric 
5857d523365SDimitry Andric   if (Start < Stop)
5862754fe60SDimitry Andric     I.insert(Start, Stop, LocNo);
5872754fe60SDimitry Andric }
5882754fe60SDimitry Andric 
5892754fe60SDimitry Andric void
5903b0f4066SDimitry Andric UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
5913b0f4066SDimitry Andric                       const SmallVectorImpl<SlotIndex> &Kills,
5923b0f4066SDimitry Andric                       SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
5933b0f4066SDimitry Andric                       MachineRegisterInfo &MRI, LiveIntervals &LIS) {
5943b0f4066SDimitry Andric   if (Kills.empty())
5953b0f4066SDimitry Andric     return;
5963b0f4066SDimitry Andric   // Don't track copies from physregs, there are too many uses.
5973b0f4066SDimitry Andric   if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
5983b0f4066SDimitry Andric     return;
5993b0f4066SDimitry Andric 
6003b0f4066SDimitry Andric   // Collect all the (vreg, valno) pairs that are copies of LI.
6013b0f4066SDimitry Andric   SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
60291bc56edSDimitry Andric   for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
60391bc56edSDimitry Andric     MachineInstr *MI = MO.getParent();
6043b0f4066SDimitry Andric     // Copies of the full value.
60591bc56edSDimitry Andric     if (MO.getSubReg() || !MI->isCopy())
6063b0f4066SDimitry Andric       continue;
6073b0f4066SDimitry Andric     unsigned DstReg = MI->getOperand(0).getReg();
6083b0f4066SDimitry Andric 
6093b0f4066SDimitry Andric     // Don't follow copies to physregs. These are usually setting up call
6103b0f4066SDimitry Andric     // arguments, and the argument registers are always call clobbered. We are
6113b0f4066SDimitry Andric     // better off in the source register which could be a callee-saved register,
6123b0f4066SDimitry Andric     // or it could be spilled.
6133b0f4066SDimitry Andric     if (!TargetRegisterInfo::isVirtualRegister(DstReg))
6143b0f4066SDimitry Andric       continue;
6153b0f4066SDimitry Andric 
6163b0f4066SDimitry Andric     // Is LocNo extended to reach this copy? If not, another def may be blocking
6173b0f4066SDimitry Andric     // it, or we are looking at a wrong value of LI.
6183ca95b02SDimitry Andric     SlotIndex Idx = LIS.getInstructionIndex(*MI);
619dff0c46cSDimitry Andric     LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
6203b0f4066SDimitry Andric     if (!I.valid() || I.value() != LocNo)
6213b0f4066SDimitry Andric       continue;
6223b0f4066SDimitry Andric 
6233b0f4066SDimitry Andric     if (!LIS.hasInterval(DstReg))
6243b0f4066SDimitry Andric       continue;
6253b0f4066SDimitry Andric     LiveInterval *DstLI = &LIS.getInterval(DstReg);
626dff0c46cSDimitry Andric     const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
627dff0c46cSDimitry Andric     assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
6283b0f4066SDimitry Andric     CopyValues.push_back(std::make_pair(DstLI, DstVNI));
6293b0f4066SDimitry Andric   }
6303b0f4066SDimitry Andric 
6313b0f4066SDimitry Andric   if (CopyValues.empty())
6323b0f4066SDimitry Andric     return;
6333b0f4066SDimitry Andric 
6343b0f4066SDimitry Andric   DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
6353b0f4066SDimitry Andric 
6363b0f4066SDimitry Andric   // Try to add defs of the copied values for each kill point.
6373b0f4066SDimitry Andric   for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
6383b0f4066SDimitry Andric     SlotIndex Idx = Kills[i];
6393b0f4066SDimitry Andric     for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
6403b0f4066SDimitry Andric       LiveInterval *DstLI = CopyValues[j].first;
6413b0f4066SDimitry Andric       const VNInfo *DstVNI = CopyValues[j].second;
6423b0f4066SDimitry Andric       if (DstLI->getVNInfoAt(Idx) != DstVNI)
6433b0f4066SDimitry Andric         continue;
6443b0f4066SDimitry Andric       // Check that there isn't already a def at Idx
6453b0f4066SDimitry Andric       LocMap::iterator I = locInts.find(Idx);
6463b0f4066SDimitry Andric       if (I.valid() && I.start() <= Idx)
6473b0f4066SDimitry Andric         continue;
6483b0f4066SDimitry Andric       DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
6493b0f4066SDimitry Andric                    << DstVNI->id << " in " << *DstLI << '\n');
6503b0f4066SDimitry Andric       MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
6513b0f4066SDimitry Andric       assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
6523b0f4066SDimitry Andric       unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
6533b0f4066SDimitry Andric       I.insert(Idx, Idx.getNextSlot(), LocNo);
6543b0f4066SDimitry Andric       NewDefs.push_back(std::make_pair(Idx, LocNo));
6553b0f4066SDimitry Andric       break;
6563b0f4066SDimitry Andric     }
6573b0f4066SDimitry Andric   }
6583b0f4066SDimitry Andric }
6593b0f4066SDimitry Andric 
6603b0f4066SDimitry Andric void
6613b0f4066SDimitry Andric UserValue::computeIntervals(MachineRegisterInfo &MRI,
6627ae0e2c9SDimitry Andric                             const TargetRegisterInfo &TRI,
6633b0f4066SDimitry Andric                             LiveIntervals &LIS,
6646122f3e6SDimitry Andric                             MachineDominatorTree &MDT,
6656122f3e6SDimitry Andric                             UserValueScopes &UVS) {
6662754fe60SDimitry Andric   SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
6672754fe60SDimitry Andric 
6682754fe60SDimitry Andric   // Collect all defs to be extended (Skipping undefs).
6692754fe60SDimitry Andric   for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
6702754fe60SDimitry Andric     if (I.value() != ~0u)
6712754fe60SDimitry Andric       Defs.push_back(std::make_pair(I.start(), I.value()));
6722754fe60SDimitry Andric 
6733b0f4066SDimitry Andric   // Extend all defs, and possibly add new ones along the way.
6743b0f4066SDimitry Andric   for (unsigned i = 0; i != Defs.size(); ++i) {
6752754fe60SDimitry Andric     SlotIndex Idx = Defs[i].first;
6762754fe60SDimitry Andric     unsigned LocNo = Defs[i].second;
6772754fe60SDimitry Andric     const MachineOperand &Loc = locations[LocNo];
6782754fe60SDimitry Andric 
6797ae0e2c9SDimitry Andric     if (!Loc.isReg()) {
68091bc56edSDimitry Andric       extendDef(Idx, LocNo, nullptr, nullptr, nullptr, LIS, MDT, UVS);
6817ae0e2c9SDimitry Andric       continue;
6827ae0e2c9SDimitry Andric     }
6837ae0e2c9SDimitry Andric 
6842754fe60SDimitry Andric     // Register locations are constrained to where the register value is live.
6857ae0e2c9SDimitry Andric     if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
68691bc56edSDimitry Andric       LiveInterval *LI = nullptr;
68791bc56edSDimitry Andric       const VNInfo *VNI = nullptr;
6887ae0e2c9SDimitry Andric       if (LIS.hasInterval(Loc.getReg())) {
6897ae0e2c9SDimitry Andric         LI = &LIS.getInterval(Loc.getReg());
6907ae0e2c9SDimitry Andric         VNI = LI->getVNInfoAt(Idx);
6917ae0e2c9SDimitry Andric       }
6923b0f4066SDimitry Andric       SmallVector<SlotIndex, 16> Kills;
6936122f3e6SDimitry Andric       extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS);
6947ae0e2c9SDimitry Andric       if (LI)
6953b0f4066SDimitry Andric         addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
6967ae0e2c9SDimitry Andric       continue;
6977ae0e2c9SDimitry Andric     }
6987ae0e2c9SDimitry Andric 
6997ae0e2c9SDimitry Andric     // For physregs, use the live range of the first regunit as a guide.
7007ae0e2c9SDimitry Andric     unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI);
701f785676fSDimitry Andric     LiveRange *LR = &LIS.getRegUnit(Unit);
702f785676fSDimitry Andric     const VNInfo *VNI = LR->getVNInfoAt(Idx);
7037ae0e2c9SDimitry Andric     // Don't track copies from physregs, it is too expensive.
70491bc56edSDimitry Andric     extendDef(Idx, LocNo, LR, VNI, nullptr, LIS, MDT, UVS);
7052754fe60SDimitry Andric   }
7062754fe60SDimitry Andric 
7072754fe60SDimitry Andric   // Finally, erase all the undefs.
7082754fe60SDimitry Andric   for (LocMap::iterator I = locInts.begin(); I.valid();)
7092754fe60SDimitry Andric     if (I.value() == ~0u)
7102754fe60SDimitry Andric       I.erase();
7112754fe60SDimitry Andric     else
7122754fe60SDimitry Andric       ++I;
7132754fe60SDimitry Andric }
7142754fe60SDimitry Andric 
7152754fe60SDimitry Andric void LDVImpl::computeIntervals() {
7163b0f4066SDimitry Andric   for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
7176122f3e6SDimitry Andric     UserValueScopes UVS(userValues[i]->getDebugLoc(), LS);
7187ae0e2c9SDimitry Andric     userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS);
7193b0f4066SDimitry Andric     userValues[i]->mapVirtRegs(this);
7203b0f4066SDimitry Andric   }
7212754fe60SDimitry Andric }
7222754fe60SDimitry Andric 
7232754fe60SDimitry Andric bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
72439d628a0SDimitry Andric   clear();
7252754fe60SDimitry Andric   MF = &mf;
7262754fe60SDimitry Andric   LIS = &pass.getAnalysis<LiveIntervals>();
7272754fe60SDimitry Andric   MDT = &pass.getAnalysis<MachineDominatorTree>();
72839d628a0SDimitry Andric   TRI = mf.getSubtarget().getRegisterInfo();
7296122f3e6SDimitry Andric   LS.initialize(mf);
7302754fe60SDimitry Andric   DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
7313861d79fSDimitry Andric                << mf.getName() << " **********\n");
7322754fe60SDimitry Andric 
7332754fe60SDimitry Andric   bool Changed = collectDebugValues(mf);
7342754fe60SDimitry Andric   computeIntervals();
7352754fe60SDimitry Andric   DEBUG(print(dbgs()));
736139f7f9bSDimitry Andric   ModifiedMF = Changed;
7372754fe60SDimitry Andric   return Changed;
7382754fe60SDimitry Andric }
7392754fe60SDimitry Andric 
74039d628a0SDimitry Andric static void removeDebugValues(MachineFunction &mf) {
74139d628a0SDimitry Andric   for (MachineBasicBlock &MBB : mf) {
74239d628a0SDimitry Andric     for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
74339d628a0SDimitry Andric       if (!MBBI->isDebugValue()) {
74439d628a0SDimitry Andric         ++MBBI;
74539d628a0SDimitry Andric         continue;
74639d628a0SDimitry Andric       }
74739d628a0SDimitry Andric       MBBI = MBB.erase(MBBI);
74839d628a0SDimitry Andric     }
74939d628a0SDimitry Andric   }
75039d628a0SDimitry Andric }
75139d628a0SDimitry Andric 
7522754fe60SDimitry Andric bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
7532754fe60SDimitry Andric   if (!EnableLDV)
7542754fe60SDimitry Andric     return false;
7557d523365SDimitry Andric   if (!mf.getFunction()->getSubprogram()) {
75639d628a0SDimitry Andric     removeDebugValues(mf);
75739d628a0SDimitry Andric     return false;
75839d628a0SDimitry Andric   }
7592754fe60SDimitry Andric   if (!pImpl)
7602754fe60SDimitry Andric     pImpl = new LDVImpl(this);
7612754fe60SDimitry Andric   return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
7622754fe60SDimitry Andric }
7632754fe60SDimitry Andric 
7642754fe60SDimitry Andric void LiveDebugVariables::releaseMemory() {
7652754fe60SDimitry Andric   if (pImpl)
7662754fe60SDimitry Andric     static_cast<LDVImpl*>(pImpl)->clear();
7672754fe60SDimitry Andric }
7682754fe60SDimitry Andric 
7692754fe60SDimitry Andric LiveDebugVariables::~LiveDebugVariables() {
7702754fe60SDimitry Andric   if (pImpl)
7712754fe60SDimitry Andric     delete static_cast<LDVImpl*>(pImpl);
7722754fe60SDimitry Andric }
7732754fe60SDimitry Andric 
774bd5abe19SDimitry Andric //===----------------------------------------------------------------------===//
775bd5abe19SDimitry Andric //                           Live Range Splitting
776bd5abe19SDimitry Andric //===----------------------------------------------------------------------===//
777bd5abe19SDimitry Andric 
778bd5abe19SDimitry Andric bool
779f785676fSDimitry Andric UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
780f785676fSDimitry Andric                          LiveIntervals& LIS) {
781bd5abe19SDimitry Andric   DEBUG({
782bd5abe19SDimitry Andric     dbgs() << "Splitting Loc" << OldLocNo << '\t';
78391bc56edSDimitry Andric     print(dbgs(), nullptr);
784bd5abe19SDimitry Andric   });
785bd5abe19SDimitry Andric   bool DidChange = false;
786bd5abe19SDimitry Andric   LocMap::iterator LocMapI;
787bd5abe19SDimitry Andric   LocMapI.setMap(locInts);
788bd5abe19SDimitry Andric   for (unsigned i = 0; i != NewRegs.size(); ++i) {
789f785676fSDimitry Andric     LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
790bd5abe19SDimitry Andric     if (LI->empty())
791bd5abe19SDimitry Andric       continue;
792bd5abe19SDimitry Andric 
793bd5abe19SDimitry Andric     // Don't allocate the new LocNo until it is needed.
794bd5abe19SDimitry Andric     unsigned NewLocNo = ~0u;
795bd5abe19SDimitry Andric 
796bd5abe19SDimitry Andric     // Iterate over the overlaps between locInts and LI.
797bd5abe19SDimitry Andric     LocMapI.find(LI->beginIndex());
798bd5abe19SDimitry Andric     if (!LocMapI.valid())
799bd5abe19SDimitry Andric       continue;
800bd5abe19SDimitry Andric     LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
801bd5abe19SDimitry Andric     LiveInterval::iterator LIE = LI->end();
802bd5abe19SDimitry Andric     while (LocMapI.valid() && LII != LIE) {
803bd5abe19SDimitry Andric       // At this point, we know that LocMapI.stop() > LII->start.
804bd5abe19SDimitry Andric       LII = LI->advanceTo(LII, LocMapI.start());
805bd5abe19SDimitry Andric       if (LII == LIE)
806bd5abe19SDimitry Andric         break;
807bd5abe19SDimitry Andric 
808bd5abe19SDimitry Andric       // Now LII->end > LocMapI.start(). Do we have an overlap?
809bd5abe19SDimitry Andric       if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
810bd5abe19SDimitry Andric         // Overlapping correct location. Allocate NewLocNo now.
811bd5abe19SDimitry Andric         if (NewLocNo == ~0u) {
812bd5abe19SDimitry Andric           MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
813bd5abe19SDimitry Andric           MO.setSubReg(locations[OldLocNo].getSubReg());
814bd5abe19SDimitry Andric           NewLocNo = getLocationNo(MO);
815bd5abe19SDimitry Andric           DidChange = true;
816bd5abe19SDimitry Andric         }
817bd5abe19SDimitry Andric 
818bd5abe19SDimitry Andric         SlotIndex LStart = LocMapI.start();
819bd5abe19SDimitry Andric         SlotIndex LStop  = LocMapI.stop();
820bd5abe19SDimitry Andric 
821bd5abe19SDimitry Andric         // Trim LocMapI down to the LII overlap.
822bd5abe19SDimitry Andric         if (LStart < LII->start)
823bd5abe19SDimitry Andric           LocMapI.setStartUnchecked(LII->start);
824bd5abe19SDimitry Andric         if (LStop > LII->end)
825bd5abe19SDimitry Andric           LocMapI.setStopUnchecked(LII->end);
826bd5abe19SDimitry Andric 
827bd5abe19SDimitry Andric         // Change the value in the overlap. This may trigger coalescing.
828bd5abe19SDimitry Andric         LocMapI.setValue(NewLocNo);
829bd5abe19SDimitry Andric 
830bd5abe19SDimitry Andric         // Re-insert any removed OldLocNo ranges.
831bd5abe19SDimitry Andric         if (LStart < LocMapI.start()) {
832bd5abe19SDimitry Andric           LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
833bd5abe19SDimitry Andric           ++LocMapI;
834bd5abe19SDimitry Andric           assert(LocMapI.valid() && "Unexpected coalescing");
835bd5abe19SDimitry Andric         }
836bd5abe19SDimitry Andric         if (LStop > LocMapI.stop()) {
837bd5abe19SDimitry Andric           ++LocMapI;
838bd5abe19SDimitry Andric           LocMapI.insert(LII->end, LStop, OldLocNo);
839bd5abe19SDimitry Andric           --LocMapI;
840bd5abe19SDimitry Andric         }
841bd5abe19SDimitry Andric       }
842bd5abe19SDimitry Andric 
843bd5abe19SDimitry Andric       // Advance to the next overlap.
844bd5abe19SDimitry Andric       if (LII->end < LocMapI.stop()) {
845bd5abe19SDimitry Andric         if (++LII == LIE)
846bd5abe19SDimitry Andric           break;
847bd5abe19SDimitry Andric         LocMapI.advanceTo(LII->start);
848bd5abe19SDimitry Andric       } else {
849bd5abe19SDimitry Andric         ++LocMapI;
850bd5abe19SDimitry Andric         if (!LocMapI.valid())
851bd5abe19SDimitry Andric           break;
852bd5abe19SDimitry Andric         LII = LI->advanceTo(LII, LocMapI.start());
853bd5abe19SDimitry Andric       }
854bd5abe19SDimitry Andric     }
855bd5abe19SDimitry Andric   }
856bd5abe19SDimitry Andric 
857bd5abe19SDimitry Andric   // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
858bd5abe19SDimitry Andric   locations.erase(locations.begin() + OldLocNo);
859bd5abe19SDimitry Andric   LocMapI.goToBegin();
860bd5abe19SDimitry Andric   while (LocMapI.valid()) {
861bd5abe19SDimitry Andric     unsigned v = LocMapI.value();
862bd5abe19SDimitry Andric     if (v == OldLocNo) {
863bd5abe19SDimitry Andric       DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
864bd5abe19SDimitry Andric                    << LocMapI.stop() << ")\n");
865bd5abe19SDimitry Andric       LocMapI.erase();
866bd5abe19SDimitry Andric     } else {
867bd5abe19SDimitry Andric       if (v > OldLocNo)
868bd5abe19SDimitry Andric         LocMapI.setValueUnchecked(v-1);
869bd5abe19SDimitry Andric       ++LocMapI;
870bd5abe19SDimitry Andric     }
871bd5abe19SDimitry Andric   }
872bd5abe19SDimitry Andric 
87391bc56edSDimitry Andric   DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);});
874bd5abe19SDimitry Andric   return DidChange;
875bd5abe19SDimitry Andric }
876bd5abe19SDimitry Andric 
877bd5abe19SDimitry Andric bool
878f785676fSDimitry Andric UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
879f785676fSDimitry Andric                          LiveIntervals &LIS) {
880bd5abe19SDimitry Andric   bool DidChange = false;
881bd5abe19SDimitry Andric   // Split locations referring to OldReg. Iterate backwards so splitLocation can
882dff0c46cSDimitry Andric   // safely erase unused locations.
883bd5abe19SDimitry Andric   for (unsigned i = locations.size(); i ; --i) {
884bd5abe19SDimitry Andric     unsigned LocNo = i-1;
885bd5abe19SDimitry Andric     const MachineOperand *Loc = &locations[LocNo];
886bd5abe19SDimitry Andric     if (!Loc->isReg() || Loc->getReg() != OldReg)
887bd5abe19SDimitry Andric       continue;
888f785676fSDimitry Andric     DidChange |= splitLocation(LocNo, NewRegs, LIS);
889bd5abe19SDimitry Andric   }
890bd5abe19SDimitry Andric   return DidChange;
891bd5abe19SDimitry Andric }
892bd5abe19SDimitry Andric 
893f785676fSDimitry Andric void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
894bd5abe19SDimitry Andric   bool DidChange = false;
895bd5abe19SDimitry Andric   for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
896f785676fSDimitry Andric     DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
897bd5abe19SDimitry Andric 
898bd5abe19SDimitry Andric   if (!DidChange)
899bd5abe19SDimitry Andric     return;
900bd5abe19SDimitry Andric 
901bd5abe19SDimitry Andric   // Map all of the new virtual registers.
902bd5abe19SDimitry Andric   UserValue *UV = lookupVirtReg(OldReg);
903bd5abe19SDimitry Andric   for (unsigned i = 0; i != NewRegs.size(); ++i)
904f785676fSDimitry Andric     mapVirtReg(NewRegs[i], UV);
905bd5abe19SDimitry Andric }
906bd5abe19SDimitry Andric 
907bd5abe19SDimitry Andric void LiveDebugVariables::
908f785676fSDimitry Andric splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
909bd5abe19SDimitry Andric   if (pImpl)
910bd5abe19SDimitry Andric     static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
911bd5abe19SDimitry Andric }
912bd5abe19SDimitry Andric 
9132754fe60SDimitry Andric void
9142754fe60SDimitry Andric UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
9152754fe60SDimitry Andric   // Iterate over locations in reverse makes it easier to handle coalescing.
9162754fe60SDimitry Andric   for (unsigned i = locations.size(); i ; --i) {
9172754fe60SDimitry Andric     unsigned LocNo = i-1;
9182754fe60SDimitry Andric     MachineOperand &Loc = locations[LocNo];
9192754fe60SDimitry Andric     // Only virtual registers are rewritten.
9202754fe60SDimitry Andric     if (!Loc.isReg() || !Loc.getReg() ||
9212754fe60SDimitry Andric         !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
9222754fe60SDimitry Andric       continue;
9232754fe60SDimitry Andric     unsigned VirtReg = Loc.getReg();
9242754fe60SDimitry Andric     if (VRM.isAssignedReg(VirtReg) &&
9252754fe60SDimitry Andric         TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
926bd5abe19SDimitry Andric       // This can create a %noreg operand in rare cases when the sub-register
927bd5abe19SDimitry Andric       // index is no longer available. That means the user value is in a
928bd5abe19SDimitry Andric       // non-existent sub-register, and %noreg is exactly what we want.
9292754fe60SDimitry Andric       Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
930dff0c46cSDimitry Andric     } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
9312754fe60SDimitry Andric       // FIXME: Translate SubIdx to a stackslot offset.
9322754fe60SDimitry Andric       Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
9332754fe60SDimitry Andric     } else {
9342754fe60SDimitry Andric       Loc.setReg(0);
9352754fe60SDimitry Andric       Loc.setSubReg(0);
9362754fe60SDimitry Andric     }
9372754fe60SDimitry Andric     coalesceLocation(LocNo);
9382754fe60SDimitry Andric   }
9392754fe60SDimitry Andric }
9402754fe60SDimitry Andric 
9412754fe60SDimitry Andric /// findInsertLocation - Find an iterator for inserting a DBG_VALUE
9422754fe60SDimitry Andric /// instruction.
9432754fe60SDimitry Andric static MachineBasicBlock::iterator
9442754fe60SDimitry Andric findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
9452754fe60SDimitry Andric                    LiveIntervals &LIS) {
9462754fe60SDimitry Andric   SlotIndex Start = LIS.getMBBStartIdx(MBB);
9472754fe60SDimitry Andric   Idx = Idx.getBaseIndex();
9482754fe60SDimitry Andric 
9492754fe60SDimitry Andric   // Try to find an insert location by going backwards from Idx.
9502754fe60SDimitry Andric   MachineInstr *MI;
9512754fe60SDimitry Andric   while (!(MI = LIS.getInstructionFromIndex(Idx))) {
9522754fe60SDimitry Andric     // We've reached the beginning of MBB.
9532754fe60SDimitry Andric     if (Idx == Start) {
9542754fe60SDimitry Andric       MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
9552754fe60SDimitry Andric       return I;
9562754fe60SDimitry Andric     }
9572754fe60SDimitry Andric     Idx = Idx.getPrevIndex();
9582754fe60SDimitry Andric   }
9592754fe60SDimitry Andric 
9602754fe60SDimitry Andric   // Don't insert anything after the first terminator, though.
961dff0c46cSDimitry Andric   return MI->isTerminator() ? MBB->getFirstTerminator() :
96291bc56edSDimitry Andric                               std::next(MachineBasicBlock::iterator(MI));
9632754fe60SDimitry Andric }
9642754fe60SDimitry Andric 
9652754fe60SDimitry Andric void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
9662754fe60SDimitry Andric                                  unsigned LocNo,
9672754fe60SDimitry Andric                                  LiveIntervals &LIS,
9682754fe60SDimitry Andric                                  const TargetInstrInfo &TII) {
9692754fe60SDimitry Andric   MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
9702754fe60SDimitry Andric   MachineOperand &Loc = locations[LocNo];
9716122f3e6SDimitry Andric   ++NumInsertedDebugValues;
9722754fe60SDimitry Andric 
973ff0cc061SDimitry Andric   assert(cast<DILocalVariable>(Variable)
974ff0cc061SDimitry Andric              ->isValidLocationForIntrinsic(getDebugLoc()) &&
975ff0cc061SDimitry Andric          "Expected inlined-at fields to agree");
976f785676fSDimitry Andric   if (Loc.isReg())
977ff0cc061SDimitry Andric     BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
97839d628a0SDimitry Andric             IsIndirect, Loc.getReg(), offset, Variable, Expression);
979f785676fSDimitry Andric   else
980ff0cc061SDimitry Andric     BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
98139d628a0SDimitry Andric         .addOperand(Loc)
98239d628a0SDimitry Andric         .addImm(offset)
98339d628a0SDimitry Andric         .addMetadata(Variable)
98439d628a0SDimitry Andric         .addMetadata(Expression);
9852754fe60SDimitry Andric }
9862754fe60SDimitry Andric 
9872754fe60SDimitry Andric void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
9882754fe60SDimitry Andric                                 const TargetInstrInfo &TII) {
9892754fe60SDimitry Andric   MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
9902754fe60SDimitry Andric 
9912754fe60SDimitry Andric   for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
9922754fe60SDimitry Andric     SlotIndex Start = I.start();
9932754fe60SDimitry Andric     SlotIndex Stop = I.stop();
9942754fe60SDimitry Andric     unsigned LocNo = I.value();
9952754fe60SDimitry Andric     DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
9967d523365SDimitry Andric     MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
9977d523365SDimitry Andric     SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
9982754fe60SDimitry Andric 
9992754fe60SDimitry Andric     DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
10007d523365SDimitry Andric     insertDebugValue(&*MBB, Start, LocNo, LIS, TII);
10012754fe60SDimitry Andric     // This interval may span multiple basic blocks.
10022754fe60SDimitry Andric     // Insert a DBG_VALUE into each one.
10032754fe60SDimitry Andric     while(Stop > MBBEnd) {
10042754fe60SDimitry Andric       // Move to the next block.
10052754fe60SDimitry Andric       Start = MBBEnd;
10062754fe60SDimitry Andric       if (++MBB == MFEnd)
10072754fe60SDimitry Andric         break;
10087d523365SDimitry Andric       MBBEnd = LIS.getMBBEndIdx(&*MBB);
10092754fe60SDimitry Andric       DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
10107d523365SDimitry Andric       insertDebugValue(&*MBB, Start, LocNo, LIS, TII);
10112754fe60SDimitry Andric     }
10122754fe60SDimitry Andric     DEBUG(dbgs() << '\n');
10132754fe60SDimitry Andric     if (MBB == MFEnd)
10142754fe60SDimitry Andric       break;
10152754fe60SDimitry Andric 
10162754fe60SDimitry Andric     ++I;
10172754fe60SDimitry Andric   }
10182754fe60SDimitry Andric }
10192754fe60SDimitry Andric 
10202754fe60SDimitry Andric void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
10212754fe60SDimitry Andric   DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
102239d628a0SDimitry Andric   if (!MF)
102339d628a0SDimitry Andric     return;
102439d628a0SDimitry Andric   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
10252754fe60SDimitry Andric   for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
1026ff0cc061SDimitry Andric     DEBUG(userValues[i]->print(dbgs(), TRI));
10272754fe60SDimitry Andric     userValues[i]->rewriteLocations(*VRM, *TRI);
10282754fe60SDimitry Andric     userValues[i]->emitDebugValues(VRM, *LIS, *TII);
10292754fe60SDimitry Andric   }
1030139f7f9bSDimitry Andric   EmitDone = true;
10312754fe60SDimitry Andric }
10322754fe60SDimitry Andric 
10332754fe60SDimitry Andric void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
10342754fe60SDimitry Andric   if (pImpl)
10352754fe60SDimitry Andric     static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
10362754fe60SDimitry Andric }
10372754fe60SDimitry Andric 
103839d628a0SDimitry Andric bool LiveDebugVariables::doInitialization(Module &M) {
103939d628a0SDimitry Andric   return Pass::doInitialization(M);
104039d628a0SDimitry Andric }
10412754fe60SDimitry Andric 
10422754fe60SDimitry Andric #ifndef NDEBUG
10433ca95b02SDimitry Andric LLVM_DUMP_METHOD void LiveDebugVariables::dump() {
10442754fe60SDimitry Andric   if (pImpl)
10452754fe60SDimitry Andric     static_cast<LDVImpl*>(pImpl)->print(dbgs());
10462754fe60SDimitry Andric }
10472754fe60SDimitry Andric #endif
1048