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