1 //===- Attributor.h --- Module-wide attribute deduction ---------*- C++ -*-===//
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 // Attributor: An inter procedural (abstract) "attribute" deduction framework.
11 // The Attributor framework is an inter procedural abstract analysis (fixpoint
12 // iteration analysis). The goal is to allow easy deduction of new attributes as
13 // well as information exchange between abstract attributes in-flight.
15 // The Attributor class is the driver and the link between the various abstract
16 // attributes. The Attributor will iterate until a fixpoint state is reached by
17 // all abstract attributes in-flight, or until it will enforce a pessimistic fix
18 // point because an iteration limit is reached.
20 // Abstract attributes, derived from the AbstractAttribute class, actually
21 // describe properties of the code. They can correspond to actual LLVM-IR
22 // attributes, or they can be more general, ultimately unrelated to LLVM-IR
23 // attributes. The latter is useful when an abstract attributes provides
24 // information to other abstract attributes in-flight but we might not want to
25 // manifest the information. The Attributor allows to query in-flight abstract
26 // attributes through the `Attributor::getAAFor` method (see the method
27 // description for an example). If the method is used by an abstract attribute
28 // P, and it results in an abstract attribute Q, the Attributor will
29 // automatically capture a potential dependence from Q to P. This dependence
30 // will cause P to be reevaluated whenever Q changes in the future.
32 // The Attributor will only reevaluated abstract attributes that might have
33 // changed since the last iteration. That means that the Attribute will not
34 // revisit all instructions/blocks/functions in the module but only query
35 // an update from a subset of the abstract attributes.
37 // The update method `AbstractAttribute::updateImpl` is implemented by the
38 // specific "abstract attribute" subclasses. The method is invoked whenever the
39 // currently assumed state (see the AbstractState class) might not be valid
40 // anymore. This can, for example, happen if the state was dependent on another
41 // abstract attribute that changed. In every invocation, the update method has
42 // to adjust the internal state of an abstract attribute to a point that is
43 // justifiable by the underlying IR and the current state of abstract attributes
44 // in-flight. Since the IR is given and assumed to be valid, the information
45 // derived from it can be assumed to hold. However, information derived from
46 // other abstract attributes is conditional on various things. If the justifying
47 // state changed, the `updateImpl` has to revisit the situation and potentially
48 // find another justification or limit the optimistic assumes made.
50 // Change is the key in this framework. Until a state of no-change, thus a
51 // fixpoint, is reached, the Attributor will query the abstract attributes
52 // in-flight to re-evaluate their state. If the (current) state is too
53 // optimistic, hence it cannot be justified anymore through other abstract
54 // attributes or the state of the IR, the state of the abstract attribute will
55 // have to change. Generally, we assume abstract attribute state to be a finite
56 // height lattice and the update function to be monotone. However, these
57 // conditions are not enforced because the iteration limit will guarantee
58 // termination. If an optimistic fixpoint is reached, or a pessimistic fix
59 // point is enforced after a timeout, the abstract attributes are tasked to
60 // manifest their result in the IR for passes to come.
62 // Attribute manifestation is not mandatory. If desired, there is support to
63 // generate a single or multiple LLVM-IR attributes already in the helper struct
64 // IRAttribute. In the simplest case, a subclass inherits from IRAttribute with
65 // a proper Attribute::AttrKind as template parameter. The Attributor
66 // manifestation framework will then create and place a new attribute if it is
67 // allowed to do so (based on the abstract state). Other use cases can be
68 // achieved by overloading AbstractAttribute or IRAttribute methods.
71 // The "mechanics" of adding a new "abstract attribute":
72 // - Define a class (transitively) inheriting from AbstractAttribute and one
73 // (which could be the same) that (transitively) inherits from AbstractState.
74 // For the latter, consider the already available BooleanState and
75 // IntegerState if they fit your needs, e.g., you require only a bit-encoding.
76 // - Implement all pure methods. Also use overloading if the attribute is not
77 // conforming with the "default" behavior: A (set of) LLVM-IR attribute(s) for
78 // an argument, call site argument, function return value, or function. See
79 // the class and method descriptions for more information on the two
80 // "Abstract" classes and their respective methods.
81 // - Register opportunities for the new abstract attribute in the
82 // `Attributor::identifyDefaultAbstractAttributes` method if it should be
83 // counted as a 'default' attribute.
84 // - Add sufficient tests.
85 // - Add a Statistics object for bookkeeping. If it is a simple (set of)
86 // attribute(s) manifested through the Attributor manifestation framework, see
87 // the bookkeeping function in Attributor.cpp.
88 // - If instructions with a certain opcode are interesting to the attribute, add
89 // that opcode to the switch in `Attributor::identifyAbstractAttributes`. This
90 // will make it possible to query all those instructions through the
91 // `InformationCache::getOpcodeInstMapForFunction` interface and eliminate the
92 // need to traverse the IR repeatedly.
94 //===----------------------------------------------------------------------===//
96 #ifndef LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
97 #define LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
99 #include "llvm/ADT/SetVector.h"
100 #include "llvm/Analysis/TargetLibraryInfo.h"
101 #include "llvm/ADT/MapVector.h"
102 #include "llvm/IR/CallSite.h"
103 #include "llvm/IR/PassManager.h"
107 struct AbstractAttribute
;
108 struct InformationCache
;
113 /// Simple enum class that forces the status to be spelled out explicitly.
116 enum class ChangeStatus
{
121 ChangeStatus
operator|(ChangeStatus l
, ChangeStatus r
);
122 ChangeStatus
operator&(ChangeStatus l
, ChangeStatus r
);
125 /// Helper to describe and deal with positions in the LLVM-IR.
127 /// A position in the IR is described by an anchor value and an "offset" that
128 /// could be the argument number, for call sites and arguments, or an indicator
129 /// of the "position kind". The kinds, specified in the Kind enum below, include
130 /// the locations in the attribute list, i.a., function scope and return value,
131 /// as well as a distinction between call sites and functions. Finally, there
132 /// are floating values that do not have a corresponding attribute list
135 virtual ~IRPosition() {}
137 /// The positions we distinguish in the IR.
139 /// The values are chosen such that the KindOrArgNo member has a value >= 1
140 /// if it is an argument or call site argument while a value < 1 indicates the
141 /// respective kind of that value.
143 IRP_INVALID
= -6, ///< An invalid position.
144 IRP_FLOAT
= -5, ///< A position that is not associated with a spot suitable
145 ///< for attributes. This could be any value or instruction.
146 IRP_RETURNED
= -4, ///< An attribute for the function return value.
147 IRP_CALL_SITE_RETURNED
= -3, ///< An attribute for a call site return value.
148 IRP_FUNCTION
= -2, ///< An attribute for a function (scope).
149 IRP_CALL_SITE
= -1, ///< An attribute for a call site (function scope).
150 IRP_ARGUMENT
= 0, ///< An attribute for a function argument.
151 IRP_CALL_SITE_ARGUMENT
= 1, ///< An attribute for a call site argument.
154 /// Default constructor available to create invalid positions implicitly. All
155 /// other positions need to be created explicitly through the appropriate
156 /// static member function.
157 IRPosition() : AnchorVal(nullptr), KindOrArgNo(IRP_INVALID
) { verify(); }
159 /// Create a position describing the value of \p V.
160 static const IRPosition
value(const Value
&V
) {
161 if (auto *Arg
= dyn_cast
<Argument
>(&V
))
162 return IRPosition::argument(*Arg
);
163 if (auto *CB
= dyn_cast
<CallBase
>(&V
))
164 return IRPosition::callsite_returned(*CB
);
165 return IRPosition(const_cast<Value
&>(V
), IRP_FLOAT
);
168 /// Create a position describing the function scope of \p F.
169 static const IRPosition
function(const Function
&F
) {
170 return IRPosition(const_cast<Function
&>(F
), IRP_FUNCTION
);
173 /// Create a position describing the returned value of \p F.
174 static const IRPosition
returned(const Function
&F
) {
175 return IRPosition(const_cast<Function
&>(F
), IRP_RETURNED
);
178 /// Create a position describing the argument \p Arg.
179 static const IRPosition
argument(const Argument
&Arg
) {
180 return IRPosition(const_cast<Argument
&>(Arg
), Kind(Arg
.getArgNo()));
183 /// Create a position describing the function scope of \p CB.
184 static const IRPosition
callsite_function(const CallBase
&CB
) {
185 return IRPosition(const_cast<CallBase
&>(CB
), IRP_CALL_SITE
);
188 /// Create a position describing the returned value of \p CB.
189 static const IRPosition
callsite_returned(const CallBase
&CB
) {
190 return IRPosition(const_cast<CallBase
&>(CB
), IRP_CALL_SITE_RETURNED
);
193 /// Create a position describing the argument of \p CB at position \p ArgNo.
194 static const IRPosition
callsite_argument(const CallBase
&CB
,
196 return IRPosition(const_cast<CallBase
&>(CB
), Kind(ArgNo
));
199 /// Create a position describing the function scope of \p ICS.
200 static const IRPosition
callsite_function(ImmutableCallSite ICS
) {
201 return IRPosition::callsite_function(cast
<CallBase
>(*ICS
.getInstruction()));
204 /// Create a position describing the returned value of \p ICS.
205 static const IRPosition
callsite_returned(ImmutableCallSite ICS
) {
206 return IRPosition::callsite_returned(cast
<CallBase
>(*ICS
.getInstruction()));
209 /// Create a position describing the argument of \p ICS at position \p ArgNo.
210 static const IRPosition
callsite_argument(ImmutableCallSite ICS
,
212 return IRPosition::callsite_argument(cast
<CallBase
>(*ICS
.getInstruction()),
216 /// Create a position with function scope matching the "context" of \p IRP.
217 /// If \p IRP is a call site (see isAnyCallSitePosition()) then the result
218 /// will be a call site position, otherwise the function position of the
219 /// associated function.
220 static const IRPosition
function_scope(const IRPosition
&IRP
) {
221 if (IRP
.isAnyCallSitePosition()) {
222 return IRPosition::callsite_function(
223 cast
<CallBase
>(IRP
.getAnchorValue()));
225 assert(IRP
.getAssociatedFunction());
226 return IRPosition::function(*IRP
.getAssociatedFunction());
229 bool operator==(const IRPosition
&RHS
) const {
230 return (AnchorVal
== RHS
.AnchorVal
) && (KindOrArgNo
== RHS
.KindOrArgNo
);
232 bool operator!=(const IRPosition
&RHS
) const { return !(*this == RHS
); }
234 /// Return the value this abstract attribute is anchored with.
236 /// The anchor value might not be the associated value if the latter is not
237 /// sufficient to determine where arguments will be manifested. This is, so
238 /// far, only the case for call site arguments as the value is not sufficient
239 /// to pinpoint them. Instead, we can use the call site as an anchor.
242 Value
&getAnchorValue() {
243 assert(KindOrArgNo
!= IRP_INVALID
&&
244 "Invalid position does not have an anchor value!");
247 const Value
&getAnchorValue() const {
248 return const_cast<IRPosition
*>(this)->getAnchorValue();
252 /// Return the associated function, if any.
255 Function
*getAssociatedFunction() {
256 if (auto *CB
= dyn_cast
<CallBase
>(AnchorVal
))
257 return CB
->getCalledFunction();
258 assert(KindOrArgNo
!= IRP_INVALID
&&
259 "Invalid position does not have an anchor scope!");
260 Value
&V
= getAnchorValue();
261 if (isa
<Function
>(V
))
262 return &cast
<Function
>(V
);
263 if (isa
<Argument
>(V
))
264 return cast
<Argument
>(V
).getParent();
265 if (isa
<Instruction
>(V
))
266 return cast
<Instruction
>(V
).getFunction();
269 const Function
*getAssociatedFunction() const {
270 return const_cast<IRPosition
*>(this)->getAssociatedFunction();
274 /// Return the associated argument, if any.
277 Argument
*getAssociatedArgument() {
278 if (auto *Arg
= dyn_cast
<Argument
>(&getAnchorValue()))
280 int ArgNo
= getArgNo();
283 Function
*AssociatedFn
= getAssociatedFunction();
284 if (!AssociatedFn
|| AssociatedFn
->arg_size() <= unsigned(ArgNo
))
286 return AssociatedFn
->arg_begin() + ArgNo
;
288 const Argument
*getAssociatedArgument() const {
289 return const_cast<IRPosition
*>(this)->getAssociatedArgument();
293 /// Return true if the position refers to a function interface, that is the
294 /// function scope, the function return, or an argumnt.
295 bool isFnInterfaceKind() const {
296 switch (getPositionKind()) {
297 case IRPosition::IRP_FUNCTION
:
298 case IRPosition::IRP_RETURNED
:
299 case IRPosition::IRP_ARGUMENT
:
306 /// Return the Function surrounding the anchor value.
309 Function
*getAnchorScope() {
310 Value
&V
= getAnchorValue();
311 if (isa
<Function
>(V
))
312 return &cast
<Function
>(V
);
313 if (isa
<Argument
>(V
))
314 return cast
<Argument
>(V
).getParent();
315 if (isa
<Instruction
>(V
))
316 return cast
<Instruction
>(V
).getFunction();
319 const Function
*getAnchorScope() const {
320 return const_cast<IRPosition
*>(this)->getAnchorScope();
324 /// Return the context instruction, if any.
327 Instruction
*getCtxI() {
328 Value
&V
= getAnchorValue();
329 if (auto *I
= dyn_cast
<Instruction
>(&V
))
331 if (auto *Arg
= dyn_cast
<Argument
>(&V
))
332 if (!Arg
->getParent()->isDeclaration())
333 return &Arg
->getParent()->getEntryBlock().front();
334 if (auto *F
= dyn_cast
<Function
>(&V
))
335 if (!F
->isDeclaration())
336 return &(F
->getEntryBlock().front());
339 const Instruction
*getCtxI() const {
340 return const_cast<IRPosition
*>(this)->getCtxI();
344 /// Return the value this abstract attribute is associated with.
347 Value
&getAssociatedValue() {
348 assert(KindOrArgNo
!= IRP_INVALID
&&
349 "Invalid position does not have an associated value!");
350 if (getArgNo() < 0 || isa
<Argument
>(AnchorVal
))
352 assert(isa
<CallBase
>(AnchorVal
) && "Expected a call base!");
353 return *cast
<CallBase
>(AnchorVal
)->getArgOperand(getArgNo());
355 const Value
&getAssociatedValue() const {
356 return const_cast<IRPosition
*>(this)->getAssociatedValue();
360 /// Return the argument number of the associated value if it is an argument or
361 /// call site argument, otherwise a negative value.
362 int getArgNo() const { return KindOrArgNo
; }
364 /// Return the index in the attribute list for this position.
365 unsigned getAttrIdx() const {
366 switch (getPositionKind()) {
367 case IRPosition::IRP_INVALID
:
368 case IRPosition::IRP_FLOAT
:
370 case IRPosition::IRP_FUNCTION
:
371 case IRPosition::IRP_CALL_SITE
:
372 return AttributeList::FunctionIndex
;
373 case IRPosition::IRP_RETURNED
:
374 case IRPosition::IRP_CALL_SITE_RETURNED
:
375 return AttributeList::ReturnIndex
;
376 case IRPosition::IRP_ARGUMENT
:
377 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
378 return KindOrArgNo
+ AttributeList::FirstArgIndex
;
381 "There is no attribute index for a floating or invalid position!");
384 /// Return the associated position kind.
385 Kind
getPositionKind() const {
386 if (getArgNo() >= 0) {
387 assert(((isa
<Argument
>(getAnchorValue()) &&
388 isa
<Argument
>(getAssociatedValue())) ||
389 isa
<CallBase
>(getAnchorValue())) &&
390 "Expected argument or call base due to argument number!");
391 if (isa
<CallBase
>(getAnchorValue()))
392 return IRP_CALL_SITE_ARGUMENT
;
396 assert(KindOrArgNo
< 0 &&
397 "Expected (call site) arguments to never reach this point!");
398 assert(!isa
<Argument
>(getAnchorValue()) &&
399 "Expected arguments to have an associated argument position!");
400 return Kind(KindOrArgNo
);
403 /// TODO: Figure out if the attribute related helper functions should live
404 /// here or somewhere else.
406 /// Return true if any kind in \p AKs existing in the IR at a position that
407 /// will affect this one. See also getAttrs(...).
408 bool hasAttr(ArrayRef
<Attribute::AttrKind
> AKs
) const;
410 /// Return the attributes of any kind in \p AKs existing in the IR at a
411 /// position that will affect this one. While each position can only have a
412 /// single attribute of any kind in \p AKs, there are "subsuming" positions
413 /// that could have an attribute as well. This method returns all attributes
414 /// found in \p Attrs.
415 void getAttrs(ArrayRef
<Attribute::AttrKind
> AKs
,
416 SmallVectorImpl
<Attribute
> &Attrs
) const;
418 /// Return the attribute of kind \p AK existing in the IR at this position.
419 Attribute
getAttr(Attribute::AttrKind AK
) const {
420 if (getPositionKind() == IRP_INVALID
|| getPositionKind() == IRP_FLOAT
)
423 AttributeList AttrList
;
424 if (ImmutableCallSite ICS
= ImmutableCallSite(&getAnchorValue()))
425 AttrList
= ICS
.getAttributes();
427 AttrList
= getAssociatedFunction()->getAttributes();
429 if (AttrList
.hasAttribute(getAttrIdx(), AK
))
430 return AttrList
.getAttribute(getAttrIdx(), AK
);
434 bool isAnyCallSitePosition() const {
435 switch (getPositionKind()) {
436 case IRPosition::IRP_CALL_SITE
:
437 case IRPosition::IRP_CALL_SITE_RETURNED
:
438 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
445 /// Special DenseMap key values.
448 static const IRPosition EmptyKey
;
449 static const IRPosition TombstoneKey
;
453 /// Private constructor for special values only!
454 explicit IRPosition(int KindOrArgNo
)
455 : AnchorVal(0), KindOrArgNo(KindOrArgNo
) {}
457 /// IRPosition anchored at \p AnchorVal with kind/argument numbet \p PK.
458 explicit IRPosition(Value
&AnchorVal
, Kind PK
)
459 : AnchorVal(&AnchorVal
), KindOrArgNo(PK
) {
463 /// Verify internal invariants.
466 /// The value this position is anchored at.
469 /// The argument number, if non-negative, or the position "kind".
473 /// Helper that allows IRPosition as a key in a DenseMap.
474 template <> struct DenseMapInfo
<IRPosition
> {
475 static inline IRPosition
getEmptyKey() { return IRPosition::EmptyKey
; }
476 static inline IRPosition
getTombstoneKey() {
477 return IRPosition::TombstoneKey
;
479 static unsigned getHashValue(const IRPosition
&IRP
) {
480 return (DenseMapInfo
<Value
*>::getHashValue(&IRP
.getAnchorValue()) << 4) ^
481 (unsigned(IRP
.getArgNo()));
483 static bool isEqual(const IRPosition
&LHS
, const IRPosition
&RHS
) {
488 /// A visitor class for IR positions.
490 /// Given a position P, the SubsumingPositionIterator allows to visit "subsuming
491 /// positions" wrt. attributes/information. Thus, if a piece of information
492 /// holds for a subsuming position, it also holds for the position P.
494 /// The subsuming positions always include the initial position and then,
495 /// depending on the position kind, additionally the following ones:
496 /// - for IRP_RETURNED:
497 /// - the function (IRP_FUNCTION)
498 /// - for IRP_ARGUMENT:
499 /// - the function (IRP_FUNCTION)
500 /// - for IRP_CALL_SITE:
501 /// - the callee (IRP_FUNCTION), if known
502 /// - for IRP_CALL_SITE_RETURNED:
503 /// - the callee (IRP_RETURNED), if known
504 /// - the call site (IRP_FUNCTION)
505 /// - the callee (IRP_FUNCTION), if known
506 /// - for IRP_CALL_SITE_ARGUMENT:
507 /// - the argument of the callee (IRP_ARGUMENT), if known
508 /// - the callee (IRP_FUNCTION), if known
509 /// - the position the call site argument is associated with if it is not
510 /// anchored to the call site, e.g., if it is an arugment then the argument
512 class SubsumingPositionIterator
{
513 SmallVector
<IRPosition
, 4> IRPositions
;
514 using iterator
= decltype(IRPositions
)::iterator
;
517 SubsumingPositionIterator(const IRPosition
&IRP
);
518 iterator
begin() { return IRPositions
.begin(); }
519 iterator
end() { return IRPositions
.end(); }
522 /// Data structure to hold cached (LLVM-IR) information.
524 /// All attributes are given an InformationCache object at creation time to
525 /// avoid inspection of the IR by all of them individually. This default
526 /// InformationCache will hold information required by 'default' attributes,
527 /// thus the ones deduced when Attributor::identifyDefaultAbstractAttributes(..)
530 /// If custom abstract attributes, registered manually through
531 /// Attributor::registerAA(...), need more information, especially if it is not
532 /// reusable, it is advised to inherit from the InformationCache and cast the
533 /// instance down in the abstract attributes.
534 struct InformationCache
{
535 InformationCache(const DataLayout
&DL
) : DL(DL
) {}
537 /// A map type from opcodes to instructions with this opcode.
538 using OpcodeInstMapTy
= DenseMap
<unsigned, SmallVector
<Instruction
*, 32>>;
540 /// Return the map that relates "interesting" opcodes with all instructions
541 /// with that opcode in \p F.
542 OpcodeInstMapTy
&getOpcodeInstMapForFunction(const Function
&F
) {
543 return FuncInstOpcodeMap
[&F
];
546 /// A vector type to hold instructions.
547 using InstructionVectorTy
= std::vector
<Instruction
*>;
549 /// Return the instructions in \p F that may read or write memory.
550 InstructionVectorTy
&getReadOrWriteInstsForFunction(const Function
&F
) {
551 return FuncRWInstsMap
[&F
];
554 /// Return TargetLibraryInfo for function \p F.
555 TargetLibraryInfo
*getTargetLibraryInfoForFunction(const Function
&F
) {
556 return FuncTLIMap
[&F
];
559 /// Return datalayout used in the module.
560 const DataLayout
&getDL() { return DL
; }
563 /// A map type from functions to opcode to instruction maps.
564 using FuncInstOpcodeMapTy
= DenseMap
<const Function
*, OpcodeInstMapTy
>;
566 /// A map type from functions to their read or write instructions.
567 using FuncRWInstsMapTy
= DenseMap
<const Function
*, InstructionVectorTy
>;
569 /// A map type from functions to their TLI.
570 using FuncTLIMapTy
= DenseMap
<const Function
*, TargetLibraryInfo
*>;
572 /// A nested map that remembers all instructions in a function with a certain
573 /// instruction opcode (Instruction::getOpcode()).
574 FuncInstOpcodeMapTy FuncInstOpcodeMap
;
576 /// A map from functions to their instructions that may read or write memory.
577 FuncRWInstsMapTy FuncRWInstsMap
;
579 /// A map from functions to their TLI.
580 FuncTLIMapTy FuncTLIMap
;
582 /// The datalayout used in the module.
583 const DataLayout
&DL
;
585 /// Give the Attributor access to the members so
586 /// Attributor::identifyDefaultAbstractAttributes(...) can initialize them.
587 friend struct Attributor
;
590 /// The fixpoint analysis framework that orchestrates the attribute deduction.
592 /// The Attributor provides a general abstract analysis framework (guided
593 /// fixpoint iteration) as well as helper functions for the deduction of
594 /// (LLVM-IR) attributes. However, also other code properties can be deduced,
595 /// propagated, and ultimately manifested through the Attributor framework. This
596 /// is particularly useful if these properties interact with attributes and a
597 /// co-scheduled deduction allows to improve the solution. Even if not, thus if
598 /// attributes/properties are completely isolated, they should use the
599 /// Attributor framework to reduce the number of fixpoint iteration frameworks
600 /// in the code base. Note that the Attributor design makes sure that isolated
601 /// attributes are not impacted, in any way, by others derived at the same time
602 /// if there is no cross-reasoning performed.
604 /// The public facing interface of the Attributor is kept simple and basically
605 /// allows abstract attributes to one thing, query abstract attributes
606 /// in-flight. There are two reasons to do this:
607 /// a) The optimistic state of one abstract attribute can justify an
608 /// optimistic state of another, allowing to framework to end up with an
609 /// optimistic (=best possible) fixpoint instead of one based solely on
610 /// information in the IR.
611 /// b) This avoids reimplementing various kinds of lookups, e.g., to check
612 /// for existing IR attributes, in favor of a single lookups interface
613 /// provided by an abstract attribute subclass.
615 /// NOTE: The mechanics of adding a new "concrete" abstract attribute are
616 /// described in the file comment.
620 /// \param InfoCache Cache to hold various information accessible for
621 /// the abstract attributes.
622 /// \param DepRecomputeInterval Number of iterations until the dependences
623 /// between abstract attributes are recomputed.
624 /// \param Whitelist If not null, a set limiting the attribute opportunities.
625 Attributor(InformationCache
&InfoCache
, unsigned DepRecomputeInterval
,
626 DenseSet
<const char *> *Whitelist
= nullptr)
627 : InfoCache(InfoCache
), DepRecomputeInterval(DepRecomputeInterval
),
628 Whitelist(Whitelist
) {}
630 ~Attributor() { DeleteContainerPointers(AllAbstractAttributes
); }
632 /// Run the analyses until a fixpoint is reached or enforced (timeout).
634 /// The attributes registered with this Attributor can be used after as long
635 /// as the Attributor is not destroyed (it owns the attributes now).
637 /// \Returns CHANGED if the IR was changed, otherwise UNCHANGED.
638 ChangeStatus
run(Module
&M
);
640 /// Lookup an abstract attribute of type \p AAType at position \p IRP. While
641 /// no abstract attribute is found equivalent positions are checked, see
642 /// SubsumingPositionIterator. Thus, the returned abstract attribute
643 /// might be anchored at a different position, e.g., the callee if \p IRP is a
646 /// This method is the only (supported) way an abstract attribute can retrieve
647 /// information from another abstract attribute. As an example, take an
648 /// abstract attribute that determines the memory access behavior for a
649 /// argument (readnone, readonly, ...). It should use `getAAFor` to get the
650 /// most optimistic information for other abstract attributes in-flight, e.g.
651 /// the one reasoning about the "captured" state for the argument or the one
652 /// reasoning on the memory access behavior of the function as a whole.
654 /// If the flag \p TrackDependence is set to false the dependence from
655 /// \p QueryingAA to the return abstract attribute is not automatically
656 /// recorded. This should only be used if the caller will record the
657 /// dependence explicitly if necessary, thus if it the returned abstract
658 /// attribute is used for reasoning. To record the dependences explicitly use
659 /// the `Attributor::recordDependence` method.
660 template <typename AAType
>
661 const AAType
&getAAFor(const AbstractAttribute
&QueryingAA
,
662 const IRPosition
&IRP
, bool TrackDependence
= true) {
663 return getOrCreateAAFor
<AAType
>(IRP
, &QueryingAA
, TrackDependence
);
666 /// Explicitly record a dependence from \p FromAA to \p ToAA, that is if
667 /// \p FromAA changes \p ToAA should be updated as well.
669 /// This method should be used in conjunction with the `getAAFor` method and
670 /// with the TrackDependence flag passed to the method set to false. This can
671 /// be beneficial to avoid false dependences but it requires the users of
672 /// `getAAFor` to explicitly record true dependences through this method.
673 void recordDependence(const AbstractAttribute
&FromAA
,
674 const AbstractAttribute
&ToAA
) {
675 QueryMap
[&FromAA
].insert(const_cast<AbstractAttribute
*>(&ToAA
));
678 /// Introduce a new abstract attribute into the fixpoint analysis.
680 /// Note that ownership of the attribute is given to the Attributor. It will
681 /// invoke delete for the Attributor on destruction of the Attributor.
683 /// Attributes are identified by their IR position (AAType::getIRPosition())
684 /// and the address of their static member (see AAType::ID).
685 template <typename AAType
> AAType
®isterAA(AAType
&AA
) {
686 static_assert(std::is_base_of
<AbstractAttribute
, AAType
>::value
,
687 "Cannot register an attribute with a type not derived from "
688 "'AbstractAttribute'!");
689 // Put the attribute in the lookup map structure and the container we use to
690 // keep track of all attributes.
691 IRPosition
&IRP
= AA
.getIRPosition();
692 auto &KindToAbstractAttributeMap
= AAMap
[IRP
];
693 assert(!KindToAbstractAttributeMap
.count(&AAType::ID
) &&
694 "Attribute already in map!");
695 KindToAbstractAttributeMap
[&AAType::ID
] = &AA
;
696 AllAbstractAttributes
.push_back(&AA
);
700 /// Return the internal information cache.
701 InformationCache
&getInfoCache() { return InfoCache
; }
703 /// Determine opportunities to derive 'default' attributes in \p F and create
704 /// abstract attribute objects for them.
706 /// \param F The function that is checked for attribute opportunities.
707 /// \param TLIGetter helper function to get TargetLibraryInfo Analysis result.
709 /// Note that abstract attribute instances are generally created even if the
710 /// IR already contains the information they would deduce. The most important
711 /// reason for this is the single interface, the one of the abstract attribute
712 /// instance, which can be queried without the need to look at the IR in
714 void identifyDefaultAbstractAttributes(
715 Function
&F
, std::function
<TargetLibraryInfo
*(Function
&)> &TLIGetter
);
717 /// Mark the internal function \p F as live.
719 /// This will trigger the identification and initialization of attributes for
721 void markLiveInternalFunction(const Function
&F
) {
722 assert(F
.hasInternalLinkage() &&
723 "Only internal linkage is assumed dead initially.");
725 std::function
<TargetLibraryInfo
*(Function
&)> TLIGetter
=
726 [&](Function
&F
) -> TargetLibraryInfo
* { return nullptr; };
728 identifyDefaultAbstractAttributes(const_cast<Function
&>(F
), TLIGetter
);
731 /// Record that \p I is deleted after information was manifested.
732 void deleteAfterManifest(Instruction
&I
) { ToBeDeletedInsts
.insert(&I
); }
734 /// Record that \p BB is deleted after information was manifested.
735 void deleteAfterManifest(BasicBlock
&BB
) { ToBeDeletedBlocks
.insert(&BB
); }
737 /// Record that \p F is deleted after information was manifested.
738 void deleteAfterManifest(Function
&F
) { ToBeDeletedFunctions
.insert(&F
); }
740 /// Return true if \p AA (or its context instruction) is assumed dead.
742 /// If \p LivenessAA is not provided it is queried.
743 bool isAssumedDead(const AbstractAttribute
&AA
, const AAIsDead
*LivenessAA
);
745 /// Check \p Pred on all function call sites.
747 /// This method will evaluate \p Pred on call sites and return
748 /// true if \p Pred holds in every call sites. However, this is only possible
749 /// all call sites are known, hence the function has internal linkage.
750 bool checkForAllCallSites(const function_ref
<bool(CallSite
)> &Pred
,
751 const AbstractAttribute
&QueryingAA
,
752 bool RequireAllCallSites
);
754 /// Check \p Pred on all values potentially returned by \p F.
756 /// This method will evaluate \p Pred on all values potentially returned by
757 /// the function associated with \p QueryingAA. The returned values are
758 /// matched with their respective return instructions. Returns true if \p Pred
759 /// holds on all of them.
760 bool checkForAllReturnedValuesAndReturnInsts(
761 const function_ref
<bool(Value
&, const SmallSetVector
<ReturnInst
*, 4> &)>
763 const AbstractAttribute
&QueryingAA
);
765 /// Check \p Pred on all values potentially returned by the function
766 /// associated with \p QueryingAA.
768 /// This is the context insensitive version of the method above.
769 bool checkForAllReturnedValues(const function_ref
<bool(Value
&)> &Pred
,
770 const AbstractAttribute
&QueryingAA
);
772 /// Check \p Pred on all instructions with an opcode present in \p Opcodes.
774 /// This method will evaluate \p Pred on all instructions with an opcode
775 /// present in \p Opcode and return true if \p Pred holds on all of them.
776 bool checkForAllInstructions(const function_ref
<bool(Instruction
&)> &Pred
,
777 const AbstractAttribute
&QueryingAA
,
778 const ArrayRef
<unsigned> &Opcodes
);
780 /// Check \p Pred on all call-like instructions (=CallBased derived).
782 /// See checkForAllCallLikeInstructions(...) for more information.
784 checkForAllCallLikeInstructions(const function_ref
<bool(Instruction
&)> &Pred
,
785 const AbstractAttribute
&QueryingAA
) {
786 return checkForAllInstructions(Pred
, QueryingAA
,
787 {(unsigned)Instruction::Invoke
,
788 (unsigned)Instruction::CallBr
,
789 (unsigned)Instruction::Call
});
792 /// Check \p Pred on all Read/Write instructions.
794 /// This method will evaluate \p Pred on all instructions that read or write
795 /// to memory present in the information cache and return true if \p Pred
796 /// holds on all of them.
797 bool checkForAllReadWriteInstructions(
798 const llvm::function_ref
<bool(Instruction
&)> &Pred
,
799 AbstractAttribute
&QueryingAA
);
801 /// Return the data layout associated with the anchor scope.
802 const DataLayout
&getDataLayout() const { return InfoCache
.DL
; }
806 /// The private version of getAAFor that allows to omit a querying abstract
807 /// attribute. See also the public getAAFor method.
808 template <typename AAType
>
809 const AAType
&getOrCreateAAFor(const IRPosition
&IRP
,
810 const AbstractAttribute
*QueryingAA
= nullptr,
811 bool TrackDependence
= false) {
812 if (const AAType
*AAPtr
=
813 lookupAAFor
<AAType
>(IRP
, QueryingAA
, TrackDependence
))
816 // No matching attribute found, create one.
817 // Use the static create method.
818 auto &AA
= AAType::createForPosition(IRP
, *this);
820 AA
.initialize(*this);
822 // Bootstrap the new attribute with an initial update to propagate
823 // information, e.g., function -> call site. If it is not on a given
824 // whitelist we will not perform updates at all.
825 if (Whitelist
&& !Whitelist
->count(&AAType::ID
))
826 AA
.getState().indicatePessimisticFixpoint();
830 if (TrackDependence
&& AA
.getState().isValidState())
831 QueryMap
[&AA
].insert(const_cast<AbstractAttribute
*>(QueryingAA
));
835 /// Return the attribute of \p AAType for \p IRP if existing.
836 template <typename AAType
>
837 const AAType
*lookupAAFor(const IRPosition
&IRP
,
838 const AbstractAttribute
*QueryingAA
= nullptr,
839 bool TrackDependence
= false) {
840 static_assert(std::is_base_of
<AbstractAttribute
, AAType
>::value
,
841 "Cannot query an attribute with a type not derived from "
842 "'AbstractAttribute'!");
843 assert((QueryingAA
|| !TrackDependence
) &&
844 "Cannot track dependences without a QueryingAA!");
846 // Lookup the abstract attribute of type AAType. If found, return it after
847 // registering a dependence of QueryingAA on the one returned attribute.
848 const auto &KindToAbstractAttributeMap
= AAMap
.lookup(IRP
);
849 if (AAType
*AA
= static_cast<AAType
*>(
850 KindToAbstractAttributeMap
.lookup(&AAType::ID
))) {
851 // Do not register a dependence on an attribute with an invalid state.
852 if (TrackDependence
&& AA
->getState().isValidState())
853 QueryMap
[AA
].insert(const_cast<AbstractAttribute
*>(QueryingAA
));
859 /// The set of all abstract attributes.
861 using AAVector
= SmallVector
<AbstractAttribute
*, 64>;
862 AAVector AllAbstractAttributes
;
865 /// A nested map to lookup abstract attributes based on the argument position
866 /// on the outer level, and the addresses of the static member (AAType::ID) on
869 using KindToAbstractAttributeMap
=
870 DenseMap
<const char *, AbstractAttribute
*>;
871 DenseMap
<IRPosition
, KindToAbstractAttributeMap
> AAMap
;
874 /// A map from abstract attributes to the ones that queried them through calls
875 /// to the getAAFor<...>(...) method.
878 MapVector
<const AbstractAttribute
*, SetVector
<AbstractAttribute
*>>;
882 /// The information cache that holds pre-processed (LLVM-IR) information.
883 InformationCache
&InfoCache
;
885 /// Number of iterations until the dependences between abstract attributes are
887 const unsigned DepRecomputeInterval
;
889 /// If not null, a set limiting the attribute opportunities.
890 const DenseSet
<const char *> *Whitelist
;
892 /// A set to remember the functions we already assume to be live and visited.
893 DenseSet
<const Function
*> VisitedFunctions
;
895 /// Functions, blocks, and instructions we delete after manifest is done.
898 SmallPtrSet
<Function
*, 8> ToBeDeletedFunctions
;
899 SmallPtrSet
<BasicBlock
*, 8> ToBeDeletedBlocks
;
900 SmallPtrSet
<Instruction
*, 8> ToBeDeletedInsts
;
904 /// An interface to query the internal state of an abstract attribute.
906 /// The abstract state is a minimal interface that allows the Attributor to
907 /// communicate with the abstract attributes about their internal state without
908 /// enforcing or exposing implementation details, e.g., the (existence of an)
909 /// underlying lattice.
911 /// It is sufficient to be able to query if a state is (1) valid or invalid, (2)
912 /// at a fixpoint, and to indicate to the state that (3) an optimistic fixpoint
913 /// was reached or (4) a pessimistic fixpoint was enforced.
915 /// All methods need to be implemented by the subclass. For the common use case,
916 /// a single boolean state or a bit-encoded state, the BooleanState and
917 /// IntegerState classes are already provided. An abstract attribute can inherit
918 /// from them to get the abstract state interface and additional methods to
919 /// directly modify the state based if needed. See the class comments for help.
920 struct AbstractState
{
921 virtual ~AbstractState() {}
923 /// Return if this abstract state is in a valid state. If false, no
924 /// information provided should be used.
925 virtual bool isValidState() const = 0;
927 /// Return if this abstract state is fixed, thus does not need to be updated
928 /// if information changes as it cannot change itself.
929 virtual bool isAtFixpoint() const = 0;
931 /// Indicate that the abstract state should converge to the optimistic state.
933 /// This will usually make the optimistically assumed state the known to be
936 /// \returns ChangeStatus::UNCHANGED as the assumed value should not change.
937 virtual ChangeStatus
indicateOptimisticFixpoint() = 0;
939 /// Indicate that the abstract state should converge to the pessimistic state.
941 /// This will usually revert the optimistically assumed state to the known to
944 /// \returns ChangeStatus::CHANGED as the assumed value may change.
945 virtual ChangeStatus
indicatePessimisticFixpoint() = 0;
948 /// Simple state with integers encoding.
950 /// The interface ensures that the assumed bits are always a subset of the known
951 /// bits. Users can only add known bits and, except through adding known bits,
952 /// they can only remove assumed bits. This should guarantee monotoniticy and
953 /// thereby the existence of a fixpoint (if used corretly). The fixpoint is
954 /// reached when the assumed and known state/bits are equal. Users can
955 /// force/inidicate a fixpoint. If an optimistic one is indicated, the known
956 /// state will catch up with the assumed one, for a pessimistic fixpoint it is
957 /// the other way around.
958 struct IntegerState
: public AbstractState
{
959 /// Underlying integer type, we assume 32 bits to be enough.
960 using base_t
= uint32_t;
962 /// Initialize the (best) state.
963 IntegerState(base_t BestState
= ~0) : Assumed(BestState
) {}
965 /// Return the worst possible representable state.
966 static constexpr base_t
getWorstState() { return 0; }
968 /// See AbstractState::isValidState()
969 /// NOTE: For now we simply pretend that the worst possible state is invalid.
970 bool isValidState() const override
{ return Assumed
!= getWorstState(); }
972 /// See AbstractState::isAtFixpoint()
973 bool isAtFixpoint() const override
{ return Assumed
== Known
; }
975 /// See AbstractState::indicateOptimisticFixpoint(...)
976 ChangeStatus
indicateOptimisticFixpoint() override
{
978 return ChangeStatus::UNCHANGED
;
981 /// See AbstractState::indicatePessimisticFixpoint(...)
982 ChangeStatus
indicatePessimisticFixpoint() override
{
984 return ChangeStatus::CHANGED
;
987 /// Return the known state encoding
988 base_t
getKnown() const { return Known
; }
990 /// Return the assumed state encoding.
991 base_t
getAssumed() const { return Assumed
; }
993 /// Return true if the bits set in \p BitsEncoding are "known bits".
994 bool isKnown(base_t BitsEncoding
) const {
995 return (Known
& BitsEncoding
) == BitsEncoding
;
998 /// Return true if the bits set in \p BitsEncoding are "assumed bits".
999 bool isAssumed(base_t BitsEncoding
) const {
1000 return (Assumed
& BitsEncoding
) == BitsEncoding
;
1003 /// Add the bits in \p BitsEncoding to the "known bits".
1004 IntegerState
&addKnownBits(base_t Bits
) {
1005 // Make sure we never miss any "known bits".
1011 /// Remove the bits in \p BitsEncoding from the "assumed bits" if not known.
1012 IntegerState
&removeAssumedBits(base_t BitsEncoding
) {
1013 // Make sure we never loose any "known bits".
1014 Assumed
= (Assumed
& ~BitsEncoding
) | Known
;
1018 /// Keep only "assumed bits" also set in \p BitsEncoding but all known ones.
1019 IntegerState
&intersectAssumedBits(base_t BitsEncoding
) {
1020 // Make sure we never loose any "known bits".
1021 Assumed
= (Assumed
& BitsEncoding
) | Known
;
1025 /// Take minimum of assumed and \p Value.
1026 IntegerState
&takeAssumedMinimum(base_t Value
) {
1027 // Make sure we never loose "known value".
1028 Assumed
= std::max(std::min(Assumed
, Value
), Known
);
1032 /// Take maximum of known and \p Value.
1033 IntegerState
&takeKnownMaximum(base_t Value
) {
1034 // Make sure we never loose "known value".
1035 Assumed
= std::max(Value
, Assumed
);
1036 Known
= std::max(Value
, Known
);
1040 /// Equality for IntegerState.
1041 bool operator==(const IntegerState
&R
) const {
1042 return this->getAssumed() == R
.getAssumed() &&
1043 this->getKnown() == R
.getKnown();
1046 /// Inequality for IntegerState.
1047 bool operator!=(const IntegerState
&R
) const { return !(*this == R
); }
1049 /// "Clamp" this state with \p R. The result is the minimum of the assumed
1050 /// information but not less than what was known before.
1052 /// TODO: Consider replacing the operator with a call or using it only when
1053 /// we can also take the maximum of the known information, thus when
1054 /// \p R is not dependent on additional assumed state.
1055 IntegerState
operator^=(const IntegerState
&R
) {
1056 takeAssumedMinimum(R
.Assumed
);
1060 /// "Clamp" this state with \p R. The result is the maximum of the known
1061 /// information but not more than what was assumed before.
1062 IntegerState
operator+=(const IntegerState
&R
) {
1063 takeKnownMaximum(R
.Known
);
1067 /// Make this the minimum, known and assumed, of this state and \p R.
1068 IntegerState
operator&=(const IntegerState
&R
) {
1069 Known
= std::min(Known
, R
.Known
);
1070 Assumed
= std::min(Assumed
, R
.Assumed
);
1074 /// Make this the maximum, known and assumed, of this state and \p R.
1075 IntegerState
operator|=(const IntegerState
&R
) {
1076 Known
= std::max(Known
, R
.Known
);
1077 Assumed
= std::max(Assumed
, R
.Assumed
);
1082 /// The known state encoding in an integer of type base_t.
1083 base_t Known
= getWorstState();
1085 /// The assumed state encoding in an integer of type base_t.
1089 /// Simple wrapper for a single bit (boolean) state.
1090 struct BooleanState
: public IntegerState
{
1091 BooleanState() : IntegerState(1){};
1094 /// Helper struct necessary as the modular build fails if the virtual method
1095 /// IRAttribute::manifest is defined in the Attributor.cpp.
1096 struct IRAttributeManifest
{
1097 static ChangeStatus
manifestAttrs(Attributor
&A
, IRPosition
&IRP
,
1098 const ArrayRef
<Attribute
> &DeducedAttrs
);
1101 /// Helper to tie a abstract state implementation to an abstract attribute.
1102 template <typename StateTy
, typename Base
>
1103 struct StateWrapper
: public StateTy
, public Base
{
1104 /// Provide static access to the type of the state.
1105 using StateType
= StateTy
;
1107 /// See AbstractAttribute::getState(...).
1108 StateType
&getState() override
{ return *this; }
1110 /// See AbstractAttribute::getState(...).
1111 const AbstractState
&getState() const override
{ return *this; }
1114 /// Helper class that provides common functionality to manifest IR attributes.
1115 template <Attribute::AttrKind AK
, typename Base
>
1116 struct IRAttribute
: public IRPosition
, public Base
{
1117 IRAttribute(const IRPosition
&IRP
) : IRPosition(IRP
) {}
1120 /// See AbstractAttribute::initialize(...).
1121 virtual void initialize(Attributor
&A
) override
{
1122 if (hasAttr(getAttrKind())) {
1123 this->getState().indicateOptimisticFixpoint();
1127 const IRPosition
&IRP
= this->getIRPosition();
1128 bool IsFnInterface
= IRP
.isFnInterfaceKind();
1129 const Function
*FnScope
= IRP
.getAnchorScope();
1130 // TODO: Not all attributes require an exact definition. Find a way to
1131 // enable deduction for some but not all attributes in case the
1132 // definition might be changed at runtime, see also
1133 // http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html.
1134 // TODO: We could always determine abstract attributes and if sufficient
1135 // information was found we could duplicate the functions that do not
1136 // have an exact definition.
1137 if (IsFnInterface
&& (!FnScope
|| !FnScope
->hasExactDefinition()))
1138 this->getState().indicatePessimisticFixpoint();
1141 /// See AbstractAttribute::manifest(...).
1142 ChangeStatus
manifest(Attributor
&A
) override
{
1143 SmallVector
<Attribute
, 4> DeducedAttrs
;
1144 getDeducedAttributes(getAnchorValue().getContext(), DeducedAttrs
);
1145 return IRAttributeManifest::manifestAttrs(A
, getIRPosition(), DeducedAttrs
);
1148 /// Return the kind that identifies the abstract attribute implementation.
1149 Attribute::AttrKind
getAttrKind() const { return AK
; }
1151 /// Return the deduced attributes in \p Attrs.
1152 virtual void getDeducedAttributes(LLVMContext
&Ctx
,
1153 SmallVectorImpl
<Attribute
> &Attrs
) const {
1154 Attrs
.emplace_back(Attribute::get(Ctx
, getAttrKind()));
1157 /// Return an IR position, see struct IRPosition.
1160 IRPosition
&getIRPosition() override
{ return *this; }
1161 const IRPosition
&getIRPosition() const override
{ return *this; }
1165 /// Base struct for all "concrete attribute" deductions.
1167 /// The abstract attribute is a minimal interface that allows the Attributor to
1168 /// orchestrate the abstract/fixpoint analysis. The design allows to hide away
1169 /// implementation choices made for the subclasses but also to structure their
1170 /// implementation and simplify the use of other abstract attributes in-flight.
1172 /// To allow easy creation of new attributes, most methods have default
1173 /// implementations. The ones that do not are generally straight forward, except
1174 /// `AbstractAttribute::updateImpl` which is the location of most reasoning
1175 /// associated with the abstract attribute. The update is invoked by the
1176 /// Attributor in case the situation used to justify the current optimistic
1177 /// state might have changed. The Attributor determines this automatically
1178 /// by monitoring the `Attributor::getAAFor` calls made by abstract attributes.
1180 /// The `updateImpl` method should inspect the IR and other abstract attributes
1181 /// in-flight to justify the best possible (=optimistic) state. The actual
1182 /// implementation is, similar to the underlying abstract state encoding, not
1183 /// exposed. In the most common case, the `updateImpl` will go through a list of
1184 /// reasons why its optimistic state is valid given the current information. If
1185 /// any combination of them holds and is sufficient to justify the current
1186 /// optimistic state, the method shall return UNCHAGED. If not, the optimistic
1187 /// state is adjusted to the situation and the method shall return CHANGED.
1189 /// If the manifestation of the "concrete attribute" deduced by the subclass
1190 /// differs from the "default" behavior, which is a (set of) LLVM-IR
1191 /// attribute(s) for an argument, call site argument, function return value, or
1192 /// function, the `AbstractAttribute::manifest` method should be overloaded.
1194 /// NOTE: If the state obtained via getState() is INVALID, thus if
1195 /// AbstractAttribute::getState().isValidState() returns false, no
1196 /// information provided by the methods of this class should be used.
1197 /// NOTE: The Attributor currently has certain limitations to what we can do.
1198 /// As a general rule of thumb, "concrete" abstract attributes should *for
1199 /// now* only perform "backward" information propagation. That means
1200 /// optimistic information obtained through abstract attributes should
1201 /// only be used at positions that precede the origin of the information
1202 /// with regards to the program flow. More practically, information can
1203 /// *now* be propagated from instructions to their enclosing function, but
1204 /// *not* from call sites to the called function. The mechanisms to allow
1205 /// both directions will be added in the future.
1206 /// NOTE: The mechanics of adding a new "concrete" abstract attribute are
1207 /// described in the file comment.
1208 struct AbstractAttribute
{
1209 using StateType
= AbstractState
;
1211 /// Virtual destructor.
1212 virtual ~AbstractAttribute() {}
1214 /// Initialize the state with the information in the Attributor \p A.
1216 /// This function is called by the Attributor once all abstract attributes
1217 /// have been identified. It can and shall be used for task like:
1218 /// - identify existing knowledge in the IR and use it for the "known state"
1219 /// - perform any work that is not going to change over time, e.g., determine
1220 /// a subset of the IR, or attributes in-flight, that have to be looked at
1221 /// in the `updateImpl` method.
1222 virtual void initialize(Attributor
&A
) {}
1224 /// Return the internal abstract state for inspection.
1225 virtual StateType
&getState() = 0;
1226 virtual const StateType
&getState() const = 0;
1228 /// Return an IR position, see struct IRPosition.
1229 virtual const IRPosition
&getIRPosition() const = 0;
1231 /// Helper functions, for debug purposes only.
1233 virtual void print(raw_ostream
&OS
) const;
1234 void dump() const { print(dbgs()); }
1236 /// This function should return the "summarized" assumed state as string.
1237 virtual const std::string
getAsStr() const = 0;
1240 /// Allow the Attributor access to the protected methods.
1241 friend struct Attributor
;
1244 /// Hook for the Attributor to trigger an update of the internal state.
1246 /// If this attribute is already fixed, this method will return UNCHANGED,
1247 /// otherwise it delegates to `AbstractAttribute::updateImpl`.
1249 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
1250 ChangeStatus
update(Attributor
&A
);
1252 /// Hook for the Attributor to trigger the manifestation of the information
1253 /// represented by the abstract attribute in the LLVM-IR.
1255 /// \Return CHANGED if the IR was altered, otherwise UNCHANGED.
1256 virtual ChangeStatus
manifest(Attributor
&A
) {
1257 return ChangeStatus::UNCHANGED
;
1260 /// Hook to enable custom statistic tracking, called after manifest that
1261 /// resulted in a change if statistics are enabled.
1263 /// We require subclasses to provide an implementation so we remember to
1264 /// add statistics for them.
1265 virtual void trackStatistics() const = 0;
1267 /// Return an IR position, see struct IRPosition.
1268 virtual IRPosition
&getIRPosition() = 0;
1270 /// The actual update/transfer function which has to be implemented by the
1271 /// derived classes.
1273 /// If it is called, the environment has changed and we have to determine if
1274 /// the current information is still valid or adjust it otherwise.
1276 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
1277 virtual ChangeStatus
updateImpl(Attributor
&A
) = 0;
1280 /// Forward declarations of output streams for debug purposes.
1283 raw_ostream
&operator<<(raw_ostream
&OS
, const AbstractAttribute
&AA
);
1284 raw_ostream
&operator<<(raw_ostream
&OS
, ChangeStatus S
);
1285 raw_ostream
&operator<<(raw_ostream
&OS
, IRPosition::Kind
);
1286 raw_ostream
&operator<<(raw_ostream
&OS
, const IRPosition
&);
1287 raw_ostream
&operator<<(raw_ostream
&OS
, const AbstractState
&State
);
1288 raw_ostream
&operator<<(raw_ostream
&OS
, const IntegerState
&S
);
1291 struct AttributorPass
: public PassInfoMixin
<AttributorPass
> {
1292 PreservedAnalyses
run(Module
&M
, ModuleAnalysisManager
&AM
);
1295 Pass
*createAttributorLegacyPass();
1297 /// ----------------------------------------------------------------------------
1298 /// Abstract Attribute Classes
1299 /// ----------------------------------------------------------------------------
1301 /// An abstract attribute for the returned values of a function.
1302 struct AAReturnedValues
1303 : public IRAttribute
<Attribute::Returned
, AbstractAttribute
> {
1304 AAReturnedValues(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1306 /// Return an assumed unique return value if a single candidate is found. If
1307 /// there cannot be one, return a nullptr. If it is not clear yet, return the
1308 /// Optional::NoneType.
1309 Optional
<Value
*> getAssumedUniqueReturnValue(Attributor
&A
) const;
1311 /// Check \p Pred on all returned values.
1313 /// This method will evaluate \p Pred on returned values and return
1314 /// true if (1) all returned values are known, and (2) \p Pred returned true
1315 /// for all returned values.
1317 /// Note: Unlike the Attributor::checkForAllReturnedValuesAndReturnInsts
1318 /// method, this one will not filter dead return instructions.
1319 virtual bool checkForAllReturnedValuesAndReturnInsts(
1320 const function_ref
<bool(Value
&, const SmallSetVector
<ReturnInst
*, 4> &)>
1323 using iterator
= MapVector
<Value
*, SmallSetVector
<ReturnInst
*, 4>>::iterator
;
1324 using const_iterator
=
1325 MapVector
<Value
*, SmallSetVector
<ReturnInst
*, 4>>::const_iterator
;
1326 virtual llvm::iterator_range
<iterator
> returned_values() = 0;
1327 virtual llvm::iterator_range
<const_iterator
> returned_values() const = 0;
1329 virtual size_t getNumReturnValues() const = 0;
1330 virtual const SmallSetVector
<CallBase
*, 4> &getUnresolvedCalls() const = 0;
1332 /// Create an abstract attribute view for the position \p IRP.
1333 static AAReturnedValues
&createForPosition(const IRPosition
&IRP
,
1336 /// Unique ID (due to the unique address)
1337 static const char ID
;
1341 : public IRAttribute
<Attribute::NoUnwind
,
1342 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1343 AANoUnwind(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1345 /// Returns true if nounwind is assumed.
1346 bool isAssumedNoUnwind() const { return getAssumed(); }
1348 /// Returns true if nounwind is known.
1349 bool isKnownNoUnwind() const { return getKnown(); }
1351 /// Create an abstract attribute view for the position \p IRP.
1352 static AANoUnwind
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1354 /// Unique ID (due to the unique address)
1355 static const char ID
;
1359 : public IRAttribute
<Attribute::NoSync
,
1360 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1361 AANoSync(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1363 /// Returns true if "nosync" is assumed.
1364 bool isAssumedNoSync() const { return getAssumed(); }
1366 /// Returns true if "nosync" is known.
1367 bool isKnownNoSync() const { return getKnown(); }
1369 /// Create an abstract attribute view for the position \p IRP.
1370 static AANoSync
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1372 /// Unique ID (due to the unique address)
1373 static const char ID
;
1376 /// An abstract interface for all nonnull attributes.
1378 : public IRAttribute
<Attribute::NonNull
,
1379 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1380 AANonNull(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1382 /// Return true if we assume that the underlying value is nonnull.
1383 bool isAssumedNonNull() const { return getAssumed(); }
1385 /// Return true if we know that underlying value is nonnull.
1386 bool isKnownNonNull() const { return getKnown(); }
1388 /// Create an abstract attribute view for the position \p IRP.
1389 static AANonNull
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1391 /// Unique ID (due to the unique address)
1392 static const char ID
;
1395 /// An abstract attribute for norecurse.
1397 : public IRAttribute
<Attribute::NoRecurse
,
1398 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1399 AANoRecurse(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1401 /// Return true if "norecurse" is assumed.
1402 bool isAssumedNoRecurse() const { return getAssumed(); }
1404 /// Return true if "norecurse" is known.
1405 bool isKnownNoRecurse() const { return getKnown(); }
1407 /// Create an abstract attribute view for the position \p IRP.
1408 static AANoRecurse
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1410 /// Unique ID (due to the unique address)
1411 static const char ID
;
1414 /// An abstract attribute for willreturn.
1416 : public IRAttribute
<Attribute::WillReturn
,
1417 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1418 AAWillReturn(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1420 /// Return true if "willreturn" is assumed.
1421 bool isAssumedWillReturn() const { return getAssumed(); }
1423 /// Return true if "willreturn" is known.
1424 bool isKnownWillReturn() const { return getKnown(); }
1426 /// Create an abstract attribute view for the position \p IRP.
1427 static AAWillReturn
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1429 /// Unique ID (due to the unique address)
1430 static const char ID
;
1433 /// An abstract interface for all noalias attributes.
1435 : public IRAttribute
<Attribute::NoAlias
,
1436 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1437 AANoAlias(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1439 /// Return true if we assume that the underlying value is alias.
1440 bool isAssumedNoAlias() const { return getAssumed(); }
1442 /// Return true if we know that underlying value is noalias.
1443 bool isKnownNoAlias() const { return getKnown(); }
1445 /// Create an abstract attribute view for the position \p IRP.
1446 static AANoAlias
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1448 /// Unique ID (due to the unique address)
1449 static const char ID
;
1452 /// An AbstractAttribute for nofree.
1454 : public IRAttribute
<Attribute::NoFree
,
1455 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1456 AANoFree(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1458 /// Return true if "nofree" is assumed.
1459 bool isAssumedNoFree() const { return getAssumed(); }
1461 /// Return true if "nofree" is known.
1462 bool isKnownNoFree() const { return getKnown(); }
1464 /// Create an abstract attribute view for the position \p IRP.
1465 static AANoFree
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1467 /// Unique ID (due to the unique address)
1468 static const char ID
;
1471 /// An AbstractAttribute for noreturn.
1473 : public IRAttribute
<Attribute::NoReturn
,
1474 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1475 AANoReturn(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1477 /// Return true if the underlying object is assumed to never return.
1478 bool isAssumedNoReturn() const { return getAssumed(); }
1480 /// Return true if the underlying object is known to never return.
1481 bool isKnownNoReturn() const { return getKnown(); }
1483 /// Create an abstract attribute view for the position \p IRP.
1484 static AANoReturn
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1486 /// Unique ID (due to the unique address)
1487 static const char ID
;
1490 /// An abstract interface for liveness abstract attribute.
1491 struct AAIsDead
: public StateWrapper
<BooleanState
, AbstractAttribute
>,
1493 AAIsDead(const IRPosition
&IRP
) : IRPosition(IRP
) {}
1495 /// Returns true if \p BB is assumed dead.
1496 virtual bool isAssumedDead(const BasicBlock
*BB
) const = 0;
1498 /// Returns true if \p BB is known dead.
1499 virtual bool isKnownDead(const BasicBlock
*BB
) const = 0;
1501 /// Returns true if \p I is assumed dead.
1502 virtual bool isAssumedDead(const Instruction
*I
) const = 0;
1504 /// Returns true if \p I is known dead.
1505 virtual bool isKnownDead(const Instruction
*I
) const = 0;
1507 /// This method is used to check if at least one instruction in a collection
1508 /// of instructions is live.
1509 template <typename T
> bool isLiveInstSet(T begin
, T end
) const {
1510 for (const auto &I
: llvm::make_range(begin
, end
)) {
1511 assert(I
->getFunction() == getIRPosition().getAssociatedFunction() &&
1512 "Instruction must be in the same anchor scope function.");
1514 if (!isAssumedDead(I
))
1521 /// Return an IR position, see struct IRPosition.
1524 IRPosition
&getIRPosition() override
{ return *this; }
1525 const IRPosition
&getIRPosition() const override
{ return *this; }
1528 /// Create an abstract attribute view for the position \p IRP.
1529 static AAIsDead
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1531 /// Unique ID (due to the unique address)
1532 static const char ID
;
1535 /// State for dereferenceable attribute
1536 struct DerefState
: AbstractState
{
1538 /// State representing for dereferenceable bytes.
1539 IntegerState DerefBytesState
;
1541 /// State representing that whether the value is globaly dereferenceable.
1542 BooleanState GlobalState
;
1544 /// See AbstractState::isValidState()
1545 bool isValidState() const override
{ return DerefBytesState
.isValidState(); }
1547 /// See AbstractState::isAtFixpoint()
1548 bool isAtFixpoint() const override
{
1549 return !isValidState() ||
1550 (DerefBytesState
.isAtFixpoint() && GlobalState
.isAtFixpoint());
1553 /// See AbstractState::indicateOptimisticFixpoint(...)
1554 ChangeStatus
indicateOptimisticFixpoint() override
{
1555 DerefBytesState
.indicateOptimisticFixpoint();
1556 GlobalState
.indicateOptimisticFixpoint();
1557 return ChangeStatus::UNCHANGED
;
1560 /// See AbstractState::indicatePessimisticFixpoint(...)
1561 ChangeStatus
indicatePessimisticFixpoint() override
{
1562 DerefBytesState
.indicatePessimisticFixpoint();
1563 GlobalState
.indicatePessimisticFixpoint();
1564 return ChangeStatus::CHANGED
;
1567 /// Update known dereferenceable bytes.
1568 void takeKnownDerefBytesMaximum(uint64_t Bytes
) {
1569 DerefBytesState
.takeKnownMaximum(Bytes
);
1572 /// Update assumed dereferenceable bytes.
1573 void takeAssumedDerefBytesMinimum(uint64_t Bytes
) {
1574 DerefBytesState
.takeAssumedMinimum(Bytes
);
1577 /// Equality for DerefState.
1578 bool operator==(const DerefState
&R
) {
1579 return this->DerefBytesState
== R
.DerefBytesState
&&
1580 this->GlobalState
== R
.GlobalState
;
1583 /// Inequality for IntegerState.
1584 bool operator!=(const DerefState
&R
) { return !(*this == R
); }
1586 /// See IntegerState::operator^=
1587 DerefState
operator^=(const DerefState
&R
) {
1588 DerefBytesState
^= R
.DerefBytesState
;
1589 GlobalState
^= R
.GlobalState
;
1593 /// See IntegerState::operator+=
1594 DerefState
operator+=(const DerefState
&R
) {
1595 DerefBytesState
+= R
.DerefBytesState
;
1596 GlobalState
+= R
.GlobalState
;
1600 /// See IntegerState::operator&=
1601 DerefState
operator&=(const DerefState
&R
) {
1602 DerefBytesState
&= R
.DerefBytesState
;
1603 GlobalState
&= R
.GlobalState
;
1607 /// See IntegerState::operator|=
1608 DerefState
operator|=(const DerefState
&R
) {
1609 DerefBytesState
|= R
.DerefBytesState
;
1610 GlobalState
|= R
.GlobalState
;
1615 const AANonNull
*NonNullAA
= nullptr;
1618 /// An abstract interface for all dereferenceable attribute.
1619 struct AADereferenceable
1620 : public IRAttribute
<Attribute::Dereferenceable
,
1621 StateWrapper
<DerefState
, AbstractAttribute
>> {
1622 AADereferenceable(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1624 /// Return true if we assume that the underlying value is nonnull.
1625 bool isAssumedNonNull() const {
1626 return NonNullAA
&& NonNullAA
->isAssumedNonNull();
1629 /// Return true if we assume that underlying value is
1630 /// dereferenceable(_or_null) globally.
1631 bool isAssumedGlobal() const { return GlobalState
.getAssumed(); }
1633 /// Return true if we know that underlying value is
1634 /// dereferenceable(_or_null) globally.
1635 bool isKnownGlobal() const { return GlobalState
.getKnown(); }
1637 /// Return assumed dereferenceable bytes.
1638 uint32_t getAssumedDereferenceableBytes() const {
1639 return DerefBytesState
.getAssumed();
1642 /// Return known dereferenceable bytes.
1643 uint32_t getKnownDereferenceableBytes() const {
1644 return DerefBytesState
.getKnown();
1647 /// Create an abstract attribute view for the position \p IRP.
1648 static AADereferenceable
&createForPosition(const IRPosition
&IRP
,
1651 /// Unique ID (due to the unique address)
1652 static const char ID
;
1655 /// An abstract interface for all align attributes.
1657 : public IRAttribute
<Attribute::Alignment
,
1658 StateWrapper
<IntegerState
, AbstractAttribute
>> {
1659 AAAlign(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1661 /// Return assumed alignment.
1662 unsigned getAssumedAlign() const { return getAssumed(); }
1664 /// Return known alignemnt.
1665 unsigned getKnownAlign() const { return getKnown(); }
1667 /// Create an abstract attribute view for the position \p IRP.
1668 static AAAlign
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1670 /// Unique ID (due to the unique address)
1671 static const char ID
;
1674 /// An abstract interface for all nocapture attributes.
1676 : public IRAttribute
<Attribute::NoCapture
,
1677 StateWrapper
<IntegerState
, AbstractAttribute
>> {
1678 AANoCapture(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1680 /// State encoding bits. A set bit in the state means the property holds.
1681 /// NO_CAPTURE is the best possible state, 0 the worst possible state.
1683 NOT_CAPTURED_IN_MEM
= 1 << 0,
1684 NOT_CAPTURED_IN_INT
= 1 << 1,
1685 NOT_CAPTURED_IN_RET
= 1 << 2,
1687 /// If we do not capture the value in memory or through integers we can only
1688 /// communicate it back as a derived pointer.
1689 NO_CAPTURE_MAYBE_RETURNED
= NOT_CAPTURED_IN_MEM
| NOT_CAPTURED_IN_INT
,
1691 /// If we do not capture the value in memory, through integers, or as a
1692 /// derived pointer we know it is not captured.
1694 NOT_CAPTURED_IN_MEM
| NOT_CAPTURED_IN_INT
| NOT_CAPTURED_IN_RET
,
1697 /// Return true if we know that the underlying value is not captured in its
1698 /// respective scope.
1699 bool isKnownNoCapture() const { return isKnown(NO_CAPTURE
); }
1701 /// Return true if we assume that the underlying value is not captured in its
1702 /// respective scope.
1703 bool isAssumedNoCapture() const { return isAssumed(NO_CAPTURE
); }
1705 /// Return true if we know that the underlying value is not captured in its
1706 /// respective scope but we allow it to escape through a "return".
1707 bool isKnownNoCaptureMaybeReturned() const {
1708 return isKnown(NO_CAPTURE_MAYBE_RETURNED
);
1711 /// Return true if we assume that the underlying value is not captured in its
1712 /// respective scope but we allow it to escape through a "return".
1713 bool isAssumedNoCaptureMaybeReturned() const {
1714 return isAssumed(NO_CAPTURE_MAYBE_RETURNED
);
1717 /// Create an abstract attribute view for the position \p IRP.
1718 static AANoCapture
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1720 /// Unique ID (due to the unique address)
1721 static const char ID
;
1724 /// An abstract interface for value simplify abstract attribute.
1725 struct AAValueSimplify
: public StateWrapper
<BooleanState
, AbstractAttribute
>,
1727 AAValueSimplify(const IRPosition
&IRP
) : IRPosition(IRP
) {}
1729 /// Return an IR position, see struct IRPosition.
1732 IRPosition
&getIRPosition() { return *this; }
1733 const IRPosition
&getIRPosition() const { return *this; }
1736 /// Return an assumed simplified value if a single candidate is found. If
1737 /// there cannot be one, return original value. If it is not clear yet, return
1738 /// the Optional::NoneType.
1739 virtual Optional
<Value
*> getAssumedSimplifiedValue(Attributor
&A
) const = 0;
1741 /// Create an abstract attribute view for the position \p IRP.
1742 static AAValueSimplify
&createForPosition(const IRPosition
&IRP
,
1745 /// Unique ID (due to the unique address)
1746 static const char ID
;
1749 struct AAHeapToStack
: public StateWrapper
<BooleanState
, AbstractAttribute
>,
1751 AAHeapToStack(const IRPosition
&IRP
) : IRPosition(IRP
) {}
1753 /// Returns true if HeapToStack conversion is assumed to be possible.
1754 bool isAssumedHeapToStack() const { return getAssumed(); }
1756 /// Returns true if HeapToStack conversion is known to be possible.
1757 bool isKnownHeapToStack() const { return getKnown(); }
1759 /// Return an IR position, see struct IRPosition.
1762 IRPosition
&getIRPosition() { return *this; }
1763 const IRPosition
&getIRPosition() const { return *this; }
1766 /// Create an abstract attribute view for the position \p IRP.
1767 static AAHeapToStack
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1769 /// Unique ID (due to the unique address)
1770 static const char ID
;
1773 } // end namespace llvm
1775 #endif // LLVM_TRANSFORMS_IPO_FUNCTIONATTRS_H