1 //===- InstrRefBasedImpl.h - Tracking Debug Value MIs ---------------------===//
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 #ifndef LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H
10 #define LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/SmallPtrSet.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/UniqueVector.h"
16 #include "llvm/CodeGen/LexicalScopes.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/TargetFrameLowering.h"
22 #include "llvm/CodeGen/TargetInstrInfo.h"
23 #include "llvm/CodeGen/TargetPassConfig.h"
24 #include "llvm/IR/DebugInfoMetadata.h"
25 
26 #include "LiveDebugValues.h"
27 
28 class TransferTracker;
29 
30 // Forward dec of unit test class, so that we can peer into the LDV object.
31 class InstrRefLDVTest;
32 
33 namespace LiveDebugValues {
34 
35 class MLocTracker;
36 
37 using namespace llvm;
38 
39 /// Handle-class for a particular "location". This value-type uniquely
40 /// symbolises a register or stack location, allowing manipulation of locations
41 /// without concern for where that location is. Practically, this allows us to
42 /// treat the state of the machine at a particular point as an array of values,
43 /// rather than a map of values.
44 class LocIdx {
45   unsigned Location;
46 
47   // Default constructor is private, initializing to an illegal location number.
48   // Use only for "not an entry" elements in IndexedMaps.
49   LocIdx() : Location(UINT_MAX) {}
50 
51 public:
52 #define NUM_LOC_BITS 24
53   LocIdx(unsigned L) : Location(L) {
54     assert(L < (1 << NUM_LOC_BITS) && "Machine locations must fit in 24 bits");
55   }
56 
57   static LocIdx MakeIllegalLoc() { return LocIdx(); }
58 
59   bool isIllegal() const { return Location == UINT_MAX; }
60 
61   uint64_t asU64() const { return Location; }
62 
63   bool operator==(unsigned L) const { return Location == L; }
64 
65   bool operator==(const LocIdx &L) const { return Location == L.Location; }
66 
67   bool operator!=(unsigned L) const { return !(*this == L); }
68 
69   bool operator!=(const LocIdx &L) const { return !(*this == L); }
70 
71   bool operator<(const LocIdx &Other) const {
72     return Location < Other.Location;
73   }
74 };
75 
76 // The location at which a spilled value resides. It consists of a register and
77 // an offset.
78 struct SpillLoc {
79   unsigned SpillBase;
80   StackOffset SpillOffset;
81   bool operator==(const SpillLoc &Other) const {
82     return std::make_pair(SpillBase, SpillOffset) ==
83            std::make_pair(Other.SpillBase, Other.SpillOffset);
84   }
85   bool operator<(const SpillLoc &Other) const {
86     return std::make_tuple(SpillBase, SpillOffset.getFixed(),
87                            SpillOffset.getScalable()) <
88            std::make_tuple(Other.SpillBase, Other.SpillOffset.getFixed(),
89                            Other.SpillOffset.getScalable());
90   }
91 };
92 
93 /// Unique identifier for a value defined by an instruction, as a value type.
94 /// Casts back and forth to a uint64_t. Probably replacable with something less
95 /// bit-constrained. Each value identifies the instruction and machine location
96 /// where the value is defined, although there may be no corresponding machine
97 /// operand for it (ex: regmasks clobbering values). The instructions are
98 /// one-based, and definitions that are PHIs have instruction number zero.
99 ///
100 /// The obvious limits of a 1M block function or 1M instruction blocks are
101 /// problematic; but by that point we should probably have bailed out of
102 /// trying to analyse the function.
103 class ValueIDNum {
104   uint64_t BlockNo : 20;         /// The block where the def happens.
105   uint64_t InstNo : 20;          /// The Instruction where the def happens.
106                                  /// One based, is distance from start of block.
107   uint64_t LocNo : NUM_LOC_BITS; /// The machine location where the def happens.
108 
109 public:
110   // Default-initialize to EmptyValue. This is necessary to make IndexedMaps
111   // of values to work.
112   ValueIDNum() : BlockNo(0xFFFFF), InstNo(0xFFFFF), LocNo(0xFFFFFF) {}
113 
114   ValueIDNum(uint64_t Block, uint64_t Inst, uint64_t Loc)
115       : BlockNo(Block), InstNo(Inst), LocNo(Loc) {}
116 
117   ValueIDNum(uint64_t Block, uint64_t Inst, LocIdx Loc)
118       : BlockNo(Block), InstNo(Inst), LocNo(Loc.asU64()) {}
119 
120   uint64_t getBlock() const { return BlockNo; }
121   uint64_t getInst() const { return InstNo; }
122   uint64_t getLoc() const { return LocNo; }
123   bool isPHI() const { return InstNo == 0; }
124 
125   uint64_t asU64() const {
126     uint64_t TmpBlock = BlockNo;
127     uint64_t TmpInst = InstNo;
128     return TmpBlock << 44ull | TmpInst << NUM_LOC_BITS | LocNo;
129   }
130 
131   static ValueIDNum fromU64(uint64_t v) {
132     uint64_t L = (v & 0x3FFF);
133     return {v >> 44ull, ((v >> NUM_LOC_BITS) & 0xFFFFF), L};
134   }
135 
136   bool operator<(const ValueIDNum &Other) const {
137     return asU64() < Other.asU64();
138   }
139 
140   bool operator==(const ValueIDNum &Other) const {
141     return std::tie(BlockNo, InstNo, LocNo) ==
142            std::tie(Other.BlockNo, Other.InstNo, Other.LocNo);
143   }
144 
145   bool operator!=(const ValueIDNum &Other) const { return !(*this == Other); }
146 
147   std::string asString(const std::string &mlocname) const {
148     return Twine("Value{bb: ")
149         .concat(Twine(BlockNo).concat(
150             Twine(", inst: ")
151                 .concat((InstNo ? Twine(InstNo) : Twine("live-in"))
152                             .concat(Twine(", loc: ").concat(Twine(mlocname)))
153                             .concat(Twine("}")))))
154         .str();
155   }
156 
157   static ValueIDNum EmptyValue;
158 };
159 
160 /// Meta qualifiers for a value. Pair of whatever expression is used to qualify
161 /// the the value, and Boolean of whether or not it's indirect.
162 class DbgValueProperties {
163 public:
164   DbgValueProperties(const DIExpression *DIExpr, bool Indirect)
165       : DIExpr(DIExpr), Indirect(Indirect) {}
166 
167   /// Extract properties from an existing DBG_VALUE instruction.
168   DbgValueProperties(const MachineInstr &MI) {
169     assert(MI.isDebugValue());
170     DIExpr = MI.getDebugExpression();
171     Indirect = MI.getOperand(1).isImm();
172   }
173 
174   bool operator==(const DbgValueProperties &Other) const {
175     return std::tie(DIExpr, Indirect) == std::tie(Other.DIExpr, Other.Indirect);
176   }
177 
178   bool operator!=(const DbgValueProperties &Other) const {
179     return !(*this == Other);
180   }
181 
182   const DIExpression *DIExpr;
183   bool Indirect;
184 };
185 
186 /// Class recording the (high level) _value_ of a variable. Identifies either
187 /// the value of the variable as a ValueIDNum, or a constant MachineOperand.
188 /// This class also stores meta-information about how the value is qualified.
189 /// Used to reason about variable values when performing the second
190 /// (DebugVariable specific) dataflow analysis.
191 class DbgValue {
192 public:
193   /// If Kind is Def, the value number that this value is based on. VPHIs set
194   /// this field to EmptyValue if there is no machine-value for this VPHI, or
195   /// the corresponding machine-value if there is one.
196   ValueIDNum ID;
197   /// If Kind is Const, the MachineOperand defining this value.
198   Optional<MachineOperand> MO;
199   /// For a NoVal or VPHI DbgValue, which block it was generated in.
200   int BlockNo;
201 
202   /// Qualifiers for the ValueIDNum above.
203   DbgValueProperties Properties;
204 
205   typedef enum {
206     Undef, // Represents a DBG_VALUE $noreg in the transfer function only.
207     Def,   // This value is defined by an inst, or is a PHI value.
208     Const, // A constant value contained in the MachineOperand field.
209     VPHI,  // Incoming values to BlockNo differ, those values must be joined by
210            // a PHI in this block.
211     NoVal, // Empty DbgValue indicating an unknown value. Used as initializer,
212            // before dominating blocks values are propagated in.
213   } KindT;
214   /// Discriminator for whether this is a constant or an in-program value.
215   KindT Kind;
216 
217   DbgValue(const ValueIDNum &Val, const DbgValueProperties &Prop, KindT Kind)
218       : ID(Val), MO(None), BlockNo(0), Properties(Prop), Kind(Kind) {
219     assert(Kind == Def);
220   }
221 
222   DbgValue(unsigned BlockNo, const DbgValueProperties &Prop, KindT Kind)
223       : ID(ValueIDNum::EmptyValue), MO(None), BlockNo(BlockNo),
224         Properties(Prop), Kind(Kind) {
225     assert(Kind == NoVal || Kind == VPHI);
226   }
227 
228   DbgValue(const MachineOperand &MO, const DbgValueProperties &Prop, KindT Kind)
229       : ID(ValueIDNum::EmptyValue), MO(MO), BlockNo(0), Properties(Prop),
230         Kind(Kind) {
231     assert(Kind == Const);
232   }
233 
234   DbgValue(const DbgValueProperties &Prop, KindT Kind)
235     : ID(ValueIDNum::EmptyValue), MO(None), BlockNo(0), Properties(Prop),
236       Kind(Kind) {
237     assert(Kind == Undef &&
238            "Empty DbgValue constructor must pass in Undef kind");
239   }
240 
241 #ifndef NDEBUG
242   void dump(const MLocTracker *MTrack) const;
243 #endif
244 
245   bool operator==(const DbgValue &Other) const {
246     if (std::tie(Kind, Properties) != std::tie(Other.Kind, Other.Properties))
247       return false;
248     else if (Kind == Def && ID != Other.ID)
249       return false;
250     else if (Kind == NoVal && BlockNo != Other.BlockNo)
251       return false;
252     else if (Kind == Const)
253       return MO->isIdenticalTo(*Other.MO);
254     else if (Kind == VPHI && BlockNo != Other.BlockNo)
255       return false;
256     else if (Kind == VPHI && ID != Other.ID)
257       return false;
258 
259     return true;
260   }
261 
262   bool operator!=(const DbgValue &Other) const { return !(*this == Other); }
263 };
264 
265 class LocIdxToIndexFunctor {
266 public:
267   using argument_type = LocIdx;
268   unsigned operator()(const LocIdx &L) const { return L.asU64(); }
269 };
270 
271 /// Tracker for what values are in machine locations. Listens to the Things
272 /// being Done by various instructions, and maintains a table of what machine
273 /// locations have what values (as defined by a ValueIDNum).
274 ///
275 /// There are potentially a much larger number of machine locations on the
276 /// target machine than the actual working-set size of the function. On x86 for
277 /// example, we're extremely unlikely to want to track values through control
278 /// or debug registers. To avoid doing so, MLocTracker has several layers of
279 /// indirection going on, with two kinds of ``location'':
280 ///  * A LocID uniquely identifies a register or spill location, with a
281 ///    predictable value.
282 ///  * A LocIdx is a key (in the database sense) for a LocID and a ValueIDNum.
283 /// Whenever a location is def'd or used by a MachineInstr, we automagically
284 /// create a new LocIdx for a location, but not otherwise. This ensures we only
285 /// account for locations that are actually used or defined. The cost is another
286 /// vector lookup (of LocID -> LocIdx) over any other implementation. This is
287 /// fairly cheap, and the compiler tries to reduce the working-set at any one
288 /// time in the function anyway.
289 ///
290 /// Register mask operands completely blow this out of the water; I've just
291 /// piled hacks on top of hacks to get around that.
292 class MLocTracker {
293 public:
294   MachineFunction &MF;
295   const TargetInstrInfo &TII;
296   const TargetRegisterInfo &TRI;
297   const TargetLowering &TLI;
298 
299   /// IndexedMap type, mapping from LocIdx to ValueIDNum.
300   using LocToValueType = IndexedMap<ValueIDNum, LocIdxToIndexFunctor>;
301 
302   /// Map of LocIdxes to the ValueIDNums that they store. This is tightly
303   /// packed, entries only exist for locations that are being tracked.
304   LocToValueType LocIdxToIDNum;
305 
306   /// "Map" of machine location IDs (i.e., raw register or spill number) to the
307   /// LocIdx key / number for that location. There are always at least as many
308   /// as the number of registers on the target -- if the value in the register
309   /// is not being tracked, then the LocIdx value will be zero. New entries are
310   /// appended if a new spill slot begins being tracked.
311   /// This, and the corresponding reverse map persist for the analysis of the
312   /// whole function, and is necessarying for decoding various vectors of
313   /// values.
314   std::vector<LocIdx> LocIDToLocIdx;
315 
316   /// Inverse map of LocIDToLocIdx.
317   IndexedMap<unsigned, LocIdxToIndexFunctor> LocIdxToLocID;
318 
319   /// When clobbering register masks, we chose to not believe the machine model
320   /// and don't clobber SP. Do the same for SP aliases, and for efficiency,
321   /// keep a set of them here.
322   SmallSet<Register, 8> SPAliases;
323 
324   /// Unique-ification of spill slots. Used to number them -- their LocID
325   /// number is the index in SpillLocs minus one plus NumRegs.
326   UniqueVector<SpillLoc> SpillLocs;
327 
328   // If we discover a new machine location, assign it an mphi with this
329   // block number.
330   unsigned CurBB;
331 
332   /// Cached local copy of the number of registers the target has.
333   unsigned NumRegs;
334 
335   /// Collection of register mask operands that have been observed. Second part
336   /// of pair indicates the instruction that they happened in. Used to
337   /// reconstruct where defs happened if we start tracking a location later
338   /// on.
339   SmallVector<std::pair<const MachineOperand *, unsigned>, 32> Masks;
340 
341   /// Iterator for locations and the values they contain. Dereferencing
342   /// produces a struct/pair containing the LocIdx key for this location,
343   /// and a reference to the value currently stored. Simplifies the process
344   /// of seeking a particular location.
345   class MLocIterator {
346     LocToValueType &ValueMap;
347     LocIdx Idx;
348 
349   public:
350     class value_type {
351     public:
352       value_type(LocIdx Idx, ValueIDNum &Value) : Idx(Idx), Value(Value) {}
353       const LocIdx Idx;  /// Read-only index of this location.
354       ValueIDNum &Value; /// Reference to the stored value at this location.
355     };
356 
357     MLocIterator(LocToValueType &ValueMap, LocIdx Idx)
358         : ValueMap(ValueMap), Idx(Idx) {}
359 
360     bool operator==(const MLocIterator &Other) const {
361       assert(&ValueMap == &Other.ValueMap);
362       return Idx == Other.Idx;
363     }
364 
365     bool operator!=(const MLocIterator &Other) const {
366       return !(*this == Other);
367     }
368 
369     void operator++() { Idx = LocIdx(Idx.asU64() + 1); }
370 
371     value_type operator*() { return value_type(Idx, ValueMap[LocIdx(Idx)]); }
372   };
373 
374   MLocTracker(MachineFunction &MF, const TargetInstrInfo &TII,
375               const TargetRegisterInfo &TRI, const TargetLowering &TLI);
376 
377   /// Produce location ID number for indexing LocIDToLocIdx. Takes the register
378   /// or spill number, and flag for whether it's a spill or not.
379   unsigned getLocID(Register RegOrSpill, bool isSpill) {
380     return (isSpill) ? RegOrSpill.id() + NumRegs - 1 : RegOrSpill.id();
381   }
382 
383   /// Accessor for reading the value at Idx.
384   ValueIDNum getNumAtPos(LocIdx Idx) const {
385     assert(Idx.asU64() < LocIdxToIDNum.size());
386     return LocIdxToIDNum[Idx];
387   }
388 
389   unsigned getNumLocs(void) const { return LocIdxToIDNum.size(); }
390 
391   /// Reset all locations to contain a PHI value at the designated block. Used
392   /// sometimes for actual PHI values, othertimes to indicate the block entry
393   /// value (before any more information is known).
394   void setMPhis(unsigned NewCurBB) {
395     CurBB = NewCurBB;
396     for (auto Location : locations())
397       Location.Value = {CurBB, 0, Location.Idx};
398   }
399 
400   /// Load values for each location from array of ValueIDNums. Take current
401   /// bbnum just in case we read a value from a hitherto untouched register.
402   void loadFromArray(ValueIDNum *Locs, unsigned NewCurBB) {
403     CurBB = NewCurBB;
404     // Iterate over all tracked locations, and load each locations live-in
405     // value into our local index.
406     for (auto Location : locations())
407       Location.Value = Locs[Location.Idx.asU64()];
408   }
409 
410   /// Wipe any un-necessary location records after traversing a block.
411   void reset(void) {
412     // We could reset all the location values too; however either loadFromArray
413     // or setMPhis should be called before this object is re-used. Just
414     // clear Masks, they're definitely not needed.
415     Masks.clear();
416   }
417 
418   /// Clear all data. Destroys the LocID <=> LocIdx map, which makes most of
419   /// the information in this pass uninterpretable.
420   void clear(void) {
421     reset();
422     LocIDToLocIdx.clear();
423     LocIdxToLocID.clear();
424     LocIdxToIDNum.clear();
425     // SpillLocs.reset(); XXX UniqueVector::reset assumes a SpillLoc casts from
426     // 0
427     SpillLocs = decltype(SpillLocs)();
428 
429     LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc());
430   }
431 
432   /// Set a locaiton to a certain value.
433   void setMLoc(LocIdx L, ValueIDNum Num) {
434     assert(L.asU64() < LocIdxToIDNum.size());
435     LocIdxToIDNum[L] = Num;
436   }
437 
438   /// Create a LocIdx for an untracked register ID. Initialize it to either an
439   /// mphi value representing a live-in, or a recent register mask clobber.
440   LocIdx trackRegister(unsigned ID);
441 
442   LocIdx lookupOrTrackRegister(unsigned ID) {
443     LocIdx &Index = LocIDToLocIdx[ID];
444     if (Index.isIllegal())
445       Index = trackRegister(ID);
446     return Index;
447   }
448 
449   /// Is register R currently tracked by MLocTracker?
450   bool isRegisterTracked(Register R) {
451     LocIdx &Index = LocIDToLocIdx[R];
452     return !Index.isIllegal();
453   }
454 
455   /// Record a definition of the specified register at the given block / inst.
456   /// This doesn't take a ValueIDNum, because the definition and its location
457   /// are synonymous.
458   void defReg(Register R, unsigned BB, unsigned Inst) {
459     unsigned ID = getLocID(R, false);
460     LocIdx Idx = lookupOrTrackRegister(ID);
461     ValueIDNum ValueID = {BB, Inst, Idx};
462     LocIdxToIDNum[Idx] = ValueID;
463   }
464 
465   /// Set a register to a value number. To be used if the value number is
466   /// known in advance.
467   void setReg(Register R, ValueIDNum ValueID) {
468     unsigned ID = getLocID(R, false);
469     LocIdx Idx = lookupOrTrackRegister(ID);
470     LocIdxToIDNum[Idx] = ValueID;
471   }
472 
473   ValueIDNum readReg(Register R) {
474     unsigned ID = getLocID(R, false);
475     LocIdx Idx = lookupOrTrackRegister(ID);
476     return LocIdxToIDNum[Idx];
477   }
478 
479   /// Reset a register value to zero / empty. Needed to replicate the
480   /// VarLoc implementation where a copy to/from a register effectively
481   /// clears the contents of the source register. (Values can only have one
482   ///  machine location in VarLocBasedImpl).
483   void wipeRegister(Register R) {
484     unsigned ID = getLocID(R, false);
485     LocIdx Idx = LocIDToLocIdx[ID];
486     LocIdxToIDNum[Idx] = ValueIDNum::EmptyValue;
487   }
488 
489   /// Determine the LocIdx of an existing register.
490   LocIdx getRegMLoc(Register R) {
491     unsigned ID = getLocID(R, false);
492     return LocIDToLocIdx[ID];
493   }
494 
495   /// Record a RegMask operand being executed. Defs any register we currently
496   /// track, stores a pointer to the mask in case we have to account for it
497   /// later.
498   void writeRegMask(const MachineOperand *MO, unsigned CurBB, unsigned InstID);
499 
500   /// Find LocIdx for SpillLoc \p L, creating a new one if it's not tracked.
501   LocIdx getOrTrackSpillLoc(SpillLoc L);
502 
503   /// Set the value stored in a spill slot.
504   void setSpill(SpillLoc L, ValueIDNum ValueID) {
505     LocIdx Idx = getOrTrackSpillLoc(L);
506     LocIdxToIDNum[Idx] = ValueID;
507   }
508 
509   /// Read whatever value is in a spill slot, or None if it isn't tracked.
510   Optional<ValueIDNum> readSpill(SpillLoc L) {
511     unsigned SpillID = SpillLocs.idFor(L);
512     if (SpillID == 0)
513       return None;
514 
515     unsigned LocID = getLocID(SpillID, true);
516     LocIdx Idx = LocIDToLocIdx[LocID];
517     return LocIdxToIDNum[Idx];
518   }
519 
520   /// Determine the LocIdx of a spill slot. Return None if it previously
521   /// hasn't had a value assigned.
522   Optional<LocIdx> getSpillMLoc(SpillLoc L) {
523     unsigned SpillID = SpillLocs.idFor(L);
524     if (SpillID == 0)
525       return None;
526     unsigned LocNo = getLocID(SpillID, true);
527     return LocIDToLocIdx[LocNo];
528   }
529 
530   /// Return true if Idx is a spill machine location.
531   bool isSpill(LocIdx Idx) const { return LocIdxToLocID[Idx] >= NumRegs; }
532 
533   MLocIterator begin() { return MLocIterator(LocIdxToIDNum, 0); }
534 
535   MLocIterator end() {
536     return MLocIterator(LocIdxToIDNum, LocIdxToIDNum.size());
537   }
538 
539   /// Return a range over all locations currently tracked.
540   iterator_range<MLocIterator> locations() {
541     return llvm::make_range(begin(), end());
542   }
543 
544   std::string LocIdxToName(LocIdx Idx) const;
545 
546   std::string IDAsString(const ValueIDNum &Num) const;
547 
548 #ifndef NDEBUG
549   LLVM_DUMP_METHOD void dump();
550 
551   LLVM_DUMP_METHOD void dump_mloc_map();
552 #endif
553 
554   /// Create a DBG_VALUE based on  machine location \p MLoc. Qualify it with the
555   /// information in \pProperties, for variable Var. Don't insert it anywhere,
556   /// just return the builder for it.
557   MachineInstrBuilder emitLoc(Optional<LocIdx> MLoc, const DebugVariable &Var,
558                               const DbgValueProperties &Properties);
559 };
560 
561 /// Collection of DBG_VALUEs observed when traversing a block. Records each
562 /// variable and the value the DBG_VALUE refers to. Requires the machine value
563 /// location dataflow algorithm to have run already, so that values can be
564 /// identified.
565 class VLocTracker {
566 public:
567   /// Map DebugVariable to the latest Value it's defined to have.
568   /// Needs to be a MapVector because we determine order-in-the-input-MIR from
569   /// the order in this container.
570   /// We only retain the last DbgValue in each block for each variable, to
571   /// determine the blocks live-out variable value. The Vars container forms the
572   /// transfer function for this block, as part of the dataflow analysis. The
573   /// movement of values between locations inside of a block is handled at a
574   /// much later stage, in the TransferTracker class.
575   MapVector<DebugVariable, DbgValue> Vars;
576   DenseMap<DebugVariable, const DILocation *> Scopes;
577   MachineBasicBlock *MBB = nullptr;
578 
579 public:
580   VLocTracker() {}
581 
582   void defVar(const MachineInstr &MI, const DbgValueProperties &Properties,
583               Optional<ValueIDNum> ID) {
584     assert(MI.isDebugValue() || MI.isDebugRef());
585     DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
586                       MI.getDebugLoc()->getInlinedAt());
587     DbgValue Rec = (ID) ? DbgValue(*ID, Properties, DbgValue::Def)
588                         : DbgValue(Properties, DbgValue::Undef);
589 
590     // Attempt insertion; overwrite if it's already mapped.
591     auto Result = Vars.insert(std::make_pair(Var, Rec));
592     if (!Result.second)
593       Result.first->second = Rec;
594     Scopes[Var] = MI.getDebugLoc().get();
595   }
596 
597   void defVar(const MachineInstr &MI, const MachineOperand &MO) {
598     // Only DBG_VALUEs can define constant-valued variables.
599     assert(MI.isDebugValue());
600     DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
601                       MI.getDebugLoc()->getInlinedAt());
602     DbgValueProperties Properties(MI);
603     DbgValue Rec = DbgValue(MO, Properties, DbgValue::Const);
604 
605     // Attempt insertion; overwrite if it's already mapped.
606     auto Result = Vars.insert(std::make_pair(Var, Rec));
607     if (!Result.second)
608       Result.first->second = Rec;
609     Scopes[Var] = MI.getDebugLoc().get();
610   }
611 };
612 
613 /// Types for recording sets of variable fragments that overlap. For a given
614 /// local variable, we record all other fragments of that variable that could
615 /// overlap it, to reduce search time.
616 using FragmentOfVar =
617     std::pair<const DILocalVariable *, DIExpression::FragmentInfo>;
618 using OverlapMap =
619     DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>;
620 
621 // XXX XXX docs
622 class InstrRefBasedLDV : public LDVImpl {
623 public:
624   friend class ::InstrRefLDVTest;
625 
626   using FragmentInfo = DIExpression::FragmentInfo;
627   using OptFragmentInfo = Optional<DIExpression::FragmentInfo>;
628 
629   // Helper while building OverlapMap, a map of all fragments seen for a given
630   // DILocalVariable.
631   using VarToFragments =
632       DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>;
633 
634   /// Machine location/value transfer function, a mapping of which locations
635   /// are assigned which new values.
636   using MLocTransferMap = std::map<LocIdx, ValueIDNum>;
637 
638   /// Live in/out structure for the variable values: a per-block map of
639   /// variables to their values.
640   using LiveIdxT = DenseMap<const MachineBasicBlock *, DbgValue *>;
641 
642   using VarAndLoc = std::pair<DebugVariable, DbgValue>;
643 
644   /// Type for a live-in value: the predecessor block, and its value.
645   using InValueT = std::pair<MachineBasicBlock *, DbgValue *>;
646 
647   /// Vector (per block) of a collection (inner smallvector) of live-ins.
648   /// Used as the result type for the variable value dataflow problem.
649   using LiveInsT = SmallVector<SmallVector<VarAndLoc, 8>, 8>;
650 
651 private:
652   MachineDominatorTree *DomTree;
653   const TargetRegisterInfo *TRI;
654   const TargetInstrInfo *TII;
655   const TargetFrameLowering *TFI;
656   const MachineFrameInfo *MFI;
657   BitVector CalleeSavedRegs;
658   LexicalScopes LS;
659   TargetPassConfig *TPC;
660 
661   // An empty DIExpression. Used default / placeholder DbgValueProperties
662   // objects, as we can't have null expressions.
663   const DIExpression *EmptyExpr;
664 
665   /// Object to track machine locations as we step through a block. Could
666   /// probably be a field rather than a pointer, as it's always used.
667   MLocTracker *MTracker;
668 
669   /// Number of the current block LiveDebugValues is stepping through.
670   unsigned CurBB;
671 
672   /// Number of the current instruction LiveDebugValues is evaluating.
673   unsigned CurInst;
674 
675   /// Variable tracker -- listens to DBG_VALUEs occurring as InstrRefBasedImpl
676   /// steps through a block. Reads the values at each location from the
677   /// MLocTracker object.
678   VLocTracker *VTracker;
679 
680   /// Tracker for transfers, listens to DBG_VALUEs and transfers of values
681   /// between locations during stepping, creates new DBG_VALUEs when values move
682   /// location.
683   TransferTracker *TTracker;
684 
685   /// Blocks which are artificial, i.e. blocks which exclusively contain
686   /// instructions without DebugLocs, or with line 0 locations.
687   SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks;
688 
689   // Mapping of blocks to and from their RPOT order.
690   DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
691   DenseMap<const MachineBasicBlock *, unsigned int> BBToOrder;
692   DenseMap<unsigned, unsigned> BBNumToRPO;
693 
694   /// Pair of MachineInstr, and its 1-based offset into the containing block.
695   using InstAndNum = std::pair<const MachineInstr *, unsigned>;
696   /// Map from debug instruction number to the MachineInstr labelled with that
697   /// number, and its location within the function. Used to transform
698   /// instruction numbers in DBG_INSTR_REFs into machine value numbers.
699   std::map<uint64_t, InstAndNum> DebugInstrNumToInstr;
700 
701   /// Record of where we observed a DBG_PHI instruction.
702   class DebugPHIRecord {
703   public:
704     uint64_t InstrNum;      ///< Instruction number of this DBG_PHI.
705     MachineBasicBlock *MBB; ///< Block where DBG_PHI occurred.
706     ValueIDNum ValueRead;   ///< The value number read by the DBG_PHI.
707     LocIdx ReadLoc;         ///< Register/Stack location the DBG_PHI reads.
708 
709     operator unsigned() const { return InstrNum; }
710   };
711 
712   /// Map from instruction numbers defined by DBG_PHIs to a record of what that
713   /// DBG_PHI read and where. Populated and edited during the machine value
714   /// location problem -- we use LLVMs SSA Updater to fix changes by
715   /// optimizations that destroy PHI instructions.
716   SmallVector<DebugPHIRecord, 32> DebugPHINumToValue;
717 
718   // Map of overlapping variable fragments.
719   OverlapMap OverlapFragments;
720   VarToFragments SeenFragments;
721 
722   /// Tests whether this instruction is a spill to a stack slot.
723   bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF);
724 
725   /// Decide if @MI is a spill instruction and return true if it is. We use 2
726   /// criteria to make this decision:
727   /// - Is this instruction a store to a spill slot?
728   /// - Is there a register operand that is both used and killed?
729   /// TODO: Store optimization can fold spills into other stores (including
730   /// other spills). We do not handle this yet (more than one memory operand).
731   bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF,
732                        unsigned &Reg);
733 
734   /// If a given instruction is identified as a spill, return the spill slot
735   /// and set \p Reg to the spilled register.
736   Optional<SpillLoc> isRestoreInstruction(const MachineInstr &MI,
737                                           MachineFunction *MF, unsigned &Reg);
738 
739   /// Given a spill instruction, extract the register and offset used to
740   /// address the spill slot in a target independent way.
741   SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI);
742 
743   /// Observe a single instruction while stepping through a block.
744   void process(MachineInstr &MI, ValueIDNum **MLiveOuts = nullptr,
745                ValueIDNum **MLiveIns = nullptr);
746 
747   /// Examines whether \p MI is a DBG_VALUE and notifies trackers.
748   /// \returns true if MI was recognized and processed.
749   bool transferDebugValue(const MachineInstr &MI);
750 
751   /// Examines whether \p MI is a DBG_INSTR_REF and notifies trackers.
752   /// \returns true if MI was recognized and processed.
753   bool transferDebugInstrRef(MachineInstr &MI, ValueIDNum **MLiveOuts,
754                              ValueIDNum **MLiveIns);
755 
756   /// Stores value-information about where this PHI occurred, and what
757   /// instruction number is associated with it.
758   /// \returns true if MI was recognized and processed.
759   bool transferDebugPHI(MachineInstr &MI);
760 
761   /// Examines whether \p MI is copy instruction, and notifies trackers.
762   /// \returns true if MI was recognized and processed.
763   bool transferRegisterCopy(MachineInstr &MI);
764 
765   /// Examines whether \p MI is stack spill or restore  instruction, and
766   /// notifies trackers. \returns true if MI was recognized and processed.
767   bool transferSpillOrRestoreInst(MachineInstr &MI);
768 
769   /// Examines \p MI for any registers that it defines, and notifies trackers.
770   void transferRegisterDef(MachineInstr &MI);
771 
772   /// Copy one location to the other, accounting for movement of subregisters
773   /// too.
774   void performCopy(Register Src, Register Dst);
775 
776   void accumulateFragmentMap(MachineInstr &MI);
777 
778   /// Determine the machine value number referred to by (potentially several)
779   /// DBG_PHI instructions. Block duplication and tail folding can duplicate
780   /// DBG_PHIs, shifting the position where values in registers merge, and
781   /// forming another mini-ssa problem to solve.
782   /// \p Here the position of a DBG_INSTR_REF seeking a machine value number
783   /// \p InstrNum Debug instruction number defined by DBG_PHI instructions.
784   /// \returns The machine value number at position Here, or None.
785   Optional<ValueIDNum> resolveDbgPHIs(MachineFunction &MF,
786                                       ValueIDNum **MLiveOuts,
787                                       ValueIDNum **MLiveIns, MachineInstr &Here,
788                                       uint64_t InstrNum);
789 
790   /// Step through the function, recording register definitions and movements
791   /// in an MLocTracker. Convert the observations into a per-block transfer
792   /// function in \p MLocTransfer, suitable for using with the machine value
793   /// location dataflow problem.
794   void
795   produceMLocTransferFunction(MachineFunction &MF,
796                               SmallVectorImpl<MLocTransferMap> &MLocTransfer,
797                               unsigned MaxNumBlocks);
798 
799   /// Solve the machine value location dataflow problem. Takes as input the
800   /// transfer functions in \p MLocTransfer. Writes the output live-in and
801   /// live-out arrays to the (initialized to zero) multidimensional arrays in
802   /// \p MInLocs and \p MOutLocs. The outer dimension is indexed by block
803   /// number, the inner by LocIdx.
804   void buildMLocValueMap(MachineFunction &MF, ValueIDNum **MInLocs,
805                          ValueIDNum **MOutLocs,
806                          SmallVectorImpl<MLocTransferMap> &MLocTransfer);
807 
808   /// Install PHI values into the live-in array for each block, according to
809   /// the IDF of each register.
810   void placeMLocPHIs(MachineFunction &MF,
811                      SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks,
812                      ValueIDNum **MInLocs,
813                      SmallVectorImpl<MLocTransferMap> &MLocTransfer);
814 
815   /// Calculate the iterated-dominance-frontier for a set of defs, using the
816   /// existing LLVM facilities for this. Works for a single "value" or
817   /// machine/variable location.
818   /// \p AllBlocks Set of blocks where we might consume the value.
819   /// \p DefBlocks Set of blocks where the value/location is defined.
820   /// \p PHIBlocks Output set of blocks where PHIs must be placed.
821   void BlockPHIPlacement(const SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks,
822                          const SmallPtrSetImpl<MachineBasicBlock *> &DefBlocks,
823                          SmallVectorImpl<MachineBasicBlock *> &PHIBlocks);
824 
825   /// Perform a control flow join (lattice value meet) of the values in machine
826   /// locations at \p MBB. Follows the algorithm described in the file-comment,
827   /// reading live-outs of predecessors from \p OutLocs, the current live ins
828   /// from \p InLocs, and assigning the newly computed live ins back into
829   /// \p InLocs. \returns two bools -- the first indicates whether a change
830   /// was made, the second whether a lattice downgrade occurred. If the latter
831   /// is true, revisiting this block is necessary.
832   bool mlocJoin(MachineBasicBlock &MBB,
833                 SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
834                 ValueIDNum **OutLocs, ValueIDNum *InLocs);
835 
836   /// Solve the variable value dataflow problem, for a single lexical scope.
837   /// Uses the algorithm from the file comment to resolve control flow joins
838   /// using PHI placement and value propagation. Reads the locations of machine
839   /// values from the \p MInLocs and \p MOutLocs arrays (see buildMLocValueMap)
840   /// and reads the variable values transfer function from \p AllTheVlocs.
841   /// Live-in and Live-out variable values are stored locally, with the live-ins
842   /// permanently stored to \p Output once a fixedpoint is reached.
843   /// \p VarsWeCareAbout contains a collection of the variables in \p Scope
844   /// that we should be tracking.
845   /// \p AssignBlocks contains the set of blocks that aren't in \p DILoc's
846   /// scope, but which do contain DBG_VALUEs, which VarLocBasedImpl tracks
847   /// locations through.
848   void buildVLocValueMap(const DILocation *DILoc,
849                     const SmallSet<DebugVariable, 4> &VarsWeCareAbout,
850                     SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks,
851                     LiveInsT &Output, ValueIDNum **MOutLocs,
852                     ValueIDNum **MInLocs,
853                     SmallVectorImpl<VLocTracker> &AllTheVLocs);
854 
855   /// Attempt to eliminate un-necessary PHIs on entry to a block. Examines the
856   /// live-in values coming from predecessors live-outs, and replaces any PHIs
857   /// already present in this blocks live-ins with a live-through value if the
858   /// PHI isn't needed.
859   /// \p LiveIn Old live-in value, overwritten with new one if live-in changes.
860   /// \returns true if any live-ins change value, either from value propagation
861   ///          or PHI elimination.
862   bool vlocJoin(MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs,
863                 SmallPtrSet<const MachineBasicBlock *, 8> &InScopeBlocks,
864                 SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore,
865                 DbgValue &LiveIn);
866 
867   /// For the given block and live-outs feeding into it, try to find a
868   /// machine location where all the variable values join together.
869   /// \returns Value ID of a machine PHI if an appropriate one is available.
870   Optional<ValueIDNum>
871   pickVPHILoc(const MachineBasicBlock &MBB, const DebugVariable &Var,
872               const LiveIdxT &LiveOuts, ValueIDNum **MOutLocs,
873               const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders);
874 
875   /// Given the solutions to the two dataflow problems, machine value locations
876   /// in \p MInLocs and live-in variable values in \p SavedLiveIns, runs the
877   /// TransferTracker class over the function to produce live-in and transfer
878   /// DBG_VALUEs, then inserts them. Groups of DBG_VALUEs are inserted in the
879   /// order given by AllVarsNumbering -- this could be any stable order, but
880   /// right now "order of appearence in function, when explored in RPO", so
881   /// that we can compare explictly against VarLocBasedImpl.
882   void emitLocations(MachineFunction &MF, LiveInsT SavedLiveIns,
883                      ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
884                      DenseMap<DebugVariable, unsigned> &AllVarsNumbering,
885                      const TargetPassConfig &TPC);
886 
887   /// Boilerplate computation of some initial sets, artifical blocks and
888   /// RPOT block ordering.
889   void initialSetup(MachineFunction &MF);
890 
891   bool ExtendRanges(MachineFunction &MF, MachineDominatorTree *DomTree,
892                     TargetPassConfig *TPC, unsigned InputBBLimit,
893                     unsigned InputDbgValLimit) override;
894 
895 public:
896   /// Default construct and initialize the pass.
897   InstrRefBasedLDV();
898 
899   LLVM_DUMP_METHOD
900   void dump_mloc_transfer(const MLocTransferMap &mloc_transfer) const;
901 
902   bool isCalleeSaved(LocIdx L) const;
903 };
904 
905 } // namespace LiveDebugValues
906 
907 #endif /* LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H */
908