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