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