xref: /llvm-project/llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp (revision b6942a2880c578d2eab89ecb75e637aac41c5e51)
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 const char *locStr(AssignmentTrackingLowering::LocKind Loc) {
1221   using LocKind = AssignmentTrackingLowering::LocKind;
1222   switch (Loc) {
1223   case LocKind::Val:
1224     return "Val";
1225   case LocKind::Mem:
1226     return "Mem";
1227   case LocKind::None:
1228     return "None";
1229   };
1230   llvm_unreachable("unknown LocKind");
1231 }
1232 
1233 void AssignmentTrackingLowering::emitDbgValue(
1234     AssignmentTrackingLowering::LocKind Kind,
1235     const DbgVariableIntrinsic *Source, Instruction *After) {
1236 
1237   DILocation *DL = Source->getDebugLoc();
1238   auto Emit = [this, Source, After, DL](Value *Val, DIExpression *Expr) {
1239     assert(Expr);
1240     // It's possible that getVariableLocationOp(0) is null. Occurs in
1241     // llvm/test/DebugInfo/Generic/2010-05-03-OriginDIE.ll Treat it as undef.
1242     if (!Val)
1243       Val = UndefValue::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 (Value *Val = DAI->getAddress()) {
1265       DIExpression *Expr = DAI->getAddressExpression();
1266       assert(!Expr->getFragmentInfo() &&
1267              "fragment info should be stored in value-expression only");
1268       // Copy the fragment info over from the value-expression to the new
1269       // DIExpression.
1270       if (auto OptFragInfo = Source->getExpression()->getFragmentInfo()) {
1271         auto FragInfo = *OptFragInfo;
1272         Expr = *DIExpression::createFragmentExpression(
1273             Expr, FragInfo.OffsetInBits, FragInfo.SizeInBits);
1274       }
1275       // The address-expression has an implicit deref, add it now.
1276       std::tie(Val, Expr) =
1277           walkToAllocaAndPrependOffsetDeref(Layout, Val, Expr);
1278       Emit(Val, Expr);
1279       return;
1280     } else {
1281       // The address isn't valid so treat this as a non-memory def.
1282       Kind = LocKind::Val;
1283     }
1284   }
1285 
1286   if (Kind == LocKind::Val) {
1287     /// Get the value component, converting to Undef if it is variadic.
1288     Value *Val =
1289         Source->hasArgList()
1290             ? UndefValue::get(Source->getVariableLocationOp(0)->getType())
1291             : Source->getVariableLocationOp(0);
1292     Emit(Val, Source->getExpression());
1293     return;
1294   }
1295 
1296   if (Kind == LocKind::None) {
1297     Value *Val = UndefValue::get(Source->getVariableLocationOp(0)->getType());
1298     Emit(Val, Source->getExpression());
1299     return;
1300   }
1301 }
1302 
1303 void AssignmentTrackingLowering::processNonDbgInstruction(
1304     Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1305   if (I.hasMetadata(LLVMContext::MD_DIAssignID))
1306     processTaggedInstruction(I, LiveSet);
1307   else
1308     processUntaggedInstruction(I, LiveSet);
1309 }
1310 
1311 void AssignmentTrackingLowering::processUntaggedInstruction(
1312     Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1313   // Interpret stack stores that are not tagged as an assignment in memory for
1314   // the variables associated with that address. These stores may not be tagged
1315   // because a) the store cannot be represented using dbg.assigns (non-const
1316   // length or offset) or b) the tag was accidentally dropped during
1317   // optimisations. For these stores we fall back to assuming that the stack
1318   // home is a valid location for the variables. The benefit is that this
1319   // prevents us missing an assignment and therefore incorrectly maintaining
1320   // earlier location definitions, and in many cases it should be a reasonable
1321   // assumption. However, this will occasionally lead to slight
1322   // inaccuracies. The value of a hoisted untagged store will be visible
1323   // "early", for example.
1324   assert(!I.hasMetadata(LLVMContext::MD_DIAssignID));
1325   auto It = UntaggedStoreVars.find(&I);
1326   if (It == UntaggedStoreVars.end())
1327     return; // No variables associated with the store destination.
1328 
1329   LLVM_DEBUG(dbgs() << "processUntaggedInstruction on UNTAGGED INST " << I
1330                     << "\n");
1331   // Iterate over the variables that this store affects, add a NoneOrPhi dbg
1332   // and mem def, set lockind to Mem, and emit a location def for each.
1333   for (auto [Var, Info] : It->second) {
1334     // This instruction is treated as both a debug and memory assignment,
1335     // meaning the memory location should be used. We don't have an assignment
1336     // ID though so use Assignment::makeNoneOrPhi() to create an imaginary one.
1337     addMemDef(LiveSet, Var, Assignment::makeNoneOrPhi());
1338     addDbgDef(LiveSet, Var, Assignment::makeNoneOrPhi());
1339     setLocKind(LiveSet, Var, LocKind::Mem);
1340     LLVM_DEBUG(dbgs() << "  setting Stack LocKind to: " << locStr(LocKind::Mem)
1341                       << "\n");
1342     // Build the dbg location def to insert.
1343     //
1344     // DIExpression: Add fragment and offset.
1345     DebugVariable V = FnVarLocs->getVariable(Var);
1346     DIExpression *DIE = DIExpression::get(I.getContext(), std::nullopt);
1347     if (auto Frag = V.getFragment()) {
1348       auto R = DIExpression::createFragmentExpression(DIE, Frag->OffsetInBits,
1349                                                       Frag->SizeInBits);
1350       assert(R && "unexpected createFragmentExpression failure");
1351       DIE = *R;
1352     }
1353     SmallVector<uint64_t, 3> Ops;
1354     if (Info.OffsetInBits)
1355       Ops = {dwarf::DW_OP_plus_uconst, Info.OffsetInBits / 8};
1356     Ops.push_back(dwarf::DW_OP_deref);
1357     DIE = DIExpression::prependOpcodes(DIE, Ops, /*StackValue=*/false,
1358                                        /*EntryValue=*/false);
1359     // Find a suitable insert point.
1360     Instruction *InsertBefore = I.getNextNode();
1361     assert(InsertBefore && "Shouldn't be inserting after a terminator");
1362 
1363     // Get DILocation for this unrecorded assignment.
1364     DILocation *InlinedAt = const_cast<DILocation *>(V.getInlinedAt());
1365     const DILocation *DILoc = DILocation::get(
1366         Fn.getContext(), 0, 0, V.getVariable()->getScope(), InlinedAt);
1367 
1368     VarLocInfo VarLoc;
1369     VarLoc.VariableID = static_cast<VariableID>(Var);
1370     VarLoc.Expr = DIE;
1371     VarLoc.V = const_cast<AllocaInst *>(Info.Base);
1372     VarLoc.DL = DILoc;
1373     // 3. Insert it into the map for later.
1374     InsertBeforeMap[InsertBefore].push_back(VarLoc);
1375   }
1376 }
1377 
1378 void AssignmentTrackingLowering::processTaggedInstruction(
1379     Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1380   auto Linked = at::getAssignmentMarkers(&I);
1381   // No dbg.assign intrinsics linked.
1382   // FIXME: All vars that have a stack slot this store modifies that don't have
1383   // a dbg.assign linked to it should probably treat this like an untagged
1384   // store.
1385   if (Linked.empty())
1386     return;
1387 
1388   LLVM_DEBUG(dbgs() << "processTaggedInstruction on " << I << "\n");
1389   for (DbgAssignIntrinsic *DAI : Linked) {
1390     VariableID Var = getVariableID(DebugVariable(DAI));
1391     // Something has gone wrong if VarsWithStackSlot doesn't contain a variable
1392     // that is linked to a store.
1393     assert(VarsWithStackSlot->count(getAggregate(DAI)) &&
1394            "expected DAI's variable to have stack slot");
1395 
1396     Assignment AV = Assignment::makeFromMemDef(getIDFromInst(I));
1397     addMemDef(LiveSet, Var, AV);
1398 
1399     LLVM_DEBUG(dbgs() << "   linked to " << *DAI << "\n");
1400     LLVM_DEBUG(dbgs() << "   LiveLoc " << locStr(getLocKind(LiveSet, Var))
1401                       << " -> ");
1402 
1403     // The last assignment to the stack is now AV. Check if the last debug
1404     // assignment has a matching Assignment.
1405     if (hasVarWithAssignment(Var, AV, LiveSet->DebugValue)) {
1406       // The StackHomeValue and DebugValue for this variable match so we can
1407       // emit a stack home location here.
1408       LLVM_DEBUG(dbgs() << "Mem, Stack matches Debug program\n";);
1409       LLVM_DEBUG(dbgs() << "   Stack val: "; AV.dump(dbgs()); dbgs() << "\n");
1410       LLVM_DEBUG(dbgs() << "   Debug val: ";
1411                  LiveSet->DebugValue[Var].dump(dbgs()); dbgs() << "\n");
1412       setLocKind(LiveSet, Var, LocKind::Mem);
1413       emitDbgValue(LocKind::Mem, DAI, &I);
1414       continue;
1415     }
1416 
1417     // The StackHomeValue and DebugValue for this variable do not match. I.e.
1418     // The value currently stored in the stack is not what we'd expect to
1419     // see, so we cannot use emit a stack home location here. Now we will
1420     // look at the live LocKind for the variable and determine an appropriate
1421     // dbg.value to emit.
1422     LocKind PrevLoc = getLocKind(LiveSet, Var);
1423     switch (PrevLoc) {
1424     case LocKind::Val: {
1425       // The value in memory in memory has changed but we're not currently
1426       // using the memory location. Do nothing.
1427       LLVM_DEBUG(dbgs() << "Val, (unchanged)\n";);
1428       setLocKind(LiveSet, Var, LocKind::Val);
1429     } break;
1430     case LocKind::Mem: {
1431       // There's been an assignment to memory that we were using as a
1432       // location for this variable, and the Assignment doesn't match what
1433       // we'd expect to see in memory.
1434       if (LiveSet->DebugValue[Var].Status == Assignment::NoneOrPhi) {
1435         // We need to terminate any previously open location now.
1436         LLVM_DEBUG(dbgs() << "None, No Debug value available\n";);
1437         setLocKind(LiveSet, Var, LocKind::None);
1438         emitDbgValue(LocKind::None, DAI, &I);
1439       } else {
1440         // The previous DebugValue Value can be used here.
1441         LLVM_DEBUG(dbgs() << "Val, Debug value is Known\n";);
1442         setLocKind(LiveSet, Var, LocKind::Val);
1443         Assignment PrevAV = LiveSet->DebugValue.lookup(Var);
1444         if (PrevAV.Source) {
1445           emitDbgValue(LocKind::Val, PrevAV.Source, &I);
1446         } else {
1447           // PrevAV.Source is nullptr so we must emit undef here.
1448           emitDbgValue(LocKind::None, DAI, &I);
1449         }
1450       }
1451     } break;
1452     case LocKind::None: {
1453       // There's been an assignment to memory and we currently are
1454       // not tracking a location for the variable. Do not emit anything.
1455       LLVM_DEBUG(dbgs() << "None, (unchanged)\n";);
1456       setLocKind(LiveSet, Var, LocKind::None);
1457     } break;
1458     }
1459   }
1460 }
1461 
1462 void AssignmentTrackingLowering::processDbgAssign(DbgAssignIntrinsic &DAI,
1463                                                   BlockInfo *LiveSet) {
1464   // Only bother tracking variables that are at some point stack homed. Other
1465   // variables can be dealt with trivially later.
1466   if (!VarsWithStackSlot->count(getAggregate(&DAI)))
1467     return;
1468 
1469   VariableID Var = getVariableID(DebugVariable(&DAI));
1470   Assignment AV = Assignment::make(getIDFromMarker(DAI), &DAI);
1471   addDbgDef(LiveSet, Var, AV);
1472 
1473   LLVM_DEBUG(dbgs() << "processDbgAssign on " << DAI << "\n";);
1474   LLVM_DEBUG(dbgs() << "   LiveLoc " << locStr(getLocKind(LiveSet, Var))
1475                     << " -> ");
1476 
1477   // Check if the DebugValue and StackHomeValue both hold the same
1478   // Assignment.
1479   if (hasVarWithAssignment(Var, AV, LiveSet->StackHomeValue)) {
1480     // They match. We can use the stack home because the debug intrinsics state
1481     // that an assignment happened here, and we know that specific assignment
1482     // was the last one to take place in memory for this variable.
1483     LocKind Kind;
1484     if (isa<UndefValue>(DAI.getAddress())) {
1485       // Address may be undef to indicate that although the store does take
1486       // place, this part of the original store has been elided.
1487       LLVM_DEBUG(
1488           dbgs() << "Val, Stack matches Debug program but address is undef\n";);
1489       Kind = LocKind::Val;
1490     } else {
1491       LLVM_DEBUG(dbgs() << "Mem, Stack matches Debug program\n";);
1492       Kind = LocKind::Mem;
1493     };
1494     setLocKind(LiveSet, Var, Kind);
1495     emitDbgValue(Kind, &DAI, &DAI);
1496   } else {
1497     // The last assignment to the memory location isn't the one that we want to
1498     // show to the user so emit a dbg.value(Value). Value may be undef.
1499     LLVM_DEBUG(dbgs() << "Val, Stack contents is unknown\n";);
1500     setLocKind(LiveSet, Var, LocKind::Val);
1501     emitDbgValue(LocKind::Val, &DAI, &DAI);
1502   }
1503 }
1504 
1505 void AssignmentTrackingLowering::processDbgValue(DbgValueInst &DVI,
1506                                                  BlockInfo *LiveSet) {
1507   // Only other tracking variables that are at some point stack homed.
1508   // Other variables can be dealt with trivally later.
1509   if (!VarsWithStackSlot->count(getAggregate(&DVI)))
1510     return;
1511 
1512   VariableID Var = getVariableID(DebugVariable(&DVI));
1513   // We have no ID to create an Assignment with so we mark this assignment as
1514   // NoneOrPhi. Note that the dbg.value still exists, we just cannot determine
1515   // the assignment responsible for setting this value.
1516   // This is fine; dbg.values are essentially interchangable with unlinked
1517   // dbg.assigns, and some passes such as mem2reg and instcombine add them to
1518   // PHIs for promoted variables.
1519   Assignment AV = Assignment::makeNoneOrPhi();
1520   addDbgDef(LiveSet, Var, AV);
1521 
1522   LLVM_DEBUG(dbgs() << "processDbgValue on " << DVI << "\n";);
1523   LLVM_DEBUG(dbgs() << "   LiveLoc " << locStr(getLocKind(LiveSet, Var))
1524                     << " -> Val, dbg.value override");
1525 
1526   setLocKind(LiveSet, Var, LocKind::Val);
1527   emitDbgValue(LocKind::Val, &DVI, &DVI);
1528 }
1529 
1530 void AssignmentTrackingLowering::processDbgInstruction(
1531     Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1532   assert(!isa<DbgAddrIntrinsic>(&I) && "unexpected dbg.addr");
1533   if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))
1534     processDbgAssign(*DAI, LiveSet);
1535   else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
1536     processDbgValue(*DVI, LiveSet);
1537 }
1538 
1539 void AssignmentTrackingLowering::resetInsertionPoint(Instruction &After) {
1540   assert(!After.isTerminator() && "Can't insert after a terminator");
1541   auto R = InsertBeforeMap.find(After.getNextNode());
1542   if (R == InsertBeforeMap.end())
1543     return;
1544   R->second.clear();
1545 }
1546 
1547 void AssignmentTrackingLowering::process(BasicBlock &BB, BlockInfo *LiveSet) {
1548   for (auto II = BB.begin(), EI = BB.end(); II != EI;) {
1549     assert(VarsTouchedThisFrame.empty());
1550     // Process the instructions in "frames". A "frame" includes a single
1551     // non-debug instruction followed any debug instructions before the
1552     // next non-debug instruction.
1553     if (!isa<DbgInfoIntrinsic>(&*II)) {
1554       if (II->isTerminator())
1555         break;
1556       resetInsertionPoint(*II);
1557       processNonDbgInstruction(*II, LiveSet);
1558       assert(LiveSet->isValid());
1559       ++II;
1560     }
1561     while (II != EI) {
1562       if (!isa<DbgInfoIntrinsic>(&*II))
1563         break;
1564       resetInsertionPoint(*II);
1565       processDbgInstruction(*II, LiveSet);
1566       assert(LiveSet->isValid());
1567       ++II;
1568     }
1569 
1570     // We've processed everything in the "frame". Now determine which variables
1571     // cannot be represented by a dbg.declare.
1572     for (auto Var : VarsTouchedThisFrame) {
1573       LocKind Loc = getLocKind(LiveSet, Var);
1574       // If a variable's LocKind is anything other than LocKind::Mem then we
1575       // must note that it cannot be represented with a dbg.declare.
1576       // Note that this check is enough without having to check the result of
1577       // joins() because for join to produce anything other than Mem after
1578       // we've already seen a Mem we'd be joining None or Val with Mem. In that
1579       // case, we've already hit this codepath when we set the LocKind to Val
1580       // or None in that block.
1581       if (Loc != LocKind::Mem) {
1582         DebugVariable DbgVar = FnVarLocs->getVariable(Var);
1583         DebugAggregate Aggr{DbgVar.getVariable(), DbgVar.getInlinedAt()};
1584         NotAlwaysStackHomed.insert(Aggr);
1585       }
1586     }
1587     VarsTouchedThisFrame.clear();
1588   }
1589 }
1590 
1591 AssignmentTrackingLowering::LocKind
1592 AssignmentTrackingLowering::joinKind(LocKind A, LocKind B) {
1593   // Partial order:
1594   // None > Mem, Val
1595   return A == B ? A : LocKind::None;
1596 }
1597 
1598 AssignmentTrackingLowering::LocMap
1599 AssignmentTrackingLowering::joinLocMap(const LocMap &A, const LocMap &B) {
1600   // Join A and B.
1601   //
1602   // U = join(a, b) for a in A, b in B where Var(a) == Var(b)
1603   // D = join(x, ⊤) for x where Var(x) is in A xor B
1604   // Join = U ∪ D
1605   //
1606   // This is achieved by performing a join on elements from A and B with
1607   // variables common to both A and B (join elements indexed by var intersect),
1608   // then adding LocKind::None elements for vars in A xor B. The latter part is
1609   // equivalent to performing join on elements with variables in A xor B with
1610   // LocKind::None (⊤) since join(x, ⊤) = ⊤.
1611   LocMap Join;
1612   SmallVector<VariableID, 16> SymmetricDifference;
1613   // Insert the join of the elements with common vars into Join. Add the
1614   // remaining elements to into SymmetricDifference.
1615   for (const auto &[Var, Loc] : A) {
1616     // If this Var doesn't exist in B then add it to the symmetric difference
1617     // set.
1618     auto R = B.find(Var);
1619     if (R == B.end()) {
1620       SymmetricDifference.push_back(Var);
1621       continue;
1622     }
1623     // There is an entry for Var in both, join it.
1624     Join[Var] = joinKind(Loc, R->second);
1625   }
1626   unsigned IntersectSize = Join.size();
1627   (void)IntersectSize;
1628 
1629   // Add the elements in B with variables that are not in A into
1630   // SymmetricDifference.
1631   for (const auto &Pair : B) {
1632     VariableID Var = Pair.first;
1633     if (A.count(Var) == 0)
1634       SymmetricDifference.push_back(Var);
1635   }
1636 
1637   // Add SymmetricDifference elements to Join and return the result.
1638   for (const auto &Var : SymmetricDifference)
1639     Join.insert({Var, LocKind::None});
1640 
1641   assert(Join.size() == (IntersectSize + SymmetricDifference.size()));
1642   assert(Join.size() >= A.size() && Join.size() >= B.size());
1643   return Join;
1644 }
1645 
1646 AssignmentTrackingLowering::Assignment
1647 AssignmentTrackingLowering::joinAssignment(const Assignment &A,
1648                                            const Assignment &B) {
1649   // Partial order:
1650   // NoneOrPhi(null, null) > Known(v, ?s)
1651 
1652   // If either are NoneOrPhi the join is NoneOrPhi.
1653   // If either value is different then the result is
1654   // NoneOrPhi (joining two values is a Phi).
1655   if (!A.isSameSourceAssignment(B))
1656     return Assignment::makeNoneOrPhi();
1657   if (A.Status == Assignment::NoneOrPhi)
1658     return Assignment::makeNoneOrPhi();
1659 
1660   // Source is used to lookup the value + expression in the debug program if
1661   // the stack slot gets assigned a value earlier than expected. Because
1662   // we're only tracking the one dbg.assign, we can't capture debug PHIs.
1663   // It's unlikely that we're losing out on much coverage by avoiding that
1664   // extra work.
1665   // The Source may differ in this situation:
1666   // Pred.1:
1667   //   dbg.assign i32 0, ..., !1, ...
1668   // Pred.2:
1669   //   dbg.assign i32 1, ..., !1, ...
1670   // Here the same assignment (!1) was performed in both preds in the source,
1671   // but we can't use either one unless they are identical (e.g. .we don't
1672   // want to arbitrarily pick between constant values).
1673   auto JoinSource = [&]() -> DbgAssignIntrinsic * {
1674     if (A.Source == B.Source)
1675       return A.Source;
1676     if (A.Source == nullptr || B.Source == nullptr)
1677       return nullptr;
1678     if (A.Source->isIdenticalTo(B.Source))
1679       return A.Source;
1680     return nullptr;
1681   };
1682   DbgAssignIntrinsic *Source = JoinSource();
1683   assert(A.Status == B.Status && A.Status == Assignment::Known);
1684   assert(A.ID == B.ID);
1685   return Assignment::make(A.ID, Source);
1686 }
1687 
1688 AssignmentTrackingLowering::AssignmentMap
1689 AssignmentTrackingLowering::joinAssignmentMap(const AssignmentMap &A,
1690                                               const AssignmentMap &B) {
1691   // Join A and B.
1692   //
1693   // U = join(a, b) for a in A, b in B where Var(a) == Var(b)
1694   // D = join(x, ⊤) for x where Var(x) is in A xor B
1695   // Join = U ∪ D
1696   //
1697   // This is achieved by performing a join on elements from A and B with
1698   // variables common to both A and B (join elements indexed by var intersect),
1699   // then adding LocKind::None elements for vars in A xor B. The latter part is
1700   // equivalent to performing join on elements with variables in A xor B with
1701   // Status::NoneOrPhi (⊤) since join(x, ⊤) = ⊤.
1702   AssignmentMap Join;
1703   SmallVector<VariableID, 16> SymmetricDifference;
1704   // Insert the join of the elements with common vars into Join. Add the
1705   // remaining elements to into SymmetricDifference.
1706   for (const auto &[Var, AV] : A) {
1707     // If this Var doesn't exist in B then add it to the symmetric difference
1708     // set.
1709     auto R = B.find(Var);
1710     if (R == B.end()) {
1711       SymmetricDifference.push_back(Var);
1712       continue;
1713     }
1714     // There is an entry for Var in both, join it.
1715     Join[Var] = joinAssignment(AV, R->second);
1716   }
1717   unsigned IntersectSize = Join.size();
1718   (void)IntersectSize;
1719 
1720   // Add the elements in B with variables that are not in A into
1721   // SymmetricDifference.
1722   for (const auto &Pair : B) {
1723     VariableID Var = Pair.first;
1724     if (A.count(Var) == 0)
1725       SymmetricDifference.push_back(Var);
1726   }
1727 
1728   // Add SymmetricDifference elements to Join and return the result.
1729   for (auto Var : SymmetricDifference)
1730     Join.insert({Var, Assignment::makeNoneOrPhi()});
1731 
1732   assert(Join.size() == (IntersectSize + SymmetricDifference.size()));
1733   assert(Join.size() >= A.size() && Join.size() >= B.size());
1734   return Join;
1735 }
1736 
1737 AssignmentTrackingLowering::BlockInfo
1738 AssignmentTrackingLowering::joinBlockInfo(const BlockInfo &A,
1739                                           const BlockInfo &B) {
1740   BlockInfo Join;
1741   Join.LiveLoc = joinLocMap(A.LiveLoc, B.LiveLoc);
1742   Join.StackHomeValue = joinAssignmentMap(A.StackHomeValue, B.StackHomeValue);
1743   Join.DebugValue = joinAssignmentMap(A.DebugValue, B.DebugValue);
1744   assert(Join.isValid());
1745   return Join;
1746 }
1747 
1748 bool AssignmentTrackingLowering::join(
1749     const BasicBlock &BB, const SmallPtrSet<BasicBlock *, 16> &Visited) {
1750   BlockInfo BBLiveIn;
1751   bool FirstJoin = true;
1752   // LiveIn locs for BB is the join of the already-processed preds' LiveOut
1753   // locs.
1754   for (auto I = pred_begin(&BB), E = pred_end(&BB); I != E; I++) {
1755     // Ignore backedges if we have not visited the predecessor yet. As the
1756     // predecessor hasn't yet had locations propagated into it, most locations
1757     // will not yet be valid, so treat them as all being uninitialized and
1758     // potentially valid. If a location guessed to be correct here is
1759     // invalidated later, we will remove it when we revisit this block. This
1760     // is essentially the same as initialising all LocKinds and Assignments to
1761     // an implicit ⊥ value which is the identity value for the join operation.
1762     const BasicBlock *Pred = *I;
1763     if (!Visited.count(Pred))
1764       continue;
1765 
1766     auto PredLiveOut = LiveOut.find(Pred);
1767     // Pred must have been processed already. See comment at start of this loop.
1768     assert(PredLiveOut != LiveOut.end());
1769 
1770     // Perform the join of BBLiveIn (current live-in info) and PrevLiveOut.
1771     if (FirstJoin)
1772       BBLiveIn = PredLiveOut->second;
1773     else
1774       BBLiveIn = joinBlockInfo(std::move(BBLiveIn), PredLiveOut->second);
1775     FirstJoin = false;
1776   }
1777 
1778   auto CurrentLiveInEntry = LiveIn.find(&BB);
1779   // Check if there isn't an entry, or there is but the LiveIn set has changed
1780   // (expensive check).
1781   if (CurrentLiveInEntry == LiveIn.end() ||
1782       BBLiveIn != CurrentLiveInEntry->second) {
1783     LiveIn[&BB] = std::move(BBLiveIn);
1784     // A change has occured.
1785     return true;
1786   }
1787   // No change.
1788   return false;
1789 }
1790 
1791 /// Return true if A fully contains B.
1792 static bool fullyContains(DIExpression::FragmentInfo A,
1793                           DIExpression::FragmentInfo B) {
1794   auto ALeft = A.OffsetInBits;
1795   auto BLeft = B.OffsetInBits;
1796   if (BLeft < ALeft)
1797     return false;
1798 
1799   auto ARight = ALeft + A.SizeInBits;
1800   auto BRight = BLeft + B.SizeInBits;
1801   if (BRight > ARight)
1802     return false;
1803   return true;
1804 }
1805 
1806 static std::optional<at::AssignmentInfo>
1807 getUntaggedStoreAssignmentInfo(const Instruction &I, const DataLayout &Layout) {
1808   // Don't bother checking if this is an AllocaInst. We know this
1809   // instruction has no tag which means there are no variables associated
1810   // with it.
1811   if (const auto *SI = dyn_cast<StoreInst>(&I))
1812     return at::getAssignmentInfo(Layout, SI);
1813   if (const auto *MI = dyn_cast<MemIntrinsic>(&I))
1814     return at::getAssignmentInfo(Layout, MI);
1815   // Alloca or non-store-like inst.
1816   return std::nullopt;
1817 }
1818 
1819 /// Build a map of {Variable x: Variables y} where all variable fragments
1820 /// contained within the variable fragment x are in set y. This means that
1821 /// y does not contain all overlaps because partial overlaps are excluded.
1822 ///
1823 /// While we're iterating over the function, add single location defs for
1824 /// dbg.declares to \p FnVarLocs
1825 ///
1826 /// Finally, populate UntaggedStoreVars with a mapping of untagged stores to
1827 /// the stored-to variable fragments.
1828 ///
1829 /// These tasks are bundled together to reduce the number of times we need
1830 /// to iterate over the function as they can be achieved together in one pass.
1831 static AssignmentTrackingLowering::OverlapMap buildOverlapMapAndRecordDeclares(
1832     Function &Fn, FunctionVarLocsBuilder *FnVarLocs,
1833     AssignmentTrackingLowering::UntaggedStoreAssignmentMap &UntaggedStoreVars) {
1834   DenseSet<DebugVariable> Seen;
1835   // Map of Variable: [Fragments].
1836   DenseMap<DebugAggregate, SmallVector<DebugVariable, 8>> FragmentMap;
1837   // Iterate over all instructions:
1838   // - dbg.declare    -> add single location variable record
1839   // - dbg.*          -> Add fragments to FragmentMap
1840   // - untagged store -> Add fragments to FragmentMap and update
1841   //                     UntaggedStoreVars.
1842   // We need to add fragments for untagged stores too so that we can correctly
1843   // clobber overlapped fragment locations later.
1844   for (auto &BB : Fn) {
1845     for (auto &I : BB) {
1846       if (auto *DDI = dyn_cast<DbgDeclareInst>(&I)) {
1847         FnVarLocs->addSingleLocVar(DebugVariable(DDI), DDI->getExpression(),
1848                                    DDI->getDebugLoc(), DDI->getAddress());
1849       } else if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
1850         DebugVariable DV = DebugVariable(DII);
1851         DebugAggregate DA = {DV.getVariable(), DV.getInlinedAt()};
1852         if (Seen.insert(DV).second)
1853           FragmentMap[DA].push_back(DV);
1854       } else if (auto Info = getUntaggedStoreAssignmentInfo(
1855                      I, Fn.getParent()->getDataLayout())) {
1856         // Find markers linked to this alloca.
1857         for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(Info->Base)) {
1858           // Discard the fragment if it covers the entire variable.
1859           std::optional<DIExpression::FragmentInfo> FragInfo =
1860               [&Info, DAI]() -> std::optional<DIExpression::FragmentInfo> {
1861             DIExpression::FragmentInfo F;
1862             F.OffsetInBits = Info->OffsetInBits;
1863             F.SizeInBits = Info->SizeInBits;
1864             if (auto ExistingFrag = DAI->getExpression()->getFragmentInfo())
1865               F.OffsetInBits += ExistingFrag->OffsetInBits;
1866             if (auto Sz = DAI->getVariable()->getSizeInBits()) {
1867               if (F.OffsetInBits == 0 && F.SizeInBits == *Sz)
1868                 return std::nullopt;
1869             }
1870             return F;
1871           }();
1872 
1873           DebugVariable DV = DebugVariable(DAI->getVariable(), FragInfo,
1874                                            DAI->getDebugLoc().getInlinedAt());
1875           DebugAggregate DA = {DV.getVariable(), DV.getInlinedAt()};
1876 
1877           // Cache this info for later.
1878           UntaggedStoreVars[&I].push_back(
1879               {FnVarLocs->insertVariable(DV), *Info});
1880 
1881           if (Seen.insert(DV).second)
1882             FragmentMap[DA].push_back(DV);
1883         }
1884       }
1885     }
1886   }
1887 
1888   // Sort the fragment map for each DebugAggregate in non-descending
1889   // order of fragment size. Assert no entries are duplicates.
1890   for (auto &Pair : FragmentMap) {
1891     SmallVector<DebugVariable, 8> &Frags = Pair.second;
1892     std::sort(
1893         Frags.begin(), Frags.end(), [](DebugVariable Next, DebugVariable Elmt) {
1894           assert(!(Elmt.getFragmentOrDefault() == Next.getFragmentOrDefault()));
1895           return Elmt.getFragmentOrDefault().SizeInBits >
1896                  Next.getFragmentOrDefault().SizeInBits;
1897         });
1898   }
1899 
1900   // Build the map.
1901   AssignmentTrackingLowering::OverlapMap Map;
1902   for (auto Pair : FragmentMap) {
1903     auto &Frags = Pair.second;
1904     for (auto It = Frags.begin(), IEnd = Frags.end(); It != IEnd; ++It) {
1905       DIExpression::FragmentInfo Frag = It->getFragmentOrDefault();
1906       // Find the frags that this is contained within.
1907       //
1908       // Because Frags is sorted by size and none have the same offset and
1909       // size, we know that this frag can only be contained by subsequent
1910       // elements.
1911       SmallVector<DebugVariable, 8>::iterator OtherIt = It;
1912       ++OtherIt;
1913       VariableID ThisVar = FnVarLocs->insertVariable(*It);
1914       for (; OtherIt != IEnd; ++OtherIt) {
1915         DIExpression::FragmentInfo OtherFrag = OtherIt->getFragmentOrDefault();
1916         VariableID OtherVar = FnVarLocs->insertVariable(*OtherIt);
1917         if (fullyContains(OtherFrag, Frag))
1918           Map[OtherVar].push_back(ThisVar);
1919       }
1920     }
1921   }
1922 
1923   return Map;
1924 }
1925 
1926 bool AssignmentTrackingLowering::run(FunctionVarLocsBuilder *FnVarLocsBuilder) {
1927   if (Fn.size() > MaxNumBlocks) {
1928     LLVM_DEBUG(dbgs() << "[AT] Dropping var locs in: " << Fn.getName()
1929                       << ": too many blocks (" << Fn.size() << ")\n");
1930     at::deleteAll(&Fn);
1931     return false;
1932   }
1933 
1934   FnVarLocs = FnVarLocsBuilder;
1935 
1936   // The general structure here is inspired by VarLocBasedImpl.cpp
1937   // (LiveDebugValues).
1938 
1939   // Build the variable fragment overlap map.
1940   // Note that this pass doesn't handle partial overlaps correctly (FWIW
1941   // neither does LiveDebugVariables) because that is difficult to do and
1942   // appears to be rare occurance.
1943   VarContains =
1944       buildOverlapMapAndRecordDeclares(Fn, FnVarLocs, UntaggedStoreVars);
1945 
1946   // Prepare for traversal.
1947   ReversePostOrderTraversal<Function *> RPOT(&Fn);
1948   std::priority_queue<unsigned int, std::vector<unsigned int>,
1949                       std::greater<unsigned int>>
1950       Worklist;
1951   std::priority_queue<unsigned int, std::vector<unsigned int>,
1952                       std::greater<unsigned int>>
1953       Pending;
1954   DenseMap<unsigned int, BasicBlock *> OrderToBB;
1955   DenseMap<BasicBlock *, unsigned int> BBToOrder;
1956   { // Init OrderToBB and BBToOrder.
1957     unsigned int RPONumber = 0;
1958     for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
1959       OrderToBB[RPONumber] = *RI;
1960       BBToOrder[*RI] = RPONumber;
1961       Worklist.push(RPONumber);
1962       ++RPONumber;
1963     }
1964     LiveIn.init(RPONumber);
1965     LiveOut.init(RPONumber);
1966   }
1967 
1968   // Perform the traversal.
1969   //
1970   // This is a standard "union of predecessor outs" dataflow problem. To solve
1971   // it, we perform join() and process() using the two worklist method until
1972   // the LiveIn data for each block becomes unchanging. The "proof" that this
1973   // terminates can be put together by looking at the comments around LocKind,
1974   // Assignment, and the various join methods, which show that all the elements
1975   // involved are made up of join-semilattices; LiveIn(n) can only
1976   // monotonically increase in value throughout the dataflow.
1977   //
1978   SmallPtrSet<BasicBlock *, 16> Visited;
1979   while (!Worklist.empty()) {
1980     // We track what is on the pending worklist to avoid inserting the same
1981     // thing twice.
1982     SmallPtrSet<BasicBlock *, 16> OnPending;
1983     LLVM_DEBUG(dbgs() << "Processing Worklist\n");
1984     while (!Worklist.empty()) {
1985       BasicBlock *BB = OrderToBB[Worklist.top()];
1986       LLVM_DEBUG(dbgs() << "\nPop BB " << BB->getName() << "\n");
1987       Worklist.pop();
1988       bool InChanged = join(*BB, Visited);
1989       // Always consider LiveIn changed on the first visit.
1990       InChanged |= Visited.insert(BB).second;
1991       if (InChanged) {
1992         LLVM_DEBUG(dbgs() << BB->getName() << " has new InLocs, process it\n");
1993         // Mutate a copy of LiveIn while processing BB. After calling process
1994         // LiveSet is the LiveOut set for BB.
1995         BlockInfo LiveSet = LiveIn[BB];
1996 
1997         // Process the instructions in the block.
1998         process(*BB, &LiveSet);
1999 
2000         // Relatively expensive check: has anything changed in LiveOut for BB?
2001         if (LiveOut[BB] != LiveSet) {
2002           LLVM_DEBUG(dbgs() << BB->getName()
2003                             << " has new OutLocs, add succs to worklist: [ ");
2004           LiveOut[BB] = std::move(LiveSet);
2005           for (auto I = succ_begin(BB), E = succ_end(BB); I != E; I++) {
2006             if (OnPending.insert(*I).second) {
2007               LLVM_DEBUG(dbgs() << I->getName() << " ");
2008               Pending.push(BBToOrder[*I]);
2009             }
2010           }
2011           LLVM_DEBUG(dbgs() << "]\n");
2012         }
2013       }
2014     }
2015     Worklist.swap(Pending);
2016     // At this point, pending must be empty, since it was just the empty
2017     // worklist
2018     assert(Pending.empty() && "Pending should be empty");
2019   }
2020 
2021   // That's the hard part over. Now we just have some admin to do.
2022 
2023   // Record whether we inserted any intrinsics.
2024   bool InsertedAnyIntrinsics = false;
2025 
2026   // Identify and add defs for single location variables.
2027   //
2028   // Go through all of the defs that we plan to add. If the aggregate variable
2029   // it's a part of is not in the NotAlwaysStackHomed set we can emit a single
2030   // location def and omit the rest. Add an entry to AlwaysStackHomed so that
2031   // we can identify those uneeded defs later.
2032   DenseSet<DebugAggregate> AlwaysStackHomed;
2033   for (const auto &Pair : InsertBeforeMap) {
2034     const auto &Vec = Pair.second;
2035     for (VarLocInfo VarLoc : Vec) {
2036       DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2037       DebugAggregate Aggr{Var.getVariable(), Var.getInlinedAt()};
2038 
2039       // Skip this Var if it's not always stack homed.
2040       if (NotAlwaysStackHomed.contains(Aggr))
2041         continue;
2042 
2043       // Skip complex cases such as when different fragments of a variable have
2044       // been split into different allocas. Skipping in this case means falling
2045       // back to using a list of defs (which could reduce coverage, but is no
2046       // less correct).
2047       bool Simple =
2048           VarLoc.Expr->getNumElements() == 1 && VarLoc.Expr->startsWithDeref();
2049       if (!Simple) {
2050         NotAlwaysStackHomed.insert(Aggr);
2051         continue;
2052       }
2053 
2054       // All source assignments to this variable remain and all stores to any
2055       // part of the variable store to the same address (with varying
2056       // offsets). We can just emit a single location for the whole variable.
2057       //
2058       // Unless we've already done so, create the single location def now.
2059       if (AlwaysStackHomed.insert(Aggr).second) {
2060         assert(isa<AllocaInst>(VarLoc.V));
2061         // TODO: When more complex cases are handled VarLoc.Expr should be
2062         // built appropriately rather than always using an empty DIExpression.
2063         // The assert below is a reminder.
2064         assert(Simple);
2065         VarLoc.Expr = DIExpression::get(Fn.getContext(), std::nullopt);
2066         DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2067         FnVarLocs->addSingleLocVar(Var, VarLoc.Expr, VarLoc.DL, VarLoc.V);
2068         InsertedAnyIntrinsics = true;
2069       }
2070     }
2071   }
2072 
2073   // Insert the other DEFs.
2074   for (const auto &[InsertBefore, Vec] : InsertBeforeMap) {
2075     SmallVector<VarLocInfo> NewDefs;
2076     for (const VarLocInfo &VarLoc : Vec) {
2077       DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2078       DebugAggregate Aggr{Var.getVariable(), Var.getInlinedAt()};
2079       // If this variable is always stack homed then we have already inserted a
2080       // dbg.declare and deleted this dbg.value.
2081       if (AlwaysStackHomed.contains(Aggr))
2082         continue;
2083       NewDefs.push_back(VarLoc);
2084       InsertedAnyIntrinsics = true;
2085     }
2086 
2087     FnVarLocs->setWedge(InsertBefore, std::move(NewDefs));
2088   }
2089 
2090   InsertedAnyIntrinsics |= emitPromotedVarLocs(FnVarLocs);
2091 
2092   return InsertedAnyIntrinsics;
2093 }
2094 
2095 bool AssignmentTrackingLowering::emitPromotedVarLocs(
2096     FunctionVarLocsBuilder *FnVarLocs) {
2097   bool InsertedAnyIntrinsics = false;
2098   // Go through every block, translating debug intrinsics for fully promoted
2099   // variables into FnVarLocs location defs. No analysis required for these.
2100   for (auto &BB : Fn) {
2101     for (auto &I : BB) {
2102       // Skip instructions other than dbg.values and dbg.assigns.
2103       auto *DVI = dyn_cast<DbgValueInst>(&I);
2104       if (!DVI)
2105         continue;
2106       // Skip variables that haven't been promoted - we've dealt with those
2107       // already.
2108       if (VarsWithStackSlot->contains(getAggregate(DVI)))
2109         continue;
2110       // Wrapper to get a single value (or undef) from DVI.
2111       auto GetValue = [DVI]() -> Value * {
2112         // Conditions for undef: Any operand undef, zero operands or single
2113         // operand is nullptr. We also can't handle variadic DIExpressions yet.
2114         // Some of those conditions don't have a type we can pick for
2115         // undef. Use i32.
2116         if (DVI->isKillLocation() || DVI->getValue() == nullptr ||
2117             DVI->hasArgList())
2118           return UndefValue::get(Type::getInt32Ty(DVI->getContext()));
2119         return DVI->getValue();
2120       };
2121       Instruction *InsertBefore = I.getNextNode();
2122       assert(InsertBefore && "Unexpected: debug intrinsics after a terminator");
2123       FnVarLocs->addVarLoc(InsertBefore, DebugVariable(DVI),
2124                            DVI->getExpression(), DVI->getDebugLoc(),
2125                            GetValue());
2126       InsertedAnyIntrinsics = true;
2127     }
2128   }
2129   return InsertedAnyIntrinsics;
2130 }
2131 
2132 /// Remove redundant definitions within sequences of consecutive location defs.
2133 /// This is done using a backward scan to keep the last def describing a
2134 /// specific variable/fragment.
2135 ///
2136 /// This implements removeRedundantDbgInstrsUsingBackwardScan from
2137 /// lib/Transforms/Utils/BasicBlockUtils.cpp for locations described with
2138 /// FunctionVarLocsBuilder instead of with intrinsics.
2139 static bool
2140 removeRedundantDbgLocsUsingBackwardScan(const BasicBlock *BB,
2141                                         FunctionVarLocsBuilder &FnVarLocs) {
2142   bool Changed = false;
2143   SmallDenseSet<DebugVariable> VariableSet;
2144 
2145   // Scan over the entire block, not just over the instructions mapped by
2146   // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
2147   // instructions.
2148   for (const Instruction &I : reverse(*BB)) {
2149     if (!isa<DbgVariableIntrinsic>(I)) {
2150       // Sequence of consecutive defs ended. Clear map for the next one.
2151       VariableSet.clear();
2152     }
2153 
2154     // Get the location defs that start just before this instruction.
2155     const auto *Locs = FnVarLocs.getWedge(&I);
2156     if (!Locs)
2157       continue;
2158 
2159     NumWedgesScanned++;
2160     bool ChangedThisWedge = false;
2161     // The new pruned set of defs, reversed because we're scanning backwards.
2162     SmallVector<VarLocInfo> NewDefsReversed;
2163 
2164     // Iterate over the existing defs in reverse.
2165     for (auto RIt = Locs->rbegin(), REnd = Locs->rend(); RIt != REnd; ++RIt) {
2166       NumDefsScanned++;
2167       const DebugVariable &Key = FnVarLocs.getVariable(RIt->VariableID);
2168       bool FirstDefOfFragment = VariableSet.insert(Key).second;
2169 
2170       // If the same variable fragment is described more than once it is enough
2171       // to keep the last one (i.e. the first found in this reverse iteration).
2172       if (FirstDefOfFragment) {
2173         // New def found: keep it.
2174         NewDefsReversed.push_back(*RIt);
2175       } else {
2176         // Redundant def found: throw it away. Since the wedge of defs is being
2177         // rebuilt, doing nothing is the same as deleting an entry.
2178         ChangedThisWedge = true;
2179         NumDefsRemoved++;
2180       }
2181       continue;
2182     }
2183 
2184     // Un-reverse the defs and replace the wedge with the pruned version.
2185     if (ChangedThisWedge) {
2186       std::reverse(NewDefsReversed.begin(), NewDefsReversed.end());
2187       FnVarLocs.setWedge(&I, std::move(NewDefsReversed));
2188       NumWedgesChanged++;
2189       Changed = true;
2190     }
2191   }
2192 
2193   return Changed;
2194 }
2195 
2196 /// Remove redundant location defs using a forward scan. This can remove a
2197 /// location definition that is redundant due to indicating that a variable has
2198 /// the same value as is already being indicated by an earlier def.
2199 ///
2200 /// This implements removeRedundantDbgInstrsUsingForwardScan from
2201 /// lib/Transforms/Utils/BasicBlockUtils.cpp for locations described with
2202 /// FunctionVarLocsBuilder instead of with intrinsics
2203 static bool
2204 removeRedundantDbgLocsUsingForwardScan(const BasicBlock *BB,
2205                                        FunctionVarLocsBuilder &FnVarLocs) {
2206   bool Changed = false;
2207   DenseMap<DebugVariable, std::pair<Value *, DIExpression *>> VariableMap;
2208 
2209   // Scan over the entire block, not just over the instructions mapped by
2210   // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
2211   // instructions.
2212   for (const Instruction &I : *BB) {
2213     // Get the defs that come just before this instruction.
2214     const auto *Locs = FnVarLocs.getWedge(&I);
2215     if (!Locs)
2216       continue;
2217 
2218     NumWedgesScanned++;
2219     bool ChangedThisWedge = false;
2220     // The new pruned set of defs.
2221     SmallVector<VarLocInfo> NewDefs;
2222 
2223     // Iterate over the existing defs.
2224     for (const VarLocInfo &Loc : *Locs) {
2225       NumDefsScanned++;
2226       DebugVariable Key(FnVarLocs.getVariable(Loc.VariableID).getVariable(),
2227                         std::nullopt, Loc.DL.getInlinedAt());
2228       auto VMI = VariableMap.find(Key);
2229 
2230       // Update the map if we found a new value/expression describing the
2231       // variable, or if the variable wasn't mapped already.
2232       if (VMI == VariableMap.end() || VMI->second.first != Loc.V ||
2233           VMI->second.second != Loc.Expr) {
2234         VariableMap[Key] = {Loc.V, Loc.Expr};
2235         NewDefs.push_back(Loc);
2236         continue;
2237       }
2238 
2239       // Did not insert this Loc, which is the same as removing it.
2240       ChangedThisWedge = true;
2241       NumDefsRemoved++;
2242     }
2243 
2244     // Replace the existing wedge with the pruned version.
2245     if (ChangedThisWedge) {
2246       FnVarLocs.setWedge(&I, std::move(NewDefs));
2247       NumWedgesChanged++;
2248       Changed = true;
2249     }
2250   }
2251 
2252   return Changed;
2253 }
2254 
2255 static bool
2256 removeUndefDbgLocsFromEntryBlock(const BasicBlock *BB,
2257                                  FunctionVarLocsBuilder &FnVarLocs) {
2258   assert(BB->isEntryBlock());
2259   // Do extra work to ensure that we remove semantically unimportant undefs.
2260   //
2261   // This is to work around the fact that SelectionDAG will hoist dbg.values
2262   // using argument values to the top of the entry block. That can move arg
2263   // dbg.values before undef and constant dbg.values which they previously
2264   // followed. The easiest thing to do is to just try to feed SelectionDAG
2265   // input it's happy with.
2266   //
2267   // Map of {Variable x: Fragments y} where the fragments y of variable x have
2268   // have at least one non-undef location defined already. Don't use directly,
2269   // instead call DefineBits and HasDefinedBits.
2270   SmallDenseMap<DebugAggregate, SmallDenseSet<DIExpression::FragmentInfo>>
2271       VarsWithDef;
2272   // Specify that V (a fragment of A) has a non-undef location.
2273   auto DefineBits = [&VarsWithDef](DebugAggregate A, DebugVariable V) {
2274     VarsWithDef[A].insert(V.getFragmentOrDefault());
2275   };
2276   // Return true if a non-undef location has been defined for V (a fragment of
2277   // A). Doesn't imply that the location is currently non-undef, just that a
2278   // non-undef location has been seen previously.
2279   auto HasDefinedBits = [&VarsWithDef](DebugAggregate A, DebugVariable V) {
2280     auto FragsIt = VarsWithDef.find(A);
2281     if (FragsIt == VarsWithDef.end())
2282       return false;
2283     return llvm::any_of(FragsIt->second, [V](auto Frag) {
2284       return DIExpression::fragmentsOverlap(Frag, V.getFragmentOrDefault());
2285     });
2286   };
2287 
2288   bool Changed = false;
2289   DenseMap<DebugVariable, std::pair<Value *, DIExpression *>> VariableMap;
2290 
2291   // Scan over the entire block, not just over the instructions mapped by
2292   // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
2293   // instructions.
2294   for (const Instruction &I : *BB) {
2295     // Get the defs that come just before this instruction.
2296     const auto *Locs = FnVarLocs.getWedge(&I);
2297     if (!Locs)
2298       continue;
2299 
2300     NumWedgesScanned++;
2301     bool ChangedThisWedge = false;
2302     // The new pruned set of defs.
2303     SmallVector<VarLocInfo> NewDefs;
2304 
2305     // Iterate over the existing defs.
2306     for (const VarLocInfo &Loc : *Locs) {
2307       NumDefsScanned++;
2308       DebugAggregate Aggr{FnVarLocs.getVariable(Loc.VariableID).getVariable(),
2309                           Loc.DL.getInlinedAt()};
2310       DebugVariable Var = FnVarLocs.getVariable(Loc.VariableID);
2311 
2312       // Remove undef entries that are encountered before any non-undef
2313       // intrinsics from the entry block.
2314       if (isa<UndefValue>(Loc.V) && !HasDefinedBits(Aggr, Var)) {
2315         // Did not insert this Loc, which is the same as removing it.
2316         NumDefsRemoved++;
2317         ChangedThisWedge = true;
2318         continue;
2319       }
2320 
2321       DefineBits(Aggr, Var);
2322       NewDefs.push_back(Loc);
2323     }
2324 
2325     // Replace the existing wedge with the pruned version.
2326     if (ChangedThisWedge) {
2327       FnVarLocs.setWedge(&I, std::move(NewDefs));
2328       NumWedgesChanged++;
2329       Changed = true;
2330     }
2331   }
2332 
2333   return Changed;
2334 }
2335 
2336 static bool removeRedundantDbgLocs(const BasicBlock *BB,
2337                                    FunctionVarLocsBuilder &FnVarLocs) {
2338   bool MadeChanges = false;
2339   MadeChanges |= removeRedundantDbgLocsUsingBackwardScan(BB, FnVarLocs);
2340   if (BB->isEntryBlock())
2341     MadeChanges |= removeUndefDbgLocsFromEntryBlock(BB, FnVarLocs);
2342   MadeChanges |= removeRedundantDbgLocsUsingForwardScan(BB, FnVarLocs);
2343 
2344   if (MadeChanges)
2345     LLVM_DEBUG(dbgs() << "Removed redundant dbg locs from: " << BB->getName()
2346                       << "\n");
2347   return MadeChanges;
2348 }
2349 
2350 static DenseSet<DebugAggregate> findVarsWithStackSlot(Function &Fn) {
2351   DenseSet<DebugAggregate> Result;
2352   for (auto &BB : Fn) {
2353     for (auto &I : BB) {
2354       // Any variable linked to an instruction is considered
2355       // interesting. Ideally we only need to check Allocas, however, a
2356       // DIAssignID might get dropped from an alloca but not stores. In that
2357       // case, we need to consider the variable interesting for NFC behaviour
2358       // with this change. TODO: Consider only looking at allocas.
2359       for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(&I)) {
2360         Result.insert({DAI->getVariable(), DAI->getDebugLoc().getInlinedAt()});
2361       }
2362     }
2363   }
2364   return Result;
2365 }
2366 
2367 static void analyzeFunction(Function &Fn, const DataLayout &Layout,
2368                             FunctionVarLocsBuilder *FnVarLocs) {
2369   // The analysis will generate location definitions for all variables, but we
2370   // only need to perform a dataflow on the set of variables which have a stack
2371   // slot. Find those now.
2372   DenseSet<DebugAggregate> VarsWithStackSlot = findVarsWithStackSlot(Fn);
2373 
2374   bool Changed = false;
2375 
2376   // Use a scope block to clean up AssignmentTrackingLowering before running
2377   // MemLocFragmentFill to reduce peak memory consumption.
2378   {
2379     AssignmentTrackingLowering Pass(Fn, Layout, &VarsWithStackSlot);
2380     Changed = Pass.run(FnVarLocs);
2381   }
2382 
2383   if (Changed) {
2384     MemLocFragmentFill Pass(Fn, &VarsWithStackSlot);
2385     Pass.run(FnVarLocs);
2386 
2387     // Remove redundant entries. As well as reducing memory consumption and
2388     // avoiding waiting cycles later by burning some now, this has another
2389     // important job. That is to work around some SelectionDAG quirks. See
2390     // removeRedundantDbgLocsUsingForwardScan comments for more info on that.
2391     for (auto &BB : Fn)
2392       removeRedundantDbgLocs(&BB, *FnVarLocs);
2393   }
2394 }
2395 
2396 bool AssignmentTrackingAnalysis::runOnFunction(Function &F) {
2397   LLVM_DEBUG(dbgs() << "AssignmentTrackingAnalysis run on " << F.getName()
2398                     << "\n");
2399   auto DL = std::make_unique<DataLayout>(F.getParent());
2400 
2401   // Clear previous results.
2402   Results->clear();
2403 
2404   FunctionVarLocsBuilder Builder;
2405   analyzeFunction(F, *DL.get(), &Builder);
2406 
2407   // Save these results.
2408   Results->init(Builder);
2409 
2410   if (PrintResults && isFunctionInPrintList(F.getName()))
2411     Results->print(errs(), F);
2412 
2413   // Return false because this pass does not modify the function.
2414   return false;
2415 }
2416 
2417 AssignmentTrackingAnalysis::AssignmentTrackingAnalysis()
2418     : FunctionPass(ID), Results(std::make_unique<FunctionVarLocs>()) {}
2419 
2420 char AssignmentTrackingAnalysis::ID = 0;
2421 
2422 INITIALIZE_PASS(AssignmentTrackingAnalysis, DEBUG_TYPE,
2423                 "Assignment Tracking Analysis", false, true)
2424