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