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