1 //===- InstrRefBasedImpl.cpp - 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 /// \file InstrRefBasedImpl.cpp
9 ///
10 /// This is a separate implementation of LiveDebugValues, see
11 /// LiveDebugValues.cpp and VarLocBasedImpl.cpp for more information.
12 ///
13 /// This pass propagates variable locations between basic blocks, resolving
14 /// control flow conflicts between them. The problem is much like SSA
15 /// construction, where each DBG_VALUE instruction assigns the *value* that
16 /// a variable has, and every instruction where the variable is in scope uses
17 /// that variable. The resulting map of instruction-to-value is then translated
18 /// into a register (or spill) location for each variable over each instruction.
19 ///
20 /// This pass determines which DBG_VALUE dominates which instructions, or if
21 /// none do, where values must be merged (like PHI nodes). The added
22 /// complication is that because codegen has already finished, a PHI node may
23 /// be needed for a variable location to be correct, but no register or spill
24 /// slot merges the necessary values. In these circumstances, the variable
25 /// location is dropped.
26 ///
27 /// What makes this analysis non-trivial is loops: we cannot tell in advance
28 /// whether a variable location is live throughout a loop, or whether its
29 /// location is clobbered (or redefined by another DBG_VALUE), without
30 /// exploring all the way through.
31 ///
32 /// To make this simpler we perform two kinds of analysis. First, we identify
33 /// every value defined by every instruction (ignoring those that only move
34 /// another value), then compute a map of which values are available for each
35 /// instruction. This is stronger than a reaching-def analysis, as we create
36 /// PHI values where other values merge.
37 ///
38 /// Secondly, for each variable, we effectively re-construct SSA using each
39 /// DBG_VALUE as a def. The DBG_VALUEs read a value-number computed by the
40 /// first analysis from the location they refer to. We can then compute the
41 /// dominance frontiers of where a variable has a value, and create PHI nodes
42 /// where they merge.
43 /// This isn't precisely SSA-construction though, because the function shape
44 /// is pre-defined. If a variable location requires a PHI node, but no
45 /// PHI for the relevant values is present in the function (as computed by the
46 /// first analysis), the location must be dropped.
47 ///
48 /// Once both are complete, we can pass back over all instructions knowing:
49 ///  * What _value_ each variable should contain, either defined by an
50 ///    instruction or where control flow merges
51 ///  * What the location of that value is (if any).
52 /// Allowing us to create appropriate live-in DBG_VALUEs, and DBG_VALUEs when
53 /// a value moves location. After this pass runs, all variable locations within
54 /// a block should be specified by DBG_VALUEs within that block, allowing
55 /// DbgEntityHistoryCalculator to focus on individual blocks.
56 ///
57 /// This pass is able to go fast because the size of the first
58 /// reaching-definition analysis is proportional to the working-set size of
59 /// the function, which the compiler tries to keep small. (It's also
60 /// proportional to the number of blocks). Additionally, we repeatedly perform
61 /// the second reaching-definition analysis with only the variables and blocks
62 /// in a single lexical scope, exploiting their locality.
63 ///
64 /// Determining where PHIs happen is trickier with this approach, and it comes
65 /// to a head in the major problem for LiveDebugValues: is a value live-through
66 /// a loop, or not? Your garden-variety dataflow analysis aims to build a set of
67 /// facts about a function, however this analysis needs to generate new value
68 /// numbers at joins.
69 ///
70 /// To do this, consider a lattice of all definition values, from instructions
71 /// and from PHIs. Each PHI is characterised by the RPO number of the block it
72 /// occurs in. Each value pair A, B can be ordered by RPO(A) < RPO(B):
73 /// with non-PHI values at the top, and any PHI value in the last block (by RPO
74 /// order) at the bottom.
75 ///
76 /// (Awkwardly: lower-down-the _lattice_ means a greater RPO _number_. Below,
77 /// "rank" always refers to the former).
78 ///
79 /// At any join, for each register, we consider:
80 ///  * All incoming values, and
81 ///  * The PREVIOUS live-in value at this join.
82 /// If all incoming values agree: that's the live-in value. If they do not, the
83 /// incoming values are ranked according to the partial order, and the NEXT
84 /// LOWEST rank after the PREVIOUS live-in value is picked (multiple values of
85 /// the same rank are ignored as conflicting). If there are no candidate values,
86 /// or if the rank of the live-in would be lower than the rank of the current
87 /// blocks PHIs, create a new PHI value.
88 ///
89 /// Intuitively: if it's not immediately obvious what value a join should result
90 /// in, we iteratively descend from instruction-definitions down through PHI
91 /// values, getting closer to the current block each time. If the current block
92 /// is a loop head, this ordering is effectively searching outer levels of
93 /// loops, to find a value that's live-through the current loop.
94 ///
95 /// If there is no value that's live-through this loop, a PHI is created for
96 /// this location instead. We can't use a lower-ranked PHI because by definition
97 /// it doesn't dominate the current block. We can't create a PHI value any
98 /// earlier, because we risk creating a PHI value at a location where values do
99 /// not in fact merge, thus misrepresenting the truth, and not making the true
100 /// live-through value for variable locations.
101 ///
102 /// This algorithm applies to both calculating the availability of values in
103 /// the first analysis, and the location of variables in the second. However
104 /// for the second we add an extra dimension of pain: creating a variable
105 /// location PHI is only valid if, for each incoming edge,
106 ///  * There is a value for the variable on the incoming edge, and
107 ///  * All the edges have that value in the same register.
108 /// Or put another way: we can only create a variable-location PHI if there is
109 /// a matching machine-location PHI, each input to which is the variables value
110 /// in the predecessor block.
111 ///
112 /// To accomodate this difference, each point on the lattice is split in
113 /// two: a "proposed" PHI and "definite" PHI. Any PHI that can immediately
114 /// have a location determined are "definite" PHIs, and no further work is
115 /// needed. Otherwise, a location that all non-backedge predecessors agree
116 /// on is picked and propagated as a "proposed" PHI value. If that PHI value
117 /// is truly live-through, it'll appear on the loop backedges on the next
118 /// dataflow iteration, after which the block live-in moves to be a "definite"
119 /// PHI. If it's not truly live-through, the variable value will be downgraded
120 /// further as we explore the lattice, or remains "proposed" and is considered
121 /// invalid once dataflow completes.
122 ///
123 /// ### Terminology
124 ///
125 /// A machine location is a register or spill slot, a value is something that's
126 /// defined by an instruction or PHI node, while a variable value is the value
127 /// assigned to a variable. A variable location is a machine location, that must
128 /// contain the appropriate variable value. A value that is a PHI node is
129 /// occasionally called an mphi.
130 ///
131 /// The first dataflow problem is the "machine value location" problem,
132 /// because we're determining which machine locations contain which values.
133 /// The "locations" are constant: what's unknown is what value they contain.
134 ///
135 /// The second dataflow problem (the one for variables) is the "variable value
136 /// problem", because it's determining what values a variable has, rather than
137 /// what location those values are placed in. Unfortunately, it's not that
138 /// simple, because producing a PHI value always involves picking a location.
139 /// This is an imperfection that we just have to accept, at least for now.
140 ///
141 /// TODO:
142 ///   Overlapping fragments
143 ///   Entry values
144 ///   Add back DEBUG statements for debugging this
145 ///   Collect statistics
146 ///
147 //===----------------------------------------------------------------------===//
148 
149 #include "llvm/ADT/DenseMap.h"
150 #include "llvm/ADT/PostOrderIterator.h"
151 #include "llvm/ADT/SmallPtrSet.h"
152 #include "llvm/ADT/SmallSet.h"
153 #include "llvm/ADT/SmallVector.h"
154 #include "llvm/ADT/Statistic.h"
155 #include "llvm/ADT/UniqueVector.h"
156 #include "llvm/CodeGen/LexicalScopes.h"
157 #include "llvm/CodeGen/MachineBasicBlock.h"
158 #include "llvm/CodeGen/MachineFrameInfo.h"
159 #include "llvm/CodeGen/MachineFunction.h"
160 #include "llvm/CodeGen/MachineFunctionPass.h"
161 #include "llvm/CodeGen/MachineInstr.h"
162 #include "llvm/CodeGen/MachineInstrBuilder.h"
163 #include "llvm/CodeGen/MachineMemOperand.h"
164 #include "llvm/CodeGen/MachineOperand.h"
165 #include "llvm/CodeGen/PseudoSourceValue.h"
166 #include "llvm/CodeGen/RegisterScavenging.h"
167 #include "llvm/CodeGen/TargetFrameLowering.h"
168 #include "llvm/CodeGen/TargetInstrInfo.h"
169 #include "llvm/CodeGen/TargetLowering.h"
170 #include "llvm/CodeGen/TargetPassConfig.h"
171 #include "llvm/CodeGen/TargetRegisterInfo.h"
172 #include "llvm/CodeGen/TargetSubtargetInfo.h"
173 #include "llvm/Config/llvm-config.h"
174 #include "llvm/IR/DIBuilder.h"
175 #include "llvm/IR/DebugInfoMetadata.h"
176 #include "llvm/IR/DebugLoc.h"
177 #include "llvm/IR/Function.h"
178 #include "llvm/IR/Module.h"
179 #include "llvm/InitializePasses.h"
180 #include "llvm/MC/MCRegisterInfo.h"
181 #include "llvm/Pass.h"
182 #include "llvm/Support/Casting.h"
183 #include "llvm/Support/Compiler.h"
184 #include "llvm/Support/Debug.h"
185 #include "llvm/Support/raw_ostream.h"
186 #include <algorithm>
187 #include <cassert>
188 #include <cstdint>
189 #include <functional>
190 #include <queue>
191 #include <tuple>
192 #include <utility>
193 #include <vector>
194 #include <limits.h>
195 #include <limits>
196 
197 #include "LiveDebugValues.h"
198 
199 using namespace llvm;
200 
201 #define DEBUG_TYPE "livedebugvalues"
202 
203 STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted");
204 STATISTIC(NumRemoved, "Number of DBG_VALUE instructions removed");
205 
206 // Act more like the VarLoc implementation, by propagating some locations too
207 // far and ignoring some transfers.
208 static cl::opt<bool> EmulateOldLDV("emulate-old-livedebugvalues", cl::Hidden,
209                                    cl::desc("Act like old LiveDebugValues did"),
210                                    cl::init(false));
211 
212 // Rely on isStoreToStackSlotPostFE and similar to observe all stack spills.
213 static cl::opt<bool>
214     ObserveAllStackops("observe-all-stack-ops", cl::Hidden,
215                        cl::desc("Allow non-kill spill and restores"),
216                        cl::init(false));
217 
218 namespace {
219 
220 // The location at which a spilled value resides. It consists of a register and
221 // an offset.
222 struct SpillLoc {
223   unsigned SpillBase;
224   int SpillOffset;
225   bool operator==(const SpillLoc &Other) const {
226     return std::tie(SpillBase, SpillOffset) ==
227            std::tie(Other.SpillBase, Other.SpillOffset);
228   }
229   bool operator<(const SpillLoc &Other) const {
230     return std::tie(SpillBase, SpillOffset) <
231            std::tie(Other.SpillBase, Other.SpillOffset);
232   }
233 };
234 
235 class LocIdx {
236   unsigned Location;
237 
238   // Default constructor is private, initializing to an illegal location number.
239   // Use only for "not an entry" elements in IndexedMaps.
240   LocIdx() : Location(UINT_MAX) { }
241 
242 public:
243   #define NUM_LOC_BITS 24
244   LocIdx(unsigned L) : Location(L) {
245     assert(L < (1 << NUM_LOC_BITS) && "Machine locations must fit in 24 bits");
246   }
247 
248   static LocIdx MakeIllegalLoc() {
249     return LocIdx();
250   }
251 
252   bool isIllegal() const {
253     return Location == UINT_MAX;
254   }
255 
256   uint64_t asU64() const {
257     return Location;
258   }
259 
260   bool operator==(unsigned L) const {
261     return Location == L;
262   }
263 
264   bool operator==(const LocIdx &L) const {
265     return Location == L.Location;
266   }
267 
268   bool operator!=(unsigned L) const {
269     return !(*this == L);
270   }
271 
272   bool operator!=(const LocIdx &L) const {
273     return !(*this == L);
274   }
275 
276   bool operator<(const LocIdx &Other) const {
277     return Location < Other.Location;
278   }
279 };
280 
281 class LocIdxToIndexFunctor {
282 public:
283   using argument_type = LocIdx;
284   unsigned operator()(const LocIdx &L) const {
285     return L.asU64();
286   }
287 };
288 
289 /// Unique identifier for a value defined by an instruction, as a value type.
290 /// Casts back and forth to a uint64_t. Probably replacable with something less
291 /// bit-constrained. Each value identifies the instruction and machine location
292 /// where the value is defined, although there may be no corresponding machine
293 /// operand for it (ex: regmasks clobbering values). The instructions are
294 /// one-based, and definitions that are PHIs have instruction number zero.
295 ///
296 /// The obvious limits of a 1M block function or 1M instruction blocks are
297 /// problematic; but by that point we should probably have bailed out of
298 /// trying to analyse the function.
299 class ValueIDNum {
300   uint64_t BlockNo : 20;         /// The block where the def happens.
301   uint64_t InstNo : 20;          /// The Instruction where the def happens.
302                                  /// One based, is distance from start of block.
303   uint64_t LocNo : NUM_LOC_BITS; /// The machine location where the def happens.
304 
305 public:
306   // XXX -- temporarily enabled while the live-in / live-out tables are moved
307   // to something more type-y
308   ValueIDNum() : BlockNo(0xFFFFF),
309                  InstNo(0xFFFFF),
310                  LocNo(0xFFFFFF) { }
311 
312   ValueIDNum(uint64_t Block, uint64_t Inst, uint64_t Loc)
313     : BlockNo(Block), InstNo(Inst), LocNo(Loc) { }
314 
315   ValueIDNum(uint64_t Block, uint64_t Inst, LocIdx Loc)
316     : BlockNo(Block), InstNo(Inst), LocNo(Loc.asU64()) { }
317 
318   uint64_t getBlock() const { return BlockNo; }
319   uint64_t getInst() const { return InstNo; }
320   uint64_t getLoc() const { return LocNo; }
321   bool isPHI() const { return InstNo == 0; }
322 
323   uint64_t asU64() const {
324     uint64_t TmpBlock = BlockNo;
325     uint64_t TmpInst = InstNo;
326     return TmpBlock << 44ull | TmpInst << NUM_LOC_BITS | LocNo;
327   }
328 
329   static ValueIDNum fromU64(uint64_t v) {
330     uint64_t L = (v & 0x3FFF);
331     return {v >> 44ull, ((v >> NUM_LOC_BITS) & 0xFFFFF), L};
332   }
333 
334   bool operator<(const ValueIDNum &Other) const {
335     return asU64() < Other.asU64();
336   }
337 
338   bool operator==(const ValueIDNum &Other) const {
339     return std::tie(BlockNo, InstNo, LocNo) ==
340            std::tie(Other.BlockNo, Other.InstNo, Other.LocNo);
341   }
342 
343   bool operator!=(const ValueIDNum &Other) const { return !(*this == Other); }
344 
345   std::string asString(const std::string &mlocname) const {
346     return Twine("bb ")
347         .concat(Twine(BlockNo).concat(Twine(" inst ").concat(
348             Twine(InstNo).concat(Twine(" loc ").concat(Twine(mlocname))))))
349         .str();
350   }
351 
352   static ValueIDNum EmptyValue;
353 };
354 
355 } // end anonymous namespace
356 
357 namespace {
358 
359 /// Meta qualifiers for a value. Pair of whatever expression is used to qualify
360 /// the the value, and Boolean of whether or not it's indirect.
361 class DbgValueProperties {
362 public:
363   DbgValueProperties(const DIExpression *DIExpr, bool Indirect)
364       : DIExpr(DIExpr), Indirect(Indirect) {}
365 
366   /// Extract properties from an existing DBG_VALUE instruction.
367   DbgValueProperties(const MachineInstr &MI) {
368     assert(MI.isDebugValue());
369     DIExpr = MI.getDebugExpression();
370     Indirect = MI.getOperand(1).isImm();
371   }
372 
373   bool operator==(const DbgValueProperties &Other) const {
374     return std::tie(DIExpr, Indirect) == std::tie(Other.DIExpr, Other.Indirect);
375   }
376 
377   bool operator!=(const DbgValueProperties &Other) const {
378     return !(*this == Other);
379   }
380 
381   const DIExpression *DIExpr;
382   bool Indirect;
383 };
384 
385 /// Tracker for what values are in machine locations. Listens to the Things
386 /// being Done by various instructions, and maintains a table of what machine
387 /// locations have what values (as defined by a ValueIDNum).
388 ///
389 /// There are potentially a much larger number of machine locations on the
390 /// target machine than the actual working-set size of the function. On x86 for
391 /// example, we're extremely unlikely to want to track values through control
392 /// or debug registers. To avoid doing so, MLocTracker has several layers of
393 /// indirection going on, with two kinds of ``location'':
394 ///  * A LocID uniquely identifies a register or spill location, with a
395 ///    predictable value.
396 ///  * A LocIdx is a key (in the database sense) for a LocID and a ValueIDNum.
397 /// Whenever a location is def'd or used by a MachineInstr, we automagically
398 /// create a new LocIdx for a location, but not otherwise. This ensures we only
399 /// account for locations that are actually used or defined. The cost is another
400 /// vector lookup (of LocID -> LocIdx) over any other implementation. This is
401 /// fairly cheap, and the compiler tries to reduce the working-set at any one
402 /// time in the function anyway.
403 ///
404 /// Register mask operands completely blow this out of the water; I've just
405 /// piled hacks on top of hacks to get around that.
406 class MLocTracker {
407 public:
408   MachineFunction &MF;
409   const TargetInstrInfo &TII;
410   const TargetRegisterInfo &TRI;
411   const TargetLowering &TLI;
412 
413   /// IndexedMap type, mapping from LocIdx to ValueIDNum.
414   typedef IndexedMap<ValueIDNum, LocIdxToIndexFunctor> LocToValueType;
415 
416   /// Map of LocIdxes to the ValueIDNums that they store. This is tightly
417   /// packed, entries only exist for locations that are being tracked.
418   LocToValueType LocIdxToIDNum;
419 
420   /// "Map" of machine location IDs (i.e., raw register or spill number) to the
421   /// LocIdx key / number for that location. There are always at least as many
422   /// as the number of registers on the target -- if the value in the register
423   /// is not being tracked, then the LocIdx value will be zero. New entries are
424   /// appended if a new spill slot begins being tracked.
425   /// This, and the corresponding reverse map persist for the analysis of the
426   /// whole function, and is necessarying for decoding various vectors of
427   /// values.
428   std::vector<LocIdx> LocIDToLocIdx;
429 
430   /// Inverse map of LocIDToLocIdx.
431   IndexedMap<unsigned, LocIdxToIndexFunctor> LocIdxToLocID;
432 
433   /// Unique-ification of spill slots. Used to number them -- their LocID
434   /// number is the index in SpillLocs minus one plus NumRegs.
435   UniqueVector<SpillLoc> SpillLocs;
436 
437   // If we discover a new machine location, assign it an mphi with this
438   // block number.
439   unsigned CurBB;
440 
441   /// Cached local copy of the number of registers the target has.
442   unsigned NumRegs;
443 
444   /// Collection of register mask operands that have been observed. Second part
445   /// of pair indicates the instruction that they happened in. Used to
446   /// reconstruct where defs happened if we start tracking a location later
447   /// on.
448   SmallVector<std::pair<const MachineOperand *, unsigned>, 32> Masks;
449 
450   /// Iterator for locations and the values they contain. Dereferencing
451   /// produces a struct/pair containing the LocIdx key for this location,
452   /// and a reference to the value currently stored. Simplifies the process
453   /// of seeking a particular location.
454   class MLocIterator {
455     LocToValueType &ValueMap;
456     LocIdx Idx;
457 
458   public:
459     class value_type {
460       public:
461       value_type(LocIdx Idx, ValueIDNum &Value) : Idx(Idx), Value(Value) { }
462       const LocIdx Idx;  /// Read-only index of this location.
463       ValueIDNum &Value; /// Reference to the stored value at this location.
464     };
465 
466     MLocIterator(LocToValueType &ValueMap, LocIdx Idx)
467       : ValueMap(ValueMap), Idx(Idx) { }
468 
469     bool operator==(const MLocIterator &Other) const {
470       assert(&ValueMap == &Other.ValueMap);
471       return Idx == Other.Idx;
472     }
473 
474     bool operator!=(const MLocIterator &Other) const {
475       return !(*this == Other);
476     }
477 
478     void operator++() {
479       Idx = LocIdx(Idx.asU64() + 1);
480     }
481 
482     value_type operator*() {
483       return value_type(Idx, ValueMap[LocIdx(Idx)]);
484     }
485   };
486 
487   MLocTracker(MachineFunction &MF, const TargetInstrInfo &TII,
488               const TargetRegisterInfo &TRI, const TargetLowering &TLI)
489       : MF(MF), TII(TII), TRI(TRI), TLI(TLI),
490         LocIdxToIDNum(ValueIDNum::EmptyValue),
491         LocIdxToLocID(0) {
492     NumRegs = TRI.getNumRegs();
493     reset();
494     LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc());
495     assert(NumRegs < (1u << NUM_LOC_BITS)); // Detect bit packing failure
496 
497     // Always track SP. This avoids the implicit clobbering caused by regmasks
498     // from affectings its values. (LiveDebugValues disbelieves calls and
499     // regmasks that claim to clobber SP).
500     unsigned SP = TLI.getStackPointerRegisterToSaveRestore();
501     if (SP) {
502       unsigned ID = getLocID(SP, false);
503       (void)lookupOrTrackRegister(ID);
504     }
505   }
506 
507   /// Produce location ID number for indexing LocIDToLocIdx. Takes the register
508   /// or spill number, and flag for whether it's a spill or not.
509   unsigned getLocID(unsigned RegOrSpill, bool isSpill) {
510     return (isSpill) ? RegOrSpill + NumRegs - 1 : RegOrSpill;
511   }
512 
513   /// Accessor for reading the value at Idx.
514   ValueIDNum getNumAtPos(LocIdx Idx) const {
515     assert(Idx.asU64() < LocIdxToIDNum.size());
516     return LocIdxToIDNum[Idx];
517   }
518 
519   unsigned getNumLocs(void) const { return LocIdxToIDNum.size(); }
520 
521   /// Reset all locations to contain a PHI value at the designated block. Used
522   /// sometimes for actual PHI values, othertimes to indicate the block entry
523   /// value (before any more information is known).
524   void setMPhis(unsigned NewCurBB) {
525     CurBB = NewCurBB;
526     for (auto Location : locations())
527       Location.Value = {CurBB, 0, Location.Idx};
528   }
529 
530   /// Load values for each location from array of ValueIDNums. Take current
531   /// bbnum just in case we read a value from a hitherto untouched register.
532   void loadFromArray(ValueIDNum *Locs, unsigned NewCurBB) {
533     CurBB = NewCurBB;
534     // Iterate over all tracked locations, and load each locations live-in
535     // value into our local index.
536     for (auto Location : locations())
537       Location.Value = Locs[Location.Idx.asU64()];
538   }
539 
540   /// Wipe any un-necessary location records after traversing a block.
541   void reset(void) {
542     // We could reset all the location values too; however either loadFromArray
543     // or setMPhis should be called before this object is re-used. Just
544     // clear Masks, they're definitely not needed.
545     Masks.clear();
546   }
547 
548   /// Clear all data. Destroys the LocID <=> LocIdx map, which makes most of
549   /// the information in this pass uninterpretable.
550   void clear(void) {
551     reset();
552     LocIDToLocIdx.clear();
553     LocIdxToLocID.clear();
554     LocIdxToIDNum.clear();
555     //SpillLocs.reset(); XXX UniqueVector::reset assumes a SpillLoc casts from 0
556     SpillLocs = decltype(SpillLocs)();
557 
558     LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc());
559   }
560 
561   /// Set a locaiton to a certain value.
562   void setMLoc(LocIdx L, ValueIDNum Num) {
563     assert(L.asU64() < LocIdxToIDNum.size());
564     LocIdxToIDNum[L] = Num;
565   }
566 
567   /// Create a LocIdx for an untracked register ID. Initialize it to either an
568   /// mphi value representing a live-in, or a recent register mask clobber.
569   LocIdx trackRegister(unsigned ID) {
570     assert(ID != 0);
571     LocIdx NewIdx = LocIdx(LocIdxToIDNum.size());
572     LocIdxToIDNum.grow(NewIdx);
573     LocIdxToLocID.grow(NewIdx);
574 
575     // Default: it's an mphi.
576     ValueIDNum ValNum = {CurBB, 0, NewIdx};
577     // Was this reg ever touched by a regmask?
578     for (const auto &MaskPair : reverse(Masks)) {
579       if (MaskPair.first->clobbersPhysReg(ID)) {
580         // There was an earlier def we skipped.
581         ValNum = {CurBB, MaskPair.second, NewIdx};
582         break;
583       }
584     }
585 
586     LocIdxToIDNum[NewIdx] = ValNum;
587     LocIdxToLocID[NewIdx] = ID;
588     return NewIdx;
589   }
590 
591   LocIdx lookupOrTrackRegister(unsigned ID) {
592     LocIdx &Index = LocIDToLocIdx[ID];
593     if (Index.isIllegal())
594       Index = trackRegister(ID);
595     return Index;
596   }
597 
598   /// Record a definition of the specified register at the given block / inst.
599   /// This doesn't take a ValueIDNum, because the definition and its location
600   /// are synonymous.
601   void defReg(Register R, unsigned BB, unsigned Inst) {
602     unsigned ID = getLocID(R, false);
603     LocIdx Idx = lookupOrTrackRegister(ID);
604     ValueIDNum ValueID = {BB, Inst, Idx};
605     LocIdxToIDNum[Idx] = ValueID;
606   }
607 
608   /// Set a register to a value number. To be used if the value number is
609   /// known in advance.
610   void setReg(Register R, ValueIDNum ValueID) {
611     unsigned ID = getLocID(R, false);
612     LocIdx Idx = lookupOrTrackRegister(ID);
613     LocIdxToIDNum[Idx] = ValueID;
614   }
615 
616   ValueIDNum readReg(Register R) {
617     unsigned ID = getLocID(R, false);
618     LocIdx Idx = lookupOrTrackRegister(ID);
619     return LocIdxToIDNum[Idx];
620   }
621 
622   /// Reset a register value to zero / empty. Needed to replicate the
623   /// VarLoc implementation where a copy to/from a register effectively
624   /// clears the contents of the source register. (Values can only have one
625   ///  machine location in VarLocBasedImpl).
626   void wipeRegister(Register R) {
627     unsigned ID = getLocID(R, false);
628     LocIdx Idx = LocIDToLocIdx[ID];
629     LocIdxToIDNum[Idx] = ValueIDNum::EmptyValue;
630   }
631 
632   /// Determine the LocIdx of an existing register.
633   LocIdx getRegMLoc(Register R) {
634     unsigned ID = getLocID(R, false);
635     return LocIDToLocIdx[ID];
636   }
637 
638   /// Record a RegMask operand being executed. Defs any register we currently
639   /// track, stores a pointer to the mask in case we have to account for it
640   /// later.
641   void writeRegMask(const MachineOperand *MO, unsigned CurBB, unsigned InstID) {
642     // Ensure SP exists, so that we don't override it later.
643     unsigned SP = TLI.getStackPointerRegisterToSaveRestore();
644 
645     // Def any register we track have that isn't preserved. The regmask
646     // terminates the liveness of a register, meaning its value can't be
647     // relied upon -- we represent this by giving it a new value.
648     for (auto Location : locations()) {
649       unsigned ID = LocIdxToLocID[Location.Idx];
650       // Don't clobber SP, even if the mask says it's clobbered.
651       if (ID < NumRegs && ID != SP && MO->clobbersPhysReg(ID))
652         defReg(ID, CurBB, InstID);
653     }
654     Masks.push_back(std::make_pair(MO, InstID));
655   }
656 
657   /// Find LocIdx for SpillLoc \p L, creating a new one if it's not tracked.
658   LocIdx getOrTrackSpillLoc(SpillLoc L) {
659     unsigned SpillID = SpillLocs.idFor(L);
660     if (SpillID == 0) {
661       SpillID = SpillLocs.insert(L);
662       unsigned L = getLocID(SpillID, true);
663       LocIdx Idx = LocIdx(LocIdxToIDNum.size()); // New idx
664       LocIdxToIDNum.grow(Idx);
665       LocIdxToLocID.grow(Idx);
666       LocIDToLocIdx.push_back(Idx);
667       LocIdxToLocID[Idx] = L;
668       return Idx;
669     } else {
670       unsigned L = getLocID(SpillID, true);
671       LocIdx Idx = LocIDToLocIdx[L];
672       return Idx;
673     }
674   }
675 
676   /// Set the value stored in a spill slot.
677   void setSpill(SpillLoc L, ValueIDNum ValueID) {
678     LocIdx Idx = getOrTrackSpillLoc(L);
679     LocIdxToIDNum[Idx] = ValueID;
680   }
681 
682   /// Read whatever value is in a spill slot, or None if it isn't tracked.
683   Optional<ValueIDNum> readSpill(SpillLoc L) {
684     unsigned SpillID = SpillLocs.idFor(L);
685     if (SpillID == 0)
686       return None;
687 
688     unsigned LocID = getLocID(SpillID, true);
689     LocIdx Idx = LocIDToLocIdx[LocID];
690     return LocIdxToIDNum[Idx];
691   }
692 
693   /// Determine the LocIdx of a spill slot. Return None if it previously
694   /// hasn't had a value assigned.
695   Optional<LocIdx> getSpillMLoc(SpillLoc L) {
696     unsigned SpillID = SpillLocs.idFor(L);
697     if (SpillID == 0)
698       return None;
699     unsigned LocNo = getLocID(SpillID, true);
700     return LocIDToLocIdx[LocNo];
701   }
702 
703   /// Return true if Idx is a spill machine location.
704   bool isSpill(LocIdx Idx) const {
705     return LocIdxToLocID[Idx] >= NumRegs;
706   }
707 
708   MLocIterator begin() {
709     return MLocIterator(LocIdxToIDNum, 0);
710   }
711 
712   MLocIterator end() {
713     return MLocIterator(LocIdxToIDNum, LocIdxToIDNum.size());
714   }
715 
716   /// Return a range over all locations currently tracked.
717   iterator_range<MLocIterator> locations() {
718     return llvm::make_range(begin(), end());
719   }
720 
721   std::string LocIdxToName(LocIdx Idx) const {
722     unsigned ID = LocIdxToLocID[Idx];
723     if (ID >= NumRegs)
724       return Twine("slot ").concat(Twine(ID - NumRegs)).str();
725     else
726       return TRI.getRegAsmName(ID).str();
727   }
728 
729   std::string IDAsString(const ValueIDNum &Num) const {
730     std::string DefName = LocIdxToName(Num.getLoc());
731     return Num.asString(DefName);
732   }
733 
734   LLVM_DUMP_METHOD
735   void dump() {
736     for (auto Location : locations()) {
737       std::string MLocName = LocIdxToName(Location.Value.getLoc());
738       std::string DefName = Location.Value.asString(MLocName);
739       dbgs() << LocIdxToName(Location.Idx) << " --> " << DefName << "\n";
740     }
741   }
742 
743   LLVM_DUMP_METHOD
744   void dump_mloc_map() {
745     for (auto Location : locations()) {
746       std::string foo = LocIdxToName(Location.Idx);
747       dbgs() << "Idx " << Location.Idx.asU64() << " " << foo << "\n";
748     }
749   }
750 
751   /// Create a DBG_VALUE based on  machine location \p MLoc. Qualify it with the
752   /// information in \pProperties, for variable Var. Don't insert it anywhere,
753   /// just return the builder for it.
754   MachineInstrBuilder emitLoc(Optional<LocIdx> MLoc, const DebugVariable &Var,
755                               const DbgValueProperties &Properties) {
756     DebugLoc DL =
757         DebugLoc::get(0, 0, Var.getVariable()->getScope(), Var.getInlinedAt());
758     auto MIB = BuildMI(MF, DL, TII.get(TargetOpcode::DBG_VALUE));
759 
760     const DIExpression *Expr = Properties.DIExpr;
761     if (!MLoc) {
762       // No location -> DBG_VALUE $noreg
763       MIB.addReg(0, RegState::Debug);
764       MIB.addReg(0, RegState::Debug);
765     } else if (LocIdxToLocID[*MLoc] >= NumRegs) {
766       unsigned LocID = LocIdxToLocID[*MLoc];
767       const SpillLoc &Spill = SpillLocs[LocID - NumRegs + 1];
768       Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset,
769                                    Spill.SpillOffset);
770       unsigned Base = Spill.SpillBase;
771       MIB.addReg(Base, RegState::Debug);
772       MIB.addImm(0);
773     } else {
774       unsigned LocID = LocIdxToLocID[*MLoc];
775       MIB.addReg(LocID, RegState::Debug);
776       if (Properties.Indirect)
777         MIB.addImm(0);
778       else
779         MIB.addReg(0, RegState::Debug);
780     }
781 
782     MIB.addMetadata(Var.getVariable());
783     MIB.addMetadata(Expr);
784     return MIB;
785   }
786 };
787 
788 /// Class recording the (high level) _value_ of a variable. Identifies either
789 /// the value of the variable as a ValueIDNum, or a constant MachineOperand.
790 /// This class also stores meta-information about how the value is qualified.
791 /// Used to reason about variable values when performing the second
792 /// (DebugVariable specific) dataflow analysis.
793 class DbgValue {
794 public:
795   union {
796     /// If Kind is Def, the value number that this value is based on.
797     ValueIDNum ID;
798     /// If Kind is Const, the MachineOperand defining this value.
799     MachineOperand MO;
800     /// For a NoVal DbgValue, which block it was generated in.
801     unsigned BlockNo;
802   };
803   /// Qualifiers for the ValueIDNum above.
804   DbgValueProperties Properties;
805 
806   typedef enum {
807     Undef,     // Represents a DBG_VALUE $noreg in the transfer function only.
808     Def,       // This value is defined by an inst, or is a PHI value.
809     Const,     // A constant value contained in the MachineOperand field.
810     Proposed,  // This is a tentative PHI value, which may be confirmed or
811                // invalidated later.
812     NoVal      // Empty DbgValue, generated during dataflow. BlockNo stores
813                // which block this was generated in.
814    } KindT;
815   /// Discriminator for whether this is a constant or an in-program value.
816   KindT Kind;
817 
818   DbgValue(const ValueIDNum &Val, const DbgValueProperties &Prop, KindT Kind)
819     : ID(Val), Properties(Prop), Kind(Kind) {
820     assert(Kind == Def || Kind == Proposed);
821   }
822 
823   DbgValue(unsigned BlockNo, const DbgValueProperties &Prop, KindT Kind)
824     : BlockNo(BlockNo), Properties(Prop), Kind(Kind) {
825     assert(Kind == NoVal);
826   }
827 
828   DbgValue(const MachineOperand &MO, const DbgValueProperties &Prop, KindT Kind)
829     : MO(MO), Properties(Prop), Kind(Kind) {
830     assert(Kind == Const);
831   }
832 
833   DbgValue(const DbgValueProperties &Prop, KindT Kind)
834     : Properties(Prop), Kind(Kind) {
835     assert(Kind == Undef &&
836            "Empty DbgValue constructor must pass in Undef kind");
837   }
838 
839   void dump(const MLocTracker *MTrack) const {
840     if (Kind == Const) {
841       MO.dump();
842     } else if (Kind == NoVal) {
843       dbgs() << "NoVal(" << BlockNo << ")";
844     } else if (Kind == Proposed) {
845       dbgs() << "VPHI(" << MTrack->IDAsString(ID) << ")";
846     } else {
847       assert(Kind == Def);
848       dbgs() << MTrack->IDAsString(ID);
849     }
850     if (Properties.Indirect)
851       dbgs() << " indir";
852     if (Properties.DIExpr)
853       dbgs() << " " << *Properties.DIExpr;
854   }
855 
856   bool operator==(const DbgValue &Other) const {
857     if (std::tie(Kind, Properties) != std::tie(Other.Kind, Other.Properties))
858       return false;
859     else if (Kind == Proposed && ID != Other.ID)
860       return false;
861     else if (Kind == Def && ID != Other.ID)
862       return false;
863     else if (Kind == NoVal && BlockNo != Other.BlockNo)
864       return false;
865     else if (Kind == Const)
866       return MO.isIdenticalTo(Other.MO);
867 
868     return true;
869   }
870 
871   bool operator!=(const DbgValue &Other) const { return !(*this == Other); }
872 };
873 
874 /// Types for recording sets of variable fragments that overlap. For a given
875 /// local variable, we record all other fragments of that variable that could
876 /// overlap it, to reduce search time.
877 using FragmentOfVar =
878     std::pair<const DILocalVariable *, DIExpression::FragmentInfo>;
879 using OverlapMap =
880     DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>;
881 
882 /// Collection of DBG_VALUEs observed when traversing a block. Records each
883 /// variable and the value the DBG_VALUE refers to. Requires the machine value
884 /// location dataflow algorithm to have run already, so that values can be
885 /// identified.
886 class VLocTracker {
887 public:
888   /// Map DebugVariable to the latest Value it's defined to have.
889   /// Needs to be a MapVector because we determine order-in-the-input-MIR from
890   /// the order in this container.
891   /// We only retain the last DbgValue in each block for each variable, to
892   /// determine the blocks live-out variable value. The Vars container forms the
893   /// transfer function for this block, as part of the dataflow analysis. The
894   /// movement of values between locations inside of a block is handled at a
895   /// much later stage, in the TransferTracker class.
896   MapVector<DebugVariable, DbgValue> Vars;
897   DenseMap<DebugVariable, const DILocation *> Scopes;
898   MachineBasicBlock *MBB;
899 
900 public:
901   VLocTracker() {}
902 
903   void defVar(const MachineInstr &MI, Optional<ValueIDNum> ID) {
904     // XXX skipping overlapping fragments for now.
905     assert(MI.isDebugValue());
906     DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
907                       MI.getDebugLoc()->getInlinedAt());
908     DbgValueProperties Properties(MI);
909     DbgValue Rec = (ID) ? DbgValue(*ID, Properties, DbgValue::Def)
910                         : DbgValue(Properties, DbgValue::Undef);
911 
912     // Attempt insertion; overwrite if it's already mapped.
913     auto Result = Vars.insert(std::make_pair(Var, Rec));
914     if (!Result.second)
915       Result.first->second = Rec;
916     Scopes[Var] = MI.getDebugLoc().get();
917   }
918 
919   void defVar(const MachineInstr &MI, const MachineOperand &MO) {
920     // XXX skipping overlapping fragments for now.
921     assert(MI.isDebugValue());
922     DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
923                       MI.getDebugLoc()->getInlinedAt());
924     DbgValueProperties Properties(MI);
925     DbgValue Rec = DbgValue(MO, Properties, DbgValue::Const);
926 
927     // Attempt insertion; overwrite if it's already mapped.
928     auto Result = Vars.insert(std::make_pair(Var, Rec));
929     if (!Result.second)
930       Result.first->second = Rec;
931     Scopes[Var] = MI.getDebugLoc().get();
932   }
933 };
934 
935 /// Tracker for converting machine value locations and variable values into
936 /// variable locations (the output of LiveDebugValues), recorded as DBG_VALUEs
937 /// specifying block live-in locations and transfers within blocks.
938 ///
939 /// Operating on a per-block basis, this class takes a (pre-loaded) MLocTracker
940 /// and must be initialized with the set of variable values that are live-in to
941 /// the block. The caller then repeatedly calls process(). TransferTracker picks
942 /// out variable locations for the live-in variable values (if there _is_ a
943 /// location) and creates the corresponding DBG_VALUEs. Then, as the block is
944 /// stepped through, transfers of values between machine locations are
945 /// identified and if profitable, a DBG_VALUE created.
946 ///
947 /// This is where debug use-before-defs would be resolved: a variable with an
948 /// unavailable value could materialize in the middle of a block, when the
949 /// value becomes available. Or, we could detect clobbers and re-specify the
950 /// variable in a backup location. (XXX these are unimplemented).
951 class TransferTracker {
952 public:
953   const TargetInstrInfo *TII;
954   /// This machine location tracker is assumed to always contain the up-to-date
955   /// value mapping for all machine locations. TransferTracker only reads
956   /// information from it. (XXX make it const?)
957   MLocTracker *MTracker;
958   MachineFunction &MF;
959 
960   /// Record of all changes in variable locations at a block position. Awkwardly
961   /// we allow inserting either before or after the point: MBB != nullptr
962   /// indicates it's before, otherwise after.
963   struct Transfer {
964     MachineBasicBlock::iterator Pos; /// Position to insert DBG_VALUes
965     MachineBasicBlock *MBB;          /// non-null if we should insert after.
966     SmallVector<MachineInstr *, 4> Insts; /// Vector of DBG_VALUEs to insert.
967   };
968 
969   typedef struct {
970     LocIdx Loc;
971     DbgValueProperties Properties;
972   } LocAndProperties;
973 
974   /// Collection of transfers (DBG_VALUEs) to be inserted.
975   SmallVector<Transfer, 32> Transfers;
976 
977   /// Local cache of what-value-is-in-what-LocIdx. Used to identify differences
978   /// between TransferTrackers view of variable locations and MLocTrackers. For
979   /// example, MLocTracker observes all clobbers, but TransferTracker lazily
980   /// does not.
981   std::vector<ValueIDNum> VarLocs;
982 
983   /// Map from LocIdxes to which DebugVariables are based that location.
984   /// Mantained while stepping through the block. Not accurate if
985   /// VarLocs[Idx] != MTracker->LocIdxToIDNum[Idx].
986   std::map<LocIdx, SmallSet<DebugVariable, 4>> ActiveMLocs;
987 
988   /// Map from DebugVariable to it's current location and qualifying meta
989   /// information. To be used in conjunction with ActiveMLocs to construct
990   /// enough information for the DBG_VALUEs for a particular LocIdx.
991   DenseMap<DebugVariable, LocAndProperties> ActiveVLocs;
992 
993   /// Temporary cache of DBG_VALUEs to be entered into the Transfers collection.
994   SmallVector<MachineInstr *, 4> PendingDbgValues;
995 
996   const TargetRegisterInfo &TRI;
997   const BitVector &CalleeSavedRegs;
998 
999   TransferTracker(const TargetInstrInfo *TII, MLocTracker *MTracker,
1000                   MachineFunction &MF, const TargetRegisterInfo &TRI,
1001                   const BitVector &CalleeSavedRegs)
1002       : TII(TII), MTracker(MTracker), MF(MF), TRI(TRI),
1003         CalleeSavedRegs(CalleeSavedRegs) {}
1004 
1005   /// Load object with live-in variable values. \p mlocs contains the live-in
1006   /// values in each machine location, while \p vlocs the live-in variable
1007   /// values. This method picks variable locations for the live-in variables,
1008   /// creates DBG_VALUEs and puts them in #Transfers, then prepares the other
1009   /// object fields to track variable locations as we step through the block.
1010   /// FIXME: could just examine mloctracker instead of passing in \p mlocs?
1011   void loadInlocs(MachineBasicBlock &MBB, ValueIDNum *MLocs,
1012                   SmallVectorImpl<std::pair<DebugVariable, DbgValue>> &VLocs,
1013                   unsigned NumLocs) {
1014     ActiveMLocs.clear();
1015     ActiveVLocs.clear();
1016     VarLocs.clear();
1017     VarLocs.reserve(NumLocs);
1018 
1019     auto isCalleeSaved = [&](LocIdx L) {
1020       unsigned Reg = MTracker->LocIdxToLocID[L];
1021       if (Reg >= MTracker->NumRegs)
1022         return false;
1023       for (MCRegAliasIterator RAI(Reg, &TRI, true); RAI.isValid(); ++RAI)
1024         if (CalleeSavedRegs.test(*RAI))
1025           return true;
1026       return false;
1027     };
1028 
1029     // Map of the preferred location for each value.
1030     std::map<ValueIDNum, LocIdx> ValueToLoc;
1031 
1032     // Produce a map of value numbers to the current machine locs they live
1033     // in. When emulating VarLocBasedImpl, there should only be one
1034     // location; when not, we get to pick.
1035     for (auto Location : MTracker->locations()) {
1036       LocIdx Idx = Location.Idx;
1037       ValueIDNum &VNum = MLocs[Idx.asU64()];
1038       VarLocs.push_back(VNum);
1039       auto it = ValueToLoc.find(VNum);
1040       // In order of preference, pick:
1041       //  * Callee saved registers,
1042       //  * Other registers,
1043       //  * Spill slots.
1044       if (it == ValueToLoc.end() || MTracker->isSpill(it->second) ||
1045           (!isCalleeSaved(it->second) && isCalleeSaved(Idx.asU64()))) {
1046         // Insert, or overwrite if insertion failed.
1047         auto PrefLocRes = ValueToLoc.insert(std::make_pair(VNum, Idx));
1048         if (!PrefLocRes.second)
1049           PrefLocRes.first->second = Idx;
1050       }
1051     }
1052 
1053     // Now map variables to their picked LocIdxes.
1054     for (auto Var : VLocs) {
1055       if (Var.second.Kind == DbgValue::Const) {
1056         PendingDbgValues.push_back(
1057             emitMOLoc(Var.second.MO, Var.first, Var.second.Properties));
1058         continue;
1059       }
1060 
1061       // If the value has no location, we can't make a variable location.
1062       auto ValuesPreferredLoc = ValueToLoc.find(Var.second.ID);
1063       if (ValuesPreferredLoc == ValueToLoc.end())
1064         continue;
1065 
1066       LocIdx M = ValuesPreferredLoc->second;
1067       auto NewValue = LocAndProperties{M, Var.second.Properties};
1068       auto Result = ActiveVLocs.insert(std::make_pair(Var.first, NewValue));
1069       if (!Result.second)
1070         Result.first->second = NewValue;
1071       ActiveMLocs[M].insert(Var.first);
1072       PendingDbgValues.push_back(
1073           MTracker->emitLoc(M, Var.first, Var.second.Properties));
1074     }
1075     flushDbgValues(MBB.begin(), &MBB);
1076   }
1077 
1078   /// Helper to move created DBG_VALUEs into Transfers collection.
1079   void flushDbgValues(MachineBasicBlock::iterator Pos, MachineBasicBlock *MBB) {
1080     if (PendingDbgValues.size() > 0) {
1081       Transfers.push_back({Pos, MBB, PendingDbgValues});
1082       PendingDbgValues.clear();
1083     }
1084   }
1085 
1086   /// Handle a DBG_VALUE within a block. Terminate the variables current
1087   /// location, and record the value its DBG_VALUE refers to, so that we can
1088   /// detect location transfers later on.
1089   void redefVar(const MachineInstr &MI) {
1090     DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
1091                       MI.getDebugLoc()->getInlinedAt());
1092     const MachineOperand &MO = MI.getOperand(0);
1093 
1094     // Erase any previous location,
1095     auto It = ActiveVLocs.find(Var);
1096     if (It != ActiveVLocs.end()) {
1097       ActiveMLocs[It->second.Loc].erase(Var);
1098     }
1099 
1100     // Insert a new variable location. Ignore non-register locations, we don't
1101     // transfer those, and can't currently describe spill locs independently of
1102     // regs.
1103     // (This is because a spill location is a DBG_VALUE of the stack pointer).
1104     if (!MO.isReg() || MO.getReg() == 0) {
1105       if (It != ActiveVLocs.end())
1106         ActiveVLocs.erase(It);
1107       return;
1108     }
1109 
1110     Register Reg = MO.getReg();
1111     LocIdx MLoc = MTracker->getRegMLoc(Reg);
1112     DbgValueProperties Properties(MI);
1113 
1114     // Check whether our local copy of values-by-location in #VarLocs is out of
1115     // date. Wipe old tracking data for the location if it's been clobbered in
1116     // the meantime.
1117     if (MTracker->getNumAtPos(MLoc) != VarLocs[MLoc.asU64()]) {
1118       for (auto &P : ActiveMLocs[MLoc.asU64()]) {
1119         ActiveVLocs.erase(P);
1120       }
1121       ActiveMLocs[MLoc].clear();
1122       VarLocs[MLoc.asU64()] = MTracker->getNumAtPos(MLoc);
1123     }
1124 
1125     ActiveMLocs[MLoc].insert(Var);
1126     if (It == ActiveVLocs.end()) {
1127       ActiveVLocs.insert(std::make_pair(Var, LocAndProperties{MLoc, Properties}));
1128     } else {
1129       It->second.Loc = MLoc;
1130       It->second.Properties = Properties;
1131     }
1132   }
1133 
1134   /// Explicitly terminate variable locations based on \p mloc. Creates undef
1135   /// DBG_VALUEs for any variables that were located there, and clears
1136   /// #ActiveMLoc / #ActiveVLoc tracking information for that location.
1137   void clobberMloc(LocIdx MLoc, MachineBasicBlock::iterator Pos) {
1138     assert(MTracker->isSpill(MLoc));
1139     auto ActiveMLocIt = ActiveMLocs.find(MLoc);
1140     if (ActiveMLocIt == ActiveMLocs.end())
1141       return;
1142 
1143     VarLocs[MLoc.asU64()] = ValueIDNum::EmptyValue;
1144 
1145     for (auto &Var : ActiveMLocIt->second) {
1146       auto ActiveVLocIt = ActiveVLocs.find(Var);
1147       // Create an undef. We can't feed in a nullptr DIExpression alas,
1148       // so use the variables last expression. Pass None as the location.
1149       const DIExpression *Expr = ActiveVLocIt->second.Properties.DIExpr;
1150       DbgValueProperties Properties(Expr, false);
1151       PendingDbgValues.push_back(MTracker->emitLoc(None, Var, Properties));
1152       ActiveVLocs.erase(ActiveVLocIt);
1153     }
1154     flushDbgValues(Pos, nullptr);
1155 
1156     ActiveMLocIt->second.clear();
1157   }
1158 
1159   /// Transfer variables based on \p Src to be based on \p Dst. This handles
1160   /// both register copies as well as spills and restores. Creates DBG_VALUEs
1161   /// describing the movement.
1162   void transferMlocs(LocIdx Src, LocIdx Dst, MachineBasicBlock::iterator Pos) {
1163     // Does Src still contain the value num we expect? If not, it's been
1164     // clobbered in the meantime, and our variable locations are stale.
1165     if (VarLocs[Src.asU64()] != MTracker->getNumAtPos(Src))
1166       return;
1167 
1168     // assert(ActiveMLocs[Dst].size() == 0);
1169     //^^^ Legitimate scenario on account of un-clobbered slot being assigned to?
1170     ActiveMLocs[Dst] = ActiveMLocs[Src];
1171     VarLocs[Dst.asU64()] = VarLocs[Src.asU64()];
1172 
1173     // For each variable based on Src; create a location at Dst.
1174     for (auto &Var : ActiveMLocs[Src]) {
1175       auto ActiveVLocIt = ActiveVLocs.find(Var);
1176       assert(ActiveVLocIt != ActiveVLocs.end());
1177       ActiveVLocIt->second.Loc = Dst;
1178 
1179       assert(Dst != 0);
1180       MachineInstr *MI =
1181           MTracker->emitLoc(Dst, Var, ActiveVLocIt->second.Properties);
1182       PendingDbgValues.push_back(MI);
1183     }
1184     ActiveMLocs[Src].clear();
1185     flushDbgValues(Pos, nullptr);
1186 
1187     // XXX XXX XXX "pretend to be old LDV" means dropping all tracking data
1188     // about the old location.
1189     if (EmulateOldLDV)
1190       VarLocs[Src.asU64()] = ValueIDNum::EmptyValue;
1191   }
1192 
1193   MachineInstrBuilder emitMOLoc(const MachineOperand &MO,
1194                                 const DebugVariable &Var,
1195                                 const DbgValueProperties &Properties) {
1196     DebugLoc DL =
1197         DebugLoc::get(0, 0, Var.getVariable()->getScope(), Var.getInlinedAt());
1198     auto MIB = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE));
1199     MIB.add(MO);
1200     if (Properties.Indirect)
1201       MIB.addImm(0);
1202     else
1203       MIB.addReg(0);
1204     MIB.addMetadata(Var.getVariable());
1205     MIB.addMetadata(Properties.DIExpr);
1206     return MIB;
1207   }
1208 };
1209 
1210 class InstrRefBasedLDV : public LDVImpl {
1211 private:
1212   using FragmentInfo = DIExpression::FragmentInfo;
1213   using OptFragmentInfo = Optional<DIExpression::FragmentInfo>;
1214 
1215   // Helper while building OverlapMap, a map of all fragments seen for a given
1216   // DILocalVariable.
1217   using VarToFragments =
1218       DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>;
1219 
1220   /// Machine location/value transfer function, a mapping of which locations
1221   // are assigned which new values.
1222   typedef std::map<LocIdx, ValueIDNum> MLocTransferMap;
1223 
1224   /// Live in/out structure for the variable values: a per-block map of
1225   /// variables to their values. XXX, better name?
1226   typedef DenseMap<const MachineBasicBlock *,
1227                    DenseMap<DebugVariable, DbgValue> *>
1228       LiveIdxT;
1229 
1230   typedef std::pair<DebugVariable, DbgValue> VarAndLoc;
1231 
1232   /// Type for a live-in value: the predecessor block, and its value.
1233   typedef std::pair<MachineBasicBlock *, DbgValue *> InValueT;
1234 
1235   /// Vector (per block) of a collection (inner smallvector) of live-ins.
1236   /// Used as the result type for the variable value dataflow problem.
1237   typedef SmallVector<SmallVector<VarAndLoc, 8>, 8> LiveInsT;
1238 
1239   const TargetRegisterInfo *TRI;
1240   const TargetInstrInfo *TII;
1241   const TargetFrameLowering *TFI;
1242   BitVector CalleeSavedRegs;
1243   LexicalScopes LS;
1244   TargetPassConfig *TPC;
1245 
1246   /// Object to track machine locations as we step through a block. Could
1247   /// probably be a field rather than a pointer, as it's always used.
1248   MLocTracker *MTracker;
1249 
1250   /// Number of the current block LiveDebugValues is stepping through.
1251   unsigned CurBB;
1252 
1253   /// Number of the current instruction LiveDebugValues is evaluating.
1254   unsigned CurInst;
1255 
1256   /// Variable tracker -- listens to DBG_VALUEs occurring as InstrRefBasedImpl
1257   /// steps through a block. Reads the values at each location from the
1258   /// MLocTracker object.
1259   VLocTracker *VTracker;
1260 
1261   /// Tracker for transfers, listens to DBG_VALUEs and transfers of values
1262   /// between locations during stepping, creates new DBG_VALUEs when values move
1263   /// location.
1264   TransferTracker *TTracker;
1265 
1266   /// Blocks which are artificial, i.e. blocks which exclusively contain
1267   /// instructions without DebugLocs, or with line 0 locations.
1268   SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks;
1269 
1270   // Mapping of blocks to and from their RPOT order.
1271   DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
1272   DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
1273   DenseMap<unsigned, unsigned> BBNumToRPO;
1274 
1275   // Map of overlapping variable fragments.
1276   OverlapMap OverlapFragments;
1277   VarToFragments SeenFragments;
1278 
1279   /// Tests whether this instruction is a spill to a stack slot.
1280   bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF);
1281 
1282   /// Decide if @MI is a spill instruction and return true if it is. We use 2
1283   /// criteria to make this decision:
1284   /// - Is this instruction a store to a spill slot?
1285   /// - Is there a register operand that is both used and killed?
1286   /// TODO: Store optimization can fold spills into other stores (including
1287   /// other spills). We do not handle this yet (more than one memory operand).
1288   bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF,
1289                        unsigned &Reg);
1290 
1291   /// If a given instruction is identified as a spill, return the spill slot
1292   /// and set \p Reg to the spilled register.
1293   Optional<SpillLoc> isRestoreInstruction(const MachineInstr &MI,
1294                                           MachineFunction *MF, unsigned &Reg);
1295 
1296   /// Given a spill instruction, extract the register and offset used to
1297   /// address the spill slot in a target independent way.
1298   SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI);
1299 
1300   /// Observe a single instruction while stepping through a block.
1301   void process(MachineInstr &MI);
1302 
1303   /// Examines whether \p MI is a DBG_VALUE and notifies trackers.
1304   /// \returns true if MI was recognized and processed.
1305   bool transferDebugValue(const MachineInstr &MI);
1306 
1307   /// Examines whether \p MI is copy instruction, and notifies trackers.
1308   /// \returns true if MI was recognized and processed.
1309   bool transferRegisterCopy(MachineInstr &MI);
1310 
1311   /// Examines whether \p MI is stack spill or restore  instruction, and
1312   /// notifies trackers. \returns true if MI was recognized and processed.
1313   bool transferSpillOrRestoreInst(MachineInstr &MI);
1314 
1315   /// Examines \p MI for any registers that it defines, and notifies trackers.
1316   void transferRegisterDef(MachineInstr &MI);
1317 
1318   /// Copy one location to the other, accounting for movement of subregisters
1319   /// too.
1320   void performCopy(Register Src, Register Dst);
1321 
1322   void accumulateFragmentMap(MachineInstr &MI);
1323 
1324   /// Step through the function, recording register definitions and movements
1325   /// in an MLocTracker. Convert the observations into a per-block transfer
1326   /// function in \p MLocTransfer, suitable for using with the machine value
1327   /// location dataflow problem. Do the same with VLoc trackers in \p VLocs,
1328   /// although the precise machine value numbers can't be known until after
1329   /// the machine value number problem is solved.
1330   void produceTransferFunctions(MachineFunction &MF,
1331                                 SmallVectorImpl<MLocTransferMap> &MLocTransfer,
1332                                 unsigned MaxNumBlocks,
1333                                 SmallVectorImpl<VLocTracker> &VLocs);
1334 
1335   /// Solve the machine value location dataflow problem. Takes as input the
1336   /// transfer functions in \p MLocTransfer. Writes the output live-in and
1337   /// live-out arrays to the (initialized to zero) multidimensional arrays in
1338   /// \p MInLocs and \p MOutLocs. The outer dimension is indexed by block
1339   /// number, the inner by LocIdx.
1340   void mlocDataflow(ValueIDNum **MInLocs, ValueIDNum **MOutLocs,
1341                     SmallVectorImpl<MLocTransferMap> &MLocTransfer);
1342 
1343   /// Perform a control flow join (lattice value meet) of the values in machine
1344   /// locations at \p MBB. Follows the algorithm described in the file-comment,
1345   /// reading live-outs of predecessors from \p OutLocs, the current live ins
1346   /// from \p InLocs, and assigning the newly computed live ins back into
1347   /// \p InLocs. \returns two bools -- the first indicates whether a change
1348   /// was made, the second whether a lattice downgrade occurred. If the latter
1349   /// is true, revisiting this block is necessary.
1350   std::tuple<bool, bool>
1351   mlocJoin(MachineBasicBlock &MBB,
1352            SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
1353            ValueIDNum **OutLocs, ValueIDNum *InLocs);
1354 
1355   /// Solve the variable value dataflow problem, for a single lexical scope.
1356   /// Uses the algorithm from the file comment to resolve control flow joins,
1357   /// although there are extra hacks, see vlocJoin. Reads the
1358   /// locations of values from the \p MInLocs and \p MOutLocs arrays (see
1359   /// mlocDataflow) and reads the variable values transfer function from
1360   /// \p AllTheVlocs. Live-in and Live-out variable values are stored locally,
1361   /// with the live-ins permanently stored to \p Output once the fixedpoint is
1362   /// reached.
1363   /// \p VarsWeCareAbout contains a collection of the variables in \p Scope
1364   /// that we should be tracking.
1365   /// \p AssignBlocks contains the set of blocks that aren't in \p Scope, but
1366   /// which do contain DBG_VALUEs, which VarLocBasedImpl tracks locations
1367   /// through.
1368   void vlocDataflow(const LexicalScope *Scope, const DILocation *DILoc,
1369                     const SmallSet<DebugVariable, 4> &VarsWeCareAbout,
1370                     SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks,
1371                     LiveInsT &Output, ValueIDNum **MOutLocs,
1372                     ValueIDNum **MInLocs,
1373                     SmallVectorImpl<VLocTracker> &AllTheVLocs);
1374 
1375   /// Compute the live-ins to a block, considering control flow merges according
1376   /// to the method in the file comment. Live out and live in variable values
1377   /// are stored in \p VLOCOutLocs and \p VLOCInLocs. The live-ins for \p MBB
1378   /// are computed and stored into \p VLOCInLocs. \returns true if the live-ins
1379   /// are modified.
1380   /// \p InLocsT Output argument, storage for calculated live-ins.
1381   /// \returns two bools -- the first indicates whether a change
1382   /// was made, the second whether a lattice downgrade occurred. If the latter
1383   /// is true, revisiting this block is necessary.
1384   std::tuple<bool, bool>
1385   vlocJoin(MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs, LiveIdxT &VLOCInLocs,
1386            SmallPtrSet<const MachineBasicBlock *, 16> *VLOCVisited,
1387            unsigned BBNum, const SmallSet<DebugVariable, 4> &AllVars,
1388            ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
1389            SmallPtrSet<const MachineBasicBlock *, 8> &InScopeBlocks,
1390            SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore,
1391            DenseMap<DebugVariable, DbgValue> &InLocsT);
1392 
1393   /// Continue exploration of the variable-value lattice, as explained in the
1394   /// file-level comment. \p OldLiveInLocation contains the current
1395   /// exploration position, from which we need to descend further. \p Values
1396   /// contains the set of live-in values, \p CurBlockRPONum the RPO number of
1397   /// the current block, and \p CandidateLocations a set of locations that
1398   /// should be considered as PHI locations, if we reach the bottom of the
1399   /// lattice. \returns true if we should downgrade; the value is the agreeing
1400   /// value number in a non-backedge predecessor.
1401   bool vlocDowngradeLattice(const MachineBasicBlock &MBB,
1402                             const DbgValue &OldLiveInLocation,
1403                             const SmallVectorImpl<InValueT> &Values,
1404                             unsigned CurBlockRPONum);
1405 
1406   /// For the given block and live-outs feeding into it, try to find a
1407   /// machine location where they all join. If a solution for all predecessors
1408   /// can't be found, a location where all non-backedge-predecessors join
1409   /// will be returned instead. While this method finds a join location, this
1410   /// says nothing as to whether it should be used.
1411   /// \returns Pair of value ID if found, and true when the correct value
1412   /// is available on all predecessor edges, or false if it's only available
1413   /// for non-backedge predecessors.
1414   std::tuple<Optional<ValueIDNum>, bool>
1415   pickVPHILoc(MachineBasicBlock &MBB, const DebugVariable &Var,
1416               const LiveIdxT &LiveOuts, ValueIDNum **MOutLocs,
1417               ValueIDNum **MInLocs,
1418               const SmallVectorImpl<MachineBasicBlock *> &BlockOrders);
1419 
1420   /// Given the solutions to the two dataflow problems, machine value locations
1421   /// in \p MInLocs and live-in variable values in \p SavedLiveIns, runs the
1422   /// TransferTracker class over the function to produce live-in and transfer
1423   /// DBG_VALUEs, then inserts them. Groups of DBG_VALUEs are inserted in the
1424   /// order given by AllVarsNumbering -- this could be any stable order, but
1425   /// right now "order of appearence in function, when explored in RPO", so
1426   /// that we can compare explictly against VarLocBasedImpl.
1427   void emitLocations(MachineFunction &MF, LiveInsT SavedLiveIns,
1428                      ValueIDNum **MInLocs,
1429                      DenseMap<DebugVariable, unsigned> &AllVarsNumbering);
1430 
1431   /// Boilerplate computation of some initial sets, artifical blocks and
1432   /// RPOT block ordering.
1433   void initialSetup(MachineFunction &MF);
1434 
1435   bool ExtendRanges(MachineFunction &MF, TargetPassConfig *TPC) override;
1436 
1437 public:
1438   /// Default construct and initialize the pass.
1439   InstrRefBasedLDV();
1440 
1441   LLVM_DUMP_METHOD
1442   void dump_mloc_transfer(const MLocTransferMap &mloc_transfer) const;
1443 
1444   bool isCalleeSaved(LocIdx L) {
1445     unsigned Reg = MTracker->LocIdxToLocID[L];
1446     for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
1447       if (CalleeSavedRegs.test(*RAI))
1448         return true;
1449     return false;
1450   }
1451 };
1452 
1453 } // end anonymous namespace
1454 
1455 //===----------------------------------------------------------------------===//
1456 //            Implementation
1457 //===----------------------------------------------------------------------===//
1458 
1459 ValueIDNum ValueIDNum::EmptyValue = {UINT_MAX, UINT_MAX, UINT_MAX};
1460 
1461 /// Default construct and initialize the pass.
1462 InstrRefBasedLDV::InstrRefBasedLDV() {}
1463 
1464 //===----------------------------------------------------------------------===//
1465 //            Debug Range Extension Implementation
1466 //===----------------------------------------------------------------------===//
1467 
1468 #ifndef NDEBUG
1469 // Something to restore in the future.
1470 // void InstrRefBasedLDV::printVarLocInMBB(..)
1471 #endif
1472 
1473 SpillLoc
1474 InstrRefBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) {
1475   assert(MI.hasOneMemOperand() &&
1476          "Spill instruction does not have exactly one memory operand?");
1477   auto MMOI = MI.memoperands_begin();
1478   const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue();
1479   assert(PVal->kind() == PseudoSourceValue::FixedStack &&
1480          "Inconsistent memory operand in spill instruction");
1481   int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
1482   const MachineBasicBlock *MBB = MI.getParent();
1483   Register Reg;
1484   int Offset = TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg);
1485   return {Reg, Offset};
1486 }
1487 
1488 /// End all previous ranges related to @MI and start a new range from @MI
1489 /// if it is a DBG_VALUE instr.
1490 bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
1491   if (!MI.isDebugValue())
1492     return false;
1493 
1494   const DILocalVariable *Var = MI.getDebugVariable();
1495   const DIExpression *Expr = MI.getDebugExpression();
1496   const DILocation *DebugLoc = MI.getDebugLoc();
1497   const DILocation *InlinedAt = DebugLoc->getInlinedAt();
1498   assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
1499          "Expected inlined-at fields to agree");
1500 
1501   DebugVariable V(Var, Expr, InlinedAt);
1502 
1503   // If there are no instructions in this lexical scope, do no location tracking
1504   // at all, this variable shouldn't get a legitimate location range.
1505   auto *Scope = LS.findLexicalScope(MI.getDebugLoc().get());
1506   if (Scope == nullptr)
1507     return true; // handled it; by doing nothing
1508 
1509   const MachineOperand &MO = MI.getOperand(0);
1510 
1511   // MLocTracker needs to know that this register is read, even if it's only
1512   // read by a debug inst.
1513   if (MO.isReg() && MO.getReg() != 0)
1514     (void)MTracker->readReg(MO.getReg());
1515 
1516   // If we're preparing for the second analysis (variables), the machine value
1517   // locations are already solved, and we report this DBG_VALUE and the value
1518   // it refers to to VLocTracker.
1519   if (VTracker) {
1520     if (MO.isReg()) {
1521       // Feed defVar the new variable location, or if this is a
1522       // DBG_VALUE $noreg, feed defVar None.
1523       if (MO.getReg())
1524         VTracker->defVar(MI, MTracker->readReg(MO.getReg()));
1525       else
1526         VTracker->defVar(MI, None);
1527     } else if (MI.getOperand(0).isImm() || MI.getOperand(0).isFPImm() ||
1528                MI.getOperand(0).isCImm()) {
1529       VTracker->defVar(MI, MI.getOperand(0));
1530     }
1531   }
1532 
1533   // If performing final tracking of transfers, report this variable definition
1534   // to the TransferTracker too.
1535   if (TTracker)
1536     TTracker->redefVar(MI);
1537   return true;
1538 }
1539 
1540 void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) {
1541   // Meta Instructions do not affect the debug liveness of any register they
1542   // define.
1543   if (MI.isImplicitDef()) {
1544     // Except when there's an implicit def, and the location it's defining has
1545     // no value number. The whole point of an implicit def is to announce that
1546     // the register is live, without be specific about it's value. So define
1547     // a value if there isn't one already.
1548     ValueIDNum Num = MTracker->readReg(MI.getOperand(0).getReg());
1549     // Has a legitimate value -> ignore the implicit def.
1550     if (Num.getLoc() != 0)
1551       return;
1552     // Otherwise, def it here.
1553   } else if (MI.isMetaInstruction())
1554     return;
1555 
1556   MachineFunction *MF = MI.getMF();
1557   const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
1558   unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
1559 
1560   // Find the regs killed by MI, and find regmasks of preserved regs.
1561   // Max out the number of statically allocated elements in `DeadRegs`, as this
1562   // prevents fallback to std::set::count() operations.
1563   SmallSet<uint32_t, 32> DeadRegs;
1564   SmallVector<const uint32_t *, 4> RegMasks;
1565   SmallVector<const MachineOperand *, 4> RegMaskPtrs;
1566   for (const MachineOperand &MO : MI.operands()) {
1567     // Determine whether the operand is a register def.
1568     if (MO.isReg() && MO.isDef() && MO.getReg() &&
1569         Register::isPhysicalRegister(MO.getReg()) &&
1570         !(MI.isCall() && MO.getReg() == SP)) {
1571       // Remove ranges of all aliased registers.
1572       for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
1573         // FIXME: Can we break out of this loop early if no insertion occurs?
1574         DeadRegs.insert(*RAI);
1575     } else if (MO.isRegMask()) {
1576       RegMasks.push_back(MO.getRegMask());
1577       RegMaskPtrs.push_back(&MO);
1578     }
1579   }
1580 
1581   // Tell MLocTracker about all definitions, of regmasks and otherwise.
1582   for (uint32_t DeadReg : DeadRegs)
1583     MTracker->defReg(DeadReg, CurBB, CurInst);
1584 
1585   for (auto *MO : RegMaskPtrs)
1586     MTracker->writeRegMask(MO, CurBB, CurInst);
1587 }
1588 
1589 void InstrRefBasedLDV::performCopy(Register SrcRegNum, Register DstRegNum) {
1590   ValueIDNum SrcValue = MTracker->readReg(SrcRegNum);
1591 
1592   MTracker->setReg(DstRegNum, SrcValue);
1593 
1594   // In all circumstances, re-def the super registers. It's definitely a new
1595   // value now. This doesn't uniquely identify the composition of subregs, for
1596   // example, two identical values in subregisters composed in different
1597   // places would not get equal value numbers.
1598   for (MCSuperRegIterator SRI(DstRegNum, TRI); SRI.isValid(); ++SRI)
1599     MTracker->defReg(*SRI, CurBB, CurInst);
1600 
1601   // If we're emulating VarLocBasedImpl, just define all the subregisters.
1602   // DBG_VALUEs of them will expect to be tracked from the DBG_VALUE, not
1603   // through prior copies.
1604   if (EmulateOldLDV) {
1605     for (MCSubRegIndexIterator DRI(DstRegNum, TRI); DRI.isValid(); ++DRI)
1606       MTracker->defReg(DRI.getSubReg(), CurBB, CurInst);
1607     return;
1608   }
1609 
1610   // Otherwise, actually copy subregisters from one location to another.
1611   // XXX: in addition, any subregisters of DstRegNum that don't line up with
1612   // the source register should be def'd.
1613   for (MCSubRegIndexIterator SRI(SrcRegNum, TRI); SRI.isValid(); ++SRI) {
1614     unsigned SrcSubReg = SRI.getSubReg();
1615     unsigned SubRegIdx = SRI.getSubRegIndex();
1616     unsigned DstSubReg = TRI->getSubReg(DstRegNum, SubRegIdx);
1617     if (!DstSubReg)
1618       continue;
1619 
1620     // Do copy. There are two matching subregisters, the source value should
1621     // have been def'd when the super-reg was, the latter might not be tracked
1622     // yet.
1623     // This will force SrcSubReg to be tracked, if it isn't yet.
1624     (void)MTracker->readReg(SrcSubReg);
1625     LocIdx SrcL = MTracker->getRegMLoc(SrcSubReg);
1626     assert(SrcL.asU64());
1627     (void)MTracker->readReg(DstSubReg);
1628     LocIdx DstL = MTracker->getRegMLoc(DstSubReg);
1629     assert(DstL.asU64());
1630     (void)DstL;
1631     ValueIDNum CpyValue = {SrcValue.getBlock(), SrcValue.getInst(), SrcL};
1632 
1633     MTracker->setReg(DstSubReg, CpyValue);
1634   }
1635 }
1636 
1637 bool InstrRefBasedLDV::isSpillInstruction(const MachineInstr &MI,
1638                                           MachineFunction *MF) {
1639   // TODO: Handle multiple stores folded into one.
1640   if (!MI.hasOneMemOperand())
1641     return false;
1642 
1643   if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII))
1644     return false; // This is not a spill instruction, since no valid size was
1645                   // returned from either function.
1646 
1647   return true;
1648 }
1649 
1650 bool InstrRefBasedLDV::isLocationSpill(const MachineInstr &MI,
1651                                        MachineFunction *MF, unsigned &Reg) {
1652   if (!isSpillInstruction(MI, MF))
1653     return false;
1654 
1655   // XXX FIXME: On x86, isStoreToStackSlotPostFE returns '1' instead of an
1656   // actual register number.
1657   if (ObserveAllStackops) {
1658     int FI;
1659     Reg = TII->isStoreToStackSlotPostFE(MI, FI);
1660     return Reg != 0;
1661   }
1662 
1663   auto isKilledReg = [&](const MachineOperand MO, unsigned &Reg) {
1664     if (!MO.isReg() || !MO.isUse()) {
1665       Reg = 0;
1666       return false;
1667     }
1668     Reg = MO.getReg();
1669     return MO.isKill();
1670   };
1671 
1672   for (const MachineOperand &MO : MI.operands()) {
1673     // In a spill instruction generated by the InlineSpiller the spilled
1674     // register has its kill flag set.
1675     if (isKilledReg(MO, Reg))
1676       return true;
1677     if (Reg != 0) {
1678       // Check whether next instruction kills the spilled register.
1679       // FIXME: Current solution does not cover search for killed register in
1680       // bundles and instructions further down the chain.
1681       auto NextI = std::next(MI.getIterator());
1682       // Skip next instruction that points to basic block end iterator.
1683       if (MI.getParent()->end() == NextI)
1684         continue;
1685       unsigned RegNext;
1686       for (const MachineOperand &MONext : NextI->operands()) {
1687         // Return true if we came across the register from the
1688         // previous spill instruction that is killed in NextI.
1689         if (isKilledReg(MONext, RegNext) && RegNext == Reg)
1690           return true;
1691       }
1692     }
1693   }
1694   // Return false if we didn't find spilled register.
1695   return false;
1696 }
1697 
1698 Optional<SpillLoc>
1699 InstrRefBasedLDV::isRestoreInstruction(const MachineInstr &MI,
1700                                        MachineFunction *MF, unsigned &Reg) {
1701   if (!MI.hasOneMemOperand())
1702     return None;
1703 
1704   // FIXME: Handle folded restore instructions with more than one memory
1705   // operand.
1706   if (MI.getRestoreSize(TII)) {
1707     Reg = MI.getOperand(0).getReg();
1708     return extractSpillBaseRegAndOffset(MI);
1709   }
1710   return None;
1711 }
1712 
1713 bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
1714   // XXX -- it's too difficult to implement VarLocBasedImpl's  stack location
1715   // limitations under the new model. Therefore, when comparing them, compare
1716   // versions that don't attempt spills or restores at all.
1717   if (EmulateOldLDV)
1718     return false;
1719 
1720   MachineFunction *MF = MI.getMF();
1721   unsigned Reg;
1722   Optional<SpillLoc> Loc;
1723 
1724   LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump(););
1725 
1726   // First, if there are any DBG_VALUEs pointing at a spill slot that is
1727   // written to, terminate that variable location. The value in memory
1728   // will have changed. DbgEntityHistoryCalculator doesn't try to detect this.
1729   if (isSpillInstruction(MI, MF)) {
1730     Loc = extractSpillBaseRegAndOffset(MI);
1731 
1732     if (TTracker) {
1733       Optional<LocIdx> MLoc = MTracker->getSpillMLoc(*Loc);
1734       if (MLoc)
1735         TTracker->clobberMloc(*MLoc, MI.getIterator());
1736     }
1737   }
1738 
1739   // Try to recognise spill and restore instructions that may transfer a value.
1740   if (isLocationSpill(MI, MF, Reg)) {
1741     Loc = extractSpillBaseRegAndOffset(MI);
1742     auto ValueID = MTracker->readReg(Reg);
1743 
1744     // If the location is empty, produce a phi, signify it's the live-in value.
1745     if (ValueID.getLoc() == 0)
1746       ValueID = {CurBB, 0, MTracker->getRegMLoc(Reg)};
1747 
1748     MTracker->setSpill(*Loc, ValueID);
1749     auto OptSpillLocIdx = MTracker->getSpillMLoc(*Loc);
1750     assert(OptSpillLocIdx && "Spill slot set but has no LocIdx?");
1751     LocIdx SpillLocIdx = *OptSpillLocIdx;
1752 
1753     // Tell TransferTracker about this spill, produce DBG_VALUEs for it.
1754     if (TTracker)
1755       TTracker->transferMlocs(MTracker->getRegMLoc(Reg), SpillLocIdx,
1756                               MI.getIterator());
1757 
1758     // VarLocBasedImpl would, at this point, stop tracking the source
1759     // register of the store.
1760     if (EmulateOldLDV) {
1761       for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
1762         MTracker->defReg(*RAI, CurBB, CurInst);
1763     }
1764   } else {
1765     if (!(Loc = isRestoreInstruction(MI, MF, Reg)))
1766       return false;
1767 
1768     // Is there a value to be restored?
1769     auto OptValueID = MTracker->readSpill(*Loc);
1770     if (OptValueID) {
1771       ValueIDNum ValueID = *OptValueID;
1772       LocIdx SpillLocIdx = *MTracker->getSpillMLoc(*Loc);
1773       // XXX -- can we recover sub-registers of this value? Until we can, first
1774       // overwrite all defs of the register being restored to.
1775       for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
1776         MTracker->defReg(*RAI, CurBB, CurInst);
1777 
1778       // Now override the reg we're restoring to.
1779       MTracker->setReg(Reg, ValueID);
1780 
1781       // Report this restore to the transfer tracker too.
1782       if (TTracker)
1783         TTracker->transferMlocs(SpillLocIdx, MTracker->getRegMLoc(Reg),
1784                                 MI.getIterator());
1785     } else {
1786       // There isn't anything in the location; not clear if this is a code path
1787       // that still runs. Def this register anyway just in case.
1788       for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
1789         MTracker->defReg(*RAI, CurBB, CurInst);
1790 
1791       // Force the spill slot to be tracked.
1792       LocIdx L = MTracker->getOrTrackSpillLoc(*Loc);
1793 
1794       // Set the restored value to be a machine phi number, signifying that it's
1795       // whatever the spills live-in value is in this block. Definitely has
1796       // a LocIdx due to the setSpill above.
1797       ValueIDNum ValueID = {CurBB, 0, L};
1798       MTracker->setReg(Reg, ValueID);
1799       MTracker->setSpill(*Loc, ValueID);
1800     }
1801   }
1802   return true;
1803 }
1804 
1805 bool InstrRefBasedLDV::transferRegisterCopy(MachineInstr &MI) {
1806   auto DestSrc = TII->isCopyInstr(MI);
1807   if (!DestSrc)
1808     return false;
1809 
1810   const MachineOperand *DestRegOp = DestSrc->Destination;
1811   const MachineOperand *SrcRegOp = DestSrc->Source;
1812 
1813   auto isCalleeSavedReg = [&](unsigned Reg) {
1814     for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
1815       if (CalleeSavedRegs.test(*RAI))
1816         return true;
1817     return false;
1818   };
1819 
1820   Register SrcReg = SrcRegOp->getReg();
1821   Register DestReg = DestRegOp->getReg();
1822 
1823   // Ignore identity copies. Yep, these make it as far as LiveDebugValues.
1824   if (SrcReg == DestReg)
1825     return true;
1826 
1827   // For emulating VarLocBasedImpl:
1828   // We want to recognize instructions where destination register is callee
1829   // saved register. If register that could be clobbered by the call is
1830   // included, there would be a great chance that it is going to be clobbered
1831   // soon. It is more likely that previous register, which is callee saved, is
1832   // going to stay unclobbered longer, even if it is killed.
1833   //
1834   // For InstrRefBasedImpl, we can track multiple locations per value, so
1835   // ignore this condition.
1836   if (EmulateOldLDV && !isCalleeSavedReg(DestReg))
1837     return false;
1838 
1839   // InstrRefBasedImpl only followed killing copies.
1840   if (EmulateOldLDV && !SrcRegOp->isKill())
1841     return false;
1842 
1843   // Copy MTracker info, including subregs if available.
1844   InstrRefBasedLDV::performCopy(SrcReg, DestReg);
1845 
1846   // Only produce a transfer of DBG_VALUE within a block where old LDV
1847   // would have. We might make use of the additional value tracking in some
1848   // other way, later.
1849   if (TTracker && isCalleeSavedReg(DestReg) && SrcRegOp->isKill())
1850     TTracker->transferMlocs(MTracker->getRegMLoc(SrcReg),
1851                             MTracker->getRegMLoc(DestReg), MI.getIterator());
1852 
1853   // VarLocBasedImpl would quit tracking the old location after copying.
1854   if (EmulateOldLDV && SrcReg != DestReg)
1855     MTracker->defReg(SrcReg, CurBB, CurInst);
1856 
1857   return true;
1858 }
1859 
1860 /// Accumulate a mapping between each DILocalVariable fragment and other
1861 /// fragments of that DILocalVariable which overlap. This reduces work during
1862 /// the data-flow stage from "Find any overlapping fragments" to "Check if the
1863 /// known-to-overlap fragments are present".
1864 /// \param MI A previously unprocessed DEBUG_VALUE instruction to analyze for
1865 ///           fragment usage.
1866 void InstrRefBasedLDV::accumulateFragmentMap(MachineInstr &MI) {
1867   DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(),
1868                       MI.getDebugLoc()->getInlinedAt());
1869   FragmentInfo ThisFragment = MIVar.getFragmentOrDefault();
1870 
1871   // If this is the first sighting of this variable, then we are guaranteed
1872   // there are currently no overlapping fragments either. Initialize the set
1873   // of seen fragments, record no overlaps for the current one, and return.
1874   auto SeenIt = SeenFragments.find(MIVar.getVariable());
1875   if (SeenIt == SeenFragments.end()) {
1876     SmallSet<FragmentInfo, 4> OneFragment;
1877     OneFragment.insert(ThisFragment);
1878     SeenFragments.insert({MIVar.getVariable(), OneFragment});
1879 
1880     OverlapFragments.insert({{MIVar.getVariable(), ThisFragment}, {}});
1881     return;
1882   }
1883 
1884   // If this particular Variable/Fragment pair already exists in the overlap
1885   // map, it has already been accounted for.
1886   auto IsInOLapMap =
1887       OverlapFragments.insert({{MIVar.getVariable(), ThisFragment}, {}});
1888   if (!IsInOLapMap.second)
1889     return;
1890 
1891   auto &ThisFragmentsOverlaps = IsInOLapMap.first->second;
1892   auto &AllSeenFragments = SeenIt->second;
1893 
1894   // Otherwise, examine all other seen fragments for this variable, with "this"
1895   // fragment being a previously unseen fragment. Record any pair of
1896   // overlapping fragments.
1897   for (auto &ASeenFragment : AllSeenFragments) {
1898     // Does this previously seen fragment overlap?
1899     if (DIExpression::fragmentsOverlap(ThisFragment, ASeenFragment)) {
1900       // Yes: Mark the current fragment as being overlapped.
1901       ThisFragmentsOverlaps.push_back(ASeenFragment);
1902       // Mark the previously seen fragment as being overlapped by the current
1903       // one.
1904       auto ASeenFragmentsOverlaps =
1905           OverlapFragments.find({MIVar.getVariable(), ASeenFragment});
1906       assert(ASeenFragmentsOverlaps != OverlapFragments.end() &&
1907              "Previously seen var fragment has no vector of overlaps");
1908       ASeenFragmentsOverlaps->second.push_back(ThisFragment);
1909     }
1910   }
1911 
1912   AllSeenFragments.insert(ThisFragment);
1913 }
1914 
1915 void InstrRefBasedLDV::process(MachineInstr &MI) {
1916   // Try to interpret an MI as a debug or transfer instruction. Only if it's
1917   // none of these should we interpret it's register defs as new value
1918   // definitions.
1919   if (transferDebugValue(MI))
1920     return;
1921   if (transferRegisterCopy(MI))
1922     return;
1923   if (transferSpillOrRestoreInst(MI))
1924     return;
1925   transferRegisterDef(MI);
1926 }
1927 
1928 void InstrRefBasedLDV::produceTransferFunctions(
1929     MachineFunction &MF, SmallVectorImpl<MLocTransferMap> &MLocTransfer,
1930     unsigned MaxNumBlocks, SmallVectorImpl<VLocTracker> &VLocs) {
1931   // Because we try to optimize around register mask operands by ignoring regs
1932   // that aren't currently tracked, we set up something ugly for later: RegMask
1933   // operands that are seen earlier than the first use of a register, still need
1934   // to clobber that register in the transfer function. But this information
1935   // isn't actively recorded. Instead, we track each RegMask used in each block,
1936   // and accumulated the clobbered but untracked registers in each block into
1937   // the following bitvector. Later, if new values are tracked, we can add
1938   // appropriate clobbers.
1939   SmallVector<BitVector, 32> BlockMasks;
1940   BlockMasks.resize(MaxNumBlocks);
1941 
1942   // Reserve one bit per register for the masks described above.
1943   unsigned BVWords = MachineOperand::getRegMaskSize(TRI->getNumRegs());
1944   for (auto &BV : BlockMasks)
1945     BV.resize(TRI->getNumRegs(), true);
1946 
1947   // Step through all instructions and inhale the transfer function.
1948   for (auto &MBB : MF) {
1949     // Object fields that are read by trackers to know where we are in the
1950     // function.
1951     CurBB = MBB.getNumber();
1952     CurInst = 1;
1953 
1954     // Set all machine locations to a PHI value. For transfer function
1955     // production only, this signifies the live-in value to the block.
1956     MTracker->reset();
1957     MTracker->setMPhis(CurBB);
1958 
1959     VTracker = &VLocs[CurBB];
1960     VTracker->MBB = &MBB;
1961 
1962     // Step through each instruction in this block.
1963     for (auto &MI : MBB) {
1964       process(MI);
1965       // Also accumulate fragment map.
1966       if (MI.isDebugValue())
1967         accumulateFragmentMap(MI);
1968       ++CurInst;
1969     }
1970 
1971     // Produce the transfer function, a map of machine location to new value. If
1972     // any machine location has the live-in phi value from the start of the
1973     // block, it's live-through and doesn't need recording in the transfer
1974     // function.
1975     for (auto Location : MTracker->locations()) {
1976       LocIdx Idx = Location.Idx;
1977       ValueIDNum &P = Location.Value;
1978       if (P.isPHI() && P.getLoc() == Idx.asU64())
1979         continue;
1980 
1981       // Insert-or-update.
1982       auto &TransferMap = MLocTransfer[CurBB];
1983       auto Result = TransferMap.insert(std::make_pair(Idx.asU64(), P));
1984       if (!Result.second)
1985         Result.first->second = P;
1986     }
1987 
1988     // Accumulate any bitmask operands into the clobberred reg mask for this
1989     // block.
1990     for (auto &P : MTracker->Masks) {
1991       BlockMasks[CurBB].clearBitsNotInMask(P.first->getRegMask(), BVWords);
1992     }
1993   }
1994 
1995   // Compute a bitvector of all the registers that are tracked in this block.
1996   const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
1997   unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
1998   BitVector UsedRegs(TRI->getNumRegs());
1999   for (auto Location : MTracker->locations()) {
2000     unsigned ID = MTracker->LocIdxToLocID[Location.Idx];
2001     if (ID >= TRI->getNumRegs() || ID == SP)
2002       continue;
2003     UsedRegs.set(ID);
2004   }
2005 
2006   // Check that any regmask-clobber of a register that gets tracked, is not
2007   // live-through in the transfer function. It needs to be clobbered at the
2008   // very least.
2009   for (unsigned int I = 0; I < MaxNumBlocks; ++I) {
2010     BitVector &BV = BlockMasks[I];
2011     BV.flip();
2012     BV &= UsedRegs;
2013     // This produces all the bits that we clobber, but also use. Check that
2014     // they're all clobbered or at least set in the designated transfer
2015     // elem.
2016     for (unsigned Bit : BV.set_bits()) {
2017       unsigned ID = MTracker->getLocID(Bit, false);
2018       LocIdx Idx = MTracker->LocIDToLocIdx[ID];
2019       auto &TransferMap = MLocTransfer[I];
2020 
2021       // Install a value representing the fact that this location is effectively
2022       // written to in this block. As there's no reserved value, instead use
2023       // a value number that is never generated. Pick the value number for the
2024       // first instruction in the block, def'ing this location, which we know
2025       // this block never used anyway.
2026       ValueIDNum NotGeneratedNum = ValueIDNum(I, 1, Idx);
2027       auto Result =
2028         TransferMap.insert(std::make_pair(Idx.asU64(), NotGeneratedNum));
2029       if (!Result.second) {
2030         ValueIDNum &ValueID = Result.first->second;
2031         if (ValueID.getBlock() == I && ValueID.isPHI())
2032           // It was left as live-through. Set it to clobbered.
2033           ValueID = NotGeneratedNum;
2034       }
2035     }
2036   }
2037 }
2038 
2039 std::tuple<bool, bool>
2040 InstrRefBasedLDV::mlocJoin(MachineBasicBlock &MBB,
2041                            SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
2042                            ValueIDNum **OutLocs, ValueIDNum *InLocs) {
2043   LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n");
2044   bool Changed = false;
2045   bool DowngradeOccurred = false;
2046 
2047   // Collect predecessors that have been visited. Anything that hasn't been
2048   // visited yet is a backedge on the first iteration, and the meet of it's
2049   // lattice value for all locations will be unaffected.
2050   SmallVector<const MachineBasicBlock *, 8> BlockOrders;
2051   for (auto Pred : MBB.predecessors()) {
2052     if (Visited.count(Pred)) {
2053       BlockOrders.push_back(Pred);
2054     }
2055   }
2056 
2057   // Visit predecessors in RPOT order.
2058   auto Cmp = [&](const MachineBasicBlock *A, const MachineBasicBlock *B) {
2059     return BBToOrder.find(A)->second < BBToOrder.find(B)->second;
2060   };
2061   llvm::sort(BlockOrders.begin(), BlockOrders.end(), Cmp);
2062 
2063   // Skip entry block.
2064   if (BlockOrders.size() == 0)
2065     return std::tuple<bool, bool>(false, false);
2066 
2067   // Step through all machine locations, then look at each predecessor and
2068   // detect disagreements.
2069   unsigned ThisBlockRPO = BBToOrder.find(&MBB)->second;
2070   for (auto Location : MTracker->locations()) {
2071     LocIdx Idx = Location.Idx;
2072     // Pick out the first predecessors live-out value for this location. It's
2073     // guaranteed to be not a backedge, as we order by RPO.
2074     ValueIDNum BaseVal = OutLocs[BlockOrders[0]->getNumber()][Idx.asU64()];
2075 
2076     // Some flags for whether there's a disagreement, and whether it's a
2077     // disagreement with a backedge or not.
2078     bool Disagree = false;
2079     bool NonBackEdgeDisagree = false;
2080 
2081     // Loop around everything that wasn't 'base'.
2082     for (unsigned int I = 1; I < BlockOrders.size(); ++I) {
2083       auto *MBB = BlockOrders[I];
2084       if (BaseVal != OutLocs[MBB->getNumber()][Idx.asU64()]) {
2085         // Live-out of a predecessor disagrees with the first predecessor.
2086         Disagree = true;
2087 
2088         // Test whether it's a disagreemnt in the backedges or not.
2089         if (BBToOrder.find(MBB)->second < ThisBlockRPO) // might be self b/e
2090           NonBackEdgeDisagree = true;
2091       }
2092     }
2093 
2094     bool OverRide = false;
2095     if (Disagree && !NonBackEdgeDisagree) {
2096       // Only the backedges disagree. Consider demoting the livein
2097       // lattice value, as per the file level comment. The value we consider
2098       // demoting to is the value that the non-backedge predecessors agree on.
2099       // The order of values is that non-PHIs are \top, a PHI at this block
2100       // \bot, and phis between the two are ordered by their RPO number.
2101       // If there's no agreement, or we've already demoted to this PHI value
2102       // before, replace with a PHI value at this block.
2103 
2104       // Calculate order numbers: zero means normal def, nonzero means RPO
2105       // number.
2106       unsigned BaseBlockRPONum = BBNumToRPO[BaseVal.getBlock()] + 1;
2107       if (!BaseVal.isPHI())
2108         BaseBlockRPONum = 0;
2109 
2110       ValueIDNum &InLocID = InLocs[Idx.asU64()];
2111       unsigned InLocRPONum = BBNumToRPO[InLocID.getBlock()] + 1;
2112       if (!InLocID.isPHI())
2113         InLocRPONum = 0;
2114 
2115       // Should we ignore the disagreeing backedges, and override with the
2116       // value the other predecessors agree on (in "base")?
2117       unsigned ThisBlockRPONum = BBNumToRPO[MBB.getNumber()] + 1;
2118       if (BaseBlockRPONum > InLocRPONum && BaseBlockRPONum < ThisBlockRPONum) {
2119         // Override.
2120         OverRide = true;
2121         DowngradeOccurred = true;
2122       }
2123     }
2124     // else: if we disagree in the non-backedges, then this is definitely
2125     // a control flow merge where different values merge. Make it a PHI.
2126 
2127     // Generate a phi...
2128     ValueIDNum PHI = {(uint64_t)MBB.getNumber(), 0, Idx};
2129     ValueIDNum NewVal = (Disagree && !OverRide) ? PHI : BaseVal;
2130     if (InLocs[Idx.asU64()] != NewVal) {
2131       Changed |= true;
2132       InLocs[Idx.asU64()] = NewVal;
2133     }
2134   }
2135 
2136   // Uhhhhhh, reimplement NumInserted and NumRemoved pls.
2137   return std::tuple<bool, bool>(Changed, DowngradeOccurred);
2138 }
2139 
2140 void InstrRefBasedLDV::mlocDataflow(
2141     ValueIDNum **MInLocs, ValueIDNum **MOutLocs,
2142     SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
2143   std::priority_queue<unsigned int, std::vector<unsigned int>,
2144                       std::greater<unsigned int>>
2145       Worklist, Pending;
2146 
2147   // We track what is on the current and pending worklist to avoid inserting
2148   // the same thing twice. We could avoid this with a custom priority queue,
2149   // but this is probably not worth it.
2150   SmallPtrSet<MachineBasicBlock *, 16> OnPending, OnWorklist;
2151 
2152   // Initialize worklist with every block to be visited.
2153   for (unsigned int I = 0; I < BBToOrder.size(); ++I) {
2154     Worklist.push(I);
2155     OnWorklist.insert(OrderToBB[I]);
2156   }
2157 
2158   MTracker->reset();
2159 
2160   // Set inlocs for entry block -- each as a PHI at the entry block. Represents
2161   // the incoming value to the function.
2162   MTracker->setMPhis(0);
2163   for (auto Location : MTracker->locations())
2164     MInLocs[0][Location.Idx.asU64()] = Location.Value;
2165 
2166   SmallPtrSet<const MachineBasicBlock *, 16> Visited;
2167   while (!Worklist.empty() || !Pending.empty()) {
2168     // Vector for storing the evaluated block transfer function.
2169     SmallVector<std::pair<LocIdx, ValueIDNum>, 32> ToRemap;
2170 
2171     while (!Worklist.empty()) {
2172       MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
2173       CurBB = MBB->getNumber();
2174       Worklist.pop();
2175 
2176       // Join the values in all predecessor blocks.
2177       bool InLocsChanged, DowngradeOccurred;
2178       std::tie(InLocsChanged, DowngradeOccurred) =
2179           mlocJoin(*MBB, Visited, MOutLocs, MInLocs[CurBB]);
2180       InLocsChanged |= Visited.insert(MBB).second;
2181 
2182       // If a downgrade occurred, book us in for re-examination on the next
2183       // iteration.
2184       if (DowngradeOccurred && OnPending.insert(MBB).second)
2185         Pending.push(BBToOrder[MBB]);
2186 
2187       // Don't examine transfer function if we've visited this loc at least
2188       // once, and inlocs haven't changed.
2189       if (!InLocsChanged)
2190         continue;
2191 
2192       // Load the current set of live-ins into MLocTracker.
2193       MTracker->loadFromArray(MInLocs[CurBB], CurBB);
2194 
2195       // Each element of the transfer function can be a new def, or a read of
2196       // a live-in value. Evaluate each element, and store to "ToRemap".
2197       ToRemap.clear();
2198       for (auto &P : MLocTransfer[CurBB]) {
2199         if (P.second.getBlock() == CurBB && P.second.isPHI()) {
2200           // This is a movement of whatever was live in. Read it.
2201           ValueIDNum NewID = MTracker->getNumAtPos(P.second.getLoc());
2202           ToRemap.push_back(std::make_pair(P.first, NewID));
2203         } else {
2204           // It's a def. Just set it.
2205           assert(P.second.getBlock() == CurBB);
2206           ToRemap.push_back(std::make_pair(P.first, P.second));
2207         }
2208       }
2209 
2210       // Commit the transfer function changes into mloc tracker, which
2211       // transforms the contents of the MLocTracker into the live-outs.
2212       for (auto &P : ToRemap)
2213         MTracker->setMLoc(P.first, P.second);
2214 
2215       // Now copy out-locs from mloc tracker into out-loc vector, checking
2216       // whether changes have occurred. These changes can have come from both
2217       // the transfer function, and mlocJoin.
2218       bool OLChanged = false;
2219       for (auto Location : MTracker->locations()) {
2220         OLChanged |= MOutLocs[CurBB][Location.Idx.asU64()] != Location.Value;
2221         MOutLocs[CurBB][Location.Idx.asU64()] = Location.Value;
2222       }
2223 
2224       MTracker->reset();
2225 
2226       // No need to examine successors again if out-locs didn't change.
2227       if (!OLChanged)
2228         continue;
2229 
2230       // All successors should be visited: put any back-edges on the pending
2231       // list for the next dataflow iteration, and any other successors to be
2232       // visited this iteration, if they're not going to be already.
2233       for (auto s : MBB->successors()) {
2234         // Does branching to this successor represent a back-edge?
2235         if (BBToOrder[s] > BBToOrder[MBB]) {
2236           // No: visit it during this dataflow iteration.
2237           if (OnWorklist.insert(s).second)
2238             Worklist.push(BBToOrder[s]);
2239         } else {
2240           // Yes: visit it on the next iteration.
2241           if (OnPending.insert(s).second)
2242             Pending.push(BBToOrder[s]);
2243         }
2244       }
2245     }
2246 
2247     Worklist.swap(Pending);
2248     std::swap(OnPending, OnWorklist);
2249     OnPending.clear();
2250     // At this point, pending must be empty, since it was just the empty
2251     // worklist
2252     assert(Pending.empty() && "Pending should be empty");
2253   }
2254 
2255   // Once all the live-ins don't change on mlocJoin(), we've reached a
2256   // fixedpoint.
2257 }
2258 
2259 bool InstrRefBasedLDV::vlocDowngradeLattice(
2260     const MachineBasicBlock &MBB, const DbgValue &OldLiveInLocation,
2261     const SmallVectorImpl<InValueT> &Values, unsigned CurBlockRPONum) {
2262   // Ranking value preference: see file level comment, the highest rank is
2263   // a plain def, followed by PHI values in reverse post-order. Numerically,
2264   // we assign all defs the rank '0', all PHIs their blocks RPO number plus
2265   // one, and consider the lowest value the highest ranked.
2266   int OldLiveInRank = BBNumToRPO[OldLiveInLocation.ID.getBlock()] + 1;
2267   if (!OldLiveInLocation.ID.isPHI())
2268     OldLiveInRank = 0;
2269 
2270   // Allow any unresolvable conflict to be over-ridden.
2271   if (OldLiveInLocation.Kind == DbgValue::NoVal) {
2272     // Although if it was an unresolvable conflict from _this_ block, then
2273     // all other seeking of downgrades and PHIs must have failed before hand.
2274     if (OldLiveInLocation.BlockNo == (unsigned)MBB.getNumber())
2275       return false;
2276     OldLiveInRank = INT_MIN;
2277   }
2278 
2279   auto &InValue = *Values[0].second;
2280 
2281   if (InValue.Kind == DbgValue::Const || InValue.Kind == DbgValue::NoVal)
2282     return false;
2283 
2284   unsigned ThisRPO = BBNumToRPO[InValue.ID.getBlock()];
2285   int ThisRank = ThisRPO + 1;
2286   if (!InValue.ID.isPHI())
2287     ThisRank = 0;
2288 
2289   // Too far down the lattice?
2290   if (ThisRPO >= CurBlockRPONum)
2291     return false;
2292 
2293   // Higher in the lattice than what we've already explored?
2294   if (ThisRank <= OldLiveInRank)
2295     return false;
2296 
2297   return true;
2298 }
2299 
2300 std::tuple<Optional<ValueIDNum>, bool> InstrRefBasedLDV::pickVPHILoc(
2301     MachineBasicBlock &MBB, const DebugVariable &Var, const LiveIdxT &LiveOuts,
2302     ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
2303     const SmallVectorImpl<MachineBasicBlock *> &BlockOrders) {
2304   // Collect a set of locations from predecessor where its live-out value can
2305   // be found.
2306   SmallVector<SmallVector<LocIdx, 4>, 8> Locs;
2307   unsigned NumLocs = MTracker->getNumLocs();
2308   unsigned BackEdgesStart = 0;
2309 
2310   for (auto p : BlockOrders) {
2311     // Pick out where backedges start in the list of predecessors. Relies on
2312     // BlockOrders being sorted by RPO.
2313     if (BBToOrder[p] < BBToOrder[&MBB])
2314       ++BackEdgesStart;
2315 
2316     // For each predecessor, create a new set of locations.
2317     Locs.resize(Locs.size() + 1);
2318     unsigned ThisBBNum = p->getNumber();
2319     auto LiveOutMap = LiveOuts.find(p);
2320     if (LiveOutMap == LiveOuts.end())
2321       // This predecessor isn't in scope, it must have no live-in/live-out
2322       // locations.
2323       continue;
2324 
2325     auto It = LiveOutMap->second->find(Var);
2326     if (It == LiveOutMap->second->end())
2327       // There's no value recorded for this variable in this predecessor,
2328       // leave an empty set of locations.
2329       continue;
2330 
2331     const DbgValue &OutVal = It->second;
2332 
2333     if (OutVal.Kind == DbgValue::Const || OutVal.Kind == DbgValue::NoVal)
2334       // Consts and no-values cannot have locations we can join on.
2335       continue;
2336 
2337     assert(OutVal.Kind == DbgValue::Proposed || OutVal.Kind == DbgValue::Def);
2338     ValueIDNum ValToLookFor = OutVal.ID;
2339 
2340     // Search the live-outs of the predecessor for the specified value.
2341     for (unsigned int I = 0; I < NumLocs; ++I) {
2342       if (MOutLocs[ThisBBNum][I] == ValToLookFor)
2343         Locs.back().push_back(LocIdx(I));
2344     }
2345   }
2346 
2347   // If there were no locations at all, return an empty result.
2348   if (Locs.empty())
2349     return std::tuple<Optional<ValueIDNum>, bool>(None, false);
2350 
2351   // Lambda for seeking a common location within a range of location-sets.
2352   typedef SmallVector<SmallVector<LocIdx, 4>, 8>::iterator LocsIt;
2353   auto SeekLocation =
2354       [&Locs](llvm::iterator_range<LocsIt> SearchRange) -> Optional<LocIdx> {
2355     // Starting with the first set of locations, take the intersection with
2356     // subsequent sets.
2357     SmallVector<LocIdx, 4> base = Locs[0];
2358     for (auto &S : SearchRange) {
2359       SmallVector<LocIdx, 4> new_base;
2360       std::set_intersection(base.begin(), base.end(), S.begin(), S.end(),
2361                             std::inserter(new_base, new_base.begin()));
2362       base = new_base;
2363     }
2364     if (base.empty())
2365       return None;
2366 
2367     // We now have a set of LocIdxes that contain the right output value in
2368     // each of the predecessors. Pick the lowest; if there's a register loc,
2369     // that'll be it.
2370     return *base.begin();
2371   };
2372 
2373   // Search for a common location for all predecessors. If we can't, then fall
2374   // back to only finding a common location between non-backedge predecessors.
2375   bool ValidForAllLocs = true;
2376   auto TheLoc = SeekLocation(Locs);
2377   if (!TheLoc) {
2378     ValidForAllLocs = false;
2379     TheLoc =
2380         SeekLocation(make_range(Locs.begin(), Locs.begin() + BackEdgesStart));
2381   }
2382 
2383   if (!TheLoc)
2384     return std::tuple<Optional<ValueIDNum>, bool>(None, false);
2385 
2386   // Return a PHI-value-number for the found location.
2387   LocIdx L = *TheLoc;
2388   ValueIDNum PHIVal = {(unsigned)MBB.getNumber(), 0, L};
2389   return std::tuple<Optional<ValueIDNum>, bool>(PHIVal, ValidForAllLocs);
2390 }
2391 
2392 std::tuple<bool, bool> InstrRefBasedLDV::vlocJoin(
2393     MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs, LiveIdxT &VLOCInLocs,
2394     SmallPtrSet<const MachineBasicBlock *, 16> *VLOCVisited, unsigned BBNum,
2395     const SmallSet<DebugVariable, 4> &AllVars, ValueIDNum **MOutLocs,
2396     ValueIDNum **MInLocs,
2397     SmallPtrSet<const MachineBasicBlock *, 8> &InScopeBlocks,
2398     SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore,
2399     DenseMap<DebugVariable, DbgValue> &InLocsT) {
2400   bool DowngradeOccurred = false;
2401 
2402   // To emulate VarLocBasedImpl, process this block if it's not in scope but
2403   // _does_ assign a variable value. No live-ins for this scope are transferred
2404   // in though, so we can return immediately.
2405   if (InScopeBlocks.count(&MBB) == 0 && !ArtificialBlocks.count(&MBB)) {
2406     if (VLOCVisited)
2407       return std::tuple<bool, bool>(true, false);
2408     return std::tuple<bool, bool>(false, false);
2409   }
2410 
2411   LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n");
2412   bool Changed = false;
2413 
2414   // Find any live-ins computed in a prior iteration.
2415   auto ILSIt = VLOCInLocs.find(&MBB);
2416   assert(ILSIt != VLOCInLocs.end());
2417   auto &ILS = *ILSIt->second;
2418 
2419   // Order predecessors by RPOT order, for exploring them in that order.
2420   SmallVector<MachineBasicBlock *, 8> BlockOrders;
2421   for (auto p : MBB.predecessors())
2422     BlockOrders.push_back(p);
2423 
2424   auto Cmp = [&](MachineBasicBlock *A, MachineBasicBlock *B) {
2425     return BBToOrder[A] < BBToOrder[B];
2426   };
2427 
2428   llvm::sort(BlockOrders.begin(), BlockOrders.end(), Cmp);
2429 
2430   unsigned CurBlockRPONum = BBToOrder[&MBB];
2431 
2432   // Force a re-visit to loop heads in the first dataflow iteration.
2433   // FIXME: if we could "propose" Const values this wouldn't be needed,
2434   // because they'd need to be confirmed before being emitted.
2435   if (!BlockOrders.empty() &&
2436       BBToOrder[BlockOrders[BlockOrders.size() - 1]] >= CurBlockRPONum &&
2437       VLOCVisited)
2438     DowngradeOccurred = true;
2439 
2440   auto ConfirmValue = [&InLocsT](const DebugVariable &DV, DbgValue VR) {
2441     auto Result = InLocsT.insert(std::make_pair(DV, VR));
2442     (void)Result;
2443     assert(Result.second);
2444   };
2445 
2446   auto ConfirmNoVal = [&ConfirmValue, &MBB](const DebugVariable &Var, const DbgValueProperties &Properties) {
2447     DbgValue NoLocPHIVal(MBB.getNumber(), Properties, DbgValue::NoVal);
2448 
2449     ConfirmValue(Var, NoLocPHIVal);
2450   };
2451 
2452   // Attempt to join the values for each variable.
2453   for (auto &Var : AllVars) {
2454     // Collect all the DbgValues for this variable.
2455     SmallVector<InValueT, 8> Values;
2456     bool Bail = false;
2457     unsigned BackEdgesStart = 0;
2458     for (auto p : BlockOrders) {
2459       // If the predecessor isn't in scope / to be explored, we'll never be
2460       // able to join any locations.
2461       if (BlocksToExplore.find(p) == BlocksToExplore.end()) {
2462         Bail = true;
2463         break;
2464       }
2465 
2466       // Don't attempt to handle unvisited predecessors: they're implicitly
2467       // "unknown"s in the lattice.
2468       if (VLOCVisited && !VLOCVisited->count(p))
2469         continue;
2470 
2471       // If the predecessors OutLocs is absent, there's not much we can do.
2472       auto OL = VLOCOutLocs.find(p);
2473       if (OL == VLOCOutLocs.end()) {
2474         Bail = true;
2475         break;
2476       }
2477 
2478       // No live-out value for this predecessor also means we can't produce
2479       // a joined value.
2480       auto VIt = OL->second->find(Var);
2481       if (VIt == OL->second->end()) {
2482         Bail = true;
2483         break;
2484       }
2485 
2486       // Keep track of where back-edges begin in the Values vector. Relies on
2487       // BlockOrders being sorted by RPO.
2488       unsigned ThisBBRPONum = BBToOrder[p];
2489       if (ThisBBRPONum < CurBlockRPONum)
2490         ++BackEdgesStart;
2491 
2492       Values.push_back(std::make_pair(p, &VIt->second));
2493     }
2494 
2495     // If there were no values, or one of the predecessors couldn't have a
2496     // value, then give up immediately. It's not safe to produce a live-in
2497     // value.
2498     if (Bail || Values.size() == 0)
2499       continue;
2500 
2501     // Enumeration identifying the current state of the predecessors values.
2502     enum {
2503       Unset = 0,
2504       Agreed,       // All preds agree on the variable value.
2505       PropDisagree, // All preds agree, but the value kind is Proposed in some.
2506       BEDisagree,   // Only back-edges disagree on variable value.
2507       PHINeeded,    // Non-back-edge predecessors have conflicing values.
2508       NoSolution    // Conflicting Value metadata makes solution impossible.
2509     } OurState = Unset;
2510 
2511     // All (non-entry) blocks have at least one non-backedge predecessor.
2512     // Pick the variable value from the first of these, to compare against
2513     // all others.
2514     const DbgValue &FirstVal = *Values[0].second;
2515     const ValueIDNum &FirstID = FirstVal.ID;
2516 
2517     // Scan for variable values that can't be resolved: if they have different
2518     // DIExpressions, different indirectness, or are mixed constants /
2519     // non-constants.
2520     for (auto &V : Values) {
2521       if (V.second->Properties != FirstVal.Properties)
2522         OurState = NoSolution;
2523       if (V.second->Kind == DbgValue::Const && FirstVal.Kind != DbgValue::Const)
2524         OurState = NoSolution;
2525     }
2526 
2527     // Flags diagnosing _how_ the values disagree.
2528     bool NonBackEdgeDisagree = false;
2529     bool DisagreeOnPHINess = false;
2530     bool IDDisagree = false;
2531     bool Disagree = false;
2532     if (OurState == Unset) {
2533       for (auto &V : Values) {
2534         if (*V.second == FirstVal)
2535           continue; // No disagreement.
2536 
2537         Disagree = true;
2538 
2539         // Flag whether the value number actually diagrees.
2540         if (V.second->ID != FirstID)
2541           IDDisagree = true;
2542 
2543         // Distinguish whether disagreement happens in backedges or not.
2544         // Relies on Values (and BlockOrders) being sorted by RPO.
2545         unsigned ThisBBRPONum = BBToOrder[V.first];
2546         if (ThisBBRPONum < CurBlockRPONum)
2547           NonBackEdgeDisagree = true;
2548 
2549         // Is there a difference in whether the value is definite or only
2550         // proposed?
2551         if (V.second->Kind != FirstVal.Kind &&
2552             (V.second->Kind == DbgValue::Proposed ||
2553              V.second->Kind == DbgValue::Def) &&
2554             (FirstVal.Kind == DbgValue::Proposed ||
2555              FirstVal.Kind == DbgValue::Def))
2556           DisagreeOnPHINess = true;
2557       }
2558 
2559       // Collect those flags together and determine an overall state for
2560       // what extend the predecessors agree on a live-in value.
2561       if (!Disagree)
2562         OurState = Agreed;
2563       else if (!IDDisagree && DisagreeOnPHINess)
2564         OurState = PropDisagree;
2565       else if (!NonBackEdgeDisagree)
2566         OurState = BEDisagree;
2567       else
2568         OurState = PHINeeded;
2569     }
2570 
2571     // An extra indicator: if we only disagree on whether the value is a
2572     // Def, or proposed, then also flag whether that disagreement happens
2573     // in backedges only.
2574     bool PropOnlyInBEs = Disagree && !IDDisagree && DisagreeOnPHINess &&
2575                          !NonBackEdgeDisagree && FirstVal.Kind == DbgValue::Def;
2576 
2577     const auto &Properties = FirstVal.Properties;
2578 
2579     auto OldLiveInIt = ILS.find(Var);
2580     const DbgValue *OldLiveInLocation =
2581         (OldLiveInIt != ILS.end()) ? &OldLiveInIt->second : nullptr;
2582 
2583     bool OverRide = false;
2584     if (OurState == BEDisagree && OldLiveInLocation) {
2585       // Only backedges disagree: we can consider downgrading. If there was a
2586       // previous live-in value, use it to work out whether the current
2587       // incoming value represents a lattice downgrade or not.
2588       OverRide =
2589           vlocDowngradeLattice(MBB, *OldLiveInLocation, Values, CurBlockRPONum);
2590     }
2591 
2592     // Use the current state of predecessor agreement and other flags to work
2593     // out what to do next. Possibilities include:
2594     //  * Accept a value all predecessors agree on, or accept one that
2595     //    represents a step down the exploration lattice,
2596     //  * Use a PHI value number, if one can be found,
2597     //  * Propose a PHI value number, and see if it gets confirmed later,
2598     //  * Emit a 'NoVal' value, indicating we couldn't resolve anything.
2599     if (OurState == Agreed) {
2600       // Easiest solution: all predecessors agree on the variable value.
2601       ConfirmValue(Var, FirstVal);
2602     } else if (OurState == BEDisagree && OverRide) {
2603       // Only backedges disagree, and the other predecessors have produced
2604       // a new live-in value further down the exploration lattice.
2605       DowngradeOccurred = true;
2606       ConfirmValue(Var, FirstVal);
2607     } else if (OurState == PropDisagree) {
2608       // Predecessors agree on value, but some say it's only a proposed value.
2609       // Propagate it as proposed: unless it was proposed in this block, in
2610       // which case we're able to confirm the value.
2611       if (FirstID.getBlock() == (uint64_t)MBB.getNumber() && FirstID.isPHI()) {
2612         ConfirmValue(Var, DbgValue(FirstID, Properties, DbgValue::Def));
2613       } else if (PropOnlyInBEs) {
2614         // If only backedges disagree, a higher (in RPO) block confirmed this
2615         // location, and we need to propagate it into this loop.
2616         ConfirmValue(Var, DbgValue(FirstID, Properties, DbgValue::Def));
2617       } else {
2618         // Otherwise; a Def meeting a Proposed is still a Proposed.
2619         ConfirmValue(Var, DbgValue(FirstID, Properties, DbgValue::Proposed));
2620       }
2621     } else if ((OurState == PHINeeded || OurState == BEDisagree)) {
2622       // Predecessors disagree and can't be downgraded: this can only be
2623       // solved with a PHI. Use pickVPHILoc to go look for one.
2624       Optional<ValueIDNum> VPHI;
2625       bool AllEdgesVPHI = false;
2626       std::tie(VPHI, AllEdgesVPHI) =
2627           pickVPHILoc(MBB, Var, VLOCOutLocs, MOutLocs, MInLocs, BlockOrders);
2628 
2629       if (VPHI && AllEdgesVPHI) {
2630         // There's a PHI value that's valid for all predecessors -- we can use
2631         // it. If any of the non-backedge predecessors have proposed values
2632         // though, this PHI is also only proposed, until the predecessors are
2633         // confirmed.
2634         DbgValue::KindT K = DbgValue::Def;
2635         for (unsigned int I = 0; I < BackEdgesStart; ++I)
2636           if (Values[I].second->Kind == DbgValue::Proposed)
2637             K = DbgValue::Proposed;
2638 
2639         ConfirmValue(Var, DbgValue(*VPHI, Properties, K));
2640       } else if (VPHI) {
2641         // There's a PHI value, but it's only legal for backedges. Leave this
2642         // as a proposed PHI value: it might come back on the backedges,
2643         // and allow us to confirm it in the future.
2644         DbgValue NoBEValue = DbgValue(*VPHI, Properties, DbgValue::Proposed);
2645         ConfirmValue(Var, NoBEValue);
2646       } else {
2647         ConfirmNoVal(Var, Properties);
2648       }
2649     } else {
2650       // Otherwise: we don't know. Emit a "phi but no real loc" phi.
2651       ConfirmNoVal(Var, Properties);
2652     }
2653   }
2654 
2655   // Store newly calculated in-locs into VLOCInLocs, if they've changed.
2656   Changed = ILS != InLocsT;
2657   if (Changed)
2658     ILS = InLocsT;
2659 
2660   return std::tuple<bool, bool>(Changed, DowngradeOccurred);
2661 }
2662 
2663 void InstrRefBasedLDV::vlocDataflow(
2664     const LexicalScope *Scope, const DILocation *DILoc,
2665     const SmallSet<DebugVariable, 4> &VarsWeCareAbout,
2666     SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, LiveInsT &Output,
2667     ValueIDNum **MOutLocs, ValueIDNum **MInLocs,
2668     SmallVectorImpl<VLocTracker> &AllTheVLocs) {
2669   // This method is much like mlocDataflow: but focuses on a single
2670   // LexicalScope at a time. Pick out a set of blocks and variables that are
2671   // to have their value assignments solved, then run our dataflow algorithm
2672   // until a fixedpoint is reached.
2673   std::priority_queue<unsigned int, std::vector<unsigned int>,
2674                       std::greater<unsigned int>>
2675       Worklist, Pending;
2676   SmallPtrSet<MachineBasicBlock *, 16> OnWorklist, OnPending;
2677 
2678   // The set of blocks we'll be examining.
2679   SmallPtrSet<const MachineBasicBlock *, 8> BlocksToExplore;
2680 
2681   // The order in which to examine them (RPO).
2682   SmallVector<MachineBasicBlock *, 8> BlockOrders;
2683 
2684   // RPO ordering function.
2685   auto Cmp = [&](MachineBasicBlock *A, MachineBasicBlock *B) {
2686     return BBToOrder[A] < BBToOrder[B];
2687   };
2688 
2689   LS.getMachineBasicBlocks(DILoc, BlocksToExplore);
2690 
2691   // A separate container to distinguish "blocks we're exploring" versus
2692   // "blocks that are potentially in scope. See comment at start of vlocJoin.
2693   SmallPtrSet<const MachineBasicBlock *, 8> InScopeBlocks = BlocksToExplore;
2694 
2695   // Old LiveDebugValues tracks variable locations that come out of blocks
2696   // not in scope, where DBG_VALUEs occur. This is something we could
2697   // legitimately ignore, but lets allow it for now.
2698   if (EmulateOldLDV)
2699     BlocksToExplore.insert(AssignBlocks.begin(), AssignBlocks.end());
2700 
2701   // We also need to propagate variable values through any artificial blocks
2702   // that immediately follow blocks in scope.
2703   DenseSet<const MachineBasicBlock *> ToAdd;
2704 
2705   // Helper lambda: For a given block in scope, perform a depth first search
2706   // of all the artificial successors, adding them to the ToAdd collection.
2707   auto AccumulateArtificialBlocks =
2708       [this, &ToAdd, &BlocksToExplore,
2709        &InScopeBlocks](const MachineBasicBlock *MBB) {
2710         // Depth-first-search state: each node is a block and which successor
2711         // we're currently exploring.
2712         SmallVector<std::pair<const MachineBasicBlock *,
2713                               MachineBasicBlock::const_succ_iterator>,
2714                     8>
2715             DFS;
2716 
2717         // Find any artificial successors not already tracked.
2718         for (auto *succ : MBB->successors()) {
2719           if (BlocksToExplore.count(succ) || InScopeBlocks.count(succ))
2720             continue;
2721           if (!ArtificialBlocks.count(succ))
2722             continue;
2723           DFS.push_back(std::make_pair(succ, succ->succ_begin()));
2724           ToAdd.insert(succ);
2725         }
2726 
2727         // Search all those blocks, depth first.
2728         while (!DFS.empty()) {
2729           const MachineBasicBlock *CurBB = DFS.back().first;
2730           MachineBasicBlock::const_succ_iterator &CurSucc = DFS.back().second;
2731           // Walk back if we've explored this blocks successors to the end.
2732           if (CurSucc == CurBB->succ_end()) {
2733             DFS.pop_back();
2734             continue;
2735           }
2736 
2737           // If the current successor is artificial and unexplored, descend into
2738           // it.
2739           if (!ToAdd.count(*CurSucc) && ArtificialBlocks.count(*CurSucc)) {
2740             DFS.push_back(std::make_pair(*CurSucc, (*CurSucc)->succ_begin()));
2741             ToAdd.insert(*CurSucc);
2742             continue;
2743           }
2744 
2745           ++CurSucc;
2746         }
2747       };
2748 
2749   // Search in-scope blocks and those containing a DBG_VALUE from this scope
2750   // for artificial successors.
2751   for (auto *MBB : BlocksToExplore)
2752     AccumulateArtificialBlocks(MBB);
2753   for (auto *MBB : InScopeBlocks)
2754     AccumulateArtificialBlocks(MBB);
2755 
2756   BlocksToExplore.insert(ToAdd.begin(), ToAdd.end());
2757   InScopeBlocks.insert(ToAdd.begin(), ToAdd.end());
2758 
2759   // Single block scope: not interesting! No propagation at all. Note that
2760   // this could probably go above ArtificialBlocks without damage, but
2761   // that then produces output differences from original-live-debug-values,
2762   // which propagates from a single block into many artificial ones.
2763   if (BlocksToExplore.size() == 1)
2764     return;
2765 
2766   // Picks out relevants blocks RPO order and sort them.
2767   for (auto *MBB : BlocksToExplore)
2768     BlockOrders.push_back(const_cast<MachineBasicBlock *>(MBB));
2769 
2770   llvm::sort(BlockOrders.begin(), BlockOrders.end(), Cmp);
2771   unsigned NumBlocks = BlockOrders.size();
2772 
2773   // Allocate some vectors for storing the live ins and live outs. Large.
2774   SmallVector<DenseMap<DebugVariable, DbgValue>, 32> LiveIns, LiveOuts;
2775   LiveIns.resize(NumBlocks);
2776   LiveOuts.resize(NumBlocks);
2777 
2778   // Produce by-MBB indexes of live-in/live-outs, to ease lookup within
2779   // vlocJoin.
2780   LiveIdxT LiveOutIdx, LiveInIdx;
2781   LiveOutIdx.reserve(NumBlocks);
2782   LiveInIdx.reserve(NumBlocks);
2783   for (unsigned I = 0; I < NumBlocks; ++I) {
2784     LiveOutIdx[BlockOrders[I]] = &LiveOuts[I];
2785     LiveInIdx[BlockOrders[I]] = &LiveIns[I];
2786   }
2787 
2788   for (auto *MBB : BlockOrders) {
2789     Worklist.push(BBToOrder[MBB]);
2790     OnWorklist.insert(MBB);
2791   }
2792 
2793   // Iterate over all the blocks we selected, propagating variable values.
2794   bool FirstTrip = true;
2795   SmallPtrSet<const MachineBasicBlock *, 16> VLOCVisited;
2796   while (!Worklist.empty() || !Pending.empty()) {
2797     while (!Worklist.empty()) {
2798       auto *MBB = OrderToBB[Worklist.top()];
2799       CurBB = MBB->getNumber();
2800       Worklist.pop();
2801 
2802       DenseMap<DebugVariable, DbgValue> JoinedInLocs;
2803 
2804       // Join values from predecessors. Updates LiveInIdx, and writes output
2805       // into JoinedInLocs.
2806       bool InLocsChanged, DowngradeOccurred;
2807       std::tie(InLocsChanged, DowngradeOccurred) = vlocJoin(
2808           *MBB, LiveOutIdx, LiveInIdx, (FirstTrip) ? &VLOCVisited : nullptr,
2809           CurBB, VarsWeCareAbout, MOutLocs, MInLocs, InScopeBlocks,
2810           BlocksToExplore, JoinedInLocs);
2811 
2812       auto &VTracker = AllTheVLocs[MBB->getNumber()];
2813       bool FirstVisit = VLOCVisited.insert(MBB).second;
2814 
2815       // Always explore transfer function if inlocs changed, or if we've not
2816       // visited this block before.
2817       InLocsChanged |= FirstVisit;
2818 
2819       // If a downgrade occurred, book us in for re-examination on the next
2820       // iteration.
2821       if (DowngradeOccurred && OnPending.insert(MBB).second)
2822         Pending.push(BBToOrder[MBB]);
2823 
2824       // Patch up the variable value transfer function to use the live-in
2825       // machine values, now that that problem is solved.
2826       if (FirstVisit) {
2827         for (auto &Transfer : VTracker.Vars) {
2828           if (Transfer.second.Kind == DbgValue::Def &&
2829               Transfer.second.ID.getBlock() == CurBB &&
2830               Transfer.second.ID.isPHI()) {
2831             LocIdx Loc = Transfer.second.ID.getLoc();
2832             Transfer.second.ID = MInLocs[CurBB][Loc.asU64()];
2833           }
2834         }
2835       }
2836 
2837       if (!InLocsChanged)
2838         continue;
2839 
2840       // Do transfer function.
2841       for (auto &Transfer : VTracker.Vars) {
2842         // Is this var we're mangling in this scope?
2843         if (VarsWeCareAbout.count(Transfer.first)) {
2844           // Erase on empty transfer (DBG_VALUE $noreg).
2845           if (Transfer.second.Kind == DbgValue::Undef) {
2846             JoinedInLocs.erase(Transfer.first);
2847           } else {
2848             // Insert new variable value; or overwrite.
2849             auto NewValuePair = std::make_pair(Transfer.first, Transfer.second);
2850             auto Result = JoinedInLocs.insert(NewValuePair);
2851             if (!Result.second)
2852               Result.first->second = Transfer.second;
2853           }
2854         }
2855       }
2856 
2857       // Did the live-out locations change?
2858       bool OLChanged = JoinedInLocs != *LiveOutIdx[MBB];
2859 
2860       // If they haven't changed, there's no need to explore further.
2861       if (!OLChanged)
2862         continue;
2863 
2864       // Commit to the live-out record.
2865       *LiveOutIdx[MBB] = JoinedInLocs;
2866 
2867       // We should visit all successors. Ensure we'll visit any non-backedge
2868       // successors during this dataflow iteration; book backedge successors
2869       // to be visited next time around.
2870       for (auto s : MBB->successors()) {
2871         // Ignore out of scope / not-to-be-explored successors.
2872         if (LiveInIdx.find(s) == LiveInIdx.end())
2873           continue;
2874 
2875         if (BBToOrder[s] > BBToOrder[MBB]) {
2876           if (OnWorklist.insert(s).second)
2877             Worklist.push(BBToOrder[s]);
2878         } else if (OnPending.insert(s).second && (FirstTrip || OLChanged)) {
2879           Pending.push(BBToOrder[s]);
2880         }
2881       }
2882     }
2883     Worklist.swap(Pending);
2884     std::swap(OnWorklist, OnPending);
2885     OnPending.clear();
2886     assert(Pending.empty());
2887     FirstTrip = false;
2888   }
2889 
2890   // Dataflow done. Now what? Save live-ins. Ignore any that are still marked
2891   // as being variable-PHIs, because those did not have their machine-PHI
2892   // value confirmed. Such variable values are places that could have been
2893   // PHIs, but are not.
2894   for (auto *MBB : BlockOrders) {
2895     auto &VarMap = *LiveInIdx[MBB];
2896     for (auto &P : VarMap) {
2897       if (P.second.Kind == DbgValue::Proposed ||
2898           P.second.Kind == DbgValue::NoVal)
2899         continue;
2900       Output[MBB->getNumber()].push_back(P);
2901     }
2902   }
2903 
2904   BlockOrders.clear();
2905   BlocksToExplore.clear();
2906 }
2907 
2908 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2909 void InstrRefBasedLDV::dump_mloc_transfer(
2910     const MLocTransferMap &mloc_transfer) const {
2911   for (auto &P : mloc_transfer) {
2912     std::string foo = MTracker->LocIdxToName(P.first);
2913     std::string bar = MTracker->IDAsString(P.second);
2914     dbgs() << "Loc " << foo << " --> " << bar << "\n";
2915   }
2916 }
2917 #endif
2918 
2919 void InstrRefBasedLDV::emitLocations(
2920     MachineFunction &MF, LiveInsT SavedLiveIns, ValueIDNum **MInLocs,
2921     DenseMap<DebugVariable, unsigned> &AllVarsNumbering) {
2922   TTracker = new TransferTracker(TII, MTracker, MF, *TRI, CalleeSavedRegs);
2923   unsigned NumLocs = MTracker->getNumLocs();
2924 
2925   // For each block, load in the machine value locations and variable value
2926   // live-ins, then step through each instruction in the block. New DBG_VALUEs
2927   // to be inserted will be created along the way.
2928   for (MachineBasicBlock &MBB : MF) {
2929     unsigned bbnum = MBB.getNumber();
2930     MTracker->reset();
2931     MTracker->loadFromArray(MInLocs[bbnum], bbnum);
2932     TTracker->loadInlocs(MBB, MInLocs[bbnum], SavedLiveIns[MBB.getNumber()],
2933                          NumLocs);
2934 
2935     CurBB = bbnum;
2936     CurInst = 1;
2937     for (auto &MI : MBB) {
2938       process(MI);
2939       ++CurInst;
2940     }
2941   }
2942 
2943   // We have to insert DBG_VALUEs in a consistent order, otherwise they appeaer
2944   // in DWARF in different orders. Use the order that they appear when walking
2945   // through each block / each instruction, stored in AllVarsNumbering.
2946   auto OrderDbgValues = [&](const MachineInstr *A,
2947                             const MachineInstr *B) -> bool {
2948     DebugVariable VarA(A->getDebugVariable(), A->getDebugExpression(),
2949                        A->getDebugLoc()->getInlinedAt());
2950     DebugVariable VarB(B->getDebugVariable(), B->getDebugExpression(),
2951                        B->getDebugLoc()->getInlinedAt());
2952     return AllVarsNumbering.find(VarA)->second <
2953            AllVarsNumbering.find(VarB)->second;
2954   };
2955 
2956   // Go through all the transfers recorded in the TransferTracker -- this is
2957   // both the live-ins to a block, and any movements of values that happen
2958   // in the middle.
2959   for (auto &P : TTracker->Transfers) {
2960     // Sort them according to appearance order.
2961     llvm::sort(P.Insts.begin(), P.Insts.end(), OrderDbgValues);
2962     // Insert either before or after the designated point...
2963     if (P.MBB) {
2964       MachineBasicBlock &MBB = *P.MBB;
2965       for (auto *MI : P.Insts) {
2966         MBB.insert(P.Pos, MI);
2967       }
2968     } else {
2969       MachineBasicBlock &MBB = *P.Pos->getParent();
2970       for (auto *MI : P.Insts) {
2971         MBB.insertAfter(P.Pos, MI);
2972       }
2973     }
2974   }
2975 }
2976 
2977 void InstrRefBasedLDV::initialSetup(MachineFunction &MF) {
2978   // Build some useful data structures.
2979   auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool {
2980     if (const DebugLoc &DL = MI.getDebugLoc())
2981       return DL.getLine() != 0;
2982     return false;
2983   };
2984   // Collect a set of all the artificial blocks.
2985   for (auto &MBB : MF)
2986     if (none_of(MBB.instrs(), hasNonArtificialLocation))
2987       ArtificialBlocks.insert(&MBB);
2988 
2989   // Compute mappings of block <=> RPO order.
2990   ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
2991   unsigned int RPONumber = 0;
2992   for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
2993     OrderToBB[RPONumber] = *RI;
2994     BBToOrder[*RI] = RPONumber;
2995     BBNumToRPO[(*RI)->getNumber()] = RPONumber;
2996     ++RPONumber;
2997   }
2998 }
2999 
3000 /// Calculate the liveness information for the given machine function and
3001 /// extend ranges across basic blocks.
3002 bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
3003                                     TargetPassConfig *TPC) {
3004   // No subprogram means this function contains no debuginfo.
3005   if (!MF.getFunction().getSubprogram())
3006     return false;
3007 
3008   LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n");
3009   this->TPC = TPC;
3010 
3011   TRI = MF.getSubtarget().getRegisterInfo();
3012   TII = MF.getSubtarget().getInstrInfo();
3013   TFI = MF.getSubtarget().getFrameLowering();
3014   TFI->getCalleeSaves(MF, CalleeSavedRegs);
3015   LS.initialize(MF);
3016 
3017   MTracker =
3018       new MLocTracker(MF, *TII, *TRI, *MF.getSubtarget().getTargetLowering());
3019   VTracker = nullptr;
3020   TTracker = nullptr;
3021 
3022   SmallVector<MLocTransferMap, 32> MLocTransfer;
3023   SmallVector<VLocTracker, 8> vlocs;
3024   LiveInsT SavedLiveIns;
3025 
3026   int MaxNumBlocks = -1;
3027   for (auto &MBB : MF)
3028     MaxNumBlocks = std::max(MBB.getNumber(), MaxNumBlocks);
3029   assert(MaxNumBlocks >= 0);
3030   ++MaxNumBlocks;
3031 
3032   MLocTransfer.resize(MaxNumBlocks);
3033   vlocs.resize(MaxNumBlocks);
3034   SavedLiveIns.resize(MaxNumBlocks);
3035 
3036   initialSetup(MF);
3037 
3038   produceTransferFunctions(MF, MLocTransfer, MaxNumBlocks, vlocs);
3039 
3040   // Allocate and initialize two array-of-arrays for the live-in and live-out
3041   // machine values. The outer dimension is the block number; while the inner
3042   // dimension is a LocIdx from MLocTracker.
3043   ValueIDNum **MOutLocs = new ValueIDNum *[MaxNumBlocks];
3044   ValueIDNum **MInLocs = new ValueIDNum *[MaxNumBlocks];
3045   unsigned NumLocs = MTracker->getNumLocs();
3046   for (int i = 0; i < MaxNumBlocks; ++i) {
3047     MOutLocs[i] = new ValueIDNum[NumLocs];
3048     MInLocs[i] = new ValueIDNum[NumLocs];
3049   }
3050 
3051   // Solve the machine value dataflow problem using the MLocTransfer function,
3052   // storing the computed live-ins / live-outs into the array-of-arrays. We use
3053   // both live-ins and live-outs for decision making in the variable value
3054   // dataflow problem.
3055   mlocDataflow(MInLocs, MOutLocs, MLocTransfer);
3056 
3057   // Number all variables in the order that they appear, to be used as a stable
3058   // insertion order later.
3059   DenseMap<DebugVariable, unsigned> AllVarsNumbering;
3060 
3061   // Map from one LexicalScope to all the variables in that scope.
3062   DenseMap<const LexicalScope *, SmallSet<DebugVariable, 4>> ScopeToVars;
3063 
3064   // Map from One lexical scope to all blocks in that scope.
3065   DenseMap<const LexicalScope *, SmallPtrSet<MachineBasicBlock *, 4>>
3066       ScopeToBlocks;
3067 
3068   // Store a DILocation that describes a scope.
3069   DenseMap<const LexicalScope *, const DILocation *> ScopeToDILocation;
3070 
3071   // To mirror old LiveDebugValues, enumerate variables in RPOT order. Otherwise
3072   // the order is unimportant, it just has to be stable.
3073   for (unsigned int I = 0; I < OrderToBB.size(); ++I) {
3074     auto *MBB = OrderToBB[I];
3075     auto *VTracker = &vlocs[MBB->getNumber()];
3076     // Collect each variable with a DBG_VALUE in this block.
3077     for (auto &idx : VTracker->Vars) {
3078       const auto &Var = idx.first;
3079       const DILocation *ScopeLoc = VTracker->Scopes[Var];
3080       assert(ScopeLoc != nullptr);
3081       auto *Scope = LS.findLexicalScope(ScopeLoc);
3082 
3083       // No insts in scope -> shouldn't have been recorded.
3084       assert(Scope != nullptr);
3085 
3086       AllVarsNumbering.insert(std::make_pair(Var, AllVarsNumbering.size()));
3087       ScopeToVars[Scope].insert(Var);
3088       ScopeToBlocks[Scope].insert(VTracker->MBB);
3089       ScopeToDILocation[Scope] = ScopeLoc;
3090     }
3091   }
3092 
3093   // OK. Iterate over scopes: there might be something to be said for
3094   // ordering them by size/locality, but that's for the future. For each scope,
3095   // solve the variable value problem, producing a map of variables to values
3096   // in SavedLiveIns.
3097   for (auto &P : ScopeToVars) {
3098     vlocDataflow(P.first, ScopeToDILocation[P.first], P.second,
3099                  ScopeToBlocks[P.first], SavedLiveIns, MOutLocs, MInLocs,
3100                  vlocs);
3101   }
3102 
3103   // Using the computed value locations and variable values for each block,
3104   // create the DBG_VALUE instructions representing the extended variable
3105   // locations.
3106   emitLocations(MF, SavedLiveIns, MInLocs, AllVarsNumbering);
3107 
3108   for (int Idx = 0; Idx < MaxNumBlocks; ++Idx) {
3109     delete[] MOutLocs[Idx];
3110     delete[] MInLocs[Idx];
3111   }
3112   delete[] MOutLocs;
3113   delete[] MInLocs;
3114 
3115   // Did we actually make any changes? If we created any DBG_VALUEs, then yes.
3116   bool Changed = TTracker->Transfers.size() != 0;
3117 
3118   delete MTracker;
3119   delete TTracker;
3120   MTracker = nullptr;
3121   VTracker = nullptr;
3122   TTracker = nullptr;
3123 
3124   ArtificialBlocks.clear();
3125   OrderToBB.clear();
3126   BBToOrder.clear();
3127   BBNumToRPO.clear();
3128 
3129   return Changed;
3130 }
3131 
3132 LDVImpl *llvm::makeInstrRefBasedLiveDebugValues() {
3133   return new InstrRefBasedLDV();
3134 }
3135