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/MapVector.h"
100 #include "llvm/ADT/SCCIterator.h"
101 #include "llvm/ADT/SetVector.h"
102 #include "llvm/Analysis/AliasAnalysis.h"
103 #include "llvm/Analysis/CallGraph.h"
104 #include "llvm/Analysis/TargetLibraryInfo.h"
105 #include "llvm/IR/CallSite.h"
106 #include "llvm/IR/PassManager.h"
110 struct AbstractAttribute
;
111 struct InformationCache
;
116 /// Simple enum class that forces the status to be spelled out explicitly.
119 enum class ChangeStatus
{
124 ChangeStatus
operator|(ChangeStatus l
, ChangeStatus r
);
125 ChangeStatus
operator&(ChangeStatus l
, ChangeStatus r
);
128 /// Helper to describe and deal with positions in the LLVM-IR.
130 /// A position in the IR is described by an anchor value and an "offset" that
131 /// could be the argument number, for call sites and arguments, or an indicator
132 /// of the "position kind". The kinds, specified in the Kind enum below, include
133 /// the locations in the attribute list, i.a., function scope and return value,
134 /// as well as a distinction between call sites and functions. Finally, there
135 /// are floating values that do not have a corresponding attribute list
138 virtual ~IRPosition() {}
140 /// The positions we distinguish in the IR.
142 /// The values are chosen such that the KindOrArgNo member has a value >= 1
143 /// if it is an argument or call site argument while a value < 1 indicates the
144 /// respective kind of that value.
146 IRP_INVALID
= -6, ///< An invalid position.
147 IRP_FLOAT
= -5, ///< A position that is not associated with a spot suitable
148 ///< for attributes. This could be any value or instruction.
149 IRP_RETURNED
= -4, ///< An attribute for the function return value.
150 IRP_CALL_SITE_RETURNED
= -3, ///< An attribute for a call site return value.
151 IRP_FUNCTION
= -2, ///< An attribute for a function (scope).
152 IRP_CALL_SITE
= -1, ///< An attribute for a call site (function scope).
153 IRP_ARGUMENT
= 0, ///< An attribute for a function argument.
154 IRP_CALL_SITE_ARGUMENT
= 1, ///< An attribute for a call site argument.
157 /// Default constructor available to create invalid positions implicitly. All
158 /// other positions need to be created explicitly through the appropriate
159 /// static member function.
160 IRPosition() : AnchorVal(nullptr), KindOrArgNo(IRP_INVALID
) { verify(); }
162 /// Create a position describing the value of \p V.
163 static const IRPosition
value(const Value
&V
) {
164 if (auto *Arg
= dyn_cast
<Argument
>(&V
))
165 return IRPosition::argument(*Arg
);
166 if (auto *CB
= dyn_cast
<CallBase
>(&V
))
167 return IRPosition::callsite_returned(*CB
);
168 return IRPosition(const_cast<Value
&>(V
), IRP_FLOAT
);
171 /// Create a position describing the function scope of \p F.
172 static const IRPosition
function(const Function
&F
) {
173 return IRPosition(const_cast<Function
&>(F
), IRP_FUNCTION
);
176 /// Create a position describing the returned value of \p F.
177 static const IRPosition
returned(const Function
&F
) {
178 return IRPosition(const_cast<Function
&>(F
), IRP_RETURNED
);
181 /// Create a position describing the argument \p Arg.
182 static const IRPosition
argument(const Argument
&Arg
) {
183 return IRPosition(const_cast<Argument
&>(Arg
), Kind(Arg
.getArgNo()));
186 /// Create a position describing the function scope of \p CB.
187 static const IRPosition
callsite_function(const CallBase
&CB
) {
188 return IRPosition(const_cast<CallBase
&>(CB
), IRP_CALL_SITE
);
191 /// Create a position describing the returned value of \p CB.
192 static const IRPosition
callsite_returned(const CallBase
&CB
) {
193 return IRPosition(const_cast<CallBase
&>(CB
), IRP_CALL_SITE_RETURNED
);
196 /// Create a position describing the argument of \p CB at position \p ArgNo.
197 static const IRPosition
callsite_argument(const CallBase
&CB
,
199 return IRPosition(const_cast<CallBase
&>(CB
), Kind(ArgNo
));
202 /// Create a position describing the function scope of \p ICS.
203 static const IRPosition
callsite_function(ImmutableCallSite ICS
) {
204 return IRPosition::callsite_function(cast
<CallBase
>(*ICS
.getInstruction()));
207 /// Create a position describing the returned value of \p ICS.
208 static const IRPosition
callsite_returned(ImmutableCallSite ICS
) {
209 return IRPosition::callsite_returned(cast
<CallBase
>(*ICS
.getInstruction()));
212 /// Create a position describing the argument of \p ICS at position \p ArgNo.
213 static const IRPosition
callsite_argument(ImmutableCallSite ICS
,
215 return IRPosition::callsite_argument(cast
<CallBase
>(*ICS
.getInstruction()),
219 /// Create a position with function scope matching the "context" of \p IRP.
220 /// If \p IRP is a call site (see isAnyCallSitePosition()) then the result
221 /// will be a call site position, otherwise the function position of the
222 /// associated function.
223 static const IRPosition
function_scope(const IRPosition
&IRP
) {
224 if (IRP
.isAnyCallSitePosition()) {
225 return IRPosition::callsite_function(
226 cast
<CallBase
>(IRP
.getAnchorValue()));
228 assert(IRP
.getAssociatedFunction());
229 return IRPosition::function(*IRP
.getAssociatedFunction());
232 bool operator==(const IRPosition
&RHS
) const {
233 return (AnchorVal
== RHS
.AnchorVal
) && (KindOrArgNo
== RHS
.KindOrArgNo
);
235 bool operator!=(const IRPosition
&RHS
) const { return !(*this == RHS
); }
237 /// Return the value this abstract attribute is anchored with.
239 /// The anchor value might not be the associated value if the latter is not
240 /// sufficient to determine where arguments will be manifested. This is, so
241 /// far, only the case for call site arguments as the value is not sufficient
242 /// to pinpoint them. Instead, we can use the call site as an anchor.
245 Value
&getAnchorValue() {
246 assert(KindOrArgNo
!= IRP_INVALID
&&
247 "Invalid position does not have an anchor value!");
250 const Value
&getAnchorValue() const {
251 return const_cast<IRPosition
*>(this)->getAnchorValue();
255 /// Return the associated function, if any.
258 Function
*getAssociatedFunction() {
259 if (auto *CB
= dyn_cast
<CallBase
>(AnchorVal
))
260 return CB
->getCalledFunction();
261 assert(KindOrArgNo
!= IRP_INVALID
&&
262 "Invalid position does not have an anchor scope!");
263 Value
&V
= getAnchorValue();
264 if (isa
<Function
>(V
))
265 return &cast
<Function
>(V
);
266 if (isa
<Argument
>(V
))
267 return cast
<Argument
>(V
).getParent();
268 if (isa
<Instruction
>(V
))
269 return cast
<Instruction
>(V
).getFunction();
272 const Function
*getAssociatedFunction() const {
273 return const_cast<IRPosition
*>(this)->getAssociatedFunction();
277 /// Return the associated argument, if any.
280 Argument
*getAssociatedArgument() {
281 if (auto *Arg
= dyn_cast
<Argument
>(&getAnchorValue()))
283 int ArgNo
= getArgNo();
286 Function
*AssociatedFn
= getAssociatedFunction();
287 if (!AssociatedFn
|| AssociatedFn
->arg_size() <= unsigned(ArgNo
))
289 return AssociatedFn
->arg_begin() + ArgNo
;
291 const Argument
*getAssociatedArgument() const {
292 return const_cast<IRPosition
*>(this)->getAssociatedArgument();
296 /// Return true if the position refers to a function interface, that is the
297 /// function scope, the function return, or an argumnt.
298 bool isFnInterfaceKind() const {
299 switch (getPositionKind()) {
300 case IRPosition::IRP_FUNCTION
:
301 case IRPosition::IRP_RETURNED
:
302 case IRPosition::IRP_ARGUMENT
:
309 /// Return the Function surrounding the anchor value.
312 Function
*getAnchorScope() {
313 Value
&V
= getAnchorValue();
314 if (isa
<Function
>(V
))
315 return &cast
<Function
>(V
);
316 if (isa
<Argument
>(V
))
317 return cast
<Argument
>(V
).getParent();
318 if (isa
<Instruction
>(V
))
319 return cast
<Instruction
>(V
).getFunction();
322 const Function
*getAnchorScope() const {
323 return const_cast<IRPosition
*>(this)->getAnchorScope();
327 /// Return the context instruction, if any.
330 Instruction
*getCtxI() {
331 Value
&V
= getAnchorValue();
332 if (auto *I
= dyn_cast
<Instruction
>(&V
))
334 if (auto *Arg
= dyn_cast
<Argument
>(&V
))
335 if (!Arg
->getParent()->isDeclaration())
336 return &Arg
->getParent()->getEntryBlock().front();
337 if (auto *F
= dyn_cast
<Function
>(&V
))
338 if (!F
->isDeclaration())
339 return &(F
->getEntryBlock().front());
342 const Instruction
*getCtxI() const {
343 return const_cast<IRPosition
*>(this)->getCtxI();
347 /// Return the value this abstract attribute is associated with.
350 Value
&getAssociatedValue() {
351 assert(KindOrArgNo
!= IRP_INVALID
&&
352 "Invalid position does not have an associated value!");
353 if (getArgNo() < 0 || isa
<Argument
>(AnchorVal
))
355 assert(isa
<CallBase
>(AnchorVal
) && "Expected a call base!");
356 return *cast
<CallBase
>(AnchorVal
)->getArgOperand(getArgNo());
358 const Value
&getAssociatedValue() const {
359 return const_cast<IRPosition
*>(this)->getAssociatedValue();
363 /// Return the argument number of the associated value if it is an argument or
364 /// call site argument, otherwise a negative value.
365 int getArgNo() const { return KindOrArgNo
; }
367 /// Return the index in the attribute list for this position.
368 unsigned getAttrIdx() const {
369 switch (getPositionKind()) {
370 case IRPosition::IRP_INVALID
:
371 case IRPosition::IRP_FLOAT
:
373 case IRPosition::IRP_FUNCTION
:
374 case IRPosition::IRP_CALL_SITE
:
375 return AttributeList::FunctionIndex
;
376 case IRPosition::IRP_RETURNED
:
377 case IRPosition::IRP_CALL_SITE_RETURNED
:
378 return AttributeList::ReturnIndex
;
379 case IRPosition::IRP_ARGUMENT
:
380 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
381 return KindOrArgNo
+ AttributeList::FirstArgIndex
;
384 "There is no attribute index for a floating or invalid position!");
387 /// Return the associated position kind.
388 Kind
getPositionKind() const {
389 if (getArgNo() >= 0) {
390 assert(((isa
<Argument
>(getAnchorValue()) &&
391 isa
<Argument
>(getAssociatedValue())) ||
392 isa
<CallBase
>(getAnchorValue())) &&
393 "Expected argument or call base due to argument number!");
394 if (isa
<CallBase
>(getAnchorValue()))
395 return IRP_CALL_SITE_ARGUMENT
;
399 assert(KindOrArgNo
< 0 &&
400 "Expected (call site) arguments to never reach this point!");
401 assert(!isa
<Argument
>(getAnchorValue()) &&
402 "Expected arguments to have an associated argument position!");
403 return Kind(KindOrArgNo
);
406 /// TODO: Figure out if the attribute related helper functions should live
407 /// here or somewhere else.
409 /// Return true if any kind in \p AKs existing in the IR at a position that
410 /// will affect this one. See also getAttrs(...).
411 bool hasAttr(ArrayRef
<Attribute::AttrKind
> AKs
) const;
413 /// Return the attributes of any kind in \p AKs existing in the IR at a
414 /// position that will affect this one. While each position can only have a
415 /// single attribute of any kind in \p AKs, there are "subsuming" positions
416 /// that could have an attribute as well. This method returns all attributes
417 /// found in \p Attrs.
418 void getAttrs(ArrayRef
<Attribute::AttrKind
> AKs
,
419 SmallVectorImpl
<Attribute
> &Attrs
) const;
421 /// Return the attribute of kind \p AK existing in the IR at this position.
422 Attribute
getAttr(Attribute::AttrKind AK
) const {
423 if (getPositionKind() == IRP_INVALID
|| getPositionKind() == IRP_FLOAT
)
426 AttributeList AttrList
;
427 if (ImmutableCallSite ICS
= ImmutableCallSite(&getAnchorValue()))
428 AttrList
= ICS
.getAttributes();
430 AttrList
= getAssociatedFunction()->getAttributes();
432 if (AttrList
.hasAttribute(getAttrIdx(), AK
))
433 return AttrList
.getAttribute(getAttrIdx(), AK
);
437 bool isAnyCallSitePosition() const {
438 switch (getPositionKind()) {
439 case IRPosition::IRP_CALL_SITE
:
440 case IRPosition::IRP_CALL_SITE_RETURNED
:
441 case IRPosition::IRP_CALL_SITE_ARGUMENT
:
448 /// Special DenseMap key values.
451 static const IRPosition EmptyKey
;
452 static const IRPosition TombstoneKey
;
456 /// Private constructor for special values only!
457 explicit IRPosition(int KindOrArgNo
)
458 : AnchorVal(0), KindOrArgNo(KindOrArgNo
) {}
460 /// IRPosition anchored at \p AnchorVal with kind/argument numbet \p PK.
461 explicit IRPosition(Value
&AnchorVal
, Kind PK
)
462 : AnchorVal(&AnchorVal
), KindOrArgNo(PK
) {
466 /// Verify internal invariants.
469 /// The value this position is anchored at.
472 /// The argument number, if non-negative, or the position "kind".
476 /// Helper that allows IRPosition as a key in a DenseMap.
477 template <> struct DenseMapInfo
<IRPosition
> {
478 static inline IRPosition
getEmptyKey() { return IRPosition::EmptyKey
; }
479 static inline IRPosition
getTombstoneKey() {
480 return IRPosition::TombstoneKey
;
482 static unsigned getHashValue(const IRPosition
&IRP
) {
483 return (DenseMapInfo
<Value
*>::getHashValue(&IRP
.getAnchorValue()) << 4) ^
484 (unsigned(IRP
.getArgNo()));
486 static bool isEqual(const IRPosition
&LHS
, const IRPosition
&RHS
) {
491 /// A visitor class for IR positions.
493 /// Given a position P, the SubsumingPositionIterator allows to visit "subsuming
494 /// positions" wrt. attributes/information. Thus, if a piece of information
495 /// holds for a subsuming position, it also holds for the position P.
497 /// The subsuming positions always include the initial position and then,
498 /// depending on the position kind, additionally the following ones:
499 /// - for IRP_RETURNED:
500 /// - the function (IRP_FUNCTION)
501 /// - for IRP_ARGUMENT:
502 /// - the function (IRP_FUNCTION)
503 /// - for IRP_CALL_SITE:
504 /// - the callee (IRP_FUNCTION), if known
505 /// - for IRP_CALL_SITE_RETURNED:
506 /// - the callee (IRP_RETURNED), if known
507 /// - the call site (IRP_FUNCTION)
508 /// - the callee (IRP_FUNCTION), if known
509 /// - for IRP_CALL_SITE_ARGUMENT:
510 /// - the argument of the callee (IRP_ARGUMENT), if known
511 /// - the callee (IRP_FUNCTION), if known
512 /// - the position the call site argument is associated with if it is not
513 /// anchored to the call site, e.g., if it is an arugment then the argument
515 class SubsumingPositionIterator
{
516 SmallVector
<IRPosition
, 4> IRPositions
;
517 using iterator
= decltype(IRPositions
)::iterator
;
520 SubsumingPositionIterator(const IRPosition
&IRP
);
521 iterator
begin() { return IRPositions
.begin(); }
522 iterator
end() { return IRPositions
.end(); }
525 /// Wrapper for FunctoinAnalysisManager.
526 struct AnalysisGetter
{
527 template <typename Analysis
>
528 typename
Analysis::Result
*getAnalysis(const Function
&F
) {
529 if (!MAM
|| !F
.getParent())
531 auto &FAM
= MAM
->getResult
<FunctionAnalysisManagerModuleProxy
>(
532 const_cast<Module
&>(*F
.getParent()))
534 return &FAM
.getResult
<Analysis
>(const_cast<Function
&>(F
));
537 template <typename Analysis
>
538 typename
Analysis::Result
*getAnalysis(const Module
&M
) {
541 return &MAM
->getResult
<Analysis
>(const_cast<Module
&>(M
));
543 AnalysisGetter(ModuleAnalysisManager
&MAM
) : MAM(&MAM
) {}
547 ModuleAnalysisManager
*MAM
= nullptr;
550 /// Data structure to hold cached (LLVM-IR) information.
552 /// All attributes are given an InformationCache object at creation time to
553 /// avoid inspection of the IR by all of them individually. This default
554 /// InformationCache will hold information required by 'default' attributes,
555 /// thus the ones deduced when Attributor::identifyDefaultAbstractAttributes(..)
558 /// If custom abstract attributes, registered manually through
559 /// Attributor::registerAA(...), need more information, especially if it is not
560 /// reusable, it is advised to inherit from the InformationCache and cast the
561 /// instance down in the abstract attributes.
562 struct InformationCache
{
563 InformationCache(const Module
&M
, AnalysisGetter
&AG
)
564 : DL(M
.getDataLayout()), AG(AG
) {
566 CallGraph
*CG
= AG
.getAnalysis
<CallGraphAnalysis
>(M
);
570 DenseMap
<const Function
*, unsigned> SccSize
;
571 for (scc_iterator
<CallGraph
*> I
= scc_begin(CG
); !I
.isAtEnd(); ++I
) {
572 for (CallGraphNode
*Node
: *I
)
573 SccSize
[Node
->getFunction()] = I
->size();
575 SccSizeOpt
= std::move(SccSize
);
578 /// A map type from opcodes to instructions with this opcode.
579 using OpcodeInstMapTy
= DenseMap
<unsigned, SmallVector
<Instruction
*, 32>>;
581 /// Return the map that relates "interesting" opcodes with all instructions
582 /// with that opcode in \p F.
583 OpcodeInstMapTy
&getOpcodeInstMapForFunction(const Function
&F
) {
584 return FuncInstOpcodeMap
[&F
];
587 /// A vector type to hold instructions.
588 using InstructionVectorTy
= std::vector
<Instruction
*>;
590 /// Return the instructions in \p F that may read or write memory.
591 InstructionVectorTy
&getReadOrWriteInstsForFunction(const Function
&F
) {
592 return FuncRWInstsMap
[&F
];
595 /// Return TargetLibraryInfo for function \p F.
596 TargetLibraryInfo
*getTargetLibraryInfoForFunction(const Function
&F
) {
597 return AG
.getAnalysis
<TargetLibraryAnalysis
>(F
);
600 /// Return AliasAnalysis Result for function \p F.
601 AAResults
*getAAResultsForFunction(const Function
&F
) {
602 return AG
.getAnalysis
<AAManager
>(F
);
605 /// Return SCC size on call graph for function \p F.
606 unsigned getSccSize(const Function
&F
) {
607 if (!SccSizeOpt
.hasValue())
609 return (SccSizeOpt
.getValue())[&F
];
612 /// Return datalayout used in the module.
613 const DataLayout
&getDL() { return DL
; }
616 /// A map type from functions to opcode to instruction maps.
617 using FuncInstOpcodeMapTy
= DenseMap
<const Function
*, OpcodeInstMapTy
>;
619 /// A map type from functions to their read or write instructions.
620 using FuncRWInstsMapTy
= DenseMap
<const Function
*, InstructionVectorTy
>;
622 /// A nested map that remembers all instructions in a function with a certain
623 /// instruction opcode (Instruction::getOpcode()).
624 FuncInstOpcodeMapTy FuncInstOpcodeMap
;
626 /// A map from functions to their instructions that may read or write memory.
627 FuncRWInstsMapTy FuncRWInstsMap
;
629 /// The datalayout used in the module.
630 const DataLayout
&DL
;
632 /// Getters for analysis.
635 /// Cache result for scc size in the call graph
636 Optional
<DenseMap
<const Function
*, unsigned>> SccSizeOpt
;
638 /// Give the Attributor access to the members so
639 /// Attributor::identifyDefaultAbstractAttributes(...) can initialize them.
640 friend struct Attributor
;
643 /// The fixpoint analysis framework that orchestrates the attribute deduction.
645 /// The Attributor provides a general abstract analysis framework (guided
646 /// fixpoint iteration) as well as helper functions for the deduction of
647 /// (LLVM-IR) attributes. However, also other code properties can be deduced,
648 /// propagated, and ultimately manifested through the Attributor framework. This
649 /// is particularly useful if these properties interact with attributes and a
650 /// co-scheduled deduction allows to improve the solution. Even if not, thus if
651 /// attributes/properties are completely isolated, they should use the
652 /// Attributor framework to reduce the number of fixpoint iteration frameworks
653 /// in the code base. Note that the Attributor design makes sure that isolated
654 /// attributes are not impacted, in any way, by others derived at the same time
655 /// if there is no cross-reasoning performed.
657 /// The public facing interface of the Attributor is kept simple and basically
658 /// allows abstract attributes to one thing, query abstract attributes
659 /// in-flight. There are two reasons to do this:
660 /// a) The optimistic state of one abstract attribute can justify an
661 /// optimistic state of another, allowing to framework to end up with an
662 /// optimistic (=best possible) fixpoint instead of one based solely on
663 /// information in the IR.
664 /// b) This avoids reimplementing various kinds of lookups, e.g., to check
665 /// for existing IR attributes, in favor of a single lookups interface
666 /// provided by an abstract attribute subclass.
668 /// NOTE: The mechanics of adding a new "concrete" abstract attribute are
669 /// described in the file comment.
673 /// \param InfoCache Cache to hold various information accessible for
674 /// the abstract attributes.
675 /// \param DepRecomputeInterval Number of iterations until the dependences
676 /// between abstract attributes are recomputed.
677 /// \param Whitelist If not null, a set limiting the attribute opportunities.
678 Attributor(InformationCache
&InfoCache
, unsigned DepRecomputeInterval
,
679 DenseSet
<const char *> *Whitelist
= nullptr)
680 : InfoCache(InfoCache
), DepRecomputeInterval(DepRecomputeInterval
),
681 Whitelist(Whitelist
) {}
683 ~Attributor() { DeleteContainerPointers(AllAbstractAttributes
); }
685 /// Run the analyses until a fixpoint is reached or enforced (timeout).
687 /// The attributes registered with this Attributor can be used after as long
688 /// as the Attributor is not destroyed (it owns the attributes now).
690 /// \Returns CHANGED if the IR was changed, otherwise UNCHANGED.
691 ChangeStatus
run(Module
&M
);
693 /// Lookup an abstract attribute of type \p AAType at position \p IRP. While
694 /// no abstract attribute is found equivalent positions are checked, see
695 /// SubsumingPositionIterator. Thus, the returned abstract attribute
696 /// might be anchored at a different position, e.g., the callee if \p IRP is a
699 /// This method is the only (supported) way an abstract attribute can retrieve
700 /// information from another abstract attribute. As an example, take an
701 /// abstract attribute that determines the memory access behavior for a
702 /// argument (readnone, readonly, ...). It should use `getAAFor` to get the
703 /// most optimistic information for other abstract attributes in-flight, e.g.
704 /// the one reasoning about the "captured" state for the argument or the one
705 /// reasoning on the memory access behavior of the function as a whole.
707 /// If the flag \p TrackDependence is set to false the dependence from
708 /// \p QueryingAA to the return abstract attribute is not automatically
709 /// recorded. This should only be used if the caller will record the
710 /// dependence explicitly if necessary, thus if it the returned abstract
711 /// attribute is used for reasoning. To record the dependences explicitly use
712 /// the `Attributor::recordDependence` method.
713 template <typename AAType
>
714 const AAType
&getAAFor(const AbstractAttribute
&QueryingAA
,
715 const IRPosition
&IRP
, bool TrackDependence
= true) {
716 return getOrCreateAAFor
<AAType
>(IRP
, &QueryingAA
, TrackDependence
);
719 /// Explicitly record a dependence from \p FromAA to \p ToAA, that is if
720 /// \p FromAA changes \p ToAA should be updated as well.
722 /// This method should be used in conjunction with the `getAAFor` method and
723 /// with the TrackDependence flag passed to the method set to false. This can
724 /// be beneficial to avoid false dependences but it requires the users of
725 /// `getAAFor` to explicitly record true dependences through this method.
726 void recordDependence(const AbstractAttribute
&FromAA
,
727 const AbstractAttribute
&ToAA
) {
728 QueryMap
[&FromAA
].insert(const_cast<AbstractAttribute
*>(&ToAA
));
731 /// Introduce a new abstract attribute into the fixpoint analysis.
733 /// Note that ownership of the attribute is given to the Attributor. It will
734 /// invoke delete for the Attributor on destruction of the Attributor.
736 /// Attributes are identified by their IR position (AAType::getIRPosition())
737 /// and the address of their static member (see AAType::ID).
738 template <typename AAType
> AAType
®isterAA(AAType
&AA
) {
739 static_assert(std::is_base_of
<AbstractAttribute
, AAType
>::value
,
740 "Cannot register an attribute with a type not derived from "
741 "'AbstractAttribute'!");
742 // Put the attribute in the lookup map structure and the container we use to
743 // keep track of all attributes.
744 IRPosition
&IRP
= AA
.getIRPosition();
745 auto &KindToAbstractAttributeMap
= AAMap
[IRP
];
746 assert(!KindToAbstractAttributeMap
.count(&AAType::ID
) &&
747 "Attribute already in map!");
748 KindToAbstractAttributeMap
[&AAType::ID
] = &AA
;
749 AllAbstractAttributes
.push_back(&AA
);
753 /// Return the internal information cache.
754 InformationCache
&getInfoCache() { return InfoCache
; }
756 /// Determine opportunities to derive 'default' attributes in \p F and create
757 /// abstract attribute objects for them.
759 /// \param F The function that is checked for attribute opportunities.
761 /// Note that abstract attribute instances are generally created even if the
762 /// IR already contains the information they would deduce. The most important
763 /// reason for this is the single interface, the one of the abstract attribute
764 /// instance, which can be queried without the need to look at the IR in
766 void identifyDefaultAbstractAttributes(Function
&F
);
768 /// Initialize the information cache for queries regarding function \p F.
770 /// This method needs to be called for all function that might be looked at
771 /// through the information cache interface *prior* to looking at them.
772 void initializeInformationCache(Function
&F
);
774 /// Mark the internal function \p F as live.
776 /// This will trigger the identification and initialization of attributes for
778 void markLiveInternalFunction(const Function
&F
) {
779 assert(F
.hasInternalLinkage() &&
780 "Only internal linkage is assumed dead initially.");
782 identifyDefaultAbstractAttributes(const_cast<Function
&>(F
));
785 /// Record that \p I is deleted after information was manifested.
786 void deleteAfterManifest(Instruction
&I
) { ToBeDeletedInsts
.insert(&I
); }
788 /// Record that \p BB is deleted after information was manifested.
789 void deleteAfterManifest(BasicBlock
&BB
) { ToBeDeletedBlocks
.insert(&BB
); }
791 /// Record that \p F is deleted after information was manifested.
792 void deleteAfterManifest(Function
&F
) { ToBeDeletedFunctions
.insert(&F
); }
794 /// Return true if \p AA (or its context instruction) is assumed dead.
796 /// If \p LivenessAA is not provided it is queried.
797 bool isAssumedDead(const AbstractAttribute
&AA
, const AAIsDead
*LivenessAA
);
799 /// Check \p Pred on all function call sites.
801 /// This method will evaluate \p Pred on call sites and return
802 /// true if \p Pred holds in every call sites. However, this is only possible
803 /// all call sites are known, hence the function has internal linkage.
804 bool checkForAllCallSites(const function_ref
<bool(CallSite
)> &Pred
,
805 const AbstractAttribute
&QueryingAA
,
806 bool RequireAllCallSites
);
808 /// Check \p Pred on all values potentially returned by \p F.
810 /// This method will evaluate \p Pred on all values potentially returned by
811 /// the function associated with \p QueryingAA. The returned values are
812 /// matched with their respective return instructions. Returns true if \p Pred
813 /// holds on all of them.
814 bool checkForAllReturnedValuesAndReturnInsts(
815 const function_ref
<bool(Value
&, const SmallSetVector
<ReturnInst
*, 4> &)>
817 const AbstractAttribute
&QueryingAA
);
819 /// Check \p Pred on all values potentially returned by the function
820 /// associated with \p QueryingAA.
822 /// This is the context insensitive version of the method above.
823 bool checkForAllReturnedValues(const function_ref
<bool(Value
&)> &Pred
,
824 const AbstractAttribute
&QueryingAA
);
826 /// Check \p Pred on all instructions with an opcode present in \p Opcodes.
828 /// This method will evaluate \p Pred on all instructions with an opcode
829 /// present in \p Opcode and return true if \p Pred holds on all of them.
830 bool checkForAllInstructions(const function_ref
<bool(Instruction
&)> &Pred
,
831 const AbstractAttribute
&QueryingAA
,
832 const ArrayRef
<unsigned> &Opcodes
);
834 /// Check \p Pred on all call-like instructions (=CallBased derived).
836 /// See checkForAllCallLikeInstructions(...) for more information.
838 checkForAllCallLikeInstructions(const function_ref
<bool(Instruction
&)> &Pred
,
839 const AbstractAttribute
&QueryingAA
) {
840 return checkForAllInstructions(Pred
, QueryingAA
,
841 {(unsigned)Instruction::Invoke
,
842 (unsigned)Instruction::CallBr
,
843 (unsigned)Instruction::Call
});
846 /// Check \p Pred on all Read/Write instructions.
848 /// This method will evaluate \p Pred on all instructions that read or write
849 /// to memory present in the information cache and return true if \p Pred
850 /// holds on all of them.
851 bool checkForAllReadWriteInstructions(
852 const llvm::function_ref
<bool(Instruction
&)> &Pred
,
853 AbstractAttribute
&QueryingAA
);
855 /// Return the data layout associated with the anchor scope.
856 const DataLayout
&getDataLayout() const { return InfoCache
.DL
; }
860 /// The private version of getAAFor that allows to omit a querying abstract
861 /// attribute. See also the public getAAFor method.
862 template <typename AAType
>
863 const AAType
&getOrCreateAAFor(const IRPosition
&IRP
,
864 const AbstractAttribute
*QueryingAA
= nullptr,
865 bool TrackDependence
= false) {
866 if (const AAType
*AAPtr
=
867 lookupAAFor
<AAType
>(IRP
, QueryingAA
, TrackDependence
))
870 // No matching attribute found, create one.
871 // Use the static create method.
872 auto &AA
= AAType::createForPosition(IRP
, *this);
874 AA
.initialize(*this);
876 // Bootstrap the new attribute with an initial update to propagate
877 // information, e.g., function -> call site. If it is not on a given
878 // whitelist we will not perform updates at all.
879 if (Whitelist
&& !Whitelist
->count(&AAType::ID
))
880 AA
.getState().indicatePessimisticFixpoint();
884 if (TrackDependence
&& AA
.getState().isValidState())
885 QueryMap
[&AA
].insert(const_cast<AbstractAttribute
*>(QueryingAA
));
889 /// Return the attribute of \p AAType for \p IRP if existing.
890 template <typename AAType
>
891 const AAType
*lookupAAFor(const IRPosition
&IRP
,
892 const AbstractAttribute
*QueryingAA
= nullptr,
893 bool TrackDependence
= false) {
894 static_assert(std::is_base_of
<AbstractAttribute
, AAType
>::value
,
895 "Cannot query an attribute with a type not derived from "
896 "'AbstractAttribute'!");
897 assert((QueryingAA
|| !TrackDependence
) &&
898 "Cannot track dependences without a QueryingAA!");
900 // Lookup the abstract attribute of type AAType. If found, return it after
901 // registering a dependence of QueryingAA on the one returned attribute.
902 const auto &KindToAbstractAttributeMap
= AAMap
.lookup(IRP
);
903 if (AAType
*AA
= static_cast<AAType
*>(
904 KindToAbstractAttributeMap
.lookup(&AAType::ID
))) {
905 // Do not register a dependence on an attribute with an invalid state.
906 if (TrackDependence
&& AA
->getState().isValidState())
907 QueryMap
[AA
].insert(const_cast<AbstractAttribute
*>(QueryingAA
));
913 /// The set of all abstract attributes.
915 using AAVector
= SmallVector
<AbstractAttribute
*, 64>;
916 AAVector AllAbstractAttributes
;
919 /// A nested map to lookup abstract attributes based on the argument position
920 /// on the outer level, and the addresses of the static member (AAType::ID) on
923 using KindToAbstractAttributeMap
=
924 DenseMap
<const char *, AbstractAttribute
*>;
925 DenseMap
<IRPosition
, KindToAbstractAttributeMap
> AAMap
;
928 /// A map from abstract attributes to the ones that queried them through calls
929 /// to the getAAFor<...>(...) method.
932 MapVector
<const AbstractAttribute
*, SetVector
<AbstractAttribute
*>>;
936 /// The information cache that holds pre-processed (LLVM-IR) information.
937 InformationCache
&InfoCache
;
939 /// Number of iterations until the dependences between abstract attributes are
941 const unsigned DepRecomputeInterval
;
943 /// If not null, a set limiting the attribute opportunities.
944 const DenseSet
<const char *> *Whitelist
;
946 /// A set to remember the functions we already assume to be live and visited.
947 DenseSet
<const Function
*> VisitedFunctions
;
949 /// Functions, blocks, and instructions we delete after manifest is done.
952 SmallPtrSet
<Function
*, 8> ToBeDeletedFunctions
;
953 SmallPtrSet
<BasicBlock
*, 8> ToBeDeletedBlocks
;
954 SmallPtrSet
<Instruction
*, 8> ToBeDeletedInsts
;
958 /// An interface to query the internal state of an abstract attribute.
960 /// The abstract state is a minimal interface that allows the Attributor to
961 /// communicate with the abstract attributes about their internal state without
962 /// enforcing or exposing implementation details, e.g., the (existence of an)
963 /// underlying lattice.
965 /// It is sufficient to be able to query if a state is (1) valid or invalid, (2)
966 /// at a fixpoint, and to indicate to the state that (3) an optimistic fixpoint
967 /// was reached or (4) a pessimistic fixpoint was enforced.
969 /// All methods need to be implemented by the subclass. For the common use case,
970 /// a single boolean state or a bit-encoded state, the BooleanState and
971 /// IntegerState classes are already provided. An abstract attribute can inherit
972 /// from them to get the abstract state interface and additional methods to
973 /// directly modify the state based if needed. See the class comments for help.
974 struct AbstractState
{
975 virtual ~AbstractState() {}
977 /// Return if this abstract state is in a valid state. If false, no
978 /// information provided should be used.
979 virtual bool isValidState() const = 0;
981 /// Return if this abstract state is fixed, thus does not need to be updated
982 /// if information changes as it cannot change itself.
983 virtual bool isAtFixpoint() const = 0;
985 /// Indicate that the abstract state should converge to the optimistic state.
987 /// This will usually make the optimistically assumed state the known to be
990 /// \returns ChangeStatus::UNCHANGED as the assumed value should not change.
991 virtual ChangeStatus
indicateOptimisticFixpoint() = 0;
993 /// Indicate that the abstract state should converge to the pessimistic state.
995 /// This will usually revert the optimistically assumed state to the known to
998 /// \returns ChangeStatus::CHANGED as the assumed value may change.
999 virtual ChangeStatus
indicatePessimisticFixpoint() = 0;
1002 /// Simple state with integers encoding.
1004 /// The interface ensures that the assumed bits are always a subset of the known
1005 /// bits. Users can only add known bits and, except through adding known bits,
1006 /// they can only remove assumed bits. This should guarantee monotoniticy and
1007 /// thereby the existence of a fixpoint (if used corretly). The fixpoint is
1008 /// reached when the assumed and known state/bits are equal. Users can
1009 /// force/inidicate a fixpoint. If an optimistic one is indicated, the known
1010 /// state will catch up with the assumed one, for a pessimistic fixpoint it is
1011 /// the other way around.
1012 struct IntegerState
: public AbstractState
{
1013 /// Underlying integer type, we assume 32 bits to be enough.
1014 using base_t
= uint32_t;
1016 /// Initialize the (best) state.
1017 IntegerState(base_t BestState
= ~0) : Assumed(BestState
) {}
1019 /// Return the worst possible representable state.
1020 static constexpr base_t
getWorstState() { return 0; }
1022 /// See AbstractState::isValidState()
1023 /// NOTE: For now we simply pretend that the worst possible state is invalid.
1024 bool isValidState() const override
{ return Assumed
!= getWorstState(); }
1026 /// See AbstractState::isAtFixpoint()
1027 bool isAtFixpoint() const override
{ return Assumed
== Known
; }
1029 /// See AbstractState::indicateOptimisticFixpoint(...)
1030 ChangeStatus
indicateOptimisticFixpoint() override
{
1032 return ChangeStatus::UNCHANGED
;
1035 /// See AbstractState::indicatePessimisticFixpoint(...)
1036 ChangeStatus
indicatePessimisticFixpoint() override
{
1038 return ChangeStatus::CHANGED
;
1041 /// Return the known state encoding
1042 base_t
getKnown() const { return Known
; }
1044 /// Return the assumed state encoding.
1045 base_t
getAssumed() const { return Assumed
; }
1047 /// Return true if the bits set in \p BitsEncoding are "known bits".
1048 bool isKnown(base_t BitsEncoding
) const {
1049 return (Known
& BitsEncoding
) == BitsEncoding
;
1052 /// Return true if the bits set in \p BitsEncoding are "assumed bits".
1053 bool isAssumed(base_t BitsEncoding
) const {
1054 return (Assumed
& BitsEncoding
) == BitsEncoding
;
1057 /// Add the bits in \p BitsEncoding to the "known bits".
1058 IntegerState
&addKnownBits(base_t Bits
) {
1059 // Make sure we never miss any "known bits".
1065 /// Remove the bits in \p BitsEncoding from the "assumed bits" if not known.
1066 IntegerState
&removeAssumedBits(base_t BitsEncoding
) {
1067 // Make sure we never loose any "known bits".
1068 Assumed
= (Assumed
& ~BitsEncoding
) | Known
;
1072 /// Keep only "assumed bits" also set in \p BitsEncoding but all known ones.
1073 IntegerState
&intersectAssumedBits(base_t BitsEncoding
) {
1074 // Make sure we never loose any "known bits".
1075 Assumed
= (Assumed
& BitsEncoding
) | Known
;
1079 /// Take minimum of assumed and \p Value.
1080 IntegerState
&takeAssumedMinimum(base_t Value
) {
1081 // Make sure we never loose "known value".
1082 Assumed
= std::max(std::min(Assumed
, Value
), Known
);
1086 /// Take maximum of known and \p Value.
1087 IntegerState
&takeKnownMaximum(base_t Value
) {
1088 // Make sure we never loose "known value".
1089 Assumed
= std::max(Value
, Assumed
);
1090 Known
= std::max(Value
, Known
);
1094 /// Equality for IntegerState.
1095 bool operator==(const IntegerState
&R
) const {
1096 return this->getAssumed() == R
.getAssumed() &&
1097 this->getKnown() == R
.getKnown();
1100 /// Inequality for IntegerState.
1101 bool operator!=(const IntegerState
&R
) const { return !(*this == R
); }
1103 /// "Clamp" this state with \p R. The result is the minimum of the assumed
1104 /// information but not less than what was known before.
1106 /// TODO: Consider replacing the operator with a call or using it only when
1107 /// we can also take the maximum of the known information, thus when
1108 /// \p R is not dependent on additional assumed state.
1109 IntegerState
operator^=(const IntegerState
&R
) {
1110 takeAssumedMinimum(R
.Assumed
);
1114 /// "Clamp" this state with \p R. The result is the maximum of the known
1115 /// information but not more than what was assumed before.
1116 IntegerState
operator+=(const IntegerState
&R
) {
1117 takeKnownMaximum(R
.Known
);
1121 /// Make this the minimum, known and assumed, of this state and \p R.
1122 IntegerState
operator&=(const IntegerState
&R
) {
1123 Known
= std::min(Known
, R
.Known
);
1124 Assumed
= std::min(Assumed
, R
.Assumed
);
1128 /// Make this the maximum, known and assumed, of this state and \p R.
1129 IntegerState
operator|=(const IntegerState
&R
) {
1130 Known
= std::max(Known
, R
.Known
);
1131 Assumed
= std::max(Assumed
, R
.Assumed
);
1136 /// The known state encoding in an integer of type base_t.
1137 base_t Known
= getWorstState();
1139 /// The assumed state encoding in an integer of type base_t.
1143 /// Simple wrapper for a single bit (boolean) state.
1144 struct BooleanState
: public IntegerState
{
1145 BooleanState() : IntegerState(1){};
1148 /// Helper struct necessary as the modular build fails if the virtual method
1149 /// IRAttribute::manifest is defined in the Attributor.cpp.
1150 struct IRAttributeManifest
{
1151 static ChangeStatus
manifestAttrs(Attributor
&A
, IRPosition
&IRP
,
1152 const ArrayRef
<Attribute
> &DeducedAttrs
);
1155 /// Helper to tie a abstract state implementation to an abstract attribute.
1156 template <typename StateTy
, typename Base
>
1157 struct StateWrapper
: public StateTy
, public Base
{
1158 /// Provide static access to the type of the state.
1159 using StateType
= StateTy
;
1161 /// See AbstractAttribute::getState(...).
1162 StateType
&getState() override
{ return *this; }
1164 /// See AbstractAttribute::getState(...).
1165 const AbstractState
&getState() const override
{ return *this; }
1168 /// Helper class that provides common functionality to manifest IR attributes.
1169 template <Attribute::AttrKind AK
, typename Base
>
1170 struct IRAttribute
: public IRPosition
, public Base
{
1171 IRAttribute(const IRPosition
&IRP
) : IRPosition(IRP
) {}
1174 /// See AbstractAttribute::initialize(...).
1175 virtual void initialize(Attributor
&A
) override
{
1176 if (hasAttr(getAttrKind())) {
1177 this->getState().indicateOptimisticFixpoint();
1181 const IRPosition
&IRP
= this->getIRPosition();
1182 bool IsFnInterface
= IRP
.isFnInterfaceKind();
1183 const Function
*FnScope
= IRP
.getAnchorScope();
1184 // TODO: Not all attributes require an exact definition. Find a way to
1185 // enable deduction for some but not all attributes in case the
1186 // definition might be changed at runtime, see also
1187 // http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html.
1188 // TODO: We could always determine abstract attributes and if sufficient
1189 // information was found we could duplicate the functions that do not
1190 // have an exact definition.
1191 if (IsFnInterface
&& (!FnScope
|| !FnScope
->hasExactDefinition()))
1192 this->getState().indicatePessimisticFixpoint();
1195 /// See AbstractAttribute::manifest(...).
1196 ChangeStatus
manifest(Attributor
&A
) override
{
1197 SmallVector
<Attribute
, 4> DeducedAttrs
;
1198 getDeducedAttributes(getAnchorValue().getContext(), DeducedAttrs
);
1199 return IRAttributeManifest::manifestAttrs(A
, getIRPosition(), DeducedAttrs
);
1202 /// Return the kind that identifies the abstract attribute implementation.
1203 Attribute::AttrKind
getAttrKind() const { return AK
; }
1205 /// Return the deduced attributes in \p Attrs.
1206 virtual void getDeducedAttributes(LLVMContext
&Ctx
,
1207 SmallVectorImpl
<Attribute
> &Attrs
) const {
1208 Attrs
.emplace_back(Attribute::get(Ctx
, getAttrKind()));
1211 /// Return an IR position, see struct IRPosition.
1214 IRPosition
&getIRPosition() override
{ return *this; }
1215 const IRPosition
&getIRPosition() const override
{ return *this; }
1219 /// Base struct for all "concrete attribute" deductions.
1221 /// The abstract attribute is a minimal interface that allows the Attributor to
1222 /// orchestrate the abstract/fixpoint analysis. The design allows to hide away
1223 /// implementation choices made for the subclasses but also to structure their
1224 /// implementation and simplify the use of other abstract attributes in-flight.
1226 /// To allow easy creation of new attributes, most methods have default
1227 /// implementations. The ones that do not are generally straight forward, except
1228 /// `AbstractAttribute::updateImpl` which is the location of most reasoning
1229 /// associated with the abstract attribute. The update is invoked by the
1230 /// Attributor in case the situation used to justify the current optimistic
1231 /// state might have changed. The Attributor determines this automatically
1232 /// by monitoring the `Attributor::getAAFor` calls made by abstract attributes.
1234 /// The `updateImpl` method should inspect the IR and other abstract attributes
1235 /// in-flight to justify the best possible (=optimistic) state. The actual
1236 /// implementation is, similar to the underlying abstract state encoding, not
1237 /// exposed. In the most common case, the `updateImpl` will go through a list of
1238 /// reasons why its optimistic state is valid given the current information. If
1239 /// any combination of them holds and is sufficient to justify the current
1240 /// optimistic state, the method shall return UNCHAGED. If not, the optimistic
1241 /// state is adjusted to the situation and the method shall return CHANGED.
1243 /// If the manifestation of the "concrete attribute" deduced by the subclass
1244 /// differs from the "default" behavior, which is a (set of) LLVM-IR
1245 /// attribute(s) for an argument, call site argument, function return value, or
1246 /// function, the `AbstractAttribute::manifest` method should be overloaded.
1248 /// NOTE: If the state obtained via getState() is INVALID, thus if
1249 /// AbstractAttribute::getState().isValidState() returns false, no
1250 /// information provided by the methods of this class should be used.
1251 /// NOTE: The Attributor currently has certain limitations to what we can do.
1252 /// As a general rule of thumb, "concrete" abstract attributes should *for
1253 /// now* only perform "backward" information propagation. That means
1254 /// optimistic information obtained through abstract attributes should
1255 /// only be used at positions that precede the origin of the information
1256 /// with regards to the program flow. More practically, information can
1257 /// *now* be propagated from instructions to their enclosing function, but
1258 /// *not* from call sites to the called function. The mechanisms to allow
1259 /// both directions will be added in the future.
1260 /// NOTE: The mechanics of adding a new "concrete" abstract attribute are
1261 /// described in the file comment.
1262 struct AbstractAttribute
{
1263 using StateType
= AbstractState
;
1265 /// Virtual destructor.
1266 virtual ~AbstractAttribute() {}
1268 /// Initialize the state with the information in the Attributor \p A.
1270 /// This function is called by the Attributor once all abstract attributes
1271 /// have been identified. It can and shall be used for task like:
1272 /// - identify existing knowledge in the IR and use it for the "known state"
1273 /// - perform any work that is not going to change over time, e.g., determine
1274 /// a subset of the IR, or attributes in-flight, that have to be looked at
1275 /// in the `updateImpl` method.
1276 virtual void initialize(Attributor
&A
) {}
1278 /// Return the internal abstract state for inspection.
1279 virtual StateType
&getState() = 0;
1280 virtual const StateType
&getState() const = 0;
1282 /// Return an IR position, see struct IRPosition.
1283 virtual const IRPosition
&getIRPosition() const = 0;
1285 /// Helper functions, for debug purposes only.
1287 virtual void print(raw_ostream
&OS
) const;
1288 void dump() const { print(dbgs()); }
1290 /// This function should return the "summarized" assumed state as string.
1291 virtual const std::string
getAsStr() const = 0;
1294 /// Allow the Attributor access to the protected methods.
1295 friend struct Attributor
;
1298 /// Hook for the Attributor to trigger an update of the internal state.
1300 /// If this attribute is already fixed, this method will return UNCHANGED,
1301 /// otherwise it delegates to `AbstractAttribute::updateImpl`.
1303 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
1304 ChangeStatus
update(Attributor
&A
);
1306 /// Hook for the Attributor to trigger the manifestation of the information
1307 /// represented by the abstract attribute in the LLVM-IR.
1309 /// \Return CHANGED if the IR was altered, otherwise UNCHANGED.
1310 virtual ChangeStatus
manifest(Attributor
&A
) {
1311 return ChangeStatus::UNCHANGED
;
1314 /// Hook to enable custom statistic tracking, called after manifest that
1315 /// resulted in a change if statistics are enabled.
1317 /// We require subclasses to provide an implementation so we remember to
1318 /// add statistics for them.
1319 virtual void trackStatistics() const = 0;
1321 /// Return an IR position, see struct IRPosition.
1322 virtual IRPosition
&getIRPosition() = 0;
1324 /// The actual update/transfer function which has to be implemented by the
1325 /// derived classes.
1327 /// If it is called, the environment has changed and we have to determine if
1328 /// the current information is still valid or adjust it otherwise.
1330 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
1331 virtual ChangeStatus
updateImpl(Attributor
&A
) = 0;
1334 /// Forward declarations of output streams for debug purposes.
1337 raw_ostream
&operator<<(raw_ostream
&OS
, const AbstractAttribute
&AA
);
1338 raw_ostream
&operator<<(raw_ostream
&OS
, ChangeStatus S
);
1339 raw_ostream
&operator<<(raw_ostream
&OS
, IRPosition::Kind
);
1340 raw_ostream
&operator<<(raw_ostream
&OS
, const IRPosition
&);
1341 raw_ostream
&operator<<(raw_ostream
&OS
, const AbstractState
&State
);
1342 raw_ostream
&operator<<(raw_ostream
&OS
, const IntegerState
&S
);
1345 struct AttributorPass
: public PassInfoMixin
<AttributorPass
> {
1346 PreservedAnalyses
run(Module
&M
, ModuleAnalysisManager
&AM
);
1349 Pass
*createAttributorLegacyPass();
1351 /// ----------------------------------------------------------------------------
1352 /// Abstract Attribute Classes
1353 /// ----------------------------------------------------------------------------
1355 /// An abstract attribute for the returned values of a function.
1356 struct AAReturnedValues
1357 : public IRAttribute
<Attribute::Returned
, AbstractAttribute
> {
1358 AAReturnedValues(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1360 /// Return an assumed unique return value if a single candidate is found. If
1361 /// there cannot be one, return a nullptr. If it is not clear yet, return the
1362 /// Optional::NoneType.
1363 Optional
<Value
*> getAssumedUniqueReturnValue(Attributor
&A
) const;
1365 /// Check \p Pred on all returned values.
1367 /// This method will evaluate \p Pred on returned values and return
1368 /// true if (1) all returned values are known, and (2) \p Pred returned true
1369 /// for all returned values.
1371 /// Note: Unlike the Attributor::checkForAllReturnedValuesAndReturnInsts
1372 /// method, this one will not filter dead return instructions.
1373 virtual bool checkForAllReturnedValuesAndReturnInsts(
1374 const function_ref
<bool(Value
&, const SmallSetVector
<ReturnInst
*, 4> &)>
1377 using iterator
= MapVector
<Value
*, SmallSetVector
<ReturnInst
*, 4>>::iterator
;
1378 using const_iterator
=
1379 MapVector
<Value
*, SmallSetVector
<ReturnInst
*, 4>>::const_iterator
;
1380 virtual llvm::iterator_range
<iterator
> returned_values() = 0;
1381 virtual llvm::iterator_range
<const_iterator
> returned_values() const = 0;
1383 virtual size_t getNumReturnValues() const = 0;
1384 virtual const SmallSetVector
<CallBase
*, 4> &getUnresolvedCalls() const = 0;
1386 /// Create an abstract attribute view for the position \p IRP.
1387 static AAReturnedValues
&createForPosition(const IRPosition
&IRP
,
1390 /// Unique ID (due to the unique address)
1391 static const char ID
;
1395 : public IRAttribute
<Attribute::NoUnwind
,
1396 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1397 AANoUnwind(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1399 /// Returns true if nounwind is assumed.
1400 bool isAssumedNoUnwind() const { return getAssumed(); }
1402 /// Returns true if nounwind is known.
1403 bool isKnownNoUnwind() const { return getKnown(); }
1405 /// Create an abstract attribute view for the position \p IRP.
1406 static AANoUnwind
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1408 /// Unique ID (due to the unique address)
1409 static const char ID
;
1413 : public IRAttribute
<Attribute::NoSync
,
1414 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1415 AANoSync(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1417 /// Returns true if "nosync" is assumed.
1418 bool isAssumedNoSync() const { return getAssumed(); }
1420 /// Returns true if "nosync" is known.
1421 bool isKnownNoSync() const { return getKnown(); }
1423 /// Create an abstract attribute view for the position \p IRP.
1424 static AANoSync
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1426 /// Unique ID (due to the unique address)
1427 static const char ID
;
1430 /// An abstract interface for all nonnull attributes.
1432 : public IRAttribute
<Attribute::NonNull
,
1433 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1434 AANonNull(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1436 /// Return true if we assume that the underlying value is nonnull.
1437 bool isAssumedNonNull() const { return getAssumed(); }
1439 /// Return true if we know that underlying value is nonnull.
1440 bool isKnownNonNull() const { return getKnown(); }
1442 /// Create an abstract attribute view for the position \p IRP.
1443 static AANonNull
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1445 /// Unique ID (due to the unique address)
1446 static const char ID
;
1449 /// An abstract attribute for norecurse.
1451 : public IRAttribute
<Attribute::NoRecurse
,
1452 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1453 AANoRecurse(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1455 /// Return true if "norecurse" is assumed.
1456 bool isAssumedNoRecurse() const { return getAssumed(); }
1458 /// Return true if "norecurse" is known.
1459 bool isKnownNoRecurse() const { return getKnown(); }
1461 /// Create an abstract attribute view for the position \p IRP.
1462 static AANoRecurse
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1464 /// Unique ID (due to the unique address)
1465 static const char ID
;
1468 /// An abstract attribute for willreturn.
1470 : public IRAttribute
<Attribute::WillReturn
,
1471 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1472 AAWillReturn(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1474 /// Return true if "willreturn" is assumed.
1475 bool isAssumedWillReturn() const { return getAssumed(); }
1477 /// Return true if "willreturn" is known.
1478 bool isKnownWillReturn() const { return getKnown(); }
1480 /// Create an abstract attribute view for the position \p IRP.
1481 static AAWillReturn
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1483 /// Unique ID (due to the unique address)
1484 static const char ID
;
1487 /// An abstract interface for all noalias attributes.
1489 : public IRAttribute
<Attribute::NoAlias
,
1490 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1491 AANoAlias(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1493 /// Return true if we assume that the underlying value is alias.
1494 bool isAssumedNoAlias() const { return getAssumed(); }
1496 /// Return true if we know that underlying value is noalias.
1497 bool isKnownNoAlias() const { return getKnown(); }
1499 /// Create an abstract attribute view for the position \p IRP.
1500 static AANoAlias
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1502 /// Unique ID (due to the unique address)
1503 static const char ID
;
1506 /// An AbstractAttribute for nofree.
1508 : public IRAttribute
<Attribute::NoFree
,
1509 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1510 AANoFree(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1512 /// Return true if "nofree" is assumed.
1513 bool isAssumedNoFree() const { return getAssumed(); }
1515 /// Return true if "nofree" is known.
1516 bool isKnownNoFree() const { return getKnown(); }
1518 /// Create an abstract attribute view for the position \p IRP.
1519 static AANoFree
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1521 /// Unique ID (due to the unique address)
1522 static const char ID
;
1525 /// An AbstractAttribute for noreturn.
1527 : public IRAttribute
<Attribute::NoReturn
,
1528 StateWrapper
<BooleanState
, AbstractAttribute
>> {
1529 AANoReturn(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1531 /// Return true if the underlying object is assumed to never return.
1532 bool isAssumedNoReturn() const { return getAssumed(); }
1534 /// Return true if the underlying object is known to never return.
1535 bool isKnownNoReturn() const { return getKnown(); }
1537 /// Create an abstract attribute view for the position \p IRP.
1538 static AANoReturn
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1540 /// Unique ID (due to the unique address)
1541 static const char ID
;
1544 /// An abstract interface for liveness abstract attribute.
1545 struct AAIsDead
: public StateWrapper
<BooleanState
, AbstractAttribute
>,
1547 AAIsDead(const IRPosition
&IRP
) : IRPosition(IRP
) {}
1549 /// Returns true if \p BB is assumed dead.
1550 virtual bool isAssumedDead(const BasicBlock
*BB
) const = 0;
1552 /// Returns true if \p BB is known dead.
1553 virtual bool isKnownDead(const BasicBlock
*BB
) const = 0;
1555 /// Returns true if \p I is assumed dead.
1556 virtual bool isAssumedDead(const Instruction
*I
) const = 0;
1558 /// Returns true if \p I is known dead.
1559 virtual bool isKnownDead(const Instruction
*I
) const = 0;
1561 /// This method is used to check if at least one instruction in a collection
1562 /// of instructions is live.
1563 template <typename T
> bool isLiveInstSet(T begin
, T end
) const {
1564 for (const auto &I
: llvm::make_range(begin
, end
)) {
1565 assert(I
->getFunction() == getIRPosition().getAssociatedFunction() &&
1566 "Instruction must be in the same anchor scope function.");
1568 if (!isAssumedDead(I
))
1575 /// Return an IR position, see struct IRPosition.
1578 IRPosition
&getIRPosition() override
{ return *this; }
1579 const IRPosition
&getIRPosition() const override
{ return *this; }
1582 /// Create an abstract attribute view for the position \p IRP.
1583 static AAIsDead
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1585 /// Unique ID (due to the unique address)
1586 static const char ID
;
1589 /// State for dereferenceable attribute
1590 struct DerefState
: AbstractState
{
1592 /// State representing for dereferenceable bytes.
1593 IntegerState DerefBytesState
;
1595 /// State representing that whether the value is globaly dereferenceable.
1596 BooleanState GlobalState
;
1598 /// See AbstractState::isValidState()
1599 bool isValidState() const override
{ return DerefBytesState
.isValidState(); }
1601 /// See AbstractState::isAtFixpoint()
1602 bool isAtFixpoint() const override
{
1603 return !isValidState() ||
1604 (DerefBytesState
.isAtFixpoint() && GlobalState
.isAtFixpoint());
1607 /// See AbstractState::indicateOptimisticFixpoint(...)
1608 ChangeStatus
indicateOptimisticFixpoint() override
{
1609 DerefBytesState
.indicateOptimisticFixpoint();
1610 GlobalState
.indicateOptimisticFixpoint();
1611 return ChangeStatus::UNCHANGED
;
1614 /// See AbstractState::indicatePessimisticFixpoint(...)
1615 ChangeStatus
indicatePessimisticFixpoint() override
{
1616 DerefBytesState
.indicatePessimisticFixpoint();
1617 GlobalState
.indicatePessimisticFixpoint();
1618 return ChangeStatus::CHANGED
;
1621 /// Update known dereferenceable bytes.
1622 void takeKnownDerefBytesMaximum(uint64_t Bytes
) {
1623 DerefBytesState
.takeKnownMaximum(Bytes
);
1626 /// Update assumed dereferenceable bytes.
1627 void takeAssumedDerefBytesMinimum(uint64_t Bytes
) {
1628 DerefBytesState
.takeAssumedMinimum(Bytes
);
1631 /// Equality for DerefState.
1632 bool operator==(const DerefState
&R
) {
1633 return this->DerefBytesState
== R
.DerefBytesState
&&
1634 this->GlobalState
== R
.GlobalState
;
1637 /// Inequality for IntegerState.
1638 bool operator!=(const DerefState
&R
) { return !(*this == R
); }
1640 /// See IntegerState::operator^=
1641 DerefState
operator^=(const DerefState
&R
) {
1642 DerefBytesState
^= R
.DerefBytesState
;
1643 GlobalState
^= R
.GlobalState
;
1647 /// See IntegerState::operator+=
1648 DerefState
operator+=(const DerefState
&R
) {
1649 DerefBytesState
+= R
.DerefBytesState
;
1650 GlobalState
+= R
.GlobalState
;
1654 /// See IntegerState::operator&=
1655 DerefState
operator&=(const DerefState
&R
) {
1656 DerefBytesState
&= R
.DerefBytesState
;
1657 GlobalState
&= R
.GlobalState
;
1661 /// See IntegerState::operator|=
1662 DerefState
operator|=(const DerefState
&R
) {
1663 DerefBytesState
|= R
.DerefBytesState
;
1664 GlobalState
|= R
.GlobalState
;
1669 const AANonNull
*NonNullAA
= nullptr;
1672 /// An abstract interface for all dereferenceable attribute.
1673 struct AADereferenceable
1674 : public IRAttribute
<Attribute::Dereferenceable
,
1675 StateWrapper
<DerefState
, AbstractAttribute
>> {
1676 AADereferenceable(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1678 /// Return true if we assume that the underlying value is nonnull.
1679 bool isAssumedNonNull() const {
1680 return NonNullAA
&& NonNullAA
->isAssumedNonNull();
1683 /// Return true if we assume that underlying value is
1684 /// dereferenceable(_or_null) globally.
1685 bool isAssumedGlobal() const { return GlobalState
.getAssumed(); }
1687 /// Return true if we know that underlying value is
1688 /// dereferenceable(_or_null) globally.
1689 bool isKnownGlobal() const { return GlobalState
.getKnown(); }
1691 /// Return assumed dereferenceable bytes.
1692 uint32_t getAssumedDereferenceableBytes() const {
1693 return DerefBytesState
.getAssumed();
1696 /// Return known dereferenceable bytes.
1697 uint32_t getKnownDereferenceableBytes() const {
1698 return DerefBytesState
.getKnown();
1701 /// Create an abstract attribute view for the position \p IRP.
1702 static AADereferenceable
&createForPosition(const IRPosition
&IRP
,
1705 /// Unique ID (due to the unique address)
1706 static const char ID
;
1709 /// An abstract interface for all align attributes.
1711 : public IRAttribute
<Attribute::Alignment
,
1712 StateWrapper
<IntegerState
, AbstractAttribute
>> {
1713 AAAlign(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1715 /// Return assumed alignment.
1716 unsigned getAssumedAlign() const { return getAssumed(); }
1718 /// Return known alignemnt.
1719 unsigned getKnownAlign() const { return getKnown(); }
1721 /// Create an abstract attribute view for the position \p IRP.
1722 static AAAlign
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1724 /// Unique ID (due to the unique address)
1725 static const char ID
;
1728 /// An abstract interface for all nocapture attributes.
1730 : public IRAttribute
<Attribute::NoCapture
,
1731 StateWrapper
<IntegerState
, AbstractAttribute
>> {
1732 AANoCapture(const IRPosition
&IRP
) : IRAttribute(IRP
) {}
1734 /// State encoding bits. A set bit in the state means the property holds.
1735 /// NO_CAPTURE is the best possible state, 0 the worst possible state.
1737 NOT_CAPTURED_IN_MEM
= 1 << 0,
1738 NOT_CAPTURED_IN_INT
= 1 << 1,
1739 NOT_CAPTURED_IN_RET
= 1 << 2,
1741 /// If we do not capture the value in memory or through integers we can only
1742 /// communicate it back as a derived pointer.
1743 NO_CAPTURE_MAYBE_RETURNED
= NOT_CAPTURED_IN_MEM
| NOT_CAPTURED_IN_INT
,
1745 /// If we do not capture the value in memory, through integers, or as a
1746 /// derived pointer we know it is not captured.
1748 NOT_CAPTURED_IN_MEM
| NOT_CAPTURED_IN_INT
| NOT_CAPTURED_IN_RET
,
1751 /// Return true if we know that the underlying value is not captured in its
1752 /// respective scope.
1753 bool isKnownNoCapture() const { return isKnown(NO_CAPTURE
); }
1755 /// Return true if we assume that the underlying value is not captured in its
1756 /// respective scope.
1757 bool isAssumedNoCapture() const { return isAssumed(NO_CAPTURE
); }
1759 /// Return true if we know that the underlying value is not captured in its
1760 /// respective scope but we allow it to escape through a "return".
1761 bool isKnownNoCaptureMaybeReturned() const {
1762 return isKnown(NO_CAPTURE_MAYBE_RETURNED
);
1765 /// Return true if we assume that the underlying value is not captured in its
1766 /// respective scope but we allow it to escape through a "return".
1767 bool isAssumedNoCaptureMaybeReturned() const {
1768 return isAssumed(NO_CAPTURE_MAYBE_RETURNED
);
1771 /// Create an abstract attribute view for the position \p IRP.
1772 static AANoCapture
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1774 /// Unique ID (due to the unique address)
1775 static const char ID
;
1778 /// An abstract interface for value simplify abstract attribute.
1779 struct AAValueSimplify
: public StateWrapper
<BooleanState
, AbstractAttribute
>,
1781 AAValueSimplify(const IRPosition
&IRP
) : IRPosition(IRP
) {}
1783 /// Return an IR position, see struct IRPosition.
1786 IRPosition
&getIRPosition() { return *this; }
1787 const IRPosition
&getIRPosition() const { return *this; }
1790 /// Return an assumed simplified value if a single candidate is found. If
1791 /// there cannot be one, return original value. If it is not clear yet, return
1792 /// the Optional::NoneType.
1793 virtual Optional
<Value
*> getAssumedSimplifiedValue(Attributor
&A
) const = 0;
1795 /// Create an abstract attribute view for the position \p IRP.
1796 static AAValueSimplify
&createForPosition(const IRPosition
&IRP
,
1799 /// Unique ID (due to the unique address)
1800 static const char ID
;
1803 struct AAHeapToStack
: public StateWrapper
<BooleanState
, AbstractAttribute
>,
1805 AAHeapToStack(const IRPosition
&IRP
) : IRPosition(IRP
) {}
1807 /// Returns true if HeapToStack conversion is assumed to be possible.
1808 bool isAssumedHeapToStack() const { return getAssumed(); }
1810 /// Returns true if HeapToStack conversion is known to be possible.
1811 bool isKnownHeapToStack() const { return getKnown(); }
1813 /// Return an IR position, see struct IRPosition.
1816 IRPosition
&getIRPosition() { return *this; }
1817 const IRPosition
&getIRPosition() const { return *this; }
1820 /// Create an abstract attribute view for the position \p IRP.
1821 static AAHeapToStack
&createForPosition(const IRPosition
&IRP
, Attributor
&A
);
1823 /// Unique ID (due to the unique address)
1824 static const char ID
;
1827 } // end namespace llvm
1829 #endif // LLVM_TRANSFORMS_IPO_FUNCTIONATTRS_H