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