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