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