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