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