xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/LiveDebugVariables.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
1 //===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LiveDebugVariables analysis.
10 //
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.
14 //
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.
18 //
19 //===----------------------------------------------------------------------===//
20 
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/MC/MCRegisterInfo.h"
53 #include "llvm/Pass.h"
54 #include "llvm/Support/Casting.h"
55 #include "llvm/Support/CommandLine.h"
56 #include "llvm/Support/Compiler.h"
57 #include "llvm/Support/Debug.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include <algorithm>
60 #include <cassert>
61 #include <iterator>
62 #include <memory>
63 #include <utility>
64 
65 using namespace llvm;
66 
67 #define DEBUG_TYPE "livedebugvars"
68 
69 static cl::opt<bool>
70 EnableLDV("live-debug-variables", cl::init(true),
71           cl::desc("Enable the live debug variables pass"), cl::Hidden);
72 
73 STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
74 STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted");
75 
76 char LiveDebugVariables::ID = 0;
77 
78 INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
79                 "Debug Variable Analysis", false, false)
80 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
81 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
82 INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
83                 "Debug Variable Analysis", false, false)
84 
85 void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
86   AU.addRequired<MachineDominatorTree>();
87   AU.addRequiredTransitive<LiveIntervals>();
88   AU.setPreservesAll();
89   MachineFunctionPass::getAnalysisUsage(AU);
90 }
91 
92 LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) {
93   initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
94 }
95 
96 enum : unsigned { UndefLocNo = ~0U };
97 
98 /// Describes a location by number along with some flags about the original
99 /// usage of the location.
100 class DbgValueLocation {
101 public:
102   DbgValueLocation(unsigned LocNo)
103       : LocNo(LocNo) {
104     static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing");
105     assert(locNo() == LocNo && "location truncation");
106   }
107 
108   DbgValueLocation() : LocNo(0) {}
109 
110   unsigned locNo() const {
111     // Fix up the undef location number, which gets truncated.
112     return LocNo == INT_MAX ? UndefLocNo : LocNo;
113   }
114   bool isUndef() const { return locNo() == UndefLocNo; }
115 
116   DbgValueLocation changeLocNo(unsigned NewLocNo) const {
117     return DbgValueLocation(NewLocNo);
118   }
119 
120   friend inline bool operator==(const DbgValueLocation &LHS,
121                                 const DbgValueLocation &RHS) {
122     return LHS.LocNo == RHS.LocNo;
123   }
124 
125   friend inline bool operator!=(const DbgValueLocation &LHS,
126                                 const DbgValueLocation &RHS) {
127     return !(LHS == RHS);
128   }
129 
130 private:
131   unsigned LocNo;
132 };
133 
134 /// Map of where a user value is live, and its location.
135 using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>;
136 
137 /// Map of stack slot offsets for spilled locations.
138 /// Non-spilled locations are not added to the map.
139 using SpillOffsetMap = DenseMap<unsigned, unsigned>;
140 
141 namespace {
142 
143 class LDVImpl;
144 
145 /// A UserValue is uniquely identified by the source variable it refers to
146 /// (Variable), the expression describing how to get the value (Expression) and
147 /// the specific usage (InlinedAt). InlinedAt differentiates both between
148 /// inline and non-inline functions, and multiple inlined instances in the same
149 /// scope. FIXME: The only part of the Expression which matters for UserValue
150 /// identification is the fragment part.
151 class UserValueIdentity {
152 private:
153   /// The debug info variable we are part of.
154   const DILocalVariable *Variable;
155   /// Any complex address expression.
156   const DIExpression *Expression;
157   /// Function usage identification.
158   const DILocation *InlinedAt;
159 
160 public:
161   UserValueIdentity(const DILocalVariable *Var, const DIExpression *Expr,
162                     const DILocation *IA)
163       : Variable(Var), Expression(Expr), InlinedAt(IA) {}
164 
165   bool match(const DILocalVariable *Var, const DIExpression *Expr,
166              const DILocation *IA) const {
167     // FIXME: The fragment should be part of the identity, but not
168     // other things in the expression like stack values.
169     return Var == Variable && Expr == Expression && IA == InlinedAt;
170   }
171 
172   bool match(const UserValueIdentity &Other) const {
173     return match(Other.Variable, Other.Expression, Other.InlinedAt);
174   }
175 
176   unsigned hash_value() const {
177     return hash_combine(Variable, Expression, InlinedAt);
178   }
179 };
180 
181 /// A user value is a part of a debug info user variable.
182 ///
183 /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
184 /// holds part of a user variable. The part is identified by a byte offset.
185 class UserValue {
186   const DILocalVariable *Variable; ///< The debug info variable we are part of.
187   const DIExpression *Expression; ///< Any complex address expression.
188   DebugLoc dl;            ///< The debug location for the variable. This is
189                           ///< used by dwarf writer to find lexical scope.
190 
191   /// Numbered locations referenced by locmap.
192   SmallVector<MachineOperand, 4> locations;
193 
194   /// Map of slot indices where this value is live.
195   LocMap locInts;
196 
197   /// Insert a DBG_VALUE into MBB at Idx for LocNo.
198   void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
199                         SlotIndex StopIdx, DbgValueLocation Loc, bool Spilled,
200                         unsigned SpillOffset, LiveIntervals &LIS,
201                         const TargetInstrInfo &TII,
202                         const TargetRegisterInfo &TRI);
203 
204   /// Replace OldLocNo ranges with NewRegs ranges where NewRegs
205   /// is live. Returns true if any changes were made.
206   bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
207                      LiveIntervals &LIS);
208 
209 public:
210   UserValue(const UserValue &) = delete;
211 
212   /// Create a new UserValue.
213   UserValue(const DILocalVariable *var, const DIExpression *expr, DebugLoc L,
214             LocMap::Allocator &alloc)
215       : Variable(var), Expression(expr), dl(std::move(L)), locInts(alloc) {}
216 
217   UserValueIdentity getId() {
218     return UserValueIdentity(Variable, Expression, dl->getInlinedAt());
219   }
220 
221   /// Return the location number that matches Loc.
222   ///
223   /// For undef values we always return location number UndefLocNo without
224   /// inserting anything in locations. Since locations is a vector and the
225   /// location number is the position in the vector and UndefLocNo is ~0,
226   /// we would need a very big vector to put the value at the right position.
227   unsigned getLocationNo(const MachineOperand &LocMO) {
228     if (LocMO.isReg()) {
229       if (LocMO.getReg() == 0)
230         return UndefLocNo;
231       // For register locations we dont care about use/def and other flags.
232       for (unsigned i = 0, e = locations.size(); i != e; ++i)
233         if (locations[i].isReg() &&
234             locations[i].getReg() == LocMO.getReg() &&
235             locations[i].getSubReg() == LocMO.getSubReg())
236           return i;
237     } else
238       for (unsigned i = 0, e = locations.size(); i != e; ++i)
239         if (LocMO.isIdenticalTo(locations[i]))
240           return i;
241     locations.push_back(LocMO);
242     // We are storing a MachineOperand outside a MachineInstr.
243     locations.back().clearParent();
244     // Don't store def operands.
245     if (locations.back().isReg()) {
246       if (locations.back().isDef())
247         locations.back().setIsDead(false);
248       locations.back().setIsUse();
249     }
250     return locations.size() - 1;
251   }
252 
253   /// Ensure that all virtual register locations are mapped.
254   void mapVirtRegs(LDVImpl *LDV);
255 
256   /// Add a definition point to this value.
257   void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
258     DbgValueLocation Loc(getLocationNo(LocMO));
259     // Add a singular (Idx,Idx) -> Loc mapping.
260     LocMap::iterator I = locInts.find(Idx);
261     if (!I.valid() || I.start() != Idx)
262       I.insert(Idx, Idx.getNextSlot(), Loc);
263     else
264       // A later DBG_VALUE at the same SlotIndex overrides the old location.
265       I.setValue(Loc);
266   }
267 
268   /// Extend the current definition as far as possible down.
269   ///
270   /// Stop when meeting an existing def or when leaving the live
271   /// range of VNI. End points where VNI is no longer live are added to Kills.
272   ///
273   /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
274   /// data-flow analysis to propagate them beyond basic block boundaries.
275   ///
276   /// \param Idx Starting point for the definition.
277   /// \param Loc Location number to propagate.
278   /// \param LR Restrict liveness to where LR has the value VNI. May be null.
279   /// \param VNI When LR is not null, this is the value to restrict to.
280   /// \param [out] Kills Append end points of VNI's live range to Kills.
281   /// \param LIS Live intervals analysis.
282   void extendDef(SlotIndex Idx, DbgValueLocation Loc,
283                  LiveRange *LR, const VNInfo *VNI,
284                  SmallVectorImpl<SlotIndex> *Kills,
285                  LiveIntervals &LIS);
286 
287   /// The value in LI/LocNo may be copies to other registers. Determine if
288   /// any of the copies are available at the kill points, and add defs if
289   /// possible.
290   ///
291   /// \param LI Scan for copies of the value in LI->reg.
292   /// \param LocNo Location number of LI->reg.
293   /// \param Kills Points where the range of LocNo could be extended.
294   /// \param [in,out] NewDefs Append (Idx, LocNo) of inserted defs here.
295   void addDefsFromCopies(
296       LiveInterval *LI, unsigned LocNo,
297       const SmallVectorImpl<SlotIndex> &Kills,
298       SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
299       MachineRegisterInfo &MRI, LiveIntervals &LIS);
300 
301   /// Compute the live intervals of all locations after collecting all their
302   /// def points.
303   void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
304                         LiveIntervals &LIS, LexicalScopes &LS);
305 
306   /// Replace OldReg ranges with NewRegs ranges where NewRegs is
307   /// live. Returns true if any changes were made.
308   bool splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
309                      LiveIntervals &LIS);
310 
311   /// Rewrite virtual register locations according to the provided virtual
312   /// register map. Record the stack slot offsets for the locations that
313   /// were spilled.
314   void rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
315                         const TargetInstrInfo &TII,
316                         const TargetRegisterInfo &TRI,
317                         SpillOffsetMap &SpillOffsets);
318 
319   /// Recreate DBG_VALUE instruction from data structures.
320   void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
321                        const TargetInstrInfo &TII,
322                        const TargetRegisterInfo &TRI,
323                        const SpillOffsetMap &SpillOffsets);
324 
325   /// Return DebugLoc of this UserValue.
326   DebugLoc getDebugLoc() { return dl;}
327 
328   void print(raw_ostream &, const TargetRegisterInfo *);
329 };
330 } // namespace
331 
332 namespace llvm {
333 template <> struct DenseMapInfo<UserValueIdentity> {
334   static UserValueIdentity getEmptyKey() {
335     auto Key = DenseMapInfo<DILocalVariable *>::getEmptyKey();
336     return UserValueIdentity(Key, nullptr, nullptr);
337   }
338   static UserValueIdentity getTombstoneKey() {
339     auto Key = DenseMapInfo<DILocalVariable *>::getTombstoneKey();
340     return UserValueIdentity(Key, nullptr, nullptr);
341   }
342   static unsigned getHashValue(const UserValueIdentity &Val) {
343     return Val.hash_value();
344   }
345   static bool isEqual(const UserValueIdentity &LHS,
346                       const UserValueIdentity &RHS) {
347     return LHS.match(RHS);
348   }
349 };
350 } // namespace llvm
351 
352 namespace {
353 /// A user label is a part of a debug info user label.
354 class UserLabel {
355   const DILabel *Label; ///< The debug info label we are part of.
356   DebugLoc dl;          ///< The debug location for the label. This is
357                         ///< used by dwarf writer to find lexical scope.
358   SlotIndex loc;        ///< Slot used by the debug label.
359 
360   /// Insert a DBG_LABEL into MBB at Idx.
361   void insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
362                         LiveIntervals &LIS, const TargetInstrInfo &TII);
363 
364 public:
365   /// Create a new UserLabel.
366   UserLabel(const DILabel *label, DebugLoc L, SlotIndex Idx)
367       : Label(label), dl(std::move(L)), loc(Idx) {}
368 
369   /// Does this UserLabel match the parameters?
370   bool match(const DILabel *L, const DILocation *IA,
371              const SlotIndex Index) const {
372     return Label == L && dl->getInlinedAt() == IA && loc == Index;
373   }
374 
375   /// Recreate DBG_LABEL instruction from data structures.
376   void emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII);
377 
378   /// Return DebugLoc of this UserLabel.
379   DebugLoc getDebugLoc() { return dl; }
380 
381   void print(raw_ostream &, const TargetRegisterInfo *);
382 };
383 
384 /// Implementation of the LiveDebugVariables pass.
385 class LDVImpl {
386   LiveDebugVariables &pass;
387   LocMap::Allocator allocator;
388   MachineFunction *MF = nullptr;
389   LiveIntervals *LIS;
390   const TargetRegisterInfo *TRI;
391 
392   /// Whether emitDebugValues is called.
393   bool EmitDone = false;
394 
395   /// Whether the machine function is modified during the pass.
396   bool ModifiedMF = false;
397 
398   /// All allocated UserValue instances.
399   SmallVector<std::unique_ptr<UserValue>, 8> userValues;
400 
401   /// All allocated UserLabel instances.
402   SmallVector<std::unique_ptr<UserLabel>, 2> userLabels;
403 
404   /// Map virtual register to UserValues which use it.
405   using VRMap = DenseMap<unsigned, SmallVector<UserValue *, 4>>;
406   VRMap VirtRegToUserVals;
407 
408   /// Map unique UserValue identity to UserValue.
409   using UVMap = DenseMap<UserValueIdentity, UserValue *>;
410   UVMap UserVarMap;
411 
412   /// Find or create a UserValue.
413   UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
414                           const DebugLoc &DL);
415 
416   /// Find the UserValues for VirtReg or null.
417   SmallVectorImpl<UserValue *> *lookupVirtReg(unsigned VirtReg);
418 
419   /// Add DBG_VALUE instruction to our maps.
420   ///
421   /// \param MI DBG_VALUE instruction
422   /// \param Idx Last valid SLotIndex before instruction.
423   ///
424   /// \returns True if the DBG_VALUE instruction should be deleted.
425   bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
426 
427   /// Add DBG_LABEL instruction to UserLabel.
428   ///
429   /// \param MI DBG_LABEL instruction
430   /// \param Idx Last valid SlotIndex before instruction.
431   ///
432   /// \returns True if the DBG_LABEL instruction should be deleted.
433   bool handleDebugLabel(MachineInstr &MI, SlotIndex Idx);
434 
435   /// Collect and erase all DBG_VALUE instructions, adding a UserValue def
436   /// for each instruction.
437   ///
438   /// \param mf MachineFunction to be scanned.
439   ///
440   /// \returns True if any debug values were found.
441   bool collectDebugValues(MachineFunction &mf);
442 
443   /// Compute the live intervals of all user values after collecting all
444   /// their def points.
445   void computeIntervals();
446 
447 public:
448   LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
449 
450   bool runOnMachineFunction(MachineFunction &mf);
451 
452   /// Release all memory.
453   void clear() {
454     MF = nullptr;
455     userValues.clear();
456     userLabels.clear();
457     VirtRegToUserVals.clear();
458     UserVarMap.clear();
459     // Make sure we call emitDebugValues if the machine function was modified.
460     assert((!ModifiedMF || EmitDone) &&
461            "Dbg values are not emitted in LDV");
462     EmitDone = false;
463     ModifiedMF = false;
464   }
465 
466   /// Map virtual register to a UserValue.
467   void mapVirtReg(unsigned VirtReg, UserValue *UV);
468 
469   /// Replace all references to OldReg with NewRegs.
470   void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
471 
472   /// Recreate DBG_VALUE instruction from data structures.
473   void emitDebugValues(VirtRegMap *VRM);
474 
475   void print(raw_ostream&);
476 };
477 
478 } // end anonymous namespace
479 
480 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
481 static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
482                           const LLVMContext &Ctx) {
483   if (!DL)
484     return;
485 
486   auto *Scope = cast<DIScope>(DL.getScope());
487   // Omit the directory, because it's likely to be long and uninteresting.
488   CommentOS << Scope->getFilename();
489   CommentOS << ':' << DL.getLine();
490   if (DL.getCol() != 0)
491     CommentOS << ':' << DL.getCol();
492 
493   DebugLoc InlinedAtDL = DL.getInlinedAt();
494   if (!InlinedAtDL)
495     return;
496 
497   CommentOS << " @[ ";
498   printDebugLoc(InlinedAtDL, CommentOS, Ctx);
499   CommentOS << " ]";
500 }
501 
502 static void printExtendedName(raw_ostream &OS, const DINode *Node,
503                               const DILocation *DL) {
504   const LLVMContext &Ctx = Node->getContext();
505   StringRef Res;
506   unsigned Line;
507   if (const auto *V = dyn_cast<const DILocalVariable>(Node)) {
508     Res = V->getName();
509     Line = V->getLine();
510   } else if (const auto *L = dyn_cast<const DILabel>(Node)) {
511     Res = L->getName();
512     Line = L->getLine();
513   }
514 
515   if (!Res.empty())
516     OS << Res << "," << Line;
517   auto *InlinedAt = DL ? DL->getInlinedAt() : nullptr;
518   if (InlinedAt) {
519     if (DebugLoc InlinedAtDL = InlinedAt) {
520       OS << " @[";
521       printDebugLoc(InlinedAtDL, OS, Ctx);
522       OS << "]";
523     }
524   }
525 }
526 
527 void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
528   OS << "!\"";
529   printExtendedName(OS, Variable, dl);
530 
531   OS << "\"\t";
532   for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
533     OS << " [" << I.start() << ';' << I.stop() << "):";
534     if (I.value().isUndef())
535       OS << "undef";
536     else {
537       OS << I.value().locNo();
538     }
539   }
540   for (unsigned i = 0, e = locations.size(); i != e; ++i) {
541     OS << " Loc" << i << '=';
542     locations[i].print(OS, TRI);
543   }
544   OS << '\n';
545 }
546 
547 void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
548   OS << "!\"";
549   printExtendedName(OS, Label, dl);
550 
551   OS << "\"\t";
552   OS << loc;
553   OS << '\n';
554 }
555 
556 void LDVImpl::print(raw_ostream &OS) {
557   OS << "********** DEBUG VARIABLES **********\n";
558   for (auto &userValue : userValues)
559     userValue->print(OS, TRI);
560   OS << "********** DEBUG LABELS **********\n";
561   for (auto &userLabel : userLabels)
562     userLabel->print(OS, TRI);
563 }
564 #endif
565 
566 void UserValue::mapVirtRegs(LDVImpl *LDV) {
567   for (unsigned i = 0, e = locations.size(); i != e; ++i)
568     if (locations[i].isReg() &&
569         Register::isVirtualRegister(locations[i].getReg()))
570       LDV->mapVirtReg(locations[i].getReg(), this);
571 }
572 
573 UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
574                                  const DIExpression *Expr, const DebugLoc &DL) {
575   auto Ident = UserValueIdentity(Var, Expr, DL->getInlinedAt());
576   UserValue *&UVEntry = UserVarMap[Ident];
577 
578   if (UVEntry)
579     return UVEntry;
580 
581   userValues.push_back(std::make_unique<UserValue>(Var, Expr, DL, allocator));
582   return UVEntry = userValues.back().get();
583 }
584 
585 void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *UV) {
586   assert(Register::isVirtualRegister(VirtReg) && "Only map VirtRegs");
587   assert(UserVarMap.find(UV->getId()) != UserVarMap.end() &&
588          "UserValue should exist in UserVarMap");
589   VirtRegToUserVals[VirtReg].push_back(UV);
590 }
591 
592 SmallVectorImpl<UserValue *> *LDVImpl::lookupVirtReg(unsigned VirtReg) {
593   VRMap::iterator Itr = VirtRegToUserVals.find(VirtReg);
594   if (Itr != VirtRegToUserVals.end())
595     return &Itr->getSecond();
596   return nullptr;
597 }
598 
599 bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
600   // DBG_VALUE loc, offset, variable
601   if (MI.getNumOperands() != 4 ||
602       !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
603       !MI.getOperand(2).isMetadata()) {
604     LLVM_DEBUG(dbgs() << "Can't handle " << MI);
605     return false;
606   }
607 
608   // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual
609   // register that hasn't been defined yet. If we do not remove those here, then
610   // the re-insertion of the DBG_VALUE instruction after register allocation
611   // will be incorrect.
612   // TODO: If earlier passes are corrected to generate sane debug information
613   // (and if the machine verifier is improved to catch this), then these checks
614   // could be removed or replaced by asserts.
615   bool Discard = false;
616   if (MI.getOperand(0).isReg() &&
617       Register::isVirtualRegister(MI.getOperand(0).getReg())) {
618     const Register Reg = MI.getOperand(0).getReg();
619     if (!LIS->hasInterval(Reg)) {
620       // The DBG_VALUE is described by a virtual register that does not have a
621       // live interval. Discard the DBG_VALUE.
622       Discard = true;
623       LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx
624                         << " " << MI);
625     } else {
626       // The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg
627       // is defined dead at Idx (where Idx is the slot index for the instruction
628       // preceding the DBG_VALUE).
629       const LiveInterval &LI = LIS->getInterval(Reg);
630       LiveQueryResult LRQ = LI.Query(Idx);
631       if (!LRQ.valueOutOrDead()) {
632         // We have found a DBG_VALUE with the value in a virtual register that
633         // is not live. Discard the DBG_VALUE.
634         Discard = true;
635         LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx
636                           << " " << MI);
637       }
638     }
639   }
640 
641   // Get or create the UserValue for (variable,offset) here.
642   assert(!MI.getOperand(1).isImm() && "DBG_VALUE with indirect flag before "
643                                       "LiveDebugVariables");
644   const DILocalVariable *Var = MI.getDebugVariable();
645   const DIExpression *Expr = MI.getDebugExpression();
646   UserValue *UV =
647       getUserValue(Var, Expr, MI.getDebugLoc());
648   if (!Discard)
649     UV->addDef(Idx, MI.getOperand(0));
650   else {
651     MachineOperand MO = MachineOperand::CreateReg(0U, false);
652     MO.setIsDebug();
653     UV->addDef(Idx, MO);
654   }
655   return true;
656 }
657 
658 bool LDVImpl::handleDebugLabel(MachineInstr &MI, SlotIndex Idx) {
659   // DBG_LABEL label
660   if (MI.getNumOperands() != 1 || !MI.getOperand(0).isMetadata()) {
661     LLVM_DEBUG(dbgs() << "Can't handle " << MI);
662     return false;
663   }
664 
665   // Get or create the UserLabel for label here.
666   const DILabel *Label = MI.getDebugLabel();
667   const DebugLoc &DL = MI.getDebugLoc();
668   bool Found = false;
669   for (auto const &L : userLabels) {
670     if (L->match(Label, DL->getInlinedAt(), Idx)) {
671       Found = true;
672       break;
673     }
674   }
675   if (!Found)
676     userLabels.push_back(std::make_unique<UserLabel>(Label, DL, Idx));
677 
678   return true;
679 }
680 
681 bool LDVImpl::collectDebugValues(MachineFunction &mf) {
682   bool Changed = false;
683   for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
684        ++MFI) {
685     MachineBasicBlock *MBB = &*MFI;
686     for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
687          MBBI != MBBE;) {
688       // Use the first debug instruction in the sequence to get a SlotIndex
689       // for following consecutive debug instructions.
690       if (!MBBI->isDebugInstr()) {
691         ++MBBI;
692         continue;
693       }
694       // Debug instructions has no slot index. Use the previous
695       // non-debug instruction's SlotIndex as its SlotIndex.
696       SlotIndex Idx =
697           MBBI == MBB->begin()
698               ? LIS->getMBBStartIdx(MBB)
699               : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
700       // Handle consecutive debug instructions with the same slot index.
701       do {
702         // Only handle DBG_VALUE in handleDebugValue(). Skip all other
703         // kinds of debug instructions.
704         if ((MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) ||
705             (MBBI->isDebugLabel() && handleDebugLabel(*MBBI, Idx))) {
706           MBBI = MBB->erase(MBBI);
707           Changed = true;
708         } else
709           ++MBBI;
710       } while (MBBI != MBBE && MBBI->isDebugInstr());
711     }
712   }
713   return Changed;
714 }
715 
716 void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR,
717                           const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
718                           LiveIntervals &LIS) {
719   SlotIndex Start = Idx;
720   MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
721   SlotIndex Stop = LIS.getMBBEndIdx(MBB);
722   LocMap::iterator I = locInts.find(Start);
723 
724   // Limit to VNI's live range.
725   bool ToEnd = true;
726   if (LR && VNI) {
727     LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
728     if (!Segment || Segment->valno != VNI) {
729       if (Kills)
730         Kills->push_back(Start);
731       return;
732     }
733     if (Segment->end < Stop) {
734       Stop = Segment->end;
735       ToEnd = false;
736     }
737   }
738 
739   // There could already be a short def at Start.
740   if (I.valid() && I.start() <= Start) {
741     // Stop when meeting a different location or an already extended interval.
742     Start = Start.getNextSlot();
743     if (I.value() != Loc || I.stop() != Start)
744       return;
745     // This is a one-slot placeholder. Just skip it.
746     ++I;
747   }
748 
749   // Limited by the next def.
750   if (I.valid() && I.start() < Stop)
751     Stop = I.start();
752   // Limited by VNI's live range.
753   else if (!ToEnd && Kills)
754     Kills->push_back(Stop);
755 
756   if (Start < Stop)
757     I.insert(Start, Stop, Loc);
758 }
759 
760 void UserValue::addDefsFromCopies(
761     LiveInterval *LI, unsigned LocNo,
762     const SmallVectorImpl<SlotIndex> &Kills,
763     SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
764     MachineRegisterInfo &MRI, LiveIntervals &LIS) {
765   if (Kills.empty())
766     return;
767   // Don't track copies from physregs, there are too many uses.
768   if (!Register::isVirtualRegister(LI->reg))
769     return;
770 
771   // Collect all the (vreg, valno) pairs that are copies of LI.
772   SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
773   for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
774     MachineInstr *MI = MO.getParent();
775     // Copies of the full value.
776     if (MO.getSubReg() || !MI->isCopy())
777       continue;
778     Register DstReg = MI->getOperand(0).getReg();
779 
780     // Don't follow copies to physregs. These are usually setting up call
781     // arguments, and the argument registers are always call clobbered. We are
782     // better off in the source register which could be a callee-saved register,
783     // or it could be spilled.
784     if (!Register::isVirtualRegister(DstReg))
785       continue;
786 
787     // Is LocNo extended to reach this copy? If not, another def may be blocking
788     // it, or we are looking at a wrong value of LI.
789     SlotIndex Idx = LIS.getInstructionIndex(*MI);
790     LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
791     if (!I.valid() || I.value().locNo() != LocNo)
792       continue;
793 
794     if (!LIS.hasInterval(DstReg))
795       continue;
796     LiveInterval *DstLI = &LIS.getInterval(DstReg);
797     const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
798     assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
799     CopyValues.push_back(std::make_pair(DstLI, DstVNI));
800   }
801 
802   if (CopyValues.empty())
803     return;
804 
805   LLVM_DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI
806                     << '\n');
807 
808   // Try to add defs of the copied values for each kill point.
809   for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
810     SlotIndex Idx = Kills[i];
811     for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
812       LiveInterval *DstLI = CopyValues[j].first;
813       const VNInfo *DstVNI = CopyValues[j].second;
814       if (DstLI->getVNInfoAt(Idx) != DstVNI)
815         continue;
816       // Check that there isn't already a def at Idx
817       LocMap::iterator I = locInts.find(Idx);
818       if (I.valid() && I.start() <= Idx)
819         continue;
820       LLVM_DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
821                         << DstVNI->id << " in " << *DstLI << '\n');
822       MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
823       assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
824       unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
825       DbgValueLocation NewLoc(LocNo);
826       I.insert(Idx, Idx.getNextSlot(), NewLoc);
827       NewDefs.push_back(std::make_pair(Idx, NewLoc));
828       break;
829     }
830   }
831 }
832 
833 void UserValue::computeIntervals(MachineRegisterInfo &MRI,
834                                  const TargetRegisterInfo &TRI,
835                                  LiveIntervals &LIS, LexicalScopes &LS) {
836   SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs;
837 
838   // Collect all defs to be extended (Skipping undefs).
839   for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
840     if (!I.value().isUndef())
841       Defs.push_back(std::make_pair(I.start(), I.value()));
842 
843   // Extend all defs, and possibly add new ones along the way.
844   for (unsigned i = 0; i != Defs.size(); ++i) {
845     SlotIndex Idx = Defs[i].first;
846     DbgValueLocation Loc = Defs[i].second;
847     const MachineOperand &LocMO = locations[Loc.locNo()];
848 
849     if (!LocMO.isReg()) {
850       extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS);
851       continue;
852     }
853 
854     // Register locations are constrained to where the register value is live.
855     if (Register::isVirtualRegister(LocMO.getReg())) {
856       LiveInterval *LI = nullptr;
857       const VNInfo *VNI = nullptr;
858       if (LIS.hasInterval(LocMO.getReg())) {
859         LI = &LIS.getInterval(LocMO.getReg());
860         VNI = LI->getVNInfoAt(Idx);
861       }
862       SmallVector<SlotIndex, 16> Kills;
863       extendDef(Idx, Loc, LI, VNI, &Kills, LIS);
864       // FIXME: Handle sub-registers in addDefsFromCopies. The problem is that
865       // if the original location for example is %vreg0:sub_hi, and we find a
866       // full register copy in addDefsFromCopies (at the moment it only handles
867       // full register copies), then we must add the sub1 sub-register index to
868       // the new location. However, that is only possible if the new virtual
869       // register is of the same regclass (or if there is an equivalent
870       // sub-register in that regclass). For now, simply skip handling copies if
871       // a sub-register is involved.
872       if (LI && !LocMO.getSubReg())
873         addDefsFromCopies(LI, Loc.locNo(), Kills, Defs, MRI, LIS);
874       continue;
875     }
876 
877     // For physregs, we only mark the start slot idx. DwarfDebug will see it
878     // as if the DBG_VALUE is valid up until the end of the basic block, or
879     // the next def of the physical register. So we do not need to extend the
880     // range. It might actually happen that the DBG_VALUE is the last use of
881     // the physical register (e.g. if this is an unused input argument to a
882     // function).
883   }
884 
885   // The computed intervals may extend beyond the range of the debug
886   // location's lexical scope. In this case, splitting of an interval
887   // can result in an interval outside of the scope being created,
888   // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
889   // this, trim the intervals to the lexical scope.
890 
891   LexicalScope *Scope = LS.findLexicalScope(dl);
892   if (!Scope)
893     return;
894 
895   SlotIndex PrevEnd;
896   LocMap::iterator I = locInts.begin();
897 
898   // Iterate over the lexical scope ranges. Each time round the loop
899   // we check the intervals for overlap with the end of the previous
900   // range and the start of the next. The first range is handled as
901   // a special case where there is no PrevEnd.
902   for (const InsnRange &Range : Scope->getRanges()) {
903     SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
904     SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
905 
906     // At the start of each iteration I has been advanced so that
907     // I.stop() >= PrevEnd. Check for overlap.
908     if (PrevEnd && I.start() < PrevEnd) {
909       SlotIndex IStop = I.stop();
910       DbgValueLocation Loc = I.value();
911 
912       // Stop overlaps previous end - trim the end of the interval to
913       // the scope range.
914       I.setStopUnchecked(PrevEnd);
915       ++I;
916 
917       // If the interval also overlaps the start of the "next" (i.e.
918       // current) range create a new interval for the remainder
919       if (RStart < IStop)
920         I.insert(RStart, IStop, Loc);
921     }
922 
923     // Advance I so that I.stop() >= RStart, and check for overlap.
924     I.advanceTo(RStart);
925     if (!I.valid())
926       return;
927 
928     // The end of a lexical scope range is the last instruction in the
929     // range. To convert to an interval we need the index of the
930     // instruction after it.
931     REnd = REnd.getNextIndex();
932 
933     // Advance I to first interval outside current range.
934     I.advanceTo(REnd);
935     if (!I.valid())
936       return;
937 
938     PrevEnd = REnd;
939   }
940 
941   // Check for overlap with end of final range.
942   if (PrevEnd && I.start() < PrevEnd)
943     I.setStopUnchecked(PrevEnd);
944 }
945 
946 void LDVImpl::computeIntervals() {
947   LexicalScopes LS;
948   LS.initialize(*MF);
949 
950   for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
951     userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS);
952     userValues[i]->mapVirtRegs(this);
953   }
954 }
955 
956 bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
957   clear();
958   MF = &mf;
959   LIS = &pass.getAnalysis<LiveIntervals>();
960   TRI = mf.getSubtarget().getRegisterInfo();
961   LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
962                     << mf.getName() << " **********\n");
963 
964   bool Changed = collectDebugValues(mf);
965   computeIntervals();
966   LLVM_DEBUG(print(dbgs()));
967   ModifiedMF = Changed;
968   return Changed;
969 }
970 
971 static void removeDebugValues(MachineFunction &mf) {
972   for (MachineBasicBlock &MBB : mf) {
973     for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
974       if (!MBBI->isDebugValue()) {
975         ++MBBI;
976         continue;
977       }
978       MBBI = MBB.erase(MBBI);
979     }
980   }
981 }
982 
983 bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
984   if (!EnableLDV)
985     return false;
986   if (!mf.getFunction().getSubprogram()) {
987     removeDebugValues(mf);
988     return false;
989   }
990   if (!pImpl)
991     pImpl = new LDVImpl(this);
992   return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
993 }
994 
995 void LiveDebugVariables::releaseMemory() {
996   if (pImpl)
997     static_cast<LDVImpl*>(pImpl)->clear();
998 }
999 
1000 LiveDebugVariables::~LiveDebugVariables() {
1001   if (pImpl)
1002     delete static_cast<LDVImpl*>(pImpl);
1003 }
1004 
1005 //===----------------------------------------------------------------------===//
1006 //                           Live Range Splitting
1007 //===----------------------------------------------------------------------===//
1008 
1009 bool
1010 UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
1011                          LiveIntervals& LIS) {
1012   LLVM_DEBUG({
1013     dbgs() << "Splitting Loc" << OldLocNo << '\t';
1014     print(dbgs(), nullptr);
1015   });
1016   bool DidChange = false;
1017   LocMap::iterator LocMapI;
1018   LocMapI.setMap(locInts);
1019   for (unsigned i = 0; i != NewRegs.size(); ++i) {
1020     LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
1021     if (LI->empty())
1022       continue;
1023 
1024     // Don't allocate the new LocNo until it is needed.
1025     unsigned NewLocNo = UndefLocNo;
1026 
1027     // Iterate over the overlaps between locInts and LI.
1028     LocMapI.find(LI->beginIndex());
1029     if (!LocMapI.valid())
1030       continue;
1031     LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
1032     LiveInterval::iterator LIE = LI->end();
1033     while (LocMapI.valid() && LII != LIE) {
1034       // At this point, we know that LocMapI.stop() > LII->start.
1035       LII = LI->advanceTo(LII, LocMapI.start());
1036       if (LII == LIE)
1037         break;
1038 
1039       // Now LII->end > LocMapI.start(). Do we have an overlap?
1040       if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) {
1041         // Overlapping correct location. Allocate NewLocNo now.
1042         if (NewLocNo == UndefLocNo) {
1043           MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
1044           MO.setSubReg(locations[OldLocNo].getSubReg());
1045           NewLocNo = getLocationNo(MO);
1046           DidChange = true;
1047         }
1048 
1049         SlotIndex LStart = LocMapI.start();
1050         SlotIndex LStop  = LocMapI.stop();
1051         DbgValueLocation OldLoc = LocMapI.value();
1052 
1053         // Trim LocMapI down to the LII overlap.
1054         if (LStart < LII->start)
1055           LocMapI.setStartUnchecked(LII->start);
1056         if (LStop > LII->end)
1057           LocMapI.setStopUnchecked(LII->end);
1058 
1059         // Change the value in the overlap. This may trigger coalescing.
1060         LocMapI.setValue(OldLoc.changeLocNo(NewLocNo));
1061 
1062         // Re-insert any removed OldLocNo ranges.
1063         if (LStart < LocMapI.start()) {
1064           LocMapI.insert(LStart, LocMapI.start(), OldLoc);
1065           ++LocMapI;
1066           assert(LocMapI.valid() && "Unexpected coalescing");
1067         }
1068         if (LStop > LocMapI.stop()) {
1069           ++LocMapI;
1070           LocMapI.insert(LII->end, LStop, OldLoc);
1071           --LocMapI;
1072         }
1073       }
1074 
1075       // Advance to the next overlap.
1076       if (LII->end < LocMapI.stop()) {
1077         if (++LII == LIE)
1078           break;
1079         LocMapI.advanceTo(LII->start);
1080       } else {
1081         ++LocMapI;
1082         if (!LocMapI.valid())
1083           break;
1084         LII = LI->advanceTo(LII, LocMapI.start());
1085       }
1086     }
1087   }
1088 
1089   // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
1090   locations.erase(locations.begin() + OldLocNo);
1091   LocMapI.goToBegin();
1092   while (LocMapI.valid()) {
1093     DbgValueLocation v = LocMapI.value();
1094     if (v.locNo() == OldLocNo) {
1095       LLVM_DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
1096                         << LocMapI.stop() << ")\n");
1097       LocMapI.erase();
1098     } else {
1099       // Undef values always have location number UndefLocNo, so don't change
1100       // locNo in that case. See getLocationNo().
1101       if (!v.isUndef() && v.locNo() > OldLocNo)
1102         LocMapI.setValueUnchecked(v.changeLocNo(v.locNo() - 1));
1103       ++LocMapI;
1104     }
1105   }
1106 
1107   LLVM_DEBUG({
1108     dbgs() << "Split result: \t";
1109     print(dbgs(), nullptr);
1110   });
1111   return DidChange;
1112 }
1113 
1114 bool
1115 UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
1116                          LiveIntervals &LIS) {
1117   bool DidChange = false;
1118   // Split locations referring to OldReg. Iterate backwards so splitLocation can
1119   // safely erase unused locations.
1120   for (unsigned i = locations.size(); i ; --i) {
1121     unsigned LocNo = i-1;
1122     const MachineOperand *Loc = &locations[LocNo];
1123     if (!Loc->isReg() || Loc->getReg() != OldReg)
1124       continue;
1125     DidChange |= splitLocation(LocNo, NewRegs, LIS);
1126   }
1127   return DidChange;
1128 }
1129 
1130 void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
1131   bool DidChange = false;
1132   if (auto *UserVals = lookupVirtReg(OldReg))
1133     for (auto *UV : *UserVals)
1134       DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
1135 
1136   if (!DidChange)
1137     return;
1138 
1139   // Map all of the new virtual registers.
1140   if (auto *UserVals = lookupVirtReg(OldReg))
1141     for (auto *UV : *UserVals)
1142       for (unsigned i = 0; i != NewRegs.size(); ++i)
1143         mapVirtReg(NewRegs[i], UV);
1144 }
1145 
1146 void LiveDebugVariables::
1147 splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
1148   if (pImpl)
1149     static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
1150 }
1151 
1152 void UserValue::rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
1153                                  const TargetInstrInfo &TII,
1154                                  const TargetRegisterInfo &TRI,
1155                                  SpillOffsetMap &SpillOffsets) {
1156   // Build a set of new locations with new numbers so we can coalesce our
1157   // IntervalMap if two vreg intervals collapse to the same physical location.
1158   // Use MapVector instead of SetVector because MapVector::insert returns the
1159   // position of the previously or newly inserted element. The boolean value
1160   // tracks if the location was produced by a spill.
1161   // FIXME: This will be problematic if we ever support direct and indirect
1162   // frame index locations, i.e. expressing both variables in memory and
1163   // 'int x, *px = &x'. The "spilled" bit must become part of the location.
1164   MapVector<MachineOperand, std::pair<bool, unsigned>> NewLocations;
1165   SmallVector<unsigned, 4> LocNoMap(locations.size());
1166   for (unsigned I = 0, E = locations.size(); I != E; ++I) {
1167     bool Spilled = false;
1168     unsigned SpillOffset = 0;
1169     MachineOperand Loc = locations[I];
1170     // Only virtual registers are rewritten.
1171     if (Loc.isReg() && Loc.getReg() &&
1172         Register::isVirtualRegister(Loc.getReg())) {
1173       Register VirtReg = Loc.getReg();
1174       if (VRM.isAssignedReg(VirtReg) &&
1175           Register::isPhysicalRegister(VRM.getPhys(VirtReg))) {
1176         // This can create a %noreg operand in rare cases when the sub-register
1177         // index is no longer available. That means the user value is in a
1178         // non-existent sub-register, and %noreg is exactly what we want.
1179         Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
1180       } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
1181         // Retrieve the stack slot offset.
1182         unsigned SpillSize;
1183         const MachineRegisterInfo &MRI = MF.getRegInfo();
1184         const TargetRegisterClass *TRC = MRI.getRegClass(VirtReg);
1185         bool Success = TII.getStackSlotRange(TRC, Loc.getSubReg(), SpillSize,
1186                                              SpillOffset, MF);
1187 
1188         // FIXME: Invalidate the location if the offset couldn't be calculated.
1189         (void)Success;
1190 
1191         Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
1192         Spilled = true;
1193       } else {
1194         Loc.setReg(0);
1195         Loc.setSubReg(0);
1196       }
1197     }
1198 
1199     // Insert this location if it doesn't already exist and record a mapping
1200     // from the old number to the new number.
1201     auto InsertResult = NewLocations.insert({Loc, {Spilled, SpillOffset}});
1202     unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
1203     LocNoMap[I] = NewLocNo;
1204   }
1205 
1206   // Rewrite the locations and record the stack slot offsets for spills.
1207   locations.clear();
1208   SpillOffsets.clear();
1209   for (auto &Pair : NewLocations) {
1210     bool Spilled;
1211     unsigned SpillOffset;
1212     std::tie(Spilled, SpillOffset) = Pair.second;
1213     locations.push_back(Pair.first);
1214     if (Spilled) {
1215       unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
1216       SpillOffsets[NewLocNo] = SpillOffset;
1217     }
1218   }
1219 
1220   // Update the interval map, but only coalesce left, since intervals to the
1221   // right use the old location numbers. This should merge two contiguous
1222   // DBG_VALUE intervals with different vregs that were allocated to the same
1223   // physical register.
1224   for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
1225     DbgValueLocation Loc = I.value();
1226     // Undef values don't exist in locations (and thus not in LocNoMap either)
1227     // so skip over them. See getLocationNo().
1228     if (Loc.isUndef())
1229       continue;
1230     unsigned NewLocNo = LocNoMap[Loc.locNo()];
1231     I.setValueUnchecked(Loc.changeLocNo(NewLocNo));
1232     I.setStart(I.start());
1233   }
1234 }
1235 
1236 /// Find an iterator for inserting a DBG_VALUE instruction.
1237 static MachineBasicBlock::iterator
1238 findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
1239                    LiveIntervals &LIS) {
1240   SlotIndex Start = LIS.getMBBStartIdx(MBB);
1241   Idx = Idx.getBaseIndex();
1242 
1243   // Try to find an insert location by going backwards from Idx.
1244   MachineInstr *MI;
1245   while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1246     // We've reached the beginning of MBB.
1247     if (Idx == Start) {
1248       MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
1249       return I;
1250     }
1251     Idx = Idx.getPrevIndex();
1252   }
1253 
1254   // Don't insert anything after the first terminator, though.
1255   return MI->isTerminator() ? MBB->getFirstTerminator() :
1256                               std::next(MachineBasicBlock::iterator(MI));
1257 }
1258 
1259 /// Find an iterator for inserting the next DBG_VALUE instruction
1260 /// (or end if no more insert locations found).
1261 static MachineBasicBlock::iterator
1262 findNextInsertLocation(MachineBasicBlock *MBB,
1263                        MachineBasicBlock::iterator I,
1264                        SlotIndex StopIdx, MachineOperand &LocMO,
1265                        LiveIntervals &LIS,
1266                        const TargetRegisterInfo &TRI) {
1267   if (!LocMO.isReg())
1268     return MBB->instr_end();
1269   Register Reg = LocMO.getReg();
1270 
1271   // Find the next instruction in the MBB that define the register Reg.
1272   while (I != MBB->end() && !I->isTerminator()) {
1273     if (!LIS.isNotInMIMap(*I) &&
1274         SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I)))
1275       break;
1276     if (I->definesRegister(Reg, &TRI))
1277       // The insert location is directly after the instruction/bundle.
1278       return std::next(I);
1279     ++I;
1280   }
1281   return MBB->end();
1282 }
1283 
1284 void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
1285                                  SlotIndex StopIdx, DbgValueLocation Loc,
1286                                  bool Spilled, unsigned SpillOffset,
1287                                  LiveIntervals &LIS, const TargetInstrInfo &TII,
1288                                  const TargetRegisterInfo &TRI) {
1289   SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB);
1290   // Only search within the current MBB.
1291   StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx;
1292   MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS);
1293   // Undef values don't exist in locations so create new "noreg" register MOs
1294   // for them. See getLocationNo().
1295   MachineOperand MO = !Loc.isUndef() ?
1296     locations[Loc.locNo()] :
1297     MachineOperand::CreateReg(/* Reg */ 0, /* isDef */ false, /* isImp */ false,
1298                               /* isKill */ false, /* isDead */ false,
1299                               /* isUndef */ false, /* isEarlyClobber */ false,
1300                               /* SubReg */ 0, /* isDebug */ true);
1301 
1302   ++NumInsertedDebugValues;
1303 
1304   assert(cast<DILocalVariable>(Variable)
1305              ->isValidLocationForIntrinsic(getDebugLoc()) &&
1306          "Expected inlined-at fields to agree");
1307 
1308   // If the location was spilled, the new DBG_VALUE will be indirect. If the
1309   // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
1310   // that the original virtual register was a pointer. Also, add the stack slot
1311   // offset for the spilled register to the expression.
1312   const DIExpression *Expr = Expression;
1313   if (Spilled)
1314     Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, SpillOffset);
1315 
1316   assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index");
1317 
1318   do {
1319     BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
1320             Spilled, MO, Variable, Expr);
1321 
1322     // Continue and insert DBG_VALUES after every redefinition of register
1323     // associated with the debug value within the range
1324     I = findNextInsertLocation(MBB, I, StopIdx, MO, LIS, TRI);
1325   } while (I != MBB->end());
1326 }
1327 
1328 void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
1329                                  LiveIntervals &LIS,
1330                                  const TargetInstrInfo &TII) {
1331   MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
1332   ++NumInsertedDebugLabels;
1333   BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_LABEL))
1334       .addMetadata(Label);
1335 }
1336 
1337 void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
1338                                 const TargetInstrInfo &TII,
1339                                 const TargetRegisterInfo &TRI,
1340                                 const SpillOffsetMap &SpillOffsets) {
1341   MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
1342 
1343   for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1344     SlotIndex Start = I.start();
1345     SlotIndex Stop = I.stop();
1346     DbgValueLocation Loc = I.value();
1347     auto SpillIt =
1348         !Loc.isUndef() ? SpillOffsets.find(Loc.locNo()) : SpillOffsets.end();
1349     bool Spilled = SpillIt != SpillOffsets.end();
1350     unsigned SpillOffset = Spilled ? SpillIt->second : 0;
1351 
1352     LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo());
1353     MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1354     SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
1355 
1356     LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1357     insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1358                      TRI);
1359     // This interval may span multiple basic blocks.
1360     // Insert a DBG_VALUE into each one.
1361     while (Stop > MBBEnd) {
1362       // Move to the next block.
1363       Start = MBBEnd;
1364       if (++MBB == MFEnd)
1365         break;
1366       MBBEnd = LIS.getMBBEndIdx(&*MBB);
1367       LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1368       insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1369                        TRI);
1370     }
1371     LLVM_DEBUG(dbgs() << '\n');
1372     if (MBB == MFEnd)
1373       break;
1374 
1375     ++I;
1376   }
1377 }
1378 
1379 void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII) {
1380   LLVM_DEBUG(dbgs() << "\t" << loc);
1381   MachineFunction::iterator MBB = LIS.getMBBFromIndex(loc)->getIterator();
1382 
1383   LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB));
1384   insertDebugLabel(&*MBB, loc, LIS, TII);
1385 
1386   LLVM_DEBUG(dbgs() << '\n');
1387 }
1388 
1389 void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
1390   LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
1391   if (!MF)
1392     return;
1393   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1394   SpillOffsetMap SpillOffsets;
1395   for (auto &userValue : userValues) {
1396     LLVM_DEBUG(userValue->print(dbgs(), TRI));
1397     userValue->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets);
1398     userValue->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets);
1399   }
1400   LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG LABELS **********\n");
1401   for (auto &userLabel : userLabels) {
1402     LLVM_DEBUG(userLabel->print(dbgs(), TRI));
1403     userLabel->emitDebugLabel(*LIS, *TII);
1404   }
1405   EmitDone = true;
1406 }
1407 
1408 void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
1409   if (pImpl)
1410     static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1411 }
1412 
1413 bool LiveDebugVariables::doInitialization(Module &M) {
1414   return Pass::doInitialization(M);
1415 }
1416 
1417 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1418 LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
1419   if (pImpl)
1420     static_cast<LDVImpl*>(pImpl)->print(dbgs());
1421 }
1422 #endif
1423