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