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