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/ArrayRef.h"
19 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/CallGraph.h"
25 #include "llvm/Analysis/CallGraphSCCPass.h"
26 #include "llvm/Analysis/InlineCost.h"
27 #include "llvm/Analysis/MemoryBuiltins.h"
28 #include "llvm/Analysis/MustExecute.h"
29 #include "llvm/IR/AttributeMask.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/Constant.h"
32 #include "llvm/IR/ConstantFold.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/GlobalValue.h"
36 #include "llvm/IR/GlobalVariable.h"
37 #include "llvm/IR/Instruction.h"
38 #include "llvm/IR/Instructions.h"
39 #include "llvm/IR/IntrinsicInst.h"
40 #include "llvm/IR/LLVMContext.h"
41 #include "llvm/IR/ValueHandle.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/DebugCounter.h"
46 #include "llvm/Support/FileSystem.h"
47 #include "llvm/Support/GraphWriter.h"
48 #include "llvm/Support/ModRef.h"
49 #include "llvm/Support/raw_ostream.h"
50 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
51 #include "llvm/Transforms/Utils/Cloning.h"
52 #include "llvm/Transforms/Utils/Local.h"
56 #ifdef EXPENSIVE_CHECKS
57 #include "llvm/IR/Verifier.h"
66 #define DEBUG_TYPE "attributor"
67 #define VERBOSE_DEBUG_TYPE DEBUG_TYPE "-verbose"
69 DEBUG_COUNTER(ManifestDBGCounter
, "attributor-manifest",
70 "Determine what attributes are manifested in the IR");
72 STATISTIC(NumFnDeleted
, "Number of function deleted");
73 STATISTIC(NumFnWithExactDefinition
,
74 "Number of functions with exact definitions");
75 STATISTIC(NumFnWithoutExactDefinition
,
76 "Number of functions without exact definitions");
77 STATISTIC(NumFnShallowWrappersCreated
, "Number of shallow wrappers created");
78 STATISTIC(NumAttributesTimedOut
,
79 "Number of abstract attributes timed out before fixpoint");
80 STATISTIC(NumAttributesValidFixpoint
,
81 "Number of abstract attributes in a valid fixpoint state");
82 STATISTIC(NumAttributesManifested
,
83 "Number of abstract attributes manifested in IR");
85 // TODO: Determine a good default value.
87 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
88 // (when run with the first 5 abstract attributes). The results also indicate
89 // that we never reach 32 iterations but always find a fixpoint sooner.
91 // This will become more evolved once we perform two interleaved fixpoint
92 // iterations: bottom-up and top-down.
93 static cl::opt
<unsigned>
94 SetFixpointIterations("attributor-max-iterations", cl::Hidden
,
95 cl::desc("Maximal number of fixpoint iterations."),
98 static cl::opt
<unsigned>
99 MaxSpecializationPerCB("attributor-max-specializations-per-call-base",
101 cl::desc("Maximal number of callees specialized for "
103 cl::init(UINT32_MAX
));
105 static cl::opt
<unsigned, true> MaxInitializationChainLengthX(
106 "attributor-max-initialization-chain-length", cl::Hidden
,
108 "Maximal number of chained initializations (to avoid stack overflows)"),
109 cl::location(MaxInitializationChainLength
), cl::init(1024));
110 unsigned llvm::MaxInitializationChainLength
;
112 static cl::opt
<bool> AnnotateDeclarationCallSites(
113 "attributor-annotate-decl-cs", cl::Hidden
,
114 cl::desc("Annotate call sites of function declarations."), cl::init(false));
116 static cl::opt
<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
117 cl::init(true), cl::Hidden
);
120 AllowShallowWrappers("attributor-allow-shallow-wrappers", cl::Hidden
,
121 cl::desc("Allow the Attributor to create shallow "
122 "wrappers for non-exact definitions."),
126 AllowDeepWrapper("attributor-allow-deep-wrappers", cl::Hidden
,
127 cl::desc("Allow the Attributor to use IP information "
128 "derived from non-exact functions via cloning"),
131 // These options can only used for debug builds.
133 static cl::list
<std::string
>
134 SeedAllowList("attributor-seed-allow-list", cl::Hidden
,
135 cl::desc("Comma seperated list of attribute names that are "
136 "allowed to be seeded."),
139 static cl::list
<std::string
> FunctionSeedAllowList(
140 "attributor-function-seed-allow-list", cl::Hidden
,
141 cl::desc("Comma seperated list of function names that are "
142 "allowed to be seeded."),
147 DumpDepGraph("attributor-dump-dep-graph", cl::Hidden
,
148 cl::desc("Dump the dependency graph to dot files."),
151 static cl::opt
<std::string
> DepGraphDotFileNamePrefix(
152 "attributor-depgraph-dot-filename-prefix", cl::Hidden
,
153 cl::desc("The prefix used for the CallGraph dot file names."));
155 static cl::opt
<bool> ViewDepGraph("attributor-view-dep-graph", cl::Hidden
,
156 cl::desc("View the dependency graph."),
159 static cl::opt
<bool> PrintDependencies("attributor-print-dep", cl::Hidden
,
160 cl::desc("Print attribute dependencies"),
163 static cl::opt
<bool> EnableCallSiteSpecific(
164 "attributor-enable-call-site-specific-deduction", cl::Hidden
,
165 cl::desc("Allow the Attributor to do call site specific analysis"),
169 PrintCallGraph("attributor-print-call-graph", cl::Hidden
,
170 cl::desc("Print Attributor's internal call graph"),
173 static cl::opt
<bool> SimplifyAllLoads("attributor-simplify-all-loads",
175 cl::desc("Try to simplify all loads."),
178 static cl::opt
<bool> CloseWorldAssumption(
179 "attributor-assume-closed-world", cl::Hidden
,
180 cl::desc("Should a closed world be assumed, or not. Default if not set."));
182 /// Logic operators for the change status enum class.
185 ChangeStatus
llvm::operator|(ChangeStatus L
, ChangeStatus R
) {
186 return L
== ChangeStatus::CHANGED
? L
: R
;
188 ChangeStatus
&llvm::operator|=(ChangeStatus
&L
, ChangeStatus R
) {
192 ChangeStatus
llvm::operator&(ChangeStatus L
, ChangeStatus R
) {
193 return L
== ChangeStatus::UNCHANGED
? L
: R
;
195 ChangeStatus
&llvm::operator&=(ChangeStatus
&L
, ChangeStatus R
) {
201 bool AA::isGPU(const Module
&M
) {
202 Triple
T(M
.getTargetTriple());
203 return T
.isAMDGPU() || T
.isNVPTX();
206 bool AA::isNoSyncInst(Attributor
&A
, const Instruction
&I
,
207 const AbstractAttribute
&QueryingAA
) {
208 // We are looking for volatile instructions or non-relaxed atomics.
209 if (const auto *CB
= dyn_cast
<CallBase
>(&I
)) {
210 if (CB
->hasFnAttr(Attribute::NoSync
))
213 // Non-convergent and readnone imply nosync.
214 if (!CB
->isConvergent() && !CB
->mayReadOrWriteMemory())
217 if (AANoSync::isNoSyncIntrinsic(&I
))
221 return AA::hasAssumedIRAttr
<Attribute::NoSync
>(
222 A
, &QueryingAA
, IRPosition::callsite_function(*CB
),
223 DepClassTy::OPTIONAL
, IsKnownNoSync
);
226 if (!I
.mayReadOrWriteMemory())
229 return !I
.isVolatile() && !AANoSync::isNonRelaxedAtomic(&I
);
232 bool AA::isDynamicallyUnique(Attributor
&A
, const AbstractAttribute
&QueryingAA
,
233 const Value
&V
, bool ForAnalysisOnly
) {
234 // TODO: See the AAInstanceInfo class comment.
235 if (!ForAnalysisOnly
)
237 auto *InstanceInfoAA
= A
.getAAFor
<AAInstanceInfo
>(
238 QueryingAA
, IRPosition::value(V
), DepClassTy::OPTIONAL
);
239 return InstanceInfoAA
&& InstanceInfoAA
->isAssumedUniqueForAnalysis();
243 AA::getInitialValueForObj(Attributor
&A
, const AbstractAttribute
&QueryingAA
,
244 Value
&Obj
, Type
&Ty
, const TargetLibraryInfo
*TLI
,
245 const DataLayout
&DL
, AA::RangeTy
*RangePtr
) {
246 if (isa
<AllocaInst
>(Obj
))
247 return UndefValue::get(&Ty
);
248 if (Constant
*Init
= getInitialValueOfAllocation(&Obj
, TLI
, &Ty
))
250 auto *GV
= dyn_cast
<GlobalVariable
>(&Obj
);
254 bool UsedAssumedInformation
= false;
255 Constant
*Initializer
= nullptr;
256 if (A
.hasGlobalVariableSimplificationCallback(*GV
)) {
257 auto AssumedGV
= A
.getAssumedInitializerFromCallBack(
258 *GV
, &QueryingAA
, UsedAssumedInformation
);
259 Initializer
= *AssumedGV
;
263 if (!GV
->hasLocalLinkage() &&
264 (GV
->isInterposable() || !(GV
->isConstant() && GV
->hasInitializer())))
266 if (!GV
->hasInitializer())
267 return UndefValue::get(&Ty
);
270 Initializer
= GV
->getInitializer();
273 if (RangePtr
&& !RangePtr
->offsetOrSizeAreUnknown()) {
274 APInt Offset
= APInt(64, RangePtr
->Offset
);
275 return ConstantFoldLoadFromConst(Initializer
, &Ty
, Offset
, DL
);
278 return ConstantFoldLoadFromUniformValue(Initializer
, &Ty
);
281 bool AA::isValidInScope(const Value
&V
, const Function
*Scope
) {
282 if (isa
<Constant
>(V
))
284 if (auto *I
= dyn_cast
<Instruction
>(&V
))
285 return I
->getFunction() == Scope
;
286 if (auto *A
= dyn_cast
<Argument
>(&V
))
287 return A
->getParent() == Scope
;
291 bool AA::isValidAtPosition(const AA::ValueAndContext
&VAC
,
292 InformationCache
&InfoCache
) {
293 if (isa
<Constant
>(VAC
.getValue()) || VAC
.getValue() == VAC
.getCtxI())
295 const Function
*Scope
= nullptr;
296 const Instruction
*CtxI
= VAC
.getCtxI();
298 Scope
= CtxI
->getFunction();
299 if (auto *A
= dyn_cast
<Argument
>(VAC
.getValue()))
300 return A
->getParent() == Scope
;
301 if (auto *I
= dyn_cast
<Instruction
>(VAC
.getValue())) {
302 if (I
->getFunction() == Scope
) {
303 if (const DominatorTree
*DT
=
304 InfoCache
.getAnalysisResultForFunction
<DominatorTreeAnalysis
>(
306 return DT
->dominates(I
, CtxI
);
307 // Local dominance check mostly for the old PM passes.
308 if (CtxI
&& I
->getParent() == CtxI
->getParent())
310 make_range(I
->getIterator(), I
->getParent()->end()),
311 [&](const Instruction
&AfterI
) { return &AfterI
== CtxI
; });
317 Value
*AA::getWithType(Value
&V
, Type
&Ty
) {
318 if (V
.getType() == &Ty
)
320 if (isa
<PoisonValue
>(V
))
321 return PoisonValue::get(&Ty
);
322 if (isa
<UndefValue
>(V
))
323 return UndefValue::get(&Ty
);
324 if (auto *C
= dyn_cast
<Constant
>(&V
)) {
325 if (C
->isNullValue())
326 return Constant::getNullValue(&Ty
);
327 if (C
->getType()->isPointerTy() && Ty
.isPointerTy())
328 return ConstantExpr::getPointerCast(C
, &Ty
);
329 if (C
->getType()->getPrimitiveSizeInBits() >= Ty
.getPrimitiveSizeInBits()) {
330 if (C
->getType()->isIntegerTy() && Ty
.isIntegerTy())
331 return ConstantExpr::getTrunc(C
, &Ty
, /* OnlyIfReduced */ true);
332 if (C
->getType()->isFloatingPointTy() && Ty
.isFloatingPointTy())
333 return ConstantFoldCastInstruction(Instruction::FPTrunc
, C
, &Ty
);
339 std::optional
<Value
*>
340 AA::combineOptionalValuesInAAValueLatice(const std::optional
<Value
*> &A
,
341 const std::optional
<Value
*> &B
,
350 return Ty
? getWithType(**B
, *Ty
) : nullptr;
354 Ty
= (*A
)->getType();
355 if (isa_and_nonnull
<UndefValue
>(*A
))
356 return getWithType(**B
, *Ty
);
357 if (isa
<UndefValue
>(*B
))
359 if (*A
&& *B
&& *A
== getWithType(**B
, *Ty
))
364 template <bool IsLoad
, typename Ty
>
365 static bool getPotentialCopiesOfMemoryValue(
366 Attributor
&A
, Ty
&I
, SmallSetVector
<Value
*, 4> &PotentialCopies
,
367 SmallSetVector
<Instruction
*, 4> *PotentialValueOrigins
,
368 const AbstractAttribute
&QueryingAA
, bool &UsedAssumedInformation
,
370 LLVM_DEBUG(dbgs() << "Trying to determine the potential copies of " << I
371 << " (only exact: " << OnlyExact
<< ")\n";);
373 Value
&Ptr
= *I
.getPointerOperand();
374 // Containers to remember the pointer infos and new copies while we are not
375 // sure that we can find all of them. If we abort we want to avoid spurious
376 // dependences and potential copies in the provided container.
377 SmallVector
<const AAPointerInfo
*> PIs
;
378 SmallSetVector
<Value
*, 8> NewCopies
;
379 SmallSetVector
<Instruction
*, 8> NewCopyOrigins
;
382 A
.getInfoCache().getTargetLibraryInfoForFunction(*I
.getFunction());
384 auto Pred
= [&](Value
&Obj
) {
385 LLVM_DEBUG(dbgs() << "Visit underlying object " << Obj
<< "\n");
386 if (isa
<UndefValue
>(&Obj
))
388 if (isa
<ConstantPointerNull
>(&Obj
)) {
389 // A null pointer access can be undefined but any offset from null may
390 // be OK. We do not try to optimize the latter.
391 if (!NullPointerIsDefined(I
.getFunction(),
392 Ptr
.getType()->getPointerAddressSpace()) &&
393 A
.getAssumedSimplified(Ptr
, QueryingAA
, UsedAssumedInformation
,
394 AA::Interprocedural
) == &Obj
)
397 dbgs() << "Underlying object is a valid nullptr, giving up.\n";);
400 // TODO: Use assumed noalias return.
401 if (!isa
<AllocaInst
>(&Obj
) && !isa
<GlobalVariable
>(&Obj
) &&
402 !(IsLoad
? isAllocationFn(&Obj
, TLI
) : isNoAliasCall(&Obj
))) {
403 LLVM_DEBUG(dbgs() << "Underlying object is not supported yet: " << Obj
407 if (auto *GV
= dyn_cast
<GlobalVariable
>(&Obj
))
408 if (!GV
->hasLocalLinkage() &&
409 !(GV
->isConstant() && GV
->hasInitializer())) {
410 LLVM_DEBUG(dbgs() << "Underlying object is global with external "
411 "linkage, not supported yet: "
416 bool NullOnly
= true;
417 bool NullRequired
= false;
418 auto CheckForNullOnlyAndUndef
= [&](std::optional
<Value
*> V
,
420 if (!V
|| *V
== nullptr)
422 else if (isa
<UndefValue
>(*V
))
424 else if (isa
<Constant
>(*V
) && cast
<Constant
>(*V
)->isNullValue())
425 NullRequired
= !IsExact
;
430 auto AdjustWrittenValueType
= [&](const AAPointerInfo::Access
&Acc
,
432 Value
*AdjV
= AA::getWithType(V
, *I
.getType());
434 LLVM_DEBUG(dbgs() << "Underlying object written but stored value "
435 "cannot be converted to read type: "
436 << *Acc
.getRemoteInst() << " : " << *I
.getType()
442 auto SkipCB
= [&](const AAPointerInfo::Access
&Acc
) {
443 if ((IsLoad
&& !Acc
.isWriteOrAssumption()) || (!IsLoad
&& !Acc
.isRead()))
446 if (Acc
.isWrittenValueYetUndetermined())
448 if (PotentialValueOrigins
&& !isa
<AssumeInst
>(Acc
.getRemoteInst()))
450 if (!Acc
.isWrittenValueUnknown())
451 if (Value
*V
= AdjustWrittenValueType(Acc
, *Acc
.getWrittenValue()))
452 if (NewCopies
.count(V
)) {
453 NewCopyOrigins
.insert(Acc
.getRemoteInst());
456 if (auto *SI
= dyn_cast
<StoreInst
>(Acc
.getRemoteInst()))
457 if (Value
*V
= AdjustWrittenValueType(Acc
, *SI
->getValueOperand()))
458 if (NewCopies
.count(V
)) {
459 NewCopyOrigins
.insert(Acc
.getRemoteInst());
466 auto CheckAccess
= [&](const AAPointerInfo::Access
&Acc
, bool IsExact
) {
467 if ((IsLoad
&& !Acc
.isWriteOrAssumption()) || (!IsLoad
&& !Acc
.isRead()))
469 if (IsLoad
&& Acc
.isWrittenValueYetUndetermined())
471 CheckForNullOnlyAndUndef(Acc
.getContent(), IsExact
);
472 if (OnlyExact
&& !IsExact
&& !NullOnly
&&
473 !isa_and_nonnull
<UndefValue
>(Acc
.getWrittenValue())) {
474 LLVM_DEBUG(dbgs() << "Non exact access " << *Acc
.getRemoteInst()
478 if (NullRequired
&& !NullOnly
) {
479 LLVM_DEBUG(dbgs() << "Required all `null` accesses due to non exact "
480 "one, however found non-null one: "
481 << *Acc
.getRemoteInst() << ", abort!\n");
485 assert(isa
<LoadInst
>(I
) && "Expected load or store instruction only!");
486 if (!Acc
.isWrittenValueUnknown()) {
487 Value
*V
= AdjustWrittenValueType(Acc
, *Acc
.getWrittenValue());
491 if (PotentialValueOrigins
)
492 NewCopyOrigins
.insert(Acc
.getRemoteInst());
495 auto *SI
= dyn_cast
<StoreInst
>(Acc
.getRemoteInst());
497 LLVM_DEBUG(dbgs() << "Underlying object written through a non-store "
498 "instruction not supported yet: "
499 << *Acc
.getRemoteInst() << "\n";);
502 Value
*V
= AdjustWrittenValueType(Acc
, *SI
->getValueOperand());
506 if (PotentialValueOrigins
)
507 NewCopyOrigins
.insert(SI
);
509 assert(isa
<StoreInst
>(I
) && "Expected load or store instruction only!");
510 auto *LI
= dyn_cast
<LoadInst
>(Acc
.getRemoteInst());
511 if (!LI
&& OnlyExact
) {
512 LLVM_DEBUG(dbgs() << "Underlying object read through a non-load "
513 "instruction not supported yet: "
514 << *Acc
.getRemoteInst() << "\n";);
517 NewCopies
.insert(Acc
.getRemoteInst());
522 // If the value has been written to we don't need the initial value of the
524 bool HasBeenWrittenTo
= false;
527 auto *PI
= A
.getAAFor
<AAPointerInfo
>(QueryingAA
, IRPosition::value(Obj
),
529 if (!PI
|| !PI
->forallInterferingAccesses(
531 /* FindInterferingWrites */ IsLoad
,
532 /* FindInterferingReads */ !IsLoad
, CheckAccess
,
533 HasBeenWrittenTo
, Range
, SkipCB
)) {
536 << "Failed to verify all interfering accesses for underlying object: "
541 if (IsLoad
&& !HasBeenWrittenTo
&& !Range
.isUnassigned()) {
542 const DataLayout
&DL
= A
.getDataLayout();
543 Value
*InitialValue
= AA::getInitialValueForObj(
544 A
, QueryingAA
, Obj
, *I
.getType(), TLI
, DL
, &Range
);
546 LLVM_DEBUG(dbgs() << "Could not determine required initial value of "
547 "underlying object, abort!\n");
550 CheckForNullOnlyAndUndef(InitialValue
, /* IsExact */ true);
551 if (NullRequired
&& !NullOnly
) {
552 LLVM_DEBUG(dbgs() << "Non exact access but initial value that is not "
553 "null or undef, abort!\n");
557 NewCopies
.insert(InitialValue
);
558 if (PotentialValueOrigins
)
559 NewCopyOrigins
.insert(nullptr);
567 const auto *AAUO
= A
.getAAFor
<AAUnderlyingObjects
>(
568 QueryingAA
, IRPosition::value(Ptr
), DepClassTy::OPTIONAL
);
569 if (!AAUO
|| !AAUO
->forallUnderlyingObjects(Pred
)) {
571 dbgs() << "Underlying objects stored into could not be determined\n";);
575 // Only if we were successful collection all potential copies we record
576 // dependences (on non-fix AAPointerInfo AAs). We also only then modify the
577 // given PotentialCopies container.
578 for (const auto *PI
: PIs
) {
579 if (!PI
->getState().isAtFixpoint())
580 UsedAssumedInformation
= true;
581 A
.recordDependence(*PI
, QueryingAA
, DepClassTy::OPTIONAL
);
583 PotentialCopies
.insert(NewCopies
.begin(), NewCopies
.end());
584 if (PotentialValueOrigins
)
585 PotentialValueOrigins
->insert(NewCopyOrigins
.begin(), NewCopyOrigins
.end());
590 bool AA::getPotentiallyLoadedValues(
591 Attributor
&A
, LoadInst
&LI
, SmallSetVector
<Value
*, 4> &PotentialValues
,
592 SmallSetVector
<Instruction
*, 4> &PotentialValueOrigins
,
593 const AbstractAttribute
&QueryingAA
, bool &UsedAssumedInformation
,
595 return getPotentialCopiesOfMemoryValue
</* IsLoad */ true>(
596 A
, LI
, PotentialValues
, &PotentialValueOrigins
, QueryingAA
,
597 UsedAssumedInformation
, OnlyExact
);
600 bool AA::getPotentialCopiesOfStoredValue(
601 Attributor
&A
, StoreInst
&SI
, SmallSetVector
<Value
*, 4> &PotentialCopies
,
602 const AbstractAttribute
&QueryingAA
, bool &UsedAssumedInformation
,
604 return getPotentialCopiesOfMemoryValue
</* IsLoad */ false>(
605 A
, SI
, PotentialCopies
, nullptr, QueryingAA
, UsedAssumedInformation
,
609 static bool isAssumedReadOnlyOrReadNone(Attributor
&A
, const IRPosition
&IRP
,
610 const AbstractAttribute
&QueryingAA
,
611 bool RequireReadNone
, bool &IsKnown
) {
612 if (RequireReadNone
) {
613 if (AA::hasAssumedIRAttr
<Attribute::ReadNone
>(
614 A
, &QueryingAA
, IRP
, DepClassTy::OPTIONAL
, IsKnown
,
615 /* IgnoreSubsumingPositions */ true))
617 } else if (AA::hasAssumedIRAttr
<Attribute::ReadOnly
>(
618 A
, &QueryingAA
, IRP
, DepClassTy::OPTIONAL
, IsKnown
,
619 /* IgnoreSubsumingPositions */ true))
622 IRPosition::Kind Kind
= IRP
.getPositionKind();
623 if (Kind
== IRPosition::IRP_FUNCTION
|| Kind
== IRPosition::IRP_CALL_SITE
) {
624 const auto *MemLocAA
=
625 A
.getAAFor
<AAMemoryLocation
>(QueryingAA
, IRP
, DepClassTy::NONE
);
626 if (MemLocAA
&& MemLocAA
->isAssumedReadNone()) {
627 IsKnown
= MemLocAA
->isKnownReadNone();
629 A
.recordDependence(*MemLocAA
, QueryingAA
, DepClassTy::OPTIONAL
);
634 const auto *MemBehaviorAA
=
635 A
.getAAFor
<AAMemoryBehavior
>(QueryingAA
, IRP
, DepClassTy::NONE
);
637 (MemBehaviorAA
->isAssumedReadNone() ||
638 (!RequireReadNone
&& MemBehaviorAA
->isAssumedReadOnly()))) {
639 IsKnown
= RequireReadNone
? MemBehaviorAA
->isKnownReadNone()
640 : MemBehaviorAA
->isKnownReadOnly();
642 A
.recordDependence(*MemBehaviorAA
, QueryingAA
, DepClassTy::OPTIONAL
);
649 bool AA::isAssumedReadOnly(Attributor
&A
, const IRPosition
&IRP
,
650 const AbstractAttribute
&QueryingAA
, bool &IsKnown
) {
651 return isAssumedReadOnlyOrReadNone(A
, IRP
, QueryingAA
,
652 /* RequireReadNone */ false, IsKnown
);
654 bool AA::isAssumedReadNone(Attributor
&A
, const IRPosition
&IRP
,
655 const AbstractAttribute
&QueryingAA
, bool &IsKnown
) {
656 return isAssumedReadOnlyOrReadNone(A
, IRP
, QueryingAA
,
657 /* RequireReadNone */ true, IsKnown
);
661 isPotentiallyReachable(Attributor
&A
, const Instruction
&FromI
,
662 const Instruction
*ToI
, const Function
&ToFn
,
663 const AbstractAttribute
&QueryingAA
,
664 const AA::InstExclusionSetTy
*ExclusionSet
,
665 std::function
<bool(const Function
&F
)> GoBackwardsCB
) {
666 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
, {
667 dbgs() << "[AA] isPotentiallyReachable @" << ToFn
.getName() << " from "
668 << FromI
<< " [GBCB: " << bool(GoBackwardsCB
) << "][#ExS: "
669 << (ExclusionSet
? std::to_string(ExclusionSet
->size()) : "none")
672 for (auto *ES
: *ExclusionSet
)
673 dbgs() << *ES
<< "\n";
676 // We know kernels (generally) cannot be called from within the module. Thus,
677 // for reachability we would need to step back from a kernel which would allow
678 // us to reach anything anyway. Even if a kernel is invoked from another
679 // kernel, values like allocas and shared memory are not accessible. We
680 // implicitly check for this situation to avoid costly lookups.
681 if (GoBackwardsCB
&& &ToFn
!= FromI
.getFunction() &&
682 !GoBackwardsCB(*FromI
.getFunction()) && ToFn
.hasFnAttribute("kernel") &&
683 FromI
.getFunction()->hasFnAttribute("kernel")) {
684 LLVM_DEBUG(dbgs() << "[AA] assume kernel cannot be reached from within the "
685 "module; success\n";);
689 // If we can go arbitrarily backwards we will eventually reach an entry point
690 // that can reach ToI. Only if a set of blocks through which we cannot go is
691 // provided, or once we track internal functions not accessible from the
692 // outside, it makes sense to perform backwards analysis in the absence of a
694 if (!GoBackwardsCB
&& !ExclusionSet
) {
695 LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn
.getName() << " from " << FromI
696 << " is not checked backwards and does not have an "
697 "exclusion set, abort\n");
701 SmallPtrSet
<const Instruction
*, 8> Visited
;
702 SmallVector
<const Instruction
*> Worklist
;
703 Worklist
.push_back(&FromI
);
705 while (!Worklist
.empty()) {
706 const Instruction
*CurFromI
= Worklist
.pop_back_val();
707 if (!Visited
.insert(CurFromI
).second
)
710 const Function
*FromFn
= CurFromI
->getFunction();
711 if (FromFn
== &ToFn
) {
714 LLVM_DEBUG(dbgs() << "[AA] check " << *ToI
<< " from " << *CurFromI
715 << " intraprocedurally\n");
716 const auto *ReachabilityAA
= A
.getAAFor
<AAIntraFnReachability
>(
717 QueryingAA
, IRPosition::function(ToFn
), DepClassTy::OPTIONAL
);
718 bool Result
= !ReachabilityAA
|| ReachabilityAA
->isAssumedReachable(
719 A
, *CurFromI
, *ToI
, ExclusionSet
);
720 LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI
<< " "
721 << (Result
? "can potentially " : "cannot ") << "reach "
722 << *ToI
<< " [Intra]\n");
728 if (!ToFn
.isDeclaration() && ToI
) {
729 const auto *ToReachabilityAA
= A
.getAAFor
<AAIntraFnReachability
>(
730 QueryingAA
, IRPosition::function(ToFn
), DepClassTy::OPTIONAL
);
731 const Instruction
&EntryI
= ToFn
.getEntryBlock().front();
732 Result
= !ToReachabilityAA
|| ToReachabilityAA
->isAssumedReachable(
733 A
, EntryI
, *ToI
, ExclusionSet
);
734 LLVM_DEBUG(dbgs() << "[AA] Entry " << EntryI
<< " of @" << ToFn
.getName()
735 << " " << (Result
? "can potentially " : "cannot ")
736 << "reach @" << *ToI
<< " [ToFn]\n");
740 // The entry of the ToFn can reach the instruction ToI. If the current
741 // instruction is already known to reach the ToFn.
742 const auto *FnReachabilityAA
= A
.getAAFor
<AAInterFnReachability
>(
743 QueryingAA
, IRPosition::function(*FromFn
), DepClassTy::OPTIONAL
);
744 Result
= !FnReachabilityAA
|| FnReachabilityAA
->instructionCanReach(
745 A
, *CurFromI
, ToFn
, ExclusionSet
);
746 LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI
<< " in @" << FromFn
->getName()
747 << " " << (Result
? "can potentially " : "cannot ")
748 << "reach @" << ToFn
.getName() << " [FromFn]\n");
753 // TODO: Check assumed nounwind.
754 const auto *ReachabilityAA
= A
.getAAFor
<AAIntraFnReachability
>(
755 QueryingAA
, IRPosition::function(*FromFn
), DepClassTy::OPTIONAL
);
756 auto ReturnInstCB
= [&](Instruction
&Ret
) {
757 bool Result
= !ReachabilityAA
|| ReachabilityAA
->isAssumedReachable(
758 A
, *CurFromI
, Ret
, ExclusionSet
);
759 LLVM_DEBUG(dbgs() << "[AA][Ret] " << *CurFromI
<< " "
760 << (Result
? "can potentially " : "cannot ") << "reach "
761 << Ret
<< " [Intra]\n");
765 // Check if we can reach returns.
766 bool UsedAssumedInformation
= false;
767 if (A
.checkForAllInstructions(ReturnInstCB
, FromFn
, &QueryingAA
,
768 {Instruction::Ret
}, UsedAssumedInformation
)) {
769 LLVM_DEBUG(dbgs() << "[AA] No return is reachable, done\n");
773 if (!GoBackwardsCB
) {
774 LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn
.getName() << " from " << FromI
775 << " is not checked backwards, abort\n");
779 // If we do not go backwards from the FromFn we are done here and so far we
780 // could not find a way to reach ToFn/ToI.
781 if (!GoBackwardsCB(*FromFn
))
784 LLVM_DEBUG(dbgs() << "Stepping backwards to the call sites of @"
785 << FromFn
->getName() << "\n");
787 auto CheckCallSite
= [&](AbstractCallSite ACS
) {
788 CallBase
*CB
= ACS
.getInstruction();
792 if (isa
<InvokeInst
>(CB
))
795 Instruction
*Inst
= CB
->getNextNonDebugInstruction();
796 Worklist
.push_back(Inst
);
800 Result
= !A
.checkForAllCallSites(CheckCallSite
, *FromFn
,
801 /* RequireAllCallSites */ true,
802 &QueryingAA
, UsedAssumedInformation
);
804 LLVM_DEBUG(dbgs() << "[AA] stepping back to call sites from " << *CurFromI
805 << " in @" << FromFn
->getName()
806 << " failed, give up\n");
810 LLVM_DEBUG(dbgs() << "[AA] stepped back to call sites from " << *CurFromI
811 << " in @" << FromFn
->getName()
812 << " worklist size is: " << Worklist
.size() << "\n");
817 bool AA::isPotentiallyReachable(
818 Attributor
&A
, const Instruction
&FromI
, const Instruction
&ToI
,
819 const AbstractAttribute
&QueryingAA
,
820 const AA::InstExclusionSetTy
*ExclusionSet
,
821 std::function
<bool(const Function
&F
)> GoBackwardsCB
) {
822 const Function
*ToFn
= ToI
.getFunction();
823 return ::isPotentiallyReachable(A
, FromI
, &ToI
, *ToFn
, QueryingAA
,
824 ExclusionSet
, GoBackwardsCB
);
827 bool AA::isPotentiallyReachable(
828 Attributor
&A
, const Instruction
&FromI
, const Function
&ToFn
,
829 const AbstractAttribute
&QueryingAA
,
830 const AA::InstExclusionSetTy
*ExclusionSet
,
831 std::function
<bool(const Function
&F
)> GoBackwardsCB
) {
832 return ::isPotentiallyReachable(A
, FromI
, /* ToI */ nullptr, ToFn
, QueryingAA
,
833 ExclusionSet
, GoBackwardsCB
);
836 bool AA::isAssumedThreadLocalObject(Attributor
&A
, Value
&Obj
,
837 const AbstractAttribute
&QueryingAA
) {
838 if (isa
<UndefValue
>(Obj
))
840 if (isa
<AllocaInst
>(Obj
)) {
841 InformationCache
&InfoCache
= A
.getInfoCache();
842 if (!InfoCache
.stackIsAccessibleByOtherThreads()) {
844 dbgs() << "[AA] Object '" << Obj
845 << "' is thread local; stack objects are thread local.\n");
848 bool IsKnownNoCapture
;
849 bool IsAssumedNoCapture
= AA::hasAssumedIRAttr
<Attribute::NoCapture
>(
850 A
, &QueryingAA
, IRPosition::value(Obj
), DepClassTy::OPTIONAL
,
852 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
<< "' is "
853 << (IsAssumedNoCapture
? "" : "not") << " thread local; "
854 << (IsAssumedNoCapture
? "non-" : "")
855 << "captured stack object.\n");
856 return IsAssumedNoCapture
;
858 if (auto *GV
= dyn_cast
<GlobalVariable
>(&Obj
)) {
859 if (GV
->isConstant()) {
860 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
861 << "' is thread local; constant global\n");
864 if (GV
->isThreadLocal()) {
865 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
866 << "' is thread local; thread local global\n");
871 if (A
.getInfoCache().targetIsGPU()) {
872 if (Obj
.getType()->getPointerAddressSpace() ==
873 (int)AA::GPUAddressSpace::Local
) {
874 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
875 << "' is thread local; GPU local memory\n");
878 if (Obj
.getType()->getPointerAddressSpace() ==
879 (int)AA::GPUAddressSpace::Constant
) {
880 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
881 << "' is thread local; GPU constant memory\n");
886 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
<< "' is not thread local\n");
890 bool AA::isPotentiallyAffectedByBarrier(Attributor
&A
, const Instruction
&I
,
891 const AbstractAttribute
&QueryingAA
) {
892 if (!I
.mayHaveSideEffects() && !I
.mayReadFromMemory())
895 SmallSetVector
<const Value
*, 8> Ptrs
;
897 auto AddLocationPtr
= [&](std::optional
<MemoryLocation
> Loc
) {
898 if (!Loc
|| !Loc
->Ptr
) {
900 dbgs() << "[AA] Access to unknown location; -> requires barriers\n");
903 Ptrs
.insert(Loc
->Ptr
);
907 if (const MemIntrinsic
*MI
= dyn_cast
<MemIntrinsic
>(&I
)) {
908 if (!AddLocationPtr(MemoryLocation::getForDest(MI
)))
910 if (const MemTransferInst
*MTI
= dyn_cast
<MemTransferInst
>(&I
))
911 if (!AddLocationPtr(MemoryLocation::getForSource(MTI
)))
913 } else if (!AddLocationPtr(MemoryLocation::getOrNone(&I
)))
916 return isPotentiallyAffectedByBarrier(A
, Ptrs
.getArrayRef(), QueryingAA
, &I
);
919 bool AA::isPotentiallyAffectedByBarrier(Attributor
&A
,
920 ArrayRef
<const Value
*> Ptrs
,
921 const AbstractAttribute
&QueryingAA
,
922 const Instruction
*CtxI
) {
923 for (const Value
*Ptr
: Ptrs
) {
925 LLVM_DEBUG(dbgs() << "[AA] nullptr; -> requires barriers\n");
929 auto Pred
= [&](Value
&Obj
) {
930 if (AA::isAssumedThreadLocalObject(A
, Obj
, QueryingAA
))
932 LLVM_DEBUG(dbgs() << "[AA] Access to '" << Obj
<< "' via '" << *Ptr
933 << "'; -> requires barrier\n");
937 const auto *UnderlyingObjsAA
= A
.getAAFor
<AAUnderlyingObjects
>(
938 QueryingAA
, IRPosition::value(*Ptr
), DepClassTy::OPTIONAL
);
939 if (!UnderlyingObjsAA
|| !UnderlyingObjsAA
->forallUnderlyingObjects(Pred
))
945 /// Return true if \p New is equal or worse than \p Old.
946 static bool isEqualOrWorse(const Attribute
&New
, const Attribute
&Old
) {
947 if (!Old
.isIntAttribute())
950 return Old
.getValueAsInt() >= New
.getValueAsInt();
953 /// Return true if the information provided by \p Attr was added to the
954 /// attribute set \p AttrSet. This is only the case if it was not already
955 /// present in \p AttrSet.
956 static bool addIfNotExistent(LLVMContext
&Ctx
, const Attribute
&Attr
,
957 AttributeSet AttrSet
, bool ForceReplace
,
960 if (Attr
.isEnumAttribute()) {
961 Attribute::AttrKind Kind
= Attr
.getKindAsEnum();
962 if (AttrSet
.hasAttribute(Kind
))
964 AB
.addAttribute(Kind
);
967 if (Attr
.isStringAttribute()) {
968 StringRef Kind
= Attr
.getKindAsString();
969 if (AttrSet
.hasAttribute(Kind
)) {
973 AB
.addAttribute(Kind
, Attr
.getValueAsString());
976 if (Attr
.isIntAttribute()) {
977 Attribute::AttrKind Kind
= Attr
.getKindAsEnum();
978 if (!ForceReplace
&& Kind
== Attribute::Memory
) {
979 MemoryEffects ME
= Attr
.getMemoryEffects() & AttrSet
.getMemoryEffects();
980 if (ME
== AttrSet
.getMemoryEffects())
982 AB
.addMemoryAttr(ME
);
985 if (AttrSet
.hasAttribute(Kind
)) {
986 if (!ForceReplace
&& isEqualOrWorse(Attr
, AttrSet
.getAttribute(Kind
)))
989 AB
.addAttribute(Attr
);
993 llvm_unreachable("Expected enum or string attribute!");
996 Argument
*IRPosition::getAssociatedArgument() const {
997 if (getPositionKind() == IRP_ARGUMENT
)
998 return cast
<Argument
>(&getAnchorValue());
1000 // Not an Argument and no argument number means this is not a call site
1001 // argument, thus we cannot find a callback argument to return.
1002 int ArgNo
= getCallSiteArgNo();
1006 // Use abstract call sites to make the connection between the call site
1007 // values and the ones in callbacks. If a callback was found that makes use
1008 // of the underlying call site operand, we want the corresponding callback
1009 // callee argument and not the direct callee argument.
1010 std::optional
<Argument
*> CBCandidateArg
;
1011 SmallVector
<const Use
*, 4> CallbackUses
;
1012 const auto &CB
= cast
<CallBase
>(getAnchorValue());
1013 AbstractCallSite::getCallbackUses(CB
, CallbackUses
);
1014 for (const Use
*U
: CallbackUses
) {
1015 AbstractCallSite
ACS(U
);
1016 assert(ACS
&& ACS
.isCallbackCall());
1017 if (!ACS
.getCalledFunction())
1020 for (unsigned u
= 0, e
= ACS
.getNumArgOperands(); u
< e
; u
++) {
1022 // Test if the underlying call site operand is argument number u of the
1024 if (ACS
.getCallArgOperandNo(u
) != ArgNo
)
1027 assert(ACS
.getCalledFunction()->arg_size() > u
&&
1028 "ACS mapped into var-args arguments!");
1029 if (CBCandidateArg
) {
1030 CBCandidateArg
= nullptr;
1033 CBCandidateArg
= ACS
.getCalledFunction()->getArg(u
);
1037 // If we found a unique callback candidate argument, return it.
1038 if (CBCandidateArg
&& *CBCandidateArg
)
1039 return *CBCandidateArg
;
1041 // If no callbacks were found, or none used the underlying call site operand
1042 // exclusively, use the direct callee argument if available.
1043 auto *Callee
= dyn_cast_if_present
<Function
>(CB
.getCalledOperand());
1044 if (Callee
&& Callee
->arg_size() > unsigned(ArgNo
))
1045 return Callee
->getArg(ArgNo
);
1050 ChangeStatus
AbstractAttribute::update(Attributor
&A
) {
1051 ChangeStatus HasChanged
= ChangeStatus::UNCHANGED
;
1052 if (getState().isAtFixpoint())
1055 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
1057 HasChanged
= updateImpl(A
);
1059 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged
<< " " << *this
1065 Attributor::Attributor(SetVector
<Function
*> &Functions
,
1066 InformationCache
&InfoCache
,
1067 AttributorConfig Configuration
)
1068 : Allocator(InfoCache
.Allocator
), Functions(Functions
),
1069 InfoCache(InfoCache
), Configuration(Configuration
) {
1070 if (!isClosedWorldModule())
1072 for (Function
*Fn
: Functions
)
1073 if (Fn
->hasAddressTaken(/*PutOffender=*/nullptr,
1074 /*IgnoreCallbackUses=*/false,
1075 /*IgnoreAssumeLikeCalls=*/true,
1076 /*IgnoreLLVMUsed=*/true,
1077 /*IgnoreARCAttachedCall=*/false,
1078 /*IgnoreCastedDirectCall=*/true))
1079 InfoCache
.IndirectlyCallableFunctions
.push_back(Fn
);
1082 bool Attributor::getAttrsFromAssumes(const IRPosition
&IRP
,
1083 Attribute::AttrKind AK
,
1084 SmallVectorImpl
<Attribute
> &Attrs
) {
1085 assert(IRP
.getPositionKind() != IRPosition::IRP_INVALID
&&
1086 "Did expect a valid position!");
1087 MustBeExecutedContextExplorer
*Explorer
=
1088 getInfoCache().getMustBeExecutedContextExplorer();
1092 Value
&AssociatedValue
= IRP
.getAssociatedValue();
1094 const Assume2KnowledgeMap
&A2K
=
1095 getInfoCache().getKnowledgeMap().lookup({&AssociatedValue
, AK
});
1097 // Check if we found any potential assume use, if not we don't need to create
1098 // explorer iterators.
1102 LLVMContext
&Ctx
= AssociatedValue
.getContext();
1103 unsigned AttrsSize
= Attrs
.size();
1104 auto EIt
= Explorer
->begin(IRP
.getCtxI()),
1105 EEnd
= Explorer
->end(IRP
.getCtxI());
1106 for (const auto &It
: A2K
)
1107 if (Explorer
->findInContextOf(It
.first
, EIt
, EEnd
))
1108 Attrs
.push_back(Attribute::get(Ctx
, AK
, It
.second
.Max
));
1109 return AttrsSize
!= Attrs
.size();
1112 template <typename DescTy
>
1114 Attributor::updateAttrMap(const IRPosition
&IRP
, ArrayRef
<DescTy
> AttrDescs
,
1115 function_ref
<bool(const DescTy
&, AttributeSet
,
1116 AttributeMask
&, AttrBuilder
&)>
1118 if (AttrDescs
.empty())
1119 return ChangeStatus::UNCHANGED
;
1120 switch (IRP
.getPositionKind()) {
1121 case IRPosition::IRP_FLOAT
:
1122 case IRPosition::IRP_INVALID
:
1123 return ChangeStatus::UNCHANGED
;
1129 Value
*AttrListAnchor
= IRP
.getAttrListAnchor();
1130 auto It
= AttrsMap
.find(AttrListAnchor
);
1131 if (It
== AttrsMap
.end())
1132 AL
= IRP
.getAttrList();
1134 AL
= It
->getSecond();
1136 LLVMContext
&Ctx
= IRP
.getAnchorValue().getContext();
1137 auto AttrIdx
= IRP
.getAttrIdx();
1138 AttributeSet AS
= AL
.getAttributes(AttrIdx
);
1140 AttrBuilder
AB(Ctx
);
1142 ChangeStatus HasChanged
= ChangeStatus::UNCHANGED
;
1143 for (const DescTy
&AttrDesc
: AttrDescs
)
1144 if (CB(AttrDesc
, AS
, AM
, AB
))
1145 HasChanged
= ChangeStatus::CHANGED
;
1147 if (HasChanged
== ChangeStatus::UNCHANGED
)
1148 return ChangeStatus::UNCHANGED
;
1150 AL
= AL
.removeAttributesAtIndex(Ctx
, AttrIdx
, AM
);
1151 AL
= AL
.addAttributesAtIndex(Ctx
, AttrIdx
, AB
);
1152 AttrsMap
[AttrListAnchor
] = AL
;
1153 return ChangeStatus::CHANGED
;
1156 bool Attributor::hasAttr(const IRPosition
&IRP
,
1157 ArrayRef
<Attribute::AttrKind
> AttrKinds
,
1158 bool IgnoreSubsumingPositions
,
1159 Attribute::AttrKind ImpliedAttributeKind
) {
1160 bool Implied
= false;
1161 bool HasAttr
= false;
1162 auto HasAttrCB
= [&](const Attribute::AttrKind
&Kind
, AttributeSet AttrSet
,
1163 AttributeMask
&, AttrBuilder
&) {
1164 if (AttrSet
.hasAttribute(Kind
)) {
1165 Implied
|= Kind
!= ImpliedAttributeKind
;
1170 for (const IRPosition
&EquivIRP
: SubsumingPositionIterator(IRP
)) {
1171 updateAttrMap
<Attribute::AttrKind
>(EquivIRP
, AttrKinds
, HasAttrCB
);
1174 // The first position returned by the SubsumingPositionIterator is
1175 // always the position itself. If we ignore subsuming positions we
1176 // are done after the first iteration.
1177 if (IgnoreSubsumingPositions
)
1183 SmallVector
<Attribute
> Attrs
;
1184 for (Attribute::AttrKind AK
: AttrKinds
)
1185 if (getAttrsFromAssumes(IRP
, AK
, Attrs
)) {
1191 // Check if we should manifest the implied attribute kind at the IRP.
1192 if (ImpliedAttributeKind
!= Attribute::None
&& HasAttr
&& Implied
)
1193 manifestAttrs(IRP
, {Attribute::get(IRP
.getAnchorValue().getContext(),
1194 ImpliedAttributeKind
)});
1198 void Attributor::getAttrs(const IRPosition
&IRP
,
1199 ArrayRef
<Attribute::AttrKind
> AttrKinds
,
1200 SmallVectorImpl
<Attribute
> &Attrs
,
1201 bool IgnoreSubsumingPositions
) {
1202 auto CollectAttrCB
= [&](const Attribute::AttrKind
&Kind
,
1203 AttributeSet AttrSet
, AttributeMask
&,
1205 if (AttrSet
.hasAttribute(Kind
))
1206 Attrs
.push_back(AttrSet
.getAttribute(Kind
));
1209 for (const IRPosition
&EquivIRP
: SubsumingPositionIterator(IRP
)) {
1210 updateAttrMap
<Attribute::AttrKind
>(EquivIRP
, AttrKinds
, CollectAttrCB
);
1211 // The first position returned by the SubsumingPositionIterator is
1212 // always the position itself. If we ignore subsuming positions we
1213 // are done after the first iteration.
1214 if (IgnoreSubsumingPositions
)
1217 for (Attribute::AttrKind AK
: AttrKinds
)
1218 getAttrsFromAssumes(IRP
, AK
, Attrs
);
1221 ChangeStatus
Attributor::removeAttrs(const IRPosition
&IRP
,
1222 ArrayRef
<Attribute::AttrKind
> AttrKinds
) {
1223 auto RemoveAttrCB
= [&](const Attribute::AttrKind
&Kind
, AttributeSet AttrSet
,
1224 AttributeMask
&AM
, AttrBuilder
&) {
1225 if (!AttrSet
.hasAttribute(Kind
))
1227 AM
.addAttribute(Kind
);
1230 return updateAttrMap
<Attribute::AttrKind
>(IRP
, AttrKinds
, RemoveAttrCB
);
1233 ChangeStatus
Attributor::removeAttrs(const IRPosition
&IRP
,
1234 ArrayRef
<StringRef
> Attrs
) {
1235 auto RemoveAttrCB
= [&](StringRef Attr
, AttributeSet AttrSet
,
1236 AttributeMask
&AM
, AttrBuilder
&) -> bool {
1237 if (!AttrSet
.hasAttribute(Attr
))
1239 AM
.addAttribute(Attr
);
1243 return updateAttrMap
<StringRef
>(IRP
, Attrs
, RemoveAttrCB
);
1246 ChangeStatus
Attributor::manifestAttrs(const IRPosition
&IRP
,
1247 ArrayRef
<Attribute
> Attrs
,
1248 bool ForceReplace
) {
1249 LLVMContext
&Ctx
= IRP
.getAnchorValue().getContext();
1250 auto AddAttrCB
= [&](const Attribute
&Attr
, AttributeSet AttrSet
,
1251 AttributeMask
&, AttrBuilder
&AB
) {
1252 return addIfNotExistent(Ctx
, Attr
, AttrSet
, ForceReplace
, AB
);
1254 return updateAttrMap
<Attribute
>(IRP
, Attrs
, AddAttrCB
);
1257 const IRPosition
IRPosition::EmptyKey(DenseMapInfo
<void *>::getEmptyKey());
1259 IRPosition::TombstoneKey(DenseMapInfo
<void *>::getTombstoneKey());
1261 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition
&IRP
) {
1262 IRPositions
.emplace_back(IRP
);
1264 // Helper to determine if operand bundles on a call site are benign or
1265 // potentially problematic. We handle only llvm.assume for now.
1266 auto CanIgnoreOperandBundles
= [](const CallBase
&CB
) {
1267 return (isa
<IntrinsicInst
>(CB
) &&
1268 cast
<IntrinsicInst
>(CB
).getIntrinsicID() == Intrinsic ::assume
);
1271 const auto *CB
= dyn_cast
<CallBase
>(&IRP
.getAnchorValue());
1272 switch (IRP
.getPositionKind()) {
1273 case IRPosition::IRP_INVALID
:
1274 case IRPosition::IRP_FLOAT
:
1275 case IRPosition::IRP_FUNCTION
:
1277 case IRPosition::IRP_ARGUMENT
:
1278 case IRPosition::IRP_RETURNED
:
1279 IRPositions
.emplace_back(IRPosition::function(*IRP
.getAnchorScope()));
1281 case IRPosition::IRP_CALL_SITE
:
1282 assert(CB
&& "Expected call site!");
1283 // TODO: We need to look at the operand bundles similar to the redirection
1285 if (!CB
->hasOperandBundles() || CanIgnoreOperandBundles(*CB
))
1286 if (auto *Callee
= dyn_cast_if_present
<Function
>(CB
->getCalledOperand()))
1287 IRPositions
.emplace_back(IRPosition::function(*Callee
));
1289 case IRPosition::IRP_CALL_SITE_RETURNED
:
1290 assert(CB
&& "Expected call site!");
1291 // TODO: We need to look at the operand bundles similar to the redirection
1293 if (!CB
->hasOperandBundles() || CanIgnoreOperandBundles(*CB
)) {
1295 dyn_cast_if_present
<Function
>(CB
->getCalledOperand())) {
1296 IRPositions
.emplace_back(IRPosition::returned(*Callee
));
1297 IRPositions
.emplace_back(IRPosition::function(*Callee
));
1298 for (const Argument
&Arg
: Callee
->args())
1299 if (Arg
.hasReturnedAttr()) {
1300 IRPositions
.emplace_back(
1301 IRPosition::callsite_argument(*CB
, Arg
.getArgNo()));
1302 IRPositions
.emplace_back(
1303 IRPosition::value(*CB
->getArgOperand(Arg
.getArgNo())));
1304 IRPositions
.emplace_back(IRPosition::argument(Arg
));
1308 IRPositions
.emplace_back(IRPosition::callsite_function(*CB
));
1310 case IRPosition::IRP_CALL_SITE_ARGUMENT
: {
1311 assert(CB
&& "Expected call site!");
1312 // TODO: We need to look at the operand bundles similar to the redirection
1314 if (!CB
->hasOperandBundles() || CanIgnoreOperandBundles(*CB
)) {
1315 auto *Callee
= dyn_cast_if_present
<Function
>(CB
->getCalledOperand());
1317 if (Argument
*Arg
= IRP
.getAssociatedArgument())
1318 IRPositions
.emplace_back(IRPosition::argument(*Arg
));
1319 IRPositions
.emplace_back(IRPosition::function(*Callee
));
1322 IRPositions
.emplace_back(IRPosition::value(IRP
.getAssociatedValue()));
1328 void IRPosition::verify() {
1329 #ifdef EXPENSIVE_CHECKS
1330 switch (getPositionKind()) {
1332 assert((CBContext
== nullptr) &&
1333 "Invalid position must not have CallBaseContext!");
1334 assert(!Enc
.getOpaqueValue() &&
1335 "Expected a nullptr for an invalid position!");
1338 assert((!isa
<Argument
>(&getAssociatedValue())) &&
1339 "Expected specialized kind for argument values!");
1342 assert(isa
<Function
>(getAsValuePtr()) &&
1343 "Expected function for a 'returned' position!");
1344 assert(getAsValuePtr() == &getAssociatedValue() &&
1345 "Associated value mismatch!");
1347 case IRP_CALL_SITE_RETURNED
:
1348 assert((CBContext
== nullptr) &&
1349 "'call site returned' position must not have CallBaseContext!");
1350 assert((isa
<CallBase
>(getAsValuePtr())) &&
1351 "Expected call base for 'call site returned' position!");
1352 assert(getAsValuePtr() == &getAssociatedValue() &&
1353 "Associated value mismatch!");
1356 assert((CBContext
== nullptr) &&
1357 "'call site function' position must not have CallBaseContext!");
1358 assert((isa
<CallBase
>(getAsValuePtr())) &&
1359 "Expected call base for 'call site function' position!");
1360 assert(getAsValuePtr() == &getAssociatedValue() &&
1361 "Associated value mismatch!");
1364 assert(isa
<Function
>(getAsValuePtr()) &&
1365 "Expected function for a 'function' position!");
1366 assert(getAsValuePtr() == &getAssociatedValue() &&
1367 "Associated value mismatch!");
1370 assert(isa
<Argument
>(getAsValuePtr()) &&
1371 "Expected argument for a 'argument' position!");
1372 assert(getAsValuePtr() == &getAssociatedValue() &&
1373 "Associated value mismatch!");
1375 case IRP_CALL_SITE_ARGUMENT
: {
1376 assert((CBContext
== nullptr) &&
1377 "'call site argument' position must not have CallBaseContext!");
1378 Use
*U
= getAsUsePtr();
1379 (void)U
; // Silence unused variable warning.
1380 assert(U
&& "Expected use for a 'call site argument' position!");
1381 assert(isa
<CallBase
>(U
->getUser()) &&
1382 "Expected call base user for a 'call site argument' position!");
1383 assert(cast
<CallBase
>(U
->getUser())->isArgOperand(U
) &&
1384 "Expected call base argument operand for a 'call site argument' "
1386 assert(cast
<CallBase
>(U
->getUser())->getArgOperandNo(U
) ==
1387 unsigned(getCallSiteArgNo()) &&
1388 "Argument number mismatch!");
1389 assert(U
->get() == &getAssociatedValue() && "Associated value mismatch!");
1396 std::optional
<Constant
*>
1397 Attributor::getAssumedConstant(const IRPosition
&IRP
,
1398 const AbstractAttribute
&AA
,
1399 bool &UsedAssumedInformation
) {
1400 // First check all callbacks provided by outside AAs. If any of them returns
1401 // a non-null value that is different from the associated value, or
1402 // std::nullopt, we assume it's simplified.
1403 for (auto &CB
: SimplificationCallbacks
.lookup(IRP
)) {
1404 std::optional
<Value
*> SimplifiedV
= CB(IRP
, &AA
, UsedAssumedInformation
);
1406 return std::nullopt
;
1407 if (isa_and_nonnull
<Constant
>(*SimplifiedV
))
1408 return cast
<Constant
>(*SimplifiedV
);
1411 if (auto *C
= dyn_cast
<Constant
>(&IRP
.getAssociatedValue()))
1413 SmallVector
<AA::ValueAndContext
> Values
;
1414 if (getAssumedSimplifiedValues(IRP
, &AA
, Values
,
1415 AA::ValueScope::Interprocedural
,
1416 UsedAssumedInformation
)) {
1418 return std::nullopt
;
1419 if (auto *C
= dyn_cast_or_null
<Constant
>(
1420 AAPotentialValues::getSingleValue(*this, AA
, IRP
, Values
)))
1426 std::optional
<Value
*> Attributor::getAssumedSimplified(
1427 const IRPosition
&IRP
, const AbstractAttribute
*AA
,
1428 bool &UsedAssumedInformation
, AA::ValueScope S
) {
1429 // First check all callbacks provided by outside AAs. If any of them returns
1430 // a non-null value that is different from the associated value, or
1431 // std::nullopt, we assume it's simplified.
1432 for (auto &CB
: SimplificationCallbacks
.lookup(IRP
))
1433 return CB(IRP
, AA
, UsedAssumedInformation
);
1435 SmallVector
<AA::ValueAndContext
> Values
;
1436 if (!getAssumedSimplifiedValues(IRP
, AA
, Values
, S
, UsedAssumedInformation
))
1437 return &IRP
.getAssociatedValue();
1439 return std::nullopt
;
1441 if (Value
*V
= AAPotentialValues::getSingleValue(*this, *AA
, IRP
, Values
))
1443 if (IRP
.getPositionKind() == IRPosition::IRP_RETURNED
||
1444 IRP
.getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED
)
1446 return &IRP
.getAssociatedValue();
1449 bool Attributor::getAssumedSimplifiedValues(
1450 const IRPosition
&InitialIRP
, const AbstractAttribute
*AA
,
1451 SmallVectorImpl
<AA::ValueAndContext
> &Values
, AA::ValueScope S
,
1452 bool &UsedAssumedInformation
, bool RecurseForSelectAndPHI
) {
1453 SmallPtrSet
<Value
*, 8> Seen
;
1454 SmallVector
<IRPosition
, 8> Worklist
;
1455 Worklist
.push_back(InitialIRP
);
1456 while (!Worklist
.empty()) {
1457 const IRPosition
&IRP
= Worklist
.pop_back_val();
1459 // First check all callbacks provided by outside AAs. If any of them returns
1460 // a non-null value that is different from the associated value, or
1461 // std::nullopt, we assume it's simplified.
1462 int NV
= Values
.size();
1463 const auto &SimplificationCBs
= SimplificationCallbacks
.lookup(IRP
);
1464 for (const auto &CB
: SimplificationCBs
) {
1465 std::optional
<Value
*> CBResult
= CB(IRP
, AA
, UsedAssumedInformation
);
1466 if (!CBResult
.has_value())
1468 Value
*V
= *CBResult
;
1471 if ((S
& AA::ValueScope::Interprocedural
) ||
1472 AA::isValidInScope(*V
, IRP
.getAnchorScope()))
1473 Values
.push_back(AA::ValueAndContext
{*V
, nullptr});
1477 if (SimplificationCBs
.empty()) {
1478 // If no high-level/outside simplification occurred, use
1479 // AAPotentialValues.
1480 const auto *PotentialValuesAA
=
1481 getOrCreateAAFor
<AAPotentialValues
>(IRP
, AA
, DepClassTy::OPTIONAL
);
1482 if (PotentialValuesAA
&& PotentialValuesAA
->getAssumedSimplifiedValues(*this, Values
, S
)) {
1483 UsedAssumedInformation
|= !PotentialValuesAA
->isAtFixpoint();
1484 } else if (IRP
.getPositionKind() != IRPosition::IRP_RETURNED
) {
1485 Values
.push_back({IRP
.getAssociatedValue(), IRP
.getCtxI()});
1487 // TODO: We could visit all returns and add the operands.
1492 if (!RecurseForSelectAndPHI
)
1495 for (int I
= NV
, E
= Values
.size(); I
< E
; ++I
) {
1496 Value
*V
= Values
[I
].getValue();
1497 if (!isa
<PHINode
>(V
) && !isa
<SelectInst
>(V
))
1499 if (!Seen
.insert(V
).second
)
1501 // Move the last element to this slot.
1502 Values
[I
] = Values
[E
- 1];
1503 // Eliminate the last slot, adjust the indices.
1507 // Add a new value (select or phi) to the worklist.
1508 Worklist
.push_back(IRPosition::value(*V
));
1514 std::optional
<Value
*> Attributor::translateArgumentToCallSiteContent(
1515 std::optional
<Value
*> V
, CallBase
&CB
, const AbstractAttribute
&AA
,
1516 bool &UsedAssumedInformation
) {
1519 if (*V
== nullptr || isa
<Constant
>(*V
))
1521 if (auto *Arg
= dyn_cast
<Argument
>(*V
))
1522 if (CB
.getCalledOperand() == Arg
->getParent() &&
1523 CB
.arg_size() > Arg
->getArgNo())
1524 if (!Arg
->hasPointeeInMemoryValueAttr())
1525 return getAssumedSimplified(
1526 IRPosition::callsite_argument(CB
, Arg
->getArgNo()), AA
,
1527 UsedAssumedInformation
, AA::Intraprocedural
);
1531 Attributor::~Attributor() {
1532 // The abstract attributes are allocated via the BumpPtrAllocator Allocator,
1533 // thus we cannot delete them. We can, and want to, destruct them though.
1534 for (auto &It
: AAMap
) {
1535 AbstractAttribute
*AA
= It
.getSecond();
1536 AA
->~AbstractAttribute();
1540 bool Attributor::isAssumedDead(const AbstractAttribute
&AA
,
1541 const AAIsDead
*FnLivenessAA
,
1542 bool &UsedAssumedInformation
,
1543 bool CheckBBLivenessOnly
, DepClassTy DepClass
) {
1544 if (!Configuration
.UseLiveness
)
1546 const IRPosition
&IRP
= AA
.getIRPosition();
1547 if (!Functions
.count(IRP
.getAnchorScope()))
1549 return isAssumedDead(IRP
, &AA
, FnLivenessAA
, UsedAssumedInformation
,
1550 CheckBBLivenessOnly
, DepClass
);
1553 bool Attributor::isAssumedDead(const Use
&U
,
1554 const AbstractAttribute
*QueryingAA
,
1555 const AAIsDead
*FnLivenessAA
,
1556 bool &UsedAssumedInformation
,
1557 bool CheckBBLivenessOnly
, DepClassTy DepClass
) {
1558 if (!Configuration
.UseLiveness
)
1560 Instruction
*UserI
= dyn_cast
<Instruction
>(U
.getUser());
1562 return isAssumedDead(IRPosition::value(*U
.get()), QueryingAA
, FnLivenessAA
,
1563 UsedAssumedInformation
, CheckBBLivenessOnly
, DepClass
);
1565 if (auto *CB
= dyn_cast
<CallBase
>(UserI
)) {
1566 // For call site argument uses we can check if the argument is
1568 if (CB
->isArgOperand(&U
)) {
1569 const IRPosition
&CSArgPos
=
1570 IRPosition::callsite_argument(*CB
, CB
->getArgOperandNo(&U
));
1571 return isAssumedDead(CSArgPos
, QueryingAA
, FnLivenessAA
,
1572 UsedAssumedInformation
, CheckBBLivenessOnly
,
1575 } else if (ReturnInst
*RI
= dyn_cast
<ReturnInst
>(UserI
)) {
1576 const IRPosition
&RetPos
= IRPosition::returned(*RI
->getFunction());
1577 return isAssumedDead(RetPos
, QueryingAA
, FnLivenessAA
,
1578 UsedAssumedInformation
, CheckBBLivenessOnly
, DepClass
);
1579 } else if (PHINode
*PHI
= dyn_cast
<PHINode
>(UserI
)) {
1580 BasicBlock
*IncomingBB
= PHI
->getIncomingBlock(U
);
1581 return isAssumedDead(*IncomingBB
->getTerminator(), QueryingAA
, FnLivenessAA
,
1582 UsedAssumedInformation
, CheckBBLivenessOnly
, DepClass
);
1583 } else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(UserI
)) {
1584 if (!CheckBBLivenessOnly
&& SI
->getPointerOperand() != U
.get()) {
1585 const IRPosition IRP
= IRPosition::inst(*SI
);
1586 const AAIsDead
*IsDeadAA
=
1587 getOrCreateAAFor
<AAIsDead
>(IRP
, QueryingAA
, DepClassTy::NONE
);
1588 if (IsDeadAA
&& IsDeadAA
->isRemovableStore()) {
1590 recordDependence(*IsDeadAA
, *QueryingAA
, DepClass
);
1591 if (!IsDeadAA
->isKnown(AAIsDead::IS_REMOVABLE
))
1592 UsedAssumedInformation
= true;
1598 return isAssumedDead(IRPosition::inst(*UserI
), QueryingAA
, FnLivenessAA
,
1599 UsedAssumedInformation
, CheckBBLivenessOnly
, DepClass
);
1602 bool Attributor::isAssumedDead(const Instruction
&I
,
1603 const AbstractAttribute
*QueryingAA
,
1604 const AAIsDead
*FnLivenessAA
,
1605 bool &UsedAssumedInformation
,
1606 bool CheckBBLivenessOnly
, DepClassTy DepClass
,
1607 bool CheckForDeadStore
) {
1608 if (!Configuration
.UseLiveness
)
1610 const IRPosition::CallBaseContext
*CBCtx
=
1611 QueryingAA
? QueryingAA
->getCallBaseContext() : nullptr;
1613 if (ManifestAddedBlocks
.contains(I
.getParent()))
1616 const Function
&F
= *I
.getFunction();
1617 if (!FnLivenessAA
|| FnLivenessAA
->getAnchorScope() != &F
)
1618 FnLivenessAA
= getOrCreateAAFor
<AAIsDead
>(IRPosition::function(F
, CBCtx
),
1619 QueryingAA
, DepClassTy::NONE
);
1621 // Don't use recursive reasoning.
1622 if (!FnLivenessAA
|| QueryingAA
== FnLivenessAA
)
1625 // If we have a context instruction and a liveness AA we use it.
1626 if (CheckBBLivenessOnly
? FnLivenessAA
->isAssumedDead(I
.getParent())
1627 : FnLivenessAA
->isAssumedDead(&I
)) {
1629 recordDependence(*FnLivenessAA
, *QueryingAA
, DepClass
);
1630 if (!FnLivenessAA
->isKnownDead(&I
))
1631 UsedAssumedInformation
= true;
1635 if (CheckBBLivenessOnly
)
1638 const IRPosition IRP
= IRPosition::inst(I
, CBCtx
);
1639 const AAIsDead
*IsDeadAA
=
1640 getOrCreateAAFor
<AAIsDead
>(IRP
, QueryingAA
, DepClassTy::NONE
);
1642 // Don't use recursive reasoning.
1643 if (!IsDeadAA
|| QueryingAA
== IsDeadAA
)
1646 if (IsDeadAA
->isAssumedDead()) {
1648 recordDependence(*IsDeadAA
, *QueryingAA
, DepClass
);
1649 if (!IsDeadAA
->isKnownDead())
1650 UsedAssumedInformation
= true;
1654 if (CheckForDeadStore
&& isa
<StoreInst
>(I
) && IsDeadAA
->isRemovableStore()) {
1656 recordDependence(*IsDeadAA
, *QueryingAA
, DepClass
);
1657 if (!IsDeadAA
->isKnownDead())
1658 UsedAssumedInformation
= true;
1665 bool Attributor::isAssumedDead(const IRPosition
&IRP
,
1666 const AbstractAttribute
*QueryingAA
,
1667 const AAIsDead
*FnLivenessAA
,
1668 bool &UsedAssumedInformation
,
1669 bool CheckBBLivenessOnly
, DepClassTy DepClass
) {
1670 if (!Configuration
.UseLiveness
)
1672 // Don't check liveness for constants, e.g. functions, used as (floating)
1673 // values since the context instruction and such is here meaningless.
1674 if (IRP
.getPositionKind() == IRPosition::IRP_FLOAT
&&
1675 isa
<Constant
>(IRP
.getAssociatedValue())) {
1679 Instruction
*CtxI
= IRP
.getCtxI();
1681 isAssumedDead(*CtxI
, QueryingAA
, FnLivenessAA
, UsedAssumedInformation
,
1682 /* CheckBBLivenessOnly */ true,
1683 CheckBBLivenessOnly
? DepClass
: DepClassTy::OPTIONAL
))
1686 if (CheckBBLivenessOnly
)
1689 // If we haven't succeeded we query the specific liveness info for the IRP.
1690 const AAIsDead
*IsDeadAA
;
1691 if (IRP
.getPositionKind() == IRPosition::IRP_CALL_SITE
)
1692 IsDeadAA
= getOrCreateAAFor
<AAIsDead
>(
1693 IRPosition::callsite_returned(cast
<CallBase
>(IRP
.getAssociatedValue())),
1694 QueryingAA
, DepClassTy::NONE
);
1696 IsDeadAA
= getOrCreateAAFor
<AAIsDead
>(IRP
, QueryingAA
, DepClassTy::NONE
);
1698 // Don't use recursive reasoning.
1699 if (!IsDeadAA
|| QueryingAA
== IsDeadAA
)
1702 if (IsDeadAA
->isAssumedDead()) {
1704 recordDependence(*IsDeadAA
, *QueryingAA
, DepClass
);
1705 if (!IsDeadAA
->isKnownDead())
1706 UsedAssumedInformation
= true;
1713 bool Attributor::isAssumedDead(const BasicBlock
&BB
,
1714 const AbstractAttribute
*QueryingAA
,
1715 const AAIsDead
*FnLivenessAA
,
1716 DepClassTy DepClass
) {
1717 if (!Configuration
.UseLiveness
)
1719 const Function
&F
= *BB
.getParent();
1720 if (!FnLivenessAA
|| FnLivenessAA
->getAnchorScope() != &F
)
1721 FnLivenessAA
= getOrCreateAAFor
<AAIsDead
>(IRPosition::function(F
),
1722 QueryingAA
, DepClassTy::NONE
);
1724 // Don't use recursive reasoning.
1725 if (!FnLivenessAA
|| QueryingAA
== FnLivenessAA
)
1728 if (FnLivenessAA
->isAssumedDead(&BB
)) {
1730 recordDependence(*FnLivenessAA
, *QueryingAA
, DepClass
);
1737 bool Attributor::checkForAllCallees(
1738 function_ref
<bool(ArrayRef
<const Function
*>)> Pred
,
1739 const AbstractAttribute
&QueryingAA
, const CallBase
&CB
) {
1740 if (const Function
*Callee
= dyn_cast
<Function
>(CB
.getCalledOperand()))
1741 return Pred(Callee
);
1743 const auto *CallEdgesAA
= getAAFor
<AACallEdges
>(
1744 QueryingAA
, IRPosition::callsite_function(CB
), DepClassTy::OPTIONAL
);
1745 if (!CallEdgesAA
|| CallEdgesAA
->hasUnknownCallee())
1748 const auto &Callees
= CallEdgesAA
->getOptimisticEdges();
1749 return Pred(Callees
.getArrayRef());
1752 bool Attributor::checkForAllUses(
1753 function_ref
<bool(const Use
&, bool &)> Pred
,
1754 const AbstractAttribute
&QueryingAA
, const Value
&V
,
1755 bool CheckBBLivenessOnly
, DepClassTy LivenessDepClass
,
1756 bool IgnoreDroppableUses
,
1757 function_ref
<bool(const Use
&OldU
, const Use
&NewU
)> EquivalentUseCB
) {
1759 // Check virtual uses first.
1760 for (VirtualUseCallbackTy
&CB
: VirtualUseCallbacks
.lookup(&V
))
1761 if (!CB(*this, &QueryingAA
))
1764 // Check the trivial case first as it catches void values.
1768 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
1769 SmallVector
<const Use
*, 16> Worklist
;
1770 SmallPtrSet
<const Use
*, 16> Visited
;
1772 auto AddUsers
= [&](const Value
&V
, const Use
*OldUse
) {
1773 for (const Use
&UU
: V
.uses()) {
1774 if (OldUse
&& EquivalentUseCB
&& !EquivalentUseCB(*OldUse
, UU
)) {
1775 LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
1776 "rejected by the equivalence call back: "
1781 Worklist
.push_back(&UU
);
1786 AddUsers(V
, /* OldUse */ nullptr);
1788 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist
.size()
1789 << " initial uses to check\n");
1791 const Function
*ScopeFn
= IRP
.getAnchorScope();
1792 const auto *LivenessAA
=
1793 ScopeFn
? getAAFor
<AAIsDead
>(QueryingAA
, IRPosition::function(*ScopeFn
),
1797 while (!Worklist
.empty()) {
1798 const Use
*U
= Worklist
.pop_back_val();
1799 if (isa
<PHINode
>(U
->getUser()) && !Visited
.insert(U
).second
)
1801 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
, {
1802 if (auto *Fn
= dyn_cast
<Function
>(U
->getUser()))
1803 dbgs() << "[Attributor] Check use: " << **U
<< " in " << Fn
->getName()
1806 dbgs() << "[Attributor] Check use: " << **U
<< " in " << *U
->getUser()
1809 bool UsedAssumedInformation
= false;
1810 if (isAssumedDead(*U
, &QueryingAA
, LivenessAA
, UsedAssumedInformation
,
1811 CheckBBLivenessOnly
, LivenessDepClass
)) {
1812 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
,
1813 dbgs() << "[Attributor] Dead use, skip!\n");
1816 if (IgnoreDroppableUses
&& U
->getUser()->isDroppable()) {
1817 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
,
1818 dbgs() << "[Attributor] Droppable user, skip!\n");
1822 if (auto *SI
= dyn_cast
<StoreInst
>(U
->getUser())) {
1823 if (&SI
->getOperandUse(0) == U
) {
1824 if (!Visited
.insert(U
).second
)
1826 SmallSetVector
<Value
*, 4> PotentialCopies
;
1827 if (AA::getPotentialCopiesOfStoredValue(
1828 *this, *SI
, PotentialCopies
, QueryingAA
, UsedAssumedInformation
,
1829 /* OnlyExact */ true)) {
1830 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
,
1832 << "[Attributor] Value is stored, continue with "
1833 << PotentialCopies
.size()
1834 << " potential copies instead!\n");
1835 for (Value
*PotentialCopy
: PotentialCopies
)
1836 if (!AddUsers(*PotentialCopy
, U
))
1843 bool Follow
= false;
1844 if (!Pred(*U
, Follow
))
1849 User
&Usr
= *U
->getUser();
1850 AddUsers(Usr
, /* OldUse */ nullptr);
1852 auto *RI
= dyn_cast
<ReturnInst
>(&Usr
);
1856 Function
&F
= *RI
->getFunction();
1857 auto CallSitePred
= [&](AbstractCallSite ACS
) {
1858 return AddUsers(*ACS
.getInstruction(), U
);
1860 if (!checkForAllCallSites(CallSitePred
, F
, /* RequireAllCallSites */ true,
1861 &QueryingAA
, UsedAssumedInformation
)) {
1862 LLVM_DEBUG(dbgs() << "[Attributor] Could not follow return instruction "
1863 "to all call sites: "
1872 bool Attributor::checkForAllCallSites(function_ref
<bool(AbstractCallSite
)> Pred
,
1873 const AbstractAttribute
&QueryingAA
,
1874 bool RequireAllCallSites
,
1875 bool &UsedAssumedInformation
) {
1876 // We can try to determine information from
1877 // the call sites. However, this is only possible all call sites are known,
1878 // hence the function has internal linkage.
1879 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
1880 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
1881 if (!AssociatedFunction
) {
1882 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
1887 return checkForAllCallSites(Pred
, *AssociatedFunction
, RequireAllCallSites
,
1888 &QueryingAA
, UsedAssumedInformation
);
1891 bool Attributor::checkForAllCallSites(function_ref
<bool(AbstractCallSite
)> Pred
,
1893 bool RequireAllCallSites
,
1894 const AbstractAttribute
*QueryingAA
,
1895 bool &UsedAssumedInformation
,
1896 bool CheckPotentiallyDead
) {
1897 if (RequireAllCallSites
&& !Fn
.hasLocalLinkage()) {
1900 << "[Attributor] Function " << Fn
.getName()
1901 << " has no internal linkage, hence not all call sites are known\n");
1904 // Check virtual uses first.
1905 for (VirtualUseCallbackTy
&CB
: VirtualUseCallbacks
.lookup(&Fn
))
1906 if (!CB(*this, QueryingAA
))
1909 SmallVector
<const Use
*, 8> Uses(make_pointer_range(Fn
.uses()));
1910 for (unsigned u
= 0; u
< Uses
.size(); ++u
) {
1911 const Use
&U
= *Uses
[u
];
1912 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
, {
1913 if (auto *Fn
= dyn_cast
<Function
>(U
))
1914 dbgs() << "[Attributor] Check use: " << Fn
->getName() << " in "
1915 << *U
.getUser() << "\n";
1917 dbgs() << "[Attributor] Check use: " << *U
<< " in " << *U
.getUser()
1920 if (!CheckPotentiallyDead
&&
1921 isAssumedDead(U
, QueryingAA
, nullptr, UsedAssumedInformation
,
1922 /* CheckBBLivenessOnly */ true)) {
1923 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
,
1924 dbgs() << "[Attributor] Dead use, skip!\n");
1927 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(U
.getUser())) {
1928 if (CE
->isCast() && CE
->getType()->isPointerTy()) {
1929 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
, {
1930 dbgs() << "[Attributor] Use, is constant cast expression, add "
1931 << CE
->getNumUses() << " uses of that expression instead!\n";
1933 for (const Use
&CEU
: CE
->uses())
1934 Uses
.push_back(&CEU
);
1939 AbstractCallSite
ACS(&U
);
1941 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn
.getName()
1942 << " has non call site use " << *U
.get() << " in "
1943 << *U
.getUser() << "\n");
1944 // BlockAddress users are allowed.
1945 if (isa
<BlockAddress
>(U
.getUser()))
1950 const Use
*EffectiveUse
=
1951 ACS
.isCallbackCall() ? &ACS
.getCalleeUseForCallback() : &U
;
1952 if (!ACS
.isCallee(EffectiveUse
)) {
1953 if (!RequireAllCallSites
) {
1954 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse
->getUser()
1955 << " is not a call of " << Fn
.getName()
1959 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse
->getUser()
1960 << " is an invalid use of " << Fn
.getName() << "\n");
1964 // Make sure the arguments that can be matched between the call site and the
1965 // callee argee on their type. It is unlikely they do not and it doesn't
1966 // make sense for all attributes to know/care about this.
1967 assert(&Fn
== ACS
.getCalledFunction() && "Expected known callee");
1968 unsigned MinArgsParams
=
1969 std::min(size_t(ACS
.getNumArgOperands()), Fn
.arg_size());
1970 for (unsigned u
= 0; u
< MinArgsParams
; ++u
) {
1971 Value
*CSArgOp
= ACS
.getCallArgOperand(u
);
1972 if (CSArgOp
&& Fn
.getArg(u
)->getType() != CSArgOp
->getType()) {
1974 dbgs() << "[Attributor] Call site / callee argument type mismatch ["
1975 << u
<< "@" << Fn
.getName() << ": "
1976 << *Fn
.getArg(u
)->getType() << " vs. "
1977 << *ACS
.getCallArgOperand(u
)->getType() << "\n");
1985 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
1986 << *ACS
.getInstruction() << "\n");
1993 bool Attributor::shouldPropagateCallBaseContext(const IRPosition
&IRP
) {
1994 // TODO: Maintain a cache of Values that are
1995 // on the pathway from a Argument to a Instruction that would effect the
1996 // liveness/return state etc.
1997 return EnableCallSiteSpecific
;
2000 bool Attributor::checkForAllReturnedValues(function_ref
<bool(Value
&)> Pred
,
2001 const AbstractAttribute
&QueryingAA
,
2003 bool RecurseForSelectAndPHI
) {
2005 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
2006 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
2007 if (!AssociatedFunction
)
2010 bool UsedAssumedInformation
= false;
2011 SmallVector
<AA::ValueAndContext
> Values
;
2012 if (!getAssumedSimplifiedValues(
2013 IRPosition::returned(*AssociatedFunction
), &QueryingAA
, Values
, S
,
2014 UsedAssumedInformation
, RecurseForSelectAndPHI
))
2017 return llvm::all_of(Values
, [&](const AA::ValueAndContext
&VAC
) {
2018 return Pred(*VAC
.getValue());
2022 static bool checkForAllInstructionsImpl(
2023 Attributor
*A
, InformationCache::OpcodeInstMapTy
&OpcodeInstMap
,
2024 function_ref
<bool(Instruction
&)> Pred
, const AbstractAttribute
*QueryingAA
,
2025 const AAIsDead
*LivenessAA
, ArrayRef
<unsigned> Opcodes
,
2026 bool &UsedAssumedInformation
, bool CheckBBLivenessOnly
= false,
2027 bool CheckPotentiallyDead
= false) {
2028 for (unsigned Opcode
: Opcodes
) {
2029 // Check if we have instructions with this opcode at all first.
2030 auto *Insts
= OpcodeInstMap
.lookup(Opcode
);
2034 for (Instruction
*I
: *Insts
) {
2035 // Skip dead instructions.
2036 if (A
&& !CheckPotentiallyDead
&&
2037 A
->isAssumedDead(IRPosition::inst(*I
), QueryingAA
, LivenessAA
,
2038 UsedAssumedInformation
, CheckBBLivenessOnly
)) {
2039 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
,
2040 dbgs() << "[Attributor] Instruction " << *I
2041 << " is potentially dead, skip!\n";);
2052 bool Attributor::checkForAllInstructions(function_ref
<bool(Instruction
&)> Pred
,
2054 const AbstractAttribute
*QueryingAA
,
2055 ArrayRef
<unsigned> Opcodes
,
2056 bool &UsedAssumedInformation
,
2057 bool CheckBBLivenessOnly
,
2058 bool CheckPotentiallyDead
) {
2059 // Since we need to provide instructions we have to have an exact definition.
2060 if (!Fn
|| Fn
->isDeclaration())
2063 const IRPosition
&QueryIRP
= IRPosition::function(*Fn
);
2064 const auto *LivenessAA
=
2065 CheckPotentiallyDead
&& QueryingAA
2066 ? (getAAFor
<AAIsDead
>(*QueryingAA
, QueryIRP
, DepClassTy::NONE
))
2069 auto &OpcodeInstMap
= InfoCache
.getOpcodeInstMapForFunction(*Fn
);
2070 if (!checkForAllInstructionsImpl(this, OpcodeInstMap
, Pred
, QueryingAA
,
2071 LivenessAA
, Opcodes
, UsedAssumedInformation
,
2072 CheckBBLivenessOnly
, CheckPotentiallyDead
))
2078 bool Attributor::checkForAllInstructions(function_ref
<bool(Instruction
&)> Pred
,
2079 const AbstractAttribute
&QueryingAA
,
2080 ArrayRef
<unsigned> Opcodes
,
2081 bool &UsedAssumedInformation
,
2082 bool CheckBBLivenessOnly
,
2083 bool CheckPotentiallyDead
) {
2084 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
2085 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
2086 return checkForAllInstructions(Pred
, AssociatedFunction
, &QueryingAA
, Opcodes
,
2087 UsedAssumedInformation
, CheckBBLivenessOnly
,
2088 CheckPotentiallyDead
);
2091 bool Attributor::checkForAllReadWriteInstructions(
2092 function_ref
<bool(Instruction
&)> Pred
, AbstractAttribute
&QueryingAA
,
2093 bool &UsedAssumedInformation
) {
2094 TimeTraceScope
TS("checkForAllReadWriteInstructions");
2096 const Function
*AssociatedFunction
=
2097 QueryingAA
.getIRPosition().getAssociatedFunction();
2098 if (!AssociatedFunction
)
2101 const IRPosition
&QueryIRP
= IRPosition::function(*AssociatedFunction
);
2102 const auto *LivenessAA
=
2103 getAAFor
<AAIsDead
>(QueryingAA
, QueryIRP
, DepClassTy::NONE
);
2105 for (Instruction
*I
:
2106 InfoCache
.getReadOrWriteInstsForFunction(*AssociatedFunction
)) {
2107 // Skip dead instructions.
2108 if (isAssumedDead(IRPosition::inst(*I
), &QueryingAA
, LivenessAA
,
2109 UsedAssumedInformation
))
2119 void Attributor::runTillFixpoint() {
2120 TimeTraceScope
TimeScope("Attributor::runTillFixpoint");
2121 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
2122 << DG
.SyntheticRoot
.Deps
.size()
2123 << " abstract attributes.\n");
2125 // Now that all abstract attributes are collected and initialized we start
2126 // the abstract analysis.
2128 unsigned IterationCounter
= 1;
2129 unsigned MaxIterations
=
2130 Configuration
.MaxFixpointIterations
.value_or(SetFixpointIterations
);
2132 SmallVector
<AbstractAttribute
*, 32> ChangedAAs
;
2133 SetVector
<AbstractAttribute
*> Worklist
, InvalidAAs
;
2134 Worklist
.insert(DG
.SyntheticRoot
.begin(), DG
.SyntheticRoot
.end());
2137 // Remember the size to determine new attributes.
2138 size_t NumAAs
= DG
.SyntheticRoot
.Deps
.size();
2139 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
2140 << ", Worklist size: " << Worklist
.size() << "\n");
2142 // For invalid AAs we can fix dependent AAs that have a required dependence,
2143 // thereby folding long dependence chains in a single step without the need
2145 for (unsigned u
= 0; u
< InvalidAAs
.size(); ++u
) {
2146 AbstractAttribute
*InvalidAA
= InvalidAAs
[u
];
2148 // Check the dependences to fast track invalidation.
2149 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
,
2150 dbgs() << "[Attributor] InvalidAA: " << *InvalidAA
2151 << " has " << InvalidAA
->Deps
.size()
2152 << " required & optional dependences\n");
2153 for (auto &DepIt
: InvalidAA
->Deps
) {
2154 AbstractAttribute
*DepAA
= cast
<AbstractAttribute
>(DepIt
.getPointer());
2155 if (DepIt
.getInt() == unsigned(DepClassTy::OPTIONAL
)) {
2156 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
,
2157 dbgs() << " - recompute: " << *DepAA
);
2158 Worklist
.insert(DepAA
);
2161 DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE
, dbgs()
2162 << " - invalidate: " << *DepAA
);
2163 DepAA
->getState().indicatePessimisticFixpoint();
2164 assert(DepAA
->getState().isAtFixpoint() && "Expected fixpoint state!");
2165 if (!DepAA
->getState().isValidState())
2166 InvalidAAs
.insert(DepAA
);
2168 ChangedAAs
.push_back(DepAA
);
2170 InvalidAA
->Deps
.clear();
2173 // Add all abstract attributes that are potentially dependent on one that
2174 // changed to the work list.
2175 for (AbstractAttribute
*ChangedAA
: ChangedAAs
) {
2176 for (auto &DepIt
: ChangedAA
->Deps
)
2177 Worklist
.insert(cast
<AbstractAttribute
>(DepIt
.getPointer()));
2178 ChangedAA
->Deps
.clear();
2181 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
2182 << ", Worklist+Dependent size: " << Worklist
.size()
2185 // Reset the changed and invalid set.
2189 // Update all abstract attribute in the work list and record the ones that
2191 for (AbstractAttribute
*AA
: Worklist
) {
2192 const auto &AAState
= AA
->getState();
2193 if (!AAState
.isAtFixpoint())
2194 if (updateAA(*AA
) == ChangeStatus::CHANGED
)
2195 ChangedAAs
.push_back(AA
);
2197 // Use the InvalidAAs vector to propagate invalid states fast transitively
2198 // without requiring updates.
2199 if (!AAState
.isValidState())
2200 InvalidAAs
.insert(AA
);
2203 // Add attributes to the changed set if they have been created in the last
2205 ChangedAAs
.append(DG
.SyntheticRoot
.begin() + NumAAs
,
2206 DG
.SyntheticRoot
.end());
2208 // Reset the work list and repopulate with the changed abstract attributes.
2209 // Note that dependent ones are added above.
2211 Worklist
.insert(ChangedAAs
.begin(), ChangedAAs
.end());
2212 Worklist
.insert(QueryAAsAwaitingUpdate
.begin(),
2213 QueryAAsAwaitingUpdate
.end());
2214 QueryAAsAwaitingUpdate
.clear();
2216 } while (!Worklist
.empty() && (IterationCounter
++ < MaxIterations
));
2218 if (IterationCounter
> MaxIterations
&& !Functions
.empty()) {
2219 auto Remark
= [&](OptimizationRemarkMissed ORM
) {
2220 return ORM
<< "Attributor did not reach a fixpoint after "
2221 << ore::NV("Iterations", MaxIterations
) << " iterations.";
2223 Function
*F
= Functions
.front();
2224 emitRemark
<OptimizationRemarkMissed
>(F
, "FixedPoint", Remark
);
2227 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
2228 << IterationCounter
<< "/" << MaxIterations
2229 << " iterations\n");
2231 // Reset abstract arguments not settled in a sound fixpoint by now. This
2232 // happens when we stopped the fixpoint iteration early. Note that only the
2233 // ones marked as "changed" *and* the ones transitively depending on them
2234 // need to be reverted to a pessimistic state. Others might not be in a
2235 // fixpoint state but we can use the optimistic results for them anyway.
2236 SmallPtrSet
<AbstractAttribute
*, 32> Visited
;
2237 for (unsigned u
= 0; u
< ChangedAAs
.size(); u
++) {
2238 AbstractAttribute
*ChangedAA
= ChangedAAs
[u
];
2239 if (!Visited
.insert(ChangedAA
).second
)
2242 AbstractState
&State
= ChangedAA
->getState();
2243 if (!State
.isAtFixpoint()) {
2244 State
.indicatePessimisticFixpoint();
2246 NumAttributesTimedOut
++;
2249 for (auto &DepIt
: ChangedAA
->Deps
)
2250 ChangedAAs
.push_back(cast
<AbstractAttribute
>(DepIt
.getPointer()));
2251 ChangedAA
->Deps
.clear();
2255 if (!Visited
.empty())
2256 dbgs() << "\n[Attributor] Finalized " << Visited
.size()
2257 << " abstract attributes.\n";
2261 void Attributor::registerForUpdate(AbstractAttribute
&AA
) {
2262 assert(AA
.isQueryAA() &&
2263 "Non-query AAs should not be required to register for updates!");
2264 QueryAAsAwaitingUpdate
.insert(&AA
);
2267 ChangeStatus
Attributor::manifestAttributes() {
2268 TimeTraceScope
TimeScope("Attributor::manifestAttributes");
2269 size_t NumFinalAAs
= DG
.SyntheticRoot
.Deps
.size();
2271 unsigned NumManifested
= 0;
2272 unsigned NumAtFixpoint
= 0;
2273 ChangeStatus ManifestChange
= ChangeStatus::UNCHANGED
;
2274 for (auto &DepAA
: DG
.SyntheticRoot
.Deps
) {
2275 AbstractAttribute
*AA
= cast
<AbstractAttribute
>(DepAA
.getPointer());
2276 AbstractState
&State
= AA
->getState();
2278 // If there is not already a fixpoint reached, we can now take the
2279 // optimistic state. This is correct because we enforced a pessimistic one
2280 // on abstract attributes that were transitively dependent on a changed one
2282 if (!State
.isAtFixpoint())
2283 State
.indicateOptimisticFixpoint();
2285 // We must not manifest Attributes that use Callbase info.
2286 if (AA
->hasCallBaseContext())
2288 // If the state is invalid, we do not try to manifest it.
2289 if (!State
.isValidState())
2292 if (AA
->getCtxI() && !isRunOn(*AA
->getAnchorScope()))
2296 bool UsedAssumedInformation
= false;
2297 if (isAssumedDead(*AA
, nullptr, UsedAssumedInformation
,
2298 /* CheckBBLivenessOnly */ true))
2300 // Check if the manifest debug counter that allows skipping manifestation of
2302 if (!DebugCounter::shouldExecute(ManifestDBGCounter
))
2304 // Manifest the state and record if we changed the IR.
2305 ChangeStatus LocalChange
= AA
->manifest(*this);
2306 if (LocalChange
== ChangeStatus::CHANGED
&& AreStatisticsEnabled())
2307 AA
->trackStatistics();
2308 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange
<< " : " << *AA
2311 ManifestChange
= ManifestChange
| LocalChange
;
2314 NumManifested
+= (LocalChange
== ChangeStatus::CHANGED
);
2317 (void)NumManifested
;
2318 (void)NumAtFixpoint
;
2319 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
2320 << " arguments while " << NumAtFixpoint
2321 << " were in a valid fixpoint state\n");
2323 NumAttributesManifested
+= NumManifested
;
2324 NumAttributesValidFixpoint
+= NumAtFixpoint
;
2327 if (NumFinalAAs
!= DG
.SyntheticRoot
.Deps
.size()) {
2328 auto DepIt
= DG
.SyntheticRoot
.Deps
.begin();
2329 for (unsigned u
= 0; u
< NumFinalAAs
; ++u
)
2331 for (unsigned u
= NumFinalAAs
; u
< DG
.SyntheticRoot
.Deps
.size();
2333 errs() << "Unexpected abstract attribute: "
2334 << cast
<AbstractAttribute
>(DepIt
->getPointer()) << " :: "
2335 << cast
<AbstractAttribute
>(DepIt
->getPointer())
2337 .getAssociatedValue()
2340 llvm_unreachable("Expected the final number of abstract attributes to "
2341 "remain unchanged!");
2344 for (auto &It
: AttrsMap
) {
2345 AttributeList
&AL
= It
.getSecond();
2346 const IRPosition
&IRP
=
2347 isa
<Function
>(It
.getFirst())
2348 ? IRPosition::function(*cast
<Function
>(It
.getFirst()))
2349 : IRPosition::callsite_function(*cast
<CallBase
>(It
.getFirst()));
2350 IRP
.setAttrList(AL
);
2353 return ManifestChange
;
2356 void Attributor::identifyDeadInternalFunctions() {
2357 // Early exit if we don't intend to delete functions.
2358 if (!Configuration
.DeleteFns
)
2361 // To avoid triggering an assertion in the lazy call graph we will not delete
2362 // any internal library functions. We should modify the assertion though and
2363 // allow internals to be deleted.
2367 : getInfoCache().getTargetLibraryInfoForFunction(*Functions
.back());
2370 // Identify dead internal functions and delete them. This happens outside
2371 // the other fixpoint analysis as we might treat potentially dead functions
2372 // as live to lower the number of iterations. If they happen to be dead, the
2373 // below fixpoint loop will identify and eliminate them.
2375 SmallVector
<Function
*, 8> InternalFns
;
2376 for (Function
*F
: Functions
)
2377 if (F
->hasLocalLinkage() && (isModulePass() || !TLI
->getLibFunc(*F
, LF
)))
2378 InternalFns
.push_back(F
);
2380 SmallPtrSet
<Function
*, 8> LiveInternalFns
;
2381 bool FoundLiveInternal
= true;
2382 while (FoundLiveInternal
) {
2383 FoundLiveInternal
= false;
2384 for (unsigned u
= 0, e
= InternalFns
.size(); u
< e
; ++u
) {
2385 Function
*F
= InternalFns
[u
];
2389 bool UsedAssumedInformation
= false;
2390 if (checkForAllCallSites(
2391 [&](AbstractCallSite ACS
) {
2392 Function
*Callee
= ACS
.getInstruction()->getFunction();
2393 return ToBeDeletedFunctions
.count(Callee
) ||
2394 (Functions
.count(Callee
) && Callee
->hasLocalLinkage() &&
2395 !LiveInternalFns
.count(Callee
));
2397 *F
, true, nullptr, UsedAssumedInformation
)) {
2401 LiveInternalFns
.insert(F
);
2402 InternalFns
[u
] = nullptr;
2403 FoundLiveInternal
= true;
2407 for (unsigned u
= 0, e
= InternalFns
.size(); u
< e
; ++u
)
2408 if (Function
*F
= InternalFns
[u
])
2409 ToBeDeletedFunctions
.insert(F
);
2412 ChangeStatus
Attributor::cleanupIR() {
2413 TimeTraceScope
TimeScope("Attributor::cleanupIR");
2414 // Delete stuff at the end to avoid invalid references and a nice order.
2415 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least "
2416 << ToBeDeletedFunctions
.size() << " functions and "
2417 << ToBeDeletedBlocks
.size() << " blocks and "
2418 << ToBeDeletedInsts
.size() << " instructions and "
2419 << ToBeChangedValues
.size() << " values and "
2420 << ToBeChangedUses
.size() << " uses. To insert "
2421 << ToBeChangedToUnreachableInsts
.size()
2422 << " unreachables.\n"
2423 << "Preserve manifest added " << ManifestAddedBlocks
.size()
2426 SmallVector
<WeakTrackingVH
, 32> DeadInsts
;
2427 SmallVector
<Instruction
*, 32> TerminatorsToFold
;
2429 auto ReplaceUse
= [&](Use
*U
, Value
*NewV
) {
2430 Value
*OldV
= U
->get();
2432 // If we plan to replace NewV we need to update it at this point.
2434 const auto &Entry
= ToBeChangedValues
.lookup(NewV
);
2437 NewV
= get
<0>(Entry
);
2440 Instruction
*I
= dyn_cast
<Instruction
>(U
->getUser());
2441 assert((!I
|| isRunOn(*I
->getFunction())) &&
2442 "Cannot replace an instruction outside the current SCC!");
2444 // Do not replace uses in returns if the value is a must-tail call we will
2446 if (auto *RI
= dyn_cast_or_null
<ReturnInst
>(I
)) {
2447 if (auto *CI
= dyn_cast
<CallInst
>(OldV
->stripPointerCasts()))
2448 if (CI
->isMustTailCall() && !ToBeDeletedInsts
.count(CI
))
2450 // If we rewrite a return and the new value is not an argument, strip the
2451 // `returned` attribute as it is wrong now.
2452 if (!isa
<Argument
>(NewV
))
2453 for (auto &Arg
: RI
->getFunction()->args())
2454 Arg
.removeAttr(Attribute::Returned
);
2457 LLVM_DEBUG(dbgs() << "Use " << *NewV
<< " in " << *U
->getUser()
2458 << " instead of " << *OldV
<< "\n");
2461 if (Instruction
*I
= dyn_cast
<Instruction
>(OldV
)) {
2462 CGModifiedFunctions
.insert(I
->getFunction());
2463 if (!isa
<PHINode
>(I
) && !ToBeDeletedInsts
.count(I
) &&
2464 isInstructionTriviallyDead(I
))
2465 DeadInsts
.push_back(I
);
2467 if (isa
<UndefValue
>(NewV
) && isa
<CallBase
>(U
->getUser())) {
2468 auto *CB
= cast
<CallBase
>(U
->getUser());
2469 if (CB
->isArgOperand(U
)) {
2470 unsigned Idx
= CB
->getArgOperandNo(U
);
2471 CB
->removeParamAttr(Idx
, Attribute::NoUndef
);
2472 auto *Callee
= dyn_cast_if_present
<Function
>(CB
->getCalledOperand());
2473 if (Callee
&& Callee
->arg_size() > Idx
)
2474 Callee
->removeParamAttr(Idx
, Attribute::NoUndef
);
2477 if (isa
<Constant
>(NewV
) && isa
<BranchInst
>(U
->getUser())) {
2478 Instruction
*UserI
= cast
<Instruction
>(U
->getUser());
2479 if (isa
<UndefValue
>(NewV
)) {
2480 ToBeChangedToUnreachableInsts
.insert(UserI
);
2482 TerminatorsToFold
.push_back(UserI
);
2487 for (auto &It
: ToBeChangedUses
) {
2489 Value
*NewV
= It
.second
;
2490 ReplaceUse(U
, NewV
);
2493 SmallVector
<Use
*, 4> Uses
;
2494 for (auto &It
: ToBeChangedValues
) {
2495 Value
*OldV
= It
.first
;
2496 auto [NewV
, Done
] = It
.second
;
2498 for (auto &U
: OldV
->uses())
2499 if (Done
|| !U
.getUser()->isDroppable())
2501 for (Use
*U
: Uses
) {
2502 if (auto *I
= dyn_cast
<Instruction
>(U
->getUser()))
2503 if (!isRunOn(*I
->getFunction()))
2505 ReplaceUse(U
, NewV
);
2509 for (const auto &V
: InvokeWithDeadSuccessor
)
2510 if (InvokeInst
*II
= dyn_cast_or_null
<InvokeInst
>(V
)) {
2511 assert(isRunOn(*II
->getFunction()) &&
2512 "Cannot replace an invoke outside the current SCC!");
2513 bool UnwindBBIsDead
= II
->hasFnAttr(Attribute::NoUnwind
);
2514 bool NormalBBIsDead
= II
->hasFnAttr(Attribute::NoReturn
);
2515 bool Invoke2CallAllowed
=
2516 !AAIsDead::mayCatchAsynchronousExceptions(*II
->getFunction());
2517 assert((UnwindBBIsDead
|| NormalBBIsDead
) &&
2518 "Invoke does not have dead successors!");
2519 BasicBlock
*BB
= II
->getParent();
2520 BasicBlock
*NormalDestBB
= II
->getNormalDest();
2521 if (UnwindBBIsDead
) {
2522 Instruction
*NormalNextIP
= &NormalDestBB
->front();
2523 if (Invoke2CallAllowed
) {
2525 NormalNextIP
= BB
->getTerminator();
2528 ToBeChangedToUnreachableInsts
.insert(NormalNextIP
);
2530 assert(NormalBBIsDead
&& "Broken invariant!");
2531 if (!NormalDestBB
->getUniquePredecessor())
2532 NormalDestBB
= SplitBlockPredecessors(NormalDestBB
, {BB
}, ".dead");
2533 ToBeChangedToUnreachableInsts
.insert(&NormalDestBB
->front());
2536 for (Instruction
*I
: TerminatorsToFold
) {
2537 assert(isRunOn(*I
->getFunction()) &&
2538 "Cannot replace a terminator outside the current SCC!");
2539 CGModifiedFunctions
.insert(I
->getFunction());
2540 ConstantFoldTerminator(I
->getParent());
2542 for (const auto &V
: ToBeChangedToUnreachableInsts
)
2543 if (Instruction
*I
= dyn_cast_or_null
<Instruction
>(V
)) {
2544 LLVM_DEBUG(dbgs() << "[Attributor] Change to unreachable: " << *I
2546 assert(isRunOn(*I
->getFunction()) &&
2547 "Cannot replace an instruction outside the current SCC!");
2548 CGModifiedFunctions
.insert(I
->getFunction());
2549 changeToUnreachable(I
);
2552 for (const auto &V
: ToBeDeletedInsts
) {
2553 if (Instruction
*I
= dyn_cast_or_null
<Instruction
>(V
)) {
2554 if (auto *CB
= dyn_cast
<CallBase
>(I
)) {
2555 assert((isa
<IntrinsicInst
>(CB
) || isRunOn(*I
->getFunction())) &&
2556 "Cannot delete an instruction outside the current SCC!");
2557 if (!isa
<IntrinsicInst
>(CB
))
2558 Configuration
.CGUpdater
.removeCallSite(*CB
);
2560 I
->dropDroppableUses();
2561 CGModifiedFunctions
.insert(I
->getFunction());
2562 if (!I
->getType()->isVoidTy())
2563 I
->replaceAllUsesWith(UndefValue::get(I
->getType()));
2564 if (!isa
<PHINode
>(I
) && isInstructionTriviallyDead(I
))
2565 DeadInsts
.push_back(I
);
2567 I
->eraseFromParent();
2571 llvm::erase_if(DeadInsts
, [&](WeakTrackingVH I
) { return !I
; });
2574 dbgs() << "[Attributor] DeadInsts size: " << DeadInsts
.size() << "\n";
2575 for (auto &I
: DeadInsts
)
2577 dbgs() << " - " << *I
<< "\n";
2580 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts
);
2582 if (unsigned NumDeadBlocks
= ToBeDeletedBlocks
.size()) {
2583 SmallVector
<BasicBlock
*, 8> ToBeDeletedBBs
;
2584 ToBeDeletedBBs
.reserve(NumDeadBlocks
);
2585 for (BasicBlock
*BB
: ToBeDeletedBlocks
) {
2586 assert(isRunOn(*BB
->getParent()) &&
2587 "Cannot delete a block outside the current SCC!");
2588 CGModifiedFunctions
.insert(BB
->getParent());
2589 // Do not delete BBs added during manifests of AAs.
2590 if (ManifestAddedBlocks
.contains(BB
))
2592 ToBeDeletedBBs
.push_back(BB
);
2594 // Actually we do not delete the blocks but squash them into a single
2595 // unreachable but untangling branches that jump here is something we need
2596 // to do in a more generic way.
2597 detachDeadBlocks(ToBeDeletedBBs
, nullptr);
2600 identifyDeadInternalFunctions();
2602 // Rewrite the functions as requested during manifest.
2603 ChangeStatus ManifestChange
= rewriteFunctionSignatures(CGModifiedFunctions
);
2605 for (Function
*Fn
: CGModifiedFunctions
)
2606 if (!ToBeDeletedFunctions
.count(Fn
) && Functions
.count(Fn
))
2607 Configuration
.CGUpdater
.reanalyzeFunction(*Fn
);
2609 for (Function
*Fn
: ToBeDeletedFunctions
) {
2610 if (!Functions
.count(Fn
))
2612 Configuration
.CGUpdater
.removeFunction(*Fn
);
2615 if (!ToBeChangedUses
.empty())
2616 ManifestChange
= ChangeStatus::CHANGED
;
2618 if (!ToBeChangedToUnreachableInsts
.empty())
2619 ManifestChange
= ChangeStatus::CHANGED
;
2621 if (!ToBeDeletedFunctions
.empty())
2622 ManifestChange
= ChangeStatus::CHANGED
;
2624 if (!ToBeDeletedBlocks
.empty())
2625 ManifestChange
= ChangeStatus::CHANGED
;
2627 if (!ToBeDeletedInsts
.empty())
2628 ManifestChange
= ChangeStatus::CHANGED
;
2630 if (!InvokeWithDeadSuccessor
.empty())
2631 ManifestChange
= ChangeStatus::CHANGED
;
2633 if (!DeadInsts
.empty())
2634 ManifestChange
= ChangeStatus::CHANGED
;
2636 NumFnDeleted
+= ToBeDeletedFunctions
.size();
2638 LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions
.size()
2639 << " functions after manifest.\n");
2641 #ifdef EXPENSIVE_CHECKS
2642 for (Function
*F
: Functions
) {
2643 if (ToBeDeletedFunctions
.count(F
))
2645 assert(!verifyFunction(*F
, &errs()) && "Module verification failed!");
2649 return ManifestChange
;
2652 ChangeStatus
Attributor::run() {
2653 TimeTraceScope
TimeScope("Attributor::run");
2654 AttributorCallGraph
ACallGraph(*this);
2657 ACallGraph
.populateAll();
2659 Phase
= AttributorPhase::UPDATE
;
2662 // dump graphs on demand
2669 if (PrintDependencies
)
2672 Phase
= AttributorPhase::MANIFEST
;
2673 ChangeStatus ManifestChange
= manifestAttributes();
2675 Phase
= AttributorPhase::CLEANUP
;
2676 ChangeStatus CleanupChange
= cleanupIR();
2681 return ManifestChange
| CleanupChange
;
2684 ChangeStatus
Attributor::updateAA(AbstractAttribute
&AA
) {
2685 TimeTraceScope
TimeScope("updateAA", [&]() {
2686 return AA
.getName() + std::to_string(AA
.getIRPosition().getPositionKind());
2688 assert(Phase
== AttributorPhase::UPDATE
&&
2689 "We can update AA only in the update stage!");
2691 // Use a new dependence vector for this update.
2692 DependenceVector DV
;
2693 DependenceStack
.push_back(&DV
);
2695 auto &AAState
= AA
.getState();
2696 ChangeStatus CS
= ChangeStatus::UNCHANGED
;
2697 bool UsedAssumedInformation
= false;
2698 if (!isAssumedDead(AA
, nullptr, UsedAssumedInformation
,
2699 /* CheckBBLivenessOnly */ true))
2700 CS
= AA
.update(*this);
2702 if (!AA
.isQueryAA() && DV
.empty() && !AA
.getState().isAtFixpoint()) {
2703 // If the AA did not rely on outside information but changed, we run it
2704 // again to see if it found a fixpoint. Most AAs do but we don't require
2705 // them to. Hence, it might take the AA multiple iterations to get to a
2706 // fixpoint even if it does not rely on outside information, which is fine.
2707 ChangeStatus RerunCS
= ChangeStatus::UNCHANGED
;
2708 if (CS
== ChangeStatus::CHANGED
)
2709 RerunCS
= AA
.update(*this);
2711 // If the attribute did not change during the run or rerun, and it still did
2712 // not query any non-fix information, the state will not change and we can
2713 // indicate that right at this point.
2714 if (RerunCS
== ChangeStatus::UNCHANGED
&& !AA
.isQueryAA() && DV
.empty())
2715 AAState
.indicateOptimisticFixpoint();
2718 if (!AAState
.isAtFixpoint())
2719 rememberDependences();
2721 // Verify the stack was used properly, that is we pop the dependence vector we
2722 // put there earlier.
2723 DependenceVector
*PoppedDV
= DependenceStack
.pop_back_val();
2725 assert(PoppedDV
== &DV
&& "Inconsistent usage of the dependence stack!");
2730 void Attributor::createShallowWrapper(Function
&F
) {
2731 assert(!F
.isDeclaration() && "Cannot create a wrapper around a declaration!");
2733 Module
&M
= *F
.getParent();
2734 LLVMContext
&Ctx
= M
.getContext();
2735 FunctionType
*FnTy
= F
.getFunctionType();
2738 Function::Create(FnTy
, F
.getLinkage(), F
.getAddressSpace(), F
.getName());
2739 F
.setName(""); // set the inside function anonymous
2740 M
.getFunctionList().insert(F
.getIterator(), Wrapper
);
2742 F
.setLinkage(GlobalValue::InternalLinkage
);
2744 F
.replaceAllUsesWith(Wrapper
);
2745 assert(F
.use_empty() && "Uses remained after wrapper was created!");
2747 // Move the COMDAT section to the wrapper.
2748 // TODO: Check if we need to keep it for F as well.
2749 Wrapper
->setComdat(F
.getComdat());
2750 F
.setComdat(nullptr);
2752 // Copy all metadata and attributes but keep them on F as well.
2753 SmallVector
<std::pair
<unsigned, MDNode
*>, 1> MDs
;
2754 F
.getAllMetadata(MDs
);
2755 for (auto MDIt
: MDs
)
2756 Wrapper
->addMetadata(MDIt
.first
, *MDIt
.second
);
2757 Wrapper
->setAttributes(F
.getAttributes());
2759 // Create the call in the wrapper.
2760 BasicBlock
*EntryBB
= BasicBlock::Create(Ctx
, "entry", Wrapper
);
2762 SmallVector
<Value
*, 8> Args
;
2763 Argument
*FArgIt
= F
.arg_begin();
2764 for (Argument
&Arg
: Wrapper
->args()) {
2765 Args
.push_back(&Arg
);
2766 Arg
.setName((FArgIt
++)->getName());
2769 CallInst
*CI
= CallInst::Create(&F
, Args
, "", EntryBB
);
2770 CI
->setTailCall(true);
2771 CI
->addFnAttr(Attribute::NoInline
);
2772 ReturnInst::Create(Ctx
, CI
->getType()->isVoidTy() ? nullptr : CI
, EntryBB
);
2774 NumFnShallowWrappersCreated
++;
2777 bool Attributor::isInternalizable(Function
&F
) {
2778 if (F
.isDeclaration() || F
.hasLocalLinkage() ||
2779 GlobalValue::isInterposableLinkage(F
.getLinkage()))
2784 Function
*Attributor::internalizeFunction(Function
&F
, bool Force
) {
2785 if (!AllowDeepWrapper
&& !Force
)
2787 if (!isInternalizable(F
))
2790 SmallPtrSet
<Function
*, 2> FnSet
= {&F
};
2791 DenseMap
<Function
*, Function
*> InternalizedFns
;
2792 internalizeFunctions(FnSet
, InternalizedFns
);
2794 return InternalizedFns
[&F
];
2797 bool Attributor::internalizeFunctions(SmallPtrSetImpl
<Function
*> &FnSet
,
2798 DenseMap
<Function
*, Function
*> &FnMap
) {
2799 for (Function
*F
: FnSet
)
2800 if (!Attributor::isInternalizable(*F
))
2804 // Generate the internalized version of each function.
2805 for (Function
*F
: FnSet
) {
2806 Module
&M
= *F
->getParent();
2807 FunctionType
*FnTy
= F
->getFunctionType();
2809 // Create a copy of the current function
2811 Function::Create(FnTy
, F
->getLinkage(), F
->getAddressSpace(),
2812 F
->getName() + ".internalized");
2813 ValueToValueMapTy VMap
;
2814 auto *NewFArgIt
= Copied
->arg_begin();
2815 for (auto &Arg
: F
->args()) {
2816 auto ArgName
= Arg
.getName();
2817 NewFArgIt
->setName(ArgName
);
2818 VMap
[&Arg
] = &(*NewFArgIt
++);
2820 SmallVector
<ReturnInst
*, 8> Returns
;
2822 // Copy the body of the original function to the new one
2823 CloneFunctionInto(Copied
, F
, VMap
,
2824 CloneFunctionChangeType::LocalChangesOnly
, Returns
);
2826 // Set the linakage and visibility late as CloneFunctionInto has some
2827 // implicit requirements.
2828 Copied
->setVisibility(GlobalValue::DefaultVisibility
);
2829 Copied
->setLinkage(GlobalValue::PrivateLinkage
);
2832 SmallVector
<std::pair
<unsigned, MDNode
*>, 1> MDs
;
2833 F
->getAllMetadata(MDs
);
2834 for (auto MDIt
: MDs
)
2835 if (!Copied
->hasMetadata())
2836 Copied
->addMetadata(MDIt
.first
, *MDIt
.second
);
2838 M
.getFunctionList().insert(F
->getIterator(), Copied
);
2839 Copied
->setDSOLocal(true);
2843 // Replace all uses of the old function with the new internalized function
2844 // unless the caller is a function that was just internalized.
2845 for (Function
*F
: FnSet
) {
2846 auto &InternalizedFn
= FnMap
[F
];
2847 auto IsNotInternalized
= [&](Use
&U
) -> bool {
2848 if (auto *CB
= dyn_cast
<CallBase
>(U
.getUser()))
2849 return !FnMap
.lookup(CB
->getCaller());
2852 F
->replaceUsesWithIf(InternalizedFn
, IsNotInternalized
);
2858 bool Attributor::isValidFunctionSignatureRewrite(
2859 Argument
&Arg
, ArrayRef
<Type
*> ReplacementTypes
) {
2861 if (!Configuration
.RewriteSignatures
)
2864 Function
*Fn
= Arg
.getParent();
2865 auto CallSiteCanBeChanged
= [Fn
](AbstractCallSite ACS
) {
2866 // Forbid the call site to cast the function return type. If we need to
2867 // rewrite these functions we need to re-create a cast for the new call site
2868 // (if the old had uses).
2869 if (!ACS
.getCalledFunction() ||
2870 ACS
.getInstruction()->getType() !=
2871 ACS
.getCalledFunction()->getReturnType())
2873 if (cast
<CallBase
>(ACS
.getInstruction())->getCalledOperand()->getType() !=
2876 if (ACS
.getNumArgOperands() != Fn
->arg_size())
2878 // Forbid must-tail calls for now.
2879 return !ACS
.isCallbackCall() && !ACS
.getInstruction()->isMustTailCall();
2882 // Avoid var-arg functions for now.
2883 if (Fn
->isVarArg()) {
2884 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
2888 // Avoid functions with complicated argument passing semantics.
2889 AttributeList FnAttributeList
= Fn
->getAttributes();
2890 if (FnAttributeList
.hasAttrSomewhere(Attribute::Nest
) ||
2891 FnAttributeList
.hasAttrSomewhere(Attribute::StructRet
) ||
2892 FnAttributeList
.hasAttrSomewhere(Attribute::InAlloca
) ||
2893 FnAttributeList
.hasAttrSomewhere(Attribute::Preallocated
)) {
2895 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
2899 // Avoid callbacks for now.
2900 bool UsedAssumedInformation
= false;
2901 if (!checkForAllCallSites(CallSiteCanBeChanged
, *Fn
, true, nullptr,
2902 UsedAssumedInformation
,
2903 /* CheckPotentiallyDead */ true)) {
2904 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
2908 auto InstPred
= [](Instruction
&I
) {
2909 if (auto *CI
= dyn_cast
<CallInst
>(&I
))
2910 return !CI
->isMustTailCall();
2914 // Forbid must-tail calls for now.
2916 auto &OpcodeInstMap
= InfoCache
.getOpcodeInstMapForFunction(*Fn
);
2917 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap
, InstPred
, nullptr,
2918 nullptr, {Instruction::Call
},
2919 UsedAssumedInformation
)) {
2920 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
2927 bool Attributor::registerFunctionSignatureRewrite(
2928 Argument
&Arg
, ArrayRef
<Type
*> ReplacementTypes
,
2929 ArgumentReplacementInfo::CalleeRepairCBTy
&&CalleeRepairCB
,
2930 ArgumentReplacementInfo::ACSRepairCBTy
&&ACSRepairCB
) {
2931 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg
<< " in "
2932 << Arg
.getParent()->getName() << " with "
2933 << ReplacementTypes
.size() << " replacements\n");
2934 assert(isValidFunctionSignatureRewrite(Arg
, ReplacementTypes
) &&
2935 "Cannot register an invalid rewrite");
2937 Function
*Fn
= Arg
.getParent();
2938 SmallVectorImpl
<std::unique_ptr
<ArgumentReplacementInfo
>> &ARIs
=
2939 ArgumentReplacementMap
[Fn
];
2941 ARIs
.resize(Fn
->arg_size());
2943 // If we have a replacement already with less than or equal new arguments,
2944 // ignore this request.
2945 std::unique_ptr
<ArgumentReplacementInfo
> &ARI
= ARIs
[Arg
.getArgNo()];
2946 if (ARI
&& ARI
->getNumReplacementArgs() <= ReplacementTypes
.size()) {
2947 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
2951 // If we have a replacement already but we like the new one better, delete
2955 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg
<< " in "
2956 << Arg
.getParent()->getName() << " with "
2957 << ReplacementTypes
.size() << " replacements\n");
2959 // Remember the replacement.
2960 ARI
.reset(new ArgumentReplacementInfo(*this, Arg
, ReplacementTypes
,
2961 std::move(CalleeRepairCB
),
2962 std::move(ACSRepairCB
)));
2967 bool Attributor::shouldSeedAttribute(AbstractAttribute
&AA
) {
2970 if (SeedAllowList
.size() != 0)
2971 Result
= llvm::is_contained(SeedAllowList
, AA
.getName());
2972 Function
*Fn
= AA
.getAnchorScope();
2973 if (FunctionSeedAllowList
.size() != 0 && Fn
)
2974 Result
&= llvm::is_contained(FunctionSeedAllowList
, Fn
->getName());
2979 ChangeStatus
Attributor::rewriteFunctionSignatures(
2980 SmallSetVector
<Function
*, 8> &ModifiedFns
) {
2981 ChangeStatus Changed
= ChangeStatus::UNCHANGED
;
2983 for (auto &It
: ArgumentReplacementMap
) {
2984 Function
*OldFn
= It
.getFirst();
2986 // Deleted functions do not require rewrites.
2987 if (!Functions
.count(OldFn
) || ToBeDeletedFunctions
.count(OldFn
))
2990 const SmallVectorImpl
<std::unique_ptr
<ArgumentReplacementInfo
>> &ARIs
=
2992 assert(ARIs
.size() == OldFn
->arg_size() && "Inconsistent state!");
2994 SmallVector
<Type
*, 16> NewArgumentTypes
;
2995 SmallVector
<AttributeSet
, 16> NewArgumentAttributes
;
2997 // Collect replacement argument types and copy over existing attributes.
2998 AttributeList OldFnAttributeList
= OldFn
->getAttributes();
2999 for (Argument
&Arg
: OldFn
->args()) {
3000 if (const std::unique_ptr
<ArgumentReplacementInfo
> &ARI
=
3001 ARIs
[Arg
.getArgNo()]) {
3002 NewArgumentTypes
.append(ARI
->ReplacementTypes
.begin(),
3003 ARI
->ReplacementTypes
.end());
3004 NewArgumentAttributes
.append(ARI
->getNumReplacementArgs(),
3007 NewArgumentTypes
.push_back(Arg
.getType());
3008 NewArgumentAttributes
.push_back(
3009 OldFnAttributeList
.getParamAttrs(Arg
.getArgNo()));
3013 uint64_t LargestVectorWidth
= 0;
3014 for (auto *I
: NewArgumentTypes
)
3015 if (auto *VT
= dyn_cast
<llvm::VectorType
>(I
))
3016 LargestVectorWidth
=
3017 std::max(LargestVectorWidth
,
3018 VT
->getPrimitiveSizeInBits().getKnownMinValue());
3020 FunctionType
*OldFnTy
= OldFn
->getFunctionType();
3021 Type
*RetTy
= OldFnTy
->getReturnType();
3023 // Construct the new function type using the new arguments types.
3024 FunctionType
*NewFnTy
=
3025 FunctionType::get(RetTy
, NewArgumentTypes
, OldFnTy
->isVarArg());
3027 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn
->getName()
3028 << "' from " << *OldFn
->getFunctionType() << " to "
3029 << *NewFnTy
<< "\n");
3031 // Create the new function body and insert it into the module.
3032 Function
*NewFn
= Function::Create(NewFnTy
, OldFn
->getLinkage(),
3033 OldFn
->getAddressSpace(), "");
3034 Functions
.insert(NewFn
);
3035 OldFn
->getParent()->getFunctionList().insert(OldFn
->getIterator(), NewFn
);
3036 NewFn
->takeName(OldFn
);
3037 NewFn
->copyAttributesFrom(OldFn
);
3039 // Patch the pointer to LLVM function in debug info descriptor.
3040 NewFn
->setSubprogram(OldFn
->getSubprogram());
3041 OldFn
->setSubprogram(nullptr);
3043 // Recompute the parameter attributes list based on the new arguments for
3045 LLVMContext
&Ctx
= OldFn
->getContext();
3046 NewFn
->setAttributes(AttributeList::get(
3047 Ctx
, OldFnAttributeList
.getFnAttrs(), OldFnAttributeList
.getRetAttrs(),
3048 NewArgumentAttributes
));
3049 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewFn
, LargestVectorWidth
);
3051 // Remove argmem from the memory effects if we have no more pointer
3052 // arguments, or they are readnone.
3053 MemoryEffects ME
= NewFn
->getMemoryEffects();
3055 if (ME
.doesAccessArgPointees() && all_of(NewArgumentTypes
, [&](Type
*T
) {
3057 return !T
->isPtrOrPtrVectorTy() ||
3058 NewFn
->hasParamAttribute(ArgNo
, Attribute::ReadNone
);
3060 NewFn
->setMemoryEffects(ME
- MemoryEffects::argMemOnly());
3063 // Since we have now created the new function, splice the body of the old
3064 // function right into the new function, leaving the old rotting hulk of the
3066 NewFn
->splice(NewFn
->begin(), OldFn
);
3068 // Fixup block addresses to reference new function.
3069 SmallVector
<BlockAddress
*, 8u> BlockAddresses
;
3070 for (User
*U
: OldFn
->users())
3071 if (auto *BA
= dyn_cast
<BlockAddress
>(U
))
3072 BlockAddresses
.push_back(BA
);
3073 for (auto *BA
: BlockAddresses
)
3074 BA
->replaceAllUsesWith(BlockAddress::get(NewFn
, BA
->getBasicBlock()));
3076 // Set of all "call-like" instructions that invoke the old function mapped
3077 // to their new replacements.
3078 SmallVector
<std::pair
<CallBase
*, CallBase
*>, 8> CallSitePairs
;
3080 // Callback to create a new "call-like" instruction for a given one.
3081 auto CallSiteReplacementCreator
= [&](AbstractCallSite ACS
) {
3082 CallBase
*OldCB
= cast
<CallBase
>(ACS
.getInstruction());
3083 const AttributeList
&OldCallAttributeList
= OldCB
->getAttributes();
3085 // Collect the new argument operands for the replacement call site.
3086 SmallVector
<Value
*, 16> NewArgOperands
;
3087 SmallVector
<AttributeSet
, 16> NewArgOperandAttributes
;
3088 for (unsigned OldArgNum
= 0; OldArgNum
< ARIs
.size(); ++OldArgNum
) {
3089 unsigned NewFirstArgNum
= NewArgOperands
.size();
3090 (void)NewFirstArgNum
; // only used inside assert.
3091 if (const std::unique_ptr
<ArgumentReplacementInfo
> &ARI
=
3093 if (ARI
->ACSRepairCB
)
3094 ARI
->ACSRepairCB(*ARI
, ACS
, NewArgOperands
);
3095 assert(ARI
->getNumReplacementArgs() + NewFirstArgNum
==
3096 NewArgOperands
.size() &&
3097 "ACS repair callback did not provide as many operand as new "
3098 "types were registered!");
3099 // TODO: Exose the attribute set to the ACS repair callback
3100 NewArgOperandAttributes
.append(ARI
->ReplacementTypes
.size(),
3103 NewArgOperands
.push_back(ACS
.getCallArgOperand(OldArgNum
));
3104 NewArgOperandAttributes
.push_back(
3105 OldCallAttributeList
.getParamAttrs(OldArgNum
));
3109 assert(NewArgOperands
.size() == NewArgOperandAttributes
.size() &&
3110 "Mismatch # argument operands vs. # argument operand attributes!");
3111 assert(NewArgOperands
.size() == NewFn
->arg_size() &&
3112 "Mismatch # argument operands vs. # function arguments!");
3114 SmallVector
<OperandBundleDef
, 4> OperandBundleDefs
;
3115 OldCB
->getOperandBundlesAsDefs(OperandBundleDefs
);
3117 // Create a new call or invoke instruction to replace the old one.
3119 if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(OldCB
)) {
3121 InvokeInst::Create(NewFn
, II
->getNormalDest(), II
->getUnwindDest(),
3122 NewArgOperands
, OperandBundleDefs
, "", OldCB
);
3124 auto *NewCI
= CallInst::Create(NewFn
, NewArgOperands
, OperandBundleDefs
,
3126 NewCI
->setTailCallKind(cast
<CallInst
>(OldCB
)->getTailCallKind());
3130 // Copy over various properties and the new attributes.
3131 NewCB
->copyMetadata(*OldCB
, {LLVMContext::MD_prof
, LLVMContext::MD_dbg
});
3132 NewCB
->setCallingConv(OldCB
->getCallingConv());
3133 NewCB
->takeName(OldCB
);
3134 NewCB
->setAttributes(AttributeList::get(
3135 Ctx
, OldCallAttributeList
.getFnAttrs(),
3136 OldCallAttributeList
.getRetAttrs(), NewArgOperandAttributes
));
3138 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewCB
->getCaller(),
3139 LargestVectorWidth
);
3141 CallSitePairs
.push_back({OldCB
, NewCB
});
3145 // Use the CallSiteReplacementCreator to create replacement call sites.
3146 bool UsedAssumedInformation
= false;
3147 bool Success
= checkForAllCallSites(CallSiteReplacementCreator
, *OldFn
,
3148 true, nullptr, UsedAssumedInformation
,
3149 /* CheckPotentiallyDead */ true);
3151 assert(Success
&& "Assumed call site replacement to succeed!");
3153 // Rewire the arguments.
3154 Argument
*OldFnArgIt
= OldFn
->arg_begin();
3155 Argument
*NewFnArgIt
= NewFn
->arg_begin();
3156 for (unsigned OldArgNum
= 0; OldArgNum
< ARIs
.size();
3157 ++OldArgNum
, ++OldFnArgIt
) {
3158 if (const std::unique_ptr
<ArgumentReplacementInfo
> &ARI
=
3160 if (ARI
->CalleeRepairCB
)
3161 ARI
->CalleeRepairCB(*ARI
, *NewFn
, NewFnArgIt
);
3162 if (ARI
->ReplacementTypes
.empty())
3163 OldFnArgIt
->replaceAllUsesWith(
3164 PoisonValue::get(OldFnArgIt
->getType()));
3165 NewFnArgIt
+= ARI
->ReplacementTypes
.size();
3167 NewFnArgIt
->takeName(&*OldFnArgIt
);
3168 OldFnArgIt
->replaceAllUsesWith(&*NewFnArgIt
);
3173 // Eliminate the instructions *after* we visited all of them.
3174 for (auto &CallSitePair
: CallSitePairs
) {
3175 CallBase
&OldCB
= *CallSitePair
.first
;
3176 CallBase
&NewCB
= *CallSitePair
.second
;
3177 assert(OldCB
.getType() == NewCB
.getType() &&
3178 "Cannot handle call sites with different types!");
3179 ModifiedFns
.insert(OldCB
.getFunction());
3180 Configuration
.CGUpdater
.replaceCallSite(OldCB
, NewCB
);
3181 OldCB
.replaceAllUsesWith(&NewCB
);
3182 OldCB
.eraseFromParent();
3185 // Replace the function in the call graph (if any).
3186 Configuration
.CGUpdater
.replaceFunctionWith(*OldFn
, *NewFn
);
3188 // If the old function was modified and needed to be reanalyzed, the new one
3190 if (ModifiedFns
.remove(OldFn
))
3191 ModifiedFns
.insert(NewFn
);
3193 Changed
= ChangeStatus::CHANGED
;
3199 void InformationCache::initializeInformationCache(const Function
&CF
,
3201 // As we do not modify the function here we can remove the const
3202 // withouth breaking implicit assumptions. At the end of the day, we could
3203 // initialize the cache eagerly which would look the same to the users.
3204 Function
&F
= const_cast<Function
&>(CF
);
3206 // Walk all instructions to find interesting instructions that might be
3207 // queried by abstract attributes during their initialization or update.
3208 // This has to happen before we create attributes.
3210 DenseMap
<const Value
*, std::optional
<short>> AssumeUsesMap
;
3212 // Add \p V to the assume uses map which track the number of uses outside of
3213 // "visited" assumes. If no outside uses are left the value is added to the
3214 // assume only use vector.
3215 auto AddToAssumeUsesMap
= [&](const Value
&V
) -> void {
3216 SmallVector
<const Instruction
*> Worklist
;
3217 if (auto *I
= dyn_cast
<Instruction
>(&V
))
3218 Worklist
.push_back(I
);
3219 while (!Worklist
.empty()) {
3220 const Instruction
*I
= Worklist
.pop_back_val();
3221 std::optional
<short> &NumUses
= AssumeUsesMap
[I
];
3223 NumUses
= I
->getNumUses();
3224 NumUses
= *NumUses
- /* this assume */ 1;
3227 AssumeOnlyValues
.insert(I
);
3228 for (const Value
*Op
: I
->operands())
3229 if (auto *OpI
= dyn_cast
<Instruction
>(Op
))
3230 Worklist
.push_back(OpI
);
3234 for (Instruction
&I
: instructions(&F
)) {
3235 bool IsInterestingOpcode
= false;
3237 // To allow easy access to all instructions in a function with a given
3238 // opcode we store them in the InfoCache. As not all opcodes are interesting
3239 // to concrete attributes we only cache the ones that are as identified in
3240 // the following switch.
3241 // Note: There are no concrete attributes now so this is initially empty.
3242 switch (I
.getOpcode()) {
3244 assert(!isa
<CallBase
>(&I
) &&
3245 "New call base instruction type needs to be known in the "
3248 case Instruction::Call
:
3249 // Calls are interesting on their own, additionally:
3250 // For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
3251 // For `must-tail` calls we remember the caller and callee.
3252 if (auto *Assume
= dyn_cast
<AssumeInst
>(&I
)) {
3253 AssumeOnlyValues
.insert(Assume
);
3254 fillMapFromAssume(*Assume
, KnowledgeMap
);
3255 AddToAssumeUsesMap(*Assume
->getArgOperand(0));
3256 } else if (cast
<CallInst
>(I
).isMustTailCall()) {
3257 FI
.ContainsMustTailCall
= true;
3258 if (auto *Callee
= dyn_cast_if_present
<Function
>(
3259 cast
<CallInst
>(I
).getCalledOperand()))
3260 getFunctionInfo(*Callee
).CalledViaMustTail
= true;
3263 case Instruction::CallBr
:
3264 case Instruction::Invoke
:
3265 case Instruction::CleanupRet
:
3266 case Instruction::CatchSwitch
:
3267 case Instruction::AtomicRMW
:
3268 case Instruction::AtomicCmpXchg
:
3269 case Instruction::Br
:
3270 case Instruction::Resume
:
3271 case Instruction::Ret
:
3272 case Instruction::Load
:
3273 // The alignment of a pointer is interesting for loads.
3274 case Instruction::Store
:
3275 // The alignment of a pointer is interesting for stores.
3276 case Instruction::Alloca
:
3277 case Instruction::AddrSpaceCast
:
3278 IsInterestingOpcode
= true;
3280 if (IsInterestingOpcode
) {
3281 auto *&Insts
= FI
.OpcodeInstMap
[I
.getOpcode()];
3283 Insts
= new (Allocator
) InstructionVectorTy();
3284 Insts
->push_back(&I
);
3286 if (I
.mayReadOrWriteMemory())
3287 FI
.RWInsts
.push_back(&I
);
3290 if (F
.hasFnAttribute(Attribute::AlwaysInline
) &&
3291 isInlineViable(F
).isSuccess())
3292 InlineableFunctions
.insert(&F
);
3295 InformationCache::FunctionInfo::~FunctionInfo() {
3296 // The instruction vectors are allocated using a BumpPtrAllocator, we need to
3297 // manually destroy them.
3298 for (auto &It
: OpcodeInstMap
)
3299 It
.getSecond()->~InstructionVectorTy();
3302 const ArrayRef
<Function
*>
3303 InformationCache::getIndirectlyCallableFunctions(Attributor
&A
) const {
3304 assert(A
.isClosedWorldModule() && "Cannot see all indirect callees!");
3305 return IndirectlyCallableFunctions
;
3308 void Attributor::recordDependence(const AbstractAttribute
&FromAA
,
3309 const AbstractAttribute
&ToAA
,
3310 DepClassTy DepClass
) {
3311 if (DepClass
== DepClassTy::NONE
)
3313 // If we are outside of an update, thus before the actual fixpoint iteration
3314 // started (= when we create AAs), we do not track dependences because we will
3315 // put all AAs into the initial worklist anyway.
3316 if (DependenceStack
.empty())
3318 if (FromAA
.getState().isAtFixpoint())
3320 DependenceStack
.back()->push_back({&FromAA
, &ToAA
, DepClass
});
3323 void Attributor::rememberDependences() {
3324 assert(!DependenceStack
.empty() && "No dependences to remember!");
3326 for (DepInfo
&DI
: *DependenceStack
.back()) {
3327 assert((DI
.DepClass
== DepClassTy::REQUIRED
||
3328 DI
.DepClass
== DepClassTy::OPTIONAL
) &&
3329 "Expected required or optional dependence (1 bit)!");
3330 auto &DepAAs
= const_cast<AbstractAttribute
&>(*DI
.FromAA
).Deps
;
3331 DepAAs
.insert(AbstractAttribute::DepTy(
3332 const_cast<AbstractAttribute
*>(DI
.ToAA
), unsigned(DI
.DepClass
)));
3336 template <Attribute::AttrKind AK
, typename AAType
>
3337 void Attributor::checkAndQueryIRAttr(const IRPosition
&IRP
,
3338 AttributeSet Attrs
) {
3340 if (!Attrs
.hasAttribute(AK
))
3341 if (!Configuration
.Allowed
|| Configuration
.Allowed
->count(&AAType::ID
))
3342 if (!AA::hasAssumedIRAttr
<AK
>(*this, nullptr, IRP
, DepClassTy::NONE
,
3344 getOrCreateAAFor
<AAType
>(IRP
);
3347 void Attributor::identifyDefaultAbstractAttributes(Function
&F
) {
3348 if (!VisitedFunctions
.insert(&F
).second
)
3350 if (F
.isDeclaration())
3353 // In non-module runs we need to look at the call sites of a function to
3354 // determine if it is part of a must-tail call edge. This will influence what
3355 // attributes we can derive.
3356 InformationCache::FunctionInfo
&FI
= InfoCache
.getFunctionInfo(F
);
3357 if (!isModulePass() && !FI
.CalledViaMustTail
) {
3358 for (const Use
&U
: F
.uses())
3359 if (const auto *CB
= dyn_cast
<CallBase
>(U
.getUser()))
3360 if (CB
->isCallee(&U
) && CB
->isMustTailCall())
3361 FI
.CalledViaMustTail
= true;
3364 IRPosition FPos
= IRPosition::function(F
);
3365 bool IsIPOAmendable
= isFunctionIPOAmendable(F
);
3366 auto Attrs
= F
.getAttributes();
3367 auto FnAttrs
= Attrs
.getFnAttrs();
3369 // Check for dead BasicBlocks in every function.
3370 // We need dead instruction detection because we do not want to deal with
3371 // broken IR in which SSA rules do not apply.
3372 getOrCreateAAFor
<AAIsDead
>(FPos
);
3374 // Every function might contain instructions that cause "undefined
3376 getOrCreateAAFor
<AAUndefinedBehavior
>(FPos
);
3378 // Every function might be applicable for Heap-To-Stack conversion.
3379 if (EnableHeapToStack
)
3380 getOrCreateAAFor
<AAHeapToStack
>(FPos
);
3382 // Every function might be "must-progress".
3383 checkAndQueryIRAttr
<Attribute::MustProgress
, AAMustProgress
>(FPos
, FnAttrs
);
3385 // Every function might be "no-free".
3386 checkAndQueryIRAttr
<Attribute::NoFree
, AANoFree
>(FPos
, FnAttrs
);
3388 // Every function might be "will-return".
3389 checkAndQueryIRAttr
<Attribute::WillReturn
, AAWillReturn
>(FPos
, FnAttrs
);
3391 // Every function might be marked "nosync"
3392 checkAndQueryIRAttr
<Attribute::NoSync
, AANoSync
>(FPos
, FnAttrs
);
3394 // Everything that is visible from the outside (=function, argument, return
3395 // positions), cannot be changed if the function is not IPO amendable. We can
3396 // however analyse the code inside.
3397 if (IsIPOAmendable
) {
3399 // Every function can be nounwind.
3400 checkAndQueryIRAttr
<Attribute::NoUnwind
, AANoUnwind
>(FPos
, FnAttrs
);
3402 // Every function might be "no-return".
3403 checkAndQueryIRAttr
<Attribute::NoReturn
, AANoReturn
>(FPos
, FnAttrs
);
3405 // Every function might be "no-recurse".
3406 checkAndQueryIRAttr
<Attribute::NoRecurse
, AANoRecurse
>(FPos
, FnAttrs
);
3408 // Every function can be "non-convergent".
3409 if (Attrs
.hasFnAttr(Attribute::Convergent
))
3410 getOrCreateAAFor
<AANonConvergent
>(FPos
);
3412 // Every function might be "readnone/readonly/writeonly/...".
3413 getOrCreateAAFor
<AAMemoryBehavior
>(FPos
);
3415 // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
3416 getOrCreateAAFor
<AAMemoryLocation
>(FPos
);
3418 // Every function can track active assumptions.
3419 getOrCreateAAFor
<AAAssumptionInfo
>(FPos
);
3421 // If we're not using a dynamic mode for float, there's nothing worthwhile
3422 // to infer. This misses the edge case denormal-fp-math="dynamic" and
3423 // denormal-fp-math-f32=something, but that likely has no real world use.
3424 DenormalMode Mode
= F
.getDenormalMode(APFloat::IEEEsingle());
3425 if (Mode
.Input
== DenormalMode::Dynamic
||
3426 Mode
.Output
== DenormalMode::Dynamic
)
3427 getOrCreateAAFor
<AADenormalFPMath
>(FPos
);
3429 // Return attributes are only appropriate if the return type is non void.
3430 Type
*ReturnType
= F
.getReturnType();
3431 if (!ReturnType
->isVoidTy()) {
3432 IRPosition RetPos
= IRPosition::returned(F
);
3433 AttributeSet RetAttrs
= Attrs
.getRetAttrs();
3435 // Every returned value might be dead.
3436 getOrCreateAAFor
<AAIsDead
>(RetPos
);
3438 // Every function might be simplified.
3439 bool UsedAssumedInformation
= false;
3440 getAssumedSimplified(RetPos
, nullptr, UsedAssumedInformation
,
3441 AA::Intraprocedural
);
3443 // Every returned value might be marked noundef.
3444 checkAndQueryIRAttr
<Attribute::NoUndef
, AANoUndef
>(RetPos
, RetAttrs
);
3446 if (ReturnType
->isPointerTy()) {
3448 // Every function with pointer return type might be marked align.
3449 getOrCreateAAFor
<AAAlign
>(RetPos
);
3451 // Every function with pointer return type might be marked nonnull.
3452 checkAndQueryIRAttr
<Attribute::NonNull
, AANonNull
>(RetPos
, RetAttrs
);
3454 // Every function with pointer return type might be marked noalias.
3455 checkAndQueryIRAttr
<Attribute::NoAlias
, AANoAlias
>(RetPos
, RetAttrs
);
3457 // Every function with pointer return type might be marked
3459 getOrCreateAAFor
<AADereferenceable
>(RetPos
);
3460 } else if (AttributeFuncs::isNoFPClassCompatibleType(ReturnType
)) {
3461 getOrCreateAAFor
<AANoFPClass
>(RetPos
);
3466 for (Argument
&Arg
: F
.args()) {
3467 IRPosition ArgPos
= IRPosition::argument(Arg
);
3468 auto ArgNo
= Arg
.getArgNo();
3469 AttributeSet ArgAttrs
= Attrs
.getParamAttrs(ArgNo
);
3471 if (!IsIPOAmendable
) {
3472 if (Arg
.getType()->isPointerTy())
3473 // Every argument with pointer type might be marked nofree.
3474 checkAndQueryIRAttr
<Attribute::NoFree
, AANoFree
>(ArgPos
, ArgAttrs
);
3478 // Every argument might be simplified. We have to go through the
3479 // Attributor interface though as outside AAs can register custom
3480 // simplification callbacks.
3481 bool UsedAssumedInformation
= false;
3482 getAssumedSimplified(ArgPos
, /* AA */ nullptr, UsedAssumedInformation
,
3483 AA::Intraprocedural
);
3485 // Every argument might be dead.
3486 getOrCreateAAFor
<AAIsDead
>(ArgPos
);
3488 // Every argument might be marked noundef.
3489 checkAndQueryIRAttr
<Attribute::NoUndef
, AANoUndef
>(ArgPos
, ArgAttrs
);
3491 if (Arg
.getType()->isPointerTy()) {
3492 // Every argument with pointer type might be marked nonnull.
3493 checkAndQueryIRAttr
<Attribute::NonNull
, AANonNull
>(ArgPos
, ArgAttrs
);
3495 // Every argument with pointer type might be marked noalias.
3496 checkAndQueryIRAttr
<Attribute::NoAlias
, AANoAlias
>(ArgPos
, ArgAttrs
);
3498 // Every argument with pointer type might be marked dereferenceable.
3499 getOrCreateAAFor
<AADereferenceable
>(ArgPos
);
3501 // Every argument with pointer type might be marked align.
3502 getOrCreateAAFor
<AAAlign
>(ArgPos
);
3504 // Every argument with pointer type might be marked nocapture.
3505 checkAndQueryIRAttr
<Attribute::NoCapture
, AANoCapture
>(ArgPos
, ArgAttrs
);
3507 // Every argument with pointer type might be marked
3508 // "readnone/readonly/writeonly/..."
3509 getOrCreateAAFor
<AAMemoryBehavior
>(ArgPos
);
3511 // Every argument with pointer type might be marked nofree.
3512 checkAndQueryIRAttr
<Attribute::NoFree
, AANoFree
>(ArgPos
, ArgAttrs
);
3514 // Every argument with pointer type might be privatizable (or
3516 getOrCreateAAFor
<AAPrivatizablePtr
>(ArgPos
);
3517 } else if (AttributeFuncs::isNoFPClassCompatibleType(Arg
.getType())) {
3518 getOrCreateAAFor
<AANoFPClass
>(ArgPos
);
3522 auto CallSitePred
= [&](Instruction
&I
) -> bool {
3523 auto &CB
= cast
<CallBase
>(I
);
3524 IRPosition CBInstPos
= IRPosition::inst(CB
);
3525 IRPosition CBFnPos
= IRPosition::callsite_function(CB
);
3527 // Call sites might be dead if they do not have side effects and no live
3528 // users. The return value might be dead if there are no live users.
3529 getOrCreateAAFor
<AAIsDead
>(CBInstPos
);
3531 Function
*Callee
= dyn_cast_if_present
<Function
>(CB
.getCalledOperand());
3532 // TODO: Even if the callee is not known now we might be able to simplify
3535 getOrCreateAAFor
<AAIndirectCallInfo
>(CBFnPos
);
3539 // Every call site can track active assumptions.
3540 getOrCreateAAFor
<AAAssumptionInfo
>(CBFnPos
);
3542 // Skip declarations except if annotations on their call sites were
3543 // explicitly requested.
3544 if (!AnnotateDeclarationCallSites
&& Callee
->isDeclaration() &&
3545 !Callee
->hasMetadata(LLVMContext::MD_callback
))
3548 if (!Callee
->getReturnType()->isVoidTy() && !CB
.use_empty()) {
3549 IRPosition CBRetPos
= IRPosition::callsite_returned(CB
);
3550 bool UsedAssumedInformation
= false;
3551 getAssumedSimplified(CBRetPos
, nullptr, UsedAssumedInformation
,
3552 AA::Intraprocedural
);
3554 if (AttributeFuncs::isNoFPClassCompatibleType(Callee
->getReturnType()))
3555 getOrCreateAAFor
<AANoFPClass
>(CBInstPos
);
3558 const AttributeList
&CBAttrs
= CBFnPos
.getAttrList();
3559 for (int I
= 0, E
= CB
.arg_size(); I
< E
; ++I
) {
3561 IRPosition CBArgPos
= IRPosition::callsite_argument(CB
, I
);
3562 AttributeSet CBArgAttrs
= CBAttrs
.getParamAttrs(I
);
3564 // Every call site argument might be dead.
3565 getOrCreateAAFor
<AAIsDead
>(CBArgPos
);
3567 // Call site argument might be simplified. We have to go through the
3568 // Attributor interface though as outside AAs can register custom
3569 // simplification callbacks.
3570 bool UsedAssumedInformation
= false;
3571 getAssumedSimplified(CBArgPos
, /* AA */ nullptr, UsedAssumedInformation
,
3572 AA::Intraprocedural
);
3574 // Every call site argument might be marked "noundef".
3575 checkAndQueryIRAttr
<Attribute::NoUndef
, AANoUndef
>(CBArgPos
, CBArgAttrs
);
3577 Type
*ArgTy
= CB
.getArgOperand(I
)->getType();
3579 if (!ArgTy
->isPointerTy()) {
3580 if (AttributeFuncs::isNoFPClassCompatibleType(ArgTy
))
3581 getOrCreateAAFor
<AANoFPClass
>(CBArgPos
);
3586 // Call site argument attribute "non-null".
3587 checkAndQueryIRAttr
<Attribute::NonNull
, AANonNull
>(CBArgPos
, CBArgAttrs
);
3589 // Call site argument attribute "nocapture".
3590 checkAndQueryIRAttr
<Attribute::NoCapture
, AANoCapture
>(CBArgPos
,
3593 // Call site argument attribute "no-alias".
3594 checkAndQueryIRAttr
<Attribute::NoAlias
, AANoAlias
>(CBArgPos
, CBArgAttrs
);
3596 // Call site argument attribute "dereferenceable".
3597 getOrCreateAAFor
<AADereferenceable
>(CBArgPos
);
3599 // Call site argument attribute "align".
3600 getOrCreateAAFor
<AAAlign
>(CBArgPos
);
3602 // Call site argument attribute
3603 // "readnone/readonly/writeonly/..."
3604 if (!CBAttrs
.hasParamAttr(I
, Attribute::ReadNone
))
3605 getOrCreateAAFor
<AAMemoryBehavior
>(CBArgPos
);
3607 // Call site argument attribute "nofree".
3608 checkAndQueryIRAttr
<Attribute::NoFree
, AANoFree
>(CBArgPos
, CBArgAttrs
);
3613 auto &OpcodeInstMap
= InfoCache
.getOpcodeInstMapForFunction(F
);
3614 [[maybe_unused
]] bool Success
;
3615 bool UsedAssumedInformation
= false;
3616 Success
= checkForAllInstructionsImpl(
3617 nullptr, OpcodeInstMap
, CallSitePred
, nullptr, nullptr,
3618 {(unsigned)Instruction::Invoke
, (unsigned)Instruction::CallBr
,
3619 (unsigned)Instruction::Call
},
3620 UsedAssumedInformation
);
3621 assert(Success
&& "Expected the check call to be successful!");
3623 auto LoadStorePred
= [&](Instruction
&I
) -> bool {
3624 if (auto *LI
= dyn_cast
<LoadInst
>(&I
)) {
3625 getOrCreateAAFor
<AAAlign
>(IRPosition::value(*LI
->getPointerOperand()));
3626 if (SimplifyAllLoads
)
3627 getAssumedSimplified(IRPosition::value(I
), nullptr,
3628 UsedAssumedInformation
, AA::Intraprocedural
);
3629 getOrCreateAAFor
<AAAddressSpace
>(
3630 IRPosition::value(*LI
->getPointerOperand()));
3632 auto &SI
= cast
<StoreInst
>(I
);
3633 getOrCreateAAFor
<AAIsDead
>(IRPosition::inst(I
));
3634 getAssumedSimplified(IRPosition::value(*SI
.getValueOperand()), nullptr,
3635 UsedAssumedInformation
, AA::Intraprocedural
);
3636 getOrCreateAAFor
<AAAlign
>(IRPosition::value(*SI
.getPointerOperand()));
3637 getOrCreateAAFor
<AAAddressSpace
>(
3638 IRPosition::value(*SI
.getPointerOperand()));
3642 Success
= checkForAllInstructionsImpl(
3643 nullptr, OpcodeInstMap
, LoadStorePred
, nullptr, nullptr,
3644 {(unsigned)Instruction::Load
, (unsigned)Instruction::Store
},
3645 UsedAssumedInformation
);
3646 assert(Success
&& "Expected the check call to be successful!");
3648 // AllocaInstPredicate
3649 auto AAAllocationInfoPred
= [&](Instruction
&I
) -> bool {
3650 getOrCreateAAFor
<AAAllocationInfo
>(IRPosition::value(I
));
3654 Success
= checkForAllInstructionsImpl(
3655 nullptr, OpcodeInstMap
, AAAllocationInfoPred
, nullptr, nullptr,
3656 {(unsigned)Instruction::Alloca
}, UsedAssumedInformation
);
3657 assert(Success
&& "Expected the check call to be successful!");
3660 bool Attributor::isClosedWorldModule() const {
3661 if (CloseWorldAssumption
.getNumOccurrences())
3662 return CloseWorldAssumption
;
3663 return isModulePass() && Configuration
.IsClosedWorldModule
;
3666 /// Helpers to ease debugging through output streams and print calls.
3669 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, ChangeStatus S
) {
3670 return OS
<< (S
== ChangeStatus::CHANGED
? "changed" : "unchanged");
3673 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, IRPosition::Kind AP
) {
3675 case IRPosition::IRP_INVALID
:
3677 case IRPosition::IRP_FLOAT
:
3679 case IRPosition::IRP_RETURNED
:
3680 return OS
<< "fn_ret";
3681 case IRPosition::IRP_CALL_SITE_RETURNED
:
3682 return OS
<< "cs_ret";
3683 case IRPosition::IRP_FUNCTION
:
3685 case IRPosition::IRP_CALL_SITE
:
3687 case IRPosition::IRP_ARGUMENT
:
3689 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
3690 return OS
<< "cs_arg";
3692 llvm_unreachable("Unknown attribute position!");
3695 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const IRPosition
&Pos
) {
3696 const Value
&AV
= Pos
.getAssociatedValue();
3697 OS
<< "{" << Pos
.getPositionKind() << ":" << AV
.getName() << " ["
3698 << Pos
.getAnchorValue().getName() << "@" << Pos
.getCallSiteArgNo() << "]";
3700 if (Pos
.hasCallBaseContext())
3701 OS
<< "[cb_context:" << *Pos
.getCallBaseContext() << "]";
3705 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const IntegerRangeState
&S
) {
3706 OS
<< "range-state(" << S
.getBitWidth() << ")<";
3707 S
.getKnown().print(OS
);
3709 S
.getAssumed().print(OS
);
3712 return OS
<< static_cast<const AbstractState
&>(S
);
3715 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const AbstractState
&S
) {
3716 return OS
<< (!S
.isValidState() ? "top" : (S
.isAtFixpoint() ? "fix" : ""));
3719 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const AbstractAttribute
&AA
) {
3724 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
3725 const PotentialConstantIntValuesState
&S
) {
3726 OS
<< "set-state(< {";
3727 if (!S
.isValidState())
3730 for (const auto &It
: S
.getAssumedSet())
3732 if (S
.undefIsContained())
3740 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
3741 const PotentialLLVMValuesState
&S
) {
3742 OS
<< "set-state(< {";
3743 if (!S
.isValidState())
3746 for (const auto &It
: S
.getAssumedSet()) {
3747 if (auto *F
= dyn_cast
<Function
>(It
.first
.getValue()))
3748 OS
<< "@" << F
->getName() << "[" << int(It
.second
) << "], ";
3750 OS
<< *It
.first
.getValue() << "[" << int(It
.second
) << "], ";
3752 if (S
.undefIsContained())
3760 void AbstractAttribute::print(Attributor
*A
, raw_ostream
&OS
) const {
3763 OS
<< "] for CtxI ";
3765 if (auto *I
= getCtxI()) {
3770 OS
<< "<<null inst>>";
3772 OS
<< " at position " << getIRPosition() << " with state " << getAsStr(A
)
3776 void AbstractAttribute::printWithDeps(raw_ostream
&OS
) const {
3779 for (const auto &DepAA
: Deps
) {
3780 auto *AA
= DepAA
.getPointer();
3788 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
3789 const AAPointerInfo::Access
&Acc
) {
3790 OS
<< " [" << Acc
.getKind() << "] " << *Acc
.getRemoteInst();
3791 if (Acc
.getLocalInst() != Acc
.getRemoteInst())
3792 OS
<< " via " << *Acc
.getLocalInst();
3793 if (Acc
.getContent()) {
3794 if (*Acc
.getContent())
3795 OS
<< " [" << **Acc
.getContent() << "]";
3797 OS
<< " [ <unknown> ]";
3803 /// ----------------------------------------------------------------------------
3804 /// Pass (Manager) Boilerplate
3805 /// ----------------------------------------------------------------------------
3807 static bool runAttributorOnFunctions(InformationCache
&InfoCache
,
3808 SetVector
<Function
*> &Functions
,
3810 CallGraphUpdater
&CGUpdater
,
3811 bool DeleteFns
, bool IsModulePass
) {
3812 if (Functions
.empty())
3816 dbgs() << "[Attributor] Run on module with " << Functions
.size()
3818 for (Function
*Fn
: Functions
)
3819 dbgs() << " - " << Fn
->getName() << "\n";
3822 // Create an Attributor and initially empty information cache that is filled
3823 // while we identify default attribute opportunities.
3824 AttributorConfig
AC(CGUpdater
);
3825 AC
.IsModulePass
= IsModulePass
;
3826 AC
.DeleteFns
= DeleteFns
;
3828 /// Tracking callback for specialization of indirect calls.
3829 DenseMap
<CallBase
*, std::unique_ptr
<SmallPtrSet
<Function
*, 8>>>
3830 IndirectCalleeTrackingMap
;
3831 if (MaxSpecializationPerCB
.getNumOccurrences()) {
3832 AC
.IndirectCalleeSpecializationCallback
=
3833 [&](Attributor
&, const AbstractAttribute
&AA
, CallBase
&CB
,
3835 if (MaxSpecializationPerCB
== 0)
3837 auto &Set
= IndirectCalleeTrackingMap
[&CB
];
3839 Set
= std::make_unique
<SmallPtrSet
<Function
*, 8>>();
3840 if (Set
->size() >= MaxSpecializationPerCB
)
3841 return Set
->contains(&Callee
);
3842 Set
->insert(&Callee
);
3847 Attributor
A(Functions
, InfoCache
, AC
);
3849 // Create shallow wrappers for all functions that are not IPO amendable
3850 if (AllowShallowWrappers
)
3851 for (Function
*F
: Functions
)
3852 if (!A
.isFunctionIPOAmendable(*F
))
3853 Attributor::createShallowWrapper(*F
);
3855 // Internalize non-exact functions
3856 // TODO: for now we eagerly internalize functions without calculating the
3857 // cost, we need a cost interface to determine whether internalizing
3858 // a function is "beneficial"
3859 if (AllowDeepWrapper
) {
3860 unsigned FunSize
= Functions
.size();
3861 for (unsigned u
= 0; u
< FunSize
; u
++) {
3862 Function
*F
= Functions
[u
];
3863 if (!F
->isDeclaration() && !F
->isDefinitionExact() && F
->getNumUses() &&
3864 !GlobalValue::isInterposableLinkage(F
->getLinkage())) {
3865 Function
*NewF
= Attributor::internalizeFunction(*F
);
3866 assert(NewF
&& "Could not internalize function.");
3867 Functions
.insert(NewF
);
3869 // Update call graph
3870 CGUpdater
.replaceFunctionWith(*F
, *NewF
);
3871 for (const Use
&U
: NewF
->uses())
3872 if (CallBase
*CB
= dyn_cast
<CallBase
>(U
.getUser())) {
3873 auto *CallerF
= CB
->getCaller();
3874 CGUpdater
.reanalyzeFunction(*CallerF
);
3880 for (Function
*F
: Functions
) {
3881 if (F
->hasExactDefinition())
3882 NumFnWithExactDefinition
++;
3884 NumFnWithoutExactDefinition
++;
3886 // We look at internal functions only on-demand but if any use is not a
3887 // direct call or outside the current set of analyzed functions, we have
3888 // to do it eagerly.
3889 if (F
->hasLocalLinkage()) {
3890 if (llvm::all_of(F
->uses(), [&Functions
](const Use
&U
) {
3891 const auto *CB
= dyn_cast
<CallBase
>(U
.getUser());
3892 return CB
&& CB
->isCallee(&U
) &&
3893 Functions
.count(const_cast<Function
*>(CB
->getCaller()));
3898 // Populate the Attributor with abstract attribute opportunities in the
3899 // function and the information cache with IR information.
3900 A
.identifyDefaultAbstractAttributes(*F
);
3903 ChangeStatus Changed
= A
.run();
3905 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions
.size()
3906 << " functions, result: " << Changed
<< ".\n");
3907 return Changed
== ChangeStatus::CHANGED
;
3910 static bool runAttributorLightOnFunctions(InformationCache
&InfoCache
,
3911 SetVector
<Function
*> &Functions
,
3913 CallGraphUpdater
&CGUpdater
,
3914 FunctionAnalysisManager
&FAM
,
3915 bool IsModulePass
) {
3916 if (Functions
.empty())
3920 dbgs() << "[AttributorLight] Run on module with " << Functions
.size()
3922 for (Function
*Fn
: Functions
)
3923 dbgs() << " - " << Fn
->getName() << "\n";
3926 // Create an Attributor and initially empty information cache that is filled
3927 // while we identify default attribute opportunities.
3928 AttributorConfig
AC(CGUpdater
);
3929 AC
.IsModulePass
= IsModulePass
;
3930 AC
.DeleteFns
= false;
3931 DenseSet
<const char *> Allowed(
3932 {&AAWillReturn::ID
, &AANoUnwind::ID
, &AANoRecurse::ID
, &AANoSync::ID
,
3933 &AANoFree::ID
, &AANoReturn::ID
, &AAMemoryLocation::ID
,
3934 &AAMemoryBehavior::ID
, &AAUnderlyingObjects::ID
, &AANoCapture::ID
,
3935 &AAInterFnReachability::ID
, &AAIntraFnReachability::ID
, &AACallEdges::ID
,
3936 &AANoFPClass::ID
, &AAMustProgress::ID
, &AANonNull::ID
});
3937 AC
.Allowed
= &Allowed
;
3938 AC
.UseLiveness
= false;
3940 Attributor
A(Functions
, InfoCache
, AC
);
3942 for (Function
*F
: Functions
) {
3943 if (F
->hasExactDefinition())
3944 NumFnWithExactDefinition
++;
3946 NumFnWithoutExactDefinition
++;
3948 // We look at internal functions only on-demand but if any use is not a
3949 // direct call or outside the current set of analyzed functions, we have
3950 // to do it eagerly.
3951 if (F
->hasLocalLinkage()) {
3952 if (llvm::all_of(F
->uses(), [&Functions
](const Use
&U
) {
3953 const auto *CB
= dyn_cast
<CallBase
>(U
.getUser());
3954 return CB
&& CB
->isCallee(&U
) &&
3955 Functions
.count(const_cast<Function
*>(CB
->getCaller()));
3960 // Populate the Attributor with abstract attribute opportunities in the
3961 // function and the information cache with IR information.
3962 A
.identifyDefaultAbstractAttributes(*F
);
3965 ChangeStatus Changed
= A
.run();
3967 if (Changed
== ChangeStatus::CHANGED
) {
3968 // Invalidate analyses for modified functions so that we don't have to
3969 // invalidate all analyses for all functions in this SCC.
3970 PreservedAnalyses FuncPA
;
3971 // We haven't changed the CFG for modified functions.
3972 FuncPA
.preserveSet
<CFGAnalyses
>();
3973 for (Function
*Changed
: A
.getModifiedFunctions()) {
3974 FAM
.invalidate(*Changed
, FuncPA
);
3975 // Also invalidate any direct callers of changed functions since analyses
3976 // may care about attributes of direct callees. For example, MemorySSA
3977 // cares about whether or not a call's callee modifies memory and queries
3978 // that through function attributes.
3979 for (auto *U
: Changed
->users()) {
3980 if (auto *Call
= dyn_cast
<CallBase
>(U
)) {
3981 if (Call
->getCalledFunction() == Changed
)
3982 FAM
.invalidate(*Call
->getFunction(), FuncPA
);
3987 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions
.size()
3988 << " functions, result: " << Changed
<< ".\n");
3989 return Changed
== ChangeStatus::CHANGED
;
3992 void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
3994 void AADepGraph::dumpGraph() {
3995 static std::atomic
<int> CallTimes
;
3998 if (!DepGraphDotFileNamePrefix
.empty())
3999 Prefix
= DepGraphDotFileNamePrefix
;
4001 Prefix
= "dep_graph";
4002 std::string Filename
=
4003 Prefix
+ "_" + std::to_string(CallTimes
.load()) + ".dot";
4005 outs() << "Dependency graph dump to " << Filename
<< ".\n";
4009 raw_fd_ostream
File(Filename
, EC
, sys::fs::OF_TextWithCRLF
);
4011 llvm::WriteGraph(File
, this);
4016 void AADepGraph::print() {
4017 for (auto DepAA
: SyntheticRoot
.Deps
)
4018 cast
<AbstractAttribute
>(DepAA
.getPointer())->printWithDeps(outs());
4021 PreservedAnalyses
AttributorPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
4022 FunctionAnalysisManager
&FAM
=
4023 AM
.getResult
<FunctionAnalysisManagerModuleProxy
>(M
).getManager();
4024 AnalysisGetter
AG(FAM
);
4026 SetVector
<Function
*> Functions
;
4027 for (Function
&F
: M
)
4028 Functions
.insert(&F
);
4030 CallGraphUpdater CGUpdater
;
4031 BumpPtrAllocator Allocator
;
4032 InformationCache
InfoCache(M
, AG
, Allocator
, /* CGSCC */ nullptr);
4033 if (runAttributorOnFunctions(InfoCache
, Functions
, AG
, CGUpdater
,
4034 /* DeleteFns */ true, /* IsModulePass */ true)) {
4035 // FIXME: Think about passes we will preserve and add them here.
4036 return PreservedAnalyses::none();
4038 return PreservedAnalyses::all();
4041 PreservedAnalyses
AttributorCGSCCPass::run(LazyCallGraph::SCC
&C
,
4042 CGSCCAnalysisManager
&AM
,
4044 CGSCCUpdateResult
&UR
) {
4045 FunctionAnalysisManager
&FAM
=
4046 AM
.getResult
<FunctionAnalysisManagerCGSCCProxy
>(C
, CG
).getManager();
4047 AnalysisGetter
AG(FAM
);
4049 SetVector
<Function
*> Functions
;
4050 for (LazyCallGraph::Node
&N
: C
)
4051 Functions
.insert(&N
.getFunction());
4053 if (Functions
.empty())
4054 return PreservedAnalyses::all();
4056 Module
&M
= *Functions
.back()->getParent();
4057 CallGraphUpdater CGUpdater
;
4058 CGUpdater
.initialize(CG
, C
, AM
, UR
);
4059 BumpPtrAllocator Allocator
;
4060 InformationCache
InfoCache(M
, AG
, Allocator
, /* CGSCC */ &Functions
);
4061 if (runAttributorOnFunctions(InfoCache
, Functions
, AG
, CGUpdater
,
4062 /* DeleteFns */ false,
4063 /* IsModulePass */ false)) {
4064 // FIXME: Think about passes we will preserve and add them here.
4065 PreservedAnalyses PA
;
4066 PA
.preserve
<FunctionAnalysisManagerCGSCCProxy
>();
4069 return PreservedAnalyses::all();
4072 PreservedAnalyses
AttributorLightPass::run(Module
&M
,
4073 ModuleAnalysisManager
&AM
) {
4074 FunctionAnalysisManager
&FAM
=
4075 AM
.getResult
<FunctionAnalysisManagerModuleProxy
>(M
).getManager();
4076 AnalysisGetter
AG(FAM
, /* CachedOnly */ true);
4078 SetVector
<Function
*> Functions
;
4079 for (Function
&F
: M
)
4080 Functions
.insert(&F
);
4082 CallGraphUpdater CGUpdater
;
4083 BumpPtrAllocator Allocator
;
4084 InformationCache
InfoCache(M
, AG
, Allocator
, /* CGSCC */ nullptr);
4085 if (runAttributorLightOnFunctions(InfoCache
, Functions
, AG
, CGUpdater
, FAM
,
4086 /* IsModulePass */ true)) {
4087 PreservedAnalyses PA
;
4088 // We have not added or removed functions.
4089 PA
.preserve
<FunctionAnalysisManagerCGSCCProxy
>();
4090 // We already invalidated all relevant function analyses above.
4091 PA
.preserveSet
<AllAnalysesOn
<Function
>>();
4094 return PreservedAnalyses::all();
4097 PreservedAnalyses
AttributorLightCGSCCPass::run(LazyCallGraph::SCC
&C
,
4098 CGSCCAnalysisManager
&AM
,
4100 CGSCCUpdateResult
&UR
) {
4101 FunctionAnalysisManager
&FAM
=
4102 AM
.getResult
<FunctionAnalysisManagerCGSCCProxy
>(C
, CG
).getManager();
4103 AnalysisGetter
AG(FAM
);
4105 SetVector
<Function
*> Functions
;
4106 for (LazyCallGraph::Node
&N
: C
)
4107 Functions
.insert(&N
.getFunction());
4109 if (Functions
.empty())
4110 return PreservedAnalyses::all();
4112 Module
&M
= *Functions
.back()->getParent();
4113 CallGraphUpdater CGUpdater
;
4114 CGUpdater
.initialize(CG
, C
, AM
, UR
);
4115 BumpPtrAllocator Allocator
;
4116 InformationCache
InfoCache(M
, AG
, Allocator
, /* CGSCC */ &Functions
);
4117 if (runAttributorLightOnFunctions(InfoCache
, Functions
, AG
, CGUpdater
, FAM
,
4118 /* IsModulePass */ false)) {
4119 PreservedAnalyses PA
;
4120 // We have not added or removed functions.
4121 PA
.preserve
<FunctionAnalysisManagerCGSCCProxy
>();
4122 // We already invalidated all relevant function analyses above.
4123 PA
.preserveSet
<AllAnalysesOn
<Function
>>();
4126 return PreservedAnalyses::all();
4130 template <> struct GraphTraits
<AADepGraphNode
*> {
4131 using NodeRef
= AADepGraphNode
*;
4132 using DepTy
= PointerIntPair
<AADepGraphNode
*, 1>;
4133 using EdgeRef
= PointerIntPair
<AADepGraphNode
*, 1>;
4135 static NodeRef
getEntryNode(AADepGraphNode
*DGN
) { return DGN
; }
4136 static NodeRef
DepGetVal(const DepTy
&DT
) { return DT
.getPointer(); }
4138 using ChildIteratorType
=
4139 mapped_iterator
<AADepGraphNode::DepSetTy::iterator
, decltype(&DepGetVal
)>;
4140 using ChildEdgeIteratorType
= AADepGraphNode::DepSetTy::iterator
;
4142 static ChildIteratorType
child_begin(NodeRef N
) { return N
->child_begin(); }
4144 static ChildIteratorType
child_end(NodeRef N
) { return N
->child_end(); }
4148 struct GraphTraits
<AADepGraph
*> : public GraphTraits
<AADepGraphNode
*> {
4149 static NodeRef
getEntryNode(AADepGraph
*DG
) { return DG
->GetEntryNode(); }
4151 using nodes_iterator
=
4152 mapped_iterator
<AADepGraphNode::DepSetTy::iterator
, decltype(&DepGetVal
)>;
4154 static nodes_iterator
nodes_begin(AADepGraph
*DG
) { return DG
->begin(); }
4156 static nodes_iterator
nodes_end(AADepGraph
*DG
) { return DG
->end(); }
4159 template <> struct DOTGraphTraits
<AADepGraph
*> : public DefaultDOTGraphTraits
{
4160 DOTGraphTraits(bool isSimple
= false) : DefaultDOTGraphTraits(isSimple
) {}
4162 static std::string
getNodeLabel(const AADepGraphNode
*Node
,
4163 const AADepGraph
*DG
) {
4164 std::string AAString
;
4165 raw_string_ostream
O(AAString
);
4171 } // end namespace llvm