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