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