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