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