Coverage Report

Created: 2024-01-17 10:31

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