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