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 =
539       getAAFor<AAValueSimplify>(AA, IRPosition::value(V), DepClassTy::NONE);
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, DepClassTy::NONE);
619 
620   // If we have a context instruction and a liveness AA we use it.
621   if (FnLivenessAA &&
622       FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() &&
623       FnLivenessAA->isAssumedDead(&I)) {
624     if (QueryingAA)
625       recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
626     return true;
627   }
628 
629   if (CheckBBLivenessOnly)
630     return false;
631 
632   const AAIsDead &IsDeadAA = getOrCreateAAFor<AAIsDead>(
633       IRPosition::value(I), QueryingAA, DepClassTy::NONE);
634   // Don't check liveness for AAIsDead.
635   if (QueryingAA == &IsDeadAA)
636     return false;
637 
638   if (IsDeadAA.isAssumedDead()) {
639     if (QueryingAA)
640       recordDependence(IsDeadAA, *QueryingAA, DepClass);
641     return true;
642   }
643 
644   return false;
645 }
646 
647 bool Attributor::isAssumedDead(const IRPosition &IRP,
648                                const AbstractAttribute *QueryingAA,
649                                const AAIsDead *FnLivenessAA,
650                                bool CheckBBLivenessOnly, DepClassTy DepClass) {
651   Instruction *CtxI = IRP.getCtxI();
652   if (CtxI &&
653       isAssumedDead(*CtxI, QueryingAA, FnLivenessAA,
654                     /* CheckBBLivenessOnly */ true,
655                     CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
656     return true;
657 
658   if (CheckBBLivenessOnly)
659     return false;
660 
661   // If we haven't succeeded we query the specific liveness info for the IRP.
662   const AAIsDead *IsDeadAA;
663   if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE)
664     IsDeadAA = &getOrCreateAAFor<AAIsDead>(
665         IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())),
666         QueryingAA, DepClassTy::NONE);
667   else
668     IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
669   // Don't check liveness for AAIsDead.
670   if (QueryingAA == IsDeadAA)
671     return false;
672 
673   if (IsDeadAA->isAssumedDead()) {
674     if (QueryingAA)
675       recordDependence(*IsDeadAA, *QueryingAA, DepClass);
676     return true;
677   }
678 
679   return false;
680 }
681 
682 bool Attributor::checkForAllUses(function_ref<bool(const Use &, bool &)> Pred,
683                                  const AbstractAttribute &QueryingAA,
684                                  const Value &V, DepClassTy LivenessDepClass) {
685 
686   // Check the trivial case first as it catches void values.
687   if (V.use_empty())
688     return true;
689 
690   // If the value is replaced by another one, for now a constant, we do not have
691   // uses. Note that this requires users of `checkForAllUses` to not recurse but
692   // instead use the `follow` callback argument to look at transitive users,
693   // however, that should be clear from the presence of the argument.
694   bool UsedAssumedInformation = false;
695   Optional<Constant *> C =
696       getAssumedConstant(V, QueryingAA, UsedAssumedInformation);
697   if (C.hasValue() && C.getValue()) {
698     LLVM_DEBUG(dbgs() << "[Attributor] Value is simplified, uses skipped: " << V
699                       << " -> " << *C.getValue() << "\n");
700     return true;
701   }
702 
703   const IRPosition &IRP = QueryingAA.getIRPosition();
704   SmallVector<const Use *, 16> Worklist;
705   SmallPtrSet<const Use *, 16> Visited;
706 
707   for (const Use &U : V.uses())
708     Worklist.push_back(&U);
709 
710   LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
711                     << " initial uses to check\n");
712 
713   const Function *ScopeFn = IRP.getAnchorScope();
714   const auto *LivenessAA =
715       ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
716                                     DepClassTy::NONE)
717               : nullptr;
718 
719   while (!Worklist.empty()) {
720     const Use *U = Worklist.pop_back_val();
721     if (!Visited.insert(U).second)
722       continue;
723     LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << " in "
724                       << *U->getUser() << "\n");
725     if (isAssumedDead(*U, &QueryingAA, LivenessAA,
726                       /* CheckBBLivenessOnly */ false, LivenessDepClass)) {
727       LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
728       continue;
729     }
730     if (U->getUser()->isDroppable()) {
731       LLVM_DEBUG(dbgs() << "[Attributor] Droppable user, skip!\n");
732       continue;
733     }
734 
735     bool Follow = false;
736     if (!Pred(*U, Follow))
737       return false;
738     if (!Follow)
739       continue;
740     for (const Use &UU : U->getUser()->uses())
741       Worklist.push_back(&UU);
742   }
743 
744   return true;
745 }
746 
747 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
748                                       const AbstractAttribute &QueryingAA,
749                                       bool RequireAllCallSites,
750                                       bool &AllCallSitesKnown) {
751   // We can try to determine information from
752   // the call sites. However, this is only possible all call sites are known,
753   // hence the function has internal linkage.
754   const IRPosition &IRP = QueryingAA.getIRPosition();
755   const Function *AssociatedFunction = IRP.getAssociatedFunction();
756   if (!AssociatedFunction) {
757     LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
758                       << "\n");
759     AllCallSitesKnown = false;
760     return false;
761   }
762 
763   return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
764                               &QueryingAA, AllCallSitesKnown);
765 }
766 
767 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
768                                       const Function &Fn,
769                                       bool RequireAllCallSites,
770                                       const AbstractAttribute *QueryingAA,
771                                       bool &AllCallSitesKnown) {
772   if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
773     LLVM_DEBUG(
774         dbgs()
775         << "[Attributor] Function " << Fn.getName()
776         << " has no internal linkage, hence not all call sites are known\n");
777     AllCallSitesKnown = false;
778     return false;
779   }
780 
781   // If we do not require all call sites we might not see all.
782   AllCallSitesKnown = RequireAllCallSites;
783 
784   SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses()));
785   for (unsigned u = 0; u < Uses.size(); ++u) {
786     const Use &U = *Uses[u];
787     LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << *U << " in "
788                       << *U.getUser() << "\n");
789     if (isAssumedDead(U, QueryingAA, nullptr, /* CheckBBLivenessOnly */ true)) {
790       LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
791       continue;
792     }
793     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
794       if (CE->isCast() && CE->getType()->isPointerTy() &&
795           CE->getType()->getPointerElementType()->isFunctionTy()) {
796         for (const Use &CEU : CE->uses())
797           Uses.push_back(&CEU);
798         continue;
799       }
800     }
801 
802     AbstractCallSite ACS(&U);
803     if (!ACS) {
804       LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
805                         << " has non call site use " << *U.get() << " in "
806                         << *U.getUser() << "\n");
807       // BlockAddress users are allowed.
808       if (isa<BlockAddress>(U.getUser()))
809         continue;
810       return false;
811     }
812 
813     const Use *EffectiveUse =
814         ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
815     if (!ACS.isCallee(EffectiveUse)) {
816       if (!RequireAllCallSites)
817         continue;
818       LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser()
819                         << " is an invalid use of " << Fn.getName() << "\n");
820       return false;
821     }
822 
823     // Make sure the arguments that can be matched between the call site and the
824     // callee argee on their type. It is unlikely they do not and it doesn't
825     // make sense for all attributes to know/care about this.
826     assert(&Fn == ACS.getCalledFunction() && "Expected known callee");
827     unsigned MinArgsParams =
828         std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
829     for (unsigned u = 0; u < MinArgsParams; ++u) {
830       Value *CSArgOp = ACS.getCallArgOperand(u);
831       if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
832         LLVM_DEBUG(
833             dbgs() << "[Attributor] Call site / callee argument type mismatch ["
834                    << u << "@" << Fn.getName() << ": "
835                    << *Fn.getArg(u)->getType() << " vs. "
836                    << *ACS.getCallArgOperand(u)->getType() << "\n");
837         return false;
838       }
839     }
840 
841     if (Pred(ACS))
842       continue;
843 
844     LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
845                       << *ACS.getInstruction() << "\n");
846     return false;
847   }
848 
849   return true;
850 }
851 
852 bool Attributor::checkForAllReturnedValuesAndReturnInsts(
853     function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred,
854     const AbstractAttribute &QueryingAA) {
855 
856   const IRPosition &IRP = QueryingAA.getIRPosition();
857   // Since we need to provide return instructions we have to have an exact
858   // definition.
859   const Function *AssociatedFunction = IRP.getAssociatedFunction();
860   if (!AssociatedFunction)
861     return false;
862 
863   // If this is a call site query we use the call site specific return values
864   // and liveness information.
865   // TODO: use the function scope once we have call site AAReturnedValues.
866   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
867   const auto &AARetVal =
868       getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED);
869   if (!AARetVal.getState().isValidState())
870     return false;
871 
872   return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
873 }
874 
875 bool Attributor::checkForAllReturnedValues(
876     function_ref<bool(Value &)> Pred, const AbstractAttribute &QueryingAA) {
877 
878   const IRPosition &IRP = QueryingAA.getIRPosition();
879   const Function *AssociatedFunction = IRP.getAssociatedFunction();
880   if (!AssociatedFunction)
881     return false;
882 
883   // TODO: use the function scope once we have call site AAReturnedValues.
884   const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
885   const auto &AARetVal =
886       getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED);
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
935           ? nullptr
936           : &(getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE));
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, DepClassTy::NONE);
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 (DepClass == DepClassTy::NONE)
1944     return;
1945   // If we are outside of an update, thus before the actual fixpoint iteration
1946   // started (= when we create AAs), we do not track dependences because we will
1947   // put all AAs into the initial worklist anyway.
1948   if (DependenceStack.empty())
1949     return;
1950   if (FromAA.getState().isAtFixpoint())
1951     return;
1952   DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass});
1953 }
1954 
1955 void Attributor::rememberDependences() {
1956   assert(!DependenceStack.empty() && "No dependences to remember!");
1957 
1958   for (DepInfo &DI : *DependenceStack.back()) {
1959     assert((DI.DepClass == DepClassTy::REQUIRED ||
1960             DI.DepClass == DepClassTy::OPTIONAL) &&
1961            "Expected required or optional dependence (1 bit)!");
1962     auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps;
1963     DepAAs.push_back(AbstractAttribute::DepTy(
1964         const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass)));
1965   }
1966 }
1967 
1968 void Attributor::identifyDefaultAbstractAttributes(Function &F) {
1969   if (!VisitedFunctions.insert(&F).second)
1970     return;
1971   if (F.isDeclaration())
1972     return;
1973 
1974   // In non-module runs we need to look at the call sites of a function to
1975   // determine if it is part of a must-tail call edge. This will influence what
1976   // attributes we can derive.
1977   InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F);
1978   if (!isModulePass() && !FI.CalledViaMustTail) {
1979     for (const Use &U : F.uses())
1980       if (const auto *CB = dyn_cast<CallBase>(U.getUser()))
1981         if (CB->isCallee(&U) && CB->isMustTailCall())
1982           FI.CalledViaMustTail = true;
1983   }
1984 
1985   IRPosition FPos = IRPosition::function(F);
1986 
1987   // Check for dead BasicBlocks in every function.
1988   // We need dead instruction detection because we do not want to deal with
1989   // broken IR in which SSA rules do not apply.
1990   getOrCreateAAFor<AAIsDead>(FPos);
1991 
1992   // Every function might be "will-return".
1993   getOrCreateAAFor<AAWillReturn>(FPos);
1994 
1995   // Every function might contain instructions that cause "undefined behavior".
1996   getOrCreateAAFor<AAUndefinedBehavior>(FPos);
1997 
1998   // Every function can be nounwind.
1999   getOrCreateAAFor<AANoUnwind>(FPos);
2000 
2001   // Every function might be marked "nosync"
2002   getOrCreateAAFor<AANoSync>(FPos);
2003 
2004   // Every function might be "no-free".
2005   getOrCreateAAFor<AANoFree>(FPos);
2006 
2007   // Every function might be "no-return".
2008   getOrCreateAAFor<AANoReturn>(FPos);
2009 
2010   // Every function might be "no-recurse".
2011   getOrCreateAAFor<AANoRecurse>(FPos);
2012 
2013   // Every function might be "readnone/readonly/writeonly/...".
2014   getOrCreateAAFor<AAMemoryBehavior>(FPos);
2015 
2016   // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
2017   getOrCreateAAFor<AAMemoryLocation>(FPos);
2018 
2019   // Every function might be applicable for Heap-To-Stack conversion.
2020   if (EnableHeapToStack)
2021     getOrCreateAAFor<AAHeapToStack>(FPos);
2022 
2023   // Return attributes are only appropriate if the return type is non void.
2024   Type *ReturnType = F.getReturnType();
2025   if (!ReturnType->isVoidTy()) {
2026     // Argument attribute "returned" --- Create only one per function even
2027     // though it is an argument attribute.
2028     getOrCreateAAFor<AAReturnedValues>(FPos);
2029 
2030     IRPosition RetPos = IRPosition::returned(F);
2031 
2032     // Every returned value might be dead.
2033     getOrCreateAAFor<AAIsDead>(RetPos);
2034 
2035     // Every function might be simplified.
2036     getOrCreateAAFor<AAValueSimplify>(RetPos);
2037 
2038     // Every returned value might be marked noundef.
2039     getOrCreateAAFor<AANoUndef>(RetPos);
2040 
2041     if (ReturnType->isPointerTy()) {
2042 
2043       // Every function with pointer return type might be marked align.
2044       getOrCreateAAFor<AAAlign>(RetPos);
2045 
2046       // Every function with pointer return type might be marked nonnull.
2047       getOrCreateAAFor<AANonNull>(RetPos);
2048 
2049       // Every function with pointer return type might be marked noalias.
2050       getOrCreateAAFor<AANoAlias>(RetPos);
2051 
2052       // Every function with pointer return type might be marked
2053       // dereferenceable.
2054       getOrCreateAAFor<AADereferenceable>(RetPos);
2055     }
2056   }
2057 
2058   for (Argument &Arg : F.args()) {
2059     IRPosition ArgPos = IRPosition::argument(Arg);
2060 
2061     // Every argument might be simplified.
2062     getOrCreateAAFor<AAValueSimplify>(ArgPos);
2063 
2064     // Every argument might be dead.
2065     getOrCreateAAFor<AAIsDead>(ArgPos);
2066 
2067     // Every argument might be marked noundef.
2068     getOrCreateAAFor<AANoUndef>(ArgPos);
2069 
2070     if (Arg.getType()->isPointerTy()) {
2071       // Every argument with pointer type might be marked nonnull.
2072       getOrCreateAAFor<AANonNull>(ArgPos);
2073 
2074       // Every argument with pointer type might be marked noalias.
2075       getOrCreateAAFor<AANoAlias>(ArgPos);
2076 
2077       // Every argument with pointer type might be marked dereferenceable.
2078       getOrCreateAAFor<AADereferenceable>(ArgPos);
2079 
2080       // Every argument with pointer type might be marked align.
2081       getOrCreateAAFor<AAAlign>(ArgPos);
2082 
2083       // Every argument with pointer type might be marked nocapture.
2084       getOrCreateAAFor<AANoCapture>(ArgPos);
2085 
2086       // Every argument with pointer type might be marked
2087       // "readnone/readonly/writeonly/..."
2088       getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
2089 
2090       // Every argument with pointer type might be marked nofree.
2091       getOrCreateAAFor<AANoFree>(ArgPos);
2092 
2093       // Every argument with pointer type might be privatizable (or promotable)
2094       getOrCreateAAFor<AAPrivatizablePtr>(ArgPos);
2095     }
2096   }
2097 
2098   auto CallSitePred = [&](Instruction &I) -> bool {
2099     auto &CB = cast<CallBase>(I);
2100     IRPosition CBRetPos = IRPosition::callsite_returned(CB);
2101 
2102     // Call sites might be dead if they do not have side effects and no live
2103     // users. The return value might be dead if there are no live users.
2104     getOrCreateAAFor<AAIsDead>(CBRetPos);
2105 
2106     Function *Callee = CB.getCalledFunction();
2107     // TODO: Even if the callee is not known now we might be able to simplify
2108     //       the call/callee.
2109     if (!Callee)
2110       return true;
2111 
2112     // Skip declarations except if annotations on their call sites were
2113     // explicitly requested.
2114     if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
2115         !Callee->hasMetadata(LLVMContext::MD_callback))
2116       return true;
2117 
2118     if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) {
2119 
2120       IRPosition CBRetPos = IRPosition::callsite_returned(CB);
2121 
2122       // Call site return integer values might be limited by a constant range.
2123       if (Callee->getReturnType()->isIntegerTy())
2124         getOrCreateAAFor<AAValueConstantRange>(CBRetPos);
2125     }
2126 
2127     for (int I = 0, E = CB.getNumArgOperands(); I < E; ++I) {
2128 
2129       IRPosition CBArgPos = IRPosition::callsite_argument(CB, I);
2130 
2131       // Every call site argument might be dead.
2132       getOrCreateAAFor<AAIsDead>(CBArgPos);
2133 
2134       // Call site argument might be simplified.
2135       getOrCreateAAFor<AAValueSimplify>(CBArgPos);
2136 
2137       // Every call site argument might be marked "noundef".
2138       getOrCreateAAFor<AANoUndef>(CBArgPos);
2139 
2140       if (!CB.getArgOperand(I)->getType()->isPointerTy())
2141         continue;
2142 
2143       // Call site argument attribute "non-null".
2144       getOrCreateAAFor<AANonNull>(CBArgPos);
2145 
2146       // Call site argument attribute "nocapture".
2147       getOrCreateAAFor<AANoCapture>(CBArgPos);
2148 
2149       // Call site argument attribute "no-alias".
2150       getOrCreateAAFor<AANoAlias>(CBArgPos);
2151 
2152       // Call site argument attribute "dereferenceable".
2153       getOrCreateAAFor<AADereferenceable>(CBArgPos);
2154 
2155       // Call site argument attribute "align".
2156       getOrCreateAAFor<AAAlign>(CBArgPos);
2157 
2158       // Call site argument attribute
2159       // "readnone/readonly/writeonly/..."
2160       getOrCreateAAFor<AAMemoryBehavior>(CBArgPos);
2161 
2162       // Call site argument attribute "nofree".
2163       getOrCreateAAFor<AANoFree>(CBArgPos);
2164     }
2165     return true;
2166   };
2167 
2168   auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
2169   bool Success;
2170   Success = checkForAllInstructionsImpl(
2171       nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
2172       {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
2173        (unsigned)Instruction::Call});
2174   (void)Success;
2175   assert(Success && "Expected the check call to be successful!");
2176 
2177   auto LoadStorePred = [&](Instruction &I) -> bool {
2178     if (isa<LoadInst>(I))
2179       getOrCreateAAFor<AAAlign>(
2180           IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
2181     else
2182       getOrCreateAAFor<AAAlign>(
2183           IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
2184     return true;
2185   };
2186   Success = checkForAllInstructionsImpl(
2187       nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
2188       {(unsigned)Instruction::Load, (unsigned)Instruction::Store});
2189   (void)Success;
2190   assert(Success && "Expected the check call to be successful!");
2191 }
2192 
2193 /// Helpers to ease debugging through output streams and print calls.
2194 ///
2195 ///{
2196 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
2197   return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
2198 }
2199 
2200 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
2201   switch (AP) {
2202   case IRPosition::IRP_INVALID:
2203     return OS << "inv";
2204   case IRPosition::IRP_FLOAT:
2205     return OS << "flt";
2206   case IRPosition::IRP_RETURNED:
2207     return OS << "fn_ret";
2208   case IRPosition::IRP_CALL_SITE_RETURNED:
2209     return OS << "cs_ret";
2210   case IRPosition::IRP_FUNCTION:
2211     return OS << "fn";
2212   case IRPosition::IRP_CALL_SITE:
2213     return OS << "cs";
2214   case IRPosition::IRP_ARGUMENT:
2215     return OS << "arg";
2216   case IRPosition::IRP_CALL_SITE_ARGUMENT:
2217     return OS << "cs_arg";
2218   }
2219   llvm_unreachable("Unknown attribute position!");
2220 }
2221 
2222 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
2223   const Value &AV = Pos.getAssociatedValue();
2224   return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
2225             << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo()
2226             << "]}";
2227 }
2228 
2229 raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) {
2230   OS << "range-state(" << S.getBitWidth() << ")<";
2231   S.getKnown().print(OS);
2232   OS << " / ";
2233   S.getAssumed().print(OS);
2234   OS << ">";
2235 
2236   return OS << static_cast<const AbstractState &>(S);
2237 }
2238 
2239 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
2240   return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
2241 }
2242 
2243 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
2244   AA.print(OS);
2245   return OS;
2246 }
2247 
2248 raw_ostream &llvm::operator<<(raw_ostream &OS,
2249                               const PotentialConstantIntValuesState &S) {
2250   OS << "set-state(< {";
2251   if (!S.isValidState())
2252     OS << "full-set";
2253   else {
2254     for (auto &it : S.getAssumedSet())
2255       OS << it << ", ";
2256     if (S.undefIsContained())
2257       OS << "undef ";
2258   }
2259   OS << "} >)";
2260 
2261   return OS;
2262 }
2263 
2264 void AbstractAttribute::print(raw_ostream &OS) const {
2265   OS << "[";
2266   OS << getName();
2267   OS << "] for CtxI ";
2268 
2269   if (auto *I = getCtxI()) {
2270     OS << "'";
2271     I->print(OS);
2272     OS << "'";
2273   } else
2274     OS << "<<null inst>>";
2275 
2276   OS << " at position " << getIRPosition() << " with state " << getAsStr()
2277      << '\n';
2278 }
2279 
2280 void AbstractAttribute::printWithDeps(raw_ostream &OS) const {
2281   print(OS);
2282 
2283   for (const auto &DepAA : Deps) {
2284     auto *AA = DepAA.getPointer();
2285     OS << "  updates ";
2286     AA->print(OS);
2287   }
2288 
2289   OS << '\n';
2290 }
2291 ///}
2292 
2293 /// ----------------------------------------------------------------------------
2294 ///                       Pass (Manager) Boilerplate
2295 /// ----------------------------------------------------------------------------
2296 
2297 static bool runAttributorOnFunctions(InformationCache &InfoCache,
2298                                      SetVector<Function *> &Functions,
2299                                      AnalysisGetter &AG,
2300                                      CallGraphUpdater &CGUpdater,
2301                                      bool DeleteFns) {
2302   if (Functions.empty())
2303     return false;
2304 
2305   LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << Functions.size()
2306                     << " functions.\n");
2307 
2308   // Create an Attributor and initially empty information cache that is filled
2309   // while we identify default attribute opportunities.
2310   Attributor A(Functions, InfoCache, CGUpdater, /* Allowed */ nullptr,
2311                DeleteFns);
2312 
2313   // Create shallow wrappers for all functions that are not IPO amendable
2314   if (AllowShallowWrappers)
2315     for (Function *F : Functions)
2316       if (!A.isFunctionIPOAmendable(*F))
2317         Attributor::createShallowWrapper(*F);
2318 
2319   // Internalize non-exact functions
2320   // TODO: for now we eagerly internalize functions without calculating the
2321   //       cost, we need a cost interface to determine whether internalizing
2322   //       a function is "benefitial"
2323   if (AllowDeepWrapper) {
2324     unsigned FunSize = Functions.size();
2325     for (unsigned u = 0; u < FunSize; u++) {
2326       Function *F = Functions[u];
2327       if (!F->isDeclaration() && !F->isDefinitionExact() && F->getNumUses() &&
2328           !GlobalValue::isInterposableLinkage(F->getLinkage())) {
2329         Function *NewF = internalizeFunction(*F);
2330         Functions.insert(NewF);
2331 
2332         // Update call graph
2333         CGUpdater.replaceFunctionWith(*F, *NewF);
2334         for (const Use &U : NewF->uses())
2335           if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) {
2336             auto *CallerF = CB->getCaller();
2337             CGUpdater.reanalyzeFunction(*CallerF);
2338           }
2339       }
2340     }
2341   }
2342 
2343   for (Function *F : Functions) {
2344     if (F->hasExactDefinition())
2345       NumFnWithExactDefinition++;
2346     else
2347       NumFnWithoutExactDefinition++;
2348 
2349     // We look at internal functions only on-demand but if any use is not a
2350     // direct call or outside the current set of analyzed functions, we have
2351     // to do it eagerly.
2352     if (F->hasLocalLinkage()) {
2353       if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
2354             const auto *CB = dyn_cast<CallBase>(U.getUser());
2355             return CB && CB->isCallee(&U) &&
2356                    Functions.count(const_cast<Function *>(CB->getCaller()));
2357           }))
2358         continue;
2359     }
2360 
2361     // Populate the Attributor with abstract attribute opportunities in the
2362     // function and the information cache with IR information.
2363     A.identifyDefaultAbstractAttributes(*F);
2364   }
2365 
2366   ChangeStatus Changed = A.run();
2367 
2368   LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
2369                     << " functions, result: " << Changed << ".\n");
2370   return Changed == ChangeStatus::CHANGED;
2371 }
2372 
2373 void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
2374 
2375 void AADepGraph::dumpGraph() {
2376   static std::atomic<int> CallTimes;
2377   std::string Prefix;
2378 
2379   if (!DepGraphDotFileNamePrefix.empty())
2380     Prefix = DepGraphDotFileNamePrefix;
2381   else
2382     Prefix = "dep_graph";
2383   std::string Filename =
2384       Prefix + "_" + std::to_string(CallTimes.load()) + ".dot";
2385 
2386   outs() << "Dependency graph dump to " << Filename << ".\n";
2387 
2388   std::error_code EC;
2389 
2390   raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
2391   if (!EC)
2392     llvm::WriteGraph(File, this);
2393 
2394   CallTimes++;
2395 }
2396 
2397 void AADepGraph::print() {
2398   for (auto DepAA : SyntheticRoot.Deps)
2399     cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs());
2400 }
2401 
2402 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
2403   FunctionAnalysisManager &FAM =
2404       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
2405   AnalysisGetter AG(FAM);
2406 
2407   SetVector<Function *> Functions;
2408   for (Function &F : M)
2409     Functions.insert(&F);
2410 
2411   CallGraphUpdater CGUpdater;
2412   BumpPtrAllocator Allocator;
2413   InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
2414   if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
2415                                /* DeleteFns */ true)) {
2416     // FIXME: Think about passes we will preserve and add them here.
2417     return PreservedAnalyses::none();
2418   }
2419   return PreservedAnalyses::all();
2420 }
2421 
2422 PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C,
2423                                            CGSCCAnalysisManager &AM,
2424                                            LazyCallGraph &CG,
2425                                            CGSCCUpdateResult &UR) {
2426   FunctionAnalysisManager &FAM =
2427       AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
2428   AnalysisGetter AG(FAM);
2429 
2430   SetVector<Function *> Functions;
2431   for (LazyCallGraph::Node &N : C)
2432     Functions.insert(&N.getFunction());
2433 
2434   if (Functions.empty())
2435     return PreservedAnalyses::all();
2436 
2437   Module &M = *Functions.back()->getParent();
2438   CallGraphUpdater CGUpdater;
2439   CGUpdater.initialize(CG, C, AM, UR);
2440   BumpPtrAllocator Allocator;
2441   InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
2442   if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
2443                                /* DeleteFns */ false)) {
2444     // FIXME: Think about passes we will preserve and add them here.
2445     PreservedAnalyses PA;
2446     PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
2447     return PA;
2448   }
2449   return PreservedAnalyses::all();
2450 }
2451 
2452 namespace llvm {
2453 
2454 template <> struct GraphTraits<AADepGraphNode *> {
2455   using NodeRef = AADepGraphNode *;
2456   using DepTy = PointerIntPair<AADepGraphNode *, 1>;
2457   using EdgeRef = PointerIntPair<AADepGraphNode *, 1>;
2458 
2459   static NodeRef getEntryNode(AADepGraphNode *DGN) { return DGN; }
2460   static NodeRef DepGetVal(DepTy &DT) { return DT.getPointer(); }
2461 
2462   using ChildIteratorType =
2463       mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
2464   using ChildEdgeIteratorType = TinyPtrVector<DepTy>::iterator;
2465 
2466   static ChildIteratorType child_begin(NodeRef N) { return N->child_begin(); }
2467 
2468   static ChildIteratorType child_end(NodeRef N) { return N->child_end(); }
2469 };
2470 
2471 template <>
2472 struct GraphTraits<AADepGraph *> : public GraphTraits<AADepGraphNode *> {
2473   static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); }
2474 
2475   using nodes_iterator =
2476       mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
2477 
2478   static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); }
2479 
2480   static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); }
2481 };
2482 
2483 template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits {
2484   DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
2485 
2486   static std::string getNodeLabel(const AADepGraphNode *Node,
2487                                   const AADepGraph *DG) {
2488     std::string AAString;
2489     raw_string_ostream O(AAString);
2490     Node->print(O);
2491     return AAString;
2492   }
2493 };
2494 
2495 } // end namespace llvm
2496 
2497 namespace {
2498 
2499 struct AttributorLegacyPass : public ModulePass {
2500   static char ID;
2501 
2502   AttributorLegacyPass() : ModulePass(ID) {
2503     initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
2504   }
2505 
2506   bool runOnModule(Module &M) override {
2507     if (skipModule(M))
2508       return false;
2509 
2510     AnalysisGetter AG;
2511     SetVector<Function *> Functions;
2512     for (Function &F : M)
2513       Functions.insert(&F);
2514 
2515     CallGraphUpdater CGUpdater;
2516     BumpPtrAllocator Allocator;
2517     InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
2518     return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
2519                                     /* DeleteFns*/ true);
2520   }
2521 
2522   void getAnalysisUsage(AnalysisUsage &AU) const override {
2523     // FIXME: Think about passes we will preserve and add them here.
2524     AU.addRequired<TargetLibraryInfoWrapperPass>();
2525   }
2526 };
2527 
2528 struct AttributorCGSCCLegacyPass : public CallGraphSCCPass {
2529   static char ID;
2530 
2531   AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) {
2532     initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
2533   }
2534 
2535   bool runOnSCC(CallGraphSCC &SCC) override {
2536     if (skipSCC(SCC))
2537       return false;
2538 
2539     SetVector<Function *> Functions;
2540     for (CallGraphNode *CGN : SCC)
2541       if (Function *Fn = CGN->getFunction())
2542         if (!Fn->isDeclaration())
2543           Functions.insert(Fn);
2544 
2545     if (Functions.empty())
2546       return false;
2547 
2548     AnalysisGetter AG;
2549     CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph());
2550     CallGraphUpdater CGUpdater;
2551     CGUpdater.initialize(CG, SCC);
2552     Module &M = *Functions.back()->getParent();
2553     BumpPtrAllocator Allocator;
2554     InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
2555     return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
2556                                     /* DeleteFns */ false);
2557   }
2558 
2559   void getAnalysisUsage(AnalysisUsage &AU) const override {
2560     // FIXME: Think about passes we will preserve and add them here.
2561     AU.addRequired<TargetLibraryInfoWrapperPass>();
2562     CallGraphSCCPass::getAnalysisUsage(AU);
2563   }
2564 };
2565 
2566 } // end anonymous namespace
2567 
2568 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
2569 Pass *llvm::createAttributorCGSCCLegacyPass() {
2570   return new AttributorCGSCCLegacyPass();
2571 }
2572 
2573 char AttributorLegacyPass::ID = 0;
2574 char AttributorCGSCCLegacyPass::ID = 0;
2575 
2576 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
2577                       "Deduce and propagate attributes", false, false)
2578 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2579 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
2580                     "Deduce and propagate attributes", false, false)
2581 INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc",
2582                       "Deduce and propagate attributes (CGSCC pass)", false,
2583                       false)
2584 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2585 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
2586 INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc",
2587                     "Deduce and propagate attributes (CGSCC pass)", false,
2588                     false)
2589