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