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