xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/IPO/AttributorAttributes.cpp (revision 1838bd0f4839006b42d41a02a787b7f578655223)
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() {}
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 =
2695         [&](Value &V, const SmallSetVector<ReturnInst *, 4> RetInsts) {
2696           // Check if a return instruction always cause UB or not
2697           // Note: It is guaranteed that the returned position of the anchor
2698           //       scope has noundef attribute when this is called.
2699           //       We also ensure the return position is not "assumed dead"
2700           //       because the returned value was then potentially simplified to
2701           //       `undef` in AAReturnedValues without removing the `noundef`
2702           //       attribute yet.
2703 
2704           // When the returned position has noundef attriubte, UB occur in the
2705           // following cases.
2706           //   (1) Returned value is known to be undef.
2707           //   (2) The value is known to be a null pointer and the returned
2708           //       position has nonnull attribute (because the returned value is
2709           //       poison).
2710           bool FoundUB = false;
2711           if (isa<UndefValue>(V)) {
2712             FoundUB = true;
2713           } else {
2714             if (isa<ConstantPointerNull>(V)) {
2715               auto &NonNullAA = A.getAAFor<AANonNull>(
2716                   *this, IRPosition::returned(*getAnchorScope()),
2717                   DepClassTy::NONE);
2718               if (NonNullAA.isKnownNonNull())
2719                 FoundUB = true;
2720             }
2721           }
2722 
2723           if (FoundUB)
2724             for (ReturnInst *RI : RetInsts)
2725               KnownUBInsts.insert(RI);
2726           return true;
2727         };
2728 
2729     bool UsedAssumedInformation = false;
2730     A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
2731                               {Instruction::Load, Instruction::Store,
2732                                Instruction::AtomicCmpXchg,
2733                                Instruction::AtomicRMW},
2734                               UsedAssumedInformation,
2735                               /* CheckBBLivenessOnly */ true);
2736     A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
2737                               UsedAssumedInformation,
2738                               /* CheckBBLivenessOnly */ true);
2739     A.checkForAllCallLikeInstructions(InspectCallSiteForUB, *this,
2740                                       UsedAssumedInformation);
2741 
2742     // If the returned position of the anchor scope has noundef attriubte, check
2743     // all returned instructions.
2744     if (!getAnchorScope()->getReturnType()->isVoidTy()) {
2745       const IRPosition &ReturnIRP = IRPosition::returned(*getAnchorScope());
2746       if (!A.isAssumedDead(ReturnIRP, this, nullptr, UsedAssumedInformation)) {
2747         auto &RetPosNoUndefAA =
2748             A.getAAFor<AANoUndef>(*this, ReturnIRP, DepClassTy::NONE);
2749         if (RetPosNoUndefAA.isKnownNoUndef())
2750           A.checkForAllReturnedValuesAndReturnInsts(InspectReturnInstForUB,
2751                                                     *this);
2752       }
2753     }
2754 
2755     if (NoUBPrevSize != AssumedNoUBInsts.size() ||
2756         UBPrevSize != KnownUBInsts.size())
2757       return ChangeStatus::CHANGED;
2758     return ChangeStatus::UNCHANGED;
2759   }
2760 
2761   bool isKnownToCauseUB(Instruction *I) const override {
2762     return KnownUBInsts.count(I);
2763   }
2764 
2765   bool isAssumedToCauseUB(Instruction *I) const override {
2766     // In simple words, if an instruction is not in the assumed to _not_
2767     // cause UB, then it is assumed UB (that includes those
2768     // in the KnownUBInsts set). The rest is boilerplate
2769     // is to ensure that it is one of the instructions we test
2770     // for UB.
2771 
2772     switch (I->getOpcode()) {
2773     case Instruction::Load:
2774     case Instruction::Store:
2775     case Instruction::AtomicCmpXchg:
2776     case Instruction::AtomicRMW:
2777       return !AssumedNoUBInsts.count(I);
2778     case Instruction::Br: {
2779       auto BrInst = cast<BranchInst>(I);
2780       if (BrInst->isUnconditional())
2781         return false;
2782       return !AssumedNoUBInsts.count(I);
2783     } break;
2784     default:
2785       return false;
2786     }
2787     return false;
2788   }
2789 
2790   ChangeStatus manifest(Attributor &A) override {
2791     if (KnownUBInsts.empty())
2792       return ChangeStatus::UNCHANGED;
2793     for (Instruction *I : KnownUBInsts)
2794       A.changeToUnreachableAfterManifest(I);
2795     return ChangeStatus::CHANGED;
2796   }
2797 
2798   /// See AbstractAttribute::getAsStr()
2799   const std::string getAsStr() const override {
2800     return getAssumed() ? "undefined-behavior" : "no-ub";
2801   }
2802 
2803   /// Note: The correctness of this analysis depends on the fact that the
2804   /// following 2 sets will stop changing after some point.
2805   /// "Change" here means that their size changes.
2806   /// The size of each set is monotonically increasing
2807   /// (we only add items to them) and it is upper bounded by the number of
2808   /// instructions in the processed function (we can never save more
2809   /// elements in either set than this number). Hence, at some point,
2810   /// they will stop increasing.
2811   /// Consequently, at some point, both sets will have stopped
2812   /// changing, effectively making the analysis reach a fixpoint.
2813 
2814   /// Note: These 2 sets are disjoint and an instruction can be considered
2815   /// one of 3 things:
2816   /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
2817   ///    the KnownUBInsts set.
2818   /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
2819   ///    has a reason to assume it).
2820   /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
2821   ///    could not find a reason to assume or prove that it can cause UB,
2822   ///    hence it assumes it doesn't. We have a set for these instructions
2823   ///    so that we don't reprocess them in every update.
2824   ///    Note however that instructions in this set may cause UB.
2825 
2826 protected:
2827   /// A set of all live instructions _known_ to cause UB.
2828   SmallPtrSet<Instruction *, 8> KnownUBInsts;
2829 
2830 private:
2831   /// A set of all the (live) instructions that are assumed to _not_ cause UB.
2832   SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
2833 
2834   // Should be called on updates in which if we're processing an instruction
2835   // \p I that depends on a value \p V, one of the following has to happen:
2836   // - If the value is assumed, then stop.
2837   // - If the value is known but undef, then consider it UB.
2838   // - Otherwise, do specific processing with the simplified value.
2839   // We return None in the first 2 cases to signify that an appropriate
2840   // action was taken and the caller should stop.
2841   // Otherwise, we return the simplified value that the caller should
2842   // use for specific processing.
2843   Optional<Value *> stopOnUndefOrAssumed(Attributor &A, Value *V,
2844                                          Instruction *I) {
2845     bool UsedAssumedInformation = false;
2846     Optional<Value *> SimplifiedV = A.getAssumedSimplified(
2847         IRPosition::value(*V), *this, UsedAssumedInformation);
2848     if (!UsedAssumedInformation) {
2849       // Don't depend on assumed values.
2850       if (!SimplifiedV.hasValue()) {
2851         // If it is known (which we tested above) but it doesn't have a value,
2852         // then we can assume `undef` and hence the instruction is UB.
2853         KnownUBInsts.insert(I);
2854         return llvm::None;
2855       }
2856       if (!SimplifiedV.getValue())
2857         return nullptr;
2858       V = *SimplifiedV;
2859     }
2860     if (isa<UndefValue>(V)) {
2861       KnownUBInsts.insert(I);
2862       return llvm::None;
2863     }
2864     return V;
2865   }
2866 };
2867 
2868 struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
2869   AAUndefinedBehaviorFunction(const IRPosition &IRP, Attributor &A)
2870       : AAUndefinedBehaviorImpl(IRP, A) {}
2871 
2872   /// See AbstractAttribute::trackStatistics()
2873   void trackStatistics() const override {
2874     STATS_DECL(UndefinedBehaviorInstruction, Instruction,
2875                "Number of instructions known to have UB");
2876     BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) +=
2877         KnownUBInsts.size();
2878   }
2879 };
2880 
2881 /// ------------------------ Will-Return Attributes ----------------------------
2882 
2883 // Helper function that checks whether a function has any cycle which we don't
2884 // know if it is bounded or not.
2885 // Loops with maximum trip count are considered bounded, any other cycle not.
2886 static bool mayContainUnboundedCycle(Function &F, Attributor &A) {
2887   ScalarEvolution *SE =
2888       A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F);
2889   LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F);
2890   // If either SCEV or LoopInfo is not available for the function then we assume
2891   // any cycle to be unbounded cycle.
2892   // We use scc_iterator which uses Tarjan algorithm to find all the maximal
2893   // SCCs.To detect if there's a cycle, we only need to find the maximal ones.
2894   if (!SE || !LI) {
2895     for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI)
2896       if (SCCI.hasCycle())
2897         return true;
2898     return false;
2899   }
2900 
2901   // If there's irreducible control, the function may contain non-loop cycles.
2902   if (mayContainIrreducibleControl(F, LI))
2903     return true;
2904 
2905   // Any loop that does not have a max trip count is considered unbounded cycle.
2906   for (auto *L : LI->getLoopsInPreorder()) {
2907     if (!SE->getSmallConstantMaxTripCount(L))
2908       return true;
2909   }
2910   return false;
2911 }
2912 
2913 struct AAWillReturnImpl : public AAWillReturn {
2914   AAWillReturnImpl(const IRPosition &IRP, Attributor &A)
2915       : AAWillReturn(IRP, A) {}
2916 
2917   /// See AbstractAttribute::initialize(...).
2918   void initialize(Attributor &A) override {
2919     AAWillReturn::initialize(A);
2920 
2921     if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ true)) {
2922       indicateOptimisticFixpoint();
2923       return;
2924     }
2925   }
2926 
2927   /// Check for `mustprogress` and `readonly` as they imply `willreturn`.
2928   bool isImpliedByMustprogressAndReadonly(Attributor &A, bool KnownOnly) {
2929     // Check for `mustprogress` in the scope and the associated function which
2930     // might be different if this is a call site.
2931     if ((!getAnchorScope() || !getAnchorScope()->mustProgress()) &&
2932         (!getAssociatedFunction() || !getAssociatedFunction()->mustProgress()))
2933       return false;
2934 
2935     bool IsKnown;
2936     if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
2937       return IsKnown || !KnownOnly;
2938     return false;
2939   }
2940 
2941   /// See AbstractAttribute::updateImpl(...).
2942   ChangeStatus updateImpl(Attributor &A) override {
2943     if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
2944       return ChangeStatus::UNCHANGED;
2945 
2946     auto CheckForWillReturn = [&](Instruction &I) {
2947       IRPosition IPos = IRPosition::callsite_function(cast<CallBase>(I));
2948       const auto &WillReturnAA =
2949           A.getAAFor<AAWillReturn>(*this, IPos, DepClassTy::REQUIRED);
2950       if (WillReturnAA.isKnownWillReturn())
2951         return true;
2952       if (!WillReturnAA.isAssumedWillReturn())
2953         return false;
2954       const auto &NoRecurseAA =
2955           A.getAAFor<AANoRecurse>(*this, IPos, DepClassTy::REQUIRED);
2956       return NoRecurseAA.isAssumedNoRecurse();
2957     };
2958 
2959     bool UsedAssumedInformation = false;
2960     if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this,
2961                                            UsedAssumedInformation))
2962       return indicatePessimisticFixpoint();
2963 
2964     return ChangeStatus::UNCHANGED;
2965   }
2966 
2967   /// See AbstractAttribute::getAsStr()
2968   const std::string getAsStr() const override {
2969     return getAssumed() ? "willreturn" : "may-noreturn";
2970   }
2971 };
2972 
2973 struct AAWillReturnFunction final : AAWillReturnImpl {
2974   AAWillReturnFunction(const IRPosition &IRP, Attributor &A)
2975       : AAWillReturnImpl(IRP, A) {}
2976 
2977   /// See AbstractAttribute::initialize(...).
2978   void initialize(Attributor &A) override {
2979     AAWillReturnImpl::initialize(A);
2980 
2981     Function *F = getAnchorScope();
2982     if (!F || F->isDeclaration() || mayContainUnboundedCycle(*F, A))
2983       indicatePessimisticFixpoint();
2984   }
2985 
2986   /// See AbstractAttribute::trackStatistics()
2987   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
2988 };
2989 
2990 /// WillReturn attribute deduction for a call sites.
2991 struct AAWillReturnCallSite final : AAWillReturnImpl {
2992   AAWillReturnCallSite(const IRPosition &IRP, Attributor &A)
2993       : AAWillReturnImpl(IRP, A) {}
2994 
2995   /// See AbstractAttribute::initialize(...).
2996   void initialize(Attributor &A) override {
2997     AAWillReturnImpl::initialize(A);
2998     Function *F = getAssociatedFunction();
2999     if (!F || !A.isFunctionIPOAmendable(*F))
3000       indicatePessimisticFixpoint();
3001   }
3002 
3003   /// See AbstractAttribute::updateImpl(...).
3004   ChangeStatus updateImpl(Attributor &A) override {
3005     if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
3006       return ChangeStatus::UNCHANGED;
3007 
3008     // TODO: Once we have call site specific value information we can provide
3009     //       call site specific liveness information and then it makes
3010     //       sense to specialize attributes for call sites arguments instead of
3011     //       redirecting requests to the callee argument.
3012     Function *F = getAssociatedFunction();
3013     const IRPosition &FnPos = IRPosition::function(*F);
3014     auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos, DepClassTy::REQUIRED);
3015     return clampStateAndIndicateChange(getState(), FnAA.getState());
3016   }
3017 
3018   /// See AbstractAttribute::trackStatistics()
3019   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
3020 };
3021 
3022 /// -------------------AAReachability Attribute--------------------------
3023 
3024 struct AAReachabilityImpl : AAReachability {
3025   AAReachabilityImpl(const IRPosition &IRP, Attributor &A)
3026       : AAReachability(IRP, A) {}
3027 
3028   const std::string getAsStr() const override {
3029     // TODO: Return the number of reachable queries.
3030     return "reachable";
3031   }
3032 
3033   /// See AbstractAttribute::updateImpl(...).
3034   ChangeStatus updateImpl(Attributor &A) override {
3035     const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
3036         *this, IRPosition::function(*getAnchorScope()), DepClassTy::REQUIRED);
3037     if (!NoRecurseAA.isAssumedNoRecurse())
3038       return indicatePessimisticFixpoint();
3039     return ChangeStatus::UNCHANGED;
3040   }
3041 };
3042 
3043 struct AAReachabilityFunction final : public AAReachabilityImpl {
3044   AAReachabilityFunction(const IRPosition &IRP, Attributor &A)
3045       : AAReachabilityImpl(IRP, A) {}
3046 
3047   /// See AbstractAttribute::trackStatistics()
3048   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); }
3049 };
3050 
3051 /// ------------------------ NoAlias Argument Attribute ------------------------
3052 
3053 struct AANoAliasImpl : AANoAlias {
3054   AANoAliasImpl(const IRPosition &IRP, Attributor &A) : AANoAlias(IRP, A) {
3055     assert(getAssociatedType()->isPointerTy() &&
3056            "Noalias is a pointer attribute");
3057   }
3058 
3059   const std::string getAsStr() const override {
3060     return getAssumed() ? "noalias" : "may-alias";
3061   }
3062 };
3063 
3064 /// NoAlias attribute for a floating value.
3065 struct AANoAliasFloating final : AANoAliasImpl {
3066   AANoAliasFloating(const IRPosition &IRP, Attributor &A)
3067       : AANoAliasImpl(IRP, A) {}
3068 
3069   /// See AbstractAttribute::initialize(...).
3070   void initialize(Attributor &A) override {
3071     AANoAliasImpl::initialize(A);
3072     Value *Val = &getAssociatedValue();
3073     do {
3074       CastInst *CI = dyn_cast<CastInst>(Val);
3075       if (!CI)
3076         break;
3077       Value *Base = CI->getOperand(0);
3078       if (!Base->hasOneUse())
3079         break;
3080       Val = Base;
3081     } while (true);
3082 
3083     if (!Val->getType()->isPointerTy()) {
3084       indicatePessimisticFixpoint();
3085       return;
3086     }
3087 
3088     if (isa<AllocaInst>(Val))
3089       indicateOptimisticFixpoint();
3090     else if (isa<ConstantPointerNull>(Val) &&
3091              !NullPointerIsDefined(getAnchorScope(),
3092                                    Val->getType()->getPointerAddressSpace()))
3093       indicateOptimisticFixpoint();
3094     else if (Val != &getAssociatedValue()) {
3095       const auto &ValNoAliasAA = A.getAAFor<AANoAlias>(
3096           *this, IRPosition::value(*Val), DepClassTy::OPTIONAL);
3097       if (ValNoAliasAA.isKnownNoAlias())
3098         indicateOptimisticFixpoint();
3099     }
3100   }
3101 
3102   /// See AbstractAttribute::updateImpl(...).
3103   ChangeStatus updateImpl(Attributor &A) override {
3104     // TODO: Implement this.
3105     return indicatePessimisticFixpoint();
3106   }
3107 
3108   /// See AbstractAttribute::trackStatistics()
3109   void trackStatistics() const override {
3110     STATS_DECLTRACK_FLOATING_ATTR(noalias)
3111   }
3112 };
3113 
3114 /// NoAlias attribute for an argument.
3115 struct AANoAliasArgument final
3116     : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
3117   using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
3118   AANoAliasArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
3119 
3120   /// See AbstractAttribute::initialize(...).
3121   void initialize(Attributor &A) override {
3122     Base::initialize(A);
3123     // See callsite argument attribute and callee argument attribute.
3124     if (hasAttr({Attribute::ByVal}))
3125       indicateOptimisticFixpoint();
3126   }
3127 
3128   /// See AbstractAttribute::update(...).
3129   ChangeStatus updateImpl(Attributor &A) override {
3130     // We have to make sure no-alias on the argument does not break
3131     // synchronization when this is a callback argument, see also [1] below.
3132     // If synchronization cannot be affected, we delegate to the base updateImpl
3133     // function, otherwise we give up for now.
3134 
3135     // If the function is no-sync, no-alias cannot break synchronization.
3136     const auto &NoSyncAA =
3137         A.getAAFor<AANoSync>(*this, IRPosition::function_scope(getIRPosition()),
3138                              DepClassTy::OPTIONAL);
3139     if (NoSyncAA.isAssumedNoSync())
3140       return Base::updateImpl(A);
3141 
3142     // If the argument is read-only, no-alias cannot break synchronization.
3143     bool IsKnown;
3144     if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
3145       return Base::updateImpl(A);
3146 
3147     // If the argument is never passed through callbacks, no-alias cannot break
3148     // synchronization.
3149     bool AllCallSitesKnown;
3150     if (A.checkForAllCallSites(
3151             [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
3152             true, AllCallSitesKnown))
3153       return Base::updateImpl(A);
3154 
3155     // TODO: add no-alias but make sure it doesn't break synchronization by
3156     // introducing fake uses. See:
3157     // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
3158     //     International Workshop on OpenMP 2018,
3159     //     http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
3160 
3161     return indicatePessimisticFixpoint();
3162   }
3163 
3164   /// See AbstractAttribute::trackStatistics()
3165   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
3166 };
3167 
3168 struct AANoAliasCallSiteArgument final : AANoAliasImpl {
3169   AANoAliasCallSiteArgument(const IRPosition &IRP, Attributor &A)
3170       : AANoAliasImpl(IRP, A) {}
3171 
3172   /// See AbstractAttribute::initialize(...).
3173   void initialize(Attributor &A) override {
3174     // See callsite argument attribute and callee argument attribute.
3175     const auto &CB = cast<CallBase>(getAnchorValue());
3176     if (CB.paramHasAttr(getCallSiteArgNo(), Attribute::NoAlias))
3177       indicateOptimisticFixpoint();
3178     Value &Val = getAssociatedValue();
3179     if (isa<ConstantPointerNull>(Val) &&
3180         !NullPointerIsDefined(getAnchorScope(),
3181                               Val.getType()->getPointerAddressSpace()))
3182       indicateOptimisticFixpoint();
3183   }
3184 
3185   /// Determine if the underlying value may alias with the call site argument
3186   /// \p OtherArgNo of \p ICS (= the underlying call site).
3187   bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
3188                             const AAMemoryBehavior &MemBehaviorAA,
3189                             const CallBase &CB, unsigned OtherArgNo) {
3190     // We do not need to worry about aliasing with the underlying IRP.
3191     if (this->getCalleeArgNo() == (int)OtherArgNo)
3192       return false;
3193 
3194     // If it is not a pointer or pointer vector we do not alias.
3195     const Value *ArgOp = CB.getArgOperand(OtherArgNo);
3196     if (!ArgOp->getType()->isPtrOrPtrVectorTy())
3197       return false;
3198 
3199     auto &CBArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
3200         *this, IRPosition::callsite_argument(CB, OtherArgNo), DepClassTy::NONE);
3201 
3202     // If the argument is readnone, there is no read-write aliasing.
3203     if (CBArgMemBehaviorAA.isAssumedReadNone()) {
3204       A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
3205       return false;
3206     }
3207 
3208     // If the argument is readonly and the underlying value is readonly, there
3209     // is no read-write aliasing.
3210     bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
3211     if (CBArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
3212       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3213       A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
3214       return false;
3215     }
3216 
3217     // We have to utilize actual alias analysis queries so we need the object.
3218     if (!AAR)
3219       AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
3220 
3221     // Try to rule it out at the call site.
3222     bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
3223     LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "
3224                          "callsite arguments: "
3225                       << getAssociatedValue() << " " << *ArgOp << " => "
3226                       << (IsAliasing ? "" : "no-") << "alias \n");
3227 
3228     return IsAliasing;
3229   }
3230 
3231   bool
3232   isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
3233                                          const AAMemoryBehavior &MemBehaviorAA,
3234                                          const AANoAlias &NoAliasAA) {
3235     // We can deduce "noalias" if the following conditions hold.
3236     // (i)   Associated value is assumed to be noalias in the definition.
3237     // (ii)  Associated value is assumed to be no-capture in all the uses
3238     //       possibly executed before this callsite.
3239     // (iii) There is no other pointer argument which could alias with the
3240     //       value.
3241 
3242     bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
3243     if (!AssociatedValueIsNoAliasAtDef) {
3244       LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()
3245                         << " is not no-alias at the definition\n");
3246       return false;
3247     }
3248 
3249     A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL);
3250 
3251     const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
3252     const Function *ScopeFn = VIRP.getAnchorScope();
3253     auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, VIRP, DepClassTy::NONE);
3254     // Check whether the value is captured in the scope using AANoCapture.
3255     //      Look at CFG and check only uses possibly executed before this
3256     //      callsite.
3257     auto UsePred = [&](const Use &U, bool &Follow) -> bool {
3258       Instruction *UserI = cast<Instruction>(U.getUser());
3259 
3260       // If UserI is the curr instruction and there is a single potential use of
3261       // the value in UserI we allow the use.
3262       // TODO: We should inspect the operands and allow those that cannot alias
3263       //       with the value.
3264       if (UserI == getCtxI() && UserI->getNumOperands() == 1)
3265         return true;
3266 
3267       if (ScopeFn) {
3268         const auto &ReachabilityAA = A.getAAFor<AAReachability>(
3269             *this, IRPosition::function(*ScopeFn), DepClassTy::OPTIONAL);
3270 
3271         if (!ReachabilityAA.isAssumedReachable(A, *UserI, *getCtxI()))
3272           return true;
3273 
3274         if (auto *CB = dyn_cast<CallBase>(UserI)) {
3275           if (CB->isArgOperand(&U)) {
3276 
3277             unsigned ArgNo = CB->getArgOperandNo(&U);
3278 
3279             const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
3280                 *this, IRPosition::callsite_argument(*CB, ArgNo),
3281                 DepClassTy::OPTIONAL);
3282 
3283             if (NoCaptureAA.isAssumedNoCapture())
3284               return true;
3285           }
3286         }
3287       }
3288 
3289       // For cases which can potentially have more users
3290       if (isa<GetElementPtrInst>(U) || isa<BitCastInst>(U) || isa<PHINode>(U) ||
3291           isa<SelectInst>(U)) {
3292         Follow = true;
3293         return true;
3294       }
3295 
3296       LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n");
3297       return false;
3298     };
3299 
3300     if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
3301       if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) {
3302         LLVM_DEBUG(
3303             dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()
3304                    << " cannot be noalias as it is potentially captured\n");
3305         return false;
3306       }
3307     }
3308     A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
3309 
3310     // Check there is no other pointer argument which could alias with the
3311     // value passed at this call site.
3312     // TODO: AbstractCallSite
3313     const auto &CB = cast<CallBase>(getAnchorValue());
3314     for (unsigned OtherArgNo = 0; OtherArgNo < CB.arg_size(); OtherArgNo++)
3315       if (mayAliasWithArgument(A, AAR, MemBehaviorAA, CB, OtherArgNo))
3316         return false;
3317 
3318     return true;
3319   }
3320 
3321   /// See AbstractAttribute::updateImpl(...).
3322   ChangeStatus updateImpl(Attributor &A) override {
3323     // If the argument is readnone we are done as there are no accesses via the
3324     // argument.
3325     auto &MemBehaviorAA =
3326         A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
3327     if (MemBehaviorAA.isAssumedReadNone()) {
3328       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3329       return ChangeStatus::UNCHANGED;
3330     }
3331 
3332     const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
3333     const auto &NoAliasAA =
3334         A.getAAFor<AANoAlias>(*this, VIRP, DepClassTy::NONE);
3335 
3336     AAResults *AAR = nullptr;
3337     if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
3338                                                NoAliasAA)) {
3339       LLVM_DEBUG(
3340           dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n");
3341       return ChangeStatus::UNCHANGED;
3342     }
3343 
3344     return indicatePessimisticFixpoint();
3345   }
3346 
3347   /// See AbstractAttribute::trackStatistics()
3348   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
3349 };
3350 
3351 /// NoAlias attribute for function return value.
3352 struct AANoAliasReturned final : AANoAliasImpl {
3353   AANoAliasReturned(const IRPosition &IRP, Attributor &A)
3354       : AANoAliasImpl(IRP, A) {}
3355 
3356   /// See AbstractAttribute::initialize(...).
3357   void initialize(Attributor &A) override {
3358     AANoAliasImpl::initialize(A);
3359     Function *F = getAssociatedFunction();
3360     if (!F || F->isDeclaration())
3361       indicatePessimisticFixpoint();
3362   }
3363 
3364   /// See AbstractAttribute::updateImpl(...).
3365   virtual ChangeStatus updateImpl(Attributor &A) override {
3366 
3367     auto CheckReturnValue = [&](Value &RV) -> bool {
3368       if (Constant *C = dyn_cast<Constant>(&RV))
3369         if (C->isNullValue() || isa<UndefValue>(C))
3370           return true;
3371 
3372       /// For now, we can only deduce noalias if we have call sites.
3373       /// FIXME: add more support.
3374       if (!isa<CallBase>(&RV))
3375         return false;
3376 
3377       const IRPosition &RVPos = IRPosition::value(RV);
3378       const auto &NoAliasAA =
3379           A.getAAFor<AANoAlias>(*this, RVPos, DepClassTy::REQUIRED);
3380       if (!NoAliasAA.isAssumedNoAlias())
3381         return false;
3382 
3383       const auto &NoCaptureAA =
3384           A.getAAFor<AANoCapture>(*this, RVPos, DepClassTy::REQUIRED);
3385       return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
3386     };
3387 
3388     if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
3389       return indicatePessimisticFixpoint();
3390 
3391     return ChangeStatus::UNCHANGED;
3392   }
3393 
3394   /// See AbstractAttribute::trackStatistics()
3395   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
3396 };
3397 
3398 /// NoAlias attribute deduction for a call site return value.
3399 struct AANoAliasCallSiteReturned final : AANoAliasImpl {
3400   AANoAliasCallSiteReturned(const IRPosition &IRP, Attributor &A)
3401       : AANoAliasImpl(IRP, A) {}
3402 
3403   /// See AbstractAttribute::initialize(...).
3404   void initialize(Attributor &A) override {
3405     AANoAliasImpl::initialize(A);
3406     Function *F = getAssociatedFunction();
3407     if (!F || F->isDeclaration())
3408       indicatePessimisticFixpoint();
3409   }
3410 
3411   /// See AbstractAttribute::updateImpl(...).
3412   ChangeStatus updateImpl(Attributor &A) override {
3413     // TODO: Once we have call site specific value information we can provide
3414     //       call site specific liveness information and then it makes
3415     //       sense to specialize attributes for call sites arguments instead of
3416     //       redirecting requests to the callee argument.
3417     Function *F = getAssociatedFunction();
3418     const IRPosition &FnPos = IRPosition::returned(*F);
3419     auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos, DepClassTy::REQUIRED);
3420     return clampStateAndIndicateChange(getState(), FnAA.getState());
3421   }
3422 
3423   /// See AbstractAttribute::trackStatistics()
3424   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
3425 };
3426 
3427 /// -------------------AAIsDead Function Attribute-----------------------
3428 
3429 struct AAIsDeadValueImpl : public AAIsDead {
3430   AAIsDeadValueImpl(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
3431 
3432   /// See AAIsDead::isAssumedDead().
3433   bool isAssumedDead() const override { return isAssumed(IS_DEAD); }
3434 
3435   /// See AAIsDead::isKnownDead().
3436   bool isKnownDead() const override { return isKnown(IS_DEAD); }
3437 
3438   /// See AAIsDead::isAssumedDead(BasicBlock *).
3439   bool isAssumedDead(const BasicBlock *BB) const override { return false; }
3440 
3441   /// See AAIsDead::isKnownDead(BasicBlock *).
3442   bool isKnownDead(const BasicBlock *BB) const override { return false; }
3443 
3444   /// See AAIsDead::isAssumedDead(Instruction *I).
3445   bool isAssumedDead(const Instruction *I) const override {
3446     return I == getCtxI() && isAssumedDead();
3447   }
3448 
3449   /// See AAIsDead::isKnownDead(Instruction *I).
3450   bool isKnownDead(const Instruction *I) const override {
3451     return isAssumedDead(I) && isKnownDead();
3452   }
3453 
3454   /// See AbstractAttribute::getAsStr().
3455   const std::string getAsStr() const override {
3456     return isAssumedDead() ? "assumed-dead" : "assumed-live";
3457   }
3458 
3459   /// Check if all uses are assumed dead.
3460   bool areAllUsesAssumedDead(Attributor &A, Value &V) {
3461     // Callers might not check the type, void has no uses.
3462     if (V.getType()->isVoidTy())
3463       return true;
3464 
3465     // If we replace a value with a constant there are no uses left afterwards.
3466     if (!isa<Constant>(V)) {
3467       bool UsedAssumedInformation = false;
3468       Optional<Constant *> C =
3469           A.getAssumedConstant(V, *this, UsedAssumedInformation);
3470       if (!C.hasValue() || *C)
3471         return true;
3472     }
3473 
3474     auto UsePred = [&](const Use &U, bool &Follow) { return false; };
3475     // Explicitly set the dependence class to required because we want a long
3476     // chain of N dependent instructions to be considered live as soon as one is
3477     // without going through N update cycles. This is not required for
3478     // correctness.
3479     return A.checkForAllUses(UsePred, *this, V, /* CheckBBLivenessOnly */ false,
3480                              DepClassTy::REQUIRED);
3481   }
3482 
3483   /// Determine if \p I is assumed to be side-effect free.
3484   bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
3485     if (!I || wouldInstructionBeTriviallyDead(I))
3486       return true;
3487 
3488     auto *CB = dyn_cast<CallBase>(I);
3489     if (!CB || isa<IntrinsicInst>(CB))
3490       return false;
3491 
3492     const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
3493     const auto &NoUnwindAA =
3494         A.getAndUpdateAAFor<AANoUnwind>(*this, CallIRP, DepClassTy::NONE);
3495     if (!NoUnwindAA.isAssumedNoUnwind())
3496       return false;
3497     if (!NoUnwindAA.isKnownNoUnwind())
3498       A.recordDependence(NoUnwindAA, *this, DepClassTy::OPTIONAL);
3499 
3500     bool IsKnown;
3501     return AA::isAssumedReadOnly(A, CallIRP, *this, IsKnown);
3502   }
3503 };
3504 
3505 struct AAIsDeadFloating : public AAIsDeadValueImpl {
3506   AAIsDeadFloating(const IRPosition &IRP, Attributor &A)
3507       : AAIsDeadValueImpl(IRP, A) {}
3508 
3509   /// See AbstractAttribute::initialize(...).
3510   void initialize(Attributor &A) override {
3511     if (isa<UndefValue>(getAssociatedValue())) {
3512       indicatePessimisticFixpoint();
3513       return;
3514     }
3515 
3516     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3517     if (!isAssumedSideEffectFree(A, I)) {
3518       if (!isa_and_nonnull<StoreInst>(I))
3519         indicatePessimisticFixpoint();
3520       else
3521         removeAssumedBits(HAS_NO_EFFECT);
3522     }
3523   }
3524 
3525   bool isDeadStore(Attributor &A, StoreInst &SI) {
3526     // Lang ref now states volatile store is not UB/dead, let's skip them.
3527     if (SI.isVolatile())
3528       return false;
3529 
3530     bool UsedAssumedInformation = false;
3531     SmallSetVector<Value *, 4> PotentialCopies;
3532     if (!AA::getPotentialCopiesOfStoredValue(A, SI, PotentialCopies, *this,
3533                                              UsedAssumedInformation))
3534       return false;
3535     return llvm::all_of(PotentialCopies, [&](Value *V) {
3536       return A.isAssumedDead(IRPosition::value(*V), this, nullptr,
3537                              UsedAssumedInformation);
3538     });
3539   }
3540 
3541   /// See AbstractAttribute::updateImpl(...).
3542   ChangeStatus updateImpl(Attributor &A) override {
3543     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3544     if (auto *SI = dyn_cast_or_null<StoreInst>(I)) {
3545       if (!isDeadStore(A, *SI))
3546         return indicatePessimisticFixpoint();
3547     } else {
3548       if (!isAssumedSideEffectFree(A, I))
3549         return indicatePessimisticFixpoint();
3550       if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3551         return indicatePessimisticFixpoint();
3552     }
3553     return ChangeStatus::UNCHANGED;
3554   }
3555 
3556   /// See AbstractAttribute::manifest(...).
3557   ChangeStatus manifest(Attributor &A) override {
3558     Value &V = getAssociatedValue();
3559     if (auto *I = dyn_cast<Instruction>(&V)) {
3560       // If we get here we basically know the users are all dead. We check if
3561       // isAssumedSideEffectFree returns true here again because it might not be
3562       // the case and only the users are dead but the instruction (=call) is
3563       // still needed.
3564       if (isa<StoreInst>(I) ||
3565           (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I))) {
3566         A.deleteAfterManifest(*I);
3567         return ChangeStatus::CHANGED;
3568       }
3569     }
3570     if (V.use_empty())
3571       return ChangeStatus::UNCHANGED;
3572 
3573     bool UsedAssumedInformation = false;
3574     Optional<Constant *> C =
3575         A.getAssumedConstant(V, *this, UsedAssumedInformation);
3576     if (C.hasValue() && C.getValue())
3577       return ChangeStatus::UNCHANGED;
3578 
3579     // Replace the value with undef as it is dead but keep droppable uses around
3580     // as they provide information we don't want to give up on just yet.
3581     UndefValue &UV = *UndefValue::get(V.getType());
3582     bool AnyChange =
3583         A.changeValueAfterManifest(V, UV, /* ChangeDropppable */ false);
3584     return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3585   }
3586 
3587   /// See AbstractAttribute::trackStatistics()
3588   void trackStatistics() const override {
3589     STATS_DECLTRACK_FLOATING_ATTR(IsDead)
3590   }
3591 };
3592 
3593 struct AAIsDeadArgument : public AAIsDeadFloating {
3594   AAIsDeadArgument(const IRPosition &IRP, Attributor &A)
3595       : AAIsDeadFloating(IRP, A) {}
3596 
3597   /// See AbstractAttribute::initialize(...).
3598   void initialize(Attributor &A) override {
3599     if (!A.isFunctionIPOAmendable(*getAnchorScope()))
3600       indicatePessimisticFixpoint();
3601   }
3602 
3603   /// See AbstractAttribute::manifest(...).
3604   ChangeStatus manifest(Attributor &A) override {
3605     ChangeStatus Changed = AAIsDeadFloating::manifest(A);
3606     Argument &Arg = *getAssociatedArgument();
3607     if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
3608       if (A.registerFunctionSignatureRewrite(
3609               Arg, /* ReplacementTypes */ {},
3610               Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
3611               Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) {
3612         Arg.dropDroppableUses();
3613         return ChangeStatus::CHANGED;
3614       }
3615     return Changed;
3616   }
3617 
3618   /// See AbstractAttribute::trackStatistics()
3619   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) }
3620 };
3621 
3622 struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
3623   AAIsDeadCallSiteArgument(const IRPosition &IRP, Attributor &A)
3624       : AAIsDeadValueImpl(IRP, A) {}
3625 
3626   /// See AbstractAttribute::initialize(...).
3627   void initialize(Attributor &A) override {
3628     if (isa<UndefValue>(getAssociatedValue()))
3629       indicatePessimisticFixpoint();
3630   }
3631 
3632   /// See AbstractAttribute::updateImpl(...).
3633   ChangeStatus updateImpl(Attributor &A) override {
3634     // TODO: Once we have call site specific value information we can provide
3635     //       call site specific liveness information and then it makes
3636     //       sense to specialize attributes for call sites arguments instead of
3637     //       redirecting requests to the callee argument.
3638     Argument *Arg = getAssociatedArgument();
3639     if (!Arg)
3640       return indicatePessimisticFixpoint();
3641     const IRPosition &ArgPos = IRPosition::argument(*Arg);
3642     auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos, DepClassTy::REQUIRED);
3643     return clampStateAndIndicateChange(getState(), ArgAA.getState());
3644   }
3645 
3646   /// See AbstractAttribute::manifest(...).
3647   ChangeStatus manifest(Attributor &A) override {
3648     CallBase &CB = cast<CallBase>(getAnchorValue());
3649     Use &U = CB.getArgOperandUse(getCallSiteArgNo());
3650     assert(!isa<UndefValue>(U.get()) &&
3651            "Expected undef values to be filtered out!");
3652     UndefValue &UV = *UndefValue::get(U->getType());
3653     if (A.changeUseAfterManifest(U, UV))
3654       return ChangeStatus::CHANGED;
3655     return ChangeStatus::UNCHANGED;
3656   }
3657 
3658   /// See AbstractAttribute::trackStatistics()
3659   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) }
3660 };
3661 
3662 struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
3663   AAIsDeadCallSiteReturned(const IRPosition &IRP, Attributor &A)
3664       : AAIsDeadFloating(IRP, A), IsAssumedSideEffectFree(true) {}
3665 
3666   /// See AAIsDead::isAssumedDead().
3667   bool isAssumedDead() const override {
3668     return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
3669   }
3670 
3671   /// See AbstractAttribute::initialize(...).
3672   void initialize(Attributor &A) override {
3673     if (isa<UndefValue>(getAssociatedValue())) {
3674       indicatePessimisticFixpoint();
3675       return;
3676     }
3677 
3678     // We track this separately as a secondary state.
3679     IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
3680   }
3681 
3682   /// See AbstractAttribute::updateImpl(...).
3683   ChangeStatus updateImpl(Attributor &A) override {
3684     ChangeStatus Changed = ChangeStatus::UNCHANGED;
3685     if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
3686       IsAssumedSideEffectFree = false;
3687       Changed = ChangeStatus::CHANGED;
3688     }
3689     if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3690       return indicatePessimisticFixpoint();
3691     return Changed;
3692   }
3693 
3694   /// See AbstractAttribute::trackStatistics()
3695   void trackStatistics() const override {
3696     if (IsAssumedSideEffectFree)
3697       STATS_DECLTRACK_CSRET_ATTR(IsDead)
3698     else
3699       STATS_DECLTRACK_CSRET_ATTR(UnusedResult)
3700   }
3701 
3702   /// See AbstractAttribute::getAsStr().
3703   const std::string getAsStr() const override {
3704     return isAssumedDead()
3705                ? "assumed-dead"
3706                : (getAssumed() ? "assumed-dead-users" : "assumed-live");
3707   }
3708 
3709 private:
3710   bool IsAssumedSideEffectFree;
3711 };
3712 
3713 struct AAIsDeadReturned : public AAIsDeadValueImpl {
3714   AAIsDeadReturned(const IRPosition &IRP, Attributor &A)
3715       : AAIsDeadValueImpl(IRP, A) {}
3716 
3717   /// See AbstractAttribute::updateImpl(...).
3718   ChangeStatus updateImpl(Attributor &A) override {
3719 
3720     bool UsedAssumedInformation = false;
3721     A.checkForAllInstructions([](Instruction &) { return true; }, *this,
3722                               {Instruction::Ret}, UsedAssumedInformation);
3723 
3724     auto PredForCallSite = [&](AbstractCallSite ACS) {
3725       if (ACS.isCallbackCall() || !ACS.getInstruction())
3726         return false;
3727       return areAllUsesAssumedDead(A, *ACS.getInstruction());
3728     };
3729 
3730     bool AllCallSitesKnown;
3731     if (!A.checkForAllCallSites(PredForCallSite, *this, true,
3732                                 AllCallSitesKnown))
3733       return indicatePessimisticFixpoint();
3734 
3735     return ChangeStatus::UNCHANGED;
3736   }
3737 
3738   /// See AbstractAttribute::manifest(...).
3739   ChangeStatus manifest(Attributor &A) override {
3740     // TODO: Rewrite the signature to return void?
3741     bool AnyChange = false;
3742     UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
3743     auto RetInstPred = [&](Instruction &I) {
3744       ReturnInst &RI = cast<ReturnInst>(I);
3745       if (!isa<UndefValue>(RI.getReturnValue()))
3746         AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
3747       return true;
3748     };
3749     bool UsedAssumedInformation = false;
3750     A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret},
3751                               UsedAssumedInformation);
3752     return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3753   }
3754 
3755   /// See AbstractAttribute::trackStatistics()
3756   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) }
3757 };
3758 
3759 struct AAIsDeadFunction : public AAIsDead {
3760   AAIsDeadFunction(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
3761 
3762   /// See AbstractAttribute::initialize(...).
3763   void initialize(Attributor &A) override {
3764     const Function *F = getAnchorScope();
3765     if (F && !F->isDeclaration()) {
3766       // We only want to compute liveness once. If the function is not part of
3767       // the SCC, skip it.
3768       if (A.isRunOn(*const_cast<Function *>(F))) {
3769         ToBeExploredFrom.insert(&F->getEntryBlock().front());
3770         assumeLive(A, F->getEntryBlock());
3771       } else {
3772         indicatePessimisticFixpoint();
3773       }
3774     }
3775   }
3776 
3777   /// See AbstractAttribute::getAsStr().
3778   const std::string getAsStr() const override {
3779     return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
3780            std::to_string(getAnchorScope()->size()) + "][#TBEP " +
3781            std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
3782            std::to_string(KnownDeadEnds.size()) + "]";
3783   }
3784 
3785   /// See AbstractAttribute::manifest(...).
3786   ChangeStatus manifest(Attributor &A) override {
3787     assert(getState().isValidState() &&
3788            "Attempted to manifest an invalid state!");
3789 
3790     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
3791     Function &F = *getAnchorScope();
3792 
3793     if (AssumedLiveBlocks.empty()) {
3794       A.deleteAfterManifest(F);
3795       return ChangeStatus::CHANGED;
3796     }
3797 
3798     // Flag to determine if we can change an invoke to a call assuming the
3799     // callee is nounwind. This is not possible if the personality of the
3800     // function allows to catch asynchronous exceptions.
3801     bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
3802 
3803     KnownDeadEnds.set_union(ToBeExploredFrom);
3804     for (const Instruction *DeadEndI : KnownDeadEnds) {
3805       auto *CB = dyn_cast<CallBase>(DeadEndI);
3806       if (!CB)
3807         continue;
3808       const auto &NoReturnAA = A.getAndUpdateAAFor<AANoReturn>(
3809           *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
3810       bool MayReturn = !NoReturnAA.isAssumedNoReturn();
3811       if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
3812         continue;
3813 
3814       if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
3815         A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
3816       else
3817         A.changeToUnreachableAfterManifest(
3818             const_cast<Instruction *>(DeadEndI->getNextNode()));
3819       HasChanged = ChangeStatus::CHANGED;
3820     }
3821 
3822     STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.");
3823     for (BasicBlock &BB : F)
3824       if (!AssumedLiveBlocks.count(&BB)) {
3825         A.deleteAfterManifest(BB);
3826         ++BUILD_STAT_NAME(AAIsDead, BasicBlock);
3827         HasChanged = ChangeStatus::CHANGED;
3828       }
3829 
3830     return HasChanged;
3831   }
3832 
3833   /// See AbstractAttribute::updateImpl(...).
3834   ChangeStatus updateImpl(Attributor &A) override;
3835 
3836   bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const override {
3837     return isValidState() && !AssumedLiveEdges.count(std::make_pair(From, To));
3838   }
3839 
3840   /// See AbstractAttribute::trackStatistics()
3841   void trackStatistics() const override {}
3842 
3843   /// Returns true if the function is assumed dead.
3844   bool isAssumedDead() const override { return false; }
3845 
3846   /// See AAIsDead::isKnownDead().
3847   bool isKnownDead() const override { return false; }
3848 
3849   /// See AAIsDead::isAssumedDead(BasicBlock *).
3850   bool isAssumedDead(const BasicBlock *BB) const override {
3851     assert(BB->getParent() == getAnchorScope() &&
3852            "BB must be in the same anchor scope function.");
3853 
3854     if (!getAssumed())
3855       return false;
3856     return !AssumedLiveBlocks.count(BB);
3857   }
3858 
3859   /// See AAIsDead::isKnownDead(BasicBlock *).
3860   bool isKnownDead(const BasicBlock *BB) const override {
3861     return getKnown() && isAssumedDead(BB);
3862   }
3863 
3864   /// See AAIsDead::isAssumed(Instruction *I).
3865   bool isAssumedDead(const Instruction *I) const override {
3866     assert(I->getParent()->getParent() == getAnchorScope() &&
3867            "Instruction must be in the same anchor scope function.");
3868 
3869     if (!getAssumed())
3870       return false;
3871 
3872     // If it is not in AssumedLiveBlocks then it for sure dead.
3873     // Otherwise, it can still be after noreturn call in a live block.
3874     if (!AssumedLiveBlocks.count(I->getParent()))
3875       return true;
3876 
3877     // If it is not after a liveness barrier it is live.
3878     const Instruction *PrevI = I->getPrevNode();
3879     while (PrevI) {
3880       if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
3881         return true;
3882       PrevI = PrevI->getPrevNode();
3883     }
3884     return false;
3885   }
3886 
3887   /// See AAIsDead::isKnownDead(Instruction *I).
3888   bool isKnownDead(const Instruction *I) const override {
3889     return getKnown() && isAssumedDead(I);
3890   }
3891 
3892   /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
3893   /// that internal function called from \p BB should now be looked at.
3894   bool assumeLive(Attributor &A, const BasicBlock &BB) {
3895     if (!AssumedLiveBlocks.insert(&BB).second)
3896       return false;
3897 
3898     // We assume that all of BB is (probably) live now and if there are calls to
3899     // internal functions we will assume that those are now live as well. This
3900     // is a performance optimization for blocks with calls to a lot of internal
3901     // functions. It can however cause dead functions to be treated as live.
3902     for (const Instruction &I : BB)
3903       if (const auto *CB = dyn_cast<CallBase>(&I))
3904         if (const Function *F = CB->getCalledFunction())
3905           if (F->hasLocalLinkage())
3906             A.markLiveInternalFunction(*F);
3907     return true;
3908   }
3909 
3910   /// Collection of instructions that need to be explored again, e.g., we
3911   /// did assume they do not transfer control to (one of their) successors.
3912   SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
3913 
3914   /// Collection of instructions that are known to not transfer control.
3915   SmallSetVector<const Instruction *, 8> KnownDeadEnds;
3916 
3917   /// Collection of all assumed live edges
3918   DenseSet<std::pair<const BasicBlock *, const BasicBlock *>> AssumedLiveEdges;
3919 
3920   /// Collection of all assumed live BasicBlocks.
3921   DenseSet<const BasicBlock *> AssumedLiveBlocks;
3922 };
3923 
3924 static bool
3925 identifyAliveSuccessors(Attributor &A, const CallBase &CB,
3926                         AbstractAttribute &AA,
3927                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3928   const IRPosition &IPos = IRPosition::callsite_function(CB);
3929 
3930   const auto &NoReturnAA =
3931       A.getAndUpdateAAFor<AANoReturn>(AA, IPos, DepClassTy::OPTIONAL);
3932   if (NoReturnAA.isAssumedNoReturn())
3933     return !NoReturnAA.isKnownNoReturn();
3934   if (CB.isTerminator())
3935     AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
3936   else
3937     AliveSuccessors.push_back(CB.getNextNode());
3938   return false;
3939 }
3940 
3941 static bool
3942 identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
3943                         AbstractAttribute &AA,
3944                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3945   bool UsedAssumedInformation =
3946       identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
3947 
3948   // First, determine if we can change an invoke to a call assuming the
3949   // callee is nounwind. This is not possible if the personality of the
3950   // function allows to catch asynchronous exceptions.
3951   if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
3952     AliveSuccessors.push_back(&II.getUnwindDest()->front());
3953   } else {
3954     const IRPosition &IPos = IRPosition::callsite_function(II);
3955     const auto &AANoUnw =
3956         A.getAndUpdateAAFor<AANoUnwind>(AA, IPos, DepClassTy::OPTIONAL);
3957     if (AANoUnw.isAssumedNoUnwind()) {
3958       UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
3959     } else {
3960       AliveSuccessors.push_back(&II.getUnwindDest()->front());
3961     }
3962   }
3963   return UsedAssumedInformation;
3964 }
3965 
3966 static bool
3967 identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
3968                         AbstractAttribute &AA,
3969                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3970   bool UsedAssumedInformation = false;
3971   if (BI.getNumSuccessors() == 1) {
3972     AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3973   } else {
3974     Optional<Constant *> C =
3975         A.getAssumedConstant(*BI.getCondition(), AA, UsedAssumedInformation);
3976     if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
3977       // No value yet, assume both edges are dead.
3978     } else if (isa_and_nonnull<ConstantInt>(*C)) {
3979       const BasicBlock *SuccBB =
3980           BI.getSuccessor(1 - cast<ConstantInt>(*C)->getValue().getZExtValue());
3981       AliveSuccessors.push_back(&SuccBB->front());
3982     } else {
3983       AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3984       AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
3985       UsedAssumedInformation = false;
3986     }
3987   }
3988   return UsedAssumedInformation;
3989 }
3990 
3991 static bool
3992 identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
3993                         AbstractAttribute &AA,
3994                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3995   bool UsedAssumedInformation = false;
3996   Optional<Constant *> C =
3997       A.getAssumedConstant(*SI.getCondition(), AA, UsedAssumedInformation);
3998   if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
3999     // No value yet, assume all edges are dead.
4000   } else if (isa_and_nonnull<ConstantInt>(C.getValue())) {
4001     for (auto &CaseIt : SI.cases()) {
4002       if (CaseIt.getCaseValue() == C.getValue()) {
4003         AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
4004         return UsedAssumedInformation;
4005       }
4006     }
4007     AliveSuccessors.push_back(&SI.getDefaultDest()->front());
4008     return UsedAssumedInformation;
4009   } else {
4010     for (const BasicBlock *SuccBB : successors(SI.getParent()))
4011       AliveSuccessors.push_back(&SuccBB->front());
4012   }
4013   return UsedAssumedInformation;
4014 }
4015 
4016 ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
4017   ChangeStatus Change = ChangeStatus::UNCHANGED;
4018 
4019   LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"
4020                     << getAnchorScope()->size() << "] BBs and "
4021                     << ToBeExploredFrom.size() << " exploration points and "
4022                     << KnownDeadEnds.size() << " known dead ends\n");
4023 
4024   // Copy and clear the list of instructions we need to explore from. It is
4025   // refilled with instructions the next update has to look at.
4026   SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
4027                                                ToBeExploredFrom.end());
4028   decltype(ToBeExploredFrom) NewToBeExploredFrom;
4029 
4030   SmallVector<const Instruction *, 8> AliveSuccessors;
4031   while (!Worklist.empty()) {
4032     const Instruction *I = Worklist.pop_back_val();
4033     LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n");
4034 
4035     // Fast forward for uninteresting instructions. We could look for UB here
4036     // though.
4037     while (!I->isTerminator() && !isa<CallBase>(I))
4038       I = I->getNextNode();
4039 
4040     AliveSuccessors.clear();
4041 
4042     bool UsedAssumedInformation = false;
4043     switch (I->getOpcode()) {
4044     // TODO: look for (assumed) UB to backwards propagate "deadness".
4045     default:
4046       assert(I->isTerminator() &&
4047              "Expected non-terminators to be handled already!");
4048       for (const BasicBlock *SuccBB : successors(I->getParent()))
4049         AliveSuccessors.push_back(&SuccBB->front());
4050       break;
4051     case Instruction::Call:
4052       UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
4053                                                        *this, AliveSuccessors);
4054       break;
4055     case Instruction::Invoke:
4056       UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
4057                                                        *this, AliveSuccessors);
4058       break;
4059     case Instruction::Br:
4060       UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
4061                                                        *this, AliveSuccessors);
4062       break;
4063     case Instruction::Switch:
4064       UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
4065                                                        *this, AliveSuccessors);
4066       break;
4067     }
4068 
4069     if (UsedAssumedInformation) {
4070       NewToBeExploredFrom.insert(I);
4071     } else if (AliveSuccessors.empty() ||
4072                (I->isTerminator() &&
4073                 AliveSuccessors.size() < I->getNumSuccessors())) {
4074       if (KnownDeadEnds.insert(I))
4075         Change = ChangeStatus::CHANGED;
4076     }
4077 
4078     LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "
4079                       << AliveSuccessors.size() << " UsedAssumedInformation: "
4080                       << UsedAssumedInformation << "\n");
4081 
4082     for (const Instruction *AliveSuccessor : AliveSuccessors) {
4083       if (!I->isTerminator()) {
4084         assert(AliveSuccessors.size() == 1 &&
4085                "Non-terminator expected to have a single successor!");
4086         Worklist.push_back(AliveSuccessor);
4087       } else {
4088         // record the assumed live edge
4089         auto Edge = std::make_pair(I->getParent(), AliveSuccessor->getParent());
4090         if (AssumedLiveEdges.insert(Edge).second)
4091           Change = ChangeStatus::CHANGED;
4092         if (assumeLive(A, *AliveSuccessor->getParent()))
4093           Worklist.push_back(AliveSuccessor);
4094       }
4095     }
4096   }
4097 
4098   // Check if the content of ToBeExploredFrom changed, ignore the order.
4099   if (NewToBeExploredFrom.size() != ToBeExploredFrom.size() ||
4100       llvm::any_of(NewToBeExploredFrom, [&](const Instruction *I) {
4101         return !ToBeExploredFrom.count(I);
4102       })) {
4103     Change = ChangeStatus::CHANGED;
4104     ToBeExploredFrom = std::move(NewToBeExploredFrom);
4105   }
4106 
4107   // If we know everything is live there is no need to query for liveness.
4108   // Instead, indicating a pessimistic fixpoint will cause the state to be
4109   // "invalid" and all queries to be answered conservatively without lookups.
4110   // To be in this state we have to (1) finished the exploration and (3) not
4111   // discovered any non-trivial dead end and (2) not ruled unreachable code
4112   // dead.
4113   if (ToBeExploredFrom.empty() &&
4114       getAnchorScope()->size() == AssumedLiveBlocks.size() &&
4115       llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
4116         return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
4117       }))
4118     return indicatePessimisticFixpoint();
4119   return Change;
4120 }
4121 
4122 /// Liveness information for a call sites.
4123 struct AAIsDeadCallSite final : AAIsDeadFunction {
4124   AAIsDeadCallSite(const IRPosition &IRP, Attributor &A)
4125       : AAIsDeadFunction(IRP, A) {}
4126 
4127   /// See AbstractAttribute::initialize(...).
4128   void initialize(Attributor &A) override {
4129     // TODO: Once we have call site specific value information we can provide
4130     //       call site specific liveness information and then it makes
4131     //       sense to specialize attributes for call sites instead of
4132     //       redirecting requests to the callee.
4133     llvm_unreachable("Abstract attributes for liveness are not "
4134                      "supported for call sites yet!");
4135   }
4136 
4137   /// See AbstractAttribute::updateImpl(...).
4138   ChangeStatus updateImpl(Attributor &A) override {
4139     return indicatePessimisticFixpoint();
4140   }
4141 
4142   /// See AbstractAttribute::trackStatistics()
4143   void trackStatistics() const override {}
4144 };
4145 
4146 /// -------------------- Dereferenceable Argument Attribute --------------------
4147 
4148 struct AADereferenceableImpl : AADereferenceable {
4149   AADereferenceableImpl(const IRPosition &IRP, Attributor &A)
4150       : AADereferenceable(IRP, A) {}
4151   using StateType = DerefState;
4152 
4153   /// See AbstractAttribute::initialize(...).
4154   void initialize(Attributor &A) override {
4155     SmallVector<Attribute, 4> Attrs;
4156     getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
4157              Attrs, /* IgnoreSubsumingPositions */ false, &A);
4158     for (const Attribute &Attr : Attrs)
4159       takeKnownDerefBytesMaximum(Attr.getValueAsInt());
4160 
4161     const IRPosition &IRP = this->getIRPosition();
4162     NonNullAA = &A.getAAFor<AANonNull>(*this, IRP, DepClassTy::NONE);
4163 
4164     bool CanBeNull, CanBeFreed;
4165     takeKnownDerefBytesMaximum(
4166         IRP.getAssociatedValue().getPointerDereferenceableBytes(
4167             A.getDataLayout(), CanBeNull, CanBeFreed));
4168 
4169     bool IsFnInterface = IRP.isFnInterfaceKind();
4170     Function *FnScope = IRP.getAnchorScope();
4171     if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope))) {
4172       indicatePessimisticFixpoint();
4173       return;
4174     }
4175 
4176     if (Instruction *CtxI = getCtxI())
4177       followUsesInMBEC(*this, A, getState(), *CtxI);
4178   }
4179 
4180   /// See AbstractAttribute::getState()
4181   /// {
4182   StateType &getState() override { return *this; }
4183   const StateType &getState() const override { return *this; }
4184   /// }
4185 
4186   /// Helper function for collecting accessed bytes in must-be-executed-context
4187   void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I,
4188                               DerefState &State) {
4189     const Value *UseV = U->get();
4190     if (!UseV->getType()->isPointerTy())
4191       return;
4192 
4193     Optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I);
4194     if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile())
4195       return;
4196 
4197     int64_t Offset;
4198     const Value *Base = GetPointerBaseWithConstantOffset(
4199         Loc->Ptr, Offset, A.getDataLayout(), /*AllowNonInbounds*/ true);
4200     if (Base && Base == &getAssociatedValue())
4201       State.addAccessedBytes(Offset, Loc->Size.getValue());
4202   }
4203 
4204   /// See followUsesInMBEC
4205   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
4206                        AADereferenceable::StateType &State) {
4207     bool IsNonNull = false;
4208     bool TrackUse = false;
4209     int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
4210         A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
4211     LLVM_DEBUG(dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytes
4212                       << " for instruction " << *I << "\n");
4213 
4214     addAccessedBytesForUse(A, U, I, State);
4215     State.takeKnownDerefBytesMaximum(DerefBytes);
4216     return TrackUse;
4217   }
4218 
4219   /// See AbstractAttribute::manifest(...).
4220   ChangeStatus manifest(Attributor &A) override {
4221     ChangeStatus Change = AADereferenceable::manifest(A);
4222     if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
4223       removeAttrs({Attribute::DereferenceableOrNull});
4224       return ChangeStatus::CHANGED;
4225     }
4226     return Change;
4227   }
4228 
4229   void getDeducedAttributes(LLVMContext &Ctx,
4230                             SmallVectorImpl<Attribute> &Attrs) const override {
4231     // TODO: Add *_globally support
4232     if (isAssumedNonNull())
4233       Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
4234           Ctx, getAssumedDereferenceableBytes()));
4235     else
4236       Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
4237           Ctx, getAssumedDereferenceableBytes()));
4238   }
4239 
4240   /// See AbstractAttribute::getAsStr().
4241   const std::string getAsStr() const override {
4242     if (!getAssumedDereferenceableBytes())
4243       return "unknown-dereferenceable";
4244     return std::string("dereferenceable") +
4245            (isAssumedNonNull() ? "" : "_or_null") +
4246            (isAssumedGlobal() ? "_globally" : "") + "<" +
4247            std::to_string(getKnownDereferenceableBytes()) + "-" +
4248            std::to_string(getAssumedDereferenceableBytes()) + ">";
4249   }
4250 };
4251 
4252 /// Dereferenceable attribute for a floating value.
4253 struct AADereferenceableFloating : AADereferenceableImpl {
4254   AADereferenceableFloating(const IRPosition &IRP, Attributor &A)
4255       : AADereferenceableImpl(IRP, A) {}
4256 
4257   /// See AbstractAttribute::updateImpl(...).
4258   ChangeStatus updateImpl(Attributor &A) override {
4259     const DataLayout &DL = A.getDataLayout();
4260 
4261     auto VisitValueCB = [&](const Value &V, const Instruction *, DerefState &T,
4262                             bool Stripped) -> bool {
4263       unsigned IdxWidth =
4264           DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
4265       APInt Offset(IdxWidth, 0);
4266       const Value *Base =
4267           stripAndAccumulateMinimalOffsets(A, *this, &V, DL, Offset, false);
4268 
4269       const auto &AA = A.getAAFor<AADereferenceable>(
4270           *this, IRPosition::value(*Base), DepClassTy::REQUIRED);
4271       int64_t DerefBytes = 0;
4272       if (!Stripped && this == &AA) {
4273         // Use IR information if we did not strip anything.
4274         // TODO: track globally.
4275         bool CanBeNull, CanBeFreed;
4276         DerefBytes =
4277             Base->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
4278         T.GlobalState.indicatePessimisticFixpoint();
4279       } else {
4280         const DerefState &DS = AA.getState();
4281         DerefBytes = DS.DerefBytesState.getAssumed();
4282         T.GlobalState &= DS.GlobalState;
4283       }
4284 
4285       // For now we do not try to "increase" dereferenceability due to negative
4286       // indices as we first have to come up with code to deal with loops and
4287       // for overflows of the dereferenceable bytes.
4288       int64_t OffsetSExt = Offset.getSExtValue();
4289       if (OffsetSExt < 0)
4290         OffsetSExt = 0;
4291 
4292       T.takeAssumedDerefBytesMinimum(
4293           std::max(int64_t(0), DerefBytes - OffsetSExt));
4294 
4295       if (this == &AA) {
4296         if (!Stripped) {
4297           // If nothing was stripped IR information is all we got.
4298           T.takeKnownDerefBytesMaximum(
4299               std::max(int64_t(0), DerefBytes - OffsetSExt));
4300           T.indicatePessimisticFixpoint();
4301         } else if (OffsetSExt > 0) {
4302           // If something was stripped but there is circular reasoning we look
4303           // for the offset. If it is positive we basically decrease the
4304           // dereferenceable bytes in a circluar loop now, which will simply
4305           // drive them down to the known value in a very slow way which we
4306           // can accelerate.
4307           T.indicatePessimisticFixpoint();
4308         }
4309       }
4310 
4311       return T.isValidState();
4312     };
4313 
4314     DerefState T;
4315     if (!genericValueTraversal<DerefState>(A, getIRPosition(), *this, T,
4316                                            VisitValueCB, getCtxI()))
4317       return indicatePessimisticFixpoint();
4318 
4319     return clampStateAndIndicateChange(getState(), T);
4320   }
4321 
4322   /// See AbstractAttribute::trackStatistics()
4323   void trackStatistics() const override {
4324     STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
4325   }
4326 };
4327 
4328 /// Dereferenceable attribute for a return value.
4329 struct AADereferenceableReturned final
4330     : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
4331   AADereferenceableReturned(const IRPosition &IRP, Attributor &A)
4332       : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
4333             IRP, A) {}
4334 
4335   /// See AbstractAttribute::trackStatistics()
4336   void trackStatistics() const override {
4337     STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
4338   }
4339 };
4340 
4341 /// Dereferenceable attribute for an argument
4342 struct AADereferenceableArgument final
4343     : AAArgumentFromCallSiteArguments<AADereferenceable,
4344                                       AADereferenceableImpl> {
4345   using Base =
4346       AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl>;
4347   AADereferenceableArgument(const IRPosition &IRP, Attributor &A)
4348       : Base(IRP, A) {}
4349 
4350   /// See AbstractAttribute::trackStatistics()
4351   void trackStatistics() const override {
4352     STATS_DECLTRACK_ARG_ATTR(dereferenceable)
4353   }
4354 };
4355 
4356 /// Dereferenceable attribute for a call site argument.
4357 struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
4358   AADereferenceableCallSiteArgument(const IRPosition &IRP, Attributor &A)
4359       : AADereferenceableFloating(IRP, A) {}
4360 
4361   /// See AbstractAttribute::trackStatistics()
4362   void trackStatistics() const override {
4363     STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
4364   }
4365 };
4366 
4367 /// Dereferenceable attribute deduction for a call site return value.
4368 struct AADereferenceableCallSiteReturned final
4369     : AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl> {
4370   using Base =
4371       AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl>;
4372   AADereferenceableCallSiteReturned(const IRPosition &IRP, Attributor &A)
4373       : Base(IRP, A) {}
4374 
4375   /// See AbstractAttribute::trackStatistics()
4376   void trackStatistics() const override {
4377     STATS_DECLTRACK_CS_ATTR(dereferenceable);
4378   }
4379 };
4380 
4381 // ------------------------ Align Argument Attribute ------------------------
4382 
4383 static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA,
4384                                     Value &AssociatedValue, const Use *U,
4385                                     const Instruction *I, bool &TrackUse) {
4386   // We need to follow common pointer manipulation uses to the accesses they
4387   // feed into.
4388   if (isa<CastInst>(I)) {
4389     // Follow all but ptr2int casts.
4390     TrackUse = !isa<PtrToIntInst>(I);
4391     return 0;
4392   }
4393   if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
4394     if (GEP->hasAllConstantIndices())
4395       TrackUse = true;
4396     return 0;
4397   }
4398 
4399   MaybeAlign MA;
4400   if (const auto *CB = dyn_cast<CallBase>(I)) {
4401     if (CB->isBundleOperand(U) || CB->isCallee(U))
4402       return 0;
4403 
4404     unsigned ArgNo = CB->getArgOperandNo(U);
4405     IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
4406     // As long as we only use known information there is no need to track
4407     // dependences here.
4408     auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, DepClassTy::NONE);
4409     MA = MaybeAlign(AlignAA.getKnownAlign());
4410   }
4411 
4412   const DataLayout &DL = A.getDataLayout();
4413   const Value *UseV = U->get();
4414   if (auto *SI = dyn_cast<StoreInst>(I)) {
4415     if (SI->getPointerOperand() == UseV)
4416       MA = SI->getAlign();
4417   } else if (auto *LI = dyn_cast<LoadInst>(I)) {
4418     if (LI->getPointerOperand() == UseV)
4419       MA = LI->getAlign();
4420   }
4421 
4422   if (!MA || *MA <= QueryingAA.getKnownAlign())
4423     return 0;
4424 
4425   unsigned Alignment = MA->value();
4426   int64_t Offset;
4427 
4428   if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
4429     if (Base == &AssociatedValue) {
4430       // BasePointerAddr + Offset = Alignment * Q for some integer Q.
4431       // So we can say that the maximum power of two which is a divisor of
4432       // gcd(Offset, Alignment) is an alignment.
4433 
4434       uint32_t gcd =
4435           greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
4436       Alignment = llvm::PowerOf2Floor(gcd);
4437     }
4438   }
4439 
4440   return Alignment;
4441 }
4442 
4443 struct AAAlignImpl : AAAlign {
4444   AAAlignImpl(const IRPosition &IRP, Attributor &A) : AAAlign(IRP, A) {}
4445 
4446   /// See AbstractAttribute::initialize(...).
4447   void initialize(Attributor &A) override {
4448     SmallVector<Attribute, 4> Attrs;
4449     getAttrs({Attribute::Alignment}, Attrs);
4450     for (const Attribute &Attr : Attrs)
4451       takeKnownMaximum(Attr.getValueAsInt());
4452 
4453     Value &V = getAssociatedValue();
4454     // TODO: This is a HACK to avoid getPointerAlignment to introduce a ptr2int
4455     //       use of the function pointer. This was caused by D73131. We want to
4456     //       avoid this for function pointers especially because we iterate
4457     //       their uses and int2ptr is not handled. It is not a correctness
4458     //       problem though!
4459     if (!V.getType()->getPointerElementType()->isFunctionTy())
4460       takeKnownMaximum(V.getPointerAlignment(A.getDataLayout()).value());
4461 
4462     if (getIRPosition().isFnInterfaceKind() &&
4463         (!getAnchorScope() ||
4464          !A.isFunctionIPOAmendable(*getAssociatedFunction()))) {
4465       indicatePessimisticFixpoint();
4466       return;
4467     }
4468 
4469     if (Instruction *CtxI = getCtxI())
4470       followUsesInMBEC(*this, A, getState(), *CtxI);
4471   }
4472 
4473   /// See AbstractAttribute::manifest(...).
4474   ChangeStatus manifest(Attributor &A) override {
4475     ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
4476 
4477     // Check for users that allow alignment annotations.
4478     Value &AssociatedValue = getAssociatedValue();
4479     for (const Use &U : AssociatedValue.uses()) {
4480       if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
4481         if (SI->getPointerOperand() == &AssociatedValue)
4482           if (SI->getAlignment() < getAssumedAlign()) {
4483             STATS_DECLTRACK(AAAlign, Store,
4484                             "Number of times alignment added to a store");
4485             SI->setAlignment(Align(getAssumedAlign()));
4486             LoadStoreChanged = ChangeStatus::CHANGED;
4487           }
4488       } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
4489         if (LI->getPointerOperand() == &AssociatedValue)
4490           if (LI->getAlignment() < getAssumedAlign()) {
4491             LI->setAlignment(Align(getAssumedAlign()));
4492             STATS_DECLTRACK(AAAlign, Load,
4493                             "Number of times alignment added to a load");
4494             LoadStoreChanged = ChangeStatus::CHANGED;
4495           }
4496       }
4497     }
4498 
4499     ChangeStatus Changed = AAAlign::manifest(A);
4500 
4501     Align InheritAlign =
4502         getAssociatedValue().getPointerAlignment(A.getDataLayout());
4503     if (InheritAlign >= getAssumedAlign())
4504       return LoadStoreChanged;
4505     return Changed | LoadStoreChanged;
4506   }
4507 
4508   // TODO: Provide a helper to determine the implied ABI alignment and check in
4509   //       the existing manifest method and a new one for AAAlignImpl that value
4510   //       to avoid making the alignment explicit if it did not improve.
4511 
4512   /// See AbstractAttribute::getDeducedAttributes
4513   virtual void
4514   getDeducedAttributes(LLVMContext &Ctx,
4515                        SmallVectorImpl<Attribute> &Attrs) const override {
4516     if (getAssumedAlign() > 1)
4517       Attrs.emplace_back(
4518           Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
4519   }
4520 
4521   /// See followUsesInMBEC
4522   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
4523                        AAAlign::StateType &State) {
4524     bool TrackUse = false;
4525 
4526     unsigned int KnownAlign =
4527         getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
4528     State.takeKnownMaximum(KnownAlign);
4529 
4530     return TrackUse;
4531   }
4532 
4533   /// See AbstractAttribute::getAsStr().
4534   const std::string getAsStr() const override {
4535     return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
4536                                 "-" + std::to_string(getAssumedAlign()) + ">")
4537                              : "unknown-align";
4538   }
4539 };
4540 
4541 /// Align attribute for a floating value.
4542 struct AAAlignFloating : AAAlignImpl {
4543   AAAlignFloating(const IRPosition &IRP, Attributor &A) : AAAlignImpl(IRP, A) {}
4544 
4545   /// See AbstractAttribute::updateImpl(...).
4546   ChangeStatus updateImpl(Attributor &A) override {
4547     const DataLayout &DL = A.getDataLayout();
4548 
4549     auto VisitValueCB = [&](Value &V, const Instruction *,
4550                             AAAlign::StateType &T, bool Stripped) -> bool {
4551       const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V),
4552                                            DepClassTy::REQUIRED);
4553       if (!Stripped && this == &AA) {
4554         int64_t Offset;
4555         unsigned Alignment = 1;
4556         if (const Value *Base =
4557                 GetPointerBaseWithConstantOffset(&V, Offset, DL)) {
4558           Align PA = Base->getPointerAlignment(DL);
4559           // BasePointerAddr + Offset = Alignment * Q for some integer Q.
4560           // So we can say that the maximum power of two which is a divisor of
4561           // gcd(Offset, Alignment) is an alignment.
4562 
4563           uint32_t gcd = greatestCommonDivisor(uint32_t(abs((int32_t)Offset)),
4564                                                uint32_t(PA.value()));
4565           Alignment = llvm::PowerOf2Floor(gcd);
4566         } else {
4567           Alignment = V.getPointerAlignment(DL).value();
4568         }
4569         // Use only IR information if we did not strip anything.
4570         T.takeKnownMaximum(Alignment);
4571         T.indicatePessimisticFixpoint();
4572       } else {
4573         // Use abstract attribute information.
4574         const AAAlign::StateType &DS = AA.getState();
4575         T ^= DS;
4576       }
4577       return T.isValidState();
4578     };
4579 
4580     StateType T;
4581     if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
4582                                           VisitValueCB, getCtxI()))
4583       return indicatePessimisticFixpoint();
4584 
4585     // TODO: If we know we visited all incoming values, thus no are assumed
4586     // dead, we can take the known information from the state T.
4587     return clampStateAndIndicateChange(getState(), T);
4588   }
4589 
4590   /// See AbstractAttribute::trackStatistics()
4591   void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
4592 };
4593 
4594 /// Align attribute for function return value.
4595 struct AAAlignReturned final
4596     : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
4597   using Base = AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>;
4598   AAAlignReturned(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
4599 
4600   /// See AbstractAttribute::initialize(...).
4601   void initialize(Attributor &A) override {
4602     Base::initialize(A);
4603     Function *F = getAssociatedFunction();
4604     if (!F || F->isDeclaration())
4605       indicatePessimisticFixpoint();
4606   }
4607 
4608   /// See AbstractAttribute::trackStatistics()
4609   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
4610 };
4611 
4612 /// Align attribute for function argument.
4613 struct AAAlignArgument final
4614     : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
4615   using Base = AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>;
4616   AAAlignArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
4617 
4618   /// See AbstractAttribute::manifest(...).
4619   ChangeStatus manifest(Attributor &A) override {
4620     // If the associated argument is involved in a must-tail call we give up
4621     // because we would need to keep the argument alignments of caller and
4622     // callee in-sync. Just does not seem worth the trouble right now.
4623     if (A.getInfoCache().isInvolvedInMustTailCall(*getAssociatedArgument()))
4624       return ChangeStatus::UNCHANGED;
4625     return Base::manifest(A);
4626   }
4627 
4628   /// See AbstractAttribute::trackStatistics()
4629   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
4630 };
4631 
4632 struct AAAlignCallSiteArgument final : AAAlignFloating {
4633   AAAlignCallSiteArgument(const IRPosition &IRP, Attributor &A)
4634       : AAAlignFloating(IRP, A) {}
4635 
4636   /// See AbstractAttribute::manifest(...).
4637   ChangeStatus manifest(Attributor &A) override {
4638     // If the associated argument is involved in a must-tail call we give up
4639     // because we would need to keep the argument alignments of caller and
4640     // callee in-sync. Just does not seem worth the trouble right now.
4641     if (Argument *Arg = getAssociatedArgument())
4642       if (A.getInfoCache().isInvolvedInMustTailCall(*Arg))
4643         return ChangeStatus::UNCHANGED;
4644     ChangeStatus Changed = AAAlignImpl::manifest(A);
4645     Align InheritAlign =
4646         getAssociatedValue().getPointerAlignment(A.getDataLayout());
4647     if (InheritAlign >= getAssumedAlign())
4648       Changed = ChangeStatus::UNCHANGED;
4649     return Changed;
4650   }
4651 
4652   /// See AbstractAttribute::updateImpl(Attributor &A).
4653   ChangeStatus updateImpl(Attributor &A) override {
4654     ChangeStatus Changed = AAAlignFloating::updateImpl(A);
4655     if (Argument *Arg = getAssociatedArgument()) {
4656       // We only take known information from the argument
4657       // so we do not need to track a dependence.
4658       const auto &ArgAlignAA = A.getAAFor<AAAlign>(
4659           *this, IRPosition::argument(*Arg), DepClassTy::NONE);
4660       takeKnownMaximum(ArgAlignAA.getKnownAlign());
4661     }
4662     return Changed;
4663   }
4664 
4665   /// See AbstractAttribute::trackStatistics()
4666   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
4667 };
4668 
4669 /// Align attribute deduction for a call site return value.
4670 struct AAAlignCallSiteReturned final
4671     : AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl> {
4672   using Base = AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl>;
4673   AAAlignCallSiteReturned(const IRPosition &IRP, Attributor &A)
4674       : Base(IRP, A) {}
4675 
4676   /// See AbstractAttribute::initialize(...).
4677   void initialize(Attributor &A) override {
4678     Base::initialize(A);
4679     Function *F = getAssociatedFunction();
4680     if (!F || F->isDeclaration())
4681       indicatePessimisticFixpoint();
4682   }
4683 
4684   /// See AbstractAttribute::trackStatistics()
4685   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
4686 };
4687 
4688 /// ------------------ Function No-Return Attribute ----------------------------
4689 struct AANoReturnImpl : public AANoReturn {
4690   AANoReturnImpl(const IRPosition &IRP, Attributor &A) : AANoReturn(IRP, A) {}
4691 
4692   /// See AbstractAttribute::initialize(...).
4693   void initialize(Attributor &A) override {
4694     AANoReturn::initialize(A);
4695     Function *F = getAssociatedFunction();
4696     if (!F || F->isDeclaration())
4697       indicatePessimisticFixpoint();
4698   }
4699 
4700   /// See AbstractAttribute::getAsStr().
4701   const std::string getAsStr() const override {
4702     return getAssumed() ? "noreturn" : "may-return";
4703   }
4704 
4705   /// See AbstractAttribute::updateImpl(Attributor &A).
4706   virtual ChangeStatus updateImpl(Attributor &A) override {
4707     auto CheckForNoReturn = [](Instruction &) { return false; };
4708     bool UsedAssumedInformation = false;
4709     if (!A.checkForAllInstructions(CheckForNoReturn, *this,
4710                                    {(unsigned)Instruction::Ret},
4711                                    UsedAssumedInformation))
4712       return indicatePessimisticFixpoint();
4713     return ChangeStatus::UNCHANGED;
4714   }
4715 };
4716 
4717 struct AANoReturnFunction final : AANoReturnImpl {
4718   AANoReturnFunction(const IRPosition &IRP, Attributor &A)
4719       : AANoReturnImpl(IRP, A) {}
4720 
4721   /// See AbstractAttribute::trackStatistics()
4722   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
4723 };
4724 
4725 /// NoReturn attribute deduction for a call sites.
4726 struct AANoReturnCallSite final : AANoReturnImpl {
4727   AANoReturnCallSite(const IRPosition &IRP, Attributor &A)
4728       : AANoReturnImpl(IRP, A) {}
4729 
4730   /// See AbstractAttribute::initialize(...).
4731   void initialize(Attributor &A) override {
4732     AANoReturnImpl::initialize(A);
4733     if (Function *F = getAssociatedFunction()) {
4734       const IRPosition &FnPos = IRPosition::function(*F);
4735       auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4736       if (!FnAA.isAssumedNoReturn())
4737         indicatePessimisticFixpoint();
4738     }
4739   }
4740 
4741   /// See AbstractAttribute::updateImpl(...).
4742   ChangeStatus updateImpl(Attributor &A) override {
4743     // TODO: Once we have call site specific value information we can provide
4744     //       call site specific liveness information and then it makes
4745     //       sense to specialize attributes for call sites arguments instead of
4746     //       redirecting requests to the callee argument.
4747     Function *F = getAssociatedFunction();
4748     const IRPosition &FnPos = IRPosition::function(*F);
4749     auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4750     return clampStateAndIndicateChange(getState(), FnAA.getState());
4751   }
4752 
4753   /// See AbstractAttribute::trackStatistics()
4754   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
4755 };
4756 
4757 /// ----------------------- Variable Capturing ---------------------------------
4758 
4759 /// A class to hold the state of for no-capture attributes.
4760 struct AANoCaptureImpl : public AANoCapture {
4761   AANoCaptureImpl(const IRPosition &IRP, Attributor &A) : AANoCapture(IRP, A) {}
4762 
4763   /// See AbstractAttribute::initialize(...).
4764   void initialize(Attributor &A) override {
4765     if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
4766       indicateOptimisticFixpoint();
4767       return;
4768     }
4769     Function *AnchorScope = getAnchorScope();
4770     if (isFnInterfaceKind() &&
4771         (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) {
4772       indicatePessimisticFixpoint();
4773       return;
4774     }
4775 
4776     // You cannot "capture" null in the default address space.
4777     if (isa<ConstantPointerNull>(getAssociatedValue()) &&
4778         getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
4779       indicateOptimisticFixpoint();
4780       return;
4781     }
4782 
4783     const Function *F =
4784         isArgumentPosition() ? getAssociatedFunction() : AnchorScope;
4785 
4786     // Check what state the associated function can actually capture.
4787     if (F)
4788       determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
4789     else
4790       indicatePessimisticFixpoint();
4791   }
4792 
4793   /// See AbstractAttribute::updateImpl(...).
4794   ChangeStatus updateImpl(Attributor &A) override;
4795 
4796   /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
4797   virtual void
4798   getDeducedAttributes(LLVMContext &Ctx,
4799                        SmallVectorImpl<Attribute> &Attrs) const override {
4800     if (!isAssumedNoCaptureMaybeReturned())
4801       return;
4802 
4803     if (isArgumentPosition()) {
4804       if (isAssumedNoCapture())
4805         Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
4806       else if (ManifestInternal)
4807         Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
4808     }
4809   }
4810 
4811   /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
4812   /// depending on the ability of the function associated with \p IRP to capture
4813   /// state in memory and through "returning/throwing", respectively.
4814   static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
4815                                                    const Function &F,
4816                                                    BitIntegerState &State) {
4817     // TODO: Once we have memory behavior attributes we should use them here.
4818 
4819     // If we know we cannot communicate or write to memory, we do not care about
4820     // ptr2int anymore.
4821     if (F.onlyReadsMemory() && F.doesNotThrow() &&
4822         F.getReturnType()->isVoidTy()) {
4823       State.addKnownBits(NO_CAPTURE);
4824       return;
4825     }
4826 
4827     // A function cannot capture state in memory if it only reads memory, it can
4828     // however return/throw state and the state might be influenced by the
4829     // pointer value, e.g., loading from a returned pointer might reveal a bit.
4830     if (F.onlyReadsMemory())
4831       State.addKnownBits(NOT_CAPTURED_IN_MEM);
4832 
4833     // A function cannot communicate state back if it does not through
4834     // exceptions and doesn not return values.
4835     if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
4836       State.addKnownBits(NOT_CAPTURED_IN_RET);
4837 
4838     // Check existing "returned" attributes.
4839     int ArgNo = IRP.getCalleeArgNo();
4840     if (F.doesNotThrow() && ArgNo >= 0) {
4841       for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
4842         if (F.hasParamAttribute(u, Attribute::Returned)) {
4843           if (u == unsigned(ArgNo))
4844             State.removeAssumedBits(NOT_CAPTURED_IN_RET);
4845           else if (F.onlyReadsMemory())
4846             State.addKnownBits(NO_CAPTURE);
4847           else
4848             State.addKnownBits(NOT_CAPTURED_IN_RET);
4849           break;
4850         }
4851     }
4852   }
4853 
4854   /// See AbstractState::getAsStr().
4855   const std::string getAsStr() const override {
4856     if (isKnownNoCapture())
4857       return "known not-captured";
4858     if (isAssumedNoCapture())
4859       return "assumed not-captured";
4860     if (isKnownNoCaptureMaybeReturned())
4861       return "known not-captured-maybe-returned";
4862     if (isAssumedNoCaptureMaybeReturned())
4863       return "assumed not-captured-maybe-returned";
4864     return "assumed-captured";
4865   }
4866 };
4867 
4868 /// Attributor-aware capture tracker.
4869 struct AACaptureUseTracker final : public CaptureTracker {
4870 
4871   /// Create a capture tracker that can lookup in-flight abstract attributes
4872   /// through the Attributor \p A.
4873   ///
4874   /// If a use leads to a potential capture, \p CapturedInMemory is set and the
4875   /// search is stopped. If a use leads to a return instruction,
4876   /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
4877   /// If a use leads to a ptr2int which may capture the value,
4878   /// \p CapturedInInteger is set. If a use is found that is currently assumed
4879   /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
4880   /// set. All values in \p PotentialCopies are later tracked as well. For every
4881   /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
4882   /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
4883   /// conservatively set to true.
4884   AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
4885                       const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
4886                       SmallSetVector<Value *, 4> &PotentialCopies,
4887                       unsigned &RemainingUsesToExplore)
4888       : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
4889         PotentialCopies(PotentialCopies),
4890         RemainingUsesToExplore(RemainingUsesToExplore) {}
4891 
4892   /// Determine if \p V maybe captured. *Also updates the state!*
4893   bool valueMayBeCaptured(const Value *V) {
4894     if (V->getType()->isPointerTy()) {
4895       PointerMayBeCaptured(V, this);
4896     } else {
4897       State.indicatePessimisticFixpoint();
4898     }
4899     return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4900   }
4901 
4902   /// See CaptureTracker::tooManyUses().
4903   void tooManyUses() override {
4904     State.removeAssumedBits(AANoCapture::NO_CAPTURE);
4905   }
4906 
4907   bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
4908     if (CaptureTracker::isDereferenceableOrNull(O, DL))
4909       return true;
4910     const auto &DerefAA = A.getAAFor<AADereferenceable>(
4911         NoCaptureAA, IRPosition::value(*O), DepClassTy::OPTIONAL);
4912     return DerefAA.getAssumedDereferenceableBytes();
4913   }
4914 
4915   /// See CaptureTracker::captured(...).
4916   bool captured(const Use *U) override {
4917     Instruction *UInst = cast<Instruction>(U->getUser());
4918     LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
4919                       << "\n");
4920 
4921     // Because we may reuse the tracker multiple times we keep track of the
4922     // number of explored uses ourselves as well.
4923     if (RemainingUsesToExplore-- == 0) {
4924       LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
4925       return isCapturedIn(/* Memory */ true, /* Integer */ true,
4926                           /* Return */ true);
4927     }
4928 
4929     // Deal with ptr2int by following uses.
4930     if (isa<PtrToIntInst>(UInst)) {
4931       LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
4932       return valueMayBeCaptured(UInst);
4933     }
4934 
4935     // For stores we check if we can follow the value through memory or not.
4936     if (auto *SI = dyn_cast<StoreInst>(UInst)) {
4937       if (SI->isVolatile())
4938         return isCapturedIn(/* Memory */ true, /* Integer */ false,
4939                             /* Return */ false);
4940       bool UsedAssumedInformation = false;
4941       if (!AA::getPotentialCopiesOfStoredValue(
4942               A, *SI, PotentialCopies, NoCaptureAA, UsedAssumedInformation))
4943         return isCapturedIn(/* Memory */ true, /* Integer */ false,
4944                             /* Return */ false);
4945       // Not captured directly, potential copies will be checked.
4946       return isCapturedIn(/* Memory */ false, /* Integer */ false,
4947                           /* Return */ false);
4948     }
4949 
4950     // Explicitly catch return instructions.
4951     if (isa<ReturnInst>(UInst)) {
4952       if (UInst->getFunction() == NoCaptureAA.getAnchorScope())
4953         return isCapturedIn(/* Memory */ false, /* Integer */ false,
4954                             /* Return */ true);
4955       return isCapturedIn(/* Memory */ true, /* Integer */ true,
4956                           /* Return */ true);
4957     }
4958 
4959     // For now we only use special logic for call sites. However, the tracker
4960     // itself knows about a lot of other non-capturing cases already.
4961     auto *CB = dyn_cast<CallBase>(UInst);
4962     if (!CB || !CB->isArgOperand(U))
4963       return isCapturedIn(/* Memory */ true, /* Integer */ true,
4964                           /* Return */ true);
4965 
4966     unsigned ArgNo = CB->getArgOperandNo(U);
4967     const IRPosition &CSArgPos = IRPosition::callsite_argument(*CB, ArgNo);
4968     // If we have a abstract no-capture attribute for the argument we can use
4969     // it to justify a non-capture attribute here. This allows recursion!
4970     auto &ArgNoCaptureAA =
4971         A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos, DepClassTy::REQUIRED);
4972     if (ArgNoCaptureAA.isAssumedNoCapture())
4973       return isCapturedIn(/* Memory */ false, /* Integer */ false,
4974                           /* Return */ false);
4975     if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
4976       addPotentialCopy(*CB);
4977       return isCapturedIn(/* Memory */ false, /* Integer */ false,
4978                           /* Return */ false);
4979     }
4980 
4981     // Lastly, we could not find a reason no-capture can be assumed so we don't.
4982     return isCapturedIn(/* Memory */ true, /* Integer */ true,
4983                         /* Return */ true);
4984   }
4985 
4986   /// Register \p CS as potential copy of the value we are checking.
4987   void addPotentialCopy(CallBase &CB) { PotentialCopies.insert(&CB); }
4988 
4989   /// See CaptureTracker::shouldExplore(...).
4990   bool shouldExplore(const Use *U) override {
4991     // Check liveness and ignore droppable users.
4992     bool UsedAssumedInformation = false;
4993     return !U->getUser()->isDroppable() &&
4994            !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA,
4995                             UsedAssumedInformation);
4996   }
4997 
4998   /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
4999   /// \p CapturedInRet, then return the appropriate value for use in the
5000   /// CaptureTracker::captured() interface.
5001   bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
5002                     bool CapturedInRet) {
5003     LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
5004                       << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
5005     if (CapturedInMem)
5006       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
5007     if (CapturedInInt)
5008       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
5009     if (CapturedInRet)
5010       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
5011     return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
5012   }
5013 
5014 private:
5015   /// The attributor providing in-flight abstract attributes.
5016   Attributor &A;
5017 
5018   /// The abstract attribute currently updated.
5019   AANoCapture &NoCaptureAA;
5020 
5021   /// The abstract liveness state.
5022   const AAIsDead &IsDeadAA;
5023 
5024   /// The state currently updated.
5025   AANoCapture::StateType &State;
5026 
5027   /// Set of potential copies of the tracked value.
5028   SmallSetVector<Value *, 4> &PotentialCopies;
5029 
5030   /// Global counter to limit the number of explored uses.
5031   unsigned &RemainingUsesToExplore;
5032 };
5033 
5034 ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
5035   const IRPosition &IRP = getIRPosition();
5036   Value *V = isArgumentPosition() ? IRP.getAssociatedArgument()
5037                                   : &IRP.getAssociatedValue();
5038   if (!V)
5039     return indicatePessimisticFixpoint();
5040 
5041   const Function *F =
5042       isArgumentPosition() ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
5043   assert(F && "Expected a function!");
5044   const IRPosition &FnPos = IRPosition::function(*F);
5045   const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos, DepClassTy::NONE);
5046 
5047   AANoCapture::StateType T;
5048 
5049   // Readonly means we cannot capture through memory.
5050   bool IsKnown;
5051   if (AA::isAssumedReadOnly(A, FnPos, *this, IsKnown)) {
5052     T.addKnownBits(NOT_CAPTURED_IN_MEM);
5053     if (IsKnown)
5054       addKnownBits(NOT_CAPTURED_IN_MEM);
5055   }
5056 
5057   // Make sure all returned values are different than the underlying value.
5058   // TODO: we could do this in a more sophisticated way inside
5059   //       AAReturnedValues, e.g., track all values that escape through returns
5060   //       directly somehow.
5061   auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
5062     bool SeenConstant = false;
5063     for (auto &It : RVAA.returned_values()) {
5064       if (isa<Constant>(It.first)) {
5065         if (SeenConstant)
5066           return false;
5067         SeenConstant = true;
5068       } else if (!isa<Argument>(It.first) ||
5069                  It.first == getAssociatedArgument())
5070         return false;
5071     }
5072     return true;
5073   };
5074 
5075   const auto &NoUnwindAA =
5076       A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::OPTIONAL);
5077   if (NoUnwindAA.isAssumedNoUnwind()) {
5078     bool IsVoidTy = F->getReturnType()->isVoidTy();
5079     const AAReturnedValues *RVAA =
5080         IsVoidTy ? nullptr
5081                  : &A.getAAFor<AAReturnedValues>(*this, FnPos,
5082 
5083                                                  DepClassTy::OPTIONAL);
5084     if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
5085       T.addKnownBits(NOT_CAPTURED_IN_RET);
5086       if (T.isKnown(NOT_CAPTURED_IN_MEM))
5087         return ChangeStatus::UNCHANGED;
5088       if (NoUnwindAA.isKnownNoUnwind() &&
5089           (IsVoidTy || RVAA->getState().isAtFixpoint())) {
5090         addKnownBits(NOT_CAPTURED_IN_RET);
5091         if (isKnown(NOT_CAPTURED_IN_MEM))
5092           return indicateOptimisticFixpoint();
5093       }
5094     }
5095   }
5096 
5097   // Use the CaptureTracker interface and logic with the specialized tracker,
5098   // defined in AACaptureUseTracker, that can look at in-flight abstract
5099   // attributes and directly updates the assumed state.
5100   SmallSetVector<Value *, 4> PotentialCopies;
5101   unsigned RemainingUsesToExplore =
5102       getDefaultMaxUsesToExploreForCaptureTracking();
5103   AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
5104                               RemainingUsesToExplore);
5105 
5106   // Check all potential copies of the associated value until we can assume
5107   // none will be captured or we have to assume at least one might be.
5108   unsigned Idx = 0;
5109   PotentialCopies.insert(V);
5110   while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
5111     Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
5112 
5113   AANoCapture::StateType &S = getState();
5114   auto Assumed = S.getAssumed();
5115   S.intersectAssumedBits(T.getAssumed());
5116   if (!isAssumedNoCaptureMaybeReturned())
5117     return indicatePessimisticFixpoint();
5118   return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
5119                                    : ChangeStatus::CHANGED;
5120 }
5121 
5122 /// NoCapture attribute for function arguments.
5123 struct AANoCaptureArgument final : AANoCaptureImpl {
5124   AANoCaptureArgument(const IRPosition &IRP, Attributor &A)
5125       : AANoCaptureImpl(IRP, A) {}
5126 
5127   /// See AbstractAttribute::trackStatistics()
5128   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
5129 };
5130 
5131 /// NoCapture attribute for call site arguments.
5132 struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
5133   AANoCaptureCallSiteArgument(const IRPosition &IRP, Attributor &A)
5134       : AANoCaptureImpl(IRP, A) {}
5135 
5136   /// See AbstractAttribute::initialize(...).
5137   void initialize(Attributor &A) override {
5138     if (Argument *Arg = getAssociatedArgument())
5139       if (Arg->hasByValAttr())
5140         indicateOptimisticFixpoint();
5141     AANoCaptureImpl::initialize(A);
5142   }
5143 
5144   /// See AbstractAttribute::updateImpl(...).
5145   ChangeStatus updateImpl(Attributor &A) override {
5146     // TODO: Once we have call site specific value information we can provide
5147     //       call site specific liveness information and then it makes
5148     //       sense to specialize attributes for call sites arguments instead of
5149     //       redirecting requests to the callee argument.
5150     Argument *Arg = getAssociatedArgument();
5151     if (!Arg)
5152       return indicatePessimisticFixpoint();
5153     const IRPosition &ArgPos = IRPosition::argument(*Arg);
5154     auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos, DepClassTy::REQUIRED);
5155     return clampStateAndIndicateChange(getState(), ArgAA.getState());
5156   }
5157 
5158   /// See AbstractAttribute::trackStatistics()
5159   void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
5160 };
5161 
5162 /// NoCapture attribute for floating values.
5163 struct AANoCaptureFloating final : AANoCaptureImpl {
5164   AANoCaptureFloating(const IRPosition &IRP, Attributor &A)
5165       : AANoCaptureImpl(IRP, A) {}
5166 
5167   /// See AbstractAttribute::trackStatistics()
5168   void trackStatistics() const override {
5169     STATS_DECLTRACK_FLOATING_ATTR(nocapture)
5170   }
5171 };
5172 
5173 /// NoCapture attribute for function return value.
5174 struct AANoCaptureReturned final : AANoCaptureImpl {
5175   AANoCaptureReturned(const IRPosition &IRP, Attributor &A)
5176       : AANoCaptureImpl(IRP, A) {
5177     llvm_unreachable("NoCapture is not applicable to function returns!");
5178   }
5179 
5180   /// See AbstractAttribute::initialize(...).
5181   void initialize(Attributor &A) override {
5182     llvm_unreachable("NoCapture is not applicable to function returns!");
5183   }
5184 
5185   /// See AbstractAttribute::updateImpl(...).
5186   ChangeStatus updateImpl(Attributor &A) override {
5187     llvm_unreachable("NoCapture is not applicable to function returns!");
5188   }
5189 
5190   /// See AbstractAttribute::trackStatistics()
5191   void trackStatistics() const override {}
5192 };
5193 
5194 /// NoCapture attribute deduction for a call site return value.
5195 struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
5196   AANoCaptureCallSiteReturned(const IRPosition &IRP, Attributor &A)
5197       : AANoCaptureImpl(IRP, A) {}
5198 
5199   /// See AbstractAttribute::initialize(...).
5200   void initialize(Attributor &A) override {
5201     const Function *F = getAnchorScope();
5202     // Check what state the associated function can actually capture.
5203     determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
5204   }
5205 
5206   /// See AbstractAttribute::trackStatistics()
5207   void trackStatistics() const override {
5208     STATS_DECLTRACK_CSRET_ATTR(nocapture)
5209   }
5210 };
5211 
5212 /// ------------------ Value Simplify Attribute ----------------------------
5213 
5214 bool ValueSimplifyStateType::unionAssumed(Optional<Value *> Other) {
5215   // FIXME: Add a typecast support.
5216   SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5217       SimplifiedAssociatedValue, Other, Ty);
5218   if (SimplifiedAssociatedValue == Optional<Value *>(nullptr))
5219     return false;
5220 
5221   LLVM_DEBUG({
5222     if (SimplifiedAssociatedValue.hasValue())
5223       dbgs() << "[ValueSimplify] is assumed to be "
5224              << **SimplifiedAssociatedValue << "\n";
5225     else
5226       dbgs() << "[ValueSimplify] is assumed to be <none>\n";
5227   });
5228   return true;
5229 }
5230 
5231 struct AAValueSimplifyImpl : AAValueSimplify {
5232   AAValueSimplifyImpl(const IRPosition &IRP, Attributor &A)
5233       : AAValueSimplify(IRP, A) {}
5234 
5235   /// See AbstractAttribute::initialize(...).
5236   void initialize(Attributor &A) override {
5237     if (getAssociatedValue().getType()->isVoidTy())
5238       indicatePessimisticFixpoint();
5239     if (A.hasSimplificationCallback(getIRPosition()))
5240       indicatePessimisticFixpoint();
5241   }
5242 
5243   /// See AbstractAttribute::getAsStr().
5244   const std::string getAsStr() const override {
5245     LLVM_DEBUG({
5246       errs() << "SAV: " << SimplifiedAssociatedValue << " ";
5247       if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue)
5248         errs() << "SAV: " << **SimplifiedAssociatedValue << " ";
5249     });
5250     return isValidState() ? (isAtFixpoint() ? "simplified" : "maybe-simple")
5251                           : "not-simple";
5252   }
5253 
5254   /// See AbstractAttribute::trackStatistics()
5255   void trackStatistics() const override {}
5256 
5257   /// See AAValueSimplify::getAssumedSimplifiedValue()
5258   Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
5259     return SimplifiedAssociatedValue;
5260   }
5261 
5262   /// Return a value we can use as replacement for the associated one, or
5263   /// nullptr if we don't have one that makes sense.
5264   Value *getReplacementValue(Attributor &A) const {
5265     Value *NewV;
5266     NewV = SimplifiedAssociatedValue.hasValue()
5267                ? SimplifiedAssociatedValue.getValue()
5268                : UndefValue::get(getAssociatedType());
5269     if (!NewV)
5270       return nullptr;
5271     NewV = AA::getWithType(*NewV, *getAssociatedType());
5272     if (!NewV || NewV == &getAssociatedValue())
5273       return nullptr;
5274     const Instruction *CtxI = getCtxI();
5275     if (CtxI && !AA::isValidAtPosition(*NewV, *CtxI, A.getInfoCache()))
5276       return nullptr;
5277     if (!CtxI && !AA::isValidInScope(*NewV, getAnchorScope()))
5278       return nullptr;
5279     return NewV;
5280   }
5281 
5282   /// Helper function for querying AAValueSimplify and updating candicate.
5283   /// \param IRP The value position we are trying to unify with SimplifiedValue
5284   bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
5285                       const IRPosition &IRP, bool Simplify = true) {
5286     bool UsedAssumedInformation = false;
5287     Optional<Value *> QueryingValueSimplified = &IRP.getAssociatedValue();
5288     if (Simplify)
5289       QueryingValueSimplified =
5290           A.getAssumedSimplified(IRP, QueryingAA, UsedAssumedInformation);
5291     return unionAssumed(QueryingValueSimplified);
5292   }
5293 
5294   /// Returns a candidate is found or not
5295   template <typename AAType> bool askSimplifiedValueFor(Attributor &A) {
5296     if (!getAssociatedValue().getType()->isIntegerTy())
5297       return false;
5298 
5299     // This will also pass the call base context.
5300     const auto &AA =
5301         A.getAAFor<AAType>(*this, getIRPosition(), DepClassTy::NONE);
5302 
5303     Optional<ConstantInt *> COpt = AA.getAssumedConstantInt(A);
5304 
5305     if (!COpt.hasValue()) {
5306       SimplifiedAssociatedValue = llvm::None;
5307       A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
5308       return true;
5309     }
5310     if (auto *C = COpt.getValue()) {
5311       SimplifiedAssociatedValue = C;
5312       A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
5313       return true;
5314     }
5315     return false;
5316   }
5317 
5318   bool askSimplifiedValueForOtherAAs(Attributor &A) {
5319     if (askSimplifiedValueFor<AAValueConstantRange>(A))
5320       return true;
5321     if (askSimplifiedValueFor<AAPotentialValues>(A))
5322       return true;
5323     return false;
5324   }
5325 
5326   /// See AbstractAttribute::manifest(...).
5327   ChangeStatus manifest(Attributor &A) override {
5328     ChangeStatus Changed = ChangeStatus::UNCHANGED;
5329     if (getAssociatedValue().user_empty())
5330       return Changed;
5331 
5332     if (auto *NewV = getReplacementValue(A)) {
5333       LLVM_DEBUG(dbgs() << "[ValueSimplify] " << getAssociatedValue() << " -> "
5334                         << *NewV << " :: " << *this << "\n");
5335       if (A.changeValueAfterManifest(getAssociatedValue(), *NewV))
5336         Changed = ChangeStatus::CHANGED;
5337     }
5338 
5339     return Changed | AAValueSimplify::manifest(A);
5340   }
5341 
5342   /// See AbstractState::indicatePessimisticFixpoint(...).
5343   ChangeStatus indicatePessimisticFixpoint() override {
5344     SimplifiedAssociatedValue = &getAssociatedValue();
5345     return AAValueSimplify::indicatePessimisticFixpoint();
5346   }
5347 
5348   static bool handleLoad(Attributor &A, const AbstractAttribute &AA,
5349                          LoadInst &L, function_ref<bool(Value &)> Union) {
5350     auto UnionWrapper = [&](Value &V, Value &Obj) {
5351       if (isa<AllocaInst>(Obj))
5352         return Union(V);
5353       if (!AA::isDynamicallyUnique(A, AA, V))
5354         return false;
5355       if (!AA::isValidAtPosition(V, L, A.getInfoCache()))
5356         return false;
5357       return Union(V);
5358     };
5359 
5360     Value &Ptr = *L.getPointerOperand();
5361     SmallVector<Value *, 8> Objects;
5362     if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, AA, &L))
5363       return false;
5364 
5365     const auto *TLI =
5366         A.getInfoCache().getTargetLibraryInfoForFunction(*L.getFunction());
5367     for (Value *Obj : Objects) {
5368       LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n");
5369       if (isa<UndefValue>(Obj))
5370         continue;
5371       if (isa<ConstantPointerNull>(Obj)) {
5372         // A null pointer access can be undefined but any offset from null may
5373         // be OK. We do not try to optimize the latter.
5374         bool UsedAssumedInformation = false;
5375         if (!NullPointerIsDefined(L.getFunction(),
5376                                   Ptr.getType()->getPointerAddressSpace()) &&
5377             A.getAssumedSimplified(Ptr, AA, UsedAssumedInformation) == Obj)
5378           continue;
5379         return false;
5380       }
5381       Constant *InitialVal = AA::getInitialValueForObj(*Obj, *L.getType(), TLI);
5382       if (!InitialVal || !Union(*InitialVal))
5383         return false;
5384 
5385       LLVM_DEBUG(dbgs() << "Underlying object amenable to load-store "
5386                            "propagation, checking accesses next.\n");
5387 
5388       auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
5389         LLVM_DEBUG(dbgs() << " - visit access " << Acc << "\n");
5390         if (Acc.isWrittenValueYetUndetermined())
5391           return true;
5392         Value *Content = Acc.getWrittenValue();
5393         if (!Content)
5394           return false;
5395         Value *CastedContent =
5396             AA::getWithType(*Content, *AA.getAssociatedType());
5397         if (!CastedContent)
5398           return false;
5399         if (IsExact)
5400           return UnionWrapper(*CastedContent, *Obj);
5401         if (auto *C = dyn_cast<Constant>(CastedContent))
5402           if (C->isNullValue() || C->isAllOnesValue() || isa<UndefValue>(C))
5403             return UnionWrapper(*CastedContent, *Obj);
5404         return false;
5405       };
5406 
5407       auto &PI = A.getAAFor<AAPointerInfo>(AA, IRPosition::value(*Obj),
5408                                            DepClassTy::REQUIRED);
5409       if (!PI.forallInterferingWrites(A, AA, L, CheckAccess))
5410         return false;
5411     }
5412     return true;
5413   }
5414 };
5415 
5416 struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
5417   AAValueSimplifyArgument(const IRPosition &IRP, Attributor &A)
5418       : AAValueSimplifyImpl(IRP, A) {}
5419 
5420   void initialize(Attributor &A) override {
5421     AAValueSimplifyImpl::initialize(A);
5422     if (!getAnchorScope() || getAnchorScope()->isDeclaration())
5423       indicatePessimisticFixpoint();
5424     if (hasAttr({Attribute::InAlloca, Attribute::Preallocated,
5425                  Attribute::StructRet, Attribute::Nest, Attribute::ByVal},
5426                 /* IgnoreSubsumingPositions */ true))
5427       indicatePessimisticFixpoint();
5428 
5429     // FIXME: This is a hack to prevent us from propagating function poiner in
5430     // the new pass manager CGSCC pass as it creates call edges the
5431     // CallGraphUpdater cannot handle yet.
5432     Value &V = getAssociatedValue();
5433     if (V.getType()->isPointerTy() &&
5434         V.getType()->getPointerElementType()->isFunctionTy() &&
5435         !A.isModulePass())
5436       indicatePessimisticFixpoint();
5437   }
5438 
5439   /// See AbstractAttribute::updateImpl(...).
5440   ChangeStatus updateImpl(Attributor &A) override {
5441     // Byval is only replacable if it is readonly otherwise we would write into
5442     // the replaced value and not the copy that byval creates implicitly.
5443     Argument *Arg = getAssociatedArgument();
5444     if (Arg->hasByValAttr()) {
5445       // TODO: We probably need to verify synchronization is not an issue, e.g.,
5446       //       there is no race by not copying a constant byval.
5447       bool IsKnown;
5448       if (!AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
5449         return indicatePessimisticFixpoint();
5450     }
5451 
5452     auto Before = SimplifiedAssociatedValue;
5453 
5454     auto PredForCallSite = [&](AbstractCallSite ACS) {
5455       const IRPosition &ACSArgPos =
5456           IRPosition::callsite_argument(ACS, getCallSiteArgNo());
5457       // Check if a coresponding argument was found or if it is on not
5458       // associated (which can happen for callback calls).
5459       if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
5460         return false;
5461 
5462       // Simplify the argument operand explicitly and check if the result is
5463       // valid in the current scope. This avoids refering to simplified values
5464       // in other functions, e.g., we don't want to say a an argument in a
5465       // static function is actually an argument in a different function.
5466       bool UsedAssumedInformation = false;
5467       Optional<Constant *> SimpleArgOp =
5468           A.getAssumedConstant(ACSArgPos, *this, UsedAssumedInformation);
5469       if (!SimpleArgOp.hasValue())
5470         return true;
5471       if (!SimpleArgOp.getValue())
5472         return false;
5473       if (!AA::isDynamicallyUnique(A, *this, **SimpleArgOp))
5474         return false;
5475       return unionAssumed(*SimpleArgOp);
5476     };
5477 
5478     // Generate a answer specific to a call site context.
5479     bool Success;
5480     bool AllCallSitesKnown;
5481     if (hasCallBaseContext() &&
5482         getCallBaseContext()->getCalledFunction() == Arg->getParent())
5483       Success = PredForCallSite(
5484           AbstractCallSite(&getCallBaseContext()->getCalledOperandUse()));
5485     else
5486       Success = A.checkForAllCallSites(PredForCallSite, *this, true,
5487                                        AllCallSitesKnown);
5488 
5489     if (!Success)
5490       if (!askSimplifiedValueForOtherAAs(A))
5491         return indicatePessimisticFixpoint();
5492 
5493     // If a candicate was found in this update, return CHANGED.
5494     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5495                                                : ChangeStatus ::CHANGED;
5496   }
5497 
5498   /// See AbstractAttribute::trackStatistics()
5499   void trackStatistics() const override {
5500     STATS_DECLTRACK_ARG_ATTR(value_simplify)
5501   }
5502 };
5503 
5504 struct AAValueSimplifyReturned : AAValueSimplifyImpl {
5505   AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A)
5506       : AAValueSimplifyImpl(IRP, A) {}
5507 
5508   /// See AAValueSimplify::getAssumedSimplifiedValue()
5509   Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
5510     if (!isValidState())
5511       return nullptr;
5512     return SimplifiedAssociatedValue;
5513   }
5514 
5515   /// See AbstractAttribute::updateImpl(...).
5516   ChangeStatus updateImpl(Attributor &A) override {
5517     auto Before = SimplifiedAssociatedValue;
5518 
5519     auto PredForReturned = [&](Value &V) {
5520       return checkAndUpdate(A, *this,
5521                             IRPosition::value(V, getCallBaseContext()));
5522     };
5523 
5524     if (!A.checkForAllReturnedValues(PredForReturned, *this))
5525       if (!askSimplifiedValueForOtherAAs(A))
5526         return indicatePessimisticFixpoint();
5527 
5528     // If a candicate was found in this update, return CHANGED.
5529     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5530                                                : ChangeStatus ::CHANGED;
5531   }
5532 
5533   ChangeStatus manifest(Attributor &A) override {
5534     ChangeStatus Changed = ChangeStatus::UNCHANGED;
5535 
5536     if (auto *NewV = getReplacementValue(A)) {
5537       auto PredForReturned =
5538           [&](Value &, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
5539             for (ReturnInst *RI : RetInsts) {
5540               Value *ReturnedVal = RI->getReturnValue();
5541               if (ReturnedVal == NewV || isa<UndefValue>(ReturnedVal))
5542                 return true;
5543               assert(RI->getFunction() == getAnchorScope() &&
5544                      "ReturnInst in wrong function!");
5545               LLVM_DEBUG(dbgs()
5546                          << "[ValueSimplify] " << *ReturnedVal << " -> "
5547                          << *NewV << " in " << *RI << " :: " << *this << "\n");
5548               if (A.changeUseAfterManifest(RI->getOperandUse(0), *NewV))
5549                 Changed = ChangeStatus::CHANGED;
5550             }
5551             return true;
5552           };
5553       A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this);
5554     }
5555 
5556     return Changed | AAValueSimplify::manifest(A);
5557   }
5558 
5559   /// See AbstractAttribute::trackStatistics()
5560   void trackStatistics() const override {
5561     STATS_DECLTRACK_FNRET_ATTR(value_simplify)
5562   }
5563 };
5564 
5565 struct AAValueSimplifyFloating : AAValueSimplifyImpl {
5566   AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A)
5567       : AAValueSimplifyImpl(IRP, A) {}
5568 
5569   /// See AbstractAttribute::initialize(...).
5570   void initialize(Attributor &A) override {
5571     AAValueSimplifyImpl::initialize(A);
5572     Value &V = getAnchorValue();
5573 
5574     // TODO: add other stuffs
5575     if (isa<Constant>(V))
5576       indicatePessimisticFixpoint();
5577   }
5578 
5579   /// Check if \p Cmp is a comparison we can simplify.
5580   ///
5581   /// We handle multiple cases, one in which at least one operand is an
5582   /// (assumed) nullptr. If so, try to simplify it using AANonNull on the other
5583   /// operand. Return true if successful, in that case SimplifiedAssociatedValue
5584   /// will be updated.
5585   bool handleCmp(Attributor &A, CmpInst &Cmp) {
5586     auto Union = [&](Value &V) {
5587       SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5588           SimplifiedAssociatedValue, &V, V.getType());
5589       return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5590     };
5591 
5592     Value *LHS = Cmp.getOperand(0);
5593     Value *RHS = Cmp.getOperand(1);
5594 
5595     // Simplify the operands first.
5596     bool UsedAssumedInformation = false;
5597     const auto &SimplifiedLHS =
5598         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
5599                                *this, UsedAssumedInformation);
5600     if (!SimplifiedLHS.hasValue())
5601       return true;
5602     if (!SimplifiedLHS.getValue())
5603       return false;
5604     LHS = *SimplifiedLHS;
5605 
5606     const auto &SimplifiedRHS =
5607         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
5608                                *this, UsedAssumedInformation);
5609     if (!SimplifiedRHS.hasValue())
5610       return true;
5611     if (!SimplifiedRHS.getValue())
5612       return false;
5613     RHS = *SimplifiedRHS;
5614 
5615     LLVMContext &Ctx = Cmp.getContext();
5616     // Handle the trivial case first in which we don't even need to think about
5617     // null or non-null.
5618     if (LHS == RHS && (Cmp.isTrueWhenEqual() || Cmp.isFalseWhenEqual())) {
5619       Constant *NewVal =
5620           ConstantInt::get(Type::getInt1Ty(Ctx), Cmp.isTrueWhenEqual());
5621       if (!Union(*NewVal))
5622         return false;
5623       if (!UsedAssumedInformation)
5624         indicateOptimisticFixpoint();
5625       return true;
5626     }
5627 
5628     // From now on we only handle equalities (==, !=).
5629     ICmpInst *ICmp = dyn_cast<ICmpInst>(&Cmp);
5630     if (!ICmp || !ICmp->isEquality())
5631       return false;
5632 
5633     bool LHSIsNull = isa<ConstantPointerNull>(LHS);
5634     bool RHSIsNull = isa<ConstantPointerNull>(RHS);
5635     if (!LHSIsNull && !RHSIsNull)
5636       return false;
5637 
5638     // Left is the nullptr ==/!= non-nullptr case. We'll use AANonNull on the
5639     // non-nullptr operand and if we assume it's non-null we can conclude the
5640     // result of the comparison.
5641     assert((LHSIsNull || RHSIsNull) &&
5642            "Expected nullptr versus non-nullptr comparison at this point");
5643 
5644     // The index is the operand that we assume is not null.
5645     unsigned PtrIdx = LHSIsNull;
5646     auto &PtrNonNullAA = A.getAAFor<AANonNull>(
5647         *this, IRPosition::value(*ICmp->getOperand(PtrIdx)),
5648         DepClassTy::REQUIRED);
5649     if (!PtrNonNullAA.isAssumedNonNull())
5650       return false;
5651     UsedAssumedInformation |= !PtrNonNullAA.isKnownNonNull();
5652 
5653     // The new value depends on the predicate, true for != and false for ==.
5654     Constant *NewVal = ConstantInt::get(
5655         Type::getInt1Ty(Ctx), ICmp->getPredicate() == CmpInst::ICMP_NE);
5656     if (!Union(*NewVal))
5657       return false;
5658 
5659     if (!UsedAssumedInformation)
5660       indicateOptimisticFixpoint();
5661 
5662     return true;
5663   }
5664 
5665   bool updateWithLoad(Attributor &A, LoadInst &L) {
5666     auto Union = [&](Value &V) {
5667       SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5668           SimplifiedAssociatedValue, &V, L.getType());
5669       return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5670     };
5671     return handleLoad(A, *this, L, Union);
5672   }
5673 
5674   /// Use the generic, non-optimistic InstSimplfy functionality if we managed to
5675   /// simplify any operand of the instruction \p I. Return true if successful,
5676   /// in that case SimplifiedAssociatedValue will be updated.
5677   bool handleGenericInst(Attributor &A, Instruction &I) {
5678     bool SomeSimplified = false;
5679     bool UsedAssumedInformation = false;
5680 
5681     SmallVector<Value *, 8> NewOps(I.getNumOperands());
5682     int Idx = 0;
5683     for (Value *Op : I.operands()) {
5684       const auto &SimplifiedOp =
5685           A.getAssumedSimplified(IRPosition::value(*Op, getCallBaseContext()),
5686                                  *this, UsedAssumedInformation);
5687       // If we are not sure about any operand we are not sure about the entire
5688       // instruction, we'll wait.
5689       if (!SimplifiedOp.hasValue())
5690         return true;
5691 
5692       if (SimplifiedOp.getValue())
5693         NewOps[Idx] = SimplifiedOp.getValue();
5694       else
5695         NewOps[Idx] = Op;
5696 
5697       SomeSimplified |= (NewOps[Idx] != Op);
5698       ++Idx;
5699     }
5700 
5701     // We won't bother with the InstSimplify interface if we didn't simplify any
5702     // operand ourselves.
5703     if (!SomeSimplified)
5704       return false;
5705 
5706     InformationCache &InfoCache = A.getInfoCache();
5707     Function *F = I.getFunction();
5708     const auto *DT =
5709         InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
5710     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5711     auto *AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
5712     OptimizationRemarkEmitter *ORE = nullptr;
5713 
5714     const DataLayout &DL = I.getModule()->getDataLayout();
5715     SimplifyQuery Q(DL, TLI, DT, AC, &I);
5716     if (Value *SimplifiedI =
5717             SimplifyInstructionWithOperands(&I, NewOps, Q, ORE)) {
5718       SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5719           SimplifiedAssociatedValue, SimplifiedI, I.getType());
5720       return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5721     }
5722     return false;
5723   }
5724 
5725   /// See AbstractAttribute::updateImpl(...).
5726   ChangeStatus updateImpl(Attributor &A) override {
5727     auto Before = SimplifiedAssociatedValue;
5728 
5729     auto VisitValueCB = [&](Value &V, const Instruction *CtxI, bool &,
5730                             bool Stripped) -> bool {
5731       auto &AA = A.getAAFor<AAValueSimplify>(
5732           *this, IRPosition::value(V, getCallBaseContext()),
5733           DepClassTy::REQUIRED);
5734       if (!Stripped && this == &AA) {
5735 
5736         if (auto *I = dyn_cast<Instruction>(&V)) {
5737           if (auto *LI = dyn_cast<LoadInst>(&V))
5738             if (updateWithLoad(A, *LI))
5739               return true;
5740           if (auto *Cmp = dyn_cast<CmpInst>(&V))
5741             if (handleCmp(A, *Cmp))
5742               return true;
5743           if (handleGenericInst(A, *I))
5744             return true;
5745         }
5746         // TODO: Look the instruction and check recursively.
5747 
5748         LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V
5749                           << "\n");
5750         return false;
5751       }
5752       return checkAndUpdate(A, *this,
5753                             IRPosition::value(V, getCallBaseContext()));
5754     };
5755 
5756     bool Dummy = false;
5757     if (!genericValueTraversal<bool>(A, getIRPosition(), *this, Dummy,
5758                                      VisitValueCB, getCtxI(),
5759                                      /* UseValueSimplify */ false))
5760       if (!askSimplifiedValueForOtherAAs(A))
5761         return indicatePessimisticFixpoint();
5762 
5763     // If a candicate was found in this update, return CHANGED.
5764     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5765                                                : ChangeStatus ::CHANGED;
5766   }
5767 
5768   /// See AbstractAttribute::trackStatistics()
5769   void trackStatistics() const override {
5770     STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
5771   }
5772 };
5773 
5774 struct AAValueSimplifyFunction : AAValueSimplifyImpl {
5775   AAValueSimplifyFunction(const IRPosition &IRP, Attributor &A)
5776       : AAValueSimplifyImpl(IRP, A) {}
5777 
5778   /// See AbstractAttribute::initialize(...).
5779   void initialize(Attributor &A) override {
5780     SimplifiedAssociatedValue = nullptr;
5781     indicateOptimisticFixpoint();
5782   }
5783   /// See AbstractAttribute::initialize(...).
5784   ChangeStatus updateImpl(Attributor &A) override {
5785     llvm_unreachable(
5786         "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
5787   }
5788   /// See AbstractAttribute::trackStatistics()
5789   void trackStatistics() const override {
5790     STATS_DECLTRACK_FN_ATTR(value_simplify)
5791   }
5792 };
5793 
5794 struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
5795   AAValueSimplifyCallSite(const IRPosition &IRP, Attributor &A)
5796       : AAValueSimplifyFunction(IRP, A) {}
5797   /// See AbstractAttribute::trackStatistics()
5798   void trackStatistics() const override {
5799     STATS_DECLTRACK_CS_ATTR(value_simplify)
5800   }
5801 };
5802 
5803 struct AAValueSimplifyCallSiteReturned : AAValueSimplifyImpl {
5804   AAValueSimplifyCallSiteReturned(const IRPosition &IRP, Attributor &A)
5805       : AAValueSimplifyImpl(IRP, A) {}
5806 
5807   void initialize(Attributor &A) override {
5808     AAValueSimplifyImpl::initialize(A);
5809     if (!getAssociatedFunction())
5810       indicatePessimisticFixpoint();
5811   }
5812 
5813   /// See AbstractAttribute::updateImpl(...).
5814   ChangeStatus updateImpl(Attributor &A) override {
5815     auto Before = SimplifiedAssociatedValue;
5816     auto &RetAA = A.getAAFor<AAReturnedValues>(
5817         *this, IRPosition::function(*getAssociatedFunction()),
5818         DepClassTy::REQUIRED);
5819     auto PredForReturned =
5820         [&](Value &RetVal, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
5821           bool UsedAssumedInformation = false;
5822           Optional<Value *> CSRetVal = A.translateArgumentToCallSiteContent(
5823               &RetVal, *cast<CallBase>(getCtxI()), *this,
5824               UsedAssumedInformation);
5825           SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5826               SimplifiedAssociatedValue, CSRetVal, getAssociatedType());
5827           return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5828         };
5829     if (!RetAA.checkForAllReturnedValuesAndReturnInsts(PredForReturned))
5830       if (!askSimplifiedValueForOtherAAs(A))
5831         return indicatePessimisticFixpoint();
5832     return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5833                                                : ChangeStatus ::CHANGED;
5834   }
5835 
5836   void trackStatistics() const override {
5837     STATS_DECLTRACK_CSRET_ATTR(value_simplify)
5838   }
5839 };
5840 
5841 struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
5842   AAValueSimplifyCallSiteArgument(const IRPosition &IRP, Attributor &A)
5843       : AAValueSimplifyFloating(IRP, A) {}
5844 
5845   /// See AbstractAttribute::manifest(...).
5846   ChangeStatus manifest(Attributor &A) override {
5847     ChangeStatus Changed = ChangeStatus::UNCHANGED;
5848 
5849     if (auto *NewV = getReplacementValue(A)) {
5850       Use &U = cast<CallBase>(&getAnchorValue())
5851                    ->getArgOperandUse(getCallSiteArgNo());
5852       if (A.changeUseAfterManifest(U, *NewV))
5853         Changed = ChangeStatus::CHANGED;
5854     }
5855 
5856     return Changed | AAValueSimplify::manifest(A);
5857   }
5858 
5859   void trackStatistics() const override {
5860     STATS_DECLTRACK_CSARG_ATTR(value_simplify)
5861   }
5862 };
5863 
5864 /// ----------------------- Heap-To-Stack Conversion ---------------------------
5865 struct AAHeapToStackFunction final : public AAHeapToStack {
5866 
5867   struct AllocationInfo {
5868     /// The call that allocates the memory.
5869     CallBase *const CB;
5870 
5871     /// The library function id for the allocation.
5872     LibFunc LibraryFunctionId = NotLibFunc;
5873 
5874     /// The status wrt. a rewrite.
5875     enum {
5876       STACK_DUE_TO_USE,
5877       STACK_DUE_TO_FREE,
5878       INVALID,
5879     } Status = STACK_DUE_TO_USE;
5880 
5881     /// Flag to indicate if we encountered a use that might free this allocation
5882     /// but which is not in the deallocation infos.
5883     bool HasPotentiallyFreeingUnknownUses = false;
5884 
5885     /// The set of free calls that use this allocation.
5886     SmallPtrSet<CallBase *, 1> PotentialFreeCalls{};
5887   };
5888 
5889   struct DeallocationInfo {
5890     /// The call that deallocates the memory.
5891     CallBase *const CB;
5892 
5893     /// Flag to indicate if we don't know all objects this deallocation might
5894     /// free.
5895     bool MightFreeUnknownObjects = false;
5896 
5897     /// The set of allocation calls that are potentially freed.
5898     SmallPtrSet<CallBase *, 1> PotentialAllocationCalls{};
5899   };
5900 
5901   AAHeapToStackFunction(const IRPosition &IRP, Attributor &A)
5902       : AAHeapToStack(IRP, A) {}
5903 
5904   ~AAHeapToStackFunction() {
5905     // Ensure we call the destructor so we release any memory allocated in the
5906     // sets.
5907     for (auto &It : AllocationInfos)
5908       It.getSecond()->~AllocationInfo();
5909     for (auto &It : DeallocationInfos)
5910       It.getSecond()->~DeallocationInfo();
5911   }
5912 
5913   void initialize(Attributor &A) override {
5914     AAHeapToStack::initialize(A);
5915 
5916     const Function *F = getAnchorScope();
5917     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5918 
5919     auto AllocationIdentifierCB = [&](Instruction &I) {
5920       CallBase *CB = dyn_cast<CallBase>(&I);
5921       if (!CB)
5922         return true;
5923       if (isFreeCall(CB, TLI)) {
5924         DeallocationInfos[CB] = new (A.Allocator) DeallocationInfo{CB};
5925         return true;
5926       }
5927       // To do heap to stack, we need to know that the allocation itself is
5928       // removable once uses are rewritten, and that we can initialize the
5929       // alloca to the same pattern as the original allocation result.
5930       if (isAllocationFn(CB, TLI) && isAllocRemovable(CB, TLI)) {
5931         auto *I8Ty = Type::getInt8Ty(CB->getParent()->getContext());
5932         if (nullptr != getInitialValueOfAllocation(CB, TLI, I8Ty)) {
5933           AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB};
5934           AllocationInfos[CB] = AI;
5935           TLI->getLibFunc(*CB, AI->LibraryFunctionId);
5936         }
5937       }
5938       return true;
5939     };
5940 
5941     bool UsedAssumedInformation = false;
5942     bool Success = A.checkForAllCallLikeInstructions(
5943         AllocationIdentifierCB, *this, UsedAssumedInformation,
5944         /* CheckBBLivenessOnly */ false,
5945         /* CheckPotentiallyDead */ true);
5946     (void)Success;
5947     assert(Success && "Did not expect the call base visit callback to fail!");
5948   }
5949 
5950   const std::string getAsStr() const override {
5951     unsigned NumH2SMallocs = 0, NumInvalidMallocs = 0;
5952     for (const auto &It : AllocationInfos) {
5953       if (It.second->Status == AllocationInfo::INVALID)
5954         ++NumInvalidMallocs;
5955       else
5956         ++NumH2SMallocs;
5957     }
5958     return "[H2S] Mallocs Good/Bad: " + std::to_string(NumH2SMallocs) + "/" +
5959            std::to_string(NumInvalidMallocs);
5960   }
5961 
5962   /// See AbstractAttribute::trackStatistics().
5963   void trackStatistics() const override {
5964     STATS_DECL(
5965         MallocCalls, Function,
5966         "Number of malloc/calloc/aligned_alloc calls converted to allocas");
5967     for (auto &It : AllocationInfos)
5968       if (It.second->Status != AllocationInfo::INVALID)
5969         ++BUILD_STAT_NAME(MallocCalls, Function);
5970   }
5971 
5972   bool isAssumedHeapToStack(const CallBase &CB) const override {
5973     if (isValidState())
5974       if (AllocationInfo *AI = AllocationInfos.lookup(&CB))
5975         return AI->Status != AllocationInfo::INVALID;
5976     return false;
5977   }
5978 
5979   bool isAssumedHeapToStackRemovedFree(CallBase &CB) const override {
5980     if (!isValidState())
5981       return false;
5982 
5983     for (auto &It : AllocationInfos) {
5984       AllocationInfo &AI = *It.second;
5985       if (AI.Status == AllocationInfo::INVALID)
5986         continue;
5987 
5988       if (AI.PotentialFreeCalls.count(&CB))
5989         return true;
5990     }
5991 
5992     return false;
5993   }
5994 
5995   ChangeStatus manifest(Attributor &A) override {
5996     assert(getState().isValidState() &&
5997            "Attempted to manifest an invalid state!");
5998 
5999     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
6000     Function *F = getAnchorScope();
6001     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6002 
6003     for (auto &It : AllocationInfos) {
6004       AllocationInfo &AI = *It.second;
6005       if (AI.Status == AllocationInfo::INVALID)
6006         continue;
6007 
6008       for (CallBase *FreeCall : AI.PotentialFreeCalls) {
6009         LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
6010         A.deleteAfterManifest(*FreeCall);
6011         HasChanged = ChangeStatus::CHANGED;
6012       }
6013 
6014       LLVM_DEBUG(dbgs() << "H2S: Removing malloc-like call: " << *AI.CB
6015                         << "\n");
6016 
6017       auto Remark = [&](OptimizationRemark OR) {
6018         LibFunc IsAllocShared;
6019         if (TLI->getLibFunc(*AI.CB, IsAllocShared))
6020           if (IsAllocShared == LibFunc___kmpc_alloc_shared)
6021             return OR << "Moving globalized variable to the stack.";
6022         return OR << "Moving memory allocation from the heap to the stack.";
6023       };
6024       if (AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
6025         A.emitRemark<OptimizationRemark>(AI.CB, "OMP110", Remark);
6026       else
6027         A.emitRemark<OptimizationRemark>(AI.CB, "HeapToStack", Remark);
6028 
6029       Value *Size;
6030       Optional<APInt> SizeAPI = getSize(A, *this, AI);
6031       if (SizeAPI.hasValue()) {
6032         Size = ConstantInt::get(AI.CB->getContext(), *SizeAPI);
6033       } else {
6034         LLVMContext &Ctx = AI.CB->getContext();
6035         auto &DL = A.getInfoCache().getDL();
6036         ObjectSizeOpts Opts;
6037         ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, Opts);
6038         SizeOffsetEvalType SizeOffsetPair = Eval.compute(AI.CB);
6039         assert(SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown() &&
6040                cast<ConstantInt>(SizeOffsetPair.second)->isZero());
6041         Size = SizeOffsetPair.first;
6042       }
6043 
6044       Align Alignment(1);
6045       if (MaybeAlign RetAlign = AI.CB->getRetAlign())
6046         Alignment = max(Alignment, RetAlign);
6047       if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
6048         Optional<APInt> AlignmentAPI = getAPInt(A, *this, *Align);
6049         assert(AlignmentAPI.hasValue() &&
6050                "Expected an alignment during manifest!");
6051         Alignment =
6052             max(Alignment, MaybeAlign(AlignmentAPI.getValue().getZExtValue()));
6053       }
6054 
6055       unsigned AS = cast<PointerType>(AI.CB->getType())->getAddressSpace();
6056       Instruction *Alloca =
6057           new AllocaInst(Type::getInt8Ty(F->getContext()), AS, Size, Alignment,
6058                          "", AI.CB->getNextNode());
6059 
6060       if (Alloca->getType() != AI.CB->getType())
6061         Alloca = new BitCastInst(Alloca, AI.CB->getType(), "malloc_bc",
6062                                  Alloca->getNextNode());
6063 
6064       auto *I8Ty = Type::getInt8Ty(F->getContext());
6065       auto *InitVal = getInitialValueOfAllocation(AI.CB, TLI, I8Ty);
6066       assert(InitVal &&
6067              "Must be able to materialize initial memory state of allocation");
6068 
6069       A.changeValueAfterManifest(*AI.CB, *Alloca);
6070 
6071       if (auto *II = dyn_cast<InvokeInst>(AI.CB)) {
6072         auto *NBB = II->getNormalDest();
6073         BranchInst::Create(NBB, AI.CB->getParent());
6074         A.deleteAfterManifest(*AI.CB);
6075       } else {
6076         A.deleteAfterManifest(*AI.CB);
6077       }
6078 
6079       // Initialize the alloca with the same value as used by the allocation
6080       // function.  We can skip undef as the initial value of an alloc is
6081       // undef, and the memset would simply end up being DSEd.
6082       if (!isa<UndefValue>(InitVal)) {
6083         IRBuilder<> Builder(Alloca->getNextNode());
6084         // TODO: Use alignment above if align!=1
6085         Builder.CreateMemSet(Alloca, InitVal, Size, None);
6086       }
6087       HasChanged = ChangeStatus::CHANGED;
6088     }
6089 
6090     return HasChanged;
6091   }
6092 
6093   Optional<APInt> getAPInt(Attributor &A, const AbstractAttribute &AA,
6094                            Value &V) {
6095     bool UsedAssumedInformation = false;
6096     Optional<Constant *> SimpleV =
6097         A.getAssumedConstant(V, AA, UsedAssumedInformation);
6098     if (!SimpleV.hasValue())
6099       return APInt(64, 0);
6100     if (auto *CI = dyn_cast_or_null<ConstantInt>(SimpleV.getValue()))
6101       return CI->getValue();
6102     return llvm::None;
6103   }
6104 
6105   Optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA,
6106                           AllocationInfo &AI) {
6107     auto Mapper = [&](const Value *V) -> const Value * {
6108       bool UsedAssumedInformation = false;
6109       if (Optional<Constant *> SimpleV =
6110               A.getAssumedConstant(*V, AA, UsedAssumedInformation))
6111         if (*SimpleV)
6112           return *SimpleV;
6113       return V;
6114     };
6115 
6116     const Function *F = getAnchorScope();
6117     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6118     return getAllocSize(AI.CB, TLI, Mapper);
6119   }
6120 
6121   /// Collection of all malloc-like calls in a function with associated
6122   /// information.
6123   DenseMap<CallBase *, AllocationInfo *> AllocationInfos;
6124 
6125   /// Collection of all free-like calls in a function with associated
6126   /// information.
6127   DenseMap<CallBase *, DeallocationInfo *> DeallocationInfos;
6128 
6129   ChangeStatus updateImpl(Attributor &A) override;
6130 };
6131 
6132 ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) {
6133   ChangeStatus Changed = ChangeStatus::UNCHANGED;
6134   const Function *F = getAnchorScope();
6135   const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
6136 
6137   const auto &LivenessAA =
6138       A.getAAFor<AAIsDead>(*this, IRPosition::function(*F), DepClassTy::NONE);
6139 
6140   MustBeExecutedContextExplorer &Explorer =
6141       A.getInfoCache().getMustBeExecutedContextExplorer();
6142 
6143   bool StackIsAccessibleByOtherThreads =
6144       A.getInfoCache().stackIsAccessibleByOtherThreads();
6145 
6146   // Flag to ensure we update our deallocation information at most once per
6147   // updateImpl call and only if we use the free check reasoning.
6148   bool HasUpdatedFrees = false;
6149 
6150   auto UpdateFrees = [&]() {
6151     HasUpdatedFrees = true;
6152 
6153     for (auto &It : DeallocationInfos) {
6154       DeallocationInfo &DI = *It.second;
6155       // For now we cannot use deallocations that have unknown inputs, skip
6156       // them.
6157       if (DI.MightFreeUnknownObjects)
6158         continue;
6159 
6160       // No need to analyze dead calls, ignore them instead.
6161       bool UsedAssumedInformation = false;
6162       if (A.isAssumedDead(*DI.CB, this, &LivenessAA, UsedAssumedInformation,
6163                           /* CheckBBLivenessOnly */ true))
6164         continue;
6165 
6166       // Use the optimistic version to get the freed objects, ignoring dead
6167       // branches etc.
6168       SmallVector<Value *, 8> Objects;
6169       if (!AA::getAssumedUnderlyingObjects(A, *DI.CB->getArgOperand(0), Objects,
6170                                            *this, DI.CB)) {
6171         LLVM_DEBUG(
6172             dbgs()
6173             << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n");
6174         DI.MightFreeUnknownObjects = true;
6175         continue;
6176       }
6177 
6178       // Check each object explicitly.
6179       for (auto *Obj : Objects) {
6180         // Free of null and undef can be ignored as no-ops (or UB in the latter
6181         // case).
6182         if (isa<ConstantPointerNull>(Obj) || isa<UndefValue>(Obj))
6183           continue;
6184 
6185         CallBase *ObjCB = dyn_cast<CallBase>(Obj);
6186         if (!ObjCB) {
6187           LLVM_DEBUG(dbgs()
6188                      << "[H2S] Free of a non-call object: " << *Obj << "\n");
6189           DI.MightFreeUnknownObjects = true;
6190           continue;
6191         }
6192 
6193         AllocationInfo *AI = AllocationInfos.lookup(ObjCB);
6194         if (!AI) {
6195           LLVM_DEBUG(dbgs() << "[H2S] Free of a non-allocation object: " << *Obj
6196                             << "\n");
6197           DI.MightFreeUnknownObjects = true;
6198           continue;
6199         }
6200 
6201         DI.PotentialAllocationCalls.insert(ObjCB);
6202       }
6203     }
6204   };
6205 
6206   auto FreeCheck = [&](AllocationInfo &AI) {
6207     // If the stack is not accessible by other threads, the "must-free" logic
6208     // doesn't apply as the pointer could be shared and needs to be places in
6209     // "shareable" memory.
6210     if (!StackIsAccessibleByOtherThreads) {
6211       auto &NoSyncAA =
6212           A.getAAFor<AANoSync>(*this, getIRPosition(), DepClassTy::OPTIONAL);
6213       if (!NoSyncAA.isAssumedNoSync()) {
6214         LLVM_DEBUG(
6215             dbgs() << "[H2S] found an escaping use, stack is not accessible by "
6216                       "other threads and function is not nosync:\n");
6217         return false;
6218       }
6219     }
6220     if (!HasUpdatedFrees)
6221       UpdateFrees();
6222 
6223     // TODO: Allow multi exit functions that have different free calls.
6224     if (AI.PotentialFreeCalls.size() != 1) {
6225       LLVM_DEBUG(dbgs() << "[H2S] did not find one free call but "
6226                         << AI.PotentialFreeCalls.size() << "\n");
6227       return false;
6228     }
6229     CallBase *UniqueFree = *AI.PotentialFreeCalls.begin();
6230     DeallocationInfo *DI = DeallocationInfos.lookup(UniqueFree);
6231     if (!DI) {
6232       LLVM_DEBUG(
6233           dbgs() << "[H2S] unique free call was not known as deallocation call "
6234                  << *UniqueFree << "\n");
6235       return false;
6236     }
6237     if (DI->MightFreeUnknownObjects) {
6238       LLVM_DEBUG(
6239           dbgs() << "[H2S] unique free call might free unknown allocations\n");
6240       return false;
6241     }
6242     if (DI->PotentialAllocationCalls.size() > 1) {
6243       LLVM_DEBUG(dbgs() << "[H2S] unique free call might free "
6244                         << DI->PotentialAllocationCalls.size()
6245                         << " different allocations\n");
6246       return false;
6247     }
6248     if (*DI->PotentialAllocationCalls.begin() != AI.CB) {
6249       LLVM_DEBUG(
6250           dbgs()
6251           << "[H2S] unique free call not known to free this allocation but "
6252           << **DI->PotentialAllocationCalls.begin() << "\n");
6253       return false;
6254     }
6255     Instruction *CtxI = isa<InvokeInst>(AI.CB) ? AI.CB : AI.CB->getNextNode();
6256     if (!Explorer.findInContextOf(UniqueFree, CtxI)) {
6257       LLVM_DEBUG(
6258           dbgs()
6259           << "[H2S] unique free call might not be executed with the allocation "
6260           << *UniqueFree << "\n");
6261       return false;
6262     }
6263     return true;
6264   };
6265 
6266   auto UsesCheck = [&](AllocationInfo &AI) {
6267     bool ValidUsesOnly = true;
6268 
6269     auto Pred = [&](const Use &U, bool &Follow) -> bool {
6270       Instruction *UserI = cast<Instruction>(U.getUser());
6271       if (isa<LoadInst>(UserI))
6272         return true;
6273       if (auto *SI = dyn_cast<StoreInst>(UserI)) {
6274         if (SI->getValueOperand() == U.get()) {
6275           LLVM_DEBUG(dbgs()
6276                      << "[H2S] escaping store to memory: " << *UserI << "\n");
6277           ValidUsesOnly = false;
6278         } else {
6279           // A store into the malloc'ed memory is fine.
6280         }
6281         return true;
6282       }
6283       if (auto *CB = dyn_cast<CallBase>(UserI)) {
6284         if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
6285           return true;
6286         if (DeallocationInfos.count(CB)) {
6287           AI.PotentialFreeCalls.insert(CB);
6288           return true;
6289         }
6290 
6291         unsigned ArgNo = CB->getArgOperandNo(&U);
6292 
6293         const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
6294             *this, IRPosition::callsite_argument(*CB, ArgNo),
6295             DepClassTy::OPTIONAL);
6296 
6297         // If a call site argument use is nofree, we are fine.
6298         const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
6299             *this, IRPosition::callsite_argument(*CB, ArgNo),
6300             DepClassTy::OPTIONAL);
6301 
6302         bool MaybeCaptured = !NoCaptureAA.isAssumedNoCapture();
6303         bool MaybeFreed = !ArgNoFreeAA.isAssumedNoFree();
6304         if (MaybeCaptured ||
6305             (AI.LibraryFunctionId != LibFunc___kmpc_alloc_shared &&
6306              MaybeFreed)) {
6307           AI.HasPotentiallyFreeingUnknownUses |= MaybeFreed;
6308 
6309           // Emit a missed remark if this is missed OpenMP globalization.
6310           auto Remark = [&](OptimizationRemarkMissed ORM) {
6311             return ORM
6312                    << "Could not move globalized variable to the stack. "
6313                       "Variable is potentially captured in call. Mark "
6314                       "parameter as `__attribute__((noescape))` to override.";
6315           };
6316 
6317           if (ValidUsesOnly &&
6318               AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
6319             A.emitRemark<OptimizationRemarkMissed>(AI.CB, "OMP113", Remark);
6320 
6321           LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
6322           ValidUsesOnly = false;
6323         }
6324         return true;
6325       }
6326 
6327       if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
6328           isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
6329         Follow = true;
6330         return true;
6331       }
6332       // Unknown user for which we can not track uses further (in a way that
6333       // makes sense).
6334       LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
6335       ValidUsesOnly = false;
6336       return true;
6337     };
6338     if (!A.checkForAllUses(Pred, *this, *AI.CB))
6339       return false;
6340     return ValidUsesOnly;
6341   };
6342 
6343   // The actual update starts here. We look at all allocations and depending on
6344   // their status perform the appropriate check(s).
6345   for (auto &It : AllocationInfos) {
6346     AllocationInfo &AI = *It.second;
6347     if (AI.Status == AllocationInfo::INVALID)
6348       continue;
6349 
6350     if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
6351       if (!getAPInt(A, *this, *Align)) {
6352         // Can't generate an alloca which respects the required alignment
6353         // on the allocation.
6354         LLVM_DEBUG(dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CB
6355                           << "\n");
6356         AI.Status = AllocationInfo::INVALID;
6357         Changed = ChangeStatus::CHANGED;
6358         continue;
6359       }
6360     }
6361 
6362     if (MaxHeapToStackSize != -1) {
6363       Optional<APInt> Size = getSize(A, *this, AI);
6364       if (!Size.hasValue() || Size.getValue().ugt(MaxHeapToStackSize)) {
6365         LLVM_DEBUG({
6366           if (!Size.hasValue())
6367             dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n";
6368           else
6369             dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. "
6370                    << MaxHeapToStackSize << "\n";
6371         });
6372 
6373         AI.Status = AllocationInfo::INVALID;
6374         Changed = ChangeStatus::CHANGED;
6375         continue;
6376       }
6377     }
6378 
6379     switch (AI.Status) {
6380     case AllocationInfo::STACK_DUE_TO_USE:
6381       if (UsesCheck(AI))
6382         continue;
6383       AI.Status = AllocationInfo::STACK_DUE_TO_FREE;
6384       LLVM_FALLTHROUGH;
6385     case AllocationInfo::STACK_DUE_TO_FREE:
6386       if (FreeCheck(AI))
6387         continue;
6388       AI.Status = AllocationInfo::INVALID;
6389       Changed = ChangeStatus::CHANGED;
6390       continue;
6391     case AllocationInfo::INVALID:
6392       llvm_unreachable("Invalid allocations should never reach this point!");
6393     };
6394   }
6395 
6396   return Changed;
6397 }
6398 
6399 /// ----------------------- Privatizable Pointers ------------------------------
6400 struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
6401   AAPrivatizablePtrImpl(const IRPosition &IRP, Attributor &A)
6402       : AAPrivatizablePtr(IRP, A), PrivatizableType(llvm::None) {}
6403 
6404   ChangeStatus indicatePessimisticFixpoint() override {
6405     AAPrivatizablePtr::indicatePessimisticFixpoint();
6406     PrivatizableType = nullptr;
6407     return ChangeStatus::CHANGED;
6408   }
6409 
6410   /// Identify the type we can chose for a private copy of the underlying
6411   /// argument. None means it is not clear yet, nullptr means there is none.
6412   virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
6413 
6414   /// Return a privatizable type that encloses both T0 and T1.
6415   /// TODO: This is merely a stub for now as we should manage a mapping as well.
6416   Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
6417     if (!T0.hasValue())
6418       return T1;
6419     if (!T1.hasValue())
6420       return T0;
6421     if (T0 == T1)
6422       return T0;
6423     return nullptr;
6424   }
6425 
6426   Optional<Type *> getPrivatizableType() const override {
6427     return PrivatizableType;
6428   }
6429 
6430   const std::string getAsStr() const override {
6431     return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
6432   }
6433 
6434 protected:
6435   Optional<Type *> PrivatizableType;
6436 };
6437 
6438 // TODO: Do this for call site arguments (probably also other values) as well.
6439 
6440 struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
6441   AAPrivatizablePtrArgument(const IRPosition &IRP, Attributor &A)
6442       : AAPrivatizablePtrImpl(IRP, A) {}
6443 
6444   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
6445   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
6446     // If this is a byval argument and we know all the call sites (so we can
6447     // rewrite them), there is no need to check them explicitly.
6448     bool AllCallSitesKnown;
6449     if (getIRPosition().hasAttr(Attribute::ByVal) &&
6450         A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
6451                                true, AllCallSitesKnown))
6452       return getAssociatedValue().getType()->getPointerElementType();
6453 
6454     Optional<Type *> Ty;
6455     unsigned ArgNo = getIRPosition().getCallSiteArgNo();
6456 
6457     // Make sure the associated call site argument has the same type at all call
6458     // sites and it is an allocation we know is safe to privatize, for now that
6459     // means we only allow alloca instructions.
6460     // TODO: We can additionally analyze the accesses in the callee to  create
6461     //       the type from that information instead. That is a little more
6462     //       involved and will be done in a follow up patch.
6463     auto CallSiteCheck = [&](AbstractCallSite ACS) {
6464       IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
6465       // Check if a coresponding argument was found or if it is one not
6466       // associated (which can happen for callback calls).
6467       if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
6468         return false;
6469 
6470       // Check that all call sites agree on a type.
6471       auto &PrivCSArgAA =
6472           A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos, DepClassTy::REQUIRED);
6473       Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
6474 
6475       LLVM_DEBUG({
6476         dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
6477         if (CSTy.hasValue() && CSTy.getValue())
6478           CSTy.getValue()->print(dbgs());
6479         else if (CSTy.hasValue())
6480           dbgs() << "<nullptr>";
6481         else
6482           dbgs() << "<none>";
6483       });
6484 
6485       Ty = combineTypes(Ty, CSTy);
6486 
6487       LLVM_DEBUG({
6488         dbgs() << " : New Type: ";
6489         if (Ty.hasValue() && Ty.getValue())
6490           Ty.getValue()->print(dbgs());
6491         else if (Ty.hasValue())
6492           dbgs() << "<nullptr>";
6493         else
6494           dbgs() << "<none>";
6495         dbgs() << "\n";
6496       });
6497 
6498       return !Ty.hasValue() || Ty.getValue();
6499     };
6500 
6501     if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown))
6502       return nullptr;
6503     return Ty;
6504   }
6505 
6506   /// See AbstractAttribute::updateImpl(...).
6507   ChangeStatus updateImpl(Attributor &A) override {
6508     PrivatizableType = identifyPrivatizableType(A);
6509     if (!PrivatizableType.hasValue())
6510       return ChangeStatus::UNCHANGED;
6511     if (!PrivatizableType.getValue())
6512       return indicatePessimisticFixpoint();
6513 
6514     // The dependence is optional so we don't give up once we give up on the
6515     // alignment.
6516     A.getAAFor<AAAlign>(*this, IRPosition::value(getAssociatedValue()),
6517                         DepClassTy::OPTIONAL);
6518 
6519     // Avoid arguments with padding for now.
6520     if (!getIRPosition().hasAttr(Attribute::ByVal) &&
6521         !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
6522                                                 A.getInfoCache().getDL())) {
6523       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
6524       return indicatePessimisticFixpoint();
6525     }
6526 
6527     // Collect the types that will replace the privatizable type in the function
6528     // signature.
6529     SmallVector<Type *, 16> ReplacementTypes;
6530     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
6531 
6532     // Verify callee and caller agree on how the promoted argument would be
6533     // passed.
6534     Function &Fn = *getIRPosition().getAnchorScope();
6535     const auto *TTI =
6536         A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
6537     if (!TTI) {
6538       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Missing TTI for function "
6539                         << Fn.getName() << "\n");
6540       return indicatePessimisticFixpoint();
6541     }
6542 
6543     auto CallSiteCheck = [&](AbstractCallSite ACS) {
6544       CallBase *CB = ACS.getInstruction();
6545       return TTI->areTypesABICompatible(
6546           CB->getCaller(), CB->getCalledFunction(), ReplacementTypes);
6547     };
6548     bool AllCallSitesKnown;
6549     if (!A.checkForAllCallSites(CallSiteCheck, *this, true,
6550                                 AllCallSitesKnown)) {
6551       LLVM_DEBUG(
6552           dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
6553                  << Fn.getName() << "\n");
6554       return indicatePessimisticFixpoint();
6555     }
6556 
6557     // Register a rewrite of the argument.
6558     Argument *Arg = getAssociatedArgument();
6559     if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
6560       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
6561       return indicatePessimisticFixpoint();
6562     }
6563 
6564     unsigned ArgNo = Arg->getArgNo();
6565 
6566     // Helper to check if for the given call site the associated argument is
6567     // passed to a callback where the privatization would be different.
6568     auto IsCompatiblePrivArgOfCallback = [&](CallBase &CB) {
6569       SmallVector<const Use *, 4> CallbackUses;
6570       AbstractCallSite::getCallbackUses(CB, CallbackUses);
6571       for (const Use *U : CallbackUses) {
6572         AbstractCallSite CBACS(U);
6573         assert(CBACS && CBACS.isCallbackCall());
6574         for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
6575           int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
6576 
6577           LLVM_DEBUG({
6578             dbgs()
6579                 << "[AAPrivatizablePtr] Argument " << *Arg
6580                 << "check if can be privatized in the context of its parent ("
6581                 << Arg->getParent()->getName()
6582                 << ")\n[AAPrivatizablePtr] because it is an argument in a "
6583                    "callback ("
6584                 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
6585                 << ")\n[AAPrivatizablePtr] " << CBArg << " : "
6586                 << CBACS.getCallArgOperand(CBArg) << " vs "
6587                 << CB.getArgOperand(ArgNo) << "\n"
6588                 << "[AAPrivatizablePtr] " << CBArg << " : "
6589                 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
6590           });
6591 
6592           if (CBArgNo != int(ArgNo))
6593             continue;
6594           const auto &CBArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
6595               *this, IRPosition::argument(CBArg), DepClassTy::REQUIRED);
6596           if (CBArgPrivAA.isValidState()) {
6597             auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
6598             if (!CBArgPrivTy.hasValue())
6599               continue;
6600             if (CBArgPrivTy.getValue() == PrivatizableType)
6601               continue;
6602           }
6603 
6604           LLVM_DEBUG({
6605             dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
6606                    << " cannot be privatized in the context of its parent ("
6607                    << Arg->getParent()->getName()
6608                    << ")\n[AAPrivatizablePtr] because it is an argument in a "
6609                       "callback ("
6610                    << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
6611                    << ").\n[AAPrivatizablePtr] for which the argument "
6612                       "privatization is not compatible.\n";
6613           });
6614           return false;
6615         }
6616       }
6617       return true;
6618     };
6619 
6620     // Helper to check if for the given call site the associated argument is
6621     // passed to a direct call where the privatization would be different.
6622     auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
6623       CallBase *DC = cast<CallBase>(ACS.getInstruction());
6624       int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
6625       assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->arg_size() &&
6626              "Expected a direct call operand for callback call operand");
6627 
6628       LLVM_DEBUG({
6629         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
6630                << " check if be privatized in the context of its parent ("
6631                << Arg->getParent()->getName()
6632                << ")\n[AAPrivatizablePtr] because it is an argument in a "
6633                   "direct call of ("
6634                << DCArgNo << "@" << DC->getCalledFunction()->getName()
6635                << ").\n";
6636       });
6637 
6638       Function *DCCallee = DC->getCalledFunction();
6639       if (unsigned(DCArgNo) < DCCallee->arg_size()) {
6640         const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
6641             *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)),
6642             DepClassTy::REQUIRED);
6643         if (DCArgPrivAA.isValidState()) {
6644           auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
6645           if (!DCArgPrivTy.hasValue())
6646             return true;
6647           if (DCArgPrivTy.getValue() == PrivatizableType)
6648             return true;
6649         }
6650       }
6651 
6652       LLVM_DEBUG({
6653         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
6654                << " cannot be privatized in the context of its parent ("
6655                << Arg->getParent()->getName()
6656                << ")\n[AAPrivatizablePtr] because it is an argument in a "
6657                   "direct call of ("
6658                << ACS.getInstruction()->getCalledFunction()->getName()
6659                << ").\n[AAPrivatizablePtr] for which the argument "
6660                   "privatization is not compatible.\n";
6661       });
6662       return false;
6663     };
6664 
6665     // Helper to check if the associated argument is used at the given abstract
6666     // call site in a way that is incompatible with the privatization assumed
6667     // here.
6668     auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
6669       if (ACS.isDirectCall())
6670         return IsCompatiblePrivArgOfCallback(*ACS.getInstruction());
6671       if (ACS.isCallbackCall())
6672         return IsCompatiblePrivArgOfDirectCS(ACS);
6673       return false;
6674     };
6675 
6676     if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
6677                                 AllCallSitesKnown))
6678       return indicatePessimisticFixpoint();
6679 
6680     return ChangeStatus::UNCHANGED;
6681   }
6682 
6683   /// Given a type to private \p PrivType, collect the constituates (which are
6684   /// used) in \p ReplacementTypes.
6685   static void
6686   identifyReplacementTypes(Type *PrivType,
6687                            SmallVectorImpl<Type *> &ReplacementTypes) {
6688     // TODO: For now we expand the privatization type to the fullest which can
6689     //       lead to dead arguments that need to be removed later.
6690     assert(PrivType && "Expected privatizable type!");
6691 
6692     // Traverse the type, extract constituate types on the outermost level.
6693     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6694       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
6695         ReplacementTypes.push_back(PrivStructType->getElementType(u));
6696     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6697       ReplacementTypes.append(PrivArrayType->getNumElements(),
6698                               PrivArrayType->getElementType());
6699     } else {
6700       ReplacementTypes.push_back(PrivType);
6701     }
6702   }
6703 
6704   /// Initialize \p Base according to the type \p PrivType at position \p IP.
6705   /// The values needed are taken from the arguments of \p F starting at
6706   /// position \p ArgNo.
6707   static void createInitialization(Type *PrivType, Value &Base, Function &F,
6708                                    unsigned ArgNo, Instruction &IP) {
6709     assert(PrivType && "Expected privatizable type!");
6710 
6711     IRBuilder<NoFolder> IRB(&IP);
6712     const DataLayout &DL = F.getParent()->getDataLayout();
6713 
6714     // Traverse the type, build GEPs and stores.
6715     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6716       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
6717       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
6718         Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
6719         Value *Ptr =
6720             constructPointer(PointeeTy, PrivType, &Base,
6721                              PrivStructLayout->getElementOffset(u), IRB, DL);
6722         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
6723       }
6724     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6725       Type *PointeeTy = PrivArrayType->getElementType();
6726       Type *PointeePtrTy = PointeeTy->getPointerTo();
6727       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
6728       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
6729         Value *Ptr = constructPointer(PointeePtrTy, PrivType, &Base,
6730                                       u * PointeeTySize, IRB, DL);
6731         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
6732       }
6733     } else {
6734       new StoreInst(F.getArg(ArgNo), &Base, &IP);
6735     }
6736   }
6737 
6738   /// Extract values from \p Base according to the type \p PrivType at the
6739   /// call position \p ACS. The values are appended to \p ReplacementValues.
6740   void createReplacementValues(Align Alignment, Type *PrivType,
6741                                AbstractCallSite ACS, Value *Base,
6742                                SmallVectorImpl<Value *> &ReplacementValues) {
6743     assert(Base && "Expected base value!");
6744     assert(PrivType && "Expected privatizable type!");
6745     Instruction *IP = ACS.getInstruction();
6746 
6747     IRBuilder<NoFolder> IRB(IP);
6748     const DataLayout &DL = IP->getModule()->getDataLayout();
6749 
6750     Type *PrivPtrType = PrivType->getPointerTo();
6751     if (Base->getType() != PrivPtrType)
6752       Base = BitCastInst::CreateBitOrPointerCast(Base, PrivPtrType, "",
6753                                                  ACS.getInstruction());
6754 
6755     // Traverse the type, build GEPs and loads.
6756     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6757       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
6758       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
6759         Type *PointeeTy = PrivStructType->getElementType(u);
6760         Value *Ptr =
6761             constructPointer(PointeeTy->getPointerTo(), PrivType, Base,
6762                              PrivStructLayout->getElementOffset(u), IRB, DL);
6763         LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
6764         L->setAlignment(Alignment);
6765         ReplacementValues.push_back(L);
6766       }
6767     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6768       Type *PointeeTy = PrivArrayType->getElementType();
6769       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
6770       Type *PointeePtrTy = PointeeTy->getPointerTo();
6771       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
6772         Value *Ptr = constructPointer(PointeePtrTy, PrivType, Base,
6773                                       u * PointeeTySize, IRB, DL);
6774         LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
6775         L->setAlignment(Alignment);
6776         ReplacementValues.push_back(L);
6777       }
6778     } else {
6779       LoadInst *L = new LoadInst(PrivType, Base, "", IP);
6780       L->setAlignment(Alignment);
6781       ReplacementValues.push_back(L);
6782     }
6783   }
6784 
6785   /// See AbstractAttribute::manifest(...)
6786   ChangeStatus manifest(Attributor &A) override {
6787     if (!PrivatizableType.hasValue())
6788       return ChangeStatus::UNCHANGED;
6789     assert(PrivatizableType.getValue() && "Expected privatizable type!");
6790 
6791     // Collect all tail calls in the function as we cannot allow new allocas to
6792     // escape into tail recursion.
6793     // TODO: Be smarter about new allocas escaping into tail calls.
6794     SmallVector<CallInst *, 16> TailCalls;
6795     bool UsedAssumedInformation = false;
6796     if (!A.checkForAllInstructions(
6797             [&](Instruction &I) {
6798               CallInst &CI = cast<CallInst>(I);
6799               if (CI.isTailCall())
6800                 TailCalls.push_back(&CI);
6801               return true;
6802             },
6803             *this, {Instruction::Call}, UsedAssumedInformation))
6804       return ChangeStatus::UNCHANGED;
6805 
6806     Argument *Arg = getAssociatedArgument();
6807     // Query AAAlign attribute for alignment of associated argument to
6808     // determine the best alignment of loads.
6809     const auto &AlignAA =
6810         A.getAAFor<AAAlign>(*this, IRPosition::value(*Arg), DepClassTy::NONE);
6811 
6812     // Callback to repair the associated function. A new alloca is placed at the
6813     // beginning and initialized with the values passed through arguments. The
6814     // new alloca replaces the use of the old pointer argument.
6815     Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
6816         [=](const Attributor::ArgumentReplacementInfo &ARI,
6817             Function &ReplacementFn, Function::arg_iterator ArgIt) {
6818           BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
6819           Instruction *IP = &*EntryBB.getFirstInsertionPt();
6820           Instruction *AI = new AllocaInst(PrivatizableType.getValue(), 0,
6821                                            Arg->getName() + ".priv", IP);
6822           createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
6823                                ArgIt->getArgNo(), *IP);
6824 
6825           if (AI->getType() != Arg->getType())
6826             AI =
6827                 BitCastInst::CreateBitOrPointerCast(AI, Arg->getType(), "", IP);
6828           Arg->replaceAllUsesWith(AI);
6829 
6830           for (CallInst *CI : TailCalls)
6831             CI->setTailCall(false);
6832         };
6833 
6834     // Callback to repair a call site of the associated function. The elements
6835     // of the privatizable type are loaded prior to the call and passed to the
6836     // new function version.
6837     Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
6838         [=, &AlignAA](const Attributor::ArgumentReplacementInfo &ARI,
6839                       AbstractCallSite ACS,
6840                       SmallVectorImpl<Value *> &NewArgOperands) {
6841           // When no alignment is specified for the load instruction,
6842           // natural alignment is assumed.
6843           createReplacementValues(
6844               assumeAligned(AlignAA.getAssumedAlign()),
6845               PrivatizableType.getValue(), ACS,
6846               ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
6847               NewArgOperands);
6848         };
6849 
6850     // Collect the types that will replace the privatizable type in the function
6851     // signature.
6852     SmallVector<Type *, 16> ReplacementTypes;
6853     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
6854 
6855     // Register a rewrite of the argument.
6856     if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
6857                                            std::move(FnRepairCB),
6858                                            std::move(ACSRepairCB)))
6859       return ChangeStatus::CHANGED;
6860     return ChangeStatus::UNCHANGED;
6861   }
6862 
6863   /// See AbstractAttribute::trackStatistics()
6864   void trackStatistics() const override {
6865     STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
6866   }
6867 };
6868 
6869 struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
6870   AAPrivatizablePtrFloating(const IRPosition &IRP, Attributor &A)
6871       : AAPrivatizablePtrImpl(IRP, A) {}
6872 
6873   /// See AbstractAttribute::initialize(...).
6874   virtual void initialize(Attributor &A) override {
6875     // TODO: We can privatize more than arguments.
6876     indicatePessimisticFixpoint();
6877   }
6878 
6879   ChangeStatus updateImpl(Attributor &A) override {
6880     llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
6881                      "updateImpl will not be called");
6882   }
6883 
6884   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
6885   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
6886     Value *Obj = getUnderlyingObject(&getAssociatedValue());
6887     if (!Obj) {
6888       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
6889       return nullptr;
6890     }
6891 
6892     if (auto *AI = dyn_cast<AllocaInst>(Obj))
6893       if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
6894         if (CI->isOne())
6895           return AI->getAllocatedType();
6896     if (auto *Arg = dyn_cast<Argument>(Obj)) {
6897       auto &PrivArgAA = A.getAAFor<AAPrivatizablePtr>(
6898           *this, IRPosition::argument(*Arg), DepClassTy::REQUIRED);
6899       if (PrivArgAA.isAssumedPrivatizablePtr())
6900         return Obj->getType()->getPointerElementType();
6901     }
6902 
6903     LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
6904                          "alloca nor privatizable argument: "
6905                       << *Obj << "!\n");
6906     return nullptr;
6907   }
6908 
6909   /// See AbstractAttribute::trackStatistics()
6910   void trackStatistics() const override {
6911     STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
6912   }
6913 };
6914 
6915 struct AAPrivatizablePtrCallSiteArgument final
6916     : public AAPrivatizablePtrFloating {
6917   AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP, Attributor &A)
6918       : AAPrivatizablePtrFloating(IRP, A) {}
6919 
6920   /// See AbstractAttribute::initialize(...).
6921   void initialize(Attributor &A) override {
6922     if (getIRPosition().hasAttr(Attribute::ByVal))
6923       indicateOptimisticFixpoint();
6924   }
6925 
6926   /// See AbstractAttribute::updateImpl(...).
6927   ChangeStatus updateImpl(Attributor &A) override {
6928     PrivatizableType = identifyPrivatizableType(A);
6929     if (!PrivatizableType.hasValue())
6930       return ChangeStatus::UNCHANGED;
6931     if (!PrivatizableType.getValue())
6932       return indicatePessimisticFixpoint();
6933 
6934     const IRPosition &IRP = getIRPosition();
6935     auto &NoCaptureAA =
6936         A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::REQUIRED);
6937     if (!NoCaptureAA.isAssumedNoCapture()) {
6938       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
6939       return indicatePessimisticFixpoint();
6940     }
6941 
6942     auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP, DepClassTy::REQUIRED);
6943     if (!NoAliasAA.isAssumedNoAlias()) {
6944       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
6945       return indicatePessimisticFixpoint();
6946     }
6947 
6948     bool IsKnown;
6949     if (!AA::isAssumedReadOnly(A, IRP, *this, IsKnown)) {
6950       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
6951       return indicatePessimisticFixpoint();
6952     }
6953 
6954     return ChangeStatus::UNCHANGED;
6955   }
6956 
6957   /// See AbstractAttribute::trackStatistics()
6958   void trackStatistics() const override {
6959     STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
6960   }
6961 };
6962 
6963 struct AAPrivatizablePtrCallSiteReturned final
6964     : public AAPrivatizablePtrFloating {
6965   AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP, Attributor &A)
6966       : AAPrivatizablePtrFloating(IRP, A) {}
6967 
6968   /// See AbstractAttribute::initialize(...).
6969   void initialize(Attributor &A) override {
6970     // TODO: We can privatize more than arguments.
6971     indicatePessimisticFixpoint();
6972   }
6973 
6974   /// See AbstractAttribute::trackStatistics()
6975   void trackStatistics() const override {
6976     STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
6977   }
6978 };
6979 
6980 struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
6981   AAPrivatizablePtrReturned(const IRPosition &IRP, Attributor &A)
6982       : AAPrivatizablePtrFloating(IRP, A) {}
6983 
6984   /// See AbstractAttribute::initialize(...).
6985   void initialize(Attributor &A) override {
6986     // TODO: We can privatize more than arguments.
6987     indicatePessimisticFixpoint();
6988   }
6989 
6990   /// See AbstractAttribute::trackStatistics()
6991   void trackStatistics() const override {
6992     STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
6993   }
6994 };
6995 
6996 /// -------------------- Memory Behavior Attributes ----------------------------
6997 /// Includes read-none, read-only, and write-only.
6998 /// ----------------------------------------------------------------------------
6999 struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
7000   AAMemoryBehaviorImpl(const IRPosition &IRP, Attributor &A)
7001       : AAMemoryBehavior(IRP, A) {}
7002 
7003   /// See AbstractAttribute::initialize(...).
7004   void initialize(Attributor &A) override {
7005     intersectAssumedBits(BEST_STATE);
7006     getKnownStateFromValue(getIRPosition(), getState());
7007     AAMemoryBehavior::initialize(A);
7008   }
7009 
7010   /// Return the memory behavior information encoded in the IR for \p IRP.
7011   static void getKnownStateFromValue(const IRPosition &IRP,
7012                                      BitIntegerState &State,
7013                                      bool IgnoreSubsumingPositions = false) {
7014     SmallVector<Attribute, 2> Attrs;
7015     IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
7016     for (const Attribute &Attr : Attrs) {
7017       switch (Attr.getKindAsEnum()) {
7018       case Attribute::ReadNone:
7019         State.addKnownBits(NO_ACCESSES);
7020         break;
7021       case Attribute::ReadOnly:
7022         State.addKnownBits(NO_WRITES);
7023         break;
7024       case Attribute::WriteOnly:
7025         State.addKnownBits(NO_READS);
7026         break;
7027       default:
7028         llvm_unreachable("Unexpected attribute!");
7029       }
7030     }
7031 
7032     if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
7033       if (!I->mayReadFromMemory())
7034         State.addKnownBits(NO_READS);
7035       if (!I->mayWriteToMemory())
7036         State.addKnownBits(NO_WRITES);
7037     }
7038   }
7039 
7040   /// See AbstractAttribute::getDeducedAttributes(...).
7041   void getDeducedAttributes(LLVMContext &Ctx,
7042                             SmallVectorImpl<Attribute> &Attrs) const override {
7043     assert(Attrs.size() == 0);
7044     if (isAssumedReadNone())
7045       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
7046     else if (isAssumedReadOnly())
7047       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
7048     else if (isAssumedWriteOnly())
7049       Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
7050     assert(Attrs.size() <= 1);
7051   }
7052 
7053   /// See AbstractAttribute::manifest(...).
7054   ChangeStatus manifest(Attributor &A) override {
7055     if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true))
7056       return ChangeStatus::UNCHANGED;
7057 
7058     const IRPosition &IRP = getIRPosition();
7059 
7060     // Check if we would improve the existing attributes first.
7061     SmallVector<Attribute, 4> DeducedAttrs;
7062     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
7063     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
7064           return IRP.hasAttr(Attr.getKindAsEnum(),
7065                              /* IgnoreSubsumingPositions */ true);
7066         }))
7067       return ChangeStatus::UNCHANGED;
7068 
7069     // Clear existing attributes.
7070     IRP.removeAttrs(AttrKinds);
7071 
7072     // Use the generic manifest method.
7073     return IRAttribute::manifest(A);
7074   }
7075 
7076   /// See AbstractState::getAsStr().
7077   const std::string getAsStr() const override {
7078     if (isAssumedReadNone())
7079       return "readnone";
7080     if (isAssumedReadOnly())
7081       return "readonly";
7082     if (isAssumedWriteOnly())
7083       return "writeonly";
7084     return "may-read/write";
7085   }
7086 
7087   /// The set of IR attributes AAMemoryBehavior deals with.
7088   static const Attribute::AttrKind AttrKinds[3];
7089 };
7090 
7091 const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
7092     Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
7093 
7094 /// Memory behavior attribute for a floating value.
7095 struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
7096   AAMemoryBehaviorFloating(const IRPosition &IRP, Attributor &A)
7097       : AAMemoryBehaviorImpl(IRP, A) {}
7098 
7099   /// See AbstractAttribute::updateImpl(...).
7100   ChangeStatus updateImpl(Attributor &A) override;
7101 
7102   /// See AbstractAttribute::trackStatistics()
7103   void trackStatistics() const override {
7104     if (isAssumedReadNone())
7105       STATS_DECLTRACK_FLOATING_ATTR(readnone)
7106     else if (isAssumedReadOnly())
7107       STATS_DECLTRACK_FLOATING_ATTR(readonly)
7108     else if (isAssumedWriteOnly())
7109       STATS_DECLTRACK_FLOATING_ATTR(writeonly)
7110   }
7111 
7112 private:
7113   /// Return true if users of \p UserI might access the underlying
7114   /// variable/location described by \p U and should therefore be analyzed.
7115   bool followUsersOfUseIn(Attributor &A, const Use &U,
7116                           const Instruction *UserI);
7117 
7118   /// Update the state according to the effect of use \p U in \p UserI.
7119   void analyzeUseIn(Attributor &A, const Use &U, const Instruction *UserI);
7120 };
7121 
7122 /// Memory behavior attribute for function argument.
7123 struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
7124   AAMemoryBehaviorArgument(const IRPosition &IRP, Attributor &A)
7125       : AAMemoryBehaviorFloating(IRP, A) {}
7126 
7127   /// See AbstractAttribute::initialize(...).
7128   void initialize(Attributor &A) override {
7129     intersectAssumedBits(BEST_STATE);
7130     const IRPosition &IRP = getIRPosition();
7131     // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
7132     // can query it when we use has/getAttr. That would allow us to reuse the
7133     // initialize of the base class here.
7134     bool HasByVal =
7135         IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
7136     getKnownStateFromValue(IRP, getState(),
7137                            /* IgnoreSubsumingPositions */ HasByVal);
7138 
7139     // Initialize the use vector with all direct uses of the associated value.
7140     Argument *Arg = getAssociatedArgument();
7141     if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent())))
7142       indicatePessimisticFixpoint();
7143   }
7144 
7145   ChangeStatus manifest(Attributor &A) override {
7146     // TODO: Pointer arguments are not supported on vectors of pointers yet.
7147     if (!getAssociatedValue().getType()->isPointerTy())
7148       return ChangeStatus::UNCHANGED;
7149 
7150     // TODO: From readattrs.ll: "inalloca parameters are always
7151     //                           considered written"
7152     if (hasAttr({Attribute::InAlloca, Attribute::Preallocated})) {
7153       removeKnownBits(NO_WRITES);
7154       removeAssumedBits(NO_WRITES);
7155     }
7156     return AAMemoryBehaviorFloating::manifest(A);
7157   }
7158 
7159   /// See AbstractAttribute::trackStatistics()
7160   void trackStatistics() const override {
7161     if (isAssumedReadNone())
7162       STATS_DECLTRACK_ARG_ATTR(readnone)
7163     else if (isAssumedReadOnly())
7164       STATS_DECLTRACK_ARG_ATTR(readonly)
7165     else if (isAssumedWriteOnly())
7166       STATS_DECLTRACK_ARG_ATTR(writeonly)
7167   }
7168 };
7169 
7170 struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
7171   AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP, Attributor &A)
7172       : AAMemoryBehaviorArgument(IRP, A) {}
7173 
7174   /// See AbstractAttribute::initialize(...).
7175   void initialize(Attributor &A) override {
7176     // If we don't have an associated attribute this is either a variadic call
7177     // or an indirect call, either way, nothing to do here.
7178     Argument *Arg = getAssociatedArgument();
7179     if (!Arg) {
7180       indicatePessimisticFixpoint();
7181       return;
7182     }
7183     if (Arg->hasByValAttr()) {
7184       addKnownBits(NO_WRITES);
7185       removeKnownBits(NO_READS);
7186       removeAssumedBits(NO_READS);
7187     }
7188     AAMemoryBehaviorArgument::initialize(A);
7189     if (getAssociatedFunction()->isDeclaration())
7190       indicatePessimisticFixpoint();
7191   }
7192 
7193   /// See AbstractAttribute::updateImpl(...).
7194   ChangeStatus updateImpl(Attributor &A) override {
7195     // TODO: Once we have call site specific value information we can provide
7196     //       call site specific liveness liveness information and then it makes
7197     //       sense to specialize attributes for call sites arguments instead of
7198     //       redirecting requests to the callee argument.
7199     Argument *Arg = getAssociatedArgument();
7200     const IRPosition &ArgPos = IRPosition::argument(*Arg);
7201     auto &ArgAA =
7202         A.getAAFor<AAMemoryBehavior>(*this, ArgPos, DepClassTy::REQUIRED);
7203     return clampStateAndIndicateChange(getState(), ArgAA.getState());
7204   }
7205 
7206   /// See AbstractAttribute::trackStatistics()
7207   void trackStatistics() const override {
7208     if (isAssumedReadNone())
7209       STATS_DECLTRACK_CSARG_ATTR(readnone)
7210     else if (isAssumedReadOnly())
7211       STATS_DECLTRACK_CSARG_ATTR(readonly)
7212     else if (isAssumedWriteOnly())
7213       STATS_DECLTRACK_CSARG_ATTR(writeonly)
7214   }
7215 };
7216 
7217 /// Memory behavior attribute for a call site return position.
7218 struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
7219   AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP, Attributor &A)
7220       : AAMemoryBehaviorFloating(IRP, A) {}
7221 
7222   /// See AbstractAttribute::initialize(...).
7223   void initialize(Attributor &A) override {
7224     AAMemoryBehaviorImpl::initialize(A);
7225     Function *F = getAssociatedFunction();
7226     if (!F || F->isDeclaration())
7227       indicatePessimisticFixpoint();
7228   }
7229 
7230   /// See AbstractAttribute::manifest(...).
7231   ChangeStatus manifest(Attributor &A) override {
7232     // We do not annotate returned values.
7233     return ChangeStatus::UNCHANGED;
7234   }
7235 
7236   /// See AbstractAttribute::trackStatistics()
7237   void trackStatistics() const override {}
7238 };
7239 
7240 /// An AA to represent the memory behavior function attributes.
7241 struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
7242   AAMemoryBehaviorFunction(const IRPosition &IRP, Attributor &A)
7243       : AAMemoryBehaviorImpl(IRP, A) {}
7244 
7245   /// See AbstractAttribute::updateImpl(Attributor &A).
7246   virtual ChangeStatus updateImpl(Attributor &A) override;
7247 
7248   /// See AbstractAttribute::manifest(...).
7249   ChangeStatus manifest(Attributor &A) override {
7250     Function &F = cast<Function>(getAnchorValue());
7251     if (isAssumedReadNone()) {
7252       F.removeFnAttr(Attribute::ArgMemOnly);
7253       F.removeFnAttr(Attribute::InaccessibleMemOnly);
7254       F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
7255     }
7256     return AAMemoryBehaviorImpl::manifest(A);
7257   }
7258 
7259   /// See AbstractAttribute::trackStatistics()
7260   void trackStatistics() const override {
7261     if (isAssumedReadNone())
7262       STATS_DECLTRACK_FN_ATTR(readnone)
7263     else if (isAssumedReadOnly())
7264       STATS_DECLTRACK_FN_ATTR(readonly)
7265     else if (isAssumedWriteOnly())
7266       STATS_DECLTRACK_FN_ATTR(writeonly)
7267   }
7268 };
7269 
7270 /// AAMemoryBehavior attribute for call sites.
7271 struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
7272   AAMemoryBehaviorCallSite(const IRPosition &IRP, Attributor &A)
7273       : AAMemoryBehaviorImpl(IRP, A) {}
7274 
7275   /// See AbstractAttribute::initialize(...).
7276   void initialize(Attributor &A) override {
7277     AAMemoryBehaviorImpl::initialize(A);
7278     Function *F = getAssociatedFunction();
7279     if (!F || F->isDeclaration())
7280       indicatePessimisticFixpoint();
7281   }
7282 
7283   /// See AbstractAttribute::updateImpl(...).
7284   ChangeStatus updateImpl(Attributor &A) override {
7285     // TODO: Once we have call site specific value information we can provide
7286     //       call site specific liveness liveness information and then it makes
7287     //       sense to specialize attributes for call sites arguments instead of
7288     //       redirecting requests to the callee argument.
7289     Function *F = getAssociatedFunction();
7290     const IRPosition &FnPos = IRPosition::function(*F);
7291     auto &FnAA =
7292         A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::REQUIRED);
7293     return clampStateAndIndicateChange(getState(), FnAA.getState());
7294   }
7295 
7296   /// See AbstractAttribute::trackStatistics()
7297   void trackStatistics() const override {
7298     if (isAssumedReadNone())
7299       STATS_DECLTRACK_CS_ATTR(readnone)
7300     else if (isAssumedReadOnly())
7301       STATS_DECLTRACK_CS_ATTR(readonly)
7302     else if (isAssumedWriteOnly())
7303       STATS_DECLTRACK_CS_ATTR(writeonly)
7304   }
7305 };
7306 
7307 ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
7308 
7309   // The current assumed state used to determine a change.
7310   auto AssumedState = getAssumed();
7311 
7312   auto CheckRWInst = [&](Instruction &I) {
7313     // If the instruction has an own memory behavior state, use it to restrict
7314     // the local state. No further analysis is required as the other memory
7315     // state is as optimistic as it gets.
7316     if (const auto *CB = dyn_cast<CallBase>(&I)) {
7317       const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
7318           *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
7319       intersectAssumedBits(MemBehaviorAA.getAssumed());
7320       return !isAtFixpoint();
7321     }
7322 
7323     // Remove access kind modifiers if necessary.
7324     if (I.mayReadFromMemory())
7325       removeAssumedBits(NO_READS);
7326     if (I.mayWriteToMemory())
7327       removeAssumedBits(NO_WRITES);
7328     return !isAtFixpoint();
7329   };
7330 
7331   bool UsedAssumedInformation = false;
7332   if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
7333                                           UsedAssumedInformation))
7334     return indicatePessimisticFixpoint();
7335 
7336   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7337                                         : ChangeStatus::UNCHANGED;
7338 }
7339 
7340 ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
7341 
7342   const IRPosition &IRP = getIRPosition();
7343   const IRPosition &FnPos = IRPosition::function_scope(IRP);
7344   AAMemoryBehavior::StateType &S = getState();
7345 
7346   // First, check the function scope. We take the known information and we avoid
7347   // work if the assumed information implies the current assumed information for
7348   // this attribute. This is a valid for all but byval arguments.
7349   Argument *Arg = IRP.getAssociatedArgument();
7350   AAMemoryBehavior::base_t FnMemAssumedState =
7351       AAMemoryBehavior::StateType::getWorstState();
7352   if (!Arg || !Arg->hasByValAttr()) {
7353     const auto &FnMemAA =
7354         A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::OPTIONAL);
7355     FnMemAssumedState = FnMemAA.getAssumed();
7356     S.addKnownBits(FnMemAA.getKnown());
7357     if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
7358       return ChangeStatus::UNCHANGED;
7359   }
7360 
7361   // The current assumed state used to determine a change.
7362   auto AssumedState = S.getAssumed();
7363 
7364   // Make sure the value is not captured (except through "return"), if
7365   // it is, any information derived would be irrelevant anyway as we cannot
7366   // check the potential aliases introduced by the capture. However, no need
7367   // to fall back to anythign less optimistic than the function state.
7368   const auto &ArgNoCaptureAA =
7369       A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::OPTIONAL);
7370   if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
7371     S.intersectAssumedBits(FnMemAssumedState);
7372     return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7373                                           : ChangeStatus::UNCHANGED;
7374   }
7375 
7376   // Visit and expand uses until all are analyzed or a fixpoint is reached.
7377   auto UsePred = [&](const Use &U, bool &Follow) -> bool {
7378     Instruction *UserI = cast<Instruction>(U.getUser());
7379     LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserI
7380                       << " \n");
7381 
7382     // Droppable users, e.g., llvm::assume does not actually perform any action.
7383     if (UserI->isDroppable())
7384       return true;
7385 
7386     // Check if the users of UserI should also be visited.
7387     Follow = followUsersOfUseIn(A, U, UserI);
7388 
7389     // If UserI might touch memory we analyze the use in detail.
7390     if (UserI->mayReadOrWriteMemory())
7391       analyzeUseIn(A, U, UserI);
7392 
7393     return !isAtFixpoint();
7394   };
7395 
7396   if (!A.checkForAllUses(UsePred, *this, getAssociatedValue()))
7397     return indicatePessimisticFixpoint();
7398 
7399   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7400                                         : ChangeStatus::UNCHANGED;
7401 }
7402 
7403 bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use &U,
7404                                                   const Instruction *UserI) {
7405   // The loaded value is unrelated to the pointer argument, no need to
7406   // follow the users of the load.
7407   if (isa<LoadInst>(UserI))
7408     return false;
7409 
7410   // By default we follow all uses assuming UserI might leak information on U,
7411   // we have special handling for call sites operands though.
7412   const auto *CB = dyn_cast<CallBase>(UserI);
7413   if (!CB || !CB->isArgOperand(&U))
7414     return true;
7415 
7416   // If the use is a call argument known not to be captured, the users of
7417   // the call do not need to be visited because they have to be unrelated to
7418   // the input. Note that this check is not trivial even though we disallow
7419   // general capturing of the underlying argument. The reason is that the
7420   // call might the argument "through return", which we allow and for which we
7421   // need to check call users.
7422   if (U.get()->getType()->isPointerTy()) {
7423     unsigned ArgNo = CB->getArgOperandNo(&U);
7424     const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
7425         *this, IRPosition::callsite_argument(*CB, ArgNo), DepClassTy::OPTIONAL);
7426     return !ArgNoCaptureAA.isAssumedNoCapture();
7427   }
7428 
7429   return true;
7430 }
7431 
7432 void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use &U,
7433                                             const Instruction *UserI) {
7434   assert(UserI->mayReadOrWriteMemory());
7435 
7436   switch (UserI->getOpcode()) {
7437   default:
7438     // TODO: Handle all atomics and other side-effect operations we know of.
7439     break;
7440   case Instruction::Load:
7441     // Loads cause the NO_READS property to disappear.
7442     removeAssumedBits(NO_READS);
7443     return;
7444 
7445   case Instruction::Store:
7446     // Stores cause the NO_WRITES property to disappear if the use is the
7447     // pointer operand. Note that while capturing was taken care of somewhere
7448     // else we need to deal with stores of the value that is not looked through.
7449     if (cast<StoreInst>(UserI)->getPointerOperand() == U.get())
7450       removeAssumedBits(NO_WRITES);
7451     else
7452       indicatePessimisticFixpoint();
7453     return;
7454 
7455   case Instruction::Call:
7456   case Instruction::CallBr:
7457   case Instruction::Invoke: {
7458     // For call sites we look at the argument memory behavior attribute (this
7459     // could be recursive!) in order to restrict our own state.
7460     const auto *CB = cast<CallBase>(UserI);
7461 
7462     // Give up on operand bundles.
7463     if (CB->isBundleOperand(&U)) {
7464       indicatePessimisticFixpoint();
7465       return;
7466     }
7467 
7468     // Calling a function does read the function pointer, maybe write it if the
7469     // function is self-modifying.
7470     if (CB->isCallee(&U)) {
7471       removeAssumedBits(NO_READS);
7472       break;
7473     }
7474 
7475     // Adjust the possible access behavior based on the information on the
7476     // argument.
7477     IRPosition Pos;
7478     if (U.get()->getType()->isPointerTy())
7479       Pos = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
7480     else
7481       Pos = IRPosition::callsite_function(*CB);
7482     const auto &MemBehaviorAA =
7483         A.getAAFor<AAMemoryBehavior>(*this, Pos, DepClassTy::OPTIONAL);
7484     // "assumed" has at most the same bits as the MemBehaviorAA assumed
7485     // and at least "known".
7486     intersectAssumedBits(MemBehaviorAA.getAssumed());
7487     return;
7488   }
7489   };
7490 
7491   // Generally, look at the "may-properties" and adjust the assumed state if we
7492   // did not trigger special handling before.
7493   if (UserI->mayReadFromMemory())
7494     removeAssumedBits(NO_READS);
7495   if (UserI->mayWriteToMemory())
7496     removeAssumedBits(NO_WRITES);
7497 }
7498 
7499 /// -------------------- Memory Locations Attributes ---------------------------
7500 /// Includes read-none, argmemonly, inaccessiblememonly,
7501 /// inaccessiblememorargmemonly
7502 /// ----------------------------------------------------------------------------
7503 
7504 std::string AAMemoryLocation::getMemoryLocationsAsStr(
7505     AAMemoryLocation::MemoryLocationsKind MLK) {
7506   if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS))
7507     return "all memory";
7508   if (MLK == AAMemoryLocation::NO_LOCATIONS)
7509     return "no memory";
7510   std::string S = "memory:";
7511   if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM))
7512     S += "stack,";
7513   if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM))
7514     S += "constant,";
7515   if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM))
7516     S += "internal global,";
7517   if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM))
7518     S += "external global,";
7519   if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM))
7520     S += "argument,";
7521   if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM))
7522     S += "inaccessible,";
7523   if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM))
7524     S += "malloced,";
7525   if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM))
7526     S += "unknown,";
7527   S.pop_back();
7528   return S;
7529 }
7530 
7531 struct AAMemoryLocationImpl : public AAMemoryLocation {
7532 
7533   AAMemoryLocationImpl(const IRPosition &IRP, Attributor &A)
7534       : AAMemoryLocation(IRP, A), Allocator(A.Allocator) {
7535     for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
7536       AccessKind2Accesses[u] = nullptr;
7537   }
7538 
7539   ~AAMemoryLocationImpl() {
7540     // The AccessSets are allocated via a BumpPtrAllocator, we call
7541     // the destructor manually.
7542     for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
7543       if (AccessKind2Accesses[u])
7544         AccessKind2Accesses[u]->~AccessSet();
7545   }
7546 
7547   /// See AbstractAttribute::initialize(...).
7548   void initialize(Attributor &A) override {
7549     intersectAssumedBits(BEST_STATE);
7550     getKnownStateFromValue(A, getIRPosition(), getState());
7551     AAMemoryLocation::initialize(A);
7552   }
7553 
7554   /// Return the memory behavior information encoded in the IR for \p IRP.
7555   static void getKnownStateFromValue(Attributor &A, const IRPosition &IRP,
7556                                      BitIntegerState &State,
7557                                      bool IgnoreSubsumingPositions = false) {
7558     // For internal functions we ignore `argmemonly` and
7559     // `inaccessiblememorargmemonly` as we might break it via interprocedural
7560     // constant propagation. It is unclear if this is the best way but it is
7561     // unlikely this will cause real performance problems. If we are deriving
7562     // attributes for the anchor function we even remove the attribute in
7563     // addition to ignoring it.
7564     bool UseArgMemOnly = true;
7565     Function *AnchorFn = IRP.getAnchorScope();
7566     if (AnchorFn && A.isRunOn(*AnchorFn))
7567       UseArgMemOnly = !AnchorFn->hasLocalLinkage();
7568 
7569     SmallVector<Attribute, 2> Attrs;
7570     IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
7571     for (const Attribute &Attr : Attrs) {
7572       switch (Attr.getKindAsEnum()) {
7573       case Attribute::ReadNone:
7574         State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM);
7575         break;
7576       case Attribute::InaccessibleMemOnly:
7577         State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
7578         break;
7579       case Attribute::ArgMemOnly:
7580         if (UseArgMemOnly)
7581           State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true));
7582         else
7583           IRP.removeAttrs({Attribute::ArgMemOnly});
7584         break;
7585       case Attribute::InaccessibleMemOrArgMemOnly:
7586         if (UseArgMemOnly)
7587           State.addKnownBits(inverseLocation(
7588               NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
7589         else
7590           IRP.removeAttrs({Attribute::InaccessibleMemOrArgMemOnly});
7591         break;
7592       default:
7593         llvm_unreachable("Unexpected attribute!");
7594       }
7595     }
7596   }
7597 
7598   /// See AbstractAttribute::getDeducedAttributes(...).
7599   void getDeducedAttributes(LLVMContext &Ctx,
7600                             SmallVectorImpl<Attribute> &Attrs) const override {
7601     assert(Attrs.size() == 0);
7602     if (isAssumedReadNone()) {
7603       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
7604     } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) {
7605       if (isAssumedInaccessibleMemOnly())
7606         Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly));
7607       else if (isAssumedArgMemOnly())
7608         Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly));
7609       else if (isAssumedInaccessibleOrArgMemOnly())
7610         Attrs.push_back(
7611             Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly));
7612     }
7613     assert(Attrs.size() <= 1);
7614   }
7615 
7616   /// See AbstractAttribute::manifest(...).
7617   ChangeStatus manifest(Attributor &A) override {
7618     const IRPosition &IRP = getIRPosition();
7619 
7620     // Check if we would improve the existing attributes first.
7621     SmallVector<Attribute, 4> DeducedAttrs;
7622     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
7623     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
7624           return IRP.hasAttr(Attr.getKindAsEnum(),
7625                              /* IgnoreSubsumingPositions */ true);
7626         }))
7627       return ChangeStatus::UNCHANGED;
7628 
7629     // Clear existing attributes.
7630     IRP.removeAttrs(AttrKinds);
7631     if (isAssumedReadNone())
7632       IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds);
7633 
7634     // Use the generic manifest method.
7635     return IRAttribute::manifest(A);
7636   }
7637 
7638   /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...).
7639   bool checkForAllAccessesToMemoryKind(
7640       function_ref<bool(const Instruction *, const Value *, AccessKind,
7641                         MemoryLocationsKind)>
7642           Pred,
7643       MemoryLocationsKind RequestedMLK) const override {
7644     if (!isValidState())
7645       return false;
7646 
7647     MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation();
7648     if (AssumedMLK == NO_LOCATIONS)
7649       return true;
7650 
7651     unsigned Idx = 0;
7652     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS;
7653          CurMLK *= 2, ++Idx) {
7654       if (CurMLK & RequestedMLK)
7655         continue;
7656 
7657       if (const AccessSet *Accesses = AccessKind2Accesses[Idx])
7658         for (const AccessInfo &AI : *Accesses)
7659           if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK))
7660             return false;
7661     }
7662 
7663     return true;
7664   }
7665 
7666   ChangeStatus indicatePessimisticFixpoint() override {
7667     // If we give up and indicate a pessimistic fixpoint this instruction will
7668     // become an access for all potential access kinds:
7669     // TODO: Add pointers for argmemonly and globals to improve the results of
7670     //       checkForAllAccessesToMemoryKind.
7671     bool Changed = false;
7672     MemoryLocationsKind KnownMLK = getKnown();
7673     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
7674     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
7675       if (!(CurMLK & KnownMLK))
7676         updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed,
7677                                   getAccessKindFromInst(I));
7678     return AAMemoryLocation::indicatePessimisticFixpoint();
7679   }
7680 
7681 protected:
7682   /// Helper struct to tie together an instruction that has a read or write
7683   /// effect with the pointer it accesses (if any).
7684   struct AccessInfo {
7685 
7686     /// The instruction that caused the access.
7687     const Instruction *I;
7688 
7689     /// The base pointer that is accessed, or null if unknown.
7690     const Value *Ptr;
7691 
7692     /// The kind of access (read/write/read+write).
7693     AccessKind Kind;
7694 
7695     bool operator==(const AccessInfo &RHS) const {
7696       return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind;
7697     }
7698     bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const {
7699       if (LHS.I != RHS.I)
7700         return LHS.I < RHS.I;
7701       if (LHS.Ptr != RHS.Ptr)
7702         return LHS.Ptr < RHS.Ptr;
7703       if (LHS.Kind != RHS.Kind)
7704         return LHS.Kind < RHS.Kind;
7705       return false;
7706     }
7707   };
7708 
7709   /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the
7710   /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind.
7711   using AccessSet = SmallSet<AccessInfo, 2, AccessInfo>;
7712   AccessSet *AccessKind2Accesses[llvm::CTLog2<VALID_STATE>()];
7713 
7714   /// Categorize the pointer arguments of CB that might access memory in
7715   /// AccessedLoc and update the state and access map accordingly.
7716   void
7717   categorizeArgumentPointerLocations(Attributor &A, CallBase &CB,
7718                                      AAMemoryLocation::StateType &AccessedLocs,
7719                                      bool &Changed);
7720 
7721   /// Return the kind(s) of location that may be accessed by \p V.
7722   AAMemoryLocation::MemoryLocationsKind
7723   categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
7724 
7725   /// Return the access kind as determined by \p I.
7726   AccessKind getAccessKindFromInst(const Instruction *I) {
7727     AccessKind AK = READ_WRITE;
7728     if (I) {
7729       AK = I->mayReadFromMemory() ? READ : NONE;
7730       AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE));
7731     }
7732     return AK;
7733   }
7734 
7735   /// Update the state \p State and the AccessKind2Accesses given that \p I is
7736   /// an access of kind \p AK to a \p MLK memory location with the access
7737   /// pointer \p Ptr.
7738   void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
7739                                  MemoryLocationsKind MLK, const Instruction *I,
7740                                  const Value *Ptr, bool &Changed,
7741                                  AccessKind AK = READ_WRITE) {
7742 
7743     assert(isPowerOf2_32(MLK) && "Expected a single location set!");
7744     auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)];
7745     if (!Accesses)
7746       Accesses = new (Allocator) AccessSet();
7747     Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second;
7748     State.removeAssumedBits(MLK);
7749   }
7750 
7751   /// Determine the underlying locations kinds for \p Ptr, e.g., globals or
7752   /// arguments, and update the state and access map accordingly.
7753   void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr,
7754                           AAMemoryLocation::StateType &State, bool &Changed);
7755 
7756   /// Used to allocate access sets.
7757   BumpPtrAllocator &Allocator;
7758 
7759   /// The set of IR attributes AAMemoryLocation deals with.
7760   static const Attribute::AttrKind AttrKinds[4];
7761 };
7762 
7763 const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = {
7764     Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly,
7765     Attribute::InaccessibleMemOrArgMemOnly};
7766 
7767 void AAMemoryLocationImpl::categorizePtrValue(
7768     Attributor &A, const Instruction &I, const Value &Ptr,
7769     AAMemoryLocation::StateType &State, bool &Changed) {
7770   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "
7771                     << Ptr << " ["
7772                     << getMemoryLocationsAsStr(State.getAssumed()) << "]\n");
7773 
7774   SmallVector<Value *, 8> Objects;
7775   if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, *this, &I,
7776                                        /* Intraprocedural */ true)) {
7777     LLVM_DEBUG(
7778         dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n");
7779     updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed,
7780                               getAccessKindFromInst(&I));
7781     return;
7782   }
7783 
7784   for (Value *Obj : Objects) {
7785     // TODO: recognize the TBAA used for constant accesses.
7786     MemoryLocationsKind MLK = NO_LOCATIONS;
7787     if (isa<UndefValue>(Obj))
7788       continue;
7789     if (isa<Argument>(Obj)) {
7790       // TODO: For now we do not treat byval arguments as local copies performed
7791       // on the call edge, though, we should. To make that happen we need to
7792       // teach various passes, e.g., DSE, about the copy effect of a byval. That
7793       // would also allow us to mark functions only accessing byval arguments as
7794       // readnone again, atguably their acceses have no effect outside of the
7795       // function, like accesses to allocas.
7796       MLK = NO_ARGUMENT_MEM;
7797     } else if (auto *GV = dyn_cast<GlobalValue>(Obj)) {
7798       // Reading constant memory is not treated as a read "effect" by the
7799       // function attr pass so we won't neither. Constants defined by TBAA are
7800       // similar. (We know we do not write it because it is constant.)
7801       if (auto *GVar = dyn_cast<GlobalVariable>(GV))
7802         if (GVar->isConstant())
7803           continue;
7804 
7805       if (GV->hasLocalLinkage())
7806         MLK = NO_GLOBAL_INTERNAL_MEM;
7807       else
7808         MLK = NO_GLOBAL_EXTERNAL_MEM;
7809     } else if (isa<ConstantPointerNull>(Obj) &&
7810                !NullPointerIsDefined(getAssociatedFunction(),
7811                                      Ptr.getType()->getPointerAddressSpace())) {
7812       continue;
7813     } else if (isa<AllocaInst>(Obj)) {
7814       MLK = NO_LOCAL_MEM;
7815     } else if (const auto *CB = dyn_cast<CallBase>(Obj)) {
7816       const auto &NoAliasAA = A.getAAFor<AANoAlias>(
7817           *this, IRPosition::callsite_returned(*CB), DepClassTy::OPTIONAL);
7818       if (NoAliasAA.isAssumedNoAlias())
7819         MLK = NO_MALLOCED_MEM;
7820       else
7821         MLK = NO_UNKOWN_MEM;
7822     } else {
7823       MLK = NO_UNKOWN_MEM;
7824     }
7825 
7826     assert(MLK != NO_LOCATIONS && "No location specified!");
7827     LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value can be categorized: "
7828                       << *Obj << " -> " << getMemoryLocationsAsStr(MLK)
7829                       << "\n");
7830     updateStateAndAccessesMap(getState(), MLK, &I, Obj, Changed,
7831                               getAccessKindFromInst(&I));
7832   }
7833 
7834   LLVM_DEBUG(
7835       dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: "
7836              << getMemoryLocationsAsStr(State.getAssumed()) << "\n");
7837 }
7838 
7839 void AAMemoryLocationImpl::categorizeArgumentPointerLocations(
7840     Attributor &A, CallBase &CB, AAMemoryLocation::StateType &AccessedLocs,
7841     bool &Changed) {
7842   for (unsigned ArgNo = 0, E = CB.arg_size(); ArgNo < E; ++ArgNo) {
7843 
7844     // Skip non-pointer arguments.
7845     const Value *ArgOp = CB.getArgOperand(ArgNo);
7846     if (!ArgOp->getType()->isPtrOrPtrVectorTy())
7847       continue;
7848 
7849     // Skip readnone arguments.
7850     const IRPosition &ArgOpIRP = IRPosition::callsite_argument(CB, ArgNo);
7851     const auto &ArgOpMemLocationAA =
7852         A.getAAFor<AAMemoryBehavior>(*this, ArgOpIRP, DepClassTy::OPTIONAL);
7853 
7854     if (ArgOpMemLocationAA.isAssumedReadNone())
7855       continue;
7856 
7857     // Categorize potentially accessed pointer arguments as if there was an
7858     // access instruction with them as pointer.
7859     categorizePtrValue(A, CB, *ArgOp, AccessedLocs, Changed);
7860   }
7861 }
7862 
7863 AAMemoryLocation::MemoryLocationsKind
7864 AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
7865                                                   bool &Changed) {
7866   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "
7867                     << I << "\n");
7868 
7869   AAMemoryLocation::StateType AccessedLocs;
7870   AccessedLocs.intersectAssumedBits(NO_LOCATIONS);
7871 
7872   if (auto *CB = dyn_cast<CallBase>(&I)) {
7873 
7874     // First check if we assume any memory is access is visible.
7875     const auto &CBMemLocationAA = A.getAAFor<AAMemoryLocation>(
7876         *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
7877     LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I
7878                       << " [" << CBMemLocationAA << "]\n");
7879 
7880     if (CBMemLocationAA.isAssumedReadNone())
7881       return NO_LOCATIONS;
7882 
7883     if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) {
7884       updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr,
7885                                 Changed, getAccessKindFromInst(&I));
7886       return AccessedLocs.getAssumed();
7887     }
7888 
7889     uint32_t CBAssumedNotAccessedLocs =
7890         CBMemLocationAA.getAssumedNotAccessedLocation();
7891 
7892     // Set the argmemonly and global bit as we handle them separately below.
7893     uint32_t CBAssumedNotAccessedLocsNoArgMem =
7894         CBAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM;
7895 
7896     for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
7897       if (CBAssumedNotAccessedLocsNoArgMem & CurMLK)
7898         continue;
7899       updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed,
7900                                 getAccessKindFromInst(&I));
7901     }
7902 
7903     // Now handle global memory if it might be accessed. This is slightly tricky
7904     // as NO_GLOBAL_MEM has multiple bits set.
7905     bool HasGlobalAccesses = ((~CBAssumedNotAccessedLocs) & NO_GLOBAL_MEM);
7906     if (HasGlobalAccesses) {
7907       auto AccessPred = [&](const Instruction *, const Value *Ptr,
7908                             AccessKind Kind, MemoryLocationsKind MLK) {
7909         updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed,
7910                                   getAccessKindFromInst(&I));
7911         return true;
7912       };
7913       if (!CBMemLocationAA.checkForAllAccessesToMemoryKind(
7914               AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false)))
7915         return AccessedLocs.getWorstState();
7916     }
7917 
7918     LLVM_DEBUG(
7919         dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "
7920                << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
7921 
7922     // Now handle argument memory if it might be accessed.
7923     bool HasArgAccesses = ((~CBAssumedNotAccessedLocs) & NO_ARGUMENT_MEM);
7924     if (HasArgAccesses)
7925       categorizeArgumentPointerLocations(A, *CB, AccessedLocs, Changed);
7926 
7927     LLVM_DEBUG(
7928         dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "
7929                << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
7930 
7931     return AccessedLocs.getAssumed();
7932   }
7933 
7934   if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) {
7935     LLVM_DEBUG(
7936         dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "
7937                << I << " [" << *Ptr << "]\n");
7938     categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed);
7939     return AccessedLocs.getAssumed();
7940   }
7941 
7942   LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "
7943                     << I << "\n");
7944   updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed,
7945                             getAccessKindFromInst(&I));
7946   return AccessedLocs.getAssumed();
7947 }
7948 
7949 /// An AA to represent the memory behavior function attributes.
7950 struct AAMemoryLocationFunction final : public AAMemoryLocationImpl {
7951   AAMemoryLocationFunction(const IRPosition &IRP, Attributor &A)
7952       : AAMemoryLocationImpl(IRP, A) {}
7953 
7954   /// See AbstractAttribute::updateImpl(Attributor &A).
7955   virtual ChangeStatus updateImpl(Attributor &A) override {
7956 
7957     const auto &MemBehaviorAA =
7958         A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
7959     if (MemBehaviorAA.isAssumedReadNone()) {
7960       if (MemBehaviorAA.isKnownReadNone())
7961         return indicateOptimisticFixpoint();
7962       assert(isAssumedReadNone() &&
7963              "AAMemoryLocation was not read-none but AAMemoryBehavior was!");
7964       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
7965       return ChangeStatus::UNCHANGED;
7966     }
7967 
7968     // The current assumed state used to determine a change.
7969     auto AssumedState = getAssumed();
7970     bool Changed = false;
7971 
7972     auto CheckRWInst = [&](Instruction &I) {
7973       MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed);
7974       LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I
7975                         << ": " << getMemoryLocationsAsStr(MLK) << "\n");
7976       removeAssumedBits(inverseLocation(MLK, false, false));
7977       // Stop once only the valid bit set in the *not assumed location*, thus
7978       // once we don't actually exclude any memory locations in the state.
7979       return getAssumedNotAccessedLocation() != VALID_STATE;
7980     };
7981 
7982     bool UsedAssumedInformation = false;
7983     if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
7984                                             UsedAssumedInformation))
7985       return indicatePessimisticFixpoint();
7986 
7987     Changed |= AssumedState != getAssumed();
7988     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
7989   }
7990 
7991   /// See AbstractAttribute::trackStatistics()
7992   void trackStatistics() const override {
7993     if (isAssumedReadNone())
7994       STATS_DECLTRACK_FN_ATTR(readnone)
7995     else if (isAssumedArgMemOnly())
7996       STATS_DECLTRACK_FN_ATTR(argmemonly)
7997     else if (isAssumedInaccessibleMemOnly())
7998       STATS_DECLTRACK_FN_ATTR(inaccessiblememonly)
7999     else if (isAssumedInaccessibleOrArgMemOnly())
8000       STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly)
8001   }
8002 };
8003 
8004 /// AAMemoryLocation attribute for call sites.
8005 struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
8006   AAMemoryLocationCallSite(const IRPosition &IRP, Attributor &A)
8007       : AAMemoryLocationImpl(IRP, A) {}
8008 
8009   /// See AbstractAttribute::initialize(...).
8010   void initialize(Attributor &A) override {
8011     AAMemoryLocationImpl::initialize(A);
8012     Function *F = getAssociatedFunction();
8013     if (!F || F->isDeclaration())
8014       indicatePessimisticFixpoint();
8015   }
8016 
8017   /// See AbstractAttribute::updateImpl(...).
8018   ChangeStatus updateImpl(Attributor &A) override {
8019     // TODO: Once we have call site specific value information we can provide
8020     //       call site specific liveness liveness information and then it makes
8021     //       sense to specialize attributes for call sites arguments instead of
8022     //       redirecting requests to the callee argument.
8023     Function *F = getAssociatedFunction();
8024     const IRPosition &FnPos = IRPosition::function(*F);
8025     auto &FnAA =
8026         A.getAAFor<AAMemoryLocation>(*this, FnPos, DepClassTy::REQUIRED);
8027     bool Changed = false;
8028     auto AccessPred = [&](const Instruction *I, const Value *Ptr,
8029                           AccessKind Kind, MemoryLocationsKind MLK) {
8030       updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed,
8031                                 getAccessKindFromInst(I));
8032       return true;
8033     };
8034     if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))
8035       return indicatePessimisticFixpoint();
8036     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
8037   }
8038 
8039   /// See AbstractAttribute::trackStatistics()
8040   void trackStatistics() const override {
8041     if (isAssumedReadNone())
8042       STATS_DECLTRACK_CS_ATTR(readnone)
8043   }
8044 };
8045 
8046 /// ------------------ Value Constant Range Attribute -------------------------
8047 
8048 struct AAValueConstantRangeImpl : AAValueConstantRange {
8049   using StateType = IntegerRangeState;
8050   AAValueConstantRangeImpl(const IRPosition &IRP, Attributor &A)
8051       : AAValueConstantRange(IRP, A) {}
8052 
8053   /// See AbstractAttribute::initialize(..).
8054   void initialize(Attributor &A) override {
8055     if (A.hasSimplificationCallback(getIRPosition())) {
8056       indicatePessimisticFixpoint();
8057       return;
8058     }
8059 
8060     // Intersect a range given by SCEV.
8061     intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
8062 
8063     // Intersect a range given by LVI.
8064     intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
8065   }
8066 
8067   /// See AbstractAttribute::getAsStr().
8068   const std::string getAsStr() const override {
8069     std::string Str;
8070     llvm::raw_string_ostream OS(Str);
8071     OS << "range(" << getBitWidth() << ")<";
8072     getKnown().print(OS);
8073     OS << " / ";
8074     getAssumed().print(OS);
8075     OS << ">";
8076     return OS.str();
8077   }
8078 
8079   /// Helper function to get a SCEV expr for the associated value at program
8080   /// point \p I.
8081   const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
8082     if (!getAnchorScope())
8083       return nullptr;
8084 
8085     ScalarEvolution *SE =
8086         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
8087             *getAnchorScope());
8088 
8089     LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
8090         *getAnchorScope());
8091 
8092     if (!SE || !LI)
8093       return nullptr;
8094 
8095     const SCEV *S = SE->getSCEV(&getAssociatedValue());
8096     if (!I)
8097       return S;
8098 
8099     return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
8100   }
8101 
8102   /// Helper function to get a range from SCEV for the associated value at
8103   /// program point \p I.
8104   ConstantRange getConstantRangeFromSCEV(Attributor &A,
8105                                          const Instruction *I = nullptr) const {
8106     if (!getAnchorScope())
8107       return getWorstState(getBitWidth());
8108 
8109     ScalarEvolution *SE =
8110         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
8111             *getAnchorScope());
8112 
8113     const SCEV *S = getSCEV(A, I);
8114     if (!SE || !S)
8115       return getWorstState(getBitWidth());
8116 
8117     return SE->getUnsignedRange(S);
8118   }
8119 
8120   /// Helper function to get a range from LVI for the associated value at
8121   /// program point \p I.
8122   ConstantRange
8123   getConstantRangeFromLVI(Attributor &A,
8124                           const Instruction *CtxI = nullptr) const {
8125     if (!getAnchorScope())
8126       return getWorstState(getBitWidth());
8127 
8128     LazyValueInfo *LVI =
8129         A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
8130             *getAnchorScope());
8131 
8132     if (!LVI || !CtxI)
8133       return getWorstState(getBitWidth());
8134     return LVI->getConstantRange(&getAssociatedValue(),
8135                                  const_cast<Instruction *>(CtxI));
8136   }
8137 
8138   /// Return true if \p CtxI is valid for querying outside analyses.
8139   /// This basically makes sure we do not ask intra-procedural analysis
8140   /// about a context in the wrong function or a context that violates
8141   /// dominance assumptions they might have. The \p AllowAACtxI flag indicates
8142   /// if the original context of this AA is OK or should be considered invalid.
8143   bool isValidCtxInstructionForOutsideAnalysis(Attributor &A,
8144                                                const Instruction *CtxI,
8145                                                bool AllowAACtxI) const {
8146     if (!CtxI || (!AllowAACtxI && CtxI == getCtxI()))
8147       return false;
8148 
8149     // Our context might be in a different function, neither intra-procedural
8150     // analysis (ScalarEvolution nor LazyValueInfo) can handle that.
8151     if (!AA::isValidInScope(getAssociatedValue(), CtxI->getFunction()))
8152       return false;
8153 
8154     // If the context is not dominated by the value there are paths to the
8155     // context that do not define the value. This cannot be handled by
8156     // LazyValueInfo so we need to bail.
8157     if (auto *I = dyn_cast<Instruction>(&getAssociatedValue())) {
8158       InformationCache &InfoCache = A.getInfoCache();
8159       const DominatorTree *DT =
8160           InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
8161               *I->getFunction());
8162       return DT && DT->dominates(I, CtxI);
8163     }
8164 
8165     return true;
8166   }
8167 
8168   /// See AAValueConstantRange::getKnownConstantRange(..).
8169   ConstantRange
8170   getKnownConstantRange(Attributor &A,
8171                         const Instruction *CtxI = nullptr) const override {
8172     if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
8173                                                  /* AllowAACtxI */ false))
8174       return getKnown();
8175 
8176     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
8177     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
8178     return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
8179   }
8180 
8181   /// See AAValueConstantRange::getAssumedConstantRange(..).
8182   ConstantRange
8183   getAssumedConstantRange(Attributor &A,
8184                           const Instruction *CtxI = nullptr) const override {
8185     // TODO: Make SCEV use Attributor assumption.
8186     //       We may be able to bound a variable range via assumptions in
8187     //       Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
8188     //       evolve to x^2 + x, then we can say that y is in [2, 12].
8189     if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
8190                                                  /* AllowAACtxI */ false))
8191       return getAssumed();
8192 
8193     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
8194     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
8195     return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
8196   }
8197 
8198   /// Helper function to create MDNode for range metadata.
8199   static MDNode *
8200   getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
8201                             const ConstantRange &AssumedConstantRange) {
8202     Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
8203                                   Ty, AssumedConstantRange.getLower())),
8204                               ConstantAsMetadata::get(ConstantInt::get(
8205                                   Ty, AssumedConstantRange.getUpper()))};
8206     return MDNode::get(Ctx, LowAndHigh);
8207   }
8208 
8209   /// Return true if \p Assumed is included in \p KnownRanges.
8210   static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
8211 
8212     if (Assumed.isFullSet())
8213       return false;
8214 
8215     if (!KnownRanges)
8216       return true;
8217 
8218     // If multiple ranges are annotated in IR, we give up to annotate assumed
8219     // range for now.
8220 
8221     // TODO:  If there exists a known range which containts assumed range, we
8222     // can say assumed range is better.
8223     if (KnownRanges->getNumOperands() > 2)
8224       return false;
8225 
8226     ConstantInt *Lower =
8227         mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
8228     ConstantInt *Upper =
8229         mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
8230 
8231     ConstantRange Known(Lower->getValue(), Upper->getValue());
8232     return Known.contains(Assumed) && Known != Assumed;
8233   }
8234 
8235   /// Helper function to set range metadata.
8236   static bool
8237   setRangeMetadataIfisBetterRange(Instruction *I,
8238                                   const ConstantRange &AssumedConstantRange) {
8239     auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
8240     if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
8241       if (!AssumedConstantRange.isEmptySet()) {
8242         I->setMetadata(LLVMContext::MD_range,
8243                        getMDNodeForConstantRange(I->getType(), I->getContext(),
8244                                                  AssumedConstantRange));
8245         return true;
8246       }
8247     }
8248     return false;
8249   }
8250 
8251   /// See AbstractAttribute::manifest()
8252   ChangeStatus manifest(Attributor &A) override {
8253     ChangeStatus Changed = ChangeStatus::UNCHANGED;
8254     ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
8255     assert(!AssumedConstantRange.isFullSet() && "Invalid state");
8256 
8257     auto &V = getAssociatedValue();
8258     if (!AssumedConstantRange.isEmptySet() &&
8259         !AssumedConstantRange.isSingleElement()) {
8260       if (Instruction *I = dyn_cast<Instruction>(&V)) {
8261         assert(I == getCtxI() && "Should not annotate an instruction which is "
8262                                  "not the context instruction");
8263         if (isa<CallInst>(I) || isa<LoadInst>(I))
8264           if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
8265             Changed = ChangeStatus::CHANGED;
8266       }
8267     }
8268 
8269     return Changed;
8270   }
8271 };
8272 
8273 struct AAValueConstantRangeArgument final
8274     : AAArgumentFromCallSiteArguments<
8275           AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
8276           true /* BridgeCallBaseContext */> {
8277   using Base = AAArgumentFromCallSiteArguments<
8278       AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
8279       true /* BridgeCallBaseContext */>;
8280   AAValueConstantRangeArgument(const IRPosition &IRP, Attributor &A)
8281       : Base(IRP, A) {}
8282 
8283   /// See AbstractAttribute::initialize(..).
8284   void initialize(Attributor &A) override {
8285     if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
8286       indicatePessimisticFixpoint();
8287     } else {
8288       Base::initialize(A);
8289     }
8290   }
8291 
8292   /// See AbstractAttribute::trackStatistics()
8293   void trackStatistics() const override {
8294     STATS_DECLTRACK_ARG_ATTR(value_range)
8295   }
8296 };
8297 
8298 struct AAValueConstantRangeReturned
8299     : AAReturnedFromReturnedValues<AAValueConstantRange,
8300                                    AAValueConstantRangeImpl,
8301                                    AAValueConstantRangeImpl::StateType,
8302                                    /* PropogateCallBaseContext */ true> {
8303   using Base =
8304       AAReturnedFromReturnedValues<AAValueConstantRange,
8305                                    AAValueConstantRangeImpl,
8306                                    AAValueConstantRangeImpl::StateType,
8307                                    /* PropogateCallBaseContext */ true>;
8308   AAValueConstantRangeReturned(const IRPosition &IRP, Attributor &A)
8309       : Base(IRP, A) {}
8310 
8311   /// See AbstractAttribute::initialize(...).
8312   void initialize(Attributor &A) override {}
8313 
8314   /// See AbstractAttribute::trackStatistics()
8315   void trackStatistics() const override {
8316     STATS_DECLTRACK_FNRET_ATTR(value_range)
8317   }
8318 };
8319 
8320 struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
8321   AAValueConstantRangeFloating(const IRPosition &IRP, Attributor &A)
8322       : AAValueConstantRangeImpl(IRP, A) {}
8323 
8324   /// See AbstractAttribute::initialize(...).
8325   void initialize(Attributor &A) override {
8326     AAValueConstantRangeImpl::initialize(A);
8327     if (isAtFixpoint())
8328       return;
8329 
8330     Value &V = getAssociatedValue();
8331 
8332     if (auto *C = dyn_cast<ConstantInt>(&V)) {
8333       unionAssumed(ConstantRange(C->getValue()));
8334       indicateOptimisticFixpoint();
8335       return;
8336     }
8337 
8338     if (isa<UndefValue>(&V)) {
8339       // Collapse the undef state to 0.
8340       unionAssumed(ConstantRange(APInt(getBitWidth(), 0)));
8341       indicateOptimisticFixpoint();
8342       return;
8343     }
8344 
8345     if (isa<CallBase>(&V))
8346       return;
8347 
8348     if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V))
8349       return;
8350 
8351     // If it is a load instruction with range metadata, use it.
8352     if (LoadInst *LI = dyn_cast<LoadInst>(&V))
8353       if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
8354         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
8355         return;
8356       }
8357 
8358     // We can work with PHI and select instruction as we traverse their operands
8359     // during update.
8360     if (isa<SelectInst>(V) || isa<PHINode>(V))
8361       return;
8362 
8363     // Otherwise we give up.
8364     indicatePessimisticFixpoint();
8365 
8366     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
8367                       << getAssociatedValue() << "\n");
8368   }
8369 
8370   bool calculateBinaryOperator(
8371       Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
8372       const Instruction *CtxI,
8373       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8374     Value *LHS = BinOp->getOperand(0);
8375     Value *RHS = BinOp->getOperand(1);
8376 
8377     // Simplify the operands first.
8378     bool UsedAssumedInformation = false;
8379     const auto &SimplifiedLHS =
8380         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8381                                *this, UsedAssumedInformation);
8382     if (!SimplifiedLHS.hasValue())
8383       return true;
8384     if (!SimplifiedLHS.getValue())
8385       return false;
8386     LHS = *SimplifiedLHS;
8387 
8388     const auto &SimplifiedRHS =
8389         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8390                                *this, UsedAssumedInformation);
8391     if (!SimplifiedRHS.hasValue())
8392       return true;
8393     if (!SimplifiedRHS.getValue())
8394       return false;
8395     RHS = *SimplifiedRHS;
8396 
8397     // TODO: Allow non integers as well.
8398     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8399       return false;
8400 
8401     auto &LHSAA = A.getAAFor<AAValueConstantRange>(
8402         *this, IRPosition::value(*LHS, getCallBaseContext()),
8403         DepClassTy::REQUIRED);
8404     QuerriedAAs.push_back(&LHSAA);
8405     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
8406 
8407     auto &RHSAA = A.getAAFor<AAValueConstantRange>(
8408         *this, IRPosition::value(*RHS, getCallBaseContext()),
8409         DepClassTy::REQUIRED);
8410     QuerriedAAs.push_back(&RHSAA);
8411     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
8412 
8413     auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
8414 
8415     T.unionAssumed(AssumedRange);
8416 
8417     // TODO: Track a known state too.
8418 
8419     return T.isValidState();
8420   }
8421 
8422   bool calculateCastInst(
8423       Attributor &A, CastInst *CastI, IntegerRangeState &T,
8424       const Instruction *CtxI,
8425       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8426     assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
8427     // TODO: Allow non integers as well.
8428     Value *OpV = CastI->getOperand(0);
8429 
8430     // Simplify the operand first.
8431     bool UsedAssumedInformation = false;
8432     const auto &SimplifiedOpV =
8433         A.getAssumedSimplified(IRPosition::value(*OpV, getCallBaseContext()),
8434                                *this, UsedAssumedInformation);
8435     if (!SimplifiedOpV.hasValue())
8436       return true;
8437     if (!SimplifiedOpV.getValue())
8438       return false;
8439     OpV = *SimplifiedOpV;
8440 
8441     if (!OpV->getType()->isIntegerTy())
8442       return false;
8443 
8444     auto &OpAA = A.getAAFor<AAValueConstantRange>(
8445         *this, IRPosition::value(*OpV, getCallBaseContext()),
8446         DepClassTy::REQUIRED);
8447     QuerriedAAs.push_back(&OpAA);
8448     T.unionAssumed(
8449         OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
8450     return T.isValidState();
8451   }
8452 
8453   bool
8454   calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
8455                    const Instruction *CtxI,
8456                    SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8457     Value *LHS = CmpI->getOperand(0);
8458     Value *RHS = CmpI->getOperand(1);
8459 
8460     // Simplify the operands first.
8461     bool UsedAssumedInformation = false;
8462     const auto &SimplifiedLHS =
8463         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8464                                *this, UsedAssumedInformation);
8465     if (!SimplifiedLHS.hasValue())
8466       return true;
8467     if (!SimplifiedLHS.getValue())
8468       return false;
8469     LHS = *SimplifiedLHS;
8470 
8471     const auto &SimplifiedRHS =
8472         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8473                                *this, UsedAssumedInformation);
8474     if (!SimplifiedRHS.hasValue())
8475       return true;
8476     if (!SimplifiedRHS.getValue())
8477       return false;
8478     RHS = *SimplifiedRHS;
8479 
8480     // TODO: Allow non integers as well.
8481     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8482       return false;
8483 
8484     auto &LHSAA = A.getAAFor<AAValueConstantRange>(
8485         *this, IRPosition::value(*LHS, getCallBaseContext()),
8486         DepClassTy::REQUIRED);
8487     QuerriedAAs.push_back(&LHSAA);
8488     auto &RHSAA = A.getAAFor<AAValueConstantRange>(
8489         *this, IRPosition::value(*RHS, getCallBaseContext()),
8490         DepClassTy::REQUIRED);
8491     QuerriedAAs.push_back(&RHSAA);
8492     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
8493     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
8494 
8495     // If one of them is empty set, we can't decide.
8496     if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
8497       return true;
8498 
8499     bool MustTrue = false, MustFalse = false;
8500 
8501     auto AllowedRegion =
8502         ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
8503 
8504     if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
8505       MustFalse = true;
8506 
8507     if (LHSAARange.icmp(CmpI->getPredicate(), RHSAARange))
8508       MustTrue = true;
8509 
8510     assert((!MustTrue || !MustFalse) &&
8511            "Either MustTrue or MustFalse should be false!");
8512 
8513     if (MustTrue)
8514       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
8515     else if (MustFalse)
8516       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
8517     else
8518       T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
8519 
8520     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
8521                       << " " << RHSAA << "\n");
8522 
8523     // TODO: Track a known state too.
8524     return T.isValidState();
8525   }
8526 
8527   /// See AbstractAttribute::updateImpl(...).
8528   ChangeStatus updateImpl(Attributor &A) override {
8529     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
8530                             IntegerRangeState &T, bool Stripped) -> bool {
8531       Instruction *I = dyn_cast<Instruction>(&V);
8532       if (!I || isa<CallBase>(I)) {
8533 
8534         // Simplify the operand first.
8535         bool UsedAssumedInformation = false;
8536         const auto &SimplifiedOpV =
8537             A.getAssumedSimplified(IRPosition::value(V, getCallBaseContext()),
8538                                    *this, UsedAssumedInformation);
8539         if (!SimplifiedOpV.hasValue())
8540           return true;
8541         if (!SimplifiedOpV.getValue())
8542           return false;
8543         Value *VPtr = *SimplifiedOpV;
8544 
8545         // If the value is not instruction, we query AA to Attributor.
8546         const auto &AA = A.getAAFor<AAValueConstantRange>(
8547             *this, IRPosition::value(*VPtr, getCallBaseContext()),
8548             DepClassTy::REQUIRED);
8549 
8550         // Clamp operator is not used to utilize a program point CtxI.
8551         T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
8552 
8553         return T.isValidState();
8554       }
8555 
8556       SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
8557       if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
8558         if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
8559           return false;
8560       } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
8561         if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
8562           return false;
8563       } else if (auto *CastI = dyn_cast<CastInst>(I)) {
8564         if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
8565           return false;
8566       } else {
8567         // Give up with other instructions.
8568         // TODO: Add other instructions
8569 
8570         T.indicatePessimisticFixpoint();
8571         return false;
8572       }
8573 
8574       // Catch circular reasoning in a pessimistic way for now.
8575       // TODO: Check how the range evolves and if we stripped anything, see also
8576       //       AADereferenceable or AAAlign for similar situations.
8577       for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
8578         if (QueriedAA != this)
8579           continue;
8580         // If we are in a stady state we do not need to worry.
8581         if (T.getAssumed() == getState().getAssumed())
8582           continue;
8583         T.indicatePessimisticFixpoint();
8584       }
8585 
8586       return T.isValidState();
8587     };
8588 
8589     IntegerRangeState T(getBitWidth());
8590 
8591     if (!genericValueTraversal<IntegerRangeState>(A, getIRPosition(), *this, T,
8592                                                   VisitValueCB, getCtxI(),
8593                                                   /* UseValueSimplify */ false))
8594       return indicatePessimisticFixpoint();
8595 
8596     // Ensure that long def-use chains can't cause circular reasoning either by
8597     // introducing a cutoff below.
8598     if (clampStateAndIndicateChange(getState(), T) == ChangeStatus::UNCHANGED)
8599       return ChangeStatus::UNCHANGED;
8600     if (++NumChanges > MaxNumChanges) {
8601       LLVM_DEBUG(dbgs() << "[AAValueConstantRange] performed " << NumChanges
8602                         << " but only " << MaxNumChanges
8603                         << " are allowed to avoid cyclic reasoning.");
8604       return indicatePessimisticFixpoint();
8605     }
8606     return ChangeStatus::CHANGED;
8607   }
8608 
8609   /// See AbstractAttribute::trackStatistics()
8610   void trackStatistics() const override {
8611     STATS_DECLTRACK_FLOATING_ATTR(value_range)
8612   }
8613 
8614   /// Tracker to bail after too many widening steps of the constant range.
8615   int NumChanges = 0;
8616 
8617   /// Upper bound for the number of allowed changes (=widening steps) for the
8618   /// constant range before we give up.
8619   static constexpr int MaxNumChanges = 5;
8620 };
8621 
8622 struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
8623   AAValueConstantRangeFunction(const IRPosition &IRP, Attributor &A)
8624       : AAValueConstantRangeImpl(IRP, A) {}
8625 
8626   /// See AbstractAttribute::initialize(...).
8627   ChangeStatus updateImpl(Attributor &A) override {
8628     llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
8629                      "not be called");
8630   }
8631 
8632   /// See AbstractAttribute::trackStatistics()
8633   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
8634 };
8635 
8636 struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
8637   AAValueConstantRangeCallSite(const IRPosition &IRP, Attributor &A)
8638       : AAValueConstantRangeFunction(IRP, A) {}
8639 
8640   /// See AbstractAttribute::trackStatistics()
8641   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
8642 };
8643 
8644 struct AAValueConstantRangeCallSiteReturned
8645     : AACallSiteReturnedFromReturned<AAValueConstantRange,
8646                                      AAValueConstantRangeImpl,
8647                                      AAValueConstantRangeImpl::StateType,
8648                                      /* IntroduceCallBaseContext */ true> {
8649   AAValueConstantRangeCallSiteReturned(const IRPosition &IRP, Attributor &A)
8650       : AACallSiteReturnedFromReturned<AAValueConstantRange,
8651                                        AAValueConstantRangeImpl,
8652                                        AAValueConstantRangeImpl::StateType,
8653                                        /* IntroduceCallBaseContext */ true>(IRP,
8654                                                                             A) {
8655   }
8656 
8657   /// See AbstractAttribute::initialize(...).
8658   void initialize(Attributor &A) override {
8659     // If it is a load instruction with range metadata, use the metadata.
8660     if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
8661       if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
8662         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
8663 
8664     AAValueConstantRangeImpl::initialize(A);
8665   }
8666 
8667   /// See AbstractAttribute::trackStatistics()
8668   void trackStatistics() const override {
8669     STATS_DECLTRACK_CSRET_ATTR(value_range)
8670   }
8671 };
8672 struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
8673   AAValueConstantRangeCallSiteArgument(const IRPosition &IRP, Attributor &A)
8674       : AAValueConstantRangeFloating(IRP, A) {}
8675 
8676   /// See AbstractAttribute::manifest()
8677   ChangeStatus manifest(Attributor &A) override {
8678     return ChangeStatus::UNCHANGED;
8679   }
8680 
8681   /// See AbstractAttribute::trackStatistics()
8682   void trackStatistics() const override {
8683     STATS_DECLTRACK_CSARG_ATTR(value_range)
8684   }
8685 };
8686 
8687 /// ------------------ Potential Values Attribute -------------------------
8688 
8689 struct AAPotentialValuesImpl : AAPotentialValues {
8690   using StateType = PotentialConstantIntValuesState;
8691 
8692   AAPotentialValuesImpl(const IRPosition &IRP, Attributor &A)
8693       : AAPotentialValues(IRP, A) {}
8694 
8695   /// See AbstractAttribute::initialize(..).
8696   void initialize(Attributor &A) override {
8697     if (A.hasSimplificationCallback(getIRPosition()))
8698       indicatePessimisticFixpoint();
8699     else
8700       AAPotentialValues::initialize(A);
8701   }
8702 
8703   /// See AbstractAttribute::getAsStr().
8704   const std::string getAsStr() const override {
8705     std::string Str;
8706     llvm::raw_string_ostream OS(Str);
8707     OS << getState();
8708     return OS.str();
8709   }
8710 
8711   /// See AbstractAttribute::updateImpl(...).
8712   ChangeStatus updateImpl(Attributor &A) override {
8713     return indicatePessimisticFixpoint();
8714   }
8715 };
8716 
8717 struct AAPotentialValuesArgument final
8718     : AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
8719                                       PotentialConstantIntValuesState> {
8720   using Base =
8721       AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
8722                                       PotentialConstantIntValuesState>;
8723   AAPotentialValuesArgument(const IRPosition &IRP, Attributor &A)
8724       : Base(IRP, A) {}
8725 
8726   /// See AbstractAttribute::initialize(..).
8727   void initialize(Attributor &A) override {
8728     if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
8729       indicatePessimisticFixpoint();
8730     } else {
8731       Base::initialize(A);
8732     }
8733   }
8734 
8735   /// See AbstractAttribute::trackStatistics()
8736   void trackStatistics() const override {
8737     STATS_DECLTRACK_ARG_ATTR(potential_values)
8738   }
8739 };
8740 
8741 struct AAPotentialValuesReturned
8742     : AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl> {
8743   using Base =
8744       AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl>;
8745   AAPotentialValuesReturned(const IRPosition &IRP, Attributor &A)
8746       : Base(IRP, A) {}
8747 
8748   /// See AbstractAttribute::trackStatistics()
8749   void trackStatistics() const override {
8750     STATS_DECLTRACK_FNRET_ATTR(potential_values)
8751   }
8752 };
8753 
8754 struct AAPotentialValuesFloating : AAPotentialValuesImpl {
8755   AAPotentialValuesFloating(const IRPosition &IRP, Attributor &A)
8756       : AAPotentialValuesImpl(IRP, A) {}
8757 
8758   /// See AbstractAttribute::initialize(..).
8759   void initialize(Attributor &A) override {
8760     AAPotentialValuesImpl::initialize(A);
8761     if (isAtFixpoint())
8762       return;
8763 
8764     Value &V = getAssociatedValue();
8765 
8766     if (auto *C = dyn_cast<ConstantInt>(&V)) {
8767       unionAssumed(C->getValue());
8768       indicateOptimisticFixpoint();
8769       return;
8770     }
8771 
8772     if (isa<UndefValue>(&V)) {
8773       unionAssumedWithUndef();
8774       indicateOptimisticFixpoint();
8775       return;
8776     }
8777 
8778     if (isa<BinaryOperator>(&V) || isa<ICmpInst>(&V) || isa<CastInst>(&V))
8779       return;
8780 
8781     if (isa<SelectInst>(V) || isa<PHINode>(V) || isa<LoadInst>(V))
8782       return;
8783 
8784     indicatePessimisticFixpoint();
8785 
8786     LLVM_DEBUG(dbgs() << "[AAPotentialValues] We give up: "
8787                       << getAssociatedValue() << "\n");
8788   }
8789 
8790   static bool calculateICmpInst(const ICmpInst *ICI, const APInt &LHS,
8791                                 const APInt &RHS) {
8792     return ICmpInst::compare(LHS, RHS, ICI->getPredicate());
8793   }
8794 
8795   static APInt calculateCastInst(const CastInst *CI, const APInt &Src,
8796                                  uint32_t ResultBitWidth) {
8797     Instruction::CastOps CastOp = CI->getOpcode();
8798     switch (CastOp) {
8799     default:
8800       llvm_unreachable("unsupported or not integer cast");
8801     case Instruction::Trunc:
8802       return Src.trunc(ResultBitWidth);
8803     case Instruction::SExt:
8804       return Src.sext(ResultBitWidth);
8805     case Instruction::ZExt:
8806       return Src.zext(ResultBitWidth);
8807     case Instruction::BitCast:
8808       return Src;
8809     }
8810   }
8811 
8812   static APInt calculateBinaryOperator(const BinaryOperator *BinOp,
8813                                        const APInt &LHS, const APInt &RHS,
8814                                        bool &SkipOperation, bool &Unsupported) {
8815     Instruction::BinaryOps BinOpcode = BinOp->getOpcode();
8816     // Unsupported is set to true when the binary operator is not supported.
8817     // SkipOperation is set to true when UB occur with the given operand pair
8818     // (LHS, RHS).
8819     // TODO: we should look at nsw and nuw keywords to handle operations
8820     //       that create poison or undef value.
8821     switch (BinOpcode) {
8822     default:
8823       Unsupported = true;
8824       return LHS;
8825     case Instruction::Add:
8826       return LHS + RHS;
8827     case Instruction::Sub:
8828       return LHS - RHS;
8829     case Instruction::Mul:
8830       return LHS * RHS;
8831     case Instruction::UDiv:
8832       if (RHS.isZero()) {
8833         SkipOperation = true;
8834         return LHS;
8835       }
8836       return LHS.udiv(RHS);
8837     case Instruction::SDiv:
8838       if (RHS.isZero()) {
8839         SkipOperation = true;
8840         return LHS;
8841       }
8842       return LHS.sdiv(RHS);
8843     case Instruction::URem:
8844       if (RHS.isZero()) {
8845         SkipOperation = true;
8846         return LHS;
8847       }
8848       return LHS.urem(RHS);
8849     case Instruction::SRem:
8850       if (RHS.isZero()) {
8851         SkipOperation = true;
8852         return LHS;
8853       }
8854       return LHS.srem(RHS);
8855     case Instruction::Shl:
8856       return LHS.shl(RHS);
8857     case Instruction::LShr:
8858       return LHS.lshr(RHS);
8859     case Instruction::AShr:
8860       return LHS.ashr(RHS);
8861     case Instruction::And:
8862       return LHS & RHS;
8863     case Instruction::Or:
8864       return LHS | RHS;
8865     case Instruction::Xor:
8866       return LHS ^ RHS;
8867     }
8868   }
8869 
8870   bool calculateBinaryOperatorAndTakeUnion(const BinaryOperator *BinOp,
8871                                            const APInt &LHS, const APInt &RHS) {
8872     bool SkipOperation = false;
8873     bool Unsupported = false;
8874     APInt Result =
8875         calculateBinaryOperator(BinOp, LHS, RHS, SkipOperation, Unsupported);
8876     if (Unsupported)
8877       return false;
8878     // If SkipOperation is true, we can ignore this operand pair (L, R).
8879     if (!SkipOperation)
8880       unionAssumed(Result);
8881     return isValidState();
8882   }
8883 
8884   ChangeStatus updateWithICmpInst(Attributor &A, ICmpInst *ICI) {
8885     auto AssumedBefore = getAssumed();
8886     Value *LHS = ICI->getOperand(0);
8887     Value *RHS = ICI->getOperand(1);
8888 
8889     // Simplify the operands first.
8890     bool UsedAssumedInformation = false;
8891     const auto &SimplifiedLHS =
8892         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8893                                *this, UsedAssumedInformation);
8894     if (!SimplifiedLHS.hasValue())
8895       return ChangeStatus::UNCHANGED;
8896     if (!SimplifiedLHS.getValue())
8897       return indicatePessimisticFixpoint();
8898     LHS = *SimplifiedLHS;
8899 
8900     const auto &SimplifiedRHS =
8901         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8902                                *this, UsedAssumedInformation);
8903     if (!SimplifiedRHS.hasValue())
8904       return ChangeStatus::UNCHANGED;
8905     if (!SimplifiedRHS.getValue())
8906       return indicatePessimisticFixpoint();
8907     RHS = *SimplifiedRHS;
8908 
8909     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8910       return indicatePessimisticFixpoint();
8911 
8912     auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
8913                                                 DepClassTy::REQUIRED);
8914     if (!LHSAA.isValidState())
8915       return indicatePessimisticFixpoint();
8916 
8917     auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
8918                                                 DepClassTy::REQUIRED);
8919     if (!RHSAA.isValidState())
8920       return indicatePessimisticFixpoint();
8921 
8922     const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
8923     const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
8924 
8925     // TODO: make use of undef flag to limit potential values aggressively.
8926     bool MaybeTrue = false, MaybeFalse = false;
8927     const APInt Zero(RHS->getType()->getIntegerBitWidth(), 0);
8928     if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
8929       // The result of any comparison between undefs can be soundly replaced
8930       // with undef.
8931       unionAssumedWithUndef();
8932     } else if (LHSAA.undefIsContained()) {
8933       for (const APInt &R : RHSAAPVS) {
8934         bool CmpResult = calculateICmpInst(ICI, Zero, R);
8935         MaybeTrue |= CmpResult;
8936         MaybeFalse |= !CmpResult;
8937         if (MaybeTrue & MaybeFalse)
8938           return indicatePessimisticFixpoint();
8939       }
8940     } else if (RHSAA.undefIsContained()) {
8941       for (const APInt &L : LHSAAPVS) {
8942         bool CmpResult = calculateICmpInst(ICI, L, Zero);
8943         MaybeTrue |= CmpResult;
8944         MaybeFalse |= !CmpResult;
8945         if (MaybeTrue & MaybeFalse)
8946           return indicatePessimisticFixpoint();
8947       }
8948     } else {
8949       for (const APInt &L : LHSAAPVS) {
8950         for (const APInt &R : RHSAAPVS) {
8951           bool CmpResult = calculateICmpInst(ICI, L, R);
8952           MaybeTrue |= CmpResult;
8953           MaybeFalse |= !CmpResult;
8954           if (MaybeTrue & MaybeFalse)
8955             return indicatePessimisticFixpoint();
8956         }
8957       }
8958     }
8959     if (MaybeTrue)
8960       unionAssumed(APInt(/* numBits */ 1, /* val */ 1));
8961     if (MaybeFalse)
8962       unionAssumed(APInt(/* numBits */ 1, /* val */ 0));
8963     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
8964                                          : ChangeStatus::CHANGED;
8965   }
8966 
8967   ChangeStatus updateWithSelectInst(Attributor &A, SelectInst *SI) {
8968     auto AssumedBefore = getAssumed();
8969     Value *LHS = SI->getTrueValue();
8970     Value *RHS = SI->getFalseValue();
8971 
8972     // Simplify the operands first.
8973     bool UsedAssumedInformation = false;
8974     const auto &SimplifiedLHS =
8975         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8976                                *this, UsedAssumedInformation);
8977     if (!SimplifiedLHS.hasValue())
8978       return ChangeStatus::UNCHANGED;
8979     if (!SimplifiedLHS.getValue())
8980       return indicatePessimisticFixpoint();
8981     LHS = *SimplifiedLHS;
8982 
8983     const auto &SimplifiedRHS =
8984         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8985                                *this, UsedAssumedInformation);
8986     if (!SimplifiedRHS.hasValue())
8987       return ChangeStatus::UNCHANGED;
8988     if (!SimplifiedRHS.getValue())
8989       return indicatePessimisticFixpoint();
8990     RHS = *SimplifiedRHS;
8991 
8992     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8993       return indicatePessimisticFixpoint();
8994 
8995     Optional<Constant *> C = A.getAssumedConstant(*SI->getCondition(), *this,
8996                                                   UsedAssumedInformation);
8997 
8998     // Check if we only need one operand.
8999     bool OnlyLeft = false, OnlyRight = false;
9000     if (C.hasValue() && *C && (*C)->isOneValue())
9001       OnlyLeft = true;
9002     else if (C.hasValue() && *C && (*C)->isZeroValue())
9003       OnlyRight = true;
9004 
9005     const AAPotentialValues *LHSAA = nullptr, *RHSAA = nullptr;
9006     if (!OnlyRight) {
9007       LHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
9008                                              DepClassTy::REQUIRED);
9009       if (!LHSAA->isValidState())
9010         return indicatePessimisticFixpoint();
9011     }
9012     if (!OnlyLeft) {
9013       RHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
9014                                              DepClassTy::REQUIRED);
9015       if (!RHSAA->isValidState())
9016         return indicatePessimisticFixpoint();
9017     }
9018 
9019     if (!LHSAA || !RHSAA) {
9020       // select (true/false), lhs, rhs
9021       auto *OpAA = LHSAA ? LHSAA : RHSAA;
9022 
9023       if (OpAA->undefIsContained())
9024         unionAssumedWithUndef();
9025       else
9026         unionAssumed(*OpAA);
9027 
9028     } else if (LHSAA->undefIsContained() && RHSAA->undefIsContained()) {
9029       // select i1 *, undef , undef => undef
9030       unionAssumedWithUndef();
9031     } else {
9032       unionAssumed(*LHSAA);
9033       unionAssumed(*RHSAA);
9034     }
9035     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9036                                          : ChangeStatus::CHANGED;
9037   }
9038 
9039   ChangeStatus updateWithCastInst(Attributor &A, CastInst *CI) {
9040     auto AssumedBefore = getAssumed();
9041     if (!CI->isIntegerCast())
9042       return indicatePessimisticFixpoint();
9043     assert(CI->getNumOperands() == 1 && "Expected cast to be unary!");
9044     uint32_t ResultBitWidth = CI->getDestTy()->getIntegerBitWidth();
9045     Value *Src = CI->getOperand(0);
9046 
9047     // Simplify the operand first.
9048     bool UsedAssumedInformation = false;
9049     const auto &SimplifiedSrc =
9050         A.getAssumedSimplified(IRPosition::value(*Src, getCallBaseContext()),
9051                                *this, UsedAssumedInformation);
9052     if (!SimplifiedSrc.hasValue())
9053       return ChangeStatus::UNCHANGED;
9054     if (!SimplifiedSrc.getValue())
9055       return indicatePessimisticFixpoint();
9056     Src = *SimplifiedSrc;
9057 
9058     auto &SrcAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*Src),
9059                                                 DepClassTy::REQUIRED);
9060     if (!SrcAA.isValidState())
9061       return indicatePessimisticFixpoint();
9062     const DenseSet<APInt> &SrcAAPVS = SrcAA.getAssumedSet();
9063     if (SrcAA.undefIsContained())
9064       unionAssumedWithUndef();
9065     else {
9066       for (const APInt &S : SrcAAPVS) {
9067         APInt T = calculateCastInst(CI, S, ResultBitWidth);
9068         unionAssumed(T);
9069       }
9070     }
9071     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9072                                          : ChangeStatus::CHANGED;
9073   }
9074 
9075   ChangeStatus updateWithBinaryOperator(Attributor &A, BinaryOperator *BinOp) {
9076     auto AssumedBefore = getAssumed();
9077     Value *LHS = BinOp->getOperand(0);
9078     Value *RHS = BinOp->getOperand(1);
9079 
9080     // Simplify the operands first.
9081     bool UsedAssumedInformation = false;
9082     const auto &SimplifiedLHS =
9083         A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
9084                                *this, UsedAssumedInformation);
9085     if (!SimplifiedLHS.hasValue())
9086       return ChangeStatus::UNCHANGED;
9087     if (!SimplifiedLHS.getValue())
9088       return indicatePessimisticFixpoint();
9089     LHS = *SimplifiedLHS;
9090 
9091     const auto &SimplifiedRHS =
9092         A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
9093                                *this, UsedAssumedInformation);
9094     if (!SimplifiedRHS.hasValue())
9095       return ChangeStatus::UNCHANGED;
9096     if (!SimplifiedRHS.getValue())
9097       return indicatePessimisticFixpoint();
9098     RHS = *SimplifiedRHS;
9099 
9100     if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
9101       return indicatePessimisticFixpoint();
9102 
9103     auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
9104                                                 DepClassTy::REQUIRED);
9105     if (!LHSAA.isValidState())
9106       return indicatePessimisticFixpoint();
9107 
9108     auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
9109                                                 DepClassTy::REQUIRED);
9110     if (!RHSAA.isValidState())
9111       return indicatePessimisticFixpoint();
9112 
9113     const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
9114     const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
9115     const APInt Zero = APInt(LHS->getType()->getIntegerBitWidth(), 0);
9116 
9117     // TODO: make use of undef flag to limit potential values aggressively.
9118     if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
9119       if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, Zero))
9120         return indicatePessimisticFixpoint();
9121     } else if (LHSAA.undefIsContained()) {
9122       for (const APInt &R : RHSAAPVS) {
9123         if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, R))
9124           return indicatePessimisticFixpoint();
9125       }
9126     } else if (RHSAA.undefIsContained()) {
9127       for (const APInt &L : LHSAAPVS) {
9128         if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, Zero))
9129           return indicatePessimisticFixpoint();
9130       }
9131     } else {
9132       for (const APInt &L : LHSAAPVS) {
9133         for (const APInt &R : RHSAAPVS) {
9134           if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, R))
9135             return indicatePessimisticFixpoint();
9136         }
9137       }
9138     }
9139     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9140                                          : ChangeStatus::CHANGED;
9141   }
9142 
9143   ChangeStatus updateWithPHINode(Attributor &A, PHINode *PHI) {
9144     auto AssumedBefore = getAssumed();
9145     for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
9146       Value *IncomingValue = PHI->getIncomingValue(u);
9147 
9148       // Simplify the operand first.
9149       bool UsedAssumedInformation = false;
9150       const auto &SimplifiedIncomingValue = A.getAssumedSimplified(
9151           IRPosition::value(*IncomingValue, getCallBaseContext()), *this,
9152           UsedAssumedInformation);
9153       if (!SimplifiedIncomingValue.hasValue())
9154         continue;
9155       if (!SimplifiedIncomingValue.getValue())
9156         return indicatePessimisticFixpoint();
9157       IncomingValue = *SimplifiedIncomingValue;
9158 
9159       auto &PotentialValuesAA = A.getAAFor<AAPotentialValues>(
9160           *this, IRPosition::value(*IncomingValue), DepClassTy::REQUIRED);
9161       if (!PotentialValuesAA.isValidState())
9162         return indicatePessimisticFixpoint();
9163       if (PotentialValuesAA.undefIsContained())
9164         unionAssumedWithUndef();
9165       else
9166         unionAssumed(PotentialValuesAA.getAssumed());
9167     }
9168     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9169                                          : ChangeStatus::CHANGED;
9170   }
9171 
9172   ChangeStatus updateWithLoad(Attributor &A, LoadInst &L) {
9173     if (!L.getType()->isIntegerTy())
9174       return indicatePessimisticFixpoint();
9175 
9176     auto Union = [&](Value &V) {
9177       if (isa<UndefValue>(V)) {
9178         unionAssumedWithUndef();
9179         return true;
9180       }
9181       if (ConstantInt *CI = dyn_cast<ConstantInt>(&V)) {
9182         unionAssumed(CI->getValue());
9183         return true;
9184       }
9185       return false;
9186     };
9187     auto AssumedBefore = getAssumed();
9188 
9189     if (!AAValueSimplifyImpl::handleLoad(A, *this, L, Union))
9190       return indicatePessimisticFixpoint();
9191 
9192     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9193                                          : ChangeStatus::CHANGED;
9194   }
9195 
9196   /// See AbstractAttribute::updateImpl(...).
9197   ChangeStatus updateImpl(Attributor &A) override {
9198     Value &V = getAssociatedValue();
9199     Instruction *I = dyn_cast<Instruction>(&V);
9200 
9201     if (auto *ICI = dyn_cast<ICmpInst>(I))
9202       return updateWithICmpInst(A, ICI);
9203 
9204     if (auto *SI = dyn_cast<SelectInst>(I))
9205       return updateWithSelectInst(A, SI);
9206 
9207     if (auto *CI = dyn_cast<CastInst>(I))
9208       return updateWithCastInst(A, CI);
9209 
9210     if (auto *BinOp = dyn_cast<BinaryOperator>(I))
9211       return updateWithBinaryOperator(A, BinOp);
9212 
9213     if (auto *PHI = dyn_cast<PHINode>(I))
9214       return updateWithPHINode(A, PHI);
9215 
9216     if (auto *L = dyn_cast<LoadInst>(I))
9217       return updateWithLoad(A, *L);
9218 
9219     return indicatePessimisticFixpoint();
9220   }
9221 
9222   /// See AbstractAttribute::trackStatistics()
9223   void trackStatistics() const override {
9224     STATS_DECLTRACK_FLOATING_ATTR(potential_values)
9225   }
9226 };
9227 
9228 struct AAPotentialValuesFunction : AAPotentialValuesImpl {
9229   AAPotentialValuesFunction(const IRPosition &IRP, Attributor &A)
9230       : AAPotentialValuesImpl(IRP, A) {}
9231 
9232   /// See AbstractAttribute::initialize(...).
9233   ChangeStatus updateImpl(Attributor &A) override {
9234     llvm_unreachable("AAPotentialValues(Function|CallSite)::updateImpl will "
9235                      "not be called");
9236   }
9237 
9238   /// See AbstractAttribute::trackStatistics()
9239   void trackStatistics() const override {
9240     STATS_DECLTRACK_FN_ATTR(potential_values)
9241   }
9242 };
9243 
9244 struct AAPotentialValuesCallSite : AAPotentialValuesFunction {
9245   AAPotentialValuesCallSite(const IRPosition &IRP, Attributor &A)
9246       : AAPotentialValuesFunction(IRP, A) {}
9247 
9248   /// See AbstractAttribute::trackStatistics()
9249   void trackStatistics() const override {
9250     STATS_DECLTRACK_CS_ATTR(potential_values)
9251   }
9252 };
9253 
9254 struct AAPotentialValuesCallSiteReturned
9255     : AACallSiteReturnedFromReturned<AAPotentialValues, AAPotentialValuesImpl> {
9256   AAPotentialValuesCallSiteReturned(const IRPosition &IRP, Attributor &A)
9257       : AACallSiteReturnedFromReturned<AAPotentialValues,
9258                                        AAPotentialValuesImpl>(IRP, A) {}
9259 
9260   /// See AbstractAttribute::trackStatistics()
9261   void trackStatistics() const override {
9262     STATS_DECLTRACK_CSRET_ATTR(potential_values)
9263   }
9264 };
9265 
9266 struct AAPotentialValuesCallSiteArgument : AAPotentialValuesFloating {
9267   AAPotentialValuesCallSiteArgument(const IRPosition &IRP, Attributor &A)
9268       : AAPotentialValuesFloating(IRP, A) {}
9269 
9270   /// See AbstractAttribute::initialize(..).
9271   void initialize(Attributor &A) override {
9272     AAPotentialValuesImpl::initialize(A);
9273     if (isAtFixpoint())
9274       return;
9275 
9276     Value &V = getAssociatedValue();
9277 
9278     if (auto *C = dyn_cast<ConstantInt>(&V)) {
9279       unionAssumed(C->getValue());
9280       indicateOptimisticFixpoint();
9281       return;
9282     }
9283 
9284     if (isa<UndefValue>(&V)) {
9285       unionAssumedWithUndef();
9286       indicateOptimisticFixpoint();
9287       return;
9288     }
9289   }
9290 
9291   /// See AbstractAttribute::updateImpl(...).
9292   ChangeStatus updateImpl(Attributor &A) override {
9293     Value &V = getAssociatedValue();
9294     auto AssumedBefore = getAssumed();
9295     auto &AA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(V),
9296                                              DepClassTy::REQUIRED);
9297     const auto &S = AA.getAssumed();
9298     unionAssumed(S);
9299     return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9300                                          : ChangeStatus::CHANGED;
9301   }
9302 
9303   /// See AbstractAttribute::trackStatistics()
9304   void trackStatistics() const override {
9305     STATS_DECLTRACK_CSARG_ATTR(potential_values)
9306   }
9307 };
9308 
9309 /// ------------------------ NoUndef Attribute ---------------------------------
9310 struct AANoUndefImpl : AANoUndef {
9311   AANoUndefImpl(const IRPosition &IRP, Attributor &A) : AANoUndef(IRP, A) {}
9312 
9313   /// See AbstractAttribute::initialize(...).
9314   void initialize(Attributor &A) override {
9315     if (getIRPosition().hasAttr({Attribute::NoUndef})) {
9316       indicateOptimisticFixpoint();
9317       return;
9318     }
9319     Value &V = getAssociatedValue();
9320     if (isa<UndefValue>(V))
9321       indicatePessimisticFixpoint();
9322     else if (isa<FreezeInst>(V))
9323       indicateOptimisticFixpoint();
9324     else if (getPositionKind() != IRPosition::IRP_RETURNED &&
9325              isGuaranteedNotToBeUndefOrPoison(&V))
9326       indicateOptimisticFixpoint();
9327     else
9328       AANoUndef::initialize(A);
9329   }
9330 
9331   /// See followUsesInMBEC
9332   bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
9333                        AANoUndef::StateType &State) {
9334     const Value *UseV = U->get();
9335     const DominatorTree *DT = nullptr;
9336     AssumptionCache *AC = nullptr;
9337     InformationCache &InfoCache = A.getInfoCache();
9338     if (Function *F = getAnchorScope()) {
9339       DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
9340       AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
9341     }
9342     State.setKnown(isGuaranteedNotToBeUndefOrPoison(UseV, AC, I, DT));
9343     bool TrackUse = false;
9344     // Track use for instructions which must produce undef or poison bits when
9345     // at least one operand contains such bits.
9346     if (isa<CastInst>(*I) || isa<GetElementPtrInst>(*I))
9347       TrackUse = true;
9348     return TrackUse;
9349   }
9350 
9351   /// See AbstractAttribute::getAsStr().
9352   const std::string getAsStr() const override {
9353     return getAssumed() ? "noundef" : "may-undef-or-poison";
9354   }
9355 
9356   ChangeStatus manifest(Attributor &A) override {
9357     // We don't manifest noundef attribute for dead positions because the
9358     // associated values with dead positions would be replaced with undef
9359     // values.
9360     bool UsedAssumedInformation = false;
9361     if (A.isAssumedDead(getIRPosition(), nullptr, nullptr,
9362                         UsedAssumedInformation))
9363       return ChangeStatus::UNCHANGED;
9364     // A position whose simplified value does not have any value is
9365     // considered to be dead. We don't manifest noundef in such positions for
9366     // the same reason above.
9367     if (!A.getAssumedSimplified(getIRPosition(), *this, UsedAssumedInformation)
9368              .hasValue())
9369       return ChangeStatus::UNCHANGED;
9370     return AANoUndef::manifest(A);
9371   }
9372 };
9373 
9374 struct AANoUndefFloating : public AANoUndefImpl {
9375   AANoUndefFloating(const IRPosition &IRP, Attributor &A)
9376       : AANoUndefImpl(IRP, A) {}
9377 
9378   /// See AbstractAttribute::initialize(...).
9379   void initialize(Attributor &A) override {
9380     AANoUndefImpl::initialize(A);
9381     if (!getState().isAtFixpoint())
9382       if (Instruction *CtxI = getCtxI())
9383         followUsesInMBEC(*this, A, getState(), *CtxI);
9384   }
9385 
9386   /// See AbstractAttribute::updateImpl(...).
9387   ChangeStatus updateImpl(Attributor &A) override {
9388     auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
9389                             AANoUndef::StateType &T, bool Stripped) -> bool {
9390       const auto &AA = A.getAAFor<AANoUndef>(*this, IRPosition::value(V),
9391                                              DepClassTy::REQUIRED);
9392       if (!Stripped && this == &AA) {
9393         T.indicatePessimisticFixpoint();
9394       } else {
9395         const AANoUndef::StateType &S =
9396             static_cast<const AANoUndef::StateType &>(AA.getState());
9397         T ^= S;
9398       }
9399       return T.isValidState();
9400     };
9401 
9402     StateType T;
9403     if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
9404                                           VisitValueCB, getCtxI()))
9405       return indicatePessimisticFixpoint();
9406 
9407     return clampStateAndIndicateChange(getState(), T);
9408   }
9409 
9410   /// See AbstractAttribute::trackStatistics()
9411   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
9412 };
9413 
9414 struct AANoUndefReturned final
9415     : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl> {
9416   AANoUndefReturned(const IRPosition &IRP, Attributor &A)
9417       : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl>(IRP, A) {}
9418 
9419   /// See AbstractAttribute::trackStatistics()
9420   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
9421 };
9422 
9423 struct AANoUndefArgument final
9424     : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl> {
9425   AANoUndefArgument(const IRPosition &IRP, Attributor &A)
9426       : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl>(IRP, A) {}
9427 
9428   /// See AbstractAttribute::trackStatistics()
9429   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noundef) }
9430 };
9431 
9432 struct AANoUndefCallSiteArgument final : AANoUndefFloating {
9433   AANoUndefCallSiteArgument(const IRPosition &IRP, Attributor &A)
9434       : AANoUndefFloating(IRP, A) {}
9435 
9436   /// See AbstractAttribute::trackStatistics()
9437   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noundef) }
9438 };
9439 
9440 struct AANoUndefCallSiteReturned final
9441     : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl> {
9442   AANoUndefCallSiteReturned(const IRPosition &IRP, Attributor &A)
9443       : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl>(IRP, A) {}
9444 
9445   /// See AbstractAttribute::trackStatistics()
9446   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noundef) }
9447 };
9448 
9449 struct AACallEdgesImpl : public AACallEdges {
9450   AACallEdgesImpl(const IRPosition &IRP, Attributor &A) : AACallEdges(IRP, A) {}
9451 
9452   virtual const SetVector<Function *> &getOptimisticEdges() const override {
9453     return CalledFunctions;
9454   }
9455 
9456   virtual bool hasUnknownCallee() const override { return HasUnknownCallee; }
9457 
9458   virtual bool hasNonAsmUnknownCallee() const override {
9459     return HasUnknownCalleeNonAsm;
9460   }
9461 
9462   const std::string getAsStr() const override {
9463     return "CallEdges[" + std::to_string(HasUnknownCallee) + "," +
9464            std::to_string(CalledFunctions.size()) + "]";
9465   }
9466 
9467   void trackStatistics() const override {}
9468 
9469 protected:
9470   void addCalledFunction(Function *Fn, ChangeStatus &Change) {
9471     if (CalledFunctions.insert(Fn)) {
9472       Change = ChangeStatus::CHANGED;
9473       LLVM_DEBUG(dbgs() << "[AACallEdges] New call edge: " << Fn->getName()
9474                         << "\n");
9475     }
9476   }
9477 
9478   void setHasUnknownCallee(bool NonAsm, ChangeStatus &Change) {
9479     if (!HasUnknownCallee)
9480       Change = ChangeStatus::CHANGED;
9481     if (NonAsm && !HasUnknownCalleeNonAsm)
9482       Change = ChangeStatus::CHANGED;
9483     HasUnknownCalleeNonAsm |= NonAsm;
9484     HasUnknownCallee = true;
9485   }
9486 
9487 private:
9488   /// Optimistic set of functions that might be called by this position.
9489   SetVector<Function *> CalledFunctions;
9490 
9491   /// Is there any call with a unknown callee.
9492   bool HasUnknownCallee = false;
9493 
9494   /// Is there any call with a unknown callee, excluding any inline asm.
9495   bool HasUnknownCalleeNonAsm = false;
9496 };
9497 
9498 struct AACallEdgesCallSite : public AACallEdgesImpl {
9499   AACallEdgesCallSite(const IRPosition &IRP, Attributor &A)
9500       : AACallEdgesImpl(IRP, A) {}
9501   /// See AbstractAttribute::updateImpl(...).
9502   ChangeStatus updateImpl(Attributor &A) override {
9503     ChangeStatus Change = ChangeStatus::UNCHANGED;
9504 
9505     auto VisitValue = [&](Value &V, const Instruction *CtxI, bool &HasUnknown,
9506                           bool Stripped) -> bool {
9507       if (Function *Fn = dyn_cast<Function>(&V)) {
9508         addCalledFunction(Fn, Change);
9509       } else {
9510         LLVM_DEBUG(dbgs() << "[AACallEdges] Unrecognized value: " << V << "\n");
9511         setHasUnknownCallee(true, Change);
9512       }
9513 
9514       // Explore all values.
9515       return true;
9516     };
9517 
9518     // Process any value that we might call.
9519     auto ProcessCalledOperand = [&](Value *V) {
9520       bool DummyValue = false;
9521       if (!genericValueTraversal<bool>(A, IRPosition::value(*V), *this,
9522                                        DummyValue, VisitValue, nullptr,
9523                                        false)) {
9524         // If we haven't gone through all values, assume that there are unknown
9525         // callees.
9526         setHasUnknownCallee(true, Change);
9527       }
9528     };
9529 
9530     CallBase *CB = cast<CallBase>(getCtxI());
9531 
9532     if (CB->isInlineAsm()) {
9533       setHasUnknownCallee(false, Change);
9534       return Change;
9535     }
9536 
9537     // Process callee metadata if available.
9538     if (auto *MD = getCtxI()->getMetadata(LLVMContext::MD_callees)) {
9539       for (auto &Op : MD->operands()) {
9540         Function *Callee = mdconst::dyn_extract_or_null<Function>(Op);
9541         if (Callee)
9542           addCalledFunction(Callee, Change);
9543       }
9544       return Change;
9545     }
9546 
9547     // The most simple case.
9548     ProcessCalledOperand(CB->getCalledOperand());
9549 
9550     // Process callback functions.
9551     SmallVector<const Use *, 4u> CallbackUses;
9552     AbstractCallSite::getCallbackUses(*CB, CallbackUses);
9553     for (const Use *U : CallbackUses)
9554       ProcessCalledOperand(U->get());
9555 
9556     return Change;
9557   }
9558 };
9559 
9560 struct AACallEdgesFunction : public AACallEdgesImpl {
9561   AACallEdgesFunction(const IRPosition &IRP, Attributor &A)
9562       : AACallEdgesImpl(IRP, A) {}
9563 
9564   /// See AbstractAttribute::updateImpl(...).
9565   ChangeStatus updateImpl(Attributor &A) override {
9566     ChangeStatus Change = ChangeStatus::UNCHANGED;
9567 
9568     auto ProcessCallInst = [&](Instruction &Inst) {
9569       CallBase &CB = cast<CallBase>(Inst);
9570 
9571       auto &CBEdges = A.getAAFor<AACallEdges>(
9572           *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
9573       if (CBEdges.hasNonAsmUnknownCallee())
9574         setHasUnknownCallee(true, Change);
9575       if (CBEdges.hasUnknownCallee())
9576         setHasUnknownCallee(false, Change);
9577 
9578       for (Function *F : CBEdges.getOptimisticEdges())
9579         addCalledFunction(F, Change);
9580 
9581       return true;
9582     };
9583 
9584     // Visit all callable instructions.
9585     bool UsedAssumedInformation = false;
9586     if (!A.checkForAllCallLikeInstructions(ProcessCallInst, *this,
9587                                            UsedAssumedInformation)) {
9588       // If we haven't looked at all call like instructions, assume that there
9589       // are unknown callees.
9590       setHasUnknownCallee(true, Change);
9591     }
9592 
9593     return Change;
9594   }
9595 };
9596 
9597 struct AAFunctionReachabilityFunction : public AAFunctionReachability {
9598 private:
9599   struct QuerySet {
9600     void markReachable(const Function &Fn) {
9601       Reachable.insert(&Fn);
9602       Unreachable.erase(&Fn);
9603     }
9604 
9605     /// If there is no information about the function None is returned.
9606     Optional<bool> isCachedReachable(const Function &Fn) {
9607       // Assume that we can reach the function.
9608       // TODO: Be more specific with the unknown callee.
9609       if (CanReachUnknownCallee)
9610         return true;
9611 
9612       if (Reachable.count(&Fn))
9613         return true;
9614 
9615       if (Unreachable.count(&Fn))
9616         return false;
9617 
9618       return llvm::None;
9619     }
9620 
9621     /// Set of functions that we know for sure is reachable.
9622     DenseSet<const Function *> Reachable;
9623 
9624     /// Set of functions that are unreachable, but might become reachable.
9625     DenseSet<const Function *> Unreachable;
9626 
9627     /// If we can reach a function with a call to a unknown function we assume
9628     /// that we can reach any function.
9629     bool CanReachUnknownCallee = false;
9630   };
9631 
9632   struct QueryResolver : public QuerySet {
9633     ChangeStatus update(Attributor &A, const AAFunctionReachability &AA,
9634                         ArrayRef<const AACallEdges *> AAEdgesList) {
9635       ChangeStatus Change = ChangeStatus::UNCHANGED;
9636 
9637       for (auto *AAEdges : AAEdgesList) {
9638         if (AAEdges->hasUnknownCallee()) {
9639           if (!CanReachUnknownCallee)
9640             Change = ChangeStatus::CHANGED;
9641           CanReachUnknownCallee = true;
9642           return Change;
9643         }
9644       }
9645 
9646       for (const Function *Fn : make_early_inc_range(Unreachable)) {
9647         if (checkIfReachable(A, AA, AAEdgesList, *Fn)) {
9648           Change = ChangeStatus::CHANGED;
9649           markReachable(*Fn);
9650         }
9651       }
9652       return Change;
9653     }
9654 
9655     bool isReachable(Attributor &A, AAFunctionReachability &AA,
9656                      ArrayRef<const AACallEdges *> AAEdgesList,
9657                      const Function &Fn) {
9658       Optional<bool> Cached = isCachedReachable(Fn);
9659       if (Cached.hasValue())
9660         return Cached.getValue();
9661 
9662       // The query was not cached, thus it is new. We need to request an update
9663       // explicitly to make sure this the information is properly run to a
9664       // fixpoint.
9665       A.registerForUpdate(AA);
9666 
9667       // We need to assume that this function can't reach Fn to prevent
9668       // an infinite loop if this function is recursive.
9669       Unreachable.insert(&Fn);
9670 
9671       bool Result = checkIfReachable(A, AA, AAEdgesList, Fn);
9672       if (Result)
9673         markReachable(Fn);
9674       return Result;
9675     }
9676 
9677     bool checkIfReachable(Attributor &A, const AAFunctionReachability &AA,
9678                           ArrayRef<const AACallEdges *> AAEdgesList,
9679                           const Function &Fn) const {
9680 
9681       // Handle the most trivial case first.
9682       for (auto *AAEdges : AAEdgesList) {
9683         const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges();
9684 
9685         if (Edges.count(const_cast<Function *>(&Fn)))
9686           return true;
9687       }
9688 
9689       SmallVector<const AAFunctionReachability *, 8> Deps;
9690       for (auto &AAEdges : AAEdgesList) {
9691         const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges();
9692 
9693         for (Function *Edge : Edges) {
9694           // We don't need a dependency if the result is reachable.
9695           const AAFunctionReachability &EdgeReachability =
9696               A.getAAFor<AAFunctionReachability>(
9697                   AA, IRPosition::function(*Edge), DepClassTy::NONE);
9698           Deps.push_back(&EdgeReachability);
9699 
9700           if (EdgeReachability.canReach(A, Fn))
9701             return true;
9702         }
9703       }
9704 
9705       // The result is false for now, set dependencies and leave.
9706       for (auto *Dep : Deps)
9707         A.recordDependence(*Dep, AA, DepClassTy::REQUIRED);
9708 
9709       return false;
9710     }
9711   };
9712 
9713   /// Get call edges that can be reached by this instruction.
9714   bool getReachableCallEdges(Attributor &A, const AAReachability &Reachability,
9715                              const Instruction &Inst,
9716                              SmallVector<const AACallEdges *> &Result) const {
9717     // Determine call like instructions that we can reach from the inst.
9718     auto CheckCallBase = [&](Instruction &CBInst) {
9719       if (!Reachability.isAssumedReachable(A, Inst, CBInst))
9720         return true;
9721 
9722       auto &CB = cast<CallBase>(CBInst);
9723       const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
9724           *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
9725 
9726       Result.push_back(&AAEdges);
9727       return true;
9728     };
9729 
9730     bool UsedAssumedInformation = false;
9731     return A.checkForAllCallLikeInstructions(CheckCallBase, *this,
9732                                              UsedAssumedInformation,
9733                                              /* CheckBBLivenessOnly */ true);
9734   }
9735 
9736 public:
9737   AAFunctionReachabilityFunction(const IRPosition &IRP, Attributor &A)
9738       : AAFunctionReachability(IRP, A) {}
9739 
9740   bool canReach(Attributor &A, const Function &Fn) const override {
9741     if (!isValidState())
9742       return true;
9743 
9744     const AACallEdges &AAEdges =
9745         A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
9746 
9747     // Attributor returns attributes as const, so this function has to be
9748     // const for users of this attribute to use it without having to do
9749     // a const_cast.
9750     // This is a hack for us to be able to cache queries.
9751     auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9752     bool Result = NonConstThis->WholeFunction.isReachable(A, *NonConstThis,
9753                                                           {&AAEdges}, Fn);
9754 
9755     return Result;
9756   }
9757 
9758   /// Can \p CB reach \p Fn
9759   bool canReach(Attributor &A, CallBase &CB,
9760                 const Function &Fn) const override {
9761     if (!isValidState())
9762       return true;
9763 
9764     const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
9765         *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
9766 
9767     // Attributor returns attributes as const, so this function has to be
9768     // const for users of this attribute to use it without having to do
9769     // a const_cast.
9770     // This is a hack for us to be able to cache queries.
9771     auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9772     QueryResolver &CBQuery = NonConstThis->CBQueries[&CB];
9773 
9774     bool Result = CBQuery.isReachable(A, *NonConstThis, {&AAEdges}, Fn);
9775 
9776     return Result;
9777   }
9778 
9779   bool instructionCanReach(Attributor &A, const Instruction &Inst,
9780                            const Function &Fn,
9781                            bool UseBackwards) const override {
9782     if (!isValidState())
9783       return true;
9784 
9785     if (UseBackwards)
9786       return AA::isPotentiallyReachable(A, Inst, Fn, *this, nullptr);
9787 
9788     const auto &Reachability = A.getAAFor<AAReachability>(
9789         *this, IRPosition::function(*getAssociatedFunction()),
9790         DepClassTy::REQUIRED);
9791 
9792     SmallVector<const AACallEdges *> CallEdges;
9793     bool AllKnown = getReachableCallEdges(A, Reachability, Inst, CallEdges);
9794     // Attributor returns attributes as const, so this function has to be
9795     // const for users of this attribute to use it without having to do
9796     // a const_cast.
9797     // This is a hack for us to be able to cache queries.
9798     auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9799     QueryResolver &InstQSet = NonConstThis->InstQueries[&Inst];
9800     if (!AllKnown)
9801       InstQSet.CanReachUnknownCallee = true;
9802 
9803     return InstQSet.isReachable(A, *NonConstThis, CallEdges, Fn);
9804   }
9805 
9806   /// See AbstractAttribute::updateImpl(...).
9807   ChangeStatus updateImpl(Attributor &A) override {
9808     const AACallEdges &AAEdges =
9809         A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
9810     ChangeStatus Change = ChangeStatus::UNCHANGED;
9811 
9812     Change |= WholeFunction.update(A, *this, {&AAEdges});
9813 
9814     for (auto &CBPair : CBQueries) {
9815       const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
9816           *this, IRPosition::callsite_function(*CBPair.first),
9817           DepClassTy::REQUIRED);
9818 
9819       Change |= CBPair.second.update(A, *this, {&AAEdges});
9820     }
9821 
9822     // Update the Instruction queries.
9823     const AAReachability *Reachability;
9824     if (!InstQueries.empty()) {
9825       Reachability = &A.getAAFor<AAReachability>(
9826           *this, IRPosition::function(*getAssociatedFunction()),
9827           DepClassTy::REQUIRED);
9828     }
9829 
9830     // Check for local callbases first.
9831     for (auto &InstPair : InstQueries) {
9832       SmallVector<const AACallEdges *> CallEdges;
9833       bool AllKnown =
9834           getReachableCallEdges(A, *Reachability, *InstPair.first, CallEdges);
9835       // Update will return change if we this effects any queries.
9836       if (!AllKnown)
9837         InstPair.second.CanReachUnknownCallee = true;
9838       Change |= InstPair.second.update(A, *this, CallEdges);
9839     }
9840 
9841     return Change;
9842   }
9843 
9844   const std::string getAsStr() const override {
9845     size_t QueryCount =
9846         WholeFunction.Reachable.size() + WholeFunction.Unreachable.size();
9847 
9848     return "FunctionReachability [" +
9849            std::to_string(WholeFunction.Reachable.size()) + "," +
9850            std::to_string(QueryCount) + "]";
9851   }
9852 
9853   void trackStatistics() const override {}
9854 
9855 private:
9856   bool canReachUnknownCallee() const override {
9857     return WholeFunction.CanReachUnknownCallee;
9858   }
9859 
9860   /// Used to answer if a the whole function can reacha a specific function.
9861   QueryResolver WholeFunction;
9862 
9863   /// Used to answer if a call base inside this function can reach a specific
9864   /// function.
9865   DenseMap<const CallBase *, QueryResolver> CBQueries;
9866 
9867   /// This is for instruction queries than scan "forward".
9868   DenseMap<const Instruction *, QueryResolver> InstQueries;
9869 };
9870 
9871 /// ---------------------- Assumption Propagation ------------------------------
9872 struct AAAssumptionInfoImpl : public AAAssumptionInfo {
9873   AAAssumptionInfoImpl(const IRPosition &IRP, Attributor &A,
9874                        const DenseSet<StringRef> &Known)
9875       : AAAssumptionInfo(IRP, A, Known) {}
9876 
9877   bool hasAssumption(const StringRef Assumption) const override {
9878     return isValidState() && setContains(Assumption);
9879   }
9880 
9881   /// See AbstractAttribute::getAsStr()
9882   const std::string getAsStr() const override {
9883     const SetContents &Known = getKnown();
9884     const SetContents &Assumed = getAssumed();
9885 
9886     const std::string KnownStr =
9887         llvm::join(Known.getSet().begin(), Known.getSet().end(), ",");
9888     const std::string AssumedStr =
9889         (Assumed.isUniversal())
9890             ? "Universal"
9891             : llvm::join(Assumed.getSet().begin(), Assumed.getSet().end(), ",");
9892 
9893     return "Known [" + KnownStr + "]," + " Assumed [" + AssumedStr + "]";
9894   }
9895 };
9896 
9897 /// Propagates assumption information from parent functions to all of their
9898 /// successors. An assumption can be propagated if the containing function
9899 /// dominates the called function.
9900 ///
9901 /// We start with a "known" set of assumptions already valid for the associated
9902 /// function and an "assumed" set that initially contains all possible
9903 /// assumptions. The assumed set is inter-procedurally updated by narrowing its
9904 /// contents as concrete values are known. The concrete values are seeded by the
9905 /// first nodes that are either entries into the call graph, or contains no
9906 /// assumptions. Each node is updated as the intersection of the assumed state
9907 /// with all of its predecessors.
9908 struct AAAssumptionInfoFunction final : AAAssumptionInfoImpl {
9909   AAAssumptionInfoFunction(const IRPosition &IRP, Attributor &A)
9910       : AAAssumptionInfoImpl(IRP, A,
9911                              getAssumptions(*IRP.getAssociatedFunction())) {}
9912 
9913   /// See AbstractAttribute::manifest(...).
9914   ChangeStatus manifest(Attributor &A) override {
9915     const auto &Assumptions = getKnown();
9916 
9917     // Don't manifest a universal set if it somehow made it here.
9918     if (Assumptions.isUniversal())
9919       return ChangeStatus::UNCHANGED;
9920 
9921     Function *AssociatedFunction = getAssociatedFunction();
9922 
9923     bool Changed = addAssumptions(*AssociatedFunction, Assumptions.getSet());
9924 
9925     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
9926   }
9927 
9928   /// See AbstractAttribute::updateImpl(...).
9929   ChangeStatus updateImpl(Attributor &A) override {
9930     bool Changed = false;
9931 
9932     auto CallSitePred = [&](AbstractCallSite ACS) {
9933       const auto &AssumptionAA = A.getAAFor<AAAssumptionInfo>(
9934           *this, IRPosition::callsite_function(*ACS.getInstruction()),
9935           DepClassTy::REQUIRED);
9936       // Get the set of assumptions shared by all of this function's callers.
9937       Changed |= getIntersection(AssumptionAA.getAssumed());
9938       return !getAssumed().empty() || !getKnown().empty();
9939     };
9940 
9941     bool AllCallSitesKnown;
9942     // Get the intersection of all assumptions held by this node's predecessors.
9943     // If we don't know all the call sites then this is either an entry into the
9944     // call graph or an empty node. This node is known to only contain its own
9945     // assumptions and can be propagated to its successors.
9946     if (!A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown))
9947       return indicatePessimisticFixpoint();
9948 
9949     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
9950   }
9951 
9952   void trackStatistics() const override {}
9953 };
9954 
9955 /// Assumption Info defined for call sites.
9956 struct AAAssumptionInfoCallSite final : AAAssumptionInfoImpl {
9957 
9958   AAAssumptionInfoCallSite(const IRPosition &IRP, Attributor &A)
9959       : AAAssumptionInfoImpl(IRP, A, getInitialAssumptions(IRP)) {}
9960 
9961   /// See AbstractAttribute::initialize(...).
9962   void initialize(Attributor &A) override {
9963     const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
9964     A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
9965   }
9966 
9967   /// See AbstractAttribute::manifest(...).
9968   ChangeStatus manifest(Attributor &A) override {
9969     // Don't manifest a universal set if it somehow made it here.
9970     if (getKnown().isUniversal())
9971       return ChangeStatus::UNCHANGED;
9972 
9973     CallBase &AssociatedCall = cast<CallBase>(getAssociatedValue());
9974     bool Changed = addAssumptions(AssociatedCall, getAssumed().getSet());
9975 
9976     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
9977   }
9978 
9979   /// See AbstractAttribute::updateImpl(...).
9980   ChangeStatus updateImpl(Attributor &A) override {
9981     const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
9982     auto &AssumptionAA =
9983         A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
9984     bool Changed = getIntersection(AssumptionAA.getAssumed());
9985     return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
9986   }
9987 
9988   /// See AbstractAttribute::trackStatistics()
9989   void trackStatistics() const override {}
9990 
9991 private:
9992   /// Helper to initialized the known set as all the assumptions this call and
9993   /// the callee contain.
9994   DenseSet<StringRef> getInitialAssumptions(const IRPosition &IRP) {
9995     const CallBase &CB = cast<CallBase>(IRP.getAssociatedValue());
9996     auto Assumptions = getAssumptions(CB);
9997     if (Function *F = IRP.getAssociatedFunction())
9998       set_union(Assumptions, getAssumptions(*F));
9999     if (Function *F = IRP.getAssociatedFunction())
10000       set_union(Assumptions, getAssumptions(*F));
10001     return Assumptions;
10002   }
10003 };
10004 
10005 AACallGraphNode *AACallEdgeIterator::operator*() const {
10006   return static_cast<AACallGraphNode *>(const_cast<AACallEdges *>(
10007       &A.getOrCreateAAFor<AACallEdges>(IRPosition::function(**I))));
10008 }
10009 
10010 void AttributorCallGraph::print() { llvm::WriteGraph(outs(), this); }
10011 
10012 const char AAReturnedValues::ID = 0;
10013 const char AANoUnwind::ID = 0;
10014 const char AANoSync::ID = 0;
10015 const char AANoFree::ID = 0;
10016 const char AANonNull::ID = 0;
10017 const char AANoRecurse::ID = 0;
10018 const char AAWillReturn::ID = 0;
10019 const char AAUndefinedBehavior::ID = 0;
10020 const char AANoAlias::ID = 0;
10021 const char AAReachability::ID = 0;
10022 const char AANoReturn::ID = 0;
10023 const char AAIsDead::ID = 0;
10024 const char AADereferenceable::ID = 0;
10025 const char AAAlign::ID = 0;
10026 const char AANoCapture::ID = 0;
10027 const char AAValueSimplify::ID = 0;
10028 const char AAHeapToStack::ID = 0;
10029 const char AAPrivatizablePtr::ID = 0;
10030 const char AAMemoryBehavior::ID = 0;
10031 const char AAMemoryLocation::ID = 0;
10032 const char AAValueConstantRange::ID = 0;
10033 const char AAPotentialValues::ID = 0;
10034 const char AANoUndef::ID = 0;
10035 const char AACallEdges::ID = 0;
10036 const char AAFunctionReachability::ID = 0;
10037 const char AAPointerInfo::ID = 0;
10038 const char AAAssumptionInfo::ID = 0;
10039 
10040 // Macro magic to create the static generator function for attributes that
10041 // follow the naming scheme.
10042 
10043 #define SWITCH_PK_INV(CLASS, PK, POS_NAME)                                     \
10044   case IRPosition::PK:                                                         \
10045     llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
10046 
10047 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX)                               \
10048   case IRPosition::PK:                                                         \
10049     AA = new (A.Allocator) CLASS##SUFFIX(IRP, A);                              \
10050     ++NumAAs;                                                                  \
10051     break;
10052 
10053 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                 \
10054   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10055     CLASS *AA = nullptr;                                                       \
10056     switch (IRP.getPositionKind()) {                                           \
10057       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10058       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
10059       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
10060       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
10061       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
10062       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
10063       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10064       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
10065     }                                                                          \
10066     return *AA;                                                                \
10067   }
10068 
10069 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                    \
10070   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10071     CLASS *AA = nullptr;                                                       \
10072     switch (IRP.getPositionKind()) {                                           \
10073       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10074       SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function")                           \
10075       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
10076       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
10077       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
10078       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
10079       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
10080       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
10081     }                                                                          \
10082     return *AA;                                                                \
10083   }
10084 
10085 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                      \
10086   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10087     CLASS *AA = nullptr;                                                       \
10088     switch (IRP.getPositionKind()) {                                           \
10089       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10090       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10091       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
10092       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
10093       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
10094       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
10095       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
10096       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
10097     }                                                                          \
10098     return *AA;                                                                \
10099   }
10100 
10101 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)            \
10102   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10103     CLASS *AA = nullptr;                                                       \
10104     switch (IRP.getPositionKind()) {                                           \
10105       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10106       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
10107       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
10108       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
10109       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
10110       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
10111       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
10112       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10113     }                                                                          \
10114     return *AA;                                                                \
10115   }
10116 
10117 #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                  \
10118   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
10119     CLASS *AA = nullptr;                                                       \
10120     switch (IRP.getPositionKind()) {                                           \
10121       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
10122       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
10123       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
10124       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
10125       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
10126       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
10127       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
10128       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
10129     }                                                                          \
10130     return *AA;                                                                \
10131   }
10132 
10133 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
10134 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
10135 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
10136 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
10137 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
10138 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
10139 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation)
10140 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AACallEdges)
10141 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAssumptionInfo)
10142 
10143 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
10144 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
10145 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
10146 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
10147 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
10148 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
10149 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
10150 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialValues)
10151 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUndef)
10152 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPointerInfo)
10153 
10154 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
10155 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
10156 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
10157 
10158 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
10159 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
10160 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
10161 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAFunctionReachability)
10162 
10163 CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
10164 
10165 #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
10166 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
10167 #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
10168 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
10169 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
10170 #undef SWITCH_PK_CREATE
10171 #undef SWITCH_PK_INV
10172