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