1 //===- AttributorAttributes.cpp - Attributes for Attributor deduction -----===//
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 // See the Attributor.h file comment and the class descriptions in that file for
10 // more information.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/IPO/Attributor.h"
15 
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/MapVector.h"
18 #include "llvm/ADT/SCCIterator.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetOperations.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/AssumeBundleQueries.h"
25 #include "llvm/Analysis/AssumptionCache.h"
26 #include "llvm/Analysis/CaptureTracking.h"
27 #include "llvm/Analysis/InstructionSimplify.h"
28 #include "llvm/Analysis/LazyValueInfo.h"
29 #include "llvm/Analysis/MemoryBuiltins.h"
30 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
31 #include "llvm/Analysis/ScalarEvolution.h"
32 #include "llvm/Analysis/TargetTransformInfo.h"
33 #include "llvm/Analysis/ValueTracking.h"
34 #include "llvm/IR/Assumptions.h"
35 #include "llvm/IR/Constants.h"
36 #include "llvm/IR/DataLayout.h"
37 #include "llvm/IR/IRBuilder.h"
38 #include "llvm/IR/Instruction.h"
39 #include "llvm/IR/Instructions.h"
40 #include "llvm/IR/IntrinsicInst.h"
41 #include "llvm/IR/Value.h"
42 #include "llvm/IR/NoFolder.h"
43 #include "llvm/Support/Alignment.h"
44 #include "llvm/Support/Casting.h"
45 #include "llvm/Support/CommandLine.h"
46 #include "llvm/Support/ErrorHandling.h"
47 #include "llvm/Support/FileSystem.h"
48 #include "llvm/Support/MathExtras.h"
49 #include "llvm/Support/raw_ostream.h"
50 #include "llvm/Transforms/IPO/ArgumentPromotion.h"
51 #include "llvm/Transforms/Utils/Local.h"
52 #include <cassert>
53 
54 using namespace llvm;
55 
56 #define DEBUG_TYPE "attributor"
57 
58 static cl::opt<bool> ManifestInternal(
59     "attributor-manifest-internal", cl::Hidden,
60     cl::desc("Manifest Attributor internal string attributes."),
61     cl::init(false));
62 
63 static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
64                                        cl::Hidden);
65 
66 template <>
67 unsigned llvm::PotentialConstantIntValuesState::MaxPotentialValues = 0;
68 
69 static cl::opt<unsigned, true> MaxPotentialValues(
70     "attributor-max-potential-values", cl::Hidden,
71     cl::desc("Maximum number of potential values to be "
72              "tracked for each position."),
73     cl::location(llvm::PotentialConstantIntValuesState::MaxPotentialValues),
74     cl::init(7));
75 
76 static cl::opt<unsigned> MaxInterferingAccesses(
77     "attributor-max-interfering-accesses", cl::Hidden,
78     cl::desc("Maximum number of interfering accesses to "
79              "check before assuming all might interfere."),
80     cl::init(6));
81 
82 STATISTIC(NumAAs, "Number of abstract attributes created");
83 
84 // Some helper macros to deal with statistics tracking.
85 //
86 // Usage:
87 // For simple IR attribute tracking overload trackStatistics in the abstract
88 // attribute and choose the right STATS_DECLTRACK_********* macro,
89 // e.g.,:
90 //  void trackStatistics() const override {
91 //    STATS_DECLTRACK_ARG_ATTR(returned)
92 //  }
93 // If there is a single "increment" side one can use the macro
94 // STATS_DECLTRACK with a custom message. If there are multiple increment
95 // sides, STATS_DECL and STATS_TRACK can also be used separately.
96 //
97 #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)                                     \
98   ("Number of " #TYPE " marked '" #NAME "'")
99 #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
100 #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
101 #define STATS_DECL(NAME, TYPE, MSG)                                            \
102   STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
103 #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
104 #define STATS_DECLTRACK(NAME, TYPE, MSG)                                       \
105   {                                                                            \
106     STATS_DECL(NAME, TYPE, MSG)                                                \
107     STATS_TRACK(NAME, TYPE)                                                    \
108   }
109 #define STATS_DECLTRACK_ARG_ATTR(NAME)                                         \
110   STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
111 #define STATS_DECLTRACK_CSARG_ATTR(NAME)                                       \
112   STATS_DECLTRACK(NAME, CSArguments,                                           \
113                   BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
114 #define STATS_DECLTRACK_FN_ATTR(NAME)                                          \
115   STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
116 #define STATS_DECLTRACK_CS_ATTR(NAME)                                          \
117   STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
118 #define STATS_DECLTRACK_FNRET_ATTR(NAME)                                       \
119   STATS_DECLTRACK(NAME, FunctionReturn,                                        \
120                   BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
121 #define STATS_DECLTRACK_CSRET_ATTR(NAME)                                       \
122   STATS_DECLTRACK(NAME, CSReturn,                                              \
123                   BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
124 #define STATS_DECLTRACK_FLOATING_ATTR(NAME)                                    \
125   STATS_DECLTRACK(NAME, Floating,                                              \
126                   ("Number of floating values known to be '" #NAME "'"))
127 
128 // Specialization of the operator<< for abstract attributes subclasses. This
129 // disambiguates situations where multiple operators are applicable.
130 namespace llvm {
131 #define PIPE_OPERATOR(CLASS)                                                   \
132   raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) {                  \
133     return OS << static_cast<const AbstractAttribute &>(AA);                   \
134   }
135 
136 PIPE_OPERATOR(AAIsDead)
137 PIPE_OPERATOR(AANoUnwind)
138 PIPE_OPERATOR(AANoSync)
139 PIPE_OPERATOR(AANoRecurse)
140 PIPE_OPERATOR(AAWillReturn)
141 PIPE_OPERATOR(AANoReturn)
142 PIPE_OPERATOR(AAReturnedValues)
143 PIPE_OPERATOR(AANonNull)
144 PIPE_OPERATOR(AANoAlias)
145 PIPE_OPERATOR(AADereferenceable)
146 PIPE_OPERATOR(AAAlign)
147 PIPE_OPERATOR(AANoCapture)
148 PIPE_OPERATOR(AAValueSimplify)
149 PIPE_OPERATOR(AANoFree)
150 PIPE_OPERATOR(AAHeapToStack)
151 PIPE_OPERATOR(AAReachability)
152 PIPE_OPERATOR(AAMemoryBehavior)
153 PIPE_OPERATOR(AAMemoryLocation)
154 PIPE_OPERATOR(AAValueConstantRange)
155 PIPE_OPERATOR(AAPrivatizablePtr)
156 PIPE_OPERATOR(AAUndefinedBehavior)
157 PIPE_OPERATOR(AAPotentialValues)
158 PIPE_OPERATOR(AANoUndef)
159 PIPE_OPERATOR(AACallEdges)
160 PIPE_OPERATOR(AAFunctionReachability)
161 PIPE_OPERATOR(AAPointerInfo)
162 PIPE_OPERATOR(AAAssumptionInfo)
163 
164 #undef PIPE_OPERATOR
165 
166 template <>
167 ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
168                                                      const DerefState &R) {
169   ChangeStatus CS0 =
170       clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
171   ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
172   return CS0 | CS1;
173 }
174 
175 } // namespace llvm
176 
177 /// Get pointer operand of memory accessing instruction. If \p I is
178 /// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
179 /// is set to false and the instruction is volatile, return nullptr.
180 static const Value *getPointerOperand(const Instruction *I,
181                                       bool AllowVolatile) {
182   if (!AllowVolatile && I->isVolatile())
183     return nullptr;
184 
185   if (auto *LI = dyn_cast<LoadInst>(I)) {
186     return LI->getPointerOperand();
187   }
188 
189   if (auto *SI = dyn_cast<StoreInst>(I)) {
190     return SI->getPointerOperand();
191   }
192 
193   if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
194     return CXI->getPointerOperand();
195   }
196 
197   if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
198     return RMWI->getPointerOperand();
199   }
200 
201   return nullptr;
202 }
203 
204 /// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
205 /// advanced by \p Offset bytes. To aid later analysis the method tries to build
206 /// getelement pointer instructions that traverse the natural type of \p Ptr if
207 /// possible. If that fails, the remaining offset is adjusted byte-wise, hence
208 /// through a cast to i8*.
209 ///
210 /// TODO: This could probably live somewhere more prominantly if it doesn't
211 ///       already exist.
212 static Value *constructPointer(Type *ResTy, Type *PtrElemTy, Value *Ptr,
213                                int64_t Offset, IRBuilder<NoFolder> &IRB,
214                                const DataLayout &DL) {
215   assert(Offset >= 0 && "Negative offset not supported yet!");
216   LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset
217                     << "-bytes as " << *ResTy << "\n");
218 
219   if (Offset) {
220     Type *Ty = PtrElemTy;
221     APInt IntOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), Offset);
222     SmallVector<APInt> IntIndices = DL.getGEPIndicesForOffset(Ty, IntOffset);
223 
224     SmallVector<Value *, 4> ValIndices;
225     std::string GEPName = Ptr->getName().str();
226     for (const APInt &Index : IntIndices) {
227       ValIndices.push_back(IRB.getInt(Index));
228       GEPName += "." + std::to_string(Index.getZExtValue());
229     }
230 
231     // Create a GEP for the indices collected above.
232     Ptr = IRB.CreateGEP(PtrElemTy, Ptr, ValIndices, GEPName);
233 
234     // If an offset is left we use byte-wise adjustment.
235     if (IntOffset != 0) {
236       Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
237       Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt(IntOffset),
238                           GEPName + ".b" + Twine(IntOffset.getZExtValue()));
239     }
240   }
241 
242   // Ensure the result has the requested type.
243   Ptr = IRB.CreatePointerBitCastOrAddrSpaceCast(Ptr, ResTy,
244                                                 Ptr->getName() + ".cast");
245 
246   LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n");
247   return Ptr;
248 }
249 
250 /// Recursively visit all values that might become \p IRP at some point. This
251 /// will be done by looking through cast instructions, selects, phis, and calls
252 /// with the "returned" attribute. Once we cannot look through the value any
253 /// further, the callback \p VisitValueCB is invoked and passed the current
254 /// value, the \p State, and a flag to indicate if we stripped anything.
255 /// Stripped means that we unpacked the value associated with \p IRP at least
256 /// once. Note that the value used for the callback may still be the value
257 /// associated with \p IRP (due to PHIs). To limit how much effort is invested,
258 /// we will never visit more values than specified by \p MaxValues.
259 /// If \p Intraprocedural is set to true only values valid in the scope of
260 /// \p CtxI will be visited and simplification into other scopes is prevented.
261 template <typename StateTy>
262 static bool genericValueTraversal(
263     Attributor &A, IRPosition IRP, const AbstractAttribute &QueryingAA,
264     StateTy &State,
265     function_ref<bool(Value &, const Instruction *, StateTy &, bool)>
266         VisitValueCB,
267     const Instruction *CtxI, bool &UsedAssumedInformation,
268     bool UseValueSimplify = true, int MaxValues = 16,
269     function_ref<Value *(Value *)> StripCB = nullptr,
270     bool Intraprocedural = false) {
271 
272   struct LivenessInfo {
273     const AAIsDead *LivenessAA = nullptr;
274     bool AnyDead = false;
275   };
276   SmallMapVector<const Function *, LivenessInfo, 4> LivenessAAs;
277   auto GetLivenessInfo = [&](const Function &F) -> LivenessInfo & {
278     LivenessInfo &LI = LivenessAAs[&F];
279     if (!LI.LivenessAA)
280       LI.LivenessAA = &A.getAAFor<AAIsDead>(QueryingAA, IRPosition::function(F),
281                                             DepClassTy::NONE);
282     return LI;
283   };
284 
285   Value *InitialV = &IRP.getAssociatedValue();
286   using Item = std::pair<Value *, const Instruction *>;
287   SmallSet<Item, 16> Visited;
288   SmallVector<Item, 16> Worklist;
289   Worklist.push_back({InitialV, CtxI});
290 
291   int Iteration = 0;
292   do {
293     Item I = Worklist.pop_back_val();
294     Value *V = I.first;
295     CtxI = I.second;
296     if (StripCB)
297       V = StripCB(V);
298 
299     // Check if we should process the current value. To prevent endless
300     // recursion keep a record of the values we followed!
301     if (!Visited.insert(I).second)
302       continue;
303 
304     // Make sure we limit the compile time for complex expressions.
305     if (Iteration++ >= MaxValues) {
306       LLVM_DEBUG(dbgs() << "Generic value traversal reached iteration limit: "
307                         << Iteration << "!\n");
308       return false;
309     }
310 
311     // Explicitly look through calls with a "returned" attribute if we do
312     // not have a pointer as stripPointerCasts only works on them.
313     Value *NewV = nullptr;
314     if (V->getType()->isPointerTy()) {
315       NewV = V->stripPointerCasts();
316     } else {
317       auto *CB = dyn_cast<CallBase>(V);
318       if (CB && CB->getCalledFunction()) {
319         for (Argument &Arg : CB->getCalledFunction()->args())
320           if (Arg.hasReturnedAttr()) {
321             NewV = CB->getArgOperand(Arg.getArgNo());
322             break;
323           }
324       }
325     }
326     if (NewV && NewV != V) {
327       Worklist.push_back({NewV, CtxI});
328       continue;
329     }
330 
331     // Look through select instructions, visit assumed potential values.
332     if (auto *SI = dyn_cast<SelectInst>(V)) {
333       Optional<Constant *> C = A.getAssumedConstant(
334           *SI->getCondition(), QueryingAA, UsedAssumedInformation);
335       bool NoValueYet = !C.hasValue();
336       if (NoValueYet || isa_and_nonnull<UndefValue>(*C))
337         continue;
338       if (auto *CI = dyn_cast_or_null<ConstantInt>(*C)) {
339         if (CI->isZero())
340           Worklist.push_back({SI->getFalseValue(), CtxI});
341         else
342           Worklist.push_back({SI->getTrueValue(), CtxI});
343         continue;
344       }
345       // We could not simplify the condition, assume both values.(
346       Worklist.push_back({SI->getTrueValue(), CtxI});
347       Worklist.push_back({SI->getFalseValue(), CtxI});
348       continue;
349     }
350 
351     // Look through phi nodes, visit all live operands.
352     if (auto *PHI = dyn_cast<PHINode>(V)) {
353       LivenessInfo &LI = GetLivenessInfo(*PHI->getFunction());
354       for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
355         BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
356         if (LI.LivenessAA->isEdgeDead(IncomingBB, PHI->getParent())) {
357           LI.AnyDead = true;
358           UsedAssumedInformation |= !LI.LivenessAA->isAtFixpoint();
359           continue;
360         }
361         Worklist.push_back(
362             {PHI->getIncomingValue(u), IncomingBB->getTerminator()});
363       }
364       continue;
365     }
366 
367     if (auto *Arg = dyn_cast<Argument>(V)) {
368       if (!Intraprocedural && !Arg->hasPassPointeeByValueCopyAttr()) {
369         SmallVector<Item> CallSiteValues;
370         bool UsedAssumedInformation = false;
371         if (A.checkForAllCallSites(
372                 [&](AbstractCallSite ACS) {
373                   // Callbacks might not have a corresponding call site operand,
374                   // stick with the argument in that case.
375                   Value *CSOp = ACS.getCallArgOperand(*Arg);
376                   if (!CSOp)
377                     return false;
378                   CallSiteValues.push_back({CSOp, ACS.getInstruction()});
379                   return true;
380                 },
381                 *Arg->getParent(), true, &QueryingAA, UsedAssumedInformation)) {
382           Worklist.append(CallSiteValues);
383           continue;
384         }
385       }
386     }
387 
388     if (UseValueSimplify && !isa<Constant>(V)) {
389       Optional<Value *> SimpleV =
390           A.getAssumedSimplified(*V, QueryingAA, UsedAssumedInformation);
391       if (!SimpleV.hasValue())
392         continue;
393       Value *NewV = SimpleV.getValue();
394       if (NewV && NewV != V) {
395         if (!Intraprocedural || !CtxI ||
396             AA::isValidInScope(*NewV, CtxI->getFunction())) {
397           Worklist.push_back({NewV, CtxI});
398           continue;
399         }
400       }
401     }
402 
403     if (auto *LI = dyn_cast<LoadInst>(V)) {
404       bool UsedAssumedInformation = false;
405       SmallSetVector<Value *, 4> PotentialCopies;
406       if (AA::getPotentiallyLoadedValues(A, *LI, PotentialCopies, QueryingAA,
407                                          UsedAssumedInformation,
408                                          /* OnlyExact */ true)) {
409         // Values have to be dynamically unique or we loose the fact that a
410         // single llvm::Value might represent two runtime values (e.g., stack
411         // locations in different recursive calls).
412         bool DynamicallyUnique =
413             llvm::all_of(PotentialCopies, [&A, &QueryingAA](Value *PC) {
414               return AA::isDynamicallyUnique(A, QueryingAA, *PC);
415             });
416         if (DynamicallyUnique &&
417             (!Intraprocedural || !CtxI ||
418              llvm::all_of(PotentialCopies, [CtxI](Value *PC) {
419                return AA::isValidInScope(*PC, CtxI->getFunction());
420              }))) {
421           for (auto *PotentialCopy : PotentialCopies)
422             Worklist.push_back({PotentialCopy, CtxI});
423           continue;
424         }
425       }
426     }
427 
428     // Once a leaf is reached we inform the user through the callback.
429     if (!VisitValueCB(*V, CtxI, State, Iteration > 1)) {
430       LLVM_DEBUG(dbgs() << "Generic value traversal visit callback failed for: "
431                         << *V << "!\n");
432       return false;
433     }
434   } while (!Worklist.empty());
435 
436   // If we actually used liveness information so we have to record a dependence.
437   for (auto &It : LivenessAAs)
438     if (It.second.AnyDead)
439       A.recordDependence(*It.second.LivenessAA, QueryingAA,
440                          DepClassTy::OPTIONAL);
441 
442   // All values have been visited.
443   return true;
444 }
445 
446 bool AA::getAssumedUnderlyingObjects(Attributor &A, const Value &Ptr,
447                                      SmallVectorImpl<Value *> &Objects,
448                                      const AbstractAttribute &QueryingAA,
449                                      const Instruction *CtxI,
450                                      bool &UsedAssumedInformation,
451                                      bool Intraprocedural) {
452   auto StripCB = [&](Value *V) { return getUnderlyingObject(V); };
453   SmallPtrSet<Value *, 8> SeenObjects;
454   auto VisitValueCB = [&SeenObjects](Value &Val, const Instruction *,
455                                      SmallVectorImpl<Value *> &Objects,
456                                      bool) -> bool {
457     if (SeenObjects.insert(&Val).second)
458       Objects.push_back(&Val);
459     return true;
460   };
461   if (!genericValueTraversal<decltype(Objects)>(
462           A, IRPosition::value(Ptr), QueryingAA, Objects, VisitValueCB, CtxI,
463           UsedAssumedInformation, true, 32, StripCB, Intraprocedural))
464     return false;
465   return true;
466 }
467 
468 static const Value *
469 stripAndAccumulateOffsets(Attributor &A, const AbstractAttribute &QueryingAA,
470                           const Value *Val, const DataLayout &DL, APInt &Offset,
471                           bool GetMinOffset, bool AllowNonInbounds,
472                           bool UseAssumed = false) {
473 
474   auto AttributorAnalysis = [&](Value &V, APInt &ROffset) -> bool {
475     const IRPosition &Pos = IRPosition::value(V);
476     // Only track dependence if we are going to use the assumed info.
477     const AAValueConstantRange &ValueConstantRangeAA =
478         A.getAAFor<AAValueConstantRange>(QueryingAA, Pos,
479                                          UseAssumed ? DepClassTy::OPTIONAL
480                                                     : DepClassTy::NONE);
481     ConstantRange Range = UseAssumed ? ValueConstantRangeAA.getAssumed()
482                                      : ValueConstantRangeAA.getKnown();
483     if (Range.isFullSet())
484       return false;
485 
486     // We can only use the lower part of the range because the upper part can
487     // be higher than what the value can really be.
488     if (GetMinOffset)
489       ROffset = Range.getSignedMin();
490     else
491       ROffset = Range.getSignedMax();
492     return true;
493   };
494 
495   return Val->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds,
496                                                 /* AllowInvariant */ true,
497                                                 AttributorAnalysis);
498 }
499 
500 static const Value *
501 getMinimalBaseOfPointer(Attributor &A, const AbstractAttribute &QueryingAA,
502                         const Value *Ptr, int64_t &BytesOffset,
503                         const DataLayout &DL, bool AllowNonInbounds = false) {
504   APInt OffsetAPInt(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
505   const Value *Base =
506       stripAndAccumulateOffsets(A, QueryingAA, Ptr, DL, OffsetAPInt,
507                                 /* GetMinOffset */ true, AllowNonInbounds);
508 
509   BytesOffset = OffsetAPInt.getSExtValue();
510   return Base;
511 }
512 
513 /// Clamp the information known for all returned values of a function
514 /// (identified by \p QueryingAA) into \p S.
515 template <typename AAType, typename StateType = typename AAType::StateType>
516 static void clampReturnedValueStates(
517     Attributor &A, const AAType &QueryingAA, StateType &S,
518     const IRPosition::CallBaseContext *CBContext = nullptr) {
519   LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
520                     << QueryingAA << " into " << S << "\n");
521 
522   assert((QueryingAA.getIRPosition().getPositionKind() ==
523               IRPosition::IRP_RETURNED ||
524           QueryingAA.getIRPosition().getPositionKind() ==
525               IRPosition::IRP_CALL_SITE_RETURNED) &&
526          "Can only clamp returned value states for a function returned or call "
527          "site returned position!");
528 
529   // Use an optional state as there might not be any return values and we want
530   // to join (IntegerState::operator&) the state of all there are.
531   Optional<StateType> T;
532 
533   // Callback for each possibly returned value.
534   auto CheckReturnValue = [&](Value &RV) -> bool {
535     const IRPosition &RVPos = IRPosition::value(RV, CBContext);
536     const AAType &AA =
537         A.getAAFor<AAType>(QueryingAA, RVPos, DepClassTy::REQUIRED);
538     LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
539                       << " @ " << RVPos << "\n");
540     const StateType &AAS = AA.getState();
541     if (T.hasValue())
542       *T &= AAS;
543     else
544       T = AAS;
545     LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
546                       << "\n");
547     return T->isValidState();
548   };
549 
550   if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
551     S.indicatePessimisticFixpoint();
552   else if (T.hasValue())
553     S ^= *T;
554 }
555 
556 namespace {
557 /// Helper class for generic deduction: return value -> returned position.
558 template <typename AAType, typename BaseType,
559           typename StateType = typename BaseType::StateType,
560           bool PropagateCallBaseContext = false>
561 struct AAReturnedFromReturnedValues : public BaseType {
562   AAReturnedFromReturnedValues(const IRPosition &IRP, Attributor &A)
563       : BaseType(IRP, A) {}
564 
565   /// See AbstractAttribute::updateImpl(...).
566   ChangeStatus updateImpl(Attributor &A) override {
567     StateType S(StateType::getBestState(this->getState()));
568     clampReturnedValueStates<AAType, StateType>(
569         A, *this, S,
570         PropagateCallBaseContext ? this->getCallBaseContext() : nullptr);
571     // TODO: If we know we visited all returned values, thus no are assumed
572     // dead, we can take the known information from the state T.
573     return clampStateAndIndicateChange<StateType>(this->getState(), S);
574   }
575 };
576 
577 /// Clamp the information known at all call sites for a given argument
578 /// (identified by \p QueryingAA) into \p S.
579 template <typename AAType, typename StateType = typename AAType::StateType>
580 static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
581                                         StateType &S) {
582   LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
583                     << QueryingAA << " into " << S << "\n");
584 
585   assert(QueryingAA.getIRPosition().getPositionKind() ==
586              IRPosition::IRP_ARGUMENT &&
587          "Can only clamp call site argument states for an argument position!");
588 
589   // Use an optional state as there might not be any return values and we want
590   // to join (IntegerState::operator&) the state of all there are.
591   Optional<StateType> T;
592 
593   // The argument number which is also the call site argument number.
594   unsigned ArgNo = QueryingAA.getIRPosition().getCallSiteArgNo();
595 
596   auto CallSiteCheck = [&](AbstractCallSite ACS) {
597     const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
598     // Check if a coresponding argument was found or if it is on not associated
599     // (which can happen for callback calls).
600     if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
601       return false;
602 
603     const AAType &AA =
604         A.getAAFor<AAType>(QueryingAA, ACSArgPos, DepClassTy::REQUIRED);
605     LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()
606                       << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n");
607     const StateType &AAS = AA.getState();
608     if (T.hasValue())
609       *T &= AAS;
610     else
611       T = AAS;
612     LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
613                       << "\n");
614     return T->isValidState();
615   };
616 
617   bool UsedAssumedInformation = false;
618   if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
619                               UsedAssumedInformation))
620     S.indicatePessimisticFixpoint();
621   else if (T.hasValue())
622     S ^= *T;
623 }
624 
625 /// This function is the bridge between argument position and the call base
626 /// context.
627 template <typename AAType, typename BaseType,
628           typename StateType = typename AAType::StateType>
629 bool getArgumentStateFromCallBaseContext(Attributor &A,
630                                          BaseType &QueryingAttribute,
631                                          IRPosition &Pos, StateType &State) {
632   assert((Pos.getPositionKind() == IRPosition::IRP_ARGUMENT) &&
633          "Expected an 'argument' position !");
634   const CallBase *CBContext = Pos.getCallBaseContext();
635   if (!CBContext)
636     return false;
637 
638   int ArgNo = Pos.getCallSiteArgNo();
639   assert(ArgNo >= 0 && "Invalid Arg No!");
640 
641   const auto &AA = A.getAAFor<AAType>(
642       QueryingAttribute, IRPosition::callsite_argument(*CBContext, ArgNo),
643       DepClassTy::REQUIRED);
644   const StateType &CBArgumentState =
645       static_cast<const StateType &>(AA.getState());
646 
647   LLVM_DEBUG(dbgs() << "[Attributor] Briding Call site context to argument"
648                     << "Position:" << Pos << "CB Arg state:" << CBArgumentState
649                     << "\n");
650 
651   // NOTE: If we want to do call site grouping it should happen here.
652   State ^= CBArgumentState;
653   return true;
654 }
655 
656 /// Helper class for generic deduction: call site argument -> argument position.
657 template <typename AAType, typename BaseType,
658           typename StateType = typename AAType::StateType,
659           bool BridgeCallBaseContext = false>
660 struct AAArgumentFromCallSiteArguments : public BaseType {
661   AAArgumentFromCallSiteArguments(const IRPosition &IRP, Attributor &A)
662       : BaseType(IRP, A) {}
663 
664   /// See AbstractAttribute::updateImpl(...).
665   ChangeStatus updateImpl(Attributor &A) override {
666     StateType S = StateType::getBestState(this->getState());
667 
668     if (BridgeCallBaseContext) {
669       bool Success =
670           getArgumentStateFromCallBaseContext<AAType, BaseType, StateType>(
671               A, *this, this->getIRPosition(), S);
672       if (Success)
673         return clampStateAndIndicateChange<StateType>(this->getState(), S);
674     }
675     clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
676 
677     // TODO: If we know we visited all incoming values, thus no are assumed
678     // dead, we can take the known information from the state T.
679     return clampStateAndIndicateChange<StateType>(this->getState(), S);
680   }
681 };
682 
683 /// Helper class for generic replication: function returned -> cs returned.
684 template <typename AAType, typename BaseType,
685           typename StateType = typename BaseType::StateType,
686           bool IntroduceCallBaseContext = false>
687 struct AACallSiteReturnedFromReturned : public BaseType {
688   AACallSiteReturnedFromReturned(const IRPosition &IRP, Attributor &A)
689       : BaseType(IRP, A) {}
690 
691   /// See AbstractAttribute::updateImpl(...).
692   ChangeStatus updateImpl(Attributor &A) override {
693     assert(this->getIRPosition().getPositionKind() ==
694                IRPosition::IRP_CALL_SITE_RETURNED &&
695            "Can only wrap function returned positions for call site returned "
696            "positions!");
697     auto &S = this->getState();
698 
699     const Function *AssociatedFunction =
700         this->getIRPosition().getAssociatedFunction();
701     if (!AssociatedFunction)
702       return S.indicatePessimisticFixpoint();
703 
704     CallBase &CBContext = cast<CallBase>(this->getAnchorValue());
705     if (IntroduceCallBaseContext)
706       LLVM_DEBUG(dbgs() << "[Attributor] Introducing call base context:"
707                         << CBContext << "\n");
708 
709     IRPosition FnPos = IRPosition::returned(
710         *AssociatedFunction, IntroduceCallBaseContext ? &CBContext : nullptr);
711     const AAType &AA = A.getAAFor<AAType>(*this, FnPos, DepClassTy::REQUIRED);
712     return clampStateAndIndicateChange(S, AA.getState());
713   }
714 };
715 
716 /// Helper function to accumulate uses.
717 template <class AAType, typename StateType = typename AAType::StateType>
718 static void followUsesInContext(AAType &AA, Attributor &A,
719                                 MustBeExecutedContextExplorer &Explorer,
720                                 const Instruction *CtxI,
721                                 SetVector<const Use *> &Uses,
722                                 StateType &State) {
723   auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
724   for (unsigned u = 0; u < Uses.size(); ++u) {
725     const Use *U = Uses[u];
726     if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
727       bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
728       if (Found && AA.followUseInMBEC(A, U, UserI, State))
729         for (const Use &Us : UserI->uses())
730           Uses.insert(&Us);
731     }
732   }
733 }
734 
735 /// Use the must-be-executed-context around \p I to add information into \p S.
736 /// The AAType class is required to have `followUseInMBEC` method with the
737 /// following signature and behaviour:
738 ///
739 /// bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I)
740 /// U - Underlying use.
741 /// I - The user of the \p U.
742 /// Returns true if the value should be tracked transitively.
743 ///
744 template <class AAType, typename StateType = typename AAType::StateType>
745 static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S,
746                              Instruction &CtxI) {
747 
748   // Container for (transitive) uses of the associated value.
749   SetVector<const Use *> Uses;
750   for (const Use &U : AA.getIRPosition().getAssociatedValue().uses())
751     Uses.insert(&U);
752 
753   MustBeExecutedContextExplorer &Explorer =
754       A.getInfoCache().getMustBeExecutedContextExplorer();
755 
756   followUsesInContext<AAType>(AA, A, Explorer, &CtxI, Uses, S);
757 
758   if (S.isAtFixpoint())
759     return;
760 
761   SmallVector<const BranchInst *, 4> BrInsts;
762   auto Pred = [&](const Instruction *I) {
763     if (const BranchInst *Br = dyn_cast<BranchInst>(I))
764       if (Br->isConditional())
765         BrInsts.push_back(Br);
766     return true;
767   };
768 
769   // Here, accumulate conditional branch instructions in the context. We
770   // explore the child paths and collect the known states. The disjunction of
771   // those states can be merged to its own state. Let ParentState_i be a state
772   // to indicate the known information for an i-th branch instruction in the
773   // context. ChildStates are created for its successors respectively.
774   //
775   // ParentS_1 = ChildS_{1, 1} /\ ChildS_{1, 2} /\ ... /\ ChildS_{1, n_1}
776   // ParentS_2 = ChildS_{2, 1} /\ ChildS_{2, 2} /\ ... /\ ChildS_{2, n_2}
777   //      ...
778   // ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m}
779   //
780   // Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m
781   //
782   // FIXME: Currently, recursive branches are not handled. For example, we
783   // can't deduce that ptr must be dereferenced in below function.
784   //
785   // void f(int a, int c, int *ptr) {
786   //    if(a)
787   //      if (b) {
788   //        *ptr = 0;
789   //      } else {
790   //        *ptr = 1;
791   //      }
792   //    else {
793   //      if (b) {
794   //        *ptr = 0;
795   //      } else {
796   //        *ptr = 1;
797   //      }
798   //    }
799   // }
800 
801   Explorer.checkForAllContext(&CtxI, Pred);
802   for (const BranchInst *Br : BrInsts) {
803     StateType ParentState;
804 
805     // The known state of the parent state is a conjunction of children's
806     // known states so it is initialized with a best state.
807     ParentState.indicateOptimisticFixpoint();
808 
809     for (const BasicBlock *BB : Br->successors()) {
810       StateType ChildState;
811 
812       size_t BeforeSize = Uses.size();
813       followUsesInContext(AA, A, Explorer, &BB->front(), Uses, ChildState);
814 
815       // Erase uses which only appear in the child.
816       for (auto It = Uses.begin() + BeforeSize; It != Uses.end();)
817         It = Uses.erase(It);
818 
819       ParentState &= ChildState;
820     }
821 
822     // Use only known state.
823     S += ParentState;
824   }
825 }
826 } // namespace
827 
828 /// ------------------------ PointerInfo ---------------------------------------
829 
830 namespace llvm {
831 namespace AA {
832 namespace PointerInfo {
833 
834 struct State;
835 
836 } // namespace PointerInfo
837 } // namespace AA
838 
839 /// Helper for AA::PointerInfo::Acccess DenseMap/Set usage.
840 template <>
841 struct DenseMapInfo<AAPointerInfo::Access> : DenseMapInfo<Instruction *> {
842   using Access = AAPointerInfo::Access;
843   static inline Access getEmptyKey();
844   static inline Access getTombstoneKey();
845   static unsigned getHashValue(const Access &A);
846   static bool isEqual(const Access &LHS, const Access &RHS);
847 };
848 
849 /// Helper that allows OffsetAndSize as a key in a DenseMap.
850 template <>
851 struct DenseMapInfo<AAPointerInfo ::OffsetAndSize>
852     : DenseMapInfo<std::pair<int64_t, int64_t>> {};
853 
854 /// Helper for AA::PointerInfo::Acccess DenseMap/Set usage ignoring everythign
855 /// but the instruction
856 struct AccessAsInstructionInfo : DenseMapInfo<Instruction *> {
857   using Base = DenseMapInfo<Instruction *>;
858   using Access = AAPointerInfo::Access;
859   static inline Access getEmptyKey();
860   static inline Access getTombstoneKey();
861   static unsigned getHashValue(const Access &A);
862   static bool isEqual(const Access &LHS, const Access &RHS);
863 };
864 
865 } // namespace llvm
866 
867 /// Implementation of the DenseMapInfo.
868 ///
869 ///{
870 inline llvm::AccessAsInstructionInfo::Access
871 llvm::AccessAsInstructionInfo::getEmptyKey() {
872   return Access(Base::getEmptyKey(), nullptr, AAPointerInfo::AK_READ, nullptr);
873 }
874 inline llvm::AccessAsInstructionInfo::Access
875 llvm::AccessAsInstructionInfo::getTombstoneKey() {
876   return Access(Base::getTombstoneKey(), nullptr, AAPointerInfo::AK_READ,
877                 nullptr);
878 }
879 unsigned llvm::AccessAsInstructionInfo::getHashValue(
880     const llvm::AccessAsInstructionInfo::Access &A) {
881   return Base::getHashValue(A.getRemoteInst());
882 }
883 bool llvm::AccessAsInstructionInfo::isEqual(
884     const llvm::AccessAsInstructionInfo::Access &LHS,
885     const llvm::AccessAsInstructionInfo::Access &RHS) {
886   return LHS.getRemoteInst() == RHS.getRemoteInst();
887 }
888 inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
889 llvm::DenseMapInfo<AAPointerInfo::Access>::getEmptyKey() {
890   return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_READ,
891                                nullptr);
892 }
893 inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
894 llvm::DenseMapInfo<AAPointerInfo::Access>::getTombstoneKey() {
895   return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_WRITE,
896                                nullptr);
897 }
898 
899 unsigned llvm::DenseMapInfo<AAPointerInfo::Access>::getHashValue(
900     const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &A) {
901   return detail::combineHashValue(
902              DenseMapInfo<Instruction *>::getHashValue(A.getRemoteInst()),
903              (A.isWrittenValueYetUndetermined()
904                   ? ~0
905                   : DenseMapInfo<Value *>::getHashValue(A.getWrittenValue()))) +
906          A.getKind();
907 }
908 
909 bool llvm::DenseMapInfo<AAPointerInfo::Access>::isEqual(
910     const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &LHS,
911     const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &RHS) {
912   return LHS == RHS;
913 }
914 ///}
915 
916 /// A type to track pointer/struct usage and accesses for AAPointerInfo.
917 struct AA::PointerInfo::State : public AbstractState {
918 
919   /// Return the best possible representable state.
920   static State getBestState(const State &SIS) { return State(); }
921 
922   /// Return the worst possible representable state.
923   static State getWorstState(const State &SIS) {
924     State R;
925     R.indicatePessimisticFixpoint();
926     return R;
927   }
928 
929   State() = default;
930   State(const State &SIS) : AccessBins(SIS.AccessBins) {}
931   State(State &&SIS) : AccessBins(std::move(SIS.AccessBins)) {}
932 
933   const State &getAssumed() const { return *this; }
934 
935   /// See AbstractState::isValidState().
936   bool isValidState() const override { return BS.isValidState(); }
937 
938   /// See AbstractState::isAtFixpoint().
939   bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
940 
941   /// See AbstractState::indicateOptimisticFixpoint().
942   ChangeStatus indicateOptimisticFixpoint() override {
943     BS.indicateOptimisticFixpoint();
944     return ChangeStatus::UNCHANGED;
945   }
946 
947   /// See AbstractState::indicatePessimisticFixpoint().
948   ChangeStatus indicatePessimisticFixpoint() override {
949     BS.indicatePessimisticFixpoint();
950     return ChangeStatus::CHANGED;
951   }
952 
953   State &operator=(const State &R) {
954     if (this == &R)
955       return *this;
956     BS = R.BS;
957     AccessBins = R.AccessBins;
958     return *this;
959   }
960 
961   State &operator=(State &&R) {
962     if (this == &R)
963       return *this;
964     std::swap(BS, R.BS);
965     std::swap(AccessBins, R.AccessBins);
966     return *this;
967   }
968 
969   bool operator==(const State &R) const {
970     if (BS != R.BS)
971       return false;
972     if (AccessBins.size() != R.AccessBins.size())
973       return false;
974     auto It = begin(), RIt = R.begin(), E = end();
975     while (It != E) {
976       if (It->getFirst() != RIt->getFirst())
977         return false;
978       auto &Accs = It->getSecond();
979       auto &RAccs = RIt->getSecond();
980       if (Accs.size() != RAccs.size())
981         return false;
982       auto AccIt = Accs.begin(), RAccIt = RAccs.begin(), AccE = Accs.end();
983       while (AccIt != AccE) {
984         if (*AccIt != *RAccIt)
985           return false;
986         ++AccIt;
987         ++RAccIt;
988       }
989       ++It;
990       ++RIt;
991     }
992     return true;
993   }
994   bool operator!=(const State &R) const { return !(*this == R); }
995 
996   /// We store accesses in a set with the instruction as key.
997   using Accesses = DenseSet<AAPointerInfo::Access, AccessAsInstructionInfo>;
998 
999   /// We store all accesses in bins denoted by their offset and size.
1000   using AccessBinsTy = DenseMap<AAPointerInfo::OffsetAndSize, Accesses>;
1001 
1002   AccessBinsTy::const_iterator begin() const { return AccessBins.begin(); }
1003   AccessBinsTy::const_iterator end() const { return AccessBins.end(); }
1004 
1005 protected:
1006   /// The bins with all the accesses for the associated pointer.
1007   DenseMap<AAPointerInfo::OffsetAndSize, Accesses> AccessBins;
1008 
1009   /// Add a new access to the state at offset \p Offset and with size \p Size.
1010   /// The access is associated with \p I, writes \p Content (if anything), and
1011   /// is of kind \p Kind.
1012   /// \Returns CHANGED, if the state changed, UNCHANGED otherwise.
1013   ChangeStatus addAccess(int64_t Offset, int64_t Size, Instruction &I,
1014                          Optional<Value *> Content,
1015                          AAPointerInfo::AccessKind Kind, Type *Ty,
1016                          Instruction *RemoteI = nullptr,
1017                          Accesses *BinPtr = nullptr) {
1018     AAPointerInfo::OffsetAndSize Key{Offset, Size};
1019     Accesses &Bin = BinPtr ? *BinPtr : AccessBins[Key];
1020     AAPointerInfo::Access Acc(&I, RemoteI ? RemoteI : &I, Content, Kind, Ty);
1021     // Check if we have an access for this instruction in this bin, if not,
1022     // simply add it.
1023     auto It = Bin.find(Acc);
1024     if (It == Bin.end()) {
1025       Bin.insert(Acc);
1026       return ChangeStatus::CHANGED;
1027     }
1028     // If the existing access is the same as then new one, nothing changed.
1029     AAPointerInfo::Access Before = *It;
1030     // The new one will be combined with the existing one.
1031     *It &= Acc;
1032     return *It == Before ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED;
1033   }
1034 
1035   /// See AAPointerInfo::forallInterferingAccesses.
1036   bool forallInterferingAccesses(
1037       AAPointerInfo::OffsetAndSize OAS,
1038       function_ref<bool(const AAPointerInfo::Access &, bool)> CB) const {
1039     if (!isValidState())
1040       return false;
1041 
1042     for (auto &It : AccessBins) {
1043       AAPointerInfo::OffsetAndSize ItOAS = It.getFirst();
1044       if (!OAS.mayOverlap(ItOAS))
1045         continue;
1046       bool IsExact = OAS == ItOAS && !OAS.offsetOrSizeAreUnknown();
1047       for (auto &Access : It.getSecond())
1048         if (!CB(Access, IsExact))
1049           return false;
1050     }
1051     return true;
1052   }
1053 
1054   /// See AAPointerInfo::forallInterferingAccesses.
1055   bool forallInterferingAccesses(
1056       Instruction &I,
1057       function_ref<bool(const AAPointerInfo::Access &, bool)> CB) const {
1058     if (!isValidState())
1059       return false;
1060 
1061     // First find the offset and size of I.
1062     AAPointerInfo::OffsetAndSize OAS(-1, -1);
1063     for (auto &It : AccessBins) {
1064       for (auto &Access : It.getSecond()) {
1065         if (Access.getRemoteInst() == &I) {
1066           OAS = It.getFirst();
1067           break;
1068         }
1069       }
1070       if (OAS.getSize() != -1)
1071         break;
1072     }
1073     // No access for I was found, we are done.
1074     if (OAS.getSize() == -1)
1075       return true;
1076 
1077     // Now that we have an offset and size, find all overlapping ones and use
1078     // the callback on the accesses.
1079     return forallInterferingAccesses(OAS, CB);
1080   }
1081 
1082 private:
1083   /// State to track fixpoint and validity.
1084   BooleanState BS;
1085 };
1086 
1087 namespace {
1088 struct AAPointerInfoImpl
1089     : public StateWrapper<AA::PointerInfo::State, AAPointerInfo> {
1090   using BaseTy = StateWrapper<AA::PointerInfo::State, AAPointerInfo>;
1091   AAPointerInfoImpl(const IRPosition &IRP, Attributor &A) : BaseTy(IRP) {}
1092 
1093   /// See AbstractAttribute::initialize(...).
1094   void initialize(Attributor &A) override { AAPointerInfo::initialize(A); }
1095 
1096   /// See AbstractAttribute::getAsStr().
1097   const std::string getAsStr() const override {
1098     return std::string("PointerInfo ") +
1099            (isValidState() ? (std::string("#") +
1100                               std::to_string(AccessBins.size()) + " bins")
1101                            : "<invalid>");
1102   }
1103 
1104   /// See AbstractAttribute::manifest(...).
1105   ChangeStatus manifest(Attributor &A) override {
1106     return AAPointerInfo::manifest(A);
1107   }
1108 
1109   bool forallInterferingAccesses(
1110       OffsetAndSize OAS,
1111       function_ref<bool(const AAPointerInfo::Access &, bool)> CB)
1112       const override {
1113     return State::forallInterferingAccesses(OAS, CB);
1114   }
1115   bool forallInterferingAccesses(
1116       Attributor &A, const AbstractAttribute &QueryingAA, Instruction &I,
1117       function_ref<bool(const Access &, bool)> UserCB) const override {
1118     SmallPtrSet<const Access *, 8> DominatingWrites;
1119     SmallVector<std::pair<const Access *, bool>, 8> InterferingAccesses;
1120 
1121     Function &Scope = *I.getFunction();
1122     const auto &NoSyncAA = A.getAAFor<AANoSync>(
1123         QueryingAA, IRPosition::function(Scope), DepClassTy::OPTIONAL);
1124     const auto *ExecDomainAA = A.lookupAAFor<AAExecutionDomain>(
1125         IRPosition::function(Scope), &QueryingAA, DepClassTy::OPTIONAL);
1126     const bool NoSync = NoSyncAA.isAssumedNoSync();
1127 
1128     // Helper to determine if we need to consider threading, which we cannot
1129     // right now. However, if the function is (assumed) nosync or the thread
1130     // executing all instructions is the main thread only we can ignore
1131     // threading.
1132     auto CanIgnoreThreading = [&](const Instruction &I) -> bool {
1133       if (NoSync)
1134         return true;
1135       if (ExecDomainAA && ExecDomainAA->isExecutedByInitialThreadOnly(I))
1136         return true;
1137       return false;
1138     };
1139 
1140     // Helper to determine if the access is executed by the same thread as the
1141     // load, for now it is sufficient to avoid any potential threading effects
1142     // as we cannot deal with them anyway.
1143     auto IsSameThreadAsLoad = [&](const Access &Acc) -> bool {
1144       return CanIgnoreThreading(*Acc.getLocalInst());
1145     };
1146 
1147     // TODO: Use inter-procedural reachability and dominance.
1148     const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
1149         QueryingAA, IRPosition::function(Scope), DepClassTy::OPTIONAL);
1150 
1151     const bool FindInterferingWrites = I.mayReadFromMemory();
1152     const bool FindInterferingReads = I.mayWriteToMemory();
1153     const bool UseDominanceReasoning = FindInterferingWrites;
1154     const bool CanUseCFGResoning = CanIgnoreThreading(I);
1155     InformationCache &InfoCache = A.getInfoCache();
1156     const DominatorTree *DT =
1157         NoRecurseAA.isKnownNoRecurse() && UseDominanceReasoning
1158             ? InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
1159                   Scope)
1160             : nullptr;
1161 
1162     enum GPUAddressSpace : unsigned {
1163       Generic = 0,
1164       Global = 1,
1165       Shared = 3,
1166       Constant = 4,
1167       Local = 5,
1168     };
1169 
1170     // Helper to check if a value has "kernel lifetime", that is it will not
1171     // outlive a GPU kernel. This is true for shared, constant, and local
1172     // globals on AMD and NVIDIA GPUs.
1173     auto HasKernelLifetime = [&](Value *V, Module &M) {
1174       Triple T(M.getTargetTriple());
1175       if (!(T.isAMDGPU() || T.isNVPTX()))
1176         return false;
1177       switch (V->getType()->getPointerAddressSpace()) {
1178       case GPUAddressSpace::Shared:
1179       case GPUAddressSpace::Constant:
1180       case GPUAddressSpace::Local:
1181         return true;
1182       default:
1183         return false;
1184       };
1185     };
1186 
1187     // The IsLiveInCalleeCB will be used by the AA::isPotentiallyReachable query
1188     // to determine if we should look at reachability from the callee. For
1189     // certain pointers we know the lifetime and we do not have to step into the
1190     // callee to determine reachability as the pointer would be dead in the
1191     // callee. See the conditional initialization below.
1192     std::function<bool(const Function &)> IsLiveInCalleeCB;
1193 
1194     if (auto *AI = dyn_cast<AllocaInst>(&getAssociatedValue())) {
1195       // If the alloca containing function is not recursive the alloca
1196       // must be dead in the callee.
1197       const Function *AIFn = AI->getFunction();
1198       const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
1199           *this, IRPosition::function(*AIFn), DepClassTy::OPTIONAL);
1200       if (NoRecurseAA.isAssumedNoRecurse()) {
1201         IsLiveInCalleeCB = [AIFn](const Function &Fn) { return AIFn != &Fn; };
1202       }
1203     } else if (auto *GV = dyn_cast<GlobalValue>(&getAssociatedValue())) {
1204       // If the global has kernel lifetime we can stop if we reach a kernel
1205       // as it is "dead" in the (unknown) callees.
1206       if (HasKernelLifetime(GV, *GV->getParent()))
1207         IsLiveInCalleeCB = [](const Function &Fn) {
1208           return !Fn.hasFnAttribute("kernel");
1209         };
1210     }
1211 
1212     auto AccessCB = [&](const Access &Acc, bool Exact) {
1213       if ((!FindInterferingWrites || !Acc.isWrite()) &&
1214           (!FindInterferingReads || !Acc.isRead()))
1215         return true;
1216 
1217       // For now we only filter accesses based on CFG reasoning which does not
1218       // work yet if we have threading effects, or the access is complicated.
1219       if (CanUseCFGResoning) {
1220         if ((!Acc.isWrite() ||
1221              !AA::isPotentiallyReachable(A, *Acc.getLocalInst(), I, QueryingAA,
1222                                          IsLiveInCalleeCB)) &&
1223             (!Acc.isRead() ||
1224              !AA::isPotentiallyReachable(A, I, *Acc.getLocalInst(), QueryingAA,
1225                                          IsLiveInCalleeCB)))
1226           return true;
1227         if (DT && Exact && (Acc.getLocalInst()->getFunction() == &Scope) &&
1228             IsSameThreadAsLoad(Acc)) {
1229           if (DT->dominates(Acc.getLocalInst(), &I))
1230             DominatingWrites.insert(&Acc);
1231         }
1232       }
1233 
1234       InterferingAccesses.push_back({&Acc, Exact});
1235       return true;
1236     };
1237     if (!State::forallInterferingAccesses(I, AccessCB))
1238       return false;
1239 
1240     // If we cannot use CFG reasoning we only filter the non-write accesses
1241     // and are done here.
1242     if (!CanUseCFGResoning) {
1243       for (auto &It : InterferingAccesses)
1244         if (!UserCB(*It.first, It.second))
1245           return false;
1246       return true;
1247     }
1248 
1249     // Helper to determine if we can skip a specific write access. This is in
1250     // the worst case quadratic as we are looking for another write that will
1251     // hide the effect of this one.
1252     auto CanSkipAccess = [&](const Access &Acc, bool Exact) {
1253       if (!IsSameThreadAsLoad(Acc))
1254         return false;
1255       if (!DominatingWrites.count(&Acc))
1256         return false;
1257       for (const Access *DomAcc : DominatingWrites) {
1258         assert(Acc.getLocalInst()->getFunction() ==
1259                    DomAcc->getLocalInst()->getFunction() &&
1260                "Expected dominating writes to be in the same function!");
1261 
1262         if (DomAcc != &Acc &&
1263             DT->dominates(Acc.getLocalInst(), DomAcc->getLocalInst())) {
1264           return true;
1265         }
1266       }
1267       return false;
1268     };
1269 
1270     // Run the user callback on all accesses we cannot skip and return if that
1271     // succeeded for all or not.
1272     unsigned NumInterferingAccesses = InterferingAccesses.size();
1273     for (auto &It : InterferingAccesses) {
1274       if (!DT || NumInterferingAccesses > MaxInterferingAccesses ||
1275           !CanSkipAccess(*It.first, It.second)) {
1276         if (!UserCB(*It.first, It.second))
1277           return false;
1278       }
1279     }
1280     return true;
1281   }
1282 
1283   ChangeStatus translateAndAddCalleeState(Attributor &A,
1284                                           const AAPointerInfo &CalleeAA,
1285                                           int64_t CallArgOffset, CallBase &CB) {
1286     using namespace AA::PointerInfo;
1287     if (!CalleeAA.getState().isValidState() || !isValidState())
1288       return indicatePessimisticFixpoint();
1289 
1290     const auto &CalleeImplAA = static_cast<const AAPointerInfoImpl &>(CalleeAA);
1291     bool IsByval = CalleeImplAA.getAssociatedArgument()->hasByValAttr();
1292 
1293     // Combine the accesses bin by bin.
1294     ChangeStatus Changed = ChangeStatus::UNCHANGED;
1295     for (auto &It : CalleeImplAA.getState()) {
1296       OffsetAndSize OAS = OffsetAndSize::getUnknown();
1297       if (CallArgOffset != OffsetAndSize::Unknown)
1298         OAS = OffsetAndSize(It.first.getOffset() + CallArgOffset,
1299                             It.first.getSize());
1300       Accesses &Bin = AccessBins[OAS];
1301       for (const AAPointerInfo::Access &RAcc : It.second) {
1302         if (IsByval && !RAcc.isRead())
1303           continue;
1304         bool UsedAssumedInformation = false;
1305         Optional<Value *> Content = A.translateArgumentToCallSiteContent(
1306             RAcc.getContent(), CB, *this, UsedAssumedInformation);
1307         AccessKind AK =
1308             AccessKind(RAcc.getKind() & (IsByval ? AccessKind::AK_READ
1309                                                  : AccessKind::AK_READ_WRITE));
1310         Changed =
1311             Changed | addAccess(OAS.getOffset(), OAS.getSize(), CB, Content, AK,
1312                                 RAcc.getType(), RAcc.getRemoteInst(), &Bin);
1313       }
1314     }
1315     return Changed;
1316   }
1317 
1318   /// Statistic tracking for all AAPointerInfo implementations.
1319   /// See AbstractAttribute::trackStatistics().
1320   void trackPointerInfoStatistics(const IRPosition &IRP) const {}
1321 };
1322 
1323 struct AAPointerInfoFloating : public AAPointerInfoImpl {
1324   using AccessKind = AAPointerInfo::AccessKind;
1325   AAPointerInfoFloating(const IRPosition &IRP, Attributor &A)
1326       : AAPointerInfoImpl(IRP, A) {}
1327 
1328   /// See AbstractAttribute::initialize(...).
1329   void initialize(Attributor &A) override { AAPointerInfoImpl::initialize(A); }
1330 
1331   /// Deal with an access and signal if it was handled successfully.
1332   bool handleAccess(Attributor &A, Instruction &I, Value &Ptr,
1333                     Optional<Value *> Content, AccessKind Kind, int64_t Offset,
1334                     ChangeStatus &Changed, Type *Ty,
1335                     int64_t Size = OffsetAndSize::Unknown) {
1336     using namespace AA::PointerInfo;
1337     // No need to find a size if one is given or the offset is unknown.
1338     if (Offset != OffsetAndSize::Unknown && Size == OffsetAndSize::Unknown &&
1339         Ty) {
1340       const DataLayout &DL = A.getDataLayout();
1341       TypeSize AccessSize = DL.getTypeStoreSize(Ty);
1342       if (!AccessSize.isScalable())
1343         Size = AccessSize.getFixedSize();
1344     }
1345     Changed = Changed | addAccess(Offset, Size, I, Content, Kind, Ty);
1346     return true;
1347   };
1348 
1349   /// Helper struct, will support ranges eventually.
1350   struct OffsetInfo {
1351     int64_t Offset = OffsetAndSize::Unknown;
1352 
1353     bool operator==(const OffsetInfo &OI) const { return Offset == OI.Offset; }
1354   };
1355 
1356   /// See AbstractAttribute::updateImpl(...).
1357   ChangeStatus updateImpl(Attributor &A) override {
1358     using namespace AA::PointerInfo;
1359     State S = getState();
1360     ChangeStatus Changed = ChangeStatus::UNCHANGED;
1361     Value &AssociatedValue = getAssociatedValue();
1362 
1363     const DataLayout &DL = A.getDataLayout();
1364     DenseMap<Value *, OffsetInfo> OffsetInfoMap;
1365     OffsetInfoMap[&AssociatedValue] = OffsetInfo{0};
1366 
1367     auto HandlePassthroughUser = [&](Value *Usr, OffsetInfo &PtrOI,
1368                                      bool &Follow) {
1369       OffsetInfo &UsrOI = OffsetInfoMap[Usr];
1370       UsrOI = PtrOI;
1371       Follow = true;
1372       return true;
1373     };
1374 
1375     const auto *TLI = getAnchorScope()
1376                           ? A.getInfoCache().getTargetLibraryInfoForFunction(
1377                                 *getAnchorScope())
1378                           : nullptr;
1379     auto UsePred = [&](const Use &U, bool &Follow) -> bool {
1380       Value *CurPtr = U.get();
1381       User *Usr = U.getUser();
1382       LLVM_DEBUG(dbgs() << "[AAPointerInfo] Analyze " << *CurPtr << " in "
1383                         << *Usr << "\n");
1384       assert(OffsetInfoMap.count(CurPtr) &&
1385              "The current pointer offset should have been seeded!");
1386 
1387       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Usr)) {
1388         if (CE->isCast())
1389           return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow);
1390         if (CE->isCompare())
1391           return true;
1392         if (!isa<GEPOperator>(CE)) {
1393           LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled constant user " << *CE
1394                             << "\n");
1395           return false;
1396         }
1397       }
1398       if (auto *GEP = dyn_cast<GEPOperator>(Usr)) {
1399         // Note the order here, the Usr access might change the map, CurPtr is
1400         // already in it though.
1401         OffsetInfo &UsrOI = OffsetInfoMap[Usr];
1402         OffsetInfo &PtrOI = OffsetInfoMap[CurPtr];
1403         UsrOI = PtrOI;
1404 
1405         // TODO: Use range information.
1406         if (PtrOI.Offset == OffsetAndSize::Unknown ||
1407             !GEP->hasAllConstantIndices()) {
1408           UsrOI.Offset = OffsetAndSize::Unknown;
1409           Follow = true;
1410           return true;
1411         }
1412 
1413         SmallVector<Value *, 8> Indices;
1414         for (Use &Idx : GEP->indices()) {
1415           if (auto *CIdx = dyn_cast<ConstantInt>(Idx)) {
1416             Indices.push_back(CIdx);
1417             continue;
1418           }
1419 
1420           LLVM_DEBUG(dbgs() << "[AAPointerInfo] Non constant GEP index " << *GEP
1421                             << " : " << *Idx << "\n");
1422           return false;
1423         }
1424         UsrOI.Offset = PtrOI.Offset + DL.getIndexedOffsetInType(
1425                                           GEP->getSourceElementType(), Indices);
1426         Follow = true;
1427         return true;
1428       }
1429       if (isa<CastInst>(Usr) || isa<SelectInst>(Usr))
1430         return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow);
1431 
1432       // For PHIs we need to take care of the recurrence explicitly as the value
1433       // might change while we iterate through a loop. For now, we give up if
1434       // the PHI is not invariant.
1435       if (isa<PHINode>(Usr)) {
1436         // Note the order here, the Usr access might change the map, CurPtr is
1437         // already in it though.
1438         OffsetInfo &UsrOI = OffsetInfoMap[Usr];
1439         OffsetInfo &PtrOI = OffsetInfoMap[CurPtr];
1440         // Check if the PHI is invariant (so far).
1441         if (UsrOI == PtrOI)
1442           return true;
1443 
1444         // Check if the PHI operand has already an unknown offset as we can't
1445         // improve on that anymore.
1446         if (PtrOI.Offset == OffsetAndSize::Unknown) {
1447           UsrOI = PtrOI;
1448           Follow = true;
1449           return true;
1450         }
1451 
1452         // Check if the PHI operand is not dependent on the PHI itself.
1453         // TODO: This is not great as we look at the pointer type. However, it
1454         // is unclear where the Offset size comes from with typeless pointers.
1455         APInt Offset(
1456             DL.getIndexSizeInBits(CurPtr->getType()->getPointerAddressSpace()),
1457             0);
1458         if (&AssociatedValue == CurPtr->stripAndAccumulateConstantOffsets(
1459                                     DL, Offset, /* AllowNonInbounds */ true)) {
1460           if (Offset != PtrOI.Offset) {
1461             LLVM_DEBUG(dbgs()
1462                        << "[AAPointerInfo] PHI operand pointer offset mismatch "
1463                        << *CurPtr << " in " << *Usr << "\n");
1464             return false;
1465           }
1466           return HandlePassthroughUser(Usr, PtrOI, Follow);
1467         }
1468 
1469         // TODO: Approximate in case we know the direction of the recurrence.
1470         LLVM_DEBUG(dbgs() << "[AAPointerInfo] PHI operand is too complex "
1471                           << *CurPtr << " in " << *Usr << "\n");
1472         UsrOI = PtrOI;
1473         UsrOI.Offset = OffsetAndSize::Unknown;
1474         Follow = true;
1475         return true;
1476       }
1477 
1478       if (auto *LoadI = dyn_cast<LoadInst>(Usr))
1479         return handleAccess(A, *LoadI, *CurPtr, /* Content */ nullptr,
1480                             AccessKind::AK_READ, OffsetInfoMap[CurPtr].Offset,
1481                             Changed, LoadI->getType());
1482       if (auto *StoreI = dyn_cast<StoreInst>(Usr)) {
1483         if (StoreI->getValueOperand() == CurPtr) {
1484           LLVM_DEBUG(dbgs() << "[AAPointerInfo] Escaping use in store "
1485                             << *StoreI << "\n");
1486           return false;
1487         }
1488         bool UsedAssumedInformation = false;
1489         Optional<Value *> Content = A.getAssumedSimplified(
1490             *StoreI->getValueOperand(), *this, UsedAssumedInformation);
1491         return handleAccess(A, *StoreI, *CurPtr, Content, AccessKind::AK_WRITE,
1492                             OffsetInfoMap[CurPtr].Offset, Changed,
1493                             StoreI->getValueOperand()->getType());
1494       }
1495       if (auto *CB = dyn_cast<CallBase>(Usr)) {
1496         if (CB->isLifetimeStartOrEnd())
1497           return true;
1498         if (TLI && isFreeCall(CB, TLI))
1499           return true;
1500         if (CB->isArgOperand(&U)) {
1501           unsigned ArgNo = CB->getArgOperandNo(&U);
1502           const auto &CSArgPI = A.getAAFor<AAPointerInfo>(
1503               *this, IRPosition::callsite_argument(*CB, ArgNo),
1504               DepClassTy::REQUIRED);
1505           Changed = translateAndAddCalleeState(
1506                         A, CSArgPI, OffsetInfoMap[CurPtr].Offset, *CB) |
1507                     Changed;
1508           return true;
1509         }
1510         LLVM_DEBUG(dbgs() << "[AAPointerInfo] Call user not handled " << *CB
1511                           << "\n");
1512         // TODO: Allow some call uses
1513         return false;
1514       }
1515 
1516       LLVM_DEBUG(dbgs() << "[AAPointerInfo] User not handled " << *Usr << "\n");
1517       return false;
1518     };
1519     auto EquivalentUseCB = [&](const Use &OldU, const Use &NewU) {
1520       if (OffsetInfoMap.count(NewU))
1521         return OffsetInfoMap[NewU] == OffsetInfoMap[OldU];
1522       OffsetInfoMap[NewU] = OffsetInfoMap[OldU];
1523       return true;
1524     };
1525     if (!A.checkForAllUses(UsePred, *this, AssociatedValue,
1526                            /* CheckBBLivenessOnly */ true, DepClassTy::OPTIONAL,
1527                            EquivalentUseCB))
1528       return indicatePessimisticFixpoint();
1529 
1530     LLVM_DEBUG({
1531       dbgs() << "Accesses by bin after update:\n";
1532       for (auto &It : AccessBins) {
1533         dbgs() << "[" << It.first.getOffset() << "-"
1534                << It.first.getOffset() + It.first.getSize()
1535                << "] : " << It.getSecond().size() << "\n";
1536         for (auto &Acc : It.getSecond()) {
1537           dbgs() << "     - " << Acc.getKind() << " - " << *Acc.getLocalInst()
1538                  << "\n";
1539           if (Acc.getLocalInst() != Acc.getRemoteInst())
1540             dbgs() << "     -->                         "
1541                    << *Acc.getRemoteInst() << "\n";
1542           if (!Acc.isWrittenValueYetUndetermined())
1543             dbgs() << "     - " << Acc.getWrittenValue() << "\n";
1544         }
1545       }
1546     });
1547 
1548     return Changed;
1549   }
1550 
1551   /// See AbstractAttribute::trackStatistics()
1552   void trackStatistics() const override {
1553     AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1554   }
1555 };
1556 
1557 struct AAPointerInfoReturned final : AAPointerInfoImpl {
1558   AAPointerInfoReturned(const IRPosition &IRP, Attributor &A)
1559       : AAPointerInfoImpl(IRP, A) {}
1560 
1561   /// See AbstractAttribute::updateImpl(...).
1562   ChangeStatus updateImpl(Attributor &A) override {
1563     return indicatePessimisticFixpoint();
1564   }
1565 
1566   /// See AbstractAttribute::trackStatistics()
1567   void trackStatistics() const override {
1568     AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1569   }
1570 };
1571 
1572 struct AAPointerInfoArgument final : AAPointerInfoFloating {
1573   AAPointerInfoArgument(const IRPosition &IRP, Attributor &A)
1574       : AAPointerInfoFloating(IRP, A) {}
1575 
1576   /// See AbstractAttribute::initialize(...).
1577   void initialize(Attributor &A) override {
1578     AAPointerInfoFloating::initialize(A);
1579     if (getAnchorScope()->isDeclaration())
1580       indicatePessimisticFixpoint();
1581   }
1582 
1583   /// See AbstractAttribute::trackStatistics()
1584   void trackStatistics() const override {
1585     AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1586   }
1587 };
1588 
1589 struct AAPointerInfoCallSiteArgument final : AAPointerInfoFloating {
1590   AAPointerInfoCallSiteArgument(const IRPosition &IRP, Attributor &A)
1591       : AAPointerInfoFloating(IRP, A) {}
1592 
1593   /// See AbstractAttribute::updateImpl(...).
1594   ChangeStatus updateImpl(Attributor &A) override {
1595     using namespace AA::PointerInfo;
1596     // We handle memory intrinsics explicitly, at least the first (=
1597     // destination) and second (=source) arguments as we know how they are
1598     // accessed.
1599     if (auto *MI = dyn_cast_or_null<MemIntrinsic>(getCtxI())) {
1600       ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength());
1601       int64_t LengthVal = OffsetAndSize::Unknown;
1602       if (Length)
1603         LengthVal = Length->getSExtValue();
1604       Value &Ptr = getAssociatedValue();
1605       unsigned ArgNo = getIRPosition().getCallSiteArgNo();
1606       ChangeStatus Changed = ChangeStatus::UNCHANGED;
1607       if (ArgNo == 0) {
1608         handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_WRITE, 0, Changed,
1609                      nullptr, LengthVal);
1610       } else if (ArgNo == 1) {
1611         handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_READ, 0, Changed,
1612                      nullptr, LengthVal);
1613       } else {
1614         LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled memory intrinsic "
1615                           << *MI << "\n");
1616         return indicatePessimisticFixpoint();
1617       }
1618       return Changed;
1619     }
1620 
1621     // TODO: Once we have call site specific value information we can provide
1622     //       call site specific liveness information and then it makes
1623     //       sense to specialize attributes for call sites arguments instead of
1624     //       redirecting requests to the callee argument.
1625     Argument *Arg = getAssociatedArgument();
1626     if (!Arg)
1627       return indicatePessimisticFixpoint();
1628     const IRPosition &ArgPos = IRPosition::argument(*Arg);
1629     auto &ArgAA =
1630         A.getAAFor<AAPointerInfo>(*this, ArgPos, DepClassTy::REQUIRED);
1631     return translateAndAddCalleeState(A, ArgAA, 0, *cast<CallBase>(getCtxI()));
1632   }
1633 
1634   /// See AbstractAttribute::trackStatistics()
1635   void trackStatistics() const override {
1636     AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1637   }
1638 };
1639 
1640 struct AAPointerInfoCallSiteReturned final : AAPointerInfoFloating {
1641   AAPointerInfoCallSiteReturned(const IRPosition &IRP, Attributor &A)
1642       : AAPointerInfoFloating(IRP, A) {}
1643 
1644   /// See AbstractAttribute::trackStatistics()
1645   void trackStatistics() const override {
1646     AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1647   }
1648 };
1649 } // namespace
1650 
1651 /// -----------------------NoUnwind Function Attribute--------------------------
1652 
1653 namespace {
1654 struct AANoUnwindImpl : AANoUnwind {
1655   AANoUnwindImpl(const IRPosition &IRP, Attributor &A) : AANoUnwind(IRP, A) {}
1656 
1657   const std::string getAsStr() const override {
1658     return getAssumed() ? "nounwind" : "may-unwind";
1659   }
1660 
1661   /// See AbstractAttribute::updateImpl(...).
1662   ChangeStatus updateImpl(Attributor &A) override {
1663     auto Opcodes = {
1664         (unsigned)Instruction::Invoke,      (unsigned)Instruction::CallBr,
1665         (unsigned)Instruction::Call,        (unsigned)Instruction::CleanupRet,
1666         (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
1667 
1668     auto CheckForNoUnwind = [&](Instruction &I) {
1669       if (!I.mayThrow())
1670         return true;
1671 
1672       if (const auto *CB = dyn_cast<CallBase>(&I)) {
1673         const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
1674             *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
1675         return NoUnwindAA.isAssumedNoUnwind();
1676       }
1677       return false;
1678     };
1679 
1680     bool UsedAssumedInformation = false;
1681     if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes,
1682                                    UsedAssumedInformation))
1683       return indicatePessimisticFixpoint();
1684 
1685     return ChangeStatus::UNCHANGED;
1686   }
1687 };
1688 
1689 struct AANoUnwindFunction final : public AANoUnwindImpl {
1690   AANoUnwindFunction(const IRPosition &IRP, Attributor &A)
1691       : AANoUnwindImpl(IRP, A) {}
1692 
1693   /// See AbstractAttribute::trackStatistics()
1694   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
1695 };
1696 
1697 /// NoUnwind attribute deduction for a call sites.
1698 struct AANoUnwindCallSite final : AANoUnwindImpl {
1699   AANoUnwindCallSite(const IRPosition &IRP, Attributor &A)
1700       : AANoUnwindImpl(IRP, A) {}
1701 
1702   /// See AbstractAttribute::initialize(...).
1703   void initialize(Attributor &A) override {
1704     AANoUnwindImpl::initialize(A);
1705     Function *F = getAssociatedFunction();
1706     if (!F || F->isDeclaration())
1707       indicatePessimisticFixpoint();
1708   }
1709 
1710   /// See AbstractAttribute::updateImpl(...).
1711   ChangeStatus updateImpl(Attributor &A) override {
1712     // TODO: Once we have call site specific value information we can provide
1713     //       call site specific liveness information and then it makes
1714     //       sense to specialize attributes for call sites arguments instead of
1715     //       redirecting requests to the callee argument.
1716     Function *F = getAssociatedFunction();
1717     const IRPosition &FnPos = IRPosition::function(*F);
1718     auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::REQUIRED);
1719     return clampStateAndIndicateChange(getState(), FnAA.getState());
1720   }
1721 
1722   /// See AbstractAttribute::trackStatistics()
1723   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
1724 };
1725 } // namespace
1726 
1727 /// --------------------- Function Return Values -------------------------------
1728 
1729 namespace {
1730 /// "Attribute" that collects all potential returned values and the return
1731 /// instructions that they arise from.
1732 ///
1733 /// If there is a unique returned value R, the manifest method will:
1734 ///   - mark R with the "returned" attribute, if R is an argument.
1735 class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
1736 
1737   /// Mapping of values potentially returned by the associated function to the
1738   /// return instructions that might return them.
1739   MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
1740 
1741   /// State flags
1742   ///
1743   ///{
1744   bool IsFixed = false;
1745   bool IsValidState = true;
1746   ///}
1747 
1748 public:
1749   AAReturnedValuesImpl(const IRPosition &IRP, Attributor &A)
1750       : AAReturnedValues(IRP, A) {}
1751 
1752   /// See AbstractAttribute::initialize(...).
1753   void initialize(Attributor &A) override {
1754     // Reset the state.
1755     IsFixed = false;
1756     IsValidState = true;
1757     ReturnedValues.clear();
1758 
1759     Function *F = getAssociatedFunction();
1760     if (!F || F->isDeclaration()) {
1761       indicatePessimisticFixpoint();
1762       return;
1763     }
1764     assert(!F->getReturnType()->isVoidTy() &&
1765            "Did not expect a void return type!");
1766 
1767     // The map from instruction opcodes to those instructions in the function.
1768     auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
1769 
1770     // Look through all arguments, if one is marked as returned we are done.
1771     for (Argument &Arg : F->args()) {
1772       if (Arg.hasReturnedAttr()) {
1773         auto &ReturnInstSet = ReturnedValues[&Arg];
1774         if (auto *Insts = OpcodeInstMap.lookup(Instruction::Ret))
1775           for (Instruction *RI : *Insts)
1776             ReturnInstSet.insert(cast<ReturnInst>(RI));
1777 
1778         indicateOptimisticFixpoint();
1779         return;
1780       }
1781     }
1782 
1783     if (!A.isFunctionIPOAmendable(*F))
1784       indicatePessimisticFixpoint();
1785   }
1786 
1787   /// See AbstractAttribute::manifest(...).
1788   ChangeStatus manifest(Attributor &A) override;
1789 
1790   /// See AbstractAttribute::getState(...).
1791   AbstractState &getState() override { return *this; }
1792 
1793   /// See AbstractAttribute::getState(...).
1794   const AbstractState &getState() const override { return *this; }
1795 
1796   /// See AbstractAttribute::updateImpl(Attributor &A).
1797   ChangeStatus updateImpl(Attributor &A) override;
1798 
1799   llvm::iterator_range<iterator> returned_values() override {
1800     return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1801   }
1802 
1803   llvm::iterator_range<const_iterator> returned_values() const override {
1804     return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1805   }
1806 
1807   /// Return the number of potential return values, -1 if unknown.
1808   size_t getNumReturnValues() const override {
1809     return isValidState() ? ReturnedValues.size() : -1;
1810   }
1811 
1812   /// Return an assumed unique return value if a single candidate is found. If
1813   /// there cannot be one, return a nullptr. If it is not clear yet, return the
1814   /// Optional::NoneType.
1815   Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
1816 
1817   /// See AbstractState::checkForAllReturnedValues(...).
1818   bool checkForAllReturnedValuesAndReturnInsts(
1819       function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
1820       const override;
1821 
1822   /// Pretty print the attribute similar to the IR representation.
1823   const std::string getAsStr() const override;
1824 
1825   /// See AbstractState::isAtFixpoint().
1826   bool isAtFixpoint() const override { return IsFixed; }
1827 
1828   /// See AbstractState::isValidState().
1829   bool isValidState() const override { return IsValidState; }
1830 
1831   /// See AbstractState::indicateOptimisticFixpoint(...).
1832   ChangeStatus indicateOptimisticFixpoint() override {
1833     IsFixed = true;
1834     return ChangeStatus::UNCHANGED;
1835   }
1836 
1837   ChangeStatus indicatePessimisticFixpoint() override {
1838     IsFixed = true;
1839     IsValidState = false;
1840     return ChangeStatus::CHANGED;
1841   }
1842 };
1843 
1844 ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
1845   ChangeStatus Changed = ChangeStatus::UNCHANGED;
1846 
1847   // Bookkeeping.
1848   assert(isValidState());
1849   STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
1850                   "Number of function with known return values");
1851 
1852   // Check if we have an assumed unique return value that we could manifest.
1853   Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
1854 
1855   if (!UniqueRV.hasValue() || !UniqueRV.getValue())
1856     return Changed;
1857 
1858   // Bookkeeping.
1859   STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
1860                   "Number of function with unique return");
1861   // If the assumed unique return value is an argument, annotate it.
1862   if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
1863     if (UniqueRVArg->getType()->canLosslesslyBitCastTo(
1864             getAssociatedFunction()->getReturnType())) {
1865       getIRPosition() = IRPosition::argument(*UniqueRVArg);
1866       Changed = IRAttribute::manifest(A);
1867     }
1868   }
1869   return Changed;
1870 }
1871 
1872 const std::string AAReturnedValuesImpl::getAsStr() const {
1873   return (isAtFixpoint() ? "returns(#" : "may-return(#") +
1874          (isValidState() ? std::to_string(getNumReturnValues()) : "?") + ")";
1875 }
1876 
1877 Optional<Value *>
1878 AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
1879   // If checkForAllReturnedValues provides a unique value, ignoring potential
1880   // undef values that can also be present, it is assumed to be the actual
1881   // return value and forwarded to the caller of this method. If there are
1882   // multiple, a nullptr is returned indicating there cannot be a unique
1883   // returned value.
1884   Optional<Value *> UniqueRV;
1885   Type *Ty = getAssociatedFunction()->getReturnType();
1886 
1887   auto Pred = [&](Value &RV) -> bool {
1888     UniqueRV = AA::combineOptionalValuesInAAValueLatice(UniqueRV, &RV, Ty);
1889     return UniqueRV != Optional<Value *>(nullptr);
1890   };
1891 
1892   if (!A.checkForAllReturnedValues(Pred, *this))
1893     UniqueRV = nullptr;
1894 
1895   return UniqueRV;
1896 }
1897 
1898 bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
1899     function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
1900     const {
1901   if (!isValidState())
1902     return false;
1903 
1904   // Check all returned values but ignore call sites as long as we have not
1905   // encountered an overdefined one during an update.
1906   for (auto &It : ReturnedValues) {
1907     Value *RV = It.first;
1908     if (!Pred(*RV, It.second))
1909       return false;
1910   }
1911 
1912   return true;
1913 }
1914 
1915 ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
1916   ChangeStatus Changed = ChangeStatus::UNCHANGED;
1917 
1918   auto ReturnValueCB = [&](Value &V, const Instruction *CtxI, ReturnInst &Ret,
1919                            bool) -> bool {
1920     assert(AA::isValidInScope(V, Ret.getFunction()) &&
1921            "Assumed returned value should be valid in function scope!");
1922     if (ReturnedValues[&V].insert(&Ret))
1923       Changed = ChangeStatus::CHANGED;
1924     return true;
1925   };
1926 
1927   bool UsedAssumedInformation = false;
1928   auto ReturnInstCB = [&](Instruction &I) {
1929     ReturnInst &Ret = cast<ReturnInst>(I);
1930     return genericValueTraversal<ReturnInst>(
1931         A, IRPosition::value(*Ret.getReturnValue()), *this, Ret, ReturnValueCB,
1932         &I, UsedAssumedInformation, /* UseValueSimplify */ true,
1933         /* MaxValues */ 16,
1934         /* StripCB */ nullptr, /* Intraprocedural */ true);
1935   };
1936 
1937   // Discover returned values from all live returned instructions in the
1938   // associated function.
1939   if (!A.checkForAllInstructions(ReturnInstCB, *this, {Instruction::Ret},
1940                                  UsedAssumedInformation))
1941     return indicatePessimisticFixpoint();
1942   return Changed;
1943 }
1944 
1945 struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1946   AAReturnedValuesFunction(const IRPosition &IRP, Attributor &A)
1947       : AAReturnedValuesImpl(IRP, A) {}
1948 
1949   /// See AbstractAttribute::trackStatistics()
1950   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
1951 };
1952 
1953 /// Returned values information for a call sites.
1954 struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1955   AAReturnedValuesCallSite(const IRPosition &IRP, Attributor &A)
1956       : AAReturnedValuesImpl(IRP, A) {}
1957 
1958   /// See AbstractAttribute::initialize(...).
1959   void initialize(Attributor &A) override {
1960     // TODO: Once we have call site specific value information we can provide
1961     //       call site specific liveness information and then it makes
1962     //       sense to specialize attributes for call sites instead of
1963     //       redirecting requests to the callee.
1964     llvm_unreachable("Abstract attributes for returned values are not "
1965                      "supported for call sites yet!");
1966   }
1967 
1968   /// See AbstractAttribute::updateImpl(...).
1969   ChangeStatus updateImpl(Attributor &A) override {
1970     return indicatePessimisticFixpoint();
1971   }
1972 
1973   /// See AbstractAttribute::trackStatistics()
1974   void trackStatistics() const override {}
1975 };
1976 } // namespace
1977 
1978 /// ------------------------ NoSync Function Attribute -------------------------
1979 
1980 bool AANoSync::isNonRelaxedAtomic(const Instruction *I) {
1981   if (!I->isAtomic())
1982     return false;
1983 
1984   if (auto *FI = dyn_cast<FenceInst>(I))
1985     // All legal orderings for fence are stronger than monotonic.
1986     return FI->getSyncScopeID() != SyncScope::SingleThread;
1987   if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I)) {
1988     // Unordered is not a legal ordering for cmpxchg.
1989     return (AI->getSuccessOrdering() != AtomicOrdering::Monotonic ||
1990             AI->getFailureOrdering() != AtomicOrdering::Monotonic);
1991   }
1992 
1993   AtomicOrdering Ordering;
1994   switch (I->getOpcode()) {
1995   case Instruction::AtomicRMW:
1996     Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1997     break;
1998   case Instruction::Store:
1999     Ordering = cast<StoreInst>(I)->getOrdering();
2000     break;
2001   case Instruction::Load:
2002     Ordering = cast<LoadInst>(I)->getOrdering();
2003     break;
2004   default:
2005     llvm_unreachable(
2006         "New atomic operations need to be known in the attributor.");
2007   }
2008 
2009   return (Ordering != AtomicOrdering::Unordered &&
2010           Ordering != AtomicOrdering::Monotonic);
2011 }
2012 
2013 /// Return true if this intrinsic is nosync.  This is only used for intrinsics
2014 /// which would be nosync except that they have a volatile flag.  All other
2015 /// intrinsics are simply annotated with the nosync attribute in Intrinsics.td.
2016 bool AANoSync::isNoSyncIntrinsic(const Instruction *I) {
2017   if (auto *MI = dyn_cast<MemIntrinsic>(I))
2018     return !MI->isVolatile();
2019   return false;
2020 }
2021 
2022 namespace {
2023 struct AANoSyncImpl : AANoSync {
2024   AANoSyncImpl(const IRPosition &IRP, Attributor &A) : AANoSync(IRP, A) {}
2025 
2026   const std::string getAsStr() const override {
2027     return getAssumed() ? "nosync" : "may-sync";
2028   }
2029 
2030   /// See AbstractAttribute::updateImpl(...).
2031   ChangeStatus updateImpl(Attributor &A) override;
2032 };
2033 
2034 ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
2035 
2036   auto CheckRWInstForNoSync = [&](Instruction &I) {
2037     return AA::isNoSyncInst(A, I, *this);
2038   };
2039 
2040   auto CheckForNoSync = [&](Instruction &I) {
2041     // At this point we handled all read/write effects and they are all
2042     // nosync, so they can be skipped.
2043     if (I.mayReadOrWriteMemory())
2044       return true;
2045 
2046     // non-convergent and readnone imply nosync.
2047     return !cast<CallBase>(I).isConvergent();
2048   };
2049 
2050   bool UsedAssumedInformation = false;
2051   if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this,
2052                                           UsedAssumedInformation) ||
2053       !A.checkForAllCallLikeInstructions(CheckForNoSync, *this,
2054                                          UsedAssumedInformation))
2055     return indicatePessimisticFixpoint();
2056 
2057   return ChangeStatus::UNCHANGED;
2058 }
2059 
2060 struct AANoSyncFunction final : public AANoSyncImpl {
2061   AANoSyncFunction(const IRPosition &IRP, Attributor &A)
2062       : AANoSyncImpl(IRP, A) {}
2063 
2064   /// See AbstractAttribute::trackStatistics()
2065   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
2066 };
2067 
2068 /// NoSync attribute deduction for a call sites.
2069 struct AANoSyncCallSite final : AANoSyncImpl {
2070   AANoSyncCallSite(const IRPosition &IRP, Attributor &A)
2071       : AANoSyncImpl(IRP, A) {}
2072 
2073   /// See AbstractAttribute::initialize(...).
2074   void initialize(Attributor &A) override {
2075     AANoSyncImpl::initialize(A);
2076     Function *F = getAssociatedFunction();
2077     if (!F || F->isDeclaration())
2078       indicatePessimisticFixpoint();
2079   }
2080 
2081   /// See AbstractAttribute::updateImpl(...).
2082   ChangeStatus updateImpl(Attributor &A) override {
2083     // TODO: Once we have call site specific value information we can provide
2084     //       call site specific liveness information and then it makes
2085     //       sense to specialize attributes for call sites arguments instead of
2086     //       redirecting requests to the callee argument.
2087     Function *F = getAssociatedFunction();
2088     const IRPosition &FnPos = IRPosition::function(*F);
2089     auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos, DepClassTy::REQUIRED);
2090     return clampStateAndIndicateChange(getState(), FnAA.getState());
2091   }
2092 
2093   /// See AbstractAttribute::trackStatistics()
2094   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
2095 };
2096 } // namespace
2097 
2098 /// ------------------------ No-Free Attributes ----------------------------
2099 
2100 namespace {
2101 struct AANoFreeImpl : public AANoFree {
2102   AANoFreeImpl(const IRPosition &IRP, Attributor &A) : AANoFree(IRP, A) {}
2103 
2104   /// See AbstractAttribute::updateImpl(...).
2105   ChangeStatus updateImpl(Attributor &A) override {
2106     auto CheckForNoFree = [&](Instruction &I) {
2107       const auto &CB = cast<CallBase>(I);
2108       if (CB.hasFnAttr(Attribute::NoFree))
2109         return true;
2110 
2111       const auto &NoFreeAA = A.getAAFor<AANoFree>(
2112           *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
2113       return NoFreeAA.isAssumedNoFree();
2114     };
2115 
2116     bool UsedAssumedInformation = false;
2117     if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this,
2118                                            UsedAssumedInformation))
2119       return indicatePessimisticFixpoint();
2120     return ChangeStatus::UNCHANGED;
2121   }
2122 
2123   /// See AbstractAttribute::getAsStr().
2124   const std::string getAsStr() const override {
2125     return getAssumed() ? "nofree" : "may-free";
2126   }
2127 };
2128 
2129 struct AANoFreeFunction final : public AANoFreeImpl {
2130   AANoFreeFunction(const IRPosition &IRP, Attributor &A)
2131       : AANoFreeImpl(IRP, A) {}
2132 
2133   /// See AbstractAttribute::trackStatistics()
2134   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
2135 };
2136 
2137 /// NoFree attribute deduction for a call sites.
2138 struct AANoFreeCallSite final : AANoFreeImpl {
2139   AANoFreeCallSite(const IRPosition &IRP, Attributor &A)
2140       : AANoFreeImpl(IRP, A) {}
2141 
2142   /// See AbstractAttribute::initialize(...).
2143   void initialize(Attributor &A) override {
2144     AANoFreeImpl::initialize(A);
2145     Function *F = getAssociatedFunction();
2146     if (!F || F->isDeclaration())
2147       indicatePessimisticFixpoint();
2148   }
2149 
2150   /// See AbstractAttribute::updateImpl(...).
2151   ChangeStatus updateImpl(Attributor &A) override {
2152     // TODO: Once we have call site specific value information we can provide
2153     //       call site specific liveness information and then it makes
2154     //       sense to specialize attributes for call sites arguments instead of
2155     //       redirecting requests to the callee argument.
2156     Function *F = getAssociatedFunction();
2157     const IRPosition &FnPos = IRPosition::function(*F);
2158     auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos, DepClassTy::REQUIRED);
2159     return clampStateAndIndicateChange(getState(), FnAA.getState());
2160   }
2161 
2162   /// See AbstractAttribute::trackStatistics()
2163   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
2164 };
2165 
2166 /// NoFree attribute for floating values.
2167 struct AANoFreeFloating : AANoFreeImpl {
2168   AANoFreeFloating(const IRPosition &IRP, Attributor &A)
2169       : AANoFreeImpl(IRP, A) {}
2170 
2171   /// See AbstractAttribute::trackStatistics()
2172   void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)}
2173 
2174   /// See Abstract Attribute::updateImpl(...).
2175   ChangeStatus updateImpl(Attributor &A) override {
2176     const IRPosition &IRP = getIRPosition();
2177 
2178     const auto &NoFreeAA = A.getAAFor<AANoFree>(
2179         *this, IRPosition::function_scope(IRP), DepClassTy::OPTIONAL);
2180     if (NoFreeAA.isAssumedNoFree())
2181       return ChangeStatus::UNCHANGED;
2182 
2183     Value &AssociatedValue = getIRPosition().getAssociatedValue();
2184     auto Pred = [&](const Use &U, bool &Follow) -> bool {
2185       Instruction *UserI = cast<Instruction>(U.getUser());
2186       if (auto *CB = dyn_cast<CallBase>(UserI)) {
2187         if (CB->isBundleOperand(&U))
2188           return false;
2189         if (!CB->isArgOperand(&U))
2190           return true;
2191         unsigned ArgNo = CB->getArgOperandNo(&U);
2192 
2193         const auto &NoFreeArg = A.getAAFor<AANoFree>(
2194             *this, IRPosition::callsite_argument(*CB, ArgNo),
2195             DepClassTy::REQUIRED);
2196         return NoFreeArg.isAssumedNoFree();
2197       }
2198 
2199       if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
2200           isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
2201         Follow = true;
2202         return true;
2203       }
2204       if (isa<StoreInst>(UserI) || isa<LoadInst>(UserI) ||
2205           isa<ReturnInst>(UserI))
2206         return true;
2207 
2208       // Unknown user.
2209       return false;
2210     };
2211     if (!A.checkForAllUses(Pred, *this, AssociatedValue))
2212       return indicatePessimisticFixpoint();
2213 
2214     return ChangeStatus::UNCHANGED;
2215   }
2216 };
2217 
2218 /// NoFree attribute for a call site argument.
2219 struct AANoFreeArgument final : AANoFreeFloating {
2220   AANoFreeArgument(const IRPosition &IRP, Attributor &A)
2221       : AANoFreeFloating(IRP, A) {}
2222 
2223   /// See AbstractAttribute::trackStatistics()
2224   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) }
2225 };
2226 
2227 /// NoFree attribute for call site arguments.
2228 struct AANoFreeCallSiteArgument final : AANoFreeFloating {
2229   AANoFreeCallSiteArgument(const IRPosition &IRP, Attributor &A)
2230       : AANoFreeFloating(IRP, A) {}
2231 
2232   /// See AbstractAttribute::updateImpl(...).
2233   ChangeStatus updateImpl(Attributor &A) override {
2234     // TODO: Once we have call site specific value information we can provide
2235     //       call site specific liveness information and then it makes
2236     //       sense to specialize attributes for call sites arguments instead of
2237     //       redirecting requests to the callee argument.
2238     Argument *Arg = getAssociatedArgument();
2239     if (!Arg)
2240       return indicatePessimisticFixpoint();
2241     const IRPosition &ArgPos = IRPosition::argument(*Arg);
2242     auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos, DepClassTy::REQUIRED);
2243     return clampStateAndIndicateChange(getState(), ArgAA.getState());
2244   }
2245 
2246   /// See AbstractAttribute::trackStatistics()
2247   void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)};
2248 };
2249 
2250 /// NoFree attribute for function return value.
2251 struct AANoFreeReturned final : AANoFreeFloating {
2252   AANoFreeReturned(const IRPosition &IRP, Attributor &A)
2253       : AANoFreeFloating(IRP, A) {
2254     llvm_unreachable("NoFree is not applicable to function returns!");
2255   }
2256 
2257   /// See AbstractAttribute::initialize(...).
2258   void initialize(Attributor &A) override {
2259     llvm_unreachable("NoFree is not applicable to function returns!");
2260   }
2261 
2262   /// See AbstractAttribute::updateImpl(...).
2263   ChangeStatus updateImpl(Attributor &A) override {
2264     llvm_unreachable("NoFree is not applicable to function returns!");
2265   }
2266 
2267   /// See AbstractAttribute::trackStatistics()
2268   void trackStatistics() const override {}
2269 };
2270 
2271 /// NoFree attribute deduction for a call site return value.
2272 struct AANoFreeCallSiteReturned final : AANoFreeFloating {
2273   AANoFreeCallSiteReturned(const IRPosition &IRP, Attributor &A)
2274       : AANoFreeFloating(IRP, A) {}
2275 
2276   ChangeStatus manifest(Attributor &A) override {
2277     return ChangeStatus::UNCHANGED;
2278   }
2279   /// See AbstractAttribute::trackStatistics()
2280   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) }
2281 };
2282 } // namespace
2283 
2284 /// ------------------------ NonNull Argument Attribute ------------------------
2285 namespace {
2286 static int64_t getKnownNonNullAndDerefBytesForUse(
2287     Attributor &A, const AbstractAttribute &QueryingAA, Value &AssociatedValue,
2288     const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
2289   TrackUse = false;
2290 
2291   const Value *UseV = U->get();
2292   if (!UseV->getType()->isPointerTy())
2293     return 0;
2294 
2295   // We need to follow common pointer manipulation uses to the accesses they
2296   // feed into. We can try to be smart to avoid looking through things we do not
2297   // like for now, e.g., non-inbounds GEPs.
2298   if (isa<CastInst>(I)) {
2299     TrackUse = true;
2300     return 0;
2301   }
2302 
2303   if (isa<GetElementPtrInst>(I)) {
2304     TrackUse = true;
2305     return 0;
2306   }
2307 
2308   Type *PtrTy = UseV->getType();
2309   const Function *F = I->getFunction();
2310   bool NullPointerIsDefined =
2311       F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
2312   const DataLayout &DL = A.getInfoCache().getDL();
2313   if (const auto *CB = dyn_cast<CallBase>(I)) {
2314     if (CB->isBundleOperand(U)) {
2315       if (RetainedKnowledge RK = getKnowledgeFromUse(
2316               U, {Attribute::NonNull, Attribute::Dereferenceable})) {
2317         IsNonNull |=
2318             (RK.AttrKind == Attribute::NonNull || !NullPointerIsDefined);
2319         return RK.ArgValue;
2320       }
2321       return 0;
2322     }
2323 
2324     if (CB->isCallee(U)) {
2325       IsNonNull |= !NullPointerIsDefined;
2326       return 0;
2327     }
2328 
2329     unsigned ArgNo = CB->getArgOperandNo(U);
2330     IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
2331     // As long as we only use known information there is no need to track
2332     // dependences here.
2333     auto &DerefAA =
2334         A.getAAFor<AADereferenceable>(QueryingAA, IRP, DepClassTy::NONE);
2335     IsNonNull |= DerefAA.isKnownNonNull();
2336     return DerefAA.getKnownDereferenceableBytes();
2337   }
2338 
2339   Optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I);
2340   if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile())
2341     return 0;
2342 
2343   int64_t Offset;
2344   const Value *Base =
2345       getMinimalBaseOfPointer(A, QueryingAA, Loc->Ptr, Offset, DL);
2346   if (Base && Base == &AssociatedValue) {
2347     int64_t DerefBytes = Loc->Size.getValue() + Offset;
2348     IsNonNull |= !NullPointerIsDefined;
2349     return std::max(int64_t(0), DerefBytes);
2350   }
2351 
2352   /// Corner case when an offset is 0.
2353   Base = GetPointerBaseWithConstantOffset(Loc->Ptr, Offset, DL,
2354                                           /*AllowNonInbounds*/ true);
2355   if (Base && Base == &AssociatedValue && Offset == 0) {
2356     int64_t DerefBytes = Loc->Size.getValue();
2357     IsNonNull |= !NullPointerIsDefined;
2358     return std::max(int64_t(0), DerefBytes);
2359   }
2360 
2361   return 0;
2362 }
2363 
2364 struct AANonNullImpl : AANonNull {
2365   AANonNullImpl(const IRPosition &IRP, Attributor &A)
2366       : AANonNull(IRP, A),
2367         NullIsDefined(NullPointerIsDefined(
2368             getAnchorScope(),
2369             getAssociatedValue().getType()->getPointerAddressSpace())) {}
2370 
2371   /// See AbstractAttribute::initialize(...).
2372   void initialize(Attributor &A) override {
2373     Value &V = getAssociatedValue();
2374     if (!NullIsDefined &&
2375         hasAttr({Attribute::NonNull, Attribute::Dereferenceable},
2376                 /* IgnoreSubsumingPositions */ false, &A)) {
2377       indicateOptimisticFixpoint();
2378       return;
2379     }
2380 
2381     if (isa<ConstantPointerNull>(V)) {
2382       indicatePessimisticFixpoint();
2383       return;
2384     }
2385 
2386     AANonNull::initialize(A);
2387 
2388     bool CanBeNull, CanBeFreed;
2389     if (V.getPointerDereferenceableBytes(A.getDataLayout(), CanBeNull,
2390                                          CanBeFreed)) {
2391       if (!CanBeNull) {
2392         indicateOptimisticFixpoint();
2393         return;
2394       }
2395     }
2396 
2397     if (isa<GlobalValue>(&getAssociatedValue())) {
2398       indicatePessimisticFixpoint();
2399       return;
2400     }
2401 
2402     if (Instruction *CtxI = getCtxI())
2403       followUsesInMBEC(*this, A, getState(), *CtxI);
2404   }
2405 
2406   /// See followUsesInMBEC
2407   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
2408                        AANonNull::StateType &State) {
2409     bool IsNonNull = false;
2410     bool TrackUse = false;
2411     getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
2412                                        IsNonNull, TrackUse);
2413     State.setKnown(IsNonNull);
2414     return TrackUse;
2415   }
2416 
2417   /// See AbstractAttribute::getAsStr().
2418   const std::string getAsStr() const override {
2419     return getAssumed() ? "nonnull" : "may-null";
2420   }
2421 
2422   /// Flag to determine if the underlying value can be null and still allow
2423   /// valid accesses.
2424   const bool NullIsDefined;
2425 };
2426 
2427 /// NonNull attribute for a floating value.
2428 struct AANonNullFloating : public AANonNullImpl {
2429   AANonNullFloating(const IRPosition &IRP, Attributor &A)
2430       : AANonNullImpl(IRP, A) {}
2431 
2432   /// See AbstractAttribute::updateImpl(...).
2433   ChangeStatus updateImpl(Attributor &A) override {
2434     const DataLayout &DL = A.getDataLayout();
2435 
2436     DominatorTree *DT = nullptr;
2437     AssumptionCache *AC = nullptr;
2438     InformationCache &InfoCache = A.getInfoCache();
2439     if (const Function *Fn = getAnchorScope()) {
2440       DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
2441       AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*Fn);
2442     }
2443 
2444     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
2445                             AANonNull::StateType &T, bool Stripped) -> bool {
2446       const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V),
2447                                              DepClassTy::REQUIRED);
2448       if (!Stripped && this == &AA) {
2449         if (!isKnownNonZero(&V, DL, 0, AC, CtxI, DT))
2450           T.indicatePessimisticFixpoint();
2451       } else {
2452         // Use abstract attribute information.
2453         const AANonNull::StateType &NS = AA.getState();
2454         T ^= NS;
2455       }
2456       return T.isValidState();
2457     };
2458 
2459     StateType T;
2460     bool UsedAssumedInformation = false;
2461     if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
2462                                           VisitValueCB, getCtxI(),
2463                                           UsedAssumedInformation))
2464       return indicatePessimisticFixpoint();
2465 
2466     return clampStateAndIndicateChange(getState(), T);
2467   }
2468 
2469   /// See AbstractAttribute::trackStatistics()
2470   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
2471 };
2472 
2473 /// NonNull attribute for function return value.
2474 struct AANonNullReturned final
2475     : AAReturnedFromReturnedValues<AANonNull, AANonNull> {
2476   AANonNullReturned(const IRPosition &IRP, Attributor &A)
2477       : AAReturnedFromReturnedValues<AANonNull, AANonNull>(IRP, A) {}
2478 
2479   /// See AbstractAttribute::getAsStr().
2480   const std::string getAsStr() const override {
2481     return getAssumed() ? "nonnull" : "may-null";
2482   }
2483 
2484   /// See AbstractAttribute::trackStatistics()
2485   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
2486 };
2487 
2488 /// NonNull attribute for function argument.
2489 struct AANonNullArgument final
2490     : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> {
2491   AANonNullArgument(const IRPosition &IRP, Attributor &A)
2492       : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP, A) {}
2493 
2494   /// See AbstractAttribute::trackStatistics()
2495   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
2496 };
2497 
2498 struct AANonNullCallSiteArgument final : AANonNullFloating {
2499   AANonNullCallSiteArgument(const IRPosition &IRP, Attributor &A)
2500       : AANonNullFloating(IRP, A) {}
2501 
2502   /// See AbstractAttribute::trackStatistics()
2503   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
2504 };
2505 
2506 /// NonNull attribute for a call site return position.
2507 struct AANonNullCallSiteReturned final
2508     : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> {
2509   AANonNullCallSiteReturned(const IRPosition &IRP, Attributor &A)
2510       : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP, A) {}
2511 
2512   /// See AbstractAttribute::trackStatistics()
2513   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
2514 };
2515 } // namespace
2516 
2517 /// ------------------------ No-Recurse Attributes ----------------------------
2518 
2519 namespace {
2520 struct AANoRecurseImpl : public AANoRecurse {
2521   AANoRecurseImpl(const IRPosition &IRP, Attributor &A) : AANoRecurse(IRP, A) {}
2522 
2523   /// See AbstractAttribute::getAsStr()
2524   const std::string getAsStr() const override {
2525     return getAssumed() ? "norecurse" : "may-recurse";
2526   }
2527 };
2528 
2529 struct AANoRecurseFunction final : AANoRecurseImpl {
2530   AANoRecurseFunction(const IRPosition &IRP, Attributor &A)
2531       : AANoRecurseImpl(IRP, A) {}
2532 
2533   /// See AbstractAttribute::updateImpl(...).
2534   ChangeStatus updateImpl(Attributor &A) override {
2535 
2536     // If all live call sites are known to be no-recurse, we are as well.
2537     auto CallSitePred = [&](AbstractCallSite ACS) {
2538       const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
2539           *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
2540           DepClassTy::NONE);
2541       return NoRecurseAA.isKnownNoRecurse();
2542     };
2543     bool UsedAssumedInformation = false;
2544     if (A.checkForAllCallSites(CallSitePred, *this, true,
2545                                UsedAssumedInformation)) {
2546       // If we know all call sites and all are known no-recurse, we are done.
2547       // If all known call sites, which might not be all that exist, are known
2548       // to be no-recurse, we are not done but we can continue to assume
2549       // no-recurse. If one of the call sites we have not visited will become
2550       // live, another update is triggered.
2551       if (!UsedAssumedInformation)
2552         indicateOptimisticFixpoint();
2553       return ChangeStatus::UNCHANGED;
2554     }
2555 
2556     const AAFunctionReachability &EdgeReachability =
2557         A.getAAFor<AAFunctionReachability>(*this, getIRPosition(),
2558                                            DepClassTy::REQUIRED);
2559     if (EdgeReachability.canReach(A, *getAnchorScope()))
2560       return indicatePessimisticFixpoint();
2561     return ChangeStatus::UNCHANGED;
2562   }
2563 
2564   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
2565 };
2566 
2567 /// NoRecurse attribute deduction for a call sites.
2568 struct AANoRecurseCallSite final : AANoRecurseImpl {
2569   AANoRecurseCallSite(const IRPosition &IRP, Attributor &A)
2570       : AANoRecurseImpl(IRP, A) {}
2571 
2572   /// See AbstractAttribute::initialize(...).
2573   void initialize(Attributor &A) override {
2574     AANoRecurseImpl::initialize(A);
2575     Function *F = getAssociatedFunction();
2576     if (!F || F->isDeclaration())
2577       indicatePessimisticFixpoint();
2578   }
2579 
2580   /// See AbstractAttribute::updateImpl(...).
2581   ChangeStatus updateImpl(Attributor &A) override {
2582     // TODO: Once we have call site specific value information we can provide
2583     //       call site specific liveness information and then it makes
2584     //       sense to specialize attributes for call sites arguments instead of
2585     //       redirecting requests to the callee argument.
2586     Function *F = getAssociatedFunction();
2587     const IRPosition &FnPos = IRPosition::function(*F);
2588     auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos, DepClassTy::REQUIRED);
2589     return clampStateAndIndicateChange(getState(), FnAA.getState());
2590   }
2591 
2592   /// See AbstractAttribute::trackStatistics()
2593   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
2594 };
2595 } // namespace
2596 
2597 /// -------------------- Undefined-Behavior Attributes ------------------------
2598 
2599 namespace {
2600 struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
2601   AAUndefinedBehaviorImpl(const IRPosition &IRP, Attributor &A)
2602       : AAUndefinedBehavior(IRP, A) {}
2603 
2604   /// See AbstractAttribute::updateImpl(...).
2605   // through a pointer (i.e. also branches etc.)
2606   ChangeStatus updateImpl(Attributor &A) override {
2607     const size_t UBPrevSize = KnownUBInsts.size();
2608     const size_t NoUBPrevSize = AssumedNoUBInsts.size();
2609 
2610     auto InspectMemAccessInstForUB = [&](Instruction &I) {
2611       // Lang ref now states volatile store is not UB, let's skip them.
2612       if (I.isVolatile() && I.mayWriteToMemory())
2613         return true;
2614 
2615       // Skip instructions that are already saved.
2616       if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2617         return true;
2618 
2619       // If we reach here, we know we have an instruction
2620       // that accesses memory through a pointer operand,
2621       // for which getPointerOperand() should give it to us.
2622       Value *PtrOp =
2623           const_cast<Value *>(getPointerOperand(&I, /* AllowVolatile */ true));
2624       assert(PtrOp &&
2625              "Expected pointer operand of memory accessing instruction");
2626 
2627       // Either we stopped and the appropriate action was taken,
2628       // or we got back a simplified value to continue.
2629       Optional<Value *> SimplifiedPtrOp = stopOnUndefOrAssumed(A, PtrOp, &I);
2630       if (!SimplifiedPtrOp.hasValue() || !SimplifiedPtrOp.getValue())
2631         return true;
2632       const Value *PtrOpVal = SimplifiedPtrOp.getValue();
2633 
2634       // A memory access through a pointer is considered UB
2635       // only if the pointer has constant null value.
2636       // TODO: Expand it to not only check constant values.
2637       if (!isa<ConstantPointerNull>(PtrOpVal)) {
2638         AssumedNoUBInsts.insert(&I);
2639         return true;
2640       }
2641       const Type *PtrTy = PtrOpVal->getType();
2642 
2643       // Because we only consider instructions inside functions,
2644       // assume that a parent function exists.
2645       const Function *F = I.getFunction();
2646 
2647       // A memory access using constant null pointer is only considered UB
2648       // if null pointer is _not_ defined for the target platform.
2649       if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
2650         AssumedNoUBInsts.insert(&I);
2651       else
2652         KnownUBInsts.insert(&I);
2653       return true;
2654     };
2655 
2656     auto InspectBrInstForUB = [&](Instruction &I) {
2657       // A conditional branch instruction is considered UB if it has `undef`
2658       // condition.
2659 
2660       // Skip instructions that are already saved.
2661       if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2662         return true;
2663 
2664       // We know we have a branch instruction.
2665       auto *BrInst = cast<BranchInst>(&I);
2666 
2667       // Unconditional branches are never considered UB.
2668       if (BrInst->isUnconditional())
2669         return true;
2670 
2671       // Either we stopped and the appropriate action was taken,
2672       // or we got back a simplified value to continue.
2673       Optional<Value *> SimplifiedCond =
2674           stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
2675       if (!SimplifiedCond.hasValue() || !SimplifiedCond.getValue())
2676         return true;
2677       AssumedNoUBInsts.insert(&I);
2678       return true;
2679     };
2680 
2681     auto InspectCallSiteForUB = [&](Instruction &I) {
2682       // Check whether a callsite always cause UB or not
2683 
2684       // Skip instructions that are already saved.
2685       if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2686         return true;
2687 
2688       // Check nonnull and noundef argument attribute violation for each
2689       // callsite.
2690       CallBase &CB = cast<CallBase>(I);
2691       Function *Callee = CB.getCalledFunction();
2692       if (!Callee)
2693         return true;
2694       for (unsigned idx = 0; idx < CB.arg_size(); idx++) {
2695         // If current argument is known to be simplified to null pointer and the
2696         // corresponding argument position is known to have nonnull attribute,
2697         // the argument is poison. Furthermore, if the argument is poison and
2698         // the position is known to have noundef attriubte, this callsite is
2699         // considered UB.
2700         if (idx >= Callee->arg_size())
2701           break;
2702         Value *ArgVal = CB.getArgOperand(idx);
2703         if (!ArgVal)
2704           continue;
2705         // Here, we handle three cases.
2706         //   (1) Not having a value means it is dead. (we can replace the value
2707         //       with undef)
2708         //   (2) Simplified to undef. The argument violate noundef attriubte.
2709         //   (3) Simplified to null pointer where known to be nonnull.
2710         //       The argument is a poison value and violate noundef attribute.
2711         IRPosition CalleeArgumentIRP = IRPosition::callsite_argument(CB, idx);
2712         auto &NoUndefAA =
2713             A.getAAFor<AANoUndef>(*this, CalleeArgumentIRP, DepClassTy::NONE);
2714         if (!NoUndefAA.isKnownNoUndef())
2715           continue;
2716         bool UsedAssumedInformation = false;
2717         Optional<Value *> SimplifiedVal = A.getAssumedSimplified(
2718             IRPosition::value(*ArgVal), *this, UsedAssumedInformation);
2719         if (UsedAssumedInformation)
2720           continue;
2721         if (SimplifiedVal.hasValue() && !SimplifiedVal.getValue())
2722           return true;
2723         if (!SimplifiedVal.hasValue() ||
2724             isa<UndefValue>(*SimplifiedVal.getValue())) {
2725           KnownUBInsts.insert(&I);
2726           continue;
2727         }
2728         if (!ArgVal->getType()->isPointerTy() ||
2729             !isa<ConstantPointerNull>(*SimplifiedVal.getValue()))
2730           continue;
2731         auto &NonNullAA =
2732             A.getAAFor<AANonNull>(*this, CalleeArgumentIRP, DepClassTy::NONE);
2733         if (NonNullAA.isKnownNonNull())
2734           KnownUBInsts.insert(&I);
2735       }
2736       return true;
2737     };
2738 
2739     auto InspectReturnInstForUB = [&](Instruction &I) {
2740       auto &RI = cast<ReturnInst>(I);
2741       // Either we stopped and the appropriate action was taken,
2742       // or we got back a simplified return value to continue.
2743       Optional<Value *> SimplifiedRetValue =
2744           stopOnUndefOrAssumed(A, RI.getReturnValue(), &I);
2745       if (!SimplifiedRetValue.hasValue() || !SimplifiedRetValue.getValue())
2746         return true;
2747 
2748       // Check if a return instruction always cause UB or not
2749       // Note: It is guaranteed that the returned position of the anchor
2750       //       scope has noundef attribute when this is called.
2751       //       We also ensure the return position is not "assumed dead"
2752       //       because the returned value was then potentially simplified to
2753       //       `undef` in AAReturnedValues without removing the `noundef`
2754       //       attribute yet.
2755 
2756       // When the returned position has noundef attriubte, UB occurs in the
2757       // following cases.
2758       //   (1) Returned value is known to be undef.
2759       //   (2) The value is known to be a null pointer and the returned
2760       //       position has nonnull attribute (because the returned value is
2761       //       poison).
2762       if (isa<ConstantPointerNull>(*SimplifiedRetValue)) {
2763         auto &NonNullAA = A.getAAFor<AANonNull>(
2764             *this, IRPosition::returned(*getAnchorScope()), DepClassTy::NONE);
2765         if (NonNullAA.isKnownNonNull())
2766           KnownUBInsts.insert(&I);
2767       }
2768 
2769       return true;
2770     };
2771 
2772     bool UsedAssumedInformation = false;
2773     A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
2774                               {Instruction::Load, Instruction::Store,
2775                                Instruction::AtomicCmpXchg,
2776                                Instruction::AtomicRMW},
2777                               UsedAssumedInformation,
2778                               /* CheckBBLivenessOnly */ true);
2779     A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
2780                               UsedAssumedInformation,
2781                               /* CheckBBLivenessOnly */ true);
2782     A.checkForAllCallLikeInstructions(InspectCallSiteForUB, *this,
2783                                       UsedAssumedInformation);
2784 
2785     // If the returned position of the anchor scope has noundef attriubte, check
2786     // all returned instructions.
2787     if (!getAnchorScope()->getReturnType()->isVoidTy()) {
2788       const IRPosition &ReturnIRP = IRPosition::returned(*getAnchorScope());
2789       if (!A.isAssumedDead(ReturnIRP, this, nullptr, UsedAssumedInformation)) {
2790         auto &RetPosNoUndefAA =
2791             A.getAAFor<AANoUndef>(*this, ReturnIRP, DepClassTy::NONE);
2792         if (RetPosNoUndefAA.isKnownNoUndef())
2793           A.checkForAllInstructions(InspectReturnInstForUB, *this,
2794                                     {Instruction::Ret}, UsedAssumedInformation,
2795                                     /* CheckBBLivenessOnly */ true);
2796       }
2797     }
2798 
2799     if (NoUBPrevSize != AssumedNoUBInsts.size() ||
2800         UBPrevSize != KnownUBInsts.size())
2801       return ChangeStatus::CHANGED;
2802     return ChangeStatus::UNCHANGED;
2803   }
2804 
2805   bool isKnownToCauseUB(Instruction *I) const override {
2806     return KnownUBInsts.count(I);
2807   }
2808 
2809   bool isAssumedToCauseUB(Instruction *I) const override {
2810     // In simple words, if an instruction is not in the assumed to _not_
2811     // cause UB, then it is assumed UB (that includes those
2812     // in the KnownUBInsts set). The rest is boilerplate
2813     // is to ensure that it is one of the instructions we test
2814     // for UB.
2815 
2816     switch (I->getOpcode()) {
2817     case Instruction::Load:
2818     case Instruction::Store:
2819     case Instruction::AtomicCmpXchg:
2820     case Instruction::AtomicRMW:
2821       return !AssumedNoUBInsts.count(I);
2822     case Instruction::Br: {
2823       auto *BrInst = cast<BranchInst>(I);
2824       if (BrInst->isUnconditional())
2825         return false;
2826       return !AssumedNoUBInsts.count(I);
2827     } break;
2828     default:
2829       return false;
2830     }
2831     return false;
2832   }
2833 
2834   ChangeStatus manifest(Attributor &A) override {
2835     if (KnownUBInsts.empty())
2836       return ChangeStatus::UNCHANGED;
2837     for (Instruction *I : KnownUBInsts)
2838       A.changeToUnreachableAfterManifest(I);
2839     return ChangeStatus::CHANGED;
2840   }
2841 
2842   /// See AbstractAttribute::getAsStr()
2843   const std::string getAsStr() const override {
2844     return getAssumed() ? "undefined-behavior" : "no-ub";
2845   }
2846 
2847   /// Note: The correctness of this analysis depends on the fact that the
2848   /// following 2 sets will stop changing after some point.
2849   /// "Change" here means that their size changes.
2850   /// The size of each set is monotonically increasing
2851   /// (we only add items to them) and it is upper bounded by the number of
2852   /// instructions in the processed function (we can never save more
2853   /// elements in either set than this number). Hence, at some point,
2854   /// they will stop increasing.
2855   /// Consequently, at some point, both sets will have stopped
2856   /// changing, effectively making the analysis reach a fixpoint.
2857 
2858   /// Note: These 2 sets are disjoint and an instruction can be considered
2859   /// one of 3 things:
2860   /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
2861   ///    the KnownUBInsts set.
2862   /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
2863   ///    has a reason to assume it).
2864   /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
2865   ///    could not find a reason to assume or prove that it can cause UB,
2866   ///    hence it assumes it doesn't. We have a set for these instructions
2867   ///    so that we don't reprocess them in every update.
2868   ///    Note however that instructions in this set may cause UB.
2869 
2870 protected:
2871   /// A set of all live instructions _known_ to cause UB.
2872   SmallPtrSet<Instruction *, 8> KnownUBInsts;
2873 
2874 private:
2875   /// A set of all the (live) instructions that are assumed to _not_ cause UB.
2876   SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
2877 
2878   // Should be called on updates in which if we're processing an instruction
2879   // \p I that depends on a value \p V, one of the following has to happen:
2880   // - If the value is assumed, then stop.
2881   // - If the value is known but undef, then consider it UB.
2882   // - Otherwise, do specific processing with the simplified value.
2883   // We return None in the first 2 cases to signify that an appropriate
2884   // action was taken and the caller should stop.
2885   // Otherwise, we return the simplified value that the caller should
2886   // use for specific processing.
2887   Optional<Value *> stopOnUndefOrAssumed(Attributor &A, Value *V,
2888                                          Instruction *I) {
2889     bool UsedAssumedInformation = false;
2890     Optional<Value *> SimplifiedV = A.getAssumedSimplified(
2891         IRPosition::value(*V), *this, UsedAssumedInformation);
2892     if (!UsedAssumedInformation) {
2893       // Don't depend on assumed values.
2894       if (!SimplifiedV.hasValue()) {
2895         // If it is known (which we tested above) but it doesn't have a value,
2896         // then we can assume `undef` and hence the instruction is UB.
2897         KnownUBInsts.insert(I);
2898         return llvm::None;
2899       }
2900       if (!SimplifiedV.getValue())
2901         return nullptr;
2902       V = *SimplifiedV;
2903     }
2904     if (isa<UndefValue>(V)) {
2905       KnownUBInsts.insert(I);
2906       return llvm::None;
2907     }
2908     return V;
2909   }
2910 };
2911 
2912 struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
2913   AAUndefinedBehaviorFunction(const IRPosition &IRP, Attributor &A)
2914       : AAUndefinedBehaviorImpl(IRP, A) {}
2915 
2916   /// See AbstractAttribute::trackStatistics()
2917   void trackStatistics() const override {
2918     STATS_DECL(UndefinedBehaviorInstruction, Instruction,
2919                "Number of instructions known to have UB");
2920     BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) +=
2921         KnownUBInsts.size();
2922   }
2923 };
2924 } // namespace
2925 
2926 /// ------------------------ Will-Return Attributes ----------------------------
2927 
2928 namespace {
2929 // Helper function that checks whether a function has any cycle which we don't
2930 // know if it is bounded or not.
2931 // Loops with maximum trip count are considered bounded, any other cycle not.
2932 static bool mayContainUnboundedCycle(Function &F, Attributor &A) {
2933   ScalarEvolution *SE =
2934       A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F);
2935   LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F);
2936   // If either SCEV or LoopInfo is not available for the function then we assume
2937   // any cycle to be unbounded cycle.
2938   // We use scc_iterator which uses Tarjan algorithm to find all the maximal
2939   // SCCs.To detect if there's a cycle, we only need to find the maximal ones.
2940   if (!SE || !LI) {
2941     for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI)
2942       if (SCCI.hasCycle())
2943         return true;
2944     return false;
2945   }
2946 
2947   // If there's irreducible control, the function may contain non-loop cycles.
2948   if (mayContainIrreducibleControl(F, LI))
2949     return true;
2950 
2951   // Any loop that does not have a max trip count is considered unbounded cycle.
2952   for (auto *L : LI->getLoopsInPreorder()) {
2953     if (!SE->getSmallConstantMaxTripCount(L))
2954       return true;
2955   }
2956   return false;
2957 }
2958 
2959 struct AAWillReturnImpl : public AAWillReturn {
2960   AAWillReturnImpl(const IRPosition &IRP, Attributor &A)
2961       : AAWillReturn(IRP, A) {}
2962 
2963   /// See AbstractAttribute::initialize(...).
2964   void initialize(Attributor &A) override {
2965     AAWillReturn::initialize(A);
2966 
2967     if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ true)) {
2968       indicateOptimisticFixpoint();
2969       return;
2970     }
2971   }
2972 
2973   /// Check for `mustprogress` and `readonly` as they imply `willreturn`.
2974   bool isImpliedByMustprogressAndReadonly(Attributor &A, bool KnownOnly) {
2975     // Check for `mustprogress` in the scope and the associated function which
2976     // might be different if this is a call site.
2977     if ((!getAnchorScope() || !getAnchorScope()->mustProgress()) &&
2978         (!getAssociatedFunction() || !getAssociatedFunction()->mustProgress()))
2979       return false;
2980 
2981     bool IsKnown;
2982     if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
2983       return IsKnown || !KnownOnly;
2984     return false;
2985   }
2986 
2987   /// See AbstractAttribute::updateImpl(...).
2988   ChangeStatus updateImpl(Attributor &A) override {
2989     if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
2990       return ChangeStatus::UNCHANGED;
2991 
2992     auto CheckForWillReturn = [&](Instruction &I) {
2993       IRPosition IPos = IRPosition::callsite_function(cast<CallBase>(I));
2994       const auto &WillReturnAA =
2995           A.getAAFor<AAWillReturn>(*this, IPos, DepClassTy::REQUIRED);
2996       if (WillReturnAA.isKnownWillReturn())
2997         return true;
2998       if (!WillReturnAA.isAssumedWillReturn())
2999         return false;
3000       const auto &NoRecurseAA =
3001           A.getAAFor<AANoRecurse>(*this, IPos, DepClassTy::REQUIRED);
3002       return NoRecurseAA.isAssumedNoRecurse();
3003     };
3004 
3005     bool UsedAssumedInformation = false;
3006     if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this,
3007                                            UsedAssumedInformation))
3008       return indicatePessimisticFixpoint();
3009 
3010     return ChangeStatus::UNCHANGED;
3011   }
3012 
3013   /// See AbstractAttribute::getAsStr()
3014   const std::string getAsStr() const override {
3015     return getAssumed() ? "willreturn" : "may-noreturn";
3016   }
3017 };
3018 
3019 struct AAWillReturnFunction final : AAWillReturnImpl {
3020   AAWillReturnFunction(const IRPosition &IRP, Attributor &A)
3021       : AAWillReturnImpl(IRP, A) {}
3022 
3023   /// See AbstractAttribute::initialize(...).
3024   void initialize(Attributor &A) override {
3025     AAWillReturnImpl::initialize(A);
3026 
3027     Function *F = getAnchorScope();
3028     if (!F || F->isDeclaration() || mayContainUnboundedCycle(*F, A))
3029       indicatePessimisticFixpoint();
3030   }
3031 
3032   /// See AbstractAttribute::trackStatistics()
3033   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
3034 };
3035 
3036 /// WillReturn attribute deduction for a call sites.
3037 struct AAWillReturnCallSite final : AAWillReturnImpl {
3038   AAWillReturnCallSite(const IRPosition &IRP, Attributor &A)
3039       : AAWillReturnImpl(IRP, A) {}
3040 
3041   /// See AbstractAttribute::initialize(...).
3042   void initialize(Attributor &A) override {
3043     AAWillReturnImpl::initialize(A);
3044     Function *F = getAssociatedFunction();
3045     if (!F || !A.isFunctionIPOAmendable(*F))
3046       indicatePessimisticFixpoint();
3047   }
3048 
3049   /// See AbstractAttribute::updateImpl(...).
3050   ChangeStatus updateImpl(Attributor &A) override {
3051     if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
3052       return ChangeStatus::UNCHANGED;
3053 
3054     // TODO: Once we have call site specific value information we can provide
3055     //       call site specific liveness information and then it makes
3056     //       sense to specialize attributes for call sites arguments instead of
3057     //       redirecting requests to the callee argument.
3058     Function *F = getAssociatedFunction();
3059     const IRPosition &FnPos = IRPosition::function(*F);
3060     auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos, DepClassTy::REQUIRED);
3061     return clampStateAndIndicateChange(getState(), FnAA.getState());
3062   }
3063 
3064   /// See AbstractAttribute::trackStatistics()
3065   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
3066 };
3067 } // namespace
3068 
3069 /// -------------------AAReachability Attribute--------------------------
3070 
3071 namespace {
3072 struct AAReachabilityImpl : AAReachability {
3073   AAReachabilityImpl(const IRPosition &IRP, Attributor &A)
3074       : AAReachability(IRP, A) {}
3075 
3076   const std::string getAsStr() const override {
3077     // TODO: Return the number of reachable queries.
3078     return "reachable";
3079   }
3080 
3081   /// See AbstractAttribute::updateImpl(...).
3082   ChangeStatus updateImpl(Attributor &A) override {
3083     const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
3084         *this, IRPosition::function(*getAnchorScope()), DepClassTy::REQUIRED);
3085     if (!NoRecurseAA.isAssumedNoRecurse())
3086       return indicatePessimisticFixpoint();
3087     return ChangeStatus::UNCHANGED;
3088   }
3089 };
3090 
3091 struct AAReachabilityFunction final : public AAReachabilityImpl {
3092   AAReachabilityFunction(const IRPosition &IRP, Attributor &A)
3093       : AAReachabilityImpl(IRP, A) {}
3094 
3095   /// See AbstractAttribute::trackStatistics()
3096   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); }
3097 };
3098 } // namespace
3099 
3100 /// ------------------------ NoAlias Argument Attribute ------------------------
3101 
3102 namespace {
3103 struct AANoAliasImpl : AANoAlias {
3104   AANoAliasImpl(const IRPosition &IRP, Attributor &A) : AANoAlias(IRP, A) {
3105     assert(getAssociatedType()->isPointerTy() &&
3106            "Noalias is a pointer attribute");
3107   }
3108 
3109   const std::string getAsStr() const override {
3110     return getAssumed() ? "noalias" : "may-alias";
3111   }
3112 };
3113 
3114 /// NoAlias attribute for a floating value.
3115 struct AANoAliasFloating final : AANoAliasImpl {
3116   AANoAliasFloating(const IRPosition &IRP, Attributor &A)
3117       : AANoAliasImpl(IRP, A) {}
3118 
3119   /// See AbstractAttribute::initialize(...).
3120   void initialize(Attributor &A) override {
3121     AANoAliasImpl::initialize(A);
3122     Value *Val = &getAssociatedValue();
3123     do {
3124       CastInst *CI = dyn_cast<CastInst>(Val);
3125       if (!CI)
3126         break;
3127       Value *Base = CI->getOperand(0);
3128       if (!Base->hasOneUse())
3129         break;
3130       Val = Base;
3131     } while (true);
3132 
3133     if (!Val->getType()->isPointerTy()) {
3134       indicatePessimisticFixpoint();
3135       return;
3136     }
3137 
3138     if (isa<AllocaInst>(Val))
3139       indicateOptimisticFixpoint();
3140     else if (isa<ConstantPointerNull>(Val) &&
3141              !NullPointerIsDefined(getAnchorScope(),
3142                                    Val->getType()->getPointerAddressSpace()))
3143       indicateOptimisticFixpoint();
3144     else if (Val != &getAssociatedValue()) {
3145       const auto &ValNoAliasAA = A.getAAFor<AANoAlias>(
3146           *this, IRPosition::value(*Val), DepClassTy::OPTIONAL);
3147       if (ValNoAliasAA.isKnownNoAlias())
3148         indicateOptimisticFixpoint();
3149     }
3150   }
3151 
3152   /// See AbstractAttribute::updateImpl(...).
3153   ChangeStatus updateImpl(Attributor &A) override {
3154     // TODO: Implement this.
3155     return indicatePessimisticFixpoint();
3156   }
3157 
3158   /// See AbstractAttribute::trackStatistics()
3159   void trackStatistics() const override {
3160     STATS_DECLTRACK_FLOATING_ATTR(noalias)
3161   }
3162 };
3163 
3164 /// NoAlias attribute for an argument.
3165 struct AANoAliasArgument final
3166     : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
3167   using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
3168   AANoAliasArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
3169 
3170   /// See AbstractAttribute::initialize(...).
3171   void initialize(Attributor &A) override {
3172     Base::initialize(A);
3173     // See callsite argument attribute and callee argument attribute.
3174     if (hasAttr({Attribute::ByVal}))
3175       indicateOptimisticFixpoint();
3176   }
3177 
3178   /// See AbstractAttribute::update(...).
3179   ChangeStatus updateImpl(Attributor &A) override {
3180     // We have to make sure no-alias on the argument does not break
3181     // synchronization when this is a callback argument, see also [1] below.
3182     // If synchronization cannot be affected, we delegate to the base updateImpl
3183     // function, otherwise we give up for now.
3184 
3185     // If the function is no-sync, no-alias cannot break synchronization.
3186     const auto &NoSyncAA =
3187         A.getAAFor<AANoSync>(*this, IRPosition::function_scope(getIRPosition()),
3188                              DepClassTy::OPTIONAL);
3189     if (NoSyncAA.isAssumedNoSync())
3190       return Base::updateImpl(A);
3191 
3192     // If the argument is read-only, no-alias cannot break synchronization.
3193     bool IsKnown;
3194     if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
3195       return Base::updateImpl(A);
3196 
3197     // If the argument is never passed through callbacks, no-alias cannot break
3198     // synchronization.
3199     bool UsedAssumedInformation = false;
3200     if (A.checkForAllCallSites(
3201             [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
3202             true, UsedAssumedInformation))
3203       return Base::updateImpl(A);
3204 
3205     // TODO: add no-alias but make sure it doesn't break synchronization by
3206     // introducing fake uses. See:
3207     // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
3208     //     International Workshop on OpenMP 2018,
3209     //     http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
3210 
3211     return indicatePessimisticFixpoint();
3212   }
3213 
3214   /// See AbstractAttribute::trackStatistics()
3215   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
3216 };
3217 
3218 struct AANoAliasCallSiteArgument final : AANoAliasImpl {
3219   AANoAliasCallSiteArgument(const IRPosition &IRP, Attributor &A)
3220       : AANoAliasImpl(IRP, A) {}
3221 
3222   /// See AbstractAttribute::initialize(...).
3223   void initialize(Attributor &A) override {
3224     // See callsite argument attribute and callee argument attribute.
3225     const auto &CB = cast<CallBase>(getAnchorValue());
3226     if (CB.paramHasAttr(getCallSiteArgNo(), Attribute::NoAlias))
3227       indicateOptimisticFixpoint();
3228     Value &Val = getAssociatedValue();
3229     if (isa<ConstantPointerNull>(Val) &&
3230         !NullPointerIsDefined(getAnchorScope(),
3231                               Val.getType()->getPointerAddressSpace()))
3232       indicateOptimisticFixpoint();
3233   }
3234 
3235   /// Determine if the underlying value may alias with the call site argument
3236   /// \p OtherArgNo of \p ICS (= the underlying call site).
3237   bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
3238                             const AAMemoryBehavior &MemBehaviorAA,
3239                             const CallBase &CB, unsigned OtherArgNo) {
3240     // We do not need to worry about aliasing with the underlying IRP.
3241     if (this->getCalleeArgNo() == (int)OtherArgNo)
3242       return false;
3243 
3244     // If it is not a pointer or pointer vector we do not alias.
3245     const Value *ArgOp = CB.getArgOperand(OtherArgNo);
3246     if (!ArgOp->getType()->isPtrOrPtrVectorTy())
3247       return false;
3248 
3249     auto &CBArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
3250         *this, IRPosition::callsite_argument(CB, OtherArgNo), DepClassTy::NONE);
3251 
3252     // If the argument is readnone, there is no read-write aliasing.
3253     if (CBArgMemBehaviorAA.isAssumedReadNone()) {
3254       A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
3255       return false;
3256     }
3257 
3258     // If the argument is readonly and the underlying value is readonly, there
3259     // is no read-write aliasing.
3260     bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
3261     if (CBArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
3262       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3263       A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
3264       return false;
3265     }
3266 
3267     // We have to utilize actual alias analysis queries so we need the object.
3268     if (!AAR)
3269       AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
3270 
3271     // Try to rule it out at the call site.
3272     bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
3273     LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "
3274                          "callsite arguments: "
3275                       << getAssociatedValue() << " " << *ArgOp << " => "
3276                       << (IsAliasing ? "" : "no-") << "alias \n");
3277 
3278     return IsAliasing;
3279   }
3280 
3281   bool
3282   isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
3283                                          const AAMemoryBehavior &MemBehaviorAA,
3284                                          const AANoAlias &NoAliasAA) {
3285     // We can deduce "noalias" if the following conditions hold.
3286     // (i)   Associated value is assumed to be noalias in the definition.
3287     // (ii)  Associated value is assumed to be no-capture in all the uses
3288     //       possibly executed before this callsite.
3289     // (iii) There is no other pointer argument which could alias with the
3290     //       value.
3291 
3292     bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
3293     if (!AssociatedValueIsNoAliasAtDef) {
3294       LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()
3295                         << " is not no-alias at the definition\n");
3296       return false;
3297     }
3298 
3299     A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL);
3300 
3301     const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
3302     const Function *ScopeFn = VIRP.getAnchorScope();
3303     auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, VIRP, DepClassTy::NONE);
3304     // Check whether the value is captured in the scope using AANoCapture.
3305     //      Look at CFG and check only uses possibly executed before this
3306     //      callsite.
3307     auto UsePred = [&](const Use &U, bool &Follow) -> bool {
3308       Instruction *UserI = cast<Instruction>(U.getUser());
3309 
3310       // If UserI is the curr instruction and there is a single potential use of
3311       // the value in UserI we allow the use.
3312       // TODO: We should inspect the operands and allow those that cannot alias
3313       //       with the value.
3314       if (UserI == getCtxI() && UserI->getNumOperands() == 1)
3315         return true;
3316 
3317       if (ScopeFn) {
3318         const auto &ReachabilityAA = A.getAAFor<AAReachability>(
3319             *this, IRPosition::function(*ScopeFn), DepClassTy::OPTIONAL);
3320 
3321         if (!ReachabilityAA.isAssumedReachable(A, *UserI, *getCtxI()))
3322           return true;
3323 
3324         if (auto *CB = dyn_cast<CallBase>(UserI)) {
3325           if (CB->isArgOperand(&U)) {
3326 
3327             unsigned ArgNo = CB->getArgOperandNo(&U);
3328 
3329             const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
3330                 *this, IRPosition::callsite_argument(*CB, ArgNo),
3331                 DepClassTy::OPTIONAL);
3332 
3333             if (NoCaptureAA.isAssumedNoCapture())
3334               return true;
3335           }
3336         }
3337       }
3338 
3339       // For cases which can potentially have more users
3340       if (isa<GetElementPtrInst>(U) || isa<BitCastInst>(U) || isa<PHINode>(U) ||
3341           isa<SelectInst>(U)) {
3342         Follow = true;
3343         return true;
3344       }
3345 
3346       LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n");
3347       return false;
3348     };
3349 
3350     if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
3351       if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) {
3352         LLVM_DEBUG(
3353             dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()
3354                    << " cannot be noalias as it is potentially captured\n");
3355         return false;
3356       }
3357     }
3358     A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
3359 
3360     // Check there is no other pointer argument which could alias with the
3361     // value passed at this call site.
3362     // TODO: AbstractCallSite
3363     const auto &CB = cast<CallBase>(getAnchorValue());
3364     for (unsigned OtherArgNo = 0; OtherArgNo < CB.arg_size(); OtherArgNo++)
3365       if (mayAliasWithArgument(A, AAR, MemBehaviorAA, CB, OtherArgNo))
3366         return false;
3367 
3368     return true;
3369   }
3370 
3371   /// See AbstractAttribute::updateImpl(...).
3372   ChangeStatus updateImpl(Attributor &A) override {
3373     // If the argument is readnone we are done as there are no accesses via the
3374     // argument.
3375     auto &MemBehaviorAA =
3376         A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
3377     if (MemBehaviorAA.isAssumedReadNone()) {
3378       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3379       return ChangeStatus::UNCHANGED;
3380     }
3381 
3382     const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
3383     const auto &NoAliasAA =
3384         A.getAAFor<AANoAlias>(*this, VIRP, DepClassTy::NONE);
3385 
3386     AAResults *AAR = nullptr;
3387     if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
3388                                                NoAliasAA)) {
3389       LLVM_DEBUG(
3390           dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n");
3391       return ChangeStatus::UNCHANGED;
3392     }
3393 
3394     return indicatePessimisticFixpoint();
3395   }
3396 
3397   /// See AbstractAttribute::trackStatistics()
3398   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
3399 };
3400 
3401 /// NoAlias attribute for function return value.
3402 struct AANoAliasReturned final : AANoAliasImpl {
3403   AANoAliasReturned(const IRPosition &IRP, Attributor &A)
3404       : AANoAliasImpl(IRP, A) {}
3405 
3406   /// See AbstractAttribute::initialize(...).
3407   void initialize(Attributor &A) override {
3408     AANoAliasImpl::initialize(A);
3409     Function *F = getAssociatedFunction();
3410     if (!F || F->isDeclaration())
3411       indicatePessimisticFixpoint();
3412   }
3413 
3414   /// See AbstractAttribute::updateImpl(...).
3415   virtual ChangeStatus updateImpl(Attributor &A) override {
3416 
3417     auto CheckReturnValue = [&](Value &RV) -> bool {
3418       if (Constant *C = dyn_cast<Constant>(&RV))
3419         if (C->isNullValue() || isa<UndefValue>(C))
3420           return true;
3421 
3422       /// For now, we can only deduce noalias if we have call sites.
3423       /// FIXME: add more support.
3424       if (!isa<CallBase>(&RV))
3425         return false;
3426 
3427       const IRPosition &RVPos = IRPosition::value(RV);
3428       const auto &NoAliasAA =
3429           A.getAAFor<AANoAlias>(*this, RVPos, DepClassTy::REQUIRED);
3430       if (!NoAliasAA.isAssumedNoAlias())
3431         return false;
3432 
3433       const auto &NoCaptureAA =
3434           A.getAAFor<AANoCapture>(*this, RVPos, DepClassTy::REQUIRED);
3435       return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
3436     };
3437 
3438     if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
3439       return indicatePessimisticFixpoint();
3440 
3441     return ChangeStatus::UNCHANGED;
3442   }
3443 
3444   /// See AbstractAttribute::trackStatistics()
3445   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
3446 };
3447 
3448 /// NoAlias attribute deduction for a call site return value.
3449 struct AANoAliasCallSiteReturned final : AANoAliasImpl {
3450   AANoAliasCallSiteReturned(const IRPosition &IRP, Attributor &A)
3451       : AANoAliasImpl(IRP, A) {}
3452 
3453   /// See AbstractAttribute::initialize(...).
3454   void initialize(Attributor &A) override {
3455     AANoAliasImpl::initialize(A);
3456     Function *F = getAssociatedFunction();
3457     if (!F || F->isDeclaration())
3458       indicatePessimisticFixpoint();
3459   }
3460 
3461   /// See AbstractAttribute::updateImpl(...).
3462   ChangeStatus updateImpl(Attributor &A) override {
3463     // TODO: Once we have call site specific value information we can provide
3464     //       call site specific liveness information and then it makes
3465     //       sense to specialize attributes for call sites arguments instead of
3466     //       redirecting requests to the callee argument.
3467     Function *F = getAssociatedFunction();
3468     const IRPosition &FnPos = IRPosition::returned(*F);
3469     auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos, DepClassTy::REQUIRED);
3470     return clampStateAndIndicateChange(getState(), FnAA.getState());
3471   }
3472 
3473   /// See AbstractAttribute::trackStatistics()
3474   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
3475 };
3476 } // namespace
3477 
3478 /// -------------------AAIsDead Function Attribute-----------------------
3479 
3480 namespace {
3481 struct AAIsDeadValueImpl : public AAIsDead {
3482   AAIsDeadValueImpl(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
3483 
3484   /// See AAIsDead::isAssumedDead().
3485   bool isAssumedDead() const override { return isAssumed(IS_DEAD); }
3486 
3487   /// See AAIsDead::isKnownDead().
3488   bool isKnownDead() const override { return isKnown(IS_DEAD); }
3489 
3490   /// See AAIsDead::isAssumedDead(BasicBlock *).
3491   bool isAssumedDead(const BasicBlock *BB) const override { return false; }
3492 
3493   /// See AAIsDead::isKnownDead(BasicBlock *).
3494   bool isKnownDead(const BasicBlock *BB) const override { return false; }
3495 
3496   /// See AAIsDead::isAssumedDead(Instruction *I).
3497   bool isAssumedDead(const Instruction *I) const override {
3498     return I == getCtxI() && isAssumedDead();
3499   }
3500 
3501   /// See AAIsDead::isKnownDead(Instruction *I).
3502   bool isKnownDead(const Instruction *I) const override {
3503     return isAssumedDead(I) && isKnownDead();
3504   }
3505 
3506   /// See AbstractAttribute::getAsStr().
3507   virtual const std::string getAsStr() const override {
3508     return isAssumedDead() ? "assumed-dead" : "assumed-live";
3509   }
3510 
3511   /// Check if all uses are assumed dead.
3512   bool areAllUsesAssumedDead(Attributor &A, Value &V) {
3513     // Callers might not check the type, void has no uses.
3514     if (V.getType()->isVoidTy())
3515       return true;
3516 
3517     // If we replace a value with a constant there are no uses left afterwards.
3518     if (!isa<Constant>(V)) {
3519       bool UsedAssumedInformation = false;
3520       Optional<Constant *> C =
3521           A.getAssumedConstant(V, *this, UsedAssumedInformation);
3522       if (!C.hasValue() || *C)
3523         return true;
3524     }
3525 
3526     auto UsePred = [&](const Use &U, bool &Follow) { return false; };
3527     // Explicitly set the dependence class to required because we want a long
3528     // chain of N dependent instructions to be considered live as soon as one is
3529     // without going through N update cycles. This is not required for
3530     // correctness.
3531     return A.checkForAllUses(UsePred, *this, V, /* CheckBBLivenessOnly */ false,
3532                              DepClassTy::REQUIRED);
3533   }
3534 
3535   /// Determine if \p I is assumed to be side-effect free.
3536   bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
3537     if (!I || wouldInstructionBeTriviallyDead(I))
3538       return true;
3539 
3540     auto *CB = dyn_cast<CallBase>(I);
3541     if (!CB || isa<IntrinsicInst>(CB))
3542       return false;
3543 
3544     const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
3545     const auto &NoUnwindAA =
3546         A.getAndUpdateAAFor<AANoUnwind>(*this, CallIRP, DepClassTy::NONE);
3547     if (!NoUnwindAA.isAssumedNoUnwind())
3548       return false;
3549     if (!NoUnwindAA.isKnownNoUnwind())
3550       A.recordDependence(NoUnwindAA, *this, DepClassTy::OPTIONAL);
3551 
3552     bool IsKnown;
3553     return AA::isAssumedReadOnly(A, CallIRP, *this, IsKnown);
3554   }
3555 };
3556 
3557 struct AAIsDeadFloating : public AAIsDeadValueImpl {
3558   AAIsDeadFloating(const IRPosition &IRP, Attributor &A)
3559       : AAIsDeadValueImpl(IRP, A) {}
3560 
3561   /// See AbstractAttribute::initialize(...).
3562   void initialize(Attributor &A) override {
3563     if (isa<UndefValue>(getAssociatedValue())) {
3564       indicatePessimisticFixpoint();
3565       return;
3566     }
3567 
3568     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3569     if (!isAssumedSideEffectFree(A, I)) {
3570       if (!isa_and_nonnull<StoreInst>(I))
3571         indicatePessimisticFixpoint();
3572       else
3573         removeAssumedBits(HAS_NO_EFFECT);
3574     }
3575   }
3576 
3577   bool isDeadStore(Attributor &A, StoreInst &SI) {
3578     // Lang ref now states volatile store is not UB/dead, let's skip them.
3579     if (SI.isVolatile())
3580       return false;
3581 
3582     bool UsedAssumedInformation = false;
3583     SmallSetVector<Value *, 4> PotentialCopies;
3584     if (!AA::getPotentialCopiesOfStoredValue(A, SI, PotentialCopies, *this,
3585                                              UsedAssumedInformation))
3586       return false;
3587     return llvm::all_of(PotentialCopies, [&](Value *V) {
3588       return A.isAssumedDead(IRPosition::value(*V), this, nullptr,
3589                              UsedAssumedInformation);
3590     });
3591   }
3592 
3593   /// See AbstractAttribute::getAsStr().
3594   const std::string getAsStr() const override {
3595     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3596     if (isa_and_nonnull<StoreInst>(I))
3597       if (isValidState())
3598         return "assumed-dead-store";
3599     return AAIsDeadValueImpl::getAsStr();
3600   }
3601 
3602   /// See AbstractAttribute::updateImpl(...).
3603   ChangeStatus updateImpl(Attributor &A) override {
3604     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3605     if (auto *SI = dyn_cast_or_null<StoreInst>(I)) {
3606       if (!isDeadStore(A, *SI))
3607         return indicatePessimisticFixpoint();
3608     } else {
3609       if (!isAssumedSideEffectFree(A, I))
3610         return indicatePessimisticFixpoint();
3611       if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3612         return indicatePessimisticFixpoint();
3613     }
3614     return ChangeStatus::UNCHANGED;
3615   }
3616 
3617   /// See AbstractAttribute::manifest(...).
3618   ChangeStatus manifest(Attributor &A) override {
3619     Value &V = getAssociatedValue();
3620     if (auto *I = dyn_cast<Instruction>(&V)) {
3621       // If we get here we basically know the users are all dead. We check if
3622       // isAssumedSideEffectFree returns true here again because it might not be
3623       // the case and only the users are dead but the instruction (=call) is
3624       // still needed.
3625       if (isa<StoreInst>(I) ||
3626           (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I))) {
3627         A.deleteAfterManifest(*I);
3628         return ChangeStatus::CHANGED;
3629       }
3630     }
3631     if (V.use_empty())
3632       return ChangeStatus::UNCHANGED;
3633 
3634     bool UsedAssumedInformation = false;
3635     Optional<Constant *> C =
3636         A.getAssumedConstant(V, *this, UsedAssumedInformation);
3637     if (C.hasValue() && C.getValue())
3638       return ChangeStatus::UNCHANGED;
3639 
3640     // Replace the value with undef as it is dead but keep droppable uses around
3641     // as they provide information we don't want to give up on just yet.
3642     UndefValue &UV = *UndefValue::get(V.getType());
3643     bool AnyChange =
3644         A.changeValueAfterManifest(V, UV, /* ChangeDropppable */ false);
3645     return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3646   }
3647 
3648   /// See AbstractAttribute::trackStatistics()
3649   void trackStatistics() const override {
3650     STATS_DECLTRACK_FLOATING_ATTR(IsDead)
3651   }
3652 };
3653 
3654 struct AAIsDeadArgument : public AAIsDeadFloating {
3655   AAIsDeadArgument(const IRPosition &IRP, Attributor &A)
3656       : AAIsDeadFloating(IRP, A) {}
3657 
3658   /// See AbstractAttribute::initialize(...).
3659   void initialize(Attributor &A) override {
3660     if (!A.isFunctionIPOAmendable(*getAnchorScope()))
3661       indicatePessimisticFixpoint();
3662   }
3663 
3664   /// See AbstractAttribute::manifest(...).
3665   ChangeStatus manifest(Attributor &A) override {
3666     ChangeStatus Changed = AAIsDeadFloating::manifest(A);
3667     Argument &Arg = *getAssociatedArgument();
3668     if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
3669       if (A.registerFunctionSignatureRewrite(
3670               Arg, /* ReplacementTypes */ {},
3671               Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
3672               Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) {
3673         Arg.dropDroppableUses();
3674         return ChangeStatus::CHANGED;
3675       }
3676     return Changed;
3677   }
3678 
3679   /// See AbstractAttribute::trackStatistics()
3680   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) }
3681 };
3682 
3683 struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
3684   AAIsDeadCallSiteArgument(const IRPosition &IRP, Attributor &A)
3685       : AAIsDeadValueImpl(IRP, A) {}
3686 
3687   /// See AbstractAttribute::initialize(...).
3688   void initialize(Attributor &A) override {
3689     if (isa<UndefValue>(getAssociatedValue()))
3690       indicatePessimisticFixpoint();
3691   }
3692 
3693   /// See AbstractAttribute::updateImpl(...).
3694   ChangeStatus updateImpl(Attributor &A) override {
3695     // TODO: Once we have call site specific value information we can provide
3696     //       call site specific liveness information and then it makes
3697     //       sense to specialize attributes for call sites arguments instead of
3698     //       redirecting requests to the callee argument.
3699     Argument *Arg = getAssociatedArgument();
3700     if (!Arg)
3701       return indicatePessimisticFixpoint();
3702     const IRPosition &ArgPos = IRPosition::argument(*Arg);
3703     auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos, DepClassTy::REQUIRED);
3704     return clampStateAndIndicateChange(getState(), ArgAA.getState());
3705   }
3706 
3707   /// See AbstractAttribute::manifest(...).
3708   ChangeStatus manifest(Attributor &A) override {
3709     CallBase &CB = cast<CallBase>(getAnchorValue());
3710     Use &U = CB.getArgOperandUse(getCallSiteArgNo());
3711     assert(!isa<UndefValue>(U.get()) &&
3712            "Expected undef values to be filtered out!");
3713     UndefValue &UV = *UndefValue::get(U->getType());
3714     if (A.changeUseAfterManifest(U, UV))
3715       return ChangeStatus::CHANGED;
3716     return ChangeStatus::UNCHANGED;
3717   }
3718 
3719   /// See AbstractAttribute::trackStatistics()
3720   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) }
3721 };
3722 
3723 struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
3724   AAIsDeadCallSiteReturned(const IRPosition &IRP, Attributor &A)
3725       : AAIsDeadFloating(IRP, A) {}
3726 
3727   /// See AAIsDead::isAssumedDead().
3728   bool isAssumedDead() const override {
3729     return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
3730   }
3731 
3732   /// See AbstractAttribute::initialize(...).
3733   void initialize(Attributor &A) override {
3734     if (isa<UndefValue>(getAssociatedValue())) {
3735       indicatePessimisticFixpoint();
3736       return;
3737     }
3738 
3739     // We track this separately as a secondary state.
3740     IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
3741   }
3742 
3743   /// See AbstractAttribute::updateImpl(...).
3744   ChangeStatus updateImpl(Attributor &A) override {
3745     ChangeStatus Changed = ChangeStatus::UNCHANGED;
3746     if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
3747       IsAssumedSideEffectFree = false;
3748       Changed = ChangeStatus::CHANGED;
3749     }
3750     if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3751       return indicatePessimisticFixpoint();
3752     return Changed;
3753   }
3754 
3755   /// See AbstractAttribute::trackStatistics()
3756   void trackStatistics() const override {
3757     if (IsAssumedSideEffectFree)
3758       STATS_DECLTRACK_CSRET_ATTR(IsDead)
3759     else
3760       STATS_DECLTRACK_CSRET_ATTR(UnusedResult)
3761   }
3762 
3763   /// See AbstractAttribute::getAsStr().
3764   const std::string getAsStr() const override {
3765     return isAssumedDead()
3766                ? "assumed-dead"
3767                : (getAssumed() ? "assumed-dead-users" : "assumed-live");
3768   }
3769 
3770 private:
3771   bool IsAssumedSideEffectFree = true;
3772 };
3773 
3774 struct AAIsDeadReturned : public AAIsDeadValueImpl {
3775   AAIsDeadReturned(const IRPosition &IRP, Attributor &A)
3776       : AAIsDeadValueImpl(IRP, A) {}
3777 
3778   /// See AbstractAttribute::updateImpl(...).
3779   ChangeStatus updateImpl(Attributor &A) override {
3780 
3781     bool UsedAssumedInformation = false;
3782     A.checkForAllInstructions([](Instruction &) { return true; }, *this,
3783                               {Instruction::Ret}, UsedAssumedInformation);
3784 
3785     auto PredForCallSite = [&](AbstractCallSite ACS) {
3786       if (ACS.isCallbackCall() || !ACS.getInstruction())
3787         return false;
3788       return areAllUsesAssumedDead(A, *ACS.getInstruction());
3789     };
3790 
3791     if (!A.checkForAllCallSites(PredForCallSite, *this, true,
3792                                 UsedAssumedInformation))
3793       return indicatePessimisticFixpoint();
3794 
3795     return ChangeStatus::UNCHANGED;
3796   }
3797 
3798   /// See AbstractAttribute::manifest(...).
3799   ChangeStatus manifest(Attributor &A) override {
3800     // TODO: Rewrite the signature to return void?
3801     bool AnyChange = false;
3802     UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
3803     auto RetInstPred = [&](Instruction &I) {
3804       ReturnInst &RI = cast<ReturnInst>(I);
3805       if (!isa<UndefValue>(RI.getReturnValue()))
3806         AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
3807       return true;
3808     };
3809     bool UsedAssumedInformation = false;
3810     A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret},
3811                               UsedAssumedInformation);
3812     return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3813   }
3814 
3815   /// See AbstractAttribute::trackStatistics()
3816   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) }
3817 };
3818 
3819 struct AAIsDeadFunction : public AAIsDead {
3820   AAIsDeadFunction(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
3821 
3822   /// See AbstractAttribute::initialize(...).
3823   void initialize(Attributor &A) override {
3824     const Function *F = getAnchorScope();
3825     if (F && !F->isDeclaration()) {
3826       // We only want to compute liveness once. If the function is not part of
3827       // the SCC, skip it.
3828       if (A.isRunOn(*const_cast<Function *>(F))) {
3829         ToBeExploredFrom.insert(&F->getEntryBlock().front());
3830         assumeLive(A, F->getEntryBlock());
3831       } else {
3832         indicatePessimisticFixpoint();
3833       }
3834     }
3835   }
3836 
3837   /// See AbstractAttribute::getAsStr().
3838   const std::string getAsStr() const override {
3839     return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
3840            std::to_string(getAnchorScope()->size()) + "][#TBEP " +
3841            std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
3842            std::to_string(KnownDeadEnds.size()) + "]";
3843   }
3844 
3845   /// See AbstractAttribute::manifest(...).
3846   ChangeStatus manifest(Attributor &A) override {
3847     assert(getState().isValidState() &&
3848            "Attempted to manifest an invalid state!");
3849 
3850     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
3851     Function &F = *getAnchorScope();
3852 
3853     if (AssumedLiveBlocks.empty()) {
3854       A.deleteAfterManifest(F);
3855       return ChangeStatus::CHANGED;
3856     }
3857 
3858     // Flag to determine if we can change an invoke to a call assuming the
3859     // callee is nounwind. This is not possible if the personality of the
3860     // function allows to catch asynchronous exceptions.
3861     bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
3862 
3863     KnownDeadEnds.set_union(ToBeExploredFrom);
3864     for (const Instruction *DeadEndI : KnownDeadEnds) {
3865       auto *CB = dyn_cast<CallBase>(DeadEndI);
3866       if (!CB)
3867         continue;
3868       const auto &NoReturnAA = A.getAndUpdateAAFor<AANoReturn>(
3869           *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
3870       bool MayReturn = !NoReturnAA.isAssumedNoReturn();
3871       if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
3872         continue;
3873 
3874       if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
3875         A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
3876       else
3877         A.changeToUnreachableAfterManifest(
3878             const_cast<Instruction *>(DeadEndI->getNextNode()));
3879       HasChanged = ChangeStatus::CHANGED;
3880     }
3881 
3882     STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.");
3883     for (BasicBlock &BB : F)
3884       if (!AssumedLiveBlocks.count(&BB)) {
3885         A.deleteAfterManifest(BB);
3886         ++BUILD_STAT_NAME(AAIsDead, BasicBlock);
3887         HasChanged = ChangeStatus::CHANGED;
3888       }
3889 
3890     return HasChanged;
3891   }
3892 
3893   /// See AbstractAttribute::updateImpl(...).
3894   ChangeStatus updateImpl(Attributor &A) override;
3895 
3896   bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const override {
3897     assert(From->getParent() == getAnchorScope() &&
3898            To->getParent() == getAnchorScope() &&
3899            "Used AAIsDead of the wrong function");
3900     return isValidState() && !AssumedLiveEdges.count(std::make_pair(From, To));
3901   }
3902 
3903   /// See AbstractAttribute::trackStatistics()
3904   void trackStatistics() const override {}
3905 
3906   /// Returns true if the function is assumed dead.
3907   bool isAssumedDead() const override { return false; }
3908 
3909   /// See AAIsDead::isKnownDead().
3910   bool isKnownDead() const override { return false; }
3911 
3912   /// See AAIsDead::isAssumedDead(BasicBlock *).
3913   bool isAssumedDead(const BasicBlock *BB) const override {
3914     assert(BB->getParent() == getAnchorScope() &&
3915            "BB must be in the same anchor scope function.");
3916 
3917     if (!getAssumed())
3918       return false;
3919     return !AssumedLiveBlocks.count(BB);
3920   }
3921 
3922   /// See AAIsDead::isKnownDead(BasicBlock *).
3923   bool isKnownDead(const BasicBlock *BB) const override {
3924     return getKnown() && isAssumedDead(BB);
3925   }
3926 
3927   /// See AAIsDead::isAssumed(Instruction *I).
3928   bool isAssumedDead(const Instruction *I) const override {
3929     assert(I->getParent()->getParent() == getAnchorScope() &&
3930            "Instruction must be in the same anchor scope function.");
3931 
3932     if (!getAssumed())
3933       return false;
3934 
3935     // If it is not in AssumedLiveBlocks then it for sure dead.
3936     // Otherwise, it can still be after noreturn call in a live block.
3937     if (!AssumedLiveBlocks.count(I->getParent()))
3938       return true;
3939 
3940     // If it is not after a liveness barrier it is live.
3941     const Instruction *PrevI = I->getPrevNode();
3942     while (PrevI) {
3943       if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
3944         return true;
3945       PrevI = PrevI->getPrevNode();
3946     }
3947     return false;
3948   }
3949 
3950   /// See AAIsDead::isKnownDead(Instruction *I).
3951   bool isKnownDead(const Instruction *I) const override {
3952     return getKnown() && isAssumedDead(I);
3953   }
3954 
3955   /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
3956   /// that internal function called from \p BB should now be looked at.
3957   bool assumeLive(Attributor &A, const BasicBlock &BB) {
3958     if (!AssumedLiveBlocks.insert(&BB).second)
3959       return false;
3960 
3961     // We assume that all of BB is (probably) live now and if there are calls to
3962     // internal functions we will assume that those are now live as well. This
3963     // is a performance optimization for blocks with calls to a lot of internal
3964     // functions. It can however cause dead functions to be treated as live.
3965     for (const Instruction &I : BB)
3966       if (const auto *CB = dyn_cast<CallBase>(&I))
3967         if (const Function *F = CB->getCalledFunction())
3968           if (F->hasLocalLinkage())
3969             A.markLiveInternalFunction(*F);
3970     return true;
3971   }
3972 
3973   /// Collection of instructions that need to be explored again, e.g., we
3974   /// did assume they do not transfer control to (one of their) successors.
3975   SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
3976 
3977   /// Collection of instructions that are known to not transfer control.
3978   SmallSetVector<const Instruction *, 8> KnownDeadEnds;
3979 
3980   /// Collection of all assumed live edges
3981   DenseSet<std::pair<const BasicBlock *, const BasicBlock *>> AssumedLiveEdges;
3982 
3983   /// Collection of all assumed live BasicBlocks.
3984   DenseSet<const BasicBlock *> AssumedLiveBlocks;
3985 };
3986 
3987 static bool
3988 identifyAliveSuccessors(Attributor &A, const CallBase &CB,
3989                         AbstractAttribute &AA,
3990                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3991   const IRPosition &IPos = IRPosition::callsite_function(CB);
3992 
3993   const auto &NoReturnAA =
3994       A.getAndUpdateAAFor<AANoReturn>(AA, IPos, DepClassTy::OPTIONAL);
3995   if (NoReturnAA.isAssumedNoReturn())
3996     return !NoReturnAA.isKnownNoReturn();
3997   if (CB.isTerminator())
3998     AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
3999   else
4000     AliveSuccessors.push_back(CB.getNextNode());
4001   return false;
4002 }
4003 
4004 static bool
4005 identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
4006                         AbstractAttribute &AA,
4007                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
4008   bool UsedAssumedInformation =
4009       identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
4010 
4011   // First, determine if we can change an invoke to a call assuming the
4012   // callee is nounwind. This is not possible if the personality of the
4013   // function allows to catch asynchronous exceptions.
4014   if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
4015     AliveSuccessors.push_back(&II.getUnwindDest()->front());
4016   } else {
4017     const IRPosition &IPos = IRPosition::callsite_function(II);
4018     const auto &AANoUnw =
4019         A.getAndUpdateAAFor<AANoUnwind>(AA, IPos, DepClassTy::OPTIONAL);
4020     if (AANoUnw.isAssumedNoUnwind()) {
4021       UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
4022     } else {
4023       AliveSuccessors.push_back(&II.getUnwindDest()->front());
4024     }
4025   }
4026   return UsedAssumedInformation;
4027 }
4028 
4029 static bool
4030 identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
4031                         AbstractAttribute &AA,
4032                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
4033   bool UsedAssumedInformation = false;
4034   if (BI.getNumSuccessors() == 1) {
4035     AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
4036   } else {
4037     Optional<Constant *> C =
4038         A.getAssumedConstant(*BI.getCondition(), AA, UsedAssumedInformation);
4039     if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
4040       // No value yet, assume both edges are dead.
4041     } else if (isa_and_nonnull<ConstantInt>(*C)) {
4042       const BasicBlock *SuccBB =
4043           BI.getSuccessor(1 - cast<ConstantInt>(*C)->getValue().getZExtValue());
4044       AliveSuccessors.push_back(&SuccBB->front());
4045     } else {
4046       AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
4047       AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
4048       UsedAssumedInformation = false;
4049     }
4050   }
4051   return UsedAssumedInformation;
4052 }
4053 
4054 static bool
4055 identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
4056                         AbstractAttribute &AA,
4057                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
4058   bool UsedAssumedInformation = false;
4059   Optional<Constant *> C =
4060       A.getAssumedConstant(*SI.getCondition(), AA, UsedAssumedInformation);
4061   if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
4062     // No value yet, assume all edges are dead.
4063   } else if (isa_and_nonnull<ConstantInt>(C.getValue())) {
4064     for (auto &CaseIt : SI.cases()) {
4065       if (CaseIt.getCaseValue() == C.getValue()) {
4066         AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
4067         return UsedAssumedInformation;
4068       }
4069     }
4070     AliveSuccessors.push_back(&SI.getDefaultDest()->front());
4071     return UsedAssumedInformation;
4072   } else {
4073     for (const BasicBlock *SuccBB : successors(SI.getParent()))
4074       AliveSuccessors.push_back(&SuccBB->front());
4075   }
4076   return UsedAssumedInformation;
4077 }
4078 
4079 ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
4080   ChangeStatus Change = ChangeStatus::UNCHANGED;
4081 
4082   LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"
4083                     << getAnchorScope()->size() << "] BBs and "
4084                     << ToBeExploredFrom.size() << " exploration points and "
4085                     << KnownDeadEnds.size() << " known dead ends\n");
4086 
4087   // Copy and clear the list of instructions we need to explore from. It is
4088   // refilled with instructions the next update has to look at.
4089   SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
4090                                                ToBeExploredFrom.end());
4091   decltype(ToBeExploredFrom) NewToBeExploredFrom;
4092 
4093   SmallVector<const Instruction *, 8> AliveSuccessors;
4094   while (!Worklist.empty()) {
4095     const Instruction *I = Worklist.pop_back_val();
4096     LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n");
4097 
4098     // Fast forward for uninteresting instructions. We could look for UB here
4099     // though.
4100     while (!I->isTerminator() && !isa<CallBase>(I))
4101       I = I->getNextNode();
4102 
4103     AliveSuccessors.clear();
4104 
4105     bool UsedAssumedInformation = false;
4106     switch (I->getOpcode()) {
4107     // TODO: look for (assumed) UB to backwards propagate "deadness".
4108     default:
4109       assert(I->isTerminator() &&
4110              "Expected non-terminators to be handled already!");
4111       for (const BasicBlock *SuccBB : successors(I->getParent()))
4112         AliveSuccessors.push_back(&SuccBB->front());
4113       break;
4114     case Instruction::Call:
4115       UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
4116                                                        *this, AliveSuccessors);
4117       break;
4118     case Instruction::Invoke:
4119       UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
4120                                                        *this, AliveSuccessors);
4121       break;
4122     case Instruction::Br:
4123       UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
4124                                                        *this, AliveSuccessors);
4125       break;
4126     case Instruction::Switch:
4127       UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
4128                                                        *this, AliveSuccessors);
4129       break;
4130     }
4131 
4132     if (UsedAssumedInformation) {
4133       NewToBeExploredFrom.insert(I);
4134     } else if (AliveSuccessors.empty() ||
4135                (I->isTerminator() &&
4136                 AliveSuccessors.size() < I->getNumSuccessors())) {
4137       if (KnownDeadEnds.insert(I))
4138         Change = ChangeStatus::CHANGED;
4139     }
4140 
4141     LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "
4142                       << AliveSuccessors.size() << " UsedAssumedInformation: "
4143                       << UsedAssumedInformation << "\n");
4144 
4145     for (const Instruction *AliveSuccessor : AliveSuccessors) {
4146       if (!I->isTerminator()) {
4147         assert(AliveSuccessors.size() == 1 &&
4148                "Non-terminator expected to have a single successor!");
4149         Worklist.push_back(AliveSuccessor);
4150       } else {
4151         // record the assumed live edge
4152         auto Edge = std::make_pair(I->getParent(), AliveSuccessor->getParent());
4153         if (AssumedLiveEdges.insert(Edge).second)
4154           Change = ChangeStatus::CHANGED;
4155         if (assumeLive(A, *AliveSuccessor->getParent()))
4156           Worklist.push_back(AliveSuccessor);
4157       }
4158     }
4159   }
4160 
4161   // Check if the content of ToBeExploredFrom changed, ignore the order.
4162   if (NewToBeExploredFrom.size() != ToBeExploredFrom.size() ||
4163       llvm::any_of(NewToBeExploredFrom, [&](const Instruction *I) {
4164         return !ToBeExploredFrom.count(I);
4165       })) {
4166     Change = ChangeStatus::CHANGED;
4167     ToBeExploredFrom = std::move(NewToBeExploredFrom);
4168   }
4169 
4170   // If we know everything is live there is no need to query for liveness.
4171   // Instead, indicating a pessimistic fixpoint will cause the state to be
4172   // "invalid" and all queries to be answered conservatively without lookups.
4173   // To be in this state we have to (1) finished the exploration and (3) not
4174   // discovered any non-trivial dead end and (2) not ruled unreachable code
4175   // dead.
4176   if (ToBeExploredFrom.empty() &&
4177       getAnchorScope()->size() == AssumedLiveBlocks.size() &&
4178       llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
4179         return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
4180       }))
4181     return indicatePessimisticFixpoint();
4182   return Change;
4183 }
4184 
4185 /// Liveness information for a call sites.
4186 struct AAIsDeadCallSite final : AAIsDeadFunction {
4187   AAIsDeadCallSite(const IRPosition &IRP, Attributor &A)
4188       : AAIsDeadFunction(IRP, A) {}
4189 
4190   /// See AbstractAttribute::initialize(...).
4191   void initialize(Attributor &A) override {
4192     // TODO: Once we have call site specific value information we can provide
4193     //       call site specific liveness information and then it makes
4194     //       sense to specialize attributes for call sites instead of
4195     //       redirecting requests to the callee.
4196     llvm_unreachable("Abstract attributes for liveness are not "
4197                      "supported for call sites yet!");
4198   }
4199 
4200   /// See AbstractAttribute::updateImpl(...).
4201   ChangeStatus updateImpl(Attributor &A) override {
4202     return indicatePessimisticFixpoint();
4203   }
4204 
4205   /// See AbstractAttribute::trackStatistics()
4206   void trackStatistics() const override {}
4207 };
4208 } // namespace
4209 
4210 /// -------------------- Dereferenceable Argument Attribute --------------------
4211 
4212 namespace {
4213 struct AADereferenceableImpl : AADereferenceable {
4214   AADereferenceableImpl(const IRPosition &IRP, Attributor &A)
4215       : AADereferenceable(IRP, A) {}
4216   using StateType = DerefState;
4217 
4218   /// See AbstractAttribute::initialize(...).
4219   void initialize(Attributor &A) override {
4220     SmallVector<Attribute, 4> Attrs;
4221     getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
4222              Attrs, /* IgnoreSubsumingPositions */ false, &A);
4223     for (const Attribute &Attr : Attrs)
4224       takeKnownDerefBytesMaximum(Attr.getValueAsInt());
4225 
4226     const IRPosition &IRP = this->getIRPosition();
4227     NonNullAA = &A.getAAFor<AANonNull>(*this, IRP, DepClassTy::NONE);
4228 
4229     bool CanBeNull, CanBeFreed;
4230     takeKnownDerefBytesMaximum(
4231         IRP.getAssociatedValue().getPointerDereferenceableBytes(
4232             A.getDataLayout(), CanBeNull, CanBeFreed));
4233 
4234     bool IsFnInterface = IRP.isFnInterfaceKind();
4235     Function *FnScope = IRP.getAnchorScope();
4236     if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope))) {
4237       indicatePessimisticFixpoint();
4238       return;
4239     }
4240 
4241     if (Instruction *CtxI = getCtxI())
4242       followUsesInMBEC(*this, A, getState(), *CtxI);
4243   }
4244 
4245   /// See AbstractAttribute::getState()
4246   /// {
4247   StateType &getState() override { return *this; }
4248   const StateType &getState() const override { return *this; }
4249   /// }
4250 
4251   /// Helper function for collecting accessed bytes in must-be-executed-context
4252   void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I,
4253                               DerefState &State) {
4254     const Value *UseV = U->get();
4255     if (!UseV->getType()->isPointerTy())
4256       return;
4257 
4258     Optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I);
4259     if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile())
4260       return;
4261 
4262     int64_t Offset;
4263     const Value *Base = GetPointerBaseWithConstantOffset(
4264         Loc->Ptr, Offset, A.getDataLayout(), /*AllowNonInbounds*/ true);
4265     if (Base && Base == &getAssociatedValue())
4266       State.addAccessedBytes(Offset, Loc->Size.getValue());
4267   }
4268 
4269   /// See followUsesInMBEC
4270   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
4271                        AADereferenceable::StateType &State) {
4272     bool IsNonNull = false;
4273     bool TrackUse = false;
4274     int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
4275         A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
4276     LLVM_DEBUG(dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytes
4277                       << " for instruction " << *I << "\n");
4278 
4279     addAccessedBytesForUse(A, U, I, State);
4280     State.takeKnownDerefBytesMaximum(DerefBytes);
4281     return TrackUse;
4282   }
4283 
4284   /// See AbstractAttribute::manifest(...).
4285   ChangeStatus manifest(Attributor &A) override {
4286     ChangeStatus Change = AADereferenceable::manifest(A);
4287     if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
4288       removeAttrs({Attribute::DereferenceableOrNull});
4289       return ChangeStatus::CHANGED;
4290     }
4291     return Change;
4292   }
4293 
4294   void getDeducedAttributes(LLVMContext &Ctx,
4295                             SmallVectorImpl<Attribute> &Attrs) const override {
4296     // TODO: Add *_globally support
4297     if (isAssumedNonNull())
4298       Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
4299           Ctx, getAssumedDereferenceableBytes()));
4300     else
4301       Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
4302           Ctx, getAssumedDereferenceableBytes()));
4303   }
4304 
4305   /// See AbstractAttribute::getAsStr().
4306   const std::string getAsStr() const override {
4307     if (!getAssumedDereferenceableBytes())
4308       return "unknown-dereferenceable";
4309     return std::string("dereferenceable") +
4310            (isAssumedNonNull() ? "" : "_or_null") +
4311            (isAssumedGlobal() ? "_globally" : "") + "<" +
4312            std::to_string(getKnownDereferenceableBytes()) + "-" +
4313            std::to_string(getAssumedDereferenceableBytes()) + ">";
4314   }
4315 };
4316 
4317 /// Dereferenceable attribute for a floating value.
4318 struct AADereferenceableFloating : AADereferenceableImpl {
4319   AADereferenceableFloating(const IRPosition &IRP, Attributor &A)
4320       : AADereferenceableImpl(IRP, A) {}
4321 
4322   /// See AbstractAttribute::updateImpl(...).
4323   ChangeStatus updateImpl(Attributor &A) override {
4324     const DataLayout &DL = A.getDataLayout();
4325 
4326     auto VisitValueCB = [&](const Value &V, const Instruction *, DerefState &T,
4327                             bool Stripped) -> bool {
4328       unsigned IdxWidth =
4329           DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
4330       APInt Offset(IdxWidth, 0);
4331       const Value *Base = stripAndAccumulateOffsets(
4332           A, *this, &V, DL, Offset, /* GetMinOffset */ false,
4333           /* AllowNonInbounds */ true);
4334 
4335       const auto &AA = A.getAAFor<AADereferenceable>(
4336           *this, IRPosition::value(*Base), DepClassTy::REQUIRED);
4337       int64_t DerefBytes = 0;
4338       if (!Stripped && this == &AA) {
4339         // Use IR information if we did not strip anything.
4340         // TODO: track globally.
4341         bool CanBeNull, CanBeFreed;
4342         DerefBytes =
4343             Base->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
4344         T.GlobalState.indicatePessimisticFixpoint();
4345       } else {
4346         const DerefState &DS = AA.getState();
4347         DerefBytes = DS.DerefBytesState.getAssumed();
4348         T.GlobalState &= DS.GlobalState;
4349       }
4350 
4351       // For now we do not try to "increase" dereferenceability due to negative
4352       // indices as we first have to come up with code to deal with loops and
4353       // for overflows of the dereferenceable bytes.
4354       int64_t OffsetSExt = Offset.getSExtValue();
4355       if (OffsetSExt < 0)
4356         OffsetSExt = 0;
4357 
4358       T.takeAssumedDerefBytesMinimum(
4359           std::max(int64_t(0), DerefBytes - OffsetSExt));
4360 
4361       if (this == &AA) {
4362         if (!Stripped) {
4363           // If nothing was stripped IR information is all we got.
4364           T.takeKnownDerefBytesMaximum(
4365               std::max(int64_t(0), DerefBytes - OffsetSExt));
4366           T.indicatePessimisticFixpoint();
4367         } else if (OffsetSExt > 0) {
4368           // If something was stripped but there is circular reasoning we look
4369           // for the offset. If it is positive we basically decrease the
4370           // dereferenceable bytes in a circluar loop now, which will simply
4371           // drive them down to the known value in a very slow way which we
4372           // can accelerate.
4373           T.indicatePessimisticFixpoint();
4374         }
4375       }
4376 
4377       return T.isValidState();
4378     };
4379 
4380     DerefState T;
4381     bool UsedAssumedInformation = false;
4382     if (!genericValueTraversal<DerefState>(A, getIRPosition(), *this, T,
4383                                            VisitValueCB, getCtxI(),
4384                                            UsedAssumedInformation))
4385       return indicatePessimisticFixpoint();
4386 
4387     return clampStateAndIndicateChange(getState(), T);
4388   }
4389 
4390   /// See AbstractAttribute::trackStatistics()
4391   void trackStatistics() const override {
4392     STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
4393   }
4394 };
4395 
4396 /// Dereferenceable attribute for a return value.
4397 struct AADereferenceableReturned final
4398     : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
4399   AADereferenceableReturned(const IRPosition &IRP, Attributor &A)
4400       : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
4401             IRP, A) {}
4402 
4403   /// See AbstractAttribute::trackStatistics()
4404   void trackStatistics() const override {
4405     STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
4406   }
4407 };
4408 
4409 /// Dereferenceable attribute for an argument
4410 struct AADereferenceableArgument final
4411     : AAArgumentFromCallSiteArguments<AADereferenceable,
4412                                       AADereferenceableImpl> {
4413   using Base =
4414       AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl>;
4415   AADereferenceableArgument(const IRPosition &IRP, Attributor &A)
4416       : Base(IRP, A) {}
4417 
4418   /// See AbstractAttribute::trackStatistics()
4419   void trackStatistics() const override {
4420     STATS_DECLTRACK_ARG_ATTR(dereferenceable)
4421   }
4422 };
4423 
4424 /// Dereferenceable attribute for a call site argument.
4425 struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
4426   AADereferenceableCallSiteArgument(const IRPosition &IRP, Attributor &A)
4427       : AADereferenceableFloating(IRP, A) {}
4428 
4429   /// See AbstractAttribute::trackStatistics()
4430   void trackStatistics() const override {
4431     STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
4432   }
4433 };
4434 
4435 /// Dereferenceable attribute deduction for a call site return value.
4436 struct AADereferenceableCallSiteReturned final
4437     : AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl> {
4438   using Base =
4439       AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl>;
4440   AADereferenceableCallSiteReturned(const IRPosition &IRP, Attributor &A)
4441       : Base(IRP, A) {}
4442 
4443   /// See AbstractAttribute::trackStatistics()
4444   void trackStatistics() const override {
4445     STATS_DECLTRACK_CS_ATTR(dereferenceable);
4446   }
4447 };
4448 } // namespace
4449 
4450 // ------------------------ Align Argument Attribute ------------------------
4451 
4452 namespace {
4453 static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA,
4454                                     Value &AssociatedValue, const Use *U,
4455                                     const Instruction *I, bool &TrackUse) {
4456   // We need to follow common pointer manipulation uses to the accesses they
4457   // feed into.
4458   if (isa<CastInst>(I)) {
4459     // Follow all but ptr2int casts.
4460     TrackUse = !isa<PtrToIntInst>(I);
4461     return 0;
4462   }
4463   if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
4464     if (GEP->hasAllConstantIndices())
4465       TrackUse = true;
4466     return 0;
4467   }
4468 
4469   MaybeAlign MA;
4470   if (const auto *CB = dyn_cast<CallBase>(I)) {
4471     if (CB->isBundleOperand(U) || CB->isCallee(U))
4472       return 0;
4473 
4474     unsigned ArgNo = CB->getArgOperandNo(U);
4475     IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
4476     // As long as we only use known information there is no need to track
4477     // dependences here.
4478     auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, DepClassTy::NONE);
4479     MA = MaybeAlign(AlignAA.getKnownAlign());
4480   }
4481 
4482   const DataLayout &DL = A.getDataLayout();
4483   const Value *UseV = U->get();
4484   if (auto *SI = dyn_cast<StoreInst>(I)) {
4485     if (SI->getPointerOperand() == UseV)
4486       MA = SI->getAlign();
4487   } else if (auto *LI = dyn_cast<LoadInst>(I)) {
4488     if (LI->getPointerOperand() == UseV)
4489       MA = LI->getAlign();
4490   }
4491 
4492   if (!MA || *MA <= QueryingAA.getKnownAlign())
4493     return 0;
4494 
4495   unsigned Alignment = MA->value();
4496   int64_t Offset;
4497 
4498   if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
4499     if (Base == &AssociatedValue) {
4500       // BasePointerAddr + Offset = Alignment * Q for some integer Q.
4501       // So we can say that the maximum power of two which is a divisor of
4502       // gcd(Offset, Alignment) is an alignment.
4503 
4504       uint32_t gcd =
4505           greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
4506       Alignment = llvm::PowerOf2Floor(gcd);
4507     }
4508   }
4509 
4510   return Alignment;
4511 }
4512 
4513 struct AAAlignImpl : AAAlign {
4514   AAAlignImpl(const IRPosition &IRP, Attributor &A) : AAAlign(IRP, A) {}
4515 
4516   /// See AbstractAttribute::initialize(...).
4517   void initialize(Attributor &A) override {
4518     SmallVector<Attribute, 4> Attrs;
4519     getAttrs({Attribute::Alignment}, Attrs);
4520     for (const Attribute &Attr : Attrs)
4521       takeKnownMaximum(Attr.getValueAsInt());
4522 
4523     Value &V = getAssociatedValue();
4524     takeKnownMaximum(V.getPointerAlignment(A.getDataLayout()).value());
4525 
4526     if (getIRPosition().isFnInterfaceKind() &&
4527         (!getAnchorScope() ||
4528          !A.isFunctionIPOAmendable(*getAssociatedFunction()))) {
4529       indicatePessimisticFixpoint();
4530       return;
4531     }
4532 
4533     if (Instruction *CtxI = getCtxI())
4534       followUsesInMBEC(*this, A, getState(), *CtxI);
4535   }
4536 
4537   /// See AbstractAttribute::manifest(...).
4538   ChangeStatus manifest(Attributor &A) override {
4539     ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
4540 
4541     // Check for users that allow alignment annotations.
4542     Value &AssociatedValue = getAssociatedValue();
4543     for (const Use &U : AssociatedValue.uses()) {
4544       if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
4545         if (SI->getPointerOperand() == &AssociatedValue)
4546           if (SI->getAlignment() < getAssumedAlign()) {
4547             STATS_DECLTRACK(AAAlign, Store,
4548                             "Number of times alignment added to a store");
4549             SI->setAlignment(Align(getAssumedAlign()));
4550             LoadStoreChanged = ChangeStatus::CHANGED;
4551           }
4552       } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
4553         if (LI->getPointerOperand() == &AssociatedValue)
4554           if (LI->getAlignment() < getAssumedAlign()) {
4555             LI->setAlignment(Align(getAssumedAlign()));
4556             STATS_DECLTRACK(AAAlign, Load,
4557                             "Number of times alignment added to a load");
4558             LoadStoreChanged = ChangeStatus::CHANGED;
4559           }
4560       }
4561     }
4562 
4563     ChangeStatus Changed = AAAlign::manifest(A);
4564 
4565     Align InheritAlign =
4566         getAssociatedValue().getPointerAlignment(A.getDataLayout());
4567     if (InheritAlign >= getAssumedAlign())
4568       return LoadStoreChanged;
4569     return Changed | LoadStoreChanged;
4570   }
4571 
4572   // TODO: Provide a helper to determine the implied ABI alignment and check in
4573   //       the existing manifest method and a new one for AAAlignImpl that value
4574   //       to avoid making the alignment explicit if it did not improve.
4575 
4576   /// See AbstractAttribute::getDeducedAttributes
4577   virtual void
4578   getDeducedAttributes(LLVMContext &Ctx,
4579                        SmallVectorImpl<Attribute> &Attrs) const override {
4580     if (getAssumedAlign() > 1)
4581       Attrs.emplace_back(
4582           Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
4583   }
4584 
4585   /// See followUsesInMBEC
4586   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
4587                        AAAlign::StateType &State) {
4588     bool TrackUse = false;
4589 
4590     unsigned int KnownAlign =
4591         getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
4592     State.takeKnownMaximum(KnownAlign);
4593 
4594     return TrackUse;
4595   }
4596 
4597   /// See AbstractAttribute::getAsStr().
4598   const std::string getAsStr() const override {
4599     return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
4600                                 "-" + std::to_string(getAssumedAlign()) + ">")
4601                              : "unknown-align";
4602   }
4603 };
4604 
4605 /// Align attribute for a floating value.
4606 struct AAAlignFloating : AAAlignImpl {
4607   AAAlignFloating(const IRPosition &IRP, Attributor &A) : AAAlignImpl(IRP, A) {}
4608 
4609   /// See AbstractAttribute::updateImpl(...).
4610   ChangeStatus updateImpl(Attributor &A) override {
4611     const DataLayout &DL = A.getDataLayout();
4612 
4613     auto VisitValueCB = [&](Value &V, const Instruction *,
4614                             AAAlign::StateType &T, bool Stripped) -> bool {
4615       if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V))
4616         return true;
4617       const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V),
4618                                            DepClassTy::REQUIRED);
4619       if (!Stripped && this == &AA) {
4620         int64_t Offset;
4621         unsigned Alignment = 1;
4622         if (const Value *Base =
4623                 GetPointerBaseWithConstantOffset(&V, Offset, DL)) {
4624           // TODO: Use AAAlign for the base too.
4625           Align PA = Base->getPointerAlignment(DL);
4626           // BasePointerAddr + Offset = Alignment * Q for some integer Q.
4627           // So we can say that the maximum power of two which is a divisor of
4628           // gcd(Offset, Alignment) is an alignment.
4629 
4630           uint32_t gcd = greatestCommonDivisor(uint32_t(abs((int32_t)Offset)),
4631                                                uint32_t(PA.value()));
4632           Alignment = llvm::PowerOf2Floor(gcd);
4633         } else {
4634           Alignment = V.getPointerAlignment(DL).value();
4635         }
4636         // Use only IR information if we did not strip anything.
4637         T.takeKnownMaximum(Alignment);
4638         T.indicatePessimisticFixpoint();
4639       } else {
4640         // Use abstract attribute information.
4641         const AAAlign::StateType &DS = AA.getState();
4642         T ^= DS;
4643       }
4644       return T.isValidState();
4645     };
4646 
4647     StateType T;
4648     bool UsedAssumedInformation = false;
4649     if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
4650                                           VisitValueCB, getCtxI(),
4651                                           UsedAssumedInformation))
4652       return indicatePessimisticFixpoint();
4653 
4654     // TODO: If we know we visited all incoming values, thus no are assumed
4655     // dead, we can take the known information from the state T.
4656     return clampStateAndIndicateChange(getState(), T);
4657   }
4658 
4659   /// See AbstractAttribute::trackStatistics()
4660   void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
4661 };
4662 
4663 /// Align attribute for function return value.
4664 struct AAAlignReturned final
4665     : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
4666   using Base = AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>;
4667   AAAlignReturned(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
4668 
4669   /// See AbstractAttribute::initialize(...).
4670   void initialize(Attributor &A) override {
4671     Base::initialize(A);
4672     Function *F = getAssociatedFunction();
4673     if (!F || F->isDeclaration())
4674       indicatePessimisticFixpoint();
4675   }
4676 
4677   /// See AbstractAttribute::trackStatistics()
4678   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
4679 };
4680 
4681 /// Align attribute for function argument.
4682 struct AAAlignArgument final
4683     : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
4684   using Base = AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>;
4685   AAAlignArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
4686 
4687   /// See AbstractAttribute::manifest(...).
4688   ChangeStatus manifest(Attributor &A) override {
4689     // If the associated argument is involved in a must-tail call we give up
4690     // because we would need to keep the argument alignments of caller and
4691     // callee in-sync. Just does not seem worth the trouble right now.
4692     if (A.getInfoCache().isInvolvedInMustTailCall(*getAssociatedArgument()))
4693       return ChangeStatus::UNCHANGED;
4694     return Base::manifest(A);
4695   }
4696 
4697   /// See AbstractAttribute::trackStatistics()
4698   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
4699 };
4700 
4701 struct AAAlignCallSiteArgument final : AAAlignFloating {
4702   AAAlignCallSiteArgument(const IRPosition &IRP, Attributor &A)
4703       : AAAlignFloating(IRP, A) {}
4704 
4705   /// See AbstractAttribute::manifest(...).
4706   ChangeStatus manifest(Attributor &A) override {
4707     // If the associated argument is involved in a must-tail call we give up
4708     // because we would need to keep the argument alignments of caller and
4709     // callee in-sync. Just does not seem worth the trouble right now.
4710     if (Argument *Arg = getAssociatedArgument())
4711       if (A.getInfoCache().isInvolvedInMustTailCall(*Arg))
4712         return ChangeStatus::UNCHANGED;
4713     ChangeStatus Changed = AAAlignImpl::manifest(A);
4714     Align InheritAlign =
4715         getAssociatedValue().getPointerAlignment(A.getDataLayout());
4716     if (InheritAlign >= getAssumedAlign())
4717       Changed = ChangeStatus::UNCHANGED;
4718     return Changed;
4719   }
4720 
4721   /// See AbstractAttribute::updateImpl(Attributor &A).
4722   ChangeStatus updateImpl(Attributor &A) override {
4723     ChangeStatus Changed = AAAlignFloating::updateImpl(A);
4724     if (Argument *Arg = getAssociatedArgument()) {
4725       // We only take known information from the argument
4726       // so we do not need to track a dependence.
4727       const auto &ArgAlignAA = A.getAAFor<AAAlign>(
4728           *this, IRPosition::argument(*Arg), DepClassTy::NONE);
4729       takeKnownMaximum(ArgAlignAA.getKnownAlign());
4730     }
4731     return Changed;
4732   }
4733 
4734   /// See AbstractAttribute::trackStatistics()
4735   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
4736 };
4737 
4738 /// Align attribute deduction for a call site return value.
4739 struct AAAlignCallSiteReturned final
4740     : AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl> {
4741   using Base = AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl>;
4742   AAAlignCallSiteReturned(const IRPosition &IRP, Attributor &A)
4743       : Base(IRP, A) {}
4744 
4745   /// See AbstractAttribute::initialize(...).
4746   void initialize(Attributor &A) override {
4747     Base::initialize(A);
4748     Function *F = getAssociatedFunction();
4749     if (!F || F->isDeclaration())
4750       indicatePessimisticFixpoint();
4751   }
4752 
4753   /// See AbstractAttribute::trackStatistics()
4754   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
4755 };
4756 } // namespace
4757 
4758 /// ------------------ Function No-Return Attribute ----------------------------
4759 namespace {
4760 struct AANoReturnImpl : public AANoReturn {
4761   AANoReturnImpl(const IRPosition &IRP, Attributor &A) : AANoReturn(IRP, A) {}
4762 
4763   /// See AbstractAttribute::initialize(...).
4764   void initialize(Attributor &A) override {
4765     AANoReturn::initialize(A);
4766     Function *F = getAssociatedFunction();
4767     if (!F || F->isDeclaration())
4768       indicatePessimisticFixpoint();
4769   }
4770 
4771   /// See AbstractAttribute::getAsStr().
4772   const std::string getAsStr() const override {
4773     return getAssumed() ? "noreturn" : "may-return";
4774   }
4775 
4776   /// See AbstractAttribute::updateImpl(Attributor &A).
4777   virtual ChangeStatus updateImpl(Attributor &A) override {
4778     auto CheckForNoReturn = [](Instruction &) { return false; };
4779     bool UsedAssumedInformation = false;
4780     if (!A.checkForAllInstructions(CheckForNoReturn, *this,
4781                                    {(unsigned)Instruction::Ret},
4782                                    UsedAssumedInformation))
4783       return indicatePessimisticFixpoint();
4784     return ChangeStatus::UNCHANGED;
4785   }
4786 };
4787 
4788 struct AANoReturnFunction final : AANoReturnImpl {
4789   AANoReturnFunction(const IRPosition &IRP, Attributor &A)
4790       : AANoReturnImpl(IRP, A) {}
4791 
4792   /// See AbstractAttribute::trackStatistics()
4793   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
4794 };
4795 
4796 /// NoReturn attribute deduction for a call sites.
4797 struct AANoReturnCallSite final : AANoReturnImpl {
4798   AANoReturnCallSite(const IRPosition &IRP, Attributor &A)
4799       : AANoReturnImpl(IRP, A) {}
4800 
4801   /// See AbstractAttribute::initialize(...).
4802   void initialize(Attributor &A) override {
4803     AANoReturnImpl::initialize(A);
4804     if (Function *F = getAssociatedFunction()) {
4805       const IRPosition &FnPos = IRPosition::function(*F);
4806       auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4807       if (!FnAA.isAssumedNoReturn())
4808         indicatePessimisticFixpoint();
4809     }
4810   }
4811 
4812   /// See AbstractAttribute::updateImpl(...).
4813   ChangeStatus updateImpl(Attributor &A) override {
4814     // TODO: Once we have call site specific value information we can provide
4815     //       call site specific liveness information and then it makes
4816     //       sense to specialize attributes for call sites arguments instead of
4817     //       redirecting requests to the callee argument.
4818     Function *F = getAssociatedFunction();
4819     const IRPosition &FnPos = IRPosition::function(*F);
4820     auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4821     return clampStateAndIndicateChange(getState(), FnAA.getState());
4822   }
4823 
4824   /// See AbstractAttribute::trackStatistics()
4825   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
4826 };
4827 } // namespace
4828 
4829 /// ----------------------- Variable Capturing ---------------------------------
4830 
4831 namespace {
4832 /// A class to hold the state of for no-capture attributes.
4833 struct AANoCaptureImpl : public AANoCapture {
4834   AANoCaptureImpl(const IRPosition &IRP, Attributor &A) : AANoCapture(IRP, A) {}
4835 
4836   /// See AbstractAttribute::initialize(...).
4837   void initialize(Attributor &A) override {
4838     if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
4839       indicateOptimisticFixpoint();
4840       return;
4841     }
4842     Function *AnchorScope = getAnchorScope();
4843     if (isFnInterfaceKind() &&
4844         (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) {
4845       indicatePessimisticFixpoint();
4846       return;
4847     }
4848 
4849     // You cannot "capture" null in the default address space.
4850     if (isa<ConstantPointerNull>(getAssociatedValue()) &&
4851         getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
4852       indicateOptimisticFixpoint();
4853       return;
4854     }
4855 
4856     const Function *F =
4857         isArgumentPosition() ? getAssociatedFunction() : AnchorScope;
4858 
4859     // Check what state the associated function can actually capture.
4860     if (F)
4861       determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
4862     else
4863       indicatePessimisticFixpoint();
4864   }
4865 
4866   /// See AbstractAttribute::updateImpl(...).
4867   ChangeStatus updateImpl(Attributor &A) override;
4868 
4869   /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
4870   virtual void
4871   getDeducedAttributes(LLVMContext &Ctx,
4872                        SmallVectorImpl<Attribute> &Attrs) const override {
4873     if (!isAssumedNoCaptureMaybeReturned())
4874       return;
4875 
4876     if (isArgumentPosition()) {
4877       if (isAssumedNoCapture())
4878         Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
4879       else if (ManifestInternal)
4880         Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
4881     }
4882   }
4883 
4884   /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
4885   /// depending on the ability of the function associated with \p IRP to capture
4886   /// state in memory and through "returning/throwing", respectively.
4887   static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
4888                                                    const Function &F,
4889                                                    BitIntegerState &State) {
4890     // TODO: Once we have memory behavior attributes we should use them here.
4891 
4892     // If we know we cannot communicate or write to memory, we do not care about
4893     // ptr2int anymore.
4894     if (F.onlyReadsMemory() && F.doesNotThrow() &&
4895         F.getReturnType()->isVoidTy()) {
4896       State.addKnownBits(NO_CAPTURE);
4897       return;
4898     }
4899 
4900     // A function cannot capture state in memory if it only reads memory, it can
4901     // however return/throw state and the state might be influenced by the
4902     // pointer value, e.g., loading from a returned pointer might reveal a bit.
4903     if (F.onlyReadsMemory())
4904       State.addKnownBits(NOT_CAPTURED_IN_MEM);
4905 
4906     // A function cannot communicate state back if it does not through
4907     // exceptions and doesn not return values.
4908     if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
4909       State.addKnownBits(NOT_CAPTURED_IN_RET);
4910 
4911     // Check existing "returned" attributes.
4912     int ArgNo = IRP.getCalleeArgNo();
4913     if (F.doesNotThrow() && ArgNo >= 0) {
4914       for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
4915         if (F.hasParamAttribute(u, Attribute::Returned)) {
4916           if (u == unsigned(ArgNo))
4917             State.removeAssumedBits(NOT_CAPTURED_IN_RET);
4918           else if (F.onlyReadsMemory())
4919             State.addKnownBits(NO_CAPTURE);
4920           else
4921             State.addKnownBits(NOT_CAPTURED_IN_RET);
4922           break;
4923         }
4924     }
4925   }
4926 
4927   /// See AbstractState::getAsStr().
4928   const std::string getAsStr() const override {
4929     if (isKnownNoCapture())
4930       return "known not-captured";
4931     if (isAssumedNoCapture())
4932       return "assumed not-captured";
4933     if (isKnownNoCaptureMaybeReturned())
4934       return "known not-captured-maybe-returned";
4935     if (isAssumedNoCaptureMaybeReturned())
4936       return "assumed not-captured-maybe-returned";
4937     return "assumed-captured";
4938   }
4939 };
4940 
4941 /// Attributor-aware capture tracker.
4942 struct AACaptureUseTracker final : public CaptureTracker {
4943 
4944   /// Create a capture tracker that can lookup in-flight abstract attributes
4945   /// through the Attributor \p A.
4946   ///
4947   /// If a use leads to a potential capture, \p CapturedInMemory is set and the
4948   /// search is stopped. If a use leads to a return instruction,
4949   /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
4950   /// If a use leads to a ptr2int which may capture the value,
4951   /// \p CapturedInInteger is set. If a use is found that is currently assumed
4952   /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
4953   /// set. All values in \p PotentialCopies are later tracked as well. For every
4954   /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
4955   /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
4956   /// conservatively set to true.
4957   AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
4958                       const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
4959                       SmallSetVector<Value *, 4> &PotentialCopies,
4960                       unsigned &RemainingUsesToExplore)
4961       : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
4962         PotentialCopies(PotentialCopies),
4963         RemainingUsesToExplore(RemainingUsesToExplore) {}
4964 
4965   /// Determine if \p V maybe captured. *Also updates the state!*
4966   bool valueMayBeCaptured(const Value *V) {
4967     if (V->getType()->isPointerTy()) {
4968       PointerMayBeCaptured(V, this);
4969     } else {
4970       State.indicatePessimisticFixpoint();
4971     }
4972     return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4973   }
4974 
4975   /// See CaptureTracker::tooManyUses().
4976   void tooManyUses() override {
4977     State.removeAssumedBits(AANoCapture::NO_CAPTURE);
4978   }
4979 
4980   bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
4981     if (CaptureTracker::isDereferenceableOrNull(O, DL))
4982       return true;
4983     const auto &DerefAA = A.getAAFor<AADereferenceable>(
4984         NoCaptureAA, IRPosition::value(*O), DepClassTy::OPTIONAL);
4985     return DerefAA.getAssumedDereferenceableBytes();
4986   }
4987 
4988   /// See CaptureTracker::captured(...).
4989   bool captured(const Use *U) override {
4990     Instruction *UInst = cast<Instruction>(U->getUser());
4991     LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
4992                       << "\n");
4993 
4994     // Because we may reuse the tracker multiple times we keep track of the
4995     // number of explored uses ourselves as well.
4996     if (RemainingUsesToExplore-- == 0) {
4997       LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
4998       return isCapturedIn(/* Memory */ true, /* Integer */ true,
4999                           /* Return */ true);
5000     }
5001 
5002     // Deal with ptr2int by following uses.
5003     if (isa<PtrToIntInst>(UInst)) {
5004       LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
5005       return valueMayBeCaptured(UInst);
5006     }
5007 
5008     // For stores we check if we can follow the value through memory or not.
5009     if (auto *SI = dyn_cast<StoreInst>(UInst)) {
5010       if (SI->isVolatile())
5011         return isCapturedIn(/* Memory */ true, /* Integer */ false,
5012                             /* Return */ false);
5013       bool UsedAssumedInformation = false;
5014       if (!AA::getPotentialCopiesOfStoredValue(
5015               A, *SI, PotentialCopies, NoCaptureAA, UsedAssumedInformation))
5016         return isCapturedIn(/* Memory */ true, /* Integer */ false,
5017                             /* Return */ false);
5018       // Not captured directly, potential copies will be checked.
5019       return isCapturedIn(/* Memory */ false, /* Integer */ false,
5020                           /* Return */ false);
5021     }
5022 
5023     // Explicitly catch return instructions.
5024     if (isa<ReturnInst>(UInst)) {
5025       if (UInst->getFunction() == NoCaptureAA.getAnchorScope())
5026         return isCapturedIn(/* Memory */ false, /* Integer */ false,
5027                             /* Return */ true);
5028       return isCapturedIn(/* Memory */ true, /* Integer */ true,
5029                           /* Return */ true);
5030     }
5031 
5032     // For now we only use special logic for call sites. However, the tracker
5033     // itself knows about a lot of other non-capturing cases already.
5034     auto *CB = dyn_cast<CallBase>(UInst);
5035     if (!CB || !CB->isArgOperand(U))
5036       return isCapturedIn(/* Memory */ true, /* Integer */ true,
5037                           /* Return */ true);
5038 
5039     unsigned ArgNo = CB->getArgOperandNo(U);
5040     const IRPosition &CSArgPos = IRPosition::callsite_argument(*CB, ArgNo);
5041     // If we have a abstract no-capture attribute for the argument we can use
5042     // it to justify a non-capture attribute here. This allows recursion!
5043     auto &ArgNoCaptureAA =
5044         A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos, DepClassTy::REQUIRED);
5045     if (ArgNoCaptureAA.isAssumedNoCapture())
5046       return isCapturedIn(/* Memory */ false, /* Integer */ false,
5047                           /* Return */ false);
5048     if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
5049       addPotentialCopy(*CB);
5050       return isCapturedIn(/* Memory */ false, /* Integer */ false,
5051                           /* Return */ false);
5052     }
5053 
5054     // Lastly, we could not find a reason no-capture can be assumed so we don't.
5055     return isCapturedIn(/* Memory */ true, /* Integer */ true,
5056                         /* Return */ true);
5057   }
5058 
5059   /// Register \p CS as potential copy of the value we are checking.
5060   void addPotentialCopy(CallBase &CB) { PotentialCopies.insert(&CB); }
5061 
5062   /// See CaptureTracker::shouldExplore(...).
5063   bool shouldExplore(const Use *U) override {
5064     // Check liveness and ignore droppable users.
5065     bool UsedAssumedInformation = false;
5066     return !U->getUser()->isDroppable() &&
5067            !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA,
5068                             UsedAssumedInformation);
5069   }
5070 
5071   /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
5072   /// \p CapturedInRet, then return the appropriate value for use in the
5073   /// CaptureTracker::captured() interface.
5074   bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
5075                     bool CapturedInRet) {
5076     LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
5077                       << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
5078     if (CapturedInMem)
5079       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
5080     if (CapturedInInt)
5081       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
5082     if (CapturedInRet)
5083       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
5084     return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
5085   }
5086 
5087 private:
5088   /// The attributor providing in-flight abstract attributes.
5089   Attributor &A;
5090 
5091   /// The abstract attribute currently updated.
5092   AANoCapture &NoCaptureAA;
5093 
5094   /// The abstract liveness state.
5095   const AAIsDead &IsDeadAA;
5096 
5097   /// The state currently updated.
5098   AANoCapture::StateType &State;
5099 
5100   /// Set of potential copies of the tracked value.
5101   SmallSetVector<Value *, 4> &PotentialCopies;
5102 
5103   /// Global counter to limit the number of explored uses.
5104   unsigned &RemainingUsesToExplore;
5105 };
5106 
5107 ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
5108   const IRPosition &IRP = getIRPosition();
5109   Value *V = isArgumentPosition() ? IRP.getAssociatedArgument()
5110                                   : &IRP.getAssociatedValue();
5111   if (!V)
5112     return indicatePessimisticFixpoint();
5113 
5114   const Function *F =
5115       isArgumentPosition() ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
5116   assert(F && "Expected a function!");
5117   const IRPosition &FnPos = IRPosition::function(*F);
5118   const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos, DepClassTy::NONE);
5119 
5120   AANoCapture::StateType T;
5121 
5122   // Readonly means we cannot capture through memory.
5123   bool IsKnown;
5124   if (AA::isAssumedReadOnly(A, FnPos, *this, IsKnown)) {
5125     T.addKnownBits(NOT_CAPTURED_IN_MEM);
5126     if (IsKnown)
5127       addKnownBits(NOT_CAPTURED_IN_MEM);
5128   }
5129 
5130   // Make sure all returned values are different than the underlying value.
5131   // TODO: we could do this in a more sophisticated way inside
5132   //       AAReturnedValues, e.g., track all values that escape through returns
5133   //       directly somehow.
5134   auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
5135     bool SeenConstant = false;
5136     for (auto &It : RVAA.returned_values()) {
5137       if (isa<Constant>(It.first)) {
5138         if (SeenConstant)
5139           return false;
5140         SeenConstant = true;
5141       } else if (!isa<Argument>(It.first) ||
5142                  It.first == getAssociatedArgument())
5143         return false;
5144     }
5145     return true;
5146   };
5147 
5148   const auto &NoUnwindAA =
5149       A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::OPTIONAL);
5150   if (NoUnwindAA.isAssumedNoUnwind()) {
5151     bool IsVoidTy = F->getReturnType()->isVoidTy();
5152     const AAReturnedValues *RVAA =
5153         IsVoidTy ? nullptr
5154                  : &A.getAAFor<AAReturnedValues>(*this, FnPos,
5155 
5156                                                  DepClassTy::OPTIONAL);
5157     if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
5158       T.addKnownBits(NOT_CAPTURED_IN_RET);
5159       if (T.isKnown(NOT_CAPTURED_IN_MEM))
5160         return ChangeStatus::UNCHANGED;
5161       if (NoUnwindAA.isKnownNoUnwind() &&
5162           (IsVoidTy || RVAA->getState().isAtFixpoint())) {
5163         addKnownBits(NOT_CAPTURED_IN_RET);
5164         if (isKnown(NOT_CAPTURED_IN_MEM))
5165           return indicateOptimisticFixpoint();
5166       }
5167     }
5168   }
5169 
5170   // Use the CaptureTracker interface and logic with the specialized tracker,
5171   // defined in AACaptureUseTracker, that can look at in-flight abstract
5172   // attributes and directly updates the assumed state.
5173   SmallSetVector<Value *, 4> PotentialCopies;
5174   unsigned RemainingUsesToExplore =
5175       getDefaultMaxUsesToExploreForCaptureTracking();
5176   AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
5177                               RemainingUsesToExplore);
5178 
5179   // Check all potential copies of the associated value until we can assume
5180   // none will be captured or we have to assume at least one might be.
5181   unsigned Idx = 0;
5182   PotentialCopies.insert(V);
5183   while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
5184     Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
5185 
5186   AANoCapture::StateType &S = getState();
5187   auto Assumed = S.getAssumed();
5188   S.intersectAssumedBits(T.getAssumed());
5189   if (!isAssumedNoCaptureMaybeReturned())
5190     return indicatePessimisticFixpoint();
5191   return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
5192                                    : ChangeStatus::CHANGED;
5193 }
5194 
5195 /// NoCapture attribute for function arguments.
5196 struct AANoCaptureArgument final : AANoCaptureImpl {
5197   AANoCaptureArgument(const IRPosition &IRP, Attributor &A)
5198       : AANoCaptureImpl(IRP, A) {}
5199 
5200   /// See AbstractAttribute::trackStatistics()
5201   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
5202 };
5203 
5204 /// NoCapture attribute for call site arguments.
5205 struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
5206   AANoCaptureCallSiteArgument(const IRPosition &IRP, Attributor &A)
5207       : AANoCaptureImpl(IRP, A) {}
5208 
5209   /// See AbstractAttribute::initialize(...).
5210   void initialize(Attributor &A) override {
5211     if (Argument *Arg = getAssociatedArgument())
5212       if (Arg->hasByValAttr())
5213         indicateOptimisticFixpoint();
5214     AANoCaptureImpl::initialize(A);
5215   }
5216 
5217   /// See AbstractAttribute::updateImpl(...).
5218   ChangeStatus updateImpl(Attributor &A) override {
5219     // TODO: Once we have call site specific value information we can provide
5220     //       call site specific liveness information and then it makes
5221     //       sense to specialize attributes for call sites arguments instead of
5222     //       redirecting requests to the callee argument.
5223     Argument *Arg = getAssociatedArgument();
5224     if (!Arg)
5225       return indicatePessimisticFixpoint();
5226     const IRPosition &ArgPos = IRPosition::argument(*Arg);
5227     auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos, DepClassTy::REQUIRED);
5228     return clampStateAndIndicateChange(getState(), ArgAA.getState());
5229   }
5230 
5231   /// See AbstractAttribute::trackStatistics()
5232   void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
5233 };
5234 
5235 /// NoCapture attribute for floating values.
5236 struct AANoCaptureFloating final : AANoCaptureImpl {
5237   AANoCaptureFloating(const IRPosition &IRP, Attributor &A)
5238       : AANoCaptureImpl(IRP, A) {}
5239 
5240   /// See AbstractAttribute::trackStatistics()
5241   void trackStatistics() const override {
5242     STATS_DECLTRACK_FLOATING_ATTR(nocapture)
5243   }
5244 };
5245 
5246 /// NoCapture attribute for function return value.
5247 struct AANoCaptureReturned final : AANoCaptureImpl {
5248   AANoCaptureReturned(const IRPosition &IRP, Attributor &A)
5249       : AANoCaptureImpl(IRP, A) {
5250     llvm_unreachable("NoCapture is not applicable to function returns!");
5251   }
5252 
5253   /// See AbstractAttribute::initialize(...).
5254   void initialize(Attributor &A) override {
5255     llvm_unreachable("NoCapture is not applicable to function returns!");
5256   }
5257 
5258   /// See AbstractAttribute::updateImpl(...).
5259   ChangeStatus updateImpl(Attributor &A) override {
5260     llvm_unreachable("NoCapture is not applicable to function returns!");
5261   }
5262 
5263   /// See AbstractAttribute::trackStatistics()
5264   void trackStatistics() const override {}
5265 };
5266 
5267 /// NoCapture attribute deduction for a call site return value.
5268 struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
5269   AANoCaptureCallSiteReturned(const IRPosition &IRP, Attributor &A)
5270       : AANoCaptureImpl(IRP, A) {}
5271 
5272   /// See AbstractAttribute::initialize(...).
5273   void initialize(Attributor &A) override {
5274     const Function *F = getAnchorScope();
5275     // Check what state the associated function can actually capture.
5276     determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
5277   }
5278 
5279   /// See AbstractAttribute::trackStatistics()
5280   void trackStatistics() const override {
5281     STATS_DECLTRACK_CSRET_ATTR(nocapture)
5282   }
5283 };
5284 } // namespace
5285 
5286 /// ------------------ Value Simplify Attribute ----------------------------
5287 
5288 bool ValueSimplifyStateType::unionAssumed(Optional<Value *> Other) {
5289   // FIXME: Add a typecast support.
5290   SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5291       SimplifiedAssociatedValue, Other, Ty);
5292   if (SimplifiedAssociatedValue == Optional<Value *>(nullptr))
5293     return false;
5294 
5295   LLVM_DEBUG({
5296     if (SimplifiedAssociatedValue.hasValue())
5297       dbgs() << "[ValueSimplify] is assumed to be "
5298              << **SimplifiedAssociatedValue << "\n";
5299     else
5300       dbgs() << "[ValueSimplify] is assumed to be <none>\n";
5301   });
5302   return true;
5303 }
5304 
5305 namespace {
5306 struct AAValueSimplifyImpl : AAValueSimplify {
5307   AAValueSimplifyImpl(const IRPosition &IRP, Attributor &A)
5308       : AAValueSimplify(IRP, A) {}
5309 
5310   /// See AbstractAttribute::initialize(...).
5311   void initialize(Attributor &A) override {
5312     if (getAssociatedValue().getType()->isVoidTy())
5313       indicatePessimisticFixpoint();
5314     if (A.hasSimplificationCallback(getIRPosition()))
5315       indicatePessimisticFixpoint();
5316   }
5317 
5318   /// See AbstractAttribute::getAsStr().
5319   const std::string getAsStr() const override {
5320     LLVM_DEBUG({
5321       errs() << "SAV: " << SimplifiedAssociatedValue << " ";
5322       if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue)
5323         errs() << "SAV: " << **SimplifiedAssociatedValue << " ";
5324     });
5325     return isValidState() ? (isAtFixpoint() ? "simplified" : "maybe-simple")
5326                           : "not-simple";
5327   }
5328 
5329   /// See AbstractAttribute::trackStatistics()
5330   void trackStatistics() const override {}
5331 
5332   /// See AAValueSimplify::getAssumedSimplifiedValue()
5333   Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
5334     return SimplifiedAssociatedValue;
5335   }
5336 
5337   /// Return a value we can use as replacement for the associated one, or
5338   /// nullptr if we don't have one that makes sense.
5339   Value *getReplacementValue(Attributor &A) const {
5340     Value *NewV;
5341     NewV = SimplifiedAssociatedValue.hasValue()
5342                ? SimplifiedAssociatedValue.getValue()
5343                : UndefValue::get(getAssociatedType());
5344     if (!NewV)
5345       return nullptr;
5346     NewV = AA::getWithType(*NewV, *getAssociatedType());
5347     if (!NewV || NewV == &getAssociatedValue())
5348       return nullptr;
5349     const Instruction *CtxI = getCtxI();
5350     if (CtxI && !AA::isValidAtPosition(*NewV, *CtxI, A.getInfoCache()))
5351       return nullptr;
5352     if (!CtxI && !AA::isValidInScope(*NewV, getAnchorScope()))
5353       return nullptr;
5354     return NewV;
5355   }
5356 
5357   /// Helper function for querying AAValueSimplify and updating candicate.
5358   /// \param IRP The value position we are trying to unify with SimplifiedValue
5359   bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
5360                       const IRPosition &IRP, bool Simplify = true) {
5361     bool UsedAssumedInformation = false;
5362     Optional<Value *> QueryingValueSimplified = &IRP.getAssociatedValue();
5363     if (Simplify)
5364       QueryingValueSimplified =
5365           A.getAssumedSimplified(IRP, QueryingAA, UsedAssumedInformation);
5366     return unionAssumed(QueryingValueSimplified);
5367   }
5368 
5369   /// Returns a candidate is found or not
5370   template <typename AAType> bool askSimplifiedValueFor(Attributor &A) {
5371     if (!getAssociatedValue().getType()->isIntegerTy())
5372       return false;
5373 
5374     // This will also pass the call base context.
5375     const auto &AA =
5376         A.getAAFor<AAType>(*this, getIRPosition(), DepClassTy::NONE);
5377 
5378     Optional<ConstantInt *> COpt = AA.getAssumedConstantInt(A);
5379 
5380     if (!COpt.hasValue()) {
5381       SimplifiedAssociatedValue = llvm::None;
5382       A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
5383       return true;
5384     }
5385     if (auto *C = COpt.getValue()) {
5386       SimplifiedAssociatedValue = C;
5387       A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
5388       return true;
5389     }
5390     return false;
5391   }
5392 
5393   bool askSimplifiedValueForOtherAAs(Attributor &A) {
5394     if (askSimplifiedValueFor<AAValueConstantRange>(A))
5395       return true;
5396     if (askSimplifiedValueFor<AAPotentialValues>(A))
5397       return true;
5398     return false;
5399   }
5400 
5401   /// See AbstractAttribute::manifest(...).
5402   ChangeStatus manifest(Attributor &A) override {
5403     ChangeStatus Changed = ChangeStatus::UNCHANGED;
5404     if (getAssociatedValue().user_empty())
5405       return Changed;
5406 
5407     if (auto *NewV = getReplacementValue(A)) {
5408       LLVM_DEBUG(dbgs() << "[ValueSimplify] " << getAssociatedValue() << " -> "
5409                         << *NewV << " :: " << *this << "\n");
5410       if (A.changeValueAfterManifest(getAssociatedValue(), *NewV))
5411         Changed = ChangeStatus::CHANGED;
5412     }
5413 
5414     return Changed | AAValueSimplify::manifest(A);
5415   }
5416 
5417   /// See AbstractState::indicatePessimisticFixpoint(...).
5418   ChangeStatus indicatePessimisticFixpoint() override {
5419     SimplifiedAssociatedValue = &getAssociatedValue();
5420     return AAValueSimplify::indicatePessimisticFixpoint();
5421   }
5422 
5423   static bool handleLoad(Attributor &A, const AbstractAttribute &AA,
5424                          LoadInst &L, function_ref<bool(Value &)> Union) {
5425     auto UnionWrapper = [&](Value &V, Value &Obj) {
5426       if (isa<AllocaInst>(Obj))
5427         return Union(V);
5428       if (!AA::isDynamicallyUnique(A, AA, V))
5429         return false;
5430       if (!AA::isValidAtPosition(V, L, A.getInfoCache()))
5431         return false;
5432       return Union(V);
5433     };
5434 
5435     Value &Ptr = *L.getPointerOperand();
5436     SmallVector<Value *, 8> Objects;
5437     bool UsedAssumedInformation = false;
5438     if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, AA, &L,
5439                                          UsedAssumedInformation))
5440       return false;
5441 
5442     const auto *TLI =
5443         A.getInfoCache().getTargetLibraryInfoForFunction(*L.getFunction());
5444     for (Value *Obj : Objects) {
5445       LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n");
5446       if (isa<UndefValue>(Obj))
5447         continue;
5448       if (isa<ConstantPointerNull>(Obj)) {
5449         // A null pointer access can be undefined but any offset from null may
5450         // be OK. We do not try to optimize the latter.
5451         if (!NullPointerIsDefined(L.getFunction(),
5452                                   Ptr.getType()->getPointerAddressSpace()) &&
5453             A.getAssumedSimplified(Ptr, AA, UsedAssumedInformation) == Obj)
5454           continue;
5455         return false;
5456       }
5457       Constant *InitialVal = AA::getInitialValueForObj(*Obj, *L.getType(), TLI);
5458       if (!InitialVal || !Union(*InitialVal))
5459         return false;
5460 
5461       LLVM_DEBUG(dbgs() << "Underlying object amenable to load-store "
5462                            "propagation, checking accesses next.\n");
5463 
5464       auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
5465         LLVM_DEBUG(dbgs() << " - visit access " << Acc << "\n");
5466         if (Acc.isWrittenValueYetUndetermined())
5467           return true;
5468         Value *Content = Acc.getWrittenValue();
5469         if (!Content)
5470           return false;
5471         Value *CastedContent =
5472             AA::getWithType(*Content, *AA.getAssociatedType());
5473         if (!CastedContent)
5474           return false;
5475         if (IsExact)
5476           return UnionWrapper(*CastedContent, *Obj);
5477         if (auto *C = dyn_cast<Constant>(CastedContent))
5478           if (C->isNullValue() || C->isAllOnesValue() || isa<UndefValue>(C))
5479             return UnionWrapper(*CastedContent, *Obj);
5480         return false;
5481       };
5482 
5483       auto &PI = A.getAAFor<AAPointerInfo>(AA, IRPosition::value(*Obj),
5484                                            DepClassTy::REQUIRED);
5485       if (!PI.forallInterferingAccesses(A, AA, L, CheckAccess))
5486         return false;
5487     }
5488     return true;
5489   }
5490 };
5491 
5492 struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
5493   AAValueSimplifyArgument(const IRPosition &IRP, Attributor &A)
5494       : AAValueSimplifyImpl(IRP, A) {}
5495 
5496   void initialize(Attributor &A) override {
5497     AAValueSimplifyImpl::initialize(A);
5498     if (!getAnchorScope() || getAnchorScope()->isDeclaration())
5499       indicatePessimisticFixpoint();
5500     if (hasAttr({Attribute::InAlloca, Attribute::Preallocated,
5501                  Attribute::StructRet, Attribute::Nest, Attribute::ByVal},
5502                 /* IgnoreSubsumingPositions */ true))
5503       indicatePessimisticFixpoint();
5504   }
5505 
5506   /// See AbstractAttribute::updateImpl(...).
5507   ChangeStatus updateImpl(Attributor &A) override {
5508     // Byval is only replacable if it is readonly otherwise we would write into
5509     // the replaced value and not the copy that byval creates implicitly.
5510     Argument *Arg = getAssociatedArgument();
5511     if (Arg->hasByValAttr()) {
5512       // TODO: We probably need to verify synchronization is not an issue, e.g.,
5513       //       there is no race by not copying a constant byval.
5514       bool IsKnown;
5515       if (!AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
5516         return indicatePessimisticFixpoint();
5517     }
5518 
5519     auto Before = SimplifiedAssociatedValue;
5520 
5521     auto PredForCallSite = [&](AbstractCallSite ACS) {
5522       const IRPosition &ACSArgPos =
5523           IRPosition::callsite_argument(ACS, getCallSiteArgNo());
5524       // Check if a coresponding argument was found or if it is on not
5525       // associated (which can happen for callback calls).
5526       if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
5527         return false;
5528 
5529       // Simplify the argument operand explicitly and check if the result is
5530       // valid in the current scope. This avoids refering to simplified values
5531       // in other functions, e.g., we don't want to say a an argument in a
5532       // static function is actually an argument in a different function.
5533       bool UsedAssumedInformation = false;
5534       Optional<Constant *> SimpleArgOp =
5535           A.getAssumedConstant(ACSArgPos, *this, UsedAssumedInformation);
5536       if (!SimpleArgOp.hasValue())
5537         return true;
5538       if (!SimpleArgOp.getValue())
5539         return false;
5540       if (!AA::isDynamicallyUnique(A, *this, **SimpleArgOp))
5541         return false;
5542       return unionAssumed(*SimpleArgOp);
5543     };
5544 
5545     // Generate a answer specific to a call site context.
5546     bool Success;
5547     bool UsedAssumedInformation = false;
5548     if (hasCallBaseContext() &&
5549         getCallBaseContext()->getCalledFunction() == Arg->getParent())
5550       Success = PredForCallSite(
5551           AbstractCallSite(&getCallBaseContext()->getCalledOperandUse()));
5552     else
5553       Success = A.checkForAllCallSites(PredForCallSite, *this, true,
5554                                        UsedAssumedInformation);
5555 
5556     if (!Success)
5557       if (!askSimplifiedValueForOtherAAs(A))
5558         return indicatePessimisticFixpoint();
5559 
5560     // If a candicate was found in this update, return CHANGED.
5561     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5562                                                : ChangeStatus ::CHANGED;
5563   }
5564 
5565   /// See AbstractAttribute::trackStatistics()
5566   void trackStatistics() const override {
5567     STATS_DECLTRACK_ARG_ATTR(value_simplify)
5568   }
5569 };
5570 
5571 struct AAValueSimplifyReturned : AAValueSimplifyImpl {
5572   AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A)
5573       : AAValueSimplifyImpl(IRP, A) {}
5574 
5575   /// See AAValueSimplify::getAssumedSimplifiedValue()
5576   Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
5577     if (!isValidState())
5578       return nullptr;
5579     return SimplifiedAssociatedValue;
5580   }
5581 
5582   /// See AbstractAttribute::updateImpl(...).
5583   ChangeStatus updateImpl(Attributor &A) override {
5584     auto Before = SimplifiedAssociatedValue;
5585 
5586     auto PredForReturned = [&](Value &V) {
5587       return checkAndUpdate(A, *this,
5588                             IRPosition::value(V, getCallBaseContext()));
5589     };
5590 
5591     if (!A.checkForAllReturnedValues(PredForReturned, *this))
5592       if (!askSimplifiedValueForOtherAAs(A))
5593         return indicatePessimisticFixpoint();
5594 
5595     // If a candicate was found in this update, return CHANGED.
5596     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5597                                                : ChangeStatus ::CHANGED;
5598   }
5599 
5600   ChangeStatus manifest(Attributor &A) override {
5601     ChangeStatus Changed = ChangeStatus::UNCHANGED;
5602     if (!A.isRunOn(*getAnchorScope()))
5603       return Changed;
5604 
5605     assert(!hasCallBaseContext() && "Should never manifest a simplified "
5606                                     "function return with call base context!");
5607 
5608     if (auto *NewV = getReplacementValue(A)) {
5609       auto PredForReturned =
5610           [&](Value &, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
5611             for (ReturnInst *RI : RetInsts) {
5612               Value *ReturnedVal = RI->getReturnValue();
5613               if (ReturnedVal == NewV || isa<UndefValue>(ReturnedVal))
5614                 return true;
5615               assert(RI->getFunction() == getAnchorScope() &&
5616                      "ReturnInst in wrong function!");
5617               LLVM_DEBUG(dbgs()
5618                          << "[ValueSimplify] " << *ReturnedVal << " -> "
5619                          << *NewV << " in " << *RI << " :: " << *this << "\n");
5620               if (A.changeUseAfterManifest(RI->getOperandUse(0), *NewV))
5621                 Changed = ChangeStatus::CHANGED;
5622             }
5623             return true;
5624           };
5625       A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this);
5626     }
5627 
5628     return Changed | AAValueSimplify::manifest(A);
5629   }
5630 
5631   /// See AbstractAttribute::trackStatistics()
5632   void trackStatistics() const override {
5633     STATS_DECLTRACK_FNRET_ATTR(value_simplify)
5634   }
5635 };
5636 
5637 struct AAValueSimplifyFloating : AAValueSimplifyImpl {
5638   AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A)
5639       : AAValueSimplifyImpl(IRP, A) {}
5640 
5641   /// See AbstractAttribute::initialize(...).
5642   void initialize(Attributor &A) override {
5643     AAValueSimplifyImpl::initialize(A);
5644     Value &V = getAnchorValue();
5645 
5646     // TODO: add other stuffs
5647     if (isa<Constant>(V))
5648       indicatePessimisticFixpoint();
5649   }
5650 
5651   /// Check if \p Cmp is a comparison we can simplify.
5652   ///
5653   /// We handle multiple cases, one in which at least one operand is an
5654   /// (assumed) nullptr. If so, try to simplify it using AANonNull on the other
5655   /// operand. Return true if successful, in that case SimplifiedAssociatedValue
5656   /// will be updated.
5657   bool handleCmp(Attributor &A, CmpInst &Cmp) {
5658     auto Union = [&](Value &V) {
5659       SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5660           SimplifiedAssociatedValue, &V, V.getType());
5661       return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5662     };
5663 
5664     Value *LHS = Cmp.getOperand(0);
5665     Value *RHS = Cmp.getOperand(1);
5666 
5667     // Simplify the operands first.
5668     bool UsedAssumedInformation = false;
5669     const auto &SimplifiedLHS =
5670         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
5671                                *this, UsedAssumedInformation);
5672     if (!SimplifiedLHS.hasValue())
5673       return true;
5674     if (!SimplifiedLHS.getValue())
5675       return false;
5676     LHS = *SimplifiedLHS;
5677 
5678     const auto &SimplifiedRHS =
5679         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
5680                                *this, UsedAssumedInformation);
5681     if (!SimplifiedRHS.hasValue())
5682       return true;
5683     if (!SimplifiedRHS.getValue())
5684       return false;
5685     RHS = *SimplifiedRHS;
5686 
5687     LLVMContext &Ctx = Cmp.getContext();
5688     // Handle the trivial case first in which we don't even need to think about
5689     // null or non-null.
5690     if (LHS == RHS && (Cmp.isTrueWhenEqual() || Cmp.isFalseWhenEqual())) {
5691       Constant *NewVal =
5692           ConstantInt::get(Type::getInt1Ty(Ctx), Cmp.isTrueWhenEqual());
5693       if (!Union(*NewVal))
5694         return false;
5695       if (!UsedAssumedInformation)
5696         indicateOptimisticFixpoint();
5697       return true;
5698     }
5699 
5700     // From now on we only handle equalities (==, !=).
5701     ICmpInst *ICmp = dyn_cast<ICmpInst>(&Cmp);
5702     if (!ICmp || !ICmp->isEquality())
5703       return false;
5704 
5705     bool LHSIsNull = isa<ConstantPointerNull>(LHS);
5706     bool RHSIsNull = isa<ConstantPointerNull>(RHS);
5707     if (!LHSIsNull && !RHSIsNull)
5708       return false;
5709 
5710     // Left is the nullptr ==/!= non-nullptr case. We'll use AANonNull on the
5711     // non-nullptr operand and if we assume it's non-null we can conclude the
5712     // result of the comparison.
5713     assert((LHSIsNull || RHSIsNull) &&
5714            "Expected nullptr versus non-nullptr comparison at this point");
5715 
5716     // The index is the operand that we assume is not null.
5717     unsigned PtrIdx = LHSIsNull;
5718     auto &PtrNonNullAA = A.getAAFor<AANonNull>(
5719         *this, IRPosition::value(*ICmp->getOperand(PtrIdx)),
5720         DepClassTy::REQUIRED);
5721     if (!PtrNonNullAA.isAssumedNonNull())
5722       return false;
5723     UsedAssumedInformation |= !PtrNonNullAA.isKnownNonNull();
5724 
5725     // The new value depends on the predicate, true for != and false for ==.
5726     Constant *NewVal = ConstantInt::get(
5727         Type::getInt1Ty(Ctx), ICmp->getPredicate() == CmpInst::ICMP_NE);
5728     if (!Union(*NewVal))
5729       return false;
5730 
5731     if (!UsedAssumedInformation)
5732       indicateOptimisticFixpoint();
5733 
5734     return true;
5735   }
5736 
5737   bool updateWithLoad(Attributor &A, LoadInst &L) {
5738     auto Union = [&](Value &V) {
5739       SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5740           SimplifiedAssociatedValue, &V, L.getType());
5741       return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5742     };
5743     return handleLoad(A, *this, L, Union);
5744   }
5745 
5746   /// Use the generic, non-optimistic InstSimplfy functionality if we managed to
5747   /// simplify any operand of the instruction \p I. Return true if successful,
5748   /// in that case SimplifiedAssociatedValue will be updated.
5749   bool handleGenericInst(Attributor &A, Instruction &I) {
5750     bool SomeSimplified = false;
5751     bool UsedAssumedInformation = false;
5752 
5753     SmallVector<Value *, 8> NewOps(I.getNumOperands());
5754     int Idx = 0;
5755     for (Value *Op : I.operands()) {
5756       const auto &SimplifiedOp =
5757           A.getAssumedSimplified(IRPosition::value(*Op, getCallBaseContext()),
5758                                  *this, UsedAssumedInformation);
5759       // If we are not sure about any operand we are not sure about the entire
5760       // instruction, we'll wait.
5761       if (!SimplifiedOp.hasValue())
5762         return true;
5763 
5764       if (SimplifiedOp.getValue())
5765         NewOps[Idx] = SimplifiedOp.getValue();
5766       else
5767         NewOps[Idx] = Op;
5768 
5769       SomeSimplified |= (NewOps[Idx] != Op);
5770       ++Idx;
5771     }
5772 
5773     // We won't bother with the InstSimplify interface if we didn't simplify any
5774     // operand ourselves.
5775     if (!SomeSimplified)
5776       return false;
5777 
5778     InformationCache &InfoCache = A.getInfoCache();
5779     Function *F = I.getFunction();
5780     const auto *DT =
5781         InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
5782     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5783     auto *AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
5784     OptimizationRemarkEmitter *ORE = nullptr;
5785 
5786     const DataLayout &DL = I.getModule()->getDataLayout();
5787     SimplifyQuery Q(DL, TLI, DT, AC, &I);
5788     if (Value *SimplifiedI =
5789             SimplifyInstructionWithOperands(&I, NewOps, Q, ORE)) {
5790       SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5791           SimplifiedAssociatedValue, SimplifiedI, I.getType());
5792       return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5793     }
5794     return false;
5795   }
5796 
5797   /// See AbstractAttribute::updateImpl(...).
5798   ChangeStatus updateImpl(Attributor &A) override {
5799     auto Before = SimplifiedAssociatedValue;
5800 
5801     auto VisitValueCB = [&](Value &V, const Instruction *CtxI, bool &,
5802                             bool Stripped) -> bool {
5803       auto &AA = A.getAAFor<AAValueSimplify>(
5804           *this, IRPosition::value(V, getCallBaseContext()),
5805           DepClassTy::REQUIRED);
5806       if (!Stripped && this == &AA) {
5807 
5808         if (auto *I = dyn_cast<Instruction>(&V)) {
5809           if (auto *LI = dyn_cast<LoadInst>(&V))
5810             if (updateWithLoad(A, *LI))
5811               return true;
5812           if (auto *Cmp = dyn_cast<CmpInst>(&V))
5813             if (handleCmp(A, *Cmp))
5814               return true;
5815           if (handleGenericInst(A, *I))
5816             return true;
5817         }
5818         // TODO: Look the instruction and check recursively.
5819 
5820         LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V
5821                           << "\n");
5822         return false;
5823       }
5824       return checkAndUpdate(A, *this,
5825                             IRPosition::value(V, getCallBaseContext()));
5826     };
5827 
5828     bool Dummy = false;
5829     bool UsedAssumedInformation = false;
5830     if (!genericValueTraversal<bool>(A, getIRPosition(), *this, Dummy,
5831                                      VisitValueCB, getCtxI(),
5832                                      UsedAssumedInformation,
5833                                      /* UseValueSimplify */ false))
5834       if (!askSimplifiedValueForOtherAAs(A))
5835         return indicatePessimisticFixpoint();
5836 
5837     // If a candicate was found in this update, return CHANGED.
5838     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5839                                                : ChangeStatus ::CHANGED;
5840   }
5841 
5842   /// See AbstractAttribute::trackStatistics()
5843   void trackStatistics() const override {
5844     STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
5845   }
5846 };
5847 
5848 struct AAValueSimplifyFunction : AAValueSimplifyImpl {
5849   AAValueSimplifyFunction(const IRPosition &IRP, Attributor &A)
5850       : AAValueSimplifyImpl(IRP, A) {}
5851 
5852   /// See AbstractAttribute::initialize(...).
5853   void initialize(Attributor &A) override {
5854     SimplifiedAssociatedValue = nullptr;
5855     indicateOptimisticFixpoint();
5856   }
5857   /// See AbstractAttribute::initialize(...).
5858   ChangeStatus updateImpl(Attributor &A) override {
5859     llvm_unreachable(
5860         "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
5861   }
5862   /// See AbstractAttribute::trackStatistics()
5863   void trackStatistics() const override {
5864     STATS_DECLTRACK_FN_ATTR(value_simplify)
5865   }
5866 };
5867 
5868 struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
5869   AAValueSimplifyCallSite(const IRPosition &IRP, Attributor &A)
5870       : AAValueSimplifyFunction(IRP, A) {}
5871   /// See AbstractAttribute::trackStatistics()
5872   void trackStatistics() const override {
5873     STATS_DECLTRACK_CS_ATTR(value_simplify)
5874   }
5875 };
5876 
5877 struct AAValueSimplifyCallSiteReturned : AAValueSimplifyImpl {
5878   AAValueSimplifyCallSiteReturned(const IRPosition &IRP, Attributor &A)
5879       : AAValueSimplifyImpl(IRP, A) {}
5880 
5881   void initialize(Attributor &A) override {
5882     AAValueSimplifyImpl::initialize(A);
5883     if (!getAssociatedFunction())
5884       indicatePessimisticFixpoint();
5885   }
5886 
5887   /// See AbstractAttribute::updateImpl(...).
5888   ChangeStatus updateImpl(Attributor &A) override {
5889     auto Before = SimplifiedAssociatedValue;
5890     auto &RetAA = A.getAAFor<AAReturnedValues>(
5891         *this, IRPosition::function(*getAssociatedFunction()),
5892         DepClassTy::REQUIRED);
5893     auto PredForReturned =
5894         [&](Value &RetVal, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
5895           bool UsedAssumedInformation = false;
5896           Optional<Value *> CSRetVal = A.translateArgumentToCallSiteContent(
5897               &RetVal, *cast<CallBase>(getCtxI()), *this,
5898               UsedAssumedInformation);
5899           SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5900               SimplifiedAssociatedValue, CSRetVal, getAssociatedType());
5901           return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5902         };
5903     if (!RetAA.checkForAllReturnedValuesAndReturnInsts(PredForReturned))
5904       if (!askSimplifiedValueForOtherAAs(A))
5905         return indicatePessimisticFixpoint();
5906     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5907                                                : ChangeStatus ::CHANGED;
5908   }
5909 
5910   void trackStatistics() const override {
5911     STATS_DECLTRACK_CSRET_ATTR(value_simplify)
5912   }
5913 };
5914 
5915 struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
5916   AAValueSimplifyCallSiteArgument(const IRPosition &IRP, Attributor &A)
5917       : AAValueSimplifyFloating(IRP, A) {}
5918 
5919   /// See AbstractAttribute::manifest(...).
5920   ChangeStatus manifest(Attributor &A) override {
5921     ChangeStatus Changed = ChangeStatus::UNCHANGED;
5922 
5923     if (auto *NewV = getReplacementValue(A)) {
5924       Use &U = cast<CallBase>(&getAnchorValue())
5925                    ->getArgOperandUse(getCallSiteArgNo());
5926       if (A.changeUseAfterManifest(U, *NewV))
5927         Changed = ChangeStatus::CHANGED;
5928     }
5929 
5930     return Changed | AAValueSimplify::manifest(A);
5931   }
5932 
5933   void trackStatistics() const override {
5934     STATS_DECLTRACK_CSARG_ATTR(value_simplify)
5935   }
5936 };
5937 } // namespace
5938 
5939 /// ----------------------- Heap-To-Stack Conversion ---------------------------
5940 namespace {
5941 struct AAHeapToStackFunction final : public AAHeapToStack {
5942 
5943   struct AllocationInfo {
5944     /// The call that allocates the memory.
5945     CallBase *const CB;
5946 
5947     /// The library function id for the allocation.
5948     LibFunc LibraryFunctionId = NotLibFunc;
5949 
5950     /// The status wrt. a rewrite.
5951     enum {
5952       STACK_DUE_TO_USE,
5953       STACK_DUE_TO_FREE,
5954       INVALID,
5955     } Status = STACK_DUE_TO_USE;
5956 
5957     /// Flag to indicate if we encountered a use that might free this allocation
5958     /// but which is not in the deallocation infos.
5959     bool HasPotentiallyFreeingUnknownUses = false;
5960 
5961     /// The set of free calls that use this allocation.
5962     SmallPtrSet<CallBase *, 1> PotentialFreeCalls{};
5963   };
5964 
5965   struct DeallocationInfo {
5966     /// The call that deallocates the memory.
5967     CallBase *const CB;
5968 
5969     /// Flag to indicate if we don't know all objects this deallocation might
5970     /// free.
5971     bool MightFreeUnknownObjects = false;
5972 
5973     /// The set of allocation calls that are potentially freed.
5974     SmallPtrSet<CallBase *, 1> PotentialAllocationCalls{};
5975   };
5976 
5977   AAHeapToStackFunction(const IRPosition &IRP, Attributor &A)
5978       : AAHeapToStack(IRP, A) {}
5979 
5980   ~AAHeapToStackFunction() {
5981     // Ensure we call the destructor so we release any memory allocated in the
5982     // sets.
5983     for (auto &It : AllocationInfos)
5984       It.getSecond()->~AllocationInfo();
5985     for (auto &It : DeallocationInfos)
5986       It.getSecond()->~DeallocationInfo();
5987   }
5988 
5989   void initialize(Attributor &A) override {
5990     AAHeapToStack::initialize(A);
5991 
5992     const Function *F = getAnchorScope();
5993     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5994 
5995     auto AllocationIdentifierCB = [&](Instruction &I) {
5996       CallBase *CB = dyn_cast<CallBase>(&I);
5997       if (!CB)
5998         return true;
5999       if (isFreeCall(CB, TLI)) {
6000         DeallocationInfos[CB] = new (A.Allocator) DeallocationInfo{CB};
6001         return true;
6002       }
6003       // To do heap to stack, we need to know that the allocation itself is
6004       // removable once uses are rewritten, and that we can initialize the
6005       // alloca to the same pattern as the original allocation result.
6006       if (isAllocationFn(CB, TLI) && isAllocRemovable(CB, TLI)) {
6007         auto *I8Ty = Type::getInt8Ty(CB->getParent()->getContext());
6008         if (nullptr != getInitialValueOfAllocation(CB, TLI, I8Ty)) {
6009           AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB};
6010           AllocationInfos[CB] = AI;
6011           TLI->getLibFunc(*CB, AI->LibraryFunctionId);
6012         }
6013       }
6014       return true;
6015     };
6016 
6017     bool UsedAssumedInformation = false;
6018     bool Success = A.checkForAllCallLikeInstructions(
6019         AllocationIdentifierCB, *this, UsedAssumedInformation,
6020         /* CheckBBLivenessOnly */ false,
6021         /* CheckPotentiallyDead */ true);
6022     (void)Success;
6023     assert(Success && "Did not expect the call base visit callback to fail!");
6024 
6025     Attributor::SimplifictionCallbackTy SCB =
6026         [](const IRPosition &, const AbstractAttribute *,
6027            bool &) -> Optional<Value *> { return nullptr; };
6028     for (const auto &It : AllocationInfos)
6029       A.registerSimplificationCallback(IRPosition::callsite_returned(*It.first),
6030                                        SCB);
6031     for (const auto &It : DeallocationInfos)
6032       A.registerSimplificationCallback(IRPosition::callsite_returned(*It.first),
6033                                        SCB);
6034   }
6035 
6036   const std::string getAsStr() const override {
6037     unsigned NumH2SMallocs = 0, NumInvalidMallocs = 0;
6038     for (const auto &It : AllocationInfos) {
6039       if (It.second->Status == AllocationInfo::INVALID)
6040         ++NumInvalidMallocs;
6041       else
6042         ++NumH2SMallocs;
6043     }
6044     return "[H2S] Mallocs Good/Bad: " + std::to_string(NumH2SMallocs) + "/" +
6045            std::to_string(NumInvalidMallocs);
6046   }
6047 
6048   /// See AbstractAttribute::trackStatistics().
6049   void trackStatistics() const override {
6050     STATS_DECL(
6051         MallocCalls, Function,
6052         "Number of malloc/calloc/aligned_alloc calls converted to allocas");
6053     for (auto &It : AllocationInfos)
6054       if (It.second->Status != AllocationInfo::INVALID)
6055         ++BUILD_STAT_NAME(MallocCalls, Function);
6056   }
6057 
6058   bool isAssumedHeapToStack(const CallBase &CB) const override {
6059     if (isValidState())
6060       if (AllocationInfo *AI = AllocationInfos.lookup(&CB))
6061         return AI->Status != AllocationInfo::INVALID;
6062     return false;
6063   }
6064 
6065   bool isAssumedHeapToStackRemovedFree(CallBase &CB) const override {
6066     if (!isValidState())
6067       return false;
6068 
6069     for (auto &It : AllocationInfos) {
6070       AllocationInfo &AI = *It.second;
6071       if (AI.Status == AllocationInfo::INVALID)
6072         continue;
6073 
6074       if (AI.PotentialFreeCalls.count(&CB))
6075         return true;
6076     }
6077 
6078     return false;
6079   }
6080 
6081   ChangeStatus manifest(Attributor &A) override {
6082     assert(getState().isValidState() &&
6083            "Attempted to manifest an invalid state!");
6084 
6085     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
6086     Function *F = getAnchorScope();
6087     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6088 
6089     for (auto &It : AllocationInfos) {
6090       AllocationInfo &AI = *It.second;
6091       if (AI.Status == AllocationInfo::INVALID)
6092         continue;
6093 
6094       for (CallBase *FreeCall : AI.PotentialFreeCalls) {
6095         LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
6096         A.deleteAfterManifest(*FreeCall);
6097         HasChanged = ChangeStatus::CHANGED;
6098       }
6099 
6100       LLVM_DEBUG(dbgs() << "H2S: Removing malloc-like call: " << *AI.CB
6101                         << "\n");
6102 
6103       auto Remark = [&](OptimizationRemark OR) {
6104         LibFunc IsAllocShared;
6105         if (TLI->getLibFunc(*AI.CB, IsAllocShared))
6106           if (IsAllocShared == LibFunc___kmpc_alloc_shared)
6107             return OR << "Moving globalized variable to the stack.";
6108         return OR << "Moving memory allocation from the heap to the stack.";
6109       };
6110       if (AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
6111         A.emitRemark<OptimizationRemark>(AI.CB, "OMP110", Remark);
6112       else
6113         A.emitRemark<OptimizationRemark>(AI.CB, "HeapToStack", Remark);
6114 
6115       const DataLayout &DL = A.getInfoCache().getDL();
6116       Value *Size;
6117       Optional<APInt> SizeAPI = getSize(A, *this, AI);
6118       if (SizeAPI.hasValue()) {
6119         Size = ConstantInt::get(AI.CB->getContext(), *SizeAPI);
6120       } else {
6121         LLVMContext &Ctx = AI.CB->getContext();
6122         ObjectSizeOpts Opts;
6123         ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, Opts);
6124         SizeOffsetEvalType SizeOffsetPair = Eval.compute(AI.CB);
6125         assert(SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown() &&
6126                cast<ConstantInt>(SizeOffsetPair.second)->isZero());
6127         Size = SizeOffsetPair.first;
6128       }
6129 
6130       Align Alignment(1);
6131       if (MaybeAlign RetAlign = AI.CB->getRetAlign())
6132         Alignment = max(Alignment, RetAlign);
6133       if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
6134         Optional<APInt> AlignmentAPI = getAPInt(A, *this, *Align);
6135         assert(AlignmentAPI.hasValue() &&
6136                "Expected an alignment during manifest!");
6137         Alignment =
6138             max(Alignment, MaybeAlign(AlignmentAPI.getValue().getZExtValue()));
6139       }
6140 
6141       // TODO: Hoist the alloca towards the function entry.
6142       unsigned AS = DL.getAllocaAddrSpace();
6143       Instruction *Alloca = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
6144                                            Size, Alignment, "", AI.CB);
6145 
6146       if (Alloca->getType() != AI.CB->getType())
6147         Alloca = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
6148             Alloca, AI.CB->getType(), "malloc_cast", AI.CB);
6149 
6150       auto *I8Ty = Type::getInt8Ty(F->getContext());
6151       auto *InitVal = getInitialValueOfAllocation(AI.CB, TLI, I8Ty);
6152       assert(InitVal &&
6153              "Must be able to materialize initial memory state of allocation");
6154 
6155       A.changeValueAfterManifest(*AI.CB, *Alloca);
6156 
6157       if (auto *II = dyn_cast<InvokeInst>(AI.CB)) {
6158         auto *NBB = II->getNormalDest();
6159         BranchInst::Create(NBB, AI.CB->getParent());
6160         A.deleteAfterManifest(*AI.CB);
6161       } else {
6162         A.deleteAfterManifest(*AI.CB);
6163       }
6164 
6165       // Initialize the alloca with the same value as used by the allocation
6166       // function.  We can skip undef as the initial value of an alloc is
6167       // undef, and the memset would simply end up being DSEd.
6168       if (!isa<UndefValue>(InitVal)) {
6169         IRBuilder<> Builder(Alloca->getNextNode());
6170         // TODO: Use alignment above if align!=1
6171         Builder.CreateMemSet(Alloca, InitVal, Size, None);
6172       }
6173       HasChanged = ChangeStatus::CHANGED;
6174     }
6175 
6176     return HasChanged;
6177   }
6178 
6179   Optional<APInt> getAPInt(Attributor &A, const AbstractAttribute &AA,
6180                            Value &V) {
6181     bool UsedAssumedInformation = false;
6182     Optional<Constant *> SimpleV =
6183         A.getAssumedConstant(V, AA, UsedAssumedInformation);
6184     if (!SimpleV.hasValue())
6185       return APInt(64, 0);
6186     if (auto *CI = dyn_cast_or_null<ConstantInt>(SimpleV.getValue()))
6187       return CI->getValue();
6188     return llvm::None;
6189   }
6190 
6191   Optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA,
6192                           AllocationInfo &AI) {
6193     auto Mapper = [&](const Value *V) -> const Value * {
6194       bool UsedAssumedInformation = false;
6195       if (Optional<Constant *> SimpleV =
6196               A.getAssumedConstant(*V, AA, UsedAssumedInformation))
6197         if (*SimpleV)
6198           return *SimpleV;
6199       return V;
6200     };
6201 
6202     const Function *F = getAnchorScope();
6203     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6204     return getAllocSize(AI.CB, TLI, Mapper);
6205   }
6206 
6207   /// Collection of all malloc-like calls in a function with associated
6208   /// information.
6209   DenseMap<CallBase *, AllocationInfo *> AllocationInfos;
6210 
6211   /// Collection of all free-like calls in a function with associated
6212   /// information.
6213   DenseMap<CallBase *, DeallocationInfo *> DeallocationInfos;
6214 
6215   ChangeStatus updateImpl(Attributor &A) override;
6216 };
6217 
6218 ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) {
6219   ChangeStatus Changed = ChangeStatus::UNCHANGED;
6220   const Function *F = getAnchorScope();
6221   const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6222 
6223   const auto &LivenessAA =
6224       A.getAAFor<AAIsDead>(*this, IRPosition::function(*F), DepClassTy::NONE);
6225 
6226   MustBeExecutedContextExplorer &Explorer =
6227       A.getInfoCache().getMustBeExecutedContextExplorer();
6228 
6229   bool StackIsAccessibleByOtherThreads =
6230       A.getInfoCache().stackIsAccessibleByOtherThreads();
6231 
6232   // Flag to ensure we update our deallocation information at most once per
6233   // updateImpl call and only if we use the free check reasoning.
6234   bool HasUpdatedFrees = false;
6235 
6236   auto UpdateFrees = [&]() {
6237     HasUpdatedFrees = true;
6238 
6239     for (auto &It : DeallocationInfos) {
6240       DeallocationInfo &DI = *It.second;
6241       // For now we cannot use deallocations that have unknown inputs, skip
6242       // them.
6243       if (DI.MightFreeUnknownObjects)
6244         continue;
6245 
6246       // No need to analyze dead calls, ignore them instead.
6247       bool UsedAssumedInformation = false;
6248       if (A.isAssumedDead(*DI.CB, this, &LivenessAA, UsedAssumedInformation,
6249                           /* CheckBBLivenessOnly */ true))
6250         continue;
6251 
6252       // Use the optimistic version to get the freed objects, ignoring dead
6253       // branches etc.
6254       SmallVector<Value *, 8> Objects;
6255       if (!AA::getAssumedUnderlyingObjects(A, *DI.CB->getArgOperand(0), Objects,
6256                                            *this, DI.CB,
6257                                            UsedAssumedInformation)) {
6258         LLVM_DEBUG(
6259             dbgs()
6260             << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n");
6261         DI.MightFreeUnknownObjects = true;
6262         continue;
6263       }
6264 
6265       // Check each object explicitly.
6266       for (auto *Obj : Objects) {
6267         // Free of null and undef can be ignored as no-ops (or UB in the latter
6268         // case).
6269         if (isa<ConstantPointerNull>(Obj) || isa<UndefValue>(Obj))
6270           continue;
6271 
6272         CallBase *ObjCB = dyn_cast<CallBase>(Obj);
6273         if (!ObjCB) {
6274           LLVM_DEBUG(dbgs()
6275                      << "[H2S] Free of a non-call object: " << *Obj << "\n");
6276           DI.MightFreeUnknownObjects = true;
6277           continue;
6278         }
6279 
6280         AllocationInfo *AI = AllocationInfos.lookup(ObjCB);
6281         if (!AI) {
6282           LLVM_DEBUG(dbgs() << "[H2S] Free of a non-allocation object: " << *Obj
6283                             << "\n");
6284           DI.MightFreeUnknownObjects = true;
6285           continue;
6286         }
6287 
6288         DI.PotentialAllocationCalls.insert(ObjCB);
6289       }
6290     }
6291   };
6292 
6293   auto FreeCheck = [&](AllocationInfo &AI) {
6294     // If the stack is not accessible by other threads, the "must-free" logic
6295     // doesn't apply as the pointer could be shared and needs to be places in
6296     // "shareable" memory.
6297     if (!StackIsAccessibleByOtherThreads) {
6298       auto &NoSyncAA =
6299           A.getAAFor<AANoSync>(*this, getIRPosition(), DepClassTy::OPTIONAL);
6300       if (!NoSyncAA.isAssumedNoSync()) {
6301         LLVM_DEBUG(
6302             dbgs() << "[H2S] found an escaping use, stack is not accessible by "
6303                       "other threads and function is not nosync:\n");
6304         return false;
6305       }
6306     }
6307     if (!HasUpdatedFrees)
6308       UpdateFrees();
6309 
6310     // TODO: Allow multi exit functions that have different free calls.
6311     if (AI.PotentialFreeCalls.size() != 1) {
6312       LLVM_DEBUG(dbgs() << "[H2S] did not find one free call but "
6313                         << AI.PotentialFreeCalls.size() << "\n");
6314       return false;
6315     }
6316     CallBase *UniqueFree = *AI.PotentialFreeCalls.begin();
6317     DeallocationInfo *DI = DeallocationInfos.lookup(UniqueFree);
6318     if (!DI) {
6319       LLVM_DEBUG(
6320           dbgs() << "[H2S] unique free call was not known as deallocation call "
6321                  << *UniqueFree << "\n");
6322       return false;
6323     }
6324     if (DI->MightFreeUnknownObjects) {
6325       LLVM_DEBUG(
6326           dbgs() << "[H2S] unique free call might free unknown allocations\n");
6327       return false;
6328     }
6329     if (DI->PotentialAllocationCalls.size() > 1) {
6330       LLVM_DEBUG(dbgs() << "[H2S] unique free call might free "
6331                         << DI->PotentialAllocationCalls.size()
6332                         << " different allocations\n");
6333       return false;
6334     }
6335     if (*DI->PotentialAllocationCalls.begin() != AI.CB) {
6336       LLVM_DEBUG(
6337           dbgs()
6338           << "[H2S] unique free call not known to free this allocation but "
6339           << **DI->PotentialAllocationCalls.begin() << "\n");
6340       return false;
6341     }
6342     Instruction *CtxI = isa<InvokeInst>(AI.CB) ? AI.CB : AI.CB->getNextNode();
6343     if (!Explorer.findInContextOf(UniqueFree, CtxI)) {
6344       LLVM_DEBUG(
6345           dbgs()
6346           << "[H2S] unique free call might not be executed with the allocation "
6347           << *UniqueFree << "\n");
6348       return false;
6349     }
6350     return true;
6351   };
6352 
6353   auto UsesCheck = [&](AllocationInfo &AI) {
6354     bool ValidUsesOnly = true;
6355 
6356     auto Pred = [&](const Use &U, bool &Follow) -> bool {
6357       Instruction *UserI = cast<Instruction>(U.getUser());
6358       if (isa<LoadInst>(UserI))
6359         return true;
6360       if (auto *SI = dyn_cast<StoreInst>(UserI)) {
6361         if (SI->getValueOperand() == U.get()) {
6362           LLVM_DEBUG(dbgs()
6363                      << "[H2S] escaping store to memory: " << *UserI << "\n");
6364           ValidUsesOnly = false;
6365         } else {
6366           // A store into the malloc'ed memory is fine.
6367         }
6368         return true;
6369       }
6370       if (auto *CB = dyn_cast<CallBase>(UserI)) {
6371         if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
6372           return true;
6373         if (DeallocationInfos.count(CB)) {
6374           AI.PotentialFreeCalls.insert(CB);
6375           return true;
6376         }
6377 
6378         unsigned ArgNo = CB->getArgOperandNo(&U);
6379 
6380         const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
6381             *this, IRPosition::callsite_argument(*CB, ArgNo),
6382             DepClassTy::OPTIONAL);
6383 
6384         // If a call site argument use is nofree, we are fine.
6385         const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
6386             *this, IRPosition::callsite_argument(*CB, ArgNo),
6387             DepClassTy::OPTIONAL);
6388 
6389         bool MaybeCaptured = !NoCaptureAA.isAssumedNoCapture();
6390         bool MaybeFreed = !ArgNoFreeAA.isAssumedNoFree();
6391         if (MaybeCaptured ||
6392             (AI.LibraryFunctionId != LibFunc___kmpc_alloc_shared &&
6393              MaybeFreed)) {
6394           AI.HasPotentiallyFreeingUnknownUses |= MaybeFreed;
6395 
6396           // Emit a missed remark if this is missed OpenMP globalization.
6397           auto Remark = [&](OptimizationRemarkMissed ORM) {
6398             return ORM
6399                    << "Could not move globalized variable to the stack. "
6400                       "Variable is potentially captured in call. Mark "
6401                       "parameter as `__attribute__((noescape))` to override.";
6402           };
6403 
6404           if (ValidUsesOnly &&
6405               AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
6406             A.emitRemark<OptimizationRemarkMissed>(CB, "OMP113", Remark);
6407 
6408           LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
6409           ValidUsesOnly = false;
6410         }
6411         return true;
6412       }
6413 
6414       if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
6415           isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
6416         Follow = true;
6417         return true;
6418       }
6419       // Unknown user for which we can not track uses further (in a way that
6420       // makes sense).
6421       LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
6422       ValidUsesOnly = false;
6423       return true;
6424     };
6425     if (!A.checkForAllUses(Pred, *this, *AI.CB))
6426       return false;
6427     return ValidUsesOnly;
6428   };
6429 
6430   // The actual update starts here. We look at all allocations and depending on
6431   // their status perform the appropriate check(s).
6432   for (auto &It : AllocationInfos) {
6433     AllocationInfo &AI = *It.second;
6434     if (AI.Status == AllocationInfo::INVALID)
6435       continue;
6436 
6437     if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
6438       Optional<APInt> APAlign = getAPInt(A, *this, *Align);
6439       if (!APAlign) {
6440         // Can't generate an alloca which respects the required alignment
6441         // on the allocation.
6442         LLVM_DEBUG(dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CB
6443                           << "\n");
6444         AI.Status = AllocationInfo::INVALID;
6445         Changed = ChangeStatus::CHANGED;
6446         continue;
6447       } else {
6448         if (APAlign->ugt(llvm::Value::MaximumAlignment) || !APAlign->isPowerOf2()) {
6449           LLVM_DEBUG(dbgs() << "[H2S] Invalid allocation alignment: " << APAlign << "\n");
6450           AI.Status = AllocationInfo::INVALID;
6451           Changed = ChangeStatus::CHANGED;
6452           continue;
6453         }
6454       }
6455     }
6456 
6457     if (MaxHeapToStackSize != -1) {
6458       Optional<APInt> Size = getSize(A, *this, AI);
6459       if (!Size.hasValue() || Size.getValue().ugt(MaxHeapToStackSize)) {
6460         LLVM_DEBUG({
6461           if (!Size.hasValue())
6462             dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n";
6463           else
6464             dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. "
6465                    << MaxHeapToStackSize << "\n";
6466         });
6467 
6468         AI.Status = AllocationInfo::INVALID;
6469         Changed = ChangeStatus::CHANGED;
6470         continue;
6471       }
6472     }
6473 
6474     switch (AI.Status) {
6475     case AllocationInfo::STACK_DUE_TO_USE:
6476       if (UsesCheck(AI))
6477         continue;
6478       AI.Status = AllocationInfo::STACK_DUE_TO_FREE;
6479       LLVM_FALLTHROUGH;
6480     case AllocationInfo::STACK_DUE_TO_FREE:
6481       if (FreeCheck(AI))
6482         continue;
6483       AI.Status = AllocationInfo::INVALID;
6484       Changed = ChangeStatus::CHANGED;
6485       continue;
6486     case AllocationInfo::INVALID:
6487       llvm_unreachable("Invalid allocations should never reach this point!");
6488     };
6489   }
6490 
6491   return Changed;
6492 }
6493 } // namespace
6494 
6495 /// ----------------------- Privatizable Pointers ------------------------------
6496 namespace {
6497 struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
6498   AAPrivatizablePtrImpl(const IRPosition &IRP, Attributor &A)
6499       : AAPrivatizablePtr(IRP, A), PrivatizableType(llvm::None) {}
6500 
6501   ChangeStatus indicatePessimisticFixpoint() override {
6502     AAPrivatizablePtr::indicatePessimisticFixpoint();
6503     PrivatizableType = nullptr;
6504     return ChangeStatus::CHANGED;
6505   }
6506 
6507   /// Identify the type we can chose for a private copy of the underlying
6508   /// argument. None means it is not clear yet, nullptr means there is none.
6509   virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
6510 
6511   /// Return a privatizable type that encloses both T0 and T1.
6512   /// TODO: This is merely a stub for now as we should manage a mapping as well.
6513   Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
6514     if (!T0.hasValue())
6515       return T1;
6516     if (!T1.hasValue())
6517       return T0;
6518     if (T0 == T1)
6519       return T0;
6520     return nullptr;
6521   }
6522 
6523   Optional<Type *> getPrivatizableType() const override {
6524     return PrivatizableType;
6525   }
6526 
6527   const std::string getAsStr() const override {
6528     return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
6529   }
6530 
6531 protected:
6532   Optional<Type *> PrivatizableType;
6533 };
6534 
6535 // TODO: Do this for call site arguments (probably also other values) as well.
6536 
6537 struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
6538   AAPrivatizablePtrArgument(const IRPosition &IRP, Attributor &A)
6539       : AAPrivatizablePtrImpl(IRP, A) {}
6540 
6541   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
6542   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
6543     // If this is a byval argument and we know all the call sites (so we can
6544     // rewrite them), there is no need to check them explicitly.
6545     bool UsedAssumedInformation = false;
6546     SmallVector<Attribute, 1> Attrs;
6547     getAttrs({Attribute::ByVal}, Attrs, /* IgnoreSubsumingPositions */ true);
6548     if (!Attrs.empty() &&
6549         A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
6550                                true, UsedAssumedInformation))
6551       return Attrs[0].getValueAsType();
6552 
6553     Optional<Type *> Ty;
6554     unsigned ArgNo = getIRPosition().getCallSiteArgNo();
6555 
6556     // Make sure the associated call site argument has the same type at all call
6557     // sites and it is an allocation we know is safe to privatize, for now that
6558     // means we only allow alloca instructions.
6559     // TODO: We can additionally analyze the accesses in the callee to  create
6560     //       the type from that information instead. That is a little more
6561     //       involved and will be done in a follow up patch.
6562     auto CallSiteCheck = [&](AbstractCallSite ACS) {
6563       IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
6564       // Check if a coresponding argument was found or if it is one not
6565       // associated (which can happen for callback calls).
6566       if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
6567         return false;
6568 
6569       // Check that all call sites agree on a type.
6570       auto &PrivCSArgAA =
6571           A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos, DepClassTy::REQUIRED);
6572       Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
6573 
6574       LLVM_DEBUG({
6575         dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
6576         if (CSTy.hasValue() && CSTy.getValue())
6577           CSTy.getValue()->print(dbgs());
6578         else if (CSTy.hasValue())
6579           dbgs() << "<nullptr>";
6580         else
6581           dbgs() << "<none>";
6582       });
6583 
6584       Ty = combineTypes(Ty, CSTy);
6585 
6586       LLVM_DEBUG({
6587         dbgs() << " : New Type: ";
6588         if (Ty.hasValue() && Ty.getValue())
6589           Ty.getValue()->print(dbgs());
6590         else if (Ty.hasValue())
6591           dbgs() << "<nullptr>";
6592         else
6593           dbgs() << "<none>";
6594         dbgs() << "\n";
6595       });
6596 
6597       return !Ty.hasValue() || Ty.getValue();
6598     };
6599 
6600     if (!A.checkForAllCallSites(CallSiteCheck, *this, true,
6601                                 UsedAssumedInformation))
6602       return nullptr;
6603     return Ty;
6604   }
6605 
6606   /// See AbstractAttribute::updateImpl(...).
6607   ChangeStatus updateImpl(Attributor &A) override {
6608     PrivatizableType = identifyPrivatizableType(A);
6609     if (!PrivatizableType.hasValue())
6610       return ChangeStatus::UNCHANGED;
6611     if (!PrivatizableType.getValue())
6612       return indicatePessimisticFixpoint();
6613 
6614     // The dependence is optional so we don't give up once we give up on the
6615     // alignment.
6616     A.getAAFor<AAAlign>(*this, IRPosition::value(getAssociatedValue()),
6617                         DepClassTy::OPTIONAL);
6618 
6619     // Avoid arguments with padding for now.
6620     if (!getIRPosition().hasAttr(Attribute::ByVal) &&
6621         !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
6622                                                 A.getInfoCache().getDL())) {
6623       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
6624       return indicatePessimisticFixpoint();
6625     }
6626 
6627     // Collect the types that will replace the privatizable type in the function
6628     // signature.
6629     SmallVector<Type *, 16> ReplacementTypes;
6630     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
6631 
6632     // Verify callee and caller agree on how the promoted argument would be
6633     // passed.
6634     Function &Fn = *getIRPosition().getAnchorScope();
6635     const auto *TTI =
6636         A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
6637     if (!TTI) {
6638       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Missing TTI for function "
6639                         << Fn.getName() << "\n");
6640       return indicatePessimisticFixpoint();
6641     }
6642 
6643     auto CallSiteCheck = [&](AbstractCallSite ACS) {
6644       CallBase *CB = ACS.getInstruction();
6645       return TTI->areTypesABICompatible(
6646           CB->getCaller(), CB->getCalledFunction(), ReplacementTypes);
6647     };
6648     bool UsedAssumedInformation = false;
6649     if (!A.checkForAllCallSites(CallSiteCheck, *this, true,
6650                                 UsedAssumedInformation)) {
6651       LLVM_DEBUG(
6652           dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
6653                  << Fn.getName() << "\n");
6654       return indicatePessimisticFixpoint();
6655     }
6656 
6657     // Register a rewrite of the argument.
6658     Argument *Arg = getAssociatedArgument();
6659     if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
6660       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
6661       return indicatePessimisticFixpoint();
6662     }
6663 
6664     unsigned ArgNo = Arg->getArgNo();
6665 
6666     // Helper to check if for the given call site the associated argument is
6667     // passed to a callback where the privatization would be different.
6668     auto IsCompatiblePrivArgOfCallback = [&](CallBase &CB) {
6669       SmallVector<const Use *, 4> CallbackUses;
6670       AbstractCallSite::getCallbackUses(CB, CallbackUses);
6671       for (const Use *U : CallbackUses) {
6672         AbstractCallSite CBACS(U);
6673         assert(CBACS && CBACS.isCallbackCall());
6674         for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
6675           int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
6676 
6677           LLVM_DEBUG({
6678             dbgs()
6679                 << "[AAPrivatizablePtr] Argument " << *Arg
6680                 << "check if can be privatized in the context of its parent ("
6681                 << Arg->getParent()->getName()
6682                 << ")\n[AAPrivatizablePtr] because it is an argument in a "
6683                    "callback ("
6684                 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
6685                 << ")\n[AAPrivatizablePtr] " << CBArg << " : "
6686                 << CBACS.getCallArgOperand(CBArg) << " vs "
6687                 << CB.getArgOperand(ArgNo) << "\n"
6688                 << "[AAPrivatizablePtr] " << CBArg << " : "
6689                 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
6690           });
6691 
6692           if (CBArgNo != int(ArgNo))
6693             continue;
6694           const auto &CBArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
6695               *this, IRPosition::argument(CBArg), DepClassTy::REQUIRED);
6696           if (CBArgPrivAA.isValidState()) {
6697             auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
6698             if (!CBArgPrivTy.hasValue())
6699               continue;
6700             if (CBArgPrivTy.getValue() == PrivatizableType)
6701               continue;
6702           }
6703 
6704           LLVM_DEBUG({
6705             dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
6706                    << " cannot be privatized in the context of its parent ("
6707                    << Arg->getParent()->getName()
6708                    << ")\n[AAPrivatizablePtr] because it is an argument in a "
6709                       "callback ("
6710                    << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
6711                    << ").\n[AAPrivatizablePtr] for which the argument "
6712                       "privatization is not compatible.\n";
6713           });
6714           return false;
6715         }
6716       }
6717       return true;
6718     };
6719 
6720     // Helper to check if for the given call site the associated argument is
6721     // passed to a direct call where the privatization would be different.
6722     auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
6723       CallBase *DC = cast<CallBase>(ACS.getInstruction());
6724       int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
6725       assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->arg_size() &&
6726              "Expected a direct call operand for callback call operand");
6727 
6728       LLVM_DEBUG({
6729         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
6730                << " check if be privatized in the context of its parent ("
6731                << Arg->getParent()->getName()
6732                << ")\n[AAPrivatizablePtr] because it is an argument in a "
6733                   "direct call of ("
6734                << DCArgNo << "@" << DC->getCalledFunction()->getName()
6735                << ").\n";
6736       });
6737 
6738       Function *DCCallee = DC->getCalledFunction();
6739       if (unsigned(DCArgNo) < DCCallee->arg_size()) {
6740         const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
6741             *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)),
6742             DepClassTy::REQUIRED);
6743         if (DCArgPrivAA.isValidState()) {
6744           auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
6745           if (!DCArgPrivTy.hasValue())
6746             return true;
6747           if (DCArgPrivTy.getValue() == PrivatizableType)
6748             return true;
6749         }
6750       }
6751 
6752       LLVM_DEBUG({
6753         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
6754                << " cannot be privatized in the context of its parent ("
6755                << Arg->getParent()->getName()
6756                << ")\n[AAPrivatizablePtr] because it is an argument in a "
6757                   "direct call of ("
6758                << ACS.getInstruction()->getCalledFunction()->getName()
6759                << ").\n[AAPrivatizablePtr] for which the argument "
6760                   "privatization is not compatible.\n";
6761       });
6762       return false;
6763     };
6764 
6765     // Helper to check if the associated argument is used at the given abstract
6766     // call site in a way that is incompatible with the privatization assumed
6767     // here.
6768     auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
6769       if (ACS.isDirectCall())
6770         return IsCompatiblePrivArgOfCallback(*ACS.getInstruction());
6771       if (ACS.isCallbackCall())
6772         return IsCompatiblePrivArgOfDirectCS(ACS);
6773       return false;
6774     };
6775 
6776     if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
6777                                 UsedAssumedInformation))
6778       return indicatePessimisticFixpoint();
6779 
6780     return ChangeStatus::UNCHANGED;
6781   }
6782 
6783   /// Given a type to private \p PrivType, collect the constituates (which are
6784   /// used) in \p ReplacementTypes.
6785   static void
6786   identifyReplacementTypes(Type *PrivType,
6787                            SmallVectorImpl<Type *> &ReplacementTypes) {
6788     // TODO: For now we expand the privatization type to the fullest which can
6789     //       lead to dead arguments that need to be removed later.
6790     assert(PrivType && "Expected privatizable type!");
6791 
6792     // Traverse the type, extract constituate types on the outermost level.
6793     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6794       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
6795         ReplacementTypes.push_back(PrivStructType->getElementType(u));
6796     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6797       ReplacementTypes.append(PrivArrayType->getNumElements(),
6798                               PrivArrayType->getElementType());
6799     } else {
6800       ReplacementTypes.push_back(PrivType);
6801     }
6802   }
6803 
6804   /// Initialize \p Base according to the type \p PrivType at position \p IP.
6805   /// The values needed are taken from the arguments of \p F starting at
6806   /// position \p ArgNo.
6807   static void createInitialization(Type *PrivType, Value &Base, Function &F,
6808                                    unsigned ArgNo, Instruction &IP) {
6809     assert(PrivType && "Expected privatizable type!");
6810 
6811     IRBuilder<NoFolder> IRB(&IP);
6812     const DataLayout &DL = F.getParent()->getDataLayout();
6813 
6814     // Traverse the type, build GEPs and stores.
6815     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6816       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
6817       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
6818         Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
6819         Value *Ptr =
6820             constructPointer(PointeeTy, PrivType, &Base,
6821                              PrivStructLayout->getElementOffset(u), IRB, DL);
6822         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
6823       }
6824     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6825       Type *PointeeTy = PrivArrayType->getElementType();
6826       Type *PointeePtrTy = PointeeTy->getPointerTo();
6827       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
6828       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
6829         Value *Ptr = constructPointer(PointeePtrTy, PrivType, &Base,
6830                                       u * PointeeTySize, IRB, DL);
6831         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
6832       }
6833     } else {
6834       new StoreInst(F.getArg(ArgNo), &Base, &IP);
6835     }
6836   }
6837 
6838   /// Extract values from \p Base according to the type \p PrivType at the
6839   /// call position \p ACS. The values are appended to \p ReplacementValues.
6840   void createReplacementValues(Align Alignment, Type *PrivType,
6841                                AbstractCallSite ACS, Value *Base,
6842                                SmallVectorImpl<Value *> &ReplacementValues) {
6843     assert(Base && "Expected base value!");
6844     assert(PrivType && "Expected privatizable type!");
6845     Instruction *IP = ACS.getInstruction();
6846 
6847     IRBuilder<NoFolder> IRB(IP);
6848     const DataLayout &DL = IP->getModule()->getDataLayout();
6849 
6850     Type *PrivPtrType = PrivType->getPointerTo();
6851     if (Base->getType() != PrivPtrType)
6852       Base = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
6853           Base, PrivPtrType, "", ACS.getInstruction());
6854 
6855     // Traverse the type, build GEPs and loads.
6856     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6857       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
6858       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
6859         Type *PointeeTy = PrivStructType->getElementType(u);
6860         Value *Ptr =
6861             constructPointer(PointeeTy->getPointerTo(), PrivType, Base,
6862                              PrivStructLayout->getElementOffset(u), IRB, DL);
6863         LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
6864         L->setAlignment(Alignment);
6865         ReplacementValues.push_back(L);
6866       }
6867     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6868       Type *PointeeTy = PrivArrayType->getElementType();
6869       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
6870       Type *PointeePtrTy = PointeeTy->getPointerTo();
6871       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
6872         Value *Ptr = constructPointer(PointeePtrTy, PrivType, Base,
6873                                       u * PointeeTySize, IRB, DL);
6874         LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
6875         L->setAlignment(Alignment);
6876         ReplacementValues.push_back(L);
6877       }
6878     } else {
6879       LoadInst *L = new LoadInst(PrivType, Base, "", IP);
6880       L->setAlignment(Alignment);
6881       ReplacementValues.push_back(L);
6882     }
6883   }
6884 
6885   /// See AbstractAttribute::manifest(...)
6886   ChangeStatus manifest(Attributor &A) override {
6887     if (!PrivatizableType.hasValue())
6888       return ChangeStatus::UNCHANGED;
6889     assert(PrivatizableType.getValue() && "Expected privatizable type!");
6890 
6891     // Collect all tail calls in the function as we cannot allow new allocas to
6892     // escape into tail recursion.
6893     // TODO: Be smarter about new allocas escaping into tail calls.
6894     SmallVector<CallInst *, 16> TailCalls;
6895     bool UsedAssumedInformation = false;
6896     if (!A.checkForAllInstructions(
6897             [&](Instruction &I) {
6898               CallInst &CI = cast<CallInst>(I);
6899               if (CI.isTailCall())
6900                 TailCalls.push_back(&CI);
6901               return true;
6902             },
6903             *this, {Instruction::Call}, UsedAssumedInformation))
6904       return ChangeStatus::UNCHANGED;
6905 
6906     Argument *Arg = getAssociatedArgument();
6907     // Query AAAlign attribute for alignment of associated argument to
6908     // determine the best alignment of loads.
6909     const auto &AlignAA =
6910         A.getAAFor<AAAlign>(*this, IRPosition::value(*Arg), DepClassTy::NONE);
6911 
6912     // Callback to repair the associated function. A new alloca is placed at the
6913     // beginning and initialized with the values passed through arguments. The
6914     // new alloca replaces the use of the old pointer argument.
6915     Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
6916         [=](const Attributor::ArgumentReplacementInfo &ARI,
6917             Function &ReplacementFn, Function::arg_iterator ArgIt) {
6918           BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
6919           Instruction *IP = &*EntryBB.getFirstInsertionPt();
6920           const DataLayout &DL = IP->getModule()->getDataLayout();
6921           unsigned AS = DL.getAllocaAddrSpace();
6922           Instruction *AI = new AllocaInst(PrivatizableType.getValue(), AS,
6923                                            Arg->getName() + ".priv", IP);
6924           createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
6925                                ArgIt->getArgNo(), *IP);
6926 
6927           if (AI->getType() != Arg->getType())
6928             AI = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
6929                 AI, Arg->getType(), "", IP);
6930           Arg->replaceAllUsesWith(AI);
6931 
6932           for (CallInst *CI : TailCalls)
6933             CI->setTailCall(false);
6934         };
6935 
6936     // Callback to repair a call site of the associated function. The elements
6937     // of the privatizable type are loaded prior to the call and passed to the
6938     // new function version.
6939     Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
6940         [=, &AlignAA](const Attributor::ArgumentReplacementInfo &ARI,
6941                       AbstractCallSite ACS,
6942                       SmallVectorImpl<Value *> &NewArgOperands) {
6943           // When no alignment is specified for the load instruction,
6944           // natural alignment is assumed.
6945           createReplacementValues(
6946               assumeAligned(AlignAA.getAssumedAlign()),
6947               PrivatizableType.getValue(), ACS,
6948               ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
6949               NewArgOperands);
6950         };
6951 
6952     // Collect the types that will replace the privatizable type in the function
6953     // signature.
6954     SmallVector<Type *, 16> ReplacementTypes;
6955     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
6956 
6957     // Register a rewrite of the argument.
6958     if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
6959                                            std::move(FnRepairCB),
6960                                            std::move(ACSRepairCB)))
6961       return ChangeStatus::CHANGED;
6962     return ChangeStatus::UNCHANGED;
6963   }
6964 
6965   /// See AbstractAttribute::trackStatistics()
6966   void trackStatistics() const override {
6967     STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
6968   }
6969 };
6970 
6971 struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
6972   AAPrivatizablePtrFloating(const IRPosition &IRP, Attributor &A)
6973       : AAPrivatizablePtrImpl(IRP, A) {}
6974 
6975   /// See AbstractAttribute::initialize(...).
6976   virtual void initialize(Attributor &A) override {
6977     // TODO: We can privatize more than arguments.
6978     indicatePessimisticFixpoint();
6979   }
6980 
6981   ChangeStatus updateImpl(Attributor &A) override {
6982     llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
6983                      "updateImpl will not be called");
6984   }
6985 
6986   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
6987   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
6988     Value *Obj = getUnderlyingObject(&getAssociatedValue());
6989     if (!Obj) {
6990       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
6991       return nullptr;
6992     }
6993 
6994     if (auto *AI = dyn_cast<AllocaInst>(Obj))
6995       if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
6996         if (CI->isOne())
6997           return AI->getAllocatedType();
6998     if (auto *Arg = dyn_cast<Argument>(Obj)) {
6999       auto &PrivArgAA = A.getAAFor<AAPrivatizablePtr>(
7000           *this, IRPosition::argument(*Arg), DepClassTy::REQUIRED);
7001       if (PrivArgAA.isAssumedPrivatizablePtr())
7002         return PrivArgAA.getPrivatizableType();
7003     }
7004 
7005     LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
7006                          "alloca nor privatizable argument: "
7007                       << *Obj << "!\n");
7008     return nullptr;
7009   }
7010 
7011   /// See AbstractAttribute::trackStatistics()
7012   void trackStatistics() const override {
7013     STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
7014   }
7015 };
7016 
7017 struct AAPrivatizablePtrCallSiteArgument final
7018     : public AAPrivatizablePtrFloating {
7019   AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP, Attributor &A)
7020       : AAPrivatizablePtrFloating(IRP, A) {}
7021 
7022   /// See AbstractAttribute::initialize(...).
7023   void initialize(Attributor &A) override {
7024     if (getIRPosition().hasAttr(Attribute::ByVal))
7025       indicateOptimisticFixpoint();
7026   }
7027 
7028   /// See AbstractAttribute::updateImpl(...).
7029   ChangeStatus updateImpl(Attributor &A) override {
7030     PrivatizableType = identifyPrivatizableType(A);
7031     if (!PrivatizableType.hasValue())
7032       return ChangeStatus::UNCHANGED;
7033     if (!PrivatizableType.getValue())
7034       return indicatePessimisticFixpoint();
7035 
7036     const IRPosition &IRP = getIRPosition();
7037     auto &NoCaptureAA =
7038         A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::REQUIRED);
7039     if (!NoCaptureAA.isAssumedNoCapture()) {
7040       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
7041       return indicatePessimisticFixpoint();
7042     }
7043 
7044     auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP, DepClassTy::REQUIRED);
7045     if (!NoAliasAA.isAssumedNoAlias()) {
7046       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
7047       return indicatePessimisticFixpoint();
7048     }
7049 
7050     bool IsKnown;
7051     if (!AA::isAssumedReadOnly(A, IRP, *this, IsKnown)) {
7052       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
7053       return indicatePessimisticFixpoint();
7054     }
7055 
7056     return ChangeStatus::UNCHANGED;
7057   }
7058 
7059   /// See AbstractAttribute::trackStatistics()
7060   void trackStatistics() const override {
7061     STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
7062   }
7063 };
7064 
7065 struct AAPrivatizablePtrCallSiteReturned final
7066     : public AAPrivatizablePtrFloating {
7067   AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP, Attributor &A)
7068       : AAPrivatizablePtrFloating(IRP, A) {}
7069 
7070   /// See AbstractAttribute::initialize(...).
7071   void initialize(Attributor &A) override {
7072     // TODO: We can privatize more than arguments.
7073     indicatePessimisticFixpoint();
7074   }
7075 
7076   /// See AbstractAttribute::trackStatistics()
7077   void trackStatistics() const override {
7078     STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
7079   }
7080 };
7081 
7082 struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
7083   AAPrivatizablePtrReturned(const IRPosition &IRP, Attributor &A)
7084       : AAPrivatizablePtrFloating(IRP, A) {}
7085 
7086   /// See AbstractAttribute::initialize(...).
7087   void initialize(Attributor &A) override {
7088     // TODO: We can privatize more than arguments.
7089     indicatePessimisticFixpoint();
7090   }
7091 
7092   /// See AbstractAttribute::trackStatistics()
7093   void trackStatistics() const override {
7094     STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
7095   }
7096 };
7097 } // namespace
7098 
7099 /// -------------------- Memory Behavior Attributes ----------------------------
7100 /// Includes read-none, read-only, and write-only.
7101 /// ----------------------------------------------------------------------------
7102 namespace {
7103 struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
7104   AAMemoryBehaviorImpl(const IRPosition &IRP, Attributor &A)
7105       : AAMemoryBehavior(IRP, A) {}
7106 
7107   /// See AbstractAttribute::initialize(...).
7108   void initialize(Attributor &A) override {
7109     intersectAssumedBits(BEST_STATE);
7110     getKnownStateFromValue(getIRPosition(), getState());
7111     AAMemoryBehavior::initialize(A);
7112   }
7113 
7114   /// Return the memory behavior information encoded in the IR for \p IRP.
7115   static void getKnownStateFromValue(const IRPosition &IRP,
7116                                      BitIntegerState &State,
7117                                      bool IgnoreSubsumingPositions = false) {
7118     SmallVector<Attribute, 2> Attrs;
7119     IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
7120     for (const Attribute &Attr : Attrs) {
7121       switch (Attr.getKindAsEnum()) {
7122       case Attribute::ReadNone:
7123         State.addKnownBits(NO_ACCESSES);
7124         break;
7125       case Attribute::ReadOnly:
7126         State.addKnownBits(NO_WRITES);
7127         break;
7128       case Attribute::WriteOnly:
7129         State.addKnownBits(NO_READS);
7130         break;
7131       default:
7132         llvm_unreachable("Unexpected attribute!");
7133       }
7134     }
7135 
7136     if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
7137       if (!I->mayReadFromMemory())
7138         State.addKnownBits(NO_READS);
7139       if (!I->mayWriteToMemory())
7140         State.addKnownBits(NO_WRITES);
7141     }
7142   }
7143 
7144   /// See AbstractAttribute::getDeducedAttributes(...).
7145   void getDeducedAttributes(LLVMContext &Ctx,
7146                             SmallVectorImpl<Attribute> &Attrs) const override {
7147     assert(Attrs.size() == 0);
7148     if (isAssumedReadNone())
7149       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
7150     else if (isAssumedReadOnly())
7151       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
7152     else if (isAssumedWriteOnly())
7153       Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
7154     assert(Attrs.size() <= 1);
7155   }
7156 
7157   /// See AbstractAttribute::manifest(...).
7158   ChangeStatus manifest(Attributor &A) override {
7159     if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true))
7160       return ChangeStatus::UNCHANGED;
7161 
7162     const IRPosition &IRP = getIRPosition();
7163 
7164     // Check if we would improve the existing attributes first.
7165     SmallVector<Attribute, 4> DeducedAttrs;
7166     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
7167     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
7168           return IRP.hasAttr(Attr.getKindAsEnum(),
7169                              /* IgnoreSubsumingPositions */ true);
7170         }))
7171       return ChangeStatus::UNCHANGED;
7172 
7173     // Clear existing attributes.
7174     IRP.removeAttrs(AttrKinds);
7175 
7176     // Use the generic manifest method.
7177     return IRAttribute::manifest(A);
7178   }
7179 
7180   /// See AbstractState::getAsStr().
7181   const std::string getAsStr() const override {
7182     if (isAssumedReadNone())
7183       return "readnone";
7184     if (isAssumedReadOnly())
7185       return "readonly";
7186     if (isAssumedWriteOnly())
7187       return "writeonly";
7188     return "may-read/write";
7189   }
7190 
7191   /// The set of IR attributes AAMemoryBehavior deals with.
7192   static const Attribute::AttrKind AttrKinds[3];
7193 };
7194 
7195 const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
7196     Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
7197 
7198 /// Memory behavior attribute for a floating value.
7199 struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
7200   AAMemoryBehaviorFloating(const IRPosition &IRP, Attributor &A)
7201       : AAMemoryBehaviorImpl(IRP, A) {}
7202 
7203   /// See AbstractAttribute::updateImpl(...).
7204   ChangeStatus updateImpl(Attributor &A) override;
7205 
7206   /// See AbstractAttribute::trackStatistics()
7207   void trackStatistics() const override {
7208     if (isAssumedReadNone())
7209       STATS_DECLTRACK_FLOATING_ATTR(readnone)
7210     else if (isAssumedReadOnly())
7211       STATS_DECLTRACK_FLOATING_ATTR(readonly)
7212     else if (isAssumedWriteOnly())
7213       STATS_DECLTRACK_FLOATING_ATTR(writeonly)
7214   }
7215 
7216 private:
7217   /// Return true if users of \p UserI might access the underlying
7218   /// variable/location described by \p U and should therefore be analyzed.
7219   bool followUsersOfUseIn(Attributor &A, const Use &U,
7220                           const Instruction *UserI);
7221 
7222   /// Update the state according to the effect of use \p U in \p UserI.
7223   void analyzeUseIn(Attributor &A, const Use &U, const Instruction *UserI);
7224 };
7225 
7226 /// Memory behavior attribute for function argument.
7227 struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
7228   AAMemoryBehaviorArgument(const IRPosition &IRP, Attributor &A)
7229       : AAMemoryBehaviorFloating(IRP, A) {}
7230 
7231   /// See AbstractAttribute::initialize(...).
7232   void initialize(Attributor &A) override {
7233     intersectAssumedBits(BEST_STATE);
7234     const IRPosition &IRP = getIRPosition();
7235     // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
7236     // can query it when we use has/getAttr. That would allow us to reuse the
7237     // initialize of the base class here.
7238     bool HasByVal =
7239         IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
7240     getKnownStateFromValue(IRP, getState(),
7241                            /* IgnoreSubsumingPositions */ HasByVal);
7242 
7243     // Initialize the use vector with all direct uses of the associated value.
7244     Argument *Arg = getAssociatedArgument();
7245     if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent())))
7246       indicatePessimisticFixpoint();
7247   }
7248 
7249   ChangeStatus manifest(Attributor &A) override {
7250     // TODO: Pointer arguments are not supported on vectors of pointers yet.
7251     if (!getAssociatedValue().getType()->isPointerTy())
7252       return ChangeStatus::UNCHANGED;
7253 
7254     // TODO: From readattrs.ll: "inalloca parameters are always
7255     //                           considered written"
7256     if (hasAttr({Attribute::InAlloca, Attribute::Preallocated})) {
7257       removeKnownBits(NO_WRITES);
7258       removeAssumedBits(NO_WRITES);
7259     }
7260     return AAMemoryBehaviorFloating::manifest(A);
7261   }
7262 
7263   /// See AbstractAttribute::trackStatistics()
7264   void trackStatistics() const override {
7265     if (isAssumedReadNone())
7266       STATS_DECLTRACK_ARG_ATTR(readnone)
7267     else if (isAssumedReadOnly())
7268       STATS_DECLTRACK_ARG_ATTR(readonly)
7269     else if (isAssumedWriteOnly())
7270       STATS_DECLTRACK_ARG_ATTR(writeonly)
7271   }
7272 };
7273 
7274 struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
7275   AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP, Attributor &A)
7276       : AAMemoryBehaviorArgument(IRP, A) {}
7277 
7278   /// See AbstractAttribute::initialize(...).
7279   void initialize(Attributor &A) override {
7280     // If we don't have an associated attribute this is either a variadic call
7281     // or an indirect call, either way, nothing to do here.
7282     Argument *Arg = getAssociatedArgument();
7283     if (!Arg) {
7284       indicatePessimisticFixpoint();
7285       return;
7286     }
7287     if (Arg->hasByValAttr()) {
7288       addKnownBits(NO_WRITES);
7289       removeKnownBits(NO_READS);
7290       removeAssumedBits(NO_READS);
7291     }
7292     AAMemoryBehaviorArgument::initialize(A);
7293     if (getAssociatedFunction()->isDeclaration())
7294       indicatePessimisticFixpoint();
7295   }
7296 
7297   /// See AbstractAttribute::updateImpl(...).
7298   ChangeStatus updateImpl(Attributor &A) override {
7299     // TODO: Once we have call site specific value information we can provide
7300     //       call site specific liveness liveness information and then it makes
7301     //       sense to specialize attributes for call sites arguments instead of
7302     //       redirecting requests to the callee argument.
7303     Argument *Arg = getAssociatedArgument();
7304     const IRPosition &ArgPos = IRPosition::argument(*Arg);
7305     auto &ArgAA =
7306         A.getAAFor<AAMemoryBehavior>(*this, ArgPos, DepClassTy::REQUIRED);
7307     return clampStateAndIndicateChange(getState(), ArgAA.getState());
7308   }
7309 
7310   /// See AbstractAttribute::trackStatistics()
7311   void trackStatistics() const override {
7312     if (isAssumedReadNone())
7313       STATS_DECLTRACK_CSARG_ATTR(readnone)
7314     else if (isAssumedReadOnly())
7315       STATS_DECLTRACK_CSARG_ATTR(readonly)
7316     else if (isAssumedWriteOnly())
7317       STATS_DECLTRACK_CSARG_ATTR(writeonly)
7318   }
7319 };
7320 
7321 /// Memory behavior attribute for a call site return position.
7322 struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
7323   AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP, Attributor &A)
7324       : AAMemoryBehaviorFloating(IRP, A) {}
7325 
7326   /// See AbstractAttribute::initialize(...).
7327   void initialize(Attributor &A) override {
7328     AAMemoryBehaviorImpl::initialize(A);
7329     Function *F = getAssociatedFunction();
7330     if (!F || F->isDeclaration())
7331       indicatePessimisticFixpoint();
7332   }
7333 
7334   /// See AbstractAttribute::manifest(...).
7335   ChangeStatus manifest(Attributor &A) override {
7336     // We do not annotate returned values.
7337     return ChangeStatus::UNCHANGED;
7338   }
7339 
7340   /// See AbstractAttribute::trackStatistics()
7341   void trackStatistics() const override {}
7342 };
7343 
7344 /// An AA to represent the memory behavior function attributes.
7345 struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
7346   AAMemoryBehaviorFunction(const IRPosition &IRP, Attributor &A)
7347       : AAMemoryBehaviorImpl(IRP, A) {}
7348 
7349   /// See AbstractAttribute::updateImpl(Attributor &A).
7350   virtual ChangeStatus updateImpl(Attributor &A) override;
7351 
7352   /// See AbstractAttribute::manifest(...).
7353   ChangeStatus manifest(Attributor &A) override {
7354     Function &F = cast<Function>(getAnchorValue());
7355     if (isAssumedReadNone()) {
7356       F.removeFnAttr(Attribute::ArgMemOnly);
7357       F.removeFnAttr(Attribute::InaccessibleMemOnly);
7358       F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
7359     }
7360     return AAMemoryBehaviorImpl::manifest(A);
7361   }
7362 
7363   /// See AbstractAttribute::trackStatistics()
7364   void trackStatistics() const override {
7365     if (isAssumedReadNone())
7366       STATS_DECLTRACK_FN_ATTR(readnone)
7367     else if (isAssumedReadOnly())
7368       STATS_DECLTRACK_FN_ATTR(readonly)
7369     else if (isAssumedWriteOnly())
7370       STATS_DECLTRACK_FN_ATTR(writeonly)
7371   }
7372 };
7373 
7374 /// AAMemoryBehavior attribute for call sites.
7375 struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
7376   AAMemoryBehaviorCallSite(const IRPosition &IRP, Attributor &A)
7377       : AAMemoryBehaviorImpl(IRP, A) {}
7378 
7379   /// See AbstractAttribute::initialize(...).
7380   void initialize(Attributor &A) override {
7381     AAMemoryBehaviorImpl::initialize(A);
7382     Function *F = getAssociatedFunction();
7383     if (!F || F->isDeclaration())
7384       indicatePessimisticFixpoint();
7385   }
7386 
7387   /// See AbstractAttribute::updateImpl(...).
7388   ChangeStatus updateImpl(Attributor &A) override {
7389     // TODO: Once we have call site specific value information we can provide
7390     //       call site specific liveness liveness information and then it makes
7391     //       sense to specialize attributes for call sites arguments instead of
7392     //       redirecting requests to the callee argument.
7393     Function *F = getAssociatedFunction();
7394     const IRPosition &FnPos = IRPosition::function(*F);
7395     auto &FnAA =
7396         A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::REQUIRED);
7397     return clampStateAndIndicateChange(getState(), FnAA.getState());
7398   }
7399 
7400   /// See AbstractAttribute::trackStatistics()
7401   void trackStatistics() const override {
7402     if (isAssumedReadNone())
7403       STATS_DECLTRACK_CS_ATTR(readnone)
7404     else if (isAssumedReadOnly())
7405       STATS_DECLTRACK_CS_ATTR(readonly)
7406     else if (isAssumedWriteOnly())
7407       STATS_DECLTRACK_CS_ATTR(writeonly)
7408   }
7409 };
7410 
7411 ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
7412 
7413   // The current assumed state used to determine a change.
7414   auto AssumedState = getAssumed();
7415 
7416   auto CheckRWInst = [&](Instruction &I) {
7417     // If the instruction has an own memory behavior state, use it to restrict
7418     // the local state. No further analysis is required as the other memory
7419     // state is as optimistic as it gets.
7420     if (const auto *CB = dyn_cast<CallBase>(&I)) {
7421       const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
7422           *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
7423       intersectAssumedBits(MemBehaviorAA.getAssumed());
7424       return !isAtFixpoint();
7425     }
7426 
7427     // Remove access kind modifiers if necessary.
7428     if (I.mayReadFromMemory())
7429       removeAssumedBits(NO_READS);
7430     if (I.mayWriteToMemory())
7431       removeAssumedBits(NO_WRITES);
7432     return !isAtFixpoint();
7433   };
7434 
7435   bool UsedAssumedInformation = false;
7436   if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
7437                                           UsedAssumedInformation))
7438     return indicatePessimisticFixpoint();
7439 
7440   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7441                                         : ChangeStatus::UNCHANGED;
7442 }
7443 
7444 ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
7445 
7446   const IRPosition &IRP = getIRPosition();
7447   const IRPosition &FnPos = IRPosition::function_scope(IRP);
7448   AAMemoryBehavior::StateType &S = getState();
7449 
7450   // First, check the function scope. We take the known information and we avoid
7451   // work if the assumed information implies the current assumed information for
7452   // this attribute. This is a valid for all but byval arguments.
7453   Argument *Arg = IRP.getAssociatedArgument();
7454   AAMemoryBehavior::base_t FnMemAssumedState =
7455       AAMemoryBehavior::StateType::getWorstState();
7456   if (!Arg || !Arg->hasByValAttr()) {
7457     const auto &FnMemAA =
7458         A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::OPTIONAL);
7459     FnMemAssumedState = FnMemAA.getAssumed();
7460     S.addKnownBits(FnMemAA.getKnown());
7461     if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
7462       return ChangeStatus::UNCHANGED;
7463   }
7464 
7465   // The current assumed state used to determine a change.
7466   auto AssumedState = S.getAssumed();
7467 
7468   // Make sure the value is not captured (except through "return"), if
7469   // it is, any information derived would be irrelevant anyway as we cannot
7470   // check the potential aliases introduced by the capture. However, no need
7471   // to fall back to anythign less optimistic than the function state.
7472   const auto &ArgNoCaptureAA =
7473       A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::OPTIONAL);
7474   if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
7475     S.intersectAssumedBits(FnMemAssumedState);
7476     return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7477                                           : ChangeStatus::UNCHANGED;
7478   }
7479 
7480   // Visit and expand uses until all are analyzed or a fixpoint is reached.
7481   auto UsePred = [&](const Use &U, bool &Follow) -> bool {
7482     Instruction *UserI = cast<Instruction>(U.getUser());
7483     LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserI
7484                       << " \n");
7485 
7486     // Droppable users, e.g., llvm::assume does not actually perform any action.
7487     if (UserI->isDroppable())
7488       return true;
7489 
7490     // Check if the users of UserI should also be visited.
7491     Follow = followUsersOfUseIn(A, U, UserI);
7492 
7493     // If UserI might touch memory we analyze the use in detail.
7494     if (UserI->mayReadOrWriteMemory())
7495       analyzeUseIn(A, U, UserI);
7496 
7497     return !isAtFixpoint();
7498   };
7499 
7500   if (!A.checkForAllUses(UsePred, *this, getAssociatedValue()))
7501     return indicatePessimisticFixpoint();
7502 
7503   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7504                                         : ChangeStatus::UNCHANGED;
7505 }
7506 
7507 bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use &U,
7508                                                   const Instruction *UserI) {
7509   // The loaded value is unrelated to the pointer argument, no need to
7510   // follow the users of the load.
7511   if (isa<LoadInst>(UserI))
7512     return false;
7513 
7514   // By default we follow all uses assuming UserI might leak information on U,
7515   // we have special handling for call sites operands though.
7516   const auto *CB = dyn_cast<CallBase>(UserI);
7517   if (!CB || !CB->isArgOperand(&U))
7518     return true;
7519 
7520   // If the use is a call argument known not to be captured, the users of
7521   // the call do not need to be visited because they have to be unrelated to
7522   // the input. Note that this check is not trivial even though we disallow
7523   // general capturing of the underlying argument. The reason is that the
7524   // call might the argument "through return", which we allow and for which we
7525   // need to check call users.
7526   if (U.get()->getType()->isPointerTy()) {
7527     unsigned ArgNo = CB->getArgOperandNo(&U);
7528     const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
7529         *this, IRPosition::callsite_argument(*CB, ArgNo), DepClassTy::OPTIONAL);
7530     return !ArgNoCaptureAA.isAssumedNoCapture();
7531   }
7532 
7533   return true;
7534 }
7535 
7536 void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use &U,
7537                                             const Instruction *UserI) {
7538   assert(UserI->mayReadOrWriteMemory());
7539 
7540   switch (UserI->getOpcode()) {
7541   default:
7542     // TODO: Handle all atomics and other side-effect operations we know of.
7543     break;
7544   case Instruction::Load:
7545     // Loads cause the NO_READS property to disappear.
7546     removeAssumedBits(NO_READS);
7547     return;
7548 
7549   case Instruction::Store:
7550     // Stores cause the NO_WRITES property to disappear if the use is the
7551     // pointer operand. Note that while capturing was taken care of somewhere
7552     // else we need to deal with stores of the value that is not looked through.
7553     if (cast<StoreInst>(UserI)->getPointerOperand() == U.get())
7554       removeAssumedBits(NO_WRITES);
7555     else
7556       indicatePessimisticFixpoint();
7557     return;
7558 
7559   case Instruction::Call:
7560   case Instruction::CallBr:
7561   case Instruction::Invoke: {
7562     // For call sites we look at the argument memory behavior attribute (this
7563     // could be recursive!) in order to restrict our own state.
7564     const auto *CB = cast<CallBase>(UserI);
7565 
7566     // Give up on operand bundles.
7567     if (CB->isBundleOperand(&U)) {
7568       indicatePessimisticFixpoint();
7569       return;
7570     }
7571 
7572     // Calling a function does read the function pointer, maybe write it if the
7573     // function is self-modifying.
7574     if (CB->isCallee(&U)) {
7575       removeAssumedBits(NO_READS);
7576       break;
7577     }
7578 
7579     // Adjust the possible access behavior based on the information on the
7580     // argument.
7581     IRPosition Pos;
7582     if (U.get()->getType()->isPointerTy())
7583       Pos = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
7584     else
7585       Pos = IRPosition::callsite_function(*CB);
7586     const auto &MemBehaviorAA =
7587         A.getAAFor<AAMemoryBehavior>(*this, Pos, DepClassTy::OPTIONAL);
7588     // "assumed" has at most the same bits as the MemBehaviorAA assumed
7589     // and at least "known".
7590     intersectAssumedBits(MemBehaviorAA.getAssumed());
7591     return;
7592   }
7593   };
7594 
7595   // Generally, look at the "may-properties" and adjust the assumed state if we
7596   // did not trigger special handling before.
7597   if (UserI->mayReadFromMemory())
7598     removeAssumedBits(NO_READS);
7599   if (UserI->mayWriteToMemory())
7600     removeAssumedBits(NO_WRITES);
7601 }
7602 } // namespace
7603 
7604 /// -------------------- Memory Locations Attributes ---------------------------
7605 /// Includes read-none, argmemonly, inaccessiblememonly,
7606 /// inaccessiblememorargmemonly
7607 /// ----------------------------------------------------------------------------
7608 
7609 std::string AAMemoryLocation::getMemoryLocationsAsStr(
7610     AAMemoryLocation::MemoryLocationsKind MLK) {
7611   if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS))
7612     return "all memory";
7613   if (MLK == AAMemoryLocation::NO_LOCATIONS)
7614     return "no memory";
7615   std::string S = "memory:";
7616   if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM))
7617     S += "stack,";
7618   if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM))
7619     S += "constant,";
7620   if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM))
7621     S += "internal global,";
7622   if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM))
7623     S += "external global,";
7624   if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM))
7625     S += "argument,";
7626   if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM))
7627     S += "inaccessible,";
7628   if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM))
7629     S += "malloced,";
7630   if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM))
7631     S += "unknown,";
7632   S.pop_back();
7633   return S;
7634 }
7635 
7636 namespace {
7637 struct AAMemoryLocationImpl : public AAMemoryLocation {
7638 
7639   AAMemoryLocationImpl(const IRPosition &IRP, Attributor &A)
7640       : AAMemoryLocation(IRP, A), Allocator(A.Allocator) {
7641     for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
7642       AccessKind2Accesses[u] = nullptr;
7643   }
7644 
7645   ~AAMemoryLocationImpl() {
7646     // The AccessSets are allocated via a BumpPtrAllocator, we call
7647     // the destructor manually.
7648     for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
7649       if (AccessKind2Accesses[u])
7650         AccessKind2Accesses[u]->~AccessSet();
7651   }
7652 
7653   /// See AbstractAttribute::initialize(...).
7654   void initialize(Attributor &A) override {
7655     intersectAssumedBits(BEST_STATE);
7656     getKnownStateFromValue(A, getIRPosition(), getState());
7657     AAMemoryLocation::initialize(A);
7658   }
7659 
7660   /// Return the memory behavior information encoded in the IR for \p IRP.
7661   static void getKnownStateFromValue(Attributor &A, const IRPosition &IRP,
7662                                      BitIntegerState &State,
7663                                      bool IgnoreSubsumingPositions = false) {
7664     // For internal functions we ignore `argmemonly` and
7665     // `inaccessiblememorargmemonly` as we might break it via interprocedural
7666     // constant propagation. It is unclear if this is the best way but it is
7667     // unlikely this will cause real performance problems. If we are deriving
7668     // attributes for the anchor function we even remove the attribute in
7669     // addition to ignoring it.
7670     bool UseArgMemOnly = true;
7671     Function *AnchorFn = IRP.getAnchorScope();
7672     if (AnchorFn && A.isRunOn(*AnchorFn))
7673       UseArgMemOnly = !AnchorFn->hasLocalLinkage();
7674 
7675     SmallVector<Attribute, 2> Attrs;
7676     IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
7677     for (const Attribute &Attr : Attrs) {
7678       switch (Attr.getKindAsEnum()) {
7679       case Attribute::ReadNone:
7680         State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM);
7681         break;
7682       case Attribute::InaccessibleMemOnly:
7683         State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
7684         break;
7685       case Attribute::ArgMemOnly:
7686         if (UseArgMemOnly)
7687           State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true));
7688         else
7689           IRP.removeAttrs({Attribute::ArgMemOnly});
7690         break;
7691       case Attribute::InaccessibleMemOrArgMemOnly:
7692         if (UseArgMemOnly)
7693           State.addKnownBits(inverseLocation(
7694               NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
7695         else
7696           IRP.removeAttrs({Attribute::InaccessibleMemOrArgMemOnly});
7697         break;
7698       default:
7699         llvm_unreachable("Unexpected attribute!");
7700       }
7701     }
7702   }
7703 
7704   /// See AbstractAttribute::getDeducedAttributes(...).
7705   void getDeducedAttributes(LLVMContext &Ctx,
7706                             SmallVectorImpl<Attribute> &Attrs) const override {
7707     assert(Attrs.size() == 0);
7708     if (isAssumedReadNone()) {
7709       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
7710     } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) {
7711       if (isAssumedInaccessibleMemOnly())
7712         Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly));
7713       else if (isAssumedArgMemOnly())
7714         Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly));
7715       else if (isAssumedInaccessibleOrArgMemOnly())
7716         Attrs.push_back(
7717             Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly));
7718     }
7719     assert(Attrs.size() <= 1);
7720   }
7721 
7722   /// See AbstractAttribute::manifest(...).
7723   ChangeStatus manifest(Attributor &A) override {
7724     const IRPosition &IRP = getIRPosition();
7725 
7726     // Check if we would improve the existing attributes first.
7727     SmallVector<Attribute, 4> DeducedAttrs;
7728     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
7729     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
7730           return IRP.hasAttr(Attr.getKindAsEnum(),
7731                              /* IgnoreSubsumingPositions */ true);
7732         }))
7733       return ChangeStatus::UNCHANGED;
7734 
7735     // Clear existing attributes.
7736     IRP.removeAttrs(AttrKinds);
7737     if (isAssumedReadNone())
7738       IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds);
7739 
7740     // Use the generic manifest method.
7741     return IRAttribute::manifest(A);
7742   }
7743 
7744   /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...).
7745   bool checkForAllAccessesToMemoryKind(
7746       function_ref<bool(const Instruction *, const Value *, AccessKind,
7747                         MemoryLocationsKind)>
7748           Pred,
7749       MemoryLocationsKind RequestedMLK) const override {
7750     if (!isValidState())
7751       return false;
7752 
7753     MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation();
7754     if (AssumedMLK == NO_LOCATIONS)
7755       return true;
7756 
7757     unsigned Idx = 0;
7758     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS;
7759          CurMLK *= 2, ++Idx) {
7760       if (CurMLK & RequestedMLK)
7761         continue;
7762 
7763       if (const AccessSet *Accesses = AccessKind2Accesses[Idx])
7764         for (const AccessInfo &AI : *Accesses)
7765           if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK))
7766             return false;
7767     }
7768 
7769     return true;
7770   }
7771 
7772   ChangeStatus indicatePessimisticFixpoint() override {
7773     // If we give up and indicate a pessimistic fixpoint this instruction will
7774     // become an access for all potential access kinds:
7775     // TODO: Add pointers for argmemonly and globals to improve the results of
7776     //       checkForAllAccessesToMemoryKind.
7777     bool Changed = false;
7778     MemoryLocationsKind KnownMLK = getKnown();
7779     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
7780     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
7781       if (!(CurMLK & KnownMLK))
7782         updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed,
7783                                   getAccessKindFromInst(I));
7784     return AAMemoryLocation::indicatePessimisticFixpoint();
7785   }
7786 
7787 protected:
7788   /// Helper struct to tie together an instruction that has a read or write
7789   /// effect with the pointer it accesses (if any).
7790   struct AccessInfo {
7791 
7792     /// The instruction that caused the access.
7793     const Instruction *I;
7794 
7795     /// The base pointer that is accessed, or null if unknown.
7796     const Value *Ptr;
7797 
7798     /// The kind of access (read/write/read+write).
7799     AccessKind Kind;
7800 
7801     bool operator==(const AccessInfo &RHS) const {
7802       return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind;
7803     }
7804     bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const {
7805       if (LHS.I != RHS.I)
7806         return LHS.I < RHS.I;
7807       if (LHS.Ptr != RHS.Ptr)
7808         return LHS.Ptr < RHS.Ptr;
7809       if (LHS.Kind != RHS.Kind)
7810         return LHS.Kind < RHS.Kind;
7811       return false;
7812     }
7813   };
7814 
7815   /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the
7816   /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind.
7817   using AccessSet = SmallSet<AccessInfo, 2, AccessInfo>;
7818   AccessSet *AccessKind2Accesses[llvm::CTLog2<VALID_STATE>()];
7819 
7820   /// Categorize the pointer arguments of CB that might access memory in
7821   /// AccessedLoc and update the state and access map accordingly.
7822   void
7823   categorizeArgumentPointerLocations(Attributor &A, CallBase &CB,
7824                                      AAMemoryLocation::StateType &AccessedLocs,
7825                                      bool &Changed);
7826 
7827   /// Return the kind(s) of location that may be accessed by \p V.
7828   AAMemoryLocation::MemoryLocationsKind
7829   categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
7830 
7831   /// Return the access kind as determined by \p I.
7832   AccessKind getAccessKindFromInst(const Instruction *I) {
7833     AccessKind AK = READ_WRITE;
7834     if (I) {
7835       AK = I->mayReadFromMemory() ? READ : NONE;
7836       AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE));
7837     }
7838     return AK;
7839   }
7840 
7841   /// Update the state \p State and the AccessKind2Accesses given that \p I is
7842   /// an access of kind \p AK to a \p MLK memory location with the access
7843   /// pointer \p Ptr.
7844   void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
7845                                  MemoryLocationsKind MLK, const Instruction *I,
7846                                  const Value *Ptr, bool &Changed,
7847                                  AccessKind AK = READ_WRITE) {
7848 
7849     assert(isPowerOf2_32(MLK) && "Expected a single location set!");
7850     auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)];
7851     if (!Accesses)
7852       Accesses = new (Allocator) AccessSet();
7853     Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second;
7854     State.removeAssumedBits(MLK);
7855   }
7856 
7857   /// Determine the underlying locations kinds for \p Ptr, e.g., globals or
7858   /// arguments, and update the state and access map accordingly.
7859   void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr,
7860                           AAMemoryLocation::StateType &State, bool &Changed);
7861 
7862   /// Used to allocate access sets.
7863   BumpPtrAllocator &Allocator;
7864 
7865   /// The set of IR attributes AAMemoryLocation deals with.
7866   static const Attribute::AttrKind AttrKinds[4];
7867 };
7868 
7869 const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = {
7870     Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly,
7871     Attribute::InaccessibleMemOrArgMemOnly};
7872 
7873 void AAMemoryLocationImpl::categorizePtrValue(
7874     Attributor &A, const Instruction &I, const Value &Ptr,
7875     AAMemoryLocation::StateType &State, bool &Changed) {
7876   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "
7877                     << Ptr << " ["
7878                     << getMemoryLocationsAsStr(State.getAssumed()) << "]\n");
7879 
7880   SmallVector<Value *, 8> Objects;
7881   bool UsedAssumedInformation = false;
7882   if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, *this, &I,
7883                                        UsedAssumedInformation,
7884                                        /* Intraprocedural */ true)) {
7885     LLVM_DEBUG(
7886         dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n");
7887     updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed,
7888                               getAccessKindFromInst(&I));
7889     return;
7890   }
7891 
7892   for (Value *Obj : Objects) {
7893     // TODO: recognize the TBAA used for constant accesses.
7894     MemoryLocationsKind MLK = NO_LOCATIONS;
7895     if (isa<UndefValue>(Obj))
7896       continue;
7897     if (isa<Argument>(Obj)) {
7898       // TODO: For now we do not treat byval arguments as local copies performed
7899       // on the call edge, though, we should. To make that happen we need to
7900       // teach various passes, e.g., DSE, about the copy effect of a byval. That
7901       // would also allow us to mark functions only accessing byval arguments as
7902       // readnone again, atguably their acceses have no effect outside of the
7903       // function, like accesses to allocas.
7904       MLK = NO_ARGUMENT_MEM;
7905     } else if (auto *GV = dyn_cast<GlobalValue>(Obj)) {
7906       // Reading constant memory is not treated as a read "effect" by the
7907       // function attr pass so we won't neither. Constants defined by TBAA are
7908       // similar. (We know we do not write it because it is constant.)
7909       if (auto *GVar = dyn_cast<GlobalVariable>(GV))
7910         if (GVar->isConstant())
7911           continue;
7912 
7913       if (GV->hasLocalLinkage())
7914         MLK = NO_GLOBAL_INTERNAL_MEM;
7915       else
7916         MLK = NO_GLOBAL_EXTERNAL_MEM;
7917     } else if (isa<ConstantPointerNull>(Obj) &&
7918                !NullPointerIsDefined(getAssociatedFunction(),
7919                                      Ptr.getType()->getPointerAddressSpace())) {
7920       continue;
7921     } else if (isa<AllocaInst>(Obj)) {
7922       MLK = NO_LOCAL_MEM;
7923     } else if (const auto *CB = dyn_cast<CallBase>(Obj)) {
7924       const auto &NoAliasAA = A.getAAFor<AANoAlias>(
7925           *this, IRPosition::callsite_returned(*CB), DepClassTy::OPTIONAL);
7926       if (NoAliasAA.isAssumedNoAlias())
7927         MLK = NO_MALLOCED_MEM;
7928       else
7929         MLK = NO_UNKOWN_MEM;
7930     } else {
7931       MLK = NO_UNKOWN_MEM;
7932     }
7933 
7934     assert(MLK != NO_LOCATIONS && "No location specified!");
7935     LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value can be categorized: "
7936                       << *Obj << " -> " << getMemoryLocationsAsStr(MLK)
7937                       << "\n");
7938     updateStateAndAccessesMap(getState(), MLK, &I, Obj, Changed,
7939                               getAccessKindFromInst(&I));
7940   }
7941 
7942   LLVM_DEBUG(
7943       dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: "
7944              << getMemoryLocationsAsStr(State.getAssumed()) << "\n");
7945 }
7946 
7947 void AAMemoryLocationImpl::categorizeArgumentPointerLocations(
7948     Attributor &A, CallBase &CB, AAMemoryLocation::StateType &AccessedLocs,
7949     bool &Changed) {
7950   for (unsigned ArgNo = 0, E = CB.arg_size(); ArgNo < E; ++ArgNo) {
7951 
7952     // Skip non-pointer arguments.
7953     const Value *ArgOp = CB.getArgOperand(ArgNo);
7954     if (!ArgOp->getType()->isPtrOrPtrVectorTy())
7955       continue;
7956 
7957     // Skip readnone arguments.
7958     const IRPosition &ArgOpIRP = IRPosition::callsite_argument(CB, ArgNo);
7959     const auto &ArgOpMemLocationAA =
7960         A.getAAFor<AAMemoryBehavior>(*this, ArgOpIRP, DepClassTy::OPTIONAL);
7961 
7962     if (ArgOpMemLocationAA.isAssumedReadNone())
7963       continue;
7964 
7965     // Categorize potentially accessed pointer arguments as if there was an
7966     // access instruction with them as pointer.
7967     categorizePtrValue(A, CB, *ArgOp, AccessedLocs, Changed);
7968   }
7969 }
7970 
7971 AAMemoryLocation::MemoryLocationsKind
7972 AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
7973                                                   bool &Changed) {
7974   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "
7975                     << I << "\n");
7976 
7977   AAMemoryLocation::StateType AccessedLocs;
7978   AccessedLocs.intersectAssumedBits(NO_LOCATIONS);
7979 
7980   if (auto *CB = dyn_cast<CallBase>(&I)) {
7981 
7982     // First check if we assume any memory is access is visible.
7983     const auto &CBMemLocationAA = A.getAAFor<AAMemoryLocation>(
7984         *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
7985     LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I
7986                       << " [" << CBMemLocationAA << "]\n");
7987 
7988     if (CBMemLocationAA.isAssumedReadNone())
7989       return NO_LOCATIONS;
7990 
7991     if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) {
7992       updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr,
7993                                 Changed, getAccessKindFromInst(&I));
7994       return AccessedLocs.getAssumed();
7995     }
7996 
7997     uint32_t CBAssumedNotAccessedLocs =
7998         CBMemLocationAA.getAssumedNotAccessedLocation();
7999 
8000     // Set the argmemonly and global bit as we handle them separately below.
8001     uint32_t CBAssumedNotAccessedLocsNoArgMem =
8002         CBAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM;
8003 
8004     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
8005       if (CBAssumedNotAccessedLocsNoArgMem & CurMLK)
8006         continue;
8007       updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed,
8008                                 getAccessKindFromInst(&I));
8009     }
8010 
8011     // Now handle global memory if it might be accessed. This is slightly tricky
8012     // as NO_GLOBAL_MEM has multiple bits set.
8013     bool HasGlobalAccesses = ((~CBAssumedNotAccessedLocs) & NO_GLOBAL_MEM);
8014     if (HasGlobalAccesses) {
8015       auto AccessPred = [&](const Instruction *, const Value *Ptr,
8016                             AccessKind Kind, MemoryLocationsKind MLK) {
8017         updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed,
8018                                   getAccessKindFromInst(&I));
8019         return true;
8020       };
8021       if (!CBMemLocationAA.checkForAllAccessesToMemoryKind(
8022               AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false)))
8023         return AccessedLocs.getWorstState();
8024     }
8025 
8026     LLVM_DEBUG(
8027         dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "
8028                << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
8029 
8030     // Now handle argument memory if it might be accessed.
8031     bool HasArgAccesses = ((~CBAssumedNotAccessedLocs) & NO_ARGUMENT_MEM);
8032     if (HasArgAccesses)
8033       categorizeArgumentPointerLocations(A, *CB, AccessedLocs, Changed);
8034 
8035     LLVM_DEBUG(
8036         dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "
8037                << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
8038 
8039     return AccessedLocs.getAssumed();
8040   }
8041 
8042   if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) {
8043     LLVM_DEBUG(
8044         dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "
8045                << I << " [" << *Ptr << "]\n");
8046     categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed);
8047     return AccessedLocs.getAssumed();
8048   }
8049 
8050   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "
8051                     << I << "\n");
8052   updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed,
8053                             getAccessKindFromInst(&I));
8054   return AccessedLocs.getAssumed();
8055 }
8056 
8057 /// An AA to represent the memory behavior function attributes.
8058 struct AAMemoryLocationFunction final : public AAMemoryLocationImpl {
8059   AAMemoryLocationFunction(const IRPosition &IRP, Attributor &A)
8060       : AAMemoryLocationImpl(IRP, A) {}
8061 
8062   /// See AbstractAttribute::updateImpl(Attributor &A).
8063   virtual ChangeStatus updateImpl(Attributor &A) override {
8064 
8065     const auto &MemBehaviorAA =
8066         A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
8067     if (MemBehaviorAA.isAssumedReadNone()) {
8068       if (MemBehaviorAA.isKnownReadNone())
8069         return indicateOptimisticFixpoint();
8070       assert(isAssumedReadNone() &&
8071              "AAMemoryLocation was not read-none but AAMemoryBehavior was!");
8072       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
8073       return ChangeStatus::UNCHANGED;
8074     }
8075 
8076     // The current assumed state used to determine a change.
8077     auto AssumedState = getAssumed();
8078     bool Changed = false;
8079 
8080     auto CheckRWInst = [&](Instruction &I) {
8081       MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed);
8082       LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I
8083                         << ": " << getMemoryLocationsAsStr(MLK) << "\n");
8084       removeAssumedBits(inverseLocation(MLK, false, false));
8085       // Stop once only the valid bit set in the *not assumed location*, thus
8086       // once we don't actually exclude any memory locations in the state.
8087       return getAssumedNotAccessedLocation() != VALID_STATE;
8088     };
8089 
8090     bool UsedAssumedInformation = false;
8091     if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
8092                                             UsedAssumedInformation))
8093       return indicatePessimisticFixpoint();
8094 
8095     Changed |= AssumedState != getAssumed();
8096     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
8097   }
8098 
8099   /// See AbstractAttribute::trackStatistics()
8100   void trackStatistics() const override {
8101     if (isAssumedReadNone())
8102       STATS_DECLTRACK_FN_ATTR(readnone)
8103     else if (isAssumedArgMemOnly())
8104       STATS_DECLTRACK_FN_ATTR(argmemonly)
8105     else if (isAssumedInaccessibleMemOnly())
8106       STATS_DECLTRACK_FN_ATTR(inaccessiblememonly)
8107     else if (isAssumedInaccessibleOrArgMemOnly())
8108       STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly)
8109   }
8110 };
8111 
8112 /// AAMemoryLocation attribute for call sites.
8113 struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
8114   AAMemoryLocationCallSite(const IRPosition &IRP, Attributor &A)
8115       : AAMemoryLocationImpl(IRP, A) {}
8116 
8117   /// See AbstractAttribute::initialize(...).
8118   void initialize(Attributor &A) override {
8119     AAMemoryLocationImpl::initialize(A);
8120     Function *F = getAssociatedFunction();
8121     if (!F || F->isDeclaration())
8122       indicatePessimisticFixpoint();
8123   }
8124 
8125   /// See AbstractAttribute::updateImpl(...).
8126   ChangeStatus updateImpl(Attributor &A) override {
8127     // TODO: Once we have call site specific value information we can provide
8128     //       call site specific liveness liveness information and then it makes
8129     //       sense to specialize attributes for call sites arguments instead of
8130     //       redirecting requests to the callee argument.
8131     Function *F = getAssociatedFunction();
8132     const IRPosition &FnPos = IRPosition::function(*F);
8133     auto &FnAA =
8134         A.getAAFor<AAMemoryLocation>(*this, FnPos, DepClassTy::REQUIRED);
8135     bool Changed = false;
8136     auto AccessPred = [&](const Instruction *I, const Value *Ptr,
8137                           AccessKind Kind, MemoryLocationsKind MLK) {
8138       updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed,
8139                                 getAccessKindFromInst(I));
8140       return true;
8141     };
8142     if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))
8143       return indicatePessimisticFixpoint();
8144     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
8145   }
8146 
8147   /// See AbstractAttribute::trackStatistics()
8148   void trackStatistics() const override {
8149     if (isAssumedReadNone())
8150       STATS_DECLTRACK_CS_ATTR(readnone)
8151   }
8152 };
8153 } // namespace
8154 
8155 /// ------------------ Value Constant Range Attribute -------------------------
8156 
8157 namespace {
8158 struct AAValueConstantRangeImpl : AAValueConstantRange {
8159   using StateType = IntegerRangeState;
8160   AAValueConstantRangeImpl(const IRPosition &IRP, Attributor &A)
8161       : AAValueConstantRange(IRP, A) {}
8162 
8163   /// See AbstractAttribute::initialize(..).
8164   void initialize(Attributor &A) override {
8165     if (A.hasSimplificationCallback(getIRPosition())) {
8166       indicatePessimisticFixpoint();
8167       return;
8168     }
8169 
8170     // Intersect a range given by SCEV.
8171     intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
8172 
8173     // Intersect a range given by LVI.
8174     intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
8175   }
8176 
8177   /// See AbstractAttribute::getAsStr().
8178   const std::string getAsStr() const override {
8179     std::string Str;
8180     llvm::raw_string_ostream OS(Str);
8181     OS << "range(" << getBitWidth() << ")<";
8182     getKnown().print(OS);
8183     OS << " / ";
8184     getAssumed().print(OS);
8185     OS << ">";
8186     return OS.str();
8187   }
8188 
8189   /// Helper function to get a SCEV expr for the associated value at program
8190   /// point \p I.
8191   const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
8192     if (!getAnchorScope())
8193       return nullptr;
8194 
8195     ScalarEvolution *SE =
8196         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
8197             *getAnchorScope());
8198 
8199     LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
8200         *getAnchorScope());
8201 
8202     if (!SE || !LI)
8203       return nullptr;
8204 
8205     const SCEV *S = SE->getSCEV(&getAssociatedValue());
8206     if (!I)
8207       return S;
8208 
8209     return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
8210   }
8211 
8212   /// Helper function to get a range from SCEV for the associated value at
8213   /// program point \p I.
8214   ConstantRange getConstantRangeFromSCEV(Attributor &A,
8215                                          const Instruction *I = nullptr) const {
8216     if (!getAnchorScope())
8217       return getWorstState(getBitWidth());
8218 
8219     ScalarEvolution *SE =
8220         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
8221             *getAnchorScope());
8222 
8223     const SCEV *S = getSCEV(A, I);
8224     if (!SE || !S)
8225       return getWorstState(getBitWidth());
8226 
8227     return SE->getUnsignedRange(S);
8228   }
8229 
8230   /// Helper function to get a range from LVI for the associated value at
8231   /// program point \p I.
8232   ConstantRange
8233   getConstantRangeFromLVI(Attributor &A,
8234                           const Instruction *CtxI = nullptr) const {
8235     if (!getAnchorScope())
8236       return getWorstState(getBitWidth());
8237 
8238     LazyValueInfo *LVI =
8239         A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
8240             *getAnchorScope());
8241 
8242     if (!LVI || !CtxI)
8243       return getWorstState(getBitWidth());
8244     return LVI->getConstantRange(&getAssociatedValue(),
8245                                  const_cast<Instruction *>(CtxI));
8246   }
8247 
8248   /// Return true if \p CtxI is valid for querying outside analyses.
8249   /// This basically makes sure we do not ask intra-procedural analysis
8250   /// about a context in the wrong function or a context that violates
8251   /// dominance assumptions they might have. The \p AllowAACtxI flag indicates
8252   /// if the original context of this AA is OK or should be considered invalid.
8253   bool isValidCtxInstructionForOutsideAnalysis(Attributor &A,
8254                                                const Instruction *CtxI,
8255                                                bool AllowAACtxI) const {
8256     if (!CtxI || (!AllowAACtxI && CtxI == getCtxI()))
8257       return false;
8258 
8259     // Our context might be in a different function, neither intra-procedural
8260     // analysis (ScalarEvolution nor LazyValueInfo) can handle that.
8261     if (!AA::isValidInScope(getAssociatedValue(), CtxI->getFunction()))
8262       return false;
8263 
8264     // If the context is not dominated by the value there are paths to the
8265     // context that do not define the value. This cannot be handled by
8266     // LazyValueInfo so we need to bail.
8267     if (auto *I = dyn_cast<Instruction>(&getAssociatedValue())) {
8268       InformationCache &InfoCache = A.getInfoCache();
8269       const DominatorTree *DT =
8270           InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
8271               *I->getFunction());
8272       return DT && DT->dominates(I, CtxI);
8273     }
8274 
8275     return true;
8276   }
8277 
8278   /// See AAValueConstantRange::getKnownConstantRange(..).
8279   ConstantRange
8280   getKnownConstantRange(Attributor &A,
8281                         const Instruction *CtxI = nullptr) const override {
8282     if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
8283                                                  /* AllowAACtxI */ false))
8284       return getKnown();
8285 
8286     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
8287     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
8288     return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
8289   }
8290 
8291   /// See AAValueConstantRange::getAssumedConstantRange(..).
8292   ConstantRange
8293   getAssumedConstantRange(Attributor &A,
8294                           const Instruction *CtxI = nullptr) const override {
8295     // TODO: Make SCEV use Attributor assumption.
8296     //       We may be able to bound a variable range via assumptions in
8297     //       Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
8298     //       evolve to x^2 + x, then we can say that y is in [2, 12].
8299     if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
8300                                                  /* AllowAACtxI */ false))
8301       return getAssumed();
8302 
8303     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
8304     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
8305     return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
8306   }
8307 
8308   /// Helper function to create MDNode for range metadata.
8309   static MDNode *
8310   getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
8311                             const ConstantRange &AssumedConstantRange) {
8312     Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
8313                                   Ty, AssumedConstantRange.getLower())),
8314                               ConstantAsMetadata::get(ConstantInt::get(
8315                                   Ty, AssumedConstantRange.getUpper()))};
8316     return MDNode::get(Ctx, LowAndHigh);
8317   }
8318 
8319   /// Return true if \p Assumed is included in \p KnownRanges.
8320   static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
8321 
8322     if (Assumed.isFullSet())
8323       return false;
8324 
8325     if (!KnownRanges)
8326       return true;
8327 
8328     // If multiple ranges are annotated in IR, we give up to annotate assumed
8329     // range for now.
8330 
8331     // TODO:  If there exists a known range which containts assumed range, we
8332     // can say assumed range is better.
8333     if (KnownRanges->getNumOperands() > 2)
8334       return false;
8335 
8336     ConstantInt *Lower =
8337         mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
8338     ConstantInt *Upper =
8339         mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
8340 
8341     ConstantRange Known(Lower->getValue(), Upper->getValue());
8342     return Known.contains(Assumed) && Known != Assumed;
8343   }
8344 
8345   /// Helper function to set range metadata.
8346   static bool
8347   setRangeMetadataIfisBetterRange(Instruction *I,
8348                                   const ConstantRange &AssumedConstantRange) {
8349     auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
8350     if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
8351       if (!AssumedConstantRange.isEmptySet()) {
8352         I->setMetadata(LLVMContext::MD_range,
8353                        getMDNodeForConstantRange(I->getType(), I->getContext(),
8354                                                  AssumedConstantRange));
8355         return true;
8356       }
8357     }
8358     return false;
8359   }
8360 
8361   /// See AbstractAttribute::manifest()
8362   ChangeStatus manifest(Attributor &A) override {
8363     ChangeStatus Changed = ChangeStatus::UNCHANGED;
8364     ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
8365     assert(!AssumedConstantRange.isFullSet() && "Invalid state");
8366 
8367     auto &V = getAssociatedValue();
8368     if (!AssumedConstantRange.isEmptySet() &&
8369         !AssumedConstantRange.isSingleElement()) {
8370       if (Instruction *I = dyn_cast<Instruction>(&V)) {
8371         assert(I == getCtxI() && "Should not annotate an instruction which is "
8372                                  "not the context instruction");
8373         if (isa<CallInst>(I) || isa<LoadInst>(I))
8374           if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
8375             Changed = ChangeStatus::CHANGED;
8376       }
8377     }
8378 
8379     return Changed;
8380   }
8381 };
8382 
8383 struct AAValueConstantRangeArgument final
8384     : AAArgumentFromCallSiteArguments<
8385           AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
8386           true /* BridgeCallBaseContext */> {
8387   using Base = AAArgumentFromCallSiteArguments<
8388       AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
8389       true /* BridgeCallBaseContext */>;
8390   AAValueConstantRangeArgument(const IRPosition &IRP, Attributor &A)
8391       : Base(IRP, A) {}
8392 
8393   /// See AbstractAttribute::initialize(..).
8394   void initialize(Attributor &A) override {
8395     if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
8396       indicatePessimisticFixpoint();
8397     } else {
8398       Base::initialize(A);
8399     }
8400   }
8401 
8402   /// See AbstractAttribute::trackStatistics()
8403   void trackStatistics() const override {
8404     STATS_DECLTRACK_ARG_ATTR(value_range)
8405   }
8406 };
8407 
8408 struct AAValueConstantRangeReturned
8409     : AAReturnedFromReturnedValues<AAValueConstantRange,
8410                                    AAValueConstantRangeImpl,
8411                                    AAValueConstantRangeImpl::StateType,
8412                                    /* PropogateCallBaseContext */ true> {
8413   using Base =
8414       AAReturnedFromReturnedValues<AAValueConstantRange,
8415                                    AAValueConstantRangeImpl,
8416                                    AAValueConstantRangeImpl::StateType,
8417                                    /* PropogateCallBaseContext */ true>;
8418   AAValueConstantRangeReturned(const IRPosition &IRP, Attributor &A)
8419       : Base(IRP, A) {}
8420 
8421   /// See AbstractAttribute::initialize(...).
8422   void initialize(Attributor &A) override {}
8423 
8424   /// See AbstractAttribute::trackStatistics()
8425   void trackStatistics() const override {
8426     STATS_DECLTRACK_FNRET_ATTR(value_range)
8427   }
8428 };
8429 
8430 struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
8431   AAValueConstantRangeFloating(const IRPosition &IRP, Attributor &A)
8432       : AAValueConstantRangeImpl(IRP, A) {}
8433 
8434   /// See AbstractAttribute::initialize(...).
8435   void initialize(Attributor &A) override {
8436     AAValueConstantRangeImpl::initialize(A);
8437     if (isAtFixpoint())
8438       return;
8439 
8440     Value &V = getAssociatedValue();
8441 
8442     if (auto *C = dyn_cast<ConstantInt>(&V)) {
8443       unionAssumed(ConstantRange(C->getValue()));
8444       indicateOptimisticFixpoint();
8445       return;
8446     }
8447 
8448     if (isa<UndefValue>(&V)) {
8449       // Collapse the undef state to 0.
8450       unionAssumed(ConstantRange(APInt(getBitWidth(), 0)));
8451       indicateOptimisticFixpoint();
8452       return;
8453     }
8454 
8455     if (isa<CallBase>(&V))
8456       return;
8457 
8458     if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V))
8459       return;
8460 
8461     // If it is a load instruction with range metadata, use it.
8462     if (LoadInst *LI = dyn_cast<LoadInst>(&V))
8463       if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
8464         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
8465         return;
8466       }
8467 
8468     // We can work with PHI and select instruction as we traverse their operands
8469     // during update.
8470     if (isa<SelectInst>(V) || isa<PHINode>(V))
8471       return;
8472 
8473     // Otherwise we give up.
8474     indicatePessimisticFixpoint();
8475 
8476     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
8477                       << getAssociatedValue() << "\n");
8478   }
8479 
8480   bool calculateBinaryOperator(
8481       Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
8482       const Instruction *CtxI,
8483       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8484     Value *LHS = BinOp->getOperand(0);
8485     Value *RHS = BinOp->getOperand(1);
8486 
8487     // Simplify the operands first.
8488     bool UsedAssumedInformation = false;
8489     const auto &SimplifiedLHS =
8490         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8491                                *this, UsedAssumedInformation);
8492     if (!SimplifiedLHS.hasValue())
8493       return true;
8494     if (!SimplifiedLHS.getValue())
8495       return false;
8496     LHS = *SimplifiedLHS;
8497 
8498     const auto &SimplifiedRHS =
8499         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8500                                *this, UsedAssumedInformation);
8501     if (!SimplifiedRHS.hasValue())
8502       return true;
8503     if (!SimplifiedRHS.getValue())
8504       return false;
8505     RHS = *SimplifiedRHS;
8506 
8507     // TODO: Allow non integers as well.
8508     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8509       return false;
8510 
8511     auto &LHSAA = A.getAAFor<AAValueConstantRange>(
8512         *this, IRPosition::value(*LHS, getCallBaseContext()),
8513         DepClassTy::REQUIRED);
8514     QuerriedAAs.push_back(&LHSAA);
8515     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
8516 
8517     auto &RHSAA = A.getAAFor<AAValueConstantRange>(
8518         *this, IRPosition::value(*RHS, getCallBaseContext()),
8519         DepClassTy::REQUIRED);
8520     QuerriedAAs.push_back(&RHSAA);
8521     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
8522 
8523     auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
8524 
8525     T.unionAssumed(AssumedRange);
8526 
8527     // TODO: Track a known state too.
8528 
8529     return T.isValidState();
8530   }
8531 
8532   bool calculateCastInst(
8533       Attributor &A, CastInst *CastI, IntegerRangeState &T,
8534       const Instruction *CtxI,
8535       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8536     assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
8537     // TODO: Allow non integers as well.
8538     Value *OpV = CastI->getOperand(0);
8539 
8540     // Simplify the operand first.
8541     bool UsedAssumedInformation = false;
8542     const auto &SimplifiedOpV =
8543         A.getAssumedSimplified(IRPosition::value(*OpV, getCallBaseContext()),
8544                                *this, UsedAssumedInformation);
8545     if (!SimplifiedOpV.hasValue())
8546       return true;
8547     if (!SimplifiedOpV.getValue())
8548       return false;
8549     OpV = *SimplifiedOpV;
8550 
8551     if (!OpV->getType()->isIntegerTy())
8552       return false;
8553 
8554     auto &OpAA = A.getAAFor<AAValueConstantRange>(
8555         *this, IRPosition::value(*OpV, getCallBaseContext()),
8556         DepClassTy::REQUIRED);
8557     QuerriedAAs.push_back(&OpAA);
8558     T.unionAssumed(
8559         OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
8560     return T.isValidState();
8561   }
8562 
8563   bool
8564   calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
8565                    const Instruction *CtxI,
8566                    SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8567     Value *LHS = CmpI->getOperand(0);
8568     Value *RHS = CmpI->getOperand(1);
8569 
8570     // Simplify the operands first.
8571     bool UsedAssumedInformation = false;
8572     const auto &SimplifiedLHS =
8573         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8574                                *this, UsedAssumedInformation);
8575     if (!SimplifiedLHS.hasValue())
8576       return true;
8577     if (!SimplifiedLHS.getValue())
8578       return false;
8579     LHS = *SimplifiedLHS;
8580 
8581     const auto &SimplifiedRHS =
8582         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8583                                *this, UsedAssumedInformation);
8584     if (!SimplifiedRHS.hasValue())
8585       return true;
8586     if (!SimplifiedRHS.getValue())
8587       return false;
8588     RHS = *SimplifiedRHS;
8589 
8590     // TODO: Allow non integers as well.
8591     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8592       return false;
8593 
8594     auto &LHSAA = A.getAAFor<AAValueConstantRange>(
8595         *this, IRPosition::value(*LHS, getCallBaseContext()),
8596         DepClassTy::REQUIRED);
8597     QuerriedAAs.push_back(&LHSAA);
8598     auto &RHSAA = A.getAAFor<AAValueConstantRange>(
8599         *this, IRPosition::value(*RHS, getCallBaseContext()),
8600         DepClassTy::REQUIRED);
8601     QuerriedAAs.push_back(&RHSAA);
8602     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
8603     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
8604 
8605     // If one of them is empty set, we can't decide.
8606     if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
8607       return true;
8608 
8609     bool MustTrue = false, MustFalse = false;
8610 
8611     auto AllowedRegion =
8612         ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
8613 
8614     if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
8615       MustFalse = true;
8616 
8617     if (LHSAARange.icmp(CmpI->getPredicate(), RHSAARange))
8618       MustTrue = true;
8619 
8620     assert((!MustTrue || !MustFalse) &&
8621            "Either MustTrue or MustFalse should be false!");
8622 
8623     if (MustTrue)
8624       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
8625     else if (MustFalse)
8626       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
8627     else
8628       T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
8629 
8630     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
8631                       << " " << RHSAA << "\n");
8632 
8633     // TODO: Track a known state too.
8634     return T.isValidState();
8635   }
8636 
8637   /// See AbstractAttribute::updateImpl(...).
8638   ChangeStatus updateImpl(Attributor &A) override {
8639     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
8640                             IntegerRangeState &T, bool Stripped) -> bool {
8641       Instruction *I = dyn_cast<Instruction>(&V);
8642       if (!I || isa<CallBase>(I)) {
8643 
8644         // Simplify the operand first.
8645         bool UsedAssumedInformation = false;
8646         const auto &SimplifiedOpV =
8647             A.getAssumedSimplified(IRPosition::value(V, getCallBaseContext()),
8648                                    *this, UsedAssumedInformation);
8649         if (!SimplifiedOpV.hasValue())
8650           return true;
8651         if (!SimplifiedOpV.getValue())
8652           return false;
8653         Value *VPtr = *SimplifiedOpV;
8654 
8655         // If the value is not instruction, we query AA to Attributor.
8656         const auto &AA = A.getAAFor<AAValueConstantRange>(
8657             *this, IRPosition::value(*VPtr, getCallBaseContext()),
8658             DepClassTy::REQUIRED);
8659 
8660         // Clamp operator is not used to utilize a program point CtxI.
8661         T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
8662 
8663         return T.isValidState();
8664       }
8665 
8666       SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
8667       if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
8668         if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
8669           return false;
8670       } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
8671         if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
8672           return false;
8673       } else if (auto *CastI = dyn_cast<CastInst>(I)) {
8674         if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
8675           return false;
8676       } else {
8677         // Give up with other instructions.
8678         // TODO: Add other instructions
8679 
8680         T.indicatePessimisticFixpoint();
8681         return false;
8682       }
8683 
8684       // Catch circular reasoning in a pessimistic way for now.
8685       // TODO: Check how the range evolves and if we stripped anything, see also
8686       //       AADereferenceable or AAAlign for similar situations.
8687       for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
8688         if (QueriedAA != this)
8689           continue;
8690         // If we are in a stady state we do not need to worry.
8691         if (T.getAssumed() == getState().getAssumed())
8692           continue;
8693         T.indicatePessimisticFixpoint();
8694       }
8695 
8696       return T.isValidState();
8697     };
8698 
8699     IntegerRangeState T(getBitWidth());
8700 
8701     bool UsedAssumedInformation = false;
8702     if (!genericValueTraversal<IntegerRangeState>(A, getIRPosition(), *this, T,
8703                                                   VisitValueCB, getCtxI(),
8704                                                   UsedAssumedInformation,
8705                                                   /* UseValueSimplify */ false))
8706       return indicatePessimisticFixpoint();
8707 
8708     // Ensure that long def-use chains can't cause circular reasoning either by
8709     // introducing a cutoff below.
8710     if (clampStateAndIndicateChange(getState(), T) == ChangeStatus::UNCHANGED)
8711       return ChangeStatus::UNCHANGED;
8712     if (++NumChanges > MaxNumChanges) {
8713       LLVM_DEBUG(dbgs() << "[AAValueConstantRange] performed " << NumChanges
8714                         << " but only " << MaxNumChanges
8715                         << " are allowed to avoid cyclic reasoning.");
8716       return indicatePessimisticFixpoint();
8717     }
8718     return ChangeStatus::CHANGED;
8719   }
8720 
8721   /// See AbstractAttribute::trackStatistics()
8722   void trackStatistics() const override {
8723     STATS_DECLTRACK_FLOATING_ATTR(value_range)
8724   }
8725 
8726   /// Tracker to bail after too many widening steps of the constant range.
8727   int NumChanges = 0;
8728 
8729   /// Upper bound for the number of allowed changes (=widening steps) for the
8730   /// constant range before we give up.
8731   static constexpr int MaxNumChanges = 5;
8732 };
8733 
8734 struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
8735   AAValueConstantRangeFunction(const IRPosition &IRP, Attributor &A)
8736       : AAValueConstantRangeImpl(IRP, A) {}
8737 
8738   /// See AbstractAttribute::initialize(...).
8739   ChangeStatus updateImpl(Attributor &A) override {
8740     llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
8741                      "not be called");
8742   }
8743 
8744   /// See AbstractAttribute::trackStatistics()
8745   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
8746 };
8747 
8748 struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
8749   AAValueConstantRangeCallSite(const IRPosition &IRP, Attributor &A)
8750       : AAValueConstantRangeFunction(IRP, A) {}
8751 
8752   /// See AbstractAttribute::trackStatistics()
8753   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
8754 };
8755 
8756 struct AAValueConstantRangeCallSiteReturned
8757     : AACallSiteReturnedFromReturned<AAValueConstantRange,
8758                                      AAValueConstantRangeImpl,
8759                                      AAValueConstantRangeImpl::StateType,
8760                                      /* IntroduceCallBaseContext */ true> {
8761   AAValueConstantRangeCallSiteReturned(const IRPosition &IRP, Attributor &A)
8762       : AACallSiteReturnedFromReturned<AAValueConstantRange,
8763                                        AAValueConstantRangeImpl,
8764                                        AAValueConstantRangeImpl::StateType,
8765                                        /* IntroduceCallBaseContext */ true>(IRP,
8766                                                                             A) {
8767   }
8768 
8769   /// See AbstractAttribute::initialize(...).
8770   void initialize(Attributor &A) override {
8771     // If it is a load instruction with range metadata, use the metadata.
8772     if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
8773       if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
8774         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
8775 
8776     AAValueConstantRangeImpl::initialize(A);
8777   }
8778 
8779   /// See AbstractAttribute::trackStatistics()
8780   void trackStatistics() const override {
8781     STATS_DECLTRACK_CSRET_ATTR(value_range)
8782   }
8783 };
8784 struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
8785   AAValueConstantRangeCallSiteArgument(const IRPosition &IRP, Attributor &A)
8786       : AAValueConstantRangeFloating(IRP, A) {}
8787 
8788   /// See AbstractAttribute::manifest()
8789   ChangeStatus manifest(Attributor &A) override {
8790     return ChangeStatus::UNCHANGED;
8791   }
8792 
8793   /// See AbstractAttribute::trackStatistics()
8794   void trackStatistics() const override {
8795     STATS_DECLTRACK_CSARG_ATTR(value_range)
8796   }
8797 };
8798 } // namespace
8799 
8800 /// ------------------ Potential Values Attribute -------------------------
8801 
8802 namespace {
8803 struct AAPotentialValuesImpl : AAPotentialValues {
8804   using StateType = PotentialConstantIntValuesState;
8805 
8806   AAPotentialValuesImpl(const IRPosition &IRP, Attributor &A)
8807       : AAPotentialValues(IRP, A) {}
8808 
8809   /// See AbstractAttribute::initialize(..).
8810   void initialize(Attributor &A) override {
8811     if (A.hasSimplificationCallback(getIRPosition()))
8812       indicatePessimisticFixpoint();
8813     else
8814       AAPotentialValues::initialize(A);
8815   }
8816 
8817   /// See AbstractAttribute::getAsStr().
8818   const std::string getAsStr() const override {
8819     std::string Str;
8820     llvm::raw_string_ostream OS(Str);
8821     OS << getState();
8822     return OS.str();
8823   }
8824 
8825   /// See AbstractAttribute::updateImpl(...).
8826   ChangeStatus updateImpl(Attributor &A) override {
8827     return indicatePessimisticFixpoint();
8828   }
8829 };
8830 
8831 struct AAPotentialValuesArgument final
8832     : AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
8833                                       PotentialConstantIntValuesState> {
8834   using Base =
8835       AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
8836                                       PotentialConstantIntValuesState>;
8837   AAPotentialValuesArgument(const IRPosition &IRP, Attributor &A)
8838       : Base(IRP, A) {}
8839 
8840   /// See AbstractAttribute::initialize(..).
8841   void initialize(Attributor &A) override {
8842     if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
8843       indicatePessimisticFixpoint();
8844     } else {
8845       Base::initialize(A);
8846     }
8847   }
8848 
8849   /// See AbstractAttribute::trackStatistics()
8850   void trackStatistics() const override {
8851     STATS_DECLTRACK_ARG_ATTR(potential_values)
8852   }
8853 };
8854 
8855 struct AAPotentialValuesReturned
8856     : AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl> {
8857   using Base =
8858       AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl>;
8859   AAPotentialValuesReturned(const IRPosition &IRP, Attributor &A)
8860       : Base(IRP, A) {}
8861 
8862   /// See AbstractAttribute::trackStatistics()
8863   void trackStatistics() const override {
8864     STATS_DECLTRACK_FNRET_ATTR(potential_values)
8865   }
8866 };
8867 
8868 struct AAPotentialValuesFloating : AAPotentialValuesImpl {
8869   AAPotentialValuesFloating(const IRPosition &IRP, Attributor &A)
8870       : AAPotentialValuesImpl(IRP, A) {}
8871 
8872   /// See AbstractAttribute::initialize(..).
8873   void initialize(Attributor &A) override {
8874     AAPotentialValuesImpl::initialize(A);
8875     if (isAtFixpoint())
8876       return;
8877 
8878     Value &V = getAssociatedValue();
8879 
8880     if (auto *C = dyn_cast<ConstantInt>(&V)) {
8881       unionAssumed(C->getValue());
8882       indicateOptimisticFixpoint();
8883       return;
8884     }
8885 
8886     if (isa<UndefValue>(&V)) {
8887       unionAssumedWithUndef();
8888       indicateOptimisticFixpoint();
8889       return;
8890     }
8891 
8892     if (isa<BinaryOperator>(&V) || isa<ICmpInst>(&V) || isa<CastInst>(&V))
8893       return;
8894 
8895     if (isa<SelectInst>(V) || isa<PHINode>(V) || isa<LoadInst>(V))
8896       return;
8897 
8898     indicatePessimisticFixpoint();
8899 
8900     LLVM_DEBUG(dbgs() << "[AAPotentialValues] We give up: "
8901                       << getAssociatedValue() << "\n");
8902   }
8903 
8904   static bool calculateICmpInst(const ICmpInst *ICI, const APInt &LHS,
8905                                 const APInt &RHS) {
8906     return ICmpInst::compare(LHS, RHS, ICI->getPredicate());
8907   }
8908 
8909   static APInt calculateCastInst(const CastInst *CI, const APInt &Src,
8910                                  uint32_t ResultBitWidth) {
8911     Instruction::CastOps CastOp = CI->getOpcode();
8912     switch (CastOp) {
8913     default:
8914       llvm_unreachable("unsupported or not integer cast");
8915     case Instruction::Trunc:
8916       return Src.trunc(ResultBitWidth);
8917     case Instruction::SExt:
8918       return Src.sext(ResultBitWidth);
8919     case Instruction::ZExt:
8920       return Src.zext(ResultBitWidth);
8921     case Instruction::BitCast:
8922       return Src;
8923     }
8924   }
8925 
8926   static APInt calculateBinaryOperator(const BinaryOperator *BinOp,
8927                                        const APInt &LHS, const APInt &RHS,
8928                                        bool &SkipOperation, bool &Unsupported) {
8929     Instruction::BinaryOps BinOpcode = BinOp->getOpcode();
8930     // Unsupported is set to true when the binary operator is not supported.
8931     // SkipOperation is set to true when UB occur with the given operand pair
8932     // (LHS, RHS).
8933     // TODO: we should look at nsw and nuw keywords to handle operations
8934     //       that create poison or undef value.
8935     switch (BinOpcode) {
8936     default:
8937       Unsupported = true;
8938       return LHS;
8939     case Instruction::Add:
8940       return LHS + RHS;
8941     case Instruction::Sub:
8942       return LHS - RHS;
8943     case Instruction::Mul:
8944       return LHS * RHS;
8945     case Instruction::UDiv:
8946       if (RHS.isZero()) {
8947         SkipOperation = true;
8948         return LHS;
8949       }
8950       return LHS.udiv(RHS);
8951     case Instruction::SDiv:
8952       if (RHS.isZero()) {
8953         SkipOperation = true;
8954         return LHS;
8955       }
8956       return LHS.sdiv(RHS);
8957     case Instruction::URem:
8958       if (RHS.isZero()) {
8959         SkipOperation = true;
8960         return LHS;
8961       }
8962       return LHS.urem(RHS);
8963     case Instruction::SRem:
8964       if (RHS.isZero()) {
8965         SkipOperation = true;
8966         return LHS;
8967       }
8968       return LHS.srem(RHS);
8969     case Instruction::Shl:
8970       return LHS.shl(RHS);
8971     case Instruction::LShr:
8972       return LHS.lshr(RHS);
8973     case Instruction::AShr:
8974       return LHS.ashr(RHS);
8975     case Instruction::And:
8976       return LHS & RHS;
8977     case Instruction::Or:
8978       return LHS | RHS;
8979     case Instruction::Xor:
8980       return LHS ^ RHS;
8981     }
8982   }
8983 
8984   bool calculateBinaryOperatorAndTakeUnion(const BinaryOperator *BinOp,
8985                                            const APInt &LHS, const APInt &RHS) {
8986     bool SkipOperation = false;
8987     bool Unsupported = false;
8988     APInt Result =
8989         calculateBinaryOperator(BinOp, LHS, RHS, SkipOperation, Unsupported);
8990     if (Unsupported)
8991       return false;
8992     // If SkipOperation is true, we can ignore this operand pair (L, R).
8993     if (!SkipOperation)
8994       unionAssumed(Result);
8995     return isValidState();
8996   }
8997 
8998   ChangeStatus updateWithICmpInst(Attributor &A, ICmpInst *ICI) {
8999     auto AssumedBefore = getAssumed();
9000     Value *LHS = ICI->getOperand(0);
9001     Value *RHS = ICI->getOperand(1);
9002 
9003     // Simplify the operands first.
9004     bool UsedAssumedInformation = false;
9005     const auto &SimplifiedLHS =
9006         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
9007                                *this, UsedAssumedInformation);
9008     if (!SimplifiedLHS.hasValue())
9009       return ChangeStatus::UNCHANGED;
9010     if (!SimplifiedLHS.getValue())
9011       return indicatePessimisticFixpoint();
9012     LHS = *SimplifiedLHS;
9013 
9014     const auto &SimplifiedRHS =
9015         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
9016                                *this, UsedAssumedInformation);
9017     if (!SimplifiedRHS.hasValue())
9018       return ChangeStatus::UNCHANGED;
9019     if (!SimplifiedRHS.getValue())
9020       return indicatePessimisticFixpoint();
9021     RHS = *SimplifiedRHS;
9022 
9023     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
9024       return indicatePessimisticFixpoint();
9025 
9026     auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
9027                                                 DepClassTy::REQUIRED);
9028     if (!LHSAA.isValidState())
9029       return indicatePessimisticFixpoint();
9030 
9031     auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
9032                                                 DepClassTy::REQUIRED);
9033     if (!RHSAA.isValidState())
9034       return indicatePessimisticFixpoint();
9035 
9036     const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
9037     const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
9038 
9039     // TODO: make use of undef flag to limit potential values aggressively.
9040     bool MaybeTrue = false, MaybeFalse = false;
9041     const APInt Zero(RHS->getType()->getIntegerBitWidth(), 0);
9042     if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
9043       // The result of any comparison between undefs can be soundly replaced
9044       // with undef.
9045       unionAssumedWithUndef();
9046     } else if (LHSAA.undefIsContained()) {
9047       for (const APInt &R : RHSAAPVS) {
9048         bool CmpResult = calculateICmpInst(ICI, Zero, R);
9049         MaybeTrue |= CmpResult;
9050         MaybeFalse |= !CmpResult;
9051         if (MaybeTrue & MaybeFalse)
9052           return indicatePessimisticFixpoint();
9053       }
9054     } else if (RHSAA.undefIsContained()) {
9055       for (const APInt &L : LHSAAPVS) {
9056         bool CmpResult = calculateICmpInst(ICI, L, Zero);
9057         MaybeTrue |= CmpResult;
9058         MaybeFalse |= !CmpResult;
9059         if (MaybeTrue & MaybeFalse)
9060           return indicatePessimisticFixpoint();
9061       }
9062     } else {
9063       for (const APInt &L : LHSAAPVS) {
9064         for (const APInt &R : RHSAAPVS) {
9065           bool CmpResult = calculateICmpInst(ICI, L, R);
9066           MaybeTrue |= CmpResult;
9067           MaybeFalse |= !CmpResult;
9068           if (MaybeTrue & MaybeFalse)
9069             return indicatePessimisticFixpoint();
9070         }
9071       }
9072     }
9073     if (MaybeTrue)
9074       unionAssumed(APInt(/* numBits */ 1, /* val */ 1));
9075     if (MaybeFalse)
9076       unionAssumed(APInt(/* numBits */ 1, /* val */ 0));
9077     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9078                                          : ChangeStatus::CHANGED;
9079   }
9080 
9081   ChangeStatus updateWithSelectInst(Attributor &A, SelectInst *SI) {
9082     auto AssumedBefore = getAssumed();
9083     Value *LHS = SI->getTrueValue();
9084     Value *RHS = SI->getFalseValue();
9085 
9086     // Simplify the operands first.
9087     bool UsedAssumedInformation = false;
9088     const auto &SimplifiedLHS =
9089         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
9090                                *this, UsedAssumedInformation);
9091     if (!SimplifiedLHS.hasValue())
9092       return ChangeStatus::UNCHANGED;
9093     if (!SimplifiedLHS.getValue())
9094       return indicatePessimisticFixpoint();
9095     LHS = *SimplifiedLHS;
9096 
9097     const auto &SimplifiedRHS =
9098         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
9099                                *this, UsedAssumedInformation);
9100     if (!SimplifiedRHS.hasValue())
9101       return ChangeStatus::UNCHANGED;
9102     if (!SimplifiedRHS.getValue())
9103       return indicatePessimisticFixpoint();
9104     RHS = *SimplifiedRHS;
9105 
9106     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
9107       return indicatePessimisticFixpoint();
9108 
9109     Optional<Constant *> C = A.getAssumedConstant(*SI->getCondition(), *this,
9110                                                   UsedAssumedInformation);
9111 
9112     // Check if we only need one operand.
9113     bool OnlyLeft = false, OnlyRight = false;
9114     if (C.hasValue() && *C && (*C)->isOneValue())
9115       OnlyLeft = true;
9116     else if (C.hasValue() && *C && (*C)->isZeroValue())
9117       OnlyRight = true;
9118 
9119     const AAPotentialValues *LHSAA = nullptr, *RHSAA = nullptr;
9120     if (!OnlyRight) {
9121       LHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
9122                                              DepClassTy::REQUIRED);
9123       if (!LHSAA->isValidState())
9124         return indicatePessimisticFixpoint();
9125     }
9126     if (!OnlyLeft) {
9127       RHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
9128                                              DepClassTy::REQUIRED);
9129       if (!RHSAA->isValidState())
9130         return indicatePessimisticFixpoint();
9131     }
9132 
9133     if (!LHSAA || !RHSAA) {
9134       // select (true/false), lhs, rhs
9135       auto *OpAA = LHSAA ? LHSAA : RHSAA;
9136 
9137       if (OpAA->undefIsContained())
9138         unionAssumedWithUndef();
9139       else
9140         unionAssumed(*OpAA);
9141 
9142     } else if (LHSAA->undefIsContained() && RHSAA->undefIsContained()) {
9143       // select i1 *, undef , undef => undef
9144       unionAssumedWithUndef();
9145     } else {
9146       unionAssumed(*LHSAA);
9147       unionAssumed(*RHSAA);
9148     }
9149     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9150                                          : ChangeStatus::CHANGED;
9151   }
9152 
9153   ChangeStatus updateWithCastInst(Attributor &A, CastInst *CI) {
9154     auto AssumedBefore = getAssumed();
9155     if (!CI->isIntegerCast())
9156       return indicatePessimisticFixpoint();
9157     assert(CI->getNumOperands() == 1 && "Expected cast to be unary!");
9158     uint32_t ResultBitWidth = CI->getDestTy()->getIntegerBitWidth();
9159     Value *Src = CI->getOperand(0);
9160 
9161     // Simplify the operand first.
9162     bool UsedAssumedInformation = false;
9163     const auto &SimplifiedSrc =
9164         A.getAssumedSimplified(IRPosition::value(*Src, getCallBaseContext()),
9165                                *this, UsedAssumedInformation);
9166     if (!SimplifiedSrc.hasValue())
9167       return ChangeStatus::UNCHANGED;
9168     if (!SimplifiedSrc.getValue())
9169       return indicatePessimisticFixpoint();
9170     Src = *SimplifiedSrc;
9171 
9172     auto &SrcAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*Src),
9173                                                 DepClassTy::REQUIRED);
9174     if (!SrcAA.isValidState())
9175       return indicatePessimisticFixpoint();
9176     const DenseSet<APInt> &SrcAAPVS = SrcAA.getAssumedSet();
9177     if (SrcAA.undefIsContained())
9178       unionAssumedWithUndef();
9179     else {
9180       for (const APInt &S : SrcAAPVS) {
9181         APInt T = calculateCastInst(CI, S, ResultBitWidth);
9182         unionAssumed(T);
9183       }
9184     }
9185     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9186                                          : ChangeStatus::CHANGED;
9187   }
9188 
9189   ChangeStatus updateWithBinaryOperator(Attributor &A, BinaryOperator *BinOp) {
9190     auto AssumedBefore = getAssumed();
9191     Value *LHS = BinOp->getOperand(0);
9192     Value *RHS = BinOp->getOperand(1);
9193 
9194     // Simplify the operands first.
9195     bool UsedAssumedInformation = false;
9196     const auto &SimplifiedLHS =
9197         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
9198                                *this, UsedAssumedInformation);
9199     if (!SimplifiedLHS.hasValue())
9200       return ChangeStatus::UNCHANGED;
9201     if (!SimplifiedLHS.getValue())
9202       return indicatePessimisticFixpoint();
9203     LHS = *SimplifiedLHS;
9204 
9205     const auto &SimplifiedRHS =
9206         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
9207                                *this, UsedAssumedInformation);
9208     if (!SimplifiedRHS.hasValue())
9209       return ChangeStatus::UNCHANGED;
9210     if (!SimplifiedRHS.getValue())
9211       return indicatePessimisticFixpoint();
9212     RHS = *SimplifiedRHS;
9213 
9214     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
9215       return indicatePessimisticFixpoint();
9216 
9217     auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
9218                                                 DepClassTy::REQUIRED);
9219     if (!LHSAA.isValidState())
9220       return indicatePessimisticFixpoint();
9221 
9222     auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
9223                                                 DepClassTy::REQUIRED);
9224     if (!RHSAA.isValidState())
9225       return indicatePessimisticFixpoint();
9226 
9227     const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
9228     const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
9229     const APInt Zero = APInt(LHS->getType()->getIntegerBitWidth(), 0);
9230 
9231     // TODO: make use of undef flag to limit potential values aggressively.
9232     if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
9233       if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, Zero))
9234         return indicatePessimisticFixpoint();
9235     } else if (LHSAA.undefIsContained()) {
9236       for (const APInt &R : RHSAAPVS) {
9237         if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, R))
9238           return indicatePessimisticFixpoint();
9239       }
9240     } else if (RHSAA.undefIsContained()) {
9241       for (const APInt &L : LHSAAPVS) {
9242         if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, Zero))
9243           return indicatePessimisticFixpoint();
9244       }
9245     } else {
9246       for (const APInt &L : LHSAAPVS) {
9247         for (const APInt &R : RHSAAPVS) {
9248           if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, R))
9249             return indicatePessimisticFixpoint();
9250         }
9251       }
9252     }
9253     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9254                                          : ChangeStatus::CHANGED;
9255   }
9256 
9257   ChangeStatus updateWithPHINode(Attributor &A, PHINode *PHI) {
9258     auto AssumedBefore = getAssumed();
9259     for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
9260       Value *IncomingValue = PHI->getIncomingValue(u);
9261 
9262       // Simplify the operand first.
9263       bool UsedAssumedInformation = false;
9264       const auto &SimplifiedIncomingValue = A.getAssumedSimplified(
9265           IRPosition::value(*IncomingValue, getCallBaseContext()), *this,
9266           UsedAssumedInformation);
9267       if (!SimplifiedIncomingValue.hasValue())
9268         continue;
9269       if (!SimplifiedIncomingValue.getValue())
9270         return indicatePessimisticFixpoint();
9271       IncomingValue = *SimplifiedIncomingValue;
9272 
9273       auto &PotentialValuesAA = A.getAAFor<AAPotentialValues>(
9274           *this, IRPosition::value(*IncomingValue), DepClassTy::REQUIRED);
9275       if (!PotentialValuesAA.isValidState())
9276         return indicatePessimisticFixpoint();
9277       if (PotentialValuesAA.undefIsContained())
9278         unionAssumedWithUndef();
9279       else
9280         unionAssumed(PotentialValuesAA.getAssumed());
9281     }
9282     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9283                                          : ChangeStatus::CHANGED;
9284   }
9285 
9286   ChangeStatus updateWithLoad(Attributor &A, LoadInst &L) {
9287     if (!L.getType()->isIntegerTy())
9288       return indicatePessimisticFixpoint();
9289 
9290     auto Union = [&](Value &V) {
9291       if (isa<UndefValue>(V)) {
9292         unionAssumedWithUndef();
9293         return true;
9294       }
9295       if (ConstantInt *CI = dyn_cast<ConstantInt>(&V)) {
9296         unionAssumed(CI->getValue());
9297         return true;
9298       }
9299       return false;
9300     };
9301     auto AssumedBefore = getAssumed();
9302 
9303     if (!AAValueSimplifyImpl::handleLoad(A, *this, L, Union))
9304       return indicatePessimisticFixpoint();
9305 
9306     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9307                                          : ChangeStatus::CHANGED;
9308   }
9309 
9310   /// See AbstractAttribute::updateImpl(...).
9311   ChangeStatus updateImpl(Attributor &A) override {
9312     Value &V = getAssociatedValue();
9313     Instruction *I = dyn_cast<Instruction>(&V);
9314 
9315     if (auto *ICI = dyn_cast<ICmpInst>(I))
9316       return updateWithICmpInst(A, ICI);
9317 
9318     if (auto *SI = dyn_cast<SelectInst>(I))
9319       return updateWithSelectInst(A, SI);
9320 
9321     if (auto *CI = dyn_cast<CastInst>(I))
9322       return updateWithCastInst(A, CI);
9323 
9324     if (auto *BinOp = dyn_cast<BinaryOperator>(I))
9325       return updateWithBinaryOperator(A, BinOp);
9326 
9327     if (auto *PHI = dyn_cast<PHINode>(I))
9328       return updateWithPHINode(A, PHI);
9329 
9330     if (auto *L = dyn_cast<LoadInst>(I))
9331       return updateWithLoad(A, *L);
9332 
9333     return indicatePessimisticFixpoint();
9334   }
9335 
9336   /// See AbstractAttribute::trackStatistics()
9337   void trackStatistics() const override {
9338     STATS_DECLTRACK_FLOATING_ATTR(potential_values)
9339   }
9340 };
9341 
9342 struct AAPotentialValuesFunction : AAPotentialValuesImpl {
9343   AAPotentialValuesFunction(const IRPosition &IRP, Attributor &A)
9344       : AAPotentialValuesImpl(IRP, A) {}
9345 
9346   /// See AbstractAttribute::initialize(...).
9347   ChangeStatus updateImpl(Attributor &A) override {
9348     llvm_unreachable("AAPotentialValues(Function|CallSite)::updateImpl will "
9349                      "not be called");
9350   }
9351 
9352   /// See AbstractAttribute::trackStatistics()
9353   void trackStatistics() const override {
9354     STATS_DECLTRACK_FN_ATTR(potential_values)
9355   }
9356 };
9357 
9358 struct AAPotentialValuesCallSite : AAPotentialValuesFunction {
9359   AAPotentialValuesCallSite(const IRPosition &IRP, Attributor &A)
9360       : AAPotentialValuesFunction(IRP, A) {}
9361 
9362   /// See AbstractAttribute::trackStatistics()
9363   void trackStatistics() const override {
9364     STATS_DECLTRACK_CS_ATTR(potential_values)
9365   }
9366 };
9367 
9368 struct AAPotentialValuesCallSiteReturned
9369     : AACallSiteReturnedFromReturned<AAPotentialValues, AAPotentialValuesImpl> {
9370   AAPotentialValuesCallSiteReturned(const IRPosition &IRP, Attributor &A)
9371       : AACallSiteReturnedFromReturned<AAPotentialValues,
9372                                        AAPotentialValuesImpl>(IRP, A) {}
9373 
9374   /// See AbstractAttribute::trackStatistics()
9375   void trackStatistics() const override {
9376     STATS_DECLTRACK_CSRET_ATTR(potential_values)
9377   }
9378 };
9379 
9380 struct AAPotentialValuesCallSiteArgument : AAPotentialValuesFloating {
9381   AAPotentialValuesCallSiteArgument(const IRPosition &IRP, Attributor &A)
9382       : AAPotentialValuesFloating(IRP, A) {}
9383 
9384   /// See AbstractAttribute::initialize(..).
9385   void initialize(Attributor &A) override {
9386     AAPotentialValuesImpl::initialize(A);
9387     if (isAtFixpoint())
9388       return;
9389 
9390     Value &V = getAssociatedValue();
9391 
9392     if (auto *C = dyn_cast<ConstantInt>(&V)) {
9393       unionAssumed(C->getValue());
9394       indicateOptimisticFixpoint();
9395       return;
9396     }
9397 
9398     if (isa<UndefValue>(&V)) {
9399       unionAssumedWithUndef();
9400       indicateOptimisticFixpoint();
9401       return;
9402     }
9403   }
9404 
9405   /// See AbstractAttribute::updateImpl(...).
9406   ChangeStatus updateImpl(Attributor &A) override {
9407     Value &V = getAssociatedValue();
9408     auto AssumedBefore = getAssumed();
9409     auto &AA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(V),
9410                                              DepClassTy::REQUIRED);
9411     const auto &S = AA.getAssumed();
9412     unionAssumed(S);
9413     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9414                                          : ChangeStatus::CHANGED;
9415   }
9416 
9417   /// See AbstractAttribute::trackStatistics()
9418   void trackStatistics() const override {
9419     STATS_DECLTRACK_CSARG_ATTR(potential_values)
9420   }
9421 };
9422 
9423 /// ------------------------ NoUndef Attribute ---------------------------------
9424 struct AANoUndefImpl : AANoUndef {
9425   AANoUndefImpl(const IRPosition &IRP, Attributor &A) : AANoUndef(IRP, A) {}
9426 
9427   /// See AbstractAttribute::initialize(...).
9428   void initialize(Attributor &A) override {
9429     if (getIRPosition().hasAttr({Attribute::NoUndef})) {
9430       indicateOptimisticFixpoint();
9431       return;
9432     }
9433     Value &V = getAssociatedValue();
9434     if (isa<UndefValue>(V))
9435       indicatePessimisticFixpoint();
9436     else if (isa<FreezeInst>(V))
9437       indicateOptimisticFixpoint();
9438     else if (getPositionKind() != IRPosition::IRP_RETURNED &&
9439              isGuaranteedNotToBeUndefOrPoison(&V))
9440       indicateOptimisticFixpoint();
9441     else
9442       AANoUndef::initialize(A);
9443   }
9444 
9445   /// See followUsesInMBEC
9446   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
9447                        AANoUndef::StateType &State) {
9448     const Value *UseV = U->get();
9449     const DominatorTree *DT = nullptr;
9450     AssumptionCache *AC = nullptr;
9451     InformationCache &InfoCache = A.getInfoCache();
9452     if (Function *F = getAnchorScope()) {
9453       DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
9454       AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
9455     }
9456     State.setKnown(isGuaranteedNotToBeUndefOrPoison(UseV, AC, I, DT));
9457     bool TrackUse = false;
9458     // Track use for instructions which must produce undef or poison bits when
9459     // at least one operand contains such bits.
9460     if (isa<CastInst>(*I) || isa<GetElementPtrInst>(*I))
9461       TrackUse = true;
9462     return TrackUse;
9463   }
9464 
9465   /// See AbstractAttribute::getAsStr().
9466   const std::string getAsStr() const override {
9467     return getAssumed() ? "noundef" : "may-undef-or-poison";
9468   }
9469 
9470   ChangeStatus manifest(Attributor &A) override {
9471     // We don't manifest noundef attribute for dead positions because the
9472     // associated values with dead positions would be replaced with undef
9473     // values.
9474     bool UsedAssumedInformation = false;
9475     if (A.isAssumedDead(getIRPosition(), nullptr, nullptr,
9476                         UsedAssumedInformation))
9477       return ChangeStatus::UNCHANGED;
9478     // A position whose simplified value does not have any value is
9479     // considered to be dead. We don't manifest noundef in such positions for
9480     // the same reason above.
9481     if (!A.getAssumedSimplified(getIRPosition(), *this, UsedAssumedInformation)
9482              .hasValue())
9483       return ChangeStatus::UNCHANGED;
9484     return AANoUndef::manifest(A);
9485   }
9486 };
9487 
9488 struct AANoUndefFloating : public AANoUndefImpl {
9489   AANoUndefFloating(const IRPosition &IRP, Attributor &A)
9490       : AANoUndefImpl(IRP, A) {}
9491 
9492   /// See AbstractAttribute::initialize(...).
9493   void initialize(Attributor &A) override {
9494     AANoUndefImpl::initialize(A);
9495     if (!getState().isAtFixpoint())
9496       if (Instruction *CtxI = getCtxI())
9497         followUsesInMBEC(*this, A, getState(), *CtxI);
9498   }
9499 
9500   /// See AbstractAttribute::updateImpl(...).
9501   ChangeStatus updateImpl(Attributor &A) override {
9502     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
9503                             AANoUndef::StateType &T, bool Stripped) -> bool {
9504       const auto &AA = A.getAAFor<AANoUndef>(*this, IRPosition::value(V),
9505                                              DepClassTy::REQUIRED);
9506       if (!Stripped && this == &AA) {
9507         T.indicatePessimisticFixpoint();
9508       } else {
9509         const AANoUndef::StateType &S =
9510             static_cast<const AANoUndef::StateType &>(AA.getState());
9511         T ^= S;
9512       }
9513       return T.isValidState();
9514     };
9515 
9516     StateType T;
9517     bool UsedAssumedInformation = false;
9518     if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
9519                                           VisitValueCB, getCtxI(),
9520                                           UsedAssumedInformation))
9521       return indicatePessimisticFixpoint();
9522 
9523     return clampStateAndIndicateChange(getState(), T);
9524   }
9525 
9526   /// See AbstractAttribute::trackStatistics()
9527   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
9528 };
9529 
9530 struct AANoUndefReturned final
9531     : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl> {
9532   AANoUndefReturned(const IRPosition &IRP, Attributor &A)
9533       : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl>(IRP, A) {}
9534 
9535   /// See AbstractAttribute::trackStatistics()
9536   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
9537 };
9538 
9539 struct AANoUndefArgument final
9540     : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl> {
9541   AANoUndefArgument(const IRPosition &IRP, Attributor &A)
9542       : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl>(IRP, A) {}
9543 
9544   /// See AbstractAttribute::trackStatistics()
9545   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noundef) }
9546 };
9547 
9548 struct AANoUndefCallSiteArgument final : AANoUndefFloating {
9549   AANoUndefCallSiteArgument(const IRPosition &IRP, Attributor &A)
9550       : AANoUndefFloating(IRP, A) {}
9551 
9552   /// See AbstractAttribute::trackStatistics()
9553   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noundef) }
9554 };
9555 
9556 struct AANoUndefCallSiteReturned final
9557     : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl> {
9558   AANoUndefCallSiteReturned(const IRPosition &IRP, Attributor &A)
9559       : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl>(IRP, A) {}
9560 
9561   /// See AbstractAttribute::trackStatistics()
9562   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noundef) }
9563 };
9564 
9565 struct AACallEdgesImpl : public AACallEdges {
9566   AACallEdgesImpl(const IRPosition &IRP, Attributor &A) : AACallEdges(IRP, A) {}
9567 
9568   virtual const SetVector<Function *> &getOptimisticEdges() const override {
9569     return CalledFunctions;
9570   }
9571 
9572   virtual bool hasUnknownCallee() const override { return HasUnknownCallee; }
9573 
9574   virtual bool hasNonAsmUnknownCallee() const override {
9575     return HasUnknownCalleeNonAsm;
9576   }
9577 
9578   const std::string getAsStr() const override {
9579     return "CallEdges[" + std::to_string(HasUnknownCallee) + "," +
9580            std::to_string(CalledFunctions.size()) + "]";
9581   }
9582 
9583   void trackStatistics() const override {}
9584 
9585 protected:
9586   void addCalledFunction(Function *Fn, ChangeStatus &Change) {
9587     if (CalledFunctions.insert(Fn)) {
9588       Change = ChangeStatus::CHANGED;
9589       LLVM_DEBUG(dbgs() << "[AACallEdges] New call edge: " << Fn->getName()
9590                         << "\n");
9591     }
9592   }
9593 
9594   void setHasUnknownCallee(bool NonAsm, ChangeStatus &Change) {
9595     if (!HasUnknownCallee)
9596       Change = ChangeStatus::CHANGED;
9597     if (NonAsm && !HasUnknownCalleeNonAsm)
9598       Change = ChangeStatus::CHANGED;
9599     HasUnknownCalleeNonAsm |= NonAsm;
9600     HasUnknownCallee = true;
9601   }
9602 
9603 private:
9604   /// Optimistic set of functions that might be called by this position.
9605   SetVector<Function *> CalledFunctions;
9606 
9607   /// Is there any call with a unknown callee.
9608   bool HasUnknownCallee = false;
9609 
9610   /// Is there any call with a unknown callee, excluding any inline asm.
9611   bool HasUnknownCalleeNonAsm = false;
9612 };
9613 
9614 struct AACallEdgesCallSite : public AACallEdgesImpl {
9615   AACallEdgesCallSite(const IRPosition &IRP, Attributor &A)
9616       : AACallEdgesImpl(IRP, A) {}
9617   /// See AbstractAttribute::updateImpl(...).
9618   ChangeStatus updateImpl(Attributor &A) override {
9619     ChangeStatus Change = ChangeStatus::UNCHANGED;
9620 
9621     auto VisitValue = [&](Value &V, const Instruction *CtxI, bool &HasUnknown,
9622                           bool Stripped) -> bool {
9623       if (Function *Fn = dyn_cast<Function>(&V)) {
9624         addCalledFunction(Fn, Change);
9625       } else {
9626         LLVM_DEBUG(dbgs() << "[AACallEdges] Unrecognized value: " << V << "\n");
9627         setHasUnknownCallee(true, Change);
9628       }
9629 
9630       // Explore all values.
9631       return true;
9632     };
9633 
9634     // Process any value that we might call.
9635     auto ProcessCalledOperand = [&](Value *V) {
9636       bool DummyValue = false;
9637       bool UsedAssumedInformation = false;
9638       if (!genericValueTraversal<bool>(A, IRPosition::value(*V), *this,
9639                                        DummyValue, VisitValue, nullptr,
9640                                        UsedAssumedInformation, false)) {
9641         // If we haven't gone through all values, assume that there are unknown
9642         // callees.
9643         setHasUnknownCallee(true, Change);
9644       }
9645     };
9646 
9647     CallBase *CB = cast<CallBase>(getCtxI());
9648 
9649     if (CB->isInlineAsm()) {
9650       setHasUnknownCallee(false, Change);
9651       return Change;
9652     }
9653 
9654     // Process callee metadata if available.
9655     if (auto *MD = getCtxI()->getMetadata(LLVMContext::MD_callees)) {
9656       for (auto &Op : MD->operands()) {
9657         Function *Callee = mdconst::dyn_extract_or_null<Function>(Op);
9658         if (Callee)
9659           addCalledFunction(Callee, Change);
9660       }
9661       return Change;
9662     }
9663 
9664     // The most simple case.
9665     ProcessCalledOperand(CB->getCalledOperand());
9666 
9667     // Process callback functions.
9668     SmallVector<const Use *, 4u> CallbackUses;
9669     AbstractCallSite::getCallbackUses(*CB, CallbackUses);
9670     for (const Use *U : CallbackUses)
9671       ProcessCalledOperand(U->get());
9672 
9673     return Change;
9674   }
9675 };
9676 
9677 struct AACallEdgesFunction : public AACallEdgesImpl {
9678   AACallEdgesFunction(const IRPosition &IRP, Attributor &A)
9679       : AACallEdgesImpl(IRP, A) {}
9680 
9681   /// See AbstractAttribute::updateImpl(...).
9682   ChangeStatus updateImpl(Attributor &A) override {
9683     ChangeStatus Change = ChangeStatus::UNCHANGED;
9684 
9685     auto ProcessCallInst = [&](Instruction &Inst) {
9686       CallBase &CB = cast<CallBase>(Inst);
9687 
9688       auto &CBEdges = A.getAAFor<AACallEdges>(
9689           *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
9690       if (CBEdges.hasNonAsmUnknownCallee())
9691         setHasUnknownCallee(true, Change);
9692       if (CBEdges.hasUnknownCallee())
9693         setHasUnknownCallee(false, Change);
9694 
9695       for (Function *F : CBEdges.getOptimisticEdges())
9696         addCalledFunction(F, Change);
9697 
9698       return true;
9699     };
9700 
9701     // Visit all callable instructions.
9702     bool UsedAssumedInformation = false;
9703     if (!A.checkForAllCallLikeInstructions(ProcessCallInst, *this,
9704                                            UsedAssumedInformation,
9705                                            /* CheckBBLivenessOnly */ true)) {
9706       // If we haven't looked at all call like instructions, assume that there
9707       // are unknown callees.
9708       setHasUnknownCallee(true, Change);
9709     }
9710 
9711     return Change;
9712   }
9713 };
9714 
9715 struct AAFunctionReachabilityFunction : public AAFunctionReachability {
9716 private:
9717   struct QuerySet {
9718     void markReachable(const Function &Fn) {
9719       Reachable.insert(&Fn);
9720       Unreachable.erase(&Fn);
9721     }
9722 
9723     /// If there is no information about the function None is returned.
9724     Optional<bool> isCachedReachable(const Function &Fn) {
9725       // Assume that we can reach the function.
9726       // TODO: Be more specific with the unknown callee.
9727       if (CanReachUnknownCallee)
9728         return true;
9729 
9730       if (Reachable.count(&Fn))
9731         return true;
9732 
9733       if (Unreachable.count(&Fn))
9734         return false;
9735 
9736       return llvm::None;
9737     }
9738 
9739     /// Set of functions that we know for sure is reachable.
9740     DenseSet<const Function *> Reachable;
9741 
9742     /// Set of functions that are unreachable, but might become reachable.
9743     DenseSet<const Function *> Unreachable;
9744 
9745     /// If we can reach a function with a call to a unknown function we assume
9746     /// that we can reach any function.
9747     bool CanReachUnknownCallee = false;
9748   };
9749 
9750   struct QueryResolver : public QuerySet {
9751     ChangeStatus update(Attributor &A, const AAFunctionReachability &AA,
9752                         ArrayRef<const AACallEdges *> AAEdgesList) {
9753       ChangeStatus Change = ChangeStatus::UNCHANGED;
9754 
9755       for (auto *AAEdges : AAEdgesList) {
9756         if (AAEdges->hasUnknownCallee()) {
9757           if (!CanReachUnknownCallee)
9758             Change = ChangeStatus::CHANGED;
9759           CanReachUnknownCallee = true;
9760           return Change;
9761         }
9762       }
9763 
9764       for (const Function *Fn : make_early_inc_range(Unreachable)) {
9765         if (checkIfReachable(A, AA, AAEdgesList, *Fn)) {
9766           Change = ChangeStatus::CHANGED;
9767           markReachable(*Fn);
9768         }
9769       }
9770       return Change;
9771     }
9772 
9773     bool isReachable(Attributor &A, AAFunctionReachability &AA,
9774                      ArrayRef<const AACallEdges *> AAEdgesList,
9775                      const Function &Fn) {
9776       Optional<bool> Cached = isCachedReachable(Fn);
9777       if (Cached.hasValue())
9778         return Cached.getValue();
9779 
9780       // The query was not cached, thus it is new. We need to request an update
9781       // explicitly to make sure this the information is properly run to a
9782       // fixpoint.
9783       A.registerForUpdate(AA);
9784 
9785       // We need to assume that this function can't reach Fn to prevent
9786       // an infinite loop if this function is recursive.
9787       Unreachable.insert(&Fn);
9788 
9789       bool Result = checkIfReachable(A, AA, AAEdgesList, Fn);
9790       if (Result)
9791         markReachable(Fn);
9792       return Result;
9793     }
9794 
9795     bool checkIfReachable(Attributor &A, const AAFunctionReachability &AA,
9796                           ArrayRef<const AACallEdges *> AAEdgesList,
9797                           const Function &Fn) const {
9798 
9799       // Handle the most trivial case first.
9800       for (auto *AAEdges : AAEdgesList) {
9801         const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges();
9802 
9803         if (Edges.count(const_cast<Function *>(&Fn)))
9804           return true;
9805       }
9806 
9807       SmallVector<const AAFunctionReachability *, 8> Deps;
9808       for (auto &AAEdges : AAEdgesList) {
9809         const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges();
9810 
9811         for (Function *Edge : Edges) {
9812           // We don't need a dependency if the result is reachable.
9813           const AAFunctionReachability &EdgeReachability =
9814               A.getAAFor<AAFunctionReachability>(
9815                   AA, IRPosition::function(*Edge), DepClassTy::NONE);
9816           Deps.push_back(&EdgeReachability);
9817 
9818           if (EdgeReachability.canReach(A, Fn))
9819             return true;
9820         }
9821       }
9822 
9823       // The result is false for now, set dependencies and leave.
9824       for (auto *Dep : Deps)
9825         A.recordDependence(*Dep, AA, DepClassTy::REQUIRED);
9826 
9827       return false;
9828     }
9829   };
9830 
9831   /// Get call edges that can be reached by this instruction.
9832   bool getReachableCallEdges(Attributor &A, const AAReachability &Reachability,
9833                              const Instruction &Inst,
9834                              SmallVector<const AACallEdges *> &Result) const {
9835     // Determine call like instructions that we can reach from the inst.
9836     auto CheckCallBase = [&](Instruction &CBInst) {
9837       if (!Reachability.isAssumedReachable(A, Inst, CBInst))
9838         return true;
9839 
9840       auto &CB = cast<CallBase>(CBInst);
9841       const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
9842           *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
9843 
9844       Result.push_back(&AAEdges);
9845       return true;
9846     };
9847 
9848     bool UsedAssumedInformation = false;
9849     return A.checkForAllCallLikeInstructions(CheckCallBase, *this,
9850                                              UsedAssumedInformation,
9851                                              /* CheckBBLivenessOnly */ true);
9852   }
9853 
9854 public:
9855   AAFunctionReachabilityFunction(const IRPosition &IRP, Attributor &A)
9856       : AAFunctionReachability(IRP, A) {}
9857 
9858   bool canReach(Attributor &A, const Function &Fn) const override {
9859     if (!isValidState())
9860       return true;
9861 
9862     const AACallEdges &AAEdges =
9863         A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
9864 
9865     // Attributor returns attributes as const, so this function has to be
9866     // const for users of this attribute to use it without having to do
9867     // a const_cast.
9868     // This is a hack for us to be able to cache queries.
9869     auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9870     bool Result = NonConstThis->WholeFunction.isReachable(A, *NonConstThis,
9871                                                           {&AAEdges}, Fn);
9872 
9873     return Result;
9874   }
9875 
9876   /// Can \p CB reach \p Fn
9877   bool canReach(Attributor &A, CallBase &CB,
9878                 const Function &Fn) const override {
9879     if (!isValidState())
9880       return true;
9881 
9882     const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
9883         *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
9884 
9885     // Attributor returns attributes as const, so this function has to be
9886     // const for users of this attribute to use it without having to do
9887     // a const_cast.
9888     // This is a hack for us to be able to cache queries.
9889     auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9890     QueryResolver &CBQuery = NonConstThis->CBQueries[&CB];
9891 
9892     bool Result = CBQuery.isReachable(A, *NonConstThis, {&AAEdges}, Fn);
9893 
9894     return Result;
9895   }
9896 
9897   bool instructionCanReach(Attributor &A, const Instruction &Inst,
9898                            const Function &Fn,
9899                            bool UseBackwards) const override {
9900     if (!isValidState())
9901       return true;
9902 
9903     if (UseBackwards)
9904       return AA::isPotentiallyReachable(A, Inst, Fn, *this, nullptr);
9905 
9906     const auto &Reachability = A.getAAFor<AAReachability>(
9907         *this, IRPosition::function(*getAssociatedFunction()),
9908         DepClassTy::REQUIRED);
9909 
9910     SmallVector<const AACallEdges *> CallEdges;
9911     bool AllKnown = getReachableCallEdges(A, Reachability, Inst, CallEdges);
9912     // Attributor returns attributes as const, so this function has to be
9913     // const for users of this attribute to use it without having to do
9914     // a const_cast.
9915     // This is a hack for us to be able to cache queries.
9916     auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9917     QueryResolver &InstQSet = NonConstThis->InstQueries[&Inst];
9918     if (!AllKnown)
9919       InstQSet.CanReachUnknownCallee = true;
9920 
9921     return InstQSet.isReachable(A, *NonConstThis, CallEdges, Fn);
9922   }
9923 
9924   /// See AbstractAttribute::updateImpl(...).
9925   ChangeStatus updateImpl(Attributor &A) override {
9926     const AACallEdges &AAEdges =
9927         A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
9928     ChangeStatus Change = ChangeStatus::UNCHANGED;
9929 
9930     Change |= WholeFunction.update(A, *this, {&AAEdges});
9931 
9932     for (auto &CBPair : CBQueries) {
9933       const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
9934           *this, IRPosition::callsite_function(*CBPair.first),
9935           DepClassTy::REQUIRED);
9936 
9937       Change |= CBPair.second.update(A, *this, {&AAEdges});
9938     }
9939 
9940     // Update the Instruction queries.
9941     if (!InstQueries.empty()) {
9942       const AAReachability *Reachability = &A.getAAFor<AAReachability>(
9943           *this, IRPosition::function(*getAssociatedFunction()),
9944           DepClassTy::REQUIRED);
9945 
9946       // Check for local callbases first.
9947       for (auto &InstPair : InstQueries) {
9948         SmallVector<const AACallEdges *> CallEdges;
9949         bool AllKnown =
9950             getReachableCallEdges(A, *Reachability, *InstPair.first, CallEdges);
9951         // Update will return change if we this effects any queries.
9952         if (!AllKnown)
9953           InstPair.second.CanReachUnknownCallee = true;
9954         Change |= InstPair.second.update(A, *this, CallEdges);
9955       }
9956     }
9957 
9958     return Change;
9959   }
9960 
9961   const std::string getAsStr() const override {
9962     size_t QueryCount =
9963         WholeFunction.Reachable.size() + WholeFunction.Unreachable.size();
9964 
9965     return "FunctionReachability [" +
9966            std::to_string(WholeFunction.Reachable.size()) + "," +
9967            std::to_string(QueryCount) + "]";
9968   }
9969 
9970   void trackStatistics() const override {}
9971 
9972 private:
9973   bool canReachUnknownCallee() const override {
9974     return WholeFunction.CanReachUnknownCallee;
9975   }
9976 
9977   /// Used to answer if a the whole function can reacha a specific function.
9978   QueryResolver WholeFunction;
9979 
9980   /// Used to answer if a call base inside this function can reach a specific
9981   /// function.
9982   DenseMap<const CallBase *, QueryResolver> CBQueries;
9983 
9984   /// This is for instruction queries than scan "forward".
9985   DenseMap<const Instruction *, QueryResolver> InstQueries;
9986 };
9987 } // namespace
9988 
9989 /// ---------------------- Assumption Propagation ------------------------------
9990 namespace {
9991 struct AAAssumptionInfoImpl : public AAAssumptionInfo {
9992   AAAssumptionInfoImpl(const IRPosition &IRP, Attributor &A,
9993                        const DenseSet<StringRef> &Known)
9994       : AAAssumptionInfo(IRP, A, Known) {}
9995 
9996   bool hasAssumption(const StringRef Assumption) const override {
9997     return isValidState() && setContains(Assumption);
9998   }
9999 
10000   /// See AbstractAttribute::getAsStr()
10001   const std::string getAsStr() const override {
10002     const SetContents &Known = getKnown();
10003     const SetContents &Assumed = getAssumed();
10004 
10005     const std::string KnownStr =
10006         llvm::join(Known.getSet().begin(), Known.getSet().end(), ",");
10007     const std::string AssumedStr =
10008         (Assumed.isUniversal())
10009             ? "Universal"
10010             : llvm::join(Assumed.getSet().begin(), Assumed.getSet().end(), ",");
10011 
10012     return "Known [" + KnownStr + "]," + " Assumed [" + AssumedStr + "]";
10013   }
10014 };
10015 
10016 /// Propagates assumption information from parent functions to all of their
10017 /// successors. An assumption can be propagated if the containing function
10018 /// dominates the called function.
10019 ///
10020 /// We start with a "known" set of assumptions already valid for the associated
10021 /// function and an "assumed" set that initially contains all possible
10022 /// assumptions. The assumed set is inter-procedurally updated by narrowing its
10023 /// contents as concrete values are known. The concrete values are seeded by the
10024 /// first nodes that are either entries into the call graph, or contains no
10025 /// assumptions. Each node is updated as the intersection of the assumed state
10026 /// with all of its predecessors.
10027 struct AAAssumptionInfoFunction final : AAAssumptionInfoImpl {
10028   AAAssumptionInfoFunction(const IRPosition &IRP, Attributor &A)
10029       : AAAssumptionInfoImpl(IRP, A,
10030                              getAssumptions(*IRP.getAssociatedFunction())) {}
10031 
10032   /// See AbstractAttribute::manifest(...).
10033   ChangeStatus manifest(Attributor &A) override {
10034     const auto &Assumptions = getKnown();
10035 
10036     // Don't manifest a universal set if it somehow made it here.
10037     if (Assumptions.isUniversal())
10038       return ChangeStatus::UNCHANGED;
10039 
10040     Function *AssociatedFunction = getAssociatedFunction();
10041 
10042     bool Changed = addAssumptions(*AssociatedFunction, Assumptions.getSet());
10043 
10044     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
10045   }
10046 
10047   /// See AbstractAttribute::updateImpl(...).
10048   ChangeStatus updateImpl(Attributor &A) override {
10049     bool Changed = false;
10050 
10051     auto CallSitePred = [&](AbstractCallSite ACS) {
10052       const auto &AssumptionAA = A.getAAFor<AAAssumptionInfo>(
10053           *this, IRPosition::callsite_function(*ACS.getInstruction()),
10054           DepClassTy::REQUIRED);
10055       // Get the set of assumptions shared by all of this function's callers.
10056       Changed |= getIntersection(AssumptionAA.getAssumed());
10057       return !getAssumed().empty() || !getKnown().empty();
10058     };
10059 
10060     bool UsedAssumedInformation = false;
10061     // Get the intersection of all assumptions held by this node's predecessors.
10062     // If we don't know all the call sites then this is either an entry into the
10063     // call graph or an empty node. This node is known to only contain its own
10064     // assumptions and can be propagated to its successors.
10065     if (!A.checkForAllCallSites(CallSitePred, *this, true,
10066                                 UsedAssumedInformation))
10067       return indicatePessimisticFixpoint();
10068 
10069     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
10070   }
10071 
10072   void trackStatistics() const override {}
10073 };
10074 
10075 /// Assumption Info defined for call sites.
10076 struct AAAssumptionInfoCallSite final : AAAssumptionInfoImpl {
10077 
10078   AAAssumptionInfoCallSite(const IRPosition &IRP, Attributor &A)
10079       : AAAssumptionInfoImpl(IRP, A, getInitialAssumptions(IRP)) {}
10080 
10081   /// See AbstractAttribute::initialize(...).
10082   void initialize(Attributor &A) override {
10083     const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
10084     A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
10085   }
10086 
10087   /// See AbstractAttribute::manifest(...).
10088   ChangeStatus manifest(Attributor &A) override {
10089     // Don't manifest a universal set if it somehow made it here.
10090     if (getKnown().isUniversal())
10091       return ChangeStatus::UNCHANGED;
10092 
10093     CallBase &AssociatedCall = cast<CallBase>(getAssociatedValue());
10094     bool Changed = addAssumptions(AssociatedCall, getAssumed().getSet());
10095 
10096     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
10097   }
10098 
10099   /// See AbstractAttribute::updateImpl(...).
10100   ChangeStatus updateImpl(Attributor &A) override {
10101     const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
10102     auto &AssumptionAA =
10103         A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
10104     bool Changed = getIntersection(AssumptionAA.getAssumed());
10105     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
10106   }
10107 
10108   /// See AbstractAttribute::trackStatistics()
10109   void trackStatistics() const override {}
10110 
10111 private:
10112   /// Helper to initialized the known set as all the assumptions this call and
10113   /// the callee contain.
10114   DenseSet<StringRef> getInitialAssumptions(const IRPosition &IRP) {
10115     const CallBase &CB = cast<CallBase>(IRP.getAssociatedValue());
10116     auto Assumptions = getAssumptions(CB);
10117     if (Function *F = IRP.getAssociatedFunction())
10118       set_union(Assumptions, getAssumptions(*F));
10119     if (Function *F = IRP.getAssociatedFunction())
10120       set_union(Assumptions, getAssumptions(*F));
10121     return Assumptions;
10122   }
10123 };
10124 } // namespace
10125 
10126 AACallGraphNode *AACallEdgeIterator::operator*() const {
10127   return static_cast<AACallGraphNode *>(const_cast<AACallEdges *>(
10128       &A.getOrCreateAAFor<AACallEdges>(IRPosition::function(**I))));
10129 }
10130 
10131 void AttributorCallGraph::print() { llvm::WriteGraph(outs(), this); }
10132 
10133 const char AAReturnedValues::ID = 0;
10134 const char AANoUnwind::ID = 0;
10135 const char AANoSync::ID = 0;
10136 const char AANoFree::ID = 0;
10137 const char AANonNull::ID = 0;
10138 const char AANoRecurse::ID = 0;
10139 const char AAWillReturn::ID = 0;
10140 const char AAUndefinedBehavior::ID = 0;
10141 const char AANoAlias::ID = 0;
10142 const char AAReachability::ID = 0;
10143 const char AANoReturn::ID = 0;
10144 const char AAIsDead::ID = 0;
10145 const char AADereferenceable::ID = 0;
10146 const char AAAlign::ID = 0;
10147 const char AANoCapture::ID = 0;
10148 const char AAValueSimplify::ID = 0;
10149 const char AAHeapToStack::ID = 0;
10150 const char AAPrivatizablePtr::ID = 0;
10151 const char AAMemoryBehavior::ID = 0;
10152 const char AAMemoryLocation::ID = 0;
10153 const char AAValueConstantRange::ID = 0;
10154 const char AAPotentialValues::ID = 0;
10155 const char AANoUndef::ID = 0;
10156 const char AACallEdges::ID = 0;
10157 const char AAFunctionReachability::ID = 0;
10158 const char AAPointerInfo::ID = 0;
10159 const char AAAssumptionInfo::ID = 0;
10160 
10161 // Macro magic to create the static generator function for attributes that
10162 // follow the naming scheme.
10163 
10164 #define SWITCH_PK_INV(CLASS, PK, POS_NAME)                                     \
10165   case IRPosition::PK:                                                         \
10166     llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
10167 
10168 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX)                               \
10169   case IRPosition::PK:                                                         \
10170     AA = new (A.Allocator) CLASS##SUFFIX(IRP, A);                              \
10171     ++NumAAs;                                                                  \
10172     break;
10173 
10174 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                 \
10175   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10176     CLASS *AA = nullptr;                                                       \
10177     switch (IRP.getPositionKind()) {                                           \
10178       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10179       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
10180       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
10181       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
10182       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
10183       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
10184       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10185       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
10186     }                                                                          \
10187     return *AA;                                                                \
10188   }
10189 
10190 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                    \
10191   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10192     CLASS *AA = nullptr;                                                       \
10193     switch (IRP.getPositionKind()) {                                           \
10194       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10195       SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function")                           \
10196       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
10197       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
10198       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
10199       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
10200       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
10201       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
10202     }                                                                          \
10203     return *AA;                                                                \
10204   }
10205 
10206 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                      \
10207   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10208     CLASS *AA = nullptr;                                                       \
10209     switch (IRP.getPositionKind()) {                                           \
10210       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10211       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10212       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
10213       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
10214       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
10215       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
10216       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
10217       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
10218     }                                                                          \
10219     return *AA;                                                                \
10220   }
10221 
10222 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)            \
10223   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10224     CLASS *AA = nullptr;                                                       \
10225     switch (IRP.getPositionKind()) {                                           \
10226       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10227       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
10228       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
10229       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
10230       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
10231       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
10232       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
10233       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10234     }                                                                          \
10235     return *AA;                                                                \
10236   }
10237 
10238 #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                  \
10239   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10240     CLASS *AA = nullptr;                                                       \
10241     switch (IRP.getPositionKind()) {                                           \
10242       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10243       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
10244       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10245       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
10246       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
10247       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
10248       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
10249       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
10250     }                                                                          \
10251     return *AA;                                                                \
10252   }
10253 
10254 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
10255 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
10256 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
10257 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
10258 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
10259 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
10260 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation)
10261 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AACallEdges)
10262 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAssumptionInfo)
10263 
10264 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
10265 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
10266 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
10267 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
10268 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
10269 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
10270 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
10271 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialValues)
10272 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUndef)
10273 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPointerInfo)
10274 
10275 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
10276 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
10277 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
10278 
10279 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
10280 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
10281 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
10282 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAFunctionReachability)
10283 
10284 CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
10285 
10286 #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
10287 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
10288 #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
10289 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
10290 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
10291 #undef SWITCH_PK_CREATE
10292 #undef SWITCH_PK_INV
10293