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