xref: /llvm-project/llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp (revision 01702c3f7f1aa3a0e68f638bd829f7b4a8b40fa3)
1 #include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
2 #include "LiveDebugValues/LiveDebugValues.h"
3 #include "llvm/ADT/BitVector.h"
4 #include "llvm/ADT/DenseMapInfo.h"
5 #include "llvm/ADT/IntervalMap.h"
6 #include "llvm/ADT/PostOrderIterator.h"
7 #include "llvm/ADT/STLExtras.h"
8 #include "llvm/ADT/Statistic.h"
9 #include "llvm/ADT/UniqueVector.h"
10 #include "llvm/Analysis/Interval.h"
11 #include "llvm/BinaryFormat/Dwarf.h"
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/DebugInfo.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/Instruction.h"
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/IR/PrintPasses.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
25 #include <assert.h>
26 #include <cstdint>
27 #include <optional>
28 #include <sstream>
29 #include <unordered_map>
30 
31 using namespace llvm;
32 #define DEBUG_TYPE "debug-ata"
33 
34 STATISTIC(NumDefsScanned, "Number of dbg locs that get scanned for removal");
35 STATISTIC(NumDefsRemoved, "Number of dbg locs removed");
36 STATISTIC(NumWedgesScanned, "Number of dbg wedges scanned");
37 STATISTIC(NumWedgesChanged, "Number of dbg wedges changed");
38 
39 static cl::opt<unsigned>
40     MaxNumBlocks("debug-ata-max-blocks", cl::init(10000),
41                  cl::desc("Maximum num basic blocks before debug info dropped"),
42                  cl::Hidden);
43 /// Option for debugging the pass, determines if the memory location fragment
44 /// filling happens after generating the variable locations.
45 static cl::opt<bool> EnableMemLocFragFill("mem-loc-frag-fill", cl::init(true),
46                                           cl::Hidden);
47 /// Print the results of the analysis. Respects -filter-print-funcs.
48 static cl::opt<bool> PrintResults("print-debug-ata", cl::init(false),
49                                   cl::Hidden);
50 
51 /// Coalesce adjacent dbg locs describing memory locations that have contiguous
52 /// fragments. This reduces the cost of LiveDebugValues which does SSA
53 /// construction for each explicitly stated variable fragment.
54 static cl::opt<cl::boolOrDefault>
55     CoalesceAdjacentFragmentsOpt("debug-ata-coalesce-frags", cl::Hidden);
56 
57 // Implicit conversions are disabled for enum class types, so unfortunately we
58 // need to create a DenseMapInfo wrapper around the specified underlying type.
59 template <> struct llvm::DenseMapInfo<VariableID> {
60   using Wrapped = DenseMapInfo<unsigned>;
61   static inline VariableID getEmptyKey() {
62     return static_cast<VariableID>(Wrapped::getEmptyKey());
63   }
64   static inline VariableID getTombstoneKey() {
65     return static_cast<VariableID>(Wrapped::getTombstoneKey());
66   }
67   static unsigned getHashValue(const VariableID &Val) {
68     return Wrapped::getHashValue(static_cast<unsigned>(Val));
69   }
70   static bool isEqual(const VariableID &LHS, const VariableID &RHS) {
71     return LHS == RHS;
72   }
73 };
74 
75 /// Helper class to build FunctionVarLocs, since that class isn't easy to
76 /// modify. TODO: There's not a great deal of value in the split, it could be
77 /// worth merging the two classes.
78 class FunctionVarLocsBuilder {
79   friend FunctionVarLocs;
80   UniqueVector<DebugVariable> Variables;
81   // Use an unordered_map so we don't invalidate iterators after
82   // insert/modifications.
83   std::unordered_map<const Instruction *, SmallVector<VarLocInfo>>
84       VarLocsBeforeInst;
85 
86   SmallVector<VarLocInfo> SingleLocVars;
87 
88 public:
89   unsigned getNumVariables() const { return Variables.size(); }
90 
91   /// Find or insert \p V and return the ID.
92   VariableID insertVariable(DebugVariable V) {
93     return static_cast<VariableID>(Variables.insert(V));
94   }
95 
96   /// Get a variable from its \p ID.
97   const DebugVariable &getVariable(VariableID ID) const {
98     return Variables[static_cast<unsigned>(ID)];
99   }
100 
101   /// Return ptr to wedge of defs or nullptr if no defs come just before /p
102   /// Before.
103   const SmallVectorImpl<VarLocInfo> *getWedge(const Instruction *Before) const {
104     auto R = VarLocsBeforeInst.find(Before);
105     if (R == VarLocsBeforeInst.end())
106       return nullptr;
107     return &R->second;
108   }
109 
110   /// Replace the defs that come just before /p Before with /p Wedge.
111   void setWedge(const Instruction *Before, SmallVector<VarLocInfo> &&Wedge) {
112     VarLocsBeforeInst[Before] = std::move(Wedge);
113   }
114 
115   /// Add a def for a variable that is valid for its lifetime.
116   void addSingleLocVar(DebugVariable Var, DIExpression *Expr, DebugLoc DL,
117                        RawLocationWrapper R) {
118     VarLocInfo VarLoc;
119     VarLoc.VariableID = insertVariable(Var);
120     VarLoc.Expr = Expr;
121     VarLoc.DL = DL;
122     VarLoc.Values = R;
123     SingleLocVars.emplace_back(VarLoc);
124   }
125 
126   /// Add a def to the wedge of defs just before /p Before.
127   void addVarLoc(Instruction *Before, DebugVariable Var, DIExpression *Expr,
128                  DebugLoc DL, RawLocationWrapper R) {
129     VarLocInfo VarLoc;
130     VarLoc.VariableID = insertVariable(Var);
131     VarLoc.Expr = Expr;
132     VarLoc.DL = DL;
133     VarLoc.Values = R;
134     VarLocsBeforeInst[Before].emplace_back(VarLoc);
135   }
136 };
137 
138 void FunctionVarLocs::print(raw_ostream &OS, const Function &Fn) const {
139   // Print the variable table first. TODO: Sorting by variable could make the
140   // output more stable?
141   unsigned Counter = -1;
142   OS << "=== Variables ===\n";
143   for (const DebugVariable &V : Variables) {
144     ++Counter;
145     // Skip first entry because it is a dummy entry.
146     if (Counter == 0) {
147       continue;
148     }
149     OS << "[" << Counter << "] " << V.getVariable()->getName();
150     if (auto F = V.getFragment())
151       OS << " bits [" << F->OffsetInBits << ", "
152          << F->OffsetInBits + F->SizeInBits << ")";
153     if (const auto *IA = V.getInlinedAt())
154       OS << " inlined-at " << *IA;
155     OS << "\n";
156   }
157 
158   auto PrintLoc = [&OS](const VarLocInfo &Loc) {
159     OS << "DEF Var=[" << (unsigned)Loc.VariableID << "]"
160        << " Expr=" << *Loc.Expr << " Values=(";
161     for (auto *Op : Loc.Values.location_ops()) {
162       errs() << Op->getName() << " ";
163     }
164     errs() << ")\n";
165   };
166 
167   // Print the single location variables.
168   OS << "=== Single location vars ===\n";
169   for (auto It = single_locs_begin(), End = single_locs_end(); It != End;
170        ++It) {
171     PrintLoc(*It);
172   }
173 
174   // Print the non-single-location defs in line with IR.
175   OS << "=== In-line variable defs ===";
176   for (const BasicBlock &BB : Fn) {
177     OS << "\n" << BB.getName() << ":\n";
178     for (const Instruction &I : BB) {
179       for (auto It = locs_begin(&I), End = locs_end(&I); It != End; ++It) {
180         PrintLoc(*It);
181       }
182       OS << I << "\n";
183     }
184   }
185 }
186 
187 void FunctionVarLocs::init(FunctionVarLocsBuilder &Builder) {
188   // Add the single-location variables first.
189   for (const auto &VarLoc : Builder.SingleLocVars)
190     VarLocRecords.emplace_back(VarLoc);
191   // Mark the end of the section.
192   SingleVarLocEnd = VarLocRecords.size();
193 
194   // Insert a contiguous block of VarLocInfos for each instruction, mapping it
195   // to the start and end position in the vector with VarLocsBeforeInst.
196   for (auto &P : Builder.VarLocsBeforeInst) {
197     unsigned BlockStart = VarLocRecords.size();
198     for (const VarLocInfo &VarLoc : P.second)
199       VarLocRecords.emplace_back(VarLoc);
200     unsigned BlockEnd = VarLocRecords.size();
201     // Record the start and end indices.
202     if (BlockEnd != BlockStart)
203       VarLocsBeforeInst[P.first] = {BlockStart, BlockEnd};
204   }
205 
206   // Copy the Variables vector from the builder's UniqueVector.
207   assert(Variables.empty() && "Expect clear before init");
208   // UniqueVectors IDs are one-based (which means the VarLocInfo VarID values
209   // are one-based) so reserve an extra and insert a dummy.
210   Variables.reserve(Builder.Variables.size() + 1);
211   Variables.push_back(DebugVariable(nullptr, std::nullopt, nullptr));
212   Variables.append(Builder.Variables.begin(), Builder.Variables.end());
213 }
214 
215 void FunctionVarLocs::clear() {
216   Variables.clear();
217   VarLocRecords.clear();
218   VarLocsBeforeInst.clear();
219   SingleVarLocEnd = 0;
220 }
221 
222 /// Walk backwards along constant GEPs and bitcasts to the base storage from \p
223 /// Start as far as possible. Prepend \Expression with the offset and append it
224 /// with a DW_OP_deref that haes been implicit until now. Returns the walked-to
225 /// value and modified expression.
226 static std::pair<Value *, DIExpression *>
227 walkToAllocaAndPrependOffsetDeref(const DataLayout &DL, Value *Start,
228                                   DIExpression *Expression) {
229   APInt OffsetInBytes(DL.getTypeSizeInBits(Start->getType()), false);
230   Value *End =
231       Start->stripAndAccumulateInBoundsConstantOffsets(DL, OffsetInBytes);
232   SmallVector<uint64_t, 3> Ops;
233   if (OffsetInBytes.getBoolValue()) {
234     Ops = {dwarf::DW_OP_plus_uconst, OffsetInBytes.getZExtValue()};
235     Expression = DIExpression::prependOpcodes(
236         Expression, Ops, /*StackValue=*/false, /*EntryValue=*/false);
237   }
238   Expression = DIExpression::append(Expression, {dwarf::DW_OP_deref});
239   return {End, Expression};
240 }
241 
242 /// Extract the offset used in \p DIExpr. Returns std::nullopt if the expression
243 /// doesn't explicitly describe a memory location with DW_OP_deref or if the
244 /// expression is too complex to interpret.
245 static std::optional<int64_t>
246 getDerefOffsetInBytes(const DIExpression *DIExpr) {
247   int64_t Offset = 0;
248   const unsigned NumElements = DIExpr->getNumElements();
249   const auto Elements = DIExpr->getElements();
250   unsigned ExpectedDerefIdx = 0;
251   // Extract the offset.
252   if (NumElements > 2 && Elements[0] == dwarf::DW_OP_plus_uconst) {
253     Offset = Elements[1];
254     ExpectedDerefIdx = 2;
255   } else if (NumElements > 3 && Elements[0] == dwarf::DW_OP_constu) {
256     ExpectedDerefIdx = 3;
257     if (Elements[2] == dwarf::DW_OP_plus)
258       Offset = Elements[1];
259     else if (Elements[2] == dwarf::DW_OP_minus)
260       Offset = -Elements[1];
261     else
262       return std::nullopt;
263   }
264 
265   // If that's all there is it means there's no deref.
266   if (ExpectedDerefIdx >= NumElements)
267     return std::nullopt;
268 
269   // Check the next element is DW_OP_deref - otherwise this is too complex or
270   // isn't a deref expression.
271   if (Elements[ExpectedDerefIdx] != dwarf::DW_OP_deref)
272     return std::nullopt;
273 
274   // Check the final operation is either the DW_OP_deref or is a fragment.
275   if (NumElements == ExpectedDerefIdx + 1)
276     return Offset; // Ends with deref.
277   unsigned ExpectedFragFirstIdx = ExpectedDerefIdx + 1;
278   unsigned ExpectedFragFinalIdx = ExpectedFragFirstIdx + 2;
279   if (NumElements == ExpectedFragFinalIdx + 1 &&
280       Elements[ExpectedFragFirstIdx] == dwarf::DW_OP_LLVM_fragment)
281     return Offset; // Ends with deref + fragment.
282 
283   // Don't bother trying to interpret anything more complex.
284   return std::nullopt;
285 }
286 
287 /// A whole (unfragmented) source variable.
288 using DebugAggregate = std::pair<const DILocalVariable *, const DILocation *>;
289 static DebugAggregate getAggregate(const DbgVariableIntrinsic *DII) {
290   return DebugAggregate(DII->getVariable(), DII->getDebugLoc().getInlinedAt());
291 }
292 static DebugAggregate getAggregate(const DebugVariable &Var) {
293   return DebugAggregate(Var.getVariable(), Var.getInlinedAt());
294 }
295 
296 static bool shouldCoalesceFragments(Function &F) {
297   // Enabling fragment coalescing reduces compiler run time when instruction
298   // referencing is enabled. However, it may cause LiveDebugVariables to create
299   // incorrect locations. Since instruction-referencing mode effectively
300   // bypasses LiveDebugVariables we only enable coalescing if the cl::opt flag
301   // has not been explicitly set and instruction-referencing is turned on.
302   switch (CoalesceAdjacentFragmentsOpt) {
303   case cl::boolOrDefault::BOU_UNSET:
304     return debuginfoShouldUseDebugInstrRef(
305         Triple(F.getParent()->getTargetTriple()));
306   case cl::boolOrDefault::BOU_TRUE:
307     return true;
308   case cl::boolOrDefault::BOU_FALSE:
309     return false;
310   }
311   llvm_unreachable("Unknown boolOrDefault value");
312 }
313 
314 namespace {
315 /// In dwarf emission, the following sequence
316 ///    1. dbg.value ... Fragment(0, 64)
317 ///    2. dbg.value ... Fragment(0, 32)
318 /// effectively sets Fragment(32, 32) to undef (each def sets all bits not in
319 /// the intersection of the fragments to having "no location"). This makes
320 /// sense for implicit location values because splitting the computed values
321 /// could be troublesome, and is probably quite uncommon.  When we convert
322 /// dbg.assigns to dbg.value+deref this kind of thing is common, and describing
323 /// a location (memory) rather than a value means we don't need to worry about
324 /// splitting any values, so we try to recover the rest of the fragment
325 /// location here.
326 /// This class performs a(nother) dataflow analysis over the function, adding
327 /// variable locations so that any bits of a variable with a memory location
328 /// have that location explicitly reinstated at each subsequent variable
329 /// location definition that that doesn't overwrite those bits. i.e. after a
330 /// variable location def, insert new defs for the memory location with
331 /// fragments for the difference of "all bits currently in memory" and "the
332 /// fragment of the second def".
333 class MemLocFragmentFill {
334   Function &Fn;
335   FunctionVarLocsBuilder *FnVarLocs;
336   const DenseSet<DebugAggregate> *VarsWithStackSlot;
337   bool CoalesceAdjacentFragments;
338 
339   // 0 = no memory location.
340   using BaseAddress = unsigned;
341   using OffsetInBitsTy = unsigned;
342   using FragTraits = IntervalMapHalfOpenInfo<OffsetInBitsTy>;
343   using FragsInMemMap = IntervalMap<
344       OffsetInBitsTy, BaseAddress,
345       IntervalMapImpl::NodeSizer<OffsetInBitsTy, BaseAddress>::LeafSize,
346       FragTraits>;
347   FragsInMemMap::Allocator IntervalMapAlloc;
348   using VarFragMap = DenseMap<unsigned, FragsInMemMap>;
349 
350   /// IDs for memory location base addresses in maps. Use 0 to indicate that
351   /// there's no memory location.
352   UniqueVector<RawLocationWrapper> Bases;
353   UniqueVector<DebugAggregate> Aggregates;
354   DenseMap<const BasicBlock *, VarFragMap> LiveIn;
355   DenseMap<const BasicBlock *, VarFragMap> LiveOut;
356 
357   struct FragMemLoc {
358     unsigned Var;
359     unsigned Base;
360     unsigned OffsetInBits;
361     unsigned SizeInBits;
362     DebugLoc DL;
363   };
364   using InsertMap = MapVector<Instruction *, SmallVector<FragMemLoc>>;
365 
366   /// BBInsertBeforeMap holds a description for the set of location defs to be
367   /// inserted after the analysis is complete. It is updated during the dataflow
368   /// and the entry for a block is CLEARED each time it is (re-)visited. After
369   /// the dataflow is complete, each block entry will contain the set of defs
370   /// calculated during the final (fixed-point) iteration.
371   DenseMap<const BasicBlock *, InsertMap> BBInsertBeforeMap;
372 
373   static bool intervalMapsAreEqual(const FragsInMemMap &A,
374                                    const FragsInMemMap &B) {
375     auto AIt = A.begin(), AEnd = A.end();
376     auto BIt = B.begin(), BEnd = B.end();
377     for (; AIt != AEnd; ++AIt, ++BIt) {
378       if (BIt == BEnd)
379         return false; // B has fewer elements than A.
380       if (AIt.start() != BIt.start() || AIt.stop() != BIt.stop())
381         return false; // Interval is different.
382       if (*AIt != *BIt)
383         return false; // Value at interval is different.
384     }
385     // AIt == AEnd. Check BIt is also now at end.
386     return BIt == BEnd;
387   }
388 
389   static bool varFragMapsAreEqual(const VarFragMap &A, const VarFragMap &B) {
390     if (A.size() != B.size())
391       return false;
392     for (const auto &APair : A) {
393       auto BIt = B.find(APair.first);
394       if (BIt == B.end())
395         return false;
396       if (!intervalMapsAreEqual(APair.second, BIt->second))
397         return false;
398     }
399     return true;
400   }
401 
402   /// Return a string for the value that \p BaseID represents.
403   std::string toString(unsigned BaseID) {
404     if (BaseID)
405       return Bases[BaseID].getVariableLocationOp(0)->getName().str();
406     else
407       return "None";
408   }
409 
410   /// Format string describing an FragsInMemMap (IntervalMap) interval.
411   std::string toString(FragsInMemMap::const_iterator It, bool Newline = true) {
412     std::string String;
413     std::stringstream S(String);
414     if (It.valid()) {
415       S << "[" << It.start() << ", " << It.stop()
416         << "): " << toString(It.value());
417     } else {
418       S << "invalid iterator (end)";
419     }
420     if (Newline)
421       S << "\n";
422     return S.str();
423   };
424 
425   FragsInMemMap meetFragments(const FragsInMemMap &A, const FragsInMemMap &B) {
426     FragsInMemMap Result(IntervalMapAlloc);
427     for (auto AIt = A.begin(), AEnd = A.end(); AIt != AEnd; ++AIt) {
428       LLVM_DEBUG(dbgs() << "a " << toString(AIt));
429       // This is basically copied from process() and inverted (process is
430       // performing something like a union whereas this is more of an
431       // intersect).
432 
433       // There's no work to do if interval `a` overlaps no fragments in map `B`.
434       if (!B.overlaps(AIt.start(), AIt.stop()))
435         continue;
436 
437       // Does StartBit intersect an existing fragment?
438       auto FirstOverlap = B.find(AIt.start());
439       assert(FirstOverlap != B.end());
440       bool IntersectStart = FirstOverlap.start() < AIt.start();
441       LLVM_DEBUG(dbgs() << "- FirstOverlap " << toString(FirstOverlap, false)
442                         << ", IntersectStart: " << IntersectStart << "\n");
443 
444       // Does EndBit intersect an existing fragment?
445       auto LastOverlap = B.find(AIt.stop());
446       bool IntersectEnd =
447           LastOverlap != B.end() && LastOverlap.start() < AIt.stop();
448       LLVM_DEBUG(dbgs() << "- LastOverlap " << toString(LastOverlap, false)
449                         << ", IntersectEnd: " << IntersectEnd << "\n");
450 
451       // Check if both ends of `a` intersect the same interval `b`.
452       if (IntersectStart && IntersectEnd && FirstOverlap == LastOverlap) {
453         // Insert `a` (`a` is contained in `b`) if the values match.
454         // [ a ]
455         // [ - b - ]
456         // -
457         // [ r ]
458         LLVM_DEBUG(dbgs() << "- a is contained within "
459                           << toString(FirstOverlap));
460         if (*AIt && *AIt == *FirstOverlap)
461           Result.insert(AIt.start(), AIt.stop(), *AIt);
462       } else {
463         // There's an overlap but `a` is not fully contained within
464         // `b`. Shorten any end-point intersections.
465         //     [ - a - ]
466         // [ - b - ]
467         // -
468         //     [ r ]
469         auto Next = FirstOverlap;
470         if (IntersectStart) {
471           LLVM_DEBUG(dbgs() << "- insert intersection of a and "
472                             << toString(FirstOverlap));
473           if (*AIt && *AIt == *FirstOverlap)
474             Result.insert(AIt.start(), FirstOverlap.stop(), *AIt);
475           ++Next;
476         }
477         // [ - a - ]
478         //     [ - b - ]
479         // -
480         //     [ r ]
481         if (IntersectEnd) {
482           LLVM_DEBUG(dbgs() << "- insert intersection of a and "
483                             << toString(LastOverlap));
484           if (*AIt && *AIt == *LastOverlap)
485             Result.insert(LastOverlap.start(), AIt.stop(), *AIt);
486         }
487 
488         // Insert all intervals in map `B` that are contained within interval
489         // `a` where the values match.
490         // [ -  - a -  - ]
491         // [ b1 ]   [ b2 ]
492         // -
493         // [ r1 ]   [ r2 ]
494         while (Next != B.end() && Next.start() < AIt.stop() &&
495                Next.stop() <= AIt.stop()) {
496           LLVM_DEBUG(dbgs()
497                      << "- insert intersection of a and " << toString(Next));
498           if (*AIt && *AIt == *Next)
499             Result.insert(Next.start(), Next.stop(), *Next);
500           ++Next;
501         }
502       }
503     }
504     return Result;
505   }
506 
507   /// Meet \p A and \p B, storing the result in \p A.
508   void meetVars(VarFragMap &A, const VarFragMap &B) {
509     // Meet A and B.
510     //
511     // Result = meet(a, b) for a in A, b in B where Var(a) == Var(b)
512     for (auto It = A.begin(), End = A.end(); It != End; ++It) {
513       unsigned AVar = It->first;
514       FragsInMemMap &AFrags = It->second;
515       auto BIt = B.find(AVar);
516       if (BIt == B.end()) {
517         A.erase(It);
518         continue; // Var has no bits defined in B.
519       }
520       LLVM_DEBUG(dbgs() << "meet fragment maps for "
521                         << Aggregates[AVar].first->getName() << "\n");
522       AFrags = meetFragments(AFrags, BIt->second);
523     }
524   }
525 
526   bool meet(const BasicBlock &BB,
527             const SmallPtrSet<BasicBlock *, 16> &Visited) {
528     LLVM_DEBUG(dbgs() << "meet block info from preds of " << BB.getName()
529                       << "\n");
530 
531     VarFragMap BBLiveIn;
532     bool FirstMeet = true;
533     // LiveIn locs for BB is the meet of the already-processed preds' LiveOut
534     // locs.
535     for (auto I = pred_begin(&BB), E = pred_end(&BB); I != E; I++) {
536       // Ignore preds that haven't been processed yet. This is essentially the
537       // same as initialising all variables to implicit top value (⊤) which is
538       // the identity value for the meet operation.
539       const BasicBlock *Pred = *I;
540       if (!Visited.count(Pred))
541         continue;
542 
543       auto PredLiveOut = LiveOut.find(Pred);
544       assert(PredLiveOut != LiveOut.end());
545 
546       if (FirstMeet) {
547         LLVM_DEBUG(dbgs() << "BBLiveIn = " << Pred->getName() << "\n");
548         BBLiveIn = PredLiveOut->second;
549         FirstMeet = false;
550       } else {
551         LLVM_DEBUG(dbgs() << "BBLiveIn = meet BBLiveIn, " << Pred->getName()
552                           << "\n");
553         meetVars(BBLiveIn, PredLiveOut->second);
554       }
555 
556       // An empty set is ⊥ for the intersect-like meet operation. If we've
557       // already got ⊥ there's no need to run the code - we know the result is
558       // ⊥ since `meet(a, ⊥) = ⊥`.
559       if (BBLiveIn.size() == 0)
560         break;
561     }
562 
563     auto CurrentLiveInEntry = LiveIn.find(&BB);
564     // If there's no LiveIn entry for the block yet, add it.
565     if (CurrentLiveInEntry == LiveIn.end()) {
566       LLVM_DEBUG(dbgs() << "change=true (first) on meet on " << BB.getName()
567                         << "\n");
568       LiveIn[&BB] = std::move(BBLiveIn);
569       return /*Changed=*/true;
570     }
571 
572     // If the LiveIn set has changed (expensive check) update it and return
573     // true.
574     if (!varFragMapsAreEqual(BBLiveIn, CurrentLiveInEntry->second)) {
575       LLVM_DEBUG(dbgs() << "change=true on meet on " << BB.getName() << "\n");
576       CurrentLiveInEntry->second = std::move(BBLiveIn);
577       return /*Changed=*/true;
578     }
579 
580     LLVM_DEBUG(dbgs() << "change=false on meet on " << BB.getName() << "\n");
581     return /*Changed=*/false;
582   }
583 
584   void insertMemLoc(BasicBlock &BB, Instruction &Before, unsigned Var,
585                     unsigned StartBit, unsigned EndBit, unsigned Base,
586                     DebugLoc DL) {
587     assert(StartBit < EndBit && "Cannot create fragment of size <= 0");
588     if (!Base)
589       return;
590     FragMemLoc Loc;
591     Loc.Var = Var;
592     Loc.OffsetInBits = StartBit;
593     Loc.SizeInBits = EndBit - StartBit;
594     assert(Base && "Expected a non-zero ID for Base address");
595     Loc.Base = Base;
596     Loc.DL = DL;
597     BBInsertBeforeMap[&BB][&Before].push_back(Loc);
598     LLVM_DEBUG(dbgs() << "Add mem def for " << Aggregates[Var].first->getName()
599                       << " bits [" << StartBit << ", " << EndBit << ")\n");
600   }
601 
602   /// Inserts a new dbg def if the interval found when looking up \p StartBit
603   /// in \p FragMap starts before \p StartBit or ends after \p EndBit (which
604   /// indicates - assuming StartBit->EndBit has just been inserted - that the
605   /// slice has been coalesced in the map).
606   void coalesceFragments(BasicBlock &BB, Instruction &Before, unsigned Var,
607                          unsigned StartBit, unsigned EndBit, unsigned Base,
608                          DebugLoc DL, const FragsInMemMap &FragMap) {
609     if (!CoalesceAdjacentFragments)
610       return;
611     // We've inserted the location into the map. The map will have coalesced
612     // adjacent intervals (variable fragments) that describe the same memory
613     // location. Use this knowledge to insert a debug location that describes
614     // that coalesced fragment. This may eclipse other locs we've just
615     // inserted. This is okay as redundant locs will be cleaned up later.
616     auto CoalescedFrag = FragMap.find(StartBit);
617     // Bail if no coalescing has taken place.
618     if (CoalescedFrag.start() == StartBit && CoalescedFrag.stop() == EndBit)
619       return;
620 
621     LLVM_DEBUG(dbgs() << "- Insert loc for bits " << CoalescedFrag.start()
622                       << " to " << CoalescedFrag.stop() << "\n");
623     insertMemLoc(BB, Before, Var, CoalescedFrag.start(), CoalescedFrag.stop(),
624                  Base, DL);
625   }
626 
627   void addDef(const VarLocInfo &VarLoc, Instruction &Before, BasicBlock &BB,
628               VarFragMap &LiveSet) {
629     DebugVariable DbgVar = FnVarLocs->getVariable(VarLoc.VariableID);
630     if (skipVariable(DbgVar.getVariable()))
631       return;
632     // Don't bother doing anything for this variables if we know it's fully
633     // promoted. We're only interested in variables that (sometimes) live on
634     // the stack here.
635     if (!VarsWithStackSlot->count(getAggregate(DbgVar)))
636       return;
637     unsigned Var = Aggregates.insert(
638         DebugAggregate(DbgVar.getVariable(), VarLoc.DL.getInlinedAt()));
639 
640     // [StartBit: EndBit) are the bits affected by this def.
641     const DIExpression *DIExpr = VarLoc.Expr;
642     unsigned StartBit;
643     unsigned EndBit;
644     if (auto Frag = DIExpr->getFragmentInfo()) {
645       StartBit = Frag->OffsetInBits;
646       EndBit = StartBit + Frag->SizeInBits;
647     } else {
648       assert(static_cast<bool>(DbgVar.getVariable()->getSizeInBits()));
649       StartBit = 0;
650       EndBit = *DbgVar.getVariable()->getSizeInBits();
651     }
652 
653     // We will only fill fragments for simple memory-describing dbg.value
654     // intrinsics. If the fragment offset is the same as the offset from the
655     // base pointer, do The Thing, otherwise fall back to normal dbg.value
656     // behaviour. AssignmentTrackingLowering has generated DIExpressions
657     // written in terms of the base pointer.
658     // TODO: Remove this condition since the fragment offset doesn't always
659     // equal the offset from base pointer (e.g. for a SROA-split variable).
660     const auto DerefOffsetInBytes = getDerefOffsetInBytes(DIExpr);
661     const unsigned Base =
662         DerefOffsetInBytes && *DerefOffsetInBytes * 8 == StartBit
663             ? Bases.insert(VarLoc.Values)
664             : 0;
665     LLVM_DEBUG(dbgs() << "DEF " << DbgVar.getVariable()->getName() << " ["
666                       << StartBit << ", " << EndBit << "): " << toString(Base)
667                       << "\n");
668 
669     // First of all, any locs that use mem that are disrupted need reinstating.
670     // Unfortunately, IntervalMap doesn't let us insert intervals that overlap
671     // with existing intervals so this code involves a lot of fiddling around
672     // with intervals to do that manually.
673     auto FragIt = LiveSet.find(Var);
674 
675     // Check if the variable does not exist in the map.
676     if (FragIt == LiveSet.end()) {
677       // Add this variable to the BB map.
678       auto P = LiveSet.try_emplace(Var, FragsInMemMap(IntervalMapAlloc));
679       assert(P.second && "Var already in map?");
680       // Add the interval to the fragment map.
681       P.first->second.insert(StartBit, EndBit, Base);
682       return;
683     }
684     // The variable has an entry in the map.
685 
686     FragsInMemMap &FragMap = FragIt->second;
687     // First check the easy case: the new fragment `f` doesn't overlap with any
688     // intervals.
689     if (!FragMap.overlaps(StartBit, EndBit)) {
690       LLVM_DEBUG(dbgs() << "- No overlaps\n");
691       FragMap.insert(StartBit, EndBit, Base);
692       coalesceFragments(BB, Before, Var, StartBit, EndBit, Base, VarLoc.DL,
693                         FragMap);
694       return;
695     }
696     // There is at least one overlap.
697 
698     // Does StartBit intersect an existing fragment?
699     auto FirstOverlap = FragMap.find(StartBit);
700     assert(FirstOverlap != FragMap.end());
701     bool IntersectStart = FirstOverlap.start() < StartBit;
702 
703     // Does EndBit intersect an existing fragment?
704     auto LastOverlap = FragMap.find(EndBit);
705     bool IntersectEnd = LastOverlap.valid() && LastOverlap.start() < EndBit;
706 
707     // Check if both ends of `f` intersect the same interval `i`.
708     if (IntersectStart && IntersectEnd && FirstOverlap == LastOverlap) {
709       LLVM_DEBUG(dbgs() << "- Intersect single interval @ both ends\n");
710       // Shorten `i` so that there's space to insert `f`.
711       //      [ f ]
712       // [  -   i   -  ]
713       // +
714       // [ i ][ f ][ i ]
715 
716       // Save values for use after inserting a new interval.
717       auto EndBitOfOverlap = FirstOverlap.stop();
718       unsigned OverlapValue = FirstOverlap.value();
719 
720       // Shorten the overlapping interval.
721       FirstOverlap.setStop(StartBit);
722       insertMemLoc(BB, Before, Var, FirstOverlap.start(), StartBit,
723                    OverlapValue, VarLoc.DL);
724 
725       // Insert a new interval to represent the end part.
726       FragMap.insert(EndBit, EndBitOfOverlap, OverlapValue);
727       insertMemLoc(BB, Before, Var, EndBit, EndBitOfOverlap, OverlapValue,
728                    VarLoc.DL);
729 
730       // Insert the new (middle) fragment now there is space.
731       FragMap.insert(StartBit, EndBit, Base);
732     } else {
733       // There's an overlap but `f` may not be fully contained within
734       // `i`. Shorten any end-point intersections so that we can then
735       // insert `f`.
736       //      [ - f - ]
737       // [ - i - ]
738       // |   |
739       // [ i ]
740       // Shorten any end-point intersections.
741       if (IntersectStart) {
742         LLVM_DEBUG(dbgs() << "- Intersect interval at start\n");
743         // Split off at the intersection.
744         FirstOverlap.setStop(StartBit);
745         insertMemLoc(BB, Before, Var, FirstOverlap.start(), StartBit,
746                      *FirstOverlap, VarLoc.DL);
747       }
748       // [ - f - ]
749       //      [ - i - ]
750       //          |   |
751       //          [ i ]
752       if (IntersectEnd) {
753         LLVM_DEBUG(dbgs() << "- Intersect interval at end\n");
754         // Split off at the intersection.
755         LastOverlap.setStart(EndBit);
756         insertMemLoc(BB, Before, Var, EndBit, LastOverlap.stop(), *LastOverlap,
757                      VarLoc.DL);
758       }
759 
760       LLVM_DEBUG(dbgs() << "- Erase intervals contained within\n");
761       // FirstOverlap and LastOverlap have been shortened such that they're
762       // no longer overlapping with [StartBit, EndBit). Delete any overlaps
763       // that remain (these will be fully contained within `f`).
764       // [ - f - ]       }
765       //      [ - i - ]  } Intersection shortening that has happened above.
766       //          |   |  }
767       //          [ i ]  }
768       // -----------------
769       // [i2 ]           } Intervals fully contained within `f` get erased.
770       // -----------------
771       // [ - f - ][ i ]  } Completed insertion.
772       auto It = FirstOverlap;
773       if (IntersectStart)
774         ++It; // IntersectStart: first overlap has been shortened.
775       while (It.valid() && It.start() >= StartBit && It.stop() <= EndBit) {
776         LLVM_DEBUG(dbgs() << "- Erase " << toString(It));
777         It.erase(); // This increments It after removing the interval.
778       }
779       // We've dealt with all the overlaps now!
780       assert(!FragMap.overlaps(StartBit, EndBit));
781       LLVM_DEBUG(dbgs() << "- Insert DEF into now-empty space\n");
782       FragMap.insert(StartBit, EndBit, Base);
783     }
784 
785     coalesceFragments(BB, Before, Var, StartBit, EndBit, Base, VarLoc.DL,
786                       FragMap);
787   }
788 
789   bool skipVariable(const DILocalVariable *V) { return !V->getSizeInBits(); }
790 
791   void process(BasicBlock &BB, VarFragMap &LiveSet) {
792     BBInsertBeforeMap[&BB].clear();
793     for (auto &I : BB) {
794       if (const auto *Locs = FnVarLocs->getWedge(&I)) {
795         for (const VarLocInfo &Loc : *Locs) {
796           addDef(Loc, I, *I.getParent(), LiveSet);
797         }
798       }
799     }
800   }
801 
802 public:
803   MemLocFragmentFill(Function &Fn,
804                      const DenseSet<DebugAggregate> *VarsWithStackSlot,
805                      bool CoalesceAdjacentFragments)
806       : Fn(Fn), VarsWithStackSlot(VarsWithStackSlot),
807         CoalesceAdjacentFragments(CoalesceAdjacentFragments) {}
808 
809   /// Add variable locations to \p FnVarLocs so that any bits of a variable
810   /// with a memory location have that location explicitly reinstated at each
811   /// subsequent variable location definition that that doesn't overwrite those
812   /// bits. i.e. after a variable location def, insert new defs for the memory
813   /// location with fragments for the difference of "all bits currently in
814   /// memory" and "the fragment of the second def". e.g.
815   ///
816   ///     Before:
817   ///
818   ///     var x bits 0 to 63:  value in memory
819   ///     more instructions
820   ///     var x bits 0 to 31:  value is %0
821   ///
822   ///     After:
823   ///
824   ///     var x bits 0 to 63:  value in memory
825   ///     more instructions
826   ///     var x bits 0 to 31:  value is %0
827   ///     var x bits 32 to 61: value in memory ; <-- new loc def
828   ///
829   void run(FunctionVarLocsBuilder *FnVarLocs) {
830     if (!EnableMemLocFragFill)
831       return;
832 
833     this->FnVarLocs = FnVarLocs;
834 
835     // Prepare for traversal.
836     //
837     ReversePostOrderTraversal<Function *> RPOT(&Fn);
838     std::priority_queue<unsigned int, std::vector<unsigned int>,
839                         std::greater<unsigned int>>
840         Worklist;
841     std::priority_queue<unsigned int, std::vector<unsigned int>,
842                         std::greater<unsigned int>>
843         Pending;
844     DenseMap<unsigned int, BasicBlock *> OrderToBB;
845     DenseMap<BasicBlock *, unsigned int> BBToOrder;
846     { // Init OrderToBB and BBToOrder.
847       unsigned int RPONumber = 0;
848       for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
849         OrderToBB[RPONumber] = *RI;
850         BBToOrder[*RI] = RPONumber;
851         Worklist.push(RPONumber);
852         ++RPONumber;
853       }
854       LiveIn.init(RPONumber);
855       LiveOut.init(RPONumber);
856     }
857 
858     // Perform the traversal.
859     //
860     // This is a standard "intersect of predecessor outs" dataflow problem. To
861     // solve it, we perform meet() and process() using the two worklist method
862     // until the LiveIn data for each block becomes unchanging.
863     //
864     // This dataflow is essentially working on maps of sets and at each meet we
865     // intersect the maps and the mapped sets. So, initialized live-in maps
866     // monotonically decrease in value throughout the dataflow.
867     SmallPtrSet<BasicBlock *, 16> Visited;
868     while (!Worklist.empty() || !Pending.empty()) {
869       // We track what is on the pending worklist to avoid inserting the same
870       // thing twice.  We could avoid this with a custom priority queue, but
871       // this is probably not worth it.
872       SmallPtrSet<BasicBlock *, 16> OnPending;
873       LLVM_DEBUG(dbgs() << "Processing Worklist\n");
874       while (!Worklist.empty()) {
875         BasicBlock *BB = OrderToBB[Worklist.top()];
876         LLVM_DEBUG(dbgs() << "\nPop BB " << BB->getName() << "\n");
877         Worklist.pop();
878         bool InChanged = meet(*BB, Visited);
879         // Always consider LiveIn changed on the first visit.
880         InChanged |= Visited.insert(BB).second;
881         if (InChanged) {
882           LLVM_DEBUG(dbgs()
883                      << BB->getName() << " has new InLocs, process it\n");
884           //  Mutate a copy of LiveIn while processing BB. Once we've processed
885           //  the terminator LiveSet is the LiveOut set for BB.
886           //  This is an expensive copy!
887           VarFragMap LiveSet = LiveIn[BB];
888 
889           // Process the instructions in the block.
890           process(*BB, LiveSet);
891 
892           // Relatively expensive check: has anything changed in LiveOut for BB?
893           if (!varFragMapsAreEqual(LiveOut[BB], LiveSet)) {
894             LLVM_DEBUG(dbgs() << BB->getName()
895                               << " has new OutLocs, add succs to worklist: [ ");
896             LiveOut[BB] = std::move(LiveSet);
897             for (auto I = succ_begin(BB), E = succ_end(BB); I != E; I++) {
898               if (OnPending.insert(*I).second) {
899                 LLVM_DEBUG(dbgs() << I->getName() << " ");
900                 Pending.push(BBToOrder[*I]);
901               }
902             }
903             LLVM_DEBUG(dbgs() << "]\n");
904           }
905         }
906       }
907       Worklist.swap(Pending);
908       // At this point, pending must be empty, since it was just the empty
909       // worklist
910       assert(Pending.empty() && "Pending should be empty");
911     }
912 
913     // Insert new location defs.
914     for (auto &Pair : BBInsertBeforeMap) {
915       InsertMap &Map = Pair.second;
916       for (auto &Pair : Map) {
917         Instruction *InsertBefore = Pair.first;
918         assert(InsertBefore && "should never be null");
919         auto FragMemLocs = Pair.second;
920         auto &Ctx = Fn.getContext();
921 
922         for (auto &FragMemLoc : FragMemLocs) {
923           DIExpression *Expr = DIExpression::get(Ctx, std::nullopt);
924           if (FragMemLoc.SizeInBits !=
925               *Aggregates[FragMemLoc.Var].first->getSizeInBits())
926             Expr = *DIExpression::createFragmentExpression(
927                 Expr, FragMemLoc.OffsetInBits, FragMemLoc.SizeInBits);
928           Expr = DIExpression::prepend(Expr, DIExpression::DerefAfter,
929                                        FragMemLoc.OffsetInBits / 8);
930           DebugVariable Var(Aggregates[FragMemLoc.Var].first, Expr,
931                             FragMemLoc.DL.getInlinedAt());
932           FnVarLocs->addVarLoc(InsertBefore, Var, Expr, FragMemLoc.DL,
933                                Bases[FragMemLoc.Base]);
934         }
935       }
936     }
937   }
938 };
939 
940 /// AssignmentTrackingLowering encapsulates a dataflow analysis over a function
941 /// that interprets assignment tracking debug info metadata and stores in IR to
942 /// create a map of variable locations.
943 class AssignmentTrackingLowering {
944 public:
945   /// The kind of location in use for a variable, where Mem is the stack home,
946   /// Val is an SSA value or const, and None means that there is not one single
947   /// kind (either because there are multiple or because there is none; it may
948   /// prove useful to split this into two values in the future).
949   ///
950   /// LocKind is a join-semilattice with the partial order:
951   /// None > Mem, Val
952   ///
953   /// i.e.
954   /// join(Mem, Mem)   = Mem
955   /// join(Val, Val)   = Val
956   /// join(Mem, Val)   = None
957   /// join(None, Mem)  = None
958   /// join(None, Val)  = None
959   /// join(None, None) = None
960   ///
961   /// Note: the order is not `None > Val > Mem` because we're using DIAssignID
962   /// to name assignments and are not tracking the actual stored values.
963   /// Therefore currently there's no way to ensure that Mem values and Val
964   /// values are the same. This could be a future extension, though it's not
965   /// clear that many additional locations would be recovered that way in
966   /// practice as the likelihood of this sitation arising naturally seems
967   /// incredibly low.
968   enum class LocKind { Mem, Val, None };
969 
970   /// An abstraction of the assignment of a value to a variable or memory
971   /// location.
972   ///
973   /// An Assignment is Known or NoneOrPhi. A Known Assignment means we have a
974   /// DIAssignID ptr that represents it. NoneOrPhi means that we don't (or
975   /// can't) know the ID of the last assignment that took place.
976   ///
977   /// The Status of the Assignment (Known or NoneOrPhi) is another
978   /// join-semilattice. The partial order is:
979   /// NoneOrPhi > Known {id_0, id_1, ...id_N}
980   ///
981   /// i.e. for all values x and y where x != y:
982   /// join(x, x) = x
983   /// join(x, y) = NoneOrPhi
984   struct Assignment {
985     enum S { Known, NoneOrPhi } Status;
986     /// ID of the assignment. nullptr if Status is not Known.
987     DIAssignID *ID;
988     /// The dbg.assign that marks this dbg-def. Mem-defs don't use this field.
989     /// May be nullptr.
990     DbgAssignIntrinsic *Source;
991 
992     bool isSameSourceAssignment(const Assignment &Other) const {
993       // Don't include Source in the equality check. Assignments are
994       // defined by their ID, not debug intrinsic(s).
995       return std::tie(Status, ID) == std::tie(Other.Status, Other.ID);
996     }
997     void dump(raw_ostream &OS) {
998       static const char *LUT[] = {"Known", "NoneOrPhi"};
999       OS << LUT[Status] << "(id=";
1000       if (ID)
1001         OS << ID;
1002       else
1003         OS << "null";
1004       OS << ", s=";
1005       if (Source)
1006         OS << *Source;
1007       else
1008         OS << "null";
1009       OS << ")";
1010     }
1011 
1012     static Assignment make(DIAssignID *ID, DbgAssignIntrinsic *Source) {
1013       return Assignment(Known, ID, Source);
1014     }
1015     static Assignment makeFromMemDef(DIAssignID *ID) {
1016       return Assignment(Known, ID, nullptr);
1017     }
1018     static Assignment makeNoneOrPhi() {
1019       return Assignment(NoneOrPhi, nullptr, nullptr);
1020     }
1021     // Again, need a Top value?
1022     Assignment()
1023         : Status(NoneOrPhi), ID(nullptr), Source(nullptr) {
1024     } // Can we delete this?
1025     Assignment(S Status, DIAssignID *ID, DbgAssignIntrinsic *Source)
1026         : Status(Status), ID(ID), Source(Source) {
1027       // If the Status is Known then we expect there to be an assignment ID.
1028       assert(Status == NoneOrPhi || ID);
1029     }
1030   };
1031 
1032   using AssignmentMap = SmallVector<Assignment>;
1033   using LocMap = SmallVector<LocKind>;
1034   using OverlapMap = DenseMap<VariableID, SmallVector<VariableID>>;
1035   using UntaggedStoreAssignmentMap =
1036       DenseMap<const Instruction *,
1037                SmallVector<std::pair<VariableID, at::AssignmentInfo>>>;
1038 
1039 private:
1040   /// The highest numbered VariableID for partially promoted variables plus 1,
1041   /// the values for which start at 1.
1042   unsigned TrackedVariablesVectorSize = 0;
1043   /// Map a variable to the set of variables that it fully contains.
1044   OverlapMap VarContains;
1045   /// Map untagged stores to the variable fragments they assign to. Used by
1046   /// processUntaggedInstruction.
1047   UntaggedStoreAssignmentMap UntaggedStoreVars;
1048 
1049   // Machinery to defer inserting dbg.values.
1050   using InsertMap = MapVector<Instruction *, SmallVector<VarLocInfo>>;
1051   InsertMap InsertBeforeMap;
1052   /// Clear the location definitions currently cached for insertion after /p
1053   /// After.
1054   void resetInsertionPoint(Instruction &After);
1055   void emitDbgValue(LocKind Kind, const DbgVariableIntrinsic *Source,
1056                     Instruction *After);
1057 
1058   static bool mapsAreEqual(const BitVector &Mask, const AssignmentMap &A,
1059                            const AssignmentMap &B) {
1060     return llvm::all_of(Mask.set_bits(), [&](unsigned VarID) {
1061       return A[VarID].isSameSourceAssignment(B[VarID]);
1062     });
1063   }
1064 
1065   /// Represents the stack and debug assignments in a block. Used to describe
1066   /// the live-in and live-out values for blocks, as well as the "current"
1067   /// value as we process each instruction in a block.
1068   struct BlockInfo {
1069     /// The set of variables (VariableID) being tracked in this block.
1070     BitVector VariableIDsInBlock;
1071     /// Dominating assignment to memory for each variable, indexed by
1072     /// VariableID.
1073     AssignmentMap StackHomeValue;
1074     /// Dominating assignemnt to each variable, indexed by VariableID.
1075     AssignmentMap DebugValue;
1076     /// Location kind for each variable. LiveLoc indicates whether the
1077     /// dominating assignment in StackHomeValue (LocKind::Mem), DebugValue
1078     /// (LocKind::Val), or neither (LocKind::None) is valid, in that order of
1079     /// preference. This cannot be derived by inspecting DebugValue and
1080     /// StackHomeValue due to the fact that there's no distinction in
1081     /// Assignment (the class) between whether an assignment is unknown or a
1082     /// merge of multiple assignments (both are Status::NoneOrPhi). In other
1083     /// words, the memory location may well be valid while both DebugValue and
1084     /// StackHomeValue contain Assignments that have a Status of NoneOrPhi.
1085     /// Indexed by VariableID.
1086     LocMap LiveLoc;
1087 
1088   public:
1089     enum AssignmentKind { Stack, Debug };
1090     const AssignmentMap &getAssignmentMap(AssignmentKind Kind) const {
1091       switch (Kind) {
1092       case Stack:
1093         return StackHomeValue;
1094       case Debug:
1095         return DebugValue;
1096       }
1097       llvm_unreachable("Unknown AssignmentKind");
1098     }
1099     AssignmentMap &getAssignmentMap(AssignmentKind Kind) {
1100       return const_cast<AssignmentMap &>(
1101           const_cast<const BlockInfo *>(this)->getAssignmentMap(Kind));
1102     }
1103 
1104     bool isVariableTracked(VariableID Var) const {
1105       return VariableIDsInBlock[static_cast<unsigned>(Var)];
1106     }
1107 
1108     const Assignment &getAssignment(AssignmentKind Kind, VariableID Var) const {
1109       assert(isVariableTracked(Var) && "Var not tracked in block");
1110       return getAssignmentMap(Kind)[static_cast<unsigned>(Var)];
1111     }
1112 
1113     LocKind getLocKind(VariableID Var) const {
1114       assert(isVariableTracked(Var) && "Var not tracked in block");
1115       return LiveLoc[static_cast<unsigned>(Var)];
1116     }
1117 
1118     /// Set LocKind for \p Var only: does not set LocKind for VariableIDs of
1119     /// fragments contained win \p Var.
1120     void setLocKind(VariableID Var, LocKind K) {
1121       VariableIDsInBlock.set(static_cast<unsigned>(Var));
1122       LiveLoc[static_cast<unsigned>(Var)] = K;
1123     }
1124 
1125     /// Set the assignment in the \p Kind assignment map for \p Var only: does
1126     /// not set the assignment for VariableIDs of fragments contained win \p
1127     /// Var.
1128     void setAssignment(AssignmentKind Kind, VariableID Var,
1129                        const Assignment &AV) {
1130       VariableIDsInBlock.set(static_cast<unsigned>(Var));
1131       getAssignmentMap(Kind)[static_cast<unsigned>(Var)] = AV;
1132     }
1133 
1134     /// Return true if there is an assignment matching \p AV in the \p Kind
1135     /// assignment map. Does consider assignments for VariableIDs of fragments
1136     /// contained win \p Var.
1137     bool hasAssignment(AssignmentKind Kind, VariableID Var,
1138                        const Assignment &AV) const {
1139       if (!isVariableTracked(Var))
1140         return false;
1141       return AV.isSameSourceAssignment(getAssignment(Kind, Var));
1142     }
1143 
1144     /// Compare every element in each map to determine structural equality
1145     /// (slow).
1146     bool operator==(const BlockInfo &Other) const {
1147       return VariableIDsInBlock == Other.VariableIDsInBlock &&
1148              LiveLoc == Other.LiveLoc &&
1149              mapsAreEqual(VariableIDsInBlock, StackHomeValue,
1150                           Other.StackHomeValue) &&
1151              mapsAreEqual(VariableIDsInBlock, DebugValue, Other.DebugValue);
1152     }
1153     bool operator!=(const BlockInfo &Other) const { return !(*this == Other); }
1154     bool isValid() {
1155       return LiveLoc.size() == DebugValue.size() &&
1156              LiveLoc.size() == StackHomeValue.size();
1157     }
1158 
1159     /// Clear everything and initialise with ⊤-values for all variables.
1160     void init(int NumVars) {
1161       StackHomeValue.clear();
1162       DebugValue.clear();
1163       LiveLoc.clear();
1164       VariableIDsInBlock = BitVector(NumVars);
1165       StackHomeValue.insert(StackHomeValue.begin(), NumVars,
1166                             Assignment::makeNoneOrPhi());
1167       DebugValue.insert(DebugValue.begin(), NumVars,
1168                         Assignment::makeNoneOrPhi());
1169       LiveLoc.insert(LiveLoc.begin(), NumVars, LocKind::None);
1170     }
1171 
1172     /// Helper for join.
1173     template <typename ElmtType, typename FnInputType>
1174     static void joinElmt(int Index, SmallVector<ElmtType> &Target,
1175                          const SmallVector<ElmtType> &A,
1176                          const SmallVector<ElmtType> &B,
1177                          ElmtType (*Fn)(FnInputType, FnInputType)) {
1178       Target[Index] = Fn(A[Index], B[Index]);
1179     }
1180 
1181     /// See comment for AssignmentTrackingLowering::joinBlockInfo.
1182     static BlockInfo join(const BlockInfo &A, const BlockInfo &B, int NumVars) {
1183       // Join A and B.
1184       //
1185       // Intersect = join(a, b) for a in A, b in B where Var(a) == Var(b)
1186       // Difference = join(x, ⊤) for x where Var(x) is in A xor B
1187       // Join = Intersect ∪ Difference
1188       //
1189       // This is achieved by performing a join on elements from A and B with
1190       // variables common to both A and B (join elements indexed by var
1191       // intersect), then adding ⊤-value elements for vars in A xor B. The
1192       // latter part is equivalent to performing join on elements with variables
1193       // in A xor B with the ⊤-value for the map element since join(x, ⊤) = ⊤.
1194       // BlockInfo::init initializes all variable entries to the ⊤ value so we
1195       // don't need to explicitly perform that step as Join.VariableIDsInBlock
1196       // is set to the union of the variables in A and B at the end of this
1197       // function.
1198       BlockInfo Join;
1199       Join.init(NumVars);
1200 
1201       BitVector Intersect = A.VariableIDsInBlock;
1202       Intersect &= B.VariableIDsInBlock;
1203 
1204       for (auto VarID : Intersect.set_bits()) {
1205         joinElmt(VarID, Join.LiveLoc, A.LiveLoc, B.LiveLoc, joinKind);
1206         joinElmt(VarID, Join.DebugValue, A.DebugValue, B.DebugValue,
1207                  joinAssignment);
1208         joinElmt(VarID, Join.StackHomeValue, A.StackHomeValue, B.StackHomeValue,
1209                  joinAssignment);
1210       }
1211 
1212       Join.VariableIDsInBlock = A.VariableIDsInBlock;
1213       Join.VariableIDsInBlock |= B.VariableIDsInBlock;
1214       assert(Join.isValid());
1215       return Join;
1216     }
1217   };
1218 
1219   Function &Fn;
1220   const DataLayout &Layout;
1221   const DenseSet<DebugAggregate> *VarsWithStackSlot;
1222   FunctionVarLocsBuilder *FnVarLocs;
1223   DenseMap<const BasicBlock *, BlockInfo> LiveIn;
1224   DenseMap<const BasicBlock *, BlockInfo> LiveOut;
1225 
1226   /// Helper for process methods to track variables touched each frame.
1227   DenseSet<VariableID> VarsTouchedThisFrame;
1228 
1229   /// The set of variables that sometimes are not located in their stack home.
1230   DenseSet<DebugAggregate> NotAlwaysStackHomed;
1231 
1232   VariableID getVariableID(const DebugVariable &Var) {
1233     return static_cast<VariableID>(FnVarLocs->insertVariable(Var));
1234   }
1235 
1236   /// Join the LiveOut values of preds that are contained in \p Visited into
1237   /// LiveIn[BB]. Return True if LiveIn[BB] has changed as a result. LiveIn[BB]
1238   /// values monotonically increase. See the @link joinMethods join methods
1239   /// @endlink documentation for more info.
1240   bool join(const BasicBlock &BB, const SmallPtrSet<BasicBlock *, 16> &Visited);
1241   ///@name joinMethods
1242   /// Functions that implement `join` (the least upper bound) for the
1243   /// join-semilattice types used in the dataflow. There is an explicit bottom
1244   /// value (⊥) for some types and and explicit top value (⊤) for all types.
1245   /// By definition:
1246   ///
1247   ///     Join(A, B) >= A && Join(A, B) >= B
1248   ///     Join(A, ⊥) = A
1249   ///     Join(A, ⊤) = ⊤
1250   ///
1251   /// These invariants are important for monotonicity.
1252   ///
1253   /// For the map-type functions, all unmapped keys in an empty map are
1254   /// associated with a bottom value (⊥). This represents their values being
1255   /// unknown. Unmapped keys in non-empty maps (joining two maps with a key
1256   /// only present in one) represents either a variable going out of scope or
1257   /// dropped debug info. It is assumed the key is associated with a top value
1258   /// (⊤) in this case (unknown location / assignment).
1259   ///@{
1260   static LocKind joinKind(LocKind A, LocKind B);
1261   static Assignment joinAssignment(const Assignment &A, const Assignment &B);
1262   BlockInfo joinBlockInfo(const BlockInfo &A, const BlockInfo &B);
1263   ///@}
1264 
1265   /// Process the instructions in \p BB updating \p LiveSet along the way. \p
1266   /// LiveSet must be initialized with the current live-in locations before
1267   /// calling this.
1268   void process(BasicBlock &BB, BlockInfo *LiveSet);
1269   ///@name processMethods
1270   /// Methods to process instructions in order to update the LiveSet (current
1271   /// location information).
1272   ///@{
1273   void processNonDbgInstruction(Instruction &I, BlockInfo *LiveSet);
1274   void processDbgInstruction(DbgInfoIntrinsic &I, BlockInfo *LiveSet);
1275   /// Update \p LiveSet after encountering an instruction with a DIAssignID
1276   /// attachment, \p I.
1277   void processTaggedInstruction(Instruction &I, BlockInfo *LiveSet);
1278   /// Update \p LiveSet after encountering an instruciton without a DIAssignID
1279   /// attachment, \p I.
1280   void processUntaggedInstruction(Instruction &I, BlockInfo *LiveSet);
1281   void processDbgAssign(DbgAssignIntrinsic &DAI, BlockInfo *LiveSet);
1282   void processDbgValue(DbgValueInst &DVI, BlockInfo *LiveSet);
1283   /// Add an assignment to memory for the variable /p Var.
1284   void addMemDef(BlockInfo *LiveSet, VariableID Var, const Assignment &AV);
1285   /// Add an assignment to the variable /p Var.
1286   void addDbgDef(BlockInfo *LiveSet, VariableID Var, const Assignment &AV);
1287   ///@}
1288 
1289   /// Set the LocKind for \p Var.
1290   void setLocKind(BlockInfo *LiveSet, VariableID Var, LocKind K);
1291   /// Get the live LocKind for a \p Var. Requires addMemDef or addDbgDef to
1292   /// have been called for \p Var first.
1293   LocKind getLocKind(BlockInfo *LiveSet, VariableID Var);
1294   /// Return true if \p Var has an assignment in \p M matching \p AV.
1295   bool hasVarWithAssignment(BlockInfo *LiveSet, BlockInfo::AssignmentKind Kind,
1296                             VariableID Var, const Assignment &AV);
1297   /// Return the set of VariableIDs corresponding the fragments contained fully
1298   /// within the variable/fragment \p Var.
1299   ArrayRef<VariableID> getContainedFragments(VariableID Var) const;
1300 
1301   /// Mark \p Var as having been touched this frame. Note, this applies only
1302   /// to the exact fragment \p Var and not to any fragments contained within.
1303   void touchFragment(VariableID Var);
1304 
1305   /// Emit info for variables that are fully promoted.
1306   bool emitPromotedVarLocs(FunctionVarLocsBuilder *FnVarLocs);
1307 
1308 public:
1309   AssignmentTrackingLowering(Function &Fn, const DataLayout &Layout,
1310                              const DenseSet<DebugAggregate> *VarsWithStackSlot)
1311       : Fn(Fn), Layout(Layout), VarsWithStackSlot(VarsWithStackSlot) {}
1312   /// Run the analysis, adding variable location info to \p FnVarLocs. Returns
1313   /// true if any variable locations have been added to FnVarLocs.
1314   bool run(FunctionVarLocsBuilder *FnVarLocs);
1315 };
1316 } // namespace
1317 
1318 ArrayRef<VariableID>
1319 AssignmentTrackingLowering::getContainedFragments(VariableID Var) const {
1320   auto R = VarContains.find(Var);
1321   if (R == VarContains.end())
1322     return std::nullopt;
1323   return R->second;
1324 }
1325 
1326 void AssignmentTrackingLowering::touchFragment(VariableID Var) {
1327   VarsTouchedThisFrame.insert(Var);
1328 }
1329 
1330 void AssignmentTrackingLowering::setLocKind(BlockInfo *LiveSet, VariableID Var,
1331                                             LocKind K) {
1332   auto SetKind = [this](BlockInfo *LiveSet, VariableID Var, LocKind K) {
1333     LiveSet->setLocKind(Var, K);
1334     touchFragment(Var);
1335   };
1336   SetKind(LiveSet, Var, K);
1337 
1338   // Update the LocKind for all fragments contained within Var.
1339   for (VariableID Frag : getContainedFragments(Var))
1340     SetKind(LiveSet, Frag, K);
1341 }
1342 
1343 AssignmentTrackingLowering::LocKind
1344 AssignmentTrackingLowering::getLocKind(BlockInfo *LiveSet, VariableID Var) {
1345   return LiveSet->getLocKind(Var);
1346 }
1347 
1348 void AssignmentTrackingLowering::addMemDef(BlockInfo *LiveSet, VariableID Var,
1349                                            const Assignment &AV) {
1350   LiveSet->setAssignment(BlockInfo::Stack, Var, AV);
1351 
1352   // Use this assigment for all fragments contained within Var, but do not
1353   // provide a Source because we cannot convert Var's value to a value for the
1354   // fragment.
1355   Assignment FragAV = AV;
1356   FragAV.Source = nullptr;
1357   for (VariableID Frag : getContainedFragments(Var))
1358     LiveSet->setAssignment(BlockInfo::Stack, Frag, FragAV);
1359 }
1360 
1361 void AssignmentTrackingLowering::addDbgDef(BlockInfo *LiveSet, VariableID Var,
1362                                            const Assignment &AV) {
1363   LiveSet->setAssignment(BlockInfo::Debug, Var, AV);
1364 
1365   // Use this assigment for all fragments contained within Var, but do not
1366   // provide a Source because we cannot convert Var's value to a value for the
1367   // fragment.
1368   Assignment FragAV = AV;
1369   FragAV.Source = nullptr;
1370   for (VariableID Frag : getContainedFragments(Var))
1371     LiveSet->setAssignment(BlockInfo::Debug, Frag, FragAV);
1372 }
1373 
1374 static DIAssignID *getIDFromInst(const Instruction &I) {
1375   return cast<DIAssignID>(I.getMetadata(LLVMContext::MD_DIAssignID));
1376 }
1377 
1378 static DIAssignID *getIDFromMarker(const DbgAssignIntrinsic &DAI) {
1379   return cast<DIAssignID>(DAI.getAssignID());
1380 }
1381 
1382 /// Return true if \p Var has an assignment in \p M matching \p AV.
1383 bool AssignmentTrackingLowering::hasVarWithAssignment(
1384     BlockInfo *LiveSet, BlockInfo::AssignmentKind Kind, VariableID Var,
1385     const Assignment &AV) {
1386   if (!LiveSet->hasAssignment(Kind, Var, AV))
1387     return false;
1388 
1389   // Check all the frags contained within Var as these will have all been
1390   // mapped to AV at the last store to Var.
1391   for (VariableID Frag : getContainedFragments(Var))
1392     if (!LiveSet->hasAssignment(Kind, Frag, AV))
1393       return false;
1394   return true;
1395 }
1396 
1397 #ifndef NDEBUG
1398 const char *locStr(AssignmentTrackingLowering::LocKind Loc) {
1399   using LocKind = AssignmentTrackingLowering::LocKind;
1400   switch (Loc) {
1401   case LocKind::Val:
1402     return "Val";
1403   case LocKind::Mem:
1404     return "Mem";
1405   case LocKind::None:
1406     return "None";
1407   };
1408   llvm_unreachable("unknown LocKind");
1409 }
1410 #endif
1411 
1412 void AssignmentTrackingLowering::emitDbgValue(
1413     AssignmentTrackingLowering::LocKind Kind,
1414     const DbgVariableIntrinsic *Source, Instruction *After) {
1415 
1416   DILocation *DL = Source->getDebugLoc();
1417   auto Emit = [this, Source, After, DL](Metadata *Val, DIExpression *Expr) {
1418     assert(Expr);
1419     if (!Val)
1420       Val = ValueAsMetadata::get(
1421           PoisonValue::get(Type::getInt1Ty(Source->getContext())));
1422 
1423     // Find a suitable insert point.
1424     Instruction *InsertBefore = After->getNextNode();
1425     assert(InsertBefore && "Shouldn't be inserting after a terminator");
1426 
1427     VariableID Var = getVariableID(DebugVariable(Source));
1428     VarLocInfo VarLoc;
1429     VarLoc.VariableID = static_cast<VariableID>(Var);
1430     VarLoc.Expr = Expr;
1431     VarLoc.Values = RawLocationWrapper(Val);
1432     VarLoc.DL = DL;
1433     // Insert it into the map for later.
1434     InsertBeforeMap[InsertBefore].push_back(VarLoc);
1435   };
1436 
1437   // NOTE: This block can mutate Kind.
1438   if (Kind == LocKind::Mem) {
1439     const auto *DAI = cast<DbgAssignIntrinsic>(Source);
1440     // Check the address hasn't been dropped (e.g. the debug uses may not have
1441     // been replaced before deleting a Value).
1442     if (DAI->isKillAddress()) {
1443       // The address isn't valid so treat this as a non-memory def.
1444       Kind = LocKind::Val;
1445     } else {
1446       Value *Val = DAI->getAddress();
1447       DIExpression *Expr = DAI->getAddressExpression();
1448       assert(!Expr->getFragmentInfo() &&
1449              "fragment info should be stored in value-expression only");
1450       // Copy the fragment info over from the value-expression to the new
1451       // DIExpression.
1452       if (auto OptFragInfo = Source->getExpression()->getFragmentInfo()) {
1453         auto FragInfo = *OptFragInfo;
1454         Expr = *DIExpression::createFragmentExpression(
1455             Expr, FragInfo.OffsetInBits, FragInfo.SizeInBits);
1456       }
1457       // The address-expression has an implicit deref, add it now.
1458       std::tie(Val, Expr) =
1459           walkToAllocaAndPrependOffsetDeref(Layout, Val, Expr);
1460       Emit(ValueAsMetadata::get(Val), Expr);
1461       return;
1462     }
1463   }
1464 
1465   if (Kind == LocKind::Val) {
1466     Emit(Source->getRawLocation(), Source->getExpression());
1467     return;
1468   }
1469 
1470   if (Kind == LocKind::None) {
1471     Emit(nullptr, Source->getExpression());
1472     return;
1473   }
1474 }
1475 
1476 void AssignmentTrackingLowering::processNonDbgInstruction(
1477     Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1478   if (I.hasMetadata(LLVMContext::MD_DIAssignID))
1479     processTaggedInstruction(I, LiveSet);
1480   else
1481     processUntaggedInstruction(I, LiveSet);
1482 }
1483 
1484 void AssignmentTrackingLowering::processUntaggedInstruction(
1485     Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1486   // Interpret stack stores that are not tagged as an assignment in memory for
1487   // the variables associated with that address. These stores may not be tagged
1488   // because a) the store cannot be represented using dbg.assigns (non-const
1489   // length or offset) or b) the tag was accidentally dropped during
1490   // optimisations. For these stores we fall back to assuming that the stack
1491   // home is a valid location for the variables. The benefit is that this
1492   // prevents us missing an assignment and therefore incorrectly maintaining
1493   // earlier location definitions, and in many cases it should be a reasonable
1494   // assumption. However, this will occasionally lead to slight
1495   // inaccuracies. The value of a hoisted untagged store will be visible
1496   // "early", for example.
1497   assert(!I.hasMetadata(LLVMContext::MD_DIAssignID));
1498   auto It = UntaggedStoreVars.find(&I);
1499   if (It == UntaggedStoreVars.end())
1500     return; // No variables associated with the store destination.
1501 
1502   LLVM_DEBUG(dbgs() << "processUntaggedInstruction on UNTAGGED INST " << I
1503                     << "\n");
1504   // Iterate over the variables that this store affects, add a NoneOrPhi dbg
1505   // and mem def, set lockind to Mem, and emit a location def for each.
1506   for (auto [Var, Info] : It->second) {
1507     // This instruction is treated as both a debug and memory assignment,
1508     // meaning the memory location should be used. We don't have an assignment
1509     // ID though so use Assignment::makeNoneOrPhi() to create an imaginary one.
1510     addMemDef(LiveSet, Var, Assignment::makeNoneOrPhi());
1511     addDbgDef(LiveSet, Var, Assignment::makeNoneOrPhi());
1512     setLocKind(LiveSet, Var, LocKind::Mem);
1513     LLVM_DEBUG(dbgs() << "  setting Stack LocKind to: " << locStr(LocKind::Mem)
1514                       << "\n");
1515     // Build the dbg location def to insert.
1516     //
1517     // DIExpression: Add fragment and offset.
1518     DebugVariable V = FnVarLocs->getVariable(Var);
1519     DIExpression *DIE = DIExpression::get(I.getContext(), std::nullopt);
1520     if (auto Frag = V.getFragment()) {
1521       auto R = DIExpression::createFragmentExpression(DIE, Frag->OffsetInBits,
1522                                                       Frag->SizeInBits);
1523       assert(R && "unexpected createFragmentExpression failure");
1524       DIE = *R;
1525     }
1526     SmallVector<uint64_t, 3> Ops;
1527     if (Info.OffsetInBits)
1528       Ops = {dwarf::DW_OP_plus_uconst, Info.OffsetInBits / 8};
1529     Ops.push_back(dwarf::DW_OP_deref);
1530     DIE = DIExpression::prependOpcodes(DIE, Ops, /*StackValue=*/false,
1531                                        /*EntryValue=*/false);
1532     // Find a suitable insert point.
1533     Instruction *InsertBefore = I.getNextNode();
1534     assert(InsertBefore && "Shouldn't be inserting after a terminator");
1535 
1536     // Get DILocation for this unrecorded assignment.
1537     DILocation *InlinedAt = const_cast<DILocation *>(V.getInlinedAt());
1538     const DILocation *DILoc = DILocation::get(
1539         Fn.getContext(), 0, 0, V.getVariable()->getScope(), InlinedAt);
1540 
1541     VarLocInfo VarLoc;
1542     VarLoc.VariableID = static_cast<VariableID>(Var);
1543     VarLoc.Expr = DIE;
1544     VarLoc.Values = RawLocationWrapper(
1545         ValueAsMetadata::get(const_cast<AllocaInst *>(Info.Base)));
1546     VarLoc.DL = DILoc;
1547     // 3. Insert it into the map for later.
1548     InsertBeforeMap[InsertBefore].push_back(VarLoc);
1549   }
1550 }
1551 
1552 void AssignmentTrackingLowering::processTaggedInstruction(
1553     Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1554   auto Linked = at::getAssignmentMarkers(&I);
1555   // No dbg.assign intrinsics linked.
1556   // FIXME: All vars that have a stack slot this store modifies that don't have
1557   // a dbg.assign linked to it should probably treat this like an untagged
1558   // store.
1559   if (Linked.empty())
1560     return;
1561 
1562   LLVM_DEBUG(dbgs() << "processTaggedInstruction on " << I << "\n");
1563   for (DbgAssignIntrinsic *DAI : Linked) {
1564     VariableID Var = getVariableID(DebugVariable(DAI));
1565     // Something has gone wrong if VarsWithStackSlot doesn't contain a variable
1566     // that is linked to a store.
1567     assert(VarsWithStackSlot->count(getAggregate(DAI)) &&
1568            "expected DAI's variable to have stack slot");
1569 
1570     Assignment AV = Assignment::makeFromMemDef(getIDFromInst(I));
1571     addMemDef(LiveSet, Var, AV);
1572 
1573     LLVM_DEBUG(dbgs() << "   linked to " << *DAI << "\n");
1574     LLVM_DEBUG(dbgs() << "   LiveLoc " << locStr(getLocKind(LiveSet, Var))
1575                       << " -> ");
1576 
1577     // The last assignment to the stack is now AV. Check if the last debug
1578     // assignment has a matching Assignment.
1579     if (hasVarWithAssignment(LiveSet, BlockInfo::Debug, Var, AV)) {
1580       // The StackHomeValue and DebugValue for this variable match so we can
1581       // emit a stack home location here.
1582       LLVM_DEBUG(dbgs() << "Mem, Stack matches Debug program\n";);
1583       LLVM_DEBUG(dbgs() << "   Stack val: "; AV.dump(dbgs()); dbgs() << "\n");
1584       LLVM_DEBUG(dbgs() << "   Debug val: ";
1585                  LiveSet->DebugValue[static_cast<unsigned>(Var)].dump(dbgs());
1586                  dbgs() << "\n");
1587       setLocKind(LiveSet, Var, LocKind::Mem);
1588       emitDbgValue(LocKind::Mem, DAI, &I);
1589       continue;
1590     }
1591 
1592     // The StackHomeValue and DebugValue for this variable do not match. I.e.
1593     // The value currently stored in the stack is not what we'd expect to
1594     // see, so we cannot use emit a stack home location here. Now we will
1595     // look at the live LocKind for the variable and determine an appropriate
1596     // dbg.value to emit.
1597     LocKind PrevLoc = getLocKind(LiveSet, Var);
1598     switch (PrevLoc) {
1599     case LocKind::Val: {
1600       // The value in memory in memory has changed but we're not currently
1601       // using the memory location. Do nothing.
1602       LLVM_DEBUG(dbgs() << "Val, (unchanged)\n";);
1603       setLocKind(LiveSet, Var, LocKind::Val);
1604     } break;
1605     case LocKind::Mem: {
1606       // There's been an assignment to memory that we were using as a
1607       // location for this variable, and the Assignment doesn't match what
1608       // we'd expect to see in memory.
1609       Assignment DbgAV = LiveSet->getAssignment(BlockInfo::Debug, Var);
1610       if (DbgAV.Status == Assignment::NoneOrPhi) {
1611         // We need to terminate any previously open location now.
1612         LLVM_DEBUG(dbgs() << "None, No Debug value available\n";);
1613         setLocKind(LiveSet, Var, LocKind::None);
1614         emitDbgValue(LocKind::None, DAI, &I);
1615       } else {
1616         // The previous DebugValue Value can be used here.
1617         LLVM_DEBUG(dbgs() << "Val, Debug value is Known\n";);
1618         setLocKind(LiveSet, Var, LocKind::Val);
1619         if (DbgAV.Source) {
1620           emitDbgValue(LocKind::Val, DbgAV.Source, &I);
1621         } else {
1622           // PrevAV.Source is nullptr so we must emit undef here.
1623           emitDbgValue(LocKind::None, DAI, &I);
1624         }
1625       }
1626     } break;
1627     case LocKind::None: {
1628       // There's been an assignment to memory and we currently are
1629       // not tracking a location for the variable. Do not emit anything.
1630       LLVM_DEBUG(dbgs() << "None, (unchanged)\n";);
1631       setLocKind(LiveSet, Var, LocKind::None);
1632     } break;
1633     }
1634   }
1635 }
1636 
1637 void AssignmentTrackingLowering::processDbgAssign(DbgAssignIntrinsic &DAI,
1638                                                   BlockInfo *LiveSet) {
1639   // Only bother tracking variables that are at some point stack homed. Other
1640   // variables can be dealt with trivially later.
1641   if (!VarsWithStackSlot->count(getAggregate(&DAI)))
1642     return;
1643 
1644   VariableID Var = getVariableID(DebugVariable(&DAI));
1645   Assignment AV = Assignment::make(getIDFromMarker(DAI), &DAI);
1646   addDbgDef(LiveSet, Var, AV);
1647 
1648   LLVM_DEBUG(dbgs() << "processDbgAssign on " << DAI << "\n";);
1649   LLVM_DEBUG(dbgs() << "   LiveLoc " << locStr(getLocKind(LiveSet, Var))
1650                     << " -> ");
1651 
1652   // Check if the DebugValue and StackHomeValue both hold the same
1653   // Assignment.
1654   if (hasVarWithAssignment(LiveSet, BlockInfo::Stack, Var, AV)) {
1655     // They match. We can use the stack home because the debug intrinsics state
1656     // that an assignment happened here, and we know that specific assignment
1657     // was the last one to take place in memory for this variable.
1658     LocKind Kind;
1659     if (DAI.isKillAddress()) {
1660       LLVM_DEBUG(
1661           dbgs()
1662               << "Val, Stack matches Debug program but address is killed\n";);
1663       Kind = LocKind::Val;
1664     } else {
1665       LLVM_DEBUG(dbgs() << "Mem, Stack matches Debug program\n";);
1666       Kind = LocKind::Mem;
1667     };
1668     setLocKind(LiveSet, Var, Kind);
1669     emitDbgValue(Kind, &DAI, &DAI);
1670   } else {
1671     // The last assignment to the memory location isn't the one that we want to
1672     // show to the user so emit a dbg.value(Value). Value may be undef.
1673     LLVM_DEBUG(dbgs() << "Val, Stack contents is unknown\n";);
1674     setLocKind(LiveSet, Var, LocKind::Val);
1675     emitDbgValue(LocKind::Val, &DAI, &DAI);
1676   }
1677 }
1678 
1679 void AssignmentTrackingLowering::processDbgValue(DbgValueInst &DVI,
1680                                                  BlockInfo *LiveSet) {
1681   // Only other tracking variables that are at some point stack homed.
1682   // Other variables can be dealt with trivally later.
1683   if (!VarsWithStackSlot->count(getAggregate(&DVI)))
1684     return;
1685 
1686   VariableID Var = getVariableID(DebugVariable(&DVI));
1687   // We have no ID to create an Assignment with so we mark this assignment as
1688   // NoneOrPhi. Note that the dbg.value still exists, we just cannot determine
1689   // the assignment responsible for setting this value.
1690   // This is fine; dbg.values are essentially interchangable with unlinked
1691   // dbg.assigns, and some passes such as mem2reg and instcombine add them to
1692   // PHIs for promoted variables.
1693   Assignment AV = Assignment::makeNoneOrPhi();
1694   addDbgDef(LiveSet, Var, AV);
1695 
1696   LLVM_DEBUG(dbgs() << "processDbgValue on " << DVI << "\n";);
1697   LLVM_DEBUG(dbgs() << "   LiveLoc " << locStr(getLocKind(LiveSet, Var))
1698                     << " -> Val, dbg.value override");
1699 
1700   setLocKind(LiveSet, Var, LocKind::Val);
1701   emitDbgValue(LocKind::Val, &DVI, &DVI);
1702 }
1703 
1704 static bool hasZeroSizedFragment(DbgVariableIntrinsic &DVI) {
1705   if (auto F = DVI.getExpression()->getFragmentInfo())
1706     return F->SizeInBits == 0;
1707   return false;
1708 }
1709 
1710 void AssignmentTrackingLowering::processDbgInstruction(
1711     DbgInfoIntrinsic &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1712   auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
1713   if (!DVI)
1714     return;
1715 
1716   // Ignore assignments to zero bits of the variable.
1717   if (hasZeroSizedFragment(*DVI))
1718     return;
1719 
1720   if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))
1721     processDbgAssign(*DAI, LiveSet);
1722   else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
1723     processDbgValue(*DVI, LiveSet);
1724 }
1725 
1726 void AssignmentTrackingLowering::resetInsertionPoint(Instruction &After) {
1727   assert(!After.isTerminator() && "Can't insert after a terminator");
1728   auto R = InsertBeforeMap.find(After.getNextNode());
1729   if (R == InsertBeforeMap.end())
1730     return;
1731   R->second.clear();
1732 }
1733 
1734 void AssignmentTrackingLowering::process(BasicBlock &BB, BlockInfo *LiveSet) {
1735   for (auto II = BB.begin(), EI = BB.end(); II != EI;) {
1736     assert(VarsTouchedThisFrame.empty());
1737     // Process the instructions in "frames". A "frame" includes a single
1738     // non-debug instruction followed any debug instructions before the
1739     // next non-debug instruction.
1740     if (!isa<DbgInfoIntrinsic>(&*II)) {
1741       if (II->isTerminator())
1742         break;
1743       resetInsertionPoint(*II);
1744       processNonDbgInstruction(*II, LiveSet);
1745       assert(LiveSet->isValid());
1746       ++II;
1747     }
1748     while (II != EI) {
1749       auto *Dbg = dyn_cast<DbgInfoIntrinsic>(&*II);
1750       if (!Dbg)
1751         break;
1752       resetInsertionPoint(*II);
1753       processDbgInstruction(*Dbg, LiveSet);
1754       assert(LiveSet->isValid());
1755       ++II;
1756     }
1757 
1758     // We've processed everything in the "frame". Now determine which variables
1759     // cannot be represented by a dbg.declare.
1760     for (auto Var : VarsTouchedThisFrame) {
1761       LocKind Loc = getLocKind(LiveSet, Var);
1762       // If a variable's LocKind is anything other than LocKind::Mem then we
1763       // must note that it cannot be represented with a dbg.declare.
1764       // Note that this check is enough without having to check the result of
1765       // joins() because for join to produce anything other than Mem after
1766       // we've already seen a Mem we'd be joining None or Val with Mem. In that
1767       // case, we've already hit this codepath when we set the LocKind to Val
1768       // or None in that block.
1769       if (Loc != LocKind::Mem) {
1770         DebugVariable DbgVar = FnVarLocs->getVariable(Var);
1771         DebugAggregate Aggr{DbgVar.getVariable(), DbgVar.getInlinedAt()};
1772         NotAlwaysStackHomed.insert(Aggr);
1773       }
1774     }
1775     VarsTouchedThisFrame.clear();
1776   }
1777 }
1778 
1779 AssignmentTrackingLowering::LocKind
1780 AssignmentTrackingLowering::joinKind(LocKind A, LocKind B) {
1781   // Partial order:
1782   // None > Mem, Val
1783   return A == B ? A : LocKind::None;
1784 }
1785 
1786 AssignmentTrackingLowering::Assignment
1787 AssignmentTrackingLowering::joinAssignment(const Assignment &A,
1788                                            const Assignment &B) {
1789   // Partial order:
1790   // NoneOrPhi(null, null) > Known(v, ?s)
1791 
1792   // If either are NoneOrPhi the join is NoneOrPhi.
1793   // If either value is different then the result is
1794   // NoneOrPhi (joining two values is a Phi).
1795   if (!A.isSameSourceAssignment(B))
1796     return Assignment::makeNoneOrPhi();
1797   if (A.Status == Assignment::NoneOrPhi)
1798     return Assignment::makeNoneOrPhi();
1799 
1800   // Source is used to lookup the value + expression in the debug program if
1801   // the stack slot gets assigned a value earlier than expected. Because
1802   // we're only tracking the one dbg.assign, we can't capture debug PHIs.
1803   // It's unlikely that we're losing out on much coverage by avoiding that
1804   // extra work.
1805   // The Source may differ in this situation:
1806   // Pred.1:
1807   //   dbg.assign i32 0, ..., !1, ...
1808   // Pred.2:
1809   //   dbg.assign i32 1, ..., !1, ...
1810   // Here the same assignment (!1) was performed in both preds in the source,
1811   // but we can't use either one unless they are identical (e.g. .we don't
1812   // want to arbitrarily pick between constant values).
1813   auto JoinSource = [&]() -> DbgAssignIntrinsic * {
1814     if (A.Source == B.Source)
1815       return A.Source;
1816     if (A.Source == nullptr || B.Source == nullptr)
1817       return nullptr;
1818     if (A.Source->isIdenticalTo(B.Source))
1819       return A.Source;
1820     return nullptr;
1821   };
1822   DbgAssignIntrinsic *Source = JoinSource();
1823   assert(A.Status == B.Status && A.Status == Assignment::Known);
1824   assert(A.ID == B.ID);
1825   return Assignment::make(A.ID, Source);
1826 }
1827 
1828 AssignmentTrackingLowering::BlockInfo
1829 AssignmentTrackingLowering::joinBlockInfo(const BlockInfo &A,
1830                                           const BlockInfo &B) {
1831   return BlockInfo::join(A, B, TrackedVariablesVectorSize);
1832 }
1833 
1834 bool AssignmentTrackingLowering::join(
1835     const BasicBlock &BB, const SmallPtrSet<BasicBlock *, 16> &Visited) {
1836 
1837   SmallVector<const BasicBlock *> VisitedPreds;
1838   // Ignore backedges if we have not visited the predecessor yet. As the
1839   // predecessor hasn't yet had locations propagated into it, most locations
1840   // will not yet be valid, so treat them as all being uninitialized and
1841   // potentially valid. If a location guessed to be correct here is
1842   // invalidated later, we will remove it when we revisit this block. This
1843   // is essentially the same as initialising all LocKinds and Assignments to
1844   // an implicit ⊥ value which is the identity value for the join operation.
1845   for (auto I = pred_begin(&BB), E = pred_end(&BB); I != E; I++) {
1846     const BasicBlock *Pred = *I;
1847     if (Visited.count(Pred))
1848       VisitedPreds.push_back(Pred);
1849   }
1850 
1851   // No preds visited yet.
1852   if (VisitedPreds.empty()) {
1853     auto It = LiveIn.try_emplace(&BB, BlockInfo());
1854     bool DidInsert = It.second;
1855     if (DidInsert)
1856       It.first->second.init(TrackedVariablesVectorSize);
1857     return /*Changed*/ DidInsert;
1858   }
1859 
1860   // Exactly one visited pred. Copy the LiveOut from that pred into BB LiveIn.
1861   if (VisitedPreds.size() == 1) {
1862     const BlockInfo &PredLiveOut = LiveOut.find(VisitedPreds[0])->second;
1863     auto CurrentLiveInEntry = LiveIn.find(&BB);
1864 
1865     // Check if there isn't an entry, or there is but the LiveIn set has
1866     // changed (expensive check).
1867     if (CurrentLiveInEntry == LiveIn.end())
1868       LiveIn.insert(std::make_pair(&BB, PredLiveOut));
1869     else if (PredLiveOut != CurrentLiveInEntry->second)
1870       CurrentLiveInEntry->second = PredLiveOut;
1871     else
1872       return /*Changed*/ false;
1873     return /*Changed*/ true;
1874   }
1875 
1876   // More than one pred. Join LiveOuts of blocks 1 and 2.
1877   assert(VisitedPreds.size() > 1);
1878   const BlockInfo &PredLiveOut0 = LiveOut.find(VisitedPreds[0])->second;
1879   const BlockInfo &PredLiveOut1 = LiveOut.find(VisitedPreds[1])->second;
1880   BlockInfo BBLiveIn = joinBlockInfo(PredLiveOut0, PredLiveOut1);
1881 
1882   // Join the LiveOuts of subsequent blocks.
1883   ArrayRef Tail = ArrayRef(VisitedPreds).drop_front(2);
1884   for (const BasicBlock *Pred : Tail) {
1885     const auto &PredLiveOut = LiveOut.find(Pred);
1886     assert(PredLiveOut != LiveOut.end() &&
1887            "block should have been processed already");
1888     BBLiveIn = joinBlockInfo(std::move(BBLiveIn), PredLiveOut->second);
1889   }
1890 
1891   // Save the joined result for BB.
1892   auto CurrentLiveInEntry = LiveIn.find(&BB);
1893   // Check if there isn't an entry, or there is but the LiveIn set has changed
1894   // (expensive check).
1895   if (CurrentLiveInEntry == LiveIn.end())
1896     LiveIn.try_emplace(&BB, std::move(BBLiveIn));
1897   else if (BBLiveIn != CurrentLiveInEntry->second)
1898     CurrentLiveInEntry->second = std::move(BBLiveIn);
1899   else
1900     return /*Changed*/ false;
1901   return /*Changed*/ true;
1902 }
1903 
1904 /// Return true if A fully contains B.
1905 static bool fullyContains(DIExpression::FragmentInfo A,
1906                           DIExpression::FragmentInfo B) {
1907   auto ALeft = A.OffsetInBits;
1908   auto BLeft = B.OffsetInBits;
1909   if (BLeft < ALeft)
1910     return false;
1911 
1912   auto ARight = ALeft + A.SizeInBits;
1913   auto BRight = BLeft + B.SizeInBits;
1914   if (BRight > ARight)
1915     return false;
1916   return true;
1917 }
1918 
1919 static std::optional<at::AssignmentInfo>
1920 getUntaggedStoreAssignmentInfo(const Instruction &I, const DataLayout &Layout) {
1921   // Don't bother checking if this is an AllocaInst. We know this
1922   // instruction has no tag which means there are no variables associated
1923   // with it.
1924   if (const auto *SI = dyn_cast<StoreInst>(&I))
1925     return at::getAssignmentInfo(Layout, SI);
1926   if (const auto *MI = dyn_cast<MemIntrinsic>(&I))
1927     return at::getAssignmentInfo(Layout, MI);
1928   // Alloca or non-store-like inst.
1929   return std::nullopt;
1930 }
1931 
1932 /// Build a map of {Variable x: Variables y} where all variable fragments
1933 /// contained within the variable fragment x are in set y. This means that
1934 /// y does not contain all overlaps because partial overlaps are excluded.
1935 ///
1936 /// While we're iterating over the function, add single location defs for
1937 /// dbg.declares to \p FnVarLocs.
1938 ///
1939 /// Variables that are interesting to this pass in are added to
1940 /// FnVarLocs->Variables first. TrackedVariablesVectorSize is set to the ID of
1941 /// the last interesting variable plus 1, meaning variables with ID 1
1942 /// (inclusive) to TrackedVariablesVectorSize (exclusive) are interesting. The
1943 /// subsequent variables are either stack homed or fully promoted.
1944 ///
1945 /// Finally, populate UntaggedStoreVars with a mapping of untagged stores to
1946 /// the stored-to variable fragments.
1947 ///
1948 /// These tasks are bundled together to reduce the number of times we need
1949 /// to iterate over the function as they can be achieved together in one pass.
1950 static AssignmentTrackingLowering::OverlapMap buildOverlapMapAndRecordDeclares(
1951     Function &Fn, FunctionVarLocsBuilder *FnVarLocs,
1952     const DenseSet<DebugAggregate> &VarsWithStackSlot,
1953     AssignmentTrackingLowering::UntaggedStoreAssignmentMap &UntaggedStoreVars,
1954     unsigned &TrackedVariablesVectorSize) {
1955   DenseSet<DebugVariable> Seen;
1956   // Map of Variable: [Fragments].
1957   DenseMap<DebugAggregate, SmallVector<DebugVariable, 8>> FragmentMap;
1958   // Iterate over all instructions:
1959   // - dbg.declare    -> add single location variable record
1960   // - dbg.*          -> Add fragments to FragmentMap
1961   // - untagged store -> Add fragments to FragmentMap and update
1962   //                     UntaggedStoreVars.
1963   // We need to add fragments for untagged stores too so that we can correctly
1964   // clobber overlapped fragment locations later.
1965   SmallVector<DbgDeclareInst *> Declares;
1966   for (auto &BB : Fn) {
1967     for (auto &I : BB) {
1968       if (auto *DDI = dyn_cast<DbgDeclareInst>(&I)) {
1969         Declares.push_back(DDI);
1970       } else if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
1971         DebugVariable DV = DebugVariable(DII);
1972         DebugAggregate DA = {DV.getVariable(), DV.getInlinedAt()};
1973         if (!VarsWithStackSlot.contains(DA))
1974           continue;
1975         if (Seen.insert(DV).second)
1976           FragmentMap[DA].push_back(DV);
1977       } else if (auto Info = getUntaggedStoreAssignmentInfo(
1978                      I, Fn.getParent()->getDataLayout())) {
1979         // Find markers linked to this alloca.
1980         for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(Info->Base)) {
1981           std::optional<DIExpression::FragmentInfo> FragInfo;
1982 
1983           // Skip this assignment if the affected bits are outside of the
1984           // variable fragment.
1985           if (!at::calculateFragmentIntersect(
1986                   I.getModule()->getDataLayout(), Info->Base,
1987                   Info->OffsetInBits, Info->SizeInBits, DAI, FragInfo) ||
1988               (FragInfo && FragInfo->SizeInBits == 0))
1989             continue;
1990 
1991           // FragInfo from calculateFragmentIntersect is nullopt if the
1992           // resultant fragment matches DAI's fragment or entire variable - in
1993           // which case copy the fragment info from DAI. If FragInfo is still
1994           // nullopt after the copy it means "no fragment info" instead, which
1995           // is how it is usually interpreted.
1996           if (!FragInfo)
1997             FragInfo = DAI->getExpression()->getFragmentInfo();
1998 
1999           DebugVariable DV = DebugVariable(DAI->getVariable(), FragInfo,
2000                                            DAI->getDebugLoc().getInlinedAt());
2001           DebugAggregate DA = {DV.getVariable(), DV.getInlinedAt()};
2002           if (!VarsWithStackSlot.contains(DA))
2003             continue;
2004 
2005           // Cache this info for later.
2006           UntaggedStoreVars[&I].push_back(
2007               {FnVarLocs->insertVariable(DV), *Info});
2008 
2009           if (Seen.insert(DV).second)
2010             FragmentMap[DA].push_back(DV);
2011         }
2012       }
2013     }
2014   }
2015 
2016   // Sort the fragment map for each DebugAggregate in ascending
2017   // order of fragment size - there should be no duplicates.
2018   for (auto &Pair : FragmentMap) {
2019     SmallVector<DebugVariable, 8> &Frags = Pair.second;
2020     std::sort(Frags.begin(), Frags.end(),
2021               [](const DebugVariable &Next, const DebugVariable &Elmt) {
2022                 return Elmt.getFragmentOrDefault().SizeInBits >
2023                        Next.getFragmentOrDefault().SizeInBits;
2024               });
2025     // Check for duplicates.
2026     assert(std::adjacent_find(Frags.begin(), Frags.end()) == Frags.end());
2027   }
2028 
2029   // Build the map.
2030   AssignmentTrackingLowering::OverlapMap Map;
2031   for (auto &Pair : FragmentMap) {
2032     auto &Frags = Pair.second;
2033     for (auto It = Frags.begin(), IEnd = Frags.end(); It != IEnd; ++It) {
2034       DIExpression::FragmentInfo Frag = It->getFragmentOrDefault();
2035       // Find the frags that this is contained within.
2036       //
2037       // Because Frags is sorted by size and none have the same offset and
2038       // size, we know that this frag can only be contained by subsequent
2039       // elements.
2040       SmallVector<DebugVariable, 8>::iterator OtherIt = It;
2041       ++OtherIt;
2042       VariableID ThisVar = FnVarLocs->insertVariable(*It);
2043       for (; OtherIt != IEnd; ++OtherIt) {
2044         DIExpression::FragmentInfo OtherFrag = OtherIt->getFragmentOrDefault();
2045         VariableID OtherVar = FnVarLocs->insertVariable(*OtherIt);
2046         if (fullyContains(OtherFrag, Frag))
2047           Map[OtherVar].push_back(ThisVar);
2048       }
2049     }
2050   }
2051 
2052   // VariableIDs are 1-based so the variable-tracking bitvector needs
2053   // NumVariables plus 1 bits.
2054   TrackedVariablesVectorSize = FnVarLocs->getNumVariables() + 1;
2055 
2056   // Finally, insert the declares afterwards, so the first IDs are all
2057   // partially stack homed vars.
2058   for (auto *DDI : Declares)
2059     FnVarLocs->addSingleLocVar(DebugVariable(DDI), DDI->getExpression(),
2060                                DDI->getDebugLoc(), DDI->getWrappedLocation());
2061   return Map;
2062 }
2063 
2064 bool AssignmentTrackingLowering::run(FunctionVarLocsBuilder *FnVarLocsBuilder) {
2065   if (Fn.size() > MaxNumBlocks) {
2066     LLVM_DEBUG(dbgs() << "[AT] Dropping var locs in: " << Fn.getName()
2067                       << ": too many blocks (" << Fn.size() << ")\n");
2068     at::deleteAll(&Fn);
2069     return false;
2070   }
2071 
2072   FnVarLocs = FnVarLocsBuilder;
2073 
2074   // The general structure here is inspired by VarLocBasedImpl.cpp
2075   // (LiveDebugValues).
2076 
2077   // Build the variable fragment overlap map.
2078   // Note that this pass doesn't handle partial overlaps correctly (FWIW
2079   // neither does LiveDebugVariables) because that is difficult to do and
2080   // appears to be rare occurance.
2081   VarContains = buildOverlapMapAndRecordDeclares(
2082       Fn, FnVarLocs, *VarsWithStackSlot, UntaggedStoreVars,
2083       TrackedVariablesVectorSize);
2084 
2085   // Prepare for traversal.
2086   ReversePostOrderTraversal<Function *> RPOT(&Fn);
2087   std::priority_queue<unsigned int, std::vector<unsigned int>,
2088                       std::greater<unsigned int>>
2089       Worklist;
2090   std::priority_queue<unsigned int, std::vector<unsigned int>,
2091                       std::greater<unsigned int>>
2092       Pending;
2093   DenseMap<unsigned int, BasicBlock *> OrderToBB;
2094   DenseMap<BasicBlock *, unsigned int> BBToOrder;
2095   { // Init OrderToBB and BBToOrder.
2096     unsigned int RPONumber = 0;
2097     for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
2098       OrderToBB[RPONumber] = *RI;
2099       BBToOrder[*RI] = RPONumber;
2100       Worklist.push(RPONumber);
2101       ++RPONumber;
2102     }
2103     LiveIn.init(RPONumber);
2104     LiveOut.init(RPONumber);
2105   }
2106 
2107   // Perform the traversal.
2108   //
2109   // This is a standard "union of predecessor outs" dataflow problem. To solve
2110   // it, we perform join() and process() using the two worklist method until
2111   // the LiveIn data for each block becomes unchanging. The "proof" that this
2112   // terminates can be put together by looking at the comments around LocKind,
2113   // Assignment, and the various join methods, which show that all the elements
2114   // involved are made up of join-semilattices; LiveIn(n) can only
2115   // monotonically increase in value throughout the dataflow.
2116   //
2117   SmallPtrSet<BasicBlock *, 16> Visited;
2118   while (!Worklist.empty()) {
2119     // We track what is on the pending worklist to avoid inserting the same
2120     // thing twice.
2121     SmallPtrSet<BasicBlock *, 16> OnPending;
2122     LLVM_DEBUG(dbgs() << "Processing Worklist\n");
2123     while (!Worklist.empty()) {
2124       BasicBlock *BB = OrderToBB[Worklist.top()];
2125       LLVM_DEBUG(dbgs() << "\nPop BB " << BB->getName() << "\n");
2126       Worklist.pop();
2127       bool InChanged = join(*BB, Visited);
2128       // Always consider LiveIn changed on the first visit.
2129       InChanged |= Visited.insert(BB).second;
2130       if (InChanged) {
2131         LLVM_DEBUG(dbgs() << BB->getName() << " has new InLocs, process it\n");
2132         // Mutate a copy of LiveIn while processing BB. After calling process
2133         // LiveSet is the LiveOut set for BB.
2134         BlockInfo LiveSet = LiveIn[BB];
2135 
2136         // Process the instructions in the block.
2137         process(*BB, &LiveSet);
2138 
2139         // Relatively expensive check: has anything changed in LiveOut for BB?
2140         if (LiveOut[BB] != LiveSet) {
2141           LLVM_DEBUG(dbgs() << BB->getName()
2142                             << " has new OutLocs, add succs to worklist: [ ");
2143           LiveOut[BB] = std::move(LiveSet);
2144           for (auto I = succ_begin(BB), E = succ_end(BB); I != E; I++) {
2145             if (OnPending.insert(*I).second) {
2146               LLVM_DEBUG(dbgs() << I->getName() << " ");
2147               Pending.push(BBToOrder[*I]);
2148             }
2149           }
2150           LLVM_DEBUG(dbgs() << "]\n");
2151         }
2152       }
2153     }
2154     Worklist.swap(Pending);
2155     // At this point, pending must be empty, since it was just the empty
2156     // worklist
2157     assert(Pending.empty() && "Pending should be empty");
2158   }
2159 
2160   // That's the hard part over. Now we just have some admin to do.
2161 
2162   // Record whether we inserted any intrinsics.
2163   bool InsertedAnyIntrinsics = false;
2164 
2165   // Identify and add defs for single location variables.
2166   //
2167   // Go through all of the defs that we plan to add. If the aggregate variable
2168   // it's a part of is not in the NotAlwaysStackHomed set we can emit a single
2169   // location def and omit the rest. Add an entry to AlwaysStackHomed so that
2170   // we can identify those uneeded defs later.
2171   DenseSet<DebugAggregate> AlwaysStackHomed;
2172   for (const auto &Pair : InsertBeforeMap) {
2173     const auto &Vec = Pair.second;
2174     for (VarLocInfo VarLoc : Vec) {
2175       DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2176       DebugAggregate Aggr{Var.getVariable(), Var.getInlinedAt()};
2177 
2178       // Skip this Var if it's not always stack homed.
2179       if (NotAlwaysStackHomed.contains(Aggr))
2180         continue;
2181 
2182       // Skip complex cases such as when different fragments of a variable have
2183       // been split into different allocas. Skipping in this case means falling
2184       // back to using a list of defs (which could reduce coverage, but is no
2185       // less correct).
2186       bool Simple =
2187           VarLoc.Expr->getNumElements() == 1 && VarLoc.Expr->startsWithDeref();
2188       if (!Simple) {
2189         NotAlwaysStackHomed.insert(Aggr);
2190         continue;
2191       }
2192 
2193       // All source assignments to this variable remain and all stores to any
2194       // part of the variable store to the same address (with varying
2195       // offsets). We can just emit a single location for the whole variable.
2196       //
2197       // Unless we've already done so, create the single location def now.
2198       if (AlwaysStackHomed.insert(Aggr).second) {
2199         assert(!VarLoc.Values.hasArgList());
2200         // TODO: When more complex cases are handled VarLoc.Expr should be
2201         // built appropriately rather than always using an empty DIExpression.
2202         // The assert below is a reminder.
2203         assert(Simple);
2204         VarLoc.Expr = DIExpression::get(Fn.getContext(), std::nullopt);
2205         DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2206         FnVarLocs->addSingleLocVar(Var, VarLoc.Expr, VarLoc.DL, VarLoc.Values);
2207         InsertedAnyIntrinsics = true;
2208       }
2209     }
2210   }
2211 
2212   // Insert the other DEFs.
2213   for (const auto &[InsertBefore, Vec] : InsertBeforeMap) {
2214     SmallVector<VarLocInfo> NewDefs;
2215     for (const VarLocInfo &VarLoc : Vec) {
2216       DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2217       DebugAggregate Aggr{Var.getVariable(), Var.getInlinedAt()};
2218       // If this variable is always stack homed then we have already inserted a
2219       // dbg.declare and deleted this dbg.value.
2220       if (AlwaysStackHomed.contains(Aggr))
2221         continue;
2222       NewDefs.push_back(VarLoc);
2223       InsertedAnyIntrinsics = true;
2224     }
2225 
2226     FnVarLocs->setWedge(InsertBefore, std::move(NewDefs));
2227   }
2228 
2229   InsertedAnyIntrinsics |= emitPromotedVarLocs(FnVarLocs);
2230 
2231   return InsertedAnyIntrinsics;
2232 }
2233 
2234 bool AssignmentTrackingLowering::emitPromotedVarLocs(
2235     FunctionVarLocsBuilder *FnVarLocs) {
2236   bool InsertedAnyIntrinsics = false;
2237   // Go through every block, translating debug intrinsics for fully promoted
2238   // variables into FnVarLocs location defs. No analysis required for these.
2239   for (auto &BB : Fn) {
2240     for (auto &I : BB) {
2241       // Skip instructions other than dbg.values and dbg.assigns.
2242       auto *DVI = dyn_cast<DbgValueInst>(&I);
2243       if (!DVI)
2244         continue;
2245       // Skip variables that haven't been promoted - we've dealt with those
2246       // already.
2247       if (VarsWithStackSlot->contains(getAggregate(DVI)))
2248         continue;
2249       Instruction *InsertBefore = I.getNextNode();
2250       assert(InsertBefore && "Unexpected: debug intrinsics after a terminator");
2251       FnVarLocs->addVarLoc(InsertBefore, DebugVariable(DVI),
2252                            DVI->getExpression(), DVI->getDebugLoc(),
2253                            DVI->getWrappedLocation());
2254       InsertedAnyIntrinsics = true;
2255     }
2256   }
2257   return InsertedAnyIntrinsics;
2258 }
2259 
2260 /// Remove redundant definitions within sequences of consecutive location defs.
2261 /// This is done using a backward scan to keep the last def describing a
2262 /// specific variable/fragment.
2263 ///
2264 /// This implements removeRedundantDbgInstrsUsingBackwardScan from
2265 /// lib/Transforms/Utils/BasicBlockUtils.cpp for locations described with
2266 /// FunctionVarLocsBuilder instead of with intrinsics.
2267 static bool
2268 removeRedundantDbgLocsUsingBackwardScan(const BasicBlock *BB,
2269                                         FunctionVarLocsBuilder &FnVarLocs) {
2270   bool Changed = false;
2271   SmallDenseMap<DebugAggregate, BitVector> VariableDefinedBits;
2272   // Scan over the entire block, not just over the instructions mapped by
2273   // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
2274   // instructions.
2275   for (const Instruction &I : reverse(*BB)) {
2276     if (!isa<DbgVariableIntrinsic>(I)) {
2277       // Sequence of consecutive defs ended. Clear map for the next one.
2278       VariableDefinedBits.clear();
2279     }
2280 
2281     // Get the location defs that start just before this instruction.
2282     const auto *Locs = FnVarLocs.getWedge(&I);
2283     if (!Locs)
2284       continue;
2285 
2286     NumWedgesScanned++;
2287     bool ChangedThisWedge = false;
2288     // The new pruned set of defs, reversed because we're scanning backwards.
2289     SmallVector<VarLocInfo> NewDefsReversed;
2290 
2291     // Iterate over the existing defs in reverse.
2292     for (auto RIt = Locs->rbegin(), REnd = Locs->rend(); RIt != REnd; ++RIt) {
2293       NumDefsScanned++;
2294       DebugAggregate Aggr =
2295           getAggregate(FnVarLocs.getVariable(RIt->VariableID));
2296       uint64_t SizeInBits = Aggr.first->getSizeInBits().value_or(0);
2297 
2298       if (SizeInBits == 0) {
2299         // If the size is unknown (0) then keep this location def to be safe.
2300         NewDefsReversed.push_back(*RIt);
2301         continue;
2302       }
2303 
2304       // Only keep this location definition if it is not fully eclipsed by
2305       // other definitions in this wedge that come after it
2306 
2307       // Inert the bits the location definition defines.
2308       auto InsertResult =
2309           VariableDefinedBits.try_emplace(Aggr, BitVector(SizeInBits));
2310       bool FirstDefinition = InsertResult.second;
2311       BitVector &DefinedBits = InsertResult.first->second;
2312 
2313       DIExpression::FragmentInfo Fragment =
2314           RIt->Expr->getFragmentInfo().value_or(
2315               DIExpression::FragmentInfo(SizeInBits, 0));
2316       bool InvalidFragment = Fragment.endInBits() > SizeInBits;
2317 
2318       // If this defines any previously undefined bits, keep it.
2319       if (FirstDefinition || InvalidFragment ||
2320           DefinedBits.find_first_unset_in(Fragment.startInBits(),
2321                                           Fragment.endInBits()) != -1) {
2322         if (!InvalidFragment)
2323           DefinedBits.set(Fragment.startInBits(), Fragment.endInBits());
2324         NewDefsReversed.push_back(*RIt);
2325         continue;
2326       }
2327 
2328       // Redundant def found: throw it away. Since the wedge of defs is being
2329       // rebuilt, doing nothing is the same as deleting an entry.
2330       ChangedThisWedge = true;
2331       NumDefsRemoved++;
2332     }
2333 
2334     // Un-reverse the defs and replace the wedge with the pruned version.
2335     if (ChangedThisWedge) {
2336       std::reverse(NewDefsReversed.begin(), NewDefsReversed.end());
2337       FnVarLocs.setWedge(&I, std::move(NewDefsReversed));
2338       NumWedgesChanged++;
2339       Changed = true;
2340     }
2341   }
2342 
2343   return Changed;
2344 }
2345 
2346 /// Remove redundant location defs using a forward scan. This can remove a
2347 /// location definition that is redundant due to indicating that a variable has
2348 /// the same value as is already being indicated by an earlier def.
2349 ///
2350 /// This implements removeRedundantDbgInstrsUsingForwardScan from
2351 /// lib/Transforms/Utils/BasicBlockUtils.cpp for locations described with
2352 /// FunctionVarLocsBuilder instead of with intrinsics
2353 static bool
2354 removeRedundantDbgLocsUsingForwardScan(const BasicBlock *BB,
2355                                        FunctionVarLocsBuilder &FnVarLocs) {
2356   bool Changed = false;
2357   DenseMap<DebugVariable, std::pair<RawLocationWrapper, DIExpression *>>
2358       VariableMap;
2359 
2360   // Scan over the entire block, not just over the instructions mapped by
2361   // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
2362   // instructions.
2363   for (const Instruction &I : *BB) {
2364     // Get the defs that come just before this instruction.
2365     const auto *Locs = FnVarLocs.getWedge(&I);
2366     if (!Locs)
2367       continue;
2368 
2369     NumWedgesScanned++;
2370     bool ChangedThisWedge = false;
2371     // The new pruned set of defs.
2372     SmallVector<VarLocInfo> NewDefs;
2373 
2374     // Iterate over the existing defs.
2375     for (const VarLocInfo &Loc : *Locs) {
2376       NumDefsScanned++;
2377       DebugVariable Key(FnVarLocs.getVariable(Loc.VariableID).getVariable(),
2378                         std::nullopt, Loc.DL.getInlinedAt());
2379       auto VMI = VariableMap.find(Key);
2380 
2381       // Update the map if we found a new value/expression describing the
2382       // variable, or if the variable wasn't mapped already.
2383       if (VMI == VariableMap.end() || VMI->second.first != Loc.Values ||
2384           VMI->second.second != Loc.Expr) {
2385         VariableMap[Key] = {Loc.Values, Loc.Expr};
2386         NewDefs.push_back(Loc);
2387         continue;
2388       }
2389 
2390       // Did not insert this Loc, which is the same as removing it.
2391       ChangedThisWedge = true;
2392       NumDefsRemoved++;
2393     }
2394 
2395     // Replace the existing wedge with the pruned version.
2396     if (ChangedThisWedge) {
2397       FnVarLocs.setWedge(&I, std::move(NewDefs));
2398       NumWedgesChanged++;
2399       Changed = true;
2400     }
2401   }
2402 
2403   return Changed;
2404 }
2405 
2406 static bool
2407 removeUndefDbgLocsFromEntryBlock(const BasicBlock *BB,
2408                                  FunctionVarLocsBuilder &FnVarLocs) {
2409   assert(BB->isEntryBlock());
2410   // Do extra work to ensure that we remove semantically unimportant undefs.
2411   //
2412   // This is to work around the fact that SelectionDAG will hoist dbg.values
2413   // using argument values to the top of the entry block. That can move arg
2414   // dbg.values before undef and constant dbg.values which they previously
2415   // followed. The easiest thing to do is to just try to feed SelectionDAG
2416   // input it's happy with.
2417   //
2418   // Map of {Variable x: Fragments y} where the fragments y of variable x have
2419   // have at least one non-undef location defined already. Don't use directly,
2420   // instead call DefineBits and HasDefinedBits.
2421   SmallDenseMap<DebugAggregate, SmallDenseSet<DIExpression::FragmentInfo>>
2422       VarsWithDef;
2423   // Specify that V (a fragment of A) has a non-undef location.
2424   auto DefineBits = [&VarsWithDef](DebugAggregate A, DebugVariable V) {
2425     VarsWithDef[A].insert(V.getFragmentOrDefault());
2426   };
2427   // Return true if a non-undef location has been defined for V (a fragment of
2428   // A). Doesn't imply that the location is currently non-undef, just that a
2429   // non-undef location has been seen previously.
2430   auto HasDefinedBits = [&VarsWithDef](DebugAggregate A, DebugVariable V) {
2431     auto FragsIt = VarsWithDef.find(A);
2432     if (FragsIt == VarsWithDef.end())
2433       return false;
2434     return llvm::any_of(FragsIt->second, [V](auto Frag) {
2435       return DIExpression::fragmentsOverlap(Frag, V.getFragmentOrDefault());
2436     });
2437   };
2438 
2439   bool Changed = false;
2440   DenseMap<DebugVariable, std::pair<Value *, DIExpression *>> VariableMap;
2441 
2442   // Scan over the entire block, not just over the instructions mapped by
2443   // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
2444   // instructions.
2445   for (const Instruction &I : *BB) {
2446     // Get the defs that come just before this instruction.
2447     const auto *Locs = FnVarLocs.getWedge(&I);
2448     if (!Locs)
2449       continue;
2450 
2451     NumWedgesScanned++;
2452     bool ChangedThisWedge = false;
2453     // The new pruned set of defs.
2454     SmallVector<VarLocInfo> NewDefs;
2455 
2456     // Iterate over the existing defs.
2457     for (const VarLocInfo &Loc : *Locs) {
2458       NumDefsScanned++;
2459       DebugAggregate Aggr{FnVarLocs.getVariable(Loc.VariableID).getVariable(),
2460                           Loc.DL.getInlinedAt()};
2461       DebugVariable Var = FnVarLocs.getVariable(Loc.VariableID);
2462 
2463       // Remove undef entries that are encountered before any non-undef
2464       // intrinsics from the entry block.
2465       if (Loc.Values.isKillLocation(Loc.Expr) && !HasDefinedBits(Aggr, Var)) {
2466         // Did not insert this Loc, which is the same as removing it.
2467         NumDefsRemoved++;
2468         ChangedThisWedge = true;
2469         continue;
2470       }
2471 
2472       DefineBits(Aggr, Var);
2473       NewDefs.push_back(Loc);
2474     }
2475 
2476     // Replace the existing wedge with the pruned version.
2477     if (ChangedThisWedge) {
2478       FnVarLocs.setWedge(&I, std::move(NewDefs));
2479       NumWedgesChanged++;
2480       Changed = true;
2481     }
2482   }
2483 
2484   return Changed;
2485 }
2486 
2487 static bool removeRedundantDbgLocs(const BasicBlock *BB,
2488                                    FunctionVarLocsBuilder &FnVarLocs) {
2489   bool MadeChanges = false;
2490   MadeChanges |= removeRedundantDbgLocsUsingBackwardScan(BB, FnVarLocs);
2491   if (BB->isEntryBlock())
2492     MadeChanges |= removeUndefDbgLocsFromEntryBlock(BB, FnVarLocs);
2493   MadeChanges |= removeRedundantDbgLocsUsingForwardScan(BB, FnVarLocs);
2494 
2495   if (MadeChanges)
2496     LLVM_DEBUG(dbgs() << "Removed redundant dbg locs from: " << BB->getName()
2497                       << "\n");
2498   return MadeChanges;
2499 }
2500 
2501 static DenseSet<DebugAggregate> findVarsWithStackSlot(Function &Fn) {
2502   DenseSet<DebugAggregate> Result;
2503   for (auto &BB : Fn) {
2504     for (auto &I : BB) {
2505       // Any variable linked to an instruction is considered
2506       // interesting. Ideally we only need to check Allocas, however, a
2507       // DIAssignID might get dropped from an alloca but not stores. In that
2508       // case, we need to consider the variable interesting for NFC behaviour
2509       // with this change. TODO: Consider only looking at allocas.
2510       for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(&I)) {
2511         Result.insert({DAI->getVariable(), DAI->getDebugLoc().getInlinedAt()});
2512       }
2513     }
2514   }
2515   return Result;
2516 }
2517 
2518 static void analyzeFunction(Function &Fn, const DataLayout &Layout,
2519                             FunctionVarLocsBuilder *FnVarLocs) {
2520   // The analysis will generate location definitions for all variables, but we
2521   // only need to perform a dataflow on the set of variables which have a stack
2522   // slot. Find those now.
2523   DenseSet<DebugAggregate> VarsWithStackSlot = findVarsWithStackSlot(Fn);
2524 
2525   bool Changed = false;
2526 
2527   // Use a scope block to clean up AssignmentTrackingLowering before running
2528   // MemLocFragmentFill to reduce peak memory consumption.
2529   {
2530     AssignmentTrackingLowering Pass(Fn, Layout, &VarsWithStackSlot);
2531     Changed = Pass.run(FnVarLocs);
2532   }
2533 
2534   if (Changed) {
2535     MemLocFragmentFill Pass(Fn, &VarsWithStackSlot,
2536                             shouldCoalesceFragments(Fn));
2537     Pass.run(FnVarLocs);
2538 
2539     // Remove redundant entries. As well as reducing memory consumption and
2540     // avoiding waiting cycles later by burning some now, this has another
2541     // important job. That is to work around some SelectionDAG quirks. See
2542     // removeRedundantDbgLocsUsingForwardScan comments for more info on that.
2543     for (auto &BB : Fn)
2544       removeRedundantDbgLocs(&BB, *FnVarLocs);
2545   }
2546 }
2547 
2548 bool AssignmentTrackingAnalysis::runOnFunction(Function &F) {
2549   if (!isAssignmentTrackingEnabled(*F.getParent()))
2550     return false;
2551 
2552   LLVM_DEBUG(dbgs() << "AssignmentTrackingAnalysis run on " << F.getName()
2553                     << "\n");
2554   auto DL = std::make_unique<DataLayout>(F.getParent());
2555 
2556   // Clear previous results.
2557   Results->clear();
2558 
2559   FunctionVarLocsBuilder Builder;
2560   analyzeFunction(F, *DL.get(), &Builder);
2561 
2562   // Save these results.
2563   Results->init(Builder);
2564 
2565   if (PrintResults && isFunctionInPrintList(F.getName()))
2566     Results->print(errs(), F);
2567 
2568   // Return false because this pass does not modify the function.
2569   return false;
2570 }
2571 
2572 AssignmentTrackingAnalysis::AssignmentTrackingAnalysis()
2573     : FunctionPass(ID), Results(std::make_unique<FunctionVarLocs>()) {}
2574 
2575 char AssignmentTrackingAnalysis::ID = 0;
2576 
2577 INITIALIZE_PASS(AssignmentTrackingAnalysis, DEBUG_TYPE,
2578                 "Assignment Tracking Analysis", false, true)
2579