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