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