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/SCCIterator.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/AssumeBundleQueries.h"
21 #include "llvm/Analysis/AssumptionCache.h"
22 #include "llvm/Analysis/CaptureTracking.h"
23 #include "llvm/Analysis/LazyValueInfo.h"
24 #include "llvm/Analysis/MemoryBuiltins.h"
25 #include "llvm/Analysis/ScalarEvolution.h"
26 #include "llvm/Analysis/TargetTransformInfo.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/IR/IRBuilder.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/IntrinsicInst.h"
31 #include "llvm/IR/NoFolder.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Transforms/IPO/ArgumentPromotion.h"
34 #include "llvm/Transforms/Utils/Local.h"
35 
36 #include <cassert>
37 
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "attributor"
41 
42 static cl::opt<bool> ManifestInternal(
43     "attributor-manifest-internal", cl::Hidden,
44     cl::desc("Manifest Attributor internal string attributes."),
45     cl::init(false));
46 
47 static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
48                                        cl::Hidden);
49 
50 template <>
51 unsigned llvm::PotentialConstantIntValuesState::MaxPotentialValues = 0;
52 
53 static cl::opt<unsigned, true> MaxPotentialValues(
54     "attributor-max-potential-values", cl::Hidden,
55     cl::desc("Maximum number of potential values to be "
56              "tracked for each position."),
57     cl::location(llvm::PotentialConstantIntValuesState::MaxPotentialValues),
58     cl::init(7));
59 
60 STATISTIC(NumAAs, "Number of abstract attributes created");
61 
62 // Some helper macros to deal with statistics tracking.
63 //
64 // Usage:
65 // For simple IR attribute tracking overload trackStatistics in the abstract
66 // attribute and choose the right STATS_DECLTRACK_********* macro,
67 // e.g.,:
68 //  void trackStatistics() const override {
69 //    STATS_DECLTRACK_ARG_ATTR(returned)
70 //  }
71 // If there is a single "increment" side one can use the macro
72 // STATS_DECLTRACK with a custom message. If there are multiple increment
73 // sides, STATS_DECL and STATS_TRACK can also be used separately.
74 //
75 #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)                                     \
76   ("Number of " #TYPE " marked '" #NAME "'")
77 #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
78 #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
79 #define STATS_DECL(NAME, TYPE, MSG)                                            \
80   STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
81 #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
82 #define STATS_DECLTRACK(NAME, TYPE, MSG)                                       \
83   {                                                                            \
84     STATS_DECL(NAME, TYPE, MSG)                                                \
85     STATS_TRACK(NAME, TYPE)                                                    \
86   }
87 #define STATS_DECLTRACK_ARG_ATTR(NAME)                                         \
88   STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
89 #define STATS_DECLTRACK_CSARG_ATTR(NAME)                                       \
90   STATS_DECLTRACK(NAME, CSArguments,                                           \
91                   BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
92 #define STATS_DECLTRACK_FN_ATTR(NAME)                                          \
93   STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
94 #define STATS_DECLTRACK_CS_ATTR(NAME)                                          \
95   STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
96 #define STATS_DECLTRACK_FNRET_ATTR(NAME)                                       \
97   STATS_DECLTRACK(NAME, FunctionReturn,                                        \
98                   BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
99 #define STATS_DECLTRACK_CSRET_ATTR(NAME)                                       \
100   STATS_DECLTRACK(NAME, CSReturn,                                              \
101                   BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
102 #define STATS_DECLTRACK_FLOATING_ATTR(NAME)                                    \
103   STATS_DECLTRACK(NAME, Floating,                                              \
104                   ("Number of floating values known to be '" #NAME "'"))
105 
106 // Specialization of the operator<< for abstract attributes subclasses. This
107 // disambiguates situations where multiple operators are applicable.
108 namespace llvm {
109 #define PIPE_OPERATOR(CLASS)                                                   \
110   raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) {                  \
111     return OS << static_cast<const AbstractAttribute &>(AA);                   \
112   }
113 
114 PIPE_OPERATOR(AAIsDead)
115 PIPE_OPERATOR(AANoUnwind)
116 PIPE_OPERATOR(AANoSync)
117 PIPE_OPERATOR(AANoRecurse)
118 PIPE_OPERATOR(AAWillReturn)
119 PIPE_OPERATOR(AANoReturn)
120 PIPE_OPERATOR(AAReturnedValues)
121 PIPE_OPERATOR(AANonNull)
122 PIPE_OPERATOR(AANoAlias)
123 PIPE_OPERATOR(AADereferenceable)
124 PIPE_OPERATOR(AAAlign)
125 PIPE_OPERATOR(AANoCapture)
126 PIPE_OPERATOR(AAValueSimplify)
127 PIPE_OPERATOR(AANoFree)
128 PIPE_OPERATOR(AAHeapToStack)
129 PIPE_OPERATOR(AAReachability)
130 PIPE_OPERATOR(AAMemoryBehavior)
131 PIPE_OPERATOR(AAMemoryLocation)
132 PIPE_OPERATOR(AAValueConstantRange)
133 PIPE_OPERATOR(AAPrivatizablePtr)
134 PIPE_OPERATOR(AAUndefinedBehavior)
135 PIPE_OPERATOR(AAPotentialValues)
136 PIPE_OPERATOR(AANoUndef)
137 
138 #undef PIPE_OPERATOR
139 } // namespace llvm
140 
141 namespace {
142 
143 static Optional<ConstantInt *>
144 getAssumedConstantInt(Attributor &A, const Value &V,
145                       const AbstractAttribute &AA,
146                       bool &UsedAssumedInformation) {
147   Optional<Constant *> C = A.getAssumedConstant(V, AA, UsedAssumedInformation);
148   if (C.hasValue())
149     return dyn_cast_or_null<ConstantInt>(C.getValue());
150   return llvm::None;
151 }
152 
153 /// Get pointer operand of memory accessing instruction. If \p I is
154 /// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
155 /// is set to false and the instruction is volatile, return nullptr.
156 static const Value *getPointerOperand(const Instruction *I,
157                                       bool AllowVolatile) {
158   if (auto *LI = dyn_cast<LoadInst>(I)) {
159     if (!AllowVolatile && LI->isVolatile())
160       return nullptr;
161     return LI->getPointerOperand();
162   }
163 
164   if (auto *SI = dyn_cast<StoreInst>(I)) {
165     if (!AllowVolatile && SI->isVolatile())
166       return nullptr;
167     return SI->getPointerOperand();
168   }
169 
170   if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
171     if (!AllowVolatile && CXI->isVolatile())
172       return nullptr;
173     return CXI->getPointerOperand();
174   }
175 
176   if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
177     if (!AllowVolatile && RMWI->isVolatile())
178       return nullptr;
179     return RMWI->getPointerOperand();
180   }
181 
182   return nullptr;
183 }
184 
185 /// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
186 /// advanced by \p Offset bytes. To aid later analysis the method tries to build
187 /// getelement pointer instructions that traverse the natural type of \p Ptr if
188 /// possible. If that fails, the remaining offset is adjusted byte-wise, hence
189 /// through a cast to i8*.
190 ///
191 /// TODO: This could probably live somewhere more prominantly if it doesn't
192 ///       already exist.
193 static Value *constructPointer(Type *ResTy, Type *PtrElemTy, Value *Ptr,
194                                int64_t Offset, IRBuilder<NoFolder> &IRB,
195                                const DataLayout &DL) {
196   assert(Offset >= 0 && "Negative offset not supported yet!");
197   LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset
198                     << "-bytes as " << *ResTy << "\n");
199 
200   if (Offset) {
201     SmallVector<Value *, 4> Indices;
202     std::string GEPName = Ptr->getName().str() + ".0";
203 
204     // Add 0 index to look through the pointer.
205     assert((uint64_t)Offset < DL.getTypeAllocSize(PtrElemTy) &&
206            "Offset out of bounds");
207     Indices.push_back(Constant::getNullValue(IRB.getInt32Ty()));
208 
209     Type *Ty = PtrElemTy;
210     do {
211       auto *STy = dyn_cast<StructType>(Ty);
212       if (!STy)
213         // Non-aggregate type, we cast and make byte-wise progress now.
214         break;
215 
216       const StructLayout *SL = DL.getStructLayout(STy);
217       if (int64_t(SL->getSizeInBytes()) < Offset)
218         break;
219 
220       uint64_t Idx = SL->getElementContainingOffset(Offset);
221       assert(Idx < STy->getNumElements() && "Offset calculation error!");
222       uint64_t Rem = Offset - SL->getElementOffset(Idx);
223       Ty = STy->getElementType(Idx);
224 
225       LLVM_DEBUG(errs() << "Ty: " << *Ty << " Offset: " << Offset
226                         << " Idx: " << Idx << " Rem: " << Rem << "\n");
227 
228       GEPName += "." + std::to_string(Idx);
229       Indices.push_back(ConstantInt::get(IRB.getInt32Ty(), Idx));
230       Offset = Rem;
231     } while (Offset);
232 
233     // Create a GEP for the indices collected above.
234     Ptr = IRB.CreateGEP(PtrElemTy, Ptr, Indices, GEPName);
235 
236     // If an offset is left we use byte-wise adjustment.
237     if (Offset) {
238       Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
239       Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt32(Offset),
240                           GEPName + ".b" + Twine(Offset));
241     }
242   }
243 
244   // Ensure the result has the requested type.
245   Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast");
246 
247   LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n");
248   return Ptr;
249 }
250 
251 /// Recursively visit all values that might become \p IRP at some point. This
252 /// will be done by looking through cast instructions, selects, phis, and calls
253 /// with the "returned" attribute. Once we cannot look through the value any
254 /// further, the callback \p VisitValueCB is invoked and passed the current
255 /// value, the \p State, and a flag to indicate if we stripped anything.
256 /// Stripped means that we unpacked the value associated with \p IRP at least
257 /// once. Note that the value used for the callback may still be the value
258 /// associated with \p IRP (due to PHIs). To limit how much effort is invested,
259 /// we will never visit more values than specified by \p MaxValues.
260 template <typename AAType, typename StateTy>
261 static bool genericValueTraversal(
262     Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State,
263     function_ref<bool(Value &, const Instruction *, StateTy &, bool)>
264         VisitValueCB,
265     const Instruction *CtxI, bool UseValueSimplify = true, int MaxValues = 16,
266     function_ref<Value *(Value *)> StripCB = nullptr) {
267 
268   const AAIsDead *LivenessAA = nullptr;
269   if (IRP.getAnchorScope())
270     LivenessAA = &A.getAAFor<AAIsDead>(
271         QueryingAA, IRPosition::function(*IRP.getAnchorScope()),
272         DepClassTy::NONE);
273   bool AnyDead = false;
274 
275   using Item = std::pair<Value *, const Instruction *>;
276   SmallSet<Item, 16> Visited;
277   SmallVector<Item, 16> Worklist;
278   Worklist.push_back({&IRP.getAssociatedValue(), CtxI});
279 
280   int Iteration = 0;
281   do {
282     Item I = Worklist.pop_back_val();
283     Value *V = I.first;
284     CtxI = I.second;
285     if (StripCB)
286       V = StripCB(V);
287 
288     // Check if we should process the current value. To prevent endless
289     // recursion keep a record of the values we followed!
290     if (!Visited.insert(I).second)
291       continue;
292 
293     // Make sure we limit the compile time for complex expressions.
294     if (Iteration++ >= MaxValues)
295       return false;
296 
297     // Explicitly look through calls with a "returned" attribute if we do
298     // not have a pointer as stripPointerCasts only works on them.
299     Value *NewV = nullptr;
300     if (V->getType()->isPointerTy()) {
301       NewV = V->stripPointerCasts();
302     } else {
303       auto *CB = dyn_cast<CallBase>(V);
304       if (CB && CB->getCalledFunction()) {
305         for (Argument &Arg : CB->getCalledFunction()->args())
306           if (Arg.hasReturnedAttr()) {
307             NewV = CB->getArgOperand(Arg.getArgNo());
308             break;
309           }
310       }
311     }
312     if (NewV && NewV != V) {
313       Worklist.push_back({NewV, CtxI});
314       continue;
315     }
316 
317     // Look through select instructions, visit both potential values.
318     if (auto *SI = dyn_cast<SelectInst>(V)) {
319       Worklist.push_back({SI->getTrueValue(), CtxI});
320       Worklist.push_back({SI->getFalseValue(), CtxI});
321       continue;
322     }
323 
324     // Look through phi nodes, visit all live operands.
325     if (auto *PHI = dyn_cast<PHINode>(V)) {
326       assert(LivenessAA &&
327              "Expected liveness in the presence of instructions!");
328       for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
329         BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
330         if (A.isAssumedDead(*IncomingBB->getTerminator(), &QueryingAA,
331                             LivenessAA,
332                             /* CheckBBLivenessOnly */ true)) {
333           AnyDead = true;
334           continue;
335         }
336         Worklist.push_back(
337             {PHI->getIncomingValue(u), IncomingBB->getTerminator()});
338       }
339       continue;
340     }
341 
342     if (UseValueSimplify && !isa<Constant>(V)) {
343       bool UsedAssumedInformation = false;
344       Optional<Constant *> C =
345           A.getAssumedConstant(*V, QueryingAA, UsedAssumedInformation);
346       if (!C.hasValue())
347         continue;
348       if (Value *NewV = C.getValue()) {
349         Worklist.push_back({NewV, CtxI});
350         continue;
351       }
352     }
353 
354     // Once a leaf is reached we inform the user through the callback.
355     if (!VisitValueCB(*V, CtxI, State, Iteration > 1))
356       return false;
357   } while (!Worklist.empty());
358 
359   // If we actually used liveness information so we have to record a dependence.
360   if (AnyDead)
361     A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
362 
363   // All values have been visited.
364   return true;
365 }
366 
367 const Value *stripAndAccumulateMinimalOffsets(
368     Attributor &A, const AbstractAttribute &QueryingAA, const Value *Val,
369     const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
370     bool UseAssumed = false) {
371 
372   auto AttributorAnalysis = [&](Value &V, APInt &ROffset) -> bool {
373     const IRPosition &Pos = IRPosition::value(V);
374     // Only track dependence if we are going to use the assumed info.
375     const AAValueConstantRange &ValueConstantRangeAA =
376         A.getAAFor<AAValueConstantRange>(QueryingAA, Pos,
377                                          UseAssumed ? DepClassTy::OPTIONAL
378                                                     : DepClassTy::NONE);
379     ConstantRange Range = UseAssumed ? ValueConstantRangeAA.getAssumed()
380                                      : ValueConstantRangeAA.getKnown();
381     // We can only use the lower part of the range because the upper part can
382     // be higher than what the value can really be.
383     ROffset = Range.getSignedMin();
384     return true;
385   };
386 
387   return Val->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds,
388                                                 AttributorAnalysis);
389 }
390 
391 static const Value *getMinimalBaseOfAccsesPointerOperand(
392     Attributor &A, const AbstractAttribute &QueryingAA, const Instruction *I,
393     int64_t &BytesOffset, const DataLayout &DL, bool AllowNonInbounds = false) {
394   const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false);
395   if (!Ptr)
396     return nullptr;
397   APInt OffsetAPInt(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
398   const Value *Base = stripAndAccumulateMinimalOffsets(
399       A, QueryingAA, Ptr, DL, OffsetAPInt, AllowNonInbounds);
400 
401   BytesOffset = OffsetAPInt.getSExtValue();
402   return Base;
403 }
404 
405 static const Value *
406 getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset,
407                                      const DataLayout &DL,
408                                      bool AllowNonInbounds = false) {
409   const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false);
410   if (!Ptr)
411     return nullptr;
412 
413   return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL,
414                                           AllowNonInbounds);
415 }
416 
417 /// Helper function to clamp a state \p S of type \p StateType with the
418 /// information in \p R and indicate/return if \p S did change (as-in update is
419 /// required to be run again).
420 template <typename StateType>
421 ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
422   auto Assumed = S.getAssumed();
423   S ^= R;
424   return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
425                                    : ChangeStatus::CHANGED;
426 }
427 
428 /// Clamp the information known for all returned values of a function
429 /// (identified by \p QueryingAA) into \p S.
430 template <typename AAType, typename StateType = typename AAType::StateType>
431 static void clampReturnedValueStates(
432     Attributor &A, const AAType &QueryingAA, StateType &S,
433     const IRPosition::CallBaseContext *CBContext = nullptr) {
434   LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
435                     << QueryingAA << " into " << S << "\n");
436 
437   assert((QueryingAA.getIRPosition().getPositionKind() ==
438               IRPosition::IRP_RETURNED ||
439           QueryingAA.getIRPosition().getPositionKind() ==
440               IRPosition::IRP_CALL_SITE_RETURNED) &&
441          "Can only clamp returned value states for a function returned or call "
442          "site returned position!");
443 
444   // Use an optional state as there might not be any return values and we want
445   // to join (IntegerState::operator&) the state of all there are.
446   Optional<StateType> T;
447 
448   // Callback for each possibly returned value.
449   auto CheckReturnValue = [&](Value &RV) -> bool {
450     const IRPosition &RVPos = IRPosition::value(RV, CBContext);
451     const AAType &AA =
452         A.getAAFor<AAType>(QueryingAA, RVPos, DepClassTy::REQUIRED);
453     LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
454                       << " @ " << RVPos << "\n");
455     const StateType &AAS = AA.getState();
456     if (T.hasValue())
457       *T &= AAS;
458     else
459       T = AAS;
460     LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
461                       << "\n");
462     return T->isValidState();
463   };
464 
465   if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
466     S.indicatePessimisticFixpoint();
467   else if (T.hasValue())
468     S ^= *T;
469 }
470 
471 /// Helper class for generic deduction: return value -> returned position.
472 template <typename AAType, typename BaseType,
473           typename StateType = typename BaseType::StateType,
474           bool PropagateCallBaseContext = false>
475 struct AAReturnedFromReturnedValues : public BaseType {
476   AAReturnedFromReturnedValues(const IRPosition &IRP, Attributor &A)
477       : BaseType(IRP, A) {}
478 
479   /// See AbstractAttribute::updateImpl(...).
480   ChangeStatus updateImpl(Attributor &A) override {
481     StateType S(StateType::getBestState(this->getState()));
482     clampReturnedValueStates<AAType, StateType>(
483         A, *this, S,
484         PropagateCallBaseContext ? this->getCallBaseContext() : nullptr);
485     // TODO: If we know we visited all returned values, thus no are assumed
486     // dead, we can take the known information from the state T.
487     return clampStateAndIndicateChange<StateType>(this->getState(), S);
488   }
489 };
490 
491 /// Clamp the information known at all call sites for a given argument
492 /// (identified by \p QueryingAA) into \p S.
493 template <typename AAType, typename StateType = typename AAType::StateType>
494 static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
495                                         StateType &S) {
496   LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
497                     << QueryingAA << " into " << S << "\n");
498 
499   assert(QueryingAA.getIRPosition().getPositionKind() ==
500              IRPosition::IRP_ARGUMENT &&
501          "Can only clamp call site argument states for an argument position!");
502 
503   // Use an optional state as there might not be any return values and we want
504   // to join (IntegerState::operator&) the state of all there are.
505   Optional<StateType> T;
506 
507   // The argument number which is also the call site argument number.
508   unsigned ArgNo = QueryingAA.getIRPosition().getCallSiteArgNo();
509 
510   auto CallSiteCheck = [&](AbstractCallSite ACS) {
511     const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
512     // Check if a coresponding argument was found or if it is on not associated
513     // (which can happen for callback calls).
514     if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
515       return false;
516 
517     const AAType &AA =
518         A.getAAFor<AAType>(QueryingAA, ACSArgPos, DepClassTy::REQUIRED);
519     LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()
520                       << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n");
521     const StateType &AAS = AA.getState();
522     if (T.hasValue())
523       *T &= AAS;
524     else
525       T = AAS;
526     LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
527                       << "\n");
528     return T->isValidState();
529   };
530 
531   bool AllCallSitesKnown;
532   if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
533                               AllCallSitesKnown))
534     S.indicatePessimisticFixpoint();
535   else if (T.hasValue())
536     S ^= *T;
537 }
538 
539 /// This function is the bridge between argument position and the call base
540 /// context.
541 template <typename AAType, typename BaseType,
542           typename StateType = typename AAType::StateType>
543 bool getArgumentStateFromCallBaseContext(Attributor &A,
544                                          BaseType &QueryingAttribute,
545                                          IRPosition &Pos, StateType &State) {
546   assert((Pos.getPositionKind() == IRPosition::IRP_ARGUMENT) &&
547          "Expected an 'argument' position !");
548   const CallBase *CBContext = Pos.getCallBaseContext();
549   if (!CBContext)
550     return false;
551 
552   int ArgNo = Pos.getCallSiteArgNo();
553   assert(ArgNo >= 0 && "Invalid Arg No!");
554 
555   const auto &AA = A.getAAFor<AAType>(
556       QueryingAttribute, IRPosition::callsite_argument(*CBContext, ArgNo),
557       DepClassTy::REQUIRED);
558   const StateType &CBArgumentState =
559       static_cast<const StateType &>(AA.getState());
560 
561   LLVM_DEBUG(dbgs() << "[Attributor] Briding Call site context to argument"
562                     << "Position:" << Pos << "CB Arg state:" << CBArgumentState
563                     << "\n");
564 
565   // NOTE: If we want to do call site grouping it should happen here.
566   State ^= CBArgumentState;
567   return true;
568 }
569 
570 /// Helper class for generic deduction: call site argument -> argument position.
571 template <typename AAType, typename BaseType,
572           typename StateType = typename AAType::StateType,
573           bool BridgeCallBaseContext = false>
574 struct AAArgumentFromCallSiteArguments : public BaseType {
575   AAArgumentFromCallSiteArguments(const IRPosition &IRP, Attributor &A)
576       : BaseType(IRP, A) {}
577 
578   /// See AbstractAttribute::updateImpl(...).
579   ChangeStatus updateImpl(Attributor &A) override {
580     StateType S = StateType::getBestState(this->getState());
581 
582     if (BridgeCallBaseContext) {
583       bool Success =
584           getArgumentStateFromCallBaseContext<AAType, BaseType, StateType>(
585               A, *this, this->getIRPosition(), S);
586       if (Success)
587         return clampStateAndIndicateChange<StateType>(this->getState(), S);
588     }
589     clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
590 
591     // TODO: If we know we visited all incoming values, thus no are assumed
592     // dead, we can take the known information from the state T.
593     return clampStateAndIndicateChange<StateType>(this->getState(), S);
594   }
595 };
596 
597 /// Helper class for generic replication: function returned -> cs returned.
598 template <typename AAType, typename BaseType,
599           typename StateType = typename BaseType::StateType,
600           bool IntroduceCallBaseContext = false>
601 struct AACallSiteReturnedFromReturned : public BaseType {
602   AACallSiteReturnedFromReturned(const IRPosition &IRP, Attributor &A)
603       : BaseType(IRP, A) {}
604 
605   /// See AbstractAttribute::updateImpl(...).
606   ChangeStatus updateImpl(Attributor &A) override {
607     assert(this->getIRPosition().getPositionKind() ==
608                IRPosition::IRP_CALL_SITE_RETURNED &&
609            "Can only wrap function returned positions for call site returned "
610            "positions!");
611     auto &S = this->getState();
612 
613     const Function *AssociatedFunction =
614         this->getIRPosition().getAssociatedFunction();
615     if (!AssociatedFunction)
616       return S.indicatePessimisticFixpoint();
617 
618     CallBase &CBContext = static_cast<CallBase &>(this->getAnchorValue());
619     if (IntroduceCallBaseContext)
620       LLVM_DEBUG(dbgs() << "[Attributor] Introducing call base context:"
621                         << CBContext << "\n");
622 
623     IRPosition FnPos = IRPosition::returned(
624         *AssociatedFunction, IntroduceCallBaseContext ? &CBContext : nullptr);
625     const AAType &AA = A.getAAFor<AAType>(*this, FnPos, DepClassTy::REQUIRED);
626     return clampStateAndIndicateChange(S, AA.getState());
627   }
628 };
629 
630 /// Helper function to accumulate uses.
631 template <class AAType, typename StateType = typename AAType::StateType>
632 static void followUsesInContext(AAType &AA, Attributor &A,
633                                 MustBeExecutedContextExplorer &Explorer,
634                                 const Instruction *CtxI,
635                                 SetVector<const Use *> &Uses,
636                                 StateType &State) {
637   auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
638   for (unsigned u = 0; u < Uses.size(); ++u) {
639     const Use *U = Uses[u];
640     if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
641       bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
642       if (Found && AA.followUseInMBEC(A, U, UserI, State))
643         for (const Use &Us : UserI->uses())
644           Uses.insert(&Us);
645     }
646   }
647 }
648 
649 /// Use the must-be-executed-context around \p I to add information into \p S.
650 /// The AAType class is required to have `followUseInMBEC` method with the
651 /// following signature and behaviour:
652 ///
653 /// bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I)
654 /// U - Underlying use.
655 /// I - The user of the \p U.
656 /// Returns true if the value should be tracked transitively.
657 ///
658 template <class AAType, typename StateType = typename AAType::StateType>
659 static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S,
660                              Instruction &CtxI) {
661 
662   // Container for (transitive) uses of the associated value.
663   SetVector<const Use *> Uses;
664   for (const Use &U : AA.getIRPosition().getAssociatedValue().uses())
665     Uses.insert(&U);
666 
667   MustBeExecutedContextExplorer &Explorer =
668       A.getInfoCache().getMustBeExecutedContextExplorer();
669 
670   followUsesInContext<AAType>(AA, A, Explorer, &CtxI, Uses, S);
671 
672   if (S.isAtFixpoint())
673     return;
674 
675   SmallVector<const BranchInst *, 4> BrInsts;
676   auto Pred = [&](const Instruction *I) {
677     if (const BranchInst *Br = dyn_cast<BranchInst>(I))
678       if (Br->isConditional())
679         BrInsts.push_back(Br);
680     return true;
681   };
682 
683   // Here, accumulate conditional branch instructions in the context. We
684   // explore the child paths and collect the known states. The disjunction of
685   // those states can be merged to its own state. Let ParentState_i be a state
686   // to indicate the known information for an i-th branch instruction in the
687   // context. ChildStates are created for its successors respectively.
688   //
689   // ParentS_1 = ChildS_{1, 1} /\ ChildS_{1, 2} /\ ... /\ ChildS_{1, n_1}
690   // ParentS_2 = ChildS_{2, 1} /\ ChildS_{2, 2} /\ ... /\ ChildS_{2, n_2}
691   //      ...
692   // ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m}
693   //
694   // Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m
695   //
696   // FIXME: Currently, recursive branches are not handled. For example, we
697   // can't deduce that ptr must be dereferenced in below function.
698   //
699   // void f(int a, int c, int *ptr) {
700   //    if(a)
701   //      if (b) {
702   //        *ptr = 0;
703   //      } else {
704   //        *ptr = 1;
705   //      }
706   //    else {
707   //      if (b) {
708   //        *ptr = 0;
709   //      } else {
710   //        *ptr = 1;
711   //      }
712   //    }
713   // }
714 
715   Explorer.checkForAllContext(&CtxI, Pred);
716   for (const BranchInst *Br : BrInsts) {
717     StateType ParentState;
718 
719     // The known state of the parent state is a conjunction of children's
720     // known states so it is initialized with a best state.
721     ParentState.indicateOptimisticFixpoint();
722 
723     for (const BasicBlock *BB : Br->successors()) {
724       StateType ChildState;
725 
726       size_t BeforeSize = Uses.size();
727       followUsesInContext(AA, A, Explorer, &BB->front(), Uses, ChildState);
728 
729       // Erase uses which only appear in the child.
730       for (auto It = Uses.begin() + BeforeSize; It != Uses.end();)
731         It = Uses.erase(It);
732 
733       ParentState &= ChildState;
734     }
735 
736     // Use only known state.
737     S += ParentState;
738   }
739 }
740 
741 /// -----------------------NoUnwind Function Attribute--------------------------
742 
743 struct AANoUnwindImpl : AANoUnwind {
744   AANoUnwindImpl(const IRPosition &IRP, Attributor &A) : AANoUnwind(IRP, A) {}
745 
746   const std::string getAsStr() const override {
747     return getAssumed() ? "nounwind" : "may-unwind";
748   }
749 
750   /// See AbstractAttribute::updateImpl(...).
751   ChangeStatus updateImpl(Attributor &A) override {
752     auto Opcodes = {
753         (unsigned)Instruction::Invoke,      (unsigned)Instruction::CallBr,
754         (unsigned)Instruction::Call,        (unsigned)Instruction::CleanupRet,
755         (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
756 
757     auto CheckForNoUnwind = [&](Instruction &I) {
758       if (!I.mayThrow())
759         return true;
760 
761       if (const auto *CB = dyn_cast<CallBase>(&I)) {
762         const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
763             *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
764         return NoUnwindAA.isAssumedNoUnwind();
765       }
766       return false;
767     };
768 
769     if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes))
770       return indicatePessimisticFixpoint();
771 
772     return ChangeStatus::UNCHANGED;
773   }
774 };
775 
776 struct AANoUnwindFunction final : public AANoUnwindImpl {
777   AANoUnwindFunction(const IRPosition &IRP, Attributor &A)
778       : AANoUnwindImpl(IRP, A) {}
779 
780   /// See AbstractAttribute::trackStatistics()
781   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
782 };
783 
784 /// NoUnwind attribute deduction for a call sites.
785 struct AANoUnwindCallSite final : AANoUnwindImpl {
786   AANoUnwindCallSite(const IRPosition &IRP, Attributor &A)
787       : AANoUnwindImpl(IRP, A) {}
788 
789   /// See AbstractAttribute::initialize(...).
790   void initialize(Attributor &A) override {
791     AANoUnwindImpl::initialize(A);
792     Function *F = getAssociatedFunction();
793     if (!F || F->isDeclaration())
794       indicatePessimisticFixpoint();
795   }
796 
797   /// See AbstractAttribute::updateImpl(...).
798   ChangeStatus updateImpl(Attributor &A) override {
799     // TODO: Once we have call site specific value information we can provide
800     //       call site specific liveness information and then it makes
801     //       sense to specialize attributes for call sites arguments instead of
802     //       redirecting requests to the callee argument.
803     Function *F = getAssociatedFunction();
804     const IRPosition &FnPos = IRPosition::function(*F);
805     auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::REQUIRED);
806     return clampStateAndIndicateChange(getState(), FnAA.getState());
807   }
808 
809   /// See AbstractAttribute::trackStatistics()
810   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
811 };
812 
813 /// --------------------- Function Return Values -------------------------------
814 
815 /// "Attribute" that collects all potential returned values and the return
816 /// instructions that they arise from.
817 ///
818 /// If there is a unique returned value R, the manifest method will:
819 ///   - mark R with the "returned" attribute, if R is an argument.
820 class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
821 
822   /// Mapping of values potentially returned by the associated function to the
823   /// return instructions that might return them.
824   MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
825 
826   /// Mapping to remember the number of returned values for a call site such
827   /// that we can avoid updates if nothing changed.
828   DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA;
829 
830   /// Set of unresolved calls returned by the associated function.
831   SmallSetVector<CallBase *, 4> UnresolvedCalls;
832 
833   /// State flags
834   ///
835   ///{
836   bool IsFixed = false;
837   bool IsValidState = true;
838   ///}
839 
840 public:
841   AAReturnedValuesImpl(const IRPosition &IRP, Attributor &A)
842       : AAReturnedValues(IRP, A) {}
843 
844   /// See AbstractAttribute::initialize(...).
845   void initialize(Attributor &A) override {
846     // Reset the state.
847     IsFixed = false;
848     IsValidState = true;
849     ReturnedValues.clear();
850 
851     Function *F = getAssociatedFunction();
852     if (!F || F->isDeclaration()) {
853       indicatePessimisticFixpoint();
854       return;
855     }
856     assert(!F->getReturnType()->isVoidTy() &&
857            "Did not expect a void return type!");
858 
859     // The map from instruction opcodes to those instructions in the function.
860     auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
861 
862     // Look through all arguments, if one is marked as returned we are done.
863     for (Argument &Arg : F->args()) {
864       if (Arg.hasReturnedAttr()) {
865         auto &ReturnInstSet = ReturnedValues[&Arg];
866         if (auto *Insts = OpcodeInstMap.lookup(Instruction::Ret))
867           for (Instruction *RI : *Insts)
868             ReturnInstSet.insert(cast<ReturnInst>(RI));
869 
870         indicateOptimisticFixpoint();
871         return;
872       }
873     }
874 
875     if (!A.isFunctionIPOAmendable(*F))
876       indicatePessimisticFixpoint();
877   }
878 
879   /// See AbstractAttribute::manifest(...).
880   ChangeStatus manifest(Attributor &A) override;
881 
882   /// See AbstractAttribute::getState(...).
883   AbstractState &getState() override { return *this; }
884 
885   /// See AbstractAttribute::getState(...).
886   const AbstractState &getState() const override { return *this; }
887 
888   /// See AbstractAttribute::updateImpl(Attributor &A).
889   ChangeStatus updateImpl(Attributor &A) override;
890 
891   llvm::iterator_range<iterator> returned_values() override {
892     return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
893   }
894 
895   llvm::iterator_range<const_iterator> returned_values() const override {
896     return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
897   }
898 
899   const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override {
900     return UnresolvedCalls;
901   }
902 
903   /// Return the number of potential return values, -1 if unknown.
904   size_t getNumReturnValues() const override {
905     return isValidState() ? ReturnedValues.size() : -1;
906   }
907 
908   /// Return an assumed unique return value if a single candidate is found. If
909   /// there cannot be one, return a nullptr. If it is not clear yet, return the
910   /// Optional::NoneType.
911   Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
912 
913   /// See AbstractState::checkForAllReturnedValues(...).
914   bool checkForAllReturnedValuesAndReturnInsts(
915       function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
916       const override;
917 
918   /// Pretty print the attribute similar to the IR representation.
919   const std::string getAsStr() const override;
920 
921   /// See AbstractState::isAtFixpoint().
922   bool isAtFixpoint() const override { return IsFixed; }
923 
924   /// See AbstractState::isValidState().
925   bool isValidState() const override { return IsValidState; }
926 
927   /// See AbstractState::indicateOptimisticFixpoint(...).
928   ChangeStatus indicateOptimisticFixpoint() override {
929     IsFixed = true;
930     return ChangeStatus::UNCHANGED;
931   }
932 
933   ChangeStatus indicatePessimisticFixpoint() override {
934     IsFixed = true;
935     IsValidState = false;
936     return ChangeStatus::CHANGED;
937   }
938 };
939 
940 ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
941   ChangeStatus Changed = ChangeStatus::UNCHANGED;
942 
943   // Bookkeeping.
944   assert(isValidState());
945   STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
946                   "Number of function with known return values");
947 
948   // Check if we have an assumed unique return value that we could manifest.
949   Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
950 
951   if (!UniqueRV.hasValue() || !UniqueRV.getValue())
952     return Changed;
953 
954   // Bookkeeping.
955   STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
956                   "Number of function with unique return");
957 
958   // Callback to replace the uses of CB with the constant C.
959   auto ReplaceCallSiteUsersWith = [&A](CallBase &CB, Constant &C) {
960     if (CB.use_empty())
961       return ChangeStatus::UNCHANGED;
962     if (A.changeValueAfterManifest(CB, C))
963       return ChangeStatus::CHANGED;
964     return ChangeStatus::UNCHANGED;
965   };
966 
967   // If the assumed unique return value is an argument, annotate it.
968   if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
969     if (UniqueRVArg->getType()->canLosslesslyBitCastTo(
970             getAssociatedFunction()->getReturnType())) {
971       getIRPosition() = IRPosition::argument(*UniqueRVArg);
972       Changed = IRAttribute::manifest(A);
973     }
974   } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) {
975     // We can replace the returned value with the unique returned constant.
976     Value &AnchorValue = getAnchorValue();
977     if (Function *F = dyn_cast<Function>(&AnchorValue)) {
978       for (const Use &U : F->uses())
979         if (CallBase *CB = dyn_cast<CallBase>(U.getUser()))
980           if (CB->isCallee(&U)) {
981             Constant *RVCCast =
982                 CB->getType() == RVC->getType()
983                     ? RVC
984                     : ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
985             Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed;
986           }
987     } else {
988       assert(isa<CallBase>(AnchorValue) &&
989              "Expcected a function or call base anchor!");
990       Constant *RVCCast =
991           AnchorValue.getType() == RVC->getType()
992               ? RVC
993               : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
994       Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast);
995     }
996     if (Changed == ChangeStatus::CHANGED)
997       STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn,
998                       "Number of function returns replaced by constant return");
999   }
1000 
1001   return Changed;
1002 }
1003 
1004 const std::string AAReturnedValuesImpl::getAsStr() const {
1005   return (isAtFixpoint() ? "returns(#" : "may-return(#") +
1006          (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
1007          ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]";
1008 }
1009 
1010 Optional<Value *>
1011 AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
1012   // If checkForAllReturnedValues provides a unique value, ignoring potential
1013   // undef values that can also be present, it is assumed to be the actual
1014   // return value and forwarded to the caller of this method. If there are
1015   // multiple, a nullptr is returned indicating there cannot be a unique
1016   // returned value.
1017   Optional<Value *> UniqueRV;
1018 
1019   auto Pred = [&](Value &RV) -> bool {
1020     // If we found a second returned value and neither the current nor the saved
1021     // one is an undef, there is no unique returned value. Undefs are special
1022     // since we can pretend they have any value.
1023     if (UniqueRV.hasValue() && UniqueRV != &RV &&
1024         !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
1025       UniqueRV = nullptr;
1026       return false;
1027     }
1028 
1029     // Do not overwrite a value with an undef.
1030     if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
1031       UniqueRV = &RV;
1032 
1033     return true;
1034   };
1035 
1036   if (!A.checkForAllReturnedValues(Pred, *this))
1037     UniqueRV = nullptr;
1038 
1039   return UniqueRV;
1040 }
1041 
1042 bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
1043     function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
1044     const {
1045   if (!isValidState())
1046     return false;
1047 
1048   // Check all returned values but ignore call sites as long as we have not
1049   // encountered an overdefined one during an update.
1050   for (auto &It : ReturnedValues) {
1051     Value *RV = It.first;
1052 
1053     CallBase *CB = dyn_cast<CallBase>(RV);
1054     if (CB && !UnresolvedCalls.count(CB))
1055       continue;
1056 
1057     if (!Pred(*RV, It.second))
1058       return false;
1059   }
1060 
1061   return true;
1062 }
1063 
1064 ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
1065   size_t NumUnresolvedCalls = UnresolvedCalls.size();
1066   bool Changed = false;
1067 
1068   // State used in the value traversals starting in returned values.
1069   struct RVState {
1070     // The map in which we collect return values -> return instrs.
1071     decltype(ReturnedValues) &RetValsMap;
1072     // The flag to indicate a change.
1073     bool &Changed;
1074     // The return instrs we come from.
1075     SmallSetVector<ReturnInst *, 4> RetInsts;
1076   };
1077 
1078   // Callback for a leaf value returned by the associated function.
1079   auto VisitValueCB = [](Value &Val, const Instruction *, RVState &RVS,
1080                          bool) -> bool {
1081     auto Size = RVS.RetValsMap[&Val].size();
1082     RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end());
1083     bool Inserted = RVS.RetValsMap[&Val].size() != Size;
1084     RVS.Changed |= Inserted;
1085     LLVM_DEBUG({
1086       if (Inserted)
1087         dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val
1088                << " => " << RVS.RetInsts.size() << "\n";
1089     });
1090     return true;
1091   };
1092 
1093   // Helper method to invoke the generic value traversal.
1094   auto VisitReturnedValue = [&](Value &RV, RVState &RVS,
1095                                 const Instruction *CtxI) {
1096     IRPosition RetValPos = IRPosition::value(RV);
1097     return genericValueTraversal<AAReturnedValues, RVState>(
1098         A, RetValPos, *this, RVS, VisitValueCB, CtxI,
1099         /* UseValueSimplify */ false);
1100   };
1101 
1102   // Callback for all "return intructions" live in the associated function.
1103   auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) {
1104     ReturnInst &Ret = cast<ReturnInst>(I);
1105     RVState RVS({ReturnedValues, Changed, {}});
1106     RVS.RetInsts.insert(&Ret);
1107     return VisitReturnedValue(*Ret.getReturnValue(), RVS, &I);
1108   };
1109 
1110   // Start by discovering returned values from all live returned instructions in
1111   // the associated function.
1112   if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret}))
1113     return indicatePessimisticFixpoint();
1114 
1115   // Once returned values "directly" present in the code are handled we try to
1116   // resolve returned calls. To avoid modifications to the ReturnedValues map
1117   // while we iterate over it we kept record of potential new entries in a copy
1118   // map, NewRVsMap.
1119   decltype(ReturnedValues) NewRVsMap;
1120 
1121   auto HandleReturnValue = [&](Value *RV,
1122                                SmallSetVector<ReturnInst *, 4> &RIs) {
1123     LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *RV << " by #"
1124                       << RIs.size() << " RIs\n");
1125     CallBase *CB = dyn_cast<CallBase>(RV);
1126     if (!CB || UnresolvedCalls.count(CB))
1127       return;
1128 
1129     if (!CB->getCalledFunction()) {
1130       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1131                         << "\n");
1132       UnresolvedCalls.insert(CB);
1133       return;
1134     }
1135 
1136     // TODO: use the function scope once we have call site AAReturnedValues.
1137     const auto &RetValAA = A.getAAFor<AAReturnedValues>(
1138         *this, IRPosition::function(*CB->getCalledFunction()),
1139         DepClassTy::REQUIRED);
1140     LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: "
1141                       << RetValAA << "\n");
1142 
1143     // Skip dead ends, thus if we do not know anything about the returned
1144     // call we mark it as unresolved and it will stay that way.
1145     if (!RetValAA.getState().isValidState()) {
1146       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1147                         << "\n");
1148       UnresolvedCalls.insert(CB);
1149       return;
1150     }
1151 
1152     // Do not try to learn partial information. If the callee has unresolved
1153     // return values we will treat the call as unresolved/opaque.
1154     auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls();
1155     if (!RetValAAUnresolvedCalls.empty()) {
1156       UnresolvedCalls.insert(CB);
1157       return;
1158     }
1159 
1160     // Now check if we can track transitively returned values. If possible, thus
1161     // if all return value can be represented in the current scope, do so.
1162     bool Unresolved = false;
1163     for (auto &RetValAAIt : RetValAA.returned_values()) {
1164       Value *RetVal = RetValAAIt.first;
1165       if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) ||
1166           isa<Constant>(RetVal))
1167         continue;
1168       // Anything that did not fit in the above categories cannot be resolved,
1169       // mark the call as unresolved.
1170       LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value "
1171                            "cannot be translated: "
1172                         << *RetVal << "\n");
1173       UnresolvedCalls.insert(CB);
1174       Unresolved = true;
1175       break;
1176     }
1177 
1178     if (Unresolved)
1179       return;
1180 
1181     // Now track transitively returned values.
1182     unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB];
1183     if (NumRetAA == RetValAA.getNumReturnValues()) {
1184       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not "
1185                            "changed since it was seen last\n");
1186       return;
1187     }
1188     NumRetAA = RetValAA.getNumReturnValues();
1189 
1190     for (auto &RetValAAIt : RetValAA.returned_values()) {
1191       Value *RetVal = RetValAAIt.first;
1192       if (Argument *Arg = dyn_cast<Argument>(RetVal)) {
1193         // Arguments are mapped to call site operands and we begin the traversal
1194         // again.
1195         bool Unused = false;
1196         RVState RVS({NewRVsMap, Unused, RetValAAIt.second});
1197         VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS, CB);
1198         continue;
1199       }
1200       if (isa<CallBase>(RetVal)) {
1201         // Call sites are resolved by the callee attribute over time, no need to
1202         // do anything for us.
1203         continue;
1204       }
1205       if (isa<Constant>(RetVal)) {
1206         // Constants are valid everywhere, we can simply take them.
1207         NewRVsMap[RetVal].insert(RIs.begin(), RIs.end());
1208         continue;
1209       }
1210     }
1211   };
1212 
1213   for (auto &It : ReturnedValues)
1214     HandleReturnValue(It.first, It.second);
1215 
1216   // Because processing the new information can again lead to new return values
1217   // we have to be careful and iterate until this iteration is complete. The
1218   // idea is that we are in a stable state at the end of an update. All return
1219   // values have been handled and properly categorized. We might not update
1220   // again if we have not requested a non-fix attribute so we cannot "wait" for
1221   // the next update to analyze a new return value.
1222   while (!NewRVsMap.empty()) {
1223     auto It = std::move(NewRVsMap.back());
1224     NewRVsMap.pop_back();
1225 
1226     assert(!It.second.empty() && "Entry does not add anything.");
1227     auto &ReturnInsts = ReturnedValues[It.first];
1228     for (ReturnInst *RI : It.second)
1229       if (ReturnInsts.insert(RI)) {
1230         LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
1231                           << *It.first << " => " << *RI << "\n");
1232         HandleReturnValue(It.first, ReturnInsts);
1233         Changed = true;
1234       }
1235   }
1236 
1237   Changed |= (NumUnresolvedCalls != UnresolvedCalls.size());
1238   return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
1239 }
1240 
1241 struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1242   AAReturnedValuesFunction(const IRPosition &IRP, Attributor &A)
1243       : AAReturnedValuesImpl(IRP, A) {}
1244 
1245   /// See AbstractAttribute::trackStatistics()
1246   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
1247 };
1248 
1249 /// Returned values information for a call sites.
1250 struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1251   AAReturnedValuesCallSite(const IRPosition &IRP, Attributor &A)
1252       : AAReturnedValuesImpl(IRP, A) {}
1253 
1254   /// See AbstractAttribute::initialize(...).
1255   void initialize(Attributor &A) override {
1256     // TODO: Once we have call site specific value information we can provide
1257     //       call site specific liveness information and then it makes
1258     //       sense to specialize attributes for call sites instead of
1259     //       redirecting requests to the callee.
1260     llvm_unreachable("Abstract attributes for returned values are not "
1261                      "supported for call sites yet!");
1262   }
1263 
1264   /// See AbstractAttribute::updateImpl(...).
1265   ChangeStatus updateImpl(Attributor &A) override {
1266     return indicatePessimisticFixpoint();
1267   }
1268 
1269   /// See AbstractAttribute::trackStatistics()
1270   void trackStatistics() const override {}
1271 };
1272 
1273 /// ------------------------ NoSync Function Attribute -------------------------
1274 
1275 struct AANoSyncImpl : AANoSync {
1276   AANoSyncImpl(const IRPosition &IRP, Attributor &A) : AANoSync(IRP, A) {}
1277 
1278   const std::string getAsStr() const override {
1279     return getAssumed() ? "nosync" : "may-sync";
1280   }
1281 
1282   /// See AbstractAttribute::updateImpl(...).
1283   ChangeStatus updateImpl(Attributor &A) override;
1284 
1285   /// Helper function used to determine whether an instruction is non-relaxed
1286   /// atomic. In other words, if an atomic instruction does not have unordered
1287   /// or monotonic ordering
1288   static bool isNonRelaxedAtomic(Instruction *I);
1289 
1290   /// Helper function used to determine whether an instruction is volatile.
1291   static bool isVolatile(Instruction *I);
1292 
1293   /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
1294   /// memset).
1295   static bool isNoSyncIntrinsic(Instruction *I);
1296 };
1297 
1298 bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
1299   if (!I->isAtomic())
1300     return false;
1301 
1302   AtomicOrdering Ordering;
1303   switch (I->getOpcode()) {
1304   case Instruction::AtomicRMW:
1305     Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1306     break;
1307   case Instruction::Store:
1308     Ordering = cast<StoreInst>(I)->getOrdering();
1309     break;
1310   case Instruction::Load:
1311     Ordering = cast<LoadInst>(I)->getOrdering();
1312     break;
1313   case Instruction::Fence: {
1314     auto *FI = cast<FenceInst>(I);
1315     if (FI->getSyncScopeID() == SyncScope::SingleThread)
1316       return false;
1317     Ordering = FI->getOrdering();
1318     break;
1319   }
1320   case Instruction::AtomicCmpXchg: {
1321     AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering();
1322     AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering();
1323     // Only if both are relaxed, than it can be treated as relaxed.
1324     // Otherwise it is non-relaxed.
1325     if (Success != AtomicOrdering::Unordered &&
1326         Success != AtomicOrdering::Monotonic)
1327       return true;
1328     if (Failure != AtomicOrdering::Unordered &&
1329         Failure != AtomicOrdering::Monotonic)
1330       return true;
1331     return false;
1332   }
1333   default:
1334     llvm_unreachable(
1335         "New atomic operations need to be known in the attributor.");
1336   }
1337 
1338   // Relaxed.
1339   if (Ordering == AtomicOrdering::Unordered ||
1340       Ordering == AtomicOrdering::Monotonic)
1341     return false;
1342   return true;
1343 }
1344 
1345 /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
1346 /// FIXME: We should ipmrove the handling of intrinsics.
1347 bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
1348   if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1349     switch (II->getIntrinsicID()) {
1350     case Intrinsic::memset:
1351     case Intrinsic::memmove:
1352     case Intrinsic::memcpy:
1353       if (!cast<MemIntrinsic>(II)->isVolatile())
1354         return true;
1355       return false;
1356     default:
1357       return false;
1358     }
1359   }
1360   return false;
1361 }
1362 
1363 bool AANoSyncImpl::isVolatile(Instruction *I) {
1364   assert(!isa<CallBase>(I) && "Calls should not be checked here");
1365 
1366   switch (I->getOpcode()) {
1367   case Instruction::AtomicRMW:
1368     return cast<AtomicRMWInst>(I)->isVolatile();
1369   case Instruction::Store:
1370     return cast<StoreInst>(I)->isVolatile();
1371   case Instruction::Load:
1372     return cast<LoadInst>(I)->isVolatile();
1373   case Instruction::AtomicCmpXchg:
1374     return cast<AtomicCmpXchgInst>(I)->isVolatile();
1375   default:
1376     return false;
1377   }
1378 }
1379 
1380 ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
1381 
1382   auto CheckRWInstForNoSync = [&](Instruction &I) {
1383     /// We are looking for volatile instructions or Non-Relaxed atomics.
1384     /// FIXME: We should improve the handling of intrinsics.
1385 
1386     if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I))
1387       return true;
1388 
1389     if (const auto *CB = dyn_cast<CallBase>(&I)) {
1390       if (CB->hasFnAttr(Attribute::NoSync))
1391         return true;
1392 
1393       const auto &NoSyncAA = A.getAAFor<AANoSync>(
1394           *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
1395       return NoSyncAA.isAssumedNoSync();
1396     }
1397 
1398     if (!isVolatile(&I) && !isNonRelaxedAtomic(&I))
1399       return true;
1400 
1401     return false;
1402   };
1403 
1404   auto CheckForNoSync = [&](Instruction &I) {
1405     // At this point we handled all read/write effects and they are all
1406     // nosync, so they can be skipped.
1407     if (I.mayReadOrWriteMemory())
1408       return true;
1409 
1410     // non-convergent and readnone imply nosync.
1411     return !cast<CallBase>(I).isConvergent();
1412   };
1413 
1414   if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) ||
1415       !A.checkForAllCallLikeInstructions(CheckForNoSync, *this))
1416     return indicatePessimisticFixpoint();
1417 
1418   return ChangeStatus::UNCHANGED;
1419 }
1420 
1421 struct AANoSyncFunction final : public AANoSyncImpl {
1422   AANoSyncFunction(const IRPosition &IRP, Attributor &A)
1423       : AANoSyncImpl(IRP, A) {}
1424 
1425   /// See AbstractAttribute::trackStatistics()
1426   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
1427 };
1428 
1429 /// NoSync attribute deduction for a call sites.
1430 struct AANoSyncCallSite final : AANoSyncImpl {
1431   AANoSyncCallSite(const IRPosition &IRP, Attributor &A)
1432       : AANoSyncImpl(IRP, A) {}
1433 
1434   /// See AbstractAttribute::initialize(...).
1435   void initialize(Attributor &A) override {
1436     AANoSyncImpl::initialize(A);
1437     Function *F = getAssociatedFunction();
1438     if (!F || F->isDeclaration())
1439       indicatePessimisticFixpoint();
1440   }
1441 
1442   /// See AbstractAttribute::updateImpl(...).
1443   ChangeStatus updateImpl(Attributor &A) override {
1444     // TODO: Once we have call site specific value information we can provide
1445     //       call site specific liveness information and then it makes
1446     //       sense to specialize attributes for call sites arguments instead of
1447     //       redirecting requests to the callee argument.
1448     Function *F = getAssociatedFunction();
1449     const IRPosition &FnPos = IRPosition::function(*F);
1450     auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos, DepClassTy::REQUIRED);
1451     return clampStateAndIndicateChange(getState(), FnAA.getState());
1452   }
1453 
1454   /// See AbstractAttribute::trackStatistics()
1455   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
1456 };
1457 
1458 /// ------------------------ No-Free Attributes ----------------------------
1459 
1460 struct AANoFreeImpl : public AANoFree {
1461   AANoFreeImpl(const IRPosition &IRP, Attributor &A) : AANoFree(IRP, A) {}
1462 
1463   /// See AbstractAttribute::updateImpl(...).
1464   ChangeStatus updateImpl(Attributor &A) override {
1465     auto CheckForNoFree = [&](Instruction &I) {
1466       const auto &CB = cast<CallBase>(I);
1467       if (CB.hasFnAttr(Attribute::NoFree))
1468         return true;
1469 
1470       const auto &NoFreeAA = A.getAAFor<AANoFree>(
1471           *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
1472       return NoFreeAA.isAssumedNoFree();
1473     };
1474 
1475     if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this))
1476       return indicatePessimisticFixpoint();
1477     return ChangeStatus::UNCHANGED;
1478   }
1479 
1480   /// See AbstractAttribute::getAsStr().
1481   const std::string getAsStr() const override {
1482     return getAssumed() ? "nofree" : "may-free";
1483   }
1484 };
1485 
1486 struct AANoFreeFunction final : public AANoFreeImpl {
1487   AANoFreeFunction(const IRPosition &IRP, Attributor &A)
1488       : AANoFreeImpl(IRP, A) {}
1489 
1490   /// See AbstractAttribute::trackStatistics()
1491   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
1492 };
1493 
1494 /// NoFree attribute deduction for a call sites.
1495 struct AANoFreeCallSite final : AANoFreeImpl {
1496   AANoFreeCallSite(const IRPosition &IRP, Attributor &A)
1497       : AANoFreeImpl(IRP, A) {}
1498 
1499   /// See AbstractAttribute::initialize(...).
1500   void initialize(Attributor &A) override {
1501     AANoFreeImpl::initialize(A);
1502     Function *F = getAssociatedFunction();
1503     if (!F || F->isDeclaration())
1504       indicatePessimisticFixpoint();
1505   }
1506 
1507   /// See AbstractAttribute::updateImpl(...).
1508   ChangeStatus updateImpl(Attributor &A) override {
1509     // TODO: Once we have call site specific value information we can provide
1510     //       call site specific liveness information and then it makes
1511     //       sense to specialize attributes for call sites arguments instead of
1512     //       redirecting requests to the callee argument.
1513     Function *F = getAssociatedFunction();
1514     const IRPosition &FnPos = IRPosition::function(*F);
1515     auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos, DepClassTy::REQUIRED);
1516     return clampStateAndIndicateChange(getState(), FnAA.getState());
1517   }
1518 
1519   /// See AbstractAttribute::trackStatistics()
1520   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
1521 };
1522 
1523 /// NoFree attribute for floating values.
1524 struct AANoFreeFloating : AANoFreeImpl {
1525   AANoFreeFloating(const IRPosition &IRP, Attributor &A)
1526       : AANoFreeImpl(IRP, A) {}
1527 
1528   /// See AbstractAttribute::trackStatistics()
1529   void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)}
1530 
1531   /// See Abstract Attribute::updateImpl(...).
1532   ChangeStatus updateImpl(Attributor &A) override {
1533     const IRPosition &IRP = getIRPosition();
1534 
1535     const auto &NoFreeAA = A.getAAFor<AANoFree>(
1536         *this, IRPosition::function_scope(IRP), DepClassTy::OPTIONAL);
1537     if (NoFreeAA.isAssumedNoFree())
1538       return ChangeStatus::UNCHANGED;
1539 
1540     Value &AssociatedValue = getIRPosition().getAssociatedValue();
1541     auto Pred = [&](const Use &U, bool &Follow) -> bool {
1542       Instruction *UserI = cast<Instruction>(U.getUser());
1543       if (auto *CB = dyn_cast<CallBase>(UserI)) {
1544         if (CB->isBundleOperand(&U))
1545           return false;
1546         if (!CB->isArgOperand(&U))
1547           return true;
1548         unsigned ArgNo = CB->getArgOperandNo(&U);
1549 
1550         const auto &NoFreeArg = A.getAAFor<AANoFree>(
1551             *this, IRPosition::callsite_argument(*CB, ArgNo),
1552             DepClassTy::REQUIRED);
1553         return NoFreeArg.isAssumedNoFree();
1554       }
1555 
1556       if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
1557           isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
1558         Follow = true;
1559         return true;
1560       }
1561       if (isa<ReturnInst>(UserI))
1562         return true;
1563 
1564       // Unknown user.
1565       return false;
1566     };
1567     if (!A.checkForAllUses(Pred, *this, AssociatedValue))
1568       return indicatePessimisticFixpoint();
1569 
1570     return ChangeStatus::UNCHANGED;
1571   }
1572 };
1573 
1574 /// NoFree attribute for a call site argument.
1575 struct AANoFreeArgument final : AANoFreeFloating {
1576   AANoFreeArgument(const IRPosition &IRP, Attributor &A)
1577       : AANoFreeFloating(IRP, A) {}
1578 
1579   /// See AbstractAttribute::trackStatistics()
1580   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) }
1581 };
1582 
1583 /// NoFree attribute for call site arguments.
1584 struct AANoFreeCallSiteArgument final : AANoFreeFloating {
1585   AANoFreeCallSiteArgument(const IRPosition &IRP, Attributor &A)
1586       : AANoFreeFloating(IRP, A) {}
1587 
1588   /// See AbstractAttribute::updateImpl(...).
1589   ChangeStatus updateImpl(Attributor &A) override {
1590     // TODO: Once we have call site specific value information we can provide
1591     //       call site specific liveness information and then it makes
1592     //       sense to specialize attributes for call sites arguments instead of
1593     //       redirecting requests to the callee argument.
1594     Argument *Arg = getAssociatedArgument();
1595     if (!Arg)
1596       return indicatePessimisticFixpoint();
1597     const IRPosition &ArgPos = IRPosition::argument(*Arg);
1598     auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos, DepClassTy::REQUIRED);
1599     return clampStateAndIndicateChange(getState(), ArgAA.getState());
1600   }
1601 
1602   /// See AbstractAttribute::trackStatistics()
1603   void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)};
1604 };
1605 
1606 /// NoFree attribute for function return value.
1607 struct AANoFreeReturned final : AANoFreeFloating {
1608   AANoFreeReturned(const IRPosition &IRP, Attributor &A)
1609       : AANoFreeFloating(IRP, A) {
1610     llvm_unreachable("NoFree is not applicable to function returns!");
1611   }
1612 
1613   /// See AbstractAttribute::initialize(...).
1614   void initialize(Attributor &A) override {
1615     llvm_unreachable("NoFree is not applicable to function returns!");
1616   }
1617 
1618   /// See AbstractAttribute::updateImpl(...).
1619   ChangeStatus updateImpl(Attributor &A) override {
1620     llvm_unreachable("NoFree is not applicable to function returns!");
1621   }
1622 
1623   /// See AbstractAttribute::trackStatistics()
1624   void trackStatistics() const override {}
1625 };
1626 
1627 /// NoFree attribute deduction for a call site return value.
1628 struct AANoFreeCallSiteReturned final : AANoFreeFloating {
1629   AANoFreeCallSiteReturned(const IRPosition &IRP, Attributor &A)
1630       : AANoFreeFloating(IRP, A) {}
1631 
1632   ChangeStatus manifest(Attributor &A) override {
1633     return ChangeStatus::UNCHANGED;
1634   }
1635   /// See AbstractAttribute::trackStatistics()
1636   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) }
1637 };
1638 
1639 /// ------------------------ NonNull Argument Attribute ------------------------
1640 static int64_t getKnownNonNullAndDerefBytesForUse(
1641     Attributor &A, const AbstractAttribute &QueryingAA, Value &AssociatedValue,
1642     const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
1643   TrackUse = false;
1644 
1645   const Value *UseV = U->get();
1646   if (!UseV->getType()->isPointerTy())
1647     return 0;
1648 
1649   // We need to follow common pointer manipulation uses to the accesses they
1650   // feed into. We can try to be smart to avoid looking through things we do not
1651   // like for now, e.g., non-inbounds GEPs.
1652   if (isa<CastInst>(I)) {
1653     TrackUse = true;
1654     return 0;
1655   }
1656 
1657   if (isa<GetElementPtrInst>(I)) {
1658     TrackUse = true;
1659     return 0;
1660   }
1661 
1662   Type *PtrTy = UseV->getType();
1663   const Function *F = I->getFunction();
1664   bool NullPointerIsDefined =
1665       F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
1666   const DataLayout &DL = A.getInfoCache().getDL();
1667   if (const auto *CB = dyn_cast<CallBase>(I)) {
1668     if (CB->isBundleOperand(U)) {
1669       if (RetainedKnowledge RK = getKnowledgeFromUse(
1670               U, {Attribute::NonNull, Attribute::Dereferenceable})) {
1671         IsNonNull |=
1672             (RK.AttrKind == Attribute::NonNull || !NullPointerIsDefined);
1673         return RK.ArgValue;
1674       }
1675       return 0;
1676     }
1677 
1678     if (CB->isCallee(U)) {
1679       IsNonNull |= !NullPointerIsDefined;
1680       return 0;
1681     }
1682 
1683     unsigned ArgNo = CB->getArgOperandNo(U);
1684     IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
1685     // As long as we only use known information there is no need to track
1686     // dependences here.
1687     auto &DerefAA =
1688         A.getAAFor<AADereferenceable>(QueryingAA, IRP, DepClassTy::NONE);
1689     IsNonNull |= DerefAA.isKnownNonNull();
1690     return DerefAA.getKnownDereferenceableBytes();
1691   }
1692 
1693   int64_t Offset;
1694   const Value *Base =
1695       getMinimalBaseOfAccsesPointerOperand(A, QueryingAA, I, Offset, DL);
1696   if (Base) {
1697     if (Base == &AssociatedValue &&
1698         getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
1699       int64_t DerefBytes =
1700           (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset;
1701 
1702       IsNonNull |= !NullPointerIsDefined;
1703       return std::max(int64_t(0), DerefBytes);
1704     }
1705   }
1706 
1707   /// Corner case when an offset is 0.
1708   Base = getBasePointerOfAccessPointerOperand(I, Offset, DL,
1709                                               /*AllowNonInbounds*/ true);
1710   if (Base) {
1711     if (Offset == 0 && Base == &AssociatedValue &&
1712         getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
1713       int64_t DerefBytes =
1714           (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType());
1715       IsNonNull |= !NullPointerIsDefined;
1716       return std::max(int64_t(0), DerefBytes);
1717     }
1718   }
1719 
1720   return 0;
1721 }
1722 
1723 struct AANonNullImpl : AANonNull {
1724   AANonNullImpl(const IRPosition &IRP, Attributor &A)
1725       : AANonNull(IRP, A),
1726         NullIsDefined(NullPointerIsDefined(
1727             getAnchorScope(),
1728             getAssociatedValue().getType()->getPointerAddressSpace())) {}
1729 
1730   /// See AbstractAttribute::initialize(...).
1731   void initialize(Attributor &A) override {
1732     Value &V = getAssociatedValue();
1733     if (!NullIsDefined &&
1734         hasAttr({Attribute::NonNull, Attribute::Dereferenceable},
1735                 /* IgnoreSubsumingPositions */ false, &A)) {
1736       indicateOptimisticFixpoint();
1737       return;
1738     }
1739 
1740     if (isa<ConstantPointerNull>(V)) {
1741       indicatePessimisticFixpoint();
1742       return;
1743     }
1744 
1745     AANonNull::initialize(A);
1746 
1747     bool CanBeNull, CanBeFreed;
1748     if (V.getPointerDereferenceableBytes(A.getDataLayout(), CanBeNull,
1749                                          CanBeFreed)) {
1750       if (!CanBeNull) {
1751         indicateOptimisticFixpoint();
1752         return;
1753       }
1754     }
1755 
1756     if (isa<GlobalValue>(&getAssociatedValue())) {
1757       indicatePessimisticFixpoint();
1758       return;
1759     }
1760 
1761     if (Instruction *CtxI = getCtxI())
1762       followUsesInMBEC(*this, A, getState(), *CtxI);
1763   }
1764 
1765   /// See followUsesInMBEC
1766   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
1767                        AANonNull::StateType &State) {
1768     bool IsNonNull = false;
1769     bool TrackUse = false;
1770     getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
1771                                        IsNonNull, TrackUse);
1772     State.setKnown(IsNonNull);
1773     return TrackUse;
1774   }
1775 
1776   /// See AbstractAttribute::getAsStr().
1777   const std::string getAsStr() const override {
1778     return getAssumed() ? "nonnull" : "may-null";
1779   }
1780 
1781   /// Flag to determine if the underlying value can be null and still allow
1782   /// valid accesses.
1783   const bool NullIsDefined;
1784 };
1785 
1786 /// NonNull attribute for a floating value.
1787 struct AANonNullFloating : public AANonNullImpl {
1788   AANonNullFloating(const IRPosition &IRP, Attributor &A)
1789       : AANonNullImpl(IRP, A) {}
1790 
1791   /// See AbstractAttribute::updateImpl(...).
1792   ChangeStatus updateImpl(Attributor &A) override {
1793     const DataLayout &DL = A.getDataLayout();
1794 
1795     DominatorTree *DT = nullptr;
1796     AssumptionCache *AC = nullptr;
1797     InformationCache &InfoCache = A.getInfoCache();
1798     if (const Function *Fn = getAnchorScope()) {
1799       DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
1800       AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*Fn);
1801     }
1802 
1803     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
1804                             AANonNull::StateType &T, bool Stripped) -> bool {
1805       const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V),
1806                                              DepClassTy::REQUIRED);
1807       if (!Stripped && this == &AA) {
1808         if (!isKnownNonZero(&V, DL, 0, AC, CtxI, DT))
1809           T.indicatePessimisticFixpoint();
1810       } else {
1811         // Use abstract attribute information.
1812         const AANonNull::StateType &NS = AA.getState();
1813         T ^= NS;
1814       }
1815       return T.isValidState();
1816     };
1817 
1818     StateType T;
1819     if (!genericValueTraversal<AANonNull, StateType>(
1820             A, getIRPosition(), *this, T, VisitValueCB, getCtxI()))
1821       return indicatePessimisticFixpoint();
1822 
1823     return clampStateAndIndicateChange(getState(), T);
1824   }
1825 
1826   /// See AbstractAttribute::trackStatistics()
1827   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
1828 };
1829 
1830 /// NonNull attribute for function return value.
1831 struct AANonNullReturned final
1832     : AAReturnedFromReturnedValues<AANonNull, AANonNull> {
1833   AANonNullReturned(const IRPosition &IRP, Attributor &A)
1834       : AAReturnedFromReturnedValues<AANonNull, AANonNull>(IRP, A) {}
1835 
1836   /// See AbstractAttribute::getAsStr().
1837   const std::string getAsStr() const override {
1838     return getAssumed() ? "nonnull" : "may-null";
1839   }
1840 
1841   /// See AbstractAttribute::trackStatistics()
1842   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
1843 };
1844 
1845 /// NonNull attribute for function argument.
1846 struct AANonNullArgument final
1847     : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> {
1848   AANonNullArgument(const IRPosition &IRP, Attributor &A)
1849       : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP, A) {}
1850 
1851   /// See AbstractAttribute::trackStatistics()
1852   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
1853 };
1854 
1855 struct AANonNullCallSiteArgument final : AANonNullFloating {
1856   AANonNullCallSiteArgument(const IRPosition &IRP, Attributor &A)
1857       : AANonNullFloating(IRP, A) {}
1858 
1859   /// See AbstractAttribute::trackStatistics()
1860   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
1861 };
1862 
1863 /// NonNull attribute for a call site return position.
1864 struct AANonNullCallSiteReturned final
1865     : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> {
1866   AANonNullCallSiteReturned(const IRPosition &IRP, Attributor &A)
1867       : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP, A) {}
1868 
1869   /// See AbstractAttribute::trackStatistics()
1870   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
1871 };
1872 
1873 /// ------------------------ No-Recurse Attributes ----------------------------
1874 
1875 struct AANoRecurseImpl : public AANoRecurse {
1876   AANoRecurseImpl(const IRPosition &IRP, Attributor &A) : AANoRecurse(IRP, A) {}
1877 
1878   /// See AbstractAttribute::getAsStr()
1879   const std::string getAsStr() const override {
1880     return getAssumed() ? "norecurse" : "may-recurse";
1881   }
1882 };
1883 
1884 struct AANoRecurseFunction final : AANoRecurseImpl {
1885   AANoRecurseFunction(const IRPosition &IRP, Attributor &A)
1886       : AANoRecurseImpl(IRP, A) {}
1887 
1888   /// See AbstractAttribute::initialize(...).
1889   void initialize(Attributor &A) override {
1890     AANoRecurseImpl::initialize(A);
1891     if (const Function *F = getAnchorScope())
1892       if (A.getInfoCache().getSccSize(*F) != 1)
1893         indicatePessimisticFixpoint();
1894   }
1895 
1896   /// See AbstractAttribute::updateImpl(...).
1897   ChangeStatus updateImpl(Attributor &A) override {
1898 
1899     // If all live call sites are known to be no-recurse, we are as well.
1900     auto CallSitePred = [&](AbstractCallSite ACS) {
1901       const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
1902           *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
1903           DepClassTy::NONE);
1904       return NoRecurseAA.isKnownNoRecurse();
1905     };
1906     bool AllCallSitesKnown;
1907     if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) {
1908       // If we know all call sites and all are known no-recurse, we are done.
1909       // If all known call sites, which might not be all that exist, are known
1910       // to be no-recurse, we are not done but we can continue to assume
1911       // no-recurse. If one of the call sites we have not visited will become
1912       // live, another update is triggered.
1913       if (AllCallSitesKnown)
1914         indicateOptimisticFixpoint();
1915       return ChangeStatus::UNCHANGED;
1916     }
1917 
1918     // If the above check does not hold anymore we look at the calls.
1919     auto CheckForNoRecurse = [&](Instruction &I) {
1920       const auto &CB = cast<CallBase>(I);
1921       if (CB.hasFnAttr(Attribute::NoRecurse))
1922         return true;
1923 
1924       const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
1925           *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
1926       if (!NoRecurseAA.isAssumedNoRecurse())
1927         return false;
1928 
1929       // Recursion to the same function
1930       if (CB.getCalledFunction() == getAnchorScope())
1931         return false;
1932 
1933       return true;
1934     };
1935 
1936     if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this))
1937       return indicatePessimisticFixpoint();
1938     return ChangeStatus::UNCHANGED;
1939   }
1940 
1941   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
1942 };
1943 
1944 /// NoRecurse attribute deduction for a call sites.
1945 struct AANoRecurseCallSite final : AANoRecurseImpl {
1946   AANoRecurseCallSite(const IRPosition &IRP, Attributor &A)
1947       : AANoRecurseImpl(IRP, A) {}
1948 
1949   /// See AbstractAttribute::initialize(...).
1950   void initialize(Attributor &A) override {
1951     AANoRecurseImpl::initialize(A);
1952     Function *F = getAssociatedFunction();
1953     if (!F || F->isDeclaration())
1954       indicatePessimisticFixpoint();
1955   }
1956 
1957   /// See AbstractAttribute::updateImpl(...).
1958   ChangeStatus updateImpl(Attributor &A) override {
1959     // TODO: Once we have call site specific value information we can provide
1960     //       call site specific liveness information and then it makes
1961     //       sense to specialize attributes for call sites arguments instead of
1962     //       redirecting requests to the callee argument.
1963     Function *F = getAssociatedFunction();
1964     const IRPosition &FnPos = IRPosition::function(*F);
1965     auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos, DepClassTy::REQUIRED);
1966     return clampStateAndIndicateChange(getState(), FnAA.getState());
1967   }
1968 
1969   /// See AbstractAttribute::trackStatistics()
1970   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
1971 };
1972 
1973 /// -------------------- Undefined-Behavior Attributes ------------------------
1974 
1975 struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
1976   AAUndefinedBehaviorImpl(const IRPosition &IRP, Attributor &A)
1977       : AAUndefinedBehavior(IRP, A) {}
1978 
1979   /// See AbstractAttribute::updateImpl(...).
1980   // through a pointer (i.e. also branches etc.)
1981   ChangeStatus updateImpl(Attributor &A) override {
1982     const size_t UBPrevSize = KnownUBInsts.size();
1983     const size_t NoUBPrevSize = AssumedNoUBInsts.size();
1984 
1985     auto InspectMemAccessInstForUB = [&](Instruction &I) {
1986       // Skip instructions that are already saved.
1987       if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
1988         return true;
1989 
1990       // If we reach here, we know we have an instruction
1991       // that accesses memory through a pointer operand,
1992       // for which getPointerOperand() should give it to us.
1993       const Value *PtrOp = getPointerOperand(&I, /* AllowVolatile */ true);
1994       assert(PtrOp &&
1995              "Expected pointer operand of memory accessing instruction");
1996 
1997       // Either we stopped and the appropriate action was taken,
1998       // or we got back a simplified value to continue.
1999       Optional<Value *> SimplifiedPtrOp = stopOnUndefOrAssumed(A, PtrOp, &I);
2000       if (!SimplifiedPtrOp.hasValue())
2001         return true;
2002       const Value *PtrOpVal = SimplifiedPtrOp.getValue();
2003 
2004       // A memory access through a pointer is considered UB
2005       // only if the pointer has constant null value.
2006       // TODO: Expand it to not only check constant values.
2007       if (!isa<ConstantPointerNull>(PtrOpVal)) {
2008         AssumedNoUBInsts.insert(&I);
2009         return true;
2010       }
2011       const Type *PtrTy = PtrOpVal->getType();
2012 
2013       // Because we only consider instructions inside functions,
2014       // assume that a parent function exists.
2015       const Function *F = I.getFunction();
2016 
2017       // A memory access using constant null pointer is only considered UB
2018       // if null pointer is _not_ defined for the target platform.
2019       if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
2020         AssumedNoUBInsts.insert(&I);
2021       else
2022         KnownUBInsts.insert(&I);
2023       return true;
2024     };
2025 
2026     auto InspectBrInstForUB = [&](Instruction &I) {
2027       // A conditional branch instruction is considered UB if it has `undef`
2028       // condition.
2029 
2030       // Skip instructions that are already saved.
2031       if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2032         return true;
2033 
2034       // We know we have a branch instruction.
2035       auto BrInst = cast<BranchInst>(&I);
2036 
2037       // Unconditional branches are never considered UB.
2038       if (BrInst->isUnconditional())
2039         return true;
2040 
2041       // Either we stopped and the appropriate action was taken,
2042       // or we got back a simplified value to continue.
2043       Optional<Value *> SimplifiedCond =
2044           stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
2045       if (!SimplifiedCond.hasValue())
2046         return true;
2047       AssumedNoUBInsts.insert(&I);
2048       return true;
2049     };
2050 
2051     auto InspectCallSiteForUB = [&](Instruction &I) {
2052       // Check whether a callsite always cause UB or not
2053 
2054       // Skip instructions that are already saved.
2055       if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2056         return true;
2057 
2058       // Check nonnull and noundef argument attribute violation for each
2059       // callsite.
2060       CallBase &CB = cast<CallBase>(I);
2061       Function *Callee = CB.getCalledFunction();
2062       if (!Callee)
2063         return true;
2064       for (unsigned idx = 0; idx < CB.getNumArgOperands(); idx++) {
2065         // If current argument is known to be simplified to null pointer and the
2066         // corresponding argument position is known to have nonnull attribute,
2067         // the argument is poison. Furthermore, if the argument is poison and
2068         // the position is known to have noundef attriubte, this callsite is
2069         // considered UB.
2070         if (idx >= Callee->arg_size())
2071           break;
2072         Value *ArgVal = CB.getArgOperand(idx);
2073         if (!ArgVal)
2074           continue;
2075         // Here, we handle three cases.
2076         //   (1) Not having a value means it is dead. (we can replace the value
2077         //       with undef)
2078         //   (2) Simplified to undef. The argument violate noundef attriubte.
2079         //   (3) Simplified to null pointer where known to be nonnull.
2080         //       The argument is a poison value and violate noundef attribute.
2081         IRPosition CalleeArgumentIRP = IRPosition::callsite_argument(CB, idx);
2082         auto &NoUndefAA =
2083             A.getAAFor<AANoUndef>(*this, CalleeArgumentIRP, DepClassTy::NONE);
2084         if (!NoUndefAA.isKnownNoUndef())
2085           continue;
2086         auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
2087             *this, IRPosition::value(*ArgVal), DepClassTy::NONE);
2088         if (!ValueSimplifyAA.isKnown())
2089           continue;
2090         Optional<Value *> SimplifiedVal =
2091             ValueSimplifyAA.getAssumedSimplifiedValue(A);
2092         if (!SimplifiedVal.hasValue() ||
2093             isa<UndefValue>(*SimplifiedVal.getValue())) {
2094           KnownUBInsts.insert(&I);
2095           continue;
2096         }
2097         if (!ArgVal->getType()->isPointerTy() ||
2098             !isa<ConstantPointerNull>(*SimplifiedVal.getValue()))
2099           continue;
2100         auto &NonNullAA =
2101             A.getAAFor<AANonNull>(*this, CalleeArgumentIRP, DepClassTy::NONE);
2102         if (NonNullAA.isKnownNonNull())
2103           KnownUBInsts.insert(&I);
2104       }
2105       return true;
2106     };
2107 
2108     auto InspectReturnInstForUB =
2109         [&](Value &V, const SmallSetVector<ReturnInst *, 4> RetInsts) {
2110           // Check if a return instruction always cause UB or not
2111           // Note: It is guaranteed that the returned position of the anchor
2112           //       scope has noundef attribute when this is called.
2113           //       We also ensure the return position is not "assumed dead"
2114           //       because the returned value was then potentially simplified to
2115           //       `undef` in AAReturnedValues without removing the `noundef`
2116           //       attribute yet.
2117 
2118           // When the returned position has noundef attriubte, UB occur in the
2119           // following cases.
2120           //   (1) Returned value is known to be undef.
2121           //   (2) The value is known to be a null pointer and the returned
2122           //       position has nonnull attribute (because the returned value is
2123           //       poison).
2124           bool FoundUB = false;
2125           if (isa<UndefValue>(V)) {
2126             FoundUB = true;
2127           } else {
2128             if (isa<ConstantPointerNull>(V)) {
2129               auto &NonNullAA = A.getAAFor<AANonNull>(
2130                   *this, IRPosition::returned(*getAnchorScope()),
2131                   DepClassTy::NONE);
2132               if (NonNullAA.isKnownNonNull())
2133                 FoundUB = true;
2134             }
2135           }
2136 
2137           if (FoundUB)
2138             for (ReturnInst *RI : RetInsts)
2139               KnownUBInsts.insert(RI);
2140           return true;
2141         };
2142 
2143     A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
2144                               {Instruction::Load, Instruction::Store,
2145                                Instruction::AtomicCmpXchg,
2146                                Instruction::AtomicRMW},
2147                               /* CheckBBLivenessOnly */ true);
2148     A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
2149                               /* CheckBBLivenessOnly */ true);
2150     A.checkForAllCallLikeInstructions(InspectCallSiteForUB, *this);
2151 
2152     // If the returned position of the anchor scope has noundef attriubte, check
2153     // all returned instructions.
2154     if (!getAnchorScope()->getReturnType()->isVoidTy()) {
2155       const IRPosition &ReturnIRP = IRPosition::returned(*getAnchorScope());
2156       if (!A.isAssumedDead(ReturnIRP, this, nullptr)) {
2157         auto &RetPosNoUndefAA =
2158             A.getAAFor<AANoUndef>(*this, ReturnIRP, DepClassTy::NONE);
2159         if (RetPosNoUndefAA.isKnownNoUndef())
2160           A.checkForAllReturnedValuesAndReturnInsts(InspectReturnInstForUB,
2161                                                     *this);
2162       }
2163     }
2164 
2165     if (NoUBPrevSize != AssumedNoUBInsts.size() ||
2166         UBPrevSize != KnownUBInsts.size())
2167       return ChangeStatus::CHANGED;
2168     return ChangeStatus::UNCHANGED;
2169   }
2170 
2171   bool isKnownToCauseUB(Instruction *I) const override {
2172     return KnownUBInsts.count(I);
2173   }
2174 
2175   bool isAssumedToCauseUB(Instruction *I) const override {
2176     // In simple words, if an instruction is not in the assumed to _not_
2177     // cause UB, then it is assumed UB (that includes those
2178     // in the KnownUBInsts set). The rest is boilerplate
2179     // is to ensure that it is one of the instructions we test
2180     // for UB.
2181 
2182     switch (I->getOpcode()) {
2183     case Instruction::Load:
2184     case Instruction::Store:
2185     case Instruction::AtomicCmpXchg:
2186     case Instruction::AtomicRMW:
2187       return !AssumedNoUBInsts.count(I);
2188     case Instruction::Br: {
2189       auto BrInst = cast<BranchInst>(I);
2190       if (BrInst->isUnconditional())
2191         return false;
2192       return !AssumedNoUBInsts.count(I);
2193     } break;
2194     default:
2195       return false;
2196     }
2197     return false;
2198   }
2199 
2200   ChangeStatus manifest(Attributor &A) override {
2201     if (KnownUBInsts.empty())
2202       return ChangeStatus::UNCHANGED;
2203     for (Instruction *I : KnownUBInsts)
2204       A.changeToUnreachableAfterManifest(I);
2205     return ChangeStatus::CHANGED;
2206   }
2207 
2208   /// See AbstractAttribute::getAsStr()
2209   const std::string getAsStr() const override {
2210     return getAssumed() ? "undefined-behavior" : "no-ub";
2211   }
2212 
2213   /// Note: The correctness of this analysis depends on the fact that the
2214   /// following 2 sets will stop changing after some point.
2215   /// "Change" here means that their size changes.
2216   /// The size of each set is monotonically increasing
2217   /// (we only add items to them) and it is upper bounded by the number of
2218   /// instructions in the processed function (we can never save more
2219   /// elements in either set than this number). Hence, at some point,
2220   /// they will stop increasing.
2221   /// Consequently, at some point, both sets will have stopped
2222   /// changing, effectively making the analysis reach a fixpoint.
2223 
2224   /// Note: These 2 sets are disjoint and an instruction can be considered
2225   /// one of 3 things:
2226   /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
2227   ///    the KnownUBInsts set.
2228   /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
2229   ///    has a reason to assume it).
2230   /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
2231   ///    could not find a reason to assume or prove that it can cause UB,
2232   ///    hence it assumes it doesn't. We have a set for these instructions
2233   ///    so that we don't reprocess them in every update.
2234   ///    Note however that instructions in this set may cause UB.
2235 
2236 protected:
2237   /// A set of all live instructions _known_ to cause UB.
2238   SmallPtrSet<Instruction *, 8> KnownUBInsts;
2239 
2240 private:
2241   /// A set of all the (live) instructions that are assumed to _not_ cause UB.
2242   SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
2243 
2244   // Should be called on updates in which if we're processing an instruction
2245   // \p I that depends on a value \p V, one of the following has to happen:
2246   // - If the value is assumed, then stop.
2247   // - If the value is known but undef, then consider it UB.
2248   // - Otherwise, do specific processing with the simplified value.
2249   // We return None in the first 2 cases to signify that an appropriate
2250   // action was taken and the caller should stop.
2251   // Otherwise, we return the simplified value that the caller should
2252   // use for specific processing.
2253   Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V,
2254                                          Instruction *I) {
2255     const auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
2256         *this, IRPosition::value(*V), DepClassTy::REQUIRED);
2257     Optional<Value *> SimplifiedV =
2258         ValueSimplifyAA.getAssumedSimplifiedValue(A);
2259     if (!ValueSimplifyAA.isKnown()) {
2260       // Don't depend on assumed values.
2261       return llvm::None;
2262     }
2263     if (!SimplifiedV.hasValue()) {
2264       // If it is known (which we tested above) but it doesn't have a value,
2265       // then we can assume `undef` and hence the instruction is UB.
2266       KnownUBInsts.insert(I);
2267       return llvm::None;
2268     }
2269     Value *Val = SimplifiedV.getValue();
2270     if (isa<UndefValue>(Val)) {
2271       KnownUBInsts.insert(I);
2272       return llvm::None;
2273     }
2274     return Val;
2275   }
2276 };
2277 
2278 struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
2279   AAUndefinedBehaviorFunction(const IRPosition &IRP, Attributor &A)
2280       : AAUndefinedBehaviorImpl(IRP, A) {}
2281 
2282   /// See AbstractAttribute::trackStatistics()
2283   void trackStatistics() const override {
2284     STATS_DECL(UndefinedBehaviorInstruction, Instruction,
2285                "Number of instructions known to have UB");
2286     BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) +=
2287         KnownUBInsts.size();
2288   }
2289 };
2290 
2291 /// ------------------------ Will-Return Attributes ----------------------------
2292 
2293 // Helper function that checks whether a function has any cycle which we don't
2294 // know if it is bounded or not.
2295 // Loops with maximum trip count are considered bounded, any other cycle not.
2296 static bool mayContainUnboundedCycle(Function &F, Attributor &A) {
2297   ScalarEvolution *SE =
2298       A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F);
2299   LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F);
2300   // If either SCEV or LoopInfo is not available for the function then we assume
2301   // any cycle to be unbounded cycle.
2302   // We use scc_iterator which uses Tarjan algorithm to find all the maximal
2303   // SCCs.To detect if there's a cycle, we only need to find the maximal ones.
2304   if (!SE || !LI) {
2305     for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI)
2306       if (SCCI.hasCycle())
2307         return true;
2308     return false;
2309   }
2310 
2311   // If there's irreducible control, the function may contain non-loop cycles.
2312   if (mayContainIrreducibleControl(F, LI))
2313     return true;
2314 
2315   // Any loop that does not have a max trip count is considered unbounded cycle.
2316   for (auto *L : LI->getLoopsInPreorder()) {
2317     if (!SE->getSmallConstantMaxTripCount(L))
2318       return true;
2319   }
2320   return false;
2321 }
2322 
2323 struct AAWillReturnImpl : public AAWillReturn {
2324   AAWillReturnImpl(const IRPosition &IRP, Attributor &A)
2325       : AAWillReturn(IRP, A) {}
2326 
2327   /// See AbstractAttribute::initialize(...).
2328   void initialize(Attributor &A) override {
2329     AAWillReturn::initialize(A);
2330 
2331     if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ true)) {
2332       indicateOptimisticFixpoint();
2333       return;
2334     }
2335   }
2336 
2337   /// Check for `mustprogress` and `readonly` as they imply `willreturn`.
2338   bool isImpliedByMustprogressAndReadonly(Attributor &A, bool KnownOnly) {
2339     // Check for `mustprogress` in the scope and the associated function which
2340     // might be different if this is a call site.
2341     if ((!getAnchorScope() || !getAnchorScope()->mustProgress()) &&
2342         (!getAssociatedFunction() || !getAssociatedFunction()->mustProgress()))
2343       return false;
2344 
2345     const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(),
2346                                                       DepClassTy::NONE);
2347     if (!MemAA.isAssumedReadOnly())
2348       return false;
2349     if (KnownOnly && !MemAA.isKnownReadOnly())
2350       return false;
2351     if (!MemAA.isKnownReadOnly())
2352       A.recordDependence(MemAA, *this, DepClassTy::OPTIONAL);
2353 
2354     return true;
2355   }
2356 
2357   /// See AbstractAttribute::updateImpl(...).
2358   ChangeStatus updateImpl(Attributor &A) override {
2359     if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
2360       return ChangeStatus::UNCHANGED;
2361 
2362     auto CheckForWillReturn = [&](Instruction &I) {
2363       IRPosition IPos = IRPosition::callsite_function(cast<CallBase>(I));
2364       const auto &WillReturnAA =
2365           A.getAAFor<AAWillReturn>(*this, IPos, DepClassTy::REQUIRED);
2366       if (WillReturnAA.isKnownWillReturn())
2367         return true;
2368       if (!WillReturnAA.isAssumedWillReturn())
2369         return false;
2370       const auto &NoRecurseAA =
2371           A.getAAFor<AANoRecurse>(*this, IPos, DepClassTy::REQUIRED);
2372       return NoRecurseAA.isAssumedNoRecurse();
2373     };
2374 
2375     if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this))
2376       return indicatePessimisticFixpoint();
2377 
2378     return ChangeStatus::UNCHANGED;
2379   }
2380 
2381   /// See AbstractAttribute::getAsStr()
2382   const std::string getAsStr() const override {
2383     return getAssumed() ? "willreturn" : "may-noreturn";
2384   }
2385 };
2386 
2387 struct AAWillReturnFunction final : AAWillReturnImpl {
2388   AAWillReturnFunction(const IRPosition &IRP, Attributor &A)
2389       : AAWillReturnImpl(IRP, A) {}
2390 
2391   /// See AbstractAttribute::initialize(...).
2392   void initialize(Attributor &A) override {
2393     AAWillReturnImpl::initialize(A);
2394 
2395     Function *F = getAnchorScope();
2396     if (!F || F->isDeclaration() || mayContainUnboundedCycle(*F, A))
2397       indicatePessimisticFixpoint();
2398   }
2399 
2400   /// See AbstractAttribute::trackStatistics()
2401   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
2402 };
2403 
2404 /// WillReturn attribute deduction for a call sites.
2405 struct AAWillReturnCallSite final : AAWillReturnImpl {
2406   AAWillReturnCallSite(const IRPosition &IRP, Attributor &A)
2407       : AAWillReturnImpl(IRP, A) {}
2408 
2409   /// See AbstractAttribute::initialize(...).
2410   void initialize(Attributor &A) override {
2411     AAWillReturnImpl::initialize(A);
2412     Function *F = getAssociatedFunction();
2413     if (!F || !A.isFunctionIPOAmendable(*F))
2414       indicatePessimisticFixpoint();
2415   }
2416 
2417   /// See AbstractAttribute::updateImpl(...).
2418   ChangeStatus updateImpl(Attributor &A) override {
2419     if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
2420       return ChangeStatus::UNCHANGED;
2421 
2422     // TODO: Once we have call site specific value information we can provide
2423     //       call site specific liveness information and then it makes
2424     //       sense to specialize attributes for call sites arguments instead of
2425     //       redirecting requests to the callee argument.
2426     Function *F = getAssociatedFunction();
2427     const IRPosition &FnPos = IRPosition::function(*F);
2428     auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos, DepClassTy::REQUIRED);
2429     return clampStateAndIndicateChange(getState(), FnAA.getState());
2430   }
2431 
2432   /// See AbstractAttribute::trackStatistics()
2433   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
2434 };
2435 
2436 /// -------------------AAReachability Attribute--------------------------
2437 
2438 struct AAReachabilityImpl : AAReachability {
2439   AAReachabilityImpl(const IRPosition &IRP, Attributor &A)
2440       : AAReachability(IRP, A) {}
2441 
2442   const std::string getAsStr() const override {
2443     // TODO: Return the number of reachable queries.
2444     return "reachable";
2445   }
2446 
2447   /// See AbstractAttribute::initialize(...).
2448   void initialize(Attributor &A) override { indicatePessimisticFixpoint(); }
2449 
2450   /// See AbstractAttribute::updateImpl(...).
2451   ChangeStatus updateImpl(Attributor &A) override {
2452     return indicatePessimisticFixpoint();
2453   }
2454 };
2455 
2456 struct AAReachabilityFunction final : public AAReachabilityImpl {
2457   AAReachabilityFunction(const IRPosition &IRP, Attributor &A)
2458       : AAReachabilityImpl(IRP, A) {}
2459 
2460   /// See AbstractAttribute::trackStatistics()
2461   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); }
2462 };
2463 
2464 /// ------------------------ NoAlias Argument Attribute ------------------------
2465 
2466 struct AANoAliasImpl : AANoAlias {
2467   AANoAliasImpl(const IRPosition &IRP, Attributor &A) : AANoAlias(IRP, A) {
2468     assert(getAssociatedType()->isPointerTy() &&
2469            "Noalias is a pointer attribute");
2470   }
2471 
2472   const std::string getAsStr() const override {
2473     return getAssumed() ? "noalias" : "may-alias";
2474   }
2475 };
2476 
2477 /// NoAlias attribute for a floating value.
2478 struct AANoAliasFloating final : AANoAliasImpl {
2479   AANoAliasFloating(const IRPosition &IRP, Attributor &A)
2480       : AANoAliasImpl(IRP, A) {}
2481 
2482   /// See AbstractAttribute::initialize(...).
2483   void initialize(Attributor &A) override {
2484     AANoAliasImpl::initialize(A);
2485     Value *Val = &getAssociatedValue();
2486     do {
2487       CastInst *CI = dyn_cast<CastInst>(Val);
2488       if (!CI)
2489         break;
2490       Value *Base = CI->getOperand(0);
2491       if (!Base->hasOneUse())
2492         break;
2493       Val = Base;
2494     } while (true);
2495 
2496     if (!Val->getType()->isPointerTy()) {
2497       indicatePessimisticFixpoint();
2498       return;
2499     }
2500 
2501     if (isa<AllocaInst>(Val))
2502       indicateOptimisticFixpoint();
2503     else if (isa<ConstantPointerNull>(Val) &&
2504              !NullPointerIsDefined(getAnchorScope(),
2505                                    Val->getType()->getPointerAddressSpace()))
2506       indicateOptimisticFixpoint();
2507     else if (Val != &getAssociatedValue()) {
2508       const auto &ValNoAliasAA = A.getAAFor<AANoAlias>(
2509           *this, IRPosition::value(*Val), DepClassTy::OPTIONAL);
2510       if (ValNoAliasAA.isKnownNoAlias())
2511         indicateOptimisticFixpoint();
2512     }
2513   }
2514 
2515   /// See AbstractAttribute::updateImpl(...).
2516   ChangeStatus updateImpl(Attributor &A) override {
2517     // TODO: Implement this.
2518     return indicatePessimisticFixpoint();
2519   }
2520 
2521   /// See AbstractAttribute::trackStatistics()
2522   void trackStatistics() const override {
2523     STATS_DECLTRACK_FLOATING_ATTR(noalias)
2524   }
2525 };
2526 
2527 /// NoAlias attribute for an argument.
2528 struct AANoAliasArgument final
2529     : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
2530   using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
2531   AANoAliasArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
2532 
2533   /// See AbstractAttribute::initialize(...).
2534   void initialize(Attributor &A) override {
2535     Base::initialize(A);
2536     // See callsite argument attribute and callee argument attribute.
2537     if (hasAttr({Attribute::ByVal}))
2538       indicateOptimisticFixpoint();
2539   }
2540 
2541   /// See AbstractAttribute::update(...).
2542   ChangeStatus updateImpl(Attributor &A) override {
2543     // We have to make sure no-alias on the argument does not break
2544     // synchronization when this is a callback argument, see also [1] below.
2545     // If synchronization cannot be affected, we delegate to the base updateImpl
2546     // function, otherwise we give up for now.
2547 
2548     // If the function is no-sync, no-alias cannot break synchronization.
2549     const auto &NoSyncAA =
2550         A.getAAFor<AANoSync>(*this, IRPosition::function_scope(getIRPosition()),
2551                              DepClassTy::OPTIONAL);
2552     if (NoSyncAA.isAssumedNoSync())
2553       return Base::updateImpl(A);
2554 
2555     // If the argument is read-only, no-alias cannot break synchronization.
2556     const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
2557         *this, getIRPosition(), DepClassTy::OPTIONAL);
2558     if (MemBehaviorAA.isAssumedReadOnly())
2559       return Base::updateImpl(A);
2560 
2561     // If the argument is never passed through callbacks, no-alias cannot break
2562     // synchronization.
2563     bool AllCallSitesKnown;
2564     if (A.checkForAllCallSites(
2565             [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
2566             true, AllCallSitesKnown))
2567       return Base::updateImpl(A);
2568 
2569     // TODO: add no-alias but make sure it doesn't break synchronization by
2570     // introducing fake uses. See:
2571     // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
2572     //     International Workshop on OpenMP 2018,
2573     //     http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
2574 
2575     return indicatePessimisticFixpoint();
2576   }
2577 
2578   /// See AbstractAttribute::trackStatistics()
2579   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
2580 };
2581 
2582 struct AANoAliasCallSiteArgument final : AANoAliasImpl {
2583   AANoAliasCallSiteArgument(const IRPosition &IRP, Attributor &A)
2584       : AANoAliasImpl(IRP, A) {}
2585 
2586   /// See AbstractAttribute::initialize(...).
2587   void initialize(Attributor &A) override {
2588     // See callsite argument attribute and callee argument attribute.
2589     const auto &CB = cast<CallBase>(getAnchorValue());
2590     if (CB.paramHasAttr(getCallSiteArgNo(), Attribute::NoAlias))
2591       indicateOptimisticFixpoint();
2592     Value &Val = getAssociatedValue();
2593     if (isa<ConstantPointerNull>(Val) &&
2594         !NullPointerIsDefined(getAnchorScope(),
2595                               Val.getType()->getPointerAddressSpace()))
2596       indicateOptimisticFixpoint();
2597   }
2598 
2599   /// Determine if the underlying value may alias with the call site argument
2600   /// \p OtherArgNo of \p ICS (= the underlying call site).
2601   bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
2602                             const AAMemoryBehavior &MemBehaviorAA,
2603                             const CallBase &CB, unsigned OtherArgNo) {
2604     // We do not need to worry about aliasing with the underlying IRP.
2605     if (this->getCalleeArgNo() == (int)OtherArgNo)
2606       return false;
2607 
2608     // If it is not a pointer or pointer vector we do not alias.
2609     const Value *ArgOp = CB.getArgOperand(OtherArgNo);
2610     if (!ArgOp->getType()->isPtrOrPtrVectorTy())
2611       return false;
2612 
2613     auto &CBArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
2614         *this, IRPosition::callsite_argument(CB, OtherArgNo), DepClassTy::NONE);
2615 
2616     // If the argument is readnone, there is no read-write aliasing.
2617     if (CBArgMemBehaviorAA.isAssumedReadNone()) {
2618       A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2619       return false;
2620     }
2621 
2622     // If the argument is readonly and the underlying value is readonly, there
2623     // is no read-write aliasing.
2624     bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
2625     if (CBArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
2626       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2627       A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2628       return false;
2629     }
2630 
2631     // We have to utilize actual alias analysis queries so we need the object.
2632     if (!AAR)
2633       AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
2634 
2635     // Try to rule it out at the call site.
2636     bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
2637     LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "
2638                          "callsite arguments: "
2639                       << getAssociatedValue() << " " << *ArgOp << " => "
2640                       << (IsAliasing ? "" : "no-") << "alias \n");
2641 
2642     return IsAliasing;
2643   }
2644 
2645   bool
2646   isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
2647                                          const AAMemoryBehavior &MemBehaviorAA,
2648                                          const AANoAlias &NoAliasAA) {
2649     // We can deduce "noalias" if the following conditions hold.
2650     // (i)   Associated value is assumed to be noalias in the definition.
2651     // (ii)  Associated value is assumed to be no-capture in all the uses
2652     //       possibly executed before this callsite.
2653     // (iii) There is no other pointer argument which could alias with the
2654     //       value.
2655 
2656     bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
2657     if (!AssociatedValueIsNoAliasAtDef) {
2658       LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()
2659                         << " is not no-alias at the definition\n");
2660       return false;
2661     }
2662 
2663     A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL);
2664 
2665     const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2666     const Function *ScopeFn = VIRP.getAnchorScope();
2667     auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, VIRP, DepClassTy::NONE);
2668     // Check whether the value is captured in the scope using AANoCapture.
2669     //      Look at CFG and check only uses possibly executed before this
2670     //      callsite.
2671     auto UsePred = [&](const Use &U, bool &Follow) -> bool {
2672       Instruction *UserI = cast<Instruction>(U.getUser());
2673 
2674       // If UserI is the curr instruction and there is a single potential use of
2675       // the value in UserI we allow the use.
2676       // TODO: We should inspect the operands and allow those that cannot alias
2677       //       with the value.
2678       if (UserI == getCtxI() && UserI->getNumOperands() == 1)
2679         return true;
2680 
2681       if (ScopeFn) {
2682         const auto &ReachabilityAA = A.getAAFor<AAReachability>(
2683             *this, IRPosition::function(*ScopeFn), DepClassTy::OPTIONAL);
2684 
2685         if (!ReachabilityAA.isAssumedReachable(A, *UserI, *getCtxI()))
2686           return true;
2687 
2688         if (auto *CB = dyn_cast<CallBase>(UserI)) {
2689           if (CB->isArgOperand(&U)) {
2690 
2691             unsigned ArgNo = CB->getArgOperandNo(&U);
2692 
2693             const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
2694                 *this, IRPosition::callsite_argument(*CB, ArgNo),
2695                 DepClassTy::OPTIONAL);
2696 
2697             if (NoCaptureAA.isAssumedNoCapture())
2698               return true;
2699           }
2700         }
2701       }
2702 
2703       // For cases which can potentially have more users
2704       if (isa<GetElementPtrInst>(U) || isa<BitCastInst>(U) || isa<PHINode>(U) ||
2705           isa<SelectInst>(U)) {
2706         Follow = true;
2707         return true;
2708       }
2709 
2710       LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n");
2711       return false;
2712     };
2713 
2714     if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
2715       if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) {
2716         LLVM_DEBUG(
2717             dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()
2718                    << " cannot be noalias as it is potentially captured\n");
2719         return false;
2720       }
2721     }
2722     A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
2723 
2724     // Check there is no other pointer argument which could alias with the
2725     // value passed at this call site.
2726     // TODO: AbstractCallSite
2727     const auto &CB = cast<CallBase>(getAnchorValue());
2728     for (unsigned OtherArgNo = 0; OtherArgNo < CB.getNumArgOperands();
2729          OtherArgNo++)
2730       if (mayAliasWithArgument(A, AAR, MemBehaviorAA, CB, OtherArgNo))
2731         return false;
2732 
2733     return true;
2734   }
2735 
2736   /// See AbstractAttribute::updateImpl(...).
2737   ChangeStatus updateImpl(Attributor &A) override {
2738     // If the argument is readnone we are done as there are no accesses via the
2739     // argument.
2740     auto &MemBehaviorAA =
2741         A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
2742     if (MemBehaviorAA.isAssumedReadNone()) {
2743       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2744       return ChangeStatus::UNCHANGED;
2745     }
2746 
2747     const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2748     const auto &NoAliasAA =
2749         A.getAAFor<AANoAlias>(*this, VIRP, DepClassTy::NONE);
2750 
2751     AAResults *AAR = nullptr;
2752     if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
2753                                                NoAliasAA)) {
2754       LLVM_DEBUG(
2755           dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n");
2756       return ChangeStatus::UNCHANGED;
2757     }
2758 
2759     return indicatePessimisticFixpoint();
2760   }
2761 
2762   /// See AbstractAttribute::trackStatistics()
2763   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
2764 };
2765 
2766 /// NoAlias attribute for function return value.
2767 struct AANoAliasReturned final : AANoAliasImpl {
2768   AANoAliasReturned(const IRPosition &IRP, Attributor &A)
2769       : AANoAliasImpl(IRP, A) {}
2770 
2771   /// See AbstractAttribute::initialize(...).
2772   void initialize(Attributor &A) override {
2773     AANoAliasImpl::initialize(A);
2774     Function *F = getAssociatedFunction();
2775     if (!F || F->isDeclaration())
2776       indicatePessimisticFixpoint();
2777   }
2778 
2779   /// See AbstractAttribute::updateImpl(...).
2780   virtual ChangeStatus updateImpl(Attributor &A) override {
2781 
2782     auto CheckReturnValue = [&](Value &RV) -> bool {
2783       if (Constant *C = dyn_cast<Constant>(&RV))
2784         if (C->isNullValue() || isa<UndefValue>(C))
2785           return true;
2786 
2787       /// For now, we can only deduce noalias if we have call sites.
2788       /// FIXME: add more support.
2789       if (!isa<CallBase>(&RV))
2790         return false;
2791 
2792       const IRPosition &RVPos = IRPosition::value(RV);
2793       const auto &NoAliasAA =
2794           A.getAAFor<AANoAlias>(*this, RVPos, DepClassTy::REQUIRED);
2795       if (!NoAliasAA.isAssumedNoAlias())
2796         return false;
2797 
2798       const auto &NoCaptureAA =
2799           A.getAAFor<AANoCapture>(*this, RVPos, DepClassTy::REQUIRED);
2800       return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
2801     };
2802 
2803     if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
2804       return indicatePessimisticFixpoint();
2805 
2806     return ChangeStatus::UNCHANGED;
2807   }
2808 
2809   /// See AbstractAttribute::trackStatistics()
2810   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
2811 };
2812 
2813 /// NoAlias attribute deduction for a call site return value.
2814 struct AANoAliasCallSiteReturned final : AANoAliasImpl {
2815   AANoAliasCallSiteReturned(const IRPosition &IRP, Attributor &A)
2816       : AANoAliasImpl(IRP, A) {}
2817 
2818   /// See AbstractAttribute::initialize(...).
2819   void initialize(Attributor &A) override {
2820     AANoAliasImpl::initialize(A);
2821     Function *F = getAssociatedFunction();
2822     if (!F || F->isDeclaration())
2823       indicatePessimisticFixpoint();
2824   }
2825 
2826   /// See AbstractAttribute::updateImpl(...).
2827   ChangeStatus updateImpl(Attributor &A) override {
2828     // TODO: Once we have call site specific value information we can provide
2829     //       call site specific liveness information and then it makes
2830     //       sense to specialize attributes for call sites arguments instead of
2831     //       redirecting requests to the callee argument.
2832     Function *F = getAssociatedFunction();
2833     const IRPosition &FnPos = IRPosition::returned(*F);
2834     auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos, DepClassTy::REQUIRED);
2835     return clampStateAndIndicateChange(getState(), FnAA.getState());
2836   }
2837 
2838   /// See AbstractAttribute::trackStatistics()
2839   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
2840 };
2841 
2842 /// -------------------AAIsDead Function Attribute-----------------------
2843 
2844 struct AAIsDeadValueImpl : public AAIsDead {
2845   AAIsDeadValueImpl(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
2846 
2847   /// See AAIsDead::isAssumedDead().
2848   bool isAssumedDead() const override { return getAssumed(); }
2849 
2850   /// See AAIsDead::isKnownDead().
2851   bool isKnownDead() const override { return getKnown(); }
2852 
2853   /// See AAIsDead::isAssumedDead(BasicBlock *).
2854   bool isAssumedDead(const BasicBlock *BB) const override { return false; }
2855 
2856   /// See AAIsDead::isKnownDead(BasicBlock *).
2857   bool isKnownDead(const BasicBlock *BB) const override { return false; }
2858 
2859   /// See AAIsDead::isAssumedDead(Instruction *I).
2860   bool isAssumedDead(const Instruction *I) const override {
2861     return I == getCtxI() && isAssumedDead();
2862   }
2863 
2864   /// See AAIsDead::isKnownDead(Instruction *I).
2865   bool isKnownDead(const Instruction *I) const override {
2866     return isAssumedDead(I) && getKnown();
2867   }
2868 
2869   /// See AbstractAttribute::getAsStr().
2870   const std::string getAsStr() const override {
2871     return isAssumedDead() ? "assumed-dead" : "assumed-live";
2872   }
2873 
2874   /// Check if all uses are assumed dead.
2875   bool areAllUsesAssumedDead(Attributor &A, Value &V) {
2876     auto UsePred = [&](const Use &U, bool &Follow) { return false; };
2877     // Explicitly set the dependence class to required because we want a long
2878     // chain of N dependent instructions to be considered live as soon as one is
2879     // without going through N update cycles. This is not required for
2880     // correctness.
2881     return A.checkForAllUses(UsePred, *this, V, DepClassTy::REQUIRED);
2882   }
2883 
2884   /// Determine if \p I is assumed to be side-effect free.
2885   bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
2886     if (!I || wouldInstructionBeTriviallyDead(I))
2887       return true;
2888 
2889     auto *CB = dyn_cast<CallBase>(I);
2890     if (!CB || isa<IntrinsicInst>(CB))
2891       return false;
2892 
2893     const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
2894     const auto &NoUnwindAA =
2895         A.getAndUpdateAAFor<AANoUnwind>(*this, CallIRP, DepClassTy::NONE);
2896     if (!NoUnwindAA.isAssumedNoUnwind())
2897       return false;
2898     if (!NoUnwindAA.isKnownNoUnwind())
2899       A.recordDependence(NoUnwindAA, *this, DepClassTy::OPTIONAL);
2900 
2901     const auto &MemBehaviorAA =
2902         A.getAndUpdateAAFor<AAMemoryBehavior>(*this, CallIRP, DepClassTy::NONE);
2903     if (MemBehaviorAA.isAssumedReadOnly()) {
2904       if (!MemBehaviorAA.isKnownReadOnly())
2905         A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2906       return true;
2907     }
2908     return false;
2909   }
2910 };
2911 
2912 struct AAIsDeadFloating : public AAIsDeadValueImpl {
2913   AAIsDeadFloating(const IRPosition &IRP, Attributor &A)
2914       : AAIsDeadValueImpl(IRP, A) {}
2915 
2916   /// See AbstractAttribute::initialize(...).
2917   void initialize(Attributor &A) override {
2918     if (isa<UndefValue>(getAssociatedValue())) {
2919       indicatePessimisticFixpoint();
2920       return;
2921     }
2922 
2923     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
2924     if (!isAssumedSideEffectFree(A, I))
2925       indicatePessimisticFixpoint();
2926   }
2927 
2928   /// See AbstractAttribute::updateImpl(...).
2929   ChangeStatus updateImpl(Attributor &A) override {
2930     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
2931     if (!isAssumedSideEffectFree(A, I))
2932       return indicatePessimisticFixpoint();
2933 
2934     if (!areAllUsesAssumedDead(A, getAssociatedValue()))
2935       return indicatePessimisticFixpoint();
2936     return ChangeStatus::UNCHANGED;
2937   }
2938 
2939   /// See AbstractAttribute::manifest(...).
2940   ChangeStatus manifest(Attributor &A) override {
2941     Value &V = getAssociatedValue();
2942     if (auto *I = dyn_cast<Instruction>(&V)) {
2943       // If we get here we basically know the users are all dead. We check if
2944       // isAssumedSideEffectFree returns true here again because it might not be
2945       // the case and only the users are dead but the instruction (=call) is
2946       // still needed.
2947       if (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I)) {
2948         A.deleteAfterManifest(*I);
2949         return ChangeStatus::CHANGED;
2950       }
2951     }
2952     if (V.use_empty())
2953       return ChangeStatus::UNCHANGED;
2954 
2955     bool UsedAssumedInformation = false;
2956     Optional<Constant *> C =
2957         A.getAssumedConstant(V, *this, UsedAssumedInformation);
2958     if (C.hasValue() && C.getValue())
2959       return ChangeStatus::UNCHANGED;
2960 
2961     // Replace the value with undef as it is dead but keep droppable uses around
2962     // as they provide information we don't want to give up on just yet.
2963     UndefValue &UV = *UndefValue::get(V.getType());
2964     bool AnyChange =
2965         A.changeValueAfterManifest(V, UV, /* ChangeDropppable */ false);
2966     return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
2967   }
2968 
2969   /// See AbstractAttribute::trackStatistics()
2970   void trackStatistics() const override {
2971     STATS_DECLTRACK_FLOATING_ATTR(IsDead)
2972   }
2973 };
2974 
2975 struct AAIsDeadArgument : public AAIsDeadFloating {
2976   AAIsDeadArgument(const IRPosition &IRP, Attributor &A)
2977       : AAIsDeadFloating(IRP, A) {}
2978 
2979   /// See AbstractAttribute::initialize(...).
2980   void initialize(Attributor &A) override {
2981     if (!A.isFunctionIPOAmendable(*getAnchorScope()))
2982       indicatePessimisticFixpoint();
2983   }
2984 
2985   /// See AbstractAttribute::manifest(...).
2986   ChangeStatus manifest(Attributor &A) override {
2987     ChangeStatus Changed = AAIsDeadFloating::manifest(A);
2988     Argument &Arg = *getAssociatedArgument();
2989     if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
2990       if (A.registerFunctionSignatureRewrite(
2991               Arg, /* ReplacementTypes */ {},
2992               Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
2993               Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) {
2994         Arg.dropDroppableUses();
2995         return ChangeStatus::CHANGED;
2996       }
2997     return Changed;
2998   }
2999 
3000   /// See AbstractAttribute::trackStatistics()
3001   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) }
3002 };
3003 
3004 struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
3005   AAIsDeadCallSiteArgument(const IRPosition &IRP, Attributor &A)
3006       : AAIsDeadValueImpl(IRP, A) {}
3007 
3008   /// See AbstractAttribute::initialize(...).
3009   void initialize(Attributor &A) override {
3010     if (isa<UndefValue>(getAssociatedValue()))
3011       indicatePessimisticFixpoint();
3012   }
3013 
3014   /// See AbstractAttribute::updateImpl(...).
3015   ChangeStatus updateImpl(Attributor &A) override {
3016     // TODO: Once we have call site specific value information we can provide
3017     //       call site specific liveness information and then it makes
3018     //       sense to specialize attributes for call sites arguments instead of
3019     //       redirecting requests to the callee argument.
3020     Argument *Arg = getAssociatedArgument();
3021     if (!Arg)
3022       return indicatePessimisticFixpoint();
3023     const IRPosition &ArgPos = IRPosition::argument(*Arg);
3024     auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos, DepClassTy::REQUIRED);
3025     return clampStateAndIndicateChange(getState(), ArgAA.getState());
3026   }
3027 
3028   /// See AbstractAttribute::manifest(...).
3029   ChangeStatus manifest(Attributor &A) override {
3030     CallBase &CB = cast<CallBase>(getAnchorValue());
3031     Use &U = CB.getArgOperandUse(getCallSiteArgNo());
3032     assert(!isa<UndefValue>(U.get()) &&
3033            "Expected undef values to be filtered out!");
3034     UndefValue &UV = *UndefValue::get(U->getType());
3035     if (A.changeUseAfterManifest(U, UV))
3036       return ChangeStatus::CHANGED;
3037     return ChangeStatus::UNCHANGED;
3038   }
3039 
3040   /// See AbstractAttribute::trackStatistics()
3041   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) }
3042 };
3043 
3044 struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
3045   AAIsDeadCallSiteReturned(const IRPosition &IRP, Attributor &A)
3046       : AAIsDeadFloating(IRP, A), IsAssumedSideEffectFree(true) {}
3047 
3048   /// See AAIsDead::isAssumedDead().
3049   bool isAssumedDead() const override {
3050     return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
3051   }
3052 
3053   /// See AbstractAttribute::initialize(...).
3054   void initialize(Attributor &A) override {
3055     if (isa<UndefValue>(getAssociatedValue())) {
3056       indicatePessimisticFixpoint();
3057       return;
3058     }
3059 
3060     // We track this separately as a secondary state.
3061     IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
3062   }
3063 
3064   /// See AbstractAttribute::updateImpl(...).
3065   ChangeStatus updateImpl(Attributor &A) override {
3066     ChangeStatus Changed = ChangeStatus::UNCHANGED;
3067     if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
3068       IsAssumedSideEffectFree = false;
3069       Changed = ChangeStatus::CHANGED;
3070     }
3071 
3072     if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3073       return indicatePessimisticFixpoint();
3074     return Changed;
3075   }
3076 
3077   /// See AbstractAttribute::trackStatistics()
3078   void trackStatistics() const override {
3079     if (IsAssumedSideEffectFree)
3080       STATS_DECLTRACK_CSRET_ATTR(IsDead)
3081     else
3082       STATS_DECLTRACK_CSRET_ATTR(UnusedResult)
3083   }
3084 
3085   /// See AbstractAttribute::getAsStr().
3086   const std::string getAsStr() const override {
3087     return isAssumedDead()
3088                ? "assumed-dead"
3089                : (getAssumed() ? "assumed-dead-users" : "assumed-live");
3090   }
3091 
3092 private:
3093   bool IsAssumedSideEffectFree;
3094 };
3095 
3096 struct AAIsDeadReturned : public AAIsDeadValueImpl {
3097   AAIsDeadReturned(const IRPosition &IRP, Attributor &A)
3098       : AAIsDeadValueImpl(IRP, A) {}
3099 
3100   /// See AbstractAttribute::updateImpl(...).
3101   ChangeStatus updateImpl(Attributor &A) override {
3102 
3103     A.checkForAllInstructions([](Instruction &) { return true; }, *this,
3104                               {Instruction::Ret});
3105 
3106     auto PredForCallSite = [&](AbstractCallSite ACS) {
3107       if (ACS.isCallbackCall() || !ACS.getInstruction())
3108         return false;
3109       return areAllUsesAssumedDead(A, *ACS.getInstruction());
3110     };
3111 
3112     bool AllCallSitesKnown;
3113     if (!A.checkForAllCallSites(PredForCallSite, *this, true,
3114                                 AllCallSitesKnown))
3115       return indicatePessimisticFixpoint();
3116 
3117     return ChangeStatus::UNCHANGED;
3118   }
3119 
3120   /// See AbstractAttribute::manifest(...).
3121   ChangeStatus manifest(Attributor &A) override {
3122     // TODO: Rewrite the signature to return void?
3123     bool AnyChange = false;
3124     UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
3125     auto RetInstPred = [&](Instruction &I) {
3126       ReturnInst &RI = cast<ReturnInst>(I);
3127       if (!isa<UndefValue>(RI.getReturnValue()))
3128         AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
3129       return true;
3130     };
3131     A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret});
3132     return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3133   }
3134 
3135   /// See AbstractAttribute::trackStatistics()
3136   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) }
3137 };
3138 
3139 struct AAIsDeadFunction : public AAIsDead {
3140   AAIsDeadFunction(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
3141 
3142   /// See AbstractAttribute::initialize(...).
3143   void initialize(Attributor &A) override {
3144     const Function *F = getAnchorScope();
3145     if (F && !F->isDeclaration()) {
3146       // We only want to compute liveness once. If the function is not part of
3147       // the SCC, skip it.
3148       if (A.isRunOn(*const_cast<Function *>(F))) {
3149         ToBeExploredFrom.insert(&F->getEntryBlock().front());
3150         assumeLive(A, F->getEntryBlock());
3151       } else {
3152         indicatePessimisticFixpoint();
3153       }
3154     }
3155   }
3156 
3157   /// See AbstractAttribute::getAsStr().
3158   const std::string getAsStr() const override {
3159     return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
3160            std::to_string(getAnchorScope()->size()) + "][#TBEP " +
3161            std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
3162            std::to_string(KnownDeadEnds.size()) + "]";
3163   }
3164 
3165   /// See AbstractAttribute::manifest(...).
3166   ChangeStatus manifest(Attributor &A) override {
3167     assert(getState().isValidState() &&
3168            "Attempted to manifest an invalid state!");
3169 
3170     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
3171     Function &F = *getAnchorScope();
3172 
3173     if (AssumedLiveBlocks.empty()) {
3174       A.deleteAfterManifest(F);
3175       return ChangeStatus::CHANGED;
3176     }
3177 
3178     // Flag to determine if we can change an invoke to a call assuming the
3179     // callee is nounwind. This is not possible if the personality of the
3180     // function allows to catch asynchronous exceptions.
3181     bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
3182 
3183     KnownDeadEnds.set_union(ToBeExploredFrom);
3184     for (const Instruction *DeadEndI : KnownDeadEnds) {
3185       auto *CB = dyn_cast<CallBase>(DeadEndI);
3186       if (!CB)
3187         continue;
3188       const auto &NoReturnAA = A.getAndUpdateAAFor<AANoReturn>(
3189           *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
3190       bool MayReturn = !NoReturnAA.isAssumedNoReturn();
3191       if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
3192         continue;
3193 
3194       if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
3195         A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
3196       else
3197         A.changeToUnreachableAfterManifest(
3198             const_cast<Instruction *>(DeadEndI->getNextNode()));
3199       HasChanged = ChangeStatus::CHANGED;
3200     }
3201 
3202     STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.");
3203     for (BasicBlock &BB : F)
3204       if (!AssumedLiveBlocks.count(&BB)) {
3205         A.deleteAfterManifest(BB);
3206         ++BUILD_STAT_NAME(AAIsDead, BasicBlock);
3207       }
3208 
3209     return HasChanged;
3210   }
3211 
3212   /// See AbstractAttribute::updateImpl(...).
3213   ChangeStatus updateImpl(Attributor &A) override;
3214 
3215   bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const override {
3216     return !AssumedLiveEdges.count(std::make_pair(From, To));
3217   }
3218 
3219   /// See AbstractAttribute::trackStatistics()
3220   void trackStatistics() const override {}
3221 
3222   /// Returns true if the function is assumed dead.
3223   bool isAssumedDead() const override { return false; }
3224 
3225   /// See AAIsDead::isKnownDead().
3226   bool isKnownDead() const override { return false; }
3227 
3228   /// See AAIsDead::isAssumedDead(BasicBlock *).
3229   bool isAssumedDead(const BasicBlock *BB) const override {
3230     assert(BB->getParent() == getAnchorScope() &&
3231            "BB must be in the same anchor scope function.");
3232 
3233     if (!getAssumed())
3234       return false;
3235     return !AssumedLiveBlocks.count(BB);
3236   }
3237 
3238   /// See AAIsDead::isKnownDead(BasicBlock *).
3239   bool isKnownDead(const BasicBlock *BB) const override {
3240     return getKnown() && isAssumedDead(BB);
3241   }
3242 
3243   /// See AAIsDead::isAssumed(Instruction *I).
3244   bool isAssumedDead(const Instruction *I) const override {
3245     assert(I->getParent()->getParent() == getAnchorScope() &&
3246            "Instruction must be in the same anchor scope function.");
3247 
3248     if (!getAssumed())
3249       return false;
3250 
3251     // If it is not in AssumedLiveBlocks then it for sure dead.
3252     // Otherwise, it can still be after noreturn call in a live block.
3253     if (!AssumedLiveBlocks.count(I->getParent()))
3254       return true;
3255 
3256     // If it is not after a liveness barrier it is live.
3257     const Instruction *PrevI = I->getPrevNode();
3258     while (PrevI) {
3259       if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
3260         return true;
3261       PrevI = PrevI->getPrevNode();
3262     }
3263     return false;
3264   }
3265 
3266   /// See AAIsDead::isKnownDead(Instruction *I).
3267   bool isKnownDead(const Instruction *I) const override {
3268     return getKnown() && isAssumedDead(I);
3269   }
3270 
3271   /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
3272   /// that internal function called from \p BB should now be looked at.
3273   bool assumeLive(Attributor &A, const BasicBlock &BB) {
3274     if (!AssumedLiveBlocks.insert(&BB).second)
3275       return false;
3276 
3277     // We assume that all of BB is (probably) live now and if there are calls to
3278     // internal functions we will assume that those are now live as well. This
3279     // is a performance optimization for blocks with calls to a lot of internal
3280     // functions. It can however cause dead functions to be treated as live.
3281     for (const Instruction &I : BB)
3282       if (const auto *CB = dyn_cast<CallBase>(&I))
3283         if (const Function *F = CB->getCalledFunction())
3284           if (F->hasLocalLinkage())
3285             A.markLiveInternalFunction(*F);
3286     return true;
3287   }
3288 
3289   /// Collection of instructions that need to be explored again, e.g., we
3290   /// did assume they do not transfer control to (one of their) successors.
3291   SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
3292 
3293   /// Collection of instructions that are known to not transfer control.
3294   SmallSetVector<const Instruction *, 8> KnownDeadEnds;
3295 
3296   /// Collection of all assumed live edges
3297   DenseSet<std::pair<const BasicBlock *, const BasicBlock *>> AssumedLiveEdges;
3298 
3299   /// Collection of all assumed live BasicBlocks.
3300   DenseSet<const BasicBlock *> AssumedLiveBlocks;
3301 };
3302 
3303 static bool
3304 identifyAliveSuccessors(Attributor &A, const CallBase &CB,
3305                         AbstractAttribute &AA,
3306                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3307   const IRPosition &IPos = IRPosition::callsite_function(CB);
3308 
3309   const auto &NoReturnAA =
3310       A.getAndUpdateAAFor<AANoReturn>(AA, IPos, DepClassTy::OPTIONAL);
3311   if (NoReturnAA.isAssumedNoReturn())
3312     return !NoReturnAA.isKnownNoReturn();
3313   if (CB.isTerminator())
3314     AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
3315   else
3316     AliveSuccessors.push_back(CB.getNextNode());
3317   return false;
3318 }
3319 
3320 static bool
3321 identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
3322                         AbstractAttribute &AA,
3323                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3324   bool UsedAssumedInformation =
3325       identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
3326 
3327   // First, determine if we can change an invoke to a call assuming the
3328   // callee is nounwind. This is not possible if the personality of the
3329   // function allows to catch asynchronous exceptions.
3330   if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
3331     AliveSuccessors.push_back(&II.getUnwindDest()->front());
3332   } else {
3333     const IRPosition &IPos = IRPosition::callsite_function(II);
3334     const auto &AANoUnw =
3335         A.getAndUpdateAAFor<AANoUnwind>(AA, IPos, DepClassTy::OPTIONAL);
3336     if (AANoUnw.isAssumedNoUnwind()) {
3337       UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
3338     } else {
3339       AliveSuccessors.push_back(&II.getUnwindDest()->front());
3340     }
3341   }
3342   return UsedAssumedInformation;
3343 }
3344 
3345 static bool
3346 identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
3347                         AbstractAttribute &AA,
3348                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3349   bool UsedAssumedInformation = false;
3350   if (BI.getNumSuccessors() == 1) {
3351     AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3352   } else {
3353     Optional<ConstantInt *> CI = getAssumedConstantInt(
3354         A, *BI.getCondition(), AA, UsedAssumedInformation);
3355     if (!CI.hasValue()) {
3356       // No value yet, assume both edges are dead.
3357     } else if (CI.getValue()) {
3358       const BasicBlock *SuccBB =
3359           BI.getSuccessor(1 - CI.getValue()->getZExtValue());
3360       AliveSuccessors.push_back(&SuccBB->front());
3361     } else {
3362       AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3363       AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
3364       UsedAssumedInformation = false;
3365     }
3366   }
3367   return UsedAssumedInformation;
3368 }
3369 
3370 static bool
3371 identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
3372                         AbstractAttribute &AA,
3373                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3374   bool UsedAssumedInformation = false;
3375   Optional<ConstantInt *> CI =
3376       getAssumedConstantInt(A, *SI.getCondition(), AA, UsedAssumedInformation);
3377   if (!CI.hasValue()) {
3378     // No value yet, assume all edges are dead.
3379   } else if (CI.getValue()) {
3380     for (auto &CaseIt : SI.cases()) {
3381       if (CaseIt.getCaseValue() == CI.getValue()) {
3382         AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
3383         return UsedAssumedInformation;
3384       }
3385     }
3386     AliveSuccessors.push_back(&SI.getDefaultDest()->front());
3387     return UsedAssumedInformation;
3388   } else {
3389     for (const BasicBlock *SuccBB : successors(SI.getParent()))
3390       AliveSuccessors.push_back(&SuccBB->front());
3391   }
3392   return UsedAssumedInformation;
3393 }
3394 
3395 ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
3396   ChangeStatus Change = ChangeStatus::UNCHANGED;
3397 
3398   LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"
3399                     << getAnchorScope()->size() << "] BBs and "
3400                     << ToBeExploredFrom.size() << " exploration points and "
3401                     << KnownDeadEnds.size() << " known dead ends\n");
3402 
3403   // Copy and clear the list of instructions we need to explore from. It is
3404   // refilled with instructions the next update has to look at.
3405   SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
3406                                                ToBeExploredFrom.end());
3407   decltype(ToBeExploredFrom) NewToBeExploredFrom;
3408 
3409   SmallVector<const Instruction *, 8> AliveSuccessors;
3410   while (!Worklist.empty()) {
3411     const Instruction *I = Worklist.pop_back_val();
3412     LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n");
3413 
3414     // Fast forward for uninteresting instructions. We could look for UB here
3415     // though.
3416     while (!I->isTerminator() && !isa<CallBase>(I)) {
3417       Change = ChangeStatus::CHANGED;
3418       I = I->getNextNode();
3419     }
3420 
3421     AliveSuccessors.clear();
3422 
3423     bool UsedAssumedInformation = false;
3424     switch (I->getOpcode()) {
3425     // TODO: look for (assumed) UB to backwards propagate "deadness".
3426     default:
3427       assert(I->isTerminator() &&
3428              "Expected non-terminators to be handled already!");
3429       for (const BasicBlock *SuccBB : successors(I->getParent()))
3430         AliveSuccessors.push_back(&SuccBB->front());
3431       break;
3432     case Instruction::Call:
3433       UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
3434                                                        *this, AliveSuccessors);
3435       break;
3436     case Instruction::Invoke:
3437       UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
3438                                                        *this, AliveSuccessors);
3439       break;
3440     case Instruction::Br:
3441       UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
3442                                                        *this, AliveSuccessors);
3443       break;
3444     case Instruction::Switch:
3445       UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
3446                                                        *this, AliveSuccessors);
3447       break;
3448     }
3449 
3450     if (UsedAssumedInformation) {
3451       NewToBeExploredFrom.insert(I);
3452     } else {
3453       Change = ChangeStatus::CHANGED;
3454       if (AliveSuccessors.empty() ||
3455           (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors()))
3456         KnownDeadEnds.insert(I);
3457     }
3458 
3459     LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "
3460                       << AliveSuccessors.size() << " UsedAssumedInformation: "
3461                       << UsedAssumedInformation << "\n");
3462 
3463     for (const Instruction *AliveSuccessor : AliveSuccessors) {
3464       if (!I->isTerminator()) {
3465         assert(AliveSuccessors.size() == 1 &&
3466                "Non-terminator expected to have a single successor!");
3467         Worklist.push_back(AliveSuccessor);
3468       } else {
3469         // record the assumed live edge
3470         AssumedLiveEdges.insert(
3471             std::make_pair(I->getParent(), AliveSuccessor->getParent()));
3472         if (assumeLive(A, *AliveSuccessor->getParent()))
3473           Worklist.push_back(AliveSuccessor);
3474       }
3475     }
3476   }
3477 
3478   ToBeExploredFrom = std::move(NewToBeExploredFrom);
3479 
3480   // If we know everything is live there is no need to query for liveness.
3481   // Instead, indicating a pessimistic fixpoint will cause the state to be
3482   // "invalid" and all queries to be answered conservatively without lookups.
3483   // To be in this state we have to (1) finished the exploration and (3) not
3484   // discovered any non-trivial dead end and (2) not ruled unreachable code
3485   // dead.
3486   if (ToBeExploredFrom.empty() &&
3487       getAnchorScope()->size() == AssumedLiveBlocks.size() &&
3488       llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
3489         return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
3490       }))
3491     return indicatePessimisticFixpoint();
3492   return Change;
3493 }
3494 
3495 /// Liveness information for a call sites.
3496 struct AAIsDeadCallSite final : AAIsDeadFunction {
3497   AAIsDeadCallSite(const IRPosition &IRP, Attributor &A)
3498       : AAIsDeadFunction(IRP, A) {}
3499 
3500   /// See AbstractAttribute::initialize(...).
3501   void initialize(Attributor &A) override {
3502     // TODO: Once we have call site specific value information we can provide
3503     //       call site specific liveness information and then it makes
3504     //       sense to specialize attributes for call sites instead of
3505     //       redirecting requests to the callee.
3506     llvm_unreachable("Abstract attributes for liveness are not "
3507                      "supported for call sites yet!");
3508   }
3509 
3510   /// See AbstractAttribute::updateImpl(...).
3511   ChangeStatus updateImpl(Attributor &A) override {
3512     return indicatePessimisticFixpoint();
3513   }
3514 
3515   /// See AbstractAttribute::trackStatistics()
3516   void trackStatistics() const override {}
3517 };
3518 
3519 /// -------------------- Dereferenceable Argument Attribute --------------------
3520 
3521 template <>
3522 ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
3523                                                      const DerefState &R) {
3524   ChangeStatus CS0 =
3525       clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
3526   ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
3527   return CS0 | CS1;
3528 }
3529 
3530 struct AADereferenceableImpl : AADereferenceable {
3531   AADereferenceableImpl(const IRPosition &IRP, Attributor &A)
3532       : AADereferenceable(IRP, A) {}
3533   using StateType = DerefState;
3534 
3535   /// See AbstractAttribute::initialize(...).
3536   void initialize(Attributor &A) override {
3537     SmallVector<Attribute, 4> Attrs;
3538     getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
3539              Attrs, /* IgnoreSubsumingPositions */ false, &A);
3540     for (const Attribute &Attr : Attrs)
3541       takeKnownDerefBytesMaximum(Attr.getValueAsInt());
3542 
3543     const IRPosition &IRP = this->getIRPosition();
3544     NonNullAA = &A.getAAFor<AANonNull>(*this, IRP, DepClassTy::NONE);
3545 
3546     bool CanBeNull, CanBeFreed;
3547     takeKnownDerefBytesMaximum(
3548         IRP.getAssociatedValue().getPointerDereferenceableBytes(
3549             A.getDataLayout(), CanBeNull, CanBeFreed));
3550 
3551     bool IsFnInterface = IRP.isFnInterfaceKind();
3552     Function *FnScope = IRP.getAnchorScope();
3553     if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope))) {
3554       indicatePessimisticFixpoint();
3555       return;
3556     }
3557 
3558     if (Instruction *CtxI = getCtxI())
3559       followUsesInMBEC(*this, A, getState(), *CtxI);
3560   }
3561 
3562   /// See AbstractAttribute::getState()
3563   /// {
3564   StateType &getState() override { return *this; }
3565   const StateType &getState() const override { return *this; }
3566   /// }
3567 
3568   /// Helper function for collecting accessed bytes in must-be-executed-context
3569   void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I,
3570                               DerefState &State) {
3571     const Value *UseV = U->get();
3572     if (!UseV->getType()->isPointerTy())
3573       return;
3574 
3575     Type *PtrTy = UseV->getType();
3576     const DataLayout &DL = A.getDataLayout();
3577     int64_t Offset;
3578     if (const Value *Base = getBasePointerOfAccessPointerOperand(
3579             I, Offset, DL, /*AllowNonInbounds*/ true)) {
3580       if (Base == &getAssociatedValue() &&
3581           getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
3582         uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType());
3583         State.addAccessedBytes(Offset, Size);
3584       }
3585     }
3586   }
3587 
3588   /// See followUsesInMBEC
3589   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
3590                        AADereferenceable::StateType &State) {
3591     bool IsNonNull = false;
3592     bool TrackUse = false;
3593     int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
3594         A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
3595     LLVM_DEBUG(dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytes
3596                       << " for instruction " << *I << "\n");
3597 
3598     addAccessedBytesForUse(A, U, I, State);
3599     State.takeKnownDerefBytesMaximum(DerefBytes);
3600     return TrackUse;
3601   }
3602 
3603   /// See AbstractAttribute::manifest(...).
3604   ChangeStatus manifest(Attributor &A) override {
3605     ChangeStatus Change = AADereferenceable::manifest(A);
3606     if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
3607       removeAttrs({Attribute::DereferenceableOrNull});
3608       return ChangeStatus::CHANGED;
3609     }
3610     return Change;
3611   }
3612 
3613   void getDeducedAttributes(LLVMContext &Ctx,
3614                             SmallVectorImpl<Attribute> &Attrs) const override {
3615     // TODO: Add *_globally support
3616     if (isAssumedNonNull())
3617       Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
3618           Ctx, getAssumedDereferenceableBytes()));
3619     else
3620       Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
3621           Ctx, getAssumedDereferenceableBytes()));
3622   }
3623 
3624   /// See AbstractAttribute::getAsStr().
3625   const std::string getAsStr() const override {
3626     if (!getAssumedDereferenceableBytes())
3627       return "unknown-dereferenceable";
3628     return std::string("dereferenceable") +
3629            (isAssumedNonNull() ? "" : "_or_null") +
3630            (isAssumedGlobal() ? "_globally" : "") + "<" +
3631            std::to_string(getKnownDereferenceableBytes()) + "-" +
3632            std::to_string(getAssumedDereferenceableBytes()) + ">";
3633   }
3634 };
3635 
3636 /// Dereferenceable attribute for a floating value.
3637 struct AADereferenceableFloating : AADereferenceableImpl {
3638   AADereferenceableFloating(const IRPosition &IRP, Attributor &A)
3639       : AADereferenceableImpl(IRP, A) {}
3640 
3641   /// See AbstractAttribute::updateImpl(...).
3642   ChangeStatus updateImpl(Attributor &A) override {
3643     const DataLayout &DL = A.getDataLayout();
3644 
3645     auto VisitValueCB = [&](const Value &V, const Instruction *, DerefState &T,
3646                             bool Stripped) -> bool {
3647       unsigned IdxWidth =
3648           DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
3649       APInt Offset(IdxWidth, 0);
3650       const Value *Base =
3651           stripAndAccumulateMinimalOffsets(A, *this, &V, DL, Offset, false);
3652 
3653       const auto &AA = A.getAAFor<AADereferenceable>(
3654           *this, IRPosition::value(*Base), DepClassTy::REQUIRED);
3655       int64_t DerefBytes = 0;
3656       if (!Stripped && this == &AA) {
3657         // Use IR information if we did not strip anything.
3658         // TODO: track globally.
3659         bool CanBeNull, CanBeFreed;
3660         DerefBytes =
3661           Base->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
3662         T.GlobalState.indicatePessimisticFixpoint();
3663       } else {
3664         const DerefState &DS = AA.getState();
3665         DerefBytes = DS.DerefBytesState.getAssumed();
3666         T.GlobalState &= DS.GlobalState;
3667       }
3668 
3669       // For now we do not try to "increase" dereferenceability due to negative
3670       // indices as we first have to come up with code to deal with loops and
3671       // for overflows of the dereferenceable bytes.
3672       int64_t OffsetSExt = Offset.getSExtValue();
3673       if (OffsetSExt < 0)
3674         OffsetSExt = 0;
3675 
3676       T.takeAssumedDerefBytesMinimum(
3677           std::max(int64_t(0), DerefBytes - OffsetSExt));
3678 
3679       if (this == &AA) {
3680         if (!Stripped) {
3681           // If nothing was stripped IR information is all we got.
3682           T.takeKnownDerefBytesMaximum(
3683               std::max(int64_t(0), DerefBytes - OffsetSExt));
3684           T.indicatePessimisticFixpoint();
3685         } else if (OffsetSExt > 0) {
3686           // If something was stripped but there is circular reasoning we look
3687           // for the offset. If it is positive we basically decrease the
3688           // dereferenceable bytes in a circluar loop now, which will simply
3689           // drive them down to the known value in a very slow way which we
3690           // can accelerate.
3691           T.indicatePessimisticFixpoint();
3692         }
3693       }
3694 
3695       return T.isValidState();
3696     };
3697 
3698     DerefState T;
3699     if (!genericValueTraversal<AADereferenceable, DerefState>(
3700             A, getIRPosition(), *this, T, VisitValueCB, getCtxI()))
3701       return indicatePessimisticFixpoint();
3702 
3703     return clampStateAndIndicateChange(getState(), T);
3704   }
3705 
3706   /// See AbstractAttribute::trackStatistics()
3707   void trackStatistics() const override {
3708     STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
3709   }
3710 };
3711 
3712 /// Dereferenceable attribute for a return value.
3713 struct AADereferenceableReturned final
3714     : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
3715   AADereferenceableReturned(const IRPosition &IRP, Attributor &A)
3716       : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
3717             IRP, A) {}
3718 
3719   /// See AbstractAttribute::trackStatistics()
3720   void trackStatistics() const override {
3721     STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
3722   }
3723 };
3724 
3725 /// Dereferenceable attribute for an argument
3726 struct AADereferenceableArgument final
3727     : AAArgumentFromCallSiteArguments<AADereferenceable,
3728                                       AADereferenceableImpl> {
3729   using Base =
3730       AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl>;
3731   AADereferenceableArgument(const IRPosition &IRP, Attributor &A)
3732       : Base(IRP, A) {}
3733 
3734   /// See AbstractAttribute::trackStatistics()
3735   void trackStatistics() const override {
3736     STATS_DECLTRACK_ARG_ATTR(dereferenceable)
3737   }
3738 };
3739 
3740 /// Dereferenceable attribute for a call site argument.
3741 struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
3742   AADereferenceableCallSiteArgument(const IRPosition &IRP, Attributor &A)
3743       : AADereferenceableFloating(IRP, A) {}
3744 
3745   /// See AbstractAttribute::trackStatistics()
3746   void trackStatistics() const override {
3747     STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
3748   }
3749 };
3750 
3751 /// Dereferenceable attribute deduction for a call site return value.
3752 struct AADereferenceableCallSiteReturned final
3753     : AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl> {
3754   using Base =
3755       AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl>;
3756   AADereferenceableCallSiteReturned(const IRPosition &IRP, Attributor &A)
3757       : Base(IRP, A) {}
3758 
3759   /// See AbstractAttribute::trackStatistics()
3760   void trackStatistics() const override {
3761     STATS_DECLTRACK_CS_ATTR(dereferenceable);
3762   }
3763 };
3764 
3765 // ------------------------ Align Argument Attribute ------------------------
3766 
3767 static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA,
3768                                     Value &AssociatedValue, const Use *U,
3769                                     const Instruction *I, bool &TrackUse) {
3770   // We need to follow common pointer manipulation uses to the accesses they
3771   // feed into.
3772   if (isa<CastInst>(I)) {
3773     // Follow all but ptr2int casts.
3774     TrackUse = !isa<PtrToIntInst>(I);
3775     return 0;
3776   }
3777   if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
3778     if (GEP->hasAllConstantIndices())
3779       TrackUse = true;
3780     return 0;
3781   }
3782 
3783   MaybeAlign MA;
3784   if (const auto *CB = dyn_cast<CallBase>(I)) {
3785     if (CB->isBundleOperand(U) || CB->isCallee(U))
3786       return 0;
3787 
3788     unsigned ArgNo = CB->getArgOperandNo(U);
3789     IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
3790     // As long as we only use known information there is no need to track
3791     // dependences here.
3792     auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, DepClassTy::NONE);
3793     MA = MaybeAlign(AlignAA.getKnownAlign());
3794   }
3795 
3796   const DataLayout &DL = A.getDataLayout();
3797   const Value *UseV = U->get();
3798   if (auto *SI = dyn_cast<StoreInst>(I)) {
3799     if (SI->getPointerOperand() == UseV)
3800       MA = SI->getAlign();
3801   } else if (auto *LI = dyn_cast<LoadInst>(I)) {
3802     if (LI->getPointerOperand() == UseV)
3803       MA = LI->getAlign();
3804   }
3805 
3806   if (!MA || *MA <= QueryingAA.getKnownAlign())
3807     return 0;
3808 
3809   unsigned Alignment = MA->value();
3810   int64_t Offset;
3811 
3812   if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
3813     if (Base == &AssociatedValue) {
3814       // BasePointerAddr + Offset = Alignment * Q for some integer Q.
3815       // So we can say that the maximum power of two which is a divisor of
3816       // gcd(Offset, Alignment) is an alignment.
3817 
3818       uint32_t gcd =
3819           greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
3820       Alignment = llvm::PowerOf2Floor(gcd);
3821     }
3822   }
3823 
3824   return Alignment;
3825 }
3826 
3827 struct AAAlignImpl : AAAlign {
3828   AAAlignImpl(const IRPosition &IRP, Attributor &A) : AAAlign(IRP, A) {}
3829 
3830   /// See AbstractAttribute::initialize(...).
3831   void initialize(Attributor &A) override {
3832     SmallVector<Attribute, 4> Attrs;
3833     getAttrs({Attribute::Alignment}, Attrs);
3834     for (const Attribute &Attr : Attrs)
3835       takeKnownMaximum(Attr.getValueAsInt());
3836 
3837     Value &V = getAssociatedValue();
3838     // TODO: This is a HACK to avoid getPointerAlignment to introduce a ptr2int
3839     //       use of the function pointer. This was caused by D73131. We want to
3840     //       avoid this for function pointers especially because we iterate
3841     //       their uses and int2ptr is not handled. It is not a correctness
3842     //       problem though!
3843     if (!V.getType()->getPointerElementType()->isFunctionTy())
3844       takeKnownMaximum(V.getPointerAlignment(A.getDataLayout()).value());
3845 
3846     if (getIRPosition().isFnInterfaceKind() &&
3847         (!getAnchorScope() ||
3848          !A.isFunctionIPOAmendable(*getAssociatedFunction()))) {
3849       indicatePessimisticFixpoint();
3850       return;
3851     }
3852 
3853     if (Instruction *CtxI = getCtxI())
3854       followUsesInMBEC(*this, A, getState(), *CtxI);
3855   }
3856 
3857   /// See AbstractAttribute::manifest(...).
3858   ChangeStatus manifest(Attributor &A) override {
3859     ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
3860 
3861     // Check for users that allow alignment annotations.
3862     Value &AssociatedValue = getAssociatedValue();
3863     for (const Use &U : AssociatedValue.uses()) {
3864       if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
3865         if (SI->getPointerOperand() == &AssociatedValue)
3866           if (SI->getAlignment() < getAssumedAlign()) {
3867             STATS_DECLTRACK(AAAlign, Store,
3868                             "Number of times alignment added to a store");
3869             SI->setAlignment(Align(getAssumedAlign()));
3870             LoadStoreChanged = ChangeStatus::CHANGED;
3871           }
3872       } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
3873         if (LI->getPointerOperand() == &AssociatedValue)
3874           if (LI->getAlignment() < getAssumedAlign()) {
3875             LI->setAlignment(Align(getAssumedAlign()));
3876             STATS_DECLTRACK(AAAlign, Load,
3877                             "Number of times alignment added to a load");
3878             LoadStoreChanged = ChangeStatus::CHANGED;
3879           }
3880       }
3881     }
3882 
3883     ChangeStatus Changed = AAAlign::manifest(A);
3884 
3885     Align InheritAlign =
3886         getAssociatedValue().getPointerAlignment(A.getDataLayout());
3887     if (InheritAlign >= getAssumedAlign())
3888       return LoadStoreChanged;
3889     return Changed | LoadStoreChanged;
3890   }
3891 
3892   // TODO: Provide a helper to determine the implied ABI alignment and check in
3893   //       the existing manifest method and a new one for AAAlignImpl that value
3894   //       to avoid making the alignment explicit if it did not improve.
3895 
3896   /// See AbstractAttribute::getDeducedAttributes
3897   virtual void
3898   getDeducedAttributes(LLVMContext &Ctx,
3899                        SmallVectorImpl<Attribute> &Attrs) const override {
3900     if (getAssumedAlign() > 1)
3901       Attrs.emplace_back(
3902           Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
3903   }
3904 
3905   /// See followUsesInMBEC
3906   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
3907                        AAAlign::StateType &State) {
3908     bool TrackUse = false;
3909 
3910     unsigned int KnownAlign =
3911         getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
3912     State.takeKnownMaximum(KnownAlign);
3913 
3914     return TrackUse;
3915   }
3916 
3917   /// See AbstractAttribute::getAsStr().
3918   const std::string getAsStr() const override {
3919     return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
3920                                 "-" + std::to_string(getAssumedAlign()) + ">")
3921                              : "unknown-align";
3922   }
3923 };
3924 
3925 /// Align attribute for a floating value.
3926 struct AAAlignFloating : AAAlignImpl {
3927   AAAlignFloating(const IRPosition &IRP, Attributor &A) : AAAlignImpl(IRP, A) {}
3928 
3929   /// See AbstractAttribute::updateImpl(...).
3930   ChangeStatus updateImpl(Attributor &A) override {
3931     const DataLayout &DL = A.getDataLayout();
3932 
3933     auto VisitValueCB = [&](Value &V, const Instruction *,
3934                             AAAlign::StateType &T, bool Stripped) -> bool {
3935       const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V),
3936                                            DepClassTy::REQUIRED);
3937       if (!Stripped && this == &AA) {
3938         int64_t Offset;
3939         unsigned Alignment = 1;
3940         if (const Value *Base =
3941                 GetPointerBaseWithConstantOffset(&V, Offset, DL)) {
3942           Align PA = Base->getPointerAlignment(DL);
3943           // BasePointerAddr + Offset = Alignment * Q for some integer Q.
3944           // So we can say that the maximum power of two which is a divisor of
3945           // gcd(Offset, Alignment) is an alignment.
3946 
3947           uint32_t gcd = greatestCommonDivisor(uint32_t(abs((int32_t)Offset)),
3948                                                uint32_t(PA.value()));
3949           Alignment = llvm::PowerOf2Floor(gcd);
3950         } else {
3951           Alignment = V.getPointerAlignment(DL).value();
3952         }
3953         // Use only IR information if we did not strip anything.
3954         T.takeKnownMaximum(Alignment);
3955         T.indicatePessimisticFixpoint();
3956       } else {
3957         // Use abstract attribute information.
3958         const AAAlign::StateType &DS = AA.getState();
3959         T ^= DS;
3960       }
3961       return T.isValidState();
3962     };
3963 
3964     StateType T;
3965     if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T,
3966                                                    VisitValueCB, getCtxI()))
3967       return indicatePessimisticFixpoint();
3968 
3969     // TODO: If we know we visited all incoming values, thus no are assumed
3970     // dead, we can take the known information from the state T.
3971     return clampStateAndIndicateChange(getState(), T);
3972   }
3973 
3974   /// See AbstractAttribute::trackStatistics()
3975   void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
3976 };
3977 
3978 /// Align attribute for function return value.
3979 struct AAAlignReturned final
3980     : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
3981   using Base = AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>;
3982   AAAlignReturned(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
3983 
3984   /// See AbstractAttribute::initialize(...).
3985   void initialize(Attributor &A) override {
3986     Base::initialize(A);
3987     Function *F = getAssociatedFunction();
3988     if (!F || F->isDeclaration())
3989       indicatePessimisticFixpoint();
3990   }
3991 
3992   /// See AbstractAttribute::trackStatistics()
3993   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
3994 };
3995 
3996 /// Align attribute for function argument.
3997 struct AAAlignArgument final
3998     : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
3999   using Base = AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>;
4000   AAAlignArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
4001 
4002   /// See AbstractAttribute::manifest(...).
4003   ChangeStatus manifest(Attributor &A) override {
4004     // If the associated argument is involved in a must-tail call we give up
4005     // because we would need to keep the argument alignments of caller and
4006     // callee in-sync. Just does not seem worth the trouble right now.
4007     if (A.getInfoCache().isInvolvedInMustTailCall(*getAssociatedArgument()))
4008       return ChangeStatus::UNCHANGED;
4009     return Base::manifest(A);
4010   }
4011 
4012   /// See AbstractAttribute::trackStatistics()
4013   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
4014 };
4015 
4016 struct AAAlignCallSiteArgument final : AAAlignFloating {
4017   AAAlignCallSiteArgument(const IRPosition &IRP, Attributor &A)
4018       : AAAlignFloating(IRP, A) {}
4019 
4020   /// See AbstractAttribute::manifest(...).
4021   ChangeStatus manifest(Attributor &A) override {
4022     // If the associated argument is involved in a must-tail call we give up
4023     // because we would need to keep the argument alignments of caller and
4024     // callee in-sync. Just does not seem worth the trouble right now.
4025     if (Argument *Arg = getAssociatedArgument())
4026       if (A.getInfoCache().isInvolvedInMustTailCall(*Arg))
4027         return ChangeStatus::UNCHANGED;
4028     ChangeStatus Changed = AAAlignImpl::manifest(A);
4029     Align InheritAlign =
4030         getAssociatedValue().getPointerAlignment(A.getDataLayout());
4031     if (InheritAlign >= getAssumedAlign())
4032       Changed = ChangeStatus::UNCHANGED;
4033     return Changed;
4034   }
4035 
4036   /// See AbstractAttribute::updateImpl(Attributor &A).
4037   ChangeStatus updateImpl(Attributor &A) override {
4038     ChangeStatus Changed = AAAlignFloating::updateImpl(A);
4039     if (Argument *Arg = getAssociatedArgument()) {
4040       // We only take known information from the argument
4041       // so we do not need to track a dependence.
4042       const auto &ArgAlignAA = A.getAAFor<AAAlign>(
4043           *this, IRPosition::argument(*Arg), DepClassTy::NONE);
4044       takeKnownMaximum(ArgAlignAA.getKnownAlign());
4045     }
4046     return Changed;
4047   }
4048 
4049   /// See AbstractAttribute::trackStatistics()
4050   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
4051 };
4052 
4053 /// Align attribute deduction for a call site return value.
4054 struct AAAlignCallSiteReturned final
4055     : AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl> {
4056   using Base = AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl>;
4057   AAAlignCallSiteReturned(const IRPosition &IRP, Attributor &A)
4058       : Base(IRP, A) {}
4059 
4060   /// See AbstractAttribute::initialize(...).
4061   void initialize(Attributor &A) override {
4062     Base::initialize(A);
4063     Function *F = getAssociatedFunction();
4064     if (!F || F->isDeclaration())
4065       indicatePessimisticFixpoint();
4066   }
4067 
4068   /// See AbstractAttribute::trackStatistics()
4069   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
4070 };
4071 
4072 /// ------------------ Function No-Return Attribute ----------------------------
4073 struct AANoReturnImpl : public AANoReturn {
4074   AANoReturnImpl(const IRPosition &IRP, Attributor &A) : AANoReturn(IRP, A) {}
4075 
4076   /// See AbstractAttribute::initialize(...).
4077   void initialize(Attributor &A) override {
4078     AANoReturn::initialize(A);
4079     Function *F = getAssociatedFunction();
4080     if (!F || F->isDeclaration())
4081       indicatePessimisticFixpoint();
4082   }
4083 
4084   /// See AbstractAttribute::getAsStr().
4085   const std::string getAsStr() const override {
4086     return getAssumed() ? "noreturn" : "may-return";
4087   }
4088 
4089   /// See AbstractAttribute::updateImpl(Attributor &A).
4090   virtual ChangeStatus updateImpl(Attributor &A) override {
4091     auto CheckForNoReturn = [](Instruction &) { return false; };
4092     if (!A.checkForAllInstructions(CheckForNoReturn, *this,
4093                                    {(unsigned)Instruction::Ret}))
4094       return indicatePessimisticFixpoint();
4095     return ChangeStatus::UNCHANGED;
4096   }
4097 };
4098 
4099 struct AANoReturnFunction final : AANoReturnImpl {
4100   AANoReturnFunction(const IRPosition &IRP, Attributor &A)
4101       : AANoReturnImpl(IRP, A) {}
4102 
4103   /// See AbstractAttribute::trackStatistics()
4104   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
4105 };
4106 
4107 /// NoReturn attribute deduction for a call sites.
4108 struct AANoReturnCallSite final : AANoReturnImpl {
4109   AANoReturnCallSite(const IRPosition &IRP, Attributor &A)
4110       : AANoReturnImpl(IRP, A) {}
4111 
4112   /// See AbstractAttribute::initialize(...).
4113   void initialize(Attributor &A) override {
4114     AANoReturnImpl::initialize(A);
4115     if (Function *F = getAssociatedFunction()) {
4116       const IRPosition &FnPos = IRPosition::function(*F);
4117       auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4118       if (!FnAA.isAssumedNoReturn())
4119         indicatePessimisticFixpoint();
4120     }
4121   }
4122 
4123   /// See AbstractAttribute::updateImpl(...).
4124   ChangeStatus updateImpl(Attributor &A) override {
4125     // TODO: Once we have call site specific value information we can provide
4126     //       call site specific liveness information and then it makes
4127     //       sense to specialize attributes for call sites arguments instead of
4128     //       redirecting requests to the callee argument.
4129     Function *F = getAssociatedFunction();
4130     const IRPosition &FnPos = IRPosition::function(*F);
4131     auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4132     return clampStateAndIndicateChange(getState(), FnAA.getState());
4133   }
4134 
4135   /// See AbstractAttribute::trackStatistics()
4136   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
4137 };
4138 
4139 /// ----------------------- Variable Capturing ---------------------------------
4140 
4141 /// A class to hold the state of for no-capture attributes.
4142 struct AANoCaptureImpl : public AANoCapture {
4143   AANoCaptureImpl(const IRPosition &IRP, Attributor &A) : AANoCapture(IRP, A) {}
4144 
4145   /// See AbstractAttribute::initialize(...).
4146   void initialize(Attributor &A) override {
4147     if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
4148       indicateOptimisticFixpoint();
4149       return;
4150     }
4151     Function *AnchorScope = getAnchorScope();
4152     if (isFnInterfaceKind() &&
4153         (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) {
4154       indicatePessimisticFixpoint();
4155       return;
4156     }
4157 
4158     // You cannot "capture" null in the default address space.
4159     if (isa<ConstantPointerNull>(getAssociatedValue()) &&
4160         getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
4161       indicateOptimisticFixpoint();
4162       return;
4163     }
4164 
4165     const Function *F =
4166         isArgumentPosition() ? getAssociatedFunction() : AnchorScope;
4167 
4168     // Check what state the associated function can actually capture.
4169     if (F)
4170       determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
4171     else
4172       indicatePessimisticFixpoint();
4173   }
4174 
4175   /// See AbstractAttribute::updateImpl(...).
4176   ChangeStatus updateImpl(Attributor &A) override;
4177 
4178   /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
4179   virtual void
4180   getDeducedAttributes(LLVMContext &Ctx,
4181                        SmallVectorImpl<Attribute> &Attrs) const override {
4182     if (!isAssumedNoCaptureMaybeReturned())
4183       return;
4184 
4185     if (isArgumentPosition()) {
4186       if (isAssumedNoCapture())
4187         Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
4188       else if (ManifestInternal)
4189         Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
4190     }
4191   }
4192 
4193   /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
4194   /// depending on the ability of the function associated with \p IRP to capture
4195   /// state in memory and through "returning/throwing", respectively.
4196   static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
4197                                                    const Function &F,
4198                                                    BitIntegerState &State) {
4199     // TODO: Once we have memory behavior attributes we should use them here.
4200 
4201     // If we know we cannot communicate or write to memory, we do not care about
4202     // ptr2int anymore.
4203     if (F.onlyReadsMemory() && F.doesNotThrow() &&
4204         F.getReturnType()->isVoidTy()) {
4205       State.addKnownBits(NO_CAPTURE);
4206       return;
4207     }
4208 
4209     // A function cannot capture state in memory if it only reads memory, it can
4210     // however return/throw state and the state might be influenced by the
4211     // pointer value, e.g., loading from a returned pointer might reveal a bit.
4212     if (F.onlyReadsMemory())
4213       State.addKnownBits(NOT_CAPTURED_IN_MEM);
4214 
4215     // A function cannot communicate state back if it does not through
4216     // exceptions and doesn not return values.
4217     if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
4218       State.addKnownBits(NOT_CAPTURED_IN_RET);
4219 
4220     // Check existing "returned" attributes.
4221     int ArgNo = IRP.getCalleeArgNo();
4222     if (F.doesNotThrow() && ArgNo >= 0) {
4223       for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
4224         if (F.hasParamAttribute(u, Attribute::Returned)) {
4225           if (u == unsigned(ArgNo))
4226             State.removeAssumedBits(NOT_CAPTURED_IN_RET);
4227           else if (F.onlyReadsMemory())
4228             State.addKnownBits(NO_CAPTURE);
4229           else
4230             State.addKnownBits(NOT_CAPTURED_IN_RET);
4231           break;
4232         }
4233     }
4234   }
4235 
4236   /// See AbstractState::getAsStr().
4237   const std::string getAsStr() const override {
4238     if (isKnownNoCapture())
4239       return "known not-captured";
4240     if (isAssumedNoCapture())
4241       return "assumed not-captured";
4242     if (isKnownNoCaptureMaybeReturned())
4243       return "known not-captured-maybe-returned";
4244     if (isAssumedNoCaptureMaybeReturned())
4245       return "assumed not-captured-maybe-returned";
4246     return "assumed-captured";
4247   }
4248 };
4249 
4250 /// Attributor-aware capture tracker.
4251 struct AACaptureUseTracker final : public CaptureTracker {
4252 
4253   /// Create a capture tracker that can lookup in-flight abstract attributes
4254   /// through the Attributor \p A.
4255   ///
4256   /// If a use leads to a potential capture, \p CapturedInMemory is set and the
4257   /// search is stopped. If a use leads to a return instruction,
4258   /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
4259   /// If a use leads to a ptr2int which may capture the value,
4260   /// \p CapturedInInteger is set. If a use is found that is currently assumed
4261   /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
4262   /// set. All values in \p PotentialCopies are later tracked as well. For every
4263   /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
4264   /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
4265   /// conservatively set to true.
4266   AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
4267                       const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
4268                       SmallVectorImpl<const Value *> &PotentialCopies,
4269                       unsigned &RemainingUsesToExplore)
4270       : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
4271         PotentialCopies(PotentialCopies),
4272         RemainingUsesToExplore(RemainingUsesToExplore) {}
4273 
4274   /// Determine if \p V maybe captured. *Also updates the state!*
4275   bool valueMayBeCaptured(const Value *V) {
4276     if (V->getType()->isPointerTy()) {
4277       PointerMayBeCaptured(V, this);
4278     } else {
4279       State.indicatePessimisticFixpoint();
4280     }
4281     return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4282   }
4283 
4284   /// See CaptureTracker::tooManyUses().
4285   void tooManyUses() override {
4286     State.removeAssumedBits(AANoCapture::NO_CAPTURE);
4287   }
4288 
4289   bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
4290     if (CaptureTracker::isDereferenceableOrNull(O, DL))
4291       return true;
4292     const auto &DerefAA = A.getAAFor<AADereferenceable>(
4293         NoCaptureAA, IRPosition::value(*O), DepClassTy::OPTIONAL);
4294     return DerefAA.getAssumedDereferenceableBytes();
4295   }
4296 
4297   /// See CaptureTracker::captured(...).
4298   bool captured(const Use *U) override {
4299     Instruction *UInst = cast<Instruction>(U->getUser());
4300     LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
4301                       << "\n");
4302 
4303     // Because we may reuse the tracker multiple times we keep track of the
4304     // number of explored uses ourselves as well.
4305     if (RemainingUsesToExplore-- == 0) {
4306       LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
4307       return isCapturedIn(/* Memory */ true, /* Integer */ true,
4308                           /* Return */ true);
4309     }
4310 
4311     // Deal with ptr2int by following uses.
4312     if (isa<PtrToIntInst>(UInst)) {
4313       LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
4314       return valueMayBeCaptured(UInst);
4315     }
4316 
4317     // Explicitly catch return instructions.
4318     if (isa<ReturnInst>(UInst))
4319       return isCapturedIn(/* Memory */ false, /* Integer */ false,
4320                           /* Return */ true);
4321 
4322     // For now we only use special logic for call sites. However, the tracker
4323     // itself knows about a lot of other non-capturing cases already.
4324     auto *CB = dyn_cast<CallBase>(UInst);
4325     if (!CB || !CB->isArgOperand(U))
4326       return isCapturedIn(/* Memory */ true, /* Integer */ true,
4327                           /* Return */ true);
4328 
4329     unsigned ArgNo = CB->getArgOperandNo(U);
4330     const IRPosition &CSArgPos = IRPosition::callsite_argument(*CB, ArgNo);
4331     // If we have a abstract no-capture attribute for the argument we can use
4332     // it to justify a non-capture attribute here. This allows recursion!
4333     auto &ArgNoCaptureAA =
4334         A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos, DepClassTy::REQUIRED);
4335     if (ArgNoCaptureAA.isAssumedNoCapture())
4336       return isCapturedIn(/* Memory */ false, /* Integer */ false,
4337                           /* Return */ false);
4338     if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
4339       addPotentialCopy(*CB);
4340       return isCapturedIn(/* Memory */ false, /* Integer */ false,
4341                           /* Return */ false);
4342     }
4343 
4344     // Lastly, we could not find a reason no-capture can be assumed so we don't.
4345     return isCapturedIn(/* Memory */ true, /* Integer */ true,
4346                         /* Return */ true);
4347   }
4348 
4349   /// Register \p CS as potential copy of the value we are checking.
4350   void addPotentialCopy(CallBase &CB) { PotentialCopies.push_back(&CB); }
4351 
4352   /// See CaptureTracker::shouldExplore(...).
4353   bool shouldExplore(const Use *U) override {
4354     // Check liveness and ignore droppable users.
4355     return !U->getUser()->isDroppable() &&
4356            !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA);
4357   }
4358 
4359   /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
4360   /// \p CapturedInRet, then return the appropriate value for use in the
4361   /// CaptureTracker::captured() interface.
4362   bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
4363                     bool CapturedInRet) {
4364     LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
4365                       << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
4366     if (CapturedInMem)
4367       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
4368     if (CapturedInInt)
4369       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
4370     if (CapturedInRet)
4371       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
4372     return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4373   }
4374 
4375 private:
4376   /// The attributor providing in-flight abstract attributes.
4377   Attributor &A;
4378 
4379   /// The abstract attribute currently updated.
4380   AANoCapture &NoCaptureAA;
4381 
4382   /// The abstract liveness state.
4383   const AAIsDead &IsDeadAA;
4384 
4385   /// The state currently updated.
4386   AANoCapture::StateType &State;
4387 
4388   /// Set of potential copies of the tracked value.
4389   SmallVectorImpl<const Value *> &PotentialCopies;
4390 
4391   /// Global counter to limit the number of explored uses.
4392   unsigned &RemainingUsesToExplore;
4393 };
4394 
4395 ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
4396   const IRPosition &IRP = getIRPosition();
4397   const Value *V = isArgumentPosition() ? IRP.getAssociatedArgument()
4398                                         : &IRP.getAssociatedValue();
4399   if (!V)
4400     return indicatePessimisticFixpoint();
4401 
4402   const Function *F =
4403       isArgumentPosition() ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
4404   assert(F && "Expected a function!");
4405   const IRPosition &FnPos = IRPosition::function(*F);
4406   const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos, DepClassTy::NONE);
4407 
4408   AANoCapture::StateType T;
4409 
4410   // Readonly means we cannot capture through memory.
4411   const auto &FnMemAA =
4412       A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::NONE);
4413   if (FnMemAA.isAssumedReadOnly()) {
4414     T.addKnownBits(NOT_CAPTURED_IN_MEM);
4415     if (FnMemAA.isKnownReadOnly())
4416       addKnownBits(NOT_CAPTURED_IN_MEM);
4417     else
4418       A.recordDependence(FnMemAA, *this, DepClassTy::OPTIONAL);
4419   }
4420 
4421   // Make sure all returned values are different than the underlying value.
4422   // TODO: we could do this in a more sophisticated way inside
4423   //       AAReturnedValues, e.g., track all values that escape through returns
4424   //       directly somehow.
4425   auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
4426     bool SeenConstant = false;
4427     for (auto &It : RVAA.returned_values()) {
4428       if (isa<Constant>(It.first)) {
4429         if (SeenConstant)
4430           return false;
4431         SeenConstant = true;
4432       } else if (!isa<Argument>(It.first) ||
4433                  It.first == getAssociatedArgument())
4434         return false;
4435     }
4436     return true;
4437   };
4438 
4439   const auto &NoUnwindAA =
4440       A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::OPTIONAL);
4441   if (NoUnwindAA.isAssumedNoUnwind()) {
4442     bool IsVoidTy = F->getReturnType()->isVoidTy();
4443     const AAReturnedValues *RVAA =
4444         IsVoidTy ? nullptr
4445                  : &A.getAAFor<AAReturnedValues>(*this, FnPos,
4446 
4447                                                  DepClassTy::OPTIONAL);
4448     if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
4449       T.addKnownBits(NOT_CAPTURED_IN_RET);
4450       if (T.isKnown(NOT_CAPTURED_IN_MEM))
4451         return ChangeStatus::UNCHANGED;
4452       if (NoUnwindAA.isKnownNoUnwind() &&
4453           (IsVoidTy || RVAA->getState().isAtFixpoint())) {
4454         addKnownBits(NOT_CAPTURED_IN_RET);
4455         if (isKnown(NOT_CAPTURED_IN_MEM))
4456           return indicateOptimisticFixpoint();
4457       }
4458     }
4459   }
4460 
4461   // Use the CaptureTracker interface and logic with the specialized tracker,
4462   // defined in AACaptureUseTracker, that can look at in-flight abstract
4463   // attributes and directly updates the assumed state.
4464   SmallVector<const Value *, 4> PotentialCopies;
4465   unsigned RemainingUsesToExplore =
4466       getDefaultMaxUsesToExploreForCaptureTracking();
4467   AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
4468                               RemainingUsesToExplore);
4469 
4470   // Check all potential copies of the associated value until we can assume
4471   // none will be captured or we have to assume at least one might be.
4472   unsigned Idx = 0;
4473   PotentialCopies.push_back(V);
4474   while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
4475     Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
4476 
4477   AANoCapture::StateType &S = getState();
4478   auto Assumed = S.getAssumed();
4479   S.intersectAssumedBits(T.getAssumed());
4480   if (!isAssumedNoCaptureMaybeReturned())
4481     return indicatePessimisticFixpoint();
4482   return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
4483                                    : ChangeStatus::CHANGED;
4484 }
4485 
4486 /// NoCapture attribute for function arguments.
4487 struct AANoCaptureArgument final : AANoCaptureImpl {
4488   AANoCaptureArgument(const IRPosition &IRP, Attributor &A)
4489       : AANoCaptureImpl(IRP, A) {}
4490 
4491   /// See AbstractAttribute::trackStatistics()
4492   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
4493 };
4494 
4495 /// NoCapture attribute for call site arguments.
4496 struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
4497   AANoCaptureCallSiteArgument(const IRPosition &IRP, Attributor &A)
4498       : AANoCaptureImpl(IRP, A) {}
4499 
4500   /// See AbstractAttribute::initialize(...).
4501   void initialize(Attributor &A) override {
4502     if (Argument *Arg = getAssociatedArgument())
4503       if (Arg->hasByValAttr())
4504         indicateOptimisticFixpoint();
4505     AANoCaptureImpl::initialize(A);
4506   }
4507 
4508   /// See AbstractAttribute::updateImpl(...).
4509   ChangeStatus updateImpl(Attributor &A) override {
4510     // TODO: Once we have call site specific value information we can provide
4511     //       call site specific liveness information and then it makes
4512     //       sense to specialize attributes for call sites arguments instead of
4513     //       redirecting requests to the callee argument.
4514     Argument *Arg = getAssociatedArgument();
4515     if (!Arg)
4516       return indicatePessimisticFixpoint();
4517     const IRPosition &ArgPos = IRPosition::argument(*Arg);
4518     auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos, DepClassTy::REQUIRED);
4519     return clampStateAndIndicateChange(getState(), ArgAA.getState());
4520   }
4521 
4522   /// See AbstractAttribute::trackStatistics()
4523   void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
4524 };
4525 
4526 /// NoCapture attribute for floating values.
4527 struct AANoCaptureFloating final : AANoCaptureImpl {
4528   AANoCaptureFloating(const IRPosition &IRP, Attributor &A)
4529       : AANoCaptureImpl(IRP, A) {}
4530 
4531   /// See AbstractAttribute::trackStatistics()
4532   void trackStatistics() const override {
4533     STATS_DECLTRACK_FLOATING_ATTR(nocapture)
4534   }
4535 };
4536 
4537 /// NoCapture attribute for function return value.
4538 struct AANoCaptureReturned final : AANoCaptureImpl {
4539   AANoCaptureReturned(const IRPosition &IRP, Attributor &A)
4540       : AANoCaptureImpl(IRP, A) {
4541     llvm_unreachable("NoCapture is not applicable to function returns!");
4542   }
4543 
4544   /// See AbstractAttribute::initialize(...).
4545   void initialize(Attributor &A) override {
4546     llvm_unreachable("NoCapture is not applicable to function returns!");
4547   }
4548 
4549   /// See AbstractAttribute::updateImpl(...).
4550   ChangeStatus updateImpl(Attributor &A) override {
4551     llvm_unreachable("NoCapture is not applicable to function returns!");
4552   }
4553 
4554   /// See AbstractAttribute::trackStatistics()
4555   void trackStatistics() const override {}
4556 };
4557 
4558 /// NoCapture attribute deduction for a call site return value.
4559 struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
4560   AANoCaptureCallSiteReturned(const IRPosition &IRP, Attributor &A)
4561       : AANoCaptureImpl(IRP, A) {}
4562 
4563   /// See AbstractAttribute::initialize(...).
4564   void initialize(Attributor &A) override {
4565     const Function *F = getAnchorScope();
4566     // Check what state the associated function can actually capture.
4567     determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
4568   }
4569 
4570   /// See AbstractAttribute::trackStatistics()
4571   void trackStatistics() const override {
4572     STATS_DECLTRACK_CSRET_ATTR(nocapture)
4573   }
4574 };
4575 
4576 /// ------------------ Value Simplify Attribute ----------------------------
4577 struct AAValueSimplifyImpl : AAValueSimplify {
4578   AAValueSimplifyImpl(const IRPosition &IRP, Attributor &A)
4579       : AAValueSimplify(IRP, A) {}
4580 
4581   /// See AbstractAttribute::initialize(...).
4582   void initialize(Attributor &A) override {
4583     if (getAssociatedValue().getType()->isVoidTy())
4584       indicatePessimisticFixpoint();
4585   }
4586 
4587   /// See AbstractAttribute::getAsStr().
4588   const std::string getAsStr() const override {
4589     return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple")
4590                         : "not-simple";
4591   }
4592 
4593   /// See AbstractAttribute::trackStatistics()
4594   void trackStatistics() const override {}
4595 
4596   /// See AAValueSimplify::getAssumedSimplifiedValue()
4597   Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
4598     if (!getAssumed())
4599       return const_cast<Value *>(&getAssociatedValue());
4600     return SimplifiedAssociatedValue;
4601   }
4602 
4603   /// Helper function for querying AAValueSimplify and updating candicate.
4604   /// \param QueryingValue Value trying to unify with SimplifiedValue
4605   /// \param AccumulatedSimplifiedValue Current simplification result.
4606   static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
4607                              Value &QueryingValue,
4608                              Optional<Value *> &AccumulatedSimplifiedValue) {
4609     // FIXME: Add a typecast support.
4610 
4611     auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
4612         QueryingAA, IRPosition::value(QueryingValue), DepClassTy::REQUIRED);
4613 
4614     Optional<Value *> QueryingValueSimplified =
4615         ValueSimplifyAA.getAssumedSimplifiedValue(A);
4616 
4617     if (!QueryingValueSimplified.hasValue())
4618       return true;
4619 
4620     if (!QueryingValueSimplified.getValue())
4621       return false;
4622 
4623     Value &QueryingValueSimplifiedUnwrapped =
4624         *QueryingValueSimplified.getValue();
4625 
4626     if (AccumulatedSimplifiedValue.hasValue() &&
4627         !isa<UndefValue>(AccumulatedSimplifiedValue.getValue()) &&
4628         !isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
4629       return AccumulatedSimplifiedValue == QueryingValueSimplified;
4630     if (AccumulatedSimplifiedValue.hasValue() &&
4631         isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
4632       return true;
4633 
4634     LLVM_DEBUG(dbgs() << "[ValueSimplify] " << QueryingValue
4635                       << " is assumed to be "
4636                       << QueryingValueSimplifiedUnwrapped << "\n");
4637 
4638     AccumulatedSimplifiedValue = QueryingValueSimplified;
4639     return true;
4640   }
4641 
4642   /// Returns a candidate is found or not
4643   template <typename AAType> bool askSimplifiedValueFor(Attributor &A) {
4644     if (!getAssociatedValue().getType()->isIntegerTy())
4645       return false;
4646 
4647     const auto &AA =
4648         A.getAAFor<AAType>(*this, getIRPosition(), DepClassTy::NONE);
4649 
4650     Optional<ConstantInt *> COpt = AA.getAssumedConstantInt(A);
4651 
4652     if (!COpt.hasValue()) {
4653       SimplifiedAssociatedValue = llvm::None;
4654       A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
4655       return true;
4656     }
4657     if (auto *C = COpt.getValue()) {
4658       SimplifiedAssociatedValue = C;
4659       A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
4660       return true;
4661     }
4662     return false;
4663   }
4664 
4665   bool askSimplifiedValueForOtherAAs(Attributor &A) {
4666     if (askSimplifiedValueFor<AAValueConstantRange>(A))
4667       return true;
4668     if (askSimplifiedValueFor<AAPotentialValues>(A))
4669       return true;
4670     return false;
4671   }
4672 
4673   /// See AbstractAttribute::manifest(...).
4674   ChangeStatus manifest(Attributor &A) override {
4675     ChangeStatus Changed = ChangeStatus::UNCHANGED;
4676 
4677     if (SimplifiedAssociatedValue.hasValue() &&
4678         !SimplifiedAssociatedValue.getValue())
4679       return Changed;
4680 
4681     Value &V = getAssociatedValue();
4682     auto *C = SimplifiedAssociatedValue.hasValue()
4683                   ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())
4684                   : UndefValue::get(V.getType());
4685     if (C) {
4686       // We can replace the AssociatedValue with the constant.
4687       if (!V.user_empty() && &V != C && V.getType() == C->getType()) {
4688         LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C
4689                           << " :: " << *this << "\n");
4690         if (A.changeValueAfterManifest(V, *C))
4691           Changed = ChangeStatus::CHANGED;
4692       }
4693     }
4694 
4695     return Changed | AAValueSimplify::manifest(A);
4696   }
4697 
4698   /// See AbstractState::indicatePessimisticFixpoint(...).
4699   ChangeStatus indicatePessimisticFixpoint() override {
4700     // NOTE: Associated value will be returned in a pessimistic fixpoint and is
4701     // regarded as known. That's why`indicateOptimisticFixpoint` is called.
4702     SimplifiedAssociatedValue = &getAssociatedValue();
4703     indicateOptimisticFixpoint();
4704     return ChangeStatus::CHANGED;
4705   }
4706 
4707 protected:
4708   // An assumed simplified value. Initially, it is set to Optional::None, which
4709   // means that the value is not clear under current assumption. If in the
4710   // pessimistic state, getAssumedSimplifiedValue doesn't return this value but
4711   // returns orignal associated value.
4712   Optional<Value *> SimplifiedAssociatedValue;
4713 };
4714 
4715 struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
4716   AAValueSimplifyArgument(const IRPosition &IRP, Attributor &A)
4717       : AAValueSimplifyImpl(IRP, A) {}
4718 
4719   void initialize(Attributor &A) override {
4720     AAValueSimplifyImpl::initialize(A);
4721     if (!getAnchorScope() || getAnchorScope()->isDeclaration())
4722       indicatePessimisticFixpoint();
4723     if (hasAttr({Attribute::InAlloca, Attribute::Preallocated,
4724                  Attribute::StructRet, Attribute::Nest},
4725                 /* IgnoreSubsumingPositions */ true))
4726       indicatePessimisticFixpoint();
4727 
4728     // FIXME: This is a hack to prevent us from propagating function poiner in
4729     // the new pass manager CGSCC pass as it creates call edges the
4730     // CallGraphUpdater cannot handle yet.
4731     Value &V = getAssociatedValue();
4732     if (V.getType()->isPointerTy() &&
4733         V.getType()->getPointerElementType()->isFunctionTy() &&
4734         !A.isModulePass())
4735       indicatePessimisticFixpoint();
4736   }
4737 
4738   /// See AbstractAttribute::updateImpl(...).
4739   ChangeStatus updateImpl(Attributor &A) override {
4740     // Byval is only replacable if it is readonly otherwise we would write into
4741     // the replaced value and not the copy that byval creates implicitly.
4742     Argument *Arg = getAssociatedArgument();
4743     if (Arg->hasByValAttr()) {
4744       // TODO: We probably need to verify synchronization is not an issue, e.g.,
4745       //       there is no race by not copying a constant byval.
4746       const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(),
4747                                                        DepClassTy::REQUIRED);
4748       if (!MemAA.isAssumedReadOnly())
4749         return indicatePessimisticFixpoint();
4750     }
4751 
4752     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4753 
4754     auto PredForCallSite = [&](AbstractCallSite ACS) {
4755       const IRPosition &ACSArgPos =
4756           IRPosition::callsite_argument(ACS, getCallSiteArgNo());
4757       // Check if a coresponding argument was found or if it is on not
4758       // associated (which can happen for callback calls).
4759       if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
4760         return false;
4761 
4762       // We can only propagate thread independent values through callbacks.
4763       // This is different to direct/indirect call sites because for them we
4764       // know the thread executing the caller and callee is the same. For
4765       // callbacks this is not guaranteed, thus a thread dependent value could
4766       // be different for the caller and callee, making it invalid to propagate.
4767       Value &ArgOp = ACSArgPos.getAssociatedValue();
4768       if (ACS.isCallbackCall())
4769         if (auto *C = dyn_cast<Constant>(&ArgOp))
4770           if (C->isThreadDependent())
4771             return false;
4772       return checkAndUpdate(A, *this, ArgOp, SimplifiedAssociatedValue);
4773     };
4774 
4775     bool AllCallSitesKnown;
4776     if (!A.checkForAllCallSites(PredForCallSite, *this, true,
4777                                 AllCallSitesKnown))
4778       if (!askSimplifiedValueForOtherAAs(A))
4779         return indicatePessimisticFixpoint();
4780 
4781     // If a candicate was found in this update, return CHANGED.
4782     return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4783                ? ChangeStatus::UNCHANGED
4784                : ChangeStatus ::CHANGED;
4785   }
4786 
4787   /// See AbstractAttribute::trackStatistics()
4788   void trackStatistics() const override {
4789     STATS_DECLTRACK_ARG_ATTR(value_simplify)
4790   }
4791 };
4792 
4793 struct AAValueSimplifyReturned : AAValueSimplifyImpl {
4794   AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A)
4795       : AAValueSimplifyImpl(IRP, A) {}
4796 
4797   /// See AbstractAttribute::updateImpl(...).
4798   ChangeStatus updateImpl(Attributor &A) override {
4799     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4800 
4801     auto PredForReturned = [&](Value &V) {
4802       return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4803     };
4804 
4805     if (!A.checkForAllReturnedValues(PredForReturned, *this))
4806       if (!askSimplifiedValueForOtherAAs(A))
4807         return indicatePessimisticFixpoint();
4808 
4809     // If a candicate was found in this update, return CHANGED.
4810     return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4811                ? ChangeStatus::UNCHANGED
4812                : ChangeStatus ::CHANGED;
4813   }
4814 
4815   ChangeStatus manifest(Attributor &A) override {
4816     ChangeStatus Changed = ChangeStatus::UNCHANGED;
4817 
4818     if (SimplifiedAssociatedValue.hasValue() &&
4819         !SimplifiedAssociatedValue.getValue())
4820       return Changed;
4821 
4822     Value &V = getAssociatedValue();
4823     auto *C = SimplifiedAssociatedValue.hasValue()
4824                   ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())
4825                   : UndefValue::get(V.getType());
4826     if (C) {
4827       auto PredForReturned =
4828           [&](Value &V, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
4829             // We can replace the AssociatedValue with the constant.
4830             if (&V == C || V.getType() != C->getType() || isa<UndefValue>(V))
4831               return true;
4832 
4833             for (ReturnInst *RI : RetInsts) {
4834               if (RI->getFunction() != getAnchorScope())
4835                 continue;
4836               auto *RC = C;
4837               if (RC->getType() != RI->getReturnValue()->getType())
4838                 RC = ConstantExpr::getBitCast(RC,
4839                                               RI->getReturnValue()->getType());
4840               LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *RC
4841                                 << " in " << *RI << " :: " << *this << "\n");
4842               if (A.changeUseAfterManifest(RI->getOperandUse(0), *RC))
4843                 Changed = ChangeStatus::CHANGED;
4844             }
4845             return true;
4846           };
4847       A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this);
4848     }
4849 
4850     return Changed | AAValueSimplify::manifest(A);
4851   }
4852 
4853   /// See AbstractAttribute::trackStatistics()
4854   void trackStatistics() const override {
4855     STATS_DECLTRACK_FNRET_ATTR(value_simplify)
4856   }
4857 };
4858 
4859 struct AAValueSimplifyFloating : AAValueSimplifyImpl {
4860   AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A)
4861       : AAValueSimplifyImpl(IRP, A) {}
4862 
4863   /// See AbstractAttribute::initialize(...).
4864   void initialize(Attributor &A) override {
4865     // FIXME: This might have exposed a SCC iterator update bug in the old PM.
4866     //        Needs investigation.
4867     // AAValueSimplifyImpl::initialize(A);
4868     Value &V = getAnchorValue();
4869 
4870     // TODO: add other stuffs
4871     if (isa<Constant>(V))
4872       indicatePessimisticFixpoint();
4873   }
4874 
4875   /// Check if \p ICmp is an equality comparison (==/!=) with at least one
4876   /// nullptr. If so, try to simplify it using AANonNull on the other operand.
4877   /// Return true if successful, in that case SimplifiedAssociatedValue will be
4878   /// updated and \p Changed is set appropriately.
4879   bool checkForNullPtrCompare(Attributor &A, ICmpInst *ICmp,
4880                               ChangeStatus &Changed) {
4881     if (!ICmp)
4882       return false;
4883     if (!ICmp->isEquality())
4884       return false;
4885 
4886     // This is a comparison with == or !-. We check for nullptr now.
4887     bool Op0IsNull = isa<ConstantPointerNull>(ICmp->getOperand(0));
4888     bool Op1IsNull = isa<ConstantPointerNull>(ICmp->getOperand(1));
4889     if (!Op0IsNull && !Op1IsNull)
4890       return false;
4891 
4892     LLVMContext &Ctx = ICmp->getContext();
4893     // Check for `nullptr ==/!= nullptr` first:
4894     if (Op0IsNull && Op1IsNull) {
4895       Value *NewVal = ConstantInt::get(
4896           Type::getInt1Ty(Ctx), ICmp->getPredicate() == CmpInst::ICMP_EQ);
4897       assert(!SimplifiedAssociatedValue.hasValue() &&
4898              "Did not expect non-fixed value for constant comparison");
4899       SimplifiedAssociatedValue = NewVal;
4900       indicateOptimisticFixpoint();
4901       Changed = ChangeStatus::CHANGED;
4902       return true;
4903     }
4904 
4905     // Left is the nullptr ==/!= non-nullptr case. We'll use AANonNull on the
4906     // non-nullptr operand and if we assume it's non-null we can conclude the
4907     // result of the comparison.
4908     assert((Op0IsNull || Op1IsNull) &&
4909            "Expected nullptr versus non-nullptr comparison at this point");
4910 
4911     // The index is the operand that we assume is not null.
4912     unsigned PtrIdx = Op0IsNull;
4913     auto &PtrNonNullAA = A.getAAFor<AANonNull>(
4914         *this, IRPosition::value(*ICmp->getOperand(PtrIdx)),
4915         DepClassTy::REQUIRED);
4916     if (!PtrNonNullAA.isAssumedNonNull())
4917       return false;
4918 
4919     // The new value depends on the predicate, true for != and false for ==.
4920     Value *NewVal = ConstantInt::get(Type::getInt1Ty(Ctx),
4921                                      ICmp->getPredicate() == CmpInst::ICMP_NE);
4922 
4923     assert((!SimplifiedAssociatedValue.hasValue() ||
4924             SimplifiedAssociatedValue == NewVal) &&
4925            "Did not expect to change value for zero-comparison");
4926 
4927     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4928     SimplifiedAssociatedValue = NewVal;
4929 
4930     if (PtrNonNullAA.isKnownNonNull())
4931       indicateOptimisticFixpoint();
4932 
4933     Changed = HasValueBefore ? ChangeStatus::UNCHANGED : ChangeStatus ::CHANGED;
4934     return true;
4935   }
4936 
4937   /// See AbstractAttribute::updateImpl(...).
4938   ChangeStatus updateImpl(Attributor &A) override {
4939     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4940 
4941     ChangeStatus Changed;
4942     if (checkForNullPtrCompare(A, dyn_cast<ICmpInst>(&getAnchorValue()),
4943                                Changed))
4944       return Changed;
4945 
4946     auto VisitValueCB = [&](Value &V, const Instruction *CtxI, bool &,
4947                             bool Stripped) -> bool {
4948       auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V),
4949                                              DepClassTy::REQUIRED);
4950       if (!Stripped && this == &AA) {
4951         // TODO: Look the instruction and check recursively.
4952 
4953         LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V
4954                           << "\n");
4955         return false;
4956       }
4957       return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4958     };
4959 
4960     bool Dummy = false;
4961     if (!genericValueTraversal<AAValueSimplify, bool>(
4962             A, getIRPosition(), *this, Dummy, VisitValueCB, getCtxI(),
4963             /* UseValueSimplify */ false))
4964       if (!askSimplifiedValueForOtherAAs(A))
4965         return indicatePessimisticFixpoint();
4966 
4967     // If a candicate was found in this update, return CHANGED.
4968 
4969     return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4970                ? ChangeStatus::UNCHANGED
4971                : ChangeStatus ::CHANGED;
4972   }
4973 
4974   /// See AbstractAttribute::trackStatistics()
4975   void trackStatistics() const override {
4976     STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
4977   }
4978 };
4979 
4980 struct AAValueSimplifyFunction : AAValueSimplifyImpl {
4981   AAValueSimplifyFunction(const IRPosition &IRP, Attributor &A)
4982       : AAValueSimplifyImpl(IRP, A) {}
4983 
4984   /// See AbstractAttribute::initialize(...).
4985   void initialize(Attributor &A) override {
4986     SimplifiedAssociatedValue = &getAnchorValue();
4987     indicateOptimisticFixpoint();
4988   }
4989   /// See AbstractAttribute::initialize(...).
4990   ChangeStatus updateImpl(Attributor &A) override {
4991     llvm_unreachable(
4992         "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
4993   }
4994   /// See AbstractAttribute::trackStatistics()
4995   void trackStatistics() const override {
4996     STATS_DECLTRACK_FN_ATTR(value_simplify)
4997   }
4998 };
4999 
5000 struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
5001   AAValueSimplifyCallSite(const IRPosition &IRP, Attributor &A)
5002       : AAValueSimplifyFunction(IRP, A) {}
5003   /// See AbstractAttribute::trackStatistics()
5004   void trackStatistics() const override {
5005     STATS_DECLTRACK_CS_ATTR(value_simplify)
5006   }
5007 };
5008 
5009 struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned {
5010   AAValueSimplifyCallSiteReturned(const IRPosition &IRP, Attributor &A)
5011       : AAValueSimplifyReturned(IRP, A) {}
5012 
5013   /// See AbstractAttribute::manifest(...).
5014   ChangeStatus manifest(Attributor &A) override {
5015     return AAValueSimplifyImpl::manifest(A);
5016   }
5017 
5018   void trackStatistics() const override {
5019     STATS_DECLTRACK_CSRET_ATTR(value_simplify)
5020   }
5021 };
5022 struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
5023   AAValueSimplifyCallSiteArgument(const IRPosition &IRP, Attributor &A)
5024       : AAValueSimplifyFloating(IRP, A) {}
5025 
5026   /// See AbstractAttribute::manifest(...).
5027   ChangeStatus manifest(Attributor &A) override {
5028     ChangeStatus Changed = ChangeStatus::UNCHANGED;
5029 
5030     if (SimplifiedAssociatedValue.hasValue() &&
5031         !SimplifiedAssociatedValue.getValue())
5032       return Changed;
5033 
5034     Value &V = getAssociatedValue();
5035     auto *C = SimplifiedAssociatedValue.hasValue()
5036                   ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())
5037                   : UndefValue::get(V.getType());
5038     if (C) {
5039       Use &U = cast<CallBase>(&getAnchorValue())
5040                    ->getArgOperandUse(getCallSiteArgNo());
5041       // We can replace the AssociatedValue with the constant.
5042       if (&V != C && V.getType() == C->getType()) {
5043         if (A.changeUseAfterManifest(U, *C))
5044           Changed = ChangeStatus::CHANGED;
5045       }
5046     }
5047 
5048     return Changed | AAValueSimplify::manifest(A);
5049   }
5050 
5051   void trackStatistics() const override {
5052     STATS_DECLTRACK_CSARG_ATTR(value_simplify)
5053   }
5054 };
5055 
5056 /// ----------------------- Heap-To-Stack Conversion ---------------------------
5057 struct AAHeapToStackImpl : public AAHeapToStack {
5058   AAHeapToStackImpl(const IRPosition &IRP, Attributor &A)
5059       : AAHeapToStack(IRP, A) {}
5060 
5061   const std::string getAsStr() const override {
5062     return "[H2S] Mallocs: " + std::to_string(MallocCalls.size());
5063   }
5064 
5065   ChangeStatus manifest(Attributor &A) override {
5066     assert(getState().isValidState() &&
5067            "Attempted to manifest an invalid state!");
5068 
5069     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
5070     Function *F = getAnchorScope();
5071     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5072 
5073     for (Instruction *MallocCall : MallocCalls) {
5074       // This malloc cannot be replaced.
5075       if (BadMallocCalls.count(MallocCall))
5076         continue;
5077 
5078       for (Instruction *FreeCall : FreesForMalloc[MallocCall]) {
5079         LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
5080         A.deleteAfterManifest(*FreeCall);
5081         HasChanged = ChangeStatus::CHANGED;
5082       }
5083 
5084       LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall
5085                         << "\n");
5086 
5087       Align Alignment;
5088       Value *Size;
5089       if (isCallocLikeFn(MallocCall, TLI)) {
5090         auto *Num = MallocCall->getOperand(0);
5091         auto *SizeT = MallocCall->getOperand(1);
5092         IRBuilder<> B(MallocCall);
5093         Size = B.CreateMul(Num, SizeT, "h2s.calloc.size");
5094       } else if (isAlignedAllocLikeFn(MallocCall, TLI)) {
5095         Size = MallocCall->getOperand(1);
5096         Alignment = MaybeAlign(cast<ConstantInt>(MallocCall->getOperand(0))
5097                                    ->getValue()
5098                                    .getZExtValue())
5099                         .valueOrOne();
5100       } else {
5101         Size = MallocCall->getOperand(0);
5102       }
5103 
5104       unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace();
5105       Instruction *AI =
5106           new AllocaInst(Type::getInt8Ty(F->getContext()), AS, Size, Alignment,
5107                          "", MallocCall->getNextNode());
5108 
5109       if (AI->getType() != MallocCall->getType())
5110         AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc",
5111                              AI->getNextNode());
5112 
5113       A.changeValueAfterManifest(*MallocCall, *AI);
5114 
5115       if (auto *II = dyn_cast<InvokeInst>(MallocCall)) {
5116         auto *NBB = II->getNormalDest();
5117         BranchInst::Create(NBB, MallocCall->getParent());
5118         A.deleteAfterManifest(*MallocCall);
5119       } else {
5120         A.deleteAfterManifest(*MallocCall);
5121       }
5122 
5123       // Zero out the allocated memory if it was a calloc.
5124       if (isCallocLikeFn(MallocCall, TLI)) {
5125         auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc",
5126                                    AI->getNextNode());
5127         Value *Ops[] = {
5128             BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size,
5129             ConstantInt::get(Type::getInt1Ty(F->getContext()), false)};
5130 
5131         Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()};
5132         Module *M = F->getParent();
5133         Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
5134         CallInst::Create(Fn, Ops, "", BI->getNextNode());
5135       }
5136       HasChanged = ChangeStatus::CHANGED;
5137     }
5138 
5139     return HasChanged;
5140   }
5141 
5142   /// Collection of all malloc calls in a function.
5143   SmallSetVector<Instruction *, 4> MallocCalls;
5144 
5145   /// Collection of malloc calls that cannot be converted.
5146   DenseSet<const Instruction *> BadMallocCalls;
5147 
5148   /// A map for each malloc call to the set of associated free calls.
5149   DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc;
5150 
5151   ChangeStatus updateImpl(Attributor &A) override;
5152 };
5153 
5154 ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
5155   const Function *F = getAnchorScope();
5156   const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5157 
5158   MustBeExecutedContextExplorer &Explorer =
5159       A.getInfoCache().getMustBeExecutedContextExplorer();
5160 
5161   auto FreeCheck = [&](Instruction &I) {
5162     const auto &Frees = FreesForMalloc.lookup(&I);
5163     if (Frees.size() != 1)
5164       return false;
5165     Instruction *UniqueFree = *Frees.begin();
5166     return Explorer.findInContextOf(UniqueFree, I.getNextNode());
5167   };
5168 
5169   auto UsesCheck = [&](Instruction &I) {
5170     bool ValidUsesOnly = true;
5171     bool MustUse = true;
5172     auto Pred = [&](const Use &U, bool &Follow) -> bool {
5173       Instruction *UserI = cast<Instruction>(U.getUser());
5174       if (isa<LoadInst>(UserI))
5175         return true;
5176       if (auto *SI = dyn_cast<StoreInst>(UserI)) {
5177         if (SI->getValueOperand() == U.get()) {
5178           LLVM_DEBUG(dbgs()
5179                      << "[H2S] escaping store to memory: " << *UserI << "\n");
5180           ValidUsesOnly = false;
5181         } else {
5182           // A store into the malloc'ed memory is fine.
5183         }
5184         return true;
5185       }
5186       if (auto *CB = dyn_cast<CallBase>(UserI)) {
5187         if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
5188           return true;
5189         // Record malloc.
5190         if (isFreeCall(UserI, TLI)) {
5191           if (MustUse) {
5192             FreesForMalloc[&I].insert(UserI);
5193           } else {
5194             LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: "
5195                               << *UserI << "\n");
5196             ValidUsesOnly = false;
5197           }
5198           return true;
5199         }
5200 
5201         unsigned ArgNo = CB->getArgOperandNo(&U);
5202 
5203         const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
5204             *this, IRPosition::callsite_argument(*CB, ArgNo),
5205             DepClassTy::REQUIRED);
5206 
5207         // If a callsite argument use is nofree, we are fine.
5208         const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
5209             *this, IRPosition::callsite_argument(*CB, ArgNo),
5210             DepClassTy::REQUIRED);
5211 
5212         if (!NoCaptureAA.isAssumedNoCapture() ||
5213             !ArgNoFreeAA.isAssumedNoFree()) {
5214           LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
5215           ValidUsesOnly = false;
5216         }
5217         return true;
5218       }
5219 
5220       if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
5221           isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
5222         MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI));
5223         Follow = true;
5224         return true;
5225       }
5226       // Unknown user for which we can not track uses further (in a way that
5227       // makes sense).
5228       LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
5229       ValidUsesOnly = false;
5230       return true;
5231     };
5232     A.checkForAllUses(Pred, *this, I);
5233     return ValidUsesOnly;
5234   };
5235 
5236   auto MallocCallocCheck = [&](Instruction &I) {
5237     if (BadMallocCalls.count(&I))
5238       return true;
5239 
5240     bool IsMalloc = isMallocLikeFn(&I, TLI);
5241     bool IsAlignedAllocLike = isAlignedAllocLikeFn(&I, TLI);
5242     bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI);
5243     if (!IsMalloc && !IsAlignedAllocLike && !IsCalloc) {
5244       BadMallocCalls.insert(&I);
5245       return true;
5246     }
5247 
5248     if (IsMalloc) {
5249       if (MaxHeapToStackSize == -1) {
5250         if (UsesCheck(I) || FreeCheck(I)) {
5251           MallocCalls.insert(&I);
5252           return true;
5253         }
5254       }
5255       if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
5256         if (Size->getValue().ule(MaxHeapToStackSize))
5257           if (UsesCheck(I) || FreeCheck(I)) {
5258             MallocCalls.insert(&I);
5259             return true;
5260           }
5261     } else if (IsAlignedAllocLike && isa<ConstantInt>(I.getOperand(0))) {
5262       if (MaxHeapToStackSize == -1) {
5263         if (UsesCheck(I) || FreeCheck(I)) {
5264           MallocCalls.insert(&I);
5265           return true;
5266         }
5267       }
5268       // Only if the alignment and sizes are constant.
5269       if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
5270         if (Size->getValue().ule(MaxHeapToStackSize))
5271           if (UsesCheck(I) || FreeCheck(I)) {
5272             MallocCalls.insert(&I);
5273             return true;
5274           }
5275     } else if (IsCalloc) {
5276       if (MaxHeapToStackSize == -1) {
5277         if (UsesCheck(I) || FreeCheck(I)) {
5278           MallocCalls.insert(&I);
5279           return true;
5280         }
5281       }
5282       bool Overflow = false;
5283       if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
5284         if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
5285           if ((Size->getValue().umul_ov(Num->getValue(), Overflow))
5286                   .ule(MaxHeapToStackSize))
5287             if (!Overflow && (UsesCheck(I) || FreeCheck(I))) {
5288               MallocCalls.insert(&I);
5289               return true;
5290             }
5291     }
5292 
5293     BadMallocCalls.insert(&I);
5294     return true;
5295   };
5296 
5297   size_t NumBadMallocs = BadMallocCalls.size();
5298 
5299   A.checkForAllCallLikeInstructions(MallocCallocCheck, *this);
5300 
5301   if (NumBadMallocs != BadMallocCalls.size())
5302     return ChangeStatus::CHANGED;
5303 
5304   return ChangeStatus::UNCHANGED;
5305 }
5306 
5307 struct AAHeapToStackFunction final : public AAHeapToStackImpl {
5308   AAHeapToStackFunction(const IRPosition &IRP, Attributor &A)
5309       : AAHeapToStackImpl(IRP, A) {}
5310 
5311   /// See AbstractAttribute::trackStatistics().
5312   void trackStatistics() const override {
5313     STATS_DECL(
5314         MallocCalls, Function,
5315         "Number of malloc/calloc/aligned_alloc calls converted to allocas");
5316     for (auto *C : MallocCalls)
5317       if (!BadMallocCalls.count(C))
5318         ++BUILD_STAT_NAME(MallocCalls, Function);
5319   }
5320 };
5321 
5322 /// ----------------------- Privatizable Pointers ------------------------------
5323 struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
5324   AAPrivatizablePtrImpl(const IRPosition &IRP, Attributor &A)
5325       : AAPrivatizablePtr(IRP, A), PrivatizableType(llvm::None) {}
5326 
5327   ChangeStatus indicatePessimisticFixpoint() override {
5328     AAPrivatizablePtr::indicatePessimisticFixpoint();
5329     PrivatizableType = nullptr;
5330     return ChangeStatus::CHANGED;
5331   }
5332 
5333   /// Identify the type we can chose for a private copy of the underlying
5334   /// argument. None means it is not clear yet, nullptr means there is none.
5335   virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
5336 
5337   /// Return a privatizable type that encloses both T0 and T1.
5338   /// TODO: This is merely a stub for now as we should manage a mapping as well.
5339   Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
5340     if (!T0.hasValue())
5341       return T1;
5342     if (!T1.hasValue())
5343       return T0;
5344     if (T0 == T1)
5345       return T0;
5346     return nullptr;
5347   }
5348 
5349   Optional<Type *> getPrivatizableType() const override {
5350     return PrivatizableType;
5351   }
5352 
5353   const std::string getAsStr() const override {
5354     return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
5355   }
5356 
5357 protected:
5358   Optional<Type *> PrivatizableType;
5359 };
5360 
5361 // TODO: Do this for call site arguments (probably also other values) as well.
5362 
5363 struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
5364   AAPrivatizablePtrArgument(const IRPosition &IRP, Attributor &A)
5365       : AAPrivatizablePtrImpl(IRP, A) {}
5366 
5367   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
5368   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
5369     // If this is a byval argument and we know all the call sites (so we can
5370     // rewrite them), there is no need to check them explicitly.
5371     bool AllCallSitesKnown;
5372     if (getIRPosition().hasAttr(Attribute::ByVal) &&
5373         A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
5374                                true, AllCallSitesKnown))
5375       return getAssociatedValue().getType()->getPointerElementType();
5376 
5377     Optional<Type *> Ty;
5378     unsigned ArgNo = getIRPosition().getCallSiteArgNo();
5379 
5380     // Make sure the associated call site argument has the same type at all call
5381     // sites and it is an allocation we know is safe to privatize, for now that
5382     // means we only allow alloca instructions.
5383     // TODO: We can additionally analyze the accesses in the callee to  create
5384     //       the type from that information instead. That is a little more
5385     //       involved and will be done in a follow up patch.
5386     auto CallSiteCheck = [&](AbstractCallSite ACS) {
5387       IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
5388       // Check if a coresponding argument was found or if it is one not
5389       // associated (which can happen for callback calls).
5390       if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
5391         return false;
5392 
5393       // Check that all call sites agree on a type.
5394       auto &PrivCSArgAA =
5395           A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos, DepClassTy::REQUIRED);
5396       Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
5397 
5398       LLVM_DEBUG({
5399         dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
5400         if (CSTy.hasValue() && CSTy.getValue())
5401           CSTy.getValue()->print(dbgs());
5402         else if (CSTy.hasValue())
5403           dbgs() << "<nullptr>";
5404         else
5405           dbgs() << "<none>";
5406       });
5407 
5408       Ty = combineTypes(Ty, CSTy);
5409 
5410       LLVM_DEBUG({
5411         dbgs() << " : New Type: ";
5412         if (Ty.hasValue() && Ty.getValue())
5413           Ty.getValue()->print(dbgs());
5414         else if (Ty.hasValue())
5415           dbgs() << "<nullptr>";
5416         else
5417           dbgs() << "<none>";
5418         dbgs() << "\n";
5419       });
5420 
5421       return !Ty.hasValue() || Ty.getValue();
5422     };
5423 
5424     if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown))
5425       return nullptr;
5426     return Ty;
5427   }
5428 
5429   /// See AbstractAttribute::updateImpl(...).
5430   ChangeStatus updateImpl(Attributor &A) override {
5431     PrivatizableType = identifyPrivatizableType(A);
5432     if (!PrivatizableType.hasValue())
5433       return ChangeStatus::UNCHANGED;
5434     if (!PrivatizableType.getValue())
5435       return indicatePessimisticFixpoint();
5436 
5437     // The dependence is optional so we don't give up once we give up on the
5438     // alignment.
5439     A.getAAFor<AAAlign>(*this, IRPosition::value(getAssociatedValue()),
5440                         DepClassTy::OPTIONAL);
5441 
5442     // Avoid arguments with padding for now.
5443     if (!getIRPosition().hasAttr(Attribute::ByVal) &&
5444         !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
5445                                                 A.getInfoCache().getDL())) {
5446       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
5447       return indicatePessimisticFixpoint();
5448     }
5449 
5450     // Verify callee and caller agree on how the promoted argument would be
5451     // passed.
5452     // TODO: The use of the ArgumentPromotion interface here is ugly, we need a
5453     // specialized form of TargetTransformInfo::areFunctionArgsABICompatible
5454     // which doesn't require the arguments ArgumentPromotion wanted to pass.
5455     Function &Fn = *getIRPosition().getAnchorScope();
5456     SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy;
5457     ArgsToPromote.insert(getAssociatedArgument());
5458     const auto *TTI =
5459         A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
5460     if (!TTI ||
5461         !ArgumentPromotionPass::areFunctionArgsABICompatible(
5462             Fn, *TTI, ArgsToPromote, Dummy) ||
5463         ArgsToPromote.empty()) {
5464       LLVM_DEBUG(
5465           dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
5466                  << Fn.getName() << "\n");
5467       return indicatePessimisticFixpoint();
5468     }
5469 
5470     // Collect the types that will replace the privatizable type in the function
5471     // signature.
5472     SmallVector<Type *, 16> ReplacementTypes;
5473     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5474 
5475     // Register a rewrite of the argument.
5476     Argument *Arg = getAssociatedArgument();
5477     if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
5478       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
5479       return indicatePessimisticFixpoint();
5480     }
5481 
5482     unsigned ArgNo = Arg->getArgNo();
5483 
5484     // Helper to check if for the given call site the associated argument is
5485     // passed to a callback where the privatization would be different.
5486     auto IsCompatiblePrivArgOfCallback = [&](CallBase &CB) {
5487       SmallVector<const Use *, 4> CallbackUses;
5488       AbstractCallSite::getCallbackUses(CB, CallbackUses);
5489       for (const Use *U : CallbackUses) {
5490         AbstractCallSite CBACS(U);
5491         assert(CBACS && CBACS.isCallbackCall());
5492         for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
5493           int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
5494 
5495           LLVM_DEBUG({
5496             dbgs()
5497                 << "[AAPrivatizablePtr] Argument " << *Arg
5498                 << "check if can be privatized in the context of its parent ("
5499                 << Arg->getParent()->getName()
5500                 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5501                    "callback ("
5502                 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5503                 << ")\n[AAPrivatizablePtr] " << CBArg << " : "
5504                 << CBACS.getCallArgOperand(CBArg) << " vs "
5505                 << CB.getArgOperand(ArgNo) << "\n"
5506                 << "[AAPrivatizablePtr] " << CBArg << " : "
5507                 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
5508           });
5509 
5510           if (CBArgNo != int(ArgNo))
5511             continue;
5512           const auto &CBArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
5513               *this, IRPosition::argument(CBArg), DepClassTy::REQUIRED);
5514           if (CBArgPrivAA.isValidState()) {
5515             auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
5516             if (!CBArgPrivTy.hasValue())
5517               continue;
5518             if (CBArgPrivTy.getValue() == PrivatizableType)
5519               continue;
5520           }
5521 
5522           LLVM_DEBUG({
5523             dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5524                    << " cannot be privatized in the context of its parent ("
5525                    << Arg->getParent()->getName()
5526                    << ")\n[AAPrivatizablePtr] because it is an argument in a "
5527                       "callback ("
5528                    << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5529                    << ").\n[AAPrivatizablePtr] for which the argument "
5530                       "privatization is not compatible.\n";
5531           });
5532           return false;
5533         }
5534       }
5535       return true;
5536     };
5537 
5538     // Helper to check if for the given call site the associated argument is
5539     // passed to a direct call where the privatization would be different.
5540     auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
5541       CallBase *DC = cast<CallBase>(ACS.getInstruction());
5542       int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
5543       assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() &&
5544              "Expected a direct call operand for callback call operand");
5545 
5546       LLVM_DEBUG({
5547         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5548                << " check if be privatized in the context of its parent ("
5549                << Arg->getParent()->getName()
5550                << ")\n[AAPrivatizablePtr] because it is an argument in a "
5551                   "direct call of ("
5552                << DCArgNo << "@" << DC->getCalledFunction()->getName()
5553                << ").\n";
5554       });
5555 
5556       Function *DCCallee = DC->getCalledFunction();
5557       if (unsigned(DCArgNo) < DCCallee->arg_size()) {
5558         const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
5559             *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)),
5560             DepClassTy::REQUIRED);
5561         if (DCArgPrivAA.isValidState()) {
5562           auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
5563           if (!DCArgPrivTy.hasValue())
5564             return true;
5565           if (DCArgPrivTy.getValue() == PrivatizableType)
5566             return true;
5567         }
5568       }
5569 
5570       LLVM_DEBUG({
5571         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5572                << " cannot be privatized in the context of its parent ("
5573                << Arg->getParent()->getName()
5574                << ")\n[AAPrivatizablePtr] because it is an argument in a "
5575                   "direct call of ("
5576                << ACS.getInstruction()->getCalledFunction()->getName()
5577                << ").\n[AAPrivatizablePtr] for which the argument "
5578                   "privatization is not compatible.\n";
5579       });
5580       return false;
5581     };
5582 
5583     // Helper to check if the associated argument is used at the given abstract
5584     // call site in a way that is incompatible with the privatization assumed
5585     // here.
5586     auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
5587       if (ACS.isDirectCall())
5588         return IsCompatiblePrivArgOfCallback(*ACS.getInstruction());
5589       if (ACS.isCallbackCall())
5590         return IsCompatiblePrivArgOfDirectCS(ACS);
5591       return false;
5592     };
5593 
5594     bool AllCallSitesKnown;
5595     if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
5596                                 AllCallSitesKnown))
5597       return indicatePessimisticFixpoint();
5598 
5599     return ChangeStatus::UNCHANGED;
5600   }
5601 
5602   /// Given a type to private \p PrivType, collect the constituates (which are
5603   /// used) in \p ReplacementTypes.
5604   static void
5605   identifyReplacementTypes(Type *PrivType,
5606                            SmallVectorImpl<Type *> &ReplacementTypes) {
5607     // TODO: For now we expand the privatization type to the fullest which can
5608     //       lead to dead arguments that need to be removed later.
5609     assert(PrivType && "Expected privatizable type!");
5610 
5611     // Traverse the type, extract constituate types on the outermost level.
5612     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5613       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
5614         ReplacementTypes.push_back(PrivStructType->getElementType(u));
5615     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5616       ReplacementTypes.append(PrivArrayType->getNumElements(),
5617                               PrivArrayType->getElementType());
5618     } else {
5619       ReplacementTypes.push_back(PrivType);
5620     }
5621   }
5622 
5623   /// Initialize \p Base according to the type \p PrivType at position \p IP.
5624   /// The values needed are taken from the arguments of \p F starting at
5625   /// position \p ArgNo.
5626   static void createInitialization(Type *PrivType, Value &Base, Function &F,
5627                                    unsigned ArgNo, Instruction &IP) {
5628     assert(PrivType && "Expected privatizable type!");
5629 
5630     IRBuilder<NoFolder> IRB(&IP);
5631     const DataLayout &DL = F.getParent()->getDataLayout();
5632 
5633     // Traverse the type, build GEPs and stores.
5634     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5635       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5636       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5637         Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
5638         Value *Ptr =
5639             constructPointer(PointeeTy, PrivType, &Base,
5640                              PrivStructLayout->getElementOffset(u), IRB, DL);
5641         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5642       }
5643     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5644       Type *PointeeTy = PrivArrayType->getElementType();
5645       Type *PointeePtrTy = PointeeTy->getPointerTo();
5646       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
5647       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5648         Value *Ptr = constructPointer(PointeePtrTy, PrivType, &Base,
5649                                       u * PointeeTySize, IRB, DL);
5650         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5651       }
5652     } else {
5653       new StoreInst(F.getArg(ArgNo), &Base, &IP);
5654     }
5655   }
5656 
5657   /// Extract values from \p Base according to the type \p PrivType at the
5658   /// call position \p ACS. The values are appended to \p ReplacementValues.
5659   void createReplacementValues(Align Alignment, Type *PrivType,
5660                                AbstractCallSite ACS, Value *Base,
5661                                SmallVectorImpl<Value *> &ReplacementValues) {
5662     assert(Base && "Expected base value!");
5663     assert(PrivType && "Expected privatizable type!");
5664     Instruction *IP = ACS.getInstruction();
5665 
5666     IRBuilder<NoFolder> IRB(IP);
5667     const DataLayout &DL = IP->getModule()->getDataLayout();
5668 
5669     if (Base->getType()->getPointerElementType() != PrivType)
5670       Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(),
5671                                                  "", ACS.getInstruction());
5672 
5673     // Traverse the type, build GEPs and loads.
5674     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5675       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5676       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5677         Type *PointeeTy = PrivStructType->getElementType(u);
5678         Value *Ptr =
5679             constructPointer(PointeeTy->getPointerTo(), PrivType, Base,
5680                              PrivStructLayout->getElementOffset(u), IRB, DL);
5681         LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
5682         L->setAlignment(Alignment);
5683         ReplacementValues.push_back(L);
5684       }
5685     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5686       Type *PointeeTy = PrivArrayType->getElementType();
5687       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
5688       Type *PointeePtrTy = PointeeTy->getPointerTo();
5689       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5690         Value *Ptr = constructPointer(PointeePtrTy, PrivType, Base,
5691                                       u * PointeeTySize, IRB, DL);
5692         LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
5693         L->setAlignment(Alignment);
5694         ReplacementValues.push_back(L);
5695       }
5696     } else {
5697       LoadInst *L = new LoadInst(PrivType, Base, "", IP);
5698       L->setAlignment(Alignment);
5699       ReplacementValues.push_back(L);
5700     }
5701   }
5702 
5703   /// See AbstractAttribute::manifest(...)
5704   ChangeStatus manifest(Attributor &A) override {
5705     if (!PrivatizableType.hasValue())
5706       return ChangeStatus::UNCHANGED;
5707     assert(PrivatizableType.getValue() && "Expected privatizable type!");
5708 
5709     // Collect all tail calls in the function as we cannot allow new allocas to
5710     // escape into tail recursion.
5711     // TODO: Be smarter about new allocas escaping into tail calls.
5712     SmallVector<CallInst *, 16> TailCalls;
5713     if (!A.checkForAllInstructions(
5714             [&](Instruction &I) {
5715               CallInst &CI = cast<CallInst>(I);
5716               if (CI.isTailCall())
5717                 TailCalls.push_back(&CI);
5718               return true;
5719             },
5720             *this, {Instruction::Call}))
5721       return ChangeStatus::UNCHANGED;
5722 
5723     Argument *Arg = getAssociatedArgument();
5724     // Query AAAlign attribute for alignment of associated argument to
5725     // determine the best alignment of loads.
5726     const auto &AlignAA =
5727         A.getAAFor<AAAlign>(*this, IRPosition::value(*Arg), DepClassTy::NONE);
5728 
5729     // Callback to repair the associated function. A new alloca is placed at the
5730     // beginning and initialized with the values passed through arguments. The
5731     // new alloca replaces the use of the old pointer argument.
5732     Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
5733         [=](const Attributor::ArgumentReplacementInfo &ARI,
5734             Function &ReplacementFn, Function::arg_iterator ArgIt) {
5735           BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
5736           Instruction *IP = &*EntryBB.getFirstInsertionPt();
5737           Instruction *AI = new AllocaInst(PrivatizableType.getValue(), 0,
5738                                            Arg->getName() + ".priv", IP);
5739           createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
5740                                ArgIt->getArgNo(), *IP);
5741 
5742           if (AI->getType() != Arg->getType())
5743             AI =
5744                 BitCastInst::CreateBitOrPointerCast(AI, Arg->getType(), "", IP);
5745           Arg->replaceAllUsesWith(AI);
5746 
5747           for (CallInst *CI : TailCalls)
5748             CI->setTailCall(false);
5749         };
5750 
5751     // Callback to repair a call site of the associated function. The elements
5752     // of the privatizable type are loaded prior to the call and passed to the
5753     // new function version.
5754     Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
5755         [=, &AlignAA](const Attributor::ArgumentReplacementInfo &ARI,
5756                       AbstractCallSite ACS,
5757                       SmallVectorImpl<Value *> &NewArgOperands) {
5758           // When no alignment is specified for the load instruction,
5759           // natural alignment is assumed.
5760           createReplacementValues(
5761               assumeAligned(AlignAA.getAssumedAlign()),
5762               PrivatizableType.getValue(), ACS,
5763               ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
5764               NewArgOperands);
5765         };
5766 
5767     // Collect the types that will replace the privatizable type in the function
5768     // signature.
5769     SmallVector<Type *, 16> ReplacementTypes;
5770     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5771 
5772     // Register a rewrite of the argument.
5773     if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
5774                                            std::move(FnRepairCB),
5775                                            std::move(ACSRepairCB)))
5776       return ChangeStatus::CHANGED;
5777     return ChangeStatus::UNCHANGED;
5778   }
5779 
5780   /// See AbstractAttribute::trackStatistics()
5781   void trackStatistics() const override {
5782     STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
5783   }
5784 };
5785 
5786 struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
5787   AAPrivatizablePtrFloating(const IRPosition &IRP, Attributor &A)
5788       : AAPrivatizablePtrImpl(IRP, A) {}
5789 
5790   /// See AbstractAttribute::initialize(...).
5791   virtual void initialize(Attributor &A) override {
5792     // TODO: We can privatize more than arguments.
5793     indicatePessimisticFixpoint();
5794   }
5795 
5796   ChangeStatus updateImpl(Attributor &A) override {
5797     llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
5798                      "updateImpl will not be called");
5799   }
5800 
5801   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
5802   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
5803     Value *Obj = getUnderlyingObject(&getAssociatedValue());
5804     if (!Obj) {
5805       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
5806       return nullptr;
5807     }
5808 
5809     if (auto *AI = dyn_cast<AllocaInst>(Obj))
5810       if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
5811         if (CI->isOne())
5812           return Obj->getType()->getPointerElementType();
5813     if (auto *Arg = dyn_cast<Argument>(Obj)) {
5814       auto &PrivArgAA = A.getAAFor<AAPrivatizablePtr>(
5815           *this, IRPosition::argument(*Arg), DepClassTy::REQUIRED);
5816       if (PrivArgAA.isAssumedPrivatizablePtr())
5817         return Obj->getType()->getPointerElementType();
5818     }
5819 
5820     LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
5821                          "alloca nor privatizable argument: "
5822                       << *Obj << "!\n");
5823     return nullptr;
5824   }
5825 
5826   /// See AbstractAttribute::trackStatistics()
5827   void trackStatistics() const override {
5828     STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
5829   }
5830 };
5831 
5832 struct AAPrivatizablePtrCallSiteArgument final
5833     : public AAPrivatizablePtrFloating {
5834   AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP, Attributor &A)
5835       : AAPrivatizablePtrFloating(IRP, A) {}
5836 
5837   /// See AbstractAttribute::initialize(...).
5838   void initialize(Attributor &A) override {
5839     if (getIRPosition().hasAttr(Attribute::ByVal))
5840       indicateOptimisticFixpoint();
5841   }
5842 
5843   /// See AbstractAttribute::updateImpl(...).
5844   ChangeStatus updateImpl(Attributor &A) override {
5845     PrivatizableType = identifyPrivatizableType(A);
5846     if (!PrivatizableType.hasValue())
5847       return ChangeStatus::UNCHANGED;
5848     if (!PrivatizableType.getValue())
5849       return indicatePessimisticFixpoint();
5850 
5851     const IRPosition &IRP = getIRPosition();
5852     auto &NoCaptureAA =
5853         A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::REQUIRED);
5854     if (!NoCaptureAA.isAssumedNoCapture()) {
5855       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
5856       return indicatePessimisticFixpoint();
5857     }
5858 
5859     auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP, DepClassTy::REQUIRED);
5860     if (!NoAliasAA.isAssumedNoAlias()) {
5861       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
5862       return indicatePessimisticFixpoint();
5863     }
5864 
5865     const auto &MemBehaviorAA =
5866         A.getAAFor<AAMemoryBehavior>(*this, IRP, DepClassTy::REQUIRED);
5867     if (!MemBehaviorAA.isAssumedReadOnly()) {
5868       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
5869       return indicatePessimisticFixpoint();
5870     }
5871 
5872     return ChangeStatus::UNCHANGED;
5873   }
5874 
5875   /// See AbstractAttribute::trackStatistics()
5876   void trackStatistics() const override {
5877     STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
5878   }
5879 };
5880 
5881 struct AAPrivatizablePtrCallSiteReturned final
5882     : public AAPrivatizablePtrFloating {
5883   AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP, Attributor &A)
5884       : AAPrivatizablePtrFloating(IRP, A) {}
5885 
5886   /// See AbstractAttribute::initialize(...).
5887   void initialize(Attributor &A) override {
5888     // TODO: We can privatize more than arguments.
5889     indicatePessimisticFixpoint();
5890   }
5891 
5892   /// See AbstractAttribute::trackStatistics()
5893   void trackStatistics() const override {
5894     STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
5895   }
5896 };
5897 
5898 struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
5899   AAPrivatizablePtrReturned(const IRPosition &IRP, Attributor &A)
5900       : AAPrivatizablePtrFloating(IRP, A) {}
5901 
5902   /// See AbstractAttribute::initialize(...).
5903   void initialize(Attributor &A) override {
5904     // TODO: We can privatize more than arguments.
5905     indicatePessimisticFixpoint();
5906   }
5907 
5908   /// See AbstractAttribute::trackStatistics()
5909   void trackStatistics() const override {
5910     STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
5911   }
5912 };
5913 
5914 /// -------------------- Memory Behavior Attributes ----------------------------
5915 /// Includes read-none, read-only, and write-only.
5916 /// ----------------------------------------------------------------------------
5917 struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
5918   AAMemoryBehaviorImpl(const IRPosition &IRP, Attributor &A)
5919       : AAMemoryBehavior(IRP, A) {}
5920 
5921   /// See AbstractAttribute::initialize(...).
5922   void initialize(Attributor &A) override {
5923     intersectAssumedBits(BEST_STATE);
5924     getKnownStateFromValue(getIRPosition(), getState());
5925     AAMemoryBehavior::initialize(A);
5926   }
5927 
5928   /// Return the memory behavior information encoded in the IR for \p IRP.
5929   static void getKnownStateFromValue(const IRPosition &IRP,
5930                                      BitIntegerState &State,
5931                                      bool IgnoreSubsumingPositions = false) {
5932     SmallVector<Attribute, 2> Attrs;
5933     IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
5934     for (const Attribute &Attr : Attrs) {
5935       switch (Attr.getKindAsEnum()) {
5936       case Attribute::ReadNone:
5937         State.addKnownBits(NO_ACCESSES);
5938         break;
5939       case Attribute::ReadOnly:
5940         State.addKnownBits(NO_WRITES);
5941         break;
5942       case Attribute::WriteOnly:
5943         State.addKnownBits(NO_READS);
5944         break;
5945       default:
5946         llvm_unreachable("Unexpected attribute!");
5947       }
5948     }
5949 
5950     if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
5951       if (!I->mayReadFromMemory())
5952         State.addKnownBits(NO_READS);
5953       if (!I->mayWriteToMemory())
5954         State.addKnownBits(NO_WRITES);
5955     }
5956   }
5957 
5958   /// See AbstractAttribute::getDeducedAttributes(...).
5959   void getDeducedAttributes(LLVMContext &Ctx,
5960                             SmallVectorImpl<Attribute> &Attrs) const override {
5961     assert(Attrs.size() == 0);
5962     if (isAssumedReadNone())
5963       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
5964     else if (isAssumedReadOnly())
5965       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
5966     else if (isAssumedWriteOnly())
5967       Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
5968     assert(Attrs.size() <= 1);
5969   }
5970 
5971   /// See AbstractAttribute::manifest(...).
5972   ChangeStatus manifest(Attributor &A) override {
5973     if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true))
5974       return ChangeStatus::UNCHANGED;
5975 
5976     const IRPosition &IRP = getIRPosition();
5977 
5978     // Check if we would improve the existing attributes first.
5979     SmallVector<Attribute, 4> DeducedAttrs;
5980     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
5981     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
5982           return IRP.hasAttr(Attr.getKindAsEnum(),
5983                              /* IgnoreSubsumingPositions */ true);
5984         }))
5985       return ChangeStatus::UNCHANGED;
5986 
5987     // Clear existing attributes.
5988     IRP.removeAttrs(AttrKinds);
5989 
5990     // Use the generic manifest method.
5991     return IRAttribute::manifest(A);
5992   }
5993 
5994   /// See AbstractState::getAsStr().
5995   const std::string getAsStr() const override {
5996     if (isAssumedReadNone())
5997       return "readnone";
5998     if (isAssumedReadOnly())
5999       return "readonly";
6000     if (isAssumedWriteOnly())
6001       return "writeonly";
6002     return "may-read/write";
6003   }
6004 
6005   /// The set of IR attributes AAMemoryBehavior deals with.
6006   static const Attribute::AttrKind AttrKinds[3];
6007 };
6008 
6009 const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
6010     Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
6011 
6012 /// Memory behavior attribute for a floating value.
6013 struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
6014   AAMemoryBehaviorFloating(const IRPosition &IRP, Attributor &A)
6015       : AAMemoryBehaviorImpl(IRP, A) {}
6016 
6017   /// See AbstractAttribute::initialize(...).
6018   void initialize(Attributor &A) override {
6019     AAMemoryBehaviorImpl::initialize(A);
6020     addUsesOf(A, getAssociatedValue());
6021   }
6022 
6023   /// See AbstractAttribute::updateImpl(...).
6024   ChangeStatus updateImpl(Attributor &A) override;
6025 
6026   /// See AbstractAttribute::trackStatistics()
6027   void trackStatistics() const override {
6028     if (isAssumedReadNone())
6029       STATS_DECLTRACK_FLOATING_ATTR(readnone)
6030     else if (isAssumedReadOnly())
6031       STATS_DECLTRACK_FLOATING_ATTR(readonly)
6032     else if (isAssumedWriteOnly())
6033       STATS_DECLTRACK_FLOATING_ATTR(writeonly)
6034   }
6035 
6036 private:
6037   /// Return true if users of \p UserI might access the underlying
6038   /// variable/location described by \p U and should therefore be analyzed.
6039   bool followUsersOfUseIn(Attributor &A, const Use *U,
6040                           const Instruction *UserI);
6041 
6042   /// Update the state according to the effect of use \p U in \p UserI.
6043   void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI);
6044 
6045 protected:
6046   /// Add the uses of \p V to the `Uses` set we look at during the update step.
6047   void addUsesOf(Attributor &A, const Value &V);
6048 
6049   /// Container for (transitive) uses of the associated argument.
6050   SmallVector<const Use *, 8> Uses;
6051 
6052   /// Set to remember the uses we already traversed.
6053   SmallPtrSet<const Use *, 8> Visited;
6054 };
6055 
6056 /// Memory behavior attribute for function argument.
6057 struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
6058   AAMemoryBehaviorArgument(const IRPosition &IRP, Attributor &A)
6059       : AAMemoryBehaviorFloating(IRP, A) {}
6060 
6061   /// See AbstractAttribute::initialize(...).
6062   void initialize(Attributor &A) override {
6063     intersectAssumedBits(BEST_STATE);
6064     const IRPosition &IRP = getIRPosition();
6065     // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
6066     // can query it when we use has/getAttr. That would allow us to reuse the
6067     // initialize of the base class here.
6068     bool HasByVal =
6069         IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
6070     getKnownStateFromValue(IRP, getState(),
6071                            /* IgnoreSubsumingPositions */ HasByVal);
6072 
6073     // Initialize the use vector with all direct uses of the associated value.
6074     Argument *Arg = getAssociatedArgument();
6075     if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent()))) {
6076       indicatePessimisticFixpoint();
6077     } else {
6078       addUsesOf(A, *Arg);
6079     }
6080   }
6081 
6082   ChangeStatus manifest(Attributor &A) override {
6083     // TODO: Pointer arguments are not supported on vectors of pointers yet.
6084     if (!getAssociatedValue().getType()->isPointerTy())
6085       return ChangeStatus::UNCHANGED;
6086 
6087     // TODO: From readattrs.ll: "inalloca parameters are always
6088     //                           considered written"
6089     if (hasAttr({Attribute::InAlloca, Attribute::Preallocated})) {
6090       removeKnownBits(NO_WRITES);
6091       removeAssumedBits(NO_WRITES);
6092     }
6093     return AAMemoryBehaviorFloating::manifest(A);
6094   }
6095 
6096   /// See AbstractAttribute::trackStatistics()
6097   void trackStatistics() const override {
6098     if (isAssumedReadNone())
6099       STATS_DECLTRACK_ARG_ATTR(readnone)
6100     else if (isAssumedReadOnly())
6101       STATS_DECLTRACK_ARG_ATTR(readonly)
6102     else if (isAssumedWriteOnly())
6103       STATS_DECLTRACK_ARG_ATTR(writeonly)
6104   }
6105 };
6106 
6107 struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
6108   AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP, Attributor &A)
6109       : AAMemoryBehaviorArgument(IRP, A) {}
6110 
6111   /// See AbstractAttribute::initialize(...).
6112   void initialize(Attributor &A) override {
6113     // If we don't have an associated attribute this is either a variadic call
6114     // or an indirect call, either way, nothing to do here.
6115     Argument *Arg = getAssociatedArgument();
6116     if (!Arg) {
6117       indicatePessimisticFixpoint();
6118       return;
6119     }
6120     if (Arg->hasByValAttr()) {
6121       addKnownBits(NO_WRITES);
6122       removeKnownBits(NO_READS);
6123       removeAssumedBits(NO_READS);
6124     }
6125     AAMemoryBehaviorArgument::initialize(A);
6126     if (getAssociatedFunction()->isDeclaration())
6127       indicatePessimisticFixpoint();
6128   }
6129 
6130   /// See AbstractAttribute::updateImpl(...).
6131   ChangeStatus updateImpl(Attributor &A) override {
6132     // TODO: Once we have call site specific value information we can provide
6133     //       call site specific liveness liveness information and then it makes
6134     //       sense to specialize attributes for call sites arguments instead of
6135     //       redirecting requests to the callee argument.
6136     Argument *Arg = getAssociatedArgument();
6137     const IRPosition &ArgPos = IRPosition::argument(*Arg);
6138     auto &ArgAA =
6139         A.getAAFor<AAMemoryBehavior>(*this, ArgPos, DepClassTy::REQUIRED);
6140     return clampStateAndIndicateChange(getState(), ArgAA.getState());
6141   }
6142 
6143   /// See AbstractAttribute::trackStatistics()
6144   void trackStatistics() const override {
6145     if (isAssumedReadNone())
6146       STATS_DECLTRACK_CSARG_ATTR(readnone)
6147     else if (isAssumedReadOnly())
6148       STATS_DECLTRACK_CSARG_ATTR(readonly)
6149     else if (isAssumedWriteOnly())
6150       STATS_DECLTRACK_CSARG_ATTR(writeonly)
6151   }
6152 };
6153 
6154 /// Memory behavior attribute for a call site return position.
6155 struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
6156   AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP, Attributor &A)
6157       : AAMemoryBehaviorFloating(IRP, A) {}
6158 
6159   /// See AbstractAttribute::initialize(...).
6160   void initialize(Attributor &A) override {
6161     AAMemoryBehaviorImpl::initialize(A);
6162     Function *F = getAssociatedFunction();
6163     if (!F || F->isDeclaration())
6164       indicatePessimisticFixpoint();
6165   }
6166 
6167   /// See AbstractAttribute::manifest(...).
6168   ChangeStatus manifest(Attributor &A) override {
6169     // We do not annotate returned values.
6170     return ChangeStatus::UNCHANGED;
6171   }
6172 
6173   /// See AbstractAttribute::trackStatistics()
6174   void trackStatistics() const override {}
6175 };
6176 
6177 /// An AA to represent the memory behavior function attributes.
6178 struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
6179   AAMemoryBehaviorFunction(const IRPosition &IRP, Attributor &A)
6180       : AAMemoryBehaviorImpl(IRP, A) {}
6181 
6182   /// See AbstractAttribute::updateImpl(Attributor &A).
6183   virtual ChangeStatus updateImpl(Attributor &A) override;
6184 
6185   /// See AbstractAttribute::manifest(...).
6186   ChangeStatus manifest(Attributor &A) override {
6187     Function &F = cast<Function>(getAnchorValue());
6188     if (isAssumedReadNone()) {
6189       F.removeFnAttr(Attribute::ArgMemOnly);
6190       F.removeFnAttr(Attribute::InaccessibleMemOnly);
6191       F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
6192     }
6193     return AAMemoryBehaviorImpl::manifest(A);
6194   }
6195 
6196   /// See AbstractAttribute::trackStatistics()
6197   void trackStatistics() const override {
6198     if (isAssumedReadNone())
6199       STATS_DECLTRACK_FN_ATTR(readnone)
6200     else if (isAssumedReadOnly())
6201       STATS_DECLTRACK_FN_ATTR(readonly)
6202     else if (isAssumedWriteOnly())
6203       STATS_DECLTRACK_FN_ATTR(writeonly)
6204   }
6205 };
6206 
6207 /// AAMemoryBehavior attribute for call sites.
6208 struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
6209   AAMemoryBehaviorCallSite(const IRPosition &IRP, Attributor &A)
6210       : AAMemoryBehaviorImpl(IRP, A) {}
6211 
6212   /// See AbstractAttribute::initialize(...).
6213   void initialize(Attributor &A) override {
6214     AAMemoryBehaviorImpl::initialize(A);
6215     Function *F = getAssociatedFunction();
6216     if (!F || F->isDeclaration())
6217       indicatePessimisticFixpoint();
6218   }
6219 
6220   /// See AbstractAttribute::updateImpl(...).
6221   ChangeStatus updateImpl(Attributor &A) override {
6222     // TODO: Once we have call site specific value information we can provide
6223     //       call site specific liveness liveness information and then it makes
6224     //       sense to specialize attributes for call sites arguments instead of
6225     //       redirecting requests to the callee argument.
6226     Function *F = getAssociatedFunction();
6227     const IRPosition &FnPos = IRPosition::function(*F);
6228     auto &FnAA =
6229         A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::REQUIRED);
6230     return clampStateAndIndicateChange(getState(), FnAA.getState());
6231   }
6232 
6233   /// See AbstractAttribute::trackStatistics()
6234   void trackStatistics() const override {
6235     if (isAssumedReadNone())
6236       STATS_DECLTRACK_CS_ATTR(readnone)
6237     else if (isAssumedReadOnly())
6238       STATS_DECLTRACK_CS_ATTR(readonly)
6239     else if (isAssumedWriteOnly())
6240       STATS_DECLTRACK_CS_ATTR(writeonly)
6241   }
6242 };
6243 
6244 ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
6245 
6246   // The current assumed state used to determine a change.
6247   auto AssumedState = getAssumed();
6248 
6249   auto CheckRWInst = [&](Instruction &I) {
6250     // If the instruction has an own memory behavior state, use it to restrict
6251     // the local state. No further analysis is required as the other memory
6252     // state is as optimistic as it gets.
6253     if (const auto *CB = dyn_cast<CallBase>(&I)) {
6254       const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
6255           *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
6256       intersectAssumedBits(MemBehaviorAA.getAssumed());
6257       return !isAtFixpoint();
6258     }
6259 
6260     // Remove access kind modifiers if necessary.
6261     if (I.mayReadFromMemory())
6262       removeAssumedBits(NO_READS);
6263     if (I.mayWriteToMemory())
6264       removeAssumedBits(NO_WRITES);
6265     return !isAtFixpoint();
6266   };
6267 
6268   if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this))
6269     return indicatePessimisticFixpoint();
6270 
6271   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
6272                                         : ChangeStatus::UNCHANGED;
6273 }
6274 
6275 ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
6276 
6277   const IRPosition &IRP = getIRPosition();
6278   const IRPosition &FnPos = IRPosition::function_scope(IRP);
6279   AAMemoryBehavior::StateType &S = getState();
6280 
6281   // First, check the function scope. We take the known information and we avoid
6282   // work if the assumed information implies the current assumed information for
6283   // this attribute. This is a valid for all but byval arguments.
6284   Argument *Arg = IRP.getAssociatedArgument();
6285   AAMemoryBehavior::base_t FnMemAssumedState =
6286       AAMemoryBehavior::StateType::getWorstState();
6287   if (!Arg || !Arg->hasByValAttr()) {
6288     const auto &FnMemAA =
6289         A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::OPTIONAL);
6290     FnMemAssumedState = FnMemAA.getAssumed();
6291     S.addKnownBits(FnMemAA.getKnown());
6292     if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
6293       return ChangeStatus::UNCHANGED;
6294   }
6295 
6296   // Make sure the value is not captured (except through "return"), if
6297   // it is, any information derived would be irrelevant anyway as we cannot
6298   // check the potential aliases introduced by the capture. However, no need
6299   // to fall back to anythign less optimistic than the function state.
6300   const auto &ArgNoCaptureAA =
6301       A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::OPTIONAL);
6302   if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
6303     S.intersectAssumedBits(FnMemAssumedState);
6304     return ChangeStatus::CHANGED;
6305   }
6306 
6307   // The current assumed state used to determine a change.
6308   auto AssumedState = S.getAssumed();
6309 
6310   // Liveness information to exclude dead users.
6311   // TODO: Take the FnPos once we have call site specific liveness information.
6312   const auto &LivenessAA = A.getAAFor<AAIsDead>(
6313       *this, IRPosition::function(*IRP.getAssociatedFunction()),
6314       DepClassTy::NONE);
6315 
6316   // Visit and expand uses until all are analyzed or a fixpoint is reached.
6317   for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) {
6318     const Use *U = Uses[i];
6319     Instruction *UserI = cast<Instruction>(U->getUser());
6320     LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI
6321                       << " [Dead: " << (A.isAssumedDead(*U, this, &LivenessAA))
6322                       << "]\n");
6323     if (A.isAssumedDead(*U, this, &LivenessAA))
6324       continue;
6325 
6326     // Droppable users, e.g., llvm::assume does not actually perform any action.
6327     if (UserI->isDroppable())
6328       continue;
6329 
6330     // Check if the users of UserI should also be visited.
6331     if (followUsersOfUseIn(A, U, UserI))
6332       addUsesOf(A, *UserI);
6333 
6334     // If UserI might touch memory we analyze the use in detail.
6335     if (UserI->mayReadOrWriteMemory())
6336       analyzeUseIn(A, U, UserI);
6337   }
6338 
6339   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
6340                                         : ChangeStatus::UNCHANGED;
6341 }
6342 
6343 void AAMemoryBehaviorFloating::addUsesOf(Attributor &A, const Value &V) {
6344   SmallVector<const Use *, 8> WL;
6345   for (const Use &U : V.uses())
6346     WL.push_back(&U);
6347 
6348   while (!WL.empty()) {
6349     const Use *U = WL.pop_back_val();
6350     if (!Visited.insert(U).second)
6351       continue;
6352 
6353     const Instruction *UserI = cast<Instruction>(U->getUser());
6354     if (UserI->mayReadOrWriteMemory()) {
6355       Uses.push_back(U);
6356       continue;
6357     }
6358     if (!followUsersOfUseIn(A, U, UserI))
6359       continue;
6360     for (const Use &UU : UserI->uses())
6361       WL.push_back(&UU);
6362   }
6363 }
6364 
6365 bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U,
6366                                                   const Instruction *UserI) {
6367   // The loaded value is unrelated to the pointer argument, no need to
6368   // follow the users of the load.
6369   if (isa<LoadInst>(UserI))
6370     return false;
6371 
6372   // By default we follow all uses assuming UserI might leak information on U,
6373   // we have special handling for call sites operands though.
6374   const auto *CB = dyn_cast<CallBase>(UserI);
6375   if (!CB || !CB->isArgOperand(U))
6376     return true;
6377 
6378   // If the use is a call argument known not to be captured, the users of
6379   // the call do not need to be visited because they have to be unrelated to
6380   // the input. Note that this check is not trivial even though we disallow
6381   // general capturing of the underlying argument. The reason is that the
6382   // call might the argument "through return", which we allow and for which we
6383   // need to check call users.
6384   if (U->get()->getType()->isPointerTy()) {
6385     unsigned ArgNo = CB->getArgOperandNo(U);
6386     const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
6387         *this, IRPosition::callsite_argument(*CB, ArgNo), DepClassTy::OPTIONAL);
6388     return !ArgNoCaptureAA.isAssumedNoCapture();
6389   }
6390 
6391   return true;
6392 }
6393 
6394 void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U,
6395                                             const Instruction *UserI) {
6396   assert(UserI->mayReadOrWriteMemory());
6397 
6398   switch (UserI->getOpcode()) {
6399   default:
6400     // TODO: Handle all atomics and other side-effect operations we know of.
6401     break;
6402   case Instruction::Load:
6403     // Loads cause the NO_READS property to disappear.
6404     removeAssumedBits(NO_READS);
6405     return;
6406 
6407   case Instruction::Store:
6408     // Stores cause the NO_WRITES property to disappear if the use is the
6409     // pointer operand. Note that we do assume that capturing was taken care of
6410     // somewhere else.
6411     if (cast<StoreInst>(UserI)->getPointerOperand() == U->get())
6412       removeAssumedBits(NO_WRITES);
6413     return;
6414 
6415   case Instruction::Call:
6416   case Instruction::CallBr:
6417   case Instruction::Invoke: {
6418     // For call sites we look at the argument memory behavior attribute (this
6419     // could be recursive!) in order to restrict our own state.
6420     const auto *CB = cast<CallBase>(UserI);
6421 
6422     // Give up on operand bundles.
6423     if (CB->isBundleOperand(U)) {
6424       indicatePessimisticFixpoint();
6425       return;
6426     }
6427 
6428     // Calling a function does read the function pointer, maybe write it if the
6429     // function is self-modifying.
6430     if (CB->isCallee(U)) {
6431       removeAssumedBits(NO_READS);
6432       break;
6433     }
6434 
6435     // Adjust the possible access behavior based on the information on the
6436     // argument.
6437     IRPosition Pos;
6438     if (U->get()->getType()->isPointerTy())
6439       Pos = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(U));
6440     else
6441       Pos = IRPosition::callsite_function(*CB);
6442     const auto &MemBehaviorAA =
6443         A.getAAFor<AAMemoryBehavior>(*this, Pos, DepClassTy::OPTIONAL);
6444     // "assumed" has at most the same bits as the MemBehaviorAA assumed
6445     // and at least "known".
6446     intersectAssumedBits(MemBehaviorAA.getAssumed());
6447     return;
6448   }
6449   };
6450 
6451   // Generally, look at the "may-properties" and adjust the assumed state if we
6452   // did not trigger special handling before.
6453   if (UserI->mayReadFromMemory())
6454     removeAssumedBits(NO_READS);
6455   if (UserI->mayWriteToMemory())
6456     removeAssumedBits(NO_WRITES);
6457 }
6458 
6459 } // namespace
6460 
6461 /// -------------------- Memory Locations Attributes ---------------------------
6462 /// Includes read-none, argmemonly, inaccessiblememonly,
6463 /// inaccessiblememorargmemonly
6464 /// ----------------------------------------------------------------------------
6465 
6466 std::string AAMemoryLocation::getMemoryLocationsAsStr(
6467     AAMemoryLocation::MemoryLocationsKind MLK) {
6468   if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS))
6469     return "all memory";
6470   if (MLK == AAMemoryLocation::NO_LOCATIONS)
6471     return "no memory";
6472   std::string S = "memory:";
6473   if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM))
6474     S += "stack,";
6475   if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM))
6476     S += "constant,";
6477   if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM))
6478     S += "internal global,";
6479   if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM))
6480     S += "external global,";
6481   if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM))
6482     S += "argument,";
6483   if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM))
6484     S += "inaccessible,";
6485   if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM))
6486     S += "malloced,";
6487   if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM))
6488     S += "unknown,";
6489   S.pop_back();
6490   return S;
6491 }
6492 
6493 namespace {
6494 struct AAMemoryLocationImpl : public AAMemoryLocation {
6495 
6496   AAMemoryLocationImpl(const IRPosition &IRP, Attributor &A)
6497       : AAMemoryLocation(IRP, A), Allocator(A.Allocator) {
6498     for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
6499       AccessKind2Accesses[u] = nullptr;
6500   }
6501 
6502   ~AAMemoryLocationImpl() {
6503     // The AccessSets are allocated via a BumpPtrAllocator, we call
6504     // the destructor manually.
6505     for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
6506       if (AccessKind2Accesses[u])
6507         AccessKind2Accesses[u]->~AccessSet();
6508   }
6509 
6510   /// See AbstractAttribute::initialize(...).
6511   void initialize(Attributor &A) override {
6512     intersectAssumedBits(BEST_STATE);
6513     getKnownStateFromValue(A, getIRPosition(), getState());
6514     AAMemoryLocation::initialize(A);
6515   }
6516 
6517   /// Return the memory behavior information encoded in the IR for \p IRP.
6518   static void getKnownStateFromValue(Attributor &A, const IRPosition &IRP,
6519                                      BitIntegerState &State,
6520                                      bool IgnoreSubsumingPositions = false) {
6521     // For internal functions we ignore `argmemonly` and
6522     // `inaccessiblememorargmemonly` as we might break it via interprocedural
6523     // constant propagation. It is unclear if this is the best way but it is
6524     // unlikely this will cause real performance problems. If we are deriving
6525     // attributes for the anchor function we even remove the attribute in
6526     // addition to ignoring it.
6527     bool UseArgMemOnly = true;
6528     Function *AnchorFn = IRP.getAnchorScope();
6529     if (AnchorFn && A.isRunOn(*AnchorFn))
6530       UseArgMemOnly = !AnchorFn->hasLocalLinkage();
6531 
6532     SmallVector<Attribute, 2> Attrs;
6533     IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
6534     for (const Attribute &Attr : Attrs) {
6535       switch (Attr.getKindAsEnum()) {
6536       case Attribute::ReadNone:
6537         State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM);
6538         break;
6539       case Attribute::InaccessibleMemOnly:
6540         State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
6541         break;
6542       case Attribute::ArgMemOnly:
6543         if (UseArgMemOnly)
6544           State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true));
6545         else
6546           IRP.removeAttrs({Attribute::ArgMemOnly});
6547         break;
6548       case Attribute::InaccessibleMemOrArgMemOnly:
6549         if (UseArgMemOnly)
6550           State.addKnownBits(inverseLocation(
6551               NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
6552         else
6553           IRP.removeAttrs({Attribute::InaccessibleMemOrArgMemOnly});
6554         break;
6555       default:
6556         llvm_unreachable("Unexpected attribute!");
6557       }
6558     }
6559   }
6560 
6561   /// See AbstractAttribute::getDeducedAttributes(...).
6562   void getDeducedAttributes(LLVMContext &Ctx,
6563                             SmallVectorImpl<Attribute> &Attrs) const override {
6564     assert(Attrs.size() == 0);
6565     if (isAssumedReadNone()) {
6566       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
6567     } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) {
6568       if (isAssumedInaccessibleMemOnly())
6569         Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly));
6570       else if (isAssumedArgMemOnly())
6571         Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly));
6572       else if (isAssumedInaccessibleOrArgMemOnly())
6573         Attrs.push_back(
6574             Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly));
6575     }
6576     assert(Attrs.size() <= 1);
6577   }
6578 
6579   /// See AbstractAttribute::manifest(...).
6580   ChangeStatus manifest(Attributor &A) override {
6581     const IRPosition &IRP = getIRPosition();
6582 
6583     // Check if we would improve the existing attributes first.
6584     SmallVector<Attribute, 4> DeducedAttrs;
6585     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
6586     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
6587           return IRP.hasAttr(Attr.getKindAsEnum(),
6588                              /* IgnoreSubsumingPositions */ true);
6589         }))
6590       return ChangeStatus::UNCHANGED;
6591 
6592     // Clear existing attributes.
6593     IRP.removeAttrs(AttrKinds);
6594     if (isAssumedReadNone())
6595       IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds);
6596 
6597     // Use the generic manifest method.
6598     return IRAttribute::manifest(A);
6599   }
6600 
6601   /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...).
6602   bool checkForAllAccessesToMemoryKind(
6603       function_ref<bool(const Instruction *, const Value *, AccessKind,
6604                         MemoryLocationsKind)>
6605           Pred,
6606       MemoryLocationsKind RequestedMLK) const override {
6607     if (!isValidState())
6608       return false;
6609 
6610     MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation();
6611     if (AssumedMLK == NO_LOCATIONS)
6612       return true;
6613 
6614     unsigned Idx = 0;
6615     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS;
6616          CurMLK *= 2, ++Idx) {
6617       if (CurMLK & RequestedMLK)
6618         continue;
6619 
6620       if (const AccessSet *Accesses = AccessKind2Accesses[Idx])
6621         for (const AccessInfo &AI : *Accesses)
6622           if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK))
6623             return false;
6624     }
6625 
6626     return true;
6627   }
6628 
6629   ChangeStatus indicatePessimisticFixpoint() override {
6630     // If we give up and indicate a pessimistic fixpoint this instruction will
6631     // become an access for all potential access kinds:
6632     // TODO: Add pointers for argmemonly and globals to improve the results of
6633     //       checkForAllAccessesToMemoryKind.
6634     bool Changed = false;
6635     MemoryLocationsKind KnownMLK = getKnown();
6636     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
6637     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
6638       if (!(CurMLK & KnownMLK))
6639         updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed,
6640                                   getAccessKindFromInst(I));
6641     return AAMemoryLocation::indicatePessimisticFixpoint();
6642   }
6643 
6644 protected:
6645   /// Helper struct to tie together an instruction that has a read or write
6646   /// effect with the pointer it accesses (if any).
6647   struct AccessInfo {
6648 
6649     /// The instruction that caused the access.
6650     const Instruction *I;
6651 
6652     /// The base pointer that is accessed, or null if unknown.
6653     const Value *Ptr;
6654 
6655     /// The kind of access (read/write/read+write).
6656     AccessKind Kind;
6657 
6658     bool operator==(const AccessInfo &RHS) const {
6659       return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind;
6660     }
6661     bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const {
6662       if (LHS.I != RHS.I)
6663         return LHS.I < RHS.I;
6664       if (LHS.Ptr != RHS.Ptr)
6665         return LHS.Ptr < RHS.Ptr;
6666       if (LHS.Kind != RHS.Kind)
6667         return LHS.Kind < RHS.Kind;
6668       return false;
6669     }
6670   };
6671 
6672   /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the
6673   /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind.
6674   using AccessSet = SmallSet<AccessInfo, 2, AccessInfo>;
6675   AccessSet *AccessKind2Accesses[llvm::CTLog2<VALID_STATE>()];
6676 
6677   /// Categorize the pointer arguments of CB that might access memory in
6678   /// AccessedLoc and update the state and access map accordingly.
6679   void
6680   categorizeArgumentPointerLocations(Attributor &A, CallBase &CB,
6681                                      AAMemoryLocation::StateType &AccessedLocs,
6682                                      bool &Changed);
6683 
6684   /// Return the kind(s) of location that may be accessed by \p V.
6685   AAMemoryLocation::MemoryLocationsKind
6686   categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
6687 
6688   /// Return the access kind as determined by \p I.
6689   AccessKind getAccessKindFromInst(const Instruction *I) {
6690     AccessKind AK = READ_WRITE;
6691     if (I) {
6692       AK = I->mayReadFromMemory() ? READ : NONE;
6693       AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE));
6694     }
6695     return AK;
6696   }
6697 
6698   /// Update the state \p State and the AccessKind2Accesses given that \p I is
6699   /// an access of kind \p AK to a \p MLK memory location with the access
6700   /// pointer \p Ptr.
6701   void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
6702                                  MemoryLocationsKind MLK, const Instruction *I,
6703                                  const Value *Ptr, bool &Changed,
6704                                  AccessKind AK = READ_WRITE) {
6705 
6706     assert(isPowerOf2_32(MLK) && "Expected a single location set!");
6707     auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)];
6708     if (!Accesses)
6709       Accesses = new (Allocator) AccessSet();
6710     Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second;
6711     State.removeAssumedBits(MLK);
6712   }
6713 
6714   /// Determine the underlying locations kinds for \p Ptr, e.g., globals or
6715   /// arguments, and update the state and access map accordingly.
6716   void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr,
6717                           AAMemoryLocation::StateType &State, bool &Changed);
6718 
6719   /// Used to allocate access sets.
6720   BumpPtrAllocator &Allocator;
6721 
6722   /// The set of IR attributes AAMemoryLocation deals with.
6723   static const Attribute::AttrKind AttrKinds[4];
6724 };
6725 
6726 const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = {
6727     Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly,
6728     Attribute::InaccessibleMemOrArgMemOnly};
6729 
6730 void AAMemoryLocationImpl::categorizePtrValue(
6731     Attributor &A, const Instruction &I, const Value &Ptr,
6732     AAMemoryLocation::StateType &State, bool &Changed) {
6733   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "
6734                     << Ptr << " ["
6735                     << getMemoryLocationsAsStr(State.getAssumed()) << "]\n");
6736 
6737   auto StripGEPCB = [](Value *V) -> Value * {
6738     auto *GEP = dyn_cast<GEPOperator>(V);
6739     while (GEP) {
6740       V = GEP->getPointerOperand();
6741       GEP = dyn_cast<GEPOperator>(V);
6742     }
6743     return V;
6744   };
6745 
6746   auto VisitValueCB = [&](Value &V, const Instruction *,
6747                           AAMemoryLocation::StateType &T,
6748                           bool Stripped) -> bool {
6749     // TODO: recognize the TBAA used for constant accesses.
6750     MemoryLocationsKind MLK = NO_LOCATIONS;
6751     assert(!isa<GEPOperator>(V) && "GEPs should have been stripped.");
6752     if (isa<UndefValue>(V))
6753       return true;
6754     if (auto *Arg = dyn_cast<Argument>(&V)) {
6755       if (Arg->hasByValAttr())
6756         MLK = NO_LOCAL_MEM;
6757       else
6758         MLK = NO_ARGUMENT_MEM;
6759     } else if (auto *GV = dyn_cast<GlobalValue>(&V)) {
6760       // Reading constant memory is not treated as a read "effect" by the
6761       // function attr pass so we won't neither. Constants defined by TBAA are
6762       // similar. (We know we do not write it because it is constant.)
6763       if (auto *GVar = dyn_cast<GlobalVariable>(GV))
6764         if (GVar->isConstant())
6765           return true;
6766 
6767       if (GV->hasLocalLinkage())
6768         MLK = NO_GLOBAL_INTERNAL_MEM;
6769       else
6770         MLK = NO_GLOBAL_EXTERNAL_MEM;
6771     } else if (isa<ConstantPointerNull>(V) &&
6772                !NullPointerIsDefined(getAssociatedFunction(),
6773                                      V.getType()->getPointerAddressSpace())) {
6774       return true;
6775     } else if (isa<AllocaInst>(V)) {
6776       MLK = NO_LOCAL_MEM;
6777     } else if (const auto *CB = dyn_cast<CallBase>(&V)) {
6778       const auto &NoAliasAA = A.getAAFor<AANoAlias>(
6779           *this, IRPosition::callsite_returned(*CB), DepClassTy::OPTIONAL);
6780       if (NoAliasAA.isAssumedNoAlias())
6781         MLK = NO_MALLOCED_MEM;
6782       else
6783         MLK = NO_UNKOWN_MEM;
6784     } else {
6785       MLK = NO_UNKOWN_MEM;
6786     }
6787 
6788     assert(MLK != NO_LOCATIONS && "No location specified!");
6789     updateStateAndAccessesMap(T, MLK, &I, &V, Changed,
6790                               getAccessKindFromInst(&I));
6791     LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value cannot be categorized: "
6792                       << V << " -> " << getMemoryLocationsAsStr(T.getAssumed())
6793                       << "\n");
6794     return true;
6795   };
6796 
6797   if (!genericValueTraversal<AAMemoryLocation, AAMemoryLocation::StateType>(
6798           A, IRPosition::value(Ptr), *this, State, VisitValueCB, getCtxI(),
6799           /* UseValueSimplify */ true,
6800           /* MaxValues */ 32, StripGEPCB)) {
6801     LLVM_DEBUG(
6802         dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n");
6803     updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed,
6804                               getAccessKindFromInst(&I));
6805   } else {
6806     LLVM_DEBUG(
6807         dbgs()
6808         << "[AAMemoryLocation] Accessed locations with pointer locations: "
6809         << getMemoryLocationsAsStr(State.getAssumed()) << "\n");
6810   }
6811 }
6812 
6813 void AAMemoryLocationImpl::categorizeArgumentPointerLocations(
6814     Attributor &A, CallBase &CB, AAMemoryLocation::StateType &AccessedLocs,
6815     bool &Changed) {
6816   for (unsigned ArgNo = 0, E = CB.getNumArgOperands(); ArgNo < E; ++ArgNo) {
6817 
6818     // Skip non-pointer arguments.
6819     const Value *ArgOp = CB.getArgOperand(ArgNo);
6820     if (!ArgOp->getType()->isPtrOrPtrVectorTy())
6821       continue;
6822 
6823     // Skip readnone arguments.
6824     const IRPosition &ArgOpIRP = IRPosition::callsite_argument(CB, ArgNo);
6825     const auto &ArgOpMemLocationAA =
6826         A.getAAFor<AAMemoryBehavior>(*this, ArgOpIRP, DepClassTy::OPTIONAL);
6827 
6828     if (ArgOpMemLocationAA.isAssumedReadNone())
6829       continue;
6830 
6831     // Categorize potentially accessed pointer arguments as if there was an
6832     // access instruction with them as pointer.
6833     categorizePtrValue(A, CB, *ArgOp, AccessedLocs, Changed);
6834   }
6835 }
6836 
6837 AAMemoryLocation::MemoryLocationsKind
6838 AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
6839                                                   bool &Changed) {
6840   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "
6841                     << I << "\n");
6842 
6843   AAMemoryLocation::StateType AccessedLocs;
6844   AccessedLocs.intersectAssumedBits(NO_LOCATIONS);
6845 
6846   if (auto *CB = dyn_cast<CallBase>(&I)) {
6847 
6848     // First check if we assume any memory is access is visible.
6849     const auto &CBMemLocationAA = A.getAAFor<AAMemoryLocation>(
6850         *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
6851     LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I
6852                       << " [" << CBMemLocationAA << "]\n");
6853 
6854     if (CBMemLocationAA.isAssumedReadNone())
6855       return NO_LOCATIONS;
6856 
6857     if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) {
6858       updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr,
6859                                 Changed, getAccessKindFromInst(&I));
6860       return AccessedLocs.getAssumed();
6861     }
6862 
6863     uint32_t CBAssumedNotAccessedLocs =
6864         CBMemLocationAA.getAssumedNotAccessedLocation();
6865 
6866     // Set the argmemonly and global bit as we handle them separately below.
6867     uint32_t CBAssumedNotAccessedLocsNoArgMem =
6868         CBAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM;
6869 
6870     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
6871       if (CBAssumedNotAccessedLocsNoArgMem & CurMLK)
6872         continue;
6873       updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed,
6874                                 getAccessKindFromInst(&I));
6875     }
6876 
6877     // Now handle global memory if it might be accessed. This is slightly tricky
6878     // as NO_GLOBAL_MEM has multiple bits set.
6879     bool HasGlobalAccesses = ((~CBAssumedNotAccessedLocs) & NO_GLOBAL_MEM);
6880     if (HasGlobalAccesses) {
6881       auto AccessPred = [&](const Instruction *, const Value *Ptr,
6882                             AccessKind Kind, MemoryLocationsKind MLK) {
6883         updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed,
6884                                   getAccessKindFromInst(&I));
6885         return true;
6886       };
6887       if (!CBMemLocationAA.checkForAllAccessesToMemoryKind(
6888               AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false)))
6889         return AccessedLocs.getWorstState();
6890     }
6891 
6892     LLVM_DEBUG(
6893         dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "
6894                << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
6895 
6896     // Now handle argument memory if it might be accessed.
6897     bool HasArgAccesses = ((~CBAssumedNotAccessedLocs) & NO_ARGUMENT_MEM);
6898     if (HasArgAccesses)
6899       categorizeArgumentPointerLocations(A, *CB, AccessedLocs, Changed);
6900 
6901     LLVM_DEBUG(
6902         dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "
6903                << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
6904 
6905     return AccessedLocs.getAssumed();
6906   }
6907 
6908   if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) {
6909     LLVM_DEBUG(
6910         dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "
6911                << I << " [" << *Ptr << "]\n");
6912     categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed);
6913     return AccessedLocs.getAssumed();
6914   }
6915 
6916   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "
6917                     << I << "\n");
6918   updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed,
6919                             getAccessKindFromInst(&I));
6920   return AccessedLocs.getAssumed();
6921 }
6922 
6923 /// An AA to represent the memory behavior function attributes.
6924 struct AAMemoryLocationFunction final : public AAMemoryLocationImpl {
6925   AAMemoryLocationFunction(const IRPosition &IRP, Attributor &A)
6926       : AAMemoryLocationImpl(IRP, A) {}
6927 
6928   /// See AbstractAttribute::updateImpl(Attributor &A).
6929   virtual ChangeStatus updateImpl(Attributor &A) override {
6930 
6931     const auto &MemBehaviorAA =
6932         A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
6933     if (MemBehaviorAA.isAssumedReadNone()) {
6934       if (MemBehaviorAA.isKnownReadNone())
6935         return indicateOptimisticFixpoint();
6936       assert(isAssumedReadNone() &&
6937              "AAMemoryLocation was not read-none but AAMemoryBehavior was!");
6938       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
6939       return ChangeStatus::UNCHANGED;
6940     }
6941 
6942     // The current assumed state used to determine a change.
6943     auto AssumedState = getAssumed();
6944     bool Changed = false;
6945 
6946     auto CheckRWInst = [&](Instruction &I) {
6947       MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed);
6948       LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I
6949                         << ": " << getMemoryLocationsAsStr(MLK) << "\n");
6950       removeAssumedBits(inverseLocation(MLK, false, false));
6951       // Stop once only the valid bit set in the *not assumed location*, thus
6952       // once we don't actually exclude any memory locations in the state.
6953       return getAssumedNotAccessedLocation() != VALID_STATE;
6954     };
6955 
6956     if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this))
6957       return indicatePessimisticFixpoint();
6958 
6959     Changed |= AssumedState != getAssumed();
6960     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
6961   }
6962 
6963   /// See AbstractAttribute::trackStatistics()
6964   void trackStatistics() const override {
6965     if (isAssumedReadNone())
6966       STATS_DECLTRACK_FN_ATTR(readnone)
6967     else if (isAssumedArgMemOnly())
6968       STATS_DECLTRACK_FN_ATTR(argmemonly)
6969     else if (isAssumedInaccessibleMemOnly())
6970       STATS_DECLTRACK_FN_ATTR(inaccessiblememonly)
6971     else if (isAssumedInaccessibleOrArgMemOnly())
6972       STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly)
6973   }
6974 };
6975 
6976 /// AAMemoryLocation attribute for call sites.
6977 struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
6978   AAMemoryLocationCallSite(const IRPosition &IRP, Attributor &A)
6979       : AAMemoryLocationImpl(IRP, A) {}
6980 
6981   /// See AbstractAttribute::initialize(...).
6982   void initialize(Attributor &A) override {
6983     AAMemoryLocationImpl::initialize(A);
6984     Function *F = getAssociatedFunction();
6985     if (!F || F->isDeclaration())
6986       indicatePessimisticFixpoint();
6987   }
6988 
6989   /// See AbstractAttribute::updateImpl(...).
6990   ChangeStatus updateImpl(Attributor &A) override {
6991     // TODO: Once we have call site specific value information we can provide
6992     //       call site specific liveness liveness information and then it makes
6993     //       sense to specialize attributes for call sites arguments instead of
6994     //       redirecting requests to the callee argument.
6995     Function *F = getAssociatedFunction();
6996     const IRPosition &FnPos = IRPosition::function(*F);
6997     auto &FnAA =
6998         A.getAAFor<AAMemoryLocation>(*this, FnPos, DepClassTy::REQUIRED);
6999     bool Changed = false;
7000     auto AccessPred = [&](const Instruction *I, const Value *Ptr,
7001                           AccessKind Kind, MemoryLocationsKind MLK) {
7002       updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed,
7003                                 getAccessKindFromInst(I));
7004       return true;
7005     };
7006     if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))
7007       return indicatePessimisticFixpoint();
7008     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
7009   }
7010 
7011   /// See AbstractAttribute::trackStatistics()
7012   void trackStatistics() const override {
7013     if (isAssumedReadNone())
7014       STATS_DECLTRACK_CS_ATTR(readnone)
7015   }
7016 };
7017 
7018 /// ------------------ Value Constant Range Attribute -------------------------
7019 
7020 struct AAValueConstantRangeImpl : AAValueConstantRange {
7021   using StateType = IntegerRangeState;
7022   AAValueConstantRangeImpl(const IRPosition &IRP, Attributor &A)
7023       : AAValueConstantRange(IRP, A) {}
7024 
7025   /// See AbstractAttribute::getAsStr().
7026   const std::string getAsStr() const override {
7027     std::string Str;
7028     llvm::raw_string_ostream OS(Str);
7029     OS << "range(" << getBitWidth() << ")<";
7030     getKnown().print(OS);
7031     OS << " / ";
7032     getAssumed().print(OS);
7033     OS << ">";
7034     return OS.str();
7035   }
7036 
7037   /// Helper function to get a SCEV expr for the associated value at program
7038   /// point \p I.
7039   const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
7040     if (!getAnchorScope())
7041       return nullptr;
7042 
7043     ScalarEvolution *SE =
7044         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
7045             *getAnchorScope());
7046 
7047     LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
7048         *getAnchorScope());
7049 
7050     if (!SE || !LI)
7051       return nullptr;
7052 
7053     const SCEV *S = SE->getSCEV(&getAssociatedValue());
7054     if (!I)
7055       return S;
7056 
7057     return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
7058   }
7059 
7060   /// Helper function to get a range from SCEV for the associated value at
7061   /// program point \p I.
7062   ConstantRange getConstantRangeFromSCEV(Attributor &A,
7063                                          const Instruction *I = nullptr) const {
7064     if (!getAnchorScope())
7065       return getWorstState(getBitWidth());
7066 
7067     ScalarEvolution *SE =
7068         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
7069             *getAnchorScope());
7070 
7071     const SCEV *S = getSCEV(A, I);
7072     if (!SE || !S)
7073       return getWorstState(getBitWidth());
7074 
7075     return SE->getUnsignedRange(S);
7076   }
7077 
7078   /// Helper function to get a range from LVI for the associated value at
7079   /// program point \p I.
7080   ConstantRange
7081   getConstantRangeFromLVI(Attributor &A,
7082                           const Instruction *CtxI = nullptr) const {
7083     if (!getAnchorScope())
7084       return getWorstState(getBitWidth());
7085 
7086     LazyValueInfo *LVI =
7087         A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
7088             *getAnchorScope());
7089 
7090     if (!LVI || !CtxI)
7091       return getWorstState(getBitWidth());
7092     return LVI->getConstantRange(&getAssociatedValue(),
7093                                  const_cast<Instruction *>(CtxI));
7094   }
7095 
7096   /// See AAValueConstantRange::getKnownConstantRange(..).
7097   ConstantRange
7098   getKnownConstantRange(Attributor &A,
7099                         const Instruction *CtxI = nullptr) const override {
7100     if (!CtxI || CtxI == getCtxI())
7101       return getKnown();
7102 
7103     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
7104     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
7105     return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
7106   }
7107 
7108   /// See AAValueConstantRange::getAssumedConstantRange(..).
7109   ConstantRange
7110   getAssumedConstantRange(Attributor &A,
7111                           const Instruction *CtxI = nullptr) const override {
7112     // TODO: Make SCEV use Attributor assumption.
7113     //       We may be able to bound a variable range via assumptions in
7114     //       Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
7115     //       evolve to x^2 + x, then we can say that y is in [2, 12].
7116 
7117     if (!CtxI || CtxI == getCtxI())
7118       return getAssumed();
7119 
7120     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
7121     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
7122     return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
7123   }
7124 
7125   /// See AbstractAttribute::initialize(..).
7126   void initialize(Attributor &A) override {
7127     // Intersect a range given by SCEV.
7128     intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
7129 
7130     // Intersect a range given by LVI.
7131     intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
7132   }
7133 
7134   /// Helper function to create MDNode for range metadata.
7135   static MDNode *
7136   getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
7137                             const ConstantRange &AssumedConstantRange) {
7138     Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
7139                                   Ty, AssumedConstantRange.getLower())),
7140                               ConstantAsMetadata::get(ConstantInt::get(
7141                                   Ty, AssumedConstantRange.getUpper()))};
7142     return MDNode::get(Ctx, LowAndHigh);
7143   }
7144 
7145   /// Return true if \p Assumed is included in \p KnownRanges.
7146   static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
7147 
7148     if (Assumed.isFullSet())
7149       return false;
7150 
7151     if (!KnownRanges)
7152       return true;
7153 
7154     // If multiple ranges are annotated in IR, we give up to annotate assumed
7155     // range for now.
7156 
7157     // TODO:  If there exists a known range which containts assumed range, we
7158     // can say assumed range is better.
7159     if (KnownRanges->getNumOperands() > 2)
7160       return false;
7161 
7162     ConstantInt *Lower =
7163         mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
7164     ConstantInt *Upper =
7165         mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
7166 
7167     ConstantRange Known(Lower->getValue(), Upper->getValue());
7168     return Known.contains(Assumed) && Known != Assumed;
7169   }
7170 
7171   /// Helper function to set range metadata.
7172   static bool
7173   setRangeMetadataIfisBetterRange(Instruction *I,
7174                                   const ConstantRange &AssumedConstantRange) {
7175     auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
7176     if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
7177       if (!AssumedConstantRange.isEmptySet()) {
7178         I->setMetadata(LLVMContext::MD_range,
7179                        getMDNodeForConstantRange(I->getType(), I->getContext(),
7180                                                  AssumedConstantRange));
7181         return true;
7182       }
7183     }
7184     return false;
7185   }
7186 
7187   /// See AbstractAttribute::manifest()
7188   ChangeStatus manifest(Attributor &A) override {
7189     ChangeStatus Changed = ChangeStatus::UNCHANGED;
7190     ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
7191     assert(!AssumedConstantRange.isFullSet() && "Invalid state");
7192 
7193     auto &V = getAssociatedValue();
7194     if (!AssumedConstantRange.isEmptySet() &&
7195         !AssumedConstantRange.isSingleElement()) {
7196       if (Instruction *I = dyn_cast<Instruction>(&V)) {
7197         assert(I == getCtxI() && "Should not annotate an instruction which is "
7198                                  "not the context instruction");
7199         if (isa<CallInst>(I) || isa<LoadInst>(I))
7200           if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
7201             Changed = ChangeStatus::CHANGED;
7202       }
7203     }
7204 
7205     return Changed;
7206   }
7207 };
7208 
7209 struct AAValueConstantRangeArgument final
7210     : AAArgumentFromCallSiteArguments<
7211           AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
7212           true /* BridgeCallBaseContext */> {
7213   using Base = AAArgumentFromCallSiteArguments<
7214       AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
7215       true /* BridgeCallBaseContext */>;
7216   AAValueConstantRangeArgument(const IRPosition &IRP, Attributor &A)
7217       : Base(IRP, A) {}
7218 
7219   /// See AbstractAttribute::initialize(..).
7220   void initialize(Attributor &A) override {
7221     if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
7222       indicatePessimisticFixpoint();
7223     } else {
7224       Base::initialize(A);
7225     }
7226   }
7227 
7228   /// See AbstractAttribute::trackStatistics()
7229   void trackStatistics() const override {
7230     STATS_DECLTRACK_ARG_ATTR(value_range)
7231   }
7232 };
7233 
7234 struct AAValueConstantRangeReturned
7235     : AAReturnedFromReturnedValues<AAValueConstantRange,
7236                                    AAValueConstantRangeImpl,
7237                                    AAValueConstantRangeImpl::StateType,
7238                                    /* PropogateCallBaseContext */ true> {
7239   using Base =
7240       AAReturnedFromReturnedValues<AAValueConstantRange,
7241                                    AAValueConstantRangeImpl,
7242                                    AAValueConstantRangeImpl::StateType,
7243                                    /* PropogateCallBaseContext */ true>;
7244   AAValueConstantRangeReturned(const IRPosition &IRP, Attributor &A)
7245       : Base(IRP, A) {}
7246 
7247   /// See AbstractAttribute::initialize(...).
7248   void initialize(Attributor &A) override {}
7249 
7250   /// See AbstractAttribute::trackStatistics()
7251   void trackStatistics() const override {
7252     STATS_DECLTRACK_FNRET_ATTR(value_range)
7253   }
7254 };
7255 
7256 struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
7257   AAValueConstantRangeFloating(const IRPosition &IRP, Attributor &A)
7258       : AAValueConstantRangeImpl(IRP, A) {}
7259 
7260   /// See AbstractAttribute::initialize(...).
7261   void initialize(Attributor &A) override {
7262     AAValueConstantRangeImpl::initialize(A);
7263     Value &V = getAssociatedValue();
7264 
7265     if (auto *C = dyn_cast<ConstantInt>(&V)) {
7266       unionAssumed(ConstantRange(C->getValue()));
7267       indicateOptimisticFixpoint();
7268       return;
7269     }
7270 
7271     if (isa<UndefValue>(&V)) {
7272       // Collapse the undef state to 0.
7273       unionAssumed(ConstantRange(APInt(getBitWidth(), 0)));
7274       indicateOptimisticFixpoint();
7275       return;
7276     }
7277 
7278     if (isa<CallBase>(&V))
7279       return;
7280 
7281     if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V))
7282       return;
7283     // If it is a load instruction with range metadata, use it.
7284     if (LoadInst *LI = dyn_cast<LoadInst>(&V))
7285       if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
7286         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
7287         return;
7288       }
7289 
7290     // We can work with PHI and select instruction as we traverse their operands
7291     // during update.
7292     if (isa<SelectInst>(V) || isa<PHINode>(V))
7293       return;
7294 
7295     // Otherwise we give up.
7296     indicatePessimisticFixpoint();
7297 
7298     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
7299                       << getAssociatedValue() << "\n");
7300   }
7301 
7302   bool calculateBinaryOperator(
7303       Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
7304       const Instruction *CtxI,
7305       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
7306     Value *LHS = BinOp->getOperand(0);
7307     Value *RHS = BinOp->getOperand(1);
7308     // TODO: Allow non integers as well.
7309     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
7310       return false;
7311 
7312     auto &LHSAA = A.getAAFor<AAValueConstantRange>(
7313         *this, IRPosition::value(*LHS, getCallBaseContext()),
7314         DepClassTy::REQUIRED);
7315     QuerriedAAs.push_back(&LHSAA);
7316     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
7317 
7318     auto &RHSAA = A.getAAFor<AAValueConstantRange>(
7319         *this, IRPosition::value(*RHS, getCallBaseContext()),
7320         DepClassTy::REQUIRED);
7321     QuerriedAAs.push_back(&RHSAA);
7322     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
7323 
7324     auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
7325 
7326     T.unionAssumed(AssumedRange);
7327 
7328     // TODO: Track a known state too.
7329 
7330     return T.isValidState();
7331   }
7332 
7333   bool calculateCastInst(
7334       Attributor &A, CastInst *CastI, IntegerRangeState &T,
7335       const Instruction *CtxI,
7336       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
7337     assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
7338     // TODO: Allow non integers as well.
7339     Value &OpV = *CastI->getOperand(0);
7340     if (!OpV.getType()->isIntegerTy())
7341       return false;
7342 
7343     auto &OpAA = A.getAAFor<AAValueConstantRange>(
7344         *this, IRPosition::value(OpV, getCallBaseContext()),
7345         DepClassTy::REQUIRED);
7346     QuerriedAAs.push_back(&OpAA);
7347     T.unionAssumed(
7348         OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
7349     return T.isValidState();
7350   }
7351 
7352   bool
7353   calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
7354                    const Instruction *CtxI,
7355                    SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
7356     Value *LHS = CmpI->getOperand(0);
7357     Value *RHS = CmpI->getOperand(1);
7358     // TODO: Allow non integers as well.
7359     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
7360       return false;
7361 
7362     auto &LHSAA = A.getAAFor<AAValueConstantRange>(
7363         *this, IRPosition::value(*LHS, getCallBaseContext()),
7364         DepClassTy::REQUIRED);
7365     QuerriedAAs.push_back(&LHSAA);
7366     auto &RHSAA = A.getAAFor<AAValueConstantRange>(
7367         *this, IRPosition::value(*RHS, getCallBaseContext()),
7368         DepClassTy::REQUIRED);
7369     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
7370     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
7371 
7372     // If one of them is empty set, we can't decide.
7373     if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
7374       return true;
7375 
7376     bool MustTrue = false, MustFalse = false;
7377 
7378     auto AllowedRegion =
7379         ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
7380 
7381     auto SatisfyingRegion = ConstantRange::makeSatisfyingICmpRegion(
7382         CmpI->getPredicate(), RHSAARange);
7383 
7384     if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
7385       MustFalse = true;
7386 
7387     if (SatisfyingRegion.contains(LHSAARange))
7388       MustTrue = true;
7389 
7390     assert((!MustTrue || !MustFalse) &&
7391            "Either MustTrue or MustFalse should be false!");
7392 
7393     if (MustTrue)
7394       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
7395     else if (MustFalse)
7396       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
7397     else
7398       T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
7399 
7400     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
7401                       << " " << RHSAA << "\n");
7402 
7403     // TODO: Track a known state too.
7404     return T.isValidState();
7405   }
7406 
7407   /// See AbstractAttribute::updateImpl(...).
7408   ChangeStatus updateImpl(Attributor &A) override {
7409     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
7410                             IntegerRangeState &T, bool Stripped) -> bool {
7411       Instruction *I = dyn_cast<Instruction>(&V);
7412       if (!I || isa<CallBase>(I)) {
7413 
7414         // If the value is not instruction, we query AA to Attributor.
7415         const auto &AA = A.getAAFor<AAValueConstantRange>(
7416             *this, IRPosition::value(V), DepClassTy::REQUIRED);
7417 
7418         // Clamp operator is not used to utilize a program point CtxI.
7419         T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
7420 
7421         return T.isValidState();
7422       }
7423 
7424       SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
7425       if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
7426         if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
7427           return false;
7428       } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
7429         if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
7430           return false;
7431       } else if (auto *CastI = dyn_cast<CastInst>(I)) {
7432         if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
7433           return false;
7434       } else {
7435         // Give up with other instructions.
7436         // TODO: Add other instructions
7437 
7438         T.indicatePessimisticFixpoint();
7439         return false;
7440       }
7441 
7442       // Catch circular reasoning in a pessimistic way for now.
7443       // TODO: Check how the range evolves and if we stripped anything, see also
7444       //       AADereferenceable or AAAlign for similar situations.
7445       for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
7446         if (QueriedAA != this)
7447           continue;
7448         // If we are in a stady state we do not need to worry.
7449         if (T.getAssumed() == getState().getAssumed())
7450           continue;
7451         T.indicatePessimisticFixpoint();
7452       }
7453 
7454       return T.isValidState();
7455     };
7456 
7457     IntegerRangeState T(getBitWidth());
7458 
7459     if (!genericValueTraversal<AAValueConstantRange, IntegerRangeState>(
7460             A, getIRPosition(), *this, T, VisitValueCB, getCtxI(),
7461             /* UseValueSimplify */ false))
7462       return indicatePessimisticFixpoint();
7463 
7464     return clampStateAndIndicateChange(getState(), T);
7465   }
7466 
7467   /// See AbstractAttribute::trackStatistics()
7468   void trackStatistics() const override {
7469     STATS_DECLTRACK_FLOATING_ATTR(value_range)
7470   }
7471 };
7472 
7473 struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
7474   AAValueConstantRangeFunction(const IRPosition &IRP, Attributor &A)
7475       : AAValueConstantRangeImpl(IRP, A) {}
7476 
7477   /// See AbstractAttribute::initialize(...).
7478   ChangeStatus updateImpl(Attributor &A) override {
7479     llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
7480                      "not be called");
7481   }
7482 
7483   /// See AbstractAttribute::trackStatistics()
7484   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
7485 };
7486 
7487 struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
7488   AAValueConstantRangeCallSite(const IRPosition &IRP, Attributor &A)
7489       : AAValueConstantRangeFunction(IRP, A) {}
7490 
7491   /// See AbstractAttribute::trackStatistics()
7492   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
7493 };
7494 
7495 struct AAValueConstantRangeCallSiteReturned
7496     : AACallSiteReturnedFromReturned<AAValueConstantRange,
7497                                      AAValueConstantRangeImpl,
7498                                      AAValueConstantRangeImpl::StateType,
7499                                      /* IntroduceCallBaseContext */ true> {
7500   AAValueConstantRangeCallSiteReturned(const IRPosition &IRP, Attributor &A)
7501       : AACallSiteReturnedFromReturned<AAValueConstantRange,
7502                                        AAValueConstantRangeImpl,
7503                                        AAValueConstantRangeImpl::StateType,
7504                                        /* IntroduceCallBaseContext */ true>(IRP,
7505                                                                             A) {
7506   }
7507 
7508   /// See AbstractAttribute::initialize(...).
7509   void initialize(Attributor &A) override {
7510     // If it is a load instruction with range metadata, use the metadata.
7511     if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
7512       if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
7513         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
7514 
7515     AAValueConstantRangeImpl::initialize(A);
7516   }
7517 
7518   /// See AbstractAttribute::trackStatistics()
7519   void trackStatistics() const override {
7520     STATS_DECLTRACK_CSRET_ATTR(value_range)
7521   }
7522 };
7523 struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
7524   AAValueConstantRangeCallSiteArgument(const IRPosition &IRP, Attributor &A)
7525       : AAValueConstantRangeFloating(IRP, A) {}
7526 
7527   /// See AbstractAttribute::manifest()
7528   ChangeStatus manifest(Attributor &A) override {
7529     return ChangeStatus::UNCHANGED;
7530   }
7531 
7532   /// See AbstractAttribute::trackStatistics()
7533   void trackStatistics() const override {
7534     STATS_DECLTRACK_CSARG_ATTR(value_range)
7535   }
7536 };
7537 
7538 /// ------------------ Potential Values Attribute -------------------------
7539 
7540 struct AAPotentialValuesImpl : AAPotentialValues {
7541   using StateType = PotentialConstantIntValuesState;
7542 
7543   AAPotentialValuesImpl(const IRPosition &IRP, Attributor &A)
7544       : AAPotentialValues(IRP, A) {}
7545 
7546   /// See AbstractAttribute::getAsStr().
7547   const std::string getAsStr() const override {
7548     std::string Str;
7549     llvm::raw_string_ostream OS(Str);
7550     OS << getState();
7551     return OS.str();
7552   }
7553 
7554   /// See AbstractAttribute::updateImpl(...).
7555   ChangeStatus updateImpl(Attributor &A) override {
7556     return indicatePessimisticFixpoint();
7557   }
7558 };
7559 
7560 struct AAPotentialValuesArgument final
7561     : AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
7562                                       PotentialConstantIntValuesState> {
7563   using Base =
7564       AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
7565                                       PotentialConstantIntValuesState>;
7566   AAPotentialValuesArgument(const IRPosition &IRP, Attributor &A)
7567       : Base(IRP, A) {}
7568 
7569   /// See AbstractAttribute::initialize(..).
7570   void initialize(Attributor &A) override {
7571     if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
7572       indicatePessimisticFixpoint();
7573     } else {
7574       Base::initialize(A);
7575     }
7576   }
7577 
7578   /// See AbstractAttribute::trackStatistics()
7579   void trackStatistics() const override {
7580     STATS_DECLTRACK_ARG_ATTR(potential_values)
7581   }
7582 };
7583 
7584 struct AAPotentialValuesReturned
7585     : AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl> {
7586   using Base =
7587       AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl>;
7588   AAPotentialValuesReturned(const IRPosition &IRP, Attributor &A)
7589       : Base(IRP, A) {}
7590 
7591   /// See AbstractAttribute::trackStatistics()
7592   void trackStatistics() const override {
7593     STATS_DECLTRACK_FNRET_ATTR(potential_values)
7594   }
7595 };
7596 
7597 struct AAPotentialValuesFloating : AAPotentialValuesImpl {
7598   AAPotentialValuesFloating(const IRPosition &IRP, Attributor &A)
7599       : AAPotentialValuesImpl(IRP, A) {}
7600 
7601   /// See AbstractAttribute::initialize(..).
7602   void initialize(Attributor &A) override {
7603     Value &V = getAssociatedValue();
7604 
7605     if (auto *C = dyn_cast<ConstantInt>(&V)) {
7606       unionAssumed(C->getValue());
7607       indicateOptimisticFixpoint();
7608       return;
7609     }
7610 
7611     if (isa<UndefValue>(&V)) {
7612       unionAssumedWithUndef();
7613       indicateOptimisticFixpoint();
7614       return;
7615     }
7616 
7617     if (isa<BinaryOperator>(&V) || isa<ICmpInst>(&V) || isa<CastInst>(&V))
7618       return;
7619 
7620     if (isa<SelectInst>(V) || isa<PHINode>(V))
7621       return;
7622 
7623     indicatePessimisticFixpoint();
7624 
7625     LLVM_DEBUG(dbgs() << "[AAPotentialValues] We give up: "
7626                       << getAssociatedValue() << "\n");
7627   }
7628 
7629   static bool calculateICmpInst(const ICmpInst *ICI, const APInt &LHS,
7630                                 const APInt &RHS) {
7631     ICmpInst::Predicate Pred = ICI->getPredicate();
7632     switch (Pred) {
7633     case ICmpInst::ICMP_UGT:
7634       return LHS.ugt(RHS);
7635     case ICmpInst::ICMP_SGT:
7636       return LHS.sgt(RHS);
7637     case ICmpInst::ICMP_EQ:
7638       return LHS.eq(RHS);
7639     case ICmpInst::ICMP_UGE:
7640       return LHS.uge(RHS);
7641     case ICmpInst::ICMP_SGE:
7642       return LHS.sge(RHS);
7643     case ICmpInst::ICMP_ULT:
7644       return LHS.ult(RHS);
7645     case ICmpInst::ICMP_SLT:
7646       return LHS.slt(RHS);
7647     case ICmpInst::ICMP_NE:
7648       return LHS.ne(RHS);
7649     case ICmpInst::ICMP_ULE:
7650       return LHS.ule(RHS);
7651     case ICmpInst::ICMP_SLE:
7652       return LHS.sle(RHS);
7653     default:
7654       llvm_unreachable("Invalid ICmp predicate!");
7655     }
7656   }
7657 
7658   static APInt calculateCastInst(const CastInst *CI, const APInt &Src,
7659                                  uint32_t ResultBitWidth) {
7660     Instruction::CastOps CastOp = CI->getOpcode();
7661     switch (CastOp) {
7662     default:
7663       llvm_unreachable("unsupported or not integer cast");
7664     case Instruction::Trunc:
7665       return Src.trunc(ResultBitWidth);
7666     case Instruction::SExt:
7667       return Src.sext(ResultBitWidth);
7668     case Instruction::ZExt:
7669       return Src.zext(ResultBitWidth);
7670     case Instruction::BitCast:
7671       return Src;
7672     }
7673   }
7674 
7675   static APInt calculateBinaryOperator(const BinaryOperator *BinOp,
7676                                        const APInt &LHS, const APInt &RHS,
7677                                        bool &SkipOperation, bool &Unsupported) {
7678     Instruction::BinaryOps BinOpcode = BinOp->getOpcode();
7679     // Unsupported is set to true when the binary operator is not supported.
7680     // SkipOperation is set to true when UB occur with the given operand pair
7681     // (LHS, RHS).
7682     // TODO: we should look at nsw and nuw keywords to handle operations
7683     //       that create poison or undef value.
7684     switch (BinOpcode) {
7685     default:
7686       Unsupported = true;
7687       return LHS;
7688     case Instruction::Add:
7689       return LHS + RHS;
7690     case Instruction::Sub:
7691       return LHS - RHS;
7692     case Instruction::Mul:
7693       return LHS * RHS;
7694     case Instruction::UDiv:
7695       if (RHS.isNullValue()) {
7696         SkipOperation = true;
7697         return LHS;
7698       }
7699       return LHS.udiv(RHS);
7700     case Instruction::SDiv:
7701       if (RHS.isNullValue()) {
7702         SkipOperation = true;
7703         return LHS;
7704       }
7705       return LHS.sdiv(RHS);
7706     case Instruction::URem:
7707       if (RHS.isNullValue()) {
7708         SkipOperation = true;
7709         return LHS;
7710       }
7711       return LHS.urem(RHS);
7712     case Instruction::SRem:
7713       if (RHS.isNullValue()) {
7714         SkipOperation = true;
7715         return LHS;
7716       }
7717       return LHS.srem(RHS);
7718     case Instruction::Shl:
7719       return LHS.shl(RHS);
7720     case Instruction::LShr:
7721       return LHS.lshr(RHS);
7722     case Instruction::AShr:
7723       return LHS.ashr(RHS);
7724     case Instruction::And:
7725       return LHS & RHS;
7726     case Instruction::Or:
7727       return LHS | RHS;
7728     case Instruction::Xor:
7729       return LHS ^ RHS;
7730     }
7731   }
7732 
7733   bool calculateBinaryOperatorAndTakeUnion(const BinaryOperator *BinOp,
7734                                            const APInt &LHS, const APInt &RHS) {
7735     bool SkipOperation = false;
7736     bool Unsupported = false;
7737     APInt Result =
7738         calculateBinaryOperator(BinOp, LHS, RHS, SkipOperation, Unsupported);
7739     if (Unsupported)
7740       return false;
7741     // If SkipOperation is true, we can ignore this operand pair (L, R).
7742     if (!SkipOperation)
7743       unionAssumed(Result);
7744     return isValidState();
7745   }
7746 
7747   ChangeStatus updateWithICmpInst(Attributor &A, ICmpInst *ICI) {
7748     auto AssumedBefore = getAssumed();
7749     Value *LHS = ICI->getOperand(0);
7750     Value *RHS = ICI->getOperand(1);
7751     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
7752       return indicatePessimisticFixpoint();
7753 
7754     auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
7755                                                 DepClassTy::REQUIRED);
7756     if (!LHSAA.isValidState())
7757       return indicatePessimisticFixpoint();
7758 
7759     auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
7760                                                 DepClassTy::REQUIRED);
7761     if (!RHSAA.isValidState())
7762       return indicatePessimisticFixpoint();
7763 
7764     const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
7765     const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
7766 
7767     // TODO: make use of undef flag to limit potential values aggressively.
7768     bool MaybeTrue = false, MaybeFalse = false;
7769     const APInt Zero(RHS->getType()->getIntegerBitWidth(), 0);
7770     if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
7771       // The result of any comparison between undefs can be soundly replaced
7772       // with undef.
7773       unionAssumedWithUndef();
7774     } else if (LHSAA.undefIsContained()) {
7775       bool MaybeTrue = false, MaybeFalse = false;
7776       for (const APInt &R : RHSAAPVS) {
7777         bool CmpResult = calculateICmpInst(ICI, Zero, R);
7778         MaybeTrue |= CmpResult;
7779         MaybeFalse |= !CmpResult;
7780         if (MaybeTrue & MaybeFalse)
7781           return indicatePessimisticFixpoint();
7782       }
7783     } else if (RHSAA.undefIsContained()) {
7784       for (const APInt &L : LHSAAPVS) {
7785         bool CmpResult = calculateICmpInst(ICI, L, Zero);
7786         MaybeTrue |= CmpResult;
7787         MaybeFalse |= !CmpResult;
7788         if (MaybeTrue & MaybeFalse)
7789           return indicatePessimisticFixpoint();
7790       }
7791     } else {
7792       for (const APInt &L : LHSAAPVS) {
7793         for (const APInt &R : RHSAAPVS) {
7794           bool CmpResult = calculateICmpInst(ICI, L, R);
7795           MaybeTrue |= CmpResult;
7796           MaybeFalse |= !CmpResult;
7797           if (MaybeTrue & MaybeFalse)
7798             return indicatePessimisticFixpoint();
7799         }
7800       }
7801     }
7802     if (MaybeTrue)
7803       unionAssumed(APInt(/* numBits */ 1, /* val */ 1));
7804     if (MaybeFalse)
7805       unionAssumed(APInt(/* numBits */ 1, /* val */ 0));
7806     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
7807                                          : ChangeStatus::CHANGED;
7808   }
7809 
7810   ChangeStatus updateWithSelectInst(Attributor &A, SelectInst *SI) {
7811     auto AssumedBefore = getAssumed();
7812     Value *LHS = SI->getTrueValue();
7813     Value *RHS = SI->getFalseValue();
7814     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
7815       return indicatePessimisticFixpoint();
7816 
7817     // TODO: Use assumed simplified condition value
7818     auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
7819                                                 DepClassTy::REQUIRED);
7820     if (!LHSAA.isValidState())
7821       return indicatePessimisticFixpoint();
7822 
7823     auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
7824                                                 DepClassTy::REQUIRED);
7825     if (!RHSAA.isValidState())
7826       return indicatePessimisticFixpoint();
7827 
7828     if (LHSAA.undefIsContained() && RHSAA.undefIsContained())
7829       // select i1 *, undef , undef => undef
7830       unionAssumedWithUndef();
7831     else {
7832       unionAssumed(LHSAA);
7833       unionAssumed(RHSAA);
7834     }
7835     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
7836                                          : ChangeStatus::CHANGED;
7837   }
7838 
7839   ChangeStatus updateWithCastInst(Attributor &A, CastInst *CI) {
7840     auto AssumedBefore = getAssumed();
7841     if (!CI->isIntegerCast())
7842       return indicatePessimisticFixpoint();
7843     assert(CI->getNumOperands() == 1 && "Expected cast to be unary!");
7844     uint32_t ResultBitWidth = CI->getDestTy()->getIntegerBitWidth();
7845     Value *Src = CI->getOperand(0);
7846     auto &SrcAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*Src),
7847                                                 DepClassTy::REQUIRED);
7848     if (!SrcAA.isValidState())
7849       return indicatePessimisticFixpoint();
7850     const DenseSet<APInt> &SrcAAPVS = SrcAA.getAssumedSet();
7851     if (SrcAA.undefIsContained())
7852       unionAssumedWithUndef();
7853     else {
7854       for (const APInt &S : SrcAAPVS) {
7855         APInt T = calculateCastInst(CI, S, ResultBitWidth);
7856         unionAssumed(T);
7857       }
7858     }
7859     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
7860                                          : ChangeStatus::CHANGED;
7861   }
7862 
7863   ChangeStatus updateWithBinaryOperator(Attributor &A, BinaryOperator *BinOp) {
7864     auto AssumedBefore = getAssumed();
7865     Value *LHS = BinOp->getOperand(0);
7866     Value *RHS = BinOp->getOperand(1);
7867     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
7868       return indicatePessimisticFixpoint();
7869 
7870     auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
7871                                                 DepClassTy::REQUIRED);
7872     if (!LHSAA.isValidState())
7873       return indicatePessimisticFixpoint();
7874 
7875     auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
7876                                                 DepClassTy::REQUIRED);
7877     if (!RHSAA.isValidState())
7878       return indicatePessimisticFixpoint();
7879 
7880     const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
7881     const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
7882     const APInt Zero = APInt(LHS->getType()->getIntegerBitWidth(), 0);
7883 
7884     // TODO: make use of undef flag to limit potential values aggressively.
7885     if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
7886       if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, Zero))
7887         return indicatePessimisticFixpoint();
7888     } else if (LHSAA.undefIsContained()) {
7889       for (const APInt &R : RHSAAPVS) {
7890         if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, R))
7891           return indicatePessimisticFixpoint();
7892       }
7893     } else if (RHSAA.undefIsContained()) {
7894       for (const APInt &L : LHSAAPVS) {
7895         if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, Zero))
7896           return indicatePessimisticFixpoint();
7897       }
7898     } else {
7899       for (const APInt &L : LHSAAPVS) {
7900         for (const APInt &R : RHSAAPVS) {
7901           if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, R))
7902             return indicatePessimisticFixpoint();
7903         }
7904       }
7905     }
7906     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
7907                                          : ChangeStatus::CHANGED;
7908   }
7909 
7910   ChangeStatus updateWithPHINode(Attributor &A, PHINode *PHI) {
7911     auto AssumedBefore = getAssumed();
7912     for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
7913       Value *IncomingValue = PHI->getIncomingValue(u);
7914       auto &PotentialValuesAA = A.getAAFor<AAPotentialValues>(
7915           *this, IRPosition::value(*IncomingValue), DepClassTy::REQUIRED);
7916       if (!PotentialValuesAA.isValidState())
7917         return indicatePessimisticFixpoint();
7918       if (PotentialValuesAA.undefIsContained())
7919         unionAssumedWithUndef();
7920       else
7921         unionAssumed(PotentialValuesAA.getAssumed());
7922     }
7923     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
7924                                          : ChangeStatus::CHANGED;
7925   }
7926 
7927   /// See AbstractAttribute::updateImpl(...).
7928   ChangeStatus updateImpl(Attributor &A) override {
7929     Value &V = getAssociatedValue();
7930     Instruction *I = dyn_cast<Instruction>(&V);
7931 
7932     if (auto *ICI = dyn_cast<ICmpInst>(I))
7933       return updateWithICmpInst(A, ICI);
7934 
7935     if (auto *SI = dyn_cast<SelectInst>(I))
7936       return updateWithSelectInst(A, SI);
7937 
7938     if (auto *CI = dyn_cast<CastInst>(I))
7939       return updateWithCastInst(A, CI);
7940 
7941     if (auto *BinOp = dyn_cast<BinaryOperator>(I))
7942       return updateWithBinaryOperator(A, BinOp);
7943 
7944     if (auto *PHI = dyn_cast<PHINode>(I))
7945       return updateWithPHINode(A, PHI);
7946 
7947     return indicatePessimisticFixpoint();
7948   }
7949 
7950   /// See AbstractAttribute::trackStatistics()
7951   void trackStatistics() const override {
7952     STATS_DECLTRACK_FLOATING_ATTR(potential_values)
7953   }
7954 };
7955 
7956 struct AAPotentialValuesFunction : AAPotentialValuesImpl {
7957   AAPotentialValuesFunction(const IRPosition &IRP, Attributor &A)
7958       : AAPotentialValuesImpl(IRP, A) {}
7959 
7960   /// See AbstractAttribute::initialize(...).
7961   ChangeStatus updateImpl(Attributor &A) override {
7962     llvm_unreachable("AAPotentialValues(Function|CallSite)::updateImpl will "
7963                      "not be called");
7964   }
7965 
7966   /// See AbstractAttribute::trackStatistics()
7967   void trackStatistics() const override {
7968     STATS_DECLTRACK_FN_ATTR(potential_values)
7969   }
7970 };
7971 
7972 struct AAPotentialValuesCallSite : AAPotentialValuesFunction {
7973   AAPotentialValuesCallSite(const IRPosition &IRP, Attributor &A)
7974       : AAPotentialValuesFunction(IRP, A) {}
7975 
7976   /// See AbstractAttribute::trackStatistics()
7977   void trackStatistics() const override {
7978     STATS_DECLTRACK_CS_ATTR(potential_values)
7979   }
7980 };
7981 
7982 struct AAPotentialValuesCallSiteReturned
7983     : AACallSiteReturnedFromReturned<AAPotentialValues, AAPotentialValuesImpl> {
7984   AAPotentialValuesCallSiteReturned(const IRPosition &IRP, Attributor &A)
7985       : AACallSiteReturnedFromReturned<AAPotentialValues,
7986                                        AAPotentialValuesImpl>(IRP, A) {}
7987 
7988   /// See AbstractAttribute::trackStatistics()
7989   void trackStatistics() const override {
7990     STATS_DECLTRACK_CSRET_ATTR(potential_values)
7991   }
7992 };
7993 
7994 struct AAPotentialValuesCallSiteArgument : AAPotentialValuesFloating {
7995   AAPotentialValuesCallSiteArgument(const IRPosition &IRP, Attributor &A)
7996       : AAPotentialValuesFloating(IRP, A) {}
7997 
7998   /// See AbstractAttribute::initialize(..).
7999   void initialize(Attributor &A) override {
8000     Value &V = getAssociatedValue();
8001 
8002     if (auto *C = dyn_cast<ConstantInt>(&V)) {
8003       unionAssumed(C->getValue());
8004       indicateOptimisticFixpoint();
8005       return;
8006     }
8007 
8008     if (isa<UndefValue>(&V)) {
8009       unionAssumedWithUndef();
8010       indicateOptimisticFixpoint();
8011       return;
8012     }
8013   }
8014 
8015   /// See AbstractAttribute::updateImpl(...).
8016   ChangeStatus updateImpl(Attributor &A) override {
8017     Value &V = getAssociatedValue();
8018     auto AssumedBefore = getAssumed();
8019     auto &AA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(V),
8020                                              DepClassTy::REQUIRED);
8021     const auto &S = AA.getAssumed();
8022     unionAssumed(S);
8023     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
8024                                          : ChangeStatus::CHANGED;
8025   }
8026 
8027   /// See AbstractAttribute::trackStatistics()
8028   void trackStatistics() const override {
8029     STATS_DECLTRACK_CSARG_ATTR(potential_values)
8030   }
8031 };
8032 
8033 /// ------------------------ NoUndef Attribute ---------------------------------
8034 struct AANoUndefImpl : AANoUndef {
8035   AANoUndefImpl(const IRPosition &IRP, Attributor &A) : AANoUndef(IRP, A) {}
8036 
8037   /// See AbstractAttribute::initialize(...).
8038   void initialize(Attributor &A) override {
8039     if (getIRPosition().hasAttr({Attribute::NoUndef})) {
8040       indicateOptimisticFixpoint();
8041       return;
8042     }
8043     Value &V = getAssociatedValue();
8044     if (isa<UndefValue>(V))
8045       indicatePessimisticFixpoint();
8046     else if (isa<FreezeInst>(V))
8047       indicateOptimisticFixpoint();
8048     else if (getPositionKind() != IRPosition::IRP_RETURNED &&
8049              isGuaranteedNotToBeUndefOrPoison(&V))
8050       indicateOptimisticFixpoint();
8051     else
8052       AANoUndef::initialize(A);
8053   }
8054 
8055   /// See followUsesInMBEC
8056   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
8057                        AANoUndef::StateType &State) {
8058     const Value *UseV = U->get();
8059     const DominatorTree *DT = nullptr;
8060     AssumptionCache *AC = nullptr;
8061     InformationCache &InfoCache = A.getInfoCache();
8062     if (Function *F = getAnchorScope()) {
8063       DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
8064       AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
8065     }
8066     State.setKnown(isGuaranteedNotToBeUndefOrPoison(UseV, AC, I, DT));
8067     bool TrackUse = false;
8068     // Track use for instructions which must produce undef or poison bits when
8069     // at least one operand contains such bits.
8070     if (isa<CastInst>(*I) || isa<GetElementPtrInst>(*I))
8071       TrackUse = true;
8072     return TrackUse;
8073   }
8074 
8075   /// See AbstractAttribute::getAsStr().
8076   const std::string getAsStr() const override {
8077     return getAssumed() ? "noundef" : "may-undef-or-poison";
8078   }
8079 
8080   ChangeStatus manifest(Attributor &A) override {
8081     // We don't manifest noundef attribute for dead positions because the
8082     // associated values with dead positions would be replaced with undef
8083     // values.
8084     if (A.isAssumedDead(getIRPosition(), nullptr, nullptr))
8085       return ChangeStatus::UNCHANGED;
8086     // A position whose simplified value does not have any value is
8087     // considered to be dead. We don't manifest noundef in such positions for
8088     // the same reason above.
8089     auto &ValueSimplifyAA =
8090         A.getAAFor<AAValueSimplify>(*this, getIRPosition(), DepClassTy::NONE);
8091     if (!ValueSimplifyAA.getAssumedSimplifiedValue(A).hasValue())
8092       return ChangeStatus::UNCHANGED;
8093     return AANoUndef::manifest(A);
8094   }
8095 };
8096 
8097 struct AANoUndefFloating : public AANoUndefImpl {
8098   AANoUndefFloating(const IRPosition &IRP, Attributor &A)
8099       : AANoUndefImpl(IRP, A) {}
8100 
8101   /// See AbstractAttribute::initialize(...).
8102   void initialize(Attributor &A) override {
8103     AANoUndefImpl::initialize(A);
8104     if (!getState().isAtFixpoint())
8105       if (Instruction *CtxI = getCtxI())
8106         followUsesInMBEC(*this, A, getState(), *CtxI);
8107   }
8108 
8109   /// See AbstractAttribute::updateImpl(...).
8110   ChangeStatus updateImpl(Attributor &A) override {
8111     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
8112                             AANoUndef::StateType &T, bool Stripped) -> bool {
8113       const auto &AA = A.getAAFor<AANoUndef>(*this, IRPosition::value(V),
8114                                              DepClassTy::REQUIRED);
8115       if (!Stripped && this == &AA) {
8116         T.indicatePessimisticFixpoint();
8117       } else {
8118         const AANoUndef::StateType &S =
8119             static_cast<const AANoUndef::StateType &>(AA.getState());
8120         T ^= S;
8121       }
8122       return T.isValidState();
8123     };
8124 
8125     StateType T;
8126     if (!genericValueTraversal<AANoUndef, StateType>(
8127             A, getIRPosition(), *this, T, VisitValueCB, getCtxI()))
8128       return indicatePessimisticFixpoint();
8129 
8130     return clampStateAndIndicateChange(getState(), T);
8131   }
8132 
8133   /// See AbstractAttribute::trackStatistics()
8134   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
8135 };
8136 
8137 struct AANoUndefReturned final
8138     : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl> {
8139   AANoUndefReturned(const IRPosition &IRP, Attributor &A)
8140       : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl>(IRP, A) {}
8141 
8142   /// See AbstractAttribute::trackStatistics()
8143   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
8144 };
8145 
8146 struct AANoUndefArgument final
8147     : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl> {
8148   AANoUndefArgument(const IRPosition &IRP, Attributor &A)
8149       : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl>(IRP, A) {}
8150 
8151   /// See AbstractAttribute::trackStatistics()
8152   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noundef) }
8153 };
8154 
8155 struct AANoUndefCallSiteArgument final : AANoUndefFloating {
8156   AANoUndefCallSiteArgument(const IRPosition &IRP, Attributor &A)
8157       : AANoUndefFloating(IRP, A) {}
8158 
8159   /// See AbstractAttribute::trackStatistics()
8160   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noundef) }
8161 };
8162 
8163 struct AANoUndefCallSiteReturned final
8164     : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl> {
8165   AANoUndefCallSiteReturned(const IRPosition &IRP, Attributor &A)
8166       : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl>(IRP, A) {}
8167 
8168   /// See AbstractAttribute::trackStatistics()
8169   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noundef) }
8170 };
8171 } // namespace
8172 
8173 const char AAReturnedValues::ID = 0;
8174 const char AANoUnwind::ID = 0;
8175 const char AANoSync::ID = 0;
8176 const char AANoFree::ID = 0;
8177 const char AANonNull::ID = 0;
8178 const char AANoRecurse::ID = 0;
8179 const char AAWillReturn::ID = 0;
8180 const char AAUndefinedBehavior::ID = 0;
8181 const char AANoAlias::ID = 0;
8182 const char AAReachability::ID = 0;
8183 const char AANoReturn::ID = 0;
8184 const char AAIsDead::ID = 0;
8185 const char AADereferenceable::ID = 0;
8186 const char AAAlign::ID = 0;
8187 const char AANoCapture::ID = 0;
8188 const char AAValueSimplify::ID = 0;
8189 const char AAHeapToStack::ID = 0;
8190 const char AAPrivatizablePtr::ID = 0;
8191 const char AAMemoryBehavior::ID = 0;
8192 const char AAMemoryLocation::ID = 0;
8193 const char AAValueConstantRange::ID = 0;
8194 const char AAPotentialValues::ID = 0;
8195 const char AANoUndef::ID = 0;
8196 
8197 // Macro magic to create the static generator function for attributes that
8198 // follow the naming scheme.
8199 
8200 #define SWITCH_PK_INV(CLASS, PK, POS_NAME)                                     \
8201   case IRPosition::PK:                                                         \
8202     llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
8203 
8204 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX)                               \
8205   case IRPosition::PK:                                                         \
8206     AA = new (A.Allocator) CLASS##SUFFIX(IRP, A);                              \
8207     ++NumAAs;                                                                  \
8208     break;
8209 
8210 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                 \
8211   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8212     CLASS *AA = nullptr;                                                       \
8213     switch (IRP.getPositionKind()) {                                           \
8214       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8215       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
8216       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
8217       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
8218       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
8219       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
8220       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
8221       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
8222     }                                                                          \
8223     return *AA;                                                                \
8224   }
8225 
8226 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                    \
8227   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8228     CLASS *AA = nullptr;                                                       \
8229     switch (IRP.getPositionKind()) {                                           \
8230       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8231       SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function")                           \
8232       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
8233       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
8234       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
8235       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
8236       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
8237       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
8238     }                                                                          \
8239     return *AA;                                                                \
8240   }
8241 
8242 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                      \
8243   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8244     CLASS *AA = nullptr;                                                       \
8245     switch (IRP.getPositionKind()) {                                           \
8246       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8247       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
8248       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
8249       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
8250       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
8251       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
8252       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
8253       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
8254     }                                                                          \
8255     return *AA;                                                                \
8256   }
8257 
8258 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)            \
8259   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8260     CLASS *AA = nullptr;                                                       \
8261     switch (IRP.getPositionKind()) {                                           \
8262       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8263       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
8264       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
8265       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
8266       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
8267       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
8268       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
8269       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
8270     }                                                                          \
8271     return *AA;                                                                \
8272   }
8273 
8274 #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                  \
8275   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8276     CLASS *AA = nullptr;                                                       \
8277     switch (IRP.getPositionKind()) {                                           \
8278       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8279       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
8280       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
8281       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
8282       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
8283       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
8284       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
8285       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
8286     }                                                                          \
8287     return *AA;                                                                \
8288   }
8289 
8290 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
8291 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
8292 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
8293 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
8294 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
8295 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
8296 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation)
8297 
8298 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
8299 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
8300 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
8301 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
8302 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
8303 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
8304 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
8305 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialValues)
8306 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUndef)
8307 
8308 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
8309 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
8310 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
8311 
8312 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
8313 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
8314 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
8315 
8316 CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
8317 
8318 #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
8319 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
8320 #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
8321 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
8322 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
8323 #undef SWITCH_PK_CREATE
8324 #undef SWITCH_PK_INV
8325