1//===- VarLocBasedImpl.cpp - Tracking Debug Value MIs with VarLoc class----===//
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/// \file VarLocBasedImpl.cpp
10///
11/// LiveDebugValues is an optimistic "available expressions" dataflow
12/// algorithm. The set of expressions is the set of machine locations
13/// (registers, spill slots, constants, and target indices) that a variable
14/// fragment might be located, qualified by a DIExpression and indirect-ness
15/// flag, while each variable is identified by a DebugVariable object. The
16/// availability of an expression begins when a DBG_VALUE instruction specifies
17/// the location of a DebugVariable, and continues until that location is
18/// clobbered or re-specified by a different DBG_VALUE for the same
19/// DebugVariable.
20///
21/// The output of LiveDebugValues is additional DBG_VALUE instructions,
22/// placed to extend variable locations as far they're available. This file
23/// and the VarLocBasedLDV class is an implementation that explicitly tracks
24/// locations, using the VarLoc class.
25///
26/// The canonical "available expressions" problem doesn't have expression
27/// clobbering, instead when a variable is re-assigned, any expressions using
28/// that variable get invalidated. LiveDebugValues can map onto "available
29/// expressions" by having every register represented by a variable, which is
30/// used in an expression that becomes available at a DBG_VALUE instruction.
31/// When the register is clobbered, its variable is effectively reassigned, and
32/// expressions computed from it become unavailable. A similar construct is
33/// needed when a DebugVariable has its location re-specified, to invalidate
34/// all other locations for that DebugVariable.
35///
36/// Using the dataflow analysis to compute the available expressions, we create
37/// a DBG_VALUE at the beginning of each block where the expression is
38/// live-in. This propagates variable locations into every basic block where
39/// the location can be determined, rather than only having DBG_VALUEs in blocks
40/// where locations are specified due to an assignment or some optimization.
41/// Movements of values between registers and spill slots are annotated with
42/// DBG_VALUEs too to track variable values bewteen locations. All this allows
43/// DbgEntityHistoryCalculator to focus on only the locations within individual
44/// blocks, facilitating testing and improving modularity.
45///
46/// We follow an optimisic dataflow approach, with this lattice:
47///
48/// \verbatim
49/// ┬ "Unknown"
50/// |
51/// v
52/// True
53/// |
54/// v
55/// ⊥ False
56/// \endverbatim With "True" signifying that the expression is available (and
57/// thus a DebugVariable's location is the corresponding register), while
58/// "False" signifies that the expression is unavailable. "Unknown"s never
59/// survive to the end of the analysis (see below).
60///
61/// Formally, all DebugVariable locations that are live-out of a block are
62/// initialized to \top. A blocks live-in values take the meet of the lattice
63/// value for every predecessors live-outs, except for the entry block, where
64/// all live-ins are \bot. The usual dataflow propagation occurs: the transfer
65/// function for a block assigns an expression for a DebugVariable to be "True"
66/// if a DBG_VALUE in the block specifies it; "False" if the location is
67/// clobbered; or the live-in value if it is unaffected by the block. We
68/// visit each block in reverse post order until a fixedpoint is reached. The
69/// solution produced is maximal.
70///
71/// Intuitively, we start by assuming that every expression / variable location
72/// is at least "True", and then propagate "False" from the entry block and any
73/// clobbers until there are no more changes to make. This gives us an accurate
74/// solution because all incorrect locations will have a "False" propagated into
75/// them. It also gives us a solution that copes well with loops by assuming
76/// that variable locations are live-through every loop, and then removing those
77/// that are not through dataflow.
78///
79/// Within LiveDebugValues: each variable location is represented by a
80/// VarLoc object that identifies the source variable, the set of
81/// machine-locations that currently describe it (a single location for
82/// DBG_VALUE or multiple for DBG_VALUE_LIST), and the DBG_VALUE inst that
83/// specifies the location. Each VarLoc is indexed in the (function-scope) \p
84/// VarLocMap, giving each VarLoc a set of unique indexes, each of which
85/// corresponds to one of the VarLoc's machine-locations and can be used to
86/// lookup the VarLoc in the VarLocMap. Rather than operate directly on machine
87/// locations, the dataflow analysis in this pass identifies locations by their
88/// indices in the VarLocMap, meaning all the variable locations in a block can
89/// be described by a sparse vector of VarLocMap indices.
90///
91/// All the storage for the dataflow analysis is local to the ExtendRanges
92/// method and passed down to helper methods. "OutLocs" and "InLocs" record the
93/// in and out lattice values for each block. "OpenRanges" maintains a list of
94/// variable locations and, with the "process" method, evaluates the transfer
95/// function of each block. "flushPendingLocs" installs debug value instructions
96/// for each live-in location at the start of blocks, while "Transfers" records
97/// transfers of values between machine-locations.
98///
99/// We avoid explicitly representing the "Unknown" (\top) lattice value in the
100/// implementation. Instead, unvisited blocks implicitly have all lattice
101/// values set as "Unknown". After being visited, there will be path back to
102/// the entry block where the lattice value is "False", and as the transfer
103/// function cannot make new "Unknown" locations, there are no scenarios where
104/// a block can have an "Unknown" location after being visited. Similarly, we
105/// don't enumerate all possible variable locations before exploring the
106/// function: when a new location is discovered, all blocks previously explored
107/// were implicitly "False" but unrecorded, and become explicitly "False" when
108/// a new VarLoc is created with its bit not set in predecessor InLocs or
109/// OutLocs.
110///
111//===----------------------------------------------------------------------===//
112
113#include "LiveDebugValues.h"
114
115#include "llvm/ADT/CoalescingBitVector.h"
116#include "llvm/ADT/DenseMap.h"
117#include "llvm/ADT/PostOrderIterator.h"
118#include "llvm/ADT/SmallPtrSet.h"
119#include "llvm/ADT/SmallSet.h"
120#include "llvm/ADT/SmallVector.h"
121#include "llvm/ADT/Statistic.h"
122#include "llvm/BinaryFormat/Dwarf.h"
123#include "llvm/CodeGen/LexicalScopes.h"
124#include "llvm/CodeGen/MachineBasicBlock.h"
125#include "llvm/CodeGen/MachineFunction.h"
126#include "llvm/CodeGen/MachineInstr.h"
127#include "llvm/CodeGen/MachineInstrBuilder.h"
128#include "llvm/CodeGen/MachineInstrBundle.h"
129#include "llvm/CodeGen/MachineMemOperand.h"
130#include "llvm/CodeGen/MachineOperand.h"
131#include "llvm/CodeGen/PseudoSourceValue.h"
132#include "llvm/CodeGen/TargetFrameLowering.h"
133#include "llvm/CodeGen/TargetInstrInfo.h"
134#include "llvm/CodeGen/TargetLowering.h"
135#include "llvm/CodeGen/TargetRegisterInfo.h"
136#include "llvm/CodeGen/TargetSubtargetInfo.h"
137#include "llvm/Config/llvm-config.h"
138#include "llvm/IR/DebugInfoMetadata.h"
139#include "llvm/IR/DebugLoc.h"
140#include "llvm/IR/Function.h"
141#include "llvm/MC/MCRegisterInfo.h"
142#include "llvm/Support/Casting.h"
143#include "llvm/Support/Debug.h"
144#include "llvm/Support/TypeSize.h"
145#include "llvm/Support/raw_ostream.h"
146#include "llvm/Target/TargetMachine.h"
147#include <cassert>
148#include <cstdint>
149#include <functional>
150#include <map>
151#include <optional>
152#include <queue>
153#include <tuple>
154#include <utility>
155#include <vector>
156
157using namespace llvm;
158
159#define DEBUG_TYPE "live-debug-values"
160
161STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted");
162
163/// If \p Op is a stack or frame register return true, otherwise return false.
164/// This is used to avoid basing the debug entry values on the registers, since
165/// we do not support it at the moment.
166static bool isRegOtherThanSPAndFP(const MachineOperand &Op,
167 const MachineInstr &MI,
168 const TargetRegisterInfo *TRI) {
169 if (!Op.isReg())
170 return false;
171
172 const MachineFunction *MF = MI.getParent()->getParent();
173 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
174 Register SP = TLI->getStackPointerRegisterToSaveRestore();
175 Register FP = TRI->getFrameRegister(MF: *MF);
176 Register Reg = Op.getReg();
177
178 return Reg && Reg != SP && Reg != FP;
179}
180
181namespace {
182
183// Max out the number of statically allocated elements in DefinedRegsSet, as
184// this prevents fallback to std::set::count() operations.
185using DefinedRegsSet = SmallSet<Register, 32>;
186
187// The IDs in this set correspond to MachineLocs in VarLocs, as well as VarLocs
188// that represent Entry Values; every VarLoc in the set will also appear
189// exactly once at Location=0.
190// As a result, each VarLoc may appear more than once in this "set", but each
191// range corresponding to a Reg, SpillLoc, or EntryValue type will still be a
192// "true" set (i.e. each VarLoc may appear only once), and the range Location=0
193// is the set of all VarLocs.
194using VarLocSet = CoalescingBitVector<uint64_t>;
195
196/// A type-checked pair of {Register Location (or 0), Index}, used to index
197/// into a \ref VarLocMap. This can be efficiently converted to a 64-bit int
198/// for insertion into a \ref VarLocSet, and efficiently converted back. The
199/// type-checker helps ensure that the conversions aren't lossy.
200///
201/// Why encode a location /into/ the VarLocMap index? This makes it possible
202/// to find the open VarLocs killed by a register def very quickly. This is a
203/// performance-critical operation for LiveDebugValues.
204struct LocIndex {
205 using u32_location_t = uint32_t;
206 using u32_index_t = uint32_t;
207
208 u32_location_t Location; // Physical registers live in the range [1;2^30) (see
209 // \ref MCRegister), so we have plenty of range left
210 // here to encode non-register locations.
211 u32_index_t Index;
212
213 /// The location that has an entry for every VarLoc in the map.
214 static constexpr u32_location_t kUniversalLocation = 0;
215
216 /// The first location that is reserved for VarLocs with locations of kind
217 /// RegisterKind.
218 static constexpr u32_location_t kFirstRegLocation = 1;
219
220 /// The first location greater than 0 that is not reserved for VarLocs with
221 /// locations of kind RegisterKind.
222 static constexpr u32_location_t kFirstInvalidRegLocation = 1 << 30;
223
224 /// A special location reserved for VarLocs with locations of kind
225 /// SpillLocKind.
226 static constexpr u32_location_t kSpillLocation = kFirstInvalidRegLocation;
227
228 /// A special location reserved for VarLocs of kind EntryValueBackupKind and
229 /// EntryValueCopyBackupKind.
230 static constexpr u32_location_t kEntryValueBackupLocation =
231 kFirstInvalidRegLocation + 1;
232
233 /// A special location reserved for VarLocs with locations of kind
234 /// WasmLocKind.
235 /// TODO Placing all Wasm target index locations in this single kWasmLocation
236 /// may cause slowdown in compilation time in very large functions. Consider
237 /// giving a each target index/offset pair its own u32_location_t if this
238 /// becomes a problem.
239 static constexpr u32_location_t kWasmLocation = kFirstInvalidRegLocation + 2;
240
241 /// The first location that is reserved for VarLocs with locations of kind
242 /// VirtualRegisterKind.
243 static constexpr u32_location_t kFirstVirtualRegLocation = 1 << 31;
244
245 LocIndex(u32_location_t Location, u32_index_t Index)
246 : Location(Location), Index(Index) {}
247
248 uint64_t getAsRawInteger() const {
249 return (static_cast<uint64_t>(Location) << 32) | Index;
250 }
251
252 template<typename IntT> static LocIndex fromRawInteger(IntT ID) {
253 static_assert(std::is_unsigned_v<IntT> && sizeof(ID) == sizeof(uint64_t),
254 "Cannot convert raw integer to LocIndex");
255 return {static_cast<u32_location_t>(ID >> 32),
256 static_cast<u32_index_t>(ID)};
257 }
258
259 /// Get the start of the interval reserved for VarLocs of kind RegisterKind
260 /// which reside in \p Reg. The end is at rawIndexForReg(Reg+1)-1.
261 static uint64_t rawIndexForReg(Register Reg) {
262 return LocIndex(Reg, 0).getAsRawInteger();
263 }
264
265 /// Return a range covering all set indices in the interval reserved for
266 /// \p Location in \p Set.
267 static auto indexRangeForLocation(const VarLocSet &Set,
268 u32_location_t Location) {
269 uint64_t Start = LocIndex(Location, 0).getAsRawInteger();
270 uint64_t End = LocIndex(Location + 1, 0).getAsRawInteger();
271 return Set.half_open_range(Start, End);
272 }
273};
274
275// Simple Set for storing all the VarLoc Indices at a Location bucket.
276using VarLocsInRange = SmallSet<LocIndex::u32_index_t, 32>;
277// Vector of all `LocIndex`s for a given VarLoc; the same Location should not
278// appear in any two of these, as each VarLoc appears at most once in any
279// Location bucket.
280using LocIndices = SmallVector<LocIndex, 2>;
281
282class VarLocBasedLDV : public LDVImpl {
283private:
284 const TargetRegisterInfo *TRI;
285 const TargetInstrInfo *TII;
286 const TargetFrameLowering *TFI;
287 bool ShouldEmitDebugEntryValues;
288 BitVector CalleeSavedRegs;
289 LexicalScopes LS;
290 VarLocSet::Allocator Alloc;
291
292 const MachineInstr *LastNonDbgMI;
293
294 enum struct TransferKind { TransferCopy, TransferSpill, TransferRestore };
295
296 using FragmentInfo = DIExpression::FragmentInfo;
297 using OptFragmentInfo = std::optional<DIExpression::FragmentInfo>;
298
299 /// A pair of debug variable and value location.
300 struct VarLoc {
301 // The location at which a spilled variable resides. It consists of a
302 // register and an offset.
303 struct SpillLoc {
304 unsigned SpillBase;
305 StackOffset SpillOffset;
306 bool operator==(const SpillLoc &Other) const {
307 return SpillBase == Other.SpillBase && SpillOffset == Other.SpillOffset;
308 }
309 bool operator!=(const SpillLoc &Other) const {
310 return !(*this == Other);
311 }
312 };
313
314 // Target indices used for wasm-specific locations.
315 struct WasmLoc {
316 // One of TargetIndex values defined in WebAssembly.h. We deal with
317 // local-related TargetIndex in this analysis (TI_LOCAL and
318 // TI_LOCAL_INDIRECT). Stack operands (TI_OPERAND_STACK) will be handled
319 // separately WebAssemblyDebugFixup pass, and we don't associate debug
320 // info with values in global operands (TI_GLOBAL_RELOC) at the moment.
321 int Index;
322 int64_t Offset;
323 bool operator==(const WasmLoc &Other) const {
324 return Index == Other.Index && Offset == Other.Offset;
325 }
326 bool operator!=(const WasmLoc &Other) const { return !(*this == Other); }
327 };
328
329 struct GlobalAddr {
330 const GlobalValue *GV;
331 int64_t Offset;
332 bool operator==(const GlobalAddr &Other) const {
333 return GV == Other.GV && Offset == Other.Offset;
334 }
335 };
336
337 /// Identity of the variable at this location.
338 const DebugVariable Var;
339
340 /// The expression applied to this location.
341 const DIExpression *Expr;
342
343 /// DBG_VALUE to clone var/expr information from if this location
344 /// is moved.
345 const MachineInstr &MI;
346
347 enum class MachineLocKind {
348 InvalidKind = 0,
349 RegisterKind,
350 SpillLocKind,
351 ImmediateKind,
352 WasmLocKind,
353 GlobalAddrKind
354 };
355
356 enum class EntryValueLocKind {
357 NonEntryValueKind = 0,
358 EntryValueKind,
359 EntryValueBackupKind,
360 EntryValueCopyBackupKind
361 } EVKind = EntryValueLocKind::NonEntryValueKind;
362
363 /// The value location. Stored separately to avoid repeatedly
364 /// extracting it from MI.
365 union MachineLocValue {
366 uint64_t RegNo;
367 SpillLoc SpillLocation;
368 uint64_t Hash;
369 int64_t Immediate;
370 const ConstantFP *FPImm;
371 const ConstantInt *CImm;
372 WasmLoc WasmLocation;
373 GlobalAddr GlobalAddress;
374 MachineLocValue() : Hash(0) {}
375 };
376
377 /// A single machine location; its Kind is a register, a spill location, an
378 /// immediate value, a WebAssembly local, or the address of a global.
379 /// If the VarLoc is not a NonEntryValueKind, then it will use only a
380 /// single MachineLoc of RegisterKind.
381 struct MachineLoc {
382 MachineLocKind Kind;
383 MachineLocValue Value;
384 bool operator==(const MachineLoc &Other) const {
385 if (Kind != Other.Kind)
386 return false;
387 switch (Kind) {
388 case MachineLocKind::SpillLocKind:
389 return Value.SpillLocation == Other.Value.SpillLocation;
390 case MachineLocKind::WasmLocKind:
391 return Value.WasmLocation == Other.Value.WasmLocation;
392 case MachineLocKind::GlobalAddrKind:
393 return Value.GlobalAddress == Other.Value.GlobalAddress;
394 case MachineLocKind::RegisterKind:
395 case MachineLocKind::ImmediateKind:
396 return Value.Hash == Other.Value.Hash;
397 default:
398 llvm_unreachable("Invalid kind");
399 }
400 }
401 bool operator<(const MachineLoc &Other) const {
402 // Order by kind first, and only then by the payload. Which union member
403 // is the active one depends on the kind, so reading either side's
404 // payload is only well defined once both kinds are known to agree.
405 if (Kind != Other.Kind)
406 return Kind < Other.Kind;
407 switch (Kind) {
408 case MachineLocKind::SpillLocKind:
409 return std::make_tuple(
410 args: Value.SpillLocation.SpillBase,
411 args: Value.SpillLocation.SpillOffset.getFixed(),
412 args: Value.SpillLocation.SpillOffset.getScalable()) <
413 std::make_tuple(
414 args: Other.Value.SpillLocation.SpillBase,
415 args: Other.Value.SpillLocation.SpillOffset.getFixed(),
416 args: Other.Value.SpillLocation.SpillOffset.getScalable());
417 case MachineLocKind::WasmLocKind:
418 return std::tie(args: Value.WasmLocation.Index, args: Value.WasmLocation.Offset) <
419 std::tie(args: Other.Value.WasmLocation.Index,
420 args: Other.Value.WasmLocation.Offset);
421 case MachineLocKind::GlobalAddrKind:
422 return std::tie(args: Value.GlobalAddress.GV, args: Value.GlobalAddress.Offset) <
423 std::tie(args: Other.Value.GlobalAddress.GV,
424 args: Other.Value.GlobalAddress.Offset);
425 case MachineLocKind::RegisterKind:
426 case MachineLocKind::ImmediateKind:
427 return Value.Hash < Other.Value.Hash;
428 default:
429 llvm_unreachable("Invalid kind");
430 }
431 }
432 };
433
434 /// The set of machine locations used to determine the variable's value, in
435 /// conjunction with Expr. Initially populated with MI's debug operands,
436 /// but may be transformed independently afterwards.
437 SmallVector<MachineLoc, 8> Locs;
438 /// Used to map the index of each location in Locs back to the index of its
439 /// original debug operand in MI. Used when multiple location operands are
440 /// coalesced and the original MI's operands need to be accessed while
441 /// emitting a debug value.
442 SmallVector<unsigned, 8> OrigLocMap;
443
444 VarLoc(const MachineInstr &MI)
445 : Var(MI.getDebugVariable(), MI.getDebugExpression(),
446 MI.getDebugLoc()->getInlinedAt()),
447 Expr(MI.getDebugExpression()), MI(MI) {
448 assert(MI.isDebugValue() && "not a DBG_VALUE");
449 assert((MI.isDebugValueList() || MI.getNumOperands() == 4) &&
450 "malformed DBG_VALUE");
451 for (const MachineOperand &Op : MI.debug_operands()) {
452 MachineLoc ML = GetLocForOp(Op);
453 auto It = find(Range&: Locs, Val: ML);
454 if (It == Locs.end()) {
455 Locs.push_back(Elt: ML);
456 OrigLocMap.push_back(Elt: MI.getDebugOperandIndex(Op: &Op));
457 } else {
458 // ML duplicates an element in Locs; replace references to Op
459 // with references to the duplicating element.
460 unsigned OpIdx = Locs.size();
461 unsigned DuplicatingIdx = std::distance(first: Locs.begin(), last: It);
462 Expr = DIExpression::replaceArg(Expr, OldArg: OpIdx, NewArg: DuplicatingIdx);
463 }
464 }
465
466 // We create the debug entry values from the factory functions rather
467 // than from this ctor.
468 assert(EVKind != EntryValueLocKind::EntryValueKind &&
469 !isEntryBackupLoc());
470 }
471
472 static MachineLoc GetLocForOp(const MachineOperand &Op) {
473 MachineLocKind Kind;
474 MachineLocValue Loc;
475 if (Op.isReg()) {
476 Kind = MachineLocKind::RegisterKind;
477 Loc.RegNo = Op.getReg();
478 } else if (Op.isImm()) {
479 Kind = MachineLocKind::ImmediateKind;
480 Loc.Immediate = Op.getImm();
481 } else if (Op.isFPImm()) {
482 Kind = MachineLocKind::ImmediateKind;
483 Loc.FPImm = Op.getFPImm();
484 } else if (Op.isCImm()) {
485 Kind = MachineLocKind::ImmediateKind;
486 Loc.CImm = Op.getCImm();
487 } else if (Op.isTargetIndex()) {
488 Kind = MachineLocKind::WasmLocKind;
489 Loc.WasmLocation = {.Index: Op.getIndex(), .Offset: Op.getOffset()};
490 } else if (Op.isGlobal()) {
491 Kind = MachineLocKind::GlobalAddrKind;
492 Loc.GlobalAddress = {.GV: Op.getGlobal(), .Offset: Op.getOffset()};
493 } else
494 llvm_unreachable("Invalid Op kind for MachineLoc.");
495 return {.Kind: Kind, .Value: Loc};
496 }
497
498 /// Take the variable and machine-location in DBG_VALUE MI, and build an
499 /// entry location using the given expression.
500 static VarLoc CreateEntryLoc(const MachineInstr &MI,
501 const DIExpression *EntryExpr, Register Reg) {
502 VarLoc VL(MI);
503 assert(VL.Locs.size() == 1 &&
504 VL.Locs[0].Kind == MachineLocKind::RegisterKind);
505 VL.EVKind = EntryValueLocKind::EntryValueKind;
506 VL.Expr = EntryExpr;
507 VL.Locs[0].Value.RegNo = Reg;
508 return VL;
509 }
510
511 /// Take the variable and machine-location from the DBG_VALUE (from the
512 /// function entry), and build an entry value backup location. The backup
513 /// location will turn into the normal location if the backup is valid at
514 /// the time of the primary location clobbering.
515 static VarLoc CreateEntryBackupLoc(const MachineInstr &MI,
516 const DIExpression *EntryExpr) {
517 VarLoc VL(MI);
518 assert(VL.Locs.size() == 1 &&
519 VL.Locs[0].Kind == MachineLocKind::RegisterKind);
520 VL.EVKind = EntryValueLocKind::EntryValueBackupKind;
521 VL.Expr = EntryExpr;
522 return VL;
523 }
524
525 /// Take the variable and machine-location from the DBG_VALUE (from the
526 /// function entry), and build a copy of an entry value backup location by
527 /// setting the register location to NewReg.
528 static VarLoc CreateEntryCopyBackupLoc(const MachineInstr &MI,
529 const DIExpression *EntryExpr,
530 Register NewReg) {
531 VarLoc VL(MI);
532 assert(VL.Locs.size() == 1 &&
533 VL.Locs[0].Kind == MachineLocKind::RegisterKind);
534 VL.EVKind = EntryValueLocKind::EntryValueCopyBackupKind;
535 VL.Expr = EntryExpr;
536 VL.Locs[0].Value.RegNo = NewReg;
537 return VL;
538 }
539
540 /// Copy the register location in DBG_VALUE MI, updating the register to
541 /// be NewReg.
542 static VarLoc CreateCopyLoc(const VarLoc &OldVL, const MachineLoc &OldML,
543 Register NewReg) {
544 VarLoc VL = OldVL;
545 for (MachineLoc &ML : VL.Locs)
546 if (ML == OldML) {
547 ML.Kind = MachineLocKind::RegisterKind;
548 ML.Value.RegNo = NewReg;
549 return VL;
550 }
551 llvm_unreachable("Should have found OldML in new VarLoc.");
552 }
553
554 /// Take the variable described by DBG_VALUE* MI, and create a VarLoc
555 /// locating it in the specified spill location.
556 static VarLoc CreateSpillLoc(const VarLoc &OldVL, const MachineLoc &OldML,
557 unsigned SpillBase, StackOffset SpillOffset) {
558 VarLoc VL = OldVL;
559 for (MachineLoc &ML : VL.Locs)
560 if (ML == OldML) {
561 ML.Kind = MachineLocKind::SpillLocKind;
562 ML.Value.SpillLocation = {.SpillBase: SpillBase, .SpillOffset: SpillOffset};
563 return VL;
564 }
565 llvm_unreachable("Should have found OldML in new VarLoc.");
566 }
567
568 /// Create a DBG_VALUE representing this VarLoc in the given function.
569 /// Copies variable-specific information such as DILocalVariable and
570 /// inlining information from the original DBG_VALUE instruction, which may
571 /// have been several transfers ago.
572 MachineInstr *BuildDbgValue(MachineFunction &MF) const {
573 assert(!isEntryBackupLoc() &&
574 "Tried to produce DBG_VALUE for backup VarLoc");
575 const DebugLoc &DbgLoc = MI.getDebugLoc();
576 bool Indirect = MI.isIndirectDebugValue();
577 const auto &IID = MI.getDesc();
578 const DILocalVariable *Var = MI.getDebugVariable();
579 NumInserted++;
580
581 const DIExpression *DIExpr = Expr;
582 SmallVector<MachineOperand, 8> MOs;
583 for (unsigned I = 0, E = Locs.size(); I < E; ++I) {
584 MachineLocKind LocKind = Locs[I].Kind;
585 MachineLocValue Loc = Locs[I].Value;
586 const MachineOperand &Orig = MI.getDebugOperand(Index: OrigLocMap[I]);
587 switch (LocKind) {
588 case MachineLocKind::RegisterKind:
589 // An entry value is a register location -- but with an updated
590 // expression. The register location of such DBG_VALUE is always the
591 // one from the entry DBG_VALUE, it does not matter if the entry value
592 // was copied in to another register due to some optimizations.
593 // Non-entry value register locations are like the source
594 // DBG_VALUE, but with the register number from this VarLoc.
595 MOs.push_back(Elt: MachineOperand::CreateReg(
596 Reg: EVKind == EntryValueLocKind::EntryValueKind ? Orig.getReg()
597 : Register(Loc.RegNo),
598 isDef: false));
599 break;
600 case MachineLocKind::SpillLocKind: {
601 // Spills are indirect DBG_VALUEs, with a base register and offset.
602 // Use the original DBG_VALUEs expression to build the spilt location
603 // on top of. FIXME: spill locations created before this pass runs
604 // are not recognized, and not handled here.
605 unsigned Base = Loc.SpillLocation.SpillBase;
606 auto *TRI = MF.getSubtarget().getRegisterInfo();
607 if (MI.isNonListDebugValue()) {
608 auto Deref = Indirect ? DIExpression::DerefAfter : 0;
609 DIExpr = TRI->prependOffsetExpression(
610 Expr: DIExpr, PrependFlags: DIExpression::ApplyOffset | Deref,
611 Offset: Loc.SpillLocation.SpillOffset);
612 Indirect = true;
613 } else {
614 SmallVector<uint64_t, 4> Ops;
615 TRI->getOffsetOpcodes(Offset: Loc.SpillLocation.SpillOffset, Ops);
616 Ops.push_back(Elt: dwarf::DW_OP_deref);
617 DIExpr = DIExpression::appendOpsToArg(Expr: DIExpr, Ops, ArgNo: I);
618 }
619 MOs.push_back(Elt: MachineOperand::CreateReg(Reg: Base, isDef: false));
620 break;
621 }
622 case MachineLocKind::ImmediateKind: {
623 MOs.push_back(Elt: Orig);
624 break;
625 }
626 case MachineLocKind::WasmLocKind:
627 case MachineLocKind::GlobalAddrKind: {
628 MOs.push_back(Elt: Orig);
629 break;
630 }
631 case MachineLocKind::InvalidKind:
632 llvm_unreachable("Tried to produce DBG_VALUE for invalid VarLoc");
633 }
634 }
635 return BuildMI(MF, DL: DbgLoc, MCID: IID, IsIndirect: Indirect, MOs, Variable: Var, Expr: DIExpr);
636 }
637
638 /// Is the Loc field a constant or constant object?
639 bool isConstant(MachineLocKind Kind) const {
640 return Kind == MachineLocKind::ImmediateKind ||
641 Kind == MachineLocKind::GlobalAddrKind;
642 }
643
644 /// Check if the Loc field is an entry backup location.
645 bool isEntryBackupLoc() const {
646 return EVKind == EntryValueLocKind::EntryValueBackupKind ||
647 EVKind == EntryValueLocKind::EntryValueCopyBackupKind;
648 }
649
650 /// If this variable is described by register \p Reg holding the entry
651 /// value, return true.
652 bool isEntryValueBackupReg(Register Reg) const {
653 return EVKind == EntryValueLocKind::EntryValueBackupKind && usesReg(Reg);
654 }
655
656 /// If this variable is described by register \p Reg holding a copy of the
657 /// entry value, return true.
658 bool isEntryValueCopyBackupReg(Register Reg) const {
659 return EVKind == EntryValueLocKind::EntryValueCopyBackupKind &&
660 usesReg(Reg);
661 }
662
663 /// If this variable is described in whole or part by \p Reg, return true.
664 bool usesReg(Register Reg) const {
665 MachineLoc RegML;
666 RegML.Kind = MachineLocKind::RegisterKind;
667 RegML.Value.RegNo = Reg;
668 return is_contained(Range: Locs, Element: RegML);
669 }
670
671 /// If this variable is described in whole or part by \p Reg, return true.
672 unsigned getRegIdx(Register Reg) const {
673 for (unsigned Idx = 0; Idx < Locs.size(); ++Idx)
674 if (Locs[Idx].Kind == MachineLocKind::RegisterKind &&
675 Register{static_cast<unsigned>(Locs[Idx].Value.RegNo)} == Reg)
676 return Idx;
677 llvm_unreachable("Could not find given Reg in Locs");
678 }
679
680 /// If this variable is described in whole or part by 1 or more registers,
681 /// add each of them to \p Regs and return true.
682 bool getDescribingRegs(SmallVectorImpl<uint32_t> &Regs) const {
683 bool AnyRegs = false;
684 for (const auto &Loc : Locs)
685 if (Loc.Kind == MachineLocKind::RegisterKind) {
686 Regs.push_back(Elt: Loc.Value.RegNo);
687 AnyRegs = true;
688 }
689 return AnyRegs;
690 }
691
692 bool containsSpillLocs() const {
693 return any_of(Range: Locs, P: [](VarLoc::MachineLoc ML) {
694 return ML.Kind == VarLoc::MachineLocKind::SpillLocKind;
695 });
696 }
697
698 /// If this variable is described in whole or part by \p SpillLocation,
699 /// return true.
700 bool usesSpillLoc(SpillLoc SpillLocation) const {
701 MachineLoc SpillML;
702 SpillML.Kind = MachineLocKind::SpillLocKind;
703 SpillML.Value.SpillLocation = SpillLocation;
704 return is_contained(Range: Locs, Element: SpillML);
705 }
706
707 /// If this variable is described in whole or part by \p SpillLocation,
708 /// return the index .
709 unsigned getSpillLocIdx(SpillLoc SpillLocation) const {
710 for (unsigned Idx = 0; Idx < Locs.size(); ++Idx)
711 if (Locs[Idx].Kind == MachineLocKind::SpillLocKind &&
712 Locs[Idx].Value.SpillLocation == SpillLocation)
713 return Idx;
714 llvm_unreachable("Could not find given SpillLoc in Locs");
715 }
716
717 bool containsWasmLocs() const {
718 return any_of(Range: Locs, P: [](VarLoc::MachineLoc ML) {
719 return ML.Kind == VarLoc::MachineLocKind::WasmLocKind;
720 });
721 }
722
723 /// If this variable is described in whole or part by \p WasmLocation,
724 /// return true.
725 bool usesWasmLoc(WasmLoc WasmLocation) const {
726 MachineLoc WasmML;
727 WasmML.Kind = MachineLocKind::WasmLocKind;
728 WasmML.Value.WasmLocation = WasmLocation;
729 return is_contained(Range: Locs, Element: WasmML);
730 }
731
732 /// Determine whether the lexical scope of this value's debug location
733 /// dominates MBB.
734 bool dominates(LexicalScopes &LS, MachineBasicBlock &MBB) const {
735 return LS.dominates(DL: MI.getDebugLoc().get(), MBB: &MBB);
736 }
737
738#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
739 // TRI and TII can be null.
740 void dump(const TargetRegisterInfo *TRI, const TargetInstrInfo *TII,
741 raw_ostream &Out = dbgs()) const {
742 Out << "VarLoc(";
743 for (const MachineLoc &MLoc : Locs) {
744 if (Locs.begin() != &MLoc)
745 Out << ", ";
746 switch (MLoc.Kind) {
747 case MachineLocKind::RegisterKind:
748 Out << printReg(MLoc.Value.RegNo, TRI);
749 break;
750 case MachineLocKind::SpillLocKind:
751 Out << printReg(MLoc.Value.SpillLocation.SpillBase, TRI);
752 Out << "[" << MLoc.Value.SpillLocation.SpillOffset.getFixed() << " + "
753 << MLoc.Value.SpillLocation.SpillOffset.getScalable()
754 << "x vscale"
755 << "]";
756 break;
757 case MachineLocKind::ImmediateKind:
758 Out << MLoc.Value.Immediate;
759 break;
760 case MachineLocKind::GlobalAddrKind:
761 Out << MLoc.Value.GlobalAddress.GV->getName();
762 if (MLoc.Value.GlobalAddress.Offset)
763 Out << '+' << MLoc.Value.GlobalAddress.Offset;
764 break;
765 case MachineLocKind::WasmLocKind: {
766 if (TII) {
767 auto Indices = TII->getSerializableTargetIndices();
768 auto Found =
769 find_if(Indices, [&](const std::pair<int, const char *> &I) {
770 return I.first == MLoc.Value.WasmLocation.Index;
771 });
772 assert(Found != Indices.end());
773 Out << Found->second;
774 if (MLoc.Value.WasmLocation.Offset > 0)
775 Out << " + " << MLoc.Value.WasmLocation.Offset;
776 } else {
777 Out << "WasmLoc";
778 }
779 break;
780 }
781 case MachineLocKind::InvalidKind:
782 llvm_unreachable("Invalid VarLoc in dump method");
783 }
784 }
785
786 Out << ", \"" << Var.getVariable()->getName() << "\", " << *Expr << ", ";
787 if (Var.getInlinedAt())
788 Out << "!" << Var.getInlinedAt()->getMetadataID() << ")\n";
789 else
790 Out << "(null))";
791
792 if (isEntryBackupLoc())
793 Out << " (backup loc)\n";
794 else
795 Out << "\n";
796 }
797#endif
798
799 bool operator==(const VarLoc &Other) const {
800 return std::tie(args: EVKind, args: Var, args: Expr, args: Locs) ==
801 std::tie(args: Other.EVKind, args: Other.Var, args: Other.Expr, args: Other.Locs);
802 }
803
804 /// This operator guarantees that VarLocs are sorted by Variable first.
805 bool operator<(const VarLoc &Other) const {
806 return std::tie(args: Var, args: EVKind, args: Locs, args: Expr) <
807 std::tie(args: Other.Var, args: Other.EVKind, args: Other.Locs, args: Other.Expr);
808 }
809 };
810
811#ifndef NDEBUG
812 using VarVec = SmallVector<VarLoc, 32>;
813#endif
814
815 /// VarLocMap is used for two things:
816 /// 1) Assigning LocIndices to a VarLoc. The LocIndices can be used to
817 /// virtually insert a VarLoc into a VarLocSet.
818 /// 2) Given a LocIndex, look up the unique associated VarLoc.
819 class VarLocMap {
820 /// Map a VarLoc to an index within the vector reserved for its location
821 /// within Loc2Vars.
822 std::map<VarLoc, LocIndices> Var2Indices;
823
824 /// Map a location to a vector which holds VarLocs which live in that
825 /// location.
826 SmallDenseMap<LocIndex::u32_location_t, std::vector<VarLoc>> Loc2Vars;
827
828 public:
829 /// Retrieve LocIndices for \p VL.
830 LocIndices insert(const VarLoc &VL) {
831 LocIndices &Indices = Var2Indices[VL];
832 // If Indices is not empty, VL is already in the map.
833 if (!Indices.empty())
834 return Indices;
835 SmallVector<LocIndex::u32_location_t, 4> Locations;
836 // LocIndices are determined by EVKind and MLs; each Register has a
837 // unique location, while all SpillLocs use a single bucket, and any EV
838 // VarLocs use only the Backup bucket or none at all (except the
839 // compulsory entry at the universal location index). LocIndices will
840 // always have an index at the universal location index as the last index.
841 if (VL.EVKind == VarLoc::EntryValueLocKind::NonEntryValueKind) {
842 VL.getDescribingRegs(Regs&: Locations);
843 assert(all_of(Locations,
844 [](auto RegNo) {
845 return (RegNo < LocIndex::kFirstInvalidRegLocation) ||
846 (LocIndex::kFirstVirtualRegLocation <= RegNo);
847 }) &&
848 "Physical or virtual register out of range?");
849 if (VL.containsSpillLocs())
850 Locations.push_back(Elt: LocIndex::kSpillLocation);
851 if (VL.containsWasmLocs())
852 Locations.push_back(Elt: LocIndex::kWasmLocation);
853 } else if (VL.EVKind != VarLoc::EntryValueLocKind::EntryValueKind) {
854 LocIndex::u32_location_t Loc = LocIndex::kEntryValueBackupLocation;
855 Locations.push_back(Elt: Loc);
856 }
857 Locations.push_back(Elt: LocIndex::kUniversalLocation);
858 for (LocIndex::u32_location_t Location : Locations) {
859 auto &Vars = Loc2Vars[Location];
860 Indices.push_back(
861 Elt: {Location, static_cast<LocIndex::u32_index_t>(Vars.size())});
862 Vars.push_back(x: VL);
863 }
864 return Indices;
865 }
866
867 LocIndices getAllIndices(const VarLoc &VL) const {
868 auto IndIt = Var2Indices.find(x: VL);
869 assert(IndIt != Var2Indices.end() && "VarLoc not tracked");
870 return IndIt->second;
871 }
872
873 /// Retrieve the unique VarLoc associated with \p ID.
874 const VarLoc &operator[](LocIndex ID) const {
875 auto LocIt = Loc2Vars.find(Val: ID.Location);
876 assert(LocIt != Loc2Vars.end() && "Location not tracked");
877 return LocIt->second[ID.Index];
878 }
879 };
880
881 using VarLocInMBB =
882 SmallDenseMap<const MachineBasicBlock *, std::unique_ptr<VarLocSet>>;
883 struct TransferDebugPair {
884 MachineInstr *TransferInst; ///< Instruction where this transfer occurs.
885 LocIndex LocationID; ///< Location number for the transfer dest.
886 };
887 using TransferMap = SmallVector<TransferDebugPair, 4>;
888 // Types for recording Entry Var Locations emitted by a single MachineInstr,
889 // as well as recording MachineInstr which last defined a register.
890 using InstToEntryLocMap = std::multimap<const MachineInstr *, LocIndex>;
891 using RegDefToInstMap = DenseMap<Register, MachineInstr *>;
892
893 // Types for recording sets of variable fragments that overlap. For a given
894 // local variable, we record all other fragments of that variable that could
895 // overlap it, to reduce search time.
896 using FragmentOfVar =
897 std::pair<const DILocalVariable *, DIExpression::FragmentInfo>;
898 using OverlapMap =
899 DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>;
900
901 // Helper while building OverlapMap, a map of all fragments seen for a given
902 // DILocalVariable.
903 using VarToFragments =
904 DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>;
905
906 /// Collects all VarLocs from \p CollectFrom. Each unique VarLoc is added
907 /// to \p Collected once, in order of insertion into \p VarLocIDs.
908 static void collectAllVarLocs(SmallVectorImpl<VarLoc> &Collected,
909 const VarLocSet &CollectFrom,
910 const VarLocMap &VarLocIDs);
911
912 /// Get the registers which are used by VarLocs of kind RegisterKind tracked
913 /// by \p CollectFrom.
914 void getUsedRegs(const VarLocSet &CollectFrom,
915 SmallVectorImpl<Register> &UsedRegs) const;
916
917 /// This holds the working set of currently open ranges. For fast
918 /// access, this is done both as a set of VarLocIDs, and a map of
919 /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all
920 /// previous open ranges for the same variable. In addition, we keep
921 /// two different maps (Vars/EntryValuesBackupVars), so erase/insert
922 /// methods act differently depending on whether a VarLoc is primary
923 /// location or backup one. In the case the VarLoc is backup location
924 /// we will erase/insert from the EntryValuesBackupVars map, otherwise
925 /// we perform the operation on the Vars.
926 class OpenRangesSet {
927 VarLocSet::Allocator &Alloc;
928 VarLocSet VarLocs;
929 // Map the DebugVariable to recent primary location ID.
930 SmallDenseMap<DebugVariable, LocIndices, 8> Vars;
931 // Map the DebugVariable to recent backup location ID.
932 SmallDenseMap<DebugVariable, LocIndices, 8> EntryValuesBackupVars;
933 OverlapMap &OverlappingFragments;
934
935 public:
936 OpenRangesSet(VarLocSet::Allocator &Alloc, OverlapMap &_OLapMap)
937 : Alloc(Alloc), VarLocs(Alloc), OverlappingFragments(_OLapMap) {}
938
939 const VarLocSet &getVarLocs() const { return VarLocs; }
940
941 // Fetches all VarLocs in \p VarLocIDs and inserts them into \p Collected.
942 // This method is needed to get every VarLoc once, as each VarLoc may have
943 // multiple indices in a VarLocMap (corresponding to each applicable
944 // location), but all VarLocs appear exactly once at the universal location
945 // index.
946 void getUniqueVarLocs(SmallVectorImpl<VarLoc> &Collected,
947 const VarLocMap &VarLocIDs) const {
948 collectAllVarLocs(Collected, CollectFrom: VarLocs, VarLocIDs);
949 }
950
951 /// Terminate all open ranges for VL.Var by removing it from the set.
952 void erase(const VarLoc &VL);
953
954 /// Terminate all open ranges listed as indices in \c KillSet with
955 /// \c Location by removing them from the set.
956 void erase(const VarLocsInRange &KillSet, const VarLocMap &VarLocIDs,
957 LocIndex::u32_location_t Location);
958
959 /// Insert a new range into the set.
960 void insert(LocIndices VarLocIDs, const VarLoc &VL);
961
962 /// Insert a set of ranges.
963 void insertFromLocSet(const VarLocSet &ToLoad, const VarLocMap &Map);
964
965 std::optional<LocIndices> getEntryValueBackup(DebugVariable Var);
966
967 /// Empty the set.
968 void clear() {
969 VarLocs.clear();
970 Vars.clear();
971 EntryValuesBackupVars.clear();
972 }
973
974 /// Return whether the set is empty or not.
975 bool empty() const {
976 assert(Vars.empty() == EntryValuesBackupVars.empty() &&
977 Vars.empty() == VarLocs.empty() &&
978 "open ranges are inconsistent");
979 return VarLocs.empty();
980 }
981
982 /// Get an empty range of VarLoc IDs.
983 auto getEmptyVarLocRange() const {
984 return iterator_range<VarLocSet::const_iterator>(getVarLocs().end(),
985 getVarLocs().end());
986 }
987
988 /// Get all set IDs for VarLocs with MLs of kind RegisterKind in \p Reg.
989 auto getRegisterVarLocs(Register Reg) const {
990 return LocIndex::indexRangeForLocation(Set: getVarLocs(), Location: Reg);
991 }
992
993 /// Get all set IDs for VarLocs with MLs of kind SpillLocKind.
994 auto getSpillVarLocs() const {
995 return LocIndex::indexRangeForLocation(Set: getVarLocs(),
996 Location: LocIndex::kSpillLocation);
997 }
998
999 /// Get all set IDs for VarLocs of EVKind EntryValueBackupKind or
1000 /// EntryValueCopyBackupKind.
1001 auto getEntryValueBackupVarLocs() const {
1002 return LocIndex::indexRangeForLocation(
1003 Set: getVarLocs(), Location: LocIndex::kEntryValueBackupLocation);
1004 }
1005
1006 /// Get all set IDs for VarLocs with MLs of kind WasmLocKind.
1007 auto getWasmVarLocs() const {
1008 return LocIndex::indexRangeForLocation(Set: getVarLocs(),
1009 Location: LocIndex::kWasmLocation);
1010 }
1011 };
1012
1013 /// Collect all VarLoc IDs from \p CollectFrom for VarLocs with MLs of kind
1014 /// RegisterKind which are located in any reg in \p Regs. The IDs for each
1015 /// VarLoc correspond to entries in the universal location bucket, which every
1016 /// VarLoc has exactly 1 entry for. Insert collected IDs into \p Collected.
1017 static void collectIDsForRegs(VarLocsInRange &Collected,
1018 ArrayRef<Register> Regs,
1019 const VarLocSet &CollectFrom,
1020 const VarLocMap &VarLocIDs);
1021
1022 VarLocSet &getVarLocsInMBB(const MachineBasicBlock *MBB, VarLocInMBB &Locs) {
1023 std::unique_ptr<VarLocSet> &VLS = Locs[MBB];
1024 if (!VLS)
1025 VLS = std::make_unique<VarLocSet>(args&: Alloc);
1026 return *VLS;
1027 }
1028
1029 const VarLocSet &getVarLocsInMBB(const MachineBasicBlock *MBB,
1030 const VarLocInMBB &Locs) const {
1031 auto It = Locs.find(Val: MBB);
1032 assert(It != Locs.end() && "MBB not in map");
1033 return *It->second;
1034 }
1035
1036 /// Tests whether this instruction is a spill to a stack location.
1037 bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF);
1038
1039 /// Decide if @MI is a spill instruction and return true if it is. We use 2
1040 /// criteria to make this decision:
1041 /// - Is this instruction a store to a spill slot?
1042 /// - Is there a register operand that is both used and killed?
1043 /// TODO: Store optimization can fold spills into other stores (including
1044 /// other spills). We do not handle this yet (more than one memory operand).
1045 bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF,
1046 Register &Reg);
1047
1048 /// Returns true if the given machine instruction is a debug value which we
1049 /// can emit entry values for.
1050 ///
1051 /// Currently, we generate debug entry values only for parameters that are
1052 /// unmodified throughout the function and located in a register.
1053 bool isEntryValueCandidate(const MachineInstr &MI,
1054 const DefinedRegsSet &Regs) const;
1055
1056 /// If a given instruction is identified as a spill, return the spill location
1057 /// and set \p Reg to the spilled register.
1058 std::optional<VarLoc::SpillLoc> isRestoreInstruction(const MachineInstr &MI,
1059 MachineFunction *MF,
1060 Register &Reg);
1061 /// Given a spill instruction, extract the register and offset used to
1062 /// address the spill location in a target independent way.
1063 VarLoc::SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI);
1064 void insertTransferDebugPair(MachineInstr &MI, OpenRangesSet &OpenRanges,
1065 TransferMap &Transfers, VarLocMap &VarLocIDs,
1066 LocIndex OldVarID, TransferKind Kind,
1067 const VarLoc::MachineLoc &OldLoc,
1068 Register NewReg = Register());
1069
1070 void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
1071 VarLocMap &VarLocIDs,
1072 InstToEntryLocMap &EntryValTransfers,
1073 RegDefToInstMap &RegSetInstrs);
1074 void transferSpillOrRestoreInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
1075 VarLocMap &VarLocIDs, TransferMap &Transfers);
1076 void cleanupEntryValueTransfers(const MachineInstr *MI,
1077 OpenRangesSet &OpenRanges,
1078 VarLocMap &VarLocIDs, const VarLoc &EntryVL,
1079 InstToEntryLocMap &EntryValTransfers);
1080 void removeEntryValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
1081 VarLocMap &VarLocIDs, const VarLoc &EntryVL,
1082 InstToEntryLocMap &EntryValTransfers,
1083 RegDefToInstMap &RegSetInstrs);
1084 void emitEntryValues(MachineInstr &MI, OpenRangesSet &OpenRanges,
1085 VarLocMap &VarLocIDs,
1086 InstToEntryLocMap &EntryValTransfers,
1087 VarLocsInRange &KillSet);
1088 void recordEntryValue(const MachineInstr &MI,
1089 const DefinedRegsSet &DefinedRegs,
1090 OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs);
1091 void transferRegisterCopy(MachineInstr &MI, OpenRangesSet &OpenRanges,
1092 VarLocMap &VarLocIDs, TransferMap &Transfers);
1093 void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
1094 VarLocMap &VarLocIDs,
1095 InstToEntryLocMap &EntryValTransfers,
1096 RegDefToInstMap &RegSetInstrs);
1097 void transferWasmDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
1098 VarLocMap &VarLocIDs);
1099 bool transferTerminator(MachineBasicBlock *MBB, OpenRangesSet &OpenRanges,
1100 VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
1101
1102 void process(MachineInstr &MI, OpenRangesSet &OpenRanges,
1103 VarLocMap &VarLocIDs, TransferMap &Transfers,
1104 InstToEntryLocMap &EntryValTransfers,
1105 RegDefToInstMap &RegSetInstrs);
1106
1107 void accumulateFragmentMap(MachineInstr &MI, VarToFragments &SeenFragments,
1108 OverlapMap &OLapMap);
1109
1110 bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
1111 const VarLocMap &VarLocIDs,
1112 SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
1113 SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks);
1114
1115 /// Create DBG_VALUE insts for inlocs that have been propagated but
1116 /// had their instruction creation deferred.
1117 void flushPendingLocs(VarLocInMBB &PendingInLocs, VarLocMap &VarLocIDs);
1118
1119 bool ExtendRanges(MachineFunction &MF, MachineDominatorTree *DomTree,
1120 bool ShouldEmitDebugEntryValues, unsigned InputBBLimit,
1121 unsigned InputDbgValLimit) override;
1122
1123public:
1124 /// Default construct and initialize the pass.
1125 VarLocBasedLDV();
1126
1127 ~VarLocBasedLDV() override;
1128
1129 /// Print to ostream with a message.
1130 void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
1131 const VarLocMap &VarLocIDs, const char *msg,
1132 raw_ostream &Out) const;
1133};
1134
1135} // end anonymous namespace
1136
1137//===----------------------------------------------------------------------===//
1138// Implementation
1139//===----------------------------------------------------------------------===//
1140
1141VarLocBasedLDV::VarLocBasedLDV() = default;
1142
1143VarLocBasedLDV::~VarLocBasedLDV() = default;
1144
1145/// Erase a variable from the set of open ranges, and additionally erase any
1146/// fragments that may overlap it. If the VarLoc is a backup location, erase
1147/// the variable from the EntryValuesBackupVars set, indicating we should stop
1148/// tracking its backup entry location. Otherwise, if the VarLoc is primary
1149/// location, erase the variable from the Vars set.
1150void VarLocBasedLDV::OpenRangesSet::erase(const VarLoc &VL) {
1151 // Erasure helper.
1152 auto DoErase = [&VL, this](DebugVariable VarToErase) {
1153 auto *EraseFrom = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars;
1154 auto It = EraseFrom->find(Val: VarToErase);
1155 if (It != EraseFrom->end()) {
1156 LocIndices IDs = It->second;
1157 for (LocIndex ID : IDs)
1158 VarLocs.reset(Index: ID.getAsRawInteger());
1159 EraseFrom->erase(I: It);
1160 }
1161 };
1162
1163 DebugVariable Var = VL.Var;
1164
1165 // Erase the variable/fragment that ends here.
1166 DoErase(Var);
1167
1168 // Extract the fragment. Interpret an empty fragment as one that covers all
1169 // possible bits.
1170 FragmentInfo ThisFragment = Var.getFragmentOrDefault();
1171
1172 // There may be fragments that overlap the designated fragment. Look them up
1173 // in the pre-computed overlap map, and erase them too.
1174 auto MapIt = OverlappingFragments.find(Val: {Var.getVariable(), ThisFragment});
1175 if (MapIt != OverlappingFragments.end()) {
1176 for (auto Fragment : MapIt->second) {
1177 VarLocBasedLDV::OptFragmentInfo FragmentHolder;
1178 if (!DebugVariable::isDefaultFragment(F: Fragment))
1179 FragmentHolder = VarLocBasedLDV::OptFragmentInfo(Fragment);
1180 DoErase({Var.getVariable(), FragmentHolder, Var.getInlinedAt()});
1181 }
1182 }
1183}
1184
1185void VarLocBasedLDV::OpenRangesSet::erase(const VarLocsInRange &KillSet,
1186 const VarLocMap &VarLocIDs,
1187 LocIndex::u32_location_t Location) {
1188 VarLocSet RemoveSet(Alloc);
1189 for (LocIndex::u32_index_t ID : KillSet) {
1190 const VarLoc &VL = VarLocIDs[LocIndex(Location, ID)];
1191 auto *EraseFrom = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars;
1192 EraseFrom->erase(Val: VL.Var);
1193 LocIndices VLI = VarLocIDs.getAllIndices(VL);
1194 for (LocIndex ID : VLI)
1195 RemoveSet.set(ID.getAsRawInteger());
1196 }
1197 VarLocs.intersectWithComplement(Other: RemoveSet);
1198}
1199
1200void VarLocBasedLDV::OpenRangesSet::insertFromLocSet(const VarLocSet &ToLoad,
1201 const VarLocMap &Map) {
1202 VarLocsInRange UniqueVarLocIDs;
1203 Register UniversalLoc = LocIndex::kUniversalLocation;
1204 collectIDsForRegs(Collected&: UniqueVarLocIDs, Regs: UniversalLoc, CollectFrom: ToLoad, VarLocIDs: Map);
1205 for (uint64_t ID : UniqueVarLocIDs) {
1206 LocIndex Idx = LocIndex::fromRawInteger(ID);
1207 const VarLoc &VarL = Map[Idx];
1208 const LocIndices Indices = Map.getAllIndices(VL: VarL);
1209 insert(VarLocIDs: Indices, VL: VarL);
1210 }
1211}
1212
1213void VarLocBasedLDV::OpenRangesSet::insert(LocIndices VarLocIDs,
1214 const VarLoc &VL) {
1215 auto *InsertInto = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars;
1216 for (LocIndex ID : VarLocIDs)
1217 VarLocs.set(ID.getAsRawInteger());
1218 InsertInto->insert(KV: {VL.Var, VarLocIDs});
1219}
1220
1221/// Return the Loc ID of an entry value backup location, if it exists for the
1222/// variable.
1223std::optional<LocIndices>
1224VarLocBasedLDV::OpenRangesSet::getEntryValueBackup(DebugVariable Var) {
1225 auto It = EntryValuesBackupVars.find(Val: Var);
1226 if (It != EntryValuesBackupVars.end())
1227 return It->second;
1228
1229 return std::nullopt;
1230}
1231
1232void VarLocBasedLDV::collectIDsForRegs(VarLocsInRange &Collected,
1233 ArrayRef<Register> Regs,
1234 const VarLocSet &CollectFrom,
1235 const VarLocMap &VarLocIDs) {
1236 assert(!Regs.empty() && "Nothing to collect");
1237 SmallVector<Register, 32> SortedRegs;
1238 append_range(C&: SortedRegs, R&: Regs);
1239 llvm::sort(C&: SortedRegs, Comp: [](Register LHS, Register RHS) { return LHS < RHS; });
1240 SortedRegs.erase(CS: llvm::unique(R&: SortedRegs), CE: SortedRegs.end());
1241 auto It = CollectFrom.find(Index: LocIndex::rawIndexForReg(Reg: SortedRegs.front()));
1242 auto End = CollectFrom.end();
1243 for (Register Reg : SortedRegs) {
1244 // The half-open interval [FirstIndexForReg, FirstInvalidIndex) contains
1245 // all possible VarLoc IDs for VarLocs with MLs of kind RegisterKind which
1246 // live in Reg.
1247 uint64_t FirstIndexForReg = LocIndex::rawIndexForReg(Reg);
1248 uint64_t FirstInvalidIndex = LocIndex::rawIndexForReg(Reg: Reg + 1);
1249 It.advanceToLowerBound(Index: FirstIndexForReg);
1250
1251 // Iterate through that half-open interval and collect all the set IDs.
1252 for (; It != End && *It < FirstInvalidIndex; ++It) {
1253 LocIndex ItIdx = LocIndex::fromRawInteger(ID: *It);
1254 const VarLoc &VL = VarLocIDs[ItIdx];
1255 LocIndices LI = VarLocIDs.getAllIndices(VL);
1256 // For now, the back index is always the universal location index.
1257 assert(LI.back().Location == LocIndex::kUniversalLocation &&
1258 "Unexpected order of LocIndices for VarLoc; was it inserted into "
1259 "the VarLocMap correctly?");
1260 Collected.insert(V: LI.back().Index);
1261 }
1262
1263 if (It == End)
1264 return;
1265 }
1266}
1267
1268void VarLocBasedLDV::getUsedRegs(const VarLocSet &CollectFrom,
1269 SmallVectorImpl<Register> &UsedRegs) const {
1270 // All register-based VarLocs are assigned indices greater than or equal to
1271 // FirstRegIndex.
1272 uint64_t FirstRegIndex =
1273 LocIndex::rawIndexForReg(Reg: LocIndex::kFirstRegLocation);
1274 uint64_t FirstInvalidIndex =
1275 LocIndex::rawIndexForReg(Reg: LocIndex::kFirstInvalidRegLocation);
1276 uint64_t FirstVirtualRegIndex =
1277 LocIndex::rawIndexForReg(Reg: LocIndex::kFirstVirtualRegLocation);
1278 auto doGetUsedRegs = [&](VarLocSet::const_iterator &It) {
1279 // We found a VarLoc ID for a VarLoc that lives in a register. Figure out
1280 // which register and add it to UsedRegs.
1281 uint32_t FoundReg = LocIndex::fromRawInteger(ID: *It).Location;
1282 assert((UsedRegs.empty() || FoundReg != UsedRegs.back()) &&
1283 "Duplicate used reg");
1284 UsedRegs.push_back(Elt: FoundReg);
1285
1286 // Skip to the next /set/ register. Note that this finds a lower bound, so
1287 // even if there aren't any VarLocs living in `FoundReg+1`, we're still
1288 // guaranteed to move on to the next register (or to end()).
1289 uint64_t NextRegIndex = LocIndex::rawIndexForReg(Reg: FoundReg + 1);
1290 It.advanceToLowerBound(Index: NextRegIndex);
1291 };
1292 for (auto It = CollectFrom.find(Index: FirstRegIndex),
1293 End = CollectFrom.find(Index: FirstInvalidIndex);
1294 It != End;) {
1295 doGetUsedRegs(It);
1296 }
1297 for (auto It = CollectFrom.find(Index: FirstVirtualRegIndex),
1298 End = CollectFrom.end();
1299 It != End;) {
1300 doGetUsedRegs(It);
1301 }
1302}
1303
1304//===----------------------------------------------------------------------===//
1305// Debug Range Extension Implementation
1306//===----------------------------------------------------------------------===//
1307
1308#ifndef NDEBUG
1309void VarLocBasedLDV::printVarLocInMBB(const MachineFunction &MF,
1310 const VarLocInMBB &V,
1311 const VarLocMap &VarLocIDs,
1312 const char *msg,
1313 raw_ostream &Out) const {
1314 Out << '\n' << msg << '\n';
1315 for (const MachineBasicBlock &BB : MF) {
1316 if (!V.count(&BB))
1317 continue;
1318 const VarLocSet &L = getVarLocsInMBB(&BB, V);
1319 if (L.empty())
1320 continue;
1321 SmallVector<VarLoc, 32> VarLocs;
1322 collectAllVarLocs(VarLocs, L, VarLocIDs);
1323 Out << "MBB: " << BB.getNumber() << ":\n";
1324 for (const VarLoc &VL : VarLocs) {
1325 Out << " Var: " << VL.Var.getVariable()->getName();
1326 Out << " MI: ";
1327 VL.dump(TRI, TII, Out);
1328 }
1329 }
1330 Out << "\n";
1331}
1332#endif
1333
1334VarLocBasedLDV::VarLoc::SpillLoc
1335VarLocBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) {
1336 assert(MI.hasOneMemOperand() &&
1337 "Spill instruction does not have exactly one memory operand?");
1338 auto MMOI = MI.memoperands_begin();
1339 const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue();
1340 assert(PVal->kind() == PseudoSourceValue::FixedStack &&
1341 "Inconsistent memory operand in spill instruction");
1342 int FI = cast<FixedStackPseudoSourceValue>(Val: PVal)->getFrameIndex();
1343 const MachineBasicBlock *MBB = MI.getParent();
1344 Register Reg;
1345 StackOffset Offset = TFI->getFrameIndexReference(MF: *MBB->getParent(), FI, FrameReg&: Reg);
1346 return {.SpillBase: Reg, .SpillOffset: Offset};
1347}
1348
1349/// Do cleanup of \p EntryValTransfers created by \p TRInst, by removing the
1350/// Transfer, which uses the to-be-deleted \p EntryVL.
1351void VarLocBasedLDV::cleanupEntryValueTransfers(
1352 const MachineInstr *TRInst, OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs,
1353 const VarLoc &EntryVL, InstToEntryLocMap &EntryValTransfers) {
1354 if (EntryValTransfers.empty() || TRInst == nullptr)
1355 return;
1356
1357 auto TransRange = EntryValTransfers.equal_range(x: TRInst);
1358 for (auto &TDPair : llvm::make_range(p: TransRange)) {
1359 const VarLoc &EmittedEV = VarLocIDs[TDPair.second];
1360 if (std::tie(args: EntryVL.Var, args: EntryVL.Locs[0].Value.RegNo, args: EntryVL.Expr) ==
1361 std::tie(args: EmittedEV.Var, args: EmittedEV.Locs[0].Value.RegNo,
1362 args: EmittedEV.Expr)) {
1363 OpenRanges.erase(VL: EmittedEV);
1364 EntryValTransfers.erase(x: TRInst);
1365 break;
1366 }
1367 }
1368}
1369
1370/// Try to salvage the debug entry value if we encounter a new debug value
1371/// describing the same parameter, otherwise stop tracking the value. Return
1372/// true if we should stop tracking the entry value and do the cleanup of
1373/// emitted Entry Value Transfers, otherwise return false.
1374void VarLocBasedLDV::removeEntryValue(const MachineInstr &MI,
1375 OpenRangesSet &OpenRanges,
1376 VarLocMap &VarLocIDs,
1377 const VarLoc &EntryVL,
1378 InstToEntryLocMap &EntryValTransfers,
1379 RegDefToInstMap &RegSetInstrs) {
1380 // Skip the DBG_VALUE which is the debug entry value itself.
1381 if (&MI == &EntryVL.MI)
1382 return;
1383
1384 // If the parameter's location is not register location, we can not track
1385 // the entry value any more. It doesn't have the TransferInst which defines
1386 // register, so no Entry Value Transfers have been emitted already.
1387 if (!MI.getDebugOperand(Index: 0).isReg())
1388 return;
1389
1390 // Try to get non-debug instruction responsible for the DBG_VALUE.
1391 Register Reg = MI.getDebugOperand(Index: 0).getReg();
1392 const MachineInstr *TransferInst =
1393 Reg.isValid() ? RegSetInstrs.lookup(Val: Reg) : nullptr;
1394
1395 // Case of the parameter's DBG_VALUE at the start of entry MBB.
1396 if (!TransferInst && !LastNonDbgMI && MI.getParent()->isEntryBlock())
1397 return;
1398
1399 // If the debug expression from the DBG_VALUE is not empty, we can assume the
1400 // parameter's value has changed indicating that we should stop tracking its
1401 // entry value as well.
1402 if (MI.getDebugExpression()->getNumElements() == 0 && TransferInst) {
1403 // If the DBG_VALUE comes from a copy instruction that copies the entry
1404 // value, it means the parameter's value has not changed and we should be
1405 // able to use its entry value.
1406 // TODO: Try to keep tracking of an entry value if we encounter a propagated
1407 // DBG_VALUE describing the copy of the entry value. (Propagated entry value
1408 // does not indicate the parameter modification.)
1409 auto DestSrc = TII->isCopyLikeInstr(MI: *TransferInst);
1410 if (DestSrc) {
1411 const MachineOperand *SrcRegOp, *DestRegOp;
1412 SrcRegOp = DestSrc->Source;
1413 DestRegOp = DestSrc->Destination;
1414 if (Reg == DestRegOp->getReg()) {
1415 for (uint64_t ID : OpenRanges.getEntryValueBackupVarLocs()) {
1416 const VarLoc &VL = VarLocIDs[LocIndex::fromRawInteger(ID)];
1417 if (VL.isEntryValueCopyBackupReg(Reg) &&
1418 // Entry Values should not be variadic.
1419 VL.MI.getDebugOperand(Index: 0).getReg() == SrcRegOp->getReg())
1420 return;
1421 }
1422 }
1423 }
1424 }
1425
1426 LLVM_DEBUG(dbgs() << "Deleting a DBG entry value because of: ";
1427 MI.print(dbgs(), /*IsStandalone*/ false,
1428 /*SkipOpers*/ false, /*SkipDebugLoc*/ false,
1429 /*AddNewLine*/ true, TII));
1430 cleanupEntryValueTransfers(TRInst: TransferInst, OpenRanges, VarLocIDs, EntryVL,
1431 EntryValTransfers);
1432 OpenRanges.erase(VL: EntryVL);
1433}
1434
1435/// End all previous ranges related to @MI and start a new range from @MI
1436/// if it is a DBG_VALUE instr.
1437void VarLocBasedLDV::transferDebugValue(const MachineInstr &MI,
1438 OpenRangesSet &OpenRanges,
1439 VarLocMap &VarLocIDs,
1440 InstToEntryLocMap &EntryValTransfers,
1441 RegDefToInstMap &RegSetInstrs) {
1442 if (!MI.isDebugValue())
1443 return;
1444 const DILocalVariable *Var = MI.getDebugVariable();
1445 const DIExpression *Expr = MI.getDebugExpression();
1446 const DILocation *DebugLoc = MI.getDebugLoc();
1447 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
1448 assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
1449 "Expected inlined-at fields to agree");
1450
1451 DebugVariable V(Var, Expr, InlinedAt);
1452
1453 // Check if this DBG_VALUE indicates a parameter's value changing.
1454 // If that is the case, we should stop tracking its entry value.
1455 auto EntryValBackupID = OpenRanges.getEntryValueBackup(Var: V);
1456 if (Var->isParameter() && EntryValBackupID) {
1457 const VarLoc &EntryVL = VarLocIDs[EntryValBackupID->back()];
1458 removeEntryValue(MI, OpenRanges, VarLocIDs, EntryVL, EntryValTransfers,
1459 RegSetInstrs);
1460 }
1461
1462 if (all_of(Range: MI.debug_operands(), P: [](const MachineOperand &MO) {
1463 return (MO.isReg() && MO.getReg()) || MO.isImm() || MO.isFPImm() ||
1464 MO.isCImm() || MO.isTargetIndex() || MO.isGlobal();
1465 })) {
1466 // Use the normal VarLoc constructor for every operand kind MachineLoc can
1467 // hold directly: registers, immediates, WebAssembly locals and globals.
1468 VarLoc VL(MI);
1469 // End all previous ranges of VL.Var.
1470 OpenRanges.erase(VL);
1471
1472 LocIndices IDs = VarLocIDs.insert(VL);
1473 // Add the VarLoc to OpenRanges from this DBG_VALUE.
1474 OpenRanges.insert(VarLocIDs: IDs, VL);
1475 } else if (MI.memoperands().size() > 0) {
1476 llvm_unreachable("DBG_VALUE with mem operand encountered after regalloc?");
1477 } else {
1478 // This must be an undefined location. If it has an open range, erase it.
1479 assert(MI.isUndefDebugValue() &&
1480 "Unexpected non-undef DBG_VALUE encountered");
1481 VarLoc VL(MI);
1482 OpenRanges.erase(VL);
1483 }
1484}
1485
1486// This should be removed later, doesn't fit the new design.
1487void VarLocBasedLDV::collectAllVarLocs(SmallVectorImpl<VarLoc> &Collected,
1488 const VarLocSet &CollectFrom,
1489 const VarLocMap &VarLocIDs) {
1490 // The half-open interval [FirstIndexForReg, FirstInvalidIndex) contains all
1491 // possible VarLoc IDs for VarLocs with MLs of kind RegisterKind which live
1492 // in Reg.
1493 uint64_t FirstIndex = LocIndex::rawIndexForReg(Reg: LocIndex::kUniversalLocation);
1494 uint64_t FirstInvalidIndex =
1495 LocIndex::rawIndexForReg(Reg: LocIndex::kUniversalLocation + 1);
1496 // Iterate through that half-open interval and collect all the set IDs.
1497 for (auto It = CollectFrom.find(Index: FirstIndex), End = CollectFrom.end();
1498 It != End && *It < FirstInvalidIndex; ++It) {
1499 LocIndex RegIdx = LocIndex::fromRawInteger(ID: *It);
1500 Collected.push_back(Elt: VarLocIDs[RegIdx]);
1501 }
1502}
1503
1504/// Turn the entry value backup locations into primary locations.
1505void VarLocBasedLDV::emitEntryValues(MachineInstr &MI,
1506 OpenRangesSet &OpenRanges,
1507 VarLocMap &VarLocIDs,
1508 InstToEntryLocMap &EntryValTransfers,
1509 VarLocsInRange &KillSet) {
1510 // Do not insert entry value locations after a terminator.
1511 if (MI.isTerminator())
1512 return;
1513
1514 for (uint32_t ID : KillSet) {
1515 // The KillSet IDs are indices for the universal location bucket.
1516 LocIndex Idx = LocIndex(LocIndex::kUniversalLocation, ID);
1517 const VarLoc &VL = VarLocIDs[Idx];
1518 if (!VL.Var.getVariable()->isParameter())
1519 continue;
1520
1521 auto DebugVar = VL.Var;
1522 std::optional<LocIndices> EntryValBackupIDs =
1523 OpenRanges.getEntryValueBackup(Var: DebugVar);
1524
1525 // If the parameter has the entry value backup, it means we should
1526 // be able to use its entry value.
1527 if (!EntryValBackupIDs)
1528 continue;
1529
1530 const VarLoc &EntryVL = VarLocIDs[EntryValBackupIDs->back()];
1531 VarLoc EntryLoc = VarLoc::CreateEntryLoc(MI: EntryVL.MI, EntryExpr: EntryVL.Expr,
1532 Reg: EntryVL.Locs[0].Value.RegNo);
1533 LocIndices EntryValueIDs = VarLocIDs.insert(VL: EntryLoc);
1534 assert(EntryValueIDs.size() == 1 &&
1535 "EntryValue loc should not be variadic");
1536 EntryValTransfers.insert(x: {&MI, EntryValueIDs.back()});
1537 OpenRanges.insert(VarLocIDs: EntryValueIDs, VL: EntryLoc);
1538 }
1539}
1540
1541/// Create new TransferDebugPair and insert it in \p Transfers. The VarLoc
1542/// with \p OldVarID should be deleted form \p OpenRanges and replaced with
1543/// new VarLoc. If \p NewReg is different than default zero value then the
1544/// new location will be register location created by the copy like instruction,
1545/// otherwise it is variable's location on the stack.
1546void VarLocBasedLDV::insertTransferDebugPair(
1547 MachineInstr &MI, OpenRangesSet &OpenRanges, TransferMap &Transfers,
1548 VarLocMap &VarLocIDs, LocIndex OldVarID, TransferKind Kind,
1549 const VarLoc::MachineLoc &OldLoc, Register NewReg) {
1550 const VarLoc &OldVarLoc = VarLocIDs[OldVarID];
1551
1552 auto ProcessVarLoc = [&MI, &OpenRanges, &Transfers, &VarLocIDs](VarLoc &VL) {
1553 LocIndices LocIds = VarLocIDs.insert(VL);
1554
1555 // Close this variable's previous location range.
1556 OpenRanges.erase(VL);
1557
1558 // Record the new location as an open range, and a postponed transfer
1559 // inserting a DBG_VALUE for this location.
1560 OpenRanges.insert(VarLocIDs: LocIds, VL);
1561 assert(!MI.isTerminator() && "Cannot insert DBG_VALUE after terminator");
1562 TransferDebugPair MIP = {.TransferInst: &MI, .LocationID: LocIds.back()};
1563 Transfers.push_back(Elt: MIP);
1564 };
1565
1566 // End all previous ranges of VL.Var.
1567 OpenRanges.erase(VL: VarLocIDs[OldVarID]);
1568 switch (Kind) {
1569 case TransferKind::TransferCopy: {
1570 assert(NewReg &&
1571 "No register supplied when handling a copy of a debug value");
1572 // Create a DBG_VALUE instruction to describe the Var in its new
1573 // register location.
1574 VarLoc VL = VarLoc::CreateCopyLoc(OldVL: OldVarLoc, OldML: OldLoc, NewReg);
1575 ProcessVarLoc(VL);
1576 LLVM_DEBUG({
1577 dbgs() << "Creating VarLoc for register copy:";
1578 VL.dump(TRI, TII);
1579 });
1580 return;
1581 }
1582 case TransferKind::TransferSpill: {
1583 // Create a DBG_VALUE instruction to describe the Var in its spilled
1584 // location.
1585 VarLoc::SpillLoc SpillLocation = extractSpillBaseRegAndOffset(MI);
1586 VarLoc VL = VarLoc::CreateSpillLoc(
1587 OldVL: OldVarLoc, OldML: OldLoc, SpillBase: SpillLocation.SpillBase, SpillOffset: SpillLocation.SpillOffset);
1588 ProcessVarLoc(VL);
1589 LLVM_DEBUG({
1590 dbgs() << "Creating VarLoc for spill:";
1591 VL.dump(TRI, TII);
1592 });
1593 return;
1594 }
1595 case TransferKind::TransferRestore: {
1596 assert(NewReg &&
1597 "No register supplied when handling a restore of a debug value");
1598 // DebugInstr refers to the pre-spill location, therefore we can reuse
1599 // its expression.
1600 VarLoc VL = VarLoc::CreateCopyLoc(OldVL: OldVarLoc, OldML: OldLoc, NewReg);
1601 ProcessVarLoc(VL);
1602 LLVM_DEBUG({
1603 dbgs() << "Creating VarLoc for restore:";
1604 VL.dump(TRI, TII);
1605 });
1606 return;
1607 }
1608 }
1609 llvm_unreachable("Invalid transfer kind");
1610}
1611
1612/// A definition of a register may mark the end of a range.
1613void VarLocBasedLDV::transferRegisterDef(MachineInstr &MI,
1614 OpenRangesSet &OpenRanges,
1615 VarLocMap &VarLocIDs,
1616 InstToEntryLocMap &EntryValTransfers,
1617 RegDefToInstMap &RegSetInstrs) {
1618
1619 // Meta Instructions do not affect the debug liveness of any register they
1620 // define.
1621 if (MI.isMetaInstruction())
1622 return;
1623
1624 MachineFunction *MF = MI.getMF();
1625 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
1626 Register SP = TLI->getStackPointerRegisterToSaveRestore();
1627
1628 // Find the regs killed by MI, and find regmasks of preserved regs.
1629 SmallVector<Register, 32> DeadRegs;
1630 SmallVector<const uint32_t *, 4> RegMasks;
1631 for (const MachineOperand &MO : MI.operands()) {
1632 // Determine whether the operand is a register def.
1633 if (MO.isReg() && MO.isDef() && MO.getReg() && MO.getReg().isPhysical() &&
1634 !(MI.isCall() && MO.getReg() == SP)) {
1635 // Remove ranges of all aliased registers.
1636 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
1637 DeadRegs.push_back(Elt: (*RAI).id());
1638 RegSetInstrs.erase(Val: MO.getReg());
1639 RegSetInstrs.insert(KV: {MO.getReg(), &MI});
1640 } else if (MO.isRegMask()) {
1641 RegMasks.push_back(Elt: MO.getRegMask());
1642 }
1643 }
1644
1645 // Erase VarLocs which reside in one of the dead registers. For performance
1646 // reasons, it's critical to not iterate over the full set of open VarLocs.
1647 // Iterate over the set of dying/used regs instead.
1648 if (!RegMasks.empty()) {
1649 SmallVector<Register, 32> UsedRegs;
1650 getUsedRegs(CollectFrom: OpenRanges.getVarLocs(), UsedRegs);
1651 for (Register Reg : UsedRegs) {
1652 // Remove ranges of all clobbered registers. Register masks don't usually
1653 // list SP as preserved. Assume that call instructions never clobber SP,
1654 // because some backends (e.g., AArch64) never list SP in the regmask.
1655 // While the debug info may be off for an instruction or two around
1656 // callee-cleanup calls, transferring the DEBUG_VALUE across the call is
1657 // still a better user experience.
1658 if (Reg == SP)
1659 continue;
1660 bool AnyRegMaskKillsReg =
1661 any_of(Range&: RegMasks, P: [Reg](const uint32_t *RegMask) {
1662 return MachineOperand::clobbersPhysReg(RegMask, PhysReg: Reg);
1663 });
1664 if (AnyRegMaskKillsReg)
1665 DeadRegs.push_back(Elt: Reg);
1666 if (AnyRegMaskKillsReg) {
1667 RegSetInstrs.erase(Val: Reg);
1668 RegSetInstrs.insert(KV: {Reg, &MI});
1669 }
1670 }
1671 }
1672
1673 if (DeadRegs.empty())
1674 return;
1675
1676 VarLocsInRange KillSet;
1677 collectIDsForRegs(Collected&: KillSet, Regs: DeadRegs, CollectFrom: OpenRanges.getVarLocs(), VarLocIDs);
1678 OpenRanges.erase(KillSet, VarLocIDs, Location: LocIndex::kUniversalLocation);
1679
1680 if (ShouldEmitDebugEntryValues)
1681 emitEntryValues(MI, OpenRanges, VarLocIDs, EntryValTransfers, KillSet);
1682}
1683
1684void VarLocBasedLDV::transferWasmDef(MachineInstr &MI,
1685 OpenRangesSet &OpenRanges,
1686 VarLocMap &VarLocIDs) {
1687 // If this is not a Wasm local.set or local.tee, which sets local values,
1688 // return.
1689 int Index;
1690 int64_t Offset;
1691 if (!TII->isExplicitTargetIndexDef(MI, Index, Offset))
1692 return;
1693
1694 // Find the target indices killed by MI, and delete those variable locations
1695 // from the open range.
1696 VarLocsInRange KillSet;
1697 VarLoc::WasmLoc Loc{.Index: Index, .Offset: Offset};
1698 for (uint64_t ID : OpenRanges.getWasmVarLocs()) {
1699 LocIndex Idx = LocIndex::fromRawInteger(ID);
1700 const VarLoc &VL = VarLocIDs[Idx];
1701 assert(VL.containsWasmLocs() && "Broken VarLocSet?");
1702 if (VL.usesWasmLoc(WasmLocation: Loc))
1703 KillSet.insert(V: ID);
1704 }
1705 OpenRanges.erase(KillSet, VarLocIDs, Location: LocIndex::kWasmLocation);
1706}
1707
1708bool VarLocBasedLDV::isSpillInstruction(const MachineInstr &MI,
1709 MachineFunction *MF) {
1710 // TODO: Handle multiple stores folded into one.
1711 if (!MI.hasOneMemOperand())
1712 return false;
1713
1714 if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII))
1715 return false; // This is not a spill instruction, since no valid size was
1716 // returned from either function.
1717
1718 return true;
1719}
1720
1721bool VarLocBasedLDV::isLocationSpill(const MachineInstr &MI,
1722 MachineFunction *MF, Register &Reg) {
1723 if (!isSpillInstruction(MI, MF))
1724 return false;
1725
1726 auto isKilledReg = [&](const MachineOperand MO, Register &Reg) {
1727 if (!MO.isReg() || !MO.isUse()) {
1728 Reg = 0;
1729 return false;
1730 }
1731 Reg = MO.getReg();
1732 return MO.isKill();
1733 };
1734
1735 for (const MachineOperand &MO : MI.operands()) {
1736 // In a spill instruction generated by the InlineSpiller the spilled
1737 // register has its kill flag set.
1738 if (isKilledReg(MO, Reg))
1739 return true;
1740 if (Reg != 0) {
1741 // Check whether next instruction kills the spilled register.
1742 // FIXME: Current solution does not cover search for killed register in
1743 // bundles and instructions further down the chain.
1744 auto NextI = std::next(x: MI.getIterator());
1745 // Skip next instruction that points to basic block end iterator.
1746 if (MI.getParent()->end() == NextI)
1747 continue;
1748 Register RegNext;
1749 for (const MachineOperand &MONext : NextI->operands()) {
1750 // Return true if we came across the register from the
1751 // previous spill instruction that is killed in NextI.
1752 if (isKilledReg(MONext, RegNext) && RegNext == Reg)
1753 return true;
1754 }
1755 }
1756 }
1757 // Return false if we didn't find spilled register.
1758 return false;
1759}
1760
1761std::optional<VarLocBasedLDV::VarLoc::SpillLoc>
1762VarLocBasedLDV::isRestoreInstruction(const MachineInstr &MI,
1763 MachineFunction *MF, Register &Reg) {
1764 if (!MI.hasOneMemOperand())
1765 return std::nullopt;
1766
1767 // FIXME: Handle folded restore instructions with more than one memory
1768 // operand.
1769 if (MI.getRestoreSize(TII)) {
1770 Reg = MI.getOperand(i: 0).getReg();
1771 return extractSpillBaseRegAndOffset(MI);
1772 }
1773 return std::nullopt;
1774}
1775
1776/// A spilled register may indicate that we have to end the current range of
1777/// a variable and create a new one for the spill location.
1778/// A restored register may indicate the reverse situation.
1779/// We don't want to insert any instructions in process(), so we just create
1780/// the DBG_VALUE without inserting it and keep track of it in \p Transfers.
1781/// It will be inserted into the BB when we're done iterating over the
1782/// instructions.
1783void VarLocBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI,
1784 OpenRangesSet &OpenRanges,
1785 VarLocMap &VarLocIDs,
1786 TransferMap &Transfers) {
1787 MachineFunction *MF = MI.getMF();
1788 TransferKind TKind;
1789 Register Reg;
1790 std::optional<VarLoc::SpillLoc> Loc;
1791
1792 LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump(););
1793
1794 // First, if there are any DBG_VALUEs pointing at a spill slot that is
1795 // written to, then close the variable location. The value in memory
1796 // will have changed.
1797 VarLocsInRange KillSet;
1798 if (isSpillInstruction(MI, MF)) {
1799 Loc = extractSpillBaseRegAndOffset(MI);
1800 for (uint64_t ID : OpenRanges.getSpillVarLocs()) {
1801 LocIndex Idx = LocIndex::fromRawInteger(ID);
1802 const VarLoc &VL = VarLocIDs[Idx];
1803 assert(VL.containsSpillLocs() && "Broken VarLocSet?");
1804 if (VL.usesSpillLoc(SpillLocation: *Loc)) {
1805 // This location is overwritten by the current instruction -- terminate
1806 // the open range, and insert an explicit DBG_VALUE $noreg.
1807 //
1808 // Doing this at a later stage would require re-interpreting all
1809 // DBG_VALUes and DIExpressions to identify whether they point at
1810 // memory, and then analysing all memory writes to see if they
1811 // overwrite that memory, which is expensive.
1812 //
1813 // At this stage, we already know which DBG_VALUEs are for spills and
1814 // where they are located; it's best to fix handle overwrites now.
1815 KillSet.insert(V: ID);
1816 unsigned SpillLocIdx = VL.getSpillLocIdx(SpillLocation: *Loc);
1817 VarLoc::MachineLoc OldLoc = VL.Locs[SpillLocIdx];
1818 VarLoc UndefVL = VarLoc::CreateCopyLoc(OldVL: VL, OldML: OldLoc, NewReg: 0);
1819 LocIndices UndefLocIDs = VarLocIDs.insert(VL: UndefVL);
1820 Transfers.push_back(Elt: {.TransferInst: &MI, .LocationID: UndefLocIDs.back()});
1821 }
1822 }
1823 OpenRanges.erase(KillSet, VarLocIDs, Location: LocIndex::kSpillLocation);
1824 }
1825
1826 // Try to recognise spill and restore instructions that may create a new
1827 // variable location.
1828 if (isLocationSpill(MI, MF, Reg)) {
1829 TKind = TransferKind::TransferSpill;
1830 LLVM_DEBUG(dbgs() << "Recognized as spill: "; MI.dump(););
1831 LLVM_DEBUG(dbgs() << "Register: " << Reg.id() << " " << printReg(Reg, TRI)
1832 << "\n");
1833 } else {
1834 if (!(Loc = isRestoreInstruction(MI, MF, Reg)))
1835 return;
1836 TKind = TransferKind::TransferRestore;
1837 LLVM_DEBUG(dbgs() << "Recognized as restore: "; MI.dump(););
1838 LLVM_DEBUG(dbgs() << "Register: " << Reg.id() << " " << printReg(Reg, TRI)
1839 << "\n");
1840 }
1841 // Check if the register or spill location is the location of a debug value.
1842 auto TransferCandidates = OpenRanges.getEmptyVarLocRange();
1843 if (TKind == TransferKind::TransferSpill)
1844 TransferCandidates = OpenRanges.getRegisterVarLocs(Reg);
1845 else if (TKind == TransferKind::TransferRestore)
1846 TransferCandidates = OpenRanges.getSpillVarLocs();
1847 for (uint64_t ID : TransferCandidates) {
1848 LocIndex Idx = LocIndex::fromRawInteger(ID);
1849 const VarLoc &VL = VarLocIDs[Idx];
1850 unsigned LocIdx;
1851 if (TKind == TransferKind::TransferSpill) {
1852 assert(VL.usesReg(Reg) && "Broken VarLocSet?");
1853 LLVM_DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '('
1854 << VL.Var.getVariable()->getName() << ")\n");
1855 LocIdx = VL.getRegIdx(Reg);
1856 } else {
1857 assert(TKind == TransferKind::TransferRestore && VL.containsSpillLocs() &&
1858 "Broken VarLocSet?");
1859 if (!VL.usesSpillLoc(SpillLocation: *Loc))
1860 // The spill location is not the location of a debug value.
1861 continue;
1862 LLVM_DEBUG(dbgs() << "Restoring Register " << printReg(Reg, TRI) << '('
1863 << VL.Var.getVariable()->getName() << ")\n");
1864 LocIdx = VL.getSpillLocIdx(SpillLocation: *Loc);
1865 }
1866 VarLoc::MachineLoc MLoc = VL.Locs[LocIdx];
1867 insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, OldVarID: Idx, Kind: TKind,
1868 OldLoc: MLoc, NewReg: Reg);
1869 // FIXME: A comment should explain why it's correct to return early here,
1870 // if that is in fact correct.
1871 return;
1872 }
1873}
1874
1875/// If \p MI is a register copy instruction, that copies a previously tracked
1876/// value from one register to another register that is callee saved, we
1877/// create new DBG_VALUE instruction described with copy destination register.
1878void VarLocBasedLDV::transferRegisterCopy(MachineInstr &MI,
1879 OpenRangesSet &OpenRanges,
1880 VarLocMap &VarLocIDs,
1881 TransferMap &Transfers) {
1882 auto DestSrc = TII->isCopyLikeInstr(MI);
1883 if (!DestSrc)
1884 return;
1885
1886 const MachineOperand *DestRegOp = DestSrc->Destination;
1887 const MachineOperand *SrcRegOp = DestSrc->Source;
1888
1889 if (!DestRegOp->isDef())
1890 return;
1891
1892 auto isCalleeSavedReg = [&](Register Reg) {
1893 for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
1894 if (CalleeSavedRegs.test(Idx: (*RAI).id()))
1895 return true;
1896 return false;
1897 };
1898
1899 Register SrcReg = SrcRegOp->getReg();
1900 Register DestReg = DestRegOp->getReg();
1901
1902 // We want to recognize instructions where destination register is callee
1903 // saved register. If register that could be clobbered by the call is
1904 // included, there would be a great chance that it is going to be clobbered
1905 // soon. It is more likely that previous register location, which is callee
1906 // saved, is going to stay unclobbered longer, even if it is killed.
1907 if (!isCalleeSavedReg(DestReg))
1908 return;
1909
1910 // Remember an entry value movement. If we encounter a new debug value of
1911 // a parameter describing only a moving of the value around, rather then
1912 // modifying it, we are still able to use the entry value if needed.
1913 if (isRegOtherThanSPAndFP(Op: *DestRegOp, MI, TRI)) {
1914 for (uint64_t ID : OpenRanges.getEntryValueBackupVarLocs()) {
1915 LocIndex Idx = LocIndex::fromRawInteger(ID);
1916 const VarLoc &VL = VarLocIDs[Idx];
1917 if (VL.isEntryValueBackupReg(Reg: SrcReg)) {
1918 LLVM_DEBUG(dbgs() << "Copy of the entry value: "; MI.dump(););
1919 VarLoc EntryValLocCopyBackup =
1920 VarLoc::CreateEntryCopyBackupLoc(MI: VL.MI, EntryExpr: VL.Expr, NewReg: DestReg);
1921 // Stop tracking the original entry value.
1922 OpenRanges.erase(VL);
1923
1924 // Start tracking the entry value copy.
1925 LocIndices EntryValCopyLocIDs = VarLocIDs.insert(VL: EntryValLocCopyBackup);
1926 OpenRanges.insert(VarLocIDs: EntryValCopyLocIDs, VL: EntryValLocCopyBackup);
1927 break;
1928 }
1929 }
1930 }
1931
1932 if (!SrcRegOp->isKill())
1933 return;
1934
1935 for (uint64_t ID : OpenRanges.getRegisterVarLocs(Reg: SrcReg)) {
1936 LocIndex Idx = LocIndex::fromRawInteger(ID);
1937 assert(VarLocIDs[Idx].usesReg(SrcReg) && "Broken VarLocSet?");
1938 VarLoc::MachineLocValue Loc;
1939 Loc.RegNo = SrcReg;
1940 VarLoc::MachineLoc MLoc{.Kind: VarLoc::MachineLocKind::RegisterKind, .Value: Loc};
1941 insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, OldVarID: Idx,
1942 Kind: TransferKind::TransferCopy, OldLoc: MLoc, NewReg: DestReg);
1943 // FIXME: A comment should explain why it's correct to return early here,
1944 // if that is in fact correct.
1945 return;
1946 }
1947}
1948
1949/// Terminate all open ranges at the end of the current basic block.
1950bool VarLocBasedLDV::transferTerminator(MachineBasicBlock *CurMBB,
1951 OpenRangesSet &OpenRanges,
1952 VarLocInMBB &OutLocs,
1953 const VarLocMap &VarLocIDs) {
1954 bool Changed = false;
1955 LLVM_DEBUG({
1956 VarVec VarLocs;
1957 OpenRanges.getUniqueVarLocs(VarLocs, VarLocIDs);
1958 for (VarLoc &VL : VarLocs) {
1959 // Copy OpenRanges to OutLocs, if not already present.
1960 dbgs() << "Add to OutLocs in MBB #" << CurMBB->getNumber() << ": ";
1961 VL.dump(TRI, TII);
1962 }
1963 });
1964 VarLocSet &VLS = getVarLocsInMBB(MBB: CurMBB, Locs&: OutLocs);
1965 Changed = VLS != OpenRanges.getVarLocs();
1966 // New OutLocs set may be different due to spill, restore or register
1967 // copy instruction processing.
1968 if (Changed)
1969 VLS = OpenRanges.getVarLocs();
1970 OpenRanges.clear();
1971 return Changed;
1972}
1973
1974/// Accumulate a mapping between each DILocalVariable fragment and other
1975/// fragments of that DILocalVariable which overlap. This reduces work during
1976/// the data-flow stage from "Find any overlapping fragments" to "Check if the
1977/// known-to-overlap fragments are present".
1978/// \param MI A previously unprocessed DEBUG_VALUE instruction to analyze for
1979/// fragment usage.
1980/// \param SeenFragments Map from DILocalVariable to all fragments of that
1981/// Variable which are known to exist.
1982/// \param OverlappingFragments The overlap map being constructed, from one
1983/// Var/Fragment pair to a vector of fragments known to overlap.
1984void VarLocBasedLDV::accumulateFragmentMap(MachineInstr &MI,
1985 VarToFragments &SeenFragments,
1986 OverlapMap &OverlappingFragments) {
1987 DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(),
1988 MI.getDebugLoc()->getInlinedAt());
1989 FragmentInfo ThisFragment = MIVar.getFragmentOrDefault();
1990
1991 // If this is the first sighting of this variable, then we are guaranteed
1992 // there are currently no overlapping fragments either. Initialize the set
1993 // of seen fragments, record no overlaps for the current one, and return.
1994 auto [SeenIt, Inserted] = SeenFragments.try_emplace(Key: MIVar.getVariable());
1995 if (Inserted) {
1996 SeenIt->second.insert(V: ThisFragment);
1997
1998 OverlappingFragments.insert(KV: {{MIVar.getVariable(), ThisFragment}, {}});
1999 return;
2000 }
2001
2002 // If this particular Variable/Fragment pair already exists in the overlap
2003 // map, it has already been accounted for.
2004 auto IsInOLapMap =
2005 OverlappingFragments.insert(KV: {{MIVar.getVariable(), ThisFragment}, {}});
2006 if (!IsInOLapMap.second)
2007 return;
2008
2009 auto &ThisFragmentsOverlaps = IsInOLapMap.first->second;
2010 auto &AllSeenFragments = SeenIt->second;
2011
2012 // Otherwise, examine all other seen fragments for this variable, with "this"
2013 // fragment being a previously unseen fragment. Record any pair of
2014 // overlapping fragments.
2015 for (const auto &ASeenFragment : AllSeenFragments) {
2016 // Does this previously seen fragment overlap?
2017 if (DIExpression::fragmentsOverlap(A: ThisFragment, B: ASeenFragment)) {
2018 // Yes: Mark the current fragment as being overlapped.
2019 ThisFragmentsOverlaps.push_back(Elt: ASeenFragment);
2020 // Mark the previously seen fragment as being overlapped by the current
2021 // one.
2022 auto ASeenFragmentsOverlaps =
2023 OverlappingFragments.find(Val: {MIVar.getVariable(), ASeenFragment});
2024 assert(ASeenFragmentsOverlaps != OverlappingFragments.end() &&
2025 "Previously seen var fragment has no vector of overlaps");
2026 ASeenFragmentsOverlaps->second.push_back(Elt: ThisFragment);
2027 }
2028 }
2029
2030 AllSeenFragments.insert(V: ThisFragment);
2031}
2032
2033/// This routine creates OpenRanges.
2034void VarLocBasedLDV::process(MachineInstr &MI, OpenRangesSet &OpenRanges,
2035 VarLocMap &VarLocIDs, TransferMap &Transfers,
2036 InstToEntryLocMap &EntryValTransfers,
2037 RegDefToInstMap &RegSetInstrs) {
2038 if (!MI.isDebugInstr())
2039 LastNonDbgMI = &MI;
2040 transferDebugValue(MI, OpenRanges, VarLocIDs, EntryValTransfers,
2041 RegSetInstrs);
2042 transferRegisterDef(MI, OpenRanges, VarLocIDs, EntryValTransfers,
2043 RegSetInstrs);
2044 transferWasmDef(MI, OpenRanges, VarLocIDs);
2045 transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers);
2046 transferSpillOrRestoreInst(MI, OpenRanges, VarLocIDs, Transfers);
2047}
2048
2049/// This routine joins the analysis results of all incoming edges in @MBB by
2050/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
2051/// source variable in all the predecessors of @MBB reside in the same location.
2052bool VarLocBasedLDV::join(
2053 MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
2054 const VarLocMap &VarLocIDs,
2055 SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
2056 SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks) {
2057 LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n");
2058
2059 VarLocSet InLocsT(Alloc); // Temporary incoming locations.
2060
2061 // For all predecessors of this MBB, find the set of VarLocs that
2062 // can be joined.
2063 int NumVisited = 0;
2064 for (auto *p : MBB.predecessors()) {
2065 // Ignore backedges if we have not visited the predecessor yet. As the
2066 // predecessor hasn't yet had locations propagated into it, most locations
2067 // will not yet be valid, so treat them as all being uninitialized and
2068 // potentially valid. If a location guessed to be correct here is
2069 // invalidated later, we will remove it when we revisit this block.
2070 if (!Visited.count(Ptr: p)) {
2071 LLVM_DEBUG(dbgs() << " ignoring unvisited pred MBB: " << p->getNumber()
2072 << "\n");
2073 continue;
2074 }
2075 auto OL = OutLocs.find(Val: p);
2076 // Join is null in case of empty OutLocs from any of the pred.
2077 if (OL == OutLocs.end())
2078 return false;
2079
2080 // Just copy over the Out locs to incoming locs for the first visited
2081 // predecessor, and for all other predecessors join the Out locs.
2082 VarLocSet &OutLocVLS = *OL->second;
2083 if (!NumVisited)
2084 InLocsT = OutLocVLS;
2085 else
2086 InLocsT &= OutLocVLS;
2087
2088 LLVM_DEBUG({
2089 if (!InLocsT.empty()) {
2090 VarVec VarLocs;
2091 collectAllVarLocs(VarLocs, InLocsT, VarLocIDs);
2092 for (const VarLoc &VL : VarLocs)
2093 dbgs() << " gathered candidate incoming var: "
2094 << VL.Var.getVariable()->getName() << "\n";
2095 }
2096 });
2097
2098 NumVisited++;
2099 }
2100
2101 // Filter out DBG_VALUES that are out of scope.
2102 VarLocSet KillSet(Alloc);
2103 bool IsArtificial = ArtificialBlocks.count(Ptr: &MBB);
2104 if (!IsArtificial) {
2105 for (uint64_t ID : InLocsT) {
2106 LocIndex Idx = LocIndex::fromRawInteger(ID);
2107 if (!VarLocIDs[Idx].dominates(LS, MBB)) {
2108 KillSet.set(ID);
2109 LLVM_DEBUG({
2110 auto Name = VarLocIDs[Idx].Var.getVariable()->getName();
2111 dbgs() << " killing " << Name << ", it doesn't dominate MBB\n";
2112 });
2113 }
2114 }
2115 }
2116 InLocsT.intersectWithComplement(Other: KillSet);
2117
2118 // As we are processing blocks in reverse post-order we
2119 // should have processed at least one predecessor, unless it
2120 // is the entry block which has no predecessor.
2121 assert((NumVisited || MBB.pred_empty()) &&
2122 "Should have processed at least one predecessor");
2123
2124 VarLocSet &ILS = getVarLocsInMBB(MBB: &MBB, Locs&: InLocs);
2125 bool Changed = false;
2126 if (ILS != InLocsT) {
2127 ILS = InLocsT;
2128 Changed = true;
2129 }
2130
2131 return Changed;
2132}
2133
2134void VarLocBasedLDV::flushPendingLocs(VarLocInMBB &PendingInLocs,
2135 VarLocMap &VarLocIDs) {
2136 // PendingInLocs records all locations propagated into blocks, which have
2137 // not had DBG_VALUE insts created. Go through and create those insts now.
2138 for (auto &Iter : PendingInLocs) {
2139 // Map is keyed on a constant pointer, unwrap it so we can insert insts.
2140 auto &MBB = const_cast<MachineBasicBlock &>(*Iter.first);
2141 VarLocSet &Pending = *Iter.second;
2142
2143 SmallVector<VarLoc, 32> VarLocs;
2144 collectAllVarLocs(Collected&: VarLocs, CollectFrom: Pending, VarLocIDs);
2145
2146 for (VarLoc DiffIt : VarLocs) {
2147 // The ID location is live-in to MBB -- work out what kind of machine
2148 // location it is and create a DBG_VALUE.
2149 if (DiffIt.isEntryBackupLoc())
2150 continue;
2151 MachineInstr *MI = DiffIt.BuildDbgValue(MF&: *MBB.getParent());
2152 MBB.insert(I: MBB.instr_begin(), M: MI);
2153
2154 (void)MI;
2155 LLVM_DEBUG(dbgs() << "Inserted: "; MI->dump(););
2156 }
2157 }
2158}
2159
2160bool VarLocBasedLDV::isEntryValueCandidate(
2161 const MachineInstr &MI, const DefinedRegsSet &DefinedRegs) const {
2162 assert(MI.isDebugValue() && "This must be DBG_VALUE.");
2163
2164 // TODO: Add support for local variables that are expressed in terms of
2165 // parameters entry values.
2166 // TODO: Add support for modified arguments that can be expressed
2167 // by using its entry value.
2168 auto *DIVar = MI.getDebugVariable();
2169 if (!DIVar->isParameter())
2170 return false;
2171
2172 // Do not consider parameters that belong to an inlined function.
2173 if (MI.getDebugLoc()->getInlinedAt())
2174 return false;
2175
2176 // Only consider parameters that are described using registers. Parameters
2177 // that are passed on the stack are not yet supported, so ignore debug
2178 // values that are described by the frame or stack pointer.
2179 if (!isRegOtherThanSPAndFP(Op: MI.getDebugOperand(Index: 0), MI, TRI))
2180 return false;
2181
2182 // If a parameter's value has been propagated from the caller, then the
2183 // parameter's DBG_VALUE may be described using a register defined by some
2184 // instruction in the entry block, in which case we shouldn't create an
2185 // entry value.
2186 if (DefinedRegs.count(V: MI.getDebugOperand(Index: 0).getReg()))
2187 return false;
2188
2189 // TODO: Add support for parameters that have a pre-existing debug expressions
2190 // (e.g. fragments).
2191 // A simple deref expression is equivalent to an indirect debug value.
2192 const DIExpression *Expr = MI.getDebugExpression();
2193 if (Expr->getNumElements() > 0 && !Expr->isDeref())
2194 return false;
2195
2196 return true;
2197}
2198
2199/// Collect all register defines (including aliases) for the given instruction.
2200static void collectRegDefs(const MachineInstr &MI, DefinedRegsSet &Regs,
2201 const TargetRegisterInfo *TRI) {
2202 for (const MachineOperand &MO : MI.all_defs()) {
2203 if (MO.getReg() && MO.getReg().isPhysical()) {
2204 Regs.insert(V: MO.getReg());
2205 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
2206 Regs.insert(V: *AI);
2207 }
2208 }
2209}
2210
2211/// This routine records the entry values of function parameters. The values
2212/// could be used as backup values. If we loose the track of some unmodified
2213/// parameters, the backup values will be used as a primary locations.
2214void VarLocBasedLDV::recordEntryValue(const MachineInstr &MI,
2215 const DefinedRegsSet &DefinedRegs,
2216 OpenRangesSet &OpenRanges,
2217 VarLocMap &VarLocIDs) {
2218 if (!ShouldEmitDebugEntryValues)
2219 return;
2220
2221 DebugVariable V(MI.getDebugVariable(), MI.getDebugExpression(),
2222 MI.getDebugLoc()->getInlinedAt());
2223
2224 if (!isEntryValueCandidate(MI, DefinedRegs) ||
2225 OpenRanges.getEntryValueBackup(Var: V))
2226 return;
2227
2228 LLVM_DEBUG(dbgs() << "Creating the backup entry location: "; MI.dump(););
2229
2230 // Create the entry value and use it as a backup location until it is
2231 // valid. It is valid until a parameter is not changed.
2232 DIExpression *NewExpr =
2233 DIExpression::prepend(Expr: MI.getDebugExpression(), Flags: DIExpression::EntryValue);
2234 VarLoc EntryValLocAsBackup = VarLoc::CreateEntryBackupLoc(MI, EntryExpr: NewExpr);
2235 LocIndices EntryValLocIDs = VarLocIDs.insert(VL: EntryValLocAsBackup);
2236 OpenRanges.insert(VarLocIDs: EntryValLocIDs, VL: EntryValLocAsBackup);
2237}
2238
2239/// Calculate the liveness information for the given machine function and
2240/// extend ranges across basic blocks.
2241bool VarLocBasedLDV::ExtendRanges(MachineFunction &MF,
2242 MachineDominatorTree *DomTree,
2243 bool ShouldEmitDebugEntryValues,
2244 unsigned InputBBLimit,
2245 unsigned InputDbgValLimit) {
2246 (void)DomTree;
2247 LLVM_DEBUG(dbgs() << "\nDebug Range Extension: " << MF.getName() << "\n");
2248
2249 if (!MF.getFunction().getSubprogram())
2250 // VarLocBaseLDV will already have removed all DBG_VALUEs.
2251 return false;
2252
2253 // Skip functions from NoDebug compilation units.
2254 if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
2255 DICompileUnit::NoDebug)
2256 return false;
2257
2258 TRI = MF.getSubtarget().getRegisterInfo();
2259 TII = MF.getSubtarget().getInstrInfo();
2260 TFI = MF.getSubtarget().getFrameLowering();
2261 TFI->getCalleeSaves(MF, SavedRegs&: CalleeSavedRegs);
2262 this->ShouldEmitDebugEntryValues = ShouldEmitDebugEntryValues;
2263
2264 LS.scanFunction(MF);
2265
2266 bool Changed = false;
2267 bool OLChanged = false;
2268 bool MBBJoined = false;
2269
2270 VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
2271 OverlapMap OverlapFragments; // Map of overlapping variable fragments.
2272 OpenRangesSet OpenRanges(Alloc, OverlapFragments);
2273 // Ranges that are open until end of bb.
2274 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
2275 VarLocInMBB InLocs; // Ranges that are incoming after joining.
2276 TransferMap Transfers; // DBG_VALUEs associated with transfers (such as
2277 // spills, copies and restores).
2278 // Map responsible MI to attached Transfer emitted from Backup Entry Value.
2279 InstToEntryLocMap EntryValTransfers;
2280 // Map a Register to the last MI which clobbered it.
2281 RegDefToInstMap RegSetInstrs;
2282
2283 VarToFragments SeenFragments;
2284
2285 // Blocks which are artificial, i.e. blocks which exclusively contain
2286 // instructions without locations, or with line 0 locations.
2287 SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks;
2288
2289 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
2290 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
2291 std::priority_queue<unsigned int, std::vector<unsigned int>,
2292 std::greater<unsigned int>>
2293 Worklist;
2294 std::priority_queue<unsigned int, std::vector<unsigned int>,
2295 std::greater<unsigned int>>
2296 Pending;
2297
2298 // Set of register defines that are seen when traversing the entry block
2299 // looking for debug entry value candidates.
2300 DefinedRegsSet DefinedRegs;
2301
2302 // Only in the case of entry MBB collect DBG_VALUEs representing
2303 // function parameters in order to generate debug entry values for them.
2304 MachineBasicBlock &First_MBB = *(MF.begin());
2305 for (auto &MI : First_MBB) {
2306 collectRegDefs(MI, Regs&: DefinedRegs, TRI);
2307 if (MI.isDebugValue())
2308 recordEntryValue(MI, DefinedRegs, OpenRanges, VarLocIDs);
2309 }
2310
2311 // Initialize per-block structures and scan for fragment overlaps.
2312 for (auto &MBB : MF)
2313 for (auto &MI : MBB)
2314 if (MI.isDebugValue())
2315 accumulateFragmentMap(MI, SeenFragments, OverlappingFragments&: OverlapFragments);
2316
2317 auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool {
2318 if (const DebugLoc &DL = MI.getDebugLoc())
2319 return DL.getLine() != 0;
2320 return false;
2321 };
2322 for (auto &MBB : MF)
2323 if (none_of(Range: MBB.instrs(), P: hasNonArtificialLocation))
2324 ArtificialBlocks.insert(Ptr: &MBB);
2325
2326 LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
2327 "OutLocs after initialization", dbgs()));
2328
2329 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
2330 unsigned int RPONumber = 0;
2331 for (MachineBasicBlock *MBB : RPOT) {
2332 OrderToBB[RPONumber] = MBB;
2333 BBToOrder[MBB] = RPONumber;
2334 Worklist.push(x: RPONumber);
2335 ++RPONumber;
2336 }
2337
2338 if (RPONumber > InputBBLimit) {
2339 unsigned NumInputDbgValues = 0;
2340 for (auto &MBB : MF)
2341 for (auto &MI : MBB)
2342 if (MI.isDebugValue())
2343 ++NumInputDbgValues;
2344 if (NumInputDbgValues > InputDbgValLimit) {
2345 LLVM_DEBUG(dbgs() << "Disabling VarLocBasedLDV: " << MF.getName()
2346 << " has " << RPONumber << " basic blocks and "
2347 << NumInputDbgValues
2348 << " input DBG_VALUEs, exceeding limits.\n");
2349 return false;
2350 }
2351 }
2352
2353 // This is a standard "union of predecessor outs" dataflow problem.
2354 // To solve it, we perform join() and process() using the two worklist method
2355 // until the ranges converge.
2356 // Ranges have converged when both worklists are empty.
2357 SmallPtrSet<const MachineBasicBlock *, 16> Visited;
2358 while (!Worklist.empty() || !Pending.empty()) {
2359 // We track what is on the pending worklist to avoid inserting the same
2360 // thing twice. We could avoid this with a custom priority queue, but this
2361 // is probably not worth it.
2362 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
2363 LLVM_DEBUG(dbgs() << "Processing Worklist\n");
2364 while (!Worklist.empty()) {
2365 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
2366 Worklist.pop();
2367 MBBJoined = join(MBB&: *MBB, OutLocs, InLocs, VarLocIDs, Visited,
2368 ArtificialBlocks);
2369 MBBJoined |= Visited.insert(Ptr: MBB).second;
2370 if (MBBJoined) {
2371 MBBJoined = false;
2372 Changed = true;
2373 // Now that we have started to extend ranges across BBs we need to
2374 // examine spill, copy and restore instructions to see whether they
2375 // operate with registers that correspond to user variables.
2376 // First load any pending inlocs.
2377 OpenRanges.insertFromLocSet(ToLoad: getVarLocsInMBB(MBB, Locs&: InLocs), Map: VarLocIDs);
2378 LastNonDbgMI = nullptr;
2379 RegSetInstrs.clear();
2380 // Iterate through instructions within each packet to handle VLIW
2381 // bundles correctly; this keeps DBG_VALUE placement valid on
2382 // packet-based targets.
2383 for (auto I = MBB->instr_begin(), E = MBB->instr_end(); I != E;) {
2384 auto BStart = llvm::getBundleStart(I);
2385 auto BEnd = llvm::getBundleEnd(I);
2386 bool PacketHasTerminator = false;
2387 for (auto BI = BStart; BI != BEnd; ++BI) {
2388 if (BI->isTerminator()) {
2389 PacketHasTerminator = true;
2390 break;
2391 }
2392 }
2393 if (PacketHasTerminator) {
2394 // FIXME: This drops debug info for spills in terminator bundles;
2395 // DBG_VALUE instructions can't be inserted after the bundle.
2396 // It may be possible to insert the DBG_VALUE elsewhere.
2397 I = BEnd;
2398 continue;
2399 }
2400 auto FirstOp = (BStart->isBundle()) ? std::next(x: BStart) : BStart;
2401 for (auto BI = FirstOp; BI != BEnd; ++BI) {
2402 if (BI->isTerminator())
2403 continue;
2404 process(MI&: *BI, OpenRanges, VarLocIDs, Transfers, EntryValTransfers,
2405 RegSetInstrs);
2406 }
2407 I = BEnd;
2408 }
2409 OLChanged |= transferTerminator(CurMBB: MBB, OpenRanges, OutLocs, VarLocIDs);
2410
2411 LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
2412 "OutLocs after propagating", dbgs()));
2413 LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
2414 "InLocs after propagating", dbgs()));
2415
2416 if (OLChanged) {
2417 OLChanged = false;
2418 for (auto *s : MBB->successors())
2419 if (OnPending.insert(Ptr: s).second) {
2420 Pending.push(x: BBToOrder[s]);
2421 }
2422 }
2423 }
2424 }
2425 Worklist.swap(pq&: Pending);
2426 // At this point, pending must be empty, since it was just the empty
2427 // worklist
2428 assert(Pending.empty() && "Pending should be empty");
2429 }
2430
2431 // Add any DBG_VALUE instructions created by location transfers.
2432 for (auto &TR : Transfers) {
2433 assert(!TR.TransferInst->isTerminator() &&
2434 "Cannot insert DBG_VALUE after terminator");
2435 MachineBasicBlock *MBB = TR.TransferInst->getParent();
2436 const VarLoc &VL = VarLocIDs[TR.LocationID];
2437 MachineInstr *MI = VL.BuildDbgValue(MF);
2438 MBB->insertAfterBundle(I: TR.TransferInst->getIterator(), MI);
2439 }
2440 Transfers.clear();
2441
2442 // Add DBG_VALUEs created using Backup Entry Value location.
2443 for (auto &TR : EntryValTransfers) {
2444 MachineInstr *TRInst = const_cast<MachineInstr *>(TR.first);
2445 assert(!TRInst->isTerminator() &&
2446 "Cannot insert DBG_VALUE after terminator");
2447 MachineBasicBlock *MBB = TRInst->getParent();
2448 const VarLoc &VL = VarLocIDs[TR.second];
2449 MachineInstr *MI = VL.BuildDbgValue(MF);
2450 MBB->insertAfterBundle(I: TRInst->getIterator(), MI);
2451 }
2452 EntryValTransfers.clear();
2453
2454 // Deferred inlocs will not have had any DBG_VALUE insts created; do
2455 // that now.
2456 flushPendingLocs(PendingInLocs&: InLocs, VarLocIDs);
2457
2458 LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()));
2459 LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()));
2460 return Changed;
2461}
2462
2463LDVImpl *
2464llvm::makeVarLocBasedLiveDebugValues()
2465{
2466 return new VarLocBasedLDV();
2467}
2468