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 inter procedural pass that deduces and/or propagating
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/DepthFirstIterator.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/CaptureTracking.h"
24 #include "llvm/Analysis/EHPersonalities.h"
25 #include "llvm/Analysis/GlobalsModRef.h"
26 #include "llvm/Analysis/Loads.h"
27 #include "llvm/Analysis/MemoryBuiltins.h"
28 #include "llvm/Analysis/ValueTracking.h"
29 #include "llvm/IR/Argument.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/CFG.h"
32 #include "llvm/IR/InstIterator.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
38 #include "llvm/Transforms/Utils/Local.h"
44 #define DEBUG_TYPE "attributor"
46 STATISTIC(NumFnWithExactDefinition
,
47 "Number of function with exact definitions");
48 STATISTIC(NumFnWithoutExactDefinition
,
49 "Number of function without exact definitions");
50 STATISTIC(NumAttributesTimedOut
,
51 "Number of abstract attributes timed out before fixpoint");
52 STATISTIC(NumAttributesValidFixpoint
,
53 "Number of abstract attributes in a valid fixpoint state");
54 STATISTIC(NumAttributesManifested
,
55 "Number of abstract attributes manifested in IR");
57 // Some helper macros to deal with statistics tracking.
60 // For simple IR attribute tracking overload trackStatistics in the abstract
61 // attribute and choose the right STATS_DECLTRACK_********* macro,
63 // void trackStatistics() const override {
64 // STATS_DECLTRACK_ARG_ATTR(returned)
66 // If there is a single "increment" side one can use the macro
67 // STATS_DECLTRACK with a custom message. If there are multiple increment
68 // sides, STATS_DECL and STATS_TRACK can also be used separatly.
70 #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \
71 ("Number of " #TYPE " marked '" #NAME "'")
72 #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
73 #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
74 #define STATS_DECL(NAME, TYPE, MSG) \
75 STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
76 #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
77 #define STATS_DECLTRACK(NAME, TYPE, MSG) \
79 STATS_DECL(NAME, TYPE, MSG) \
80 STATS_TRACK(NAME, TYPE) \
82 #define STATS_DECLTRACK_ARG_ATTR(NAME) \
83 STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
84 #define STATS_DECLTRACK_CSARG_ATTR(NAME) \
85 STATS_DECLTRACK(NAME, CSArguments, \
86 BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
87 #define STATS_DECLTRACK_FN_ATTR(NAME) \
88 STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
89 #define STATS_DECLTRACK_CS_ATTR(NAME) \
90 STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
91 #define STATS_DECLTRACK_FNRET_ATTR(NAME) \
92 STATS_DECLTRACK(NAME, FunctionReturn, \
93 BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
94 #define STATS_DECLTRACK_CSRET_ATTR(NAME) \
95 STATS_DECLTRACK(NAME, CSReturn, \
96 BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
97 #define STATS_DECLTRACK_FLOATING_ATTR(NAME) \
98 STATS_DECLTRACK(NAME, Floating, \
99 ("Number of floating values known to be '" #NAME "'"))
101 // TODO: Determine a good default value.
103 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
104 // (when run with the first 5 abstract attributes). The results also indicate
105 // that we never reach 32 iterations but always find a fixpoint sooner.
107 // This will become more evolved once we perform two interleaved fixpoint
108 // iterations: bottom-up and top-down.
109 static cl::opt
<unsigned>
110 MaxFixpointIterations("attributor-max-iterations", cl::Hidden
,
111 cl::desc("Maximal number of fixpoint iterations."),
113 static cl::opt
<bool> VerifyMaxFixpointIterations(
114 "attributor-max-iterations-verify", cl::Hidden
,
115 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
118 static cl::opt
<bool> DisableAttributor(
119 "attributor-disable", cl::Hidden
,
120 cl::desc("Disable the attributor inter-procedural deduction pass."),
123 static cl::opt
<bool> ManifestInternal(
124 "attributor-manifest-internal", cl::Hidden
,
125 cl::desc("Manifest Attributor internal string attributes."),
128 static cl::opt
<bool> VerifyAttributor(
129 "attributor-verify", cl::Hidden
,
130 cl::desc("Verify the Attributor deduction and "
131 "manifestation of attributes -- may issue false-positive errors"),
134 static cl::opt
<unsigned> DepRecInterval(
135 "attributor-dependence-recompute-interval", cl::Hidden
,
136 cl::desc("Number of iterations until dependences are recomputed."),
139 static cl::opt
<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
140 cl::init(true), cl::Hidden
);
142 static cl::opt
<int> MaxHeapToStackSize("max-heap-to-stack-size",
143 cl::init(128), cl::Hidden
);
145 /// Logic operators for the change status enum class.
148 ChangeStatus
llvm::operator|(ChangeStatus l
, ChangeStatus r
) {
149 return l
== ChangeStatus::CHANGED
? l
: r
;
151 ChangeStatus
llvm::operator&(ChangeStatus l
, ChangeStatus r
) {
152 return l
== ChangeStatus::UNCHANGED
? l
: r
;
156 /// Recursively visit all values that might become \p IRP at some point. This
157 /// will be done by looking through cast instructions, selects, phis, and calls
158 /// with the "returned" attribute. Once we cannot look through the value any
159 /// further, the callback \p VisitValueCB is invoked and passed the current
160 /// value, the \p State, and a flag to indicate if we stripped anything. To
161 /// limit how much effort is invested, we will never visit more values than
162 /// specified by \p MaxValues.
163 template <typename AAType
, typename StateTy
>
164 bool genericValueTraversal(
165 Attributor
&A
, IRPosition IRP
, const AAType
&QueryingAA
, StateTy
&State
,
166 const function_ref
<bool(Value
&, StateTy
&, bool)> &VisitValueCB
,
169 const AAIsDead
*LivenessAA
= nullptr;
170 if (IRP
.getAnchorScope())
171 LivenessAA
= &A
.getAAFor
<AAIsDead
>(
172 QueryingAA
, IRPosition::function(*IRP
.getAnchorScope()),
173 /* TrackDependence */ false);
174 bool AnyDead
= false;
176 // TODO: Use Positions here to allow context sensitivity in VisitValueCB
177 SmallPtrSet
<Value
*, 16> Visited
;
178 SmallVector
<Value
*, 16> Worklist
;
179 Worklist
.push_back(&IRP
.getAssociatedValue());
183 Value
*V
= Worklist
.pop_back_val();
185 // Check if we should process the current value. To prevent endless
186 // recursion keep a record of the values we followed!
187 if (!Visited
.insert(V
).second
)
190 // Make sure we limit the compile time for complex expressions.
191 if (Iteration
++ >= MaxValues
)
194 // Explicitly look through calls with a "returned" attribute if we do
195 // not have a pointer as stripPointerCasts only works on them.
196 Value
*NewV
= nullptr;
197 if (V
->getType()->isPointerTy()) {
198 NewV
= V
->stripPointerCasts();
201 if (CS
&& CS
.getCalledFunction()) {
202 for (Argument
&Arg
: CS
.getCalledFunction()->args())
203 if (Arg
.hasReturnedAttr()) {
204 NewV
= CS
.getArgOperand(Arg
.getArgNo());
209 if (NewV
&& NewV
!= V
) {
210 Worklist
.push_back(NewV
);
214 // Look through select instructions, visit both potential values.
215 if (auto *SI
= dyn_cast
<SelectInst
>(V
)) {
216 Worklist
.push_back(SI
->getTrueValue());
217 Worklist
.push_back(SI
->getFalseValue());
221 // Look through phi nodes, visit all live operands.
222 if (auto *PHI
= dyn_cast
<PHINode
>(V
)) {
224 "Expected liveness in the presence of instructions!");
225 for (unsigned u
= 0, e
= PHI
->getNumIncomingValues(); u
< e
; u
++) {
226 const BasicBlock
*IncomingBB
= PHI
->getIncomingBlock(u
);
227 if (LivenessAA
->isAssumedDead(IncomingBB
->getTerminator())) {
231 Worklist
.push_back(PHI
->getIncomingValue(u
));
236 // Once a leaf is reached we inform the user through the callback.
237 if (!VisitValueCB(*V
, State
, Iteration
> 1))
239 } while (!Worklist
.empty());
241 // If we actually used liveness information so we have to record a dependence.
243 A
.recordDependence(*LivenessAA
, QueryingAA
);
245 // All values have been visited.
249 /// Return true if \p New is equal or worse than \p Old.
250 static bool isEqualOrWorse(const Attribute
&New
, const Attribute
&Old
) {
251 if (!Old
.isIntAttribute())
254 return Old
.getValueAsInt() >= New
.getValueAsInt();
257 /// Return true if the information provided by \p Attr was added to the
258 /// attribute list \p Attrs. This is only the case if it was not already present
259 /// in \p Attrs at the position describe by \p PK and \p AttrIdx.
260 static bool addIfNotExistent(LLVMContext
&Ctx
, const Attribute
&Attr
,
261 AttributeList
&Attrs
, int AttrIdx
) {
263 if (Attr
.isEnumAttribute()) {
264 Attribute::AttrKind Kind
= Attr
.getKindAsEnum();
265 if (Attrs
.hasAttribute(AttrIdx
, Kind
))
266 if (isEqualOrWorse(Attr
, Attrs
.getAttribute(AttrIdx
, Kind
)))
268 Attrs
= Attrs
.addAttribute(Ctx
, AttrIdx
, Attr
);
271 if (Attr
.isStringAttribute()) {
272 StringRef Kind
= Attr
.getKindAsString();
273 if (Attrs
.hasAttribute(AttrIdx
, Kind
))
274 if (isEqualOrWorse(Attr
, Attrs
.getAttribute(AttrIdx
, Kind
)))
276 Attrs
= Attrs
.addAttribute(Ctx
, AttrIdx
, Attr
);
279 if (Attr
.isIntAttribute()) {
280 Attribute::AttrKind Kind
= Attr
.getKindAsEnum();
281 if (Attrs
.hasAttribute(AttrIdx
, Kind
))
282 if (isEqualOrWorse(Attr
, Attrs
.getAttribute(AttrIdx
, Kind
)))
284 Attrs
= Attrs
.removeAttribute(Ctx
, AttrIdx
, Kind
);
285 Attrs
= Attrs
.addAttribute(Ctx
, AttrIdx
, Attr
);
289 llvm_unreachable("Expected enum or string attribute!");
292 ChangeStatus
AbstractAttribute::update(Attributor
&A
) {
293 ChangeStatus HasChanged
= ChangeStatus::UNCHANGED
;
294 if (getState().isAtFixpoint())
297 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
299 HasChanged
= updateImpl(A
);
301 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged
<< " " << *this
308 IRAttributeManifest::manifestAttrs(Attributor
&A
, IRPosition
&IRP
,
309 const ArrayRef
<Attribute
> &DeducedAttrs
) {
310 Function
*ScopeFn
= IRP
.getAssociatedFunction();
311 IRPosition::Kind PK
= IRP
.getPositionKind();
313 // In the following some generic code that will manifest attributes in
314 // DeducedAttrs if they improve the current IR. Due to the different
315 // annotation positions we use the underlying AttributeList interface.
319 case IRPosition::IRP_INVALID
:
320 case IRPosition::IRP_FLOAT
:
321 return ChangeStatus::UNCHANGED
;
322 case IRPosition::IRP_ARGUMENT
:
323 case IRPosition::IRP_FUNCTION
:
324 case IRPosition::IRP_RETURNED
:
325 Attrs
= ScopeFn
->getAttributes();
327 case IRPosition::IRP_CALL_SITE
:
328 case IRPosition::IRP_CALL_SITE_RETURNED
:
329 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
330 Attrs
= ImmutableCallSite(&IRP
.getAnchorValue()).getAttributes();
334 ChangeStatus HasChanged
= ChangeStatus::UNCHANGED
;
335 LLVMContext
&Ctx
= IRP
.getAnchorValue().getContext();
336 for (const Attribute
&Attr
: DeducedAttrs
) {
337 if (!addIfNotExistent(Ctx
, Attr
, Attrs
, IRP
.getAttrIdx()))
340 HasChanged
= ChangeStatus::CHANGED
;
343 if (HasChanged
== ChangeStatus::UNCHANGED
)
347 case IRPosition::IRP_ARGUMENT
:
348 case IRPosition::IRP_FUNCTION
:
349 case IRPosition::IRP_RETURNED
:
350 ScopeFn
->setAttributes(Attrs
);
352 case IRPosition::IRP_CALL_SITE
:
353 case IRPosition::IRP_CALL_SITE_RETURNED
:
354 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
355 CallSite(&IRP
.getAnchorValue()).setAttributes(Attrs
);
357 case IRPosition::IRP_INVALID
:
358 case IRPosition::IRP_FLOAT
:
365 const IRPosition
IRPosition::EmptyKey(255);
366 const IRPosition
IRPosition::TombstoneKey(256);
368 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition
&IRP
) {
369 IRPositions
.emplace_back(IRP
);
371 ImmutableCallSite
ICS(&IRP
.getAnchorValue());
372 switch (IRP
.getPositionKind()) {
373 case IRPosition::IRP_INVALID
:
374 case IRPosition::IRP_FLOAT
:
375 case IRPosition::IRP_FUNCTION
:
377 case IRPosition::IRP_ARGUMENT
:
378 case IRPosition::IRP_RETURNED
:
379 IRPositions
.emplace_back(
380 IRPosition::function(*IRP
.getAssociatedFunction()));
382 case IRPosition::IRP_CALL_SITE
:
383 assert(ICS
&& "Expected call site!");
384 // TODO: We need to look at the operand bundles similar to the redirection
386 if (!ICS
.hasOperandBundles())
387 if (const Function
*Callee
= ICS
.getCalledFunction())
388 IRPositions
.emplace_back(IRPosition::function(*Callee
));
390 case IRPosition::IRP_CALL_SITE_RETURNED
:
391 assert(ICS
&& "Expected call site!");
392 // TODO: We need to look at the operand bundles similar to the redirection
394 if (!ICS
.hasOperandBundles()) {
395 if (const Function
*Callee
= ICS
.getCalledFunction()) {
396 IRPositions
.emplace_back(IRPosition::returned(*Callee
));
397 IRPositions
.emplace_back(IRPosition::function(*Callee
));
400 IRPositions
.emplace_back(
401 IRPosition::callsite_function(cast
<CallBase
>(*ICS
.getInstruction())));
403 case IRPosition::IRP_CALL_SITE_ARGUMENT
: {
404 int ArgNo
= IRP
.getArgNo();
405 assert(ICS
&& ArgNo
>= 0 && "Expected call site!");
406 // TODO: We need to look at the operand bundles similar to the redirection
408 if (!ICS
.hasOperandBundles()) {
409 const Function
*Callee
= ICS
.getCalledFunction();
410 if (Callee
&& Callee
->arg_size() > unsigned(ArgNo
))
411 IRPositions
.emplace_back(IRPosition::argument(*Callee
->getArg(ArgNo
)));
413 IRPositions
.emplace_back(IRPosition::function(*Callee
));
415 IRPositions
.emplace_back(IRPosition::value(IRP
.getAssociatedValue()));
421 bool IRPosition::hasAttr(ArrayRef
<Attribute::AttrKind
> AKs
) const {
422 for (const IRPosition
&EquivIRP
: SubsumingPositionIterator(*this))
423 for (Attribute::AttrKind AK
: AKs
)
424 if (EquivIRP
.getAttr(AK
).getKindAsEnum() == AK
)
429 void IRPosition::getAttrs(ArrayRef
<Attribute::AttrKind
> AKs
,
430 SmallVectorImpl
<Attribute
> &Attrs
) const {
431 for (const IRPosition
&EquivIRP
: SubsumingPositionIterator(*this))
432 for (Attribute::AttrKind AK
: AKs
) {
433 const Attribute
&Attr
= EquivIRP
.getAttr(AK
);
434 if (Attr
.getKindAsEnum() == AK
)
435 Attrs
.push_back(Attr
);
439 void IRPosition::verify() {
440 switch (KindOrArgNo
) {
442 assert(KindOrArgNo
>= 0 && "Expected argument or call site argument!");
443 assert((isa
<CallBase
>(AnchorVal
) || isa
<Argument
>(AnchorVal
)) &&
444 "Expected call base or argument for positive attribute index!");
445 if (isa
<Argument
>(AnchorVal
)) {
446 assert(cast
<Argument
>(AnchorVal
)->getArgNo() == unsigned(getArgNo()) &&
447 "Argument number mismatch!");
448 assert(cast
<Argument
>(AnchorVal
) == &getAssociatedValue() &&
449 "Associated value mismatch!");
451 assert(cast
<CallBase
>(*AnchorVal
).arg_size() > unsigned(getArgNo()) &&
452 "Call site argument number mismatch!");
453 assert(cast
<CallBase
>(*AnchorVal
).getArgOperand(getArgNo()) ==
454 &getAssociatedValue() &&
455 "Associated value mismatch!");
459 assert(!AnchorVal
&& "Expected no value for an invalid position!");
462 assert((!isa
<CallBase
>(&getAssociatedValue()) &&
463 !isa
<Argument
>(&getAssociatedValue())) &&
464 "Expected specialized kind for call base and argument values!");
467 assert(isa
<Function
>(AnchorVal
) &&
468 "Expected function for a 'returned' position!");
469 assert(AnchorVal
== &getAssociatedValue() && "Associated value mismatch!");
471 case IRP_CALL_SITE_RETURNED
:
472 assert((isa
<CallBase
>(AnchorVal
)) &&
473 "Expected call base for 'call site returned' position!");
474 assert(AnchorVal
== &getAssociatedValue() && "Associated value mismatch!");
477 assert((isa
<CallBase
>(AnchorVal
)) &&
478 "Expected call base for 'call site function' position!");
479 assert(AnchorVal
== &getAssociatedValue() && "Associated value mismatch!");
482 assert(isa
<Function
>(AnchorVal
) &&
483 "Expected function for a 'function' position!");
484 assert(AnchorVal
== &getAssociatedValue() && "Associated value mismatch!");
489 /// Helper functions to clamp a state \p S of type \p StateType with the
490 /// information in \p R and indicate/return if \p S did change (as-in update is
491 /// required to be run again).
494 template <typename StateType
>
495 ChangeStatus
clampStateAndIndicateChange(StateType
&S
, const StateType
&R
);
498 ChangeStatus clampStateAndIndicateChange
<IntegerState
>(IntegerState
&S
,
499 const IntegerState
&R
) {
500 auto Assumed
= S
.getAssumed();
502 return Assumed
== S
.getAssumed() ? ChangeStatus::UNCHANGED
503 : ChangeStatus::CHANGED
;
507 ChangeStatus clampStateAndIndicateChange
<BooleanState
>(BooleanState
&S
,
508 const BooleanState
&R
) {
509 return clampStateAndIndicateChange
<IntegerState
>(S
, R
);
513 /// Clamp the information known for all returned values of a function
514 /// (identified by \p QueryingAA) into \p S.
515 template <typename AAType
, typename StateType
= typename
AAType::StateType
>
516 static void clampReturnedValueStates(Attributor
&A
, const AAType
&QueryingAA
,
518 LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
519 << static_cast<const AbstractAttribute
&>(QueryingAA
)
520 << " into " << S
<< "\n");
522 assert((QueryingAA
.getIRPosition().getPositionKind() ==
523 IRPosition::IRP_RETURNED
||
524 QueryingAA
.getIRPosition().getPositionKind() ==
525 IRPosition::IRP_CALL_SITE_RETURNED
) &&
526 "Can only clamp returned value states for a function returned or call "
527 "site returned position!");
529 // Use an optional state as there might not be any return values and we want
530 // to join (IntegerState::operator&) the state of all there are.
531 Optional
<StateType
> T
;
533 // Callback for each possibly returned value.
534 auto CheckReturnValue
= [&](Value
&RV
) -> bool {
535 const IRPosition
&RVPos
= IRPosition::value(RV
);
536 const AAType
&AA
= A
.getAAFor
<AAType
>(QueryingAA
, RVPos
);
537 LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV
<< " AA: " << AA
.getAsStr()
538 << " @ " << RVPos
<< "\n");
539 const StateType
&AAS
= static_cast<const StateType
&>(AA
.getState());
544 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS
<< " RV State: " << T
546 return T
->isValidState();
549 if (!A
.checkForAllReturnedValues(CheckReturnValue
, QueryingAA
))
550 S
.indicatePessimisticFixpoint();
551 else if (T
.hasValue())
555 /// Helper class for generic deduction: return value -> returned position.
556 template <typename AAType
, typename Base
,
557 typename StateType
= typename
AAType::StateType
>
558 struct AAReturnedFromReturnedValues
: public Base
{
559 AAReturnedFromReturnedValues(const IRPosition
&IRP
) : Base(IRP
) {}
561 /// See AbstractAttribute::updateImpl(...).
562 ChangeStatus
updateImpl(Attributor
&A
) override
{
564 clampReturnedValueStates
<AAType
, StateType
>(A
, *this, S
);
565 // TODO: If we know we visited all returned values, thus no are assumed
566 // dead, we can take the known information from the state T.
567 return clampStateAndIndicateChange
<StateType
>(this->getState(), S
);
571 /// Clamp the information known at all call sites for a given argument
572 /// (identified by \p QueryingAA) into \p S.
573 template <typename AAType
, typename StateType
= typename
AAType::StateType
>
574 static void clampCallSiteArgumentStates(Attributor
&A
, const AAType
&QueryingAA
,
576 LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
577 << static_cast<const AbstractAttribute
&>(QueryingAA
)
578 << " into " << S
<< "\n");
580 assert(QueryingAA
.getIRPosition().getPositionKind() ==
581 IRPosition::IRP_ARGUMENT
&&
582 "Can only clamp call site argument states for an argument position!");
584 // Use an optional state as there might not be any return values and we want
585 // to join (IntegerState::operator&) the state of all there are.
586 Optional
<StateType
> T
;
588 // The argument number which is also the call site argument number.
589 unsigned ArgNo
= QueryingAA
.getIRPosition().getArgNo();
591 auto CallSiteCheck
= [&](CallSite CS
) {
592 const IRPosition
&CSArgPos
= IRPosition::callsite_argument(CS
, ArgNo
);
593 const AAType
&AA
= A
.getAAFor
<AAType
>(QueryingAA
, CSArgPos
);
594 LLVM_DEBUG(dbgs() << "[Attributor] CS: " << *CS
.getInstruction()
595 << " AA: " << AA
.getAsStr() << " @" << CSArgPos
<< "\n");
596 const StateType
&AAS
= static_cast<const StateType
&>(AA
.getState());
601 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS
<< " CSA State: " << T
603 return T
->isValidState();
606 if (!A
.checkForAllCallSites(CallSiteCheck
, QueryingAA
, true))
607 S
.indicatePessimisticFixpoint();
608 else if (T
.hasValue())
612 /// Helper class for generic deduction: call site argument -> argument position.
613 template <typename AAType
, typename Base
,
614 typename StateType
= typename
AAType::StateType
>
615 struct AAArgumentFromCallSiteArguments
: public Base
{
616 AAArgumentFromCallSiteArguments(const IRPosition
&IRP
) : Base(IRP
) {}
618 /// See AbstractAttribute::updateImpl(...).
619 ChangeStatus
updateImpl(Attributor
&A
) override
{
621 clampCallSiteArgumentStates
<AAType
, StateType
>(A
, *this, S
);
622 // TODO: If we know we visited all incoming values, thus no are assumed
623 // dead, we can take the known information from the state T.
624 return clampStateAndIndicateChange
<StateType
>(this->getState(), S
);
628 /// Helper class for generic replication: function returned -> cs returned.
629 template <typename AAType
, typename Base
>
630 struct AACallSiteReturnedFromReturned
: public Base
{
631 AACallSiteReturnedFromReturned(const IRPosition
&IRP
) : Base(IRP
) {}
633 /// See AbstractAttribute::updateImpl(...).
634 ChangeStatus
updateImpl(Attributor
&A
) override
{
635 assert(this->getIRPosition().getPositionKind() ==
636 IRPosition::IRP_CALL_SITE_RETURNED
&&
637 "Can only wrap function returned positions for call site returned "
639 auto &S
= this->getState();
641 const Function
*AssociatedFunction
=
642 this->getIRPosition().getAssociatedFunction();
643 if (!AssociatedFunction
)
644 return S
.indicatePessimisticFixpoint();
646 IRPosition FnPos
= IRPosition::returned(*AssociatedFunction
);
647 const AAType
&AA
= A
.getAAFor
<AAType
>(*this, FnPos
);
648 return clampStateAndIndicateChange(
649 S
, static_cast<const typename
AAType::StateType
&>(AA
.getState()));
653 /// -----------------------NoUnwind Function Attribute--------------------------
655 struct AANoUnwindImpl
: AANoUnwind
{
656 AANoUnwindImpl(const IRPosition
&IRP
) : AANoUnwind(IRP
) {}
658 const std::string
getAsStr() const override
{
659 return getAssumed() ? "nounwind" : "may-unwind";
662 /// See AbstractAttribute::updateImpl(...).
663 ChangeStatus
updateImpl(Attributor
&A
) override
{
665 (unsigned)Instruction::Invoke
, (unsigned)Instruction::CallBr
,
666 (unsigned)Instruction::Call
, (unsigned)Instruction::CleanupRet
,
667 (unsigned)Instruction::CatchSwitch
, (unsigned)Instruction::Resume
};
669 auto CheckForNoUnwind
= [&](Instruction
&I
) {
673 if (ImmutableCallSite ICS
= ImmutableCallSite(&I
)) {
674 const auto &NoUnwindAA
=
675 A
.getAAFor
<AANoUnwind
>(*this, IRPosition::callsite_function(ICS
));
676 return NoUnwindAA
.isAssumedNoUnwind();
681 if (!A
.checkForAllInstructions(CheckForNoUnwind
, *this, Opcodes
))
682 return indicatePessimisticFixpoint();
684 return ChangeStatus::UNCHANGED
;
688 struct AANoUnwindFunction final
: public AANoUnwindImpl
{
689 AANoUnwindFunction(const IRPosition
&IRP
) : AANoUnwindImpl(IRP
) {}
691 /// See AbstractAttribute::trackStatistics()
692 void trackStatistics() const override
{ STATS_DECLTRACK_FN_ATTR(nounwind
) }
695 /// NoUnwind attribute deduction for a call sites.
696 struct AANoUnwindCallSite final
: AANoUnwindImpl
{
697 AANoUnwindCallSite(const IRPosition
&IRP
) : AANoUnwindImpl(IRP
) {}
699 /// See AbstractAttribute::initialize(...).
700 void initialize(Attributor
&A
) override
{
701 AANoUnwindImpl::initialize(A
);
702 Function
*F
= getAssociatedFunction();
704 indicatePessimisticFixpoint();
707 /// See AbstractAttribute::updateImpl(...).
708 ChangeStatus
updateImpl(Attributor
&A
) override
{
709 // TODO: Once we have call site specific value information we can provide
710 // call site specific liveness information and then it makes
711 // sense to specialize attributes for call sites arguments instead of
712 // redirecting requests to the callee argument.
713 Function
*F
= getAssociatedFunction();
714 const IRPosition
&FnPos
= IRPosition::function(*F
);
715 auto &FnAA
= A
.getAAFor
<AANoUnwind
>(*this, FnPos
);
716 return clampStateAndIndicateChange(
718 static_cast<const AANoUnwind::StateType
&>(FnAA
.getState()));
721 /// See AbstractAttribute::trackStatistics()
722 void trackStatistics() const override
{ STATS_DECLTRACK_CS_ATTR(nounwind
); }
725 /// --------------------- Function Return Values -------------------------------
727 /// "Attribute" that collects all potential returned values and the return
728 /// instructions that they arise from.
730 /// If there is a unique returned value R, the manifest method will:
731 /// - mark R with the "returned" attribute, if R is an argument.
732 class AAReturnedValuesImpl
: public AAReturnedValues
, public AbstractState
{
734 /// Mapping of values potentially returned by the associated function to the
735 /// return instructions that might return them.
736 MapVector
<Value
*, SmallSetVector
<ReturnInst
*, 4>> ReturnedValues
;
738 /// Mapping to remember the number of returned values for a call site such
739 /// that we can avoid updates if nothing changed.
740 DenseMap
<const CallBase
*, unsigned> NumReturnedValuesPerKnownAA
;
742 /// Set of unresolved calls returned by the associated function.
743 SmallSetVector
<CallBase
*, 4> UnresolvedCalls
;
748 bool IsFixed
= false;
749 bool IsValidState
= true;
753 AAReturnedValuesImpl(const IRPosition
&IRP
) : AAReturnedValues(IRP
) {}
755 /// See AbstractAttribute::initialize(...).
756 void initialize(Attributor
&A
) override
{
760 ReturnedValues
.clear();
762 Function
*F
= getAssociatedFunction();
764 indicatePessimisticFixpoint();
768 // The map from instruction opcodes to those instructions in the function.
769 auto &OpcodeInstMap
= A
.getInfoCache().getOpcodeInstMapForFunction(*F
);
771 // Look through all arguments, if one is marked as returned we are done.
772 for (Argument
&Arg
: F
->args()) {
773 if (Arg
.hasReturnedAttr()) {
774 auto &ReturnInstSet
= ReturnedValues
[&Arg
];
775 for (Instruction
*RI
: OpcodeInstMap
[Instruction::Ret
])
776 ReturnInstSet
.insert(cast
<ReturnInst
>(RI
));
778 indicateOptimisticFixpoint();
783 if (!F
->hasExactDefinition())
784 indicatePessimisticFixpoint();
787 /// See AbstractAttribute::manifest(...).
788 ChangeStatus
manifest(Attributor
&A
) override
;
790 /// See AbstractAttribute::getState(...).
791 AbstractState
&getState() override
{ return *this; }
793 /// See AbstractAttribute::getState(...).
794 const AbstractState
&getState() const override
{ return *this; }
796 /// See AbstractAttribute::updateImpl(Attributor &A).
797 ChangeStatus
updateImpl(Attributor
&A
) override
;
799 llvm::iterator_range
<iterator
> returned_values() override
{
800 return llvm::make_range(ReturnedValues
.begin(), ReturnedValues
.end());
803 llvm::iterator_range
<const_iterator
> returned_values() const override
{
804 return llvm::make_range(ReturnedValues
.begin(), ReturnedValues
.end());
807 const SmallSetVector
<CallBase
*, 4> &getUnresolvedCalls() const override
{
808 return UnresolvedCalls
;
811 /// Return the number of potential return values, -1 if unknown.
812 size_t getNumReturnValues() const override
{
813 return isValidState() ? ReturnedValues
.size() : -1;
816 /// Return an assumed unique return value if a single candidate is found. If
817 /// there cannot be one, return a nullptr. If it is not clear yet, return the
818 /// Optional::NoneType.
819 Optional
<Value
*> getAssumedUniqueReturnValue(Attributor
&A
) const;
821 /// See AbstractState::checkForAllReturnedValues(...).
822 bool checkForAllReturnedValuesAndReturnInsts(
823 const function_ref
<bool(Value
&, const SmallSetVector
<ReturnInst
*, 4> &)>
824 &Pred
) const override
;
826 /// Pretty print the attribute similar to the IR representation.
827 const std::string
getAsStr() const override
;
829 /// See AbstractState::isAtFixpoint().
830 bool isAtFixpoint() const override
{ return IsFixed
; }
832 /// See AbstractState::isValidState().
833 bool isValidState() const override
{ return IsValidState
; }
835 /// See AbstractState::indicateOptimisticFixpoint(...).
836 ChangeStatus
indicateOptimisticFixpoint() override
{
838 return ChangeStatus::UNCHANGED
;
841 ChangeStatus
indicatePessimisticFixpoint() override
{
843 IsValidState
= false;
844 return ChangeStatus::CHANGED
;
848 ChangeStatus
AAReturnedValuesImpl::manifest(Attributor
&A
) {
849 ChangeStatus Changed
= ChangeStatus::UNCHANGED
;
852 assert(isValidState());
853 STATS_DECLTRACK(KnownReturnValues
, FunctionReturn
,
854 "Number of function with known return values");
856 // Check if we have an assumed unique return value that we could manifest.
857 Optional
<Value
*> UniqueRV
= getAssumedUniqueReturnValue(A
);
859 if (!UniqueRV
.hasValue() || !UniqueRV
.getValue())
863 STATS_DECLTRACK(UniqueReturnValue
, FunctionReturn
,
864 "Number of function with unique return");
866 // Callback to replace the uses of CB with the constant C.
867 auto ReplaceCallSiteUsersWith
= [](CallBase
&CB
, Constant
&C
) {
868 if (CB
.getNumUses() == 0)
869 return ChangeStatus::UNCHANGED
;
870 CB
.replaceAllUsesWith(&C
);
871 return ChangeStatus::CHANGED
;
874 // If the assumed unique return value is an argument, annotate it.
875 if (auto *UniqueRVArg
= dyn_cast
<Argument
>(UniqueRV
.getValue())) {
876 getIRPosition() = IRPosition::argument(*UniqueRVArg
);
877 Changed
= IRAttribute::manifest(A
);
878 } else if (auto *RVC
= dyn_cast
<Constant
>(UniqueRV
.getValue())) {
879 // We can replace the returned value with the unique returned constant.
880 Value
&AnchorValue
= getAnchorValue();
881 if (Function
*F
= dyn_cast
<Function
>(&AnchorValue
)) {
882 for (const Use
&U
: F
->uses())
883 if (CallBase
*CB
= dyn_cast
<CallBase
>(U
.getUser()))
884 if (CB
->isCallee(&U
)) {
886 ConstantExpr::getTruncOrBitCast(RVC
, CB
->getType());
887 Changed
= ReplaceCallSiteUsersWith(*CB
, *RVCCast
) | Changed
;
890 assert(isa
<CallBase
>(AnchorValue
) &&
891 "Expcected a function or call base anchor!");
893 ConstantExpr::getTruncOrBitCast(RVC
, AnchorValue
.getType());
894 Changed
= ReplaceCallSiteUsersWith(cast
<CallBase
>(AnchorValue
), *RVCCast
);
896 if (Changed
== ChangeStatus::CHANGED
)
897 STATS_DECLTRACK(UniqueConstantReturnValue
, FunctionReturn
,
898 "Number of function returns replaced by constant return");
904 const std::string
AAReturnedValuesImpl::getAsStr() const {
905 return (isAtFixpoint() ? "returns(#" : "may-return(#") +
906 (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
907 ")[#UC: " + std::to_string(UnresolvedCalls
.size()) + "]";
911 AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor
&A
) const {
912 // If checkForAllReturnedValues provides a unique value, ignoring potential
913 // undef values that can also be present, it is assumed to be the actual
914 // return value and forwarded to the caller of this method. If there are
915 // multiple, a nullptr is returned indicating there cannot be a unique
917 Optional
<Value
*> UniqueRV
;
919 auto Pred
= [&](Value
&RV
) -> bool {
920 // If we found a second returned value and neither the current nor the saved
921 // one is an undef, there is no unique returned value. Undefs are special
922 // since we can pretend they have any value.
923 if (UniqueRV
.hasValue() && UniqueRV
!= &RV
&&
924 !(isa
<UndefValue
>(RV
) || isa
<UndefValue
>(UniqueRV
.getValue()))) {
929 // Do not overwrite a value with an undef.
930 if (!UniqueRV
.hasValue() || !isa
<UndefValue
>(RV
))
936 if (!A
.checkForAllReturnedValues(Pred
, *this))
942 bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
943 const function_ref
<bool(Value
&, const SmallSetVector
<ReturnInst
*, 4> &)>
948 // Check all returned values but ignore call sites as long as we have not
949 // encountered an overdefined one during an update.
950 for (auto &It
: ReturnedValues
) {
951 Value
*RV
= It
.first
;
953 CallBase
*CB
= dyn_cast
<CallBase
>(RV
);
954 if (CB
&& !UnresolvedCalls
.count(CB
))
957 if (!Pred(*RV
, It
.second
))
964 ChangeStatus
AAReturnedValuesImpl::updateImpl(Attributor
&A
) {
965 size_t NumUnresolvedCalls
= UnresolvedCalls
.size();
966 bool Changed
= false;
968 // State used in the value traversals starting in returned values.
970 // The map in which we collect return values -> return instrs.
971 decltype(ReturnedValues
) &RetValsMap
;
972 // The flag to indicate a change.
974 // The return instrs we come from.
975 SmallSetVector
<ReturnInst
*, 4> RetInsts
;
978 // Callback for a leaf value returned by the associated function.
979 auto VisitValueCB
= [](Value
&Val
, RVState
&RVS
, bool) -> bool {
980 auto Size
= RVS
.RetValsMap
[&Val
].size();
981 RVS
.RetValsMap
[&Val
].insert(RVS
.RetInsts
.begin(), RVS
.RetInsts
.end());
982 bool Inserted
= RVS
.RetValsMap
[&Val
].size() != Size
;
983 RVS
.Changed
|= Inserted
;
986 dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val
987 << " => " << RVS
.RetInsts
.size() << "\n";
992 // Helper method to invoke the generic value traversal.
993 auto VisitReturnedValue
= [&](Value
&RV
, RVState
&RVS
) {
994 IRPosition RetValPos
= IRPosition::value(RV
);
995 return genericValueTraversal
<AAReturnedValues
, RVState
>(A
, RetValPos
, *this,
999 // Callback for all "return intructions" live in the associated function.
1000 auto CheckReturnInst
= [this, &VisitReturnedValue
, &Changed
](Instruction
&I
) {
1001 ReturnInst
&Ret
= cast
<ReturnInst
>(I
);
1002 RVState
RVS({ReturnedValues
, Changed
, {}});
1003 RVS
.RetInsts
.insert(&Ret
);
1004 return VisitReturnedValue(*Ret
.getReturnValue(), RVS
);
1007 // Start by discovering returned values from all live returned instructions in
1008 // the associated function.
1009 if (!A
.checkForAllInstructions(CheckReturnInst
, *this, {Instruction::Ret
}))
1010 return indicatePessimisticFixpoint();
1012 // Once returned values "directly" present in the code are handled we try to
1013 // resolve returned calls.
1014 decltype(ReturnedValues
) NewRVsMap
;
1015 for (auto &It
: ReturnedValues
) {
1016 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It
.first
1017 << " by #" << It
.second
.size() << " RIs\n");
1018 CallBase
*CB
= dyn_cast
<CallBase
>(It
.first
);
1019 if (!CB
|| UnresolvedCalls
.count(CB
))
1022 if (!CB
->getCalledFunction()) {
1023 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1025 UnresolvedCalls
.insert(CB
);
1029 // TODO: use the function scope once we have call site AAReturnedValues.
1030 const auto &RetValAA
= A
.getAAFor
<AAReturnedValues
>(
1031 *this, IRPosition::function(*CB
->getCalledFunction()));
1032 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: "
1033 << static_cast<const AbstractAttribute
&>(RetValAA
)
1036 // Skip dead ends, thus if we do not know anything about the returned
1037 // call we mark it as unresolved and it will stay that way.
1038 if (!RetValAA
.getState().isValidState()) {
1039 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1041 UnresolvedCalls
.insert(CB
);
1045 // Do not try to learn partial information. If the callee has unresolved
1046 // return values we will treat the call as unresolved/opaque.
1047 auto &RetValAAUnresolvedCalls
= RetValAA
.getUnresolvedCalls();
1048 if (!RetValAAUnresolvedCalls
.empty()) {
1049 UnresolvedCalls
.insert(CB
);
1053 // Now check if we can track transitively returned values. If possible, thus
1054 // if all return value can be represented in the current scope, do so.
1055 bool Unresolved
= false;
1056 for (auto &RetValAAIt
: RetValAA
.returned_values()) {
1057 Value
*RetVal
= RetValAAIt
.first
;
1058 if (isa
<Argument
>(RetVal
) || isa
<CallBase
>(RetVal
) ||
1059 isa
<Constant
>(RetVal
))
1061 // Anything that did not fit in the above categories cannot be resolved,
1062 // mark the call as unresolved.
1063 LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value "
1064 "cannot be translated: "
1065 << *RetVal
<< "\n");
1066 UnresolvedCalls
.insert(CB
);
1074 // Now track transitively returned values.
1075 unsigned &NumRetAA
= NumReturnedValuesPerKnownAA
[CB
];
1076 if (NumRetAA
== RetValAA
.getNumReturnValues()) {
1077 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not "
1078 "changed since it was seen last\n");
1081 NumRetAA
= RetValAA
.getNumReturnValues();
1083 for (auto &RetValAAIt
: RetValAA
.returned_values()) {
1084 Value
*RetVal
= RetValAAIt
.first
;
1085 if (Argument
*Arg
= dyn_cast
<Argument
>(RetVal
)) {
1086 // Arguments are mapped to call site operands and we begin the traversal
1088 bool Unused
= false;
1089 RVState
RVS({NewRVsMap
, Unused
, RetValAAIt
.second
});
1090 VisitReturnedValue(*CB
->getArgOperand(Arg
->getArgNo()), RVS
);
1092 } else if (isa
<CallBase
>(RetVal
)) {
1093 // Call sites are resolved by the callee attribute over time, no need to
1094 // do anything for us.
1096 } else if (isa
<Constant
>(RetVal
)) {
1097 // Constants are valid everywhere, we can simply take them.
1098 NewRVsMap
[RetVal
].insert(It
.second
.begin(), It
.second
.end());
1104 // To avoid modifications to the ReturnedValues map while we iterate over it
1105 // we kept record of potential new entries in a copy map, NewRVsMap.
1106 for (auto &It
: NewRVsMap
) {
1107 assert(!It
.second
.empty() && "Entry does not add anything.");
1108 auto &ReturnInsts
= ReturnedValues
[It
.first
];
1109 for (ReturnInst
*RI
: It
.second
)
1110 if (ReturnInsts
.insert(RI
)) {
1111 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
1112 << *It
.first
<< " => " << *RI
<< "\n");
1117 Changed
|= (NumUnresolvedCalls
!= UnresolvedCalls
.size());
1118 return Changed
? ChangeStatus::CHANGED
: ChangeStatus::UNCHANGED
;
1121 struct AAReturnedValuesFunction final
: public AAReturnedValuesImpl
{
1122 AAReturnedValuesFunction(const IRPosition
&IRP
) : AAReturnedValuesImpl(IRP
) {}
1124 /// See AbstractAttribute::trackStatistics()
1125 void trackStatistics() const override
{ STATS_DECLTRACK_ARG_ATTR(returned
) }
1128 /// Returned values information for a call sites.
1129 struct AAReturnedValuesCallSite final
: AAReturnedValuesImpl
{
1130 AAReturnedValuesCallSite(const IRPosition
&IRP
) : AAReturnedValuesImpl(IRP
) {}
1132 /// See AbstractAttribute::initialize(...).
1133 void initialize(Attributor
&A
) override
{
1134 // TODO: Once we have call site specific value information we can provide
1135 // call site specific liveness information and then it makes
1136 // sense to specialize attributes for call sites instead of
1137 // redirecting requests to the callee.
1138 llvm_unreachable("Abstract attributes for returned values are not "
1139 "supported for call sites yet!");
1142 /// See AbstractAttribute::updateImpl(...).
1143 ChangeStatus
updateImpl(Attributor
&A
) override
{
1144 return indicatePessimisticFixpoint();
1147 /// See AbstractAttribute::trackStatistics()
1148 void trackStatistics() const override
{}
1151 /// ------------------------ NoSync Function Attribute -------------------------
1153 struct AANoSyncImpl
: AANoSync
{
1154 AANoSyncImpl(const IRPosition
&IRP
) : AANoSync(IRP
) {}
1156 const std::string
getAsStr() const override
{
1157 return getAssumed() ? "nosync" : "may-sync";
1160 /// See AbstractAttribute::updateImpl(...).
1161 ChangeStatus
updateImpl(Attributor
&A
) override
;
1163 /// Helper function used to determine whether an instruction is non-relaxed
1164 /// atomic. In other words, if an atomic instruction does not have unordered
1165 /// or monotonic ordering
1166 static bool isNonRelaxedAtomic(Instruction
*I
);
1168 /// Helper function used to determine whether an instruction is volatile.
1169 static bool isVolatile(Instruction
*I
);
1171 /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
1173 static bool isNoSyncIntrinsic(Instruction
*I
);
1176 bool AANoSyncImpl::isNonRelaxedAtomic(Instruction
*I
) {
1180 AtomicOrdering Ordering
;
1181 switch (I
->getOpcode()) {
1182 case Instruction::AtomicRMW
:
1183 Ordering
= cast
<AtomicRMWInst
>(I
)->getOrdering();
1185 case Instruction::Store
:
1186 Ordering
= cast
<StoreInst
>(I
)->getOrdering();
1188 case Instruction::Load
:
1189 Ordering
= cast
<LoadInst
>(I
)->getOrdering();
1191 case Instruction::Fence
: {
1192 auto *FI
= cast
<FenceInst
>(I
);
1193 if (FI
->getSyncScopeID() == SyncScope::SingleThread
)
1195 Ordering
= FI
->getOrdering();
1198 case Instruction::AtomicCmpXchg
: {
1199 AtomicOrdering Success
= cast
<AtomicCmpXchgInst
>(I
)->getSuccessOrdering();
1200 AtomicOrdering Failure
= cast
<AtomicCmpXchgInst
>(I
)->getFailureOrdering();
1201 // Only if both are relaxed, than it can be treated as relaxed.
1202 // Otherwise it is non-relaxed.
1203 if (Success
!= AtomicOrdering::Unordered
&&
1204 Success
!= AtomicOrdering::Monotonic
)
1206 if (Failure
!= AtomicOrdering::Unordered
&&
1207 Failure
!= AtomicOrdering::Monotonic
)
1213 "New atomic operations need to be known in the attributor.");
1217 if (Ordering
== AtomicOrdering::Unordered
||
1218 Ordering
== AtomicOrdering::Monotonic
)
1223 /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
1224 /// FIXME: We should ipmrove the handling of intrinsics.
1225 bool AANoSyncImpl::isNoSyncIntrinsic(Instruction
*I
) {
1226 if (auto *II
= dyn_cast
<IntrinsicInst
>(I
)) {
1227 switch (II
->getIntrinsicID()) {
1228 /// Element wise atomic memory intrinsics are can only be unordered,
1229 /// therefore nosync.
1230 case Intrinsic::memset_element_unordered_atomic
:
1231 case Intrinsic::memmove_element_unordered_atomic
:
1232 case Intrinsic::memcpy_element_unordered_atomic
:
1234 case Intrinsic::memset
:
1235 case Intrinsic::memmove
:
1236 case Intrinsic::memcpy
:
1237 if (!cast
<MemIntrinsic
>(II
)->isVolatile())
1247 bool AANoSyncImpl::isVolatile(Instruction
*I
) {
1248 assert(!ImmutableCallSite(I
) && !isa
<CallBase
>(I
) &&
1249 "Calls should not be checked here");
1251 switch (I
->getOpcode()) {
1252 case Instruction::AtomicRMW
:
1253 return cast
<AtomicRMWInst
>(I
)->isVolatile();
1254 case Instruction::Store
:
1255 return cast
<StoreInst
>(I
)->isVolatile();
1256 case Instruction::Load
:
1257 return cast
<LoadInst
>(I
)->isVolatile();
1258 case Instruction::AtomicCmpXchg
:
1259 return cast
<AtomicCmpXchgInst
>(I
)->isVolatile();
1265 ChangeStatus
AANoSyncImpl::updateImpl(Attributor
&A
) {
1267 auto CheckRWInstForNoSync
= [&](Instruction
&I
) {
1268 /// We are looking for volatile instructions or Non-Relaxed atomics.
1269 /// FIXME: We should ipmrove the handling of intrinsics.
1271 if (isa
<IntrinsicInst
>(&I
) && isNoSyncIntrinsic(&I
))
1274 if (ImmutableCallSite ICS
= ImmutableCallSite(&I
)) {
1275 if (ICS
.hasFnAttr(Attribute::NoSync
))
1278 const auto &NoSyncAA
=
1279 A
.getAAFor
<AANoSync
>(*this, IRPosition::callsite_function(ICS
));
1280 if (NoSyncAA
.isAssumedNoSync())
1285 if (!isVolatile(&I
) && !isNonRelaxedAtomic(&I
))
1291 auto CheckForNoSync
= [&](Instruction
&I
) {
1292 // At this point we handled all read/write effects and they are all
1293 // nosync, so they can be skipped.
1294 if (I
.mayReadOrWriteMemory())
1297 // non-convergent and readnone imply nosync.
1298 return !ImmutableCallSite(&I
).isConvergent();
1301 if (!A
.checkForAllReadWriteInstructions(CheckRWInstForNoSync
, *this) ||
1302 !A
.checkForAllCallLikeInstructions(CheckForNoSync
, *this))
1303 return indicatePessimisticFixpoint();
1305 return ChangeStatus::UNCHANGED
;
1308 struct AANoSyncFunction final
: public AANoSyncImpl
{
1309 AANoSyncFunction(const IRPosition
&IRP
) : AANoSyncImpl(IRP
) {}
1311 /// See AbstractAttribute::trackStatistics()
1312 void trackStatistics() const override
{ STATS_DECLTRACK_FN_ATTR(nosync
) }
1315 /// NoSync attribute deduction for a call sites.
1316 struct AANoSyncCallSite final
: AANoSyncImpl
{
1317 AANoSyncCallSite(const IRPosition
&IRP
) : AANoSyncImpl(IRP
) {}
1319 /// See AbstractAttribute::initialize(...).
1320 void initialize(Attributor
&A
) override
{
1321 AANoSyncImpl::initialize(A
);
1322 Function
*F
= getAssociatedFunction();
1324 indicatePessimisticFixpoint();
1327 /// See AbstractAttribute::updateImpl(...).
1328 ChangeStatus
updateImpl(Attributor
&A
) override
{
1329 // TODO: Once we have call site specific value information we can provide
1330 // call site specific liveness information and then it makes
1331 // sense to specialize attributes for call sites arguments instead of
1332 // redirecting requests to the callee argument.
1333 Function
*F
= getAssociatedFunction();
1334 const IRPosition
&FnPos
= IRPosition::function(*F
);
1335 auto &FnAA
= A
.getAAFor
<AANoSync
>(*this, FnPos
);
1336 return clampStateAndIndicateChange(
1337 getState(), static_cast<const AANoSync::StateType
&>(FnAA
.getState()));
1340 /// See AbstractAttribute::trackStatistics()
1341 void trackStatistics() const override
{ STATS_DECLTRACK_CS_ATTR(nosync
); }
1344 /// ------------------------ No-Free Attributes ----------------------------
1346 struct AANoFreeImpl
: public AANoFree
{
1347 AANoFreeImpl(const IRPosition
&IRP
) : AANoFree(IRP
) {}
1349 /// See AbstractAttribute::updateImpl(...).
1350 ChangeStatus
updateImpl(Attributor
&A
) override
{
1351 auto CheckForNoFree
= [&](Instruction
&I
) {
1352 ImmutableCallSite
ICS(&I
);
1353 if (ICS
.hasFnAttr(Attribute::NoFree
))
1356 const auto &NoFreeAA
=
1357 A
.getAAFor
<AANoFree
>(*this, IRPosition::callsite_function(ICS
));
1358 return NoFreeAA
.isAssumedNoFree();
1361 if (!A
.checkForAllCallLikeInstructions(CheckForNoFree
, *this))
1362 return indicatePessimisticFixpoint();
1363 return ChangeStatus::UNCHANGED
;
1366 /// See AbstractAttribute::getAsStr().
1367 const std::string
getAsStr() const override
{
1368 return getAssumed() ? "nofree" : "may-free";
1372 struct AANoFreeFunction final
: public AANoFreeImpl
{
1373 AANoFreeFunction(const IRPosition
&IRP
) : AANoFreeImpl(IRP
) {}
1375 /// See AbstractAttribute::trackStatistics()
1376 void trackStatistics() const override
{ STATS_DECLTRACK_FN_ATTR(nofree
) }
1379 /// NoFree attribute deduction for a call sites.
1380 struct AANoFreeCallSite final
: AANoFreeImpl
{
1381 AANoFreeCallSite(const IRPosition
&IRP
) : AANoFreeImpl(IRP
) {}
1383 /// See AbstractAttribute::initialize(...).
1384 void initialize(Attributor
&A
) override
{
1385 AANoFreeImpl::initialize(A
);
1386 Function
*F
= getAssociatedFunction();
1388 indicatePessimisticFixpoint();
1391 /// See AbstractAttribute::updateImpl(...).
1392 ChangeStatus
updateImpl(Attributor
&A
) override
{
1393 // TODO: Once we have call site specific value information we can provide
1394 // call site specific liveness information and then it makes
1395 // sense to specialize attributes for call sites arguments instead of
1396 // redirecting requests to the callee argument.
1397 Function
*F
= getAssociatedFunction();
1398 const IRPosition
&FnPos
= IRPosition::function(*F
);
1399 auto &FnAA
= A
.getAAFor
<AANoFree
>(*this, FnPos
);
1400 return clampStateAndIndicateChange(
1401 getState(), static_cast<const AANoFree::StateType
&>(FnAA
.getState()));
1404 /// See AbstractAttribute::trackStatistics()
1405 void trackStatistics() const override
{ STATS_DECLTRACK_CS_ATTR(nofree
); }
1408 /// ------------------------ NonNull Argument Attribute ------------------------
1409 struct AANonNullImpl
: AANonNull
{
1410 AANonNullImpl(const IRPosition
&IRP
) : AANonNull(IRP
) {}
1412 /// See AbstractAttribute::initialize(...).
1413 void initialize(Attributor
&A
) override
{
1414 if (hasAttr({Attribute::NonNull
, Attribute::Dereferenceable
}))
1415 indicateOptimisticFixpoint();
1417 AANonNull::initialize(A
);
1420 /// See AbstractAttribute::getAsStr().
1421 const std::string
getAsStr() const override
{
1422 return getAssumed() ? "nonnull" : "may-null";
1426 /// NonNull attribute for a floating value.
1427 struct AANonNullFloating
: AANonNullImpl
{
1428 AANonNullFloating(const IRPosition
&IRP
) : AANonNullImpl(IRP
) {}
1430 /// See AbstractAttribute::initialize(...).
1431 void initialize(Attributor
&A
) override
{
1432 AANonNullImpl::initialize(A
);
1437 const IRPosition
&IRP
= getIRPosition();
1438 const Value
&V
= IRP
.getAssociatedValue();
1439 const DataLayout
&DL
= A
.getDataLayout();
1441 // TODO: This context sensitive query should be removed once we can do
1442 // context sensitive queries in the genericValueTraversal below.
1443 if (isKnownNonZero(&V
, DL
, 0, /* TODO: AC */ nullptr, IRP
.getCtxI(),
1444 /* TODO: DT */ nullptr))
1445 indicateOptimisticFixpoint();
1448 /// See AbstractAttribute::updateImpl(...).
1449 ChangeStatus
updateImpl(Attributor
&A
) override
{
1450 const DataLayout
&DL
= A
.getDataLayout();
1452 auto VisitValueCB
= [&](Value
&V
, AAAlign::StateType
&T
,
1453 bool Stripped
) -> bool {
1454 const auto &AA
= A
.getAAFor
<AANonNull
>(*this, IRPosition::value(V
));
1455 if (!Stripped
&& this == &AA
) {
1456 if (!isKnownNonZero(&V
, DL
, 0, /* TODO: AC */ nullptr,
1457 /* TODO: CtxI */ nullptr,
1458 /* TODO: DT */ nullptr))
1459 T
.indicatePessimisticFixpoint();
1461 // Use abstract attribute information.
1462 const AANonNull::StateType
&NS
=
1463 static_cast<const AANonNull::StateType
&>(AA
.getState());
1466 return T
.isValidState();
1470 if (!genericValueTraversal
<AANonNull
, StateType
>(A
, getIRPosition(), *this,
1472 return indicatePessimisticFixpoint();
1474 return clampStateAndIndicateChange(getState(), T
);
1477 /// See AbstractAttribute::trackStatistics()
1478 void trackStatistics() const override
{ STATS_DECLTRACK_FNRET_ATTR(nonnull
) }
1481 /// NonNull attribute for function return value.
1482 struct AANonNullReturned final
1483 : AAReturnedFromReturnedValues
<AANonNull
, AANonNullImpl
> {
1484 AANonNullReturned(const IRPosition
&IRP
)
1485 : AAReturnedFromReturnedValues
<AANonNull
, AANonNullImpl
>(IRP
) {}
1487 /// See AbstractAttribute::trackStatistics()
1488 void trackStatistics() const override
{ STATS_DECLTRACK_FNRET_ATTR(nonnull
) }
1491 /// NonNull attribute for function argument.
1492 struct AANonNullArgument final
1493 : AAArgumentFromCallSiteArguments
<AANonNull
, AANonNullImpl
> {
1494 AANonNullArgument(const IRPosition
&IRP
)
1495 : AAArgumentFromCallSiteArguments
<AANonNull
, AANonNullImpl
>(IRP
) {}
1497 /// See AbstractAttribute::trackStatistics()
1498 void trackStatistics() const override
{ STATS_DECLTRACK_ARG_ATTR(nonnull
) }
1501 struct AANonNullCallSiteArgument final
: AANonNullFloating
{
1502 AANonNullCallSiteArgument(const IRPosition
&IRP
) : AANonNullFloating(IRP
) {}
1504 /// See AbstractAttribute::trackStatistics()
1505 void trackStatistics() const override
{ STATS_DECLTRACK_CSARG_ATTR(nonnull
) }
1508 /// NonNull attribute for a call site return position.
1509 struct AANonNullCallSiteReturned final
1510 : AACallSiteReturnedFromReturned
<AANonNull
, AANonNullImpl
> {
1511 AANonNullCallSiteReturned(const IRPosition
&IRP
)
1512 : AACallSiteReturnedFromReturned
<AANonNull
, AANonNullImpl
>(IRP
) {}
1514 /// See AbstractAttribute::trackStatistics()
1515 void trackStatistics() const override
{ STATS_DECLTRACK_CSRET_ATTR(nonnull
) }
1518 /// ------------------------ No-Recurse Attributes ----------------------------
1520 struct AANoRecurseImpl
: public AANoRecurse
{
1521 AANoRecurseImpl(const IRPosition
&IRP
) : AANoRecurse(IRP
) {}
1523 /// See AbstractAttribute::getAsStr()
1524 const std::string
getAsStr() const override
{
1525 return getAssumed() ? "norecurse" : "may-recurse";
1529 struct AANoRecurseFunction final
: AANoRecurseImpl
{
1530 AANoRecurseFunction(const IRPosition
&IRP
) : AANoRecurseImpl(IRP
) {}
1532 /// See AbstractAttribute::updateImpl(...).
1533 ChangeStatus
updateImpl(Attributor
&A
) override
{
1534 // TODO: Implement this.
1535 return indicatePessimisticFixpoint();
1538 void trackStatistics() const override
{ STATS_DECLTRACK_FN_ATTR(norecurse
) }
1541 /// NoRecurse attribute deduction for a call sites.
1542 struct AANoRecurseCallSite final
: AANoRecurseImpl
{
1543 AANoRecurseCallSite(const IRPosition
&IRP
) : AANoRecurseImpl(IRP
) {}
1545 /// See AbstractAttribute::initialize(...).
1546 void initialize(Attributor
&A
) override
{
1547 AANoRecurseImpl::initialize(A
);
1548 Function
*F
= getAssociatedFunction();
1550 indicatePessimisticFixpoint();
1553 /// See AbstractAttribute::updateImpl(...).
1554 ChangeStatus
updateImpl(Attributor
&A
) override
{
1555 // TODO: Once we have call site specific value information we can provide
1556 // call site specific liveness information and then it makes
1557 // sense to specialize attributes for call sites arguments instead of
1558 // redirecting requests to the callee argument.
1559 Function
*F
= getAssociatedFunction();
1560 const IRPosition
&FnPos
= IRPosition::function(*F
);
1561 auto &FnAA
= A
.getAAFor
<AANoRecurse
>(*this, FnPos
);
1562 return clampStateAndIndicateChange(
1564 static_cast<const AANoRecurse::StateType
&>(FnAA
.getState()));
1567 /// See AbstractAttribute::trackStatistics()
1568 void trackStatistics() const override
{ STATS_DECLTRACK_CS_ATTR(norecurse
); }
1571 /// ------------------------ Will-Return Attributes ----------------------------
1573 // Helper function that checks whether a function has any cycle.
1574 // TODO: Replace with more efficent code
1575 static bool containsCycle(Function
&F
) {
1576 SmallPtrSet
<BasicBlock
*, 32> Visited
;
1578 // Traverse BB by dfs and check whether successor is already visited.
1579 for (BasicBlock
*BB
: depth_first(&F
)) {
1581 for (auto *SuccBB
: successors(BB
)) {
1582 if (Visited
.count(SuccBB
))
1589 // Helper function that checks the function have a loop which might become an
1591 // FIXME: Any cycle is regarded as endless loop for now.
1592 // We have to allow some patterns.
1593 static bool containsPossiblyEndlessLoop(Function
*F
) {
1594 return !F
|| !F
->hasExactDefinition() || containsCycle(*F
);
1597 struct AAWillReturnImpl
: public AAWillReturn
{
1598 AAWillReturnImpl(const IRPosition
&IRP
) : AAWillReturn(IRP
) {}
1600 /// See AbstractAttribute::initialize(...).
1601 void initialize(Attributor
&A
) override
{
1602 AAWillReturn::initialize(A
);
1604 Function
*F
= getAssociatedFunction();
1605 if (containsPossiblyEndlessLoop(F
))
1606 indicatePessimisticFixpoint();
1609 /// See AbstractAttribute::updateImpl(...).
1610 ChangeStatus
updateImpl(Attributor
&A
) override
{
1611 auto CheckForWillReturn
= [&](Instruction
&I
) {
1612 IRPosition IPos
= IRPosition::callsite_function(ImmutableCallSite(&I
));
1613 const auto &WillReturnAA
= A
.getAAFor
<AAWillReturn
>(*this, IPos
);
1614 if (WillReturnAA
.isKnownWillReturn())
1616 if (!WillReturnAA
.isAssumedWillReturn())
1618 const auto &NoRecurseAA
= A
.getAAFor
<AANoRecurse
>(*this, IPos
);
1619 return NoRecurseAA
.isAssumedNoRecurse();
1622 if (!A
.checkForAllCallLikeInstructions(CheckForWillReturn
, *this))
1623 return indicatePessimisticFixpoint();
1625 return ChangeStatus::UNCHANGED
;
1628 /// See AbstractAttribute::getAsStr()
1629 const std::string
getAsStr() const override
{
1630 return getAssumed() ? "willreturn" : "may-noreturn";
1634 struct AAWillReturnFunction final
: AAWillReturnImpl
{
1635 AAWillReturnFunction(const IRPosition
&IRP
) : AAWillReturnImpl(IRP
) {}
1637 /// See AbstractAttribute::trackStatistics()
1638 void trackStatistics() const override
{ STATS_DECLTRACK_FN_ATTR(willreturn
) }
1641 /// WillReturn attribute deduction for a call sites.
1642 struct AAWillReturnCallSite final
: AAWillReturnImpl
{
1643 AAWillReturnCallSite(const IRPosition
&IRP
) : AAWillReturnImpl(IRP
) {}
1645 /// See AbstractAttribute::initialize(...).
1646 void initialize(Attributor
&A
) override
{
1647 AAWillReturnImpl::initialize(A
);
1648 Function
*F
= getAssociatedFunction();
1650 indicatePessimisticFixpoint();
1653 /// See AbstractAttribute::updateImpl(...).
1654 ChangeStatus
updateImpl(Attributor
&A
) override
{
1655 // TODO: Once we have call site specific value information we can provide
1656 // call site specific liveness information and then it makes
1657 // sense to specialize attributes for call sites arguments instead of
1658 // redirecting requests to the callee argument.
1659 Function
*F
= getAssociatedFunction();
1660 const IRPosition
&FnPos
= IRPosition::function(*F
);
1661 auto &FnAA
= A
.getAAFor
<AAWillReturn
>(*this, FnPos
);
1662 return clampStateAndIndicateChange(
1664 static_cast<const AAWillReturn::StateType
&>(FnAA
.getState()));
1667 /// See AbstractAttribute::trackStatistics()
1668 void trackStatistics() const override
{ STATS_DECLTRACK_CS_ATTR(willreturn
); }
1671 /// ------------------------ NoAlias Argument Attribute ------------------------
1673 struct AANoAliasImpl
: AANoAlias
{
1674 AANoAliasImpl(const IRPosition
&IRP
) : AANoAlias(IRP
) {}
1676 const std::string
getAsStr() const override
{
1677 return getAssumed() ? "noalias" : "may-alias";
1681 /// NoAlias attribute for a floating value.
1682 struct AANoAliasFloating final
: AANoAliasImpl
{
1683 AANoAliasFloating(const IRPosition
&IRP
) : AANoAliasImpl(IRP
) {}
1685 /// See AbstractAttribute::initialize(...).
1686 void initialize(Attributor
&A
) override
{
1687 AANoAliasImpl::initialize(A
);
1688 if (isa
<AllocaInst
>(getAnchorValue()))
1689 indicateOptimisticFixpoint();
1692 /// See AbstractAttribute::updateImpl(...).
1693 ChangeStatus
updateImpl(Attributor
&A
) override
{
1694 // TODO: Implement this.
1695 return indicatePessimisticFixpoint();
1698 /// See AbstractAttribute::trackStatistics()
1699 void trackStatistics() const override
{
1700 STATS_DECLTRACK_FLOATING_ATTR(noalias
)
1704 /// NoAlias attribute for an argument.
1705 struct AANoAliasArgument final
1706 : AAArgumentFromCallSiteArguments
<AANoAlias
, AANoAliasImpl
> {
1707 AANoAliasArgument(const IRPosition
&IRP
)
1708 : AAArgumentFromCallSiteArguments
<AANoAlias
, AANoAliasImpl
>(IRP
) {}
1710 /// See AbstractAttribute::trackStatistics()
1711 void trackStatistics() const override
{ STATS_DECLTRACK_ARG_ATTR(noalias
) }
1714 struct AANoAliasCallSiteArgument final
: AANoAliasImpl
{
1715 AANoAliasCallSiteArgument(const IRPosition
&IRP
) : AANoAliasImpl(IRP
) {}
1717 /// See AbstractAttribute::initialize(...).
1718 void initialize(Attributor
&A
) override
{
1719 // See callsite argument attribute and callee argument attribute.
1720 ImmutableCallSite
ICS(&getAnchorValue());
1721 if (ICS
.paramHasAttr(getArgNo(), Attribute::NoAlias
))
1722 indicateOptimisticFixpoint();
1725 /// See AbstractAttribute::updateImpl(...).
1726 ChangeStatus
updateImpl(Attributor
&A
) override
{
1727 // We can deduce "noalias" if the following conditions hold.
1728 // (i) Associated value is assumed to be noalias in the definition.
1729 // (ii) Associated value is assumed to be no-capture in all the uses
1730 // possibly executed before this callsite.
1731 // (iii) There is no other pointer argument which could alias with the
1734 const Value
&V
= getAssociatedValue();
1735 const IRPosition IRP
= IRPosition::value(V
);
1737 // (i) Check whether noalias holds in the definition.
1739 auto &NoAliasAA
= A
.getAAFor
<AANoAlias
>(*this, IRP
);
1741 if (!NoAliasAA
.isAssumedNoAlias())
1742 return indicatePessimisticFixpoint();
1744 LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] " << V
1745 << " is assumed NoAlias in the definition\n");
1747 // (ii) Check whether the value is captured in the scope using AANoCapture.
1748 // FIXME: This is conservative though, it is better to look at CFG and
1749 // check only uses possibly executed before this callsite.
1751 auto &NoCaptureAA
= A
.getAAFor
<AANoCapture
>(*this, IRP
);
1752 if (!NoCaptureAA
.isAssumedNoCaptureMaybeReturned())
1753 return indicatePessimisticFixpoint();
1755 // (iii) Check there is no other pointer argument which could alias with the
1757 ImmutableCallSite
ICS(&getAnchorValue());
1758 for (unsigned i
= 0; i
< ICS
.getNumArgOperands(); i
++) {
1759 if (getArgNo() == (int)i
)
1761 const Value
*ArgOp
= ICS
.getArgOperand(i
);
1762 if (!ArgOp
->getType()->isPointerTy())
1765 // TODO: Use AliasAnalysis
1766 // AAResults& AAR = ..;
1767 // if(AAR.isNoAlias(&getAssociatedValue(), ArgOp))
1768 // return indicatePessimitisicFixpoint();
1770 return indicatePessimisticFixpoint();
1773 return ChangeStatus::UNCHANGED
;
1776 /// See AbstractAttribute::trackStatistics()
1777 void trackStatistics() const override
{ STATS_DECLTRACK_CSARG_ATTR(noalias
) }
1780 /// NoAlias attribute for function return value.
1781 struct AANoAliasReturned final
: AANoAliasImpl
{
1782 AANoAliasReturned(const IRPosition
&IRP
) : AANoAliasImpl(IRP
) {}
1784 /// See AbstractAttribute::updateImpl(...).
1785 virtual ChangeStatus
updateImpl(Attributor
&A
) override
{
1787 auto CheckReturnValue
= [&](Value
&RV
) -> bool {
1788 if (Constant
*C
= dyn_cast
<Constant
>(&RV
))
1789 if (C
->isNullValue() || isa
<UndefValue
>(C
))
1792 /// For now, we can only deduce noalias if we have call sites.
1793 /// FIXME: add more support.
1794 ImmutableCallSite
ICS(&RV
);
1798 const IRPosition
&RVPos
= IRPosition::value(RV
);
1799 const auto &NoAliasAA
= A
.getAAFor
<AANoAlias
>(*this, RVPos
);
1800 if (!NoAliasAA
.isAssumedNoAlias())
1803 const auto &NoCaptureAA
= A
.getAAFor
<AANoCapture
>(*this, RVPos
);
1804 return NoCaptureAA
.isAssumedNoCaptureMaybeReturned();
1807 if (!A
.checkForAllReturnedValues(CheckReturnValue
, *this))
1808 return indicatePessimisticFixpoint();
1810 return ChangeStatus::UNCHANGED
;
1813 /// See AbstractAttribute::trackStatistics()
1814 void trackStatistics() const override
{ STATS_DECLTRACK_FNRET_ATTR(noalias
) }
1817 /// NoAlias attribute deduction for a call site return value.
1818 struct AANoAliasCallSiteReturned final
: AANoAliasImpl
{
1819 AANoAliasCallSiteReturned(const IRPosition
&IRP
) : AANoAliasImpl(IRP
) {}
1821 /// See AbstractAttribute::initialize(...).
1822 void initialize(Attributor
&A
) override
{
1823 AANoAliasImpl::initialize(A
);
1824 Function
*F
= getAssociatedFunction();
1826 indicatePessimisticFixpoint();
1829 /// See AbstractAttribute::updateImpl(...).
1830 ChangeStatus
updateImpl(Attributor
&A
) override
{
1831 // TODO: Once we have call site specific value information we can provide
1832 // call site specific liveness information and then it makes
1833 // sense to specialize attributes for call sites arguments instead of
1834 // redirecting requests to the callee argument.
1835 Function
*F
= getAssociatedFunction();
1836 const IRPosition
&FnPos
= IRPosition::returned(*F
);
1837 auto &FnAA
= A
.getAAFor
<AANoAlias
>(*this, FnPos
);
1838 return clampStateAndIndicateChange(
1839 getState(), static_cast<const AANoAlias::StateType
&>(FnAA
.getState()));
1842 /// See AbstractAttribute::trackStatistics()
1843 void trackStatistics() const override
{ STATS_DECLTRACK_CSRET_ATTR(noalias
); }
1846 /// -------------------AAIsDead Function Attribute-----------------------
1848 struct AAIsDeadImpl
: public AAIsDead
{
1849 AAIsDeadImpl(const IRPosition
&IRP
) : AAIsDead(IRP
) {}
1851 void initialize(Attributor
&A
) override
{
1852 const Function
*F
= getAssociatedFunction();
1853 if (F
&& !F
->isDeclaration())
1854 exploreFromEntry(A
, F
);
1857 void exploreFromEntry(Attributor
&A
, const Function
*F
) {
1858 ToBeExploredPaths
.insert(&(F
->getEntryBlock().front()));
1859 assumeLive(A
, F
->getEntryBlock());
1861 for (size_t i
= 0; i
< ToBeExploredPaths
.size(); ++i
)
1862 if (const Instruction
*NextNoReturnI
=
1863 findNextNoReturn(A
, ToBeExploredPaths
[i
]))
1864 NoReturnCalls
.insert(NextNoReturnI
);
1867 /// Find the next assumed noreturn instruction in the block of \p I starting
1868 /// from, thus including, \p I.
1870 /// The caller is responsible to monitor the ToBeExploredPaths set as new
1871 /// instructions discovered in other basic block will be placed in there.
1873 /// \returns The next assumed noreturn instructions in the block of \p I
1874 /// starting from, thus including, \p I.
1875 const Instruction
*findNextNoReturn(Attributor
&A
, const Instruction
*I
);
1877 /// See AbstractAttribute::getAsStr().
1878 const std::string
getAsStr() const override
{
1879 return "Live[#BB " + std::to_string(AssumedLiveBlocks
.size()) + "/" +
1880 std::to_string(getAssociatedFunction()->size()) + "][#NRI " +
1881 std::to_string(NoReturnCalls
.size()) + "]";
1884 /// See AbstractAttribute::manifest(...).
1885 ChangeStatus
manifest(Attributor
&A
) override
{
1886 assert(getState().isValidState() &&
1887 "Attempted to manifest an invalid state!");
1889 ChangeStatus HasChanged
= ChangeStatus::UNCHANGED
;
1890 Function
&F
= *getAssociatedFunction();
1892 if (AssumedLiveBlocks
.empty()) {
1893 A
.deleteAfterManifest(F
);
1894 return ChangeStatus::CHANGED
;
1897 // Flag to determine if we can change an invoke to a call assuming the
1898 // callee is nounwind. This is not possible if the personality of the
1899 // function allows to catch asynchronous exceptions.
1900 bool Invoke2CallAllowed
= !mayCatchAsynchronousExceptions(F
);
1902 for (const Instruction
*NRC
: NoReturnCalls
) {
1903 Instruction
*I
= const_cast<Instruction
*>(NRC
);
1904 BasicBlock
*BB
= I
->getParent();
1905 Instruction
*SplitPos
= I
->getNextNode();
1906 // TODO: mark stuff before unreachable instructions as dead.
1907 if (isa_and_nonnull
<UnreachableInst
>(SplitPos
))
1910 if (auto *II
= dyn_cast
<InvokeInst
>(I
)) {
1911 // If we keep the invoke the split position is at the beginning of the
1912 // normal desitination block (it invokes a noreturn function after all).
1913 BasicBlock
*NormalDestBB
= II
->getNormalDest();
1914 SplitPos
= &NormalDestBB
->front();
1916 /// Invoke is replaced with a call and unreachable is placed after it if
1917 /// the callee is nounwind and noreturn. Otherwise, we keep the invoke
1918 /// and only place an unreachable in the normal successor.
1919 if (Invoke2CallAllowed
) {
1920 if (II
->getCalledFunction()) {
1921 const IRPosition
&IPos
= IRPosition::callsite_function(*II
);
1922 const auto &AANoUnw
= A
.getAAFor
<AANoUnwind
>(*this, IPos
);
1923 if (AANoUnw
.isAssumedNoUnwind()) {
1925 << "[AAIsDead] Replace invoke with call inst\n");
1926 // We do not need an invoke (II) but instead want a call followed
1927 // by an unreachable. However, we do not remove II as other
1928 // abstract attributes might have it cached as part of their
1929 // results. Given that we modify the CFG anyway, we simply keep II
1930 // around but in a new dead block. To avoid II being live through
1931 // a different edge we have to ensure the block we place it in is
1932 // only reached from the current block of II and then not reached
1933 // at all when we insert the unreachable.
1934 SplitBlockPredecessors(NormalDestBB
, {BB
}, ".i2c");
1935 CallInst
*CI
= createCallMatchingInvoke(II
);
1936 CI
->insertBefore(II
);
1938 II
->replaceAllUsesWith(CI
);
1939 SplitPos
= CI
->getNextNode();
1944 if (SplitPos
== &NormalDestBB
->front()) {
1945 // If this is an invoke of a noreturn function the edge to the normal
1946 // destination block is dead but not necessarily the block itself.
1947 // TODO: We need to move to an edge based system during deduction and
1949 assert(!NormalDestBB
->isLandingPad() &&
1950 "Expected the normal destination not to be a landingpad!");
1951 BasicBlock
*SplitBB
=
1952 SplitBlockPredecessors(NormalDestBB
, {BB
}, ".dead");
1953 // The split block is live even if it contains only an unreachable
1954 // instruction at the end.
1955 assumeLive(A
, *SplitBB
);
1956 SplitPos
= SplitBB
->getTerminator();
1960 BB
= SplitPos
->getParent();
1961 SplitBlock(BB
, SplitPos
);
1962 changeToUnreachable(BB
->getTerminator(), /* UseLLVMTrap */ false);
1963 HasChanged
= ChangeStatus::CHANGED
;
1966 for (BasicBlock
&BB
: F
)
1967 if (!AssumedLiveBlocks
.count(&BB
))
1968 A
.deleteAfterManifest(BB
);
1973 /// See AbstractAttribute::updateImpl(...).
1974 ChangeStatus
updateImpl(Attributor
&A
) override
;
1976 /// See AAIsDead::isAssumedDead(BasicBlock *).
1977 bool isAssumedDead(const BasicBlock
*BB
) const override
{
1978 assert(BB
->getParent() == getAssociatedFunction() &&
1979 "BB must be in the same anchor scope function.");
1983 return !AssumedLiveBlocks
.count(BB
);
1986 /// See AAIsDead::isKnownDead(BasicBlock *).
1987 bool isKnownDead(const BasicBlock
*BB
) const override
{
1988 return getKnown() && isAssumedDead(BB
);
1991 /// See AAIsDead::isAssumed(Instruction *I).
1992 bool isAssumedDead(const Instruction
*I
) const override
{
1993 assert(I
->getParent()->getParent() == getAssociatedFunction() &&
1994 "Instruction must be in the same anchor scope function.");
1999 // If it is not in AssumedLiveBlocks then it for sure dead.
2000 // Otherwise, it can still be after noreturn call in a live block.
2001 if (!AssumedLiveBlocks
.count(I
->getParent()))
2004 // If it is not after a noreturn call, than it is live.
2005 return isAfterNoReturn(I
);
2008 /// See AAIsDead::isKnownDead(Instruction *I).
2009 bool isKnownDead(const Instruction
*I
) const override
{
2010 return getKnown() && isAssumedDead(I
);
2013 /// Check if instruction is after noreturn call, in other words, assumed dead.
2014 bool isAfterNoReturn(const Instruction
*I
) const;
2016 /// Determine if \p F might catch asynchronous exceptions.
2017 static bool mayCatchAsynchronousExceptions(const Function
&F
) {
2018 return F
.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F
);
2021 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
2022 /// that internal function called from \p BB should now be looked at.
2023 void assumeLive(Attributor
&A
, const BasicBlock
&BB
) {
2024 if (!AssumedLiveBlocks
.insert(&BB
).second
)
2027 // We assume that all of BB is (probably) live now and if there are calls to
2028 // internal functions we will assume that those are now live as well. This
2029 // is a performance optimization for blocks with calls to a lot of internal
2030 // functions. It can however cause dead functions to be treated as live.
2031 for (const Instruction
&I
: BB
)
2032 if (ImmutableCallSite ICS
= ImmutableCallSite(&I
))
2033 if (const Function
*F
= ICS
.getCalledFunction())
2034 if (F
->hasInternalLinkage())
2035 A
.markLiveInternalFunction(*F
);
2038 /// Collection of to be explored paths.
2039 SmallSetVector
<const Instruction
*, 8> ToBeExploredPaths
;
2041 /// Collection of all assumed live BasicBlocks.
2042 DenseSet
<const BasicBlock
*> AssumedLiveBlocks
;
2044 /// Collection of calls with noreturn attribute, assumed or knwon.
2045 SmallSetVector
<const Instruction
*, 4> NoReturnCalls
;
2048 struct AAIsDeadFunction final
: public AAIsDeadImpl
{
2049 AAIsDeadFunction(const IRPosition
&IRP
) : AAIsDeadImpl(IRP
) {}
2051 /// See AbstractAttribute::trackStatistics()
2052 void trackStatistics() const override
{
2053 STATS_DECL(PartiallyDeadBlocks
, Function
,
2054 "Number of basic blocks classified as partially dead");
2055 BUILD_STAT_NAME(PartiallyDeadBlocks
, Function
) += NoReturnCalls
.size();
2059 bool AAIsDeadImpl::isAfterNoReturn(const Instruction
*I
) const {
2060 const Instruction
*PrevI
= I
->getPrevNode();
2062 if (NoReturnCalls
.count(PrevI
))
2064 PrevI
= PrevI
->getPrevNode();
2069 const Instruction
*AAIsDeadImpl::findNextNoReturn(Attributor
&A
,
2070 const Instruction
*I
) {
2071 const BasicBlock
*BB
= I
->getParent();
2072 const Function
&F
= *BB
->getParent();
2074 // Flag to determine if we can change an invoke to a call assuming the callee
2075 // is nounwind. This is not possible if the personality of the function allows
2076 // to catch asynchronous exceptions.
2077 bool Invoke2CallAllowed
= !mayCatchAsynchronousExceptions(F
);
2079 // TODO: We should have a function that determines if an "edge" is dead.
2080 // Edges could be from an instruction to the next or from a terminator
2081 // to the successor. For now, we need to special case the unwind block
2082 // of InvokeInst below.
2085 ImmutableCallSite
ICS(I
);
2088 const IRPosition
&IPos
= IRPosition::callsite_function(ICS
);
2089 // Regarless of the no-return property of an invoke instruction we only
2090 // learn that the regular successor is not reachable through this
2091 // instruction but the unwind block might still be.
2092 if (auto *Invoke
= dyn_cast
<InvokeInst
>(I
)) {
2093 // Use nounwind to justify the unwind block is dead as well.
2094 const auto &AANoUnw
= A
.getAAFor
<AANoUnwind
>(*this, IPos
);
2095 if (!Invoke2CallAllowed
|| !AANoUnw
.isAssumedNoUnwind()) {
2096 assumeLive(A
, *Invoke
->getUnwindDest());
2097 ToBeExploredPaths
.insert(&Invoke
->getUnwindDest()->front());
2101 const auto &NoReturnAA
= A
.getAAFor
<AANoReturn
>(*this, IPos
);
2102 if (NoReturnAA
.isAssumedNoReturn())
2106 I
= I
->getNextNode();
2109 // get new paths (reachable blocks).
2110 for (const BasicBlock
*SuccBB
: successors(BB
)) {
2111 assumeLive(A
, *SuccBB
);
2112 ToBeExploredPaths
.insert(&SuccBB
->front());
2115 // No noreturn instruction found.
2119 ChangeStatus
AAIsDeadImpl::updateImpl(Attributor
&A
) {
2120 ChangeStatus Status
= ChangeStatus::UNCHANGED
;
2122 // Temporary collection to iterate over existing noreturn instructions. This
2123 // will alow easier modification of NoReturnCalls collection
2124 SmallVector
<const Instruction
*, 8> NoReturnChanged
;
2126 for (const Instruction
*I
: NoReturnCalls
)
2127 NoReturnChanged
.push_back(I
);
2129 for (const Instruction
*I
: NoReturnChanged
) {
2130 size_t Size
= ToBeExploredPaths
.size();
2132 const Instruction
*NextNoReturnI
= findNextNoReturn(A
, I
);
2133 if (NextNoReturnI
!= I
) {
2134 Status
= ChangeStatus::CHANGED
;
2135 NoReturnCalls
.remove(I
);
2137 NoReturnCalls
.insert(NextNoReturnI
);
2140 // Explore new paths.
2141 while (Size
!= ToBeExploredPaths
.size()) {
2142 Status
= ChangeStatus::CHANGED
;
2143 if (const Instruction
*NextNoReturnI
=
2144 findNextNoReturn(A
, ToBeExploredPaths
[Size
++]))
2145 NoReturnCalls
.insert(NextNoReturnI
);
2149 LLVM_DEBUG(dbgs() << "[AAIsDead] AssumedLiveBlocks: "
2150 << AssumedLiveBlocks
.size() << " Total number of blocks: "
2151 << getAssociatedFunction()->size() << "\n");
2153 // If we know everything is live there is no need to query for liveness.
2154 if (NoReturnCalls
.empty() &&
2155 getAssociatedFunction()->size() == AssumedLiveBlocks
.size()) {
2156 // Indicating a pessimistic fixpoint will cause the state to be "invalid"
2157 // which will cause the Attributor to not return the AAIsDead on request,
2158 // which will prevent us from querying isAssumedDead().
2159 indicatePessimisticFixpoint();
2160 assert(!isValidState() && "Expected an invalid state!");
2161 Status
= ChangeStatus::CHANGED
;
2167 /// Liveness information for a call sites.
2168 struct AAIsDeadCallSite final
: AAIsDeadImpl
{
2169 AAIsDeadCallSite(const IRPosition
&IRP
) : AAIsDeadImpl(IRP
) {}
2171 /// See AbstractAttribute::initialize(...).
2172 void initialize(Attributor
&A
) override
{
2173 // TODO: Once we have call site specific value information we can provide
2174 // call site specific liveness information and then it makes
2175 // sense to specialize attributes for call sites instead of
2176 // redirecting requests to the callee.
2177 llvm_unreachable("Abstract attributes for liveness are not "
2178 "supported for call sites yet!");
2181 /// See AbstractAttribute::updateImpl(...).
2182 ChangeStatus
updateImpl(Attributor
&A
) override
{
2183 return indicatePessimisticFixpoint();
2186 /// See AbstractAttribute::trackStatistics()
2187 void trackStatistics() const override
{}
2190 /// -------------------- Dereferenceable Argument Attribute --------------------
2193 ChangeStatus clampStateAndIndicateChange
<DerefState
>(DerefState
&S
,
2194 const DerefState
&R
) {
2195 ChangeStatus CS0
= clampStateAndIndicateChange
<IntegerState
>(
2196 S
.DerefBytesState
, R
.DerefBytesState
);
2198 clampStateAndIndicateChange
<IntegerState
>(S
.GlobalState
, R
.GlobalState
);
2202 struct AADereferenceableImpl
: AADereferenceable
{
2203 AADereferenceableImpl(const IRPosition
&IRP
) : AADereferenceable(IRP
) {}
2204 using StateType
= DerefState
;
2206 void initialize(Attributor
&A
) override
{
2207 SmallVector
<Attribute
, 4> Attrs
;
2208 getAttrs({Attribute::Dereferenceable
, Attribute::DereferenceableOrNull
},
2210 for (const Attribute
&Attr
: Attrs
)
2211 takeKnownDerefBytesMaximum(Attr
.getValueAsInt());
2213 NonNullAA
= &A
.getAAFor
<AANonNull
>(*this, getIRPosition());
2215 const IRPosition
&IRP
= this->getIRPosition();
2216 bool IsFnInterface
= IRP
.isFnInterfaceKind();
2217 const Function
*FnScope
= IRP
.getAnchorScope();
2218 if (IsFnInterface
&& (!FnScope
|| !FnScope
->hasExactDefinition()))
2219 indicatePessimisticFixpoint();
2222 /// See AbstractAttribute::getState()
2224 StateType
&getState() override
{ return *this; }
2225 const StateType
&getState() const override
{ return *this; }
2228 void getDeducedAttributes(LLVMContext
&Ctx
,
2229 SmallVectorImpl
<Attribute
> &Attrs
) const override
{
2230 // TODO: Add *_globally support
2231 if (isAssumedNonNull())
2232 Attrs
.emplace_back(Attribute::getWithDereferenceableBytes(
2233 Ctx
, getAssumedDereferenceableBytes()));
2235 Attrs
.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
2236 Ctx
, getAssumedDereferenceableBytes()));
2239 /// See AbstractAttribute::getAsStr().
2240 const std::string
getAsStr() const override
{
2241 if (!getAssumedDereferenceableBytes())
2242 return "unknown-dereferenceable";
2243 return std::string("dereferenceable") +
2244 (isAssumedNonNull() ? "" : "_or_null") +
2245 (isAssumedGlobal() ? "_globally" : "") + "<" +
2246 std::to_string(getKnownDereferenceableBytes()) + "-" +
2247 std::to_string(getAssumedDereferenceableBytes()) + ">";
2251 /// Dereferenceable attribute for a floating value.
2252 struct AADereferenceableFloating
: AADereferenceableImpl
{
2253 AADereferenceableFloating(const IRPosition
&IRP
)
2254 : AADereferenceableImpl(IRP
) {}
2256 /// See AbstractAttribute::updateImpl(...).
2257 ChangeStatus
updateImpl(Attributor
&A
) override
{
2258 const DataLayout
&DL
= A
.getDataLayout();
2260 auto VisitValueCB
= [&](Value
&V
, DerefState
&T
, bool Stripped
) -> bool {
2262 DL
.getIndexSizeInBits(V
.getType()->getPointerAddressSpace());
2263 APInt
Offset(IdxWidth
, 0);
2265 V
.stripAndAccumulateInBoundsConstantOffsets(DL
, Offset
);
2268 A
.getAAFor
<AADereferenceable
>(*this, IRPosition::value(*Base
));
2269 int64_t DerefBytes
= 0;
2270 if (!Stripped
&& this == &AA
) {
2271 // Use IR information if we did not strip anything.
2272 // TODO: track globally.
2274 DerefBytes
= Base
->getPointerDereferenceableBytes(DL
, CanBeNull
);
2275 T
.GlobalState
.indicatePessimisticFixpoint();
2277 const DerefState
&DS
= static_cast<const DerefState
&>(AA
.getState());
2278 DerefBytes
= DS
.DerefBytesState
.getAssumed();
2279 T
.GlobalState
&= DS
.GlobalState
;
2282 // For now we do not try to "increase" dereferenceability due to negative
2283 // indices as we first have to come up with code to deal with loops and
2284 // for overflows of the dereferenceable bytes.
2285 int64_t OffsetSExt
= Offset
.getSExtValue();
2289 T
.takeAssumedDerefBytesMinimum(
2290 std::max(int64_t(0), DerefBytes
- OffsetSExt
));
2294 // If nothing was stripped IR information is all we got.
2295 T
.takeKnownDerefBytesMaximum(
2296 std::max(int64_t(0), DerefBytes
- OffsetSExt
));
2297 T
.indicatePessimisticFixpoint();
2298 } else if (OffsetSExt
> 0) {
2299 // If something was stripped but there is circular reasoning we look
2300 // for the offset. If it is positive we basically decrease the
2301 // dereferenceable bytes in a circluar loop now, which will simply
2302 // drive them down to the known value in a very slow way which we
2304 T
.indicatePessimisticFixpoint();
2308 return T
.isValidState();
2312 if (!genericValueTraversal
<AADereferenceable
, DerefState
>(
2313 A
, getIRPosition(), *this, T
, VisitValueCB
))
2314 return indicatePessimisticFixpoint();
2316 return clampStateAndIndicateChange(getState(), T
);
2319 /// See AbstractAttribute::trackStatistics()
2320 void trackStatistics() const override
{
2321 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable
)
2325 /// Dereferenceable attribute for a return value.
2326 struct AADereferenceableReturned final
2327 : AAReturnedFromReturnedValues
<AADereferenceable
, AADereferenceableImpl
,
2329 AADereferenceableReturned(const IRPosition
&IRP
)
2330 : AAReturnedFromReturnedValues
<AADereferenceable
, AADereferenceableImpl
,
2333 /// See AbstractAttribute::trackStatistics()
2334 void trackStatistics() const override
{
2335 STATS_DECLTRACK_FNRET_ATTR(dereferenceable
)
2339 /// Dereferenceable attribute for an argument
2340 struct AADereferenceableArgument final
2341 : AAArgumentFromCallSiteArguments
<AADereferenceable
, AADereferenceableImpl
,
2343 AADereferenceableArgument(const IRPosition
&IRP
)
2344 : AAArgumentFromCallSiteArguments
<AADereferenceable
,
2345 AADereferenceableImpl
, DerefState
>(
2348 /// See AbstractAttribute::trackStatistics()
2349 void trackStatistics() const override
{
2350 STATS_DECLTRACK_ARG_ATTR(dereferenceable
)
2354 /// Dereferenceable attribute for a call site argument.
2355 struct AADereferenceableCallSiteArgument final
: AADereferenceableFloating
{
2356 AADereferenceableCallSiteArgument(const IRPosition
&IRP
)
2357 : AADereferenceableFloating(IRP
) {}
2359 /// See AbstractAttribute::trackStatistics()
2360 void trackStatistics() const override
{
2361 STATS_DECLTRACK_CSARG_ATTR(dereferenceable
)
2365 /// Dereferenceable attribute deduction for a call site return value.
2366 struct AADereferenceableCallSiteReturned final
: AADereferenceableImpl
{
2367 AADereferenceableCallSiteReturned(const IRPosition
&IRP
)
2368 : AADereferenceableImpl(IRP
) {}
2370 /// See AbstractAttribute::initialize(...).
2371 void initialize(Attributor
&A
) override
{
2372 AADereferenceableImpl::initialize(A
);
2373 Function
*F
= getAssociatedFunction();
2375 indicatePessimisticFixpoint();
2378 /// See AbstractAttribute::updateImpl(...).
2379 ChangeStatus
updateImpl(Attributor
&A
) override
{
2380 // TODO: Once we have call site specific value information we can provide
2381 // call site specific liveness information and then it makes
2382 // sense to specialize attributes for call sites arguments instead of
2383 // redirecting requests to the callee argument.
2384 Function
*F
= getAssociatedFunction();
2385 const IRPosition
&FnPos
= IRPosition::returned(*F
);
2386 auto &FnAA
= A
.getAAFor
<AADereferenceable
>(*this, FnPos
);
2387 return clampStateAndIndicateChange(
2388 getState(), static_cast<const DerefState
&>(FnAA
.getState()));
2391 /// See AbstractAttribute::trackStatistics()
2392 void trackStatistics() const override
{
2393 STATS_DECLTRACK_CS_ATTR(dereferenceable
);
2397 // ------------------------ Align Argument Attribute ------------------------
2399 struct AAAlignImpl
: AAAlign
{
2400 AAAlignImpl(const IRPosition
&IRP
) : AAAlign(IRP
) {}
2402 // Max alignemnt value allowed in IR
2403 static const unsigned MAX_ALIGN
= 1U << 29;
2405 /// See AbstractAttribute::initialize(...).
2406 void initialize(Attributor
&A
) override
{
2407 takeAssumedMinimum(MAX_ALIGN
);
2409 SmallVector
<Attribute
, 4> Attrs
;
2410 getAttrs({Attribute::Alignment
}, Attrs
);
2411 for (const Attribute
&Attr
: Attrs
)
2412 takeKnownMaximum(Attr
.getValueAsInt());
2414 if (getIRPosition().isFnInterfaceKind() &&
2415 (!getAssociatedFunction() ||
2416 !getAssociatedFunction()->hasExactDefinition()))
2417 indicatePessimisticFixpoint();
2420 /// See AbstractAttribute::manifest(...).
2421 ChangeStatus
manifest(Attributor
&A
) override
{
2422 ChangeStatus Changed
= ChangeStatus::UNCHANGED
;
2424 // Check for users that allow alignment annotations.
2425 Value
&AnchorVal
= getIRPosition().getAnchorValue();
2426 for (const Use
&U
: AnchorVal
.uses()) {
2427 if (auto *SI
= dyn_cast
<StoreInst
>(U
.getUser())) {
2428 if (SI
->getPointerOperand() == &AnchorVal
)
2429 if (SI
->getAlignment() < getAssumedAlign()) {
2430 STATS_DECLTRACK(AAAlign
, Store
,
2431 "Number of times alignemnt added to a store");
2432 SI
->setAlignment(getAssumedAlign());
2433 Changed
= ChangeStatus::CHANGED
;
2435 } else if (auto *LI
= dyn_cast
<LoadInst
>(U
.getUser())) {
2436 if (LI
->getPointerOperand() == &AnchorVal
)
2437 if (LI
->getAlignment() < getAssumedAlign()) {
2438 LI
->setAlignment(getAssumedAlign());
2439 STATS_DECLTRACK(AAAlign
, Load
,
2440 "Number of times alignemnt added to a load");
2441 Changed
= ChangeStatus::CHANGED
;
2446 return AAAlign::manifest(A
) | Changed
;
2449 // TODO: Provide a helper to determine the implied ABI alignment and check in
2450 // the existing manifest method and a new one for AAAlignImpl that value
2451 // to avoid making the alignment explicit if it did not improve.
2453 /// See AbstractAttribute::getDeducedAttributes
2455 getDeducedAttributes(LLVMContext
&Ctx
,
2456 SmallVectorImpl
<Attribute
> &Attrs
) const override
{
2457 if (getAssumedAlign() > 1)
2458 Attrs
.emplace_back(Attribute::getWithAlignment(Ctx
, getAssumedAlign()));
2461 /// See AbstractAttribute::getAsStr().
2462 const std::string
getAsStr() const override
{
2463 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
2464 "-" + std::to_string(getAssumedAlign()) + ">")
2469 /// Align attribute for a floating value.
2470 struct AAAlignFloating
: AAAlignImpl
{
2471 AAAlignFloating(const IRPosition
&IRP
) : AAAlignImpl(IRP
) {}
2473 /// See AbstractAttribute::updateImpl(...).
2474 ChangeStatus
updateImpl(Attributor
&A
) override
{
2475 const DataLayout
&DL
= A
.getDataLayout();
2477 auto VisitValueCB
= [&](Value
&V
, AAAlign::StateType
&T
,
2478 bool Stripped
) -> bool {
2479 const auto &AA
= A
.getAAFor
<AAAlign
>(*this, IRPosition::value(V
));
2480 if (!Stripped
&& this == &AA
) {
2481 // Use only IR information if we did not strip anything.
2482 T
.takeKnownMaximum(V
.getPointerAlignment(DL
));
2483 T
.indicatePessimisticFixpoint();
2485 // Use abstract attribute information.
2486 const AAAlign::StateType
&DS
=
2487 static_cast<const AAAlign::StateType
&>(AA
.getState());
2490 return T
.isValidState();
2494 if (!genericValueTraversal
<AAAlign
, StateType
>(A
, getIRPosition(), *this, T
,
2496 return indicatePessimisticFixpoint();
2498 // TODO: If we know we visited all incoming values, thus no are assumed
2499 // dead, we can take the known information from the state T.
2500 return clampStateAndIndicateChange(getState(), T
);
2503 /// See AbstractAttribute::trackStatistics()
2504 void trackStatistics() const override
{ STATS_DECLTRACK_FLOATING_ATTR(align
) }
2507 /// Align attribute for function return value.
2508 struct AAAlignReturned final
2509 : AAReturnedFromReturnedValues
<AAAlign
, AAAlignImpl
> {
2510 AAAlignReturned(const IRPosition
&IRP
)
2511 : AAReturnedFromReturnedValues
<AAAlign
, AAAlignImpl
>(IRP
) {}
2513 /// See AbstractAttribute::trackStatistics()
2514 void trackStatistics() const override
{ STATS_DECLTRACK_FNRET_ATTR(aligned
) }
2517 /// Align attribute for function argument.
2518 struct AAAlignArgument final
2519 : AAArgumentFromCallSiteArguments
<AAAlign
, AAAlignImpl
> {
2520 AAAlignArgument(const IRPosition
&IRP
)
2521 : AAArgumentFromCallSiteArguments
<AAAlign
, AAAlignImpl
>(IRP
) {}
2523 /// See AbstractAttribute::trackStatistics()
2524 void trackStatistics() const override
{ STATS_DECLTRACK_ARG_ATTR(aligned
) }
2527 struct AAAlignCallSiteArgument final
: AAAlignFloating
{
2528 AAAlignCallSiteArgument(const IRPosition
&IRP
) : AAAlignFloating(IRP
) {}
2530 /// See AbstractAttribute::manifest(...).
2531 ChangeStatus
manifest(Attributor
&A
) override
{
2532 return AAAlignImpl::manifest(A
);
2535 /// See AbstractAttribute::trackStatistics()
2536 void trackStatistics() const override
{ STATS_DECLTRACK_CSARG_ATTR(aligned
) }
2539 /// Align attribute deduction for a call site return value.
2540 struct AAAlignCallSiteReturned final
: AAAlignImpl
{
2541 AAAlignCallSiteReturned(const IRPosition
&IRP
) : AAAlignImpl(IRP
) {}
2543 /// See AbstractAttribute::initialize(...).
2544 void initialize(Attributor
&A
) override
{
2545 AAAlignImpl::initialize(A
);
2546 Function
*F
= getAssociatedFunction();
2548 indicatePessimisticFixpoint();
2551 /// See AbstractAttribute::updateImpl(...).
2552 ChangeStatus
updateImpl(Attributor
&A
) override
{
2553 // TODO: Once we have call site specific value information we can provide
2554 // call site specific liveness information and then it makes
2555 // sense to specialize attributes for call sites arguments instead of
2556 // redirecting requests to the callee argument.
2557 Function
*F
= getAssociatedFunction();
2558 const IRPosition
&FnPos
= IRPosition::returned(*F
);
2559 auto &FnAA
= A
.getAAFor
<AAAlign
>(*this, FnPos
);
2560 return clampStateAndIndicateChange(
2561 getState(), static_cast<const AAAlign::StateType
&>(FnAA
.getState()));
2564 /// See AbstractAttribute::trackStatistics()
2565 void trackStatistics() const override
{ STATS_DECLTRACK_CS_ATTR(align
); }
2568 /// ------------------ Function No-Return Attribute ----------------------------
2569 struct AANoReturnImpl
: public AANoReturn
{
2570 AANoReturnImpl(const IRPosition
&IRP
) : AANoReturn(IRP
) {}
2572 /// See AbstractAttribute::getAsStr().
2573 const std::string
getAsStr() const override
{
2574 return getAssumed() ? "noreturn" : "may-return";
2577 /// See AbstractAttribute::updateImpl(Attributor &A).
2578 virtual ChangeStatus
updateImpl(Attributor
&A
) override
{
2579 auto CheckForNoReturn
= [](Instruction
&) { return false; };
2580 if (!A
.checkForAllInstructions(CheckForNoReturn
, *this,
2581 {(unsigned)Instruction::Ret
}))
2582 return indicatePessimisticFixpoint();
2583 return ChangeStatus::UNCHANGED
;
2587 struct AANoReturnFunction final
: AANoReturnImpl
{
2588 AANoReturnFunction(const IRPosition
&IRP
) : AANoReturnImpl(IRP
) {}
2590 /// See AbstractAttribute::trackStatistics()
2591 void trackStatistics() const override
{ STATS_DECLTRACK_FN_ATTR(noreturn
) }
2594 /// NoReturn attribute deduction for a call sites.
2595 struct AANoReturnCallSite final
: AANoReturnImpl
{
2596 AANoReturnCallSite(const IRPosition
&IRP
) : AANoReturnImpl(IRP
) {}
2598 /// See AbstractAttribute::initialize(...).
2599 void initialize(Attributor
&A
) override
{
2600 AANoReturnImpl::initialize(A
);
2601 Function
*F
= getAssociatedFunction();
2603 indicatePessimisticFixpoint();
2606 /// See AbstractAttribute::updateImpl(...).
2607 ChangeStatus
updateImpl(Attributor
&A
) override
{
2608 // TODO: Once we have call site specific value information we can provide
2609 // call site specific liveness information and then it makes
2610 // sense to specialize attributes for call sites arguments instead of
2611 // redirecting requests to the callee argument.
2612 Function
*F
= getAssociatedFunction();
2613 const IRPosition
&FnPos
= IRPosition::function(*F
);
2614 auto &FnAA
= A
.getAAFor
<AANoReturn
>(*this, FnPos
);
2615 return clampStateAndIndicateChange(
2617 static_cast<const AANoReturn::StateType
&>(FnAA
.getState()));
2620 /// See AbstractAttribute::trackStatistics()
2621 void trackStatistics() const override
{ STATS_DECLTRACK_CS_ATTR(noreturn
); }
2624 /// ----------------------- Variable Capturing ---------------------------------
2626 /// A class to hold the state of for no-capture attributes.
2627 struct AANoCaptureImpl
: public AANoCapture
{
2628 AANoCaptureImpl(const IRPosition
&IRP
) : AANoCapture(IRP
) {}
2630 /// See AbstractAttribute::initialize(...).
2631 void initialize(Attributor
&A
) override
{
2632 AANoCapture::initialize(A
);
2634 const IRPosition
&IRP
= getIRPosition();
2636 getArgNo() >= 0 ? IRP
.getAssociatedFunction() : IRP
.getAnchorScope();
2638 // Check what state the associated function can actually capture.
2640 determineFunctionCaptureCapabilities(*F
, *this);
2642 indicatePessimisticFixpoint();
2645 /// See AbstractAttribute::updateImpl(...).
2646 ChangeStatus
updateImpl(Attributor
&A
) override
;
2648 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
2650 getDeducedAttributes(LLVMContext
&Ctx
,
2651 SmallVectorImpl
<Attribute
> &Attrs
) const override
{
2652 if (!isAssumedNoCaptureMaybeReturned())
2655 if (getArgNo() >= 0) {
2656 if (isAssumedNoCapture())
2657 Attrs
.emplace_back(Attribute::get(Ctx
, Attribute::NoCapture
));
2658 else if (ManifestInternal
)
2659 Attrs
.emplace_back(Attribute::get(Ctx
, "no-capture-maybe-returned"));
2663 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
2664 /// depending on the ability of the function associated with \p IRP to capture
2665 /// state in memory and through "returning/throwing", respectively.
2666 static void determineFunctionCaptureCapabilities(const Function
&F
,
2667 IntegerState
&State
) {
2668 // TODO: Once we have memory behavior attributes we should use them here.
2670 // If we know we cannot communicate or write to memory, we do not care about
2672 if (F
.onlyReadsMemory() && F
.doesNotThrow() &&
2673 F
.getReturnType()->isVoidTy()) {
2674 State
.addKnownBits(NO_CAPTURE
);
2678 // A function cannot capture state in memory if it only reads memory, it can
2679 // however return/throw state and the state might be influenced by the
2680 // pointer value, e.g., loading from a returned pointer might reveal a bit.
2681 if (F
.onlyReadsMemory())
2682 State
.addKnownBits(NOT_CAPTURED_IN_MEM
);
2684 // A function cannot communicate state back if it does not through
2685 // exceptions and doesn not return values.
2686 if (F
.doesNotThrow() && F
.getReturnType()->isVoidTy())
2687 State
.addKnownBits(NOT_CAPTURED_IN_RET
);
2690 /// See AbstractState::getAsStr().
2691 const std::string
getAsStr() const override
{
2692 if (isKnownNoCapture())
2693 return "known not-captured";
2694 if (isAssumedNoCapture())
2695 return "assumed not-captured";
2696 if (isKnownNoCaptureMaybeReturned())
2697 return "known not-captured-maybe-returned";
2698 if (isAssumedNoCaptureMaybeReturned())
2699 return "assumed not-captured-maybe-returned";
2700 return "assumed-captured";
2704 /// Attributor-aware capture tracker.
2705 struct AACaptureUseTracker final
: public CaptureTracker
{
2707 /// Create a capture tracker that can lookup in-flight abstract attributes
2708 /// through the Attributor \p A.
2710 /// If a use leads to a potential capture, \p CapturedInMemory is set and the
2711 /// search is stopped. If a use leads to a return instruction,
2712 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
2713 /// If a use leads to a ptr2int which may capture the value,
2714 /// \p CapturedInInteger is set. If a use is found that is currently assumed
2715 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
2716 /// set. All values in \p PotentialCopies are later tracked as well. For every
2717 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
2718 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
2719 /// conservatively set to true.
2720 AACaptureUseTracker(Attributor
&A
, AANoCapture
&NoCaptureAA
,
2721 const AAIsDead
&IsDeadAA
, IntegerState
&State
,
2722 SmallVectorImpl
<const Value
*> &PotentialCopies
,
2723 unsigned &RemainingUsesToExplore
)
2724 : A(A
), NoCaptureAA(NoCaptureAA
), IsDeadAA(IsDeadAA
), State(State
),
2725 PotentialCopies(PotentialCopies
),
2726 RemainingUsesToExplore(RemainingUsesToExplore
) {}
2728 /// Determine if \p V maybe captured. *Also updates the state!*
2729 bool valueMayBeCaptured(const Value
*V
) {
2730 if (V
->getType()->isPointerTy()) {
2731 PointerMayBeCaptured(V
, this);
2733 State
.indicatePessimisticFixpoint();
2735 return State
.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED
);
2738 /// See CaptureTracker::tooManyUses().
2739 void tooManyUses() override
{
2740 State
.removeAssumedBits(AANoCapture::NO_CAPTURE
);
2743 bool isDereferenceableOrNull(Value
*O
, const DataLayout
&DL
) override
{
2744 if (CaptureTracker::isDereferenceableOrNull(O
, DL
))
2746 const auto &DerefAA
=
2747 A
.getAAFor
<AADereferenceable
>(NoCaptureAA
, IRPosition::value(*O
));
2748 return DerefAA
.getAssumedDereferenceableBytes();
2751 /// See CaptureTracker::captured(...).
2752 bool captured(const Use
*U
) override
{
2753 Instruction
*UInst
= cast
<Instruction
>(U
->getUser());
2754 LLVM_DEBUG(dbgs() << "Check use: " << *U
->get() << " in " << *UInst
2757 // Because we may reuse the tracker multiple times we keep track of the
2758 // number of explored uses ourselves as well.
2759 if (RemainingUsesToExplore
-- == 0) {
2760 LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
2761 return isCapturedIn(/* Memory */ true, /* Integer */ true,
2765 // Deal with ptr2int by following uses.
2766 if (isa
<PtrToIntInst
>(UInst
)) {
2767 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
2768 return valueMayBeCaptured(UInst
);
2771 // Explicitly catch return instructions.
2772 if (isa
<ReturnInst
>(UInst
))
2773 return isCapturedIn(/* Memory */ false, /* Integer */ false,
2776 // For now we only use special logic for call sites. However, the tracker
2777 // itself knows about a lot of other non-capturing cases already.
2779 if (!CS
|| !CS
.isArgOperand(U
))
2780 return isCapturedIn(/* Memory */ true, /* Integer */ true,
2783 unsigned ArgNo
= CS
.getArgumentNo(U
);
2784 const IRPosition
&CSArgPos
= IRPosition::callsite_argument(CS
, ArgNo
);
2785 // If we have a abstract no-capture attribute for the argument we can use
2786 // it to justify a non-capture attribute here. This allows recursion!
2787 auto &ArgNoCaptureAA
= A
.getAAFor
<AANoCapture
>(NoCaptureAA
, CSArgPos
);
2788 if (ArgNoCaptureAA
.isAssumedNoCapture())
2789 return isCapturedIn(/* Memory */ false, /* Integer */ false,
2790 /* Return */ false);
2791 if (ArgNoCaptureAA
.isAssumedNoCaptureMaybeReturned()) {
2792 addPotentialCopy(CS
);
2793 return isCapturedIn(/* Memory */ false, /* Integer */ false,
2794 /* Return */ false);
2797 // Lastly, we could not find a reason no-capture can be assumed so we don't.
2798 return isCapturedIn(/* Memory */ true, /* Integer */ true,
2802 /// Register \p CS as potential copy of the value we are checking.
2803 void addPotentialCopy(CallSite CS
) {
2804 PotentialCopies
.push_back(CS
.getInstruction());
2807 /// See CaptureTracker::shouldExplore(...).
2808 bool shouldExplore(const Use
*U
) override
{
2810 return !IsDeadAA
.isAssumedDead(cast
<Instruction
>(U
->getUser()));
2813 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
2814 /// \p CapturedInRet, then return the appropriate value for use in the
2815 /// CaptureTracker::captured() interface.
2816 bool isCapturedIn(bool CapturedInMem
, bool CapturedInInt
,
2817 bool CapturedInRet
) {
2818 LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem
<< "|Int "
2819 << CapturedInInt
<< "|Ret " << CapturedInRet
<< "]\n");
2821 State
.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM
);
2823 State
.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT
);
2825 State
.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET
);
2826 return !State
.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED
);
2830 /// The attributor providing in-flight abstract attributes.
2833 /// The abstract attribute currently updated.
2834 AANoCapture
&NoCaptureAA
;
2836 /// The abstract liveness state.
2837 const AAIsDead
&IsDeadAA
;
2839 /// The state currently updated.
2840 IntegerState
&State
;
2842 /// Set of potential copies of the tracked value.
2843 SmallVectorImpl
<const Value
*> &PotentialCopies
;
2845 /// Global counter to limit the number of explored uses.
2846 unsigned &RemainingUsesToExplore
;
2849 ChangeStatus
AANoCaptureImpl::updateImpl(Attributor
&A
) {
2850 const IRPosition
&IRP
= getIRPosition();
2852 getArgNo() >= 0 ? IRP
.getAssociatedArgument() : &IRP
.getAssociatedValue();
2854 return indicatePessimisticFixpoint();
2857 getArgNo() >= 0 ? IRP
.getAssociatedFunction() : IRP
.getAnchorScope();
2858 assert(F
&& "Expected a function!");
2859 const auto &IsDeadAA
= A
.getAAFor
<AAIsDead
>(*this, IRPosition::function(*F
));
2861 AANoCapture::StateType T
;
2862 // TODO: Once we have memory behavior attributes we should use them here
2863 // similar to the reasoning in
2864 // AANoCaptureImpl::determineFunctionCaptureCapabilities(...).
2866 // TODO: Use the AAReturnedValues to learn if the argument can return or
2869 // Use the CaptureTracker interface and logic with the specialized tracker,
2870 // defined in AACaptureUseTracker, that can look at in-flight abstract
2871 // attributes and directly updates the assumed state.
2872 SmallVector
<const Value
*, 4> PotentialCopies
;
2873 unsigned RemainingUsesToExplore
= DefaultMaxUsesToExplore
;
2874 AACaptureUseTracker
Tracker(A
, *this, IsDeadAA
, T
, PotentialCopies
,
2875 RemainingUsesToExplore
);
2877 // Check all potential copies of the associated value until we can assume
2878 // none will be captured or we have to assume at least one might be.
2880 PotentialCopies
.push_back(V
);
2881 while (T
.isAssumed(NO_CAPTURE_MAYBE_RETURNED
) && Idx
< PotentialCopies
.size())
2882 Tracker
.valueMayBeCaptured(PotentialCopies
[Idx
++]);
2884 AAAlign::StateType
&S
= getState();
2885 auto Assumed
= S
.getAssumed();
2886 S
.intersectAssumedBits(T
.getAssumed());
2887 return Assumed
== S
.getAssumed() ? ChangeStatus::UNCHANGED
2888 : ChangeStatus::CHANGED
;
2891 /// NoCapture attribute for function arguments.
2892 struct AANoCaptureArgument final
: AANoCaptureImpl
{
2893 AANoCaptureArgument(const IRPosition
&IRP
) : AANoCaptureImpl(IRP
) {}
2895 /// See AbstractAttribute::trackStatistics()
2896 void trackStatistics() const override
{ STATS_DECLTRACK_ARG_ATTR(nocapture
) }
2899 /// NoCapture attribute for call site arguments.
2900 struct AANoCaptureCallSiteArgument final
: AANoCaptureImpl
{
2901 AANoCaptureCallSiteArgument(const IRPosition
&IRP
) : AANoCaptureImpl(IRP
) {}
2903 /// See AbstractAttribute::updateImpl(...).
2904 ChangeStatus
updateImpl(Attributor
&A
) override
{
2905 // TODO: Once we have call site specific value information we can provide
2906 // call site specific liveness information and then it makes
2907 // sense to specialize attributes for call sites arguments instead of
2908 // redirecting requests to the callee argument.
2909 Argument
*Arg
= getAssociatedArgument();
2911 return indicatePessimisticFixpoint();
2912 const IRPosition
&ArgPos
= IRPosition::argument(*Arg
);
2913 auto &ArgAA
= A
.getAAFor
<AANoCapture
>(*this, ArgPos
);
2914 return clampStateAndIndicateChange(
2916 static_cast<const AANoCapture::StateType
&>(ArgAA
.getState()));
2919 /// See AbstractAttribute::trackStatistics()
2920 void trackStatistics() const override
{STATS_DECLTRACK_CSARG_ATTR(nocapture
)};
2923 /// NoCapture attribute for floating values.
2924 struct AANoCaptureFloating final
: AANoCaptureImpl
{
2925 AANoCaptureFloating(const IRPosition
&IRP
) : AANoCaptureImpl(IRP
) {}
2927 /// See AbstractAttribute::trackStatistics()
2928 void trackStatistics() const override
{
2929 STATS_DECLTRACK_FLOATING_ATTR(nocapture
)
2933 /// NoCapture attribute for function return value.
2934 struct AANoCaptureReturned final
: AANoCaptureImpl
{
2935 AANoCaptureReturned(const IRPosition
&IRP
) : AANoCaptureImpl(IRP
) {
2936 llvm_unreachable("NoCapture is not applicable to function returns!");
2939 /// See AbstractAttribute::initialize(...).
2940 void initialize(Attributor
&A
) override
{
2941 llvm_unreachable("NoCapture is not applicable to function returns!");
2944 /// See AbstractAttribute::updateImpl(...).
2945 ChangeStatus
updateImpl(Attributor
&A
) override
{
2946 llvm_unreachable("NoCapture is not applicable to function returns!");
2949 /// See AbstractAttribute::trackStatistics()
2950 void trackStatistics() const override
{}
2953 /// NoCapture attribute deduction for a call site return value.
2954 struct AANoCaptureCallSiteReturned final
: AANoCaptureImpl
{
2955 AANoCaptureCallSiteReturned(const IRPosition
&IRP
) : AANoCaptureImpl(IRP
) {}
2957 /// See AbstractAttribute::trackStatistics()
2958 void trackStatistics() const override
{
2959 STATS_DECLTRACK_CSRET_ATTR(nocapture
)
2963 /// ------------------ Value Simplify Attribute ----------------------------
2964 struct AAValueSimplifyImpl
: AAValueSimplify
{
2965 AAValueSimplifyImpl(const IRPosition
&IRP
) : AAValueSimplify(IRP
) {}
2967 /// See AbstractAttribute::getAsStr().
2968 const std::string
getAsStr() const override
{
2969 return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple")
2973 /// See AbstractAttribute::trackStatistics()
2974 void trackStatistics() const override
{}
2976 /// See AAValueSimplify::getAssumedSimplifiedValue()
2977 Optional
<Value
*> getAssumedSimplifiedValue(Attributor
&A
) const override
{
2979 return const_cast<Value
*>(&getAssociatedValue());
2980 return SimplifiedAssociatedValue
;
2982 void initialize(Attributor
&A
) override
{}
2984 /// Helper function for querying AAValueSimplify and updating candicate.
2985 /// \param QueryingValue Value trying to unify with SimplifiedValue
2986 /// \param AccumulatedSimplifiedValue Current simplification result.
2987 static bool checkAndUpdate(Attributor
&A
, const AbstractAttribute
&QueryingAA
,
2988 Value
&QueryingValue
,
2989 Optional
<Value
*> &AccumulatedSimplifiedValue
) {
2990 // FIXME: Add a typecast support.
2992 auto &ValueSimpifyAA
= A
.getAAFor
<AAValueSimplify
>(
2993 QueryingAA
, IRPosition::value(QueryingValue
));
2995 Optional
<Value
*> QueryingValueSimplified
=
2996 ValueSimpifyAA
.getAssumedSimplifiedValue(A
);
2998 if (!QueryingValueSimplified
.hasValue())
3001 if (!QueryingValueSimplified
.getValue())
3004 Value
&QueryingValueSimplifiedUnwrapped
=
3005 *QueryingValueSimplified
.getValue();
3007 if (isa
<UndefValue
>(QueryingValueSimplifiedUnwrapped
))
3010 if (AccumulatedSimplifiedValue
.hasValue())
3011 return AccumulatedSimplifiedValue
== QueryingValueSimplified
;
3013 LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << QueryingValue
3014 << " is assumed to be "
3015 << QueryingValueSimplifiedUnwrapped
<< "\n");
3017 AccumulatedSimplifiedValue
= QueryingValueSimplified
;
3021 /// See AbstractAttribute::manifest(...).
3022 ChangeStatus
manifest(Attributor
&A
) override
{
3023 ChangeStatus Changed
= ChangeStatus::UNCHANGED
;
3025 if (!SimplifiedAssociatedValue
.hasValue() ||
3026 !SimplifiedAssociatedValue
.getValue())
3029 if (auto *C
= dyn_cast
<Constant
>(SimplifiedAssociatedValue
.getValue())) {
3030 // We can replace the AssociatedValue with the constant.
3031 Value
&V
= getAssociatedValue();
3032 if (!V
.user_empty() && &V
!= C
&& V
.getType() == C
->getType()) {
3033 LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << V
<< " -> " << *C
3035 V
.replaceAllUsesWith(C
);
3036 Changed
= ChangeStatus::CHANGED
;
3040 return Changed
| AAValueSimplify::manifest(A
);
3044 // An assumed simplified value. Initially, it is set to Optional::None, which
3045 // means that the value is not clear under current assumption. If in the
3046 // pessimistic state, getAssumedSimplifiedValue doesn't return this value but
3047 // returns orignal associated value.
3048 Optional
<Value
*> SimplifiedAssociatedValue
;
3051 struct AAValueSimplifyArgument final
: AAValueSimplifyImpl
{
3052 AAValueSimplifyArgument(const IRPosition
&IRP
) : AAValueSimplifyImpl(IRP
) {}
3054 /// See AbstractAttribute::updateImpl(...).
3055 ChangeStatus
updateImpl(Attributor
&A
) override
{
3056 bool HasValueBefore
= SimplifiedAssociatedValue
.hasValue();
3058 auto PredForCallSite
= [&](CallSite CS
) {
3059 return checkAndUpdate(A
, *this, *CS
.getArgOperand(getArgNo()),
3060 SimplifiedAssociatedValue
);
3063 if (!A
.checkForAllCallSites(PredForCallSite
, *this, true))
3064 return indicatePessimisticFixpoint();
3066 // If a candicate was found in this update, return CHANGED.
3067 return HasValueBefore
== SimplifiedAssociatedValue
.hasValue()
3068 ? ChangeStatus::UNCHANGED
3069 : ChangeStatus ::CHANGED
;
3072 /// See AbstractAttribute::trackStatistics()
3073 void trackStatistics() const override
{
3074 STATS_DECLTRACK_ARG_ATTR(value_simplify
)
3078 struct AAValueSimplifyReturned
: AAValueSimplifyImpl
{
3079 AAValueSimplifyReturned(const IRPosition
&IRP
) : AAValueSimplifyImpl(IRP
) {}
3081 /// See AbstractAttribute::updateImpl(...).
3082 ChangeStatus
updateImpl(Attributor
&A
) override
{
3083 bool HasValueBefore
= SimplifiedAssociatedValue
.hasValue();
3085 auto PredForReturned
= [&](Value
&V
) {
3086 return checkAndUpdate(A
, *this, V
, SimplifiedAssociatedValue
);
3089 if (!A
.checkForAllReturnedValues(PredForReturned
, *this))
3090 return indicatePessimisticFixpoint();
3092 // If a candicate was found in this update, return CHANGED.
3093 return HasValueBefore
== SimplifiedAssociatedValue
.hasValue()
3094 ? ChangeStatus::UNCHANGED
3095 : ChangeStatus ::CHANGED
;
3097 /// See AbstractAttribute::trackStatistics()
3098 void trackStatistics() const override
{
3099 STATS_DECLTRACK_FNRET_ATTR(value_simplify
)
3103 struct AAValueSimplifyFloating
: AAValueSimplifyImpl
{
3104 AAValueSimplifyFloating(const IRPosition
&IRP
) : AAValueSimplifyImpl(IRP
) {}
3106 /// See AbstractAttribute::initialize(...).
3107 void initialize(Attributor
&A
) override
{
3108 Value
&V
= getAnchorValue();
3110 // TODO: add other stuffs
3111 if (isa
<Constant
>(V
) || isa
<UndefValue
>(V
))
3112 indicatePessimisticFixpoint();
3115 /// See AbstractAttribute::updateImpl(...).
3116 ChangeStatus
updateImpl(Attributor
&A
) override
{
3117 bool HasValueBefore
= SimplifiedAssociatedValue
.hasValue();
3119 auto VisitValueCB
= [&](Value
&V
, BooleanState
, bool Stripped
) -> bool {
3120 auto &AA
= A
.getAAFor
<AAValueSimplify
>(*this, IRPosition::value(V
));
3121 if (!Stripped
&& this == &AA
) {
3122 // TODO: Look the instruction and check recursively.
3124 dbgs() << "[Attributor][ValueSimplify] Can't be stripped more : "
3126 indicatePessimisticFixpoint();
3129 return checkAndUpdate(A
, *this, V
, SimplifiedAssociatedValue
);
3132 if (!genericValueTraversal
<AAValueSimplify
, BooleanState
>(
3133 A
, getIRPosition(), *this, static_cast<BooleanState
&>(*this),
3135 return indicatePessimisticFixpoint();
3137 // If a candicate was found in this update, return CHANGED.
3139 return HasValueBefore
== SimplifiedAssociatedValue
.hasValue()
3140 ? ChangeStatus::UNCHANGED
3141 : ChangeStatus ::CHANGED
;
3144 /// See AbstractAttribute::trackStatistics()
3145 void trackStatistics() const override
{
3146 STATS_DECLTRACK_FLOATING_ATTR(value_simplify
)
3150 struct AAValueSimplifyFunction
: AAValueSimplifyImpl
{
3151 AAValueSimplifyFunction(const IRPosition
&IRP
) : AAValueSimplifyImpl(IRP
) {}
3153 /// See AbstractAttribute::initialize(...).
3154 void initialize(Attributor
&A
) override
{
3155 SimplifiedAssociatedValue
= &getAnchorValue();
3156 indicateOptimisticFixpoint();
3158 /// See AbstractAttribute::initialize(...).
3159 ChangeStatus
updateImpl(Attributor
&A
) override
{
3161 "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
3163 /// See AbstractAttribute::trackStatistics()
3164 void trackStatistics() const override
{
3165 STATS_DECLTRACK_FN_ATTR(value_simplify
)
3169 struct AAValueSimplifyCallSite
: AAValueSimplifyFunction
{
3170 AAValueSimplifyCallSite(const IRPosition
&IRP
)
3171 : AAValueSimplifyFunction(IRP
) {}
3172 /// See AbstractAttribute::trackStatistics()
3173 void trackStatistics() const override
{
3174 STATS_DECLTRACK_CS_ATTR(value_simplify
)
3178 struct AAValueSimplifyCallSiteReturned
: AAValueSimplifyReturned
{
3179 AAValueSimplifyCallSiteReturned(const IRPosition
&IRP
)
3180 : AAValueSimplifyReturned(IRP
) {}
3182 void trackStatistics() const override
{
3183 STATS_DECLTRACK_CSRET_ATTR(value_simplify
)
3186 struct AAValueSimplifyCallSiteArgument
: AAValueSimplifyFloating
{
3187 AAValueSimplifyCallSiteArgument(const IRPosition
&IRP
)
3188 : AAValueSimplifyFloating(IRP
) {}
3190 void trackStatistics() const override
{
3191 STATS_DECLTRACK_CSARG_ATTR(value_simplify
)
3195 /// ----------------------- Heap-To-Stack Conversion ---------------------------
3196 struct AAHeapToStackImpl
: public AAHeapToStack
{
3197 AAHeapToStackImpl(const IRPosition
&IRP
) : AAHeapToStack(IRP
) {}
3199 const std::string
getAsStr() const override
{
3200 return "[H2S] Mallocs: " + std::to_string(MallocCalls
.size());
3203 ChangeStatus
manifest(Attributor
&A
) override
{
3204 assert(getState().isValidState() &&
3205 "Attempted to manifest an invalid state!");
3207 ChangeStatus HasChanged
= ChangeStatus::UNCHANGED
;
3208 Function
*F
= getAssociatedFunction();
3209 const auto *TLI
= A
.getInfoCache().getTargetLibraryInfoForFunction(*F
);
3211 for (Instruction
*MallocCall
: MallocCalls
) {
3212 // This malloc cannot be replaced.
3213 if (BadMallocCalls
.count(MallocCall
))
3216 for (Instruction
*FreeCall
: FreesForMalloc
[MallocCall
]) {
3217 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall
<< "\n");
3218 A
.deleteAfterManifest(*FreeCall
);
3219 HasChanged
= ChangeStatus::CHANGED
;
3222 LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall
3226 if (isCallocLikeFn(MallocCall
, TLI
)) {
3227 auto *Num
= cast
<ConstantInt
>(MallocCall
->getOperand(0));
3228 auto *SizeT
= dyn_cast
<ConstantInt
>(MallocCall
->getOperand(1));
3229 APInt TotalSize
= SizeT
->getValue() * Num
->getValue();
3231 ConstantInt::get(MallocCall
->getOperand(0)->getType(), TotalSize
);
3233 Size
= cast
<ConstantInt
>(MallocCall
->getOperand(0));
3236 unsigned AS
= cast
<PointerType
>(MallocCall
->getType())->getAddressSpace();
3237 Instruction
*AI
= new AllocaInst(Type::getInt8Ty(F
->getContext()), AS
,
3238 Size
, "", MallocCall
->getNextNode());
3240 if (AI
->getType() != MallocCall
->getType())
3241 AI
= new BitCastInst(AI
, MallocCall
->getType(), "malloc_bc",
3244 MallocCall
->replaceAllUsesWith(AI
);
3246 if (auto *II
= dyn_cast
<InvokeInst
>(MallocCall
)) {
3247 auto *NBB
= II
->getNormalDest();
3248 BranchInst::Create(NBB
, MallocCall
->getParent());
3249 A
.deleteAfterManifest(*MallocCall
);
3251 A
.deleteAfterManifest(*MallocCall
);
3254 if (isCallocLikeFn(MallocCall
, TLI
)) {
3255 auto *BI
= new BitCastInst(AI
, MallocCall
->getType(), "calloc_bc",
3258 BI
, ConstantInt::get(F
->getContext(), APInt(8, 0, false)), Size
,
3259 ConstantInt::get(Type::getInt1Ty(F
->getContext()), false)};
3261 Type
*Tys
[] = {BI
->getType(), MallocCall
->getOperand(0)->getType()};
3262 Module
*M
= F
->getParent();
3263 Function
*Fn
= Intrinsic::getDeclaration(M
, Intrinsic::memset
, Tys
);
3264 CallInst::Create(Fn
, Ops
, "", BI
->getNextNode());
3266 HasChanged
= ChangeStatus::CHANGED
;
3272 /// Collection of all malloc calls in a function.
3273 SmallSetVector
<Instruction
*, 4> MallocCalls
;
3275 /// Collection of malloc calls that cannot be converted.
3276 DenseSet
<const Instruction
*> BadMallocCalls
;
3278 /// A map for each malloc call to the set of associated free calls.
3279 DenseMap
<Instruction
*, SmallPtrSet
<Instruction
*, 4>> FreesForMalloc
;
3281 ChangeStatus
updateImpl(Attributor
&A
) override
;
3284 ChangeStatus
AAHeapToStackImpl::updateImpl(Attributor
&A
) {
3285 const Function
*F
= getAssociatedFunction();
3286 const auto *TLI
= A
.getInfoCache().getTargetLibraryInfoForFunction(*F
);
3288 auto UsesCheck
= [&](Instruction
&I
) {
3289 SmallPtrSet
<const Use
*, 8> Visited
;
3290 SmallVector
<const Use
*, 8> Worklist
;
3292 for (Use
&U
: I
.uses())
3293 Worklist
.push_back(&U
);
3295 while (!Worklist
.empty()) {
3296 const Use
*U
= Worklist
.pop_back_val();
3297 if (!Visited
.insert(U
).second
)
3300 auto *UserI
= U
->getUser();
3302 if (isa
<LoadInst
>(UserI
) || isa
<StoreInst
>(UserI
))
3305 // NOTE: Right now, if a function that has malloc pointer as an argument
3306 // frees memory, we assume that the malloc pointer is freed.
3308 // TODO: Add nofree callsite argument attribute to indicate that pointer
3309 // argument is not freed.
3310 if (auto *CB
= dyn_cast
<CallBase
>(UserI
)) {
3311 if (!CB
->isArgOperand(U
))
3314 if (CB
->isLifetimeStartOrEnd())
3318 if (isFreeCall(UserI
, TLI
)) {
3319 FreesForMalloc
[&I
].insert(
3320 cast
<Instruction
>(const_cast<User
*>(UserI
)));
3324 // If a function does not free memory we are fine
3325 const auto &NoFreeAA
=
3326 A
.getAAFor
<AANoFree
>(*this, IRPosition::callsite_function(*CB
));
3328 unsigned ArgNo
= U
- CB
->arg_begin();
3329 const auto &NoCaptureAA
= A
.getAAFor
<AANoCapture
>(
3330 *this, IRPosition::callsite_argument(*CB
, ArgNo
));
3332 if (!NoCaptureAA
.isAssumedNoCapture() || !NoFreeAA
.isAssumedNoFree()) {
3333 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI
<< "\n");
3339 if (isa
<GetElementPtrInst
>(UserI
) || isa
<BitCastInst
>(UserI
)) {
3340 for (Use
&U
: UserI
->uses())
3341 Worklist
.push_back(&U
);
3346 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI
<< "\n");
3352 auto MallocCallocCheck
= [&](Instruction
&I
) {
3353 if (isMallocLikeFn(&I
, TLI
)) {
3354 if (auto *Size
= dyn_cast
<ConstantInt
>(I
.getOperand(0)))
3355 if (!Size
->getValue().sle(MaxHeapToStackSize
))
3357 } else if (isCallocLikeFn(&I
, TLI
)) {
3358 bool Overflow
= false;
3359 if (auto *Num
= dyn_cast
<ConstantInt
>(I
.getOperand(0)))
3360 if (auto *Size
= dyn_cast
<ConstantInt
>(I
.getOperand(1)))
3361 if (!(Size
->getValue().umul_ov(Num
->getValue(), Overflow
))
3362 .sle(MaxHeapToStackSize
))
3366 BadMallocCalls
.insert(&I
);
3370 if (BadMallocCalls
.count(&I
))
3374 MallocCalls
.insert(&I
);
3376 BadMallocCalls
.insert(&I
);
3380 size_t NumBadMallocs
= BadMallocCalls
.size();
3382 A
.checkForAllCallLikeInstructions(MallocCallocCheck
, *this);
3384 if (NumBadMallocs
!= BadMallocCalls
.size())
3385 return ChangeStatus::CHANGED
;
3387 return ChangeStatus::UNCHANGED
;
3390 struct AAHeapToStackFunction final
: public AAHeapToStackImpl
{
3391 AAHeapToStackFunction(const IRPosition
&IRP
) : AAHeapToStackImpl(IRP
) {}
3393 /// See AbstractAttribute::trackStatistics()
3394 void trackStatistics() const override
{
3395 STATS_DECL(MallocCalls
, Function
,
3396 "Number of MallocCalls converted to allocas");
3397 BUILD_STAT_NAME(MallocCalls
, Function
) += MallocCalls
.size();
3401 /// ----------------------------------------------------------------------------
3403 /// ----------------------------------------------------------------------------
3405 bool Attributor::isAssumedDead(const AbstractAttribute
&AA
,
3406 const AAIsDead
*LivenessAA
) {
3407 const Instruction
*CtxI
= AA
.getIRPosition().getCtxI();
3413 &getAAFor
<AAIsDead
>(AA
, IRPosition::function(*CtxI
->getFunction()),
3414 /* TrackDependence */ false);
3416 // Don't check liveness for AAIsDead.
3417 if (&AA
== LivenessAA
)
3420 if (!LivenessAA
->isAssumedDead(CtxI
))
3423 // We actually used liveness information so we have to record a dependence.
3424 recordDependence(*LivenessAA
, AA
);
3429 bool Attributor::checkForAllCallSites(const function_ref
<bool(CallSite
)> &Pred
,
3430 const AbstractAttribute
&QueryingAA
,
3431 bool RequireAllCallSites
) {
3432 // We can try to determine information from
3433 // the call sites. However, this is only possible all call sites are known,
3434 // hence the function has internal linkage.
3435 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
3436 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
3437 if (!AssociatedFunction
)
3440 if (RequireAllCallSites
&& !AssociatedFunction
->hasInternalLinkage()) {
3443 << "[Attributor] Function " << AssociatedFunction
->getName()
3444 << " has no internal linkage, hence not all call sites are known\n");
3448 for (const Use
&U
: AssociatedFunction
->uses()) {
3449 Instruction
*I
= dyn_cast
<Instruction
>(U
.getUser());
3450 // TODO: Deal with abstract call sites here.
3454 Function
*Caller
= I
->getFunction();
3456 const auto &LivenessAA
= getAAFor
<AAIsDead
>(
3457 QueryingAA
, IRPosition::function(*Caller
), /* TrackDependence */ false);
3460 if (LivenessAA
.isAssumedDead(I
)) {
3461 // We actually used liveness information so we have to record a
3463 recordDependence(LivenessAA
, QueryingAA
);
3467 CallSite
CS(U
.getUser());
3468 if (!CS
|| !CS
.isCallee(&U
)) {
3469 if (!RequireAllCallSites
)
3472 LLVM_DEBUG(dbgs() << "[Attributor] User " << *U
.getUser()
3473 << " is an invalid use of "
3474 << AssociatedFunction
->getName() << "\n");
3481 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
3482 << *CS
.getInstruction() << "\n");
3489 bool Attributor::checkForAllReturnedValuesAndReturnInsts(
3490 const function_ref
<bool(Value
&, const SmallSetVector
<ReturnInst
*, 4> &)>
3492 const AbstractAttribute
&QueryingAA
) {
3494 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
3495 // Since we need to provide return instructions we have to have an exact
3497 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
3498 if (!AssociatedFunction
)
3501 // If this is a call site query we use the call site specific return values
3502 // and liveness information.
3503 // TODO: use the function scope once we have call site AAReturnedValues.
3504 const IRPosition
&QueryIRP
= IRPosition::function(*AssociatedFunction
);
3505 const auto &AARetVal
= getAAFor
<AAReturnedValues
>(QueryingAA
, QueryIRP
);
3506 if (!AARetVal
.getState().isValidState())
3509 return AARetVal
.checkForAllReturnedValuesAndReturnInsts(Pred
);
3512 bool Attributor::checkForAllReturnedValues(
3513 const function_ref
<bool(Value
&)> &Pred
,
3514 const AbstractAttribute
&QueryingAA
) {
3516 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
3517 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
3518 if (!AssociatedFunction
)
3521 // TODO: use the function scope once we have call site AAReturnedValues.
3522 const IRPosition
&QueryIRP
= IRPosition::function(*AssociatedFunction
);
3523 const auto &AARetVal
= getAAFor
<AAReturnedValues
>(QueryingAA
, QueryIRP
);
3524 if (!AARetVal
.getState().isValidState())
3527 return AARetVal
.checkForAllReturnedValuesAndReturnInsts(
3528 [&](Value
&RV
, const SmallSetVector
<ReturnInst
*, 4> &) {
3533 bool Attributor::checkForAllInstructions(
3534 const llvm::function_ref
<bool(Instruction
&)> &Pred
,
3535 const AbstractAttribute
&QueryingAA
, const ArrayRef
<unsigned> &Opcodes
) {
3537 const IRPosition
&IRP
= QueryingAA
.getIRPosition();
3538 // Since we need to provide instructions we have to have an exact definition.
3539 const Function
*AssociatedFunction
= IRP
.getAssociatedFunction();
3540 if (!AssociatedFunction
)
3543 // TODO: use the function scope once we have call site AAReturnedValues.
3544 const IRPosition
&QueryIRP
= IRPosition::function(*AssociatedFunction
);
3545 const auto &LivenessAA
=
3546 getAAFor
<AAIsDead
>(QueryingAA
, QueryIRP
, /* TrackDependence */ false);
3547 bool AnyDead
= false;
3549 auto &OpcodeInstMap
=
3550 InfoCache
.getOpcodeInstMapForFunction(*AssociatedFunction
);
3551 for (unsigned Opcode
: Opcodes
) {
3552 for (Instruction
*I
: OpcodeInstMap
[Opcode
]) {
3553 // Skip dead instructions.
3554 if (LivenessAA
.isAssumedDead(I
)) {
3564 // If we actually used liveness information so we have to record a dependence.
3566 recordDependence(LivenessAA
, QueryingAA
);
3571 bool Attributor::checkForAllReadWriteInstructions(
3572 const llvm::function_ref
<bool(Instruction
&)> &Pred
,
3573 AbstractAttribute
&QueryingAA
) {
3575 const Function
*AssociatedFunction
=
3576 QueryingAA
.getIRPosition().getAssociatedFunction();
3577 if (!AssociatedFunction
)
3580 // TODO: use the function scope once we have call site AAReturnedValues.
3581 const IRPosition
&QueryIRP
= IRPosition::function(*AssociatedFunction
);
3582 const auto &LivenessAA
=
3583 getAAFor
<AAIsDead
>(QueryingAA
, QueryIRP
, /* TrackDependence */ false);
3584 bool AnyDead
= false;
3586 for (Instruction
*I
:
3587 InfoCache
.getReadOrWriteInstsForFunction(*AssociatedFunction
)) {
3588 // Skip dead instructions.
3589 if (LivenessAA
.isAssumedDead(I
)) {
3598 // If we actually used liveness information so we have to record a dependence.
3600 recordDependence(LivenessAA
, QueryingAA
);
3605 ChangeStatus
Attributor::run(Module
&M
) {
3606 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
3607 << AllAbstractAttributes
.size()
3608 << " abstract attributes.\n");
3610 // Now that all abstract attributes are collected and initialized we start
3611 // the abstract analysis.
3613 unsigned IterationCounter
= 1;
3615 SmallVector
<AbstractAttribute
*, 64> ChangedAAs
;
3616 SetVector
<AbstractAttribute
*> Worklist
;
3617 Worklist
.insert(AllAbstractAttributes
.begin(), AllAbstractAttributes
.end());
3619 bool RecomputeDependences
= false;
3622 // Remember the size to determine new attributes.
3623 size_t NumAAs
= AllAbstractAttributes
.size();
3624 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
3625 << ", Worklist size: " << Worklist
.size() << "\n");
3627 // If dependences (=QueryMap) are recomputed we have to look at all abstract
3628 // attributes again, regardless of what changed in the last iteration.
3629 if (RecomputeDependences
) {
3631 dbgs() << "[Attributor] Run all AAs to recompute dependences\n");
3634 Worklist
.insert(AllAbstractAttributes
.begin(),
3635 AllAbstractAttributes
.end());
3638 // Add all abstract attributes that are potentially dependent on one that
3639 // changed to the work list.
3640 for (AbstractAttribute
*ChangedAA
: ChangedAAs
) {
3641 auto &QuerriedAAs
= QueryMap
[ChangedAA
];
3642 Worklist
.insert(QuerriedAAs
.begin(), QuerriedAAs
.end());
3645 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
3646 << ", Worklist+Dependent size: " << Worklist
.size()
3649 // Reset the changed set.
3652 // Update all abstract attribute in the work list and record the ones that
3654 for (AbstractAttribute
*AA
: Worklist
)
3655 if (!isAssumedDead(*AA
, nullptr))
3656 if (AA
->update(*this) == ChangeStatus::CHANGED
)
3657 ChangedAAs
.push_back(AA
);
3659 // Check if we recompute the dependences in the next iteration.
3660 RecomputeDependences
= (DepRecomputeInterval
> 0 &&
3661 IterationCounter
% DepRecomputeInterval
== 0);
3663 // Add attributes to the changed set if they have been created in the last
3665 ChangedAAs
.append(AllAbstractAttributes
.begin() + NumAAs
,
3666 AllAbstractAttributes
.end());
3668 // Reset the work list and repopulate with the changed abstract attributes.
3669 // Note that dependent ones are added above.
3671 Worklist
.insert(ChangedAAs
.begin(), ChangedAAs
.end());
3673 } while (!Worklist
.empty() && (IterationCounter
++ < MaxFixpointIterations
||
3674 VerifyMaxFixpointIterations
));
3676 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
3677 << IterationCounter
<< "/" << MaxFixpointIterations
3678 << " iterations\n");
3680 size_t NumFinalAAs
= AllAbstractAttributes
.size();
3682 bool FinishedAtFixpoint
= Worklist
.empty();
3684 // Reset abstract arguments not settled in a sound fixpoint by now. This
3685 // happens when we stopped the fixpoint iteration early. Note that only the
3686 // ones marked as "changed" *and* the ones transitively depending on them
3687 // need to be reverted to a pessimistic state. Others might not be in a
3688 // fixpoint state but we can use the optimistic results for them anyway.
3689 SmallPtrSet
<AbstractAttribute
*, 32> Visited
;
3690 for (unsigned u
= 0; u
< ChangedAAs
.size(); u
++) {
3691 AbstractAttribute
*ChangedAA
= ChangedAAs
[u
];
3692 if (!Visited
.insert(ChangedAA
).second
)
3695 AbstractState
&State
= ChangedAA
->getState();
3696 if (!State
.isAtFixpoint()) {
3697 State
.indicatePessimisticFixpoint();
3699 NumAttributesTimedOut
++;
3702 auto &QuerriedAAs
= QueryMap
[ChangedAA
];
3703 ChangedAAs
.append(QuerriedAAs
.begin(), QuerriedAAs
.end());
3707 if (!Visited
.empty())
3708 dbgs() << "\n[Attributor] Finalized " << Visited
.size()
3709 << " abstract attributes.\n";
3712 unsigned NumManifested
= 0;
3713 unsigned NumAtFixpoint
= 0;
3714 ChangeStatus ManifestChange
= ChangeStatus::UNCHANGED
;
3715 for (AbstractAttribute
*AA
: AllAbstractAttributes
) {
3716 AbstractState
&State
= AA
->getState();
3718 // If there is not already a fixpoint reached, we can now take the
3719 // optimistic state. This is correct because we enforced a pessimistic one
3720 // on abstract attributes that were transitively dependent on a changed one
3722 if (!State
.isAtFixpoint())
3723 State
.indicateOptimisticFixpoint();
3725 // If the state is invalid, we do not try to manifest it.
3726 if (!State
.isValidState())
3730 if (isAssumedDead(*AA
, nullptr))
3732 // Manifest the state and record if we changed the IR.
3733 ChangeStatus LocalChange
= AA
->manifest(*this);
3734 if (LocalChange
== ChangeStatus::CHANGED
&& AreStatisticsEnabled())
3735 AA
->trackStatistics();
3737 ManifestChange
= ManifestChange
| LocalChange
;
3740 NumManifested
+= (LocalChange
== ChangeStatus::CHANGED
);
3743 (void)NumManifested
;
3744 (void)NumAtFixpoint
;
3745 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
3746 << " arguments while " << NumAtFixpoint
3747 << " were in a valid fixpoint state\n");
3749 // If verification is requested, we finished this run at a fixpoint, and the
3750 // IR was changed, we re-run the whole fixpoint analysis, starting at
3751 // re-initialization of the arguments. This re-run should not result in an IR
3752 // change. Though, the (virtual) state of attributes at the end of the re-run
3753 // might be more optimistic than the known state or the IR state if the better
3754 // state cannot be manifested.
3755 if (VerifyAttributor
&& FinishedAtFixpoint
&&
3756 ManifestChange
== ChangeStatus::CHANGED
) {
3757 VerifyAttributor
= false;
3758 ChangeStatus VerifyStatus
= run(M
);
3759 if (VerifyStatus
!= ChangeStatus::UNCHANGED
)
3761 "Attributor verification failed, re-run did result in an IR change "
3762 "even after a fixpoint was reached in the original run. (False "
3763 "positives possible!)");
3764 VerifyAttributor
= true;
3767 NumAttributesManifested
+= NumManifested
;
3768 NumAttributesValidFixpoint
+= NumAtFixpoint
;
3772 NumFinalAAs
== AllAbstractAttributes
.size() &&
3773 "Expected the final number of abstract attributes to remain unchanged!");
3775 // Delete stuff at the end to avoid invalid references and a nice order.
3777 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least "
3778 << ToBeDeletedFunctions
.size() << " functions and "
3779 << ToBeDeletedBlocks
.size() << " blocks and "
3780 << ToBeDeletedInsts
.size() << " instructions\n");
3781 for (Instruction
*I
: ToBeDeletedInsts
) {
3782 if (!I
->use_empty())
3783 I
->replaceAllUsesWith(UndefValue::get(I
->getType()));
3784 I
->eraseFromParent();
3787 if (unsigned NumDeadBlocks
= ToBeDeletedBlocks
.size()) {
3788 SmallVector
<BasicBlock
*, 8> ToBeDeletedBBs
;
3789 ToBeDeletedBBs
.reserve(NumDeadBlocks
);
3790 ToBeDeletedBBs
.append(ToBeDeletedBlocks
.begin(), ToBeDeletedBlocks
.end());
3791 DeleteDeadBlocks(ToBeDeletedBBs
);
3792 STATS_DECLTRACK(AAIsDead
, BasicBlock
,
3793 "Number of dead basic blocks deleted.");
3796 STATS_DECL(AAIsDead
, Function
, "Number of dead functions deleted.");
3797 for (Function
*Fn
: ToBeDeletedFunctions
) {
3798 Fn
->replaceAllUsesWith(UndefValue::get(Fn
->getType()));
3799 Fn
->eraseFromParent();
3800 STATS_TRACK(AAIsDead
, Function
);
3803 // Identify dead internal functions and delete them. This happens outside
3804 // the other fixpoint analysis as we might treat potentially dead functions
3805 // as live to lower the number of iterations. If they happen to be dead, the
3806 // below fixpoint loop will identify and eliminate them.
3807 SmallVector
<Function
*, 8> InternalFns
;
3808 for (Function
&F
: M
)
3809 if (F
.hasInternalLinkage())
3810 InternalFns
.push_back(&F
);
3812 bool FoundDeadFn
= true;
3813 while (FoundDeadFn
) {
3814 FoundDeadFn
= false;
3815 for (unsigned u
= 0, e
= InternalFns
.size(); u
< e
; ++u
) {
3816 Function
*F
= InternalFns
[u
];
3820 const auto *LivenessAA
=
3821 lookupAAFor
<AAIsDead
>(IRPosition::function(*F
));
3823 !checkForAllCallSites([](CallSite CS
) { return false; },
3827 STATS_TRACK(AAIsDead
, Function
);
3828 F
->replaceAllUsesWith(UndefValue::get(F
->getType()));
3829 F
->eraseFromParent();
3830 InternalFns
[u
] = nullptr;
3836 if (VerifyMaxFixpointIterations
&&
3837 IterationCounter
!= MaxFixpointIterations
) {
3838 errs() << "\n[Attributor] Fixpoint iteration done after: "
3839 << IterationCounter
<< "/" << MaxFixpointIterations
3841 llvm_unreachable("The fixpoint was not reached with exactly the number of "
3842 "specified iterations!");
3845 return ManifestChange
;
3848 void Attributor::identifyDefaultAbstractAttributes(
3849 Function
&F
, std::function
<TargetLibraryInfo
*(Function
&)> &TLIGetter
) {
3850 if (!VisitedFunctions
.insert(&F
).second
)
3853 if (EnableHeapToStack
)
3854 InfoCache
.FuncTLIMap
[&F
] = TLIGetter(F
);
3856 IRPosition FPos
= IRPosition::function(F
);
3858 // Check for dead BasicBlocks in every function.
3859 // We need dead instruction detection because we do not want to deal with
3860 // broken IR in which SSA rules do not apply.
3861 getOrCreateAAFor
<AAIsDead
>(FPos
);
3863 // Every function might be "will-return".
3864 getOrCreateAAFor
<AAWillReturn
>(FPos
);
3866 // Every function can be nounwind.
3867 getOrCreateAAFor
<AANoUnwind
>(FPos
);
3869 // Every function might be marked "nosync"
3870 getOrCreateAAFor
<AANoSync
>(FPos
);
3872 // Every function might be "no-free".
3873 getOrCreateAAFor
<AANoFree
>(FPos
);
3875 // Every function might be "no-return".
3876 getOrCreateAAFor
<AANoReturn
>(FPos
);
3878 // Every function might be applicable for Heap-To-Stack conversion.
3879 if (EnableHeapToStack
)
3880 getOrCreateAAFor
<AAHeapToStack
>(FPos
);
3882 // Return attributes are only appropriate if the return type is non void.
3883 Type
*ReturnType
= F
.getReturnType();
3884 if (!ReturnType
->isVoidTy()) {
3885 // Argument attribute "returned" --- Create only one per function even
3886 // though it is an argument attribute.
3887 getOrCreateAAFor
<AAReturnedValues
>(FPos
);
3889 IRPosition RetPos
= IRPosition::returned(F
);
3891 // Every function might be simplified.
3892 getOrCreateAAFor
<AAValueSimplify
>(RetPos
);
3894 if (ReturnType
->isPointerTy()) {
3896 // Every function with pointer return type might be marked align.
3897 getOrCreateAAFor
<AAAlign
>(RetPos
);
3899 // Every function with pointer return type might be marked nonnull.
3900 getOrCreateAAFor
<AANonNull
>(RetPos
);
3902 // Every function with pointer return type might be marked noalias.
3903 getOrCreateAAFor
<AANoAlias
>(RetPos
);
3905 // Every function with pointer return type might be marked
3907 getOrCreateAAFor
<AADereferenceable
>(RetPos
);
3911 for (Argument
&Arg
: F
.args()) {
3912 IRPosition ArgPos
= IRPosition::argument(Arg
);
3914 // Every argument might be simplified.
3915 getOrCreateAAFor
<AAValueSimplify
>(ArgPos
);
3917 if (Arg
.getType()->isPointerTy()) {
3918 // Every argument with pointer type might be marked nonnull.
3919 getOrCreateAAFor
<AANonNull
>(ArgPos
);
3921 // Every argument with pointer type might be marked noalias.
3922 getOrCreateAAFor
<AANoAlias
>(ArgPos
);
3924 // Every argument with pointer type might be marked dereferenceable.
3925 getOrCreateAAFor
<AADereferenceable
>(ArgPos
);
3927 // Every argument with pointer type might be marked align.
3928 getOrCreateAAFor
<AAAlign
>(ArgPos
);
3930 // Every argument with pointer type might be marked nocapture.
3931 getOrCreateAAFor
<AANoCapture
>(ArgPos
);
3935 // Walk all instructions to find more attribute opportunities and also
3936 // interesting instructions that might be queried by abstract attributes
3937 // during their initialization or update.
3938 auto &ReadOrWriteInsts
= InfoCache
.FuncRWInstsMap
[&F
];
3939 auto &InstOpcodeMap
= InfoCache
.FuncInstOpcodeMap
[&F
];
3941 for (Instruction
&I
: instructions(&F
)) {
3942 bool IsInterestingOpcode
= false;
3944 // To allow easy access to all instructions in a function with a given
3945 // opcode we store them in the InfoCache. As not all opcodes are interesting
3946 // to concrete attributes we only cache the ones that are as identified in
3947 // the following switch.
3948 // Note: There are no concrete attributes now so this is initially empty.
3949 switch (I
.getOpcode()) {
3951 assert((!ImmutableCallSite(&I
)) && (!isa
<CallBase
>(&I
)) &&
3952 "New call site/base instruction type needs to be known int the "
3955 case Instruction::Load
:
3956 // The alignment of a pointer is interesting for loads.
3957 getOrCreateAAFor
<AAAlign
>(
3958 IRPosition::value(*cast
<LoadInst
>(I
).getPointerOperand()));
3960 case Instruction::Store
:
3961 // The alignment of a pointer is interesting for stores.
3962 getOrCreateAAFor
<AAAlign
>(
3963 IRPosition::value(*cast
<StoreInst
>(I
).getPointerOperand()));
3965 case Instruction::Call
:
3966 case Instruction::CallBr
:
3967 case Instruction::Invoke
:
3968 case Instruction::CleanupRet
:
3969 case Instruction::CatchSwitch
:
3970 case Instruction::Resume
:
3971 case Instruction::Ret
:
3972 IsInterestingOpcode
= true;
3974 if (IsInterestingOpcode
)
3975 InstOpcodeMap
[I
.getOpcode()].push_back(&I
);
3976 if (I
.mayReadOrWriteMemory())
3977 ReadOrWriteInsts
.push_back(&I
);
3980 if (CS
&& CS
.getCalledFunction()) {
3981 for (int i
= 0, e
= CS
.getCalledFunction()->arg_size(); i
< e
; i
++) {
3983 IRPosition CSArgPos
= IRPosition::callsite_argument(CS
, i
);
3985 // Call site argument might be simplified.
3986 getOrCreateAAFor
<AAValueSimplify
>(CSArgPos
);
3988 if (!CS
.getArgument(i
)->getType()->isPointerTy())
3991 // Call site argument attribute "non-null".
3992 getOrCreateAAFor
<AANonNull
>(CSArgPos
);
3994 // Call site argument attribute "no-alias".
3995 getOrCreateAAFor
<AANoAlias
>(CSArgPos
);
3997 // Call site argument attribute "dereferenceable".
3998 getOrCreateAAFor
<AADereferenceable
>(CSArgPos
);
4000 // Call site argument attribute "align".
4001 getOrCreateAAFor
<AAAlign
>(CSArgPos
);
4007 /// Helpers to ease debugging through output streams and print calls.
4010 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, ChangeStatus S
) {
4011 return OS
<< (S
== ChangeStatus::CHANGED
? "changed" : "unchanged");
4014 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, IRPosition::Kind AP
) {
4016 case IRPosition::IRP_INVALID
:
4018 case IRPosition::IRP_FLOAT
:
4020 case IRPosition::IRP_RETURNED
:
4021 return OS
<< "fn_ret";
4022 case IRPosition::IRP_CALL_SITE_RETURNED
:
4023 return OS
<< "cs_ret";
4024 case IRPosition::IRP_FUNCTION
:
4026 case IRPosition::IRP_CALL_SITE
:
4028 case IRPosition::IRP_ARGUMENT
:
4030 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
4031 return OS
<< "cs_arg";
4033 llvm_unreachable("Unknown attribute position!");
4036 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const IRPosition
&Pos
) {
4037 const Value
&AV
= Pos
.getAssociatedValue();
4038 return OS
<< "{" << Pos
.getPositionKind() << ":" << AV
.getName() << " ["
4039 << Pos
.getAnchorValue().getName() << "@" << Pos
.getArgNo() << "]}";
4042 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const IntegerState
&S
) {
4043 return OS
<< "(" << S
.getKnown() << "-" << S
.getAssumed() << ")"
4044 << static_cast<const AbstractState
&>(S
);
4047 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const AbstractState
&S
) {
4048 return OS
<< (!S
.isValidState() ? "top" : (S
.isAtFixpoint() ? "fix" : ""));
4051 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const AbstractAttribute
&AA
) {
4056 void AbstractAttribute::print(raw_ostream
&OS
) const {
4057 OS
<< "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState()
4062 /// ----------------------------------------------------------------------------
4063 /// Pass (Manager) Boilerplate
4064 /// ----------------------------------------------------------------------------
4066 static bool runAttributorOnModule(
4067 Module
&M
, std::function
<TargetLibraryInfo
*(Function
&)> &TLIGetter
) {
4068 if (DisableAttributor
)
4071 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M
.size()
4072 << " functions.\n");
4074 // Create an Attributor and initially empty information cache that is filled
4075 // while we identify default attribute opportunities.
4076 InformationCache
InfoCache(M
.getDataLayout());
4077 Attributor
A(InfoCache
, DepRecInterval
);
4079 for (Function
&F
: M
) {
4080 if (F
.hasExactDefinition())
4081 NumFnWithExactDefinition
++;
4083 NumFnWithoutExactDefinition
++;
4085 // For now we ignore naked and optnone functions.
4086 if (F
.hasFnAttribute(Attribute::Naked
) ||
4087 F
.hasFnAttribute(Attribute::OptimizeNone
))
4090 // We look at internal functions only on-demand but if any use is not a
4091 // direct call, we have to do it eagerly.
4092 if (F
.hasInternalLinkage()) {
4093 if (llvm::all_of(F
.uses(), [](const Use
&U
) {
4094 return ImmutableCallSite(U
.getUser()) &&
4095 ImmutableCallSite(U
.getUser()).isCallee(&U
);
4100 // Populate the Attributor with abstract attribute opportunities in the
4101 // function and the information cache with IR information.
4102 A
.identifyDefaultAbstractAttributes(F
, TLIGetter
);
4105 return A
.run(M
) == ChangeStatus::CHANGED
;
4108 PreservedAnalyses
AttributorPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
4109 auto &FAM
= AM
.getResult
<FunctionAnalysisManagerModuleProxy
>(M
).getManager();
4111 std::function
<TargetLibraryInfo
*(Function
&)> TLIGetter
=
4112 [&](Function
&F
) -> TargetLibraryInfo
* {
4113 return &FAM
.getResult
<TargetLibraryAnalysis
>(F
);
4116 if (runAttributorOnModule(M
, TLIGetter
)) {
4117 // FIXME: Think about passes we will preserve and add them here.
4118 return PreservedAnalyses::none();
4120 return PreservedAnalyses::all();
4125 struct AttributorLegacyPass
: public ModulePass
{
4128 AttributorLegacyPass() : ModulePass(ID
) {
4129 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
4132 bool runOnModule(Module
&M
) override
{
4135 std::function
<TargetLibraryInfo
*(Function
&)> TLIGetter
=
4136 [&](Function
&F
) -> TargetLibraryInfo
* { return nullptr; };
4138 return runAttributorOnModule(M
, TLIGetter
);
4141 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
4142 // FIXME: Think about passes we will preserve and add them here.
4143 AU
.addRequired
<TargetLibraryInfoWrapperPass
>();
4147 } // end anonymous namespace
4149 Pass
*llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
4151 char AttributorLegacyPass::ID
= 0;
4153 const char AAReturnedValues::ID
= 0;
4154 const char AANoUnwind::ID
= 0;
4155 const char AANoSync::ID
= 0;
4156 const char AANoFree::ID
= 0;
4157 const char AANonNull::ID
= 0;
4158 const char AANoRecurse::ID
= 0;
4159 const char AAWillReturn::ID
= 0;
4160 const char AANoAlias::ID
= 0;
4161 const char AANoReturn::ID
= 0;
4162 const char AAIsDead::ID
= 0;
4163 const char AADereferenceable::ID
= 0;
4164 const char AAAlign::ID
= 0;
4165 const char AANoCapture::ID
= 0;
4166 const char AAValueSimplify::ID
= 0;
4167 const char AAHeapToStack::ID
= 0;
4169 // Macro magic to create the static generator function for attributes that
4170 // follow the naming scheme.
4172 #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \
4173 case IRPosition::PK: \
4174 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
4176 #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \
4177 case IRPosition::PK: \
4178 AA = new CLASS##SUFFIX(IRP); \
4181 #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4182 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4183 CLASS *AA = nullptr; \
4184 switch (IRP.getPositionKind()) { \
4185 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4186 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
4187 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
4188 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
4189 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
4190 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
4191 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
4192 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
4197 #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4198 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4199 CLASS *AA = nullptr; \
4200 switch (IRP.getPositionKind()) { \
4201 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4202 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \
4203 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
4204 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
4205 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
4206 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
4207 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
4208 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
4213 #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4214 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4215 CLASS *AA = nullptr; \
4216 switch (IRP.getPositionKind()) { \
4217 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4218 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
4219 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
4220 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
4221 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
4222 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
4223 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
4224 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
4229 #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4230 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4231 CLASS *AA = nullptr; \
4232 switch (IRP.getPositionKind()) { \
4233 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4234 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
4235 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
4236 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
4237 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
4238 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
4239 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
4240 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
4242 AA->initialize(A); \
4246 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind
)
4247 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync
)
4248 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree
)
4249 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse
)
4250 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn
)
4251 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn
)
4252 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead
)
4253 CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues
)
4255 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull
)
4256 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias
)
4257 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable
)
4258 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign
)
4259 CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture
)
4261 CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify
)
4263 CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack
)
4265 #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
4266 #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
4267 #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
4268 #undef SWITCH_PK_CREATE
4269 #undef SWITCH_PK_INV
4271 INITIALIZE_PASS_BEGIN(AttributorLegacyPass
, "attributor",
4272 "Deduce and propagate attributes", false, false)
4273 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass
)
4274 INITIALIZE_PASS_END(AttributorLegacyPass
, "attributor",
4275 "Deduce and propagate attributes", false, false)