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