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