1 //===- Attributor.cpp - Module-wide attribute deduction -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements an interprocedural pass that deduces and/or propagates
10 // attributes. This is done in an abstract interpretation style fixpoint
11 // iteration. See the Attributor.h file comment and the class descriptions in
12 // that file for more information.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Transforms/IPO/Attributor.h"
17 
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/TinyPtrVector.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/CallGraph.h"
24 #include "llvm/Analysis/CallGraphSCCPass.h"
25 #include "llvm/Analysis/InlineCost.h"
26 #include "llvm/Analysis/MemoryBuiltins.h"
27 #include "llvm/Analysis/MustExecute.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/Constant.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/GlobalValue.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/IntrinsicInst.h"
36 #include "llvm/IR/ValueHandle.h"
37 #include "llvm/InitializePasses.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/DebugCounter.h"
42 #include "llvm/Support/FileSystem.h"
43 #include "llvm/Support/GraphWriter.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
46 #include "llvm/Transforms/Utils/Cloning.h"
47 #include "llvm/Transforms/Utils/Local.h"
48 
49 #ifdef EXPENSIVE_CHECKS
50 #include "llvm/IR/Verifier.h"
51 #endif
52 
53 #include <cassert>
54 #include <string>
55 
56 using namespace llvm;
57 
58 #define DEBUG_TYPE "attributor"
59 
60 DEBUG_COUNTER(ManifestDBGCounter, "attributor-manifest",
61               "Determine what attributes are manifested in the IR");
62 
63 STATISTIC(NumFnDeleted, "Number of function deleted");
64 STATISTIC(NumFnWithExactDefinition,
65           "Number of functions with exact definitions");
66 STATISTIC(NumFnWithoutExactDefinition,
67           "Number of functions without exact definitions");
68 STATISTIC(NumFnShallowWrappersCreated, "Number of shallow wrappers created");
69 STATISTIC(NumAttributesTimedOut,
70           "Number of abstract attributes timed out before fixpoint");
71 STATISTIC(NumAttributesValidFixpoint,
72           "Number of abstract attributes in a valid fixpoint state");
73 STATISTIC(NumAttributesManifested,
74           "Number of abstract attributes manifested in IR");
75 
76 // TODO: Determine a good default value.
77 //
78 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
79 // (when run with the first 5 abstract attributes). The results also indicate
80 // that we never reach 32 iterations but always find a fixpoint sooner.
81 //
82 // This will become more evolved once we perform two interleaved fixpoint
83 // iterations: bottom-up and top-down.
84 static cl::opt<unsigned>
85     SetFixpointIterations("attributor-max-iterations", cl::Hidden,
86                           cl::desc("Maximal number of fixpoint iterations."),
87                           cl::init(32));
88 
89 static cl::opt<unsigned, true> MaxInitializationChainLengthX(
90     "attributor-max-initialization-chain-length", cl::Hidden,
91     cl::desc(
92         "Maximal number of chained initializations (to avoid stack overflows)"),
93     cl::location(MaxInitializationChainLength), cl::init(1024));
94 unsigned llvm::MaxInitializationChainLength;
95 
96 static cl::opt<bool> VerifyMaxFixpointIterations(
97     "attributor-max-iterations-verify", cl::Hidden,
98     cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
99     cl::init(false));
100 
101 static cl::opt<bool> AnnotateDeclarationCallSites(
102     "attributor-annotate-decl-cs", cl::Hidden,
103     cl::desc("Annotate call sites of function declarations."), cl::init(false));
104 
105 static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
106                                        cl::init(true), cl::Hidden);
107 
108 static cl::opt<bool>
109     AllowShallowWrappers("attributor-allow-shallow-wrappers", cl::Hidden,
110                          cl::desc("Allow the Attributor to create shallow "
111                                   "wrappers for non-exact definitions."),
112                          cl::init(false));
113 
114 static cl::opt<bool>
115     AllowDeepWrapper("attributor-allow-deep-wrappers", cl::Hidden,
116                      cl::desc("Allow the Attributor to use IP information "
117                               "derived from non-exact functions via cloning"),
118                      cl::init(false));
119 
120 // These options can only used for debug builds.
121 #ifndef NDEBUG
122 static cl::list<std::string>
123     SeedAllowList("attributor-seed-allow-list", cl::Hidden,
124                   cl::desc("Comma seperated list of attribute names that are "
125                            "allowed to be seeded."),
126                   cl::CommaSeparated);
127 
128 static cl::list<std::string> FunctionSeedAllowList(
129     "attributor-function-seed-allow-list", cl::Hidden,
130     cl::desc("Comma seperated list of function names that are "
131              "allowed to be seeded."),
132     cl::CommaSeparated);
133 #endif
134 
135 static cl::opt<bool>
136     DumpDepGraph("attributor-dump-dep-graph", cl::Hidden,
137                  cl::desc("Dump the dependency graph to dot files."),
138                  cl::init(false));
139 
140 static cl::opt<std::string> DepGraphDotFileNamePrefix(
141     "attributor-depgraph-dot-filename-prefix", cl::Hidden,
142     cl::desc("The prefix used for the CallGraph dot file names."));
143 
144 static cl::opt<bool> ViewDepGraph("attributor-view-dep-graph", cl::Hidden,
145                                   cl::desc("View the dependency graph."),
146                                   cl::init(false));
147 
148 static cl::opt<bool> PrintDependencies("attributor-print-dep", cl::Hidden,
149                                        cl::desc("Print attribute dependencies"),
150                                        cl::init(false));
151 
152 static cl::opt<bool> EnableCallSiteSpecific(
153     "attributor-enable-call-site-specific-deduction", cl::Hidden,
154     cl::desc("Allow the Attributor to do call site specific analysis"),
155     cl::init(false));
156 
157 static cl::opt<bool>
158     PrintCallGraph("attributor-print-call-graph", cl::Hidden,
159                    cl::desc("Print Attributor's internal call graph"),
160                    cl::init(false));
161 
162 static cl::opt<bool> SimplifyAllLoads("attributor-simplify-all-loads",
163                                       cl::Hidden,
164                                       cl::desc("Try to simplify all loads."),
165                                       cl::init(true));
166 
167 /// Logic operators for the change status enum class.
168 ///
169 ///{
170 ChangeStatus llvm::operator|(ChangeStatus L, ChangeStatus R) {
171   return L == ChangeStatus::CHANGED ? L : R;
172 }
173 ChangeStatus &llvm::operator|=(ChangeStatus &L, ChangeStatus R) {
174   L = L | R;
175   return L;
176 }
177 ChangeStatus llvm::operator&(ChangeStatus L, ChangeStatus R) {
178   return L == ChangeStatus::UNCHANGED ? L : R;
179 }
180 ChangeStatus &llvm::operator&=(ChangeStatus &L, ChangeStatus R) {
181   L = L & R;
182   return L;
183 }
184 ///}
185 
186 bool AA::isNoSyncInst(Attributor &A, const Instruction &I,
187                       const AbstractAttribute &QueryingAA) {
188   // We are looking for volatile instructions or non-relaxed atomics.
189   if (const auto *CB = dyn_cast<CallBase>(&I)) {
190     if (CB->hasFnAttr(Attribute::NoSync))
191       return true;
192 
193     // Non-convergent and readnone imply nosync.
194     if (!CB->isConvergent() && !CB->mayReadOrWriteMemory())
195       return true;
196 
197     if (AANoSync::isNoSyncIntrinsic(&I))
198       return true;
199 
200     const auto &NoSyncAA = A.getAAFor<AANoSync>(
201         QueryingAA, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
202     return NoSyncAA.isAssumedNoSync();
203   }
204 
205   if (!I.mayReadOrWriteMemory())
206     return true;
207 
208   return !I.isVolatile() && !AANoSync::isNonRelaxedAtomic(&I);
209 }
210 
211 bool AA::isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA,
212                              const Value &V, bool ForAnalysisOnly) {
213   // TODO: See the AAInstanceInfo class comment.
214   if (!ForAnalysisOnly)
215     return false;
216   auto &InstanceInfoAA = A.getAAFor<AAInstanceInfo>(
217       QueryingAA, IRPosition::value(V), DepClassTy::OPTIONAL);
218   return InstanceInfoAA.isAssumedUniqueForAnalysis();
219 }
220 
221 Constant *AA::getInitialValueForObj(Value &Obj, Type &Ty,
222                                     const TargetLibraryInfo *TLI) {
223   if (isa<AllocaInst>(Obj))
224     return UndefValue::get(&Ty);
225   if (Constant *Init = getInitialValueOfAllocation(&Obj, TLI, &Ty))
226     return Init;
227   auto *GV = dyn_cast<GlobalVariable>(&Obj);
228   if (!GV)
229     return nullptr;
230   if (!GV->hasLocalLinkage() && !(GV->isConstant() && GV->hasInitializer()))
231     return nullptr;
232   if (!GV->hasInitializer())
233     return UndefValue::get(&Ty);
234   return dyn_cast_or_null<Constant>(getWithType(*GV->getInitializer(), Ty));
235 }
236 
237 bool AA::isValidInScope(const Value &V, const Function *Scope) {
238   if (isa<Constant>(V))
239     return true;
240   if (auto *I = dyn_cast<Instruction>(&V))
241     return I->getFunction() == Scope;
242   if (auto *A = dyn_cast<Argument>(&V))
243     return A->getParent() == Scope;
244   return false;
245 }
246 
247 bool AA::isValidAtPosition(const AA::ValueAndContext &VAC,
248                            InformationCache &InfoCache) {
249   if (isa<Constant>(VAC.getValue()) || VAC.getValue() == VAC.getCtxI())
250     return true;
251   const Function *Scope = nullptr;
252   const Instruction *CtxI = VAC.getCtxI();
253   if (CtxI)
254     Scope = CtxI->getFunction();
255   if (auto *A = dyn_cast<Argument>(VAC.getValue()))
256     return A->getParent() == Scope;
257   if (auto *I = dyn_cast<Instruction>(VAC.getValue())) {
258     if (I->getFunction() == Scope) {
259       if (const DominatorTree *DT =
260               InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
261                   *Scope))
262         return DT->dominates(I, CtxI);
263       // Local dominance check mostly for the old PM passes.
264       if (CtxI && I->getParent() == CtxI->getParent())
265         return llvm::any_of(
266             make_range(I->getIterator(), I->getParent()->end()),
267             [&](const Instruction &AfterI) { return &AfterI == CtxI; });
268     }
269   }
270   return false;
271 }
272 
273 Value *AA::getWithType(Value &V, Type &Ty) {
274   if (V.getType() == &Ty)
275     return &V;
276   if (isa<PoisonValue>(V))
277     return PoisonValue::get(&Ty);
278   if (isa<UndefValue>(V))
279     return UndefValue::get(&Ty);
280   if (auto *C = dyn_cast<Constant>(&V)) {
281     if (C->isNullValue())
282       return Constant::getNullValue(&Ty);
283     if (C->getType()->isPointerTy() && Ty.isPointerTy())
284       return ConstantExpr::getPointerCast(C, &Ty);
285     if (C->getType()->getPrimitiveSizeInBits() >= Ty.getPrimitiveSizeInBits()) {
286       if (C->getType()->isIntegerTy() && Ty.isIntegerTy())
287         return ConstantExpr::getTrunc(C, &Ty, /* OnlyIfReduced */ true);
288       if (C->getType()->isFloatingPointTy() && Ty.isFloatingPointTy())
289         return ConstantExpr::getFPTrunc(C, &Ty, /* OnlyIfReduced */ true);
290     }
291   }
292   return nullptr;
293 }
294 
295 Optional<Value *>
296 AA::combineOptionalValuesInAAValueLatice(const Optional<Value *> &A,
297                                          const Optional<Value *> &B, Type *Ty) {
298   if (A == B)
299     return A;
300   if (!B)
301     return A;
302   if (*B == nullptr)
303     return nullptr;
304   if (!A)
305     return Ty ? getWithType(**B, *Ty) : nullptr;
306   if (*A == nullptr)
307     return nullptr;
308   if (!Ty)
309     Ty = (*A)->getType();
310   if (isa_and_nonnull<UndefValue>(*A))
311     return getWithType(**B, *Ty);
312   if (isa<UndefValue>(*B))
313     return A;
314   if (*A && *B && *A == getWithType(**B, *Ty))
315     return A;
316   return nullptr;
317 }
318 
319 template <bool IsLoad, typename Ty>
320 static bool getPotentialCopiesOfMemoryValue(
321     Attributor &A, Ty &I, SmallSetVector<Value *, 4> &PotentialCopies,
322     SmallSetVector<Instruction *, 4> &PotentialValueOrigins,
323     const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
324     bool OnlyExact) {
325   LLVM_DEBUG(dbgs() << "Trying to determine the potential copies of " << I
326                     << " (only exact: " << OnlyExact << ")\n";);
327 
328   Value &Ptr = *I.getPointerOperand();
329   SmallSetVector<Value *, 8> Objects;
330   if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, QueryingAA, &I,
331                                        UsedAssumedInformation)) {
332     LLVM_DEBUG(
333         dbgs() << "Underlying objects stored into could not be determined\n";);
334     return false;
335   }
336 
337   // Containers to remember the pointer infos and new copies while we are not
338   // sure that we can find all of them. If we abort we want to avoid spurious
339   // dependences and potential copies in the provided container.
340   SmallVector<const AAPointerInfo *> PIs;
341   SmallVector<Value *> NewCopies;
342   SmallVector<Instruction *> NewCopyOrigins;
343 
344   const auto *TLI =
345       A.getInfoCache().getTargetLibraryInfoForFunction(*I.getFunction());
346   for (Value *Obj : Objects) {
347     LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n");
348     if (isa<UndefValue>(Obj))
349       continue;
350     if (isa<ConstantPointerNull>(Obj)) {
351       // A null pointer access can be undefined but any offset from null may
352       // be OK. We do not try to optimize the latter.
353       if (!NullPointerIsDefined(I.getFunction(),
354                                 Ptr.getType()->getPointerAddressSpace()) &&
355           A.getAssumedSimplified(Ptr, QueryingAA, UsedAssumedInformation,
356                                  AA::Interprocedural) == Obj)
357         continue;
358       LLVM_DEBUG(
359           dbgs() << "Underlying object is a valid nullptr, giving up.\n";);
360       return false;
361     }
362     // TODO: Use assumed noalias return.
363     if (!isa<AllocaInst>(Obj) && !isa<GlobalVariable>(Obj) &&
364         !(IsLoad ? isAllocationFn(Obj, TLI) : isNoAliasCall(Obj))) {
365       LLVM_DEBUG(dbgs() << "Underlying object is not supported yet: " << *Obj
366                         << "\n";);
367       return false;
368     }
369     if (auto *GV = dyn_cast<GlobalVariable>(Obj))
370       if (!GV->hasLocalLinkage() &&
371           !(GV->isConstant() && GV->hasInitializer())) {
372         LLVM_DEBUG(dbgs() << "Underlying object is global with external "
373                              "linkage, not supported yet: "
374                           << *Obj << "\n";);
375         return false;
376       }
377 
378     bool NullOnly = true;
379     bool NullRequired = false;
380     auto CheckForNullOnlyAndUndef = [&](Optional<Value *> V) {
381       if (!V || *V == nullptr)
382         NullOnly = false;
383       else if (isa<UndefValue>(*V))
384         /* No op */;
385       else if (isa<Constant>(*V) && cast<Constant>(*V)->isNullValue())
386         NullRequired = true;
387       else
388         NullOnly = false;
389     };
390 
391     if (IsLoad) {
392       Value *InitialValue = AA::getInitialValueForObj(*Obj, *I.getType(), TLI);
393       if (!InitialValue)
394         return false;
395       CheckForNullOnlyAndUndef(InitialValue);
396       NewCopies.push_back(InitialValue);
397       NewCopyOrigins.push_back(nullptr);
398     }
399 
400     auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
401       if ((IsLoad && !Acc.isWrite()) || (!IsLoad && !Acc.isRead()))
402         return true;
403       if (IsLoad && Acc.isWrittenValueYetUndetermined())
404         return true;
405       CheckForNullOnlyAndUndef(Acc.getContent());
406       if (OnlyExact && !IsExact && !NullOnly &&
407           !isa_and_nonnull<UndefValue>(Acc.getWrittenValue())) {
408         LLVM_DEBUG(dbgs() << "Non exact access " << *Acc.getRemoteInst()
409                           << ", abort!\n");
410         return false;
411       }
412       if (NullRequired && !NullOnly) {
413         LLVM_DEBUG(dbgs() << "Required all `null` accesses due to non exact "
414                              "one, however found non-null one: "
415                           << *Acc.getRemoteInst() << ", abort!\n");
416         return false;
417       }
418       if (IsLoad) {
419         assert(isa<LoadInst>(I) && "Expected load or store instruction only!");
420         if (!Acc.isWrittenValueUnknown()) {
421           NewCopies.push_back(Acc.getWrittenValue());
422           NewCopyOrigins.push_back(Acc.getRemoteInst());
423           return true;
424         }
425         auto *SI = dyn_cast<StoreInst>(Acc.getRemoteInst());
426         if (!SI) {
427           LLVM_DEBUG(dbgs() << "Underlying object written through a non-store "
428                                "instruction not supported yet: "
429                             << *Acc.getRemoteInst() << "\n";);
430           return false;
431         }
432         NewCopies.push_back(SI->getValueOperand());
433         NewCopyOrigins.push_back(SI);
434       } else {
435         assert(isa<StoreInst>(I) && "Expected load or store instruction only!");
436         auto *LI = dyn_cast<LoadInst>(Acc.getRemoteInst());
437         if (!LI && OnlyExact) {
438           LLVM_DEBUG(dbgs() << "Underlying object read through a non-load "
439                                "instruction not supported yet: "
440                             << *Acc.getRemoteInst() << "\n";);
441           return false;
442         }
443         NewCopies.push_back(Acc.getRemoteInst());
444       }
445       return true;
446     };
447 
448     auto &PI = A.getAAFor<AAPointerInfo>(QueryingAA, IRPosition::value(*Obj),
449                                          DepClassTy::NONE);
450     if (!PI.forallInterferingAccesses(A, QueryingAA, I, CheckAccess)) {
451       LLVM_DEBUG(
452           dbgs()
453           << "Failed to verify all interfering accesses for underlying object: "
454           << *Obj << "\n");
455       return false;
456     }
457     PIs.push_back(&PI);
458   }
459 
460   // Only if we were successful collection all potential copies we record
461   // dependences (on non-fix AAPointerInfo AAs). We also only then modify the
462   // given PotentialCopies container.
463   for (auto *PI : PIs) {
464     if (!PI->getState().isAtFixpoint())
465       UsedAssumedInformation = true;
466     A.recordDependence(*PI, QueryingAA, DepClassTy::OPTIONAL);
467   }
468   PotentialCopies.insert(NewCopies.begin(), NewCopies.end());
469   PotentialValueOrigins.insert(NewCopyOrigins.begin(), NewCopyOrigins.end());
470 
471   return true;
472 }
473 
474 bool AA::getPotentiallyLoadedValues(
475     Attributor &A, LoadInst &LI, SmallSetVector<Value *, 4> &PotentialValues,
476     SmallSetVector<Instruction *, 4> &PotentialValueOrigins,
477     const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
478     bool OnlyExact) {
479   return getPotentialCopiesOfMemoryValue</* IsLoad */ true>(
480       A, LI, PotentialValues, PotentialValueOrigins, QueryingAA,
481       UsedAssumedInformation, OnlyExact);
482 }
483 
484 bool AA::getPotentialCopiesOfStoredValue(
485     Attributor &A, StoreInst &SI, SmallSetVector<Value *, 4> &PotentialCopies,
486     const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
487     bool OnlyExact) {
488   SmallSetVector<Instruction *, 4> PotentialValueOrigins;
489   return getPotentialCopiesOfMemoryValue</* IsLoad */ false>(
490       A, SI, PotentialCopies, PotentialValueOrigins, QueryingAA,
491       UsedAssumedInformation, OnlyExact);
492 }
493 
494 static bool isAssumedReadOnlyOrReadNone(Attributor &A, const IRPosition &IRP,
495                                         const AbstractAttribute &QueryingAA,
496                                         bool RequireReadNone, bool &IsKnown) {
497 
498   IRPosition::Kind Kind = IRP.getPositionKind();
499   if (Kind == IRPosition::IRP_FUNCTION || Kind == IRPosition::IRP_CALL_SITE) {
500     const auto &MemLocAA =
501         A.getAAFor<AAMemoryLocation>(QueryingAA, IRP, DepClassTy::NONE);
502     if (MemLocAA.isAssumedReadNone()) {
503       IsKnown = MemLocAA.isKnownReadNone();
504       if (!IsKnown)
505         A.recordDependence(MemLocAA, QueryingAA, DepClassTy::OPTIONAL);
506       return true;
507     }
508   }
509 
510   const auto &MemBehaviorAA =
511       A.getAAFor<AAMemoryBehavior>(QueryingAA, IRP, DepClassTy::NONE);
512   if (MemBehaviorAA.isAssumedReadNone() ||
513       (!RequireReadNone && MemBehaviorAA.isAssumedReadOnly())) {
514     IsKnown = RequireReadNone ? MemBehaviorAA.isKnownReadNone()
515                               : MemBehaviorAA.isKnownReadOnly();
516     if (!IsKnown)
517       A.recordDependence(MemBehaviorAA, QueryingAA, DepClassTy::OPTIONAL);
518     return true;
519   }
520 
521   return false;
522 }
523 
524 bool AA::isAssumedReadOnly(Attributor &A, const IRPosition &IRP,
525                            const AbstractAttribute &QueryingAA, bool &IsKnown) {
526   return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
527                                      /* RequireReadNone */ false, IsKnown);
528 }
529 bool AA::isAssumedReadNone(Attributor &A, const IRPosition &IRP,
530                            const AbstractAttribute &QueryingAA, bool &IsKnown) {
531   return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
532                                      /* RequireReadNone */ true, IsKnown);
533 }
534 
535 static bool
536 isPotentiallyReachable(Attributor &A, const Instruction &FromI,
537                        const Instruction *ToI, const Function &ToFn,
538                        const AbstractAttribute &QueryingAA,
539                        std::function<bool(const Function &F)> GoBackwardsCB) {
540   LLVM_DEBUG(dbgs() << "[AA] isPotentiallyReachable @" << ToFn.getName()
541                     << " from " << FromI << " [GBCB: " << bool(GoBackwardsCB)
542                     << "]\n");
543 
544   SmallPtrSet<const Instruction *, 8> Visited;
545   SmallVector<const Instruction *> Worklist;
546   Worklist.push_back(&FromI);
547 
548   const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
549       QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL);
550   while (!Worklist.empty()) {
551     const Instruction *CurFromI = Worklist.pop_back_val();
552     if (!Visited.insert(CurFromI).second)
553       continue;
554 
555     const Function *FromFn = CurFromI->getFunction();
556     if (FromFn == &ToFn) {
557       if (!ToI)
558         return true;
559       LLVM_DEBUG(dbgs() << "[AA] check " << *ToI << " from " << *CurFromI
560                         << " intraprocedurally\n");
561       const auto &ReachabilityAA = A.getAAFor<AAReachability>(
562           QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL);
563       bool Result = ReachabilityAA.isAssumedReachable(A, *CurFromI, *ToI);
564       LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " "
565                         << (Result ? "can potentially " : "cannot ") << "reach "
566                         << *ToI << " [Intra]\n");
567       if (Result)
568         return true;
569       if (NoRecurseAA.isAssumedNoRecurse())
570         continue;
571     }
572 
573     // TODO: If we can go arbitrarily backwards we will eventually reach an
574     // entry point that can reach ToI. Only once this takes a set of blocks
575     // through which we cannot go, or once we track internal functions not
576     // accessible from the outside, it makes sense to perform backwards analysis
577     // in the absence of a GoBackwardsCB.
578     if (!GoBackwardsCB) {
579       LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn.getName() << " from "
580                         << *CurFromI << " is not checked backwards, abort\n");
581       return true;
582     }
583 
584     // Check if the current instruction is already known to reach the ToFn.
585     const auto &FnReachabilityAA = A.getAAFor<AAFunctionReachability>(
586         QueryingAA, IRPosition::function(*FromFn), DepClassTy::OPTIONAL);
587     bool Result = FnReachabilityAA.instructionCanReach(
588         A, *CurFromI, ToFn, /* UseBackwards */ false);
589     LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " in @" << FromFn->getName()
590                       << " " << (Result ? "can potentially " : "cannot ")
591                       << "reach @" << ToFn.getName() << " [FromFn]\n");
592     if (Result)
593       return true;
594 
595     // If we do not go backwards from the FromFn we are done here and so far we
596     // could not find a way to reach ToFn/ToI.
597     if (!GoBackwardsCB(*FromFn))
598       continue;
599 
600     LLVM_DEBUG(dbgs() << "Stepping backwards to the call sites of @"
601                       << FromFn->getName() << "\n");
602 
603     auto CheckCallSite = [&](AbstractCallSite ACS) {
604       CallBase *CB = ACS.getInstruction();
605       if (!CB)
606         return false;
607 
608       if (isa<InvokeInst>(CB))
609         return false;
610 
611       Instruction *Inst = CB->getNextNonDebugInstruction();
612       Worklist.push_back(Inst);
613       return true;
614     };
615 
616     bool UsedAssumedInformation = false;
617     Result = !A.checkForAllCallSites(CheckCallSite, *FromFn,
618                                      /* RequireAllCallSites */ true,
619                                      &QueryingAA, UsedAssumedInformation);
620     if (Result) {
621       LLVM_DEBUG(dbgs() << "[AA] stepping back to call sites from " << *CurFromI
622                         << " in @" << FromFn->getName()
623                         << " failed, give up\n");
624       return true;
625     }
626 
627     LLVM_DEBUG(dbgs() << "[AA] stepped back to call sites from " << *CurFromI
628                       << " in @" << FromFn->getName()
629                       << " worklist size is: " << Worklist.size() << "\n");
630   }
631   return false;
632 }
633 
634 bool AA::isPotentiallyReachable(
635     Attributor &A, const Instruction &FromI, const Instruction &ToI,
636     const AbstractAttribute &QueryingAA,
637     std::function<bool(const Function &F)> GoBackwardsCB) {
638   LLVM_DEBUG(dbgs() << "[AA] isPotentiallyReachable " << ToI << " from "
639                     << FromI << " [GBCB: " << bool(GoBackwardsCB) << "]\n");
640   const Function *ToFn = ToI.getFunction();
641   return ::isPotentiallyReachable(A, FromI, &ToI, *ToFn, QueryingAA,
642                                   GoBackwardsCB);
643 }
644 
645 bool AA::isPotentiallyReachable(
646     Attributor &A, const Instruction &FromI, const Function &ToFn,
647     const AbstractAttribute &QueryingAA,
648     std::function<bool(const Function &F)> GoBackwardsCB) {
649   return ::isPotentiallyReachable(A, FromI, /* ToI */ nullptr, ToFn, QueryingAA,
650                                   GoBackwardsCB);
651 }
652 
653 /// Return true if \p New is equal or worse than \p Old.
654 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
655   if (!Old.isIntAttribute())
656     return true;
657 
658   return Old.getValueAsInt() >= New.getValueAsInt();
659 }
660 
661 /// Return true if the information provided by \p Attr was added to the
662 /// attribute list \p Attrs. This is only the case if it was not already present
663 /// in \p Attrs at the position describe by \p PK and \p AttrIdx.
664 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
665                              AttributeList &Attrs, int AttrIdx,
666                              bool ForceReplace = false) {
667 
668   if (Attr.isEnumAttribute()) {
669     Attribute::AttrKind Kind = Attr.getKindAsEnum();
670     if (Attrs.hasAttributeAtIndex(AttrIdx, Kind))
671       if (!ForceReplace &&
672           isEqualOrWorse(Attr, Attrs.getAttributeAtIndex(AttrIdx, Kind)))
673         return false;
674     Attrs = Attrs.addAttributeAtIndex(Ctx, AttrIdx, Attr);
675     return true;
676   }
677   if (Attr.isStringAttribute()) {
678     StringRef Kind = Attr.getKindAsString();
679     if (Attrs.hasAttributeAtIndex(AttrIdx, Kind))
680       if (!ForceReplace &&
681           isEqualOrWorse(Attr, Attrs.getAttributeAtIndex(AttrIdx, Kind)))
682         return false;
683     Attrs = Attrs.addAttributeAtIndex(Ctx, AttrIdx, Attr);
684     return true;
685   }
686   if (Attr.isIntAttribute()) {
687     Attribute::AttrKind Kind = Attr.getKindAsEnum();
688     if (Attrs.hasAttributeAtIndex(AttrIdx, Kind))
689       if (!ForceReplace &&
690           isEqualOrWorse(Attr, Attrs.getAttributeAtIndex(AttrIdx, Kind)))
691         return false;
692     Attrs = Attrs.removeAttributeAtIndex(Ctx, AttrIdx, Kind);
693     Attrs = Attrs.addAttributeAtIndex(Ctx, AttrIdx, Attr);
694     return true;
695   }
696 
697   llvm_unreachable("Expected enum or string attribute!");
698 }
699 
700 Argument *IRPosition::getAssociatedArgument() const {
701   if (getPositionKind() == IRP_ARGUMENT)
702     return cast<Argument>(&getAnchorValue());
703 
704   // Not an Argument and no argument number means this is not a call site
705   // argument, thus we cannot find a callback argument to return.
706   int ArgNo = getCallSiteArgNo();
707   if (ArgNo < 0)
708     return nullptr;
709 
710   // Use abstract call sites to make the connection between the call site
711   // values and the ones in callbacks. If a callback was found that makes use
712   // of the underlying call site operand, we want the corresponding callback
713   // callee argument and not the direct callee argument.
714   Optional<Argument *> CBCandidateArg;
715   SmallVector<const Use *, 4> CallbackUses;
716   const auto &CB = cast<CallBase>(getAnchorValue());
717   AbstractCallSite::getCallbackUses(CB, CallbackUses);
718   for (const Use *U : CallbackUses) {
719     AbstractCallSite ACS(U);
720     assert(ACS && ACS.isCallbackCall());
721     if (!ACS.getCalledFunction())
722       continue;
723 
724     for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
725 
726       // Test if the underlying call site operand is argument number u of the
727       // callback callee.
728       if (ACS.getCallArgOperandNo(u) != ArgNo)
729         continue;
730 
731       assert(ACS.getCalledFunction()->arg_size() > u &&
732              "ACS mapped into var-args arguments!");
733       if (CBCandidateArg) {
734         CBCandidateArg = nullptr;
735         break;
736       }
737       CBCandidateArg = ACS.getCalledFunction()->getArg(u);
738     }
739   }
740 
741   // If we found a unique callback candidate argument, return it.
742   if (CBCandidateArg && CBCandidateArg.value())
743     return CBCandidateArg.value();
744 
745   // If no callbacks were found, or none used the underlying call site operand
746   // exclusively, use the direct callee argument if available.
747   const Function *Callee = CB.getCalledFunction();
748   if (Callee && Callee->arg_size() > unsigned(ArgNo))
749     return Callee->getArg(ArgNo);
750 
751   return nullptr;
752 }
753 
754 ChangeStatus AbstractAttribute::update(Attributor &A) {
755   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
756   if (getState().isAtFixpoint())
757     return HasChanged;
758 
759   LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
760 
761   HasChanged = updateImpl(A);
762 
763   LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
764                     << "\n");
765 
766   return HasChanged;
767 }
768 
769 ChangeStatus
770 IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
771                                    const ArrayRef<Attribute> &DeducedAttrs,
772                                    bool ForceReplace) {
773   Function *ScopeFn = IRP.getAnchorScope();
774   IRPosition::Kind PK = IRP.getPositionKind();
775 
776   // In the following some generic code that will manifest attributes in
777   // DeducedAttrs if they improve the current IR. Due to the different
778   // annotation positions we use the underlying AttributeList interface.
779 
780   AttributeList Attrs;
781   switch (PK) {
782   case IRPosition::IRP_INVALID:
783   case IRPosition::IRP_FLOAT:
784     return ChangeStatus::UNCHANGED;
785   case IRPosition::IRP_ARGUMENT:
786   case IRPosition::IRP_FUNCTION:
787   case IRPosition::IRP_RETURNED:
788     Attrs = ScopeFn->getAttributes();
789     break;
790   case IRPosition::IRP_CALL_SITE:
791   case IRPosition::IRP_CALL_SITE_RETURNED:
792   case IRPosition::IRP_CALL_SITE_ARGUMENT:
793     Attrs = cast<CallBase>(IRP.getAnchorValue()).getAttributes();
794     break;
795   }
796 
797   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
798   LLVMContext &Ctx = IRP.getAnchorValue().getContext();
799   for (const Attribute &Attr : DeducedAttrs) {
800     if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx(), ForceReplace))
801       continue;
802 
803     HasChanged = ChangeStatus::CHANGED;
804   }
805 
806   if (HasChanged == ChangeStatus::UNCHANGED)
807     return HasChanged;
808 
809   switch (PK) {
810   case IRPosition::IRP_ARGUMENT:
811   case IRPosition::IRP_FUNCTION:
812   case IRPosition::IRP_RETURNED:
813     ScopeFn->setAttributes(Attrs);
814     break;
815   case IRPosition::IRP_CALL_SITE:
816   case IRPosition::IRP_CALL_SITE_RETURNED:
817   case IRPosition::IRP_CALL_SITE_ARGUMENT:
818     cast<CallBase>(IRP.getAnchorValue()).setAttributes(Attrs);
819     break;
820   case IRPosition::IRP_INVALID:
821   case IRPosition::IRP_FLOAT:
822     break;
823   }
824 
825   return HasChanged;
826 }
827 
828 const IRPosition IRPosition::EmptyKey(DenseMapInfo<void *>::getEmptyKey());
829 const IRPosition
830     IRPosition::TombstoneKey(DenseMapInfo<void *>::getTombstoneKey());
831 
832 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
833   IRPositions.emplace_back(IRP);
834 
835   // Helper to determine if operand bundles on a call site are benin or
836   // potentially problematic. We handle only llvm.assume for now.
837   auto CanIgnoreOperandBundles = [](const CallBase &CB) {
838     return (isa<IntrinsicInst>(CB) &&
839             cast<IntrinsicInst>(CB).getIntrinsicID() == Intrinsic ::assume);
840   };
841 
842   const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue());
843   switch (IRP.getPositionKind()) {
844   case IRPosition::IRP_INVALID:
845   case IRPosition::IRP_FLOAT:
846   case IRPosition::IRP_FUNCTION:
847     return;
848   case IRPosition::IRP_ARGUMENT:
849   case IRPosition::IRP_RETURNED:
850     IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope()));
851     return;
852   case IRPosition::IRP_CALL_SITE:
853     assert(CB && "Expected call site!");
854     // TODO: We need to look at the operand bundles similar to the redirection
855     //       in CallBase.
856     if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB))
857       if (const Function *Callee = CB->getCalledFunction())
858         IRPositions.emplace_back(IRPosition::function(*Callee));
859     return;
860   case IRPosition::IRP_CALL_SITE_RETURNED:
861     assert(CB && "Expected call site!");
862     // TODO: We need to look at the operand bundles similar to the redirection
863     //       in CallBase.
864     if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
865       if (const Function *Callee = CB->getCalledFunction()) {
866         IRPositions.emplace_back(IRPosition::returned(*Callee));
867         IRPositions.emplace_back(IRPosition::function(*Callee));
868         for (const Argument &Arg : Callee->args())
869           if (Arg.hasReturnedAttr()) {
870             IRPositions.emplace_back(
871                 IRPosition::callsite_argument(*CB, Arg.getArgNo()));
872             IRPositions.emplace_back(
873                 IRPosition::value(*CB->getArgOperand(Arg.getArgNo())));
874             IRPositions.emplace_back(IRPosition::argument(Arg));
875           }
876       }
877     }
878     IRPositions.emplace_back(IRPosition::callsite_function(*CB));
879     return;
880   case IRPosition::IRP_CALL_SITE_ARGUMENT: {
881     assert(CB && "Expected call site!");
882     // TODO: We need to look at the operand bundles similar to the redirection
883     //       in CallBase.
884     if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
885       const Function *Callee = CB->getCalledFunction();
886       if (Callee) {
887         if (Argument *Arg = IRP.getAssociatedArgument())
888           IRPositions.emplace_back(IRPosition::argument(*Arg));
889         IRPositions.emplace_back(IRPosition::function(*Callee));
890       }
891     }
892     IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
893     return;
894   }
895   }
896 }
897 
898 bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs,
899                          bool IgnoreSubsumingPositions, Attributor *A) const {
900   SmallVector<Attribute, 4> Attrs;
901   for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
902     for (Attribute::AttrKind AK : AKs)
903       if (EquivIRP.getAttrsFromIRAttr(AK, Attrs))
904         return true;
905     // The first position returned by the SubsumingPositionIterator is
906     // always the position itself. If we ignore subsuming positions we
907     // are done after the first iteration.
908     if (IgnoreSubsumingPositions)
909       break;
910   }
911   if (A)
912     for (Attribute::AttrKind AK : AKs)
913       if (getAttrsFromAssumes(AK, Attrs, *A))
914         return true;
915   return false;
916 }
917 
918 void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
919                           SmallVectorImpl<Attribute> &Attrs,
920                           bool IgnoreSubsumingPositions, Attributor *A) const {
921   for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
922     for (Attribute::AttrKind AK : AKs)
923       EquivIRP.getAttrsFromIRAttr(AK, Attrs);
924     // The first position returned by the SubsumingPositionIterator is
925     // always the position itself. If we ignore subsuming positions we
926     // are done after the first iteration.
927     if (IgnoreSubsumingPositions)
928       break;
929   }
930   if (A)
931     for (Attribute::AttrKind AK : AKs)
932       getAttrsFromAssumes(AK, Attrs, *A);
933 }
934 
935 bool IRPosition::getAttrsFromIRAttr(Attribute::AttrKind AK,
936                                     SmallVectorImpl<Attribute> &Attrs) const {
937   if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT)
938     return false;
939 
940   AttributeList AttrList;
941   if (const auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
942     AttrList = CB->getAttributes();
943   else
944     AttrList = getAssociatedFunction()->getAttributes();
945 
946   bool HasAttr = AttrList.hasAttributeAtIndex(getAttrIdx(), AK);
947   if (HasAttr)
948     Attrs.push_back(AttrList.getAttributeAtIndex(getAttrIdx(), AK));
949   return HasAttr;
950 }
951 
952 bool IRPosition::getAttrsFromAssumes(Attribute::AttrKind AK,
953                                      SmallVectorImpl<Attribute> &Attrs,
954                                      Attributor &A) const {
955   assert(getPositionKind() != IRP_INVALID && "Did expect a valid position!");
956   Value &AssociatedValue = getAssociatedValue();
957 
958   const Assume2KnowledgeMap &A2K =
959       A.getInfoCache().getKnowledgeMap().lookup({&AssociatedValue, AK});
960 
961   // Check if we found any potential assume use, if not we don't need to create
962   // explorer iterators.
963   if (A2K.empty())
964     return false;
965 
966   LLVMContext &Ctx = AssociatedValue.getContext();
967   unsigned AttrsSize = Attrs.size();
968   MustBeExecutedContextExplorer &Explorer =
969       A.getInfoCache().getMustBeExecutedContextExplorer();
970   auto EIt = Explorer.begin(getCtxI()), EEnd = Explorer.end(getCtxI());
971   for (auto &It : A2K)
972     if (Explorer.findInContextOf(It.first, EIt, EEnd))
973       Attrs.push_back(Attribute::get(Ctx, AK, It.second.Max));
974   return AttrsSize != Attrs.size();
975 }
976 
977 void IRPosition::verify() {
978 #ifdef EXPENSIVE_CHECKS
979   switch (getPositionKind()) {
980   case IRP_INVALID:
981     assert((CBContext == nullptr) &&
982            "Invalid position must not have CallBaseContext!");
983     assert(!Enc.getOpaqueValue() &&
984            "Expected a nullptr for an invalid position!");
985     return;
986   case IRP_FLOAT:
987     assert((!isa<Argument>(&getAssociatedValue())) &&
988            "Expected specialized kind for argument values!");
989     return;
990   case IRP_RETURNED:
991     assert(isa<Function>(getAsValuePtr()) &&
992            "Expected function for a 'returned' position!");
993     assert(getAsValuePtr() == &getAssociatedValue() &&
994            "Associated value mismatch!");
995     return;
996   case IRP_CALL_SITE_RETURNED:
997     assert((CBContext == nullptr) &&
998            "'call site returned' position must not have CallBaseContext!");
999     assert((isa<CallBase>(getAsValuePtr())) &&
1000            "Expected call base for 'call site returned' position!");
1001     assert(getAsValuePtr() == &getAssociatedValue() &&
1002            "Associated value mismatch!");
1003     return;
1004   case IRP_CALL_SITE:
1005     assert((CBContext == nullptr) &&
1006            "'call site function' position must not have CallBaseContext!");
1007     assert((isa<CallBase>(getAsValuePtr())) &&
1008            "Expected call base for 'call site function' position!");
1009     assert(getAsValuePtr() == &getAssociatedValue() &&
1010            "Associated value mismatch!");
1011     return;
1012   case IRP_FUNCTION:
1013     assert(isa<Function>(getAsValuePtr()) &&
1014            "Expected function for a 'function' position!");
1015     assert(getAsValuePtr() == &getAssociatedValue() &&
1016            "Associated value mismatch!");
1017     return;
1018   case IRP_ARGUMENT:
1019     assert(isa<Argument>(getAsValuePtr()) &&
1020            "Expected argument for a 'argument' position!");
1021     assert(getAsValuePtr() == &getAssociatedValue() &&
1022            "Associated value mismatch!");
1023     return;
1024   case IRP_CALL_SITE_ARGUMENT: {
1025     assert((CBContext == nullptr) &&
1026            "'call site argument' position must not have CallBaseContext!");
1027     Use *U = getAsUsePtr();
1028     (void)U; // Silence unused variable warning.
1029     assert(U && "Expected use for a 'call site argument' position!");
1030     assert(isa<CallBase>(U->getUser()) &&
1031            "Expected call base user for a 'call site argument' position!");
1032     assert(cast<CallBase>(U->getUser())->isArgOperand(U) &&
1033            "Expected call base argument operand for a 'call site argument' "
1034            "position");
1035     assert(cast<CallBase>(U->getUser())->getArgOperandNo(U) ==
1036                unsigned(getCallSiteArgNo()) &&
1037            "Argument number mismatch!");
1038     assert(U->get() == &getAssociatedValue() && "Associated value mismatch!");
1039     return;
1040   }
1041   }
1042 #endif
1043 }
1044 
1045 Optional<Constant *>
1046 Attributor::getAssumedConstant(const IRPosition &IRP,
1047                                const AbstractAttribute &AA,
1048                                bool &UsedAssumedInformation) {
1049   // First check all callbacks provided by outside AAs. If any of them returns
1050   // a non-null value that is different from the associated value, or None, we
1051   // assume it's simplified.
1052   for (auto &CB : SimplificationCallbacks.lookup(IRP)) {
1053     Optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation);
1054     if (!SimplifiedV)
1055       return llvm::None;
1056     if (isa_and_nonnull<Constant>(*SimplifiedV))
1057       return cast<Constant>(*SimplifiedV);
1058     return nullptr;
1059   }
1060   if (auto *C = dyn_cast<Constant>(&IRP.getAssociatedValue()))
1061     return C;
1062   SmallVector<AA::ValueAndContext> Values;
1063   if (getAssumedSimplifiedValues(IRP, &AA, Values,
1064                                  AA::ValueScope::Interprocedural,
1065                                  UsedAssumedInformation)) {
1066     if (Values.empty())
1067       return llvm::None;
1068     if (auto *C = dyn_cast_or_null<Constant>(
1069             AAPotentialValues::getSingleValue(*this, AA, IRP, Values)))
1070       return C;
1071   }
1072   return nullptr;
1073 }
1074 
1075 Optional<Value *> Attributor::getAssumedSimplified(const IRPosition &IRP,
1076                                                    const AbstractAttribute *AA,
1077                                                    bool &UsedAssumedInformation,
1078                                                    AA::ValueScope S) {
1079   // First check all callbacks provided by outside AAs. If any of them returns
1080   // a non-null value that is different from the associated value, or None, we
1081   // assume it's simplified.
1082   for (auto &CB : SimplificationCallbacks.lookup(IRP))
1083     return CB(IRP, AA, UsedAssumedInformation);
1084 
1085   SmallVector<AA::ValueAndContext> Values;
1086   if (!getAssumedSimplifiedValues(IRP, AA, Values, S, UsedAssumedInformation))
1087     return &IRP.getAssociatedValue();
1088   if (Values.empty())
1089     return llvm::None;
1090   if (AA)
1091     if (Value *V = AAPotentialValues::getSingleValue(*this, *AA, IRP, Values))
1092       return V;
1093   if (IRP.getPositionKind() == IRPosition::IRP_RETURNED ||
1094       IRP.getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED)
1095     return nullptr;
1096   return &IRP.getAssociatedValue();
1097 }
1098 
1099 bool Attributor::getAssumedSimplifiedValues(
1100     const IRPosition &IRP, const AbstractAttribute *AA,
1101     SmallVectorImpl<AA::ValueAndContext> &Values, AA::ValueScope S,
1102     bool &UsedAssumedInformation) {
1103   // First check all callbacks provided by outside AAs. If any of them returns
1104   // a non-null value that is different from the associated value, or None, we
1105   // assume it's simplified.
1106   const auto &SimplificationCBs = SimplificationCallbacks.lookup(IRP);
1107   for (auto &CB : SimplificationCBs) {
1108     Optional<Value *> CBResult = CB(IRP, AA, UsedAssumedInformation);
1109     if (!CBResult.hasValue())
1110       continue;
1111     Value *V = CBResult.getValue();
1112     if (!V)
1113       return false;
1114     if ((S & AA::ValueScope::Interprocedural) ||
1115         AA::isValidInScope(*V, IRP.getAnchorScope()))
1116       Values.push_back(AA::ValueAndContext{*V, nullptr});
1117     else
1118       return false;
1119   }
1120   if (!SimplificationCBs.empty())
1121     return true;
1122 
1123   // If no high-level/outside simplification occurred, use AAPotentialValues.
1124   const auto &PotentialValuesAA =
1125       getOrCreateAAFor<AAPotentialValues>(IRP, AA, DepClassTy::OPTIONAL);
1126   if (!PotentialValuesAA.getAssumedSimplifiedValues(*this, Values, S))
1127     return false;
1128   UsedAssumedInformation |= !PotentialValuesAA.isAtFixpoint();
1129   return true;
1130 }
1131 
1132 Optional<Value *> Attributor::translateArgumentToCallSiteContent(
1133     Optional<Value *> V, CallBase &CB, const AbstractAttribute &AA,
1134     bool &UsedAssumedInformation) {
1135   if (!V)
1136     return V;
1137   if (*V == nullptr || isa<Constant>(*V))
1138     return V;
1139   if (auto *Arg = dyn_cast<Argument>(*V))
1140     if (CB.getCalledFunction() == Arg->getParent())
1141       if (!Arg->hasPointeeInMemoryValueAttr())
1142         return getAssumedSimplified(
1143             IRPosition::callsite_argument(CB, Arg->getArgNo()), AA,
1144             UsedAssumedInformation, AA::Intraprocedural);
1145   return nullptr;
1146 }
1147 
1148 Attributor::~Attributor() {
1149   // The abstract attributes are allocated via the BumpPtrAllocator Allocator,
1150   // thus we cannot delete them. We can, and want to, destruct them though.
1151   for (auto &DepAA : DG.SyntheticRoot.Deps) {
1152     AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
1153     AA->~AbstractAttribute();
1154   }
1155 }
1156 
1157 bool Attributor::isAssumedDead(const AbstractAttribute &AA,
1158                                const AAIsDead *FnLivenessAA,
1159                                bool &UsedAssumedInformation,
1160                                bool CheckBBLivenessOnly, DepClassTy DepClass) {
1161   const IRPosition &IRP = AA.getIRPosition();
1162   if (!Functions.count(IRP.getAnchorScope()))
1163     return false;
1164   return isAssumedDead(IRP, &AA, FnLivenessAA, UsedAssumedInformation,
1165                        CheckBBLivenessOnly, DepClass);
1166 }
1167 
1168 bool Attributor::isAssumedDead(const Use &U,
1169                                const AbstractAttribute *QueryingAA,
1170                                const AAIsDead *FnLivenessAA,
1171                                bool &UsedAssumedInformation,
1172                                bool CheckBBLivenessOnly, DepClassTy DepClass) {
1173   Instruction *UserI = dyn_cast<Instruction>(U.getUser());
1174   if (!UserI)
1175     return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
1176                          UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1177 
1178   if (auto *CB = dyn_cast<CallBase>(UserI)) {
1179     // For call site argument uses we can check if the argument is
1180     // unused/dead.
1181     if (CB->isArgOperand(&U)) {
1182       const IRPosition &CSArgPos =
1183           IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
1184       return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
1185                            UsedAssumedInformation, CheckBBLivenessOnly,
1186                            DepClass);
1187     }
1188   } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
1189     const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
1190     return isAssumedDead(RetPos, QueryingAA, FnLivenessAA,
1191                          UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1192   } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
1193     BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
1194     return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
1195                          UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1196   } else if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) {
1197     if (!CheckBBLivenessOnly && SI->getPointerOperand() != U.get()) {
1198       const IRPosition IRP = IRPosition::inst(*SI);
1199       const AAIsDead &IsDeadAA =
1200           getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
1201       if (IsDeadAA.isRemovableStore()) {
1202         if (QueryingAA)
1203           recordDependence(IsDeadAA, *QueryingAA, DepClass);
1204         if (!IsDeadAA.isKnown(AAIsDead::IS_REMOVABLE))
1205           UsedAssumedInformation = true;
1206         return true;
1207       }
1208     }
1209   }
1210 
1211   return isAssumedDead(IRPosition::inst(*UserI), QueryingAA, FnLivenessAA,
1212                        UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1213 }
1214 
1215 bool Attributor::isAssumedDead(const Instruction &I,
1216                                const AbstractAttribute *QueryingAA,
1217                                const AAIsDead *FnLivenessAA,
1218                                bool &UsedAssumedInformation,
1219                                bool CheckBBLivenessOnly, DepClassTy DepClass) {
1220   const IRPosition::CallBaseContext *CBCtx =
1221       QueryingAA ? QueryingAA->getCallBaseContext() : nullptr;
1222 
1223   if (ManifestAddedBlocks.contains(I.getParent()))
1224     return false;
1225 
1226   if (!FnLivenessAA)
1227     FnLivenessAA =
1228         lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction(), CBCtx),
1229                               QueryingAA, DepClassTy::NONE);
1230 
1231   // If we have a context instruction and a liveness AA we use it.
1232   if (FnLivenessAA &&
1233       FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() &&
1234       (CheckBBLivenessOnly ? FnLivenessAA->isAssumedDead(I.getParent())
1235                            : FnLivenessAA->isAssumedDead(&I))) {
1236     if (QueryingAA)
1237       recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
1238     if (!FnLivenessAA->isKnownDead(&I))
1239       UsedAssumedInformation = true;
1240     return true;
1241   }
1242 
1243   if (CheckBBLivenessOnly)
1244     return false;
1245 
1246   const IRPosition IRP = IRPosition::inst(I, CBCtx);
1247   const AAIsDead &IsDeadAA =
1248       getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
1249   // Don't check liveness for AAIsDead.
1250   if (QueryingAA == &IsDeadAA)
1251     return false;
1252 
1253   if (IsDeadAA.isAssumedDead()) {
1254     if (QueryingAA)
1255       recordDependence(IsDeadAA, *QueryingAA, DepClass);
1256     if (!IsDeadAA.isKnownDead())
1257       UsedAssumedInformation = true;
1258     return true;
1259   }
1260 
1261   return false;
1262 }
1263 
1264 bool Attributor::isAssumedDead(const IRPosition &IRP,
1265                                const AbstractAttribute *QueryingAA,
1266                                const AAIsDead *FnLivenessAA,
1267                                bool &UsedAssumedInformation,
1268                                bool CheckBBLivenessOnly, DepClassTy DepClass) {
1269   Instruction *CtxI = IRP.getCtxI();
1270   if (CtxI &&
1271       isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, UsedAssumedInformation,
1272                     /* CheckBBLivenessOnly */ true,
1273                     CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
1274     return true;
1275 
1276   if (CheckBBLivenessOnly)
1277     return false;
1278 
1279   // If we haven't succeeded we query the specific liveness info for the IRP.
1280   const AAIsDead *IsDeadAA;
1281   if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE)
1282     IsDeadAA = &getOrCreateAAFor<AAIsDead>(
1283         IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())),
1284         QueryingAA, DepClassTy::NONE);
1285   else
1286     IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
1287   // Don't check liveness for AAIsDead.
1288   if (QueryingAA == IsDeadAA)
1289     return false;
1290 
1291   if (IsDeadAA->isAssumedDead()) {
1292     if (QueryingAA)
1293       recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1294     if (!IsDeadAA->isKnownDead())
1295       UsedAssumedInformation = true;
1296     return true;
1297   }
1298 
1299   return false;
1300 }
1301 
1302 bool Attributor::isAssumedDead(const BasicBlock &BB,
1303                                const AbstractAttribute *QueryingAA,
1304                                const AAIsDead *FnLivenessAA,
1305                                DepClassTy DepClass) {
1306   if (!FnLivenessAA)
1307     FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*BB.getParent()),
1308                                          QueryingAA, DepClassTy::NONE);
1309   if (FnLivenessAA->isAssumedDead(&BB)) {
1310     if (QueryingAA)
1311       recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
1312     return true;
1313   }
1314 
1315   return false;
1316 }
1317 
1318 bool Attributor::checkForAllUses(
1319     function_ref<bool(const Use &, bool &)> Pred,
1320     const AbstractAttribute &QueryingAA, const Value &V,
1321     bool CheckBBLivenessOnly, DepClassTy LivenessDepClass,
1322     bool IgnoreDroppableUses,
1323     function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB) {
1324 
1325   // Check the trivial case first as it catches void values.
1326   if (V.use_empty())
1327     return true;
1328 
1329   const IRPosition &IRP = QueryingAA.getIRPosition();
1330   SmallVector<const Use *, 16> Worklist;
1331   SmallPtrSet<const Use *, 16> Visited;
1332 
1333   for (const Use &U : V.uses())
1334     Worklist.push_back(&U);
1335 
1336   LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
1337                     << " initial uses to check\n");
1338 
1339   const Function *ScopeFn = IRP.getAnchorScope();
1340   const auto *LivenessAA =
1341       ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
1342                                     DepClassTy::NONE)
1343               : nullptr;
1344 
1345   while (!Worklist.empty()) {
1346     const Use *U = Worklist.pop_back_val();
1347     if (isa<PHINode>(U->getUser()) && !Visited.insert(U).second)
1348       continue;
1349     LLVM_DEBUG({
1350       if (auto *Fn = dyn_cast<Function>(U->getUser()))
1351         dbgs() << "[Attributor] Check use: " << **U << " in " << Fn->getName()
1352                << "\n";
1353       else
1354         dbgs() << "[Attributor] Check use: " << **U << " in " << *U->getUser()
1355                << "\n";
1356     });
1357     bool UsedAssumedInformation = false;
1358     if (isAssumedDead(*U, &QueryingAA, LivenessAA, UsedAssumedInformation,
1359                       CheckBBLivenessOnly, LivenessDepClass)) {
1360       LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
1361       continue;
1362     }
1363     if (IgnoreDroppableUses && U->getUser()->isDroppable()) {
1364       LLVM_DEBUG(dbgs() << "[Attributor] Droppable user, skip!\n");
1365       continue;
1366     }
1367 
1368     if (auto *SI = dyn_cast<StoreInst>(U->getUser())) {
1369       if (&SI->getOperandUse(0) == U) {
1370         if (!Visited.insert(U).second)
1371           continue;
1372         SmallSetVector<Value *, 4> PotentialCopies;
1373         if (AA::getPotentialCopiesOfStoredValue(
1374                 *this, *SI, PotentialCopies, QueryingAA, UsedAssumedInformation,
1375                 /* OnlyExact */ true)) {
1376           LLVM_DEBUG(dbgs() << "[Attributor] Value is stored, continue with "
1377                             << PotentialCopies.size()
1378                             << " potential copies instead!\n");
1379           for (Value *PotentialCopy : PotentialCopies)
1380             for (const Use &CopyUse : PotentialCopy->uses()) {
1381               if (EquivalentUseCB && !EquivalentUseCB(*U, CopyUse)) {
1382                 LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
1383                                      "rejected by the equivalence call back: "
1384                                   << *CopyUse << "!\n");
1385                 return false;
1386               }
1387               Worklist.push_back(&CopyUse);
1388             }
1389           continue;
1390         }
1391       }
1392     }
1393 
1394     bool Follow = false;
1395     if (!Pred(*U, Follow))
1396       return false;
1397     if (!Follow)
1398       continue;
1399     for (const Use &UU : U->getUser()->uses())
1400       Worklist.push_back(&UU);
1401   }
1402 
1403   return true;
1404 }
1405 
1406 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
1407                                       const AbstractAttribute &QueryingAA,
1408                                       bool RequireAllCallSites,
1409                                       bool &UsedAssumedInformation) {
1410   // We can try to determine information from
1411   // the call sites. However, this is only possible all call sites are known,
1412   // hence the function has internal linkage.
1413   const IRPosition &IRP = QueryingAA.getIRPosition();
1414   const Function *AssociatedFunction = IRP.getAssociatedFunction();
1415   if (!AssociatedFunction) {
1416     LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
1417                       << "\n");
1418     return false;
1419   }
1420 
1421   return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
1422                               &QueryingAA, UsedAssumedInformation);
1423 }
1424 
1425 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
1426                                       const Function &Fn,
1427                                       bool RequireAllCallSites,
1428                                       const AbstractAttribute *QueryingAA,
1429                                       bool &UsedAssumedInformation) {
1430   if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
1431     LLVM_DEBUG(
1432         dbgs()
1433         << "[Attributor] Function " << Fn.getName()
1434         << " has no internal linkage, hence not all call sites are known\n");
1435     return false;
1436   }
1437 
1438   SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses()));
1439   for (unsigned u = 0; u < Uses.size(); ++u) {
1440     const Use &U = *Uses[u];
1441     LLVM_DEBUG({
1442       if (auto *Fn = dyn_cast<Function>(U))
1443         dbgs() << "[Attributor] Check use: " << Fn->getName() << " in "
1444                << *U.getUser() << "\n";
1445       else
1446         dbgs() << "[Attributor] Check use: " << *U << " in " << *U.getUser()
1447                << "\n";
1448     });
1449     if (isAssumedDead(U, QueryingAA, nullptr, UsedAssumedInformation,
1450                       /* CheckBBLivenessOnly */ true)) {
1451       LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
1452       continue;
1453     }
1454     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
1455       if (CE->isCast() && CE->getType()->isPointerTy()) {
1456         LLVM_DEBUG(
1457             dbgs() << "[Attributor] Use, is constant cast expression, add "
1458                    << CE->getNumUses()
1459                    << " uses of that expression instead!\n");
1460         for (const Use &CEU : CE->uses())
1461           Uses.push_back(&CEU);
1462         continue;
1463       }
1464     }
1465 
1466     AbstractCallSite ACS(&U);
1467     if (!ACS) {
1468       LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
1469                         << " has non call site use " << *U.get() << " in "
1470                         << *U.getUser() << "\n");
1471       // BlockAddress users are allowed.
1472       if (isa<BlockAddress>(U.getUser()))
1473         continue;
1474       return false;
1475     }
1476 
1477     const Use *EffectiveUse =
1478         ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
1479     if (!ACS.isCallee(EffectiveUse)) {
1480       if (!RequireAllCallSites) {
1481         LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
1482                           << " is not a call of " << Fn.getName()
1483                           << ", skip use\n");
1484         continue;
1485       }
1486       LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
1487                         << " is an invalid use of " << Fn.getName() << "\n");
1488       return false;
1489     }
1490 
1491     // Make sure the arguments that can be matched between the call site and the
1492     // callee argee on their type. It is unlikely they do not and it doesn't
1493     // make sense for all attributes to know/care about this.
1494     assert(&Fn == ACS.getCalledFunction() && "Expected known callee");
1495     unsigned MinArgsParams =
1496         std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
1497     for (unsigned u = 0; u < MinArgsParams; ++u) {
1498       Value *CSArgOp = ACS.getCallArgOperand(u);
1499       if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
1500         LLVM_DEBUG(
1501             dbgs() << "[Attributor] Call site / callee argument type mismatch ["
1502                    << u << "@" << Fn.getName() << ": "
1503                    << *Fn.getArg(u)->getType() << " vs. "
1504                    << *ACS.getCallArgOperand(u)->getType() << "\n");
1505         return false;
1506       }
1507     }
1508 
1509     if (Pred(ACS))
1510       continue;
1511 
1512     LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
1513                       << *ACS.getInstruction() << "\n");
1514     return false;
1515   }
1516 
1517   return true;
1518 }
1519 
1520 bool Attributor::shouldPropagateCallBaseContext(const IRPosition &IRP) {
1521   // TODO: Maintain a cache of Values that are
1522   // on the pathway from a Argument to a Instruction that would effect the
1523   // liveness/return state etc.
1524   return EnableCallSiteSpecific;
1525 }
1526 
1527 bool Attributor::checkForAllReturnedValuesAndReturnInsts(
1528     function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred,
1529     const AbstractAttribute &QueryingAA) {
1530 
1531   const IRPosition &IRP = QueryingAA.getIRPosition();
1532   // Since we need to provide return instructions we have to have an exact
1533   // definition.
1534   const Function *AssociatedFunction = IRP.getAssociatedFunction();
1535   if (!AssociatedFunction)
1536     return false;
1537 
1538   // If this is a call site query we use the call site specific return values
1539   // and liveness information.
1540   // TODO: use the function scope once we have call site AAReturnedValues.
1541   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
1542   const auto &AARetVal =
1543       getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED);
1544   if (!AARetVal.getState().isValidState())
1545     return false;
1546 
1547   return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
1548 }
1549 
1550 bool Attributor::checkForAllReturnedValues(
1551     function_ref<bool(Value &)> Pred, const AbstractAttribute &QueryingAA) {
1552 
1553   const IRPosition &IRP = QueryingAA.getIRPosition();
1554   const Function *AssociatedFunction = IRP.getAssociatedFunction();
1555   if (!AssociatedFunction)
1556     return false;
1557 
1558   // TODO: use the function scope once we have call site AAReturnedValues.
1559   const IRPosition &QueryIRP = IRPosition::function(
1560       *AssociatedFunction, QueryingAA.getCallBaseContext());
1561   const auto &AARetVal =
1562       getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED);
1563   if (!AARetVal.getState().isValidState())
1564     return false;
1565 
1566   return AARetVal.checkForAllReturnedValuesAndReturnInsts(
1567       [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
1568         return Pred(RV);
1569       });
1570 }
1571 
1572 static bool checkForAllInstructionsImpl(
1573     Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap,
1574     function_ref<bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA,
1575     const AAIsDead *LivenessAA, const ArrayRef<unsigned> &Opcodes,
1576     bool &UsedAssumedInformation, bool CheckBBLivenessOnly = false,
1577     bool CheckPotentiallyDead = false) {
1578   for (unsigned Opcode : Opcodes) {
1579     // Check if we have instructions with this opcode at all first.
1580     auto *Insts = OpcodeInstMap.lookup(Opcode);
1581     if (!Insts)
1582       continue;
1583 
1584     for (Instruction *I : *Insts) {
1585       // Skip dead instructions.
1586       if (A && !CheckPotentiallyDead &&
1587           A->isAssumedDead(IRPosition::inst(*I), QueryingAA, LivenessAA,
1588                            UsedAssumedInformation, CheckBBLivenessOnly)) {
1589         LLVM_DEBUG(dbgs() << "[Attributor] Instruction " << *I
1590                           << " is potentially dead, skip!\n";);
1591         continue;
1592       }
1593 
1594       if (!Pred(*I))
1595         return false;
1596     }
1597   }
1598   return true;
1599 }
1600 
1601 bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
1602                                          const Function *Fn,
1603                                          const AbstractAttribute &QueryingAA,
1604                                          const ArrayRef<unsigned> &Opcodes,
1605                                          bool &UsedAssumedInformation,
1606                                          bool CheckBBLivenessOnly,
1607                                          bool CheckPotentiallyDead) {
1608   // Since we need to provide instructions we have to have an exact definition.
1609   if (!Fn || Fn->isDeclaration())
1610     return false;
1611 
1612   // TODO: use the function scope once we have call site AAReturnedValues.
1613   const IRPosition &QueryIRP = IRPosition::function(*Fn);
1614   const auto *LivenessAA =
1615       (CheckBBLivenessOnly || CheckPotentiallyDead)
1616           ? nullptr
1617           : &(getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE));
1618 
1619   auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
1620   if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA,
1621                                    LivenessAA, Opcodes, UsedAssumedInformation,
1622                                    CheckBBLivenessOnly, CheckPotentiallyDead))
1623     return false;
1624 
1625   return true;
1626 }
1627 
1628 bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
1629                                          const AbstractAttribute &QueryingAA,
1630                                          const ArrayRef<unsigned> &Opcodes,
1631                                          bool &UsedAssumedInformation,
1632                                          bool CheckBBLivenessOnly,
1633                                          bool CheckPotentiallyDead) {
1634   const IRPosition &IRP = QueryingAA.getIRPosition();
1635   const Function *AssociatedFunction = IRP.getAssociatedFunction();
1636   return checkForAllInstructions(Pred, AssociatedFunction, QueryingAA, Opcodes,
1637                                  UsedAssumedInformation, CheckBBLivenessOnly,
1638                                  CheckPotentiallyDead);
1639 }
1640 
1641 bool Attributor::checkForAllReadWriteInstructions(
1642     function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA,
1643     bool &UsedAssumedInformation) {
1644 
1645   const Function *AssociatedFunction =
1646       QueryingAA.getIRPosition().getAssociatedFunction();
1647   if (!AssociatedFunction)
1648     return false;
1649 
1650   // TODO: use the function scope once we have call site AAReturnedValues.
1651   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
1652   const auto &LivenessAA =
1653       getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE);
1654 
1655   for (Instruction *I :
1656        InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
1657     // Skip dead instructions.
1658     if (isAssumedDead(IRPosition::inst(*I), &QueryingAA, &LivenessAA,
1659                       UsedAssumedInformation))
1660       continue;
1661 
1662     if (!Pred(*I))
1663       return false;
1664   }
1665 
1666   return true;
1667 }
1668 
1669 void Attributor::runTillFixpoint() {
1670   TimeTraceScope TimeScope("Attributor::runTillFixpoint");
1671   LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
1672                     << DG.SyntheticRoot.Deps.size()
1673                     << " abstract attributes.\n");
1674 
1675   // Now that all abstract attributes are collected and initialized we start
1676   // the abstract analysis.
1677 
1678   unsigned IterationCounter = 1;
1679   unsigned MaxIterations =
1680       Configuration.MaxFixpointIterations.value_or(SetFixpointIterations);
1681 
1682   SmallVector<AbstractAttribute *, 32> ChangedAAs;
1683   SetVector<AbstractAttribute *> Worklist, InvalidAAs;
1684   Worklist.insert(DG.SyntheticRoot.begin(), DG.SyntheticRoot.end());
1685 
1686   do {
1687     // Remember the size to determine new attributes.
1688     size_t NumAAs = DG.SyntheticRoot.Deps.size();
1689     LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
1690                       << ", Worklist size: " << Worklist.size() << "\n");
1691 
1692     // For invalid AAs we can fix dependent AAs that have a required dependence,
1693     // thereby folding long dependence chains in a single step without the need
1694     // to run updates.
1695     for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
1696       AbstractAttribute *InvalidAA = InvalidAAs[u];
1697 
1698       // Check the dependences to fast track invalidation.
1699       LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has "
1700                         << InvalidAA->Deps.size()
1701                         << " required & optional dependences\n");
1702       while (!InvalidAA->Deps.empty()) {
1703         const auto &Dep = InvalidAA->Deps.back();
1704         InvalidAA->Deps.pop_back();
1705         AbstractAttribute *DepAA = cast<AbstractAttribute>(Dep.getPointer());
1706         if (Dep.getInt() == unsigned(DepClassTy::OPTIONAL)) {
1707           LLVM_DEBUG(dbgs() << " - recompute: " << *DepAA);
1708           Worklist.insert(DepAA);
1709           continue;
1710         }
1711         LLVM_DEBUG(dbgs() << " - invalidate: " << *DepAA);
1712         DepAA->getState().indicatePessimisticFixpoint();
1713         assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!");
1714         if (!DepAA->getState().isValidState())
1715           InvalidAAs.insert(DepAA);
1716         else
1717           ChangedAAs.push_back(DepAA);
1718       }
1719     }
1720 
1721     // Add all abstract attributes that are potentially dependent on one that
1722     // changed to the work list.
1723     for (AbstractAttribute *ChangedAA : ChangedAAs)
1724       while (!ChangedAA->Deps.empty()) {
1725         Worklist.insert(
1726             cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer()));
1727         ChangedAA->Deps.pop_back();
1728       }
1729 
1730     LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
1731                       << ", Worklist+Dependent size: " << Worklist.size()
1732                       << "\n");
1733 
1734     // Reset the changed and invalid set.
1735     ChangedAAs.clear();
1736     InvalidAAs.clear();
1737 
1738     // Update all abstract attribute in the work list and record the ones that
1739     // changed.
1740     for (AbstractAttribute *AA : Worklist) {
1741       const auto &AAState = AA->getState();
1742       if (!AAState.isAtFixpoint())
1743         if (updateAA(*AA) == ChangeStatus::CHANGED)
1744           ChangedAAs.push_back(AA);
1745 
1746       // Use the InvalidAAs vector to propagate invalid states fast transitively
1747       // without requiring updates.
1748       if (!AAState.isValidState())
1749         InvalidAAs.insert(AA);
1750     }
1751 
1752     // Add attributes to the changed set if they have been created in the last
1753     // iteration.
1754     ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs,
1755                       DG.SyntheticRoot.end());
1756 
1757     // Reset the work list and repopulate with the changed abstract attributes.
1758     // Note that dependent ones are added above.
1759     Worklist.clear();
1760     Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
1761     Worklist.insert(QueryAAsAwaitingUpdate.begin(),
1762                     QueryAAsAwaitingUpdate.end());
1763     QueryAAsAwaitingUpdate.clear();
1764 
1765   } while (!Worklist.empty() &&
1766            (IterationCounter++ < MaxIterations || VerifyMaxFixpointIterations));
1767 
1768   if (IterationCounter > MaxIterations && !Functions.empty()) {
1769     auto Remark = [&](OptimizationRemarkMissed ORM) {
1770       return ORM << "Attributor did not reach a fixpoint after "
1771                  << ore::NV("Iterations", MaxIterations) << " iterations.";
1772     };
1773     Function *F = Functions.front();
1774     emitRemark<OptimizationRemarkMissed>(F, "FixedPoint", Remark);
1775   }
1776 
1777   LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
1778                     << IterationCounter << "/" << MaxIterations
1779                     << " iterations\n");
1780 
1781   // Reset abstract arguments not settled in a sound fixpoint by now. This
1782   // happens when we stopped the fixpoint iteration early. Note that only the
1783   // ones marked as "changed" *and* the ones transitively depending on them
1784   // need to be reverted to a pessimistic state. Others might not be in a
1785   // fixpoint state but we can use the optimistic results for them anyway.
1786   SmallPtrSet<AbstractAttribute *, 32> Visited;
1787   for (unsigned u = 0; u < ChangedAAs.size(); u++) {
1788     AbstractAttribute *ChangedAA = ChangedAAs[u];
1789     if (!Visited.insert(ChangedAA).second)
1790       continue;
1791 
1792     AbstractState &State = ChangedAA->getState();
1793     if (!State.isAtFixpoint()) {
1794       State.indicatePessimisticFixpoint();
1795 
1796       NumAttributesTimedOut++;
1797     }
1798 
1799     while (!ChangedAA->Deps.empty()) {
1800       ChangedAAs.push_back(
1801           cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer()));
1802       ChangedAA->Deps.pop_back();
1803     }
1804   }
1805 
1806   LLVM_DEBUG({
1807     if (!Visited.empty())
1808       dbgs() << "\n[Attributor] Finalized " << Visited.size()
1809              << " abstract attributes.\n";
1810   });
1811 
1812   if (VerifyMaxFixpointIterations && IterationCounter != MaxIterations) {
1813     errs() << "\n[Attributor] Fixpoint iteration done after: "
1814            << IterationCounter << "/" << MaxIterations << " iterations\n";
1815     llvm_unreachable("The fixpoint was not reached with exactly the number of "
1816                      "specified iterations!");
1817   }
1818 }
1819 
1820 void Attributor::registerForUpdate(AbstractAttribute &AA) {
1821   assert(AA.isQueryAA() &&
1822          "Non-query AAs should not be required to register for updates!");
1823   QueryAAsAwaitingUpdate.insert(&AA);
1824 }
1825 
1826 ChangeStatus Attributor::manifestAttributes() {
1827   TimeTraceScope TimeScope("Attributor::manifestAttributes");
1828   size_t NumFinalAAs = DG.SyntheticRoot.Deps.size();
1829 
1830   unsigned NumManifested = 0;
1831   unsigned NumAtFixpoint = 0;
1832   ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
1833   for (auto &DepAA : DG.SyntheticRoot.Deps) {
1834     AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
1835     AbstractState &State = AA->getState();
1836 
1837     // If there is not already a fixpoint reached, we can now take the
1838     // optimistic state. This is correct because we enforced a pessimistic one
1839     // on abstract attributes that were transitively dependent on a changed one
1840     // already above.
1841     if (!State.isAtFixpoint())
1842       State.indicateOptimisticFixpoint();
1843 
1844     // We must not manifest Attributes that use Callbase info.
1845     if (AA->hasCallBaseContext())
1846       continue;
1847     // If the state is invalid, we do not try to manifest it.
1848     if (!State.isValidState())
1849       continue;
1850 
1851     if (AA->getCtxI() && !isRunOn(*AA->getAnchorScope()))
1852       continue;
1853 
1854     // Skip dead code.
1855     bool UsedAssumedInformation = false;
1856     if (isAssumedDead(*AA, nullptr, UsedAssumedInformation,
1857                       /* CheckBBLivenessOnly */ true))
1858       continue;
1859     // Check if the manifest debug counter that allows skipping manifestation of
1860     // AAs
1861     if (!DebugCounter::shouldExecute(ManifestDBGCounter))
1862       continue;
1863     // Manifest the state and record if we changed the IR.
1864     ChangeStatus LocalChange = AA->manifest(*this);
1865     if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
1866       AA->trackStatistics();
1867     LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA
1868                       << "\n");
1869 
1870     ManifestChange = ManifestChange | LocalChange;
1871 
1872     NumAtFixpoint++;
1873     NumManifested += (LocalChange == ChangeStatus::CHANGED);
1874   }
1875 
1876   (void)NumManifested;
1877   (void)NumAtFixpoint;
1878   LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
1879                     << " arguments while " << NumAtFixpoint
1880                     << " were in a valid fixpoint state\n");
1881 
1882   NumAttributesManifested += NumManifested;
1883   NumAttributesValidFixpoint += NumAtFixpoint;
1884 
1885   (void)NumFinalAAs;
1886   if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) {
1887     for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size(); ++u)
1888       errs() << "Unexpected abstract attribute: "
1889              << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer())
1890              << " :: "
1891              << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer())
1892                     ->getIRPosition()
1893                     .getAssociatedValue()
1894              << "\n";
1895     llvm_unreachable("Expected the final number of abstract attributes to "
1896                      "remain unchanged!");
1897   }
1898   return ManifestChange;
1899 }
1900 
1901 void Attributor::identifyDeadInternalFunctions() {
1902   // Early exit if we don't intend to delete functions.
1903   if (!Configuration.DeleteFns)
1904     return;
1905 
1906   // Identify dead internal functions and delete them. This happens outside
1907   // the other fixpoint analysis as we might treat potentially dead functions
1908   // as live to lower the number of iterations. If they happen to be dead, the
1909   // below fixpoint loop will identify and eliminate them.
1910   SmallVector<Function *, 8> InternalFns;
1911   for (Function *F : Functions)
1912     if (F->hasLocalLinkage())
1913       InternalFns.push_back(F);
1914 
1915   SmallPtrSet<Function *, 8> LiveInternalFns;
1916   bool FoundLiveInternal = true;
1917   while (FoundLiveInternal) {
1918     FoundLiveInternal = false;
1919     for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
1920       Function *F = InternalFns[u];
1921       if (!F)
1922         continue;
1923 
1924       bool UsedAssumedInformation = false;
1925       if (checkForAllCallSites(
1926               [&](AbstractCallSite ACS) {
1927                 Function *Callee = ACS.getInstruction()->getFunction();
1928                 return ToBeDeletedFunctions.count(Callee) ||
1929                        (Functions.count(Callee) && Callee->hasLocalLinkage() &&
1930                         !LiveInternalFns.count(Callee));
1931               },
1932               *F, true, nullptr, UsedAssumedInformation)) {
1933         continue;
1934       }
1935 
1936       LiveInternalFns.insert(F);
1937       InternalFns[u] = nullptr;
1938       FoundLiveInternal = true;
1939     }
1940   }
1941 
1942   for (unsigned u = 0, e = InternalFns.size(); u < e; ++u)
1943     if (Function *F = InternalFns[u])
1944       ToBeDeletedFunctions.insert(F);
1945 }
1946 
1947 ChangeStatus Attributor::cleanupIR() {
1948   TimeTraceScope TimeScope("Attributor::cleanupIR");
1949   // Delete stuff at the end to avoid invalid references and a nice order.
1950   LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least "
1951                     << ToBeDeletedFunctions.size() << " functions and "
1952                     << ToBeDeletedBlocks.size() << " blocks and "
1953                     << ToBeDeletedInsts.size() << " instructions and "
1954                     << ToBeChangedValues.size() << " values and "
1955                     << ToBeChangedUses.size() << " uses. To insert "
1956                     << ToBeChangedToUnreachableInsts.size()
1957                     << " unreachables.\n"
1958                     << "Preserve manifest added " << ManifestAddedBlocks.size()
1959                     << " blocks\n");
1960 
1961   SmallVector<WeakTrackingVH, 32> DeadInsts;
1962   SmallVector<Instruction *, 32> TerminatorsToFold;
1963 
1964   auto ReplaceUse = [&](Use *U, Value *NewV) {
1965     Value *OldV = U->get();
1966 
1967     // If we plan to replace NewV we need to update it at this point.
1968     do {
1969       const auto &Entry = ToBeChangedValues.lookup(NewV);
1970       if (!Entry.first)
1971         break;
1972       NewV = Entry.first;
1973     } while (true);
1974 
1975     Instruction *I = dyn_cast<Instruction>(U->getUser());
1976     assert((!I || isRunOn(*I->getFunction())) &&
1977            "Cannot replace an instruction outside the current SCC!");
1978 
1979     // Do not replace uses in returns if the value is a must-tail call we will
1980     // not delete.
1981     if (auto *RI = dyn_cast_or_null<ReturnInst>(I)) {
1982       if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts()))
1983         if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI))
1984           return;
1985       // If we rewrite a return and the new value is not an argument, strip the
1986       // `returned` attribute as it is wrong now.
1987       if (!isa<Argument>(NewV))
1988         for (auto &Arg : RI->getFunction()->args())
1989           Arg.removeAttr(Attribute::Returned);
1990     }
1991 
1992     // Do not perform call graph altering changes outside the SCC.
1993     if (auto *CB = dyn_cast_or_null<CallBase>(I))
1994       if (CB->isCallee(U))
1995         return;
1996 
1997     LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
1998                       << " instead of " << *OldV << "\n");
1999     U->set(NewV);
2000 
2001     if (Instruction *I = dyn_cast<Instruction>(OldV)) {
2002       CGModifiedFunctions.insert(I->getFunction());
2003       if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
2004           isInstructionTriviallyDead(I))
2005         DeadInsts.push_back(I);
2006     }
2007     if (isa<UndefValue>(NewV) && isa<CallBase>(U->getUser())) {
2008       auto *CB = cast<CallBase>(U->getUser());
2009       if (CB->isArgOperand(U)) {
2010         unsigned Idx = CB->getArgOperandNo(U);
2011         CB->removeParamAttr(Idx, Attribute::NoUndef);
2012         Function *Fn = CB->getCalledFunction();
2013         if (Fn && Fn->arg_size() > Idx)
2014           Fn->removeParamAttr(Idx, Attribute::NoUndef);
2015       }
2016     }
2017     if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
2018       Instruction *UserI = cast<Instruction>(U->getUser());
2019       if (isa<UndefValue>(NewV)) {
2020         ToBeChangedToUnreachableInsts.insert(UserI);
2021       } else {
2022         TerminatorsToFold.push_back(UserI);
2023       }
2024     }
2025   };
2026 
2027   for (auto &It : ToBeChangedUses) {
2028     Use *U = It.first;
2029     Value *NewV = It.second;
2030     ReplaceUse(U, NewV);
2031   }
2032 
2033   SmallVector<Use *, 4> Uses;
2034   for (auto &It : ToBeChangedValues) {
2035     Value *OldV = It.first;
2036     auto &Entry = It.second;
2037     Value *NewV = Entry.first;
2038     Uses.clear();
2039     for (auto &U : OldV->uses())
2040       if (Entry.second || !U.getUser()->isDroppable())
2041         Uses.push_back(&U);
2042     for (Use *U : Uses) {
2043       if (auto *I = dyn_cast<Instruction>(U->getUser()))
2044         if (!isRunOn(*I->getFunction()))
2045           continue;
2046       ReplaceUse(U, NewV);
2047     }
2048   }
2049 
2050   for (auto &V : InvokeWithDeadSuccessor)
2051     if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
2052       assert(isRunOn(*II->getFunction()) &&
2053              "Cannot replace an invoke outside the current SCC!");
2054       bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
2055       bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
2056       bool Invoke2CallAllowed =
2057           !AAIsDead::mayCatchAsynchronousExceptions(*II->getFunction());
2058       assert((UnwindBBIsDead || NormalBBIsDead) &&
2059              "Invoke does not have dead successors!");
2060       BasicBlock *BB = II->getParent();
2061       BasicBlock *NormalDestBB = II->getNormalDest();
2062       if (UnwindBBIsDead) {
2063         Instruction *NormalNextIP = &NormalDestBB->front();
2064         if (Invoke2CallAllowed) {
2065           changeToCall(II);
2066           NormalNextIP = BB->getTerminator();
2067         }
2068         if (NormalBBIsDead)
2069           ToBeChangedToUnreachableInsts.insert(NormalNextIP);
2070       } else {
2071         assert(NormalBBIsDead && "Broken invariant!");
2072         if (!NormalDestBB->getUniquePredecessor())
2073           NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
2074         ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
2075       }
2076     }
2077   for (Instruction *I : TerminatorsToFold) {
2078     assert(isRunOn(*I->getFunction()) &&
2079            "Cannot replace a terminator outside the current SCC!");
2080     CGModifiedFunctions.insert(I->getFunction());
2081     ConstantFoldTerminator(I->getParent());
2082   }
2083   for (auto &V : ToBeChangedToUnreachableInsts)
2084     if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
2085       LLVM_DEBUG(dbgs() << "[Attributor] Change to unreachable: " << *I
2086                         << "\n");
2087       assert(isRunOn(*I->getFunction()) &&
2088              "Cannot replace an instruction outside the current SCC!");
2089       CGModifiedFunctions.insert(I->getFunction());
2090       changeToUnreachable(I);
2091     }
2092 
2093   for (auto &V : ToBeDeletedInsts) {
2094     if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
2095       if (auto *CB = dyn_cast<CallBase>(I)) {
2096         assert(isRunOn(*I->getFunction()) &&
2097                "Cannot delete an instruction outside the current SCC!");
2098         if (!isa<IntrinsicInst>(CB))
2099           Configuration.CGUpdater.removeCallSite(*CB);
2100       }
2101       I->dropDroppableUses();
2102       CGModifiedFunctions.insert(I->getFunction());
2103       if (!I->getType()->isVoidTy())
2104         I->replaceAllUsesWith(UndefValue::get(I->getType()));
2105       if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
2106         DeadInsts.push_back(I);
2107       else
2108         I->eraseFromParent();
2109     }
2110   }
2111 
2112   llvm::erase_if(DeadInsts, [&](WeakTrackingVH I) { return !I; });
2113 
2114   LLVM_DEBUG({
2115     dbgs() << "[Attributor] DeadInsts size: " << DeadInsts.size() << "\n";
2116     for (auto &I : DeadInsts)
2117       if (I)
2118         dbgs() << "  - " << *I << "\n";
2119   });
2120 
2121   RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);
2122 
2123   if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
2124     SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
2125     ToBeDeletedBBs.reserve(NumDeadBlocks);
2126     for (BasicBlock *BB : ToBeDeletedBlocks) {
2127       assert(isRunOn(*BB->getParent()) &&
2128              "Cannot delete a block outside the current SCC!");
2129       CGModifiedFunctions.insert(BB->getParent());
2130       // Do not delete BBs added during manifests of AAs.
2131       if (ManifestAddedBlocks.contains(BB))
2132         continue;
2133       ToBeDeletedBBs.push_back(BB);
2134     }
2135     // Actually we do not delete the blocks but squash them into a single
2136     // unreachable but untangling branches that jump here is something we need
2137     // to do in a more generic way.
2138     detachDeadBlocks(ToBeDeletedBBs, nullptr);
2139   }
2140 
2141   identifyDeadInternalFunctions();
2142 
2143   // Rewrite the functions as requested during manifest.
2144   ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions);
2145 
2146   for (Function *Fn : CGModifiedFunctions)
2147     if (!ToBeDeletedFunctions.count(Fn) && Functions.count(Fn))
2148       Configuration.CGUpdater.reanalyzeFunction(*Fn);
2149 
2150   for (Function *Fn : ToBeDeletedFunctions) {
2151     if (!Functions.count(Fn))
2152       continue;
2153     Configuration.CGUpdater.removeFunction(*Fn);
2154   }
2155 
2156   if (!ToBeChangedUses.empty())
2157     ManifestChange = ChangeStatus::CHANGED;
2158 
2159   if (!ToBeChangedToUnreachableInsts.empty())
2160     ManifestChange = ChangeStatus::CHANGED;
2161 
2162   if (!ToBeDeletedFunctions.empty())
2163     ManifestChange = ChangeStatus::CHANGED;
2164 
2165   if (!ToBeDeletedBlocks.empty())
2166     ManifestChange = ChangeStatus::CHANGED;
2167 
2168   if (!ToBeDeletedInsts.empty())
2169     ManifestChange = ChangeStatus::CHANGED;
2170 
2171   if (!InvokeWithDeadSuccessor.empty())
2172     ManifestChange = ChangeStatus::CHANGED;
2173 
2174   if (!DeadInsts.empty())
2175     ManifestChange = ChangeStatus::CHANGED;
2176 
2177   NumFnDeleted += ToBeDeletedFunctions.size();
2178 
2179   LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions.size()
2180                     << " functions after manifest.\n");
2181 
2182 #ifdef EXPENSIVE_CHECKS
2183   for (Function *F : Functions) {
2184     if (ToBeDeletedFunctions.count(F))
2185       continue;
2186     assert(!verifyFunction(*F, &errs()) && "Module verification failed!");
2187   }
2188 #endif
2189 
2190   return ManifestChange;
2191 }
2192 
2193 ChangeStatus Attributor::run() {
2194   TimeTraceScope TimeScope("Attributor::run");
2195   AttributorCallGraph ACallGraph(*this);
2196 
2197   if (PrintCallGraph)
2198     ACallGraph.populateAll();
2199 
2200   Phase = AttributorPhase::UPDATE;
2201   runTillFixpoint();
2202 
2203   // dump graphs on demand
2204   if (DumpDepGraph)
2205     DG.dumpGraph();
2206 
2207   if (ViewDepGraph)
2208     DG.viewGraph();
2209 
2210   if (PrintDependencies)
2211     DG.print();
2212 
2213   Phase = AttributorPhase::MANIFEST;
2214   ChangeStatus ManifestChange = manifestAttributes();
2215 
2216   Phase = AttributorPhase::CLEANUP;
2217   ChangeStatus CleanupChange = cleanupIR();
2218 
2219   if (PrintCallGraph)
2220     ACallGraph.print();
2221 
2222   return ManifestChange | CleanupChange;
2223 }
2224 
2225 ChangeStatus Attributor::updateAA(AbstractAttribute &AA) {
2226   TimeTraceScope TimeScope(
2227       AA.getName() + std::to_string(AA.getIRPosition().getPositionKind()) +
2228       "::updateAA");
2229   assert(Phase == AttributorPhase::UPDATE &&
2230          "We can update AA only in the update stage!");
2231 
2232   // Use a new dependence vector for this update.
2233   DependenceVector DV;
2234   DependenceStack.push_back(&DV);
2235 
2236   auto &AAState = AA.getState();
2237   ChangeStatus CS = ChangeStatus::UNCHANGED;
2238   bool UsedAssumedInformation = false;
2239   if (!isAssumedDead(AA, nullptr, UsedAssumedInformation,
2240                      /* CheckBBLivenessOnly */ true))
2241     CS = AA.update(*this);
2242 
2243   if (!AA.isQueryAA() && DV.empty()) {
2244     // If the attribute did not query any non-fix information, the state
2245     // will not change and we can indicate that right away.
2246     AAState.indicateOptimisticFixpoint();
2247   }
2248 
2249   if (!AAState.isAtFixpoint())
2250     rememberDependences();
2251 
2252   // Verify the stack was used properly, that is we pop the dependence vector we
2253   // put there earlier.
2254   DependenceVector *PoppedDV = DependenceStack.pop_back_val();
2255   (void)PoppedDV;
2256   assert(PoppedDV == &DV && "Inconsistent usage of the dependence stack!");
2257 
2258   return CS;
2259 }
2260 
2261 void Attributor::createShallowWrapper(Function &F) {
2262   assert(!F.isDeclaration() && "Cannot create a wrapper around a declaration!");
2263 
2264   Module &M = *F.getParent();
2265   LLVMContext &Ctx = M.getContext();
2266   FunctionType *FnTy = F.getFunctionType();
2267 
2268   Function *Wrapper =
2269       Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(), F.getName());
2270   F.setName(""); // set the inside function anonymous
2271   M.getFunctionList().insert(F.getIterator(), Wrapper);
2272 
2273   F.setLinkage(GlobalValue::InternalLinkage);
2274 
2275   F.replaceAllUsesWith(Wrapper);
2276   assert(F.use_empty() && "Uses remained after wrapper was created!");
2277 
2278   // Move the COMDAT section to the wrapper.
2279   // TODO: Check if we need to keep it for F as well.
2280   Wrapper->setComdat(F.getComdat());
2281   F.setComdat(nullptr);
2282 
2283   // Copy all metadata and attributes but keep them on F as well.
2284   SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
2285   F.getAllMetadata(MDs);
2286   for (auto MDIt : MDs)
2287     Wrapper->addMetadata(MDIt.first, *MDIt.second);
2288   Wrapper->setAttributes(F.getAttributes());
2289 
2290   // Create the call in the wrapper.
2291   BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", Wrapper);
2292 
2293   SmallVector<Value *, 8> Args;
2294   Argument *FArgIt = F.arg_begin();
2295   for (Argument &Arg : Wrapper->args()) {
2296     Args.push_back(&Arg);
2297     Arg.setName((FArgIt++)->getName());
2298   }
2299 
2300   CallInst *CI = CallInst::Create(&F, Args, "", EntryBB);
2301   CI->setTailCall(true);
2302   CI->addFnAttr(Attribute::NoInline);
2303   ReturnInst::Create(Ctx, CI->getType()->isVoidTy() ? nullptr : CI, EntryBB);
2304 
2305   NumFnShallowWrappersCreated++;
2306 }
2307 
2308 bool Attributor::isInternalizable(Function &F) {
2309   if (F.isDeclaration() || F.hasLocalLinkage() ||
2310       GlobalValue::isInterposableLinkage(F.getLinkage()))
2311     return false;
2312   return true;
2313 }
2314 
2315 Function *Attributor::internalizeFunction(Function &F, bool Force) {
2316   if (!AllowDeepWrapper && !Force)
2317     return nullptr;
2318   if (!isInternalizable(F))
2319     return nullptr;
2320 
2321   SmallPtrSet<Function *, 2> FnSet = {&F};
2322   DenseMap<Function *, Function *> InternalizedFns;
2323   internalizeFunctions(FnSet, InternalizedFns);
2324 
2325   return InternalizedFns[&F];
2326 }
2327 
2328 bool Attributor::internalizeFunctions(SmallPtrSetImpl<Function *> &FnSet,
2329                                       DenseMap<Function *, Function *> &FnMap) {
2330   for (Function *F : FnSet)
2331     if (!Attributor::isInternalizable(*F))
2332       return false;
2333 
2334   FnMap.clear();
2335   // Generate the internalized version of each function.
2336   for (Function *F : FnSet) {
2337     Module &M = *F->getParent();
2338     FunctionType *FnTy = F->getFunctionType();
2339 
2340     // Create a copy of the current function
2341     Function *Copied =
2342         Function::Create(FnTy, F->getLinkage(), F->getAddressSpace(),
2343                          F->getName() + ".internalized");
2344     ValueToValueMapTy VMap;
2345     auto *NewFArgIt = Copied->arg_begin();
2346     for (auto &Arg : F->args()) {
2347       auto ArgName = Arg.getName();
2348       NewFArgIt->setName(ArgName);
2349       VMap[&Arg] = &(*NewFArgIt++);
2350     }
2351     SmallVector<ReturnInst *, 8> Returns;
2352 
2353     // Copy the body of the original function to the new one
2354     CloneFunctionInto(Copied, F, VMap,
2355                       CloneFunctionChangeType::LocalChangesOnly, Returns);
2356 
2357     // Set the linakage and visibility late as CloneFunctionInto has some
2358     // implicit requirements.
2359     Copied->setVisibility(GlobalValue::DefaultVisibility);
2360     Copied->setLinkage(GlobalValue::PrivateLinkage);
2361 
2362     // Copy metadata
2363     SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
2364     F->getAllMetadata(MDs);
2365     for (auto MDIt : MDs)
2366       if (!Copied->hasMetadata())
2367         Copied->addMetadata(MDIt.first, *MDIt.second);
2368 
2369     M.getFunctionList().insert(F->getIterator(), Copied);
2370     Copied->setDSOLocal(true);
2371     FnMap[F] = Copied;
2372   }
2373 
2374   // Replace all uses of the old function with the new internalized function
2375   // unless the caller is a function that was just internalized.
2376   for (Function *F : FnSet) {
2377     auto &InternalizedFn = FnMap[F];
2378     auto IsNotInternalized = [&](Use &U) -> bool {
2379       if (auto *CB = dyn_cast<CallBase>(U.getUser()))
2380         return !FnMap.lookup(CB->getCaller());
2381       return false;
2382     };
2383     F->replaceUsesWithIf(InternalizedFn, IsNotInternalized);
2384   }
2385 
2386   return true;
2387 }
2388 
2389 bool Attributor::isValidFunctionSignatureRewrite(
2390     Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
2391 
2392   if (!Configuration.RewriteSignatures)
2393     return false;
2394 
2395   Function *Fn = Arg.getParent();
2396   auto CallSiteCanBeChanged = [Fn](AbstractCallSite ACS) {
2397     // Forbid the call site to cast the function return type. If we need to
2398     // rewrite these functions we need to re-create a cast for the new call site
2399     // (if the old had uses).
2400     if (!ACS.getCalledFunction() ||
2401         ACS.getInstruction()->getType() !=
2402             ACS.getCalledFunction()->getReturnType())
2403       return false;
2404     if (ACS.getCalledOperand()->getType() != Fn->getType())
2405       return false;
2406     // Forbid must-tail calls for now.
2407     return !ACS.isCallbackCall() && !ACS.getInstruction()->isMustTailCall();
2408   };
2409 
2410   // Avoid var-arg functions for now.
2411   if (Fn->isVarArg()) {
2412     LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
2413     return false;
2414   }
2415 
2416   // Avoid functions with complicated argument passing semantics.
2417   AttributeList FnAttributeList = Fn->getAttributes();
2418   if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
2419       FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
2420       FnAttributeList.hasAttrSomewhere(Attribute::InAlloca) ||
2421       FnAttributeList.hasAttrSomewhere(Attribute::Preallocated)) {
2422     LLVM_DEBUG(
2423         dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
2424     return false;
2425   }
2426 
2427   // Avoid callbacks for now.
2428   bool UsedAssumedInformation = false;
2429   if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
2430                             UsedAssumedInformation)) {
2431     LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
2432     return false;
2433   }
2434 
2435   auto InstPred = [](Instruction &I) {
2436     if (auto *CI = dyn_cast<CallInst>(&I))
2437       return !CI->isMustTailCall();
2438     return true;
2439   };
2440 
2441   // Forbid must-tail calls for now.
2442   // TODO:
2443   auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
2444   if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
2445                                    nullptr, {Instruction::Call},
2446                                    UsedAssumedInformation)) {
2447     LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
2448     return false;
2449   }
2450 
2451   return true;
2452 }
2453 
2454 bool Attributor::registerFunctionSignatureRewrite(
2455     Argument &Arg, ArrayRef<Type *> ReplacementTypes,
2456     ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
2457     ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) {
2458   LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
2459                     << Arg.getParent()->getName() << " with "
2460                     << ReplacementTypes.size() << " replacements\n");
2461   assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
2462          "Cannot register an invalid rewrite");
2463 
2464   Function *Fn = Arg.getParent();
2465   SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs =
2466       ArgumentReplacementMap[Fn];
2467   if (ARIs.empty())
2468     ARIs.resize(Fn->arg_size());
2469 
2470   // If we have a replacement already with less than or equal new arguments,
2471   // ignore this request.
2472   std::unique_ptr<ArgumentReplacementInfo> &ARI = ARIs[Arg.getArgNo()];
2473   if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
2474     LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
2475     return false;
2476   }
2477 
2478   // If we have a replacement already but we like the new one better, delete
2479   // the old.
2480   ARI.reset();
2481 
2482   LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
2483                     << Arg.getParent()->getName() << " with "
2484                     << ReplacementTypes.size() << " replacements\n");
2485 
2486   // Remember the replacement.
2487   ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
2488                                         std::move(CalleeRepairCB),
2489                                         std::move(ACSRepairCB)));
2490 
2491   return true;
2492 }
2493 
2494 bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) {
2495   bool Result = true;
2496 #ifndef NDEBUG
2497   if (SeedAllowList.size() != 0)
2498     Result = llvm::is_contained(SeedAllowList, AA.getName());
2499   Function *Fn = AA.getAnchorScope();
2500   if (FunctionSeedAllowList.size() != 0 && Fn)
2501     Result &= llvm::is_contained(FunctionSeedAllowList, Fn->getName());
2502 #endif
2503   return Result;
2504 }
2505 
2506 ChangeStatus Attributor::rewriteFunctionSignatures(
2507     SmallSetVector<Function *, 8> &ModifiedFns) {
2508   ChangeStatus Changed = ChangeStatus::UNCHANGED;
2509 
2510   for (auto &It : ArgumentReplacementMap) {
2511     Function *OldFn = It.getFirst();
2512 
2513     // Deleted functions do not require rewrites.
2514     if (!Functions.count(OldFn) || ToBeDeletedFunctions.count(OldFn))
2515       continue;
2516 
2517     const SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs =
2518         It.getSecond();
2519     assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
2520 
2521     SmallVector<Type *, 16> NewArgumentTypes;
2522     SmallVector<AttributeSet, 16> NewArgumentAttributes;
2523 
2524     // Collect replacement argument types and copy over existing attributes.
2525     AttributeList OldFnAttributeList = OldFn->getAttributes();
2526     for (Argument &Arg : OldFn->args()) {
2527       if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
2528               ARIs[Arg.getArgNo()]) {
2529         NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
2530                                 ARI->ReplacementTypes.end());
2531         NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
2532                                      AttributeSet());
2533       } else {
2534         NewArgumentTypes.push_back(Arg.getType());
2535         NewArgumentAttributes.push_back(
2536             OldFnAttributeList.getParamAttrs(Arg.getArgNo()));
2537       }
2538     }
2539 
2540     uint64_t LargestVectorWidth = 0;
2541     for (auto *I : NewArgumentTypes)
2542       if (auto *VT = dyn_cast<llvm::VectorType>(I))
2543         LargestVectorWidth = std::max(
2544             LargestVectorWidth, VT->getPrimitiveSizeInBits().getKnownMinSize());
2545 
2546     FunctionType *OldFnTy = OldFn->getFunctionType();
2547     Type *RetTy = OldFnTy->getReturnType();
2548 
2549     // Construct the new function type using the new arguments types.
2550     FunctionType *NewFnTy =
2551         FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
2552 
2553     LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
2554                       << "' from " << *OldFn->getFunctionType() << " to "
2555                       << *NewFnTy << "\n");
2556 
2557     // Create the new function body and insert it into the module.
2558     Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
2559                                        OldFn->getAddressSpace(), "");
2560     Functions.insert(NewFn);
2561     OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
2562     NewFn->takeName(OldFn);
2563     NewFn->copyAttributesFrom(OldFn);
2564 
2565     // Patch the pointer to LLVM function in debug info descriptor.
2566     NewFn->setSubprogram(OldFn->getSubprogram());
2567     OldFn->setSubprogram(nullptr);
2568 
2569     // Recompute the parameter attributes list based on the new arguments for
2570     // the function.
2571     LLVMContext &Ctx = OldFn->getContext();
2572     NewFn->setAttributes(AttributeList::get(
2573         Ctx, OldFnAttributeList.getFnAttrs(), OldFnAttributeList.getRetAttrs(),
2574         NewArgumentAttributes));
2575     AttributeFuncs::updateMinLegalVectorWidthAttr(*NewFn, LargestVectorWidth);
2576 
2577     // Since we have now created the new function, splice the body of the old
2578     // function right into the new function, leaving the old rotting hulk of the
2579     // function empty.
2580     NewFn->getBasicBlockList().splice(NewFn->begin(),
2581                                       OldFn->getBasicBlockList());
2582 
2583     // Fixup block addresses to reference new function.
2584     SmallVector<BlockAddress *, 8u> BlockAddresses;
2585     for (User *U : OldFn->users())
2586       if (auto *BA = dyn_cast<BlockAddress>(U))
2587         BlockAddresses.push_back(BA);
2588     for (auto *BA : BlockAddresses)
2589       BA->replaceAllUsesWith(BlockAddress::get(NewFn, BA->getBasicBlock()));
2590 
2591     // Set of all "call-like" instructions that invoke the old function mapped
2592     // to their new replacements.
2593     SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs;
2594 
2595     // Callback to create a new "call-like" instruction for a given one.
2596     auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
2597       CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
2598       const AttributeList &OldCallAttributeList = OldCB->getAttributes();
2599 
2600       // Collect the new argument operands for the replacement call site.
2601       SmallVector<Value *, 16> NewArgOperands;
2602       SmallVector<AttributeSet, 16> NewArgOperandAttributes;
2603       for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
2604         unsigned NewFirstArgNum = NewArgOperands.size();
2605         (void)NewFirstArgNum; // only used inside assert.
2606         if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
2607                 ARIs[OldArgNum]) {
2608           if (ARI->ACSRepairCB)
2609             ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
2610           assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
2611                      NewArgOperands.size() &&
2612                  "ACS repair callback did not provide as many operand as new "
2613                  "types were registered!");
2614           // TODO: Exose the attribute set to the ACS repair callback
2615           NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
2616                                          AttributeSet());
2617         } else {
2618           NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
2619           NewArgOperandAttributes.push_back(
2620               OldCallAttributeList.getParamAttrs(OldArgNum));
2621         }
2622       }
2623 
2624       assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
2625              "Mismatch # argument operands vs. # argument operand attributes!");
2626       assert(NewArgOperands.size() == NewFn->arg_size() &&
2627              "Mismatch # argument operands vs. # function arguments!");
2628 
2629       SmallVector<OperandBundleDef, 4> OperandBundleDefs;
2630       OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
2631 
2632       // Create a new call or invoke instruction to replace the old one.
2633       CallBase *NewCB;
2634       if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
2635         NewCB =
2636             InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(),
2637                                NewArgOperands, OperandBundleDefs, "", OldCB);
2638       } else {
2639         auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
2640                                        "", OldCB);
2641         NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
2642         NewCB = NewCI;
2643       }
2644 
2645       // Copy over various properties and the new attributes.
2646       NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
2647       NewCB->setCallingConv(OldCB->getCallingConv());
2648       NewCB->takeName(OldCB);
2649       NewCB->setAttributes(AttributeList::get(
2650           Ctx, OldCallAttributeList.getFnAttrs(),
2651           OldCallAttributeList.getRetAttrs(), NewArgOperandAttributes));
2652 
2653       AttributeFuncs::updateMinLegalVectorWidthAttr(*NewCB->getCaller(),
2654                                                     LargestVectorWidth);
2655 
2656       CallSitePairs.push_back({OldCB, NewCB});
2657       return true;
2658     };
2659 
2660     // Use the CallSiteReplacementCreator to create replacement call sites.
2661     bool UsedAssumedInformation = false;
2662     bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
2663                                         true, nullptr, UsedAssumedInformation);
2664     (void)Success;
2665     assert(Success && "Assumed call site replacement to succeed!");
2666 
2667     // Rewire the arguments.
2668     Argument *OldFnArgIt = OldFn->arg_begin();
2669     Argument *NewFnArgIt = NewFn->arg_begin();
2670     for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
2671          ++OldArgNum, ++OldFnArgIt) {
2672       if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
2673               ARIs[OldArgNum]) {
2674         if (ARI->CalleeRepairCB)
2675           ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
2676         if (ARI->ReplacementTypes.empty())
2677           OldFnArgIt->replaceAllUsesWith(
2678               PoisonValue::get(OldFnArgIt->getType()));
2679         NewFnArgIt += ARI->ReplacementTypes.size();
2680       } else {
2681         NewFnArgIt->takeName(&*OldFnArgIt);
2682         OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
2683         ++NewFnArgIt;
2684       }
2685     }
2686 
2687     // Eliminate the instructions *after* we visited all of them.
2688     for (auto &CallSitePair : CallSitePairs) {
2689       CallBase &OldCB = *CallSitePair.first;
2690       CallBase &NewCB = *CallSitePair.second;
2691       assert(OldCB.getType() == NewCB.getType() &&
2692              "Cannot handle call sites with different types!");
2693       ModifiedFns.insert(OldCB.getFunction());
2694       Configuration.CGUpdater.replaceCallSite(OldCB, NewCB);
2695       OldCB.replaceAllUsesWith(&NewCB);
2696       OldCB.eraseFromParent();
2697     }
2698 
2699     // Replace the function in the call graph (if any).
2700     Configuration.CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
2701 
2702     // If the old function was modified and needed to be reanalyzed, the new one
2703     // does now.
2704     if (ModifiedFns.remove(OldFn))
2705       ModifiedFns.insert(NewFn);
2706 
2707     Changed = ChangeStatus::CHANGED;
2708   }
2709 
2710   return Changed;
2711 }
2712 
2713 void InformationCache::initializeInformationCache(const Function &CF,
2714                                                   FunctionInfo &FI) {
2715   // As we do not modify the function here we can remove the const
2716   // withouth breaking implicit assumptions. At the end of the day, we could
2717   // initialize the cache eagerly which would look the same to the users.
2718   Function &F = const_cast<Function &>(CF);
2719 
2720   // Walk all instructions to find interesting instructions that might be
2721   // queried by abstract attributes during their initialization or update.
2722   // This has to happen before we create attributes.
2723 
2724   DenseMap<const Value *, Optional<short>> AssumeUsesMap;
2725 
2726   // Add \p V to the assume uses map which track the number of uses outside of
2727   // "visited" assumes. If no outside uses are left the value is added to the
2728   // assume only use vector.
2729   auto AddToAssumeUsesMap = [&](const Value &V) -> void {
2730     SmallVector<const Instruction *> Worklist;
2731     if (auto *I = dyn_cast<Instruction>(&V))
2732       Worklist.push_back(I);
2733     while (!Worklist.empty()) {
2734       const Instruction *I = Worklist.pop_back_val();
2735       Optional<short> &NumUses = AssumeUsesMap[I];
2736       if (!NumUses)
2737         NumUses = I->getNumUses();
2738       NumUses = NumUses.value() - /* this assume */ 1;
2739       if (NumUses.value() != 0)
2740         continue;
2741       AssumeOnlyValues.insert(I);
2742       for (const Value *Op : I->operands())
2743         if (auto *OpI = dyn_cast<Instruction>(Op))
2744           Worklist.push_back(OpI);
2745     }
2746   };
2747 
2748   for (Instruction &I : instructions(&F)) {
2749     bool IsInterestingOpcode = false;
2750 
2751     // To allow easy access to all instructions in a function with a given
2752     // opcode we store them in the InfoCache. As not all opcodes are interesting
2753     // to concrete attributes we only cache the ones that are as identified in
2754     // the following switch.
2755     // Note: There are no concrete attributes now so this is initially empty.
2756     switch (I.getOpcode()) {
2757     default:
2758       assert(!isa<CallBase>(&I) &&
2759              "New call base instruction type needs to be known in the "
2760              "Attributor.");
2761       break;
2762     case Instruction::Call:
2763       // Calls are interesting on their own, additionally:
2764       // For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
2765       // For `must-tail` calls we remember the caller and callee.
2766       if (auto *Assume = dyn_cast<AssumeInst>(&I)) {
2767         fillMapFromAssume(*Assume, KnowledgeMap);
2768         AddToAssumeUsesMap(*Assume->getArgOperand(0));
2769       } else if (cast<CallInst>(I).isMustTailCall()) {
2770         FI.ContainsMustTailCall = true;
2771         if (const Function *Callee = cast<CallInst>(I).getCalledFunction())
2772           getFunctionInfo(*Callee).CalledViaMustTail = true;
2773       }
2774       LLVM_FALLTHROUGH;
2775     case Instruction::CallBr:
2776     case Instruction::Invoke:
2777     case Instruction::CleanupRet:
2778     case Instruction::CatchSwitch:
2779     case Instruction::AtomicRMW:
2780     case Instruction::AtomicCmpXchg:
2781     case Instruction::Br:
2782     case Instruction::Resume:
2783     case Instruction::Ret:
2784     case Instruction::Load:
2785       // The alignment of a pointer is interesting for loads.
2786     case Instruction::Store:
2787       // The alignment of a pointer is interesting for stores.
2788     case Instruction::Alloca:
2789     case Instruction::AddrSpaceCast:
2790       IsInterestingOpcode = true;
2791     }
2792     if (IsInterestingOpcode) {
2793       auto *&Insts = FI.OpcodeInstMap[I.getOpcode()];
2794       if (!Insts)
2795         Insts = new (Allocator) InstructionVectorTy();
2796       Insts->push_back(&I);
2797     }
2798     if (I.mayReadOrWriteMemory())
2799       FI.RWInsts.push_back(&I);
2800   }
2801 
2802   if (F.hasFnAttribute(Attribute::AlwaysInline) &&
2803       isInlineViable(F).isSuccess())
2804     InlineableFunctions.insert(&F);
2805 }
2806 
2807 AAResults *InformationCache::getAAResultsForFunction(const Function &F) {
2808   return AG.getAnalysis<AAManager>(F);
2809 }
2810 
2811 InformationCache::FunctionInfo::~FunctionInfo() {
2812   // The instruction vectors are allocated using a BumpPtrAllocator, we need to
2813   // manually destroy them.
2814   for (auto &It : OpcodeInstMap)
2815     It.getSecond()->~InstructionVectorTy();
2816 }
2817 
2818 void Attributor::recordDependence(const AbstractAttribute &FromAA,
2819                                   const AbstractAttribute &ToAA,
2820                                   DepClassTy DepClass) {
2821   if (DepClass == DepClassTy::NONE)
2822     return;
2823   // If we are outside of an update, thus before the actual fixpoint iteration
2824   // started (= when we create AAs), we do not track dependences because we will
2825   // put all AAs into the initial worklist anyway.
2826   if (DependenceStack.empty())
2827     return;
2828   if (FromAA.getState().isAtFixpoint())
2829     return;
2830   DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass});
2831 }
2832 
2833 void Attributor::rememberDependences() {
2834   assert(!DependenceStack.empty() && "No dependences to remember!");
2835 
2836   for (DepInfo &DI : *DependenceStack.back()) {
2837     assert((DI.DepClass == DepClassTy::REQUIRED ||
2838             DI.DepClass == DepClassTy::OPTIONAL) &&
2839            "Expected required or optional dependence (1 bit)!");
2840     auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps;
2841     DepAAs.push_back(AbstractAttribute::DepTy(
2842         const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass)));
2843   }
2844 }
2845 
2846 void Attributor::identifyDefaultAbstractAttributes(Function &F) {
2847   if (!VisitedFunctions.insert(&F).second)
2848     return;
2849   if (F.isDeclaration())
2850     return;
2851 
2852   // In non-module runs we need to look at the call sites of a function to
2853   // determine if it is part of a must-tail call edge. This will influence what
2854   // attributes we can derive.
2855   InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F);
2856   if (!isModulePass() && !FI.CalledViaMustTail) {
2857     for (const Use &U : F.uses())
2858       if (const auto *CB = dyn_cast<CallBase>(U.getUser()))
2859         if (CB->isCallee(&U) && CB->isMustTailCall())
2860           FI.CalledViaMustTail = true;
2861   }
2862 
2863   IRPosition FPos = IRPosition::function(F);
2864 
2865   // Check for dead BasicBlocks in every function.
2866   // We need dead instruction detection because we do not want to deal with
2867   // broken IR in which SSA rules do not apply.
2868   getOrCreateAAFor<AAIsDead>(FPos);
2869 
2870   // Every function might be "will-return".
2871   getOrCreateAAFor<AAWillReturn>(FPos);
2872 
2873   // Every function might contain instructions that cause "undefined behavior".
2874   getOrCreateAAFor<AAUndefinedBehavior>(FPos);
2875 
2876   // Every function can be nounwind.
2877   getOrCreateAAFor<AANoUnwind>(FPos);
2878 
2879   // Every function might be marked "nosync"
2880   getOrCreateAAFor<AANoSync>(FPos);
2881 
2882   // Every function might be "no-free".
2883   getOrCreateAAFor<AANoFree>(FPos);
2884 
2885   // Every function might be "no-return".
2886   getOrCreateAAFor<AANoReturn>(FPos);
2887 
2888   // Every function might be "no-recurse".
2889   getOrCreateAAFor<AANoRecurse>(FPos);
2890 
2891   // Every function might be "readnone/readonly/writeonly/...".
2892   getOrCreateAAFor<AAMemoryBehavior>(FPos);
2893 
2894   // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
2895   getOrCreateAAFor<AAMemoryLocation>(FPos);
2896 
2897   // Every function can track active assumptions.
2898   getOrCreateAAFor<AAAssumptionInfo>(FPos);
2899 
2900   // Every function might be applicable for Heap-To-Stack conversion.
2901   if (EnableHeapToStack)
2902     getOrCreateAAFor<AAHeapToStack>(FPos);
2903 
2904   // Return attributes are only appropriate if the return type is non void.
2905   Type *ReturnType = F.getReturnType();
2906   if (!ReturnType->isVoidTy()) {
2907     // Argument attribute "returned" --- Create only one per function even
2908     // though it is an argument attribute.
2909     getOrCreateAAFor<AAReturnedValues>(FPos);
2910 
2911     IRPosition RetPos = IRPosition::returned(F);
2912 
2913     // Every returned value might be dead.
2914     getOrCreateAAFor<AAIsDead>(RetPos);
2915 
2916     // Every function might be simplified.
2917     bool UsedAssumedInformation = false;
2918     getAssumedSimplified(RetPos, nullptr, UsedAssumedInformation,
2919                          AA::Intraprocedural);
2920 
2921     // Every returned value might be marked noundef.
2922     getOrCreateAAFor<AANoUndef>(RetPos);
2923 
2924     if (ReturnType->isPointerTy()) {
2925 
2926       // Every function with pointer return type might be marked align.
2927       getOrCreateAAFor<AAAlign>(RetPos);
2928 
2929       // Every function with pointer return type might be marked nonnull.
2930       getOrCreateAAFor<AANonNull>(RetPos);
2931 
2932       // Every function with pointer return type might be marked noalias.
2933       getOrCreateAAFor<AANoAlias>(RetPos);
2934 
2935       // Every function with pointer return type might be marked
2936       // dereferenceable.
2937       getOrCreateAAFor<AADereferenceable>(RetPos);
2938     }
2939   }
2940 
2941   for (Argument &Arg : F.args()) {
2942     IRPosition ArgPos = IRPosition::argument(Arg);
2943 
2944     // Every argument might be simplified. We have to go through the Attributor
2945     // interface though as outside AAs can register custom simplification
2946     // callbacks.
2947     bool UsedAssumedInformation = false;
2948     getAssumedSimplified(ArgPos, /* AA */ nullptr, UsedAssumedInformation,
2949                          AA::Intraprocedural);
2950 
2951     // Every argument might be dead.
2952     getOrCreateAAFor<AAIsDead>(ArgPos);
2953 
2954     // Every argument might be marked noundef.
2955     getOrCreateAAFor<AANoUndef>(ArgPos);
2956 
2957     if (Arg.getType()->isPointerTy()) {
2958       // Every argument with pointer type might be marked nonnull.
2959       getOrCreateAAFor<AANonNull>(ArgPos);
2960 
2961       // Every argument with pointer type might be marked noalias.
2962       getOrCreateAAFor<AANoAlias>(ArgPos);
2963 
2964       // Every argument with pointer type might be marked dereferenceable.
2965       getOrCreateAAFor<AADereferenceable>(ArgPos);
2966 
2967       // Every argument with pointer type might be marked align.
2968       getOrCreateAAFor<AAAlign>(ArgPos);
2969 
2970       // Every argument with pointer type might be marked nocapture.
2971       getOrCreateAAFor<AANoCapture>(ArgPos);
2972 
2973       // Every argument with pointer type might be marked
2974       // "readnone/readonly/writeonly/..."
2975       getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
2976 
2977       // Every argument with pointer type might be marked nofree.
2978       getOrCreateAAFor<AANoFree>(ArgPos);
2979 
2980       // Every argument with pointer type might be privatizable (or promotable)
2981       getOrCreateAAFor<AAPrivatizablePtr>(ArgPos);
2982     }
2983   }
2984 
2985   auto CallSitePred = [&](Instruction &I) -> bool {
2986     auto &CB = cast<CallBase>(I);
2987     IRPosition CBInstPos = IRPosition::inst(CB);
2988     IRPosition CBFnPos = IRPosition::callsite_function(CB);
2989 
2990     // Call sites might be dead if they do not have side effects and no live
2991     // users. The return value might be dead if there are no live users.
2992     getOrCreateAAFor<AAIsDead>(CBInstPos);
2993 
2994     Function *Callee = CB.getCalledFunction();
2995     // TODO: Even if the callee is not known now we might be able to simplify
2996     //       the call/callee.
2997     if (!Callee)
2998       return true;
2999 
3000     // Every call site can track active assumptions.
3001     getOrCreateAAFor<AAAssumptionInfo>(CBFnPos);
3002 
3003     // Skip declarations except if annotations on their call sites were
3004     // explicitly requested.
3005     if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
3006         !Callee->hasMetadata(LLVMContext::MD_callback))
3007       return true;
3008 
3009     if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) {
3010 
3011       IRPosition CBRetPos = IRPosition::callsite_returned(CB);
3012       bool UsedAssumedInformation = false;
3013       getAssumedSimplified(CBRetPos, nullptr, UsedAssumedInformation,
3014                            AA::Intraprocedural);
3015     }
3016 
3017     for (int I = 0, E = CB.arg_size(); I < E; ++I) {
3018 
3019       IRPosition CBArgPos = IRPosition::callsite_argument(CB, I);
3020 
3021       // Every call site argument might be dead.
3022       getOrCreateAAFor<AAIsDead>(CBArgPos);
3023 
3024       // Call site argument might be simplified. We have to go through the
3025       // Attributor interface though as outside AAs can register custom
3026       // simplification callbacks.
3027       bool UsedAssumedInformation = false;
3028       getAssumedSimplified(CBArgPos, /* AA */ nullptr, UsedAssumedInformation,
3029                            AA::Intraprocedural);
3030 
3031       // Every call site argument might be marked "noundef".
3032       getOrCreateAAFor<AANoUndef>(CBArgPos);
3033 
3034       if (!CB.getArgOperand(I)->getType()->isPointerTy())
3035         continue;
3036 
3037       // Call site argument attribute "non-null".
3038       getOrCreateAAFor<AANonNull>(CBArgPos);
3039 
3040       // Call site argument attribute "nocapture".
3041       getOrCreateAAFor<AANoCapture>(CBArgPos);
3042 
3043       // Call site argument attribute "no-alias".
3044       getOrCreateAAFor<AANoAlias>(CBArgPos);
3045 
3046       // Call site argument attribute "dereferenceable".
3047       getOrCreateAAFor<AADereferenceable>(CBArgPos);
3048 
3049       // Call site argument attribute "align".
3050       getOrCreateAAFor<AAAlign>(CBArgPos);
3051 
3052       // Call site argument attribute
3053       // "readnone/readonly/writeonly/..."
3054       getOrCreateAAFor<AAMemoryBehavior>(CBArgPos);
3055 
3056       // Call site argument attribute "nofree".
3057       getOrCreateAAFor<AANoFree>(CBArgPos);
3058     }
3059     return true;
3060   };
3061 
3062   auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
3063   bool Success;
3064   bool UsedAssumedInformation = false;
3065   Success = checkForAllInstructionsImpl(
3066       nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
3067       {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
3068        (unsigned)Instruction::Call},
3069       UsedAssumedInformation);
3070   (void)Success;
3071   assert(Success && "Expected the check call to be successful!");
3072 
3073   auto LoadStorePred = [&](Instruction &I) -> bool {
3074     if (isa<LoadInst>(I)) {
3075       getOrCreateAAFor<AAAlign>(
3076           IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
3077       if (SimplifyAllLoads)
3078         getAssumedSimplified(IRPosition::value(I), nullptr,
3079                              UsedAssumedInformation, AA::Intraprocedural);
3080     } else {
3081       auto &SI = cast<StoreInst>(I);
3082       getOrCreateAAFor<AAIsDead>(IRPosition::inst(I));
3083       getAssumedSimplified(IRPosition::value(*SI.getValueOperand()), nullptr,
3084                            UsedAssumedInformation, AA::Intraprocedural);
3085       getOrCreateAAFor<AAAlign>(IRPosition::value(*SI.getPointerOperand()));
3086     }
3087     return true;
3088   };
3089   Success = checkForAllInstructionsImpl(
3090       nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
3091       {(unsigned)Instruction::Load, (unsigned)Instruction::Store},
3092       UsedAssumedInformation);
3093   (void)Success;
3094   assert(Success && "Expected the check call to be successful!");
3095 }
3096 
3097 /// Helpers to ease debugging through output streams and print calls.
3098 ///
3099 ///{
3100 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
3101   return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
3102 }
3103 
3104 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
3105   switch (AP) {
3106   case IRPosition::IRP_INVALID:
3107     return OS << "inv";
3108   case IRPosition::IRP_FLOAT:
3109     return OS << "flt";
3110   case IRPosition::IRP_RETURNED:
3111     return OS << "fn_ret";
3112   case IRPosition::IRP_CALL_SITE_RETURNED:
3113     return OS << "cs_ret";
3114   case IRPosition::IRP_FUNCTION:
3115     return OS << "fn";
3116   case IRPosition::IRP_CALL_SITE:
3117     return OS << "cs";
3118   case IRPosition::IRP_ARGUMENT:
3119     return OS << "arg";
3120   case IRPosition::IRP_CALL_SITE_ARGUMENT:
3121     return OS << "cs_arg";
3122   }
3123   llvm_unreachable("Unknown attribute position!");
3124 }
3125 
3126 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
3127   const Value &AV = Pos.getAssociatedValue();
3128   OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
3129      << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo() << "]";
3130 
3131   if (Pos.hasCallBaseContext())
3132     OS << "[cb_context:" << *Pos.getCallBaseContext() << "]";
3133   return OS << "}";
3134 }
3135 
3136 raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) {
3137   OS << "range-state(" << S.getBitWidth() << ")<";
3138   S.getKnown().print(OS);
3139   OS << " / ";
3140   S.getAssumed().print(OS);
3141   OS << ">";
3142 
3143   return OS << static_cast<const AbstractState &>(S);
3144 }
3145 
3146 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
3147   return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
3148 }
3149 
3150 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
3151   AA.print(OS);
3152   return OS;
3153 }
3154 
3155 raw_ostream &llvm::operator<<(raw_ostream &OS,
3156                               const PotentialConstantIntValuesState &S) {
3157   OS << "set-state(< {";
3158   if (!S.isValidState())
3159     OS << "full-set";
3160   else {
3161     for (auto &It : S.getAssumedSet())
3162       OS << It << ", ";
3163     if (S.undefIsContained())
3164       OS << "undef ";
3165   }
3166   OS << "} >)";
3167 
3168   return OS;
3169 }
3170 
3171 raw_ostream &llvm::operator<<(raw_ostream &OS,
3172                               const PotentialLLVMValuesState &S) {
3173   OS << "set-state(< {";
3174   if (!S.isValidState())
3175     OS << "full-set";
3176   else {
3177     for (auto &It : S.getAssumedSet()) {
3178       if (auto *F = dyn_cast<Function>(It.first.getValue()))
3179         OS << "@" << F->getName() << "[" << int(It.second) << "], ";
3180       else
3181         OS << *It.first.getValue() << "[" << int(It.second) << "], ";
3182     }
3183     if (S.undefIsContained())
3184       OS << "undef ";
3185   }
3186   OS << "} >)";
3187 
3188   return OS;
3189 }
3190 
3191 void AbstractAttribute::print(raw_ostream &OS) const {
3192   OS << "[";
3193   OS << getName();
3194   OS << "] for CtxI ";
3195 
3196   if (auto *I = getCtxI()) {
3197     OS << "'";
3198     I->print(OS);
3199     OS << "'";
3200   } else
3201     OS << "<<null inst>>";
3202 
3203   OS << " at position " << getIRPosition() << " with state " << getAsStr()
3204      << '\n';
3205 }
3206 
3207 void AbstractAttribute::printWithDeps(raw_ostream &OS) const {
3208   print(OS);
3209 
3210   for (const auto &DepAA : Deps) {
3211     auto *AA = DepAA.getPointer();
3212     OS << "  updates ";
3213     AA->print(OS);
3214   }
3215 
3216   OS << '\n';
3217 }
3218 
3219 raw_ostream &llvm::operator<<(raw_ostream &OS,
3220                               const AAPointerInfo::Access &Acc) {
3221   OS << " [" << Acc.getKind() << "] " << *Acc.getRemoteInst();
3222   if (Acc.getLocalInst() != Acc.getRemoteInst())
3223     OS << " via " << *Acc.getLocalInst();
3224   if (Acc.getContent()) {
3225     if (*Acc.getContent())
3226       OS << " [" << **Acc.getContent() << "]";
3227     else
3228       OS << " [ <unknown> ]";
3229   }
3230   return OS;
3231 }
3232 ///}
3233 
3234 /// ----------------------------------------------------------------------------
3235 ///                       Pass (Manager) Boilerplate
3236 /// ----------------------------------------------------------------------------
3237 
3238 static bool runAttributorOnFunctions(InformationCache &InfoCache,
3239                                      SetVector<Function *> &Functions,
3240                                      AnalysisGetter &AG,
3241                                      CallGraphUpdater &CGUpdater,
3242                                      bool DeleteFns, bool IsModulePass) {
3243   if (Functions.empty())
3244     return false;
3245 
3246   LLVM_DEBUG({
3247     dbgs() << "[Attributor] Run on module with " << Functions.size()
3248            << " functions:\n";
3249     for (Function *Fn : Functions)
3250       dbgs() << "  - " << Fn->getName() << "\n";
3251   });
3252 
3253   // Create an Attributor and initially empty information cache that is filled
3254   // while we identify default attribute opportunities.
3255   AttributorConfig AC(CGUpdater);
3256   AC.IsModulePass = IsModulePass;
3257   AC.DeleteFns = DeleteFns;
3258   Attributor A(Functions, InfoCache, AC);
3259 
3260   // Create shallow wrappers for all functions that are not IPO amendable
3261   if (AllowShallowWrappers)
3262     for (Function *F : Functions)
3263       if (!A.isFunctionIPOAmendable(*F))
3264         Attributor::createShallowWrapper(*F);
3265 
3266   // Internalize non-exact functions
3267   // TODO: for now we eagerly internalize functions without calculating the
3268   //       cost, we need a cost interface to determine whether internalizing
3269   //       a function is "benefitial"
3270   if (AllowDeepWrapper) {
3271     unsigned FunSize = Functions.size();
3272     for (unsigned u = 0; u < FunSize; u++) {
3273       Function *F = Functions[u];
3274       if (!F->isDeclaration() && !F->isDefinitionExact() && F->getNumUses() &&
3275           !GlobalValue::isInterposableLinkage(F->getLinkage())) {
3276         Function *NewF = Attributor::internalizeFunction(*F);
3277         assert(NewF && "Could not internalize function.");
3278         Functions.insert(NewF);
3279 
3280         // Update call graph
3281         CGUpdater.replaceFunctionWith(*F, *NewF);
3282         for (const Use &U : NewF->uses())
3283           if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) {
3284             auto *CallerF = CB->getCaller();
3285             CGUpdater.reanalyzeFunction(*CallerF);
3286           }
3287       }
3288     }
3289   }
3290 
3291   for (Function *F : Functions) {
3292     if (F->hasExactDefinition())
3293       NumFnWithExactDefinition++;
3294     else
3295       NumFnWithoutExactDefinition++;
3296 
3297     // We look at internal functions only on-demand but if any use is not a
3298     // direct call or outside the current set of analyzed functions, we have
3299     // to do it eagerly.
3300     if (F->hasLocalLinkage()) {
3301       if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
3302             const auto *CB = dyn_cast<CallBase>(U.getUser());
3303             return CB && CB->isCallee(&U) &&
3304                    Functions.count(const_cast<Function *>(CB->getCaller()));
3305           }))
3306         continue;
3307     }
3308 
3309     // Populate the Attributor with abstract attribute opportunities in the
3310     // function and the information cache with IR information.
3311     A.identifyDefaultAbstractAttributes(*F);
3312   }
3313 
3314   ChangeStatus Changed = A.run();
3315 
3316   LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
3317                     << " functions, result: " << Changed << ".\n");
3318   return Changed == ChangeStatus::CHANGED;
3319 }
3320 
3321 void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
3322 
3323 void AADepGraph::dumpGraph() {
3324   static std::atomic<int> CallTimes;
3325   std::string Prefix;
3326 
3327   if (!DepGraphDotFileNamePrefix.empty())
3328     Prefix = DepGraphDotFileNamePrefix;
3329   else
3330     Prefix = "dep_graph";
3331   std::string Filename =
3332       Prefix + "_" + std::to_string(CallTimes.load()) + ".dot";
3333 
3334   outs() << "Dependency graph dump to " << Filename << ".\n";
3335 
3336   std::error_code EC;
3337 
3338   raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
3339   if (!EC)
3340     llvm::WriteGraph(File, this);
3341 
3342   CallTimes++;
3343 }
3344 
3345 void AADepGraph::print() {
3346   for (auto DepAA : SyntheticRoot.Deps)
3347     cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs());
3348 }
3349 
3350 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
3351   FunctionAnalysisManager &FAM =
3352       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
3353   AnalysisGetter AG(FAM);
3354 
3355   SetVector<Function *> Functions;
3356   for (Function &F : M)
3357     Functions.insert(&F);
3358 
3359   CallGraphUpdater CGUpdater;
3360   BumpPtrAllocator Allocator;
3361   InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
3362   if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
3363                                /* DeleteFns */ true, /* IsModulePass */ true)) {
3364     // FIXME: Think about passes we will preserve and add them here.
3365     return PreservedAnalyses::none();
3366   }
3367   return PreservedAnalyses::all();
3368 }
3369 
3370 PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C,
3371                                            CGSCCAnalysisManager &AM,
3372                                            LazyCallGraph &CG,
3373                                            CGSCCUpdateResult &UR) {
3374   FunctionAnalysisManager &FAM =
3375       AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
3376   AnalysisGetter AG(FAM);
3377 
3378   SetVector<Function *> Functions;
3379   for (LazyCallGraph::Node &N : C)
3380     Functions.insert(&N.getFunction());
3381 
3382   if (Functions.empty())
3383     return PreservedAnalyses::all();
3384 
3385   Module &M = *Functions.back()->getParent();
3386   CallGraphUpdater CGUpdater;
3387   CGUpdater.initialize(CG, C, AM, UR);
3388   BumpPtrAllocator Allocator;
3389   InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
3390   if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
3391                                /* DeleteFns */ false,
3392                                /* IsModulePass */ false)) {
3393     // FIXME: Think about passes we will preserve and add them here.
3394     PreservedAnalyses PA;
3395     PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
3396     return PA;
3397   }
3398   return PreservedAnalyses::all();
3399 }
3400 
3401 namespace llvm {
3402 
3403 template <> struct GraphTraits<AADepGraphNode *> {
3404   using NodeRef = AADepGraphNode *;
3405   using DepTy = PointerIntPair<AADepGraphNode *, 1>;
3406   using EdgeRef = PointerIntPair<AADepGraphNode *, 1>;
3407 
3408   static NodeRef getEntryNode(AADepGraphNode *DGN) { return DGN; }
3409   static NodeRef DepGetVal(DepTy &DT) { return DT.getPointer(); }
3410 
3411   using ChildIteratorType =
3412       mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
3413   using ChildEdgeIteratorType = TinyPtrVector<DepTy>::iterator;
3414 
3415   static ChildIteratorType child_begin(NodeRef N) { return N->child_begin(); }
3416 
3417   static ChildIteratorType child_end(NodeRef N) { return N->child_end(); }
3418 };
3419 
3420 template <>
3421 struct GraphTraits<AADepGraph *> : public GraphTraits<AADepGraphNode *> {
3422   static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); }
3423 
3424   using nodes_iterator =
3425       mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
3426 
3427   static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); }
3428 
3429   static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); }
3430 };
3431 
3432 template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits {
3433   DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
3434 
3435   static std::string getNodeLabel(const AADepGraphNode *Node,
3436                                   const AADepGraph *DG) {
3437     std::string AAString;
3438     raw_string_ostream O(AAString);
3439     Node->print(O);
3440     return AAString;
3441   }
3442 };
3443 
3444 } // end namespace llvm
3445 
3446 namespace {
3447 
3448 struct AttributorLegacyPass : public ModulePass {
3449   static char ID;
3450 
3451   AttributorLegacyPass() : ModulePass(ID) {
3452     initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
3453   }
3454 
3455   bool runOnModule(Module &M) override {
3456     if (skipModule(M))
3457       return false;
3458 
3459     AnalysisGetter AG;
3460     SetVector<Function *> Functions;
3461     for (Function &F : M)
3462       Functions.insert(&F);
3463 
3464     CallGraphUpdater CGUpdater;
3465     BumpPtrAllocator Allocator;
3466     InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
3467     return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
3468                                     /* DeleteFns*/ true,
3469                                     /* IsModulePass */ true);
3470   }
3471 
3472   void getAnalysisUsage(AnalysisUsage &AU) const override {
3473     // FIXME: Think about passes we will preserve and add them here.
3474     AU.addRequired<TargetLibraryInfoWrapperPass>();
3475   }
3476 };
3477 
3478 struct AttributorCGSCCLegacyPass : public CallGraphSCCPass {
3479   static char ID;
3480 
3481   AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) {
3482     initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
3483   }
3484 
3485   bool runOnSCC(CallGraphSCC &SCC) override {
3486     if (skipSCC(SCC))
3487       return false;
3488 
3489     SetVector<Function *> Functions;
3490     for (CallGraphNode *CGN : SCC)
3491       if (Function *Fn = CGN->getFunction())
3492         if (!Fn->isDeclaration())
3493           Functions.insert(Fn);
3494 
3495     if (Functions.empty())
3496       return false;
3497 
3498     AnalysisGetter AG;
3499     CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph());
3500     CallGraphUpdater CGUpdater;
3501     CGUpdater.initialize(CG, SCC);
3502     Module &M = *Functions.back()->getParent();
3503     BumpPtrAllocator Allocator;
3504     InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
3505     return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
3506                                     /* DeleteFns */ false,
3507                                     /* IsModulePass */ false);
3508   }
3509 
3510   void getAnalysisUsage(AnalysisUsage &AU) const override {
3511     // FIXME: Think about passes we will preserve and add them here.
3512     AU.addRequired<TargetLibraryInfoWrapperPass>();
3513     CallGraphSCCPass::getAnalysisUsage(AU);
3514   }
3515 };
3516 
3517 } // end anonymous namespace
3518 
3519 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
3520 Pass *llvm::createAttributorCGSCCLegacyPass() {
3521   return new AttributorCGSCCLegacyPass();
3522 }
3523 
3524 char AttributorLegacyPass::ID = 0;
3525 char AttributorCGSCCLegacyPass::ID = 0;
3526 
3527 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
3528                       "Deduce and propagate attributes", false, false)
3529 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
3530 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
3531                     "Deduce and propagate attributes", false, false)
3532 INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc",
3533                       "Deduce and propagate attributes (CGSCC pass)", false,
3534                       false)
3535 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
3536 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
3537 INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc",
3538                     "Deduce and propagate attributes (CGSCC pass)", false,
3539                     false)
3540