1 //===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the LiveDebugVariables analysis.
11 // Remove all DBG_VALUE instructions referencing virtual registers and replace
12 // them with a data structure tracking where live user variables are kept - in a
13 // virtual register or in a stack slot.
15 // Allow the data structure to be updated during register allocation when values
16 // are moved between registers and stack slots. Finally emit new DBG_VALUE
17 // instructions after register allocation is complete.
19 //===----------------------------------------------------------------------===//
21 #include "LiveDebugVariables.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/IntervalMap.h"
25 #include "llvm/ADT/MapVector.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallSet.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/CodeGen/LexicalScopes.h"
32 #include "llvm/CodeGen/LiveInterval.h"
33 #include "llvm/CodeGen/LiveIntervals.h"
34 #include "llvm/CodeGen/MachineBasicBlock.h"
35 #include "llvm/CodeGen/MachineDominators.h"
36 #include "llvm/CodeGen/MachineFunction.h"
37 #include "llvm/CodeGen/MachineInstr.h"
38 #include "llvm/CodeGen/MachineInstrBuilder.h"
39 #include "llvm/CodeGen/MachineOperand.h"
40 #include "llvm/CodeGen/MachineRegisterInfo.h"
41 #include "llvm/CodeGen/SlotIndexes.h"
42 #include "llvm/CodeGen/TargetInstrInfo.h"
43 #include "llvm/CodeGen/TargetOpcodes.h"
44 #include "llvm/CodeGen/TargetRegisterInfo.h"
45 #include "llvm/CodeGen/TargetSubtargetInfo.h"
46 #include "llvm/CodeGen/VirtRegMap.h"
47 #include "llvm/Config/llvm-config.h"
48 #include "llvm/IR/DebugInfoMetadata.h"
49 #include "llvm/IR/DebugLoc.h"
50 #include "llvm/IR/Function.h"
51 #include "llvm/IR/Metadata.h"
52 #include "llvm/InitializePasses.h"
53 #include "llvm/MC/MCRegisterInfo.h"
54 #include "llvm/Pass.h"
55 #include "llvm/Support/Casting.h"
56 #include "llvm/Support/CommandLine.h"
57 #include "llvm/Support/Compiler.h"
58 #include "llvm/Support/Debug.h"
59 #include "llvm/Support/raw_ostream.h"
68 #define DEBUG_TYPE "livedebugvars"
71 EnableLDV("live-debug-variables", cl::init(true),
72 cl::desc("Enable the live debug variables pass"), cl::Hidden);
74 STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
75 STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted");
77 char LiveDebugVariables::ID = 0;
79 INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
80 "Debug Variable Analysis", false, false)
81 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
82 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
83 INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
84 "Debug Variable Analysis", false, false)
86 void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
87 AU.addRequired<MachineDominatorTree>();
88 AU.addRequiredTransitive<LiveIntervals>();
90 MachineFunctionPass::getAnalysisUsage(AU);
93 LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) {
94 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
97 enum : unsigned { UndefLocNo = ~0U };
99 /// Describes a location by number along with some flags about the original
100 /// usage of the location.
101 class DbgValueLocation {
103 DbgValueLocation(unsigned LocNo, bool WasIndirect)
104 : LocNo(LocNo), WasIndirect(WasIndirect) {
105 static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing");
106 assert(locNo() == LocNo && "location truncation");
109 DbgValueLocation() : LocNo(0), WasIndirect(0) {}
111 unsigned locNo() const {
112 // Fix up the undef location number, which gets truncated.
113 return LocNo == INT_MAX ? UndefLocNo : LocNo;
115 bool wasIndirect() const { return WasIndirect; }
116 bool isUndef() const { return locNo() == UndefLocNo; }
118 DbgValueLocation changeLocNo(unsigned NewLocNo) const {
119 return DbgValueLocation(NewLocNo, WasIndirect);
122 friend inline bool operator==(const DbgValueLocation &LHS,
123 const DbgValueLocation &RHS) {
124 return LHS.LocNo == RHS.LocNo && LHS.WasIndirect == RHS.WasIndirect;
127 friend inline bool operator!=(const DbgValueLocation &LHS,
128 const DbgValueLocation &RHS) {
129 return !(LHS == RHS);
134 unsigned WasIndirect : 1;
137 /// Map of where a user value is live, and its location.
138 using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>;
140 /// Map of stack slot offsets for spilled locations.
141 /// Non-spilled locations are not added to the map.
142 using SpillOffsetMap = DenseMap<unsigned, unsigned>;
148 /// A user value is a part of a debug info user variable.
150 /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
151 /// holds part of a user variable. The part is identified by a byte offset.
153 /// UserValues are grouped into equivalence classes for easier searching. Two
154 /// user values are related if they refer to the same variable, or if they are
155 /// held by the same virtual register. The equivalence class is the transitive
156 /// closure of that relation.
158 const DILocalVariable *Variable; ///< The debug info variable we are part of.
159 const DIExpression *Expression; ///< Any complex address expression.
160 DebugLoc dl; ///< The debug location for the variable. This is
161 ///< used by dwarf writer to find lexical scope.
162 UserValue *leader; ///< Equivalence class leader.
163 UserValue *next = nullptr; ///< Next value in equivalence class, or null.
165 /// Numbered locations referenced by locmap.
166 SmallVector<MachineOperand, 4> locations;
168 /// Map of slot indices where this value is live.
171 /// Set of interval start indexes that have been trimmed to the
173 SmallSet<SlotIndex, 2> trimmedDefs;
175 /// Insert a DBG_VALUE into MBB at Idx for LocNo.
176 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
177 SlotIndex StopIdx, DbgValueLocation Loc, bool Spilled,
178 unsigned SpillOffset, LiveIntervals &LIS,
179 const TargetInstrInfo &TII,
180 const TargetRegisterInfo &TRI);
182 /// Replace OldLocNo ranges with NewRegs ranges where NewRegs
183 /// is live. Returns true if any changes were made.
184 bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
188 /// Create a new UserValue.
189 UserValue(const DILocalVariable *var, const DIExpression *expr, DebugLoc L,
190 LocMap::Allocator &alloc)
191 : Variable(var), Expression(expr), dl(std::move(L)), leader(this),
194 /// Get the leader of this value's equivalence class.
195 UserValue *getLeader() {
196 UserValue *l = leader;
197 while (l != l->leader)
202 /// Return the next UserValue in the equivalence class.
203 UserValue *getNext() const { return next; }
205 /// Does this UserValue match the parameters?
206 bool match(const DILocalVariable *Var, const DIExpression *Expr,
207 const DILocation *IA) const {
208 // FIXME: The fragment should be part of the equivalence class, but not
209 // other things in the expression like stack values.
210 return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA;
213 /// Merge equivalence classes.
214 static UserValue *merge(UserValue *L1, UserValue *L2) {
215 L2 = L2->getLeader();
218 L1 = L1->getLeader();
221 // Splice L2 before L1's members.
228 End->next = L1->next;
233 /// Return the location number that matches Loc.
235 /// For undef values we always return location number UndefLocNo without
236 /// inserting anything in locations. Since locations is a vector and the
237 /// location number is the position in the vector and UndefLocNo is ~0,
238 /// we would need a very big vector to put the value at the right position.
239 unsigned getLocationNo(const MachineOperand &LocMO) {
241 if (LocMO.getReg() == 0)
243 // For register locations we dont care about use/def and other flags.
244 for (unsigned i = 0, e = locations.size(); i != e; ++i)
245 if (locations[i].isReg() &&
246 locations[i].getReg() == LocMO.getReg() &&
247 locations[i].getSubReg() == LocMO.getSubReg())
250 for (unsigned i = 0, e = locations.size(); i != e; ++i)
251 if (LocMO.isIdenticalTo(locations[i]))
253 locations.push_back(LocMO);
254 // We are storing a MachineOperand outside a MachineInstr.
255 locations.back().clearParent();
256 // Don't store def operands.
257 if (locations.back().isReg()) {
258 if (locations.back().isDef())
259 locations.back().setIsDead(false);
260 locations.back().setIsUse();
262 return locations.size() - 1;
265 /// Remove (recycle) a location number. If \p LocNo still is used by the
266 /// locInts nothing is done.
267 void removeLocationIfUnused(unsigned LocNo) {
268 // Bail out if LocNo still is used.
269 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
270 DbgValueLocation Loc = I.value();
271 if (Loc.locNo() == LocNo)
274 // Remove the entry in the locations vector, and adjust all references to
275 // location numbers above the removed entry.
276 locations.erase(locations.begin() + LocNo);
277 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
278 DbgValueLocation Loc = I.value();
279 if (!Loc.isUndef() && Loc.locNo() > LocNo)
280 I.setValueUnchecked(Loc.changeLocNo(Loc.locNo() - 1));
284 /// Ensure that all virtual register locations are mapped.
285 void mapVirtRegs(LDVImpl *LDV);
287 /// Add a definition point to this value.
288 void addDef(SlotIndex Idx, const MachineOperand &LocMO, bool IsIndirect) {
289 DbgValueLocation Loc(getLocationNo(LocMO), IsIndirect);
290 // Add a singular (Idx,Idx) -> Loc mapping.
291 LocMap::iterator I = locInts.find(Idx);
292 if (!I.valid() || I.start() != Idx)
293 I.insert(Idx, Idx.getNextSlot(), Loc);
295 // A later DBG_VALUE at the same SlotIndex overrides the old location.
299 /// Extend the current definition as far as possible down.
301 /// Stop when meeting an existing def or when leaving the live
302 /// range of VNI. End points where VNI is no longer live are added to Kills.
304 /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
305 /// data-flow analysis to propagate them beyond basic block boundaries.
307 /// \param Idx Starting point for the definition.
308 /// \param Loc Location number to propagate.
309 /// \param LR Restrict liveness to where LR has the value VNI. May be null.
310 /// \param VNI When LR is not null, this is the value to restrict to.
311 /// \param [out] Kills Append end points of VNI's live range to Kills.
312 /// \param LIS Live intervals analysis.
313 void extendDef(SlotIndex Idx, DbgValueLocation Loc,
314 LiveRange *LR, const VNInfo *VNI,
315 SmallVectorImpl<SlotIndex> *Kills,
318 /// The value in LI/LocNo may be copies to other registers. Determine if
319 /// any of the copies are available at the kill points, and add defs if
322 /// \param LI Scan for copies of the value in LI->reg.
323 /// \param LocNo Location number of LI->reg.
324 /// \param WasIndirect Indicates if the original use of LI->reg was indirect
325 /// \param Kills Points where the range of LocNo could be extended.
326 /// \param [in,out] NewDefs Append (Idx, LocNo) of inserted defs here.
327 void addDefsFromCopies(
328 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
329 const SmallVectorImpl<SlotIndex> &Kills,
330 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
331 MachineRegisterInfo &MRI, LiveIntervals &LIS);
333 /// Compute the live intervals of all locations after collecting all their
335 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
336 LiveIntervals &LIS, LexicalScopes &LS);
338 /// Replace OldReg ranges with NewRegs ranges where NewRegs is
339 /// live. Returns true if any changes were made.
340 bool splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
343 /// Rewrite virtual register locations according to the provided virtual
344 /// register map. Record the stack slot offsets for the locations that
346 void rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
347 const TargetInstrInfo &TII,
348 const TargetRegisterInfo &TRI,
349 SpillOffsetMap &SpillOffsets);
351 /// Recreate DBG_VALUE instruction from data structures.
352 void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
353 const TargetInstrInfo &TII,
354 const TargetRegisterInfo &TRI,
355 const SpillOffsetMap &SpillOffsets);
357 /// Return DebugLoc of this UserValue.
358 DebugLoc getDebugLoc() { return dl;}
360 void print(raw_ostream &, const TargetRegisterInfo *);
363 /// A user label is a part of a debug info user label.
365 const DILabel *Label; ///< The debug info label we are part of.
366 DebugLoc dl; ///< The debug location for the label. This is
367 ///< used by dwarf writer to find lexical scope.
368 SlotIndex loc; ///< Slot used by the debug label.
370 /// Insert a DBG_LABEL into MBB at Idx.
371 void insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
372 LiveIntervals &LIS, const TargetInstrInfo &TII);
375 /// Create a new UserLabel.
376 UserLabel(const DILabel *label, DebugLoc L, SlotIndex Idx)
377 : Label(label), dl(std::move(L)), loc(Idx) {}
379 /// Does this UserLabel match the parameters?
380 bool match(const DILabel *L, const DILocation *IA,
381 const SlotIndex Index) const {
382 return Label == L && dl->getInlinedAt() == IA && loc == Index;
385 /// Recreate DBG_LABEL instruction from data structures.
386 void emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII);
388 /// Return DebugLoc of this UserLabel.
389 DebugLoc getDebugLoc() { return dl; }
391 void print(raw_ostream &, const TargetRegisterInfo *);
394 /// Implementation of the LiveDebugVariables pass.
396 LiveDebugVariables &pass;
397 LocMap::Allocator allocator;
398 MachineFunction *MF = nullptr;
400 const TargetRegisterInfo *TRI;
402 /// Whether emitDebugValues is called.
403 bool EmitDone = false;
405 /// Whether the machine function is modified during the pass.
406 bool ModifiedMF = false;
408 /// All allocated UserValue instances.
409 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
411 /// All allocated UserLabel instances.
412 SmallVector<std::unique_ptr<UserLabel>, 2> userLabels;
414 /// Map virtual register to eq class leader.
415 using VRMap = DenseMap<unsigned, UserValue *>;
416 VRMap virtRegToEqClass;
418 /// Map user variable to eq class leader.
419 using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
422 /// Find or create a UserValue.
423 UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
426 /// Find the EC leader for VirtReg or null.
427 UserValue *lookupVirtReg(unsigned VirtReg);
429 /// Add DBG_VALUE instruction to our maps.
431 /// \param MI DBG_VALUE instruction
432 /// \param Idx Last valid SLotIndex before instruction.
434 /// \returns True if the DBG_VALUE instruction should be deleted.
435 bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
437 /// Add DBG_LABEL instruction to UserLabel.
439 /// \param MI DBG_LABEL instruction
440 /// \param Idx Last valid SlotIndex before instruction.
442 /// \returns True if the DBG_LABEL instruction should be deleted.
443 bool handleDebugLabel(MachineInstr &MI, SlotIndex Idx);
445 /// Collect and erase all DBG_VALUE instructions, adding a UserValue def
446 /// for each instruction.
448 /// \param mf MachineFunction to be scanned.
450 /// \returns True if any debug values were found.
451 bool collectDebugValues(MachineFunction &mf);
453 /// Compute the live intervals of all user values after collecting all
454 /// their def points.
455 void computeIntervals();
458 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
460 bool runOnMachineFunction(MachineFunction &mf);
462 /// Release all memory.
467 virtRegToEqClass.clear();
469 // Make sure we call emitDebugValues if the machine function was modified.
470 assert((!ModifiedMF || EmitDone) &&
471 "Dbg values are not emitted in LDV");
476 /// Map virtual register to an equivalence class.
477 void mapVirtReg(unsigned VirtReg, UserValue *EC);
479 /// Replace all references to OldReg with NewRegs.
480 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
482 /// Recreate DBG_VALUE instruction from data structures.
483 void emitDebugValues(VirtRegMap *VRM);
485 void print(raw_ostream&);
488 } // end anonymous namespace
490 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
491 static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
492 const LLVMContext &Ctx) {
496 auto *Scope = cast<DIScope>(DL.getScope());
497 // Omit the directory, because it's likely to be long and uninteresting.
498 CommentOS << Scope->getFilename();
499 CommentOS << ':' << DL.getLine();
500 if (DL.getCol() != 0)
501 CommentOS << ':' << DL.getCol();
503 DebugLoc InlinedAtDL = DL.getInlinedAt();
508 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
512 static void printExtendedName(raw_ostream &OS, const DINode *Node,
513 const DILocation *DL) {
514 const LLVMContext &Ctx = Node->getContext();
517 if (const auto *V = dyn_cast<const DILocalVariable>(Node)) {
520 } else if (const auto *L = dyn_cast<const DILabel>(Node)) {
526 OS << Res << "," << Line;
527 auto *InlinedAt = DL ? DL->getInlinedAt() : nullptr;
529 if (DebugLoc InlinedAtDL = InlinedAt) {
531 printDebugLoc(InlinedAtDL, OS, Ctx);
537 void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
539 printExtendedName(OS, Variable, dl);
542 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
543 OS << " [" << I.start() << ';' << I.stop() << "):";
544 if (I.value().isUndef())
547 OS << I.value().locNo();
548 if (I.value().wasIndirect())
552 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
553 OS << " Loc" << i << '=';
554 locations[i].print(OS, TRI);
559 void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
561 printExtendedName(OS, Label, dl);
568 void LDVImpl::print(raw_ostream &OS) {
569 OS << "********** DEBUG VARIABLES **********\n";
570 for (auto &userValue : userValues)
571 userValue->print(OS, TRI);
572 OS << "********** DEBUG LABELS **********\n";
573 for (auto &userLabel : userLabels)
574 userLabel->print(OS, TRI);
578 void UserValue::mapVirtRegs(LDVImpl *LDV) {
579 for (unsigned i = 0, e = locations.size(); i != e; ++i)
580 if (locations[i].isReg() &&
581 Register::isVirtualRegister(locations[i].getReg()))
582 LDV->mapVirtReg(locations[i].getReg(), this);
585 UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
586 const DIExpression *Expr, const DebugLoc &DL) {
587 UserValue *&Leader = userVarMap[Var];
589 UserValue *UV = Leader->getLeader();
591 for (; UV; UV = UV->getNext())
592 if (UV->match(Var, Expr, DL->getInlinedAt()))
596 userValues.push_back(
597 std::make_unique<UserValue>(Var, Expr, DL, allocator));
598 UserValue *UV = userValues.back().get();
599 Leader = UserValue::merge(Leader, UV);
603 void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
604 assert(Register::isVirtualRegister(VirtReg) && "Only map VirtRegs");
605 UserValue *&Leader = virtRegToEqClass[VirtReg];
606 Leader = UserValue::merge(Leader, EC);
609 UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
610 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
611 return UV->getLeader();
615 bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
616 // DBG_VALUE loc, offset, variable
617 if (MI.getNumOperands() != 4 ||
618 !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
619 !MI.getOperand(2).isMetadata()) {
620 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
624 // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual
625 // register that hasn't been defined yet. If we do not remove those here, then
626 // the re-insertion of the DBG_VALUE instruction after register allocation
627 // will be incorrect.
628 // TODO: If earlier passes are corrected to generate sane debug information
629 // (and if the machine verifier is improved to catch this), then these checks
630 // could be removed or replaced by asserts.
631 bool Discard = false;
632 if (MI.getOperand(0).isReg() &&
633 Register::isVirtualRegister(MI.getOperand(0).getReg())) {
634 const Register Reg = MI.getOperand(0).getReg();
635 if (!LIS->hasInterval(Reg)) {
636 // The DBG_VALUE is described by a virtual register that does not have a
637 // live interval. Discard the DBG_VALUE.
639 LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx
642 // The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg
643 // is defined dead at Idx (where Idx is the slot index for the instruction
644 // preceding the DBG_VALUE).
645 const LiveInterval &LI = LIS->getInterval(Reg);
646 LiveQueryResult LRQ = LI.Query(Idx);
647 if (!LRQ.valueOutOrDead()) {
648 // We have found a DBG_VALUE with the value in a virtual register that
649 // is not live. Discard the DBG_VALUE.
651 LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx
657 // Get or create the UserValue for (variable,offset) here.
658 bool IsIndirect = MI.getOperand(1).isImm();
660 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
661 const DILocalVariable *Var = MI.getDebugVariable();
662 const DIExpression *Expr = MI.getDebugExpression();
664 getUserValue(Var, Expr, MI.getDebugLoc());
666 UV->addDef(Idx, MI.getOperand(0), IsIndirect);
668 MachineOperand MO = MachineOperand::CreateReg(0U, false);
670 UV->addDef(Idx, MO, false);
675 bool LDVImpl::handleDebugLabel(MachineInstr &MI, SlotIndex Idx) {
677 if (MI.getNumOperands() != 1 || !MI.getOperand(0).isMetadata()) {
678 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
682 // Get or create the UserLabel for label here.
683 const DILabel *Label = MI.getDebugLabel();
684 const DebugLoc &DL = MI.getDebugLoc();
686 for (auto const &L : userLabels) {
687 if (L->match(Label, DL->getInlinedAt(), Idx)) {
693 userLabels.push_back(std::make_unique<UserLabel>(Label, DL, Idx));
698 bool LDVImpl::collectDebugValues(MachineFunction &mf) {
699 bool Changed = false;
700 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
702 MachineBasicBlock *MBB = &*MFI;
703 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
705 // Use the first debug instruction in the sequence to get a SlotIndex
706 // for following consecutive debug instructions.
707 if (!MBBI->isDebugInstr()) {
711 // Debug instructions has no slot index. Use the previous
712 // non-debug instruction's SlotIndex as its SlotIndex.
715 ? LIS->getMBBStartIdx(MBB)
716 : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
717 // Handle consecutive debug instructions with the same slot index.
719 // Only handle DBG_VALUE in handleDebugValue(). Skip all other
720 // kinds of debug instructions.
721 if ((MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) ||
722 (MBBI->isDebugLabel() && handleDebugLabel(*MBBI, Idx))) {
723 MBBI = MBB->erase(MBBI);
727 } while (MBBI != MBBE && MBBI->isDebugInstr());
733 void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR,
734 const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
735 LiveIntervals &LIS) {
736 SlotIndex Start = Idx;
737 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
738 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
739 LocMap::iterator I = locInts.find(Start);
741 // Limit to VNI's live range.
744 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
745 if (!Segment || Segment->valno != VNI) {
747 Kills->push_back(Start);
750 if (Segment->end < Stop) {
756 // There could already be a short def at Start.
757 if (I.valid() && I.start() <= Start) {
758 // Stop when meeting a different location or an already extended interval.
759 Start = Start.getNextSlot();
760 if (I.value() != Loc || I.stop() != Start)
762 // This is a one-slot placeholder. Just skip it.
766 // Limited by the next def.
767 if (I.valid() && I.start() < Stop)
769 // Limited by VNI's live range.
770 else if (!ToEnd && Kills)
771 Kills->push_back(Stop);
774 I.insert(Start, Stop, Loc);
777 void UserValue::addDefsFromCopies(
778 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
779 const SmallVectorImpl<SlotIndex> &Kills,
780 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
781 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
784 // Don't track copies from physregs, there are too many uses.
785 if (!Register::isVirtualRegister(LI->reg))
788 // Collect all the (vreg, valno) pairs that are copies of LI.
789 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
790 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
791 MachineInstr *MI = MO.getParent();
792 // Copies of the full value.
793 if (MO.getSubReg() || !MI->isCopy())
795 Register DstReg = MI->getOperand(0).getReg();
797 // Don't follow copies to physregs. These are usually setting up call
798 // arguments, and the argument registers are always call clobbered. We are
799 // better off in the source register which could be a callee-saved register,
800 // or it could be spilled.
801 if (!Register::isVirtualRegister(DstReg))
804 // Is LocNo extended to reach this copy? If not, another def may be blocking
805 // it, or we are looking at a wrong value of LI.
806 SlotIndex Idx = LIS.getInstructionIndex(*MI);
807 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
808 if (!I.valid() || I.value().locNo() != LocNo)
811 if (!LIS.hasInterval(DstReg))
813 LiveInterval *DstLI = &LIS.getInterval(DstReg);
814 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
815 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
816 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
819 if (CopyValues.empty())
822 LLVM_DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI
825 // Try to add defs of the copied values for each kill point.
826 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
827 SlotIndex Idx = Kills[i];
828 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
829 LiveInterval *DstLI = CopyValues[j].first;
830 const VNInfo *DstVNI = CopyValues[j].second;
831 if (DstLI->getVNInfoAt(Idx) != DstVNI)
833 // Check that there isn't already a def at Idx
834 LocMap::iterator I = locInts.find(Idx);
835 if (I.valid() && I.start() <= Idx)
837 LLVM_DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
838 << DstVNI->id << " in " << *DstLI << '\n');
839 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
840 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
841 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
842 DbgValueLocation NewLoc(LocNo, WasIndirect);
843 I.insert(Idx, Idx.getNextSlot(), NewLoc);
844 NewDefs.push_back(std::make_pair(Idx, NewLoc));
850 void UserValue::computeIntervals(MachineRegisterInfo &MRI,
851 const TargetRegisterInfo &TRI,
852 LiveIntervals &LIS, LexicalScopes &LS) {
853 SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs;
855 // Collect all defs to be extended (Skipping undefs).
856 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
857 if (!I.value().isUndef())
858 Defs.push_back(std::make_pair(I.start(), I.value()));
860 // Extend all defs, and possibly add new ones along the way.
861 for (unsigned i = 0; i != Defs.size(); ++i) {
862 SlotIndex Idx = Defs[i].first;
863 DbgValueLocation Loc = Defs[i].second;
864 const MachineOperand &LocMO = locations[Loc.locNo()];
866 if (!LocMO.isReg()) {
867 extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS);
871 // Register locations are constrained to where the register value is live.
872 if (Register::isVirtualRegister(LocMO.getReg())) {
873 LiveInterval *LI = nullptr;
874 const VNInfo *VNI = nullptr;
875 if (LIS.hasInterval(LocMO.getReg())) {
876 LI = &LIS.getInterval(LocMO.getReg());
877 VNI = LI->getVNInfoAt(Idx);
879 SmallVector<SlotIndex, 16> Kills;
880 extendDef(Idx, Loc, LI, VNI, &Kills, LIS);
881 // FIXME: Handle sub-registers in addDefsFromCopies. The problem is that
882 // if the original location for example is %vreg0:sub_hi, and we find a
883 // full register copy in addDefsFromCopies (at the moment it only handles
884 // full register copies), then we must add the sub1 sub-register index to
885 // the new location. However, that is only possible if the new virtual
886 // register is of the same regclass (or if there is an equivalent
887 // sub-register in that regclass). For now, simply skip handling copies if
888 // a sub-register is involved.
889 if (LI && !LocMO.getSubReg())
890 addDefsFromCopies(LI, Loc.locNo(), Loc.wasIndirect(), Kills, Defs, MRI,
895 // For physregs, we only mark the start slot idx. DwarfDebug will see it
896 // as if the DBG_VALUE is valid up until the end of the basic block, or
897 // the next def of the physical register. So we do not need to extend the
898 // range. It might actually happen that the DBG_VALUE is the last use of
899 // the physical register (e.g. if this is an unused input argument to a
903 // The computed intervals may extend beyond the range of the debug
904 // location's lexical scope. In this case, splitting of an interval
905 // can result in an interval outside of the scope being created,
906 // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
907 // this, trim the intervals to the lexical scope.
909 LexicalScope *Scope = LS.findLexicalScope(dl);
914 LocMap::iterator I = locInts.begin();
916 // Iterate over the lexical scope ranges. Each time round the loop
917 // we check the intervals for overlap with the end of the previous
918 // range and the start of the next. The first range is handled as
919 // a special case where there is no PrevEnd.
920 for (const InsnRange &Range : Scope->getRanges()) {
921 SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
922 SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
924 // Variable locations at the first instruction of a block should be
925 // based on the block's SlotIndex, not the first instruction's index.
926 if (Range.first == Range.first->getParent()->begin())
927 RStart = LIS.getSlotIndexes()->getIndexBefore(*Range.first);
929 // At the start of each iteration I has been advanced so that
930 // I.stop() >= PrevEnd. Check for overlap.
931 if (PrevEnd && I.start() < PrevEnd) {
932 SlotIndex IStop = I.stop();
933 DbgValueLocation Loc = I.value();
935 // Stop overlaps previous end - trim the end of the interval to
937 I.setStopUnchecked(PrevEnd);
940 // If the interval also overlaps the start of the "next" (i.e.
941 // current) range create a new interval for the remainder (which
942 // may be further trimmed).
944 I.insert(RStart, IStop, Loc);
947 // Advance I so that I.stop() >= RStart, and check for overlap.
952 if (I.start() < RStart) {
953 // Interval start overlaps range - trim to the scope range.
954 I.setStartUnchecked(RStart);
955 // Remember that this interval was trimmed.
956 trimmedDefs.insert(RStart);
959 // The end of a lexical scope range is the last instruction in the
960 // range. To convert to an interval we need the index of the
961 // instruction after it.
962 REnd = REnd.getNextIndex();
964 // Advance I to first interval outside current range.
972 // Check for overlap with end of final range.
973 if (PrevEnd && I.start() < PrevEnd)
974 I.setStopUnchecked(PrevEnd);
977 void LDVImpl::computeIntervals() {
981 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
982 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS);
983 userValues[i]->mapVirtRegs(this);
987 bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
990 LIS = &pass.getAnalysis<LiveIntervals>();
991 TRI = mf.getSubtarget().getRegisterInfo();
992 LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
993 << mf.getName() << " **********\n");
995 bool Changed = collectDebugValues(mf);
997 LLVM_DEBUG(print(dbgs()));
998 ModifiedMF = Changed;
1002 static void removeDebugValues(MachineFunction &mf) {
1003 for (MachineBasicBlock &MBB : mf) {
1004 for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
1005 if (!MBBI->isDebugValue()) {
1009 MBBI = MBB.erase(MBBI);
1014 bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
1017 if (!mf.getFunction().getSubprogram()) {
1018 removeDebugValues(mf);
1022 pImpl = new LDVImpl(this);
1023 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
1026 void LiveDebugVariables::releaseMemory() {
1028 static_cast<LDVImpl*>(pImpl)->clear();
1031 LiveDebugVariables::~LiveDebugVariables() {
1033 delete static_cast<LDVImpl*>(pImpl);
1036 //===----------------------------------------------------------------------===//
1037 // Live Range Splitting
1038 //===----------------------------------------------------------------------===//
1041 UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
1042 LiveIntervals& LIS) {
1044 dbgs() << "Splitting Loc" << OldLocNo << '\t';
1045 print(dbgs(), nullptr);
1047 bool DidChange = false;
1048 LocMap::iterator LocMapI;
1049 LocMapI.setMap(locInts);
1050 for (unsigned i = 0; i != NewRegs.size(); ++i) {
1051 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
1055 // Don't allocate the new LocNo until it is needed.
1056 unsigned NewLocNo = UndefLocNo;
1058 // Iterate over the overlaps between locInts and LI.
1059 LocMapI.find(LI->beginIndex());
1060 if (!LocMapI.valid())
1062 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
1063 LiveInterval::iterator LIE = LI->end();
1064 while (LocMapI.valid() && LII != LIE) {
1065 // At this point, we know that LocMapI.stop() > LII->start.
1066 LII = LI->advanceTo(LII, LocMapI.start());
1070 // Now LII->end > LocMapI.start(). Do we have an overlap?
1071 if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) {
1072 // Overlapping correct location. Allocate NewLocNo now.
1073 if (NewLocNo == UndefLocNo) {
1074 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
1075 MO.setSubReg(locations[OldLocNo].getSubReg());
1076 NewLocNo = getLocationNo(MO);
1080 SlotIndex LStart = LocMapI.start();
1081 SlotIndex LStop = LocMapI.stop();
1082 DbgValueLocation OldLoc = LocMapI.value();
1084 // Trim LocMapI down to the LII overlap.
1085 if (LStart < LII->start)
1086 LocMapI.setStartUnchecked(LII->start);
1087 if (LStop > LII->end)
1088 LocMapI.setStopUnchecked(LII->end);
1090 // Change the value in the overlap. This may trigger coalescing.
1091 LocMapI.setValue(OldLoc.changeLocNo(NewLocNo));
1093 // Re-insert any removed OldLocNo ranges.
1094 if (LStart < LocMapI.start()) {
1095 LocMapI.insert(LStart, LocMapI.start(), OldLoc);
1097 assert(LocMapI.valid() && "Unexpected coalescing");
1099 if (LStop > LocMapI.stop()) {
1101 LocMapI.insert(LII->end, LStop, OldLoc);
1106 // Advance to the next overlap.
1107 if (LII->end < LocMapI.stop()) {
1110 LocMapI.advanceTo(LII->start);
1113 if (!LocMapI.valid())
1115 LII = LI->advanceTo(LII, LocMapI.start());
1120 // Finally, remove OldLocNo unless it is still used by some interval in the
1121 // locInts map. One case when OldLocNo still is in use is when the register
1122 // has been spilled. In such situations the spilled register is kept as a
1123 // location until rewriteLocations is called (VirtRegMap is mapping the old
1124 // register to the spill slot). So for a while we can have locations that map
1125 // to virtual registers that have been removed from both the MachineFunction
1126 // and from LiveIntervals.
1127 removeLocationIfUnused(OldLocNo);
1130 dbgs() << "Split result: \t";
1131 print(dbgs(), nullptr);
1137 UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
1138 LiveIntervals &LIS) {
1139 bool DidChange = false;
1140 // Split locations referring to OldReg. Iterate backwards so splitLocation can
1141 // safely erase unused locations.
1142 for (unsigned i = locations.size(); i ; --i) {
1143 unsigned LocNo = i-1;
1144 const MachineOperand *Loc = &locations[LocNo];
1145 if (!Loc->isReg() || Loc->getReg() != OldReg)
1147 DidChange |= splitLocation(LocNo, NewRegs, LIS);
1152 void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
1153 bool DidChange = false;
1154 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
1155 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
1160 // Map all of the new virtual registers.
1161 UserValue *UV = lookupVirtReg(OldReg);
1162 for (unsigned i = 0; i != NewRegs.size(); ++i)
1163 mapVirtReg(NewRegs[i], UV);
1166 void LiveDebugVariables::
1167 splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
1169 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
1172 void UserValue::rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
1173 const TargetInstrInfo &TII,
1174 const TargetRegisterInfo &TRI,
1175 SpillOffsetMap &SpillOffsets) {
1176 // Build a set of new locations with new numbers so we can coalesce our
1177 // IntervalMap if two vreg intervals collapse to the same physical location.
1178 // Use MapVector instead of SetVector because MapVector::insert returns the
1179 // position of the previously or newly inserted element. The boolean value
1180 // tracks if the location was produced by a spill.
1181 // FIXME: This will be problematic if we ever support direct and indirect
1182 // frame index locations, i.e. expressing both variables in memory and
1183 // 'int x, *px = &x'. The "spilled" bit must become part of the location.
1184 MapVector<MachineOperand, std::pair<bool, unsigned>> NewLocations;
1185 SmallVector<unsigned, 4> LocNoMap(locations.size());
1186 for (unsigned I = 0, E = locations.size(); I != E; ++I) {
1187 bool Spilled = false;
1188 unsigned SpillOffset = 0;
1189 MachineOperand Loc = locations[I];
1190 // Only virtual registers are rewritten.
1191 if (Loc.isReg() && Loc.getReg() &&
1192 Register::isVirtualRegister(Loc.getReg())) {
1193 Register VirtReg = Loc.getReg();
1194 if (VRM.isAssignedReg(VirtReg) &&
1195 Register::isPhysicalRegister(VRM.getPhys(VirtReg))) {
1196 // This can create a %noreg operand in rare cases when the sub-register
1197 // index is no longer available. That means the user value is in a
1198 // non-existent sub-register, and %noreg is exactly what we want.
1199 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
1200 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
1201 // Retrieve the stack slot offset.
1203 const MachineRegisterInfo &MRI = MF.getRegInfo();
1204 const TargetRegisterClass *TRC = MRI.getRegClass(VirtReg);
1205 bool Success = TII.getStackSlotRange(TRC, Loc.getSubReg(), SpillSize,
1208 // FIXME: Invalidate the location if the offset couldn't be calculated.
1211 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
1219 // Insert this location if it doesn't already exist and record a mapping
1220 // from the old number to the new number.
1221 auto InsertResult = NewLocations.insert({Loc, {Spilled, SpillOffset}});
1222 unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
1223 LocNoMap[I] = NewLocNo;
1226 // Rewrite the locations and record the stack slot offsets for spills.
1228 SpillOffsets.clear();
1229 for (auto &Pair : NewLocations) {
1231 unsigned SpillOffset;
1232 std::tie(Spilled, SpillOffset) = Pair.second;
1233 locations.push_back(Pair.first);
1235 unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
1236 SpillOffsets[NewLocNo] = SpillOffset;
1240 // Update the interval map, but only coalesce left, since intervals to the
1241 // right use the old location numbers. This should merge two contiguous
1242 // DBG_VALUE intervals with different vregs that were allocated to the same
1243 // physical register.
1244 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
1245 DbgValueLocation Loc = I.value();
1246 // Undef values don't exist in locations (and thus not in LocNoMap either)
1247 // so skip over them. See getLocationNo().
1250 unsigned NewLocNo = LocNoMap[Loc.locNo()];
1251 I.setValueUnchecked(Loc.changeLocNo(NewLocNo));
1252 I.setStart(I.start());
1256 /// Find an iterator for inserting a DBG_VALUE instruction.
1257 static MachineBasicBlock::iterator
1258 findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
1259 LiveIntervals &LIS) {
1260 SlotIndex Start = LIS.getMBBStartIdx(MBB);
1261 Idx = Idx.getBaseIndex();
1263 // Try to find an insert location by going backwards from Idx.
1265 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1266 // We've reached the beginning of MBB.
1268 MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
1271 Idx = Idx.getPrevIndex();
1274 // Don't insert anything after the first terminator, though.
1275 return MI->isTerminator() ? MBB->getFirstTerminator() :
1276 std::next(MachineBasicBlock::iterator(MI));
1279 /// Find an iterator for inserting the next DBG_VALUE instruction
1280 /// (or end if no more insert locations found).
1281 static MachineBasicBlock::iterator
1282 findNextInsertLocation(MachineBasicBlock *MBB,
1283 MachineBasicBlock::iterator I,
1284 SlotIndex StopIdx, MachineOperand &LocMO,
1286 const TargetRegisterInfo &TRI) {
1288 return MBB->instr_end();
1289 Register Reg = LocMO.getReg();
1291 // Find the next instruction in the MBB that define the register Reg.
1292 while (I != MBB->end() && !I->isTerminator()) {
1293 if (!LIS.isNotInMIMap(*I) &&
1294 SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I)))
1296 if (I->definesRegister(Reg, &TRI))
1297 // The insert location is directly after the instruction/bundle.
1298 return std::next(I);
1304 void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
1305 SlotIndex StopIdx, DbgValueLocation Loc,
1306 bool Spilled, unsigned SpillOffset,
1307 LiveIntervals &LIS, const TargetInstrInfo &TII,
1308 const TargetRegisterInfo &TRI) {
1309 SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB);
1310 // Only search within the current MBB.
1311 StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx;
1312 MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS);
1313 // Undef values don't exist in locations so create new "noreg" register MOs
1314 // for them. See getLocationNo().
1315 MachineOperand MO = !Loc.isUndef() ?
1316 locations[Loc.locNo()] :
1317 MachineOperand::CreateReg(/* Reg */ 0, /* isDef */ false, /* isImp */ false,
1318 /* isKill */ false, /* isDead */ false,
1319 /* isUndef */ false, /* isEarlyClobber */ false,
1320 /* SubReg */ 0, /* isDebug */ true);
1322 ++NumInsertedDebugValues;
1324 assert(cast<DILocalVariable>(Variable)
1325 ->isValidLocationForIntrinsic(getDebugLoc()) &&
1326 "Expected inlined-at fields to agree");
1328 // If the location was spilled, the new DBG_VALUE will be indirect. If the
1329 // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
1330 // that the original virtual register was a pointer. Also, add the stack slot
1331 // offset for the spilled register to the expression.
1332 const DIExpression *Expr = Expression;
1333 uint8_t DIExprFlags = DIExpression::ApplyOffset;
1334 bool IsIndirect = Loc.wasIndirect();
1337 DIExprFlags |= DIExpression::DerefAfter;
1339 DIExpression::prepend(Expr, DIExprFlags, SpillOffset);
1343 assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index");
1346 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
1347 IsIndirect, MO, Variable, Expr);
1349 // Continue and insert DBG_VALUES after every redefinition of register
1350 // associated with the debug value within the range
1351 I = findNextInsertLocation(MBB, I, StopIdx, MO, LIS, TRI);
1352 } while (I != MBB->end());
1355 void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
1357 const TargetInstrInfo &TII) {
1358 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
1359 ++NumInsertedDebugLabels;
1360 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_LABEL))
1361 .addMetadata(Label);
1364 void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
1365 const TargetInstrInfo &TII,
1366 const TargetRegisterInfo &TRI,
1367 const SpillOffsetMap &SpillOffsets) {
1368 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
1370 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1371 SlotIndex Start = I.start();
1372 SlotIndex Stop = I.stop();
1373 DbgValueLocation Loc = I.value();
1375 !Loc.isUndef() ? SpillOffsets.find(Loc.locNo()) : SpillOffsets.end();
1376 bool Spilled = SpillIt != SpillOffsets.end();
1377 unsigned SpillOffset = Spilled ? SpillIt->second : 0;
1379 // If the interval start was trimmed to the lexical scope insert the
1380 // DBG_VALUE at the previous index (otherwise it appears after the
1381 // first instruction in the range).
1382 if (trimmedDefs.count(Start))
1383 Start = Start.getPrevIndex();
1385 LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo());
1386 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1387 SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
1389 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1390 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1392 // This interval may span multiple basic blocks.
1393 // Insert a DBG_VALUE into each one.
1394 while (Stop > MBBEnd) {
1395 // Move to the next block.
1399 MBBEnd = LIS.getMBBEndIdx(&*MBB);
1400 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1401 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1404 LLVM_DEBUG(dbgs() << '\n');
1412 void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII) {
1413 LLVM_DEBUG(dbgs() << "\t" << loc);
1414 MachineFunction::iterator MBB = LIS.getMBBFromIndex(loc)->getIterator();
1416 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB));
1417 insertDebugLabel(&*MBB, loc, LIS, TII);
1419 LLVM_DEBUG(dbgs() << '\n');
1422 void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
1423 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
1426 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1427 SpillOffsetMap SpillOffsets;
1428 for (auto &userValue : userValues) {
1429 LLVM_DEBUG(userValue->print(dbgs(), TRI));
1430 userValue->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets);
1431 userValue->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets);
1433 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG LABELS **********\n");
1434 for (auto &userLabel : userLabels) {
1435 LLVM_DEBUG(userLabel->print(dbgs(), TRI));
1436 userLabel->emitDebugLabel(*LIS, *TII);
1441 void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
1443 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1446 bool LiveDebugVariables::doInitialization(Module &M) {
1447 return Pass::doInitialization(M);
1450 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1451 LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
1453 static_cast<LDVImpl*>(pImpl)->print(dbgs());