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