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