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

source code of llvm/lib/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp