12754fe60SDimitry Andric //===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
22754fe60SDimitry Andric //
32754fe60SDimitry Andric // The LLVM Compiler Infrastructure
42754fe60SDimitry Andric //
52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source
62754fe60SDimitry Andric // License. See LICENSE.TXT for details.
72754fe60SDimitry Andric //
82754fe60SDimitry Andric //===----------------------------------------------------------------------===//
92754fe60SDimitry Andric //
102754fe60SDimitry Andric // This file implements the LiveDebugVariables analysis.
112754fe60SDimitry Andric //
122754fe60SDimitry Andric // Remove all DBG_VALUE instructions referencing virtual registers and replace
132754fe60SDimitry Andric // them with a data structure tracking where live user variables are kept - in a
142754fe60SDimitry Andric // virtual register or in a stack slot.
152754fe60SDimitry Andric //
162754fe60SDimitry Andric // Allow the data structure to be updated during register allocation when values
172754fe60SDimitry Andric // are moved between registers and stack slots. Finally emit new DBG_VALUE
182754fe60SDimitry Andric // instructions after register allocation is complete.
192754fe60SDimitry Andric //
202754fe60SDimitry Andric //===----------------------------------------------------------------------===//
212754fe60SDimitry Andric
222754fe60SDimitry Andric #include "LiveDebugVariables.h"
232cab237bSDimitry Andric #include "llvm/ADT/ArrayRef.h"
242cab237bSDimitry Andric #include "llvm/ADT/DenseMap.h"
252754fe60SDimitry Andric #include "llvm/ADT/IntervalMap.h"
262cab237bSDimitry Andric #include "llvm/ADT/STLExtras.h"
272cab237bSDimitry Andric #include "llvm/ADT/SmallSet.h"
282cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h"
296122f3e6SDimitry Andric #include "llvm/ADT/Statistic.h"
302cab237bSDimitry Andric #include "llvm/ADT/StringRef.h"
312cab237bSDimitry Andric #include "llvm/CodeGen/LexicalScopes.h"
322cab237bSDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
332cab237bSDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
342cab237bSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
352754fe60SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
362754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
372cab237bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
382754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
392cab237bSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
403b0f4066SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
412cab237bSDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
422cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
432cab237bSDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
442cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
452cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
46139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
474ba319b5SDimitry Andric #include "llvm/Config/llvm-config.h"
482cab237bSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
492cab237bSDimitry Andric #include "llvm/IR/DebugLoc.h"
502cab237bSDimitry Andric #include "llvm/IR/Function.h"
51139f7f9bSDimitry Andric #include "llvm/IR/Metadata.h"
522cab237bSDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
532cab237bSDimitry Andric #include "llvm/Pass.h"
542cab237bSDimitry Andric #include "llvm/Support/Casting.h"
552754fe60SDimitry Andric #include "llvm/Support/CommandLine.h"
562cab237bSDimitry Andric #include "llvm/Support/Compiler.h"
572754fe60SDimitry Andric #include "llvm/Support/Debug.h"
58ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h"
592cab237bSDimitry Andric #include <algorithm>
602cab237bSDimitry Andric #include <cassert>
612cab237bSDimitry Andric #include <iterator>
6291bc56edSDimitry Andric #include <memory>
633ca95b02SDimitry Andric #include <utility>
6491bc56edSDimitry Andric
652754fe60SDimitry Andric using namespace llvm;
662754fe60SDimitry Andric
67302affcbSDimitry Andric #define DEBUG_TYPE "livedebugvars"
6891bc56edSDimitry Andric
692754fe60SDimitry Andric static cl::opt<bool>
702754fe60SDimitry Andric EnableLDV("live-debug-variables", cl::init(true),
712754fe60SDimitry Andric cl::desc("Enable the live debug variables pass"), cl::Hidden);
722754fe60SDimitry Andric
736122f3e6SDimitry Andric STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
742cab237bSDimitry Andric
752754fe60SDimitry Andric char LiveDebugVariables::ID = 0;
762754fe60SDimitry Andric
77302affcbSDimitry Andric INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
782754fe60SDimitry Andric "Debug Variable Analysis", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)792754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
802754fe60SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
81302affcbSDimitry Andric INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
822754fe60SDimitry Andric "Debug Variable Analysis", false, false)
832754fe60SDimitry Andric
842754fe60SDimitry Andric void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
852754fe60SDimitry Andric AU.addRequired<MachineDominatorTree>();
862754fe60SDimitry Andric AU.addRequiredTransitive<LiveIntervals>();
872754fe60SDimitry Andric AU.setPreservesAll();
882754fe60SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
892754fe60SDimitry Andric }
902754fe60SDimitry Andric
LiveDebugVariables()912cab237bSDimitry Andric LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) {
922754fe60SDimitry Andric initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
932754fe60SDimitry Andric }
942754fe60SDimitry Andric
952cab237bSDimitry Andric enum : unsigned { UndefLocNo = ~0U };
962cab237bSDimitry Andric
972cab237bSDimitry Andric /// Describes a location by number along with some flags about the original
982cab237bSDimitry Andric /// usage of the location.
992cab237bSDimitry Andric class DbgValueLocation {
1002cab237bSDimitry Andric public:
DbgValueLocation(unsigned LocNo,bool WasIndirect)1012cab237bSDimitry Andric DbgValueLocation(unsigned LocNo, bool WasIndirect)
1022cab237bSDimitry Andric : LocNo(LocNo), WasIndirect(WasIndirect) {
1032cab237bSDimitry Andric static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing");
1042cab237bSDimitry Andric assert(locNo() == LocNo && "location truncation");
1052cab237bSDimitry Andric }
1062cab237bSDimitry Andric
DbgValueLocation()1072cab237bSDimitry Andric DbgValueLocation() : LocNo(0), WasIndirect(0) {}
1082cab237bSDimitry Andric
locNo() const1092cab237bSDimitry Andric unsigned locNo() const {
1102cab237bSDimitry Andric // Fix up the undef location number, which gets truncated.
1112cab237bSDimitry Andric return LocNo == INT_MAX ? UndefLocNo : LocNo;
1122cab237bSDimitry Andric }
wasIndirect() const1132cab237bSDimitry Andric bool wasIndirect() const { return WasIndirect; }
isUndef() const1142cab237bSDimitry Andric bool isUndef() const { return locNo() == UndefLocNo; }
1152cab237bSDimitry Andric
changeLocNo(unsigned NewLocNo) const1162cab237bSDimitry Andric DbgValueLocation changeLocNo(unsigned NewLocNo) const {
1172cab237bSDimitry Andric return DbgValueLocation(NewLocNo, WasIndirect);
1182cab237bSDimitry Andric }
1192cab237bSDimitry Andric
operator ==(const DbgValueLocation & LHS,const DbgValueLocation & RHS)1202cab237bSDimitry Andric friend inline bool operator==(const DbgValueLocation &LHS,
1212cab237bSDimitry Andric const DbgValueLocation &RHS) {
1222cab237bSDimitry Andric return LHS.LocNo == RHS.LocNo && LHS.WasIndirect == RHS.WasIndirect;
1232cab237bSDimitry Andric }
1242cab237bSDimitry Andric
operator !=(const DbgValueLocation & LHS,const DbgValueLocation & RHS)1252cab237bSDimitry Andric friend inline bool operator!=(const DbgValueLocation &LHS,
1262cab237bSDimitry Andric const DbgValueLocation &RHS) {
1272cab237bSDimitry Andric return !(LHS == RHS);
1282cab237bSDimitry Andric }
1292cab237bSDimitry Andric
1302cab237bSDimitry Andric private:
1312cab237bSDimitry Andric unsigned LocNo : 31;
1322cab237bSDimitry Andric unsigned WasIndirect : 1;
1332cab237bSDimitry Andric };
1342cab237bSDimitry Andric
135*b5893f02SDimitry Andric /// Map of where a user value is live, and its location.
1362cab237bSDimitry Andric using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>;
1372cab237bSDimitry Andric
138*b5893f02SDimitry Andric /// Map of stack slot offsets for spilled locations.
139*b5893f02SDimitry Andric /// Non-spilled locations are not added to the map.
140*b5893f02SDimitry Andric using SpillOffsetMap = DenseMap<unsigned, unsigned>;
141*b5893f02SDimitry Andric
1422cab237bSDimitry Andric namespace {
1432cab237bSDimitry Andric
1442cab237bSDimitry Andric class LDVImpl;
1452754fe60SDimitry Andric
146*b5893f02SDimitry Andric /// A user value is a part of a debug info user variable.
1472754fe60SDimitry Andric ///
1482754fe60SDimitry Andric /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
1492754fe60SDimitry Andric /// holds part of a user variable. The part is identified by a byte offset.
1502754fe60SDimitry Andric ///
1512754fe60SDimitry Andric /// UserValues are grouped into equivalence classes for easier searching. Two
1522754fe60SDimitry Andric /// user values are related if they refer to the same variable, or if they are
1532754fe60SDimitry Andric /// held by the same virtual register. The equivalence class is the transitive
1542754fe60SDimitry Andric /// closure of that relation.
1552754fe60SDimitry Andric class UserValue {
1562cab237bSDimitry Andric const DILocalVariable *Variable; ///< The debug info variable we are part of.
1572cab237bSDimitry Andric const DIExpression *Expression; ///< Any complex address expression.
1582754fe60SDimitry Andric DebugLoc dl; ///< The debug location for the variable. This is
1592754fe60SDimitry Andric ///< used by dwarf writer to find lexical scope.
1602754fe60SDimitry Andric UserValue *leader; ///< Equivalence class leader.
1612cab237bSDimitry Andric UserValue *next = nullptr; ///< Next value in equivalence class, or null.
1622754fe60SDimitry Andric
1632754fe60SDimitry Andric /// Numbered locations referenced by locmap.
1642754fe60SDimitry Andric SmallVector<MachineOperand, 4> locations;
1652754fe60SDimitry Andric
1662754fe60SDimitry Andric /// Map of slot indices where this value is live.
1672754fe60SDimitry Andric LocMap locInts;
1682754fe60SDimitry Andric
1692cab237bSDimitry Andric /// Set of interval start indexes that have been trimmed to the
1702cab237bSDimitry Andric /// lexical scope.
1712cab237bSDimitry Andric SmallSet<SlotIndex, 2> trimmedDefs;
1722754fe60SDimitry Andric
173*b5893f02SDimitry Andric /// Insert a DBG_VALUE into MBB at Idx for LocNo.
1742cab237bSDimitry Andric void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
175*b5893f02SDimitry Andric SlotIndex StopIdx, DbgValueLocation Loc, bool Spilled,
176*b5893f02SDimitry Andric unsigned SpillOffset, LiveIntervals &LIS,
1772cab237bSDimitry Andric const TargetInstrInfo &TII,
1782cab237bSDimitry Andric const TargetRegisterInfo &TRI);
1792754fe60SDimitry Andric
180*b5893f02SDimitry Andric /// Replace OldLocNo ranges with NewRegs ranges where NewRegs
181bd5abe19SDimitry Andric /// is live. Returns true if any changes were made.
182f785676fSDimitry Andric bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
183f785676fSDimitry Andric LiveIntervals &LIS);
184bd5abe19SDimitry Andric
1852754fe60SDimitry Andric public:
186*b5893f02SDimitry Andric /// Create a new UserValue.
UserValue(const DILocalVariable * var,const DIExpression * expr,DebugLoc L,LocMap::Allocator & alloc)1872cab237bSDimitry Andric UserValue(const DILocalVariable *var, const DIExpression *expr, DebugLoc L,
1882cab237bSDimitry Andric LocMap::Allocator &alloc)
1892cab237bSDimitry Andric : Variable(var), Expression(expr), dl(std::move(L)), leader(this),
1902cab237bSDimitry Andric locInts(alloc) {}
1912754fe60SDimitry Andric
192*b5893f02SDimitry Andric /// Get the leader of this value's equivalence class.
getLeader()1932754fe60SDimitry Andric UserValue *getLeader() {
1942754fe60SDimitry Andric UserValue *l = leader;
1952754fe60SDimitry Andric while (l != l->leader)
1962754fe60SDimitry Andric l = l->leader;
1972754fe60SDimitry Andric return leader = l;
1982754fe60SDimitry Andric }
1992754fe60SDimitry Andric
200*b5893f02SDimitry Andric /// Return the next UserValue in the equivalence class.
getNext() const2012754fe60SDimitry Andric UserValue *getNext() const { return next; }
2022754fe60SDimitry Andric
203*b5893f02SDimitry Andric /// Does this UserValue match the parameters?
match(const DILocalVariable * Var,const DIExpression * Expr,const DILocation * IA) const2042cab237bSDimitry Andric bool match(const DILocalVariable *Var, const DIExpression *Expr,
2052cab237bSDimitry Andric const DILocation *IA) const {
2062cab237bSDimitry Andric // FIXME: The fragment should be part of the equivalence class, but not
2072cab237bSDimitry Andric // other things in the expression like stack values.
2082cab237bSDimitry Andric return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA;
2092754fe60SDimitry Andric }
2102754fe60SDimitry Andric
211*b5893f02SDimitry Andric /// Merge equivalence classes.
merge(UserValue * L1,UserValue * L2)2122754fe60SDimitry Andric static UserValue *merge(UserValue *L1, UserValue *L2) {
2132754fe60SDimitry Andric L2 = L2->getLeader();
2142754fe60SDimitry Andric if (!L1)
2152754fe60SDimitry Andric return L2;
2162754fe60SDimitry Andric L1 = L1->getLeader();
2172754fe60SDimitry Andric if (L1 == L2)
2182754fe60SDimitry Andric return L1;
2192754fe60SDimitry Andric // Splice L2 before L1's members.
2202754fe60SDimitry Andric UserValue *End = L2;
2213ca95b02SDimitry Andric while (End->next) {
2223ca95b02SDimitry Andric End->leader = L1;
2233ca95b02SDimitry Andric End = End->next;
2243ca95b02SDimitry Andric }
2252754fe60SDimitry Andric End->leader = L1;
2262754fe60SDimitry Andric End->next = L1->next;
2272754fe60SDimitry Andric L1->next = L2;
2282754fe60SDimitry Andric return L1;
2292754fe60SDimitry Andric }
2302754fe60SDimitry Andric
2314ba319b5SDimitry Andric /// Return the location number that matches Loc.
2324ba319b5SDimitry Andric ///
2334ba319b5SDimitry Andric /// For undef values we always return location number UndefLocNo without
2344ba319b5SDimitry Andric /// inserting anything in locations. Since locations is a vector and the
2354ba319b5SDimitry Andric /// location number is the position in the vector and UndefLocNo is ~0,
2364ba319b5SDimitry Andric /// we would need a very big vector to put the value at the right position.
getLocationNo(const MachineOperand & LocMO)2372754fe60SDimitry Andric unsigned getLocationNo(const MachineOperand &LocMO) {
2383b0f4066SDimitry Andric if (LocMO.isReg()) {
2393b0f4066SDimitry Andric if (LocMO.getReg() == 0)
2402cab237bSDimitry Andric return UndefLocNo;
2413b0f4066SDimitry Andric // For register locations we dont care about use/def and other flags.
2423b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i)
2433b0f4066SDimitry Andric if (locations[i].isReg() &&
2443b0f4066SDimitry Andric locations[i].getReg() == LocMO.getReg() &&
2453b0f4066SDimitry Andric locations[i].getSubReg() == LocMO.getSubReg())
2463b0f4066SDimitry Andric return i;
2473b0f4066SDimitry Andric } else
2482754fe60SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i)
2492754fe60SDimitry Andric if (LocMO.isIdenticalTo(locations[i]))
2502754fe60SDimitry Andric return i;
2512754fe60SDimitry Andric locations.push_back(LocMO);
2522754fe60SDimitry Andric // We are storing a MachineOperand outside a MachineInstr.
2532754fe60SDimitry Andric locations.back().clearParent();
2543b0f4066SDimitry Andric // Don't store def operands.
25530785c0eSDimitry Andric if (locations.back().isReg()) {
25630785c0eSDimitry Andric if (locations.back().isDef())
25730785c0eSDimitry Andric locations.back().setIsDead(false);
2583b0f4066SDimitry Andric locations.back().setIsUse();
25930785c0eSDimitry Andric }
2602754fe60SDimitry Andric return locations.size() - 1;
2612754fe60SDimitry Andric }
2622754fe60SDimitry Andric
263*b5893f02SDimitry Andric /// Ensure that all virtual register locations are mapped.
2643b0f4066SDimitry Andric void mapVirtRegs(LDVImpl *LDV);
2653b0f4066SDimitry Andric
266*b5893f02SDimitry Andric /// Add a definition point to this value.
addDef(SlotIndex Idx,const MachineOperand & LocMO,bool IsIndirect)2672cab237bSDimitry Andric void addDef(SlotIndex Idx, const MachineOperand &LocMO, bool IsIndirect) {
2682cab237bSDimitry Andric DbgValueLocation Loc(getLocationNo(LocMO), IsIndirect);
2692754fe60SDimitry Andric // Add a singular (Idx,Idx) -> Loc mapping.
2702754fe60SDimitry Andric LocMap::iterator I = locInts.find(Idx);
2712754fe60SDimitry Andric if (!I.valid() || I.start() != Idx)
2722cab237bSDimitry Andric I.insert(Idx, Idx.getNextSlot(), Loc);
2736122f3e6SDimitry Andric else
2746122f3e6SDimitry Andric // A later DBG_VALUE at the same SlotIndex overrides the old location.
2752cab237bSDimitry Andric I.setValue(Loc);
2762754fe60SDimitry Andric }
2772754fe60SDimitry Andric
278*b5893f02SDimitry Andric /// Extend the current definition as far as possible down.
279*b5893f02SDimitry Andric ///
280d88c1a5aSDimitry Andric /// Stop when meeting an existing def or when leaving the live
281*b5893f02SDimitry Andric /// range of VNI. End points where VNI is no longer live are added to Kills.
282*b5893f02SDimitry Andric ///
283*b5893f02SDimitry Andric /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
284*b5893f02SDimitry Andric /// data-flow analysis to propagate them beyond basic block boundaries.
285*b5893f02SDimitry Andric ///
286*b5893f02SDimitry Andric /// \param Idx Starting point for the definition.
287*b5893f02SDimitry Andric /// \param Loc Location number to propagate.
288*b5893f02SDimitry Andric /// \param LR Restrict liveness to where LR has the value VNI. May be null.
289*b5893f02SDimitry Andric /// \param VNI When LR is not null, this is the value to restrict to.
290*b5893f02SDimitry Andric /// \param [out] Kills Append end points of VNI's live range to Kills.
291*b5893f02SDimitry Andric /// \param LIS Live intervals analysis.
2922cab237bSDimitry Andric void extendDef(SlotIndex Idx, DbgValueLocation Loc,
293f785676fSDimitry Andric LiveRange *LR, const VNInfo *VNI,
2943b0f4066SDimitry Andric SmallVectorImpl<SlotIndex> *Kills,
295d88c1a5aSDimitry Andric LiveIntervals &LIS);
2962754fe60SDimitry Andric
297*b5893f02SDimitry Andric /// The value in LI/LocNo may be copies to other registers. Determine if
298*b5893f02SDimitry Andric /// any of the copies are available at the kill points, and add defs if
299*b5893f02SDimitry Andric /// possible.
300*b5893f02SDimitry Andric ///
301*b5893f02SDimitry Andric /// \param LI Scan for copies of the value in LI->reg.
302*b5893f02SDimitry Andric /// \param LocNo Location number of LI->reg.
303*b5893f02SDimitry Andric /// \param WasIndirect Indicates if the original use of LI->reg was indirect
304*b5893f02SDimitry Andric /// \param Kills Points where the range of LocNo could be extended.
305*b5893f02SDimitry Andric /// \param [in,out] NewDefs Append (Idx, LocNo) of inserted defs here.
3062cab237bSDimitry Andric void addDefsFromCopies(
3072cab237bSDimitry Andric LiveInterval *LI, unsigned LocNo, bool WasIndirect,
3083b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills,
3092cab237bSDimitry Andric SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
3102cab237bSDimitry Andric MachineRegisterInfo &MRI, LiveIntervals &LIS);
3113b0f4066SDimitry Andric
312*b5893f02SDimitry Andric /// Compute the live intervals of all locations after collecting all their
313*b5893f02SDimitry Andric /// def points.
3147ae0e2c9SDimitry Andric void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
3152cab237bSDimitry Andric LiveIntervals &LIS, LexicalScopes &LS);
3162754fe60SDimitry Andric
317*b5893f02SDimitry Andric /// Replace OldReg ranges with NewRegs ranges where NewRegs is
318bd5abe19SDimitry Andric /// live. Returns true if any changes were made.
3194ba319b5SDimitry Andric bool splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
320f785676fSDimitry Andric LiveIntervals &LIS);
321bd5abe19SDimitry Andric
322*b5893f02SDimitry Andric /// Rewrite virtual register locations according to the provided virtual
323*b5893f02SDimitry Andric /// register map. Record the stack slot offsets for the locations that
324*b5893f02SDimitry Andric /// were spilled.
325*b5893f02SDimitry Andric void rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
326*b5893f02SDimitry Andric const TargetInstrInfo &TII,
327*b5893f02SDimitry Andric const TargetRegisterInfo &TRI,
328*b5893f02SDimitry Andric SpillOffsetMap &SpillOffsets);
3292754fe60SDimitry Andric
330*b5893f02SDimitry Andric /// Recreate DBG_VALUE instruction from data structures.
3312cab237bSDimitry Andric void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
3322cab237bSDimitry Andric const TargetInstrInfo &TII,
3332cab237bSDimitry Andric const TargetRegisterInfo &TRI,
334*b5893f02SDimitry Andric const SpillOffsetMap &SpillOffsets);
3352754fe60SDimitry Andric
336*b5893f02SDimitry Andric /// Return DebugLoc of this UserValue.
getDebugLoc()3376122f3e6SDimitry Andric DebugLoc getDebugLoc() { return dl;}
3382cab237bSDimitry Andric
339ff0cc061SDimitry Andric void print(raw_ostream &, const TargetRegisterInfo *);
3402754fe60SDimitry Andric };
3412754fe60SDimitry Andric
342*b5893f02SDimitry Andric /// Implementation of the LiveDebugVariables pass.
3432754fe60SDimitry Andric class LDVImpl {
3442754fe60SDimitry Andric LiveDebugVariables &pass;
3452754fe60SDimitry Andric LocMap::Allocator allocator;
3462cab237bSDimitry Andric MachineFunction *MF = nullptr;
3472754fe60SDimitry Andric LiveIntervals *LIS;
3482754fe60SDimitry Andric const TargetRegisterInfo *TRI;
3492754fe60SDimitry Andric
350139f7f9bSDimitry Andric /// Whether emitDebugValues is called.
3512cab237bSDimitry Andric bool EmitDone = false;
3522cab237bSDimitry Andric
353139f7f9bSDimitry Andric /// Whether the machine function is modified during the pass.
3542cab237bSDimitry Andric bool ModifiedMF = false;
355139f7f9bSDimitry Andric
356*b5893f02SDimitry Andric /// All allocated UserValue instances.
35791bc56edSDimitry Andric SmallVector<std::unique_ptr<UserValue>, 8> userValues;
3582754fe60SDimitry Andric
3592754fe60SDimitry Andric /// Map virtual register to eq class leader.
3602cab237bSDimitry Andric using VRMap = DenseMap<unsigned, UserValue *>;
3612754fe60SDimitry Andric VRMap virtRegToEqClass;
3622754fe60SDimitry Andric
3632754fe60SDimitry Andric /// Map user variable to eq class leader.
3642cab237bSDimitry Andric using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
3652754fe60SDimitry Andric UVMap userVarMap;
3662754fe60SDimitry Andric
367*b5893f02SDimitry Andric /// Find or create a UserValue.
3682cab237bSDimitry Andric UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
3692cab237bSDimitry Andric const DebugLoc &DL);
3702754fe60SDimitry Andric
371*b5893f02SDimitry Andric /// Find the EC leader for VirtReg or null.
3722754fe60SDimitry Andric UserValue *lookupVirtReg(unsigned VirtReg);
3732754fe60SDimitry Andric
374*b5893f02SDimitry Andric /// Add DBG_VALUE instruction to our maps.
375*b5893f02SDimitry Andric ///
376*b5893f02SDimitry Andric /// \param MI DBG_VALUE instruction
377*b5893f02SDimitry Andric /// \param Idx Last valid SLotIndex before instruction.
378*b5893f02SDimitry Andric ///
379*b5893f02SDimitry Andric /// \returns True if the DBG_VALUE instruction should be deleted.
3803ca95b02SDimitry Andric bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
3812754fe60SDimitry Andric
382*b5893f02SDimitry Andric /// Collect and erase all DBG_VALUE instructions, adding a UserValue def
383*b5893f02SDimitry Andric /// for each instruction.
384*b5893f02SDimitry Andric ///
385*b5893f02SDimitry Andric /// \param mf MachineFunction to be scanned.
386*b5893f02SDimitry Andric ///
387*b5893f02SDimitry Andric /// \returns True if any debug values were found.
3882754fe60SDimitry Andric bool collectDebugValues(MachineFunction &mf);
3892754fe60SDimitry Andric
390*b5893f02SDimitry Andric /// Compute the live intervals of all user values after collecting all
391*b5893f02SDimitry Andric /// their def points.
3922754fe60SDimitry Andric void computeIntervals();
3932754fe60SDimitry Andric
3942754fe60SDimitry Andric public:
LDVImpl(LiveDebugVariables * ps)3952cab237bSDimitry Andric LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
3962cab237bSDimitry Andric
3972754fe60SDimitry Andric bool runOnMachineFunction(MachineFunction &mf);
3982754fe60SDimitry Andric
399*b5893f02SDimitry Andric /// Release all memory.
clear()4002754fe60SDimitry Andric void clear() {
40139d628a0SDimitry Andric MF = nullptr;
4022754fe60SDimitry Andric userValues.clear();
4032754fe60SDimitry Andric virtRegToEqClass.clear();
4042754fe60SDimitry Andric userVarMap.clear();
405139f7f9bSDimitry Andric // Make sure we call emitDebugValues if the machine function was modified.
406139f7f9bSDimitry Andric assert((!ModifiedMF || EmitDone) &&
407139f7f9bSDimitry Andric "Dbg values are not emitted in LDV");
408139f7f9bSDimitry Andric EmitDone = false;
409139f7f9bSDimitry Andric ModifiedMF = false;
4102754fe60SDimitry Andric }
4112754fe60SDimitry Andric
412*b5893f02SDimitry Andric /// Map virtual register to an equivalence class.
4133b0f4066SDimitry Andric void mapVirtReg(unsigned VirtReg, UserValue *EC);
4143b0f4066SDimitry Andric
415*b5893f02SDimitry Andric /// Replace all references to OldReg with NewRegs.
416f785676fSDimitry Andric void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
417bd5abe19SDimitry Andric
418*b5893f02SDimitry Andric /// Recreate DBG_VALUE instruction from data structures.
4192754fe60SDimitry Andric void emitDebugValues(VirtRegMap *VRM);
4202754fe60SDimitry Andric
4212754fe60SDimitry Andric void print(raw_ostream&);
4222754fe60SDimitry Andric };
4232754fe60SDimitry Andric
4242cab237bSDimitry Andric } // end anonymous namespace
4252cab237bSDimitry Andric
4262cab237bSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
printDebugLoc(const DebugLoc & DL,raw_ostream & CommentOS,const LLVMContext & Ctx)4273ca95b02SDimitry Andric static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
428ff0cc061SDimitry Andric const LLVMContext &Ctx) {
429ff0cc061SDimitry Andric if (!DL)
430ff0cc061SDimitry Andric return;
431ff0cc061SDimitry Andric
432ff0cc061SDimitry Andric auto *Scope = cast<DIScope>(DL.getScope());
433ff0cc061SDimitry Andric // Omit the directory, because it's likely to be long and uninteresting.
434ff0cc061SDimitry Andric CommentOS << Scope->getFilename();
435ff0cc061SDimitry Andric CommentOS << ':' << DL.getLine();
436ff0cc061SDimitry Andric if (DL.getCol() != 0)
437ff0cc061SDimitry Andric CommentOS << ':' << DL.getCol();
438ff0cc061SDimitry Andric
439ff0cc061SDimitry Andric DebugLoc InlinedAtDL = DL.getInlinedAt();
440ff0cc061SDimitry Andric if (!InlinedAtDL)
441ff0cc061SDimitry Andric return;
442ff0cc061SDimitry Andric
443ff0cc061SDimitry Andric CommentOS << " @[ ";
444ff0cc061SDimitry Andric printDebugLoc(InlinedAtDL, CommentOS, Ctx);
445ff0cc061SDimitry Andric CommentOS << " ]";
446ff0cc061SDimitry Andric }
447ff0cc061SDimitry Andric
printExtendedName(raw_ostream & OS,const DILocalVariable * V,const DILocation * DL)448ff0cc061SDimitry Andric static void printExtendedName(raw_ostream &OS, const DILocalVariable *V,
449ff0cc061SDimitry Andric const DILocation *DL) {
450ff0cc061SDimitry Andric const LLVMContext &Ctx = V->getContext();
451ff0cc061SDimitry Andric StringRef Res = V->getName();
452ff0cc061SDimitry Andric if (!Res.empty())
453ff0cc061SDimitry Andric OS << Res << "," << V->getLine();
454ff0cc061SDimitry Andric if (auto *InlinedAt = DL->getInlinedAt()) {
455ff0cc061SDimitry Andric if (DebugLoc InlinedAtDL = InlinedAt) {
456ff0cc061SDimitry Andric OS << " @[";
457ff0cc061SDimitry Andric printDebugLoc(InlinedAtDL, OS, Ctx);
458ff0cc061SDimitry Andric OS << "]";
459ff0cc061SDimitry Andric }
460ff0cc061SDimitry Andric }
461ff0cc061SDimitry Andric }
462ff0cc061SDimitry Andric
print(raw_ostream & OS,const TargetRegisterInfo * TRI)463ff0cc061SDimitry Andric void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
464ff0cc061SDimitry Andric auto *DV = cast<DILocalVariable>(Variable);
4656122f3e6SDimitry Andric OS << "!\"";
466ff0cc061SDimitry Andric printExtendedName(OS, DV, dl);
467ff0cc061SDimitry Andric
4686122f3e6SDimitry Andric OS << "\"\t";
4692754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
4702754fe60SDimitry Andric OS << " [" << I.start() << ';' << I.stop() << "):";
4712cab237bSDimitry Andric if (I.value().isUndef())
4722754fe60SDimitry Andric OS << "undef";
4732cab237bSDimitry Andric else {
4742cab237bSDimitry Andric OS << I.value().locNo();
4752cab237bSDimitry Andric if (I.value().wasIndirect())
4762cab237bSDimitry Andric OS << " ind";
4772cab237bSDimitry Andric }
4782754fe60SDimitry Andric }
479bd5abe19SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) {
480bd5abe19SDimitry Andric OS << " Loc" << i << '=';
481ff0cc061SDimitry Andric locations[i].print(OS, TRI);
482bd5abe19SDimitry Andric }
4832754fe60SDimitry Andric OS << '\n';
4842754fe60SDimitry Andric }
4852754fe60SDimitry Andric
print(raw_ostream & OS)4862754fe60SDimitry Andric void LDVImpl::print(raw_ostream &OS) {
4872754fe60SDimitry Andric OS << "********** DEBUG VARIABLES **********\n";
4882754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i)
489ff0cc061SDimitry Andric userValues[i]->print(OS, TRI);
4902754fe60SDimitry Andric }
4912cab237bSDimitry Andric #endif
4922754fe60SDimitry Andric
mapVirtRegs(LDVImpl * LDV)4933b0f4066SDimitry Andric void UserValue::mapVirtRegs(LDVImpl *LDV) {
4943b0f4066SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i)
4953b0f4066SDimitry Andric if (locations[i].isReg() &&
4963b0f4066SDimitry Andric TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
4973b0f4066SDimitry Andric LDV->mapVirtReg(locations[i].getReg(), this);
4983b0f4066SDimitry Andric }
4993b0f4066SDimitry Andric
getUserValue(const DILocalVariable * Var,const DIExpression * Expr,const DebugLoc & DL)5002cab237bSDimitry Andric UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
5012cab237bSDimitry Andric const DIExpression *Expr, const DebugLoc &DL) {
5022754fe60SDimitry Andric UserValue *&Leader = userVarMap[Var];
5032754fe60SDimitry Andric if (Leader) {
5042754fe60SDimitry Andric UserValue *UV = Leader->getLeader();
5052754fe60SDimitry Andric Leader = UV;
5062754fe60SDimitry Andric for (; UV; UV = UV->getNext())
5072cab237bSDimitry Andric if (UV->match(Var, Expr, DL->getInlinedAt()))
5082754fe60SDimitry Andric return UV;
5092754fe60SDimitry Andric }
5102754fe60SDimitry Andric
51191bc56edSDimitry Andric userValues.push_back(
5122cab237bSDimitry Andric llvm::make_unique<UserValue>(Var, Expr, DL, allocator));
51391bc56edSDimitry Andric UserValue *UV = userValues.back().get();
5142754fe60SDimitry Andric Leader = UserValue::merge(Leader, UV);
5152754fe60SDimitry Andric return UV;
5162754fe60SDimitry Andric }
5172754fe60SDimitry Andric
mapVirtReg(unsigned VirtReg,UserValue * EC)5182754fe60SDimitry Andric void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
5192754fe60SDimitry Andric assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
5202754fe60SDimitry Andric UserValue *&Leader = virtRegToEqClass[VirtReg];
5212754fe60SDimitry Andric Leader = UserValue::merge(Leader, EC);
5222754fe60SDimitry Andric }
5232754fe60SDimitry Andric
lookupVirtReg(unsigned VirtReg)5242754fe60SDimitry Andric UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
5252754fe60SDimitry Andric if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
5262754fe60SDimitry Andric return UV->getLeader();
52791bc56edSDimitry Andric return nullptr;
5282754fe60SDimitry Andric }
5292754fe60SDimitry Andric
handleDebugValue(MachineInstr & MI,SlotIndex Idx)5303ca95b02SDimitry Andric bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
5312754fe60SDimitry Andric // DBG_VALUE loc, offset, variable
5323ca95b02SDimitry Andric if (MI.getNumOperands() != 4 ||
5333ca95b02SDimitry Andric !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
5343ca95b02SDimitry Andric !MI.getOperand(2).isMetadata()) {
5354ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Can't handle " << MI);
5362754fe60SDimitry Andric return false;
5372754fe60SDimitry Andric }
5382754fe60SDimitry Andric
5396ccc06f6SDimitry Andric // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual
5406ccc06f6SDimitry Andric // register that hasn't been defined yet. If we do not remove those here, then
5416ccc06f6SDimitry Andric // the re-insertion of the DBG_VALUE instruction after register allocation
5426ccc06f6SDimitry Andric // will be incorrect.
5436ccc06f6SDimitry Andric // TODO: If earlier passes are corrected to generate sane debug information
5446ccc06f6SDimitry Andric // (and if the machine verifier is improved to catch this), then these checks
5456ccc06f6SDimitry Andric // could be removed or replaced by asserts.
5466ccc06f6SDimitry Andric bool Discard = false;
5476ccc06f6SDimitry Andric if (MI.getOperand(0).isReg() &&
5486ccc06f6SDimitry Andric TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg())) {
5496ccc06f6SDimitry Andric const unsigned Reg = MI.getOperand(0).getReg();
5506ccc06f6SDimitry Andric if (!LIS->hasInterval(Reg)) {
5516ccc06f6SDimitry Andric // The DBG_VALUE is described by a virtual register that does not have a
5526ccc06f6SDimitry Andric // live interval. Discard the DBG_VALUE.
5536ccc06f6SDimitry Andric Discard = true;
5544ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx
5554ba319b5SDimitry Andric << " " << MI);
5566ccc06f6SDimitry Andric } else {
5576ccc06f6SDimitry Andric // The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg
5586ccc06f6SDimitry Andric // is defined dead at Idx (where Idx is the slot index for the instruction
5596ccc06f6SDimitry Andric // preceeding the DBG_VALUE).
5606ccc06f6SDimitry Andric const LiveInterval &LI = LIS->getInterval(Reg);
5616ccc06f6SDimitry Andric LiveQueryResult LRQ = LI.Query(Idx);
5626ccc06f6SDimitry Andric if (!LRQ.valueOutOrDead()) {
5636ccc06f6SDimitry Andric // We have found a DBG_VALUE with the value in a virtual register that
5646ccc06f6SDimitry Andric // is not live. Discard the DBG_VALUE.
5656ccc06f6SDimitry Andric Discard = true;
5664ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx
5674ba319b5SDimitry Andric << " " << MI);
5686ccc06f6SDimitry Andric }
5696ccc06f6SDimitry Andric }
5706ccc06f6SDimitry Andric }
5716ccc06f6SDimitry Andric
5722cab237bSDimitry Andric // Get or create the UserValue for (variable,offset) here.
5732cab237bSDimitry Andric bool IsIndirect = MI.getOperand(1).isImm();
5742cab237bSDimitry Andric if (IsIndirect)
5752cab237bSDimitry Andric assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
5762cab237bSDimitry Andric const DILocalVariable *Var = MI.getDebugVariable();
5772cab237bSDimitry Andric const DIExpression *Expr = MI.getDebugExpression();
5782cab237bSDimitry Andric UserValue *UV =
5792cab237bSDimitry Andric getUserValue(Var, Expr, MI.getDebugLoc());
5806ccc06f6SDimitry Andric if (!Discard)
5812cab237bSDimitry Andric UV->addDef(Idx, MI.getOperand(0), IsIndirect);
5826ccc06f6SDimitry Andric else {
5836ccc06f6SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(0U, false);
5846ccc06f6SDimitry Andric MO.setIsDebug();
5856ccc06f6SDimitry Andric UV->addDef(Idx, MO, false);
5866ccc06f6SDimitry Andric }
5872754fe60SDimitry Andric return true;
5882754fe60SDimitry Andric }
5892754fe60SDimitry Andric
collectDebugValues(MachineFunction & mf)5902754fe60SDimitry Andric bool LDVImpl::collectDebugValues(MachineFunction &mf) {
5912754fe60SDimitry Andric bool Changed = false;
5922754fe60SDimitry Andric for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
5932754fe60SDimitry Andric ++MFI) {
5947d523365SDimitry Andric MachineBasicBlock *MBB = &*MFI;
5952754fe60SDimitry Andric for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
5962754fe60SDimitry Andric MBBI != MBBE;) {
597*b5893f02SDimitry Andric // Use the first debug instruction in the sequence to get a SlotIndex
598*b5893f02SDimitry Andric // for following consecutive debug instructions.
599*b5893f02SDimitry Andric if (!MBBI->isDebugInstr()) {
6002754fe60SDimitry Andric ++MBBI;
6012754fe60SDimitry Andric continue;
6022754fe60SDimitry Andric }
603*b5893f02SDimitry Andric // Debug instructions has no slot index. Use the previous
604*b5893f02SDimitry Andric // non-debug instruction's SlotIndex as its SlotIndex.
6053ca95b02SDimitry Andric SlotIndex Idx =
6063ca95b02SDimitry Andric MBBI == MBB->begin()
6073ca95b02SDimitry Andric ? LIS->getMBBStartIdx(MBB)
6083ca95b02SDimitry Andric : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
609*b5893f02SDimitry Andric // Handle consecutive debug instructions with the same slot index.
6102754fe60SDimitry Andric do {
611*b5893f02SDimitry Andric // Only handle DBG_VALUE in handleDebugValue(). Skip all other
612*b5893f02SDimitry Andric // kinds of debug instructions.
613*b5893f02SDimitry Andric if (MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) {
6142754fe60SDimitry Andric MBBI = MBB->erase(MBBI);
6152754fe60SDimitry Andric Changed = true;
6162754fe60SDimitry Andric } else
6172754fe60SDimitry Andric ++MBBI;
618*b5893f02SDimitry Andric } while (MBBI != MBBE && MBBI->isDebugInstr());
6192754fe60SDimitry Andric }
6202754fe60SDimitry Andric }
6212754fe60SDimitry Andric return Changed;
6222754fe60SDimitry Andric }
6232754fe60SDimitry Andric
extendDef(SlotIndex Idx,DbgValueLocation Loc,LiveRange * LR,const VNInfo * VNI,SmallVectorImpl<SlotIndex> * Kills,LiveIntervals & LIS)6242cab237bSDimitry Andric void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR,
6257d523365SDimitry Andric const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
626d88c1a5aSDimitry Andric LiveIntervals &LIS) {
6277d523365SDimitry Andric SlotIndex Start = Idx;
6282754fe60SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
6292754fe60SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(MBB);
6302754fe60SDimitry Andric LocMap::iterator I = locInts.find(Start);
6312754fe60SDimitry Andric
6322754fe60SDimitry Andric // Limit to VNI's live range.
6332754fe60SDimitry Andric bool ToEnd = true;
634f785676fSDimitry Andric if (LR && VNI) {
635f785676fSDimitry Andric LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
636f785676fSDimitry Andric if (!Segment || Segment->valno != VNI) {
6373b0f4066SDimitry Andric if (Kills)
6383b0f4066SDimitry Andric Kills->push_back(Start);
6397d523365SDimitry Andric return;
6403b0f4066SDimitry Andric }
6413ca95b02SDimitry Andric if (Segment->end < Stop) {
6423ca95b02SDimitry Andric Stop = Segment->end;
6433ca95b02SDimitry Andric ToEnd = false;
6443ca95b02SDimitry Andric }
6452754fe60SDimitry Andric }
6462754fe60SDimitry Andric
6472754fe60SDimitry Andric // There could already be a short def at Start.
6482754fe60SDimitry Andric if (I.valid() && I.start() <= Start) {
6492754fe60SDimitry Andric // Stop when meeting a different location or an already extended interval.
6502754fe60SDimitry Andric Start = Start.getNextSlot();
6512cab237bSDimitry Andric if (I.value() != Loc || I.stop() != Start)
6527d523365SDimitry Andric return;
6532754fe60SDimitry Andric // This is a one-slot placeholder. Just skip it.
6542754fe60SDimitry Andric ++I;
6552754fe60SDimitry Andric }
6562754fe60SDimitry Andric
6572754fe60SDimitry Andric // Limited by the next def.
6583ca95b02SDimitry Andric if (I.valid() && I.start() < Stop) {
6593ca95b02SDimitry Andric Stop = I.start();
6603ca95b02SDimitry Andric ToEnd = false;
6613ca95b02SDimitry Andric }
6623b0f4066SDimitry Andric // Limited by VNI's live range.
6633b0f4066SDimitry Andric else if (!ToEnd && Kills)
6643b0f4066SDimitry Andric Kills->push_back(Stop);
6652754fe60SDimitry Andric
6667d523365SDimitry Andric if (Start < Stop)
6672cab237bSDimitry Andric I.insert(Start, Stop, Loc);
6682754fe60SDimitry Andric }
6692754fe60SDimitry Andric
addDefsFromCopies(LiveInterval * LI,unsigned LocNo,bool WasIndirect,const SmallVectorImpl<SlotIndex> & Kills,SmallVectorImpl<std::pair<SlotIndex,DbgValueLocation>> & NewDefs,MachineRegisterInfo & MRI,LiveIntervals & LIS)6702cab237bSDimitry Andric void UserValue::addDefsFromCopies(
6712cab237bSDimitry Andric LiveInterval *LI, unsigned LocNo, bool WasIndirect,
6723b0f4066SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills,
6732cab237bSDimitry Andric SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
6743b0f4066SDimitry Andric MachineRegisterInfo &MRI, LiveIntervals &LIS) {
6753b0f4066SDimitry Andric if (Kills.empty())
6763b0f4066SDimitry Andric return;
6773b0f4066SDimitry Andric // Don't track copies from physregs, there are too many uses.
6783b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
6793b0f4066SDimitry Andric return;
6803b0f4066SDimitry Andric
6813b0f4066SDimitry Andric // Collect all the (vreg, valno) pairs that are copies of LI.
6823b0f4066SDimitry Andric SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
68391bc56edSDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
68491bc56edSDimitry Andric MachineInstr *MI = MO.getParent();
6853b0f4066SDimitry Andric // Copies of the full value.
68691bc56edSDimitry Andric if (MO.getSubReg() || !MI->isCopy())
6873b0f4066SDimitry Andric continue;
6883b0f4066SDimitry Andric unsigned DstReg = MI->getOperand(0).getReg();
6893b0f4066SDimitry Andric
6903b0f4066SDimitry Andric // Don't follow copies to physregs. These are usually setting up call
6913b0f4066SDimitry Andric // arguments, and the argument registers are always call clobbered. We are
6923b0f4066SDimitry Andric // better off in the source register which could be a callee-saved register,
6933b0f4066SDimitry Andric // or it could be spilled.
6943b0f4066SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(DstReg))
6953b0f4066SDimitry Andric continue;
6963b0f4066SDimitry Andric
6973b0f4066SDimitry Andric // Is LocNo extended to reach this copy? If not, another def may be blocking
6983b0f4066SDimitry Andric // it, or we are looking at a wrong value of LI.
6993ca95b02SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI);
700dff0c46cSDimitry Andric LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
7012cab237bSDimitry Andric if (!I.valid() || I.value().locNo() != LocNo)
7023b0f4066SDimitry Andric continue;
7033b0f4066SDimitry Andric
7043b0f4066SDimitry Andric if (!LIS.hasInterval(DstReg))
7053b0f4066SDimitry Andric continue;
7063b0f4066SDimitry Andric LiveInterval *DstLI = &LIS.getInterval(DstReg);
707dff0c46cSDimitry Andric const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
708dff0c46cSDimitry Andric assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
7093b0f4066SDimitry Andric CopyValues.push_back(std::make_pair(DstLI, DstVNI));
7103b0f4066SDimitry Andric }
7113b0f4066SDimitry Andric
7123b0f4066SDimitry Andric if (CopyValues.empty())
7133b0f4066SDimitry Andric return;
7143b0f4066SDimitry Andric
7154ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI
7164ba319b5SDimitry Andric << '\n');
7173b0f4066SDimitry Andric
7183b0f4066SDimitry Andric // Try to add defs of the copied values for each kill point.
7193b0f4066SDimitry Andric for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
7203b0f4066SDimitry Andric SlotIndex Idx = Kills[i];
7213b0f4066SDimitry Andric for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
7223b0f4066SDimitry Andric LiveInterval *DstLI = CopyValues[j].first;
7233b0f4066SDimitry Andric const VNInfo *DstVNI = CopyValues[j].second;
7243b0f4066SDimitry Andric if (DstLI->getVNInfoAt(Idx) != DstVNI)
7253b0f4066SDimitry Andric continue;
7263b0f4066SDimitry Andric // Check that there isn't already a def at Idx
7273b0f4066SDimitry Andric LocMap::iterator I = locInts.find(Idx);
7283b0f4066SDimitry Andric if (I.valid() && I.start() <= Idx)
7293b0f4066SDimitry Andric continue;
7304ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
7313b0f4066SDimitry Andric << DstVNI->id << " in " << *DstLI << '\n');
7323b0f4066SDimitry Andric MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
7333b0f4066SDimitry Andric assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
7343b0f4066SDimitry Andric unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
7352cab237bSDimitry Andric DbgValueLocation NewLoc(LocNo, WasIndirect);
7362cab237bSDimitry Andric I.insert(Idx, Idx.getNextSlot(), NewLoc);
7372cab237bSDimitry Andric NewDefs.push_back(std::make_pair(Idx, NewLoc));
7383b0f4066SDimitry Andric break;
7393b0f4066SDimitry Andric }
7403b0f4066SDimitry Andric }
7413b0f4066SDimitry Andric }
7423b0f4066SDimitry Andric
computeIntervals(MachineRegisterInfo & MRI,const TargetRegisterInfo & TRI,LiveIntervals & LIS,LexicalScopes & LS)7432cab237bSDimitry Andric void UserValue::computeIntervals(MachineRegisterInfo &MRI,
7447ae0e2c9SDimitry Andric const TargetRegisterInfo &TRI,
7452cab237bSDimitry Andric LiveIntervals &LIS, LexicalScopes &LS) {
7462cab237bSDimitry Andric SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs;
7472754fe60SDimitry Andric
7482754fe60SDimitry Andric // Collect all defs to be extended (Skipping undefs).
7492754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
7502cab237bSDimitry Andric if (!I.value().isUndef())
7512754fe60SDimitry Andric Defs.push_back(std::make_pair(I.start(), I.value()));
7522754fe60SDimitry Andric
7533b0f4066SDimitry Andric // Extend all defs, and possibly add new ones along the way.
7543b0f4066SDimitry Andric for (unsigned i = 0; i != Defs.size(); ++i) {
7552754fe60SDimitry Andric SlotIndex Idx = Defs[i].first;
7562cab237bSDimitry Andric DbgValueLocation Loc = Defs[i].second;
7572cab237bSDimitry Andric const MachineOperand &LocMO = locations[Loc.locNo()];
7582754fe60SDimitry Andric
7592cab237bSDimitry Andric if (!LocMO.isReg()) {
7602cab237bSDimitry Andric extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS);
7617ae0e2c9SDimitry Andric continue;
7627ae0e2c9SDimitry Andric }
7637ae0e2c9SDimitry Andric
7642754fe60SDimitry Andric // Register locations are constrained to where the register value is live.
7652cab237bSDimitry Andric if (TargetRegisterInfo::isVirtualRegister(LocMO.getReg())) {
76691bc56edSDimitry Andric LiveInterval *LI = nullptr;
76791bc56edSDimitry Andric const VNInfo *VNI = nullptr;
7682cab237bSDimitry Andric if (LIS.hasInterval(LocMO.getReg())) {
7692cab237bSDimitry Andric LI = &LIS.getInterval(LocMO.getReg());
7707ae0e2c9SDimitry Andric VNI = LI->getVNInfoAt(Idx);
7717ae0e2c9SDimitry Andric }
7723b0f4066SDimitry Andric SmallVector<SlotIndex, 16> Kills;
7732cab237bSDimitry Andric extendDef(Idx, Loc, LI, VNI, &Kills, LIS);
774*b5893f02SDimitry Andric // FIXME: Handle sub-registers in addDefsFromCopies. The problem is that
775*b5893f02SDimitry Andric // if the original location for example is %vreg0:sub_hi, and we find a
776*b5893f02SDimitry Andric // full register copy in addDefsFromCopies (at the moment it only handles
777*b5893f02SDimitry Andric // full register copies), then we must add the sub1 sub-register index to
778*b5893f02SDimitry Andric // the new location. However, that is only possible if the new virtual
779*b5893f02SDimitry Andric // register is of the same regclass (or if there is an equivalent
780*b5893f02SDimitry Andric // sub-register in that regclass). For now, simply skip handling copies if
781*b5893f02SDimitry Andric // a sub-register is involved.
782*b5893f02SDimitry Andric if (LI && !LocMO.getSubReg())
7832cab237bSDimitry Andric addDefsFromCopies(LI, Loc.locNo(), Loc.wasIndirect(), Kills, Defs, MRI,
7842cab237bSDimitry Andric LIS);
7857ae0e2c9SDimitry Andric continue;
7867ae0e2c9SDimitry Andric }
7877ae0e2c9SDimitry Andric
7882cab237bSDimitry Andric // For physregs, we only mark the start slot idx. DwarfDebug will see it
7892cab237bSDimitry Andric // as if the DBG_VALUE is valid up until the end of the basic block, or
7902cab237bSDimitry Andric // the next def of the physical register. So we do not need to extend the
7912cab237bSDimitry Andric // range. It might actually happen that the DBG_VALUE is the last use of
7922cab237bSDimitry Andric // the physical register (e.g. if this is an unused input argument to a
7932cab237bSDimitry Andric // function).
7942754fe60SDimitry Andric }
7952754fe60SDimitry Andric
7962cab237bSDimitry Andric // The computed intervals may extend beyond the range of the debug
7972cab237bSDimitry Andric // location's lexical scope. In this case, splitting of an interval
7982cab237bSDimitry Andric // can result in an interval outside of the scope being created,
7992cab237bSDimitry Andric // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
8002cab237bSDimitry Andric // this, trim the intervals to the lexical scope.
8012cab237bSDimitry Andric
8022cab237bSDimitry Andric LexicalScope *Scope = LS.findLexicalScope(dl);
8032cab237bSDimitry Andric if (!Scope)
8042cab237bSDimitry Andric return;
8052cab237bSDimitry Andric
8062cab237bSDimitry Andric SlotIndex PrevEnd;
8072cab237bSDimitry Andric LocMap::iterator I = locInts.begin();
8082cab237bSDimitry Andric
8092cab237bSDimitry Andric // Iterate over the lexical scope ranges. Each time round the loop
8102cab237bSDimitry Andric // we check the intervals for overlap with the end of the previous
8112cab237bSDimitry Andric // range and the start of the next. The first range is handled as
8122cab237bSDimitry Andric // a special case where there is no PrevEnd.
8132cab237bSDimitry Andric for (const InsnRange &Range : Scope->getRanges()) {
8142cab237bSDimitry Andric SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
8152cab237bSDimitry Andric SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
8162cab237bSDimitry Andric
8172cab237bSDimitry Andric // At the start of each iteration I has been advanced so that
8182cab237bSDimitry Andric // I.stop() >= PrevEnd. Check for overlap.
8192cab237bSDimitry Andric if (PrevEnd && I.start() < PrevEnd) {
8202cab237bSDimitry Andric SlotIndex IStop = I.stop();
8212cab237bSDimitry Andric DbgValueLocation Loc = I.value();
8222cab237bSDimitry Andric
8232cab237bSDimitry Andric // Stop overlaps previous end - trim the end of the interval to
8242cab237bSDimitry Andric // the scope range.
8252cab237bSDimitry Andric I.setStopUnchecked(PrevEnd);
8262cab237bSDimitry Andric ++I;
8272cab237bSDimitry Andric
8282cab237bSDimitry Andric // If the interval also overlaps the start of the "next" (i.e.
8292cab237bSDimitry Andric // current) range create a new interval for the remainder (which
8302cab237bSDimitry Andric // may be further trimmed).
8312cab237bSDimitry Andric if (RStart < IStop)
8322cab237bSDimitry Andric I.insert(RStart, IStop, Loc);
8332cab237bSDimitry Andric }
8342cab237bSDimitry Andric
8352cab237bSDimitry Andric // Advance I so that I.stop() >= RStart, and check for overlap.
8362cab237bSDimitry Andric I.advanceTo(RStart);
8372cab237bSDimitry Andric if (!I.valid())
8382cab237bSDimitry Andric return;
8392cab237bSDimitry Andric
8402cab237bSDimitry Andric if (I.start() < RStart) {
8412cab237bSDimitry Andric // Interval start overlaps range - trim to the scope range.
8422cab237bSDimitry Andric I.setStartUnchecked(RStart);
8432cab237bSDimitry Andric // Remember that this interval was trimmed.
8442cab237bSDimitry Andric trimmedDefs.insert(RStart);
8452cab237bSDimitry Andric }
8462cab237bSDimitry Andric
8472cab237bSDimitry Andric // The end of a lexical scope range is the last instruction in the
8482cab237bSDimitry Andric // range. To convert to an interval we need the index of the
8492cab237bSDimitry Andric // instruction after it.
8502cab237bSDimitry Andric REnd = REnd.getNextIndex();
8512cab237bSDimitry Andric
8522cab237bSDimitry Andric // Advance I to first interval outside current range.
8532cab237bSDimitry Andric I.advanceTo(REnd);
8542cab237bSDimitry Andric if (!I.valid())
8552cab237bSDimitry Andric return;
8562cab237bSDimitry Andric
8572cab237bSDimitry Andric PrevEnd = REnd;
8582cab237bSDimitry Andric }
8592cab237bSDimitry Andric
8602cab237bSDimitry Andric // Check for overlap with end of final range.
8612cab237bSDimitry Andric if (PrevEnd && I.start() < PrevEnd)
8622cab237bSDimitry Andric I.setStopUnchecked(PrevEnd);
8632754fe60SDimitry Andric }
8642754fe60SDimitry Andric
computeIntervals()8652754fe60SDimitry Andric void LDVImpl::computeIntervals() {
8662cab237bSDimitry Andric LexicalScopes LS;
8672cab237bSDimitry Andric LS.initialize(*MF);
8682cab237bSDimitry Andric
8693b0f4066SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
8702cab237bSDimitry Andric userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS);
8713b0f4066SDimitry Andric userValues[i]->mapVirtRegs(this);
8723b0f4066SDimitry Andric }
8732754fe60SDimitry Andric }
8742754fe60SDimitry Andric
runOnMachineFunction(MachineFunction & mf)8752754fe60SDimitry Andric bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
87639d628a0SDimitry Andric clear();
8772754fe60SDimitry Andric MF = &mf;
8782754fe60SDimitry Andric LIS = &pass.getAnalysis<LiveIntervals>();
87939d628a0SDimitry Andric TRI = mf.getSubtarget().getRegisterInfo();
8804ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
8813861d79fSDimitry Andric << mf.getName() << " **********\n");
8822754fe60SDimitry Andric
8832754fe60SDimitry Andric bool Changed = collectDebugValues(mf);
8842754fe60SDimitry Andric computeIntervals();
8854ba319b5SDimitry Andric LLVM_DEBUG(print(dbgs()));
886139f7f9bSDimitry Andric ModifiedMF = Changed;
8872754fe60SDimitry Andric return Changed;
8882754fe60SDimitry Andric }
8892754fe60SDimitry Andric
removeDebugValues(MachineFunction & mf)89039d628a0SDimitry Andric static void removeDebugValues(MachineFunction &mf) {
89139d628a0SDimitry Andric for (MachineBasicBlock &MBB : mf) {
89239d628a0SDimitry Andric for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
89339d628a0SDimitry Andric if (!MBBI->isDebugValue()) {
89439d628a0SDimitry Andric ++MBBI;
89539d628a0SDimitry Andric continue;
89639d628a0SDimitry Andric }
89739d628a0SDimitry Andric MBBI = MBB.erase(MBBI);
89839d628a0SDimitry Andric }
89939d628a0SDimitry Andric }
90039d628a0SDimitry Andric }
90139d628a0SDimitry Andric
runOnMachineFunction(MachineFunction & mf)9022754fe60SDimitry Andric bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
9032754fe60SDimitry Andric if (!EnableLDV)
9042754fe60SDimitry Andric return false;
9052cab237bSDimitry Andric if (!mf.getFunction().getSubprogram()) {
90639d628a0SDimitry Andric removeDebugValues(mf);
90739d628a0SDimitry Andric return false;
90839d628a0SDimitry Andric }
9092754fe60SDimitry Andric if (!pImpl)
9102754fe60SDimitry Andric pImpl = new LDVImpl(this);
9112754fe60SDimitry Andric return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
9122754fe60SDimitry Andric }
9132754fe60SDimitry Andric
releaseMemory()9142754fe60SDimitry Andric void LiveDebugVariables::releaseMemory() {
9152754fe60SDimitry Andric if (pImpl)
9162754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->clear();
9172754fe60SDimitry Andric }
9182754fe60SDimitry Andric
~LiveDebugVariables()9192754fe60SDimitry Andric LiveDebugVariables::~LiveDebugVariables() {
9202754fe60SDimitry Andric if (pImpl)
9212754fe60SDimitry Andric delete static_cast<LDVImpl*>(pImpl);
9222754fe60SDimitry Andric }
9232754fe60SDimitry Andric
924bd5abe19SDimitry Andric //===----------------------------------------------------------------------===//
925bd5abe19SDimitry Andric // Live Range Splitting
926bd5abe19SDimitry Andric //===----------------------------------------------------------------------===//
927bd5abe19SDimitry Andric
928bd5abe19SDimitry Andric bool
splitLocation(unsigned OldLocNo,ArrayRef<unsigned> NewRegs,LiveIntervals & LIS)929f785676fSDimitry Andric UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
930f785676fSDimitry Andric LiveIntervals& LIS) {
9314ba319b5SDimitry Andric LLVM_DEBUG({
932bd5abe19SDimitry Andric dbgs() << "Splitting Loc" << OldLocNo << '\t';
93391bc56edSDimitry Andric print(dbgs(), nullptr);
934bd5abe19SDimitry Andric });
935bd5abe19SDimitry Andric bool DidChange = false;
936bd5abe19SDimitry Andric LocMap::iterator LocMapI;
937bd5abe19SDimitry Andric LocMapI.setMap(locInts);
938bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) {
939f785676fSDimitry Andric LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
940bd5abe19SDimitry Andric if (LI->empty())
941bd5abe19SDimitry Andric continue;
942bd5abe19SDimitry Andric
943bd5abe19SDimitry Andric // Don't allocate the new LocNo until it is needed.
9442cab237bSDimitry Andric unsigned NewLocNo = UndefLocNo;
945bd5abe19SDimitry Andric
946bd5abe19SDimitry Andric // Iterate over the overlaps between locInts and LI.
947bd5abe19SDimitry Andric LocMapI.find(LI->beginIndex());
948bd5abe19SDimitry Andric if (!LocMapI.valid())
949bd5abe19SDimitry Andric continue;
950bd5abe19SDimitry Andric LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
951bd5abe19SDimitry Andric LiveInterval::iterator LIE = LI->end();
952bd5abe19SDimitry Andric while (LocMapI.valid() && LII != LIE) {
953bd5abe19SDimitry Andric // At this point, we know that LocMapI.stop() > LII->start.
954bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start());
955bd5abe19SDimitry Andric if (LII == LIE)
956bd5abe19SDimitry Andric break;
957bd5abe19SDimitry Andric
958bd5abe19SDimitry Andric // Now LII->end > LocMapI.start(). Do we have an overlap?
9592cab237bSDimitry Andric if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) {
960bd5abe19SDimitry Andric // Overlapping correct location. Allocate NewLocNo now.
9612cab237bSDimitry Andric if (NewLocNo == UndefLocNo) {
962bd5abe19SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
963bd5abe19SDimitry Andric MO.setSubReg(locations[OldLocNo].getSubReg());
964bd5abe19SDimitry Andric NewLocNo = getLocationNo(MO);
965bd5abe19SDimitry Andric DidChange = true;
966bd5abe19SDimitry Andric }
967bd5abe19SDimitry Andric
968bd5abe19SDimitry Andric SlotIndex LStart = LocMapI.start();
969bd5abe19SDimitry Andric SlotIndex LStop = LocMapI.stop();
9702cab237bSDimitry Andric DbgValueLocation OldLoc = LocMapI.value();
971bd5abe19SDimitry Andric
972bd5abe19SDimitry Andric // Trim LocMapI down to the LII overlap.
973bd5abe19SDimitry Andric if (LStart < LII->start)
974bd5abe19SDimitry Andric LocMapI.setStartUnchecked(LII->start);
975bd5abe19SDimitry Andric if (LStop > LII->end)
976bd5abe19SDimitry Andric LocMapI.setStopUnchecked(LII->end);
977bd5abe19SDimitry Andric
978bd5abe19SDimitry Andric // Change the value in the overlap. This may trigger coalescing.
9792cab237bSDimitry Andric LocMapI.setValue(OldLoc.changeLocNo(NewLocNo));
980bd5abe19SDimitry Andric
981bd5abe19SDimitry Andric // Re-insert any removed OldLocNo ranges.
982bd5abe19SDimitry Andric if (LStart < LocMapI.start()) {
9832cab237bSDimitry Andric LocMapI.insert(LStart, LocMapI.start(), OldLoc);
984bd5abe19SDimitry Andric ++LocMapI;
985bd5abe19SDimitry Andric assert(LocMapI.valid() && "Unexpected coalescing");
986bd5abe19SDimitry Andric }
987bd5abe19SDimitry Andric if (LStop > LocMapI.stop()) {
988bd5abe19SDimitry Andric ++LocMapI;
9892cab237bSDimitry Andric LocMapI.insert(LII->end, LStop, OldLoc);
990bd5abe19SDimitry Andric --LocMapI;
991bd5abe19SDimitry Andric }
992bd5abe19SDimitry Andric }
993bd5abe19SDimitry Andric
994bd5abe19SDimitry Andric // Advance to the next overlap.
995bd5abe19SDimitry Andric if (LII->end < LocMapI.stop()) {
996bd5abe19SDimitry Andric if (++LII == LIE)
997bd5abe19SDimitry Andric break;
998bd5abe19SDimitry Andric LocMapI.advanceTo(LII->start);
999bd5abe19SDimitry Andric } else {
1000bd5abe19SDimitry Andric ++LocMapI;
1001bd5abe19SDimitry Andric if (!LocMapI.valid())
1002bd5abe19SDimitry Andric break;
1003bd5abe19SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start());
1004bd5abe19SDimitry Andric }
1005bd5abe19SDimitry Andric }
1006bd5abe19SDimitry Andric }
1007bd5abe19SDimitry Andric
1008bd5abe19SDimitry Andric // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
1009bd5abe19SDimitry Andric locations.erase(locations.begin() + OldLocNo);
1010bd5abe19SDimitry Andric LocMapI.goToBegin();
1011bd5abe19SDimitry Andric while (LocMapI.valid()) {
10122cab237bSDimitry Andric DbgValueLocation v = LocMapI.value();
10132cab237bSDimitry Andric if (v.locNo() == OldLocNo) {
10144ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
1015bd5abe19SDimitry Andric << LocMapI.stop() << ")\n");
1016bd5abe19SDimitry Andric LocMapI.erase();
1017bd5abe19SDimitry Andric } else {
10184ba319b5SDimitry Andric // Undef values always have location number UndefLocNo, so don't change
10194ba319b5SDimitry Andric // locNo in that case. See getLocationNo().
10204ba319b5SDimitry Andric if (!v.isUndef() && v.locNo() > OldLocNo)
10212cab237bSDimitry Andric LocMapI.setValueUnchecked(v.changeLocNo(v.locNo() - 1));
1022bd5abe19SDimitry Andric ++LocMapI;
1023bd5abe19SDimitry Andric }
1024bd5abe19SDimitry Andric }
1025bd5abe19SDimitry Andric
10264ba319b5SDimitry Andric LLVM_DEBUG({
10274ba319b5SDimitry Andric dbgs() << "Split result: \t";
10284ba319b5SDimitry Andric print(dbgs(), nullptr);
10294ba319b5SDimitry Andric });
1030bd5abe19SDimitry Andric return DidChange;
1031bd5abe19SDimitry Andric }
1032bd5abe19SDimitry Andric
1033bd5abe19SDimitry Andric bool
splitRegister(unsigned OldReg,ArrayRef<unsigned> NewRegs,LiveIntervals & LIS)1034f785676fSDimitry Andric UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
1035f785676fSDimitry Andric LiveIntervals &LIS) {
1036bd5abe19SDimitry Andric bool DidChange = false;
1037bd5abe19SDimitry Andric // Split locations referring to OldReg. Iterate backwards so splitLocation can
1038dff0c46cSDimitry Andric // safely erase unused locations.
1039bd5abe19SDimitry Andric for (unsigned i = locations.size(); i ; --i) {
1040bd5abe19SDimitry Andric unsigned LocNo = i-1;
1041bd5abe19SDimitry Andric const MachineOperand *Loc = &locations[LocNo];
1042bd5abe19SDimitry Andric if (!Loc->isReg() || Loc->getReg() != OldReg)
1043bd5abe19SDimitry Andric continue;
1044f785676fSDimitry Andric DidChange |= splitLocation(LocNo, NewRegs, LIS);
1045bd5abe19SDimitry Andric }
1046bd5abe19SDimitry Andric return DidChange;
1047bd5abe19SDimitry Andric }
1048bd5abe19SDimitry Andric
splitRegister(unsigned OldReg,ArrayRef<unsigned> NewRegs)1049f785676fSDimitry Andric void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
1050bd5abe19SDimitry Andric bool DidChange = false;
1051bd5abe19SDimitry Andric for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
1052f785676fSDimitry Andric DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
1053bd5abe19SDimitry Andric
1054bd5abe19SDimitry Andric if (!DidChange)
1055bd5abe19SDimitry Andric return;
1056bd5abe19SDimitry Andric
1057bd5abe19SDimitry Andric // Map all of the new virtual registers.
1058bd5abe19SDimitry Andric UserValue *UV = lookupVirtReg(OldReg);
1059bd5abe19SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i)
1060f785676fSDimitry Andric mapVirtReg(NewRegs[i], UV);
1061bd5abe19SDimitry Andric }
1062bd5abe19SDimitry Andric
1063bd5abe19SDimitry Andric void LiveDebugVariables::
splitRegister(unsigned OldReg,ArrayRef<unsigned> NewRegs,LiveIntervals & LIS)1064f785676fSDimitry Andric splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
1065bd5abe19SDimitry Andric if (pImpl)
1066bd5abe19SDimitry Andric static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
1067bd5abe19SDimitry Andric }
1068bd5abe19SDimitry Andric
rewriteLocations(VirtRegMap & VRM,const MachineFunction & MF,const TargetInstrInfo & TII,const TargetRegisterInfo & TRI,SpillOffsetMap & SpillOffsets)1069*b5893f02SDimitry Andric void UserValue::rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
1070*b5893f02SDimitry Andric const TargetInstrInfo &TII,
1071*b5893f02SDimitry Andric const TargetRegisterInfo &TRI,
1072*b5893f02SDimitry Andric SpillOffsetMap &SpillOffsets) {
10732cab237bSDimitry Andric // Build a set of new locations with new numbers so we can coalesce our
10742cab237bSDimitry Andric // IntervalMap if two vreg intervals collapse to the same physical location.
10752cab237bSDimitry Andric // Use MapVector instead of SetVector because MapVector::insert returns the
10762cab237bSDimitry Andric // position of the previously or newly inserted element. The boolean value
10772cab237bSDimitry Andric // tracks if the location was produced by a spill.
10782cab237bSDimitry Andric // FIXME: This will be problematic if we ever support direct and indirect
10792cab237bSDimitry Andric // frame index locations, i.e. expressing both variables in memory and
10802cab237bSDimitry Andric // 'int x, *px = &x'. The "spilled" bit must become part of the location.
1081*b5893f02SDimitry Andric MapVector<MachineOperand, std::pair<bool, unsigned>> NewLocations;
10822cab237bSDimitry Andric SmallVector<unsigned, 4> LocNoMap(locations.size());
10832cab237bSDimitry Andric for (unsigned I = 0, E = locations.size(); I != E; ++I) {
10842cab237bSDimitry Andric bool Spilled = false;
1085*b5893f02SDimitry Andric unsigned SpillOffset = 0;
10862cab237bSDimitry Andric MachineOperand Loc = locations[I];
10872754fe60SDimitry Andric // Only virtual registers are rewritten.
10882cab237bSDimitry Andric if (Loc.isReg() && Loc.getReg() &&
10892cab237bSDimitry Andric TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
10902754fe60SDimitry Andric unsigned VirtReg = Loc.getReg();
10912754fe60SDimitry Andric if (VRM.isAssignedReg(VirtReg) &&
10922754fe60SDimitry Andric TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
1093bd5abe19SDimitry Andric // This can create a %noreg operand in rare cases when the sub-register
1094bd5abe19SDimitry Andric // index is no longer available. That means the user value is in a
1095bd5abe19SDimitry Andric // non-existent sub-register, and %noreg is exactly what we want.
10962754fe60SDimitry Andric Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
1097dff0c46cSDimitry Andric } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
1098*b5893f02SDimitry Andric // Retrieve the stack slot offset.
1099*b5893f02SDimitry Andric unsigned SpillSize;
1100*b5893f02SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
1101*b5893f02SDimitry Andric const TargetRegisterClass *TRC = MRI.getRegClass(VirtReg);
1102*b5893f02SDimitry Andric bool Success = TII.getStackSlotRange(TRC, Loc.getSubReg(), SpillSize,
1103*b5893f02SDimitry Andric SpillOffset, MF);
1104*b5893f02SDimitry Andric
1105*b5893f02SDimitry Andric // FIXME: Invalidate the location if the offset couldn't be calculated.
1106*b5893f02SDimitry Andric (void)Success;
1107*b5893f02SDimitry Andric
11082754fe60SDimitry Andric Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
11092cab237bSDimitry Andric Spilled = true;
11102754fe60SDimitry Andric } else {
11112754fe60SDimitry Andric Loc.setReg(0);
11122754fe60SDimitry Andric Loc.setSubReg(0);
11132754fe60SDimitry Andric }
11142cab237bSDimitry Andric }
11152cab237bSDimitry Andric
11162cab237bSDimitry Andric // Insert this location if it doesn't already exist and record a mapping
11172cab237bSDimitry Andric // from the old number to the new number.
1118*b5893f02SDimitry Andric auto InsertResult = NewLocations.insert({Loc, {Spilled, SpillOffset}});
11192cab237bSDimitry Andric unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
11202cab237bSDimitry Andric LocNoMap[I] = NewLocNo;
11212cab237bSDimitry Andric }
11222cab237bSDimitry Andric
1123*b5893f02SDimitry Andric // Rewrite the locations and record the stack slot offsets for spills.
11242cab237bSDimitry Andric locations.clear();
1125*b5893f02SDimitry Andric SpillOffsets.clear();
11262cab237bSDimitry Andric for (auto &Pair : NewLocations) {
1127*b5893f02SDimitry Andric bool Spilled;
1128*b5893f02SDimitry Andric unsigned SpillOffset;
1129*b5893f02SDimitry Andric std::tie(Spilled, SpillOffset) = Pair.second;
11302cab237bSDimitry Andric locations.push_back(Pair.first);
1131*b5893f02SDimitry Andric if (Spilled) {
11322cab237bSDimitry Andric unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
1133*b5893f02SDimitry Andric SpillOffsets[NewLocNo] = SpillOffset;
11342754fe60SDimitry Andric }
11352754fe60SDimitry Andric }
11362754fe60SDimitry Andric
11372cab237bSDimitry Andric // Update the interval map, but only coalesce left, since intervals to the
11382cab237bSDimitry Andric // right use the old location numbers. This should merge two contiguous
11392cab237bSDimitry Andric // DBG_VALUE intervals with different vregs that were allocated to the same
11402cab237bSDimitry Andric // physical register.
11412cab237bSDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
11422cab237bSDimitry Andric DbgValueLocation Loc = I.value();
11434ba319b5SDimitry Andric // Undef values don't exist in locations (and thus not in LocNoMap either)
11444ba319b5SDimitry Andric // so skip over them. See getLocationNo().
11454ba319b5SDimitry Andric if (Loc.isUndef())
11464ba319b5SDimitry Andric continue;
11472cab237bSDimitry Andric unsigned NewLocNo = LocNoMap[Loc.locNo()];
11482cab237bSDimitry Andric I.setValueUnchecked(Loc.changeLocNo(NewLocNo));
11492cab237bSDimitry Andric I.setStart(I.start());
11502cab237bSDimitry Andric }
11512cab237bSDimitry Andric }
11522cab237bSDimitry Andric
11532cab237bSDimitry Andric /// Find an iterator for inserting a DBG_VALUE instruction.
11542754fe60SDimitry Andric static MachineBasicBlock::iterator
findInsertLocation(MachineBasicBlock * MBB,SlotIndex Idx,LiveIntervals & LIS)11552754fe60SDimitry Andric findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
11562754fe60SDimitry Andric LiveIntervals &LIS) {
11572754fe60SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB);
11582754fe60SDimitry Andric Idx = Idx.getBaseIndex();
11592754fe60SDimitry Andric
11602754fe60SDimitry Andric // Try to find an insert location by going backwards from Idx.
11612754fe60SDimitry Andric MachineInstr *MI;
11622754fe60SDimitry Andric while (!(MI = LIS.getInstructionFromIndex(Idx))) {
11632754fe60SDimitry Andric // We've reached the beginning of MBB.
11642754fe60SDimitry Andric if (Idx == Start) {
1165d88c1a5aSDimitry Andric MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
11662754fe60SDimitry Andric return I;
11672754fe60SDimitry Andric }
11682754fe60SDimitry Andric Idx = Idx.getPrevIndex();
11692754fe60SDimitry Andric }
11702754fe60SDimitry Andric
11712754fe60SDimitry Andric // Don't insert anything after the first terminator, though.
1172dff0c46cSDimitry Andric return MI->isTerminator() ? MBB->getFirstTerminator() :
117391bc56edSDimitry Andric std::next(MachineBasicBlock::iterator(MI));
11742754fe60SDimitry Andric }
11752754fe60SDimitry Andric
11762cab237bSDimitry Andric /// Find an iterator for inserting the next DBG_VALUE instruction
11772cab237bSDimitry Andric /// (or end if no more insert locations found).
11782cab237bSDimitry Andric static MachineBasicBlock::iterator
findNextInsertLocation(MachineBasicBlock * MBB,MachineBasicBlock::iterator I,SlotIndex StopIdx,MachineOperand & LocMO,LiveIntervals & LIS,const TargetRegisterInfo & TRI)11792cab237bSDimitry Andric findNextInsertLocation(MachineBasicBlock *MBB,
11802cab237bSDimitry Andric MachineBasicBlock::iterator I,
11812cab237bSDimitry Andric SlotIndex StopIdx, MachineOperand &LocMO,
11822754fe60SDimitry Andric LiveIntervals &LIS,
11832cab237bSDimitry Andric const TargetRegisterInfo &TRI) {
11842cab237bSDimitry Andric if (!LocMO.isReg())
11852cab237bSDimitry Andric return MBB->instr_end();
11862cab237bSDimitry Andric unsigned Reg = LocMO.getReg();
11872cab237bSDimitry Andric
11882cab237bSDimitry Andric // Find the next instruction in the MBB that define the register Reg.
11894ba319b5SDimitry Andric while (I != MBB->end() && !I->isTerminator()) {
11902cab237bSDimitry Andric if (!LIS.isNotInMIMap(*I) &&
11912cab237bSDimitry Andric SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I)))
11922cab237bSDimitry Andric break;
11932cab237bSDimitry Andric if (I->definesRegister(Reg, &TRI))
11942cab237bSDimitry Andric // The insert location is directly after the instruction/bundle.
11952cab237bSDimitry Andric return std::next(I);
11962cab237bSDimitry Andric ++I;
11972cab237bSDimitry Andric }
11982cab237bSDimitry Andric return MBB->end();
11992cab237bSDimitry Andric }
12002cab237bSDimitry Andric
insertDebugValue(MachineBasicBlock * MBB,SlotIndex StartIdx,SlotIndex StopIdx,DbgValueLocation Loc,bool Spilled,unsigned SpillOffset,LiveIntervals & LIS,const TargetInstrInfo & TII,const TargetRegisterInfo & TRI)12012cab237bSDimitry Andric void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
1202*b5893f02SDimitry Andric SlotIndex StopIdx, DbgValueLocation Loc,
1203*b5893f02SDimitry Andric bool Spilled, unsigned SpillOffset,
1204*b5893f02SDimitry Andric LiveIntervals &LIS, const TargetInstrInfo &TII,
12052cab237bSDimitry Andric const TargetRegisterInfo &TRI) {
12062cab237bSDimitry Andric SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB);
12072cab237bSDimitry Andric // Only search within the current MBB.
12082cab237bSDimitry Andric StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx;
12092cab237bSDimitry Andric MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS);
12104ba319b5SDimitry Andric // Undef values don't exist in locations so create new "noreg" register MOs
12114ba319b5SDimitry Andric // for them. See getLocationNo().
12124ba319b5SDimitry Andric MachineOperand MO = !Loc.isUndef() ?
12134ba319b5SDimitry Andric locations[Loc.locNo()] :
12144ba319b5SDimitry Andric MachineOperand::CreateReg(/* Reg */ 0, /* isDef */ false, /* isImp */ false,
12154ba319b5SDimitry Andric /* isKill */ false, /* isDead */ false,
12164ba319b5SDimitry Andric /* isUndef */ false, /* isEarlyClobber */ false,
12174ba319b5SDimitry Andric /* SubReg */ 0, /* isDebug */ true);
12184ba319b5SDimitry Andric
12196122f3e6SDimitry Andric ++NumInsertedDebugValues;
12202754fe60SDimitry Andric
1221ff0cc061SDimitry Andric assert(cast<DILocalVariable>(Variable)
1222ff0cc061SDimitry Andric ->isValidLocationForIntrinsic(getDebugLoc()) &&
1223ff0cc061SDimitry Andric "Expected inlined-at fields to agree");
12242cab237bSDimitry Andric
12252cab237bSDimitry Andric // If the location was spilled, the new DBG_VALUE will be indirect. If the
12262cab237bSDimitry Andric // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
1227*b5893f02SDimitry Andric // that the original virtual register was a pointer. Also, add the stack slot
1228*b5893f02SDimitry Andric // offset for the spilled register to the expression.
12292cab237bSDimitry Andric const DIExpression *Expr = Expression;
12302cab237bSDimitry Andric bool IsIndirect = Loc.wasIndirect();
12312cab237bSDimitry Andric if (Spilled) {
1232*b5893f02SDimitry Andric auto Deref = IsIndirect ? DIExpression::WithDeref : DIExpression::NoDeref;
1233*b5893f02SDimitry Andric Expr =
1234*b5893f02SDimitry Andric DIExpression::prepend(Expr, DIExpression::NoDeref, SpillOffset, Deref);
12352cab237bSDimitry Andric IsIndirect = true;
12362cab237bSDimitry Andric }
12372cab237bSDimitry Andric
12382cab237bSDimitry Andric assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index");
12392cab237bSDimitry Andric
12402cab237bSDimitry Andric do {
12414ba319b5SDimitry Andric BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
12424ba319b5SDimitry Andric IsIndirect, MO, Variable, Expr);
12432cab237bSDimitry Andric
12442cab237bSDimitry Andric // Continue and insert DBG_VALUES after every redefinition of register
12452cab237bSDimitry Andric // associated with the debug value within the range
12462cab237bSDimitry Andric I = findNextInsertLocation(MBB, I, StopIdx, MO, LIS, TRI);
12472cab237bSDimitry Andric } while (I != MBB->end());
12482754fe60SDimitry Andric }
12492754fe60SDimitry Andric
emitDebugValues(VirtRegMap * VRM,LiveIntervals & LIS,const TargetInstrInfo & TII,const TargetRegisterInfo & TRI,const SpillOffsetMap & SpillOffsets)12502754fe60SDimitry Andric void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
12512cab237bSDimitry Andric const TargetInstrInfo &TII,
12522cab237bSDimitry Andric const TargetRegisterInfo &TRI,
1253*b5893f02SDimitry Andric const SpillOffsetMap &SpillOffsets) {
12542754fe60SDimitry Andric MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
12552754fe60SDimitry Andric
12562754fe60SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
12572754fe60SDimitry Andric SlotIndex Start = I.start();
12582754fe60SDimitry Andric SlotIndex Stop = I.stop();
12592cab237bSDimitry Andric DbgValueLocation Loc = I.value();
1260*b5893f02SDimitry Andric auto SpillIt =
1261*b5893f02SDimitry Andric !Loc.isUndef() ? SpillOffsets.find(Loc.locNo()) : SpillOffsets.end();
1262*b5893f02SDimitry Andric bool Spilled = SpillIt != SpillOffsets.end();
1263*b5893f02SDimitry Andric unsigned SpillOffset = Spilled ? SpillIt->second : 0;
12642cab237bSDimitry Andric
12652cab237bSDimitry Andric // If the interval start was trimmed to the lexical scope insert the
12662cab237bSDimitry Andric // DBG_VALUE at the previous index (otherwise it appears after the
12672cab237bSDimitry Andric // first instruction in the range).
12682cab237bSDimitry Andric if (trimmedDefs.count(Start))
12692cab237bSDimitry Andric Start = Start.getPrevIndex();
12702cab237bSDimitry Andric
12714ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo());
12727d523365SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
12737d523365SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
12742754fe60SDimitry Andric
12754ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1276*b5893f02SDimitry Andric insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1277*b5893f02SDimitry Andric TRI);
12782754fe60SDimitry Andric // This interval may span multiple basic blocks.
12792754fe60SDimitry Andric // Insert a DBG_VALUE into each one.
12802754fe60SDimitry Andric while (Stop > MBBEnd) {
12812754fe60SDimitry Andric // Move to the next block.
12822754fe60SDimitry Andric Start = MBBEnd;
12832754fe60SDimitry Andric if (++MBB == MFEnd)
12842754fe60SDimitry Andric break;
12857d523365SDimitry Andric MBBEnd = LIS.getMBBEndIdx(&*MBB);
12864ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1287*b5893f02SDimitry Andric insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1288*b5893f02SDimitry Andric TRI);
12892754fe60SDimitry Andric }
12904ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << '\n');
12912754fe60SDimitry Andric if (MBB == MFEnd)
12922754fe60SDimitry Andric break;
12932754fe60SDimitry Andric
12942754fe60SDimitry Andric ++I;
12952754fe60SDimitry Andric }
12962754fe60SDimitry Andric }
12972754fe60SDimitry Andric
emitDebugValues(VirtRegMap * VRM)12982754fe60SDimitry Andric void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
12994ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
130039d628a0SDimitry Andric if (!MF)
130139d628a0SDimitry Andric return;
130239d628a0SDimitry Andric const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1303*b5893f02SDimitry Andric SpillOffsetMap SpillOffsets;
13042754fe60SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
13054ba319b5SDimitry Andric LLVM_DEBUG(userValues[i]->print(dbgs(), TRI));
1306*b5893f02SDimitry Andric userValues[i]->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets);
1307*b5893f02SDimitry Andric userValues[i]->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets);
13082754fe60SDimitry Andric }
1309139f7f9bSDimitry Andric EmitDone = true;
13102754fe60SDimitry Andric }
13112754fe60SDimitry Andric
emitDebugValues(VirtRegMap * VRM)13122754fe60SDimitry Andric void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
13132754fe60SDimitry Andric if (pImpl)
13142754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
13152754fe60SDimitry Andric }
13162754fe60SDimitry Andric
doInitialization(Module & M)131739d628a0SDimitry Andric bool LiveDebugVariables::doInitialization(Module &M) {
131839d628a0SDimitry Andric return Pass::doInitialization(M);
131939d628a0SDimitry Andric }
13202754fe60SDimitry Andric
13217a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const1322edd7eaddSDimitry Andric LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
13232754fe60SDimitry Andric if (pImpl)
13242754fe60SDimitry Andric static_cast<LDVImpl*>(pImpl)->print(dbgs());
13252754fe60SDimitry Andric }
13262754fe60SDimitry Andric #endif
1327