1 //===- Attributor.cpp - Module-wide attribute 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 // This file implements an inter procedural pass that deduces and/or propagating
10 // attributes. This is done in an abstract interpretation style fixpoint
11 // iteration. See the Attributor.h file comment and the class descriptions in
12 // that file for more information.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Transforms/IPO/Attributor.h"
17 
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/CaptureTracking.h"
24 #include "llvm/Analysis/EHPersonalities.h"
25 #include "llvm/Analysis/GlobalsModRef.h"
26 #include "llvm/Analysis/Loads.h"
27 #include "llvm/Analysis/MemoryBuiltins.h"
28 #include "llvm/Analysis/ValueTracking.h"
29 #include "llvm/IR/Argument.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/CFG.h"
32 #include "llvm/IR/InstIterator.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
38 #include "llvm/Transforms/Utils/Local.h"
39 
40 #include <cassert>
41 
42 using namespace llvm;
43 
44 #define DEBUG_TYPE "attributor"
45 
46 STATISTIC(NumFnWithExactDefinition,
47           "Number of function with exact definitions");
48 STATISTIC(NumFnWithoutExactDefinition,
49           "Number of function without exact definitions");
50 STATISTIC(NumAttributesTimedOut,
51           "Number of abstract attributes timed out before fixpoint");
52 STATISTIC(NumAttributesValidFixpoint,
53           "Number of abstract attributes in a valid fixpoint state");
54 STATISTIC(NumAttributesManifested,
55           "Number of abstract attributes manifested in IR");
56 
57 // Some helper macros to deal with statistics tracking.
58 //
59 // Usage:
60 // For simple IR attribute tracking overload trackStatistics in the abstract
61 // attribute and choose the right STATS_DECLTRACK_********* macro,
62 // e.g.,:
63 //  void trackStatistics() const override {
64 //    STATS_DECLTRACK_ARG_ATTR(returned)
65 //  }
66 // If there is a single "increment" side one can use the macro
67 // STATS_DECLTRACK with a custom message. If there are multiple increment
68 // sides, STATS_DECL and STATS_TRACK can also be used separatly.
69 //
70 #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)                                     \
71   ("Number of " #TYPE " marked '" #NAME "'")
72 #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
73 #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
74 #define STATS_DECL(NAME, TYPE, MSG)                                            \
75   STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
76 #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
77 #define STATS_DECLTRACK(NAME, TYPE, MSG)                                       \
78   {                                                                            \
79     STATS_DECL(NAME, TYPE, MSG)                                                \
80     STATS_TRACK(NAME, TYPE)                                                    \
81   }
82 #define STATS_DECLTRACK_ARG_ATTR(NAME)                                         \
83   STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
84 #define STATS_DECLTRACK_CSARG_ATTR(NAME)                                       \
85   STATS_DECLTRACK(NAME, CSArguments,                                           \
86                   BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
87 #define STATS_DECLTRACK_FN_ATTR(NAME)                                          \
88   STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
89 #define STATS_DECLTRACK_CS_ATTR(NAME)                                          \
90   STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
91 #define STATS_DECLTRACK_FNRET_ATTR(NAME)                                       \
92   STATS_DECLTRACK(NAME, FunctionReturn,                                        \
93                   BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
94 #define STATS_DECLTRACK_CSRET_ATTR(NAME)                                       \
95   STATS_DECLTRACK(NAME, CSReturn,                                              \
96                   BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
97 #define STATS_DECLTRACK_FLOATING_ATTR(NAME)                                    \
98   STATS_DECLTRACK(NAME, Floating,                                              \
99                   ("Number of floating values known to be '" #NAME "'"))
100 
101 // TODO: Determine a good default value.
102 //
103 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
104 // (when run with the first 5 abstract attributes). The results also indicate
105 // that we never reach 32 iterations but always find a fixpoint sooner.
106 //
107 // This will become more evolved once we perform two interleaved fixpoint
108 // iterations: bottom-up and top-down.
109 static cl::opt<unsigned>
110     MaxFixpointIterations("attributor-max-iterations", cl::Hidden,
111                           cl::desc("Maximal number of fixpoint iterations."),
112                           cl::init(32));
113 static cl::opt<bool> VerifyMaxFixpointIterations(
114     "attributor-max-iterations-verify", cl::Hidden,
115     cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
116     cl::init(false));
117 
118 static cl::opt<bool> DisableAttributor(
119     "attributor-disable", cl::Hidden,
120     cl::desc("Disable the attributor inter-procedural deduction pass."),
121     cl::init(true));
122 
123 static cl::opt<bool> ManifestInternal(
124     "attributor-manifest-internal", cl::Hidden,
125     cl::desc("Manifest Attributor internal string attributes."),
126     cl::init(false));
127 
128 static cl::opt<unsigned> DepRecInterval(
129     "attributor-dependence-recompute-interval", cl::Hidden,
130     cl::desc("Number of iterations until dependences are recomputed."),
131     cl::init(4));
132 
133 static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
134                                        cl::init(true), cl::Hidden);
135 
136 static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
137                                        cl::Hidden);
138 
139 /// Logic operators for the change status enum class.
140 ///
141 ///{
142 ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) {
143   return l == ChangeStatus::CHANGED ? l : r;
144 }
145 ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) {
146   return l == ChangeStatus::UNCHANGED ? l : r;
147 }
148 ///}
149 
150 /// Recursively visit all values that might become \p IRP at some point. This
151 /// will be done by looking through cast instructions, selects, phis, and calls
152 /// with the "returned" attribute. Once we cannot look through the value any
153 /// further, the callback \p VisitValueCB is invoked and passed the current
154 /// value, the \p State, and a flag to indicate if we stripped anything. To
155 /// limit how much effort is invested, we will never visit more values than
156 /// specified by \p MaxValues.
157 template <typename AAType, typename StateTy>
158 static bool genericValueTraversal(
159     Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State,
160     const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB,
161     int MaxValues = 8) {
162 
163   const AAIsDead *LivenessAA = nullptr;
164   if (IRP.getAnchorScope())
165     LivenessAA = &A.getAAFor<AAIsDead>(
166         QueryingAA, IRPosition::function(*IRP.getAnchorScope()),
167         /* TrackDependence */ false);
168   bool AnyDead = false;
169 
170   // TODO: Use Positions here to allow context sensitivity in VisitValueCB
171   SmallPtrSet<Value *, 16> Visited;
172   SmallVector<Value *, 16> Worklist;
173   Worklist.push_back(&IRP.getAssociatedValue());
174 
175   int Iteration = 0;
176   do {
177     Value *V = Worklist.pop_back_val();
178 
179     // Check if we should process the current value. To prevent endless
180     // recursion keep a record of the values we followed!
181     if (!Visited.insert(V).second)
182       continue;
183 
184     // Make sure we limit the compile time for complex expressions.
185     if (Iteration++ >= MaxValues)
186       return false;
187 
188     // Explicitly look through calls with a "returned" attribute if we do
189     // not have a pointer as stripPointerCasts only works on them.
190     Value *NewV = nullptr;
191     if (V->getType()->isPointerTy()) {
192       NewV = V->stripPointerCasts();
193     } else {
194       CallSite CS(V);
195       if (CS && CS.getCalledFunction()) {
196         for (Argument &Arg : CS.getCalledFunction()->args())
197           if (Arg.hasReturnedAttr()) {
198             NewV = CS.getArgOperand(Arg.getArgNo());
199             break;
200           }
201       }
202     }
203     if (NewV && NewV != V) {
204       Worklist.push_back(NewV);
205       continue;
206     }
207 
208     // Look through select instructions, visit both potential values.
209     if (auto *SI = dyn_cast<SelectInst>(V)) {
210       Worklist.push_back(SI->getTrueValue());
211       Worklist.push_back(SI->getFalseValue());
212       continue;
213     }
214 
215     // Look through phi nodes, visit all live operands.
216     if (auto *PHI = dyn_cast<PHINode>(V)) {
217       assert(LivenessAA &&
218              "Expected liveness in the presence of instructions!");
219       for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
220         const BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
221         if (LivenessAA->isAssumedDead(IncomingBB->getTerminator())) {
222           AnyDead = true;
223           continue;
224         }
225         Worklist.push_back(PHI->getIncomingValue(u));
226       }
227       continue;
228     }
229 
230     // Once a leaf is reached we inform the user through the callback.
231     if (!VisitValueCB(*V, State, Iteration > 1))
232       return false;
233   } while (!Worklist.empty());
234 
235   // If we actually used liveness information so we have to record a dependence.
236   if (AnyDead)
237     A.recordDependence(*LivenessAA, QueryingAA);
238 
239   // All values have been visited.
240   return true;
241 }
242 
243 /// Return true if \p New is equal or worse than \p Old.
244 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
245   if (!Old.isIntAttribute())
246     return true;
247 
248   return Old.getValueAsInt() >= New.getValueAsInt();
249 }
250 
251 /// Return true if the information provided by \p Attr was added to the
252 /// attribute list \p Attrs. This is only the case if it was not already present
253 /// in \p Attrs at the position describe by \p PK and \p AttrIdx.
254 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
255                              AttributeList &Attrs, int AttrIdx) {
256 
257   if (Attr.isEnumAttribute()) {
258     Attribute::AttrKind Kind = Attr.getKindAsEnum();
259     if (Attrs.hasAttribute(AttrIdx, Kind))
260       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
261         return false;
262     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
263     return true;
264   }
265   if (Attr.isStringAttribute()) {
266     StringRef Kind = Attr.getKindAsString();
267     if (Attrs.hasAttribute(AttrIdx, Kind))
268       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
269         return false;
270     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
271     return true;
272   }
273   if (Attr.isIntAttribute()) {
274     Attribute::AttrKind Kind = Attr.getKindAsEnum();
275     if (Attrs.hasAttribute(AttrIdx, Kind))
276       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
277         return false;
278     Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind);
279     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
280     return true;
281   }
282 
283   llvm_unreachable("Expected enum or string attribute!");
284 }
285 static const Value *getPointerOperand(const Instruction *I) {
286   if (auto *LI = dyn_cast<LoadInst>(I))
287     if (!LI->isVolatile())
288       return LI->getPointerOperand();
289 
290   if (auto *SI = dyn_cast<StoreInst>(I))
291     if (!SI->isVolatile())
292       return SI->getPointerOperand();
293 
294   if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I))
295     if (!CXI->isVolatile())
296       return CXI->getPointerOperand();
297 
298   if (auto *RMWI = dyn_cast<AtomicRMWInst>(I))
299     if (!RMWI->isVolatile())
300       return RMWI->getPointerOperand();
301 
302   return nullptr;
303 }
304 static const Value *getBasePointerOfAccessPointerOperand(const Instruction *I,
305                                                          int64_t &BytesOffset,
306                                                          const DataLayout &DL) {
307   const Value *Ptr = getPointerOperand(I);
308   if (!Ptr)
309     return nullptr;
310 
311   return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL,
312                                           /*AllowNonInbounds*/ false);
313 }
314 
315 ChangeStatus AbstractAttribute::update(Attributor &A) {
316   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
317   if (getState().isAtFixpoint())
318     return HasChanged;
319 
320   LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
321 
322   HasChanged = updateImpl(A);
323 
324   LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
325                     << "\n");
326 
327   return HasChanged;
328 }
329 
330 ChangeStatus
331 IRAttributeManifest::manifestAttrs(Attributor &A, IRPosition &IRP,
332                                    const ArrayRef<Attribute> &DeducedAttrs) {
333   Function *ScopeFn = IRP.getAssociatedFunction();
334   IRPosition::Kind PK = IRP.getPositionKind();
335 
336   // In the following some generic code that will manifest attributes in
337   // DeducedAttrs if they improve the current IR. Due to the different
338   // annotation positions we use the underlying AttributeList interface.
339 
340   AttributeList Attrs;
341   switch (PK) {
342   case IRPosition::IRP_INVALID:
343   case IRPosition::IRP_FLOAT:
344     return ChangeStatus::UNCHANGED;
345   case IRPosition::IRP_ARGUMENT:
346   case IRPosition::IRP_FUNCTION:
347   case IRPosition::IRP_RETURNED:
348     Attrs = ScopeFn->getAttributes();
349     break;
350   case IRPosition::IRP_CALL_SITE:
351   case IRPosition::IRP_CALL_SITE_RETURNED:
352   case IRPosition::IRP_CALL_SITE_ARGUMENT:
353     Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes();
354     break;
355   }
356 
357   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
358   LLVMContext &Ctx = IRP.getAnchorValue().getContext();
359   for (const Attribute &Attr : DeducedAttrs) {
360     if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx()))
361       continue;
362 
363     HasChanged = ChangeStatus::CHANGED;
364   }
365 
366   if (HasChanged == ChangeStatus::UNCHANGED)
367     return HasChanged;
368 
369   switch (PK) {
370   case IRPosition::IRP_ARGUMENT:
371   case IRPosition::IRP_FUNCTION:
372   case IRPosition::IRP_RETURNED:
373     ScopeFn->setAttributes(Attrs);
374     break;
375   case IRPosition::IRP_CALL_SITE:
376   case IRPosition::IRP_CALL_SITE_RETURNED:
377   case IRPosition::IRP_CALL_SITE_ARGUMENT:
378     CallSite(&IRP.getAnchorValue()).setAttributes(Attrs);
379     break;
380   case IRPosition::IRP_INVALID:
381   case IRPosition::IRP_FLOAT:
382     break;
383   }
384 
385   return HasChanged;
386 }
387 
388 const IRPosition IRPosition::EmptyKey(255);
389 const IRPosition IRPosition::TombstoneKey(256);
390 
391 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
392   IRPositions.emplace_back(IRP);
393 
394   ImmutableCallSite ICS(&IRP.getAnchorValue());
395   switch (IRP.getPositionKind()) {
396   case IRPosition::IRP_INVALID:
397   case IRPosition::IRP_FLOAT:
398   case IRPosition::IRP_FUNCTION:
399     return;
400   case IRPosition::IRP_ARGUMENT:
401   case IRPosition::IRP_RETURNED:
402     IRPositions.emplace_back(
403         IRPosition::function(*IRP.getAssociatedFunction()));
404     return;
405   case IRPosition::IRP_CALL_SITE:
406     assert(ICS && "Expected call site!");
407     // TODO: We need to look at the operand bundles similar to the redirection
408     //       in CallBase.
409     if (!ICS.hasOperandBundles())
410       if (const Function *Callee = ICS.getCalledFunction())
411         IRPositions.emplace_back(IRPosition::function(*Callee));
412     return;
413   case IRPosition::IRP_CALL_SITE_RETURNED:
414     assert(ICS && "Expected call site!");
415     // TODO: We need to look at the operand bundles similar to the redirection
416     //       in CallBase.
417     if (!ICS.hasOperandBundles()) {
418       if (const Function *Callee = ICS.getCalledFunction()) {
419         IRPositions.emplace_back(IRPosition::returned(*Callee));
420         IRPositions.emplace_back(IRPosition::function(*Callee));
421       }
422     }
423     IRPositions.emplace_back(
424         IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction())));
425     return;
426   case IRPosition::IRP_CALL_SITE_ARGUMENT: {
427     int ArgNo = IRP.getArgNo();
428     assert(ICS && ArgNo >= 0 && "Expected call site!");
429     // TODO: We need to look at the operand bundles similar to the redirection
430     //       in CallBase.
431     if (!ICS.hasOperandBundles()) {
432       const Function *Callee = ICS.getCalledFunction();
433       if (Callee && Callee->arg_size() > unsigned(ArgNo))
434         IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo)));
435       if (Callee)
436         IRPositions.emplace_back(IRPosition::function(*Callee));
437     }
438     IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
439     return;
440   }
441   }
442 }
443 
444 bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs,
445                          bool IgnoreSubsumingPositions) const {
446   for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
447     for (Attribute::AttrKind AK : AKs)
448       if (EquivIRP.getAttr(AK).getKindAsEnum() == AK)
449         return true;
450     // The first position returned by the SubsumingPositionIterator is
451     // always the position itself. If we ignore subsuming positions we
452     // are done after the first iteration.
453     if (IgnoreSubsumingPositions)
454       break;
455   }
456   return false;
457 }
458 
459 void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
460                           SmallVectorImpl<Attribute> &Attrs) const {
461   for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this))
462     for (Attribute::AttrKind AK : AKs) {
463       const Attribute &Attr = EquivIRP.getAttr(AK);
464       if (Attr.getKindAsEnum() == AK)
465         Attrs.push_back(Attr);
466     }
467 }
468 
469 void IRPosition::verify() {
470   switch (KindOrArgNo) {
471   default:
472     assert(KindOrArgNo >= 0 && "Expected argument or call site argument!");
473     assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) &&
474            "Expected call base or argument for positive attribute index!");
475     if (isa<Argument>(AnchorVal)) {
476       assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) &&
477              "Argument number mismatch!");
478       assert(cast<Argument>(AnchorVal) == &getAssociatedValue() &&
479              "Associated value mismatch!");
480     } else {
481       assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) &&
482              "Call site argument number mismatch!");
483       assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) ==
484                  &getAssociatedValue() &&
485              "Associated value mismatch!");
486     }
487     break;
488   case IRP_INVALID:
489     assert(!AnchorVal && "Expected no value for an invalid position!");
490     break;
491   case IRP_FLOAT:
492     assert((!isa<CallBase>(&getAssociatedValue()) &&
493             !isa<Argument>(&getAssociatedValue())) &&
494            "Expected specialized kind for call base and argument values!");
495     break;
496   case IRP_RETURNED:
497     assert(isa<Function>(AnchorVal) &&
498            "Expected function for a 'returned' position!");
499     assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
500     break;
501   case IRP_CALL_SITE_RETURNED:
502     assert((isa<CallBase>(AnchorVal)) &&
503            "Expected call base for 'call site returned' position!");
504     assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
505     break;
506   case IRP_CALL_SITE:
507     assert((isa<CallBase>(AnchorVal)) &&
508            "Expected call base for 'call site function' position!");
509     assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
510     break;
511   case IRP_FUNCTION:
512     assert(isa<Function>(AnchorVal) &&
513            "Expected function for a 'function' position!");
514     assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
515     break;
516   }
517 }
518 
519 namespace {
520 /// Helper function to clamp a state \p S of type \p StateType with the
521 /// information in \p R and indicate/return if \p S did change (as-in update is
522 /// required to be run again).
523 template <typename StateType>
524 ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
525   auto Assumed = S.getAssumed();
526   S ^= R;
527   return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
528                                    : ChangeStatus::CHANGED;
529 }
530 
531 /// Clamp the information known for all returned values of a function
532 /// (identified by \p QueryingAA) into \p S.
533 template <typename AAType, typename StateType = typename AAType::StateType>
534 static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA,
535                                      StateType &S) {
536   LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
537                     << static_cast<const AbstractAttribute &>(QueryingAA)
538                     << " into " << S << "\n");
539 
540   assert((QueryingAA.getIRPosition().getPositionKind() ==
541               IRPosition::IRP_RETURNED ||
542           QueryingAA.getIRPosition().getPositionKind() ==
543               IRPosition::IRP_CALL_SITE_RETURNED) &&
544          "Can only clamp returned value states for a function returned or call "
545          "site returned position!");
546 
547   // Use an optional state as there might not be any return values and we want
548   // to join (IntegerState::operator&) the state of all there are.
549   Optional<StateType> T;
550 
551   // Callback for each possibly returned value.
552   auto CheckReturnValue = [&](Value &RV) -> bool {
553     const IRPosition &RVPos = IRPosition::value(RV);
554     const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos);
555     LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
556                       << " @ " << RVPos << "\n");
557     const StateType &AAS = static_cast<const StateType &>(AA.getState());
558     if (T.hasValue())
559       *T &= AAS;
560     else
561       T = AAS;
562     LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
563                       << "\n");
564     return T->isValidState();
565   };
566 
567   if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
568     S.indicatePessimisticFixpoint();
569   else if (T.hasValue())
570     S ^= *T;
571 }
572 
573 /// Helper class to compose two generic deduction
574 template <typename AAType, typename Base, typename StateType,
575           template <typename...> class F, template <typename...> class G>
576 struct AAComposeTwoGenericDeduction
577     : public F<AAType, G<AAType, Base, StateType>, StateType> {
578   AAComposeTwoGenericDeduction(const IRPosition &IRP)
579       : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {}
580 
581   /// See AbstractAttribute::updateImpl(...).
582   ChangeStatus updateImpl(Attributor &A) override {
583     ChangeStatus ChangedF = F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A);
584     ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A);
585     return ChangedF | ChangedG;
586   }
587 };
588 
589 /// Helper class for generic deduction: return value -> returned position.
590 template <typename AAType, typename Base,
591           typename StateType = typename AAType::StateType>
592 struct AAReturnedFromReturnedValues : public Base {
593   AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {}
594 
595   /// See AbstractAttribute::updateImpl(...).
596   ChangeStatus updateImpl(Attributor &A) override {
597     StateType S;
598     clampReturnedValueStates<AAType, StateType>(A, *this, S);
599     // TODO: If we know we visited all returned values, thus no are assumed
600     // dead, we can take the known information from the state T.
601     return clampStateAndIndicateChange<StateType>(this->getState(), S);
602   }
603 };
604 
605 /// Clamp the information known at all call sites for a given argument
606 /// (identified by \p QueryingAA) into \p S.
607 template <typename AAType, typename StateType = typename AAType::StateType>
608 static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
609                                         StateType &S) {
610   LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
611                     << static_cast<const AbstractAttribute &>(QueryingAA)
612                     << " into " << S << "\n");
613 
614   assert(QueryingAA.getIRPosition().getPositionKind() ==
615              IRPosition::IRP_ARGUMENT &&
616          "Can only clamp call site argument states for an argument position!");
617 
618   // Use an optional state as there might not be any return values and we want
619   // to join (IntegerState::operator&) the state of all there are.
620   Optional<StateType> T;
621 
622   // The argument number which is also the call site argument number.
623   unsigned ArgNo = QueryingAA.getIRPosition().getArgNo();
624 
625   auto CallSiteCheck = [&](AbstractCallSite ACS) {
626     const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
627     // Check if a coresponding argument was found or if it is on not associated
628     // (which can happen for callback calls).
629     if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
630       return false;
631 
632     const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos);
633     LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()
634                       << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n");
635     const StateType &AAS = static_cast<const StateType &>(AA.getState());
636     if (T.hasValue())
637       *T &= AAS;
638     else
639       T = AAS;
640     LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
641                       << "\n");
642     return T->isValidState();
643   };
644 
645   if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true))
646     S.indicatePessimisticFixpoint();
647   else if (T.hasValue())
648     S ^= *T;
649 }
650 
651 /// Helper class for generic deduction: call site argument -> argument position.
652 template <typename AAType, typename Base,
653           typename StateType = typename AAType::StateType>
654 struct AAArgumentFromCallSiteArguments : public Base {
655   AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {}
656 
657   /// See AbstractAttribute::updateImpl(...).
658   ChangeStatus updateImpl(Attributor &A) override {
659     StateType S;
660     clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
661     // TODO: If we know we visited all incoming values, thus no are assumed
662     // dead, we can take the known information from the state T.
663     return clampStateAndIndicateChange<StateType>(this->getState(), S);
664   }
665 };
666 
667 /// Helper class for generic replication: function returned -> cs returned.
668 template <typename AAType, typename Base,
669           typename StateType = typename AAType::StateType>
670 struct AACallSiteReturnedFromReturned : public Base {
671   AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {}
672 
673   /// See AbstractAttribute::updateImpl(...).
674   ChangeStatus updateImpl(Attributor &A) override {
675     assert(this->getIRPosition().getPositionKind() ==
676                IRPosition::IRP_CALL_SITE_RETURNED &&
677            "Can only wrap function returned positions for call site returned "
678            "positions!");
679     auto &S = this->getState();
680 
681     const Function *AssociatedFunction =
682         this->getIRPosition().getAssociatedFunction();
683     if (!AssociatedFunction)
684       return S.indicatePessimisticFixpoint();
685 
686     IRPosition FnPos = IRPosition::returned(*AssociatedFunction);
687     const AAType &AA = A.getAAFor<AAType>(*this, FnPos);
688     return clampStateAndIndicateChange(
689         S, static_cast<const typename AAType::StateType &>(AA.getState()));
690   }
691 };
692 
693 /// Helper class for generic deduction using must-be-executed-context
694 /// Base class is required to have `followUse` method.
695 
696 /// bool followUse(Attributor &A, const Use *U, const Instruction *I)
697 /// U - Underlying use.
698 /// I - The user of the \p U.
699 /// `followUse` returns true if the value should be tracked transitively.
700 
701 template <typename AAType, typename Base,
702           typename StateType = typename AAType::StateType>
703 struct AAFromMustBeExecutedContext : public Base {
704   AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {}
705 
706   void initialize(Attributor &A) override {
707     Base::initialize(A);
708     IRPosition &IRP = this->getIRPosition();
709     Instruction *CtxI = IRP.getCtxI();
710 
711     if (!CtxI)
712       return;
713 
714     for (const Use &U : IRP.getAssociatedValue().uses())
715       Uses.insert(&U);
716   }
717 
718   /// See AbstractAttribute::updateImpl(...).
719   ChangeStatus updateImpl(Attributor &A) override {
720     auto BeforeState = this->getState();
721     auto &S = this->getState();
722     Instruction *CtxI = this->getIRPosition().getCtxI();
723     if (!CtxI)
724       return ChangeStatus::UNCHANGED;
725 
726     MustBeExecutedContextExplorer &Explorer =
727         A.getInfoCache().getMustBeExecutedContextExplorer();
728 
729     SetVector<const Use *> NextUses;
730 
731     for (const Use *U : Uses) {
732       if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
733         auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
734         bool Found = EIt.count(UserI);
735         while (!Found && ++EIt != EEnd)
736           Found = EIt.getCurrentInst() == UserI;
737         if (Found && Base::followUse(A, U, UserI))
738           for (const Use &Us : UserI->uses())
739             NextUses.insert(&Us);
740       }
741     }
742     for (const Use *U : NextUses)
743       Uses.insert(U);
744 
745     return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED;
746   }
747 
748 private:
749   /// Container for (transitive) uses of the associated value.
750   SetVector<const Use *> Uses;
751 };
752 
753 template <typename AAType, typename Base,
754           typename StateType = typename AAType::StateType>
755 using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext =
756     AAComposeTwoGenericDeduction<AAType, Base, StateType,
757                                  AAFromMustBeExecutedContext,
758                                  AAArgumentFromCallSiteArguments>;
759 
760 template <typename AAType, typename Base,
761           typename StateType = typename AAType::StateType>
762 using AACallSiteReturnedFromReturnedAndMustBeExecutedContext =
763     AAComposeTwoGenericDeduction<AAType, Base, StateType,
764                                  AAFromMustBeExecutedContext,
765                                  AACallSiteReturnedFromReturned>;
766 
767 /// -----------------------NoUnwind Function Attribute--------------------------
768 
769 struct AANoUnwindImpl : AANoUnwind {
770   AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {}
771 
772   const std::string getAsStr() const override {
773     return getAssumed() ? "nounwind" : "may-unwind";
774   }
775 
776   /// See AbstractAttribute::updateImpl(...).
777   ChangeStatus updateImpl(Attributor &A) override {
778     auto Opcodes = {
779         (unsigned)Instruction::Invoke,      (unsigned)Instruction::CallBr,
780         (unsigned)Instruction::Call,        (unsigned)Instruction::CleanupRet,
781         (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
782 
783     auto CheckForNoUnwind = [&](Instruction &I) {
784       if (!I.mayThrow())
785         return true;
786 
787       if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
788         const auto &NoUnwindAA =
789             A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS));
790         return NoUnwindAA.isAssumedNoUnwind();
791       }
792       return false;
793     };
794 
795     if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes))
796       return indicatePessimisticFixpoint();
797 
798     return ChangeStatus::UNCHANGED;
799   }
800 };
801 
802 struct AANoUnwindFunction final : public AANoUnwindImpl {
803   AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
804 
805   /// See AbstractAttribute::trackStatistics()
806   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
807 };
808 
809 /// NoUnwind attribute deduction for a call sites.
810 struct AANoUnwindCallSite final : AANoUnwindImpl {
811   AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
812 
813   /// See AbstractAttribute::initialize(...).
814   void initialize(Attributor &A) override {
815     AANoUnwindImpl::initialize(A);
816     Function *F = getAssociatedFunction();
817     if (!F)
818       indicatePessimisticFixpoint();
819   }
820 
821   /// See AbstractAttribute::updateImpl(...).
822   ChangeStatus updateImpl(Attributor &A) override {
823     // TODO: Once we have call site specific value information we can provide
824     //       call site specific liveness information and then it makes
825     //       sense to specialize attributes for call sites arguments instead of
826     //       redirecting requests to the callee argument.
827     Function *F = getAssociatedFunction();
828     const IRPosition &FnPos = IRPosition::function(*F);
829     auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos);
830     return clampStateAndIndicateChange(
831         getState(),
832         static_cast<const AANoUnwind::StateType &>(FnAA.getState()));
833   }
834 
835   /// See AbstractAttribute::trackStatistics()
836   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
837 };
838 
839 /// --------------------- Function Return Values -------------------------------
840 
841 /// "Attribute" that collects all potential returned values and the return
842 /// instructions that they arise from.
843 ///
844 /// If there is a unique returned value R, the manifest method will:
845 ///   - mark R with the "returned" attribute, if R is an argument.
846 class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
847 
848   /// Mapping of values potentially returned by the associated function to the
849   /// return instructions that might return them.
850   MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
851 
852   /// Mapping to remember the number of returned values for a call site such
853   /// that we can avoid updates if nothing changed.
854   DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA;
855 
856   /// Set of unresolved calls returned by the associated function.
857   SmallSetVector<CallBase *, 4> UnresolvedCalls;
858 
859   /// State flags
860   ///
861   ///{
862   bool IsFixed = false;
863   bool IsValidState = true;
864   ///}
865 
866 public:
867   AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {}
868 
869   /// See AbstractAttribute::initialize(...).
870   void initialize(Attributor &A) override {
871     // Reset the state.
872     IsFixed = false;
873     IsValidState = true;
874     ReturnedValues.clear();
875 
876     Function *F = getAssociatedFunction();
877     if (!F) {
878       indicatePessimisticFixpoint();
879       return;
880     }
881 
882     // The map from instruction opcodes to those instructions in the function.
883     auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
884 
885     // Look through all arguments, if one is marked as returned we are done.
886     for (Argument &Arg : F->args()) {
887       if (Arg.hasReturnedAttr()) {
888         auto &ReturnInstSet = ReturnedValues[&Arg];
889         for (Instruction *RI : OpcodeInstMap[Instruction::Ret])
890           ReturnInstSet.insert(cast<ReturnInst>(RI));
891 
892         indicateOptimisticFixpoint();
893         return;
894       }
895     }
896 
897     if (!F->hasExactDefinition())
898       indicatePessimisticFixpoint();
899   }
900 
901   /// See AbstractAttribute::manifest(...).
902   ChangeStatus manifest(Attributor &A) override;
903 
904   /// See AbstractAttribute::getState(...).
905   AbstractState &getState() override { return *this; }
906 
907   /// See AbstractAttribute::getState(...).
908   const AbstractState &getState() const override { return *this; }
909 
910   /// See AbstractAttribute::updateImpl(Attributor &A).
911   ChangeStatus updateImpl(Attributor &A) override;
912 
913   llvm::iterator_range<iterator> returned_values() override {
914     return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
915   }
916 
917   llvm::iterator_range<const_iterator> returned_values() const override {
918     return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
919   }
920 
921   const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override {
922     return UnresolvedCalls;
923   }
924 
925   /// Return the number of potential return values, -1 if unknown.
926   size_t getNumReturnValues() const override {
927     return isValidState() ? ReturnedValues.size() : -1;
928   }
929 
930   /// Return an assumed unique return value if a single candidate is found. If
931   /// there cannot be one, return a nullptr. If it is not clear yet, return the
932   /// Optional::NoneType.
933   Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
934 
935   /// See AbstractState::checkForAllReturnedValues(...).
936   bool checkForAllReturnedValuesAndReturnInsts(
937       const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
938           &Pred) const override;
939 
940   /// Pretty print the attribute similar to the IR representation.
941   const std::string getAsStr() const override;
942 
943   /// See AbstractState::isAtFixpoint().
944   bool isAtFixpoint() const override { return IsFixed; }
945 
946   /// See AbstractState::isValidState().
947   bool isValidState() const override { return IsValidState; }
948 
949   /// See AbstractState::indicateOptimisticFixpoint(...).
950   ChangeStatus indicateOptimisticFixpoint() override {
951     IsFixed = true;
952     return ChangeStatus::UNCHANGED;
953   }
954 
955   ChangeStatus indicatePessimisticFixpoint() override {
956     IsFixed = true;
957     IsValidState = false;
958     return ChangeStatus::CHANGED;
959   }
960 };
961 
962 ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
963   ChangeStatus Changed = ChangeStatus::UNCHANGED;
964 
965   // Bookkeeping.
966   assert(isValidState());
967   STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
968                   "Number of function with known return values");
969 
970   // Check if we have an assumed unique return value that we could manifest.
971   Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
972 
973   if (!UniqueRV.hasValue() || !UniqueRV.getValue())
974     return Changed;
975 
976   // Bookkeeping.
977   STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
978                   "Number of function with unique return");
979 
980   // Callback to replace the uses of CB with the constant C.
981   auto ReplaceCallSiteUsersWith = [](CallBase &CB, Constant &C) {
982     if (CB.getNumUses() == 0 || CB.isMustTailCall())
983       return ChangeStatus::UNCHANGED;
984     CB.replaceAllUsesWith(&C);
985     return ChangeStatus::CHANGED;
986   };
987 
988   // If the assumed unique return value is an argument, annotate it.
989   if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
990     getIRPosition() = IRPosition::argument(*UniqueRVArg);
991     Changed = IRAttribute::manifest(A);
992   } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) {
993     // We can replace the returned value with the unique returned constant.
994     Value &AnchorValue = getAnchorValue();
995     if (Function *F = dyn_cast<Function>(&AnchorValue)) {
996       for (const Use &U : F->uses())
997         if (CallBase *CB = dyn_cast<CallBase>(U.getUser()))
998           if (CB->isCallee(&U)) {
999             Constant *RVCCast =
1000                 ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
1001             Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed;
1002           }
1003     } else {
1004       assert(isa<CallBase>(AnchorValue) &&
1005              "Expcected a function or call base anchor!");
1006       Constant *RVCCast =
1007           ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
1008       Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast);
1009     }
1010     if (Changed == ChangeStatus::CHANGED)
1011       STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn,
1012                       "Number of function returns replaced by constant return");
1013   }
1014 
1015   return Changed;
1016 }
1017 
1018 const std::string AAReturnedValuesImpl::getAsStr() const {
1019   return (isAtFixpoint() ? "returns(#" : "may-return(#") +
1020          (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
1021          ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]";
1022 }
1023 
1024 Optional<Value *>
1025 AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
1026   // If checkForAllReturnedValues provides a unique value, ignoring potential
1027   // undef values that can also be present, it is assumed to be the actual
1028   // return value and forwarded to the caller of this method. If there are
1029   // multiple, a nullptr is returned indicating there cannot be a unique
1030   // returned value.
1031   Optional<Value *> UniqueRV;
1032 
1033   auto Pred = [&](Value &RV) -> bool {
1034     // If we found a second returned value and neither the current nor the saved
1035     // one is an undef, there is no unique returned value. Undefs are special
1036     // since we can pretend they have any value.
1037     if (UniqueRV.hasValue() && UniqueRV != &RV &&
1038         !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
1039       UniqueRV = nullptr;
1040       return false;
1041     }
1042 
1043     // Do not overwrite a value with an undef.
1044     if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
1045       UniqueRV = &RV;
1046 
1047     return true;
1048   };
1049 
1050   if (!A.checkForAllReturnedValues(Pred, *this))
1051     UniqueRV = nullptr;
1052 
1053   return UniqueRV;
1054 }
1055 
1056 bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
1057     const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
1058         &Pred) const {
1059   if (!isValidState())
1060     return false;
1061 
1062   // Check all returned values but ignore call sites as long as we have not
1063   // encountered an overdefined one during an update.
1064   for (auto &It : ReturnedValues) {
1065     Value *RV = It.first;
1066 
1067     CallBase *CB = dyn_cast<CallBase>(RV);
1068     if (CB && !UnresolvedCalls.count(CB))
1069       continue;
1070 
1071     if (!Pred(*RV, It.second))
1072       return false;
1073   }
1074 
1075   return true;
1076 }
1077 
1078 ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
1079   size_t NumUnresolvedCalls = UnresolvedCalls.size();
1080   bool Changed = false;
1081 
1082   // State used in the value traversals starting in returned values.
1083   struct RVState {
1084     // The map in which we collect return values -> return instrs.
1085     decltype(ReturnedValues) &RetValsMap;
1086     // The flag to indicate a change.
1087     bool &Changed;
1088     // The return instrs we come from.
1089     SmallSetVector<ReturnInst *, 4> RetInsts;
1090   };
1091 
1092   // Callback for a leaf value returned by the associated function.
1093   auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool {
1094     auto Size = RVS.RetValsMap[&Val].size();
1095     RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end());
1096     bool Inserted = RVS.RetValsMap[&Val].size() != Size;
1097     RVS.Changed |= Inserted;
1098     LLVM_DEBUG({
1099       if (Inserted)
1100         dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val
1101                << " => " << RVS.RetInsts.size() << "\n";
1102     });
1103     return true;
1104   };
1105 
1106   // Helper method to invoke the generic value traversal.
1107   auto VisitReturnedValue = [&](Value &RV, RVState &RVS) {
1108     IRPosition RetValPos = IRPosition::value(RV);
1109     return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this,
1110                                                             RVS, VisitValueCB);
1111   };
1112 
1113   // Callback for all "return intructions" live in the associated function.
1114   auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) {
1115     ReturnInst &Ret = cast<ReturnInst>(I);
1116     RVState RVS({ReturnedValues, Changed, {}});
1117     RVS.RetInsts.insert(&Ret);
1118     return VisitReturnedValue(*Ret.getReturnValue(), RVS);
1119   };
1120 
1121   // Start by discovering returned values from all live returned instructions in
1122   // the associated function.
1123   if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret}))
1124     return indicatePessimisticFixpoint();
1125 
1126   // Once returned values "directly" present in the code are handled we try to
1127   // resolve returned calls.
1128   decltype(ReturnedValues) NewRVsMap;
1129   for (auto &It : ReturnedValues) {
1130     LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first
1131                       << " by #" << It.second.size() << " RIs\n");
1132     CallBase *CB = dyn_cast<CallBase>(It.first);
1133     if (!CB || UnresolvedCalls.count(CB))
1134       continue;
1135 
1136     if (!CB->getCalledFunction()) {
1137       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1138                         << "\n");
1139       UnresolvedCalls.insert(CB);
1140       continue;
1141     }
1142 
1143     // TODO: use the function scope once we have call site AAReturnedValues.
1144     const auto &RetValAA = A.getAAFor<AAReturnedValues>(
1145         *this, IRPosition::function(*CB->getCalledFunction()));
1146     LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: "
1147                       << static_cast<const AbstractAttribute &>(RetValAA)
1148                       << "\n");
1149 
1150     // Skip dead ends, thus if we do not know anything about the returned
1151     // call we mark it as unresolved and it will stay that way.
1152     if (!RetValAA.getState().isValidState()) {
1153       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1154                         << "\n");
1155       UnresolvedCalls.insert(CB);
1156       continue;
1157     }
1158 
1159     // Do not try to learn partial information. If the callee has unresolved
1160     // return values we will treat the call as unresolved/opaque.
1161     auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls();
1162     if (!RetValAAUnresolvedCalls.empty()) {
1163       UnresolvedCalls.insert(CB);
1164       continue;
1165     }
1166 
1167     // Now check if we can track transitively returned values. If possible, thus
1168     // if all return value can be represented in the current scope, do so.
1169     bool Unresolved = false;
1170     for (auto &RetValAAIt : RetValAA.returned_values()) {
1171       Value *RetVal = RetValAAIt.first;
1172       if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) ||
1173           isa<Constant>(RetVal))
1174         continue;
1175       // Anything that did not fit in the above categories cannot be resolved,
1176       // mark the call as unresolved.
1177       LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value "
1178                            "cannot be translated: "
1179                         << *RetVal << "\n");
1180       UnresolvedCalls.insert(CB);
1181       Unresolved = true;
1182       break;
1183     }
1184 
1185     if (Unresolved)
1186       continue;
1187 
1188     // Now track transitively returned values.
1189     unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB];
1190     if (NumRetAA == RetValAA.getNumReturnValues()) {
1191       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not "
1192                            "changed since it was seen last\n");
1193       continue;
1194     }
1195     NumRetAA = RetValAA.getNumReturnValues();
1196 
1197     for (auto &RetValAAIt : RetValAA.returned_values()) {
1198       Value *RetVal = RetValAAIt.first;
1199       if (Argument *Arg = dyn_cast<Argument>(RetVal)) {
1200         // Arguments are mapped to call site operands and we begin the traversal
1201         // again.
1202         bool Unused = false;
1203         RVState RVS({NewRVsMap, Unused, RetValAAIt.second});
1204         VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS);
1205         continue;
1206       } else if (isa<CallBase>(RetVal)) {
1207         // Call sites are resolved by the callee attribute over time, no need to
1208         // do anything for us.
1209         continue;
1210       } else if (isa<Constant>(RetVal)) {
1211         // Constants are valid everywhere, we can simply take them.
1212         NewRVsMap[RetVal].insert(It.second.begin(), It.second.end());
1213         continue;
1214       }
1215     }
1216   }
1217 
1218   // To avoid modifications to the ReturnedValues map while we iterate over it
1219   // we kept record of potential new entries in a copy map, NewRVsMap.
1220   for (auto &It : NewRVsMap) {
1221     assert(!It.second.empty() && "Entry does not add anything.");
1222     auto &ReturnInsts = ReturnedValues[It.first];
1223     for (ReturnInst *RI : It.second)
1224       if (ReturnInsts.insert(RI)) {
1225         LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
1226                           << *It.first << " => " << *RI << "\n");
1227         Changed = true;
1228       }
1229   }
1230 
1231   Changed |= (NumUnresolvedCalls != UnresolvedCalls.size());
1232   return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
1233 }
1234 
1235 struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1236   AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1237 
1238   /// See AbstractAttribute::trackStatistics()
1239   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
1240 };
1241 
1242 /// Returned values information for a call sites.
1243 struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1244   AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1245 
1246   /// See AbstractAttribute::initialize(...).
1247   void initialize(Attributor &A) override {
1248     // TODO: Once we have call site specific value information we can provide
1249     //       call site specific liveness information and then it makes
1250     //       sense to specialize attributes for call sites instead of
1251     //       redirecting requests to the callee.
1252     llvm_unreachable("Abstract attributes for returned values are not "
1253                      "supported for call sites yet!");
1254   }
1255 
1256   /// See AbstractAttribute::updateImpl(...).
1257   ChangeStatus updateImpl(Attributor &A) override {
1258     return indicatePessimisticFixpoint();
1259   }
1260 
1261   /// See AbstractAttribute::trackStatistics()
1262   void trackStatistics() const override {}
1263 };
1264 
1265 /// ------------------------ NoSync Function Attribute -------------------------
1266 
1267 struct AANoSyncImpl : AANoSync {
1268   AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {}
1269 
1270   const std::string getAsStr() const override {
1271     return getAssumed() ? "nosync" : "may-sync";
1272   }
1273 
1274   /// See AbstractAttribute::updateImpl(...).
1275   ChangeStatus updateImpl(Attributor &A) override;
1276 
1277   /// Helper function used to determine whether an instruction is non-relaxed
1278   /// atomic. In other words, if an atomic instruction does not have unordered
1279   /// or monotonic ordering
1280   static bool isNonRelaxedAtomic(Instruction *I);
1281 
1282   /// Helper function used to determine whether an instruction is volatile.
1283   static bool isVolatile(Instruction *I);
1284 
1285   /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
1286   /// memset).
1287   static bool isNoSyncIntrinsic(Instruction *I);
1288 };
1289 
1290 bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
1291   if (!I->isAtomic())
1292     return false;
1293 
1294   AtomicOrdering Ordering;
1295   switch (I->getOpcode()) {
1296   case Instruction::AtomicRMW:
1297     Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1298     break;
1299   case Instruction::Store:
1300     Ordering = cast<StoreInst>(I)->getOrdering();
1301     break;
1302   case Instruction::Load:
1303     Ordering = cast<LoadInst>(I)->getOrdering();
1304     break;
1305   case Instruction::Fence: {
1306     auto *FI = cast<FenceInst>(I);
1307     if (FI->getSyncScopeID() == SyncScope::SingleThread)
1308       return false;
1309     Ordering = FI->getOrdering();
1310     break;
1311   }
1312   case Instruction::AtomicCmpXchg: {
1313     AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering();
1314     AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering();
1315     // Only if both are relaxed, than it can be treated as relaxed.
1316     // Otherwise it is non-relaxed.
1317     if (Success != AtomicOrdering::Unordered &&
1318         Success != AtomicOrdering::Monotonic)
1319       return true;
1320     if (Failure != AtomicOrdering::Unordered &&
1321         Failure != AtomicOrdering::Monotonic)
1322       return true;
1323     return false;
1324   }
1325   default:
1326     llvm_unreachable(
1327         "New atomic operations need to be known in the attributor.");
1328   }
1329 
1330   // Relaxed.
1331   if (Ordering == AtomicOrdering::Unordered ||
1332       Ordering == AtomicOrdering::Monotonic)
1333     return false;
1334   return true;
1335 }
1336 
1337 /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
1338 /// FIXME: We should ipmrove the handling of intrinsics.
1339 bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
1340   if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1341     switch (II->getIntrinsicID()) {
1342     /// Element wise atomic memory intrinsics are can only be unordered,
1343     /// therefore nosync.
1344     case Intrinsic::memset_element_unordered_atomic:
1345     case Intrinsic::memmove_element_unordered_atomic:
1346     case Intrinsic::memcpy_element_unordered_atomic:
1347       return true;
1348     case Intrinsic::memset:
1349     case Intrinsic::memmove:
1350     case Intrinsic::memcpy:
1351       if (!cast<MemIntrinsic>(II)->isVolatile())
1352         return true;
1353       return false;
1354     default:
1355       return false;
1356     }
1357   }
1358   return false;
1359 }
1360 
1361 bool AANoSyncImpl::isVolatile(Instruction *I) {
1362   assert(!ImmutableCallSite(I) && !isa<CallBase>(I) &&
1363          "Calls should not be checked here");
1364 
1365   switch (I->getOpcode()) {
1366   case Instruction::AtomicRMW:
1367     return cast<AtomicRMWInst>(I)->isVolatile();
1368   case Instruction::Store:
1369     return cast<StoreInst>(I)->isVolatile();
1370   case Instruction::Load:
1371     return cast<LoadInst>(I)->isVolatile();
1372   case Instruction::AtomicCmpXchg:
1373     return cast<AtomicCmpXchgInst>(I)->isVolatile();
1374   default:
1375     return false;
1376   }
1377 }
1378 
1379 ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
1380 
1381   auto CheckRWInstForNoSync = [&](Instruction &I) {
1382     /// We are looking for volatile instructions or Non-Relaxed atomics.
1383     /// FIXME: We should ipmrove the handling of intrinsics.
1384 
1385     if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I))
1386       return true;
1387 
1388     if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1389       if (ICS.hasFnAttr(Attribute::NoSync))
1390         return true;
1391 
1392       const auto &NoSyncAA =
1393           A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS));
1394       if (NoSyncAA.isAssumedNoSync())
1395         return true;
1396       return false;
1397     }
1398 
1399     if (!isVolatile(&I) && !isNonRelaxedAtomic(&I))
1400       return true;
1401 
1402     return false;
1403   };
1404 
1405   auto CheckForNoSync = [&](Instruction &I) {
1406     // At this point we handled all read/write effects and they are all
1407     // nosync, so they can be skipped.
1408     if (I.mayReadOrWriteMemory())
1409       return true;
1410 
1411     // non-convergent and readnone imply nosync.
1412     return !ImmutableCallSite(&I).isConvergent();
1413   };
1414 
1415   if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) ||
1416       !A.checkForAllCallLikeInstructions(CheckForNoSync, *this))
1417     return indicatePessimisticFixpoint();
1418 
1419   return ChangeStatus::UNCHANGED;
1420 }
1421 
1422 struct AANoSyncFunction final : public AANoSyncImpl {
1423   AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1424 
1425   /// See AbstractAttribute::trackStatistics()
1426   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
1427 };
1428 
1429 /// NoSync attribute deduction for a call sites.
1430 struct AANoSyncCallSite final : AANoSyncImpl {
1431   AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1432 
1433   /// See AbstractAttribute::initialize(...).
1434   void initialize(Attributor &A) override {
1435     AANoSyncImpl::initialize(A);
1436     Function *F = getAssociatedFunction();
1437     if (!F)
1438       indicatePessimisticFixpoint();
1439   }
1440 
1441   /// See AbstractAttribute::updateImpl(...).
1442   ChangeStatus updateImpl(Attributor &A) override {
1443     // TODO: Once we have call site specific value information we can provide
1444     //       call site specific liveness information and then it makes
1445     //       sense to specialize attributes for call sites arguments instead of
1446     //       redirecting requests to the callee argument.
1447     Function *F = getAssociatedFunction();
1448     const IRPosition &FnPos = IRPosition::function(*F);
1449     auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos);
1450     return clampStateAndIndicateChange(
1451         getState(), static_cast<const AANoSync::StateType &>(FnAA.getState()));
1452   }
1453 
1454   /// See AbstractAttribute::trackStatistics()
1455   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
1456 };
1457 
1458 /// ------------------------ No-Free Attributes ----------------------------
1459 
1460 struct AANoFreeImpl : public AANoFree {
1461   AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {}
1462 
1463   /// See AbstractAttribute::updateImpl(...).
1464   ChangeStatus updateImpl(Attributor &A) override {
1465     auto CheckForNoFree = [&](Instruction &I) {
1466       ImmutableCallSite ICS(&I);
1467       if (ICS.hasFnAttr(Attribute::NoFree))
1468         return true;
1469 
1470       const auto &NoFreeAA =
1471           A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS));
1472       return NoFreeAA.isAssumedNoFree();
1473     };
1474 
1475     if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this))
1476       return indicatePessimisticFixpoint();
1477     return ChangeStatus::UNCHANGED;
1478   }
1479 
1480   /// See AbstractAttribute::getAsStr().
1481   const std::string getAsStr() const override {
1482     return getAssumed() ? "nofree" : "may-free";
1483   }
1484 };
1485 
1486 struct AANoFreeFunction final : public AANoFreeImpl {
1487   AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1488 
1489   /// See AbstractAttribute::trackStatistics()
1490   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
1491 };
1492 
1493 /// NoFree attribute deduction for a call sites.
1494 struct AANoFreeCallSite final : AANoFreeImpl {
1495   AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1496 
1497   /// See AbstractAttribute::initialize(...).
1498   void initialize(Attributor &A) override {
1499     AANoFreeImpl::initialize(A);
1500     Function *F = getAssociatedFunction();
1501     if (!F)
1502       indicatePessimisticFixpoint();
1503   }
1504 
1505   /// See AbstractAttribute::updateImpl(...).
1506   ChangeStatus updateImpl(Attributor &A) override {
1507     // TODO: Once we have call site specific value information we can provide
1508     //       call site specific liveness information and then it makes
1509     //       sense to specialize attributes for call sites arguments instead of
1510     //       redirecting requests to the callee argument.
1511     Function *F = getAssociatedFunction();
1512     const IRPosition &FnPos = IRPosition::function(*F);
1513     auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos);
1514     return clampStateAndIndicateChange(
1515         getState(), static_cast<const AANoFree::StateType &>(FnAA.getState()));
1516   }
1517 
1518   /// See AbstractAttribute::trackStatistics()
1519   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
1520 };
1521 
1522 /// ------------------------ NonNull Argument Attribute ------------------------
1523 static int64_t getKnownNonNullAndDerefBytesForUse(
1524     Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue,
1525     const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
1526   TrackUse = false;
1527 
1528   const Value *UseV = U->get();
1529   if (!UseV->getType()->isPointerTy())
1530     return 0;
1531 
1532   Type *PtrTy = UseV->getType();
1533   const Function *F = I->getFunction();
1534   bool NullPointerIsDefined =
1535       F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
1536   const DataLayout &DL = A.getInfoCache().getDL();
1537   if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
1538     if (ICS.isBundleOperand(U))
1539       return 0;
1540 
1541     if (ICS.isCallee(U)) {
1542       IsNonNull |= !NullPointerIsDefined;
1543       return 0;
1544     }
1545 
1546     unsigned ArgNo = ICS.getArgumentNo(U);
1547     IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo);
1548     auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP);
1549     IsNonNull |= DerefAA.isKnownNonNull();
1550     return DerefAA.getKnownDereferenceableBytes();
1551   }
1552 
1553   int64_t Offset;
1554   if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) {
1555     if (Base == &AssociatedValue && getPointerOperand(I) == UseV) {
1556       int64_t DerefBytes =
1557           Offset + (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType());
1558 
1559       IsNonNull |= !NullPointerIsDefined;
1560       return DerefBytes;
1561     }
1562   }
1563   if (const Value *Base =
1564           GetPointerBaseWithConstantOffset(UseV, Offset, DL,
1565                                            /*AllowNonInbounds*/ false)) {
1566     auto &DerefAA =
1567         A.getAAFor<AADereferenceable>(QueryingAA, IRPosition::value(*Base));
1568     IsNonNull |= (!NullPointerIsDefined && DerefAA.isKnownNonNull());
1569     IsNonNull |= (!NullPointerIsDefined && (Offset != 0));
1570     int64_t DerefBytes = DerefAA.getKnownDereferenceableBytes();
1571     return std::max(int64_t(0), DerefBytes - Offset);
1572   }
1573 
1574   return 0;
1575 }
1576 
1577 struct AANonNullImpl : AANonNull {
1578   AANonNullImpl(const IRPosition &IRP)
1579       : AANonNull(IRP),
1580         NullIsDefined(NullPointerIsDefined(
1581             getAnchorScope(),
1582             getAssociatedValue().getType()->getPointerAddressSpace())) {}
1583 
1584   /// See AbstractAttribute::initialize(...).
1585   void initialize(Attributor &A) override {
1586     if (!NullIsDefined &&
1587         hasAttr({Attribute::NonNull, Attribute::Dereferenceable}))
1588       indicateOptimisticFixpoint();
1589     else
1590       AANonNull::initialize(A);
1591   }
1592 
1593   /// See AAFromMustBeExecutedContext
1594   bool followUse(Attributor &A, const Use *U, const Instruction *I) {
1595     bool IsNonNull = false;
1596     bool TrackUse = false;
1597     getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
1598                                        IsNonNull, TrackUse);
1599     setKnown(IsNonNull);
1600     return TrackUse;
1601   }
1602 
1603   /// See AbstractAttribute::getAsStr().
1604   const std::string getAsStr() const override {
1605     return getAssumed() ? "nonnull" : "may-null";
1606   }
1607 
1608   /// Flag to determine if the underlying value can be null and still allow
1609   /// valid accesses.
1610   const bool NullIsDefined;
1611 };
1612 
1613 /// NonNull attribute for a floating value.
1614 struct AANonNullFloating
1615     : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> {
1616   using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>;
1617   AANonNullFloating(const IRPosition &IRP) : Base(IRP) {}
1618 
1619   /// See AbstractAttribute::initialize(...).
1620   void initialize(Attributor &A) override {
1621     Base::initialize(A);
1622 
1623     if (isAtFixpoint())
1624       return;
1625 
1626     const IRPosition &IRP = getIRPosition();
1627     const Value &V = IRP.getAssociatedValue();
1628     const DataLayout &DL = A.getDataLayout();
1629 
1630     // TODO: This context sensitive query should be removed once we can do
1631     // context sensitive queries in the genericValueTraversal below.
1632     if (isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, IRP.getCtxI(),
1633                        /* TODO: DT */ nullptr))
1634       indicateOptimisticFixpoint();
1635   }
1636 
1637   /// See AbstractAttribute::updateImpl(...).
1638   ChangeStatus updateImpl(Attributor &A) override {
1639     ChangeStatus Change = Base::updateImpl(A);
1640     if (isKnownNonNull())
1641       return Change;
1642 
1643     if (!NullIsDefined) {
1644       const auto &DerefAA = A.getAAFor<AADereferenceable>(*this, getIRPosition());
1645       if (DerefAA.getAssumedDereferenceableBytes())
1646         return Change;
1647     }
1648 
1649     const DataLayout &DL = A.getDataLayout();
1650 
1651     auto VisitValueCB = [&](Value &V, AANonNull::StateType &T,
1652                             bool Stripped) -> bool {
1653       const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V));
1654       if (!Stripped && this == &AA) {
1655         if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr,
1656                             /* CtxI */ getCtxI(),
1657                             /* TODO: DT */ nullptr))
1658           T.indicatePessimisticFixpoint();
1659       } else {
1660         // Use abstract attribute information.
1661         const AANonNull::StateType &NS =
1662             static_cast<const AANonNull::StateType &>(AA.getState());
1663         T ^= NS;
1664       }
1665       return T.isValidState();
1666     };
1667 
1668     StateType T;
1669     if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this,
1670                                                      T, VisitValueCB))
1671       return indicatePessimisticFixpoint();
1672 
1673     return clampStateAndIndicateChange(getState(), T);
1674   }
1675 
1676   /// See AbstractAttribute::trackStatistics()
1677   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
1678 };
1679 
1680 /// NonNull attribute for function return value.
1681 struct AANonNullReturned final
1682     : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> {
1683   AANonNullReturned(const IRPosition &IRP)
1684       : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {}
1685 
1686   /// See AbstractAttribute::trackStatistics()
1687   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
1688 };
1689 
1690 /// NonNull attribute for function argument.
1691 struct AANonNullArgument final
1692     : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
1693                                                               AANonNullImpl> {
1694   AANonNullArgument(const IRPosition &IRP)
1695       : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
1696                                                                 AANonNullImpl>(
1697             IRP) {}
1698 
1699   /// See AbstractAttribute::trackStatistics()
1700   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
1701 };
1702 
1703 struct AANonNullCallSiteArgument final : AANonNullFloating {
1704   AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {}
1705 
1706   /// See AbstractAttribute::trackStatistics()
1707   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
1708 };
1709 
1710 /// NonNull attribute for a call site return position.
1711 struct AANonNullCallSiteReturned final
1712     : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
1713                                                              AANonNullImpl> {
1714   AANonNullCallSiteReturned(const IRPosition &IRP)
1715       : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
1716                                                                AANonNullImpl>(
1717             IRP) {}
1718 
1719   /// See AbstractAttribute::trackStatistics()
1720   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
1721 };
1722 
1723 /// ------------------------ No-Recurse Attributes ----------------------------
1724 
1725 struct AANoRecurseImpl : public AANoRecurse {
1726   AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {}
1727 
1728   /// See AbstractAttribute::getAsStr()
1729   const std::string getAsStr() const override {
1730     return getAssumed() ? "norecurse" : "may-recurse";
1731   }
1732 };
1733 
1734 struct AANoRecurseFunction final : AANoRecurseImpl {
1735   AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
1736 
1737   /// See AbstractAttribute::initialize(...).
1738   void initialize(Attributor &A) override {
1739     AANoRecurseImpl::initialize(A);
1740     if (const Function *F = getAnchorScope())
1741       if (A.getInfoCache().getSccSize(*F) == 1)
1742         return;
1743     indicatePessimisticFixpoint();
1744   }
1745 
1746   /// See AbstractAttribute::updateImpl(...).
1747   ChangeStatus updateImpl(Attributor &A) override {
1748 
1749     auto CheckForNoRecurse = [&](Instruction &I) {
1750       ImmutableCallSite ICS(&I);
1751       if (ICS.hasFnAttr(Attribute::NoRecurse))
1752         return true;
1753 
1754       const auto &NoRecurseAA =
1755           A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS));
1756       if (!NoRecurseAA.isAssumedNoRecurse())
1757         return false;
1758 
1759       // Recursion to the same function
1760       if (ICS.getCalledFunction() == getAnchorScope())
1761         return false;
1762 
1763       return true;
1764     };
1765 
1766     if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this))
1767       return indicatePessimisticFixpoint();
1768     return ChangeStatus::UNCHANGED;
1769   }
1770 
1771   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
1772 };
1773 
1774 /// NoRecurse attribute deduction for a call sites.
1775 struct AANoRecurseCallSite final : AANoRecurseImpl {
1776   AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
1777 
1778   /// See AbstractAttribute::initialize(...).
1779   void initialize(Attributor &A) override {
1780     AANoRecurseImpl::initialize(A);
1781     Function *F = getAssociatedFunction();
1782     if (!F)
1783       indicatePessimisticFixpoint();
1784   }
1785 
1786   /// See AbstractAttribute::updateImpl(...).
1787   ChangeStatus updateImpl(Attributor &A) override {
1788     // TODO: Once we have call site specific value information we can provide
1789     //       call site specific liveness information and then it makes
1790     //       sense to specialize attributes for call sites arguments instead of
1791     //       redirecting requests to the callee argument.
1792     Function *F = getAssociatedFunction();
1793     const IRPosition &FnPos = IRPosition::function(*F);
1794     auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos);
1795     return clampStateAndIndicateChange(
1796         getState(),
1797         static_cast<const AANoRecurse::StateType &>(FnAA.getState()));
1798   }
1799 
1800   /// See AbstractAttribute::trackStatistics()
1801   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
1802 };
1803 
1804 /// ------------------------ Will-Return Attributes ----------------------------
1805 
1806 // Helper function that checks whether a function has any cycle.
1807 // TODO: Replace with more efficent code
1808 static bool containsCycle(Function &F) {
1809   SmallPtrSet<BasicBlock *, 32> Visited;
1810 
1811   // Traverse BB by dfs and check whether successor is already visited.
1812   for (BasicBlock *BB : depth_first(&F)) {
1813     Visited.insert(BB);
1814     for (auto *SuccBB : successors(BB)) {
1815       if (Visited.count(SuccBB))
1816         return true;
1817     }
1818   }
1819   return false;
1820 }
1821 
1822 // Helper function that checks the function have a loop which might become an
1823 // endless loop
1824 // FIXME: Any cycle is regarded as endless loop for now.
1825 //        We have to allow some patterns.
1826 static bool containsPossiblyEndlessLoop(Function *F) {
1827   return !F || !F->hasExactDefinition() || containsCycle(*F);
1828 }
1829 
1830 struct AAWillReturnImpl : public AAWillReturn {
1831   AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {}
1832 
1833   /// See AbstractAttribute::initialize(...).
1834   void initialize(Attributor &A) override {
1835     AAWillReturn::initialize(A);
1836 
1837     Function *F = getAssociatedFunction();
1838     if (containsPossiblyEndlessLoop(F))
1839       indicatePessimisticFixpoint();
1840   }
1841 
1842   /// See AbstractAttribute::updateImpl(...).
1843   ChangeStatus updateImpl(Attributor &A) override {
1844     auto CheckForWillReturn = [&](Instruction &I) {
1845       IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I));
1846       const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos);
1847       if (WillReturnAA.isKnownWillReturn())
1848         return true;
1849       if (!WillReturnAA.isAssumedWillReturn())
1850         return false;
1851       const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos);
1852       return NoRecurseAA.isAssumedNoRecurse();
1853     };
1854 
1855     if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this))
1856       return indicatePessimisticFixpoint();
1857 
1858     return ChangeStatus::UNCHANGED;
1859   }
1860 
1861   /// See AbstractAttribute::getAsStr()
1862   const std::string getAsStr() const override {
1863     return getAssumed() ? "willreturn" : "may-noreturn";
1864   }
1865 };
1866 
1867 struct AAWillReturnFunction final : AAWillReturnImpl {
1868   AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
1869 
1870   /// See AbstractAttribute::trackStatistics()
1871   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
1872 };
1873 
1874 /// WillReturn attribute deduction for a call sites.
1875 struct AAWillReturnCallSite final : AAWillReturnImpl {
1876   AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
1877 
1878   /// See AbstractAttribute::initialize(...).
1879   void initialize(Attributor &A) override {
1880     AAWillReturnImpl::initialize(A);
1881     Function *F = getAssociatedFunction();
1882     if (!F)
1883       indicatePessimisticFixpoint();
1884   }
1885 
1886   /// See AbstractAttribute::updateImpl(...).
1887   ChangeStatus updateImpl(Attributor &A) override {
1888     // TODO: Once we have call site specific value information we can provide
1889     //       call site specific liveness information and then it makes
1890     //       sense to specialize attributes for call sites arguments instead of
1891     //       redirecting requests to the callee argument.
1892     Function *F = getAssociatedFunction();
1893     const IRPosition &FnPos = IRPosition::function(*F);
1894     auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos);
1895     return clampStateAndIndicateChange(
1896         getState(),
1897         static_cast<const AAWillReturn::StateType &>(FnAA.getState()));
1898   }
1899 
1900   /// See AbstractAttribute::trackStatistics()
1901   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
1902 };
1903 
1904 /// ------------------------ NoAlias Argument Attribute ------------------------
1905 
1906 struct AANoAliasImpl : AANoAlias {
1907   AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {}
1908 
1909   const std::string getAsStr() const override {
1910     return getAssumed() ? "noalias" : "may-alias";
1911   }
1912 };
1913 
1914 /// NoAlias attribute for a floating value.
1915 struct AANoAliasFloating final : AANoAliasImpl {
1916   AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
1917 
1918   /// See AbstractAttribute::initialize(...).
1919   void initialize(Attributor &A) override {
1920     AANoAliasImpl::initialize(A);
1921     Value &Val = getAssociatedValue();
1922     if (isa<AllocaInst>(Val))
1923       indicateOptimisticFixpoint();
1924     if (isa<ConstantPointerNull>(Val) &&
1925         Val.getType()->getPointerAddressSpace() == 0)
1926       indicateOptimisticFixpoint();
1927   }
1928 
1929   /// See AbstractAttribute::updateImpl(...).
1930   ChangeStatus updateImpl(Attributor &A) override {
1931     // TODO: Implement this.
1932     return indicatePessimisticFixpoint();
1933   }
1934 
1935   /// See AbstractAttribute::trackStatistics()
1936   void trackStatistics() const override {
1937     STATS_DECLTRACK_FLOATING_ATTR(noalias)
1938   }
1939 };
1940 
1941 /// NoAlias attribute for an argument.
1942 struct AANoAliasArgument final
1943     : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
1944   AANoAliasArgument(const IRPosition &IRP)
1945       : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>(IRP) {}
1946 
1947   /// See AbstractAttribute::trackStatistics()
1948   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
1949 };
1950 
1951 struct AANoAliasCallSiteArgument final : AANoAliasImpl {
1952   AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
1953 
1954   /// See AbstractAttribute::initialize(...).
1955   void initialize(Attributor &A) override {
1956     // See callsite argument attribute and callee argument attribute.
1957     ImmutableCallSite ICS(&getAnchorValue());
1958     if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias))
1959       indicateOptimisticFixpoint();
1960   }
1961 
1962   /// See AbstractAttribute::updateImpl(...).
1963   ChangeStatus updateImpl(Attributor &A) override {
1964     // We can deduce "noalias" if the following conditions hold.
1965     // (i)   Associated value is assumed to be noalias in the definition.
1966     // (ii)  Associated value is assumed to be no-capture in all the uses
1967     //       possibly executed before this callsite.
1968     // (iii) There is no other pointer argument which could alias with the
1969     //       value.
1970 
1971     const Value &V = getAssociatedValue();
1972     const IRPosition IRP = IRPosition::value(V);
1973 
1974     // (i) Check whether noalias holds in the definition.
1975 
1976     auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP);
1977 
1978     if (!NoAliasAA.isAssumedNoAlias())
1979       return indicatePessimisticFixpoint();
1980 
1981     LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] " << V
1982                       << " is assumed NoAlias in the definition\n");
1983 
1984     // (ii) Check whether the value is captured in the scope using AANoCapture.
1985     //      FIXME: This is conservative though, it is better to look at CFG and
1986     //             check only uses possibly executed before this callsite.
1987 
1988     auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP);
1989     if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
1990       LLVM_DEBUG(
1991           dbgs() << "[Attributor][AANoAliasCSArg] " << V
1992                  << " cannot be noalias as it is potentially captured\n");
1993       return indicatePessimisticFixpoint();
1994     }
1995 
1996     // (iii) Check there is no other pointer argument which could alias with the
1997     // value.
1998     ImmutableCallSite ICS(&getAnchorValue());
1999     for (unsigned i = 0; i < ICS.getNumArgOperands(); i++) {
2000       if (getArgNo() == (int)i)
2001         continue;
2002       const Value *ArgOp = ICS.getArgOperand(i);
2003       if (!ArgOp->getType()->isPointerTy())
2004         continue;
2005 
2006       if (const Function *F = getAnchorScope()) {
2007         if (AAResults *AAR = A.getInfoCache().getAAResultsForFunction(*F)) {
2008           bool IsAliasing = AAR->isNoAlias(&getAssociatedValue(), ArgOp);
2009           LLVM_DEBUG(dbgs()
2010                      << "[Attributor][NoAliasCSArg] Check alias between "
2011                         "callsite arguments "
2012                      << AAR->isNoAlias(&getAssociatedValue(), ArgOp) << " "
2013                      << getAssociatedValue() << " " << *ArgOp << " => "
2014                      << (IsAliasing ? "" : "no-") << "alias \n");
2015 
2016           if (IsAliasing)
2017             continue;
2018         }
2019       }
2020       return indicatePessimisticFixpoint();
2021     }
2022 
2023     return ChangeStatus::UNCHANGED;
2024   }
2025 
2026   /// See AbstractAttribute::trackStatistics()
2027   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
2028 };
2029 
2030 /// NoAlias attribute for function return value.
2031 struct AANoAliasReturned final : AANoAliasImpl {
2032   AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2033 
2034   /// See AbstractAttribute::updateImpl(...).
2035   virtual ChangeStatus updateImpl(Attributor &A) override {
2036 
2037     auto CheckReturnValue = [&](Value &RV) -> bool {
2038       if (Constant *C = dyn_cast<Constant>(&RV))
2039         if (C->isNullValue() || isa<UndefValue>(C))
2040           return true;
2041 
2042       /// For now, we can only deduce noalias if we have call sites.
2043       /// FIXME: add more support.
2044       ImmutableCallSite ICS(&RV);
2045       if (!ICS)
2046         return false;
2047 
2048       const IRPosition &RVPos = IRPosition::value(RV);
2049       const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos);
2050       if (!NoAliasAA.isAssumedNoAlias())
2051         return false;
2052 
2053       const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos);
2054       return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
2055     };
2056 
2057     if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
2058       return indicatePessimisticFixpoint();
2059 
2060     return ChangeStatus::UNCHANGED;
2061   }
2062 
2063   /// See AbstractAttribute::trackStatistics()
2064   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
2065 };
2066 
2067 /// NoAlias attribute deduction for a call site return value.
2068 struct AANoAliasCallSiteReturned final : AANoAliasImpl {
2069   AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2070 
2071   /// See AbstractAttribute::initialize(...).
2072   void initialize(Attributor &A) override {
2073     AANoAliasImpl::initialize(A);
2074     Function *F = getAssociatedFunction();
2075     if (!F)
2076       indicatePessimisticFixpoint();
2077   }
2078 
2079   /// See AbstractAttribute::updateImpl(...).
2080   ChangeStatus updateImpl(Attributor &A) override {
2081     // TODO: Once we have call site specific value information we can provide
2082     //       call site specific liveness information and then it makes
2083     //       sense to specialize attributes for call sites arguments instead of
2084     //       redirecting requests to the callee argument.
2085     Function *F = getAssociatedFunction();
2086     const IRPosition &FnPos = IRPosition::returned(*F);
2087     auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos);
2088     return clampStateAndIndicateChange(
2089         getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState()));
2090   }
2091 
2092   /// See AbstractAttribute::trackStatistics()
2093   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
2094 };
2095 
2096 /// -------------------AAIsDead Function Attribute-----------------------
2097 
2098 struct AAIsDeadImpl : public AAIsDead {
2099   AAIsDeadImpl(const IRPosition &IRP) : AAIsDead(IRP) {}
2100 
2101   void initialize(Attributor &A) override {
2102     const Function *F = getAssociatedFunction();
2103     if (F && !F->isDeclaration())
2104       exploreFromEntry(A, F);
2105   }
2106 
2107   void exploreFromEntry(Attributor &A, const Function *F) {
2108     ToBeExploredPaths.insert(&(F->getEntryBlock().front()));
2109 
2110     for (size_t i = 0; i < ToBeExploredPaths.size(); ++i)
2111       if (const Instruction *NextNoReturnI =
2112               findNextNoReturn(A, ToBeExploredPaths[i]))
2113         NoReturnCalls.insert(NextNoReturnI);
2114 
2115     // Mark the block live after we looked for no-return instructions.
2116     assumeLive(A, F->getEntryBlock());
2117   }
2118 
2119   /// Find the next assumed noreturn instruction in the block of \p I starting
2120   /// from, thus including, \p I.
2121   ///
2122   /// The caller is responsible to monitor the ToBeExploredPaths set as new
2123   /// instructions discovered in other basic block will be placed in there.
2124   ///
2125   /// \returns The next assumed noreturn instructions in the block of \p I
2126   ///          starting from, thus including, \p I.
2127   const Instruction *findNextNoReturn(Attributor &A, const Instruction *I);
2128 
2129   /// See AbstractAttribute::getAsStr().
2130   const std::string getAsStr() const override {
2131     return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
2132            std::to_string(getAssociatedFunction()->size()) + "][#NRI " +
2133            std::to_string(NoReturnCalls.size()) + "]";
2134   }
2135 
2136   /// See AbstractAttribute::manifest(...).
2137   ChangeStatus manifest(Attributor &A) override {
2138     assert(getState().isValidState() &&
2139            "Attempted to manifest an invalid state!");
2140 
2141     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
2142     Function &F = *getAssociatedFunction();
2143 
2144     if (AssumedLiveBlocks.empty()) {
2145       A.deleteAfterManifest(F);
2146       return ChangeStatus::CHANGED;
2147     }
2148 
2149     // Flag to determine if we can change an invoke to a call assuming the
2150     // callee is nounwind. This is not possible if the personality of the
2151     // function allows to catch asynchronous exceptions.
2152     bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
2153 
2154     for (const Instruction *NRC : NoReturnCalls) {
2155       Instruction *I = const_cast<Instruction *>(NRC);
2156       BasicBlock *BB = I->getParent();
2157       Instruction *SplitPos = I->getNextNode();
2158       // TODO: mark stuff before unreachable instructions as dead.
2159 
2160       if (auto *II = dyn_cast<InvokeInst>(I)) {
2161         // If we keep the invoke the split position is at the beginning of the
2162         // normal desitination block (it invokes a noreturn function after all).
2163         BasicBlock *NormalDestBB = II->getNormalDest();
2164         SplitPos = &NormalDestBB->front();
2165 
2166         /// Invoke is replaced with a call and unreachable is placed after it if
2167         /// the callee is nounwind and noreturn. Otherwise, we keep the invoke
2168         /// and only place an unreachable in the normal successor.
2169         if (Invoke2CallAllowed) {
2170           if (II->getCalledFunction()) {
2171             const IRPosition &IPos = IRPosition::callsite_function(*II);
2172             const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos);
2173             if (AANoUnw.isAssumedNoUnwind()) {
2174               LLVM_DEBUG(dbgs()
2175                          << "[AAIsDead] Replace invoke with call inst\n");
2176               // We do not need an invoke (II) but instead want a call followed
2177               // by an unreachable. However, we do not remove II as other
2178               // abstract attributes might have it cached as part of their
2179               // results. Given that we modify the CFG anyway, we simply keep II
2180               // around but in a new dead block. To avoid II being live through
2181               // a different edge we have to ensure the block we place it in is
2182               // only reached from the current block of II and then not reached
2183               // at all when we insert the unreachable.
2184               SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c");
2185               CallInst *CI = createCallMatchingInvoke(II);
2186               CI->insertBefore(II);
2187               CI->takeName(II);
2188               II->replaceAllUsesWith(CI);
2189               SplitPos = CI->getNextNode();
2190             }
2191           }
2192         }
2193 
2194         if (SplitPos == &NormalDestBB->front()) {
2195           // If this is an invoke of a noreturn function the edge to the normal
2196           // destination block is dead but not necessarily the block itself.
2197           // TODO: We need to move to an edge based system during deduction and
2198           //       also manifest.
2199           assert(!NormalDestBB->isLandingPad() &&
2200                  "Expected the normal destination not to be a landingpad!");
2201           if (NormalDestBB->getUniquePredecessor() == BB) {
2202             assumeLive(A, *NormalDestBB);
2203           } else {
2204             BasicBlock *SplitBB =
2205                 SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
2206             // The split block is live even if it contains only an unreachable
2207             // instruction at the end.
2208             assumeLive(A, *SplitBB);
2209             SplitPos = SplitBB->getTerminator();
2210             HasChanged = ChangeStatus::CHANGED;
2211           }
2212         }
2213       }
2214 
2215       if (isa_and_nonnull<UnreachableInst>(SplitPos))
2216         continue;
2217 
2218       BB = SplitPos->getParent();
2219       SplitBlock(BB, SplitPos);
2220       changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false);
2221       HasChanged = ChangeStatus::CHANGED;
2222     }
2223 
2224     for (BasicBlock &BB : F)
2225       if (!AssumedLiveBlocks.count(&BB))
2226         A.deleteAfterManifest(BB);
2227 
2228     return HasChanged;
2229   }
2230 
2231   /// See AbstractAttribute::updateImpl(...).
2232   ChangeStatus updateImpl(Attributor &A) override;
2233 
2234   /// See AAIsDead::isAssumedDead(BasicBlock *).
2235   bool isAssumedDead(const BasicBlock *BB) const override {
2236     assert(BB->getParent() == getAssociatedFunction() &&
2237            "BB must be in the same anchor scope function.");
2238 
2239     if (!getAssumed())
2240       return false;
2241     return !AssumedLiveBlocks.count(BB);
2242   }
2243 
2244   /// See AAIsDead::isKnownDead(BasicBlock *).
2245   bool isKnownDead(const BasicBlock *BB) const override {
2246     return getKnown() && isAssumedDead(BB);
2247   }
2248 
2249   /// See AAIsDead::isAssumed(Instruction *I).
2250   bool isAssumedDead(const Instruction *I) const override {
2251     assert(I->getParent()->getParent() == getAssociatedFunction() &&
2252            "Instruction must be in the same anchor scope function.");
2253 
2254     if (!getAssumed())
2255       return false;
2256 
2257     // If it is not in AssumedLiveBlocks then it for sure dead.
2258     // Otherwise, it can still be after noreturn call in a live block.
2259     if (!AssumedLiveBlocks.count(I->getParent()))
2260       return true;
2261 
2262     // If it is not after a noreturn call, than it is live.
2263     return isAfterNoReturn(I);
2264   }
2265 
2266   /// See AAIsDead::isKnownDead(Instruction *I).
2267   bool isKnownDead(const Instruction *I) const override {
2268     return getKnown() && isAssumedDead(I);
2269   }
2270 
2271   /// Check if instruction is after noreturn call, in other words, assumed dead.
2272   bool isAfterNoReturn(const Instruction *I) const;
2273 
2274   /// Determine if \p F might catch asynchronous exceptions.
2275   static bool mayCatchAsynchronousExceptions(const Function &F) {
2276     return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
2277   }
2278 
2279   /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
2280   /// that internal function called from \p BB should now be looked at.
2281   void assumeLive(Attributor &A, const BasicBlock &BB) {
2282     if (!AssumedLiveBlocks.insert(&BB).second)
2283       return;
2284 
2285     // We assume that all of BB is (probably) live now and if there are calls to
2286     // internal functions we will assume that those are now live as well. This
2287     // is a performance optimization for blocks with calls to a lot of internal
2288     // functions. It can however cause dead functions to be treated as live.
2289     for (const Instruction &I : BB)
2290       if (ImmutableCallSite ICS = ImmutableCallSite(&I))
2291         if (const Function *F = ICS.getCalledFunction())
2292           if (F->hasLocalLinkage())
2293             A.markLiveInternalFunction(*F);
2294   }
2295 
2296   /// Collection of to be explored paths.
2297   SmallSetVector<const Instruction *, 8> ToBeExploredPaths;
2298 
2299   /// Collection of all assumed live BasicBlocks.
2300   DenseSet<const BasicBlock *> AssumedLiveBlocks;
2301 
2302   /// Collection of calls with noreturn attribute, assumed or knwon.
2303   SmallSetVector<const Instruction *, 4> NoReturnCalls;
2304 };
2305 
2306 struct AAIsDeadFunction final : public AAIsDeadImpl {
2307   AAIsDeadFunction(const IRPosition &IRP) : AAIsDeadImpl(IRP) {}
2308 
2309   /// See AbstractAttribute::trackStatistics()
2310   void trackStatistics() const override {
2311     STATS_DECL(PartiallyDeadBlocks, Function,
2312                "Number of basic blocks classified as partially dead");
2313     BUILD_STAT_NAME(PartiallyDeadBlocks, Function) += NoReturnCalls.size();
2314   }
2315 };
2316 
2317 bool AAIsDeadImpl::isAfterNoReturn(const Instruction *I) const {
2318   const Instruction *PrevI = I->getPrevNode();
2319   while (PrevI) {
2320     if (NoReturnCalls.count(PrevI))
2321       return true;
2322     PrevI = PrevI->getPrevNode();
2323   }
2324   return false;
2325 }
2326 
2327 const Instruction *AAIsDeadImpl::findNextNoReturn(Attributor &A,
2328                                                   const Instruction *I) {
2329   const BasicBlock *BB = I->getParent();
2330   const Function &F = *BB->getParent();
2331 
2332   // Flag to determine if we can change an invoke to a call assuming the callee
2333   // is nounwind. This is not possible if the personality of the function allows
2334   // to catch asynchronous exceptions.
2335   bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
2336 
2337   // TODO: We should have a function that determines if an "edge" is dead.
2338   //       Edges could be from an instruction to the next or from a terminator
2339   //       to the successor. For now, we need to special case the unwind block
2340   //       of InvokeInst below.
2341 
2342   while (I) {
2343     ImmutableCallSite ICS(I);
2344 
2345     if (ICS) {
2346       const IRPosition &IPos = IRPosition::callsite_function(ICS);
2347       // Regarless of the no-return property of an invoke instruction we only
2348       // learn that the regular successor is not reachable through this
2349       // instruction but the unwind block might still be.
2350       if (auto *Invoke = dyn_cast<InvokeInst>(I)) {
2351         // Use nounwind to justify the unwind block is dead as well.
2352         const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos);
2353         if (!Invoke2CallAllowed || !AANoUnw.isAssumedNoUnwind()) {
2354           assumeLive(A, *Invoke->getUnwindDest());
2355           ToBeExploredPaths.insert(&Invoke->getUnwindDest()->front());
2356         }
2357       }
2358 
2359       const auto &NoReturnAA = A.getAAFor<AANoReturn>(*this, IPos);
2360       if (NoReturnAA.isAssumedNoReturn())
2361         return I;
2362     }
2363 
2364     I = I->getNextNode();
2365   }
2366 
2367   // get new paths (reachable blocks).
2368   for (const BasicBlock *SuccBB : successors(BB)) {
2369     assumeLive(A, *SuccBB);
2370     ToBeExploredPaths.insert(&SuccBB->front());
2371   }
2372 
2373   // No noreturn instruction found.
2374   return nullptr;
2375 }
2376 
2377 ChangeStatus AAIsDeadImpl::updateImpl(Attributor &A) {
2378   ChangeStatus Status = ChangeStatus::UNCHANGED;
2379 
2380   // Temporary collection to iterate over existing noreturn instructions. This
2381   // will alow easier modification of NoReturnCalls collection
2382   SmallVector<const Instruction *, 8> NoReturnChanged;
2383 
2384   for (const Instruction *I : NoReturnCalls)
2385     NoReturnChanged.push_back(I);
2386 
2387   for (const Instruction *I : NoReturnChanged) {
2388     size_t Size = ToBeExploredPaths.size();
2389 
2390     const Instruction *NextNoReturnI = findNextNoReturn(A, I);
2391     if (NextNoReturnI != I) {
2392       Status = ChangeStatus::CHANGED;
2393       NoReturnCalls.remove(I);
2394       if (NextNoReturnI)
2395         NoReturnCalls.insert(NextNoReturnI);
2396     }
2397 
2398     // Explore new paths.
2399     while (Size != ToBeExploredPaths.size()) {
2400       Status = ChangeStatus::CHANGED;
2401       if (const Instruction *NextNoReturnI =
2402               findNextNoReturn(A, ToBeExploredPaths[Size++]))
2403         NoReturnCalls.insert(NextNoReturnI);
2404     }
2405   }
2406 
2407   LLVM_DEBUG(dbgs() << "[AAIsDead] AssumedLiveBlocks: "
2408                     << AssumedLiveBlocks.size() << " Total number of blocks: "
2409                     << getAssociatedFunction()->size() << "\n");
2410 
2411   // If we know everything is live there is no need to query for liveness.
2412   if (NoReturnCalls.empty() &&
2413       getAssociatedFunction()->size() == AssumedLiveBlocks.size()) {
2414     // Indicating a pessimistic fixpoint will cause the state to be "invalid"
2415     // which will cause the Attributor to not return the AAIsDead on request,
2416     // which will prevent us from querying isAssumedDead().
2417     indicatePessimisticFixpoint();
2418     assert(!isValidState() && "Expected an invalid state!");
2419     Status = ChangeStatus::CHANGED;
2420   }
2421 
2422   return Status;
2423 }
2424 
2425 /// Liveness information for a call sites.
2426 struct AAIsDeadCallSite final : AAIsDeadImpl {
2427   AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadImpl(IRP) {}
2428 
2429   /// See AbstractAttribute::initialize(...).
2430   void initialize(Attributor &A) override {
2431     // TODO: Once we have call site specific value information we can provide
2432     //       call site specific liveness information and then it makes
2433     //       sense to specialize attributes for call sites instead of
2434     //       redirecting requests to the callee.
2435     llvm_unreachable("Abstract attributes for liveness are not "
2436                      "supported for call sites yet!");
2437   }
2438 
2439   /// See AbstractAttribute::updateImpl(...).
2440   ChangeStatus updateImpl(Attributor &A) override {
2441     return indicatePessimisticFixpoint();
2442   }
2443 
2444   /// See AbstractAttribute::trackStatistics()
2445   void trackStatistics() const override {}
2446 };
2447 
2448 /// -------------------- Dereferenceable Argument Attribute --------------------
2449 
2450 template <>
2451 ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
2452                                                      const DerefState &R) {
2453   ChangeStatus CS0 = clampStateAndIndicateChange<IncIntegerState>(
2454       S.DerefBytesState, R.DerefBytesState);
2455   ChangeStatus CS1 =
2456       clampStateAndIndicateChange<BooleanState>(S.GlobalState, R.GlobalState);
2457   return CS0 | CS1;
2458 }
2459 
2460 struct AADereferenceableImpl : AADereferenceable {
2461   AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {}
2462   using StateType = DerefState;
2463 
2464   void initialize(Attributor &A) override {
2465     SmallVector<Attribute, 4> Attrs;
2466     getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
2467              Attrs);
2468     for (const Attribute &Attr : Attrs)
2469       takeKnownDerefBytesMaximum(Attr.getValueAsInt());
2470 
2471     NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition());
2472 
2473     const IRPosition &IRP = this->getIRPosition();
2474     bool IsFnInterface = IRP.isFnInterfaceKind();
2475     const Function *FnScope = IRP.getAnchorScope();
2476     if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition()))
2477       indicatePessimisticFixpoint();
2478   }
2479 
2480   /// See AbstractAttribute::getState()
2481   /// {
2482   StateType &getState() override { return *this; }
2483   const StateType &getState() const override { return *this; }
2484   /// }
2485 
2486   /// See AAFromMustBeExecutedContext
2487   bool followUse(Attributor &A, const Use *U, const Instruction *I) {
2488     bool IsNonNull = false;
2489     bool TrackUse = false;
2490     int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
2491         A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
2492     takeKnownDerefBytesMaximum(DerefBytes);
2493     return TrackUse;
2494   }
2495 
2496   void getDeducedAttributes(LLVMContext &Ctx,
2497                             SmallVectorImpl<Attribute> &Attrs) const override {
2498     // TODO: Add *_globally support
2499     if (isAssumedNonNull())
2500       Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
2501           Ctx, getAssumedDereferenceableBytes()));
2502     else
2503       Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
2504           Ctx, getAssumedDereferenceableBytes()));
2505   }
2506 
2507   /// See AbstractAttribute::getAsStr().
2508   const std::string getAsStr() const override {
2509     if (!getAssumedDereferenceableBytes())
2510       return "unknown-dereferenceable";
2511     return std::string("dereferenceable") +
2512            (isAssumedNonNull() ? "" : "_or_null") +
2513            (isAssumedGlobal() ? "_globally" : "") + "<" +
2514            std::to_string(getKnownDereferenceableBytes()) + "-" +
2515            std::to_string(getAssumedDereferenceableBytes()) + ">";
2516   }
2517 };
2518 
2519 /// Dereferenceable attribute for a floating value.
2520 struct AADereferenceableFloating
2521     : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> {
2522   using Base =
2523       AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>;
2524   AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {}
2525 
2526   /// See AbstractAttribute::updateImpl(...).
2527   ChangeStatus updateImpl(Attributor &A) override {
2528     ChangeStatus Change = Base::updateImpl(A);
2529 
2530     const DataLayout &DL = A.getDataLayout();
2531 
2532     auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool {
2533       unsigned IdxWidth =
2534           DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
2535       APInt Offset(IdxWidth, 0);
2536       const Value *Base =
2537           V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
2538 
2539       const auto &AA =
2540           A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base));
2541       int64_t DerefBytes = 0;
2542       if (!Stripped && this == &AA) {
2543         // Use IR information if we did not strip anything.
2544         // TODO: track globally.
2545         bool CanBeNull;
2546         DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull);
2547         T.GlobalState.indicatePessimisticFixpoint();
2548       } else {
2549         const DerefState &DS = static_cast<const DerefState &>(AA.getState());
2550         DerefBytes = DS.DerefBytesState.getAssumed();
2551         T.GlobalState &= DS.GlobalState;
2552       }
2553 
2554       // For now we do not try to "increase" dereferenceability due to negative
2555       // indices as we first have to come up with code to deal with loops and
2556       // for overflows of the dereferenceable bytes.
2557       int64_t OffsetSExt = Offset.getSExtValue();
2558       if (OffsetSExt < 0)
2559         OffsetSExt = 0;
2560 
2561       T.takeAssumedDerefBytesMinimum(
2562           std::max(int64_t(0), DerefBytes - OffsetSExt));
2563 
2564       if (this == &AA) {
2565         if (!Stripped) {
2566           // If nothing was stripped IR information is all we got.
2567           T.takeKnownDerefBytesMaximum(
2568               std::max(int64_t(0), DerefBytes - OffsetSExt));
2569           T.indicatePessimisticFixpoint();
2570         } else if (OffsetSExt > 0) {
2571           // If something was stripped but there is circular reasoning we look
2572           // for the offset. If it is positive we basically decrease the
2573           // dereferenceable bytes in a circluar loop now, which will simply
2574           // drive them down to the known value in a very slow way which we
2575           // can accelerate.
2576           T.indicatePessimisticFixpoint();
2577         }
2578       }
2579 
2580       return T.isValidState();
2581     };
2582 
2583     DerefState T;
2584     if (!genericValueTraversal<AADereferenceable, DerefState>(
2585             A, getIRPosition(), *this, T, VisitValueCB))
2586       return indicatePessimisticFixpoint();
2587 
2588     return Change | clampStateAndIndicateChange(getState(), T);
2589   }
2590 
2591   /// See AbstractAttribute::trackStatistics()
2592   void trackStatistics() const override {
2593     STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
2594   }
2595 };
2596 
2597 /// Dereferenceable attribute for a return value.
2598 struct AADereferenceableReturned final
2599     : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl,
2600                                    DerefState> {
2601   AADereferenceableReturned(const IRPosition &IRP)
2602       : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl,
2603                                      DerefState>(IRP) {}
2604 
2605   /// See AbstractAttribute::trackStatistics()
2606   void trackStatistics() const override {
2607     STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
2608   }
2609 };
2610 
2611 /// Dereferenceable attribute for an argument
2612 struct AADereferenceableArgument final
2613     : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
2614           AADereferenceable, AADereferenceableImpl, DerefState> {
2615   using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
2616       AADereferenceable, AADereferenceableImpl, DerefState>;
2617   AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {}
2618 
2619   /// See AbstractAttribute::trackStatistics()
2620   void trackStatistics() const override {
2621     STATS_DECLTRACK_ARG_ATTR(dereferenceable)
2622   }
2623 };
2624 
2625 /// Dereferenceable attribute for a call site argument.
2626 struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
2627   AADereferenceableCallSiteArgument(const IRPosition &IRP)
2628       : AADereferenceableFloating(IRP) {}
2629 
2630   /// See AbstractAttribute::trackStatistics()
2631   void trackStatistics() const override {
2632     STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
2633   }
2634 };
2635 
2636 /// Dereferenceable attribute deduction for a call site return value.
2637 struct AADereferenceableCallSiteReturned final
2638     : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
2639           AADereferenceable, AADereferenceableImpl> {
2640   using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
2641       AADereferenceable, AADereferenceableImpl>;
2642   AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {}
2643 
2644   /// See AbstractAttribute::initialize(...).
2645   void initialize(Attributor &A) override {
2646     Base::initialize(A);
2647     Function *F = getAssociatedFunction();
2648     if (!F)
2649       indicatePessimisticFixpoint();
2650   }
2651 
2652   /// See AbstractAttribute::updateImpl(...).
2653   ChangeStatus updateImpl(Attributor &A) override {
2654     // TODO: Once we have call site specific value information we can provide
2655     //       call site specific liveness information and then it makes
2656     //       sense to specialize attributes for call sites arguments instead of
2657     //       redirecting requests to the callee argument.
2658 
2659     ChangeStatus Change = Base::updateImpl(A);
2660     Function *F = getAssociatedFunction();
2661     const IRPosition &FnPos = IRPosition::returned(*F);
2662     auto &FnAA = A.getAAFor<AADereferenceable>(*this, FnPos);
2663     return Change |
2664            clampStateAndIndicateChange(
2665                getState(), static_cast<const DerefState &>(FnAA.getState()));
2666   }
2667 
2668   /// See AbstractAttribute::trackStatistics()
2669   void trackStatistics() const override {
2670     STATS_DECLTRACK_CS_ATTR(dereferenceable);
2671   }
2672 };
2673 
2674 // ------------------------ Align Argument Attribute ------------------------
2675 
2676 struct AAAlignImpl : AAAlign {
2677   AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {}
2678 
2679   // Max alignemnt value allowed in IR
2680   static const unsigned MAX_ALIGN = 1U << 29;
2681 
2682   /// See AbstractAttribute::initialize(...).
2683   void initialize(Attributor &A) override {
2684     takeAssumedMinimum(MAX_ALIGN);
2685 
2686     SmallVector<Attribute, 4> Attrs;
2687     getAttrs({Attribute::Alignment}, Attrs);
2688     for (const Attribute &Attr : Attrs)
2689       takeKnownMaximum(Attr.getValueAsInt());
2690 
2691     if (getIRPosition().isFnInterfaceKind() &&
2692         (!getAssociatedFunction() ||
2693          !getAssociatedFunction()->hasExactDefinition()))
2694       indicatePessimisticFixpoint();
2695   }
2696 
2697   /// See AbstractAttribute::manifest(...).
2698   ChangeStatus manifest(Attributor &A) override {
2699     ChangeStatus Changed = ChangeStatus::UNCHANGED;
2700 
2701     // Check for users that allow alignment annotations.
2702     Value &AnchorVal = getIRPosition().getAnchorValue();
2703     for (const Use &U : AnchorVal.uses()) {
2704       if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
2705         if (SI->getPointerOperand() == &AnchorVal)
2706           if (SI->getAlignment() < getAssumedAlign()) {
2707             STATS_DECLTRACK(AAAlign, Store,
2708                             "Number of times alignemnt added to a store");
2709             SI->setAlignment(Align(getAssumedAlign()));
2710             Changed = ChangeStatus::CHANGED;
2711           }
2712       } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
2713         if (LI->getPointerOperand() == &AnchorVal)
2714           if (LI->getAlignment() < getAssumedAlign()) {
2715             LI->setAlignment(Align(getAssumedAlign()));
2716             STATS_DECLTRACK(AAAlign, Load,
2717                             "Number of times alignemnt added to a load");
2718             Changed = ChangeStatus::CHANGED;
2719           }
2720       }
2721     }
2722 
2723     return AAAlign::manifest(A) | Changed;
2724   }
2725 
2726   // TODO: Provide a helper to determine the implied ABI alignment and check in
2727   //       the existing manifest method and a new one for AAAlignImpl that value
2728   //       to avoid making the alignment explicit if it did not improve.
2729 
2730   /// See AbstractAttribute::getDeducedAttributes
2731   virtual void
2732   getDeducedAttributes(LLVMContext &Ctx,
2733                        SmallVectorImpl<Attribute> &Attrs) const override {
2734     if (getAssumedAlign() > 1)
2735       Attrs.emplace_back(
2736           Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
2737   }
2738 
2739   /// See AbstractAttribute::getAsStr().
2740   const std::string getAsStr() const override {
2741     return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
2742                                 "-" + std::to_string(getAssumedAlign()) + ">")
2743                              : "unknown-align";
2744   }
2745 };
2746 
2747 /// Align attribute for a floating value.
2748 struct AAAlignFloating : AAAlignImpl {
2749   AAAlignFloating(const IRPosition &IRP) : AAAlignImpl(IRP) {}
2750 
2751   /// See AbstractAttribute::updateImpl(...).
2752   ChangeStatus updateImpl(Attributor &A) override {
2753     const DataLayout &DL = A.getDataLayout();
2754 
2755     auto VisitValueCB = [&](Value &V, AAAlign::StateType &T,
2756                             bool Stripped) -> bool {
2757       const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V));
2758       if (!Stripped && this == &AA) {
2759         // Use only IR information if we did not strip anything.
2760         const MaybeAlign PA = V.getPointerAlignment(DL);
2761         T.takeKnownMaximum(PA ? PA->value() : 0);
2762         T.indicatePessimisticFixpoint();
2763       } else {
2764         // Use abstract attribute information.
2765         const AAAlign::StateType &DS =
2766             static_cast<const AAAlign::StateType &>(AA.getState());
2767         T ^= DS;
2768       }
2769       return T.isValidState();
2770     };
2771 
2772     StateType T;
2773     if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T,
2774                                                    VisitValueCB))
2775       return indicatePessimisticFixpoint();
2776 
2777     // TODO: If we know we visited all incoming values, thus no are assumed
2778     // dead, we can take the known information from the state T.
2779     return clampStateAndIndicateChange(getState(), T);
2780   }
2781 
2782   /// See AbstractAttribute::trackStatistics()
2783   void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
2784 };
2785 
2786 /// Align attribute for function return value.
2787 struct AAAlignReturned final
2788     : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
2789   AAAlignReturned(const IRPosition &IRP)
2790       : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {}
2791 
2792   /// See AbstractAttribute::trackStatistics()
2793   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
2794 };
2795 
2796 /// Align attribute for function argument.
2797 struct AAAlignArgument final
2798     : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
2799   AAAlignArgument(const IRPosition &IRP)
2800       : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>(IRP) {}
2801 
2802   /// See AbstractAttribute::trackStatistics()
2803   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
2804 };
2805 
2806 struct AAAlignCallSiteArgument final : AAAlignFloating {
2807   AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {}
2808 
2809   /// See AbstractAttribute::manifest(...).
2810   ChangeStatus manifest(Attributor &A) override {
2811     return AAAlignImpl::manifest(A);
2812   }
2813 
2814   /// See AbstractAttribute::trackStatistics()
2815   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
2816 };
2817 
2818 /// Align attribute deduction for a call site return value.
2819 struct AAAlignCallSiteReturned final : AAAlignImpl {
2820   AAAlignCallSiteReturned(const IRPosition &IRP) : AAAlignImpl(IRP) {}
2821 
2822   /// See AbstractAttribute::initialize(...).
2823   void initialize(Attributor &A) override {
2824     AAAlignImpl::initialize(A);
2825     Function *F = getAssociatedFunction();
2826     if (!F)
2827       indicatePessimisticFixpoint();
2828   }
2829 
2830   /// See AbstractAttribute::updateImpl(...).
2831   ChangeStatus updateImpl(Attributor &A) override {
2832     // TODO: Once we have call site specific value information we can provide
2833     //       call site specific liveness information and then it makes
2834     //       sense to specialize attributes for call sites arguments instead of
2835     //       redirecting requests to the callee argument.
2836     Function *F = getAssociatedFunction();
2837     const IRPosition &FnPos = IRPosition::returned(*F);
2838     auto &FnAA = A.getAAFor<AAAlign>(*this, FnPos);
2839     return clampStateAndIndicateChange(
2840         getState(), static_cast<const AAAlign::StateType &>(FnAA.getState()));
2841   }
2842 
2843   /// See AbstractAttribute::trackStatistics()
2844   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
2845 };
2846 
2847 /// ------------------ Function No-Return Attribute ----------------------------
2848 struct AANoReturnImpl : public AANoReturn {
2849   AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {}
2850 
2851   /// See AbstractAttribute::initialize(...).
2852   void initialize(Attributor &A) override {
2853     AANoReturn::initialize(A);
2854     Function *F = getAssociatedFunction();
2855     if (!F || F->hasFnAttribute(Attribute::WillReturn))
2856       indicatePessimisticFixpoint();
2857   }
2858 
2859   /// See AbstractAttribute::getAsStr().
2860   const std::string getAsStr() const override {
2861     return getAssumed() ? "noreturn" : "may-return";
2862   }
2863 
2864   /// See AbstractAttribute::updateImpl(Attributor &A).
2865   virtual ChangeStatus updateImpl(Attributor &A) override {
2866     const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, getIRPosition());
2867     if (WillReturnAA.isKnownWillReturn())
2868       return indicatePessimisticFixpoint();
2869     auto CheckForNoReturn = [](Instruction &) { return false; };
2870     if (!A.checkForAllInstructions(CheckForNoReturn, *this,
2871                                    {(unsigned)Instruction::Ret}))
2872       return indicatePessimisticFixpoint();
2873     return ChangeStatus::UNCHANGED;
2874   }
2875 };
2876 
2877 struct AANoReturnFunction final : AANoReturnImpl {
2878   AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
2879 
2880   /// See AbstractAttribute::trackStatistics()
2881   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
2882 };
2883 
2884 /// NoReturn attribute deduction for a call sites.
2885 struct AANoReturnCallSite final : AANoReturnImpl {
2886   AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
2887 
2888   /// See AbstractAttribute::updateImpl(...).
2889   ChangeStatus updateImpl(Attributor &A) override {
2890     // TODO: Once we have call site specific value information we can provide
2891     //       call site specific liveness information and then it makes
2892     //       sense to specialize attributes for call sites arguments instead of
2893     //       redirecting requests to the callee argument.
2894     Function *F = getAssociatedFunction();
2895     const IRPosition &FnPos = IRPosition::function(*F);
2896     auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos);
2897     return clampStateAndIndicateChange(
2898         getState(),
2899         static_cast<const AANoReturn::StateType &>(FnAA.getState()));
2900   }
2901 
2902   /// See AbstractAttribute::trackStatistics()
2903   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
2904 };
2905 
2906 /// ----------------------- Variable Capturing ---------------------------------
2907 
2908 /// A class to hold the state of for no-capture attributes.
2909 struct AANoCaptureImpl : public AANoCapture {
2910   AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {}
2911 
2912   /// See AbstractAttribute::initialize(...).
2913   void initialize(Attributor &A) override {
2914     AANoCapture::initialize(A);
2915 
2916     // You cannot "capture" null in the default address space.
2917     if (isa<ConstantPointerNull>(getAssociatedValue()) &&
2918         getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
2919       indicateOptimisticFixpoint();
2920       return;
2921     }
2922 
2923     const IRPosition &IRP = getIRPosition();
2924     const Function *F =
2925         getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
2926 
2927     // Check what state the associated function can actually capture.
2928     if (F)
2929       determineFunctionCaptureCapabilities(IRP, *F, *this);
2930     else
2931       indicatePessimisticFixpoint();
2932   }
2933 
2934   /// See AbstractAttribute::updateImpl(...).
2935   ChangeStatus updateImpl(Attributor &A) override;
2936 
2937   /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
2938   virtual void
2939   getDeducedAttributes(LLVMContext &Ctx,
2940                        SmallVectorImpl<Attribute> &Attrs) const override {
2941     if (!isAssumedNoCaptureMaybeReturned())
2942       return;
2943 
2944     if (getArgNo() >= 0) {
2945       if (isAssumedNoCapture())
2946         Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
2947       else if (ManifestInternal)
2948         Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
2949     }
2950   }
2951 
2952   /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
2953   /// depending on the ability of the function associated with \p IRP to capture
2954   /// state in memory and through "returning/throwing", respectively.
2955   static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
2956                                                    const Function &F,
2957                                                    BitIntegerState &State) {
2958     // TODO: Once we have memory behavior attributes we should use them here.
2959 
2960     // If we know we cannot communicate or write to memory, we do not care about
2961     // ptr2int anymore.
2962     if (F.onlyReadsMemory() && F.doesNotThrow() &&
2963         F.getReturnType()->isVoidTy()) {
2964       State.addKnownBits(NO_CAPTURE);
2965       return;
2966     }
2967 
2968     // A function cannot capture state in memory if it only reads memory, it can
2969     // however return/throw state and the state might be influenced by the
2970     // pointer value, e.g., loading from a returned pointer might reveal a bit.
2971     if (F.onlyReadsMemory())
2972       State.addKnownBits(NOT_CAPTURED_IN_MEM);
2973 
2974     // A function cannot communicate state back if it does not through
2975     // exceptions and doesn not return values.
2976     if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
2977       State.addKnownBits(NOT_CAPTURED_IN_RET);
2978 
2979     // Check existing "returned" attributes.
2980     int ArgNo = IRP.getArgNo();
2981     if (F.doesNotThrow() && ArgNo >= 0) {
2982       for (unsigned u = 0, e = F.arg_size(); u< e; ++u)
2983         if (F.hasParamAttribute(u, Attribute::Returned)) {
2984           if (u == unsigned(ArgNo))
2985             State.removeAssumedBits(NOT_CAPTURED_IN_RET);
2986           else if (F.onlyReadsMemory())
2987             State.addKnownBits(NO_CAPTURE);
2988           else
2989             State.addKnownBits(NOT_CAPTURED_IN_RET);
2990           break;
2991         }
2992     }
2993   }
2994 
2995   /// See AbstractState::getAsStr().
2996   const std::string getAsStr() const override {
2997     if (isKnownNoCapture())
2998       return "known not-captured";
2999     if (isAssumedNoCapture())
3000       return "assumed not-captured";
3001     if (isKnownNoCaptureMaybeReturned())
3002       return "known not-captured-maybe-returned";
3003     if (isAssumedNoCaptureMaybeReturned())
3004       return "assumed not-captured-maybe-returned";
3005     return "assumed-captured";
3006   }
3007 };
3008 
3009 /// Attributor-aware capture tracker.
3010 struct AACaptureUseTracker final : public CaptureTracker {
3011 
3012   /// Create a capture tracker that can lookup in-flight abstract attributes
3013   /// through the Attributor \p A.
3014   ///
3015   /// If a use leads to a potential capture, \p CapturedInMemory is set and the
3016   /// search is stopped. If a use leads to a return instruction,
3017   /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
3018   /// If a use leads to a ptr2int which may capture the value,
3019   /// \p CapturedInInteger is set. If a use is found that is currently assumed
3020   /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
3021   /// set. All values in \p PotentialCopies are later tracked as well. For every
3022   /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
3023   /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
3024   /// conservatively set to true.
3025   AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
3026                       const AAIsDead &IsDeadAA, BitIntegerState &State,
3027                       SmallVectorImpl<const Value *> &PotentialCopies,
3028                       unsigned &RemainingUsesToExplore)
3029       : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
3030         PotentialCopies(PotentialCopies),
3031         RemainingUsesToExplore(RemainingUsesToExplore) {}
3032 
3033   /// Determine if \p V maybe captured. *Also updates the state!*
3034   bool valueMayBeCaptured(const Value *V) {
3035     if (V->getType()->isPointerTy()) {
3036       PointerMayBeCaptured(V, this);
3037     } else {
3038       State.indicatePessimisticFixpoint();
3039     }
3040     return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
3041   }
3042 
3043   /// See CaptureTracker::tooManyUses().
3044   void tooManyUses() override {
3045     State.removeAssumedBits(AANoCapture::NO_CAPTURE);
3046   }
3047 
3048   bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
3049     if (CaptureTracker::isDereferenceableOrNull(O, DL))
3050       return true;
3051     const auto &DerefAA =
3052         A.getAAFor<AADereferenceable>(NoCaptureAA, IRPosition::value(*O));
3053     return DerefAA.getAssumedDereferenceableBytes();
3054   }
3055 
3056   /// See CaptureTracker::captured(...).
3057   bool captured(const Use *U) override {
3058     Instruction *UInst = cast<Instruction>(U->getUser());
3059     LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
3060                       << "\n");
3061 
3062     // Because we may reuse the tracker multiple times we keep track of the
3063     // number of explored uses ourselves as well.
3064     if (RemainingUsesToExplore-- == 0) {
3065       LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
3066       return isCapturedIn(/* Memory */ true, /* Integer */ true,
3067                           /* Return */ true);
3068     }
3069 
3070     // Deal with ptr2int by following uses.
3071     if (isa<PtrToIntInst>(UInst)) {
3072       LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
3073       return valueMayBeCaptured(UInst);
3074     }
3075 
3076     // Explicitly catch return instructions.
3077     if (isa<ReturnInst>(UInst))
3078       return isCapturedIn(/* Memory */ false, /* Integer */ false,
3079                           /* Return */ true);
3080 
3081     // For now we only use special logic for call sites. However, the tracker
3082     // itself knows about a lot of other non-capturing cases already.
3083     CallSite CS(UInst);
3084     if (!CS || !CS.isArgOperand(U))
3085       return isCapturedIn(/* Memory */ true, /* Integer */ true,
3086                           /* Return */ true);
3087 
3088     unsigned ArgNo = CS.getArgumentNo(U);
3089     const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
3090     // If we have a abstract no-capture attribute for the argument we can use
3091     // it to justify a non-capture attribute here. This allows recursion!
3092     auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos);
3093     if (ArgNoCaptureAA.isAssumedNoCapture())
3094       return isCapturedIn(/* Memory */ false, /* Integer */ false,
3095                           /* Return */ false);
3096     if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
3097       addPotentialCopy(CS);
3098       return isCapturedIn(/* Memory */ false, /* Integer */ false,
3099                           /* Return */ false);
3100     }
3101 
3102     // Lastly, we could not find a reason no-capture can be assumed so we don't.
3103     return isCapturedIn(/* Memory */ true, /* Integer */ true,
3104                         /* Return */ true);
3105   }
3106 
3107   /// Register \p CS as potential copy of the value we are checking.
3108   void addPotentialCopy(CallSite CS) {
3109     PotentialCopies.push_back(CS.getInstruction());
3110   }
3111 
3112   /// See CaptureTracker::shouldExplore(...).
3113   bool shouldExplore(const Use *U) override {
3114     // Check liveness.
3115     return !IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser()));
3116   }
3117 
3118   /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
3119   /// \p CapturedInRet, then return the appropriate value for use in the
3120   /// CaptureTracker::captured() interface.
3121   bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
3122                     bool CapturedInRet) {
3123     LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
3124                       << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
3125     if (CapturedInMem)
3126       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
3127     if (CapturedInInt)
3128       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
3129     if (CapturedInRet)
3130       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
3131     return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
3132   }
3133 
3134 private:
3135   /// The attributor providing in-flight abstract attributes.
3136   Attributor &A;
3137 
3138   /// The abstract attribute currently updated.
3139   AANoCapture &NoCaptureAA;
3140 
3141   /// The abstract liveness state.
3142   const AAIsDead &IsDeadAA;
3143 
3144   /// The state currently updated.
3145   BitIntegerState &State;
3146 
3147   /// Set of potential copies of the tracked value.
3148   SmallVectorImpl<const Value *> &PotentialCopies;
3149 
3150   /// Global counter to limit the number of explored uses.
3151   unsigned &RemainingUsesToExplore;
3152 };
3153 
3154 ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
3155   const IRPosition &IRP = getIRPosition();
3156   const Value *V =
3157       getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue();
3158   if (!V)
3159     return indicatePessimisticFixpoint();
3160 
3161   const Function *F =
3162       getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
3163   assert(F && "Expected a function!");
3164   const IRPosition &FnPos = IRPosition::function(*F);
3165   const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos);
3166 
3167   AANoCapture::StateType T;
3168 
3169   // Readonly means we cannot capture through memory.
3170   const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos);
3171   if (FnMemAA.isAssumedReadOnly()) {
3172     T.addKnownBits(NOT_CAPTURED_IN_MEM);
3173     if (FnMemAA.isKnownReadOnly())
3174       addKnownBits(NOT_CAPTURED_IN_MEM);
3175   }
3176 
3177   // Make sure all returned values are different than the underlying value.
3178   // TODO: we could do this in a more sophisticated way inside
3179   //       AAReturnedValues, e.g., track all values that escape through returns
3180   //       directly somehow.
3181   auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
3182     bool SeenConstant = false;
3183     for (auto &It : RVAA.returned_values()) {
3184       if (isa<Constant>(It.first)) {
3185         if (SeenConstant)
3186           return false;
3187         SeenConstant = true;
3188       } else if (!isa<Argument>(It.first) ||
3189                  It.first == getAssociatedArgument())
3190         return false;
3191     }
3192     return true;
3193   };
3194 
3195   const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, FnPos);
3196   if (NoUnwindAA.isAssumedNoUnwind()) {
3197     bool IsVoidTy = F->getReturnType()->isVoidTy();
3198     const AAReturnedValues *RVAA =
3199         IsVoidTy ? nullptr : &A.getAAFor<AAReturnedValues>(*this, FnPos);
3200     if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
3201       T.addKnownBits(NOT_CAPTURED_IN_RET);
3202       if (T.isKnown(NOT_CAPTURED_IN_MEM))
3203         return ChangeStatus::UNCHANGED;
3204       if (NoUnwindAA.isKnownNoUnwind() &&
3205           (IsVoidTy || RVAA->getState().isAtFixpoint())) {
3206         addKnownBits(NOT_CAPTURED_IN_RET);
3207         if (isKnown(NOT_CAPTURED_IN_MEM))
3208           return indicateOptimisticFixpoint();
3209       }
3210     }
3211   }
3212 
3213   // Use the CaptureTracker interface and logic with the specialized tracker,
3214   // defined in AACaptureUseTracker, that can look at in-flight abstract
3215   // attributes and directly updates the assumed state.
3216   SmallVector<const Value *, 4> PotentialCopies;
3217   unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore;
3218   AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
3219                               RemainingUsesToExplore);
3220 
3221   // Check all potential copies of the associated value until we can assume
3222   // none will be captured or we have to assume at least one might be.
3223   unsigned Idx = 0;
3224   PotentialCopies.push_back(V);
3225   while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
3226     Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
3227 
3228   AANoCapture::StateType &S = getState();
3229   auto Assumed = S.getAssumed();
3230   S.intersectAssumedBits(T.getAssumed());
3231   return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
3232                                    : ChangeStatus::CHANGED;
3233 }
3234 
3235 /// NoCapture attribute for function arguments.
3236 struct AANoCaptureArgument final : AANoCaptureImpl {
3237   AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
3238 
3239   /// See AbstractAttribute::trackStatistics()
3240   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
3241 };
3242 
3243 /// NoCapture attribute for call site arguments.
3244 struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
3245   AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
3246 
3247   /// See AbstractAttribute::updateImpl(...).
3248   ChangeStatus updateImpl(Attributor &A) override {
3249     // TODO: Once we have call site specific value information we can provide
3250     //       call site specific liveness information and then it makes
3251     //       sense to specialize attributes for call sites arguments instead of
3252     //       redirecting requests to the callee argument.
3253     Argument *Arg = getAssociatedArgument();
3254     if (!Arg)
3255       return indicatePessimisticFixpoint();
3256     const IRPosition &ArgPos = IRPosition::argument(*Arg);
3257     auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos);
3258     return clampStateAndIndicateChange(
3259         getState(),
3260         static_cast<const AANoCapture::StateType &>(ArgAA.getState()));
3261   }
3262 
3263   /// See AbstractAttribute::trackStatistics()
3264   void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
3265 };
3266 
3267 /// NoCapture attribute for floating values.
3268 struct AANoCaptureFloating final : AANoCaptureImpl {
3269   AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
3270 
3271   /// See AbstractAttribute::trackStatistics()
3272   void trackStatistics() const override {
3273     STATS_DECLTRACK_FLOATING_ATTR(nocapture)
3274   }
3275 };
3276 
3277 /// NoCapture attribute for function return value.
3278 struct AANoCaptureReturned final : AANoCaptureImpl {
3279   AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {
3280     llvm_unreachable("NoCapture is not applicable to function returns!");
3281   }
3282 
3283   /// See AbstractAttribute::initialize(...).
3284   void initialize(Attributor &A) override {
3285     llvm_unreachable("NoCapture is not applicable to function returns!");
3286   }
3287 
3288   /// See AbstractAttribute::updateImpl(...).
3289   ChangeStatus updateImpl(Attributor &A) override {
3290     llvm_unreachable("NoCapture is not applicable to function returns!");
3291   }
3292 
3293   /// See AbstractAttribute::trackStatistics()
3294   void trackStatistics() const override {}
3295 };
3296 
3297 /// NoCapture attribute deduction for a call site return value.
3298 struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
3299   AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
3300 
3301   /// See AbstractAttribute::trackStatistics()
3302   void trackStatistics() const override {
3303     STATS_DECLTRACK_CSRET_ATTR(nocapture)
3304   }
3305 };
3306 
3307 /// ------------------ Value Simplify Attribute ----------------------------
3308 struct AAValueSimplifyImpl : AAValueSimplify {
3309   AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {}
3310 
3311   /// See AbstractAttribute::getAsStr().
3312   const std::string getAsStr() const override {
3313     return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple")
3314                         : "not-simple";
3315   }
3316 
3317   /// See AbstractAttribute::trackStatistics()
3318   void trackStatistics() const override {}
3319 
3320   /// See AAValueSimplify::getAssumedSimplifiedValue()
3321   Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
3322     if (!getAssumed())
3323       return const_cast<Value *>(&getAssociatedValue());
3324     return SimplifiedAssociatedValue;
3325   }
3326   void initialize(Attributor &A) override {}
3327 
3328   /// Helper function for querying AAValueSimplify and updating candicate.
3329   /// \param QueryingValue Value trying to unify with SimplifiedValue
3330   /// \param AccumulatedSimplifiedValue Current simplification result.
3331   static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
3332                              Value &QueryingValue,
3333                              Optional<Value *> &AccumulatedSimplifiedValue) {
3334     // FIXME: Add a typecast support.
3335 
3336     auto &ValueSimpifyAA = A.getAAFor<AAValueSimplify>(
3337         QueryingAA, IRPosition::value(QueryingValue));
3338 
3339     Optional<Value *> QueryingValueSimplified =
3340         ValueSimpifyAA.getAssumedSimplifiedValue(A);
3341 
3342     if (!QueryingValueSimplified.hasValue())
3343       return true;
3344 
3345     if (!QueryingValueSimplified.getValue())
3346       return false;
3347 
3348     Value &QueryingValueSimplifiedUnwrapped =
3349         *QueryingValueSimplified.getValue();
3350 
3351     if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
3352       return true;
3353 
3354     if (AccumulatedSimplifiedValue.hasValue())
3355       return AccumulatedSimplifiedValue == QueryingValueSimplified;
3356 
3357     LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << QueryingValue
3358                       << " is assumed to be "
3359                       << QueryingValueSimplifiedUnwrapped << "\n");
3360 
3361     AccumulatedSimplifiedValue = QueryingValueSimplified;
3362     return true;
3363   }
3364 
3365   /// See AbstractAttribute::manifest(...).
3366   ChangeStatus manifest(Attributor &A) override {
3367     ChangeStatus Changed = ChangeStatus::UNCHANGED;
3368 
3369     if (!SimplifiedAssociatedValue.hasValue() ||
3370         !SimplifiedAssociatedValue.getValue())
3371       return Changed;
3372 
3373     if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) {
3374       // We can replace the AssociatedValue with the constant.
3375       Value &V = getAssociatedValue();
3376       if (!V.user_empty() && &V != C && V.getType() == C->getType()) {
3377         LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << V << " -> " << *C
3378                           << "\n");
3379         V.replaceAllUsesWith(C);
3380         Changed = ChangeStatus::CHANGED;
3381       }
3382     }
3383 
3384     return Changed | AAValueSimplify::manifest(A);
3385   }
3386 
3387 protected:
3388   // An assumed simplified value. Initially, it is set to Optional::None, which
3389   // means that the value is not clear under current assumption. If in the
3390   // pessimistic state, getAssumedSimplifiedValue doesn't return this value but
3391   // returns orignal associated value.
3392   Optional<Value *> SimplifiedAssociatedValue;
3393 };
3394 
3395 struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
3396   AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3397 
3398   /// See AbstractAttribute::updateImpl(...).
3399   ChangeStatus updateImpl(Attributor &A) override {
3400     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
3401 
3402     auto PredForCallSite = [&](AbstractCallSite ACS) {
3403       // Check if we have an associated argument or not (which can happen for
3404       // callback calls).
3405       if (Value *ArgOp = ACS.getCallArgOperand(getArgNo()))
3406         return checkAndUpdate(A, *this, *ArgOp, SimplifiedAssociatedValue);
3407       return false;
3408     };
3409 
3410     if (!A.checkForAllCallSites(PredForCallSite, *this, true))
3411       return indicatePessimisticFixpoint();
3412 
3413     // If a candicate was found in this update, return CHANGED.
3414     return HasValueBefore == SimplifiedAssociatedValue.hasValue()
3415                ? ChangeStatus::UNCHANGED
3416                : ChangeStatus ::CHANGED;
3417   }
3418 
3419   /// See AbstractAttribute::trackStatistics()
3420   void trackStatistics() const override {
3421     STATS_DECLTRACK_ARG_ATTR(value_simplify)
3422   }
3423 };
3424 
3425 struct AAValueSimplifyReturned : AAValueSimplifyImpl {
3426   AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3427 
3428   /// See AbstractAttribute::updateImpl(...).
3429   ChangeStatus updateImpl(Attributor &A) override {
3430     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
3431 
3432     auto PredForReturned = [&](Value &V) {
3433       return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
3434     };
3435 
3436     if (!A.checkForAllReturnedValues(PredForReturned, *this))
3437       return indicatePessimisticFixpoint();
3438 
3439     // If a candicate was found in this update, return CHANGED.
3440     return HasValueBefore == SimplifiedAssociatedValue.hasValue()
3441                ? ChangeStatus::UNCHANGED
3442                : ChangeStatus ::CHANGED;
3443   }
3444   /// See AbstractAttribute::trackStatistics()
3445   void trackStatistics() const override {
3446     STATS_DECLTRACK_FNRET_ATTR(value_simplify)
3447   }
3448 };
3449 
3450 struct AAValueSimplifyFloating : AAValueSimplifyImpl {
3451   AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3452 
3453   /// See AbstractAttribute::initialize(...).
3454   void initialize(Attributor &A) override {
3455     Value &V = getAnchorValue();
3456 
3457     // TODO: add other stuffs
3458     if (isa<Constant>(V) || isa<UndefValue>(V))
3459       indicatePessimisticFixpoint();
3460   }
3461 
3462   /// See AbstractAttribute::updateImpl(...).
3463   ChangeStatus updateImpl(Attributor &A) override {
3464     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
3465 
3466     auto VisitValueCB = [&](Value &V, BooleanState, bool Stripped) -> bool {
3467       auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V));
3468       if (!Stripped && this == &AA) {
3469         // TODO: Look the instruction and check recursively.
3470         LLVM_DEBUG(
3471             dbgs() << "[Attributor][ValueSimplify] Can't be stripped more : "
3472                    << V << "\n");
3473         indicatePessimisticFixpoint();
3474         return false;
3475       }
3476       return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
3477     };
3478 
3479     if (!genericValueTraversal<AAValueSimplify, BooleanState>(
3480             A, getIRPosition(), *this, static_cast<BooleanState &>(*this),
3481             VisitValueCB))
3482       return indicatePessimisticFixpoint();
3483 
3484     // If a candicate was found in this update, return CHANGED.
3485 
3486     return HasValueBefore == SimplifiedAssociatedValue.hasValue()
3487                ? ChangeStatus::UNCHANGED
3488                : ChangeStatus ::CHANGED;
3489   }
3490 
3491   /// See AbstractAttribute::trackStatistics()
3492   void trackStatistics() const override {
3493     STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
3494   }
3495 };
3496 
3497 struct AAValueSimplifyFunction : AAValueSimplifyImpl {
3498   AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3499 
3500   /// See AbstractAttribute::initialize(...).
3501   void initialize(Attributor &A) override {
3502     SimplifiedAssociatedValue = &getAnchorValue();
3503     indicateOptimisticFixpoint();
3504   }
3505   /// See AbstractAttribute::initialize(...).
3506   ChangeStatus updateImpl(Attributor &A) override {
3507     llvm_unreachable(
3508         "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
3509   }
3510   /// See AbstractAttribute::trackStatistics()
3511   void trackStatistics() const override {
3512     STATS_DECLTRACK_FN_ATTR(value_simplify)
3513   }
3514 };
3515 
3516 struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
3517   AAValueSimplifyCallSite(const IRPosition &IRP)
3518       : AAValueSimplifyFunction(IRP) {}
3519   /// See AbstractAttribute::trackStatistics()
3520   void trackStatistics() const override {
3521     STATS_DECLTRACK_CS_ATTR(value_simplify)
3522   }
3523 };
3524 
3525 struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned {
3526   AAValueSimplifyCallSiteReturned(const IRPosition &IRP)
3527       : AAValueSimplifyReturned(IRP) {}
3528 
3529   void trackStatistics() const override {
3530     STATS_DECLTRACK_CSRET_ATTR(value_simplify)
3531   }
3532 };
3533 struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
3534   AAValueSimplifyCallSiteArgument(const IRPosition &IRP)
3535       : AAValueSimplifyFloating(IRP) {}
3536 
3537   void trackStatistics() const override {
3538     STATS_DECLTRACK_CSARG_ATTR(value_simplify)
3539   }
3540 };
3541 
3542 /// ----------------------- Heap-To-Stack Conversion ---------------------------
3543 struct AAHeapToStackImpl : public AAHeapToStack {
3544   AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {}
3545 
3546   const std::string getAsStr() const override {
3547     return "[H2S] Mallocs: " + std::to_string(MallocCalls.size());
3548   }
3549 
3550   ChangeStatus manifest(Attributor &A) override {
3551     assert(getState().isValidState() &&
3552            "Attempted to manifest an invalid state!");
3553 
3554     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
3555     Function *F = getAssociatedFunction();
3556     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
3557 
3558     for (Instruction *MallocCall : MallocCalls) {
3559       // This malloc cannot be replaced.
3560       if (BadMallocCalls.count(MallocCall))
3561         continue;
3562 
3563       for (Instruction *FreeCall : FreesForMalloc[MallocCall]) {
3564         LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
3565         A.deleteAfterManifest(*FreeCall);
3566         HasChanged = ChangeStatus::CHANGED;
3567       }
3568 
3569       LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall
3570                         << "\n");
3571 
3572       Constant *Size;
3573       if (isCallocLikeFn(MallocCall, TLI)) {
3574         auto *Num = cast<ConstantInt>(MallocCall->getOperand(0));
3575         auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1));
3576         APInt TotalSize = SizeT->getValue() * Num->getValue();
3577         Size =
3578             ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize);
3579       } else {
3580         Size = cast<ConstantInt>(MallocCall->getOperand(0));
3581       }
3582 
3583       unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace();
3584       Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
3585                                        Size, "", MallocCall->getNextNode());
3586 
3587       if (AI->getType() != MallocCall->getType())
3588         AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc",
3589                              AI->getNextNode());
3590 
3591       MallocCall->replaceAllUsesWith(AI);
3592 
3593       if (auto *II = dyn_cast<InvokeInst>(MallocCall)) {
3594         auto *NBB = II->getNormalDest();
3595         BranchInst::Create(NBB, MallocCall->getParent());
3596         A.deleteAfterManifest(*MallocCall);
3597       } else {
3598         A.deleteAfterManifest(*MallocCall);
3599       }
3600 
3601       if (isCallocLikeFn(MallocCall, TLI)) {
3602         auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc",
3603                                    AI->getNextNode());
3604         Value *Ops[] = {
3605             BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size,
3606             ConstantInt::get(Type::getInt1Ty(F->getContext()), false)};
3607 
3608         Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()};
3609         Module *M = F->getParent();
3610         Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
3611         CallInst::Create(Fn, Ops, "", BI->getNextNode());
3612       }
3613       HasChanged = ChangeStatus::CHANGED;
3614     }
3615 
3616     return HasChanged;
3617   }
3618 
3619   /// Collection of all malloc calls in a function.
3620   SmallSetVector<Instruction *, 4> MallocCalls;
3621 
3622   /// Collection of malloc calls that cannot be converted.
3623   DenseSet<const Instruction *> BadMallocCalls;
3624 
3625   /// A map for each malloc call to the set of associated free calls.
3626   DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc;
3627 
3628   ChangeStatus updateImpl(Attributor &A) override;
3629 };
3630 
3631 ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
3632   const Function *F = getAssociatedFunction();
3633   const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
3634 
3635   auto UsesCheck = [&](Instruction &I) {
3636     SmallPtrSet<const Use *, 8> Visited;
3637     SmallVector<const Use *, 8> Worklist;
3638 
3639     for (Use &U : I.uses())
3640       Worklist.push_back(&U);
3641 
3642     while (!Worklist.empty()) {
3643       const Use *U = Worklist.pop_back_val();
3644       if (!Visited.insert(U).second)
3645         continue;
3646 
3647       auto *UserI = U->getUser();
3648 
3649       if (isa<LoadInst>(UserI))
3650         continue;
3651       if (auto *SI = dyn_cast<StoreInst>(UserI)) {
3652         if (SI->getValueOperand() == U->get()) {
3653           LLVM_DEBUG(dbgs() << "[H2S] escaping store to memory: " << *UserI << "\n");
3654           return false;
3655         }
3656         // A store into the malloc'ed memory is fine.
3657         continue;
3658       }
3659 
3660       // NOTE: Right now, if a function that has malloc pointer as an argument
3661       // frees memory, we assume that the malloc pointer is freed.
3662 
3663       // TODO: Add nofree callsite argument attribute to indicate that pointer
3664       // argument is not freed.
3665       if (auto *CB = dyn_cast<CallBase>(UserI)) {
3666         if (!CB->isArgOperand(U))
3667           continue;
3668 
3669         if (CB->isLifetimeStartOrEnd())
3670           continue;
3671 
3672         // Record malloc.
3673         if (isFreeCall(UserI, TLI)) {
3674           FreesForMalloc[&I].insert(
3675               cast<Instruction>(const_cast<User *>(UserI)));
3676           continue;
3677         }
3678 
3679         // If a function does not free memory we are fine
3680         const auto &NoFreeAA =
3681             A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(*CB));
3682 
3683         unsigned ArgNo = U - CB->arg_begin();
3684         const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
3685             *this, IRPosition::callsite_argument(*CB, ArgNo));
3686 
3687         if (!NoCaptureAA.isAssumedNoCapture() || !NoFreeAA.isAssumedNoFree()) {
3688           LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
3689           return false;
3690         }
3691         continue;
3692       }
3693 
3694       if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI)) {
3695         for (Use &U : UserI->uses())
3696           Worklist.push_back(&U);
3697         continue;
3698       }
3699 
3700       // Unknown user.
3701       LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
3702       return false;
3703     }
3704     return true;
3705   };
3706 
3707   auto MallocCallocCheck = [&](Instruction &I) {
3708     if (BadMallocCalls.count(&I))
3709       return true;
3710 
3711     bool IsMalloc = isMallocLikeFn(&I, TLI);
3712     bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI);
3713     if (!IsMalloc && !IsCalloc) {
3714       BadMallocCalls.insert(&I);
3715       return true;
3716     }
3717 
3718     if (IsMalloc) {
3719       if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
3720         if (Size->getValue().sle(MaxHeapToStackSize))
3721           if (UsesCheck(I)) {
3722             MallocCalls.insert(&I);
3723             return true;
3724           }
3725     } else if (IsCalloc) {
3726       bool Overflow = false;
3727       if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
3728         if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
3729           if ((Size->getValue().umul_ov(Num->getValue(), Overflow))
3730                    .sle(MaxHeapToStackSize))
3731             if (!Overflow && UsesCheck(I)) {
3732               MallocCalls.insert(&I);
3733               return true;
3734             }
3735     }
3736 
3737     BadMallocCalls.insert(&I);
3738     return true;
3739   };
3740 
3741   size_t NumBadMallocs = BadMallocCalls.size();
3742 
3743   A.checkForAllCallLikeInstructions(MallocCallocCheck, *this);
3744 
3745   if (NumBadMallocs != BadMallocCalls.size())
3746     return ChangeStatus::CHANGED;
3747 
3748   return ChangeStatus::UNCHANGED;
3749 }
3750 
3751 struct AAHeapToStackFunction final : public AAHeapToStackImpl {
3752   AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {}
3753 
3754   /// See AbstractAttribute::trackStatistics()
3755   void trackStatistics() const override {
3756     STATS_DECL(MallocCalls, Function,
3757                "Number of MallocCalls converted to allocas");
3758     BUILD_STAT_NAME(MallocCalls, Function) += MallocCalls.size();
3759   }
3760 };
3761 
3762 /// -------------------- Memory Behavior Attributes ----------------------------
3763 /// Includes read-none, read-only, and write-only.
3764 /// ----------------------------------------------------------------------------
3765 struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
3766   AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {}
3767 
3768   /// See AbstractAttribute::initialize(...).
3769   void initialize(Attributor &A) override {
3770     intersectAssumedBits(BEST_STATE);
3771     getKnownStateFromValue(getIRPosition(), getState());
3772     IRAttribute::initialize(A);
3773   }
3774 
3775   /// Return the memory behavior information encoded in the IR for \p IRP.
3776   static void getKnownStateFromValue(const IRPosition &IRP,
3777                                      BitIntegerState &State) {
3778     SmallVector<Attribute, 2> Attrs;
3779     IRP.getAttrs(AttrKinds, Attrs);
3780     for (const Attribute &Attr : Attrs) {
3781       switch (Attr.getKindAsEnum()) {
3782       case Attribute::ReadNone:
3783         State.addKnownBits(NO_ACCESSES);
3784         break;
3785       case Attribute::ReadOnly:
3786         State.addKnownBits(NO_WRITES);
3787         break;
3788       case Attribute::WriteOnly:
3789         State.addKnownBits(NO_READS);
3790         break;
3791       default:
3792         llvm_unreachable("Unexpcted attribute!");
3793       }
3794     }
3795 
3796     if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
3797       if (!I->mayReadFromMemory())
3798         State.addKnownBits(NO_READS);
3799       if (!I->mayWriteToMemory())
3800         State.addKnownBits(NO_WRITES);
3801     }
3802   }
3803 
3804   /// See AbstractAttribute::getDeducedAttributes(...).
3805   void getDeducedAttributes(LLVMContext &Ctx,
3806                             SmallVectorImpl<Attribute> &Attrs) const override {
3807     assert(Attrs.size() == 0);
3808     if (isAssumedReadNone())
3809       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
3810     else if (isAssumedReadOnly())
3811       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
3812     else if (isAssumedWriteOnly())
3813       Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
3814     assert(Attrs.size() <= 1);
3815   }
3816 
3817   /// See AbstractAttribute::manifest(...).
3818   ChangeStatus manifest(Attributor &A) override {
3819     IRPosition &IRP = getIRPosition();
3820 
3821     // Check if we would improve the existing attributes first.
3822     SmallVector<Attribute, 4> DeducedAttrs;
3823     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
3824     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
3825           return IRP.hasAttr(Attr.getKindAsEnum(),
3826                              /* IgnoreSubsumingPositions */ true);
3827         }))
3828       return ChangeStatus::UNCHANGED;
3829 
3830     // Clear existing attributes.
3831     IRP.removeAttrs(AttrKinds);
3832 
3833     // Use the generic manifest method.
3834     return IRAttribute::manifest(A);
3835   }
3836 
3837   /// See AbstractState::getAsStr().
3838   const std::string getAsStr() const override {
3839     if (isAssumedReadNone())
3840       return "readnone";
3841     if (isAssumedReadOnly())
3842       return "readonly";
3843     if (isAssumedWriteOnly())
3844       return "writeonly";
3845     return "may-read/write";
3846   }
3847 
3848   /// The set of IR attributes AAMemoryBehavior deals with.
3849   static const Attribute::AttrKind AttrKinds[3];
3850 };
3851 
3852 const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
3853     Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
3854 
3855 /// Memory behavior attribute for a floating value.
3856 struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
3857   AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
3858 
3859   /// See AbstractAttribute::initialize(...).
3860   void initialize(Attributor &A) override {
3861     AAMemoryBehaviorImpl::initialize(A);
3862     // Initialize the use vector with all direct uses of the associated value.
3863     for (const Use &U : getAssociatedValue().uses())
3864       Uses.insert(&U);
3865   }
3866 
3867   /// See AbstractAttribute::updateImpl(...).
3868   ChangeStatus updateImpl(Attributor &A) override;
3869 
3870   /// See AbstractAttribute::trackStatistics()
3871   void trackStatistics() const override {
3872     if (isAssumedReadNone())
3873       STATS_DECLTRACK_FLOATING_ATTR(readnone)
3874     else if (isAssumedReadOnly())
3875       STATS_DECLTRACK_FLOATING_ATTR(readonly)
3876     else if (isAssumedWriteOnly())
3877       STATS_DECLTRACK_FLOATING_ATTR(writeonly)
3878   }
3879 
3880 private:
3881   /// Return true if users of \p UserI might access the underlying
3882   /// variable/location described by \p U and should therefore be analyzed.
3883   bool followUsersOfUseIn(Attributor &A, const Use *U,
3884                           const Instruction *UserI);
3885 
3886   /// Update the state according to the effect of use \p U in \p UserI.
3887   void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI);
3888 
3889 protected:
3890   /// Container for (transitive) uses of the associated argument.
3891   SetVector<const Use *> Uses;
3892 };
3893 
3894 /// Memory behavior attribute for function argument.
3895 struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
3896   AAMemoryBehaviorArgument(const IRPosition &IRP)
3897       : AAMemoryBehaviorFloating(IRP) {}
3898 
3899   /// See AbstractAttribute::initialize(...).
3900   void initialize(Attributor &A) override {
3901     AAMemoryBehaviorFloating::initialize(A);
3902 
3903     // Initialize the use vector with all direct uses of the associated value.
3904     Argument *Arg = getAssociatedArgument();
3905     if (!Arg || !Arg->getParent()->hasExactDefinition())
3906       indicatePessimisticFixpoint();
3907   }
3908 
3909   ChangeStatus manifest(Attributor &A) override {
3910     // TODO: From readattrs.ll: "inalloca parameters are always
3911     //                           considered written"
3912     if (hasAttr({Attribute::InAlloca})) {
3913       removeKnownBits(NO_WRITES);
3914       removeAssumedBits(NO_WRITES);
3915     }
3916     return AAMemoryBehaviorFloating::manifest(A);
3917   }
3918 
3919 
3920   /// See AbstractAttribute::trackStatistics()
3921   void trackStatistics() const override {
3922     if (isAssumedReadNone())
3923       STATS_DECLTRACK_ARG_ATTR(readnone)
3924     else if (isAssumedReadOnly())
3925       STATS_DECLTRACK_ARG_ATTR(readonly)
3926     else if (isAssumedWriteOnly())
3927       STATS_DECLTRACK_ARG_ATTR(writeonly)
3928   }
3929 };
3930 
3931 struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
3932   AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP)
3933       : AAMemoryBehaviorArgument(IRP) {}
3934 
3935   /// See AbstractAttribute::updateImpl(...).
3936   ChangeStatus updateImpl(Attributor &A) override {
3937     // TODO: Once we have call site specific value information we can provide
3938     //       call site specific liveness liveness information and then it makes
3939     //       sense to specialize attributes for call sites arguments instead of
3940     //       redirecting requests to the callee argument.
3941     Argument *Arg = getAssociatedArgument();
3942     const IRPosition &ArgPos = IRPosition::argument(*Arg);
3943     auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos);
3944     return clampStateAndIndicateChange(
3945         getState(),
3946         static_cast<const AANoCapture::StateType &>(ArgAA.getState()));
3947   }
3948 
3949   /// See AbstractAttribute::trackStatistics()
3950   void trackStatistics() const override {
3951     if (isAssumedReadNone())
3952       STATS_DECLTRACK_CSARG_ATTR(readnone)
3953     else if (isAssumedReadOnly())
3954       STATS_DECLTRACK_CSARG_ATTR(readonly)
3955     else if (isAssumedWriteOnly())
3956       STATS_DECLTRACK_CSARG_ATTR(writeonly)
3957   }
3958 };
3959 
3960 /// Memory behavior attribute for a call site return position.
3961 struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
3962   AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP)
3963       : AAMemoryBehaviorFloating(IRP) {}
3964 
3965   /// See AbstractAttribute::manifest(...).
3966   ChangeStatus manifest(Attributor &A) override {
3967     // We do not annotate returned values.
3968     return ChangeStatus::UNCHANGED;
3969   }
3970 
3971   /// See AbstractAttribute::trackStatistics()
3972   void trackStatistics() const override {}
3973 };
3974 
3975 /// An AA to represent the memory behavior function attributes.
3976 struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
3977   AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
3978 
3979   /// See AbstractAttribute::updateImpl(Attributor &A).
3980   virtual ChangeStatus updateImpl(Attributor &A) override;
3981 
3982   /// See AbstractAttribute::manifest(...).
3983   ChangeStatus manifest(Attributor &A) override {
3984     Function &F = cast<Function>(getAnchorValue());
3985     if (isAssumedReadNone()) {
3986       F.removeFnAttr(Attribute::ArgMemOnly);
3987       F.removeFnAttr(Attribute::InaccessibleMemOnly);
3988       F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
3989     }
3990     return AAMemoryBehaviorImpl::manifest(A);
3991   }
3992 
3993   /// See AbstractAttribute::trackStatistics()
3994   void trackStatistics() const override {
3995     if (isAssumedReadNone())
3996       STATS_DECLTRACK_FN_ATTR(readnone)
3997     else if (isAssumedReadOnly())
3998       STATS_DECLTRACK_FN_ATTR(readonly)
3999     else if (isAssumedWriteOnly())
4000       STATS_DECLTRACK_FN_ATTR(writeonly)
4001   }
4002 };
4003 
4004 /// AAMemoryBehavior attribute for call sites.
4005 struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
4006   AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
4007 
4008   /// See AbstractAttribute::initialize(...).
4009   void initialize(Attributor &A) override {
4010     AAMemoryBehaviorImpl::initialize(A);
4011     Function *F = getAssociatedFunction();
4012     if (!F || !F->hasExactDefinition())
4013       indicatePessimisticFixpoint();
4014   }
4015 
4016   /// See AbstractAttribute::updateImpl(...).
4017   ChangeStatus updateImpl(Attributor &A) override {
4018     // TODO: Once we have call site specific value information we can provide
4019     //       call site specific liveness liveness information and then it makes
4020     //       sense to specialize attributes for call sites arguments instead of
4021     //       redirecting requests to the callee argument.
4022     Function *F = getAssociatedFunction();
4023     const IRPosition &FnPos = IRPosition::function(*F);
4024     auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos);
4025     return clampStateAndIndicateChange(
4026         getState(),
4027         static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState()));
4028   }
4029 
4030   /// See AbstractAttribute::trackStatistics()
4031   void trackStatistics() const override {
4032     if (isAssumedReadNone())
4033       STATS_DECLTRACK_CS_ATTR(readnone)
4034     else if (isAssumedReadOnly())
4035       STATS_DECLTRACK_CS_ATTR(readonly)
4036     else if (isAssumedWriteOnly())
4037       STATS_DECLTRACK_CS_ATTR(writeonly)
4038   }
4039 };
4040 } // namespace
4041 
4042 ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
4043 
4044   // The current assumed state used to determine a change.
4045   auto AssumedState = getAssumed();
4046 
4047   auto CheckRWInst = [&](Instruction &I) {
4048     // If the instruction has an own memory behavior state, use it to restrict
4049     // the local state. No further analysis is required as the other memory
4050     // state is as optimistic as it gets.
4051     if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
4052       const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
4053           *this, IRPosition::callsite_function(ICS));
4054       intersectAssumedBits(MemBehaviorAA.getAssumed());
4055       return !isAtFixpoint();
4056     }
4057 
4058     // Remove access kind modifiers if necessary.
4059     if (I.mayReadFromMemory())
4060       removeAssumedBits(NO_READS);
4061     if (I.mayWriteToMemory())
4062       removeAssumedBits(NO_WRITES);
4063     return !isAtFixpoint();
4064   };
4065 
4066   if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this))
4067     return indicatePessimisticFixpoint();
4068 
4069   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
4070                                         : ChangeStatus::UNCHANGED;
4071 }
4072 
4073 ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
4074 
4075   const IRPosition &IRP = getIRPosition();
4076   const IRPosition &FnPos = IRPosition::function_scope(IRP);
4077   AAMemoryBehavior::StateType &S = getState();
4078 
4079   // First, check the function scope. We take the known information and we avoid
4080   // work if the assumed information implies the current assumed information for
4081   // this attribute.
4082   const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos);
4083   S.addKnownBits(FnMemAA.getKnown());
4084   if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
4085     return ChangeStatus::UNCHANGED;
4086 
4087   // Make sure the value is not captured (except through "return"), if
4088   // it is, any information derived would be irrelevant anyway as we cannot
4089   // check the potential aliases introduced by the capture. However, no need
4090   // to fall back to anythign less optimistic than the function state.
4091   const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP);
4092   if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
4093     S.intersectAssumedBits(FnMemAA.getAssumed());
4094     return ChangeStatus::CHANGED;
4095   }
4096 
4097   // The current assumed state used to determine a change.
4098   auto AssumedState = S.getAssumed();
4099 
4100   // Liveness information to exclude dead users.
4101   // TODO: Take the FnPos once we have call site specific liveness information.
4102   const auto &LivenessAA = A.getAAFor<AAIsDead>(
4103       *this, IRPosition::function(*IRP.getAssociatedFunction()));
4104 
4105   // Visit and expand uses until all are analyzed or a fixpoint is reached.
4106   for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) {
4107     const Use *U = Uses[i];
4108     Instruction *UserI = cast<Instruction>(U->getUser());
4109     LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI
4110                       << " [Dead: " << (LivenessAA.isAssumedDead(UserI))
4111                       << "]\n");
4112     if (LivenessAA.isAssumedDead(UserI))
4113       continue;
4114 
4115     // Check if the users of UserI should also be visited.
4116     if (followUsersOfUseIn(A, U, UserI))
4117       for (const Use &UserIUse : UserI->uses())
4118         Uses.insert(&UserIUse);
4119 
4120     // If UserI might touch memory we analyze the use in detail.
4121     if (UserI->mayReadOrWriteMemory())
4122       analyzeUseIn(A, U, UserI);
4123   }
4124 
4125   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
4126                                         : ChangeStatus::UNCHANGED;
4127 }
4128 
4129 bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U,
4130                                                   const Instruction *UserI) {
4131   // The loaded value is unrelated to the pointer argument, no need to
4132   // follow the users of the load.
4133   if (isa<LoadInst>(UserI))
4134     return false;
4135 
4136   // By default we follow all uses assuming UserI might leak information on U,
4137   // we have special handling for call sites operands though.
4138   ImmutableCallSite ICS(UserI);
4139   if (!ICS || !ICS.isArgOperand(U))
4140     return true;
4141 
4142   // If the use is a call argument known not to be captured, the users of
4143   // the call do not need to be visited because they have to be unrelated to
4144   // the input. Note that this check is not trivial even though we disallow
4145   // general capturing of the underlying argument. The reason is that the
4146   // call might the argument "through return", which we allow and for which we
4147   // need to check call users.
4148   unsigned ArgNo = ICS.getArgumentNo(U);
4149   const auto &ArgNoCaptureAA =
4150       A.getAAFor<AANoCapture>(*this, IRPosition::callsite_argument(ICS, ArgNo));
4151   return !ArgNoCaptureAA.isAssumedNoCapture();
4152 }
4153 
4154 void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U,
4155                                             const Instruction *UserI) {
4156   assert(UserI->mayReadOrWriteMemory());
4157 
4158   switch (UserI->getOpcode()) {
4159   default:
4160     // TODO: Handle all atomics and other side-effect operations we know of.
4161     break;
4162   case Instruction::Load:
4163     // Loads cause the NO_READS property to disappear.
4164     removeAssumedBits(NO_READS);
4165     return;
4166 
4167   case Instruction::Store:
4168     // Stores cause the NO_WRITES property to disappear if the use is the
4169     // pointer operand. Note that we do assume that capturing was taken care of
4170     // somewhere else.
4171     if (cast<StoreInst>(UserI)->getPointerOperand() == U->get())
4172       removeAssumedBits(NO_WRITES);
4173     return;
4174 
4175   case Instruction::Call:
4176   case Instruction::CallBr:
4177   case Instruction::Invoke: {
4178     // For call sites we look at the argument memory behavior attribute (this
4179     // could be recursive!) in order to restrict our own state.
4180     ImmutableCallSite ICS(UserI);
4181 
4182     // Give up on operand bundles.
4183     if (ICS.isBundleOperand(U)) {
4184       indicatePessimisticFixpoint();
4185       return;
4186     }
4187 
4188     // Calling a function does read the function pointer, maybe write it if the
4189     // function is self-modifying.
4190     if (ICS.isCallee(U)) {
4191       removeAssumedBits(NO_READS);
4192       break;
4193     }
4194 
4195     // Adjust the possible access behavior based on the information on the
4196     // argument.
4197     unsigned ArgNo = ICS.getArgumentNo(U);
4198     const IRPosition &ArgPos = IRPosition::callsite_argument(ICS, ArgNo);
4199     const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos);
4200     // "assumed" has at most the same bits as the MemBehaviorAA assumed
4201     // and at least "known".
4202     intersectAssumedBits(MemBehaviorAA.getAssumed());
4203     return;
4204   }
4205   };
4206 
4207   // Generally, look at the "may-properties" and adjust the assumed state if we
4208   // did not trigger special handling before.
4209   if (UserI->mayReadFromMemory())
4210     removeAssumedBits(NO_READS);
4211   if (UserI->mayWriteToMemory())
4212     removeAssumedBits(NO_WRITES);
4213 }
4214 
4215 /// ----------------------------------------------------------------------------
4216 ///                               Attributor
4217 /// ----------------------------------------------------------------------------
4218 
4219 bool Attributor::isAssumedDead(const AbstractAttribute &AA,
4220                                const AAIsDead *LivenessAA) {
4221   const Instruction *CtxI = AA.getIRPosition().getCtxI();
4222   if (!CtxI)
4223     return false;
4224 
4225   if (!LivenessAA)
4226     LivenessAA =
4227         &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()),
4228                             /* TrackDependence */ false);
4229 
4230   // Don't check liveness for AAIsDead.
4231   if (&AA == LivenessAA)
4232     return false;
4233 
4234   if (!LivenessAA->isAssumedDead(CtxI))
4235     return false;
4236 
4237   // We actually used liveness information so we have to record a dependence.
4238   recordDependence(*LivenessAA, AA);
4239 
4240   return true;
4241 }
4242 
4243 bool Attributor::checkForAllCallSites(
4244     const function_ref<bool(AbstractCallSite)> &Pred,
4245     const AbstractAttribute &QueryingAA, bool RequireAllCallSites) {
4246   // We can try to determine information from
4247   // the call sites. However, this is only possible all call sites are known,
4248   // hence the function has internal linkage.
4249   const IRPosition &IRP = QueryingAA.getIRPosition();
4250   const Function *AssociatedFunction = IRP.getAssociatedFunction();
4251   if (!AssociatedFunction) {
4252     LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
4253                       << "\n");
4254     return false;
4255   }
4256 
4257   return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
4258                               &QueryingAA);
4259 }
4260 
4261 bool Attributor::checkForAllCallSites(
4262     const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn,
4263     bool RequireAllCallSites, const AbstractAttribute *QueryingAA) {
4264   if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
4265     LLVM_DEBUG(
4266         dbgs()
4267         << "[Attributor] Function " << Fn.getName()
4268         << " has no internal linkage, hence not all call sites are known\n");
4269     return false;
4270   }
4271 
4272   for (const Use &U : Fn.uses()) {
4273     AbstractCallSite ACS(&U);
4274     if (!ACS) {
4275       LLVM_DEBUG(dbgs() << "[Attributor] Function "
4276                         << Fn.getName()
4277                         << " has non call site use " << *U.get() << " in "
4278                         << *U.getUser() << "\n");
4279       return false;
4280     }
4281 
4282     Instruction *I = ACS.getInstruction();
4283     Function *Caller = I->getFunction();
4284 
4285     const auto *LivenessAA =
4286         lookupAAFor<AAIsDead>(IRPosition::function(*Caller), QueryingAA,
4287                            /* TrackDependence */ false);
4288 
4289     // Skip dead calls.
4290     if (LivenessAA && LivenessAA->isAssumedDead(I)) {
4291       // We actually used liveness information so we have to record a
4292       // dependence.
4293       if (QueryingAA)
4294         recordDependence(*LivenessAA, *QueryingAA);
4295       continue;
4296     }
4297 
4298     const Use *EffectiveUse =
4299         ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
4300     if (!ACS.isCallee(EffectiveUse)) {
4301       if (!RequireAllCallSites)
4302         continue;
4303       LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser()
4304                         << " is an invalid use of "
4305                         << Fn.getName() << "\n");
4306       return false;
4307     }
4308 
4309     if (Pred(ACS))
4310       continue;
4311 
4312     LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
4313                       << *ACS.getInstruction() << "\n");
4314     return false;
4315   }
4316 
4317   return true;
4318 }
4319 
4320 bool Attributor::checkForAllReturnedValuesAndReturnInsts(
4321     const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
4322         &Pred,
4323     const AbstractAttribute &QueryingAA) {
4324 
4325   const IRPosition &IRP = QueryingAA.getIRPosition();
4326   // Since we need to provide return instructions we have to have an exact
4327   // definition.
4328   const Function *AssociatedFunction = IRP.getAssociatedFunction();
4329   if (!AssociatedFunction)
4330     return false;
4331 
4332   // If this is a call site query we use the call site specific return values
4333   // and liveness information.
4334   // TODO: use the function scope once we have call site AAReturnedValues.
4335   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
4336   const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
4337   if (!AARetVal.getState().isValidState())
4338     return false;
4339 
4340   return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
4341 }
4342 
4343 bool Attributor::checkForAllReturnedValues(
4344     const function_ref<bool(Value &)> &Pred,
4345     const AbstractAttribute &QueryingAA) {
4346 
4347   const IRPosition &IRP = QueryingAA.getIRPosition();
4348   const Function *AssociatedFunction = IRP.getAssociatedFunction();
4349   if (!AssociatedFunction)
4350     return false;
4351 
4352   // TODO: use the function scope once we have call site AAReturnedValues.
4353   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
4354   const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
4355   if (!AARetVal.getState().isValidState())
4356     return false;
4357 
4358   return AARetVal.checkForAllReturnedValuesAndReturnInsts(
4359       [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
4360         return Pred(RV);
4361       });
4362 }
4363 
4364 static bool
4365 checkForAllInstructionsImpl(InformationCache::OpcodeInstMapTy &OpcodeInstMap,
4366                             const function_ref<bool(Instruction &)> &Pred,
4367                             const AAIsDead *LivenessAA, bool &AnyDead,
4368                             const ArrayRef<unsigned> &Opcodes) {
4369   for (unsigned Opcode : Opcodes) {
4370     for (Instruction *I : OpcodeInstMap[Opcode]) {
4371       // Skip dead instructions.
4372       if (LivenessAA && LivenessAA->isAssumedDead(I)) {
4373         AnyDead = true;
4374         continue;
4375       }
4376 
4377       if (!Pred(*I))
4378         return false;
4379     }
4380   }
4381   return true;
4382 }
4383 
4384 bool Attributor::checkForAllInstructions(
4385     const llvm::function_ref<bool(Instruction &)> &Pred,
4386     const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) {
4387 
4388   const IRPosition &IRP = QueryingAA.getIRPosition();
4389   // Since we need to provide instructions we have to have an exact definition.
4390   const Function *AssociatedFunction = IRP.getAssociatedFunction();
4391   if (!AssociatedFunction)
4392     return false;
4393 
4394   // TODO: use the function scope once we have call site AAReturnedValues.
4395   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
4396   const auto &LivenessAA =
4397       getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
4398   bool AnyDead = false;
4399 
4400   auto &OpcodeInstMap =
4401       InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
4402   if (!checkForAllInstructionsImpl(OpcodeInstMap, Pred, &LivenessAA, AnyDead,
4403                                    Opcodes))
4404     return false;
4405 
4406   // If we actually used liveness information so we have to record a dependence.
4407   if (AnyDead)
4408     recordDependence(LivenessAA, QueryingAA);
4409 
4410   return true;
4411 }
4412 
4413 bool Attributor::checkForAllReadWriteInstructions(
4414     const llvm::function_ref<bool(Instruction &)> &Pred,
4415     AbstractAttribute &QueryingAA) {
4416 
4417   const Function *AssociatedFunction =
4418       QueryingAA.getIRPosition().getAssociatedFunction();
4419   if (!AssociatedFunction)
4420     return false;
4421 
4422   // TODO: use the function scope once we have call site AAReturnedValues.
4423   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
4424   const auto &LivenessAA =
4425       getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
4426   bool AnyDead = false;
4427 
4428   for (Instruction *I :
4429        InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
4430     // Skip dead instructions.
4431     if (LivenessAA.isAssumedDead(I)) {
4432       AnyDead = true;
4433       continue;
4434     }
4435 
4436     if (!Pred(*I))
4437       return false;
4438   }
4439 
4440   // If we actually used liveness information so we have to record a dependence.
4441   if (AnyDead)
4442     recordDependence(LivenessAA, QueryingAA);
4443 
4444   return true;
4445 }
4446 
4447 ChangeStatus Attributor::run(Module &M) {
4448   LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
4449                     << AllAbstractAttributes.size()
4450                     << " abstract attributes.\n");
4451 
4452   // Now that all abstract attributes are collected and initialized we start
4453   // the abstract analysis.
4454 
4455   unsigned IterationCounter = 1;
4456 
4457   SmallVector<AbstractAttribute *, 64> ChangedAAs;
4458   SetVector<AbstractAttribute *> Worklist;
4459   Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end());
4460 
4461   bool RecomputeDependences = false;
4462 
4463   do {
4464     // Remember the size to determine new attributes.
4465     size_t NumAAs = AllAbstractAttributes.size();
4466     LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
4467                       << ", Worklist size: " << Worklist.size() << "\n");
4468 
4469     // If dependences (=QueryMap) are recomputed we have to look at all abstract
4470     // attributes again, regardless of what changed in the last iteration.
4471     if (RecomputeDependences) {
4472       LLVM_DEBUG(
4473           dbgs() << "[Attributor] Run all AAs to recompute dependences\n");
4474       QueryMap.clear();
4475       ChangedAAs.clear();
4476       Worklist.insert(AllAbstractAttributes.begin(),
4477                       AllAbstractAttributes.end());
4478     }
4479 
4480     // Add all abstract attributes that are potentially dependent on one that
4481     // changed to the work list.
4482     for (AbstractAttribute *ChangedAA : ChangedAAs) {
4483       auto &QuerriedAAs = QueryMap[ChangedAA];
4484       Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end());
4485     }
4486 
4487     LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
4488                       << ", Worklist+Dependent size: " << Worklist.size()
4489                       << "\n");
4490 
4491     // Reset the changed set.
4492     ChangedAAs.clear();
4493 
4494     // Update all abstract attribute in the work list and record the ones that
4495     // changed.
4496     for (AbstractAttribute *AA : Worklist)
4497       if (!isAssumedDead(*AA, nullptr))
4498         if (AA->update(*this) == ChangeStatus::CHANGED)
4499           ChangedAAs.push_back(AA);
4500 
4501     // Check if we recompute the dependences in the next iteration.
4502     RecomputeDependences = (DepRecomputeInterval > 0 &&
4503                             IterationCounter % DepRecomputeInterval == 0);
4504 
4505     // Add attributes to the changed set if they have been created in the last
4506     // iteration.
4507     ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs,
4508                       AllAbstractAttributes.end());
4509 
4510     // Reset the work list and repopulate with the changed abstract attributes.
4511     // Note that dependent ones are added above.
4512     Worklist.clear();
4513     Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
4514 
4515   } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations ||
4516                                  VerifyMaxFixpointIterations));
4517 
4518   LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
4519                     << IterationCounter << "/" << MaxFixpointIterations
4520                     << " iterations\n");
4521 
4522   size_t NumFinalAAs = AllAbstractAttributes.size();
4523 
4524   // Reset abstract arguments not settled in a sound fixpoint by now. This
4525   // happens when we stopped the fixpoint iteration early. Note that only the
4526   // ones marked as "changed" *and* the ones transitively depending on them
4527   // need to be reverted to a pessimistic state. Others might not be in a
4528   // fixpoint state but we can use the optimistic results for them anyway.
4529   SmallPtrSet<AbstractAttribute *, 32> Visited;
4530   for (unsigned u = 0; u < ChangedAAs.size(); u++) {
4531     AbstractAttribute *ChangedAA = ChangedAAs[u];
4532     if (!Visited.insert(ChangedAA).second)
4533       continue;
4534 
4535     AbstractState &State = ChangedAA->getState();
4536     if (!State.isAtFixpoint()) {
4537       State.indicatePessimisticFixpoint();
4538 
4539       NumAttributesTimedOut++;
4540     }
4541 
4542     auto &QuerriedAAs = QueryMap[ChangedAA];
4543     ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end());
4544   }
4545 
4546   LLVM_DEBUG({
4547     if (!Visited.empty())
4548       dbgs() << "\n[Attributor] Finalized " << Visited.size()
4549              << " abstract attributes.\n";
4550   });
4551 
4552   unsigned NumManifested = 0;
4553   unsigned NumAtFixpoint = 0;
4554   ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
4555   for (AbstractAttribute *AA : AllAbstractAttributes) {
4556     AbstractState &State = AA->getState();
4557 
4558     // If there is not already a fixpoint reached, we can now take the
4559     // optimistic state. This is correct because we enforced a pessimistic one
4560     // on abstract attributes that were transitively dependent on a changed one
4561     // already above.
4562     if (!State.isAtFixpoint())
4563       State.indicateOptimisticFixpoint();
4564 
4565     // If the state is invalid, we do not try to manifest it.
4566     if (!State.isValidState())
4567       continue;
4568 
4569     // Skip dead code.
4570     if (isAssumedDead(*AA, nullptr))
4571       continue;
4572     // Manifest the state and record if we changed the IR.
4573     ChangeStatus LocalChange = AA->manifest(*this);
4574     if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
4575       AA->trackStatistics();
4576 
4577     ManifestChange = ManifestChange | LocalChange;
4578 
4579     NumAtFixpoint++;
4580     NumManifested += (LocalChange == ChangeStatus::CHANGED);
4581   }
4582 
4583   (void)NumManifested;
4584   (void)NumAtFixpoint;
4585   LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
4586                     << " arguments while " << NumAtFixpoint
4587                     << " were in a valid fixpoint state\n");
4588 
4589   NumAttributesManifested += NumManifested;
4590   NumAttributesValidFixpoint += NumAtFixpoint;
4591 
4592   (void)NumFinalAAs;
4593   assert(
4594       NumFinalAAs == AllAbstractAttributes.size() &&
4595       "Expected the final number of abstract attributes to remain unchanged!");
4596 
4597   // Delete stuff at the end to avoid invalid references and a nice order.
4598   {
4599     LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least "
4600                       << ToBeDeletedFunctions.size() << " functions and "
4601                       << ToBeDeletedBlocks.size() << " blocks and "
4602                       << ToBeDeletedInsts.size() << " instructions\n");
4603     for (Instruction *I : ToBeDeletedInsts) {
4604       if (!I->use_empty())
4605         I->replaceAllUsesWith(UndefValue::get(I->getType()));
4606       I->eraseFromParent();
4607     }
4608 
4609     if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
4610       SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
4611       ToBeDeletedBBs.reserve(NumDeadBlocks);
4612       ToBeDeletedBBs.append(ToBeDeletedBlocks.begin(), ToBeDeletedBlocks.end());
4613       DeleteDeadBlocks(ToBeDeletedBBs);
4614       STATS_DECLTRACK(AAIsDead, BasicBlock,
4615                       "Number of dead basic blocks deleted.");
4616     }
4617 
4618     STATS_DECL(AAIsDead, Function, "Number of dead functions deleted.");
4619     for (Function *Fn : ToBeDeletedFunctions) {
4620       Fn->replaceAllUsesWith(UndefValue::get(Fn->getType()));
4621       Fn->eraseFromParent();
4622       STATS_TRACK(AAIsDead, Function);
4623     }
4624 
4625     // Identify dead internal functions and delete them. This happens outside
4626     // the other fixpoint analysis as we might treat potentially dead functions
4627     // as live to lower the number of iterations. If they happen to be dead, the
4628     // below fixpoint loop will identify and eliminate them.
4629     SmallVector<Function *, 8> InternalFns;
4630     for (Function &F : M)
4631       if (F.hasLocalLinkage())
4632         InternalFns.push_back(&F);
4633 
4634     bool FoundDeadFn = true;
4635     while (FoundDeadFn) {
4636       FoundDeadFn = false;
4637       for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
4638         Function *F = InternalFns[u];
4639         if (!F)
4640           continue;
4641 
4642         const auto *LivenessAA =
4643             lookupAAFor<AAIsDead>(IRPosition::function(*F));
4644         if (LivenessAA &&
4645             !checkForAllCallSites([](AbstractCallSite ACS) { return false; },
4646                                   *LivenessAA, true))
4647           continue;
4648 
4649         STATS_TRACK(AAIsDead, Function);
4650         F->replaceAllUsesWith(UndefValue::get(F->getType()));
4651         F->eraseFromParent();
4652         InternalFns[u] = nullptr;
4653         FoundDeadFn = true;
4654       }
4655     }
4656   }
4657 
4658   if (VerifyMaxFixpointIterations &&
4659       IterationCounter != MaxFixpointIterations) {
4660     errs() << "\n[Attributor] Fixpoint iteration done after: "
4661            << IterationCounter << "/" << MaxFixpointIterations
4662            << " iterations\n";
4663     llvm_unreachable("The fixpoint was not reached with exactly the number of "
4664                      "specified iterations!");
4665   }
4666 
4667   return ManifestChange;
4668 }
4669 
4670 void Attributor::initializeInformationCache(Function &F) {
4671 
4672   // Walk all instructions to find interesting instructions that might be
4673   // queried by abstract attributes during their initialization or update.
4674   // This has to happen before we create attributes.
4675   auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F];
4676   auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F];
4677 
4678   for (Instruction &I : instructions(&F)) {
4679     bool IsInterestingOpcode = false;
4680 
4681     // To allow easy access to all instructions in a function with a given
4682     // opcode we store them in the InfoCache. As not all opcodes are interesting
4683     // to concrete attributes we only cache the ones that are as identified in
4684     // the following switch.
4685     // Note: There are no concrete attributes now so this is initially empty.
4686     switch (I.getOpcode()) {
4687     default:
4688       assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
4689              "New call site/base instruction type needs to be known int the "
4690              "Attributor.");
4691       break;
4692     case Instruction::Load:
4693       // The alignment of a pointer is interesting for loads.
4694     case Instruction::Store:
4695       // The alignment of a pointer is interesting for stores.
4696     case Instruction::Call:
4697     case Instruction::CallBr:
4698     case Instruction::Invoke:
4699     case Instruction::CleanupRet:
4700     case Instruction::CatchSwitch:
4701     case Instruction::Resume:
4702     case Instruction::Ret:
4703       IsInterestingOpcode = true;
4704     }
4705     if (IsInterestingOpcode)
4706       InstOpcodeMap[I.getOpcode()].push_back(&I);
4707     if (I.mayReadOrWriteMemory())
4708       ReadOrWriteInsts.push_back(&I);
4709   }
4710 }
4711 
4712 void Attributor::identifyDefaultAbstractAttributes(Function &F) {
4713   if (!VisitedFunctions.insert(&F).second)
4714     return;
4715 
4716   IRPosition FPos = IRPosition::function(F);
4717 
4718   // Check for dead BasicBlocks in every function.
4719   // We need dead instruction detection because we do not want to deal with
4720   // broken IR in which SSA rules do not apply.
4721   getOrCreateAAFor<AAIsDead>(FPos);
4722 
4723   // Every function might be "will-return".
4724   getOrCreateAAFor<AAWillReturn>(FPos);
4725 
4726   // Every function can be nounwind.
4727   getOrCreateAAFor<AANoUnwind>(FPos);
4728 
4729   // Every function might be marked "nosync"
4730   getOrCreateAAFor<AANoSync>(FPos);
4731 
4732   // Every function might be "no-free".
4733   getOrCreateAAFor<AANoFree>(FPos);
4734 
4735   // Every function might be "no-return".
4736   getOrCreateAAFor<AANoReturn>(FPos);
4737 
4738   // Every function might be "no-recurse".
4739   getOrCreateAAFor<AANoRecurse>(FPos);
4740 
4741   // Every function might be "readnone/readonly/writeonly/...".
4742   getOrCreateAAFor<AAMemoryBehavior>(FPos);
4743 
4744   // Every function might be applicable for Heap-To-Stack conversion.
4745   if (EnableHeapToStack)
4746     getOrCreateAAFor<AAHeapToStack>(FPos);
4747 
4748   // Return attributes are only appropriate if the return type is non void.
4749   Type *ReturnType = F.getReturnType();
4750   if (!ReturnType->isVoidTy()) {
4751     // Argument attribute "returned" --- Create only one per function even
4752     // though it is an argument attribute.
4753     getOrCreateAAFor<AAReturnedValues>(FPos);
4754 
4755     IRPosition RetPos = IRPosition::returned(F);
4756 
4757     // Every function might be simplified.
4758     getOrCreateAAFor<AAValueSimplify>(RetPos);
4759 
4760     if (ReturnType->isPointerTy()) {
4761 
4762       // Every function with pointer return type might be marked align.
4763       getOrCreateAAFor<AAAlign>(RetPos);
4764 
4765       // Every function with pointer return type might be marked nonnull.
4766       getOrCreateAAFor<AANonNull>(RetPos);
4767 
4768       // Every function with pointer return type might be marked noalias.
4769       getOrCreateAAFor<AANoAlias>(RetPos);
4770 
4771       // Every function with pointer return type might be marked
4772       // dereferenceable.
4773       getOrCreateAAFor<AADereferenceable>(RetPos);
4774     }
4775   }
4776 
4777   for (Argument &Arg : F.args()) {
4778     IRPosition ArgPos = IRPosition::argument(Arg);
4779 
4780     // Every argument might be simplified.
4781     getOrCreateAAFor<AAValueSimplify>(ArgPos);
4782 
4783     if (Arg.getType()->isPointerTy()) {
4784       // Every argument with pointer type might be marked nonnull.
4785       getOrCreateAAFor<AANonNull>(ArgPos);
4786 
4787       // Every argument with pointer type might be marked noalias.
4788       getOrCreateAAFor<AANoAlias>(ArgPos);
4789 
4790       // Every argument with pointer type might be marked dereferenceable.
4791       getOrCreateAAFor<AADereferenceable>(ArgPos);
4792 
4793       // Every argument with pointer type might be marked align.
4794       getOrCreateAAFor<AAAlign>(ArgPos);
4795 
4796       // Every argument with pointer type might be marked nocapture.
4797       getOrCreateAAFor<AANoCapture>(ArgPos);
4798 
4799       // Every argument with pointer type might be marked
4800       // "readnone/readonly/writeonly/..."
4801       getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
4802     }
4803   }
4804 
4805   auto CallSitePred = [&](Instruction &I) -> bool {
4806     CallSite CS(&I);
4807     if (CS.getCalledFunction()) {
4808       for (int i = 0, e = CS.getCalledFunction()->arg_size(); i < e; i++) {
4809 
4810         IRPosition CSArgPos = IRPosition::callsite_argument(CS, i);
4811 
4812         // Call site argument might be simplified.
4813         getOrCreateAAFor<AAValueSimplify>(CSArgPos);
4814 
4815         if (!CS.getArgument(i)->getType()->isPointerTy())
4816           continue;
4817 
4818         // Call site argument attribute "non-null".
4819         getOrCreateAAFor<AANonNull>(CSArgPos);
4820 
4821         // Call site argument attribute "no-alias".
4822         getOrCreateAAFor<AANoAlias>(CSArgPos);
4823 
4824         // Call site argument attribute "dereferenceable".
4825         getOrCreateAAFor<AADereferenceable>(CSArgPos);
4826 
4827         // Call site argument attribute "align".
4828         getOrCreateAAFor<AAAlign>(CSArgPos);
4829       }
4830     }
4831     return true;
4832   };
4833 
4834   auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
4835   bool Success, AnyDead = false;
4836   Success = checkForAllInstructionsImpl(
4837       OpcodeInstMap, CallSitePred, nullptr, AnyDead,
4838       {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
4839        (unsigned)Instruction::Call});
4840   (void)Success;
4841   assert(Success && !AnyDead && "Expected the check call to be successful!");
4842 
4843   auto LoadStorePred = [&](Instruction &I) -> bool {
4844     if (isa<LoadInst>(I))
4845       getOrCreateAAFor<AAAlign>(
4846           IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
4847     else
4848       getOrCreateAAFor<AAAlign>(
4849           IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
4850     return true;
4851   };
4852   Success = checkForAllInstructionsImpl(
4853       OpcodeInstMap, LoadStorePred, nullptr, AnyDead,
4854       {(unsigned)Instruction::Load, (unsigned)Instruction::Store});
4855   (void)Success;
4856   assert(Success && !AnyDead && "Expected the check call to be successful!");
4857 }
4858 
4859 /// Helpers to ease debugging through output streams and print calls.
4860 ///
4861 ///{
4862 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
4863   return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
4864 }
4865 
4866 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
4867   switch (AP) {
4868   case IRPosition::IRP_INVALID:
4869     return OS << "inv";
4870   case IRPosition::IRP_FLOAT:
4871     return OS << "flt";
4872   case IRPosition::IRP_RETURNED:
4873     return OS << "fn_ret";
4874   case IRPosition::IRP_CALL_SITE_RETURNED:
4875     return OS << "cs_ret";
4876   case IRPosition::IRP_FUNCTION:
4877     return OS << "fn";
4878   case IRPosition::IRP_CALL_SITE:
4879     return OS << "cs";
4880   case IRPosition::IRP_ARGUMENT:
4881     return OS << "arg";
4882   case IRPosition::IRP_CALL_SITE_ARGUMENT:
4883     return OS << "cs_arg";
4884   }
4885   llvm_unreachable("Unknown attribute position!");
4886 }
4887 
4888 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
4889   const Value &AV = Pos.getAssociatedValue();
4890   return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
4891             << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}";
4892 }
4893 
4894 template <typename base_ty, base_ty BestState, base_ty WorstState>
4895 raw_ostream &llvm::
4896 operator<<(raw_ostream &OS,
4897            const IntegerStateBase<base_ty, BestState, WorstState> &S) {
4898   return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
4899             << static_cast<const AbstractState &>(S);
4900 }
4901 
4902 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
4903   return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
4904 }
4905 
4906 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
4907   AA.print(OS);
4908   return OS;
4909 }
4910 
4911 void AbstractAttribute::print(raw_ostream &OS) const {
4912   OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState()
4913      << "]";
4914 }
4915 ///}
4916 
4917 /// ----------------------------------------------------------------------------
4918 ///                       Pass (Manager) Boilerplate
4919 /// ----------------------------------------------------------------------------
4920 
4921 static bool runAttributorOnModule(Module &M, AnalysisGetter &AG) {
4922   if (DisableAttributor)
4923     return false;
4924 
4925   LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size()
4926                     << " functions.\n");
4927 
4928   // Create an Attributor and initially empty information cache that is filled
4929   // while we identify default attribute opportunities.
4930   InformationCache InfoCache(M, AG);
4931   Attributor A(InfoCache, DepRecInterval);
4932 
4933   for (Function &F : M)
4934     A.initializeInformationCache(F);
4935 
4936   for (Function &F : M) {
4937     if (F.hasExactDefinition())
4938       NumFnWithExactDefinition++;
4939     else
4940       NumFnWithoutExactDefinition++;
4941 
4942     // We look at internal functions only on-demand but if any use is not a
4943     // direct call, we have to do it eagerly.
4944     if (F.hasLocalLinkage()) {
4945       if (llvm::all_of(F.uses(), [](const Use &U) {
4946             return ImmutableCallSite(U.getUser()) &&
4947                    ImmutableCallSite(U.getUser()).isCallee(&U);
4948           }))
4949         continue;
4950     }
4951 
4952     // Populate the Attributor with abstract attribute opportunities in the
4953     // function and the information cache with IR information.
4954     A.identifyDefaultAbstractAttributes(F);
4955   }
4956 
4957   return A.run(M) == ChangeStatus::CHANGED;
4958 }
4959 
4960 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
4961   AnalysisGetter AG(AM);
4962   if (runAttributorOnModule(M, AG)) {
4963     // FIXME: Think about passes we will preserve and add them here.
4964     return PreservedAnalyses::none();
4965   }
4966   return PreservedAnalyses::all();
4967 }
4968 
4969 namespace {
4970 
4971 struct AttributorLegacyPass : public ModulePass {
4972   static char ID;
4973 
4974   AttributorLegacyPass() : ModulePass(ID) {
4975     initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
4976   }
4977 
4978   bool runOnModule(Module &M) override {
4979     if (skipModule(M))
4980       return false;
4981 
4982     AnalysisGetter AG;
4983     return runAttributorOnModule(M, AG);
4984   }
4985 
4986   void getAnalysisUsage(AnalysisUsage &AU) const override {
4987     // FIXME: Think about passes we will preserve and add them here.
4988     AU.addRequired<TargetLibraryInfoWrapperPass>();
4989   }
4990 };
4991 
4992 } // end anonymous namespace
4993 
4994 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
4995 
4996 char AttributorLegacyPass::ID = 0;
4997 
4998 const char AAReturnedValues::ID = 0;
4999 const char AANoUnwind::ID = 0;
5000 const char AANoSync::ID = 0;
5001 const char AANoFree::ID = 0;
5002 const char AANonNull::ID = 0;
5003 const char AANoRecurse::ID = 0;
5004 const char AAWillReturn::ID = 0;
5005 const char AANoAlias::ID = 0;
5006 const char AANoReturn::ID = 0;
5007 const char AAIsDead::ID = 0;
5008 const char AADereferenceable::ID = 0;
5009 const char AAAlign::ID = 0;
5010 const char AANoCapture::ID = 0;
5011 const char AAValueSimplify::ID = 0;
5012 const char AAHeapToStack::ID = 0;
5013 const char AAMemoryBehavior::ID = 0;
5014 
5015 // Macro magic to create the static generator function for attributes that
5016 // follow the naming scheme.
5017 
5018 #define SWITCH_PK_INV(CLASS, PK, POS_NAME)                                     \
5019   case IRPosition::PK:                                                         \
5020     llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
5021 
5022 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX)                               \
5023   case IRPosition::PK:                                                         \
5024     AA = new CLASS##SUFFIX(IRP);                                               \
5025     break;
5026 
5027 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                 \
5028   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
5029     CLASS *AA = nullptr;                                                       \
5030     switch (IRP.getPositionKind()) {                                           \
5031       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
5032       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
5033       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
5034       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
5035       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
5036       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
5037       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
5038       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
5039     }                                                                          \
5040     return *AA;                                                                \
5041   }
5042 
5043 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                    \
5044   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
5045     CLASS *AA = nullptr;                                                       \
5046     switch (IRP.getPositionKind()) {                                           \
5047       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
5048       SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function")                           \
5049       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
5050       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
5051       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
5052       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
5053       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
5054       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
5055     }                                                                          \
5056     return *AA;                                                                \
5057   }
5058 
5059 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                      \
5060   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
5061     CLASS *AA = nullptr;                                                       \
5062     switch (IRP.getPositionKind()) {                                           \
5063       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
5064       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
5065       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
5066       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
5067       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
5068       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
5069       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
5070       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
5071     }                                                                          \
5072     return *AA;                                                                \
5073   }
5074 
5075 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)            \
5076   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
5077     CLASS *AA = nullptr;                                                       \
5078     switch (IRP.getPositionKind()) {                                           \
5079       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
5080       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
5081       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
5082       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
5083       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
5084       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
5085       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
5086       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
5087     }                                                                          \
5088     return *AA;                                                                \
5089   }
5090 
5091 #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                  \
5092   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
5093     CLASS *AA = nullptr;                                                       \
5094     switch (IRP.getPositionKind()) {                                           \
5095       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
5096       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
5097       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
5098       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
5099       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
5100       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
5101       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
5102       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
5103     }                                                                          \
5104     return *AA;                                                                \
5105   }
5106 
5107 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
5108 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
5109 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
5110 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
5111 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
5112 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
5113 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
5114 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
5115 
5116 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
5117 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
5118 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
5119 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
5120 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
5121 
5122 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
5123 
5124 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
5125 
5126 CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
5127 
5128 #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
5129 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
5130 #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
5131 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
5132 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
5133 #undef SWITCH_PK_CREATE
5134 #undef SWITCH_PK_INV
5135 
5136 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
5137                       "Deduce and propagate attributes", false, false)
5138 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
5139 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
5140                     "Deduce and propagate attributes", false, false)
5141