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/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"
60 #include <algorithm>
61 #include <cassert>
62 #include <iterator>
63 #include <memory>
64 #include <utility>
65 
66 using namespace llvm;
67 
68 #define DEBUG_TYPE "livedebugvars"
69 
70 static cl::opt<bool>
71 EnableLDV("live-debug-variables", cl::init(true),
72           cl::desc("Enable the live debug variables pass"), cl::Hidden);
73 
74 STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
75 STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted");
76 
77 char LiveDebugVariables::ID = 0;
78 
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)
85 
86 void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
87   AU.addRequired<MachineDominatorTree>();
88   AU.addRequiredTransitive<LiveIntervals>();
89   AU.setPreservesAll();
90   MachineFunctionPass::getAnalysisUsage(AU);
91 }
92 
93 LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) {
94   initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
95 }
96 
97 enum : unsigned { UndefLocNo = ~0U };
98 
99 /// Describes a location by number along with some flags about the original
100 /// usage of the location.
101 class DbgValueLocation {
102 public:
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");
107   }
108 
109   DbgValueLocation() : LocNo(0), WasIndirect(0) {}
110 
111   unsigned locNo() const {
112     // Fix up the undef location number, which gets truncated.
113     return LocNo == INT_MAX ? UndefLocNo : LocNo;
114   }
115   bool wasIndirect() const { return WasIndirect; }
116   bool isUndef() const { return locNo() == UndefLocNo; }
117 
118   DbgValueLocation changeLocNo(unsigned NewLocNo) const {
119     return DbgValueLocation(NewLocNo, WasIndirect);
120   }
121 
122   friend inline bool operator==(const DbgValueLocation &LHS,
123                                 const DbgValueLocation &RHS) {
124     return LHS.LocNo == RHS.LocNo && LHS.WasIndirect == RHS.WasIndirect;
125   }
126 
127   friend inline bool operator!=(const DbgValueLocation &LHS,
128                                 const DbgValueLocation &RHS) {
129     return !(LHS == RHS);
130   }
131 
132 private:
133   unsigned LocNo : 31;
134   unsigned WasIndirect : 1;
135 };
136 
137 /// Map of where a user value is live, and its location.
138 using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>;
139 
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>;
143 
144 namespace {
145 
146 class LDVImpl;
147 
148 /// A user value is a part of a debug info user variable.
149 ///
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.
152 ///
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.
157 class UserValue {
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.
164 
165   /// Numbered locations referenced by locmap.
166   SmallVector<MachineOperand, 4> locations;
167 
168   /// Map of slot indices where this value is live.
169   LocMap locInts;
170 
171   /// Set of interval start indexes that have been trimmed to the
172   /// lexical scope.
173   SmallSet<SlotIndex, 2> trimmedDefs;
174 
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);
181 
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,
185                      LiveIntervals &LIS);
186 
187 public:
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),
192         locInts(alloc) {}
193 
194   /// Get the leader of this value's equivalence class.
195   UserValue *getLeader() {
196     UserValue *l = leader;
197     while (l != l->leader)
198       l = l->leader;
199     return leader = l;
200   }
201 
202   /// Return the next UserValue in the equivalence class.
203   UserValue *getNext() const { return next; }
204 
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;
211   }
212 
213   /// Merge equivalence classes.
214   static UserValue *merge(UserValue *L1, UserValue *L2) {
215     L2 = L2->getLeader();
216     if (!L1)
217       return L2;
218     L1 = L1->getLeader();
219     if (L1 == L2)
220       return L1;
221     // Splice L2 before L1's members.
222     UserValue *End = L2;
223     while (End->next) {
224       End->leader = L1;
225       End = End->next;
226     }
227     End->leader = L1;
228     End->next = L1->next;
229     L1->next = L2;
230     return L1;
231   }
232 
233   /// Return the location number that matches Loc.
234   ///
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) {
240     if (LocMO.isReg()) {
241       if (LocMO.getReg() == 0)
242         return UndefLocNo;
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())
248           return i;
249     } else
250       for (unsigned i = 0, e = locations.size(); i != e; ++i)
251         if (LocMO.isIdenticalTo(locations[i]))
252           return 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();
261     }
262     return locations.size() - 1;
263   }
264 
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)
272         return;
273     }
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));
281     }
282   }
283 
284   /// Ensure that all virtual register locations are mapped.
285   void mapVirtRegs(LDVImpl *LDV);
286 
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);
294     else
295       // A later DBG_VALUE at the same SlotIndex overrides the old location.
296       I.setValue(Loc);
297   }
298 
299   /// Extend the current definition as far as possible down.
300   ///
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.
303   ///
304   /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
305   /// data-flow analysis to propagate them beyond basic block boundaries.
306   ///
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,
316                  LiveIntervals &LIS);
317 
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
320   /// possible.
321   ///
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);
332 
333   /// Compute the live intervals of all locations after collecting all their
334   /// def points.
335   void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
336                         LiveIntervals &LIS, LexicalScopes &LS);
337 
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,
341                      LiveIntervals &LIS);
342 
343   /// Rewrite virtual register locations according to the provided virtual
344   /// register map. Record the stack slot offsets for the locations that
345   /// were spilled.
346   void rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
347                         const TargetInstrInfo &TII,
348                         const TargetRegisterInfo &TRI,
349                         SpillOffsetMap &SpillOffsets);
350 
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);
356 
357   /// Return DebugLoc of this UserValue.
358   DebugLoc getDebugLoc() { return dl;}
359 
360   void print(raw_ostream &, const TargetRegisterInfo *);
361 };
362 
363 /// A user label is a part of a debug info user label.
364 class UserLabel {
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.
369 
370   /// Insert a DBG_LABEL into MBB at Idx.
371   void insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
372                         LiveIntervals &LIS, const TargetInstrInfo &TII);
373 
374 public:
375   /// Create a new UserLabel.
376   UserLabel(const DILabel *label, DebugLoc L, SlotIndex Idx)
377       : Label(label), dl(std::move(L)), loc(Idx) {}
378 
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;
383   }
384 
385   /// Recreate DBG_LABEL instruction from data structures.
386   void emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII);
387 
388   /// Return DebugLoc of this UserLabel.
389   DebugLoc getDebugLoc() { return dl; }
390 
391   void print(raw_ostream &, const TargetRegisterInfo *);
392 };
393 
394 /// Implementation of the LiveDebugVariables pass.
395 class LDVImpl {
396   LiveDebugVariables &pass;
397   LocMap::Allocator allocator;
398   MachineFunction *MF = nullptr;
399   LiveIntervals *LIS;
400   const TargetRegisterInfo *TRI;
401 
402   /// Whether emitDebugValues is called.
403   bool EmitDone = false;
404 
405   /// Whether the machine function is modified during the pass.
406   bool ModifiedMF = false;
407 
408   /// All allocated UserValue instances.
409   SmallVector<std::unique_ptr<UserValue>, 8> userValues;
410 
411   /// All allocated UserLabel instances.
412   SmallVector<std::unique_ptr<UserLabel>, 2> userLabels;
413 
414   /// Map virtual register to eq class leader.
415   using VRMap = DenseMap<unsigned, UserValue *>;
416   VRMap virtRegToEqClass;
417 
418   /// Map user variable to eq class leader.
419   using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
420   UVMap userVarMap;
421 
422   /// Find or create a UserValue.
423   UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
424                           const DebugLoc &DL);
425 
426   /// Find the EC leader for VirtReg or null.
427   UserValue *lookupVirtReg(unsigned VirtReg);
428 
429   /// Add DBG_VALUE instruction to our maps.
430   ///
431   /// \param MI DBG_VALUE instruction
432   /// \param Idx Last valid SLotIndex before instruction.
433   ///
434   /// \returns True if the DBG_VALUE instruction should be deleted.
435   bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
436 
437   /// Add DBG_LABEL instruction to UserLabel.
438   ///
439   /// \param MI DBG_LABEL instruction
440   /// \param Idx Last valid SlotIndex before instruction.
441   ///
442   /// \returns True if the DBG_LABEL instruction should be deleted.
443   bool handleDebugLabel(MachineInstr &MI, SlotIndex Idx);
444 
445   /// Collect and erase all DBG_VALUE instructions, adding a UserValue def
446   /// for each instruction.
447   ///
448   /// \param mf MachineFunction to be scanned.
449   ///
450   /// \returns True if any debug values were found.
451   bool collectDebugValues(MachineFunction &mf);
452 
453   /// Compute the live intervals of all user values after collecting all
454   /// their def points.
455   void computeIntervals();
456 
457 public:
458   LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
459 
460   bool runOnMachineFunction(MachineFunction &mf);
461 
462   /// Release all memory.
463   void clear() {
464     MF = nullptr;
465     userValues.clear();
466     userLabels.clear();
467     virtRegToEqClass.clear();
468     userVarMap.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");
472     EmitDone = false;
473     ModifiedMF = false;
474   }
475 
476   /// Map virtual register to an equivalence class.
477   void mapVirtReg(unsigned VirtReg, UserValue *EC);
478 
479   /// Replace all references to OldReg with NewRegs.
480   void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
481 
482   /// Recreate DBG_VALUE instruction from data structures.
483   void emitDebugValues(VirtRegMap *VRM);
484 
485   void print(raw_ostream&);
486 };
487 
488 } // end anonymous namespace
489 
490 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
491 static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
492                           const LLVMContext &Ctx) {
493   if (!DL)
494     return;
495 
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();
502 
503   DebugLoc InlinedAtDL = DL.getInlinedAt();
504   if (!InlinedAtDL)
505     return;
506 
507   CommentOS << " @[ ";
508   printDebugLoc(InlinedAtDL, CommentOS, Ctx);
509   CommentOS << " ]";
510 }
511 
512 static void printExtendedName(raw_ostream &OS, const DINode *Node,
513                               const DILocation *DL) {
514   const LLVMContext &Ctx = Node->getContext();
515   StringRef Res;
516   unsigned Line = 0;
517   if (const auto *V = dyn_cast<const DILocalVariable>(Node)) {
518     Res = V->getName();
519     Line = V->getLine();
520   } else if (const auto *L = dyn_cast<const DILabel>(Node)) {
521     Res = L->getName();
522     Line = L->getLine();
523   }
524 
525   if (!Res.empty())
526     OS << Res << "," << Line;
527   auto *InlinedAt = DL ? DL->getInlinedAt() : nullptr;
528   if (InlinedAt) {
529     if (DebugLoc InlinedAtDL = InlinedAt) {
530       OS << " @[";
531       printDebugLoc(InlinedAtDL, OS, Ctx);
532       OS << "]";
533     }
534   }
535 }
536 
537 void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
538   OS << "!\"";
539   printExtendedName(OS, Variable, dl);
540 
541   OS << "\"\t";
542   for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
543     OS << " [" << I.start() << ';' << I.stop() << "):";
544     if (I.value().isUndef())
545       OS << "undef";
546     else {
547       OS << I.value().locNo();
548       if (I.value().wasIndirect())
549         OS << " ind";
550     }
551   }
552   for (unsigned i = 0, e = locations.size(); i != e; ++i) {
553     OS << " Loc" << i << '=';
554     locations[i].print(OS, TRI);
555   }
556   OS << '\n';
557 }
558 
559 void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
560   OS << "!\"";
561   printExtendedName(OS, Label, dl);
562 
563   OS << "\"\t";
564   OS << loc;
565   OS << '\n';
566 }
567 
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);
575 }
576 #endif
577 
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);
583 }
584 
585 UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
586                                  const DIExpression *Expr, const DebugLoc &DL) {
587   UserValue *&Leader = userVarMap[Var];
588   if (Leader) {
589     UserValue *UV = Leader->getLeader();
590     Leader = UV;
591     for (; UV; UV = UV->getNext())
592       if (UV->match(Var, Expr, DL->getInlinedAt()))
593         return UV;
594   }
595 
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);
600   return UV;
601 }
602 
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);
607 }
608 
609 UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
610   if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
611     return UV->getLeader();
612   return nullptr;
613 }
614 
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);
621     return false;
622   }
623 
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.
638       Discard = true;
639       LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx
640                         << " " << MI);
641     } else {
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.
650         Discard = true;
651         LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx
652                           << " " << MI);
653       }
654     }
655   }
656 
657   // Get or create the UserValue for (variable,offset) here.
658   bool IsIndirect = MI.getOperand(1).isImm();
659   if (IsIndirect)
660     assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
661   const DILocalVariable *Var = MI.getDebugVariable();
662   const DIExpression *Expr = MI.getDebugExpression();
663   UserValue *UV =
664       getUserValue(Var, Expr, MI.getDebugLoc());
665   if (!Discard)
666     UV->addDef(Idx, MI.getOperand(0), IsIndirect);
667   else {
668     MachineOperand MO = MachineOperand::CreateReg(0U, false);
669     MO.setIsDebug();
670     UV->addDef(Idx, MO, false);
671   }
672   return true;
673 }
674 
675 bool LDVImpl::handleDebugLabel(MachineInstr &MI, SlotIndex Idx) {
676   // DBG_LABEL label
677   if (MI.getNumOperands() != 1 || !MI.getOperand(0).isMetadata()) {
678     LLVM_DEBUG(dbgs() << "Can't handle " << MI);
679     return false;
680   }
681 
682   // Get or create the UserLabel for label here.
683   const DILabel *Label = MI.getDebugLabel();
684   const DebugLoc &DL = MI.getDebugLoc();
685   bool Found = false;
686   for (auto const &L : userLabels) {
687     if (L->match(Label, DL->getInlinedAt(), Idx)) {
688       Found = true;
689       break;
690     }
691   }
692   if (!Found)
693     userLabels.push_back(std::make_unique<UserLabel>(Label, DL, Idx));
694 
695   return true;
696 }
697 
698 bool LDVImpl::collectDebugValues(MachineFunction &mf) {
699   bool Changed = false;
700   for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
701        ++MFI) {
702     MachineBasicBlock *MBB = &*MFI;
703     for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
704          MBBI != MBBE;) {
705       // Use the first debug instruction in the sequence to get a SlotIndex
706       // for following consecutive debug instructions.
707       if (!MBBI->isDebugInstr()) {
708         ++MBBI;
709         continue;
710       }
711       // Debug instructions has no slot index. Use the previous
712       // non-debug instruction's SlotIndex as its SlotIndex.
713       SlotIndex Idx =
714           MBBI == MBB->begin()
715               ? LIS->getMBBStartIdx(MBB)
716               : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
717       // Handle consecutive debug instructions with the same slot index.
718       do {
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);
724           Changed = true;
725         } else
726           ++MBBI;
727       } while (MBBI != MBBE && MBBI->isDebugInstr());
728     }
729   }
730   return Changed;
731 }
732 
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);
740 
741   // Limit to VNI's live range.
742   bool ToEnd = true;
743   if (LR && VNI) {
744     LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
745     if (!Segment || Segment->valno != VNI) {
746       if (Kills)
747         Kills->push_back(Start);
748       return;
749     }
750     if (Segment->end < Stop) {
751       Stop = Segment->end;
752       ToEnd = false;
753     }
754   }
755 
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)
761       return;
762     // This is a one-slot placeholder. Just skip it.
763     ++I;
764   }
765 
766   // Limited by the next def.
767   if (I.valid() && I.start() < Stop)
768     Stop = I.start();
769   // Limited by VNI's live range.
770   else if (!ToEnd && Kills)
771     Kills->push_back(Stop);
772 
773   if (Start < Stop)
774     I.insert(Start, Stop, Loc);
775 }
776 
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) {
782   if (Kills.empty())
783     return;
784   // Don't track copies from physregs, there are too many uses.
785   if (!Register::isVirtualRegister(LI->reg))
786     return;
787 
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())
794       continue;
795     Register DstReg = MI->getOperand(0).getReg();
796 
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))
802       continue;
803 
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)
809       continue;
810 
811     if (!LIS.hasInterval(DstReg))
812       continue;
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));
817   }
818 
819   if (CopyValues.empty())
820     return;
821 
822   LLVM_DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI
823                     << '\n');
824 
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)
832         continue;
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)
836         continue;
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));
845       break;
846     }
847   }
848 }
849 
850 void UserValue::computeIntervals(MachineRegisterInfo &MRI,
851                                  const TargetRegisterInfo &TRI,
852                                  LiveIntervals &LIS, LexicalScopes &LS) {
853   SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs;
854 
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()));
859 
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()];
865 
866     if (!LocMO.isReg()) {
867       extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS);
868       continue;
869     }
870 
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);
878       }
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,
891                           LIS);
892       continue;
893     }
894 
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
900     // function).
901   }
902 
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.
908 
909   LexicalScope *Scope = LS.findLexicalScope(dl);
910   if (!Scope)
911     return;
912 
913   SlotIndex PrevEnd;
914   LocMap::iterator I = locInts.begin();
915 
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);
923 
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);
928 
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();
934 
935       // Stop overlaps previous end - trim the end of the interval to
936       // the scope range.
937       I.setStopUnchecked(PrevEnd);
938       ++I;
939 
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).
943       if (RStart < IStop)
944         I.insert(RStart, IStop, Loc);
945     }
946 
947     // Advance I so that I.stop() >= RStart, and check for overlap.
948     I.advanceTo(RStart);
949     if (!I.valid())
950       return;
951 
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);
957     }
958 
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();
963 
964     // Advance I to first interval outside current range.
965     I.advanceTo(REnd);
966     if (!I.valid())
967       return;
968 
969     PrevEnd = REnd;
970   }
971 
972   // Check for overlap with end of final range.
973   if (PrevEnd && I.start() < PrevEnd)
974     I.setStopUnchecked(PrevEnd);
975 }
976 
977 void LDVImpl::computeIntervals() {
978   LexicalScopes LS;
979   LS.initialize(*MF);
980 
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);
984   }
985 }
986 
987 bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
988   clear();
989   MF = &mf;
990   LIS = &pass.getAnalysis<LiveIntervals>();
991   TRI = mf.getSubtarget().getRegisterInfo();
992   LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
993                     << mf.getName() << " **********\n");
994 
995   bool Changed = collectDebugValues(mf);
996   computeIntervals();
997   LLVM_DEBUG(print(dbgs()));
998   ModifiedMF = Changed;
999   return Changed;
1000 }
1001 
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()) {
1006         ++MBBI;
1007         continue;
1008       }
1009       MBBI = MBB.erase(MBBI);
1010     }
1011   }
1012 }
1013 
1014 bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
1015   if (!EnableLDV)
1016     return false;
1017   if (!mf.getFunction().getSubprogram()) {
1018     removeDebugValues(mf);
1019     return false;
1020   }
1021   if (!pImpl)
1022     pImpl = new LDVImpl(this);
1023   return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
1024 }
1025 
1026 void LiveDebugVariables::releaseMemory() {
1027   if (pImpl)
1028     static_cast<LDVImpl*>(pImpl)->clear();
1029 }
1030 
1031 LiveDebugVariables::~LiveDebugVariables() {
1032   if (pImpl)
1033     delete static_cast<LDVImpl*>(pImpl);
1034 }
1035 
1036 //===----------------------------------------------------------------------===//
1037 //                           Live Range Splitting
1038 //===----------------------------------------------------------------------===//
1039 
1040 bool
1041 UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
1042                          LiveIntervals& LIS) {
1043   LLVM_DEBUG({
1044     dbgs() << "Splitting Loc" << OldLocNo << '\t';
1045     print(dbgs(), nullptr);
1046   });
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]);
1052     if (LI->empty())
1053       continue;
1054 
1055     // Don't allocate the new LocNo until it is needed.
1056     unsigned NewLocNo = UndefLocNo;
1057 
1058     // Iterate over the overlaps between locInts and LI.
1059     LocMapI.find(LI->beginIndex());
1060     if (!LocMapI.valid())
1061       continue;
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());
1067       if (LII == LIE)
1068         break;
1069 
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);
1077           DidChange = true;
1078         }
1079 
1080         SlotIndex LStart = LocMapI.start();
1081         SlotIndex LStop  = LocMapI.stop();
1082         DbgValueLocation OldLoc = LocMapI.value();
1083 
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);
1089 
1090         // Change the value in the overlap. This may trigger coalescing.
1091         LocMapI.setValue(OldLoc.changeLocNo(NewLocNo));
1092 
1093         // Re-insert any removed OldLocNo ranges.
1094         if (LStart < LocMapI.start()) {
1095           LocMapI.insert(LStart, LocMapI.start(), OldLoc);
1096           ++LocMapI;
1097           assert(LocMapI.valid() && "Unexpected coalescing");
1098         }
1099         if (LStop > LocMapI.stop()) {
1100           ++LocMapI;
1101           LocMapI.insert(LII->end, LStop, OldLoc);
1102           --LocMapI;
1103         }
1104       }
1105 
1106       // Advance to the next overlap.
1107       if (LII->end < LocMapI.stop()) {
1108         if (++LII == LIE)
1109           break;
1110         LocMapI.advanceTo(LII->start);
1111       } else {
1112         ++LocMapI;
1113         if (!LocMapI.valid())
1114           break;
1115         LII = LI->advanceTo(LII, LocMapI.start());
1116       }
1117     }
1118   }
1119 
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);
1128 
1129   LLVM_DEBUG({
1130     dbgs() << "Split result: \t";
1131     print(dbgs(), nullptr);
1132   });
1133   return DidChange;
1134 }
1135 
1136 bool
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)
1146       continue;
1147     DidChange |= splitLocation(LocNo, NewRegs, LIS);
1148   }
1149   return DidChange;
1150 }
1151 
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);
1156 
1157   if (!DidChange)
1158     return;
1159 
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);
1164 }
1165 
1166 void LiveDebugVariables::
1167 splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
1168   if (pImpl)
1169     static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
1170 }
1171 
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.
1202         unsigned SpillSize;
1203         const MachineRegisterInfo &MRI = MF.getRegInfo();
1204         const TargetRegisterClass *TRC = MRI.getRegClass(VirtReg);
1205         bool Success = TII.getStackSlotRange(TRC, Loc.getSubReg(), SpillSize,
1206                                              SpillOffset, MF);
1207 
1208         // FIXME: Invalidate the location if the offset couldn't be calculated.
1209         (void)Success;
1210 
1211         Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
1212         Spilled = true;
1213       } else {
1214         Loc.setReg(0);
1215         Loc.setSubReg(0);
1216       }
1217     }
1218 
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;
1224   }
1225 
1226   // Rewrite the locations and record the stack slot offsets for spills.
1227   locations.clear();
1228   SpillOffsets.clear();
1229   for (auto &Pair : NewLocations) {
1230     bool Spilled;
1231     unsigned SpillOffset;
1232     std::tie(Spilled, SpillOffset) = Pair.second;
1233     locations.push_back(Pair.first);
1234     if (Spilled) {
1235       unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
1236       SpillOffsets[NewLocNo] = SpillOffset;
1237     }
1238   }
1239 
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().
1248     if (Loc.isUndef())
1249       continue;
1250     unsigned NewLocNo = LocNoMap[Loc.locNo()];
1251     I.setValueUnchecked(Loc.changeLocNo(NewLocNo));
1252     I.setStart(I.start());
1253   }
1254 }
1255 
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();
1262 
1263   // Try to find an insert location by going backwards from Idx.
1264   MachineInstr *MI;
1265   while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1266     // We've reached the beginning of MBB.
1267     if (Idx == Start) {
1268       MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
1269       return I;
1270     }
1271     Idx = Idx.getPrevIndex();
1272   }
1273 
1274   // Don't insert anything after the first terminator, though.
1275   return MI->isTerminator() ? MBB->getFirstTerminator() :
1276                               std::next(MachineBasicBlock::iterator(MI));
1277 }
1278 
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,
1285                        LiveIntervals &LIS,
1286                        const TargetRegisterInfo &TRI) {
1287   if (!LocMO.isReg())
1288     return MBB->instr_end();
1289   Register Reg = LocMO.getReg();
1290 
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)))
1295       break;
1296     if (I->definesRegister(Reg, &TRI))
1297       // The insert location is directly after the instruction/bundle.
1298       return std::next(I);
1299     ++I;
1300   }
1301   return MBB->end();
1302 }
1303 
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);
1321 
1322   ++NumInsertedDebugValues;
1323 
1324   assert(cast<DILocalVariable>(Variable)
1325              ->isValidLocationForIntrinsic(getDebugLoc()) &&
1326          "Expected inlined-at fields to agree");
1327 
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();
1335   if (Spilled) {
1336     if (IsIndirect)
1337       DIExprFlags |= DIExpression::DerefAfter;
1338     Expr =
1339         DIExpression::prepend(Expr, DIExprFlags, SpillOffset);
1340     IsIndirect = true;
1341   }
1342 
1343   assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index");
1344 
1345   do {
1346     BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
1347             IsIndirect, MO, Variable, Expr);
1348 
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());
1353 }
1354 
1355 void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
1356                                  LiveIntervals &LIS,
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);
1362 }
1363 
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();
1369 
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();
1374     auto SpillIt =
1375         !Loc.isUndef() ? SpillOffsets.find(Loc.locNo()) : SpillOffsets.end();
1376     bool Spilled = SpillIt != SpillOffsets.end();
1377     unsigned SpillOffset = Spilled ? SpillIt->second : 0;
1378 
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();
1384 
1385     LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo());
1386     MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1387     SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
1388 
1389     LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1390     insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1391                      TRI);
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.
1396       Start = MBBEnd;
1397       if (++MBB == MFEnd)
1398         break;
1399       MBBEnd = LIS.getMBBEndIdx(&*MBB);
1400       LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1401       insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1402                        TRI);
1403     }
1404     LLVM_DEBUG(dbgs() << '\n');
1405     if (MBB == MFEnd)
1406       break;
1407 
1408     ++I;
1409   }
1410 }
1411 
1412 void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII) {
1413   LLVM_DEBUG(dbgs() << "\t" << loc);
1414   MachineFunction::iterator MBB = LIS.getMBBFromIndex(loc)->getIterator();
1415 
1416   LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB));
1417   insertDebugLabel(&*MBB, loc, LIS, TII);
1418 
1419   LLVM_DEBUG(dbgs() << '\n');
1420 }
1421 
1422 void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
1423   LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
1424   if (!MF)
1425     return;
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);
1432   }
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);
1437   }
1438   EmitDone = true;
1439 }
1440 
1441 void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
1442   if (pImpl)
1443     static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1444 }
1445 
1446 bool LiveDebugVariables::doInitialization(Module &M) {
1447   return Pass::doInitialization(M);
1448 }
1449 
1450 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1451 LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
1452   if (pImpl)
1453     static_cast<LDVImpl*>(pImpl)->print(dbgs());
1454 }
1455 #endif
1456