xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp (revision 1b1e392aed4957a38c49599512b4f65b844a0772)
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 DPValue *>;
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 DPValue *>(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 : 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 (auto &DPV : I.getDbgValueRange()) {
833          if (const auto *Locs = FnVarLocs->getWedge(&DPV)) {
834            for (const VarLocInfo &Loc : *Locs) {
835              addDef(Loc, &DPV, *I.getParent(), LiveSet);
836            }
837          }
838        }
839        if (const auto *Locs = FnVarLocs->getWedge(&I)) {
840          for (const VarLocInfo &Loc : *Locs) {
841            addDef(Loc, &I, *I.getParent(), LiveSet);
842          }
843        }
844      }
845    }
846  
847  public:
848    MemLocFragmentFill(Function &Fn,
849                       const DenseSet<DebugAggregate> *VarsWithStackSlot,
850                       bool CoalesceAdjacentFragments)
851        : Fn(Fn), VarsWithStackSlot(VarsWithStackSlot),
852          CoalesceAdjacentFragments(CoalesceAdjacentFragments) {}
853  
854    /// Add variable locations to \p FnVarLocs so that any bits of a variable
855    /// with a memory location have that location explicitly reinstated at each
856    /// subsequent variable location definition that that doesn't overwrite those
857    /// bits. i.e. after a variable location def, insert new defs for the memory
858    /// location with fragments for the difference of "all bits currently in
859    /// memory" and "the fragment of the second def". e.g.
860    ///
861    ///     Before:
862    ///
863    ///     var x bits 0 to 63:  value in memory
864    ///     more instructions
865    ///     var x bits 0 to 31:  value is %0
866    ///
867    ///     After:
868    ///
869    ///     var x bits 0 to 63:  value in memory
870    ///     more instructions
871    ///     var x bits 0 to 31:  value is %0
872    ///     var x bits 32 to 61: value in memory ; <-- new loc def
873    ///
874    void run(FunctionVarLocsBuilder *FnVarLocs) {
875      if (!EnableMemLocFragFill)
876        return;
877  
878      this->FnVarLocs = FnVarLocs;
879  
880      // Prepare for traversal.
881      //
882      ReversePostOrderTraversal<Function *> RPOT(&Fn);
883      std::priority_queue<unsigned int, std::vector<unsigned int>,
884                          std::greater<unsigned int>>
885          Worklist;
886      std::priority_queue<unsigned int, std::vector<unsigned int>,
887                          std::greater<unsigned int>>
888          Pending;
889      DenseMap<unsigned int, BasicBlock *> OrderToBB;
890      DenseMap<BasicBlock *, unsigned int> BBToOrder;
891      { // Init OrderToBB and BBToOrder.
892        unsigned int RPONumber = 0;
893        for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
894          OrderToBB[RPONumber] = *RI;
895          BBToOrder[*RI] = RPONumber;
896          Worklist.push(RPONumber);
897          ++RPONumber;
898        }
899        LiveIn.init(RPONumber);
900        LiveOut.init(RPONumber);
901      }
902  
903      // Perform the traversal.
904      //
905      // This is a standard "intersect of predecessor outs" dataflow problem. To
906      // solve it, we perform meet() and process() using the two worklist method
907      // until the LiveIn data for each block becomes unchanging.
908      //
909      // This dataflow is essentially working on maps of sets and at each meet we
910      // intersect the maps and the mapped sets. So, initialized live-in maps
911      // monotonically decrease in value throughout the dataflow.
912      SmallPtrSet<BasicBlock *, 16> Visited;
913      while (!Worklist.empty() || !Pending.empty()) {
914        // We track what is on the pending worklist to avoid inserting the same
915        // thing twice.  We could avoid this with a custom priority queue, but
916        // this is probably not worth it.
917        SmallPtrSet<BasicBlock *, 16> OnPending;
918        LLVM_DEBUG(dbgs() << "Processing Worklist\n");
919        while (!Worklist.empty()) {
920          BasicBlock *BB = OrderToBB[Worklist.top()];
921          LLVM_DEBUG(dbgs() << "\nPop BB " << BB->getName() << "\n");
922          Worklist.pop();
923          bool InChanged = meet(*BB, Visited);
924          // Always consider LiveIn changed on the first visit.
925          InChanged |= Visited.insert(BB).second;
926          if (InChanged) {
927            LLVM_DEBUG(dbgs()
928                       << BB->getName() << " has new InLocs, process it\n");
929            //  Mutate a copy of LiveIn while processing BB. Once we've processed
930            //  the terminator LiveSet is the LiveOut set for BB.
931            //  This is an expensive copy!
932            VarFragMap LiveSet = LiveIn[BB];
933  
934            // Process the instructions in the block.
935            process(*BB, LiveSet);
936  
937            // Relatively expensive check: has anything changed in LiveOut for BB?
938            if (!varFragMapsAreEqual(LiveOut[BB], LiveSet)) {
939              LLVM_DEBUG(dbgs() << BB->getName()
940                                << " has new OutLocs, add succs to worklist: [ ");
941              LiveOut[BB] = std::move(LiveSet);
942              for (auto I = succ_begin(BB), E = succ_end(BB); I != E; I++) {
943                if (OnPending.insert(*I).second) {
944                  LLVM_DEBUG(dbgs() << I->getName() << " ");
945                  Pending.push(BBToOrder[*I]);
946                }
947              }
948              LLVM_DEBUG(dbgs() << "]\n");
949            }
950          }
951        }
952        Worklist.swap(Pending);
953        // At this point, pending must be empty, since it was just the empty
954        // worklist
955        assert(Pending.empty() && "Pending should be empty");
956      }
957  
958      // Insert new location defs.
959      for (auto &Pair : BBInsertBeforeMap) {
960        InsertMap &Map = Pair.second;
961        for (auto &Pair : Map) {
962          auto InsertBefore = Pair.first;
963          assert(InsertBefore && "should never be null");
964          auto FragMemLocs = Pair.second;
965          auto &Ctx = Fn.getContext();
966  
967          for (auto &FragMemLoc : FragMemLocs) {
968            DIExpression *Expr = DIExpression::get(Ctx, std::nullopt);
969            if (FragMemLoc.SizeInBits !=
970                *Aggregates[FragMemLoc.Var].first->getSizeInBits())
971              Expr = *DIExpression::createFragmentExpression(
972                  Expr, FragMemLoc.OffsetInBits, FragMemLoc.SizeInBits);
973            Expr = DIExpression::prepend(Expr, DIExpression::DerefAfter,
974                                         FragMemLoc.OffsetInBits / 8);
975            DebugVariable Var(Aggregates[FragMemLoc.Var].first, Expr,
976                              FragMemLoc.DL.getInlinedAt());
977            FnVarLocs->addVarLoc(InsertBefore, Var, Expr, FragMemLoc.DL,
978                                 Bases[FragMemLoc.Base]);
979          }
980        }
981      }
982    }
983  };
984  
985  /// AssignmentTrackingLowering encapsulates a dataflow analysis over a function
986  /// that interprets assignment tracking debug info metadata and stores in IR to
987  /// create a map of variable locations.
988  class AssignmentTrackingLowering {
989  public:
990    /// The kind of location in use for a variable, where Mem is the stack home,
991    /// Val is an SSA value or const, and None means that there is not one single
992    /// kind (either because there are multiple or because there is none; it may
993    /// prove useful to split this into two values in the future).
994    ///
995    /// LocKind is a join-semilattice with the partial order:
996    /// None > Mem, Val
997    ///
998    /// i.e.
999    /// join(Mem, Mem)   = Mem
1000    /// join(Val, Val)   = Val
1001    /// join(Mem, Val)   = None
1002    /// join(None, Mem)  = None
1003    /// join(None, Val)  = None
1004    /// join(None, None) = None
1005    ///
1006    /// Note: the order is not `None > Val > Mem` because we're using DIAssignID
1007    /// to name assignments and are not tracking the actual stored values.
1008    /// Therefore currently there's no way to ensure that Mem values and Val
1009    /// values are the same. This could be a future extension, though it's not
1010    /// clear that many additional locations would be recovered that way in
1011    /// practice as the likelihood of this sitation arising naturally seems
1012    /// incredibly low.
1013    enum class LocKind { Mem, Val, None };
1014  
1015    /// An abstraction of the assignment of a value to a variable or memory
1016    /// location.
1017    ///
1018    /// An Assignment is Known or NoneOrPhi. A Known Assignment means we have a
1019    /// DIAssignID ptr that represents it. NoneOrPhi means that we don't (or
1020    /// can't) know the ID of the last assignment that took place.
1021    ///
1022    /// The Status of the Assignment (Known or NoneOrPhi) is another
1023    /// join-semilattice. The partial order is:
1024    /// NoneOrPhi > Known {id_0, id_1, ...id_N}
1025    ///
1026    /// i.e. for all values x and y where x != y:
1027    /// join(x, x) = x
1028    /// join(x, y) = NoneOrPhi
1029    using AssignRecord = PointerUnion<DbgAssignIntrinsic *, DPValue *>;
1030    struct Assignment {
1031      enum S { Known, NoneOrPhi } Status;
1032      /// ID of the assignment. nullptr if Status is not Known.
1033      DIAssignID *ID;
1034      /// The dbg.assign that marks this dbg-def. Mem-defs don't use this field.
1035      /// May be nullptr.
1036      AssignRecord Source;
1037  
1038      bool isSameSourceAssignment(const Assignment &Other) const {
1039        // Don't include Source in the equality check. Assignments are
1040        // defined by their ID, not debug intrinsic(s).
1041        return std::tie(Status, ID) == std::tie(Other.Status, Other.ID);
1042      }
1043      void dump(raw_ostream &OS) {
1044        static const char *LUT[] = {"Known", "NoneOrPhi"};
1045        OS << LUT[Status] << "(id=";
1046        if (ID)
1047          OS << ID;
1048        else
1049          OS << "null";
1050        OS << ", s=";
1051        if (Source.isNull())
1052          OS << "null";
1053        else if (isa<DbgAssignIntrinsic *>(Source))
1054          OS << Source.get<DbgAssignIntrinsic *>();
1055        else
1056          OS << Source.get<DPValue *>();
1057        OS << ")";
1058      }
1059  
1060      static Assignment make(DIAssignID *ID, DbgAssignIntrinsic *Source) {
1061        return Assignment(Known, ID, Source);
1062      }
1063      static Assignment make(DIAssignID *ID, DPValue *Source) {
1064        assert(Source->isDbgAssign() &&
1065               "Cannot make an assignment from a non-assign DPValue");
1066        return Assignment(Known, ID, Source);
1067      }
1068      static Assignment make(DIAssignID *ID, AssignRecord Source) {
1069        return Assignment(Known, ID, Source);
1070      }
1071      static Assignment makeFromMemDef(DIAssignID *ID) {
1072        return Assignment(Known, ID);
1073      }
1074      static Assignment makeNoneOrPhi() { return Assignment(NoneOrPhi, nullptr); }
1075      // Again, need a Top value?
1076      Assignment() : Status(NoneOrPhi), ID(nullptr) {} // Can we delete this?
1077      Assignment(S Status, DIAssignID *ID) : Status(Status), ID(ID) {
1078        // If the Status is Known then we expect there to be an assignment ID.
1079        assert(Status == NoneOrPhi || ID);
1080      }
1081      Assignment(S Status, DIAssignID *ID, DbgAssignIntrinsic *Source)
1082          : Status(Status), ID(ID), Source(Source) {
1083        // If the Status is Known then we expect there to be an assignment ID.
1084        assert(Status == NoneOrPhi || ID);
1085      }
1086      Assignment(S Status, DIAssignID *ID, DPValue *Source)
1087          : Status(Status), ID(ID), Source(Source) {
1088        // If the Status is Known then we expect there to be an assignment ID.
1089        assert(Status == NoneOrPhi || ID);
1090      }
1091      Assignment(S Status, DIAssignID *ID, AssignRecord Source)
1092          : Status(Status), ID(ID), Source(Source) {
1093        // If the Status is Known then we expect there to be an assignment ID.
1094        assert(Status == NoneOrPhi || ID);
1095      }
1096    };
1097  
1098    using AssignmentMap = SmallVector<Assignment>;
1099    using LocMap = SmallVector<LocKind>;
1100    using OverlapMap = DenseMap<VariableID, SmallVector<VariableID>>;
1101    using UntaggedStoreAssignmentMap =
1102        DenseMap<const Instruction *,
1103                 SmallVector<std::pair<VariableID, at::AssignmentInfo>>>;
1104  
1105  private:
1106    /// The highest numbered VariableID for partially promoted variables plus 1,
1107    /// the values for which start at 1.
1108    unsigned TrackedVariablesVectorSize = 0;
1109    /// Map a variable to the set of variables that it fully contains.
1110    OverlapMap VarContains;
1111    /// Map untagged stores to the variable fragments they assign to. Used by
1112    /// processUntaggedInstruction.
1113    UntaggedStoreAssignmentMap UntaggedStoreVars;
1114  
1115    // Machinery to defer inserting dbg.values.
1116    using InstInsertMap = MapVector<VarLocInsertPt, SmallVector<VarLocInfo>>;
1117    InstInsertMap InsertBeforeMap;
1118    /// Clear the location definitions currently cached for insertion after /p
1119    /// After.
1120    void resetInsertionPoint(Instruction &After);
1121    void resetInsertionPoint(DPValue &After);
1122  
1123    // emitDbgValue can be called with:
1124    //   Source=[AssignRecord|DbgValueInst*|DbgAssignIntrinsic*|DPValue*]
1125    // Since AssignRecord can be cast to one of the latter two types, and all
1126    // other types have a shared interface, we use a template to handle the latter
1127    // three types, and an explicit overload for AssignRecord that forwards to
1128    // the template version with the right type.
1129    void emitDbgValue(LocKind Kind, AssignRecord Source, VarLocInsertPt After);
1130    template <typename T>
1131    void emitDbgValue(LocKind Kind, const T Source, VarLocInsertPt After);
1132  
1133    static bool mapsAreEqual(const BitVector &Mask, const AssignmentMap &A,
1134                             const AssignmentMap &B) {
1135      return llvm::all_of(Mask.set_bits(), [&](unsigned VarID) {
1136        return A[VarID].isSameSourceAssignment(B[VarID]);
1137      });
1138    }
1139  
1140    /// Represents the stack and debug assignments in a block. Used to describe
1141    /// the live-in and live-out values for blocks, as well as the "current"
1142    /// value as we process each instruction in a block.
1143    struct BlockInfo {
1144      /// The set of variables (VariableID) being tracked in this block.
1145      BitVector VariableIDsInBlock;
1146      /// Dominating assignment to memory for each variable, indexed by
1147      /// VariableID.
1148      AssignmentMap StackHomeValue;
1149      /// Dominating assignemnt to each variable, indexed by VariableID.
1150      AssignmentMap DebugValue;
1151      /// Location kind for each variable. LiveLoc indicates whether the
1152      /// dominating assignment in StackHomeValue (LocKind::Mem), DebugValue
1153      /// (LocKind::Val), or neither (LocKind::None) is valid, in that order of
1154      /// preference. This cannot be derived by inspecting DebugValue and
1155      /// StackHomeValue due to the fact that there's no distinction in
1156      /// Assignment (the class) between whether an assignment is unknown or a
1157      /// merge of multiple assignments (both are Status::NoneOrPhi). In other
1158      /// words, the memory location may well be valid while both DebugValue and
1159      /// StackHomeValue contain Assignments that have a Status of NoneOrPhi.
1160      /// Indexed by VariableID.
1161      LocMap LiveLoc;
1162  
1163    public:
1164      enum AssignmentKind { Stack, Debug };
1165      const AssignmentMap &getAssignmentMap(AssignmentKind Kind) const {
1166        switch (Kind) {
1167        case Stack:
1168          return StackHomeValue;
1169        case Debug:
1170          return DebugValue;
1171        }
1172        llvm_unreachable("Unknown AssignmentKind");
1173      }
1174      AssignmentMap &getAssignmentMap(AssignmentKind Kind) {
1175        return const_cast<AssignmentMap &>(
1176            const_cast<const BlockInfo *>(this)->getAssignmentMap(Kind));
1177      }
1178  
1179      bool isVariableTracked(VariableID Var) const {
1180        return VariableIDsInBlock[static_cast<unsigned>(Var)];
1181      }
1182  
1183      const Assignment &getAssignment(AssignmentKind Kind, VariableID Var) const {
1184        assert(isVariableTracked(Var) && "Var not tracked in block");
1185        return getAssignmentMap(Kind)[static_cast<unsigned>(Var)];
1186      }
1187  
1188      LocKind getLocKind(VariableID Var) const {
1189        assert(isVariableTracked(Var) && "Var not tracked in block");
1190        return LiveLoc[static_cast<unsigned>(Var)];
1191      }
1192  
1193      /// Set LocKind for \p Var only: does not set LocKind for VariableIDs of
1194      /// fragments contained win \p Var.
1195      void setLocKind(VariableID Var, LocKind K) {
1196        VariableIDsInBlock.set(static_cast<unsigned>(Var));
1197        LiveLoc[static_cast<unsigned>(Var)] = K;
1198      }
1199  
1200      /// Set the assignment in the \p Kind assignment map for \p Var only: does
1201      /// not set the assignment for VariableIDs of fragments contained win \p
1202      /// Var.
1203      void setAssignment(AssignmentKind Kind, VariableID Var,
1204                         const Assignment &AV) {
1205        VariableIDsInBlock.set(static_cast<unsigned>(Var));
1206        getAssignmentMap(Kind)[static_cast<unsigned>(Var)] = AV;
1207      }
1208  
1209      /// Return true if there is an assignment matching \p AV in the \p Kind
1210      /// assignment map. Does consider assignments for VariableIDs of fragments
1211      /// contained win \p Var.
1212      bool hasAssignment(AssignmentKind Kind, VariableID Var,
1213                         const Assignment &AV) const {
1214        if (!isVariableTracked(Var))
1215          return false;
1216        return AV.isSameSourceAssignment(getAssignment(Kind, Var));
1217      }
1218  
1219      /// Compare every element in each map to determine structural equality
1220      /// (slow).
1221      bool operator==(const BlockInfo &Other) const {
1222        return VariableIDsInBlock == Other.VariableIDsInBlock &&
1223               LiveLoc == Other.LiveLoc &&
1224               mapsAreEqual(VariableIDsInBlock, StackHomeValue,
1225                            Other.StackHomeValue) &&
1226               mapsAreEqual(VariableIDsInBlock, DebugValue, Other.DebugValue);
1227      }
1228      bool operator!=(const BlockInfo &Other) const { return !(*this == Other); }
1229      bool isValid() {
1230        return LiveLoc.size() == DebugValue.size() &&
1231               LiveLoc.size() == StackHomeValue.size();
1232      }
1233  
1234      /// Clear everything and initialise with ⊤-values for all variables.
1235      void init(int NumVars) {
1236        StackHomeValue.clear();
1237        DebugValue.clear();
1238        LiveLoc.clear();
1239        VariableIDsInBlock = BitVector(NumVars);
1240        StackHomeValue.insert(StackHomeValue.begin(), NumVars,
1241                              Assignment::makeNoneOrPhi());
1242        DebugValue.insert(DebugValue.begin(), NumVars,
1243                          Assignment::makeNoneOrPhi());
1244        LiveLoc.insert(LiveLoc.begin(), NumVars, LocKind::None);
1245      }
1246  
1247      /// Helper for join.
1248      template <typename ElmtType, typename FnInputType>
1249      static void joinElmt(int Index, SmallVector<ElmtType> &Target,
1250                           const SmallVector<ElmtType> &A,
1251                           const SmallVector<ElmtType> &B,
1252                           ElmtType (*Fn)(FnInputType, FnInputType)) {
1253        Target[Index] = Fn(A[Index], B[Index]);
1254      }
1255  
1256      /// See comment for AssignmentTrackingLowering::joinBlockInfo.
1257      static BlockInfo join(const BlockInfo &A, const BlockInfo &B, int NumVars) {
1258        // Join A and B.
1259        //
1260        // Intersect = join(a, b) for a in A, b in B where Var(a) == Var(b)
1261        // Difference = join(x, ⊤) for x where Var(x) is in A xor B
1262        // Join = Intersect ∪ Difference
1263        //
1264        // This is achieved by performing a join on elements from A and B with
1265        // variables common to both A and B (join elements indexed by var
1266        // intersect), then adding ⊤-value elements for vars in A xor B. The
1267        // latter part is equivalent to performing join on elements with variables
1268        // in A xor B with the ⊤-value for the map element since join(x, ⊤) = ⊤.
1269        // BlockInfo::init initializes all variable entries to the ⊤ value so we
1270        // don't need to explicitly perform that step as Join.VariableIDsInBlock
1271        // is set to the union of the variables in A and B at the end of this
1272        // function.
1273        BlockInfo Join;
1274        Join.init(NumVars);
1275  
1276        BitVector Intersect = A.VariableIDsInBlock;
1277        Intersect &= B.VariableIDsInBlock;
1278  
1279        for (auto VarID : Intersect.set_bits()) {
1280          joinElmt(VarID, Join.LiveLoc, A.LiveLoc, B.LiveLoc, joinKind);
1281          joinElmt(VarID, Join.DebugValue, A.DebugValue, B.DebugValue,
1282                   joinAssignment);
1283          joinElmt(VarID, Join.StackHomeValue, A.StackHomeValue, B.StackHomeValue,
1284                   joinAssignment);
1285        }
1286  
1287        Join.VariableIDsInBlock = A.VariableIDsInBlock;
1288        Join.VariableIDsInBlock |= B.VariableIDsInBlock;
1289        assert(Join.isValid());
1290        return Join;
1291      }
1292    };
1293  
1294    Function &Fn;
1295    const DataLayout &Layout;
1296    const DenseSet<DebugAggregate> *VarsWithStackSlot;
1297    FunctionVarLocsBuilder *FnVarLocs;
1298    DenseMap<const BasicBlock *, BlockInfo> LiveIn;
1299    DenseMap<const BasicBlock *, BlockInfo> LiveOut;
1300  
1301    /// Helper for process methods to track variables touched each frame.
1302    DenseSet<VariableID> VarsTouchedThisFrame;
1303  
1304    /// The set of variables that sometimes are not located in their stack home.
1305    DenseSet<DebugAggregate> NotAlwaysStackHomed;
1306  
1307    VariableID getVariableID(const DebugVariable &Var) {
1308      return static_cast<VariableID>(FnVarLocs->insertVariable(Var));
1309    }
1310  
1311    /// Join the LiveOut values of preds that are contained in \p Visited into
1312    /// LiveIn[BB]. Return True if LiveIn[BB] has changed as a result. LiveIn[BB]
1313    /// values monotonically increase. See the @link joinMethods join methods
1314    /// @endlink documentation for more info.
1315    bool join(const BasicBlock &BB, const SmallPtrSet<BasicBlock *, 16> &Visited);
1316    ///@name joinMethods
1317    /// Functions that implement `join` (the least upper bound) for the
1318    /// join-semilattice types used in the dataflow. There is an explicit bottom
1319    /// value (⊥) for some types and and explicit top value (⊤) for all types.
1320    /// By definition:
1321    ///
1322    ///     Join(A, B) >= A && Join(A, B) >= B
1323    ///     Join(A, ⊥) = A
1324    ///     Join(A, ⊤) = ⊤
1325    ///
1326    /// These invariants are important for monotonicity.
1327    ///
1328    /// For the map-type functions, all unmapped keys in an empty map are
1329    /// associated with a bottom value (⊥). This represents their values being
1330    /// unknown. Unmapped keys in non-empty maps (joining two maps with a key
1331    /// only present in one) represents either a variable going out of scope or
1332    /// dropped debug info. It is assumed the key is associated with a top value
1333    /// (⊤) in this case (unknown location / assignment).
1334    ///@{
1335    static LocKind joinKind(LocKind A, LocKind B);
1336    static Assignment joinAssignment(const Assignment &A, const Assignment &B);
1337    BlockInfo joinBlockInfo(const BlockInfo &A, const BlockInfo &B);
1338    ///@}
1339  
1340    /// Process the instructions in \p BB updating \p LiveSet along the way. \p
1341    /// LiveSet must be initialized with the current live-in locations before
1342    /// calling this.
1343    void process(BasicBlock &BB, BlockInfo *LiveSet);
1344    ///@name processMethods
1345    /// Methods to process instructions in order to update the LiveSet (current
1346    /// location information).
1347    ///@{
1348    void processNonDbgInstruction(Instruction &I, BlockInfo *LiveSet);
1349    void processDbgInstruction(DbgInfoIntrinsic &I, BlockInfo *LiveSet);
1350    /// Update \p LiveSet after encountering an instruction with a DIAssignID
1351    /// attachment, \p I.
1352    void processTaggedInstruction(Instruction &I, BlockInfo *LiveSet);
1353    /// Update \p LiveSet after encountering an instruciton without a DIAssignID
1354    /// attachment, \p I.
1355    void processUntaggedInstruction(Instruction &I, BlockInfo *LiveSet);
1356    void processDbgAssign(AssignRecord Assign, BlockInfo *LiveSet);
1357    void processDPValue(DPValue &DPV, BlockInfo *LiveSet);
1358    void processDbgValue(PointerUnion<DbgValueInst *, DPValue *> DbgValueRecord,
1359                         BlockInfo *LiveSet);
1360    /// Add an assignment to memory for the variable /p Var.
1361    void addMemDef(BlockInfo *LiveSet, VariableID Var, const Assignment &AV);
1362    /// Add an assignment to the variable /p Var.
1363    void addDbgDef(BlockInfo *LiveSet, VariableID Var, const Assignment &AV);
1364    ///@}
1365  
1366    /// Set the LocKind for \p Var.
1367    void setLocKind(BlockInfo *LiveSet, VariableID Var, LocKind K);
1368    /// Get the live LocKind for a \p Var. Requires addMemDef or addDbgDef to
1369    /// have been called for \p Var first.
1370    LocKind getLocKind(BlockInfo *LiveSet, VariableID Var);
1371    /// Return true if \p Var has an assignment in \p M matching \p AV.
1372    bool hasVarWithAssignment(BlockInfo *LiveSet, BlockInfo::AssignmentKind Kind,
1373                              VariableID Var, const Assignment &AV);
1374    /// Return the set of VariableIDs corresponding the fragments contained fully
1375    /// within the variable/fragment \p Var.
1376    ArrayRef<VariableID> getContainedFragments(VariableID Var) const;
1377  
1378    /// Mark \p Var as having been touched this frame. Note, this applies only
1379    /// to the exact fragment \p Var and not to any fragments contained within.
1380    void touchFragment(VariableID Var);
1381  
1382    /// Emit info for variables that are fully promoted.
1383    bool emitPromotedVarLocs(FunctionVarLocsBuilder *FnVarLocs);
1384  
1385  public:
1386    AssignmentTrackingLowering(Function &Fn, const DataLayout &Layout,
1387                               const DenseSet<DebugAggregate> *VarsWithStackSlot)
1388        : Fn(Fn), Layout(Layout), VarsWithStackSlot(VarsWithStackSlot) {}
1389    /// Run the analysis, adding variable location info to \p FnVarLocs. Returns
1390    /// true if any variable locations have been added to FnVarLocs.
1391    bool run(FunctionVarLocsBuilder *FnVarLocs);
1392  };
1393  } // namespace
1394  
1395  ArrayRef<VariableID>
1396  AssignmentTrackingLowering::getContainedFragments(VariableID Var) const {
1397    auto R = VarContains.find(Var);
1398    if (R == VarContains.end())
1399      return std::nullopt;
1400    return R->second;
1401  }
1402  
1403  void AssignmentTrackingLowering::touchFragment(VariableID Var) {
1404    VarsTouchedThisFrame.insert(Var);
1405  }
1406  
1407  void AssignmentTrackingLowering::setLocKind(BlockInfo *LiveSet, VariableID Var,
1408                                              LocKind K) {
1409    auto SetKind = [this](BlockInfo *LiveSet, VariableID Var, LocKind K) {
1410      LiveSet->setLocKind(Var, K);
1411      touchFragment(Var);
1412    };
1413    SetKind(LiveSet, Var, K);
1414  
1415    // Update the LocKind for all fragments contained within Var.
1416    for (VariableID Frag : getContainedFragments(Var))
1417      SetKind(LiveSet, Frag, K);
1418  }
1419  
1420  AssignmentTrackingLowering::LocKind
1421  AssignmentTrackingLowering::getLocKind(BlockInfo *LiveSet, VariableID Var) {
1422    return LiveSet->getLocKind(Var);
1423  }
1424  
1425  void AssignmentTrackingLowering::addMemDef(BlockInfo *LiveSet, VariableID Var,
1426                                             const Assignment &AV) {
1427    LiveSet->setAssignment(BlockInfo::Stack, Var, AV);
1428  
1429    // Use this assigment for all fragments contained within Var, but do not
1430    // provide a Source because we cannot convert Var's value to a value for the
1431    // fragment.
1432    Assignment FragAV = AV;
1433    FragAV.Source = nullptr;
1434    for (VariableID Frag : getContainedFragments(Var))
1435      LiveSet->setAssignment(BlockInfo::Stack, Frag, FragAV);
1436  }
1437  
1438  void AssignmentTrackingLowering::addDbgDef(BlockInfo *LiveSet, VariableID Var,
1439                                             const Assignment &AV) {
1440    LiveSet->setAssignment(BlockInfo::Debug, Var, AV);
1441  
1442    // Use this assigment for all fragments contained within Var, but do not
1443    // provide a Source because we cannot convert Var's value to a value for the
1444    // fragment.
1445    Assignment FragAV = AV;
1446    FragAV.Source = nullptr;
1447    for (VariableID Frag : getContainedFragments(Var))
1448      LiveSet->setAssignment(BlockInfo::Debug, Frag, FragAV);
1449  }
1450  
1451  static DIAssignID *getIDFromInst(const Instruction &I) {
1452    return cast<DIAssignID>(I.getMetadata(LLVMContext::MD_DIAssignID));
1453  }
1454  
1455  static DIAssignID *getIDFromMarker(const DbgAssignIntrinsic &DAI) {
1456    return cast<DIAssignID>(DAI.getAssignID());
1457  }
1458  
1459  static DIAssignID *getIDFromMarker(const DPValue &DPV) {
1460    assert(DPV.isDbgAssign() &&
1461           "Cannot get a DIAssignID from a non-assign DPValue!");
1462    return DPV.getAssignID();
1463  }
1464  
1465  /// Return true if \p Var has an assignment in \p M matching \p AV.
1466  bool AssignmentTrackingLowering::hasVarWithAssignment(
1467      BlockInfo *LiveSet, BlockInfo::AssignmentKind Kind, VariableID Var,
1468      const Assignment &AV) {
1469    if (!LiveSet->hasAssignment(Kind, Var, AV))
1470      return false;
1471  
1472    // Check all the frags contained within Var as these will have all been
1473    // mapped to AV at the last store to Var.
1474    for (VariableID Frag : getContainedFragments(Var))
1475      if (!LiveSet->hasAssignment(Kind, Frag, AV))
1476        return false;
1477    return true;
1478  }
1479  
1480  #ifndef NDEBUG
1481  const char *locStr(AssignmentTrackingLowering::LocKind Loc) {
1482    using LocKind = AssignmentTrackingLowering::LocKind;
1483    switch (Loc) {
1484    case LocKind::Val:
1485      return "Val";
1486    case LocKind::Mem:
1487      return "Mem";
1488    case LocKind::None:
1489      return "None";
1490    };
1491    llvm_unreachable("unknown LocKind");
1492  }
1493  #endif
1494  
1495  VarLocInsertPt getNextNode(const DPValue *DPV) {
1496    auto NextIt = ++(DPV->getIterator());
1497    if (NextIt == DPV->getMarker()->getDbgValueRange().end())
1498      return DPV->getMarker()->MarkedInstr;
1499    return &*NextIt;
1500  }
1501  VarLocInsertPt getNextNode(const Instruction *Inst) {
1502    const Instruction *Next = Inst->getNextNode();
1503    if (!Next->hasDbgValues())
1504      return Next;
1505    return &*Next->getDbgValueRange().begin();
1506  }
1507  VarLocInsertPt getNextNode(VarLocInsertPt InsertPt) {
1508    if (isa<const Instruction *>(InsertPt))
1509      return getNextNode(cast<const Instruction *>(InsertPt));
1510    return getNextNode(cast<const DPValue *>(InsertPt));
1511  }
1512  
1513  DbgAssignIntrinsic *CastToDbgAssign(DbgVariableIntrinsic *DVI) {
1514    return cast<DbgAssignIntrinsic>(DVI);
1515  }
1516  
1517  DPValue *CastToDbgAssign(DPValue *DPV) {
1518    assert(DPV->isDbgAssign() &&
1519           "Attempted to cast non-assign DPValue to DPVAssign.");
1520    return DPV;
1521  }
1522  
1523  void AssignmentTrackingLowering::emitDbgValue(
1524      AssignmentTrackingLowering::LocKind Kind,
1525      AssignmentTrackingLowering::AssignRecord Source, VarLocInsertPt After) {
1526    if (isa<DbgAssignIntrinsic *>(Source))
1527      emitDbgValue(Kind, cast<DbgAssignIntrinsic *>(Source), After);
1528    else
1529      emitDbgValue(Kind, cast<DPValue *>(Source), After);
1530  }
1531  template <typename T>
1532  void AssignmentTrackingLowering::emitDbgValue(
1533      AssignmentTrackingLowering::LocKind Kind, const T Source,
1534      VarLocInsertPt After) {
1535  
1536    DILocation *DL = Source->getDebugLoc();
1537    auto Emit = [this, Source, After, DL](Metadata *Val, DIExpression *Expr) {
1538      assert(Expr);
1539      if (!Val)
1540        Val = ValueAsMetadata::get(
1541            PoisonValue::get(Type::getInt1Ty(Source->getContext())));
1542  
1543      // Find a suitable insert point.
1544      auto InsertBefore = getNextNode(After);
1545      assert(InsertBefore && "Shouldn't be inserting after a terminator");
1546  
1547      VariableID Var = getVariableID(DebugVariable(Source));
1548      VarLocInfo VarLoc;
1549      VarLoc.VariableID = static_cast<VariableID>(Var);
1550      VarLoc.Expr = Expr;
1551      VarLoc.Values = RawLocationWrapper(Val);
1552      VarLoc.DL = DL;
1553      // Insert it into the map for later.
1554      InsertBeforeMap[InsertBefore].push_back(VarLoc);
1555    };
1556  
1557    // NOTE: This block can mutate Kind.
1558    if (Kind == LocKind::Mem) {
1559      const auto *Assign = CastToDbgAssign(Source);
1560      // Check the address hasn't been dropped (e.g. the debug uses may not have
1561      // been replaced before deleting a Value).
1562      if (Assign->isKillAddress()) {
1563        // The address isn't valid so treat this as a non-memory def.
1564        Kind = LocKind::Val;
1565      } else {
1566        Value *Val = Assign->getAddress();
1567        DIExpression *Expr = Assign->getAddressExpression();
1568        assert(!Expr->getFragmentInfo() &&
1569               "fragment info should be stored in value-expression only");
1570        // Copy the fragment info over from the value-expression to the new
1571        // DIExpression.
1572        if (auto OptFragInfo = Source->getExpression()->getFragmentInfo()) {
1573          auto FragInfo = *OptFragInfo;
1574          Expr = *DIExpression::createFragmentExpression(
1575              Expr, FragInfo.OffsetInBits, FragInfo.SizeInBits);
1576        }
1577        // The address-expression has an implicit deref, add it now.
1578        std::tie(Val, Expr) =
1579            walkToAllocaAndPrependOffsetDeref(Layout, Val, Expr);
1580        Emit(ValueAsMetadata::get(Val), Expr);
1581        return;
1582      }
1583    }
1584  
1585    if (Kind == LocKind::Val) {
1586      Emit(Source->getRawLocation(), Source->getExpression());
1587      return;
1588    }
1589  
1590    if (Kind == LocKind::None) {
1591      Emit(nullptr, Source->getExpression());
1592      return;
1593    }
1594  }
1595  
1596  void AssignmentTrackingLowering::processNonDbgInstruction(
1597      Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1598    if (I.hasMetadata(LLVMContext::MD_DIAssignID))
1599      processTaggedInstruction(I, LiveSet);
1600    else
1601      processUntaggedInstruction(I, LiveSet);
1602  }
1603  
1604  void AssignmentTrackingLowering::processUntaggedInstruction(
1605      Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1606    // Interpret stack stores that are not tagged as an assignment in memory for
1607    // the variables associated with that address. These stores may not be tagged
1608    // because a) the store cannot be represented using dbg.assigns (non-const
1609    // length or offset) or b) the tag was accidentally dropped during
1610    // optimisations. For these stores we fall back to assuming that the stack
1611    // home is a valid location for the variables. The benefit is that this
1612    // prevents us missing an assignment and therefore incorrectly maintaining
1613    // earlier location definitions, and in many cases it should be a reasonable
1614    // assumption. However, this will occasionally lead to slight
1615    // inaccuracies. The value of a hoisted untagged store will be visible
1616    // "early", for example.
1617    assert(!I.hasMetadata(LLVMContext::MD_DIAssignID));
1618    auto It = UntaggedStoreVars.find(&I);
1619    if (It == UntaggedStoreVars.end())
1620      return; // No variables associated with the store destination.
1621  
1622    LLVM_DEBUG(dbgs() << "processUntaggedInstruction on UNTAGGED INST " << I
1623                      << "\n");
1624    // Iterate over the variables that this store affects, add a NoneOrPhi dbg
1625    // and mem def, set lockind to Mem, and emit a location def for each.
1626    for (auto [Var, Info] : It->second) {
1627      // This instruction is treated as both a debug and memory assignment,
1628      // meaning the memory location should be used. We don't have an assignment
1629      // ID though so use Assignment::makeNoneOrPhi() to create an imaginary one.
1630      addMemDef(LiveSet, Var, Assignment::makeNoneOrPhi());
1631      addDbgDef(LiveSet, Var, Assignment::makeNoneOrPhi());
1632      setLocKind(LiveSet, Var, LocKind::Mem);
1633      LLVM_DEBUG(dbgs() << "  setting Stack LocKind to: " << locStr(LocKind::Mem)
1634                        << "\n");
1635      // Build the dbg location def to insert.
1636      //
1637      // DIExpression: Add fragment and offset.
1638      DebugVariable V = FnVarLocs->getVariable(Var);
1639      DIExpression *DIE = DIExpression::get(I.getContext(), std::nullopt);
1640      if (auto Frag = V.getFragment()) {
1641        auto R = DIExpression::createFragmentExpression(DIE, Frag->OffsetInBits,
1642                                                        Frag->SizeInBits);
1643        assert(R && "unexpected createFragmentExpression failure");
1644        DIE = *R;
1645      }
1646      SmallVector<uint64_t, 3> Ops;
1647      if (Info.OffsetInBits)
1648        Ops = {dwarf::DW_OP_plus_uconst, Info.OffsetInBits / 8};
1649      Ops.push_back(dwarf::DW_OP_deref);
1650      DIE = DIExpression::prependOpcodes(DIE, Ops, /*StackValue=*/false,
1651                                         /*EntryValue=*/false);
1652      // Find a suitable insert point, before the next instruction or DPValue
1653      // after I.
1654      auto InsertBefore = getNextNode(&I);
1655      assert(InsertBefore && "Shouldn't be inserting after a terminator");
1656  
1657      // Get DILocation for this unrecorded assignment.
1658      DILocation *InlinedAt = const_cast<DILocation *>(V.getInlinedAt());
1659      const DILocation *DILoc = DILocation::get(
1660          Fn.getContext(), 0, 0, V.getVariable()->getScope(), InlinedAt);
1661  
1662      VarLocInfo VarLoc;
1663      VarLoc.VariableID = static_cast<VariableID>(Var);
1664      VarLoc.Expr = DIE;
1665      VarLoc.Values = RawLocationWrapper(
1666          ValueAsMetadata::get(const_cast<AllocaInst *>(Info.Base)));
1667      VarLoc.DL = DILoc;
1668      // 3. Insert it into the map for later.
1669      InsertBeforeMap[InsertBefore].push_back(VarLoc);
1670    }
1671  }
1672  
1673  void AssignmentTrackingLowering::processTaggedInstruction(
1674      Instruction &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1675    auto Linked = at::getAssignmentMarkers(&I);
1676    auto LinkedDPAssigns = at::getDPVAssignmentMarkers(&I);
1677    // No dbg.assign intrinsics linked.
1678    // FIXME: All vars that have a stack slot this store modifies that don't have
1679    // a dbg.assign linked to it should probably treat this like an untagged
1680    // store.
1681    if (Linked.empty() && LinkedDPAssigns.empty())
1682      return;
1683  
1684    LLVM_DEBUG(dbgs() << "processTaggedInstruction on " << I << "\n");
1685    auto ProcessLinkedAssign = [&](auto *Assign) {
1686      VariableID Var = getVariableID(DebugVariable(Assign));
1687      // Something has gone wrong if VarsWithStackSlot doesn't contain a variable
1688      // that is linked to a store.
1689      assert(VarsWithStackSlot->count(getAggregate(Assign)) &&
1690             "expected Assign's variable to have stack slot");
1691  
1692      Assignment AV = Assignment::makeFromMemDef(getIDFromInst(I));
1693      addMemDef(LiveSet, Var, AV);
1694  
1695      LLVM_DEBUG(dbgs() << "   linked to " << *Assign << "\n");
1696      LLVM_DEBUG(dbgs() << "   LiveLoc " << locStr(getLocKind(LiveSet, Var))
1697                        << " -> ");
1698  
1699      // The last assignment to the stack is now AV. Check if the last debug
1700      // assignment has a matching Assignment.
1701      if (hasVarWithAssignment(LiveSet, BlockInfo::Debug, Var, AV)) {
1702        // The StackHomeValue and DebugValue for this variable match so we can
1703        // emit a stack home location here.
1704        LLVM_DEBUG(dbgs() << "Mem, Stack matches Debug program\n";);
1705        LLVM_DEBUG(dbgs() << "   Stack val: "; AV.dump(dbgs()); dbgs() << "\n");
1706        LLVM_DEBUG(dbgs() << "   Debug val: ";
1707                   LiveSet->DebugValue[static_cast<unsigned>(Var)].dump(dbgs());
1708                   dbgs() << "\n");
1709        setLocKind(LiveSet, Var, LocKind::Mem);
1710        emitDbgValue(LocKind::Mem, Assign, &I);
1711        return;
1712      }
1713  
1714      // The StackHomeValue and DebugValue for this variable do not match. I.e.
1715      // The value currently stored in the stack is not what we'd expect to
1716      // see, so we cannot use emit a stack home location here. Now we will
1717      // look at the live LocKind for the variable and determine an appropriate
1718      // dbg.value to emit.
1719      LocKind PrevLoc = getLocKind(LiveSet, Var);
1720      switch (PrevLoc) {
1721      case LocKind::Val: {
1722        // The value in memory in memory has changed but we're not currently
1723        // using the memory location. Do nothing.
1724        LLVM_DEBUG(dbgs() << "Val, (unchanged)\n";);
1725        setLocKind(LiveSet, Var, LocKind::Val);
1726      } break;
1727      case LocKind::Mem: {
1728        // There's been an assignment to memory that we were using as a
1729        // location for this variable, and the Assignment doesn't match what
1730        // we'd expect to see in memory.
1731        Assignment DbgAV = LiveSet->getAssignment(BlockInfo::Debug, Var);
1732        if (DbgAV.Status == Assignment::NoneOrPhi) {
1733          // We need to terminate any previously open location now.
1734          LLVM_DEBUG(dbgs() << "None, No Debug value available\n";);
1735          setLocKind(LiveSet, Var, LocKind::None);
1736          emitDbgValue(LocKind::None, Assign, &I);
1737        } else {
1738          // The previous DebugValue Value can be used here.
1739          LLVM_DEBUG(dbgs() << "Val, Debug value is Known\n";);
1740          setLocKind(LiveSet, Var, LocKind::Val);
1741          if (DbgAV.Source) {
1742            emitDbgValue(LocKind::Val, DbgAV.Source, &I);
1743          } else {
1744            // PrevAV.Source is nullptr so we must emit undef here.
1745            emitDbgValue(LocKind::None, Assign, &I);
1746          }
1747        }
1748      } break;
1749      case LocKind::None: {
1750        // There's been an assignment to memory and we currently are
1751        // not tracking a location for the variable. Do not emit anything.
1752        LLVM_DEBUG(dbgs() << "None, (unchanged)\n";);
1753        setLocKind(LiveSet, Var, LocKind::None);
1754      } break;
1755      }
1756    };
1757    for (DbgAssignIntrinsic *DAI : Linked)
1758      ProcessLinkedAssign(DAI);
1759    for (DPValue *DPV : LinkedDPAssigns)
1760      ProcessLinkedAssign(DPV);
1761  }
1762  
1763  void AssignmentTrackingLowering::processDbgAssign(AssignRecord Assign,
1764                                                    BlockInfo *LiveSet) {
1765    auto ProcessDbgAssignImpl = [&](auto *DbgAssign) {
1766      // Only bother tracking variables that are at some point stack homed. Other
1767      // variables can be dealt with trivially later.
1768      if (!VarsWithStackSlot->count(getAggregate(DbgAssign)))
1769        return;
1770  
1771      VariableID Var = getVariableID(DebugVariable(DbgAssign));
1772      Assignment AV = Assignment::make(getIDFromMarker(*DbgAssign), DbgAssign);
1773      addDbgDef(LiveSet, Var, AV);
1774  
1775      LLVM_DEBUG(dbgs() << "processDbgAssign on " << *DbgAssign << "\n";);
1776      LLVM_DEBUG(dbgs() << "   LiveLoc " << locStr(getLocKind(LiveSet, Var))
1777                        << " -> ");
1778  
1779      // Check if the DebugValue and StackHomeValue both hold the same
1780      // Assignment.
1781      if (hasVarWithAssignment(LiveSet, BlockInfo::Stack, Var, AV)) {
1782        // They match. We can use the stack home because the debug intrinsics
1783        // state that an assignment happened here, and we know that specific
1784        // assignment was the last one to take place in memory for this variable.
1785        LocKind Kind;
1786        if (DbgAssign->isKillAddress()) {
1787          LLVM_DEBUG(
1788              dbgs()
1789                  << "Val, Stack matches Debug program but address is killed\n";);
1790          Kind = LocKind::Val;
1791        } else {
1792          LLVM_DEBUG(dbgs() << "Mem, Stack matches Debug program\n";);
1793          Kind = LocKind::Mem;
1794        };
1795        setLocKind(LiveSet, Var, Kind);
1796        emitDbgValue(Kind, DbgAssign, DbgAssign);
1797      } else {
1798        // The last assignment to the memory location isn't the one that we want
1799        // to show to the user so emit a dbg.value(Value). Value may be undef.
1800        LLVM_DEBUG(dbgs() << "Val, Stack contents is unknown\n";);
1801        setLocKind(LiveSet, Var, LocKind::Val);
1802        emitDbgValue(LocKind::Val, DbgAssign, DbgAssign);
1803      }
1804    };
1805    if (isa<DPValue *>(Assign))
1806      return ProcessDbgAssignImpl(cast<DPValue *>(Assign));
1807    return ProcessDbgAssignImpl(cast<DbgAssignIntrinsic *>(Assign));
1808  }
1809  
1810  void AssignmentTrackingLowering::processDbgValue(
1811      PointerUnion<DbgValueInst *, DPValue *> DbgValueRecord,
1812      BlockInfo *LiveSet) {
1813    auto ProcessDbgValueImpl = [&](auto *DbgValue) {
1814      // Only other tracking variables that are at some point stack homed.
1815      // Other variables can be dealt with trivally later.
1816      if (!VarsWithStackSlot->count(getAggregate(DbgValue)))
1817        return;
1818  
1819      VariableID Var = getVariableID(DebugVariable(DbgValue));
1820      // We have no ID to create an Assignment with so we mark this assignment as
1821      // NoneOrPhi. Note that the dbg.value still exists, we just cannot determine
1822      // the assignment responsible for setting this value.
1823      // This is fine; dbg.values are essentially interchangable with unlinked
1824      // dbg.assigns, and some passes such as mem2reg and instcombine add them to
1825      // PHIs for promoted variables.
1826      Assignment AV = Assignment::makeNoneOrPhi();
1827      addDbgDef(LiveSet, Var, AV);
1828  
1829      LLVM_DEBUG(dbgs() << "processDbgValue on " << *DbgValue << "\n";);
1830      LLVM_DEBUG(dbgs() << "   LiveLoc " << locStr(getLocKind(LiveSet, Var))
1831                        << " -> Val, dbg.value override");
1832  
1833      setLocKind(LiveSet, Var, LocKind::Val);
1834      emitDbgValue(LocKind::Val, DbgValue, DbgValue);
1835    };
1836    if (isa<DPValue *>(DbgValueRecord))
1837      return ProcessDbgValueImpl(cast<DPValue *>(DbgValueRecord));
1838    return ProcessDbgValueImpl(cast<DbgValueInst *>(DbgValueRecord));
1839  }
1840  
1841  template <typename T> static bool hasZeroSizedFragment(T &DbgValue) {
1842    if (auto F = DbgValue.getExpression()->getFragmentInfo())
1843      return F->SizeInBits == 0;
1844    return false;
1845  }
1846  
1847  void AssignmentTrackingLowering::processDbgInstruction(
1848      DbgInfoIntrinsic &I, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1849    auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
1850    if (!DVI)
1851      return;
1852  
1853    // Ignore assignments to zero bits of the variable.
1854    if (hasZeroSizedFragment(*DVI))
1855      return;
1856  
1857    if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))
1858      processDbgAssign(DAI, LiveSet);
1859    else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
1860      processDbgValue(DVI, LiveSet);
1861  }
1862  void AssignmentTrackingLowering::processDPValue(
1863      DPValue &DPV, AssignmentTrackingLowering::BlockInfo *LiveSet) {
1864    // Ignore assignments to zero bits of the variable.
1865    if (hasZeroSizedFragment(DPV))
1866      return;
1867  
1868    if (DPV.isDbgAssign())
1869      processDbgAssign(&DPV, LiveSet);
1870    else if (DPV.isDbgValue())
1871      processDbgValue(&DPV, LiveSet);
1872  }
1873  
1874  void AssignmentTrackingLowering::resetInsertionPoint(Instruction &After) {
1875    assert(!After.isTerminator() && "Can't insert after a terminator");
1876    auto *R = InsertBeforeMap.find(getNextNode(&After));
1877    if (R == InsertBeforeMap.end())
1878      return;
1879    R->second.clear();
1880  }
1881  void AssignmentTrackingLowering::resetInsertionPoint(DPValue &After) {
1882    auto *R = InsertBeforeMap.find(getNextNode(&After));
1883    if (R == InsertBeforeMap.end())
1884      return;
1885    R->second.clear();
1886  }
1887  
1888  void AssignmentTrackingLowering::process(BasicBlock &BB, BlockInfo *LiveSet) {
1889    // If the block starts with DPValues, we need to process those DPValues as
1890    // their own frame without processing any instructions first.
1891    bool ProcessedLeadingDPValues = !BB.begin()->hasDbgValues();
1892    for (auto II = BB.begin(), EI = BB.end(); II != EI;) {
1893      assert(VarsTouchedThisFrame.empty());
1894      // Process the instructions in "frames". A "frame" includes a single
1895      // non-debug instruction followed any debug instructions before the
1896      // next non-debug instruction.
1897  
1898      // Skip the current instruction if it has unprocessed DPValues attached (see
1899      // comment above `ProcessedLeadingDPValues`).
1900      if (ProcessedLeadingDPValues) {
1901        // II is now either a debug intrinsic, a non-debug instruction with no
1902        // attached DPValues, or a non-debug instruction with attached processed
1903        // DPValues.
1904        // II has not been processed.
1905        if (!isa<DbgInfoIntrinsic>(&*II)) {
1906          if (II->isTerminator())
1907            break;
1908          resetInsertionPoint(*II);
1909          processNonDbgInstruction(*II, LiveSet);
1910          assert(LiveSet->isValid());
1911          ++II;
1912        }
1913      }
1914      // II is now either a debug intrinsic, a non-debug instruction with no
1915      // attached DPValues, or a non-debug instruction with attached unprocessed
1916      // DPValues.
1917      if (II != EI && II->hasDbgValues()) {
1918        for (DPValue &DPV : II->getDbgValueRange()) {
1919          resetInsertionPoint(DPV);
1920          processDPValue(DPV, LiveSet);
1921          assert(LiveSet->isValid());
1922        }
1923      }
1924      ProcessedLeadingDPValues = true;
1925      while (II != EI) {
1926        auto *Dbg = dyn_cast<DbgInfoIntrinsic>(&*II);
1927        if (!Dbg)
1928          break;
1929        resetInsertionPoint(*II);
1930        processDbgInstruction(*Dbg, LiveSet);
1931        assert(LiveSet->isValid());
1932        ++II;
1933      }
1934      // II is now a non-debug instruction either with no attached DPValues, or
1935      // with attached processed DPValues. II has not been processed, and all
1936      // debug instructions or DPValues in the frame preceding II have been
1937      // processed.
1938  
1939      // We've processed everything in the "frame". Now determine which variables
1940      // cannot be represented by a dbg.declare.
1941      for (auto Var : VarsTouchedThisFrame) {
1942        LocKind Loc = getLocKind(LiveSet, Var);
1943        // If a variable's LocKind is anything other than LocKind::Mem then we
1944        // must note that it cannot be represented with a dbg.declare.
1945        // Note that this check is enough without having to check the result of
1946        // joins() because for join to produce anything other than Mem after
1947        // we've already seen a Mem we'd be joining None or Val with Mem. In that
1948        // case, we've already hit this codepath when we set the LocKind to Val
1949        // or None in that block.
1950        if (Loc != LocKind::Mem) {
1951          DebugVariable DbgVar = FnVarLocs->getVariable(Var);
1952          DebugAggregate Aggr{DbgVar.getVariable(), DbgVar.getInlinedAt()};
1953          NotAlwaysStackHomed.insert(Aggr);
1954        }
1955      }
1956      VarsTouchedThisFrame.clear();
1957    }
1958  }
1959  
1960  AssignmentTrackingLowering::LocKind
1961  AssignmentTrackingLowering::joinKind(LocKind A, LocKind B) {
1962    // Partial order:
1963    // None > Mem, Val
1964    return A == B ? A : LocKind::None;
1965  }
1966  
1967  AssignmentTrackingLowering::Assignment
1968  AssignmentTrackingLowering::joinAssignment(const Assignment &A,
1969                                             const Assignment &B) {
1970    // Partial order:
1971    // NoneOrPhi(null, null) > Known(v, ?s)
1972  
1973    // If either are NoneOrPhi the join is NoneOrPhi.
1974    // If either value is different then the result is
1975    // NoneOrPhi (joining two values is a Phi).
1976    if (!A.isSameSourceAssignment(B))
1977      return Assignment::makeNoneOrPhi();
1978    if (A.Status == Assignment::NoneOrPhi)
1979      return Assignment::makeNoneOrPhi();
1980  
1981    // Source is used to lookup the value + expression in the debug program if
1982    // the stack slot gets assigned a value earlier than expected. Because
1983    // we're only tracking the one dbg.assign, we can't capture debug PHIs.
1984    // It's unlikely that we're losing out on much coverage by avoiding that
1985    // extra work.
1986    // The Source may differ in this situation:
1987    // Pred.1:
1988    //   dbg.assign i32 0, ..., !1, ...
1989    // Pred.2:
1990    //   dbg.assign i32 1, ..., !1, ...
1991    // Here the same assignment (!1) was performed in both preds in the source,
1992    // but we can't use either one unless they are identical (e.g. .we don't
1993    // want to arbitrarily pick between constant values).
1994    auto JoinSource = [&]() -> AssignRecord {
1995      if (A.Source == B.Source)
1996        return A.Source;
1997      if (!A.Source || !B.Source)
1998        return AssignRecord();
1999      assert(isa<DPValue *>(A.Source) == isa<DPValue *>(B.Source));
2000      if (isa<DPValue *>(A.Source) &&
2001          cast<DPValue *>(A.Source)->isEquivalentTo(*cast<DPValue *>(B.Source)))
2002        return A.Source;
2003      if (isa<DbgAssignIntrinsic *>(A.Source) &&
2004          cast<DbgAssignIntrinsic *>(A.Source)->isIdenticalTo(
2005              cast<DbgAssignIntrinsic *>(B.Source)))
2006        return A.Source;
2007      return AssignRecord();
2008    };
2009    AssignRecord Source = JoinSource();
2010    assert(A.Status == B.Status && A.Status == Assignment::Known);
2011    assert(A.ID == B.ID);
2012    return Assignment::make(A.ID, Source);
2013  }
2014  
2015  AssignmentTrackingLowering::BlockInfo
2016  AssignmentTrackingLowering::joinBlockInfo(const BlockInfo &A,
2017                                            const BlockInfo &B) {
2018    return BlockInfo::join(A, B, TrackedVariablesVectorSize);
2019  }
2020  
2021  bool AssignmentTrackingLowering::join(
2022      const BasicBlock &BB, const SmallPtrSet<BasicBlock *, 16> &Visited) {
2023  
2024    SmallVector<const BasicBlock *> VisitedPreds;
2025    // Ignore backedges if we have not visited the predecessor yet. As the
2026    // predecessor hasn't yet had locations propagated into it, most locations
2027    // will not yet be valid, so treat them as all being uninitialized and
2028    // potentially valid. If a location guessed to be correct here is
2029    // invalidated later, we will remove it when we revisit this block. This
2030    // is essentially the same as initialising all LocKinds and Assignments to
2031    // an implicit ⊥ value which is the identity value for the join operation.
2032    for (const BasicBlock *Pred : predecessors(&BB)) {
2033      if (Visited.count(Pred))
2034        VisitedPreds.push_back(Pred);
2035    }
2036  
2037    // No preds visited yet.
2038    if (VisitedPreds.empty()) {
2039      auto It = LiveIn.try_emplace(&BB, BlockInfo());
2040      bool DidInsert = It.second;
2041      if (DidInsert)
2042        It.first->second.init(TrackedVariablesVectorSize);
2043      return /*Changed*/ DidInsert;
2044    }
2045  
2046    // Exactly one visited pred. Copy the LiveOut from that pred into BB LiveIn.
2047    if (VisitedPreds.size() == 1) {
2048      const BlockInfo &PredLiveOut = LiveOut.find(VisitedPreds[0])->second;
2049      auto CurrentLiveInEntry = LiveIn.find(&BB);
2050  
2051      // Check if there isn't an entry, or there is but the LiveIn set has
2052      // changed (expensive check).
2053      if (CurrentLiveInEntry == LiveIn.end())
2054        LiveIn.insert(std::make_pair(&BB, PredLiveOut));
2055      else if (PredLiveOut != CurrentLiveInEntry->second)
2056        CurrentLiveInEntry->second = PredLiveOut;
2057      else
2058        return /*Changed*/ false;
2059      return /*Changed*/ true;
2060    }
2061  
2062    // More than one pred. Join LiveOuts of blocks 1 and 2.
2063    assert(VisitedPreds.size() > 1);
2064    const BlockInfo &PredLiveOut0 = LiveOut.find(VisitedPreds[0])->second;
2065    const BlockInfo &PredLiveOut1 = LiveOut.find(VisitedPreds[1])->second;
2066    BlockInfo BBLiveIn = joinBlockInfo(PredLiveOut0, PredLiveOut1);
2067  
2068    // Join the LiveOuts of subsequent blocks.
2069    ArrayRef Tail = ArrayRef(VisitedPreds).drop_front(2);
2070    for (const BasicBlock *Pred : Tail) {
2071      const auto &PredLiveOut = LiveOut.find(Pred);
2072      assert(PredLiveOut != LiveOut.end() &&
2073             "block should have been processed already");
2074      BBLiveIn = joinBlockInfo(std::move(BBLiveIn), PredLiveOut->second);
2075    }
2076  
2077    // Save the joined result for BB.
2078    auto CurrentLiveInEntry = LiveIn.find(&BB);
2079    // Check if there isn't an entry, or there is but the LiveIn set has changed
2080    // (expensive check).
2081    if (CurrentLiveInEntry == LiveIn.end())
2082      LiveIn.try_emplace(&BB, std::move(BBLiveIn));
2083    else if (BBLiveIn != CurrentLiveInEntry->second)
2084      CurrentLiveInEntry->second = std::move(BBLiveIn);
2085    else
2086      return /*Changed*/ false;
2087    return /*Changed*/ true;
2088  }
2089  
2090  /// Return true if A fully contains B.
2091  static bool fullyContains(DIExpression::FragmentInfo A,
2092                            DIExpression::FragmentInfo B) {
2093    auto ALeft = A.OffsetInBits;
2094    auto BLeft = B.OffsetInBits;
2095    if (BLeft < ALeft)
2096      return false;
2097  
2098    auto ARight = ALeft + A.SizeInBits;
2099    auto BRight = BLeft + B.SizeInBits;
2100    if (BRight > ARight)
2101      return false;
2102    return true;
2103  }
2104  
2105  static std::optional<at::AssignmentInfo>
2106  getUntaggedStoreAssignmentInfo(const Instruction &I, const DataLayout &Layout) {
2107    // Don't bother checking if this is an AllocaInst. We know this
2108    // instruction has no tag which means there are no variables associated
2109    // with it.
2110    if (const auto *SI = dyn_cast<StoreInst>(&I))
2111      return at::getAssignmentInfo(Layout, SI);
2112    if (const auto *MI = dyn_cast<MemIntrinsic>(&I))
2113      return at::getAssignmentInfo(Layout, MI);
2114    // Alloca or non-store-like inst.
2115    return std::nullopt;
2116  }
2117  
2118  DbgDeclareInst *DynCastToDbgDeclare(DbgVariableIntrinsic *DVI) {
2119    return dyn_cast<DbgDeclareInst>(DVI);
2120  }
2121  
2122  DPValue *DynCastToDbgDeclare(DPValue *DPV) {
2123    return DPV->isDbgDeclare() ? DPV : nullptr;
2124  }
2125  
2126  /// Build a map of {Variable x: Variables y} where all variable fragments
2127  /// contained within the variable fragment x are in set y. This means that
2128  /// y does not contain all overlaps because partial overlaps are excluded.
2129  ///
2130  /// While we're iterating over the function, add single location defs for
2131  /// dbg.declares to \p FnVarLocs.
2132  ///
2133  /// Variables that are interesting to this pass in are added to
2134  /// FnVarLocs->Variables first. TrackedVariablesVectorSize is set to the ID of
2135  /// the last interesting variable plus 1, meaning variables with ID 1
2136  /// (inclusive) to TrackedVariablesVectorSize (exclusive) are interesting. The
2137  /// subsequent variables are either stack homed or fully promoted.
2138  ///
2139  /// Finally, populate UntaggedStoreVars with a mapping of untagged stores to
2140  /// the stored-to variable fragments.
2141  ///
2142  /// These tasks are bundled together to reduce the number of times we need
2143  /// to iterate over the function as they can be achieved together in one pass.
2144  static AssignmentTrackingLowering::OverlapMap buildOverlapMapAndRecordDeclares(
2145      Function &Fn, FunctionVarLocsBuilder *FnVarLocs,
2146      const DenseSet<DebugAggregate> &VarsWithStackSlot,
2147      AssignmentTrackingLowering::UntaggedStoreAssignmentMap &UntaggedStoreVars,
2148      unsigned &TrackedVariablesVectorSize) {
2149    DenseSet<DebugVariable> Seen;
2150    // Map of Variable: [Fragments].
2151    DenseMap<DebugAggregate, SmallVector<DebugVariable, 8>> FragmentMap;
2152    // Iterate over all instructions:
2153    // - dbg.declare    -> add single location variable record
2154    // - dbg.*          -> Add fragments to FragmentMap
2155    // - untagged store -> Add fragments to FragmentMap and update
2156    //                     UntaggedStoreVars.
2157    // We need to add fragments for untagged stores too so that we can correctly
2158    // clobber overlapped fragment locations later.
2159    SmallVector<DbgDeclareInst *> InstDeclares;
2160    SmallVector<DPValue *> DPDeclares;
2161    auto ProcessDbgRecord = [&](auto *Record, auto &DeclareList) {
2162      if (auto *Declare = DynCastToDbgDeclare(Record)) {
2163        DeclareList.push_back(Declare);
2164        return;
2165      }
2166      DebugVariable DV = DebugVariable(Record);
2167      DebugAggregate DA = {DV.getVariable(), DV.getInlinedAt()};
2168      if (!VarsWithStackSlot.contains(DA))
2169        return;
2170      if (Seen.insert(DV).second)
2171        FragmentMap[DA].push_back(DV);
2172    };
2173    for (auto &BB : Fn) {
2174      for (auto &I : BB) {
2175        for (auto &DPV : I.getDbgValueRange())
2176          ProcessDbgRecord(&DPV, DPDeclares);
2177        if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
2178          ProcessDbgRecord(DII, InstDeclares);
2179        } else if (auto Info = getUntaggedStoreAssignmentInfo(
2180                       I, Fn.getParent()->getDataLayout())) {
2181          // Find markers linked to this alloca.
2182          auto HandleDbgAssignForStore = [&](auto *Assign) {
2183            std::optional<DIExpression::FragmentInfo> FragInfo;
2184  
2185            // Skip this assignment if the affected bits are outside of the
2186            // variable fragment.
2187            if (!at::calculateFragmentIntersect(
2188                    I.getModule()->getDataLayout(), Info->Base,
2189                    Info->OffsetInBits, Info->SizeInBits, Assign, FragInfo) ||
2190                (FragInfo && FragInfo->SizeInBits == 0))
2191              return;
2192  
2193            // FragInfo from calculateFragmentIntersect is nullopt if the
2194            // resultant fragment matches DAI's fragment or entire variable - in
2195            // which case copy the fragment info from DAI. If FragInfo is still
2196            // nullopt after the copy it means "no fragment info" instead, which
2197            // is how it is usually interpreted.
2198            if (!FragInfo)
2199              FragInfo = Assign->getExpression()->getFragmentInfo();
2200  
2201            DebugVariable DV =
2202                DebugVariable(Assign->getVariable(), FragInfo,
2203                              Assign->getDebugLoc().getInlinedAt());
2204            DebugAggregate DA = {DV.getVariable(), DV.getInlinedAt()};
2205            if (!VarsWithStackSlot.contains(DA))
2206              return;
2207  
2208            // Cache this info for later.
2209            UntaggedStoreVars[&I].push_back(
2210                {FnVarLocs->insertVariable(DV), *Info});
2211  
2212            if (Seen.insert(DV).second)
2213              FragmentMap[DA].push_back(DV);
2214          };
2215          for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(Info->Base))
2216            HandleDbgAssignForStore(DAI);
2217          for (DPValue *DPV : at::getDPVAssignmentMarkers(Info->Base))
2218            HandleDbgAssignForStore(DPV);
2219        }
2220      }
2221    }
2222  
2223    // Sort the fragment map for each DebugAggregate in ascending
2224    // order of fragment size - there should be no duplicates.
2225    for (auto &Pair : FragmentMap) {
2226      SmallVector<DebugVariable, 8> &Frags = Pair.second;
2227      std::sort(Frags.begin(), Frags.end(),
2228                [](const DebugVariable &Next, const DebugVariable &Elmt) {
2229                  return Elmt.getFragmentOrDefault().SizeInBits >
2230                         Next.getFragmentOrDefault().SizeInBits;
2231                });
2232      // Check for duplicates.
2233      assert(std::adjacent_find(Frags.begin(), Frags.end()) == Frags.end());
2234    }
2235  
2236    // Build the map.
2237    AssignmentTrackingLowering::OverlapMap Map;
2238    for (auto &Pair : FragmentMap) {
2239      auto &Frags = Pair.second;
2240      for (auto It = Frags.begin(), IEnd = Frags.end(); It != IEnd; ++It) {
2241        DIExpression::FragmentInfo Frag = It->getFragmentOrDefault();
2242        // Find the frags that this is contained within.
2243        //
2244        // Because Frags is sorted by size and none have the same offset and
2245        // size, we know that this frag can only be contained by subsequent
2246        // elements.
2247        SmallVector<DebugVariable, 8>::iterator OtherIt = It;
2248        ++OtherIt;
2249        VariableID ThisVar = FnVarLocs->insertVariable(*It);
2250        for (; OtherIt != IEnd; ++OtherIt) {
2251          DIExpression::FragmentInfo OtherFrag = OtherIt->getFragmentOrDefault();
2252          VariableID OtherVar = FnVarLocs->insertVariable(*OtherIt);
2253          if (fullyContains(OtherFrag, Frag))
2254            Map[OtherVar].push_back(ThisVar);
2255        }
2256      }
2257    }
2258  
2259    // VariableIDs are 1-based so the variable-tracking bitvector needs
2260    // NumVariables plus 1 bits.
2261    TrackedVariablesVectorSize = FnVarLocs->getNumVariables() + 1;
2262  
2263    // Finally, insert the declares afterwards, so the first IDs are all
2264    // partially stack homed vars.
2265    for (auto *DDI : InstDeclares)
2266      FnVarLocs->addSingleLocVar(DebugVariable(DDI), DDI->getExpression(),
2267                                 DDI->getDebugLoc(), DDI->getWrappedLocation());
2268    for (auto *DPV : DPDeclares)
2269      FnVarLocs->addSingleLocVar(DebugVariable(DPV), DPV->getExpression(),
2270                                 DPV->getDebugLoc(),
2271                                 RawLocationWrapper(DPV->getRawLocation()));
2272    return Map;
2273  }
2274  
2275  bool AssignmentTrackingLowering::run(FunctionVarLocsBuilder *FnVarLocsBuilder) {
2276    if (Fn.size() > MaxNumBlocks) {
2277      LLVM_DEBUG(dbgs() << "[AT] Dropping var locs in: " << Fn.getName()
2278                        << ": too many blocks (" << Fn.size() << ")\n");
2279      at::deleteAll(&Fn);
2280      return false;
2281    }
2282  
2283    FnVarLocs = FnVarLocsBuilder;
2284  
2285    // The general structure here is inspired by VarLocBasedImpl.cpp
2286    // (LiveDebugValues).
2287  
2288    // Build the variable fragment overlap map.
2289    // Note that this pass doesn't handle partial overlaps correctly (FWIW
2290    // neither does LiveDebugVariables) because that is difficult to do and
2291    // appears to be rare occurance.
2292    VarContains = buildOverlapMapAndRecordDeclares(
2293        Fn, FnVarLocs, *VarsWithStackSlot, UntaggedStoreVars,
2294        TrackedVariablesVectorSize);
2295  
2296    // Prepare for traversal.
2297    ReversePostOrderTraversal<Function *> RPOT(&Fn);
2298    std::priority_queue<unsigned int, std::vector<unsigned int>,
2299                        std::greater<unsigned int>>
2300        Worklist;
2301    std::priority_queue<unsigned int, std::vector<unsigned int>,
2302                        std::greater<unsigned int>>
2303        Pending;
2304    DenseMap<unsigned int, BasicBlock *> OrderToBB;
2305    DenseMap<BasicBlock *, unsigned int> BBToOrder;
2306    { // Init OrderToBB and BBToOrder.
2307      unsigned int RPONumber = 0;
2308      for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
2309        OrderToBB[RPONumber] = *RI;
2310        BBToOrder[*RI] = RPONumber;
2311        Worklist.push(RPONumber);
2312        ++RPONumber;
2313      }
2314      LiveIn.init(RPONumber);
2315      LiveOut.init(RPONumber);
2316    }
2317  
2318    // Perform the traversal.
2319    //
2320    // This is a standard "union of predecessor outs" dataflow problem. To solve
2321    // it, we perform join() and process() using the two worklist method until
2322    // the LiveIn data for each block becomes unchanging. The "proof" that this
2323    // terminates can be put together by looking at the comments around LocKind,
2324    // Assignment, and the various join methods, which show that all the elements
2325    // involved are made up of join-semilattices; LiveIn(n) can only
2326    // monotonically increase in value throughout the dataflow.
2327    //
2328    SmallPtrSet<BasicBlock *, 16> Visited;
2329    while (!Worklist.empty()) {
2330      // We track what is on the pending worklist to avoid inserting the same
2331      // thing twice.
2332      SmallPtrSet<BasicBlock *, 16> OnPending;
2333      LLVM_DEBUG(dbgs() << "Processing Worklist\n");
2334      while (!Worklist.empty()) {
2335        BasicBlock *BB = OrderToBB[Worklist.top()];
2336        LLVM_DEBUG(dbgs() << "\nPop BB " << BB->getName() << "\n");
2337        Worklist.pop();
2338        bool InChanged = join(*BB, Visited);
2339        // Always consider LiveIn changed on the first visit.
2340        InChanged |= Visited.insert(BB).second;
2341        if (InChanged) {
2342          LLVM_DEBUG(dbgs() << BB->getName() << " has new InLocs, process it\n");
2343          // Mutate a copy of LiveIn while processing BB. After calling process
2344          // LiveSet is the LiveOut set for BB.
2345          BlockInfo LiveSet = LiveIn[BB];
2346  
2347          // Process the instructions in the block.
2348          process(*BB, &LiveSet);
2349  
2350          // Relatively expensive check: has anything changed in LiveOut for BB?
2351          if (LiveOut[BB] != LiveSet) {
2352            LLVM_DEBUG(dbgs() << BB->getName()
2353                              << " has new OutLocs, add succs to worklist: [ ");
2354            LiveOut[BB] = std::move(LiveSet);
2355            for (auto I = succ_begin(BB), E = succ_end(BB); I != E; I++) {
2356              if (OnPending.insert(*I).second) {
2357                LLVM_DEBUG(dbgs() << I->getName() << " ");
2358                Pending.push(BBToOrder[*I]);
2359              }
2360            }
2361            LLVM_DEBUG(dbgs() << "]\n");
2362          }
2363        }
2364      }
2365      Worklist.swap(Pending);
2366      // At this point, pending must be empty, since it was just the empty
2367      // worklist
2368      assert(Pending.empty() && "Pending should be empty");
2369    }
2370  
2371    // That's the hard part over. Now we just have some admin to do.
2372  
2373    // Record whether we inserted any intrinsics.
2374    bool InsertedAnyIntrinsics = false;
2375  
2376    // Identify and add defs for single location variables.
2377    //
2378    // Go through all of the defs that we plan to add. If the aggregate variable
2379    // it's a part of is not in the NotAlwaysStackHomed set we can emit a single
2380    // location def and omit the rest. Add an entry to AlwaysStackHomed so that
2381    // we can identify those uneeded defs later.
2382    DenseSet<DebugAggregate> AlwaysStackHomed;
2383    for (const auto &Pair : InsertBeforeMap) {
2384      auto &Vec = Pair.second;
2385      for (VarLocInfo VarLoc : Vec) {
2386        DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2387        DebugAggregate Aggr{Var.getVariable(), Var.getInlinedAt()};
2388  
2389        // Skip this Var if it's not always stack homed.
2390        if (NotAlwaysStackHomed.contains(Aggr))
2391          continue;
2392  
2393        // Skip complex cases such as when different fragments of a variable have
2394        // been split into different allocas. Skipping in this case means falling
2395        // back to using a list of defs (which could reduce coverage, but is no
2396        // less correct).
2397        bool Simple =
2398            VarLoc.Expr->getNumElements() == 1 && VarLoc.Expr->startsWithDeref();
2399        if (!Simple) {
2400          NotAlwaysStackHomed.insert(Aggr);
2401          continue;
2402        }
2403  
2404        // All source assignments to this variable remain and all stores to any
2405        // part of the variable store to the same address (with varying
2406        // offsets). We can just emit a single location for the whole variable.
2407        //
2408        // Unless we've already done so, create the single location def now.
2409        if (AlwaysStackHomed.insert(Aggr).second) {
2410          assert(!VarLoc.Values.hasArgList());
2411          // TODO: When more complex cases are handled VarLoc.Expr should be
2412          // built appropriately rather than always using an empty DIExpression.
2413          // The assert below is a reminder.
2414          assert(Simple);
2415          VarLoc.Expr = DIExpression::get(Fn.getContext(), std::nullopt);
2416          DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2417          FnVarLocs->addSingleLocVar(Var, VarLoc.Expr, VarLoc.DL, VarLoc.Values);
2418          InsertedAnyIntrinsics = true;
2419        }
2420      }
2421    }
2422  
2423    // Insert the other DEFs.
2424    for (const auto &[InsertBefore, Vec] : InsertBeforeMap) {
2425      SmallVector<VarLocInfo> NewDefs;
2426      for (const VarLocInfo &VarLoc : Vec) {
2427        DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2428        DebugAggregate Aggr{Var.getVariable(), Var.getInlinedAt()};
2429        // If this variable is always stack homed then we have already inserted a
2430        // dbg.declare and deleted this dbg.value.
2431        if (AlwaysStackHomed.contains(Aggr))
2432          continue;
2433        NewDefs.push_back(VarLoc);
2434        InsertedAnyIntrinsics = true;
2435      }
2436  
2437      FnVarLocs->setWedge(InsertBefore, std::move(NewDefs));
2438    }
2439  
2440    InsertedAnyIntrinsics |= emitPromotedVarLocs(FnVarLocs);
2441  
2442    return InsertedAnyIntrinsics;
2443  }
2444  
2445  bool AssignmentTrackingLowering::emitPromotedVarLocs(
2446      FunctionVarLocsBuilder *FnVarLocs) {
2447    bool InsertedAnyIntrinsics = false;
2448    // Go through every block, translating debug intrinsics for fully promoted
2449    // variables into FnVarLocs location defs. No analysis required for these.
2450    auto TranslateDbgRecord = [&](auto *Record) {
2451      // Skip variables that haven't been promoted - we've dealt with those
2452      // already.
2453      if (VarsWithStackSlot->contains(getAggregate(Record)))
2454        return;
2455      auto InsertBefore = getNextNode(Record);
2456      assert(InsertBefore && "Unexpected: debug intrinsics after a terminator");
2457      FnVarLocs->addVarLoc(InsertBefore, DebugVariable(Record),
2458                           Record->getExpression(), Record->getDebugLoc(),
2459                           RawLocationWrapper(Record->getRawLocation()));
2460      InsertedAnyIntrinsics = true;
2461    };
2462    for (auto &BB : Fn) {
2463      for (auto &I : BB) {
2464        // Skip instructions other than dbg.values and dbg.assigns.
2465        for (DPValue &DPV : I.getDbgValueRange())
2466          if (DPV.isDbgValue() || DPV.isDbgAssign())
2467            TranslateDbgRecord(&DPV);
2468        auto *DVI = dyn_cast<DbgValueInst>(&I);
2469        if (DVI)
2470          TranslateDbgRecord(DVI);
2471      }
2472    }
2473    return InsertedAnyIntrinsics;
2474  }
2475  
2476  /// Remove redundant definitions within sequences of consecutive location defs.
2477  /// This is done using a backward scan to keep the last def describing a
2478  /// specific variable/fragment.
2479  ///
2480  /// This implements removeRedundantDbgInstrsUsingBackwardScan from
2481  /// lib/Transforms/Utils/BasicBlockUtils.cpp for locations described with
2482  /// FunctionVarLocsBuilder instead of with intrinsics.
2483  static bool
2484  removeRedundantDbgLocsUsingBackwardScan(const BasicBlock *BB,
2485                                          FunctionVarLocsBuilder &FnVarLocs) {
2486    bool Changed = false;
2487    SmallDenseMap<DebugAggregate, BitVector> VariableDefinedBytes;
2488    // Scan over the entire block, not just over the instructions mapped by
2489    // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
2490    // instructions.
2491    for (const Instruction &I : reverse(*BB)) {
2492      if (!isa<DbgVariableIntrinsic>(I)) {
2493        // Sequence of consecutive defs ended. Clear map for the next one.
2494        VariableDefinedBytes.clear();
2495      }
2496  
2497      auto HandleLocsForWedge = [&](auto *WedgePosition) {
2498        // Get the location defs that start just before this instruction.
2499        const auto *Locs = FnVarLocs.getWedge(WedgePosition);
2500        if (!Locs)
2501          return;
2502  
2503        NumWedgesScanned++;
2504        bool ChangedThisWedge = false;
2505        // The new pruned set of defs, reversed because we're scanning backwards.
2506        SmallVector<VarLocInfo> NewDefsReversed;
2507  
2508        // Iterate over the existing defs in reverse.
2509        for (auto RIt = Locs->rbegin(), REnd = Locs->rend(); RIt != REnd; ++RIt) {
2510          NumDefsScanned++;
2511          DebugAggregate Aggr =
2512              getAggregate(FnVarLocs.getVariable(RIt->VariableID));
2513          uint64_t SizeInBits = Aggr.first->getSizeInBits().value_or(0);
2514          uint64_t SizeInBytes = divideCeil(SizeInBits, 8);
2515  
2516          // Cutoff for large variables to prevent expensive bitvector operations.
2517          const uint64_t MaxSizeBytes = 2048;
2518  
2519          if (SizeInBytes == 0 || SizeInBytes > MaxSizeBytes) {
2520            // If the size is unknown (0) then keep this location def to be safe.
2521            // Do the same for defs of large variables, which would be expensive
2522            // to represent with a BitVector.
2523            NewDefsReversed.push_back(*RIt);
2524            continue;
2525          }
2526  
2527          // Only keep this location definition if it is not fully eclipsed by
2528          // other definitions in this wedge that come after it
2529  
2530          // Inert the bytes the location definition defines.
2531          auto InsertResult =
2532              VariableDefinedBytes.try_emplace(Aggr, BitVector(SizeInBytes));
2533          bool FirstDefinition = InsertResult.second;
2534          BitVector &DefinedBytes = InsertResult.first->second;
2535  
2536          DIExpression::FragmentInfo Fragment =
2537              RIt->Expr->getFragmentInfo().value_or(
2538                  DIExpression::FragmentInfo(SizeInBits, 0));
2539          bool InvalidFragment = Fragment.endInBits() > SizeInBits;
2540          uint64_t StartInBytes = Fragment.startInBits() / 8;
2541          uint64_t EndInBytes = divideCeil(Fragment.endInBits(), 8);
2542  
2543          // If this defines any previously undefined bytes, keep it.
2544          if (FirstDefinition || InvalidFragment ||
2545              DefinedBytes.find_first_unset_in(StartInBytes, EndInBytes) != -1) {
2546            if (!InvalidFragment)
2547              DefinedBytes.set(StartInBytes, EndInBytes);
2548            NewDefsReversed.push_back(*RIt);
2549            continue;
2550          }
2551  
2552          // Redundant def found: throw it away. Since the wedge of defs is being
2553          // rebuilt, doing nothing is the same as deleting an entry.
2554          ChangedThisWedge = true;
2555          NumDefsRemoved++;
2556        }
2557  
2558        // Un-reverse the defs and replace the wedge with the pruned version.
2559        if (ChangedThisWedge) {
2560          std::reverse(NewDefsReversed.begin(), NewDefsReversed.end());
2561          FnVarLocs.setWedge(WedgePosition, std::move(NewDefsReversed));
2562          NumWedgesChanged++;
2563          Changed = true;
2564        }
2565      };
2566      HandleLocsForWedge(&I);
2567      for (DPValue &DPV : reverse(I.getDbgValueRange()))
2568        HandleLocsForWedge(&DPV);
2569    }
2570  
2571    return Changed;
2572  }
2573  
2574  /// Remove redundant location defs using a forward scan. This can remove a
2575  /// location definition that is redundant due to indicating that a variable has
2576  /// the same value as is already being indicated by an earlier def.
2577  ///
2578  /// This implements removeRedundantDbgInstrsUsingForwardScan from
2579  /// lib/Transforms/Utils/BasicBlockUtils.cpp for locations described with
2580  /// FunctionVarLocsBuilder instead of with intrinsics
2581  static bool
2582  removeRedundantDbgLocsUsingForwardScan(const BasicBlock *BB,
2583                                         FunctionVarLocsBuilder &FnVarLocs) {
2584    bool Changed = false;
2585    DenseMap<DebugVariable, std::pair<RawLocationWrapper, DIExpression *>>
2586        VariableMap;
2587  
2588    // Scan over the entire block, not just over the instructions mapped by
2589    // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
2590    // instructions.
2591    for (const Instruction &I : *BB) {
2592      // Get the defs that come just before this instruction.
2593      auto HandleLocsForWedge = [&](auto *WedgePosition) {
2594        const auto *Locs = FnVarLocs.getWedge(WedgePosition);
2595        if (!Locs)
2596          return;
2597  
2598        NumWedgesScanned++;
2599        bool ChangedThisWedge = false;
2600        // The new pruned set of defs.
2601        SmallVector<VarLocInfo> NewDefs;
2602  
2603        // Iterate over the existing defs.
2604        for (const VarLocInfo &Loc : *Locs) {
2605          NumDefsScanned++;
2606          DebugVariable Key(FnVarLocs.getVariable(Loc.VariableID).getVariable(),
2607                            std::nullopt, Loc.DL.getInlinedAt());
2608          auto VMI = VariableMap.find(Key);
2609  
2610          // Update the map if we found a new value/expression describing the
2611          // variable, or if the variable wasn't mapped already.
2612          if (VMI == VariableMap.end() || VMI->second.first != Loc.Values ||
2613              VMI->second.second != Loc.Expr) {
2614            VariableMap[Key] = {Loc.Values, Loc.Expr};
2615            NewDefs.push_back(Loc);
2616            continue;
2617          }
2618  
2619          // Did not insert this Loc, which is the same as removing it.
2620          ChangedThisWedge = true;
2621          NumDefsRemoved++;
2622        }
2623  
2624        // Replace the existing wedge with the pruned version.
2625        if (ChangedThisWedge) {
2626          FnVarLocs.setWedge(WedgePosition, std::move(NewDefs));
2627          NumWedgesChanged++;
2628          Changed = true;
2629        }
2630      };
2631  
2632      for (DPValue &DPV : I.getDbgValueRange())
2633        HandleLocsForWedge(&DPV);
2634      HandleLocsForWedge(&I);
2635    }
2636  
2637    return Changed;
2638  }
2639  
2640  static bool
2641  removeUndefDbgLocsFromEntryBlock(const BasicBlock *BB,
2642                                   FunctionVarLocsBuilder &FnVarLocs) {
2643    assert(BB->isEntryBlock());
2644    // Do extra work to ensure that we remove semantically unimportant undefs.
2645    //
2646    // This is to work around the fact that SelectionDAG will hoist dbg.values
2647    // using argument values to the top of the entry block. That can move arg
2648    // dbg.values before undef and constant dbg.values which they previously
2649    // followed. The easiest thing to do is to just try to feed SelectionDAG
2650    // input it's happy with.
2651    //
2652    // Map of {Variable x: Fragments y} where the fragments y of variable x have
2653    // have at least one non-undef location defined already. Don't use directly,
2654    // instead call DefineBits and HasDefinedBits.
2655    SmallDenseMap<DebugAggregate, SmallDenseSet<DIExpression::FragmentInfo>>
2656        VarsWithDef;
2657    // Specify that V (a fragment of A) has a non-undef location.
2658    auto DefineBits = [&VarsWithDef](DebugAggregate A, DebugVariable V) {
2659      VarsWithDef[A].insert(V.getFragmentOrDefault());
2660    };
2661    // Return true if a non-undef location has been defined for V (a fragment of
2662    // A). Doesn't imply that the location is currently non-undef, just that a
2663    // non-undef location has been seen previously.
2664    auto HasDefinedBits = [&VarsWithDef](DebugAggregate A, DebugVariable V) {
2665      auto FragsIt = VarsWithDef.find(A);
2666      if (FragsIt == VarsWithDef.end())
2667        return false;
2668      return llvm::any_of(FragsIt->second, [V](auto Frag) {
2669        return DIExpression::fragmentsOverlap(Frag, V.getFragmentOrDefault());
2670      });
2671    };
2672  
2673    bool Changed = false;
2674    DenseMap<DebugVariable, std::pair<Value *, DIExpression *>> VariableMap;
2675  
2676    // Scan over the entire block, not just over the instructions mapped by
2677    // FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
2678    // instructions.
2679    for (const Instruction &I : *BB) {
2680      // Get the defs that come just before this instruction.
2681      auto HandleLocsForWedge = [&](auto *WedgePosition) {
2682        const auto *Locs = FnVarLocs.getWedge(WedgePosition);
2683        if (!Locs)
2684          return;
2685  
2686        NumWedgesScanned++;
2687        bool ChangedThisWedge = false;
2688        // The new pruned set of defs.
2689        SmallVector<VarLocInfo> NewDefs;
2690  
2691        // Iterate over the existing defs.
2692        for (const VarLocInfo &Loc : *Locs) {
2693          NumDefsScanned++;
2694          DebugAggregate Aggr{FnVarLocs.getVariable(Loc.VariableID).getVariable(),
2695                              Loc.DL.getInlinedAt()};
2696          DebugVariable Var = FnVarLocs.getVariable(Loc.VariableID);
2697  
2698          // Remove undef entries that are encountered before any non-undef
2699          // intrinsics from the entry block.
2700          if (Loc.Values.isKillLocation(Loc.Expr) && !HasDefinedBits(Aggr, Var)) {
2701            // Did not insert this Loc, which is the same as removing it.
2702            NumDefsRemoved++;
2703            ChangedThisWedge = true;
2704            continue;
2705          }
2706  
2707          DefineBits(Aggr, Var);
2708          NewDefs.push_back(Loc);
2709        }
2710  
2711        // Replace the existing wedge with the pruned version.
2712        if (ChangedThisWedge) {
2713          FnVarLocs.setWedge(WedgePosition, std::move(NewDefs));
2714          NumWedgesChanged++;
2715          Changed = true;
2716        }
2717      };
2718      for (DPValue &DPV : I.getDbgValueRange())
2719        HandleLocsForWedge(&DPV);
2720      HandleLocsForWedge(&I);
2721    }
2722  
2723    return Changed;
2724  }
2725  
2726  static bool removeRedundantDbgLocs(const BasicBlock *BB,
2727                                     FunctionVarLocsBuilder &FnVarLocs) {
2728    bool MadeChanges = false;
2729    MadeChanges |= removeRedundantDbgLocsUsingBackwardScan(BB, FnVarLocs);
2730    if (BB->isEntryBlock())
2731      MadeChanges |= removeUndefDbgLocsFromEntryBlock(BB, FnVarLocs);
2732    MadeChanges |= removeRedundantDbgLocsUsingForwardScan(BB, FnVarLocs);
2733  
2734    if (MadeChanges)
2735      LLVM_DEBUG(dbgs() << "Removed redundant dbg locs from: " << BB->getName()
2736                        << "\n");
2737    return MadeChanges;
2738  }
2739  
2740  static DenseSet<DebugAggregate> findVarsWithStackSlot(Function &Fn) {
2741    DenseSet<DebugAggregate> Result;
2742    for (auto &BB : Fn) {
2743      for (auto &I : BB) {
2744        // Any variable linked to an instruction is considered
2745        // interesting. Ideally we only need to check Allocas, however, a
2746        // DIAssignID might get dropped from an alloca but not stores. In that
2747        // case, we need to consider the variable interesting for NFC behaviour
2748        // with this change. TODO: Consider only looking at allocas.
2749        for (DbgAssignIntrinsic *DAI : at::getAssignmentMarkers(&I)) {
2750          Result.insert({DAI->getVariable(), DAI->getDebugLoc().getInlinedAt()});
2751        }
2752        for (DPValue *DPV : at::getDPVAssignmentMarkers(&I)) {
2753          Result.insert({DPV->getVariable(), DPV->getDebugLoc().getInlinedAt()});
2754        }
2755      }
2756    }
2757    return Result;
2758  }
2759  
2760  static void analyzeFunction(Function &Fn, const DataLayout &Layout,
2761                              FunctionVarLocsBuilder *FnVarLocs) {
2762    // The analysis will generate location definitions for all variables, but we
2763    // only need to perform a dataflow on the set of variables which have a stack
2764    // slot. Find those now.
2765    DenseSet<DebugAggregate> VarsWithStackSlot = findVarsWithStackSlot(Fn);
2766  
2767    bool Changed = false;
2768  
2769    // Use a scope block to clean up AssignmentTrackingLowering before running
2770    // MemLocFragmentFill to reduce peak memory consumption.
2771    {
2772      AssignmentTrackingLowering Pass(Fn, Layout, &VarsWithStackSlot);
2773      Changed = Pass.run(FnVarLocs);
2774    }
2775  
2776    if (Changed) {
2777      MemLocFragmentFill Pass(Fn, &VarsWithStackSlot,
2778                              shouldCoalesceFragments(Fn));
2779      Pass.run(FnVarLocs);
2780  
2781      // Remove redundant entries. As well as reducing memory consumption and
2782      // avoiding waiting cycles later by burning some now, this has another
2783      // important job. That is to work around some SelectionDAG quirks. See
2784      // removeRedundantDbgLocsUsingForwardScan comments for more info on that.
2785      for (auto &BB : Fn)
2786        removeRedundantDbgLocs(&BB, *FnVarLocs);
2787    }
2788  }
2789  
2790  FunctionVarLocs
2791  DebugAssignmentTrackingAnalysis::run(Function &F,
2792                                       FunctionAnalysisManager &FAM) {
2793    if (!isAssignmentTrackingEnabled(*F.getParent()))
2794      return FunctionVarLocs();
2795  
2796    auto &DL = F.getParent()->getDataLayout();
2797  
2798    FunctionVarLocsBuilder Builder;
2799    analyzeFunction(F, DL, &Builder);
2800  
2801    // Save these results.
2802    FunctionVarLocs Results;
2803    Results.init(Builder);
2804    return Results;
2805  }
2806  
2807  AnalysisKey DebugAssignmentTrackingAnalysis::Key;
2808  
2809  PreservedAnalyses
2810  DebugAssignmentTrackingPrinterPass::run(Function &F,
2811                                          FunctionAnalysisManager &FAM) {
2812    FAM.getResult<DebugAssignmentTrackingAnalysis>(F).print(OS, F);
2813    return PreservedAnalyses::all();
2814  }
2815  
2816  bool AssignmentTrackingAnalysis::runOnFunction(Function &F) {
2817    if (!isAssignmentTrackingEnabled(*F.getParent()))
2818      return false;
2819  
2820    LLVM_DEBUG(dbgs() << "AssignmentTrackingAnalysis run on " << F.getName()
2821                      << "\n");
2822    auto DL = std::make_unique<DataLayout>(F.getParent());
2823  
2824    // Clear previous results.
2825    Results->clear();
2826  
2827    FunctionVarLocsBuilder Builder;
2828    analyzeFunction(F, *DL.get(), &Builder);
2829  
2830    // Save these results.
2831    Results->init(Builder);
2832  
2833    if (PrintResults && isFunctionInPrintList(F.getName()))
2834      Results->print(errs(), F);
2835  
2836    // Return false because this pass does not modify the function.
2837    return false;
2838  }
2839  
2840  AssignmentTrackingAnalysis::AssignmentTrackingAnalysis()
2841      : FunctionPass(ID), Results(std::make_unique<FunctionVarLocs>()) {}
2842  
2843  char AssignmentTrackingAnalysis::ID = 0;
2844  
2845  INITIALIZE_PASS(AssignmentTrackingAnalysis, DEBUG_TYPE,
2846                  "Assignment Tracking Analysis", false, true)
2847