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/CallGraph.h"
24 #include "llvm/Analysis/CallGraphSCCPass.h"
25 #include "llvm/Analysis/CaptureTracking.h"
26 #include "llvm/Analysis/EHPersonalities.h"
27 #include "llvm/Analysis/GlobalsModRef.h"
28 #include "llvm/Analysis/LazyValueInfo.h"
29 #include "llvm/Analysis/Loads.h"
30 #include "llvm/Analysis/MemoryBuiltins.h"
31 #include "llvm/Analysis/ScalarEvolution.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/IR/Argument.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/CFG.h"
36 #include "llvm/IR/IRBuilder.h"
37 #include "llvm/IR/InstIterator.h"
38 #include "llvm/IR/IntrinsicInst.h"
39 #include "llvm/IR/Verifier.h"
40 #include "llvm/InitializePasses.h"
41 #include "llvm/IR/NoFolder.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Transforms/IPO/ArgumentPromotion.h"
46 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
47 #include "llvm/Transforms/Utils/Local.h"
48 
49 #include <cassert>
50 
51 using namespace llvm;
52 
53 #define DEBUG_TYPE "attributor"
54 
55 STATISTIC(NumFnWithExactDefinition,
56           "Number of function with exact definitions");
57 STATISTIC(NumFnWithoutExactDefinition,
58           "Number of function without exact definitions");
59 STATISTIC(NumAttributesTimedOut,
60           "Number of abstract attributes timed out before fixpoint");
61 STATISTIC(NumAttributesValidFixpoint,
62           "Number of abstract attributes in a valid fixpoint state");
63 STATISTIC(NumAttributesManifested,
64           "Number of abstract attributes manifested in IR");
65 STATISTIC(NumAttributesFixedDueToRequiredDependences,
66           "Number of abstract attributes fixed due to required dependences");
67 
68 // Some helper macros to deal with statistics tracking.
69 //
70 // Usage:
71 // For simple IR attribute tracking overload trackStatistics in the abstract
72 // attribute and choose the right STATS_DECLTRACK_********* macro,
73 // e.g.,:
74 //  void trackStatistics() const override {
75 //    STATS_DECLTRACK_ARG_ATTR(returned)
76 //  }
77 // If there is a single "increment" side one can use the macro
78 // STATS_DECLTRACK with a custom message. If there are multiple increment
79 // sides, STATS_DECL and STATS_TRACK can also be used separatly.
80 //
81 #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)                                     \
82   ("Number of " #TYPE " marked '" #NAME "'")
83 #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
84 #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
85 #define STATS_DECL(NAME, TYPE, MSG)                                            \
86   STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
87 #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
88 #define STATS_DECLTRACK(NAME, TYPE, MSG)                                       \
89   {                                                                            \
90     STATS_DECL(NAME, TYPE, MSG)                                                \
91     STATS_TRACK(NAME, TYPE)                                                    \
92   }
93 #define STATS_DECLTRACK_ARG_ATTR(NAME)                                         \
94   STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
95 #define STATS_DECLTRACK_CSARG_ATTR(NAME)                                       \
96   STATS_DECLTRACK(NAME, CSArguments,                                           \
97                   BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
98 #define STATS_DECLTRACK_FN_ATTR(NAME)                                          \
99   STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
100 #define STATS_DECLTRACK_CS_ATTR(NAME)                                          \
101   STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
102 #define STATS_DECLTRACK_FNRET_ATTR(NAME)                                       \
103   STATS_DECLTRACK(NAME, FunctionReturn,                                        \
104                   BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
105 #define STATS_DECLTRACK_CSRET_ATTR(NAME)                                       \
106   STATS_DECLTRACK(NAME, CSReturn,                                              \
107                   BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
108 #define STATS_DECLTRACK_FLOATING_ATTR(NAME)                                    \
109   STATS_DECLTRACK(NAME, Floating,                                              \
110                   ("Number of floating values known to be '" #NAME "'"))
111 
112 // Specialization of the operator<< for abstract attributes subclasses. This
113 // disambiguates situations where multiple operators are applicable.
114 namespace llvm {
115 #define PIPE_OPERATOR(CLASS)                                                   \
116   raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) {                  \
117     return OS << static_cast<const AbstractAttribute &>(AA);                   \
118   }
119 
120 PIPE_OPERATOR(AAIsDead)
121 PIPE_OPERATOR(AANoUnwind)
122 PIPE_OPERATOR(AANoSync)
123 PIPE_OPERATOR(AANoRecurse)
124 PIPE_OPERATOR(AAWillReturn)
125 PIPE_OPERATOR(AANoReturn)
126 PIPE_OPERATOR(AAReturnedValues)
127 PIPE_OPERATOR(AANonNull)
128 PIPE_OPERATOR(AANoAlias)
129 PIPE_OPERATOR(AADereferenceable)
130 PIPE_OPERATOR(AAAlign)
131 PIPE_OPERATOR(AANoCapture)
132 PIPE_OPERATOR(AAValueSimplify)
133 PIPE_OPERATOR(AANoFree)
134 PIPE_OPERATOR(AAHeapToStack)
135 PIPE_OPERATOR(AAReachability)
136 PIPE_OPERATOR(AAMemoryBehavior)
137 PIPE_OPERATOR(AAValueConstantRange)
138 PIPE_OPERATOR(AAPrivatizablePtr)
139 
140 #undef PIPE_OPERATOR
141 } // namespace llvm
142 
143 // TODO: Determine a good default value.
144 //
145 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
146 // (when run with the first 5 abstract attributes). The results also indicate
147 // that we never reach 32 iterations but always find a fixpoint sooner.
148 //
149 // This will become more evolved once we perform two interleaved fixpoint
150 // iterations: bottom-up and top-down.
151 static cl::opt<unsigned>
152     MaxFixpointIterations("attributor-max-iterations", cl::Hidden,
153                           cl::desc("Maximal number of fixpoint iterations."),
154                           cl::init(32));
155 static cl::opt<bool> VerifyMaxFixpointIterations(
156     "attributor-max-iterations-verify", cl::Hidden,
157     cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
158     cl::init(false));
159 
160 static cl::opt<bool> DisableAttributor(
161     "attributor-disable", cl::Hidden,
162     cl::desc("Disable the attributor inter-procedural deduction pass."),
163     cl::init(true));
164 
165 static cl::opt<bool> AnnotateDeclarationCallSites(
166     "attributor-annotate-decl-cs", cl::Hidden,
167     cl::desc("Annotate call sites of function declarations."), cl::init(false));
168 
169 static cl::opt<bool> ManifestInternal(
170     "attributor-manifest-internal", cl::Hidden,
171     cl::desc("Manifest Attributor internal string attributes."),
172     cl::init(false));
173 
174 static cl::opt<unsigned> DepRecInterval(
175     "attributor-dependence-recompute-interval", cl::Hidden,
176     cl::desc("Number of iterations until dependences are recomputed."),
177     cl::init(4));
178 
179 static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
180                                        cl::init(true), cl::Hidden);
181 
182 static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
183                                        cl::Hidden);
184 
185 /// Logic operators for the change status enum class.
186 ///
187 ///{
188 ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) {
189   return l == ChangeStatus::CHANGED ? l : r;
190 }
191 ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) {
192   return l == ChangeStatus::UNCHANGED ? l : r;
193 }
194 ///}
195 
196 Argument *IRPosition::getAssociatedArgument() const {
197   if (getPositionKind() == IRP_ARGUMENT)
198     return cast<Argument>(&getAnchorValue());
199 
200   // Not an Argument and no argument number means this is not a call site
201   // argument, thus we cannot find a callback argument to return.
202   int ArgNo = getArgNo();
203   if (ArgNo < 0)
204     return nullptr;
205 
206   // Use abstract call sites to make the connection between the call site
207   // values and the ones in callbacks. If a callback was found that makes use
208   // of the underlying call site operand, we want the corresponding callback
209   // callee argument and not the direct callee argument.
210   Optional<Argument *> CBCandidateArg;
211   SmallVector<const Use *, 4> CBUses;
212   ImmutableCallSite ICS(&getAnchorValue());
213   AbstractCallSite::getCallbackUses(ICS, CBUses);
214   for (const Use *U : CBUses) {
215     AbstractCallSite ACS(U);
216     assert(ACS && ACS.isCallbackCall());
217     if (!ACS.getCalledFunction())
218       continue;
219 
220     for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
221 
222       // Test if the underlying call site operand is argument number u of the
223       // callback callee.
224       if (ACS.getCallArgOperandNo(u) != ArgNo)
225         continue;
226 
227       assert(ACS.getCalledFunction()->arg_size() > u &&
228              "ACS mapped into var-args arguments!");
229       if (CBCandidateArg.hasValue()) {
230         CBCandidateArg = nullptr;
231         break;
232       }
233       CBCandidateArg = ACS.getCalledFunction()->getArg(u);
234     }
235   }
236 
237   // If we found a unique callback candidate argument, return it.
238   if (CBCandidateArg.hasValue() && CBCandidateArg.getValue())
239     return CBCandidateArg.getValue();
240 
241   // If no callbacks were found, or none used the underlying call site operand
242   // exclusively, use the direct callee argument if available.
243   const Function *Callee = ICS.getCalledFunction();
244   if (Callee && Callee->arg_size() > unsigned(ArgNo))
245     return Callee->getArg(ArgNo);
246 
247   return nullptr;
248 }
249 
250 static Optional<ConstantInt *>
251 getAssumedConstant(Attributor &A, const Value &V, const AbstractAttribute &AA,
252                    bool &UsedAssumedInformation) {
253   const auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
254       AA, IRPosition::value(V), /* TrackDependence */ false);
255   Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A);
256   bool IsKnown = ValueSimplifyAA.isKnown();
257   UsedAssumedInformation |= !IsKnown;
258   if (!SimplifiedV.hasValue()) {
259     A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
260     return llvm::None;
261   }
262   if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) {
263     A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
264     return llvm::None;
265   }
266   ConstantInt *CI = dyn_cast_or_null<ConstantInt>(SimplifiedV.getValue());
267   if (CI)
268     A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
269   return CI;
270 }
271 
272 /// Get pointer operand of memory accessing instruction. If \p I is
273 /// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
274 /// is set to false and the instruction is volatile, return nullptr.
275 static const Value *getPointerOperand(const Instruction *I,
276                                       bool AllowVolatile) {
277   if (auto *LI = dyn_cast<LoadInst>(I)) {
278     if (!AllowVolatile && LI->isVolatile())
279       return nullptr;
280     return LI->getPointerOperand();
281   }
282 
283   if (auto *SI = dyn_cast<StoreInst>(I)) {
284     if (!AllowVolatile && SI->isVolatile())
285       return nullptr;
286     return SI->getPointerOperand();
287   }
288 
289   if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
290     if (!AllowVolatile && CXI->isVolatile())
291       return nullptr;
292     return CXI->getPointerOperand();
293   }
294 
295   if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
296     if (!AllowVolatile && RMWI->isVolatile())
297       return nullptr;
298     return RMWI->getPointerOperand();
299   }
300 
301   return nullptr;
302 }
303 
304 /// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
305 /// advanced by \p Offset bytes. To aid later analysis the method tries to build
306 /// getelement pointer instructions that traverse the natural type of \p Ptr if
307 /// possible. If that fails, the remaining offset is adjusted byte-wise, hence
308 /// through a cast to i8*.
309 ///
310 /// TODO: This could probably live somewhere more prominantly if it doesn't
311 ///       already exist.
312 static Value *constructPointer(Type *ResTy, Value *Ptr, int64_t Offset,
313                                IRBuilder<NoFolder> &IRB, const DataLayout &DL) {
314   assert(Offset >= 0 && "Negative offset not supported yet!");
315   LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset
316                     << "-bytes as " << *ResTy << "\n");
317 
318   // The initial type we are trying to traverse to get nice GEPs.
319   Type *Ty = Ptr->getType();
320 
321   SmallVector<Value *, 4> Indices;
322   std::string GEPName = Ptr->getName().str();
323   while (Offset) {
324     uint64_t Idx, Rem;
325 
326     if (auto *STy = dyn_cast<StructType>(Ty)) {
327       const StructLayout *SL = DL.getStructLayout(STy);
328       if (int64_t(SL->getSizeInBytes()) < Offset)
329         break;
330       Idx = SL->getElementContainingOffset(Offset);
331       assert(Idx < STy->getNumElements() && "Offset calculation error!");
332       Rem = Offset - SL->getElementOffset(Idx);
333       Ty = STy->getElementType(Idx);
334     } else if (auto *PTy = dyn_cast<PointerType>(Ty)) {
335       Ty = PTy->getElementType();
336       if (!Ty->isSized())
337         break;
338       uint64_t ElementSize = DL.getTypeAllocSize(Ty);
339       assert(ElementSize && "Expected type with size!");
340       Idx = Offset / ElementSize;
341       Rem = Offset % ElementSize;
342     } else {
343       // Non-aggregate type, we cast and make byte-wise progress now.
344       break;
345     }
346 
347     LLVM_DEBUG(errs() << "Ty: " << *Ty << " Offset: " << Offset
348                       << " Idx: " << Idx << " Rem: " << Rem << "\n");
349 
350     GEPName += "." + std::to_string(Idx);
351     Indices.push_back(ConstantInt::get(IRB.getInt32Ty(), Idx));
352     Offset = Rem;
353   }
354 
355   // Create a GEP if we collected indices above.
356   if (Indices.size())
357     Ptr = IRB.CreateGEP(Ptr, Indices, GEPName);
358 
359   // If an offset is left we use byte-wise adjustment.
360   if (Offset) {
361     Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
362     Ptr = IRB.CreateGEP(Ptr, IRB.getInt32(Offset),
363                         GEPName + ".b" + Twine(Offset));
364   }
365 
366   // Ensure the result has the requested type.
367   Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast");
368 
369   LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n");
370   return Ptr;
371 }
372 
373 /// Recursively visit all values that might become \p IRP at some point. This
374 /// will be done by looking through cast instructions, selects, phis, and calls
375 /// with the "returned" attribute. Once we cannot look through the value any
376 /// further, the callback \p VisitValueCB is invoked and passed the current
377 /// value, the \p State, and a flag to indicate if we stripped anything.
378 /// Stripped means that we unpacked the value associated with \p IRP at least
379 /// once. Note that the value used for the callback may still be the value
380 /// associated with \p IRP (due to PHIs). To limit how much effort is invested,
381 /// we will never visit more values than specified by \p MaxValues.
382 template <typename AAType, typename StateTy>
383 static bool genericValueTraversal(
384     Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State,
385     const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB,
386     int MaxValues = 8) {
387 
388   const AAIsDead *LivenessAA = nullptr;
389   if (IRP.getAnchorScope())
390     LivenessAA = &A.getAAFor<AAIsDead>(
391         QueryingAA, IRPosition::function(*IRP.getAnchorScope()),
392         /* TrackDependence */ false);
393   bool AnyDead = false;
394 
395   // TODO: Use Positions here to allow context sensitivity in VisitValueCB
396   SmallPtrSet<Value *, 16> Visited;
397   SmallVector<Value *, 16> Worklist;
398   Worklist.push_back(&IRP.getAssociatedValue());
399 
400   int Iteration = 0;
401   do {
402     Value *V = Worklist.pop_back_val();
403 
404     // Check if we should process the current value. To prevent endless
405     // recursion keep a record of the values we followed!
406     if (!Visited.insert(V).second)
407       continue;
408 
409     // Make sure we limit the compile time for complex expressions.
410     if (Iteration++ >= MaxValues)
411       return false;
412 
413     // Explicitly look through calls with a "returned" attribute if we do
414     // not have a pointer as stripPointerCasts only works on them.
415     Value *NewV = nullptr;
416     if (V->getType()->isPointerTy()) {
417       NewV = V->stripPointerCasts();
418     } else {
419       CallSite CS(V);
420       if (CS && CS.getCalledFunction()) {
421         for (Argument &Arg : CS.getCalledFunction()->args())
422           if (Arg.hasReturnedAttr()) {
423             NewV = CS.getArgOperand(Arg.getArgNo());
424             break;
425           }
426       }
427     }
428     if (NewV && NewV != V) {
429       Worklist.push_back(NewV);
430       continue;
431     }
432 
433     // Look through select instructions, visit both potential values.
434     if (auto *SI = dyn_cast<SelectInst>(V)) {
435       Worklist.push_back(SI->getTrueValue());
436       Worklist.push_back(SI->getFalseValue());
437       continue;
438     }
439 
440     // Look through phi nodes, visit all live operands.
441     if (auto *PHI = dyn_cast<PHINode>(V)) {
442       assert(LivenessAA &&
443              "Expected liveness in the presence of instructions!");
444       for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
445         const BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
446         if (A.isAssumedDead(*IncomingBB->getTerminator(), &QueryingAA,
447                             LivenessAA,
448                             /* CheckBBLivenessOnly */ true)) {
449           AnyDead = true;
450           continue;
451         }
452         Worklist.push_back(PHI->getIncomingValue(u));
453       }
454       continue;
455     }
456 
457     // Once a leaf is reached we inform the user through the callback.
458     if (!VisitValueCB(*V, State, Iteration > 1))
459       return false;
460   } while (!Worklist.empty());
461 
462   // If we actually used liveness information so we have to record a dependence.
463   if (AnyDead)
464     A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
465 
466   // All values have been visited.
467   return true;
468 }
469 
470 /// Return true if \p New is equal or worse than \p Old.
471 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
472   if (!Old.isIntAttribute())
473     return true;
474 
475   return Old.getValueAsInt() >= New.getValueAsInt();
476 }
477 
478 /// Return true if the information provided by \p Attr was added to the
479 /// attribute list \p Attrs. This is only the case if it was not already present
480 /// in \p Attrs at the position describe by \p PK and \p AttrIdx.
481 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
482                              AttributeList &Attrs, int AttrIdx) {
483 
484   if (Attr.isEnumAttribute()) {
485     Attribute::AttrKind Kind = Attr.getKindAsEnum();
486     if (Attrs.hasAttribute(AttrIdx, Kind))
487       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
488         return false;
489     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
490     return true;
491   }
492   if (Attr.isStringAttribute()) {
493     StringRef Kind = Attr.getKindAsString();
494     if (Attrs.hasAttribute(AttrIdx, Kind))
495       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
496         return false;
497     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
498     return true;
499   }
500   if (Attr.isIntAttribute()) {
501     Attribute::AttrKind Kind = Attr.getKindAsEnum();
502     if (Attrs.hasAttribute(AttrIdx, Kind))
503       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
504         return false;
505     Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind);
506     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
507     return true;
508   }
509 
510   llvm_unreachable("Expected enum or string attribute!");
511 }
512 
513 static const Value *
514 getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset,
515                                      const DataLayout &DL,
516                                      bool AllowNonInbounds = false) {
517   const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false);
518   if (!Ptr)
519     return nullptr;
520 
521   return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL,
522                                           AllowNonInbounds);
523 }
524 
525 ChangeStatus AbstractAttribute::update(Attributor &A) {
526   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
527   if (getState().isAtFixpoint())
528     return HasChanged;
529 
530   LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
531 
532   HasChanged = updateImpl(A);
533 
534   LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
535                     << "\n");
536 
537   return HasChanged;
538 }
539 
540 ChangeStatus
541 IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
542                                    const ArrayRef<Attribute> &DeducedAttrs) {
543   Function *ScopeFn = IRP.getAssociatedFunction();
544   IRPosition::Kind PK = IRP.getPositionKind();
545 
546   // In the following some generic code that will manifest attributes in
547   // DeducedAttrs if they improve the current IR. Due to the different
548   // annotation positions we use the underlying AttributeList interface.
549 
550   AttributeList Attrs;
551   switch (PK) {
552   case IRPosition::IRP_INVALID:
553   case IRPosition::IRP_FLOAT:
554     return ChangeStatus::UNCHANGED;
555   case IRPosition::IRP_ARGUMENT:
556   case IRPosition::IRP_FUNCTION:
557   case IRPosition::IRP_RETURNED:
558     Attrs = ScopeFn->getAttributes();
559     break;
560   case IRPosition::IRP_CALL_SITE:
561   case IRPosition::IRP_CALL_SITE_RETURNED:
562   case IRPosition::IRP_CALL_SITE_ARGUMENT:
563     Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes();
564     break;
565   }
566 
567   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
568   LLVMContext &Ctx = IRP.getAnchorValue().getContext();
569   for (const Attribute &Attr : DeducedAttrs) {
570     if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx()))
571       continue;
572 
573     HasChanged = ChangeStatus::CHANGED;
574   }
575 
576   if (HasChanged == ChangeStatus::UNCHANGED)
577     return HasChanged;
578 
579   switch (PK) {
580   case IRPosition::IRP_ARGUMENT:
581   case IRPosition::IRP_FUNCTION:
582   case IRPosition::IRP_RETURNED:
583     ScopeFn->setAttributes(Attrs);
584     break;
585   case IRPosition::IRP_CALL_SITE:
586   case IRPosition::IRP_CALL_SITE_RETURNED:
587   case IRPosition::IRP_CALL_SITE_ARGUMENT:
588     CallSite(&IRP.getAnchorValue()).setAttributes(Attrs);
589     break;
590   case IRPosition::IRP_INVALID:
591   case IRPosition::IRP_FLOAT:
592     break;
593   }
594 
595   return HasChanged;
596 }
597 
598 const IRPosition IRPosition::EmptyKey(255);
599 const IRPosition IRPosition::TombstoneKey(256);
600 
601 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
602   IRPositions.emplace_back(IRP);
603 
604   ImmutableCallSite ICS(&IRP.getAnchorValue());
605   switch (IRP.getPositionKind()) {
606   case IRPosition::IRP_INVALID:
607   case IRPosition::IRP_FLOAT:
608   case IRPosition::IRP_FUNCTION:
609     return;
610   case IRPosition::IRP_ARGUMENT:
611   case IRPosition::IRP_RETURNED:
612     IRPositions.emplace_back(
613         IRPosition::function(*IRP.getAssociatedFunction()));
614     return;
615   case IRPosition::IRP_CALL_SITE:
616     assert(ICS && "Expected call site!");
617     // TODO: We need to look at the operand bundles similar to the redirection
618     //       in CallBase.
619     if (!ICS.hasOperandBundles())
620       if (const Function *Callee = ICS.getCalledFunction())
621         IRPositions.emplace_back(IRPosition::function(*Callee));
622     return;
623   case IRPosition::IRP_CALL_SITE_RETURNED:
624     assert(ICS && "Expected call site!");
625     // TODO: We need to look at the operand bundles similar to the redirection
626     //       in CallBase.
627     if (!ICS.hasOperandBundles()) {
628       if (const Function *Callee = ICS.getCalledFunction()) {
629         IRPositions.emplace_back(IRPosition::returned(*Callee));
630         IRPositions.emplace_back(IRPosition::function(*Callee));
631       }
632     }
633     IRPositions.emplace_back(
634         IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction())));
635     return;
636   case IRPosition::IRP_CALL_SITE_ARGUMENT: {
637     int ArgNo = IRP.getArgNo();
638     assert(ICS && ArgNo >= 0 && "Expected call site!");
639     // TODO: We need to look at the operand bundles similar to the redirection
640     //       in CallBase.
641     if (!ICS.hasOperandBundles()) {
642       const Function *Callee = ICS.getCalledFunction();
643       if (Callee && Callee->arg_size() > unsigned(ArgNo))
644         IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo)));
645       if (Callee)
646         IRPositions.emplace_back(IRPosition::function(*Callee));
647     }
648     IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
649     return;
650   }
651   }
652 }
653 
654 bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs,
655                          bool IgnoreSubsumingPositions) const {
656   for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
657     for (Attribute::AttrKind AK : AKs)
658       if (EquivIRP.getAttr(AK).getKindAsEnum() == AK)
659         return true;
660     // The first position returned by the SubsumingPositionIterator is
661     // always the position itself. If we ignore subsuming positions we
662     // are done after the first iteration.
663     if (IgnoreSubsumingPositions)
664       break;
665   }
666   return false;
667 }
668 
669 void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
670                           SmallVectorImpl<Attribute> &Attrs,
671                           bool IgnoreSubsumingPositions) const {
672   for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
673     for (Attribute::AttrKind AK : AKs) {
674       const Attribute &Attr = EquivIRP.getAttr(AK);
675       if (Attr.getKindAsEnum() == AK)
676         Attrs.push_back(Attr);
677     }
678     // The first position returned by the SubsumingPositionIterator is
679     // always the position itself. If we ignore subsuming positions we
680     // are done after the first iteration.
681     if (IgnoreSubsumingPositions)
682       break;
683   }
684 }
685 
686 void IRPosition::verify() {
687   switch (KindOrArgNo) {
688   default:
689     assert(KindOrArgNo >= 0 && "Expected argument or call site argument!");
690     assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) &&
691            "Expected call base or argument for positive attribute index!");
692     if (isa<Argument>(AnchorVal)) {
693       assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) &&
694              "Argument number mismatch!");
695       assert(cast<Argument>(AnchorVal) == &getAssociatedValue() &&
696              "Associated value mismatch!");
697     } else {
698       assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) &&
699              "Call site argument number mismatch!");
700       assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) ==
701                  &getAssociatedValue() &&
702              "Associated value mismatch!");
703     }
704     break;
705   case IRP_INVALID:
706     assert(!AnchorVal && "Expected no value for an invalid position!");
707     break;
708   case IRP_FLOAT:
709     assert((!isa<CallBase>(&getAssociatedValue()) &&
710             !isa<Argument>(&getAssociatedValue())) &&
711            "Expected specialized kind for call base and argument values!");
712     break;
713   case IRP_RETURNED:
714     assert(isa<Function>(AnchorVal) &&
715            "Expected function for a 'returned' position!");
716     assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
717     break;
718   case IRP_CALL_SITE_RETURNED:
719     assert((isa<CallBase>(AnchorVal)) &&
720            "Expected call base for 'call site returned' position!");
721     assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
722     break;
723   case IRP_CALL_SITE:
724     assert((isa<CallBase>(AnchorVal)) &&
725            "Expected call base for 'call site function' position!");
726     assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
727     break;
728   case IRP_FUNCTION:
729     assert(isa<Function>(AnchorVal) &&
730            "Expected function for a 'function' position!");
731     assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
732     break;
733   }
734 }
735 
736 namespace {
737 /// Helper function to clamp a state \p S of type \p StateType with the
738 /// information in \p R and indicate/return if \p S did change (as-in update is
739 /// required to be run again).
740 template <typename StateType>
741 ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
742   auto Assumed = S.getAssumed();
743   S ^= R;
744   return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
745                                    : ChangeStatus::CHANGED;
746 }
747 
748 /// Clamp the information known for all returned values of a function
749 /// (identified by \p QueryingAA) into \p S.
750 template <typename AAType, typename StateType = typename AAType::StateType>
751 static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA,
752                                      StateType &S) {
753   LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
754                     << QueryingAA << " into " << S << "\n");
755 
756   assert((QueryingAA.getIRPosition().getPositionKind() ==
757               IRPosition::IRP_RETURNED ||
758           QueryingAA.getIRPosition().getPositionKind() ==
759               IRPosition::IRP_CALL_SITE_RETURNED) &&
760          "Can only clamp returned value states for a function returned or call "
761          "site returned position!");
762 
763   // Use an optional state as there might not be any return values and we want
764   // to join (IntegerState::operator&) the state of all there are.
765   Optional<StateType> T;
766 
767   // Callback for each possibly returned value.
768   auto CheckReturnValue = [&](Value &RV) -> bool {
769     const IRPosition &RVPos = IRPosition::value(RV);
770     const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos);
771     LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
772                       << " @ " << RVPos << "\n");
773     const StateType &AAS = static_cast<const StateType &>(AA.getState());
774     if (T.hasValue())
775       *T &= AAS;
776     else
777       T = AAS;
778     LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
779                       << "\n");
780     return T->isValidState();
781   };
782 
783   if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
784     S.indicatePessimisticFixpoint();
785   else if (T.hasValue())
786     S ^= *T;
787 }
788 
789 /// Helper class to compose two generic deduction
790 template <typename AAType, typename Base, typename StateType,
791           template <typename...> class F, template <typename...> class G>
792 struct AAComposeTwoGenericDeduction
793     : public F<AAType, G<AAType, Base, StateType>, StateType> {
794   AAComposeTwoGenericDeduction(const IRPosition &IRP)
795       : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {}
796 
797   /// See AbstractAttribute::updateImpl(...).
798   ChangeStatus updateImpl(Attributor &A) override {
799     ChangeStatus ChangedF =
800         F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A);
801     ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A);
802     return ChangedF | ChangedG;
803   }
804 };
805 
806 /// Helper class for generic deduction: return value -> returned position.
807 template <typename AAType, typename Base,
808           typename StateType = typename Base::StateType>
809 struct AAReturnedFromReturnedValues : public Base {
810   AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {}
811 
812   /// See AbstractAttribute::updateImpl(...).
813   ChangeStatus updateImpl(Attributor &A) override {
814     StateType S(StateType::getBestState(this->getState()));
815     clampReturnedValueStates<AAType, StateType>(A, *this, S);
816     // TODO: If we know we visited all returned values, thus no are assumed
817     // dead, we can take the known information from the state T.
818     return clampStateAndIndicateChange<StateType>(this->getState(), S);
819   }
820 };
821 
822 /// Clamp the information known at all call sites for a given argument
823 /// (identified by \p QueryingAA) into \p S.
824 template <typename AAType, typename StateType = typename AAType::StateType>
825 static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
826                                         StateType &S) {
827   LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
828                     << QueryingAA << " into " << S << "\n");
829 
830   assert(QueryingAA.getIRPosition().getPositionKind() ==
831              IRPosition::IRP_ARGUMENT &&
832          "Can only clamp call site argument states for an argument position!");
833 
834   // Use an optional state as there might not be any return values and we want
835   // to join (IntegerState::operator&) the state of all there are.
836   Optional<StateType> T;
837 
838   // The argument number which is also the call site argument number.
839   unsigned ArgNo = QueryingAA.getIRPosition().getArgNo();
840 
841   auto CallSiteCheck = [&](AbstractCallSite ACS) {
842     const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
843     // Check if a coresponding argument was found or if it is on not associated
844     // (which can happen for callback calls).
845     if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
846       return false;
847 
848     const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos);
849     LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()
850                       << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n");
851     const StateType &AAS = static_cast<const StateType &>(AA.getState());
852     if (T.hasValue())
853       *T &= AAS;
854     else
855       T = AAS;
856     LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
857                       << "\n");
858     return T->isValidState();
859   };
860 
861   bool AllCallSitesKnown;
862   if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
863                               AllCallSitesKnown))
864     S.indicatePessimisticFixpoint();
865   else if (T.hasValue())
866     S ^= *T;
867 }
868 
869 /// Helper class for generic deduction: call site argument -> argument position.
870 template <typename AAType, typename Base,
871           typename StateType = typename AAType::StateType>
872 struct AAArgumentFromCallSiteArguments : public Base {
873   AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {}
874 
875   /// See AbstractAttribute::updateImpl(...).
876   ChangeStatus updateImpl(Attributor &A) override {
877     StateType S(StateType::getBestState(this->getState()));
878     clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
879     // TODO: If we know we visited all incoming values, thus no are assumed
880     // dead, we can take the known information from the state T.
881     return clampStateAndIndicateChange<StateType>(this->getState(), S);
882   }
883 };
884 
885 /// Helper class for generic replication: function returned -> cs returned.
886 template <typename AAType, typename Base,
887           typename StateType = typename Base::StateType>
888 struct AACallSiteReturnedFromReturned : public Base {
889   AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {}
890 
891   /// See AbstractAttribute::updateImpl(...).
892   ChangeStatus updateImpl(Attributor &A) override {
893     assert(this->getIRPosition().getPositionKind() ==
894                IRPosition::IRP_CALL_SITE_RETURNED &&
895            "Can only wrap function returned positions for call site returned "
896            "positions!");
897     auto &S = this->getState();
898 
899     const Function *AssociatedFunction =
900         this->getIRPosition().getAssociatedFunction();
901     if (!AssociatedFunction)
902       return S.indicatePessimisticFixpoint();
903 
904     IRPosition FnPos = IRPosition::returned(*AssociatedFunction);
905     const AAType &AA = A.getAAFor<AAType>(*this, FnPos);
906     return clampStateAndIndicateChange(
907         S, static_cast<const StateType &>(AA.getState()));
908   }
909 };
910 
911 /// Helper class for generic deduction using must-be-executed-context
912 /// Base class is required to have `followUse` method.
913 
914 /// bool followUse(Attributor &A, const Use *U, const Instruction *I)
915 /// U - Underlying use.
916 /// I - The user of the \p U.
917 /// `followUse` returns true if the value should be tracked transitively.
918 
919 template <typename AAType, typename Base,
920           typename StateType = typename AAType::StateType>
921 struct AAFromMustBeExecutedContext : public Base {
922   AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {}
923 
924   void initialize(Attributor &A) override {
925     Base::initialize(A);
926     const IRPosition &IRP = this->getIRPosition();
927     Instruction *CtxI = IRP.getCtxI();
928 
929     if (!CtxI)
930       return;
931 
932     for (const Use &U : IRP.getAssociatedValue().uses())
933       Uses.insert(&U);
934   }
935 
936   /// See AbstractAttribute::updateImpl(...).
937   ChangeStatus updateImpl(Attributor &A) override {
938     auto BeforeState = this->getState();
939     auto &S = this->getState();
940     Instruction *CtxI = this->getIRPosition().getCtxI();
941     if (!CtxI)
942       return ChangeStatus::UNCHANGED;
943 
944     MustBeExecutedContextExplorer &Explorer =
945         A.getInfoCache().getMustBeExecutedContextExplorer();
946 
947     auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
948     for (unsigned u = 0; u < Uses.size(); ++u) {
949       const Use *U = Uses[u];
950       if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
951         bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
952         if (Found && Base::followUse(A, U, UserI))
953           for (const Use &Us : UserI->uses())
954             Uses.insert(&Us);
955       }
956     }
957 
958     return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED;
959   }
960 
961 private:
962   /// Container for (transitive) uses of the associated value.
963   SetVector<const Use *> Uses;
964 };
965 
966 template <typename AAType, typename Base,
967           typename StateType = typename AAType::StateType>
968 using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext =
969     AAComposeTwoGenericDeduction<AAType, Base, StateType,
970                                  AAFromMustBeExecutedContext,
971                                  AAArgumentFromCallSiteArguments>;
972 
973 template <typename AAType, typename Base,
974           typename StateType = typename AAType::StateType>
975 using AACallSiteReturnedFromReturnedAndMustBeExecutedContext =
976     AAComposeTwoGenericDeduction<AAType, Base, StateType,
977                                  AAFromMustBeExecutedContext,
978                                  AACallSiteReturnedFromReturned>;
979 
980 /// -----------------------NoUnwind Function Attribute--------------------------
981 
982 struct AANoUnwindImpl : AANoUnwind {
983   AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {}
984 
985   const std::string getAsStr() const override {
986     return getAssumed() ? "nounwind" : "may-unwind";
987   }
988 
989   /// See AbstractAttribute::updateImpl(...).
990   ChangeStatus updateImpl(Attributor &A) override {
991     auto Opcodes = {
992         (unsigned)Instruction::Invoke,      (unsigned)Instruction::CallBr,
993         (unsigned)Instruction::Call,        (unsigned)Instruction::CleanupRet,
994         (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
995 
996     auto CheckForNoUnwind = [&](Instruction &I) {
997       if (!I.mayThrow())
998         return true;
999 
1000       if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1001         const auto &NoUnwindAA =
1002             A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS));
1003         return NoUnwindAA.isAssumedNoUnwind();
1004       }
1005       return false;
1006     };
1007 
1008     if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes))
1009       return indicatePessimisticFixpoint();
1010 
1011     return ChangeStatus::UNCHANGED;
1012   }
1013 };
1014 
1015 struct AANoUnwindFunction final : public AANoUnwindImpl {
1016   AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
1017 
1018   /// See AbstractAttribute::trackStatistics()
1019   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
1020 };
1021 
1022 /// NoUnwind attribute deduction for a call sites.
1023 struct AANoUnwindCallSite final : AANoUnwindImpl {
1024   AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
1025 
1026   /// See AbstractAttribute::initialize(...).
1027   void initialize(Attributor &A) override {
1028     AANoUnwindImpl::initialize(A);
1029     Function *F = getAssociatedFunction();
1030     if (!F)
1031       indicatePessimisticFixpoint();
1032   }
1033 
1034   /// See AbstractAttribute::updateImpl(...).
1035   ChangeStatus updateImpl(Attributor &A) override {
1036     // TODO: Once we have call site specific value information we can provide
1037     //       call site specific liveness information and then it makes
1038     //       sense to specialize attributes for call sites arguments instead of
1039     //       redirecting requests to the callee argument.
1040     Function *F = getAssociatedFunction();
1041     const IRPosition &FnPos = IRPosition::function(*F);
1042     auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos);
1043     return clampStateAndIndicateChange(
1044         getState(),
1045         static_cast<const AANoUnwind::StateType &>(FnAA.getState()));
1046   }
1047 
1048   /// See AbstractAttribute::trackStatistics()
1049   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
1050 };
1051 
1052 /// --------------------- Function Return Values -------------------------------
1053 
1054 /// "Attribute" that collects all potential returned values and the return
1055 /// instructions that they arise from.
1056 ///
1057 /// If there is a unique returned value R, the manifest method will:
1058 ///   - mark R with the "returned" attribute, if R is an argument.
1059 class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
1060 
1061   /// Mapping of values potentially returned by the associated function to the
1062   /// return instructions that might return them.
1063   MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
1064 
1065   /// Mapping to remember the number of returned values for a call site such
1066   /// that we can avoid updates if nothing changed.
1067   DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA;
1068 
1069   /// Set of unresolved calls returned by the associated function.
1070   SmallSetVector<CallBase *, 4> UnresolvedCalls;
1071 
1072   /// State flags
1073   ///
1074   ///{
1075   bool IsFixed = false;
1076   bool IsValidState = true;
1077   ///}
1078 
1079 public:
1080   AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {}
1081 
1082   /// See AbstractAttribute::initialize(...).
1083   void initialize(Attributor &A) override {
1084     // Reset the state.
1085     IsFixed = false;
1086     IsValidState = true;
1087     ReturnedValues.clear();
1088 
1089     Function *F = getAssociatedFunction();
1090     if (!F) {
1091       indicatePessimisticFixpoint();
1092       return;
1093     }
1094     assert(!F->getReturnType()->isVoidTy() &&
1095            "Did not expect a void return type!");
1096 
1097     // The map from instruction opcodes to those instructions in the function.
1098     auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
1099 
1100     // Look through all arguments, if one is marked as returned we are done.
1101     for (Argument &Arg : F->args()) {
1102       if (Arg.hasReturnedAttr()) {
1103         auto &ReturnInstSet = ReturnedValues[&Arg];
1104         for (Instruction *RI : OpcodeInstMap[Instruction::Ret])
1105           ReturnInstSet.insert(cast<ReturnInst>(RI));
1106 
1107         indicateOptimisticFixpoint();
1108         return;
1109       }
1110     }
1111 
1112     if (!F->hasExactDefinition())
1113       indicatePessimisticFixpoint();
1114   }
1115 
1116   /// See AbstractAttribute::manifest(...).
1117   ChangeStatus manifest(Attributor &A) override;
1118 
1119   /// See AbstractAttribute::getState(...).
1120   AbstractState &getState() override { return *this; }
1121 
1122   /// See AbstractAttribute::getState(...).
1123   const AbstractState &getState() const override { return *this; }
1124 
1125   /// See AbstractAttribute::updateImpl(Attributor &A).
1126   ChangeStatus updateImpl(Attributor &A) override;
1127 
1128   llvm::iterator_range<iterator> returned_values() override {
1129     return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1130   }
1131 
1132   llvm::iterator_range<const_iterator> returned_values() const override {
1133     return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1134   }
1135 
1136   const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override {
1137     return UnresolvedCalls;
1138   }
1139 
1140   /// Return the number of potential return values, -1 if unknown.
1141   size_t getNumReturnValues() const override {
1142     return isValidState() ? ReturnedValues.size() : -1;
1143   }
1144 
1145   /// Return an assumed unique return value if a single candidate is found. If
1146   /// there cannot be one, return a nullptr. If it is not clear yet, return the
1147   /// Optional::NoneType.
1148   Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
1149 
1150   /// See AbstractState::checkForAllReturnedValues(...).
1151   bool checkForAllReturnedValuesAndReturnInsts(
1152       const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
1153           &Pred) const override;
1154 
1155   /// Pretty print the attribute similar to the IR representation.
1156   const std::string getAsStr() const override;
1157 
1158   /// See AbstractState::isAtFixpoint().
1159   bool isAtFixpoint() const override { return IsFixed; }
1160 
1161   /// See AbstractState::isValidState().
1162   bool isValidState() const override { return IsValidState; }
1163 
1164   /// See AbstractState::indicateOptimisticFixpoint(...).
1165   ChangeStatus indicateOptimisticFixpoint() override {
1166     IsFixed = true;
1167     return ChangeStatus::UNCHANGED;
1168   }
1169 
1170   ChangeStatus indicatePessimisticFixpoint() override {
1171     IsFixed = true;
1172     IsValidState = false;
1173     return ChangeStatus::CHANGED;
1174   }
1175 };
1176 
1177 ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
1178   ChangeStatus Changed = ChangeStatus::UNCHANGED;
1179 
1180   // Bookkeeping.
1181   assert(isValidState());
1182   STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
1183                   "Number of function with known return values");
1184 
1185   // Check if we have an assumed unique return value that we could manifest.
1186   Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
1187 
1188   if (!UniqueRV.hasValue() || !UniqueRV.getValue())
1189     return Changed;
1190 
1191   // Bookkeeping.
1192   STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
1193                   "Number of function with unique return");
1194 
1195   // Callback to replace the uses of CB with the constant C.
1196   auto ReplaceCallSiteUsersWith = [&A](CallBase &CB, Constant &C) {
1197     if (CB.getNumUses() == 0 || CB.isMustTailCall())
1198       return ChangeStatus::UNCHANGED;
1199     if (A.changeValueAfterManifest(CB, C))
1200       return ChangeStatus::CHANGED;
1201     return ChangeStatus::UNCHANGED;
1202   };
1203 
1204   // If the assumed unique return value is an argument, annotate it.
1205   if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
1206     // TODO: This should be handled differently!
1207     this->AnchorVal = UniqueRVArg;
1208     this->KindOrArgNo = UniqueRVArg->getArgNo();
1209     Changed = IRAttribute::manifest(A);
1210   } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) {
1211     // We can replace the returned value with the unique returned constant.
1212     Value &AnchorValue = getAnchorValue();
1213     if (Function *F = dyn_cast<Function>(&AnchorValue)) {
1214       for (const Use &U : F->uses())
1215         if (CallBase *CB = dyn_cast<CallBase>(U.getUser()))
1216           if (CB->isCallee(&U)) {
1217             Constant *RVCCast =
1218                 CB->getType() == RVC->getType()
1219                     ? RVC
1220                     : ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
1221             Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed;
1222           }
1223     } else {
1224       assert(isa<CallBase>(AnchorValue) &&
1225              "Expcected a function or call base anchor!");
1226       Constant *RVCCast =
1227           AnchorValue.getType() == RVC->getType()
1228               ? RVC
1229               : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
1230       Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast);
1231     }
1232     if (Changed == ChangeStatus::CHANGED)
1233       STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn,
1234                       "Number of function returns replaced by constant return");
1235   }
1236 
1237   return Changed;
1238 }
1239 
1240 const std::string AAReturnedValuesImpl::getAsStr() const {
1241   return (isAtFixpoint() ? "returns(#" : "may-return(#") +
1242          (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
1243          ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]";
1244 }
1245 
1246 Optional<Value *>
1247 AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
1248   // If checkForAllReturnedValues provides a unique value, ignoring potential
1249   // undef values that can also be present, it is assumed to be the actual
1250   // return value and forwarded to the caller of this method. If there are
1251   // multiple, a nullptr is returned indicating there cannot be a unique
1252   // returned value.
1253   Optional<Value *> UniqueRV;
1254 
1255   auto Pred = [&](Value &RV) -> bool {
1256     // If we found a second returned value and neither the current nor the saved
1257     // one is an undef, there is no unique returned value. Undefs are special
1258     // since we can pretend they have any value.
1259     if (UniqueRV.hasValue() && UniqueRV != &RV &&
1260         !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
1261       UniqueRV = nullptr;
1262       return false;
1263     }
1264 
1265     // Do not overwrite a value with an undef.
1266     if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
1267       UniqueRV = &RV;
1268 
1269     return true;
1270   };
1271 
1272   if (!A.checkForAllReturnedValues(Pred, *this))
1273     UniqueRV = nullptr;
1274 
1275   return UniqueRV;
1276 }
1277 
1278 bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
1279     const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
1280         &Pred) const {
1281   if (!isValidState())
1282     return false;
1283 
1284   // Check all returned values but ignore call sites as long as we have not
1285   // encountered an overdefined one during an update.
1286   for (auto &It : ReturnedValues) {
1287     Value *RV = It.first;
1288 
1289     CallBase *CB = dyn_cast<CallBase>(RV);
1290     if (CB && !UnresolvedCalls.count(CB))
1291       continue;
1292 
1293     if (!Pred(*RV, It.second))
1294       return false;
1295   }
1296 
1297   return true;
1298 }
1299 
1300 ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
1301   size_t NumUnresolvedCalls = UnresolvedCalls.size();
1302   bool Changed = false;
1303 
1304   // State used in the value traversals starting in returned values.
1305   struct RVState {
1306     // The map in which we collect return values -> return instrs.
1307     decltype(ReturnedValues) &RetValsMap;
1308     // The flag to indicate a change.
1309     bool &Changed;
1310     // The return instrs we come from.
1311     SmallSetVector<ReturnInst *, 4> RetInsts;
1312   };
1313 
1314   // Callback for a leaf value returned by the associated function.
1315   auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool {
1316     auto Size = RVS.RetValsMap[&Val].size();
1317     RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end());
1318     bool Inserted = RVS.RetValsMap[&Val].size() != Size;
1319     RVS.Changed |= Inserted;
1320     LLVM_DEBUG({
1321       if (Inserted)
1322         dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val
1323                << " => " << RVS.RetInsts.size() << "\n";
1324     });
1325     return true;
1326   };
1327 
1328   // Helper method to invoke the generic value traversal.
1329   auto VisitReturnedValue = [&](Value &RV, RVState &RVS) {
1330     IRPosition RetValPos = IRPosition::value(RV);
1331     return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this,
1332                                                             RVS, VisitValueCB);
1333   };
1334 
1335   // Callback for all "return intructions" live in the associated function.
1336   auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) {
1337     ReturnInst &Ret = cast<ReturnInst>(I);
1338     RVState RVS({ReturnedValues, Changed, {}});
1339     RVS.RetInsts.insert(&Ret);
1340     return VisitReturnedValue(*Ret.getReturnValue(), RVS);
1341   };
1342 
1343   // Start by discovering returned values from all live returned instructions in
1344   // the associated function.
1345   if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret}))
1346     return indicatePessimisticFixpoint();
1347 
1348   // Once returned values "directly" present in the code are handled we try to
1349   // resolve returned calls.
1350   decltype(ReturnedValues) NewRVsMap;
1351   for (auto &It : ReturnedValues) {
1352     LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first
1353                       << " by #" << It.second.size() << " RIs\n");
1354     CallBase *CB = dyn_cast<CallBase>(It.first);
1355     if (!CB || UnresolvedCalls.count(CB))
1356       continue;
1357 
1358     if (!CB->getCalledFunction()) {
1359       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1360                         << "\n");
1361       UnresolvedCalls.insert(CB);
1362       continue;
1363     }
1364 
1365     // TODO: use the function scope once we have call site AAReturnedValues.
1366     const auto &RetValAA = A.getAAFor<AAReturnedValues>(
1367         *this, IRPosition::function(*CB->getCalledFunction()));
1368     LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: "
1369                       << RetValAA << "\n");
1370 
1371     // Skip dead ends, thus if we do not know anything about the returned
1372     // call we mark it as unresolved and it will stay that way.
1373     if (!RetValAA.getState().isValidState()) {
1374       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1375                         << "\n");
1376       UnresolvedCalls.insert(CB);
1377       continue;
1378     }
1379 
1380     // Do not try to learn partial information. If the callee has unresolved
1381     // return values we will treat the call as unresolved/opaque.
1382     auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls();
1383     if (!RetValAAUnresolvedCalls.empty()) {
1384       UnresolvedCalls.insert(CB);
1385       continue;
1386     }
1387 
1388     // Now check if we can track transitively returned values. If possible, thus
1389     // if all return value can be represented in the current scope, do so.
1390     bool Unresolved = false;
1391     for (auto &RetValAAIt : RetValAA.returned_values()) {
1392       Value *RetVal = RetValAAIt.first;
1393       if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) ||
1394           isa<Constant>(RetVal))
1395         continue;
1396       // Anything that did not fit in the above categories cannot be resolved,
1397       // mark the call as unresolved.
1398       LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value "
1399                            "cannot be translated: "
1400                         << *RetVal << "\n");
1401       UnresolvedCalls.insert(CB);
1402       Unresolved = true;
1403       break;
1404     }
1405 
1406     if (Unresolved)
1407       continue;
1408 
1409     // Now track transitively returned values.
1410     unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB];
1411     if (NumRetAA == RetValAA.getNumReturnValues()) {
1412       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not "
1413                            "changed since it was seen last\n");
1414       continue;
1415     }
1416     NumRetAA = RetValAA.getNumReturnValues();
1417 
1418     for (auto &RetValAAIt : RetValAA.returned_values()) {
1419       Value *RetVal = RetValAAIt.first;
1420       if (Argument *Arg = dyn_cast<Argument>(RetVal)) {
1421         // Arguments are mapped to call site operands and we begin the traversal
1422         // again.
1423         bool Unused = false;
1424         RVState RVS({NewRVsMap, Unused, RetValAAIt.second});
1425         VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS);
1426         continue;
1427       } else if (isa<CallBase>(RetVal)) {
1428         // Call sites are resolved by the callee attribute over time, no need to
1429         // do anything for us.
1430         continue;
1431       } else if (isa<Constant>(RetVal)) {
1432         // Constants are valid everywhere, we can simply take them.
1433         NewRVsMap[RetVal].insert(It.second.begin(), It.second.end());
1434         continue;
1435       }
1436     }
1437   }
1438 
1439   // To avoid modifications to the ReturnedValues map while we iterate over it
1440   // we kept record of potential new entries in a copy map, NewRVsMap.
1441   for (auto &It : NewRVsMap) {
1442     assert(!It.second.empty() && "Entry does not add anything.");
1443     auto &ReturnInsts = ReturnedValues[It.first];
1444     for (ReturnInst *RI : It.second)
1445       if (ReturnInsts.insert(RI)) {
1446         LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
1447                           << *It.first << " => " << *RI << "\n");
1448         Changed = true;
1449       }
1450   }
1451 
1452   Changed |= (NumUnresolvedCalls != UnresolvedCalls.size());
1453   return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
1454 }
1455 
1456 struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1457   AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1458 
1459   /// See AbstractAttribute::trackStatistics()
1460   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
1461 };
1462 
1463 /// Returned values information for a call sites.
1464 struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1465   AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1466 
1467   /// See AbstractAttribute::initialize(...).
1468   void initialize(Attributor &A) override {
1469     // TODO: Once we have call site specific value information we can provide
1470     //       call site specific liveness information and then it makes
1471     //       sense to specialize attributes for call sites instead of
1472     //       redirecting requests to the callee.
1473     llvm_unreachable("Abstract attributes for returned values are not "
1474                      "supported for call sites yet!");
1475   }
1476 
1477   /// See AbstractAttribute::updateImpl(...).
1478   ChangeStatus updateImpl(Attributor &A) override {
1479     return indicatePessimisticFixpoint();
1480   }
1481 
1482   /// See AbstractAttribute::trackStatistics()
1483   void trackStatistics() const override {}
1484 };
1485 
1486 /// ------------------------ NoSync Function Attribute -------------------------
1487 
1488 struct AANoSyncImpl : AANoSync {
1489   AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {}
1490 
1491   const std::string getAsStr() const override {
1492     return getAssumed() ? "nosync" : "may-sync";
1493   }
1494 
1495   /// See AbstractAttribute::updateImpl(...).
1496   ChangeStatus updateImpl(Attributor &A) override;
1497 
1498   /// Helper function used to determine whether an instruction is non-relaxed
1499   /// atomic. In other words, if an atomic instruction does not have unordered
1500   /// or monotonic ordering
1501   static bool isNonRelaxedAtomic(Instruction *I);
1502 
1503   /// Helper function used to determine whether an instruction is volatile.
1504   static bool isVolatile(Instruction *I);
1505 
1506   /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
1507   /// memset).
1508   static bool isNoSyncIntrinsic(Instruction *I);
1509 };
1510 
1511 bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
1512   if (!I->isAtomic())
1513     return false;
1514 
1515   AtomicOrdering Ordering;
1516   switch (I->getOpcode()) {
1517   case Instruction::AtomicRMW:
1518     Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1519     break;
1520   case Instruction::Store:
1521     Ordering = cast<StoreInst>(I)->getOrdering();
1522     break;
1523   case Instruction::Load:
1524     Ordering = cast<LoadInst>(I)->getOrdering();
1525     break;
1526   case Instruction::Fence: {
1527     auto *FI = cast<FenceInst>(I);
1528     if (FI->getSyncScopeID() == SyncScope::SingleThread)
1529       return false;
1530     Ordering = FI->getOrdering();
1531     break;
1532   }
1533   case Instruction::AtomicCmpXchg: {
1534     AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering();
1535     AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering();
1536     // Only if both are relaxed, than it can be treated as relaxed.
1537     // Otherwise it is non-relaxed.
1538     if (Success != AtomicOrdering::Unordered &&
1539         Success != AtomicOrdering::Monotonic)
1540       return true;
1541     if (Failure != AtomicOrdering::Unordered &&
1542         Failure != AtomicOrdering::Monotonic)
1543       return true;
1544     return false;
1545   }
1546   default:
1547     llvm_unreachable(
1548         "New atomic operations need to be known in the attributor.");
1549   }
1550 
1551   // Relaxed.
1552   if (Ordering == AtomicOrdering::Unordered ||
1553       Ordering == AtomicOrdering::Monotonic)
1554     return false;
1555   return true;
1556 }
1557 
1558 /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
1559 /// FIXME: We should ipmrove the handling of intrinsics.
1560 bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
1561   if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1562     switch (II->getIntrinsicID()) {
1563     /// Element wise atomic memory intrinsics are can only be unordered,
1564     /// therefore nosync.
1565     case Intrinsic::memset_element_unordered_atomic:
1566     case Intrinsic::memmove_element_unordered_atomic:
1567     case Intrinsic::memcpy_element_unordered_atomic:
1568       return true;
1569     case Intrinsic::memset:
1570     case Intrinsic::memmove:
1571     case Intrinsic::memcpy:
1572       if (!cast<MemIntrinsic>(II)->isVolatile())
1573         return true;
1574       return false;
1575     default:
1576       return false;
1577     }
1578   }
1579   return false;
1580 }
1581 
1582 bool AANoSyncImpl::isVolatile(Instruction *I) {
1583   assert(!ImmutableCallSite(I) && !isa<CallBase>(I) &&
1584          "Calls should not be checked here");
1585 
1586   switch (I->getOpcode()) {
1587   case Instruction::AtomicRMW:
1588     return cast<AtomicRMWInst>(I)->isVolatile();
1589   case Instruction::Store:
1590     return cast<StoreInst>(I)->isVolatile();
1591   case Instruction::Load:
1592     return cast<LoadInst>(I)->isVolatile();
1593   case Instruction::AtomicCmpXchg:
1594     return cast<AtomicCmpXchgInst>(I)->isVolatile();
1595   default:
1596     return false;
1597   }
1598 }
1599 
1600 ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
1601 
1602   auto CheckRWInstForNoSync = [&](Instruction &I) {
1603     /// We are looking for volatile instructions or Non-Relaxed atomics.
1604     /// FIXME: We should improve the handling of intrinsics.
1605 
1606     if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I))
1607       return true;
1608 
1609     if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1610       if (ICS.hasFnAttr(Attribute::NoSync))
1611         return true;
1612 
1613       const auto &NoSyncAA =
1614           A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS));
1615       if (NoSyncAA.isAssumedNoSync())
1616         return true;
1617       return false;
1618     }
1619 
1620     if (!isVolatile(&I) && !isNonRelaxedAtomic(&I))
1621       return true;
1622 
1623     return false;
1624   };
1625 
1626   auto CheckForNoSync = [&](Instruction &I) {
1627     // At this point we handled all read/write effects and they are all
1628     // nosync, so they can be skipped.
1629     if (I.mayReadOrWriteMemory())
1630       return true;
1631 
1632     // non-convergent and readnone imply nosync.
1633     return !ImmutableCallSite(&I).isConvergent();
1634   };
1635 
1636   if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) ||
1637       !A.checkForAllCallLikeInstructions(CheckForNoSync, *this))
1638     return indicatePessimisticFixpoint();
1639 
1640   return ChangeStatus::UNCHANGED;
1641 }
1642 
1643 struct AANoSyncFunction final : public AANoSyncImpl {
1644   AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1645 
1646   /// See AbstractAttribute::trackStatistics()
1647   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
1648 };
1649 
1650 /// NoSync attribute deduction for a call sites.
1651 struct AANoSyncCallSite final : AANoSyncImpl {
1652   AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1653 
1654   /// See AbstractAttribute::initialize(...).
1655   void initialize(Attributor &A) override {
1656     AANoSyncImpl::initialize(A);
1657     Function *F = getAssociatedFunction();
1658     if (!F)
1659       indicatePessimisticFixpoint();
1660   }
1661 
1662   /// See AbstractAttribute::updateImpl(...).
1663   ChangeStatus updateImpl(Attributor &A) override {
1664     // TODO: Once we have call site specific value information we can provide
1665     //       call site specific liveness information and then it makes
1666     //       sense to specialize attributes for call sites arguments instead of
1667     //       redirecting requests to the callee argument.
1668     Function *F = getAssociatedFunction();
1669     const IRPosition &FnPos = IRPosition::function(*F);
1670     auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos);
1671     return clampStateAndIndicateChange(
1672         getState(), static_cast<const AANoSync::StateType &>(FnAA.getState()));
1673   }
1674 
1675   /// See AbstractAttribute::trackStatistics()
1676   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
1677 };
1678 
1679 /// ------------------------ No-Free Attributes ----------------------------
1680 
1681 struct AANoFreeImpl : public AANoFree {
1682   AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {}
1683 
1684   /// See AbstractAttribute::updateImpl(...).
1685   ChangeStatus updateImpl(Attributor &A) override {
1686     auto CheckForNoFree = [&](Instruction &I) {
1687       ImmutableCallSite ICS(&I);
1688       if (ICS.hasFnAttr(Attribute::NoFree))
1689         return true;
1690 
1691       const auto &NoFreeAA =
1692           A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS));
1693       return NoFreeAA.isAssumedNoFree();
1694     };
1695 
1696     if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this))
1697       return indicatePessimisticFixpoint();
1698     return ChangeStatus::UNCHANGED;
1699   }
1700 
1701   /// See AbstractAttribute::getAsStr().
1702   const std::string getAsStr() const override {
1703     return getAssumed() ? "nofree" : "may-free";
1704   }
1705 };
1706 
1707 struct AANoFreeFunction final : public AANoFreeImpl {
1708   AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1709 
1710   /// See AbstractAttribute::trackStatistics()
1711   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
1712 };
1713 
1714 /// NoFree attribute deduction for a call sites.
1715 struct AANoFreeCallSite final : AANoFreeImpl {
1716   AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1717 
1718   /// See AbstractAttribute::initialize(...).
1719   void initialize(Attributor &A) override {
1720     AANoFreeImpl::initialize(A);
1721     Function *F = getAssociatedFunction();
1722     if (!F)
1723       indicatePessimisticFixpoint();
1724   }
1725 
1726   /// See AbstractAttribute::updateImpl(...).
1727   ChangeStatus updateImpl(Attributor &A) override {
1728     // TODO: Once we have call site specific value information we can provide
1729     //       call site specific liveness information and then it makes
1730     //       sense to specialize attributes for call sites arguments instead of
1731     //       redirecting requests to the callee argument.
1732     Function *F = getAssociatedFunction();
1733     const IRPosition &FnPos = IRPosition::function(*F);
1734     auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos);
1735     return clampStateAndIndicateChange(
1736         getState(), static_cast<const AANoFree::StateType &>(FnAA.getState()));
1737   }
1738 
1739   /// See AbstractAttribute::trackStatistics()
1740   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
1741 };
1742 
1743 /// NoFree attribute for floating values.
1744 struct AANoFreeFloating : AANoFreeImpl {
1745   AANoFreeFloating(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1746 
1747   /// See AbstractAttribute::trackStatistics()
1748   void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)}
1749 
1750   /// See Abstract Attribute::updateImpl(...).
1751   ChangeStatus updateImpl(Attributor &A) override {
1752     const IRPosition &IRP = getIRPosition();
1753 
1754     const auto &NoFreeAA =
1755         A.getAAFor<AANoFree>(*this, IRPosition::function_scope(IRP));
1756     if (NoFreeAA.isAssumedNoFree())
1757       return ChangeStatus::UNCHANGED;
1758 
1759     Value &AssociatedValue = getIRPosition().getAssociatedValue();
1760     auto Pred = [&](const Use &U, bool &Follow) -> bool {
1761       Instruction *UserI = cast<Instruction>(U.getUser());
1762       if (auto *CB = dyn_cast<CallBase>(UserI)) {
1763         if (CB->isBundleOperand(&U))
1764           return false;
1765         if (!CB->isArgOperand(&U))
1766           return true;
1767         unsigned ArgNo = CB->getArgOperandNo(&U);
1768 
1769         const auto &NoFreeArg = A.getAAFor<AANoFree>(
1770             *this, IRPosition::callsite_argument(*CB, ArgNo));
1771         return NoFreeArg.isAssumedNoFree();
1772       }
1773 
1774       if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
1775           isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
1776         Follow = true;
1777         return true;
1778       }
1779       if (isa<ReturnInst>(UserI))
1780         return true;
1781 
1782       // Unknown user.
1783       return false;
1784     };
1785     if (!A.checkForAllUses(Pred, *this, AssociatedValue))
1786       return indicatePessimisticFixpoint();
1787 
1788     return ChangeStatus::UNCHANGED;
1789   }
1790 };
1791 
1792 /// NoFree attribute for a call site argument.
1793 struct AANoFreeArgument final : AANoFreeFloating {
1794   AANoFreeArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1795 
1796   /// See AbstractAttribute::trackStatistics()
1797   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) }
1798 };
1799 
1800 /// NoFree attribute for call site arguments.
1801 struct AANoFreeCallSiteArgument final : AANoFreeFloating {
1802   AANoFreeCallSiteArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1803 
1804   /// See AbstractAttribute::updateImpl(...).
1805   ChangeStatus updateImpl(Attributor &A) override {
1806     // TODO: Once we have call site specific value information we can provide
1807     //       call site specific liveness information and then it makes
1808     //       sense to specialize attributes for call sites arguments instead of
1809     //       redirecting requests to the callee argument.
1810     Argument *Arg = getAssociatedArgument();
1811     if (!Arg)
1812       return indicatePessimisticFixpoint();
1813     const IRPosition &ArgPos = IRPosition::argument(*Arg);
1814     auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos);
1815     return clampStateAndIndicateChange(
1816         getState(), static_cast<const AANoFree::StateType &>(ArgAA.getState()));
1817   }
1818 
1819   /// See AbstractAttribute::trackStatistics()
1820   void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)};
1821 };
1822 
1823 /// NoFree attribute for function return value.
1824 struct AANoFreeReturned final : AANoFreeFloating {
1825   AANoFreeReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {
1826     llvm_unreachable("NoFree is not applicable to function returns!");
1827   }
1828 
1829   /// See AbstractAttribute::initialize(...).
1830   void initialize(Attributor &A) override {
1831     llvm_unreachable("NoFree is not applicable to function returns!");
1832   }
1833 
1834   /// See AbstractAttribute::updateImpl(...).
1835   ChangeStatus updateImpl(Attributor &A) override {
1836     llvm_unreachable("NoFree is not applicable to function returns!");
1837   }
1838 
1839   /// See AbstractAttribute::trackStatistics()
1840   void trackStatistics() const override {}
1841 };
1842 
1843 /// NoFree attribute deduction for a call site return value.
1844 struct AANoFreeCallSiteReturned final : AANoFreeFloating {
1845   AANoFreeCallSiteReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1846 
1847   ChangeStatus manifest(Attributor &A) override {
1848     return ChangeStatus::UNCHANGED;
1849   }
1850   /// See AbstractAttribute::trackStatistics()
1851   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) }
1852 };
1853 
1854 /// ------------------------ NonNull Argument Attribute ------------------------
1855 static int64_t getKnownNonNullAndDerefBytesForUse(
1856     Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue,
1857     const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
1858   TrackUse = false;
1859 
1860   const Value *UseV = U->get();
1861   if (!UseV->getType()->isPointerTy())
1862     return 0;
1863 
1864   Type *PtrTy = UseV->getType();
1865   const Function *F = I->getFunction();
1866   bool NullPointerIsDefined =
1867       F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
1868   const DataLayout &DL = A.getInfoCache().getDL();
1869   if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
1870     if (ICS.isBundleOperand(U))
1871       return 0;
1872 
1873     if (ICS.isCallee(U)) {
1874       IsNonNull |= !NullPointerIsDefined;
1875       return 0;
1876     }
1877 
1878     unsigned ArgNo = ICS.getArgumentNo(U);
1879     IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo);
1880     // As long as we only use known information there is no need to track
1881     // dependences here.
1882     auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP,
1883                                                   /* TrackDependence */ false);
1884     IsNonNull |= DerefAA.isKnownNonNull();
1885     return DerefAA.getKnownDereferenceableBytes();
1886   }
1887 
1888   // We need to follow common pointer manipulation uses to the accesses they
1889   // feed into. We can try to be smart to avoid looking through things we do not
1890   // like for now, e.g., non-inbounds GEPs.
1891   if (isa<CastInst>(I)) {
1892     TrackUse = true;
1893     return 0;
1894   }
1895   if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
1896     if (GEP->hasAllConstantIndices()) {
1897       TrackUse = true;
1898       return 0;
1899     }
1900 
1901   int64_t Offset;
1902   if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) {
1903     if (Base == &AssociatedValue &&
1904         getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
1905       int64_t DerefBytes =
1906           (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset;
1907 
1908       IsNonNull |= !NullPointerIsDefined;
1909       return std::max(int64_t(0), DerefBytes);
1910     }
1911   }
1912 
1913   /// Corner case when an offset is 0.
1914   if (const Value *Base = getBasePointerOfAccessPointerOperand(
1915           I, Offset, DL, /*AllowNonInbounds*/ true)) {
1916     if (Offset == 0 && Base == &AssociatedValue &&
1917         getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
1918       int64_t DerefBytes =
1919           (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType());
1920       IsNonNull |= !NullPointerIsDefined;
1921       return std::max(int64_t(0), DerefBytes);
1922     }
1923   }
1924 
1925   return 0;
1926 }
1927 
1928 struct AANonNullImpl : AANonNull {
1929   AANonNullImpl(const IRPosition &IRP)
1930       : AANonNull(IRP),
1931         NullIsDefined(NullPointerIsDefined(
1932             getAnchorScope(),
1933             getAssociatedValue().getType()->getPointerAddressSpace())) {}
1934 
1935   /// See AbstractAttribute::initialize(...).
1936   void initialize(Attributor &A) override {
1937     if (!NullIsDefined &&
1938         hasAttr({Attribute::NonNull, Attribute::Dereferenceable}))
1939       indicateOptimisticFixpoint();
1940     else if (isa<ConstantPointerNull>(getAssociatedValue()))
1941       indicatePessimisticFixpoint();
1942     else
1943       AANonNull::initialize(A);
1944   }
1945 
1946   /// See AAFromMustBeExecutedContext
1947   bool followUse(Attributor &A, const Use *U, const Instruction *I) {
1948     bool IsNonNull = false;
1949     bool TrackUse = false;
1950     getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
1951                                        IsNonNull, TrackUse);
1952     setKnown(IsNonNull);
1953     return TrackUse;
1954   }
1955 
1956   /// See AbstractAttribute::getAsStr().
1957   const std::string getAsStr() const override {
1958     return getAssumed() ? "nonnull" : "may-null";
1959   }
1960 
1961   /// Flag to determine if the underlying value can be null and still allow
1962   /// valid accesses.
1963   const bool NullIsDefined;
1964 };
1965 
1966 /// NonNull attribute for a floating value.
1967 struct AANonNullFloating
1968     : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> {
1969   using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>;
1970   AANonNullFloating(const IRPosition &IRP) : Base(IRP) {}
1971 
1972   /// See AbstractAttribute::updateImpl(...).
1973   ChangeStatus updateImpl(Attributor &A) override {
1974     ChangeStatus Change = Base::updateImpl(A);
1975     if (isKnownNonNull())
1976       return Change;
1977 
1978     if (!NullIsDefined) {
1979       const auto &DerefAA =
1980           A.getAAFor<AADereferenceable>(*this, getIRPosition());
1981       if (DerefAA.getAssumedDereferenceableBytes())
1982         return Change;
1983     }
1984 
1985     const DataLayout &DL = A.getDataLayout();
1986 
1987     DominatorTree *DT = nullptr;
1988     InformationCache &InfoCache = A.getInfoCache();
1989     if (const Function *Fn = getAnchorScope())
1990       DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
1991 
1992     auto VisitValueCB = [&](Value &V, AANonNull::StateType &T,
1993                             bool Stripped) -> bool {
1994       const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V));
1995       if (!Stripped && this == &AA) {
1996         if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, getCtxI(), DT))
1997           T.indicatePessimisticFixpoint();
1998       } else {
1999         // Use abstract attribute information.
2000         const AANonNull::StateType &NS =
2001             static_cast<const AANonNull::StateType &>(AA.getState());
2002         T ^= NS;
2003       }
2004       return T.isValidState();
2005     };
2006 
2007     StateType T;
2008     if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this,
2009                                                      T, VisitValueCB))
2010       return indicatePessimisticFixpoint();
2011 
2012     return clampStateAndIndicateChange(getState(), T);
2013   }
2014 
2015   /// See AbstractAttribute::trackStatistics()
2016   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
2017 };
2018 
2019 /// NonNull attribute for function return value.
2020 struct AANonNullReturned final
2021     : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> {
2022   AANonNullReturned(const IRPosition &IRP)
2023       : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {}
2024 
2025   /// See AbstractAttribute::trackStatistics()
2026   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
2027 };
2028 
2029 /// NonNull attribute for function argument.
2030 struct AANonNullArgument final
2031     : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
2032                                                               AANonNullImpl> {
2033   AANonNullArgument(const IRPosition &IRP)
2034       : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
2035                                                                 AANonNullImpl>(
2036             IRP) {}
2037 
2038   /// See AbstractAttribute::trackStatistics()
2039   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
2040 };
2041 
2042 struct AANonNullCallSiteArgument final : AANonNullFloating {
2043   AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {}
2044 
2045   /// See AbstractAttribute::trackStatistics()
2046   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
2047 };
2048 
2049 /// NonNull attribute for a call site return position.
2050 struct AANonNullCallSiteReturned final
2051     : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
2052                                                              AANonNullImpl> {
2053   AANonNullCallSiteReturned(const IRPosition &IRP)
2054       : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
2055                                                                AANonNullImpl>(
2056             IRP) {}
2057 
2058   /// See AbstractAttribute::trackStatistics()
2059   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
2060 };
2061 
2062 /// ------------------------ No-Recurse Attributes ----------------------------
2063 
2064 struct AANoRecurseImpl : public AANoRecurse {
2065   AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {}
2066 
2067   /// See AbstractAttribute::getAsStr()
2068   const std::string getAsStr() const override {
2069     return getAssumed() ? "norecurse" : "may-recurse";
2070   }
2071 };
2072 
2073 struct AANoRecurseFunction final : AANoRecurseImpl {
2074   AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
2075 
2076   /// See AbstractAttribute::initialize(...).
2077   void initialize(Attributor &A) override {
2078     AANoRecurseImpl::initialize(A);
2079     if (const Function *F = getAnchorScope())
2080       if (A.getInfoCache().getSccSize(*F) != 1)
2081         indicatePessimisticFixpoint();
2082   }
2083 
2084   /// See AbstractAttribute::updateImpl(...).
2085   ChangeStatus updateImpl(Attributor &A) override {
2086 
2087     // If all live call sites are known to be no-recurse, we are as well.
2088     auto CallSitePred = [&](AbstractCallSite ACS) {
2089       const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
2090           *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
2091           /* TrackDependence */ false, DepClassTy::OPTIONAL);
2092       return NoRecurseAA.isKnownNoRecurse();
2093     };
2094     bool AllCallSitesKnown;
2095     if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) {
2096       // If we know all call sites and all are known no-recurse, we are done.
2097       // If all known call sites, which might not be all that exist, are known
2098       // to be no-recurse, we are not done but we can continue to assume
2099       // no-recurse. If one of the call sites we have not visited will become
2100       // live, another update is triggered.
2101       if (AllCallSitesKnown)
2102         indicateOptimisticFixpoint();
2103       return ChangeStatus::UNCHANGED;
2104     }
2105 
2106     // If the above check does not hold anymore we look at the calls.
2107     auto CheckForNoRecurse = [&](Instruction &I) {
2108       ImmutableCallSite ICS(&I);
2109       if (ICS.hasFnAttr(Attribute::NoRecurse))
2110         return true;
2111 
2112       const auto &NoRecurseAA =
2113           A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS));
2114       if (!NoRecurseAA.isAssumedNoRecurse())
2115         return false;
2116 
2117       // Recursion to the same function
2118       if (ICS.getCalledFunction() == getAnchorScope())
2119         return false;
2120 
2121       return true;
2122     };
2123 
2124     if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this))
2125       return indicatePessimisticFixpoint();
2126     return ChangeStatus::UNCHANGED;
2127   }
2128 
2129   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
2130 };
2131 
2132 /// NoRecurse attribute deduction for a call sites.
2133 struct AANoRecurseCallSite final : AANoRecurseImpl {
2134   AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
2135 
2136   /// See AbstractAttribute::initialize(...).
2137   void initialize(Attributor &A) override {
2138     AANoRecurseImpl::initialize(A);
2139     Function *F = getAssociatedFunction();
2140     if (!F)
2141       indicatePessimisticFixpoint();
2142   }
2143 
2144   /// See AbstractAttribute::updateImpl(...).
2145   ChangeStatus updateImpl(Attributor &A) override {
2146     // TODO: Once we have call site specific value information we can provide
2147     //       call site specific liveness information and then it makes
2148     //       sense to specialize attributes for call sites arguments instead of
2149     //       redirecting requests to the callee argument.
2150     Function *F = getAssociatedFunction();
2151     const IRPosition &FnPos = IRPosition::function(*F);
2152     auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos);
2153     return clampStateAndIndicateChange(
2154         getState(),
2155         static_cast<const AANoRecurse::StateType &>(FnAA.getState()));
2156   }
2157 
2158   /// See AbstractAttribute::trackStatistics()
2159   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
2160 };
2161 
2162 /// -------------------- Undefined-Behavior Attributes ------------------------
2163 
2164 struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
2165   AAUndefinedBehaviorImpl(const IRPosition &IRP) : AAUndefinedBehavior(IRP) {}
2166 
2167   /// See AbstractAttribute::updateImpl(...).
2168   // through a pointer (i.e. also branches etc.)
2169   ChangeStatus updateImpl(Attributor &A) override {
2170     const size_t UBPrevSize = KnownUBInsts.size();
2171     const size_t NoUBPrevSize = AssumedNoUBInsts.size();
2172 
2173     auto InspectMemAccessInstForUB = [&](Instruction &I) {
2174       // Skip instructions that are already saved.
2175       if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2176         return true;
2177 
2178       // If we reach here, we know we have an instruction
2179       // that accesses memory through a pointer operand,
2180       // for which getPointerOperand() should give it to us.
2181       const Value *PtrOp = getPointerOperand(&I, /* AllowVolatile */ true);
2182       assert(PtrOp &&
2183              "Expected pointer operand of memory accessing instruction");
2184 
2185       // A memory access through a pointer is considered UB
2186       // only if the pointer has constant null value.
2187       // TODO: Expand it to not only check constant values.
2188       if (!isa<ConstantPointerNull>(PtrOp)) {
2189         AssumedNoUBInsts.insert(&I);
2190         return true;
2191       }
2192       const Type *PtrTy = PtrOp->getType();
2193 
2194       // Because we only consider instructions inside functions,
2195       // assume that a parent function exists.
2196       const Function *F = I.getFunction();
2197 
2198       // A memory access using constant null pointer is only considered UB
2199       // if null pointer is _not_ defined for the target platform.
2200       if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
2201         AssumedNoUBInsts.insert(&I);
2202       else
2203         KnownUBInsts.insert(&I);
2204       return true;
2205     };
2206 
2207     auto InspectBrInstForUB = [&](Instruction &I) {
2208       // A conditional branch instruction is considered UB if it has `undef`
2209       // condition.
2210 
2211       // Skip instructions that are already saved.
2212       if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2213         return true;
2214 
2215       // We know we have a branch instruction.
2216       auto BrInst = cast<BranchInst>(&I);
2217 
2218       // Unconditional branches are never considered UB.
2219       if (BrInst->isUnconditional())
2220         return true;
2221 
2222       // Either we stopped and the appropriate action was taken,
2223       // or we got back a simplified value to continue.
2224       Optional<Value *> SimplifiedCond =
2225           stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
2226       if (!SimplifiedCond.hasValue())
2227         return true;
2228       AssumedNoUBInsts.insert(&I);
2229       return true;
2230     };
2231 
2232     A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
2233                               {Instruction::Load, Instruction::Store,
2234                                Instruction::AtomicCmpXchg,
2235                                Instruction::AtomicRMW},
2236                               /* CheckBBLivenessOnly */ true);
2237     A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
2238                               /* CheckBBLivenessOnly */ true);
2239     if (NoUBPrevSize != AssumedNoUBInsts.size() ||
2240         UBPrevSize != KnownUBInsts.size())
2241       return ChangeStatus::CHANGED;
2242     return ChangeStatus::UNCHANGED;
2243   }
2244 
2245   bool isKnownToCauseUB(Instruction *I) const override {
2246     return KnownUBInsts.count(I);
2247   }
2248 
2249   bool isAssumedToCauseUB(Instruction *I) const override {
2250     // In simple words, if an instruction is not in the assumed to _not_
2251     // cause UB, then it is assumed UB (that includes those
2252     // in the KnownUBInsts set). The rest is boilerplate
2253     // is to ensure that it is one of the instructions we test
2254     // for UB.
2255 
2256     switch (I->getOpcode()) {
2257     case Instruction::Load:
2258     case Instruction::Store:
2259     case Instruction::AtomicCmpXchg:
2260     case Instruction::AtomicRMW:
2261       return !AssumedNoUBInsts.count(I);
2262     case Instruction::Br: {
2263       auto BrInst = cast<BranchInst>(I);
2264       if (BrInst->isUnconditional())
2265         return false;
2266       return !AssumedNoUBInsts.count(I);
2267     } break;
2268     default:
2269       return false;
2270     }
2271     return false;
2272   }
2273 
2274   ChangeStatus manifest(Attributor &A) override {
2275     if (KnownUBInsts.empty())
2276       return ChangeStatus::UNCHANGED;
2277     for (Instruction *I : KnownUBInsts)
2278       A.changeToUnreachableAfterManifest(I);
2279     return ChangeStatus::CHANGED;
2280   }
2281 
2282   /// See AbstractAttribute::getAsStr()
2283   const std::string getAsStr() const override {
2284     return getAssumed() ? "undefined-behavior" : "no-ub";
2285   }
2286 
2287   /// Note: The correctness of this analysis depends on the fact that the
2288   /// following 2 sets will stop changing after some point.
2289   /// "Change" here means that their size changes.
2290   /// The size of each set is monotonically increasing
2291   /// (we only add items to them) and it is upper bounded by the number of
2292   /// instructions in the processed function (we can never save more
2293   /// elements in either set than this number). Hence, at some point,
2294   /// they will stop increasing.
2295   /// Consequently, at some point, both sets will have stopped
2296   /// changing, effectively making the analysis reach a fixpoint.
2297 
2298   /// Note: These 2 sets are disjoint and an instruction can be considered
2299   /// one of 3 things:
2300   /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
2301   ///    the KnownUBInsts set.
2302   /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
2303   ///    has a reason to assume it).
2304   /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
2305   ///    could not find a reason to assume or prove that it can cause UB,
2306   ///    hence it assumes it doesn't. We have a set for these instructions
2307   ///    so that we don't reprocess them in every update.
2308   ///    Note however that instructions in this set may cause UB.
2309 
2310 protected:
2311   /// A set of all live instructions _known_ to cause UB.
2312   SmallPtrSet<Instruction *, 8> KnownUBInsts;
2313 
2314 private:
2315   /// A set of all the (live) instructions that are assumed to _not_ cause UB.
2316   SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
2317 
2318   // Should be called on updates in which if we're processing an instruction
2319   // \p I that depends on a value \p V, one of the following has to happen:
2320   // - If the value is assumed, then stop.
2321   // - If the value is known but undef, then consider it UB.
2322   // - Otherwise, do specific processing with the simplified value.
2323   // We return None in the first 2 cases to signify that an appropriate
2324   // action was taken and the caller should stop.
2325   // Otherwise, we return the simplified value that the caller should
2326   // use for specific processing.
2327   Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V,
2328                                          Instruction *I) {
2329     const auto &ValueSimplifyAA =
2330         A.getAAFor<AAValueSimplify>(*this, IRPosition::value(*V));
2331     Optional<Value *> SimplifiedV =
2332         ValueSimplifyAA.getAssumedSimplifiedValue(A);
2333     if (!ValueSimplifyAA.isKnown()) {
2334       // Don't depend on assumed values.
2335       return llvm::None;
2336     }
2337     if (!SimplifiedV.hasValue()) {
2338       // If it is known (which we tested above) but it doesn't have a value,
2339       // then we can assume `undef` and hence the instruction is UB.
2340       KnownUBInsts.insert(I);
2341       return llvm::None;
2342     }
2343     Value *Val = SimplifiedV.getValue();
2344     if (isa<UndefValue>(Val)) {
2345       KnownUBInsts.insert(I);
2346       return llvm::None;
2347     }
2348     return Val;
2349   }
2350 };
2351 
2352 struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
2353   AAUndefinedBehaviorFunction(const IRPosition &IRP)
2354       : AAUndefinedBehaviorImpl(IRP) {}
2355 
2356   /// See AbstractAttribute::trackStatistics()
2357   void trackStatistics() const override {
2358     STATS_DECL(UndefinedBehaviorInstruction, Instruction,
2359                "Number of instructions known to have UB");
2360     BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) +=
2361         KnownUBInsts.size();
2362   }
2363 };
2364 
2365 /// ------------------------ Will-Return Attributes ----------------------------
2366 
2367 // Helper function that checks whether a function has any cycle.
2368 // TODO: Replace with more efficent code
2369 static bool containsCycle(Function &F) {
2370   SmallPtrSet<BasicBlock *, 32> Visited;
2371 
2372   // Traverse BB by dfs and check whether successor is already visited.
2373   for (BasicBlock *BB : depth_first(&F)) {
2374     Visited.insert(BB);
2375     for (auto *SuccBB : successors(BB)) {
2376       if (Visited.count(SuccBB))
2377         return true;
2378     }
2379   }
2380   return false;
2381 }
2382 
2383 // Helper function that checks the function have a loop which might become an
2384 // endless loop
2385 // FIXME: Any cycle is regarded as endless loop for now.
2386 //        We have to allow some patterns.
2387 static bool containsPossiblyEndlessLoop(Function *F) {
2388   return !F || !F->hasExactDefinition() || containsCycle(*F);
2389 }
2390 
2391 struct AAWillReturnImpl : public AAWillReturn {
2392   AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {}
2393 
2394   /// See AbstractAttribute::initialize(...).
2395   void initialize(Attributor &A) override {
2396     AAWillReturn::initialize(A);
2397 
2398     Function *F = getAssociatedFunction();
2399     if (containsPossiblyEndlessLoop(F))
2400       indicatePessimisticFixpoint();
2401   }
2402 
2403   /// See AbstractAttribute::updateImpl(...).
2404   ChangeStatus updateImpl(Attributor &A) override {
2405     auto CheckForWillReturn = [&](Instruction &I) {
2406       IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I));
2407       const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos);
2408       if (WillReturnAA.isKnownWillReturn())
2409         return true;
2410       if (!WillReturnAA.isAssumedWillReturn())
2411         return false;
2412       const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos);
2413       return NoRecurseAA.isAssumedNoRecurse();
2414     };
2415 
2416     if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this))
2417       return indicatePessimisticFixpoint();
2418 
2419     return ChangeStatus::UNCHANGED;
2420   }
2421 
2422   /// See AbstractAttribute::getAsStr()
2423   const std::string getAsStr() const override {
2424     return getAssumed() ? "willreturn" : "may-noreturn";
2425   }
2426 };
2427 
2428 struct AAWillReturnFunction final : AAWillReturnImpl {
2429   AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
2430 
2431   /// See AbstractAttribute::trackStatistics()
2432   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
2433 };
2434 
2435 /// WillReturn attribute deduction for a call sites.
2436 struct AAWillReturnCallSite final : AAWillReturnImpl {
2437   AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
2438 
2439   /// See AbstractAttribute::initialize(...).
2440   void initialize(Attributor &A) override {
2441     AAWillReturnImpl::initialize(A);
2442     Function *F = getAssociatedFunction();
2443     if (!F)
2444       indicatePessimisticFixpoint();
2445   }
2446 
2447   /// See AbstractAttribute::updateImpl(...).
2448   ChangeStatus updateImpl(Attributor &A) override {
2449     // TODO: Once we have call site specific value information we can provide
2450     //       call site specific liveness information and then it makes
2451     //       sense to specialize attributes for call sites arguments instead of
2452     //       redirecting requests to the callee argument.
2453     Function *F = getAssociatedFunction();
2454     const IRPosition &FnPos = IRPosition::function(*F);
2455     auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos);
2456     return clampStateAndIndicateChange(
2457         getState(),
2458         static_cast<const AAWillReturn::StateType &>(FnAA.getState()));
2459   }
2460 
2461   /// See AbstractAttribute::trackStatistics()
2462   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
2463 };
2464 
2465 /// -------------------AAReachability Attribute--------------------------
2466 
2467 struct AAReachabilityImpl : AAReachability {
2468   AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {}
2469 
2470   const std::string getAsStr() const override {
2471     // TODO: Return the number of reachable queries.
2472     return "reachable";
2473   }
2474 
2475   /// See AbstractAttribute::initialize(...).
2476   void initialize(Attributor &A) override { indicatePessimisticFixpoint(); }
2477 
2478   /// See AbstractAttribute::updateImpl(...).
2479   ChangeStatus updateImpl(Attributor &A) override {
2480     return indicatePessimisticFixpoint();
2481   }
2482 };
2483 
2484 struct AAReachabilityFunction final : public AAReachabilityImpl {
2485   AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {}
2486 
2487   /// See AbstractAttribute::trackStatistics()
2488   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); }
2489 };
2490 
2491 /// ------------------------ NoAlias Argument Attribute ------------------------
2492 
2493 struct AANoAliasImpl : AANoAlias {
2494   AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {}
2495 
2496   const std::string getAsStr() const override {
2497     return getAssumed() ? "noalias" : "may-alias";
2498   }
2499 };
2500 
2501 /// NoAlias attribute for a floating value.
2502 struct AANoAliasFloating final : AANoAliasImpl {
2503   AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2504 
2505   /// See AbstractAttribute::initialize(...).
2506   void initialize(Attributor &A) override {
2507     AANoAliasImpl::initialize(A);
2508     Value *Val = &getAssociatedValue();
2509     do {
2510       CastInst *CI = dyn_cast<CastInst>(Val);
2511       if (!CI)
2512         break;
2513       Value *Base = CI->getOperand(0);
2514       if (Base->getNumUses() != 1)
2515         break;
2516       Val = Base;
2517     } while (true);
2518 
2519     if (isa<AllocaInst>(Val))
2520       indicateOptimisticFixpoint();
2521     else if (isa<ConstantPointerNull>(Val) &&
2522              !NullPointerIsDefined(getAnchorScope(),
2523                                    Val->getType()->getPointerAddressSpace()))
2524       indicateOptimisticFixpoint();
2525     else if (Val != &getAssociatedValue()) {
2526       const auto &ValNoAliasAA =
2527           A.getAAFor<AANoAlias>(*this, IRPosition::value(*Val));
2528       if (ValNoAliasAA.isKnownNoAlias())
2529         indicateOptimisticFixpoint();
2530     }
2531   }
2532 
2533   /// See AbstractAttribute::updateImpl(...).
2534   ChangeStatus updateImpl(Attributor &A) override {
2535     // TODO: Implement this.
2536     return indicatePessimisticFixpoint();
2537   }
2538 
2539   /// See AbstractAttribute::trackStatistics()
2540   void trackStatistics() const override {
2541     STATS_DECLTRACK_FLOATING_ATTR(noalias)
2542   }
2543 };
2544 
2545 /// NoAlias attribute for an argument.
2546 struct AANoAliasArgument final
2547     : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
2548   using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
2549   AANoAliasArgument(const IRPosition &IRP) : Base(IRP) {}
2550 
2551   /// See AbstractAttribute::initialize(...).
2552   void initialize(Attributor &A) override {
2553     Base::initialize(A);
2554     // See callsite argument attribute and callee argument attribute.
2555     if (hasAttr({Attribute::ByVal}))
2556       indicateOptimisticFixpoint();
2557   }
2558 
2559   /// See AbstractAttribute::update(...).
2560   ChangeStatus updateImpl(Attributor &A) override {
2561     // We have to make sure no-alias on the argument does not break
2562     // synchronization when this is a callback argument, see also [1] below.
2563     // If synchronization cannot be affected, we delegate to the base updateImpl
2564     // function, otherwise we give up for now.
2565 
2566     // If the function is no-sync, no-alias cannot break synchronization.
2567     const auto &NoSyncAA = A.getAAFor<AANoSync>(
2568         *this, IRPosition::function_scope(getIRPosition()));
2569     if (NoSyncAA.isAssumedNoSync())
2570       return Base::updateImpl(A);
2571 
2572     // If the argument is read-only, no-alias cannot break synchronization.
2573     const auto &MemBehaviorAA =
2574         A.getAAFor<AAMemoryBehavior>(*this, getIRPosition());
2575     if (MemBehaviorAA.isAssumedReadOnly())
2576       return Base::updateImpl(A);
2577 
2578     // If the argument is never passed through callbacks, no-alias cannot break
2579     // synchronization.
2580     bool AllCallSitesKnown;
2581     if (A.checkForAllCallSites(
2582             [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
2583             true, AllCallSitesKnown))
2584       return Base::updateImpl(A);
2585 
2586     // TODO: add no-alias but make sure it doesn't break synchronization by
2587     // introducing fake uses. See:
2588     // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
2589     //     International Workshop on OpenMP 2018,
2590     //     http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
2591 
2592     return indicatePessimisticFixpoint();
2593   }
2594 
2595   /// See AbstractAttribute::trackStatistics()
2596   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
2597 };
2598 
2599 struct AANoAliasCallSiteArgument final : AANoAliasImpl {
2600   AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2601 
2602   /// See AbstractAttribute::initialize(...).
2603   void initialize(Attributor &A) override {
2604     // See callsite argument attribute and callee argument attribute.
2605     ImmutableCallSite ICS(&getAnchorValue());
2606     if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias))
2607       indicateOptimisticFixpoint();
2608     Value &Val = getAssociatedValue();
2609     if (isa<ConstantPointerNull>(Val) &&
2610         !NullPointerIsDefined(getAnchorScope(),
2611                               Val.getType()->getPointerAddressSpace()))
2612       indicateOptimisticFixpoint();
2613   }
2614 
2615   /// Determine if the underlying value may alias with the call site argument
2616   /// \p OtherArgNo of \p ICS (= the underlying call site).
2617   bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
2618                             const AAMemoryBehavior &MemBehaviorAA,
2619                             ImmutableCallSite ICS, unsigned OtherArgNo) {
2620     // We do not need to worry about aliasing with the underlying IRP.
2621     if (this->getArgNo() == (int)OtherArgNo)
2622       return false;
2623 
2624     // If it is not a pointer or pointer vector we do not alias.
2625     const Value *ArgOp = ICS.getArgOperand(OtherArgNo);
2626     if (!ArgOp->getType()->isPtrOrPtrVectorTy())
2627       return false;
2628 
2629     auto &ICSArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
2630         *this, IRPosition::callsite_argument(ICS, OtherArgNo),
2631         /* TrackDependence */ false);
2632 
2633     // If the argument is readnone, there is no read-write aliasing.
2634     if (ICSArgMemBehaviorAA.isAssumedReadNone()) {
2635       A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2636       return false;
2637     }
2638 
2639     // If the argument is readonly and the underlying value is readonly, there
2640     // is no read-write aliasing.
2641     bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
2642     if (ICSArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
2643       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2644       A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2645       return false;
2646     }
2647 
2648     // We have to utilize actual alias analysis queries so we need the object.
2649     if (!AAR)
2650       AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
2651 
2652     // Try to rule it out at the call site.
2653     bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
2654     LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "
2655                          "callsite arguments: "
2656                       << getAssociatedValue() << " " << *ArgOp << " => "
2657                       << (IsAliasing ? "" : "no-") << "alias \n");
2658 
2659     return IsAliasing;
2660   }
2661 
2662   bool
2663   isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
2664                                          const AAMemoryBehavior &MemBehaviorAA,
2665                                          const AANoAlias &NoAliasAA) {
2666     // We can deduce "noalias" if the following conditions hold.
2667     // (i)   Associated value is assumed to be noalias in the definition.
2668     // (ii)  Associated value is assumed to be no-capture in all the uses
2669     //       possibly executed before this callsite.
2670     // (iii) There is no other pointer argument which could alias with the
2671     //       value.
2672 
2673     bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
2674     if (!AssociatedValueIsNoAliasAtDef) {
2675       LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()
2676                         << " is not no-alias at the definition\n");
2677       return false;
2678     }
2679 
2680     const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2681     auto &NoCaptureAA =
2682         A.getAAFor<AANoCapture>(*this, VIRP, /* TrackDependence */ false);
2683     // Check whether the value is captured in the scope using AANoCapture.
2684     // FIXME: This is conservative though, it is better to look at CFG and
2685     //        check only uses possibly executed before this callsite.
2686     if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
2687       LLVM_DEBUG(
2688           dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()
2689                  << " cannot be noalias as it is potentially captured\n");
2690       return false;
2691     }
2692     A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
2693 
2694     // Check there is no other pointer argument which could alias with the
2695     // value passed at this call site.
2696     // TODO: AbstractCallSite
2697     ImmutableCallSite ICS(&getAnchorValue());
2698     for (unsigned OtherArgNo = 0; OtherArgNo < ICS.getNumArgOperands();
2699          OtherArgNo++)
2700       if (mayAliasWithArgument(A, AAR, MemBehaviorAA, ICS, OtherArgNo))
2701         return false;
2702 
2703     return true;
2704   }
2705 
2706   /// See AbstractAttribute::updateImpl(...).
2707   ChangeStatus updateImpl(Attributor &A) override {
2708     // If the argument is readnone we are done as there are no accesses via the
2709     // argument.
2710     auto &MemBehaviorAA =
2711         A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(),
2712                                      /* TrackDependence */ false);
2713     if (MemBehaviorAA.isAssumedReadNone()) {
2714       A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2715       return ChangeStatus::UNCHANGED;
2716     }
2717 
2718     const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2719     const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, VIRP,
2720                                                   /* TrackDependence */ false);
2721 
2722     AAResults *AAR = nullptr;
2723     if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
2724                                                NoAliasAA)) {
2725       LLVM_DEBUG(
2726           dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n");
2727       return ChangeStatus::UNCHANGED;
2728     }
2729 
2730     return indicatePessimisticFixpoint();
2731   }
2732 
2733   /// See AbstractAttribute::trackStatistics()
2734   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
2735 };
2736 
2737 /// NoAlias attribute for function return value.
2738 struct AANoAliasReturned final : AANoAliasImpl {
2739   AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2740 
2741   /// See AbstractAttribute::updateImpl(...).
2742   virtual ChangeStatus updateImpl(Attributor &A) override {
2743 
2744     auto CheckReturnValue = [&](Value &RV) -> bool {
2745       if (Constant *C = dyn_cast<Constant>(&RV))
2746         if (C->isNullValue() || isa<UndefValue>(C))
2747           return true;
2748 
2749       /// For now, we can only deduce noalias if we have call sites.
2750       /// FIXME: add more support.
2751       ImmutableCallSite ICS(&RV);
2752       if (!ICS)
2753         return false;
2754 
2755       const IRPosition &RVPos = IRPosition::value(RV);
2756       const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos);
2757       if (!NoAliasAA.isAssumedNoAlias())
2758         return false;
2759 
2760       const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos);
2761       return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
2762     };
2763 
2764     if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
2765       return indicatePessimisticFixpoint();
2766 
2767     return ChangeStatus::UNCHANGED;
2768   }
2769 
2770   /// See AbstractAttribute::trackStatistics()
2771   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
2772 };
2773 
2774 /// NoAlias attribute deduction for a call site return value.
2775 struct AANoAliasCallSiteReturned final : AANoAliasImpl {
2776   AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2777 
2778   /// See AbstractAttribute::initialize(...).
2779   void initialize(Attributor &A) override {
2780     AANoAliasImpl::initialize(A);
2781     Function *F = getAssociatedFunction();
2782     if (!F)
2783       indicatePessimisticFixpoint();
2784   }
2785 
2786   /// See AbstractAttribute::updateImpl(...).
2787   ChangeStatus updateImpl(Attributor &A) override {
2788     // TODO: Once we have call site specific value information we can provide
2789     //       call site specific liveness information and then it makes
2790     //       sense to specialize attributes for call sites arguments instead of
2791     //       redirecting requests to the callee argument.
2792     Function *F = getAssociatedFunction();
2793     const IRPosition &FnPos = IRPosition::returned(*F);
2794     auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos);
2795     return clampStateAndIndicateChange(
2796         getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState()));
2797   }
2798 
2799   /// See AbstractAttribute::trackStatistics()
2800   void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
2801 };
2802 
2803 /// -------------------AAIsDead Function Attribute-----------------------
2804 
2805 struct AAIsDeadValueImpl : public AAIsDead {
2806   AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {}
2807 
2808   /// See AAIsDead::isAssumedDead().
2809   bool isAssumedDead() const override { return getAssumed(); }
2810 
2811   /// See AAIsDead::isKnownDead().
2812   bool isKnownDead() const override { return getKnown(); }
2813 
2814   /// See AAIsDead::isAssumedDead(BasicBlock *).
2815   bool isAssumedDead(const BasicBlock *BB) const override { return false; }
2816 
2817   /// See AAIsDead::isKnownDead(BasicBlock *).
2818   bool isKnownDead(const BasicBlock *BB) const override { return false; }
2819 
2820   /// See AAIsDead::isAssumedDead(Instruction *I).
2821   bool isAssumedDead(const Instruction *I) const override {
2822     return I == getCtxI() && isAssumedDead();
2823   }
2824 
2825   /// See AAIsDead::isKnownDead(Instruction *I).
2826   bool isKnownDead(const Instruction *I) const override {
2827     return isAssumedDead(I) && getKnown();
2828   }
2829 
2830   /// See AbstractAttribute::getAsStr().
2831   const std::string getAsStr() const override {
2832     return isAssumedDead() ? "assumed-dead" : "assumed-live";
2833   }
2834 
2835   /// Check if all uses are assumed dead.
2836   bool areAllUsesAssumedDead(Attributor &A, Value &V) {
2837     auto UsePred = [&](const Use &U, bool &Follow) { return false; };
2838     // Explicitly set the dependence class to required because we want a long
2839     // chain of N dependent instructions to be considered live as soon as one is
2840     // without going through N update cycles. This is not required for
2841     // correctness.
2842     return A.checkForAllUses(UsePred, *this, V, DepClassTy::REQUIRED);
2843   }
2844 
2845   /// Determine if \p I is assumed to be side-effect free.
2846   bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
2847     if (!I || wouldInstructionBeTriviallyDead(I))
2848       return true;
2849 
2850     auto *CB = dyn_cast<CallBase>(I);
2851     if (!CB || isa<IntrinsicInst>(CB))
2852       return false;
2853 
2854     const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
2855     const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, CallIRP);
2856     if (!NoUnwindAA.isAssumedNoUnwind())
2857       return false;
2858 
2859     const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, CallIRP);
2860     if (!MemBehaviorAA.isAssumedReadOnly())
2861       return false;
2862 
2863     return true;
2864   }
2865 };
2866 
2867 struct AAIsDeadFloating : public AAIsDeadValueImpl {
2868   AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
2869 
2870   /// See AbstractAttribute::initialize(...).
2871   void initialize(Attributor &A) override {
2872     if (isa<UndefValue>(getAssociatedValue())) {
2873       indicatePessimisticFixpoint();
2874       return;
2875     }
2876 
2877     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
2878     if (!isAssumedSideEffectFree(A, I))
2879       indicatePessimisticFixpoint();
2880   }
2881 
2882   /// See AbstractAttribute::updateImpl(...).
2883   ChangeStatus updateImpl(Attributor &A) override {
2884     Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
2885     if (!isAssumedSideEffectFree(A, I))
2886       return indicatePessimisticFixpoint();
2887 
2888     if (!areAllUsesAssumedDead(A, getAssociatedValue()))
2889       return indicatePessimisticFixpoint();
2890     return ChangeStatus::UNCHANGED;
2891   }
2892 
2893   /// See AbstractAttribute::manifest(...).
2894   ChangeStatus manifest(Attributor &A) override {
2895     Value &V = getAssociatedValue();
2896     if (auto *I = dyn_cast<Instruction>(&V)) {
2897       // If we get here we basically know the users are all dead. We check if
2898       // isAssumedSideEffectFree returns true here again because it might not be
2899       // the case and only the users are dead but the instruction (=call) is
2900       // still needed.
2901       if (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I)) {
2902         A.deleteAfterManifest(*I);
2903         return ChangeStatus::CHANGED;
2904       }
2905     }
2906     if (V.use_empty())
2907       return ChangeStatus::UNCHANGED;
2908 
2909     bool UsedAssumedInformation = false;
2910     Optional<ConstantInt *> CI =
2911         getAssumedConstant(A, V, *this, UsedAssumedInformation);
2912     if (CI.hasValue() && CI.getValue())
2913       return ChangeStatus::UNCHANGED;
2914 
2915     UndefValue &UV = *UndefValue::get(V.getType());
2916     bool AnyChange = A.changeValueAfterManifest(V, UV);
2917     return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
2918   }
2919 
2920   /// See AbstractAttribute::trackStatistics()
2921   void trackStatistics() const override {
2922     STATS_DECLTRACK_FLOATING_ATTR(IsDead)
2923   }
2924 };
2925 
2926 struct AAIsDeadArgument : public AAIsDeadFloating {
2927   AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {}
2928 
2929   /// See AbstractAttribute::initialize(...).
2930   void initialize(Attributor &A) override {
2931     if (!getAssociatedFunction()->hasExactDefinition())
2932       indicatePessimisticFixpoint();
2933   }
2934 
2935   /// See AbstractAttribute::manifest(...).
2936   ChangeStatus manifest(Attributor &A) override {
2937     ChangeStatus Changed = AAIsDeadFloating::manifest(A);
2938     Argument &Arg = *getAssociatedArgument();
2939     if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
2940       if (A.registerFunctionSignatureRewrite(
2941               Arg, /* ReplacementTypes */ {},
2942               Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
2943               Attributor::ArgumentReplacementInfo::ACSRepairCBTy{}))
2944         return ChangeStatus::CHANGED;
2945     return Changed;
2946   }
2947 
2948   /// See AbstractAttribute::trackStatistics()
2949   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) }
2950 };
2951 
2952 struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
2953   AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
2954 
2955   /// See AbstractAttribute::initialize(...).
2956   void initialize(Attributor &A) override {
2957     if (isa<UndefValue>(getAssociatedValue()))
2958       indicatePessimisticFixpoint();
2959   }
2960 
2961   /// See AbstractAttribute::updateImpl(...).
2962   ChangeStatus updateImpl(Attributor &A) override {
2963     // TODO: Once we have call site specific value information we can provide
2964     //       call site specific liveness information and then it makes
2965     //       sense to specialize attributes for call sites arguments instead of
2966     //       redirecting requests to the callee argument.
2967     Argument *Arg = getAssociatedArgument();
2968     if (!Arg)
2969       return indicatePessimisticFixpoint();
2970     const IRPosition &ArgPos = IRPosition::argument(*Arg);
2971     auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos);
2972     return clampStateAndIndicateChange(
2973         getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState()));
2974   }
2975 
2976   /// See AbstractAttribute::manifest(...).
2977   ChangeStatus manifest(Attributor &A) override {
2978     CallBase &CB = cast<CallBase>(getAnchorValue());
2979     Use &U = CB.getArgOperandUse(getArgNo());
2980     assert(!isa<UndefValue>(U.get()) &&
2981            "Expected undef values to be filtered out!");
2982     UndefValue &UV = *UndefValue::get(U->getType());
2983     if (A.changeUseAfterManifest(U, UV))
2984       return ChangeStatus::CHANGED;
2985     return ChangeStatus::UNCHANGED;
2986   }
2987 
2988   /// See AbstractAttribute::trackStatistics()
2989   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) }
2990 };
2991 
2992 struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
2993   AAIsDeadCallSiteReturned(const IRPosition &IRP)
2994       : AAIsDeadFloating(IRP), IsAssumedSideEffectFree(true) {}
2995 
2996   /// See AAIsDead::isAssumedDead().
2997   bool isAssumedDead() const override {
2998     return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
2999   }
3000 
3001   /// See AbstractAttribute::initialize(...).
3002   void initialize(Attributor &A) override {
3003     if (isa<UndefValue>(getAssociatedValue())) {
3004       indicatePessimisticFixpoint();
3005       return;
3006     }
3007 
3008     // We track this separately as a secondary state.
3009     IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
3010   }
3011 
3012   /// See AbstractAttribute::updateImpl(...).
3013   ChangeStatus updateImpl(Attributor &A) override {
3014     ChangeStatus Changed = ChangeStatus::UNCHANGED;
3015     if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
3016       IsAssumedSideEffectFree = false;
3017       Changed = ChangeStatus::CHANGED;
3018     }
3019 
3020     if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3021       return indicatePessimisticFixpoint();
3022     return Changed;
3023   }
3024 
3025   /// See AbstractAttribute::manifest(...).
3026   ChangeStatus manifest(Attributor &A) override {
3027     if (auto *CI = dyn_cast<CallInst>(&getAssociatedValue()))
3028       if (CI->isMustTailCall())
3029         return ChangeStatus::UNCHANGED;
3030     return AAIsDeadFloating::manifest(A);
3031   }
3032 
3033   /// See AbstractAttribute::trackStatistics()
3034   void trackStatistics() const override {
3035     if (IsAssumedSideEffectFree)
3036       STATS_DECLTRACK_CSRET_ATTR(IsDead)
3037     else
3038       STATS_DECLTRACK_CSRET_ATTR(UnusedResult)
3039   }
3040 
3041   /// See AbstractAttribute::getAsStr().
3042   const std::string getAsStr() const override {
3043     return isAssumedDead()
3044                ? "assumed-dead"
3045                : (getAssumed() ? "assumed-dead-users" : "assumed-live");
3046   }
3047 
3048 private:
3049   bool IsAssumedSideEffectFree;
3050 };
3051 
3052 struct AAIsDeadReturned : public AAIsDeadValueImpl {
3053   AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
3054 
3055   /// See AbstractAttribute::updateImpl(...).
3056   ChangeStatus updateImpl(Attributor &A) override {
3057 
3058     auto PredForCallSite = [&](AbstractCallSite ACS) {
3059       if (ACS.isCallbackCall() || !ACS.getInstruction())
3060         return false;
3061       return areAllUsesAssumedDead(A, *ACS.getInstruction());
3062     };
3063 
3064     bool AllCallSitesKnown;
3065     if (!A.checkForAllCallSites(PredForCallSite, *this, true,
3066                                 AllCallSitesKnown))
3067       return indicatePessimisticFixpoint();
3068 
3069     return ChangeStatus::UNCHANGED;
3070   }
3071 
3072   /// See AbstractAttribute::manifest(...).
3073   ChangeStatus manifest(Attributor &A) override {
3074     // TODO: Rewrite the signature to return void?
3075     bool AnyChange = false;
3076     UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
3077     auto RetInstPred = [&](Instruction &I) {
3078       ReturnInst &RI = cast<ReturnInst>(I);
3079       if (auto *CI = dyn_cast<CallInst>(RI.getReturnValue()))
3080         if (CI->isMustTailCall())
3081           return true;
3082       if (!isa<UndefValue>(RI.getReturnValue()))
3083         AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
3084       return true;
3085     };
3086     A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret});
3087     return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3088   }
3089 
3090   /// See AbstractAttribute::trackStatistics()
3091   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) }
3092 };
3093 
3094 struct AAIsDeadFunction : public AAIsDead {
3095   AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {}
3096 
3097   /// See AbstractAttribute::initialize(...).
3098   void initialize(Attributor &A) override {
3099     const Function *F = getAssociatedFunction();
3100     if (F && !F->isDeclaration()) {
3101       ToBeExploredFrom.insert(&F->getEntryBlock().front());
3102       assumeLive(A, F->getEntryBlock());
3103     }
3104   }
3105 
3106   /// See AbstractAttribute::getAsStr().
3107   const std::string getAsStr() const override {
3108     return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
3109            std::to_string(getAssociatedFunction()->size()) + "][#TBEP " +
3110            std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
3111            std::to_string(KnownDeadEnds.size()) + "]";
3112   }
3113 
3114   /// See AbstractAttribute::manifest(...).
3115   ChangeStatus manifest(Attributor &A) override {
3116     assert(getState().isValidState() &&
3117            "Attempted to manifest an invalid state!");
3118 
3119     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
3120     Function &F = *getAssociatedFunction();
3121 
3122     if (AssumedLiveBlocks.empty()) {
3123       A.deleteAfterManifest(F);
3124       return ChangeStatus::CHANGED;
3125     }
3126 
3127     // Flag to determine if we can change an invoke to a call assuming the
3128     // callee is nounwind. This is not possible if the personality of the
3129     // function allows to catch asynchronous exceptions.
3130     bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
3131 
3132     KnownDeadEnds.set_union(ToBeExploredFrom);
3133     for (const Instruction *DeadEndI : KnownDeadEnds) {
3134       auto *CB = dyn_cast<CallBase>(DeadEndI);
3135       if (!CB)
3136         continue;
3137       const auto &NoReturnAA =
3138           A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB));
3139       bool MayReturn = !NoReturnAA.isAssumedNoReturn();
3140       if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
3141         continue;
3142 
3143       if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
3144         A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
3145       else
3146         A.changeToUnreachableAfterManifest(
3147             const_cast<Instruction *>(DeadEndI->getNextNode()));
3148       HasChanged = ChangeStatus::CHANGED;
3149     }
3150 
3151     for (BasicBlock &BB : F)
3152       if (!AssumedLiveBlocks.count(&BB))
3153         A.deleteAfterManifest(BB);
3154 
3155     return HasChanged;
3156   }
3157 
3158   /// See AbstractAttribute::updateImpl(...).
3159   ChangeStatus updateImpl(Attributor &A) override;
3160 
3161   /// See AbstractAttribute::trackStatistics()
3162   void trackStatistics() const override {}
3163 
3164   /// Returns true if the function is assumed dead.
3165   bool isAssumedDead() const override { return false; }
3166 
3167   /// See AAIsDead::isKnownDead().
3168   bool isKnownDead() const override { return false; }
3169 
3170   /// See AAIsDead::isAssumedDead(BasicBlock *).
3171   bool isAssumedDead(const BasicBlock *BB) const override {
3172     assert(BB->getParent() == getAssociatedFunction() &&
3173            "BB must be in the same anchor scope function.");
3174 
3175     if (!getAssumed())
3176       return false;
3177     return !AssumedLiveBlocks.count(BB);
3178   }
3179 
3180   /// See AAIsDead::isKnownDead(BasicBlock *).
3181   bool isKnownDead(const BasicBlock *BB) const override {
3182     return getKnown() && isAssumedDead(BB);
3183   }
3184 
3185   /// See AAIsDead::isAssumed(Instruction *I).
3186   bool isAssumedDead(const Instruction *I) const override {
3187     assert(I->getParent()->getParent() == getAssociatedFunction() &&
3188            "Instruction must be in the same anchor scope function.");
3189 
3190     if (!getAssumed())
3191       return false;
3192 
3193     // If it is not in AssumedLiveBlocks then it for sure dead.
3194     // Otherwise, it can still be after noreturn call in a live block.
3195     if (!AssumedLiveBlocks.count(I->getParent()))
3196       return true;
3197 
3198     // If it is not after a liveness barrier it is live.
3199     const Instruction *PrevI = I->getPrevNode();
3200     while (PrevI) {
3201       if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
3202         return true;
3203       PrevI = PrevI->getPrevNode();
3204     }
3205     return false;
3206   }
3207 
3208   /// See AAIsDead::isKnownDead(Instruction *I).
3209   bool isKnownDead(const Instruction *I) const override {
3210     return getKnown() && isAssumedDead(I);
3211   }
3212 
3213   /// Determine if \p F might catch asynchronous exceptions.
3214   static bool mayCatchAsynchronousExceptions(const Function &F) {
3215     return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
3216   }
3217 
3218   /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
3219   /// that internal function called from \p BB should now be looked at.
3220   bool assumeLive(Attributor &A, const BasicBlock &BB) {
3221     if (!AssumedLiveBlocks.insert(&BB).second)
3222       return false;
3223 
3224     // We assume that all of BB is (probably) live now and if there are calls to
3225     // internal functions we will assume that those are now live as well. This
3226     // is a performance optimization for blocks with calls to a lot of internal
3227     // functions. It can however cause dead functions to be treated as live.
3228     for (const Instruction &I : BB)
3229       if (ImmutableCallSite ICS = ImmutableCallSite(&I))
3230         if (const Function *F = ICS.getCalledFunction())
3231           if (F->hasLocalLinkage())
3232             A.markLiveInternalFunction(*F);
3233     return true;
3234   }
3235 
3236   /// Collection of instructions that need to be explored again, e.g., we
3237   /// did assume they do not transfer control to (one of their) successors.
3238   SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
3239 
3240   /// Collection of instructions that are known to not transfer control.
3241   SmallSetVector<const Instruction *, 8> KnownDeadEnds;
3242 
3243   /// Collection of all assumed live BasicBlocks.
3244   DenseSet<const BasicBlock *> AssumedLiveBlocks;
3245 };
3246 
3247 static bool
3248 identifyAliveSuccessors(Attributor &A, const CallBase &CB,
3249                         AbstractAttribute &AA,
3250                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3251   const IRPosition &IPos = IRPosition::callsite_function(CB);
3252 
3253   const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos);
3254   if (NoReturnAA.isAssumedNoReturn())
3255     return !NoReturnAA.isKnownNoReturn();
3256   if (CB.isTerminator())
3257     AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
3258   else
3259     AliveSuccessors.push_back(CB.getNextNode());
3260   return false;
3261 }
3262 
3263 static bool
3264 identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
3265                         AbstractAttribute &AA,
3266                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3267   bool UsedAssumedInformation =
3268       identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
3269 
3270   // First, determine if we can change an invoke to a call assuming the
3271   // callee is nounwind. This is not possible if the personality of the
3272   // function allows to catch asynchronous exceptions.
3273   if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
3274     AliveSuccessors.push_back(&II.getUnwindDest()->front());
3275   } else {
3276     const IRPosition &IPos = IRPosition::callsite_function(II);
3277     const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos);
3278     if (AANoUnw.isAssumedNoUnwind()) {
3279       UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
3280     } else {
3281       AliveSuccessors.push_back(&II.getUnwindDest()->front());
3282     }
3283   }
3284   return UsedAssumedInformation;
3285 }
3286 
3287 static bool
3288 identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
3289                         AbstractAttribute &AA,
3290                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3291   bool UsedAssumedInformation = false;
3292   if (BI.getNumSuccessors() == 1) {
3293     AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3294   } else {
3295     Optional<ConstantInt *> CI =
3296         getAssumedConstant(A, *BI.getCondition(), AA, UsedAssumedInformation);
3297     if (!CI.hasValue()) {
3298       // No value yet, assume both edges are dead.
3299     } else if (CI.getValue()) {
3300       const BasicBlock *SuccBB =
3301           BI.getSuccessor(1 - CI.getValue()->getZExtValue());
3302       AliveSuccessors.push_back(&SuccBB->front());
3303     } else {
3304       AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3305       AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
3306       UsedAssumedInformation = false;
3307     }
3308   }
3309   return UsedAssumedInformation;
3310 }
3311 
3312 static bool
3313 identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
3314                         AbstractAttribute &AA,
3315                         SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3316   bool UsedAssumedInformation = false;
3317   Optional<ConstantInt *> CI =
3318       getAssumedConstant(A, *SI.getCondition(), AA, UsedAssumedInformation);
3319   if (!CI.hasValue()) {
3320     // No value yet, assume all edges are dead.
3321   } else if (CI.getValue()) {
3322     for (auto &CaseIt : SI.cases()) {
3323       if (CaseIt.getCaseValue() == CI.getValue()) {
3324         AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
3325         return UsedAssumedInformation;
3326       }
3327     }
3328     AliveSuccessors.push_back(&SI.getDefaultDest()->front());
3329     return UsedAssumedInformation;
3330   } else {
3331     for (const BasicBlock *SuccBB : successors(SI.getParent()))
3332       AliveSuccessors.push_back(&SuccBB->front());
3333   }
3334   return UsedAssumedInformation;
3335 }
3336 
3337 ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
3338   ChangeStatus Change = ChangeStatus::UNCHANGED;
3339 
3340   LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"
3341                     << getAssociatedFunction()->size() << "] BBs and "
3342                     << ToBeExploredFrom.size() << " exploration points and "
3343                     << KnownDeadEnds.size() << " known dead ends\n");
3344 
3345   // Copy and clear the list of instructions we need to explore from. It is
3346   // refilled with instructions the next update has to look at.
3347   SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
3348                                                ToBeExploredFrom.end());
3349   decltype(ToBeExploredFrom) NewToBeExploredFrom;
3350 
3351   SmallVector<const Instruction *, 8> AliveSuccessors;
3352   while (!Worklist.empty()) {
3353     const Instruction *I = Worklist.pop_back_val();
3354     LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n");
3355 
3356     AliveSuccessors.clear();
3357 
3358     bool UsedAssumedInformation = false;
3359     switch (I->getOpcode()) {
3360     // TODO: look for (assumed) UB to backwards propagate "deadness".
3361     default:
3362       if (I->isTerminator()) {
3363         for (const BasicBlock *SuccBB : successors(I->getParent()))
3364           AliveSuccessors.push_back(&SuccBB->front());
3365       } else {
3366         AliveSuccessors.push_back(I->getNextNode());
3367       }
3368       break;
3369     case Instruction::Call:
3370       UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
3371                                                        *this, AliveSuccessors);
3372       break;
3373     case Instruction::Invoke:
3374       UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
3375                                                        *this, AliveSuccessors);
3376       break;
3377     case Instruction::Br:
3378       UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
3379                                                        *this, AliveSuccessors);
3380       break;
3381     case Instruction::Switch:
3382       UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
3383                                                        *this, AliveSuccessors);
3384       break;
3385     }
3386 
3387     if (UsedAssumedInformation) {
3388       NewToBeExploredFrom.insert(I);
3389     } else {
3390       Change = ChangeStatus::CHANGED;
3391       if (AliveSuccessors.empty() ||
3392           (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors()))
3393         KnownDeadEnds.insert(I);
3394     }
3395 
3396     LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "
3397                       << AliveSuccessors.size() << " UsedAssumedInformation: "
3398                       << UsedAssumedInformation << "\n");
3399 
3400     for (const Instruction *AliveSuccessor : AliveSuccessors) {
3401       if (!I->isTerminator()) {
3402         assert(AliveSuccessors.size() == 1 &&
3403                "Non-terminator expected to have a single successor!");
3404         Worklist.push_back(AliveSuccessor);
3405       } else {
3406         if (assumeLive(A, *AliveSuccessor->getParent()))
3407           Worklist.push_back(AliveSuccessor);
3408       }
3409     }
3410   }
3411 
3412   ToBeExploredFrom = std::move(NewToBeExploredFrom);
3413 
3414   // If we know everything is live there is no need to query for liveness.
3415   // Instead, indicating a pessimistic fixpoint will cause the state to be
3416   // "invalid" and all queries to be answered conservatively without lookups.
3417   // To be in this state we have to (1) finished the exploration and (3) not
3418   // discovered any non-trivial dead end and (2) not ruled unreachable code
3419   // dead.
3420   if (ToBeExploredFrom.empty() &&
3421       getAssociatedFunction()->size() == AssumedLiveBlocks.size() &&
3422       llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
3423         return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
3424       }))
3425     return indicatePessimisticFixpoint();
3426   return Change;
3427 }
3428 
3429 /// Liveness information for a call sites.
3430 struct AAIsDeadCallSite final : AAIsDeadFunction {
3431   AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {}
3432 
3433   /// See AbstractAttribute::initialize(...).
3434   void initialize(Attributor &A) override {
3435     // TODO: Once we have call site specific value information we can provide
3436     //       call site specific liveness information and then it makes
3437     //       sense to specialize attributes for call sites instead of
3438     //       redirecting requests to the callee.
3439     llvm_unreachable("Abstract attributes for liveness are not "
3440                      "supported for call sites yet!");
3441   }
3442 
3443   /// See AbstractAttribute::updateImpl(...).
3444   ChangeStatus updateImpl(Attributor &A) override {
3445     return indicatePessimisticFixpoint();
3446   }
3447 
3448   /// See AbstractAttribute::trackStatistics()
3449   void trackStatistics() const override {}
3450 };
3451 
3452 /// -------------------- Dereferenceable Argument Attribute --------------------
3453 
3454 template <>
3455 ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
3456                                                      const DerefState &R) {
3457   ChangeStatus CS0 =
3458       clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
3459   ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
3460   return CS0 | CS1;
3461 }
3462 
3463 struct AADereferenceableImpl : AADereferenceable {
3464   AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {}
3465   using StateType = DerefState;
3466 
3467   void initialize(Attributor &A) override {
3468     SmallVector<Attribute, 4> Attrs;
3469     getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
3470              Attrs);
3471     for (const Attribute &Attr : Attrs)
3472       takeKnownDerefBytesMaximum(Attr.getValueAsInt());
3473 
3474     NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition(),
3475                                        /* TrackDependence */ false);
3476 
3477     const IRPosition &IRP = this->getIRPosition();
3478     bool IsFnInterface = IRP.isFnInterfaceKind();
3479     const Function *FnScope = IRP.getAnchorScope();
3480     if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition()))
3481       indicatePessimisticFixpoint();
3482   }
3483 
3484   /// See AbstractAttribute::getState()
3485   /// {
3486   StateType &getState() override { return *this; }
3487   const StateType &getState() const override { return *this; }
3488   /// }
3489 
3490   /// Helper function for collecting accessed bytes in must-be-executed-context
3491   void addAccessedBytesForUse(Attributor &A, const Use *U,
3492                               const Instruction *I) {
3493     const Value *UseV = U->get();
3494     if (!UseV->getType()->isPointerTy())
3495       return;
3496 
3497     Type *PtrTy = UseV->getType();
3498     const DataLayout &DL = A.getDataLayout();
3499     int64_t Offset;
3500     if (const Value *Base = getBasePointerOfAccessPointerOperand(
3501             I, Offset, DL, /*AllowNonInbounds*/ true)) {
3502       if (Base == &getAssociatedValue() &&
3503           getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
3504         uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType());
3505         addAccessedBytes(Offset, Size);
3506       }
3507     }
3508     return;
3509   }
3510 
3511   /// See AAFromMustBeExecutedContext
3512   bool followUse(Attributor &A, const Use *U, const Instruction *I) {
3513     bool IsNonNull = false;
3514     bool TrackUse = false;
3515     int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
3516         A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
3517 
3518     addAccessedBytesForUse(A, U, I);
3519     takeKnownDerefBytesMaximum(DerefBytes);
3520     return TrackUse;
3521   }
3522 
3523   /// See AbstractAttribute::manifest(...).
3524   ChangeStatus manifest(Attributor &A) override {
3525     ChangeStatus Change = AADereferenceable::manifest(A);
3526     if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
3527       removeAttrs({Attribute::DereferenceableOrNull});
3528       return ChangeStatus::CHANGED;
3529     }
3530     return Change;
3531   }
3532 
3533   void getDeducedAttributes(LLVMContext &Ctx,
3534                             SmallVectorImpl<Attribute> &Attrs) const override {
3535     // TODO: Add *_globally support
3536     if (isAssumedNonNull())
3537       Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
3538           Ctx, getAssumedDereferenceableBytes()));
3539     else
3540       Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
3541           Ctx, getAssumedDereferenceableBytes()));
3542   }
3543 
3544   /// See AbstractAttribute::getAsStr().
3545   const std::string getAsStr() const override {
3546     if (!getAssumedDereferenceableBytes())
3547       return "unknown-dereferenceable";
3548     return std::string("dereferenceable") +
3549            (isAssumedNonNull() ? "" : "_or_null") +
3550            (isAssumedGlobal() ? "_globally" : "") + "<" +
3551            std::to_string(getKnownDereferenceableBytes()) + "-" +
3552            std::to_string(getAssumedDereferenceableBytes()) + ">";
3553   }
3554 };
3555 
3556 /// Dereferenceable attribute for a floating value.
3557 struct AADereferenceableFloating
3558     : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> {
3559   using Base =
3560       AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>;
3561   AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {}
3562 
3563   /// See AbstractAttribute::updateImpl(...).
3564   ChangeStatus updateImpl(Attributor &A) override {
3565     ChangeStatus Change = Base::updateImpl(A);
3566 
3567     const DataLayout &DL = A.getDataLayout();
3568 
3569     auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool {
3570       unsigned IdxWidth =
3571           DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
3572       APInt Offset(IdxWidth, 0);
3573       const Value *Base =
3574           V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
3575 
3576       const auto &AA =
3577           A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base));
3578       int64_t DerefBytes = 0;
3579       if (!Stripped && this == &AA) {
3580         // Use IR information if we did not strip anything.
3581         // TODO: track globally.
3582         bool CanBeNull;
3583         DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull);
3584         T.GlobalState.indicatePessimisticFixpoint();
3585       } else {
3586         const DerefState &DS = static_cast<const DerefState &>(AA.getState());
3587         DerefBytes = DS.DerefBytesState.getAssumed();
3588         T.GlobalState &= DS.GlobalState;
3589       }
3590 
3591       // TODO: Use `AAConstantRange` to infer dereferenceable bytes.
3592 
3593       // For now we do not try to "increase" dereferenceability due to negative
3594       // indices as we first have to come up with code to deal with loops and
3595       // for overflows of the dereferenceable bytes.
3596       int64_t OffsetSExt = Offset.getSExtValue();
3597       if (OffsetSExt < 0)
3598         OffsetSExt = 0;
3599 
3600       T.takeAssumedDerefBytesMinimum(
3601           std::max(int64_t(0), DerefBytes - OffsetSExt));
3602 
3603       if (this == &AA) {
3604         if (!Stripped) {
3605           // If nothing was stripped IR information is all we got.
3606           T.takeKnownDerefBytesMaximum(
3607               std::max(int64_t(0), DerefBytes - OffsetSExt));
3608           T.indicatePessimisticFixpoint();
3609         } else if (OffsetSExt > 0) {
3610           // If something was stripped but there is circular reasoning we look
3611           // for the offset. If it is positive we basically decrease the
3612           // dereferenceable bytes in a circluar loop now, which will simply
3613           // drive them down to the known value in a very slow way which we
3614           // can accelerate.
3615           T.indicatePessimisticFixpoint();
3616         }
3617       }
3618 
3619       return T.isValidState();
3620     };
3621 
3622     DerefState T;
3623     if (!genericValueTraversal<AADereferenceable, DerefState>(
3624             A, getIRPosition(), *this, T, VisitValueCB))
3625       return indicatePessimisticFixpoint();
3626 
3627     return Change | clampStateAndIndicateChange(getState(), T);
3628   }
3629 
3630   /// See AbstractAttribute::trackStatistics()
3631   void trackStatistics() const override {
3632     STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
3633   }
3634 };
3635 
3636 /// Dereferenceable attribute for a return value.
3637 struct AADereferenceableReturned final
3638     : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
3639   AADereferenceableReturned(const IRPosition &IRP)
3640       : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
3641             IRP) {}
3642 
3643   /// See AbstractAttribute::trackStatistics()
3644   void trackStatistics() const override {
3645     STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
3646   }
3647 };
3648 
3649 /// Dereferenceable attribute for an argument
3650 struct AADereferenceableArgument final
3651     : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
3652           AADereferenceable, AADereferenceableImpl> {
3653   using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
3654       AADereferenceable, AADereferenceableImpl>;
3655   AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {}
3656 
3657   /// See AbstractAttribute::trackStatistics()
3658   void trackStatistics() const override {
3659     STATS_DECLTRACK_ARG_ATTR(dereferenceable)
3660   }
3661 };
3662 
3663 /// Dereferenceable attribute for a call site argument.
3664 struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
3665   AADereferenceableCallSiteArgument(const IRPosition &IRP)
3666       : AADereferenceableFloating(IRP) {}
3667 
3668   /// See AbstractAttribute::trackStatistics()
3669   void trackStatistics() const override {
3670     STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
3671   }
3672 };
3673 
3674 /// Dereferenceable attribute deduction for a call site return value.
3675 struct AADereferenceableCallSiteReturned final
3676     : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
3677           AADereferenceable, AADereferenceableImpl> {
3678   using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
3679       AADereferenceable, AADereferenceableImpl>;
3680   AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {}
3681 
3682   /// See AbstractAttribute::trackStatistics()
3683   void trackStatistics() const override {
3684     STATS_DECLTRACK_CS_ATTR(dereferenceable);
3685   }
3686 };
3687 
3688 // ------------------------ Align Argument Attribute ------------------------
3689 
3690 static unsigned int getKnownAlignForUse(Attributor &A,
3691                                         AbstractAttribute &QueryingAA,
3692                                         Value &AssociatedValue, const Use *U,
3693                                         const Instruction *I, bool &TrackUse) {
3694   // We need to follow common pointer manipulation uses to the accesses they
3695   // feed into.
3696   if (isa<CastInst>(I)) {
3697     // Follow all but ptr2int casts.
3698     TrackUse = !isa<PtrToIntInst>(I);
3699     return 0;
3700   }
3701   if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
3702     if (GEP->hasAllConstantIndices()) {
3703       TrackUse = true;
3704       return 0;
3705     }
3706   }
3707 
3708   unsigned Alignment = 0;
3709   if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
3710     if (ICS.isBundleOperand(U) || ICS.isCallee(U))
3711       return 0;
3712 
3713     unsigned ArgNo = ICS.getArgumentNo(U);
3714     IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo);
3715     // As long as we only use known information there is no need to track
3716     // dependences here.
3717     auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP,
3718                                         /* TrackDependence */ false);
3719     Alignment = AlignAA.getKnownAlign();
3720   }
3721 
3722   const Value *UseV = U->get();
3723   if (auto *SI = dyn_cast<StoreInst>(I)) {
3724     if (SI->getPointerOperand() == UseV)
3725       Alignment = SI->getAlignment();
3726   } else if (auto *LI = dyn_cast<LoadInst>(I))
3727     Alignment = LI->getAlignment();
3728 
3729   if (Alignment <= 1)
3730     return 0;
3731 
3732   auto &DL = A.getDataLayout();
3733   int64_t Offset;
3734 
3735   if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
3736     if (Base == &AssociatedValue) {
3737       // BasePointerAddr + Offset = Alignment * Q for some integer Q.
3738       // So we can say that the maximum power of two which is a divisor of
3739       // gcd(Offset, Alignment) is an alignment.
3740 
3741       uint32_t gcd =
3742           greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
3743       Alignment = llvm::PowerOf2Floor(gcd);
3744     }
3745   }
3746 
3747   return Alignment;
3748 }
3749 struct AAAlignImpl : AAAlign {
3750   AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {}
3751 
3752   /// See AbstractAttribute::initialize(...).
3753   void initialize(Attributor &A) override {
3754     SmallVector<Attribute, 4> Attrs;
3755     getAttrs({Attribute::Alignment}, Attrs);
3756     for (const Attribute &Attr : Attrs)
3757       takeKnownMaximum(Attr.getValueAsInt());
3758 
3759     if (getIRPosition().isFnInterfaceKind() &&
3760         (!getAssociatedFunction() ||
3761          !getAssociatedFunction()->hasExactDefinition()))
3762       indicatePessimisticFixpoint();
3763   }
3764 
3765   /// See AbstractAttribute::manifest(...).
3766   ChangeStatus manifest(Attributor &A) override {
3767     ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
3768 
3769     // Check for users that allow alignment annotations.
3770     Value &AnchorVal = getIRPosition().getAnchorValue();
3771     for (const Use &U : AnchorVal.uses()) {
3772       if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
3773         if (SI->getPointerOperand() == &AnchorVal)
3774           if (SI->getAlignment() < getAssumedAlign()) {
3775             STATS_DECLTRACK(AAAlign, Store,
3776                             "Number of times alignment added to a store");
3777             SI->setAlignment(Align(getAssumedAlign()));
3778             LoadStoreChanged = ChangeStatus::CHANGED;
3779           }
3780       } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
3781         if (LI->getPointerOperand() == &AnchorVal)
3782           if (LI->getAlignment() < getAssumedAlign()) {
3783             LI->setAlignment(Align(getAssumedAlign()));
3784             STATS_DECLTRACK(AAAlign, Load,
3785                             "Number of times alignment added to a load");
3786             LoadStoreChanged = ChangeStatus::CHANGED;
3787           }
3788       }
3789     }
3790 
3791     ChangeStatus Changed = AAAlign::manifest(A);
3792 
3793     MaybeAlign InheritAlign =
3794         getAssociatedValue().getPointerAlignment(A.getDataLayout());
3795     if (InheritAlign.valueOrOne() >= getAssumedAlign())
3796       return LoadStoreChanged;
3797     return Changed | LoadStoreChanged;
3798   }
3799 
3800   // TODO: Provide a helper to determine the implied ABI alignment and check in
3801   //       the existing manifest method and a new one for AAAlignImpl that value
3802   //       to avoid making the alignment explicit if it did not improve.
3803 
3804   /// See AbstractAttribute::getDeducedAttributes
3805   virtual void
3806   getDeducedAttributes(LLVMContext &Ctx,
3807                        SmallVectorImpl<Attribute> &Attrs) const override {
3808     if (getAssumedAlign() > 1)
3809       Attrs.emplace_back(
3810           Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
3811   }
3812   /// See AAFromMustBeExecutedContext
3813   bool followUse(Attributor &A, const Use *U, const Instruction *I) {
3814     bool TrackUse = false;
3815 
3816     unsigned int KnownAlign =
3817         getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
3818     takeKnownMaximum(KnownAlign);
3819 
3820     return TrackUse;
3821   }
3822 
3823   /// See AbstractAttribute::getAsStr().
3824   const std::string getAsStr() const override {
3825     return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
3826                                 "-" + std::to_string(getAssumedAlign()) + ">")
3827                              : "unknown-align";
3828   }
3829 };
3830 
3831 /// Align attribute for a floating value.
3832 struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> {
3833   using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>;
3834   AAAlignFloating(const IRPosition &IRP) : Base(IRP) {}
3835 
3836   /// See AbstractAttribute::updateImpl(...).
3837   ChangeStatus updateImpl(Attributor &A) override {
3838     Base::updateImpl(A);
3839 
3840     const DataLayout &DL = A.getDataLayout();
3841 
3842     auto VisitValueCB = [&](Value &V, AAAlign::StateType &T,
3843                             bool Stripped) -> bool {
3844       const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V));
3845       if (!Stripped && this == &AA) {
3846         // Use only IR information if we did not strip anything.
3847         const MaybeAlign PA = V.getPointerAlignment(DL);
3848         T.takeKnownMaximum(PA ? PA->value() : 0);
3849         T.indicatePessimisticFixpoint();
3850       } else {
3851         // Use abstract attribute information.
3852         const AAAlign::StateType &DS =
3853             static_cast<const AAAlign::StateType &>(AA.getState());
3854         T ^= DS;
3855       }
3856       return T.isValidState();
3857     };
3858 
3859     StateType T;
3860     if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T,
3861                                                    VisitValueCB))
3862       return indicatePessimisticFixpoint();
3863 
3864     // TODO: If we know we visited all incoming values, thus no are assumed
3865     // dead, we can take the known information from the state T.
3866     return clampStateAndIndicateChange(getState(), T);
3867   }
3868 
3869   /// See AbstractAttribute::trackStatistics()
3870   void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
3871 };
3872 
3873 /// Align attribute for function return value.
3874 struct AAAlignReturned final
3875     : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
3876   AAAlignReturned(const IRPosition &IRP)
3877       : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {}
3878 
3879   /// See AbstractAttribute::trackStatistics()
3880   void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
3881 };
3882 
3883 /// Align attribute for function argument.
3884 struct AAAlignArgument final
3885     : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign,
3886                                                               AAAlignImpl> {
3887   AAAlignArgument(const IRPosition &IRP)
3888       : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign,
3889                                                                 AAAlignImpl>(
3890             IRP) {}
3891 
3892   /// See AbstractAttribute::trackStatistics()
3893   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
3894 };
3895 
3896 struct AAAlignCallSiteArgument final : AAAlignFloating {
3897   AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {}
3898 
3899   /// See AbstractAttribute::manifest(...).
3900   ChangeStatus manifest(Attributor &A) override {
3901     ChangeStatus Changed = AAAlignImpl::manifest(A);
3902     MaybeAlign InheritAlign =
3903         getAssociatedValue().getPointerAlignment(A.getDataLayout());
3904     if (InheritAlign.valueOrOne() >= getAssumedAlign())
3905       Changed = ChangeStatus::UNCHANGED;
3906     return Changed;
3907   }
3908 
3909   /// See AbstractAttribute::updateImpl(Attributor &A).
3910   ChangeStatus updateImpl(Attributor &A) override {
3911     ChangeStatus Changed = AAAlignFloating::updateImpl(A);
3912     if (Argument *Arg = getAssociatedArgument()) {
3913       // We only take known information from the argument
3914       // so we do not need to track a dependence.
3915       const auto &ArgAlignAA = A.getAAFor<AAAlign>(
3916           *this, IRPosition::argument(*Arg), /* TrackDependence */ false);
3917       takeKnownMaximum(ArgAlignAA.getKnownAlign());
3918     }
3919     return Changed;
3920   }
3921 
3922   /// See AbstractAttribute::trackStatistics()
3923   void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
3924 };
3925 
3926 /// Align attribute deduction for a call site return value.
3927 struct AAAlignCallSiteReturned final
3928     : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign,
3929                                                              AAAlignImpl> {
3930   using Base =
3931       AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign,
3932                                                              AAAlignImpl>;
3933   AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {}
3934 
3935   /// See AbstractAttribute::initialize(...).
3936   void initialize(Attributor &A) override {
3937     Base::initialize(A);
3938     Function *F = getAssociatedFunction();
3939     if (!F)
3940       indicatePessimisticFixpoint();
3941   }
3942 
3943   /// See AbstractAttribute::trackStatistics()
3944   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
3945 };
3946 
3947 /// ------------------ Function No-Return Attribute ----------------------------
3948 struct AANoReturnImpl : public AANoReturn {
3949   AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {}
3950 
3951   /// See AbstractAttribute::initialize(...).
3952   void initialize(Attributor &A) override {
3953     AANoReturn::initialize(A);
3954     Function *F = getAssociatedFunction();
3955     if (!F)
3956       indicatePessimisticFixpoint();
3957   }
3958 
3959   /// See AbstractAttribute::getAsStr().
3960   const std::string getAsStr() const override {
3961     return getAssumed() ? "noreturn" : "may-return";
3962   }
3963 
3964   /// See AbstractAttribute::updateImpl(Attributor &A).
3965   virtual ChangeStatus updateImpl(Attributor &A) override {
3966     auto CheckForNoReturn = [](Instruction &) { return false; };
3967     if (!A.checkForAllInstructions(CheckForNoReturn, *this,
3968                                    {(unsigned)Instruction::Ret}))
3969       return indicatePessimisticFixpoint();
3970     return ChangeStatus::UNCHANGED;
3971   }
3972 };
3973 
3974 struct AANoReturnFunction final : AANoReturnImpl {
3975   AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
3976 
3977   /// See AbstractAttribute::trackStatistics()
3978   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
3979 };
3980 
3981 /// NoReturn attribute deduction for a call sites.
3982 struct AANoReturnCallSite final : AANoReturnImpl {
3983   AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
3984 
3985   /// See AbstractAttribute::updateImpl(...).
3986   ChangeStatus updateImpl(Attributor &A) override {
3987     // TODO: Once we have call site specific value information we can provide
3988     //       call site specific liveness information and then it makes
3989     //       sense to specialize attributes for call sites arguments instead of
3990     //       redirecting requests to the callee argument.
3991     Function *F = getAssociatedFunction();
3992     const IRPosition &FnPos = IRPosition::function(*F);
3993     auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos);
3994     return clampStateAndIndicateChange(
3995         getState(),
3996         static_cast<const AANoReturn::StateType &>(FnAA.getState()));
3997   }
3998 
3999   /// See AbstractAttribute::trackStatistics()
4000   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
4001 };
4002 
4003 /// ----------------------- Variable Capturing ---------------------------------
4004 
4005 /// A class to hold the state of for no-capture attributes.
4006 struct AANoCaptureImpl : public AANoCapture {
4007   AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {}
4008 
4009   /// See AbstractAttribute::initialize(...).
4010   void initialize(Attributor &A) override {
4011     if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
4012       indicateOptimisticFixpoint();
4013       return;
4014     }
4015     Function *AnchorScope = getAnchorScope();
4016     if (isFnInterfaceKind() &&
4017         (!AnchorScope || !AnchorScope->hasExactDefinition())) {
4018       indicatePessimisticFixpoint();
4019       return;
4020     }
4021 
4022     // You cannot "capture" null in the default address space.
4023     if (isa<ConstantPointerNull>(getAssociatedValue()) &&
4024         getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
4025       indicateOptimisticFixpoint();
4026       return;
4027     }
4028 
4029     const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope;
4030 
4031     // Check what state the associated function can actually capture.
4032     if (F)
4033       determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
4034     else
4035       indicatePessimisticFixpoint();
4036   }
4037 
4038   /// See AbstractAttribute::updateImpl(...).
4039   ChangeStatus updateImpl(Attributor &A) override;
4040 
4041   /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
4042   virtual void
4043   getDeducedAttributes(LLVMContext &Ctx,
4044                        SmallVectorImpl<Attribute> &Attrs) const override {
4045     if (!isAssumedNoCaptureMaybeReturned())
4046       return;
4047 
4048     if (getArgNo() >= 0) {
4049       if (isAssumedNoCapture())
4050         Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
4051       else if (ManifestInternal)
4052         Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
4053     }
4054   }
4055 
4056   /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
4057   /// depending on the ability of the function associated with \p IRP to capture
4058   /// state in memory and through "returning/throwing", respectively.
4059   static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
4060                                                    const Function &F,
4061                                                    BitIntegerState &State) {
4062     // TODO: Once we have memory behavior attributes we should use them here.
4063 
4064     // If we know we cannot communicate or write to memory, we do not care about
4065     // ptr2int anymore.
4066     if (F.onlyReadsMemory() && F.doesNotThrow() &&
4067         F.getReturnType()->isVoidTy()) {
4068       State.addKnownBits(NO_CAPTURE);
4069       return;
4070     }
4071 
4072     // A function cannot capture state in memory if it only reads memory, it can
4073     // however return/throw state and the state might be influenced by the
4074     // pointer value, e.g., loading from a returned pointer might reveal a bit.
4075     if (F.onlyReadsMemory())
4076       State.addKnownBits(NOT_CAPTURED_IN_MEM);
4077 
4078     // A function cannot communicate state back if it does not through
4079     // exceptions and doesn not return values.
4080     if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
4081       State.addKnownBits(NOT_CAPTURED_IN_RET);
4082 
4083     // Check existing "returned" attributes.
4084     int ArgNo = IRP.getArgNo();
4085     if (F.doesNotThrow() && ArgNo >= 0) {
4086       for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
4087         if (F.hasParamAttribute(u, Attribute::Returned)) {
4088           if (u == unsigned(ArgNo))
4089             State.removeAssumedBits(NOT_CAPTURED_IN_RET);
4090           else if (F.onlyReadsMemory())
4091             State.addKnownBits(NO_CAPTURE);
4092           else
4093             State.addKnownBits(NOT_CAPTURED_IN_RET);
4094           break;
4095         }
4096     }
4097   }
4098 
4099   /// See AbstractState::getAsStr().
4100   const std::string getAsStr() const override {
4101     if (isKnownNoCapture())
4102       return "known not-captured";
4103     if (isAssumedNoCapture())
4104       return "assumed not-captured";
4105     if (isKnownNoCaptureMaybeReturned())
4106       return "known not-captured-maybe-returned";
4107     if (isAssumedNoCaptureMaybeReturned())
4108       return "assumed not-captured-maybe-returned";
4109     return "assumed-captured";
4110   }
4111 };
4112 
4113 /// Attributor-aware capture tracker.
4114 struct AACaptureUseTracker final : public CaptureTracker {
4115 
4116   /// Create a capture tracker that can lookup in-flight abstract attributes
4117   /// through the Attributor \p A.
4118   ///
4119   /// If a use leads to a potential capture, \p CapturedInMemory is set and the
4120   /// search is stopped. If a use leads to a return instruction,
4121   /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
4122   /// If a use leads to a ptr2int which may capture the value,
4123   /// \p CapturedInInteger is set. If a use is found that is currently assumed
4124   /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
4125   /// set. All values in \p PotentialCopies are later tracked as well. For every
4126   /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
4127   /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
4128   /// conservatively set to true.
4129   AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
4130                       const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
4131                       SmallVectorImpl<const Value *> &PotentialCopies,
4132                       unsigned &RemainingUsesToExplore)
4133       : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
4134         PotentialCopies(PotentialCopies),
4135         RemainingUsesToExplore(RemainingUsesToExplore) {}
4136 
4137   /// Determine if \p V maybe captured. *Also updates the state!*
4138   bool valueMayBeCaptured(const Value *V) {
4139     if (V->getType()->isPointerTy()) {
4140       PointerMayBeCaptured(V, this);
4141     } else {
4142       State.indicatePessimisticFixpoint();
4143     }
4144     return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4145   }
4146 
4147   /// See CaptureTracker::tooManyUses().
4148   void tooManyUses() override {
4149     State.removeAssumedBits(AANoCapture::NO_CAPTURE);
4150   }
4151 
4152   bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
4153     if (CaptureTracker::isDereferenceableOrNull(O, DL))
4154       return true;
4155     const auto &DerefAA = A.getAAFor<AADereferenceable>(
4156         NoCaptureAA, IRPosition::value(*O), /* TrackDependence */ true,
4157         DepClassTy::OPTIONAL);
4158     return DerefAA.getAssumedDereferenceableBytes();
4159   }
4160 
4161   /// See CaptureTracker::captured(...).
4162   bool captured(const Use *U) override {
4163     Instruction *UInst = cast<Instruction>(U->getUser());
4164     LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
4165                       << "\n");
4166 
4167     // Because we may reuse the tracker multiple times we keep track of the
4168     // number of explored uses ourselves as well.
4169     if (RemainingUsesToExplore-- == 0) {
4170       LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
4171       return isCapturedIn(/* Memory */ true, /* Integer */ true,
4172                           /* Return */ true);
4173     }
4174 
4175     // Deal with ptr2int by following uses.
4176     if (isa<PtrToIntInst>(UInst)) {
4177       LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
4178       return valueMayBeCaptured(UInst);
4179     }
4180 
4181     // Explicitly catch return instructions.
4182     if (isa<ReturnInst>(UInst))
4183       return isCapturedIn(/* Memory */ false, /* Integer */ false,
4184                           /* Return */ true);
4185 
4186     // For now we only use special logic for call sites. However, the tracker
4187     // itself knows about a lot of other non-capturing cases already.
4188     CallSite CS(UInst);
4189     if (!CS || !CS.isArgOperand(U))
4190       return isCapturedIn(/* Memory */ true, /* Integer */ true,
4191                           /* Return */ true);
4192 
4193     unsigned ArgNo = CS.getArgumentNo(U);
4194     const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
4195     // If we have a abstract no-capture attribute for the argument we can use
4196     // it to justify a non-capture attribute here. This allows recursion!
4197     auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos);
4198     if (ArgNoCaptureAA.isAssumedNoCapture())
4199       return isCapturedIn(/* Memory */ false, /* Integer */ false,
4200                           /* Return */ false);
4201     if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
4202       addPotentialCopy(CS);
4203       return isCapturedIn(/* Memory */ false, /* Integer */ false,
4204                           /* Return */ false);
4205     }
4206 
4207     // Lastly, we could not find a reason no-capture can be assumed so we don't.
4208     return isCapturedIn(/* Memory */ true, /* Integer */ true,
4209                         /* Return */ true);
4210   }
4211 
4212   /// Register \p CS as potential copy of the value we are checking.
4213   void addPotentialCopy(CallSite CS) {
4214     PotentialCopies.push_back(CS.getInstruction());
4215   }
4216 
4217   /// See CaptureTracker::shouldExplore(...).
4218   bool shouldExplore(const Use *U) override {
4219     // Check liveness.
4220     return !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA);
4221   }
4222 
4223   /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
4224   /// \p CapturedInRet, then return the appropriate value for use in the
4225   /// CaptureTracker::captured() interface.
4226   bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
4227                     bool CapturedInRet) {
4228     LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
4229                       << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
4230     if (CapturedInMem)
4231       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
4232     if (CapturedInInt)
4233       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
4234     if (CapturedInRet)
4235       State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
4236     return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4237   }
4238 
4239 private:
4240   /// The attributor providing in-flight abstract attributes.
4241   Attributor &A;
4242 
4243   /// The abstract attribute currently updated.
4244   AANoCapture &NoCaptureAA;
4245 
4246   /// The abstract liveness state.
4247   const AAIsDead &IsDeadAA;
4248 
4249   /// The state currently updated.
4250   AANoCapture::StateType &State;
4251 
4252   /// Set of potential copies of the tracked value.
4253   SmallVectorImpl<const Value *> &PotentialCopies;
4254 
4255   /// Global counter to limit the number of explored uses.
4256   unsigned &RemainingUsesToExplore;
4257 };
4258 
4259 ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
4260   const IRPosition &IRP = getIRPosition();
4261   const Value *V =
4262       getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue();
4263   if (!V)
4264     return indicatePessimisticFixpoint();
4265 
4266   const Function *F =
4267       getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
4268   assert(F && "Expected a function!");
4269   const IRPosition &FnPos = IRPosition::function(*F);
4270   const auto &IsDeadAA =
4271       A.getAAFor<AAIsDead>(*this, FnPos, /* TrackDependence */ false);
4272 
4273   AANoCapture::StateType T;
4274 
4275   // Readonly means we cannot capture through memory.
4276   const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(
4277       *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
4278   if (FnMemAA.isAssumedReadOnly()) {
4279     T.addKnownBits(NOT_CAPTURED_IN_MEM);
4280     if (FnMemAA.isKnownReadOnly())
4281       addKnownBits(NOT_CAPTURED_IN_MEM);
4282   }
4283 
4284   // Make sure all returned values are different than the underlying value.
4285   // TODO: we could do this in a more sophisticated way inside
4286   //       AAReturnedValues, e.g., track all values that escape through returns
4287   //       directly somehow.
4288   auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
4289     bool SeenConstant = false;
4290     for (auto &It : RVAA.returned_values()) {
4291       if (isa<Constant>(It.first)) {
4292         if (SeenConstant)
4293           return false;
4294         SeenConstant = true;
4295       } else if (!isa<Argument>(It.first) ||
4296                  It.first == getAssociatedArgument())
4297         return false;
4298     }
4299     return true;
4300   };
4301 
4302   const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
4303       *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
4304   if (NoUnwindAA.isAssumedNoUnwind()) {
4305     bool IsVoidTy = F->getReturnType()->isVoidTy();
4306     const AAReturnedValues *RVAA =
4307         IsVoidTy ? nullptr
4308                  : &A.getAAFor<AAReturnedValues>(*this, FnPos,
4309                                                  /* TrackDependence */ true,
4310                                                  DepClassTy::OPTIONAL);
4311     if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
4312       T.addKnownBits(NOT_CAPTURED_IN_RET);
4313       if (T.isKnown(NOT_CAPTURED_IN_MEM))
4314         return ChangeStatus::UNCHANGED;
4315       if (NoUnwindAA.isKnownNoUnwind() &&
4316           (IsVoidTy || RVAA->getState().isAtFixpoint())) {
4317         addKnownBits(NOT_CAPTURED_IN_RET);
4318         if (isKnown(NOT_CAPTURED_IN_MEM))
4319           return indicateOptimisticFixpoint();
4320       }
4321     }
4322   }
4323 
4324   // Use the CaptureTracker interface and logic with the specialized tracker,
4325   // defined in AACaptureUseTracker, that can look at in-flight abstract
4326   // attributes and directly updates the assumed state.
4327   SmallVector<const Value *, 4> PotentialCopies;
4328   unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore;
4329   AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
4330                               RemainingUsesToExplore);
4331 
4332   // Check all potential copies of the associated value until we can assume
4333   // none will be captured or we have to assume at least one might be.
4334   unsigned Idx = 0;
4335   PotentialCopies.push_back(V);
4336   while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
4337     Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
4338 
4339   AANoCapture::StateType &S = getState();
4340   auto Assumed = S.getAssumed();
4341   S.intersectAssumedBits(T.getAssumed());
4342   if (!isAssumedNoCaptureMaybeReturned())
4343     return indicatePessimisticFixpoint();
4344   return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
4345                                    : ChangeStatus::CHANGED;
4346 }
4347 
4348 /// NoCapture attribute for function arguments.
4349 struct AANoCaptureArgument final : AANoCaptureImpl {
4350   AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4351 
4352   /// See AbstractAttribute::trackStatistics()
4353   void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
4354 };
4355 
4356 /// NoCapture attribute for call site arguments.
4357 struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
4358   AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4359 
4360   /// See AbstractAttribute::initialize(...).
4361   void initialize(Attributor &A) override {
4362     if (Argument *Arg = getAssociatedArgument())
4363       if (Arg->hasByValAttr())
4364         indicateOptimisticFixpoint();
4365     AANoCaptureImpl::initialize(A);
4366   }
4367 
4368   /// See AbstractAttribute::updateImpl(...).
4369   ChangeStatus updateImpl(Attributor &A) override {
4370     // TODO: Once we have call site specific value information we can provide
4371     //       call site specific liveness information and then it makes
4372     //       sense to specialize attributes for call sites arguments instead of
4373     //       redirecting requests to the callee argument.
4374     Argument *Arg = getAssociatedArgument();
4375     if (!Arg)
4376       return indicatePessimisticFixpoint();
4377     const IRPosition &ArgPos = IRPosition::argument(*Arg);
4378     auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos);
4379     return clampStateAndIndicateChange(
4380         getState(),
4381         static_cast<const AANoCapture::StateType &>(ArgAA.getState()));
4382   }
4383 
4384   /// See AbstractAttribute::trackStatistics()
4385   void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
4386 };
4387 
4388 /// NoCapture attribute for floating values.
4389 struct AANoCaptureFloating final : AANoCaptureImpl {
4390   AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4391 
4392   /// See AbstractAttribute::trackStatistics()
4393   void trackStatistics() const override {
4394     STATS_DECLTRACK_FLOATING_ATTR(nocapture)
4395   }
4396 };
4397 
4398 /// NoCapture attribute for function return value.
4399 struct AANoCaptureReturned final : AANoCaptureImpl {
4400   AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {
4401     llvm_unreachable("NoCapture is not applicable to function returns!");
4402   }
4403 
4404   /// See AbstractAttribute::initialize(...).
4405   void initialize(Attributor &A) override {
4406     llvm_unreachable("NoCapture is not applicable to function returns!");
4407   }
4408 
4409   /// See AbstractAttribute::updateImpl(...).
4410   ChangeStatus updateImpl(Attributor &A) override {
4411     llvm_unreachable("NoCapture is not applicable to function returns!");
4412   }
4413 
4414   /// See AbstractAttribute::trackStatistics()
4415   void trackStatistics() const override {}
4416 };
4417 
4418 /// NoCapture attribute deduction for a call site return value.
4419 struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
4420   AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4421 
4422   /// See AbstractAttribute::trackStatistics()
4423   void trackStatistics() const override {
4424     STATS_DECLTRACK_CSRET_ATTR(nocapture)
4425   }
4426 };
4427 
4428 /// ------------------ Value Simplify Attribute ----------------------------
4429 struct AAValueSimplifyImpl : AAValueSimplify {
4430   AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {}
4431 
4432   /// See AbstractAttribute::initialize(...).
4433   void initialize(Attributor &A) override {
4434     if (getAssociatedValue().getType()->isVoidTy())
4435       indicatePessimisticFixpoint();
4436   }
4437 
4438   /// See AbstractAttribute::getAsStr().
4439   const std::string getAsStr() const override {
4440     return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple")
4441                         : "not-simple";
4442   }
4443 
4444   /// See AbstractAttribute::trackStatistics()
4445   void trackStatistics() const override {}
4446 
4447   /// See AAValueSimplify::getAssumedSimplifiedValue()
4448   Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
4449     if (!getAssumed())
4450       return const_cast<Value *>(&getAssociatedValue());
4451     return SimplifiedAssociatedValue;
4452   }
4453 
4454   /// Helper function for querying AAValueSimplify and updating candicate.
4455   /// \param QueryingValue Value trying to unify with SimplifiedValue
4456   /// \param AccumulatedSimplifiedValue Current simplification result.
4457   static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
4458                              Value &QueryingValue,
4459                              Optional<Value *> &AccumulatedSimplifiedValue) {
4460     // FIXME: Add a typecast support.
4461 
4462     auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
4463         QueryingAA, IRPosition::value(QueryingValue));
4464 
4465     Optional<Value *> QueryingValueSimplified =
4466         ValueSimplifyAA.getAssumedSimplifiedValue(A);
4467 
4468     if (!QueryingValueSimplified.hasValue())
4469       return true;
4470 
4471     if (!QueryingValueSimplified.getValue())
4472       return false;
4473 
4474     Value &QueryingValueSimplifiedUnwrapped =
4475         *QueryingValueSimplified.getValue();
4476 
4477     if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
4478       return true;
4479 
4480     if (AccumulatedSimplifiedValue.hasValue())
4481       return AccumulatedSimplifiedValue == QueryingValueSimplified;
4482 
4483     LLVM_DEBUG(dbgs() << "[ValueSimplify] " << QueryingValue
4484                       << " is assumed to be "
4485                       << QueryingValueSimplifiedUnwrapped << "\n");
4486 
4487     AccumulatedSimplifiedValue = QueryingValueSimplified;
4488     return true;
4489   }
4490 
4491   bool askSimplifiedValueForAAValueConstantRange(Attributor &A) {
4492     if (!getAssociatedValue().getType()->isIntegerTy())
4493       return false;
4494 
4495     const auto &ValueConstantRangeAA =
4496         A.getAAFor<AAValueConstantRange>(*this, getIRPosition());
4497 
4498     Optional<ConstantInt *> COpt =
4499         ValueConstantRangeAA.getAssumedConstantInt(A);
4500     if (COpt.hasValue()) {
4501       if (auto *C = COpt.getValue())
4502         SimplifiedAssociatedValue = C;
4503       else
4504         return false;
4505     } else {
4506       SimplifiedAssociatedValue = llvm::None;
4507     }
4508     return true;
4509   }
4510 
4511   /// See AbstractAttribute::manifest(...).
4512   ChangeStatus manifest(Attributor &A) override {
4513     ChangeStatus Changed = ChangeStatus::UNCHANGED;
4514 
4515     if (!SimplifiedAssociatedValue.hasValue() ||
4516         !SimplifiedAssociatedValue.getValue())
4517       return Changed;
4518 
4519     if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) {
4520       // We can replace the AssociatedValue with the constant.
4521       Value &V = getAssociatedValue();
4522       if (!V.user_empty() && &V != C && V.getType() == C->getType()) {
4523         LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C
4524                           << " :: " << *this << "\n");
4525         if (A.changeValueAfterManifest(V, *C))
4526           Changed = ChangeStatus::CHANGED;
4527       }
4528     }
4529 
4530     return Changed | AAValueSimplify::manifest(A);
4531   }
4532 
4533   /// See AbstractState::indicatePessimisticFixpoint(...).
4534   ChangeStatus indicatePessimisticFixpoint() override {
4535     // NOTE: Associated value will be returned in a pessimistic fixpoint and is
4536     // regarded as known. That's why`indicateOptimisticFixpoint` is called.
4537     SimplifiedAssociatedValue = &getAssociatedValue();
4538     indicateOptimisticFixpoint();
4539     return ChangeStatus::CHANGED;
4540   }
4541 
4542 protected:
4543   // An assumed simplified value. Initially, it is set to Optional::None, which
4544   // means that the value is not clear under current assumption. If in the
4545   // pessimistic state, getAssumedSimplifiedValue doesn't return this value but
4546   // returns orignal associated value.
4547   Optional<Value *> SimplifiedAssociatedValue;
4548 };
4549 
4550 struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
4551   AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4552 
4553   void initialize(Attributor &A) override {
4554     AAValueSimplifyImpl::initialize(A);
4555     if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration())
4556       indicatePessimisticFixpoint();
4557     if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest},
4558                 /* IgnoreSubsumingPositions */ true))
4559       indicatePessimisticFixpoint();
4560   }
4561 
4562   /// See AbstractAttribute::updateImpl(...).
4563   ChangeStatus updateImpl(Attributor &A) override {
4564     // Byval is only replacable if it is readonly otherwise we would write into
4565     // the replaced value and not the copy that byval creates implicitly.
4566     Argument *Arg = getAssociatedArgument();
4567     if (Arg->hasByValAttr()) {
4568       const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition());
4569       if (!MemAA.isAssumedReadOnly())
4570         return indicatePessimisticFixpoint();
4571     }
4572 
4573     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4574 
4575     auto PredForCallSite = [&](AbstractCallSite ACS) {
4576       // Check if we have an associated argument or not (which can happen for
4577       // callback calls).
4578       Value *ArgOp = ACS.getCallArgOperand(getArgNo());
4579       if (!ArgOp)
4580         return false;
4581       // We can only propagate thread independent values through callbacks.
4582       // This is different to direct/indirect call sites because for them we
4583       // know the thread executing the caller and callee is the same. For
4584       // callbacks this is not guaranteed, thus a thread dependent value could
4585       // be different for the caller and callee, making it invalid to propagate.
4586       if (ACS.isCallbackCall())
4587         if (auto *C = dyn_cast<Constant>(ArgOp))
4588           if (C->isThreadDependent())
4589             return false;
4590       return checkAndUpdate(A, *this, *ArgOp, SimplifiedAssociatedValue);
4591     };
4592 
4593     bool AllCallSitesKnown;
4594     if (!A.checkForAllCallSites(PredForCallSite, *this, true,
4595                                 AllCallSitesKnown))
4596       if (!askSimplifiedValueForAAValueConstantRange(A))
4597         return indicatePessimisticFixpoint();
4598 
4599     // If a candicate was found in this update, return CHANGED.
4600     return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4601                ? ChangeStatus::UNCHANGED
4602                : ChangeStatus ::CHANGED;
4603   }
4604 
4605   /// See AbstractAttribute::trackStatistics()
4606   void trackStatistics() const override {
4607     STATS_DECLTRACK_ARG_ATTR(value_simplify)
4608   }
4609 };
4610 
4611 struct AAValueSimplifyReturned : AAValueSimplifyImpl {
4612   AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4613 
4614   /// See AbstractAttribute::updateImpl(...).
4615   ChangeStatus updateImpl(Attributor &A) override {
4616     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4617 
4618     auto PredForReturned = [&](Value &V) {
4619       return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4620     };
4621 
4622     if (!A.checkForAllReturnedValues(PredForReturned, *this))
4623       if (!askSimplifiedValueForAAValueConstantRange(A))
4624         return indicatePessimisticFixpoint();
4625 
4626     // If a candicate was found in this update, return CHANGED.
4627     return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4628                ? ChangeStatus::UNCHANGED
4629                : ChangeStatus ::CHANGED;
4630   }
4631   /// See AbstractAttribute::trackStatistics()
4632   void trackStatistics() const override {
4633     STATS_DECLTRACK_FNRET_ATTR(value_simplify)
4634   }
4635 };
4636 
4637 struct AAValueSimplifyFloating : AAValueSimplifyImpl {
4638   AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4639 
4640   /// See AbstractAttribute::initialize(...).
4641   void initialize(Attributor &A) override {
4642     Value &V = getAnchorValue();
4643 
4644     // TODO: add other stuffs
4645     if (isa<Constant>(V))
4646       indicatePessimisticFixpoint();
4647   }
4648 
4649   /// See AbstractAttribute::updateImpl(...).
4650   ChangeStatus updateImpl(Attributor &A) override {
4651     bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4652 
4653     auto VisitValueCB = [&](Value &V, bool &, bool Stripped) -> bool {
4654       auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V));
4655       if (!Stripped && this == &AA) {
4656         // TODO: Look the instruction and check recursively.
4657 
4658         LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V
4659                           << "\n");
4660         return false;
4661       }
4662       return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4663     };
4664 
4665     bool Dummy = false;
4666     if (!genericValueTraversal<AAValueSimplify, bool>(A, getIRPosition(), *this,
4667                                                       Dummy, VisitValueCB))
4668       if (!askSimplifiedValueForAAValueConstantRange(A))
4669         return indicatePessimisticFixpoint();
4670 
4671     // If a candicate was found in this update, return CHANGED.
4672 
4673     return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4674                ? ChangeStatus::UNCHANGED
4675                : ChangeStatus ::CHANGED;
4676   }
4677 
4678   /// See AbstractAttribute::trackStatistics()
4679   void trackStatistics() const override {
4680     STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
4681   }
4682 };
4683 
4684 struct AAValueSimplifyFunction : AAValueSimplifyImpl {
4685   AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4686 
4687   /// See AbstractAttribute::initialize(...).
4688   void initialize(Attributor &A) override {
4689     SimplifiedAssociatedValue = &getAnchorValue();
4690     indicateOptimisticFixpoint();
4691   }
4692   /// See AbstractAttribute::initialize(...).
4693   ChangeStatus updateImpl(Attributor &A) override {
4694     llvm_unreachable(
4695         "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
4696   }
4697   /// See AbstractAttribute::trackStatistics()
4698   void trackStatistics() const override {
4699     STATS_DECLTRACK_FN_ATTR(value_simplify)
4700   }
4701 };
4702 
4703 struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
4704   AAValueSimplifyCallSite(const IRPosition &IRP)
4705       : AAValueSimplifyFunction(IRP) {}
4706   /// See AbstractAttribute::trackStatistics()
4707   void trackStatistics() const override {
4708     STATS_DECLTRACK_CS_ATTR(value_simplify)
4709   }
4710 };
4711 
4712 struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned {
4713   AAValueSimplifyCallSiteReturned(const IRPosition &IRP)
4714       : AAValueSimplifyReturned(IRP) {}
4715 
4716   /// See AbstractAttribute::manifest(...).
4717   ChangeStatus manifest(Attributor &A) override {
4718     if (auto *CI = dyn_cast<CallInst>(&getAssociatedValue()))
4719       if (CI->isMustTailCall())
4720         return ChangeStatus::UNCHANGED;
4721     return AAValueSimplifyReturned::manifest(A);
4722   }
4723 
4724   void trackStatistics() const override {
4725     STATS_DECLTRACK_CSRET_ATTR(value_simplify)
4726   }
4727 };
4728 struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
4729   AAValueSimplifyCallSiteArgument(const IRPosition &IRP)
4730       : AAValueSimplifyFloating(IRP) {}
4731 
4732   void trackStatistics() const override {
4733     STATS_DECLTRACK_CSARG_ATTR(value_simplify)
4734   }
4735 };
4736 
4737 /// ----------------------- Heap-To-Stack Conversion ---------------------------
4738 struct AAHeapToStackImpl : public AAHeapToStack {
4739   AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {}
4740 
4741   const std::string getAsStr() const override {
4742     return "[H2S] Mallocs: " + std::to_string(MallocCalls.size());
4743   }
4744 
4745   ChangeStatus manifest(Attributor &A) override {
4746     assert(getState().isValidState() &&
4747            "Attempted to manifest an invalid state!");
4748 
4749     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
4750     Function *F = getAssociatedFunction();
4751     const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
4752 
4753     for (Instruction *MallocCall : MallocCalls) {
4754       // This malloc cannot be replaced.
4755       if (BadMallocCalls.count(MallocCall))
4756         continue;
4757 
4758       for (Instruction *FreeCall : FreesForMalloc[MallocCall]) {
4759         LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
4760         A.deleteAfterManifest(*FreeCall);
4761         HasChanged = ChangeStatus::CHANGED;
4762       }
4763 
4764       LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall
4765                         << "\n");
4766 
4767       Constant *Size;
4768       if (isCallocLikeFn(MallocCall, TLI)) {
4769         auto *Num = cast<ConstantInt>(MallocCall->getOperand(0));
4770         auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1));
4771         APInt TotalSize = SizeT->getValue() * Num->getValue();
4772         Size =
4773             ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize);
4774       } else {
4775         Size = cast<ConstantInt>(MallocCall->getOperand(0));
4776       }
4777 
4778       unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace();
4779       Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
4780                                        Size, "", MallocCall->getNextNode());
4781 
4782       if (AI->getType() != MallocCall->getType())
4783         AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc",
4784                              AI->getNextNode());
4785 
4786       A.changeValueAfterManifest(*MallocCall, *AI);
4787 
4788       if (auto *II = dyn_cast<InvokeInst>(MallocCall)) {
4789         auto *NBB = II->getNormalDest();
4790         BranchInst::Create(NBB, MallocCall->getParent());
4791         A.deleteAfterManifest(*MallocCall);
4792       } else {
4793         A.deleteAfterManifest(*MallocCall);
4794       }
4795 
4796       if (isCallocLikeFn(MallocCall, TLI)) {
4797         auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc",
4798                                    AI->getNextNode());
4799         Value *Ops[] = {
4800             BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size,
4801             ConstantInt::get(Type::getInt1Ty(F->getContext()), false)};
4802 
4803         Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()};
4804         Module *M = F->getParent();
4805         Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
4806         CallInst::Create(Fn, Ops, "", BI->getNextNode());
4807       }
4808       HasChanged = ChangeStatus::CHANGED;
4809     }
4810 
4811     return HasChanged;
4812   }
4813 
4814   /// Collection of all malloc calls in a function.
4815   SmallSetVector<Instruction *, 4> MallocCalls;
4816 
4817   /// Collection of malloc calls that cannot be converted.
4818   DenseSet<const Instruction *> BadMallocCalls;
4819 
4820   /// A map for each malloc call to the set of associated free calls.
4821   DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc;
4822 
4823   ChangeStatus updateImpl(Attributor &A) override;
4824 };
4825 
4826 ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
4827   const Function *F = getAssociatedFunction();
4828   const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
4829 
4830   MustBeExecutedContextExplorer &Explorer =
4831       A.getInfoCache().getMustBeExecutedContextExplorer();
4832 
4833   auto FreeCheck = [&](Instruction &I) {
4834     const auto &Frees = FreesForMalloc.lookup(&I);
4835     if (Frees.size() != 1)
4836       return false;
4837     Instruction *UniqueFree = *Frees.begin();
4838     return Explorer.findInContextOf(UniqueFree, I.getNextNode());
4839   };
4840 
4841   auto UsesCheck = [&](Instruction &I) {
4842     bool ValidUsesOnly = true;
4843     bool MustUse = true;
4844     auto Pred = [&](const Use &U, bool &Follow) -> bool {
4845       Instruction *UserI = cast<Instruction>(U.getUser());
4846       if (isa<LoadInst>(UserI))
4847         return true;
4848       if (auto *SI = dyn_cast<StoreInst>(UserI)) {
4849         if (SI->getValueOperand() == U.get()) {
4850           LLVM_DEBUG(dbgs()
4851                      << "[H2S] escaping store to memory: " << *UserI << "\n");
4852           ValidUsesOnly = false;
4853         } else {
4854           // A store into the malloc'ed memory is fine.
4855         }
4856         return true;
4857       }
4858       if (auto *CB = dyn_cast<CallBase>(UserI)) {
4859         if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
4860           return true;
4861         // Record malloc.
4862         if (isFreeCall(UserI, TLI)) {
4863           if (MustUse) {
4864             FreesForMalloc[&I].insert(UserI);
4865           } else {
4866             LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: "
4867                               << *UserI << "\n");
4868             ValidUsesOnly = false;
4869           }
4870           return true;
4871         }
4872 
4873         unsigned ArgNo = CB->getArgOperandNo(&U);
4874 
4875         const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
4876             *this, IRPosition::callsite_argument(*CB, ArgNo));
4877 
4878         // If a callsite argument use is nofree, we are fine.
4879         const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
4880             *this, IRPosition::callsite_argument(*CB, ArgNo));
4881 
4882         if (!NoCaptureAA.isAssumedNoCapture() ||
4883             !ArgNoFreeAA.isAssumedNoFree()) {
4884           LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
4885           ValidUsesOnly = false;
4886         }
4887         return true;
4888       }
4889 
4890       if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
4891           isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
4892         MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI));
4893         Follow = true;
4894         return true;
4895       }
4896       // Unknown user for which we can not track uses further (in a way that
4897       // makes sense).
4898       LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
4899       ValidUsesOnly = false;
4900       return true;
4901     };
4902     A.checkForAllUses(Pred, *this, I);
4903     return ValidUsesOnly;
4904   };
4905 
4906   auto MallocCallocCheck = [&](Instruction &I) {
4907     if (BadMallocCalls.count(&I))
4908       return true;
4909 
4910     bool IsMalloc = isMallocLikeFn(&I, TLI);
4911     bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI);
4912     if (!IsMalloc && !IsCalloc) {
4913       BadMallocCalls.insert(&I);
4914       return true;
4915     }
4916 
4917     if (IsMalloc) {
4918       if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
4919         if (Size->getValue().ule(MaxHeapToStackSize))
4920           if (UsesCheck(I) || FreeCheck(I)) {
4921             MallocCalls.insert(&I);
4922             return true;
4923           }
4924     } else if (IsCalloc) {
4925       bool Overflow = false;
4926       if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
4927         if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
4928           if ((Size->getValue().umul_ov(Num->getValue(), Overflow))
4929                   .ule(MaxHeapToStackSize))
4930             if (!Overflow && (UsesCheck(I) || FreeCheck(I))) {
4931               MallocCalls.insert(&I);
4932               return true;
4933             }
4934     }
4935 
4936     BadMallocCalls.insert(&I);
4937     return true;
4938   };
4939 
4940   size_t NumBadMallocs = BadMallocCalls.size();
4941 
4942   A.checkForAllCallLikeInstructions(MallocCallocCheck, *this);
4943 
4944   if (NumBadMallocs != BadMallocCalls.size())
4945     return ChangeStatus::CHANGED;
4946 
4947   return ChangeStatus::UNCHANGED;
4948 }
4949 
4950 struct AAHeapToStackFunction final : public AAHeapToStackImpl {
4951   AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {}
4952 
4953   /// See AbstractAttribute::trackStatistics()
4954   void trackStatistics() const override {
4955     STATS_DECL(MallocCalls, Function,
4956                "Number of malloc calls converted to allocas");
4957     for (auto *C : MallocCalls)
4958       if (!BadMallocCalls.count(C))
4959         ++BUILD_STAT_NAME(MallocCalls, Function);
4960   }
4961 };
4962 
4963 /// ----------------------- Privatizable Pointers ------------------------------
4964 struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
4965   AAPrivatizablePtrImpl(const IRPosition &IRP)
4966       : AAPrivatizablePtr(IRP), PrivatizableType(llvm::None) {}
4967 
4968   ChangeStatus indicatePessimisticFixpoint() override {
4969     AAPrivatizablePtr::indicatePessimisticFixpoint();
4970     PrivatizableType = nullptr;
4971     return ChangeStatus::CHANGED;
4972   }
4973 
4974   /// Identify the type we can chose for a private copy of the underlying
4975   /// argument. None means it is not clear yet, nullptr means there is none.
4976   virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
4977 
4978   /// Return a privatizable type that encloses both T0 and T1.
4979   /// TODO: This is merely a stub for now as we should manage a mapping as well.
4980   Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
4981     if (!T0.hasValue())
4982       return T1;
4983     if (!T1.hasValue())
4984       return T0;
4985     if (T0 == T1)
4986       return T0;
4987     return nullptr;
4988   }
4989 
4990   Optional<Type *> getPrivatizableType() const override {
4991     return PrivatizableType;
4992   }
4993 
4994   const std::string getAsStr() const override {
4995     return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
4996   }
4997 
4998 protected:
4999   Optional<Type *> PrivatizableType;
5000 };
5001 
5002 // TODO: Do this for call site arguments (probably also other values) as well.
5003 
5004 struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
5005   AAPrivatizablePtrArgument(const IRPosition &IRP)
5006       : AAPrivatizablePtrImpl(IRP) {}
5007 
5008   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
5009   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
5010     // If this is a byval argument and we know all the call sites (so we can
5011     // rewrite them), there is no need to check them explicitly.
5012     bool AllCallSitesKnown;
5013     if (getIRPosition().hasAttr(Attribute::ByVal) &&
5014         A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
5015                                true, AllCallSitesKnown))
5016       return getAssociatedValue().getType()->getPointerElementType();
5017 
5018     Optional<Type *> Ty;
5019     unsigned ArgNo = getIRPosition().getArgNo();
5020 
5021     // Make sure the associated call site argument has the same type at all call
5022     // sites and it is an allocation we know is safe to privatize, for now that
5023     // means we only allow alloca instructions.
5024     // TODO: We can additionally analyze the accesses in the callee to  create
5025     //       the type from that information instead. That is a little more
5026     //       involved and will be done in a follow up patch.
5027     auto CallSiteCheck = [&](AbstractCallSite ACS) {
5028       IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
5029       // Check if a coresponding argument was found or if it is one not
5030       // associated (which can happen for callback calls).
5031       if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
5032         return false;
5033 
5034       // Check that all call sites agree on a type.
5035       auto &PrivCSArgAA = A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos);
5036       Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
5037 
5038       LLVM_DEBUG({
5039         dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
5040         if (CSTy.hasValue() && CSTy.getValue())
5041           CSTy.getValue()->print(dbgs());
5042         else if (CSTy.hasValue())
5043           dbgs() << "<nullptr>";
5044         else
5045           dbgs() << "<none>";
5046       });
5047 
5048       Ty = combineTypes(Ty, CSTy);
5049 
5050       LLVM_DEBUG({
5051         dbgs() << " : New Type: ";
5052         if (Ty.hasValue() && Ty.getValue())
5053           Ty.getValue()->print(dbgs());
5054         else if (Ty.hasValue())
5055           dbgs() << "<nullptr>";
5056         else
5057           dbgs() << "<none>";
5058         dbgs() << "\n";
5059       });
5060 
5061       return !Ty.hasValue() || Ty.getValue();
5062     };
5063 
5064     if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown))
5065       return nullptr;
5066     return Ty;
5067   }
5068 
5069   /// See AbstractAttribute::updateImpl(...).
5070   ChangeStatus updateImpl(Attributor &A) override {
5071     PrivatizableType = identifyPrivatizableType(A);
5072     if (!PrivatizableType.hasValue())
5073       return ChangeStatus::UNCHANGED;
5074     if (!PrivatizableType.getValue())
5075       return indicatePessimisticFixpoint();
5076 
5077     // Avoid arguments with padding for now.
5078     if (!getIRPosition().hasAttr(Attribute::ByVal) &&
5079         !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
5080                                                 A.getInfoCache().getDL())) {
5081       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
5082       return indicatePessimisticFixpoint();
5083     }
5084 
5085     // Verify callee and caller agree on how the promoted argument would be
5086     // passed.
5087     // TODO: The use of the ArgumentPromotion interface here is ugly, we need a
5088     // specialized form of TargetTransformInfo::areFunctionArgsABICompatible
5089     // which doesn't require the arguments ArgumentPromotion wanted to pass.
5090     Function &Fn = *getIRPosition().getAnchorScope();
5091     SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy;
5092     ArgsToPromote.insert(getAssociatedArgument());
5093     const auto *TTI =
5094         A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
5095     if (!TTI ||
5096         !ArgumentPromotionPass::areFunctionArgsABICompatible(
5097             Fn, *TTI, ArgsToPromote, Dummy) ||
5098         ArgsToPromote.empty()) {
5099       LLVM_DEBUG(
5100           dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
5101                  << Fn.getName() << "\n");
5102       return indicatePessimisticFixpoint();
5103     }
5104 
5105     // Collect the types that will replace the privatizable type in the function
5106     // signature.
5107     SmallVector<Type *, 16> ReplacementTypes;
5108     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5109 
5110     // Register a rewrite of the argument.
5111     Argument *Arg = getAssociatedArgument();
5112     if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
5113       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
5114       return indicatePessimisticFixpoint();
5115     }
5116 
5117     unsigned ArgNo = Arg->getArgNo();
5118 
5119     // Helper to check if for the given call site the associated argument is
5120     // passed to a callback where the privatization would be different.
5121     auto IsCompatiblePrivArgOfCallback = [&](CallSite CS) {
5122       SmallVector<const Use *, 4> CBUses;
5123       AbstractCallSite::getCallbackUses(CS, CBUses);
5124       for (const Use *U : CBUses) {
5125         AbstractCallSite CBACS(U);
5126         assert(CBACS && CBACS.isCallbackCall());
5127         for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
5128           int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
5129 
5130           LLVM_DEBUG({
5131             dbgs()
5132                 << "[AAPrivatizablePtr] Argument " << *Arg
5133                 << "check if can be privatized in the context of its parent ("
5134                 << Arg->getParent()->getName()
5135                 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5136                    "callback ("
5137                 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5138                 << ")\n[AAPrivatizablePtr] " << CBArg << " : "
5139                 << CBACS.getCallArgOperand(CBArg) << " vs "
5140                 << CS.getArgOperand(ArgNo) << "\n"
5141                 << "[AAPrivatizablePtr] " << CBArg << " : "
5142                 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
5143           });
5144 
5145           if (CBArgNo != int(ArgNo))
5146             continue;
5147           const auto &CBArgPrivAA =
5148               A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(CBArg));
5149           if (CBArgPrivAA.isValidState()) {
5150             auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
5151             if (!CBArgPrivTy.hasValue())
5152               continue;
5153             if (CBArgPrivTy.getValue() == PrivatizableType)
5154               continue;
5155           }
5156 
5157           LLVM_DEBUG({
5158             dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5159                    << " cannot be privatized in the context of its parent ("
5160                    << Arg->getParent()->getName()
5161                    << ")\n[AAPrivatizablePtr] because it is an argument in a "
5162                       "callback ("
5163                    << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5164                    << ").\n[AAPrivatizablePtr] for which the argument "
5165                       "privatization is not compatible.\n";
5166           });
5167           return false;
5168         }
5169       }
5170       return true;
5171     };
5172 
5173     // Helper to check if for the given call site the associated argument is
5174     // passed to a direct call where the privatization would be different.
5175     auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
5176       CallBase *DC = cast<CallBase>(ACS.getInstruction());
5177       int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
5178       assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() &&
5179              "Expected a direct call operand for callback call operand");
5180 
5181       LLVM_DEBUG({
5182         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5183                << " check if be privatized in the context of its parent ("
5184                << Arg->getParent()->getName()
5185                << ")\n[AAPrivatizablePtr] because it is an argument in a "
5186                   "direct call of ("
5187                << DCArgNo << "@" << DC->getCalledFunction()->getName()
5188                << ").\n";
5189       });
5190 
5191       Function *DCCallee = DC->getCalledFunction();
5192       if (unsigned(DCArgNo) < DCCallee->arg_size()) {
5193         const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
5194             *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)));
5195         if (DCArgPrivAA.isValidState()) {
5196           auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
5197           if (!DCArgPrivTy.hasValue())
5198             return true;
5199           if (DCArgPrivTy.getValue() == PrivatizableType)
5200             return true;
5201         }
5202       }
5203 
5204       LLVM_DEBUG({
5205         dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5206                << " cannot be privatized in the context of its parent ("
5207                << Arg->getParent()->getName()
5208                << ")\n[AAPrivatizablePtr] because it is an argument in a "
5209                   "direct call of ("
5210                << ACS.getCallSite().getCalledFunction()->getName()
5211                << ").\n[AAPrivatizablePtr] for which the argument "
5212                   "privatization is not compatible.\n";
5213       });
5214       return false;
5215     };
5216 
5217     // Helper to check if the associated argument is used at the given abstract
5218     // call site in a way that is incompatible with the privatization assumed
5219     // here.
5220     auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
5221       if (ACS.isDirectCall())
5222         return IsCompatiblePrivArgOfCallback(ACS.getCallSite());
5223       if (ACS.isCallbackCall())
5224         return IsCompatiblePrivArgOfDirectCS(ACS);
5225       return false;
5226     };
5227 
5228     bool AllCallSitesKnown;
5229     if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
5230                                 AllCallSitesKnown))
5231       return indicatePessimisticFixpoint();
5232 
5233     return ChangeStatus::UNCHANGED;
5234   }
5235 
5236   /// Given a type to private \p PrivType, collect the constituates (which are
5237   /// used) in \p ReplacementTypes.
5238   static void
5239   identifyReplacementTypes(Type *PrivType,
5240                            SmallVectorImpl<Type *> &ReplacementTypes) {
5241     // TODO: For now we expand the privatization type to the fullest which can
5242     //       lead to dead arguments that need to be removed later.
5243     assert(PrivType && "Expected privatizable type!");
5244 
5245     // Traverse the type, extract constituate types on the outermost level.
5246     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5247       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
5248         ReplacementTypes.push_back(PrivStructType->getElementType(u));
5249     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5250       ReplacementTypes.append(PrivArrayType->getNumElements(),
5251                               PrivArrayType->getElementType());
5252     } else {
5253       ReplacementTypes.push_back(PrivType);
5254     }
5255   }
5256 
5257   /// Initialize \p Base according to the type \p PrivType at position \p IP.
5258   /// The values needed are taken from the arguments of \p F starting at
5259   /// position \p ArgNo.
5260   static void createInitialization(Type *PrivType, Value &Base, Function &F,
5261                                    unsigned ArgNo, Instruction &IP) {
5262     assert(PrivType && "Expected privatizable type!");
5263 
5264     IRBuilder<NoFolder> IRB(&IP);
5265     const DataLayout &DL = F.getParent()->getDataLayout();
5266 
5267     // Traverse the type, build GEPs and stores.
5268     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5269       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5270       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5271         Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
5272         Value *Ptr = constructPointer(
5273             PointeeTy, &Base, PrivStructLayout->getElementOffset(u), IRB, DL);
5274         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5275       }
5276     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5277       Type *PointeePtrTy = PrivArrayType->getElementType()->getPointerTo();
5278       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeePtrTy);
5279       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5280         Value *Ptr =
5281             constructPointer(PointeePtrTy, &Base, u * PointeeTySize, IRB, DL);
5282         new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5283       }
5284     } else {
5285       new StoreInst(F.getArg(ArgNo), &Base, &IP);
5286     }
5287   }
5288 
5289   /// Extract values from \p Base according to the type \p PrivType at the
5290   /// call position \p ACS. The values are appended to \p ReplacementValues.
5291   void createReplacementValues(Type *PrivType, AbstractCallSite ACS,
5292                                Value *Base,
5293                                SmallVectorImpl<Value *> &ReplacementValues) {
5294     assert(Base && "Expected base value!");
5295     assert(PrivType && "Expected privatizable type!");
5296     Instruction *IP = ACS.getInstruction();
5297 
5298     IRBuilder<NoFolder> IRB(IP);
5299     const DataLayout &DL = IP->getModule()->getDataLayout();
5300 
5301     if (Base->getType()->getPointerElementType() != PrivType)
5302       Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(),
5303                                                  "", ACS.getInstruction());
5304 
5305     // TODO: Improve the alignment of the loads.
5306     // Traverse the type, build GEPs and loads.
5307     if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5308       const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5309       for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5310         Type *PointeeTy = PrivStructType->getElementType(u);
5311         Value *Ptr =
5312             constructPointer(PointeeTy->getPointerTo(), Base,
5313                              PrivStructLayout->getElementOffset(u), IRB, DL);
5314         LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
5315         L->setAlignment(MaybeAlign(1));
5316         ReplacementValues.push_back(L);
5317       }
5318     } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5319       Type *PointeeTy = PrivArrayType->getElementType();
5320       uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
5321       Type *PointeePtrTy = PointeeTy->getPointerTo();
5322       for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5323         Value *Ptr =
5324             constructPointer(PointeePtrTy, Base, u * PointeeTySize, IRB, DL);
5325         LoadInst *L = new LoadInst(PointeePtrTy, Ptr, "", IP);
5326         L->setAlignment(MaybeAlign(1));
5327         ReplacementValues.push_back(L);
5328       }
5329     } else {
5330       LoadInst *L = new LoadInst(PrivType, Base, "", IP);
5331       L->setAlignment(MaybeAlign(1));
5332       ReplacementValues.push_back(L);
5333     }
5334   }
5335 
5336   /// See AbstractAttribute::manifest(...)
5337   ChangeStatus manifest(Attributor &A) override {
5338     if (!PrivatizableType.hasValue())
5339       return ChangeStatus::UNCHANGED;
5340     assert(PrivatizableType.getValue() && "Expected privatizable type!");
5341 
5342     // Collect all tail calls in the function as we cannot allow new allocas to
5343     // escape into tail recursion.
5344     // TODO: Be smarter about new allocas escaping into tail calls.
5345     SmallVector<CallInst *, 16> TailCalls;
5346     if (!A.checkForAllInstructions(
5347             [&](Instruction &I) {
5348               CallInst &CI = cast<CallInst>(I);
5349               if (CI.isTailCall())
5350                 TailCalls.push_back(&CI);
5351               return true;
5352             },
5353             *this, {Instruction::Call}))
5354       return ChangeStatus::UNCHANGED;
5355 
5356     Argument *Arg = getAssociatedArgument();
5357 
5358     // Callback to repair the associated function. A new alloca is placed at the
5359     // beginning and initialized with the values passed through arguments. The
5360     // new alloca replaces the use of the old pointer argument.
5361     Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
5362         [=](const Attributor::ArgumentReplacementInfo &ARI,
5363             Function &ReplacementFn, Function::arg_iterator ArgIt) {
5364           BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
5365           Instruction *IP = &*EntryBB.getFirstInsertionPt();
5366           auto *AI = new AllocaInst(PrivatizableType.getValue(), 0,
5367                                     Arg->getName() + ".priv", IP);
5368           createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
5369                                ArgIt->getArgNo(), *IP);
5370           Arg->replaceAllUsesWith(AI);
5371 
5372           for (CallInst *CI : TailCalls)
5373             CI->setTailCall(false);
5374         };
5375 
5376     // Callback to repair a call site of the associated function. The elements
5377     // of the privatizable type are loaded prior to the call and passed to the
5378     // new function version.
5379     Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
5380         [=](const Attributor::ArgumentReplacementInfo &ARI,
5381             AbstractCallSite ACS, SmallVectorImpl<Value *> &NewArgOperands) {
5382           createReplacementValues(
5383               PrivatizableType.getValue(), ACS,
5384               ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
5385                                   NewArgOperands);
5386         };
5387 
5388     // Collect the types that will replace the privatizable type in the function
5389     // signature.
5390     SmallVector<Type *, 16> ReplacementTypes;
5391     identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5392 
5393     // Register a rewrite of the argument.
5394     if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
5395                                            std::move(FnRepairCB),
5396                                            std::move(ACSRepairCB)))
5397       return ChangeStatus::CHANGED;
5398     return ChangeStatus::UNCHANGED;
5399   }
5400 
5401   /// See AbstractAttribute::trackStatistics()
5402   void trackStatistics() const override {
5403     STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
5404   }
5405 };
5406 
5407 struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
5408   AAPrivatizablePtrFloating(const IRPosition &IRP)
5409       : AAPrivatizablePtrImpl(IRP) {}
5410 
5411   /// See AbstractAttribute::initialize(...).
5412   virtual void initialize(Attributor &A) override {
5413     // TODO: We can privatize more than arguments.
5414     indicatePessimisticFixpoint();
5415   }
5416 
5417   ChangeStatus updateImpl(Attributor &A) override {
5418     llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
5419                      "updateImpl will not be called");
5420   }
5421 
5422   /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
5423   Optional<Type *> identifyPrivatizableType(Attributor &A) override {
5424     Value *Obj =
5425         GetUnderlyingObject(&getAssociatedValue(), A.getInfoCache().getDL());
5426     if (!Obj) {
5427       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
5428       return nullptr;
5429     }
5430 
5431     if (auto *AI = dyn_cast<AllocaInst>(Obj))
5432       if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
5433         if (CI->isOne())
5434           return Obj->getType()->getPointerElementType();
5435     if (auto *Arg = dyn_cast<Argument>(Obj)) {
5436       auto &PrivArgAA =
5437           A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(*Arg));
5438       if (PrivArgAA.isAssumedPrivatizablePtr())
5439         return Obj->getType()->getPointerElementType();
5440     }
5441 
5442     LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
5443                          "alloca nor privatizable argument: "
5444                       << *Obj << "!\n");
5445     return nullptr;
5446   }
5447 
5448   /// See AbstractAttribute::trackStatistics()
5449   void trackStatistics() const override {
5450     STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
5451   }
5452 };
5453 
5454 struct AAPrivatizablePtrCallSiteArgument final
5455     : public AAPrivatizablePtrFloating {
5456   AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP)
5457       : AAPrivatizablePtrFloating(IRP) {}
5458 
5459   /// See AbstractAttribute::initialize(...).
5460   void initialize(Attributor &A) override {
5461     if (getIRPosition().hasAttr(Attribute::ByVal))
5462       indicateOptimisticFixpoint();
5463   }
5464 
5465   /// See AbstractAttribute::updateImpl(...).
5466   ChangeStatus updateImpl(Attributor &A) override {
5467     PrivatizableType = identifyPrivatizableType(A);
5468     if (!PrivatizableType.hasValue())
5469       return ChangeStatus::UNCHANGED;
5470     if (!PrivatizableType.getValue())
5471       return indicatePessimisticFixpoint();
5472 
5473     const IRPosition &IRP = getIRPosition();
5474     auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP);
5475     if (!NoCaptureAA.isAssumedNoCapture()) {
5476       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
5477       return indicatePessimisticFixpoint();
5478     }
5479 
5480     auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP);
5481     if (!NoAliasAA.isAssumedNoAlias()) {
5482       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
5483       return indicatePessimisticFixpoint();
5484     }
5485 
5486     const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, IRP);
5487     if (!MemBehaviorAA.isAssumedReadOnly()) {
5488       LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
5489       return indicatePessimisticFixpoint();
5490     }
5491 
5492     return ChangeStatus::UNCHANGED;
5493   }
5494 
5495   /// See AbstractAttribute::trackStatistics()
5496   void trackStatistics() const override {
5497     STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
5498   }
5499 };
5500 
5501 struct AAPrivatizablePtrCallSiteReturned final
5502     : public AAPrivatizablePtrFloating {
5503   AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP)
5504       : AAPrivatizablePtrFloating(IRP) {}
5505 
5506   /// See AbstractAttribute::initialize(...).
5507   void initialize(Attributor &A) override {
5508     // TODO: We can privatize more than arguments.
5509     indicatePessimisticFixpoint();
5510   }
5511 
5512   /// See AbstractAttribute::trackStatistics()
5513   void trackStatistics() const override {
5514     STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
5515   }
5516 };
5517 
5518 struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
5519   AAPrivatizablePtrReturned(const IRPosition &IRP)
5520       : AAPrivatizablePtrFloating(IRP) {}
5521 
5522   /// See AbstractAttribute::initialize(...).
5523   void initialize(Attributor &A) override {
5524     // TODO: We can privatize more than arguments.
5525     indicatePessimisticFixpoint();
5526   }
5527 
5528   /// See AbstractAttribute::trackStatistics()
5529   void trackStatistics() const override {
5530     STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
5531   }
5532 };
5533 
5534 /// -------------------- Memory Behavior Attributes ----------------------------
5535 /// Includes read-none, read-only, and write-only.
5536 /// ----------------------------------------------------------------------------
5537 struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
5538   AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {}
5539 
5540   /// See AbstractAttribute::initialize(...).
5541   void initialize(Attributor &A) override {
5542     intersectAssumedBits(BEST_STATE);
5543     getKnownStateFromValue(getIRPosition(), getState());
5544     IRAttribute::initialize(A);
5545   }
5546 
5547   /// Return the memory behavior information encoded in the IR for \p IRP.
5548   static void getKnownStateFromValue(const IRPosition &IRP,
5549                                      BitIntegerState &State,
5550                                      bool IgnoreSubsumingPositions = false) {
5551     SmallVector<Attribute, 2> Attrs;
5552     IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
5553     for (const Attribute &Attr : Attrs) {
5554       switch (Attr.getKindAsEnum()) {
5555       case Attribute::ReadNone:
5556         State.addKnownBits(NO_ACCESSES);
5557         break;
5558       case Attribute::ReadOnly:
5559         State.addKnownBits(NO_WRITES);
5560         break;
5561       case Attribute::WriteOnly:
5562         State.addKnownBits(NO_READS);
5563         break;
5564       default:
5565         llvm_unreachable("Unexpcted attribute!");
5566       }
5567     }
5568 
5569     if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
5570       if (!I->mayReadFromMemory())
5571         State.addKnownBits(NO_READS);
5572       if (!I->mayWriteToMemory())
5573         State.addKnownBits(NO_WRITES);
5574     }
5575   }
5576 
5577   /// See AbstractAttribute::getDeducedAttributes(...).
5578   void getDeducedAttributes(LLVMContext &Ctx,
5579                             SmallVectorImpl<Attribute> &Attrs) const override {
5580     assert(Attrs.size() == 0);
5581     if (isAssumedReadNone())
5582       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
5583     else if (isAssumedReadOnly())
5584       Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
5585     else if (isAssumedWriteOnly())
5586       Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
5587     assert(Attrs.size() <= 1);
5588   }
5589 
5590   /// See AbstractAttribute::manifest(...).
5591   ChangeStatus manifest(Attributor &A) override {
5592     const IRPosition &IRP = getIRPosition();
5593 
5594     // Check if we would improve the existing attributes first.
5595     SmallVector<Attribute, 4> DeducedAttrs;
5596     getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
5597     if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
5598           return IRP.hasAttr(Attr.getKindAsEnum(),
5599                              /* IgnoreSubsumingPositions */ true);
5600         }))
5601       return ChangeStatus::UNCHANGED;
5602 
5603     // Clear existing attributes.
5604     IRP.removeAttrs(AttrKinds);
5605 
5606     // Use the generic manifest method.
5607     return IRAttribute::manifest(A);
5608   }
5609 
5610   /// See AbstractState::getAsStr().
5611   const std::string getAsStr() const override {
5612     if (isAssumedReadNone())
5613       return "readnone";
5614     if (isAssumedReadOnly())
5615       return "readonly";
5616     if (isAssumedWriteOnly())
5617       return "writeonly";
5618     return "may-read/write";
5619   }
5620 
5621   /// The set of IR attributes AAMemoryBehavior deals with.
5622   static const Attribute::AttrKind AttrKinds[3];
5623 };
5624 
5625 const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
5626     Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
5627 
5628 /// Memory behavior attribute for a floating value.
5629 struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
5630   AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5631 
5632   /// See AbstractAttribute::initialize(...).
5633   void initialize(Attributor &A) override {
5634     AAMemoryBehaviorImpl::initialize(A);
5635     // Initialize the use vector with all direct uses of the associated value.
5636     for (const Use &U : getAssociatedValue().uses())
5637       Uses.insert(&U);
5638   }
5639 
5640   /// See AbstractAttribute::updateImpl(...).
5641   ChangeStatus updateImpl(Attributor &A) override;
5642 
5643   /// See AbstractAttribute::trackStatistics()
5644   void trackStatistics() const override {
5645     if (isAssumedReadNone())
5646       STATS_DECLTRACK_FLOATING_ATTR(readnone)
5647     else if (isAssumedReadOnly())
5648       STATS_DECLTRACK_FLOATING_ATTR(readonly)
5649     else if (isAssumedWriteOnly())
5650       STATS_DECLTRACK_FLOATING_ATTR(writeonly)
5651   }
5652 
5653 private:
5654   /// Return true if users of \p UserI might access the underlying
5655   /// variable/location described by \p U and should therefore be analyzed.
5656   bool followUsersOfUseIn(Attributor &A, const Use *U,
5657                           const Instruction *UserI);
5658 
5659   /// Update the state according to the effect of use \p U in \p UserI.
5660   void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI);
5661 
5662 protected:
5663   /// Container for (transitive) uses of the associated argument.
5664   SetVector<const Use *> Uses;
5665 };
5666 
5667 /// Memory behavior attribute for function argument.
5668 struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
5669   AAMemoryBehaviorArgument(const IRPosition &IRP)
5670       : AAMemoryBehaviorFloating(IRP) {}
5671 
5672   /// See AbstractAttribute::initialize(...).
5673   void initialize(Attributor &A) override {
5674     intersectAssumedBits(BEST_STATE);
5675     const IRPosition &IRP = getIRPosition();
5676     // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
5677     // can query it when we use has/getAttr. That would allow us to reuse the
5678     // initialize of the base class here.
5679     bool HasByVal =
5680         IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
5681     getKnownStateFromValue(IRP, getState(),
5682                            /* IgnoreSubsumingPositions */ HasByVal);
5683 
5684     // Initialize the use vector with all direct uses of the associated value.
5685     Argument *Arg = getAssociatedArgument();
5686     if (!Arg || !Arg->getParent()->hasExactDefinition()) {
5687       indicatePessimisticFixpoint();
5688     } else {
5689       // Initialize the use vector with all direct uses of the associated value.
5690       for (const Use &U : Arg->uses())
5691         Uses.insert(&U);
5692     }
5693   }
5694 
5695   ChangeStatus manifest(Attributor &A) override {
5696     // TODO: From readattrs.ll: "inalloca parameters are always
5697     //                           considered written"
5698     if (hasAttr({Attribute::InAlloca})) {
5699       removeKnownBits(NO_WRITES);
5700       removeAssumedBits(NO_WRITES);
5701     }
5702     return AAMemoryBehaviorFloating::manifest(A);
5703   }
5704 
5705   /// See AbstractAttribute::trackStatistics()
5706   void trackStatistics() const override {
5707     if (isAssumedReadNone())
5708       STATS_DECLTRACK_ARG_ATTR(readnone)
5709     else if (isAssumedReadOnly())
5710       STATS_DECLTRACK_ARG_ATTR(readonly)
5711     else if (isAssumedWriteOnly())
5712       STATS_DECLTRACK_ARG_ATTR(writeonly)
5713   }
5714 };
5715 
5716 struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
5717   AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP)
5718       : AAMemoryBehaviorArgument(IRP) {}
5719 
5720   /// See AbstractAttribute::initialize(...).
5721   void initialize(Attributor &A) override {
5722     if (Argument *Arg = getAssociatedArgument()) {
5723       if (Arg->hasByValAttr()) {
5724         addKnownBits(NO_WRITES);
5725         removeKnownBits(NO_READS);
5726         removeAssumedBits(NO_READS);
5727       }
5728     } else {
5729     }
5730     AAMemoryBehaviorArgument::initialize(A);
5731   }
5732 
5733   /// See AbstractAttribute::updateImpl(...).
5734   ChangeStatus updateImpl(Attributor &A) override {
5735     // TODO: Once we have call site specific value information we can provide
5736     //       call site specific liveness liveness information and then it makes
5737     //       sense to specialize attributes for call sites arguments instead of
5738     //       redirecting requests to the callee argument.
5739     Argument *Arg = getAssociatedArgument();
5740     const IRPosition &ArgPos = IRPosition::argument(*Arg);
5741     auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos);
5742     return clampStateAndIndicateChange(
5743         getState(),
5744         static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState()));
5745   }
5746 
5747   /// See AbstractAttribute::trackStatistics()
5748   void trackStatistics() const override {
5749     if (isAssumedReadNone())
5750       STATS_DECLTRACK_CSARG_ATTR(readnone)
5751     else if (isAssumedReadOnly())
5752       STATS_DECLTRACK_CSARG_ATTR(readonly)
5753     else if (isAssumedWriteOnly())
5754       STATS_DECLTRACK_CSARG_ATTR(writeonly)
5755   }
5756 };
5757 
5758 /// Memory behavior attribute for a call site return position.
5759 struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
5760   AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP)
5761       : AAMemoryBehaviorFloating(IRP) {}
5762 
5763   /// See AbstractAttribute::manifest(...).
5764   ChangeStatus manifest(Attributor &A) override {
5765     // We do not annotate returned values.
5766     return ChangeStatus::UNCHANGED;
5767   }
5768 
5769   /// See AbstractAttribute::trackStatistics()
5770   void trackStatistics() const override {}
5771 };
5772 
5773 /// An AA to represent the memory behavior function attributes.
5774 struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
5775   AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5776 
5777   /// See AbstractAttribute::updateImpl(Attributor &A).
5778   virtual ChangeStatus updateImpl(Attributor &A) override;
5779 
5780   /// See AbstractAttribute::manifest(...).
5781   ChangeStatus manifest(Attributor &A) override {
5782     Function &F = cast<Function>(getAnchorValue());
5783     if (isAssumedReadNone()) {
5784       F.removeFnAttr(Attribute::ArgMemOnly);
5785       F.removeFnAttr(Attribute::InaccessibleMemOnly);
5786       F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
5787     }
5788     return AAMemoryBehaviorImpl::manifest(A);
5789   }
5790 
5791   /// See AbstractAttribute::trackStatistics()
5792   void trackStatistics() const override {
5793     if (isAssumedReadNone())
5794       STATS_DECLTRACK_FN_ATTR(readnone)
5795     else if (isAssumedReadOnly())
5796       STATS_DECLTRACK_FN_ATTR(readonly)
5797     else if (isAssumedWriteOnly())
5798       STATS_DECLTRACK_FN_ATTR(writeonly)
5799   }
5800 };
5801 
5802 /// AAMemoryBehavior attribute for call sites.
5803 struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
5804   AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5805 
5806   /// See AbstractAttribute::initialize(...).
5807   void initialize(Attributor &A) override {
5808     AAMemoryBehaviorImpl::initialize(A);
5809     Function *F = getAssociatedFunction();
5810     if (!F || !F->hasExactDefinition())
5811       indicatePessimisticFixpoint();
5812   }
5813 
5814   /// See AbstractAttribute::updateImpl(...).
5815   ChangeStatus updateImpl(Attributor &A) override {
5816     // TODO: Once we have call site specific value information we can provide
5817     //       call site specific liveness liveness information and then it makes
5818     //       sense to specialize attributes for call sites arguments instead of
5819     //       redirecting requests to the callee argument.
5820     Function *F = getAssociatedFunction();
5821     const IRPosition &FnPos = IRPosition::function(*F);
5822     auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos);
5823     return clampStateAndIndicateChange(
5824         getState(),
5825         static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState()));
5826   }
5827 
5828   /// See AbstractAttribute::trackStatistics()
5829   void trackStatistics() const override {
5830     if (isAssumedReadNone())
5831       STATS_DECLTRACK_CS_ATTR(readnone)
5832     else if (isAssumedReadOnly())
5833       STATS_DECLTRACK_CS_ATTR(readonly)
5834     else if (isAssumedWriteOnly())
5835       STATS_DECLTRACK_CS_ATTR(writeonly)
5836   }
5837 };
5838 } // namespace
5839 
5840 ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
5841 
5842   // The current assumed state used to determine a change.
5843   auto AssumedState = getAssumed();
5844 
5845   auto CheckRWInst = [&](Instruction &I) {
5846     // If the instruction has an own memory behavior state, use it to restrict
5847     // the local state. No further analysis is required as the other memory
5848     // state is as optimistic as it gets.
5849     if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
5850       const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
5851           *this, IRPosition::callsite_function(ICS));
5852       intersectAssumedBits(MemBehaviorAA.getAssumed());
5853       return !isAtFixpoint();
5854     }
5855 
5856     // Remove access kind modifiers if necessary.
5857     if (I.mayReadFromMemory())
5858       removeAssumedBits(NO_READS);
5859     if (I.mayWriteToMemory())
5860       removeAssumedBits(NO_WRITES);
5861     return !isAtFixpoint();
5862   };
5863 
5864   if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this))
5865     return indicatePessimisticFixpoint();
5866 
5867   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
5868                                         : ChangeStatus::UNCHANGED;
5869 }
5870 
5871 ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
5872 
5873   const IRPosition &IRP = getIRPosition();
5874   const IRPosition &FnPos = IRPosition::function_scope(IRP);
5875   AAMemoryBehavior::StateType &S = getState();
5876 
5877   // First, check the function scope. We take the known information and we avoid
5878   // work if the assumed information implies the current assumed information for
5879   // this attribute. This is a valid for all but byval arguments.
5880   Argument *Arg = IRP.getAssociatedArgument();
5881   AAMemoryBehavior::base_t FnMemAssumedState =
5882       AAMemoryBehavior::StateType::getWorstState();
5883   if (!Arg || !Arg->hasByValAttr()) {
5884     const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(
5885         *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
5886     FnMemAssumedState = FnMemAA.getAssumed();
5887     S.addKnownBits(FnMemAA.getKnown());
5888     if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
5889       return ChangeStatus::UNCHANGED;
5890   }
5891 
5892   // Make sure the value is not captured (except through "return"), if
5893   // it is, any information derived would be irrelevant anyway as we cannot
5894   // check the potential aliases introduced by the capture. However, no need
5895   // to fall back to anythign less optimistic than the function state.
5896   const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
5897       *this, IRP, /* TrackDependence */ true, DepClassTy::OPTIONAL);
5898   if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
5899     S.intersectAssumedBits(FnMemAssumedState);
5900     return ChangeStatus::CHANGED;
5901   }
5902 
5903   // The current assumed state used to determine a change.
5904   auto AssumedState = S.getAssumed();
5905 
5906   // Liveness information to exclude dead users.
5907   // TODO: Take the FnPos once we have call site specific liveness information.
5908   const auto &LivenessAA = A.getAAFor<AAIsDead>(
5909       *this, IRPosition::function(*IRP.getAssociatedFunction()),
5910       /* TrackDependence */ false);
5911 
5912   // Visit and expand uses until all are analyzed or a fixpoint is reached.
5913   for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) {
5914     const Use *U = Uses[i];
5915     Instruction *UserI = cast<Instruction>(U->getUser());
5916     LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI
5917                       << " [Dead: " << (A.isAssumedDead(*U, this, &LivenessAA))
5918                       << "]\n");
5919     if (A.isAssumedDead(*U, this, &LivenessAA))
5920       continue;
5921 
5922     // Check if the users of UserI should also be visited.
5923     if (followUsersOfUseIn(A, U, UserI))
5924       for (const Use &UserIUse : UserI->uses())
5925         Uses.insert(&UserIUse);
5926 
5927     // If UserI might touch memory we analyze the use in detail.
5928     if (UserI->mayReadOrWriteMemory())
5929       analyzeUseIn(A, U, UserI);
5930   }
5931 
5932   return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
5933                                         : ChangeStatus::UNCHANGED;
5934 }
5935 
5936 bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U,
5937                                                   const Instruction *UserI) {
5938   // The loaded value is unrelated to the pointer argument, no need to
5939   // follow the users of the load.
5940   if (isa<LoadInst>(UserI))
5941     return false;
5942 
5943   // By default we follow all uses assuming UserI might leak information on U,
5944   // we have special handling for call sites operands though.
5945   ImmutableCallSite ICS(UserI);
5946   if (!ICS || !ICS.isArgOperand(U))
5947     return true;
5948 
5949   // If the use is a call argument known not to be captured, the users of
5950   // the call do not need to be visited because they have to be unrelated to
5951   // the input. Note that this check is not trivial even though we disallow
5952   // general capturing of the underlying argument. The reason is that the
5953   // call might the argument "through return", which we allow and for which we
5954   // need to check call users.
5955   if (U->get()->getType()->isPointerTy()) {
5956     unsigned ArgNo = ICS.getArgumentNo(U);
5957     const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
5958         *this, IRPosition::callsite_argument(ICS, ArgNo),
5959         /* TrackDependence */ true, DepClassTy::OPTIONAL);
5960     return !ArgNoCaptureAA.isAssumedNoCapture();
5961   }
5962 
5963   return true;
5964 }
5965 
5966 void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U,
5967                                             const Instruction *UserI) {
5968   assert(UserI->mayReadOrWriteMemory());
5969 
5970   switch (UserI->getOpcode()) {
5971   default:
5972     // TODO: Handle all atomics and other side-effect operations we know of.
5973     break;
5974   case Instruction::Load:
5975     // Loads cause the NO_READS property to disappear.
5976     removeAssumedBits(NO_READS);
5977     return;
5978 
5979   case Instruction::Store:
5980     // Stores cause the NO_WRITES property to disappear if the use is the
5981     // pointer operand. Note that we do assume that capturing was taken care of
5982     // somewhere else.
5983     if (cast<StoreInst>(UserI)->getPointerOperand() == U->get())
5984       removeAssumedBits(NO_WRITES);
5985     return;
5986 
5987   case Instruction::Call:
5988   case Instruction::CallBr:
5989   case Instruction::Invoke: {
5990     // For call sites we look at the argument memory behavior attribute (this
5991     // could be recursive!) in order to restrict our own state.
5992     ImmutableCallSite ICS(UserI);
5993 
5994     // Give up on operand bundles.
5995     if (ICS.isBundleOperand(U)) {
5996       indicatePessimisticFixpoint();
5997       return;
5998     }
5999 
6000     // Calling a function does read the function pointer, maybe write it if the
6001     // function is self-modifying.
6002     if (ICS.isCallee(U)) {
6003       removeAssumedBits(NO_READS);
6004       break;
6005     }
6006 
6007     // Adjust the possible access behavior based on the information on the
6008     // argument.
6009     IRPosition Pos;
6010     if (U->get()->getType()->isPointerTy())
6011       Pos = IRPosition::callsite_argument(ICS, ICS.getArgumentNo(U));
6012     else
6013       Pos = IRPosition::callsite_function(ICS);
6014     const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
6015         *this, Pos,
6016         /* TrackDependence */ true, DepClassTy::OPTIONAL);
6017     // "assumed" has at most the same bits as the MemBehaviorAA assumed
6018     // and at least "known".
6019     intersectAssumedBits(MemBehaviorAA.getAssumed());
6020     return;
6021   }
6022   };
6023 
6024   // Generally, look at the "may-properties" and adjust the assumed state if we
6025   // did not trigger special handling before.
6026   if (UserI->mayReadFromMemory())
6027     removeAssumedBits(NO_READS);
6028   if (UserI->mayWriteToMemory())
6029     removeAssumedBits(NO_WRITES);
6030 }
6031 /// ------------------ Value Constant Range Attribute -------------------------
6032 
6033 struct AAValueConstantRangeImpl : AAValueConstantRange {
6034   using StateType = IntegerRangeState;
6035   AAValueConstantRangeImpl(const IRPosition &IRP) : AAValueConstantRange(IRP) {}
6036 
6037   /// See AbstractAttribute::getAsStr().
6038   const std::string getAsStr() const override {
6039     std::string Str;
6040     llvm::raw_string_ostream OS(Str);
6041     OS << "range(" << getBitWidth() << ")<";
6042     getKnown().print(OS);
6043     OS << " / ";
6044     getAssumed().print(OS);
6045     OS << ">";
6046     return OS.str();
6047   }
6048 
6049   /// Helper function to get a SCEV expr for the associated value at program
6050   /// point \p I.
6051   const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
6052     if (!getAnchorScope())
6053       return nullptr;
6054 
6055     ScalarEvolution *SE =
6056         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
6057             *getAnchorScope());
6058 
6059     LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
6060         *getAnchorScope());
6061 
6062     if (!SE || !LI)
6063       return nullptr;
6064 
6065     const SCEV *S = SE->getSCEV(&getAssociatedValue());
6066     if (!I)
6067       return S;
6068 
6069     return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
6070   }
6071 
6072   /// Helper function to get a range from SCEV for the associated value at
6073   /// program point \p I.
6074   ConstantRange getConstantRangeFromSCEV(Attributor &A,
6075                                          const Instruction *I = nullptr) const {
6076     if (!getAnchorScope())
6077       return getWorstState(getBitWidth());
6078 
6079     ScalarEvolution *SE =
6080         A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
6081             *getAnchorScope());
6082 
6083     const SCEV *S = getSCEV(A, I);
6084     if (!SE || !S)
6085       return getWorstState(getBitWidth());
6086 
6087     return SE->getUnsignedRange(S);
6088   }
6089 
6090   /// Helper function to get a range from LVI for the associated value at
6091   /// program point \p I.
6092   ConstantRange
6093   getConstantRangeFromLVI(Attributor &A,
6094                           const Instruction *CtxI = nullptr) const {
6095     if (!getAnchorScope())
6096       return getWorstState(getBitWidth());
6097 
6098     LazyValueInfo *LVI =
6099         A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
6100             *getAnchorScope());
6101 
6102     if (!LVI || !CtxI)
6103       return getWorstState(getBitWidth());
6104     return LVI->getConstantRange(&getAssociatedValue(),
6105                                  const_cast<BasicBlock *>(CtxI->getParent()),
6106                                  const_cast<Instruction *>(CtxI));
6107   }
6108 
6109   /// See AAValueConstantRange::getKnownConstantRange(..).
6110   ConstantRange
6111   getKnownConstantRange(Attributor &A,
6112                         const Instruction *CtxI = nullptr) const override {
6113     if (!CtxI || CtxI == getCtxI())
6114       return getKnown();
6115 
6116     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
6117     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
6118     return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
6119   }
6120 
6121   /// See AAValueConstantRange::getAssumedConstantRange(..).
6122   ConstantRange
6123   getAssumedConstantRange(Attributor &A,
6124                           const Instruction *CtxI = nullptr) const override {
6125     // TODO: Make SCEV use Attributor assumption.
6126     //       We may be able to bound a variable range via assumptions in
6127     //       Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
6128     //       evolve to x^2 + x, then we can say that y is in [2, 12].
6129 
6130     if (!CtxI || CtxI == getCtxI())
6131       return getAssumed();
6132 
6133     ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
6134     ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
6135     return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
6136   }
6137 
6138   /// See AbstractAttribute::initialize(..).
6139   void initialize(Attributor &A) override {
6140     // Intersect a range given by SCEV.
6141     intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
6142 
6143     // Intersect a range given by LVI.
6144     intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
6145   }
6146 
6147   /// Helper function to create MDNode for range metadata.
6148   static MDNode *
6149   getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
6150                             const ConstantRange &AssumedConstantRange) {
6151     Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
6152                                   Ty, AssumedConstantRange.getLower())),
6153                               ConstantAsMetadata::get(ConstantInt::get(
6154                                   Ty, AssumedConstantRange.getUpper()))};
6155     return MDNode::get(Ctx, LowAndHigh);
6156   }
6157 
6158   /// Return true if \p Assumed is included in \p KnownRanges.
6159   static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
6160 
6161     if (Assumed.isFullSet())
6162       return false;
6163 
6164     if (!KnownRanges)
6165       return true;
6166 
6167     // If multiple ranges are annotated in IR, we give up to annotate assumed
6168     // range for now.
6169 
6170     // TODO:  If there exists a known range which containts assumed range, we
6171     // can say assumed range is better.
6172     if (KnownRanges->getNumOperands() > 2)
6173       return false;
6174 
6175     ConstantInt *Lower =
6176         mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
6177     ConstantInt *Upper =
6178         mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
6179 
6180     ConstantRange Known(Lower->getValue(), Upper->getValue());
6181     return Known.contains(Assumed) && Known != Assumed;
6182   }
6183 
6184   /// Helper function to set range metadata.
6185   static bool
6186   setRangeMetadataIfisBetterRange(Instruction *I,
6187                                   const ConstantRange &AssumedConstantRange) {
6188     auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
6189     if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
6190       if (!AssumedConstantRange.isEmptySet()) {
6191         I->setMetadata(LLVMContext::MD_range,
6192                        getMDNodeForConstantRange(I->getType(), I->getContext(),
6193                                                  AssumedConstantRange));
6194         return true;
6195       }
6196     }
6197     return false;
6198   }
6199 
6200   /// See AbstractAttribute::manifest()
6201   ChangeStatus manifest(Attributor &A) override {
6202     ChangeStatus Changed = ChangeStatus::UNCHANGED;
6203     ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
6204     assert(!AssumedConstantRange.isFullSet() && "Invalid state");
6205 
6206     auto &V = getAssociatedValue();
6207     if (!AssumedConstantRange.isEmptySet() &&
6208         !AssumedConstantRange.isSingleElement()) {
6209       if (Instruction *I = dyn_cast<Instruction>(&V))
6210         if (isa<CallInst>(I) || isa<LoadInst>(I))
6211           if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
6212             Changed = ChangeStatus::CHANGED;
6213     }
6214 
6215     return Changed;
6216   }
6217 };
6218 
6219 struct AAValueConstantRangeArgument final
6220     : AAArgumentFromCallSiteArguments<
6221           AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState> {
6222   AAValueConstantRangeArgument(const IRPosition &IRP)
6223       : AAArgumentFromCallSiteArguments<
6224             AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState>(
6225             IRP) {}
6226 
6227   /// See AbstractAttribute::trackStatistics()
6228   void trackStatistics() const override {
6229     STATS_DECLTRACK_ARG_ATTR(value_range)
6230   }
6231 };
6232 
6233 struct AAValueConstantRangeReturned
6234     : AAReturnedFromReturnedValues<AAValueConstantRange,
6235                                    AAValueConstantRangeImpl> {
6236   using Base = AAReturnedFromReturnedValues<AAValueConstantRange,
6237                                             AAValueConstantRangeImpl>;
6238   AAValueConstantRangeReturned(const IRPosition &IRP) : Base(IRP) {}
6239 
6240   /// See AbstractAttribute::initialize(...).
6241   void initialize(Attributor &A) override {}
6242 
6243   /// See AbstractAttribute::trackStatistics()
6244   void trackStatistics() const override {
6245     STATS_DECLTRACK_FNRET_ATTR(value_range)
6246   }
6247 };
6248 
6249 struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
6250   AAValueConstantRangeFloating(const IRPosition &IRP)
6251       : AAValueConstantRangeImpl(IRP) {}
6252 
6253   /// See AbstractAttribute::initialize(...).
6254   void initialize(Attributor &A) override {
6255     AAValueConstantRangeImpl::initialize(A);
6256     Value &V = getAssociatedValue();
6257 
6258     if (auto *C = dyn_cast<ConstantInt>(&V)) {
6259       unionAssumed(ConstantRange(C->getValue()));
6260       indicateOptimisticFixpoint();
6261       return;
6262     }
6263 
6264     if (isa<UndefValue>(&V)) {
6265       indicateOptimisticFixpoint();
6266       return;
6267     }
6268 
6269     if (auto *I = dyn_cast<Instruction>(&V))
6270       if (isa<BinaryOperator>(I) || isa<CmpInst>(I)) {
6271         Value *LHS = I->getOperand(0);
6272         Value *RHS = I->getOperand(1);
6273 
6274         if (LHS->getType()->isIntegerTy() && RHS->getType()->isIntegerTy())
6275           return;
6276       }
6277 
6278     // If it is a load instruction with range metadata, use it.
6279     if (LoadInst *LI = dyn_cast<LoadInst>(&V))
6280       if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
6281         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
6282         return;
6283       }
6284 
6285     // We handle casts in the updateImpl.
6286     // TODO: Allow non integers as well.
6287     if (CastInst *CI = dyn_cast<CastInst>(&V))
6288       if (CI->getOperand(0)->getType()->isIntegerTy())
6289         return;
6290 
6291     // We can work with PHI and select instruction as we traverse their operands
6292     // during update.
6293     if (isa<SelectInst>(V) || isa<PHINode>(V))
6294       return;
6295 
6296     // Otherwise we give up.
6297     indicatePessimisticFixpoint();
6298 
6299     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
6300                       << getAssociatedValue() << "\n");
6301   }
6302 
6303   bool calculateBinaryOperator(
6304       Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
6305       Instruction *CtxI,
6306       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
6307     Value *LHS = BinOp->getOperand(0);
6308     Value *RHS = BinOp->getOperand(1);
6309 
6310     auto &LHSAA =
6311         A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS));
6312     QuerriedAAs.push_back(&LHSAA);
6313     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
6314 
6315     auto &RHSAA =
6316         A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS));
6317     QuerriedAAs.push_back(&RHSAA);
6318     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
6319 
6320     auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
6321 
6322     T.unionAssumed(AssumedRange);
6323 
6324     // TODO: Track a known state too.
6325 
6326     return T.isValidState();
6327   }
6328 
6329   bool calculateCastInst(
6330       Attributor &A, CastInst *CastI, IntegerRangeState &T, Instruction *CtxI,
6331       SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
6332     assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
6333     // TODO: Allow non integers as well.
6334     Value &OpV = *CastI->getOperand(0);
6335     assert(OpV.getType()->isIntegerTy() && "Expected integer cast");
6336 
6337     auto &OpAA =
6338         A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(OpV));
6339     QuerriedAAs.push_back(&OpAA);
6340     T.unionAssumed(
6341         OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
6342     return T.isValidState();
6343   }
6344 
6345   bool
6346   calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
6347                    Instruction *CtxI,
6348                    SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
6349     Value *LHS = CmpI->getOperand(0);
6350     Value *RHS = CmpI->getOperand(1);
6351 
6352     auto &LHSAA =
6353         A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS));
6354     QuerriedAAs.push_back(&LHSAA);
6355     auto &RHSAA =
6356         A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS));
6357     QuerriedAAs.push_back(&RHSAA);
6358 
6359     auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
6360     auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
6361 
6362     // If one of them is empty set, we can't decide.
6363     if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
6364       return true;
6365 
6366     bool MustTrue = false, MustFalse = false;
6367 
6368     auto AllowedRegion =
6369         ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
6370 
6371     auto SatisfyingRegion = ConstantRange::makeSatisfyingICmpRegion(
6372         CmpI->getPredicate(), RHSAARange);
6373 
6374     if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
6375       MustFalse = true;
6376 
6377     if (SatisfyingRegion.contains(LHSAARange))
6378       MustTrue = true;
6379 
6380     assert((!MustTrue || !MustFalse) &&
6381            "Either MustTrue or MustFalse should be false!");
6382 
6383     if (MustTrue)
6384       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
6385     else if (MustFalse)
6386       T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
6387     else
6388       T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
6389 
6390     LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
6391                       << " " << RHSAA << "\n");
6392 
6393     // TODO: Track a known state too.
6394     return T.isValidState();
6395   }
6396 
6397   /// See AbstractAttribute::updateImpl(...).
6398   ChangeStatus updateImpl(Attributor &A) override {
6399     Instruction *CtxI = getCtxI();
6400     auto VisitValueCB = [&](Value &V, IntegerRangeState &T,
6401                             bool Stripped) -> bool {
6402       Instruction *I = dyn_cast<Instruction>(&V);
6403       if (!I) {
6404 
6405         // If the value is not instruction, we query AA to Attributor.
6406         const auto &AA =
6407             A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(V));
6408 
6409         // Clamp operator is not used to utilize a program point CtxI.
6410         T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
6411 
6412         return T.isValidState();
6413       }
6414 
6415       SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
6416       if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
6417         if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
6418           return false;
6419       } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
6420         if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
6421           return false;
6422       } else if (auto *CastI = dyn_cast<CastInst>(I)) {
6423         if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
6424           return false;
6425       } else {
6426         // Give up with other instructions.
6427         // TODO: Add other instructions
6428 
6429         T.indicatePessimisticFixpoint();
6430         return false;
6431       }
6432 
6433       // Catch circular reasoning in a pessimistic way for now.
6434       // TODO: Check how the range evolves and if we stripped anything, see also
6435       //       AADereferenceable or AAAlign for similar situations.
6436       for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
6437         if (QueriedAA != this)
6438           continue;
6439         // If we are in a stady state we do not need to worry.
6440         if (T.getAssumed() == getState().getAssumed())
6441           continue;
6442         T.indicatePessimisticFixpoint();
6443       }
6444 
6445       return T.isValidState();
6446     };
6447 
6448     IntegerRangeState T(getBitWidth());
6449 
6450     if (!genericValueTraversal<AAValueConstantRange, IntegerRangeState>(
6451             A, getIRPosition(), *this, T, VisitValueCB))
6452       return indicatePessimisticFixpoint();
6453 
6454     return clampStateAndIndicateChange(getState(), T);
6455   }
6456 
6457   /// See AbstractAttribute::trackStatistics()
6458   void trackStatistics() const override {
6459     STATS_DECLTRACK_FLOATING_ATTR(value_range)
6460   }
6461 };
6462 
6463 struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
6464   AAValueConstantRangeFunction(const IRPosition &IRP)
6465       : AAValueConstantRangeImpl(IRP) {}
6466 
6467   /// See AbstractAttribute::initialize(...).
6468   ChangeStatus updateImpl(Attributor &A) override {
6469     llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
6470                      "not be called");
6471   }
6472 
6473   /// See AbstractAttribute::trackStatistics()
6474   void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
6475 };
6476 
6477 struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
6478   AAValueConstantRangeCallSite(const IRPosition &IRP)
6479       : AAValueConstantRangeFunction(IRP) {}
6480 
6481   /// See AbstractAttribute::trackStatistics()
6482   void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
6483 };
6484 
6485 struct AAValueConstantRangeCallSiteReturned
6486     : AACallSiteReturnedFromReturned<AAValueConstantRange,
6487                                      AAValueConstantRangeImpl> {
6488   AAValueConstantRangeCallSiteReturned(const IRPosition &IRP)
6489       : AACallSiteReturnedFromReturned<AAValueConstantRange,
6490                                        AAValueConstantRangeImpl>(IRP) {}
6491 
6492   /// See AbstractAttribute::initialize(...).
6493   void initialize(Attributor &A) override {
6494     // If it is a load instruction with range metadata, use the metadata.
6495     if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
6496       if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
6497         intersectKnown(getConstantRangeFromMetadata(*RangeMD));
6498 
6499     AAValueConstantRangeImpl::initialize(A);
6500   }
6501 
6502   /// See AbstractAttribute::trackStatistics()
6503   void trackStatistics() const override {
6504     STATS_DECLTRACK_CSRET_ATTR(value_range)
6505   }
6506 };
6507 struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
6508   AAValueConstantRangeCallSiteArgument(const IRPosition &IRP)
6509       : AAValueConstantRangeFloating(IRP) {}
6510 
6511   /// See AbstractAttribute::trackStatistics()
6512   void trackStatistics() const override {
6513     STATS_DECLTRACK_CSARG_ATTR(value_range)
6514   }
6515 };
6516 /// ----------------------------------------------------------------------------
6517 ///                               Attributor
6518 /// ----------------------------------------------------------------------------
6519 
6520 bool Attributor::isAssumedDead(const AbstractAttribute &AA,
6521                                const AAIsDead *FnLivenessAA,
6522                                bool CheckBBLivenessOnly, DepClassTy DepClass) {
6523   const IRPosition &IRP = AA.getIRPosition();
6524   if (!Functions.count(IRP.getAnchorScope()))
6525     return false;
6526   return isAssumedDead(IRP, &AA, FnLivenessAA, CheckBBLivenessOnly, DepClass);
6527 }
6528 
6529 bool Attributor::isAssumedDead(const Use &U,
6530                                const AbstractAttribute *QueryingAA,
6531                                const AAIsDead *FnLivenessAA,
6532                                bool CheckBBLivenessOnly, DepClassTy DepClass) {
6533   Instruction *UserI = dyn_cast<Instruction>(U.getUser());
6534   if (!UserI)
6535     return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
6536                          CheckBBLivenessOnly, DepClass);
6537 
6538   if (CallSite CS = CallSite(UserI)) {
6539     // For call site argument uses we can check if the argument is
6540     // unused/dead.
6541     if (CS.isArgOperand(&U)) {
6542       const IRPosition &CSArgPos =
6543           IRPosition::callsite_argument(CS, CS.getArgumentNo(&U));
6544       return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
6545                            CheckBBLivenessOnly, DepClass);
6546     }
6547   } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
6548     const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
6549     return isAssumedDead(RetPos, QueryingAA, FnLivenessAA, CheckBBLivenessOnly,
6550                          DepClass);
6551   } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
6552     BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
6553     return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
6554                          CheckBBLivenessOnly, DepClass);
6555   }
6556 
6557   return isAssumedDead(IRPosition::value(*UserI), QueryingAA, FnLivenessAA,
6558                        CheckBBLivenessOnly, DepClass);
6559 }
6560 
6561 bool Attributor::isAssumedDead(const Instruction &I,
6562                                const AbstractAttribute *QueryingAA,
6563                                const AAIsDead *FnLivenessAA,
6564                                bool CheckBBLivenessOnly, DepClassTy DepClass) {
6565   if (!FnLivenessAA)
6566     FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction()),
6567                                          QueryingAA,
6568                                          /* TrackDependence */ false);
6569 
6570   // If we have a context instruction and a liveness AA we use it.
6571   if (FnLivenessAA &&
6572       FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() &&
6573       FnLivenessAA->isAssumedDead(&I)) {
6574     if (QueryingAA)
6575       recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
6576     return true;
6577   }
6578 
6579   if (CheckBBLivenessOnly)
6580     return false;
6581 
6582   const AAIsDead &IsDeadAA = getOrCreateAAFor<AAIsDead>(
6583       IRPosition::value(I), QueryingAA, /* TrackDependence */ false);
6584   // Don't check liveness for AAIsDead.
6585   if (QueryingAA == &IsDeadAA)
6586     return false;
6587 
6588   if (IsDeadAA.isAssumedDead()) {
6589     if (QueryingAA)
6590       recordDependence(IsDeadAA, *QueryingAA, DepClass);
6591     return true;
6592   }
6593 
6594   return false;
6595 }
6596 
6597 bool Attributor::isAssumedDead(const IRPosition &IRP,
6598                                const AbstractAttribute *QueryingAA,
6599                                const AAIsDead *FnLivenessAA,
6600                                bool CheckBBLivenessOnly, DepClassTy DepClass) {
6601   Instruction *CtxI = IRP.getCtxI();
6602   if (CtxI &&
6603       isAssumedDead(*CtxI, QueryingAA, FnLivenessAA,
6604                     /* CheckBBLivenessOnly */ true,
6605                     CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
6606     return true;
6607 
6608   if (CheckBBLivenessOnly)
6609     return false;
6610 
6611   // If we haven't succeeded we query the specific liveness info for the IRP.
6612   const AAIsDead *IsDeadAA;
6613   if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE)
6614     IsDeadAA = &getOrCreateAAFor<AAIsDead>(
6615         IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())),
6616         QueryingAA, /* TrackDependence */ false);
6617   else
6618     IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA,
6619                                            /* TrackDependence */ false);
6620   // Don't check liveness for AAIsDead.
6621   if (QueryingAA == IsDeadAA)
6622     return false;
6623 
6624   if (IsDeadAA->isAssumedDead()) {
6625     if (QueryingAA)
6626       recordDependence(*IsDeadAA, *QueryingAA, DepClass);
6627     return true;
6628   }
6629 
6630   return false;
6631 }
6632 
6633 bool Attributor::checkForAllUses(
6634     const function_ref<bool(const Use &, bool &)> &Pred,
6635     const AbstractAttribute &QueryingAA, const Value &V,
6636     DepClassTy LivenessDepClass) {
6637 
6638   // Check the trivial case first as it catches void values.
6639   if (V.use_empty())
6640     return true;
6641 
6642   // If the value is replaced by another one, for now a constant, we do not have
6643   // uses. Note that this requires users of `checkForAllUses` to not recurse but
6644   // instead use the `follow` callback argument to look at transitive users,
6645   // however, that should be clear from the presence of the argument.
6646   bool UsedAssumedInformation = false;
6647   Optional<ConstantInt *> CI =
6648       getAssumedConstant(*this, V, QueryingAA, UsedAssumedInformation);
6649   if (CI.hasValue() && CI.getValue()) {
6650     LLVM_DEBUG(dbgs() << "[Attributor] Value is simplified, uses skipped: " << V
6651                       << " -> " << *CI.getValue() << "\n");
6652     return true;
6653   }
6654 
6655   const IRPosition &IRP = QueryingAA.getIRPosition();
6656   SmallVector<const Use *, 16> Worklist;
6657   SmallPtrSet<const Use *, 16> Visited;
6658 
6659   for (const Use &U : V.uses())
6660     Worklist.push_back(&U);
6661 
6662   LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
6663                     << " initial uses to check\n");
6664 
6665   const Function *ScopeFn = IRP.getAnchorScope();
6666   const auto *LivenessAA =
6667       ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
6668                                     /* TrackDependence */ false)
6669               : nullptr;
6670 
6671   while (!Worklist.empty()) {
6672     const Use *U = Worklist.pop_back_val();
6673     if (!Visited.insert(U).second)
6674       continue;
6675     LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << " in "
6676                       << *U->getUser() << "\n");
6677     if (isAssumedDead(*U, &QueryingAA, LivenessAA,
6678                       /* CheckBBLivenessOnly */ false, LivenessDepClass)) {
6679       LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
6680       continue;
6681     }
6682 
6683     bool Follow = false;
6684     if (!Pred(*U, Follow))
6685       return false;
6686     if (!Follow)
6687       continue;
6688     for (const Use &UU : U->getUser()->uses())
6689       Worklist.push_back(&UU);
6690   }
6691 
6692   return true;
6693 }
6694 
6695 bool Attributor::checkForAllCallSites(
6696     const function_ref<bool(AbstractCallSite)> &Pred,
6697     const AbstractAttribute &QueryingAA, bool RequireAllCallSites,
6698     bool &AllCallSitesKnown) {
6699   // We can try to determine information from
6700   // the call sites. However, this is only possible all call sites are known,
6701   // hence the function has internal linkage.
6702   const IRPosition &IRP = QueryingAA.getIRPosition();
6703   const Function *AssociatedFunction = IRP.getAssociatedFunction();
6704   if (!AssociatedFunction) {
6705     LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
6706                       << "\n");
6707     AllCallSitesKnown = false;
6708     return false;
6709   }
6710 
6711   return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
6712                               &QueryingAA, AllCallSitesKnown);
6713 }
6714 
6715 bool Attributor::checkForAllCallSites(
6716     const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn,
6717     bool RequireAllCallSites, const AbstractAttribute *QueryingAA,
6718     bool &AllCallSitesKnown) {
6719   if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
6720     LLVM_DEBUG(
6721         dbgs()
6722         << "[Attributor] Function " << Fn.getName()
6723         << " has no internal linkage, hence not all call sites are known\n");
6724     AllCallSitesKnown = false;
6725     return false;
6726   }
6727 
6728   // If we do not require all call sites we might not see all.
6729   AllCallSitesKnown = RequireAllCallSites;
6730 
6731   for (const Use &U : Fn.uses()) {
6732     LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << *U << " in "
6733                       << *U.getUser() << "\n");
6734     if (isAssumedDead(U, QueryingAA, nullptr, /* CheckBBLivenessOnly */ true)) {
6735       LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
6736       continue;
6737     }
6738 
6739     AbstractCallSite ACS(&U);
6740     if (!ACS) {
6741       LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
6742                         << " has non call site use " << *U.get() << " in "
6743                         << *U.getUser() << "\n");
6744       // BlockAddress users are allowed.
6745       if (isa<BlockAddress>(U.getUser()))
6746         continue;
6747       return false;
6748     }
6749 
6750     const Use *EffectiveUse =
6751         ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
6752     if (!ACS.isCallee(EffectiveUse)) {
6753       if (!RequireAllCallSites)
6754         continue;
6755       LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser()
6756                         << " is an invalid use of " << Fn.getName() << "\n");
6757       return false;
6758     }
6759 
6760     if (Pred(ACS))
6761       continue;
6762 
6763     LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
6764                       << *ACS.getInstruction() << "\n");
6765     return false;
6766   }
6767 
6768   return true;
6769 }
6770 
6771 bool Attributor::checkForAllReturnedValuesAndReturnInsts(
6772     const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
6773         &Pred,
6774     const AbstractAttribute &QueryingAA) {
6775 
6776   const IRPosition &IRP = QueryingAA.getIRPosition();
6777   // Since we need to provide return instructions we have to have an exact
6778   // definition.
6779   const Function *AssociatedFunction = IRP.getAssociatedFunction();
6780   if (!AssociatedFunction)
6781     return false;
6782 
6783   // If this is a call site query we use the call site specific return values
6784   // and liveness information.
6785   // TODO: use the function scope once we have call site AAReturnedValues.
6786   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
6787   const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
6788   if (!AARetVal.getState().isValidState())
6789     return false;
6790 
6791   return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
6792 }
6793 
6794 bool Attributor::checkForAllReturnedValues(
6795     const function_ref<bool(Value &)> &Pred,
6796     const AbstractAttribute &QueryingAA) {
6797 
6798   const IRPosition &IRP = QueryingAA.getIRPosition();
6799   const Function *AssociatedFunction = IRP.getAssociatedFunction();
6800   if (!AssociatedFunction)
6801     return false;
6802 
6803   // TODO: use the function scope once we have call site AAReturnedValues.
6804   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
6805   const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
6806   if (!AARetVal.getState().isValidState())
6807     return false;
6808 
6809   return AARetVal.checkForAllReturnedValuesAndReturnInsts(
6810       [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
6811         return Pred(RV);
6812       });
6813 }
6814 
6815 static bool checkForAllInstructionsImpl(
6816     Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap,
6817     const function_ref<bool(Instruction &)> &Pred,
6818     const AbstractAttribute *QueryingAA, const AAIsDead *LivenessAA,
6819     const ArrayRef<unsigned> &Opcodes, bool CheckBBLivenessOnly = false) {
6820   for (unsigned Opcode : Opcodes) {
6821     for (Instruction *I : OpcodeInstMap[Opcode]) {
6822       // Skip dead instructions.
6823       if (A && A->isAssumedDead(IRPosition::value(*I), QueryingAA, LivenessAA,
6824                                 CheckBBLivenessOnly))
6825         continue;
6826 
6827       if (!Pred(*I))
6828         return false;
6829     }
6830   }
6831   return true;
6832 }
6833 
6834 bool Attributor::checkForAllInstructions(
6835     const llvm::function_ref<bool(Instruction &)> &Pred,
6836     const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes,
6837     bool CheckBBLivenessOnly) {
6838 
6839   const IRPosition &IRP = QueryingAA.getIRPosition();
6840   // Since we need to provide instructions we have to have an exact definition.
6841   const Function *AssociatedFunction = IRP.getAssociatedFunction();
6842   if (!AssociatedFunction)
6843     return false;
6844 
6845   // TODO: use the function scope once we have call site AAReturnedValues.
6846   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
6847   const auto &LivenessAA =
6848       getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
6849 
6850   auto &OpcodeInstMap =
6851       InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
6852   if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA,
6853                                    &LivenessAA, Opcodes, CheckBBLivenessOnly))
6854     return false;
6855 
6856   return true;
6857 }
6858 
6859 bool Attributor::checkForAllReadWriteInstructions(
6860     const llvm::function_ref<bool(Instruction &)> &Pred,
6861     AbstractAttribute &QueryingAA) {
6862 
6863   const Function *AssociatedFunction =
6864       QueryingAA.getIRPosition().getAssociatedFunction();
6865   if (!AssociatedFunction)
6866     return false;
6867 
6868   // TODO: use the function scope once we have call site AAReturnedValues.
6869   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
6870   const auto &LivenessAA =
6871       getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
6872 
6873   for (Instruction *I :
6874        InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
6875     // Skip dead instructions.
6876     if (isAssumedDead(IRPosition::value(*I), &QueryingAA, &LivenessAA))
6877       continue;
6878 
6879     if (!Pred(*I))
6880       return false;
6881   }
6882 
6883   return true;
6884 }
6885 
6886 ChangeStatus Attributor::run() {
6887   LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
6888                     << AllAbstractAttributes.size()
6889                     << " abstract attributes.\n");
6890 
6891   // Now that all abstract attributes are collected and initialized we start
6892   // the abstract analysis.
6893 
6894   unsigned IterationCounter = 1;
6895 
6896   SmallVector<AbstractAttribute *, 64> ChangedAAs;
6897   SetVector<AbstractAttribute *> Worklist, InvalidAAs;
6898   Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end());
6899 
6900   bool RecomputeDependences = false;
6901 
6902   do {
6903     // Remember the size to determine new attributes.
6904     size_t NumAAs = AllAbstractAttributes.size();
6905     LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
6906                       << ", Worklist size: " << Worklist.size() << "\n");
6907 
6908     // For invalid AAs we can fix dependent AAs that have a required dependence,
6909     // thereby folding long dependence chains in a single step without the need
6910     // to run updates.
6911     for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
6912       AbstractAttribute *InvalidAA = InvalidAAs[u];
6913       auto &QuerriedAAs = QueryMap[InvalidAA];
6914       LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has "
6915                         << QuerriedAAs.RequiredAAs.size() << "/"
6916                         << QuerriedAAs.OptionalAAs.size()
6917                         << " required/optional dependences\n");
6918       for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) {
6919         AbstractState &DOIAAState = DepOnInvalidAA->getState();
6920         DOIAAState.indicatePessimisticFixpoint();
6921         ++NumAttributesFixedDueToRequiredDependences;
6922         assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!");
6923         if (!DOIAAState.isValidState())
6924           InvalidAAs.insert(DepOnInvalidAA);
6925         else
6926           ChangedAAs.push_back(DepOnInvalidAA);
6927       }
6928       if (!RecomputeDependences)
6929         Worklist.insert(QuerriedAAs.OptionalAAs.begin(),
6930                         QuerriedAAs.OptionalAAs.end());
6931     }
6932 
6933     // If dependences (=QueryMap) are recomputed we have to look at all abstract
6934     // attributes again, regardless of what changed in the last iteration.
6935     if (RecomputeDependences) {
6936       LLVM_DEBUG(
6937           dbgs() << "[Attributor] Run all AAs to recompute dependences\n");
6938       QueryMap.clear();
6939       ChangedAAs.clear();
6940       Worklist.insert(AllAbstractAttributes.begin(),
6941                       AllAbstractAttributes.end());
6942     }
6943 
6944     // Add all abstract attributes that are potentially dependent on one that
6945     // changed to the work list.
6946     for (AbstractAttribute *ChangedAA : ChangedAAs) {
6947       auto &QuerriedAAs = QueryMap[ChangedAA];
6948       Worklist.insert(QuerriedAAs.OptionalAAs.begin(),
6949                       QuerriedAAs.OptionalAAs.end());
6950       Worklist.insert(QuerriedAAs.RequiredAAs.begin(),
6951                       QuerriedAAs.RequiredAAs.end());
6952     }
6953 
6954     LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
6955                       << ", Worklist+Dependent size: " << Worklist.size()
6956                       << "\n");
6957 
6958     // Reset the changed and invalid set.
6959     ChangedAAs.clear();
6960     InvalidAAs.clear();
6961 
6962     // Update all abstract attribute in the work list and record the ones that
6963     // changed.
6964     for (AbstractAttribute *AA : Worklist)
6965       if (!AA->getState().isAtFixpoint() &&
6966           !isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true)) {
6967         QueriedNonFixAA = false;
6968         if (AA->update(*this) == ChangeStatus::CHANGED) {
6969           ChangedAAs.push_back(AA);
6970           if (!AA->getState().isValidState())
6971             InvalidAAs.insert(AA);
6972         } else if (!QueriedNonFixAA) {
6973           // If the attribute did not query any non-fix information, the state
6974           // will not change and we can indicate that right away.
6975           AA->getState().indicateOptimisticFixpoint();
6976         }
6977       }
6978 
6979     // Check if we recompute the dependences in the next iteration.
6980     RecomputeDependences = (DepRecomputeInterval > 0 &&
6981                             IterationCounter % DepRecomputeInterval == 0);
6982 
6983     // Add attributes to the changed set if they have been created in the last
6984     // iteration.
6985     ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs,
6986                       AllAbstractAttributes.end());
6987 
6988     // Reset the work list and repopulate with the changed abstract attributes.
6989     // Note that dependent ones are added above.
6990     Worklist.clear();
6991     Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
6992 
6993   } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations ||
6994                                  VerifyMaxFixpointIterations));
6995 
6996   LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
6997                     << IterationCounter << "/" << MaxFixpointIterations
6998                     << " iterations\n");
6999 
7000   size_t NumFinalAAs = AllAbstractAttributes.size();
7001 
7002   // Reset abstract arguments not settled in a sound fixpoint by now. This
7003   // happens when we stopped the fixpoint iteration early. Note that only the
7004   // ones marked as "changed" *and* the ones transitively depending on them
7005   // need to be reverted to a pessimistic state. Others might not be in a
7006   // fixpoint state but we can use the optimistic results for them anyway.
7007   SmallPtrSet<AbstractAttribute *, 32> Visited;
7008   for (unsigned u = 0; u < ChangedAAs.size(); u++) {
7009     AbstractAttribute *ChangedAA = ChangedAAs[u];
7010     if (!Visited.insert(ChangedAA).second)
7011       continue;
7012 
7013     AbstractState &State = ChangedAA->getState();
7014     if (!State.isAtFixpoint()) {
7015       State.indicatePessimisticFixpoint();
7016 
7017       NumAttributesTimedOut++;
7018     }
7019 
7020     auto &QuerriedAAs = QueryMap[ChangedAA];
7021     ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(),
7022                       QuerriedAAs.OptionalAAs.end());
7023     ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(),
7024                       QuerriedAAs.RequiredAAs.end());
7025   }
7026 
7027   LLVM_DEBUG({
7028     if (!Visited.empty())
7029       dbgs() << "\n[Attributor] Finalized " << Visited.size()
7030              << " abstract attributes.\n";
7031   });
7032 
7033   unsigned NumManifested = 0;
7034   unsigned NumAtFixpoint = 0;
7035   ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
7036   for (AbstractAttribute *AA : AllAbstractAttributes) {
7037     AbstractState &State = AA->getState();
7038 
7039     // If there is not already a fixpoint reached, we can now take the
7040     // optimistic state. This is correct because we enforced a pessimistic one
7041     // on abstract attributes that were transitively dependent on a changed one
7042     // already above.
7043     if (!State.isAtFixpoint())
7044       State.indicateOptimisticFixpoint();
7045 
7046     // If the state is invalid, we do not try to manifest it.
7047     if (!State.isValidState())
7048       continue;
7049 
7050     // Skip dead code.
7051     if (isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true))
7052       continue;
7053     // Manifest the state and record if we changed the IR.
7054     ChangeStatus LocalChange = AA->manifest(*this);
7055     if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
7056       AA->trackStatistics();
7057 
7058     ManifestChange = ManifestChange | LocalChange;
7059 
7060     NumAtFixpoint++;
7061     NumManifested += (LocalChange == ChangeStatus::CHANGED);
7062   }
7063 
7064   (void)NumManifested;
7065   (void)NumAtFixpoint;
7066   LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
7067                     << " arguments while " << NumAtFixpoint
7068                     << " were in a valid fixpoint state\n");
7069 
7070   NumAttributesManifested += NumManifested;
7071   NumAttributesValidFixpoint += NumAtFixpoint;
7072 
7073   (void)NumFinalAAs;
7074   assert(NumFinalAAs == AllAbstractAttributes.size() &&
7075          "Expected the final number of abstract attributes to remain "
7076          "unchanged!");
7077 
7078   // Delete stuff at the end to avoid invalid references and a nice order.
7079   {
7080     LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least "
7081                       << ToBeDeletedFunctions.size() << " functions and "
7082                       << ToBeDeletedBlocks.size() << " blocks and "
7083                       << ToBeDeletedInsts.size() << " instructions and "
7084                       << ToBeChangedUses.size() << " uses\n");
7085 
7086     SmallVector<WeakTrackingVH, 32> DeadInsts;
7087     SmallVector<Instruction *, 32> TerminatorsToFold;
7088 
7089     for (auto &It : ToBeChangedUses) {
7090       Use *U = It.first;
7091       Value *NewV = It.second;
7092       Value *OldV = U->get();
7093       LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
7094                         << " instead of " << *OldV << "\n");
7095       U->set(NewV);
7096       if (Instruction *I = dyn_cast<Instruction>(OldV)) {
7097         CGModifiedFunctions.insert(I->getFunction());
7098         if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
7099             isInstructionTriviallyDead(I))
7100           DeadInsts.push_back(I);
7101       }
7102       if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
7103         Instruction *UserI = cast<Instruction>(U->getUser());
7104         if (isa<UndefValue>(NewV)) {
7105           ToBeChangedToUnreachableInsts.insert(UserI);
7106         } else {
7107           TerminatorsToFold.push_back(UserI);
7108         }
7109       }
7110     }
7111     for (auto &V : InvokeWithDeadSuccessor)
7112       if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
7113         bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
7114         bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
7115         bool Invoke2CallAllowed =
7116             !AAIsDeadFunction::mayCatchAsynchronousExceptions(
7117                 *II->getFunction());
7118         assert((UnwindBBIsDead || NormalBBIsDead) &&
7119                "Invoke does not have dead successors!");
7120         BasicBlock *BB = II->getParent();
7121         BasicBlock *NormalDestBB = II->getNormalDest();
7122         if (UnwindBBIsDead) {
7123           Instruction *NormalNextIP = &NormalDestBB->front();
7124           if (Invoke2CallAllowed) {
7125             changeToCall(II);
7126             NormalNextIP = BB->getTerminator();
7127           }
7128           if (NormalBBIsDead)
7129             ToBeChangedToUnreachableInsts.insert(NormalNextIP);
7130         } else {
7131           assert(NormalBBIsDead && "Broken invariant!");
7132           if (!NormalDestBB->getUniquePredecessor())
7133             NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
7134           ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
7135         }
7136       }
7137     for (auto &V : ToBeChangedToUnreachableInsts)
7138       if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
7139         CGModifiedFunctions.insert(I->getFunction());
7140         changeToUnreachable(I, /* UseLLVMTrap */ false);
7141       }
7142     for (Instruction *I : TerminatorsToFold) {
7143       CGModifiedFunctions.insert(I->getFunction());
7144       ConstantFoldTerminator(I->getParent());
7145     }
7146 
7147     for (auto &V : ToBeDeletedInsts) {
7148       if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
7149         CGModifiedFunctions.insert(I->getFunction());
7150         I->replaceAllUsesWith(UndefValue::get(I->getType()));
7151         if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
7152           DeadInsts.push_back(I);
7153         else
7154           I->eraseFromParent();
7155       }
7156     }
7157 
7158     RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);
7159 
7160     if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
7161       SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
7162       ToBeDeletedBBs.reserve(NumDeadBlocks);
7163       for (BasicBlock *BB : ToBeDeletedBlocks) {
7164         CGModifiedFunctions.insert(BB->getParent());
7165         ToBeDeletedBBs.push_back(BB);
7166       }
7167       // Actually we do not delete the blocks but squash them into a single
7168       // unreachable but untangling branches that jump here is something we need
7169       // to do in a more generic way.
7170       DetatchDeadBlocks(ToBeDeletedBBs, nullptr);
7171       STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.");
7172       BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size();
7173     }
7174 
7175     // Identify dead internal functions and delete them. This happens outside
7176     // the other fixpoint analysis as we might treat potentially dead functions
7177     // as live to lower the number of iterations. If they happen to be dead, the
7178     // below fixpoint loop will identify and eliminate them.
7179     SmallVector<Function *, 8> InternalFns;
7180     for (Function *F : Functions)
7181       if (F->hasLocalLinkage())
7182         InternalFns.push_back(F);
7183 
7184     bool FoundDeadFn = true;
7185     while (FoundDeadFn) {
7186       FoundDeadFn = false;
7187       for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
7188         Function *F = InternalFns[u];
7189         if (!F)
7190           continue;
7191 
7192         bool AllCallSitesKnown;
7193         if (!checkForAllCallSites(
7194                 [this](AbstractCallSite ACS) {
7195                   return ToBeDeletedFunctions.count(
7196                       ACS.getInstruction()->getFunction());
7197                 },
7198                 *F, true, nullptr, AllCallSitesKnown))
7199           continue;
7200 
7201         ToBeDeletedFunctions.insert(F);
7202         InternalFns[u] = nullptr;
7203         FoundDeadFn = true;
7204       }
7205     }
7206   }
7207 
7208   // Rewrite the functions as requested during manifest.
7209   ManifestChange =
7210       ManifestChange | rewriteFunctionSignatures(CGModifiedFunctions);
7211 
7212   for (Function *Fn : CGModifiedFunctions)
7213     CGUpdater.reanalyzeFunction(*Fn);
7214 
7215   STATS_DECL(AAIsDead, Function, "Number of dead functions deleted.");
7216   BUILD_STAT_NAME(AAIsDead, Function) += ToBeDeletedFunctions.size();
7217 
7218   for (Function *Fn : ToBeDeletedFunctions)
7219     CGUpdater.removeFunction(*Fn);
7220 
7221   if (VerifyMaxFixpointIterations &&
7222       IterationCounter != MaxFixpointIterations) {
7223     errs() << "\n[Attributor] Fixpoint iteration done after: "
7224            << IterationCounter << "/" << MaxFixpointIterations
7225            << " iterations\n";
7226     llvm_unreachable("The fixpoint was not reached with exactly the number of "
7227                      "specified iterations!");
7228   }
7229 
7230   return ManifestChange;
7231 }
7232 
7233 bool Attributor::isValidFunctionSignatureRewrite(
7234     Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
7235 
7236   auto CallSiteCanBeChanged = [](AbstractCallSite ACS) {
7237     // Forbid must-tail calls for now.
7238     return !ACS.isCallbackCall() && !ACS.getCallSite().isMustTailCall();
7239   };
7240 
7241   Function *Fn = Arg.getParent();
7242   // Avoid var-arg functions for now.
7243   if (Fn->isVarArg()) {
7244     LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
7245     return false;
7246   }
7247 
7248   // Avoid functions with complicated argument passing semantics.
7249   AttributeList FnAttributeList = Fn->getAttributes();
7250   if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
7251       FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
7252       FnAttributeList.hasAttrSomewhere(Attribute::InAlloca)) {
7253     LLVM_DEBUG(
7254         dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
7255     return false;
7256   }
7257 
7258   // Avoid callbacks for now.
7259   bool AllCallSitesKnown;
7260   if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
7261                             AllCallSitesKnown)) {
7262     LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
7263     return false;
7264   }
7265 
7266   auto InstPred = [](Instruction &I) {
7267     if (auto *CI = dyn_cast<CallInst>(&I))
7268       return !CI->isMustTailCall();
7269     return true;
7270   };
7271 
7272   // Forbid must-tail calls for now.
7273   // TODO:
7274   auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
7275   if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
7276                                    nullptr, {Instruction::Call})) {
7277     LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
7278     return false;
7279   }
7280 
7281   return true;
7282 }
7283 
7284 bool Attributor::registerFunctionSignatureRewrite(
7285     Argument &Arg, ArrayRef<Type *> ReplacementTypes,
7286     ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
7287     ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) {
7288   LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
7289                     << Arg.getParent()->getName() << " with "
7290                     << ReplacementTypes.size() << " replacements\n");
7291   assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
7292          "Cannot register an invalid rewrite");
7293 
7294   Function *Fn = Arg.getParent();
7295   SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = ArgumentReplacementMap[Fn];
7296   if (ARIs.empty())
7297     ARIs.resize(Fn->arg_size());
7298 
7299   // If we have a replacement already with less than or equal new arguments,
7300   // ignore this request.
7301   ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()];
7302   if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
7303     LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
7304     return false;
7305   }
7306 
7307   // If we have a replacement already but we like the new one better, delete
7308   // the old.
7309   if (ARI)
7310     delete ARI;
7311 
7312   LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
7313                     << Arg.getParent()->getName() << " with "
7314                     << ReplacementTypes.size() << " replacements\n");
7315 
7316   // Remember the replacement.
7317   ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
7318                                     std::move(CalleeRepairCB),
7319                                     std::move(ACSRepairCB));
7320 
7321   return true;
7322 }
7323 
7324 ChangeStatus Attributor::rewriteFunctionSignatures(
7325     SmallPtrSetImpl<Function *> &ModifiedFns) {
7326   ChangeStatus Changed = ChangeStatus::UNCHANGED;
7327 
7328   for (auto &It : ArgumentReplacementMap) {
7329     Function *OldFn = It.getFirst();
7330 
7331     // Deleted functions do not require rewrites.
7332     if (ToBeDeletedFunctions.count(OldFn))
7333       continue;
7334 
7335     const SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = It.getSecond();
7336     assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
7337 
7338     SmallVector<Type *, 16> NewArgumentTypes;
7339     SmallVector<AttributeSet, 16> NewArgumentAttributes;
7340 
7341     // Collect replacement argument types and copy over existing attributes.
7342     AttributeList OldFnAttributeList = OldFn->getAttributes();
7343     for (Argument &Arg : OldFn->args()) {
7344       if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) {
7345         NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
7346                                 ARI->ReplacementTypes.end());
7347         NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
7348                                      AttributeSet());
7349       } else {
7350         NewArgumentTypes.push_back(Arg.getType());
7351         NewArgumentAttributes.push_back(
7352             OldFnAttributeList.getParamAttributes(Arg.getArgNo()));
7353       }
7354     }
7355 
7356     FunctionType *OldFnTy = OldFn->getFunctionType();
7357     Type *RetTy = OldFnTy->getReturnType();
7358 
7359     // Construct the new function type using the new arguments types.
7360     FunctionType *NewFnTy =
7361         FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
7362 
7363     LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
7364                       << "' from " << *OldFn->getFunctionType() << " to "
7365                       << *NewFnTy << "\n");
7366 
7367     // Create the new function body and insert it into the module.
7368     Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
7369                                        OldFn->getAddressSpace(), "");
7370     OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
7371     NewFn->takeName(OldFn);
7372     NewFn->copyAttributesFrom(OldFn);
7373 
7374     // Patch the pointer to LLVM function in debug info descriptor.
7375     NewFn->setSubprogram(OldFn->getSubprogram());
7376     OldFn->setSubprogram(nullptr);
7377 
7378     // Recompute the parameter attributes list based on the new arguments for
7379     // the function.
7380     LLVMContext &Ctx = OldFn->getContext();
7381     NewFn->setAttributes(AttributeList::get(
7382         Ctx, OldFnAttributeList.getFnAttributes(),
7383         OldFnAttributeList.getRetAttributes(), NewArgumentAttributes));
7384 
7385     // Since we have now created the new function, splice the body of the old
7386     // function right into the new function, leaving the old rotting hulk of the
7387     // function empty.
7388     NewFn->getBasicBlockList().splice(NewFn->begin(),
7389                                       OldFn->getBasicBlockList());
7390 
7391     // Set of all "call-like" instructions that invoke the old function mapped
7392     // to their new replacements.
7393     SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs;
7394 
7395     // Callback to create a new "call-like" instruction for a given one.
7396     auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
7397       CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
7398       const AttributeList &OldCallAttributeList = OldCB->getAttributes();
7399 
7400       // Collect the new argument operands for the replacement call site.
7401       SmallVector<Value *, 16> NewArgOperands;
7402       SmallVector<AttributeSet, 16> NewArgOperandAttributes;
7403       for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
7404         unsigned NewFirstArgNum = NewArgOperands.size();
7405         (void)NewFirstArgNum; // only used inside assert.
7406         if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) {
7407           if (ARI->ACSRepairCB)
7408             ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
7409           assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
7410                      NewArgOperands.size() &&
7411                  "ACS repair callback did not provide as many operand as new "
7412                  "types were registered!");
7413           // TODO: Exose the attribute set to the ACS repair callback
7414           NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
7415                                          AttributeSet());
7416         } else {
7417           NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
7418           NewArgOperandAttributes.push_back(
7419               OldCallAttributeList.getParamAttributes(OldArgNum));
7420         }
7421       }
7422 
7423       assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
7424              "Mismatch # argument operands vs. # argument operand attributes!");
7425       assert(NewArgOperands.size() == NewFn->arg_size() &&
7426              "Mismatch # argument operands vs. # function arguments!");
7427 
7428       SmallVector<OperandBundleDef, 4> OperandBundleDefs;
7429       OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
7430 
7431       // Create a new call or invoke instruction to replace the old one.
7432       CallBase *NewCB;
7433       if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
7434         NewCB =
7435             InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(),
7436                                NewArgOperands, OperandBundleDefs, "", OldCB);
7437       } else {
7438         auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
7439                                        "", OldCB);
7440         NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
7441         NewCB = NewCI;
7442       }
7443 
7444       // Copy over various properties and the new attributes.
7445       uint64_t W;
7446       if (OldCB->extractProfTotalWeight(W))
7447         NewCB->setProfWeight(W);
7448       NewCB->setCallingConv(OldCB->getCallingConv());
7449       NewCB->setDebugLoc(OldCB->getDebugLoc());
7450       NewCB->takeName(OldCB);
7451       NewCB->setAttributes(AttributeList::get(
7452           Ctx, OldCallAttributeList.getFnAttributes(),
7453           OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes));
7454 
7455       CallSitePairs.push_back({OldCB, NewCB});
7456       return true;
7457     };
7458 
7459     // Use the CallSiteReplacementCreator to create replacement call sites.
7460     bool AllCallSitesKnown;
7461     bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
7462                                         true, nullptr, AllCallSitesKnown);
7463     (void)Success;
7464     assert(Success && "Assumed call site replacement to succeed!");
7465 
7466     // Rewire the arguments.
7467     auto OldFnArgIt = OldFn->arg_begin();
7468     auto NewFnArgIt = NewFn->arg_begin();
7469     for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
7470          ++OldArgNum, ++OldFnArgIt) {
7471       if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) {
7472         if (ARI->CalleeRepairCB)
7473           ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
7474         NewFnArgIt += ARI->ReplacementTypes.size();
7475       } else {
7476         NewFnArgIt->takeName(&*OldFnArgIt);
7477         OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
7478         ++NewFnArgIt;
7479       }
7480     }
7481 
7482     // Eliminate the instructions *after* we visited all of them.
7483     for (auto &CallSitePair : CallSitePairs) {
7484       CallBase &OldCB = *CallSitePair.first;
7485       CallBase &NewCB = *CallSitePair.second;
7486       // We do not modify the call graph here but simply reanalyze the old
7487       // function. This should be revisited once the old PM is gone.
7488       ModifiedFns.insert(OldCB.getFunction());
7489       OldCB.replaceAllUsesWith(&NewCB);
7490       OldCB.eraseFromParent();
7491     }
7492 
7493     // Replace the function in the call graph (if any).
7494     CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
7495 
7496     // If the old function was modified and needed to be reanalyzed, the new one
7497     // does now.
7498     if (ModifiedFns.erase(OldFn))
7499       ModifiedFns.insert(NewFn);
7500 
7501     Changed = ChangeStatus::CHANGED;
7502   }
7503 
7504   return Changed;
7505 }
7506 
7507 void Attributor::initializeInformationCache(Function &F) {
7508 
7509   // Walk all instructions to find interesting instructions that might be
7510   // queried by abstract attributes during their initialization or update.
7511   // This has to happen before we create attributes.
7512   auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F];
7513   auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F];
7514 
7515   for (Instruction &I : instructions(&F)) {
7516     bool IsInterestingOpcode = false;
7517 
7518     // To allow easy access to all instructions in a function with a given
7519     // opcode we store them in the InfoCache. As not all opcodes are interesting
7520     // to concrete attributes we only cache the ones that are as identified in
7521     // the following switch.
7522     // Note: There are no concrete attributes now so this is initially empty.
7523     switch (I.getOpcode()) {
7524     default:
7525       assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
7526              "New call site/base instruction type needs to be known int the "
7527              "Attributor.");
7528       break;
7529     case Instruction::Load:
7530       // The alignment of a pointer is interesting for loads.
7531     case Instruction::Store:
7532       // The alignment of a pointer is interesting for stores.
7533     case Instruction::Call:
7534     case Instruction::CallBr:
7535     case Instruction::Invoke:
7536     case Instruction::CleanupRet:
7537     case Instruction::CatchSwitch:
7538     case Instruction::AtomicRMW:
7539     case Instruction::AtomicCmpXchg:
7540     case Instruction::Br:
7541     case Instruction::Resume:
7542     case Instruction::Ret:
7543       IsInterestingOpcode = true;
7544     }
7545     if (IsInterestingOpcode)
7546       InstOpcodeMap[I.getOpcode()].push_back(&I);
7547     if (I.mayReadOrWriteMemory())
7548       ReadOrWriteInsts.push_back(&I);
7549   }
7550 }
7551 
7552 void Attributor::recordDependence(const AbstractAttribute &FromAA,
7553                                   const AbstractAttribute &ToAA,
7554                                   DepClassTy DepClass) {
7555   if (FromAA.getState().isAtFixpoint())
7556     return;
7557 
7558   if (DepClass == DepClassTy::REQUIRED)
7559     QueryMap[&FromAA].RequiredAAs.insert(
7560         const_cast<AbstractAttribute *>(&ToAA));
7561   else
7562     QueryMap[&FromAA].OptionalAAs.insert(
7563         const_cast<AbstractAttribute *>(&ToAA));
7564   QueriedNonFixAA = true;
7565 }
7566 
7567 void Attributor::identifyDefaultAbstractAttributes(Function &F) {
7568   if (!VisitedFunctions.insert(&F).second)
7569     return;
7570   if (F.isDeclaration())
7571     return;
7572 
7573   IRPosition FPos = IRPosition::function(F);
7574 
7575   // Check for dead BasicBlocks in every function.
7576   // We need dead instruction detection because we do not want to deal with
7577   // broken IR in which SSA rules do not apply.
7578   getOrCreateAAFor<AAIsDead>(FPos);
7579 
7580   // Every function might be "will-return".
7581   getOrCreateAAFor<AAWillReturn>(FPos);
7582 
7583   // Every function might contain instructions that cause "undefined behavior".
7584   getOrCreateAAFor<AAUndefinedBehavior>(FPos);
7585 
7586   // Every function can be nounwind.
7587   getOrCreateAAFor<AANoUnwind>(FPos);
7588 
7589   // Every function might be marked "nosync"
7590   getOrCreateAAFor<AANoSync>(FPos);
7591 
7592   // Every function might be "no-free".
7593   getOrCreateAAFor<AANoFree>(FPos);
7594 
7595   // Every function might be "no-return".
7596   getOrCreateAAFor<AANoReturn>(FPos);
7597 
7598   // Every function might be "no-recurse".
7599   getOrCreateAAFor<AANoRecurse>(FPos);
7600 
7601   // Every function might be "readnone/readonly/writeonly/...".
7602   getOrCreateAAFor<AAMemoryBehavior>(FPos);
7603 
7604   // Every function might be applicable for Heap-To-Stack conversion.
7605   if (EnableHeapToStack)
7606     getOrCreateAAFor<AAHeapToStack>(FPos);
7607 
7608   // Return attributes are only appropriate if the return type is non void.
7609   Type *ReturnType = F.getReturnType();
7610   if (!ReturnType->isVoidTy()) {
7611     // Argument attribute "returned" --- Create only one per function even
7612     // though it is an argument attribute.
7613     getOrCreateAAFor<AAReturnedValues>(FPos);
7614 
7615     IRPosition RetPos = IRPosition::returned(F);
7616 
7617     // Every returned value might be dead.
7618     getOrCreateAAFor<AAIsDead>(RetPos);
7619 
7620     // Every function might be simplified.
7621     getOrCreateAAFor<AAValueSimplify>(RetPos);
7622 
7623     if (ReturnType->isPointerTy()) {
7624 
7625       // Every function with pointer return type might be marked align.
7626       getOrCreateAAFor<AAAlign>(RetPos);
7627 
7628       // Every function with pointer return type might be marked nonnull.
7629       getOrCreateAAFor<AANonNull>(RetPos);
7630 
7631       // Every function with pointer return type might be marked noalias.
7632       getOrCreateAAFor<AANoAlias>(RetPos);
7633 
7634       // Every function with pointer return type might be marked
7635       // dereferenceable.
7636       getOrCreateAAFor<AADereferenceable>(RetPos);
7637     }
7638   }
7639 
7640   for (Argument &Arg : F.args()) {
7641     IRPosition ArgPos = IRPosition::argument(Arg);
7642 
7643     // Every argument might be simplified.
7644     getOrCreateAAFor<AAValueSimplify>(ArgPos);
7645 
7646     if (Arg.getType()->isPointerTy()) {
7647       // Every argument with pointer type might be marked nonnull.
7648       getOrCreateAAFor<AANonNull>(ArgPos);
7649 
7650       // Every argument with pointer type might be marked noalias.
7651       getOrCreateAAFor<AANoAlias>(ArgPos);
7652 
7653       // Every argument with pointer type might be marked dereferenceable.
7654       getOrCreateAAFor<AADereferenceable>(ArgPos);
7655 
7656       // Every argument with pointer type might be marked align.
7657       getOrCreateAAFor<AAAlign>(ArgPos);
7658 
7659       // Every argument with pointer type might be marked nocapture.
7660       getOrCreateAAFor<AANoCapture>(ArgPos);
7661 
7662       // Every argument with pointer type might be marked
7663       // "readnone/readonly/writeonly/..."
7664       getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
7665 
7666       // Every argument with pointer type might be marked nofree.
7667       getOrCreateAAFor<AANoFree>(ArgPos);
7668 
7669       // Every argument with pointer type might be privatizable (or promotable)
7670       getOrCreateAAFor<AAPrivatizablePtr>(ArgPos);
7671     }
7672   }
7673 
7674   auto CallSitePred = [&](Instruction &I) -> bool {
7675     CallSite CS(&I);
7676     IRPosition CSRetPos = IRPosition::callsite_returned(CS);
7677 
7678     // Call sites might be dead if they do not have side effects and no live
7679     // users. The return value might be dead if there are no live users.
7680     getOrCreateAAFor<AAIsDead>(CSRetPos);
7681 
7682     if (Function *Callee = CS.getCalledFunction()) {
7683       // Skip declerations except if annotations on their call sites were
7684       // explicitly requested.
7685       if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
7686           !Callee->hasMetadata(LLVMContext::MD_callback))
7687         return true;
7688 
7689       if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) {
7690 
7691         IRPosition CSRetPos = IRPosition::callsite_returned(CS);
7692 
7693         // Call site return integer values might be limited by a constant range.
7694         if (Callee->getReturnType()->isIntegerTy())
7695           getOrCreateAAFor<AAValueConstantRange>(CSRetPos);
7696       }
7697 
7698       for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) {
7699 
7700         IRPosition CSArgPos = IRPosition::callsite_argument(CS, i);
7701 
7702         // Every call site argument might be dead.
7703         getOrCreateAAFor<AAIsDead>(CSArgPos);
7704 
7705         // Call site argument might be simplified.
7706         getOrCreateAAFor<AAValueSimplify>(CSArgPos);
7707 
7708         if (!CS.getArgument(i)->getType()->isPointerTy())
7709           continue;
7710 
7711         // Call site argument attribute "non-null".
7712         getOrCreateAAFor<AANonNull>(CSArgPos);
7713 
7714         // Call site argument attribute "no-alias".
7715         getOrCreateAAFor<AANoAlias>(CSArgPos);
7716 
7717         // Call site argument attribute "dereferenceable".
7718         getOrCreateAAFor<AADereferenceable>(CSArgPos);
7719 
7720         // Call site argument attribute "align".
7721         getOrCreateAAFor<AAAlign>(CSArgPos);
7722 
7723         // Call site argument attribute
7724         // "readnone/readonly/writeonly/..."
7725         getOrCreateAAFor<AAMemoryBehavior>(CSArgPos);
7726 
7727         // Call site argument attribute "nofree".
7728         getOrCreateAAFor<AANoFree>(CSArgPos);
7729       }
7730     }
7731     return true;
7732   };
7733 
7734   auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
7735   bool Success;
7736   Success = checkForAllInstructionsImpl(
7737       nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
7738       {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
7739        (unsigned)Instruction::Call});
7740   (void)Success;
7741   assert(Success && "Expected the check call to be successful!");
7742 
7743   auto LoadStorePred = [&](Instruction &I) -> bool {
7744     if (isa<LoadInst>(I))
7745       getOrCreateAAFor<AAAlign>(
7746           IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
7747     else
7748       getOrCreateAAFor<AAAlign>(
7749           IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
7750     return true;
7751   };
7752   Success = checkForAllInstructionsImpl(
7753       nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
7754       {(unsigned)Instruction::Load, (unsigned)Instruction::Store});
7755   (void)Success;
7756   assert(Success && "Expected the check call to be successful!");
7757 }
7758 
7759 /// Helpers to ease debugging through output streams and print calls.
7760 ///
7761 ///{
7762 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
7763   return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
7764 }
7765 
7766 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
7767   switch (AP) {
7768   case IRPosition::IRP_INVALID:
7769     return OS << "inv";
7770   case IRPosition::IRP_FLOAT:
7771     return OS << "flt";
7772   case IRPosition::IRP_RETURNED:
7773     return OS << "fn_ret";
7774   case IRPosition::IRP_CALL_SITE_RETURNED:
7775     return OS << "cs_ret";
7776   case IRPosition::IRP_FUNCTION:
7777     return OS << "fn";
7778   case IRPosition::IRP_CALL_SITE:
7779     return OS << "cs";
7780   case IRPosition::IRP_ARGUMENT:
7781     return OS << "arg";
7782   case IRPosition::IRP_CALL_SITE_ARGUMENT:
7783     return OS << "cs_arg";
7784   }
7785   llvm_unreachable("Unknown attribute position!");
7786 }
7787 
7788 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
7789   const Value &AV = Pos.getAssociatedValue();
7790   return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
7791             << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}";
7792 }
7793 
7794 template <typename base_ty, base_ty BestState, base_ty WorstState>
7795 raw_ostream &
7796 llvm::operator<<(raw_ostream &OS,
7797                  const IntegerStateBase<base_ty, BestState, WorstState> &S) {
7798   return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
7799             << static_cast<const AbstractState &>(S);
7800 }
7801 
7802 raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) {
7803   OS << "range-state(" << S.getBitWidth() << ")<";
7804   S.getKnown().print(OS);
7805   OS << " / ";
7806   S.getAssumed().print(OS);
7807   OS << ">";
7808 
7809   return OS << static_cast<const AbstractState &>(S);
7810 }
7811 
7812 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
7813   return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
7814 }
7815 
7816 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
7817   AA.print(OS);
7818   return OS;
7819 }
7820 
7821 void AbstractAttribute::print(raw_ostream &OS) const {
7822   OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState()
7823      << "]";
7824 }
7825 ///}
7826 
7827 /// ----------------------------------------------------------------------------
7828 ///                       Pass (Manager) Boilerplate
7829 /// ----------------------------------------------------------------------------
7830 
7831 static bool runAttributorOnFunctions(InformationCache &InfoCache,
7832                                      SetVector<Function *> &Functions,
7833                                      AnalysisGetter &AG,
7834                                      CallGraphUpdater &CGUpdater) {
7835   if (DisableAttributor || Functions.empty())
7836     return false;
7837 
7838   LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << Functions.size()
7839                     << " functions.\n");
7840 
7841   // Create an Attributor and initially empty information cache that is filled
7842   // while we identify default attribute opportunities.
7843   Attributor A(Functions, InfoCache, CGUpdater, DepRecInterval);
7844 
7845   for (Function *F : Functions)
7846     A.initializeInformationCache(*F);
7847 
7848   for (Function *F : Functions) {
7849     if (F->hasExactDefinition())
7850       NumFnWithExactDefinition++;
7851     else
7852       NumFnWithoutExactDefinition++;
7853 
7854     // We look at internal functions only on-demand but if any use is not a
7855     // direct call or outside the current set of analyzed functions, we have to
7856     // do it eagerly.
7857     if (F->hasLocalLinkage()) {
7858       if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
7859             ImmutableCallSite ICS(U.getUser());
7860             return ICS && ICS.isCallee(&U) &&
7861                    Functions.count(const_cast<Function *>(ICS.getCaller()));
7862           }))
7863         continue;
7864     }
7865 
7866     // Populate the Attributor with abstract attribute opportunities in the
7867     // function and the information cache with IR information.
7868     A.identifyDefaultAbstractAttributes(*F);
7869   }
7870 
7871   ChangeStatus Changed = A.run();
7872   assert(!verifyModule(*Functions.front()->getParent(), &errs()) &&
7873          "Module verification failed!");
7874   LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
7875                     << " functions, result: " << Changed << ".\n");
7876   return Changed == ChangeStatus::CHANGED;
7877 }
7878 
7879 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
7880   FunctionAnalysisManager &FAM =
7881       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
7882   AnalysisGetter AG(FAM);
7883 
7884   SetVector<Function *> Functions;
7885   for (Function &F : M)
7886     Functions.insert(&F);
7887 
7888   CallGraphUpdater CGUpdater;
7889   InformationCache InfoCache(M, AG, /* CGSCC */ nullptr);
7890   if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
7891     // FIXME: Think about passes we will preserve and add them here.
7892     return PreservedAnalyses::none();
7893   }
7894   return PreservedAnalyses::all();
7895 }
7896 
7897 PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C,
7898                                            CGSCCAnalysisManager &AM,
7899                                            LazyCallGraph &CG,
7900                                            CGSCCUpdateResult &UR) {
7901   FunctionAnalysisManager &FAM =
7902       AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
7903   AnalysisGetter AG(FAM);
7904 
7905   SetVector<Function *> Functions;
7906   for (LazyCallGraph::Node &N : C)
7907     Functions.insert(&N.getFunction());
7908 
7909   if (Functions.empty())
7910     return PreservedAnalyses::all();
7911 
7912   Module &M = *Functions.back()->getParent();
7913   CallGraphUpdater CGUpdater;
7914   CGUpdater.initialize(CG, C, AM, UR);
7915   InformationCache InfoCache(M, AG, /* CGSCC */ &Functions);
7916   if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
7917     // FIXME: Think about passes we will preserve and add them here.
7918     return PreservedAnalyses::none();
7919   }
7920   return PreservedAnalyses::all();
7921 }
7922 
7923 namespace {
7924 
7925 struct AttributorLegacyPass : public ModulePass {
7926   static char ID;
7927 
7928   AttributorLegacyPass() : ModulePass(ID) {
7929     initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
7930   }
7931 
7932   bool runOnModule(Module &M) override {
7933     if (skipModule(M))
7934       return false;
7935 
7936     AnalysisGetter AG;
7937     SetVector<Function *> Functions;
7938     for (Function &F : M)
7939       Functions.insert(&F);
7940 
7941     CallGraphUpdater CGUpdater;
7942     InformationCache InfoCache(M, AG, /* CGSCC */ nullptr);
7943     return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
7944   }
7945 
7946   void getAnalysisUsage(AnalysisUsage &AU) const override {
7947     // FIXME: Think about passes we will preserve and add them here.
7948     AU.addRequired<TargetLibraryInfoWrapperPass>();
7949   }
7950 };
7951 
7952 struct AttributorCGSCCLegacyPass : public CallGraphSCCPass {
7953   CallGraphUpdater CGUpdater;
7954   static char ID;
7955 
7956   AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) {
7957     initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
7958   }
7959 
7960   bool runOnSCC(CallGraphSCC &SCC) override {
7961     if (skipSCC(SCC))
7962       return false;
7963 
7964     SetVector<Function *> Functions;
7965     for (CallGraphNode *CGN : SCC)
7966       if (Function *Fn = CGN->getFunction())
7967         if (!Fn->isDeclaration())
7968           Functions.insert(Fn);
7969 
7970     if (Functions.empty())
7971       return false;
7972 
7973     AnalysisGetter AG;
7974     CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph());
7975     CGUpdater.initialize(CG, SCC);
7976     Module &M = *Functions.back()->getParent();
7977     InformationCache InfoCache(M, AG, /* CGSCC */ &Functions);
7978     return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
7979   }
7980 
7981   bool doFinalization(CallGraph &CG) override { return CGUpdater.finalize(); }
7982 
7983   void getAnalysisUsage(AnalysisUsage &AU) const override {
7984     // FIXME: Think about passes we will preserve and add them here.
7985     AU.addRequired<TargetLibraryInfoWrapperPass>();
7986     CallGraphSCCPass::getAnalysisUsage(AU);
7987   }
7988 };
7989 
7990 } // end anonymous namespace
7991 
7992 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
7993 Pass *llvm::createAttributorCGSCCLegacyPass() {
7994   return new AttributorCGSCCLegacyPass();
7995 }
7996 
7997 char AttributorLegacyPass::ID = 0;
7998 char AttributorCGSCCLegacyPass::ID = 0;
7999 
8000 const char AAReturnedValues::ID = 0;
8001 const char AANoUnwind::ID = 0;
8002 const char AANoSync::ID = 0;
8003 const char AANoFree::ID = 0;
8004 const char AANonNull::ID = 0;
8005 const char AANoRecurse::ID = 0;
8006 const char AAWillReturn::ID = 0;
8007 const char AAUndefinedBehavior::ID = 0;
8008 const char AANoAlias::ID = 0;
8009 const char AAReachability::ID = 0;
8010 const char AANoReturn::ID = 0;
8011 const char AAIsDead::ID = 0;
8012 const char AADereferenceable::ID = 0;
8013 const char AAAlign::ID = 0;
8014 const char AANoCapture::ID = 0;
8015 const char AAValueSimplify::ID = 0;
8016 const char AAHeapToStack::ID = 0;
8017 const char AAPrivatizablePtr::ID = 0;
8018 const char AAMemoryBehavior::ID = 0;
8019 const char AAValueConstantRange::ID = 0;
8020 
8021 // Macro magic to create the static generator function for attributes that
8022 // follow the naming scheme.
8023 
8024 #define SWITCH_PK_INV(CLASS, PK, POS_NAME)                                     \
8025   case IRPosition::PK:                                                         \
8026     llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
8027 
8028 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX)                               \
8029   case IRPosition::PK:                                                         \
8030     AA = new CLASS##SUFFIX(IRP);                                               \
8031     break;
8032 
8033 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                 \
8034   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8035     CLASS *AA = nullptr;                                                       \
8036     switch (IRP.getPositionKind()) {                                           \
8037       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8038       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
8039       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
8040       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
8041       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
8042       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
8043       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
8044       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
8045     }                                                                          \
8046     return *AA;                                                                \
8047   }
8048 
8049 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                    \
8050   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8051     CLASS *AA = nullptr;                                                       \
8052     switch (IRP.getPositionKind()) {                                           \
8053       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8054       SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function")                           \
8055       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
8056       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
8057       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
8058       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
8059       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
8060       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
8061     }                                                                          \
8062     return *AA;                                                                \
8063   }
8064 
8065 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                      \
8066   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8067     CLASS *AA = nullptr;                                                       \
8068     switch (IRP.getPositionKind()) {                                           \
8069       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8070       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
8071       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
8072       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
8073       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
8074       SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned)                     \
8075       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
8076       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
8077     }                                                                          \
8078     return *AA;                                                                \
8079   }
8080 
8081 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)            \
8082   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8083     CLASS *AA = nullptr;                                                       \
8084     switch (IRP.getPositionKind()) {                                           \
8085       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8086       SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument")                           \
8087       SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating")                              \
8088       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
8089       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned")       \
8090       SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument")       \
8091       SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site")                         \
8092       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
8093     }                                                                          \
8094     return *AA;                                                                \
8095   }
8096 
8097 #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS)                  \
8098   CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) {      \
8099     CLASS *AA = nullptr;                                                       \
8100     switch (IRP.getPositionKind()) {                                           \
8101       SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid")                             \
8102       SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned")                           \
8103       SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function)                     \
8104       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite)                    \
8105       SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating)                        \
8106       SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument)                     \
8107       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned)   \
8108       SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument)   \
8109     }                                                                          \
8110     return *AA;                                                                \
8111   }
8112 
8113 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
8114 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
8115 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
8116 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
8117 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
8118 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
8119 
8120 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
8121 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
8122 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
8123 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
8124 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
8125 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
8126 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
8127 
8128 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
8129 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
8130 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
8131 
8132 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
8133 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
8134 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
8135 
8136 CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
8137 
8138 #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
8139 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
8140 #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
8141 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
8142 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
8143 #undef SWITCH_PK_CREATE
8144 #undef SWITCH_PK_INV
8145 
8146 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
8147                       "Deduce and propagate attributes", false, false)
8148 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
8149 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
8150                     "Deduce and propagate attributes", false, false)
8151 INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc",
8152                       "Deduce and propagate attributes (CGSCC pass)", false,
8153                       false)
8154 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
8155 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
8156 INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc",
8157                     "Deduce and propagate attributes (CGSCC pass)", false,
8158                     false)
8159