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