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