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