xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 //===- llvm/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp -------------===//
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 #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
10 #include "llvm/ADT/BitVector.h"
11 #include "llvm/ADT/Optional.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/SmallSet.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/CodeGen/LexicalScopes.h"
16 #include "llvm/CodeGen/MachineBasicBlock.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineOperand.h"
20 #include "llvm/CodeGen/TargetLowering.h"
21 #include "llvm/CodeGen/TargetRegisterInfo.h"
22 #include "llvm/CodeGen/TargetSubtargetInfo.h"
23 #include "llvm/IR/DebugInfoMetadata.h"
24 #include "llvm/IR/DebugLoc.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <cassert>
29 #include <map>
30 #include <utility>
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "dwarfdebug"
35 
36 namespace {
37 using EntryIndex = DbgValueHistoryMap::EntryIndex;
38 }
39 
40 void InstructionOrdering::initialize(const MachineFunction &MF) {
41   // We give meta instructions the same ordinal as the preceding instruction
42   // because this class is written for the task of comparing positions of
43   // variable location ranges against scope ranges. To reflect what we'll see
44   // in the binary, when we look at location ranges we must consider all
45   // DBG_VALUEs between two real instructions at the same position. And a
46   // scope range which ends on a meta instruction should be considered to end
47   // at the last seen real instruction. E.g.
48   //
49   //  1 instruction p      Both the variable location for x and for y start
50   //  1 DBG_VALUE for "x"  after instruction p so we give them all the same
51   //  1 DBG_VALUE for "y"  number. If a scope range ends at DBG_VALUE for "y",
52   //  2 instruction q      we should treat it as ending after instruction p
53   //                       because it will be the last real instruction in the
54   //                       range. DBG_VALUEs at or after this position for
55   //                       variables declared in the scope will have no effect.
56   clear();
57   unsigned Position = 0;
58   for (const MachineBasicBlock &MBB : MF)
59     for (const MachineInstr &MI : MBB)
60       InstNumberMap[&MI] = MI.isMetaInstruction() ? Position : ++Position;
61 }
62 
63 bool InstructionOrdering::isBefore(const MachineInstr *A,
64                                    const MachineInstr *B) const {
65   assert(A->getParent() && B->getParent() && "Operands must have a parent");
66   assert(A->getMF() == B->getMF() &&
67          "Operands must be in the same MachineFunction");
68   return InstNumberMap.lookup(A) < InstNumberMap.lookup(B);
69 }
70 
71 bool DbgValueHistoryMap::startDbgValue(InlinedEntity Var,
72                                        const MachineInstr &MI,
73                                        EntryIndex &NewIndex) {
74   // Instruction range should start with a DBG_VALUE instruction for the
75   // variable.
76   assert(MI.isDebugValue() && "not a DBG_VALUE");
77   auto &Entries = VarEntries[Var];
78   if (!Entries.empty() && Entries.back().isDbgValue() &&
79       !Entries.back().isClosed() &&
80       Entries.back().getInstr()->isIdenticalTo(MI)) {
81     LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
82                       << "\t" << Entries.back().getInstr() << "\t" << MI
83                       << "\n");
84     return false;
85   }
86   Entries.emplace_back(&MI, Entry::DbgValue);
87   NewIndex = Entries.size() - 1;
88   return true;
89 }
90 
91 EntryIndex DbgValueHistoryMap::startClobber(InlinedEntity Var,
92                                             const MachineInstr &MI) {
93   auto &Entries = VarEntries[Var];
94   // If an instruction clobbers multiple registers that the variable is
95   // described by, then we may have already created a clobbering instruction.
96   if (Entries.back().isClobber() && Entries.back().getInstr() == &MI)
97     return Entries.size() - 1;
98   Entries.emplace_back(&MI, Entry::Clobber);
99   return Entries.size() - 1;
100 }
101 
102 void DbgValueHistoryMap::Entry::endEntry(EntryIndex Index) {
103   // For now, instruction ranges are not allowed to cross basic block
104   // boundaries.
105   assert(isDbgValue() && "Setting end index for non-debug value");
106   assert(!isClosed() && "End index has already been set");
107   EndIndex = Index;
108 }
109 
110 /// Check if the instruction range [StartMI, EndMI] intersects any instruction
111 /// range in Ranges. EndMI can be nullptr to indicate that the range is
112 /// unbounded. Assumes Ranges is ordered and disjoint. Returns true and points
113 /// to the first intersecting scope range if one exists.
114 static Optional<ArrayRef<InsnRange>::iterator>
115 intersects(const MachineInstr *StartMI, const MachineInstr *EndMI,
116            const ArrayRef<InsnRange> &Ranges,
117            const InstructionOrdering &Ordering) {
118   for (auto RangesI = Ranges.begin(), RangesE = Ranges.end();
119        RangesI != RangesE; ++RangesI) {
120     if (EndMI && Ordering.isBefore(EndMI, RangesI->first))
121       return None;
122     if (EndMI && !Ordering.isBefore(RangesI->second, EndMI))
123       return RangesI;
124     if (Ordering.isBefore(StartMI, RangesI->second))
125       return RangesI;
126   }
127   return None;
128 }
129 
130 void DbgValueHistoryMap::trimLocationRanges(
131     const MachineFunction &MF, LexicalScopes &LScopes,
132     const InstructionOrdering &Ordering) {
133   // The indices of the entries we're going to remove for each variable.
134   SmallVector<EntryIndex, 4> ToRemove;
135   // Entry reference count for each variable. Clobbers left with no references
136   // will be removed.
137   SmallVector<int, 4> ReferenceCount;
138   // Entries reference other entries by index. Offsets is used to remap these
139   // references if any entries are removed.
140   SmallVector<size_t, 4> Offsets;
141 
142   for (auto &Record : VarEntries) {
143     auto &HistoryMapEntries = Record.second;
144     if (HistoryMapEntries.empty())
145       continue;
146 
147     InlinedEntity Entity = Record.first;
148     const DILocalVariable *LocalVar = cast<DILocalVariable>(Entity.first);
149 
150     LexicalScope *Scope = nullptr;
151     if (const DILocation *InlinedAt = Entity.second) {
152       Scope = LScopes.findInlinedScope(LocalVar->getScope(), InlinedAt);
153     } else {
154       Scope = LScopes.findLexicalScope(LocalVar->getScope());
155       // Ignore variables for non-inlined function level scopes. The scope
156       // ranges (from scope->getRanges()) will not include any instructions
157       // before the first one with a debug-location, which could cause us to
158       // incorrectly drop a location. We could introduce special casing for
159       // these variables, but it doesn't seem worth it because no out-of-scope
160       // locations have been observed for variables declared in function level
161       // scopes.
162       if (Scope &&
163           (Scope->getScopeNode() == Scope->getScopeNode()->getSubprogram()) &&
164           (Scope->getScopeNode() == LocalVar->getScope()))
165         continue;
166     }
167 
168     // If there is no scope for the variable then something has probably gone
169     // wrong.
170     if (!Scope)
171       continue;
172 
173     ToRemove.clear();
174     // Zero the reference counts.
175     ReferenceCount.assign(HistoryMapEntries.size(), 0);
176     // Index of the DBG_VALUE which marks the start of the current location
177     // range.
178     EntryIndex StartIndex = 0;
179     ArrayRef<InsnRange> ScopeRanges(Scope->getRanges());
180     for (auto EI = HistoryMapEntries.begin(), EE = HistoryMapEntries.end();
181          EI != EE; ++EI, ++StartIndex) {
182       // Only DBG_VALUEs can open location ranges so skip anything else.
183       if (!EI->isDbgValue())
184         continue;
185 
186       // Index of the entry which closes this range.
187       EntryIndex EndIndex = EI->getEndIndex();
188       // If this range is closed bump the reference count of the closing entry.
189       if (EndIndex != NoEntry)
190         ReferenceCount[EndIndex] += 1;
191       // Skip this location range if the opening entry is still referenced. It
192       // may close a location range which intersects a scope range.
193       // TODO: We could be 'smarter' and trim these kinds of ranges such that
194       // they do not leak out of the scope ranges if they partially overlap.
195       if (ReferenceCount[StartIndex] > 0)
196         continue;
197 
198       const MachineInstr *StartMI = EI->getInstr();
199       const MachineInstr *EndMI = EndIndex != NoEntry
200                                       ? HistoryMapEntries[EndIndex].getInstr()
201                                       : nullptr;
202       // Check if the location range [StartMI, EndMI] intersects with any scope
203       // range for the variable.
204       if (auto R = intersects(StartMI, EndMI, ScopeRanges, Ordering)) {
205         // Adjust ScopeRanges to exclude ranges which subsequent location ranges
206         // cannot possibly intersect.
207         ScopeRanges = ArrayRef<InsnRange>(R.getValue(), ScopeRanges.end());
208       } else {
209         // If the location range does not intersect any scope range then the
210         // DBG_VALUE which opened this location range is usless, mark it for
211         // removal.
212         ToRemove.push_back(StartIndex);
213         // Because we'll be removing this entry we need to update the reference
214         // count of the closing entry, if one exists.
215         if (EndIndex != NoEntry)
216           ReferenceCount[EndIndex] -= 1;
217       }
218     }
219 
220     // If there is nothing to remove then jump to next variable.
221     if (ToRemove.empty())
222       continue;
223 
224     // Mark clobbers that will no longer close any location ranges for removal.
225     for (size_t i = 0; i < HistoryMapEntries.size(); ++i)
226       if (ReferenceCount[i] <= 0 && HistoryMapEntries[i].isClobber())
227         ToRemove.push_back(i);
228 
229     llvm::sort(ToRemove);
230 
231     // Build an offset map so we can update the EndIndex of the remaining
232     // entries.
233     // Zero the offsets.
234     Offsets.assign(HistoryMapEntries.size(), 0);
235     size_t CurOffset = 0;
236     auto ToRemoveItr = ToRemove.begin();
237     for (size_t EntryIdx = *ToRemoveItr; EntryIdx < HistoryMapEntries.size();
238          ++EntryIdx) {
239       // Check if this is an entry which will be removed.
240       if (ToRemoveItr != ToRemove.end() && *ToRemoveItr == EntryIdx) {
241         ++ToRemoveItr;
242         ++CurOffset;
243       }
244       Offsets[EntryIdx] = CurOffset;
245     }
246 
247     // Update the EndIndex of the entries to account for those which will be
248     // removed.
249     for (auto &Entry : HistoryMapEntries)
250       if (Entry.isClosed())
251         Entry.EndIndex -= Offsets[Entry.EndIndex];
252 
253     // Now actually remove the entries. Iterate backwards so that our remaining
254     // ToRemove indices are valid after each erase.
255     for (EntryIndex Idx : llvm::reverse(ToRemove))
256       HistoryMapEntries.erase(HistoryMapEntries.begin() + Idx);
257   }
258 }
259 
260 bool DbgValueHistoryMap::hasNonEmptyLocation(const Entries &Entries) const {
261   for (const auto &Entry : Entries) {
262     if (!Entry.isDbgValue())
263       continue;
264 
265     const MachineInstr *MI = Entry.getInstr();
266     assert(MI->isDebugValue());
267     // A DBG_VALUE $noreg is an empty variable location
268     if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg() == 0)
269       continue;
270 
271     return true;
272   }
273 
274   return false;
275 }
276 
277 void DbgLabelInstrMap::addInstr(InlinedEntity Label, const MachineInstr &MI) {
278   assert(MI.isDebugLabel() && "not a DBG_LABEL");
279   LabelInstr[Label] = &MI;
280 }
281 
282 namespace {
283 
284 // Maps physreg numbers to the variables they describe.
285 using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
286 using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedEntity, 1>>;
287 
288 // Keeps track of the debug value entries that are currently live for each
289 // inlined entity. As the history map entries are stored in a SmallVector, they
290 // may be moved at insertion of new entries, so store indices rather than
291 // pointers.
292 using DbgValueEntriesMap = std::map<InlinedEntity, SmallSet<EntryIndex, 1>>;
293 
294 } // end anonymous namespace
295 
296 // Claim that @Var is not described by @RegNo anymore.
297 static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
298                                 InlinedEntity Var) {
299   const auto &I = RegVars.find(RegNo);
300   assert(RegNo != 0U && I != RegVars.end());
301   auto &VarSet = I->second;
302   const auto &VarPos = llvm::find(VarSet, Var);
303   assert(VarPos != VarSet.end());
304   VarSet.erase(VarPos);
305   // Don't keep empty sets in a map to keep it as small as possible.
306   if (VarSet.empty())
307     RegVars.erase(I);
308 }
309 
310 // Claim that @Var is now described by @RegNo.
311 static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
312                                InlinedEntity Var) {
313   assert(RegNo != 0U);
314   auto &VarSet = RegVars[RegNo];
315   assert(!is_contained(VarSet, Var));
316   VarSet.push_back(Var);
317 }
318 
319 /// Create a clobbering entry and end all open debug value entries
320 /// for \p Var that are described by \p RegNo using that entry. Inserts into \p
321 /// FellowRegisters the set of Registers that were also used to describe \p Var
322 /// alongside \p RegNo.
323 static void clobberRegEntries(InlinedEntity Var, unsigned RegNo,
324                               const MachineInstr &ClobberingInstr,
325                               DbgValueEntriesMap &LiveEntries,
326                               DbgValueHistoryMap &HistMap,
327                               SmallVectorImpl<Register> &FellowRegisters) {
328   EntryIndex ClobberIndex = HistMap.startClobber(Var, ClobberingInstr);
329   // Close all entries whose values are described by the register.
330   SmallVector<EntryIndex, 4> IndicesToErase;
331   // If a given register appears in a live DBG_VALUE_LIST for Var alongside the
332   // clobbered register, and never appears in a live DBG_VALUE* for Var without
333   // the clobbered register, then it is no longer linked to the variable.
334   SmallSet<Register, 4> MaybeRemovedRegisters;
335   SmallSet<Register, 4> KeepRegisters;
336   for (auto Index : LiveEntries[Var]) {
337     auto &Entry = HistMap.getEntry(Var, Index);
338     assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
339     if (Entry.getInstr()->isDebugEntryValue())
340       continue;
341     if (Entry.getInstr()->hasDebugOperandForReg(RegNo)) {
342       IndicesToErase.push_back(Index);
343       Entry.endEntry(ClobberIndex);
344       for (auto &MO : Entry.getInstr()->debug_operands())
345         if (MO.isReg() && MO.getReg() && MO.getReg() != RegNo)
346           MaybeRemovedRegisters.insert(MO.getReg());
347     } else {
348       for (auto &MO : Entry.getInstr()->debug_operands())
349         if (MO.isReg() && MO.getReg())
350           KeepRegisters.insert(MO.getReg());
351     }
352   }
353 
354   for (Register Reg : MaybeRemovedRegisters)
355     if (!KeepRegisters.contains(Reg))
356       FellowRegisters.push_back(Reg);
357 
358   // Drop all entries that have ended.
359   for (auto Index : IndicesToErase)
360     LiveEntries[Var].erase(Index);
361 }
362 
363 /// Add a new debug value for \p Var. Closes all overlapping debug values.
364 static void handleNewDebugValue(InlinedEntity Var, const MachineInstr &DV,
365                                 RegDescribedVarsMap &RegVars,
366                                 DbgValueEntriesMap &LiveEntries,
367                                 DbgValueHistoryMap &HistMap) {
368   EntryIndex NewIndex;
369   if (HistMap.startDbgValue(Var, DV, NewIndex)) {
370     SmallDenseMap<unsigned, bool, 4> TrackedRegs;
371 
372     // If we have created a new debug value entry, close all preceding
373     // live entries that overlap.
374     SmallVector<EntryIndex, 4> IndicesToErase;
375     const DIExpression *DIExpr = DV.getDebugExpression();
376     for (auto Index : LiveEntries[Var]) {
377       auto &Entry = HistMap.getEntry(Var, Index);
378       assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
379       const MachineInstr &DV = *Entry.getInstr();
380       bool Overlaps = DIExpr->fragmentsOverlap(DV.getDebugExpression());
381       if (Overlaps) {
382         IndicesToErase.push_back(Index);
383         Entry.endEntry(NewIndex);
384       }
385       if (!DV.isDebugEntryValue())
386         for (const MachineOperand &Op : DV.debug_operands())
387           if (Op.isReg() && Op.getReg())
388             TrackedRegs[Op.getReg()] |= !Overlaps;
389     }
390 
391     // If the new debug value is described by a register, add tracking of
392     // that register if it is not already tracked.
393     if (!DV.isDebugEntryValue()) {
394       for (const MachineOperand &Op : DV.debug_operands()) {
395         if (Op.isReg() && Op.getReg()) {
396           Register NewReg = Op.getReg();
397           if (!TrackedRegs.count(NewReg))
398             addRegDescribedVar(RegVars, NewReg, Var);
399           LiveEntries[Var].insert(NewIndex);
400           TrackedRegs[NewReg] = true;
401         }
402       }
403     }
404 
405     // Drop tracking of registers that are no longer used.
406     for (auto I : TrackedRegs)
407       if (!I.second)
408         dropRegDescribedVar(RegVars, I.first, Var);
409 
410     // Drop all entries that have ended, and mark the new entry as live.
411     for (auto Index : IndicesToErase)
412       LiveEntries[Var].erase(Index);
413     LiveEntries[Var].insert(NewIndex);
414   }
415 }
416 
417 // Terminate the location range for variables described by register at
418 // @I by inserting @ClobberingInstr to their history.
419 static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
420                                 RegDescribedVarsMap::iterator I,
421                                 DbgValueHistoryMap &HistMap,
422                                 DbgValueEntriesMap &LiveEntries,
423                                 const MachineInstr &ClobberingInstr) {
424   // Iterate over all variables described by this register and add this
425   // instruction to their history, clobbering it. All registers that also
426   // describe the clobbered variables (i.e. in variadic debug values) will have
427   // those Variables removed from their DescribedVars.
428   for (const auto &Var : I->second) {
429     SmallVector<Register, 4> FellowRegisters;
430     clobberRegEntries(Var, I->first, ClobberingInstr, LiveEntries, HistMap,
431                       FellowRegisters);
432     for (Register RegNo : FellowRegisters)
433       dropRegDescribedVar(RegVars, RegNo, Var);
434   }
435   RegVars.erase(I);
436 }
437 
438 // Terminate the location range for variables described by register
439 // @RegNo by inserting @ClobberingInstr to their history.
440 static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
441                                 DbgValueHistoryMap &HistMap,
442                                 DbgValueEntriesMap &LiveEntries,
443                                 const MachineInstr &ClobberingInstr) {
444   const auto &I = RegVars.find(RegNo);
445   if (I == RegVars.end())
446     return;
447   clobberRegisterUses(RegVars, I, HistMap, LiveEntries, ClobberingInstr);
448 }
449 
450 void llvm::calculateDbgEntityHistory(const MachineFunction *MF,
451                                      const TargetRegisterInfo *TRI,
452                                      DbgValueHistoryMap &DbgValues,
453                                      DbgLabelInstrMap &DbgLabels) {
454   const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
455   Register SP = TLI->getStackPointerRegisterToSaveRestore();
456   Register FrameReg = TRI->getFrameRegister(*MF);
457   RegDescribedVarsMap RegVars;
458   DbgValueEntriesMap LiveEntries;
459   for (const auto &MBB : *MF) {
460     for (const auto &MI : MBB) {
461       if (MI.isDebugValue()) {
462         assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
463         // Use the base variable (without any DW_OP_piece expressions)
464         // as index into History. The full variables including the
465         // piece expressions are attached to the MI.
466         const DILocalVariable *RawVar = MI.getDebugVariable();
467         assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
468                "Expected inlined-at fields to agree");
469         InlinedEntity Var(RawVar, MI.getDebugLoc()->getInlinedAt());
470 
471         handleNewDebugValue(Var, MI, RegVars, LiveEntries, DbgValues);
472       } else if (MI.isDebugLabel()) {
473         assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!");
474         const DILabel *RawLabel = MI.getDebugLabel();
475         assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
476             "Expected inlined-at fields to agree");
477         // When collecting debug information for labels, there is no MCSymbol
478         // generated for it. So, we keep MachineInstr in DbgLabels in order
479         // to query MCSymbol afterward.
480         InlinedEntity L(RawLabel, MI.getDebugLoc()->getInlinedAt());
481         DbgLabels.addInstr(L, MI);
482       }
483 
484       // Meta Instructions have no output and do not change any values and so
485       // can be safely ignored.
486       if (MI.isMetaInstruction())
487         continue;
488 
489       // Not a DBG_VALUE instruction. It may clobber registers which describe
490       // some variables.
491       for (const MachineOperand &MO : MI.operands()) {
492         if (MO.isReg() && MO.isDef() && MO.getReg()) {
493           // Ignore call instructions that claim to clobber SP. The AArch64
494           // backend does this for aggregate function arguments.
495           if (MI.isCall() && MO.getReg() == SP)
496             continue;
497           // If this is a virtual register, only clobber it since it doesn't
498           // have aliases.
499           if (Register::isVirtualRegister(MO.getReg()))
500             clobberRegisterUses(RegVars, MO.getReg(), DbgValues, LiveEntries,
501                                 MI);
502           // If this is a register def operand, it may end a debug value
503           // range. Ignore frame-register defs in the epilogue and prologue,
504           // we expect debuggers to understand that stack-locations are
505           // invalid outside of the function body.
506           else if (MO.getReg() != FrameReg ||
507                    (!MI.getFlag(MachineInstr::FrameDestroy) &&
508                    !MI.getFlag(MachineInstr::FrameSetup))) {
509             for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
510                  ++AI)
511               clobberRegisterUses(RegVars, *AI, DbgValues, LiveEntries, MI);
512           }
513         } else if (MO.isRegMask()) {
514           // If this is a register mask operand, clobber all debug values in
515           // non-CSRs.
516           SmallVector<unsigned, 32> RegsToClobber;
517           // Don't consider SP to be clobbered by register masks.
518           for (auto It : RegVars) {
519             unsigned int Reg = It.first;
520             if (Reg != SP && Register::isPhysicalRegister(Reg) &&
521                 MO.clobbersPhysReg(Reg))
522               RegsToClobber.push_back(Reg);
523           }
524 
525           for (unsigned Reg : RegsToClobber) {
526             clobberRegisterUses(RegVars, Reg, DbgValues, LiveEntries, MI);
527           }
528         }
529       } // End MO loop.
530     }   // End instr loop.
531 
532     // Make sure locations for all variables are valid only until the end of
533     // the basic block (unless it's the last basic block, in which case let
534     // their liveness run off to the end of the function).
535     if (!MBB.empty() && &MBB != &MF->back()) {
536       // Iterate over all variables that have open debug values.
537       for (auto &Pair : LiveEntries) {
538         if (Pair.second.empty())
539           continue;
540 
541         // Create a clobbering entry.
542         EntryIndex ClobIdx = DbgValues.startClobber(Pair.first, MBB.back());
543 
544         // End all entries.
545         for (EntryIndex Idx : Pair.second) {
546           DbgValueHistoryMap::Entry &Ent = DbgValues.getEntry(Pair.first, Idx);
547           assert(Ent.isDbgValue() && !Ent.isClosed());
548           Ent.endEntry(ClobIdx);
549         }
550       }
551 
552       LiveEntries.clear();
553       RegVars.clear();
554     }
555   }
556 }
557 
558 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
559 LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const {
560   dbgs() << "DbgValueHistoryMap:\n";
561   for (const auto &VarRangePair : *this) {
562     const InlinedEntity &Var = VarRangePair.first;
563     const Entries &Entries = VarRangePair.second;
564 
565     const DILocalVariable *LocalVar = cast<DILocalVariable>(Var.first);
566     const DILocation *Location = Var.second;
567 
568     dbgs() << " - " << LocalVar->getName() << " at ";
569 
570     if (Location)
571       dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
572              << Location->getColumn();
573     else
574       dbgs() << "<unknown location>";
575 
576     dbgs() << " --\n";
577 
578     for (const auto &E : enumerate(Entries)) {
579       const auto &Entry = E.value();
580       dbgs() << "  Entry[" << E.index() << "]: ";
581       if (Entry.isDbgValue())
582         dbgs() << "Debug value\n";
583       else
584         dbgs() << "Clobber\n";
585       dbgs() << "   Instr: " << *Entry.getInstr();
586       if (Entry.isDbgValue()) {
587         if (Entry.getEndIndex() == NoEntry)
588           dbgs() << "   - Valid until end of function\n";
589         else
590           dbgs() << "   - Closed by Entry[" << Entry.getEndIndex() << "]\n";
591       }
592       dbgs() << "\n";
593     }
594   }
595 }
596 #endif
597