1 //===- Attributor.cpp - Module-wide attribute deduction -------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/IPO/Attributor.h"
18 #include "llvm/ADT/GraphTraits.h"
19 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/TinyPtrVector.h"
23 #include "llvm/Analysis/InlineCost.h"
24 #include "llvm/Analysis/LazyValueInfo.h"
25 #include "llvm/Analysis/MemorySSAUpdater.h"
26 #include "llvm/Analysis/MustExecute.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/Constant.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/GlobalValue.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/IRBuilder.h"
34 #include "llvm/IR/Instruction.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/IR/IntrinsicInst.h"
37 #include "llvm/IR/NoFolder.h"
38 #include "llvm/IR/ValueHandle.h"
39 #include "llvm/IR/Verifier.h"
40 #include "llvm/InitializePasses.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/DebugCounter.h"
45 #include "llvm/Support/FileSystem.h"
46 #include "llvm/Support/GraphWriter.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
49 #include "llvm/Transforms/Utils/Cloning.h"
50 #include "llvm/Transforms/Utils/Local.h"
57 #define DEBUG_TYPE "attributor"
59 DEBUG_COUNTER(ManifestDBGCounter
, "attributor-manifest",
60 "Determine what attributes are manifested in the IR");
62 STATISTIC(NumFnDeleted
, "Number of function deleted");
63 STATISTIC(NumFnWithExactDefinition
,
64 "Number of functions with exact definitions");
65 STATISTIC(NumFnWithoutExactDefinition
,
66 "Number of functions without exact definitions");
67 STATISTIC(NumFnShallowWrappersCreated
, "Number of shallow wrappers created");
68 STATISTIC(NumAttributesTimedOut
,
69 "Number of abstract attributes timed out before fixpoint");
70 STATISTIC(NumAttributesValidFixpoint
,
71 "Number of abstract attributes in a valid fixpoint state");
72 STATISTIC(NumAttributesManifested
,
73 "Number of abstract attributes manifested in IR");
75 // TODO: Determine a good default value.
77 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
78 // (when run with the first 5 abstract attributes). The results also indicate
79 // that we never reach 32 iterations but always find a fixpoint sooner.
81 // This will become more evolved once we perform two interleaved fixpoint
82 // iterations: bottom-up and top-down.
83 static cl::opt
<unsigned>
84 SetFixpointIterations("attributor-max-iterations", cl::Hidden
,
85 cl::desc("Maximal number of fixpoint iterations."),
88 static cl::opt
<unsigned, true> MaxInitializationChainLengthX(
89 "attributor-max-initialization-chain-length", cl::Hidden
,
91 "Maximal number of chained initializations (to avoid stack overflows)"),
92 cl::location(MaxInitializationChainLength
), cl::init(1024));
93 unsigned llvm::MaxInitializationChainLength
;
95 static cl::opt
<bool> VerifyMaxFixpointIterations(
96 "attributor-max-iterations-verify", cl::Hidden
,
97 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
100 static cl::opt
<bool> AnnotateDeclarationCallSites(
101 "attributor-annotate-decl-cs", cl::Hidden
,
102 cl::desc("Annotate call sites of function declarations."), cl::init(false));
104 static cl::opt
<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
105 cl::init(true), cl::Hidden
);
108 AllowShallowWrappers("attributor-allow-shallow-wrappers", cl::Hidden
,
109 cl::desc("Allow the Attributor to create shallow "
110 "wrappers for non-exact definitions."),
114 AllowDeepWrapper("attributor-allow-deep-wrappers", cl::Hidden
,
115 cl::desc("Allow the Attributor to use IP information "
116 "derived from non-exact functions via cloning"),
119 // These options can only used for debug builds.
121 static cl::list
<std::string
>
122 SeedAllowList("attributor-seed-allow-list", cl::Hidden
,
123 cl::desc("Comma seperated list of attribute names that are "
124 "allowed to be seeded."),
125 cl::ZeroOrMore
, cl::CommaSeparated
);
127 static cl::list
<std::string
> FunctionSeedAllowList(
128 "attributor-function-seed-allow-list", cl::Hidden
,
129 cl::desc("Comma seperated list of function names that are "
130 "allowed to be seeded."),
131 cl::ZeroOrMore
, cl::CommaSeparated
);
135 DumpDepGraph("attributor-dump-dep-graph", cl::Hidden
,
136 cl::desc("Dump the dependency graph to dot files."),
139 static cl::opt
<std::string
> DepGraphDotFileNamePrefix(
140 "attributor-depgraph-dot-filename-prefix", cl::Hidden
,
141 cl::desc("The prefix used for the CallGraph dot file names."));
143 static cl::opt
<bool> ViewDepGraph("attributor-view-dep-graph", cl::Hidden
,
144 cl::desc("View the dependency graph."),
147 static cl::opt
<bool> PrintDependencies("attributor-print-dep", cl::Hidden
,
148 cl::desc("Print attribute dependencies"),
151 static cl::opt
<bool> EnableCallSiteSpecific(
152 "attributor-enable-call-site-specific-deduction", cl::Hidden
,
153 cl::desc("Allow the Attributor to do call site specific analysis"),
157 PrintCallGraph("attributor-print-call-graph", cl::Hidden
,
158 cl::desc("Print Attributor's internal call graph"),
161 static cl::opt
<bool> SimplifyAllLoads("attributor-simplify-all-loads",
163 cl::desc("Try to simplify all loads."),
166 /// Logic operators for the change status enum class.
169 ChangeStatus
llvm::operator|(ChangeStatus L
, ChangeStatus R
) {
170 return L
== ChangeStatus::CHANGED
? L
: R
;
172 ChangeStatus
&llvm::operator|=(ChangeStatus
&L
, ChangeStatus R
) {
176 ChangeStatus
llvm::operator&(ChangeStatus L
, ChangeStatus R
) {
177 return L
== ChangeStatus::UNCHANGED
? L
: R
;
179 ChangeStatus
&llvm::operator&=(ChangeStatus
&L
, ChangeStatus R
) {
185 bool AA::isDynamicallyUnique(Attributor
&A
, const AbstractAttribute
&QueryingAA
,
187 if (auto *C
= dyn_cast
<Constant
>(&V
))
188 return !C
->isThreadDependent();
189 // TODO: Inspect and cache more complex instructions.
190 if (auto *CB
= dyn_cast
<CallBase
>(&V
))
191 return CB
->getNumOperands() == 0 && !CB
->mayHaveSideEffects() &&
192 !CB
->mayReadFromMemory();
193 const Function
*Scope
= nullptr;
194 if (auto *I
= dyn_cast
<Instruction
>(&V
))
195 Scope
= I
->getFunction();
196 if (auto *A
= dyn_cast
<Argument
>(&V
))
197 Scope
= A
->getParent();
200 auto &NoRecurseAA
= A
.getAAFor
<AANoRecurse
>(
201 QueryingAA
, IRPosition::function(*Scope
), DepClassTy::OPTIONAL
);
202 return NoRecurseAA
.isAssumedNoRecurse();
205 Constant
*AA::getInitialValueForObj(Value
&Obj
, Type
&Ty
) {
206 if (isa
<AllocaInst
>(Obj
))
207 return UndefValue::get(&Ty
);
208 auto *GV
= dyn_cast
<GlobalVariable
>(&Obj
);
209 if (!GV
|| !GV
->hasLocalLinkage())
211 if (!GV
->hasInitializer())
212 return UndefValue::get(&Ty
);
213 return dyn_cast_or_null
<Constant
>(getWithType(*GV
->getInitializer(), Ty
));
216 bool AA::isValidInScope(const Value
&V
, const Function
*Scope
) {
217 if (isa
<Constant
>(V
))
219 if (auto *I
= dyn_cast
<Instruction
>(&V
))
220 return I
->getFunction() == Scope
;
221 if (auto *A
= dyn_cast
<Argument
>(&V
))
222 return A
->getParent() == Scope
;
226 bool AA::isValidAtPosition(const Value
&V
, const Instruction
&CtxI
,
227 InformationCache
&InfoCache
) {
228 if (isa
<Constant
>(V
))
230 const Function
*Scope
= CtxI
.getFunction();
231 if (auto *A
= dyn_cast
<Argument
>(&V
))
232 return A
->getParent() == Scope
;
233 if (auto *I
= dyn_cast
<Instruction
>(&V
))
234 if (I
->getFunction() == Scope
) {
235 const DominatorTree
*DT
=
236 InfoCache
.getAnalysisResultForFunction
<DominatorTreeAnalysis
>(*Scope
);
237 return DT
&& DT
->dominates(I
, &CtxI
);
242 Value
*AA::getWithType(Value
&V
, Type
&Ty
) {
243 if (V
.getType() == &Ty
)
245 if (isa
<PoisonValue
>(V
))
246 return PoisonValue::get(&Ty
);
247 if (isa
<UndefValue
>(V
))
248 return UndefValue::get(&Ty
);
249 if (auto *C
= dyn_cast
<Constant
>(&V
)) {
250 if (C
->isNullValue())
251 return Constant::getNullValue(&Ty
);
252 if (C
->getType()->isPointerTy() && Ty
.isPointerTy())
253 return ConstantExpr::getPointerCast(C
, &Ty
);
254 if (C
->getType()->getPrimitiveSizeInBits() >= Ty
.getPrimitiveSizeInBits()) {
255 if (C
->getType()->isIntegerTy() && Ty
.isIntegerTy())
256 return ConstantExpr::getTrunc(C
, &Ty
, /* OnlyIfReduced */ true);
257 if (C
->getType()->isFloatingPointTy() && Ty
.isFloatingPointTy())
258 return ConstantExpr::getFPTrunc(C
, &Ty
, /* OnlyIfReduced */ true);
265 AA::combineOptionalValuesInAAValueLatice(const Optional
<Value
*> &A
,
266 const Optional
<Value
*> &B
, Type
*Ty
) {
274 return Ty
? getWithType(**B
, *Ty
) : nullptr;
278 Ty
= (*A
)->getType();
279 if (isa_and_nonnull
<UndefValue
>(*A
))
280 return getWithType(**B
, *Ty
);
281 if (isa
<UndefValue
>(*B
))
283 if (*A
&& *B
&& *A
== getWithType(**B
, *Ty
))
288 bool AA::getPotentialCopiesOfStoredValue(
289 Attributor
&A
, StoreInst
&SI
, SmallSetVector
<Value
*, 4> &PotentialCopies
,
290 const AbstractAttribute
&QueryingAA
, bool &UsedAssumedInformation
) {
292 Value
&Ptr
= *SI
.getPointerOperand();
293 SmallVector
<Value
*, 8> Objects
;
294 if (!AA::getAssumedUnderlyingObjects(A
, Ptr
, Objects
, QueryingAA
, &SI
)) {
296 dbgs() << "Underlying objects stored into could not be determined\n";);
300 SmallVector
<const AAPointerInfo
*> PIs
;
301 SmallVector
<Value
*> NewCopies
;
303 for (Value
*Obj
: Objects
) {
304 LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj
<< "\n");
305 if (isa
<UndefValue
>(Obj
))
307 if (isa
<ConstantPointerNull
>(Obj
)) {
308 // A null pointer access can be undefined but any offset from null may
309 // be OK. We do not try to optimize the latter.
310 if (!NullPointerIsDefined(SI
.getFunction(),
311 Ptr
.getType()->getPointerAddressSpace()) &&
312 A
.getAssumedSimplified(Ptr
, QueryingAA
, UsedAssumedInformation
) ==
316 dbgs() << "Underlying object is a valid nullptr, giving up.\n";);
319 if (!isa
<AllocaInst
>(Obj
) && !isa
<GlobalVariable
>(Obj
)) {
320 LLVM_DEBUG(dbgs() << "Underlying object is not supported yet: " << *Obj
324 if (auto *GV
= dyn_cast
<GlobalVariable
>(Obj
))
325 if (!GV
->hasLocalLinkage()) {
326 LLVM_DEBUG(dbgs() << "Underlying object is global with external "
327 "linkage, not supported yet: "
332 auto CheckAccess
= [&](const AAPointerInfo::Access
&Acc
, bool IsExact
) {
335 auto *LI
= dyn_cast
<LoadInst
>(Acc
.getRemoteInst());
337 LLVM_DEBUG(dbgs() << "Underlying object read through a non-load "
338 "instruction not supported yet: "
339 << *Acc
.getRemoteInst() << "\n";);
342 NewCopies
.push_back(LI
);
346 auto &PI
= A
.getAAFor
<AAPointerInfo
>(QueryingAA
, IRPosition::value(*Obj
),
348 if (!PI
.forallInterferingAccesses(SI
, CheckAccess
)) {
351 << "Failed to verify all interfering accesses for underlying object: "
358 for (auto *PI
: PIs
) {
359 if (!PI
->getState().isAtFixpoint())
360 UsedAssumedInformation
= true;
361 A
.recordDependence(*PI
, QueryingAA
, DepClassTy::OPTIONAL
);
363 PotentialCopies
.insert(NewCopies
.begin(), NewCopies
.end());
368 /// Return true if \p New is equal or worse than \p Old.
369 static bool isEqualOrWorse(const Attribute
&New
, const Attribute
&Old
) {
370 if (!Old
.isIntAttribute())
373 return Old
.getValueAsInt() >= New
.getValueAsInt();
376 /// Return true if the information provided by \p Attr was added to the
377 /// attribute list \p Attrs. This is only the case if it was not already present
378 /// in \p Attrs at the position describe by \p PK and \p AttrIdx.
379 static bool addIfNotExistent(LLVMContext
&Ctx
, const Attribute
&Attr
,
380 AttributeList
&Attrs
, int AttrIdx
,
381 bool ForceReplace
= false) {
383 if (Attr
.isEnumAttribute()) {
384 Attribute::AttrKind Kind
= Attr
.getKindAsEnum();
385 if (Attrs
.hasAttribute(AttrIdx
, Kind
))
387 isEqualOrWorse(Attr
, Attrs
.getAttribute(AttrIdx
, Kind
)))
389 Attrs
= Attrs
.addAttribute(Ctx
, AttrIdx
, Attr
);
392 if (Attr
.isStringAttribute()) {
393 StringRef Kind
= Attr
.getKindAsString();
394 if (Attrs
.hasAttribute(AttrIdx
, Kind
))
396 isEqualOrWorse(Attr
, Attrs
.getAttribute(AttrIdx
, Kind
)))
398 Attrs
= Attrs
.addAttribute(Ctx
, AttrIdx
, Attr
);
401 if (Attr
.isIntAttribute()) {
402 Attribute::AttrKind Kind
= Attr
.getKindAsEnum();
403 if (Attrs
.hasAttribute(AttrIdx
, Kind
))
405 isEqualOrWorse(Attr
, Attrs
.getAttribute(AttrIdx
, Kind
)))
407 Attrs
= Attrs
.removeAttribute(Ctx
, AttrIdx
, Kind
);
408 Attrs
= Attrs
.addAttribute(Ctx
, AttrIdx
, Attr
);
412 llvm_unreachable("Expected enum or string attribute!");
415 Argument
*IRPosition::getAssociatedArgument() const {
416 if (getPositionKind() == IRP_ARGUMENT
)
417 return cast
<Argument
>(&getAnchorValue());
419 // Not an Argument and no argument number means this is not a call site
420 // argument, thus we cannot find a callback argument to return.
421 int ArgNo
= getCallSiteArgNo();
425 // Use abstract call sites to make the connection between the call site
426 // values and the ones in callbacks. If a callback was found that makes use
427 // of the underlying call site operand, we want the corresponding callback
428 // callee argument and not the direct callee argument.
429 Optional
<Argument
*> CBCandidateArg
;
430 SmallVector
<const Use
*, 4> CallbackUses
;
431 const auto &CB
= cast
<CallBase
>(getAnchorValue());
432 AbstractCallSite::getCallbackUses(CB
, CallbackUses
);
433 for (const Use
*U
: CallbackUses
) {
434 AbstractCallSite
ACS(U
);
435 assert(ACS
&& ACS
.isCallbackCall());
436 if (!ACS
.getCalledFunction())
439 for (unsigned u
= 0, e
= ACS
.getNumArgOperands(); u
< e
; u
++) {
441 // Test if the underlying call site operand is argument number u of the
443 if (ACS
.getCallArgOperandNo(u
) != ArgNo
)
446 assert(ACS
.getCalledFunction()->arg_size() > u
&&
447 "ACS mapped into var-args arguments!");
448 if (CBCandidateArg
.hasValue()) {
449 CBCandidateArg
= nullptr;
452 CBCandidateArg
= ACS
.getCalledFunction()->getArg(u
);
456 // If we found a unique callback candidate argument, return it.
457 if (CBCandidateArg
.hasValue() && CBCandidateArg
.getValue())
458 return CBCandidateArg
.getValue();
460 // If no callbacks were found, or none used the underlying call site operand
461 // exclusively, use the direct callee argument if available.
462 const Function
*Callee
= CB
.getCalledFunction();
463 if (Callee
&& Callee
->arg_size() > unsigned(ArgNo
))
464 return Callee
->getArg(ArgNo
);
469 ChangeStatus
AbstractAttribute::update(Attributor
&A
) {
470 ChangeStatus HasChanged
= ChangeStatus::UNCHANGED
;
471 if (getState().isAtFixpoint())
474 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
476 HasChanged
= updateImpl(A
);
478 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged
<< " " << *this
485 IRAttributeManifest::manifestAttrs(Attributor
&A
, const IRPosition
&IRP
,
486 const ArrayRef
<Attribute
> &DeducedAttrs
,
488 Function
*ScopeFn
= IRP
.getAnchorScope();
489 IRPosition::Kind PK
= IRP
.getPositionKind();
491 // In the following some generic code that will manifest attributes in
492 // DeducedAttrs if they improve the current IR. Due to the different
493 // annotation positions we use the underlying AttributeList interface.
497 case IRPosition::IRP_INVALID
:
498 case IRPosition::IRP_FLOAT
:
499 return ChangeStatus::UNCHANGED
;
500 case IRPosition::IRP_ARGUMENT
:
501 case IRPosition::IRP_FUNCTION
:
502 case IRPosition::IRP_RETURNED
:
503 Attrs
= ScopeFn
->getAttributes();
505 case IRPosition::IRP_CALL_SITE
:
506 case IRPosition::IRP_CALL_SITE_RETURNED
:
507 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
508 Attrs
= cast
<CallBase
>(IRP
.getAnchorValue()).getAttributes();
512 ChangeStatus HasChanged
= ChangeStatus::UNCHANGED
;
513 LLVMContext
&Ctx
= IRP
.getAnchorValue().getContext();
514 for (const Attribute
&Attr
: DeducedAttrs
) {
515 if (!addIfNotExistent(Ctx
, Attr
, Attrs
, IRP
.getAttrIdx(), ForceReplace
))
518 HasChanged
= ChangeStatus::CHANGED
;
521 if (HasChanged
== ChangeStatus::UNCHANGED
)
525 case IRPosition::IRP_ARGUMENT
:
526 case IRPosition::IRP_FUNCTION
:
527 case IRPosition::IRP_RETURNED
:
528 ScopeFn
->setAttributes(Attrs
);
530 case IRPosition::IRP_CALL_SITE
:
531 case IRPosition::IRP_CALL_SITE_RETURNED
:
532 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
533 cast
<CallBase
>(IRP
.getAnchorValue()).setAttributes(Attrs
);
535 case IRPosition::IRP_INVALID
:
536 case IRPosition::IRP_FLOAT
:
543 const IRPosition
IRPosition::EmptyKey(DenseMapInfo
<void *>::getEmptyKey());
545 IRPosition::TombstoneKey(DenseMapInfo
<void *>::getTombstoneKey());
547 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition
&IRP
) {
548 IRPositions
.emplace_back(IRP
);
550 // Helper to determine if operand bundles on a call site are benin or
551 // potentially problematic. We handle only llvm.assume for now.
552 auto CanIgnoreOperandBundles
= [](const CallBase
&CB
) {
553 return (isa
<IntrinsicInst
>(CB
) &&
554 cast
<IntrinsicInst
>(CB
).getIntrinsicID() == Intrinsic ::assume
);
557 const auto *CB
= dyn_cast
<CallBase
>(&IRP
.getAnchorValue());
558 switch (IRP
.getPositionKind()) {
559 case IRPosition::IRP_INVALID
:
560 case IRPosition::IRP_FLOAT
:
561 case IRPosition::IRP_FUNCTION
:
563 case IRPosition::IRP_ARGUMENT
:
564 case IRPosition::IRP_RETURNED
:
565 IRPositions
.emplace_back(IRPosition::function(*IRP
.getAnchorScope()));
567 case IRPosition::IRP_CALL_SITE
:
568 assert(CB
&& "Expected call site!");
569 // TODO: We need to look at the operand bundles similar to the redirection
571 if (!CB
->hasOperandBundles() || CanIgnoreOperandBundles(*CB
))
572 if (const Function
*Callee
= CB
->getCalledFunction())
573 IRPositions
.emplace_back(IRPosition::function(*Callee
));
575 case IRPosition::IRP_CALL_SITE_RETURNED
:
576 assert(CB
&& "Expected call site!");
577 // TODO: We need to look at the operand bundles similar to the redirection
579 if (!CB
->hasOperandBundles() || CanIgnoreOperandBundles(*CB
)) {
580 if (const Function
*Callee
= CB
->getCalledFunction()) {
581 IRPositions
.emplace_back(IRPosition::returned(*Callee
));
582 IRPositions
.emplace_back(IRPosition::function(*Callee
));
583 for (const Argument
&Arg
: Callee
->args())
584 if (Arg
.hasReturnedAttr()) {
585 IRPositions
.emplace_back(
586 IRPosition::callsite_argument(*CB
, Arg
.getArgNo()));
587 IRPositions
.emplace_back(
588 IRPosition::value(*CB
->getArgOperand(Arg
.getArgNo())));
589 IRPositions
.emplace_back(IRPosition::argument(Arg
));
593 IRPositions
.emplace_back(IRPosition::callsite_function(*CB
));
595 case IRPosition::IRP_CALL_SITE_ARGUMENT
: {
596 assert(CB
&& "Expected call site!");
597 // TODO: We need to look at the operand bundles similar to the redirection
599 if (!CB
->hasOperandBundles() || CanIgnoreOperandBundles(*CB
)) {
600 const Function
*Callee
= CB
->getCalledFunction();
602 if (Argument
*Arg
= IRP
.getAssociatedArgument())
603 IRPositions
.emplace_back(IRPosition::argument(*Arg
));
604 IRPositions
.emplace_back(IRPosition::function(*Callee
));
607 IRPositions
.emplace_back(IRPosition::value(IRP
.getAssociatedValue()));
613 bool IRPosition::hasAttr(ArrayRef
<Attribute::AttrKind
> AKs
,
614 bool IgnoreSubsumingPositions
, Attributor
*A
) const {
615 SmallVector
<Attribute
, 4> Attrs
;
616 for (const IRPosition
&EquivIRP
: SubsumingPositionIterator(*this)) {
617 for (Attribute::AttrKind AK
: AKs
)
618 if (EquivIRP
.getAttrsFromIRAttr(AK
, Attrs
))
620 // The first position returned by the SubsumingPositionIterator is
621 // always the position itself. If we ignore subsuming positions we
622 // are done after the first iteration.
623 if (IgnoreSubsumingPositions
)
627 for (Attribute::AttrKind AK
: AKs
)
628 if (getAttrsFromAssumes(AK
, Attrs
, *A
))
633 void IRPosition::getAttrs(ArrayRef
<Attribute::AttrKind
> AKs
,
634 SmallVectorImpl
<Attribute
> &Attrs
,
635 bool IgnoreSubsumingPositions
, Attributor
*A
) const {
636 for (const IRPosition
&EquivIRP
: SubsumingPositionIterator(*this)) {
637 for (Attribute::AttrKind AK
: AKs
)
638 EquivIRP
.getAttrsFromIRAttr(AK
, Attrs
);
639 // The first position returned by the SubsumingPositionIterator is
640 // always the position itself. If we ignore subsuming positions we
641 // are done after the first iteration.
642 if (IgnoreSubsumingPositions
)
646 for (Attribute::AttrKind AK
: AKs
)
647 getAttrsFromAssumes(AK
, Attrs
, *A
);
650 bool IRPosition::getAttrsFromIRAttr(Attribute::AttrKind AK
,
651 SmallVectorImpl
<Attribute
> &Attrs
) const {
652 if (getPositionKind() == IRP_INVALID
|| getPositionKind() == IRP_FLOAT
)
655 AttributeList AttrList
;
656 if (const auto *CB
= dyn_cast
<CallBase
>(&getAnchorValue()))
657 AttrList
= CB
->getAttributes();
659 AttrList
= getAssociatedFunction()->getAttributes();
661 bool HasAttr
= AttrList
.hasAttribute(getAttrIdx(), AK
);
663 Attrs
.push_back(AttrList
.getAttribute(getAttrIdx(), AK
));
667 bool IRPosition::getAttrsFromAssumes(Attribute::AttrKind AK
,
668 SmallVectorImpl
<Attribute
> &Attrs
,
669 Attributor
&A
) const {
670 assert(getPositionKind() != IRP_INVALID
&& "Did expect a valid position!");
671 Value
&AssociatedValue
= getAssociatedValue();
673 const Assume2KnowledgeMap
&A2K
=
674 A
.getInfoCache().getKnowledgeMap().lookup({&AssociatedValue
, AK
});
676 // Check if we found any potential assume use, if not we don't need to create
677 // explorer iterators.
681 LLVMContext
&Ctx
= AssociatedValue
.getContext();
682 unsigned AttrsSize
= Attrs
.size();
683 MustBeExecutedContextExplorer
&Explorer
=
684 A
.getInfoCache().getMustBeExecutedContextExplorer();
685 auto EIt
= Explorer
.begin(getCtxI()), EEnd
= Explorer
.end(getCtxI());
687 if (Explorer
.findInContextOf(It
.first
, EIt
, EEnd
))
688 Attrs
.push_back(Attribute::get(Ctx
, AK
, It
.second
.Max
));
689 return AttrsSize
!= Attrs
.size();
692 void IRPosition::verify() {
693 #ifdef EXPENSIVE_CHECKS
694 switch (getPositionKind()) {
696 assert((CBContext
== nullptr) &&
697 "Invalid position must not have CallBaseContext!");
698 assert(!Enc
.getOpaqueValue() &&
699 "Expected a nullptr for an invalid position!");
702 assert((!isa
<CallBase
>(&getAssociatedValue()) &&
703 !isa
<Argument
>(&getAssociatedValue())) &&
704 "Expected specialized kind for call base and argument values!");
707 assert(isa
<Function
>(getAsValuePtr()) &&
708 "Expected function for a 'returned' position!");
709 assert(getAsValuePtr() == &getAssociatedValue() &&
710 "Associated value mismatch!");
712 case IRP_CALL_SITE_RETURNED
:
713 assert((CBContext
== nullptr) &&
714 "'call site returned' position must not have CallBaseContext!");
715 assert((isa
<CallBase
>(getAsValuePtr())) &&
716 "Expected call base for 'call site returned' position!");
717 assert(getAsValuePtr() == &getAssociatedValue() &&
718 "Associated value mismatch!");
721 assert((CBContext
== nullptr) &&
722 "'call site function' position must not have CallBaseContext!");
723 assert((isa
<CallBase
>(getAsValuePtr())) &&
724 "Expected call base for 'call site function' position!");
725 assert(getAsValuePtr() == &getAssociatedValue() &&
726 "Associated value mismatch!");
729 assert(isa
<Function
>(getAsValuePtr()) &&
730 "Expected function for a 'function' position!");
731 assert(getAsValuePtr() == &getAssociatedValue() &&
732 "Associated value mismatch!");
735 assert(isa
<Argument
>(getAsValuePtr()) &&
736 "Expected argument for a 'argument' position!");
737 assert(getAsValuePtr() == &getAssociatedValue() &&
738 "Associated value mismatch!");
740 case IRP_CALL_SITE_ARGUMENT
: {
741 assert((CBContext
== nullptr) &&
742 "'call site argument' position must not have CallBaseContext!");
743 Use
*U
= getAsUsePtr();
744 assert(U
&& "Expected use for a 'call site argument' position!");
745 assert(isa
<CallBase
>(U
->getUser()) &&
746 "Expected call base user for a 'call site argument' position!");
747 assert(cast
<CallBase
>(U
->getUser())->isArgOperand(U
) &&
748 "Expected call base argument operand for a 'call site argument' "
750 assert(cast
<CallBase
>(U
->getUser())->getArgOperandNo(U
) ==
751 unsigned(getCallSiteArgNo()) &&
752 "Argument number mismatch!");
753 assert(U
->get() == &getAssociatedValue() && "Associated value mismatch!");
761 Attributor::getAssumedConstant(const IRPosition
&IRP
,
762 const AbstractAttribute
&AA
,
763 bool &UsedAssumedInformation
) {
764 // First check all callbacks provided by outside AAs. If any of them returns
765 // a non-null value that is different from the associated value, or None, we
766 // assume it's simpliied.
767 for (auto &CB
: SimplificationCallbacks
.lookup(IRP
)) {
768 Optional
<Value
*> SimplifiedV
= CB(IRP
, &AA
, UsedAssumedInformation
);
769 if (!SimplifiedV
.hasValue())
771 if (isa_and_nonnull
<Constant
>(*SimplifiedV
))
772 return cast
<Constant
>(*SimplifiedV
);
775 const auto &ValueSimplifyAA
=
776 getAAFor
<AAValueSimplify
>(AA
, IRP
, DepClassTy::NONE
);
777 Optional
<Value
*> SimplifiedV
=
778 ValueSimplifyAA
.getAssumedSimplifiedValue(*this);
779 bool IsKnown
= ValueSimplifyAA
.isAtFixpoint();
780 UsedAssumedInformation
|= !IsKnown
;
781 if (!SimplifiedV
.hasValue()) {
782 recordDependence(ValueSimplifyAA
, AA
, DepClassTy::OPTIONAL
);
785 if (isa_and_nonnull
<UndefValue
>(SimplifiedV
.getValue())) {
786 recordDependence(ValueSimplifyAA
, AA
, DepClassTy::OPTIONAL
);
787 return UndefValue::get(IRP
.getAssociatedType());
789 Constant
*CI
= dyn_cast_or_null
<Constant
>(SimplifiedV
.getValue());
791 CI
= dyn_cast_or_null
<Constant
>(
792 AA::getWithType(*CI
, *IRP
.getAssociatedType()));
794 recordDependence(ValueSimplifyAA
, AA
, DepClassTy::OPTIONAL
);
799 Attributor::getAssumedSimplified(const IRPosition
&IRP
,
800 const AbstractAttribute
*AA
,
801 bool &UsedAssumedInformation
) {
802 // First check all callbacks provided by outside AAs. If any of them returns
803 // a non-null value that is different from the associated value, or None, we
804 // assume it's simpliied.
805 for (auto &CB
: SimplificationCallbacks
.lookup(IRP
))
806 return CB(IRP
, AA
, UsedAssumedInformation
);
808 // If no high-level/outside simplification occured, use AAValueSimplify.
809 const auto &ValueSimplifyAA
=
810 getOrCreateAAFor
<AAValueSimplify
>(IRP
, AA
, DepClassTy::NONE
);
811 Optional
<Value
*> SimplifiedV
=
812 ValueSimplifyAA
.getAssumedSimplifiedValue(*this);
813 bool IsKnown
= ValueSimplifyAA
.isAtFixpoint();
814 UsedAssumedInformation
|= !IsKnown
;
815 if (!SimplifiedV
.hasValue()) {
817 recordDependence(ValueSimplifyAA
, *AA
, DepClassTy::OPTIONAL
);
820 if (*SimplifiedV
== nullptr)
821 return const_cast<Value
*>(&IRP
.getAssociatedValue());
823 AA::getWithType(**SimplifiedV
, *IRP
.getAssociatedType())) {
825 recordDependence(ValueSimplifyAA
, *AA
, DepClassTy::OPTIONAL
);
828 return const_cast<Value
*>(&IRP
.getAssociatedValue());
831 Optional
<Value
*> Attributor::translateArgumentToCallSiteContent(
832 Optional
<Value
*> V
, CallBase
&CB
, const AbstractAttribute
&AA
,
833 bool &UsedAssumedInformation
) {
836 if (*V
== nullptr || isa
<Constant
>(*V
))
838 if (auto *Arg
= dyn_cast
<Argument
>(*V
))
839 if (CB
.getCalledFunction() == Arg
->getParent())
840 if (!Arg
->hasPointeeInMemoryValueAttr())
841 return getAssumedSimplified(
842 IRPosition::callsite_argument(CB
, Arg
->getArgNo()), AA
,
843 UsedAssumedInformation
);
847 Attributor::~Attributor() {
848 // The abstract attributes are allocated via the BumpPtrAllocator Allocator,
849 // thus we cannot delete them. We can, and want to, destruct them though.
850 for (auto &DepAA
: DG
.SyntheticRoot
.Deps
) {
851 AbstractAttribute
*AA
= cast
<AbstractAttribute
>(DepAA
.getPointer());
852 AA
->~AbstractAttribute();
856 bool Attributor::isAssumedDead(const AbstractAttribute
&AA
,
857 const AAIsDead
*FnLivenessAA
,
858 bool &UsedAssumedInformation
,
859 bool CheckBBLivenessOnly
, DepClassTy DepClass
) {
860 const IRPosition
&IRP
= AA
.getIRPosition();
861 if (!Functions
.count(IRP
.getAnchorScope()))
863 return isAssumedDead(IRP
, &AA
, FnLivenessAA
, UsedAssumedInformation
,
864 CheckBBLivenessOnly
, DepClass
);
867 bool Attributor::isAssumedDead(const Use
&U
,
868 const AbstractAttribute
*QueryingAA
,
869 const AAIsDead
*FnLivenessAA
,
870 bool &UsedAssumedInformation
,
871 bool CheckBBLivenessOnly
, DepClassTy DepClass
) {
872 Instruction
*UserI
= dyn_cast
<Instruction
>(U
.getUser());
874 return isAssumedDead(IRPosition::value(*U
.get()), QueryingAA
, FnLivenessAA
,
875 UsedAssumedInformation
, CheckBBLivenessOnly
, DepClass
);
877 if (auto *CB
= dyn_cast
<CallBase
>(UserI
)) {
878 // For call site argument uses we can check if the argument is
880 if (CB
->isArgOperand(&U
)) {
881 const IRPosition
&CSArgPos
=
882 IRPosition::callsite_argument(*CB
, CB
->getArgOperandNo(&U
));
883 return isAssumedDead(CSArgPos
, QueryingAA
, FnLivenessAA
,
884 UsedAssumedInformation
, CheckBBLivenessOnly
,
887 } else if (ReturnInst
*RI
= dyn_cast
<ReturnInst
>(UserI
)) {
888 const IRPosition
&RetPos
= IRPosition::returned(*RI
->getFunction());
889 return isAssumedDead(RetPos
, QueryingAA
, FnLivenessAA
,
890 UsedAssumedInformation
, CheckBBLivenessOnly
, DepClass
);
891 } else if (PHINode
*PHI
= dyn_cast
<PHINode
>(UserI
)) {
892 BasicBlock
*IncomingBB
= PHI
->getIncomingBlock(U
);
893 return isAssumedDead(*IncomingBB
->getTerminator(), QueryingAA
, FnLivenessAA
,
894 UsedAssumedInformation
, CheckBBLivenessOnly
, DepClass
);
897 return isAssumedDead(IRPosition::value(*UserI
), QueryingAA
, FnLivenessAA
,
898 UsedAssumedInformation
, CheckBBLivenessOnly
, DepClass
);
901 bool Attributor::isAssumedDead(const Instruction
&I
,
902 const AbstractAttribute
*QueryingAA
,
903 const AAIsDead
*FnLivenessAA
,
904 bool &UsedAssumedInformation
,
905 bool CheckBBLivenessOnly
, DepClassTy DepClass
) {
906 const IRPosition::CallBaseContext
*CBCtx
=
907 QueryingAA
? QueryingAA
->getCallBaseContext() : nullptr;
909 if (ManifestAddedBlocks
.contains(I
.getParent()))
914 lookupAAFor
<AAIsDead
>(IRPosition::function(*I
.getFunction(), CBCtx
),
915 QueryingAA
, DepClassTy::NONE
);
917 // If we have a context instruction and a liveness AA we use it.
919 FnLivenessAA
->getIRPosition().getAnchorScope() == I
.getFunction() &&
920 FnLivenessAA
->isAssumedDead(&I
)) {
922 recordDependence(*FnLivenessAA
, *QueryingAA
, DepClass
);
923 if (!FnLivenessAA
->isKnownDead(&I
))
924 UsedAssumedInformation
= true;
928 if (CheckBBLivenessOnly
)
931 const AAIsDead
&IsDeadAA
= getOrCreateAAFor
<AAIsDead
>(
932 IRPosition::value(I
, CBCtx
), QueryingAA
, DepClassTy::NONE
);
933 // Don't check liveness for AAIsDead.
934 if (QueryingAA
== &IsDeadAA
)
937 if (IsDeadAA
.isAssumedDead()) {
939 recordDependence(IsDeadAA
, *QueryingAA
, DepClass
);
940 if (!IsDeadAA
.isKnownDead())
941 UsedAssumedInformation
= true;
948 bool Attributor::isAssumedDead(const IRPosition
&IRP
,
949 const AbstractAttribute
*QueryingAA
,
950 const AAIsDead
*FnLivenessAA
,
951 bool &UsedAssumedInformation
,
952 bool CheckBBLivenessOnly
, DepClassTy DepClass
) {
953 Instruction
*CtxI
= IRP
.getCtxI();
955 isAssumedDead(*CtxI
, QueryingAA
, FnLivenessAA
, UsedAssumedInformation
,
956 /* CheckBBLivenessOnly */ true,
957 CheckBBLivenessOnly
? DepClass
: DepClassTy::OPTIONAL
))
960 if (CheckBBLivenessOnly
)
963 // If we haven't succeeded we query the specific liveness info for the IRP.
964 const AAIsDead
*IsDeadAA
;
965 if (IRP
.getPositionKind() == IRPosition::IRP_CALL_SITE
)
966 IsDeadAA
= &getOrCreateAAFor
<AAIsDead
>(
967 IRPosition::callsite_returned(cast
<CallBase
>(IRP
.getAssociatedValue())),
968 QueryingAA
, DepClassTy::NONE
);
970 IsDeadAA
= &getOrCreateAAFor
<AAIsDead
>(IRP
, QueryingAA
, DepClassTy::NONE
);
971 // Don't check liveness for AAIsDead.
972 if (QueryingAA
== IsDeadAA
)
975 if (IsDeadAA
->isAssumedDead()) {
977 recordDependence(*IsDeadAA
, *QueryingAA
, DepClass
);
978 if (!IsDeadAA
->isKnownDead())
979 UsedAssumedInformation
= true;
986 bool Attributor::isAssumedDead(const BasicBlock
&BB
,
987 const AbstractAttribute
*QueryingAA
,
988 const AAIsDead
*FnLivenessAA
,
989 DepClassTy DepClass
) {
991 FnLivenessAA
= lookupAAFor
<AAIsDead
>(IRPosition::function(*BB
.getParent()),
992 QueryingAA
, DepClassTy::NONE
);
993 if (FnLivenessAA
->isAssumedDead(&BB
)) {
995 recordDependence(*FnLivenessAA
, *QueryingAA
, DepClass
);
1002 bool Attributor::checkForAllUses(function_ref
<bool(const Use
&, bool &)> Pred
,
1003 const AbstractAttribute
&QueryingAA
,
1004 const Value
&V
, bool CheckBBLivenessOnly
,
1005 DepClassTy LivenessDepClass
) {
1007 // Check the trivial case first as it catches void values.
1011 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
1012 SmallVector
<const Use
*, 16> Worklist
;
1013 SmallPtrSet
<const Use
*, 16> Visited
;
1015 for (const Use
&U
: V
.uses())
1016 Worklist
.push_back(&U
);
1018 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist
.size()
1019 << " initial uses to check\n");
1021 const Function
*ScopeFn
= IRP
.getAnchorScope();
1022 const auto *LivenessAA
=
1023 ScopeFn
? &getAAFor
<AAIsDead
>(QueryingAA
, IRPosition::function(*ScopeFn
),
1027 while (!Worklist
.empty()) {
1028 const Use
*U
= Worklist
.pop_back_val();
1029 if (isa
<PHINode
>(U
->getUser()) && !Visited
.insert(U
).second
)
1031 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U
<< " in "
1032 << *U
->getUser() << "\n");
1033 bool UsedAssumedInformation
= false;
1034 if (isAssumedDead(*U
, &QueryingAA
, LivenessAA
, UsedAssumedInformation
,
1035 CheckBBLivenessOnly
, LivenessDepClass
)) {
1036 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
1039 if (U
->getUser()->isDroppable()) {
1040 LLVM_DEBUG(dbgs() << "[Attributor] Droppable user, skip!\n");
1044 if (auto *SI
= dyn_cast
<StoreInst
>(U
->getUser())) {
1045 if (&SI
->getOperandUse(0) == U
) {
1046 SmallSetVector
<Value
*, 4> PotentialCopies
;
1047 if (AA::getPotentialCopiesOfStoredValue(*this, *SI
, PotentialCopies
,
1049 UsedAssumedInformation
)) {
1050 LLVM_DEBUG(dbgs() << "[Attributor] Value is stored, continue with "
1051 << PotentialCopies
.size()
1052 << " potential copies instead!\n");
1053 for (Value
*PotentialCopy
: PotentialCopies
)
1054 for (const Use
&U
: PotentialCopy
->uses())
1055 Worklist
.push_back(&U
);
1061 bool Follow
= false;
1062 if (!Pred(*U
, Follow
))
1066 for (const Use
&UU
: U
->getUser()->uses())
1067 Worklist
.push_back(&UU
);
1073 bool Attributor::checkForAllCallSites(function_ref
<bool(AbstractCallSite
)> Pred
,
1074 const AbstractAttribute
&QueryingAA
,
1075 bool RequireAllCallSites
,
1076 bool &AllCallSitesKnown
) {
1077 // We can try to determine information from
1078 // the call sites. However, this is only possible all call sites are known,
1079 // hence the function has internal linkage.
1080 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
1081 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
1082 if (!AssociatedFunction
) {
1083 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
1085 AllCallSitesKnown
= false;
1089 return checkForAllCallSites(Pred
, *AssociatedFunction
, RequireAllCallSites
,
1090 &QueryingAA
, AllCallSitesKnown
);
1093 bool Attributor::checkForAllCallSites(function_ref
<bool(AbstractCallSite
)> Pred
,
1095 bool RequireAllCallSites
,
1096 const AbstractAttribute
*QueryingAA
,
1097 bool &AllCallSitesKnown
) {
1098 if (RequireAllCallSites
&& !Fn
.hasLocalLinkage()) {
1101 << "[Attributor] Function " << Fn
.getName()
1102 << " has no internal linkage, hence not all call sites are known\n");
1103 AllCallSitesKnown
= false;
1107 // If we do not require all call sites we might not see all.
1108 AllCallSitesKnown
= RequireAllCallSites
;
1110 SmallVector
<const Use
*, 8> Uses(make_pointer_range(Fn
.uses()));
1111 for (unsigned u
= 0; u
< Uses
.size(); ++u
) {
1112 const Use
&U
= *Uses
[u
];
1113 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << *U
<< " in "
1114 << *U
.getUser() << "\n");
1115 bool UsedAssumedInformation
= false;
1116 if (isAssumedDead(U
, QueryingAA
, nullptr, UsedAssumedInformation
,
1117 /* CheckBBLivenessOnly */ true)) {
1118 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
1121 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(U
.getUser())) {
1122 if (CE
->isCast() && CE
->getType()->isPointerTy() &&
1123 CE
->getType()->getPointerElementType()->isFunctionTy()) {
1124 for (const Use
&CEU
: CE
->uses())
1125 Uses
.push_back(&CEU
);
1130 AbstractCallSite
ACS(&U
);
1132 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn
.getName()
1133 << " has non call site use " << *U
.get() << " in "
1134 << *U
.getUser() << "\n");
1135 // BlockAddress users are allowed.
1136 if (isa
<BlockAddress
>(U
.getUser()))
1141 const Use
*EffectiveUse
=
1142 ACS
.isCallbackCall() ? &ACS
.getCalleeUseForCallback() : &U
;
1143 if (!ACS
.isCallee(EffectiveUse
)) {
1144 if (!RequireAllCallSites
)
1146 LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse
->getUser()
1147 << " is an invalid use of " << Fn
.getName() << "\n");
1151 // Make sure the arguments that can be matched between the call site and the
1152 // callee argee on their type. It is unlikely they do not and it doesn't
1153 // make sense for all attributes to know/care about this.
1154 assert(&Fn
== ACS
.getCalledFunction() && "Expected known callee");
1155 unsigned MinArgsParams
=
1156 std::min(size_t(ACS
.getNumArgOperands()), Fn
.arg_size());
1157 for (unsigned u
= 0; u
< MinArgsParams
; ++u
) {
1158 Value
*CSArgOp
= ACS
.getCallArgOperand(u
);
1159 if (CSArgOp
&& Fn
.getArg(u
)->getType() != CSArgOp
->getType()) {
1161 dbgs() << "[Attributor] Call site / callee argument type mismatch ["
1162 << u
<< "@" << Fn
.getName() << ": "
1163 << *Fn
.getArg(u
)->getType() << " vs. "
1164 << *ACS
.getCallArgOperand(u
)->getType() << "\n");
1172 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
1173 << *ACS
.getInstruction() << "\n");
1180 bool Attributor::shouldPropagateCallBaseContext(const IRPosition
&IRP
) {
1181 // TODO: Maintain a cache of Values that are
1182 // on the pathway from a Argument to a Instruction that would effect the
1183 // liveness/return state etc.
1184 return EnableCallSiteSpecific
;
1187 bool Attributor::checkForAllReturnedValuesAndReturnInsts(
1188 function_ref
<bool(Value
&, const SmallSetVector
<ReturnInst
*, 4> &)> Pred
,
1189 const AbstractAttribute
&QueryingAA
) {
1191 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
1192 // Since we need to provide return instructions we have to have an exact
1194 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
1195 if (!AssociatedFunction
)
1198 // If this is a call site query we use the call site specific return values
1199 // and liveness information.
1200 // TODO: use the function scope once we have call site AAReturnedValues.
1201 const IRPosition
&QueryIRP
= IRPosition::function(*AssociatedFunction
);
1202 const auto &AARetVal
=
1203 getAAFor
<AAReturnedValues
>(QueryingAA
, QueryIRP
, DepClassTy::REQUIRED
);
1204 if (!AARetVal
.getState().isValidState())
1207 return AARetVal
.checkForAllReturnedValuesAndReturnInsts(Pred
);
1210 bool Attributor::checkForAllReturnedValues(
1211 function_ref
<bool(Value
&)> Pred
, const AbstractAttribute
&QueryingAA
) {
1213 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
1214 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
1215 if (!AssociatedFunction
)
1218 // TODO: use the function scope once we have call site AAReturnedValues.
1219 const IRPosition
&QueryIRP
= IRPosition::function(
1220 *AssociatedFunction
, QueryingAA
.getCallBaseContext());
1221 const auto &AARetVal
=
1222 getAAFor
<AAReturnedValues
>(QueryingAA
, QueryIRP
, DepClassTy::REQUIRED
);
1223 if (!AARetVal
.getState().isValidState())
1226 return AARetVal
.checkForAllReturnedValuesAndReturnInsts(
1227 [&](Value
&RV
, const SmallSetVector
<ReturnInst
*, 4> &) {
1232 static bool checkForAllInstructionsImpl(
1233 Attributor
*A
, InformationCache::OpcodeInstMapTy
&OpcodeInstMap
,
1234 function_ref
<bool(Instruction
&)> Pred
, const AbstractAttribute
*QueryingAA
,
1235 const AAIsDead
*LivenessAA
, const ArrayRef
<unsigned> &Opcodes
,
1236 bool &UsedAssumedInformation
, bool CheckBBLivenessOnly
= false,
1237 bool CheckPotentiallyDead
= false) {
1238 for (unsigned Opcode
: Opcodes
) {
1239 // Check if we have instructions with this opcode at all first.
1240 auto *Insts
= OpcodeInstMap
.lookup(Opcode
);
1244 for (Instruction
*I
: *Insts
) {
1245 // Skip dead instructions.
1246 if (A
&& !CheckPotentiallyDead
&&
1247 A
->isAssumedDead(IRPosition::value(*I
), QueryingAA
, LivenessAA
,
1248 UsedAssumedInformation
, CheckBBLivenessOnly
))
1258 bool Attributor::checkForAllInstructions(function_ref
<bool(Instruction
&)> Pred
,
1259 const AbstractAttribute
&QueryingAA
,
1260 const ArrayRef
<unsigned> &Opcodes
,
1261 bool &UsedAssumedInformation
,
1262 bool CheckBBLivenessOnly
,
1263 bool CheckPotentiallyDead
) {
1265 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
1266 // Since we need to provide instructions we have to have an exact definition.
1267 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
1268 if (!AssociatedFunction
)
1271 if (AssociatedFunction
->isDeclaration())
1274 // TODO: use the function scope once we have call site AAReturnedValues.
1275 const IRPosition
&QueryIRP
= IRPosition::function(*AssociatedFunction
);
1276 const auto *LivenessAA
=
1277 (CheckBBLivenessOnly
|| CheckPotentiallyDead
)
1279 : &(getAAFor
<AAIsDead
>(QueryingAA
, QueryIRP
, DepClassTy::NONE
));
1281 auto &OpcodeInstMap
=
1282 InfoCache
.getOpcodeInstMapForFunction(*AssociatedFunction
);
1283 if (!checkForAllInstructionsImpl(this, OpcodeInstMap
, Pred
, &QueryingAA
,
1284 LivenessAA
, Opcodes
, UsedAssumedInformation
,
1285 CheckBBLivenessOnly
, CheckPotentiallyDead
))
1291 bool Attributor::checkForAllReadWriteInstructions(
1292 function_ref
<bool(Instruction
&)> Pred
, AbstractAttribute
&QueryingAA
,
1293 bool &UsedAssumedInformation
) {
1295 const Function
*AssociatedFunction
=
1296 QueryingAA
.getIRPosition().getAssociatedFunction();
1297 if (!AssociatedFunction
)
1300 // TODO: use the function scope once we have call site AAReturnedValues.
1301 const IRPosition
&QueryIRP
= IRPosition::function(*AssociatedFunction
);
1302 const auto &LivenessAA
=
1303 getAAFor
<AAIsDead
>(QueryingAA
, QueryIRP
, DepClassTy::NONE
);
1305 for (Instruction
*I
:
1306 InfoCache
.getReadOrWriteInstsForFunction(*AssociatedFunction
)) {
1307 // Skip dead instructions.
1308 if (isAssumedDead(IRPosition::value(*I
), &QueryingAA
, &LivenessAA
,
1309 UsedAssumedInformation
))
1319 void Attributor::runTillFixpoint() {
1320 TimeTraceScope
TimeScope("Attributor::runTillFixpoint");
1321 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
1322 << DG
.SyntheticRoot
.Deps
.size()
1323 << " abstract attributes.\n");
1325 // Now that all abstract attributes are collected and initialized we start
1326 // the abstract analysis.
1328 unsigned IterationCounter
= 1;
1329 unsigned MaxFixedPointIterations
;
1330 if (MaxFixpointIterations
)
1331 MaxFixedPointIterations
= MaxFixpointIterations
.getValue();
1333 MaxFixedPointIterations
= SetFixpointIterations
;
1335 SmallVector
<AbstractAttribute
*, 32> ChangedAAs
;
1336 SetVector
<AbstractAttribute
*> Worklist
, InvalidAAs
;
1337 Worklist
.insert(DG
.SyntheticRoot
.begin(), DG
.SyntheticRoot
.end());
1340 // Remember the size to determine new attributes.
1341 size_t NumAAs
= DG
.SyntheticRoot
.Deps
.size();
1342 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
1343 << ", Worklist size: " << Worklist
.size() << "\n");
1345 // For invalid AAs we can fix dependent AAs that have a required dependence,
1346 // thereby folding long dependence chains in a single step without the need
1348 for (unsigned u
= 0; u
< InvalidAAs
.size(); ++u
) {
1349 AbstractAttribute
*InvalidAA
= InvalidAAs
[u
];
1351 // Check the dependences to fast track invalidation.
1352 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA
<< " has "
1353 << InvalidAA
->Deps
.size()
1354 << " required & optional dependences\n");
1355 while (!InvalidAA
->Deps
.empty()) {
1356 const auto &Dep
= InvalidAA
->Deps
.back();
1357 InvalidAA
->Deps
.pop_back();
1358 AbstractAttribute
*DepAA
= cast
<AbstractAttribute
>(Dep
.getPointer());
1359 if (Dep
.getInt() == unsigned(DepClassTy::OPTIONAL
)) {
1360 Worklist
.insert(DepAA
);
1363 DepAA
->getState().indicatePessimisticFixpoint();
1364 assert(DepAA
->getState().isAtFixpoint() && "Expected fixpoint state!");
1365 if (!DepAA
->getState().isValidState())
1366 InvalidAAs
.insert(DepAA
);
1368 ChangedAAs
.push_back(DepAA
);
1372 // Add all abstract attributes that are potentially dependent on one that
1373 // changed to the work list.
1374 for (AbstractAttribute
*ChangedAA
: ChangedAAs
)
1375 while (!ChangedAA
->Deps
.empty()) {
1377 cast
<AbstractAttribute
>(ChangedAA
->Deps
.back().getPointer()));
1378 ChangedAA
->Deps
.pop_back();
1381 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
1382 << ", Worklist+Dependent size: " << Worklist
.size()
1385 // Reset the changed and invalid set.
1389 // Update all abstract attribute in the work list and record the ones that
1391 for (AbstractAttribute
*AA
: Worklist
) {
1392 const auto &AAState
= AA
->getState();
1393 if (!AAState
.isAtFixpoint())
1394 if (updateAA(*AA
) == ChangeStatus::CHANGED
)
1395 ChangedAAs
.push_back(AA
);
1397 // Use the InvalidAAs vector to propagate invalid states fast transitively
1398 // without requiring updates.
1399 if (!AAState
.isValidState())
1400 InvalidAAs
.insert(AA
);
1403 // Add attributes to the changed set if they have been created in the last
1405 ChangedAAs
.append(DG
.SyntheticRoot
.begin() + NumAAs
,
1406 DG
.SyntheticRoot
.end());
1408 // Reset the work list and repopulate with the changed abstract attributes.
1409 // Note that dependent ones are added above.
1411 Worklist
.insert(ChangedAAs
.begin(), ChangedAAs
.end());
1413 } while (!Worklist
.empty() && (IterationCounter
++ < MaxFixedPointIterations
||
1414 VerifyMaxFixpointIterations
));
1416 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
1417 << IterationCounter
<< "/" << MaxFixpointIterations
1418 << " iterations\n");
1420 // Reset abstract arguments not settled in a sound fixpoint by now. This
1421 // happens when we stopped the fixpoint iteration early. Note that only the
1422 // ones marked as "changed" *and* the ones transitively depending on them
1423 // need to be reverted to a pessimistic state. Others might not be in a
1424 // fixpoint state but we can use the optimistic results for them anyway.
1425 SmallPtrSet
<AbstractAttribute
*, 32> Visited
;
1426 for (unsigned u
= 0; u
< ChangedAAs
.size(); u
++) {
1427 AbstractAttribute
*ChangedAA
= ChangedAAs
[u
];
1428 if (!Visited
.insert(ChangedAA
).second
)
1431 AbstractState
&State
= ChangedAA
->getState();
1432 if (!State
.isAtFixpoint()) {
1433 State
.indicatePessimisticFixpoint();
1435 NumAttributesTimedOut
++;
1438 while (!ChangedAA
->Deps
.empty()) {
1439 ChangedAAs
.push_back(
1440 cast
<AbstractAttribute
>(ChangedAA
->Deps
.back().getPointer()));
1441 ChangedAA
->Deps
.pop_back();
1446 if (!Visited
.empty())
1447 dbgs() << "\n[Attributor] Finalized " << Visited
.size()
1448 << " abstract attributes.\n";
1451 if (VerifyMaxFixpointIterations
&&
1452 IterationCounter
!= MaxFixedPointIterations
) {
1453 errs() << "\n[Attributor] Fixpoint iteration done after: "
1454 << IterationCounter
<< "/" << MaxFixedPointIterations
1456 llvm_unreachable("The fixpoint was not reached with exactly the number of "
1457 "specified iterations!");
1461 ChangeStatus
Attributor::manifestAttributes() {
1462 TimeTraceScope
TimeScope("Attributor::manifestAttributes");
1463 size_t NumFinalAAs
= DG
.SyntheticRoot
.Deps
.size();
1465 unsigned NumManifested
= 0;
1466 unsigned NumAtFixpoint
= 0;
1467 ChangeStatus ManifestChange
= ChangeStatus::UNCHANGED
;
1468 for (auto &DepAA
: DG
.SyntheticRoot
.Deps
) {
1469 AbstractAttribute
*AA
= cast
<AbstractAttribute
>(DepAA
.getPointer());
1470 AbstractState
&State
= AA
->getState();
1472 // If there is not already a fixpoint reached, we can now take the
1473 // optimistic state. This is correct because we enforced a pessimistic one
1474 // on abstract attributes that were transitively dependent on a changed one
1476 if (!State
.isAtFixpoint())
1477 State
.indicateOptimisticFixpoint();
1479 // We must not manifest Attributes that use Callbase info.
1480 if (AA
->hasCallBaseContext())
1482 // If the state is invalid, we do not try to manifest it.
1483 if (!State
.isValidState())
1487 bool UsedAssumedInformation
= false;
1488 if (isAssumedDead(*AA
, nullptr, UsedAssumedInformation
,
1489 /* CheckBBLivenessOnly */ true))
1491 // Check if the manifest debug counter that allows skipping manifestation of
1493 if (!DebugCounter::shouldExecute(ManifestDBGCounter
))
1495 // Manifest the state and record if we changed the IR.
1496 ChangeStatus LocalChange
= AA
->manifest(*this);
1497 if (LocalChange
== ChangeStatus::CHANGED
&& AreStatisticsEnabled())
1498 AA
->trackStatistics();
1499 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange
<< " : " << *AA
1502 ManifestChange
= ManifestChange
| LocalChange
;
1505 NumManifested
+= (LocalChange
== ChangeStatus::CHANGED
);
1508 (void)NumManifested
;
1509 (void)NumAtFixpoint
;
1510 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
1511 << " arguments while " << NumAtFixpoint
1512 << " were in a valid fixpoint state\n");
1514 NumAttributesManifested
+= NumManifested
;
1515 NumAttributesValidFixpoint
+= NumAtFixpoint
;
1518 if (NumFinalAAs
!= DG
.SyntheticRoot
.Deps
.size()) {
1519 for (unsigned u
= NumFinalAAs
; u
< DG
.SyntheticRoot
.Deps
.size(); ++u
)
1520 errs() << "Unexpected abstract attribute: "
1521 << cast
<AbstractAttribute
>(DG
.SyntheticRoot
.Deps
[u
].getPointer())
1523 << cast
<AbstractAttribute
>(DG
.SyntheticRoot
.Deps
[u
].getPointer())
1525 .getAssociatedValue()
1527 llvm_unreachable("Expected the final number of abstract attributes to "
1528 "remain unchanged!");
1530 return ManifestChange
;
1533 void Attributor::identifyDeadInternalFunctions() {
1534 // Early exit if we don't intend to delete functions.
1538 // Identify dead internal functions and delete them. This happens outside
1539 // the other fixpoint analysis as we might treat potentially dead functions
1540 // as live to lower the number of iterations. If they happen to be dead, the
1541 // below fixpoint loop will identify and eliminate them.
1542 SmallVector
<Function
*, 8> InternalFns
;
1543 for (Function
*F
: Functions
)
1544 if (F
->hasLocalLinkage())
1545 InternalFns
.push_back(F
);
1547 SmallPtrSet
<Function
*, 8> LiveInternalFns
;
1548 bool FoundLiveInternal
= true;
1549 while (FoundLiveInternal
) {
1550 FoundLiveInternal
= false;
1551 for (unsigned u
= 0, e
= InternalFns
.size(); u
< e
; ++u
) {
1552 Function
*F
= InternalFns
[u
];
1556 bool AllCallSitesKnown
;
1557 if (checkForAllCallSites(
1558 [&](AbstractCallSite ACS
) {
1559 Function
*Callee
= ACS
.getInstruction()->getFunction();
1560 return ToBeDeletedFunctions
.count(Callee
) ||
1561 (Functions
.count(Callee
) && Callee
->hasLocalLinkage() &&
1562 !LiveInternalFns
.count(Callee
));
1564 *F
, true, nullptr, AllCallSitesKnown
)) {
1568 LiveInternalFns
.insert(F
);
1569 InternalFns
[u
] = nullptr;
1570 FoundLiveInternal
= true;
1574 for (unsigned u
= 0, e
= InternalFns
.size(); u
< e
; ++u
)
1575 if (Function
*F
= InternalFns
[u
])
1576 ToBeDeletedFunctions
.insert(F
);
1579 ChangeStatus
Attributor::cleanupIR() {
1580 TimeTraceScope
TimeScope("Attributor::cleanupIR");
1581 // Delete stuff at the end to avoid invalid references and a nice order.
1582 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least "
1583 << ToBeDeletedFunctions
.size() << " functions and "
1584 << ToBeDeletedBlocks
.size() << " blocks and "
1585 << ToBeDeletedInsts
.size() << " instructions and "
1586 << ToBeChangedValues
.size() << " values and "
1587 << ToBeChangedUses
.size() << " uses. "
1588 << "Preserve manifest added " << ManifestAddedBlocks
.size()
1591 SmallVector
<WeakTrackingVH
, 32> DeadInsts
;
1592 SmallVector
<Instruction
*, 32> TerminatorsToFold
;
1594 auto ReplaceUse
= [&](Use
*U
, Value
*NewV
) {
1595 Value
*OldV
= U
->get();
1597 // If we plan to replace NewV we need to update it at this point.
1599 const auto &Entry
= ToBeChangedValues
.lookup(NewV
);
1605 // Do not replace uses in returns if the value is a must-tail call we will
1607 if (auto *RI
= dyn_cast
<ReturnInst
>(U
->getUser())) {
1608 if (auto *CI
= dyn_cast
<CallInst
>(OldV
->stripPointerCasts()))
1609 if (CI
->isMustTailCall() &&
1610 (!ToBeDeletedInsts
.count(CI
) || !isRunOn(*CI
->getCaller())))
1612 // If we rewrite a return and the new value is not an argument, strip the
1613 // `returned` attribute as it is wrong now.
1614 if (!isa
<Argument
>(NewV
))
1615 for (auto &Arg
: RI
->getFunction()->args())
1616 Arg
.removeAttr(Attribute::Returned
);
1619 // Do not perform call graph altering changes outside the SCC.
1620 if (auto *CB
= dyn_cast
<CallBase
>(U
->getUser()))
1621 if (CB
->isCallee(U
) && !isRunOn(*CB
->getCaller()))
1624 LLVM_DEBUG(dbgs() << "Use " << *NewV
<< " in " << *U
->getUser()
1625 << " instead of " << *OldV
<< "\n");
1628 if (Instruction
*I
= dyn_cast
<Instruction
>(OldV
)) {
1629 CGModifiedFunctions
.insert(I
->getFunction());
1630 if (!isa
<PHINode
>(I
) && !ToBeDeletedInsts
.count(I
) &&
1631 isInstructionTriviallyDead(I
))
1632 DeadInsts
.push_back(I
);
1634 if (isa
<UndefValue
>(NewV
) && isa
<CallBase
>(U
->getUser())) {
1635 auto *CB
= cast
<CallBase
>(U
->getUser());
1636 if (CB
->isArgOperand(U
)) {
1637 unsigned Idx
= CB
->getArgOperandNo(U
);
1638 CB
->removeParamAttr(Idx
, Attribute::NoUndef
);
1639 Function
*Fn
= CB
->getCalledFunction();
1640 if (Fn
&& Fn
->arg_size() > Idx
)
1641 Fn
->removeParamAttr(Idx
, Attribute::NoUndef
);
1644 if (isa
<Constant
>(NewV
) && isa
<BranchInst
>(U
->getUser())) {
1645 Instruction
*UserI
= cast
<Instruction
>(U
->getUser());
1646 if (isa
<UndefValue
>(NewV
)) {
1647 ToBeChangedToUnreachableInsts
.insert(UserI
);
1649 TerminatorsToFold
.push_back(UserI
);
1654 for (auto &It
: ToBeChangedUses
) {
1656 Value
*NewV
= It
.second
;
1657 ReplaceUse(U
, NewV
);
1660 SmallVector
<Use
*, 4> Uses
;
1661 for (auto &It
: ToBeChangedValues
) {
1662 Value
*OldV
= It
.first
;
1663 auto &Entry
= It
.second
;
1664 Value
*NewV
= Entry
.first
;
1666 for (auto &U
: OldV
->uses())
1667 if (Entry
.second
|| !U
.getUser()->isDroppable())
1670 ReplaceUse(U
, NewV
);
1673 for (auto &V
: InvokeWithDeadSuccessor
)
1674 if (InvokeInst
*II
= dyn_cast_or_null
<InvokeInst
>(V
)) {
1675 assert(isRunOn(*II
->getFunction()) &&
1676 "Cannot replace an invoke outside the current SCC!");
1677 bool UnwindBBIsDead
= II
->hasFnAttr(Attribute::NoUnwind
);
1678 bool NormalBBIsDead
= II
->hasFnAttr(Attribute::NoReturn
);
1679 bool Invoke2CallAllowed
=
1680 !AAIsDead::mayCatchAsynchronousExceptions(*II
->getFunction());
1681 assert((UnwindBBIsDead
|| NormalBBIsDead
) &&
1682 "Invoke does not have dead successors!");
1683 BasicBlock
*BB
= II
->getParent();
1684 BasicBlock
*NormalDestBB
= II
->getNormalDest();
1685 if (UnwindBBIsDead
) {
1686 Instruction
*NormalNextIP
= &NormalDestBB
->front();
1687 if (Invoke2CallAllowed
) {
1689 NormalNextIP
= BB
->getTerminator();
1692 ToBeChangedToUnreachableInsts
.insert(NormalNextIP
);
1694 assert(NormalBBIsDead
&& "Broken invariant!");
1695 if (!NormalDestBB
->getUniquePredecessor())
1696 NormalDestBB
= SplitBlockPredecessors(NormalDestBB
, {BB
}, ".dead");
1697 ToBeChangedToUnreachableInsts
.insert(&NormalDestBB
->front());
1700 for (Instruction
*I
: TerminatorsToFold
) {
1701 if (!isRunOn(*I
->getFunction()))
1703 CGModifiedFunctions
.insert(I
->getFunction());
1704 ConstantFoldTerminator(I
->getParent());
1706 for (auto &V
: ToBeChangedToUnreachableInsts
)
1707 if (Instruction
*I
= dyn_cast_or_null
<Instruction
>(V
)) {
1708 if (!isRunOn(*I
->getFunction()))
1710 CGModifiedFunctions
.insert(I
->getFunction());
1711 changeToUnreachable(I
);
1714 for (auto &V
: ToBeDeletedInsts
) {
1715 if (Instruction
*I
= dyn_cast_or_null
<Instruction
>(V
)) {
1716 if (auto *CB
= dyn_cast
<CallBase
>(I
)) {
1717 if (!isRunOn(*I
->getFunction()))
1719 if (!isa
<IntrinsicInst
>(CB
))
1720 CGUpdater
.removeCallSite(*CB
);
1722 I
->dropDroppableUses();
1723 CGModifiedFunctions
.insert(I
->getFunction());
1724 if (!I
->getType()->isVoidTy())
1725 I
->replaceAllUsesWith(UndefValue::get(I
->getType()));
1726 if (!isa
<PHINode
>(I
) && isInstructionTriviallyDead(I
))
1727 DeadInsts
.push_back(I
);
1729 I
->eraseFromParent();
1733 llvm::erase_if(DeadInsts
, [&](WeakTrackingVH I
) {
1734 return !I
|| !isRunOn(*cast
<Instruction
>(I
)->getFunction());
1738 dbgs() << "[Attributor] DeadInsts size: " << DeadInsts
.size() << "\n";
1739 for (auto &I
: DeadInsts
)
1741 dbgs() << " - " << *I
<< "\n";
1744 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts
);
1746 if (unsigned NumDeadBlocks
= ToBeDeletedBlocks
.size()) {
1747 SmallVector
<BasicBlock
*, 8> ToBeDeletedBBs
;
1748 ToBeDeletedBBs
.reserve(NumDeadBlocks
);
1749 for (BasicBlock
*BB
: ToBeDeletedBlocks
) {
1750 assert(isRunOn(*BB
->getParent()) &&
1751 "Cannot delete a block outside the current SCC!");
1752 CGModifiedFunctions
.insert(BB
->getParent());
1753 // Do not delete BBs added during manifests of AAs.
1754 if (ManifestAddedBlocks
.contains(BB
))
1756 ToBeDeletedBBs
.push_back(BB
);
1758 // Actually we do not delete the blocks but squash them into a single
1759 // unreachable but untangling branches that jump here is something we need
1760 // to do in a more generic way.
1761 DetatchDeadBlocks(ToBeDeletedBBs
, nullptr);
1764 identifyDeadInternalFunctions();
1766 // Rewrite the functions as requested during manifest.
1767 ChangeStatus ManifestChange
= rewriteFunctionSignatures(CGModifiedFunctions
);
1769 for (Function
*Fn
: CGModifiedFunctions
)
1770 if (!ToBeDeletedFunctions
.count(Fn
) && Functions
.count(Fn
))
1771 CGUpdater
.reanalyzeFunction(*Fn
);
1773 for (Function
*Fn
: ToBeDeletedFunctions
) {
1774 if (!Functions
.count(Fn
))
1776 CGUpdater
.removeFunction(*Fn
);
1779 if (!ToBeChangedUses
.empty())
1780 ManifestChange
= ChangeStatus::CHANGED
;
1782 if (!ToBeChangedToUnreachableInsts
.empty())
1783 ManifestChange
= ChangeStatus::CHANGED
;
1785 if (!ToBeDeletedFunctions
.empty())
1786 ManifestChange
= ChangeStatus::CHANGED
;
1788 if (!ToBeDeletedBlocks
.empty())
1789 ManifestChange
= ChangeStatus::CHANGED
;
1791 if (!ToBeDeletedInsts
.empty())
1792 ManifestChange
= ChangeStatus::CHANGED
;
1794 if (!InvokeWithDeadSuccessor
.empty())
1795 ManifestChange
= ChangeStatus::CHANGED
;
1797 if (!DeadInsts
.empty())
1798 ManifestChange
= ChangeStatus::CHANGED
;
1800 NumFnDeleted
+= ToBeDeletedFunctions
.size();
1802 LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions
.size()
1803 << " functions after manifest.\n");
1805 #ifdef EXPENSIVE_CHECKS
1806 for (Function
*F
: Functions
) {
1807 if (ToBeDeletedFunctions
.count(F
))
1809 assert(!verifyFunction(*F
, &errs()) && "Module verification failed!");
1813 return ManifestChange
;
1816 ChangeStatus
Attributor::run() {
1817 TimeTraceScope
TimeScope("Attributor::run");
1818 AttributorCallGraph
ACallGraph(*this);
1821 ACallGraph
.populateAll();
1823 Phase
= AttributorPhase::UPDATE
;
1826 // dump graphs on demand
1833 if (PrintDependencies
)
1836 Phase
= AttributorPhase::MANIFEST
;
1837 ChangeStatus ManifestChange
= manifestAttributes();
1839 Phase
= AttributorPhase::CLEANUP
;
1840 ChangeStatus CleanupChange
= cleanupIR();
1845 return ManifestChange
| CleanupChange
;
1848 ChangeStatus
Attributor::updateAA(AbstractAttribute
&AA
) {
1849 TimeTraceScope
TimeScope(
1850 AA
.getName() + std::to_string(AA
.getIRPosition().getPositionKind()) +
1852 assert(Phase
== AttributorPhase::UPDATE
&&
1853 "We can update AA only in the update stage!");
1855 // Use a new dependence vector for this update.
1856 DependenceVector DV
;
1857 DependenceStack
.push_back(&DV
);
1859 auto &AAState
= AA
.getState();
1860 ChangeStatus CS
= ChangeStatus::UNCHANGED
;
1861 bool UsedAssumedInformation
= false;
1862 if (!isAssumedDead(AA
, nullptr, UsedAssumedInformation
,
1863 /* CheckBBLivenessOnly */ true))
1864 CS
= AA
.update(*this);
1867 // If the attribute did not query any non-fix information, the state
1868 // will not change and we can indicate that right away.
1869 AAState
.indicateOptimisticFixpoint();
1872 if (!AAState
.isAtFixpoint())
1873 rememberDependences();
1875 // Verify the stack was used properly, that is we pop the dependence vector we
1876 // put there earlier.
1877 DependenceVector
*PoppedDV
= DependenceStack
.pop_back_val();
1879 assert(PoppedDV
== &DV
&& "Inconsistent usage of the dependence stack!");
1884 void Attributor::createShallowWrapper(Function
&F
) {
1885 assert(!F
.isDeclaration() && "Cannot create a wrapper around a declaration!");
1887 Module
&M
= *F
.getParent();
1888 LLVMContext
&Ctx
= M
.getContext();
1889 FunctionType
*FnTy
= F
.getFunctionType();
1892 Function::Create(FnTy
, F
.getLinkage(), F
.getAddressSpace(), F
.getName());
1893 F
.setName(""); // set the inside function anonymous
1894 M
.getFunctionList().insert(F
.getIterator(), Wrapper
);
1896 F
.setLinkage(GlobalValue::InternalLinkage
);
1898 F
.replaceAllUsesWith(Wrapper
);
1899 assert(F
.use_empty() && "Uses remained after wrapper was created!");
1901 // Move the COMDAT section to the wrapper.
1902 // TODO: Check if we need to keep it for F as well.
1903 Wrapper
->setComdat(F
.getComdat());
1904 F
.setComdat(nullptr);
1906 // Copy all metadata and attributes but keep them on F as well.
1907 SmallVector
<std::pair
<unsigned, MDNode
*>, 1> MDs
;
1908 F
.getAllMetadata(MDs
);
1909 for (auto MDIt
: MDs
)
1910 Wrapper
->addMetadata(MDIt
.first
, *MDIt
.second
);
1911 Wrapper
->setAttributes(F
.getAttributes());
1913 // Create the call in the wrapper.
1914 BasicBlock
*EntryBB
= BasicBlock::Create(Ctx
, "entry", Wrapper
);
1916 SmallVector
<Value
*, 8> Args
;
1917 Argument
*FArgIt
= F
.arg_begin();
1918 for (Argument
&Arg
: Wrapper
->args()) {
1919 Args
.push_back(&Arg
);
1920 Arg
.setName((FArgIt
++)->getName());
1923 CallInst
*CI
= CallInst::Create(&F
, Args
, "", EntryBB
);
1924 CI
->setTailCall(true);
1925 CI
->addFnAttr(Attribute::NoInline
);
1926 ReturnInst::Create(Ctx
, CI
->getType()->isVoidTy() ? nullptr : CI
, EntryBB
);
1928 NumFnShallowWrappersCreated
++;
1931 bool Attributor::isInternalizable(Function
&F
) {
1932 if (F
.isDeclaration() || F
.hasLocalLinkage() ||
1933 GlobalValue::isInterposableLinkage(F
.getLinkage()))
1938 Function
*Attributor::internalizeFunction(Function
&F
, bool Force
) {
1939 if (!AllowDeepWrapper
&& !Force
)
1941 if (!isInternalizable(F
))
1944 SmallPtrSet
<Function
*, 2> FnSet
= {&F
};
1945 DenseMap
<Function
*, Function
*> InternalizedFns
;
1946 internalizeFunctions(FnSet
, InternalizedFns
);
1948 return InternalizedFns
[&F
];
1951 bool Attributor::internalizeFunctions(SmallPtrSetImpl
<Function
*> &FnSet
,
1952 DenseMap
<Function
*, Function
*> &FnMap
) {
1953 for (Function
*F
: FnSet
)
1954 if (!Attributor::isInternalizable(*F
))
1958 // Generate the internalized version of each function.
1959 for (Function
*F
: FnSet
) {
1960 Module
&M
= *F
->getParent();
1961 FunctionType
*FnTy
= F
->getFunctionType();
1963 // Create a copy of the current function
1965 Function::Create(FnTy
, F
->getLinkage(), F
->getAddressSpace(),
1966 F
->getName() + ".internalized");
1967 ValueToValueMapTy VMap
;
1968 auto *NewFArgIt
= Copied
->arg_begin();
1969 for (auto &Arg
: F
->args()) {
1970 auto ArgName
= Arg
.getName();
1971 NewFArgIt
->setName(ArgName
);
1972 VMap
[&Arg
] = &(*NewFArgIt
++);
1974 SmallVector
<ReturnInst
*, 8> Returns
;
1976 // Copy the body of the original function to the new one
1977 CloneFunctionInto(Copied
, F
, VMap
,
1978 CloneFunctionChangeType::LocalChangesOnly
, Returns
);
1980 // Set the linakage and visibility late as CloneFunctionInto has some
1981 // implicit requirements.
1982 Copied
->setVisibility(GlobalValue::DefaultVisibility
);
1983 Copied
->setLinkage(GlobalValue::PrivateLinkage
);
1986 SmallVector
<std::pair
<unsigned, MDNode
*>, 1> MDs
;
1987 F
->getAllMetadata(MDs
);
1988 for (auto MDIt
: MDs
)
1989 if (!Copied
->hasMetadata())
1990 Copied
->addMetadata(MDIt
.first
, *MDIt
.second
);
1992 M
.getFunctionList().insert(F
->getIterator(), Copied
);
1993 Copied
->setDSOLocal(true);
1997 // Replace all uses of the old function with the new internalized function
1998 // unless the caller is a function that was just internalized.
1999 for (Function
*F
: FnSet
) {
2000 auto &InternalizedFn
= FnMap
[F
];
2001 auto IsNotInternalized
= [&](Use
&U
) -> bool {
2002 if (auto *CB
= dyn_cast
<CallBase
>(U
.getUser()))
2003 return !FnMap
.lookup(CB
->getCaller());
2006 F
->replaceUsesWithIf(InternalizedFn
, IsNotInternalized
);
2012 bool Attributor::isValidFunctionSignatureRewrite(
2013 Argument
&Arg
, ArrayRef
<Type
*> ReplacementTypes
) {
2015 if (!RewriteSignatures
)
2018 Function
*Fn
= Arg
.getParent();
2019 auto CallSiteCanBeChanged
= [Fn
](AbstractCallSite ACS
) {
2020 // Forbid the call site to cast the function return type. If we need to
2021 // rewrite these functions we need to re-create a cast for the new call site
2022 // (if the old had uses).
2023 if (!ACS
.getCalledFunction() ||
2024 ACS
.getInstruction()->getType() !=
2025 ACS
.getCalledFunction()->getReturnType())
2027 if (ACS
.getCalledOperand()->getType() != Fn
->getType())
2029 // Forbid must-tail calls for now.
2030 return !ACS
.isCallbackCall() && !ACS
.getInstruction()->isMustTailCall();
2033 // Avoid var-arg functions for now.
2034 if (Fn
->isVarArg()) {
2035 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
2039 // Avoid functions with complicated argument passing semantics.
2040 AttributeList FnAttributeList
= Fn
->getAttributes();
2041 if (FnAttributeList
.hasAttrSomewhere(Attribute::Nest
) ||
2042 FnAttributeList
.hasAttrSomewhere(Attribute::StructRet
) ||
2043 FnAttributeList
.hasAttrSomewhere(Attribute::InAlloca
) ||
2044 FnAttributeList
.hasAttrSomewhere(Attribute::Preallocated
)) {
2046 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
2050 // Avoid callbacks for now.
2051 bool AllCallSitesKnown
;
2052 if (!checkForAllCallSites(CallSiteCanBeChanged
, *Fn
, true, nullptr,
2053 AllCallSitesKnown
)) {
2054 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
2058 auto InstPred
= [](Instruction
&I
) {
2059 if (auto *CI
= dyn_cast
<CallInst
>(&I
))
2060 return !CI
->isMustTailCall();
2064 // Forbid must-tail calls for now.
2066 bool UsedAssumedInformation
= false;
2067 auto &OpcodeInstMap
= InfoCache
.getOpcodeInstMapForFunction(*Fn
);
2068 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap
, InstPred
, nullptr,
2069 nullptr, {Instruction::Call
},
2070 UsedAssumedInformation
)) {
2071 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
2078 bool Attributor::registerFunctionSignatureRewrite(
2079 Argument
&Arg
, ArrayRef
<Type
*> ReplacementTypes
,
2080 ArgumentReplacementInfo::CalleeRepairCBTy
&&CalleeRepairCB
,
2081 ArgumentReplacementInfo::ACSRepairCBTy
&&ACSRepairCB
) {
2082 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg
<< " in "
2083 << Arg
.getParent()->getName() << " with "
2084 << ReplacementTypes
.size() << " replacements\n");
2085 assert(isValidFunctionSignatureRewrite(Arg
, ReplacementTypes
) &&
2086 "Cannot register an invalid rewrite");
2088 Function
*Fn
= Arg
.getParent();
2089 SmallVectorImpl
<std::unique_ptr
<ArgumentReplacementInfo
>> &ARIs
=
2090 ArgumentReplacementMap
[Fn
];
2092 ARIs
.resize(Fn
->arg_size());
2094 // If we have a replacement already with less than or equal new arguments,
2095 // ignore this request.
2096 std::unique_ptr
<ArgumentReplacementInfo
> &ARI
= ARIs
[Arg
.getArgNo()];
2097 if (ARI
&& ARI
->getNumReplacementArgs() <= ReplacementTypes
.size()) {
2098 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
2102 // If we have a replacement already but we like the new one better, delete
2106 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg
<< " in "
2107 << Arg
.getParent()->getName() << " with "
2108 << ReplacementTypes
.size() << " replacements\n");
2110 // Remember the replacement.
2111 ARI
.reset(new ArgumentReplacementInfo(*this, Arg
, ReplacementTypes
,
2112 std::move(CalleeRepairCB
),
2113 std::move(ACSRepairCB
)));
2118 bool Attributor::shouldSeedAttribute(AbstractAttribute
&AA
) {
2121 if (SeedAllowList
.size() != 0)
2123 std::count(SeedAllowList
.begin(), SeedAllowList
.end(), AA
.getName());
2124 Function
*Fn
= AA
.getAnchorScope();
2125 if (FunctionSeedAllowList
.size() != 0 && Fn
)
2126 Result
&= std::count(FunctionSeedAllowList
.begin(),
2127 FunctionSeedAllowList
.end(), Fn
->getName());
2132 ChangeStatus
Attributor::rewriteFunctionSignatures(
2133 SmallPtrSetImpl
<Function
*> &ModifiedFns
) {
2134 ChangeStatus Changed
= ChangeStatus::UNCHANGED
;
2136 for (auto &It
: ArgumentReplacementMap
) {
2137 Function
*OldFn
= It
.getFirst();
2139 // Deleted functions do not require rewrites.
2140 if (!Functions
.count(OldFn
) || ToBeDeletedFunctions
.count(OldFn
))
2143 const SmallVectorImpl
<std::unique_ptr
<ArgumentReplacementInfo
>> &ARIs
=
2145 assert(ARIs
.size() == OldFn
->arg_size() && "Inconsistent state!");
2147 SmallVector
<Type
*, 16> NewArgumentTypes
;
2148 SmallVector
<AttributeSet
, 16> NewArgumentAttributes
;
2150 // Collect replacement argument types and copy over existing attributes.
2151 AttributeList OldFnAttributeList
= OldFn
->getAttributes();
2152 for (Argument
&Arg
: OldFn
->args()) {
2153 if (const std::unique_ptr
<ArgumentReplacementInfo
> &ARI
=
2154 ARIs
[Arg
.getArgNo()]) {
2155 NewArgumentTypes
.append(ARI
->ReplacementTypes
.begin(),
2156 ARI
->ReplacementTypes
.end());
2157 NewArgumentAttributes
.append(ARI
->getNumReplacementArgs(),
2160 NewArgumentTypes
.push_back(Arg
.getType());
2161 NewArgumentAttributes
.push_back(
2162 OldFnAttributeList
.getParamAttrs(Arg
.getArgNo()));
2166 FunctionType
*OldFnTy
= OldFn
->getFunctionType();
2167 Type
*RetTy
= OldFnTy
->getReturnType();
2169 // Construct the new function type using the new arguments types.
2170 FunctionType
*NewFnTy
=
2171 FunctionType::get(RetTy
, NewArgumentTypes
, OldFnTy
->isVarArg());
2173 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn
->getName()
2174 << "' from " << *OldFn
->getFunctionType() << " to "
2175 << *NewFnTy
<< "\n");
2177 // Create the new function body and insert it into the module.
2178 Function
*NewFn
= Function::Create(NewFnTy
, OldFn
->getLinkage(),
2179 OldFn
->getAddressSpace(), "");
2180 Functions
.insert(NewFn
);
2181 OldFn
->getParent()->getFunctionList().insert(OldFn
->getIterator(), NewFn
);
2182 NewFn
->takeName(OldFn
);
2183 NewFn
->copyAttributesFrom(OldFn
);
2185 // Patch the pointer to LLVM function in debug info descriptor.
2186 NewFn
->setSubprogram(OldFn
->getSubprogram());
2187 OldFn
->setSubprogram(nullptr);
2189 // Recompute the parameter attributes list based on the new arguments for
2191 LLVMContext
&Ctx
= OldFn
->getContext();
2192 NewFn
->setAttributes(AttributeList::get(
2193 Ctx
, OldFnAttributeList
.getFnAttrs(), OldFnAttributeList
.getRetAttrs(),
2194 NewArgumentAttributes
));
2196 // Since we have now created the new function, splice the body of the old
2197 // function right into the new function, leaving the old rotting hulk of the
2199 NewFn
->getBasicBlockList().splice(NewFn
->begin(),
2200 OldFn
->getBasicBlockList());
2202 // Fixup block addresses to reference new function.
2203 SmallVector
<BlockAddress
*, 8u> BlockAddresses
;
2204 for (User
*U
: OldFn
->users())
2205 if (auto *BA
= dyn_cast
<BlockAddress
>(U
))
2206 BlockAddresses
.push_back(BA
);
2207 for (auto *BA
: BlockAddresses
)
2208 BA
->replaceAllUsesWith(BlockAddress::get(NewFn
, BA
->getBasicBlock()));
2210 // Set of all "call-like" instructions that invoke the old function mapped
2211 // to their new replacements.
2212 SmallVector
<std::pair
<CallBase
*, CallBase
*>, 8> CallSitePairs
;
2214 // Callback to create a new "call-like" instruction for a given one.
2215 auto CallSiteReplacementCreator
= [&](AbstractCallSite ACS
) {
2216 CallBase
*OldCB
= cast
<CallBase
>(ACS
.getInstruction());
2217 const AttributeList
&OldCallAttributeList
= OldCB
->getAttributes();
2219 // Collect the new argument operands for the replacement call site.
2220 SmallVector
<Value
*, 16> NewArgOperands
;
2221 SmallVector
<AttributeSet
, 16> NewArgOperandAttributes
;
2222 for (unsigned OldArgNum
= 0; OldArgNum
< ARIs
.size(); ++OldArgNum
) {
2223 unsigned NewFirstArgNum
= NewArgOperands
.size();
2224 (void)NewFirstArgNum
; // only used inside assert.
2225 if (const std::unique_ptr
<ArgumentReplacementInfo
> &ARI
=
2227 if (ARI
->ACSRepairCB
)
2228 ARI
->ACSRepairCB(*ARI
, ACS
, NewArgOperands
);
2229 assert(ARI
->getNumReplacementArgs() + NewFirstArgNum
==
2230 NewArgOperands
.size() &&
2231 "ACS repair callback did not provide as many operand as new "
2232 "types were registered!");
2233 // TODO: Exose the attribute set to the ACS repair callback
2234 NewArgOperandAttributes
.append(ARI
->ReplacementTypes
.size(),
2237 NewArgOperands
.push_back(ACS
.getCallArgOperand(OldArgNum
));
2238 NewArgOperandAttributes
.push_back(
2239 OldCallAttributeList
.getParamAttrs(OldArgNum
));
2243 assert(NewArgOperands
.size() == NewArgOperandAttributes
.size() &&
2244 "Mismatch # argument operands vs. # argument operand attributes!");
2245 assert(NewArgOperands
.size() == NewFn
->arg_size() &&
2246 "Mismatch # argument operands vs. # function arguments!");
2248 SmallVector
<OperandBundleDef
, 4> OperandBundleDefs
;
2249 OldCB
->getOperandBundlesAsDefs(OperandBundleDefs
);
2251 // Create a new call or invoke instruction to replace the old one.
2253 if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(OldCB
)) {
2255 InvokeInst::Create(NewFn
, II
->getNormalDest(), II
->getUnwindDest(),
2256 NewArgOperands
, OperandBundleDefs
, "", OldCB
);
2258 auto *NewCI
= CallInst::Create(NewFn
, NewArgOperands
, OperandBundleDefs
,
2260 NewCI
->setTailCallKind(cast
<CallInst
>(OldCB
)->getTailCallKind());
2264 // Copy over various properties and the new attributes.
2265 NewCB
->copyMetadata(*OldCB
, {LLVMContext::MD_prof
, LLVMContext::MD_dbg
});
2266 NewCB
->setCallingConv(OldCB
->getCallingConv());
2267 NewCB
->takeName(OldCB
);
2268 NewCB
->setAttributes(AttributeList::get(
2269 Ctx
, OldCallAttributeList
.getFnAttrs(),
2270 OldCallAttributeList
.getRetAttrs(), NewArgOperandAttributes
));
2272 CallSitePairs
.push_back({OldCB
, NewCB
});
2276 // Use the CallSiteReplacementCreator to create replacement call sites.
2277 bool AllCallSitesKnown
;
2278 bool Success
= checkForAllCallSites(CallSiteReplacementCreator
, *OldFn
,
2279 true, nullptr, AllCallSitesKnown
);
2281 assert(Success
&& "Assumed call site replacement to succeed!");
2283 // Rewire the arguments.
2284 Argument
*OldFnArgIt
= OldFn
->arg_begin();
2285 Argument
*NewFnArgIt
= NewFn
->arg_begin();
2286 for (unsigned OldArgNum
= 0; OldArgNum
< ARIs
.size();
2287 ++OldArgNum
, ++OldFnArgIt
) {
2288 if (const std::unique_ptr
<ArgumentReplacementInfo
> &ARI
=
2290 if (ARI
->CalleeRepairCB
)
2291 ARI
->CalleeRepairCB(*ARI
, *NewFn
, NewFnArgIt
);
2292 NewFnArgIt
+= ARI
->ReplacementTypes
.size();
2294 NewFnArgIt
->takeName(&*OldFnArgIt
);
2295 OldFnArgIt
->replaceAllUsesWith(&*NewFnArgIt
);
2300 // Eliminate the instructions *after* we visited all of them.
2301 for (auto &CallSitePair
: CallSitePairs
) {
2302 CallBase
&OldCB
= *CallSitePair
.first
;
2303 CallBase
&NewCB
= *CallSitePair
.second
;
2304 assert(OldCB
.getType() == NewCB
.getType() &&
2305 "Cannot handle call sites with different types!");
2306 ModifiedFns
.insert(OldCB
.getFunction());
2307 CGUpdater
.replaceCallSite(OldCB
, NewCB
);
2308 OldCB
.replaceAllUsesWith(&NewCB
);
2309 OldCB
.eraseFromParent();
2312 // Replace the function in the call graph (if any).
2313 CGUpdater
.replaceFunctionWith(*OldFn
, *NewFn
);
2315 // If the old function was modified and needed to be reanalyzed, the new one
2317 if (ModifiedFns
.erase(OldFn
))
2318 ModifiedFns
.insert(NewFn
);
2320 Changed
= ChangeStatus::CHANGED
;
2326 void InformationCache::initializeInformationCache(const Function
&CF
,
2328 // As we do not modify the function here we can remove the const
2329 // withouth breaking implicit assumptions. At the end of the day, we could
2330 // initialize the cache eagerly which would look the same to the users.
2331 Function
&F
= const_cast<Function
&>(CF
);
2333 // Walk all instructions to find interesting instructions that might be
2334 // queried by abstract attributes during their initialization or update.
2335 // This has to happen before we create attributes.
2337 for (Instruction
&I
: instructions(&F
)) {
2338 bool IsInterestingOpcode
= false;
2340 // To allow easy access to all instructions in a function with a given
2341 // opcode we store them in the InfoCache. As not all opcodes are interesting
2342 // to concrete attributes we only cache the ones that are as identified in
2343 // the following switch.
2344 // Note: There are no concrete attributes now so this is initially empty.
2345 switch (I
.getOpcode()) {
2347 assert(!isa
<CallBase
>(&I
) &&
2348 "New call base instruction type needs to be known in the "
2351 case Instruction::Call
:
2352 // Calls are interesting on their own, additionally:
2353 // For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
2354 // For `must-tail` calls we remember the caller and callee.
2355 if (auto *Assume
= dyn_cast
<AssumeInst
>(&I
)) {
2356 fillMapFromAssume(*Assume
, KnowledgeMap
);
2357 } else if (cast
<CallInst
>(I
).isMustTailCall()) {
2358 FI
.ContainsMustTailCall
= true;
2359 if (const Function
*Callee
= cast
<CallInst
>(I
).getCalledFunction())
2360 getFunctionInfo(*Callee
).CalledViaMustTail
= true;
2363 case Instruction::CallBr
:
2364 case Instruction::Invoke
:
2365 case Instruction::CleanupRet
:
2366 case Instruction::CatchSwitch
:
2367 case Instruction::AtomicRMW
:
2368 case Instruction::AtomicCmpXchg
:
2369 case Instruction::Br
:
2370 case Instruction::Resume
:
2371 case Instruction::Ret
:
2372 case Instruction::Load
:
2373 // The alignment of a pointer is interesting for loads.
2374 case Instruction::Store
:
2375 // The alignment of a pointer is interesting for stores.
2376 case Instruction::Alloca
:
2377 case Instruction::AddrSpaceCast
:
2378 IsInterestingOpcode
= true;
2380 if (IsInterestingOpcode
) {
2381 auto *&Insts
= FI
.OpcodeInstMap
[I
.getOpcode()];
2383 Insts
= new (Allocator
) InstructionVectorTy();
2384 Insts
->push_back(&I
);
2386 if (I
.mayReadOrWriteMemory())
2387 FI
.RWInsts
.push_back(&I
);
2390 if (F
.hasFnAttribute(Attribute::AlwaysInline
) &&
2391 isInlineViable(F
).isSuccess())
2392 InlineableFunctions
.insert(&F
);
2395 AAResults
*InformationCache::getAAResultsForFunction(const Function
&F
) {
2396 return AG
.getAnalysis
<AAManager
>(F
);
2399 InformationCache::FunctionInfo::~FunctionInfo() {
2400 // The instruction vectors are allocated using a BumpPtrAllocator, we need to
2401 // manually destroy them.
2402 for (auto &It
: OpcodeInstMap
)
2403 It
.getSecond()->~InstructionVectorTy();
2406 void Attributor::recordDependence(const AbstractAttribute
&FromAA
,
2407 const AbstractAttribute
&ToAA
,
2408 DepClassTy DepClass
) {
2409 if (DepClass
== DepClassTy::NONE
)
2411 // If we are outside of an update, thus before the actual fixpoint iteration
2412 // started (= when we create AAs), we do not track dependences because we will
2413 // put all AAs into the initial worklist anyway.
2414 if (DependenceStack
.empty())
2416 if (FromAA
.getState().isAtFixpoint())
2418 DependenceStack
.back()->push_back({&FromAA
, &ToAA
, DepClass
});
2421 void Attributor::rememberDependences() {
2422 assert(!DependenceStack
.empty() && "No dependences to remember!");
2424 for (DepInfo
&DI
: *DependenceStack
.back()) {
2425 assert((DI
.DepClass
== DepClassTy::REQUIRED
||
2426 DI
.DepClass
== DepClassTy::OPTIONAL
) &&
2427 "Expected required or optional dependence (1 bit)!");
2428 auto &DepAAs
= const_cast<AbstractAttribute
&>(*DI
.FromAA
).Deps
;
2429 DepAAs
.push_back(AbstractAttribute::DepTy(
2430 const_cast<AbstractAttribute
*>(DI
.ToAA
), unsigned(DI
.DepClass
)));
2434 void Attributor::identifyDefaultAbstractAttributes(Function
&F
) {
2435 if (!VisitedFunctions
.insert(&F
).second
)
2437 if (F
.isDeclaration())
2440 // In non-module runs we need to look at the call sites of a function to
2441 // determine if it is part of a must-tail call edge. This will influence what
2442 // attributes we can derive.
2443 InformationCache::FunctionInfo
&FI
= InfoCache
.getFunctionInfo(F
);
2444 if (!isModulePass() && !FI
.CalledViaMustTail
) {
2445 for (const Use
&U
: F
.uses())
2446 if (const auto *CB
= dyn_cast
<CallBase
>(U
.getUser()))
2447 if (CB
->isCallee(&U
) && CB
->isMustTailCall())
2448 FI
.CalledViaMustTail
= true;
2451 IRPosition FPos
= IRPosition::function(F
);
2453 // Check for dead BasicBlocks in every function.
2454 // We need dead instruction detection because we do not want to deal with
2455 // broken IR in which SSA rules do not apply.
2456 getOrCreateAAFor
<AAIsDead
>(FPos
);
2458 // Every function might be "will-return".
2459 getOrCreateAAFor
<AAWillReturn
>(FPos
);
2461 // Every function might contain instructions that cause "undefined behavior".
2462 getOrCreateAAFor
<AAUndefinedBehavior
>(FPos
);
2464 // Every function can be nounwind.
2465 getOrCreateAAFor
<AANoUnwind
>(FPos
);
2467 // Every function might be marked "nosync"
2468 getOrCreateAAFor
<AANoSync
>(FPos
);
2470 // Every function might be "no-free".
2471 getOrCreateAAFor
<AANoFree
>(FPos
);
2473 // Every function might be "no-return".
2474 getOrCreateAAFor
<AANoReturn
>(FPos
);
2476 // Every function might be "no-recurse".
2477 getOrCreateAAFor
<AANoRecurse
>(FPos
);
2479 // Every function might be "readnone/readonly/writeonly/...".
2480 getOrCreateAAFor
<AAMemoryBehavior
>(FPos
);
2482 // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
2483 getOrCreateAAFor
<AAMemoryLocation
>(FPos
);
2485 // Every function might be applicable for Heap-To-Stack conversion.
2486 if (EnableHeapToStack
)
2487 getOrCreateAAFor
<AAHeapToStack
>(FPos
);
2489 // Return attributes are only appropriate if the return type is non void.
2490 Type
*ReturnType
= F
.getReturnType();
2491 if (!ReturnType
->isVoidTy()) {
2492 // Argument attribute "returned" --- Create only one per function even
2493 // though it is an argument attribute.
2494 getOrCreateAAFor
<AAReturnedValues
>(FPos
);
2496 IRPosition RetPos
= IRPosition::returned(F
);
2498 // Every returned value might be dead.
2499 getOrCreateAAFor
<AAIsDead
>(RetPos
);
2501 // Every function might be simplified.
2502 getOrCreateAAFor
<AAValueSimplify
>(RetPos
);
2504 // Every returned value might be marked noundef.
2505 getOrCreateAAFor
<AANoUndef
>(RetPos
);
2507 if (ReturnType
->isPointerTy()) {
2509 // Every function with pointer return type might be marked align.
2510 getOrCreateAAFor
<AAAlign
>(RetPos
);
2512 // Every function with pointer return type might be marked nonnull.
2513 getOrCreateAAFor
<AANonNull
>(RetPos
);
2515 // Every function with pointer return type might be marked noalias.
2516 getOrCreateAAFor
<AANoAlias
>(RetPos
);
2518 // Every function with pointer return type might be marked
2520 getOrCreateAAFor
<AADereferenceable
>(RetPos
);
2524 for (Argument
&Arg
: F
.args()) {
2525 IRPosition ArgPos
= IRPosition::argument(Arg
);
2527 // Every argument might be simplified. We have to go through the Attributor
2528 // interface though as outside AAs can register custom simplification
2530 bool UsedAssumedInformation
= false;
2531 getAssumedSimplified(ArgPos
, /* AA */ nullptr, UsedAssumedInformation
);
2533 // Every argument might be dead.
2534 getOrCreateAAFor
<AAIsDead
>(ArgPos
);
2536 // Every argument might be marked noundef.
2537 getOrCreateAAFor
<AANoUndef
>(ArgPos
);
2539 if (Arg
.getType()->isPointerTy()) {
2540 // Every argument with pointer type might be marked nonnull.
2541 getOrCreateAAFor
<AANonNull
>(ArgPos
);
2543 // Every argument with pointer type might be marked noalias.
2544 getOrCreateAAFor
<AANoAlias
>(ArgPos
);
2546 // Every argument with pointer type might be marked dereferenceable.
2547 getOrCreateAAFor
<AADereferenceable
>(ArgPos
);
2549 // Every argument with pointer type might be marked align.
2550 getOrCreateAAFor
<AAAlign
>(ArgPos
);
2552 // Every argument with pointer type might be marked nocapture.
2553 getOrCreateAAFor
<AANoCapture
>(ArgPos
);
2555 // Every argument with pointer type might be marked
2556 // "readnone/readonly/writeonly/..."
2557 getOrCreateAAFor
<AAMemoryBehavior
>(ArgPos
);
2559 // Every argument with pointer type might be marked nofree.
2560 getOrCreateAAFor
<AANoFree
>(ArgPos
);
2562 // Every argument with pointer type might be privatizable (or promotable)
2563 getOrCreateAAFor
<AAPrivatizablePtr
>(ArgPos
);
2567 auto CallSitePred
= [&](Instruction
&I
) -> bool {
2568 auto &CB
= cast
<CallBase
>(I
);
2569 IRPosition CBRetPos
= IRPosition::callsite_returned(CB
);
2571 // Call sites might be dead if they do not have side effects and no live
2572 // users. The return value might be dead if there are no live users.
2573 getOrCreateAAFor
<AAIsDead
>(CBRetPos
);
2575 Function
*Callee
= CB
.getCalledFunction();
2576 // TODO: Even if the callee is not known now we might be able to simplify
2581 // Skip declarations except if annotations on their call sites were
2582 // explicitly requested.
2583 if (!AnnotateDeclarationCallSites
&& Callee
->isDeclaration() &&
2584 !Callee
->hasMetadata(LLVMContext::MD_callback
))
2587 if (!Callee
->getReturnType()->isVoidTy() && !CB
.use_empty()) {
2589 IRPosition CBRetPos
= IRPosition::callsite_returned(CB
);
2590 getOrCreateAAFor
<AAValueSimplify
>(CBRetPos
);
2593 for (int I
= 0, E
= CB
.getNumArgOperands(); I
< E
; ++I
) {
2595 IRPosition CBArgPos
= IRPosition::callsite_argument(CB
, I
);
2597 // Every call site argument might be dead.
2598 getOrCreateAAFor
<AAIsDead
>(CBArgPos
);
2600 // Call site argument might be simplified. We have to go through the
2601 // Attributor interface though as outside AAs can register custom
2602 // simplification callbacks.
2603 bool UsedAssumedInformation
= false;
2604 getAssumedSimplified(CBArgPos
, /* AA */ nullptr, UsedAssumedInformation
);
2606 // Every call site argument might be marked "noundef".
2607 getOrCreateAAFor
<AANoUndef
>(CBArgPos
);
2609 if (!CB
.getArgOperand(I
)->getType()->isPointerTy())
2612 // Call site argument attribute "non-null".
2613 getOrCreateAAFor
<AANonNull
>(CBArgPos
);
2615 // Call site argument attribute "nocapture".
2616 getOrCreateAAFor
<AANoCapture
>(CBArgPos
);
2618 // Call site argument attribute "no-alias".
2619 getOrCreateAAFor
<AANoAlias
>(CBArgPos
);
2621 // Call site argument attribute "dereferenceable".
2622 getOrCreateAAFor
<AADereferenceable
>(CBArgPos
);
2624 // Call site argument attribute "align".
2625 getOrCreateAAFor
<AAAlign
>(CBArgPos
);
2627 // Call site argument attribute
2628 // "readnone/readonly/writeonly/..."
2629 getOrCreateAAFor
<AAMemoryBehavior
>(CBArgPos
);
2631 // Call site argument attribute "nofree".
2632 getOrCreateAAFor
<AANoFree
>(CBArgPos
);
2637 auto &OpcodeInstMap
= InfoCache
.getOpcodeInstMapForFunction(F
);
2639 bool UsedAssumedInformation
= false;
2640 Success
= checkForAllInstructionsImpl(
2641 nullptr, OpcodeInstMap
, CallSitePred
, nullptr, nullptr,
2642 {(unsigned)Instruction::Invoke
, (unsigned)Instruction::CallBr
,
2643 (unsigned)Instruction::Call
},
2644 UsedAssumedInformation
);
2646 assert(Success
&& "Expected the check call to be successful!");
2648 auto LoadStorePred
= [&](Instruction
&I
) -> bool {
2649 if (isa
<LoadInst
>(I
)) {
2650 getOrCreateAAFor
<AAAlign
>(
2651 IRPosition::value(*cast
<LoadInst
>(I
).getPointerOperand()));
2652 if (SimplifyAllLoads
)
2653 getOrCreateAAFor
<AAValueSimplify
>(IRPosition::value(I
));
2655 getOrCreateAAFor
<AAAlign
>(
2656 IRPosition::value(*cast
<StoreInst
>(I
).getPointerOperand()));
2659 Success
= checkForAllInstructionsImpl(
2660 nullptr, OpcodeInstMap
, LoadStorePred
, nullptr, nullptr,
2661 {(unsigned)Instruction::Load
, (unsigned)Instruction::Store
},
2662 UsedAssumedInformation
);
2664 assert(Success
&& "Expected the check call to be successful!");
2667 /// Helpers to ease debugging through output streams and print calls.
2670 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, ChangeStatus S
) {
2671 return OS
<< (S
== ChangeStatus::CHANGED
? "changed" : "unchanged");
2674 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, IRPosition::Kind AP
) {
2676 case IRPosition::IRP_INVALID
:
2678 case IRPosition::IRP_FLOAT
:
2680 case IRPosition::IRP_RETURNED
:
2681 return OS
<< "fn_ret";
2682 case IRPosition::IRP_CALL_SITE_RETURNED
:
2683 return OS
<< "cs_ret";
2684 case IRPosition::IRP_FUNCTION
:
2686 case IRPosition::IRP_CALL_SITE
:
2688 case IRPosition::IRP_ARGUMENT
:
2690 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
2691 return OS
<< "cs_arg";
2693 llvm_unreachable("Unknown attribute position!");
2696 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const IRPosition
&Pos
) {
2697 const Value
&AV
= Pos
.getAssociatedValue();
2698 OS
<< "{" << Pos
.getPositionKind() << ":" << AV
.getName() << " ["
2699 << Pos
.getAnchorValue().getName() << "@" << Pos
.getCallSiteArgNo() << "]";
2701 if (Pos
.hasCallBaseContext())
2702 OS
<< "[cb_context:" << *Pos
.getCallBaseContext() << "]";
2706 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const IntegerRangeState
&S
) {
2707 OS
<< "range-state(" << S
.getBitWidth() << ")<";
2708 S
.getKnown().print(OS
);
2710 S
.getAssumed().print(OS
);
2713 return OS
<< static_cast<const AbstractState
&>(S
);
2716 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const AbstractState
&S
) {
2717 return OS
<< (!S
.isValidState() ? "top" : (S
.isAtFixpoint() ? "fix" : ""));
2720 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const AbstractAttribute
&AA
) {
2725 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
2726 const PotentialConstantIntValuesState
&S
) {
2727 OS
<< "set-state(< {";
2728 if (!S
.isValidState())
2731 for (auto &it
: S
.getAssumedSet())
2733 if (S
.undefIsContained())
2741 void AbstractAttribute::print(raw_ostream
&OS
) const {
2744 OS
<< "] for CtxI ";
2746 if (auto *I
= getCtxI()) {
2751 OS
<< "<<null inst>>";
2753 OS
<< " at position " << getIRPosition() << " with state " << getAsStr()
2757 void AbstractAttribute::printWithDeps(raw_ostream
&OS
) const {
2760 for (const auto &DepAA
: Deps
) {
2761 auto *AA
= DepAA
.getPointer();
2769 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
2770 const AAPointerInfo::Access
&Acc
) {
2771 OS
<< " [" << Acc
.getKind() << "] " << *Acc
.getRemoteInst();
2772 if (Acc
.getLocalInst() != Acc
.getRemoteInst())
2773 OS
<< " via " << *Acc
.getLocalInst();
2774 if (Acc
.getContent().hasValue())
2775 OS
<< " [" << *Acc
.getContent() << "]";
2780 /// ----------------------------------------------------------------------------
2781 /// Pass (Manager) Boilerplate
2782 /// ----------------------------------------------------------------------------
2784 static bool runAttributorOnFunctions(InformationCache
&InfoCache
,
2785 SetVector
<Function
*> &Functions
,
2787 CallGraphUpdater
&CGUpdater
,
2789 if (Functions
.empty())
2793 dbgs() << "[Attributor] Run on module with " << Functions
.size()
2795 for (Function
*Fn
: Functions
)
2796 dbgs() << " - " << Fn
->getName() << "\n";
2799 // Create an Attributor and initially empty information cache that is filled
2800 // while we identify default attribute opportunities.
2801 Attributor
A(Functions
, InfoCache
, CGUpdater
, /* Allowed */ nullptr,
2804 // Create shallow wrappers for all functions that are not IPO amendable
2805 if (AllowShallowWrappers
)
2806 for (Function
*F
: Functions
)
2807 if (!A
.isFunctionIPOAmendable(*F
))
2808 Attributor::createShallowWrapper(*F
);
2810 // Internalize non-exact functions
2811 // TODO: for now we eagerly internalize functions without calculating the
2812 // cost, we need a cost interface to determine whether internalizing
2813 // a function is "benefitial"
2814 if (AllowDeepWrapper
) {
2815 unsigned FunSize
= Functions
.size();
2816 for (unsigned u
= 0; u
< FunSize
; u
++) {
2817 Function
*F
= Functions
[u
];
2818 if (!F
->isDeclaration() && !F
->isDefinitionExact() && F
->getNumUses() &&
2819 !GlobalValue::isInterposableLinkage(F
->getLinkage())) {
2820 Function
*NewF
= Attributor::internalizeFunction(*F
);
2821 assert(NewF
&& "Could not internalize function.");
2822 Functions
.insert(NewF
);
2824 // Update call graph
2825 CGUpdater
.replaceFunctionWith(*F
, *NewF
);
2826 for (const Use
&U
: NewF
->uses())
2827 if (CallBase
*CB
= dyn_cast
<CallBase
>(U
.getUser())) {
2828 auto *CallerF
= CB
->getCaller();
2829 CGUpdater
.reanalyzeFunction(*CallerF
);
2835 for (Function
*F
: Functions
) {
2836 if (F
->hasExactDefinition())
2837 NumFnWithExactDefinition
++;
2839 NumFnWithoutExactDefinition
++;
2841 // We look at internal functions only on-demand but if any use is not a
2842 // direct call or outside the current set of analyzed functions, we have
2843 // to do it eagerly.
2844 if (F
->hasLocalLinkage()) {
2845 if (llvm::all_of(F
->uses(), [&Functions
](const Use
&U
) {
2846 const auto *CB
= dyn_cast
<CallBase
>(U
.getUser());
2847 return CB
&& CB
->isCallee(&U
) &&
2848 Functions
.count(const_cast<Function
*>(CB
->getCaller()));
2853 // Populate the Attributor with abstract attribute opportunities in the
2854 // function and the information cache with IR information.
2855 A
.identifyDefaultAbstractAttributes(*F
);
2858 ChangeStatus Changed
= A
.run();
2860 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions
.size()
2861 << " functions, result: " << Changed
<< ".\n");
2862 return Changed
== ChangeStatus::CHANGED
;
2865 void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
2867 void AADepGraph::dumpGraph() {
2868 static std::atomic
<int> CallTimes
;
2871 if (!DepGraphDotFileNamePrefix
.empty())
2872 Prefix
= DepGraphDotFileNamePrefix
;
2874 Prefix
= "dep_graph";
2875 std::string Filename
=
2876 Prefix
+ "_" + std::to_string(CallTimes
.load()) + ".dot";
2878 outs() << "Dependency graph dump to " << Filename
<< ".\n";
2882 raw_fd_ostream
File(Filename
, EC
, sys::fs::OF_TextWithCRLF
);
2884 llvm::WriteGraph(File
, this);
2889 void AADepGraph::print() {
2890 for (auto DepAA
: SyntheticRoot
.Deps
)
2891 cast
<AbstractAttribute
>(DepAA
.getPointer())->printWithDeps(outs());
2894 PreservedAnalyses
AttributorPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
2895 FunctionAnalysisManager
&FAM
=
2896 AM
.getResult
<FunctionAnalysisManagerModuleProxy
>(M
).getManager();
2897 AnalysisGetter
AG(FAM
);
2899 SetVector
<Function
*> Functions
;
2900 for (Function
&F
: M
)
2901 Functions
.insert(&F
);
2903 CallGraphUpdater CGUpdater
;
2904 BumpPtrAllocator Allocator
;
2905 InformationCache
InfoCache(M
, AG
, Allocator
, /* CGSCC */ nullptr);
2906 if (runAttributorOnFunctions(InfoCache
, Functions
, AG
, CGUpdater
,
2907 /* DeleteFns */ true)) {
2908 // FIXME: Think about passes we will preserve and add them here.
2909 return PreservedAnalyses::none();
2911 return PreservedAnalyses::all();
2914 PreservedAnalyses
AttributorCGSCCPass::run(LazyCallGraph::SCC
&C
,
2915 CGSCCAnalysisManager
&AM
,
2917 CGSCCUpdateResult
&UR
) {
2918 FunctionAnalysisManager
&FAM
=
2919 AM
.getResult
<FunctionAnalysisManagerCGSCCProxy
>(C
, CG
).getManager();
2920 AnalysisGetter
AG(FAM
);
2922 SetVector
<Function
*> Functions
;
2923 for (LazyCallGraph::Node
&N
: C
)
2924 Functions
.insert(&N
.getFunction());
2926 if (Functions
.empty())
2927 return PreservedAnalyses::all();
2929 Module
&M
= *Functions
.back()->getParent();
2930 CallGraphUpdater CGUpdater
;
2931 CGUpdater
.initialize(CG
, C
, AM
, UR
);
2932 BumpPtrAllocator Allocator
;
2933 InformationCache
InfoCache(M
, AG
, Allocator
, /* CGSCC */ &Functions
);
2934 if (runAttributorOnFunctions(InfoCache
, Functions
, AG
, CGUpdater
,
2935 /* DeleteFns */ false)) {
2936 // FIXME: Think about passes we will preserve and add them here.
2937 PreservedAnalyses PA
;
2938 PA
.preserve
<FunctionAnalysisManagerCGSCCProxy
>();
2941 return PreservedAnalyses::all();
2946 template <> struct GraphTraits
<AADepGraphNode
*> {
2947 using NodeRef
= AADepGraphNode
*;
2948 using DepTy
= PointerIntPair
<AADepGraphNode
*, 1>;
2949 using EdgeRef
= PointerIntPair
<AADepGraphNode
*, 1>;
2951 static NodeRef
getEntryNode(AADepGraphNode
*DGN
) { return DGN
; }
2952 static NodeRef
DepGetVal(DepTy
&DT
) { return DT
.getPointer(); }
2954 using ChildIteratorType
=
2955 mapped_iterator
<TinyPtrVector
<DepTy
>::iterator
, decltype(&DepGetVal
)>;
2956 using ChildEdgeIteratorType
= TinyPtrVector
<DepTy
>::iterator
;
2958 static ChildIteratorType
child_begin(NodeRef N
) { return N
->child_begin(); }
2960 static ChildIteratorType
child_end(NodeRef N
) { return N
->child_end(); }
2964 struct GraphTraits
<AADepGraph
*> : public GraphTraits
<AADepGraphNode
*> {
2965 static NodeRef
getEntryNode(AADepGraph
*DG
) { return DG
->GetEntryNode(); }
2967 using nodes_iterator
=
2968 mapped_iterator
<TinyPtrVector
<DepTy
>::iterator
, decltype(&DepGetVal
)>;
2970 static nodes_iterator
nodes_begin(AADepGraph
*DG
) { return DG
->begin(); }
2972 static nodes_iterator
nodes_end(AADepGraph
*DG
) { return DG
->end(); }
2975 template <> struct DOTGraphTraits
<AADepGraph
*> : public DefaultDOTGraphTraits
{
2976 DOTGraphTraits(bool isSimple
= false) : DefaultDOTGraphTraits(isSimple
) {}
2978 static std::string
getNodeLabel(const AADepGraphNode
*Node
,
2979 const AADepGraph
*DG
) {
2980 std::string AAString
;
2981 raw_string_ostream
O(AAString
);
2987 } // end namespace llvm
2991 struct AttributorLegacyPass
: public ModulePass
{
2994 AttributorLegacyPass() : ModulePass(ID
) {
2995 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
2998 bool runOnModule(Module
&M
) override
{
3003 SetVector
<Function
*> Functions
;
3004 for (Function
&F
: M
)
3005 Functions
.insert(&F
);
3007 CallGraphUpdater CGUpdater
;
3008 BumpPtrAllocator Allocator
;
3009 InformationCache
InfoCache(M
, AG
, Allocator
, /* CGSCC */ nullptr);
3010 return runAttributorOnFunctions(InfoCache
, Functions
, AG
, CGUpdater
,
3011 /* DeleteFns*/ true);
3014 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
3015 // FIXME: Think about passes we will preserve and add them here.
3016 AU
.addRequired
<TargetLibraryInfoWrapperPass
>();
3020 struct AttributorCGSCCLegacyPass
: public CallGraphSCCPass
{
3023 AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID
) {
3024 initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
3027 bool runOnSCC(CallGraphSCC
&SCC
) override
{
3031 SetVector
<Function
*> Functions
;
3032 for (CallGraphNode
*CGN
: SCC
)
3033 if (Function
*Fn
= CGN
->getFunction())
3034 if (!Fn
->isDeclaration())
3035 Functions
.insert(Fn
);
3037 if (Functions
.empty())
3041 CallGraph
&CG
= const_cast<CallGraph
&>(SCC
.getCallGraph());
3042 CallGraphUpdater CGUpdater
;
3043 CGUpdater
.initialize(CG
, SCC
);
3044 Module
&M
= *Functions
.back()->getParent();
3045 BumpPtrAllocator Allocator
;
3046 InformationCache
InfoCache(M
, AG
, Allocator
, /* CGSCC */ &Functions
);
3047 return runAttributorOnFunctions(InfoCache
, Functions
, AG
, CGUpdater
,
3048 /* DeleteFns */ false);
3051 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
3052 // FIXME: Think about passes we will preserve and add them here.
3053 AU
.addRequired
<TargetLibraryInfoWrapperPass
>();
3054 CallGraphSCCPass::getAnalysisUsage(AU
);
3058 } // end anonymous namespace
3060 Pass
*llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
3061 Pass
*llvm::createAttributorCGSCCLegacyPass() {
3062 return new AttributorCGSCCLegacyPass();
3065 char AttributorLegacyPass::ID
= 0;
3066 char AttributorCGSCCLegacyPass::ID
= 0;
3068 INITIALIZE_PASS_BEGIN(AttributorLegacyPass
, "attributor",
3069 "Deduce and propagate attributes", false, false)
3070 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass
)
3071 INITIALIZE_PASS_END(AttributorLegacyPass
, "attributor",
3072 "Deduce and propagate attributes", false, false)
3073 INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass
, "attributor-cgscc",
3074 "Deduce and propagate attributes (CGSCC pass)", false,
3076 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass
)
3077 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass
)
3078 INITIALIZE_PASS_END(AttributorCGSCCLegacyPass
, "attributor-cgscc",
3079 "Deduce and propagate attributes (CGSCC pass)", false,