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