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