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