xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Transforms/IPO/Attributor.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- Attributor.h --- Module-wide attribute deduction ---------*- C++ -*-===//
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 // Attributor: An inter procedural (abstract) "attribute" deduction framework.
10 //
11 // The Attributor framework is an inter procedural abstract analysis (fixpoint
12 // iteration analysis). The goal is to allow easy deduction of new attributes as
13 // well as information exchange between abstract attributes in-flight.
14 //
15 // The Attributor class is the driver and the link between the various abstract
16 // attributes. The Attributor will iterate until a fixpoint state is reached by
17 // all abstract attributes in-flight, or until it will enforce a pessimistic fix
18 // point because an iteration limit is reached.
19 //
20 // Abstract attributes, derived from the AbstractAttribute class, actually
21 // describe properties of the code. They can correspond to actual LLVM-IR
22 // attributes, or they can be more general, ultimately unrelated to LLVM-IR
23 // attributes. The latter is useful when an abstract attributes provides
24 // information to other abstract attributes in-flight but we might not want to
25 // manifest the information. The Attributor allows to query in-flight abstract
26 // attributes through the `Attributor::getAAFor` method (see the method
27 // description for an example). If the method is used by an abstract attribute
28 // P, and it results in an abstract attribute Q, the Attributor will
29 // automatically capture a potential dependence from Q to P. This dependence
30 // will cause P to be reevaluated whenever Q changes in the future.
31 //
32 // The Attributor will only reevaluate abstract attributes that might have
33 // changed since the last iteration. That means that the Attribute will not
34 // revisit all instructions/blocks/functions in the module but only query
35 // an update from a subset of the abstract attributes.
36 //
37 // The update method `AbstractAttribute::updateImpl` is implemented by the
38 // specific "abstract attribute" subclasses. The method is invoked whenever the
39 // currently assumed state (see the AbstractState class) might not be valid
40 // anymore. This can, for example, happen if the state was dependent on another
41 // abstract attribute that changed. In every invocation, the update method has
42 // to adjust the internal state of an abstract attribute to a point that is
43 // justifiable by the underlying IR and the current state of abstract attributes
44 // in-flight. Since the IR is given and assumed to be valid, the information
45 // derived from it can be assumed to hold. However, information derived from
46 // other abstract attributes is conditional on various things. If the justifying
47 // state changed, the `updateImpl` has to revisit the situation and potentially
48 // find another justification or limit the optimistic assumes made.
49 //
50 // Change is the key in this framework. Until a state of no-change, thus a
51 // fixpoint, is reached, the Attributor will query the abstract attributes
52 // in-flight to re-evaluate their state. If the (current) state is too
53 // optimistic, hence it cannot be justified anymore through other abstract
54 // attributes or the state of the IR, the state of the abstract attribute will
55 // have to change. Generally, we assume abstract attribute state to be a finite
56 // height lattice and the update function to be monotone. However, these
57 // conditions are not enforced because the iteration limit will guarantee
58 // termination. If an optimistic fixpoint is reached, or a pessimistic fix
59 // point is enforced after a timeout, the abstract attributes are tasked to
60 // manifest their result in the IR for passes to come.
61 //
62 // Attribute manifestation is not mandatory. If desired, there is support to
63 // generate a single or multiple LLVM-IR attributes already in the helper struct
64 // IRAttribute. In the simplest case, a subclass inherits from IRAttribute with
65 // a proper Attribute::AttrKind as template parameter. The Attributor
66 // manifestation framework will then create and place a new attribute if it is
67 // allowed to do so (based on the abstract state). Other use cases can be
68 // achieved by overloading AbstractAttribute or IRAttribute methods.
69 //
70 //
71 // The "mechanics" of adding a new "abstract attribute":
72 // - Define a class (transitively) inheriting from AbstractAttribute and one
73 //   (which could be the same) that (transitively) inherits from AbstractState.
74 //   For the latter, consider the already available BooleanState and
75 //   {Inc,Dec,Bit}IntegerState if they fit your needs, e.g., you require only a
76 //   number tracking or bit-encoding.
77 // - Implement all pure methods. Also use overloading if the attribute is not
78 //   conforming with the "default" behavior: A (set of) LLVM-IR attribute(s) for
79 //   an argument, call site argument, function return value, or function. See
80 //   the class and method descriptions for more information on the two
81 //   "Abstract" classes and their respective methods.
82 // - Register opportunities for the new abstract attribute in the
83 //   `Attributor::identifyDefaultAbstractAttributes` method if it should be
84 //   counted as a 'default' attribute.
85 // - Add sufficient tests.
86 // - Add a Statistics object for bookkeeping. If it is a simple (set of)
87 //   attribute(s) manifested through the Attributor manifestation framework, see
88 //   the bookkeeping function in Attributor.cpp.
89 // - If instructions with a certain opcode are interesting to the attribute, add
90 //   that opcode to the switch in `Attributor::identifyAbstractAttributes`. This
91 //   will make it possible to query all those instructions through the
92 //   `InformationCache::getOpcodeInstMapForFunction` interface and eliminate the
93 //   need to traverse the IR repeatedly.
94 //
95 //===----------------------------------------------------------------------===//
96 
97 #ifndef LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
98 #define LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
99 
100 #include "llvm/ADT/DenseSet.h"
101 #include "llvm/ADT/GraphTraits.h"
102 #include "llvm/ADT/IntervalMap.h"
103 #include "llvm/ADT/MapVector.h"
104 #include "llvm/ADT/STLExtras.h"
105 #include "llvm/ADT/SetOperations.h"
106 #include "llvm/ADT/SetVector.h"
107 #include "llvm/ADT/SmallSet.h"
108 #include "llvm/ADT/iterator.h"
109 #include "llvm/Analysis/AssumeBundleQueries.h"
110 #include "llvm/Analysis/CFG.h"
111 #include "llvm/Analysis/CGSCCPassManager.h"
112 #include "llvm/Analysis/LazyCallGraph.h"
113 #include "llvm/Analysis/LoopInfo.h"
114 #include "llvm/Analysis/MemoryLocation.h"
115 #include "llvm/Analysis/MustExecute.h"
116 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
117 #include "llvm/Analysis/PostDominators.h"
118 #include "llvm/Analysis/TargetLibraryInfo.h"
119 #include "llvm/IR/AbstractCallSite.h"
120 #include "llvm/IR/Attributes.h"
121 #include "llvm/IR/ConstantRange.h"
122 #include "llvm/IR/Constants.h"
123 #include "llvm/IR/GlobalValue.h"
124 #include "llvm/IR/InstIterator.h"
125 #include "llvm/IR/Instruction.h"
126 #include "llvm/IR/Instructions.h"
127 #include "llvm/IR/Module.h"
128 #include "llvm/IR/PassManager.h"
129 #include "llvm/IR/Value.h"
130 #include "llvm/Support/Alignment.h"
131 #include "llvm/Support/Allocator.h"
132 #include "llvm/Support/Casting.h"
133 #include "llvm/Support/Compiler.h"
134 #include "llvm/Support/DOTGraphTraits.h"
135 #include "llvm/Support/DebugCounter.h"
136 #include "llvm/Support/ErrorHandling.h"
137 #include "llvm/Support/ModRef.h"
138 #include "llvm/Support/TimeProfiler.h"
139 #include "llvm/Support/TypeSize.h"
140 #include "llvm/TargetParser/Triple.h"
141 #include "llvm/Transforms/Utils/CallGraphUpdater.h"
142 
143 #include <limits>
144 #include <map>
145 #include <optional>
146 
147 namespace llvm {
148 
149 class DataLayout;
150 class LLVMContext;
151 class Pass;
152 template <typename Fn> class function_ref;
153 struct AADepGraphNode;
154 struct AADepGraph;
155 struct Attributor;
156 struct AbstractAttribute;
157 struct InformationCache;
158 struct AAIsDead;
159 struct AttributorCallGraph;
160 struct IRPosition;
161 
162 class Function;
163 
164 /// Abstract Attribute helper functions.
165 namespace AA {
166 using InstExclusionSetTy = SmallPtrSet<Instruction *, 4>;
167 
168 enum class GPUAddressSpace : unsigned {
169   Generic = 0,
170   Global = 1,
171   Shared = 3,
172   Constant = 4,
173   Local = 5,
174 };
175 
176 /// Return true iff \p M target a GPU (and we can use GPU AS reasoning).
177 LLVM_ABI bool isGPU(const Module &M);
178 
179 /// Flags to distinguish intra-procedural queries from *potentially*
180 /// inter-procedural queries. Not that information can be valid for both and
181 /// therefore both bits might be set.
182 enum ValueScope : uint8_t {
183   Intraprocedural = 1,
184   Interprocedural = 2,
185   AnyScope = Intraprocedural | Interprocedural,
186 };
187 
188 struct ValueAndContext : public std::pair<Value *, const Instruction *> {
189   using Base = std::pair<Value *, const Instruction *>;
ValueAndContextValueAndContext190   ValueAndContext(const Base &B) : Base(B) {}
ValueAndContextValueAndContext191   ValueAndContext(Value &V, const Instruction *CtxI) : Base(&V, CtxI) {}
ValueAndContextValueAndContext192   ValueAndContext(Value &V, const Instruction &CtxI) : Base(&V, &CtxI) {}
193 
getValueValueAndContext194   Value *getValue() const { return this->first; }
getCtxIValueAndContext195   const Instruction *getCtxI() const { return this->second; }
196 };
197 
198 /// Return true if \p I is a `nosync` instruction. Use generic reasoning and
199 /// potentially the corresponding AANoSync.
200 LLVM_ABI bool isNoSyncInst(Attributor &A, const Instruction &I,
201                            const AbstractAttribute &QueryingAA);
202 
203 /// Return true if \p V is dynamically unique, that is, there are no two
204 /// "instances" of \p V at runtime with different values.
205 /// Note: If \p ForAnalysisOnly is set we only check that the Attributor will
206 /// never use \p V to represent two "instances" not that \p V could not
207 /// technically represent them.
208 LLVM_ABI bool isDynamicallyUnique(Attributor &A,
209                                   const AbstractAttribute &QueryingAA,
210                                   const Value &V, bool ForAnalysisOnly = true);
211 
212 /// Return true if \p V is a valid value in \p Scope, that is a constant or an
213 /// instruction/argument of \p Scope.
214 LLVM_ABI bool isValidInScope(const Value &V, const Function *Scope);
215 
216 /// Return true if the value of \p VAC is a valid at the position of \p VAC,
217 /// that is a constant, an argument of the same function, or an instruction in
218 /// that function that dominates the position.
219 LLVM_ABI bool isValidAtPosition(const ValueAndContext &VAC,
220                                 InformationCache &InfoCache);
221 
222 /// Try to convert \p V to type \p Ty without introducing new instructions. If
223 /// this is not possible return `nullptr`. Note: this function basically knows
224 /// how to cast various constants.
225 LLVM_ABI Value *getWithType(Value &V, Type &Ty);
226 
227 /// Return the combination of \p A and \p B such that the result is a possible
228 /// value of both. \p B is potentially casted to match the type \p Ty or the
229 /// type of \p A if \p Ty is null.
230 ///
231 /// Examples:
232 ///        X + none  => X
233 /// not_none + undef => not_none
234 ///          V1 + V2 => nullptr
235 LLVM_ABI std::optional<Value *>
236 combineOptionalValuesInAAValueLatice(const std::optional<Value *> &A,
237                                      const std::optional<Value *> &B, Type *Ty);
238 
239 /// Helper to represent an access offset and size, with logic to deal with
240 /// uncertainty and check for overlapping accesses.
241 struct RangeTy {
242   int64_t Offset = Unassigned;
243   int64_t Size = Unassigned;
244 
RangeTyRangeTy245   RangeTy(int64_t Offset, int64_t Size) : Offset(Offset), Size(Size) {}
246   RangeTy() = default;
getUnknownRangeTy247   static RangeTy getUnknown() { return RangeTy{Unknown, Unknown}; }
248 
249   /// Return true if offset or size are unknown.
offsetOrSizeAreUnknownRangeTy250   bool offsetOrSizeAreUnknown() const {
251     return Offset == RangeTy::Unknown || Size == RangeTy::Unknown;
252   }
253 
254   /// Return true if offset and size are unknown, thus this is the default
255   /// unknown object.
offsetAndSizeAreUnknownRangeTy256   bool offsetAndSizeAreUnknown() const {
257     return Offset == RangeTy::Unknown && Size == RangeTy::Unknown;
258   }
259 
260   /// Return true if the offset and size are unassigned.
isUnassignedRangeTy261   bool isUnassigned() const {
262     assert((Offset == RangeTy::Unassigned) == (Size == RangeTy::Unassigned) &&
263            "Inconsistent state!");
264     return Offset == RangeTy::Unassigned;
265   }
266 
267   /// Return true if this offset and size pair might describe an address that
268   /// overlaps with \p Range.
mayOverlapRangeTy269   bool mayOverlap(const RangeTy &Range) const {
270     // Any unknown value and we are giving up -> overlap.
271     if (offsetOrSizeAreUnknown() || Range.offsetOrSizeAreUnknown())
272       return true;
273 
274     // Check if one offset point is in the other interval [offset,
275     // offset+size].
276     return Range.Offset + Range.Size > Offset && Range.Offset < Offset + Size;
277   }
278 
279   RangeTy &operator&=(const RangeTy &R) {
280     if (R.isUnassigned())
281       return *this;
282     if (isUnassigned())
283       return *this = R;
284     if (Offset == Unknown || R.Offset == Unknown)
285       Offset = Unknown;
286     if (Size == Unknown || R.Size == Unknown)
287       Size = Unknown;
288     if (offsetAndSizeAreUnknown())
289       return *this;
290     if (Offset == Unknown) {
291       Size = std::max(Size, R.Size);
292     } else if (Size == Unknown) {
293       Offset = std::min(Offset, R.Offset);
294     } else {
295       Offset = std::min(Offset, R.Offset);
296       Size = std::max(Offset + Size, R.Offset + R.Size) - Offset;
297     }
298     return *this;
299   }
300 
301   /// Comparison for sorting ranges.
302   ///
303   /// Returns true if the offset of \p L is less than that of \p R. If the two
304   /// offsets are same, compare the sizes instead.
LessThanRangeTy305   inline static bool LessThan(const RangeTy &L, const RangeTy &R) {
306     if (L.Offset < R.Offset)
307       return true;
308     if (L.Offset == R.Offset)
309       return L.Size < R.Size;
310     return false;
311   }
312 
313   /// Constants used to represent special offsets or sizes.
314   /// - We cannot assume that Offsets and Size are non-negative.
315   /// - The constants should not clash with DenseMapInfo, such as EmptyKey
316   ///   (INT64_MAX) and TombstoneKey (INT64_MIN).
317   /// We use values "in the middle" of the 64 bit range to represent these
318   /// special cases.
319   static constexpr int64_t Unassigned = std::numeric_limits<int32_t>::min();
320   static constexpr int64_t Unknown = std::numeric_limits<int32_t>::max();
321 };
322 
323 inline raw_ostream &operator<<(raw_ostream &OS, const RangeTy &R) {
324   OS << "[" << R.Offset << ", " << R.Size << "]";
325   return OS;
326 }
327 
328 inline bool operator==(const RangeTy &A, const RangeTy &B) {
329   return A.Offset == B.Offset && A.Size == B.Size;
330 }
331 
332 inline bool operator!=(const RangeTy &A, const RangeTy &B) { return !(A == B); }
333 
334 /// Return the initial value of \p Obj with type \p Ty if that is a constant.
335 LLVM_ABI Constant *
336 getInitialValueForObj(Attributor &A, const AbstractAttribute &QueryingAA,
337                       Value &Obj, Type &Ty, const TargetLibraryInfo *TLI,
338                       const DataLayout &DL, RangeTy *RangePtr = nullptr);
339 
340 /// Collect all potential values \p LI could read into \p PotentialValues. That
341 /// is, the only values read by \p LI are assumed to be known and all are in
342 /// \p PotentialValues. \p PotentialValueOrigins will contain all the
343 /// instructions that might have put a potential value into \p PotentialValues.
344 /// Dependences onto \p QueryingAA are properly tracked, \p
345 /// UsedAssumedInformation will inform the caller if assumed information was
346 /// used.
347 ///
348 /// \returns True if the assumed potential copies are all in \p PotentialValues,
349 ///          false if something went wrong and the copies could not be
350 ///          determined.
351 LLVM_ABI bool getPotentiallyLoadedValues(
352     Attributor &A, LoadInst &LI, SmallSetVector<Value *, 4> &PotentialValues,
353     SmallSetVector<Instruction *, 4> &PotentialValueOrigins,
354     const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
355     bool OnlyExact = false);
356 
357 /// Collect all potential values of the one stored by \p SI into
358 /// \p PotentialCopies. That is, the only copies that were made via the
359 /// store are assumed to be known and all are in \p PotentialCopies. Dependences
360 /// onto \p QueryingAA are properly tracked, \p UsedAssumedInformation will
361 /// inform the caller if assumed information was used.
362 ///
363 /// \returns True if the assumed potential copies are all in \p PotentialCopies,
364 ///          false if something went wrong and the copies could not be
365 ///          determined.
366 LLVM_ABI bool getPotentialCopiesOfStoredValue(
367     Attributor &A, StoreInst &SI, SmallSetVector<Value *, 4> &PotentialCopies,
368     const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
369     bool OnlyExact = false);
370 
371 /// Return true if \p IRP is readonly. This will query respective AAs that
372 /// deduce the information and introduce dependences for \p QueryingAA.
373 LLVM_ABI bool isAssumedReadOnly(Attributor &A, const IRPosition &IRP,
374                                 const AbstractAttribute &QueryingAA,
375                                 bool &IsKnown);
376 
377 /// Return true if \p IRP is readnone. This will query respective AAs that
378 /// deduce the information and introduce dependences for \p QueryingAA.
379 LLVM_ABI bool isAssumedReadNone(Attributor &A, const IRPosition &IRP,
380                                 const AbstractAttribute &QueryingAA,
381                                 bool &IsKnown);
382 
383 /// Return true if \p ToI is potentially reachable from \p FromI without running
384 /// into any instruction in \p ExclusionSet The two instructions do not need to
385 /// be in the same function. \p GoBackwardsCB can be provided to convey domain
386 /// knowledge about the "lifespan" the user is interested in. By default, the
387 /// callers of \p FromI are checked as well to determine if \p ToI can be
388 /// reached. If the query is not interested in callers beyond a certain point,
389 /// e.g., a GPU kernel entry or the function containing an alloca, the
390 /// \p GoBackwardsCB should return false.
391 LLVM_ABI bool isPotentiallyReachable(
392     Attributor &A, const Instruction &FromI, const Instruction &ToI,
393     const AbstractAttribute &QueryingAA,
394     const AA::InstExclusionSetTy *ExclusionSet = nullptr,
395     std::function<bool(const Function &F)> GoBackwardsCB = nullptr);
396 
397 /// Same as above but it is sufficient to reach any instruction in \p ToFn.
398 LLVM_ABI bool isPotentiallyReachable(
399     Attributor &A, const Instruction &FromI, const Function &ToFn,
400     const AbstractAttribute &QueryingAA,
401     const AA::InstExclusionSetTy *ExclusionSet = nullptr,
402     std::function<bool(const Function &F)> GoBackwardsCB = nullptr);
403 
404 /// Return true if \p Obj is assumed to be a thread local object.
405 LLVM_ABI bool isAssumedThreadLocalObject(Attributor &A, Value &Obj,
406                                          const AbstractAttribute &QueryingAA);
407 
408 /// Return true if \p I is potentially affected by a barrier.
409 LLVM_ABI bool
410 isPotentiallyAffectedByBarrier(Attributor &A, const Instruction &I,
411                                const AbstractAttribute &QueryingAA);
412 LLVM_ABI bool
413 isPotentiallyAffectedByBarrier(Attributor &A, ArrayRef<const Value *> Ptrs,
414                                const AbstractAttribute &QueryingAA,
415                                const Instruction *CtxI);
416 } // namespace AA
417 
418 template <>
419 struct DenseMapInfo<AA::ValueAndContext>
420     : public DenseMapInfo<AA::ValueAndContext::Base> {
421   using Base = DenseMapInfo<AA::ValueAndContext::Base>;
422   static inline AA::ValueAndContext getEmptyKey() {
423     return Base::getEmptyKey();
424   }
425   static inline AA::ValueAndContext getTombstoneKey() {
426     return Base::getTombstoneKey();
427   }
428   static unsigned getHashValue(const AA::ValueAndContext &VAC) {
429     return Base::getHashValue(VAC);
430   }
431 
432   static bool isEqual(const AA::ValueAndContext &LHS,
433                       const AA::ValueAndContext &RHS) {
434     return Base::isEqual(LHS, RHS);
435   }
436 };
437 
438 template <>
439 struct DenseMapInfo<AA::ValueScope> : public DenseMapInfo<unsigned char> {
440   using Base = DenseMapInfo<unsigned char>;
441   static inline AA::ValueScope getEmptyKey() {
442     return AA::ValueScope(Base::getEmptyKey());
443   }
444   static inline AA::ValueScope getTombstoneKey() {
445     return AA::ValueScope(Base::getTombstoneKey());
446   }
447   static unsigned getHashValue(const AA::ValueScope &S) {
448     return Base::getHashValue(S);
449   }
450 
451   static bool isEqual(const AA::ValueScope &LHS, const AA::ValueScope &RHS) {
452     return Base::isEqual(LHS, RHS);
453   }
454 };
455 
456 template <>
457 struct DenseMapInfo<const AA::InstExclusionSetTy *>
458     : public DenseMapInfo<void *> {
459   using super = DenseMapInfo<void *>;
460   static inline const AA::InstExclusionSetTy *getEmptyKey() {
461     return static_cast<const AA::InstExclusionSetTy *>(super::getEmptyKey());
462   }
463   static inline const AA::InstExclusionSetTy *getTombstoneKey() {
464     return static_cast<const AA::InstExclusionSetTy *>(
465         super::getTombstoneKey());
466   }
467   static unsigned getHashValue(const AA::InstExclusionSetTy *BES) {
468     unsigned H = 0;
469     if (BES)
470       for (const auto *II : *BES)
471         H += DenseMapInfo<const Instruction *>::getHashValue(II);
472     return H;
473   }
474   static bool isEqual(const AA::InstExclusionSetTy *LHS,
475                       const AA::InstExclusionSetTy *RHS) {
476     if (LHS == RHS)
477       return true;
478     if (LHS == getEmptyKey() || RHS == getEmptyKey() ||
479         LHS == getTombstoneKey() || RHS == getTombstoneKey())
480       return false;
481     auto SizeLHS = LHS ? LHS->size() : 0;
482     auto SizeRHS = RHS ? RHS->size() : 0;
483     if (SizeLHS != SizeRHS)
484       return false;
485     if (SizeRHS == 0)
486       return true;
487     return llvm::set_is_subset(*LHS, *RHS);
488   }
489 };
490 
491 /// The value passed to the line option that defines the maximal initialization
492 /// chain length.
493 LLVM_ABI extern unsigned MaxInitializationChainLength;
494 
495 ///{
496 enum class ChangeStatus {
497   CHANGED,
498   UNCHANGED,
499 };
500 
501 LLVM_ABI ChangeStatus operator|(ChangeStatus l, ChangeStatus r);
502 LLVM_ABI ChangeStatus &operator|=(ChangeStatus &l, ChangeStatus r);
503 LLVM_ABI ChangeStatus operator&(ChangeStatus l, ChangeStatus r);
504 LLVM_ABI ChangeStatus &operator&=(ChangeStatus &l, ChangeStatus r);
505 
506 enum class DepClassTy {
507   REQUIRED, ///< The target cannot be valid if the source is not.
508   OPTIONAL, ///< The target may be valid if the source is not.
509   NONE,     ///< Do not track a dependence between source and target.
510 };
511 ///}
512 
513 /// The data structure for the nodes of a dependency graph
514 struct AADepGraphNode {
515 public:
516   virtual ~AADepGraphNode() = default;
517   using DepTy = PointerIntPair<AADepGraphNode *, 1>;
518   using DepSetTy = SmallSetVector<DepTy, 2>;
519 
520 protected:
521   /// Set of dependency graph nodes which should be updated if this one
522   /// is updated. The bit encodes if it is optional.
523   DepSetTy Deps;
524 
525   static AADepGraphNode *DepGetVal(const DepTy &DT) { return DT.getPointer(); }
526   static AbstractAttribute *DepGetValAA(const DepTy &DT) {
527     return cast<AbstractAttribute>(DT.getPointer());
528   }
529 
530   operator AbstractAttribute *() { return cast<AbstractAttribute>(this); }
531 
532 public:
533   using iterator = mapped_iterator<DepSetTy::iterator, decltype(&DepGetVal)>;
534   using aaiterator =
535       mapped_iterator<DepSetTy::iterator, decltype(&DepGetValAA)>;
536 
537   aaiterator begin() { return aaiterator(Deps.begin(), &DepGetValAA); }
538   aaiterator end() { return aaiterator(Deps.end(), &DepGetValAA); }
539   iterator child_begin() { return iterator(Deps.begin(), &DepGetVal); }
540   iterator child_end() { return iterator(Deps.end(), &DepGetVal); }
541 
542   void print(raw_ostream &OS) const { print(nullptr, OS); }
543   virtual void print(Attributor *, raw_ostream &OS) const {
544     OS << "AADepNode Impl\n";
545   }
546   DepSetTy &getDeps() { return Deps; }
547 
548   friend struct Attributor;
549   friend struct AADepGraph;
550 };
551 
552 /// The data structure for the dependency graph
553 ///
554 /// Note that in this graph if there is an edge from A to B (A -> B),
555 /// then it means that B depends on A, and when the state of A is
556 /// updated, node B should also be updated
557 struct AADepGraph {
558   AADepGraph() = default;
559   ~AADepGraph() = default;
560 
561   using DepTy = AADepGraphNode::DepTy;
562   static AADepGraphNode *DepGetVal(const DepTy &DT) { return DT.getPointer(); }
563   using iterator =
564       mapped_iterator<AADepGraphNode::DepSetTy::iterator, decltype(&DepGetVal)>;
565 
566   /// There is no root node for the dependency graph. But the SCCIterator
567   /// requires a single entry point, so we maintain a fake("synthetic") root
568   /// node that depends on every node.
569   AADepGraphNode SyntheticRoot;
570   AADepGraphNode *GetEntryNode() { return &SyntheticRoot; }
571 
572   iterator begin() { return SyntheticRoot.child_begin(); }
573   iterator end() { return SyntheticRoot.child_end(); }
574 
575   LLVM_ABI void viewGraph();
576 
577   /// Dump graph to file
578   LLVM_ABI void dumpGraph();
579 
580   /// Print dependency graph
581   LLVM_ABI void print();
582 };
583 
584 /// Helper to describe and deal with positions in the LLVM-IR.
585 ///
586 /// A position in the IR is described by an anchor value and an "offset" that
587 /// could be the argument number, for call sites and arguments, or an indicator
588 /// of the "position kind". The kinds, specified in the Kind enum below, include
589 /// the locations in the attribute list, i.a., function scope and return value,
590 /// as well as a distinction between call sites and functions. Finally, there
591 /// are floating values that do not have a corresponding attribute list
592 /// position.
593 struct IRPosition {
594   // NOTE: In the future this definition can be changed to support recursive
595   // functions.
596   using CallBaseContext = CallBase;
597 
598   /// The positions we distinguish in the IR.
599   enum Kind : char {
600     IRP_INVALID,  ///< An invalid position.
601     IRP_FLOAT,    ///< A position that is not associated with a spot suitable
602                   ///< for attributes. This could be any value or instruction.
603     IRP_RETURNED, ///< An attribute for the function return value.
604     IRP_CALL_SITE_RETURNED, ///< An attribute for a call site return value.
605     IRP_FUNCTION,           ///< An attribute for a function (scope).
606     IRP_CALL_SITE,          ///< An attribute for a call site (function scope).
607     IRP_ARGUMENT,           ///< An attribute for a function argument.
608     IRP_CALL_SITE_ARGUMENT, ///< An attribute for a call site argument.
609   };
610 
611   /// Default constructor available to create invalid positions implicitly. All
612   /// other positions need to be created explicitly through the appropriate
613   /// static member function.
614   IRPosition() : Enc(nullptr, ENC_VALUE) { verify(); }
615 
616   /// Create a position describing the value of \p V.
617   static const IRPosition value(const Value &V,
618                                 const CallBaseContext *CBContext = nullptr) {
619     if (auto *Arg = dyn_cast<Argument>(&V))
620       return IRPosition::argument(*Arg, CBContext);
621     if (auto *CB = dyn_cast<CallBase>(&V))
622       return IRPosition::callsite_returned(*CB);
623     return IRPosition(const_cast<Value &>(V), IRP_FLOAT, CBContext);
624   }
625 
626   /// Create a position describing the instruction \p I. This is different from
627   /// the value version because call sites are treated as intrusctions rather
628   /// than their return value in this function.
629   static const IRPosition inst(const Instruction &I,
630                                const CallBaseContext *CBContext = nullptr) {
631     return IRPosition(const_cast<Instruction &>(I), IRP_FLOAT, CBContext);
632   }
633 
634   /// Create a position describing the function scope of \p F.
635   /// \p CBContext is used for call base specific analysis.
636   static const IRPosition function(const Function &F,
637                                    const CallBaseContext *CBContext = nullptr) {
638     return IRPosition(const_cast<Function &>(F), IRP_FUNCTION, CBContext);
639   }
640 
641   /// Create a position describing the returned value of \p F.
642   /// \p CBContext is used for call base specific analysis.
643   static const IRPosition returned(const Function &F,
644                                    const CallBaseContext *CBContext = nullptr) {
645     return IRPosition(const_cast<Function &>(F), IRP_RETURNED, CBContext);
646   }
647 
648   /// Create a position describing the argument \p Arg.
649   /// \p CBContext is used for call base specific analysis.
650   static const IRPosition argument(const Argument &Arg,
651                                    const CallBaseContext *CBContext = nullptr) {
652     return IRPosition(const_cast<Argument &>(Arg), IRP_ARGUMENT, CBContext);
653   }
654 
655   /// Create a position describing the function scope of \p CB.
656   static const IRPosition callsite_function(const CallBase &CB) {
657     return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE);
658   }
659 
660   /// Create a position describing the returned value of \p CB.
661   static const IRPosition callsite_returned(const CallBase &CB) {
662     return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE_RETURNED);
663   }
664 
665   /// Create a position describing the argument of \p CB at position \p ArgNo.
666   static const IRPosition callsite_argument(const CallBase &CB,
667                                             unsigned ArgNo) {
668     return IRPosition(const_cast<Use &>(CB.getArgOperandUse(ArgNo)),
669                       IRP_CALL_SITE_ARGUMENT);
670   }
671 
672   /// Create a position describing the argument of \p ACS at position \p ArgNo.
673   static const IRPosition callsite_argument(AbstractCallSite ACS,
674                                             unsigned ArgNo) {
675     if (ACS.getNumArgOperands() <= ArgNo)
676       return IRPosition();
677     int CSArgNo = ACS.getCallArgOperandNo(ArgNo);
678     if (CSArgNo >= 0)
679       return IRPosition::callsite_argument(
680           cast<CallBase>(*ACS.getInstruction()), CSArgNo);
681     return IRPosition();
682   }
683 
684   /// Create a position with function scope matching the "context" of \p IRP.
685   /// If \p IRP is a call site (see isAnyCallSitePosition()) then the result
686   /// will be a call site position, otherwise the function position of the
687   /// associated function.
688   static const IRPosition
689   function_scope(const IRPosition &IRP,
690                  const CallBaseContext *CBContext = nullptr) {
691     if (IRP.isAnyCallSitePosition()) {
692       return IRPosition::callsite_function(
693           cast<CallBase>(IRP.getAnchorValue()));
694     }
695     assert(IRP.getAssociatedFunction());
696     return IRPosition::function(*IRP.getAssociatedFunction(), CBContext);
697   }
698 
699   bool operator==(const IRPosition &RHS) const {
700     return Enc == RHS.Enc && RHS.CBContext == CBContext;
701   }
702   bool operator!=(const IRPosition &RHS) const { return !(*this == RHS); }
703 
704   /// Return the value this abstract attribute is anchored with.
705   ///
706   /// The anchor value might not be the associated value if the latter is not
707   /// sufficient to determine where arguments will be manifested. This is, so
708   /// far, only the case for call site arguments as the value is not sufficient
709   /// to pinpoint them. Instead, we can use the call site as an anchor.
710   Value &getAnchorValue() const {
711     switch (getEncodingBits()) {
712     case ENC_VALUE:
713     case ENC_RETURNED_VALUE:
714     case ENC_FLOATING_FUNCTION:
715       return *getAsValuePtr();
716     case ENC_CALL_SITE_ARGUMENT_USE:
717       return *(getAsUsePtr()->getUser());
718     default:
719       llvm_unreachable("Unkown encoding!");
720     };
721   }
722 
723   /// Return the associated function, if any.
724   Function *getAssociatedFunction() const {
725     if (auto *CB = dyn_cast<CallBase>(&getAnchorValue())) {
726       // We reuse the logic that associates callback calles to arguments of a
727       // call site here to identify the callback callee as the associated
728       // function.
729       if (Argument *Arg = getAssociatedArgument())
730         return Arg->getParent();
731       return dyn_cast_if_present<Function>(
732           CB->getCalledOperand()->stripPointerCasts());
733     }
734     return getAnchorScope();
735   }
736 
737   /// Return the associated argument, if any.
738   LLVM_ABI Argument *getAssociatedArgument() const;
739 
740   /// Return true if the position refers to a function interface, that is the
741   /// function scope, the function return, or an argument.
742   bool isFnInterfaceKind() const {
743     switch (getPositionKind()) {
744     case IRPosition::IRP_FUNCTION:
745     case IRPosition::IRP_RETURNED:
746     case IRPosition::IRP_ARGUMENT:
747       return true;
748     default:
749       return false;
750     }
751   }
752 
753   /// Return true if this is a function or call site position.
754   bool isFunctionScope() const {
755     switch (getPositionKind()) {
756     case IRPosition::IRP_CALL_SITE:
757     case IRPosition::IRP_FUNCTION:
758       return true;
759     default:
760       return false;
761     };
762   }
763 
764   /// Return the Function surrounding the anchor value.
765   Function *getAnchorScope() const {
766     Value &V = getAnchorValue();
767     if (isa<Function>(V))
768       return &cast<Function>(V);
769     if (isa<Argument>(V))
770       return cast<Argument>(V).getParent();
771     if (isa<Instruction>(V))
772       return cast<Instruction>(V).getFunction();
773     return nullptr;
774   }
775 
776   /// Return the context instruction, if any.
777   Instruction *getCtxI() const {
778     Value &V = getAnchorValue();
779     if (auto *I = dyn_cast<Instruction>(&V))
780       return I;
781     if (auto *Arg = dyn_cast<Argument>(&V))
782       if (!Arg->getParent()->isDeclaration())
783         return &Arg->getParent()->getEntryBlock().front();
784     if (auto *F = dyn_cast<Function>(&V))
785       if (!F->isDeclaration())
786         return &(F->getEntryBlock().front());
787     return nullptr;
788   }
789 
790   /// Return the value this abstract attribute is associated with.
791   Value &getAssociatedValue() const {
792     if (getCallSiteArgNo() < 0 || isa<Argument>(&getAnchorValue()))
793       return getAnchorValue();
794     assert(isa<CallBase>(&getAnchorValue()) && "Expected a call base!");
795     return *cast<CallBase>(&getAnchorValue())
796                 ->getArgOperand(getCallSiteArgNo());
797   }
798 
799   /// Return the type this abstract attribute is associated with.
800   Type *getAssociatedType() const {
801     if (getPositionKind() == IRPosition::IRP_RETURNED)
802       return getAssociatedFunction()->getReturnType();
803     return getAssociatedValue().getType();
804   }
805 
806   /// Return the callee argument number of the associated value if it is an
807   /// argument or call site argument, otherwise a negative value. In contrast to
808   /// `getCallSiteArgNo` this method will always return the "argument number"
809   /// from the perspective of the callee. This may not the same as the call site
810   /// if this is a callback call.
811   int getCalleeArgNo() const {
812     return getArgNo(/* CallbackCalleeArgIfApplicable */ true);
813   }
814 
815   /// Return the call site argument number of the associated value if it is an
816   /// argument or call site argument, otherwise a negative value. In contrast to
817   /// `getCalleArgNo` this method will always return the "operand number" from
818   /// the perspective of the call site. This may not the same as the callee
819   /// perspective if this is a callback call.
820   int getCallSiteArgNo() const {
821     return getArgNo(/* CallbackCalleeArgIfApplicable */ false);
822   }
823 
824   /// Return the index in the attribute list for this position.
825   unsigned getAttrIdx() const {
826     switch (getPositionKind()) {
827     case IRPosition::IRP_INVALID:
828     case IRPosition::IRP_FLOAT:
829       break;
830     case IRPosition::IRP_FUNCTION:
831     case IRPosition::IRP_CALL_SITE:
832       return AttributeList::FunctionIndex;
833     case IRPosition::IRP_RETURNED:
834     case IRPosition::IRP_CALL_SITE_RETURNED:
835       return AttributeList::ReturnIndex;
836     case IRPosition::IRP_ARGUMENT:
837       return getCalleeArgNo() + AttributeList::FirstArgIndex;
838     case IRPosition::IRP_CALL_SITE_ARGUMENT:
839       return getCallSiteArgNo() + AttributeList::FirstArgIndex;
840     }
841     llvm_unreachable(
842         "There is no attribute index for a floating or invalid position!");
843   }
844 
845   /// Return the value attributes are attached to.
846   Value *getAttrListAnchor() const {
847     if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
848       return CB;
849     return getAssociatedFunction();
850   }
851 
852   /// Return the attributes associated with this function or call site scope.
853   AttributeList getAttrList() const {
854     if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
855       return CB->getAttributes();
856     return getAssociatedFunction()->getAttributes();
857   }
858 
859   /// Update the attributes associated with this function or call site scope.
860   void setAttrList(const AttributeList &AttrList) const {
861     if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
862       return CB->setAttributes(AttrList);
863     return getAssociatedFunction()->setAttributes(AttrList);
864   }
865 
866   /// Return the number of arguments associated with this function or call site
867   /// scope.
868   unsigned getNumArgs() const {
869     assert((getPositionKind() == IRP_CALL_SITE ||
870             getPositionKind() == IRP_FUNCTION) &&
871            "Only valid for function/call site positions!");
872     if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
873       return CB->arg_size();
874     return getAssociatedFunction()->arg_size();
875   }
876 
877   /// Return theargument \p ArgNo associated with this function or call site
878   /// scope.
879   Value *getArg(unsigned ArgNo) const {
880     assert((getPositionKind() == IRP_CALL_SITE ||
881             getPositionKind() == IRP_FUNCTION) &&
882            "Only valid for function/call site positions!");
883     if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
884       return CB->getArgOperand(ArgNo);
885     return getAssociatedFunction()->getArg(ArgNo);
886   }
887 
888   /// Return the associated position kind.
889   Kind getPositionKind() const {
890     char EncodingBits = getEncodingBits();
891     if (EncodingBits == ENC_CALL_SITE_ARGUMENT_USE)
892       return IRP_CALL_SITE_ARGUMENT;
893     if (EncodingBits == ENC_FLOATING_FUNCTION)
894       return IRP_FLOAT;
895 
896     Value *V = getAsValuePtr();
897     if (!V)
898       return IRP_INVALID;
899     if (isa<Argument>(V))
900       return IRP_ARGUMENT;
901     if (isa<Function>(V))
902       return isReturnPosition(EncodingBits) ? IRP_RETURNED : IRP_FUNCTION;
903     if (isa<CallBase>(V))
904       return isReturnPosition(EncodingBits) ? IRP_CALL_SITE_RETURNED
905                                             : IRP_CALL_SITE;
906     return IRP_FLOAT;
907   }
908 
909   bool isAnyCallSitePosition() const {
910     switch (getPositionKind()) {
911     case IRPosition::IRP_CALL_SITE:
912     case IRPosition::IRP_CALL_SITE_RETURNED:
913     case IRPosition::IRP_CALL_SITE_ARGUMENT:
914       return true;
915     default:
916       return false;
917     }
918   }
919 
920   /// Return true if the position is an argument or call site argument.
921   bool isArgumentPosition() const {
922     switch (getPositionKind()) {
923     case IRPosition::IRP_ARGUMENT:
924     case IRPosition::IRP_CALL_SITE_ARGUMENT:
925       return true;
926     default:
927       return false;
928     }
929   }
930 
931   /// Return the same position without the call base context.
932   IRPosition stripCallBaseContext() const {
933     IRPosition Result = *this;
934     Result.CBContext = nullptr;
935     return Result;
936   }
937 
938   /// Get the call base context from the position.
939   const CallBaseContext *getCallBaseContext() const { return CBContext; }
940 
941   /// Check if the position has any call base context.
942   bool hasCallBaseContext() const { return CBContext != nullptr; }
943 
944   /// Special DenseMap key values.
945   ///
946   ///{
947   LLVM_ABI static const IRPosition EmptyKey;
948   LLVM_ABI static const IRPosition TombstoneKey;
949   ///}
950 
951   /// Conversion into a void * to allow reuse of pointer hashing.
952   operator void *() const { return Enc.getOpaqueValue(); }
953 
954 private:
955   /// Private constructor for special values only!
956   explicit IRPosition(void *Ptr, const CallBaseContext *CBContext = nullptr)
957       : CBContext(CBContext) {
958     Enc.setFromOpaqueValue(Ptr);
959   }
960 
961   /// IRPosition anchored at \p AnchorVal with kind/argument numbet \p PK.
962   explicit IRPosition(Value &AnchorVal, Kind PK,
963                       const CallBaseContext *CBContext = nullptr)
964       : CBContext(CBContext) {
965     switch (PK) {
966     case IRPosition::IRP_INVALID:
967       llvm_unreachable("Cannot create invalid IRP with an anchor value!");
968       break;
969     case IRPosition::IRP_FLOAT:
970       // Special case for floating functions.
971       if (isa<Function>(AnchorVal) || isa<CallBase>(AnchorVal))
972         Enc = {&AnchorVal, ENC_FLOATING_FUNCTION};
973       else
974         Enc = {&AnchorVal, ENC_VALUE};
975       break;
976     case IRPosition::IRP_FUNCTION:
977     case IRPosition::IRP_CALL_SITE:
978       Enc = {&AnchorVal, ENC_VALUE};
979       break;
980     case IRPosition::IRP_RETURNED:
981     case IRPosition::IRP_CALL_SITE_RETURNED:
982       Enc = {&AnchorVal, ENC_RETURNED_VALUE};
983       break;
984     case IRPosition::IRP_ARGUMENT:
985       Enc = {&AnchorVal, ENC_VALUE};
986       break;
987     case IRPosition::IRP_CALL_SITE_ARGUMENT:
988       llvm_unreachable(
989           "Cannot create call site argument IRP with an anchor value!");
990       break;
991     }
992     verify();
993   }
994 
995   /// Return the callee argument number of the associated value if it is an
996   /// argument or call site argument. See also `getCalleeArgNo` and
997   /// `getCallSiteArgNo`.
998   int getArgNo(bool CallbackCalleeArgIfApplicable) const {
999     if (CallbackCalleeArgIfApplicable)
1000       if (Argument *Arg = getAssociatedArgument())
1001         return Arg->getArgNo();
1002     switch (getPositionKind()) {
1003     case IRPosition::IRP_ARGUMENT:
1004       return cast<Argument>(getAsValuePtr())->getArgNo();
1005     case IRPosition::IRP_CALL_SITE_ARGUMENT: {
1006       Use &U = *getAsUsePtr();
1007       return cast<CallBase>(U.getUser())->getArgOperandNo(&U);
1008     }
1009     default:
1010       return -1;
1011     }
1012   }
1013 
1014   /// IRPosition for the use \p U. The position kind \p PK needs to be
1015   /// IRP_CALL_SITE_ARGUMENT, the anchor value is the user, the associated value
1016   /// the used value.
1017   explicit IRPosition(Use &U, Kind PK) {
1018     assert(PK == IRP_CALL_SITE_ARGUMENT &&
1019            "Use constructor is for call site arguments only!");
1020     Enc = {&U, ENC_CALL_SITE_ARGUMENT_USE};
1021     verify();
1022   }
1023 
1024   /// Verify internal invariants.
1025   LLVM_ABI void verify();
1026 
1027   /// Return the underlying pointer as Value *, valid for all positions but
1028   /// IRP_CALL_SITE_ARGUMENT.
1029   Value *getAsValuePtr() const {
1030     assert(getEncodingBits() != ENC_CALL_SITE_ARGUMENT_USE &&
1031            "Not a value pointer!");
1032     return reinterpret_cast<Value *>(Enc.getPointer());
1033   }
1034 
1035   /// Return the underlying pointer as Use *, valid only for
1036   /// IRP_CALL_SITE_ARGUMENT positions.
1037   Use *getAsUsePtr() const {
1038     assert(getEncodingBits() == ENC_CALL_SITE_ARGUMENT_USE &&
1039            "Not a value pointer!");
1040     return reinterpret_cast<Use *>(Enc.getPointer());
1041   }
1042 
1043   /// Return true if \p EncodingBits describe a returned or call site returned
1044   /// position.
1045   static bool isReturnPosition(char EncodingBits) {
1046     return EncodingBits == ENC_RETURNED_VALUE;
1047   }
1048 
1049   /// Return true if the encoding bits describe a returned or call site returned
1050   /// position.
1051   bool isReturnPosition() const { return isReturnPosition(getEncodingBits()); }
1052 
1053   /// The encoding of the IRPosition is a combination of a pointer and two
1054   /// encoding bits. The values of the encoding bits are defined in the enum
1055   /// below. The pointer is either a Value* (for the first three encoding bit
1056   /// combinations) or Use* (for ENC_CALL_SITE_ARGUMENT_USE).
1057   ///
1058   ///{
1059   enum {
1060     ENC_VALUE = 0b00,
1061     ENC_RETURNED_VALUE = 0b01,
1062     ENC_FLOATING_FUNCTION = 0b10,
1063     ENC_CALL_SITE_ARGUMENT_USE = 0b11,
1064   };
1065 
1066   // Reserve the maximal amount of bits so there is no need to mask out the
1067   // remaining ones. We will not encode anything else in the pointer anyway.
1068   static constexpr int NumEncodingBits =
1069       PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
1070   static_assert(NumEncodingBits >= 2, "At least two bits are required!");
1071 
1072   /// The pointer with the encoding bits.
1073   PointerIntPair<void *, NumEncodingBits, char> Enc;
1074   ///}
1075 
1076   /// Call base context. Used for callsite specific analysis.
1077   const CallBaseContext *CBContext = nullptr;
1078 
1079   /// Return the encoding bits.
1080   char getEncodingBits() const { return Enc.getInt(); }
1081 };
1082 
1083 /// Helper that allows IRPosition as a key in a DenseMap.
1084 template <> struct DenseMapInfo<IRPosition> {
1085   static inline IRPosition getEmptyKey() { return IRPosition::EmptyKey; }
1086   static inline IRPosition getTombstoneKey() {
1087     return IRPosition::TombstoneKey;
1088   }
1089   static unsigned getHashValue(const IRPosition &IRP) {
1090     return (DenseMapInfo<void *>::getHashValue(IRP) << 4) ^
1091            (DenseMapInfo<Value *>::getHashValue(IRP.getCallBaseContext()));
1092   }
1093 
1094   static bool isEqual(const IRPosition &a, const IRPosition &b) {
1095     return a == b;
1096   }
1097 };
1098 
1099 /// A visitor class for IR positions.
1100 ///
1101 /// Given a position P, the SubsumingPositionIterator allows to visit "subsuming
1102 /// positions" wrt. attributes/information. Thus, if a piece of information
1103 /// holds for a subsuming position, it also holds for the position P.
1104 ///
1105 /// The subsuming positions always include the initial position and then,
1106 /// depending on the position kind, additionally the following ones:
1107 /// - for IRP_RETURNED:
1108 ///   - the function (IRP_FUNCTION)
1109 /// - for IRP_ARGUMENT:
1110 ///   - the function (IRP_FUNCTION)
1111 /// - for IRP_CALL_SITE:
1112 ///   - the callee (IRP_FUNCTION), if known
1113 /// - for IRP_CALL_SITE_RETURNED:
1114 ///   - the callee (IRP_RETURNED), if known
1115 ///   - the call site (IRP_FUNCTION)
1116 ///   - the callee (IRP_FUNCTION), if known
1117 /// - for IRP_CALL_SITE_ARGUMENT:
1118 ///   - the argument of the callee (IRP_ARGUMENT), if known
1119 ///   - the callee (IRP_FUNCTION), if known
1120 ///   - the position the call site argument is associated with if it is not
1121 ///     anchored to the call site, e.g., if it is an argument then the argument
1122 ///     (IRP_ARGUMENT)
1123 class SubsumingPositionIterator {
1124   SmallVector<IRPosition, 4> IRPositions;
1125   using iterator = decltype(IRPositions)::iterator;
1126 
1127 public:
1128   LLVM_ABI SubsumingPositionIterator(const IRPosition &IRP);
1129   iterator begin() { return IRPositions.begin(); }
1130   iterator end() { return IRPositions.end(); }
1131 };
1132 
1133 /// Wrapper for FunctionAnalysisManager.
1134 struct AnalysisGetter {
1135   // The client may be running the old pass manager, in which case, we need to
1136   // map the requested Analysis to its equivalent wrapper in the old pass
1137   // manager. The scheme implemented here does not require every Analysis to be
1138   // updated. Only those new analyses that the client cares about in the old
1139   // pass manager need to expose a LegacyWrapper type, and that wrapper should
1140   // support a getResult() method that matches the new Analysis.
1141   //
1142   // We need SFINAE to check for the LegacyWrapper, but function templates don't
1143   // allow partial specialization, which is needed in this case. So instead, we
1144   // use a constexpr bool to perform the SFINAE, and then use this information
1145   // inside the function template.
1146   template <typename, typename = void>
1147   static constexpr bool HasLegacyWrapper = false;
1148 
1149   template <typename Analysis>
1150   typename Analysis::Result *getAnalysis(const Function &F,
1151                                          bool RequestCachedOnly = false) {
1152     if (!LegacyPass && !FAM)
1153       return nullptr;
1154     if (FAM) {
1155       if (CachedOnly || RequestCachedOnly)
1156         return FAM->getCachedResult<Analysis>(const_cast<Function &>(F));
1157       return &FAM->getResult<Analysis>(const_cast<Function &>(F));
1158     }
1159     if constexpr (HasLegacyWrapper<Analysis>) {
1160       if (!CachedOnly && !RequestCachedOnly)
1161         return &LegacyPass
1162                     ->getAnalysis<typename Analysis::LegacyWrapper>(
1163                         const_cast<Function &>(F))
1164                     .getResult();
1165       if (auto *P =
1166               LegacyPass
1167                   ->getAnalysisIfAvailable<typename Analysis::LegacyWrapper>())
1168         return &P->getResult();
1169     }
1170     return nullptr;
1171   }
1172 
1173   /// Invalidates the analyses. Valid only when using the new pass manager.
1174   void invalidateAnalyses() {
1175     assert(FAM && "Can only be used from the new PM!");
1176     FAM->clear();
1177   }
1178 
1179   AnalysisGetter(FunctionAnalysisManager &FAM, bool CachedOnly = false)
1180       : FAM(&FAM), CachedOnly(CachedOnly) {}
1181   AnalysisGetter(Pass *P, bool CachedOnly = false)
1182       : LegacyPass(P), CachedOnly(CachedOnly) {}
1183   AnalysisGetter() = default;
1184 
1185 private:
1186   FunctionAnalysisManager *FAM = nullptr;
1187   Pass *LegacyPass = nullptr;
1188 
1189   /// If \p CachedOnly is true, no pass is created, just existing results are
1190   /// used. Also available per request.
1191   bool CachedOnly = false;
1192 };
1193 
1194 template <typename Analysis>
1195 constexpr bool AnalysisGetter::HasLegacyWrapper<
1196     Analysis, std::void_t<typename Analysis::LegacyWrapper>> = true;
1197 
1198 /// Data structure to hold cached (LLVM-IR) information.
1199 ///
1200 /// All attributes are given an InformationCache object at creation time to
1201 /// avoid inspection of the IR by all of them individually. This default
1202 /// InformationCache will hold information required by 'default' attributes,
1203 /// thus the ones deduced when Attributor::identifyDefaultAbstractAttributes(..)
1204 /// is called.
1205 ///
1206 /// If custom abstract attributes, registered manually through
1207 /// Attributor::registerAA(...), need more information, especially if it is not
1208 /// reusable, it is advised to inherit from the InformationCache and cast the
1209 /// instance down in the abstract attributes.
1210 struct InformationCache {
1211   InformationCache(const Module &M, AnalysisGetter &AG,
1212                    BumpPtrAllocator &Allocator, SetVector<Function *> *CGSCC,
1213                    bool UseExplorer = true)
1214       : CGSCC(CGSCC), DL(M.getDataLayout()), Allocator(Allocator), AG(AG),
1215         TargetTriple(M.getTargetTriple()) {
1216     if (UseExplorer)
1217       Explorer = new (Allocator) MustBeExecutedContextExplorer(
1218           /* ExploreInterBlock */
1219           true, /* ExploreCFGForward */ true,
1220           /* ExploreCFGBackward */ true,
1221           /* LIGetter */
1222           [&](const Function &F) { return AG.getAnalysis<LoopAnalysis>(F); },
1223           /* DTGetter */
1224           [&](const Function &F) {
1225             return AG.getAnalysis<DominatorTreeAnalysis>(F);
1226           },
1227           /* PDTGetter */
1228           [&](const Function &F) {
1229             return AG.getAnalysis<PostDominatorTreeAnalysis>(F);
1230           });
1231   }
1232 
1233   virtual ~InformationCache() {
1234     // The FunctionInfo objects are allocated via a BumpPtrAllocator, we call
1235     // the destructor manually.
1236     for (auto &It : FuncInfoMap)
1237       It.getSecond()->~FunctionInfo();
1238     // Same is true for the instruction exclusions sets.
1239     using AA::InstExclusionSetTy;
1240     for (auto *BES : BESets)
1241       BES->~InstExclusionSetTy();
1242     if (Explorer)
1243       Explorer->~MustBeExecutedContextExplorer();
1244   }
1245 
1246   /// Apply \p CB to all uses of \p F. If \p LookThroughConstantExprUses is
1247   /// true, constant expression users are not given to \p CB but their uses are
1248   /// traversed transitively.
1249   template <typename CBTy>
1250   static void foreachUse(Function &F, CBTy CB,
1251                          bool LookThroughConstantExprUses = true) {
1252     SmallVector<Use *, 8> Worklist(make_pointer_range(F.uses()));
1253 
1254     for (unsigned Idx = 0; Idx < Worklist.size(); ++Idx) {
1255       Use &U = *Worklist[Idx];
1256 
1257       // Allow use in constant bitcasts and simply look through them.
1258       if (LookThroughConstantExprUses && isa<ConstantExpr>(U.getUser())) {
1259         for (Use &CEU : cast<ConstantExpr>(U.getUser())->uses())
1260           Worklist.push_back(&CEU);
1261         continue;
1262       }
1263 
1264       CB(U);
1265     }
1266   }
1267 
1268   /// The CG-SCC the pass is run on, or nullptr if it is a module pass.
1269   const SetVector<Function *> *const CGSCC = nullptr;
1270 
1271   /// A vector type to hold instructions.
1272   using InstructionVectorTy = SmallVector<Instruction *, 8>;
1273 
1274   /// A map type from opcodes to instructions with this opcode.
1275   using OpcodeInstMapTy = DenseMap<unsigned, InstructionVectorTy *>;
1276 
1277   /// Return the map that relates "interesting" opcodes with all instructions
1278   /// with that opcode in \p F.
1279   OpcodeInstMapTy &getOpcodeInstMapForFunction(const Function &F) {
1280     return getFunctionInfo(F).OpcodeInstMap;
1281   }
1282 
1283   /// Return the instructions in \p F that may read or write memory.
1284   InstructionVectorTy &getReadOrWriteInstsForFunction(const Function &F) {
1285     return getFunctionInfo(F).RWInsts;
1286   }
1287 
1288   /// Return MustBeExecutedContextExplorer
1289   MustBeExecutedContextExplorer *getMustBeExecutedContextExplorer() {
1290     return Explorer;
1291   }
1292 
1293   /// Return TargetLibraryInfo for function \p F.
1294   TargetLibraryInfo *getTargetLibraryInfoForFunction(const Function &F) {
1295     return AG.getAnalysis<TargetLibraryAnalysis>(F);
1296   }
1297 
1298   /// Return true if \p F has the "kernel" function attribute
1299   bool isKernel(const Function &F) {
1300     FunctionInfo &FI = getFunctionInfo(F);
1301     return FI.IsKernel;
1302   }
1303 
1304   /// Return true if \p Arg is involved in a must-tail call, thus the argument
1305   /// of the caller or callee.
1306   bool isInvolvedInMustTailCall(const Argument &Arg) {
1307     FunctionInfo &FI = getFunctionInfo(*Arg.getParent());
1308     return FI.CalledViaMustTail || FI.ContainsMustTailCall;
1309   }
1310 
1311   bool isOnlyUsedByAssume(const Instruction &I) const {
1312     return AssumeOnlyValues.contains(&I);
1313   }
1314 
1315   /// Invalidates the cached analyses. Valid only when using the new pass
1316   /// manager.
1317   void invalidateAnalyses() { AG.invalidateAnalyses(); }
1318 
1319   /// Return the analysis result from a pass \p AP for function \p F.
1320   template <typename AP>
1321   typename AP::Result *getAnalysisResultForFunction(const Function &F,
1322                                                     bool CachedOnly = false) {
1323     return AG.getAnalysis<AP>(F, CachedOnly);
1324   }
1325 
1326   /// Return datalayout used in the module.
1327   const DataLayout &getDL() { return DL; }
1328 
1329   /// Return the map conaining all the knowledge we have from `llvm.assume`s.
1330   const RetainedKnowledgeMap &getKnowledgeMap() const { return KnowledgeMap; }
1331 
1332   /// Given \p BES, return a uniqued version.
1333   const AA::InstExclusionSetTy *
1334   getOrCreateUniqueBlockExecutionSet(const AA::InstExclusionSetTy *BES) {
1335     auto It = BESets.find(BES);
1336     if (It != BESets.end())
1337       return *It;
1338     auto *UniqueBES = new (Allocator) AA::InstExclusionSetTy(*BES);
1339     bool Success = BESets.insert(UniqueBES).second;
1340     (void)Success;
1341     assert(Success && "Expected only new entries to be added");
1342     return UniqueBES;
1343   }
1344 
1345   /// Return true if the stack (llvm::Alloca) can be accessed by other threads.
1346   bool stackIsAccessibleByOtherThreads() { return !targetIsGPU(); }
1347 
1348   /// Return true if the target is a GPU.
1349   bool targetIsGPU() { return TargetTriple.isGPU(); }
1350 
1351   /// Return all functions that might be called indirectly, only valid for
1352   /// closed world modules (see isClosedWorldModule).
1353   LLVM_ABI ArrayRef<Function *>
1354   getIndirectlyCallableFunctions(Attributor &A) const;
1355 
1356   /// Return the flat address space if the associated target has.
1357   LLVM_ABI std::optional<unsigned> getFlatAddressSpace() const;
1358 
1359   virtual unsigned getMaxAddrSpace() const { return ~0U; }
1360 
1361 private:
1362   struct FunctionInfo {
1363     LLVM_ABI ~FunctionInfo();
1364 
1365     /// A nested map that remembers all instructions in a function with a
1366     /// certain instruction opcode (Instruction::getOpcode()).
1367     OpcodeInstMapTy OpcodeInstMap;
1368 
1369     /// A map from functions to their instructions that may read or write
1370     /// memory.
1371     InstructionVectorTy RWInsts;
1372 
1373     /// Function is called by a `musttail` call.
1374     bool CalledViaMustTail;
1375 
1376     /// Function contains a `musttail` call.
1377     bool ContainsMustTailCall;
1378 
1379     /// Function has the `"kernel"` attribute
1380     bool IsKernel;
1381   };
1382 
1383   /// A map type from functions to informatio about it.
1384   DenseMap<const Function *, FunctionInfo *> FuncInfoMap;
1385 
1386   /// Return information about the function \p F, potentially by creating it.
1387   FunctionInfo &getFunctionInfo(const Function &F) {
1388     FunctionInfo *&FI = FuncInfoMap[&F];
1389     if (!FI) {
1390       FI = new (Allocator) FunctionInfo();
1391       initializeInformationCache(F, *FI);
1392     }
1393     return *FI;
1394   }
1395 
1396   /// Vector of functions that might be callable indirectly, i.a., via a
1397   /// function pointer.
1398   SmallVector<Function *> IndirectlyCallableFunctions;
1399 
1400   /// Initialize the function information cache \p FI for the function \p F.
1401   ///
1402   /// This method needs to be called for all function that might be looked at
1403   /// through the information cache interface *prior* to looking at them.
1404   LLVM_ABI void initializeInformationCache(const Function &F, FunctionInfo &FI);
1405 
1406   /// The datalayout used in the module.
1407   const DataLayout &DL;
1408 
1409   /// The allocator used to allocate memory, e.g. for `FunctionInfo`s.
1410   BumpPtrAllocator &Allocator;
1411 
1412   /// MustBeExecutedContextExplorer
1413   MustBeExecutedContextExplorer *Explorer = nullptr;
1414 
1415   /// A map with knowledge retained in `llvm.assume` instructions.
1416   RetainedKnowledgeMap KnowledgeMap;
1417 
1418   /// A container for all instructions that are only used by `llvm.assume`.
1419   SetVector<const Instruction *> AssumeOnlyValues;
1420 
1421   /// Cache for block sets to allow reuse.
1422   DenseSet<const AA::InstExclusionSetTy *> BESets;
1423 
1424   /// Getters for analysis.
1425   AnalysisGetter &AG;
1426 
1427   /// Set of inlineable functions
1428   SmallPtrSet<const Function *, 8> InlineableFunctions;
1429 
1430   /// The triple describing the target machine.
1431   Triple TargetTriple;
1432 
1433   /// Give the Attributor access to the members so
1434   /// Attributor::identifyDefaultAbstractAttributes(...) can initialize them.
1435   friend struct Attributor;
1436 };
1437 
1438 /// Configuration for the Attributor.
1439 struct AttributorConfig {
1440 
1441   AttributorConfig(CallGraphUpdater &CGUpdater) : CGUpdater(CGUpdater) {}
1442 
1443   /// Is the user of the Attributor a module pass or not. This determines what
1444   /// IR we can look at and modify. If it is a module pass we might deduce facts
1445   /// outside the initial function set and modify functions outside that set,
1446   /// but only as part of the optimization of the functions in the initial
1447   /// function set. For CGSCC passes we can look at the IR of the module slice
1448   /// but never run any deduction, or perform any modification, outside the
1449   /// initial function set (which we assume is the SCC).
1450   bool IsModulePass = true;
1451 
1452   /// Flag to determine if we can delete functions or keep dead ones around.
1453   bool DeleteFns = true;
1454 
1455   /// Flag to determine if we rewrite function signatures.
1456   bool RewriteSignatures = true;
1457 
1458   /// Flag to determine if we want to initialize all default AAs for an internal
1459   /// function marked live. See also: InitializationCallback>
1460   bool DefaultInitializeLiveInternals = true;
1461 
1462   /// Flag to determine if we should skip all liveness checks early on.
1463   bool UseLiveness = true;
1464 
1465   /// Flag to indicate if the entire world is contained in this module, that
1466   /// is, no outside functions exist.
1467   bool IsClosedWorldModule = false;
1468 
1469   /// Callback function to be invoked on internal functions marked live.
1470   std::function<void(Attributor &A, const Function &F)> InitializationCallback =
1471       nullptr;
1472 
1473   /// Callback function to determine if an indirect call targets should be made
1474   /// direct call targets (with an if-cascade).
1475   std::function<bool(Attributor &A, const AbstractAttribute &AA, CallBase &CB,
1476                      Function &AssumedCallee, unsigned NumAssumedCallees)>
1477       IndirectCalleeSpecializationCallback = nullptr;
1478 
1479   /// Helper to update an underlying call graph and to delete functions.
1480   CallGraphUpdater &CGUpdater;
1481 
1482   /// If not null, a set limiting the attribute opportunities.
1483   DenseSet<const char *> *Allowed = nullptr;
1484 
1485   /// Maximum number of iterations to run until fixpoint.
1486   std::optional<unsigned> MaxFixpointIterations;
1487 
1488   /// A callback function that returns an ORE object from a Function pointer.
1489   ///{
1490   using OptimizationRemarkGetter =
1491       function_ref<OptimizationRemarkEmitter &(Function *)>;
1492   OptimizationRemarkGetter OREGetter = nullptr;
1493   ///}
1494 
1495   /// The name of the pass running the attributor, used to emit remarks.
1496   const char *PassName = nullptr;
1497 
1498   using IPOAmendableCBTy = std::function<bool(const Function &F)>;
1499   IPOAmendableCBTy IPOAmendableCB;
1500 };
1501 
1502 /// A debug counter to limit the number of AAs created.
1503 DEBUG_COUNTER(NumAbstractAttributes, "num-abstract-attributes",
1504               "How many AAs should be initialized");
1505 
1506 /// The fixpoint analysis framework that orchestrates the attribute deduction.
1507 ///
1508 /// The Attributor provides a general abstract analysis framework (guided
1509 /// fixpoint iteration) as well as helper functions for the deduction of
1510 /// (LLVM-IR) attributes. However, also other code properties can be deduced,
1511 /// propagated, and ultimately manifested through the Attributor framework. This
1512 /// is particularly useful if these properties interact with attributes and a
1513 /// co-scheduled deduction allows to improve the solution. Even if not, thus if
1514 /// attributes/properties are completely isolated, they should use the
1515 /// Attributor framework to reduce the number of fixpoint iteration frameworks
1516 /// in the code base. Note that the Attributor design makes sure that isolated
1517 /// attributes are not impacted, in any way, by others derived at the same time
1518 /// if there is no cross-reasoning performed.
1519 ///
1520 /// The public facing interface of the Attributor is kept simple and basically
1521 /// allows abstract attributes to one thing, query abstract attributes
1522 /// in-flight. There are two reasons to do this:
1523 ///    a) The optimistic state of one abstract attribute can justify an
1524 ///       optimistic state of another, allowing to framework to end up with an
1525 ///       optimistic (=best possible) fixpoint instead of one based solely on
1526 ///       information in the IR.
1527 ///    b) This avoids reimplementing various kinds of lookups, e.g., to check
1528 ///       for existing IR attributes, in favor of a single lookups interface
1529 ///       provided by an abstract attribute subclass.
1530 ///
1531 /// NOTE: The mechanics of adding a new "concrete" abstract attribute are
1532 ///       described in the file comment.
1533 struct Attributor {
1534 
1535   /// Constructor
1536   ///
1537   /// \param Functions The set of functions we are deriving attributes for.
1538   /// \param InfoCache Cache to hold various information accessible for
1539   ///                  the abstract attributes.
1540   /// \param Configuration The Attributor configuration which determines what
1541   ///                      generic features to use.
1542   LLVM_ABI Attributor(SetVector<Function *> &Functions,
1543                       InformationCache &InfoCache,
1544                       AttributorConfig Configuration);
1545 
1546   LLVM_ABI ~Attributor();
1547 
1548   /// Run the analyses until a fixpoint is reached or enforced (timeout).
1549   ///
1550   /// The attributes registered with this Attributor can be used after as long
1551   /// as the Attributor is not destroyed (it owns the attributes now).
1552   ///
1553   /// \Returns CHANGED if the IR was changed, otherwise UNCHANGED.
1554   LLVM_ABI ChangeStatus run();
1555 
1556   /// Lookup an abstract attribute of type \p AAType at position \p IRP. While
1557   /// no abstract attribute is found equivalent positions are checked, see
1558   /// SubsumingPositionIterator. Thus, the returned abstract attribute
1559   /// might be anchored at a different position, e.g., the callee if \p IRP is a
1560   /// call base.
1561   ///
1562   /// This method is the only (supported) way an abstract attribute can retrieve
1563   /// information from another abstract attribute. As an example, take an
1564   /// abstract attribute that determines the memory access behavior for a
1565   /// argument (readnone, readonly, ...). It should use `getAAFor` to get the
1566   /// most optimistic information for other abstract attributes in-flight, e.g.
1567   /// the one reasoning about the "captured" state for the argument or the one
1568   /// reasoning on the memory access behavior of the function as a whole.
1569   ///
1570   /// If the DepClass enum is set to `DepClassTy::None` the dependence from
1571   /// \p QueryingAA to the return abstract attribute is not automatically
1572   /// recorded. This should only be used if the caller will record the
1573   /// dependence explicitly if necessary, thus if it the returned abstract
1574   /// attribute is used for reasoning. To record the dependences explicitly use
1575   /// the `Attributor::recordDependence` method.
1576   template <typename AAType>
1577   const AAType *getAAFor(const AbstractAttribute &QueryingAA,
1578                          const IRPosition &IRP, DepClassTy DepClass) {
1579     return getOrCreateAAFor<AAType>(IRP, &QueryingAA, DepClass,
1580                                     /* ForceUpdate */ false);
1581   }
1582 
1583   /// The version of getAAFor that allows to omit a querying abstract
1584   /// attribute. Using this after Attributor started running is restricted to
1585   /// only the Attributor itself. Initial seeding of AAs can be done via this
1586   /// function.
1587   /// NOTE: ForceUpdate is ignored in any stage other than the update stage.
1588   template <typename AAType>
1589   const AAType *getOrCreateAAFor(IRPosition IRP,
1590                                  const AbstractAttribute *QueryingAA,
1591                                  DepClassTy DepClass, bool ForceUpdate = false,
1592                                  bool UpdateAfterInit = true) {
1593     if (!shouldPropagateCallBaseContext(IRP))
1594       IRP = IRP.stripCallBaseContext();
1595 
1596     if (AAType *AAPtr = lookupAAFor<AAType>(IRP, QueryingAA, DepClass,
1597                                             /* AllowInvalidState */ true)) {
1598       if (ForceUpdate && Phase == AttributorPhase::UPDATE)
1599         updateAA(*AAPtr);
1600       return AAPtr;
1601     }
1602 
1603     bool ShouldUpdateAA;
1604     if (!shouldInitialize<AAType>(IRP, ShouldUpdateAA))
1605       return nullptr;
1606 
1607     if (!DebugCounter::shouldExecute(NumAbstractAttributes))
1608       return nullptr;
1609 
1610     // No matching attribute found, create one.
1611     // Use the static create method.
1612     auto &AA = AAType::createForPosition(IRP, *this);
1613 
1614     // Always register a new attribute to make sure we clean up the allocated
1615     // memory properly.
1616     registerAA(AA);
1617 
1618     // If we are currenty seeding attributes, enforce seeding rules.
1619     if (Phase == AttributorPhase::SEEDING && !shouldSeedAttribute(AA)) {
1620       AA.getState().indicatePessimisticFixpoint();
1621       return &AA;
1622     }
1623 
1624     // Bootstrap the new attribute with an initial update to propagate
1625     // information, e.g., function -> call site.
1626     {
1627       TimeTraceScope TimeScope("initialize", [&]() {
1628         return AA.getName().str() +
1629                std::to_string(AA.getIRPosition().getPositionKind());
1630       });
1631       ++InitializationChainLength;
1632       AA.initialize(*this);
1633       --InitializationChainLength;
1634     }
1635 
1636     if (!ShouldUpdateAA) {
1637       AA.getState().indicatePessimisticFixpoint();
1638       return &AA;
1639     }
1640 
1641     // Allow seeded attributes to declare dependencies.
1642     // Remember the seeding state.
1643     if (UpdateAfterInit) {
1644       AttributorPhase OldPhase = Phase;
1645       Phase = AttributorPhase::UPDATE;
1646 
1647       updateAA(AA);
1648 
1649       Phase = OldPhase;
1650     }
1651 
1652     if (QueryingAA && AA.getState().isValidState())
1653       recordDependence(AA, const_cast<AbstractAttribute &>(*QueryingAA),
1654                        DepClass);
1655     return &AA;
1656   }
1657 
1658   template <typename AAType>
1659   const AAType *getOrCreateAAFor(const IRPosition &IRP) {
1660     return getOrCreateAAFor<AAType>(IRP, /* QueryingAA */ nullptr,
1661                                     DepClassTy::NONE);
1662   }
1663 
1664   /// Return the attribute of \p AAType for \p IRP if existing and valid. This
1665   /// also allows non-AA users lookup.
1666   template <typename AAType>
1667   AAType *lookupAAFor(const IRPosition &IRP,
1668                       const AbstractAttribute *QueryingAA = nullptr,
1669                       DepClassTy DepClass = DepClassTy::OPTIONAL,
1670                       bool AllowInvalidState = false) {
1671     static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
1672                   "Cannot query an attribute with a type not derived from "
1673                   "'AbstractAttribute'!");
1674     // Lookup the abstract attribute of type AAType. If found, return it after
1675     // registering a dependence of QueryingAA on the one returned attribute.
1676     AbstractAttribute *AAPtr = AAMap.lookup({&AAType::ID, IRP});
1677     if (!AAPtr)
1678       return nullptr;
1679 
1680     AAType *AA = static_cast<AAType *>(AAPtr);
1681 
1682     // Do not register a dependence on an attribute with an invalid state.
1683     if (DepClass != DepClassTy::NONE && QueryingAA &&
1684         AA->getState().isValidState())
1685       recordDependence(*AA, const_cast<AbstractAttribute &>(*QueryingAA),
1686                        DepClass);
1687 
1688     // Return nullptr if this attribute has an invalid state.
1689     if (!AllowInvalidState && !AA->getState().isValidState())
1690       return nullptr;
1691     return AA;
1692   }
1693 
1694   /// Allows a query AA to request an update if a new query was received.
1695   LLVM_ABI void registerForUpdate(AbstractAttribute &AA);
1696 
1697   /// Explicitly record a dependence from \p FromAA to \p ToAA, that is if
1698   /// \p FromAA changes \p ToAA should be updated as well.
1699   ///
1700   /// This method should be used in conjunction with the `getAAFor` method and
1701   /// with the DepClass enum passed to the method set to None. This can
1702   /// be beneficial to avoid false dependences but it requires the users of
1703   /// `getAAFor` to explicitly record true dependences through this method.
1704   /// The \p DepClass flag indicates if the dependence is striclty necessary.
1705   /// That means for required dependences, if \p FromAA changes to an invalid
1706   /// state, \p ToAA can be moved to a pessimistic fixpoint because it required
1707   /// information from \p FromAA but none are available anymore.
1708   LLVM_ABI void recordDependence(const AbstractAttribute &FromAA,
1709                                  const AbstractAttribute &ToAA,
1710                                  DepClassTy DepClass);
1711 
1712   /// Introduce a new abstract attribute into the fixpoint analysis.
1713   ///
1714   /// Note that ownership of the attribute is given to the Attributor. It will
1715   /// invoke delete for the Attributor on destruction of the Attributor.
1716   ///
1717   /// Attributes are identified by their IR position (AAType::getIRPosition())
1718   /// and the address of their static member (see AAType::ID).
1719   template <typename AAType> AAType &registerAA(AAType &AA) {
1720     static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
1721                   "Cannot register an attribute with a type not derived from "
1722                   "'AbstractAttribute'!");
1723     // Put the attribute in the lookup map structure and the container we use to
1724     // keep track of all attributes.
1725     const IRPosition &IRP = AA.getIRPosition();
1726     AbstractAttribute *&AAPtr = AAMap[{&AAType::ID, IRP}];
1727 
1728     assert(!AAPtr && "Attribute already in map!");
1729     AAPtr = &AA;
1730 
1731     // Register AA with the synthetic root only before the manifest stage.
1732     if (Phase == AttributorPhase::SEEDING || Phase == AttributorPhase::UPDATE)
1733       DG.SyntheticRoot.Deps.insert(
1734           AADepGraphNode::DepTy(&AA, unsigned(DepClassTy::REQUIRED)));
1735 
1736     return AA;
1737   }
1738 
1739   /// Return the internal information cache.
1740   InformationCache &getInfoCache() { return InfoCache; }
1741 
1742   /// Return true if this is a module pass, false otherwise.
1743   bool isModulePass() const { return Configuration.IsModulePass; }
1744 
1745   /// Return true if we should specialize the call site \b CB for the potential
1746   /// callee \p Fn.
1747   bool shouldSpecializeCallSiteForCallee(const AbstractAttribute &AA,
1748                                          CallBase &CB, Function &Callee,
1749                                          unsigned NumAssumedCallees) {
1750     return Configuration.IndirectCalleeSpecializationCallback
1751                ? Configuration.IndirectCalleeSpecializationCallback(
1752                      *this, AA, CB, Callee, NumAssumedCallees)
1753                : true;
1754   }
1755 
1756   /// Return true if the module contains the whole world, thus, no outside
1757   /// functions exist.
1758   LLVM_ABI bool isClosedWorldModule() const;
1759 
1760   /// Return true if we derive attributes for \p Fn
1761   bool isRunOn(Function &Fn) const { return isRunOn(&Fn); }
1762   bool isRunOn(Function *Fn) const {
1763     return Functions.empty() || Functions.count(Fn);
1764   }
1765 
1766   template <typename AAType> bool shouldUpdateAA(const IRPosition &IRP) {
1767     // If this is queried in the manifest stage, we force the AA to indicate
1768     // pessimistic fixpoint immediately.
1769     if (Phase == AttributorPhase::MANIFEST || Phase == AttributorPhase::CLEANUP)
1770       return false;
1771 
1772     Function *AssociatedFn = IRP.getAssociatedFunction();
1773 
1774     if (IRP.isAnyCallSitePosition()) {
1775       // Check if we require a callee but there is none.
1776       if (!AssociatedFn && AAType::requiresCalleeForCallBase())
1777         return false;
1778 
1779       // Check if we require non-asm but it is inline asm.
1780       if (AAType::requiresNonAsmForCallBase() &&
1781           cast<CallBase>(IRP.getAnchorValue()).isInlineAsm())
1782         return false;
1783     }
1784 
1785     // Check if we require a calles but we can't see all.
1786     if (AAType::requiresCallersForArgOrFunction())
1787       if (IRP.getPositionKind() == IRPosition::IRP_FUNCTION ||
1788           IRP.getPositionKind() == IRPosition::IRP_ARGUMENT)
1789         if (!AssociatedFn->hasLocalLinkage())
1790           return false;
1791 
1792     if (!AAType::isValidIRPositionForUpdate(*this, IRP))
1793       return false;
1794 
1795     // We update only AAs associated with functions in the Functions set or
1796     // call sites of them.
1797     return (!AssociatedFn || isModulePass() || isRunOn(AssociatedFn) ||
1798             isRunOn(IRP.getAnchorScope()));
1799   }
1800 
1801   template <typename AAType>
1802   bool shouldInitialize(const IRPosition &IRP, bool &ShouldUpdateAA) {
1803     if (!AAType::isValidIRPositionForInit(*this, IRP))
1804       return false;
1805 
1806     if (Configuration.Allowed && !Configuration.Allowed->count(&AAType::ID))
1807       return false;
1808 
1809     // For now we skip anything in naked and optnone functions.
1810     const Function *AnchorFn = IRP.getAnchorScope();
1811     if (AnchorFn && (AnchorFn->hasFnAttribute(Attribute::Naked) ||
1812                      AnchorFn->hasFnAttribute(Attribute::OptimizeNone)))
1813       return false;
1814 
1815     // Avoid too many nested initializations to prevent a stack overflow.
1816     if (InitializationChainLength > MaxInitializationChainLength)
1817       return false;
1818 
1819     ShouldUpdateAA = shouldUpdateAA<AAType>(IRP);
1820 
1821     return !AAType::hasTrivialInitializer() || ShouldUpdateAA;
1822   }
1823 
1824   /// Determine opportunities to derive 'default' attributes in \p F and create
1825   /// abstract attribute objects for them.
1826   ///
1827   /// \param F The function that is checked for attribute opportunities.
1828   ///
1829   /// Note that abstract attribute instances are generally created even if the
1830   /// IR already contains the information they would deduce. The most important
1831   /// reason for this is the single interface, the one of the abstract attribute
1832   /// instance, which can be queried without the need to look at the IR in
1833   /// various places.
1834   LLVM_ABI void identifyDefaultAbstractAttributes(Function &F);
1835 
1836   /// Determine whether the function \p F is IPO amendable
1837   ///
1838   /// If a function is exactly defined or it has alwaysinline attribute
1839   /// and is viable to be inlined, we say it is IPO amendable
1840   bool isFunctionIPOAmendable(const Function &F) {
1841     return F.hasExactDefinition() || InfoCache.InlineableFunctions.count(&F) ||
1842            (Configuration.IPOAmendableCB && Configuration.IPOAmendableCB(F));
1843   }
1844 
1845   /// Mark the internal function \p F as live.
1846   ///
1847   /// This will trigger the identification and initialization of attributes for
1848   /// \p F.
1849   void markLiveInternalFunction(const Function &F) {
1850     assert(F.hasLocalLinkage() &&
1851            "Only local linkage is assumed dead initially.");
1852 
1853     if (Configuration.DefaultInitializeLiveInternals)
1854       identifyDefaultAbstractAttributes(const_cast<Function &>(F));
1855     if (Configuration.InitializationCallback)
1856       Configuration.InitializationCallback(*this, F);
1857   }
1858 
1859   /// Record that \p U is to be replaces with \p NV after information was
1860   /// manifested. This also triggers deletion of trivially dead istructions.
1861   bool changeUseAfterManifest(Use &U, Value &NV) {
1862     Value *&V = ToBeChangedUses[&U];
1863     if (V && (V->stripPointerCasts() == NV.stripPointerCasts() ||
1864               isa_and_nonnull<UndefValue>(V)))
1865       return false;
1866     assert((!V || V == &NV || isa<UndefValue>(NV)) &&
1867            "Use was registered twice for replacement with different values!");
1868     V = &NV;
1869     return true;
1870   }
1871 
1872   /// Helper function to replace all uses associated with \p IRP with \p NV.
1873   /// Return true if there is any change. The flag \p ChangeDroppable indicates
1874   /// if dropppable uses should be changed too.
1875   bool changeAfterManifest(const IRPosition IRP, Value &NV,
1876                            bool ChangeDroppable = true) {
1877     if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE_ARGUMENT) {
1878       auto *CB = cast<CallBase>(IRP.getCtxI());
1879       return changeUseAfterManifest(
1880           CB->getArgOperandUse(IRP.getCallSiteArgNo()), NV);
1881     }
1882     Value &V = IRP.getAssociatedValue();
1883     auto &Entry = ToBeChangedValues[&V];
1884     Value *CurNV = get<0>(Entry);
1885     if (CurNV && (CurNV->stripPointerCasts() == NV.stripPointerCasts() ||
1886                   isa<UndefValue>(CurNV)))
1887       return false;
1888     assert((!CurNV || CurNV == &NV || isa<UndefValue>(NV)) &&
1889            "Value replacement was registered twice with different values!");
1890     Entry = {&NV, ChangeDroppable};
1891     return true;
1892   }
1893 
1894   /// Record that \p I is to be replaced with `unreachable` after information
1895   /// was manifested.
1896   void changeToUnreachableAfterManifest(Instruction *I) {
1897     ToBeChangedToUnreachableInsts.insert(I);
1898   }
1899 
1900   /// Record that \p II has at least one dead successor block. This information
1901   /// is used, e.g., to replace \p II with a call, after information was
1902   /// manifested.
1903   void registerInvokeWithDeadSuccessor(InvokeInst &II) {
1904     InvokeWithDeadSuccessor.insert(&II);
1905   }
1906 
1907   /// Record that \p I is deleted after information was manifested. This also
1908   /// triggers deletion of trivially dead istructions.
1909   void deleteAfterManifest(Instruction &I) { ToBeDeletedInsts.insert(&I); }
1910 
1911   /// Record that \p BB is deleted after information was manifested. This also
1912   /// triggers deletion of trivially dead istructions.
1913   void deleteAfterManifest(BasicBlock &BB) { ToBeDeletedBlocks.insert(&BB); }
1914 
1915   // Record that \p BB is added during the manifest of an AA. Added basic blocks
1916   // are preserved in the IR.
1917   void registerManifestAddedBasicBlock(BasicBlock &BB) {
1918     ManifestAddedBlocks.insert(&BB);
1919   }
1920 
1921   /// Record that \p F is deleted after information was manifested.
1922   void deleteAfterManifest(Function &F) {
1923     if (Configuration.DeleteFns)
1924       ToBeDeletedFunctions.insert(&F);
1925   }
1926 
1927   /// Return the attributes of kind \p AK existing in the IR as operand bundles
1928   /// of an llvm.assume.
1929   LLVM_ABI bool getAttrsFromAssumes(const IRPosition &IRP,
1930                                     Attribute::AttrKind AK,
1931                                     SmallVectorImpl<Attribute> &Attrs);
1932 
1933   /// Return true if any kind in \p AKs existing in the IR at a position that
1934   /// will affect this one. See also getAttrs(...).
1935   /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
1936   ///                                 e.g., the function position if this is an
1937   ///                                 argument position, should be ignored.
1938   LLVM_ABI bool
1939   hasAttr(const IRPosition &IRP, ArrayRef<Attribute::AttrKind> AKs,
1940           bool IgnoreSubsumingPositions = false,
1941           Attribute::AttrKind ImpliedAttributeKind = Attribute::None);
1942 
1943   /// Return the attributes of any kind in \p AKs existing in the IR at a
1944   /// position that will affect this one. While each position can only have a
1945   /// single attribute of any kind in \p AKs, there are "subsuming" positions
1946   /// that could have an attribute as well. This method returns all attributes
1947   /// found in \p Attrs.
1948   /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
1949   ///                                 e.g., the function position if this is an
1950   ///                                 argument position, should be ignored.
1951   LLVM_ABI void getAttrs(const IRPosition &IRP,
1952                          ArrayRef<Attribute::AttrKind> AKs,
1953                          SmallVectorImpl<Attribute> &Attrs,
1954                          bool IgnoreSubsumingPositions = false);
1955 
1956   /// Remove all \p AttrKinds attached to \p IRP.
1957   LLVM_ABI ChangeStatus removeAttrs(const IRPosition &IRP,
1958                                     ArrayRef<Attribute::AttrKind> AttrKinds);
1959   LLVM_ABI ChangeStatus removeAttrs(const IRPosition &IRP,
1960                                     ArrayRef<StringRef> Attrs);
1961 
1962   /// Attach \p DeducedAttrs to \p IRP, if \p ForceReplace is set we do this
1963   /// even if the same attribute kind was already present.
1964   LLVM_ABI ChangeStatus manifestAttrs(const IRPosition &IRP,
1965                                       ArrayRef<Attribute> DeducedAttrs,
1966                                       bool ForceReplace = false);
1967 
1968 private:
1969   /// Helper to check \p Attrs for \p AK, if not found, check if \p
1970   /// AAType::isImpliedByIR is true, and if not, create AAType for \p IRP.
1971   /// If \p SkipHasAttrCheck is true, don't check whether the attribute is set
1972   /// first. This should be used if only some values of a complex IR attribute
1973   /// imply the AAType.
1974   template <Attribute::AttrKind AK, typename AAType>
1975   void checkAndQueryIRAttr(const IRPosition &IRP, AttributeSet Attrs,
1976                            bool SkipHasAttrCheck = false);
1977 
1978   /// Helper to apply \p CB on all attributes of type \p AttrDescs of \p IRP.
1979   template <typename DescTy>
1980   ChangeStatus updateAttrMap(const IRPosition &IRP, ArrayRef<DescTy> AttrDescs,
1981                              function_ref<bool(const DescTy &, AttributeSet,
1982                                                AttributeMask &, AttrBuilder &)>
1983                                  CB);
1984 
1985   /// Mapping from functions/call sites to their attributes.
1986   DenseMap<Value *, AttributeList> AttrsMap;
1987 
1988 public:
1989   /// If \p IRP is assumed to be a constant, return it, if it is unclear yet,
1990   /// return std::nullopt, otherwise return `nullptr`.
1991   LLVM_ABI std::optional<Constant *>
1992   getAssumedConstant(const IRPosition &IRP, const AbstractAttribute &AA,
1993                      bool &UsedAssumedInformation);
1994   std::optional<Constant *> getAssumedConstant(const Value &V,
1995                                                const AbstractAttribute &AA,
1996                                                bool &UsedAssumedInformation) {
1997     return getAssumedConstant(IRPosition::value(V), AA, UsedAssumedInformation);
1998   }
1999 
2000   /// If \p V is assumed simplified, return it, if it is unclear yet,
2001   /// return std::nullopt, otherwise return `nullptr`.
2002   std::optional<Value *> getAssumedSimplified(const IRPosition &IRP,
2003                                               const AbstractAttribute &AA,
2004                                               bool &UsedAssumedInformation,
2005                                               AA::ValueScope S) {
2006     return getAssumedSimplified(IRP, &AA, UsedAssumedInformation, S);
2007   }
2008   std::optional<Value *> getAssumedSimplified(const Value &V,
2009                                               const AbstractAttribute &AA,
2010                                               bool &UsedAssumedInformation,
2011                                               AA::ValueScope S) {
2012     return getAssumedSimplified(IRPosition::value(V), AA,
2013                                 UsedAssumedInformation, S);
2014   }
2015 
2016   /// If \p V is assumed simplified, return it, if it is unclear yet,
2017   /// return std::nullopt, otherwise return `nullptr`. Same as the public
2018   /// version except that it can be used without recording dependences on any \p
2019   /// AA.
2020   LLVM_ABI std::optional<Value *>
2021   getAssumedSimplified(const IRPosition &V, const AbstractAttribute *AA,
2022                        bool &UsedAssumedInformation, AA::ValueScope S);
2023 
2024   /// Try to simplify \p IRP and in the scope \p S. If successful, true is
2025   /// returned and all potential values \p IRP can take are put into \p Values.
2026   /// If the result in \p Values contains select or PHI instructions it means
2027   /// those could not be simplified to a single value. Recursive calls with
2028   /// these instructions will yield their respective potential values. If false
2029   /// is returned no other information is valid.
2030   LLVM_ABI bool
2031   getAssumedSimplifiedValues(const IRPosition &IRP, const AbstractAttribute *AA,
2032                              SmallVectorImpl<AA::ValueAndContext> &Values,
2033                              AA::ValueScope S, bool &UsedAssumedInformation,
2034                              bool RecurseForSelectAndPHI = true);
2035 
2036   /// Register \p CB as a simplification callback.
2037   /// `Attributor::getAssumedSimplified` will use these callbacks before
2038   /// we it will ask `AAValueSimplify`. It is important to ensure this
2039   /// is called before `identifyDefaultAbstractAttributes`, assuming the
2040   /// latter is called at all.
2041   using SimplifictionCallbackTy = std::function<std::optional<Value *>(
2042       const IRPosition &, const AbstractAttribute *, bool &)>;
2043   void registerSimplificationCallback(const IRPosition &IRP,
2044                                       const SimplifictionCallbackTy &CB) {
2045     SimplificationCallbacks[IRP].emplace_back(CB);
2046   }
2047 
2048   /// Return true if there is a simplification callback for \p IRP.
2049   bool hasSimplificationCallback(const IRPosition &IRP) {
2050     return SimplificationCallbacks.count(IRP);
2051   }
2052 
2053   /// Register \p CB as a simplification callback.
2054   /// Similar to \p registerSimplificationCallback, the call back will be called
2055   /// first when we simplify a global variable \p GV.
2056   using GlobalVariableSimplifictionCallbackTy =
2057       std::function<std::optional<Constant *>(
2058           const GlobalVariable &, const AbstractAttribute *, bool &)>;
2059   void registerGlobalVariableSimplificationCallback(
2060       const GlobalVariable &GV,
2061       const GlobalVariableSimplifictionCallbackTy &CB) {
2062     GlobalVariableSimplificationCallbacks[&GV].emplace_back(CB);
2063   }
2064 
2065   /// Return true if there is a simplification callback for \p GV.
2066   bool hasGlobalVariableSimplificationCallback(const GlobalVariable &GV) {
2067     return GlobalVariableSimplificationCallbacks.count(&GV);
2068   }
2069 
2070   /// Return \p std::nullopt if there is no call back registered for \p GV or
2071   /// the call back is still not sure if \p GV can be simplified. Return \p
2072   /// nullptr if \p GV can't be simplified.
2073   std::optional<Constant *>
2074   getAssumedInitializerFromCallBack(const GlobalVariable &GV,
2075                                     const AbstractAttribute *AA,
2076                                     bool &UsedAssumedInformation) {
2077     assert(GlobalVariableSimplificationCallbacks.contains(&GV));
2078     for (auto &CB : GlobalVariableSimplificationCallbacks.lookup(&GV)) {
2079       auto SimplifiedGV = CB(GV, AA, UsedAssumedInformation);
2080       // For now we assume the call back will not return a std::nullopt.
2081       assert(SimplifiedGV.has_value() && "SimplifiedGV has not value");
2082       return *SimplifiedGV;
2083     }
2084     llvm_unreachable("there must be a callback registered");
2085   }
2086 
2087   using VirtualUseCallbackTy =
2088       std::function<bool(Attributor &, const AbstractAttribute *)>;
2089   void registerVirtualUseCallback(const Value &V,
2090                                   const VirtualUseCallbackTy &CB) {
2091     VirtualUseCallbacks[&V].emplace_back(CB);
2092   }
2093 
2094 private:
2095   /// The vector with all simplification callbacks registered by outside AAs.
2096   DenseMap<IRPosition, SmallVector<SimplifictionCallbackTy, 1>>
2097       SimplificationCallbacks;
2098 
2099   /// The vector with all simplification callbacks for global variables
2100   /// registered by outside AAs.
2101   DenseMap<const GlobalVariable *,
2102            SmallVector<GlobalVariableSimplifictionCallbackTy, 1>>
2103       GlobalVariableSimplificationCallbacks;
2104 
2105   DenseMap<const Value *, SmallVector<VirtualUseCallbackTy, 1>>
2106       VirtualUseCallbacks;
2107 
2108 public:
2109   /// Translate \p V from the callee context into the call site context.
2110   LLVM_ABI std::optional<Value *>
2111   translateArgumentToCallSiteContent(std::optional<Value *> V, CallBase &CB,
2112                                      const AbstractAttribute &AA,
2113                                      bool &UsedAssumedInformation);
2114 
2115   /// Return true if \p AA (or its context instruction) is assumed dead.
2116   ///
2117   /// If \p LivenessAA is not provided it is queried.
2118   LLVM_ABI bool isAssumedDead(const AbstractAttribute &AA,
2119                               const AAIsDead *LivenessAA,
2120                               bool &UsedAssumedInformation,
2121                               bool CheckBBLivenessOnly = false,
2122                               DepClassTy DepClass = DepClassTy::OPTIONAL);
2123 
2124   /// Return true if \p I is assumed dead.
2125   ///
2126   /// If \p LivenessAA is not provided it is queried.
2127   LLVM_ABI bool isAssumedDead(const Instruction &I,
2128                               const AbstractAttribute *QueryingAA,
2129                               const AAIsDead *LivenessAA,
2130                               bool &UsedAssumedInformation,
2131                               bool CheckBBLivenessOnly = false,
2132                               DepClassTy DepClass = DepClassTy::OPTIONAL,
2133                               bool CheckForDeadStore = false);
2134 
2135   /// Return true if \p U is assumed dead.
2136   ///
2137   /// If \p FnLivenessAA is not provided it is queried.
2138   LLVM_ABI bool isAssumedDead(const Use &U, const AbstractAttribute *QueryingAA,
2139                               const AAIsDead *FnLivenessAA,
2140                               bool &UsedAssumedInformation,
2141                               bool CheckBBLivenessOnly = false,
2142                               DepClassTy DepClass = DepClassTy::OPTIONAL);
2143 
2144   /// Return true if \p IRP is assumed dead.
2145   ///
2146   /// If \p FnLivenessAA is not provided it is queried.
2147   LLVM_ABI bool isAssumedDead(const IRPosition &IRP,
2148                               const AbstractAttribute *QueryingAA,
2149                               const AAIsDead *FnLivenessAA,
2150                               bool &UsedAssumedInformation,
2151                               bool CheckBBLivenessOnly = false,
2152                               DepClassTy DepClass = DepClassTy::OPTIONAL);
2153 
2154   /// Return true if \p BB is assumed dead.
2155   ///
2156   /// If \p LivenessAA is not provided it is queried.
2157   LLVM_ABI bool isAssumedDead(const BasicBlock &BB,
2158                               const AbstractAttribute *QueryingAA,
2159                               const AAIsDead *FnLivenessAA,
2160                               DepClassTy DepClass = DepClassTy::OPTIONAL);
2161 
2162   /// Check \p Pred on all potential Callees of \p CB.
2163   ///
2164   /// This method will evaluate \p Pred with all potential callees of \p CB as
2165   /// input and return true if \p Pred does. If some callees might be unknown
2166   /// this function will return false.
2167   LLVM_ABI bool checkForAllCallees(
2168       function_ref<bool(ArrayRef<const Function *> Callees)> Pred,
2169       const AbstractAttribute &QueryingAA, const CallBase &CB);
2170 
2171   /// Check \p Pred on all (transitive) uses of \p V.
2172   ///
2173   /// This method will evaluate \p Pred on all (transitive) uses of the
2174   /// associated value and return true if \p Pred holds every time.
2175   /// If uses are skipped in favor of equivalent ones, e.g., if we look through
2176   /// memory, the \p EquivalentUseCB will be used to give the caller an idea
2177   /// what original used was replaced by a new one (or new ones). The visit is
2178   /// cut short if \p EquivalentUseCB returns false and the function will return
2179   /// false as well.
2180   LLVM_ABI bool checkForAllUses(
2181       function_ref<bool(const Use &, bool &)> Pred,
2182       const AbstractAttribute &QueryingAA, const Value &V,
2183       bool CheckBBLivenessOnly = false,
2184       DepClassTy LivenessDepClass = DepClassTy::OPTIONAL,
2185       bool IgnoreDroppableUses = true,
2186       function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB =
2187           nullptr);
2188 
2189   /// Emit a remark generically.
2190   ///
2191   /// This template function can be used to generically emit a remark. The
2192   /// RemarkKind should be one of the following:
2193   ///   - OptimizationRemark to indicate a successful optimization attempt
2194   ///   - OptimizationRemarkMissed to report a failed optimization attempt
2195   ///   - OptimizationRemarkAnalysis to provide additional information about an
2196   ///     optimization attempt
2197   ///
2198   /// The remark is built using a callback function \p RemarkCB that takes a
2199   /// RemarkKind as input and returns a RemarkKind.
2200   template <typename RemarkKind, typename RemarkCallBack>
2201   void emitRemark(Instruction *I, StringRef RemarkName,
2202                   RemarkCallBack &&RemarkCB) const {
2203     if (!Configuration.OREGetter)
2204       return;
2205 
2206     Function *F = I->getFunction();
2207     auto &ORE = Configuration.OREGetter(F);
2208 
2209     if (RemarkName.starts_with("OMP"))
2210       ORE.emit([&]() {
2211         return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, I))
2212                << " [" << RemarkName << "]";
2213       });
2214     else
2215       ORE.emit([&]() {
2216         return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, I));
2217       });
2218   }
2219 
2220   /// Emit a remark on a function.
2221   template <typename RemarkKind, typename RemarkCallBack>
2222   void emitRemark(Function *F, StringRef RemarkName,
2223                   RemarkCallBack &&RemarkCB) const {
2224     if (!Configuration.OREGetter)
2225       return;
2226 
2227     auto &ORE = Configuration.OREGetter(F);
2228 
2229     if (RemarkName.starts_with("OMP"))
2230       ORE.emit([&]() {
2231         return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, F))
2232                << " [" << RemarkName << "]";
2233       });
2234     else
2235       ORE.emit([&]() {
2236         return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, F));
2237       });
2238   }
2239 
2240   /// Helper struct used in the communication between an abstract attribute (AA)
2241   /// that wants to change the signature of a function and the Attributor which
2242   /// applies the changes. The struct is partially initialized with the
2243   /// information from the AA (see the constructor). All other members are
2244   /// provided by the Attributor prior to invoking any callbacks.
2245   struct ArgumentReplacementInfo {
2246     /// Callee repair callback type
2247     ///
2248     /// The function repair callback is invoked once to rewire the replacement
2249     /// arguments in the body of the new function. The argument replacement info
2250     /// is passed, as build from the registerFunctionSignatureRewrite call, as
2251     /// well as the replacement function and an iteratore to the first
2252     /// replacement argument.
2253     using CalleeRepairCBTy = std::function<void(
2254         const ArgumentReplacementInfo &, Function &, Function::arg_iterator)>;
2255 
2256     /// Abstract call site (ACS) repair callback type
2257     ///
2258     /// The abstract call site repair callback is invoked once on every abstract
2259     /// call site of the replaced function (\see ReplacedFn). The callback needs
2260     /// to provide the operands for the call to the new replacement function.
2261     /// The number and type of the operands appended to the provided vector
2262     /// (second argument) is defined by the number and types determined through
2263     /// the replacement type vector (\see ReplacementTypes). The first argument
2264     /// is the ArgumentReplacementInfo object registered with the Attributor
2265     /// through the registerFunctionSignatureRewrite call.
2266     using ACSRepairCBTy =
2267         std::function<void(const ArgumentReplacementInfo &, AbstractCallSite,
2268                            SmallVectorImpl<Value *> &)>;
2269 
2270     /// Simple getters, see the corresponding members for details.
2271     ///{
2272 
2273     Attributor &getAttributor() const { return A; }
2274     const Function &getReplacedFn() const { return ReplacedFn; }
2275     const Argument &getReplacedArg() const { return ReplacedArg; }
2276     unsigned getNumReplacementArgs() const { return ReplacementTypes.size(); }
2277     const SmallVectorImpl<Type *> &getReplacementTypes() const {
2278       return ReplacementTypes;
2279     }
2280 
2281     ///}
2282 
2283   private:
2284     /// Constructor that takes the argument to be replaced, the types of
2285     /// the replacement arguments, as well as callbacks to repair the call sites
2286     /// and new function after the replacement happened.
2287     ArgumentReplacementInfo(Attributor &A, Argument &Arg,
2288                             ArrayRef<Type *> ReplacementTypes,
2289                             CalleeRepairCBTy &&CalleeRepairCB,
2290                             ACSRepairCBTy &&ACSRepairCB)
2291         : A(A), ReplacedFn(*Arg.getParent()), ReplacedArg(Arg),
2292           ReplacementTypes(ReplacementTypes),
2293           CalleeRepairCB(std::move(CalleeRepairCB)),
2294           ACSRepairCB(std::move(ACSRepairCB)) {}
2295 
2296     /// Reference to the attributor to allow access from the callbacks.
2297     Attributor &A;
2298 
2299     /// The "old" function replaced by ReplacementFn.
2300     const Function &ReplacedFn;
2301 
2302     /// The "old" argument replaced by new ones defined via ReplacementTypes.
2303     const Argument &ReplacedArg;
2304 
2305     /// The types of the arguments replacing ReplacedArg.
2306     const SmallVector<Type *, 8> ReplacementTypes;
2307 
2308     /// Callee repair callback, see CalleeRepairCBTy.
2309     const CalleeRepairCBTy CalleeRepairCB;
2310 
2311     /// Abstract call site (ACS) repair callback, see ACSRepairCBTy.
2312     const ACSRepairCBTy ACSRepairCB;
2313 
2314     /// Allow access to the private members from the Attributor.
2315     friend struct Attributor;
2316   };
2317 
2318   /// Check if we can rewrite a function signature.
2319   ///
2320   /// The argument \p Arg is replaced with new ones defined by the number,
2321   /// order, and types in \p ReplacementTypes.
2322   ///
2323   /// \returns True, if the replacement can be registered, via
2324   /// registerFunctionSignatureRewrite, false otherwise.
2325   LLVM_ABI bool
2326   isValidFunctionSignatureRewrite(Argument &Arg,
2327                                   ArrayRef<Type *> ReplacementTypes);
2328 
2329   /// Register a rewrite for a function signature.
2330   ///
2331   /// The argument \p Arg is replaced with new ones defined by the number,
2332   /// order, and types in \p ReplacementTypes. The rewiring at the call sites is
2333   /// done through \p ACSRepairCB and at the callee site through
2334   /// \p CalleeRepairCB.
2335   ///
2336   /// \returns True, if the replacement was registered, false otherwise.
2337   LLVM_ABI bool registerFunctionSignatureRewrite(
2338       Argument &Arg, ArrayRef<Type *> ReplacementTypes,
2339       ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
2340       ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB);
2341 
2342   /// Check \p Pred on all function call sites.
2343   ///
2344   /// This method will evaluate \p Pred on call sites and return
2345   /// true if \p Pred holds in every call sites. However, this is only possible
2346   /// all call sites are known, hence the function has internal linkage.
2347   /// If true is returned, \p UsedAssumedInformation is set if assumed
2348   /// information was used to skip or simplify potential call sites.
2349   LLVM_ABI bool checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
2350                                      const AbstractAttribute &QueryingAA,
2351                                      bool RequireAllCallSites,
2352                                      bool &UsedAssumedInformation);
2353 
2354   /// Check \p Pred on all call sites of \p Fn.
2355   ///
2356   /// This method will evaluate \p Pred on call sites and return
2357   /// true if \p Pred holds in every call sites. However, this is only possible
2358   /// all call sites are known, hence the function has internal linkage.
2359   /// If true is returned, \p UsedAssumedInformation is set if assumed
2360   /// information was used to skip or simplify potential call sites.
2361   LLVM_ABI bool checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
2362                                      const Function &Fn,
2363                                      bool RequireAllCallSites,
2364                                      const AbstractAttribute *QueryingAA,
2365                                      bool &UsedAssumedInformation,
2366                                      bool CheckPotentiallyDead = false);
2367 
2368   /// Check \p Pred on all values potentially returned by the function
2369   /// associated with \p QueryingAA.
2370   ///
2371   /// This is the context insensitive version of the method above.
2372   LLVM_ABI bool
2373   checkForAllReturnedValues(function_ref<bool(Value &)> Pred,
2374                             const AbstractAttribute &QueryingAA,
2375                             AA::ValueScope S = AA::ValueScope::Intraprocedural,
2376                             bool RecurseForSelectAndPHI = true);
2377 
2378   /// Check \p Pred on all instructions in \p Fn with an opcode present in
2379   /// \p Opcodes.
2380   ///
2381   /// This method will evaluate \p Pred on all instructions with an opcode
2382   /// present in \p Opcode and return true if \p Pred holds on all of them.
2383   LLVM_ABI bool checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
2384                                         const Function *Fn,
2385                                         const AbstractAttribute *QueryingAA,
2386                                         ArrayRef<unsigned> Opcodes,
2387                                         bool &UsedAssumedInformation,
2388                                         bool CheckBBLivenessOnly = false,
2389                                         bool CheckPotentiallyDead = false);
2390 
2391   /// Check \p Pred on all instructions with an opcode present in \p Opcodes.
2392   ///
2393   /// This method will evaluate \p Pred on all instructions with an opcode
2394   /// present in \p Opcode and return true if \p Pred holds on all of them.
2395   LLVM_ABI bool checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
2396                                         const AbstractAttribute &QueryingAA,
2397                                         ArrayRef<unsigned> Opcodes,
2398                                         bool &UsedAssumedInformation,
2399                                         bool CheckBBLivenessOnly = false,
2400                                         bool CheckPotentiallyDead = false);
2401 
2402   /// Check \p Pred on all call-like instructions (=CallBased derived).
2403   ///
2404   /// See checkForAllCallLikeInstructions(...) for more information.
2405   bool checkForAllCallLikeInstructions(function_ref<bool(Instruction &)> Pred,
2406                                        const AbstractAttribute &QueryingAA,
2407                                        bool &UsedAssumedInformation,
2408                                        bool CheckBBLivenessOnly = false,
2409                                        bool CheckPotentiallyDead = false) {
2410     return checkForAllInstructions(
2411         Pred, QueryingAA,
2412         {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
2413          (unsigned)Instruction::Call},
2414         UsedAssumedInformation, CheckBBLivenessOnly, CheckPotentiallyDead);
2415   }
2416 
2417   /// Check \p Pred on all Read/Write instructions.
2418   ///
2419   /// This method will evaluate \p Pred on all instructions that read or write
2420   /// to memory present in the information cache and return true if \p Pred
2421   /// holds on all of them.
2422   LLVM_ABI bool
2423   checkForAllReadWriteInstructions(function_ref<bool(Instruction &)> Pred,
2424                                    AbstractAttribute &QueryingAA,
2425                                    bool &UsedAssumedInformation);
2426 
2427   /// Create a shallow wrapper for \p F such that \p F has internal linkage
2428   /// afterwards. It also sets the original \p F 's name to anonymous
2429   ///
2430   /// A wrapper is a function with the same type (and attributes) as \p F
2431   /// that will only call \p F and return the result, if any.
2432   ///
2433   /// Assuming the declaration of looks like:
2434   ///   rty F(aty0 arg0, ..., atyN argN);
2435   ///
2436   /// The wrapper will then look as follows:
2437   ///   rty wrapper(aty0 arg0, ..., atyN argN) {
2438   ///     return F(arg0, ..., argN);
2439   ///   }
2440   ///
2441   LLVM_ABI static void createShallowWrapper(Function &F);
2442 
2443   /// Returns true if the function \p F can be internalized. i.e. it has a
2444   /// compatible linkage.
2445   LLVM_ABI static bool isInternalizable(Function &F);
2446 
2447   /// Make another copy of the function \p F such that the copied version has
2448   /// internal linkage afterwards and can be analysed. Then we replace all uses
2449   /// of the original function to the copied one
2450   ///
2451   /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
2452   /// linkage can be internalized because these linkages guarantee that other
2453   /// definitions with the same name have the same semantics as this one.
2454   ///
2455   /// This will only be run if the `attributor-allow-deep-wrappers` option is
2456   /// set, or if the function is called with \p Force set to true.
2457   ///
2458   /// If the function \p F failed to be internalized the return value will be a
2459   /// null pointer.
2460   LLVM_ABI static Function *internalizeFunction(Function &F,
2461                                                 bool Force = false);
2462 
2463   /// Make copies of each function in the set \p FnSet such that the copied
2464   /// version has internal linkage afterwards and can be analysed. Then we
2465   /// replace all uses of the original function to the copied one. The map
2466   /// \p FnMap contains a mapping of functions to their internalized versions.
2467   ///
2468   /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
2469   /// linkage can be internalized because these linkages guarantee that other
2470   /// definitions with the same name have the same semantics as this one.
2471   ///
2472   /// This version will internalize all the functions in the set \p FnSet at
2473   /// once and then replace the uses. This prevents internalized functions being
2474   /// called by external functions when there is an internalized version in the
2475   /// module.
2476   LLVM_ABI static bool
2477   internalizeFunctions(SmallPtrSetImpl<Function *> &FnSet,
2478                        DenseMap<Function *, Function *> &FnMap);
2479 
2480   /// Return the data layout associated with the anchor scope.
2481   const DataLayout &getDataLayout() const { return InfoCache.DL; }
2482 
2483   /// The allocator used to allocate memory, e.g. for `AbstractAttribute`s.
2484   BumpPtrAllocator &Allocator;
2485 
2486   const SmallSetVector<Function *, 8> &getModifiedFunctions() {
2487     return CGModifiedFunctions;
2488   }
2489 
2490 private:
2491   /// This method will do fixpoint iteration until fixpoint or the
2492   /// maximum iteration count is reached.
2493   ///
2494   /// If the maximum iteration count is reached, This method will
2495   /// indicate pessimistic fixpoint on attributes that transitively depend
2496   /// on attributes that were scheduled for an update.
2497   void runTillFixpoint();
2498 
2499   /// Gets called after scheduling, manifests attributes to the LLVM IR.
2500   ChangeStatus manifestAttributes();
2501 
2502   /// Gets called after attributes have been manifested, cleans up the IR.
2503   /// Deletes dead functions, blocks and instructions.
2504   /// Rewrites function signitures and updates the call graph.
2505   ChangeStatus cleanupIR();
2506 
2507   /// Identify internal functions that are effectively dead, thus not reachable
2508   /// from a live entry point. The functions are added to ToBeDeletedFunctions.
2509   void identifyDeadInternalFunctions();
2510 
2511   /// Run `::update` on \p AA and track the dependences queried while doing so.
2512   /// Also adjust the state if we know further updates are not necessary.
2513   LLVM_ABI ChangeStatus updateAA(AbstractAttribute &AA);
2514 
2515   /// Remember the dependences on the top of the dependence stack such that they
2516   /// may trigger further updates. (\see DependenceStack)
2517   void rememberDependences();
2518 
2519   /// Determine if CallBase context in \p IRP should be propagated.
2520   LLVM_ABI bool shouldPropagateCallBaseContext(const IRPosition &IRP);
2521 
2522   /// Apply all requested function signature rewrites
2523   /// (\see registerFunctionSignatureRewrite) and return Changed if the module
2524   /// was altered.
2525   ChangeStatus
2526   rewriteFunctionSignatures(SmallSetVector<Function *, 8> &ModifiedFns);
2527 
2528   /// Check if the Attribute \p AA should be seeded.
2529   /// See getOrCreateAAFor.
2530   LLVM_ABI bool shouldSeedAttribute(AbstractAttribute &AA);
2531 
2532   /// A nested map to lookup abstract attributes based on the argument position
2533   /// on the outer level, and the addresses of the static member (AAType::ID) on
2534   /// the inner level.
2535   ///{
2536   using AAMapKeyTy = std::pair<const char *, IRPosition>;
2537   DenseMap<AAMapKeyTy, AbstractAttribute *> AAMap;
2538   ///}
2539 
2540   /// Map to remember all requested signature changes (= argument replacements).
2541   DenseMap<Function *, SmallVector<std::unique_ptr<ArgumentReplacementInfo>, 8>>
2542       ArgumentReplacementMap;
2543 
2544   /// The set of functions we are deriving attributes for.
2545   SetVector<Function *> &Functions;
2546 
2547   /// The information cache that holds pre-processed (LLVM-IR) information.
2548   InformationCache &InfoCache;
2549 
2550   /// Abstract Attribute dependency graph
2551   AADepGraph DG;
2552 
2553   /// Set of functions for which we modified the content such that it might
2554   /// impact the call graph.
2555   SmallSetVector<Function *, 8> CGModifiedFunctions;
2556 
2557   /// Information about a dependence. If FromAA is changed ToAA needs to be
2558   /// updated as well.
2559   struct DepInfo {
2560     const AbstractAttribute *FromAA;
2561     const AbstractAttribute *ToAA;
2562     DepClassTy DepClass;
2563   };
2564 
2565   /// The dependence stack is used to track dependences during an
2566   /// `AbstractAttribute::update` call. As `AbstractAttribute::update` can be
2567   /// recursive we might have multiple vectors of dependences in here. The stack
2568   /// size, should be adjusted according to the expected recursion depth and the
2569   /// inner dependence vector size to the expected number of dependences per
2570   /// abstract attribute. Since the inner vectors are actually allocated on the
2571   /// stack we can be generous with their size.
2572   using DependenceVector = SmallVector<DepInfo, 8>;
2573   SmallVector<DependenceVector *, 16> DependenceStack;
2574 
2575   /// A set to remember the functions we already assume to be live and visited.
2576   DenseSet<const Function *> VisitedFunctions;
2577 
2578   /// Uses we replace with a new value after manifest is done. We will remove
2579   /// then trivially dead instructions as well.
2580   SmallMapVector<Use *, Value *, 32> ToBeChangedUses;
2581 
2582   /// Values we replace with a new value after manifest is done. We will remove
2583   /// then trivially dead instructions as well.
2584   SmallMapVector<Value *, PointerIntPair<Value *, 1, bool>, 32>
2585       ToBeChangedValues;
2586 
2587   /// Instructions we replace with `unreachable` insts after manifest is done.
2588   SmallSetVector<WeakVH, 16> ToBeChangedToUnreachableInsts;
2589 
2590   /// Invoke instructions with at least a single dead successor block.
2591   SmallSetVector<WeakVH, 16> InvokeWithDeadSuccessor;
2592 
2593   /// A flag that indicates which stage of the process we are in. Initially, the
2594   /// phase is SEEDING. Phase is changed in `Attributor::run()`
2595   enum class AttributorPhase {
2596     SEEDING,
2597     UPDATE,
2598     MANIFEST,
2599     CLEANUP,
2600   } Phase = AttributorPhase::SEEDING;
2601 
2602   /// The current initialization chain length. Tracked to avoid stack overflows.
2603   unsigned InitializationChainLength = 0;
2604 
2605   /// Functions, blocks, and instructions we delete after manifest is done.
2606   ///
2607   ///{
2608   SmallPtrSet<BasicBlock *, 8> ManifestAddedBlocks;
2609   SmallSetVector<Function *, 8> ToBeDeletedFunctions;
2610   SmallSetVector<BasicBlock *, 8> ToBeDeletedBlocks;
2611   SmallSetVector<WeakVH, 8> ToBeDeletedInsts;
2612   ///}
2613 
2614   /// Container with all the query AAs that requested an update via
2615   /// registerForUpdate.
2616   SmallSetVector<AbstractAttribute *, 16> QueryAAsAwaitingUpdate;
2617 
2618   /// User provided configuration for this Attributor instance.
2619   const AttributorConfig Configuration;
2620 
2621   friend AADepGraph;
2622   friend AttributorCallGraph;
2623 };
2624 
2625 /// An interface to query the internal state of an abstract attribute.
2626 ///
2627 /// The abstract state is a minimal interface that allows the Attributor to
2628 /// communicate with the abstract attributes about their internal state without
2629 /// enforcing or exposing implementation details, e.g., the (existence of an)
2630 /// underlying lattice.
2631 ///
2632 /// It is sufficient to be able to query if a state is (1) valid or invalid, (2)
2633 /// at a fixpoint, and to indicate to the state that (3) an optimistic fixpoint
2634 /// was reached or (4) a pessimistic fixpoint was enforced.
2635 ///
2636 /// All methods need to be implemented by the subclass. For the common use case,
2637 /// a single boolean state or a bit-encoded state, the BooleanState and
2638 /// {Inc,Dec,Bit}IntegerState classes are already provided. An abstract
2639 /// attribute can inherit from them to get the abstract state interface and
2640 /// additional methods to directly modify the state based if needed. See the
2641 /// class comments for help.
2642 struct AbstractState {
2643   virtual ~AbstractState() = default;
2644 
2645   /// Return if this abstract state is in a valid state. If false, no
2646   /// information provided should be used.
2647   virtual bool isValidState() const = 0;
2648 
2649   /// Return if this abstract state is fixed, thus does not need to be updated
2650   /// if information changes as it cannot change itself.
2651   virtual bool isAtFixpoint() const = 0;
2652 
2653   /// Indicate that the abstract state should converge to the optimistic state.
2654   ///
2655   /// This will usually make the optimistically assumed state the known to be
2656   /// true state.
2657   ///
2658   /// \returns ChangeStatus::UNCHANGED as the assumed value should not change.
2659   virtual ChangeStatus indicateOptimisticFixpoint() = 0;
2660 
2661   /// Indicate that the abstract state should converge to the pessimistic state.
2662   ///
2663   /// This will usually revert the optimistically assumed state to the known to
2664   /// be true state.
2665   ///
2666   /// \returns ChangeStatus::CHANGED as the assumed value may change.
2667   virtual ChangeStatus indicatePessimisticFixpoint() = 0;
2668 };
2669 
2670 /// Simple state with integers encoding.
2671 ///
2672 /// The interface ensures that the assumed bits are always a subset of the known
2673 /// bits. Users can only add known bits and, except through adding known bits,
2674 /// they can only remove assumed bits. This should guarantee monotonicity and
2675 /// thereby the existence of a fixpoint (if used correctly). The fixpoint is
2676 /// reached when the assumed and known state/bits are equal. Users can
2677 /// force/inidicate a fixpoint. If an optimistic one is indicated, the known
2678 /// state will catch up with the assumed one, for a pessimistic fixpoint it is
2679 /// the other way around.
2680 template <typename base_ty, base_ty BestState, base_ty WorstState>
2681 struct IntegerStateBase : public AbstractState {
2682   using base_t = base_ty;
2683 
2684   IntegerStateBase() = default;
2685   IntegerStateBase(base_t Assumed) : Assumed(Assumed) {}
2686 
2687   /// Return the best possible representable state.
2688   static constexpr base_t getBestState() { return BestState; }
2689   static constexpr base_t getBestState(const IntegerStateBase &) {
2690     return getBestState();
2691   }
2692 
2693   /// Return the worst possible representable state.
2694   static constexpr base_t getWorstState() { return WorstState; }
2695   static constexpr base_t getWorstState(const IntegerStateBase &) {
2696     return getWorstState();
2697   }
2698 
2699   /// See AbstractState::isValidState()
2700   /// NOTE: For now we simply pretend that the worst possible state is invalid.
2701   bool isValidState() const override { return Assumed != getWorstState(); }
2702 
2703   /// See AbstractState::isAtFixpoint()
2704   bool isAtFixpoint() const override { return Assumed == Known; }
2705 
2706   /// See AbstractState::indicateOptimisticFixpoint(...)
2707   ChangeStatus indicateOptimisticFixpoint() override {
2708     Known = Assumed;
2709     return ChangeStatus::UNCHANGED;
2710   }
2711 
2712   /// See AbstractState::indicatePessimisticFixpoint(...)
2713   ChangeStatus indicatePessimisticFixpoint() override {
2714     Assumed = Known;
2715     return ChangeStatus::CHANGED;
2716   }
2717 
2718   /// Return the known state encoding
2719   base_t getKnown() const { return Known; }
2720 
2721   /// Return the assumed state encoding.
2722   base_t getAssumed() const { return Assumed; }
2723 
2724   /// Equality for IntegerStateBase.
2725   bool
2726   operator==(const IntegerStateBase<base_t, BestState, WorstState> &R) const {
2727     return this->getAssumed() == R.getAssumed() &&
2728            this->getKnown() == R.getKnown();
2729   }
2730 
2731   /// Inequality for IntegerStateBase.
2732   bool
2733   operator!=(const IntegerStateBase<base_t, BestState, WorstState> &R) const {
2734     return !(*this == R);
2735   }
2736 
2737   /// "Clamp" this state with \p R. The result is subtype dependent but it is
2738   /// intended that only information assumed in both states will be assumed in
2739   /// this one afterwards.
2740   void operator^=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
2741     handleNewAssumedValue(R.getAssumed());
2742   }
2743 
2744   /// "Clamp" this state with \p R. The result is subtype dependent but it is
2745   /// intended that information known in either state will be known in
2746   /// this one afterwards.
2747   void operator+=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
2748     handleNewKnownValue(R.getKnown());
2749   }
2750 
2751   void operator|=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
2752     joinOR(R.getAssumed(), R.getKnown());
2753   }
2754 
2755   void operator&=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
2756     joinAND(R.getAssumed(), R.getKnown());
2757   }
2758 
2759 protected:
2760   /// Handle a new assumed value \p Value. Subtype dependent.
2761   virtual void handleNewAssumedValue(base_t Value) = 0;
2762 
2763   /// Handle a new known value \p Value. Subtype dependent.
2764   virtual void handleNewKnownValue(base_t Value) = 0;
2765 
2766   /// Handle a  value \p Value. Subtype dependent.
2767   virtual void joinOR(base_t AssumedValue, base_t KnownValue) = 0;
2768 
2769   /// Handle a new assumed value \p Value. Subtype dependent.
2770   virtual void joinAND(base_t AssumedValue, base_t KnownValue) = 0;
2771 
2772   /// The known state encoding in an integer of type base_t.
2773   base_t Known = getWorstState();
2774 
2775   /// The assumed state encoding in an integer of type base_t.
2776   base_t Assumed = getBestState();
2777 };
2778 
2779 /// Specialization of the integer state for a bit-wise encoding.
2780 template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
2781           base_ty WorstState = 0>
2782 struct BitIntegerState
2783     : public IntegerStateBase<base_ty, BestState, WorstState> {
2784   using super = IntegerStateBase<base_ty, BestState, WorstState>;
2785   using base_t = base_ty;
2786   BitIntegerState() = default;
2787   BitIntegerState(base_t Assumed) : super(Assumed) {}
2788 
2789   /// Return true if the bits set in \p BitsEncoding are "known bits".
2790   bool isKnown(base_t BitsEncoding = BestState) const {
2791     return (this->Known & BitsEncoding) == BitsEncoding;
2792   }
2793 
2794   /// Return true if the bits set in \p BitsEncoding are "assumed bits".
2795   bool isAssumed(base_t BitsEncoding = BestState) const {
2796     return (this->Assumed & BitsEncoding) == BitsEncoding;
2797   }
2798 
2799   /// Add the bits in \p BitsEncoding to the "known bits".
2800   BitIntegerState &addKnownBits(base_t Bits) {
2801     // Make sure we never miss any "known bits".
2802     this->Assumed |= Bits;
2803     this->Known |= Bits;
2804     return *this;
2805   }
2806 
2807   /// Remove the bits in \p BitsEncoding from the "assumed bits" if not known.
2808   BitIntegerState &removeAssumedBits(base_t BitsEncoding) {
2809     return intersectAssumedBits(~BitsEncoding);
2810   }
2811 
2812   /// Remove the bits in \p BitsEncoding from the "known bits".
2813   BitIntegerState &removeKnownBits(base_t BitsEncoding) {
2814     this->Known = (this->Known & ~BitsEncoding);
2815     return *this;
2816   }
2817 
2818   /// Keep only "assumed bits" also set in \p BitsEncoding but all known ones.
2819   BitIntegerState &intersectAssumedBits(base_t BitsEncoding) {
2820     // Make sure we never lose any "known bits".
2821     this->Assumed = (this->Assumed & BitsEncoding) | this->Known;
2822     return *this;
2823   }
2824 
2825 private:
2826   void handleNewAssumedValue(base_t Value) override {
2827     intersectAssumedBits(Value);
2828   }
2829   void handleNewKnownValue(base_t Value) override { addKnownBits(Value); }
2830   void joinOR(base_t AssumedValue, base_t KnownValue) override {
2831     this->Known |= KnownValue;
2832     this->Assumed |= AssumedValue;
2833   }
2834   void joinAND(base_t AssumedValue, base_t KnownValue) override {
2835     this->Known &= KnownValue;
2836     this->Assumed &= AssumedValue;
2837   }
2838 };
2839 
2840 /// Specialization of the integer state for an increasing value, hence ~0u is
2841 /// the best state and 0 the worst.
2842 template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
2843           base_ty WorstState = 0>
2844 struct IncIntegerState
2845     : public IntegerStateBase<base_ty, BestState, WorstState> {
2846   using super = IntegerStateBase<base_ty, BestState, WorstState>;
2847   using base_t = base_ty;
2848 
2849   IncIntegerState() : super() {}
2850   IncIntegerState(base_t Assumed) : super(Assumed) {}
2851 
2852   /// Return the best possible representable state.
2853   static constexpr base_t getBestState() { return BestState; }
2854   static constexpr base_t
2855   getBestState(const IncIntegerState<base_ty, BestState, WorstState> &) {
2856     return getBestState();
2857   }
2858 
2859   /// Take minimum of assumed and \p Value.
2860   IncIntegerState &takeAssumedMinimum(base_t Value) {
2861     // Make sure we never lose "known value".
2862     this->Assumed = std::max(std::min(this->Assumed, Value), this->Known);
2863     return *this;
2864   }
2865 
2866   /// Take maximum of known and \p Value.
2867   IncIntegerState &takeKnownMaximum(base_t Value) {
2868     // Make sure we never lose "known value".
2869     this->Assumed = std::max(Value, this->Assumed);
2870     this->Known = std::max(Value, this->Known);
2871     return *this;
2872   }
2873 
2874 private:
2875   void handleNewAssumedValue(base_t Value) override {
2876     takeAssumedMinimum(Value);
2877   }
2878   void handleNewKnownValue(base_t Value) override { takeKnownMaximum(Value); }
2879   void joinOR(base_t AssumedValue, base_t KnownValue) override {
2880     this->Known = std::max(this->Known, KnownValue);
2881     this->Assumed = std::max(this->Assumed, AssumedValue);
2882   }
2883   void joinAND(base_t AssumedValue, base_t KnownValue) override {
2884     this->Known = std::min(this->Known, KnownValue);
2885     this->Assumed = std::min(this->Assumed, AssumedValue);
2886   }
2887 };
2888 
2889 /// Specialization of the integer state for a decreasing value, hence 0 is the
2890 /// best state and ~0u the worst.
2891 template <typename base_ty = uint32_t>
2892 struct DecIntegerState : public IntegerStateBase<base_ty, 0, ~base_ty(0)> {
2893   using base_t = base_ty;
2894 
2895   /// Take maximum of assumed and \p Value.
2896   DecIntegerState &takeAssumedMaximum(base_t Value) {
2897     // Make sure we never lose "known value".
2898     this->Assumed = std::min(std::max(this->Assumed, Value), this->Known);
2899     return *this;
2900   }
2901 
2902   /// Take minimum of known and \p Value.
2903   DecIntegerState &takeKnownMinimum(base_t Value) {
2904     // Make sure we never lose "known value".
2905     this->Assumed = std::min(Value, this->Assumed);
2906     this->Known = std::min(Value, this->Known);
2907     return *this;
2908   }
2909 
2910 private:
2911   void handleNewAssumedValue(base_t Value) override {
2912     takeAssumedMaximum(Value);
2913   }
2914   void handleNewKnownValue(base_t Value) override { takeKnownMinimum(Value); }
2915   void joinOR(base_t AssumedValue, base_t KnownValue) override {
2916     this->Assumed = std::min(this->Assumed, KnownValue);
2917     this->Assumed = std::min(this->Assumed, AssumedValue);
2918   }
2919   void joinAND(base_t AssumedValue, base_t KnownValue) override {
2920     this->Assumed = std::max(this->Assumed, KnownValue);
2921     this->Assumed = std::max(this->Assumed, AssumedValue);
2922   }
2923 };
2924 
2925 /// Simple wrapper for a single bit (boolean) state.
2926 struct BooleanState : public IntegerStateBase<bool, true, false> {
2927   using super = IntegerStateBase<bool, true, false>;
2928   using base_t = IntegerStateBase::base_t;
2929 
2930   BooleanState() = default;
2931   BooleanState(base_t Assumed) : super(Assumed) {}
2932 
2933   /// Set the assumed value to \p Value but never below the known one.
2934   void setAssumed(bool Value) { Assumed &= (Known | Value); }
2935 
2936   /// Set the known and asssumed value to \p Value.
2937   void setKnown(bool Value) {
2938     Known |= Value;
2939     Assumed |= Value;
2940   }
2941 
2942   /// Return true if the state is assumed to hold.
2943   bool isAssumed() const { return getAssumed(); }
2944 
2945   /// Return true if the state is known to hold.
2946   bool isKnown() const { return getKnown(); }
2947 
2948 private:
2949   void handleNewAssumedValue(base_t Value) override {
2950     if (!Value)
2951       Assumed = Known;
2952   }
2953   void handleNewKnownValue(base_t Value) override {
2954     if (Value)
2955       Known = (Assumed = Value);
2956   }
2957   void joinOR(base_t AssumedValue, base_t KnownValue) override {
2958     Known |= KnownValue;
2959     Assumed |= AssumedValue;
2960   }
2961   void joinAND(base_t AssumedValue, base_t KnownValue) override {
2962     Known &= KnownValue;
2963     Assumed &= AssumedValue;
2964   }
2965 };
2966 
2967 /// State for an integer range.
2968 struct IntegerRangeState : public AbstractState {
2969 
2970   /// Bitwidth of the associated value.
2971   uint32_t BitWidth;
2972 
2973   /// State representing assumed range, initially set to empty.
2974   ConstantRange Assumed;
2975 
2976   /// State representing known range, initially set to [-inf, inf].
2977   ConstantRange Known;
2978 
2979   IntegerRangeState(uint32_t BitWidth)
2980       : BitWidth(BitWidth), Assumed(ConstantRange::getEmpty(BitWidth)),
2981         Known(ConstantRange::getFull(BitWidth)) {}
2982 
2983   IntegerRangeState(const ConstantRange &CR)
2984       : BitWidth(CR.getBitWidth()), Assumed(CR),
2985         Known(getWorstState(CR.getBitWidth())) {}
2986 
2987   /// Return the worst possible representable state.
2988   static ConstantRange getWorstState(uint32_t BitWidth) {
2989     return ConstantRange::getFull(BitWidth);
2990   }
2991 
2992   /// Return the best possible representable state.
2993   static ConstantRange getBestState(uint32_t BitWidth) {
2994     return ConstantRange::getEmpty(BitWidth);
2995   }
2996   static ConstantRange getBestState(const IntegerRangeState &IRS) {
2997     return getBestState(IRS.getBitWidth());
2998   }
2999 
3000   /// Return associated values' bit width.
3001   uint32_t getBitWidth() const { return BitWidth; }
3002 
3003   /// See AbstractState::isValidState()
3004   bool isValidState() const override {
3005     return BitWidth > 0 && !Assumed.isFullSet();
3006   }
3007 
3008   /// See AbstractState::isAtFixpoint()
3009   bool isAtFixpoint() const override { return Assumed == Known; }
3010 
3011   /// See AbstractState::indicateOptimisticFixpoint(...)
3012   ChangeStatus indicateOptimisticFixpoint() override {
3013     Known = Assumed;
3014     return ChangeStatus::CHANGED;
3015   }
3016 
3017   /// See AbstractState::indicatePessimisticFixpoint(...)
3018   ChangeStatus indicatePessimisticFixpoint() override {
3019     Assumed = Known;
3020     return ChangeStatus::CHANGED;
3021   }
3022 
3023   /// Return the known state encoding
3024   ConstantRange getKnown() const { return Known; }
3025 
3026   /// Return the assumed state encoding.
3027   ConstantRange getAssumed() const { return Assumed; }
3028 
3029   /// Unite assumed range with the passed state.
3030   void unionAssumed(const ConstantRange &R) {
3031     // Don't lose a known range.
3032     Assumed = Assumed.unionWith(R).intersectWith(Known);
3033   }
3034 
3035   /// See IntegerRangeState::unionAssumed(..).
3036   void unionAssumed(const IntegerRangeState &R) {
3037     unionAssumed(R.getAssumed());
3038   }
3039 
3040   /// Intersect known range with the passed state.
3041   void intersectKnown(const ConstantRange &R) {
3042     Assumed = Assumed.intersectWith(R);
3043     Known = Known.intersectWith(R);
3044   }
3045 
3046   /// See IntegerRangeState::intersectKnown(..).
3047   void intersectKnown(const IntegerRangeState &R) {
3048     intersectKnown(R.getKnown());
3049   }
3050 
3051   /// Equality for IntegerRangeState.
3052   bool operator==(const IntegerRangeState &R) const {
3053     return getAssumed() == R.getAssumed() && getKnown() == R.getKnown();
3054   }
3055 
3056   /// "Clamp" this state with \p R. The result is subtype dependent but it is
3057   /// intended that only information assumed in both states will be assumed in
3058   /// this one afterwards.
3059   IntegerRangeState operator^=(const IntegerRangeState &R) {
3060     // NOTE: `^=` operator seems like `intersect` but in this case, we need to
3061     // take `union`.
3062     unionAssumed(R);
3063     return *this;
3064   }
3065 
3066   IntegerRangeState operator&=(const IntegerRangeState &R) {
3067     // NOTE: `&=` operator seems like `intersect` but in this case, we need to
3068     // take `union`.
3069     Known = Known.unionWith(R.getKnown());
3070     Assumed = Assumed.unionWith(R.getAssumed());
3071     return *this;
3072   }
3073 };
3074 
3075 /// Simple state for a set.
3076 ///
3077 /// This represents a state containing a set of values. The interface supports
3078 /// modelling sets that contain all possible elements. The state's internal
3079 /// value is modified using union or intersection operations.
3080 template <typename BaseTy> struct SetState : public AbstractState {
3081   /// A wrapper around a set that has semantics for handling unions and
3082   /// intersections with a "universal" set that contains all elements.
3083   struct SetContents {
3084     /// Creates a universal set with no concrete elements or an empty set.
3085     SetContents(bool Universal) : Universal(Universal) {}
3086 
3087     /// Creates a non-universal set with concrete values.
3088     SetContents(const DenseSet<BaseTy> &Assumptions)
3089         : Universal(false), Set(Assumptions) {}
3090 
3091     SetContents(bool Universal, const DenseSet<BaseTy> &Assumptions)
3092         : Universal(Universal), Set(Assumptions) {}
3093 
3094     const DenseSet<BaseTy> &getSet() const { return Set; }
3095 
3096     bool isUniversal() const { return Universal; }
3097 
3098     bool empty() const { return Set.empty() && !Universal; }
3099 
3100     /// Finds A := A ^ B where A or B could be the "Universal" set which
3101     /// contains every possible attribute. Returns true if changes were made.
3102     bool getIntersection(const SetContents &RHS) {
3103       bool IsUniversal = Universal;
3104       unsigned Size = Set.size();
3105 
3106       // A := A ^ U = A
3107       if (RHS.isUniversal())
3108         return false;
3109 
3110       // A := U ^ B = B
3111       if (Universal)
3112         Set = RHS.getSet();
3113       else
3114         set_intersect(Set, RHS.getSet());
3115 
3116       Universal &= RHS.isUniversal();
3117       return IsUniversal != Universal || Size != Set.size();
3118     }
3119 
3120     /// Finds A := A u B where A or B could be the "Universal" set which
3121     /// contains every possible attribute. returns true if changes were made.
3122     bool getUnion(const SetContents &RHS) {
3123       bool IsUniversal = Universal;
3124       unsigned Size = Set.size();
3125 
3126       // A := A u U = U = U u B
3127       if (!RHS.isUniversal() && !Universal)
3128         set_union(Set, RHS.getSet());
3129 
3130       Universal |= RHS.isUniversal();
3131       return IsUniversal != Universal || Size != Set.size();
3132     }
3133 
3134   private:
3135     /// Indicates if this set is "universal", containing every possible element.
3136     bool Universal;
3137 
3138     /// The set of currently active assumptions.
3139     DenseSet<BaseTy> Set;
3140   };
3141 
3142   SetState() : Known(false), Assumed(true), IsAtFixedpoint(false) {}
3143 
3144   /// Initializes the known state with an initial set and initializes the
3145   /// assumed state as universal.
3146   SetState(const DenseSet<BaseTy> &Known)
3147       : Known(Known), Assumed(true), IsAtFixedpoint(false) {}
3148 
3149   /// See AbstractState::isValidState()
3150   bool isValidState() const override { return !Assumed.empty(); }
3151 
3152   /// See AbstractState::isAtFixpoint()
3153   bool isAtFixpoint() const override { return IsAtFixedpoint; }
3154 
3155   /// See AbstractState::indicateOptimisticFixpoint(...)
3156   ChangeStatus indicateOptimisticFixpoint() override {
3157     IsAtFixedpoint = true;
3158     Known = Assumed;
3159     return ChangeStatus::UNCHANGED;
3160   }
3161 
3162   /// See AbstractState::indicatePessimisticFixpoint(...)
3163   ChangeStatus indicatePessimisticFixpoint() override {
3164     IsAtFixedpoint = true;
3165     Assumed = Known;
3166     return ChangeStatus::CHANGED;
3167   }
3168 
3169   /// Return the known state encoding.
3170   const SetContents &getKnown() const { return Known; }
3171 
3172   /// Return the assumed state encoding.
3173   const SetContents &getAssumed() const { return Assumed; }
3174 
3175   /// Returns if the set state contains the element.
3176   bool setContains(const BaseTy &Elem) const {
3177     return Assumed.getSet().contains(Elem) || Known.getSet().contains(Elem);
3178   }
3179 
3180   /// Performs the set intersection between this set and \p RHS. Returns true if
3181   /// changes were made.
3182   bool getIntersection(const SetContents &RHS) {
3183     bool IsUniversal = Assumed.isUniversal();
3184     unsigned SizeBefore = Assumed.getSet().size();
3185 
3186     // Get intersection and make sure that the known set is still a proper
3187     // subset of the assumed set. A := K u (A ^ R).
3188     Assumed.getIntersection(RHS);
3189     Assumed.getUnion(Known);
3190 
3191     return SizeBefore != Assumed.getSet().size() ||
3192            IsUniversal != Assumed.isUniversal();
3193   }
3194 
3195   /// Performs the set union between this set and \p RHS. Returns true if
3196   /// changes were made.
3197   bool getUnion(const SetContents &RHS) { return Assumed.getUnion(RHS); }
3198 
3199 private:
3200   /// The set of values known for this state.
3201   SetContents Known;
3202 
3203   /// The set of assumed values for this state.
3204   SetContents Assumed;
3205 
3206   bool IsAtFixedpoint;
3207 };
3208 
3209 /// Helper to tie a abstract state implementation to an abstract attribute.
3210 template <typename StateTy, typename BaseType, class... Ts>
3211 struct StateWrapper : public BaseType, public StateTy {
3212   /// Provide static access to the type of the state.
3213   using StateType = StateTy;
3214 
3215   StateWrapper(const IRPosition &IRP, Ts... Args)
3216       : BaseType(IRP), StateTy(Args...) {}
3217 
3218   /// See AbstractAttribute::getState(...).
3219   StateType &getState() override { return *this; }
3220 
3221   /// See AbstractAttribute::getState(...).
3222   const StateType &getState() const override { return *this; }
3223 };
3224 
3225 /// Helper class that provides common functionality to manifest IR attributes.
3226 template <Attribute::AttrKind AK, typename BaseType, typename AAType>
3227 struct IRAttribute : public BaseType {
3228   IRAttribute(const IRPosition &IRP) : BaseType(IRP) {}
3229 
3230   /// Most boolean IRAttribute AAs don't do anything non-trivial
3231   /// in their initializers while non-boolean ones often do. Subclasses can
3232   /// change this.
3233   static bool hasTrivialInitializer() { return Attribute::isEnumAttrKind(AK); }
3234 
3235   /// Compile time access to the IR attribute kind.
3236   static constexpr Attribute::AttrKind IRAttributeKind = AK;
3237 
3238   /// Return true if the IR attribute(s) associated with this AA are implied for
3239   /// an undef value.
3240   static bool isImpliedByUndef() { return true; }
3241 
3242   /// Return true if the IR attribute(s) associated with this AA are implied for
3243   /// an poison value.
3244   static bool isImpliedByPoison() { return true; }
3245 
3246   static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3247                             Attribute::AttrKind ImpliedAttributeKind = AK,
3248                             bool IgnoreSubsumingPositions = false) {
3249     if (AAType::isImpliedByUndef() && isa<UndefValue>(IRP.getAssociatedValue()))
3250       return true;
3251     if (AAType::isImpliedByPoison() &&
3252         isa<PoisonValue>(IRP.getAssociatedValue()))
3253       return true;
3254     return A.hasAttr(IRP, {ImpliedAttributeKind}, IgnoreSubsumingPositions,
3255                      ImpliedAttributeKind);
3256   }
3257 
3258   /// See AbstractAttribute::manifest(...).
3259   ChangeStatus manifest(Attributor &A) override {
3260     if (isa<UndefValue>(this->getIRPosition().getAssociatedValue()))
3261       return ChangeStatus::UNCHANGED;
3262     SmallVector<Attribute, 4> DeducedAttrs;
3263     getDeducedAttributes(A, this->getAnchorValue().getContext(), DeducedAttrs);
3264     if (DeducedAttrs.empty())
3265       return ChangeStatus::UNCHANGED;
3266     return A.manifestAttrs(this->getIRPosition(), DeducedAttrs);
3267   }
3268 
3269   /// Return the kind that identifies the abstract attribute implementation.
3270   Attribute::AttrKind getAttrKind() const { return AK; }
3271 
3272   /// Return the deduced attributes in \p Attrs.
3273   virtual void getDeducedAttributes(Attributor &A, LLVMContext &Ctx,
3274                                     SmallVectorImpl<Attribute> &Attrs) const {
3275     Attrs.emplace_back(Attribute::get(Ctx, getAttrKind()));
3276   }
3277 };
3278 
3279 /// Base struct for all "concrete attribute" deductions.
3280 ///
3281 /// The abstract attribute is a minimal interface that allows the Attributor to
3282 /// orchestrate the abstract/fixpoint analysis. The design allows to hide away
3283 /// implementation choices made for the subclasses but also to structure their
3284 /// implementation and simplify the use of other abstract attributes in-flight.
3285 ///
3286 /// To allow easy creation of new attributes, most methods have default
3287 /// implementations. The ones that do not are generally straight forward, except
3288 /// `AbstractAttribute::updateImpl` which is the location of most reasoning
3289 /// associated with the abstract attribute. The update is invoked by the
3290 /// Attributor in case the situation used to justify the current optimistic
3291 /// state might have changed. The Attributor determines this automatically
3292 /// by monitoring the `Attributor::getAAFor` calls made by abstract attributes.
3293 ///
3294 /// The `updateImpl` method should inspect the IR and other abstract attributes
3295 /// in-flight to justify the best possible (=optimistic) state. The actual
3296 /// implementation is, similar to the underlying abstract state encoding, not
3297 /// exposed. In the most common case, the `updateImpl` will go through a list of
3298 /// reasons why its optimistic state is valid given the current information. If
3299 /// any combination of them holds and is sufficient to justify the current
3300 /// optimistic state, the method shall return UNCHAGED. If not, the optimistic
3301 /// state is adjusted to the situation and the method shall return CHANGED.
3302 ///
3303 /// If the manifestation of the "concrete attribute" deduced by the subclass
3304 /// differs from the "default" behavior, which is a (set of) LLVM-IR
3305 /// attribute(s) for an argument, call site argument, function return value, or
3306 /// function, the `AbstractAttribute::manifest` method should be overloaded.
3307 ///
3308 /// NOTE: If the state obtained via getState() is INVALID, thus if
3309 ///       AbstractAttribute::getState().isValidState() returns false, no
3310 ///       information provided by the methods of this class should be used.
3311 /// NOTE: The Attributor currently has certain limitations to what we can do.
3312 ///       As a general rule of thumb, "concrete" abstract attributes should *for
3313 ///       now* only perform "backward" information propagation. That means
3314 ///       optimistic information obtained through abstract attributes should
3315 ///       only be used at positions that precede the origin of the information
3316 ///       with regards to the program flow. More practically, information can
3317 ///       *now* be propagated from instructions to their enclosing function, but
3318 ///       *not* from call sites to the called function. The mechanisms to allow
3319 ///       both directions will be added in the future.
3320 /// NOTE: The mechanics of adding a new "concrete" abstract attribute are
3321 ///       described in the file comment.
3322 struct LLVM_ABI AbstractAttribute : public IRPosition, public AADepGraphNode {
3323   using StateType = AbstractState;
3324 
3325   AbstractAttribute(const IRPosition &IRP) : IRPosition(IRP) {}
3326 
3327   /// Virtual destructor.
3328   virtual ~AbstractAttribute() = default;
3329 
3330   /// Compile time access to the IR attribute kind.
3331   static constexpr Attribute::AttrKind IRAttributeKind = Attribute::None;
3332 
3333   /// This function is used to identify if an \p DGN is of type
3334   /// AbstractAttribute so that the dyn_cast and cast can use such information
3335   /// to cast an AADepGraphNode to an AbstractAttribute.
3336   ///
3337   /// We eagerly return true here because all AADepGraphNodes except for the
3338   /// Synthethis Node are of type AbstractAttribute
3339   static bool classof(const AADepGraphNode *DGN) { return true; }
3340 
3341   /// Return false if this AA does anything non-trivial (hence not done by
3342   /// default) in its initializer.
3343   static bool hasTrivialInitializer() { return false; }
3344 
3345   /// Return true if this AA requires a "callee" (or an associted function) for
3346   /// a call site positon. Default is optimistic to minimize AAs.
3347   static bool requiresCalleeForCallBase() { return false; }
3348 
3349   /// Return true if this AA requires non-asm "callee" for a call site positon.
3350   static bool requiresNonAsmForCallBase() { return true; }
3351 
3352   /// Return true if this AA requires all callees for an argument or function
3353   /// positon.
3354   static bool requiresCallersForArgOrFunction() { return false; }
3355 
3356   /// Return false if an AA should not be created for \p IRP.
3357   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
3358     return true;
3359   }
3360 
3361   /// Return false if an AA should not be updated for \p IRP.
3362   static bool isValidIRPositionForUpdate(Attributor &A, const IRPosition &IRP) {
3363     Function *AssociatedFn = IRP.getAssociatedFunction();
3364     bool IsFnInterface = IRP.isFnInterfaceKind();
3365     assert((!IsFnInterface || AssociatedFn) &&
3366            "Function interface without a function?");
3367 
3368     // TODO: Not all attributes require an exact definition. Find a way to
3369     //       enable deduction for some but not all attributes in case the
3370     //       definition might be changed at runtime, see also
3371     //       http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html.
3372     // TODO: We could always determine abstract attributes and if sufficient
3373     //       information was found we could duplicate the functions that do not
3374     //       have an exact definition.
3375     return !IsFnInterface || A.isFunctionIPOAmendable(*AssociatedFn);
3376   }
3377 
3378   /// Initialize the state with the information in the Attributor \p A.
3379   ///
3380   /// This function is called by the Attributor once all abstract attributes
3381   /// have been identified. It can and shall be used for task like:
3382   ///  - identify existing knowledge in the IR and use it for the "known state"
3383   ///  - perform any work that is not going to change over time, e.g., determine
3384   ///    a subset of the IR, or attributes in-flight, that have to be looked at
3385   ///    in the `updateImpl` method.
3386   virtual void initialize(Attributor &A) {}
3387 
3388   /// A query AA is always scheduled as long as we do updates because it does
3389   /// lazy computation that cannot be determined to be done from the outside.
3390   /// However, while query AAs will not be fixed if they do not have outstanding
3391   /// dependences, we will only schedule them like other AAs. If a query AA that
3392   /// received a new query it needs to request an update via
3393   /// `Attributor::requestUpdateForAA`.
3394   virtual bool isQueryAA() const { return false; }
3395 
3396   /// Return the internal abstract state for inspection.
3397   virtual StateType &getState() = 0;
3398   virtual const StateType &getState() const = 0;
3399 
3400   /// Return an IR position, see struct IRPosition.
3401   const IRPosition &getIRPosition() const { return *this; };
3402   IRPosition &getIRPosition() { return *this; };
3403 
3404   /// Helper functions, for debug purposes only.
3405   ///{
3406   void print(raw_ostream &OS) const { print(nullptr, OS); }
3407   void print(Attributor *, raw_ostream &OS) const override;
3408   virtual void printWithDeps(raw_ostream &OS) const;
3409   void dump() const { this->print(dbgs()); }
3410 
3411   /// This function should return the "summarized" assumed state as string.
3412   virtual const std::string getAsStr(Attributor *A) const = 0;
3413 
3414   /// This function should return the name of the AbstractAttribute
3415   virtual StringRef getName() const = 0;
3416 
3417   /// This function should return the address of the ID of the AbstractAttribute
3418   virtual const char *getIdAddr() const = 0;
3419   ///}
3420 
3421   /// Allow the Attributor access to the protected methods.
3422   friend struct Attributor;
3423 
3424 protected:
3425   /// Hook for the Attributor to trigger an update of the internal state.
3426   ///
3427   /// If this attribute is already fixed, this method will return UNCHANGED,
3428   /// otherwise it delegates to `AbstractAttribute::updateImpl`.
3429   ///
3430   /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
3431   ChangeStatus update(Attributor &A);
3432 
3433   /// Hook for the Attributor to trigger the manifestation of the information
3434   /// represented by the abstract attribute in the LLVM-IR.
3435   ///
3436   /// \Return CHANGED if the IR was altered, otherwise UNCHANGED.
3437   virtual ChangeStatus manifest(Attributor &A) {
3438     return ChangeStatus::UNCHANGED;
3439   }
3440 
3441   /// Hook to enable custom statistic tracking, called after manifest that
3442   /// resulted in a change if statistics are enabled.
3443   ///
3444   /// We require subclasses to provide an implementation so we remember to
3445   /// add statistics for them.
3446   virtual void trackStatistics() const = 0;
3447 
3448   /// The actual update/transfer function which has to be implemented by the
3449   /// derived classes.
3450   ///
3451   /// If it is called, the environment has changed and we have to determine if
3452   /// the current information is still valid or adjust it otherwise.
3453   ///
3454   /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
3455   virtual ChangeStatus updateImpl(Attributor &A) = 0;
3456 };
3457 
3458 /// Forward declarations of output streams for debug purposes.
3459 ///
3460 ///{
3461 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const AbstractAttribute &AA);
3462 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, ChangeStatus S);
3463 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, IRPosition::Kind);
3464 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const IRPosition &);
3465 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const AbstractState &State);
3466 template <typename base_ty, base_ty BestState, base_ty WorstState>
3467 raw_ostream &
3468 operator<<(raw_ostream &OS,
3469            const IntegerStateBase<base_ty, BestState, WorstState> &S) {
3470   return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
3471             << static_cast<const AbstractState &>(S);
3472 }
3473 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS,
3474                                  const IntegerRangeState &State);
3475 ///}
3476 
3477 struct AttributorPass : public PassInfoMixin<AttributorPass> {
3478   LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
3479 };
3480 struct AttributorCGSCCPass : public PassInfoMixin<AttributorCGSCCPass> {
3481   LLVM_ABI PreservedAnalyses run(LazyCallGraph::SCC &C,
3482                                  CGSCCAnalysisManager &AM, LazyCallGraph &CG,
3483                                  CGSCCUpdateResult &UR);
3484 };
3485 
3486 /// A more lightweight version of the Attributor which only runs attribute
3487 /// inference but no simplifications.
3488 struct AttributorLightPass : public PassInfoMixin<AttributorLightPass> {
3489   LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
3490 };
3491 
3492 /// A more lightweight version of the Attributor which only runs attribute
3493 /// inference but no simplifications.
3494 struct AttributorLightCGSCCPass
3495     : public PassInfoMixin<AttributorLightCGSCCPass> {
3496   LLVM_ABI PreservedAnalyses run(LazyCallGraph::SCC &C,
3497                                  CGSCCAnalysisManager &AM, LazyCallGraph &CG,
3498                                  CGSCCUpdateResult &UR);
3499 };
3500 
3501 /// Helper function to clamp a state \p S of type \p StateType with the
3502 /// information in \p R and indicate/return if \p S did change (as-in update is
3503 /// required to be run again).
3504 template <typename StateType>
3505 ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
3506   auto Assumed = S.getAssumed();
3507   S ^= R;
3508   return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
3509                                    : ChangeStatus::CHANGED;
3510 }
3511 
3512 /// ----------------------------------------------------------------------------
3513 ///                       Abstract Attribute Classes
3514 /// ----------------------------------------------------------------------------
3515 
3516 struct AANoUnwind
3517     : public IRAttribute<Attribute::NoUnwind,
3518                          StateWrapper<BooleanState, AbstractAttribute>,
3519                          AANoUnwind> {
3520   AANoUnwind(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3521 
3522   /// Returns true if nounwind is assumed.
3523   bool isAssumedNoUnwind() const { return getAssumed(); }
3524 
3525   /// Returns true if nounwind is known.
3526   bool isKnownNoUnwind() const { return getKnown(); }
3527 
3528   /// Create an abstract attribute view for the position \p IRP.
3529   LLVM_ABI static AANoUnwind &createForPosition(const IRPosition &IRP,
3530                                                 Attributor &A);
3531 
3532   /// See AbstractAttribute::getName()
3533   StringRef getName() const override { return "AANoUnwind"; }
3534 
3535   /// See AbstractAttribute::getIdAddr()
3536   const char *getIdAddr() const override { return &ID; }
3537 
3538   /// This function should return true if the type of the \p AA is AANoUnwind
3539   static bool classof(const AbstractAttribute *AA) {
3540     return (AA->getIdAddr() == &ID);
3541   }
3542 
3543   /// Unique ID (due to the unique address)
3544   LLVM_ABI static const char ID;
3545 };
3546 
3547 struct AANoSync
3548     : public IRAttribute<Attribute::NoSync,
3549                          StateWrapper<BooleanState, AbstractAttribute>,
3550                          AANoSync> {
3551   AANoSync(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3552 
3553   static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3554                             Attribute::AttrKind ImpliedAttributeKind,
3555                             bool IgnoreSubsumingPositions = false) {
3556     // Note: This is also run for non-IPO amendable functions.
3557     assert(ImpliedAttributeKind == Attribute::NoSync);
3558     if (A.hasAttr(IRP, {Attribute::NoSync}, IgnoreSubsumingPositions,
3559                   Attribute::NoSync))
3560       return true;
3561 
3562     // Check for readonly + non-convergent.
3563     // TODO: We should be able to use hasAttr for Attributes, not only
3564     // AttrKinds.
3565     Function *F = IRP.getAssociatedFunction();
3566     if (!F || F->isConvergent())
3567       return false;
3568 
3569     SmallVector<Attribute, 2> Attrs;
3570     A.getAttrs(IRP, {Attribute::Memory}, Attrs, IgnoreSubsumingPositions);
3571 
3572     MemoryEffects ME = MemoryEffects::unknown();
3573     for (const Attribute &Attr : Attrs)
3574       ME &= Attr.getMemoryEffects();
3575 
3576     if (!ME.onlyReadsMemory())
3577       return false;
3578 
3579     A.manifestAttrs(IRP, Attribute::get(F->getContext(), Attribute::NoSync));
3580     return true;
3581   }
3582 
3583   /// See AbstractAttribute::isValidIRPositionForInit
3584   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
3585     if (!IRP.isFunctionScope() &&
3586         !IRP.getAssociatedType()->isPtrOrPtrVectorTy())
3587       return false;
3588     return IRAttribute::isValidIRPositionForInit(A, IRP);
3589   }
3590 
3591   /// Returns true if "nosync" is assumed.
3592   bool isAssumedNoSync() const { return getAssumed(); }
3593 
3594   /// Returns true if "nosync" is known.
3595   bool isKnownNoSync() const { return getKnown(); }
3596 
3597   /// Helper function used to determine whether an instruction is non-relaxed
3598   /// atomic. In other words, if an atomic instruction does not have unordered
3599   /// or monotonic ordering
3600   LLVM_ABI static bool isNonRelaxedAtomic(const Instruction *I);
3601 
3602   /// Helper function specific for intrinsics which are potentially volatile.
3603   LLVM_ABI static bool isNoSyncIntrinsic(const Instruction *I);
3604 
3605   /// Helper function to determine if \p CB is an aligned (GPU) barrier. Aligned
3606   /// barriers have to be executed by all threads. The flag \p ExecutedAligned
3607   /// indicates if the call is executed by all threads in a (thread) block in an
3608   /// aligned way. If that is the case, non-aligned barriers are effectively
3609   /// aligned barriers.
3610   LLVM_ABI static bool isAlignedBarrier(const CallBase &CB,
3611                                         bool ExecutedAligned);
3612 
3613   /// Create an abstract attribute view for the position \p IRP.
3614   LLVM_ABI static AANoSync &createForPosition(const IRPosition &IRP,
3615                                               Attributor &A);
3616 
3617   /// See AbstractAttribute::getName()
3618   StringRef getName() const override { return "AANoSync"; }
3619 
3620   /// See AbstractAttribute::getIdAddr()
3621   const char *getIdAddr() const override { return &ID; }
3622 
3623   /// This function should return true if the type of the \p AA is AANoSync
3624   static bool classof(const AbstractAttribute *AA) {
3625     return (AA->getIdAddr() == &ID);
3626   }
3627 
3628   /// Unique ID (due to the unique address)
3629   LLVM_ABI static const char ID;
3630 };
3631 
3632 /// An abstract interface for all nonnull attributes.
3633 struct AAMustProgress
3634     : public IRAttribute<Attribute::MustProgress,
3635                          StateWrapper<BooleanState, AbstractAttribute>,
3636                          AAMustProgress> {
3637   AAMustProgress(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3638 
3639   static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3640                             Attribute::AttrKind ImpliedAttributeKind,
3641                             bool IgnoreSubsumingPositions = false) {
3642     // Note: This is also run for non-IPO amendable functions.
3643     assert(ImpliedAttributeKind == Attribute::MustProgress);
3644     return A.hasAttr(IRP, {Attribute::MustProgress, Attribute::WillReturn},
3645                      IgnoreSubsumingPositions, Attribute::MustProgress);
3646   }
3647 
3648   /// Return true if we assume that the underlying value is nonnull.
3649   bool isAssumedMustProgress() const { return getAssumed(); }
3650 
3651   /// Return true if we know that underlying value is nonnull.
3652   bool isKnownMustProgress() const { return getKnown(); }
3653 
3654   /// Create an abstract attribute view for the position \p IRP.
3655   LLVM_ABI static AAMustProgress &createForPosition(const IRPosition &IRP,
3656                                                     Attributor &A);
3657 
3658   /// See AbstractAttribute::getName()
3659   StringRef getName() const override { return "AAMustProgress"; }
3660 
3661   /// See AbstractAttribute::getIdAddr()
3662   const char *getIdAddr() const override { return &ID; }
3663 
3664   /// This function should return true if the type of the \p AA is
3665   /// AAMustProgress
3666   static bool classof(const AbstractAttribute *AA) {
3667     return (AA->getIdAddr() == &ID);
3668   }
3669 
3670   /// Unique ID (due to the unique address)
3671   LLVM_ABI static const char ID;
3672 };
3673 
3674 /// An abstract interface for all nonnull attributes.
3675 struct AANonNull
3676     : public IRAttribute<Attribute::NonNull,
3677                          StateWrapper<BooleanState, AbstractAttribute>,
3678                          AANonNull> {
3679   AANonNull(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3680 
3681   /// See AbstractAttribute::hasTrivialInitializer.
3682   static bool hasTrivialInitializer() { return false; }
3683 
3684   /// See IRAttribute::isImpliedByUndef.
3685   /// Undef is not necessarily nonnull as nonnull + noundef would cause poison.
3686   /// Poison implies nonnull though.
3687   static bool isImpliedByUndef() { return false; }
3688 
3689   /// See AbstractAttribute::isValidIRPositionForInit
3690   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
3691     if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy())
3692       return false;
3693     return IRAttribute::isValidIRPositionForInit(A, IRP);
3694   }
3695 
3696   /// See AbstractAttribute::isImpliedByIR(...).
3697   LLVM_ABI static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3698                                      Attribute::AttrKind ImpliedAttributeKind,
3699                                      bool IgnoreSubsumingPositions = false);
3700 
3701   /// Return true if we assume that the underlying value is nonnull.
3702   bool isAssumedNonNull() const { return getAssumed(); }
3703 
3704   /// Return true if we know that underlying value is nonnull.
3705   bool isKnownNonNull() const { return getKnown(); }
3706 
3707   /// Create an abstract attribute view for the position \p IRP.
3708   LLVM_ABI static AANonNull &createForPosition(const IRPosition &IRP,
3709                                                Attributor &A);
3710 
3711   /// See AbstractAttribute::getName()
3712   StringRef getName() const override { return "AANonNull"; }
3713 
3714   /// See AbstractAttribute::getIdAddr()
3715   const char *getIdAddr() const override { return &ID; }
3716 
3717   /// This function should return true if the type of the \p AA is AANonNull
3718   static bool classof(const AbstractAttribute *AA) {
3719     return (AA->getIdAddr() == &ID);
3720   }
3721 
3722   /// Unique ID (due to the unique address)
3723   LLVM_ABI static const char ID;
3724 };
3725 
3726 /// An abstract attribute for norecurse.
3727 struct AANoRecurse
3728     : public IRAttribute<Attribute::NoRecurse,
3729                          StateWrapper<BooleanState, AbstractAttribute>,
3730                          AANoRecurse> {
3731   AANoRecurse(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3732 
3733   /// Return true if "norecurse" is assumed.
3734   bool isAssumedNoRecurse() const { return getAssumed(); }
3735 
3736   /// Return true if "norecurse" is known.
3737   bool isKnownNoRecurse() const { return getKnown(); }
3738 
3739   /// Create an abstract attribute view for the position \p IRP.
3740   LLVM_ABI static AANoRecurse &createForPosition(const IRPosition &IRP,
3741                                                  Attributor &A);
3742 
3743   /// See AbstractAttribute::getName()
3744   StringRef getName() const override { return "AANoRecurse"; }
3745 
3746   /// See AbstractAttribute::getIdAddr()
3747   const char *getIdAddr() const override { return &ID; }
3748 
3749   /// This function should return true if the type of the \p AA is AANoRecurse
3750   static bool classof(const AbstractAttribute *AA) {
3751     return (AA->getIdAddr() == &ID);
3752   }
3753 
3754   /// Unique ID (due to the unique address)
3755   LLVM_ABI static const char ID;
3756 };
3757 
3758 /// An abstract attribute for willreturn.
3759 struct AAWillReturn
3760     : public IRAttribute<Attribute::WillReturn,
3761                          StateWrapper<BooleanState, AbstractAttribute>,
3762                          AAWillReturn> {
3763   AAWillReturn(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3764 
3765   static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3766                             Attribute::AttrKind ImpliedAttributeKind,
3767                             bool IgnoreSubsumingPositions = false) {
3768     // Note: This is also run for non-IPO amendable functions.
3769     assert(ImpliedAttributeKind == Attribute::WillReturn);
3770     if (IRAttribute::isImpliedByIR(A, IRP, ImpliedAttributeKind,
3771                                    IgnoreSubsumingPositions))
3772       return true;
3773     if (!isImpliedByMustprogressAndReadonly(A, IRP))
3774       return false;
3775     A.manifestAttrs(IRP, Attribute::get(IRP.getAnchorValue().getContext(),
3776                                         Attribute::WillReturn));
3777     return true;
3778   }
3779 
3780   /// Check for `mustprogress` and `readonly` as they imply `willreturn`.
3781   static bool isImpliedByMustprogressAndReadonly(Attributor &A,
3782                                                  const IRPosition &IRP) {
3783     // Check for `mustprogress` in the scope and the associated function which
3784     // might be different if this is a call site.
3785     if (!A.hasAttr(IRP, {Attribute::MustProgress}))
3786       return false;
3787 
3788     SmallVector<Attribute, 2> Attrs;
3789     A.getAttrs(IRP, {Attribute::Memory}, Attrs,
3790                /* IgnoreSubsumingPositions */ false);
3791 
3792     MemoryEffects ME = MemoryEffects::unknown();
3793     for (const Attribute &Attr : Attrs)
3794       ME &= Attr.getMemoryEffects();
3795     return ME.onlyReadsMemory();
3796   }
3797 
3798   /// Return true if "willreturn" is assumed.
3799   bool isAssumedWillReturn() const { return getAssumed(); }
3800 
3801   /// Return true if "willreturn" is known.
3802   bool isKnownWillReturn() const { return getKnown(); }
3803 
3804   /// Create an abstract attribute view for the position \p IRP.
3805   LLVM_ABI static AAWillReturn &createForPosition(const IRPosition &IRP,
3806                                                   Attributor &A);
3807 
3808   /// See AbstractAttribute::getName()
3809   StringRef getName() const override { return "AAWillReturn"; }
3810 
3811   /// See AbstractAttribute::getIdAddr()
3812   const char *getIdAddr() const override { return &ID; }
3813 
3814   /// This function should return true if the type of the \p AA is AAWillReturn
3815   static bool classof(const AbstractAttribute *AA) {
3816     return (AA->getIdAddr() == &ID);
3817   }
3818 
3819   /// Unique ID (due to the unique address)
3820   LLVM_ABI static const char ID;
3821 };
3822 
3823 /// An abstract attribute for undefined behavior.
3824 struct AAUndefinedBehavior
3825     : public StateWrapper<BooleanState, AbstractAttribute> {
3826   using Base = StateWrapper<BooleanState, AbstractAttribute>;
3827   AAUndefinedBehavior(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
3828 
3829   /// Return true if "undefined behavior" is assumed.
3830   bool isAssumedToCauseUB() const { return getAssumed(); }
3831 
3832   /// Return true if "undefined behavior" is assumed for a specific instruction.
3833   virtual bool isAssumedToCauseUB(Instruction *I) const = 0;
3834 
3835   /// Return true if "undefined behavior" is known.
3836   bool isKnownToCauseUB() const { return getKnown(); }
3837 
3838   /// Return true if "undefined behavior" is known for a specific instruction.
3839   virtual bool isKnownToCauseUB(Instruction *I) const = 0;
3840 
3841   /// Create an abstract attribute view for the position \p IRP.
3842   LLVM_ABI static AAUndefinedBehavior &createForPosition(const IRPosition &IRP,
3843                                                          Attributor &A);
3844 
3845   /// See AbstractAttribute::getName()
3846   StringRef getName() const override { return "AAUndefinedBehavior"; }
3847 
3848   /// See AbstractAttribute::getIdAddr()
3849   const char *getIdAddr() const override { return &ID; }
3850 
3851   /// This function should return true if the type of the \p AA is
3852   /// AAUndefineBehavior
3853   static bool classof(const AbstractAttribute *AA) {
3854     return (AA->getIdAddr() == &ID);
3855   }
3856 
3857   /// Unique ID (due to the unique address)
3858   LLVM_ABI static const char ID;
3859 };
3860 
3861 /// An abstract interface to determine reachability of point A to B.
3862 struct AAIntraFnReachability
3863     : public StateWrapper<BooleanState, AbstractAttribute> {
3864   using Base = StateWrapper<BooleanState, AbstractAttribute>;
3865   AAIntraFnReachability(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
3866 
3867   /// Returns true if 'From' instruction is assumed to reach, 'To' instruction.
3868   /// Users should provide two positions they are interested in, and the class
3869   /// determines (and caches) reachability.
3870   virtual bool isAssumedReachable(
3871       Attributor &A, const Instruction &From, const Instruction &To,
3872       const AA::InstExclusionSetTy *ExclusionSet = nullptr) const = 0;
3873 
3874   /// Create an abstract attribute view for the position \p IRP.
3875   LLVM_ABI static AAIntraFnReachability &
3876   createForPosition(const IRPosition &IRP, Attributor &A);
3877 
3878   /// See AbstractAttribute::getName()
3879   StringRef getName() const override { return "AAIntraFnReachability"; }
3880 
3881   /// See AbstractAttribute::getIdAddr()
3882   const char *getIdAddr() const override { return &ID; }
3883 
3884   /// This function should return true if the type of the \p AA is
3885   /// AAIntraFnReachability
3886   static bool classof(const AbstractAttribute *AA) {
3887     return (AA->getIdAddr() == &ID);
3888   }
3889 
3890   /// Unique ID (due to the unique address)
3891   LLVM_ABI static const char ID;
3892 };
3893 
3894 /// An abstract interface for all noalias attributes.
3895 struct AANoAlias
3896     : public IRAttribute<Attribute::NoAlias,
3897                          StateWrapper<BooleanState, AbstractAttribute>,
3898                          AANoAlias> {
3899   AANoAlias(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3900 
3901   /// See AbstractAttribute::isValidIRPositionForInit
3902   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
3903     if (!IRP.getAssociatedType()->isPointerTy())
3904       return false;
3905     return IRAttribute::isValidIRPositionForInit(A, IRP);
3906   }
3907 
3908   /// See IRAttribute::isImpliedByIR
3909   LLVM_ABI static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3910                                      Attribute::AttrKind ImpliedAttributeKind,
3911                                      bool IgnoreSubsumingPositions = false);
3912 
3913   /// See AbstractAttribute::requiresCallersForArgOrFunction
3914   static bool requiresCallersForArgOrFunction() { return true; }
3915 
3916   /// Return true if we assume that the underlying value is alias.
3917   bool isAssumedNoAlias() const { return getAssumed(); }
3918 
3919   /// Return true if we know that underlying value is noalias.
3920   bool isKnownNoAlias() const { return getKnown(); }
3921 
3922   /// Create an abstract attribute view for the position \p IRP.
3923   LLVM_ABI static AANoAlias &createForPosition(const IRPosition &IRP,
3924                                                Attributor &A);
3925 
3926   /// See AbstractAttribute::getName()
3927   StringRef getName() const override { return "AANoAlias"; }
3928 
3929   /// See AbstractAttribute::getIdAddr()
3930   const char *getIdAddr() const override { return &ID; }
3931 
3932   /// This function should return true if the type of the \p AA is AANoAlias
3933   static bool classof(const AbstractAttribute *AA) {
3934     return (AA->getIdAddr() == &ID);
3935   }
3936 
3937   /// Unique ID (due to the unique address)
3938   LLVM_ABI static const char ID;
3939 };
3940 
3941 /// An AbstractAttribute for nofree.
3942 struct AANoFree
3943     : public IRAttribute<Attribute::NoFree,
3944                          StateWrapper<BooleanState, AbstractAttribute>,
3945                          AANoFree> {
3946   AANoFree(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3947 
3948   /// See IRAttribute::isImpliedByIR
3949   static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3950                             Attribute::AttrKind ImpliedAttributeKind,
3951                             bool IgnoreSubsumingPositions = false) {
3952     // Note: This is also run for non-IPO amendable functions.
3953     assert(ImpliedAttributeKind == Attribute::NoFree);
3954     return A.hasAttr(
3955         IRP, {Attribute::ReadNone, Attribute::ReadOnly, Attribute::NoFree},
3956         IgnoreSubsumingPositions, Attribute::NoFree);
3957   }
3958 
3959   /// See AbstractAttribute::isValidIRPositionForInit
3960   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
3961     if (!IRP.isFunctionScope() &&
3962         !IRP.getAssociatedType()->isPtrOrPtrVectorTy())
3963       return false;
3964     return IRAttribute::isValidIRPositionForInit(A, IRP);
3965   }
3966 
3967   /// Return true if "nofree" is assumed.
3968   bool isAssumedNoFree() const { return getAssumed(); }
3969 
3970   /// Return true if "nofree" is known.
3971   bool isKnownNoFree() const { return getKnown(); }
3972 
3973   /// Create an abstract attribute view for the position \p IRP.
3974   LLVM_ABI static AANoFree &createForPosition(const IRPosition &IRP,
3975                                               Attributor &A);
3976 
3977   /// See AbstractAttribute::getName()
3978   StringRef getName() const override { return "AANoFree"; }
3979 
3980   /// See AbstractAttribute::getIdAddr()
3981   const char *getIdAddr() const override { return &ID; }
3982 
3983   /// This function should return true if the type of the \p AA is AANoFree
3984   static bool classof(const AbstractAttribute *AA) {
3985     return (AA->getIdAddr() == &ID);
3986   }
3987 
3988   /// Unique ID (due to the unique address)
3989   LLVM_ABI static const char ID;
3990 };
3991 
3992 /// An AbstractAttribute for noreturn.
3993 struct AANoReturn
3994     : public IRAttribute<Attribute::NoReturn,
3995                          StateWrapper<BooleanState, AbstractAttribute>,
3996                          AANoReturn> {
3997   AANoReturn(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3998 
3999   /// Return true if the underlying object is assumed to never return.
4000   bool isAssumedNoReturn() const { return getAssumed(); }
4001 
4002   /// Return true if the underlying object is known to never return.
4003   bool isKnownNoReturn() const { return getKnown(); }
4004 
4005   /// Create an abstract attribute view for the position \p IRP.
4006   LLVM_ABI static AANoReturn &createForPosition(const IRPosition &IRP,
4007                                                 Attributor &A);
4008 
4009   /// See AbstractAttribute::getName()
4010   StringRef getName() const override { return "AANoReturn"; }
4011 
4012   /// See AbstractAttribute::getIdAddr()
4013   const char *getIdAddr() const override { return &ID; }
4014 
4015   /// This function should return true if the type of the \p AA is AANoReturn
4016   static bool classof(const AbstractAttribute *AA) {
4017     return (AA->getIdAddr() == &ID);
4018   }
4019 
4020   /// Unique ID (due to the unique address)
4021   LLVM_ABI static const char ID;
4022 };
4023 
4024 /// An abstract interface for liveness abstract attribute.
4025 struct AAIsDead
4026     : public StateWrapper<BitIntegerState<uint8_t, 3, 0>, AbstractAttribute> {
4027   using Base = StateWrapper<BitIntegerState<uint8_t, 3, 0>, AbstractAttribute>;
4028   AAIsDead(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
4029 
4030   /// See AbstractAttribute::isValidIRPositionForInit
4031   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
4032     if (IRP.getPositionKind() == IRPosition::IRP_FUNCTION)
4033       return isa<Function>(IRP.getAnchorValue()) &&
4034              !cast<Function>(IRP.getAnchorValue()).isDeclaration();
4035     return true;
4036   }
4037 
4038   /// State encoding bits. A set bit in the state means the property holds.
4039   enum {
4040     HAS_NO_EFFECT = 1 << 0,
4041     IS_REMOVABLE = 1 << 1,
4042 
4043     IS_DEAD = HAS_NO_EFFECT | IS_REMOVABLE,
4044   };
4045   static_assert(IS_DEAD == getBestState(), "Unexpected BEST_STATE value");
4046 
4047 protected:
4048   /// The query functions are protected such that other attributes need to go
4049   /// through the Attributor interfaces: `Attributor::isAssumedDead(...)`
4050 
4051   /// Returns true if the underlying value is assumed dead.
4052   virtual bool isAssumedDead() const = 0;
4053 
4054   /// Returns true if the underlying value is known dead.
4055   virtual bool isKnownDead() const = 0;
4056 
4057   /// Returns true if \p BB is known dead.
4058   virtual bool isKnownDead(const BasicBlock *BB) const = 0;
4059 
4060   /// Returns true if \p I is assumed dead.
4061   virtual bool isAssumedDead(const Instruction *I) const = 0;
4062 
4063   /// Returns true if \p I is known dead.
4064   virtual bool isKnownDead(const Instruction *I) const = 0;
4065 
4066   /// Return true if the underlying value is a store that is known to be
4067   /// removable. This is different from dead stores as the removable store
4068   /// can have an effect on live values, especially loads, but that effect
4069   /// is propagated which allows us to remove the store in turn.
4070   virtual bool isRemovableStore() const { return false; }
4071 
4072   /// This method is used to check if at least one instruction in a collection
4073   /// of instructions is live.
4074   template <typename T> bool isLiveInstSet(T begin, T end) const {
4075     for (const auto &I : llvm::make_range(begin, end)) {
4076       assert(I->getFunction() == getIRPosition().getAssociatedFunction() &&
4077              "Instruction must be in the same anchor scope function.");
4078 
4079       if (!isAssumedDead(I))
4080         return true;
4081     }
4082 
4083     return false;
4084   }
4085 
4086 public:
4087   /// Create an abstract attribute view for the position \p IRP.
4088   LLVM_ABI static AAIsDead &createForPosition(const IRPosition &IRP,
4089                                               Attributor &A);
4090 
4091   /// Determine if \p F might catch asynchronous exceptions.
4092   static bool mayCatchAsynchronousExceptions(const Function &F) {
4093     return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
4094   }
4095 
4096   /// Returns true if \p BB is assumed dead.
4097   virtual bool isAssumedDead(const BasicBlock *BB) const = 0;
4098 
4099   /// Return if the edge from \p From BB to \p To BB is assumed dead.
4100   /// This is specifically useful in AAReachability.
4101   virtual bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const {
4102     return false;
4103   }
4104 
4105   /// See AbstractAttribute::getName()
4106   StringRef getName() const override { return "AAIsDead"; }
4107 
4108   /// See AbstractAttribute::getIdAddr()
4109   const char *getIdAddr() const override { return &ID; }
4110 
4111   /// This function should return true if the type of the \p AA is AAIsDead
4112   static bool classof(const AbstractAttribute *AA) {
4113     return (AA->getIdAddr() == &ID);
4114   }
4115 
4116   /// Unique ID (due to the unique address)
4117   LLVM_ABI static const char ID;
4118 
4119   friend struct Attributor;
4120 };
4121 
4122 /// State for dereferenceable attribute
4123 struct DerefState : AbstractState {
4124 
4125   static DerefState getBestState() { return DerefState(); }
4126   static DerefState getBestState(const DerefState &) { return getBestState(); }
4127 
4128   /// Return the worst possible representable state.
4129   static DerefState getWorstState() {
4130     DerefState DS;
4131     DS.indicatePessimisticFixpoint();
4132     return DS;
4133   }
4134   static DerefState getWorstState(const DerefState &) {
4135     return getWorstState();
4136   }
4137 
4138   /// State representing for dereferenceable bytes.
4139   IncIntegerState<> DerefBytesState;
4140 
4141   /// Map representing for accessed memory offsets and sizes.
4142   /// A key is Offset and a value is size.
4143   /// If there is a load/store instruction something like,
4144   ///   p[offset] = v;
4145   /// (offset, sizeof(v)) will be inserted to this map.
4146   /// std::map is used because we want to iterate keys in ascending order.
4147   std::map<int64_t, uint64_t> AccessedBytesMap;
4148 
4149   /// Helper function to calculate dereferenceable bytes from current known
4150   /// bytes and accessed bytes.
4151   ///
4152   /// int f(int *A){
4153   ///    *A = 0;
4154   ///    *(A+2) = 2;
4155   ///    *(A+1) = 1;
4156   ///    *(A+10) = 10;
4157   /// }
4158   /// ```
4159   /// In that case, AccessedBytesMap is `{0:4, 4:4, 8:4, 40:4}`.
4160   /// AccessedBytesMap is std::map so it is iterated in accending order on
4161   /// key(Offset). So KnownBytes will be updated like this:
4162   ///
4163   /// |Access | KnownBytes
4164   /// |(0, 4)| 0 -> 4
4165   /// |(4, 4)| 4 -> 8
4166   /// |(8, 4)| 8 -> 12
4167   /// |(40, 4) | 12 (break)
4168   void computeKnownDerefBytesFromAccessedMap() {
4169     int64_t KnownBytes = DerefBytesState.getKnown();
4170     for (auto &Access : AccessedBytesMap) {
4171       if (KnownBytes < Access.first)
4172         break;
4173       KnownBytes = std::max(KnownBytes, Access.first + (int64_t)Access.second);
4174     }
4175 
4176     DerefBytesState.takeKnownMaximum(KnownBytes);
4177   }
4178 
4179   /// State representing that whether the value is globaly dereferenceable.
4180   BooleanState GlobalState;
4181 
4182   /// See AbstractState::isValidState()
4183   bool isValidState() const override { return DerefBytesState.isValidState(); }
4184 
4185   /// See AbstractState::isAtFixpoint()
4186   bool isAtFixpoint() const override {
4187     return !isValidState() ||
4188            (DerefBytesState.isAtFixpoint() && GlobalState.isAtFixpoint());
4189   }
4190 
4191   /// See AbstractState::indicateOptimisticFixpoint(...)
4192   ChangeStatus indicateOptimisticFixpoint() override {
4193     DerefBytesState.indicateOptimisticFixpoint();
4194     GlobalState.indicateOptimisticFixpoint();
4195     return ChangeStatus::UNCHANGED;
4196   }
4197 
4198   /// See AbstractState::indicatePessimisticFixpoint(...)
4199   ChangeStatus indicatePessimisticFixpoint() override {
4200     DerefBytesState.indicatePessimisticFixpoint();
4201     GlobalState.indicatePessimisticFixpoint();
4202     return ChangeStatus::CHANGED;
4203   }
4204 
4205   /// Update known dereferenceable bytes.
4206   void takeKnownDerefBytesMaximum(uint64_t Bytes) {
4207     DerefBytesState.takeKnownMaximum(Bytes);
4208 
4209     // Known bytes might increase.
4210     computeKnownDerefBytesFromAccessedMap();
4211   }
4212 
4213   /// Update assumed dereferenceable bytes.
4214   void takeAssumedDerefBytesMinimum(uint64_t Bytes) {
4215     DerefBytesState.takeAssumedMinimum(Bytes);
4216   }
4217 
4218   /// Add accessed bytes to the map.
4219   void addAccessedBytes(int64_t Offset, uint64_t Size) {
4220     uint64_t &AccessedBytes = AccessedBytesMap[Offset];
4221     AccessedBytes = std::max(AccessedBytes, Size);
4222 
4223     // Known bytes might increase.
4224     computeKnownDerefBytesFromAccessedMap();
4225   }
4226 
4227   /// Equality for DerefState.
4228   bool operator==(const DerefState &R) const {
4229     return this->DerefBytesState == R.DerefBytesState &&
4230            this->GlobalState == R.GlobalState;
4231   }
4232 
4233   /// Inequality for DerefState.
4234   bool operator!=(const DerefState &R) const { return !(*this == R); }
4235 
4236   /// See IntegerStateBase::operator^=
4237   DerefState operator^=(const DerefState &R) {
4238     DerefBytesState ^= R.DerefBytesState;
4239     GlobalState ^= R.GlobalState;
4240     return *this;
4241   }
4242 
4243   /// See IntegerStateBase::operator+=
4244   DerefState operator+=(const DerefState &R) {
4245     DerefBytesState += R.DerefBytesState;
4246     GlobalState += R.GlobalState;
4247     return *this;
4248   }
4249 
4250   /// See IntegerStateBase::operator&=
4251   DerefState operator&=(const DerefState &R) {
4252     DerefBytesState &= R.DerefBytesState;
4253     GlobalState &= R.GlobalState;
4254     return *this;
4255   }
4256 
4257   /// See IntegerStateBase::operator|=
4258   DerefState operator|=(const DerefState &R) {
4259     DerefBytesState |= R.DerefBytesState;
4260     GlobalState |= R.GlobalState;
4261     return *this;
4262   }
4263 };
4264 
4265 /// An abstract interface for all dereferenceable attribute.
4266 struct AADereferenceable
4267     : public IRAttribute<Attribute::Dereferenceable,
4268                          StateWrapper<DerefState, AbstractAttribute>,
4269                          AADereferenceable> {
4270   AADereferenceable(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
4271 
4272   /// See AbstractAttribute::isValidIRPositionForInit
4273   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
4274     if (!IRP.getAssociatedType()->isPointerTy())
4275       return false;
4276     return IRAttribute::isValidIRPositionForInit(A, IRP);
4277   }
4278 
4279   /// Return true if we assume that underlying value is
4280   /// dereferenceable(_or_null) globally.
4281   bool isAssumedGlobal() const { return GlobalState.getAssumed(); }
4282 
4283   /// Return true if we know that underlying value is
4284   /// dereferenceable(_or_null) globally.
4285   bool isKnownGlobal() const { return GlobalState.getKnown(); }
4286 
4287   /// Return assumed dereferenceable bytes.
4288   uint32_t getAssumedDereferenceableBytes() const {
4289     return DerefBytesState.getAssumed();
4290   }
4291 
4292   /// Return known dereferenceable bytes.
4293   uint32_t getKnownDereferenceableBytes() const {
4294     return DerefBytesState.getKnown();
4295   }
4296 
4297   /// Create an abstract attribute view for the position \p IRP.
4298   LLVM_ABI static AADereferenceable &createForPosition(const IRPosition &IRP,
4299                                                        Attributor &A);
4300 
4301   /// See AbstractAttribute::getName()
4302   StringRef getName() const override { return "AADereferenceable"; }
4303 
4304   /// See AbstractAttribute::getIdAddr()
4305   const char *getIdAddr() const override { return &ID; }
4306 
4307   /// This function should return true if the type of the \p AA is
4308   /// AADereferenceable
4309   static bool classof(const AbstractAttribute *AA) {
4310     return (AA->getIdAddr() == &ID);
4311   }
4312 
4313   /// Unique ID (due to the unique address)
4314   LLVM_ABI static const char ID;
4315 };
4316 
4317 using AAAlignmentStateType =
4318     IncIntegerState<uint64_t, Value::MaximumAlignment, 1>;
4319 /// An abstract interface for all align attributes.
4320 struct AAAlign
4321     : public IRAttribute<Attribute::Alignment,
4322                          StateWrapper<AAAlignmentStateType, AbstractAttribute>,
4323                          AAAlign> {
4324   AAAlign(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
4325 
4326   /// See AbstractAttribute::isValidIRPositionForInit
4327   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
4328     if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy())
4329       return false;
4330     return IRAttribute::isValidIRPositionForInit(A, IRP);
4331   }
4332 
4333   /// Return assumed alignment.
4334   Align getAssumedAlign() const { return Align(getAssumed()); }
4335 
4336   /// Return known alignment.
4337   Align getKnownAlign() const { return Align(getKnown()); }
4338 
4339   /// See AbstractAttribute::getName()
4340   StringRef getName() const override { return "AAAlign"; }
4341 
4342   /// See AbstractAttribute::getIdAddr()
4343   const char *getIdAddr() const override { return &ID; }
4344 
4345   /// This function should return true if the type of the \p AA is AAAlign
4346   static bool classof(const AbstractAttribute *AA) {
4347     return (AA->getIdAddr() == &ID);
4348   }
4349 
4350   /// Create an abstract attribute view for the position \p IRP.
4351   LLVM_ABI static AAAlign &createForPosition(const IRPosition &IRP,
4352                                              Attributor &A);
4353 
4354   /// Unique ID (due to the unique address)
4355   LLVM_ABI static const char ID;
4356 };
4357 
4358 /// An abstract interface to track if a value leaves it's defining function
4359 /// instance.
4360 /// TODO: We should make it a ternary AA tracking uniqueness, and uniqueness
4361 /// wrt. the Attributor analysis separately.
4362 struct AAInstanceInfo : public StateWrapper<BooleanState, AbstractAttribute> {
4363   AAInstanceInfo(const IRPosition &IRP, Attributor &A)
4364       : StateWrapper<BooleanState, AbstractAttribute>(IRP) {}
4365 
4366   /// Return true if we know that the underlying value is unique in its scope
4367   /// wrt. the Attributor analysis. That means it might not be unique but we can
4368   /// still use pointer equality without risking to represent two instances with
4369   /// one `llvm::Value`.
4370   bool isKnownUniqueForAnalysis() const { return isKnown(); }
4371 
4372   /// Return true if we assume that the underlying value is unique in its scope
4373   /// wrt. the Attributor analysis. That means it might not be unique but we can
4374   /// still use pointer equality without risking to represent two instances with
4375   /// one `llvm::Value`.
4376   bool isAssumedUniqueForAnalysis() const { return isAssumed(); }
4377 
4378   /// Create an abstract attribute view for the position \p IRP.
4379   LLVM_ABI static AAInstanceInfo &createForPosition(const IRPosition &IRP,
4380                                                     Attributor &A);
4381 
4382   /// See AbstractAttribute::getName()
4383   StringRef getName() const override { return "AAInstanceInfo"; }
4384 
4385   /// See AbstractAttribute::getIdAddr()
4386   const char *getIdAddr() const override { return &ID; }
4387 
4388   /// This function should return true if the type of the \p AA is
4389   /// AAInstanceInfo
4390   static bool classof(const AbstractAttribute *AA) {
4391     return (AA->getIdAddr() == &ID);
4392   }
4393 
4394   /// Unique ID (due to the unique address)
4395   LLVM_ABI static const char ID;
4396 };
4397 
4398 /// An abstract interface for all nocapture attributes.
4399 struct AANoCapture
4400     : public IRAttribute<
4401           Attribute::Captures,
4402           StateWrapper<BitIntegerState<uint16_t, 7, 0>, AbstractAttribute>,
4403           AANoCapture> {
4404   AANoCapture(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
4405 
4406   /// See IRAttribute::isImpliedByIR
4407   LLVM_ABI static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
4408                                      Attribute::AttrKind ImpliedAttributeKind,
4409                                      bool IgnoreSubsumingPositions = false);
4410 
4411   /// Update \p State according to the capture capabilities of \p F for position
4412   /// \p IRP.
4413   LLVM_ABI static void
4414   determineFunctionCaptureCapabilities(const IRPosition &IRP, const Function &F,
4415                                        BitIntegerState &State);
4416 
4417   /// See AbstractAttribute::isValidIRPositionForInit
4418   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
4419     if (!IRP.getAssociatedType()->isPointerTy())
4420       return false;
4421     return IRAttribute::isValidIRPositionForInit(A, IRP);
4422   }
4423 
4424   /// State encoding bits. A set bit in the state means the property holds.
4425   /// NO_CAPTURE is the best possible state, 0 the worst possible state.
4426   enum {
4427     NOT_CAPTURED_IN_MEM = 1 << 0,
4428     NOT_CAPTURED_IN_INT = 1 << 1,
4429     NOT_CAPTURED_IN_RET = 1 << 2,
4430 
4431     /// If we do not capture the value in memory or through integers we can only
4432     /// communicate it back as a derived pointer.
4433     NO_CAPTURE_MAYBE_RETURNED = NOT_CAPTURED_IN_MEM | NOT_CAPTURED_IN_INT,
4434 
4435     /// If we do not capture the value in memory, through integers, or as a
4436     /// derived pointer we know it is not captured.
4437     NO_CAPTURE =
4438         NOT_CAPTURED_IN_MEM | NOT_CAPTURED_IN_INT | NOT_CAPTURED_IN_RET,
4439   };
4440 
4441   /// Return true if we know that the underlying value is not captured in its
4442   /// respective scope.
4443   bool isKnownNoCapture() const { return isKnown(NO_CAPTURE); }
4444 
4445   /// Return true if we assume that the underlying value is not captured in its
4446   /// respective scope.
4447   bool isAssumedNoCapture() const { return isAssumed(NO_CAPTURE); }
4448 
4449   /// Return true if we know that the underlying value is not captured in its
4450   /// respective scope but we allow it to escape through a "return".
4451   bool isKnownNoCaptureMaybeReturned() const {
4452     return isKnown(NO_CAPTURE_MAYBE_RETURNED);
4453   }
4454 
4455   /// Return true if we assume that the underlying value is not captured in its
4456   /// respective scope but we allow it to escape through a "return".
4457   bool isAssumedNoCaptureMaybeReturned() const {
4458     return isAssumed(NO_CAPTURE_MAYBE_RETURNED);
4459   }
4460 
4461   /// Create an abstract attribute view for the position \p IRP.
4462   LLVM_ABI static AANoCapture &createForPosition(const IRPosition &IRP,
4463                                                  Attributor &A);
4464 
4465   /// See AbstractAttribute::getName()
4466   StringRef getName() const override { return "AANoCapture"; }
4467 
4468   /// See AbstractAttribute::getIdAddr()
4469   const char *getIdAddr() const override { return &ID; }
4470 
4471   /// This function should return true if the type of the \p AA is AANoCapture
4472   static bool classof(const AbstractAttribute *AA) {
4473     return (AA->getIdAddr() == &ID);
4474   }
4475 
4476   /// Unique ID (due to the unique address)
4477   LLVM_ABI static const char ID;
4478 };
4479 
4480 struct ValueSimplifyStateType : public AbstractState {
4481 
4482   ValueSimplifyStateType(Type *Ty) : Ty(Ty) {}
4483 
4484   static ValueSimplifyStateType getBestState(Type *Ty) {
4485     return ValueSimplifyStateType(Ty);
4486   }
4487   static ValueSimplifyStateType getBestState(const ValueSimplifyStateType &VS) {
4488     return getBestState(VS.Ty);
4489   }
4490 
4491   /// Return the worst possible representable state.
4492   static ValueSimplifyStateType getWorstState(Type *Ty) {
4493     ValueSimplifyStateType DS(Ty);
4494     DS.indicatePessimisticFixpoint();
4495     return DS;
4496   }
4497   static ValueSimplifyStateType
4498   getWorstState(const ValueSimplifyStateType &VS) {
4499     return getWorstState(VS.Ty);
4500   }
4501 
4502   /// See AbstractState::isValidState(...)
4503   bool isValidState() const override { return BS.isValidState(); }
4504 
4505   /// See AbstractState::isAtFixpoint(...)
4506   bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
4507 
4508   /// Return the assumed state encoding.
4509   ValueSimplifyStateType getAssumed() { return *this; }
4510   const ValueSimplifyStateType &getAssumed() const { return *this; }
4511 
4512   /// See AbstractState::indicatePessimisticFixpoint(...)
4513   ChangeStatus indicatePessimisticFixpoint() override {
4514     return BS.indicatePessimisticFixpoint();
4515   }
4516 
4517   /// See AbstractState::indicateOptimisticFixpoint(...)
4518   ChangeStatus indicateOptimisticFixpoint() override {
4519     return BS.indicateOptimisticFixpoint();
4520   }
4521 
4522   /// "Clamp" this state with \p PVS.
4523   ValueSimplifyStateType operator^=(const ValueSimplifyStateType &VS) {
4524     BS ^= VS.BS;
4525     unionAssumed(VS.SimplifiedAssociatedValue);
4526     return *this;
4527   }
4528 
4529   bool operator==(const ValueSimplifyStateType &RHS) const {
4530     if (isValidState() != RHS.isValidState())
4531       return false;
4532     if (!isValidState() && !RHS.isValidState())
4533       return true;
4534     return SimplifiedAssociatedValue == RHS.SimplifiedAssociatedValue;
4535   }
4536 
4537 protected:
4538   /// The type of the original value.
4539   Type *Ty;
4540 
4541   /// Merge \p Other into the currently assumed simplified value
4542   LLVM_ABI bool unionAssumed(std::optional<Value *> Other);
4543 
4544   /// Helper to track validity and fixpoint
4545   BooleanState BS;
4546 
4547   /// An assumed simplified value. Initially, it is set to std::nullopt, which
4548   /// means that the value is not clear under current assumption. If in the
4549   /// pessimistic state, getAssumedSimplifiedValue doesn't return this value but
4550   /// returns orignal associated value.
4551   std::optional<Value *> SimplifiedAssociatedValue;
4552 };
4553 
4554 /// An abstract interface for value simplify abstract attribute.
4555 struct AAValueSimplify
4556     : public StateWrapper<ValueSimplifyStateType, AbstractAttribute, Type *> {
4557   using Base = StateWrapper<ValueSimplifyStateType, AbstractAttribute, Type *>;
4558   AAValueSimplify(const IRPosition &IRP, Attributor &A)
4559       : Base(IRP, IRP.getAssociatedType()) {}
4560 
4561   /// Create an abstract attribute view for the position \p IRP.
4562   LLVM_ABI static AAValueSimplify &createForPosition(const IRPosition &IRP,
4563                                                      Attributor &A);
4564 
4565   /// See AbstractAttribute::getName()
4566   StringRef getName() const override { return "AAValueSimplify"; }
4567 
4568   /// See AbstractAttribute::getIdAddr()
4569   const char *getIdAddr() const override { return &ID; }
4570 
4571   /// This function should return true if the type of the \p AA is
4572   /// AAValueSimplify
4573   static bool classof(const AbstractAttribute *AA) {
4574     return (AA->getIdAddr() == &ID);
4575   }
4576 
4577   /// Unique ID (due to the unique address)
4578   LLVM_ABI static const char ID;
4579 
4580 private:
4581   /// Return an assumed simplified value if a single candidate is found. If
4582   /// there cannot be one, return original value. If it is not clear yet, return
4583   /// std::nullopt.
4584   ///
4585   /// Use `Attributor::getAssumedSimplified` for value simplification.
4586   virtual std::optional<Value *>
4587   getAssumedSimplifiedValue(Attributor &A) const = 0;
4588 
4589   friend struct Attributor;
4590 };
4591 
4592 struct AAHeapToStack : public StateWrapper<BooleanState, AbstractAttribute> {
4593   using Base = StateWrapper<BooleanState, AbstractAttribute>;
4594   AAHeapToStack(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
4595 
4596   /// Returns true if HeapToStack conversion is assumed to be possible.
4597   virtual bool isAssumedHeapToStack(const CallBase &CB) const = 0;
4598 
4599   /// Returns true if HeapToStack conversion is assumed and the CB is a
4600   /// callsite to a free operation to be removed.
4601   virtual bool isAssumedHeapToStackRemovedFree(CallBase &CB) const = 0;
4602 
4603   /// Create an abstract attribute view for the position \p IRP.
4604   LLVM_ABI static AAHeapToStack &createForPosition(const IRPosition &IRP,
4605                                                    Attributor &A);
4606 
4607   /// See AbstractAttribute::getName()
4608   StringRef getName() const override { return "AAHeapToStack"; }
4609 
4610   /// See AbstractAttribute::getIdAddr()
4611   const char *getIdAddr() const override { return &ID; }
4612 
4613   /// This function should return true if the type of the \p AA is AAHeapToStack
4614   static bool classof(const AbstractAttribute *AA) {
4615     return (AA->getIdAddr() == &ID);
4616   }
4617 
4618   /// Unique ID (due to the unique address)
4619   LLVM_ABI static const char ID;
4620 };
4621 
4622 /// An abstract interface for privatizability.
4623 ///
4624 /// A pointer is privatizable if it can be replaced by a new, private one.
4625 /// Privatizing pointer reduces the use count, interaction between unrelated
4626 /// code parts.
4627 ///
4628 /// In order for a pointer to be privatizable its value cannot be observed
4629 /// (=nocapture), it is (for now) not written (=readonly & noalias), we know
4630 /// what values are necessary to make the private copy look like the original
4631 /// one, and the values we need can be loaded (=dereferenceable).
4632 struct AAPrivatizablePtr
4633     : public StateWrapper<BooleanState, AbstractAttribute> {
4634   using Base = StateWrapper<BooleanState, AbstractAttribute>;
4635   AAPrivatizablePtr(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
4636 
4637   /// See AbstractAttribute::isValidIRPositionForInit
4638   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
4639     if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy())
4640       return false;
4641     return AbstractAttribute::isValidIRPositionForInit(A, IRP);
4642   }
4643 
4644   /// Returns true if pointer privatization is assumed to be possible.
4645   bool isAssumedPrivatizablePtr() const { return getAssumed(); }
4646 
4647   /// Returns true if pointer privatization is known to be possible.
4648   bool isKnownPrivatizablePtr() const { return getKnown(); }
4649 
4650   /// See AbstractAttribute::requiresCallersForArgOrFunction
4651   static bool requiresCallersForArgOrFunction() { return true; }
4652 
4653   /// Return the type we can choose for a private copy of the underlying
4654   /// value. std::nullopt means it is not clear yet, nullptr means there is
4655   /// none.
4656   virtual std::optional<Type *> getPrivatizableType() const = 0;
4657 
4658   /// Create an abstract attribute view for the position \p IRP.
4659   LLVM_ABI static AAPrivatizablePtr &createForPosition(const IRPosition &IRP,
4660                                                        Attributor &A);
4661 
4662   /// See AbstractAttribute::getName()
4663   StringRef getName() const override { return "AAPrivatizablePtr"; }
4664 
4665   /// See AbstractAttribute::getIdAddr()
4666   const char *getIdAddr() const override { return &ID; }
4667 
4668   /// This function should return true if the type of the \p AA is
4669   /// AAPricatizablePtr
4670   static bool classof(const AbstractAttribute *AA) {
4671     return (AA->getIdAddr() == &ID);
4672   }
4673 
4674   /// Unique ID (due to the unique address)
4675   LLVM_ABI static const char ID;
4676 };
4677 
4678 /// An abstract interface for memory access kind related attributes
4679 /// (readnone/readonly/writeonly).
4680 struct AAMemoryBehavior
4681     : public IRAttribute<
4682           Attribute::None,
4683           StateWrapper<BitIntegerState<uint8_t, 3>, AbstractAttribute>,
4684           AAMemoryBehavior> {
4685   AAMemoryBehavior(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
4686 
4687   /// See AbstractAttribute::hasTrivialInitializer.
4688   static bool hasTrivialInitializer() { return false; }
4689 
4690   /// See AbstractAttribute::isValidIRPositionForInit
4691   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
4692     if (!IRP.isFunctionScope() && !IRP.getAssociatedType()->isPointerTy())
4693       return false;
4694     return IRAttribute::isValidIRPositionForInit(A, IRP);
4695   }
4696 
4697   /// State encoding bits. A set bit in the state means the property holds.
4698   /// BEST_STATE is the best possible state, 0 the worst possible state.
4699   enum {
4700     NO_READS = 1 << 0,
4701     NO_WRITES = 1 << 1,
4702     NO_ACCESSES = NO_READS | NO_WRITES,
4703 
4704     BEST_STATE = NO_ACCESSES,
4705   };
4706   static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
4707 
4708   /// Return true if we know that the underlying value is not read or accessed
4709   /// in its respective scope.
4710   bool isKnownReadNone() const { return isKnown(NO_ACCESSES); }
4711 
4712   /// Return true if we assume that the underlying value is not read or accessed
4713   /// in its respective scope.
4714   bool isAssumedReadNone() const { return isAssumed(NO_ACCESSES); }
4715 
4716   /// Return true if we know that the underlying value is not accessed
4717   /// (=written) in its respective scope.
4718   bool isKnownReadOnly() const { return isKnown(NO_WRITES); }
4719 
4720   /// Return true if we assume that the underlying value is not accessed
4721   /// (=written) in its respective scope.
4722   bool isAssumedReadOnly() const { return isAssumed(NO_WRITES); }
4723 
4724   /// Return true if we know that the underlying value is not read in its
4725   /// respective scope.
4726   bool isKnownWriteOnly() const { return isKnown(NO_READS); }
4727 
4728   /// Return true if we assume that the underlying value is not read in its
4729   /// respective scope.
4730   bool isAssumedWriteOnly() const { return isAssumed(NO_READS); }
4731 
4732   /// Create an abstract attribute view for the position \p IRP.
4733   LLVM_ABI static AAMemoryBehavior &createForPosition(const IRPosition &IRP,
4734                                                       Attributor &A);
4735 
4736   /// See AbstractAttribute::getName()
4737   StringRef getName() const override { return "AAMemoryBehavior"; }
4738 
4739   /// See AbstractAttribute::getIdAddr()
4740   const char *getIdAddr() const override { return &ID; }
4741 
4742   /// This function should return true if the type of the \p AA is
4743   /// AAMemoryBehavior
4744   static bool classof(const AbstractAttribute *AA) {
4745     return (AA->getIdAddr() == &ID);
4746   }
4747 
4748   /// Unique ID (due to the unique address)
4749   LLVM_ABI static const char ID;
4750 };
4751 
4752 /// An abstract interface for all memory location attributes
4753 /// (readnone/argmemonly/inaccessiblememonly/inaccessibleorargmemonly).
4754 struct AAMemoryLocation
4755     : public IRAttribute<
4756           Attribute::None,
4757           StateWrapper<BitIntegerState<uint32_t, 511>, AbstractAttribute>,
4758           AAMemoryLocation> {
4759   using MemoryLocationsKind = StateType::base_t;
4760 
4761   AAMemoryLocation(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
4762 
4763   /// See AbstractAttribute::requiresCalleeForCallBase.
4764   static bool requiresCalleeForCallBase() { return true; }
4765 
4766   /// See AbstractAttribute::hasTrivialInitializer.
4767   static bool hasTrivialInitializer() { return false; }
4768 
4769   /// See AbstractAttribute::isValidIRPositionForInit
4770   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
4771     if (!IRP.isFunctionScope() &&
4772         !IRP.getAssociatedType()->isPtrOrPtrVectorTy())
4773       return false;
4774     return IRAttribute::isValidIRPositionForInit(A, IRP);
4775   }
4776 
4777   /// Encoding of different locations that could be accessed by a memory
4778   /// access.
4779   enum {
4780     ALL_LOCATIONS = 0,
4781     NO_LOCAL_MEM = 1 << 0,
4782     NO_CONST_MEM = 1 << 1,
4783     NO_GLOBAL_INTERNAL_MEM = 1 << 2,
4784     NO_GLOBAL_EXTERNAL_MEM = 1 << 3,
4785     NO_GLOBAL_MEM = NO_GLOBAL_INTERNAL_MEM | NO_GLOBAL_EXTERNAL_MEM,
4786     NO_ARGUMENT_MEM = 1 << 4,
4787     NO_INACCESSIBLE_MEM = 1 << 5,
4788     NO_MALLOCED_MEM = 1 << 6,
4789     NO_UNKOWN_MEM = 1 << 7,
4790     NO_LOCATIONS = NO_LOCAL_MEM | NO_CONST_MEM | NO_GLOBAL_INTERNAL_MEM |
4791                    NO_GLOBAL_EXTERNAL_MEM | NO_ARGUMENT_MEM |
4792                    NO_INACCESSIBLE_MEM | NO_MALLOCED_MEM | NO_UNKOWN_MEM,
4793 
4794     // Helper bit to track if we gave up or not.
4795     VALID_STATE = NO_LOCATIONS + 1,
4796 
4797     BEST_STATE = NO_LOCATIONS | VALID_STATE,
4798   };
4799   static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
4800 
4801   /// Return true if we know that the associated functions has no observable
4802   /// accesses.
4803   bool isKnownReadNone() const { return isKnown(NO_LOCATIONS); }
4804 
4805   /// Return true if we assume that the associated functions has no observable
4806   /// accesses.
4807   bool isAssumedReadNone() const {
4808     return isAssumed(NO_LOCATIONS) || isAssumedStackOnly();
4809   }
4810 
4811   /// Return true if we know that the associated functions has at most
4812   /// local/stack accesses.
4813   bool isKnowStackOnly() const {
4814     return isKnown(inverseLocation(NO_LOCAL_MEM, true, true));
4815   }
4816 
4817   /// Return true if we assume that the associated functions has at most
4818   /// local/stack accesses.
4819   bool isAssumedStackOnly() const {
4820     return isAssumed(inverseLocation(NO_LOCAL_MEM, true, true));
4821   }
4822 
4823   /// Return true if we know that the underlying value will only access
4824   /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
4825   bool isKnownInaccessibleMemOnly() const {
4826     return isKnown(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
4827   }
4828 
4829   /// Return true if we assume that the underlying value will only access
4830   /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
4831   bool isAssumedInaccessibleMemOnly() const {
4832     return isAssumed(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
4833   }
4834 
4835   /// Return true if we know that the underlying value will only access
4836   /// argument pointees (see Attribute::ArgMemOnly).
4837   bool isKnownArgMemOnly() const {
4838     return isKnown(inverseLocation(NO_ARGUMENT_MEM, true, true));
4839   }
4840 
4841   /// Return true if we assume that the underlying value will only access
4842   /// argument pointees (see Attribute::ArgMemOnly).
4843   bool isAssumedArgMemOnly() const {
4844     return isAssumed(inverseLocation(NO_ARGUMENT_MEM, true, true));
4845   }
4846 
4847   /// Return true if we know that the underlying value will only access
4848   /// inaccesible memory or argument pointees (see
4849   /// Attribute::InaccessibleOrArgMemOnly).
4850   bool isKnownInaccessibleOrArgMemOnly() const {
4851     return isKnown(
4852         inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
4853   }
4854 
4855   /// Return true if we assume that the underlying value will only access
4856   /// inaccesible memory or argument pointees (see
4857   /// Attribute::InaccessibleOrArgMemOnly).
4858   bool isAssumedInaccessibleOrArgMemOnly() const {
4859     return isAssumed(
4860         inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
4861   }
4862 
4863   /// Return true if the underlying value may access memory through arguement
4864   /// pointers of the associated function, if any.
4865   bool mayAccessArgMem() const { return !isAssumed(NO_ARGUMENT_MEM); }
4866 
4867   /// Return true if only the memory locations specififed by \p MLK are assumed
4868   /// to be accessed by the associated function.
4869   bool isAssumedSpecifiedMemOnly(MemoryLocationsKind MLK) const {
4870     return isAssumed(MLK);
4871   }
4872 
4873   /// Return the locations that are assumed to be not accessed by the associated
4874   /// function, if any.
4875   MemoryLocationsKind getAssumedNotAccessedLocation() const {
4876     return getAssumed();
4877   }
4878 
4879   /// Return the inverse of location \p Loc, thus for NO_XXX the return
4880   /// describes ONLY_XXX. The flags \p AndLocalMem and \p AndConstMem determine
4881   /// if local (=stack) and constant memory are allowed as well. Most of the
4882   /// time we do want them to be included, e.g., argmemonly allows accesses via
4883   /// argument pointers or local or constant memory accesses.
4884   static MemoryLocationsKind
4885   inverseLocation(MemoryLocationsKind Loc, bool AndLocalMem, bool AndConstMem) {
4886     return NO_LOCATIONS & ~(Loc | (AndLocalMem ? NO_LOCAL_MEM : 0) |
4887                             (AndConstMem ? NO_CONST_MEM : 0));
4888   };
4889 
4890   /// Return the locations encoded by \p MLK as a readable string.
4891   LLVM_ABI static std::string getMemoryLocationsAsStr(MemoryLocationsKind MLK);
4892 
4893   /// Simple enum to distinguish read/write/read-write accesses.
4894   enum AccessKind {
4895     NONE = 0,
4896     READ = 1 << 0,
4897     WRITE = 1 << 1,
4898     READ_WRITE = READ | WRITE,
4899   };
4900 
4901   /// Check \p Pred on all accesses to the memory kinds specified by \p MLK.
4902   ///
4903   /// This method will evaluate \p Pred on all accesses (access instruction +
4904   /// underlying accessed memory pointer) and it will return true if \p Pred
4905   /// holds every time.
4906   virtual bool checkForAllAccessesToMemoryKind(
4907       function_ref<bool(const Instruction *, const Value *, AccessKind,
4908                         MemoryLocationsKind)>
4909           Pred,
4910       MemoryLocationsKind MLK) const = 0;
4911 
4912   /// Create an abstract attribute view for the position \p IRP.
4913   LLVM_ABI static AAMemoryLocation &createForPosition(const IRPosition &IRP,
4914                                                       Attributor &A);
4915 
4916   /// See AbstractState::getAsStr(Attributor).
4917   const std::string getAsStr(Attributor *A) const override {
4918     return getMemoryLocationsAsStr(getAssumedNotAccessedLocation());
4919   }
4920 
4921   /// See AbstractAttribute::getName()
4922   StringRef getName() const override { return "AAMemoryLocation"; }
4923 
4924   /// See AbstractAttribute::getIdAddr()
4925   const char *getIdAddr() const override { return &ID; }
4926 
4927   /// This function should return true if the type of the \p AA is
4928   /// AAMemoryLocation
4929   static bool classof(const AbstractAttribute *AA) {
4930     return (AA->getIdAddr() == &ID);
4931   }
4932 
4933   /// Unique ID (due to the unique address)
4934   LLVM_ABI static const char ID;
4935 };
4936 
4937 /// An abstract interface for range value analysis.
4938 struct AAValueConstantRange
4939     : public StateWrapper<IntegerRangeState, AbstractAttribute, uint32_t> {
4940   using Base = StateWrapper<IntegerRangeState, AbstractAttribute, uint32_t>;
4941   AAValueConstantRange(const IRPosition &IRP, Attributor &A)
4942       : Base(IRP, IRP.getAssociatedType()->getIntegerBitWidth()) {}
4943 
4944   /// See AbstractAttribute::isValidIRPositionForInit
4945   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
4946     if (!IRP.getAssociatedType()->isIntegerTy())
4947       return false;
4948     return AbstractAttribute::isValidIRPositionForInit(A, IRP);
4949   }
4950 
4951   /// See AbstractAttribute::requiresCallersForArgOrFunction
4952   static bool requiresCallersForArgOrFunction() { return true; }
4953 
4954   /// See AbstractAttribute::getState(...).
4955   IntegerRangeState &getState() override { return *this; }
4956   const IntegerRangeState &getState() const override { return *this; }
4957 
4958   /// Create an abstract attribute view for the position \p IRP.
4959   LLVM_ABI static AAValueConstantRange &createForPosition(const IRPosition &IRP,
4960                                                           Attributor &A);
4961 
4962   /// Return an assumed range for the associated value a program point \p CtxI.
4963   /// If \p I is nullptr, simply return an assumed range.
4964   virtual ConstantRange
4965   getAssumedConstantRange(Attributor &A,
4966                           const Instruction *CtxI = nullptr) const = 0;
4967 
4968   /// Return a known range for the associated value at a program point \p CtxI.
4969   /// If \p I is nullptr, simply return a known range.
4970   virtual ConstantRange
4971   getKnownConstantRange(Attributor &A,
4972                         const Instruction *CtxI = nullptr) const = 0;
4973 
4974   /// Return an assumed constant for the associated value a program point \p
4975   /// CtxI.
4976   std::optional<Constant *>
4977   getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const {
4978     ConstantRange RangeV = getAssumedConstantRange(A, CtxI);
4979     if (auto *C = RangeV.getSingleElement()) {
4980       Type *Ty = getAssociatedValue().getType();
4981       return cast_or_null<Constant>(
4982           AA::getWithType(*ConstantInt::get(Ty->getContext(), *C), *Ty));
4983     }
4984     if (RangeV.isEmptySet())
4985       return std::nullopt;
4986     return nullptr;
4987   }
4988 
4989   /// See AbstractAttribute::getName()
4990   StringRef getName() const override { return "AAValueConstantRange"; }
4991 
4992   /// See AbstractAttribute::getIdAddr()
4993   const char *getIdAddr() const override { return &ID; }
4994 
4995   /// This function should return true if the type of the \p AA is
4996   /// AAValueConstantRange
4997   static bool classof(const AbstractAttribute *AA) {
4998     return (AA->getIdAddr() == &ID);
4999   }
5000 
5001   /// Unique ID (due to the unique address)
5002   LLVM_ABI static const char ID;
5003 };
5004 
5005 /// A class for a set state.
5006 /// The assumed boolean state indicates whether the corresponding set is full
5007 /// set or not. If the assumed state is false, this is the worst state. The
5008 /// worst state (invalid state) of set of potential values is when the set
5009 /// contains every possible value (i.e. we cannot in any way limit the value
5010 /// that the target position can take). That never happens naturally, we only
5011 /// force it. As for the conditions under which we force it, see
5012 /// AAPotentialConstantValues.
5013 template <typename MemberTy> struct PotentialValuesState : AbstractState {
5014   using SetTy = SmallSetVector<MemberTy, 8>;
5015 
5016   PotentialValuesState() : IsValidState(true), UndefIsContained(false) {}
5017 
5018   PotentialValuesState(bool IsValid)
5019       : IsValidState(IsValid), UndefIsContained(false) {}
5020 
5021   /// See AbstractState::isValidState(...)
5022   bool isValidState() const override { return IsValidState.isValidState(); }
5023 
5024   /// See AbstractState::isAtFixpoint(...)
5025   bool isAtFixpoint() const override { return IsValidState.isAtFixpoint(); }
5026 
5027   /// See AbstractState::indicatePessimisticFixpoint(...)
5028   ChangeStatus indicatePessimisticFixpoint() override {
5029     return IsValidState.indicatePessimisticFixpoint();
5030   }
5031 
5032   /// See AbstractState::indicateOptimisticFixpoint(...)
5033   ChangeStatus indicateOptimisticFixpoint() override {
5034     return IsValidState.indicateOptimisticFixpoint();
5035   }
5036 
5037   /// Return the assumed state
5038   PotentialValuesState &getAssumed() { return *this; }
5039   const PotentialValuesState &getAssumed() const { return *this; }
5040 
5041   /// Return this set. We should check whether this set is valid or not by
5042   /// isValidState() before calling this function.
5043   const SetTy &getAssumedSet() const {
5044     assert(isValidState() && "This set shoud not be used when it is invalid!");
5045     return Set;
5046   }
5047 
5048   /// Returns whether this state contains an undef value or not.
5049   bool undefIsContained() const {
5050     assert(isValidState() && "This flag shoud not be used when it is invalid!");
5051     return UndefIsContained;
5052   }
5053 
5054   bool operator==(const PotentialValuesState &RHS) const {
5055     if (isValidState() != RHS.isValidState())
5056       return false;
5057     if (!isValidState() && !RHS.isValidState())
5058       return true;
5059     if (undefIsContained() != RHS.undefIsContained())
5060       return false;
5061     return Set == RHS.getAssumedSet();
5062   }
5063 
5064   /// Maximum number of potential values to be tracked.
5065   /// This is set by -attributor-max-potential-values command line option
5066   static unsigned MaxPotentialValues;
5067 
5068   /// Return empty set as the best state of potential values.
5069   static PotentialValuesState getBestState() {
5070     return PotentialValuesState(true);
5071   }
5072 
5073   static PotentialValuesState getBestState(const PotentialValuesState &PVS) {
5074     return getBestState();
5075   }
5076 
5077   /// Return full set as the worst state of potential values.
5078   static PotentialValuesState getWorstState() {
5079     return PotentialValuesState(false);
5080   }
5081 
5082   /// Union assumed set with the passed value.
5083   void unionAssumed(const MemberTy &C) { insert(C); }
5084 
5085   /// Union assumed set with assumed set of the passed state \p PVS.
5086   void unionAssumed(const PotentialValuesState &PVS) { unionWith(PVS); }
5087 
5088   /// Union assumed set with an undef value.
5089   void unionAssumedWithUndef() { unionWithUndef(); }
5090 
5091   /// "Clamp" this state with \p PVS.
5092   PotentialValuesState operator^=(const PotentialValuesState &PVS) {
5093     IsValidState ^= PVS.IsValidState;
5094     unionAssumed(PVS);
5095     return *this;
5096   }
5097 
5098   PotentialValuesState operator&=(const PotentialValuesState &PVS) {
5099     IsValidState &= PVS.IsValidState;
5100     unionAssumed(PVS);
5101     return *this;
5102   }
5103 
5104   bool contains(const MemberTy &V) const {
5105     return !isValidState() ? true : Set.contains(V);
5106   }
5107 
5108 protected:
5109   SetTy &getAssumedSet() {
5110     assert(isValidState() && "This set shoud not be used when it is invalid!");
5111     return Set;
5112   }
5113 
5114 private:
5115   /// Check the size of this set, and invalidate when the size is no
5116   /// less than \p MaxPotentialValues threshold.
5117   void checkAndInvalidate() {
5118     if (Set.size() >= MaxPotentialValues)
5119       indicatePessimisticFixpoint();
5120     else
5121       reduceUndefValue();
5122   }
5123 
5124   /// If this state contains both undef and not undef, we can reduce
5125   /// undef to the not undef value.
5126   void reduceUndefValue() { UndefIsContained = UndefIsContained & Set.empty(); }
5127 
5128   /// Insert an element into this set.
5129   void insert(const MemberTy &C) {
5130     if (!isValidState())
5131       return;
5132     Set.insert(C);
5133     checkAndInvalidate();
5134   }
5135 
5136   /// Take union with R.
5137   void unionWith(const PotentialValuesState &R) {
5138     /// If this is a full set, do nothing.
5139     if (!isValidState())
5140       return;
5141     /// If R is full set, change L to a full set.
5142     if (!R.isValidState()) {
5143       indicatePessimisticFixpoint();
5144       return;
5145     }
5146     Set.insert_range(R.Set);
5147     UndefIsContained |= R.undefIsContained();
5148     checkAndInvalidate();
5149   }
5150 
5151   /// Take union with an undef value.
5152   void unionWithUndef() {
5153     UndefIsContained = true;
5154     reduceUndefValue();
5155   }
5156 
5157   /// Take intersection with R.
5158   void intersectWith(const PotentialValuesState &R) {
5159     /// If R is a full set, do nothing.
5160     if (!R.isValidState())
5161       return;
5162     /// If this is a full set, change this to R.
5163     if (!isValidState()) {
5164       *this = R;
5165       return;
5166     }
5167     SetTy IntersectSet;
5168     for (const MemberTy &C : Set) {
5169       if (R.Set.count(C))
5170         IntersectSet.insert(C);
5171     }
5172     Set = IntersectSet;
5173     UndefIsContained &= R.undefIsContained();
5174     reduceUndefValue();
5175   }
5176 
5177   /// A helper state which indicate whether this state is valid or not.
5178   BooleanState IsValidState;
5179 
5180   /// Container for potential values
5181   SetTy Set;
5182 
5183   /// Flag for undef value
5184   bool UndefIsContained;
5185 };
5186 
5187 struct DenormalFPMathState : public AbstractState {
5188   struct DenormalState {
5189     DenormalMode Mode = DenormalMode::getInvalid();
5190     DenormalMode ModeF32 = DenormalMode::getInvalid();
5191 
5192     bool operator==(const DenormalState Other) const {
5193       return Mode == Other.Mode && ModeF32 == Other.ModeF32;
5194     }
5195 
5196     bool operator!=(const DenormalState Other) const {
5197       return Mode != Other.Mode || ModeF32 != Other.ModeF32;
5198     }
5199 
5200     bool isValid() const { return Mode.isValid() && ModeF32.isValid(); }
5201 
5202     static DenormalMode::DenormalModeKind
5203     unionDenormalKind(DenormalMode::DenormalModeKind Callee,
5204                       DenormalMode::DenormalModeKind Caller) {
5205       if (Caller == Callee)
5206         return Caller;
5207       if (Callee == DenormalMode::Dynamic)
5208         return Caller;
5209       if (Caller == DenormalMode::Dynamic)
5210         return Callee;
5211       return DenormalMode::Invalid;
5212     }
5213 
5214     static DenormalMode unionAssumed(DenormalMode Callee, DenormalMode Caller) {
5215       return DenormalMode{unionDenormalKind(Callee.Output, Caller.Output),
5216                           unionDenormalKind(Callee.Input, Caller.Input)};
5217     }
5218 
5219     DenormalState unionWith(DenormalState Caller) const {
5220       DenormalState Callee(*this);
5221       Callee.Mode = unionAssumed(Callee.Mode, Caller.Mode);
5222       Callee.ModeF32 = unionAssumed(Callee.ModeF32, Caller.ModeF32);
5223       return Callee;
5224     }
5225   };
5226 
5227   DenormalState Known;
5228 
5229   /// Explicitly track whether we've hit a fixed point.
5230   bool IsAtFixedpoint = false;
5231 
5232   DenormalFPMathState() = default;
5233 
5234   DenormalState getKnown() const { return Known; }
5235 
5236   // There's only really known or unknown, there's no speculatively assumable
5237   // state.
5238   DenormalState getAssumed() const { return Known; }
5239 
5240   bool isValidState() const override { return Known.isValid(); }
5241 
5242   /// Return true if there are no dynamic components to the denormal mode worth
5243   /// specializing.
5244   bool isModeFixed() const {
5245     return Known.Mode.Input != DenormalMode::Dynamic &&
5246            Known.Mode.Output != DenormalMode::Dynamic &&
5247            Known.ModeF32.Input != DenormalMode::Dynamic &&
5248            Known.ModeF32.Output != DenormalMode::Dynamic;
5249   }
5250 
5251   bool isAtFixpoint() const override { return IsAtFixedpoint; }
5252 
5253   ChangeStatus indicateFixpoint() {
5254     bool Changed = !IsAtFixedpoint;
5255     IsAtFixedpoint = true;
5256     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
5257   }
5258 
5259   ChangeStatus indicateOptimisticFixpoint() override {
5260     return indicateFixpoint();
5261   }
5262 
5263   ChangeStatus indicatePessimisticFixpoint() override {
5264     return indicateFixpoint();
5265   }
5266 
5267   DenormalFPMathState operator^=(const DenormalFPMathState &Caller) {
5268     Known = Known.unionWith(Caller.getKnown());
5269     return *this;
5270   }
5271 };
5272 
5273 using PotentialConstantIntValuesState = PotentialValuesState<APInt>;
5274 using PotentialLLVMValuesState =
5275     PotentialValuesState<std::pair<AA::ValueAndContext, AA::ValueScope>>;
5276 
5277 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS,
5278                                  const PotentialConstantIntValuesState &R);
5279 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS,
5280                                  const PotentialLLVMValuesState &R);
5281 
5282 /// An abstract interface for potential values analysis.
5283 ///
5284 /// This AA collects potential values for each IR position.
5285 /// An assumed set of potential values is initialized with the empty set (the
5286 /// best state) and it will grow monotonically as we find more potential values
5287 /// for this position.
5288 /// The set might be forced to the worst state, that is, to contain every
5289 /// possible value for this position in 2 cases.
5290 ///   1. We surpassed the \p MaxPotentialValues threshold. This includes the
5291 ///      case that this position is affected (e.g. because of an operation) by a
5292 ///      Value that is in the worst state.
5293 ///   2. We tried to initialize on a Value that we cannot handle (e.g. an
5294 ///      operator we do not currently handle).
5295 ///
5296 /// For non constant integers see AAPotentialValues.
5297 struct AAPotentialConstantValues
5298     : public StateWrapper<PotentialConstantIntValuesState, AbstractAttribute> {
5299   using Base = StateWrapper<PotentialConstantIntValuesState, AbstractAttribute>;
5300   AAPotentialConstantValues(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
5301 
5302   /// See AbstractAttribute::isValidIRPositionForInit
5303   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
5304     if (!IRP.getAssociatedType()->isIntegerTy())
5305       return false;
5306     return AbstractAttribute::isValidIRPositionForInit(A, IRP);
5307   }
5308 
5309   /// See AbstractAttribute::requiresCallersForArgOrFunction
5310   static bool requiresCallersForArgOrFunction() { return true; }
5311 
5312   /// See AbstractAttribute::getState(...).
5313   PotentialConstantIntValuesState &getState() override { return *this; }
5314   const PotentialConstantIntValuesState &getState() const override {
5315     return *this;
5316   }
5317 
5318   /// Create an abstract attribute view for the position \p IRP.
5319   LLVM_ABI static AAPotentialConstantValues &
5320   createForPosition(const IRPosition &IRP, Attributor &A);
5321 
5322   /// Return assumed constant for the associated value
5323   std::optional<Constant *>
5324   getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const {
5325     if (!isValidState())
5326       return nullptr;
5327     if (getAssumedSet().size() == 1) {
5328       Type *Ty = getAssociatedValue().getType();
5329       return cast_or_null<Constant>(AA::getWithType(
5330           *ConstantInt::get(Ty->getContext(), *(getAssumedSet().begin())),
5331           *Ty));
5332     }
5333     if (getAssumedSet().size() == 0) {
5334       if (undefIsContained())
5335         return UndefValue::get(getAssociatedValue().getType());
5336       return std::nullopt;
5337     }
5338 
5339     return nullptr;
5340   }
5341 
5342   /// See AbstractAttribute::getName()
5343   StringRef getName() const override { return "AAPotentialConstantValues"; }
5344 
5345   /// See AbstractAttribute::getIdAddr()
5346   const char *getIdAddr() const override { return &ID; }
5347 
5348   /// This function should return true if the type of the \p AA is
5349   /// AAPotentialConstantValues
5350   static bool classof(const AbstractAttribute *AA) {
5351     return (AA->getIdAddr() == &ID);
5352   }
5353 
5354   /// Unique ID (due to the unique address)
5355   LLVM_ABI static const char ID;
5356 };
5357 
5358 struct AAPotentialValues
5359     : public StateWrapper<PotentialLLVMValuesState, AbstractAttribute> {
5360   using Base = StateWrapper<PotentialLLVMValuesState, AbstractAttribute>;
5361   AAPotentialValues(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
5362 
5363   /// See AbstractAttribute::requiresCallersForArgOrFunction
5364   static bool requiresCallersForArgOrFunction() { return true; }
5365 
5366   /// See AbstractAttribute::getState(...).
5367   PotentialLLVMValuesState &getState() override { return *this; }
5368   const PotentialLLVMValuesState &getState() const override { return *this; }
5369 
5370   /// Create an abstract attribute view for the position \p IRP.
5371   LLVM_ABI static AAPotentialValues &createForPosition(const IRPosition &IRP,
5372                                                        Attributor &A);
5373 
5374   /// Extract the single value in \p Values if any.
5375   LLVM_ABI static Value *
5376   getSingleValue(Attributor &A, const AbstractAttribute &AA,
5377                  const IRPosition &IRP,
5378                  SmallVectorImpl<AA::ValueAndContext> &Values);
5379 
5380   /// See AbstractAttribute::getName()
5381   StringRef getName() const override { return "AAPotentialValues"; }
5382 
5383   /// See AbstractAttribute::getIdAddr()
5384   const char *getIdAddr() const override { return &ID; }
5385 
5386   /// This function should return true if the type of the \p AA is
5387   /// AAPotentialValues
5388   static bool classof(const AbstractAttribute *AA) {
5389     return (AA->getIdAddr() == &ID);
5390   }
5391 
5392   /// Unique ID (due to the unique address)
5393   LLVM_ABI static const char ID;
5394 
5395 private:
5396   virtual bool getAssumedSimplifiedValues(
5397       Attributor &A, SmallVectorImpl<AA::ValueAndContext> &Values,
5398       AA::ValueScope, bool RecurseForSelectAndPHI = false) const = 0;
5399 
5400   friend struct Attributor;
5401 };
5402 
5403 /// An abstract interface for all noundef attributes.
5404 struct AANoUndef
5405     : public IRAttribute<Attribute::NoUndef,
5406                          StateWrapper<BooleanState, AbstractAttribute>,
5407                          AANoUndef> {
5408   AANoUndef(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
5409 
5410   /// See IRAttribute::isImpliedByUndef
5411   static bool isImpliedByUndef() { return false; }
5412 
5413   /// See IRAttribute::isImpliedByPoison
5414   static bool isImpliedByPoison() { return false; }
5415 
5416   /// See IRAttribute::isImpliedByIR
5417   LLVM_ABI static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
5418                                      Attribute::AttrKind ImpliedAttributeKind,
5419                                      bool IgnoreSubsumingPositions = false);
5420 
5421   /// Return true if we assume that the underlying value is noundef.
5422   bool isAssumedNoUndef() const { return getAssumed(); }
5423 
5424   /// Return true if we know that underlying value is noundef.
5425   bool isKnownNoUndef() const { return getKnown(); }
5426 
5427   /// Create an abstract attribute view for the position \p IRP.
5428   LLVM_ABI static AANoUndef &createForPosition(const IRPosition &IRP,
5429                                                Attributor &A);
5430 
5431   /// See AbstractAttribute::getName()
5432   StringRef getName() const override { return "AANoUndef"; }
5433 
5434   /// See AbstractAttribute::getIdAddr()
5435   const char *getIdAddr() const override { return &ID; }
5436 
5437   /// This function should return true if the type of the \p AA is AANoUndef
5438   static bool classof(const AbstractAttribute *AA) {
5439     return (AA->getIdAddr() == &ID);
5440   }
5441 
5442   /// Unique ID (due to the unique address)
5443   LLVM_ABI static const char ID;
5444 };
5445 
5446 struct AANoFPClass
5447     : public IRAttribute<
5448           Attribute::NoFPClass,
5449           StateWrapper<BitIntegerState<uint32_t, fcAllFlags, fcNone>,
5450                        AbstractAttribute>,
5451           AANoFPClass> {
5452   using Base = StateWrapper<BitIntegerState<uint32_t, fcAllFlags, fcNone>,
5453                             AbstractAttribute>;
5454 
5455   AANoFPClass(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
5456 
5457   /// See AbstractAttribute::isValidIRPositionForInit
5458   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
5459     Type *Ty = IRP.getAssociatedType();
5460     do {
5461       if (Ty->isFPOrFPVectorTy())
5462         return IRAttribute::isValidIRPositionForInit(A, IRP);
5463       if (!Ty->isArrayTy())
5464         break;
5465       Ty = Ty->getArrayElementType();
5466     } while (true);
5467     return false;
5468   }
5469 
5470   /// Return the underlying assumed nofpclass.
5471   FPClassTest getAssumedNoFPClass() const {
5472     return static_cast<FPClassTest>(getAssumed());
5473   }
5474   /// Return the underlying known nofpclass.
5475   FPClassTest getKnownNoFPClass() const {
5476     return static_cast<FPClassTest>(getKnown());
5477   }
5478 
5479   /// Create an abstract attribute view for the position \p IRP.
5480   LLVM_ABI static AANoFPClass &createForPosition(const IRPosition &IRP,
5481                                                  Attributor &A);
5482 
5483   /// See AbstractAttribute::getName()
5484   StringRef getName() const override { return "AANoFPClass"; }
5485 
5486   /// See AbstractAttribute::getIdAddr()
5487   const char *getIdAddr() const override { return &ID; }
5488 
5489   /// This function should return true if the type of the \p AA is AANoFPClass
5490   static bool classof(const AbstractAttribute *AA) {
5491     return (AA->getIdAddr() == &ID);
5492   }
5493 
5494   /// Unique ID (due to the unique address)
5495   LLVM_ABI static const char ID;
5496 };
5497 
5498 struct AACallGraphNode;
5499 struct AACallEdges;
5500 
5501 /// An Iterator for call edges, creates AACallEdges attributes in a lazy way.
5502 /// This iterator becomes invalid if the underlying edge list changes.
5503 /// So This shouldn't outlive a iteration of Attributor.
5504 class AACallEdgeIterator
5505     : public iterator_adaptor_base<AACallEdgeIterator,
5506                                    SetVector<Function *>::iterator> {
5507   AACallEdgeIterator(Attributor &A, SetVector<Function *>::iterator Begin)
5508       : iterator_adaptor_base(Begin), A(A) {}
5509 
5510 public:
5511   LLVM_ABI AACallGraphNode *operator*() const;
5512 
5513 private:
5514   Attributor &A;
5515   friend AACallEdges;
5516   friend AttributorCallGraph;
5517 };
5518 
5519 struct AACallGraphNode {
5520   AACallGraphNode(Attributor &A) : A(A) {}
5521   virtual ~AACallGraphNode() = default;
5522 
5523   virtual AACallEdgeIterator optimisticEdgesBegin() const = 0;
5524   virtual AACallEdgeIterator optimisticEdgesEnd() const = 0;
5525 
5526   /// Iterator range for exploring the call graph.
5527   iterator_range<AACallEdgeIterator> optimisticEdgesRange() const {
5528     return iterator_range<AACallEdgeIterator>(optimisticEdgesBegin(),
5529                                               optimisticEdgesEnd());
5530   }
5531 
5532 protected:
5533   /// Reference to Attributor needed for GraphTraits implementation.
5534   Attributor &A;
5535 };
5536 
5537 /// An abstract state for querying live call edges.
5538 /// This interface uses the Attributor's optimistic liveness
5539 /// information to compute the edges that are alive.
5540 struct AACallEdges : public StateWrapper<BooleanState, AbstractAttribute>,
5541                      AACallGraphNode {
5542   using Base = StateWrapper<BooleanState, AbstractAttribute>;
5543 
5544   AACallEdges(const IRPosition &IRP, Attributor &A)
5545       : Base(IRP), AACallGraphNode(A) {}
5546 
5547   /// See AbstractAttribute::requiresNonAsmForCallBase.
5548   static bool requiresNonAsmForCallBase() { return false; }
5549 
5550   /// Get the optimistic edges.
5551   virtual const SetVector<Function *> &getOptimisticEdges() const = 0;
5552 
5553   /// Is there any call with a unknown callee.
5554   virtual bool hasUnknownCallee() const = 0;
5555 
5556   /// Is there any call with a unknown callee, excluding any inline asm.
5557   virtual bool hasNonAsmUnknownCallee() const = 0;
5558 
5559   /// Iterator for exploring the call graph.
5560   AACallEdgeIterator optimisticEdgesBegin() const override {
5561     return AACallEdgeIterator(A, getOptimisticEdges().begin());
5562   }
5563 
5564   /// Iterator for exploring the call graph.
5565   AACallEdgeIterator optimisticEdgesEnd() const override {
5566     return AACallEdgeIterator(A, getOptimisticEdges().end());
5567   }
5568 
5569   /// Create an abstract attribute view for the position \p IRP.
5570   LLVM_ABI static AACallEdges &createForPosition(const IRPosition &IRP,
5571                                                  Attributor &A);
5572 
5573   /// See AbstractAttribute::getName()
5574   StringRef getName() const override { return "AACallEdges"; }
5575 
5576   /// See AbstractAttribute::getIdAddr()
5577   const char *getIdAddr() const override { return &ID; }
5578 
5579   /// This function should return true if the type of the \p AA is AACallEdges.
5580   static bool classof(const AbstractAttribute *AA) {
5581     return (AA->getIdAddr() == &ID);
5582   }
5583 
5584   /// Unique ID (due to the unique address)
5585   LLVM_ABI static const char ID;
5586 };
5587 
5588 // Synthetic root node for the Attributor's internal call graph.
5589 struct AttributorCallGraph : public AACallGraphNode {
5590   AttributorCallGraph(Attributor &A) : AACallGraphNode(A) {}
5591   virtual ~AttributorCallGraph() = default;
5592 
5593   AACallEdgeIterator optimisticEdgesBegin() const override {
5594     return AACallEdgeIterator(A, A.Functions.begin());
5595   }
5596 
5597   AACallEdgeIterator optimisticEdgesEnd() const override {
5598     return AACallEdgeIterator(A, A.Functions.end());
5599   }
5600 
5601   /// Force populate the entire call graph.
5602   void populateAll() const {
5603     for (const AACallGraphNode *AA : optimisticEdgesRange()) {
5604       // Nothing else to do here.
5605       (void)AA;
5606     }
5607   }
5608 
5609   LLVM_ABI void print();
5610 };
5611 
5612 template <> struct GraphTraits<AACallGraphNode *> {
5613   using NodeRef = AACallGraphNode *;
5614   using ChildIteratorType = AACallEdgeIterator;
5615 
5616   static AACallEdgeIterator child_begin(AACallGraphNode *Node) {
5617     return Node->optimisticEdgesBegin();
5618   }
5619 
5620   static AACallEdgeIterator child_end(AACallGraphNode *Node) {
5621     return Node->optimisticEdgesEnd();
5622   }
5623 };
5624 
5625 template <>
5626 struct GraphTraits<AttributorCallGraph *>
5627     : public GraphTraits<AACallGraphNode *> {
5628   using nodes_iterator = AACallEdgeIterator;
5629 
5630   static AACallGraphNode *getEntryNode(AttributorCallGraph *G) {
5631     return static_cast<AACallGraphNode *>(G);
5632   }
5633 
5634   static AACallEdgeIterator nodes_begin(const AttributorCallGraph *G) {
5635     return G->optimisticEdgesBegin();
5636   }
5637 
5638   static AACallEdgeIterator nodes_end(const AttributorCallGraph *G) {
5639     return G->optimisticEdgesEnd();
5640   }
5641 };
5642 
5643 template <>
5644 struct DOTGraphTraits<AttributorCallGraph *> : public DefaultDOTGraphTraits {
5645   DOTGraphTraits(bool Simple = false) : DefaultDOTGraphTraits(Simple) {}
5646 
5647   std::string getNodeLabel(const AACallGraphNode *Node,
5648                            const AttributorCallGraph *Graph) {
5649     const AACallEdges *AACE = static_cast<const AACallEdges *>(Node);
5650     return AACE->getAssociatedFunction()->getName().str();
5651   }
5652 
5653   static bool isNodeHidden(const AACallGraphNode *Node,
5654                            const AttributorCallGraph *Graph) {
5655     // Hide the synth root.
5656     return static_cast<const AACallGraphNode *>(Graph) == Node;
5657   }
5658 };
5659 
5660 struct AAExecutionDomain
5661     : public StateWrapper<BooleanState, AbstractAttribute> {
5662   using Base = StateWrapper<BooleanState, AbstractAttribute>;
5663   AAExecutionDomain(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
5664 
5665   /// Summary about the execution domain of a block or instruction.
5666   struct ExecutionDomainTy {
5667     using BarriersSetTy = SmallPtrSet<CallBase *, 2>;
5668     using AssumesSetTy = SmallPtrSet<AssumeInst *, 4>;
5669 
5670     void addAssumeInst(Attributor &A, AssumeInst &AI) {
5671       EncounteredAssumes.insert(&AI);
5672     }
5673 
5674     void addAlignedBarrier(Attributor &A, CallBase &CB) {
5675       AlignedBarriers.insert(&CB);
5676     }
5677 
5678     void clearAssumeInstAndAlignedBarriers() {
5679       EncounteredAssumes.clear();
5680       AlignedBarriers.clear();
5681     }
5682 
5683     bool IsExecutedByInitialThreadOnly = true;
5684     bool IsReachedFromAlignedBarrierOnly = true;
5685     bool IsReachingAlignedBarrierOnly = true;
5686     bool EncounteredNonLocalSideEffect = false;
5687     BarriersSetTy AlignedBarriers;
5688     AssumesSetTy EncounteredAssumes;
5689   };
5690 
5691   /// Create an abstract attribute view for the position \p IRP.
5692   LLVM_ABI static AAExecutionDomain &createForPosition(const IRPosition &IRP,
5693                                                        Attributor &A);
5694 
5695   /// See AbstractAttribute::getName().
5696   StringRef getName() const override { return "AAExecutionDomain"; }
5697 
5698   /// See AbstractAttribute::getIdAddr().
5699   const char *getIdAddr() const override { return &ID; }
5700 
5701   /// Check if an instruction is executed only by the initial thread.
5702   bool isExecutedByInitialThreadOnly(const Instruction &I) const {
5703     return isExecutedByInitialThreadOnly(*I.getParent());
5704   }
5705 
5706   /// Check if a basic block is executed only by the initial thread.
5707   virtual bool isExecutedByInitialThreadOnly(const BasicBlock &) const = 0;
5708 
5709   /// Check if the instruction \p I is executed in an aligned region, that is,
5710   /// the synchronizing effects before and after \p I are both aligned barriers.
5711   /// This effectively means all threads execute \p I together.
5712   virtual bool isExecutedInAlignedRegion(Attributor &A,
5713                                          const Instruction &I) const = 0;
5714 
5715   virtual ExecutionDomainTy getExecutionDomain(const BasicBlock &) const = 0;
5716   /// Return the execution domain with which the call \p CB is entered and the
5717   /// one with which it is left.
5718   virtual std::pair<ExecutionDomainTy, ExecutionDomainTy>
5719   getExecutionDomain(const CallBase &CB) const = 0;
5720   virtual ExecutionDomainTy getFunctionExecutionDomain() const = 0;
5721 
5722   /// Helper function to determine if \p FI is a no-op given the information
5723   /// about its execution from \p ExecDomainAA.
5724   virtual bool isNoOpFence(const FenceInst &FI) const = 0;
5725 
5726   /// This function should return true if the type of the \p AA is
5727   /// AAExecutionDomain.
5728   static bool classof(const AbstractAttribute *AA) {
5729     return (AA->getIdAddr() == &ID);
5730   }
5731 
5732   /// Unique ID (due to the unique address)
5733   LLVM_ABI static const char ID;
5734 };
5735 
5736 /// An abstract Attribute for computing reachability between functions.
5737 struct AAInterFnReachability
5738     : public StateWrapper<BooleanState, AbstractAttribute> {
5739   using Base = StateWrapper<BooleanState, AbstractAttribute>;
5740 
5741   AAInterFnReachability(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
5742 
5743   /// If the function represented by this possition can reach \p Fn.
5744   bool canReach(Attributor &A, const Function &Fn) const {
5745     Function *Scope = getAnchorScope();
5746     if (!Scope || Scope->isDeclaration())
5747       return true;
5748     return instructionCanReach(A, Scope->getEntryBlock().front(), Fn);
5749   }
5750 
5751   /// Can  \p Inst reach \p Fn.
5752   /// See also AA::isPotentiallyReachable.
5753   virtual bool instructionCanReach(
5754       Attributor &A, const Instruction &Inst, const Function &Fn,
5755       const AA::InstExclusionSetTy *ExclusionSet = nullptr) const = 0;
5756 
5757   /// Create an abstract attribute view for the position \p IRP.
5758   LLVM_ABI static AAInterFnReachability &
5759   createForPosition(const IRPosition &IRP, Attributor &A);
5760 
5761   /// See AbstractAttribute::getName()
5762   StringRef getName() const override { return "AAInterFnReachability"; }
5763 
5764   /// See AbstractAttribute::getIdAddr()
5765   const char *getIdAddr() const override { return &ID; }
5766 
5767   /// This function should return true if the type of the \p AA is AACallEdges.
5768   static bool classof(const AbstractAttribute *AA) {
5769     return (AA->getIdAddr() == &ID);
5770   }
5771 
5772   /// Unique ID (due to the unique address)
5773   LLVM_ABI static const char ID;
5774 };
5775 
5776 /// An abstract Attribute for determining the necessity of the convergent
5777 /// attribute.
5778 struct AANonConvergent : public StateWrapper<BooleanState, AbstractAttribute> {
5779   using Base = StateWrapper<BooleanState, AbstractAttribute>;
5780 
5781   AANonConvergent(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
5782 
5783   /// Create an abstract attribute view for the position \p IRP.
5784   LLVM_ABI static AANonConvergent &createForPosition(const IRPosition &IRP,
5785                                                      Attributor &A);
5786 
5787   /// Return true if "non-convergent" is assumed.
5788   bool isAssumedNotConvergent() const { return getAssumed(); }
5789 
5790   /// Return true if "non-convergent" is known.
5791   bool isKnownNotConvergent() const { return getKnown(); }
5792 
5793   /// See AbstractAttribute::getName()
5794   StringRef getName() const override { return "AANonConvergent"; }
5795 
5796   /// See AbstractAttribute::getIdAddr()
5797   const char *getIdAddr() const override { return &ID; }
5798 
5799   /// This function should return true if the type of the \p AA is
5800   /// AANonConvergent.
5801   static bool classof(const AbstractAttribute *AA) {
5802     return (AA->getIdAddr() == &ID);
5803   }
5804 
5805   /// Unique ID (due to the unique address)
5806   LLVM_ABI static const char ID;
5807 };
5808 
5809 /// An abstract interface for struct information.
5810 struct AAPointerInfo : public AbstractAttribute {
5811   AAPointerInfo(const IRPosition &IRP) : AbstractAttribute(IRP) {}
5812 
5813   /// See AbstractAttribute::isValidIRPositionForInit
5814   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
5815     if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy())
5816       return false;
5817     return AbstractAttribute::isValidIRPositionForInit(A, IRP);
5818   }
5819 
5820   enum AccessKind {
5821     // First two bits to distinguish may and must accesses.
5822     AK_MUST = 1 << 0,
5823     AK_MAY = 1 << 1,
5824 
5825     // Then two bits for read and write. These are not exclusive.
5826     AK_R = 1 << 2,
5827     AK_W = 1 << 3,
5828     AK_RW = AK_R | AK_W,
5829 
5830     // One special case for assumptions about memory content. These
5831     // are neither reads nor writes. They are however always modeled
5832     // as read to avoid using them for write removal.
5833     AK_ASSUMPTION = (1 << 4) | AK_MUST,
5834 
5835     // Helper for easy access.
5836     AK_MAY_READ = AK_MAY | AK_R,
5837     AK_MAY_WRITE = AK_MAY | AK_W,
5838     AK_MAY_READ_WRITE = AK_MAY | AK_R | AK_W,
5839     AK_MUST_READ = AK_MUST | AK_R,
5840     AK_MUST_WRITE = AK_MUST | AK_W,
5841     AK_MUST_READ_WRITE = AK_MUST | AK_R | AK_W,
5842   };
5843 
5844   /// A helper containing a list of offsets computed for a Use. Ideally this
5845   /// list should be strictly ascending, but we ensure that only when we
5846   /// actually translate the list of offsets to a RangeList.
5847   struct OffsetInfo {
5848     using VecTy = SmallSet<int64_t, 4>;
5849     using const_iterator = VecTy::const_iterator;
5850     VecTy Offsets;
5851 
5852     const_iterator begin() const { return Offsets.begin(); }
5853     const_iterator end() const { return Offsets.end(); }
5854 
5855     bool operator==(const OffsetInfo &RHS) const {
5856       return Offsets == RHS.Offsets;
5857     }
5858 
5859     bool operator!=(const OffsetInfo &RHS) const { return !(*this == RHS); }
5860 
5861     bool insert(int64_t Offset) { return Offsets.insert(Offset).second; }
5862     bool isUnassigned() const { return Offsets.size() == 0; }
5863 
5864     bool isUnknown() const {
5865       if (isUnassigned())
5866         return false;
5867       if (Offsets.size() == 1)
5868         return *Offsets.begin() == AA::RangeTy::Unknown;
5869       return false;
5870     }
5871 
5872     void setUnknown() {
5873       Offsets.clear();
5874       Offsets.insert(AA::RangeTy::Unknown);
5875     }
5876 
5877     void addToAll(int64_t Inc) {
5878       VecTy NewOffsets;
5879       for (auto &Offset : Offsets)
5880         NewOffsets.insert(Offset + Inc);
5881       Offsets = std::move(NewOffsets);
5882     }
5883 
5884     /// Copy offsets from \p R into the current list.
5885     ///
5886     /// Ideally all lists should be strictly ascending, but we defer that to the
5887     /// actual use of the list. So we just blindly append here.
5888     bool merge(const OffsetInfo &R) { return set_union(Offsets, R.Offsets); }
5889   };
5890 
5891   /// A container for a list of ranges.
5892   struct RangeList {
5893     // The set of ranges rarely contains more than one element, and is unlikely
5894     // to contain more than say four elements. So we find the middle-ground with
5895     // a sorted vector. This avoids hard-coding a rarely used number like "four"
5896     // into every instance of a SmallSet.
5897     using RangeTy = AA::RangeTy;
5898     using VecTy = SmallVector<RangeTy>;
5899     using iterator = VecTy::iterator;
5900     using const_iterator = VecTy::const_iterator;
5901     VecTy Ranges;
5902 
5903     RangeList(const RangeTy &R) { Ranges.push_back(R); }
5904     RangeList(ArrayRef<int64_t> Offsets, int64_t Size) {
5905       Ranges.reserve(Offsets.size());
5906       for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
5907         assert(((i + 1 == e) || Offsets[i] < Offsets[i + 1]) &&
5908                "Expected strictly ascending offsets.");
5909         Ranges.emplace_back(Offsets[i], Size);
5910       }
5911     }
5912     RangeList() = default;
5913 
5914     iterator begin() { return Ranges.begin(); }
5915     iterator end() { return Ranges.end(); }
5916     const_iterator begin() const { return Ranges.begin(); }
5917     const_iterator end() const { return Ranges.end(); }
5918 
5919     // Helpers required for std::set_difference
5920     using value_type = RangeTy;
5921     void push_back(const RangeTy &R) {
5922       assert((Ranges.empty() || RangeTy::LessThan(Ranges.back(), R)) &&
5923              "Ensure the last element is the greatest.");
5924       Ranges.push_back(R);
5925     }
5926 
5927     /// Copy ranges from \p L that are not in \p R, into \p D.
5928     static void set_difference(const RangeList &L, const RangeList &R,
5929                                RangeList &D) {
5930       std::set_difference(L.begin(), L.end(), R.begin(), R.end(),
5931                           std::back_inserter(D), RangeTy::LessThan);
5932     }
5933 
5934     unsigned size() const { return Ranges.size(); }
5935 
5936     bool operator==(const RangeList &OI) const { return Ranges == OI.Ranges; }
5937 
5938     /// Merge the ranges in \p RHS into the current ranges.
5939     /// - Merging a list of  unknown ranges makes the current list unknown.
5940     /// - Ranges with the same offset are merged according to RangeTy::operator&
5941     /// \return true if the current RangeList changed.
5942     bool merge(const RangeList &RHS) {
5943       if (isUnknown())
5944         return false;
5945       if (RHS.isUnknown()) {
5946         setUnknown();
5947         return true;
5948       }
5949 
5950       if (Ranges.empty()) {
5951         Ranges = RHS.Ranges;
5952         return true;
5953       }
5954 
5955       bool Changed = false;
5956       auto LPos = Ranges.begin();
5957       for (auto &R : RHS.Ranges) {
5958         auto Result = insert(LPos, R);
5959         if (isUnknown())
5960           return true;
5961         LPos = Result.first;
5962         Changed |= Result.second;
5963       }
5964       return Changed;
5965     }
5966 
5967     /// Insert \p R at the given iterator \p Pos, and merge if necessary.
5968     ///
5969     /// This assumes that all ranges before \p Pos are LessThan \p R, and
5970     /// then maintains the sorted order for the suffix list.
5971     ///
5972     /// \return The place of insertion and true iff anything changed.
5973     std::pair<iterator, bool> insert(iterator Pos, const RangeTy &R) {
5974       if (isUnknown())
5975         return std::make_pair(Ranges.begin(), false);
5976       if (R.offsetOrSizeAreUnknown()) {
5977         return std::make_pair(setUnknown(), true);
5978       }
5979 
5980       // Maintain this as a sorted vector of unique entries.
5981       auto LB = std::lower_bound(Pos, Ranges.end(), R, RangeTy::LessThan);
5982       if (LB == Ranges.end() || LB->Offset != R.Offset)
5983         return std::make_pair(Ranges.insert(LB, R), true);
5984       bool Changed = *LB != R;
5985       *LB &= R;
5986       if (LB->offsetOrSizeAreUnknown())
5987         return std::make_pair(setUnknown(), true);
5988       return std::make_pair(LB, Changed);
5989     }
5990 
5991     /// Insert the given range \p R, maintaining sorted order.
5992     ///
5993     /// \return The place of insertion and true iff anything changed.
5994     std::pair<iterator, bool> insert(const RangeTy &R) {
5995       return insert(Ranges.begin(), R);
5996     }
5997 
5998     /// Add the increment \p Inc to the offset of every range.
5999     void addToAllOffsets(int64_t Inc) {
6000       assert(!isUnassigned() &&
6001              "Cannot increment if the offset is not yet computed!");
6002       if (isUnknown())
6003         return;
6004       for (auto &R : Ranges) {
6005         R.Offset += Inc;
6006       }
6007     }
6008 
6009     /// Return true iff there is exactly one range and it is known.
6010     bool isUnique() const {
6011       return Ranges.size() == 1 && !Ranges.front().offsetOrSizeAreUnknown();
6012     }
6013 
6014     /// Return the unique range, assuming it exists.
6015     const RangeTy &getUnique() const {
6016       assert(isUnique() && "No unique range to return!");
6017       return Ranges.front();
6018     }
6019 
6020     /// Return true iff the list contains an unknown range.
6021     bool isUnknown() const {
6022       if (isUnassigned())
6023         return false;
6024       if (Ranges.front().offsetOrSizeAreUnknown()) {
6025         assert(Ranges.size() == 1 && "Unknown is a singleton range.");
6026         return true;
6027       }
6028       return false;
6029     }
6030 
6031     /// Discard all ranges and insert a single unknown range.
6032     iterator setUnknown() {
6033       Ranges.clear();
6034       Ranges.push_back(RangeTy::getUnknown());
6035       return Ranges.begin();
6036     }
6037 
6038     /// Return true if no ranges have been inserted.
6039     bool isUnassigned() const { return Ranges.size() == 0; }
6040   };
6041 
6042   /// An access description.
6043   struct Access {
6044     Access(Instruction *I, int64_t Offset, int64_t Size,
6045            std::optional<Value *> Content, AccessKind Kind, Type *Ty)
6046         : LocalI(I), RemoteI(I), Content(Content), Ranges(Offset, Size),
6047           Kind(Kind), Ty(Ty) {
6048       verify();
6049     }
6050     Access(Instruction *LocalI, Instruction *RemoteI, const RangeList &Ranges,
6051            std::optional<Value *> Content, AccessKind K, Type *Ty)
6052         : LocalI(LocalI), RemoteI(RemoteI), Content(Content), Ranges(Ranges),
6053           Kind(K), Ty(Ty) {
6054       if (Ranges.size() > 1) {
6055         Kind = AccessKind(Kind | AK_MAY);
6056         Kind = AccessKind(Kind & ~AK_MUST);
6057       }
6058       verify();
6059     }
6060     Access(Instruction *LocalI, Instruction *RemoteI, int64_t Offset,
6061            int64_t Size, std::optional<Value *> Content, AccessKind Kind,
6062            Type *Ty)
6063         : LocalI(LocalI), RemoteI(RemoteI), Content(Content),
6064           Ranges(Offset, Size), Kind(Kind), Ty(Ty) {
6065       verify();
6066     }
6067     Access(const Access &Other) = default;
6068 
6069     Access &operator=(const Access &Other) = default;
6070     bool operator==(const Access &R) const {
6071       return LocalI == R.LocalI && RemoteI == R.RemoteI && Ranges == R.Ranges &&
6072              Content == R.Content && Kind == R.Kind;
6073     }
6074     bool operator!=(const Access &R) const { return !(*this == R); }
6075 
6076     Access &operator&=(const Access &R) {
6077       assert(RemoteI == R.RemoteI && "Expected same instruction!");
6078       assert(LocalI == R.LocalI && "Expected same instruction!");
6079 
6080       // Note that every Access object corresponds to a unique Value, and only
6081       // accesses to the same Value are merged. Hence we assume that all ranges
6082       // are the same size. If ranges can be different size, then the contents
6083       // must be dropped.
6084       Ranges.merge(R.Ranges);
6085       Content =
6086           AA::combineOptionalValuesInAAValueLatice(Content, R.Content, Ty);
6087 
6088       // Combine the access kind, which results in a bitwise union.
6089       // If there is more than one range, then this must be a MAY.
6090       // If we combine a may and a must access we clear the must bit.
6091       Kind = AccessKind(Kind | R.Kind);
6092       if ((Kind & AK_MAY) || Ranges.size() > 1) {
6093         Kind = AccessKind(Kind | AK_MAY);
6094         Kind = AccessKind(Kind & ~AK_MUST);
6095       }
6096       verify();
6097       return *this;
6098     }
6099 
6100     void verify() {
6101       assert(isMustAccess() + isMayAccess() == 1 &&
6102              "Expect must or may access, not both.");
6103       assert(isAssumption() + isWrite() <= 1 &&
6104              "Expect assumption access or write access, never both.");
6105       assert((isMayAccess() || Ranges.size() == 1) &&
6106              "Cannot be a must access if there are multiple ranges.");
6107     }
6108 
6109     /// Return the access kind.
6110     AccessKind getKind() const { return Kind; }
6111 
6112     /// Return true if this is a read access.
6113     bool isRead() const { return Kind & AK_R; }
6114 
6115     /// Return true if this is a write access.
6116     bool isWrite() const { return Kind & AK_W; }
6117 
6118     /// Return true if this is a write access.
6119     bool isWriteOrAssumption() const { return isWrite() || isAssumption(); }
6120 
6121     /// Return true if this is an assumption access.
6122     bool isAssumption() const { return Kind == AK_ASSUMPTION; }
6123 
6124     bool isMustAccess() const {
6125       bool MustAccess = Kind & AK_MUST;
6126       assert((!MustAccess || Ranges.size() < 2) &&
6127              "Cannot be a must access if there are multiple ranges.");
6128       return MustAccess;
6129     }
6130 
6131     bool isMayAccess() const {
6132       bool MayAccess = Kind & AK_MAY;
6133       assert((MayAccess || Ranges.size() < 2) &&
6134              "Cannot be a must access if there are multiple ranges.");
6135       return MayAccess;
6136     }
6137 
6138     /// Return the instruction that causes the access with respect to the local
6139     /// scope of the associated attribute.
6140     Instruction *getLocalInst() const { return LocalI; }
6141 
6142     /// Return the actual instruction that causes the access.
6143     Instruction *getRemoteInst() const { return RemoteI; }
6144 
6145     /// Return true if the value written is not known yet.
6146     bool isWrittenValueYetUndetermined() const { return !Content; }
6147 
6148     /// Return true if the value written cannot be determined at all.
6149     bool isWrittenValueUnknown() const {
6150       return Content.has_value() && !*Content;
6151     }
6152 
6153     /// Set the value written to nullptr, i.e., unknown.
6154     void setWrittenValueUnknown() { Content = nullptr; }
6155 
6156     /// Return the type associated with the access, if known.
6157     Type *getType() const { return Ty; }
6158 
6159     /// Return the value writen, if any.
6160     Value *getWrittenValue() const {
6161       assert(!isWrittenValueYetUndetermined() &&
6162              "Value needs to be determined before accessing it.");
6163       return *Content;
6164     }
6165 
6166     /// Return the written value which can be `llvm::null` if it is not yet
6167     /// determined.
6168     std::optional<Value *> getContent() const { return Content; }
6169 
6170     bool hasUniqueRange() const { return Ranges.isUnique(); }
6171     const AA::RangeTy &getUniqueRange() const { return Ranges.getUnique(); }
6172 
6173     /// Add a range accessed by this Access.
6174     ///
6175     /// If there are multiple ranges, then this is a "may access".
6176     void addRange(int64_t Offset, int64_t Size) {
6177       Ranges.insert({Offset, Size});
6178       if (!hasUniqueRange()) {
6179         Kind = AccessKind(Kind | AK_MAY);
6180         Kind = AccessKind(Kind & ~AK_MUST);
6181       }
6182     }
6183 
6184     const RangeList &getRanges() const { return Ranges; }
6185 
6186     using const_iterator = RangeList::const_iterator;
6187     const_iterator begin() const { return Ranges.begin(); }
6188     const_iterator end() const { return Ranges.end(); }
6189 
6190   private:
6191     /// The instruction responsible for the access with respect to the local
6192     /// scope of the associated attribute.
6193     Instruction *LocalI;
6194 
6195     /// The instruction responsible for the access.
6196     Instruction *RemoteI;
6197 
6198     /// The value written, if any. `std::nullopt` means "not known yet",
6199     /// `nullptr` cannot be determined.
6200     std::optional<Value *> Content;
6201 
6202     /// Set of potential ranges accessed from the base pointer.
6203     RangeList Ranges;
6204 
6205     /// The access kind, e.g., READ, as bitset (could be more than one).
6206     AccessKind Kind;
6207 
6208     /// The type of the content, thus the type read/written, can be null if not
6209     /// available.
6210     Type *Ty;
6211   };
6212 
6213   /// Create an abstract attribute view for the position \p IRP.
6214   LLVM_ABI static AAPointerInfo &createForPosition(const IRPosition &IRP,
6215                                                    Attributor &A);
6216 
6217   /// See AbstractAttribute::getName()
6218   StringRef getName() const override { return "AAPointerInfo"; }
6219 
6220   /// See AbstractAttribute::getIdAddr()
6221   const char *getIdAddr() const override { return &ID; }
6222 
6223   using OffsetBinsTy = DenseMap<AA::RangeTy, SmallSet<unsigned, 4>>;
6224   using const_bin_iterator = OffsetBinsTy::const_iterator;
6225   virtual const_bin_iterator begin() const = 0;
6226   virtual const_bin_iterator end() const = 0;
6227   virtual int64_t numOffsetBins() const = 0;
6228   virtual bool reachesReturn() const = 0;
6229   virtual void addReturnedOffsetsTo(OffsetInfo &) const = 0;
6230 
6231   /// Call \p CB on all accesses that might interfere with \p Range and return
6232   /// true if all such accesses were known and the callback returned true for
6233   /// all of them, false otherwise. An access interferes with an offset-size
6234   /// pair if it might read or write that memory region.
6235   virtual bool forallInterferingAccesses(
6236       AA::RangeTy Range, function_ref<bool(const Access &, bool)> CB) const = 0;
6237 
6238   /// Call \p CB on all accesses that might interfere with \p I and
6239   /// return true if all such accesses were known and the callback returned true
6240   /// for all of them, false otherwise. In contrast to forallInterferingAccesses
6241   /// this function will perform reasoning to exclude write accesses that cannot
6242   /// affect the load even if they on the surface look as if they would. The
6243   /// flag \p HasBeenWrittenTo will be set to true if we know that \p I does not
6244   /// read the initial value of the underlying memory. If \p SkipCB is given and
6245   /// returns false for a potentially interfering access, that access is not
6246   /// checked for actual interference.
6247   virtual bool forallInterferingAccesses(
6248       Attributor &A, const AbstractAttribute &QueryingAA, Instruction &I,
6249       bool FindInterferingWrites, bool FindInterferingReads,
6250       function_ref<bool(const Access &, bool)> CB, bool &HasBeenWrittenTo,
6251       AA::RangeTy &Range,
6252       function_ref<bool(const Access &)> SkipCB = nullptr) const = 0;
6253 
6254   /// This function should return true if the type of the \p AA is AAPointerInfo
6255   static bool classof(const AbstractAttribute *AA) {
6256     return (AA->getIdAddr() == &ID);
6257   }
6258 
6259   /// Unique ID (due to the unique address)
6260   LLVM_ABI static const char ID;
6261 };
6262 
6263 LLVM_ABI raw_ostream &operator<<(raw_ostream &, const AAPointerInfo::Access &);
6264 
6265 /// An abstract attribute for getting assumption information.
6266 struct AAAssumptionInfo
6267     : public StateWrapper<SetState<StringRef>, AbstractAttribute,
6268                           DenseSet<StringRef>> {
6269   using Base =
6270       StateWrapper<SetState<StringRef>, AbstractAttribute, DenseSet<StringRef>>;
6271 
6272   AAAssumptionInfo(const IRPosition &IRP, Attributor &A,
6273                    const DenseSet<StringRef> &Known)
6274       : Base(IRP, Known) {}
6275 
6276   /// Returns true if the assumption set contains the assumption \p Assumption.
6277   virtual bool hasAssumption(const StringRef Assumption) const = 0;
6278 
6279   /// Create an abstract attribute view for the position \p IRP.
6280   LLVM_ABI static AAAssumptionInfo &createForPosition(const IRPosition &IRP,
6281                                                       Attributor &A);
6282 
6283   /// See AbstractAttribute::getName()
6284   StringRef getName() const override { return "AAAssumptionInfo"; }
6285 
6286   /// See AbstractAttribute::getIdAddr()
6287   const char *getIdAddr() const override { return &ID; }
6288 
6289   /// This function should return true if the type of the \p AA is
6290   /// AAAssumptionInfo
6291   static bool classof(const AbstractAttribute *AA) {
6292     return (AA->getIdAddr() == &ID);
6293   }
6294 
6295   /// Unique ID (due to the unique address)
6296   LLVM_ABI static const char ID;
6297 };
6298 
6299 /// An abstract attribute for getting all assumption underlying objects.
6300 struct AAUnderlyingObjects : AbstractAttribute {
6301   AAUnderlyingObjects(const IRPosition &IRP) : AbstractAttribute(IRP) {}
6302 
6303   /// See AbstractAttribute::isValidIRPositionForInit
6304   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
6305     if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy())
6306       return false;
6307     return AbstractAttribute::isValidIRPositionForInit(A, IRP);
6308   }
6309 
6310   /// See AbstractAttribute::requiresCallersForArgOrFunction
6311   static bool requiresCallersForArgOrFunction() { return true; }
6312 
6313   /// Create an abstract attribute biew for the position \p IRP.
6314   LLVM_ABI static AAUnderlyingObjects &createForPosition(const IRPosition &IRP,
6315                                                          Attributor &A);
6316 
6317   /// See AbstractAttribute::getName()
6318   StringRef getName() const override { return "AAUnderlyingObjects"; }
6319 
6320   /// See AbstractAttribute::getIdAddr()
6321   const char *getIdAddr() const override { return &ID; }
6322 
6323   /// This function should return true if the type of the \p AA is
6324   /// AAUnderlyingObjects.
6325   static bool classof(const AbstractAttribute *AA) {
6326     return (AA->getIdAddr() == &ID);
6327   }
6328 
6329   /// Unique ID (due to the unique address)
6330   LLVM_ABI static const char ID;
6331 
6332   /// Check \p Pred on all underlying objects in \p Scope collected so far.
6333   ///
6334   /// This method will evaluate \p Pred on all underlying objects in \p Scope
6335   /// collected so far and return true if \p Pred holds on all of them.
6336   virtual bool
6337   forallUnderlyingObjects(function_ref<bool(Value &)> Pred,
6338                           AA::ValueScope Scope = AA::Interprocedural) const = 0;
6339 };
6340 
6341 /// An abstract interface for identifying pointers from which loads can be
6342 /// marked invariant.
6343 struct AAInvariantLoadPointer : public AbstractAttribute {
6344   AAInvariantLoadPointer(const IRPosition &IRP) : AbstractAttribute(IRP) {}
6345 
6346   /// See AbstractAttribute::isValidIRPositionForInit
6347   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
6348     if (!IRP.getAssociatedType()->isPointerTy())
6349       return false;
6350 
6351     return AbstractAttribute::isValidIRPositionForInit(A, IRP);
6352   }
6353 
6354   /// Create an abstract attribute view for the position \p IRP.
6355   LLVM_ABI static AAInvariantLoadPointer &
6356   createForPosition(const IRPosition &IRP, Attributor &A);
6357 
6358   /// Return true if the pointer's contents are known to remain invariant.
6359   virtual bool isKnownInvariant() const = 0;
6360   virtual bool isKnownLocallyInvariant() const = 0;
6361 
6362   /// Return true if the pointer's contents are assumed to remain invariant.
6363   virtual bool isAssumedInvariant() const = 0;
6364   virtual bool isAssumedLocallyInvariant() const = 0;
6365 
6366   /// See AbstractAttribute::getName().
6367   StringRef getName() const override { return "AAInvariantLoadPointer"; }
6368 
6369   /// See AbstractAttribute::getIdAddr().
6370   const char *getIdAddr() const override { return &ID; }
6371 
6372   /// This function should return true if the type of the \p AA is
6373   /// AAInvariantLoadPointer
6374   static bool classof(const AbstractAttribute *AA) {
6375     return (AA->getIdAddr() == &ID);
6376   }
6377 
6378   /// Unique ID (due to the unique address).
6379   LLVM_ABI static const char ID;
6380 };
6381 
6382 /// An abstract interface for address space information.
6383 struct AAAddressSpace : public StateWrapper<BooleanState, AbstractAttribute> {
6384   AAAddressSpace(const IRPosition &IRP, Attributor &A)
6385       : StateWrapper<BooleanState, AbstractAttribute>(IRP) {}
6386 
6387   /// See AbstractAttribute::isValidIRPositionForInit
6388   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
6389     if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy())
6390       return false;
6391     return AbstractAttribute::isValidIRPositionForInit(A, IRP);
6392   }
6393 
6394   /// See AbstractAttribute::requiresCallersForArgOrFunction
6395   static bool requiresCallersForArgOrFunction() { return true; }
6396 
6397   /// Return the address space of the associated value. \p NoAddressSpace is
6398   /// returned if the associated value is dead. This functions is not supposed
6399   /// to be called if the AA is invalid.
6400   virtual uint32_t getAddressSpace() const = 0;
6401 
6402   /// Create an abstract attribute view for the position \p IRP.
6403   LLVM_ABI static AAAddressSpace &createForPosition(const IRPosition &IRP,
6404                                                     Attributor &A);
6405 
6406   /// See AbstractAttribute::getName()
6407   StringRef getName() const override { return "AAAddressSpace"; }
6408 
6409   /// See AbstractAttribute::getIdAddr()
6410   const char *getIdAddr() const override { return &ID; }
6411 
6412   /// This function should return true if the type of the \p AA is
6413   /// AAAssumptionInfo
6414   static bool classof(const AbstractAttribute *AA) {
6415     return (AA->getIdAddr() == &ID);
6416   }
6417 
6418   /// Unique ID (due to the unique address)
6419   LLVM_ABI static const char ID;
6420 
6421 protected:
6422   // Invalid address space which indicates the associated value is dead.
6423   static const uint32_t InvalidAddressSpace = ~0U;
6424 };
6425 
6426 /// An abstract interface for potential address space information.
6427 struct AANoAliasAddrSpace
6428     : public StateWrapper<BooleanState, AbstractAttribute> {
6429   using Base = StateWrapper<BooleanState, AbstractAttribute>;
6430   using RangeMap = IntervalMap<unsigned, bool>;
6431   AANoAliasAddrSpace(const IRPosition &IRP, Attributor &A)
6432       : Base(IRP), Map(Allocator) {}
6433 
6434   /// See AbstractAttribute::isValidIRPositionForInit
6435   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
6436     if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy())
6437       return false;
6438     return AbstractAttribute::isValidIRPositionForInit(A, IRP);
6439   }
6440 
6441   /// See AbstractAttribute::requiresCallersForArgOrFunction
6442   static bool requiresCallersForArgOrFunction() { return true; }
6443 
6444   /// Create an abstract attribute view for the position \p IRP.
6445   LLVM_ABI static AANoAliasAddrSpace &createForPosition(const IRPosition &IRP,
6446                                                         Attributor &A);
6447   /// See AbstractAttribute::getName()
6448   StringRef getName() const override { return "AANoAliasAddrSpace"; }
6449 
6450   /// See AbstractAttribute::getIdAddr()
6451   const char *getIdAddr() const override { return &ID; }
6452 
6453   /// This function should return true if the type of the \p AA is
6454   /// AAAssumptionInfo
6455   static bool classof(const AbstractAttribute *AA) {
6456     return (AA->getIdAddr() == &ID);
6457   }
6458 
6459   /// Unique ID (due to the unique address)
6460   LLVM_ABI static const char ID;
6461 
6462 protected:
6463   RangeMap::Allocator Allocator;
6464   RangeMap Map;
6465 };
6466 
6467 struct AAAllocationInfo : public StateWrapper<BooleanState, AbstractAttribute> {
6468   AAAllocationInfo(const IRPosition &IRP, Attributor &A)
6469       : StateWrapper<BooleanState, AbstractAttribute>(IRP) {}
6470 
6471   /// See AbstractAttribute::isValidIRPositionForInit
6472   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
6473     if (!IRP.getAssociatedType()->isPtrOrPtrVectorTy())
6474       return false;
6475     return AbstractAttribute::isValidIRPositionForInit(A, IRP);
6476   }
6477 
6478   /// Create an abstract attribute view for the position \p IRP.
6479   LLVM_ABI static AAAllocationInfo &createForPosition(const IRPosition &IRP,
6480                                                       Attributor &A);
6481 
6482   virtual std::optional<TypeSize> getAllocatedSize() const = 0;
6483 
6484   /// See AbstractAttribute::getName()
6485   StringRef getName() const override { return "AAAllocationInfo"; }
6486 
6487   /// See AbstractAttribute::getIdAddr()
6488   const char *getIdAddr() const override { return &ID; }
6489 
6490   /// This function should return true if the type of the \p AA is
6491   /// AAAllocationInfo
6492   static bool classof(const AbstractAttribute *AA) {
6493     return (AA->getIdAddr() == &ID);
6494   }
6495 
6496   constexpr static const std::optional<TypeSize> HasNoAllocationSize =
6497       std::optional<TypeSize>(TypeSize(-1, true));
6498 
6499   LLVM_ABI static const char ID;
6500 };
6501 
6502 /// An abstract interface for llvm::GlobalValue information interference.
6503 struct AAGlobalValueInfo
6504     : public StateWrapper<BooleanState, AbstractAttribute> {
6505   AAGlobalValueInfo(const IRPosition &IRP, Attributor &A)
6506       : StateWrapper<BooleanState, AbstractAttribute>(IRP) {}
6507 
6508   /// See AbstractAttribute::isValidIRPositionForInit
6509   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
6510     if (IRP.getPositionKind() != IRPosition::IRP_FLOAT)
6511       return false;
6512     auto *GV = dyn_cast<GlobalValue>(&IRP.getAnchorValue());
6513     if (!GV)
6514       return false;
6515     return GV->hasLocalLinkage();
6516   }
6517 
6518   /// Create an abstract attribute view for the position \p IRP.
6519   LLVM_ABI static AAGlobalValueInfo &createForPosition(const IRPosition &IRP,
6520                                                        Attributor &A);
6521 
6522   /// Return true iff \p U is a potential use of the associated global value.
6523   virtual bool isPotentialUse(const Use &U) const = 0;
6524 
6525   /// See AbstractAttribute::getName()
6526   StringRef getName() const override { return "AAGlobalValueInfo"; }
6527 
6528   /// See AbstractAttribute::getIdAddr()
6529   const char *getIdAddr() const override { return &ID; }
6530 
6531   /// This function should return true if the type of the \p AA is
6532   /// AAGlobalValueInfo
6533   static bool classof(const AbstractAttribute *AA) {
6534     return (AA->getIdAddr() == &ID);
6535   }
6536 
6537   /// Unique ID (due to the unique address)
6538   LLVM_ABI static const char ID;
6539 };
6540 
6541 /// An abstract interface for indirect call information interference.
6542 struct AAIndirectCallInfo
6543     : public StateWrapper<BooleanState, AbstractAttribute> {
6544   AAIndirectCallInfo(const IRPosition &IRP, Attributor &A)
6545       : StateWrapper<BooleanState, AbstractAttribute>(IRP) {}
6546 
6547   /// See AbstractAttribute::isValidIRPositionForInit
6548   static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP) {
6549     if (IRP.getPositionKind() != IRPosition::IRP_CALL_SITE)
6550       return false;
6551     auto *CB = cast<CallBase>(IRP.getCtxI());
6552     return CB->getOpcode() == Instruction::Call && CB->isIndirectCall() &&
6553            !CB->isMustTailCall();
6554   }
6555 
6556   /// Create an abstract attribute view for the position \p IRP.
6557   LLVM_ABI static AAIndirectCallInfo &createForPosition(const IRPosition &IRP,
6558                                                         Attributor &A);
6559 
6560   /// Call \CB on each potential callee value and return true if all were known
6561   /// and \p CB returned true on all of them. Otherwise, return false.
6562   virtual bool foreachCallee(function_ref<bool(Function *)> CB) const = 0;
6563 
6564   /// See AbstractAttribute::getName()
6565   StringRef getName() const override { return "AAIndirectCallInfo"; }
6566 
6567   /// See AbstractAttribute::getIdAddr()
6568   const char *getIdAddr() const override { return &ID; }
6569 
6570   /// This function should return true if the type of the \p AA is
6571   /// AAIndirectCallInfo
6572   /// This function should return true if the type of the \p AA is
6573   /// AADenormalFPMath.
6574   static bool classof(const AbstractAttribute *AA) {
6575     return (AA->getIdAddr() == &ID);
6576   }
6577 
6578   /// Unique ID (due to the unique address)
6579   LLVM_ABI static const char ID;
6580 };
6581 
6582 /// An abstract Attribute for specializing "dynamic" components of
6583 /// "denormal-fp-math" and "denormal-fp-math-f32" to a known denormal mode.
6584 struct AADenormalFPMath
6585     : public StateWrapper<DenormalFPMathState, AbstractAttribute> {
6586   using Base = StateWrapper<DenormalFPMathState, AbstractAttribute>;
6587 
6588   AADenormalFPMath(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
6589 
6590   /// Create an abstract attribute view for the position \p IRP.
6591   LLVM_ABI static AADenormalFPMath &createForPosition(const IRPosition &IRP,
6592                                                       Attributor &A);
6593 
6594   /// See AbstractAttribute::getName()
6595   StringRef getName() const override { return "AADenormalFPMath"; }
6596 
6597   /// See AbstractAttribute::getIdAddr()
6598   const char *getIdAddr() const override { return &ID; }
6599 
6600   /// This function should return true if the type of the \p AA is
6601   /// AADenormalFPMath.
6602   static bool classof(const AbstractAttribute *AA) {
6603     return (AA->getIdAddr() == &ID);
6604   }
6605 
6606   /// Unique ID (due to the unique address)
6607   LLVM_ABI static const char ID;
6608 };
6609 
6610 /// Run options, used by the pass manager.
6611 enum AttributorRunOption {
6612   NONE = 0,
6613   MODULE = 1 << 0,
6614   CGSCC = 1 << 1,
6615   ALL = MODULE | CGSCC
6616 };
6617 
6618 namespace AA {
6619 /// Helper to avoid creating an AA for IR Attributes that might already be set.
6620 template <Attribute::AttrKind AK, typename AAType = AbstractAttribute>
6621 bool hasAssumedIRAttr(Attributor &A, const AbstractAttribute *QueryingAA,
6622                       const IRPosition &IRP, DepClassTy DepClass, bool &IsKnown,
6623                       bool IgnoreSubsumingPositions = false,
6624                       const AAType **AAPtr = nullptr) {
6625   IsKnown = false;
6626   switch (AK) {
6627 #define CASE(ATTRNAME, AANAME, ...)                                            \
6628   case Attribute::ATTRNAME: {                                                  \
6629     if (AANAME::isImpliedByIR(A, IRP, AK, IgnoreSubsumingPositions))           \
6630       return IsKnown = true;                                                   \
6631     if (!QueryingAA)                                                           \
6632       return false;                                                            \
6633     const auto *AA = A.getAAFor<AANAME>(*QueryingAA, IRP, DepClass);           \
6634     if (AAPtr)                                                                 \
6635       *AAPtr = reinterpret_cast<const AAType *>(AA);                           \
6636     if (!AA || !AA->isAssumed(__VA_ARGS__))                                    \
6637       return false;                                                            \
6638     IsKnown = AA->isKnown(__VA_ARGS__);                                        \
6639     return true;                                                               \
6640   }
6641     CASE(NoUnwind, AANoUnwind, );
6642     CASE(WillReturn, AAWillReturn, );
6643     CASE(NoFree, AANoFree, );
6644     CASE(Captures, AANoCapture, );
6645     CASE(NoRecurse, AANoRecurse, );
6646     CASE(NoReturn, AANoReturn, );
6647     CASE(NoSync, AANoSync, );
6648     CASE(NoAlias, AANoAlias, );
6649     CASE(NonNull, AANonNull, );
6650     CASE(MustProgress, AAMustProgress, );
6651     CASE(NoUndef, AANoUndef, );
6652     CASE(ReadNone, AAMemoryBehavior, AAMemoryBehavior::NO_ACCESSES);
6653     CASE(ReadOnly, AAMemoryBehavior, AAMemoryBehavior::NO_WRITES);
6654     CASE(WriteOnly, AAMemoryBehavior, AAMemoryBehavior::NO_READS);
6655 #undef CASE
6656   default:
6657     llvm_unreachable("hasAssumedIRAttr not available for this attribute kind");
6658   };
6659 }
6660 } // namespace AA
6661 
6662 } // end namespace llvm
6663 
6664 #endif // LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
6665