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