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