/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Transforms/IPO/Attributor.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Attributor.h --- Module-wide attribute deduction ---------*- C++ -*-===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // Attributor: An inter procedural (abstract) "attribute" deduction framework. |
10 | | // |
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. |
14 | | // |
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. |
19 | | // |
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. |
31 | | // |
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. |
36 | | // |
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. |
49 | | // |
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. |
61 | | // |
62 | | // Attribute manifestation is not mandatory. If desired, there is support to |
63 | | // generate a single LLVM-IR attribute already in the AbstractAttribute base |
64 | | // class. In the simplest case, a subclass overloads |
65 | | // `AbstractAttribute::getManifestPosition()` and |
66 | | // `AbstractAttribute::getAttrKind()` to return the appropriate values. The |
67 | | // Attributor manifestation framework will then create and place a new attribute |
68 | | // if it is allowed to do so (based on the abstract state). Other use cases can |
69 | | // be achieved by overloading other abstract attribute methods. |
70 | | // |
71 | | // |
72 | | // The "mechanics" of adding a new "abstract attribute": |
73 | | // - Define a class (transitively) inheriting from AbstractAttribute and one |
74 | | // (which could be the same) that (transitively) inherits from AbstractState. |
75 | | // For the latter, consider the already available BooleanState and |
76 | | // IntegerState if they fit your needs, e.g., you require only a bit-encoding. |
77 | | // - Implement all pure methods. Also use overloading if the attribute is not |
78 | | // conforming with the "default" behavior: A (set of) LLVM-IR attribute(s) for |
79 | | // an argument, call site argument, function return value, or function. See |
80 | | // the class and method descriptions for more information on the two |
81 | | // "Abstract" classes and their respective methods. |
82 | | // - Register opportunities for the new abstract attribute in the |
83 | | // `Attributor::identifyDefaultAbstractAttributes` method if it should be |
84 | | // counted as a 'default' attribute. |
85 | | // - Add sufficient tests. |
86 | | // - Add a Statistics object for bookkeeping. If it is a simple (set of) |
87 | | // attribute(s) manifested through the Attributor manifestation framework, see |
88 | | // the bookkeeping function in Attributor.cpp. |
89 | | // - If instructions with a certain opcode are interesting to the attribute, add |
90 | | // that opcode to the switch in `Attributor::identifyAbstractAttributes`. This |
91 | | // will make it possible to query all those instructions through the |
92 | | // `InformationCache::getOpcodeInstMapForFunction` interface and eliminate the |
93 | | // need to traverse the IR repeatedly. |
94 | | // |
95 | | //===----------------------------------------------------------------------===// |
96 | | |
97 | | #ifndef LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H |
98 | | #define LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H |
99 | | |
100 | | #include "llvm/Analysis/LazyCallGraph.h" |
101 | | #include "llvm/IR/CallSite.h" |
102 | | #include "llvm/IR/PassManager.h" |
103 | | |
104 | | namespace llvm { |
105 | | |
106 | | struct AbstractAttribute; |
107 | | struct InformationCache; |
108 | | |
109 | | class Function; |
110 | | |
111 | | /// Simple enum class that forces the status to be spelled out explicitly. |
112 | | /// |
113 | | ///{ |
114 | | enum class ChangeStatus { |
115 | | CHANGED, |
116 | | UNCHANGED, |
117 | | }; |
118 | | |
119 | | ChangeStatus operator|(ChangeStatus l, ChangeStatus r); |
120 | | ChangeStatus operator&(ChangeStatus l, ChangeStatus r); |
121 | | ///} |
122 | | |
123 | | /// The fixpoint analysis framework that orchestrates the attribute deduction. |
124 | | /// |
125 | | /// The Attributor provides a general abstract analysis framework (guided |
126 | | /// fixpoint iteration) as well as helper functions for the deduction of |
127 | | /// (LLVM-IR) attributes. However, also other code properties can be deduced, |
128 | | /// propagated, and ultimately manifested through the Attributor framework. This |
129 | | /// is particularly useful if these properties interact with attributes and a |
130 | | /// co-scheduled deduction allows to improve the solution. Even if not, thus if |
131 | | /// attributes/properties are completely isolated, they should use the |
132 | | /// Attributor framework to reduce the number of fixpoint iteration frameworks |
133 | | /// in the code base. Note that the Attributor design makes sure that isolated |
134 | | /// attributes are not impacted, in any way, by others derived at the same time |
135 | | /// if there is no cross-reasoning performed. |
136 | | /// |
137 | | /// The public facing interface of the Attributor is kept simple and basically |
138 | | /// allows abstract attributes to one thing, query abstract attributes |
139 | | /// in-flight. There are two reasons to do this: |
140 | | /// a) The optimistic state of one abstract attribute can justify an |
141 | | /// optimistic state of another, allowing to framework to end up with an |
142 | | /// optimistic (=best possible) fixpoint instead of one based solely on |
143 | | /// information in the IR. |
144 | | /// b) This avoids reimplementing various kinds of lookups, e.g., to check |
145 | | /// for existing IR attributes, in favor of a single lookups interface |
146 | | /// provided by an abstract attribute subclass. |
147 | | /// |
148 | | /// NOTE: The mechanics of adding a new "concrete" abstract attribute are |
149 | | /// described in the file comment. |
150 | | struct Attributor { |
151 | 16 | ~Attributor() { DeleteContainerPointers(AllAbstractAttributes); } |
152 | | |
153 | | /// Run the analyses until a fixpoint is reached or enforced (timeout). |
154 | | /// |
155 | | /// The attributes registered with this Attributor can be used after as long |
156 | | /// as the Attributor is not destroyed (it owns the attributes now). |
157 | | /// |
158 | | /// \Returns CHANGED if the IR was changed, otherwise UNCHANGED. |
159 | | ChangeStatus run(); |
160 | | |
161 | | /// Lookup an abstract attribute of type \p AAType anchored at value \p V and |
162 | | /// argument number \p ArgNo. If no attribute is found and \p V is a call base |
163 | | /// instruction, the called function is tried as a value next. Thus, the |
164 | | /// returned abstract attribute might be anchored at the callee of \p V. |
165 | | /// |
166 | | /// This method is the only (supported) way an abstract attribute can retrieve |
167 | | /// information from another abstract attribute. As an example, take an |
168 | | /// abstract attribute that determines the memory access behavior for a |
169 | | /// argument (readnone, readonly, ...). It should use `getAAFor` to get the |
170 | | /// most optimistic information for other abstract attributes in-flight, e.g. |
171 | | /// the one reasoning about the "captured" state for the argument or the one |
172 | | /// reasoning on the memory access behavior of the function as a whole. |
173 | | template <typename AAType> |
174 | | const AAType *getAAFor(AbstractAttribute &QueryingAA, const Value &V, |
175 | 9.61k | int ArgNo = -1) { |
176 | 9.61k | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, |
177 | 9.61k | "Cannot query an attribute with a type not derived from " |
178 | 9.61k | "'AbstractAttribute'!"); |
179 | 9.61k | assert(AAType::ID != Attribute::None && |
180 | 9.61k | "Cannot lookup generic abstract attributes!"); |
181 | 9.61k | |
182 | 9.61k | // Determine the argument number automatically for llvm::Arguments if none |
183 | 9.61k | // is set. Do not override a given one as it could be a use of the argument |
184 | 9.61k | // in a call site. |
185 | 9.61k | if (ArgNo == -1) |
186 | 6.94k | if (auto *Arg = dyn_cast<Argument>(&V)) |
187 | 1.79k | ArgNo = Arg->getArgNo(); |
188 | 9.61k | |
189 | 9.61k | // If a function was given together with an argument number, perform the |
190 | 9.61k | // lookup for the actual argument instead. Don't do it for variadic |
191 | 9.61k | // arguments. |
192 | 9.61k | if (ArgNo >= 0 && isa<Function>(&V)4.45k && |
193 | 9.61k | cast<Function>(&V)->arg_size() > (size_t)ArgNo872 ) |
194 | 872 | return getAAFor<AAType>( |
195 | 872 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); |
196 | 8.73k | |
197 | 8.73k | // Lookup the abstract attribute of type AAType. If found, return it after |
198 | 8.73k | // registering a dependence of QueryingAA on the one returned attribute. |
199 | 8.73k | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); |
200 | 8.73k | if (AAType *AA = static_cast<AAType *>( |
201 | 5.26k | KindToAbstractAttributeMap.lookup(AAType::ID))) { |
202 | 5.26k | // Do not return an attribute with an invalid state. This minimizes checks |
203 | 5.26k | // at the calls sites and allows the fallback below to kick in. |
204 | 5.26k | if (AA->getState().isValidState()) { |
205 | 2.12k | QueryMap[AA].insert(&QueryingAA); |
206 | 2.12k | return AA; |
207 | 2.12k | } |
208 | 6.61k | } |
209 | 6.61k | |
210 | 6.61k | // If no abstract attribute was found and we look for a call site argument, |
211 | 6.61k | // defer to the actual argument instead. |
212 | 6.61k | ImmutableCallSite ICS(&V); |
213 | 6.61k | if (ICS && ICS.getCalledValue()2.72k ) |
214 | 2.72k | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); |
215 | 3.89k | |
216 | 3.89k | // No matching attribute found |
217 | 3.89k | return nullptr; |
218 | 3.89k | } llvm::AANoUnwind const* llvm::Attributor::getAAFor<llvm::AANoUnwind>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 150 | int ArgNo = -1) { | 176 | 150 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 150 | "Cannot query an attribute with a type not derived from " | 178 | 150 | "'AbstractAttribute'!"); | 179 | 150 | assert(AAType::ID != Attribute::None && | 180 | 150 | "Cannot lookup generic abstract attributes!"); | 181 | 150 | | 182 | 150 | // Determine the argument number automatically for llvm::Arguments if none | 183 | 150 | // is set. Do not override a given one as it could be a use of the argument | 184 | 150 | // in a call site. | 185 | 150 | if (ArgNo == -1) | 186 | 150 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 0 | ArgNo = Arg->getArgNo(); | 188 | 150 | | 189 | 150 | // If a function was given together with an argument number, perform the | 190 | 150 | // lookup for the actual argument instead. Don't do it for variadic | 191 | 150 | // arguments. | 192 | 150 | if (ArgNo >= 0 && isa<Function>(&V)0 && | 193 | 150 | cast<Function>(&V)->arg_size() > (size_t)ArgNo0 ) | 194 | 0 | return getAAFor<AAType>( | 195 | 0 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 150 | | 197 | 150 | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 150 | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 150 | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 150 | if (AAType *AA = static_cast<AAType *>( | 201 | 17 | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 17 | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 17 | // at the calls sites and allows the fallback below to kick in. | 204 | 17 | if (AA->getState().isValidState()) { | 205 | 16 | QueryMap[AA].insert(&QueryingAA); | 206 | 16 | return AA; | 207 | 16 | } | 208 | 134 | } | 209 | 134 | | 210 | 134 | // If no abstract attribute was found and we look for a call site argument, | 211 | 134 | // defer to the actual argument instead. | 212 | 134 | ImmutableCallSite ICS(&V); | 213 | 134 | if (ICS && ICS.getCalledValue()75 ) | 214 | 75 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 59 | | 216 | 59 | // No matching attribute found | 217 | 59 | return nullptr; | 218 | 59 | } |
AAReturnedValuesImpl const* llvm::Attributor::getAAFor<AAReturnedValuesImpl>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 737 | int ArgNo = -1) { | 176 | 737 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 737 | "Cannot query an attribute with a type not derived from " | 178 | 737 | "'AbstractAttribute'!"); | 179 | 737 | assert(AAType::ID != Attribute::None && | 180 | 737 | "Cannot lookup generic abstract attributes!"); | 181 | 737 | | 182 | 737 | // Determine the argument number automatically for llvm::Arguments if none | 183 | 737 | // is set. Do not override a given one as it could be a use of the argument | 184 | 737 | // in a call site. | 185 | 737 | if (ArgNo == -1) | 186 | 737 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 0 | ArgNo = Arg->getArgNo(); | 188 | 737 | | 189 | 737 | // If a function was given together with an argument number, perform the | 190 | 737 | // lookup for the actual argument instead. Don't do it for variadic | 191 | 737 | // arguments. | 192 | 737 | if (ArgNo >= 0 && isa<Function>(&V)0 && | 193 | 737 | cast<Function>(&V)->arg_size() > (size_t)ArgNo0 ) | 194 | 0 | return getAAFor<AAType>( | 195 | 0 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 737 | | 197 | 737 | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 737 | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 737 | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 737 | if (AAType *AA = static_cast<AAType *>( | 201 | 393 | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 393 | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 393 | // at the calls sites and allows the fallback below to kick in. | 204 | 393 | if (AA->getState().isValidState()) { | 205 | 393 | QueryMap[AA].insert(&QueryingAA); | 206 | 393 | return AA; | 207 | 393 | } | 208 | 344 | } | 209 | 344 | | 210 | 344 | // If no abstract attribute was found and we look for a call site argument, | 211 | 344 | // defer to the actual argument instead. | 212 | 344 | ImmutableCallSite ICS(&V); | 213 | 344 | if (ICS && ICS.getCalledValue()308 ) | 214 | 308 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 36 | | 216 | 36 | // No matching attribute found | 217 | 36 | return nullptr; | 218 | 36 | } |
AANoSyncFunction const* llvm::Attributor::getAAFor<AANoSyncFunction>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 612 | int ArgNo = -1) { | 176 | 612 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 612 | "Cannot query an attribute with a type not derived from " | 178 | 612 | "'AbstractAttribute'!"); | 179 | 612 | assert(AAType::ID != Attribute::None && | 180 | 612 | "Cannot lookup generic abstract attributes!"); | 181 | 612 | | 182 | 612 | // Determine the argument number automatically for llvm::Arguments if none | 183 | 612 | // is set. Do not override a given one as it could be a use of the argument | 184 | 612 | // in a call site. | 185 | 612 | if (ArgNo == -1) | 186 | 612 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 0 | ArgNo = Arg->getArgNo(); | 188 | 612 | | 189 | 612 | // If a function was given together with an argument number, perform the | 190 | 612 | // lookup for the actual argument instead. Don't do it for variadic | 191 | 612 | // arguments. | 192 | 612 | if (ArgNo >= 0 && isa<Function>(&V)0 && | 193 | 612 | cast<Function>(&V)->arg_size() > (size_t)ArgNo0 ) | 194 | 0 | return getAAFor<AAType>( | 195 | 0 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 612 | | 197 | 612 | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 612 | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 612 | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 612 | if (AAType *AA = static_cast<AAType *>( | 201 | 185 | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 185 | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 185 | // at the calls sites and allows the fallback below to kick in. | 204 | 185 | if (AA->getState().isValidState()) { | 205 | 180 | QueryMap[AA].insert(&QueryingAA); | 206 | 180 | return AA; | 207 | 180 | } | 208 | 432 | } | 209 | 432 | | 210 | 432 | // If no abstract attribute was found and we look for a call site argument, | 211 | 432 | // defer to the actual argument instead. | 212 | 432 | ImmutableCallSite ICS(&V); | 213 | 432 | if (ICS && ICS.getCalledValue()272 ) | 214 | 272 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 160 | | 216 | 160 | // No matching attribute found | 217 | 160 | return nullptr; | 218 | 160 | } |
AANoFreeFunction const* llvm::Attributor::getAAFor<AANoFreeFunction>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 742 | int ArgNo = -1) { | 176 | 742 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 742 | "Cannot query an attribute with a type not derived from " | 178 | 742 | "'AbstractAttribute'!"); | 179 | 742 | assert(AAType::ID != Attribute::None && | 180 | 742 | "Cannot lookup generic abstract attributes!"); | 181 | 742 | | 182 | 742 | // Determine the argument number automatically for llvm::Arguments if none | 183 | 742 | // is set. Do not override a given one as it could be a use of the argument | 184 | 742 | // in a call site. | 185 | 742 | if (ArgNo == -1) | 186 | 742 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 0 | ArgNo = Arg->getArgNo(); | 188 | 742 | | 189 | 742 | // If a function was given together with an argument number, perform the | 190 | 742 | // lookup for the actual argument instead. Don't do it for variadic | 191 | 742 | // arguments. | 192 | 742 | if (ArgNo >= 0 && isa<Function>(&V)0 && | 193 | 742 | cast<Function>(&V)->arg_size() > (size_t)ArgNo0 ) | 194 | 0 | return getAAFor<AAType>( | 195 | 0 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 742 | | 197 | 742 | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 742 | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 742 | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 742 | if (AAType *AA = static_cast<AAType *>( | 201 | 277 | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 277 | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 277 | // at the calls sites and allows the fallback below to kick in. | 204 | 277 | if (AA->getState().isValidState()) { | 205 | 274 | QueryMap[AA].insert(&QueryingAA); | 206 | 274 | return AA; | 207 | 274 | } | 208 | 468 | } | 209 | 468 | | 210 | 468 | // If no abstract attribute was found and we look for a call site argument, | 211 | 468 | // defer to the actual argument instead. | 212 | 468 | ImmutableCallSite ICS(&V); | 213 | 468 | if (ICS && ICS.getCalledValue()371 ) | 214 | 371 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 97 | | 216 | 97 | // No matching attribute found | 217 | 97 | return nullptr; | 218 | 97 | } |
llvm::AANonNull const* llvm::Attributor::getAAFor<llvm::AANonNull>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 3.31k | int ArgNo = -1) { | 176 | 3.31k | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 3.31k | "Cannot query an attribute with a type not derived from " | 178 | 3.31k | "'AbstractAttribute'!"); | 179 | 3.31k | assert(AAType::ID != Attribute::None && | 180 | 3.31k | "Cannot lookup generic abstract attributes!"); | 181 | 3.31k | | 182 | 3.31k | // Determine the argument number automatically for llvm::Arguments if none | 183 | 3.31k | // is set. Do not override a given one as it could be a use of the argument | 184 | 3.31k | // in a call site. | 185 | 3.31k | if (ArgNo == -1) | 186 | 743 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 388 | ArgNo = Arg->getArgNo(); | 188 | 3.31k | | 189 | 3.31k | // If a function was given together with an argument number, perform the | 190 | 3.31k | // lookup for the actual argument instead. Don't do it for variadic | 191 | 3.31k | // arguments. | 192 | 3.31k | if (ArgNo >= 0 && isa<Function>(&V)2.96k && | 193 | 3.31k | cast<Function>(&V)->arg_size() > (size_t)ArgNo853 ) | 194 | 853 | return getAAFor<AAType>( | 195 | 853 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 2.46k | | 197 | 2.46k | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 2.46k | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 2.46k | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 2.46k | if (AAType *AA = static_cast<AAType *>( | 201 | 2.23k | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 2.23k | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 2.23k | // at the calls sites and allows the fallback below to kick in. | 204 | 2.23k | if (AA->getState().isValidState()) { | 205 | 577 | QueryMap[AA].insert(&QueryingAA); | 206 | 577 | return AA; | 207 | 577 | } | 208 | 1.88k | } | 209 | 1.88k | | 210 | 1.88k | // If no abstract attribute was found and we look for a call site argument, | 211 | 1.88k | // defer to the actual argument instead. | 212 | 1.88k | ImmutableCallSite ICS(&V); | 213 | 1.88k | if (ICS && ICS.getCalledValue()694 ) | 214 | 694 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 1.19k | | 216 | 1.19k | // No matching attribute found | 217 | 1.19k | return nullptr; | 218 | 1.19k | } |
llvm::AAReturnedValues const* llvm::Attributor::getAAFor<llvm::AAReturnedValues>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 381 | int ArgNo = -1) { | 176 | 381 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 381 | "Cannot query an attribute with a type not derived from " | 178 | 381 | "'AbstractAttribute'!"); | 179 | 381 | assert(AAType::ID != Attribute::None && | 180 | 381 | "Cannot lookup generic abstract attributes!"); | 181 | 381 | | 182 | 381 | // Determine the argument number automatically for llvm::Arguments if none | 183 | 381 | // is set. Do not override a given one as it could be a use of the argument | 184 | 381 | // in a call site. | 185 | 381 | if (ArgNo == -1) | 186 | 381 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 0 | ArgNo = Arg->getArgNo(); | 188 | 381 | | 189 | 381 | // If a function was given together with an argument number, perform the | 190 | 381 | // lookup for the actual argument instead. Don't do it for variadic | 191 | 381 | // arguments. | 192 | 381 | if (ArgNo >= 0 && isa<Function>(&V)0 && | 193 | 381 | cast<Function>(&V)->arg_size() > (size_t)ArgNo0 ) | 194 | 0 | return getAAFor<AAType>( | 195 | 0 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 381 | | 197 | 381 | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 381 | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 381 | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 381 | if (AAType *AA = static_cast<AAType *>( | 201 | 381 | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 381 | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 381 | // at the calls sites and allows the fallback below to kick in. | 204 | 381 | if (AA->getState().isValidState()) { | 205 | 381 | QueryMap[AA].insert(&QueryingAA); | 206 | 381 | return AA; | 207 | 381 | } | 208 | 0 | } | 209 | 0 | | 210 | 0 | // If no abstract attribute was found and we look for a call site argument, | 211 | 0 | // defer to the actual argument instead. | 212 | 0 | ImmutableCallSite ICS(&V); | 213 | 0 | if (ICS && ICS.getCalledValue()) | 214 | 0 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 0 | | 216 | 0 | // No matching attribute found | 217 | 0 | return nullptr; | 218 | 0 | } |
llvm::AAWillReturn const* llvm::Attributor::getAAFor<llvm::AAWillReturn>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 278 | int ArgNo = -1) { | 176 | 278 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 278 | "Cannot query an attribute with a type not derived from " | 178 | 278 | "'AbstractAttribute'!"); | 179 | 278 | assert(AAType::ID != Attribute::None && | 180 | 278 | "Cannot lookup generic abstract attributes!"); | 181 | 278 | | 182 | 278 | // Determine the argument number automatically for llvm::Arguments if none | 183 | 278 | // is set. Do not override a given one as it could be a use of the argument | 184 | 278 | // in a call site. | 185 | 278 | if (ArgNo == -1) | 186 | 278 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 0 | ArgNo = Arg->getArgNo(); | 188 | 278 | | 189 | 278 | // If a function was given together with an argument number, perform the | 190 | 278 | // lookup for the actual argument instead. Don't do it for variadic | 191 | 278 | // arguments. | 192 | 278 | if (ArgNo >= 0 && isa<Function>(&V)0 && | 193 | 278 | cast<Function>(&V)->arg_size() > (size_t)ArgNo0 ) | 194 | 0 | return getAAFor<AAType>( | 195 | 0 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 278 | | 197 | 278 | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 278 | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 278 | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 278 | if (AAType *AA = static_cast<AAType *>( | 201 | 68 | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 68 | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 68 | // at the calls sites and allows the fallback below to kick in. | 204 | 68 | if (AA->getState().isValidState()) { | 205 | 49 | QueryMap[AA].insert(&QueryingAA); | 206 | 49 | return AA; | 207 | 49 | } | 208 | 229 | } | 209 | 229 | | 210 | 229 | // If no abstract attribute was found and we look for a call site argument, | 211 | 229 | // defer to the actual argument instead. | 212 | 229 | ImmutableCallSite ICS(&V); | 213 | 229 | if (ICS && ICS.getCalledValue()139 ) | 214 | 139 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 90 | | 216 | 90 | // No matching attribute found | 217 | 90 | return nullptr; | 218 | 90 | } |
llvm::AANoRecurse const* llvm::Attributor::getAAFor<llvm::AANoRecurse>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 98 | int ArgNo = -1) { | 176 | 98 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 98 | "Cannot query an attribute with a type not derived from " | 178 | 98 | "'AbstractAttribute'!"); | 179 | 98 | assert(AAType::ID != Attribute::None && | 180 | 98 | "Cannot lookup generic abstract attributes!"); | 181 | 98 | | 182 | 98 | // Determine the argument number automatically for llvm::Arguments if none | 183 | 98 | // is set. Do not override a given one as it could be a use of the argument | 184 | 98 | // in a call site. | 185 | 98 | if (ArgNo == -1) | 186 | 98 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 0 | ArgNo = Arg->getArgNo(); | 188 | 98 | | 189 | 98 | // If a function was given together with an argument number, perform the | 190 | 98 | // lookup for the actual argument instead. Don't do it for variadic | 191 | 98 | // arguments. | 192 | 98 | if (ArgNo >= 0 && isa<Function>(&V)0 && | 193 | 98 | cast<Function>(&V)->arg_size() > (size_t)ArgNo0 ) | 194 | 0 | return getAAFor<AAType>( | 195 | 0 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 98 | | 197 | 98 | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 98 | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 98 | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 98 | if (AAType *AA = static_cast<AAType *>( | 201 | 0 | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 0 | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 0 | // at the calls sites and allows the fallback below to kick in. | 204 | 0 | if (AA->getState().isValidState()) { | 205 | 0 | QueryMap[AA].insert(&QueryingAA); | 206 | 0 | return AA; | 207 | 0 | } | 208 | 98 | } | 209 | 98 | | 210 | 98 | // If no abstract attribute was found and we look for a call site argument, | 211 | 98 | // defer to the actual argument instead. | 212 | 98 | ImmutableCallSite ICS(&V); | 213 | 98 | if (ICS && ICS.getCalledValue()49 ) | 214 | 49 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 49 | | 216 | 49 | // No matching attribute found | 217 | 49 | return nullptr; | 218 | 49 | } |
llvm::AANoAlias const* llvm::Attributor::getAAFor<llvm::AANoAlias>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 36 | int ArgNo = -1) { | 176 | 36 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 36 | "Cannot query an attribute with a type not derived from " | 178 | 36 | "'AbstractAttribute'!"); | 179 | 36 | assert(AAType::ID != Attribute::None && | 180 | 36 | "Cannot lookup generic abstract attributes!"); | 181 | 36 | | 182 | 36 | // Determine the argument number automatically for llvm::Arguments if none | 183 | 36 | // is set. Do not override a given one as it could be a use of the argument | 184 | 36 | // in a call site. | 185 | 36 | if (ArgNo == -1) | 186 | 36 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 0 | ArgNo = Arg->getArgNo(); | 188 | 36 | | 189 | 36 | // If a function was given together with an argument number, perform the | 190 | 36 | // lookup for the actual argument instead. Don't do it for variadic | 191 | 36 | // arguments. | 192 | 36 | if (ArgNo >= 0 && isa<Function>(&V)0 && | 193 | 36 | cast<Function>(&V)->arg_size() > (size_t)ArgNo0 ) | 194 | 0 | return getAAFor<AAType>( | 195 | 0 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 36 | | 197 | 36 | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 36 | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 36 | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 36 | if (AAType *AA = static_cast<AAType *>( | 201 | 1 | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 1 | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 1 | // at the calls sites and allows the fallback below to kick in. | 204 | 1 | if (AA->getState().isValidState()) { | 205 | 0 | QueryMap[AA].insert(&QueryingAA); | 206 | 0 | return AA; | 207 | 0 | } | 208 | 36 | } | 209 | 36 | | 210 | 36 | // If no abstract attribute was found and we look for a call site argument, | 211 | 36 | // defer to the actual argument instead. | 212 | 36 | ImmutableCallSite ICS(&V); | 213 | 36 | if (ICS && ICS.getCalledValue()18 ) | 214 | 18 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 18 | | 216 | 18 | // No matching attribute found | 217 | 18 | return nullptr; | 218 | 18 | } |
llvm::AANoReturn const* llvm::Attributor::getAAFor<llvm::AANoReturn>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 980 | int ArgNo = -1) { | 176 | 980 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 980 | "Cannot query an attribute with a type not derived from " | 178 | 980 | "'AbstractAttribute'!"); | 179 | 980 | assert(AAType::ID != Attribute::None && | 180 | 980 | "Cannot lookup generic abstract attributes!"); | 181 | 980 | | 182 | 980 | // Determine the argument number automatically for llvm::Arguments if none | 183 | 980 | // is set. Do not override a given one as it could be a use of the argument | 184 | 980 | // in a call site. | 185 | 980 | if (ArgNo == -1) | 186 | 980 | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 0 | ArgNo = Arg->getArgNo(); | 188 | 980 | | 189 | 980 | // If a function was given together with an argument number, perform the | 190 | 980 | // lookup for the actual argument instead. Don't do it for variadic | 191 | 980 | // arguments. | 192 | 980 | if (ArgNo >= 0 && isa<Function>(&V)0 && | 193 | 980 | cast<Function>(&V)->arg_size() > (size_t)ArgNo0 ) | 194 | 0 | return getAAFor<AAType>( | 195 | 0 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 980 | | 197 | 980 | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 980 | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 980 | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 980 | if (AAType *AA = static_cast<AAType *>( | 201 | 0 | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 0 | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 0 | // at the calls sites and allows the fallback below to kick in. | 204 | 0 | if (AA->getState().isValidState()) { | 205 | 0 | QueryMap[AA].insert(&QueryingAA); | 206 | 0 | return AA; | 207 | 0 | } | 208 | 980 | } | 209 | 980 | | 210 | 980 | // If no abstract attribute was found and we look for a call site argument, | 211 | 980 | // defer to the actual argument instead. | 212 | 980 | ImmutableCallSite ICS(&V); | 213 | 980 | if (ICS && ICS.getCalledValue()490 ) | 214 | 490 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 490 | | 216 | 490 | // No matching attribute found | 217 | 490 | return nullptr; | 218 | 490 | } |
llvm::AADereferenceable const* llvm::Attributor::getAAFor<llvm::AADereferenceable>(llvm::AbstractAttribute&, llvm::Value const&, int) Line | Count | Source | 175 | 2.28k | int ArgNo = -1) { | 176 | 2.28k | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 177 | 2.28k | "Cannot query an attribute with a type not derived from " | 178 | 2.28k | "'AbstractAttribute'!"); | 179 | 2.28k | assert(AAType::ID != Attribute::None && | 180 | 2.28k | "Cannot lookup generic abstract attributes!"); | 181 | 2.28k | | 182 | 2.28k | // Determine the argument number automatically for llvm::Arguments if none | 183 | 2.28k | // is set. Do not override a given one as it could be a use of the argument | 184 | 2.28k | // in a call site. | 185 | 2.28k | if (ArgNo == -1) | 186 | 2.19k | if (auto *Arg = dyn_cast<Argument>(&V)) | 187 | 1.40k | ArgNo = Arg->getArgNo(); | 188 | 2.28k | | 189 | 2.28k | // If a function was given together with an argument number, perform the | 190 | 2.28k | // lookup for the actual argument instead. Don't do it for variadic | 191 | 2.28k | // arguments. | 192 | 2.28k | if (ArgNo >= 0 && isa<Function>(&V)1.49k && | 193 | 2.28k | cast<Function>(&V)->arg_size() > (size_t)ArgNo19 ) | 194 | 19 | return getAAFor<AAType>( | 195 | 19 | QueryingAA, *(cast<Function>(&V)->arg_begin() + ArgNo), ArgNo); | 196 | 2.26k | | 197 | 2.26k | // Lookup the abstract attribute of type AAType. If found, return it after | 198 | 2.26k | // registering a dependence of QueryingAA on the one returned attribute. | 199 | 2.26k | const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo}); | 200 | 2.26k | if (AAType *AA = static_cast<AAType *>( | 201 | 1.70k | KindToAbstractAttributeMap.lookup(AAType::ID))) { | 202 | 1.70k | // Do not return an attribute with an invalid state. This minimizes checks | 203 | 1.70k | // at the calls sites and allows the fallback below to kick in. | 204 | 1.70k | if (AA->getState().isValidState()) { | 205 | 251 | QueryMap[AA].insert(&QueryingAA); | 206 | 251 | return AA; | 207 | 251 | } | 208 | 2.01k | } | 209 | 2.01k | | 210 | 2.01k | // If no abstract attribute was found and we look for a call site argument, | 211 | 2.01k | // defer to the actual argument instead. | 212 | 2.01k | ImmutableCallSite ICS(&V); | 213 | 2.01k | if (ICS && ICS.getCalledValue()309 ) | 214 | 309 | return getAAFor<AAType>(QueryingAA, *ICS.getCalledValue(), ArgNo); | 215 | 1.70k | | 216 | 1.70k | // No matching attribute found | 217 | 1.70k | return nullptr; | 218 | 1.70k | } |
|
219 | | |
220 | | /// Introduce a new abstract attribute into the fixpoint analysis. |
221 | | /// |
222 | | /// Note that ownership of the attribute is given to the Attributor. It will |
223 | | /// invoke delete for the Attributor on destruction of the Attributor. |
224 | | /// |
225 | | /// Attributes are identified by |
226 | | /// (1) their anchored value (see AA.getAnchoredValue()), |
227 | | /// (2) their argument number (\p ArgNo, or Argument::getArgNo()), and |
228 | | /// (3) their default attribute kind (see AAType::ID). |
229 | 2.96k | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { |
230 | 2.96k | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, |
231 | 2.96k | "Cannot register an attribute with a type not derived from " |
232 | 2.96k | "'AbstractAttribute'!"); |
233 | 2.96k | |
234 | 2.96k | // Determine the anchor value and the argument number which are used to |
235 | 2.96k | // lookup the attribute together with AAType::ID. If passed an argument, |
236 | 2.96k | // use its argument number but do not override a given one as it could be a |
237 | 2.96k | // use of the argument at a call site. |
238 | 2.96k | Value &AnchoredVal = AA.getAnchoredValue(); |
239 | 2.96k | if (ArgNo == -1) |
240 | 2.21k | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) |
241 | 432 | ArgNo = Arg->getArgNo(); |
242 | 2.96k | |
243 | 2.96k | // Put the attribute in the lookup map structure and the container we use to |
244 | 2.96k | // keep track of all attributes. |
245 | 2.96k | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; |
246 | 2.96k | AllAbstractAttributes.push_back(&AA); |
247 | 2.96k | return AA; |
248 | 2.96k | } AANoUnwindFunction& llvm::Attributor::registerAA<AANoUnwindFunction>(AANoUnwindFunction&, int) Line | Count | Source | 229 | 248 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 248 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 248 | "Cannot register an attribute with a type not derived from " | 232 | 248 | "'AbstractAttribute'!"); | 233 | 248 | | 234 | 248 | // Determine the anchor value and the argument number which are used to | 235 | 248 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 248 | // use its argument number but do not override a given one as it could be a | 237 | 248 | // use of the argument at a call site. | 238 | 248 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 248 | if (ArgNo == -1) | 240 | 248 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 248 | | 243 | 248 | // Put the attribute in the lookup map structure and the container we use to | 244 | 248 | // keep track of all attributes. | 245 | 248 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 248 | AllAbstractAttributes.push_back(&AA); | 247 | 248 | return AA; | 248 | 248 | } |
AANoSyncFunction& llvm::Attributor::registerAA<AANoSyncFunction>(AANoSyncFunction&, int) Line | Count | Source | 229 | 248 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 248 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 248 | "Cannot register an attribute with a type not derived from " | 232 | 248 | "'AbstractAttribute'!"); | 233 | 248 | | 234 | 248 | // Determine the anchor value and the argument number which are used to | 235 | 248 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 248 | // use its argument number but do not override a given one as it could be a | 237 | 248 | // use of the argument at a call site. | 238 | 248 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 248 | if (ArgNo == -1) | 240 | 248 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 248 | | 243 | 248 | // Put the attribute in the lookup map structure and the container we use to | 244 | 248 | // keep track of all attributes. | 245 | 248 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 248 | AllAbstractAttributes.push_back(&AA); | 247 | 248 | return AA; | 248 | 248 | } |
AANoFreeFunction& llvm::Attributor::registerAA<AANoFreeFunction>(AANoFreeFunction&, int) Line | Count | Source | 229 | 248 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 248 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 248 | "Cannot register an attribute with a type not derived from " | 232 | 248 | "'AbstractAttribute'!"); | 233 | 248 | | 234 | 248 | // Determine the anchor value and the argument number which are used to | 235 | 248 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 248 | // use its argument number but do not override a given one as it could be a | 237 | 248 | // use of the argument at a call site. | 238 | 248 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 248 | if (ArgNo == -1) | 240 | 248 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 248 | | 243 | 248 | // Put the attribute in the lookup map structure and the container we use to | 244 | 248 | // keep track of all attributes. | 245 | 248 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 248 | AllAbstractAttributes.push_back(&AA); | 247 | 248 | return AA; | 248 | 248 | } |
AAReturnedValuesImpl& llvm::Attributor::registerAA<AAReturnedValuesImpl>(AAReturnedValuesImpl&, int) Line | Count | Source | 229 | 180 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 180 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 180 | "Cannot register an attribute with a type not derived from " | 232 | 180 | "'AbstractAttribute'!"); | 233 | 180 | | 234 | 180 | // Determine the anchor value and the argument number which are used to | 235 | 180 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 180 | // use its argument number but do not override a given one as it could be a | 237 | 180 | // use of the argument at a call site. | 238 | 180 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 180 | if (ArgNo == -1) | 240 | 180 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 180 | | 243 | 180 | // Put the attribute in the lookup map structure and the container we use to | 244 | 180 | // keep track of all attributes. | 245 | 180 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 180 | AllAbstractAttributes.push_back(&AA); | 247 | 180 | return AA; | 248 | 180 | } |
AANonNullReturned& llvm::Attributor::registerAA<AANonNullReturned>(AANonNullReturned&, int) Line | Count | Source | 229 | 120 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 120 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 120 | "Cannot register an attribute with a type not derived from " | 232 | 120 | "'AbstractAttribute'!"); | 233 | 120 | | 234 | 120 | // Determine the anchor value and the argument number which are used to | 235 | 120 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 120 | // use its argument number but do not override a given one as it could be a | 237 | 120 | // use of the argument at a call site. | 238 | 120 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 120 | if (ArgNo == -1) | 240 | 120 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 120 | | 243 | 120 | // Put the attribute in the lookup map structure and the container we use to | 244 | 120 | // keep track of all attributes. | 245 | 120 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 120 | AllAbstractAttributes.push_back(&AA); | 247 | 120 | return AA; | 248 | 120 | } |
AANoAliasReturned& llvm::Attributor::registerAA<AANoAliasReturned>(AANoAliasReturned&, int) Line | Count | Source | 229 | 120 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 120 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 120 | "Cannot register an attribute with a type not derived from " | 232 | 120 | "'AbstractAttribute'!"); | 233 | 120 | | 234 | 120 | // Determine the anchor value and the argument number which are used to | 235 | 120 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 120 | // use its argument number but do not override a given one as it could be a | 237 | 120 | // use of the argument at a call site. | 238 | 120 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 120 | if (ArgNo == -1) | 240 | 120 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 120 | | 243 | 120 | // Put the attribute in the lookup map structure and the container we use to | 244 | 120 | // keep track of all attributes. | 245 | 120 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 120 | AllAbstractAttributes.push_back(&AA); | 247 | 120 | return AA; | 248 | 120 | } |
AADereferenceableReturned& llvm::Attributor::registerAA<AADereferenceableReturned>(AADereferenceableReturned&, int) Line | Count | Source | 229 | 120 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 120 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 120 | "Cannot register an attribute with a type not derived from " | 232 | 120 | "'AbstractAttribute'!"); | 233 | 120 | | 234 | 120 | // Determine the anchor value and the argument number which are used to | 235 | 120 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 120 | // use its argument number but do not override a given one as it could be a | 237 | 120 | // use of the argument at a call site. | 238 | 120 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 120 | if (ArgNo == -1) | 240 | 120 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 120 | | 243 | 120 | // Put the attribute in the lookup map structure and the container we use to | 244 | 120 | // keep track of all attributes. | 245 | 120 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 120 | AllAbstractAttributes.push_back(&AA); | 247 | 120 | return AA; | 248 | 120 | } |
AANonNullArgument& llvm::Attributor::registerAA<AANonNullArgument>(AANonNullArgument&, int) Line | Count | Source | 229 | 216 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 216 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 216 | "Cannot register an attribute with a type not derived from " | 232 | 216 | "'AbstractAttribute'!"); | 233 | 216 | | 234 | 216 | // Determine the anchor value and the argument number which are used to | 235 | 216 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 216 | // use its argument number but do not override a given one as it could be a | 237 | 216 | // use of the argument at a call site. | 238 | 216 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 216 | if (ArgNo == -1) | 240 | 216 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 216 | ArgNo = Arg->getArgNo(); | 242 | 216 | | 243 | 216 | // Put the attribute in the lookup map structure and the container we use to | 244 | 216 | // keep track of all attributes. | 245 | 216 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 216 | AllAbstractAttributes.push_back(&AA); | 247 | 216 | return AA; | 248 | 216 | } |
AADereferenceableArgument& llvm::Attributor::registerAA<AADereferenceableArgument>(AADereferenceableArgument&, int) Line | Count | Source | 229 | 216 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 216 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 216 | "Cannot register an attribute with a type not derived from " | 232 | 216 | "'AbstractAttribute'!"); | 233 | 216 | | 234 | 216 | // Determine the anchor value and the argument number which are used to | 235 | 216 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 216 | // use its argument number but do not override a given one as it could be a | 237 | 216 | // use of the argument at a call site. | 238 | 216 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 216 | if (ArgNo == -1) | 240 | 216 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 216 | ArgNo = Arg->getArgNo(); | 242 | 216 | | 243 | 216 | // Put the attribute in the lookup map structure and the container we use to | 244 | 216 | // keep track of all attributes. | 245 | 216 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 216 | AllAbstractAttributes.push_back(&AA); | 247 | 216 | return AA; | 248 | 216 | } |
AAWillReturnFunction& llvm::Attributor::registerAA<AAWillReturnFunction>(AAWillReturnFunction&, int) Line | Count | Source | 229 | 248 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 248 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 248 | "Cannot register an attribute with a type not derived from " | 232 | 248 | "'AbstractAttribute'!"); | 233 | 248 | | 234 | 248 | // Determine the anchor value and the argument number which are used to | 235 | 248 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 248 | // use its argument number but do not override a given one as it could be a | 237 | 248 | // use of the argument at a call site. | 238 | 248 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 248 | if (ArgNo == -1) | 240 | 248 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 248 | | 243 | 248 | // Put the attribute in the lookup map structure and the container we use to | 244 | 248 | // keep track of all attributes. | 245 | 248 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 248 | AllAbstractAttributes.push_back(&AA); | 247 | 248 | return AA; | 248 | 248 | } |
AAIsDeadFunction& llvm::Attributor::registerAA<AAIsDeadFunction>(AAIsDeadFunction&, int) Line | Count | Source | 229 | 248 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 248 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 248 | "Cannot register an attribute with a type not derived from " | 232 | 248 | "'AbstractAttribute'!"); | 233 | 248 | | 234 | 248 | // Determine the anchor value and the argument number which are used to | 235 | 248 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 248 | // use its argument number but do not override a given one as it could be a | 237 | 248 | // use of the argument at a call site. | 238 | 248 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 248 | if (ArgNo == -1) | 240 | 248 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 248 | | 243 | 248 | // Put the attribute in the lookup map structure and the container we use to | 244 | 248 | // keep track of all attributes. | 245 | 248 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 248 | AllAbstractAttributes.push_back(&AA); | 247 | 248 | return AA; | 248 | 248 | } |
AANonNullCallSiteArgument& llvm::Attributor::registerAA<AANonNullCallSiteArgument>(AANonNullCallSiteArgument&, int) Line | Count | Source | 229 | 375 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 375 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 375 | "Cannot register an attribute with a type not derived from " | 232 | 375 | "'AbstractAttribute'!"); | 233 | 375 | | 234 | 375 | // Determine the anchor value and the argument number which are used to | 235 | 375 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 375 | // use its argument number but do not override a given one as it could be a | 237 | 375 | // use of the argument at a call site. | 238 | 375 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 375 | if (ArgNo == -1) | 240 | 0 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 375 | | 243 | 375 | // Put the attribute in the lookup map structure and the container we use to | 244 | 375 | // keep track of all attributes. | 245 | 375 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 375 | AllAbstractAttributes.push_back(&AA); | 247 | 375 | return AA; | 248 | 375 | } |
AADereferenceableCallSiteArgument& llvm::Attributor::registerAA<AADereferenceableCallSiteArgument>(AADereferenceableCallSiteArgument&, int) Line | Count | Source | 229 | 375 | template <typename AAType> AAType ®isterAA(AAType &AA, int ArgNo = -1) { | 230 | 375 | static_assert(std::is_base_of<AbstractAttribute, AAType>::value, | 231 | 375 | "Cannot register an attribute with a type not derived from " | 232 | 375 | "'AbstractAttribute'!"); | 233 | 375 | | 234 | 375 | // Determine the anchor value and the argument number which are used to | 235 | 375 | // lookup the attribute together with AAType::ID. If passed an argument, | 236 | 375 | // use its argument number but do not override a given one as it could be a | 237 | 375 | // use of the argument at a call site. | 238 | 375 | Value &AnchoredVal = AA.getAnchoredValue(); | 239 | 375 | if (ArgNo == -1) | 240 | 0 | if (auto *Arg = dyn_cast<Argument>(&AnchoredVal)) | 241 | 0 | ArgNo = Arg->getArgNo(); | 242 | 375 | | 243 | 375 | // Put the attribute in the lookup map structure and the container we use to | 244 | 375 | // keep track of all attributes. | 245 | 375 | AAMap[{&AnchoredVal, ArgNo}][AAType::ID] = &AA; | 246 | 375 | AllAbstractAttributes.push_back(&AA); | 247 | 375 | return AA; | 248 | 375 | } |
|
249 | | |
250 | | /// Determine opportunities to derive 'default' attributes in \p F and create |
251 | | /// abstract attribute objects for them. |
252 | | /// |
253 | | /// \param F The function that is checked for attribute opportunities. |
254 | | /// \param InfoCache A cache for information queryable by the new attributes. |
255 | | /// \param Whitelist If not null, a set limiting the attribute opportunities. |
256 | | /// |
257 | | /// Note that abstract attribute instances are generally created even if the |
258 | | /// IR already contains the information they would deduce. The most important |
259 | | /// reason for this is the single interface, the one of the abstract attribute |
260 | | /// instance, which can be queried without the need to look at the IR in |
261 | | /// various places. |
262 | | void identifyDefaultAbstractAttributes( |
263 | | Function &F, InformationCache &InfoCache, |
264 | | DenseSet</* Attribute::AttrKind */ unsigned> *Whitelist = nullptr); |
265 | | |
266 | | /// Check \p Pred on all function call sites. |
267 | | /// |
268 | | /// This method will evaluate \p Pred on call sites and return |
269 | | /// true if \p Pred holds in every call sites. However, this is only possible |
270 | | /// all call sites are known, hence the function has internal linkage. |
271 | | bool checkForAllCallSites(Function &F, std::function<bool(CallSite)> &Pred, |
272 | | bool RequireAllCallSites); |
273 | | |
274 | | private: |
275 | | /// The set of all abstract attributes. |
276 | | ///{ |
277 | | using AAVector = SmallVector<AbstractAttribute *, 64>; |
278 | | AAVector AllAbstractAttributes; |
279 | | ///} |
280 | | |
281 | | /// A nested map to lookup abstract attributes based on the anchored value and |
282 | | /// an argument positions (or -1) on the outer level, and attribute kinds |
283 | | /// (Attribute::AttrKind) on the inner level. |
284 | | ///{ |
285 | | using KindToAbstractAttributeMap = DenseMap<unsigned, AbstractAttribute *>; |
286 | | DenseMap<std::pair<const Value *, int>, KindToAbstractAttributeMap> AAMap; |
287 | | ///} |
288 | | |
289 | | /// A map from abstract attributes to the ones that queried them through calls |
290 | | /// to the getAAFor<...>(...) method. |
291 | | ///{ |
292 | | using QueryMapTy = |
293 | | DenseMap<AbstractAttribute *, SetVector<AbstractAttribute *>>; |
294 | | QueryMapTy QueryMap; |
295 | | ///} |
296 | | }; |
297 | | |
298 | | /// Data structure to hold cached (LLVM-IR) information. |
299 | | /// |
300 | | /// All attributes are given an InformationCache object at creation time to |
301 | | /// avoid inspection of the IR by all of them individually. This default |
302 | | /// InformationCache will hold information required by 'default' attributes, |
303 | | /// thus the ones deduced when Attributor::identifyDefaultAbstractAttributes(..) |
304 | | /// is called. |
305 | | /// |
306 | | /// If custom abstract attributes, registered manually through |
307 | | /// Attributor::registerAA(...), need more information, especially if it is not |
308 | | /// reusable, it is advised to inherit from the InformationCache and cast the |
309 | | /// instance down in the abstract attributes. |
310 | | struct InformationCache { |
311 | | /// A map type from opcodes to instructions with this opcode. |
312 | | using OpcodeInstMapTy = DenseMap<unsigned, SmallVector<Instruction *, 32>>; |
313 | | |
314 | | /// Return the map that relates "interesting" opcodes with all instructions |
315 | | /// with that opcode in \p F. |
316 | 1.05k | OpcodeInstMapTy &getOpcodeInstMapForFunction(Function &F) { |
317 | 1.05k | return FuncInstOpcodeMap[&F]; |
318 | 1.05k | } |
319 | | |
320 | | /// A vector type to hold instructions. |
321 | | using InstructionVectorTy = std::vector<Instruction *>; |
322 | | |
323 | | /// Return the instructions in \p F that may read or write memory. |
324 | 250 | InstructionVectorTy &getReadOrWriteInstsForFunction(Function &F) { |
325 | 250 | return FuncRWInstsMap[&F]; |
326 | 250 | } |
327 | | |
328 | | private: |
329 | | /// A map type from functions to opcode to instruction maps. |
330 | | using FuncInstOpcodeMapTy = DenseMap<Function *, OpcodeInstMapTy>; |
331 | | |
332 | | /// A map type from functions to their read or write instructions. |
333 | | using FuncRWInstsMapTy = DenseMap<Function *, InstructionVectorTy>; |
334 | | |
335 | | /// A nested map that remembers all instructions in a function with a certain |
336 | | /// instruction opcode (Instruction::getOpcode()). |
337 | | FuncInstOpcodeMapTy FuncInstOpcodeMap; |
338 | | |
339 | | /// A map from functions to their instructions that may read or write memory. |
340 | | FuncRWInstsMapTy FuncRWInstsMap; |
341 | | |
342 | | /// Give the Attributor access to the members so |
343 | | /// Attributor::identifyDefaultAbstractAttributes(...) can initialize them. |
344 | | friend struct Attributor; |
345 | | }; |
346 | | |
347 | | /// An interface to query the internal state of an abstract attribute. |
348 | | /// |
349 | | /// The abstract state is a minimal interface that allows the Attributor to |
350 | | /// communicate with the abstract attributes about their internal state without |
351 | | /// enforcing or exposing implementation details, e.g., the (existence of an) |
352 | | /// underlying lattice. |
353 | | /// |
354 | | /// It is sufficient to be able to query if a state is (1) valid or invalid, (2) |
355 | | /// at a fixpoint, and to indicate to the state that (3) an optimistic fixpoint |
356 | | /// was reached or (4) a pessimistic fixpoint was enforced. |
357 | | /// |
358 | | /// All methods need to be implemented by the subclass. For the common use case, |
359 | | /// a single boolean state or a bit-encoded state, the BooleanState and |
360 | | /// IntegerState classes are already provided. An abstract attribute can inherit |
361 | | /// from them to get the abstract state interface and additional methods to |
362 | | /// directly modify the state based if needed. See the class comments for help. |
363 | | struct AbstractState { |
364 | 10.7k | virtual ~AbstractState() {} |
365 | | |
366 | | /// Return if this abstract state is in a valid state. If false, no |
367 | | /// information provided should be used. |
368 | | virtual bool isValidState() const = 0; |
369 | | |
370 | | /// Return if this abstract state is fixed, thus does not need to be updated |
371 | | /// if information changes as it cannot change itself. |
372 | | virtual bool isAtFixpoint() const = 0; |
373 | | |
374 | | /// Indicate that the abstract state should converge to the optimistic state. |
375 | | /// |
376 | | /// This will usually make the optimistically assumed state the known to be |
377 | | /// true state. |
378 | | virtual void indicateOptimisticFixpoint() = 0; |
379 | | |
380 | | /// Indicate that the abstract state should converge to the pessimistic state. |
381 | | /// |
382 | | /// This will usually revert the optimistically assumed state to the known to |
383 | | /// be true state. |
384 | | virtual void indicatePessimisticFixpoint() = 0; |
385 | | }; |
386 | | |
387 | | /// Simple state with integers encoding. |
388 | | /// |
389 | | /// The interface ensures that the assumed bits are always a subset of the known |
390 | | /// bits. Users can only add known bits and, except through adding known bits, |
391 | | /// they can only remove assumed bits. This should guarantee monotoniticy and |
392 | | /// thereby the existence of a fixpoint (if used corretly). The fixpoint is |
393 | | /// reached when the assumed and known state/bits are equal. Users can |
394 | | /// force/inidicate a fixpoint. If an optimistic one is indicated, the known |
395 | | /// state will catch up with the assumed one, for a pessimistic fixpoint it is |
396 | | /// the other way around. |
397 | | struct IntegerState : public AbstractState { |
398 | | /// Underlying integer type, we assume 32 bits to be enough. |
399 | | using base_t = uint32_t; |
400 | | |
401 | | /// Initialize the (best) state. |
402 | 3.49k | IntegerState(base_t BestState = ~0) : Assumed(BestState) {} |
403 | | |
404 | | /// Return the worst possible representable state. |
405 | 11.5k | static constexpr base_t getWorstState() { return 0; } |
406 | | |
407 | | /// See AbstractState::isValidState() |
408 | | /// NOTE: For now we simply pretend that the worst possible state is invalid. |
409 | 8.07k | bool isValidState() const override { return Assumed != getWorstState(); } |
410 | | |
411 | | /// See AbstractState::isAtFixpoint() |
412 | 10.4k | bool isAtFixpoint() const override { return Assumed == Known; } |
413 | | |
414 | | /// See AbstractState::indicateOptimisticFixpoint(...) |
415 | 1.72k | void indicateOptimisticFixpoint() override { Known = Assumed; } |
416 | | |
417 | | /// See AbstractState::indicatePessimisticFixpoint(...) |
418 | 1.80k | void indicatePessimisticFixpoint() override { Assumed = Known; } |
419 | | |
420 | | /// Return the known state encoding |
421 | 2.35k | base_t getKnown() const { return Known; } |
422 | | |
423 | | /// Return the assumed state encoding. |
424 | 4.25k | base_t getAssumed() const { return Assumed; } |
425 | | |
426 | | /// Return true if the bits set in \p BitsEncoding are "known bits". |
427 | 808 | bool isKnown(base_t BitsEncoding) const { |
428 | 808 | return (Known & BitsEncoding) == BitsEncoding; |
429 | 808 | } |
430 | | |
431 | | /// Return true if the bits set in \p BitsEncoding are "assumed bits". |
432 | 1.95k | bool isAssumed(base_t BitsEncoding) const { |
433 | 1.95k | return (Assumed & BitsEncoding) == BitsEncoding; |
434 | 1.95k | } |
435 | | |
436 | | /// Add the bits in \p BitsEncoding to the "known bits". |
437 | 160 | IntegerState &addKnownBits(base_t Bits) { |
438 | 160 | // Make sure we never miss any "known bits". |
439 | 160 | Assumed |= Bits; |
440 | 160 | Known |= Bits; |
441 | 160 | return *this; |
442 | 160 | } |
443 | | |
444 | | /// Remove the bits in \p BitsEncoding from the "assumed bits" if not known. |
445 | 2.40k | IntegerState &removeAssumedBits(base_t BitsEncoding) { |
446 | 2.40k | // Make sure we never loose any "known bits". |
447 | 2.40k | Assumed = (Assumed & ~BitsEncoding) | Known; |
448 | 2.40k | return *this; |
449 | 2.40k | } |
450 | | |
451 | | /// Keep only "assumed bits" also set in \p BitsEncoding but all known ones. |
452 | 0 | IntegerState &intersectAssumedBits(base_t BitsEncoding) { |
453 | 0 | // Make sure we never loose any "known bits". |
454 | 0 | Assumed = (Assumed & BitsEncoding) | Known; |
455 | 0 | return *this; |
456 | 0 | } |
457 | | |
458 | | /// Take minimum of assumed and \p Value. |
459 | 1.06k | IntegerState &takeAssumedMinimum(base_t Value) { |
460 | 1.06k | // Make sure we never loose "known value". |
461 | 1.06k | Assumed = std::max(std::min(Assumed, Value), Known); |
462 | 1.06k | return *this; |
463 | 1.06k | } |
464 | | |
465 | | /// Take maximum of known and \p Value. |
466 | 27 | IntegerState &takeKnownMaximum(base_t Value) { |
467 | 27 | // Make sure we never loose "known value". |
468 | 27 | Assumed = std::max(Value, Assumed); |
469 | 27 | Known = std::max(Value, Known); |
470 | 27 | return *this; |
471 | 27 | } |
472 | | |
473 | | /// Equality for IntegerState. |
474 | 1.44k | bool operator==(const IntegerState &R) const { |
475 | 1.44k | return this->getAssumed() == R.getAssumed() && |
476 | 1.44k | this->getKnown() == R.getKnown()960 ; |
477 | 1.44k | } |
478 | | |
479 | | private: |
480 | | /// The known state encoding in an integer of type base_t. |
481 | | base_t Known = getWorstState(); |
482 | | |
483 | | /// The assumed state encoding in an integer of type base_t. |
484 | | base_t Assumed; |
485 | | }; |
486 | | |
487 | | /// Simple wrapper for a single bit (boolean) state. |
488 | | struct BooleanState : public IntegerState { |
489 | 2.07k | BooleanState() : IntegerState(1){}; |
490 | | }; |
491 | | |
492 | | /// Base struct for all "concrete attribute" deductions. |
493 | | /// |
494 | | /// The abstract attribute is a minimal interface that allows the Attributor to |
495 | | /// orchestrate the abstract/fixpoint analysis. The design allows to hide away |
496 | | /// implementation choices made for the subclasses but also to structure their |
497 | | /// implementation and simplify the use of other abstract attributes in-flight. |
498 | | /// |
499 | | /// To allow easy creation of new attributes, most methods have default |
500 | | /// implementations. The ones that do not are generally straight forward, except |
501 | | /// `AbstractAttribute::updateImpl` which is the location of most reasoning |
502 | | /// associated with the abstract attribute. The update is invoked by the |
503 | | /// Attributor in case the situation used to justify the current optimistic |
504 | | /// state might have changed. The Attributor determines this automatically |
505 | | /// by monitoring the `Attributor::getAAFor` calls made by abstract attributes. |
506 | | /// |
507 | | /// The `updateImpl` method should inspect the IR and other abstract attributes |
508 | | /// in-flight to justify the best possible (=optimistic) state. The actual |
509 | | /// implementation is, similar to the underlying abstract state encoding, not |
510 | | /// exposed. In the most common case, the `updateImpl` will go through a list of |
511 | | /// reasons why its optimistic state is valid given the current information. If |
512 | | /// any combination of them holds and is sufficient to justify the current |
513 | | /// optimistic state, the method shall return UNCHAGED. If not, the optimistic |
514 | | /// state is adjusted to the situation and the method shall return CHANGED. |
515 | | /// |
516 | | /// If the manifestation of the "concrete attribute" deduced by the subclass |
517 | | /// differs from the "default" behavior, which is a (set of) LLVM-IR |
518 | | /// attribute(s) for an argument, call site argument, function return value, or |
519 | | /// function, the `AbstractAttribute::manifest` method should be overloaded. |
520 | | /// |
521 | | /// NOTE: If the state obtained via getState() is INVALID, thus if |
522 | | /// AbstractAttribute::getState().isValidState() returns false, no |
523 | | /// information provided by the methods of this class should be used. |
524 | | /// NOTE: The Attributor currently has certain limitations to what we can do. |
525 | | /// As a general rule of thumb, "concrete" abstract attributes should *for |
526 | | /// now* only perform "backward" information propagation. That means |
527 | | /// optimistic information obtained through abstract attributes should |
528 | | /// only be used at positions that precede the origin of the information |
529 | | /// with regards to the program flow. More practically, information can |
530 | | /// *now* be propagated from instructions to their enclosing function, but |
531 | | /// *not* from call sites to the called function. The mechanisms to allow |
532 | | /// both directions will be added in the future. |
533 | | /// NOTE: The mechanics of adding a new "concrete" abstract attribute are |
534 | | /// described in the file comment. |
535 | | struct AbstractAttribute { |
536 | | |
537 | | /// The positions attributes can be manifested in. |
538 | | enum ManifestPosition { |
539 | | MP_ARGUMENT, ///< An attribute for a function argument. |
540 | | MP_CALL_SITE_ARGUMENT, ///< An attribute for a call site argument. |
541 | | MP_FUNCTION, ///< An attribute for a function as a whole. |
542 | | MP_RETURNED, ///< An attribute for the function return value. |
543 | | }; |
544 | | |
545 | | /// An abstract attribute associated with \p AssociatedVal and anchored at |
546 | | /// \p AnchoredVal. |
547 | | /// |
548 | | /// \param AssociatedVal The value this abstract attribute is associated with. |
549 | | /// \param AnchoredVal The value this abstract attributes is anchored at. |
550 | | /// \param InfoCache Cached information accessible to the abstract attribute. |
551 | | AbstractAttribute(Value *AssociatedVal, Value &AnchoredVal, |
552 | | InformationCache &InfoCache) |
553 | | : AssociatedVal(AssociatedVal), AnchoredVal(AnchoredVal), |
554 | 2.96k | InfoCache(InfoCache) {} |
555 | | |
556 | | /// An abstract attribute associated with and anchored at \p V. |
557 | | AbstractAttribute(Value &V, InformationCache &InfoCache) |
558 | 2.21k | : AbstractAttribute(&V, V, InfoCache) {} |
559 | | |
560 | | /// Virtual destructor. |
561 | 2.96k | virtual ~AbstractAttribute() {} |
562 | | |
563 | | /// Initialize the state with the information in the Attributor \p A. |
564 | | /// |
565 | | /// This function is called by the Attributor once all abstract attributes |
566 | | /// have been identified. It can and shall be used for task like: |
567 | | /// - identify existing knowledge in the IR and use it for the "known state" |
568 | | /// - perform any work that is not going to change over time, e.g., determine |
569 | | /// a subset of the IR, or attributes in-flight, that have to be looked at |
570 | | /// in the `updateImpl` method. |
571 | 846 | virtual void initialize(Attributor &A) {} |
572 | | |
573 | | /// Return the internal abstract state for inspection. |
574 | | virtual const AbstractState &getState() const = 0; |
575 | | |
576 | | /// Return the value this abstract attribute is anchored with. |
577 | | /// |
578 | | /// The anchored value might not be the associated value if the latter is not |
579 | | /// sufficient to determine where arguments will be manifested. This is mostly |
580 | | /// the case for call site arguments as the value is not sufficient to |
581 | | /// pinpoint them. Instead, we can use the call site as an anchor. |
582 | | /// |
583 | | ///{ |
584 | 11.5k | Value &getAnchoredValue() { return AnchoredVal; } |
585 | 111 | const Value &getAnchoredValue() const { return AnchoredVal; } |
586 | | ///} |
587 | | |
588 | | /// Return the llvm::Function surrounding the anchored value. |
589 | | /// |
590 | | ///{ |
591 | | Function &getAnchorScope(); |
592 | | const Function &getAnchorScope() const; |
593 | | ///} |
594 | | |
595 | | /// Return the value this abstract attribute is associated with. |
596 | | /// |
597 | | /// The abstract state usually represents this value. |
598 | | /// |
599 | | ///{ |
600 | 2.14k | virtual Value *getAssociatedValue() { return AssociatedVal; } |
601 | 0 | virtual const Value *getAssociatedValue() const { return AssociatedVal; } |
602 | | ///} |
603 | | |
604 | | /// Return the position this abstract state is manifested in. |
605 | | virtual ManifestPosition getManifestPosition() const = 0; |
606 | | |
607 | | /// Return the kind that identifies the abstract attribute implementation. |
608 | | virtual Attribute::AttrKind getAttrKind() const = 0; |
609 | | |
610 | | /// Return the deduced attributes in \p Attrs. |
611 | 880 | virtual void getDeducedAttributes(SmallVectorImpl<Attribute> &Attrs) const { |
612 | 880 | LLVMContext &Ctx = AnchoredVal.getContext(); |
613 | 880 | Attrs.emplace_back(Attribute::get(Ctx, getAttrKind())); |
614 | 880 | } |
615 | | |
616 | | /// Helper functions, for debug purposes only. |
617 | | ///{ |
618 | | virtual void print(raw_ostream &OS) const; |
619 | 0 | void dump() const { print(dbgs()); } |
620 | | |
621 | | /// This function should return the "summarized" assumed state as string. |
622 | | virtual const std::string getAsStr() const = 0; |
623 | | ///} |
624 | | |
625 | | /// Allow the Attributor access to the protected methods. |
626 | | friend struct Attributor; |
627 | | |
628 | | protected: |
629 | | /// Hook for the Attributor to trigger an update of the internal state. |
630 | | /// |
631 | | /// If this attribute is already fixed, this method will return UNCHANGED, |
632 | | /// otherwise it delegates to `AbstractAttribute::updateImpl`. |
633 | | /// |
634 | | /// \Return CHANGED if the internal state changed, otherwise UNCHANGED. |
635 | | ChangeStatus update(Attributor &A); |
636 | | |
637 | | /// Hook for the Attributor to trigger the manifestation of the information |
638 | | /// represented by the abstract attribute in the LLVM-IR. |
639 | | /// |
640 | | /// \Return CHANGED if the IR was altered, otherwise UNCHANGED. |
641 | | virtual ChangeStatus manifest(Attributor &A); |
642 | | |
643 | | /// Return the internal abstract state for careful modification. |
644 | | virtual AbstractState &getState() = 0; |
645 | | |
646 | | /// The actual update/transfer function which has to be implemented by the |
647 | | /// derived classes. |
648 | | /// |
649 | | /// If it is called, the environment has changed and we have to determine if |
650 | | /// the current information is still valid or adjust it otherwise. |
651 | | /// |
652 | | /// \Return CHANGED if the internal state changed, otherwise UNCHANGED. |
653 | | virtual ChangeStatus updateImpl(Attributor &A) = 0; |
654 | | |
655 | | /// The value this abstract attribute is associated with. |
656 | | Value *AssociatedVal; |
657 | | |
658 | | /// The value this abstract attribute is anchored at. |
659 | | Value &AnchoredVal; |
660 | | |
661 | | /// The information cache accessible to this abstract attribute. |
662 | | InformationCache &InfoCache; |
663 | | }; |
664 | | |
665 | | /// Forward declarations of output streams for debug purposes. |
666 | | /// |
667 | | ///{ |
668 | | raw_ostream &operator<<(raw_ostream &OS, const AbstractAttribute &AA); |
669 | | raw_ostream &operator<<(raw_ostream &OS, ChangeStatus S); |
670 | | raw_ostream &operator<<(raw_ostream &OS, AbstractAttribute::ManifestPosition); |
671 | | raw_ostream &operator<<(raw_ostream &OS, const AbstractState &State); |
672 | | ///} |
673 | | |
674 | | struct AttributorPass : public PassInfoMixin<AttributorPass> { |
675 | | PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); |
676 | | }; |
677 | | |
678 | | Pass *createAttributorLegacyPass(); |
679 | | |
680 | | /// ---------------------------------------------------------------------------- |
681 | | /// Abstract Attribute Classes |
682 | | /// ---------------------------------------------------------------------------- |
683 | | |
684 | | /// An abstract attribute for the returned values of a function. |
685 | | struct AAReturnedValues : public AbstractAttribute { |
686 | | /// See AbstractAttribute::AbstractAttribute(...). |
687 | | AAReturnedValues(Function &F, InformationCache &InfoCache) |
688 | 180 | : AbstractAttribute(F, InfoCache) {} |
689 | | |
690 | | /// Check \p Pred on all returned values. |
691 | | /// |
692 | | /// This method will evaluate \p Pred on returned values and return |
693 | | /// true if (1) all returned values are known, and (2) \p Pred returned true |
694 | | /// for all returned values. |
695 | | virtual bool |
696 | | checkForallReturnedValues(std::function<bool(Value &)> &Pred) const = 0; |
697 | | |
698 | | /// See AbstractAttribute::getAttrKind() |
699 | 91 | Attribute::AttrKind getAttrKind() const override { return ID; } |
700 | | |
701 | | /// The identifier used by the Attributor for this class of attributes. |
702 | | static constexpr Attribute::AttrKind ID = Attribute::Returned; |
703 | | }; |
704 | | |
705 | | struct AANoUnwind : public AbstractAttribute { |
706 | | /// An abstract interface for all nosync attributes. |
707 | | AANoUnwind(Value &V, InformationCache &InfoCache) |
708 | 248 | : AbstractAttribute(V, InfoCache) {} |
709 | | |
710 | | /// See AbstractAttribute::getAttrKind()/ |
711 | 217 | Attribute::AttrKind getAttrKind() const override { return ID; } |
712 | | |
713 | | static constexpr Attribute::AttrKind ID = Attribute::NoUnwind; |
714 | | |
715 | | /// Returns true if nounwind is assumed. |
716 | | virtual bool isAssumedNoUnwind() const = 0; |
717 | | |
718 | | /// Returns true if nounwind is known. |
719 | | virtual bool isKnownNoUnwind() const = 0; |
720 | | }; |
721 | | |
722 | | struct AANoSync : public AbstractAttribute { |
723 | | /// An abstract interface for all nosync attributes. |
724 | | AANoSync(Value &V, InformationCache &InfoCache) |
725 | 248 | : AbstractAttribute(V, InfoCache) {} |
726 | | |
727 | | /// See AbstractAttribute::getAttrKind(). |
728 | 174 | Attribute::AttrKind getAttrKind() const override { return ID; } |
729 | | |
730 | | static constexpr Attribute::AttrKind ID = |
731 | | Attribute::AttrKind(Attribute::NoSync); |
732 | | |
733 | | /// Returns true if "nosync" is assumed. |
734 | | virtual bool isAssumedNoSync() const = 0; |
735 | | |
736 | | /// Returns true if "nosync" is known. |
737 | | virtual bool isKnownNoSync() const = 0; |
738 | | }; |
739 | | |
740 | | /// An abstract interface for all nonnull attributes. |
741 | | struct AANonNull : public AbstractAttribute { |
742 | | |
743 | | /// See AbstractAttribute::AbstractAttribute(...). |
744 | | AANonNull(Value &V, InformationCache &InfoCache) |
745 | 336 | : AbstractAttribute(V, InfoCache) {} |
746 | | |
747 | | /// See AbstractAttribute::AbstractAttribute(...). |
748 | | AANonNull(Value *AssociatedVal, Value &AnchoredValue, |
749 | | InformationCache &InfoCache) |
750 | 375 | : AbstractAttribute(AssociatedVal, AnchoredValue, InfoCache) {} |
751 | | |
752 | | /// Return true if we assume that the underlying value is nonnull. |
753 | | virtual bool isAssumedNonNull() const = 0; |
754 | | |
755 | | /// Return true if we know that underlying value is nonnull. |
756 | | virtual bool isKnownNonNull() const = 0; |
757 | | |
758 | | /// See AbastractState::getAttrKind(). |
759 | 615 | Attribute::AttrKind getAttrKind() const override { return ID; } |
760 | | |
761 | | /// The identifier used by the Attributor for this class of attributes. |
762 | | static constexpr Attribute::AttrKind ID = Attribute::NonNull; |
763 | | }; |
764 | | |
765 | | /// An abstract attribute for norecurse. |
766 | | struct AANoRecurse : public AbstractAttribute { |
767 | | |
768 | | /// See AbstractAttribute::AbstractAttribute(...). |
769 | | AANoRecurse(Value &V, InformationCache &InfoCache) |
770 | 0 | : AbstractAttribute(V, InfoCache) {} |
771 | | |
772 | | /// See AbstractAttribute::getAttrKind() |
773 | 0 | virtual Attribute::AttrKind getAttrKind() const override { |
774 | 0 | return Attribute::NoRecurse; |
775 | 0 | } |
776 | | |
777 | | /// Return true if "norecurse" is known. |
778 | | virtual bool isKnownNoRecurse() const = 0; |
779 | | |
780 | | /// Return true if "norecurse" is assumed. |
781 | | virtual bool isAssumedNoRecurse() const = 0; |
782 | | |
783 | | /// The identifier used by the Attributor for this class of attributes. |
784 | | static constexpr Attribute::AttrKind ID = Attribute::NoRecurse; |
785 | | }; |
786 | | |
787 | | /// An abstract attribute for willreturn. |
788 | | struct AAWillReturn : public AbstractAttribute { |
789 | | |
790 | | /// See AbstractAttribute::AbstractAttribute(...). |
791 | | AAWillReturn(Value &V, InformationCache &InfoCache) |
792 | 248 | : AbstractAttribute(V, InfoCache) {} |
793 | | |
794 | | /// See AbstractAttribute::getAttrKind() |
795 | 73 | virtual Attribute::AttrKind getAttrKind() const override { |
796 | 73 | return Attribute::WillReturn; |
797 | 73 | } |
798 | | |
799 | | /// Return true if "willreturn" is known. |
800 | | virtual bool isKnownWillReturn() const = 0; |
801 | | |
802 | | /// Return true if "willreturn" is assumed. |
803 | | virtual bool isAssumedWillReturn() const = 0; |
804 | | |
805 | | /// The identifier used by the Attributor for this class of attributes. |
806 | | static constexpr Attribute::AttrKind ID = Attribute::WillReturn; |
807 | | }; |
808 | | |
809 | | /// An abstract interface for all noalias attributes. |
810 | | struct AANoAlias : public AbstractAttribute { |
811 | | |
812 | | /// See AbstractAttribute::AbstractAttribute(...). |
813 | | AANoAlias(Value &V, InformationCache &InfoCache) |
814 | 120 | : AbstractAttribute(V, InfoCache) {} |
815 | | |
816 | | /// Return true if we assume that the underlying value is alias. |
817 | | virtual bool isAssumedNoAlias() const = 0; |
818 | | |
819 | | /// Return true if we know that underlying value is noalias. |
820 | | virtual bool isKnownNoAlias() const = 0; |
821 | | |
822 | | /// See AbastractState::getAttrKind(). |
823 | 20 | Attribute::AttrKind getAttrKind() const override { return ID; } |
824 | | |
825 | | /// The identifier used by the Attributor for this class of attributes. |
826 | | static constexpr Attribute::AttrKind ID = Attribute::NoAlias; |
827 | | }; |
828 | | |
829 | | /// An AbstractAttribute for noreturn. |
830 | | struct AANoReturn : public AbstractAttribute { |
831 | | |
832 | | /// See AbstractAttribute::AbstractAttribute(...). |
833 | | AANoReturn(Value &V, InformationCache &InfoCache) |
834 | 0 | : AbstractAttribute(V, InfoCache) {} |
835 | | |
836 | | /// Return true if the underlying object is known to never return. |
837 | | virtual bool isKnownNoReturn() const = 0; |
838 | | |
839 | | /// Return true if the underlying object is assumed to never return. |
840 | | virtual bool isAssumedNoReturn() const = 0; |
841 | | |
842 | | /// See AbstractAttribute::getAttrKind() |
843 | 0 | Attribute::AttrKind getAttrKind() const override { return ID; } |
844 | | |
845 | | /// The identifier used by the Attributor for this class of attributes. |
846 | | static constexpr Attribute::AttrKind ID = Attribute::NoReturn; |
847 | | }; |
848 | | |
849 | | /// An abstract interface for liveness abstract attribute. |
850 | | struct AAIsDead : public AbstractAttribute { |
851 | | |
852 | | /// See AbstractAttribute::AbstractAttribute(...). |
853 | | AAIsDead(Value &V, InformationCache &InfoCache) |
854 | 248 | : AbstractAttribute(V, InfoCache) {} |
855 | | |
856 | | /// See AbstractAttribute::getAttrKind() |
857 | 0 | Attribute::AttrKind getAttrKind() const override { return ID; } |
858 | | |
859 | | static constexpr Attribute::AttrKind ID = |
860 | | Attribute::AttrKind(Attribute::EndAttrKinds + 1); |
861 | | |
862 | | /// Returns true if \p BB is assumed dead. |
863 | | virtual bool isAssumedDead(BasicBlock *BB) const = 0; |
864 | | |
865 | | /// Returns true if \p BB is known dead. |
866 | | virtual bool isKnownDead(BasicBlock *BB) const = 0; |
867 | | }; |
868 | | |
869 | | /// An abstract interface for all dereferenceable attribute. |
870 | | struct AADereferenceable : public AbstractAttribute { |
871 | | |
872 | | /// See AbstractAttribute::AbstractAttribute(...). |
873 | | AADereferenceable(Value &V, InformationCache &InfoCache) |
874 | 336 | : AbstractAttribute(V, InfoCache) {} |
875 | | |
876 | | /// See AbstractAttribute::AbstractAttribute(...). |
877 | | AADereferenceable(Value *AssociatedVal, Value &AnchoredValue, |
878 | | InformationCache &InfoCache) |
879 | 375 | : AbstractAttribute(AssociatedVal, AnchoredValue, InfoCache) {} |
880 | | |
881 | | /// Return true if we assume that the underlying value is nonnull. |
882 | | virtual bool isAssumedNonNull() const = 0; |
883 | | |
884 | | /// Return true if we know that underlying value is nonnull. |
885 | | virtual bool isKnownNonNull() const = 0; |
886 | | |
887 | | /// Return true if we assume that underlying value is |
888 | | /// dereferenceable(_or_null) globally. |
889 | | virtual bool isAssumedGlobal() const = 0; |
890 | | |
891 | | /// Return true if we know that underlying value is |
892 | | /// dereferenceable(_or_null) globally. |
893 | | virtual bool isKnownGlobal() const = 0; |
894 | | |
895 | | /// Return assumed dereferenceable bytes. |
896 | | virtual uint32_t getAssumedDereferenceableBytes() const = 0; |
897 | | |
898 | | /// Return known dereferenceable bytes. |
899 | | virtual uint32_t getKnownDereferenceableBytes() const = 0; |
900 | | |
901 | | /// See AbastractState::getAttrKind(). |
902 | 0 | Attribute::AttrKind getAttrKind() const override { return ID; } |
903 | | |
904 | | /// The identifier used by the Attributor for this class of attributes. |
905 | | static constexpr Attribute::AttrKind ID = Attribute::Dereferenceable; |
906 | | }; |
907 | | |
908 | | } // end namespace llvm |
909 | | |
910 | | #endif // LLVM_TRANSFORMS_IPO_FUNCTIONATTRS_H |