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     // FIXME: This is a hack to prevent us from propagating function poiner in
5506     // the new pass manager CGSCC pass as it creates call edges the
5507     // CallGraphUpdater cannot handle yet.
5508     Value &V = getAssociatedValue();
5509     if (V.getType()->isPointerTy() &&
5510         V.getType()->getPointerElementType()->isFunctionTy() &&
5511         !A.isModulePass())
5512       indicatePessimisticFixpoint();
5513   }
5514 
5515   /// See AbstractAttribute::updateImpl(...).
5516   ChangeStatus updateImpl(Attributor &A) override {
5517     // Byval is only replacable if it is readonly otherwise we would write into
5518     // the replaced value and not the copy that byval creates implicitly.
5519     Argument *Arg = getAssociatedArgument();
5520     if (Arg->hasByValAttr()) {
5521       // TODO: We probably need to verify synchronization is not an issue, e.g.,
5522       //       there is no race by not copying a constant byval.
5523       bool IsKnown;
5524       if (!AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
5525         return indicatePessimisticFixpoint();
5526     }
5527 
5528     auto Before = SimplifiedAssociatedValue;
5529 
5530     auto PredForCallSite = [&](AbstractCallSite ACS) {
5531       const IRPosition &ACSArgPos =
5532           IRPosition::callsite_argument(ACS, getCallSiteArgNo());
5533       // Check if a coresponding argument was found or if it is on not
5534       // associated (which can happen for callback calls).
5535       if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
5536         return false;
5537 
5538       // Simplify the argument operand explicitly and check if the result is
5539       // valid in the current scope. This avoids refering to simplified values
5540       // in other functions, e.g., we don't want to say a an argument in a
5541       // static function is actually an argument in a different function.
5542       bool UsedAssumedInformation = false;
5543       Optional<Constant *> SimpleArgOp =
5544           A.getAssumedConstant(ACSArgPos, *this, UsedAssumedInformation);
5545       if (!SimpleArgOp.hasValue())
5546         return true;
5547       if (!SimpleArgOp.getValue())
5548         return false;
5549       if (!AA::isDynamicallyUnique(A, *this, **SimpleArgOp))
5550         return false;
5551       return unionAssumed(*SimpleArgOp);
5552     };
5553 
5554     // Generate a answer specific to a call site context.
5555     bool Success;
5556     bool UsedAssumedInformation = false;
5557     if (hasCallBaseContext() &&
5558         getCallBaseContext()->getCalledFunction() == Arg->getParent())
5559       Success = PredForCallSite(
5560           AbstractCallSite(&getCallBaseContext()->getCalledOperandUse()));
5561     else
5562       Success = A.checkForAllCallSites(PredForCallSite, *this, true,
5563                                        UsedAssumedInformation);
5564 
5565     if (!Success)
5566       if (!askSimplifiedValueForOtherAAs(A))
5567         return indicatePessimisticFixpoint();
5568 
5569     // If a candicate was found in this update, return CHANGED.
5570     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5571                                                : ChangeStatus ::CHANGED;
5572   }
5573 
5574   /// See AbstractAttribute::trackStatistics()
5575   void trackStatistics() const override {
5576     STATS_DECLTRACK_ARG_ATTR(value_simplify)
5577   }
5578 };
5579 
5580 struct AAValueSimplifyReturned : AAValueSimplifyImpl {
5581   AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A)
5582       : AAValueSimplifyImpl(IRP, A) {}
5583 
5584   /// See AAValueSimplify::getAssumedSimplifiedValue()
5585   Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
5586     if (!isValidState())
5587       return nullptr;
5588     return SimplifiedAssociatedValue;
5589   }
5590 
5591   /// See AbstractAttribute::updateImpl(...).
5592   ChangeStatus updateImpl(Attributor &A) override {
5593     auto Before = SimplifiedAssociatedValue;
5594 
5595     auto PredForReturned = [&](Value &V) {
5596       return checkAndUpdate(A, *this,
5597                             IRPosition::value(V, getCallBaseContext()));
5598     };
5599 
5600     if (!A.checkForAllReturnedValues(PredForReturned, *this))
5601       if (!askSimplifiedValueForOtherAAs(A))
5602         return indicatePessimisticFixpoint();
5603 
5604     // If a candicate was found in this update, return CHANGED.
5605     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5606                                                : ChangeStatus ::CHANGED;
5607   }
5608 
5609   ChangeStatus manifest(Attributor &A) override {
5610     ChangeStatus Changed = ChangeStatus::UNCHANGED;
5611     if (!A.isRunOn(*getAnchorScope()))
5612       return Changed;
5613 
5614     assert(!hasCallBaseContext() && "Should never manifest a simplified "
5615                                     "function return with call base context!");
5616 
5617     if (auto *NewV = getReplacementValue(A)) {
5618       auto PredForReturned =
5619           [&](Value &, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
5620             for (ReturnInst *RI : RetInsts) {
5621               Value *ReturnedVal = RI->getReturnValue();
5622               if (ReturnedVal == NewV || isa<UndefValue>(ReturnedVal))
5623                 return true;
5624               assert(RI->getFunction() == getAnchorScope() &&
5625                      "ReturnInst in wrong function!");
5626               LLVM_DEBUG(dbgs()
5627                          << "[ValueSimplify] " << *ReturnedVal << " -> "
5628                          << *NewV << " in " << *RI << " :: " << *this << "\n");
5629               if (A.changeUseAfterManifest(RI->getOperandUse(0), *NewV))
5630                 Changed = ChangeStatus::CHANGED;
5631             }
5632             return true;
5633           };
5634       A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this);
5635     }
5636 
5637     return Changed | AAValueSimplify::manifest(A);
5638   }
5639 
5640   /// See AbstractAttribute::trackStatistics()
5641   void trackStatistics() const override {
5642     STATS_DECLTRACK_FNRET_ATTR(value_simplify)
5643   }
5644 };
5645 
5646 struct AAValueSimplifyFloating : AAValueSimplifyImpl {
5647   AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A)
5648       : AAValueSimplifyImpl(IRP, A) {}
5649 
5650   /// See AbstractAttribute::initialize(...).
5651   void initialize(Attributor &A) override {
5652     AAValueSimplifyImpl::initialize(A);
5653     Value &V = getAnchorValue();
5654 
5655     // TODO: add other stuffs
5656     if (isa<Constant>(V))
5657       indicatePessimisticFixpoint();
5658   }
5659 
5660   /// Check if \p Cmp is a comparison we can simplify.
5661   ///
5662   /// We handle multiple cases, one in which at least one operand is an
5663   /// (assumed) nullptr. If so, try to simplify it using AANonNull on the other
5664   /// operand. Return true if successful, in that case SimplifiedAssociatedValue
5665   /// will be updated.
5666   bool handleCmp(Attributor &A, CmpInst &Cmp) {
5667     auto Union = [&](Value &V) {
5668       SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5669           SimplifiedAssociatedValue, &V, V.getType());
5670       return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5671     };
5672 
5673     Value *LHS = Cmp.getOperand(0);
5674     Value *RHS = Cmp.getOperand(1);
5675 
5676     // Simplify the operands first.
5677     bool UsedAssumedInformation = false;
5678     const auto &SimplifiedLHS =
5679         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
5680                                *this, UsedAssumedInformation);
5681     if (!SimplifiedLHS.hasValue())
5682       return true;
5683     if (!SimplifiedLHS.getValue())
5684       return false;
5685     LHS = *SimplifiedLHS;
5686 
5687     const auto &SimplifiedRHS =
5688         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
5689                                *this, UsedAssumedInformation);
5690     if (!SimplifiedRHS.hasValue())
5691       return true;
5692     if (!SimplifiedRHS.getValue())
5693       return false;
5694     RHS = *SimplifiedRHS;
5695 
5696     LLVMContext &Ctx = Cmp.getContext();
5697     // Handle the trivial case first in which we don't even need to think about
5698     // null or non-null.
5699     if (LHS == RHS && (Cmp.isTrueWhenEqual() || Cmp.isFalseWhenEqual())) {
5700       Constant *NewVal =
5701           ConstantInt::get(Type::getInt1Ty(Ctx), Cmp.isTrueWhenEqual());
5702       if (!Union(*NewVal))
5703         return false;
5704       if (!UsedAssumedInformation)
5705         indicateOptimisticFixpoint();
5706       return true;
5707     }
5708 
5709     // From now on we only handle equalities (==, !=).
5710     ICmpInst *ICmp = dyn_cast<ICmpInst>(&Cmp);
5711     if (!ICmp || !ICmp->isEquality())
5712       return false;
5713 
5714     bool LHSIsNull = isa<ConstantPointerNull>(LHS);
5715     bool RHSIsNull = isa<ConstantPointerNull>(RHS);
5716     if (!LHSIsNull && !RHSIsNull)
5717       return false;
5718 
5719     // Left is the nullptr ==/!= non-nullptr case. We'll use AANonNull on the
5720     // non-nullptr operand and if we assume it's non-null we can conclude the
5721     // result of the comparison.
5722     assert((LHSIsNull || RHSIsNull) &&
5723            "Expected nullptr versus non-nullptr comparison at this point");
5724 
5725     // The index is the operand that we assume is not null.
5726     unsigned PtrIdx = LHSIsNull;
5727     auto &PtrNonNullAA = A.getAAFor<AANonNull>(
5728         *this, IRPosition::value(*ICmp->getOperand(PtrIdx)),
5729         DepClassTy::REQUIRED);
5730     if (!PtrNonNullAA.isAssumedNonNull())
5731       return false;
5732     UsedAssumedInformation |= !PtrNonNullAA.isKnownNonNull();
5733 
5734     // The new value depends on the predicate, true for != and false for ==.
5735     Constant *NewVal = ConstantInt::get(
5736         Type::getInt1Ty(Ctx), ICmp->getPredicate() == CmpInst::ICMP_NE);
5737     if (!Union(*NewVal))
5738       return false;
5739 
5740     if (!UsedAssumedInformation)
5741       indicateOptimisticFixpoint();
5742 
5743     return true;
5744   }
5745 
5746   bool updateWithLoad(Attributor &A, LoadInst &L) {
5747     auto Union = [&](Value &V) {
5748       SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5749           SimplifiedAssociatedValue, &V, L.getType());
5750       return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5751     };
5752     return handleLoad(A, *this, L, Union);
5753   }
5754 
5755   /// Use the generic, non-optimistic InstSimplfy functionality if we managed to
5756   /// simplify any operand of the instruction \p I. Return true if successful,
5757   /// in that case SimplifiedAssociatedValue will be updated.
5758   bool handleGenericInst(Attributor &A, Instruction &I) {
5759     bool SomeSimplified = false;
5760     bool UsedAssumedInformation = false;
5761 
5762     SmallVector<Value *, 8> NewOps(I.getNumOperands());
5763     int Idx = 0;
5764     for (Value *Op : I.operands()) {
5765       const auto &SimplifiedOp =
5766           A.getAssumedSimplified(IRPosition::value(*Op, getCallBaseContext()),
5767                                  *this, UsedAssumedInformation);
5768       // If we are not sure about any operand we are not sure about the entire
5769       // instruction, we'll wait.
5770       if (!SimplifiedOp.hasValue())
5771         return true;
5772 
5773       if (SimplifiedOp.getValue())
5774         NewOps[Idx] = SimplifiedOp.getValue();
5775       else
5776         NewOps[Idx] = Op;
5777 
5778       SomeSimplified |= (NewOps[Idx] != Op);
5779       ++Idx;
5780     }
5781 
5782     // We won't bother with the InstSimplify interface if we didn't simplify any
5783     // operand ourselves.
5784     if (!SomeSimplified)
5785       return false;
5786 
5787     InformationCache &InfoCache = A.getInfoCache();
5788     Function *F = I.getFunction();
5789     const auto *DT =
5790         InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
5791     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5792     auto *AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
5793     OptimizationRemarkEmitter *ORE = nullptr;
5794 
5795     const DataLayout &DL = I.getModule()->getDataLayout();
5796     SimplifyQuery Q(DL, TLI, DT, AC, &I);
5797     if (Value *SimplifiedI =
5798             SimplifyInstructionWithOperands(&I, NewOps, Q, ORE)) {
5799       SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5800           SimplifiedAssociatedValue, SimplifiedI, I.getType());
5801       return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5802     }
5803     return false;
5804   }
5805 
5806   /// See AbstractAttribute::updateImpl(...).
5807   ChangeStatus updateImpl(Attributor &A) override {
5808     auto Before = SimplifiedAssociatedValue;
5809 
5810     auto VisitValueCB = [&](Value &V, const Instruction *CtxI, bool &,
5811                             bool Stripped) -> bool {
5812       auto &AA = A.getAAFor<AAValueSimplify>(
5813           *this, IRPosition::value(V, getCallBaseContext()),
5814           DepClassTy::REQUIRED);
5815       if (!Stripped && this == &AA) {
5816 
5817         if (auto *I = dyn_cast<Instruction>(&V)) {
5818           if (auto *LI = dyn_cast<LoadInst>(&V))
5819             if (updateWithLoad(A, *LI))
5820               return true;
5821           if (auto *Cmp = dyn_cast<CmpInst>(&V))
5822             if (handleCmp(A, *Cmp))
5823               return true;
5824           if (handleGenericInst(A, *I))
5825             return true;
5826         }
5827         // TODO: Look the instruction and check recursively.
5828 
5829         LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V
5830                           << "\n");
5831         return false;
5832       }
5833       return checkAndUpdate(A, *this,
5834                             IRPosition::value(V, getCallBaseContext()));
5835     };
5836 
5837     bool Dummy = false;
5838     bool UsedAssumedInformation = false;
5839     if (!genericValueTraversal<bool>(A, getIRPosition(), *this, Dummy,
5840                                      VisitValueCB, getCtxI(),
5841                                      UsedAssumedInformation,
5842                                      /* UseValueSimplify */ false))
5843       if (!askSimplifiedValueForOtherAAs(A))
5844         return indicatePessimisticFixpoint();
5845 
5846     // If a candicate was found in this update, return CHANGED.
5847     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5848                                                : ChangeStatus ::CHANGED;
5849   }
5850 
5851   /// See AbstractAttribute::trackStatistics()
5852   void trackStatistics() const override {
5853     STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
5854   }
5855 };
5856 
5857 struct AAValueSimplifyFunction : AAValueSimplifyImpl {
5858   AAValueSimplifyFunction(const IRPosition &IRP, Attributor &A)
5859       : AAValueSimplifyImpl(IRP, A) {}
5860 
5861   /// See AbstractAttribute::initialize(...).
5862   void initialize(Attributor &A) override {
5863     SimplifiedAssociatedValue = nullptr;
5864     indicateOptimisticFixpoint();
5865   }
5866   /// See AbstractAttribute::initialize(...).
5867   ChangeStatus updateImpl(Attributor &A) override {
5868     llvm_unreachable(
5869         "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
5870   }
5871   /// See AbstractAttribute::trackStatistics()
5872   void trackStatistics() const override {
5873     STATS_DECLTRACK_FN_ATTR(value_simplify)
5874   }
5875 };
5876 
5877 struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
5878   AAValueSimplifyCallSite(const IRPosition &IRP, Attributor &A)
5879       : AAValueSimplifyFunction(IRP, A) {}
5880   /// See AbstractAttribute::trackStatistics()
5881   void trackStatistics() const override {
5882     STATS_DECLTRACK_CS_ATTR(value_simplify)
5883   }
5884 };
5885 
5886 struct AAValueSimplifyCallSiteReturned : AAValueSimplifyImpl {
5887   AAValueSimplifyCallSiteReturned(const IRPosition &IRP, Attributor &A)
5888       : AAValueSimplifyImpl(IRP, A) {}
5889 
5890   void initialize(Attributor &A) override {
5891     AAValueSimplifyImpl::initialize(A);
5892     if (!getAssociatedFunction())
5893       indicatePessimisticFixpoint();
5894   }
5895 
5896   /// See AbstractAttribute::updateImpl(...).
5897   ChangeStatus updateImpl(Attributor &A) override {
5898     auto Before = SimplifiedAssociatedValue;
5899     auto &RetAA = A.getAAFor<AAReturnedValues>(
5900         *this, IRPosition::function(*getAssociatedFunction()),
5901         DepClassTy::REQUIRED);
5902     auto PredForReturned =
5903         [&](Value &RetVal, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
5904           bool UsedAssumedInformation = false;
5905           Optional<Value *> CSRetVal = A.translateArgumentToCallSiteContent(
5906               &RetVal, *cast<CallBase>(getCtxI()), *this,
5907               UsedAssumedInformation);
5908           SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5909               SimplifiedAssociatedValue, CSRetVal, getAssociatedType());
5910           return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5911         };
5912     if (!RetAA.checkForAllReturnedValuesAndReturnInsts(PredForReturned))
5913       if (!askSimplifiedValueForOtherAAs(A))
5914         return indicatePessimisticFixpoint();
5915     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5916                                                : ChangeStatus ::CHANGED;
5917   }
5918 
5919   void trackStatistics() const override {
5920     STATS_DECLTRACK_CSRET_ATTR(value_simplify)
5921   }
5922 };
5923 
5924 struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
5925   AAValueSimplifyCallSiteArgument(const IRPosition &IRP, Attributor &A)
5926       : AAValueSimplifyFloating(IRP, A) {}
5927 
5928   /// See AbstractAttribute::manifest(...).
5929   ChangeStatus manifest(Attributor &A) override {
5930     ChangeStatus Changed = ChangeStatus::UNCHANGED;
5931 
5932     if (auto *NewV = getReplacementValue(A)) {
5933       Use &U = cast<CallBase>(&getAnchorValue())
5934                    ->getArgOperandUse(getCallSiteArgNo());
5935       if (A.changeUseAfterManifest(U, *NewV))
5936         Changed = ChangeStatus::CHANGED;
5937     }
5938 
5939     return Changed | AAValueSimplify::manifest(A);
5940   }
5941 
5942   void trackStatistics() const override {
5943     STATS_DECLTRACK_CSARG_ATTR(value_simplify)
5944   }
5945 };
5946 } // namespace
5947 
5948 /// ----------------------- Heap-To-Stack Conversion ---------------------------
5949 namespace {
5950 struct AAHeapToStackFunction final : public AAHeapToStack {
5951 
5952   struct AllocationInfo {
5953     /// The call that allocates the memory.
5954     CallBase *const CB;
5955 
5956     /// The library function id for the allocation.
5957     LibFunc LibraryFunctionId = NotLibFunc;
5958 
5959     /// The status wrt. a rewrite.
5960     enum {
5961       STACK_DUE_TO_USE,
5962       STACK_DUE_TO_FREE,
5963       INVALID,
5964     } Status = STACK_DUE_TO_USE;
5965 
5966     /// Flag to indicate if we encountered a use that might free this allocation
5967     /// but which is not in the deallocation infos.
5968     bool HasPotentiallyFreeingUnknownUses = false;
5969 
5970     /// The set of free calls that use this allocation.
5971     SmallPtrSet<CallBase *, 1> PotentialFreeCalls{};
5972   };
5973 
5974   struct DeallocationInfo {
5975     /// The call that deallocates the memory.
5976     CallBase *const CB;
5977 
5978     /// Flag to indicate if we don't know all objects this deallocation might
5979     /// free.
5980     bool MightFreeUnknownObjects = false;
5981 
5982     /// The set of allocation calls that are potentially freed.
5983     SmallPtrSet<CallBase *, 1> PotentialAllocationCalls{};
5984   };
5985 
5986   AAHeapToStackFunction(const IRPosition &IRP, Attributor &A)
5987       : AAHeapToStack(IRP, A) {}
5988 
5989   ~AAHeapToStackFunction() {
5990     // Ensure we call the destructor so we release any memory allocated in the
5991     // sets.
5992     for (auto &It : AllocationInfos)
5993       It.getSecond()->~AllocationInfo();
5994     for (auto &It : DeallocationInfos)
5995       It.getSecond()->~DeallocationInfo();
5996   }
5997 
5998   void initialize(Attributor &A) override {
5999     AAHeapToStack::initialize(A);
6000 
6001     const Function *F = getAnchorScope();
6002     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6003 
6004     auto AllocationIdentifierCB = [&](Instruction &I) {
6005       CallBase *CB = dyn_cast<CallBase>(&I);
6006       if (!CB)
6007         return true;
6008       if (isFreeCall(CB, TLI)) {
6009         DeallocationInfos[CB] = new (A.Allocator) DeallocationInfo{CB};
6010         return true;
6011       }
6012       // To do heap to stack, we need to know that the allocation itself is
6013       // removable once uses are rewritten, and that we can initialize the
6014       // alloca to the same pattern as the original allocation result.
6015       if (isAllocationFn(CB, TLI) && isAllocRemovable(CB, TLI)) {
6016         auto *I8Ty = Type::getInt8Ty(CB->getParent()->getContext());
6017         if (nullptr != getInitialValueOfAllocation(CB, TLI, I8Ty)) {
6018           AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB};
6019           AllocationInfos[CB] = AI;
6020           TLI->getLibFunc(*CB, AI->LibraryFunctionId);
6021         }
6022       }
6023       return true;
6024     };
6025 
6026     bool UsedAssumedInformation = false;
6027     bool Success = A.checkForAllCallLikeInstructions(
6028         AllocationIdentifierCB, *this, UsedAssumedInformation,
6029         /* CheckBBLivenessOnly */ false,
6030         /* CheckPotentiallyDead */ true);
6031     (void)Success;
6032     assert(Success && "Did not expect the call base visit callback to fail!");
6033 
6034     Attributor::SimplifictionCallbackTy SCB =
6035         [](const IRPosition &, const AbstractAttribute *,
6036            bool &) -> Optional<Value *> { return nullptr; };
6037     for (const auto &It : AllocationInfos)
6038       A.registerSimplificationCallback(IRPosition::callsite_returned(*It.first),
6039                                        SCB);
6040     for (const auto &It : DeallocationInfos)
6041       A.registerSimplificationCallback(IRPosition::callsite_returned(*It.first),
6042                                        SCB);
6043   }
6044 
6045   const std::string getAsStr() const override {
6046     unsigned NumH2SMallocs = 0, NumInvalidMallocs = 0;
6047     for (const auto &It : AllocationInfos) {
6048       if (It.second->Status == AllocationInfo::INVALID)
6049         ++NumInvalidMallocs;
6050       else
6051         ++NumH2SMallocs;
6052     }
6053     return "[H2S] Mallocs Good/Bad: " + std::to_string(NumH2SMallocs) + "/" +
6054            std::to_string(NumInvalidMallocs);
6055   }
6056 
6057   /// See AbstractAttribute::trackStatistics().
6058   void trackStatistics() const override {
6059     STATS_DECL(
6060         MallocCalls, Function,
6061         "Number of malloc/calloc/aligned_alloc calls converted to allocas");
6062     for (auto &It : AllocationInfos)
6063       if (It.second->Status != AllocationInfo::INVALID)
6064         ++BUILD_STAT_NAME(MallocCalls, Function);
6065   }
6066 
6067   bool isAssumedHeapToStack(const CallBase &CB) const override {
6068     if (isValidState())
6069       if (AllocationInfo *AI = AllocationInfos.lookup(&CB))
6070         return AI->Status != AllocationInfo::INVALID;
6071     return false;
6072   }
6073 
6074   bool isAssumedHeapToStackRemovedFree(CallBase &CB) const override {
6075     if (!isValidState())
6076       return false;
6077 
6078     for (auto &It : AllocationInfos) {
6079       AllocationInfo &AI = *It.second;
6080       if (AI.Status == AllocationInfo::INVALID)
6081         continue;
6082 
6083       if (AI.PotentialFreeCalls.count(&CB))
6084         return true;
6085     }
6086 
6087     return false;
6088   }
6089 
6090   ChangeStatus manifest(Attributor &A) override {
6091     assert(getState().isValidState() &&
6092            "Attempted to manifest an invalid state!");
6093 
6094     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
6095     Function *F = getAnchorScope();
6096     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6097 
6098     for (auto &It : AllocationInfos) {
6099       AllocationInfo &AI = *It.second;
6100       if (AI.Status == AllocationInfo::INVALID)
6101         continue;
6102 
6103       for (CallBase *FreeCall : AI.PotentialFreeCalls) {
6104         LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
6105         A.deleteAfterManifest(*FreeCall);
6106         HasChanged = ChangeStatus::CHANGED;
6107       }
6108 
6109       LLVM_DEBUG(dbgs() << "H2S: Removing malloc-like call: " << *AI.CB
6110                         << "\n");
6111 
6112       auto Remark = [&](OptimizationRemark OR) {
6113         LibFunc IsAllocShared;
6114         if (TLI->getLibFunc(*AI.CB, IsAllocShared))
6115           if (IsAllocShared == LibFunc___kmpc_alloc_shared)
6116             return OR << "Moving globalized variable to the stack.";
6117         return OR << "Moving memory allocation from the heap to the stack.";
6118       };
6119       if (AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
6120         A.emitRemark<OptimizationRemark>(AI.CB, "OMP110", Remark);
6121       else
6122         A.emitRemark<OptimizationRemark>(AI.CB, "HeapToStack", Remark);
6123 
6124       const DataLayout &DL = A.getInfoCache().getDL();
6125       Value *Size;
6126       Optional<APInt> SizeAPI = getSize(A, *this, AI);
6127       if (SizeAPI.hasValue()) {
6128         Size = ConstantInt::get(AI.CB->getContext(), *SizeAPI);
6129       } else {
6130         LLVMContext &Ctx = AI.CB->getContext();
6131         ObjectSizeOpts Opts;
6132         ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, Opts);
6133         SizeOffsetEvalType SizeOffsetPair = Eval.compute(AI.CB);
6134         assert(SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown() &&
6135                cast<ConstantInt>(SizeOffsetPair.second)->isZero());
6136         Size = SizeOffsetPair.first;
6137       }
6138 
6139       Align Alignment(1);
6140       if (MaybeAlign RetAlign = AI.CB->getRetAlign())
6141         Alignment = max(Alignment, RetAlign);
6142       if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
6143         Optional<APInt> AlignmentAPI = getAPInt(A, *this, *Align);
6144         assert(AlignmentAPI.hasValue() &&
6145                "Expected an alignment during manifest!");
6146         Alignment =
6147             max(Alignment, MaybeAlign(AlignmentAPI.getValue().getZExtValue()));
6148       }
6149 
6150       // TODO: Hoist the alloca towards the function entry.
6151       unsigned AS = DL.getAllocaAddrSpace();
6152       Instruction *Alloca = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
6153                                            Size, Alignment, "", AI.CB);
6154 
6155       if (Alloca->getType() != AI.CB->getType())
6156         Alloca = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
6157             Alloca, AI.CB->getType(), "malloc_cast", AI.CB);
6158 
6159       auto *I8Ty = Type::getInt8Ty(F->getContext());
6160       auto *InitVal = getInitialValueOfAllocation(AI.CB, TLI, I8Ty);
6161       assert(InitVal &&
6162              "Must be able to materialize initial memory state of allocation");
6163 
6164       A.changeValueAfterManifest(*AI.CB, *Alloca);
6165 
6166       if (auto *II = dyn_cast<InvokeInst>(AI.CB)) {
6167         auto *NBB = II->getNormalDest();
6168         BranchInst::Create(NBB, AI.CB->getParent());
6169         A.deleteAfterManifest(*AI.CB);
6170       } else {
6171         A.deleteAfterManifest(*AI.CB);
6172       }
6173 
6174       // Initialize the alloca with the same value as used by the allocation
6175       // function.  We can skip undef as the initial value of an alloc is
6176       // undef, and the memset would simply end up being DSEd.
6177       if (!isa<UndefValue>(InitVal)) {
6178         IRBuilder<> Builder(Alloca->getNextNode());
6179         // TODO: Use alignment above if align!=1
6180         Builder.CreateMemSet(Alloca, InitVal, Size, None);
6181       }
6182       HasChanged = ChangeStatus::CHANGED;
6183     }
6184 
6185     return HasChanged;
6186   }
6187 
6188   Optional<APInt> getAPInt(Attributor &A, const AbstractAttribute &AA,
6189                            Value &V) {
6190     bool UsedAssumedInformation = false;
6191     Optional<Constant *> SimpleV =
6192         A.getAssumedConstant(V, AA, UsedAssumedInformation);
6193     if (!SimpleV.hasValue())
6194       return APInt(64, 0);
6195     if (auto *CI = dyn_cast_or_null<ConstantInt>(SimpleV.getValue()))
6196       return CI->getValue();
6197     return llvm::None;
6198   }
6199 
6200   Optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA,
6201                           AllocationInfo &AI) {
6202     auto Mapper = [&](const Value *V) -> const Value * {
6203       bool UsedAssumedInformation = false;
6204       if (Optional<Constant *> SimpleV =
6205               A.getAssumedConstant(*V, AA, UsedAssumedInformation))
6206         if (*SimpleV)
6207           return *SimpleV;
6208       return V;
6209     };
6210 
6211     const Function *F = getAnchorScope();
6212     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6213     return getAllocSize(AI.CB, TLI, Mapper);
6214   }
6215 
6216   /// Collection of all malloc-like calls in a function with associated
6217   /// information.
6218   DenseMap<CallBase *, AllocationInfo *> AllocationInfos;
6219 
6220   /// Collection of all free-like calls in a function with associated
6221   /// information.
6222   DenseMap<CallBase *, DeallocationInfo *> DeallocationInfos;
6223 
6224   ChangeStatus updateImpl(Attributor &A) override;
6225 };
6226 
6227 ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) {
6228   ChangeStatus Changed = ChangeStatus::UNCHANGED;
6229   const Function *F = getAnchorScope();
6230   const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6231 
6232   const auto &LivenessAA =
6233       A.getAAFor<AAIsDead>(*this, IRPosition::function(*F), DepClassTy::NONE);
6234 
6235   MustBeExecutedContextExplorer &Explorer =
6236       A.getInfoCache().getMustBeExecutedContextExplorer();
6237 
6238   bool StackIsAccessibleByOtherThreads =
6239       A.getInfoCache().stackIsAccessibleByOtherThreads();
6240 
6241   // Flag to ensure we update our deallocation information at most once per
6242   // updateImpl call and only if we use the free check reasoning.
6243   bool HasUpdatedFrees = false;
6244 
6245   auto UpdateFrees = [&]() {
6246     HasUpdatedFrees = true;
6247 
6248     for (auto &It : DeallocationInfos) {
6249       DeallocationInfo &DI = *It.second;
6250       // For now we cannot use deallocations that have unknown inputs, skip
6251       // them.
6252       if (DI.MightFreeUnknownObjects)
6253         continue;
6254 
6255       // No need to analyze dead calls, ignore them instead.
6256       bool UsedAssumedInformation = false;
6257       if (A.isAssumedDead(*DI.CB, this, &LivenessAA, UsedAssumedInformation,
6258                           /* CheckBBLivenessOnly */ true))
6259         continue;
6260 
6261       // Use the optimistic version to get the freed objects, ignoring dead
6262       // branches etc.
6263       SmallVector<Value *, 8> Objects;
6264       if (!AA::getAssumedUnderlyingObjects(A, *DI.CB->getArgOperand(0), Objects,
6265                                            *this, DI.CB,
6266                                            UsedAssumedInformation)) {
6267         LLVM_DEBUG(
6268             dbgs()
6269             << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n");
6270         DI.MightFreeUnknownObjects = true;
6271         continue;
6272       }
6273 
6274       // Check each object explicitly.
6275       for (auto *Obj : Objects) {
6276         // Free of null and undef can be ignored as no-ops (or UB in the latter
6277         // case).
6278         if (isa<ConstantPointerNull>(Obj) || isa<UndefValue>(Obj))
6279           continue;
6280 
6281         CallBase *ObjCB = dyn_cast<CallBase>(Obj);
6282         if (!ObjCB) {
6283           LLVM_DEBUG(dbgs()
6284                      << "[H2S] Free of a non-call object: " << *Obj << "\n");
6285           DI.MightFreeUnknownObjects = true;
6286           continue;
6287         }
6288 
6289         AllocationInfo *AI = AllocationInfos.lookup(ObjCB);
6290         if (!AI) {
6291           LLVM_DEBUG(dbgs() << "[H2S] Free of a non-allocation object: " << *Obj
6292                             << "\n");
6293           DI.MightFreeUnknownObjects = true;
6294           continue;
6295         }
6296 
6297         DI.PotentialAllocationCalls.insert(ObjCB);
6298       }
6299     }
6300   };
6301 
6302   auto FreeCheck = [&](AllocationInfo &AI) {
6303     // If the stack is not accessible by other threads, the "must-free" logic
6304     // doesn't apply as the pointer could be shared and needs to be places in
6305     // "shareable" memory.
6306     if (!StackIsAccessibleByOtherThreads) {
6307       auto &NoSyncAA =
6308           A.getAAFor<AANoSync>(*this, getIRPosition(), DepClassTy::OPTIONAL);
6309       if (!NoSyncAA.isAssumedNoSync()) {
6310         LLVM_DEBUG(
6311             dbgs() << "[H2S] found an escaping use, stack is not accessible by "
6312                       "other threads and function is not nosync:\n");
6313         return false;
6314       }
6315     }
6316     if (!HasUpdatedFrees)
6317       UpdateFrees();
6318 
6319     // TODO: Allow multi exit functions that have different free calls.
6320     if (AI.PotentialFreeCalls.size() != 1) {
6321       LLVM_DEBUG(dbgs() << "[H2S] did not find one free call but "
6322                         << AI.PotentialFreeCalls.size() << "\n");
6323       return false;
6324     }
6325     CallBase *UniqueFree = *AI.PotentialFreeCalls.begin();
6326     DeallocationInfo *DI = DeallocationInfos.lookup(UniqueFree);
6327     if (!DI) {
6328       LLVM_DEBUG(
6329           dbgs() << "[H2S] unique free call was not known as deallocation call "
6330                  << *UniqueFree << "\n");
6331       return false;
6332     }
6333     if (DI->MightFreeUnknownObjects) {
6334       LLVM_DEBUG(
6335           dbgs() << "[H2S] unique free call might free unknown allocations\n");
6336       return false;
6337     }
6338     if (DI->PotentialAllocationCalls.size() > 1) {
6339       LLVM_DEBUG(dbgs() << "[H2S] unique free call might free "
6340                         << DI->PotentialAllocationCalls.size()
6341                         << " different allocations\n");
6342       return false;
6343     }
6344     if (*DI->PotentialAllocationCalls.begin() != AI.CB) {
6345       LLVM_DEBUG(
6346           dbgs()
6347           << "[H2S] unique free call not known to free this allocation but "
6348           << **DI->PotentialAllocationCalls.begin() << "\n");
6349       return false;
6350     }
6351     Instruction *CtxI = isa<InvokeInst>(AI.CB) ? AI.CB : AI.CB->getNextNode();
6352     if (!Explorer.findInContextOf(UniqueFree, CtxI)) {
6353       LLVM_DEBUG(
6354           dbgs()
6355           << "[H2S] unique free call might not be executed with the allocation "
6356           << *UniqueFree << "\n");
6357       return false;
6358     }
6359     return true;
6360   };
6361 
6362   auto UsesCheck = [&](AllocationInfo &AI) {
6363     bool ValidUsesOnly = true;
6364 
6365     auto Pred = [&](const Use &U, bool &Follow) -> bool {
6366       Instruction *UserI = cast<Instruction>(U.getUser());
6367       if (isa<LoadInst>(UserI))
6368         return true;
6369       if (auto *SI = dyn_cast<StoreInst>(UserI)) {
6370         if (SI->getValueOperand() == U.get()) {
6371           LLVM_DEBUG(dbgs()
6372                      << "[H2S] escaping store to memory: " << *UserI << "\n");
6373           ValidUsesOnly = false;
6374         } else {
6375           // A store into the malloc'ed memory is fine.
6376         }
6377         return true;
6378       }
6379       if (auto *CB = dyn_cast<CallBase>(UserI)) {
6380         if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
6381           return true;
6382         if (DeallocationInfos.count(CB)) {
6383           AI.PotentialFreeCalls.insert(CB);
6384           return true;
6385         }
6386 
6387         unsigned ArgNo = CB->getArgOperandNo(&U);
6388 
6389         const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
6390             *this, IRPosition::callsite_argument(*CB, ArgNo),
6391             DepClassTy::OPTIONAL);
6392 
6393         // If a call site argument use is nofree, we are fine.
6394         const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
6395             *this, IRPosition::callsite_argument(*CB, ArgNo),
6396             DepClassTy::OPTIONAL);
6397 
6398         bool MaybeCaptured = !NoCaptureAA.isAssumedNoCapture();
6399         bool MaybeFreed = !ArgNoFreeAA.isAssumedNoFree();
6400         if (MaybeCaptured ||
6401             (AI.LibraryFunctionId != LibFunc___kmpc_alloc_shared &&
6402              MaybeFreed)) {
6403           AI.HasPotentiallyFreeingUnknownUses |= MaybeFreed;
6404 
6405           // Emit a missed remark if this is missed OpenMP globalization.
6406           auto Remark = [&](OptimizationRemarkMissed ORM) {
6407             return ORM
6408                    << "Could not move globalized variable to the stack. "
6409                       "Variable is potentially captured in call. Mark "
6410                       "parameter as `__attribute__((noescape))` to override.";
6411           };
6412 
6413           if (ValidUsesOnly &&
6414               AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
6415             A.emitRemark<OptimizationRemarkMissed>(CB, "OMP113", Remark);
6416 
6417           LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
6418           ValidUsesOnly = false;
6419         }
6420         return true;
6421       }
6422 
6423       if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
6424           isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
6425         Follow = true;
6426         return true;
6427       }
6428       // Unknown user for which we can not track uses further (in a way that
6429       // makes sense).
6430       LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
6431       ValidUsesOnly = false;
6432       return true;
6433     };
6434     if (!A.checkForAllUses(Pred, *this, *AI.CB))
6435       return false;
6436     return ValidUsesOnly;
6437   };
6438 
6439   // The actual update starts here. We look at all allocations and depending on
6440   // their status perform the appropriate check(s).
6441   for (auto &It : AllocationInfos) {
6442     AllocationInfo &AI = *It.second;
6443     if (AI.Status == AllocationInfo::INVALID)
6444       continue;
6445 
6446     if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
6447       Optional<APInt> APAlign = getAPInt(A, *this, *Align);
6448       if (!APAlign) {
6449         // Can't generate an alloca which respects the required alignment
6450         // on the allocation.
6451         LLVM_DEBUG(dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CB
6452                           << "\n");
6453         AI.Status = AllocationInfo::INVALID;
6454         Changed = ChangeStatus::CHANGED;
6455         continue;
6456       } else {
6457         if (APAlign->ugt(llvm::Value::MaximumAlignment) || !APAlign->isPowerOf2()) {
6458           LLVM_DEBUG(dbgs() << "[H2S] Invalid allocation alignment: " << APAlign << "\n");
6459           AI.Status = AllocationInfo::INVALID;
6460           Changed = ChangeStatus::CHANGED;
6461           continue;
6462         }
6463       }
6464     }
6465 
6466     if (MaxHeapToStackSize != -1) {
6467       Optional<APInt> Size = getSize(A, *this, AI);
6468       if (!Size.hasValue() || Size.getValue().ugt(MaxHeapToStackSize)) {
6469         LLVM_DEBUG({
6470           if (!Size.hasValue())
6471             dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n";
6472           else
6473             dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. "
6474                    << MaxHeapToStackSize << "\n";
6475         });
6476 
6477         AI.Status = AllocationInfo::INVALID;
6478         Changed = ChangeStatus::CHANGED;
6479         continue;
6480       }
6481     }
6482 
6483     switch (AI.Status) {
6484     case AllocationInfo::STACK_DUE_TO_USE:
6485       if (UsesCheck(AI))
6486         continue;
6487       AI.Status = AllocationInfo::STACK_DUE_TO_FREE;
6488       LLVM_FALLTHROUGH;
6489     case AllocationInfo::STACK_DUE_TO_FREE:
6490       if (FreeCheck(AI))
6491         continue;
6492       AI.Status = AllocationInfo::INVALID;
6493       Changed = ChangeStatus::CHANGED;
6494       continue;
6495     case AllocationInfo::INVALID:
6496       llvm_unreachable("Invalid allocations should never reach this point!");
6497     };
6498   }
6499 
6500   return Changed;
6501 }
6502 } // namespace
6503 
6504 /// ----------------------- Privatizable Pointers ------------------------------
6505 namespace {
6506 struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
6507   AAPrivatizablePtrImpl(const IRPosition &IRP, Attributor &A)
6508       : AAPrivatizablePtr(IRP, A), PrivatizableType(llvm::None) {}
6509 
6510   ChangeStatus indicatePessimisticFixpoint() override {
6511     AAPrivatizablePtr::indicatePessimisticFixpoint();
6512     PrivatizableType = nullptr;
6513     return ChangeStatus::CHANGED;
6514   }
6515 
6516   /// Identify the type we can chose for a private copy of the underlying
6517   /// argument. None means it is not clear yet, nullptr means there is none.
6518   virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
6519 
6520   /// Return a privatizable type that encloses both T0 and T1.
6521   /// TODO: This is merely a stub for now as we should manage a mapping as well.
6522   Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
6523     if (!T0.hasValue())
6524       return T1;
6525     if (!T1.hasValue())
6526       return T0;
6527     if (T0 == T1)
6528       return T0;
6529     return nullptr;
6530   }
6531 
6532   Optional<Type *> getPrivatizableType() const override {
6533     return PrivatizableType;
6534   }
6535 
6536   const std::string getAsStr() const override {
6537     return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
6538   }
6539 
6540 protected:
6541   Optional<Type *> PrivatizableType;
6542 };
6543 
6544 // TODO: Do this for call site arguments (probably also other values) as well.
6545 
6546 struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
6547   AAPrivatizablePtrArgument(const IRPosition &IRP, Attributor &A)
6548       : AAPrivatizablePtrImpl(IRP, A) {}
6549 
6550   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
6551   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
6552     // If this is a byval argument and we know all the call sites (so we can
6553     // rewrite them), there is no need to check them explicitly.
6554     bool UsedAssumedInformation = false;
6555     if (getIRPosition().hasAttr(Attribute::ByVal) &&
6556         A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
6557                                true, UsedAssumedInformation))
6558       return getAssociatedValue().getType()->getPointerElementType();
6559 
6560     Optional<Type *> Ty;
6561     unsigned ArgNo = getIRPosition().getCallSiteArgNo();
6562 
6563     // Make sure the associated call site argument has the same type at all call
6564     // sites and it is an allocation we know is safe to privatize, for now that
6565     // means we only allow alloca instructions.
6566     // TODO: We can additionally analyze the accesses in the callee to  create
6567     //       the type from that information instead. That is a little more
6568     //       involved and will be done in a follow up patch.
6569     auto CallSiteCheck = [&](AbstractCallSite ACS) {
6570       IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
6571       // Check if a coresponding argument was found or if it is one not
6572       // associated (which can happen for callback calls).
6573       if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
6574         return false;
6575 
6576       // Check that all call sites agree on a type.
6577       auto &PrivCSArgAA =
6578           A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos, DepClassTy::REQUIRED);
6579       Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
6580 
6581       LLVM_DEBUG({
6582         dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
6583         if (CSTy.hasValue() && CSTy.getValue())
6584           CSTy.getValue()->print(dbgs());
6585         else if (CSTy.hasValue())
6586           dbgs() << "<nullptr>";
6587         else
6588           dbgs() << "<none>";
6589       });
6590 
6591       Ty = combineTypes(Ty, CSTy);
6592 
6593       LLVM_DEBUG({
6594         dbgs() << " : New Type: ";
6595         if (Ty.hasValue() && Ty.getValue())
6596           Ty.getValue()->print(dbgs());
6597         else if (Ty.hasValue())
6598           dbgs() << "<nullptr>";
6599         else
6600           dbgs() << "<none>";
6601         dbgs() << "\n";
6602       });
6603 
6604       return !Ty.hasValue() || Ty.getValue();
6605     };
6606 
6607     if (!A.checkForAllCallSites(CallSiteCheck, *this, true,
6608                                 UsedAssumedInformation))
6609       return nullptr;
6610     return Ty;
6611   }
6612 
6613   /// See AbstractAttribute::updateImpl(...).
6614   ChangeStatus updateImpl(Attributor &A) override {
6615     PrivatizableType = identifyPrivatizableType(A);
6616     if (!PrivatizableType.hasValue())
6617       return ChangeStatus::UNCHANGED;
6618     if (!PrivatizableType.getValue())
6619       return indicatePessimisticFixpoint();
6620 
6621     // The dependence is optional so we don't give up once we give up on the
6622     // alignment.
6623     A.getAAFor<AAAlign>(*this, IRPosition::value(getAssociatedValue()),
6624                         DepClassTy::OPTIONAL);
6625 
6626     // Avoid arguments with padding for now.
6627     if (!getIRPosition().hasAttr(Attribute::ByVal) &&
6628         !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
6629                                                 A.getInfoCache().getDL())) {
6630       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
6631       return indicatePessimisticFixpoint();
6632     }
6633 
6634     // Collect the types that will replace the privatizable type in the function
6635     // signature.
6636     SmallVector<Type *, 16> ReplacementTypes;
6637     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
6638 
6639     // Verify callee and caller agree on how the promoted argument would be
6640     // passed.
6641     Function &Fn = *getIRPosition().getAnchorScope();
6642     const auto *TTI =
6643         A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
6644     if (!TTI) {
6645       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Missing TTI for function "
6646                         << Fn.getName() << "\n");
6647       return indicatePessimisticFixpoint();
6648     }
6649 
6650     auto CallSiteCheck = [&](AbstractCallSite ACS) {
6651       CallBase *CB = ACS.getInstruction();
6652       return TTI->areTypesABICompatible(
6653           CB->getCaller(), CB->getCalledFunction(), ReplacementTypes);
6654     };
6655     bool UsedAssumedInformation = false;
6656     if (!A.checkForAllCallSites(CallSiteCheck, *this, true,
6657                                 UsedAssumedInformation)) {
6658       LLVM_DEBUG(
6659           dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
6660                  << Fn.getName() << "\n");
6661       return indicatePessimisticFixpoint();
6662     }
6663 
6664     // Register a rewrite of the argument.
6665     Argument *Arg = getAssociatedArgument();
6666     if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
6667       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
6668       return indicatePessimisticFixpoint();
6669     }
6670 
6671     unsigned ArgNo = Arg->getArgNo();
6672 
6673     // Helper to check if for the given call site the associated argument is
6674     // passed to a callback where the privatization would be different.
6675     auto IsCompatiblePrivArgOfCallback = [&](CallBase &CB) {
6676       SmallVector<const Use *, 4> CallbackUses;
6677       AbstractCallSite::getCallbackUses(CB, CallbackUses);
6678       for (const Use *U : CallbackUses) {
6679         AbstractCallSite CBACS(U);
6680         assert(CBACS && CBACS.isCallbackCall());
6681         for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
6682           int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
6683 
6684           LLVM_DEBUG({
6685             dbgs()
6686                 << "[AAPrivatizablePtr] Argument " << *Arg
6687                 << "check if can be privatized in the context of its parent ("
6688                 << Arg->getParent()->getName()
6689                 << ")\n[AAPrivatizablePtr] because it is an argument in a "
6690                    "callback ("
6691                 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
6692                 << ")\n[AAPrivatizablePtr] " << CBArg << " : "
6693                 << CBACS.getCallArgOperand(CBArg) << " vs "
6694                 << CB.getArgOperand(ArgNo) << "\n"
6695                 << "[AAPrivatizablePtr] " << CBArg << " : "
6696                 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
6697           });
6698 
6699           if (CBArgNo != int(ArgNo))
6700             continue;
6701           const auto &CBArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
6702               *this, IRPosition::argument(CBArg), DepClassTy::REQUIRED);
6703           if (CBArgPrivAA.isValidState()) {
6704             auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
6705             if (!CBArgPrivTy.hasValue())
6706               continue;
6707             if (CBArgPrivTy.getValue() == PrivatizableType)
6708               continue;
6709           }
6710 
6711           LLVM_DEBUG({
6712             dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
6713                    << " cannot be privatized in the context of its parent ("
6714                    << Arg->getParent()->getName()
6715                    << ")\n[AAPrivatizablePtr] because it is an argument in a "
6716                       "callback ("
6717                    << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
6718                    << ").\n[AAPrivatizablePtr] for which the argument "
6719                       "privatization is not compatible.\n";
6720           });
6721           return false;
6722         }
6723       }
6724       return true;
6725     };
6726 
6727     // Helper to check if for the given call site the associated argument is
6728     // passed to a direct call where the privatization would be different.
6729     auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
6730       CallBase *DC = cast<CallBase>(ACS.getInstruction());
6731       int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
6732       assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->arg_size() &&
6733              "Expected a direct call operand for callback call operand");
6734 
6735       LLVM_DEBUG({
6736         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
6737                << " check if be privatized in the context of its parent ("
6738                << Arg->getParent()->getName()
6739                << ")\n[AAPrivatizablePtr] because it is an argument in a "
6740                   "direct call of ("
6741                << DCArgNo << "@" << DC->getCalledFunction()->getName()
6742                << ").\n";
6743       });
6744 
6745       Function *DCCallee = DC->getCalledFunction();
6746       if (unsigned(DCArgNo) < DCCallee->arg_size()) {
6747         const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
6748             *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)),
6749             DepClassTy::REQUIRED);
6750         if (DCArgPrivAA.isValidState()) {
6751           auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
6752           if (!DCArgPrivTy.hasValue())
6753             return true;
6754           if (DCArgPrivTy.getValue() == PrivatizableType)
6755             return true;
6756         }
6757       }
6758 
6759       LLVM_DEBUG({
6760         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
6761                << " cannot be privatized in the context of its parent ("
6762                << Arg->getParent()->getName()
6763                << ")\n[AAPrivatizablePtr] because it is an argument in a "
6764                   "direct call of ("
6765                << ACS.getInstruction()->getCalledFunction()->getName()
6766                << ").\n[AAPrivatizablePtr] for which the argument "
6767                   "privatization is not compatible.\n";
6768       });
6769       return false;
6770     };
6771 
6772     // Helper to check if the associated argument is used at the given abstract
6773     // call site in a way that is incompatible with the privatization assumed
6774     // here.
6775     auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
6776       if (ACS.isDirectCall())
6777         return IsCompatiblePrivArgOfCallback(*ACS.getInstruction());
6778       if (ACS.isCallbackCall())
6779         return IsCompatiblePrivArgOfDirectCS(ACS);
6780       return false;
6781     };
6782 
6783     if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
6784                                 UsedAssumedInformation))
6785       return indicatePessimisticFixpoint();
6786 
6787     return ChangeStatus::UNCHANGED;
6788   }
6789 
6790   /// Given a type to private \p PrivType, collect the constituates (which are
6791   /// used) in \p ReplacementTypes.
6792   static void
6793   identifyReplacementTypes(Type *PrivType,
6794                            SmallVectorImpl<Type *> &ReplacementTypes) {
6795     // TODO: For now we expand the privatization type to the fullest which can
6796     //       lead to dead arguments that need to be removed later.
6797     assert(PrivType && "Expected privatizable type!");
6798 
6799     // Traverse the type, extract constituate types on the outermost level.
6800     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6801       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
6802         ReplacementTypes.push_back(PrivStructType->getElementType(u));
6803     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6804       ReplacementTypes.append(PrivArrayType->getNumElements(),
6805                               PrivArrayType->getElementType());
6806     } else {
6807       ReplacementTypes.push_back(PrivType);
6808     }
6809   }
6810 
6811   /// Initialize \p Base according to the type \p PrivType at position \p IP.
6812   /// The values needed are taken from the arguments of \p F starting at
6813   /// position \p ArgNo.
6814   static void createInitialization(Type *PrivType, Value &Base, Function &F,
6815                                    unsigned ArgNo, Instruction &IP) {
6816     assert(PrivType && "Expected privatizable type!");
6817 
6818     IRBuilder<NoFolder> IRB(&IP);
6819     const DataLayout &DL = F.getParent()->getDataLayout();
6820 
6821     // Traverse the type, build GEPs and stores.
6822     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6823       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
6824       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
6825         Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
6826         Value *Ptr =
6827             constructPointer(PointeeTy, PrivType, &Base,
6828                              PrivStructLayout->getElementOffset(u), IRB, DL);
6829         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
6830       }
6831     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6832       Type *PointeeTy = PrivArrayType->getElementType();
6833       Type *PointeePtrTy = PointeeTy->getPointerTo();
6834       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
6835       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
6836         Value *Ptr = constructPointer(PointeePtrTy, PrivType, &Base,
6837                                       u * PointeeTySize, IRB, DL);
6838         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
6839       }
6840     } else {
6841       new StoreInst(F.getArg(ArgNo), &Base, &IP);
6842     }
6843   }
6844 
6845   /// Extract values from \p Base according to the type \p PrivType at the
6846   /// call position \p ACS. The values are appended to \p ReplacementValues.
6847   void createReplacementValues(Align Alignment, Type *PrivType,
6848                                AbstractCallSite ACS, Value *Base,
6849                                SmallVectorImpl<Value *> &ReplacementValues) {
6850     assert(Base && "Expected base value!");
6851     assert(PrivType && "Expected privatizable type!");
6852     Instruction *IP = ACS.getInstruction();
6853 
6854     IRBuilder<NoFolder> IRB(IP);
6855     const DataLayout &DL = IP->getModule()->getDataLayout();
6856 
6857     Type *PrivPtrType = PrivType->getPointerTo();
6858     if (Base->getType() != PrivPtrType)
6859       Base = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
6860           Base, PrivPtrType, "", ACS.getInstruction());
6861 
6862     // Traverse the type, build GEPs and loads.
6863     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6864       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
6865       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
6866         Type *PointeeTy = PrivStructType->getElementType(u);
6867         Value *Ptr =
6868             constructPointer(PointeeTy->getPointerTo(), PrivType, Base,
6869                              PrivStructLayout->getElementOffset(u), IRB, DL);
6870         LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
6871         L->setAlignment(Alignment);
6872         ReplacementValues.push_back(L);
6873       }
6874     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6875       Type *PointeeTy = PrivArrayType->getElementType();
6876       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
6877       Type *PointeePtrTy = PointeeTy->getPointerTo();
6878       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
6879         Value *Ptr = constructPointer(PointeePtrTy, PrivType, Base,
6880                                       u * PointeeTySize, IRB, DL);
6881         LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
6882         L->setAlignment(Alignment);
6883         ReplacementValues.push_back(L);
6884       }
6885     } else {
6886       LoadInst *L = new LoadInst(PrivType, Base, "", IP);
6887       L->setAlignment(Alignment);
6888       ReplacementValues.push_back(L);
6889     }
6890   }
6891 
6892   /// See AbstractAttribute::manifest(...)
6893   ChangeStatus manifest(Attributor &A) override {
6894     if (!PrivatizableType.hasValue())
6895       return ChangeStatus::UNCHANGED;
6896     assert(PrivatizableType.getValue() && "Expected privatizable type!");
6897 
6898     // Collect all tail calls in the function as we cannot allow new allocas to
6899     // escape into tail recursion.
6900     // TODO: Be smarter about new allocas escaping into tail calls.
6901     SmallVector<CallInst *, 16> TailCalls;
6902     bool UsedAssumedInformation = false;
6903     if (!A.checkForAllInstructions(
6904             [&](Instruction &I) {
6905               CallInst &CI = cast<CallInst>(I);
6906               if (CI.isTailCall())
6907                 TailCalls.push_back(&CI);
6908               return true;
6909             },
6910             *this, {Instruction::Call}, UsedAssumedInformation))
6911       return ChangeStatus::UNCHANGED;
6912 
6913     Argument *Arg = getAssociatedArgument();
6914     // Query AAAlign attribute for alignment of associated argument to
6915     // determine the best alignment of loads.
6916     const auto &AlignAA =
6917         A.getAAFor<AAAlign>(*this, IRPosition::value(*Arg), DepClassTy::NONE);
6918 
6919     // Callback to repair the associated function. A new alloca is placed at the
6920     // beginning and initialized with the values passed through arguments. The
6921     // new alloca replaces the use of the old pointer argument.
6922     Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
6923         [=](const Attributor::ArgumentReplacementInfo &ARI,
6924             Function &ReplacementFn, Function::arg_iterator ArgIt) {
6925           BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
6926           Instruction *IP = &*EntryBB.getFirstInsertionPt();
6927           const DataLayout &DL = IP->getModule()->getDataLayout();
6928           unsigned AS = DL.getAllocaAddrSpace();
6929           Instruction *AI = new AllocaInst(PrivatizableType.getValue(), AS,
6930                                            Arg->getName() + ".priv", IP);
6931           createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
6932                                ArgIt->getArgNo(), *IP);
6933 
6934           if (AI->getType() != Arg->getType())
6935             AI = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
6936                 AI, Arg->getType(), "", IP);
6937           Arg->replaceAllUsesWith(AI);
6938 
6939           for (CallInst *CI : TailCalls)
6940             CI->setTailCall(false);
6941         };
6942 
6943     // Callback to repair a call site of the associated function. The elements
6944     // of the privatizable type are loaded prior to the call and passed to the
6945     // new function version.
6946     Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
6947         [=, &AlignAA](const Attributor::ArgumentReplacementInfo &ARI,
6948                       AbstractCallSite ACS,
6949                       SmallVectorImpl<Value *> &NewArgOperands) {
6950           // When no alignment is specified for the load instruction,
6951           // natural alignment is assumed.
6952           createReplacementValues(
6953               assumeAligned(AlignAA.getAssumedAlign()),
6954               PrivatizableType.getValue(), ACS,
6955               ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
6956               NewArgOperands);
6957         };
6958 
6959     // Collect the types that will replace the privatizable type in the function
6960     // signature.
6961     SmallVector<Type *, 16> ReplacementTypes;
6962     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
6963 
6964     // Register a rewrite of the argument.
6965     if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
6966                                            std::move(FnRepairCB),
6967                                            std::move(ACSRepairCB)))
6968       return ChangeStatus::CHANGED;
6969     return ChangeStatus::UNCHANGED;
6970   }
6971 
6972   /// See AbstractAttribute::trackStatistics()
6973   void trackStatistics() const override {
6974     STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
6975   }
6976 };
6977 
6978 struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
6979   AAPrivatizablePtrFloating(const IRPosition &IRP, Attributor &A)
6980       : AAPrivatizablePtrImpl(IRP, A) {}
6981 
6982   /// See AbstractAttribute::initialize(...).
6983   virtual void initialize(Attributor &A) override {
6984     // TODO: We can privatize more than arguments.
6985     indicatePessimisticFixpoint();
6986   }
6987 
6988   ChangeStatus updateImpl(Attributor &A) override {
6989     llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
6990                      "updateImpl will not be called");
6991   }
6992 
6993   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
6994   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
6995     Value *Obj = getUnderlyingObject(&getAssociatedValue());
6996     if (!Obj) {
6997       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
6998       return nullptr;
6999     }
7000 
7001     if (auto *AI = dyn_cast<AllocaInst>(Obj))
7002       if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
7003         if (CI->isOne())
7004           return AI->getAllocatedType();
7005     if (auto *Arg = dyn_cast<Argument>(Obj)) {
7006       auto &PrivArgAA = A.getAAFor<AAPrivatizablePtr>(
7007           *this, IRPosition::argument(*Arg), DepClassTy::REQUIRED);
7008       if (PrivArgAA.isAssumedPrivatizablePtr())
7009         return Obj->getType()->getPointerElementType();
7010     }
7011 
7012     LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
7013                          "alloca nor privatizable argument: "
7014                       << *Obj << "!\n");
7015     return nullptr;
7016   }
7017 
7018   /// See AbstractAttribute::trackStatistics()
7019   void trackStatistics() const override {
7020     STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
7021   }
7022 };
7023 
7024 struct AAPrivatizablePtrCallSiteArgument final
7025     : public AAPrivatizablePtrFloating {
7026   AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP, Attributor &A)
7027       : AAPrivatizablePtrFloating(IRP, A) {}
7028 
7029   /// See AbstractAttribute::initialize(...).
7030   void initialize(Attributor &A) override {
7031     if (getIRPosition().hasAttr(Attribute::ByVal))
7032       indicateOptimisticFixpoint();
7033   }
7034 
7035   /// See AbstractAttribute::updateImpl(...).
7036   ChangeStatus updateImpl(Attributor &A) override {
7037     PrivatizableType = identifyPrivatizableType(A);
7038     if (!PrivatizableType.hasValue())
7039       return ChangeStatus::UNCHANGED;
7040     if (!PrivatizableType.getValue())
7041       return indicatePessimisticFixpoint();
7042 
7043     const IRPosition &IRP = getIRPosition();
7044     auto &NoCaptureAA =
7045         A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::REQUIRED);
7046     if (!NoCaptureAA.isAssumedNoCapture()) {
7047       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
7048       return indicatePessimisticFixpoint();
7049     }
7050 
7051     auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP, DepClassTy::REQUIRED);
7052     if (!NoAliasAA.isAssumedNoAlias()) {
7053       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
7054       return indicatePessimisticFixpoint();
7055     }
7056 
7057     bool IsKnown;
7058     if (!AA::isAssumedReadOnly(A, IRP, *this, IsKnown)) {
7059       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
7060       return indicatePessimisticFixpoint();
7061     }
7062 
7063     return ChangeStatus::UNCHANGED;
7064   }
7065 
7066   /// See AbstractAttribute::trackStatistics()
7067   void trackStatistics() const override {
7068     STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
7069   }
7070 };
7071 
7072 struct AAPrivatizablePtrCallSiteReturned final
7073     : public AAPrivatizablePtrFloating {
7074   AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP, Attributor &A)
7075       : AAPrivatizablePtrFloating(IRP, A) {}
7076 
7077   /// See AbstractAttribute::initialize(...).
7078   void initialize(Attributor &A) override {
7079     // TODO: We can privatize more than arguments.
7080     indicatePessimisticFixpoint();
7081   }
7082 
7083   /// See AbstractAttribute::trackStatistics()
7084   void trackStatistics() const override {
7085     STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
7086   }
7087 };
7088 
7089 struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
7090   AAPrivatizablePtrReturned(const IRPosition &IRP, Attributor &A)
7091       : AAPrivatizablePtrFloating(IRP, A) {}
7092 
7093   /// See AbstractAttribute::initialize(...).
7094   void initialize(Attributor &A) override {
7095     // TODO: We can privatize more than arguments.
7096     indicatePessimisticFixpoint();
7097   }
7098 
7099   /// See AbstractAttribute::trackStatistics()
7100   void trackStatistics() const override {
7101     STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
7102   }
7103 };
7104 } // namespace
7105 
7106 /// -------------------- Memory Behavior Attributes ----------------------------
7107 /// Includes read-none, read-only, and write-only.
7108 /// ----------------------------------------------------------------------------
7109 namespace {
7110 struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
7111   AAMemoryBehaviorImpl(const IRPosition &IRP, Attributor &A)
7112       : AAMemoryBehavior(IRP, A) {}
7113 
7114   /// See AbstractAttribute::initialize(...).
7115   void initialize(Attributor &A) override {
7116     intersectAssumedBits(BEST_STATE);
7117     getKnownStateFromValue(getIRPosition(), getState());
7118     AAMemoryBehavior::initialize(A);
7119   }
7120 
7121   /// Return the memory behavior information encoded in the IR for \p IRP.
7122   static void getKnownStateFromValue(const IRPosition &IRP,
7123                                      BitIntegerState &State,
7124                                      bool IgnoreSubsumingPositions = false) {
7125     SmallVector<Attribute, 2> Attrs;
7126     IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
7127     for (const Attribute &Attr : Attrs) {
7128       switch (Attr.getKindAsEnum()) {
7129       case Attribute::ReadNone:
7130         State.addKnownBits(NO_ACCESSES);
7131         break;
7132       case Attribute::ReadOnly:
7133         State.addKnownBits(NO_WRITES);
7134         break;
7135       case Attribute::WriteOnly:
7136         State.addKnownBits(NO_READS);
7137         break;
7138       default:
7139         llvm_unreachable("Unexpected attribute!");
7140       }
7141     }
7142 
7143     if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
7144       if (!I->mayReadFromMemory())
7145         State.addKnownBits(NO_READS);
7146       if (!I->mayWriteToMemory())
7147         State.addKnownBits(NO_WRITES);
7148     }
7149   }
7150 
7151   /// See AbstractAttribute::getDeducedAttributes(...).
7152   void getDeducedAttributes(LLVMContext &Ctx,
7153                             SmallVectorImpl<Attribute> &Attrs) const override {
7154     assert(Attrs.size() == 0);
7155     if (isAssumedReadNone())
7156       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
7157     else if (isAssumedReadOnly())
7158       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
7159     else if (isAssumedWriteOnly())
7160       Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
7161     assert(Attrs.size() <= 1);
7162   }
7163 
7164   /// See AbstractAttribute::manifest(...).
7165   ChangeStatus manifest(Attributor &A) override {
7166     if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true))
7167       return ChangeStatus::UNCHANGED;
7168 
7169     const IRPosition &IRP = getIRPosition();
7170 
7171     // Check if we would improve the existing attributes first.
7172     SmallVector<Attribute, 4> DeducedAttrs;
7173     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
7174     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
7175           return IRP.hasAttr(Attr.getKindAsEnum(),
7176                              /* IgnoreSubsumingPositions */ true);
7177         }))
7178       return ChangeStatus::UNCHANGED;
7179 
7180     // Clear existing attributes.
7181     IRP.removeAttrs(AttrKinds);
7182 
7183     // Use the generic manifest method.
7184     return IRAttribute::manifest(A);
7185   }
7186 
7187   /// See AbstractState::getAsStr().
7188   const std::string getAsStr() const override {
7189     if (isAssumedReadNone())
7190       return "readnone";
7191     if (isAssumedReadOnly())
7192       return "readonly";
7193     if (isAssumedWriteOnly())
7194       return "writeonly";
7195     return "may-read/write";
7196   }
7197 
7198   /// The set of IR attributes AAMemoryBehavior deals with.
7199   static const Attribute::AttrKind AttrKinds[3];
7200 };
7201 
7202 const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
7203     Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
7204 
7205 /// Memory behavior attribute for a floating value.
7206 struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
7207   AAMemoryBehaviorFloating(const IRPosition &IRP, Attributor &A)
7208       : AAMemoryBehaviorImpl(IRP, A) {}
7209 
7210   /// See AbstractAttribute::updateImpl(...).
7211   ChangeStatus updateImpl(Attributor &A) override;
7212 
7213   /// See AbstractAttribute::trackStatistics()
7214   void trackStatistics() const override {
7215     if (isAssumedReadNone())
7216       STATS_DECLTRACK_FLOATING_ATTR(readnone)
7217     else if (isAssumedReadOnly())
7218       STATS_DECLTRACK_FLOATING_ATTR(readonly)
7219     else if (isAssumedWriteOnly())
7220       STATS_DECLTRACK_FLOATING_ATTR(writeonly)
7221   }
7222 
7223 private:
7224   /// Return true if users of \p UserI might access the underlying
7225   /// variable/location described by \p U and should therefore be analyzed.
7226   bool followUsersOfUseIn(Attributor &A, const Use &U,
7227                           const Instruction *UserI);
7228 
7229   /// Update the state according to the effect of use \p U in \p UserI.
7230   void analyzeUseIn(Attributor &A, const Use &U, const Instruction *UserI);
7231 };
7232 
7233 /// Memory behavior attribute for function argument.
7234 struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
7235   AAMemoryBehaviorArgument(const IRPosition &IRP, Attributor &A)
7236       : AAMemoryBehaviorFloating(IRP, A) {}
7237 
7238   /// See AbstractAttribute::initialize(...).
7239   void initialize(Attributor &A) override {
7240     intersectAssumedBits(BEST_STATE);
7241     const IRPosition &IRP = getIRPosition();
7242     // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
7243     // can query it when we use has/getAttr. That would allow us to reuse the
7244     // initialize of the base class here.
7245     bool HasByVal =
7246         IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
7247     getKnownStateFromValue(IRP, getState(),
7248                            /* IgnoreSubsumingPositions */ HasByVal);
7249 
7250     // Initialize the use vector with all direct uses of the associated value.
7251     Argument *Arg = getAssociatedArgument();
7252     if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent())))
7253       indicatePessimisticFixpoint();
7254   }
7255 
7256   ChangeStatus manifest(Attributor &A) override {
7257     // TODO: Pointer arguments are not supported on vectors of pointers yet.
7258     if (!getAssociatedValue().getType()->isPointerTy())
7259       return ChangeStatus::UNCHANGED;
7260 
7261     // TODO: From readattrs.ll: "inalloca parameters are always
7262     //                           considered written"
7263     if (hasAttr({Attribute::InAlloca, Attribute::Preallocated})) {
7264       removeKnownBits(NO_WRITES);
7265       removeAssumedBits(NO_WRITES);
7266     }
7267     return AAMemoryBehaviorFloating::manifest(A);
7268   }
7269 
7270   /// See AbstractAttribute::trackStatistics()
7271   void trackStatistics() const override {
7272     if (isAssumedReadNone())
7273       STATS_DECLTRACK_ARG_ATTR(readnone)
7274     else if (isAssumedReadOnly())
7275       STATS_DECLTRACK_ARG_ATTR(readonly)
7276     else if (isAssumedWriteOnly())
7277       STATS_DECLTRACK_ARG_ATTR(writeonly)
7278   }
7279 };
7280 
7281 struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
7282   AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP, Attributor &A)
7283       : AAMemoryBehaviorArgument(IRP, A) {}
7284 
7285   /// See AbstractAttribute::initialize(...).
7286   void initialize(Attributor &A) override {
7287     // If we don't have an associated attribute this is either a variadic call
7288     // or an indirect call, either way, nothing to do here.
7289     Argument *Arg = getAssociatedArgument();
7290     if (!Arg) {
7291       indicatePessimisticFixpoint();
7292       return;
7293     }
7294     if (Arg->hasByValAttr()) {
7295       addKnownBits(NO_WRITES);
7296       removeKnownBits(NO_READS);
7297       removeAssumedBits(NO_READS);
7298     }
7299     AAMemoryBehaviorArgument::initialize(A);
7300     if (getAssociatedFunction()->isDeclaration())
7301       indicatePessimisticFixpoint();
7302   }
7303 
7304   /// See AbstractAttribute::updateImpl(...).
7305   ChangeStatus updateImpl(Attributor &A) override {
7306     // TODO: Once we have call site specific value information we can provide
7307     //       call site specific liveness liveness information and then it makes
7308     //       sense to specialize attributes for call sites arguments instead of
7309     //       redirecting requests to the callee argument.
7310     Argument *Arg = getAssociatedArgument();
7311     const IRPosition &ArgPos = IRPosition::argument(*Arg);
7312     auto &ArgAA =
7313         A.getAAFor<AAMemoryBehavior>(*this, ArgPos, DepClassTy::REQUIRED);
7314     return clampStateAndIndicateChange(getState(), ArgAA.getState());
7315   }
7316 
7317   /// See AbstractAttribute::trackStatistics()
7318   void trackStatistics() const override {
7319     if (isAssumedReadNone())
7320       STATS_DECLTRACK_CSARG_ATTR(readnone)
7321     else if (isAssumedReadOnly())
7322       STATS_DECLTRACK_CSARG_ATTR(readonly)
7323     else if (isAssumedWriteOnly())
7324       STATS_DECLTRACK_CSARG_ATTR(writeonly)
7325   }
7326 };
7327 
7328 /// Memory behavior attribute for a call site return position.
7329 struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
7330   AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP, Attributor &A)
7331       : AAMemoryBehaviorFloating(IRP, A) {}
7332 
7333   /// See AbstractAttribute::initialize(...).
7334   void initialize(Attributor &A) override {
7335     AAMemoryBehaviorImpl::initialize(A);
7336     Function *F = getAssociatedFunction();
7337     if (!F || F->isDeclaration())
7338       indicatePessimisticFixpoint();
7339   }
7340 
7341   /// See AbstractAttribute::manifest(...).
7342   ChangeStatus manifest(Attributor &A) override {
7343     // We do not annotate returned values.
7344     return ChangeStatus::UNCHANGED;
7345   }
7346 
7347   /// See AbstractAttribute::trackStatistics()
7348   void trackStatistics() const override {}
7349 };
7350 
7351 /// An AA to represent the memory behavior function attributes.
7352 struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
7353   AAMemoryBehaviorFunction(const IRPosition &IRP, Attributor &A)
7354       : AAMemoryBehaviorImpl(IRP, A) {}
7355 
7356   /// See AbstractAttribute::updateImpl(Attributor &A).
7357   virtual ChangeStatus updateImpl(Attributor &A) override;
7358 
7359   /// See AbstractAttribute::manifest(...).
7360   ChangeStatus manifest(Attributor &A) override {
7361     Function &F = cast<Function>(getAnchorValue());
7362     if (isAssumedReadNone()) {
7363       F.removeFnAttr(Attribute::ArgMemOnly);
7364       F.removeFnAttr(Attribute::InaccessibleMemOnly);
7365       F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
7366     }
7367     return AAMemoryBehaviorImpl::manifest(A);
7368   }
7369 
7370   /// See AbstractAttribute::trackStatistics()
7371   void trackStatistics() const override {
7372     if (isAssumedReadNone())
7373       STATS_DECLTRACK_FN_ATTR(readnone)
7374     else if (isAssumedReadOnly())
7375       STATS_DECLTRACK_FN_ATTR(readonly)
7376     else if (isAssumedWriteOnly())
7377       STATS_DECLTRACK_FN_ATTR(writeonly)
7378   }
7379 };
7380 
7381 /// AAMemoryBehavior attribute for call sites.
7382 struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
7383   AAMemoryBehaviorCallSite(const IRPosition &IRP, Attributor &A)
7384       : AAMemoryBehaviorImpl(IRP, A) {}
7385 
7386   /// See AbstractAttribute::initialize(...).
7387   void initialize(Attributor &A) override {
7388     AAMemoryBehaviorImpl::initialize(A);
7389     Function *F = getAssociatedFunction();
7390     if (!F || F->isDeclaration())
7391       indicatePessimisticFixpoint();
7392   }
7393 
7394   /// See AbstractAttribute::updateImpl(...).
7395   ChangeStatus updateImpl(Attributor &A) override {
7396     // TODO: Once we have call site specific value information we can provide
7397     //       call site specific liveness liveness information and then it makes
7398     //       sense to specialize attributes for call sites arguments instead of
7399     //       redirecting requests to the callee argument.
7400     Function *F = getAssociatedFunction();
7401     const IRPosition &FnPos = IRPosition::function(*F);
7402     auto &FnAA =
7403         A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::REQUIRED);
7404     return clampStateAndIndicateChange(getState(), FnAA.getState());
7405   }
7406 
7407   /// See AbstractAttribute::trackStatistics()
7408   void trackStatistics() const override {
7409     if (isAssumedReadNone())
7410       STATS_DECLTRACK_CS_ATTR(readnone)
7411     else if (isAssumedReadOnly())
7412       STATS_DECLTRACK_CS_ATTR(readonly)
7413     else if (isAssumedWriteOnly())
7414       STATS_DECLTRACK_CS_ATTR(writeonly)
7415   }
7416 };
7417 
7418 ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
7419 
7420   // The current assumed state used to determine a change.
7421   auto AssumedState = getAssumed();
7422 
7423   auto CheckRWInst = [&](Instruction &I) {
7424     // If the instruction has an own memory behavior state, use it to restrict
7425     // the local state. No further analysis is required as the other memory
7426     // state is as optimistic as it gets.
7427     if (const auto *CB = dyn_cast<CallBase>(&I)) {
7428       const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
7429           *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
7430       intersectAssumedBits(MemBehaviorAA.getAssumed());
7431       return !isAtFixpoint();
7432     }
7433 
7434     // Remove access kind modifiers if necessary.
7435     if (I.mayReadFromMemory())
7436       removeAssumedBits(NO_READS);
7437     if (I.mayWriteToMemory())
7438       removeAssumedBits(NO_WRITES);
7439     return !isAtFixpoint();
7440   };
7441 
7442   bool UsedAssumedInformation = false;
7443   if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
7444                                           UsedAssumedInformation))
7445     return indicatePessimisticFixpoint();
7446 
7447   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7448                                         : ChangeStatus::UNCHANGED;
7449 }
7450 
7451 ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
7452 
7453   const IRPosition &IRP = getIRPosition();
7454   const IRPosition &FnPos = IRPosition::function_scope(IRP);
7455   AAMemoryBehavior::StateType &S = getState();
7456 
7457   // First, check the function scope. We take the known information and we avoid
7458   // work if the assumed information implies the current assumed information for
7459   // this attribute. This is a valid for all but byval arguments.
7460   Argument *Arg = IRP.getAssociatedArgument();
7461   AAMemoryBehavior::base_t FnMemAssumedState =
7462       AAMemoryBehavior::StateType::getWorstState();
7463   if (!Arg || !Arg->hasByValAttr()) {
7464     const auto &FnMemAA =
7465         A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::OPTIONAL);
7466     FnMemAssumedState = FnMemAA.getAssumed();
7467     S.addKnownBits(FnMemAA.getKnown());
7468     if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
7469       return ChangeStatus::UNCHANGED;
7470   }
7471 
7472   // The current assumed state used to determine a change.
7473   auto AssumedState = S.getAssumed();
7474 
7475   // Make sure the value is not captured (except through "return"), if
7476   // it is, any information derived would be irrelevant anyway as we cannot
7477   // check the potential aliases introduced by the capture. However, no need
7478   // to fall back to anythign less optimistic than the function state.
7479   const auto &ArgNoCaptureAA =
7480       A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::OPTIONAL);
7481   if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
7482     S.intersectAssumedBits(FnMemAssumedState);
7483     return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7484                                           : ChangeStatus::UNCHANGED;
7485   }
7486 
7487   // Visit and expand uses until all are analyzed or a fixpoint is reached.
7488   auto UsePred = [&](const Use &U, bool &Follow) -> bool {
7489     Instruction *UserI = cast<Instruction>(U.getUser());
7490     LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserI
7491                       << " \n");
7492 
7493     // Droppable users, e.g., llvm::assume does not actually perform any action.
7494     if (UserI->isDroppable())
7495       return true;
7496 
7497     // Check if the users of UserI should also be visited.
7498     Follow = followUsersOfUseIn(A, U, UserI);
7499 
7500     // If UserI might touch memory we analyze the use in detail.
7501     if (UserI->mayReadOrWriteMemory())
7502       analyzeUseIn(A, U, UserI);
7503 
7504     return !isAtFixpoint();
7505   };
7506 
7507   if (!A.checkForAllUses(UsePred, *this, getAssociatedValue()))
7508     return indicatePessimisticFixpoint();
7509 
7510   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7511                                         : ChangeStatus::UNCHANGED;
7512 }
7513 
7514 bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use &U,
7515                                                   const Instruction *UserI) {
7516   // The loaded value is unrelated to the pointer argument, no need to
7517   // follow the users of the load.
7518   if (isa<LoadInst>(UserI))
7519     return false;
7520 
7521   // By default we follow all uses assuming UserI might leak information on U,
7522   // we have special handling for call sites operands though.
7523   const auto *CB = dyn_cast<CallBase>(UserI);
7524   if (!CB || !CB->isArgOperand(&U))
7525     return true;
7526 
7527   // If the use is a call argument known not to be captured, the users of
7528   // the call do not need to be visited because they have to be unrelated to
7529   // the input. Note that this check is not trivial even though we disallow
7530   // general capturing of the underlying argument. The reason is that the
7531   // call might the argument "through return", which we allow and for which we
7532   // need to check call users.
7533   if (U.get()->getType()->isPointerTy()) {
7534     unsigned ArgNo = CB->getArgOperandNo(&U);
7535     const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
7536         *this, IRPosition::callsite_argument(*CB, ArgNo), DepClassTy::OPTIONAL);
7537     return !ArgNoCaptureAA.isAssumedNoCapture();
7538   }
7539 
7540   return true;
7541 }
7542 
7543 void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use &U,
7544                                             const Instruction *UserI) {
7545   assert(UserI->mayReadOrWriteMemory());
7546 
7547   switch (UserI->getOpcode()) {
7548   default:
7549     // TODO: Handle all atomics and other side-effect operations we know of.
7550     break;
7551   case Instruction::Load:
7552     // Loads cause the NO_READS property to disappear.
7553     removeAssumedBits(NO_READS);
7554     return;
7555 
7556   case Instruction::Store:
7557     // Stores cause the NO_WRITES property to disappear if the use is the
7558     // pointer operand. Note that while capturing was taken care of somewhere
7559     // else we need to deal with stores of the value that is not looked through.
7560     if (cast<StoreInst>(UserI)->getPointerOperand() == U.get())
7561       removeAssumedBits(NO_WRITES);
7562     else
7563       indicatePessimisticFixpoint();
7564     return;
7565 
7566   case Instruction::Call:
7567   case Instruction::CallBr:
7568   case Instruction::Invoke: {
7569     // For call sites we look at the argument memory behavior attribute (this
7570     // could be recursive!) in order to restrict our own state.
7571     const auto *CB = cast<CallBase>(UserI);
7572 
7573     // Give up on operand bundles.
7574     if (CB->isBundleOperand(&U)) {
7575       indicatePessimisticFixpoint();
7576       return;
7577     }
7578 
7579     // Calling a function does read the function pointer, maybe write it if the
7580     // function is self-modifying.
7581     if (CB->isCallee(&U)) {
7582       removeAssumedBits(NO_READS);
7583       break;
7584     }
7585 
7586     // Adjust the possible access behavior based on the information on the
7587     // argument.
7588     IRPosition Pos;
7589     if (U.get()->getType()->isPointerTy())
7590       Pos = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
7591     else
7592       Pos = IRPosition::callsite_function(*CB);
7593     const auto &MemBehaviorAA =
7594         A.getAAFor<AAMemoryBehavior>(*this, Pos, DepClassTy::OPTIONAL);
7595     // "assumed" has at most the same bits as the MemBehaviorAA assumed
7596     // and at least "known".
7597     intersectAssumedBits(MemBehaviorAA.getAssumed());
7598     return;
7599   }
7600   };
7601 
7602   // Generally, look at the "may-properties" and adjust the assumed state if we
7603   // did not trigger special handling before.
7604   if (UserI->mayReadFromMemory())
7605     removeAssumedBits(NO_READS);
7606   if (UserI->mayWriteToMemory())
7607     removeAssumedBits(NO_WRITES);
7608 }
7609 } // namespace
7610 
7611 /// -------------------- Memory Locations Attributes ---------------------------
7612 /// Includes read-none, argmemonly, inaccessiblememonly,
7613 /// inaccessiblememorargmemonly
7614 /// ----------------------------------------------------------------------------
7615 
7616 std::string AAMemoryLocation::getMemoryLocationsAsStr(
7617     AAMemoryLocation::MemoryLocationsKind MLK) {
7618   if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS))
7619     return "all memory";
7620   if (MLK == AAMemoryLocation::NO_LOCATIONS)
7621     return "no memory";
7622   std::string S = "memory:";
7623   if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM))
7624     S += "stack,";
7625   if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM))
7626     S += "constant,";
7627   if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM))
7628     S += "internal global,";
7629   if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM))
7630     S += "external global,";
7631   if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM))
7632     S += "argument,";
7633   if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM))
7634     S += "inaccessible,";
7635   if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM))
7636     S += "malloced,";
7637   if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM))
7638     S += "unknown,";
7639   S.pop_back();
7640   return S;
7641 }
7642 
7643 namespace {
7644 struct AAMemoryLocationImpl : public AAMemoryLocation {
7645 
7646   AAMemoryLocationImpl(const IRPosition &IRP, Attributor &A)
7647       : AAMemoryLocation(IRP, A), Allocator(A.Allocator) {
7648     for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
7649       AccessKind2Accesses[u] = nullptr;
7650   }
7651 
7652   ~AAMemoryLocationImpl() {
7653     // The AccessSets are allocated via a BumpPtrAllocator, we call
7654     // the destructor manually.
7655     for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
7656       if (AccessKind2Accesses[u])
7657         AccessKind2Accesses[u]->~AccessSet();
7658   }
7659 
7660   /// See AbstractAttribute::initialize(...).
7661   void initialize(Attributor &A) override {
7662     intersectAssumedBits(BEST_STATE);
7663     getKnownStateFromValue(A, getIRPosition(), getState());
7664     AAMemoryLocation::initialize(A);
7665   }
7666 
7667   /// Return the memory behavior information encoded in the IR for \p IRP.
7668   static void getKnownStateFromValue(Attributor &A, const IRPosition &IRP,
7669                                      BitIntegerState &State,
7670                                      bool IgnoreSubsumingPositions = false) {
7671     // For internal functions we ignore `argmemonly` and
7672     // `inaccessiblememorargmemonly` as we might break it via interprocedural
7673     // constant propagation. It is unclear if this is the best way but it is
7674     // unlikely this will cause real performance problems. If we are deriving
7675     // attributes for the anchor function we even remove the attribute in
7676     // addition to ignoring it.
7677     bool UseArgMemOnly = true;
7678     Function *AnchorFn = IRP.getAnchorScope();
7679     if (AnchorFn && A.isRunOn(*AnchorFn))
7680       UseArgMemOnly = !AnchorFn->hasLocalLinkage();
7681 
7682     SmallVector<Attribute, 2> Attrs;
7683     IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
7684     for (const Attribute &Attr : Attrs) {
7685       switch (Attr.getKindAsEnum()) {
7686       case Attribute::ReadNone:
7687         State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM);
7688         break;
7689       case Attribute::InaccessibleMemOnly:
7690         State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
7691         break;
7692       case Attribute::ArgMemOnly:
7693         if (UseArgMemOnly)
7694           State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true));
7695         else
7696           IRP.removeAttrs({Attribute::ArgMemOnly});
7697         break;
7698       case Attribute::InaccessibleMemOrArgMemOnly:
7699         if (UseArgMemOnly)
7700           State.addKnownBits(inverseLocation(
7701               NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
7702         else
7703           IRP.removeAttrs({Attribute::InaccessibleMemOrArgMemOnly});
7704         break;
7705       default:
7706         llvm_unreachable("Unexpected attribute!");
7707       }
7708     }
7709   }
7710 
7711   /// See AbstractAttribute::getDeducedAttributes(...).
7712   void getDeducedAttributes(LLVMContext &Ctx,
7713                             SmallVectorImpl<Attribute> &Attrs) const override {
7714     assert(Attrs.size() == 0);
7715     if (isAssumedReadNone()) {
7716       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
7717     } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) {
7718       if (isAssumedInaccessibleMemOnly())
7719         Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly));
7720       else if (isAssumedArgMemOnly())
7721         Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly));
7722       else if (isAssumedInaccessibleOrArgMemOnly())
7723         Attrs.push_back(
7724             Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly));
7725     }
7726     assert(Attrs.size() <= 1);
7727   }
7728 
7729   /// See AbstractAttribute::manifest(...).
7730   ChangeStatus manifest(Attributor &A) override {
7731     const IRPosition &IRP = getIRPosition();
7732 
7733     // Check if we would improve the existing attributes first.
7734     SmallVector<Attribute, 4> DeducedAttrs;
7735     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
7736     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
7737           return IRP.hasAttr(Attr.getKindAsEnum(),
7738                              /* IgnoreSubsumingPositions */ true);
7739         }))
7740       return ChangeStatus::UNCHANGED;
7741 
7742     // Clear existing attributes.
7743     IRP.removeAttrs(AttrKinds);
7744     if (isAssumedReadNone())
7745       IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds);
7746 
7747     // Use the generic manifest method.
7748     return IRAttribute::manifest(A);
7749   }
7750 
7751   /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...).
7752   bool checkForAllAccessesToMemoryKind(
7753       function_ref<bool(const Instruction *, const Value *, AccessKind,
7754                         MemoryLocationsKind)>
7755           Pred,
7756       MemoryLocationsKind RequestedMLK) const override {
7757     if (!isValidState())
7758       return false;
7759 
7760     MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation();
7761     if (AssumedMLK == NO_LOCATIONS)
7762       return true;
7763 
7764     unsigned Idx = 0;
7765     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS;
7766          CurMLK *= 2, ++Idx) {
7767       if (CurMLK & RequestedMLK)
7768         continue;
7769 
7770       if (const AccessSet *Accesses = AccessKind2Accesses[Idx])
7771         for (const AccessInfo &AI : *Accesses)
7772           if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK))
7773             return false;
7774     }
7775 
7776     return true;
7777   }
7778 
7779   ChangeStatus indicatePessimisticFixpoint() override {
7780     // If we give up and indicate a pessimistic fixpoint this instruction will
7781     // become an access for all potential access kinds:
7782     // TODO: Add pointers for argmemonly and globals to improve the results of
7783     //       checkForAllAccessesToMemoryKind.
7784     bool Changed = false;
7785     MemoryLocationsKind KnownMLK = getKnown();
7786     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
7787     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
7788       if (!(CurMLK & KnownMLK))
7789         updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed,
7790                                   getAccessKindFromInst(I));
7791     return AAMemoryLocation::indicatePessimisticFixpoint();
7792   }
7793 
7794 protected:
7795   /// Helper struct to tie together an instruction that has a read or write
7796   /// effect with the pointer it accesses (if any).
7797   struct AccessInfo {
7798 
7799     /// The instruction that caused the access.
7800     const Instruction *I;
7801 
7802     /// The base pointer that is accessed, or null if unknown.
7803     const Value *Ptr;
7804 
7805     /// The kind of access (read/write/read+write).
7806     AccessKind Kind;
7807 
7808     bool operator==(const AccessInfo &RHS) const {
7809       return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind;
7810     }
7811     bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const {
7812       if (LHS.I != RHS.I)
7813         return LHS.I < RHS.I;
7814       if (LHS.Ptr != RHS.Ptr)
7815         return LHS.Ptr < RHS.Ptr;
7816       if (LHS.Kind != RHS.Kind)
7817         return LHS.Kind < RHS.Kind;
7818       return false;
7819     }
7820   };
7821 
7822   /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the
7823   /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind.
7824   using AccessSet = SmallSet<AccessInfo, 2, AccessInfo>;
7825   AccessSet *AccessKind2Accesses[llvm::CTLog2<VALID_STATE>()];
7826 
7827   /// Categorize the pointer arguments of CB that might access memory in
7828   /// AccessedLoc and update the state and access map accordingly.
7829   void
7830   categorizeArgumentPointerLocations(Attributor &A, CallBase &CB,
7831                                      AAMemoryLocation::StateType &AccessedLocs,
7832                                      bool &Changed);
7833 
7834   /// Return the kind(s) of location that may be accessed by \p V.
7835   AAMemoryLocation::MemoryLocationsKind
7836   categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
7837 
7838   /// Return the access kind as determined by \p I.
7839   AccessKind getAccessKindFromInst(const Instruction *I) {
7840     AccessKind AK = READ_WRITE;
7841     if (I) {
7842       AK = I->mayReadFromMemory() ? READ : NONE;
7843       AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE));
7844     }
7845     return AK;
7846   }
7847 
7848   /// Update the state \p State and the AccessKind2Accesses given that \p I is
7849   /// an access of kind \p AK to a \p MLK memory location with the access
7850   /// pointer \p Ptr.
7851   void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
7852                                  MemoryLocationsKind MLK, const Instruction *I,
7853                                  const Value *Ptr, bool &Changed,
7854                                  AccessKind AK = READ_WRITE) {
7855 
7856     assert(isPowerOf2_32(MLK) && "Expected a single location set!");
7857     auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)];
7858     if (!Accesses)
7859       Accesses = new (Allocator) AccessSet();
7860     Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second;
7861     State.removeAssumedBits(MLK);
7862   }
7863 
7864   /// Determine the underlying locations kinds for \p Ptr, e.g., globals or
7865   /// arguments, and update the state and access map accordingly.
7866   void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr,
7867                           AAMemoryLocation::StateType &State, bool &Changed);
7868 
7869   /// Used to allocate access sets.
7870   BumpPtrAllocator &Allocator;
7871 
7872   /// The set of IR attributes AAMemoryLocation deals with.
7873   static const Attribute::AttrKind AttrKinds[4];
7874 };
7875 
7876 const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = {
7877     Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly,
7878     Attribute::InaccessibleMemOrArgMemOnly};
7879 
7880 void AAMemoryLocationImpl::categorizePtrValue(
7881     Attributor &A, const Instruction &I, const Value &Ptr,
7882     AAMemoryLocation::StateType &State, bool &Changed) {
7883   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "
7884                     << Ptr << " ["
7885                     << getMemoryLocationsAsStr(State.getAssumed()) << "]\n");
7886 
7887   SmallVector<Value *, 8> Objects;
7888   bool UsedAssumedInformation = false;
7889   if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, *this, &I,
7890                                        UsedAssumedInformation,
7891                                        /* Intraprocedural */ true)) {
7892     LLVM_DEBUG(
7893         dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n");
7894     updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed,
7895                               getAccessKindFromInst(&I));
7896     return;
7897   }
7898 
7899   for (Value *Obj : Objects) {
7900     // TODO: recognize the TBAA used for constant accesses.
7901     MemoryLocationsKind MLK = NO_LOCATIONS;
7902     if (isa<UndefValue>(Obj))
7903       continue;
7904     if (isa<Argument>(Obj)) {
7905       // TODO: For now we do not treat byval arguments as local copies performed
7906       // on the call edge, though, we should. To make that happen we need to
7907       // teach various passes, e.g., DSE, about the copy effect of a byval. That
7908       // would also allow us to mark functions only accessing byval arguments as
7909       // readnone again, atguably their acceses have no effect outside of the
7910       // function, like accesses to allocas.
7911       MLK = NO_ARGUMENT_MEM;
7912     } else if (auto *GV = dyn_cast<GlobalValue>(Obj)) {
7913       // Reading constant memory is not treated as a read "effect" by the
7914       // function attr pass so we won't neither. Constants defined by TBAA are
7915       // similar. (We know we do not write it because it is constant.)
7916       if (auto *GVar = dyn_cast<GlobalVariable>(GV))
7917         if (GVar->isConstant())
7918           continue;
7919 
7920       if (GV->hasLocalLinkage())
7921         MLK = NO_GLOBAL_INTERNAL_MEM;
7922       else
7923         MLK = NO_GLOBAL_EXTERNAL_MEM;
7924     } else if (isa<ConstantPointerNull>(Obj) &&
7925                !NullPointerIsDefined(getAssociatedFunction(),
7926                                      Ptr.getType()->getPointerAddressSpace())) {
7927       continue;
7928     } else if (isa<AllocaInst>(Obj)) {
7929       MLK = NO_LOCAL_MEM;
7930     } else if (const auto *CB = dyn_cast<CallBase>(Obj)) {
7931       const auto &NoAliasAA = A.getAAFor<AANoAlias>(
7932           *this, IRPosition::callsite_returned(*CB), DepClassTy::OPTIONAL);
7933       if (NoAliasAA.isAssumedNoAlias())
7934         MLK = NO_MALLOCED_MEM;
7935       else
7936         MLK = NO_UNKOWN_MEM;
7937     } else {
7938       MLK = NO_UNKOWN_MEM;
7939     }
7940 
7941     assert(MLK != NO_LOCATIONS && "No location specified!");
7942     LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value can be categorized: "
7943                       << *Obj << " -> " << getMemoryLocationsAsStr(MLK)
7944                       << "\n");
7945     updateStateAndAccessesMap(getState(), MLK, &I, Obj, Changed,
7946                               getAccessKindFromInst(&I));
7947   }
7948 
7949   LLVM_DEBUG(
7950       dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: "
7951              << getMemoryLocationsAsStr(State.getAssumed()) << "\n");
7952 }
7953 
7954 void AAMemoryLocationImpl::categorizeArgumentPointerLocations(
7955     Attributor &A, CallBase &CB, AAMemoryLocation::StateType &AccessedLocs,
7956     bool &Changed) {
7957   for (unsigned ArgNo = 0, E = CB.arg_size(); ArgNo < E; ++ArgNo) {
7958 
7959     // Skip non-pointer arguments.
7960     const Value *ArgOp = CB.getArgOperand(ArgNo);
7961     if (!ArgOp->getType()->isPtrOrPtrVectorTy())
7962       continue;
7963 
7964     // Skip readnone arguments.
7965     const IRPosition &ArgOpIRP = IRPosition::callsite_argument(CB, ArgNo);
7966     const auto &ArgOpMemLocationAA =
7967         A.getAAFor<AAMemoryBehavior>(*this, ArgOpIRP, DepClassTy::OPTIONAL);
7968 
7969     if (ArgOpMemLocationAA.isAssumedReadNone())
7970       continue;
7971 
7972     // Categorize potentially accessed pointer arguments as if there was an
7973     // access instruction with them as pointer.
7974     categorizePtrValue(A, CB, *ArgOp, AccessedLocs, Changed);
7975   }
7976 }
7977 
7978 AAMemoryLocation::MemoryLocationsKind
7979 AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
7980                                                   bool &Changed) {
7981   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "
7982                     << I << "\n");
7983 
7984   AAMemoryLocation::StateType AccessedLocs;
7985   AccessedLocs.intersectAssumedBits(NO_LOCATIONS);
7986 
7987   if (auto *CB = dyn_cast<CallBase>(&I)) {
7988 
7989     // First check if we assume any memory is access is visible.
7990     const auto &CBMemLocationAA = A.getAAFor<AAMemoryLocation>(
7991         *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
7992     LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I
7993                       << " [" << CBMemLocationAA << "]\n");
7994 
7995     if (CBMemLocationAA.isAssumedReadNone())
7996       return NO_LOCATIONS;
7997 
7998     if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) {
7999       updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr,
8000                                 Changed, getAccessKindFromInst(&I));
8001       return AccessedLocs.getAssumed();
8002     }
8003 
8004     uint32_t CBAssumedNotAccessedLocs =
8005         CBMemLocationAA.getAssumedNotAccessedLocation();
8006 
8007     // Set the argmemonly and global bit as we handle them separately below.
8008     uint32_t CBAssumedNotAccessedLocsNoArgMem =
8009         CBAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM;
8010 
8011     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
8012       if (CBAssumedNotAccessedLocsNoArgMem & CurMLK)
8013         continue;
8014       updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed,
8015                                 getAccessKindFromInst(&I));
8016     }
8017 
8018     // Now handle global memory if it might be accessed. This is slightly tricky
8019     // as NO_GLOBAL_MEM has multiple bits set.
8020     bool HasGlobalAccesses = ((~CBAssumedNotAccessedLocs) & NO_GLOBAL_MEM);
8021     if (HasGlobalAccesses) {
8022       auto AccessPred = [&](const Instruction *, const Value *Ptr,
8023                             AccessKind Kind, MemoryLocationsKind MLK) {
8024         updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed,
8025                                   getAccessKindFromInst(&I));
8026         return true;
8027       };
8028       if (!CBMemLocationAA.checkForAllAccessesToMemoryKind(
8029               AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false)))
8030         return AccessedLocs.getWorstState();
8031     }
8032 
8033     LLVM_DEBUG(
8034         dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "
8035                << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
8036 
8037     // Now handle argument memory if it might be accessed.
8038     bool HasArgAccesses = ((~CBAssumedNotAccessedLocs) & NO_ARGUMENT_MEM);
8039     if (HasArgAccesses)
8040       categorizeArgumentPointerLocations(A, *CB, AccessedLocs, Changed);
8041 
8042     LLVM_DEBUG(
8043         dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "
8044                << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
8045 
8046     return AccessedLocs.getAssumed();
8047   }
8048 
8049   if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) {
8050     LLVM_DEBUG(
8051         dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "
8052                << I << " [" << *Ptr << "]\n");
8053     categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed);
8054     return AccessedLocs.getAssumed();
8055   }
8056 
8057   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "
8058                     << I << "\n");
8059   updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed,
8060                             getAccessKindFromInst(&I));
8061   return AccessedLocs.getAssumed();
8062 }
8063 
8064 /// An AA to represent the memory behavior function attributes.
8065 struct AAMemoryLocationFunction final : public AAMemoryLocationImpl {
8066   AAMemoryLocationFunction(const IRPosition &IRP, Attributor &A)
8067       : AAMemoryLocationImpl(IRP, A) {}
8068 
8069   /// See AbstractAttribute::updateImpl(Attributor &A).
8070   virtual ChangeStatus updateImpl(Attributor &A) override {
8071 
8072     const auto &MemBehaviorAA =
8073         A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
8074     if (MemBehaviorAA.isAssumedReadNone()) {
8075       if (MemBehaviorAA.isKnownReadNone())
8076         return indicateOptimisticFixpoint();
8077       assert(isAssumedReadNone() &&
8078              "AAMemoryLocation was not read-none but AAMemoryBehavior was!");
8079       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
8080       return ChangeStatus::UNCHANGED;
8081     }
8082 
8083     // The current assumed state used to determine a change.
8084     auto AssumedState = getAssumed();
8085     bool Changed = false;
8086 
8087     auto CheckRWInst = [&](Instruction &I) {
8088       MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed);
8089       LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I
8090                         << ": " << getMemoryLocationsAsStr(MLK) << "\n");
8091       removeAssumedBits(inverseLocation(MLK, false, false));
8092       // Stop once only the valid bit set in the *not assumed location*, thus
8093       // once we don't actually exclude any memory locations in the state.
8094       return getAssumedNotAccessedLocation() != VALID_STATE;
8095     };
8096 
8097     bool UsedAssumedInformation = false;
8098     if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
8099                                             UsedAssumedInformation))
8100       return indicatePessimisticFixpoint();
8101 
8102     Changed |= AssumedState != getAssumed();
8103     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
8104   }
8105 
8106   /// See AbstractAttribute::trackStatistics()
8107   void trackStatistics() const override {
8108     if (isAssumedReadNone())
8109       STATS_DECLTRACK_FN_ATTR(readnone)
8110     else if (isAssumedArgMemOnly())
8111       STATS_DECLTRACK_FN_ATTR(argmemonly)
8112     else if (isAssumedInaccessibleMemOnly())
8113       STATS_DECLTRACK_FN_ATTR(inaccessiblememonly)
8114     else if (isAssumedInaccessibleOrArgMemOnly())
8115       STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly)
8116   }
8117 };
8118 
8119 /// AAMemoryLocation attribute for call sites.
8120 struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
8121   AAMemoryLocationCallSite(const IRPosition &IRP, Attributor &A)
8122       : AAMemoryLocationImpl(IRP, A) {}
8123 
8124   /// See AbstractAttribute::initialize(...).
8125   void initialize(Attributor &A) override {
8126     AAMemoryLocationImpl::initialize(A);
8127     Function *F = getAssociatedFunction();
8128     if (!F || F->isDeclaration())
8129       indicatePessimisticFixpoint();
8130   }
8131 
8132   /// See AbstractAttribute::updateImpl(...).
8133   ChangeStatus updateImpl(Attributor &A) override {
8134     // TODO: Once we have call site specific value information we can provide
8135     //       call site specific liveness liveness information and then it makes
8136     //       sense to specialize attributes for call sites arguments instead of
8137     //       redirecting requests to the callee argument.
8138     Function *F = getAssociatedFunction();
8139     const IRPosition &FnPos = IRPosition::function(*F);
8140     auto &FnAA =
8141         A.getAAFor<AAMemoryLocation>(*this, FnPos, DepClassTy::REQUIRED);
8142     bool Changed = false;
8143     auto AccessPred = [&](const Instruction *I, const Value *Ptr,
8144                           AccessKind Kind, MemoryLocationsKind MLK) {
8145       updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed,
8146                                 getAccessKindFromInst(I));
8147       return true;
8148     };
8149     if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))
8150       return indicatePessimisticFixpoint();
8151     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
8152   }
8153 
8154   /// See AbstractAttribute::trackStatistics()
8155   void trackStatistics() const override {
8156     if (isAssumedReadNone())
8157       STATS_DECLTRACK_CS_ATTR(readnone)
8158   }
8159 };
8160 } // namespace
8161 
8162 /// ------------------ Value Constant Range Attribute -------------------------
8163 
8164 namespace {
8165 struct AAValueConstantRangeImpl : AAValueConstantRange {
8166   using StateType = IntegerRangeState;
8167   AAValueConstantRangeImpl(const IRPosition &IRP, Attributor &A)
8168       : AAValueConstantRange(IRP, A) {}
8169 
8170   /// See AbstractAttribute::initialize(..).
8171   void initialize(Attributor &A) override {
8172     if (A.hasSimplificationCallback(getIRPosition())) {
8173       indicatePessimisticFixpoint();
8174       return;
8175     }
8176 
8177     // Intersect a range given by SCEV.
8178     intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
8179 
8180     // Intersect a range given by LVI.
8181     intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
8182   }
8183 
8184   /// See AbstractAttribute::getAsStr().
8185   const std::string getAsStr() const override {
8186     std::string Str;
8187     llvm::raw_string_ostream OS(Str);
8188     OS << "range(" << getBitWidth() << ")<";
8189     getKnown().print(OS);
8190     OS << " / ";
8191     getAssumed().print(OS);
8192     OS << ">";
8193     return OS.str();
8194   }
8195 
8196   /// Helper function to get a SCEV expr for the associated value at program
8197   /// point \p I.
8198   const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
8199     if (!getAnchorScope())
8200       return nullptr;
8201 
8202     ScalarEvolution *SE =
8203         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
8204             *getAnchorScope());
8205 
8206     LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
8207         *getAnchorScope());
8208 
8209     if (!SE || !LI)
8210       return nullptr;
8211 
8212     const SCEV *S = SE->getSCEV(&getAssociatedValue());
8213     if (!I)
8214       return S;
8215 
8216     return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
8217   }
8218 
8219   /// Helper function to get a range from SCEV for the associated value at
8220   /// program point \p I.
8221   ConstantRange getConstantRangeFromSCEV(Attributor &A,
8222                                          const Instruction *I = nullptr) const {
8223     if (!getAnchorScope())
8224       return getWorstState(getBitWidth());
8225 
8226     ScalarEvolution *SE =
8227         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
8228             *getAnchorScope());
8229 
8230     const SCEV *S = getSCEV(A, I);
8231     if (!SE || !S)
8232       return getWorstState(getBitWidth());
8233 
8234     return SE->getUnsignedRange(S);
8235   }
8236 
8237   /// Helper function to get a range from LVI for the associated value at
8238   /// program point \p I.
8239   ConstantRange
8240   getConstantRangeFromLVI(Attributor &A,
8241                           const Instruction *CtxI = nullptr) const {
8242     if (!getAnchorScope())
8243       return getWorstState(getBitWidth());
8244 
8245     LazyValueInfo *LVI =
8246         A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
8247             *getAnchorScope());
8248 
8249     if (!LVI || !CtxI)
8250       return getWorstState(getBitWidth());
8251     return LVI->getConstantRange(&getAssociatedValue(),
8252                                  const_cast<Instruction *>(CtxI));
8253   }
8254 
8255   /// Return true if \p CtxI is valid for querying outside analyses.
8256   /// This basically makes sure we do not ask intra-procedural analysis
8257   /// about a context in the wrong function or a context that violates
8258   /// dominance assumptions they might have. The \p AllowAACtxI flag indicates
8259   /// if the original context of this AA is OK or should be considered invalid.
8260   bool isValidCtxInstructionForOutsideAnalysis(Attributor &A,
8261                                                const Instruction *CtxI,
8262                                                bool AllowAACtxI) const {
8263     if (!CtxI || (!AllowAACtxI && CtxI == getCtxI()))
8264       return false;
8265 
8266     // Our context might be in a different function, neither intra-procedural
8267     // analysis (ScalarEvolution nor LazyValueInfo) can handle that.
8268     if (!AA::isValidInScope(getAssociatedValue(), CtxI->getFunction()))
8269       return false;
8270 
8271     // If the context is not dominated by the value there are paths to the
8272     // context that do not define the value. This cannot be handled by
8273     // LazyValueInfo so we need to bail.
8274     if (auto *I = dyn_cast<Instruction>(&getAssociatedValue())) {
8275       InformationCache &InfoCache = A.getInfoCache();
8276       const DominatorTree *DT =
8277           InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
8278               *I->getFunction());
8279       return DT && DT->dominates(I, CtxI);
8280     }
8281 
8282     return true;
8283   }
8284 
8285   /// See AAValueConstantRange::getKnownConstantRange(..).
8286   ConstantRange
8287   getKnownConstantRange(Attributor &A,
8288                         const Instruction *CtxI = nullptr) const override {
8289     if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
8290                                                  /* AllowAACtxI */ false))
8291       return getKnown();
8292 
8293     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
8294     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
8295     return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
8296   }
8297 
8298   /// See AAValueConstantRange::getAssumedConstantRange(..).
8299   ConstantRange
8300   getAssumedConstantRange(Attributor &A,
8301                           const Instruction *CtxI = nullptr) const override {
8302     // TODO: Make SCEV use Attributor assumption.
8303     //       We may be able to bound a variable range via assumptions in
8304     //       Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
8305     //       evolve to x^2 + x, then we can say that y is in [2, 12].
8306     if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
8307                                                  /* AllowAACtxI */ false))
8308       return getAssumed();
8309 
8310     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
8311     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
8312     return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
8313   }
8314 
8315   /// Helper function to create MDNode for range metadata.
8316   static MDNode *
8317   getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
8318                             const ConstantRange &AssumedConstantRange) {
8319     Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
8320                                   Ty, AssumedConstantRange.getLower())),
8321                               ConstantAsMetadata::get(ConstantInt::get(
8322                                   Ty, AssumedConstantRange.getUpper()))};
8323     return MDNode::get(Ctx, LowAndHigh);
8324   }
8325 
8326   /// Return true if \p Assumed is included in \p KnownRanges.
8327   static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
8328 
8329     if (Assumed.isFullSet())
8330       return false;
8331 
8332     if (!KnownRanges)
8333       return true;
8334 
8335     // If multiple ranges are annotated in IR, we give up to annotate assumed
8336     // range for now.
8337 
8338     // TODO:  If there exists a known range which containts assumed range, we
8339     // can say assumed range is better.
8340     if (KnownRanges->getNumOperands() > 2)
8341       return false;
8342 
8343     ConstantInt *Lower =
8344         mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
8345     ConstantInt *Upper =
8346         mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
8347 
8348     ConstantRange Known(Lower->getValue(), Upper->getValue());
8349     return Known.contains(Assumed) && Known != Assumed;
8350   }
8351 
8352   /// Helper function to set range metadata.
8353   static bool
8354   setRangeMetadataIfisBetterRange(Instruction *I,
8355                                   const ConstantRange &AssumedConstantRange) {
8356     auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
8357     if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
8358       if (!AssumedConstantRange.isEmptySet()) {
8359         I->setMetadata(LLVMContext::MD_range,
8360                        getMDNodeForConstantRange(I->getType(), I->getContext(),
8361                                                  AssumedConstantRange));
8362         return true;
8363       }
8364     }
8365     return false;
8366   }
8367 
8368   /// See AbstractAttribute::manifest()
8369   ChangeStatus manifest(Attributor &A) override {
8370     ChangeStatus Changed = ChangeStatus::UNCHANGED;
8371     ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
8372     assert(!AssumedConstantRange.isFullSet() && "Invalid state");
8373 
8374     auto &V = getAssociatedValue();
8375     if (!AssumedConstantRange.isEmptySet() &&
8376         !AssumedConstantRange.isSingleElement()) {
8377       if (Instruction *I = dyn_cast<Instruction>(&V)) {
8378         assert(I == getCtxI() && "Should not annotate an instruction which is "
8379                                  "not the context instruction");
8380         if (isa<CallInst>(I) || isa<LoadInst>(I))
8381           if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
8382             Changed = ChangeStatus::CHANGED;
8383       }
8384     }
8385 
8386     return Changed;
8387   }
8388 };
8389 
8390 struct AAValueConstantRangeArgument final
8391     : AAArgumentFromCallSiteArguments<
8392           AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
8393           true /* BridgeCallBaseContext */> {
8394   using Base = AAArgumentFromCallSiteArguments<
8395       AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
8396       true /* BridgeCallBaseContext */>;
8397   AAValueConstantRangeArgument(const IRPosition &IRP, Attributor &A)
8398       : Base(IRP, A) {}
8399 
8400   /// See AbstractAttribute::initialize(..).
8401   void initialize(Attributor &A) override {
8402     if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
8403       indicatePessimisticFixpoint();
8404     } else {
8405       Base::initialize(A);
8406     }
8407   }
8408 
8409   /// See AbstractAttribute::trackStatistics()
8410   void trackStatistics() const override {
8411     STATS_DECLTRACK_ARG_ATTR(value_range)
8412   }
8413 };
8414 
8415 struct AAValueConstantRangeReturned
8416     : AAReturnedFromReturnedValues<AAValueConstantRange,
8417                                    AAValueConstantRangeImpl,
8418                                    AAValueConstantRangeImpl::StateType,
8419                                    /* PropogateCallBaseContext */ true> {
8420   using Base =
8421       AAReturnedFromReturnedValues<AAValueConstantRange,
8422                                    AAValueConstantRangeImpl,
8423                                    AAValueConstantRangeImpl::StateType,
8424                                    /* PropogateCallBaseContext */ true>;
8425   AAValueConstantRangeReturned(const IRPosition &IRP, Attributor &A)
8426       : Base(IRP, A) {}
8427 
8428   /// See AbstractAttribute::initialize(...).
8429   void initialize(Attributor &A) override {}
8430 
8431   /// See AbstractAttribute::trackStatistics()
8432   void trackStatistics() const override {
8433     STATS_DECLTRACK_FNRET_ATTR(value_range)
8434   }
8435 };
8436 
8437 struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
8438   AAValueConstantRangeFloating(const IRPosition &IRP, Attributor &A)
8439       : AAValueConstantRangeImpl(IRP, A) {}
8440 
8441   /// See AbstractAttribute::initialize(...).
8442   void initialize(Attributor &A) override {
8443     AAValueConstantRangeImpl::initialize(A);
8444     if (isAtFixpoint())
8445       return;
8446 
8447     Value &V = getAssociatedValue();
8448 
8449     if (auto *C = dyn_cast<ConstantInt>(&V)) {
8450       unionAssumed(ConstantRange(C->getValue()));
8451       indicateOptimisticFixpoint();
8452       return;
8453     }
8454 
8455     if (isa<UndefValue>(&V)) {
8456       // Collapse the undef state to 0.
8457       unionAssumed(ConstantRange(APInt(getBitWidth(), 0)));
8458       indicateOptimisticFixpoint();
8459       return;
8460     }
8461 
8462     if (isa<CallBase>(&V))
8463       return;
8464 
8465     if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V))
8466       return;
8467 
8468     // If it is a load instruction with range metadata, use it.
8469     if (LoadInst *LI = dyn_cast<LoadInst>(&V))
8470       if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
8471         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
8472         return;
8473       }
8474 
8475     // We can work with PHI and select instruction as we traverse their operands
8476     // during update.
8477     if (isa<SelectInst>(V) || isa<PHINode>(V))
8478       return;
8479 
8480     // Otherwise we give up.
8481     indicatePessimisticFixpoint();
8482 
8483     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
8484                       << getAssociatedValue() << "\n");
8485   }
8486 
8487   bool calculateBinaryOperator(
8488       Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
8489       const Instruction *CtxI,
8490       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8491     Value *LHS = BinOp->getOperand(0);
8492     Value *RHS = BinOp->getOperand(1);
8493 
8494     // Simplify the operands first.
8495     bool UsedAssumedInformation = false;
8496     const auto &SimplifiedLHS =
8497         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8498                                *this, UsedAssumedInformation);
8499     if (!SimplifiedLHS.hasValue())
8500       return true;
8501     if (!SimplifiedLHS.getValue())
8502       return false;
8503     LHS = *SimplifiedLHS;
8504 
8505     const auto &SimplifiedRHS =
8506         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8507                                *this, UsedAssumedInformation);
8508     if (!SimplifiedRHS.hasValue())
8509       return true;
8510     if (!SimplifiedRHS.getValue())
8511       return false;
8512     RHS = *SimplifiedRHS;
8513 
8514     // TODO: Allow non integers as well.
8515     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8516       return false;
8517 
8518     auto &LHSAA = A.getAAFor<AAValueConstantRange>(
8519         *this, IRPosition::value(*LHS, getCallBaseContext()),
8520         DepClassTy::REQUIRED);
8521     QuerriedAAs.push_back(&LHSAA);
8522     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
8523 
8524     auto &RHSAA = A.getAAFor<AAValueConstantRange>(
8525         *this, IRPosition::value(*RHS, getCallBaseContext()),
8526         DepClassTy::REQUIRED);
8527     QuerriedAAs.push_back(&RHSAA);
8528     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
8529 
8530     auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
8531 
8532     T.unionAssumed(AssumedRange);
8533 
8534     // TODO: Track a known state too.
8535 
8536     return T.isValidState();
8537   }
8538 
8539   bool calculateCastInst(
8540       Attributor &A, CastInst *CastI, IntegerRangeState &T,
8541       const Instruction *CtxI,
8542       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8543     assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
8544     // TODO: Allow non integers as well.
8545     Value *OpV = CastI->getOperand(0);
8546 
8547     // Simplify the operand first.
8548     bool UsedAssumedInformation = false;
8549     const auto &SimplifiedOpV =
8550         A.getAssumedSimplified(IRPosition::value(*OpV, getCallBaseContext()),
8551                                *this, UsedAssumedInformation);
8552     if (!SimplifiedOpV.hasValue())
8553       return true;
8554     if (!SimplifiedOpV.getValue())
8555       return false;
8556     OpV = *SimplifiedOpV;
8557 
8558     if (!OpV->getType()->isIntegerTy())
8559       return false;
8560 
8561     auto &OpAA = A.getAAFor<AAValueConstantRange>(
8562         *this, IRPosition::value(*OpV, getCallBaseContext()),
8563         DepClassTy::REQUIRED);
8564     QuerriedAAs.push_back(&OpAA);
8565     T.unionAssumed(
8566         OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
8567     return T.isValidState();
8568   }
8569 
8570   bool
8571   calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
8572                    const Instruction *CtxI,
8573                    SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8574     Value *LHS = CmpI->getOperand(0);
8575     Value *RHS = CmpI->getOperand(1);
8576 
8577     // Simplify the operands first.
8578     bool UsedAssumedInformation = false;
8579     const auto &SimplifiedLHS =
8580         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8581                                *this, UsedAssumedInformation);
8582     if (!SimplifiedLHS.hasValue())
8583       return true;
8584     if (!SimplifiedLHS.getValue())
8585       return false;
8586     LHS = *SimplifiedLHS;
8587 
8588     const auto &SimplifiedRHS =
8589         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8590                                *this, UsedAssumedInformation);
8591     if (!SimplifiedRHS.hasValue())
8592       return true;
8593     if (!SimplifiedRHS.getValue())
8594       return false;
8595     RHS = *SimplifiedRHS;
8596 
8597     // TODO: Allow non integers as well.
8598     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8599       return false;
8600 
8601     auto &LHSAA = A.getAAFor<AAValueConstantRange>(
8602         *this, IRPosition::value(*LHS, getCallBaseContext()),
8603         DepClassTy::REQUIRED);
8604     QuerriedAAs.push_back(&LHSAA);
8605     auto &RHSAA = A.getAAFor<AAValueConstantRange>(
8606         *this, IRPosition::value(*RHS, getCallBaseContext()),
8607         DepClassTy::REQUIRED);
8608     QuerriedAAs.push_back(&RHSAA);
8609     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
8610     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
8611 
8612     // If one of them is empty set, we can't decide.
8613     if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
8614       return true;
8615 
8616     bool MustTrue = false, MustFalse = false;
8617 
8618     auto AllowedRegion =
8619         ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
8620 
8621     if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
8622       MustFalse = true;
8623 
8624     if (LHSAARange.icmp(CmpI->getPredicate(), RHSAARange))
8625       MustTrue = true;
8626 
8627     assert((!MustTrue || !MustFalse) &&
8628            "Either MustTrue or MustFalse should be false!");
8629 
8630     if (MustTrue)
8631       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
8632     else if (MustFalse)
8633       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
8634     else
8635       T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
8636 
8637     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
8638                       << " " << RHSAA << "\n");
8639 
8640     // TODO: Track a known state too.
8641     return T.isValidState();
8642   }
8643 
8644   /// See AbstractAttribute::updateImpl(...).
8645   ChangeStatus updateImpl(Attributor &A) override {
8646     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
8647                             IntegerRangeState &T, bool Stripped) -> bool {
8648       Instruction *I = dyn_cast<Instruction>(&V);
8649       if (!I || isa<CallBase>(I)) {
8650 
8651         // Simplify the operand first.
8652         bool UsedAssumedInformation = false;
8653         const auto &SimplifiedOpV =
8654             A.getAssumedSimplified(IRPosition::value(V, getCallBaseContext()),
8655                                    *this, UsedAssumedInformation);
8656         if (!SimplifiedOpV.hasValue())
8657           return true;
8658         if (!SimplifiedOpV.getValue())
8659           return false;
8660         Value *VPtr = *SimplifiedOpV;
8661 
8662         // If the value is not instruction, we query AA to Attributor.
8663         const auto &AA = A.getAAFor<AAValueConstantRange>(
8664             *this, IRPosition::value(*VPtr, getCallBaseContext()),
8665             DepClassTy::REQUIRED);
8666 
8667         // Clamp operator is not used to utilize a program point CtxI.
8668         T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
8669 
8670         return T.isValidState();
8671       }
8672 
8673       SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
8674       if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
8675         if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
8676           return false;
8677       } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
8678         if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
8679           return false;
8680       } else if (auto *CastI = dyn_cast<CastInst>(I)) {
8681         if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
8682           return false;
8683       } else {
8684         // Give up with other instructions.
8685         // TODO: Add other instructions
8686 
8687         T.indicatePessimisticFixpoint();
8688         return false;
8689       }
8690 
8691       // Catch circular reasoning in a pessimistic way for now.
8692       // TODO: Check how the range evolves and if we stripped anything, see also
8693       //       AADereferenceable or AAAlign for similar situations.
8694       for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
8695         if (QueriedAA != this)
8696           continue;
8697         // If we are in a stady state we do not need to worry.
8698         if (T.getAssumed() == getState().getAssumed())
8699           continue;
8700         T.indicatePessimisticFixpoint();
8701       }
8702 
8703       return T.isValidState();
8704     };
8705 
8706     IntegerRangeState T(getBitWidth());
8707 
8708     bool UsedAssumedInformation = false;
8709     if (!genericValueTraversal<IntegerRangeState>(A, getIRPosition(), *this, T,
8710                                                   VisitValueCB, getCtxI(),
8711                                                   UsedAssumedInformation,
8712                                                   /* UseValueSimplify */ false))
8713       return indicatePessimisticFixpoint();
8714 
8715     // Ensure that long def-use chains can't cause circular reasoning either by
8716     // introducing a cutoff below.
8717     if (clampStateAndIndicateChange(getState(), T) == ChangeStatus::UNCHANGED)
8718       return ChangeStatus::UNCHANGED;
8719     if (++NumChanges > MaxNumChanges) {
8720       LLVM_DEBUG(dbgs() << "[AAValueConstantRange] performed " << NumChanges
8721                         << " but only " << MaxNumChanges
8722                         << " are allowed to avoid cyclic reasoning.");
8723       return indicatePessimisticFixpoint();
8724     }
8725     return ChangeStatus::CHANGED;
8726   }
8727 
8728   /// See AbstractAttribute::trackStatistics()
8729   void trackStatistics() const override {
8730     STATS_DECLTRACK_FLOATING_ATTR(value_range)
8731   }
8732 
8733   /// Tracker to bail after too many widening steps of the constant range.
8734   int NumChanges = 0;
8735 
8736   /// Upper bound for the number of allowed changes (=widening steps) for the
8737   /// constant range before we give up.
8738   static constexpr int MaxNumChanges = 5;
8739 };
8740 
8741 struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
8742   AAValueConstantRangeFunction(const IRPosition &IRP, Attributor &A)
8743       : AAValueConstantRangeImpl(IRP, A) {}
8744 
8745   /// See AbstractAttribute::initialize(...).
8746   ChangeStatus updateImpl(Attributor &A) override {
8747     llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
8748                      "not be called");
8749   }
8750 
8751   /// See AbstractAttribute::trackStatistics()
8752   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
8753 };
8754 
8755 struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
8756   AAValueConstantRangeCallSite(const IRPosition &IRP, Attributor &A)
8757       : AAValueConstantRangeFunction(IRP, A) {}
8758 
8759   /// See AbstractAttribute::trackStatistics()
8760   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
8761 };
8762 
8763 struct AAValueConstantRangeCallSiteReturned
8764     : AACallSiteReturnedFromReturned<AAValueConstantRange,
8765                                      AAValueConstantRangeImpl,
8766                                      AAValueConstantRangeImpl::StateType,
8767                                      /* IntroduceCallBaseContext */ true> {
8768   AAValueConstantRangeCallSiteReturned(const IRPosition &IRP, Attributor &A)
8769       : AACallSiteReturnedFromReturned<AAValueConstantRange,
8770                                        AAValueConstantRangeImpl,
8771                                        AAValueConstantRangeImpl::StateType,
8772                                        /* IntroduceCallBaseContext */ true>(IRP,
8773                                                                             A) {
8774   }
8775 
8776   /// See AbstractAttribute::initialize(...).
8777   void initialize(Attributor &A) override {
8778     // If it is a load instruction with range metadata, use the metadata.
8779     if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
8780       if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
8781         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
8782 
8783     AAValueConstantRangeImpl::initialize(A);
8784   }
8785 
8786   /// See AbstractAttribute::trackStatistics()
8787   void trackStatistics() const override {
8788     STATS_DECLTRACK_CSRET_ATTR(value_range)
8789   }
8790 };
8791 struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
8792   AAValueConstantRangeCallSiteArgument(const IRPosition &IRP, Attributor &A)
8793       : AAValueConstantRangeFloating(IRP, A) {}
8794 
8795   /// See AbstractAttribute::manifest()
8796   ChangeStatus manifest(Attributor &A) override {
8797     return ChangeStatus::UNCHANGED;
8798   }
8799 
8800   /// See AbstractAttribute::trackStatistics()
8801   void trackStatistics() const override {
8802     STATS_DECLTRACK_CSARG_ATTR(value_range)
8803   }
8804 };
8805 } // namespace
8806 
8807 /// ------------------ Potential Values Attribute -------------------------
8808 
8809 namespace {
8810 struct AAPotentialValuesImpl : AAPotentialValues {
8811   using StateType = PotentialConstantIntValuesState;
8812 
8813   AAPotentialValuesImpl(const IRPosition &IRP, Attributor &A)
8814       : AAPotentialValues(IRP, A) {}
8815 
8816   /// See AbstractAttribute::initialize(..).
8817   void initialize(Attributor &A) override {
8818     if (A.hasSimplificationCallback(getIRPosition()))
8819       indicatePessimisticFixpoint();
8820     else
8821       AAPotentialValues::initialize(A);
8822   }
8823 
8824   /// See AbstractAttribute::getAsStr().
8825   const std::string getAsStr() const override {
8826     std::string Str;
8827     llvm::raw_string_ostream OS(Str);
8828     OS << getState();
8829     return OS.str();
8830   }
8831 
8832   /// See AbstractAttribute::updateImpl(...).
8833   ChangeStatus updateImpl(Attributor &A) override {
8834     return indicatePessimisticFixpoint();
8835   }
8836 };
8837 
8838 struct AAPotentialValuesArgument final
8839     : AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
8840                                       PotentialConstantIntValuesState> {
8841   using Base =
8842       AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
8843                                       PotentialConstantIntValuesState>;
8844   AAPotentialValuesArgument(const IRPosition &IRP, Attributor &A)
8845       : Base(IRP, A) {}
8846 
8847   /// See AbstractAttribute::initialize(..).
8848   void initialize(Attributor &A) override {
8849     if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
8850       indicatePessimisticFixpoint();
8851     } else {
8852       Base::initialize(A);
8853     }
8854   }
8855 
8856   /// See AbstractAttribute::trackStatistics()
8857   void trackStatistics() const override {
8858     STATS_DECLTRACK_ARG_ATTR(potential_values)
8859   }
8860 };
8861 
8862 struct AAPotentialValuesReturned
8863     : AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl> {
8864   using Base =
8865       AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl>;
8866   AAPotentialValuesReturned(const IRPosition &IRP, Attributor &A)
8867       : Base(IRP, A) {}
8868 
8869   /// See AbstractAttribute::trackStatistics()
8870   void trackStatistics() const override {
8871     STATS_DECLTRACK_FNRET_ATTR(potential_values)
8872   }
8873 };
8874 
8875 struct AAPotentialValuesFloating : AAPotentialValuesImpl {
8876   AAPotentialValuesFloating(const IRPosition &IRP, Attributor &A)
8877       : AAPotentialValuesImpl(IRP, A) {}
8878 
8879   /// See AbstractAttribute::initialize(..).
8880   void initialize(Attributor &A) override {
8881     AAPotentialValuesImpl::initialize(A);
8882     if (isAtFixpoint())
8883       return;
8884 
8885     Value &V = getAssociatedValue();
8886 
8887     if (auto *C = dyn_cast<ConstantInt>(&V)) {
8888       unionAssumed(C->getValue());
8889       indicateOptimisticFixpoint();
8890       return;
8891     }
8892 
8893     if (isa<UndefValue>(&V)) {
8894       unionAssumedWithUndef();
8895       indicateOptimisticFixpoint();
8896       return;
8897     }
8898 
8899     if (isa<BinaryOperator>(&V) || isa<ICmpInst>(&V) || isa<CastInst>(&V))
8900       return;
8901 
8902     if (isa<SelectInst>(V) || isa<PHINode>(V) || isa<LoadInst>(V))
8903       return;
8904 
8905     indicatePessimisticFixpoint();
8906 
8907     LLVM_DEBUG(dbgs() << "[AAPotentialValues] We give up: "
8908                       << getAssociatedValue() << "\n");
8909   }
8910 
8911   static bool calculateICmpInst(const ICmpInst *ICI, const APInt &LHS,
8912                                 const APInt &RHS) {
8913     return ICmpInst::compare(LHS, RHS, ICI->getPredicate());
8914   }
8915 
8916   static APInt calculateCastInst(const CastInst *CI, const APInt &Src,
8917                                  uint32_t ResultBitWidth) {
8918     Instruction::CastOps CastOp = CI->getOpcode();
8919     switch (CastOp) {
8920     default:
8921       llvm_unreachable("unsupported or not integer cast");
8922     case Instruction::Trunc:
8923       return Src.trunc(ResultBitWidth);
8924     case Instruction::SExt:
8925       return Src.sext(ResultBitWidth);
8926     case Instruction::ZExt:
8927       return Src.zext(ResultBitWidth);
8928     case Instruction::BitCast:
8929       return Src;
8930     }
8931   }
8932 
8933   static APInt calculateBinaryOperator(const BinaryOperator *BinOp,
8934                                        const APInt &LHS, const APInt &RHS,
8935                                        bool &SkipOperation, bool &Unsupported) {
8936     Instruction::BinaryOps BinOpcode = BinOp->getOpcode();
8937     // Unsupported is set to true when the binary operator is not supported.
8938     // SkipOperation is set to true when UB occur with the given operand pair
8939     // (LHS, RHS).
8940     // TODO: we should look at nsw and nuw keywords to handle operations
8941     //       that create poison or undef value.
8942     switch (BinOpcode) {
8943     default:
8944       Unsupported = true;
8945       return LHS;
8946     case Instruction::Add:
8947       return LHS + RHS;
8948     case Instruction::Sub:
8949       return LHS - RHS;
8950     case Instruction::Mul:
8951       return LHS * RHS;
8952     case Instruction::UDiv:
8953       if (RHS.isZero()) {
8954         SkipOperation = true;
8955         return LHS;
8956       }
8957       return LHS.udiv(RHS);
8958     case Instruction::SDiv:
8959       if (RHS.isZero()) {
8960         SkipOperation = true;
8961         return LHS;
8962       }
8963       return LHS.sdiv(RHS);
8964     case Instruction::URem:
8965       if (RHS.isZero()) {
8966         SkipOperation = true;
8967         return LHS;
8968       }
8969       return LHS.urem(RHS);
8970     case Instruction::SRem:
8971       if (RHS.isZero()) {
8972         SkipOperation = true;
8973         return LHS;
8974       }
8975       return LHS.srem(RHS);
8976     case Instruction::Shl:
8977       return LHS.shl(RHS);
8978     case Instruction::LShr:
8979       return LHS.lshr(RHS);
8980     case Instruction::AShr:
8981       return LHS.ashr(RHS);
8982     case Instruction::And:
8983       return LHS & RHS;
8984     case Instruction::Or:
8985       return LHS | RHS;
8986     case Instruction::Xor:
8987       return LHS ^ RHS;
8988     }
8989   }
8990 
8991   bool calculateBinaryOperatorAndTakeUnion(const BinaryOperator *BinOp,
8992                                            const APInt &LHS, const APInt &RHS) {
8993     bool SkipOperation = false;
8994     bool Unsupported = false;
8995     APInt Result =
8996         calculateBinaryOperator(BinOp, LHS, RHS, SkipOperation, Unsupported);
8997     if (Unsupported)
8998       return false;
8999     // If SkipOperation is true, we can ignore this operand pair (L, R).
9000     if (!SkipOperation)
9001       unionAssumed(Result);
9002     return isValidState();
9003   }
9004 
9005   ChangeStatus updateWithICmpInst(Attributor &A, ICmpInst *ICI) {
9006     auto AssumedBefore = getAssumed();
9007     Value *LHS = ICI->getOperand(0);
9008     Value *RHS = ICI->getOperand(1);
9009 
9010     // Simplify the operands first.
9011     bool UsedAssumedInformation = false;
9012     const auto &SimplifiedLHS =
9013         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
9014                                *this, UsedAssumedInformation);
9015     if (!SimplifiedLHS.hasValue())
9016       return ChangeStatus::UNCHANGED;
9017     if (!SimplifiedLHS.getValue())
9018       return indicatePessimisticFixpoint();
9019     LHS = *SimplifiedLHS;
9020 
9021     const auto &SimplifiedRHS =
9022         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
9023                                *this, UsedAssumedInformation);
9024     if (!SimplifiedRHS.hasValue())
9025       return ChangeStatus::UNCHANGED;
9026     if (!SimplifiedRHS.getValue())
9027       return indicatePessimisticFixpoint();
9028     RHS = *SimplifiedRHS;
9029 
9030     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
9031       return indicatePessimisticFixpoint();
9032 
9033     auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
9034                                                 DepClassTy::REQUIRED);
9035     if (!LHSAA.isValidState())
9036       return indicatePessimisticFixpoint();
9037 
9038     auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
9039                                                 DepClassTy::REQUIRED);
9040     if (!RHSAA.isValidState())
9041       return indicatePessimisticFixpoint();
9042 
9043     const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
9044     const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
9045 
9046     // TODO: make use of undef flag to limit potential values aggressively.
9047     bool MaybeTrue = false, MaybeFalse = false;
9048     const APInt Zero(RHS->getType()->getIntegerBitWidth(), 0);
9049     if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
9050       // The result of any comparison between undefs can be soundly replaced
9051       // with undef.
9052       unionAssumedWithUndef();
9053     } else if (LHSAA.undefIsContained()) {
9054       for (const APInt &R : RHSAAPVS) {
9055         bool CmpResult = calculateICmpInst(ICI, Zero, R);
9056         MaybeTrue |= CmpResult;
9057         MaybeFalse |= !CmpResult;
9058         if (MaybeTrue & MaybeFalse)
9059           return indicatePessimisticFixpoint();
9060       }
9061     } else if (RHSAA.undefIsContained()) {
9062       for (const APInt &L : LHSAAPVS) {
9063         bool CmpResult = calculateICmpInst(ICI, L, Zero);
9064         MaybeTrue |= CmpResult;
9065         MaybeFalse |= !CmpResult;
9066         if (MaybeTrue & MaybeFalse)
9067           return indicatePessimisticFixpoint();
9068       }
9069     } else {
9070       for (const APInt &L : LHSAAPVS) {
9071         for (const APInt &R : RHSAAPVS) {
9072           bool CmpResult = calculateICmpInst(ICI, L, R);
9073           MaybeTrue |= CmpResult;
9074           MaybeFalse |= !CmpResult;
9075           if (MaybeTrue & MaybeFalse)
9076             return indicatePessimisticFixpoint();
9077         }
9078       }
9079     }
9080     if (MaybeTrue)
9081       unionAssumed(APInt(/* numBits */ 1, /* val */ 1));
9082     if (MaybeFalse)
9083       unionAssumed(APInt(/* numBits */ 1, /* val */ 0));
9084     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9085                                          : ChangeStatus::CHANGED;
9086   }
9087 
9088   ChangeStatus updateWithSelectInst(Attributor &A, SelectInst *SI) {
9089     auto AssumedBefore = getAssumed();
9090     Value *LHS = SI->getTrueValue();
9091     Value *RHS = SI->getFalseValue();
9092 
9093     // Simplify the operands first.
9094     bool UsedAssumedInformation = false;
9095     const auto &SimplifiedLHS =
9096         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
9097                                *this, UsedAssumedInformation);
9098     if (!SimplifiedLHS.hasValue())
9099       return ChangeStatus::UNCHANGED;
9100     if (!SimplifiedLHS.getValue())
9101       return indicatePessimisticFixpoint();
9102     LHS = *SimplifiedLHS;
9103 
9104     const auto &SimplifiedRHS =
9105         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
9106                                *this, UsedAssumedInformation);
9107     if (!SimplifiedRHS.hasValue())
9108       return ChangeStatus::UNCHANGED;
9109     if (!SimplifiedRHS.getValue())
9110       return indicatePessimisticFixpoint();
9111     RHS = *SimplifiedRHS;
9112 
9113     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
9114       return indicatePessimisticFixpoint();
9115 
9116     Optional<Constant *> C = A.getAssumedConstant(*SI->getCondition(), *this,
9117                                                   UsedAssumedInformation);
9118 
9119     // Check if we only need one operand.
9120     bool OnlyLeft = false, OnlyRight = false;
9121     if (C.hasValue() && *C && (*C)->isOneValue())
9122       OnlyLeft = true;
9123     else if (C.hasValue() && *C && (*C)->isZeroValue())
9124       OnlyRight = true;
9125 
9126     const AAPotentialValues *LHSAA = nullptr, *RHSAA = nullptr;
9127     if (!OnlyRight) {
9128       LHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
9129                                              DepClassTy::REQUIRED);
9130       if (!LHSAA->isValidState())
9131         return indicatePessimisticFixpoint();
9132     }
9133     if (!OnlyLeft) {
9134       RHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
9135                                              DepClassTy::REQUIRED);
9136       if (!RHSAA->isValidState())
9137         return indicatePessimisticFixpoint();
9138     }
9139 
9140     if (!LHSAA || !RHSAA) {
9141       // select (true/false), lhs, rhs
9142       auto *OpAA = LHSAA ? LHSAA : RHSAA;
9143 
9144       if (OpAA->undefIsContained())
9145         unionAssumedWithUndef();
9146       else
9147         unionAssumed(*OpAA);
9148 
9149     } else if (LHSAA->undefIsContained() && RHSAA->undefIsContained()) {
9150       // select i1 *, undef , undef => undef
9151       unionAssumedWithUndef();
9152     } else {
9153       unionAssumed(*LHSAA);
9154       unionAssumed(*RHSAA);
9155     }
9156     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9157                                          : ChangeStatus::CHANGED;
9158   }
9159 
9160   ChangeStatus updateWithCastInst(Attributor &A, CastInst *CI) {
9161     auto AssumedBefore = getAssumed();
9162     if (!CI->isIntegerCast())
9163       return indicatePessimisticFixpoint();
9164     assert(CI->getNumOperands() == 1 && "Expected cast to be unary!");
9165     uint32_t ResultBitWidth = CI->getDestTy()->getIntegerBitWidth();
9166     Value *Src = CI->getOperand(0);
9167 
9168     // Simplify the operand first.
9169     bool UsedAssumedInformation = false;
9170     const auto &SimplifiedSrc =
9171         A.getAssumedSimplified(IRPosition::value(*Src, getCallBaseContext()),
9172                                *this, UsedAssumedInformation);
9173     if (!SimplifiedSrc.hasValue())
9174       return ChangeStatus::UNCHANGED;
9175     if (!SimplifiedSrc.getValue())
9176       return indicatePessimisticFixpoint();
9177     Src = *SimplifiedSrc;
9178 
9179     auto &SrcAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*Src),
9180                                                 DepClassTy::REQUIRED);
9181     if (!SrcAA.isValidState())
9182       return indicatePessimisticFixpoint();
9183     const DenseSet<APInt> &SrcAAPVS = SrcAA.getAssumedSet();
9184     if (SrcAA.undefIsContained())
9185       unionAssumedWithUndef();
9186     else {
9187       for (const APInt &S : SrcAAPVS) {
9188         APInt T = calculateCastInst(CI, S, ResultBitWidth);
9189         unionAssumed(T);
9190       }
9191     }
9192     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9193                                          : ChangeStatus::CHANGED;
9194   }
9195 
9196   ChangeStatus updateWithBinaryOperator(Attributor &A, BinaryOperator *BinOp) {
9197     auto AssumedBefore = getAssumed();
9198     Value *LHS = BinOp->getOperand(0);
9199     Value *RHS = BinOp->getOperand(1);
9200 
9201     // Simplify the operands first.
9202     bool UsedAssumedInformation = false;
9203     const auto &SimplifiedLHS =
9204         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
9205                                *this, UsedAssumedInformation);
9206     if (!SimplifiedLHS.hasValue())
9207       return ChangeStatus::UNCHANGED;
9208     if (!SimplifiedLHS.getValue())
9209       return indicatePessimisticFixpoint();
9210     LHS = *SimplifiedLHS;
9211 
9212     const auto &SimplifiedRHS =
9213         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
9214                                *this, UsedAssumedInformation);
9215     if (!SimplifiedRHS.hasValue())
9216       return ChangeStatus::UNCHANGED;
9217     if (!SimplifiedRHS.getValue())
9218       return indicatePessimisticFixpoint();
9219     RHS = *SimplifiedRHS;
9220 
9221     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
9222       return indicatePessimisticFixpoint();
9223 
9224     auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
9225                                                 DepClassTy::REQUIRED);
9226     if (!LHSAA.isValidState())
9227       return indicatePessimisticFixpoint();
9228 
9229     auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
9230                                                 DepClassTy::REQUIRED);
9231     if (!RHSAA.isValidState())
9232       return indicatePessimisticFixpoint();
9233 
9234     const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
9235     const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
9236     const APInt Zero = APInt(LHS->getType()->getIntegerBitWidth(), 0);
9237 
9238     // TODO: make use of undef flag to limit potential values aggressively.
9239     if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
9240       if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, Zero))
9241         return indicatePessimisticFixpoint();
9242     } else if (LHSAA.undefIsContained()) {
9243       for (const APInt &R : RHSAAPVS) {
9244         if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, R))
9245           return indicatePessimisticFixpoint();
9246       }
9247     } else if (RHSAA.undefIsContained()) {
9248       for (const APInt &L : LHSAAPVS) {
9249         if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, Zero))
9250           return indicatePessimisticFixpoint();
9251       }
9252     } else {
9253       for (const APInt &L : LHSAAPVS) {
9254         for (const APInt &R : RHSAAPVS) {
9255           if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, R))
9256             return indicatePessimisticFixpoint();
9257         }
9258       }
9259     }
9260     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9261                                          : ChangeStatus::CHANGED;
9262   }
9263 
9264   ChangeStatus updateWithPHINode(Attributor &A, PHINode *PHI) {
9265     auto AssumedBefore = getAssumed();
9266     for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
9267       Value *IncomingValue = PHI->getIncomingValue(u);
9268 
9269       // Simplify the operand first.
9270       bool UsedAssumedInformation = false;
9271       const auto &SimplifiedIncomingValue = A.getAssumedSimplified(
9272           IRPosition::value(*IncomingValue, getCallBaseContext()), *this,
9273           UsedAssumedInformation);
9274       if (!SimplifiedIncomingValue.hasValue())
9275         continue;
9276       if (!SimplifiedIncomingValue.getValue())
9277         return indicatePessimisticFixpoint();
9278       IncomingValue = *SimplifiedIncomingValue;
9279 
9280       auto &PotentialValuesAA = A.getAAFor<AAPotentialValues>(
9281           *this, IRPosition::value(*IncomingValue), DepClassTy::REQUIRED);
9282       if (!PotentialValuesAA.isValidState())
9283         return indicatePessimisticFixpoint();
9284       if (PotentialValuesAA.undefIsContained())
9285         unionAssumedWithUndef();
9286       else
9287         unionAssumed(PotentialValuesAA.getAssumed());
9288     }
9289     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9290                                          : ChangeStatus::CHANGED;
9291   }
9292 
9293   ChangeStatus updateWithLoad(Attributor &A, LoadInst &L) {
9294     if (!L.getType()->isIntegerTy())
9295       return indicatePessimisticFixpoint();
9296 
9297     auto Union = [&](Value &V) {
9298       if (isa<UndefValue>(V)) {
9299         unionAssumedWithUndef();
9300         return true;
9301       }
9302       if (ConstantInt *CI = dyn_cast<ConstantInt>(&V)) {
9303         unionAssumed(CI->getValue());
9304         return true;
9305       }
9306       return false;
9307     };
9308     auto AssumedBefore = getAssumed();
9309 
9310     if (!AAValueSimplifyImpl::handleLoad(A, *this, L, Union))
9311       return indicatePessimisticFixpoint();
9312 
9313     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9314                                          : ChangeStatus::CHANGED;
9315   }
9316 
9317   /// See AbstractAttribute::updateImpl(...).
9318   ChangeStatus updateImpl(Attributor &A) override {
9319     Value &V = getAssociatedValue();
9320     Instruction *I = dyn_cast<Instruction>(&V);
9321 
9322     if (auto *ICI = dyn_cast<ICmpInst>(I))
9323       return updateWithICmpInst(A, ICI);
9324 
9325     if (auto *SI = dyn_cast<SelectInst>(I))
9326       return updateWithSelectInst(A, SI);
9327 
9328     if (auto *CI = dyn_cast<CastInst>(I))
9329       return updateWithCastInst(A, CI);
9330 
9331     if (auto *BinOp = dyn_cast<BinaryOperator>(I))
9332       return updateWithBinaryOperator(A, BinOp);
9333 
9334     if (auto *PHI = dyn_cast<PHINode>(I))
9335       return updateWithPHINode(A, PHI);
9336 
9337     if (auto *L = dyn_cast<LoadInst>(I))
9338       return updateWithLoad(A, *L);
9339 
9340     return indicatePessimisticFixpoint();
9341   }
9342 
9343   /// See AbstractAttribute::trackStatistics()
9344   void trackStatistics() const override {
9345     STATS_DECLTRACK_FLOATING_ATTR(potential_values)
9346   }
9347 };
9348 
9349 struct AAPotentialValuesFunction : AAPotentialValuesImpl {
9350   AAPotentialValuesFunction(const IRPosition &IRP, Attributor &A)
9351       : AAPotentialValuesImpl(IRP, A) {}
9352 
9353   /// See AbstractAttribute::initialize(...).
9354   ChangeStatus updateImpl(Attributor &A) override {
9355     llvm_unreachable("AAPotentialValues(Function|CallSite)::updateImpl will "
9356                      "not be called");
9357   }
9358 
9359   /// See AbstractAttribute::trackStatistics()
9360   void trackStatistics() const override {
9361     STATS_DECLTRACK_FN_ATTR(potential_values)
9362   }
9363 };
9364 
9365 struct AAPotentialValuesCallSite : AAPotentialValuesFunction {
9366   AAPotentialValuesCallSite(const IRPosition &IRP, Attributor &A)
9367       : AAPotentialValuesFunction(IRP, A) {}
9368 
9369   /// See AbstractAttribute::trackStatistics()
9370   void trackStatistics() const override {
9371     STATS_DECLTRACK_CS_ATTR(potential_values)
9372   }
9373 };
9374 
9375 struct AAPotentialValuesCallSiteReturned
9376     : AACallSiteReturnedFromReturned<AAPotentialValues, AAPotentialValuesImpl> {
9377   AAPotentialValuesCallSiteReturned(const IRPosition &IRP, Attributor &A)
9378       : AACallSiteReturnedFromReturned<AAPotentialValues,
9379                                        AAPotentialValuesImpl>(IRP, A) {}
9380 
9381   /// See AbstractAttribute::trackStatistics()
9382   void trackStatistics() const override {
9383     STATS_DECLTRACK_CSRET_ATTR(potential_values)
9384   }
9385 };
9386 
9387 struct AAPotentialValuesCallSiteArgument : AAPotentialValuesFloating {
9388   AAPotentialValuesCallSiteArgument(const IRPosition &IRP, Attributor &A)
9389       : AAPotentialValuesFloating(IRP, A) {}
9390 
9391   /// See AbstractAttribute::initialize(..).
9392   void initialize(Attributor &A) override {
9393     AAPotentialValuesImpl::initialize(A);
9394     if (isAtFixpoint())
9395       return;
9396 
9397     Value &V = getAssociatedValue();
9398 
9399     if (auto *C = dyn_cast<ConstantInt>(&V)) {
9400       unionAssumed(C->getValue());
9401       indicateOptimisticFixpoint();
9402       return;
9403     }
9404 
9405     if (isa<UndefValue>(&V)) {
9406       unionAssumedWithUndef();
9407       indicateOptimisticFixpoint();
9408       return;
9409     }
9410   }
9411 
9412   /// See AbstractAttribute::updateImpl(...).
9413   ChangeStatus updateImpl(Attributor &A) override {
9414     Value &V = getAssociatedValue();
9415     auto AssumedBefore = getAssumed();
9416     auto &AA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(V),
9417                                              DepClassTy::REQUIRED);
9418     const auto &S = AA.getAssumed();
9419     unionAssumed(S);
9420     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9421                                          : ChangeStatus::CHANGED;
9422   }
9423 
9424   /// See AbstractAttribute::trackStatistics()
9425   void trackStatistics() const override {
9426     STATS_DECLTRACK_CSARG_ATTR(potential_values)
9427   }
9428 };
9429 
9430 /// ------------------------ NoUndef Attribute ---------------------------------
9431 struct AANoUndefImpl : AANoUndef {
9432   AANoUndefImpl(const IRPosition &IRP, Attributor &A) : AANoUndef(IRP, A) {}
9433 
9434   /// See AbstractAttribute::initialize(...).
9435   void initialize(Attributor &A) override {
9436     if (getIRPosition().hasAttr({Attribute::NoUndef})) {
9437       indicateOptimisticFixpoint();
9438       return;
9439     }
9440     Value &V = getAssociatedValue();
9441     if (isa<UndefValue>(V))
9442       indicatePessimisticFixpoint();
9443     else if (isa<FreezeInst>(V))
9444       indicateOptimisticFixpoint();
9445     else if (getPositionKind() != IRPosition::IRP_RETURNED &&
9446              isGuaranteedNotToBeUndefOrPoison(&V))
9447       indicateOptimisticFixpoint();
9448     else
9449       AANoUndef::initialize(A);
9450   }
9451 
9452   /// See followUsesInMBEC
9453   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
9454                        AANoUndef::StateType &State) {
9455     const Value *UseV = U->get();
9456     const DominatorTree *DT = nullptr;
9457     AssumptionCache *AC = nullptr;
9458     InformationCache &InfoCache = A.getInfoCache();
9459     if (Function *F = getAnchorScope()) {
9460       DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
9461       AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
9462     }
9463     State.setKnown(isGuaranteedNotToBeUndefOrPoison(UseV, AC, I, DT));
9464     bool TrackUse = false;
9465     // Track use for instructions which must produce undef or poison bits when
9466     // at least one operand contains such bits.
9467     if (isa<CastInst>(*I) || isa<GetElementPtrInst>(*I))
9468       TrackUse = true;
9469     return TrackUse;
9470   }
9471 
9472   /// See AbstractAttribute::getAsStr().
9473   const std::string getAsStr() const override {
9474     return getAssumed() ? "noundef" : "may-undef-or-poison";
9475   }
9476 
9477   ChangeStatus manifest(Attributor &A) override {
9478     // We don't manifest noundef attribute for dead positions because the
9479     // associated values with dead positions would be replaced with undef
9480     // values.
9481     bool UsedAssumedInformation = false;
9482     if (A.isAssumedDead(getIRPosition(), nullptr, nullptr,
9483                         UsedAssumedInformation))
9484       return ChangeStatus::UNCHANGED;
9485     // A position whose simplified value does not have any value is
9486     // considered to be dead. We don't manifest noundef in such positions for
9487     // the same reason above.
9488     if (!A.getAssumedSimplified(getIRPosition(), *this, UsedAssumedInformation)
9489              .hasValue())
9490       return ChangeStatus::UNCHANGED;
9491     return AANoUndef::manifest(A);
9492   }
9493 };
9494 
9495 struct AANoUndefFloating : public AANoUndefImpl {
9496   AANoUndefFloating(const IRPosition &IRP, Attributor &A)
9497       : AANoUndefImpl(IRP, A) {}
9498 
9499   /// See AbstractAttribute::initialize(...).
9500   void initialize(Attributor &A) override {
9501     AANoUndefImpl::initialize(A);
9502     if (!getState().isAtFixpoint())
9503       if (Instruction *CtxI = getCtxI())
9504         followUsesInMBEC(*this, A, getState(), *CtxI);
9505   }
9506 
9507   /// See AbstractAttribute::updateImpl(...).
9508   ChangeStatus updateImpl(Attributor &A) override {
9509     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
9510                             AANoUndef::StateType &T, bool Stripped) -> bool {
9511       const auto &AA = A.getAAFor<AANoUndef>(*this, IRPosition::value(V),
9512                                              DepClassTy::REQUIRED);
9513       if (!Stripped && this == &AA) {
9514         T.indicatePessimisticFixpoint();
9515       } else {
9516         const AANoUndef::StateType &S =
9517             static_cast<const AANoUndef::StateType &>(AA.getState());
9518         T ^= S;
9519       }
9520       return T.isValidState();
9521     };
9522 
9523     StateType T;
9524     bool UsedAssumedInformation = false;
9525     if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
9526                                           VisitValueCB, getCtxI(),
9527                                           UsedAssumedInformation))
9528       return indicatePessimisticFixpoint();
9529 
9530     return clampStateAndIndicateChange(getState(), T);
9531   }
9532 
9533   /// See AbstractAttribute::trackStatistics()
9534   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
9535 };
9536 
9537 struct AANoUndefReturned final
9538     : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl> {
9539   AANoUndefReturned(const IRPosition &IRP, Attributor &A)
9540       : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl>(IRP, A) {}
9541 
9542   /// See AbstractAttribute::trackStatistics()
9543   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
9544 };
9545 
9546 struct AANoUndefArgument final
9547     : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl> {
9548   AANoUndefArgument(const IRPosition &IRP, Attributor &A)
9549       : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl>(IRP, A) {}
9550 
9551   /// See AbstractAttribute::trackStatistics()
9552   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noundef) }
9553 };
9554 
9555 struct AANoUndefCallSiteArgument final : AANoUndefFloating {
9556   AANoUndefCallSiteArgument(const IRPosition &IRP, Attributor &A)
9557       : AANoUndefFloating(IRP, A) {}
9558 
9559   /// See AbstractAttribute::trackStatistics()
9560   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noundef) }
9561 };
9562 
9563 struct AANoUndefCallSiteReturned final
9564     : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl> {
9565   AANoUndefCallSiteReturned(const IRPosition &IRP, Attributor &A)
9566       : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl>(IRP, A) {}
9567 
9568   /// See AbstractAttribute::trackStatistics()
9569   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noundef) }
9570 };
9571 
9572 struct AACallEdgesImpl : public AACallEdges {
9573   AACallEdgesImpl(const IRPosition &IRP, Attributor &A) : AACallEdges(IRP, A) {}
9574 
9575   virtual const SetVector<Function *> &getOptimisticEdges() const override {
9576     return CalledFunctions;
9577   }
9578 
9579   virtual bool hasUnknownCallee() const override { return HasUnknownCallee; }
9580 
9581   virtual bool hasNonAsmUnknownCallee() const override {
9582     return HasUnknownCalleeNonAsm;
9583   }
9584 
9585   const std::string getAsStr() const override {
9586     return "CallEdges[" + std::to_string(HasUnknownCallee) + "," +
9587            std::to_string(CalledFunctions.size()) + "]";
9588   }
9589 
9590   void trackStatistics() const override {}
9591 
9592 protected:
9593   void addCalledFunction(Function *Fn, ChangeStatus &Change) {
9594     if (CalledFunctions.insert(Fn)) {
9595       Change = ChangeStatus::CHANGED;
9596       LLVM_DEBUG(dbgs() << "[AACallEdges] New call edge: " << Fn->getName()
9597                         << "\n");
9598     }
9599   }
9600 
9601   void setHasUnknownCallee(bool NonAsm, ChangeStatus &Change) {
9602     if (!HasUnknownCallee)
9603       Change = ChangeStatus::CHANGED;
9604     if (NonAsm && !HasUnknownCalleeNonAsm)
9605       Change = ChangeStatus::CHANGED;
9606     HasUnknownCalleeNonAsm |= NonAsm;
9607     HasUnknownCallee = true;
9608   }
9609 
9610 private:
9611   /// Optimistic set of functions that might be called by this position.
9612   SetVector<Function *> CalledFunctions;
9613 
9614   /// Is there any call with a unknown callee.
9615   bool HasUnknownCallee = false;
9616 
9617   /// Is there any call with a unknown callee, excluding any inline asm.
9618   bool HasUnknownCalleeNonAsm = false;
9619 };
9620 
9621 struct AACallEdgesCallSite : public AACallEdgesImpl {
9622   AACallEdgesCallSite(const IRPosition &IRP, Attributor &A)
9623       : AACallEdgesImpl(IRP, A) {}
9624   /// See AbstractAttribute::updateImpl(...).
9625   ChangeStatus updateImpl(Attributor &A) override {
9626     ChangeStatus Change = ChangeStatus::UNCHANGED;
9627 
9628     auto VisitValue = [&](Value &V, const Instruction *CtxI, bool &HasUnknown,
9629                           bool Stripped) -> bool {
9630       if (Function *Fn = dyn_cast<Function>(&V)) {
9631         addCalledFunction(Fn, Change);
9632       } else {
9633         LLVM_DEBUG(dbgs() << "[AACallEdges] Unrecognized value: " << V << "\n");
9634         setHasUnknownCallee(true, Change);
9635       }
9636 
9637       // Explore all values.
9638       return true;
9639     };
9640 
9641     // Process any value that we might call.
9642     auto ProcessCalledOperand = [&](Value *V) {
9643       bool DummyValue = false;
9644       bool UsedAssumedInformation = false;
9645       if (!genericValueTraversal<bool>(A, IRPosition::value(*V), *this,
9646                                        DummyValue, VisitValue, nullptr,
9647                                        UsedAssumedInformation, false)) {
9648         // If we haven't gone through all values, assume that there are unknown
9649         // callees.
9650         setHasUnknownCallee(true, Change);
9651       }
9652     };
9653 
9654     CallBase *CB = cast<CallBase>(getCtxI());
9655 
9656     if (CB->isInlineAsm()) {
9657       setHasUnknownCallee(false, Change);
9658       return Change;
9659     }
9660 
9661     // Process callee metadata if available.
9662     if (auto *MD = getCtxI()->getMetadata(LLVMContext::MD_callees)) {
9663       for (auto &Op : MD->operands()) {
9664         Function *Callee = mdconst::dyn_extract_or_null<Function>(Op);
9665         if (Callee)
9666           addCalledFunction(Callee, Change);
9667       }
9668       return Change;
9669     }
9670 
9671     // The most simple case.
9672     ProcessCalledOperand(CB->getCalledOperand());
9673 
9674     // Process callback functions.
9675     SmallVector<const Use *, 4u> CallbackUses;
9676     AbstractCallSite::getCallbackUses(*CB, CallbackUses);
9677     for (const Use *U : CallbackUses)
9678       ProcessCalledOperand(U->get());
9679 
9680     return Change;
9681   }
9682 };
9683 
9684 struct AACallEdgesFunction : public AACallEdgesImpl {
9685   AACallEdgesFunction(const IRPosition &IRP, Attributor &A)
9686       : AACallEdgesImpl(IRP, A) {}
9687 
9688   /// See AbstractAttribute::updateImpl(...).
9689   ChangeStatus updateImpl(Attributor &A) override {
9690     ChangeStatus Change = ChangeStatus::UNCHANGED;
9691 
9692     auto ProcessCallInst = [&](Instruction &Inst) {
9693       CallBase &CB = cast<CallBase>(Inst);
9694 
9695       auto &CBEdges = A.getAAFor<AACallEdges>(
9696           *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
9697       if (CBEdges.hasNonAsmUnknownCallee())
9698         setHasUnknownCallee(true, Change);
9699       if (CBEdges.hasUnknownCallee())
9700         setHasUnknownCallee(false, Change);
9701 
9702       for (Function *F : CBEdges.getOptimisticEdges())
9703         addCalledFunction(F, Change);
9704 
9705       return true;
9706     };
9707 
9708     // Visit all callable instructions.
9709     bool UsedAssumedInformation = false;
9710     if (!A.checkForAllCallLikeInstructions(ProcessCallInst, *this,
9711                                            UsedAssumedInformation,
9712                                            /* CheckBBLivenessOnly */ true)) {
9713       // If we haven't looked at all call like instructions, assume that there
9714       // are unknown callees.
9715       setHasUnknownCallee(true, Change);
9716     }
9717 
9718     return Change;
9719   }
9720 };
9721 
9722 struct AAFunctionReachabilityFunction : public AAFunctionReachability {
9723 private:
9724   struct QuerySet {
9725     void markReachable(const Function &Fn) {
9726       Reachable.insert(&Fn);
9727       Unreachable.erase(&Fn);
9728     }
9729 
9730     /// If there is no information about the function None is returned.
9731     Optional<bool> isCachedReachable(const Function &Fn) {
9732       // Assume that we can reach the function.
9733       // TODO: Be more specific with the unknown callee.
9734       if (CanReachUnknownCallee)
9735         return true;
9736 
9737       if (Reachable.count(&Fn))
9738         return true;
9739 
9740       if (Unreachable.count(&Fn))
9741         return false;
9742 
9743       return llvm::None;
9744     }
9745 
9746     /// Set of functions that we know for sure is reachable.
9747     DenseSet<const Function *> Reachable;
9748 
9749     /// Set of functions that are unreachable, but might become reachable.
9750     DenseSet<const Function *> Unreachable;
9751 
9752     /// If we can reach a function with a call to a unknown function we assume
9753     /// that we can reach any function.
9754     bool CanReachUnknownCallee = false;
9755   };
9756 
9757   struct QueryResolver : public QuerySet {
9758     ChangeStatus update(Attributor &A, const AAFunctionReachability &AA,
9759                         ArrayRef<const AACallEdges *> AAEdgesList) {
9760       ChangeStatus Change = ChangeStatus::UNCHANGED;
9761 
9762       for (auto *AAEdges : AAEdgesList) {
9763         if (AAEdges->hasUnknownCallee()) {
9764           if (!CanReachUnknownCallee)
9765             Change = ChangeStatus::CHANGED;
9766           CanReachUnknownCallee = true;
9767           return Change;
9768         }
9769       }
9770 
9771       for (const Function *Fn : make_early_inc_range(Unreachable)) {
9772         if (checkIfReachable(A, AA, AAEdgesList, *Fn)) {
9773           Change = ChangeStatus::CHANGED;
9774           markReachable(*Fn);
9775         }
9776       }
9777       return Change;
9778     }
9779 
9780     bool isReachable(Attributor &A, AAFunctionReachability &AA,
9781                      ArrayRef<const AACallEdges *> AAEdgesList,
9782                      const Function &Fn) {
9783       Optional<bool> Cached = isCachedReachable(Fn);
9784       if (Cached.hasValue())
9785         return Cached.getValue();
9786 
9787       // The query was not cached, thus it is new. We need to request an update
9788       // explicitly to make sure this the information is properly run to a
9789       // fixpoint.
9790       A.registerForUpdate(AA);
9791 
9792       // We need to assume that this function can't reach Fn to prevent
9793       // an infinite loop if this function is recursive.
9794       Unreachable.insert(&Fn);
9795 
9796       bool Result = checkIfReachable(A, AA, AAEdgesList, Fn);
9797       if (Result)
9798         markReachable(Fn);
9799       return Result;
9800     }
9801 
9802     bool checkIfReachable(Attributor &A, const AAFunctionReachability &AA,
9803                           ArrayRef<const AACallEdges *> AAEdgesList,
9804                           const Function &Fn) const {
9805 
9806       // Handle the most trivial case first.
9807       for (auto *AAEdges : AAEdgesList) {
9808         const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges();
9809 
9810         if (Edges.count(const_cast<Function *>(&Fn)))
9811           return true;
9812       }
9813 
9814       SmallVector<const AAFunctionReachability *, 8> Deps;
9815       for (auto &AAEdges : AAEdgesList) {
9816         const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges();
9817 
9818         for (Function *Edge : Edges) {
9819           // We don't need a dependency if the result is reachable.
9820           const AAFunctionReachability &EdgeReachability =
9821               A.getAAFor<AAFunctionReachability>(
9822                   AA, IRPosition::function(*Edge), DepClassTy::NONE);
9823           Deps.push_back(&EdgeReachability);
9824 
9825           if (EdgeReachability.canReach(A, Fn))
9826             return true;
9827         }
9828       }
9829 
9830       // The result is false for now, set dependencies and leave.
9831       for (auto *Dep : Deps)
9832         A.recordDependence(*Dep, AA, DepClassTy::REQUIRED);
9833 
9834       return false;
9835     }
9836   };
9837 
9838   /// Get call edges that can be reached by this instruction.
9839   bool getReachableCallEdges(Attributor &A, const AAReachability &Reachability,
9840                              const Instruction &Inst,
9841                              SmallVector<const AACallEdges *> &Result) const {
9842     // Determine call like instructions that we can reach from the inst.
9843     auto CheckCallBase = [&](Instruction &CBInst) {
9844       if (!Reachability.isAssumedReachable(A, Inst, CBInst))
9845         return true;
9846 
9847       auto &CB = cast<CallBase>(CBInst);
9848       const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
9849           *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
9850 
9851       Result.push_back(&AAEdges);
9852       return true;
9853     };
9854 
9855     bool UsedAssumedInformation = false;
9856     return A.checkForAllCallLikeInstructions(CheckCallBase, *this,
9857                                              UsedAssumedInformation,
9858                                              /* CheckBBLivenessOnly */ true);
9859   }
9860 
9861 public:
9862   AAFunctionReachabilityFunction(const IRPosition &IRP, Attributor &A)
9863       : AAFunctionReachability(IRP, A) {}
9864 
9865   bool canReach(Attributor &A, const Function &Fn) const override {
9866     if (!isValidState())
9867       return true;
9868 
9869     const AACallEdges &AAEdges =
9870         A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
9871 
9872     // Attributor returns attributes as const, so this function has to be
9873     // const for users of this attribute to use it without having to do
9874     // a const_cast.
9875     // This is a hack for us to be able to cache queries.
9876     auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9877     bool Result = NonConstThis->WholeFunction.isReachable(A, *NonConstThis,
9878                                                           {&AAEdges}, Fn);
9879 
9880     return Result;
9881   }
9882 
9883   /// Can \p CB reach \p Fn
9884   bool canReach(Attributor &A, CallBase &CB,
9885                 const Function &Fn) const override {
9886     if (!isValidState())
9887       return true;
9888 
9889     const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
9890         *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
9891 
9892     // Attributor returns attributes as const, so this function has to be
9893     // const for users of this attribute to use it without having to do
9894     // a const_cast.
9895     // This is a hack for us to be able to cache queries.
9896     auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9897     QueryResolver &CBQuery = NonConstThis->CBQueries[&CB];
9898 
9899     bool Result = CBQuery.isReachable(A, *NonConstThis, {&AAEdges}, Fn);
9900 
9901     return Result;
9902   }
9903 
9904   bool instructionCanReach(Attributor &A, const Instruction &Inst,
9905                            const Function &Fn,
9906                            bool UseBackwards) const override {
9907     if (!isValidState())
9908       return true;
9909 
9910     if (UseBackwards)
9911       return AA::isPotentiallyReachable(A, Inst, Fn, *this, nullptr);
9912 
9913     const auto &Reachability = A.getAAFor<AAReachability>(
9914         *this, IRPosition::function(*getAssociatedFunction()),
9915         DepClassTy::REQUIRED);
9916 
9917     SmallVector<const AACallEdges *> CallEdges;
9918     bool AllKnown = getReachableCallEdges(A, Reachability, Inst, CallEdges);
9919     // Attributor returns attributes as const, so this function has to be
9920     // const for users of this attribute to use it without having to do
9921     // a const_cast.
9922     // This is a hack for us to be able to cache queries.
9923     auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9924     QueryResolver &InstQSet = NonConstThis->InstQueries[&Inst];
9925     if (!AllKnown)
9926       InstQSet.CanReachUnknownCallee = true;
9927 
9928     return InstQSet.isReachable(A, *NonConstThis, CallEdges, Fn);
9929   }
9930 
9931   /// See AbstractAttribute::updateImpl(...).
9932   ChangeStatus updateImpl(Attributor &A) override {
9933     const AACallEdges &AAEdges =
9934         A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
9935     ChangeStatus Change = ChangeStatus::UNCHANGED;
9936 
9937     Change |= WholeFunction.update(A, *this, {&AAEdges});
9938 
9939     for (auto &CBPair : CBQueries) {
9940       const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
9941           *this, IRPosition::callsite_function(*CBPair.first),
9942           DepClassTy::REQUIRED);
9943 
9944       Change |= CBPair.second.update(A, *this, {&AAEdges});
9945     }
9946 
9947     // Update the Instruction queries.
9948     if (!InstQueries.empty()) {
9949       const AAReachability *Reachability = &A.getAAFor<AAReachability>(
9950           *this, IRPosition::function(*getAssociatedFunction()),
9951           DepClassTy::REQUIRED);
9952 
9953       // Check for local callbases first.
9954       for (auto &InstPair : InstQueries) {
9955         SmallVector<const AACallEdges *> CallEdges;
9956         bool AllKnown =
9957             getReachableCallEdges(A, *Reachability, *InstPair.first, CallEdges);
9958         // Update will return change if we this effects any queries.
9959         if (!AllKnown)
9960           InstPair.second.CanReachUnknownCallee = true;
9961         Change |= InstPair.second.update(A, *this, CallEdges);
9962       }
9963     }
9964 
9965     return Change;
9966   }
9967 
9968   const std::string getAsStr() const override {
9969     size_t QueryCount =
9970         WholeFunction.Reachable.size() + WholeFunction.Unreachable.size();
9971 
9972     return "FunctionReachability [" +
9973            std::to_string(WholeFunction.Reachable.size()) + "," +
9974            std::to_string(QueryCount) + "]";
9975   }
9976 
9977   void trackStatistics() const override {}
9978 
9979 private:
9980   bool canReachUnknownCallee() const override {
9981     return WholeFunction.CanReachUnknownCallee;
9982   }
9983 
9984   /// Used to answer if a the whole function can reacha a specific function.
9985   QueryResolver WholeFunction;
9986 
9987   /// Used to answer if a call base inside this function can reach a specific
9988   /// function.
9989   DenseMap<const CallBase *, QueryResolver> CBQueries;
9990 
9991   /// This is for instruction queries than scan "forward".
9992   DenseMap<const Instruction *, QueryResolver> InstQueries;
9993 };
9994 } // namespace
9995 
9996 /// ---------------------- Assumption Propagation ------------------------------
9997 namespace {
9998 struct AAAssumptionInfoImpl : public AAAssumptionInfo {
9999   AAAssumptionInfoImpl(const IRPosition &IRP, Attributor &A,
10000                        const DenseSet<StringRef> &Known)
10001       : AAAssumptionInfo(IRP, A, Known) {}
10002 
10003   bool hasAssumption(const StringRef Assumption) const override {
10004     return isValidState() && setContains(Assumption);
10005   }
10006 
10007   /// See AbstractAttribute::getAsStr()
10008   const std::string getAsStr() const override {
10009     const SetContents &Known = getKnown();
10010     const SetContents &Assumed = getAssumed();
10011 
10012     const std::string KnownStr =
10013         llvm::join(Known.getSet().begin(), Known.getSet().end(), ",");
10014     const std::string AssumedStr =
10015         (Assumed.isUniversal())
10016             ? "Universal"
10017             : llvm::join(Assumed.getSet().begin(), Assumed.getSet().end(), ",");
10018 
10019     return "Known [" + KnownStr + "]," + " Assumed [" + AssumedStr + "]";
10020   }
10021 };
10022 
10023 /// Propagates assumption information from parent functions to all of their
10024 /// successors. An assumption can be propagated if the containing function
10025 /// dominates the called function.
10026 ///
10027 /// We start with a "known" set of assumptions already valid for the associated
10028 /// function and an "assumed" set that initially contains all possible
10029 /// assumptions. The assumed set is inter-procedurally updated by narrowing its
10030 /// contents as concrete values are known. The concrete values are seeded by the
10031 /// first nodes that are either entries into the call graph, or contains no
10032 /// assumptions. Each node is updated as the intersection of the assumed state
10033 /// with all of its predecessors.
10034 struct AAAssumptionInfoFunction final : AAAssumptionInfoImpl {
10035   AAAssumptionInfoFunction(const IRPosition &IRP, Attributor &A)
10036       : AAAssumptionInfoImpl(IRP, A,
10037                              getAssumptions(*IRP.getAssociatedFunction())) {}
10038 
10039   /// See AbstractAttribute::manifest(...).
10040   ChangeStatus manifest(Attributor &A) override {
10041     const auto &Assumptions = getKnown();
10042 
10043     // Don't manifest a universal set if it somehow made it here.
10044     if (Assumptions.isUniversal())
10045       return ChangeStatus::UNCHANGED;
10046 
10047     Function *AssociatedFunction = getAssociatedFunction();
10048 
10049     bool Changed = addAssumptions(*AssociatedFunction, Assumptions.getSet());
10050 
10051     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
10052   }
10053 
10054   /// See AbstractAttribute::updateImpl(...).
10055   ChangeStatus updateImpl(Attributor &A) override {
10056     bool Changed = false;
10057 
10058     auto CallSitePred = [&](AbstractCallSite ACS) {
10059       const auto &AssumptionAA = A.getAAFor<AAAssumptionInfo>(
10060           *this, IRPosition::callsite_function(*ACS.getInstruction()),
10061           DepClassTy::REQUIRED);
10062       // Get the set of assumptions shared by all of this function's callers.
10063       Changed |= getIntersection(AssumptionAA.getAssumed());
10064       return !getAssumed().empty() || !getKnown().empty();
10065     };
10066 
10067     bool UsedAssumedInformation = false;
10068     // Get the intersection of all assumptions held by this node's predecessors.
10069     // If we don't know all the call sites then this is either an entry into the
10070     // call graph or an empty node. This node is known to only contain its own
10071     // assumptions and can be propagated to its successors.
10072     if (!A.checkForAllCallSites(CallSitePred, *this, true,
10073                                 UsedAssumedInformation))
10074       return indicatePessimisticFixpoint();
10075 
10076     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
10077   }
10078 
10079   void trackStatistics() const override {}
10080 };
10081 
10082 /// Assumption Info defined for call sites.
10083 struct AAAssumptionInfoCallSite final : AAAssumptionInfoImpl {
10084 
10085   AAAssumptionInfoCallSite(const IRPosition &IRP, Attributor &A)
10086       : AAAssumptionInfoImpl(IRP, A, getInitialAssumptions(IRP)) {}
10087 
10088   /// See AbstractAttribute::initialize(...).
10089   void initialize(Attributor &A) override {
10090     const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
10091     A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
10092   }
10093 
10094   /// See AbstractAttribute::manifest(...).
10095   ChangeStatus manifest(Attributor &A) override {
10096     // Don't manifest a universal set if it somehow made it here.
10097     if (getKnown().isUniversal())
10098       return ChangeStatus::UNCHANGED;
10099 
10100     CallBase &AssociatedCall = cast<CallBase>(getAssociatedValue());
10101     bool Changed = addAssumptions(AssociatedCall, getAssumed().getSet());
10102 
10103     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
10104   }
10105 
10106   /// See AbstractAttribute::updateImpl(...).
10107   ChangeStatus updateImpl(Attributor &A) override {
10108     const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
10109     auto &AssumptionAA =
10110         A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
10111     bool Changed = getIntersection(AssumptionAA.getAssumed());
10112     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
10113   }
10114 
10115   /// See AbstractAttribute::trackStatistics()
10116   void trackStatistics() const override {}
10117 
10118 private:
10119   /// Helper to initialized the known set as all the assumptions this call and
10120   /// the callee contain.
10121   DenseSet<StringRef> getInitialAssumptions(const IRPosition &IRP) {
10122     const CallBase &CB = cast<CallBase>(IRP.getAssociatedValue());
10123     auto Assumptions = getAssumptions(CB);
10124     if (Function *F = IRP.getAssociatedFunction())
10125       set_union(Assumptions, getAssumptions(*F));
10126     if (Function *F = IRP.getAssociatedFunction())
10127       set_union(Assumptions, getAssumptions(*F));
10128     return Assumptions;
10129   }
10130 };
10131 } // namespace
10132 
10133 AACallGraphNode *AACallEdgeIterator::operator*() const {
10134   return static_cast<AACallGraphNode *>(const_cast<AACallEdges *>(
10135       &A.getOrCreateAAFor<AACallEdges>(IRPosition::function(**I))));
10136 }
10137 
10138 void AttributorCallGraph::print() { llvm::WriteGraph(outs(), this); }
10139 
10140 const char AAReturnedValues::ID = 0;
10141 const char AANoUnwind::ID = 0;
10142 const char AANoSync::ID = 0;
10143 const char AANoFree::ID = 0;
10144 const char AANonNull::ID = 0;
10145 const char AANoRecurse::ID = 0;
10146 const char AAWillReturn::ID = 0;
10147 const char AAUndefinedBehavior::ID = 0;
10148 const char AANoAlias::ID = 0;
10149 const char AAReachability::ID = 0;
10150 const char AANoReturn::ID = 0;
10151 const char AAIsDead::ID = 0;
10152 const char AADereferenceable::ID = 0;
10153 const char AAAlign::ID = 0;
10154 const char AANoCapture::ID = 0;
10155 const char AAValueSimplify::ID = 0;
10156 const char AAHeapToStack::ID = 0;
10157 const char AAPrivatizablePtr::ID = 0;
10158 const char AAMemoryBehavior::ID = 0;
10159 const char AAMemoryLocation::ID = 0;
10160 const char AAValueConstantRange::ID = 0;
10161 const char AAPotentialValues::ID = 0;
10162 const char AANoUndef::ID = 0;
10163 const char AACallEdges::ID = 0;
10164 const char AAFunctionReachability::ID = 0;
10165 const char AAPointerInfo::ID = 0;
10166 const char AAAssumptionInfo::ID = 0;
10167 
10168 // Macro magic to create the static generator function for attributes that
10169 // follow the naming scheme.
10170 
10171 #define SWITCH_PK_INV(CLASS, PK, POS_NAME)                                     \
10172   case IRPosition::PK:                                                         \
10173     llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
10174 
10175 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX)                               \
10176   case IRPosition::PK:                                                         \
10177     AA = new (A.Allocator) CLASS##SUFFIX(IRP, A);                              \
10178     ++NumAAs;                                                                  \
10179     break;
10180 
10181 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                 \
10182   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10183     CLASS *AA = nullptr;                                                       \
10184     switch (IRP.getPositionKind()) {                                           \
10185       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10186       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
10187       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
10188       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
10189       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
10190       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
10191       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10192       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
10193     }                                                                          \
10194     return *AA;                                                                \
10195   }
10196 
10197 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                    \
10198   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10199     CLASS *AA = nullptr;                                                       \
10200     switch (IRP.getPositionKind()) {                                           \
10201       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10202       SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function")                           \
10203       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
10204       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
10205       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
10206       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
10207       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
10208       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
10209     }                                                                          \
10210     return *AA;                                                                \
10211   }
10212 
10213 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                      \
10214   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10215     CLASS *AA = nullptr;                                                       \
10216     switch (IRP.getPositionKind()) {                                           \
10217       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10218       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10219       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
10220       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
10221       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
10222       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
10223       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
10224       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
10225     }                                                                          \
10226     return *AA;                                                                \
10227   }
10228 
10229 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)            \
10230   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10231     CLASS *AA = nullptr;                                                       \
10232     switch (IRP.getPositionKind()) {                                           \
10233       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10234       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
10235       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
10236       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
10237       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
10238       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
10239       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
10240       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10241     }                                                                          \
10242     return *AA;                                                                \
10243   }
10244 
10245 #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                  \
10246   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10247     CLASS *AA = nullptr;                                                       \
10248     switch (IRP.getPositionKind()) {                                           \
10249       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10250       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
10251       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10252       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
10253       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
10254       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
10255       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
10256       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
10257     }                                                                          \
10258     return *AA;                                                                \
10259   }
10260 
10261 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
10262 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
10263 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
10264 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
10265 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
10266 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
10267 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation)
10268 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AACallEdges)
10269 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAssumptionInfo)
10270 
10271 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
10272 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
10273 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
10274 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
10275 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
10276 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
10277 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
10278 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialValues)
10279 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUndef)
10280 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPointerInfo)
10281 
10282 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
10283 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
10284 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
10285 
10286 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
10287 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
10288 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
10289 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAFunctionReachability)
10290 
10291 CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
10292 
10293 #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
10294 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
10295 #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
10296 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
10297 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
10298 #undef SWITCH_PK_CREATE
10299 #undef SWITCH_PK_INV
10300