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 unsigned Position = 0;
56 for (const MachineBasicBlock &MBB : MF)
57 for (const MachineInstr &MI : MBB)
58 InstNumberMap[&MI] = MI.isMetaInstruction() ? Position : ++Position;
59}
60
61bool InstructionOrdering::isBefore(const MachineInstr *A,
62 const MachineInstr *B) const {
63 assert(A->getParent() && B->getParent() && "Operands must have a parent");
64 assert(A->getMF() == B->getMF() &&
65 "Operands must be in the same MachineFunction");
66 return InstNumberMap.lookup(Val: A) < InstNumberMap.lookup(Val: B);
67}
68
69bool DbgValueHistoryMap::startDbgValue(InlinedEntity Var,
70 const MachineInstr &MI,
71 EntryIndex &NewIndex) {
72 // Instruction range should start with a DBG_VALUE instruction for the
73 // variable.
74 assert(MI.isDebugValue() && "not a DBG_VALUE");
75 auto &Entries = VarEntries[Var];
76 if (!Entries.empty() && Entries.back().isDbgValue() &&
77 !Entries.back().isClosed() &&
78 Entries.back().getInstr()->isEquivalentDbgInstr(Other: MI)) {
79 LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
80 << "\t" << Entries.back().getInstr() << "\t" << MI
81 << "\n");
82 return false;
83 }
84 Entries.emplace_back(Args: &MI, Args: Entry::DbgValue);
85 NewIndex = Entries.size() - 1;
86 return true;
87}
88
89EntryIndex DbgValueHistoryMap::startClobber(InlinedEntity Var,
90 const MachineInstr &MI) {
91 auto &Entries = VarEntries[Var];
92 // If an instruction clobbers multiple registers that the variable is
93 // described by, then we may have already created a clobbering instruction.
94 if (Entries.back().isClobber() && Entries.back().getInstr() == &MI)
95 return Entries.size() - 1;
96 Entries.emplace_back(Args: &MI, Args: Entry::Clobber);
97 return Entries.size() - 1;
98}
99
100void DbgValueHistoryMap::Entry::endEntry(EntryIndex Index) {
101 // For now, instruction ranges are not allowed to cross basic block
102 // boundaries.
103 assert(isDbgValue() && "Setting end index for non-debug value");
104 assert(!isClosed() && "End index has already been set");
105 EndIndex = Index;
106}
107
108/// Check if the instruction range [StartMI, EndMI] intersects any instruction
109/// range in Ranges. EndMI can be nullptr to indicate that the range is
110/// unbounded. Assumes Ranges is ordered and disjoint. Returns true and points
111/// to the first intersecting scope range if one exists.
112static std::optional<ArrayRef<InsnRange>::iterator>
113intersects(const MachineInstr *StartMI, const MachineInstr *EndMI,
114 ArrayRef<InsnRange> Ranges, const InstructionOrdering &Ordering) {
115 for (auto RangesI = Ranges.begin(), RangesE = Ranges.end();
116 RangesI != RangesE; ++RangesI) {
117 if (EndMI && Ordering.isBefore(A: EndMI, B: RangesI->first))
118 return std::nullopt;
119 if (EndMI && !Ordering.isBefore(A: RangesI->second, B: EndMI))
120 return RangesI;
121 if (Ordering.isBefore(A: StartMI, B: RangesI->second))
122 return RangesI;
123 }
124 return std::nullopt;
125}
126
127void DbgValueHistoryMap::trimLocationRanges(
128 const MachineFunction &MF, LexicalScopes &LScopes,
129 const InstructionOrdering &Ordering) {
130 // The indices of the entries we're going to remove for each variable.
131 SmallVector<EntryIndex, 4> ToRemove;
132 // Entry reference count for each variable. Clobbers left with no references
133 // will be removed.
134 SmallVector<int, 4> ReferenceCount;
135 // Entries reference other entries by index. Offsets is used to remap these
136 // references if any entries are removed.
137 SmallVector<size_t, 4> Offsets;
138
139 LLVM_DEBUG(dbgs() << "Trimming location ranges for function '" << MF.getName()
140 << "'\n");
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>(Val: Entity.first);
149
150 LexicalScope *Scope = nullptr;
151 if (const DILocation *InlinedAt = Entity.second) {
152 Scope = LScopes.findInlinedScope(N: LocalVar->getScope(), IA: InlinedAt);
153 } else {
154 Scope = LScopes.findLexicalScope(N: 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(NumElts: HistoryMapEntries.size(), Elt: 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, Ranges: ScopeRanges, Ordering)) {
205 // Adjust ScopeRanges to exclude ranges which subsequent location ranges
206 // cannot possibly intersect.
207 ScopeRanges = ArrayRef<InsnRange>(*R, 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(Elt: 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 LLVM_DEBUG(dbgs() << "Dropping value outside scope range of variable: ";
218 StartMI->print(llvm::dbgs()););
219 }
220 }
221
222 // If there is nothing to remove then jump to next variable.
223 if (ToRemove.empty())
224 continue;
225
226 // Mark clobbers that will no longer close any location ranges for removal.
227 for (size_t i = 0; i < HistoryMapEntries.size(); ++i)
228 if (ReferenceCount[i] <= 0 && HistoryMapEntries[i].isClobber())
229 ToRemove.push_back(Elt: i);
230
231 llvm::sort(C&: ToRemove);
232
233 // Build an offset map so we can update the EndIndex of the remaining
234 // entries.
235 // Zero the offsets.
236 Offsets.assign(NumElts: HistoryMapEntries.size(), Elt: 0);
237 size_t CurOffset = 0;
238 auto ToRemoveItr = ToRemove.begin();
239 for (size_t EntryIdx = *ToRemoveItr; EntryIdx < HistoryMapEntries.size();
240 ++EntryIdx) {
241 // Check if this is an entry which will be removed.
242 if (ToRemoveItr != ToRemove.end() && *ToRemoveItr == EntryIdx) {
243 ++ToRemoveItr;
244 ++CurOffset;
245 }
246 Offsets[EntryIdx] = CurOffset;
247 }
248
249 // Update the EndIndex of the entries to account for those which will be
250 // removed.
251 for (auto &Entry : HistoryMapEntries)
252 if (Entry.isClosed())
253 Entry.EndIndex -= Offsets[Entry.EndIndex];
254
255 // Now actually remove the entries. Iterate backwards so that our remaining
256 // ToRemove indices are valid after each erase.
257 for (EntryIndex Idx : llvm::reverse(C&: ToRemove))
258 HistoryMapEntries.erase(CI: HistoryMapEntries.begin() + Idx);
259 LLVM_DEBUG(llvm::dbgs() << "New HistoryMap('" << LocalVar->getName()
260 << "') size: " << HistoryMapEntries.size() << "\n");
261 }
262}
263
264bool DbgValueHistoryMap::hasNonEmptyLocation(const Entries &Entries) const {
265 for (const auto &Entry : Entries) {
266 if (!Entry.isDbgValue())
267 continue;
268
269 const MachineInstr *MI = Entry.getInstr();
270 assert(MI->isDebugValue());
271 // A DBG_VALUE $noreg is an empty variable location
272 if (MI->isUndefDebugValue())
273 continue;
274
275 return true;
276 }
277
278 return false;
279}
280
281void DbgLabelInstrMap::addInstr(InlinedEntity Label, const MachineInstr &MI) {
282 assert(MI.isDebugLabel() && "not a DBG_LABEL");
283 LabelInstr[Label] = &MI;
284}
285
286namespace {
287
288// Maps physreg numbers to the variables they describe.
289using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
290using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedEntity, 1>>;
291
292// Keeps track of the debug value entries that are currently live for each
293// inlined entity. As the history map entries are stored in a SmallVector, they
294// may be moved at insertion of new entries, so store indices rather than
295// pointers.
296using DbgValueEntriesMap = std::map<InlinedEntity, SmallSet<EntryIndex, 1>>;
297
298} // end anonymous namespace
299
300// Claim that @Var is not described by @RegNo anymore.
301static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
302 InlinedEntity Var) {
303 const auto &I = RegVars.find(x: RegNo);
304 assert(RegNo != 0U && I != RegVars.end());
305 auto &VarSet = I->second;
306 const auto &VarPos = llvm::find(Range&: VarSet, Val: Var);
307 assert(VarPos != VarSet.end());
308 VarSet.erase(CI: VarPos);
309 // Don't keep empty sets in a map to keep it as small as possible.
310 if (VarSet.empty())
311 RegVars.erase(position: I);
312}
313
314// Claim that @Var is now described by @RegNo.
315static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
316 InlinedEntity Var) {
317 assert(RegNo != 0U);
318 auto &VarSet = RegVars[RegNo];
319 assert(!is_contained(VarSet, Var));
320 VarSet.push_back(Elt: Var);
321}
322
323/// Create a clobbering entry and end all open debug value entries
324/// for \p Var that are described by \p RegNo using that entry. Inserts into \p
325/// FellowRegisters the set of Registers that were also used to describe \p Var
326/// alongside \p RegNo.
327static void clobberRegEntries(InlinedEntity Var, unsigned RegNo,
328 const MachineInstr &ClobberingInstr,
329 DbgValueEntriesMap &LiveEntries,
330 DbgValueHistoryMap &HistMap,
331 SmallVectorImpl<Register> &FellowRegisters) {
332 EntryIndex ClobberIndex = HistMap.startClobber(Var, MI: ClobberingInstr);
333 // Close all entries whose values are described by the register.
334 SmallVector<EntryIndex, 4> IndicesToErase;
335 // If a given register appears in a live DBG_VALUE_LIST for Var alongside the
336 // clobbered register, and never appears in a live DBG_VALUE* for Var without
337 // the clobbered register, then it is no longer linked to the variable.
338 SmallSet<Register, 4> MaybeRemovedRegisters;
339 SmallSet<Register, 4> KeepRegisters;
340 for (auto Index : LiveEntries[Var]) {
341 auto &Entry = HistMap.getEntry(Var, Index);
342 assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
343 if (Entry.getInstr()->isDebugEntryValue())
344 continue;
345 if (Entry.getInstr()->hasDebugOperandForReg(Reg: RegNo)) {
346 IndicesToErase.push_back(Elt: Index);
347 Entry.endEntry(Index: ClobberIndex);
348 for (const auto &MO : Entry.getInstr()->debug_operands())
349 if (MO.isReg() && MO.getReg() && MO.getReg() != RegNo)
350 MaybeRemovedRegisters.insert(V: MO.getReg());
351 } else {
352 for (const auto &MO : Entry.getInstr()->debug_operands())
353 if (MO.isReg() && MO.getReg())
354 KeepRegisters.insert(V: MO.getReg());
355 }
356 }
357
358 for (Register Reg : MaybeRemovedRegisters)
359 if (!KeepRegisters.contains(V: Reg))
360 FellowRegisters.push_back(Elt: Reg);
361
362 // Drop all entries that have ended.
363 auto &Entries = LiveEntries[Var];
364 for (auto Index : IndicesToErase)
365 Entries.erase(V: Index);
366}
367
368/// Add a new debug value for \p Var. Closes all overlapping debug values.
369static void handleNewDebugValue(InlinedEntity Var, const MachineInstr &DV,
370 RegDescribedVarsMap &RegVars,
371 DbgValueEntriesMap &LiveEntries,
372 DbgValueHistoryMap &HistMap) {
373 EntryIndex NewIndex;
374 if (HistMap.startDbgValue(Var, MI: DV, NewIndex)) {
375 // As we already need to iterate all LiveEntries when handling a DbgValue,
376 // we use this map to avoid a more expensive check against RegVars. There
377 // is an assert that we handle this correctly in addRegDescribedVar.
378 //
379 // In other terms, the presence in this map indicates the presence of a
380 // corresponding entry in RegVars.
381 //
382 // The bool value then tracks whether an entry is to be retained (true) or
383 // removed (false); as we end previous entries we speculatively assume they
384 // can be dropped from RegVars, but we then also visit the new entry whose
385 // set of debug register operands may overlap and "save" a reg from being
386 // dropped.
387 SmallDenseMap<unsigned, bool, 4> TrackedRegs;
388
389 // If we have created a new debug value entry, close all preceding
390 // live entries that overlap.
391 SmallVector<EntryIndex, 4> IndicesToErase;
392 const DIExpression *DIExpr = DV.getDebugExpression();
393 for (auto Index : LiveEntries[Var]) {
394 auto &Entry = HistMap.getEntry(Var, Index);
395 assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
396 const MachineInstr &DV = *Entry.getInstr();
397 bool Overlaps = DIExpr->fragmentsOverlap(Other: DV.getDebugExpression());
398 if (Overlaps) {
399 IndicesToErase.push_back(Elt: Index);
400 Entry.endEntry(Index: NewIndex);
401 }
402 if (!DV.isDebugEntryValue())
403 for (const MachineOperand &Op : DV.debug_operands())
404 if (Op.isReg() && Op.getReg())
405 TrackedRegs[Op.getReg()] |= !Overlaps;
406 }
407
408 // If the new debug value is described by a register, add tracking of
409 // that register if it is not already tracked.
410 if (!DV.isDebugEntryValue()) {
411 for (const MachineOperand &Op : DV.debug_operands()) {
412 if (Op.isReg() && Op.getReg()) {
413 Register NewReg = Op.getReg();
414 if (TrackedRegs.insert_or_assign(Key: NewReg, Val: true).second)
415 addRegDescribedVar(RegVars, RegNo: NewReg, Var);
416 LiveEntries[Var].insert(V: NewIndex);
417 }
418 }
419 }
420
421 // Drop tracking of registers that are no longer used.
422 for (auto I : TrackedRegs)
423 if (!I.second)
424 dropRegDescribedVar(RegVars, RegNo: I.first, Var);
425
426 // Drop all entries that have ended, and mark the new entry as live.
427 auto &Entries = LiveEntries[Var];
428 for (auto Index : IndicesToErase)
429 Entries.erase(V: Index);
430 Entries.insert(V: NewIndex);
431 }
432}
433
434// Terminate the location range for variables described by register at
435// @I by inserting @ClobberingInstr to their history.
436static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
437 RegDescribedVarsMap::iterator I,
438 DbgValueHistoryMap &HistMap,
439 DbgValueEntriesMap &LiveEntries,
440 const MachineInstr &ClobberingInstr) {
441 // Iterate over all variables described by this register and add this
442 // instruction to their history, clobbering it. All registers that also
443 // describe the clobbered variables (i.e. in variadic debug values) will have
444 // those Variables removed from their DescribedVars.
445 for (const auto &Var : I->second) {
446 SmallVector<Register, 4> FellowRegisters;
447 clobberRegEntries(Var, RegNo: I->first, ClobberingInstr, LiveEntries, HistMap,
448 FellowRegisters);
449 for (Register RegNo : FellowRegisters)
450 dropRegDescribedVar(RegVars, RegNo, Var);
451 }
452 RegVars.erase(position: I);
453}
454
455// Terminate the location range for variables described by register
456// @RegNo by inserting @ClobberingInstr to their history.
457static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
458 DbgValueHistoryMap &HistMap,
459 DbgValueEntriesMap &LiveEntries,
460 const MachineInstr &ClobberingInstr) {
461 const auto &I = RegVars.find(x: RegNo);
462 if (I == RegVars.end())
463 return;
464 clobberRegisterUses(RegVars, I, HistMap, LiveEntries, ClobberingInstr);
465}
466
467void llvm::calculateDbgEntityHistory(const MachineFunction *MF,
468 const TargetRegisterInfo *TRI,
469 DbgValueHistoryMap &DbgValues,
470 DbgLabelInstrMap &DbgLabels) {
471 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
472 Register SP = TLI->getStackPointerRegisterToSaveRestore();
473 Register FrameReg = TRI->getFrameRegister(MF: *MF);
474 RegDescribedVarsMap RegVars;
475 DbgValueEntriesMap LiveEntries;
476 for (const auto &MBB : *MF) {
477 for (const auto &MI : MBB) {
478 if (MI.isDebugValue()) {
479 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
480 const DILocalVariable *RawVar = MI.getDebugVariable();
481 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
482 "Expected inlined-at fields to agree");
483 InlinedEntity Var(RawVar, MI.getDebugLoc()->getInlinedAt());
484
485 handleNewDebugValue(Var, DV: MI, RegVars, LiveEntries, HistMap&: DbgValues);
486 } else if (MI.isDebugLabel()) {
487 assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!");
488 const DILabel *RawLabel = MI.getDebugLabel();
489 assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
490 "Expected inlined-at fields to agree");
491 // When collecting debug information for labels, there is no MCSymbol
492 // generated for it. So, we keep MachineInstr in DbgLabels in order
493 // to query MCSymbol afterward.
494 InlinedEntity L(RawLabel, MI.getDebugLoc()->getInlinedAt());
495 DbgLabels.addInstr(Label: L, MI);
496 }
497
498 // Meta Instructions have no output and do not change any values and so
499 // can be safely ignored.
500 if (MI.isMetaInstruction())
501 continue;
502
503 // Other instructions may clobber registers which describe some variables.
504 for (const MachineOperand &MO : MI.operands()) {
505 if (MO.isReg() && MO.isDef() && MO.getReg()) {
506 // Ignore call instructions that claim to clobber SP. The AArch64
507 // backend does this for aggregate function arguments.
508 if (MI.isCall() && MO.getReg() == SP)
509 continue;
510 // If this is a virtual register, only clobber it since it doesn't
511 // have aliases.
512 if (MO.getReg().isVirtual())
513 clobberRegisterUses(RegVars, RegNo: MO.getReg(), HistMap&: DbgValues, LiveEntries,
514 ClobberingInstr: MI);
515 // If this is a register def operand, it may end a debug value
516 // range. Ignore frame-register defs in the epilogue and prologue,
517 // we expect debuggers to understand that stack-locations are
518 // invalid outside of the function body.
519 else if (MO.getReg() != FrameReg ||
520 (!MI.getFlag(Flag: MachineInstr::FrameDestroy) &&
521 !MI.getFlag(Flag: MachineInstr::FrameSetup))) {
522 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
523 ++AI)
524 clobberRegisterUses(RegVars, RegNo: *AI, HistMap&: DbgValues, LiveEntries, ClobberingInstr: MI);
525 }
526 } else if (MO.isRegMask()) {
527 // If this is a register mask operand, clobber all debug values in
528 // non-CSRs.
529 SmallVector<unsigned, 32> RegsToClobber;
530 // Don't consider SP to be clobbered by register masks.
531 for (auto It : RegVars) {
532 unsigned int Reg = It.first;
533 if (Reg != SP && Register::isPhysicalRegister(Reg) &&
534 MO.clobbersPhysReg(PhysReg: Reg))
535 RegsToClobber.push_back(Elt: Reg);
536 }
537
538 for (unsigned Reg : RegsToClobber) {
539 clobberRegisterUses(RegVars, RegNo: Reg, HistMap&: DbgValues, LiveEntries, ClobberingInstr: MI);
540 }
541 }
542 } // End MO loop.
543 } // End instr loop.
544
545 // Make sure locations for all variables are valid only until the end of
546 // the basic block (unless it's the last basic block, in which case let
547 // their liveness run off to the end of the function).
548 if (!MBB.empty() && &MBB != &MF->back()) {
549 // Iterate over all variables that have open debug values.
550 for (auto &Pair : LiveEntries) {
551 if (Pair.second.empty())
552 continue;
553
554 // Create a clobbering entry.
555 EntryIndex ClobIdx = DbgValues.startClobber(Var: Pair.first, MI: MBB.back());
556
557 // End all entries.
558 for (EntryIndex Idx : Pair.second) {
559 DbgValueHistoryMap::Entry &Ent = DbgValues.getEntry(Var: Pair.first, Index: Idx);
560 assert(Ent.isDbgValue() && !Ent.isClosed());
561 Ent.endEntry(Index: ClobIdx);
562 }
563 }
564
565 LiveEntries.clear();
566 RegVars.clear();
567 }
568 }
569}
570
571#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
572LLVM_DUMP_METHOD void DbgValueHistoryMap::dump(StringRef FuncName) const {
573 dbgs() << "DbgValueHistoryMap('" << FuncName << "'):\n";
574 for (const auto &VarRangePair : *this) {
575 const InlinedEntity &Var = VarRangePair.first;
576 const Entries &Entries = VarRangePair.second;
577
578 const DILocalVariable *LocalVar = cast<DILocalVariable>(Var.first);
579 const DILocation *Location = Var.second;
580
581 dbgs() << " - " << LocalVar->getName() << " at ";
582
583 if (Location)
584 dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
585 << Location->getColumn();
586 else
587 dbgs() << "<unknown location>";
588
589 dbgs() << " --\n";
590
591 for (const auto &E : enumerate(Entries)) {
592 const auto &Entry = E.value();
593 dbgs() << " Entry[" << E.index() << "]: ";
594 if (Entry.isDbgValue())
595 dbgs() << "Debug value\n";
596 else
597 dbgs() << "Clobber\n";
598 dbgs() << " Instr: " << *Entry.getInstr();
599 if (Entry.isDbgValue()) {
600 if (Entry.getEndIndex() == NoEntry)
601 dbgs() << " - Valid until end of function\n";
602 else
603 dbgs() << " - Closed by Entry[" << Entry.getEndIndex() << "]\n";
604 }
605 dbgs() << "\n";
606 }
607 }
608}
609#endif
610