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