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