File: | build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/llvm/lib/Transforms/IPO/AttributorAttributes.cpp |
Warning: | line 1315, column 15 1st function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- AttributorAttributes.cpp - Attributes for Attributor deduction -----===// | ||||
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 | // See the Attributor.h file comment and the class descriptions in that file for | ||||
10 | // more information. | ||||
11 | // | ||||
12 | //===----------------------------------------------------------------------===// | ||||
13 | |||||
14 | #include "llvm/Transforms/IPO/Attributor.h" | ||||
15 | |||||
16 | #include "llvm/ADT/APInt.h" | ||||
17 | #include "llvm/ADT/MapVector.h" | ||||
18 | #include "llvm/ADT/SCCIterator.h" | ||||
19 | #include "llvm/ADT/STLExtras.h" | ||||
20 | #include "llvm/ADT/SetOperations.h" | ||||
21 | #include "llvm/ADT/SmallPtrSet.h" | ||||
22 | #include "llvm/ADT/Statistic.h" | ||||
23 | #include "llvm/Analysis/AliasAnalysis.h" | ||||
24 | #include "llvm/Analysis/AssumeBundleQueries.h" | ||||
25 | #include "llvm/Analysis/AssumptionCache.h" | ||||
26 | #include "llvm/Analysis/CaptureTracking.h" | ||||
27 | #include "llvm/Analysis/InstructionSimplify.h" | ||||
28 | #include "llvm/Analysis/LazyValueInfo.h" | ||||
29 | #include "llvm/Analysis/MemoryBuiltins.h" | ||||
30 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||||
31 | #include "llvm/Analysis/ScalarEvolution.h" | ||||
32 | #include "llvm/Analysis/TargetTransformInfo.h" | ||||
33 | #include "llvm/Analysis/ValueTracking.h" | ||||
34 | #include "llvm/IR/Assumptions.h" | ||||
35 | #include "llvm/IR/Constants.h" | ||||
36 | #include "llvm/IR/DataLayout.h" | ||||
37 | #include "llvm/IR/IRBuilder.h" | ||||
38 | #include "llvm/IR/Instruction.h" | ||||
39 | #include "llvm/IR/Instructions.h" | ||||
40 | #include "llvm/IR/IntrinsicInst.h" | ||||
41 | #include "llvm/IR/Value.h" | ||||
42 | #include "llvm/IR/NoFolder.h" | ||||
43 | #include "llvm/Support/Alignment.h" | ||||
44 | #include "llvm/Support/Casting.h" | ||||
45 | #include "llvm/Support/CommandLine.h" | ||||
46 | #include "llvm/Support/ErrorHandling.h" | ||||
47 | #include "llvm/Support/FileSystem.h" | ||||
48 | #include "llvm/Support/MathExtras.h" | ||||
49 | #include "llvm/Support/raw_ostream.h" | ||||
50 | #include "llvm/Transforms/IPO/ArgumentPromotion.h" | ||||
51 | #include "llvm/Transforms/Utils/Local.h" | ||||
52 | #include <cassert> | ||||
53 | |||||
54 | using namespace llvm; | ||||
55 | |||||
56 | #define DEBUG_TYPE"attributor" "attributor" | ||||
57 | |||||
58 | static cl::opt<bool> ManifestInternal( | ||||
59 | "attributor-manifest-internal", cl::Hidden, | ||||
60 | cl::desc("Manifest Attributor internal string attributes."), | ||||
61 | cl::init(false)); | ||||
62 | |||||
63 | static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128), | ||||
64 | cl::Hidden); | ||||
65 | |||||
66 | template <> | ||||
67 | unsigned llvm::PotentialConstantIntValuesState::MaxPotentialValues = 0; | ||||
68 | |||||
69 | static cl::opt<unsigned, true> MaxPotentialValues( | ||||
70 | "attributor-max-potential-values", cl::Hidden, | ||||
71 | cl::desc("Maximum number of potential values to be " | ||||
72 | "tracked for each position."), | ||||
73 | cl::location(llvm::PotentialConstantIntValuesState::MaxPotentialValues), | ||||
74 | cl::init(7)); | ||||
75 | |||||
76 | static cl::opt<unsigned> | ||||
77 | MaxInterferingWrites("attributor-max-interfering-writes", cl::Hidden, | ||||
78 | cl::desc("Maximum number of interfering writes to " | ||||
79 | "check before assuming all might interfere."), | ||||
80 | cl::init(6)); | ||||
81 | |||||
82 | STATISTIC(NumAAs, "Number of abstract attributes created")static llvm::Statistic NumAAs = {"attributor", "NumAAs", "Number of abstract attributes created" }; | ||||
83 | |||||
84 | // Some helper macros to deal with statistics tracking. | ||||
85 | // | ||||
86 | // Usage: | ||||
87 | // For simple IR attribute tracking overload trackStatistics in the abstract | ||||
88 | // attribute and choose the right STATS_DECLTRACK_********* macro, | ||||
89 | // e.g.,: | ||||
90 | // void trackStatistics() const override { | ||||
91 | // STATS_DECLTRACK_ARG_ATTR(returned) | ||||
92 | // } | ||||
93 | // If there is a single "increment" side one can use the macro | ||||
94 | // STATS_DECLTRACK with a custom message. If there are multiple increment | ||||
95 | // sides, STATS_DECL and STATS_TRACK can also be used separately. | ||||
96 | // | ||||
97 | #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)("Number of " "TYPE" " marked '" "NAME" "'") \ | ||||
98 | ("Number of " #TYPE " marked '" #NAME "'") | ||||
99 | #define BUILD_STAT_NAME(NAME, TYPE)NumIRTYPE_NAME NumIR##TYPE##_##NAME | ||||
100 | #define STATS_DECL_(NAME, MSG)static llvm::Statistic NAME = {"attributor", "NAME", MSG}; STATISTIC(NAME, MSG)static llvm::Statistic NAME = {"attributor", "NAME", MSG}; | ||||
101 | #define STATS_DECL(NAME, TYPE, MSG)static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME" , MSG};; \ | ||||
102 | STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG)static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME" , MSG};; | ||||
103 | #define STATS_TRACK(NAME, TYPE)++(NumIRTYPE_NAME); ++(BUILD_STAT_NAME(NAME, TYPE)NumIRTYPE_NAME); | ||||
104 | #define STATS_DECLTRACK(NAME, TYPE, MSG){ static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME" , MSG};; ++(NumIRTYPE_NAME); } \ | ||||
105 | { \ | ||||
106 | STATS_DECL(NAME, TYPE, MSG)static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME" , MSG};; \ | ||||
107 | STATS_TRACK(NAME, TYPE)++(NumIRTYPE_NAME); \ | ||||
108 | } | ||||
109 | #define STATS_DECLTRACK_ARG_ATTR(NAME){ static llvm::Statistic NumIRArguments_NAME = {"attributor", "NumIRArguments_NAME", ("Number of " "arguments" " marked '" "NAME" "'")};; ++(NumIRArguments_NAME); } \ | ||||
110 | STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME)){ static llvm::Statistic NumIRArguments_NAME = {"attributor", "NumIRArguments_NAME", ("Number of " "arguments" " marked '" "NAME" "'")};; ++(NumIRArguments_NAME); } | ||||
111 | #define STATS_DECLTRACK_CSARG_ATTR(NAME){ static llvm::Statistic NumIRCSArguments_NAME = {"attributor" , "NumIRCSArguments_NAME", ("Number of " "call site arguments" " marked '" "NAME" "'")};; ++(NumIRCSArguments_NAME); } \ | ||||
112 | STATS_DECLTRACK(NAME, CSArguments, \{ static llvm::Statistic NumIRCSArguments_NAME = {"attributor" , "NumIRCSArguments_NAME", ("Number of " "call site arguments" " marked '" "NAME" "'")};; ++(NumIRCSArguments_NAME); } | ||||
113 | BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME)){ static llvm::Statistic NumIRCSArguments_NAME = {"attributor" , "NumIRCSArguments_NAME", ("Number of " "call site arguments" " marked '" "NAME" "'")};; ++(NumIRCSArguments_NAME); } | ||||
114 | #define STATS_DECLTRACK_FN_ATTR(NAME){ static llvm::Statistic NumIRFunction_NAME = {"attributor", "NumIRFunction_NAME" , ("Number of " "functions" " marked '" "NAME" "'")};; ++(NumIRFunction_NAME ); } \ | ||||
115 | STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME)){ static llvm::Statistic NumIRFunction_NAME = {"attributor", "NumIRFunction_NAME" , ("Number of " "functions" " marked '" "NAME" "'")};; ++(NumIRFunction_NAME ); } | ||||
116 | #define STATS_DECLTRACK_CS_ATTR(NAME){ static llvm::Statistic NumIRCS_NAME = {"attributor", "NumIRCS_NAME" , ("Number of " "call site" " marked '" "NAME" "'")};; ++(NumIRCS_NAME ); } \ | ||||
117 | STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME)){ static llvm::Statistic NumIRCS_NAME = {"attributor", "NumIRCS_NAME" , ("Number of " "call site" " marked '" "NAME" "'")};; ++(NumIRCS_NAME ); } | ||||
118 | #define STATS_DECLTRACK_FNRET_ATTR(NAME){ static llvm::Statistic NumIRFunctionReturn_NAME = {"attributor" , "NumIRFunctionReturn_NAME", ("Number of " "function returns" " marked '" "NAME" "'")};; ++(NumIRFunctionReturn_NAME); } \ | ||||
119 | STATS_DECLTRACK(NAME, FunctionReturn, \{ static llvm::Statistic NumIRFunctionReturn_NAME = {"attributor" , "NumIRFunctionReturn_NAME", ("Number of " "function returns" " marked '" "NAME" "'")};; ++(NumIRFunctionReturn_NAME); } | ||||
120 | BUILD_STAT_MSG_IR_ATTR(function returns, NAME)){ static llvm::Statistic NumIRFunctionReturn_NAME = {"attributor" , "NumIRFunctionReturn_NAME", ("Number of " "function returns" " marked '" "NAME" "'")};; ++(NumIRFunctionReturn_NAME); } | ||||
121 | #define STATS_DECLTRACK_CSRET_ATTR(NAME){ static llvm::Statistic NumIRCSReturn_NAME = {"attributor", "NumIRCSReturn_NAME" , ("Number of " "call site returns" " marked '" "NAME" "'")}; ; ++(NumIRCSReturn_NAME); } \ | ||||
122 | STATS_DECLTRACK(NAME, CSReturn, \{ static llvm::Statistic NumIRCSReturn_NAME = {"attributor", "NumIRCSReturn_NAME" , ("Number of " "call site returns" " marked '" "NAME" "'")}; ; ++(NumIRCSReturn_NAME); } | ||||
123 | BUILD_STAT_MSG_IR_ATTR(call site returns, NAME)){ static llvm::Statistic NumIRCSReturn_NAME = {"attributor", "NumIRCSReturn_NAME" , ("Number of " "call site returns" " marked '" "NAME" "'")}; ; ++(NumIRCSReturn_NAME); } | ||||
124 | #define STATS_DECLTRACK_FLOATING_ATTR(NAME){ static llvm::Statistic NumIRFloating_NAME = {"attributor", "NumIRFloating_NAME" , ("Number of floating values known to be '" "NAME" "'")};; ++ (NumIRFloating_NAME); } \ | ||||
125 | STATS_DECLTRACK(NAME, Floating, \{ static llvm::Statistic NumIRFloating_NAME = {"attributor", "NumIRFloating_NAME" , ("Number of floating values known to be '" #NAME "'")};; ++ (NumIRFloating_NAME); } | ||||
126 | ("Number of floating values known to be '" #NAME "'")){ static llvm::Statistic NumIRFloating_NAME = {"attributor", "NumIRFloating_NAME" , ("Number of floating values known to be '" #NAME "'")};; ++ (NumIRFloating_NAME); } | ||||
127 | |||||
128 | // Specialization of the operator<< for abstract attributes subclasses. This | ||||
129 | // disambiguates situations where multiple operators are applicable. | ||||
130 | namespace llvm { | ||||
131 | #define PIPE_OPERATOR(CLASS) \ | ||||
132 | raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \ | ||||
133 | return OS << static_cast<const AbstractAttribute &>(AA); \ | ||||
134 | } | ||||
135 | |||||
136 | PIPE_OPERATOR(AAIsDead) | ||||
137 | PIPE_OPERATOR(AANoUnwind) | ||||
138 | PIPE_OPERATOR(AANoSync) | ||||
139 | PIPE_OPERATOR(AANoRecurse) | ||||
140 | PIPE_OPERATOR(AAWillReturn) | ||||
141 | PIPE_OPERATOR(AANoReturn) | ||||
142 | PIPE_OPERATOR(AAReturnedValues) | ||||
143 | PIPE_OPERATOR(AANonNull) | ||||
144 | PIPE_OPERATOR(AANoAlias) | ||||
145 | PIPE_OPERATOR(AADereferenceable) | ||||
146 | PIPE_OPERATOR(AAAlign) | ||||
147 | PIPE_OPERATOR(AANoCapture) | ||||
148 | PIPE_OPERATOR(AAValueSimplify) | ||||
149 | PIPE_OPERATOR(AANoFree) | ||||
150 | PIPE_OPERATOR(AAHeapToStack) | ||||
151 | PIPE_OPERATOR(AAReachability) | ||||
152 | PIPE_OPERATOR(AAMemoryBehavior) | ||||
153 | PIPE_OPERATOR(AAMemoryLocation) | ||||
154 | PIPE_OPERATOR(AAValueConstantRange) | ||||
155 | PIPE_OPERATOR(AAPrivatizablePtr) | ||||
156 | PIPE_OPERATOR(AAUndefinedBehavior) | ||||
157 | PIPE_OPERATOR(AAPotentialValues) | ||||
158 | PIPE_OPERATOR(AANoUndef) | ||||
159 | PIPE_OPERATOR(AACallEdges) | ||||
160 | PIPE_OPERATOR(AAFunctionReachability) | ||||
161 | PIPE_OPERATOR(AAPointerInfo) | ||||
162 | PIPE_OPERATOR(AAAssumptionInfo) | ||||
163 | |||||
164 | #undef PIPE_OPERATOR | ||||
165 | |||||
166 | template <> | ||||
167 | ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S, | ||||
168 | const DerefState &R) { | ||||
169 | ChangeStatus CS0 = | ||||
170 | clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState); | ||||
171 | ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState); | ||||
172 | return CS0 | CS1; | ||||
173 | } | ||||
174 | |||||
175 | } // namespace llvm | ||||
176 | |||||
177 | /// Get pointer operand of memory accessing instruction. If \p I is | ||||
178 | /// not a memory accessing instruction, return nullptr. If \p AllowVolatile, | ||||
179 | /// is set to false and the instruction is volatile, return nullptr. | ||||
180 | static const Value *getPointerOperand(const Instruction *I, | ||||
181 | bool AllowVolatile) { | ||||
182 | if (!AllowVolatile && I->isVolatile()) | ||||
183 | return nullptr; | ||||
184 | |||||
185 | if (auto *LI = dyn_cast<LoadInst>(I)) { | ||||
186 | return LI->getPointerOperand(); | ||||
187 | } | ||||
188 | |||||
189 | if (auto *SI = dyn_cast<StoreInst>(I)) { | ||||
190 | return SI->getPointerOperand(); | ||||
191 | } | ||||
192 | |||||
193 | if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) { | ||||
194 | return CXI->getPointerOperand(); | ||||
195 | } | ||||
196 | |||||
197 | if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) { | ||||
198 | return RMWI->getPointerOperand(); | ||||
199 | } | ||||
200 | |||||
201 | return nullptr; | ||||
202 | } | ||||
203 | |||||
204 | /// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and | ||||
205 | /// advanced by \p Offset bytes. To aid later analysis the method tries to build | ||||
206 | /// getelement pointer instructions that traverse the natural type of \p Ptr if | ||||
207 | /// possible. If that fails, the remaining offset is adjusted byte-wise, hence | ||||
208 | /// through a cast to i8*. | ||||
209 | /// | ||||
210 | /// TODO: This could probably live somewhere more prominantly if it doesn't | ||||
211 | /// already exist. | ||||
212 | static Value *constructPointer(Type *ResTy, Type *PtrElemTy, Value *Ptr, | ||||
213 | int64_t Offset, IRBuilder<NoFolder> &IRB, | ||||
214 | const DataLayout &DL) { | ||||
215 | assert(Offset >= 0 && "Negative offset not supported yet!")(static_cast <bool> (Offset >= 0 && "Negative offset not supported yet!" ) ? void (0) : __assert_fail ("Offset >= 0 && \"Negative offset not supported yet!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 215, __extension__ __PRETTY_FUNCTION__)); | ||||
216 | LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offsetdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Construct pointer: " << *Ptr << " + " << Offset << "-bytes as " << *ResTy << "\n"; } } while (false) | ||||
217 | << "-bytes as " << *ResTy << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Construct pointer: " << *Ptr << " + " << Offset << "-bytes as " << *ResTy << "\n"; } } while (false); | ||||
218 | |||||
219 | if (Offset) { | ||||
220 | Type *Ty = PtrElemTy; | ||||
221 | APInt IntOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), Offset); | ||||
222 | SmallVector<APInt> IntIndices = DL.getGEPIndicesForOffset(Ty, IntOffset); | ||||
223 | |||||
224 | SmallVector<Value *, 4> ValIndices; | ||||
225 | std::string GEPName = Ptr->getName().str(); | ||||
226 | for (const APInt &Index : IntIndices) { | ||||
227 | ValIndices.push_back(IRB.getInt(Index)); | ||||
228 | GEPName += "." + std::to_string(Index.getZExtValue()); | ||||
229 | } | ||||
230 | |||||
231 | // Create a GEP for the indices collected above. | ||||
232 | Ptr = IRB.CreateGEP(PtrElemTy, Ptr, ValIndices, GEPName); | ||||
233 | |||||
234 | // If an offset is left we use byte-wise adjustment. | ||||
235 | if (IntOffset != 0) { | ||||
236 | Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy()); | ||||
237 | Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt(IntOffset), | ||||
238 | GEPName + ".b" + Twine(IntOffset.getZExtValue())); | ||||
239 | } | ||||
240 | } | ||||
241 | |||||
242 | // Ensure the result has the requested type. | ||||
243 | Ptr = IRB.CreatePointerBitCastOrAddrSpaceCast(Ptr, ResTy, | ||||
244 | Ptr->getName() + ".cast"); | ||||
245 | |||||
246 | LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Constructed pointer: " << *Ptr << "\n"; } } while (false); | ||||
247 | return Ptr; | ||||
248 | } | ||||
249 | |||||
250 | /// Recursively visit all values that might become \p IRP at some point. This | ||||
251 | /// will be done by looking through cast instructions, selects, phis, and calls | ||||
252 | /// with the "returned" attribute. Once we cannot look through the value any | ||||
253 | /// further, the callback \p VisitValueCB is invoked and passed the current | ||||
254 | /// value, the \p State, and a flag to indicate if we stripped anything. | ||||
255 | /// Stripped means that we unpacked the value associated with \p IRP at least | ||||
256 | /// once. Note that the value used for the callback may still be the value | ||||
257 | /// associated with \p IRP (due to PHIs). To limit how much effort is invested, | ||||
258 | /// we will never visit more values than specified by \p MaxValues. | ||||
259 | /// If \p Intraprocedural is set to true only values valid in the scope of | ||||
260 | /// \p CtxI will be visited and simplification into other scopes is prevented. | ||||
261 | template <typename StateTy> | ||||
262 | static bool genericValueTraversal( | ||||
263 | Attributor &A, IRPosition IRP, const AbstractAttribute &QueryingAA, | ||||
264 | StateTy &State, | ||||
265 | function_ref<bool(Value &, const Instruction *, StateTy &, bool)> | ||||
266 | VisitValueCB, | ||||
267 | const Instruction *CtxI, bool &UsedAssumedInformation, | ||||
268 | bool UseValueSimplify = true, int MaxValues = 16, | ||||
269 | function_ref<Value *(Value *)> StripCB = nullptr, | ||||
270 | bool Intraprocedural = false) { | ||||
271 | |||||
272 | struct LivenessInfo { | ||||
273 | const AAIsDead *LivenessAA = nullptr; | ||||
274 | bool AnyDead = false; | ||||
275 | }; | ||||
276 | SmallMapVector<const Function *, LivenessInfo, 4> LivenessAAs; | ||||
277 | auto GetLivenessInfo = [&](const Function &F) -> LivenessInfo & { | ||||
278 | LivenessInfo &LI = LivenessAAs[&F]; | ||||
279 | if (!LI.LivenessAA) | ||||
280 | LI.LivenessAA = &A.getAAFor<AAIsDead>(QueryingAA, IRPosition::function(F), | ||||
281 | DepClassTy::NONE); | ||||
282 | return LI; | ||||
283 | }; | ||||
284 | |||||
285 | Value *InitialV = &IRP.getAssociatedValue(); | ||||
286 | using Item = std::pair<Value *, const Instruction *>; | ||||
287 | SmallSet<Item, 16> Visited; | ||||
288 | SmallVector<Item, 16> Worklist; | ||||
289 | Worklist.push_back({InitialV, CtxI}); | ||||
290 | |||||
291 | int Iteration = 0; | ||||
292 | do { | ||||
293 | Item I = Worklist.pop_back_val(); | ||||
294 | Value *V = I.first; | ||||
295 | CtxI = I.second; | ||||
296 | if (StripCB) | ||||
297 | V = StripCB(V); | ||||
298 | |||||
299 | // Check if we should process the current value. To prevent endless | ||||
300 | // recursion keep a record of the values we followed! | ||||
301 | if (!Visited.insert(I).second) | ||||
302 | continue; | ||||
303 | |||||
304 | // Make sure we limit the compile time for complex expressions. | ||||
305 | if (Iteration++ >= MaxValues) { | ||||
306 | LLVM_DEBUG(dbgs() << "Generic value traversal reached iteration limit: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Generic value traversal reached iteration limit: " << Iteration << "!\n"; } } while (false) | ||||
307 | << Iteration << "!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Generic value traversal reached iteration limit: " << Iteration << "!\n"; } } while (false); | ||||
308 | return false; | ||||
309 | } | ||||
310 | |||||
311 | // Explicitly look through calls with a "returned" attribute if we do | ||||
312 | // not have a pointer as stripPointerCasts only works on them. | ||||
313 | Value *NewV = nullptr; | ||||
314 | if (V->getType()->isPointerTy()) { | ||||
315 | NewV = V->stripPointerCasts(); | ||||
316 | } else { | ||||
317 | auto *CB = dyn_cast<CallBase>(V); | ||||
318 | if (CB && CB->getCalledFunction()) { | ||||
319 | for (Argument &Arg : CB->getCalledFunction()->args()) | ||||
320 | if (Arg.hasReturnedAttr()) { | ||||
321 | NewV = CB->getArgOperand(Arg.getArgNo()); | ||||
322 | break; | ||||
323 | } | ||||
324 | } | ||||
325 | } | ||||
326 | if (NewV && NewV != V) { | ||||
327 | Worklist.push_back({NewV, CtxI}); | ||||
328 | continue; | ||||
329 | } | ||||
330 | |||||
331 | // Look through select instructions, visit assumed potential values. | ||||
332 | if (auto *SI = dyn_cast<SelectInst>(V)) { | ||||
333 | Optional<Constant *> C = A.getAssumedConstant( | ||||
334 | *SI->getCondition(), QueryingAA, UsedAssumedInformation); | ||||
335 | bool NoValueYet = !C.hasValue(); | ||||
336 | if (NoValueYet || isa_and_nonnull<UndefValue>(*C)) | ||||
337 | continue; | ||||
338 | if (auto *CI = dyn_cast_or_null<ConstantInt>(*C)) { | ||||
339 | if (CI->isZero()) | ||||
340 | Worklist.push_back({SI->getFalseValue(), CtxI}); | ||||
341 | else | ||||
342 | Worklist.push_back({SI->getTrueValue(), CtxI}); | ||||
343 | continue; | ||||
344 | } | ||||
345 | // We could not simplify the condition, assume both values.( | ||||
346 | Worklist.push_back({SI->getTrueValue(), CtxI}); | ||||
347 | Worklist.push_back({SI->getFalseValue(), CtxI}); | ||||
348 | continue; | ||||
349 | } | ||||
350 | |||||
351 | // Look through phi nodes, visit all live operands. | ||||
352 | if (auto *PHI = dyn_cast<PHINode>(V)) { | ||||
353 | LivenessInfo &LI = GetLivenessInfo(*PHI->getFunction()); | ||||
354 | for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { | ||||
355 | BasicBlock *IncomingBB = PHI->getIncomingBlock(u); | ||||
356 | if (LI.LivenessAA->isEdgeDead(IncomingBB, PHI->getParent())) { | ||||
357 | LI.AnyDead = true; | ||||
358 | UsedAssumedInformation |= !LI.LivenessAA->isAtFixpoint(); | ||||
359 | continue; | ||||
360 | } | ||||
361 | Worklist.push_back( | ||||
362 | {PHI->getIncomingValue(u), IncomingBB->getTerminator()}); | ||||
363 | } | ||||
364 | continue; | ||||
365 | } | ||||
366 | |||||
367 | if (auto *Arg = dyn_cast<Argument>(V)) { | ||||
368 | if (!Intraprocedural && !Arg->hasPassPointeeByValueCopyAttr()) { | ||||
369 | SmallVector<Item> CallSiteValues; | ||||
370 | bool UsedAssumedInformation = false; | ||||
371 | if (A.checkForAllCallSites( | ||||
372 | [&](AbstractCallSite ACS) { | ||||
373 | // Callbacks might not have a corresponding call site operand, | ||||
374 | // stick with the argument in that case. | ||||
375 | Value *CSOp = ACS.getCallArgOperand(*Arg); | ||||
376 | if (!CSOp) | ||||
377 | return false; | ||||
378 | CallSiteValues.push_back({CSOp, ACS.getInstruction()}); | ||||
379 | return true; | ||||
380 | }, | ||||
381 | *Arg->getParent(), true, &QueryingAA, UsedAssumedInformation)) { | ||||
382 | Worklist.append(CallSiteValues); | ||||
383 | continue; | ||||
384 | } | ||||
385 | } | ||||
386 | } | ||||
387 | |||||
388 | if (UseValueSimplify && !isa<Constant>(V)) { | ||||
389 | Optional<Value *> SimpleV = | ||||
390 | A.getAssumedSimplified(*V, QueryingAA, UsedAssumedInformation); | ||||
391 | if (!SimpleV.hasValue()) | ||||
392 | continue; | ||||
393 | Value *NewV = SimpleV.getValue(); | ||||
394 | if (NewV && NewV != V) { | ||||
395 | if (!Intraprocedural || !CtxI || | ||||
396 | AA::isValidInScope(*NewV, CtxI->getFunction())) { | ||||
397 | Worklist.push_back({NewV, CtxI}); | ||||
398 | continue; | ||||
399 | } | ||||
400 | } | ||||
401 | } | ||||
402 | |||||
403 | // Once a leaf is reached we inform the user through the callback. | ||||
404 | if (!VisitValueCB(*V, CtxI, State, Iteration > 1)) { | ||||
405 | LLVM_DEBUG(dbgs() << "Generic value traversal visit callback failed for: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Generic value traversal visit callback failed for: " << *V << "!\n"; } } while (false) | ||||
406 | << *V << "!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Generic value traversal visit callback failed for: " << *V << "!\n"; } } while (false); | ||||
407 | return false; | ||||
408 | } | ||||
409 | } while (!Worklist.empty()); | ||||
410 | |||||
411 | // If we actually used liveness information so we have to record a dependence. | ||||
412 | for (auto &It : LivenessAAs) | ||||
413 | if (It.second.AnyDead) | ||||
414 | A.recordDependence(*It.second.LivenessAA, QueryingAA, | ||||
415 | DepClassTy::OPTIONAL); | ||||
416 | |||||
417 | // All values have been visited. | ||||
418 | return true; | ||||
419 | } | ||||
420 | |||||
421 | bool AA::getAssumedUnderlyingObjects(Attributor &A, const Value &Ptr, | ||||
422 | SmallVectorImpl<Value *> &Objects, | ||||
423 | const AbstractAttribute &QueryingAA, | ||||
424 | const Instruction *CtxI, | ||||
425 | bool &UsedAssumedInformation, | ||||
426 | bool Intraprocedural) { | ||||
427 | auto StripCB = [&](Value *V) { return getUnderlyingObject(V); }; | ||||
428 | SmallPtrSet<Value *, 8> SeenObjects; | ||||
429 | auto VisitValueCB = [&SeenObjects](Value &Val, const Instruction *, | ||||
430 | SmallVectorImpl<Value *> &Objects, | ||||
431 | bool) -> bool { | ||||
432 | if (SeenObjects.insert(&Val).second) | ||||
433 | Objects.push_back(&Val); | ||||
434 | return true; | ||||
435 | }; | ||||
436 | if (!genericValueTraversal<decltype(Objects)>( | ||||
437 | A, IRPosition::value(Ptr), QueryingAA, Objects, VisitValueCB, CtxI, | ||||
438 | UsedAssumedInformation, true, 32, StripCB, Intraprocedural)) | ||||
439 | return false; | ||||
440 | return true; | ||||
441 | } | ||||
442 | |||||
443 | const Value *stripAndAccumulateMinimalOffsets( | ||||
444 | Attributor &A, const AbstractAttribute &QueryingAA, const Value *Val, | ||||
445 | const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, | ||||
446 | bool UseAssumed = false) { | ||||
447 | |||||
448 | auto AttributorAnalysis = [&](Value &V, APInt &ROffset) -> bool { | ||||
449 | const IRPosition &Pos = IRPosition::value(V); | ||||
450 | // Only track dependence if we are going to use the assumed info. | ||||
451 | const AAValueConstantRange &ValueConstantRangeAA = | ||||
452 | A.getAAFor<AAValueConstantRange>(QueryingAA, Pos, | ||||
453 | UseAssumed ? DepClassTy::OPTIONAL | ||||
454 | : DepClassTy::NONE); | ||||
455 | ConstantRange Range = UseAssumed ? ValueConstantRangeAA.getAssumed() | ||||
456 | : ValueConstantRangeAA.getKnown(); | ||||
457 | // We can only use the lower part of the range because the upper part can | ||||
458 | // be higher than what the value can really be. | ||||
459 | ROffset = Range.getSignedMin(); | ||||
460 | return true; | ||||
461 | }; | ||||
462 | |||||
463 | return Val->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds, | ||||
464 | /* AllowInvariant */ false, | ||||
465 | AttributorAnalysis); | ||||
466 | } | ||||
467 | |||||
468 | static const Value * | ||||
469 | getMinimalBaseOfPointer(Attributor &A, const AbstractAttribute &QueryingAA, | ||||
470 | const Value *Ptr, int64_t &BytesOffset, | ||||
471 | const DataLayout &DL, bool AllowNonInbounds = false) { | ||||
472 | APInt OffsetAPInt(DL.getIndexTypeSizeInBits(Ptr->getType()), 0); | ||||
473 | const Value *Base = stripAndAccumulateMinimalOffsets( | ||||
474 | A, QueryingAA, Ptr, DL, OffsetAPInt, AllowNonInbounds); | ||||
475 | |||||
476 | BytesOffset = OffsetAPInt.getSExtValue(); | ||||
477 | return Base; | ||||
478 | } | ||||
479 | |||||
480 | /// Clamp the information known for all returned values of a function | ||||
481 | /// (identified by \p QueryingAA) into \p S. | ||||
482 | template <typename AAType, typename StateType = typename AAType::StateType> | ||||
483 | static void clampReturnedValueStates( | ||||
484 | Attributor &A, const AAType &QueryingAA, StateType &S, | ||||
485 | const IRPosition::CallBaseContext *CBContext = nullptr) { | ||||
486 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] Clamp return value states for " << QueryingAA << " into " << S << "\n" ; } } while (false) | ||||
487 | << QueryingAA << " into " << S << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] Clamp return value states for " << QueryingAA << " into " << S << "\n" ; } } while (false); | ||||
488 | |||||
489 | assert((QueryingAA.getIRPosition().getPositionKind() ==(static_cast <bool> ((QueryingAA.getIRPosition().getPositionKind () == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition(). getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && "Can only clamp returned value states for a function returned or call " "site returned position!") ? void (0) : __assert_fail ("(QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && \"Can only clamp returned value states for a function returned or call \" \"site returned position!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 494, __extension__ __PRETTY_FUNCTION__)) | ||||
490 | IRPosition::IRP_RETURNED ||(static_cast <bool> ((QueryingAA.getIRPosition().getPositionKind () == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition(). getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && "Can only clamp returned value states for a function returned or call " "site returned position!") ? void (0) : __assert_fail ("(QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && \"Can only clamp returned value states for a function returned or call \" \"site returned position!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 494, __extension__ __PRETTY_FUNCTION__)) | ||||
491 | QueryingAA.getIRPosition().getPositionKind() ==(static_cast <bool> ((QueryingAA.getIRPosition().getPositionKind () == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition(). getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && "Can only clamp returned value states for a function returned or call " "site returned position!") ? void (0) : __assert_fail ("(QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && \"Can only clamp returned value states for a function returned or call \" \"site returned position!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 494, __extension__ __PRETTY_FUNCTION__)) | ||||
492 | IRPosition::IRP_CALL_SITE_RETURNED) &&(static_cast <bool> ((QueryingAA.getIRPosition().getPositionKind () == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition(). getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && "Can only clamp returned value states for a function returned or call " "site returned position!") ? void (0) : __assert_fail ("(QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && \"Can only clamp returned value states for a function returned or call \" \"site returned position!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 494, __extension__ __PRETTY_FUNCTION__)) | ||||
493 | "Can only clamp returned value states for a function returned or call "(static_cast <bool> ((QueryingAA.getIRPosition().getPositionKind () == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition(). getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && "Can only clamp returned value states for a function returned or call " "site returned position!") ? void (0) : __assert_fail ("(QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && \"Can only clamp returned value states for a function returned or call \" \"site returned position!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 494, __extension__ __PRETTY_FUNCTION__)) | ||||
494 | "site returned position!")(static_cast <bool> ((QueryingAA.getIRPosition().getPositionKind () == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition(). getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && "Can only clamp returned value states for a function returned or call " "site returned position!") ? void (0) : __assert_fail ("(QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_RETURNED || QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED) && \"Can only clamp returned value states for a function returned or call \" \"site returned position!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 494, __extension__ __PRETTY_FUNCTION__)); | ||||
495 | |||||
496 | // Use an optional state as there might not be any return values and we want | ||||
497 | // to join (IntegerState::operator&) the state of all there are. | ||||
498 | Optional<StateType> T; | ||||
499 | |||||
500 | // Callback for each possibly returned value. | ||||
501 | auto CheckReturnValue = [&](Value &RV) -> bool { | ||||
502 | const IRPosition &RVPos = IRPosition::value(RV, CBContext); | ||||
503 | const AAType &AA = | ||||
504 | A.getAAFor<AAType>(QueryingAA, RVPos, DepClassTy::REQUIRED); | ||||
505 | LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() << " @ " << RVPos << "\n"; } } while (false) | ||||
506 | << " @ " << RVPos << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr() << " @ " << RVPos << "\n"; } } while (false); | ||||
507 | const StateType &AAS = AA.getState(); | ||||
508 | if (T.hasValue()) | ||||
509 | *T &= AAS; | ||||
510 | else | ||||
511 | T = AAS; | ||||
512 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << Tdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T << "\n"; } } while (false) | ||||
513 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T << "\n"; } } while (false); | ||||
514 | return T->isValidState(); | ||||
515 | }; | ||||
516 | |||||
517 | if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA)) | ||||
518 | S.indicatePessimisticFixpoint(); | ||||
519 | else if (T.hasValue()) | ||||
520 | S ^= *T; | ||||
521 | } | ||||
522 | |||||
523 | namespace { | ||||
524 | /// Helper class for generic deduction: return value -> returned position. | ||||
525 | template <typename AAType, typename BaseType, | ||||
526 | typename StateType = typename BaseType::StateType, | ||||
527 | bool PropagateCallBaseContext = false> | ||||
528 | struct AAReturnedFromReturnedValues : public BaseType { | ||||
529 | AAReturnedFromReturnedValues(const IRPosition &IRP, Attributor &A) | ||||
530 | : BaseType(IRP, A) {} | ||||
531 | |||||
532 | /// See AbstractAttribute::updateImpl(...). | ||||
533 | ChangeStatus updateImpl(Attributor &A) override { | ||||
534 | StateType S(StateType::getBestState(this->getState())); | ||||
535 | clampReturnedValueStates<AAType, StateType>( | ||||
536 | A, *this, S, | ||||
537 | PropagateCallBaseContext ? this->getCallBaseContext() : nullptr); | ||||
538 | // TODO: If we know we visited all returned values, thus no are assumed | ||||
539 | // dead, we can take the known information from the state T. | ||||
540 | return clampStateAndIndicateChange<StateType>(this->getState(), S); | ||||
541 | } | ||||
542 | }; | ||||
543 | |||||
544 | /// Clamp the information known at all call sites for a given argument | ||||
545 | /// (identified by \p QueryingAA) into \p S. | ||||
546 | template <typename AAType, typename StateType = typename AAType::StateType> | ||||
547 | static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA, | ||||
548 | StateType &S) { | ||||
549 | LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] Clamp call site argument states for " << QueryingAA << " into " << S << "\n" ; } } while (false) | ||||
550 | << QueryingAA << " into " << S << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] Clamp call site argument states for " << QueryingAA << " into " << S << "\n" ; } } while (false); | ||||
551 | |||||
552 | assert(QueryingAA.getIRPosition().getPositionKind() ==(static_cast <bool> (QueryingAA.getIRPosition().getPositionKind () == IRPosition::IRP_ARGUMENT && "Can only clamp call site argument states for an argument position!" ) ? void (0) : __assert_fail ("QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_ARGUMENT && \"Can only clamp call site argument states for an argument position!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 554, __extension__ __PRETTY_FUNCTION__)) | ||||
553 | IRPosition::IRP_ARGUMENT &&(static_cast <bool> (QueryingAA.getIRPosition().getPositionKind () == IRPosition::IRP_ARGUMENT && "Can only clamp call site argument states for an argument position!" ) ? void (0) : __assert_fail ("QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_ARGUMENT && \"Can only clamp call site argument states for an argument position!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 554, __extension__ __PRETTY_FUNCTION__)) | ||||
554 | "Can only clamp call site argument states for an argument position!")(static_cast <bool> (QueryingAA.getIRPosition().getPositionKind () == IRPosition::IRP_ARGUMENT && "Can only clamp call site argument states for an argument position!" ) ? void (0) : __assert_fail ("QueryingAA.getIRPosition().getPositionKind() == IRPosition::IRP_ARGUMENT && \"Can only clamp call site argument states for an argument position!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 554, __extension__ __PRETTY_FUNCTION__)); | ||||
555 | |||||
556 | // Use an optional state as there might not be any return values and we want | ||||
557 | // to join (IntegerState::operator&) the state of all there are. | ||||
558 | Optional<StateType> T; | ||||
559 | |||||
560 | // The argument number which is also the call site argument number. | ||||
561 | unsigned ArgNo = QueryingAA.getIRPosition().getCallSiteArgNo(); | ||||
562 | |||||
563 | auto CallSiteCheck = [&](AbstractCallSite ACS) { | ||||
564 | const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); | ||||
565 | // Check if a coresponding argument was found or if it is on not associated | ||||
566 | // (which can happen for callback calls). | ||||
567 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) | ||||
568 | return false; | ||||
569 | |||||
570 | const AAType &AA = | ||||
571 | A.getAAFor<AAType>(QueryingAA, ACSArgPos, DepClassTy::REQUIRED); | ||||
572 | LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] ACS: " << *ACS.getInstruction() << " AA: " << AA.getAsStr( ) << " @" << ACSArgPos << "\n"; } } while ( false) | ||||
573 | << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] ACS: " << *ACS.getInstruction() << " AA: " << AA.getAsStr( ) << " @" << ACSArgPos << "\n"; } } while ( false); | ||||
574 | const StateType &AAS = AA.getState(); | ||||
575 | if (T.hasValue()) | ||||
576 | *T &= AAS; | ||||
577 | else | ||||
578 | T = AAS; | ||||
579 | LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << Tdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T << "\n"; } } while (false) | ||||
580 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T << "\n"; } } while (false); | ||||
581 | return T->isValidState(); | ||||
582 | }; | ||||
583 | |||||
584 | bool UsedAssumedInformation = false; | ||||
585 | if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true, | ||||
586 | UsedAssumedInformation)) | ||||
587 | S.indicatePessimisticFixpoint(); | ||||
588 | else if (T.hasValue()) | ||||
589 | S ^= *T; | ||||
590 | } | ||||
591 | |||||
592 | /// This function is the bridge between argument position and the call base | ||||
593 | /// context. | ||||
594 | template <typename AAType, typename BaseType, | ||||
595 | typename StateType = typename AAType::StateType> | ||||
596 | bool getArgumentStateFromCallBaseContext(Attributor &A, | ||||
597 | BaseType &QueryingAttribute, | ||||
598 | IRPosition &Pos, StateType &State) { | ||||
599 | assert((Pos.getPositionKind() == IRPosition::IRP_ARGUMENT) &&(static_cast <bool> ((Pos.getPositionKind() == IRPosition ::IRP_ARGUMENT) && "Expected an 'argument' position !" ) ? void (0) : __assert_fail ("(Pos.getPositionKind() == IRPosition::IRP_ARGUMENT) && \"Expected an 'argument' position !\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 600, __extension__ __PRETTY_FUNCTION__)) | ||||
600 | "Expected an 'argument' position !")(static_cast <bool> ((Pos.getPositionKind() == IRPosition ::IRP_ARGUMENT) && "Expected an 'argument' position !" ) ? void (0) : __assert_fail ("(Pos.getPositionKind() == IRPosition::IRP_ARGUMENT) && \"Expected an 'argument' position !\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 600, __extension__ __PRETTY_FUNCTION__)); | ||||
601 | const CallBase *CBContext = Pos.getCallBaseContext(); | ||||
602 | if (!CBContext) | ||||
603 | return false; | ||||
604 | |||||
605 | int ArgNo = Pos.getCallSiteArgNo(); | ||||
606 | assert(ArgNo >= 0 && "Invalid Arg No!")(static_cast <bool> (ArgNo >= 0 && "Invalid Arg No!" ) ? void (0) : __assert_fail ("ArgNo >= 0 && \"Invalid Arg No!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 606, __extension__ __PRETTY_FUNCTION__)); | ||||
607 | |||||
608 | const auto &AA = A.getAAFor<AAType>( | ||||
609 | QueryingAttribute, IRPosition::callsite_argument(*CBContext, ArgNo), | ||||
610 | DepClassTy::REQUIRED); | ||||
611 | const StateType &CBArgumentState = | ||||
612 | static_cast<const StateType &>(AA.getState()); | ||||
613 | |||||
614 | LLVM_DEBUG(dbgs() << "[Attributor] Briding Call site context to argument"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] Briding Call site context to argument" << "Position:" << Pos << "CB Arg state:" << CBArgumentState << "\n"; } } while (false) | ||||
615 | << "Position:" << Pos << "CB Arg state:" << CBArgumentStatedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] Briding Call site context to argument" << "Position:" << Pos << "CB Arg state:" << CBArgumentState << "\n"; } } while (false) | ||||
616 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] Briding Call site context to argument" << "Position:" << Pos << "CB Arg state:" << CBArgumentState << "\n"; } } while (false); | ||||
617 | |||||
618 | // NOTE: If we want to do call site grouping it should happen here. | ||||
619 | State ^= CBArgumentState; | ||||
620 | return true; | ||||
621 | } | ||||
622 | |||||
623 | /// Helper class for generic deduction: call site argument -> argument position. | ||||
624 | template <typename AAType, typename BaseType, | ||||
625 | typename StateType = typename AAType::StateType, | ||||
626 | bool BridgeCallBaseContext = false> | ||||
627 | struct AAArgumentFromCallSiteArguments : public BaseType { | ||||
628 | AAArgumentFromCallSiteArguments(const IRPosition &IRP, Attributor &A) | ||||
629 | : BaseType(IRP, A) {} | ||||
630 | |||||
631 | /// See AbstractAttribute::updateImpl(...). | ||||
632 | ChangeStatus updateImpl(Attributor &A) override { | ||||
633 | StateType S = StateType::getBestState(this->getState()); | ||||
634 | |||||
635 | if (BridgeCallBaseContext) { | ||||
636 | bool Success = | ||||
637 | getArgumentStateFromCallBaseContext<AAType, BaseType, StateType>( | ||||
638 | A, *this, this->getIRPosition(), S); | ||||
639 | if (Success) | ||||
640 | return clampStateAndIndicateChange<StateType>(this->getState(), S); | ||||
641 | } | ||||
642 | clampCallSiteArgumentStates<AAType, StateType>(A, *this, S); | ||||
643 | |||||
644 | // TODO: If we know we visited all incoming values, thus no are assumed | ||||
645 | // dead, we can take the known information from the state T. | ||||
646 | return clampStateAndIndicateChange<StateType>(this->getState(), S); | ||||
647 | } | ||||
648 | }; | ||||
649 | |||||
650 | /// Helper class for generic replication: function returned -> cs returned. | ||||
651 | template <typename AAType, typename BaseType, | ||||
652 | typename StateType = typename BaseType::StateType, | ||||
653 | bool IntroduceCallBaseContext = false> | ||||
654 | struct AACallSiteReturnedFromReturned : public BaseType { | ||||
655 | AACallSiteReturnedFromReturned(const IRPosition &IRP, Attributor &A) | ||||
656 | : BaseType(IRP, A) {} | ||||
657 | |||||
658 | /// See AbstractAttribute::updateImpl(...). | ||||
659 | ChangeStatus updateImpl(Attributor &A) override { | ||||
660 | assert(this->getIRPosition().getPositionKind() ==(static_cast <bool> (this->getIRPosition().getPositionKind () == IRPosition::IRP_CALL_SITE_RETURNED && "Can only wrap function returned positions for call site returned " "positions!") ? void (0) : __assert_fail ("this->getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED && \"Can only wrap function returned positions for call site returned \" \"positions!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 663, __extension__ __PRETTY_FUNCTION__)) | ||||
661 | IRPosition::IRP_CALL_SITE_RETURNED &&(static_cast <bool> (this->getIRPosition().getPositionKind () == IRPosition::IRP_CALL_SITE_RETURNED && "Can only wrap function returned positions for call site returned " "positions!") ? void (0) : __assert_fail ("this->getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED && \"Can only wrap function returned positions for call site returned \" \"positions!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 663, __extension__ __PRETTY_FUNCTION__)) | ||||
662 | "Can only wrap function returned positions for call site returned "(static_cast <bool> (this->getIRPosition().getPositionKind () == IRPosition::IRP_CALL_SITE_RETURNED && "Can only wrap function returned positions for call site returned " "positions!") ? void (0) : __assert_fail ("this->getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED && \"Can only wrap function returned positions for call site returned \" \"positions!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 663, __extension__ __PRETTY_FUNCTION__)) | ||||
663 | "positions!")(static_cast <bool> (this->getIRPosition().getPositionKind () == IRPosition::IRP_CALL_SITE_RETURNED && "Can only wrap function returned positions for call site returned " "positions!") ? void (0) : __assert_fail ("this->getIRPosition().getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED && \"Can only wrap function returned positions for call site returned \" \"positions!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 663, __extension__ __PRETTY_FUNCTION__)); | ||||
664 | auto &S = this->getState(); | ||||
665 | |||||
666 | const Function *AssociatedFunction = | ||||
667 | this->getIRPosition().getAssociatedFunction(); | ||||
668 | if (!AssociatedFunction) | ||||
669 | return S.indicatePessimisticFixpoint(); | ||||
670 | |||||
671 | CallBase &CBContext = cast<CallBase>(this->getAnchorValue()); | ||||
672 | if (IntroduceCallBaseContext) | ||||
673 | LLVM_DEBUG(dbgs() << "[Attributor] Introducing call base context:"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] Introducing call base context:" << CBContext << "\n"; } } while (false) | ||||
674 | << CBContext << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[Attributor] Introducing call base context:" << CBContext << "\n"; } } while (false); | ||||
675 | |||||
676 | IRPosition FnPos = IRPosition::returned( | ||||
677 | *AssociatedFunction, IntroduceCallBaseContext ? &CBContext : nullptr); | ||||
678 | const AAType &AA = A.getAAFor<AAType>(*this, FnPos, DepClassTy::REQUIRED); | ||||
679 | return clampStateAndIndicateChange(S, AA.getState()); | ||||
680 | } | ||||
681 | }; | ||||
682 | } // namespace | ||||
683 | |||||
684 | /// Helper function to accumulate uses. | ||||
685 | template <class AAType, typename StateType = typename AAType::StateType> | ||||
686 | static void followUsesInContext(AAType &AA, Attributor &A, | ||||
687 | MustBeExecutedContextExplorer &Explorer, | ||||
688 | const Instruction *CtxI, | ||||
689 | SetVector<const Use *> &Uses, | ||||
690 | StateType &State) { | ||||
691 | auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI); | ||||
692 | for (unsigned u = 0; u < Uses.size(); ++u) { | ||||
693 | const Use *U = Uses[u]; | ||||
694 | if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) { | ||||
695 | bool Found = Explorer.findInContextOf(UserI, EIt, EEnd); | ||||
696 | if (Found && AA.followUseInMBEC(A, U, UserI, State)) | ||||
697 | for (const Use &Us : UserI->uses()) | ||||
698 | Uses.insert(&Us); | ||||
699 | } | ||||
700 | } | ||||
701 | } | ||||
702 | |||||
703 | /// Use the must-be-executed-context around \p I to add information into \p S. | ||||
704 | /// The AAType class is required to have `followUseInMBEC` method with the | ||||
705 | /// following signature and behaviour: | ||||
706 | /// | ||||
707 | /// bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I) | ||||
708 | /// U - Underlying use. | ||||
709 | /// I - The user of the \p U. | ||||
710 | /// Returns true if the value should be tracked transitively. | ||||
711 | /// | ||||
712 | template <class AAType, typename StateType = typename AAType::StateType> | ||||
713 | static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S, | ||||
714 | Instruction &CtxI) { | ||||
715 | |||||
716 | // Container for (transitive) uses of the associated value. | ||||
717 | SetVector<const Use *> Uses; | ||||
718 | for (const Use &U : AA.getIRPosition().getAssociatedValue().uses()) | ||||
719 | Uses.insert(&U); | ||||
720 | |||||
721 | MustBeExecutedContextExplorer &Explorer = | ||||
722 | A.getInfoCache().getMustBeExecutedContextExplorer(); | ||||
723 | |||||
724 | followUsesInContext<AAType>(AA, A, Explorer, &CtxI, Uses, S); | ||||
725 | |||||
726 | if (S.isAtFixpoint()) | ||||
727 | return; | ||||
728 | |||||
729 | SmallVector<const BranchInst *, 4> BrInsts; | ||||
730 | auto Pred = [&](const Instruction *I) { | ||||
731 | if (const BranchInst *Br = dyn_cast<BranchInst>(I)) | ||||
732 | if (Br->isConditional()) | ||||
733 | BrInsts.push_back(Br); | ||||
734 | return true; | ||||
735 | }; | ||||
736 | |||||
737 | // Here, accumulate conditional branch instructions in the context. We | ||||
738 | // explore the child paths and collect the known states. The disjunction of | ||||
739 | // those states can be merged to its own state. Let ParentState_i be a state | ||||
740 | // to indicate the known information for an i-th branch instruction in the | ||||
741 | // context. ChildStates are created for its successors respectively. | ||||
742 | // | ||||
743 | // ParentS_1 = ChildS_{1, 1} /\ ChildS_{1, 2} /\ ... /\ ChildS_{1, n_1} | ||||
744 | // ParentS_2 = ChildS_{2, 1} /\ ChildS_{2, 2} /\ ... /\ ChildS_{2, n_2} | ||||
745 | // ... | ||||
746 | // ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m} | ||||
747 | // | ||||
748 | // Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m | ||||
749 | // | ||||
750 | // FIXME: Currently, recursive branches are not handled. For example, we | ||||
751 | // can't deduce that ptr must be dereferenced in below function. | ||||
752 | // | ||||
753 | // void f(int a, int c, int *ptr) { | ||||
754 | // if(a) | ||||
755 | // if (b) { | ||||
756 | // *ptr = 0; | ||||
757 | // } else { | ||||
758 | // *ptr = 1; | ||||
759 | // } | ||||
760 | // else { | ||||
761 | // if (b) { | ||||
762 | // *ptr = 0; | ||||
763 | // } else { | ||||
764 | // *ptr = 1; | ||||
765 | // } | ||||
766 | // } | ||||
767 | // } | ||||
768 | |||||
769 | Explorer.checkForAllContext(&CtxI, Pred); | ||||
770 | for (const BranchInst *Br : BrInsts) { | ||||
771 | StateType ParentState; | ||||
772 | |||||
773 | // The known state of the parent state is a conjunction of children's | ||||
774 | // known states so it is initialized with a best state. | ||||
775 | ParentState.indicateOptimisticFixpoint(); | ||||
776 | |||||
777 | for (const BasicBlock *BB : Br->successors()) { | ||||
778 | StateType ChildState; | ||||
779 | |||||
780 | size_t BeforeSize = Uses.size(); | ||||
781 | followUsesInContext(AA, A, Explorer, &BB->front(), Uses, ChildState); | ||||
782 | |||||
783 | // Erase uses which only appear in the child. | ||||
784 | for (auto It = Uses.begin() + BeforeSize; It != Uses.end();) | ||||
785 | It = Uses.erase(It); | ||||
786 | |||||
787 | ParentState &= ChildState; | ||||
788 | } | ||||
789 | |||||
790 | // Use only known state. | ||||
791 | S += ParentState; | ||||
792 | } | ||||
793 | } | ||||
794 | |||||
795 | /// ------------------------ PointerInfo --------------------------------------- | ||||
796 | |||||
797 | namespace llvm { | ||||
798 | namespace AA { | ||||
799 | namespace PointerInfo { | ||||
800 | |||||
801 | struct State; | ||||
802 | |||||
803 | } // namespace PointerInfo | ||||
804 | } // namespace AA | ||||
805 | |||||
806 | /// Helper for AA::PointerInfo::Acccess DenseMap/Set usage. | ||||
807 | template <> | ||||
808 | struct DenseMapInfo<AAPointerInfo::Access> : DenseMapInfo<Instruction *> { | ||||
809 | using Access = AAPointerInfo::Access; | ||||
810 | static inline Access getEmptyKey(); | ||||
811 | static inline Access getTombstoneKey(); | ||||
812 | static unsigned getHashValue(const Access &A); | ||||
813 | static bool isEqual(const Access &LHS, const Access &RHS); | ||||
814 | }; | ||||
815 | |||||
816 | /// Helper that allows OffsetAndSize as a key in a DenseMap. | ||||
817 | template <> | ||||
818 | struct DenseMapInfo<AAPointerInfo ::OffsetAndSize> | ||||
819 | : DenseMapInfo<std::pair<int64_t, int64_t>> {}; | ||||
820 | |||||
821 | /// Helper for AA::PointerInfo::Acccess DenseMap/Set usage ignoring everythign | ||||
822 | /// but the instruction | ||||
823 | struct AccessAsInstructionInfo : DenseMapInfo<Instruction *> { | ||||
824 | using Base = DenseMapInfo<Instruction *>; | ||||
825 | using Access = AAPointerInfo::Access; | ||||
826 | static inline Access getEmptyKey(); | ||||
827 | static inline Access getTombstoneKey(); | ||||
828 | static unsigned getHashValue(const Access &A); | ||||
829 | static bool isEqual(const Access &LHS, const Access &RHS); | ||||
830 | }; | ||||
831 | |||||
832 | } // namespace llvm | ||||
833 | |||||
834 | /// Implementation of the DenseMapInfo. | ||||
835 | /// | ||||
836 | ///{ | ||||
837 | inline llvm::AccessAsInstructionInfo::Access | ||||
838 | llvm::AccessAsInstructionInfo::getEmptyKey() { | ||||
839 | return Access(Base::getEmptyKey(), nullptr, AAPointerInfo::AK_READ, nullptr); | ||||
840 | } | ||||
841 | inline llvm::AccessAsInstructionInfo::Access | ||||
842 | llvm::AccessAsInstructionInfo::getTombstoneKey() { | ||||
843 | return Access(Base::getTombstoneKey(), nullptr, AAPointerInfo::AK_READ, | ||||
844 | nullptr); | ||||
845 | } | ||||
846 | unsigned llvm::AccessAsInstructionInfo::getHashValue( | ||||
847 | const llvm::AccessAsInstructionInfo::Access &A) { | ||||
848 | return Base::getHashValue(A.getRemoteInst()); | ||||
849 | } | ||||
850 | bool llvm::AccessAsInstructionInfo::isEqual( | ||||
851 | const llvm::AccessAsInstructionInfo::Access &LHS, | ||||
852 | const llvm::AccessAsInstructionInfo::Access &RHS) { | ||||
853 | return LHS.getRemoteInst() == RHS.getRemoteInst(); | ||||
854 | } | ||||
855 | inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access | ||||
856 | llvm::DenseMapInfo<AAPointerInfo::Access>::getEmptyKey() { | ||||
857 | return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_READ, | ||||
858 | nullptr); | ||||
859 | } | ||||
860 | inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access | ||||
861 | llvm::DenseMapInfo<AAPointerInfo::Access>::getTombstoneKey() { | ||||
862 | return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_WRITE, | ||||
863 | nullptr); | ||||
864 | } | ||||
865 | |||||
866 | unsigned llvm::DenseMapInfo<AAPointerInfo::Access>::getHashValue( | ||||
867 | const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &A) { | ||||
868 | return detail::combineHashValue( | ||||
869 | DenseMapInfo<Instruction *>::getHashValue(A.getRemoteInst()), | ||||
870 | (A.isWrittenValueYetUndetermined() | ||||
871 | ? ~0 | ||||
872 | : DenseMapInfo<Value *>::getHashValue(A.getWrittenValue()))) + | ||||
873 | A.getKind(); | ||||
874 | } | ||||
875 | |||||
876 | bool llvm::DenseMapInfo<AAPointerInfo::Access>::isEqual( | ||||
877 | const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &LHS, | ||||
878 | const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &RHS) { | ||||
879 | return LHS == RHS; | ||||
880 | } | ||||
881 | ///} | ||||
882 | |||||
883 | /// A type to track pointer/struct usage and accesses for AAPointerInfo. | ||||
884 | struct AA::PointerInfo::State : public AbstractState { | ||||
885 | |||||
886 | /// Return the best possible representable state. | ||||
887 | static State getBestState(const State &SIS) { return State(); } | ||||
888 | |||||
889 | /// Return the worst possible representable state. | ||||
890 | static State getWorstState(const State &SIS) { | ||||
891 | State R; | ||||
892 | R.indicatePessimisticFixpoint(); | ||||
893 | return R; | ||||
894 | } | ||||
895 | |||||
896 | State() = default; | ||||
897 | State(const State &SIS) : AccessBins(SIS.AccessBins) {} | ||||
898 | State(State &&SIS) : AccessBins(std::move(SIS.AccessBins)) {} | ||||
899 | |||||
900 | const State &getAssumed() const { return *this; } | ||||
901 | |||||
902 | /// See AbstractState::isValidState(). | ||||
903 | bool isValidState() const override { return BS.isValidState(); } | ||||
904 | |||||
905 | /// See AbstractState::isAtFixpoint(). | ||||
906 | bool isAtFixpoint() const override { return BS.isAtFixpoint(); } | ||||
907 | |||||
908 | /// See AbstractState::indicateOptimisticFixpoint(). | ||||
909 | ChangeStatus indicateOptimisticFixpoint() override { | ||||
910 | BS.indicateOptimisticFixpoint(); | ||||
911 | return ChangeStatus::UNCHANGED; | ||||
912 | } | ||||
913 | |||||
914 | /// See AbstractState::indicatePessimisticFixpoint(). | ||||
915 | ChangeStatus indicatePessimisticFixpoint() override { | ||||
916 | BS.indicatePessimisticFixpoint(); | ||||
917 | return ChangeStatus::CHANGED; | ||||
918 | } | ||||
919 | |||||
920 | State &operator=(const State &R) { | ||||
921 | if (this == &R) | ||||
922 | return *this; | ||||
923 | BS = R.BS; | ||||
924 | AccessBins = R.AccessBins; | ||||
925 | return *this; | ||||
926 | } | ||||
927 | |||||
928 | State &operator=(State &&R) { | ||||
929 | if (this == &R) | ||||
930 | return *this; | ||||
931 | std::swap(BS, R.BS); | ||||
932 | std::swap(AccessBins, R.AccessBins); | ||||
933 | return *this; | ||||
934 | } | ||||
935 | |||||
936 | bool operator==(const State &R) const { | ||||
937 | if (BS != R.BS) | ||||
938 | return false; | ||||
939 | if (AccessBins.size() != R.AccessBins.size()) | ||||
940 | return false; | ||||
941 | auto It = begin(), RIt = R.begin(), E = end(); | ||||
942 | while (It != E) { | ||||
943 | if (It->getFirst() != RIt->getFirst()) | ||||
944 | return false; | ||||
945 | auto &Accs = It->getSecond(); | ||||
946 | auto &RAccs = RIt->getSecond(); | ||||
947 | if (Accs.size() != RAccs.size()) | ||||
948 | return false; | ||||
949 | auto AccIt = Accs.begin(), RAccIt = RAccs.begin(), AccE = Accs.end(); | ||||
950 | while (AccIt != AccE) { | ||||
951 | if (*AccIt != *RAccIt) | ||||
952 | return false; | ||||
953 | ++AccIt; | ||||
954 | ++RAccIt; | ||||
955 | } | ||||
956 | ++It; | ||||
957 | ++RIt; | ||||
958 | } | ||||
959 | return true; | ||||
960 | } | ||||
961 | bool operator!=(const State &R) const { return !(*this == R); } | ||||
962 | |||||
963 | /// We store accesses in a set with the instruction as key. | ||||
964 | using Accesses = DenseSet<AAPointerInfo::Access, AccessAsInstructionInfo>; | ||||
965 | |||||
966 | /// We store all accesses in bins denoted by their offset and size. | ||||
967 | using AccessBinsTy = DenseMap<AAPointerInfo::OffsetAndSize, Accesses>; | ||||
968 | |||||
969 | AccessBinsTy::const_iterator begin() const { return AccessBins.begin(); } | ||||
970 | AccessBinsTy::const_iterator end() const { return AccessBins.end(); } | ||||
971 | |||||
972 | protected: | ||||
973 | /// The bins with all the accesses for the associated pointer. | ||||
974 | DenseMap<AAPointerInfo::OffsetAndSize, Accesses> AccessBins; | ||||
975 | |||||
976 | /// Add a new access to the state at offset \p Offset and with size \p Size. | ||||
977 | /// The access is associated with \p I, writes \p Content (if anything), and | ||||
978 | /// is of kind \p Kind. | ||||
979 | /// \Returns CHANGED, if the state changed, UNCHANGED otherwise. | ||||
980 | ChangeStatus addAccess(int64_t Offset, int64_t Size, Instruction &I, | ||||
981 | Optional<Value *> Content, | ||||
982 | AAPointerInfo::AccessKind Kind, Type *Ty, | ||||
983 | Instruction *RemoteI = nullptr, | ||||
984 | Accesses *BinPtr = nullptr) { | ||||
985 | AAPointerInfo::OffsetAndSize Key{Offset, Size}; | ||||
986 | Accesses &Bin = BinPtr ? *BinPtr : AccessBins[Key]; | ||||
987 | AAPointerInfo::Access Acc(&I, RemoteI ? RemoteI : &I, Content, Kind, Ty); | ||||
988 | // Check if we have an access for this instruction in this bin, if not, | ||||
989 | // simply add it. | ||||
990 | auto It = Bin.find(Acc); | ||||
991 | if (It == Bin.end()) { | ||||
992 | Bin.insert(Acc); | ||||
993 | return ChangeStatus::CHANGED; | ||||
994 | } | ||||
995 | // If the existing access is the same as then new one, nothing changed. | ||||
996 | AAPointerInfo::Access Before = *It; | ||||
997 | // The new one will be combined with the existing one. | ||||
998 | *It &= Acc; | ||||
999 | return *It == Before ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED; | ||||
1000 | } | ||||
1001 | |||||
1002 | /// See AAPointerInfo::forallInterferingAccesses. | ||||
1003 | bool forallInterferingAccesses( | ||||
1004 | AAPointerInfo::OffsetAndSize OAS, | ||||
1005 | function_ref<bool(const AAPointerInfo::Access &, bool)> CB) const { | ||||
1006 | if (!isValidState()) | ||||
1007 | return false; | ||||
1008 | |||||
1009 | for (auto &It : AccessBins) { | ||||
1010 | AAPointerInfo::OffsetAndSize ItOAS = It.getFirst(); | ||||
1011 | if (!OAS.mayOverlap(ItOAS)) | ||||
1012 | continue; | ||||
1013 | bool IsExact = OAS == ItOAS && !OAS.offsetOrSizeAreUnknown(); | ||||
1014 | for (auto &Access : It.getSecond()) | ||||
1015 | if (!CB(Access, IsExact)) | ||||
1016 | return false; | ||||
1017 | } | ||||
1018 | return true; | ||||
1019 | } | ||||
1020 | |||||
1021 | /// See AAPointerInfo::forallInterferingAccesses. | ||||
1022 | bool forallInterferingAccesses( | ||||
1023 | Instruction &I, | ||||
1024 | function_ref<bool(const AAPointerInfo::Access &, bool)> CB) const { | ||||
1025 | if (!isValidState()) | ||||
1026 | return false; | ||||
1027 | |||||
1028 | // First find the offset and size of I. | ||||
1029 | AAPointerInfo::OffsetAndSize OAS(-1, -1); | ||||
1030 | for (auto &It : AccessBins) { | ||||
1031 | for (auto &Access : It.getSecond()) { | ||||
1032 | if (Access.getRemoteInst() == &I) { | ||||
1033 | OAS = It.getFirst(); | ||||
1034 | break; | ||||
1035 | } | ||||
1036 | } | ||||
1037 | if (OAS.getSize() != -1) | ||||
1038 | break; | ||||
1039 | } | ||||
1040 | // No access for I was found, we are done. | ||||
1041 | if (OAS.getSize() == -1) | ||||
1042 | return true; | ||||
1043 | |||||
1044 | // Now that we have an offset and size, find all overlapping ones and use | ||||
1045 | // the callback on the accesses. | ||||
1046 | return forallInterferingAccesses(OAS, CB); | ||||
1047 | } | ||||
1048 | |||||
1049 | private: | ||||
1050 | /// State to track fixpoint and validity. | ||||
1051 | BooleanState BS; | ||||
1052 | }; | ||||
1053 | |||||
1054 | struct AAPointerInfoImpl | ||||
1055 | : public StateWrapper<AA::PointerInfo::State, AAPointerInfo> { | ||||
1056 | using BaseTy = StateWrapper<AA::PointerInfo::State, AAPointerInfo>; | ||||
1057 | AAPointerInfoImpl(const IRPosition &IRP, Attributor &A) : BaseTy(IRP) {} | ||||
1058 | |||||
1059 | /// See AbstractAttribute::initialize(...). | ||||
1060 | void initialize(Attributor &A) override { AAPointerInfo::initialize(A); } | ||||
1061 | |||||
1062 | /// See AbstractAttribute::getAsStr(). | ||||
1063 | const std::string getAsStr() const override { | ||||
1064 | return std::string("PointerInfo ") + | ||||
1065 | (isValidState() ? (std::string("#") + | ||||
1066 | std::to_string(AccessBins.size()) + " bins") | ||||
1067 | : "<invalid>"); | ||||
1068 | } | ||||
1069 | |||||
1070 | /// See AbstractAttribute::manifest(...). | ||||
1071 | ChangeStatus manifest(Attributor &A) override { | ||||
1072 | return AAPointerInfo::manifest(A); | ||||
1073 | } | ||||
1074 | |||||
1075 | bool forallInterferingAccesses( | ||||
1076 | OffsetAndSize OAS, | ||||
1077 | function_ref<bool(const AAPointerInfo::Access &, bool)> CB) | ||||
1078 | const override { | ||||
1079 | return State::forallInterferingAccesses(OAS, CB); | ||||
1080 | } | ||||
1081 | bool forallInterferingAccesses( | ||||
1082 | LoadInst &LI, function_ref<bool(const AAPointerInfo::Access &, bool)> CB) | ||||
1083 | const override { | ||||
1084 | return State::forallInterferingAccesses(LI, CB); | ||||
1085 | } | ||||
1086 | bool forallInterferingAccesses( | ||||
1087 | StoreInst &SI, function_ref<bool(const AAPointerInfo::Access &, bool)> CB) | ||||
1088 | const override { | ||||
1089 | return State::forallInterferingAccesses(SI, CB); | ||||
1090 | } | ||||
1091 | bool forallInterferingWrites( | ||||
1092 | Attributor &A, const AbstractAttribute &QueryingAA, LoadInst &LI, | ||||
1093 | function_ref<bool(const Access &, bool)> UserCB) const override { | ||||
1094 | SmallPtrSet<const Access *, 8> DominatingWrites; | ||||
1095 | SmallVector<std::pair<const Access *, bool>, 8> InterferingWrites; | ||||
1096 | |||||
1097 | Function &Scope = *LI.getFunction(); | ||||
1098 | const auto &NoSyncAA = A.getAAFor<AANoSync>( | ||||
1099 | QueryingAA, IRPosition::function(Scope), DepClassTy::OPTIONAL); | ||||
1100 | const auto *ExecDomainAA = A.lookupAAFor<AAExecutionDomain>( | ||||
1101 | IRPosition::function(Scope), &QueryingAA, DepClassTy::OPTIONAL); | ||||
1102 | const bool NoSync = NoSyncAA.isAssumedNoSync(); | ||||
1103 | |||||
1104 | // Helper to determine if we need to consider threading, which we cannot | ||||
1105 | // right now. However, if the function is (assumed) nosync or the thread | ||||
1106 | // executing all instructions is the main thread only we can ignore | ||||
1107 | // threading. | ||||
1108 | auto CanIgnoreThreading = [&](const Instruction &I) -> bool { | ||||
1109 | if (NoSync) | ||||
1110 | return true; | ||||
1111 | if (ExecDomainAA && ExecDomainAA->isExecutedByInitialThreadOnly(I)) | ||||
1112 | return true; | ||||
1113 | return false; | ||||
1114 | }; | ||||
1115 | |||||
1116 | // Helper to determine if the access is executed by the same thread as the | ||||
1117 | // load, for now it is sufficient to avoid any potential threading effects | ||||
1118 | // as we cannot deal with them anyway. | ||||
1119 | auto IsSameThreadAsLoad = [&](const Access &Acc) -> bool { | ||||
1120 | return CanIgnoreThreading(*Acc.getLocalInst()); | ||||
1121 | }; | ||||
1122 | |||||
1123 | // TODO: Use inter-procedural reachability and dominance. | ||||
1124 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>( | ||||
1125 | QueryingAA, IRPosition::function(*LI.getFunction()), | ||||
1126 | DepClassTy::OPTIONAL); | ||||
1127 | |||||
1128 | const bool CanUseCFGResoning = CanIgnoreThreading(LI); | ||||
1129 | InformationCache &InfoCache = A.getInfoCache(); | ||||
1130 | const DominatorTree *DT = | ||||
1131 | NoRecurseAA.isKnownNoRecurse() | ||||
1132 | ? InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>( | ||||
1133 | Scope) | ||||
1134 | : nullptr; | ||||
1135 | |||||
1136 | enum GPUAddressSpace : unsigned { | ||||
1137 | Generic = 0, | ||||
1138 | Global = 1, | ||||
1139 | Shared = 3, | ||||
1140 | Constant = 4, | ||||
1141 | Local = 5, | ||||
1142 | }; | ||||
1143 | |||||
1144 | // Helper to check if a value has "kernel lifetime", that is it will not | ||||
1145 | // outlive a GPU kernel. This is true for shared, constant, and local | ||||
1146 | // globals on AMD and NVIDIA GPUs. | ||||
1147 | auto HasKernelLifetime = [&](Value *V, Module &M) { | ||||
1148 | Triple T(M.getTargetTriple()); | ||||
1149 | if (!(T.isAMDGPU() || T.isNVPTX())) | ||||
1150 | return false; | ||||
1151 | switch (V->getType()->getPointerAddressSpace()) { | ||||
1152 | case GPUAddressSpace::Shared: | ||||
1153 | case GPUAddressSpace::Constant: | ||||
1154 | case GPUAddressSpace::Local: | ||||
1155 | return true; | ||||
1156 | default: | ||||
1157 | return false; | ||||
1158 | }; | ||||
1159 | }; | ||||
1160 | |||||
1161 | // The IsLiveInCalleeCB will be used by the AA::isPotentiallyReachable query | ||||
1162 | // to determine if we should look at reachability from the callee. For | ||||
1163 | // certain pointers we know the lifetime and we do not have to step into the | ||||
1164 | // callee to determine reachability as the pointer would be dead in the | ||||
1165 | // callee. See the conditional initialization below. | ||||
1166 | std::function<bool(const Function &)> IsLiveInCalleeCB; | ||||
1167 | |||||
1168 | if (auto *AI = dyn_cast<AllocaInst>(&getAssociatedValue())) { | ||||
1169 | // If the alloca containing function is not recursive the alloca | ||||
1170 | // must be dead in the callee. | ||||
1171 | const Function *AIFn = AI->getFunction(); | ||||
1172 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>( | ||||
1173 | *this, IRPosition::function(*AIFn), DepClassTy::OPTIONAL); | ||||
1174 | if (NoRecurseAA.isAssumedNoRecurse()) { | ||||
1175 | IsLiveInCalleeCB = [AIFn](const Function &Fn) { return AIFn != &Fn; }; | ||||
1176 | } | ||||
1177 | } else if (auto *GV = dyn_cast<GlobalValue>(&getAssociatedValue())) { | ||||
1178 | // If the global has kernel lifetime we can stop if we reach a kernel | ||||
1179 | // as it is "dead" in the (unknown) callees. | ||||
1180 | if (HasKernelLifetime(GV, *GV->getParent())) | ||||
1181 | IsLiveInCalleeCB = [](const Function &Fn) { | ||||
1182 | return !Fn.hasFnAttribute("kernel"); | ||||
1183 | }; | ||||
1184 | } | ||||
1185 | |||||
1186 | auto AccessCB = [&](const Access &Acc, bool Exact) { | ||||
1187 | if (!Acc.isWrite()) | ||||
1188 | return true; | ||||
1189 | |||||
1190 | // For now we only filter accesses based on CFG reasoning which does not | ||||
1191 | // work yet if we have threading effects, or the access is complicated. | ||||
1192 | if (CanUseCFGResoning) { | ||||
1193 | if (!AA::isPotentiallyReachable(A, *Acc.getLocalInst(), LI, QueryingAA, | ||||
1194 | IsLiveInCalleeCB)) | ||||
1195 | return true; | ||||
1196 | if (DT && Exact && | ||||
1197 | (Acc.getLocalInst()->getFunction() == LI.getFunction()) && | ||||
1198 | IsSameThreadAsLoad(Acc)) { | ||||
1199 | if (DT->dominates(Acc.getLocalInst(), &LI)) | ||||
1200 | DominatingWrites.insert(&Acc); | ||||
1201 | } | ||||
1202 | } | ||||
1203 | |||||
1204 | InterferingWrites.push_back({&Acc, Exact}); | ||||
1205 | return true; | ||||
1206 | }; | ||||
1207 | if (!State::forallInterferingAccesses(LI, AccessCB)) | ||||
1208 | return false; | ||||
1209 | |||||
1210 | // If we cannot use CFG reasoning we only filter the non-write accesses | ||||
1211 | // and are done here. | ||||
1212 | if (!CanUseCFGResoning) { | ||||
1213 | for (auto &It : InterferingWrites) | ||||
1214 | if (!UserCB(*It.first, It.second)) | ||||
1215 | return false; | ||||
1216 | return true; | ||||
1217 | } | ||||
1218 | |||||
1219 | // Helper to determine if we can skip a specific write access. This is in | ||||
1220 | // the worst case quadratic as we are looking for another write that will | ||||
1221 | // hide the effect of this one. | ||||
1222 | auto CanSkipAccess = [&](const Access &Acc, bool Exact) { | ||||
1223 | if (!IsSameThreadAsLoad(Acc)) | ||||
1224 | return false; | ||||
1225 | if (!DominatingWrites.count(&Acc)) | ||||
1226 | return false; | ||||
1227 | for (const Access *DomAcc : DominatingWrites) { | ||||
1228 | assert(Acc.getLocalInst()->getFunction() ==(static_cast <bool> (Acc.getLocalInst()->getFunction () == DomAcc->getLocalInst()->getFunction() && "Expected dominating writes to be in the same function!" ) ? void (0) : __assert_fail ("Acc.getLocalInst()->getFunction() == DomAcc->getLocalInst()->getFunction() && \"Expected dominating writes to be in the same function!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1230, __extension__ __PRETTY_FUNCTION__)) | ||||
1229 | DomAcc->getLocalInst()->getFunction() &&(static_cast <bool> (Acc.getLocalInst()->getFunction () == DomAcc->getLocalInst()->getFunction() && "Expected dominating writes to be in the same function!" ) ? void (0) : __assert_fail ("Acc.getLocalInst()->getFunction() == DomAcc->getLocalInst()->getFunction() && \"Expected dominating writes to be in the same function!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1230, __extension__ __PRETTY_FUNCTION__)) | ||||
1230 | "Expected dominating writes to be in the same function!")(static_cast <bool> (Acc.getLocalInst()->getFunction () == DomAcc->getLocalInst()->getFunction() && "Expected dominating writes to be in the same function!" ) ? void (0) : __assert_fail ("Acc.getLocalInst()->getFunction() == DomAcc->getLocalInst()->getFunction() && \"Expected dominating writes to be in the same function!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1230, __extension__ __PRETTY_FUNCTION__)); | ||||
1231 | |||||
1232 | if (DomAcc != &Acc && | ||||
1233 | DT->dominates(Acc.getLocalInst(), DomAcc->getLocalInst())) { | ||||
1234 | return true; | ||||
1235 | } | ||||
1236 | } | ||||
1237 | return false; | ||||
1238 | }; | ||||
1239 | |||||
1240 | // Run the user callback on all writes we cannot skip and return if that | ||||
1241 | // succeeded for all or not. | ||||
1242 | unsigned NumInterferingWrites = InterferingWrites.size(); | ||||
1243 | for (auto &It : InterferingWrites) { | ||||
1244 | if (!DT || NumInterferingWrites > MaxInterferingWrites || | ||||
1245 | !CanSkipAccess(*It.first, It.second)) { | ||||
1246 | if (!UserCB(*It.first, It.second)) | ||||
1247 | return false; | ||||
1248 | } | ||||
1249 | } | ||||
1250 | return true; | ||||
1251 | } | ||||
1252 | |||||
1253 | ChangeStatus translateAndAddCalleeState(Attributor &A, | ||||
1254 | const AAPointerInfo &CalleeAA, | ||||
1255 | int64_t CallArgOffset, CallBase &CB) { | ||||
1256 | using namespace AA::PointerInfo; | ||||
1257 | if (!CalleeAA.getState().isValidState() || !isValidState()) | ||||
1258 | return indicatePessimisticFixpoint(); | ||||
1259 | |||||
1260 | const auto &CalleeImplAA = static_cast<const AAPointerInfoImpl &>(CalleeAA); | ||||
1261 | bool IsByval = CalleeImplAA.getAssociatedArgument()->hasByValAttr(); | ||||
1262 | |||||
1263 | // Combine the accesses bin by bin. | ||||
1264 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
1265 | for (auto &It : CalleeImplAA.getState()) { | ||||
1266 | OffsetAndSize OAS = OffsetAndSize::getUnknown(); | ||||
1267 | if (CallArgOffset != OffsetAndSize::Unknown) | ||||
1268 | OAS = OffsetAndSize(It.first.getOffset() + CallArgOffset, | ||||
1269 | It.first.getSize()); | ||||
1270 | Accesses &Bin = AccessBins[OAS]; | ||||
1271 | for (const AAPointerInfo::Access &RAcc : It.second) { | ||||
1272 | if (IsByval && !RAcc.isRead()) | ||||
1273 | continue; | ||||
1274 | bool UsedAssumedInformation = false; | ||||
1275 | Optional<Value *> Content = A.translateArgumentToCallSiteContent( | ||||
1276 | RAcc.getContent(), CB, *this, UsedAssumedInformation); | ||||
1277 | AccessKind AK = | ||||
1278 | AccessKind(RAcc.getKind() & (IsByval ? AccessKind::AK_READ | ||||
1279 | : AccessKind::AK_READ_WRITE)); | ||||
1280 | Changed = | ||||
1281 | Changed | addAccess(OAS.getOffset(), OAS.getSize(), CB, Content, AK, | ||||
1282 | RAcc.getType(), RAcc.getRemoteInst(), &Bin); | ||||
1283 | } | ||||
1284 | } | ||||
1285 | return Changed; | ||||
1286 | } | ||||
1287 | |||||
1288 | /// Statistic tracking for all AAPointerInfo implementations. | ||||
1289 | /// See AbstractAttribute::trackStatistics(). | ||||
1290 | void trackPointerInfoStatistics(const IRPosition &IRP) const {} | ||||
1291 | }; | ||||
1292 | |||||
1293 | struct AAPointerInfoFloating : public AAPointerInfoImpl { | ||||
1294 | using AccessKind = AAPointerInfo::AccessKind; | ||||
1295 | AAPointerInfoFloating(const IRPosition &IRP, Attributor &A) | ||||
1296 | : AAPointerInfoImpl(IRP, A) {} | ||||
1297 | |||||
1298 | /// See AbstractAttribute::initialize(...). | ||||
1299 | void initialize(Attributor &A) override { AAPointerInfoImpl::initialize(A); } | ||||
1300 | |||||
1301 | /// Deal with an access and signal if it was handled successfully. | ||||
1302 | bool handleAccess(Attributor &A, Instruction &I, Value &Ptr, | ||||
1303 | Optional<Value *> Content, AccessKind Kind, int64_t Offset, | ||||
1304 | ChangeStatus &Changed, Type *Ty, | ||||
1305 | int64_t Size = OffsetAndSize::Unknown) { | ||||
1306 | using namespace AA::PointerInfo; | ||||
1307 | // No need to find a size if one is given or the offset is unknown. | ||||
1308 | if (Offset
| ||||
1309 | Ty
| ||||
1310 | const DataLayout &DL = A.getDataLayout(); | ||||
1311 | TypeSize AccessSize = DL.getTypeStoreSize(Ty); | ||||
1312 | if (!AccessSize.isScalable()) | ||||
1313 | Size = AccessSize.getFixedSize(); | ||||
1314 | } | ||||
1315 | Changed = Changed | addAccess(Offset, Size, I, Content, Kind, Ty); | ||||
| |||||
1316 | return true; | ||||
1317 | }; | ||||
1318 | |||||
1319 | /// Helper struct, will support ranges eventually. | ||||
1320 | struct OffsetInfo { | ||||
1321 | int64_t Offset = OffsetAndSize::Unknown; | ||||
1322 | |||||
1323 | bool operator==(const OffsetInfo &OI) const { return Offset == OI.Offset; } | ||||
1324 | }; | ||||
1325 | |||||
1326 | /// See AbstractAttribute::updateImpl(...). | ||||
1327 | ChangeStatus updateImpl(Attributor &A) override { | ||||
1328 | using namespace AA::PointerInfo; | ||||
1329 | State S = getState(); | ||||
1330 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
1331 | Value &AssociatedValue = getAssociatedValue(); | ||||
1332 | |||||
1333 | const DataLayout &DL = A.getDataLayout(); | ||||
1334 | DenseMap<Value *, OffsetInfo> OffsetInfoMap; | ||||
1335 | OffsetInfoMap[&AssociatedValue] = OffsetInfo{0}; | ||||
1336 | |||||
1337 | auto HandlePassthroughUser = [&](Value *Usr, OffsetInfo &PtrOI, | ||||
1338 | bool &Follow) { | ||||
1339 | OffsetInfo &UsrOI = OffsetInfoMap[Usr]; | ||||
1340 | UsrOI = PtrOI; | ||||
1341 | Follow = true; | ||||
1342 | return true; | ||||
1343 | }; | ||||
1344 | |||||
1345 | const auto *TLI = getAnchorScope() | ||||
1346 | ? A.getInfoCache().getTargetLibraryInfoForFunction( | ||||
1347 | *getAnchorScope()) | ||||
1348 | : nullptr; | ||||
1349 | auto UsePred = [&](const Use &U, bool &Follow) -> bool { | ||||
1350 | Value *CurPtr = U.get(); | ||||
1351 | User *Usr = U.getUser(); | ||||
1352 | LLVM_DEBUG(dbgs() << "[AAPointerInfo] Analyze " << *CurPtr << " in "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Analyze " << *CurPtr << " in " << *Usr << "\n"; } } while (false) | ||||
1353 | << *Usr << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Analyze " << *CurPtr << " in " << *Usr << "\n"; } } while (false); | ||||
1354 | assert(OffsetInfoMap.count(CurPtr) &&(static_cast <bool> (OffsetInfoMap.count(CurPtr) && "The current pointer offset should have been seeded!") ? void (0) : __assert_fail ("OffsetInfoMap.count(CurPtr) && \"The current pointer offset should have been seeded!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1355, __extension__ __PRETTY_FUNCTION__)) | ||||
1355 | "The current pointer offset should have been seeded!")(static_cast <bool> (OffsetInfoMap.count(CurPtr) && "The current pointer offset should have been seeded!") ? void (0) : __assert_fail ("OffsetInfoMap.count(CurPtr) && \"The current pointer offset should have been seeded!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1355, __extension__ __PRETTY_FUNCTION__)); | ||||
1356 | |||||
1357 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Usr)) { | ||||
1358 | if (CE->isCast()) | ||||
1359 | return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow); | ||||
1360 | if (CE->isCompare()) | ||||
1361 | return true; | ||||
1362 | if (!isa<GEPOperator>(CE)) { | ||||
1363 | LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled constant user " << *CEdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Unhandled constant user " << *CE << "\n"; } } while (false) | ||||
1364 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Unhandled constant user " << *CE << "\n"; } } while (false); | ||||
1365 | return false; | ||||
1366 | } | ||||
1367 | } | ||||
1368 | if (auto *GEP = dyn_cast<GEPOperator>(Usr)) { | ||||
1369 | // Note the order here, the Usr access might change the map, CurPtr is | ||||
1370 | // already in it though. | ||||
1371 | OffsetInfo &UsrOI = OffsetInfoMap[Usr]; | ||||
1372 | OffsetInfo &PtrOI = OffsetInfoMap[CurPtr]; | ||||
1373 | UsrOI = PtrOI; | ||||
1374 | |||||
1375 | // TODO: Use range information. | ||||
1376 | if (PtrOI.Offset == OffsetAndSize::Unknown || | ||||
1377 | !GEP->hasAllConstantIndices()) { | ||||
1378 | UsrOI.Offset = OffsetAndSize::Unknown; | ||||
1379 | Follow = true; | ||||
1380 | return true; | ||||
1381 | } | ||||
1382 | |||||
1383 | SmallVector<Value *, 8> Indices; | ||||
1384 | for (Use &Idx : GEP->indices()) { | ||||
1385 | if (auto *CIdx = dyn_cast<ConstantInt>(Idx)) { | ||||
1386 | Indices.push_back(CIdx); | ||||
1387 | continue; | ||||
1388 | } | ||||
1389 | |||||
1390 | LLVM_DEBUG(dbgs() << "[AAPointerInfo] Non constant GEP index " << *GEPdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Non constant GEP index " << *GEP << " : " << *Idx << "\n"; } } while (false) | ||||
1391 | << " : " << *Idx << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Non constant GEP index " << *GEP << " : " << *Idx << "\n"; } } while (false); | ||||
1392 | return false; | ||||
1393 | } | ||||
1394 | UsrOI.Offset = PtrOI.Offset + DL.getIndexedOffsetInType( | ||||
1395 | GEP->getSourceElementType(), Indices); | ||||
1396 | Follow = true; | ||||
1397 | return true; | ||||
1398 | } | ||||
1399 | if (isa<CastInst>(Usr) || isa<SelectInst>(Usr)) | ||||
1400 | return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow); | ||||
1401 | |||||
1402 | // For PHIs we need to take care of the recurrence explicitly as the value | ||||
1403 | // might change while we iterate through a loop. For now, we give up if | ||||
1404 | // the PHI is not invariant. | ||||
1405 | if (isa<PHINode>(Usr)) { | ||||
1406 | // Note the order here, the Usr access might change the map, CurPtr is | ||||
1407 | // already in it though. | ||||
1408 | OffsetInfo &UsrOI = OffsetInfoMap[Usr]; | ||||
1409 | OffsetInfo &PtrOI = OffsetInfoMap[CurPtr]; | ||||
1410 | // Check if the PHI is invariant (so far). | ||||
1411 | if (UsrOI == PtrOI) | ||||
1412 | return true; | ||||
1413 | |||||
1414 | // Check if the PHI operand has already an unknown offset as we can't | ||||
1415 | // improve on that anymore. | ||||
1416 | if (PtrOI.Offset == OffsetAndSize::Unknown) { | ||||
1417 | UsrOI = PtrOI; | ||||
1418 | Follow = true; | ||||
1419 | return true; | ||||
1420 | } | ||||
1421 | |||||
1422 | // Check if the PHI operand is not dependent on the PHI itself. | ||||
1423 | // TODO: This is not great as we look at the pointer type. However, it | ||||
1424 | // is unclear where the Offset size comes from with typeless pointers. | ||||
1425 | APInt Offset( | ||||
1426 | DL.getIndexSizeInBits(CurPtr->getType()->getPointerAddressSpace()), | ||||
1427 | 0); | ||||
1428 | if (&AssociatedValue == CurPtr->stripAndAccumulateConstantOffsets( | ||||
1429 | DL, Offset, /* AllowNonInbounds */ true)) { | ||||
1430 | if (Offset != PtrOI.Offset) { | ||||
1431 | LLVM_DEBUG(dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] PHI operand pointer offset mismatch " << *CurPtr << " in " << *Usr << "\n" ; } } while (false) | ||||
1432 | << "[AAPointerInfo] PHI operand pointer offset mismatch "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] PHI operand pointer offset mismatch " << *CurPtr << " in " << *Usr << "\n" ; } } while (false) | ||||
1433 | << *CurPtr << " in " << *Usr << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] PHI operand pointer offset mismatch " << *CurPtr << " in " << *Usr << "\n" ; } } while (false); | ||||
1434 | return false; | ||||
1435 | } | ||||
1436 | return HandlePassthroughUser(Usr, PtrOI, Follow); | ||||
1437 | } | ||||
1438 | |||||
1439 | // TODO: Approximate in case we know the direction of the recurrence. | ||||
1440 | LLVM_DEBUG(dbgs() << "[AAPointerInfo] PHI operand is too complex "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] PHI operand is too complex " << *CurPtr << " in " << *Usr << "\n" ; } } while (false) | ||||
1441 | << *CurPtr << " in " << *Usr << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] PHI operand is too complex " << *CurPtr << " in " << *Usr << "\n" ; } } while (false); | ||||
1442 | UsrOI = PtrOI; | ||||
1443 | UsrOI.Offset = OffsetAndSize::Unknown; | ||||
1444 | Follow = true; | ||||
1445 | return true; | ||||
1446 | } | ||||
1447 | |||||
1448 | if (auto *LoadI = dyn_cast<LoadInst>(Usr)) | ||||
1449 | return handleAccess(A, *LoadI, *CurPtr, /* Content */ nullptr, | ||||
1450 | AccessKind::AK_READ, OffsetInfoMap[CurPtr].Offset, | ||||
1451 | Changed, LoadI->getType()); | ||||
1452 | if (auto *StoreI = dyn_cast<StoreInst>(Usr)) { | ||||
1453 | if (StoreI->getValueOperand() == CurPtr) { | ||||
1454 | LLVM_DEBUG(dbgs() << "[AAPointerInfo] Escaping use in store "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Escaping use in store " << *StoreI << "\n"; } } while (false) | ||||
1455 | << *StoreI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Escaping use in store " << *StoreI << "\n"; } } while (false); | ||||
1456 | return false; | ||||
1457 | } | ||||
1458 | bool UsedAssumedInformation = false; | ||||
1459 | Optional<Value *> Content = A.getAssumedSimplified( | ||||
1460 | *StoreI->getValueOperand(), *this, UsedAssumedInformation); | ||||
1461 | return handleAccess(A, *StoreI, *CurPtr, Content, AccessKind::AK_WRITE, | ||||
1462 | OffsetInfoMap[CurPtr].Offset, Changed, | ||||
1463 | StoreI->getValueOperand()->getType()); | ||||
1464 | } | ||||
1465 | if (auto *CB = dyn_cast<CallBase>(Usr)) { | ||||
1466 | if (CB->isLifetimeStartOrEnd()) | ||||
1467 | return true; | ||||
1468 | if (TLI && isFreeCall(CB, TLI)) | ||||
1469 | return true; | ||||
1470 | if (CB->isArgOperand(&U)) { | ||||
1471 | unsigned ArgNo = CB->getArgOperandNo(&U); | ||||
1472 | const auto &CSArgPI = A.getAAFor<AAPointerInfo>( | ||||
1473 | *this, IRPosition::callsite_argument(*CB, ArgNo), | ||||
1474 | DepClassTy::REQUIRED); | ||||
1475 | Changed = translateAndAddCalleeState( | ||||
1476 | A, CSArgPI, OffsetInfoMap[CurPtr].Offset, *CB) | | ||||
1477 | Changed; | ||||
1478 | return true; | ||||
1479 | } | ||||
1480 | LLVM_DEBUG(dbgs() << "[AAPointerInfo] Call user not handled " << *CBdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Call user not handled " << *CB << "\n"; } } while (false) | ||||
1481 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Call user not handled " << *CB << "\n"; } } while (false); | ||||
1482 | // TODO: Allow some call uses | ||||
1483 | return false; | ||||
1484 | } | ||||
1485 | |||||
1486 | LLVM_DEBUG(dbgs() << "[AAPointerInfo] User not handled " << *Usr << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] User not handled " << *Usr << "\n"; } } while (false); | ||||
1487 | return false; | ||||
1488 | }; | ||||
1489 | auto EquivalentUseCB = [&](const Use &OldU, const Use &NewU) { | ||||
1490 | if (OffsetInfoMap.count(NewU)) | ||||
1491 | return OffsetInfoMap[NewU] == OffsetInfoMap[OldU]; | ||||
1492 | OffsetInfoMap[NewU] = OffsetInfoMap[OldU]; | ||||
1493 | return true; | ||||
1494 | }; | ||||
1495 | if (!A.checkForAllUses(UsePred, *this, AssociatedValue, | ||||
1496 | /* CheckBBLivenessOnly */ true, DepClassTy::OPTIONAL, | ||||
1497 | EquivalentUseCB)) | ||||
1498 | return indicatePessimisticFixpoint(); | ||||
1499 | |||||
1500 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1501 | dbgs() << "Accesses by bin after update:\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1502 | for (auto &It : AccessBins) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1503 | dbgs() << "[" << It.first.getOffset() << "-"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1504 | << It.first.getOffset() + It.first.getSize()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1505 | << "] : " << It.getSecond().size() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1506 | for (auto &Acc : It.getSecond()) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1507 | dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1508 | << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1509 | if (Acc.getLocalInst() != Acc.getRemoteInst())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1510 | dbgs() << " --> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1511 | << *Acc.getRemoteInst() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1512 | if (!Acc.isWrittenValueYetUndetermined())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1513 | dbgs() << " - " << Acc.getWrittenValue() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1514 | }do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1515 | }do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false) | ||||
1516 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "Accesses by bin after update:\n" ; for (auto &It : AccessBins) { dbgs() << "[" << It.first.getOffset() << "-" << It.first.getOffset () + It.first.getSize() << "] : " << It.getSecond ().size() << "\n"; for (auto &Acc : It.getSecond()) { dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n"; if (Acc.getLocalInst () != Acc.getRemoteInst()) dbgs() << " --> " << *Acc.getRemoteInst() << "\n"; if (!Acc.isWrittenValueYetUndetermined ()) dbgs() << " - " << Acc.getWrittenValue() << "\n"; } } }; } } while (false); | ||||
1517 | |||||
1518 | return Changed; | ||||
1519 | } | ||||
1520 | |||||
1521 | /// See AbstractAttribute::trackStatistics() | ||||
1522 | void trackStatistics() const override { | ||||
1523 | AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition()); | ||||
1524 | } | ||||
1525 | }; | ||||
1526 | |||||
1527 | struct AAPointerInfoReturned final : AAPointerInfoImpl { | ||||
1528 | AAPointerInfoReturned(const IRPosition &IRP, Attributor &A) | ||||
1529 | : AAPointerInfoImpl(IRP, A) {} | ||||
1530 | |||||
1531 | /// See AbstractAttribute::updateImpl(...). | ||||
1532 | ChangeStatus updateImpl(Attributor &A) override { | ||||
1533 | return indicatePessimisticFixpoint(); | ||||
1534 | } | ||||
1535 | |||||
1536 | /// See AbstractAttribute::trackStatistics() | ||||
1537 | void trackStatistics() const override { | ||||
1538 | AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition()); | ||||
1539 | } | ||||
1540 | }; | ||||
1541 | |||||
1542 | struct AAPointerInfoArgument final : AAPointerInfoFloating { | ||||
1543 | AAPointerInfoArgument(const IRPosition &IRP, Attributor &A) | ||||
1544 | : AAPointerInfoFloating(IRP, A) {} | ||||
1545 | |||||
1546 | /// See AbstractAttribute::initialize(...). | ||||
1547 | void initialize(Attributor &A) override { | ||||
1548 | AAPointerInfoFloating::initialize(A); | ||||
1549 | if (getAnchorScope()->isDeclaration()) | ||||
1550 | indicatePessimisticFixpoint(); | ||||
1551 | } | ||||
1552 | |||||
1553 | /// See AbstractAttribute::trackStatistics() | ||||
1554 | void trackStatistics() const override { | ||||
1555 | AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition()); | ||||
1556 | } | ||||
1557 | }; | ||||
1558 | |||||
1559 | struct AAPointerInfoCallSiteArgument final : AAPointerInfoFloating { | ||||
1560 | AAPointerInfoCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
1561 | : AAPointerInfoFloating(IRP, A) {} | ||||
1562 | |||||
1563 | /// See AbstractAttribute::updateImpl(...). | ||||
1564 | ChangeStatus updateImpl(Attributor &A) override { | ||||
1565 | using namespace AA::PointerInfo; | ||||
1566 | // We handle memory intrinsics explicitly, at least the first (= | ||||
1567 | // destination) and second (=source) arguments as we know how they are | ||||
1568 | // accessed. | ||||
1569 | if (auto *MI
| ||||
| |||||
1570 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); | ||||
1571 | int64_t LengthVal = OffsetAndSize::Unknown; | ||||
1572 | if (Length) | ||||
1573 | LengthVal = Length->getSExtValue(); | ||||
1574 | Value &Ptr = getAssociatedValue(); | ||||
1575 | unsigned ArgNo = getIRPosition().getCallSiteArgNo(); | ||||
1576 | ChangeStatus Changed; | ||||
1577 | if (ArgNo == 0) { | ||||
1578 | handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_WRITE, 0, Changed, | ||||
1579 | nullptr, LengthVal); | ||||
1580 | } else if (ArgNo == 1) { | ||||
1581 | handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_READ, 0, Changed, | ||||
1582 | nullptr, LengthVal); | ||||
1583 | } else { | ||||
1584 | LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled memory intrinsic "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Unhandled memory intrinsic " << *MI << "\n"; } } while (false) | ||||
1585 | << *MI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPointerInfo] Unhandled memory intrinsic " << *MI << "\n"; } } while (false); | ||||
1586 | return indicatePessimisticFixpoint(); | ||||
1587 | } | ||||
1588 | return Changed; | ||||
1589 | } | ||||
1590 | |||||
1591 | // TODO: Once we have call site specific value information we can provide | ||||
1592 | // call site specific liveness information and then it makes | ||||
1593 | // sense to specialize attributes for call sites arguments instead of | ||||
1594 | // redirecting requests to the callee argument. | ||||
1595 | Argument *Arg = getAssociatedArgument(); | ||||
1596 | if (!Arg) | ||||
1597 | return indicatePessimisticFixpoint(); | ||||
1598 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | ||||
1599 | auto &ArgAA = | ||||
1600 | A.getAAFor<AAPointerInfo>(*this, ArgPos, DepClassTy::REQUIRED); | ||||
1601 | return translateAndAddCalleeState(A, ArgAA, 0, *cast<CallBase>(getCtxI())); | ||||
1602 | } | ||||
1603 | |||||
1604 | /// See AbstractAttribute::trackStatistics() | ||||
1605 | void trackStatistics() const override { | ||||
1606 | AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition()); | ||||
1607 | } | ||||
1608 | }; | ||||
1609 | |||||
1610 | struct AAPointerInfoCallSiteReturned final : AAPointerInfoFloating { | ||||
1611 | AAPointerInfoCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
1612 | : AAPointerInfoFloating(IRP, A) {} | ||||
1613 | |||||
1614 | /// See AbstractAttribute::trackStatistics() | ||||
1615 | void trackStatistics() const override { | ||||
1616 | AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition()); | ||||
1617 | } | ||||
1618 | }; | ||||
1619 | |||||
1620 | /// -----------------------NoUnwind Function Attribute-------------------------- | ||||
1621 | |||||
1622 | struct AANoUnwindImpl : AANoUnwind { | ||||
1623 | AANoUnwindImpl(const IRPosition &IRP, Attributor &A) : AANoUnwind(IRP, A) {} | ||||
1624 | |||||
1625 | const std::string getAsStr() const override { | ||||
1626 | return getAssumed() ? "nounwind" : "may-unwind"; | ||||
1627 | } | ||||
1628 | |||||
1629 | /// See AbstractAttribute::updateImpl(...). | ||||
1630 | ChangeStatus updateImpl(Attributor &A) override { | ||||
1631 | auto Opcodes = { | ||||
1632 | (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, | ||||
1633 | (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet, | ||||
1634 | (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume}; | ||||
1635 | |||||
1636 | auto CheckForNoUnwind = [&](Instruction &I) { | ||||
1637 | if (!I.mayThrow()) | ||||
1638 | return true; | ||||
1639 | |||||
1640 | if (const auto *CB = dyn_cast<CallBase>(&I)) { | ||||
1641 | const auto &NoUnwindAA = A.getAAFor<AANoUnwind>( | ||||
1642 | *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED); | ||||
1643 | return NoUnwindAA.isAssumedNoUnwind(); | ||||
1644 | } | ||||
1645 | return false; | ||||
1646 | }; | ||||
1647 | |||||
1648 | bool UsedAssumedInformation = false; | ||||
1649 | if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes, | ||||
1650 | UsedAssumedInformation)) | ||||
1651 | return indicatePessimisticFixpoint(); | ||||
1652 | |||||
1653 | return ChangeStatus::UNCHANGED; | ||||
1654 | } | ||||
1655 | }; | ||||
1656 | |||||
1657 | struct AANoUnwindFunction final : public AANoUnwindImpl { | ||||
1658 | AANoUnwindFunction(const IRPosition &IRP, Attributor &A) | ||||
1659 | : AANoUnwindImpl(IRP, A) {} | ||||
1660 | |||||
1661 | /// See AbstractAttribute::trackStatistics() | ||||
1662 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind){ static llvm::Statistic NumIRFunction_nounwind = {"attributor" , "NumIRFunction_nounwind", ("Number of " "functions" " marked '" "nounwind" "'")};; ++(NumIRFunction_nounwind); } } | ||||
1663 | }; | ||||
1664 | |||||
1665 | /// NoUnwind attribute deduction for a call sites. | ||||
1666 | struct AANoUnwindCallSite final : AANoUnwindImpl { | ||||
1667 | AANoUnwindCallSite(const IRPosition &IRP, Attributor &A) | ||||
1668 | : AANoUnwindImpl(IRP, A) {} | ||||
1669 | |||||
1670 | /// See AbstractAttribute::initialize(...). | ||||
1671 | void initialize(Attributor &A) override { | ||||
1672 | AANoUnwindImpl::initialize(A); | ||||
1673 | Function *F = getAssociatedFunction(); | ||||
1674 | if (!F || F->isDeclaration()) | ||||
1675 | indicatePessimisticFixpoint(); | ||||
1676 | } | ||||
1677 | |||||
1678 | /// See AbstractAttribute::updateImpl(...). | ||||
1679 | ChangeStatus updateImpl(Attributor &A) override { | ||||
1680 | // TODO: Once we have call site specific value information we can provide | ||||
1681 | // call site specific liveness information and then it makes | ||||
1682 | // sense to specialize attributes for call sites arguments instead of | ||||
1683 | // redirecting requests to the callee argument. | ||||
1684 | Function *F = getAssociatedFunction(); | ||||
1685 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
1686 | auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::REQUIRED); | ||||
1687 | return clampStateAndIndicateChange(getState(), FnAA.getState()); | ||||
1688 | } | ||||
1689 | |||||
1690 | /// See AbstractAttribute::trackStatistics() | ||||
1691 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind){ static llvm::Statistic NumIRCS_nounwind = {"attributor", "NumIRCS_nounwind" , ("Number of " "call site" " marked '" "nounwind" "'")};; ++ (NumIRCS_nounwind); }; } | ||||
1692 | }; | ||||
1693 | |||||
1694 | /// --------------------- Function Return Values ------------------------------- | ||||
1695 | |||||
1696 | /// "Attribute" that collects all potential returned values and the return | ||||
1697 | /// instructions that they arise from. | ||||
1698 | /// | ||||
1699 | /// If there is a unique returned value R, the manifest method will: | ||||
1700 | /// - mark R with the "returned" attribute, if R is an argument. | ||||
1701 | class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState { | ||||
1702 | |||||
1703 | /// Mapping of values potentially returned by the associated function to the | ||||
1704 | /// return instructions that might return them. | ||||
1705 | MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues; | ||||
1706 | |||||
1707 | /// State flags | ||||
1708 | /// | ||||
1709 | ///{ | ||||
1710 | bool IsFixed = false; | ||||
1711 | bool IsValidState = true; | ||||
1712 | ///} | ||||
1713 | |||||
1714 | public: | ||||
1715 | AAReturnedValuesImpl(const IRPosition &IRP, Attributor &A) | ||||
1716 | : AAReturnedValues(IRP, A) {} | ||||
1717 | |||||
1718 | /// See AbstractAttribute::initialize(...). | ||||
1719 | void initialize(Attributor &A) override { | ||||
1720 | // Reset the state. | ||||
1721 | IsFixed = false; | ||||
1722 | IsValidState = true; | ||||
1723 | ReturnedValues.clear(); | ||||
1724 | |||||
1725 | Function *F = getAssociatedFunction(); | ||||
1726 | if (!F || F->isDeclaration()) { | ||||
1727 | indicatePessimisticFixpoint(); | ||||
1728 | return; | ||||
1729 | } | ||||
1730 | assert(!F->getReturnType()->isVoidTy() &&(static_cast <bool> (!F->getReturnType()->isVoidTy () && "Did not expect a void return type!") ? void (0 ) : __assert_fail ("!F->getReturnType()->isVoidTy() && \"Did not expect a void return type!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1731, __extension__ __PRETTY_FUNCTION__)) | ||||
1731 | "Did not expect a void return type!")(static_cast <bool> (!F->getReturnType()->isVoidTy () && "Did not expect a void return type!") ? void (0 ) : __assert_fail ("!F->getReturnType()->isVoidTy() && \"Did not expect a void return type!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1731, __extension__ __PRETTY_FUNCTION__)); | ||||
1732 | |||||
1733 | // The map from instruction opcodes to those instructions in the function. | ||||
1734 | auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F); | ||||
1735 | |||||
1736 | // Look through all arguments, if one is marked as returned we are done. | ||||
1737 | for (Argument &Arg : F->args()) { | ||||
1738 | if (Arg.hasReturnedAttr()) { | ||||
1739 | auto &ReturnInstSet = ReturnedValues[&Arg]; | ||||
1740 | if (auto *Insts = OpcodeInstMap.lookup(Instruction::Ret)) | ||||
1741 | for (Instruction *RI : *Insts) | ||||
1742 | ReturnInstSet.insert(cast<ReturnInst>(RI)); | ||||
1743 | |||||
1744 | indicateOptimisticFixpoint(); | ||||
1745 | return; | ||||
1746 | } | ||||
1747 | } | ||||
1748 | |||||
1749 | if (!A.isFunctionIPOAmendable(*F)) | ||||
1750 | indicatePessimisticFixpoint(); | ||||
1751 | } | ||||
1752 | |||||
1753 | /// See AbstractAttribute::manifest(...). | ||||
1754 | ChangeStatus manifest(Attributor &A) override; | ||||
1755 | |||||
1756 | /// See AbstractAttribute::getState(...). | ||||
1757 | AbstractState &getState() override { return *this; } | ||||
1758 | |||||
1759 | /// See AbstractAttribute::getState(...). | ||||
1760 | const AbstractState &getState() const override { return *this; } | ||||
1761 | |||||
1762 | /// See AbstractAttribute::updateImpl(Attributor &A). | ||||
1763 | ChangeStatus updateImpl(Attributor &A) override; | ||||
1764 | |||||
1765 | llvm::iterator_range<iterator> returned_values() override { | ||||
1766 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); | ||||
1767 | } | ||||
1768 | |||||
1769 | llvm::iterator_range<const_iterator> returned_values() const override { | ||||
1770 | return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end()); | ||||
1771 | } | ||||
1772 | |||||
1773 | /// Return the number of potential return values, -1 if unknown. | ||||
1774 | size_t getNumReturnValues() const override { | ||||
1775 | return isValidState() ? ReturnedValues.size() : -1; | ||||
1776 | } | ||||
1777 | |||||
1778 | /// Return an assumed unique return value if a single candidate is found. If | ||||
1779 | /// there cannot be one, return a nullptr. If it is not clear yet, return the | ||||
1780 | /// Optional::NoneType. | ||||
1781 | Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const; | ||||
1782 | |||||
1783 | /// See AbstractState::checkForAllReturnedValues(...). | ||||
1784 | bool checkForAllReturnedValuesAndReturnInsts( | ||||
1785 | function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred) | ||||
1786 | const override; | ||||
1787 | |||||
1788 | /// Pretty print the attribute similar to the IR representation. | ||||
1789 | const std::string getAsStr() const override; | ||||
1790 | |||||
1791 | /// See AbstractState::isAtFixpoint(). | ||||
1792 | bool isAtFixpoint() const override { return IsFixed; } | ||||
1793 | |||||
1794 | /// See AbstractState::isValidState(). | ||||
1795 | bool isValidState() const override { return IsValidState; } | ||||
1796 | |||||
1797 | /// See AbstractState::indicateOptimisticFixpoint(...). | ||||
1798 | ChangeStatus indicateOptimisticFixpoint() override { | ||||
1799 | IsFixed = true; | ||||
1800 | return ChangeStatus::UNCHANGED; | ||||
1801 | } | ||||
1802 | |||||
1803 | ChangeStatus indicatePessimisticFixpoint() override { | ||||
1804 | IsFixed = true; | ||||
1805 | IsValidState = false; | ||||
1806 | return ChangeStatus::CHANGED; | ||||
1807 | } | ||||
1808 | }; | ||||
1809 | |||||
1810 | ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { | ||||
1811 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
1812 | |||||
1813 | // Bookkeeping. | ||||
1814 | assert(isValidState())(static_cast <bool> (isValidState()) ? void (0) : __assert_fail ("isValidState()", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 1814, __extension__ __PRETTY_FUNCTION__)); | ||||
1815 | STATS_DECLTRACK(KnownReturnValues, FunctionReturn,{ static llvm::Statistic NumIRFunctionReturn_KnownReturnValues = {"attributor", "NumIRFunctionReturn_KnownReturnValues", "Number of function with known return values" };; ++(NumIRFunctionReturn_KnownReturnValues); } | ||||
1816 | "Number of function with known return values"){ static llvm::Statistic NumIRFunctionReturn_KnownReturnValues = {"attributor", "NumIRFunctionReturn_KnownReturnValues", "Number of function with known return values" };; ++(NumIRFunctionReturn_KnownReturnValues); }; | ||||
1817 | |||||
1818 | // Check if we have an assumed unique return value that we could manifest. | ||||
1819 | Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A); | ||||
1820 | |||||
1821 | if (!UniqueRV.hasValue() || !UniqueRV.getValue()) | ||||
1822 | return Changed; | ||||
1823 | |||||
1824 | // Bookkeeping. | ||||
1825 | STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,{ static llvm::Statistic NumIRFunctionReturn_UniqueReturnValue = {"attributor", "NumIRFunctionReturn_UniqueReturnValue", "Number of function with unique return" };; ++(NumIRFunctionReturn_UniqueReturnValue); } | ||||
1826 | "Number of function with unique return"){ static llvm::Statistic NumIRFunctionReturn_UniqueReturnValue = {"attributor", "NumIRFunctionReturn_UniqueReturnValue", "Number of function with unique return" };; ++(NumIRFunctionReturn_UniqueReturnValue); }; | ||||
1827 | // If the assumed unique return value is an argument, annotate it. | ||||
1828 | if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) { | ||||
1829 | if (UniqueRVArg->getType()->canLosslesslyBitCastTo( | ||||
1830 | getAssociatedFunction()->getReturnType())) { | ||||
1831 | getIRPosition() = IRPosition::argument(*UniqueRVArg); | ||||
1832 | Changed = IRAttribute::manifest(A); | ||||
1833 | } | ||||
1834 | } | ||||
1835 | return Changed; | ||||
1836 | } | ||||
1837 | |||||
1838 | const std::string AAReturnedValuesImpl::getAsStr() const { | ||||
1839 | return (isAtFixpoint() ? "returns(#" : "may-return(#") + | ||||
1840 | (isValidState() ? std::to_string(getNumReturnValues()) : "?") + ")"; | ||||
1841 | } | ||||
1842 | |||||
1843 | Optional<Value *> | ||||
1844 | AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const { | ||||
1845 | // If checkForAllReturnedValues provides a unique value, ignoring potential | ||||
1846 | // undef values that can also be present, it is assumed to be the actual | ||||
1847 | // return value and forwarded to the caller of this method. If there are | ||||
1848 | // multiple, a nullptr is returned indicating there cannot be a unique | ||||
1849 | // returned value. | ||||
1850 | Optional<Value *> UniqueRV; | ||||
1851 | Type *Ty = getAssociatedFunction()->getReturnType(); | ||||
1852 | |||||
1853 | auto Pred = [&](Value &RV) -> bool { | ||||
1854 | UniqueRV = AA::combineOptionalValuesInAAValueLatice(UniqueRV, &RV, Ty); | ||||
1855 | return UniqueRV != Optional<Value *>(nullptr); | ||||
1856 | }; | ||||
1857 | |||||
1858 | if (!A.checkForAllReturnedValues(Pred, *this)) | ||||
1859 | UniqueRV = nullptr; | ||||
1860 | |||||
1861 | return UniqueRV; | ||||
1862 | } | ||||
1863 | |||||
1864 | bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts( | ||||
1865 | function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred) | ||||
1866 | const { | ||||
1867 | if (!isValidState()) | ||||
1868 | return false; | ||||
1869 | |||||
1870 | // Check all returned values but ignore call sites as long as we have not | ||||
1871 | // encountered an overdefined one during an update. | ||||
1872 | for (auto &It : ReturnedValues) { | ||||
1873 | Value *RV = It.first; | ||||
1874 | if (!Pred(*RV, It.second)) | ||||
1875 | return false; | ||||
1876 | } | ||||
1877 | |||||
1878 | return true; | ||||
1879 | } | ||||
1880 | |||||
1881 | ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) { | ||||
1882 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
1883 | |||||
1884 | auto ReturnValueCB = [&](Value &V, const Instruction *CtxI, ReturnInst &Ret, | ||||
1885 | bool) -> bool { | ||||
1886 | assert(AA::isValidInScope(V, Ret.getFunction()) &&(static_cast <bool> (AA::isValidInScope(V, Ret.getFunction ()) && "Assumed returned value should be valid in function scope!" ) ? void (0) : __assert_fail ("AA::isValidInScope(V, Ret.getFunction()) && \"Assumed returned value should be valid in function scope!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1887, __extension__ __PRETTY_FUNCTION__)) | ||||
1887 | "Assumed returned value should be valid in function scope!")(static_cast <bool> (AA::isValidInScope(V, Ret.getFunction ()) && "Assumed returned value should be valid in function scope!" ) ? void (0) : __assert_fail ("AA::isValidInScope(V, Ret.getFunction()) && \"Assumed returned value should be valid in function scope!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1887, __extension__ __PRETTY_FUNCTION__)); | ||||
1888 | if (ReturnedValues[&V].insert(&Ret)) | ||||
1889 | Changed = ChangeStatus::CHANGED; | ||||
1890 | return true; | ||||
1891 | }; | ||||
1892 | |||||
1893 | bool UsedAssumedInformation = false; | ||||
1894 | auto ReturnInstCB = [&](Instruction &I) { | ||||
1895 | ReturnInst &Ret = cast<ReturnInst>(I); | ||||
1896 | return genericValueTraversal<ReturnInst>( | ||||
1897 | A, IRPosition::value(*Ret.getReturnValue()), *this, Ret, ReturnValueCB, | ||||
1898 | &I, UsedAssumedInformation, /* UseValueSimplify */ true, | ||||
1899 | /* MaxValues */ 16, | ||||
1900 | /* StripCB */ nullptr, /* Intraprocedural */ true); | ||||
1901 | }; | ||||
1902 | |||||
1903 | // Discover returned values from all live returned instructions in the | ||||
1904 | // associated function. | ||||
1905 | if (!A.checkForAllInstructions(ReturnInstCB, *this, {Instruction::Ret}, | ||||
1906 | UsedAssumedInformation)) | ||||
1907 | return indicatePessimisticFixpoint(); | ||||
1908 | return Changed; | ||||
1909 | } | ||||
1910 | |||||
1911 | struct AAReturnedValuesFunction final : public AAReturnedValuesImpl { | ||||
1912 | AAReturnedValuesFunction(const IRPosition &IRP, Attributor &A) | ||||
1913 | : AAReturnedValuesImpl(IRP, A) {} | ||||
1914 | |||||
1915 | /// See AbstractAttribute::trackStatistics() | ||||
1916 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned){ static llvm::Statistic NumIRArguments_returned = {"attributor" , "NumIRArguments_returned", ("Number of " "arguments" " marked '" "returned" "'")};; ++(NumIRArguments_returned); } } | ||||
1917 | }; | ||||
1918 | |||||
1919 | /// Returned values information for a call sites. | ||||
1920 | struct AAReturnedValuesCallSite final : AAReturnedValuesImpl { | ||||
1921 | AAReturnedValuesCallSite(const IRPosition &IRP, Attributor &A) | ||||
1922 | : AAReturnedValuesImpl(IRP, A) {} | ||||
1923 | |||||
1924 | /// See AbstractAttribute::initialize(...). | ||||
1925 | void initialize(Attributor &A) override { | ||||
1926 | // TODO: Once we have call site specific value information we can provide | ||||
1927 | // call site specific liveness information and then it makes | ||||
1928 | // sense to specialize attributes for call sites instead of | ||||
1929 | // redirecting requests to the callee. | ||||
1930 | llvm_unreachable("Abstract attributes for returned values are not "::llvm::llvm_unreachable_internal("Abstract attributes for returned values are not " "supported for call sites yet!", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 1931) | ||||
1931 | "supported for call sites yet!")::llvm::llvm_unreachable_internal("Abstract attributes for returned values are not " "supported for call sites yet!", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 1931); | ||||
1932 | } | ||||
1933 | |||||
1934 | /// See AbstractAttribute::updateImpl(...). | ||||
1935 | ChangeStatus updateImpl(Attributor &A) override { | ||||
1936 | return indicatePessimisticFixpoint(); | ||||
1937 | } | ||||
1938 | |||||
1939 | /// See AbstractAttribute::trackStatistics() | ||||
1940 | void trackStatistics() const override {} | ||||
1941 | }; | ||||
1942 | |||||
1943 | /// ------------------------ NoSync Function Attribute ------------------------- | ||||
1944 | |||||
1945 | struct AANoSyncImpl : AANoSync { | ||||
1946 | AANoSyncImpl(const IRPosition &IRP, Attributor &A) : AANoSync(IRP, A) {} | ||||
1947 | |||||
1948 | const std::string getAsStr() const override { | ||||
1949 | return getAssumed() ? "nosync" : "may-sync"; | ||||
1950 | } | ||||
1951 | |||||
1952 | /// See AbstractAttribute::updateImpl(...). | ||||
1953 | ChangeStatus updateImpl(Attributor &A) override; | ||||
1954 | }; | ||||
1955 | |||||
1956 | bool AANoSync::isNonRelaxedAtomic(const Instruction *I) { | ||||
1957 | if (!I->isAtomic()) | ||||
1958 | return false; | ||||
1959 | |||||
1960 | if (auto *FI = dyn_cast<FenceInst>(I)) | ||||
1961 | // All legal orderings for fence are stronger than monotonic. | ||||
1962 | return FI->getSyncScopeID() != SyncScope::SingleThread; | ||||
1963 | if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I)) { | ||||
1964 | // Unordered is not a legal ordering for cmpxchg. | ||||
1965 | return (AI->getSuccessOrdering() != AtomicOrdering::Monotonic || | ||||
1966 | AI->getFailureOrdering() != AtomicOrdering::Monotonic); | ||||
1967 | } | ||||
1968 | |||||
1969 | AtomicOrdering Ordering; | ||||
1970 | switch (I->getOpcode()) { | ||||
1971 | case Instruction::AtomicRMW: | ||||
1972 | Ordering = cast<AtomicRMWInst>(I)->getOrdering(); | ||||
1973 | break; | ||||
1974 | case Instruction::Store: | ||||
1975 | Ordering = cast<StoreInst>(I)->getOrdering(); | ||||
1976 | break; | ||||
1977 | case Instruction::Load: | ||||
1978 | Ordering = cast<LoadInst>(I)->getOrdering(); | ||||
1979 | break; | ||||
1980 | default: | ||||
1981 | llvm_unreachable(::llvm::llvm_unreachable_internal("New atomic operations need to be known in the attributor." , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1982) | ||||
1982 | "New atomic operations need to be known in the attributor.")::llvm::llvm_unreachable_internal("New atomic operations need to be known in the attributor." , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1982); | ||||
1983 | } | ||||
1984 | |||||
1985 | return (Ordering != AtomicOrdering::Unordered && | ||||
1986 | Ordering != AtomicOrdering::Monotonic); | ||||
1987 | } | ||||
1988 | |||||
1989 | /// Return true if this intrinsic is nosync. This is only used for intrinsics | ||||
1990 | /// which would be nosync except that they have a volatile flag. All other | ||||
1991 | /// intrinsics are simply annotated with the nosync attribute in Intrinsics.td. | ||||
1992 | bool AANoSync::isNoSyncIntrinsic(const Instruction *I) { | ||||
1993 | if (auto *MI = dyn_cast<MemIntrinsic>(I)) | ||||
1994 | return !MI->isVolatile(); | ||||
1995 | return false; | ||||
1996 | } | ||||
1997 | |||||
1998 | ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) { | ||||
1999 | |||||
2000 | auto CheckRWInstForNoSync = [&](Instruction &I) { | ||||
2001 | return AA::isNoSyncInst(A, I, *this); | ||||
2002 | }; | ||||
2003 | |||||
2004 | auto CheckForNoSync = [&](Instruction &I) { | ||||
2005 | // At this point we handled all read/write effects and they are all | ||||
2006 | // nosync, so they can be skipped. | ||||
2007 | if (I.mayReadOrWriteMemory()) | ||||
2008 | return true; | ||||
2009 | |||||
2010 | // non-convergent and readnone imply nosync. | ||||
2011 | return !cast<CallBase>(I).isConvergent(); | ||||
2012 | }; | ||||
2013 | |||||
2014 | bool UsedAssumedInformation = false; | ||||
2015 | if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this, | ||||
2016 | UsedAssumedInformation) || | ||||
2017 | !A.checkForAllCallLikeInstructions(CheckForNoSync, *this, | ||||
2018 | UsedAssumedInformation)) | ||||
2019 | return indicatePessimisticFixpoint(); | ||||
2020 | |||||
2021 | return ChangeStatus::UNCHANGED; | ||||
2022 | } | ||||
2023 | |||||
2024 | struct AANoSyncFunction final : public AANoSyncImpl { | ||||
2025 | AANoSyncFunction(const IRPosition &IRP, Attributor &A) | ||||
2026 | : AANoSyncImpl(IRP, A) {} | ||||
2027 | |||||
2028 | /// See AbstractAttribute::trackStatistics() | ||||
2029 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync){ static llvm::Statistic NumIRFunction_nosync = {"attributor" , "NumIRFunction_nosync", ("Number of " "functions" " marked '" "nosync" "'")};; ++(NumIRFunction_nosync); } } | ||||
2030 | }; | ||||
2031 | |||||
2032 | /// NoSync attribute deduction for a call sites. | ||||
2033 | struct AANoSyncCallSite final : AANoSyncImpl { | ||||
2034 | AANoSyncCallSite(const IRPosition &IRP, Attributor &A) | ||||
2035 | : AANoSyncImpl(IRP, A) {} | ||||
2036 | |||||
2037 | /// See AbstractAttribute::initialize(...). | ||||
2038 | void initialize(Attributor &A) override { | ||||
2039 | AANoSyncImpl::initialize(A); | ||||
2040 | Function *F = getAssociatedFunction(); | ||||
2041 | if (!F || F->isDeclaration()) | ||||
2042 | indicatePessimisticFixpoint(); | ||||
2043 | } | ||||
2044 | |||||
2045 | /// See AbstractAttribute::updateImpl(...). | ||||
2046 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2047 | // TODO: Once we have call site specific value information we can provide | ||||
2048 | // call site specific liveness information and then it makes | ||||
2049 | // sense to specialize attributes for call sites arguments instead of | ||||
2050 | // redirecting requests to the callee argument. | ||||
2051 | Function *F = getAssociatedFunction(); | ||||
2052 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
2053 | auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos, DepClassTy::REQUIRED); | ||||
2054 | return clampStateAndIndicateChange(getState(), FnAA.getState()); | ||||
2055 | } | ||||
2056 | |||||
2057 | /// See AbstractAttribute::trackStatistics() | ||||
2058 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync){ static llvm::Statistic NumIRCS_nosync = {"attributor", "NumIRCS_nosync" , ("Number of " "call site" " marked '" "nosync" "'")};; ++(NumIRCS_nosync ); }; } | ||||
2059 | }; | ||||
2060 | |||||
2061 | /// ------------------------ No-Free Attributes ---------------------------- | ||||
2062 | |||||
2063 | struct AANoFreeImpl : public AANoFree { | ||||
2064 | AANoFreeImpl(const IRPosition &IRP, Attributor &A) : AANoFree(IRP, A) {} | ||||
2065 | |||||
2066 | /// See AbstractAttribute::updateImpl(...). | ||||
2067 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2068 | auto CheckForNoFree = [&](Instruction &I) { | ||||
2069 | const auto &CB = cast<CallBase>(I); | ||||
2070 | if (CB.hasFnAttr(Attribute::NoFree)) | ||||
2071 | return true; | ||||
2072 | |||||
2073 | const auto &NoFreeAA = A.getAAFor<AANoFree>( | ||||
2074 | *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED); | ||||
2075 | return NoFreeAA.isAssumedNoFree(); | ||||
2076 | }; | ||||
2077 | |||||
2078 | bool UsedAssumedInformation = false; | ||||
2079 | if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this, | ||||
2080 | UsedAssumedInformation)) | ||||
2081 | return indicatePessimisticFixpoint(); | ||||
2082 | return ChangeStatus::UNCHANGED; | ||||
2083 | } | ||||
2084 | |||||
2085 | /// See AbstractAttribute::getAsStr(). | ||||
2086 | const std::string getAsStr() const override { | ||||
2087 | return getAssumed() ? "nofree" : "may-free"; | ||||
2088 | } | ||||
2089 | }; | ||||
2090 | |||||
2091 | struct AANoFreeFunction final : public AANoFreeImpl { | ||||
2092 | AANoFreeFunction(const IRPosition &IRP, Attributor &A) | ||||
2093 | : AANoFreeImpl(IRP, A) {} | ||||
2094 | |||||
2095 | /// See AbstractAttribute::trackStatistics() | ||||
2096 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree){ static llvm::Statistic NumIRFunction_nofree = {"attributor" , "NumIRFunction_nofree", ("Number of " "functions" " marked '" "nofree" "'")};; ++(NumIRFunction_nofree); } } | ||||
2097 | }; | ||||
2098 | |||||
2099 | /// NoFree attribute deduction for a call sites. | ||||
2100 | struct AANoFreeCallSite final : AANoFreeImpl { | ||||
2101 | AANoFreeCallSite(const IRPosition &IRP, Attributor &A) | ||||
2102 | : AANoFreeImpl(IRP, A) {} | ||||
2103 | |||||
2104 | /// See AbstractAttribute::initialize(...). | ||||
2105 | void initialize(Attributor &A) override { | ||||
2106 | AANoFreeImpl::initialize(A); | ||||
2107 | Function *F = getAssociatedFunction(); | ||||
2108 | if (!F || F->isDeclaration()) | ||||
2109 | indicatePessimisticFixpoint(); | ||||
2110 | } | ||||
2111 | |||||
2112 | /// See AbstractAttribute::updateImpl(...). | ||||
2113 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2114 | // TODO: Once we have call site specific value information we can provide | ||||
2115 | // call site specific liveness information and then it makes | ||||
2116 | // sense to specialize attributes for call sites arguments instead of | ||||
2117 | // redirecting requests to the callee argument. | ||||
2118 | Function *F = getAssociatedFunction(); | ||||
2119 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
2120 | auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos, DepClassTy::REQUIRED); | ||||
2121 | return clampStateAndIndicateChange(getState(), FnAA.getState()); | ||||
2122 | } | ||||
2123 | |||||
2124 | /// See AbstractAttribute::trackStatistics() | ||||
2125 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree){ static llvm::Statistic NumIRCS_nofree = {"attributor", "NumIRCS_nofree" , ("Number of " "call site" " marked '" "nofree" "'")};; ++(NumIRCS_nofree ); }; } | ||||
2126 | }; | ||||
2127 | |||||
2128 | /// NoFree attribute for floating values. | ||||
2129 | struct AANoFreeFloating : AANoFreeImpl { | ||||
2130 | AANoFreeFloating(const IRPosition &IRP, Attributor &A) | ||||
2131 | : AANoFreeImpl(IRP, A) {} | ||||
2132 | |||||
2133 | /// See AbstractAttribute::trackStatistics() | ||||
2134 | void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree){ static llvm::Statistic NumIRFloating_nofree = {"attributor" , "NumIRFloating_nofree", ("Number of floating values known to be '" "nofree" "'")};; ++(NumIRFloating_nofree); }} | ||||
2135 | |||||
2136 | /// See Abstract Attribute::updateImpl(...). | ||||
2137 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2138 | const IRPosition &IRP = getIRPosition(); | ||||
2139 | |||||
2140 | const auto &NoFreeAA = A.getAAFor<AANoFree>( | ||||
2141 | *this, IRPosition::function_scope(IRP), DepClassTy::OPTIONAL); | ||||
2142 | if (NoFreeAA.isAssumedNoFree()) | ||||
2143 | return ChangeStatus::UNCHANGED; | ||||
2144 | |||||
2145 | Value &AssociatedValue = getIRPosition().getAssociatedValue(); | ||||
2146 | auto Pred = [&](const Use &U, bool &Follow) -> bool { | ||||
2147 | Instruction *UserI = cast<Instruction>(U.getUser()); | ||||
2148 | if (auto *CB = dyn_cast<CallBase>(UserI)) { | ||||
2149 | if (CB->isBundleOperand(&U)) | ||||
2150 | return false; | ||||
2151 | if (!CB->isArgOperand(&U)) | ||||
2152 | return true; | ||||
2153 | unsigned ArgNo = CB->getArgOperandNo(&U); | ||||
2154 | |||||
2155 | const auto &NoFreeArg = A.getAAFor<AANoFree>( | ||||
2156 | *this, IRPosition::callsite_argument(*CB, ArgNo), | ||||
2157 | DepClassTy::REQUIRED); | ||||
2158 | return NoFreeArg.isAssumedNoFree(); | ||||
2159 | } | ||||
2160 | |||||
2161 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || | ||||
2162 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { | ||||
2163 | Follow = true; | ||||
2164 | return true; | ||||
2165 | } | ||||
2166 | if (isa<StoreInst>(UserI) || isa<LoadInst>(UserI) || | ||||
2167 | isa<ReturnInst>(UserI)) | ||||
2168 | return true; | ||||
2169 | |||||
2170 | // Unknown user. | ||||
2171 | return false; | ||||
2172 | }; | ||||
2173 | if (!A.checkForAllUses(Pred, *this, AssociatedValue)) | ||||
2174 | return indicatePessimisticFixpoint(); | ||||
2175 | |||||
2176 | return ChangeStatus::UNCHANGED; | ||||
2177 | } | ||||
2178 | }; | ||||
2179 | |||||
2180 | /// NoFree attribute for a call site argument. | ||||
2181 | struct AANoFreeArgument final : AANoFreeFloating { | ||||
2182 | AANoFreeArgument(const IRPosition &IRP, Attributor &A) | ||||
2183 | : AANoFreeFloating(IRP, A) {} | ||||
2184 | |||||
2185 | /// See AbstractAttribute::trackStatistics() | ||||
2186 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree){ static llvm::Statistic NumIRArguments_nofree = {"attributor" , "NumIRArguments_nofree", ("Number of " "arguments" " marked '" "nofree" "'")};; ++(NumIRArguments_nofree); } } | ||||
2187 | }; | ||||
2188 | |||||
2189 | /// NoFree attribute for call site arguments. | ||||
2190 | struct AANoFreeCallSiteArgument final : AANoFreeFloating { | ||||
2191 | AANoFreeCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
2192 | : AANoFreeFloating(IRP, A) {} | ||||
2193 | |||||
2194 | /// See AbstractAttribute::updateImpl(...). | ||||
2195 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2196 | // TODO: Once we have call site specific value information we can provide | ||||
2197 | // call site specific liveness information and then it makes | ||||
2198 | // sense to specialize attributes for call sites arguments instead of | ||||
2199 | // redirecting requests to the callee argument. | ||||
2200 | Argument *Arg = getAssociatedArgument(); | ||||
2201 | if (!Arg) | ||||
2202 | return indicatePessimisticFixpoint(); | ||||
2203 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | ||||
2204 | auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos, DepClassTy::REQUIRED); | ||||
2205 | return clampStateAndIndicateChange(getState(), ArgAA.getState()); | ||||
2206 | } | ||||
2207 | |||||
2208 | /// See AbstractAttribute::trackStatistics() | ||||
2209 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree){ static llvm::Statistic NumIRCSArguments_nofree = {"attributor" , "NumIRCSArguments_nofree", ("Number of " "call site arguments" " marked '" "nofree" "'")};; ++(NumIRCSArguments_nofree); }}; | ||||
2210 | }; | ||||
2211 | |||||
2212 | /// NoFree attribute for function return value. | ||||
2213 | struct AANoFreeReturned final : AANoFreeFloating { | ||||
2214 | AANoFreeReturned(const IRPosition &IRP, Attributor &A) | ||||
2215 | : AANoFreeFloating(IRP, A) { | ||||
2216 | llvm_unreachable("NoFree is not applicable to function returns!")::llvm::llvm_unreachable_internal("NoFree is not applicable to function returns!" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 2216); | ||||
2217 | } | ||||
2218 | |||||
2219 | /// See AbstractAttribute::initialize(...). | ||||
2220 | void initialize(Attributor &A) override { | ||||
2221 | llvm_unreachable("NoFree is not applicable to function returns!")::llvm::llvm_unreachable_internal("NoFree is not applicable to function returns!" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 2221); | ||||
2222 | } | ||||
2223 | |||||
2224 | /// See AbstractAttribute::updateImpl(...). | ||||
2225 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2226 | llvm_unreachable("NoFree is not applicable to function returns!")::llvm::llvm_unreachable_internal("NoFree is not applicable to function returns!" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 2226); | ||||
2227 | } | ||||
2228 | |||||
2229 | /// See AbstractAttribute::trackStatistics() | ||||
2230 | void trackStatistics() const override {} | ||||
2231 | }; | ||||
2232 | |||||
2233 | /// NoFree attribute deduction for a call site return value. | ||||
2234 | struct AANoFreeCallSiteReturned final : AANoFreeFloating { | ||||
2235 | AANoFreeCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
2236 | : AANoFreeFloating(IRP, A) {} | ||||
2237 | |||||
2238 | ChangeStatus manifest(Attributor &A) override { | ||||
2239 | return ChangeStatus::UNCHANGED; | ||||
2240 | } | ||||
2241 | /// See AbstractAttribute::trackStatistics() | ||||
2242 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree){ static llvm::Statistic NumIRCSReturn_nofree = {"attributor" , "NumIRCSReturn_nofree", ("Number of " "call site returns" " marked '" "nofree" "'")};; ++(NumIRCSReturn_nofree); } } | ||||
2243 | }; | ||||
2244 | |||||
2245 | /// ------------------------ NonNull Argument Attribute ------------------------ | ||||
2246 | static int64_t getKnownNonNullAndDerefBytesForUse( | ||||
2247 | Attributor &A, const AbstractAttribute &QueryingAA, Value &AssociatedValue, | ||||
2248 | const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) { | ||||
2249 | TrackUse = false; | ||||
2250 | |||||
2251 | const Value *UseV = U->get(); | ||||
2252 | if (!UseV->getType()->isPointerTy()) | ||||
2253 | return 0; | ||||
2254 | |||||
2255 | // We need to follow common pointer manipulation uses to the accesses they | ||||
2256 | // feed into. We can try to be smart to avoid looking through things we do not | ||||
2257 | // like for now, e.g., non-inbounds GEPs. | ||||
2258 | if (isa<CastInst>(I)) { | ||||
2259 | TrackUse = true; | ||||
2260 | return 0; | ||||
2261 | } | ||||
2262 | |||||
2263 | if (isa<GetElementPtrInst>(I)) { | ||||
2264 | TrackUse = true; | ||||
2265 | return 0; | ||||
2266 | } | ||||
2267 | |||||
2268 | Type *PtrTy = UseV->getType(); | ||||
2269 | const Function *F = I->getFunction(); | ||||
2270 | bool NullPointerIsDefined = | ||||
2271 | F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true; | ||||
2272 | const DataLayout &DL = A.getInfoCache().getDL(); | ||||
2273 | if (const auto *CB = dyn_cast<CallBase>(I)) { | ||||
2274 | if (CB->isBundleOperand(U)) { | ||||
2275 | if (RetainedKnowledge RK = getKnowledgeFromUse( | ||||
2276 | U, {Attribute::NonNull, Attribute::Dereferenceable})) { | ||||
2277 | IsNonNull |= | ||||
2278 | (RK.AttrKind == Attribute::NonNull || !NullPointerIsDefined); | ||||
2279 | return RK.ArgValue; | ||||
2280 | } | ||||
2281 | return 0; | ||||
2282 | } | ||||
2283 | |||||
2284 | if (CB->isCallee(U)) { | ||||
2285 | IsNonNull |= !NullPointerIsDefined; | ||||
2286 | return 0; | ||||
2287 | } | ||||
2288 | |||||
2289 | unsigned ArgNo = CB->getArgOperandNo(U); | ||||
2290 | IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo); | ||||
2291 | // As long as we only use known information there is no need to track | ||||
2292 | // dependences here. | ||||
2293 | auto &DerefAA = | ||||
2294 | A.getAAFor<AADereferenceable>(QueryingAA, IRP, DepClassTy::NONE); | ||||
2295 | IsNonNull |= DerefAA.isKnownNonNull(); | ||||
2296 | return DerefAA.getKnownDereferenceableBytes(); | ||||
2297 | } | ||||
2298 | |||||
2299 | Optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I); | ||||
2300 | if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile()) | ||||
2301 | return 0; | ||||
2302 | |||||
2303 | int64_t Offset; | ||||
2304 | const Value *Base = | ||||
2305 | getMinimalBaseOfPointer(A, QueryingAA, Loc->Ptr, Offset, DL); | ||||
2306 | if (Base && Base == &AssociatedValue) { | ||||
2307 | int64_t DerefBytes = Loc->Size.getValue() + Offset; | ||||
2308 | IsNonNull |= !NullPointerIsDefined; | ||||
2309 | return std::max(int64_t(0), DerefBytes); | ||||
2310 | } | ||||
2311 | |||||
2312 | /// Corner case when an offset is 0. | ||||
2313 | Base = GetPointerBaseWithConstantOffset(Loc->Ptr, Offset, DL, | ||||
2314 | /*AllowNonInbounds*/ true); | ||||
2315 | if (Base && Base == &AssociatedValue && Offset == 0) { | ||||
2316 | int64_t DerefBytes = Loc->Size.getValue(); | ||||
2317 | IsNonNull |= !NullPointerIsDefined; | ||||
2318 | return std::max(int64_t(0), DerefBytes); | ||||
2319 | } | ||||
2320 | |||||
2321 | return 0; | ||||
2322 | } | ||||
2323 | |||||
2324 | struct AANonNullImpl : AANonNull { | ||||
2325 | AANonNullImpl(const IRPosition &IRP, Attributor &A) | ||||
2326 | : AANonNull(IRP, A), | ||||
2327 | NullIsDefined(NullPointerIsDefined( | ||||
2328 | getAnchorScope(), | ||||
2329 | getAssociatedValue().getType()->getPointerAddressSpace())) {} | ||||
2330 | |||||
2331 | /// See AbstractAttribute::initialize(...). | ||||
2332 | void initialize(Attributor &A) override { | ||||
2333 | Value &V = getAssociatedValue(); | ||||
2334 | if (!NullIsDefined && | ||||
2335 | hasAttr({Attribute::NonNull, Attribute::Dereferenceable}, | ||||
2336 | /* IgnoreSubsumingPositions */ false, &A)) { | ||||
2337 | indicateOptimisticFixpoint(); | ||||
2338 | return; | ||||
2339 | } | ||||
2340 | |||||
2341 | if (isa<ConstantPointerNull>(V)) { | ||||
2342 | indicatePessimisticFixpoint(); | ||||
2343 | return; | ||||
2344 | } | ||||
2345 | |||||
2346 | AANonNull::initialize(A); | ||||
2347 | |||||
2348 | bool CanBeNull, CanBeFreed; | ||||
2349 | if (V.getPointerDereferenceableBytes(A.getDataLayout(), CanBeNull, | ||||
2350 | CanBeFreed)) { | ||||
2351 | if (!CanBeNull) { | ||||
2352 | indicateOptimisticFixpoint(); | ||||
2353 | return; | ||||
2354 | } | ||||
2355 | } | ||||
2356 | |||||
2357 | if (isa<GlobalValue>(&getAssociatedValue())) { | ||||
2358 | indicatePessimisticFixpoint(); | ||||
2359 | return; | ||||
2360 | } | ||||
2361 | |||||
2362 | if (Instruction *CtxI = getCtxI()) | ||||
2363 | followUsesInMBEC(*this, A, getState(), *CtxI); | ||||
2364 | } | ||||
2365 | |||||
2366 | /// See followUsesInMBEC | ||||
2367 | bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I, | ||||
2368 | AANonNull::StateType &State) { | ||||
2369 | bool IsNonNull = false; | ||||
2370 | bool TrackUse = false; | ||||
2371 | getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I, | ||||
2372 | IsNonNull, TrackUse); | ||||
2373 | State.setKnown(IsNonNull); | ||||
2374 | return TrackUse; | ||||
2375 | } | ||||
2376 | |||||
2377 | /// See AbstractAttribute::getAsStr(). | ||||
2378 | const std::string getAsStr() const override { | ||||
2379 | return getAssumed() ? "nonnull" : "may-null"; | ||||
2380 | } | ||||
2381 | |||||
2382 | /// Flag to determine if the underlying value can be null and still allow | ||||
2383 | /// valid accesses. | ||||
2384 | const bool NullIsDefined; | ||||
2385 | }; | ||||
2386 | |||||
2387 | /// NonNull attribute for a floating value. | ||||
2388 | struct AANonNullFloating : public AANonNullImpl { | ||||
2389 | AANonNullFloating(const IRPosition &IRP, Attributor &A) | ||||
2390 | : AANonNullImpl(IRP, A) {} | ||||
2391 | |||||
2392 | /// See AbstractAttribute::updateImpl(...). | ||||
2393 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2394 | const DataLayout &DL = A.getDataLayout(); | ||||
2395 | |||||
2396 | DominatorTree *DT = nullptr; | ||||
2397 | AssumptionCache *AC = nullptr; | ||||
2398 | InformationCache &InfoCache = A.getInfoCache(); | ||||
2399 | if (const Function *Fn = getAnchorScope()) { | ||||
2400 | DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn); | ||||
2401 | AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*Fn); | ||||
2402 | } | ||||
2403 | |||||
2404 | auto VisitValueCB = [&](Value &V, const Instruction *CtxI, | ||||
2405 | AANonNull::StateType &T, bool Stripped) -> bool { | ||||
2406 | const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V), | ||||
2407 | DepClassTy::REQUIRED); | ||||
2408 | if (!Stripped && this == &AA) { | ||||
2409 | if (!isKnownNonZero(&V, DL, 0, AC, CtxI, DT)) | ||||
2410 | T.indicatePessimisticFixpoint(); | ||||
2411 | } else { | ||||
2412 | // Use abstract attribute information. | ||||
2413 | const AANonNull::StateType &NS = AA.getState(); | ||||
2414 | T ^= NS; | ||||
2415 | } | ||||
2416 | return T.isValidState(); | ||||
2417 | }; | ||||
2418 | |||||
2419 | StateType T; | ||||
2420 | bool UsedAssumedInformation = false; | ||||
2421 | if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T, | ||||
2422 | VisitValueCB, getCtxI(), | ||||
2423 | UsedAssumedInformation)) | ||||
2424 | return indicatePessimisticFixpoint(); | ||||
2425 | |||||
2426 | return clampStateAndIndicateChange(getState(), T); | ||||
2427 | } | ||||
2428 | |||||
2429 | /// See AbstractAttribute::trackStatistics() | ||||
2430 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull){ static llvm::Statistic NumIRFunctionReturn_nonnull = {"attributor" , "NumIRFunctionReturn_nonnull", ("Number of " "function returns" " marked '" "nonnull" "'")};; ++(NumIRFunctionReturn_nonnull ); } } | ||||
2431 | }; | ||||
2432 | |||||
2433 | /// NonNull attribute for function return value. | ||||
2434 | struct AANonNullReturned final | ||||
2435 | : AAReturnedFromReturnedValues<AANonNull, AANonNull> { | ||||
2436 | AANonNullReturned(const IRPosition &IRP, Attributor &A) | ||||
2437 | : AAReturnedFromReturnedValues<AANonNull, AANonNull>(IRP, A) {} | ||||
2438 | |||||
2439 | /// See AbstractAttribute::getAsStr(). | ||||
2440 | const std::string getAsStr() const override { | ||||
2441 | return getAssumed() ? "nonnull" : "may-null"; | ||||
2442 | } | ||||
2443 | |||||
2444 | /// See AbstractAttribute::trackStatistics() | ||||
2445 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull){ static llvm::Statistic NumIRFunctionReturn_nonnull = {"attributor" , "NumIRFunctionReturn_nonnull", ("Number of " "function returns" " marked '" "nonnull" "'")};; ++(NumIRFunctionReturn_nonnull ); } } | ||||
2446 | }; | ||||
2447 | |||||
2448 | /// NonNull attribute for function argument. | ||||
2449 | struct AANonNullArgument final | ||||
2450 | : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> { | ||||
2451 | AANonNullArgument(const IRPosition &IRP, Attributor &A) | ||||
2452 | : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP, A) {} | ||||
2453 | |||||
2454 | /// See AbstractAttribute::trackStatistics() | ||||
2455 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull){ static llvm::Statistic NumIRArguments_nonnull = {"attributor" , "NumIRArguments_nonnull", ("Number of " "arguments" " marked '" "nonnull" "'")};; ++(NumIRArguments_nonnull); } } | ||||
2456 | }; | ||||
2457 | |||||
2458 | struct AANonNullCallSiteArgument final : AANonNullFloating { | ||||
2459 | AANonNullCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
2460 | : AANonNullFloating(IRP, A) {} | ||||
2461 | |||||
2462 | /// See AbstractAttribute::trackStatistics() | ||||
2463 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull){ static llvm::Statistic NumIRCSArguments_nonnull = {"attributor" , "NumIRCSArguments_nonnull", ("Number of " "call site arguments" " marked '" "nonnull" "'")};; ++(NumIRCSArguments_nonnull); } } | ||||
2464 | }; | ||||
2465 | |||||
2466 | /// NonNull attribute for a call site return position. | ||||
2467 | struct AANonNullCallSiteReturned final | ||||
2468 | : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> { | ||||
2469 | AANonNullCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
2470 | : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP, A) {} | ||||
2471 | |||||
2472 | /// See AbstractAttribute::trackStatistics() | ||||
2473 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull){ static llvm::Statistic NumIRCSReturn_nonnull = {"attributor" , "NumIRCSReturn_nonnull", ("Number of " "call site returns" " marked '" "nonnull" "'")};; ++(NumIRCSReturn_nonnull); } } | ||||
2474 | }; | ||||
2475 | |||||
2476 | /// ------------------------ No-Recurse Attributes ---------------------------- | ||||
2477 | |||||
2478 | struct AANoRecurseImpl : public AANoRecurse { | ||||
2479 | AANoRecurseImpl(const IRPosition &IRP, Attributor &A) : AANoRecurse(IRP, A) {} | ||||
2480 | |||||
2481 | /// See AbstractAttribute::getAsStr() | ||||
2482 | const std::string getAsStr() const override { | ||||
2483 | return getAssumed() ? "norecurse" : "may-recurse"; | ||||
2484 | } | ||||
2485 | }; | ||||
2486 | |||||
2487 | struct AANoRecurseFunction final : AANoRecurseImpl { | ||||
2488 | AANoRecurseFunction(const IRPosition &IRP, Attributor &A) | ||||
2489 | : AANoRecurseImpl(IRP, A) {} | ||||
2490 | |||||
2491 | /// See AbstractAttribute::updateImpl(...). | ||||
2492 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2493 | |||||
2494 | // If all live call sites are known to be no-recurse, we are as well. | ||||
2495 | auto CallSitePred = [&](AbstractCallSite ACS) { | ||||
2496 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>( | ||||
2497 | *this, IRPosition::function(*ACS.getInstruction()->getFunction()), | ||||
2498 | DepClassTy::NONE); | ||||
2499 | return NoRecurseAA.isKnownNoRecurse(); | ||||
2500 | }; | ||||
2501 | bool UsedAssumedInformation = false; | ||||
2502 | if (A.checkForAllCallSites(CallSitePred, *this, true, | ||||
2503 | UsedAssumedInformation)) { | ||||
2504 | // If we know all call sites and all are known no-recurse, we are done. | ||||
2505 | // If all known call sites, which might not be all that exist, are known | ||||
2506 | // to be no-recurse, we are not done but we can continue to assume | ||||
2507 | // no-recurse. If one of the call sites we have not visited will become | ||||
2508 | // live, another update is triggered. | ||||
2509 | if (!UsedAssumedInformation) | ||||
2510 | indicateOptimisticFixpoint(); | ||||
2511 | return ChangeStatus::UNCHANGED; | ||||
2512 | } | ||||
2513 | |||||
2514 | const AAFunctionReachability &EdgeReachability = | ||||
2515 | A.getAAFor<AAFunctionReachability>(*this, getIRPosition(), | ||||
2516 | DepClassTy::REQUIRED); | ||||
2517 | if (EdgeReachability.canReach(A, *getAnchorScope())) | ||||
2518 | return indicatePessimisticFixpoint(); | ||||
2519 | return ChangeStatus::UNCHANGED; | ||||
2520 | } | ||||
2521 | |||||
2522 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse){ static llvm::Statistic NumIRFunction_norecurse = {"attributor" , "NumIRFunction_norecurse", ("Number of " "functions" " marked '" "norecurse" "'")};; ++(NumIRFunction_norecurse); } } | ||||
2523 | }; | ||||
2524 | |||||
2525 | /// NoRecurse attribute deduction for a call sites. | ||||
2526 | struct AANoRecurseCallSite final : AANoRecurseImpl { | ||||
2527 | AANoRecurseCallSite(const IRPosition &IRP, Attributor &A) | ||||
2528 | : AANoRecurseImpl(IRP, A) {} | ||||
2529 | |||||
2530 | /// See AbstractAttribute::initialize(...). | ||||
2531 | void initialize(Attributor &A) override { | ||||
2532 | AANoRecurseImpl::initialize(A); | ||||
2533 | Function *F = getAssociatedFunction(); | ||||
2534 | if (!F || F->isDeclaration()) | ||||
2535 | indicatePessimisticFixpoint(); | ||||
2536 | } | ||||
2537 | |||||
2538 | /// See AbstractAttribute::updateImpl(...). | ||||
2539 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2540 | // TODO: Once we have call site specific value information we can provide | ||||
2541 | // call site specific liveness information and then it makes | ||||
2542 | // sense to specialize attributes for call sites arguments instead of | ||||
2543 | // redirecting requests to the callee argument. | ||||
2544 | Function *F = getAssociatedFunction(); | ||||
2545 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
2546 | auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos, DepClassTy::REQUIRED); | ||||
2547 | return clampStateAndIndicateChange(getState(), FnAA.getState()); | ||||
2548 | } | ||||
2549 | |||||
2550 | /// See AbstractAttribute::trackStatistics() | ||||
2551 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse){ static llvm::Statistic NumIRCS_norecurse = {"attributor", "NumIRCS_norecurse" , ("Number of " "call site" " marked '" "norecurse" "'")};; ++ (NumIRCS_norecurse); }; } | ||||
2552 | }; | ||||
2553 | |||||
2554 | /// -------------------- Undefined-Behavior Attributes ------------------------ | ||||
2555 | |||||
2556 | struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior { | ||||
2557 | AAUndefinedBehaviorImpl(const IRPosition &IRP, Attributor &A) | ||||
2558 | : AAUndefinedBehavior(IRP, A) {} | ||||
2559 | |||||
2560 | /// See AbstractAttribute::updateImpl(...). | ||||
2561 | // through a pointer (i.e. also branches etc.) | ||||
2562 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2563 | const size_t UBPrevSize = KnownUBInsts.size(); | ||||
2564 | const size_t NoUBPrevSize = AssumedNoUBInsts.size(); | ||||
2565 | |||||
2566 | auto InspectMemAccessInstForUB = [&](Instruction &I) { | ||||
2567 | // Lang ref now states volatile store is not UB, let's skip them. | ||||
2568 | if (I.isVolatile() && I.mayWriteToMemory()) | ||||
2569 | return true; | ||||
2570 | |||||
2571 | // Skip instructions that are already saved. | ||||
2572 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) | ||||
2573 | return true; | ||||
2574 | |||||
2575 | // If we reach here, we know we have an instruction | ||||
2576 | // that accesses memory through a pointer operand, | ||||
2577 | // for which getPointerOperand() should give it to us. | ||||
2578 | Value *PtrOp = | ||||
2579 | const_cast<Value *>(getPointerOperand(&I, /* AllowVolatile */ true)); | ||||
2580 | assert(PtrOp &&(static_cast <bool> (PtrOp && "Expected pointer operand of memory accessing instruction" ) ? void (0) : __assert_fail ("PtrOp && \"Expected pointer operand of memory accessing instruction\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 2581, __extension__ __PRETTY_FUNCTION__)) | ||||
2581 | "Expected pointer operand of memory accessing instruction")(static_cast <bool> (PtrOp && "Expected pointer operand of memory accessing instruction" ) ? void (0) : __assert_fail ("PtrOp && \"Expected pointer operand of memory accessing instruction\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 2581, __extension__ __PRETTY_FUNCTION__)); | ||||
2582 | |||||
2583 | // Either we stopped and the appropriate action was taken, | ||||
2584 | // or we got back a simplified value to continue. | ||||
2585 | Optional<Value *> SimplifiedPtrOp = stopOnUndefOrAssumed(A, PtrOp, &I); | ||||
2586 | if (!SimplifiedPtrOp.hasValue() || !SimplifiedPtrOp.getValue()) | ||||
2587 | return true; | ||||
2588 | const Value *PtrOpVal = SimplifiedPtrOp.getValue(); | ||||
2589 | |||||
2590 | // A memory access through a pointer is considered UB | ||||
2591 | // only if the pointer has constant null value. | ||||
2592 | // TODO: Expand it to not only check constant values. | ||||
2593 | if (!isa<ConstantPointerNull>(PtrOpVal)) { | ||||
2594 | AssumedNoUBInsts.insert(&I); | ||||
2595 | return true; | ||||
2596 | } | ||||
2597 | const Type *PtrTy = PtrOpVal->getType(); | ||||
2598 | |||||
2599 | // Because we only consider instructions inside functions, | ||||
2600 | // assume that a parent function exists. | ||||
2601 | const Function *F = I.getFunction(); | ||||
2602 | |||||
2603 | // A memory access using constant null pointer is only considered UB | ||||
2604 | // if null pointer is _not_ defined for the target platform. | ||||
2605 | if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace())) | ||||
2606 | AssumedNoUBInsts.insert(&I); | ||||
2607 | else | ||||
2608 | KnownUBInsts.insert(&I); | ||||
2609 | return true; | ||||
2610 | }; | ||||
2611 | |||||
2612 | auto InspectBrInstForUB = [&](Instruction &I) { | ||||
2613 | // A conditional branch instruction is considered UB if it has `undef` | ||||
2614 | // condition. | ||||
2615 | |||||
2616 | // Skip instructions that are already saved. | ||||
2617 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) | ||||
2618 | return true; | ||||
2619 | |||||
2620 | // We know we have a branch instruction. | ||||
2621 | auto *BrInst = cast<BranchInst>(&I); | ||||
2622 | |||||
2623 | // Unconditional branches are never considered UB. | ||||
2624 | if (BrInst->isUnconditional()) | ||||
2625 | return true; | ||||
2626 | |||||
2627 | // Either we stopped and the appropriate action was taken, | ||||
2628 | // or we got back a simplified value to continue. | ||||
2629 | Optional<Value *> SimplifiedCond = | ||||
2630 | stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst); | ||||
2631 | if (!SimplifiedCond.hasValue() || !SimplifiedCond.getValue()) | ||||
2632 | return true; | ||||
2633 | AssumedNoUBInsts.insert(&I); | ||||
2634 | return true; | ||||
2635 | }; | ||||
2636 | |||||
2637 | auto InspectCallSiteForUB = [&](Instruction &I) { | ||||
2638 | // Check whether a callsite always cause UB or not | ||||
2639 | |||||
2640 | // Skip instructions that are already saved. | ||||
2641 | if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I)) | ||||
2642 | return true; | ||||
2643 | |||||
2644 | // Check nonnull and noundef argument attribute violation for each | ||||
2645 | // callsite. | ||||
2646 | CallBase &CB = cast<CallBase>(I); | ||||
2647 | Function *Callee = CB.getCalledFunction(); | ||||
2648 | if (!Callee) | ||||
2649 | return true; | ||||
2650 | for (unsigned idx = 0; idx < CB.arg_size(); idx++) { | ||||
2651 | // If current argument is known to be simplified to null pointer and the | ||||
2652 | // corresponding argument position is known to have nonnull attribute, | ||||
2653 | // the argument is poison. Furthermore, if the argument is poison and | ||||
2654 | // the position is known to have noundef attriubte, this callsite is | ||||
2655 | // considered UB. | ||||
2656 | if (idx >= Callee->arg_size()) | ||||
2657 | break; | ||||
2658 | Value *ArgVal = CB.getArgOperand(idx); | ||||
2659 | if (!ArgVal) | ||||
2660 | continue; | ||||
2661 | // Here, we handle three cases. | ||||
2662 | // (1) Not having a value means it is dead. (we can replace the value | ||||
2663 | // with undef) | ||||
2664 | // (2) Simplified to undef. The argument violate noundef attriubte. | ||||
2665 | // (3) Simplified to null pointer where known to be nonnull. | ||||
2666 | // The argument is a poison value and violate noundef attribute. | ||||
2667 | IRPosition CalleeArgumentIRP = IRPosition::callsite_argument(CB, idx); | ||||
2668 | auto &NoUndefAA = | ||||
2669 | A.getAAFor<AANoUndef>(*this, CalleeArgumentIRP, DepClassTy::NONE); | ||||
2670 | if (!NoUndefAA.isKnownNoUndef()) | ||||
2671 | continue; | ||||
2672 | bool UsedAssumedInformation = false; | ||||
2673 | Optional<Value *> SimplifiedVal = A.getAssumedSimplified( | ||||
2674 | IRPosition::value(*ArgVal), *this, UsedAssumedInformation); | ||||
2675 | if (UsedAssumedInformation) | ||||
2676 | continue; | ||||
2677 | if (SimplifiedVal.hasValue() && !SimplifiedVal.getValue()) | ||||
2678 | return true; | ||||
2679 | if (!SimplifiedVal.hasValue() || | ||||
2680 | isa<UndefValue>(*SimplifiedVal.getValue())) { | ||||
2681 | KnownUBInsts.insert(&I); | ||||
2682 | continue; | ||||
2683 | } | ||||
2684 | if (!ArgVal->getType()->isPointerTy() || | ||||
2685 | !isa<ConstantPointerNull>(*SimplifiedVal.getValue())) | ||||
2686 | continue; | ||||
2687 | auto &NonNullAA = | ||||
2688 | A.getAAFor<AANonNull>(*this, CalleeArgumentIRP, DepClassTy::NONE); | ||||
2689 | if (NonNullAA.isKnownNonNull()) | ||||
2690 | KnownUBInsts.insert(&I); | ||||
2691 | } | ||||
2692 | return true; | ||||
2693 | }; | ||||
2694 | |||||
2695 | auto InspectReturnInstForUB = [&](Instruction &I) { | ||||
2696 | auto &RI = cast<ReturnInst>(I); | ||||
2697 | // Either we stopped and the appropriate action was taken, | ||||
2698 | // or we got back a simplified return value to continue. | ||||
2699 | Optional<Value *> SimplifiedRetValue = | ||||
2700 | stopOnUndefOrAssumed(A, RI.getReturnValue(), &I); | ||||
2701 | if (!SimplifiedRetValue.hasValue() || !SimplifiedRetValue.getValue()) | ||||
2702 | return true; | ||||
2703 | |||||
2704 | // Check if a return instruction always cause UB or not | ||||
2705 | // Note: It is guaranteed that the returned position of the anchor | ||||
2706 | // scope has noundef attribute when this is called. | ||||
2707 | // We also ensure the return position is not "assumed dead" | ||||
2708 | // because the returned value was then potentially simplified to | ||||
2709 | // `undef` in AAReturnedValues without removing the `noundef` | ||||
2710 | // attribute yet. | ||||
2711 | |||||
2712 | // When the returned position has noundef attriubte, UB occurs in the | ||||
2713 | // following cases. | ||||
2714 | // (1) Returned value is known to be undef. | ||||
2715 | // (2) The value is known to be a null pointer and the returned | ||||
2716 | // position has nonnull attribute (because the returned value is | ||||
2717 | // poison). | ||||
2718 | if (isa<ConstantPointerNull>(*SimplifiedRetValue)) { | ||||
2719 | auto &NonNullAA = A.getAAFor<AANonNull>( | ||||
2720 | *this, IRPosition::returned(*getAnchorScope()), DepClassTy::NONE); | ||||
2721 | if (NonNullAA.isKnownNonNull()) | ||||
2722 | KnownUBInsts.insert(&I); | ||||
2723 | } | ||||
2724 | |||||
2725 | return true; | ||||
2726 | }; | ||||
2727 | |||||
2728 | bool UsedAssumedInformation = false; | ||||
2729 | A.checkForAllInstructions(InspectMemAccessInstForUB, *this, | ||||
2730 | {Instruction::Load, Instruction::Store, | ||||
2731 | Instruction::AtomicCmpXchg, | ||||
2732 | Instruction::AtomicRMW}, | ||||
2733 | UsedAssumedInformation, | ||||
2734 | /* CheckBBLivenessOnly */ true); | ||||
2735 | A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br}, | ||||
2736 | UsedAssumedInformation, | ||||
2737 | /* CheckBBLivenessOnly */ true); | ||||
2738 | A.checkForAllCallLikeInstructions(InspectCallSiteForUB, *this, | ||||
2739 | UsedAssumedInformation); | ||||
2740 | |||||
2741 | // If the returned position of the anchor scope has noundef attriubte, check | ||||
2742 | // all returned instructions. | ||||
2743 | if (!getAnchorScope()->getReturnType()->isVoidTy()) { | ||||
2744 | const IRPosition &ReturnIRP = IRPosition::returned(*getAnchorScope()); | ||||
2745 | if (!A.isAssumedDead(ReturnIRP, this, nullptr, UsedAssumedInformation)) { | ||||
2746 | auto &RetPosNoUndefAA = | ||||
2747 | A.getAAFor<AANoUndef>(*this, ReturnIRP, DepClassTy::NONE); | ||||
2748 | if (RetPosNoUndefAA.isKnownNoUndef()) | ||||
2749 | A.checkForAllInstructions(InspectReturnInstForUB, *this, | ||||
2750 | {Instruction::Ret}, UsedAssumedInformation, | ||||
2751 | /* CheckBBLivenessOnly */ true); | ||||
2752 | } | ||||
2753 | } | ||||
2754 | |||||
2755 | if (NoUBPrevSize != AssumedNoUBInsts.size() || | ||||
2756 | UBPrevSize != KnownUBInsts.size()) | ||||
2757 | return ChangeStatus::CHANGED; | ||||
2758 | return ChangeStatus::UNCHANGED; | ||||
2759 | } | ||||
2760 | |||||
2761 | bool isKnownToCauseUB(Instruction *I) const override { | ||||
2762 | return KnownUBInsts.count(I); | ||||
2763 | } | ||||
2764 | |||||
2765 | bool isAssumedToCauseUB(Instruction *I) const override { | ||||
2766 | // In simple words, if an instruction is not in the assumed to _not_ | ||||
2767 | // cause UB, then it is assumed UB (that includes those | ||||
2768 | // in the KnownUBInsts set). The rest is boilerplate | ||||
2769 | // is to ensure that it is one of the instructions we test | ||||
2770 | // for UB. | ||||
2771 | |||||
2772 | switch (I->getOpcode()) { | ||||
2773 | case Instruction::Load: | ||||
2774 | case Instruction::Store: | ||||
2775 | case Instruction::AtomicCmpXchg: | ||||
2776 | case Instruction::AtomicRMW: | ||||
2777 | return !AssumedNoUBInsts.count(I); | ||||
2778 | case Instruction::Br: { | ||||
2779 | auto BrInst = cast<BranchInst>(I); | ||||
2780 | if (BrInst->isUnconditional()) | ||||
2781 | return false; | ||||
2782 | return !AssumedNoUBInsts.count(I); | ||||
2783 | } break; | ||||
2784 | default: | ||||
2785 | return false; | ||||
2786 | } | ||||
2787 | return false; | ||||
2788 | } | ||||
2789 | |||||
2790 | ChangeStatus manifest(Attributor &A) override { | ||||
2791 | if (KnownUBInsts.empty()) | ||||
2792 | return ChangeStatus::UNCHANGED; | ||||
2793 | for (Instruction *I : KnownUBInsts) | ||||
2794 | A.changeToUnreachableAfterManifest(I); | ||||
2795 | return ChangeStatus::CHANGED; | ||||
2796 | } | ||||
2797 | |||||
2798 | /// See AbstractAttribute::getAsStr() | ||||
2799 | const std::string getAsStr() const override { | ||||
2800 | return getAssumed() ? "undefined-behavior" : "no-ub"; | ||||
2801 | } | ||||
2802 | |||||
2803 | /// Note: The correctness of this analysis depends on the fact that the | ||||
2804 | /// following 2 sets will stop changing after some point. | ||||
2805 | /// "Change" here means that their size changes. | ||||
2806 | /// The size of each set is monotonically increasing | ||||
2807 | /// (we only add items to them) and it is upper bounded by the number of | ||||
2808 | /// instructions in the processed function (we can never save more | ||||
2809 | /// elements in either set than this number). Hence, at some point, | ||||
2810 | /// they will stop increasing. | ||||
2811 | /// Consequently, at some point, both sets will have stopped | ||||
2812 | /// changing, effectively making the analysis reach a fixpoint. | ||||
2813 | |||||
2814 | /// Note: These 2 sets are disjoint and an instruction can be considered | ||||
2815 | /// one of 3 things: | ||||
2816 | /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in | ||||
2817 | /// the KnownUBInsts set. | ||||
2818 | /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior | ||||
2819 | /// has a reason to assume it). | ||||
2820 | /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior | ||||
2821 | /// could not find a reason to assume or prove that it can cause UB, | ||||
2822 | /// hence it assumes it doesn't. We have a set for these instructions | ||||
2823 | /// so that we don't reprocess them in every update. | ||||
2824 | /// Note however that instructions in this set may cause UB. | ||||
2825 | |||||
2826 | protected: | ||||
2827 | /// A set of all live instructions _known_ to cause UB. | ||||
2828 | SmallPtrSet<Instruction *, 8> KnownUBInsts; | ||||
2829 | |||||
2830 | private: | ||||
2831 | /// A set of all the (live) instructions that are assumed to _not_ cause UB. | ||||
2832 | SmallPtrSet<Instruction *, 8> AssumedNoUBInsts; | ||||
2833 | |||||
2834 | // Should be called on updates in which if we're processing an instruction | ||||
2835 | // \p I that depends on a value \p V, one of the following has to happen: | ||||
2836 | // - If the value is assumed, then stop. | ||||
2837 | // - If the value is known but undef, then consider it UB. | ||||
2838 | // - Otherwise, do specific processing with the simplified value. | ||||
2839 | // We return None in the first 2 cases to signify that an appropriate | ||||
2840 | // action was taken and the caller should stop. | ||||
2841 | // Otherwise, we return the simplified value that the caller should | ||||
2842 | // use for specific processing. | ||||
2843 | Optional<Value *> stopOnUndefOrAssumed(Attributor &A, Value *V, | ||||
2844 | Instruction *I) { | ||||
2845 | bool UsedAssumedInformation = false; | ||||
2846 | Optional<Value *> SimplifiedV = A.getAssumedSimplified( | ||||
2847 | IRPosition::value(*V), *this, UsedAssumedInformation); | ||||
2848 | if (!UsedAssumedInformation) { | ||||
2849 | // Don't depend on assumed values. | ||||
2850 | if (!SimplifiedV.hasValue()) { | ||||
2851 | // If it is known (which we tested above) but it doesn't have a value, | ||||
2852 | // then we can assume `undef` and hence the instruction is UB. | ||||
2853 | KnownUBInsts.insert(I); | ||||
2854 | return llvm::None; | ||||
2855 | } | ||||
2856 | if (!SimplifiedV.getValue()) | ||||
2857 | return nullptr; | ||||
2858 | V = *SimplifiedV; | ||||
2859 | } | ||||
2860 | if (isa<UndefValue>(V)) { | ||||
2861 | KnownUBInsts.insert(I); | ||||
2862 | return llvm::None; | ||||
2863 | } | ||||
2864 | return V; | ||||
2865 | } | ||||
2866 | }; | ||||
2867 | |||||
2868 | struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl { | ||||
2869 | AAUndefinedBehaviorFunction(const IRPosition &IRP, Attributor &A) | ||||
2870 | : AAUndefinedBehaviorImpl(IRP, A) {} | ||||
2871 | |||||
2872 | /// See AbstractAttribute::trackStatistics() | ||||
2873 | void trackStatistics() const override { | ||||
2874 | STATS_DECL(UndefinedBehaviorInstruction, Instruction,static llvm::Statistic NumIRInstruction_UndefinedBehaviorInstruction = {"attributor", "NumIRInstruction_UndefinedBehaviorInstruction" , "Number of instructions known to have UB"};; | ||||
2875 | "Number of instructions known to have UB")static llvm::Statistic NumIRInstruction_UndefinedBehaviorInstruction = {"attributor", "NumIRInstruction_UndefinedBehaviorInstruction" , "Number of instructions known to have UB"};;; | ||||
2876 | BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction)NumIRInstruction_UndefinedBehaviorInstruction += | ||||
2877 | KnownUBInsts.size(); | ||||
2878 | } | ||||
2879 | }; | ||||
2880 | |||||
2881 | /// ------------------------ Will-Return Attributes ---------------------------- | ||||
2882 | |||||
2883 | // Helper function that checks whether a function has any cycle which we don't | ||||
2884 | // know if it is bounded or not. | ||||
2885 | // Loops with maximum trip count are considered bounded, any other cycle not. | ||||
2886 | static bool mayContainUnboundedCycle(Function &F, Attributor &A) { | ||||
2887 | ScalarEvolution *SE = | ||||
2888 | A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F); | ||||
2889 | LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F); | ||||
2890 | // If either SCEV or LoopInfo is not available for the function then we assume | ||||
2891 | // any cycle to be unbounded cycle. | ||||
2892 | // We use scc_iterator which uses Tarjan algorithm to find all the maximal | ||||
2893 | // SCCs.To detect if there's a cycle, we only need to find the maximal ones. | ||||
2894 | if (!SE || !LI) { | ||||
2895 | for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) | ||||
2896 | if (SCCI.hasCycle()) | ||||
2897 | return true; | ||||
2898 | return false; | ||||
2899 | } | ||||
2900 | |||||
2901 | // If there's irreducible control, the function may contain non-loop cycles. | ||||
2902 | if (mayContainIrreducibleControl(F, LI)) | ||||
2903 | return true; | ||||
2904 | |||||
2905 | // Any loop that does not have a max trip count is considered unbounded cycle. | ||||
2906 | for (auto *L : LI->getLoopsInPreorder()) { | ||||
2907 | if (!SE->getSmallConstantMaxTripCount(L)) | ||||
2908 | return true; | ||||
2909 | } | ||||
2910 | return false; | ||||
2911 | } | ||||
2912 | |||||
2913 | struct AAWillReturnImpl : public AAWillReturn { | ||||
2914 | AAWillReturnImpl(const IRPosition &IRP, Attributor &A) | ||||
2915 | : AAWillReturn(IRP, A) {} | ||||
2916 | |||||
2917 | /// See AbstractAttribute::initialize(...). | ||||
2918 | void initialize(Attributor &A) override { | ||||
2919 | AAWillReturn::initialize(A); | ||||
2920 | |||||
2921 | if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ true)) { | ||||
2922 | indicateOptimisticFixpoint(); | ||||
2923 | return; | ||||
2924 | } | ||||
2925 | } | ||||
2926 | |||||
2927 | /// Check for `mustprogress` and `readonly` as they imply `willreturn`. | ||||
2928 | bool isImpliedByMustprogressAndReadonly(Attributor &A, bool KnownOnly) { | ||||
2929 | // Check for `mustprogress` in the scope and the associated function which | ||||
2930 | // might be different if this is a call site. | ||||
2931 | if ((!getAnchorScope() || !getAnchorScope()->mustProgress()) && | ||||
2932 | (!getAssociatedFunction() || !getAssociatedFunction()->mustProgress())) | ||||
2933 | return false; | ||||
2934 | |||||
2935 | bool IsKnown; | ||||
2936 | if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown)) | ||||
2937 | return IsKnown || !KnownOnly; | ||||
2938 | return false; | ||||
2939 | } | ||||
2940 | |||||
2941 | /// See AbstractAttribute::updateImpl(...). | ||||
2942 | ChangeStatus updateImpl(Attributor &A) override { | ||||
2943 | if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false)) | ||||
2944 | return ChangeStatus::UNCHANGED; | ||||
2945 | |||||
2946 | auto CheckForWillReturn = [&](Instruction &I) { | ||||
2947 | IRPosition IPos = IRPosition::callsite_function(cast<CallBase>(I)); | ||||
2948 | const auto &WillReturnAA = | ||||
2949 | A.getAAFor<AAWillReturn>(*this, IPos, DepClassTy::REQUIRED); | ||||
2950 | if (WillReturnAA.isKnownWillReturn()) | ||||
2951 | return true; | ||||
2952 | if (!WillReturnAA.isAssumedWillReturn()) | ||||
2953 | return false; | ||||
2954 | const auto &NoRecurseAA = | ||||
2955 | A.getAAFor<AANoRecurse>(*this, IPos, DepClassTy::REQUIRED); | ||||
2956 | return NoRecurseAA.isAssumedNoRecurse(); | ||||
2957 | }; | ||||
2958 | |||||
2959 | bool UsedAssumedInformation = false; | ||||
2960 | if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this, | ||||
2961 | UsedAssumedInformation)) | ||||
2962 | return indicatePessimisticFixpoint(); | ||||
2963 | |||||
2964 | return ChangeStatus::UNCHANGED; | ||||
2965 | } | ||||
2966 | |||||
2967 | /// See AbstractAttribute::getAsStr() | ||||
2968 | const std::string getAsStr() const override { | ||||
2969 | return getAssumed() ? "willreturn" : "may-noreturn"; | ||||
2970 | } | ||||
2971 | }; | ||||
2972 | |||||
2973 | struct AAWillReturnFunction final : AAWillReturnImpl { | ||||
2974 | AAWillReturnFunction(const IRPosition &IRP, Attributor &A) | ||||
2975 | : AAWillReturnImpl(IRP, A) {} | ||||
2976 | |||||
2977 | /// See AbstractAttribute::initialize(...). | ||||
2978 | void initialize(Attributor &A) override { | ||||
2979 | AAWillReturnImpl::initialize(A); | ||||
2980 | |||||
2981 | Function *F = getAnchorScope(); | ||||
2982 | if (!F || F->isDeclaration() || mayContainUnboundedCycle(*F, A)) | ||||
2983 | indicatePessimisticFixpoint(); | ||||
2984 | } | ||||
2985 | |||||
2986 | /// See AbstractAttribute::trackStatistics() | ||||
2987 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn){ static llvm::Statistic NumIRFunction_willreturn = {"attributor" , "NumIRFunction_willreturn", ("Number of " "functions" " marked '" "willreturn" "'")};; ++(NumIRFunction_willreturn); } } | ||||
2988 | }; | ||||
2989 | |||||
2990 | /// WillReturn attribute deduction for a call sites. | ||||
2991 | struct AAWillReturnCallSite final : AAWillReturnImpl { | ||||
2992 | AAWillReturnCallSite(const IRPosition &IRP, Attributor &A) | ||||
2993 | : AAWillReturnImpl(IRP, A) {} | ||||
2994 | |||||
2995 | /// See AbstractAttribute::initialize(...). | ||||
2996 | void initialize(Attributor &A) override { | ||||
2997 | AAWillReturnImpl::initialize(A); | ||||
2998 | Function *F = getAssociatedFunction(); | ||||
2999 | if (!F || !A.isFunctionIPOAmendable(*F)) | ||||
3000 | indicatePessimisticFixpoint(); | ||||
3001 | } | ||||
3002 | |||||
3003 | /// See AbstractAttribute::updateImpl(...). | ||||
3004 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3005 | if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false)) | ||||
3006 | return ChangeStatus::UNCHANGED; | ||||
3007 | |||||
3008 | // TODO: Once we have call site specific value information we can provide | ||||
3009 | // call site specific liveness information and then it makes | ||||
3010 | // sense to specialize attributes for call sites arguments instead of | ||||
3011 | // redirecting requests to the callee argument. | ||||
3012 | Function *F = getAssociatedFunction(); | ||||
3013 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
3014 | auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos, DepClassTy::REQUIRED); | ||||
3015 | return clampStateAndIndicateChange(getState(), FnAA.getState()); | ||||
3016 | } | ||||
3017 | |||||
3018 | /// See AbstractAttribute::trackStatistics() | ||||
3019 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn){ static llvm::Statistic NumIRCS_willreturn = {"attributor", "NumIRCS_willreturn" , ("Number of " "call site" " marked '" "willreturn" "'")};; ++ (NumIRCS_willreturn); }; } | ||||
3020 | }; | ||||
3021 | |||||
3022 | /// -------------------AAReachability Attribute-------------------------- | ||||
3023 | |||||
3024 | struct AAReachabilityImpl : AAReachability { | ||||
3025 | AAReachabilityImpl(const IRPosition &IRP, Attributor &A) | ||||
3026 | : AAReachability(IRP, A) {} | ||||
3027 | |||||
3028 | const std::string getAsStr() const override { | ||||
3029 | // TODO: Return the number of reachable queries. | ||||
3030 | return "reachable"; | ||||
3031 | } | ||||
3032 | |||||
3033 | /// See AbstractAttribute::updateImpl(...). | ||||
3034 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3035 | const auto &NoRecurseAA = A.getAAFor<AANoRecurse>( | ||||
3036 | *this, IRPosition::function(*getAnchorScope()), DepClassTy::REQUIRED); | ||||
3037 | if (!NoRecurseAA.isAssumedNoRecurse()) | ||||
3038 | return indicatePessimisticFixpoint(); | ||||
3039 | return ChangeStatus::UNCHANGED; | ||||
3040 | } | ||||
3041 | }; | ||||
3042 | |||||
3043 | struct AAReachabilityFunction final : public AAReachabilityImpl { | ||||
3044 | AAReachabilityFunction(const IRPosition &IRP, Attributor &A) | ||||
3045 | : AAReachabilityImpl(IRP, A) {} | ||||
3046 | |||||
3047 | /// See AbstractAttribute::trackStatistics() | ||||
3048 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable){ static llvm::Statistic NumIRFunction_reachable = {"attributor" , "NumIRFunction_reachable", ("Number of " "functions" " marked '" "reachable" "'")};; ++(NumIRFunction_reachable); }; } | ||||
3049 | }; | ||||
3050 | |||||
3051 | /// ------------------------ NoAlias Argument Attribute ------------------------ | ||||
3052 | |||||
3053 | struct AANoAliasImpl : AANoAlias { | ||||
3054 | AANoAliasImpl(const IRPosition &IRP, Attributor &A) : AANoAlias(IRP, A) { | ||||
3055 | assert(getAssociatedType()->isPointerTy() &&(static_cast <bool> (getAssociatedType()->isPointerTy () && "Noalias is a pointer attribute") ? void (0) : __assert_fail ("getAssociatedType()->isPointerTy() && \"Noalias is a pointer attribute\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3056, __extension__ __PRETTY_FUNCTION__)) | ||||
3056 | "Noalias is a pointer attribute")(static_cast <bool> (getAssociatedType()->isPointerTy () && "Noalias is a pointer attribute") ? void (0) : __assert_fail ("getAssociatedType()->isPointerTy() && \"Noalias is a pointer attribute\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3056, __extension__ __PRETTY_FUNCTION__)); | ||||
3057 | } | ||||
3058 | |||||
3059 | const std::string getAsStr() const override { | ||||
3060 | return getAssumed() ? "noalias" : "may-alias"; | ||||
3061 | } | ||||
3062 | }; | ||||
3063 | |||||
3064 | /// NoAlias attribute for a floating value. | ||||
3065 | struct AANoAliasFloating final : AANoAliasImpl { | ||||
3066 | AANoAliasFloating(const IRPosition &IRP, Attributor &A) | ||||
3067 | : AANoAliasImpl(IRP, A) {} | ||||
3068 | |||||
3069 | /// See AbstractAttribute::initialize(...). | ||||
3070 | void initialize(Attributor &A) override { | ||||
3071 | AANoAliasImpl::initialize(A); | ||||
3072 | Value *Val = &getAssociatedValue(); | ||||
3073 | do { | ||||
3074 | CastInst *CI = dyn_cast<CastInst>(Val); | ||||
3075 | if (!CI) | ||||
3076 | break; | ||||
3077 | Value *Base = CI->getOperand(0); | ||||
3078 | if (!Base->hasOneUse()) | ||||
3079 | break; | ||||
3080 | Val = Base; | ||||
3081 | } while (true); | ||||
3082 | |||||
3083 | if (!Val->getType()->isPointerTy()) { | ||||
3084 | indicatePessimisticFixpoint(); | ||||
3085 | return; | ||||
3086 | } | ||||
3087 | |||||
3088 | if (isa<AllocaInst>(Val)) | ||||
3089 | indicateOptimisticFixpoint(); | ||||
3090 | else if (isa<ConstantPointerNull>(Val) && | ||||
3091 | !NullPointerIsDefined(getAnchorScope(), | ||||
3092 | Val->getType()->getPointerAddressSpace())) | ||||
3093 | indicateOptimisticFixpoint(); | ||||
3094 | else if (Val != &getAssociatedValue()) { | ||||
3095 | const auto &ValNoAliasAA = A.getAAFor<AANoAlias>( | ||||
3096 | *this, IRPosition::value(*Val), DepClassTy::OPTIONAL); | ||||
3097 | if (ValNoAliasAA.isKnownNoAlias()) | ||||
3098 | indicateOptimisticFixpoint(); | ||||
3099 | } | ||||
3100 | } | ||||
3101 | |||||
3102 | /// See AbstractAttribute::updateImpl(...). | ||||
3103 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3104 | // TODO: Implement this. | ||||
3105 | return indicatePessimisticFixpoint(); | ||||
3106 | } | ||||
3107 | |||||
3108 | /// See AbstractAttribute::trackStatistics() | ||||
3109 | void trackStatistics() const override { | ||||
3110 | STATS_DECLTRACK_FLOATING_ATTR(noalias){ static llvm::Statistic NumIRFloating_noalias = {"attributor" , "NumIRFloating_noalias", ("Number of floating values known to be '" "noalias" "'")};; ++(NumIRFloating_noalias); } | ||||
3111 | } | ||||
3112 | }; | ||||
3113 | |||||
3114 | /// NoAlias attribute for an argument. | ||||
3115 | struct AANoAliasArgument final | ||||
3116 | : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> { | ||||
3117 | using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>; | ||||
3118 | AANoAliasArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {} | ||||
3119 | |||||
3120 | /// See AbstractAttribute::initialize(...). | ||||
3121 | void initialize(Attributor &A) override { | ||||
3122 | Base::initialize(A); | ||||
3123 | // See callsite argument attribute and callee argument attribute. | ||||
3124 | if (hasAttr({Attribute::ByVal})) | ||||
3125 | indicateOptimisticFixpoint(); | ||||
3126 | } | ||||
3127 | |||||
3128 | /// See AbstractAttribute::update(...). | ||||
3129 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3130 | // We have to make sure no-alias on the argument does not break | ||||
3131 | // synchronization when this is a callback argument, see also [1] below. | ||||
3132 | // If synchronization cannot be affected, we delegate to the base updateImpl | ||||
3133 | // function, otherwise we give up for now. | ||||
3134 | |||||
3135 | // If the function is no-sync, no-alias cannot break synchronization. | ||||
3136 | const auto &NoSyncAA = | ||||
3137 | A.getAAFor<AANoSync>(*this, IRPosition::function_scope(getIRPosition()), | ||||
3138 | DepClassTy::OPTIONAL); | ||||
3139 | if (NoSyncAA.isAssumedNoSync()) | ||||
3140 | return Base::updateImpl(A); | ||||
3141 | |||||
3142 | // If the argument is read-only, no-alias cannot break synchronization. | ||||
3143 | bool IsKnown; | ||||
3144 | if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown)) | ||||
3145 | return Base::updateImpl(A); | ||||
3146 | |||||
3147 | // If the argument is never passed through callbacks, no-alias cannot break | ||||
3148 | // synchronization. | ||||
3149 | bool UsedAssumedInformation = false; | ||||
3150 | if (A.checkForAllCallSites( | ||||
3151 | [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this, | ||||
3152 | true, UsedAssumedInformation)) | ||||
3153 | return Base::updateImpl(A); | ||||
3154 | |||||
3155 | // TODO: add no-alias but make sure it doesn't break synchronization by | ||||
3156 | // introducing fake uses. See: | ||||
3157 | // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel, | ||||
3158 | // International Workshop on OpenMP 2018, | ||||
3159 | // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf | ||||
3160 | |||||
3161 | return indicatePessimisticFixpoint(); | ||||
3162 | } | ||||
3163 | |||||
3164 | /// See AbstractAttribute::trackStatistics() | ||||
3165 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias){ static llvm::Statistic NumIRArguments_noalias = {"attributor" , "NumIRArguments_noalias", ("Number of " "arguments" " marked '" "noalias" "'")};; ++(NumIRArguments_noalias); } } | ||||
3166 | }; | ||||
3167 | |||||
3168 | struct AANoAliasCallSiteArgument final : AANoAliasImpl { | ||||
3169 | AANoAliasCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
3170 | : AANoAliasImpl(IRP, A) {} | ||||
3171 | |||||
3172 | /// See AbstractAttribute::initialize(...). | ||||
3173 | void initialize(Attributor &A) override { | ||||
3174 | // See callsite argument attribute and callee argument attribute. | ||||
3175 | const auto &CB = cast<CallBase>(getAnchorValue()); | ||||
3176 | if (CB.paramHasAttr(getCallSiteArgNo(), Attribute::NoAlias)) | ||||
3177 | indicateOptimisticFixpoint(); | ||||
3178 | Value &Val = getAssociatedValue(); | ||||
3179 | if (isa<ConstantPointerNull>(Val) && | ||||
3180 | !NullPointerIsDefined(getAnchorScope(), | ||||
3181 | Val.getType()->getPointerAddressSpace())) | ||||
3182 | indicateOptimisticFixpoint(); | ||||
3183 | } | ||||
3184 | |||||
3185 | /// Determine if the underlying value may alias with the call site argument | ||||
3186 | /// \p OtherArgNo of \p ICS (= the underlying call site). | ||||
3187 | bool mayAliasWithArgument(Attributor &A, AAResults *&AAR, | ||||
3188 | const AAMemoryBehavior &MemBehaviorAA, | ||||
3189 | const CallBase &CB, unsigned OtherArgNo) { | ||||
3190 | // We do not need to worry about aliasing with the underlying IRP. | ||||
3191 | if (this->getCalleeArgNo() == (int)OtherArgNo) | ||||
3192 | return false; | ||||
3193 | |||||
3194 | // If it is not a pointer or pointer vector we do not alias. | ||||
3195 | const Value *ArgOp = CB.getArgOperand(OtherArgNo); | ||||
3196 | if (!ArgOp->getType()->isPtrOrPtrVectorTy()) | ||||
3197 | return false; | ||||
3198 | |||||
3199 | auto &CBArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>( | ||||
3200 | *this, IRPosition::callsite_argument(CB, OtherArgNo), DepClassTy::NONE); | ||||
3201 | |||||
3202 | // If the argument is readnone, there is no read-write aliasing. | ||||
3203 | if (CBArgMemBehaviorAA.isAssumedReadNone()) { | ||||
3204 | A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL); | ||||
3205 | return false; | ||||
3206 | } | ||||
3207 | |||||
3208 | // If the argument is readonly and the underlying value is readonly, there | ||||
3209 | // is no read-write aliasing. | ||||
3210 | bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly(); | ||||
3211 | if (CBArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) { | ||||
3212 | A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); | ||||
3213 | A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL); | ||||
3214 | return false; | ||||
3215 | } | ||||
3216 | |||||
3217 | // We have to utilize actual alias analysis queries so we need the object. | ||||
3218 | if (!AAR) | ||||
3219 | AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope()); | ||||
3220 | |||||
3221 | // Try to rule it out at the call site. | ||||
3222 | bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp); | ||||
3223 | LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[NoAliasCSArg] Check alias between " "callsite arguments: " << getAssociatedValue() << " " << *ArgOp << " => " << (IsAliasing ? "" : "no-") << "alias \n"; } } while (false) | ||||
3224 | "callsite arguments: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[NoAliasCSArg] Check alias between " "callsite arguments: " << getAssociatedValue() << " " << *ArgOp << " => " << (IsAliasing ? "" : "no-") << "alias \n"; } } while (false) | ||||
3225 | << getAssociatedValue() << " " << *ArgOp << " => "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[NoAliasCSArg] Check alias between " "callsite arguments: " << getAssociatedValue() << " " << *ArgOp << " => " << (IsAliasing ? "" : "no-") << "alias \n"; } } while (false) | ||||
3226 | << (IsAliasing ? "" : "no-") << "alias \n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[NoAliasCSArg] Check alias between " "callsite arguments: " << getAssociatedValue() << " " << *ArgOp << " => " << (IsAliasing ? "" : "no-") << "alias \n"; } } while (false); | ||||
3227 | |||||
3228 | return IsAliasing; | ||||
3229 | } | ||||
3230 | |||||
3231 | bool | ||||
3232 | isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR, | ||||
3233 | const AAMemoryBehavior &MemBehaviorAA, | ||||
3234 | const AANoAlias &NoAliasAA) { | ||||
3235 | // We can deduce "noalias" if the following conditions hold. | ||||
3236 | // (i) Associated value is assumed to be noalias in the definition. | ||||
3237 | // (ii) Associated value is assumed to be no-capture in all the uses | ||||
3238 | // possibly executed before this callsite. | ||||
3239 | // (iii) There is no other pointer argument which could alias with the | ||||
3240 | // value. | ||||
3241 | |||||
3242 | bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias(); | ||||
3243 | if (!AssociatedValueIsNoAliasAtDef) { | ||||
3244 | LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AANoAlias] " << getAssociatedValue () << " is not no-alias at the definition\n"; } } while (false) | ||||
3245 | << " is not no-alias at the definition\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AANoAlias] " << getAssociatedValue () << " is not no-alias at the definition\n"; } } while (false); | ||||
3246 | return false; | ||||
3247 | } | ||||
3248 | |||||
3249 | A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL); | ||||
3250 | |||||
3251 | const IRPosition &VIRP = IRPosition::value(getAssociatedValue()); | ||||
3252 | const Function *ScopeFn = VIRP.getAnchorScope(); | ||||
3253 | auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, VIRP, DepClassTy::NONE); | ||||
3254 | // Check whether the value is captured in the scope using AANoCapture. | ||||
3255 | // Look at CFG and check only uses possibly executed before this | ||||
3256 | // callsite. | ||||
3257 | auto UsePred = [&](const Use &U, bool &Follow) -> bool { | ||||
3258 | Instruction *UserI = cast<Instruction>(U.getUser()); | ||||
3259 | |||||
3260 | // If UserI is the curr instruction and there is a single potential use of | ||||
3261 | // the value in UserI we allow the use. | ||||
3262 | // TODO: We should inspect the operands and allow those that cannot alias | ||||
3263 | // with the value. | ||||
3264 | if (UserI == getCtxI() && UserI->getNumOperands() == 1) | ||||
3265 | return true; | ||||
3266 | |||||
3267 | if (ScopeFn) { | ||||
3268 | const auto &ReachabilityAA = A.getAAFor<AAReachability>( | ||||
3269 | *this, IRPosition::function(*ScopeFn), DepClassTy::OPTIONAL); | ||||
3270 | |||||
3271 | if (!ReachabilityAA.isAssumedReachable(A, *UserI, *getCtxI())) | ||||
3272 | return true; | ||||
3273 | |||||
3274 | if (auto *CB = dyn_cast<CallBase>(UserI)) { | ||||
3275 | if (CB->isArgOperand(&U)) { | ||||
3276 | |||||
3277 | unsigned ArgNo = CB->getArgOperandNo(&U); | ||||
3278 | |||||
3279 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>( | ||||
3280 | *this, IRPosition::callsite_argument(*CB, ArgNo), | ||||
3281 | DepClassTy::OPTIONAL); | ||||
3282 | |||||
3283 | if (NoCaptureAA.isAssumedNoCapture()) | ||||
3284 | return true; | ||||
3285 | } | ||||
3286 | } | ||||
3287 | } | ||||
3288 | |||||
3289 | // For cases which can potentially have more users | ||||
3290 | if (isa<GetElementPtrInst>(U) || isa<BitCastInst>(U) || isa<PHINode>(U) || | ||||
3291 | isa<SelectInst>(U)) { | ||||
3292 | Follow = true; | ||||
3293 | return true; | ||||
3294 | } | ||||
3295 | |||||
3296 | LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n"; } } while (false); | ||||
3297 | return false; | ||||
3298 | }; | ||||
3299 | |||||
3300 | if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | ||||
3301 | if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) { | ||||
3302 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AANoAliasCSArg] " << getAssociatedValue() << " cannot be noalias as it is potentially captured\n" ; } } while (false) | ||||
3303 | dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AANoAliasCSArg] " << getAssociatedValue() << " cannot be noalias as it is potentially captured\n" ; } } while (false) | ||||
3304 | << " cannot be noalias as it is potentially captured\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AANoAliasCSArg] " << getAssociatedValue() << " cannot be noalias as it is potentially captured\n" ; } } while (false); | ||||
3305 | return false; | ||||
3306 | } | ||||
3307 | } | ||||
3308 | A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL); | ||||
3309 | |||||
3310 | // Check there is no other pointer argument which could alias with the | ||||
3311 | // value passed at this call site. | ||||
3312 | // TODO: AbstractCallSite | ||||
3313 | const auto &CB = cast<CallBase>(getAnchorValue()); | ||||
3314 | for (unsigned OtherArgNo = 0; OtherArgNo < CB.arg_size(); OtherArgNo++) | ||||
3315 | if (mayAliasWithArgument(A, AAR, MemBehaviorAA, CB, OtherArgNo)) | ||||
3316 | return false; | ||||
3317 | |||||
3318 | return true; | ||||
3319 | } | ||||
3320 | |||||
3321 | /// See AbstractAttribute::updateImpl(...). | ||||
3322 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3323 | // If the argument is readnone we are done as there are no accesses via the | ||||
3324 | // argument. | ||||
3325 | auto &MemBehaviorAA = | ||||
3326 | A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE); | ||||
3327 | if (MemBehaviorAA.isAssumedReadNone()) { | ||||
3328 | A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); | ||||
3329 | return ChangeStatus::UNCHANGED; | ||||
3330 | } | ||||
3331 | |||||
3332 | const IRPosition &VIRP = IRPosition::value(getAssociatedValue()); | ||||
3333 | const auto &NoAliasAA = | ||||
3334 | A.getAAFor<AANoAlias>(*this, VIRP, DepClassTy::NONE); | ||||
3335 | |||||
3336 | AAResults *AAR = nullptr; | ||||
3337 | if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA, | ||||
3338 | NoAliasAA)) { | ||||
3339 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n" ; } } while (false) | ||||
3340 | dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n" ; } } while (false); | ||||
3341 | return ChangeStatus::UNCHANGED; | ||||
3342 | } | ||||
3343 | |||||
3344 | return indicatePessimisticFixpoint(); | ||||
3345 | } | ||||
3346 | |||||
3347 | /// See AbstractAttribute::trackStatistics() | ||||
3348 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias){ static llvm::Statistic NumIRCSArguments_noalias = {"attributor" , "NumIRCSArguments_noalias", ("Number of " "call site arguments" " marked '" "noalias" "'")};; ++(NumIRCSArguments_noalias); } } | ||||
3349 | }; | ||||
3350 | |||||
3351 | /// NoAlias attribute for function return value. | ||||
3352 | struct AANoAliasReturned final : AANoAliasImpl { | ||||
3353 | AANoAliasReturned(const IRPosition &IRP, Attributor &A) | ||||
3354 | : AANoAliasImpl(IRP, A) {} | ||||
3355 | |||||
3356 | /// See AbstractAttribute::initialize(...). | ||||
3357 | void initialize(Attributor &A) override { | ||||
3358 | AANoAliasImpl::initialize(A); | ||||
3359 | Function *F = getAssociatedFunction(); | ||||
3360 | if (!F || F->isDeclaration()) | ||||
3361 | indicatePessimisticFixpoint(); | ||||
3362 | } | ||||
3363 | |||||
3364 | /// See AbstractAttribute::updateImpl(...). | ||||
3365 | virtual ChangeStatus updateImpl(Attributor &A) override { | ||||
3366 | |||||
3367 | auto CheckReturnValue = [&](Value &RV) -> bool { | ||||
3368 | if (Constant *C = dyn_cast<Constant>(&RV)) | ||||
3369 | if (C->isNullValue() || isa<UndefValue>(C)) | ||||
3370 | return true; | ||||
3371 | |||||
3372 | /// For now, we can only deduce noalias if we have call sites. | ||||
3373 | /// FIXME: add more support. | ||||
3374 | if (!isa<CallBase>(&RV)) | ||||
3375 | return false; | ||||
3376 | |||||
3377 | const IRPosition &RVPos = IRPosition::value(RV); | ||||
3378 | const auto &NoAliasAA = | ||||
3379 | A.getAAFor<AANoAlias>(*this, RVPos, DepClassTy::REQUIRED); | ||||
3380 | if (!NoAliasAA.isAssumedNoAlias()) | ||||
3381 | return false; | ||||
3382 | |||||
3383 | const auto &NoCaptureAA = | ||||
3384 | A.getAAFor<AANoCapture>(*this, RVPos, DepClassTy::REQUIRED); | ||||
3385 | return NoCaptureAA.isAssumedNoCaptureMaybeReturned(); | ||||
3386 | }; | ||||
3387 | |||||
3388 | if (!A.checkForAllReturnedValues(CheckReturnValue, *this)) | ||||
3389 | return indicatePessimisticFixpoint(); | ||||
3390 | |||||
3391 | return ChangeStatus::UNCHANGED; | ||||
3392 | } | ||||
3393 | |||||
3394 | /// See AbstractAttribute::trackStatistics() | ||||
3395 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias){ static llvm::Statistic NumIRFunctionReturn_noalias = {"attributor" , "NumIRFunctionReturn_noalias", ("Number of " "function returns" " marked '" "noalias" "'")};; ++(NumIRFunctionReturn_noalias ); } } | ||||
3396 | }; | ||||
3397 | |||||
3398 | /// NoAlias attribute deduction for a call site return value. | ||||
3399 | struct AANoAliasCallSiteReturned final : AANoAliasImpl { | ||||
3400 | AANoAliasCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
3401 | : AANoAliasImpl(IRP, A) {} | ||||
3402 | |||||
3403 | /// See AbstractAttribute::initialize(...). | ||||
3404 | void initialize(Attributor &A) override { | ||||
3405 | AANoAliasImpl::initialize(A); | ||||
3406 | Function *F = getAssociatedFunction(); | ||||
3407 | if (!F || F->isDeclaration()) | ||||
3408 | indicatePessimisticFixpoint(); | ||||
3409 | } | ||||
3410 | |||||
3411 | /// See AbstractAttribute::updateImpl(...). | ||||
3412 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3413 | // TODO: Once we have call site specific value information we can provide | ||||
3414 | // call site specific liveness information and then it makes | ||||
3415 | // sense to specialize attributes for call sites arguments instead of | ||||
3416 | // redirecting requests to the callee argument. | ||||
3417 | Function *F = getAssociatedFunction(); | ||||
3418 | const IRPosition &FnPos = IRPosition::returned(*F); | ||||
3419 | auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos, DepClassTy::REQUIRED); | ||||
3420 | return clampStateAndIndicateChange(getState(), FnAA.getState()); | ||||
3421 | } | ||||
3422 | |||||
3423 | /// See AbstractAttribute::trackStatistics() | ||||
3424 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias){ static llvm::Statistic NumIRCSReturn_noalias = {"attributor" , "NumIRCSReturn_noalias", ("Number of " "call site returns" " marked '" "noalias" "'")};; ++(NumIRCSReturn_noalias); }; } | ||||
3425 | }; | ||||
3426 | |||||
3427 | /// -------------------AAIsDead Function Attribute----------------------- | ||||
3428 | |||||
3429 | struct AAIsDeadValueImpl : public AAIsDead { | ||||
3430 | AAIsDeadValueImpl(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {} | ||||
3431 | |||||
3432 | /// See AAIsDead::isAssumedDead(). | ||||
3433 | bool isAssumedDead() const override { return isAssumed(IS_DEAD); } | ||||
3434 | |||||
3435 | /// See AAIsDead::isKnownDead(). | ||||
3436 | bool isKnownDead() const override { return isKnown(IS_DEAD); } | ||||
3437 | |||||
3438 | /// See AAIsDead::isAssumedDead(BasicBlock *). | ||||
3439 | bool isAssumedDead(const BasicBlock *BB) const override { return false; } | ||||
3440 | |||||
3441 | /// See AAIsDead::isKnownDead(BasicBlock *). | ||||
3442 | bool isKnownDead(const BasicBlock *BB) const override { return false; } | ||||
3443 | |||||
3444 | /// See AAIsDead::isAssumedDead(Instruction *I). | ||||
3445 | bool isAssumedDead(const Instruction *I) const override { | ||||
3446 | return I == getCtxI() && isAssumedDead(); | ||||
3447 | } | ||||
3448 | |||||
3449 | /// See AAIsDead::isKnownDead(Instruction *I). | ||||
3450 | bool isKnownDead(const Instruction *I) const override { | ||||
3451 | return isAssumedDead(I) && isKnownDead(); | ||||
3452 | } | ||||
3453 | |||||
3454 | /// See AbstractAttribute::getAsStr(). | ||||
3455 | const std::string getAsStr() const override { | ||||
3456 | return isAssumedDead() ? "assumed-dead" : "assumed-live"; | ||||
3457 | } | ||||
3458 | |||||
3459 | /// Check if all uses are assumed dead. | ||||
3460 | bool areAllUsesAssumedDead(Attributor &A, Value &V) { | ||||
3461 | // Callers might not check the type, void has no uses. | ||||
3462 | if (V.getType()->isVoidTy()) | ||||
3463 | return true; | ||||
3464 | |||||
3465 | // If we replace a value with a constant there are no uses left afterwards. | ||||
3466 | if (!isa<Constant>(V)) { | ||||
3467 | bool UsedAssumedInformation = false; | ||||
3468 | Optional<Constant *> C = | ||||
3469 | A.getAssumedConstant(V, *this, UsedAssumedInformation); | ||||
3470 | if (!C.hasValue() || *C) | ||||
3471 | return true; | ||||
3472 | } | ||||
3473 | |||||
3474 | auto UsePred = [&](const Use &U, bool &Follow) { return false; }; | ||||
3475 | // Explicitly set the dependence class to required because we want a long | ||||
3476 | // chain of N dependent instructions to be considered live as soon as one is | ||||
3477 | // without going through N update cycles. This is not required for | ||||
3478 | // correctness. | ||||
3479 | return A.checkForAllUses(UsePred, *this, V, /* CheckBBLivenessOnly */ false, | ||||
3480 | DepClassTy::REQUIRED); | ||||
3481 | } | ||||
3482 | |||||
3483 | /// Determine if \p I is assumed to be side-effect free. | ||||
3484 | bool isAssumedSideEffectFree(Attributor &A, Instruction *I) { | ||||
3485 | if (!I || wouldInstructionBeTriviallyDead(I)) | ||||
3486 | return true; | ||||
3487 | |||||
3488 | auto *CB = dyn_cast<CallBase>(I); | ||||
3489 | if (!CB || isa<IntrinsicInst>(CB)) | ||||
3490 | return false; | ||||
3491 | |||||
3492 | const IRPosition &CallIRP = IRPosition::callsite_function(*CB); | ||||
3493 | const auto &NoUnwindAA = | ||||
3494 | A.getAndUpdateAAFor<AANoUnwind>(*this, CallIRP, DepClassTy::NONE); | ||||
3495 | if (!NoUnwindAA.isAssumedNoUnwind()) | ||||
3496 | return false; | ||||
3497 | if (!NoUnwindAA.isKnownNoUnwind()) | ||||
3498 | A.recordDependence(NoUnwindAA, *this, DepClassTy::OPTIONAL); | ||||
3499 | |||||
3500 | bool IsKnown; | ||||
3501 | return AA::isAssumedReadOnly(A, CallIRP, *this, IsKnown); | ||||
3502 | } | ||||
3503 | }; | ||||
3504 | |||||
3505 | struct AAIsDeadFloating : public AAIsDeadValueImpl { | ||||
3506 | AAIsDeadFloating(const IRPosition &IRP, Attributor &A) | ||||
3507 | : AAIsDeadValueImpl(IRP, A) {} | ||||
3508 | |||||
3509 | /// See AbstractAttribute::initialize(...). | ||||
3510 | void initialize(Attributor &A) override { | ||||
3511 | if (isa<UndefValue>(getAssociatedValue())) { | ||||
3512 | indicatePessimisticFixpoint(); | ||||
3513 | return; | ||||
3514 | } | ||||
3515 | |||||
3516 | Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); | ||||
3517 | if (!isAssumedSideEffectFree(A, I)) { | ||||
3518 | if (!isa_and_nonnull<StoreInst>(I)) | ||||
3519 | indicatePessimisticFixpoint(); | ||||
3520 | else | ||||
3521 | removeAssumedBits(HAS_NO_EFFECT); | ||||
3522 | } | ||||
3523 | } | ||||
3524 | |||||
3525 | bool isDeadStore(Attributor &A, StoreInst &SI) { | ||||
3526 | // Lang ref now states volatile store is not UB/dead, let's skip them. | ||||
3527 | if (SI.isVolatile()) | ||||
3528 | return false; | ||||
3529 | |||||
3530 | bool UsedAssumedInformation = false; | ||||
3531 | SmallSetVector<Value *, 4> PotentialCopies; | ||||
3532 | if (!AA::getPotentialCopiesOfStoredValue(A, SI, PotentialCopies, *this, | ||||
3533 | UsedAssumedInformation)) | ||||
3534 | return false; | ||||
3535 | return llvm::all_of(PotentialCopies, [&](Value *V) { | ||||
3536 | return A.isAssumedDead(IRPosition::value(*V), this, nullptr, | ||||
3537 | UsedAssumedInformation); | ||||
3538 | }); | ||||
3539 | } | ||||
3540 | |||||
3541 | /// See AbstractAttribute::updateImpl(...). | ||||
3542 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3543 | Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); | ||||
3544 | if (auto *SI = dyn_cast_or_null<StoreInst>(I)) { | ||||
3545 | if (!isDeadStore(A, *SI)) | ||||
3546 | return indicatePessimisticFixpoint(); | ||||
3547 | } else { | ||||
3548 | if (!isAssumedSideEffectFree(A, I)) | ||||
3549 | return indicatePessimisticFixpoint(); | ||||
3550 | if (!areAllUsesAssumedDead(A, getAssociatedValue())) | ||||
3551 | return indicatePessimisticFixpoint(); | ||||
3552 | } | ||||
3553 | return ChangeStatus::UNCHANGED; | ||||
3554 | } | ||||
3555 | |||||
3556 | /// See AbstractAttribute::manifest(...). | ||||
3557 | ChangeStatus manifest(Attributor &A) override { | ||||
3558 | Value &V = getAssociatedValue(); | ||||
3559 | if (auto *I = dyn_cast<Instruction>(&V)) { | ||||
3560 | // If we get here we basically know the users are all dead. We check if | ||||
3561 | // isAssumedSideEffectFree returns true here again because it might not be | ||||
3562 | // the case and only the users are dead but the instruction (=call) is | ||||
3563 | // still needed. | ||||
3564 | if (isa<StoreInst>(I) || | ||||
3565 | (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I))) { | ||||
3566 | A.deleteAfterManifest(*I); | ||||
3567 | return ChangeStatus::CHANGED; | ||||
3568 | } | ||||
3569 | } | ||||
3570 | if (V.use_empty()) | ||||
3571 | return ChangeStatus::UNCHANGED; | ||||
3572 | |||||
3573 | bool UsedAssumedInformation = false; | ||||
3574 | Optional<Constant *> C = | ||||
3575 | A.getAssumedConstant(V, *this, UsedAssumedInformation); | ||||
3576 | if (C.hasValue() && C.getValue()) | ||||
3577 | return ChangeStatus::UNCHANGED; | ||||
3578 | |||||
3579 | // Replace the value with undef as it is dead but keep droppable uses around | ||||
3580 | // as they provide information we don't want to give up on just yet. | ||||
3581 | UndefValue &UV = *UndefValue::get(V.getType()); | ||||
3582 | bool AnyChange = | ||||
3583 | A.changeValueAfterManifest(V, UV, /* ChangeDropppable */ false); | ||||
3584 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | ||||
3585 | } | ||||
3586 | |||||
3587 | /// See AbstractAttribute::trackStatistics() | ||||
3588 | void trackStatistics() const override { | ||||
3589 | STATS_DECLTRACK_FLOATING_ATTR(IsDead){ static llvm::Statistic NumIRFloating_IsDead = {"attributor" , "NumIRFloating_IsDead", ("Number of floating values known to be '" "IsDead" "'")};; ++(NumIRFloating_IsDead); } | ||||
3590 | } | ||||
3591 | }; | ||||
3592 | |||||
3593 | struct AAIsDeadArgument : public AAIsDeadFloating { | ||||
3594 | AAIsDeadArgument(const IRPosition &IRP, Attributor &A) | ||||
3595 | : AAIsDeadFloating(IRP, A) {} | ||||
3596 | |||||
3597 | /// See AbstractAttribute::initialize(...). | ||||
3598 | void initialize(Attributor &A) override { | ||||
3599 | if (!A.isFunctionIPOAmendable(*getAnchorScope())) | ||||
3600 | indicatePessimisticFixpoint(); | ||||
3601 | } | ||||
3602 | |||||
3603 | /// See AbstractAttribute::manifest(...). | ||||
3604 | ChangeStatus manifest(Attributor &A) override { | ||||
3605 | ChangeStatus Changed = AAIsDeadFloating::manifest(A); | ||||
3606 | Argument &Arg = *getAssociatedArgument(); | ||||
3607 | if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {})) | ||||
3608 | if (A.registerFunctionSignatureRewrite( | ||||
3609 | Arg, /* ReplacementTypes */ {}, | ||||
3610 | Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{}, | ||||
3611 | Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) { | ||||
3612 | Arg.dropDroppableUses(); | ||||
3613 | return ChangeStatus::CHANGED; | ||||
3614 | } | ||||
3615 | return Changed; | ||||
3616 | } | ||||
3617 | |||||
3618 | /// See AbstractAttribute::trackStatistics() | ||||
3619 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead){ static llvm::Statistic NumIRArguments_IsDead = {"attributor" , "NumIRArguments_IsDead", ("Number of " "arguments" " marked '" "IsDead" "'")};; ++(NumIRArguments_IsDead); } } | ||||
3620 | }; | ||||
3621 | |||||
3622 | struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl { | ||||
3623 | AAIsDeadCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
3624 | : AAIsDeadValueImpl(IRP, A) {} | ||||
3625 | |||||
3626 | /// See AbstractAttribute::initialize(...). | ||||
3627 | void initialize(Attributor &A) override { | ||||
3628 | if (isa<UndefValue>(getAssociatedValue())) | ||||
3629 | indicatePessimisticFixpoint(); | ||||
3630 | } | ||||
3631 | |||||
3632 | /// See AbstractAttribute::updateImpl(...). | ||||
3633 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3634 | // TODO: Once we have call site specific value information we can provide | ||||
3635 | // call site specific liveness information and then it makes | ||||
3636 | // sense to specialize attributes for call sites arguments instead of | ||||
3637 | // redirecting requests to the callee argument. | ||||
3638 | Argument *Arg = getAssociatedArgument(); | ||||
3639 | if (!Arg) | ||||
3640 | return indicatePessimisticFixpoint(); | ||||
3641 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | ||||
3642 | auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos, DepClassTy::REQUIRED); | ||||
3643 | return clampStateAndIndicateChange(getState(), ArgAA.getState()); | ||||
3644 | } | ||||
3645 | |||||
3646 | /// See AbstractAttribute::manifest(...). | ||||
3647 | ChangeStatus manifest(Attributor &A) override { | ||||
3648 | CallBase &CB = cast<CallBase>(getAnchorValue()); | ||||
3649 | Use &U = CB.getArgOperandUse(getCallSiteArgNo()); | ||||
3650 | assert(!isa<UndefValue>(U.get()) &&(static_cast <bool> (!isa<UndefValue>(U.get()) && "Expected undef values to be filtered out!") ? void (0) : __assert_fail ("!isa<UndefValue>(U.get()) && \"Expected undef values to be filtered out!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3651, __extension__ __PRETTY_FUNCTION__)) | ||||
3651 | "Expected undef values to be filtered out!")(static_cast <bool> (!isa<UndefValue>(U.get()) && "Expected undef values to be filtered out!") ? void (0) : __assert_fail ("!isa<UndefValue>(U.get()) && \"Expected undef values to be filtered out!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3651, __extension__ __PRETTY_FUNCTION__)); | ||||
3652 | UndefValue &UV = *UndefValue::get(U->getType()); | ||||
3653 | if (A.changeUseAfterManifest(U, UV)) | ||||
3654 | return ChangeStatus::CHANGED; | ||||
3655 | return ChangeStatus::UNCHANGED; | ||||
3656 | } | ||||
3657 | |||||
3658 | /// See AbstractAttribute::trackStatistics() | ||||
3659 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead){ static llvm::Statistic NumIRCSArguments_IsDead = {"attributor" , "NumIRCSArguments_IsDead", ("Number of " "call site arguments" " marked '" "IsDead" "'")};; ++(NumIRCSArguments_IsDead); } } | ||||
3660 | }; | ||||
3661 | |||||
3662 | struct AAIsDeadCallSiteReturned : public AAIsDeadFloating { | ||||
3663 | AAIsDeadCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
3664 | : AAIsDeadFloating(IRP, A) {} | ||||
3665 | |||||
3666 | /// See AAIsDead::isAssumedDead(). | ||||
3667 | bool isAssumedDead() const override { | ||||
3668 | return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree; | ||||
3669 | } | ||||
3670 | |||||
3671 | /// See AbstractAttribute::initialize(...). | ||||
3672 | void initialize(Attributor &A) override { | ||||
3673 | if (isa<UndefValue>(getAssociatedValue())) { | ||||
3674 | indicatePessimisticFixpoint(); | ||||
3675 | return; | ||||
3676 | } | ||||
3677 | |||||
3678 | // We track this separately as a secondary state. | ||||
3679 | IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI()); | ||||
3680 | } | ||||
3681 | |||||
3682 | /// See AbstractAttribute::updateImpl(...). | ||||
3683 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3684 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
3685 | if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) { | ||||
3686 | IsAssumedSideEffectFree = false; | ||||
3687 | Changed = ChangeStatus::CHANGED; | ||||
3688 | } | ||||
3689 | if (!areAllUsesAssumedDead(A, getAssociatedValue())) | ||||
3690 | return indicatePessimisticFixpoint(); | ||||
3691 | return Changed; | ||||
3692 | } | ||||
3693 | |||||
3694 | /// See AbstractAttribute::trackStatistics() | ||||
3695 | void trackStatistics() const override { | ||||
3696 | if (IsAssumedSideEffectFree) | ||||
3697 | STATS_DECLTRACK_CSRET_ATTR(IsDead){ static llvm::Statistic NumIRCSReturn_IsDead = {"attributor" , "NumIRCSReturn_IsDead", ("Number of " "call site returns" " marked '" "IsDead" "'")};; ++(NumIRCSReturn_IsDead); } | ||||
3698 | else | ||||
3699 | STATS_DECLTRACK_CSRET_ATTR(UnusedResult){ static llvm::Statistic NumIRCSReturn_UnusedResult = {"attributor" , "NumIRCSReturn_UnusedResult", ("Number of " "call site returns" " marked '" "UnusedResult" "'")};; ++(NumIRCSReturn_UnusedResult ); } | ||||
3700 | } | ||||
3701 | |||||
3702 | /// See AbstractAttribute::getAsStr(). | ||||
3703 | const std::string getAsStr() const override { | ||||
3704 | return isAssumedDead() | ||||
3705 | ? "assumed-dead" | ||||
3706 | : (getAssumed() ? "assumed-dead-users" : "assumed-live"); | ||||
3707 | } | ||||
3708 | |||||
3709 | private: | ||||
3710 | bool IsAssumedSideEffectFree = true; | ||||
3711 | }; | ||||
3712 | |||||
3713 | struct AAIsDeadReturned : public AAIsDeadValueImpl { | ||||
3714 | AAIsDeadReturned(const IRPosition &IRP, Attributor &A) | ||||
3715 | : AAIsDeadValueImpl(IRP, A) {} | ||||
3716 | |||||
3717 | /// See AbstractAttribute::updateImpl(...). | ||||
3718 | ChangeStatus updateImpl(Attributor &A) override { | ||||
3719 | |||||
3720 | bool UsedAssumedInformation = false; | ||||
3721 | A.checkForAllInstructions([](Instruction &) { return true; }, *this, | ||||
3722 | {Instruction::Ret}, UsedAssumedInformation); | ||||
3723 | |||||
3724 | auto PredForCallSite = [&](AbstractCallSite ACS) { | ||||
3725 | if (ACS.isCallbackCall() || !ACS.getInstruction()) | ||||
3726 | return false; | ||||
3727 | return areAllUsesAssumedDead(A, *ACS.getInstruction()); | ||||
3728 | }; | ||||
3729 | |||||
3730 | if (!A.checkForAllCallSites(PredForCallSite, *this, true, | ||||
3731 | UsedAssumedInformation)) | ||||
3732 | return indicatePessimisticFixpoint(); | ||||
3733 | |||||
3734 | return ChangeStatus::UNCHANGED; | ||||
3735 | } | ||||
3736 | |||||
3737 | /// See AbstractAttribute::manifest(...). | ||||
3738 | ChangeStatus manifest(Attributor &A) override { | ||||
3739 | // TODO: Rewrite the signature to return void? | ||||
3740 | bool AnyChange = false; | ||||
3741 | UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); | ||||
3742 | auto RetInstPred = [&](Instruction &I) { | ||||
3743 | ReturnInst &RI = cast<ReturnInst>(I); | ||||
3744 | if (!isa<UndefValue>(RI.getReturnValue())) | ||||
3745 | AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV); | ||||
3746 | return true; | ||||
3747 | }; | ||||
3748 | bool UsedAssumedInformation = false; | ||||
3749 | A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret}, | ||||
3750 | UsedAssumedInformation); | ||||
3751 | return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | ||||
3752 | } | ||||
3753 | |||||
3754 | /// See AbstractAttribute::trackStatistics() | ||||
3755 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead){ static llvm::Statistic NumIRFunctionReturn_IsDead = {"attributor" , "NumIRFunctionReturn_IsDead", ("Number of " "function returns" " marked '" "IsDead" "'")};; ++(NumIRFunctionReturn_IsDead); } } | ||||
3756 | }; | ||||
3757 | |||||
3758 | struct AAIsDeadFunction : public AAIsDead { | ||||
3759 | AAIsDeadFunction(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {} | ||||
3760 | |||||
3761 | /// See AbstractAttribute::initialize(...). | ||||
3762 | void initialize(Attributor &A) override { | ||||
3763 | const Function *F = getAnchorScope(); | ||||
3764 | if (F && !F->isDeclaration()) { | ||||
3765 | // We only want to compute liveness once. If the function is not part of | ||||
3766 | // the SCC, skip it. | ||||
3767 | if (A.isRunOn(*const_cast<Function *>(F))) { | ||||
3768 | ToBeExploredFrom.insert(&F->getEntryBlock().front()); | ||||
3769 | assumeLive(A, F->getEntryBlock()); | ||||
3770 | } else { | ||||
3771 | indicatePessimisticFixpoint(); | ||||
3772 | } | ||||
3773 | } | ||||
3774 | } | ||||
3775 | |||||
3776 | /// See AbstractAttribute::getAsStr(). | ||||
3777 | const std::string getAsStr() const override { | ||||
3778 | return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" + | ||||
3779 | std::to_string(getAnchorScope()->size()) + "][#TBEP " + | ||||
3780 | std::to_string(ToBeExploredFrom.size()) + "][#KDE " + | ||||
3781 | std::to_string(KnownDeadEnds.size()) + "]"; | ||||
3782 | } | ||||
3783 | |||||
3784 | /// See AbstractAttribute::manifest(...). | ||||
3785 | ChangeStatus manifest(Attributor &A) override { | ||||
3786 | assert(getState().isValidState() &&(static_cast <bool> (getState().isValidState() && "Attempted to manifest an invalid state!") ? void (0) : __assert_fail ("getState().isValidState() && \"Attempted to manifest an invalid state!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3787, __extension__ __PRETTY_FUNCTION__)) | ||||
3787 | "Attempted to manifest an invalid state!")(static_cast <bool> (getState().isValidState() && "Attempted to manifest an invalid state!") ? void (0) : __assert_fail ("getState().isValidState() && \"Attempted to manifest an invalid state!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3787, __extension__ __PRETTY_FUNCTION__)); | ||||
3788 | |||||
3789 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | ||||
3790 | Function &F = *getAnchorScope(); | ||||
3791 | |||||
3792 | if (AssumedLiveBlocks.empty()) { | ||||
3793 | A.deleteAfterManifest(F); | ||||
3794 | return ChangeStatus::CHANGED; | ||||
3795 | } | ||||
3796 | |||||
3797 | // Flag to determine if we can change an invoke to a call assuming the | ||||
3798 | // callee is nounwind. This is not possible if the personality of the | ||||
3799 | // function allows to catch asynchronous exceptions. | ||||
3800 | bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F); | ||||
3801 | |||||
3802 | KnownDeadEnds.set_union(ToBeExploredFrom); | ||||
3803 | for (const Instruction *DeadEndI : KnownDeadEnds) { | ||||
3804 | auto *CB = dyn_cast<CallBase>(DeadEndI); | ||||
3805 | if (!CB) | ||||
3806 | continue; | ||||
3807 | const auto &NoReturnAA = A.getAndUpdateAAFor<AANoReturn>( | ||||
3808 | *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL); | ||||
3809 | bool MayReturn = !NoReturnAA.isAssumedNoReturn(); | ||||
3810 | if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB))) | ||||
3811 | continue; | ||||
3812 | |||||
3813 | if (auto *II = dyn_cast<InvokeInst>(DeadEndI)) | ||||
3814 | A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II)); | ||||
3815 | else | ||||
3816 | A.changeToUnreachableAfterManifest( | ||||
3817 | const_cast<Instruction *>(DeadEndI->getNextNode())); | ||||
3818 | HasChanged = ChangeStatus::CHANGED; | ||||
3819 | } | ||||
3820 | |||||
3821 | STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.")static llvm::Statistic NumIRBasicBlock_AAIsDead = {"attributor" , "NumIRBasicBlock_AAIsDead", "Number of dead basic blocks deleted." };;; | ||||
3822 | for (BasicBlock &BB : F) | ||||
3823 | if (!AssumedLiveBlocks.count(&BB)) { | ||||
3824 | A.deleteAfterManifest(BB); | ||||
3825 | ++BUILD_STAT_NAME(AAIsDead, BasicBlock)NumIRBasicBlock_AAIsDead; | ||||
3826 | HasChanged = ChangeStatus::CHANGED; | ||||
3827 | } | ||||
3828 | |||||
3829 | return HasChanged; | ||||
3830 | } | ||||
3831 | |||||
3832 | /// See AbstractAttribute::updateImpl(...). | ||||
3833 | ChangeStatus updateImpl(Attributor &A) override; | ||||
3834 | |||||
3835 | bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const override { | ||||
3836 | assert(From->getParent() == getAnchorScope() &&(static_cast <bool> (From->getParent() == getAnchorScope () && To->getParent() == getAnchorScope() && "Used AAIsDead of the wrong function") ? void (0) : __assert_fail ("From->getParent() == getAnchorScope() && To->getParent() == getAnchorScope() && \"Used AAIsDead of the wrong function\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3838, __extension__ __PRETTY_FUNCTION__)) | ||||
3837 | To->getParent() == getAnchorScope() &&(static_cast <bool> (From->getParent() == getAnchorScope () && To->getParent() == getAnchorScope() && "Used AAIsDead of the wrong function") ? void (0) : __assert_fail ("From->getParent() == getAnchorScope() && To->getParent() == getAnchorScope() && \"Used AAIsDead of the wrong function\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3838, __extension__ __PRETTY_FUNCTION__)) | ||||
3838 | "Used AAIsDead of the wrong function")(static_cast <bool> (From->getParent() == getAnchorScope () && To->getParent() == getAnchorScope() && "Used AAIsDead of the wrong function") ? void (0) : __assert_fail ("From->getParent() == getAnchorScope() && To->getParent() == getAnchorScope() && \"Used AAIsDead of the wrong function\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3838, __extension__ __PRETTY_FUNCTION__)); | ||||
3839 | return isValidState() && !AssumedLiveEdges.count(std::make_pair(From, To)); | ||||
3840 | } | ||||
3841 | |||||
3842 | /// See AbstractAttribute::trackStatistics() | ||||
3843 | void trackStatistics() const override {} | ||||
3844 | |||||
3845 | /// Returns true if the function is assumed dead. | ||||
3846 | bool isAssumedDead() const override { return false; } | ||||
3847 | |||||
3848 | /// See AAIsDead::isKnownDead(). | ||||
3849 | bool isKnownDead() const override { return false; } | ||||
3850 | |||||
3851 | /// See AAIsDead::isAssumedDead(BasicBlock *). | ||||
3852 | bool isAssumedDead(const BasicBlock *BB) const override { | ||||
3853 | assert(BB->getParent() == getAnchorScope() &&(static_cast <bool> (BB->getParent() == getAnchorScope () && "BB must be in the same anchor scope function." ) ? void (0) : __assert_fail ("BB->getParent() == getAnchorScope() && \"BB must be in the same anchor scope function.\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3854, __extension__ __PRETTY_FUNCTION__)) | ||||
3854 | "BB must be in the same anchor scope function.")(static_cast <bool> (BB->getParent() == getAnchorScope () && "BB must be in the same anchor scope function." ) ? void (0) : __assert_fail ("BB->getParent() == getAnchorScope() && \"BB must be in the same anchor scope function.\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3854, __extension__ __PRETTY_FUNCTION__)); | ||||
3855 | |||||
3856 | if (!getAssumed()) | ||||
3857 | return false; | ||||
3858 | return !AssumedLiveBlocks.count(BB); | ||||
3859 | } | ||||
3860 | |||||
3861 | /// See AAIsDead::isKnownDead(BasicBlock *). | ||||
3862 | bool isKnownDead(const BasicBlock *BB) const override { | ||||
3863 | return getKnown() && isAssumedDead(BB); | ||||
3864 | } | ||||
3865 | |||||
3866 | /// See AAIsDead::isAssumed(Instruction *I). | ||||
3867 | bool isAssumedDead(const Instruction *I) const override { | ||||
3868 | assert(I->getParent()->getParent() == getAnchorScope() &&(static_cast <bool> (I->getParent()->getParent() == getAnchorScope() && "Instruction must be in the same anchor scope function." ) ? void (0) : __assert_fail ("I->getParent()->getParent() == getAnchorScope() && \"Instruction must be in the same anchor scope function.\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3869, __extension__ __PRETTY_FUNCTION__)) | ||||
3869 | "Instruction must be in the same anchor scope function.")(static_cast <bool> (I->getParent()->getParent() == getAnchorScope() && "Instruction must be in the same anchor scope function." ) ? void (0) : __assert_fail ("I->getParent()->getParent() == getAnchorScope() && \"Instruction must be in the same anchor scope function.\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 3869, __extension__ __PRETTY_FUNCTION__)); | ||||
3870 | |||||
3871 | if (!getAssumed()) | ||||
3872 | return false; | ||||
3873 | |||||
3874 | // If it is not in AssumedLiveBlocks then it for sure dead. | ||||
3875 | // Otherwise, it can still be after noreturn call in a live block. | ||||
3876 | if (!AssumedLiveBlocks.count(I->getParent())) | ||||
3877 | return true; | ||||
3878 | |||||
3879 | // If it is not after a liveness barrier it is live. | ||||
3880 | const Instruction *PrevI = I->getPrevNode(); | ||||
3881 | while (PrevI) { | ||||
3882 | if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI)) | ||||
3883 | return true; | ||||
3884 | PrevI = PrevI->getPrevNode(); | ||||
3885 | } | ||||
3886 | return false; | ||||
3887 | } | ||||
3888 | |||||
3889 | /// See AAIsDead::isKnownDead(Instruction *I). | ||||
3890 | bool isKnownDead(const Instruction *I) const override { | ||||
3891 | return getKnown() && isAssumedDead(I); | ||||
3892 | } | ||||
3893 | |||||
3894 | /// Assume \p BB is (partially) live now and indicate to the Attributor \p A | ||||
3895 | /// that internal function called from \p BB should now be looked at. | ||||
3896 | bool assumeLive(Attributor &A, const BasicBlock &BB) { | ||||
3897 | if (!AssumedLiveBlocks.insert(&BB).second) | ||||
3898 | return false; | ||||
3899 | |||||
3900 | // We assume that all of BB is (probably) live now and if there are calls to | ||||
3901 | // internal functions we will assume that those are now live as well. This | ||||
3902 | // is a performance optimization for blocks with calls to a lot of internal | ||||
3903 | // functions. It can however cause dead functions to be treated as live. | ||||
3904 | for (const Instruction &I : BB) | ||||
3905 | if (const auto *CB = dyn_cast<CallBase>(&I)) | ||||
3906 | if (const Function *F = CB->getCalledFunction()) | ||||
3907 | if (F->hasLocalLinkage()) | ||||
3908 | A.markLiveInternalFunction(*F); | ||||
3909 | return true; | ||||
3910 | } | ||||
3911 | |||||
3912 | /// Collection of instructions that need to be explored again, e.g., we | ||||
3913 | /// did assume they do not transfer control to (one of their) successors. | ||||
3914 | SmallSetVector<const Instruction *, 8> ToBeExploredFrom; | ||||
3915 | |||||
3916 | /// Collection of instructions that are known to not transfer control. | ||||
3917 | SmallSetVector<const Instruction *, 8> KnownDeadEnds; | ||||
3918 | |||||
3919 | /// Collection of all assumed live edges | ||||
3920 | DenseSet<std::pair<const BasicBlock *, const BasicBlock *>> AssumedLiveEdges; | ||||
3921 | |||||
3922 | /// Collection of all assumed live BasicBlocks. | ||||
3923 | DenseSet<const BasicBlock *> AssumedLiveBlocks; | ||||
3924 | }; | ||||
3925 | |||||
3926 | static bool | ||||
3927 | identifyAliveSuccessors(Attributor &A, const CallBase &CB, | ||||
3928 | AbstractAttribute &AA, | ||||
3929 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | ||||
3930 | const IRPosition &IPos = IRPosition::callsite_function(CB); | ||||
3931 | |||||
3932 | const auto &NoReturnAA = | ||||
3933 | A.getAndUpdateAAFor<AANoReturn>(AA, IPos, DepClassTy::OPTIONAL); | ||||
3934 | if (NoReturnAA.isAssumedNoReturn()) | ||||
3935 | return !NoReturnAA.isKnownNoReturn(); | ||||
3936 | if (CB.isTerminator()) | ||||
3937 | AliveSuccessors.push_back(&CB.getSuccessor(0)->front()); | ||||
3938 | else | ||||
3939 | AliveSuccessors.push_back(CB.getNextNode()); | ||||
3940 | return false; | ||||
3941 | } | ||||
3942 | |||||
3943 | static bool | ||||
3944 | identifyAliveSuccessors(Attributor &A, const InvokeInst &II, | ||||
3945 | AbstractAttribute &AA, | ||||
3946 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | ||||
3947 | bool UsedAssumedInformation = | ||||
3948 | identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors); | ||||
3949 | |||||
3950 | // First, determine if we can change an invoke to a call assuming the | ||||
3951 | // callee is nounwind. This is not possible if the personality of the | ||||
3952 | // function allows to catch asynchronous exceptions. | ||||
3953 | if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) { | ||||
3954 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); | ||||
3955 | } else { | ||||
3956 | const IRPosition &IPos = IRPosition::callsite_function(II); | ||||
3957 | const auto &AANoUnw = | ||||
3958 | A.getAndUpdateAAFor<AANoUnwind>(AA, IPos, DepClassTy::OPTIONAL); | ||||
3959 | if (AANoUnw.isAssumedNoUnwind()) { | ||||
3960 | UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind(); | ||||
3961 | } else { | ||||
3962 | AliveSuccessors.push_back(&II.getUnwindDest()->front()); | ||||
3963 | } | ||||
3964 | } | ||||
3965 | return UsedAssumedInformation; | ||||
3966 | } | ||||
3967 | |||||
3968 | static bool | ||||
3969 | identifyAliveSuccessors(Attributor &A, const BranchInst &BI, | ||||
3970 | AbstractAttribute &AA, | ||||
3971 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | ||||
3972 | bool UsedAssumedInformation = false; | ||||
3973 | if (BI.getNumSuccessors() == 1) { | ||||
3974 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); | ||||
3975 | } else { | ||||
3976 | Optional<Constant *> C = | ||||
3977 | A.getAssumedConstant(*BI.getCondition(), AA, UsedAssumedInformation); | ||||
3978 | if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) { | ||||
3979 | // No value yet, assume both edges are dead. | ||||
3980 | } else if (isa_and_nonnull<ConstantInt>(*C)) { | ||||
3981 | const BasicBlock *SuccBB = | ||||
3982 | BI.getSuccessor(1 - cast<ConstantInt>(*C)->getValue().getZExtValue()); | ||||
3983 | AliveSuccessors.push_back(&SuccBB->front()); | ||||
3984 | } else { | ||||
3985 | AliveSuccessors.push_back(&BI.getSuccessor(0)->front()); | ||||
3986 | AliveSuccessors.push_back(&BI.getSuccessor(1)->front()); | ||||
3987 | UsedAssumedInformation = false; | ||||
3988 | } | ||||
3989 | } | ||||
3990 | return UsedAssumedInformation; | ||||
3991 | } | ||||
3992 | |||||
3993 | static bool | ||||
3994 | identifyAliveSuccessors(Attributor &A, const SwitchInst &SI, | ||||
3995 | AbstractAttribute &AA, | ||||
3996 | SmallVectorImpl<const Instruction *> &AliveSuccessors) { | ||||
3997 | bool UsedAssumedInformation = false; | ||||
3998 | Optional<Constant *> C = | ||||
3999 | A.getAssumedConstant(*SI.getCondition(), AA, UsedAssumedInformation); | ||||
4000 | if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) { | ||||
4001 | // No value yet, assume all edges are dead. | ||||
4002 | } else if (isa_and_nonnull<ConstantInt>(C.getValue())) { | ||||
4003 | for (auto &CaseIt : SI.cases()) { | ||||
4004 | if (CaseIt.getCaseValue() == C.getValue()) { | ||||
4005 | AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front()); | ||||
4006 | return UsedAssumedInformation; | ||||
4007 | } | ||||
4008 | } | ||||
4009 | AliveSuccessors.push_back(&SI.getDefaultDest()->front()); | ||||
4010 | return UsedAssumedInformation; | ||||
4011 | } else { | ||||
4012 | for (const BasicBlock *SuccBB : successors(SI.getParent())) | ||||
4013 | AliveSuccessors.push_back(&SuccBB->front()); | ||||
4014 | } | ||||
4015 | return UsedAssumedInformation; | ||||
4016 | } | ||||
4017 | |||||
4018 | ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) { | ||||
4019 | ChangeStatus Change = ChangeStatus::UNCHANGED; | ||||
4020 | |||||
4021 | LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" << getAnchorScope ()->size() << "] BBs and " << ToBeExploredFrom .size() << " exploration points and " << KnownDeadEnds .size() << " known dead ends\n"; } } while (false) | ||||
4022 | << getAnchorScope()->size() << "] BBs and "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" << getAnchorScope ()->size() << "] BBs and " << ToBeExploredFrom .size() << " exploration points and " << KnownDeadEnds .size() << " known dead ends\n"; } } while (false) | ||||
4023 | << ToBeExploredFrom.size() << " exploration points and "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" << getAnchorScope ()->size() << "] BBs and " << ToBeExploredFrom .size() << " exploration points and " << KnownDeadEnds .size() << " known dead ends\n"; } } while (false) | ||||
4024 | << KnownDeadEnds.size() << " known dead ends\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/" << getAnchorScope ()->size() << "] BBs and " << ToBeExploredFrom .size() << " exploration points and " << KnownDeadEnds .size() << " known dead ends\n"; } } while (false); | ||||
4025 | |||||
4026 | // Copy and clear the list of instructions we need to explore from. It is | ||||
4027 | // refilled with instructions the next update has to look at. | ||||
4028 | SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(), | ||||
4029 | ToBeExploredFrom.end()); | ||||
4030 | decltype(ToBeExploredFrom) NewToBeExploredFrom; | ||||
4031 | |||||
4032 | SmallVector<const Instruction *, 8> AliveSuccessors; | ||||
4033 | while (!Worklist.empty()) { | ||||
4034 | const Instruction *I = Worklist.pop_back_val(); | ||||
4035 | LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n"; } } while (false); | ||||
4036 | |||||
4037 | // Fast forward for uninteresting instructions. We could look for UB here | ||||
4038 | // though. | ||||
4039 | while (!I->isTerminator() && !isa<CallBase>(I)) | ||||
4040 | I = I->getNextNode(); | ||||
4041 | |||||
4042 | AliveSuccessors.clear(); | ||||
4043 | |||||
4044 | bool UsedAssumedInformation = false; | ||||
4045 | switch (I->getOpcode()) { | ||||
4046 | // TODO: look for (assumed) UB to backwards propagate "deadness". | ||||
4047 | default: | ||||
4048 | assert(I->isTerminator() &&(static_cast <bool> (I->isTerminator() && "Expected non-terminators to be handled already!" ) ? void (0) : __assert_fail ("I->isTerminator() && \"Expected non-terminators to be handled already!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 4049, __extension__ __PRETTY_FUNCTION__)) | ||||
4049 | "Expected non-terminators to be handled already!")(static_cast <bool> (I->isTerminator() && "Expected non-terminators to be handled already!" ) ? void (0) : __assert_fail ("I->isTerminator() && \"Expected non-terminators to be handled already!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 4049, __extension__ __PRETTY_FUNCTION__)); | ||||
4050 | for (const BasicBlock *SuccBB : successors(I->getParent())) | ||||
4051 | AliveSuccessors.push_back(&SuccBB->front()); | ||||
4052 | break; | ||||
4053 | case Instruction::Call: | ||||
4054 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I), | ||||
4055 | *this, AliveSuccessors); | ||||
4056 | break; | ||||
4057 | case Instruction::Invoke: | ||||
4058 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I), | ||||
4059 | *this, AliveSuccessors); | ||||
4060 | break; | ||||
4061 | case Instruction::Br: | ||||
4062 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I), | ||||
4063 | *this, AliveSuccessors); | ||||
4064 | break; | ||||
4065 | case Instruction::Switch: | ||||
4066 | UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I), | ||||
4067 | *this, AliveSuccessors); | ||||
4068 | break; | ||||
4069 | } | ||||
4070 | |||||
4071 | if (UsedAssumedInformation) { | ||||
4072 | NewToBeExploredFrom.insert(I); | ||||
4073 | } else if (AliveSuccessors.empty() || | ||||
4074 | (I->isTerminator() && | ||||
4075 | AliveSuccessors.size() < I->getNumSuccessors())) { | ||||
4076 | if (KnownDeadEnds.insert(I)) | ||||
4077 | Change = ChangeStatus::CHANGED; | ||||
4078 | } | ||||
4079 | |||||
4080 | LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAIsDead] #AliveSuccessors: " << AliveSuccessors.size() << " UsedAssumedInformation: " << UsedAssumedInformation << "\n"; } } while (false ) | ||||
4081 | << AliveSuccessors.size() << " UsedAssumedInformation: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAIsDead] #AliveSuccessors: " << AliveSuccessors.size() << " UsedAssumedInformation: " << UsedAssumedInformation << "\n"; } } while (false ) | ||||
4082 | << UsedAssumedInformation << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAIsDead] #AliveSuccessors: " << AliveSuccessors.size() << " UsedAssumedInformation: " << UsedAssumedInformation << "\n"; } } while (false ); | ||||
4083 | |||||
4084 | for (const Instruction *AliveSuccessor : AliveSuccessors) { | ||||
4085 | if (!I->isTerminator()) { | ||||
4086 | assert(AliveSuccessors.size() == 1 &&(static_cast <bool> (AliveSuccessors.size() == 1 && "Non-terminator expected to have a single successor!") ? void (0) : __assert_fail ("AliveSuccessors.size() == 1 && \"Non-terminator expected to have a single successor!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 4087, __extension__ __PRETTY_FUNCTION__)) | ||||
4087 | "Non-terminator expected to have a single successor!")(static_cast <bool> (AliveSuccessors.size() == 1 && "Non-terminator expected to have a single successor!") ? void (0) : __assert_fail ("AliveSuccessors.size() == 1 && \"Non-terminator expected to have a single successor!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 4087, __extension__ __PRETTY_FUNCTION__)); | ||||
4088 | Worklist.push_back(AliveSuccessor); | ||||
4089 | } else { | ||||
4090 | // record the assumed live edge | ||||
4091 | auto Edge = std::make_pair(I->getParent(), AliveSuccessor->getParent()); | ||||
4092 | if (AssumedLiveEdges.insert(Edge).second) | ||||
4093 | Change = ChangeStatus::CHANGED; | ||||
4094 | if (assumeLive(A, *AliveSuccessor->getParent())) | ||||
4095 | Worklist.push_back(AliveSuccessor); | ||||
4096 | } | ||||
4097 | } | ||||
4098 | } | ||||
4099 | |||||
4100 | // Check if the content of ToBeExploredFrom changed, ignore the order. | ||||
4101 | if (NewToBeExploredFrom.size() != ToBeExploredFrom.size() || | ||||
4102 | llvm::any_of(NewToBeExploredFrom, [&](const Instruction *I) { | ||||
4103 | return !ToBeExploredFrom.count(I); | ||||
4104 | })) { | ||||
4105 | Change = ChangeStatus::CHANGED; | ||||
4106 | ToBeExploredFrom = std::move(NewToBeExploredFrom); | ||||
4107 | } | ||||
4108 | |||||
4109 | // If we know everything is live there is no need to query for liveness. | ||||
4110 | // Instead, indicating a pessimistic fixpoint will cause the state to be | ||||
4111 | // "invalid" and all queries to be answered conservatively without lookups. | ||||
4112 | // To be in this state we have to (1) finished the exploration and (3) not | ||||
4113 | // discovered any non-trivial dead end and (2) not ruled unreachable code | ||||
4114 | // dead. | ||||
4115 | if (ToBeExploredFrom.empty() && | ||||
4116 | getAnchorScope()->size() == AssumedLiveBlocks.size() && | ||||
4117 | llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) { | ||||
4118 | return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0; | ||||
4119 | })) | ||||
4120 | return indicatePessimisticFixpoint(); | ||||
4121 | return Change; | ||||
4122 | } | ||||
4123 | |||||
4124 | /// Liveness information for a call sites. | ||||
4125 | struct AAIsDeadCallSite final : AAIsDeadFunction { | ||||
4126 | AAIsDeadCallSite(const IRPosition &IRP, Attributor &A) | ||||
4127 | : AAIsDeadFunction(IRP, A) {} | ||||
4128 | |||||
4129 | /// See AbstractAttribute::initialize(...). | ||||
4130 | void initialize(Attributor &A) override { | ||||
4131 | // TODO: Once we have call site specific value information we can provide | ||||
4132 | // call site specific liveness information and then it makes | ||||
4133 | // sense to specialize attributes for call sites instead of | ||||
4134 | // redirecting requests to the callee. | ||||
4135 | llvm_unreachable("Abstract attributes for liveness are not "::llvm::llvm_unreachable_internal("Abstract attributes for liveness are not " "supported for call sites yet!", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 4136) | ||||
4136 | "supported for call sites yet!")::llvm::llvm_unreachable_internal("Abstract attributes for liveness are not " "supported for call sites yet!", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 4136); | ||||
4137 | } | ||||
4138 | |||||
4139 | /// See AbstractAttribute::updateImpl(...). | ||||
4140 | ChangeStatus updateImpl(Attributor &A) override { | ||||
4141 | return indicatePessimisticFixpoint(); | ||||
4142 | } | ||||
4143 | |||||
4144 | /// See AbstractAttribute::trackStatistics() | ||||
4145 | void trackStatistics() const override {} | ||||
4146 | }; | ||||
4147 | |||||
4148 | /// -------------------- Dereferenceable Argument Attribute -------------------- | ||||
4149 | |||||
4150 | struct AADereferenceableImpl : AADereferenceable { | ||||
4151 | AADereferenceableImpl(const IRPosition &IRP, Attributor &A) | ||||
4152 | : AADereferenceable(IRP, A) {} | ||||
4153 | using StateType = DerefState; | ||||
4154 | |||||
4155 | /// See AbstractAttribute::initialize(...). | ||||
4156 | void initialize(Attributor &A) override { | ||||
4157 | SmallVector<Attribute, 4> Attrs; | ||||
4158 | getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull}, | ||||
4159 | Attrs, /* IgnoreSubsumingPositions */ false, &A); | ||||
4160 | for (const Attribute &Attr : Attrs) | ||||
4161 | takeKnownDerefBytesMaximum(Attr.getValueAsInt()); | ||||
4162 | |||||
4163 | const IRPosition &IRP = this->getIRPosition(); | ||||
4164 | NonNullAA = &A.getAAFor<AANonNull>(*this, IRP, DepClassTy::NONE); | ||||
4165 | |||||
4166 | bool CanBeNull, CanBeFreed; | ||||
4167 | takeKnownDerefBytesMaximum( | ||||
4168 | IRP.getAssociatedValue().getPointerDereferenceableBytes( | ||||
4169 | A.getDataLayout(), CanBeNull, CanBeFreed)); | ||||
4170 | |||||
4171 | bool IsFnInterface = IRP.isFnInterfaceKind(); | ||||
4172 | Function *FnScope = IRP.getAnchorScope(); | ||||
4173 | if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope))) { | ||||
4174 | indicatePessimisticFixpoint(); | ||||
4175 | return; | ||||
4176 | } | ||||
4177 | |||||
4178 | if (Instruction *CtxI = getCtxI()) | ||||
4179 | followUsesInMBEC(*this, A, getState(), *CtxI); | ||||
4180 | } | ||||
4181 | |||||
4182 | /// See AbstractAttribute::getState() | ||||
4183 | /// { | ||||
4184 | StateType &getState() override { return *this; } | ||||
4185 | const StateType &getState() const override { return *this; } | ||||
4186 | /// } | ||||
4187 | |||||
4188 | /// Helper function for collecting accessed bytes in must-be-executed-context | ||||
4189 | void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I, | ||||
4190 | DerefState &State) { | ||||
4191 | const Value *UseV = U->get(); | ||||
4192 | if (!UseV->getType()->isPointerTy()) | ||||
4193 | return; | ||||
4194 | |||||
4195 | Optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I); | ||||
4196 | if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile()) | ||||
4197 | return; | ||||
4198 | |||||
4199 | int64_t Offset; | ||||
4200 | const Value *Base = GetPointerBaseWithConstantOffset( | ||||
4201 | Loc->Ptr, Offset, A.getDataLayout(), /*AllowNonInbounds*/ true); | ||||
4202 | if (Base && Base == &getAssociatedValue()) | ||||
4203 | State.addAccessedBytes(Offset, Loc->Size.getValue()); | ||||
4204 | } | ||||
4205 | |||||
4206 | /// See followUsesInMBEC | ||||
4207 | bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I, | ||||
4208 | AADereferenceable::StateType &State) { | ||||
4209 | bool IsNonNull = false; | ||||
4210 | bool TrackUse = false; | ||||
4211 | int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse( | ||||
4212 | A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse); | ||||
4213 | LLVM_DEBUG(dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytesdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytes << " for instruction " << *I << "\n"; } } while (false) | ||||
4214 | << " for instruction " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytes << " for instruction " << *I << "\n"; } } while (false); | ||||
4215 | |||||
4216 | addAccessedBytesForUse(A, U, I, State); | ||||
4217 | State.takeKnownDerefBytesMaximum(DerefBytes); | ||||
4218 | return TrackUse; | ||||
4219 | } | ||||
4220 | |||||
4221 | /// See AbstractAttribute::manifest(...). | ||||
4222 | ChangeStatus manifest(Attributor &A) override { | ||||
4223 | ChangeStatus Change = AADereferenceable::manifest(A); | ||||
4224 | if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) { | ||||
4225 | removeAttrs({Attribute::DereferenceableOrNull}); | ||||
4226 | return ChangeStatus::CHANGED; | ||||
4227 | } | ||||
4228 | return Change; | ||||
4229 | } | ||||
4230 | |||||
4231 | void getDeducedAttributes(LLVMContext &Ctx, | ||||
4232 | SmallVectorImpl<Attribute> &Attrs) const override { | ||||
4233 | // TODO: Add *_globally support | ||||
4234 | if (isAssumedNonNull()) | ||||
4235 | Attrs.emplace_back(Attribute::getWithDereferenceableBytes( | ||||
4236 | Ctx, getAssumedDereferenceableBytes())); | ||||
4237 | else | ||||
4238 | Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes( | ||||
4239 | Ctx, getAssumedDereferenceableBytes())); | ||||
4240 | } | ||||
4241 | |||||
4242 | /// See AbstractAttribute::getAsStr(). | ||||
4243 | const std::string getAsStr() const override { | ||||
4244 | if (!getAssumedDereferenceableBytes()) | ||||
4245 | return "unknown-dereferenceable"; | ||||
4246 | return std::string("dereferenceable") + | ||||
4247 | (isAssumedNonNull() ? "" : "_or_null") + | ||||
4248 | (isAssumedGlobal() ? "_globally" : "") + "<" + | ||||
4249 | std::to_string(getKnownDereferenceableBytes()) + "-" + | ||||
4250 | std::to_string(getAssumedDereferenceableBytes()) + ">"; | ||||
4251 | } | ||||
4252 | }; | ||||
4253 | |||||
4254 | /// Dereferenceable attribute for a floating value. | ||||
4255 | struct AADereferenceableFloating : AADereferenceableImpl { | ||||
4256 | AADereferenceableFloating(const IRPosition &IRP, Attributor &A) | ||||
4257 | : AADereferenceableImpl(IRP, A) {} | ||||
4258 | |||||
4259 | /// See AbstractAttribute::updateImpl(...). | ||||
4260 | ChangeStatus updateImpl(Attributor &A) override { | ||||
4261 | const DataLayout &DL = A.getDataLayout(); | ||||
4262 | |||||
4263 | auto VisitValueCB = [&](const Value &V, const Instruction *, DerefState &T, | ||||
4264 | bool Stripped) -> bool { | ||||
4265 | unsigned IdxWidth = | ||||
4266 | DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace()); | ||||
4267 | APInt Offset(IdxWidth, 0); | ||||
4268 | const Value *Base = | ||||
4269 | stripAndAccumulateMinimalOffsets(A, *this, &V, DL, Offset, false); | ||||
4270 | |||||
4271 | const auto &AA = A.getAAFor<AADereferenceable>( | ||||
4272 | *this, IRPosition::value(*Base), DepClassTy::REQUIRED); | ||||
4273 | int64_t DerefBytes = 0; | ||||
4274 | if (!Stripped && this == &AA) { | ||||
4275 | // Use IR information if we did not strip anything. | ||||
4276 | // TODO: track globally. | ||||
4277 | bool CanBeNull, CanBeFreed; | ||||
4278 | DerefBytes = | ||||
4279 | Base->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed); | ||||
4280 | T.GlobalState.indicatePessimisticFixpoint(); | ||||
4281 | } else { | ||||
4282 | const DerefState &DS = AA.getState(); | ||||
4283 | DerefBytes = DS.DerefBytesState.getAssumed(); | ||||
4284 | T.GlobalState &= DS.GlobalState; | ||||
4285 | } | ||||
4286 | |||||
4287 | // For now we do not try to "increase" dereferenceability due to negative | ||||
4288 | // indices as we first have to come up with code to deal with loops and | ||||
4289 | // for overflows of the dereferenceable bytes. | ||||
4290 | int64_t OffsetSExt = Offset.getSExtValue(); | ||||
4291 | if (OffsetSExt < 0) | ||||
4292 | OffsetSExt = 0; | ||||
4293 | |||||
4294 | T.takeAssumedDerefBytesMinimum( | ||||
4295 | std::max(int64_t(0), DerefBytes - OffsetSExt)); | ||||
4296 | |||||
4297 | if (this == &AA) { | ||||
4298 | if (!Stripped) { | ||||
4299 | // If nothing was stripped IR information is all we got. | ||||
4300 | T.takeKnownDerefBytesMaximum( | ||||
4301 | std::max(int64_t(0), DerefBytes - OffsetSExt)); | ||||
4302 | T.indicatePessimisticFixpoint(); | ||||
4303 | } else if (OffsetSExt > 0) { | ||||
4304 | // If something was stripped but there is circular reasoning we look | ||||
4305 | // for the offset. If it is positive we basically decrease the | ||||
4306 | // dereferenceable bytes in a circluar loop now, which will simply | ||||
4307 | // drive them down to the known value in a very slow way which we | ||||
4308 | // can accelerate. | ||||
4309 | T.indicatePessimisticFixpoint(); | ||||
4310 | } | ||||
4311 | } | ||||
4312 | |||||
4313 | return T.isValidState(); | ||||
4314 | }; | ||||
4315 | |||||
4316 | DerefState T; | ||||
4317 | bool UsedAssumedInformation = false; | ||||
4318 | if (!genericValueTraversal<DerefState>(A, getIRPosition(), *this, T, | ||||
4319 | VisitValueCB, getCtxI(), | ||||
4320 | UsedAssumedInformation)) | ||||
4321 | return indicatePessimisticFixpoint(); | ||||
4322 | |||||
4323 | return clampStateAndIndicateChange(getState(), T); | ||||
4324 | } | ||||
4325 | |||||
4326 | /// See AbstractAttribute::trackStatistics() | ||||
4327 | void trackStatistics() const override { | ||||
4328 | STATS_DECLTRACK_FLOATING_ATTR(dereferenceable){ static llvm::Statistic NumIRFloating_dereferenceable = {"attributor" , "NumIRFloating_dereferenceable", ("Number of floating values known to be '" "dereferenceable" "'")};; ++(NumIRFloating_dereferenceable); } | ||||
4329 | } | ||||
4330 | }; | ||||
4331 | |||||
4332 | /// Dereferenceable attribute for a return value. | ||||
4333 | struct AADereferenceableReturned final | ||||
4334 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> { | ||||
4335 | AADereferenceableReturned(const IRPosition &IRP, Attributor &A) | ||||
4336 | : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>( | ||||
4337 | IRP, A) {} | ||||
4338 | |||||
4339 | /// See AbstractAttribute::trackStatistics() | ||||
4340 | void trackStatistics() const override { | ||||
4341 | STATS_DECLTRACK_FNRET_ATTR(dereferenceable){ static llvm::Statistic NumIRFunctionReturn_dereferenceable = {"attributor", "NumIRFunctionReturn_dereferenceable", ("Number of " "function returns" " marked '" "dereferenceable" "'")};; ++( NumIRFunctionReturn_dereferenceable); } | ||||
4342 | } | ||||
4343 | }; | ||||
4344 | |||||
4345 | /// Dereferenceable attribute for an argument | ||||
4346 | struct AADereferenceableArgument final | ||||
4347 | : AAArgumentFromCallSiteArguments<AADereferenceable, | ||||
4348 | AADereferenceableImpl> { | ||||
4349 | using Base = | ||||
4350 | AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl>; | ||||
4351 | AADereferenceableArgument(const IRPosition &IRP, Attributor &A) | ||||
4352 | : Base(IRP, A) {} | ||||
4353 | |||||
4354 | /// See AbstractAttribute::trackStatistics() | ||||
4355 | void trackStatistics() const override { | ||||
4356 | STATS_DECLTRACK_ARG_ATTR(dereferenceable){ static llvm::Statistic NumIRArguments_dereferenceable = {"attributor" , "NumIRArguments_dereferenceable", ("Number of " "arguments" " marked '" "dereferenceable" "'")};; ++(NumIRArguments_dereferenceable ); } | ||||
4357 | } | ||||
4358 | }; | ||||
4359 | |||||
4360 | /// Dereferenceable attribute for a call site argument. | ||||
4361 | struct AADereferenceableCallSiteArgument final : AADereferenceableFloating { | ||||
4362 | AADereferenceableCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
4363 | : AADereferenceableFloating(IRP, A) {} | ||||
4364 | |||||
4365 | /// See AbstractAttribute::trackStatistics() | ||||
4366 | void trackStatistics() const override { | ||||
4367 | STATS_DECLTRACK_CSARG_ATTR(dereferenceable){ static llvm::Statistic NumIRCSArguments_dereferenceable = { "attributor", "NumIRCSArguments_dereferenceable", ("Number of " "call site arguments" " marked '" "dereferenceable" "'")};; ++ (NumIRCSArguments_dereferenceable); } | ||||
4368 | } | ||||
4369 | }; | ||||
4370 | |||||
4371 | /// Dereferenceable attribute deduction for a call site return value. | ||||
4372 | struct AADereferenceableCallSiteReturned final | ||||
4373 | : AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl> { | ||||
4374 | using Base = | ||||
4375 | AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl>; | ||||
4376 | AADereferenceableCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
4377 | : Base(IRP, A) {} | ||||
4378 | |||||
4379 | /// See AbstractAttribute::trackStatistics() | ||||
4380 | void trackStatistics() const override { | ||||
4381 | STATS_DECLTRACK_CS_ATTR(dereferenceable){ static llvm::Statistic NumIRCS_dereferenceable = {"attributor" , "NumIRCS_dereferenceable", ("Number of " "call site" " marked '" "dereferenceable" "'")};; ++(NumIRCS_dereferenceable); }; | ||||
4382 | } | ||||
4383 | }; | ||||
4384 | |||||
4385 | // ------------------------ Align Argument Attribute ------------------------ | ||||
4386 | |||||
4387 | static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA, | ||||
4388 | Value &AssociatedValue, const Use *U, | ||||
4389 | const Instruction *I, bool &TrackUse) { | ||||
4390 | // We need to follow common pointer manipulation uses to the accesses they | ||||
4391 | // feed into. | ||||
4392 | if (isa<CastInst>(I)) { | ||||
4393 | // Follow all but ptr2int casts. | ||||
4394 | TrackUse = !isa<PtrToIntInst>(I); | ||||
4395 | return 0; | ||||
4396 | } | ||||
4397 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { | ||||
4398 | if (GEP->hasAllConstantIndices()) | ||||
4399 | TrackUse = true; | ||||
4400 | return 0; | ||||
4401 | } | ||||
4402 | |||||
4403 | MaybeAlign MA; | ||||
4404 | if (const auto *CB = dyn_cast<CallBase>(I)) { | ||||
4405 | if (CB->isBundleOperand(U) || CB->isCallee(U)) | ||||
4406 | return 0; | ||||
4407 | |||||
4408 | unsigned ArgNo = CB->getArgOperandNo(U); | ||||
4409 | IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo); | ||||
4410 | // As long as we only use known information there is no need to track | ||||
4411 | // dependences here. | ||||
4412 | auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, DepClassTy::NONE); | ||||
4413 | MA = MaybeAlign(AlignAA.getKnownAlign()); | ||||
4414 | } | ||||
4415 | |||||
4416 | const DataLayout &DL = A.getDataLayout(); | ||||
4417 | const Value *UseV = U->get(); | ||||
4418 | if (auto *SI = dyn_cast<StoreInst>(I)) { | ||||
4419 | if (SI->getPointerOperand() == UseV) | ||||
4420 | MA = SI->getAlign(); | ||||
4421 | } else if (auto *LI = dyn_cast<LoadInst>(I)) { | ||||
4422 | if (LI->getPointerOperand() == UseV) | ||||
4423 | MA = LI->getAlign(); | ||||
4424 | } | ||||
4425 | |||||
4426 | if (!MA || *MA <= QueryingAA.getKnownAlign()) | ||||
4427 | return 0; | ||||
4428 | |||||
4429 | unsigned Alignment = MA->value(); | ||||
4430 | int64_t Offset; | ||||
4431 | |||||
4432 | if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) { | ||||
4433 | if (Base == &AssociatedValue) { | ||||
4434 | // BasePointerAddr + Offset = Alignment * Q for some integer Q. | ||||
4435 | // So we can say that the maximum power of two which is a divisor of | ||||
4436 | // gcd(Offset, Alignment) is an alignment. | ||||
4437 | |||||
4438 | uint32_t gcd = | ||||
4439 | greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment); | ||||
4440 | Alignment = llvm::PowerOf2Floor(gcd); | ||||
4441 | } | ||||
4442 | } | ||||
4443 | |||||
4444 | return Alignment; | ||||
4445 | } | ||||
4446 | |||||
4447 | struct AAAlignImpl : AAAlign { | ||||
4448 | AAAlignImpl(const IRPosition &IRP, Attributor &A) : AAAlign(IRP, A) {} | ||||
4449 | |||||
4450 | /// See AbstractAttribute::initialize(...). | ||||
4451 | void initialize(Attributor &A) override { | ||||
4452 | SmallVector<Attribute, 4> Attrs; | ||||
4453 | getAttrs({Attribute::Alignment}, Attrs); | ||||
4454 | for (const Attribute &Attr : Attrs) | ||||
4455 | takeKnownMaximum(Attr.getValueAsInt()); | ||||
4456 | |||||
4457 | Value &V = getAssociatedValue(); | ||||
4458 | // TODO: This is a HACK to avoid getPointerAlignment to introduce a ptr2int | ||||
4459 | // use of the function pointer. This was caused by D73131. We want to | ||||
4460 | // avoid this for function pointers especially because we iterate | ||||
4461 | // their uses and int2ptr is not handled. It is not a correctness | ||||
4462 | // problem though! | ||||
4463 | if (!V.getType()->getPointerElementType()->isFunctionTy()) | ||||
4464 | takeKnownMaximum(V.getPointerAlignment(A.getDataLayout()).value()); | ||||
4465 | |||||
4466 | if (getIRPosition().isFnInterfaceKind() && | ||||
4467 | (!getAnchorScope() || | ||||
4468 | !A.isFunctionIPOAmendable(*getAssociatedFunction()))) { | ||||
4469 | indicatePessimisticFixpoint(); | ||||
4470 | return; | ||||
4471 | } | ||||
4472 | |||||
4473 | if (Instruction *CtxI = getCtxI()) | ||||
4474 | followUsesInMBEC(*this, A, getState(), *CtxI); | ||||
4475 | } | ||||
4476 | |||||
4477 | /// See AbstractAttribute::manifest(...). | ||||
4478 | ChangeStatus manifest(Attributor &A) override { | ||||
4479 | ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED; | ||||
4480 | |||||
4481 | // Check for users that allow alignment annotations. | ||||
4482 | Value &AssociatedValue = getAssociatedValue(); | ||||
4483 | for (const Use &U : AssociatedValue.uses()) { | ||||
4484 | if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { | ||||
4485 | if (SI->getPointerOperand() == &AssociatedValue) | ||||
4486 | if (SI->getAlignment() < getAssumedAlign()) { | ||||
4487 | STATS_DECLTRACK(AAAlign, Store,{ static llvm::Statistic NumIRStore_AAAlign = {"attributor", "NumIRStore_AAAlign" , "Number of times alignment added to a store"};; ++(NumIRStore_AAAlign ); } | ||||
4488 | "Number of times alignment added to a store"){ static llvm::Statistic NumIRStore_AAAlign = {"attributor", "NumIRStore_AAAlign" , "Number of times alignment added to a store"};; ++(NumIRStore_AAAlign ); }; | ||||
4489 | SI->setAlignment(Align(getAssumedAlign())); | ||||
4490 | LoadStoreChanged = ChangeStatus::CHANGED; | ||||
4491 | } | ||||
4492 | } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { | ||||
4493 | if (LI->getPointerOperand() == &AssociatedValue) | ||||
4494 | if (LI->getAlignment() < getAssumedAlign()) { | ||||
4495 | LI->setAlignment(Align(getAssumedAlign())); | ||||
4496 | STATS_DECLTRACK(AAAlign, Load,{ static llvm::Statistic NumIRLoad_AAAlign = {"attributor", "NumIRLoad_AAAlign" , "Number of times alignment added to a load"};; ++(NumIRLoad_AAAlign ); } | ||||
4497 | "Number of times alignment added to a load"){ static llvm::Statistic NumIRLoad_AAAlign = {"attributor", "NumIRLoad_AAAlign" , "Number of times alignment added to a load"};; ++(NumIRLoad_AAAlign ); }; | ||||
4498 | LoadStoreChanged = ChangeStatus::CHANGED; | ||||
4499 | } | ||||
4500 | } | ||||
4501 | } | ||||
4502 | |||||
4503 | ChangeStatus Changed = AAAlign::manifest(A); | ||||
4504 | |||||
4505 | Align InheritAlign = | ||||
4506 | getAssociatedValue().getPointerAlignment(A.getDataLayout()); | ||||
4507 | if (InheritAlign >= getAssumedAlign()) | ||||
4508 | return LoadStoreChanged; | ||||
4509 | return Changed | LoadStoreChanged; | ||||
4510 | } | ||||
4511 | |||||
4512 | // TODO: Provide a helper to determine the implied ABI alignment and check in | ||||
4513 | // the existing manifest method and a new one for AAAlignImpl that value | ||||
4514 | // to avoid making the alignment explicit if it did not improve. | ||||
4515 | |||||
4516 | /// See AbstractAttribute::getDeducedAttributes | ||||
4517 | virtual void | ||||
4518 | getDeducedAttributes(LLVMContext &Ctx, | ||||
4519 | SmallVectorImpl<Attribute> &Attrs) const override { | ||||
4520 | if (getAssumedAlign() > 1) | ||||
4521 | Attrs.emplace_back( | ||||
4522 | Attribute::getWithAlignment(Ctx, Align(getAssumedAlign()))); | ||||
4523 | } | ||||
4524 | |||||
4525 | /// See followUsesInMBEC | ||||
4526 | bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I, | ||||
4527 | AAAlign::StateType &State) { | ||||
4528 | bool TrackUse = false; | ||||
4529 | |||||
4530 | unsigned int KnownAlign = | ||||
4531 | getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse); | ||||
4532 | State.takeKnownMaximum(KnownAlign); | ||||
4533 | |||||
4534 | return TrackUse; | ||||
4535 | } | ||||
4536 | |||||
4537 | /// See AbstractAttribute::getAsStr(). | ||||
4538 | const std::string getAsStr() const override { | ||||
4539 | return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) + | ||||
4540 | "-" + std::to_string(getAssumedAlign()) + ">") | ||||
4541 | : "unknown-align"; | ||||
4542 | } | ||||
4543 | }; | ||||
4544 | |||||
4545 | /// Align attribute for a floating value. | ||||
4546 | struct AAAlignFloating : AAAlignImpl { | ||||
4547 | AAAlignFloating(const IRPosition &IRP, Attributor &A) : AAAlignImpl(IRP, A) {} | ||||
4548 | |||||
4549 | /// See AbstractAttribute::updateImpl(...). | ||||
4550 | ChangeStatus updateImpl(Attributor &A) override { | ||||
4551 | const DataLayout &DL = A.getDataLayout(); | ||||
4552 | |||||
4553 | auto VisitValueCB = [&](Value &V, const Instruction *, | ||||
4554 | AAAlign::StateType &T, bool Stripped) -> bool { | ||||
4555 | const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V), | ||||
4556 | DepClassTy::REQUIRED); | ||||
4557 | if (!Stripped && this == &AA) { | ||||
4558 | int64_t Offset; | ||||
4559 | unsigned Alignment = 1; | ||||
4560 | if (const Value *Base = | ||||
4561 | GetPointerBaseWithConstantOffset(&V, Offset, DL)) { | ||||
4562 | Align PA = Base->getPointerAlignment(DL); | ||||
4563 | // BasePointerAddr + Offset = Alignment * Q for some integer Q. | ||||
4564 | // So we can say that the maximum power of two which is a divisor of | ||||
4565 | // gcd(Offset, Alignment) is an alignment. | ||||
4566 | |||||
4567 | uint32_t gcd = greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), | ||||
4568 | uint32_t(PA.value())); | ||||
4569 | Alignment = llvm::PowerOf2Floor(gcd); | ||||
4570 | } else { | ||||
4571 | Alignment = V.getPointerAlignment(DL).value(); | ||||
4572 | } | ||||
4573 | // Use only IR information if we did not strip anything. | ||||
4574 | T.takeKnownMaximum(Alignment); | ||||
4575 | T.indicatePessimisticFixpoint(); | ||||
4576 | } else { | ||||
4577 | // Use abstract attribute information. | ||||
4578 | const AAAlign::StateType &DS = AA.getState(); | ||||
4579 | T ^= DS; | ||||
4580 | } | ||||
4581 | return T.isValidState(); | ||||
4582 | }; | ||||
4583 | |||||
4584 | StateType T; | ||||
4585 | bool UsedAssumedInformation = false; | ||||
4586 | if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T, | ||||
4587 | VisitValueCB, getCtxI(), | ||||
4588 | UsedAssumedInformation)) | ||||
4589 | return indicatePessimisticFixpoint(); | ||||
4590 | |||||
4591 | // TODO: If we know we visited all incoming values, thus no are assumed | ||||
4592 | // dead, we can take the known information from the state T. | ||||
4593 | return clampStateAndIndicateChange(getState(), T); | ||||
4594 | } | ||||
4595 | |||||
4596 | /// See AbstractAttribute::trackStatistics() | ||||
4597 | void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align){ static llvm::Statistic NumIRFloating_align = {"attributor", "NumIRFloating_align", ("Number of floating values known to be '" "align" "'")};; ++(NumIRFloating_align); } } | ||||
4598 | }; | ||||
4599 | |||||
4600 | /// Align attribute for function return value. | ||||
4601 | struct AAAlignReturned final | ||||
4602 | : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> { | ||||
4603 | using Base = AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>; | ||||
4604 | AAAlignReturned(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {} | ||||
4605 | |||||
4606 | /// See AbstractAttribute::initialize(...). | ||||
4607 | void initialize(Attributor &A) override { | ||||
4608 | Base::initialize(A); | ||||
4609 | Function *F = getAssociatedFunction(); | ||||
4610 | if (!F || F->isDeclaration()) | ||||
4611 | indicatePessimisticFixpoint(); | ||||
4612 | } | ||||
4613 | |||||
4614 | /// See AbstractAttribute::trackStatistics() | ||||
4615 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned){ static llvm::Statistic NumIRFunctionReturn_aligned = {"attributor" , "NumIRFunctionReturn_aligned", ("Number of " "function returns" " marked '" "aligned" "'")};; ++(NumIRFunctionReturn_aligned ); } } | ||||
4616 | }; | ||||
4617 | |||||
4618 | /// Align attribute for function argument. | ||||
4619 | struct AAAlignArgument final | ||||
4620 | : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> { | ||||
4621 | using Base = AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>; | ||||
4622 | AAAlignArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {} | ||||
4623 | |||||
4624 | /// See AbstractAttribute::manifest(...). | ||||
4625 | ChangeStatus manifest(Attributor &A) override { | ||||
4626 | // If the associated argument is involved in a must-tail call we give up | ||||
4627 | // because we would need to keep the argument alignments of caller and | ||||
4628 | // callee in-sync. Just does not seem worth the trouble right now. | ||||
4629 | if (A.getInfoCache().isInvolvedInMustTailCall(*getAssociatedArgument())) | ||||
4630 | return ChangeStatus::UNCHANGED; | ||||
4631 | return Base::manifest(A); | ||||
4632 | } | ||||
4633 | |||||
4634 | /// See AbstractAttribute::trackStatistics() | ||||
4635 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned){ static llvm::Statistic NumIRArguments_aligned = {"attributor" , "NumIRArguments_aligned", ("Number of " "arguments" " marked '" "aligned" "'")};; ++(NumIRArguments_aligned); } } | ||||
4636 | }; | ||||
4637 | |||||
4638 | struct AAAlignCallSiteArgument final : AAAlignFloating { | ||||
4639 | AAAlignCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
4640 | : AAAlignFloating(IRP, A) {} | ||||
4641 | |||||
4642 | /// See AbstractAttribute::manifest(...). | ||||
4643 | ChangeStatus manifest(Attributor &A) override { | ||||
4644 | // If the associated argument is involved in a must-tail call we give up | ||||
4645 | // because we would need to keep the argument alignments of caller and | ||||
4646 | // callee in-sync. Just does not seem worth the trouble right now. | ||||
4647 | if (Argument *Arg = getAssociatedArgument()) | ||||
4648 | if (A.getInfoCache().isInvolvedInMustTailCall(*Arg)) | ||||
4649 | return ChangeStatus::UNCHANGED; | ||||
4650 | ChangeStatus Changed = AAAlignImpl::manifest(A); | ||||
4651 | Align InheritAlign = | ||||
4652 | getAssociatedValue().getPointerAlignment(A.getDataLayout()); | ||||
4653 | if (InheritAlign >= getAssumedAlign()) | ||||
4654 | Changed = ChangeStatus::UNCHANGED; | ||||
4655 | return Changed; | ||||
4656 | } | ||||
4657 | |||||
4658 | /// See AbstractAttribute::updateImpl(Attributor &A). | ||||
4659 | ChangeStatus updateImpl(Attributor &A) override { | ||||
4660 | ChangeStatus Changed = AAAlignFloating::updateImpl(A); | ||||
4661 | if (Argument *Arg = getAssociatedArgument()) { | ||||
4662 | // We only take known information from the argument | ||||
4663 | // so we do not need to track a dependence. | ||||
4664 | const auto &ArgAlignAA = A.getAAFor<AAAlign>( | ||||
4665 | *this, IRPosition::argument(*Arg), DepClassTy::NONE); | ||||
4666 | takeKnownMaximum(ArgAlignAA.getKnownAlign()); | ||||
4667 | } | ||||
4668 | return Changed; | ||||
4669 | } | ||||
4670 | |||||
4671 | /// See AbstractAttribute::trackStatistics() | ||||
4672 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned){ static llvm::Statistic NumIRCSArguments_aligned = {"attributor" , "NumIRCSArguments_aligned", ("Number of " "call site arguments" " marked '" "aligned" "'")};; ++(NumIRCSArguments_aligned); } } | ||||
4673 | }; | ||||
4674 | |||||
4675 | /// Align attribute deduction for a call site return value. | ||||
4676 | struct AAAlignCallSiteReturned final | ||||
4677 | : AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl> { | ||||
4678 | using Base = AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl>; | ||||
4679 | AAAlignCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
4680 | : Base(IRP, A) {} | ||||
4681 | |||||
4682 | /// See AbstractAttribute::initialize(...). | ||||
4683 | void initialize(Attributor &A) override { | ||||
4684 | Base::initialize(A); | ||||
4685 | Function *F = getAssociatedFunction(); | ||||
4686 | if (!F || F->isDeclaration()) | ||||
4687 | indicatePessimisticFixpoint(); | ||||
4688 | } | ||||
4689 | |||||
4690 | /// See AbstractAttribute::trackStatistics() | ||||
4691 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align){ static llvm::Statistic NumIRCS_align = {"attributor", "NumIRCS_align" , ("Number of " "call site" " marked '" "align" "'")};; ++(NumIRCS_align ); }; } | ||||
4692 | }; | ||||
4693 | |||||
4694 | /// ------------------ Function No-Return Attribute ---------------------------- | ||||
4695 | struct AANoReturnImpl : public AANoReturn { | ||||
4696 | AANoReturnImpl(const IRPosition &IRP, Attributor &A) : AANoReturn(IRP, A) {} | ||||
4697 | |||||
4698 | /// See AbstractAttribute::initialize(...). | ||||
4699 | void initialize(Attributor &A) override { | ||||
4700 | AANoReturn::initialize(A); | ||||
4701 | Function *F = getAssociatedFunction(); | ||||
4702 | if (!F || F->isDeclaration()) | ||||
4703 | indicatePessimisticFixpoint(); | ||||
4704 | } | ||||
4705 | |||||
4706 | /// See AbstractAttribute::getAsStr(). | ||||
4707 | const std::string getAsStr() const override { | ||||
4708 | return getAssumed() ? "noreturn" : "may-return"; | ||||
4709 | } | ||||
4710 | |||||
4711 | /// See AbstractAttribute::updateImpl(Attributor &A). | ||||
4712 | virtual ChangeStatus updateImpl(Attributor &A) override { | ||||
4713 | auto CheckForNoReturn = [](Instruction &) { return false; }; | ||||
4714 | bool UsedAssumedInformation = false; | ||||
4715 | if (!A.checkForAllInstructions(CheckForNoReturn, *this, | ||||
4716 | {(unsigned)Instruction::Ret}, | ||||
4717 | UsedAssumedInformation)) | ||||
4718 | return indicatePessimisticFixpoint(); | ||||
4719 | return ChangeStatus::UNCHANGED; | ||||
4720 | } | ||||
4721 | }; | ||||
4722 | |||||
4723 | struct AANoReturnFunction final : AANoReturnImpl { | ||||
4724 | AANoReturnFunction(const IRPosition &IRP, Attributor &A) | ||||
4725 | : AANoReturnImpl(IRP, A) {} | ||||
4726 | |||||
4727 | /// See AbstractAttribute::trackStatistics() | ||||
4728 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn){ static llvm::Statistic NumIRFunction_noreturn = {"attributor" , "NumIRFunction_noreturn", ("Number of " "functions" " marked '" "noreturn" "'")};; ++(NumIRFunction_noreturn); } } | ||||
4729 | }; | ||||
4730 | |||||
4731 | /// NoReturn attribute deduction for a call sites. | ||||
4732 | struct AANoReturnCallSite final : AANoReturnImpl { | ||||
4733 | AANoReturnCallSite(const IRPosition &IRP, Attributor &A) | ||||
4734 | : AANoReturnImpl(IRP, A) {} | ||||
4735 | |||||
4736 | /// See AbstractAttribute::initialize(...). | ||||
4737 | void initialize(Attributor &A) override { | ||||
4738 | AANoReturnImpl::initialize(A); | ||||
4739 | if (Function *F = getAssociatedFunction()) { | ||||
4740 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
4741 | auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED); | ||||
4742 | if (!FnAA.isAssumedNoReturn()) | ||||
4743 | indicatePessimisticFixpoint(); | ||||
4744 | } | ||||
4745 | } | ||||
4746 | |||||
4747 | /// See AbstractAttribute::updateImpl(...). | ||||
4748 | ChangeStatus updateImpl(Attributor &A) override { | ||||
4749 | // TODO: Once we have call site specific value information we can provide | ||||
4750 | // call site specific liveness information and then it makes | ||||
4751 | // sense to specialize attributes for call sites arguments instead of | ||||
4752 | // redirecting requests to the callee argument. | ||||
4753 | Function *F = getAssociatedFunction(); | ||||
4754 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
4755 | auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED); | ||||
4756 | return clampStateAndIndicateChange(getState(), FnAA.getState()); | ||||
4757 | } | ||||
4758 | |||||
4759 | /// See AbstractAttribute::trackStatistics() | ||||
4760 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn){ static llvm::Statistic NumIRCS_noreturn = {"attributor", "NumIRCS_noreturn" , ("Number of " "call site" " marked '" "noreturn" "'")};; ++ (NumIRCS_noreturn); }; } | ||||
4761 | }; | ||||
4762 | |||||
4763 | /// ----------------------- Variable Capturing --------------------------------- | ||||
4764 | |||||
4765 | /// A class to hold the state of for no-capture attributes. | ||||
4766 | struct AANoCaptureImpl : public AANoCapture { | ||||
4767 | AANoCaptureImpl(const IRPosition &IRP, Attributor &A) : AANoCapture(IRP, A) {} | ||||
4768 | |||||
4769 | /// See AbstractAttribute::initialize(...). | ||||
4770 | void initialize(Attributor &A) override { | ||||
4771 | if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) { | ||||
4772 | indicateOptimisticFixpoint(); | ||||
4773 | return; | ||||
4774 | } | ||||
4775 | Function *AnchorScope = getAnchorScope(); | ||||
4776 | if (isFnInterfaceKind() && | ||||
4777 | (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) { | ||||
4778 | indicatePessimisticFixpoint(); | ||||
4779 | return; | ||||
4780 | } | ||||
4781 | |||||
4782 | // You cannot "capture" null in the default address space. | ||||
4783 | if (isa<ConstantPointerNull>(getAssociatedValue()) && | ||||
4784 | getAssociatedValue().getType()->getPointerAddressSpace() == 0) { | ||||
4785 | indicateOptimisticFixpoint(); | ||||
4786 | return; | ||||
4787 | } | ||||
4788 | |||||
4789 | const Function *F = | ||||
4790 | isArgumentPosition() ? getAssociatedFunction() : AnchorScope; | ||||
4791 | |||||
4792 | // Check what state the associated function can actually capture. | ||||
4793 | if (F) | ||||
4794 | determineFunctionCaptureCapabilities(getIRPosition(), *F, *this); | ||||
4795 | else | ||||
4796 | indicatePessimisticFixpoint(); | ||||
4797 | } | ||||
4798 | |||||
4799 | /// See AbstractAttribute::updateImpl(...). | ||||
4800 | ChangeStatus updateImpl(Attributor &A) override; | ||||
4801 | |||||
4802 | /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...). | ||||
4803 | virtual void | ||||
4804 | getDeducedAttributes(LLVMContext &Ctx, | ||||
4805 | SmallVectorImpl<Attribute> &Attrs) const override { | ||||
4806 | if (!isAssumedNoCaptureMaybeReturned()) | ||||
4807 | return; | ||||
4808 | |||||
4809 | if (isArgumentPosition()) { | ||||
4810 | if (isAssumedNoCapture()) | ||||
4811 | Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture)); | ||||
4812 | else if (ManifestInternal) | ||||
4813 | Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned")); | ||||
4814 | } | ||||
4815 | } | ||||
4816 | |||||
4817 | /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known | ||||
4818 | /// depending on the ability of the function associated with \p IRP to capture | ||||
4819 | /// state in memory and through "returning/throwing", respectively. | ||||
4820 | static void determineFunctionCaptureCapabilities(const IRPosition &IRP, | ||||
4821 | const Function &F, | ||||
4822 | BitIntegerState &State) { | ||||
4823 | // TODO: Once we have memory behavior attributes we should use them here. | ||||
4824 | |||||
4825 | // If we know we cannot communicate or write to memory, we do not care about | ||||
4826 | // ptr2int anymore. | ||||
4827 | if (F.onlyReadsMemory() && F.doesNotThrow() && | ||||
4828 | F.getReturnType()->isVoidTy()) { | ||||
4829 | State.addKnownBits(NO_CAPTURE); | ||||
4830 | return; | ||||
4831 | } | ||||
4832 | |||||
4833 | // A function cannot capture state in memory if it only reads memory, it can | ||||
4834 | // however return/throw state and the state might be influenced by the | ||||
4835 | // pointer value, e.g., loading from a returned pointer might reveal a bit. | ||||
4836 | if (F.onlyReadsMemory()) | ||||
4837 | State.addKnownBits(NOT_CAPTURED_IN_MEM); | ||||
4838 | |||||
4839 | // A function cannot communicate state back if it does not through | ||||
4840 | // exceptions and doesn not return values. | ||||
4841 | if (F.doesNotThrow() && F.getReturnType()->isVoidTy()) | ||||
4842 | State.addKnownBits(NOT_CAPTURED_IN_RET); | ||||
4843 | |||||
4844 | // Check existing "returned" attributes. | ||||
4845 | int ArgNo = IRP.getCalleeArgNo(); | ||||
4846 | if (F.doesNotThrow() && ArgNo >= 0) { | ||||
4847 | for (unsigned u = 0, e = F.arg_size(); u < e; ++u) | ||||
4848 | if (F.hasParamAttribute(u, Attribute::Returned)) { | ||||
4849 | if (u == unsigned(ArgNo)) | ||||
4850 | State.removeAssumedBits(NOT_CAPTURED_IN_RET); | ||||
4851 | else if (F.onlyReadsMemory()) | ||||
4852 | State.addKnownBits(NO_CAPTURE); | ||||
4853 | else | ||||
4854 | State.addKnownBits(NOT_CAPTURED_IN_RET); | ||||
4855 | break; | ||||
4856 | } | ||||
4857 | } | ||||
4858 | } | ||||
4859 | |||||
4860 | /// See AbstractState::getAsStr(). | ||||
4861 | const std::string getAsStr() const override { | ||||
4862 | if (isKnownNoCapture()) | ||||
4863 | return "known not-captured"; | ||||
4864 | if (isAssumedNoCapture()) | ||||
4865 | return "assumed not-captured"; | ||||
4866 | if (isKnownNoCaptureMaybeReturned()) | ||||
4867 | return "known not-captured-maybe-returned"; | ||||
4868 | if (isAssumedNoCaptureMaybeReturned()) | ||||
4869 | return "assumed not-captured-maybe-returned"; | ||||
4870 | return "assumed-captured"; | ||||
4871 | } | ||||
4872 | }; | ||||
4873 | |||||
4874 | /// Attributor-aware capture tracker. | ||||
4875 | struct AACaptureUseTracker final : public CaptureTracker { | ||||
4876 | |||||
4877 | /// Create a capture tracker that can lookup in-flight abstract attributes | ||||
4878 | /// through the Attributor \p A. | ||||
4879 | /// | ||||
4880 | /// If a use leads to a potential capture, \p CapturedInMemory is set and the | ||||
4881 | /// search is stopped. If a use leads to a return instruction, | ||||
4882 | /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed. | ||||
4883 | /// If a use leads to a ptr2int which may capture the value, | ||||
4884 | /// \p CapturedInInteger is set. If a use is found that is currently assumed | ||||
4885 | /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies | ||||
4886 | /// set. All values in \p PotentialCopies are later tracked as well. For every | ||||
4887 | /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0, | ||||
4888 | /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger | ||||
4889 | /// conservatively set to true. | ||||
4890 | AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA, | ||||
4891 | const AAIsDead &IsDeadAA, AANoCapture::StateType &State, | ||||
4892 | SmallSetVector<Value *, 4> &PotentialCopies, | ||||
4893 | unsigned &RemainingUsesToExplore) | ||||
4894 | : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State), | ||||
4895 | PotentialCopies(PotentialCopies), | ||||
4896 | RemainingUsesToExplore(RemainingUsesToExplore) {} | ||||
4897 | |||||
4898 | /// Determine if \p V maybe captured. *Also updates the state!* | ||||
4899 | bool valueMayBeCaptured(const Value *V) { | ||||
4900 | if (V->getType()->isPointerTy()) { | ||||
4901 | PointerMayBeCaptured(V, this); | ||||
4902 | } else { | ||||
4903 | State.indicatePessimisticFixpoint(); | ||||
4904 | } | ||||
4905 | return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); | ||||
4906 | } | ||||
4907 | |||||
4908 | /// See CaptureTracker::tooManyUses(). | ||||
4909 | void tooManyUses() override { | ||||
4910 | State.removeAssumedBits(AANoCapture::NO_CAPTURE); | ||||
4911 | } | ||||
4912 | |||||
4913 | bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override { | ||||
4914 | if (CaptureTracker::isDereferenceableOrNull(O, DL)) | ||||
4915 | return true; | ||||
4916 | const auto &DerefAA = A.getAAFor<AADereferenceable>( | ||||
4917 | NoCaptureAA, IRPosition::value(*O), DepClassTy::OPTIONAL); | ||||
4918 | return DerefAA.getAssumedDereferenceableBytes(); | ||||
4919 | } | ||||
4920 | |||||
4921 | /// See CaptureTracker::captured(...). | ||||
4922 | bool captured(const Use *U) override { | ||||
4923 | Instruction *UInst = cast<Instruction>(U->getUser()); | ||||
4924 | LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInstdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Check use: " << *U-> get() << " in " << *UInst << "\n"; } } while (false) | ||||
4925 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Check use: " << *U-> get() << " in " << *UInst << "\n"; } } while (false); | ||||
4926 | |||||
4927 | // Because we may reuse the tracker multiple times we keep track of the | ||||
4928 | // number of explored uses ourselves as well. | ||||
4929 | if (RemainingUsesToExplore-- == 0) { | ||||
4930 | LLVM_DEBUG(dbgs() << " - too many uses to explore!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << " - too many uses to explore!\n" ; } } while (false); | ||||
4931 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | ||||
4932 | /* Return */ true); | ||||
4933 | } | ||||
4934 | |||||
4935 | // Deal with ptr2int by following uses. | ||||
4936 | if (isa<PtrToIntInst>(UInst)) { | ||||
4937 | LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << " - ptr2int assume the worst!\n" ; } } while (false); | ||||
4938 | return valueMayBeCaptured(UInst); | ||||
4939 | } | ||||
4940 | |||||
4941 | // For stores we check if we can follow the value through memory or not. | ||||
4942 | if (auto *SI = dyn_cast<StoreInst>(UInst)) { | ||||
4943 | if (SI->isVolatile()) | ||||
4944 | return isCapturedIn(/* Memory */ true, /* Integer */ false, | ||||
4945 | /* Return */ false); | ||||
4946 | bool UsedAssumedInformation = false; | ||||
4947 | if (!AA::getPotentialCopiesOfStoredValue( | ||||
4948 | A, *SI, PotentialCopies, NoCaptureAA, UsedAssumedInformation)) | ||||
4949 | return isCapturedIn(/* Memory */ true, /* Integer */ false, | ||||
4950 | /* Return */ false); | ||||
4951 | // Not captured directly, potential copies will be checked. | ||||
4952 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | ||||
4953 | /* Return */ false); | ||||
4954 | } | ||||
4955 | |||||
4956 | // Explicitly catch return instructions. | ||||
4957 | if (isa<ReturnInst>(UInst)) { | ||||
4958 | if (UInst->getFunction() == NoCaptureAA.getAnchorScope()) | ||||
4959 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | ||||
4960 | /* Return */ true); | ||||
4961 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | ||||
4962 | /* Return */ true); | ||||
4963 | } | ||||
4964 | |||||
4965 | // For now we only use special logic for call sites. However, the tracker | ||||
4966 | // itself knows about a lot of other non-capturing cases already. | ||||
4967 | auto *CB = dyn_cast<CallBase>(UInst); | ||||
4968 | if (!CB || !CB->isArgOperand(U)) | ||||
4969 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | ||||
4970 | /* Return */ true); | ||||
4971 | |||||
4972 | unsigned ArgNo = CB->getArgOperandNo(U); | ||||
4973 | const IRPosition &CSArgPos = IRPosition::callsite_argument(*CB, ArgNo); | ||||
4974 | // If we have a abstract no-capture attribute for the argument we can use | ||||
4975 | // it to justify a non-capture attribute here. This allows recursion! | ||||
4976 | auto &ArgNoCaptureAA = | ||||
4977 | A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos, DepClassTy::REQUIRED); | ||||
4978 | if (ArgNoCaptureAA.isAssumedNoCapture()) | ||||
4979 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | ||||
4980 | /* Return */ false); | ||||
4981 | if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | ||||
4982 | addPotentialCopy(*CB); | ||||
4983 | return isCapturedIn(/* Memory */ false, /* Integer */ false, | ||||
4984 | /* Return */ false); | ||||
4985 | } | ||||
4986 | |||||
4987 | // Lastly, we could not find a reason no-capture can be assumed so we don't. | ||||
4988 | return isCapturedIn(/* Memory */ true, /* Integer */ true, | ||||
4989 | /* Return */ true); | ||||
4990 | } | ||||
4991 | |||||
4992 | /// Register \p CS as potential copy of the value we are checking. | ||||
4993 | void addPotentialCopy(CallBase &CB) { PotentialCopies.insert(&CB); } | ||||
4994 | |||||
4995 | /// See CaptureTracker::shouldExplore(...). | ||||
4996 | bool shouldExplore(const Use *U) override { | ||||
4997 | // Check liveness and ignore droppable users. | ||||
4998 | bool UsedAssumedInformation = false; | ||||
4999 | return !U->getUser()->isDroppable() && | ||||
5000 | !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA, | ||||
5001 | UsedAssumedInformation); | ||||
5002 | } | ||||
5003 | |||||
5004 | /// Update the state according to \p CapturedInMem, \p CapturedInInt, and | ||||
5005 | /// \p CapturedInRet, then return the appropriate value for use in the | ||||
5006 | /// CaptureTracker::captured() interface. | ||||
5007 | bool isCapturedIn(bool CapturedInMem, bool CapturedInInt, | ||||
5008 | bool CapturedInRet) { | ||||
5009 | LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << " - captures [Mem " << CapturedInMem << "|Int " << CapturedInInt << "|Ret " << CapturedInRet << "]\n"; } } while (false ) | ||||
5010 | << CapturedInInt << "|Ret " << CapturedInRet << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << " - captures [Mem " << CapturedInMem << "|Int " << CapturedInInt << "|Ret " << CapturedInRet << "]\n"; } } while (false ); | ||||
5011 | if (CapturedInMem) | ||||
5012 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM); | ||||
5013 | if (CapturedInInt) | ||||
5014 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT); | ||||
5015 | if (CapturedInRet) | ||||
5016 | State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET); | ||||
5017 | return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED); | ||||
5018 | } | ||||
5019 | |||||
5020 | private: | ||||
5021 | /// The attributor providing in-flight abstract attributes. | ||||
5022 | Attributor &A; | ||||
5023 | |||||
5024 | /// The abstract attribute currently updated. | ||||
5025 | AANoCapture &NoCaptureAA; | ||||
5026 | |||||
5027 | /// The abstract liveness state. | ||||
5028 | const AAIsDead &IsDeadAA; | ||||
5029 | |||||
5030 | /// The state currently updated. | ||||
5031 | AANoCapture::StateType &State; | ||||
5032 | |||||
5033 | /// Set of potential copies of the tracked value. | ||||
5034 | SmallSetVector<Value *, 4> &PotentialCopies; | ||||
5035 | |||||
5036 | /// Global counter to limit the number of explored uses. | ||||
5037 | unsigned &RemainingUsesToExplore; | ||||
5038 | }; | ||||
5039 | |||||
5040 | ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) { | ||||
5041 | const IRPosition &IRP = getIRPosition(); | ||||
5042 | Value *V = isArgumentPosition() ? IRP.getAssociatedArgument() | ||||
5043 | : &IRP.getAssociatedValue(); | ||||
5044 | if (!V) | ||||
5045 | return indicatePessimisticFixpoint(); | ||||
5046 | |||||
5047 | const Function *F = | ||||
5048 | isArgumentPosition() ? IRP.getAssociatedFunction() : IRP.getAnchorScope(); | ||||
5049 | assert(F && "Expected a function!")(static_cast <bool> (F && "Expected a function!" ) ? void (0) : __assert_fail ("F && \"Expected a function!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5049, __extension__ __PRETTY_FUNCTION__)); | ||||
5050 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
5051 | const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos, DepClassTy::NONE); | ||||
5052 | |||||
5053 | AANoCapture::StateType T; | ||||
5054 | |||||
5055 | // Readonly means we cannot capture through memory. | ||||
5056 | bool IsKnown; | ||||
5057 | if (AA::isAssumedReadOnly(A, FnPos, *this, IsKnown)) { | ||||
5058 | T.addKnownBits(NOT_CAPTURED_IN_MEM); | ||||
5059 | if (IsKnown) | ||||
5060 | addKnownBits(NOT_CAPTURED_IN_MEM); | ||||
5061 | } | ||||
5062 | |||||
5063 | // Make sure all returned values are different than the underlying value. | ||||
5064 | // TODO: we could do this in a more sophisticated way inside | ||||
5065 | // AAReturnedValues, e.g., track all values that escape through returns | ||||
5066 | // directly somehow. | ||||
5067 | auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) { | ||||
5068 | bool SeenConstant = false; | ||||
5069 | for (auto &It : RVAA.returned_values()) { | ||||
5070 | if (isa<Constant>(It.first)) { | ||||
5071 | if (SeenConstant) | ||||
5072 | return false; | ||||
5073 | SeenConstant = true; | ||||
5074 | } else if (!isa<Argument>(It.first) || | ||||
5075 | It.first == getAssociatedArgument()) | ||||
5076 | return false; | ||||
5077 | } | ||||
5078 | return true; | ||||
5079 | }; | ||||
5080 | |||||
5081 | const auto &NoUnwindAA = | ||||
5082 | A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::OPTIONAL); | ||||
5083 | if (NoUnwindAA.isAssumedNoUnwind()) { | ||||
5084 | bool IsVoidTy = F->getReturnType()->isVoidTy(); | ||||
5085 | const AAReturnedValues *RVAA = | ||||
5086 | IsVoidTy ? nullptr | ||||
5087 | : &A.getAAFor<AAReturnedValues>(*this, FnPos, | ||||
5088 | |||||
5089 | DepClassTy::OPTIONAL); | ||||
5090 | if (IsVoidTy || CheckReturnedArgs(*RVAA)) { | ||||
5091 | T.addKnownBits(NOT_CAPTURED_IN_RET); | ||||
5092 | if (T.isKnown(NOT_CAPTURED_IN_MEM)) | ||||
5093 | return ChangeStatus::UNCHANGED; | ||||
5094 | if (NoUnwindAA.isKnownNoUnwind() && | ||||
5095 | (IsVoidTy || RVAA->getState().isAtFixpoint())) { | ||||
5096 | addKnownBits(NOT_CAPTURED_IN_RET); | ||||
5097 | if (isKnown(NOT_CAPTURED_IN_MEM)) | ||||
5098 | return indicateOptimisticFixpoint(); | ||||
5099 | } | ||||
5100 | } | ||||
5101 | } | ||||
5102 | |||||
5103 | // Use the CaptureTracker interface and logic with the specialized tracker, | ||||
5104 | // defined in AACaptureUseTracker, that can look at in-flight abstract | ||||
5105 | // attributes and directly updates the assumed state. | ||||
5106 | SmallSetVector<Value *, 4> PotentialCopies; | ||||
5107 | unsigned RemainingUsesToExplore = | ||||
5108 | getDefaultMaxUsesToExploreForCaptureTracking(); | ||||
5109 | AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies, | ||||
5110 | RemainingUsesToExplore); | ||||
5111 | |||||
5112 | // Check all potential copies of the associated value until we can assume | ||||
5113 | // none will be captured or we have to assume at least one might be. | ||||
5114 | unsigned Idx = 0; | ||||
5115 | PotentialCopies.insert(V); | ||||
5116 | while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size()) | ||||
5117 | Tracker.valueMayBeCaptured(PotentialCopies[Idx++]); | ||||
5118 | |||||
5119 | AANoCapture::StateType &S = getState(); | ||||
5120 | auto Assumed = S.getAssumed(); | ||||
5121 | S.intersectAssumedBits(T.getAssumed()); | ||||
5122 | if (!isAssumedNoCaptureMaybeReturned()) | ||||
5123 | return indicatePessimisticFixpoint(); | ||||
5124 | return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED | ||||
5125 | : ChangeStatus::CHANGED; | ||||
5126 | } | ||||
5127 | |||||
5128 | /// NoCapture attribute for function arguments. | ||||
5129 | struct AANoCaptureArgument final : AANoCaptureImpl { | ||||
5130 | AANoCaptureArgument(const IRPosition &IRP, Attributor &A) | ||||
5131 | : AANoCaptureImpl(IRP, A) {} | ||||
5132 | |||||
5133 | /// See AbstractAttribute::trackStatistics() | ||||
5134 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture){ static llvm::Statistic NumIRArguments_nocapture = {"attributor" , "NumIRArguments_nocapture", ("Number of " "arguments" " marked '" "nocapture" "'")};; ++(NumIRArguments_nocapture); } } | ||||
5135 | }; | ||||
5136 | |||||
5137 | /// NoCapture attribute for call site arguments. | ||||
5138 | struct AANoCaptureCallSiteArgument final : AANoCaptureImpl { | ||||
5139 | AANoCaptureCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
5140 | : AANoCaptureImpl(IRP, A) {} | ||||
5141 | |||||
5142 | /// See AbstractAttribute::initialize(...). | ||||
5143 | void initialize(Attributor &A) override { | ||||
5144 | if (Argument *Arg = getAssociatedArgument()) | ||||
5145 | if (Arg->hasByValAttr()) | ||||
5146 | indicateOptimisticFixpoint(); | ||||
5147 | AANoCaptureImpl::initialize(A); | ||||
5148 | } | ||||
5149 | |||||
5150 | /// See AbstractAttribute::updateImpl(...). | ||||
5151 | ChangeStatus updateImpl(Attributor &A) override { | ||||
5152 | // TODO: Once we have call site specific value information we can provide | ||||
5153 | // call site specific liveness information and then it makes | ||||
5154 | // sense to specialize attributes for call sites arguments instead of | ||||
5155 | // redirecting requests to the callee argument. | ||||
5156 | Argument *Arg = getAssociatedArgument(); | ||||
5157 | if (!Arg) | ||||
5158 | return indicatePessimisticFixpoint(); | ||||
5159 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | ||||
5160 | auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos, DepClassTy::REQUIRED); | ||||
5161 | return clampStateAndIndicateChange(getState(), ArgAA.getState()); | ||||
5162 | } | ||||
5163 | |||||
5164 | /// See AbstractAttribute::trackStatistics() | ||||
5165 | void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture){ static llvm::Statistic NumIRCSArguments_nocapture = {"attributor" , "NumIRCSArguments_nocapture", ("Number of " "call site arguments" " marked '" "nocapture" "'")};; ++(NumIRCSArguments_nocapture ); }}; | ||||
5166 | }; | ||||
5167 | |||||
5168 | /// NoCapture attribute for floating values. | ||||
5169 | struct AANoCaptureFloating final : AANoCaptureImpl { | ||||
5170 | AANoCaptureFloating(const IRPosition &IRP, Attributor &A) | ||||
5171 | : AANoCaptureImpl(IRP, A) {} | ||||
5172 | |||||
5173 | /// See AbstractAttribute::trackStatistics() | ||||
5174 | void trackStatistics() const override { | ||||
5175 | STATS_DECLTRACK_FLOATING_ATTR(nocapture){ static llvm::Statistic NumIRFloating_nocapture = {"attributor" , "NumIRFloating_nocapture", ("Number of floating values known to be '" "nocapture" "'")};; ++(NumIRFloating_nocapture); } | ||||
5176 | } | ||||
5177 | }; | ||||
5178 | |||||
5179 | /// NoCapture attribute for function return value. | ||||
5180 | struct AANoCaptureReturned final : AANoCaptureImpl { | ||||
5181 | AANoCaptureReturned(const IRPosition &IRP, Attributor &A) | ||||
5182 | : AANoCaptureImpl(IRP, A) { | ||||
5183 | llvm_unreachable("NoCapture is not applicable to function returns!")::llvm::llvm_unreachable_internal("NoCapture is not applicable to function returns!" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5183); | ||||
5184 | } | ||||
5185 | |||||
5186 | /// See AbstractAttribute::initialize(...). | ||||
5187 | void initialize(Attributor &A) override { | ||||
5188 | llvm_unreachable("NoCapture is not applicable to function returns!")::llvm::llvm_unreachable_internal("NoCapture is not applicable to function returns!" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5188); | ||||
5189 | } | ||||
5190 | |||||
5191 | /// See AbstractAttribute::updateImpl(...). | ||||
5192 | ChangeStatus updateImpl(Attributor &A) override { | ||||
5193 | llvm_unreachable("NoCapture is not applicable to function returns!")::llvm::llvm_unreachable_internal("NoCapture is not applicable to function returns!" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5193); | ||||
5194 | } | ||||
5195 | |||||
5196 | /// See AbstractAttribute::trackStatistics() | ||||
5197 | void trackStatistics() const override {} | ||||
5198 | }; | ||||
5199 | |||||
5200 | /// NoCapture attribute deduction for a call site return value. | ||||
5201 | struct AANoCaptureCallSiteReturned final : AANoCaptureImpl { | ||||
5202 | AANoCaptureCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
5203 | : AANoCaptureImpl(IRP, A) {} | ||||
5204 | |||||
5205 | /// See AbstractAttribute::initialize(...). | ||||
5206 | void initialize(Attributor &A) override { | ||||
5207 | const Function *F = getAnchorScope(); | ||||
5208 | // Check what state the associated function can actually capture. | ||||
5209 | determineFunctionCaptureCapabilities(getIRPosition(), *F, *this); | ||||
5210 | } | ||||
5211 | |||||
5212 | /// See AbstractAttribute::trackStatistics() | ||||
5213 | void trackStatistics() const override { | ||||
5214 | STATS_DECLTRACK_CSRET_ATTR(nocapture){ static llvm::Statistic NumIRCSReturn_nocapture = {"attributor" , "NumIRCSReturn_nocapture", ("Number of " "call site returns" " marked '" "nocapture" "'")};; ++(NumIRCSReturn_nocapture); } | ||||
5215 | } | ||||
5216 | }; | ||||
5217 | |||||
5218 | /// ------------------ Value Simplify Attribute ---------------------------- | ||||
5219 | |||||
5220 | bool ValueSimplifyStateType::unionAssumed(Optional<Value *> Other) { | ||||
5221 | // FIXME: Add a typecast support. | ||||
5222 | SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice( | ||||
5223 | SimplifiedAssociatedValue, Other, Ty); | ||||
5224 | if (SimplifiedAssociatedValue == Optional<Value *>(nullptr)) | ||||
5225 | return false; | ||||
5226 | |||||
5227 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (SimplifiedAssociatedValue.hasValue()) dbgs() << "[ValueSimplify] is assumed to be " << **SimplifiedAssociatedValue << "\n"; else dbgs() << "[ValueSimplify] is assumed to be <none>\n"; }; } } while (false) | ||||
5228 | if (SimplifiedAssociatedValue.hasValue())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (SimplifiedAssociatedValue.hasValue()) dbgs() << "[ValueSimplify] is assumed to be " << **SimplifiedAssociatedValue << "\n"; else dbgs() << "[ValueSimplify] is assumed to be <none>\n"; }; } } while (false) | ||||
5229 | dbgs() << "[ValueSimplify] is assumed to be "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (SimplifiedAssociatedValue.hasValue()) dbgs() << "[ValueSimplify] is assumed to be " << **SimplifiedAssociatedValue << "\n"; else dbgs() << "[ValueSimplify] is assumed to be <none>\n"; }; } } while (false) | ||||
5230 | << **SimplifiedAssociatedValue << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (SimplifiedAssociatedValue.hasValue()) dbgs() << "[ValueSimplify] is assumed to be " << **SimplifiedAssociatedValue << "\n"; else dbgs() << "[ValueSimplify] is assumed to be <none>\n"; }; } } while (false) | ||||
5231 | elsedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (SimplifiedAssociatedValue.hasValue()) dbgs() << "[ValueSimplify] is assumed to be " << **SimplifiedAssociatedValue << "\n"; else dbgs() << "[ValueSimplify] is assumed to be <none>\n"; }; } } while (false) | ||||
5232 | dbgs() << "[ValueSimplify] is assumed to be <none>\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (SimplifiedAssociatedValue.hasValue()) dbgs() << "[ValueSimplify] is assumed to be " << **SimplifiedAssociatedValue << "\n"; else dbgs() << "[ValueSimplify] is assumed to be <none>\n"; }; } } while (false) | ||||
5233 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (SimplifiedAssociatedValue.hasValue()) dbgs() << "[ValueSimplify] is assumed to be " << **SimplifiedAssociatedValue << "\n"; else dbgs() << "[ValueSimplify] is assumed to be <none>\n"; }; } } while (false); | ||||
5234 | return true; | ||||
5235 | } | ||||
5236 | |||||
5237 | struct AAValueSimplifyImpl : AAValueSimplify { | ||||
5238 | AAValueSimplifyImpl(const IRPosition &IRP, Attributor &A) | ||||
5239 | : AAValueSimplify(IRP, A) {} | ||||
5240 | |||||
5241 | /// See AbstractAttribute::initialize(...). | ||||
5242 | void initialize(Attributor &A) override { | ||||
5243 | if (getAssociatedValue().getType()->isVoidTy()) | ||||
5244 | indicatePessimisticFixpoint(); | ||||
5245 | if (A.hasSimplificationCallback(getIRPosition())) | ||||
5246 | indicatePessimisticFixpoint(); | ||||
5247 | } | ||||
5248 | |||||
5249 | /// See AbstractAttribute::getAsStr(). | ||||
5250 | const std::string getAsStr() const override { | ||||
5251 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue << " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue ) errs() << "SAV: " << **SimplifiedAssociatedValue << " "; }; } } while (false) | ||||
5252 | errs() << "SAV: " << SimplifiedAssociatedValue << " ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue << " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue ) errs() << "SAV: " << **SimplifiedAssociatedValue << " "; }; } } while (false) | ||||
5253 | if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue << " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue ) errs() << "SAV: " << **SimplifiedAssociatedValue << " "; }; } } while (false) | ||||
5254 | errs() << "SAV: " << **SimplifiedAssociatedValue << " ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue << " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue ) errs() << "SAV: " << **SimplifiedAssociatedValue << " "; }; } } while (false) | ||||
5255 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue << " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue ) errs() << "SAV: " << **SimplifiedAssociatedValue << " "; }; } } while (false); | ||||
5256 | return isValidState() ? (isAtFixpoint() ? "simplified" : "maybe-simple") | ||||
5257 | : "not-simple"; | ||||
5258 | } | ||||
5259 | |||||
5260 | /// See AbstractAttribute::trackStatistics() | ||||
5261 | void trackStatistics() const override {} | ||||
5262 | |||||
5263 | /// See AAValueSimplify::getAssumedSimplifiedValue() | ||||
5264 | Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { | ||||
5265 | return SimplifiedAssociatedValue; | ||||
5266 | } | ||||
5267 | |||||
5268 | /// Return a value we can use as replacement for the associated one, or | ||||
5269 | /// nullptr if we don't have one that makes sense. | ||||
5270 | Value *getReplacementValue(Attributor &A) const { | ||||
5271 | Value *NewV; | ||||
5272 | NewV = SimplifiedAssociatedValue.hasValue() | ||||
5273 | ? SimplifiedAssociatedValue.getValue() | ||||
5274 | : UndefValue::get(getAssociatedType()); | ||||
5275 | if (!NewV) | ||||
5276 | return nullptr; | ||||
5277 | NewV = AA::getWithType(*NewV, *getAssociatedType()); | ||||
5278 | if (!NewV || NewV == &getAssociatedValue()) | ||||
5279 | return nullptr; | ||||
5280 | const Instruction *CtxI = getCtxI(); | ||||
5281 | if (CtxI && !AA::isValidAtPosition(*NewV, *CtxI, A.getInfoCache())) | ||||
5282 | return nullptr; | ||||
5283 | if (!CtxI && !AA::isValidInScope(*NewV, getAnchorScope())) | ||||
5284 | return nullptr; | ||||
5285 | return NewV; | ||||
5286 | } | ||||
5287 | |||||
5288 | /// Helper function for querying AAValueSimplify and updating candicate. | ||||
5289 | /// \param IRP The value position we are trying to unify with SimplifiedValue | ||||
5290 | bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA, | ||||
5291 | const IRPosition &IRP, bool Simplify = true) { | ||||
5292 | bool UsedAssumedInformation = false; | ||||
5293 | Optional<Value *> QueryingValueSimplified = &IRP.getAssociatedValue(); | ||||
5294 | if (Simplify) | ||||
5295 | QueryingValueSimplified = | ||||
5296 | A.getAssumedSimplified(IRP, QueryingAA, UsedAssumedInformation); | ||||
5297 | return unionAssumed(QueryingValueSimplified); | ||||
5298 | } | ||||
5299 | |||||
5300 | /// Returns a candidate is found or not | ||||
5301 | template <typename AAType> bool askSimplifiedValueFor(Attributor &A) { | ||||
5302 | if (!getAssociatedValue().getType()->isIntegerTy()) | ||||
5303 | return false; | ||||
5304 | |||||
5305 | // This will also pass the call base context. | ||||
5306 | const auto &AA = | ||||
5307 | A.getAAFor<AAType>(*this, getIRPosition(), DepClassTy::NONE); | ||||
5308 | |||||
5309 | Optional<ConstantInt *> COpt = AA.getAssumedConstantInt(A); | ||||
5310 | |||||
5311 | if (!COpt.hasValue()) { | ||||
5312 | SimplifiedAssociatedValue = llvm::None; | ||||
5313 | A.recordDependence(AA, *this, DepClassTy::OPTIONAL); | ||||
5314 | return true; | ||||
5315 | } | ||||
5316 | if (auto *C = COpt.getValue()) { | ||||
5317 | SimplifiedAssociatedValue = C; | ||||
5318 | A.recordDependence(AA, *this, DepClassTy::OPTIONAL); | ||||
5319 | return true; | ||||
5320 | } | ||||
5321 | return false; | ||||
5322 | } | ||||
5323 | |||||
5324 | bool askSimplifiedValueForOtherAAs(Attributor &A) { | ||||
5325 | if (askSimplifiedValueFor<AAValueConstantRange>(A)) | ||||
5326 | return true; | ||||
5327 | if (askSimplifiedValueFor<AAPotentialValues>(A)) | ||||
5328 | return true; | ||||
5329 | return false; | ||||
5330 | } | ||||
5331 | |||||
5332 | /// See AbstractAttribute::manifest(...). | ||||
5333 | ChangeStatus manifest(Attributor &A) override { | ||||
5334 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
5335 | if (getAssociatedValue().user_empty()) | ||||
5336 | return Changed; | ||||
5337 | |||||
5338 | if (auto *NewV = getReplacementValue(A)) { | ||||
5339 | LLVM_DEBUG(dbgs() << "[ValueSimplify] " << getAssociatedValue() << " -> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[ValueSimplify] " << getAssociatedValue() << " -> " << *NewV << " :: " << *this << "\n"; } } while (false) | ||||
5340 | << *NewV << " :: " << *this << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[ValueSimplify] " << getAssociatedValue() << " -> " << *NewV << " :: " << *this << "\n"; } } while (false); | ||||
5341 | if (A.changeValueAfterManifest(getAssociatedValue(), *NewV)) | ||||
5342 | Changed = ChangeStatus::CHANGED; | ||||
5343 | } | ||||
5344 | |||||
5345 | return Changed | AAValueSimplify::manifest(A); | ||||
5346 | } | ||||
5347 | |||||
5348 | /// See AbstractState::indicatePessimisticFixpoint(...). | ||||
5349 | ChangeStatus indicatePessimisticFixpoint() override { | ||||
5350 | SimplifiedAssociatedValue = &getAssociatedValue(); | ||||
5351 | return AAValueSimplify::indicatePessimisticFixpoint(); | ||||
5352 | } | ||||
5353 | |||||
5354 | static bool handleLoad(Attributor &A, const AbstractAttribute &AA, | ||||
5355 | LoadInst &L, function_ref<bool(Value &)> Union) { | ||||
5356 | auto UnionWrapper = [&](Value &V, Value &Obj) { | ||||
5357 | if (isa<AllocaInst>(Obj)) | ||||
5358 | return Union(V); | ||||
5359 | if (!AA::isDynamicallyUnique(A, AA, V)) | ||||
5360 | return false; | ||||
5361 | if (!AA::isValidAtPosition(V, L, A.getInfoCache())) | ||||
5362 | return false; | ||||
5363 | return Union(V); | ||||
5364 | }; | ||||
5365 | |||||
5366 | Value &Ptr = *L.getPointerOperand(); | ||||
5367 | SmallVector<Value *, 8> Objects; | ||||
5368 | bool UsedAssumedInformation = false; | ||||
5369 | if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, AA, &L, | ||||
5370 | UsedAssumedInformation)) | ||||
5371 | return false; | ||||
5372 | |||||
5373 | const auto *TLI = | ||||
5374 | A.getInfoCache().getTargetLibraryInfoForFunction(*L.getFunction()); | ||||
5375 | for (Value *Obj : Objects) { | ||||
5376 | LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Visit underlying object " << *Obj << "\n"; } } while (false); | ||||
5377 | if (isa<UndefValue>(Obj)) | ||||
5378 | continue; | ||||
5379 | if (isa<ConstantPointerNull>(Obj)) { | ||||
5380 | // A null pointer access can be undefined but any offset from null may | ||||
5381 | // be OK. We do not try to optimize the latter. | ||||
5382 | if (!NullPointerIsDefined(L.getFunction(), | ||||
5383 | Ptr.getType()->getPointerAddressSpace()) && | ||||
5384 | A.getAssumedSimplified(Ptr, AA, UsedAssumedInformation) == Obj) | ||||
5385 | continue; | ||||
5386 | return false; | ||||
5387 | } | ||||
5388 | Constant *InitialVal = AA::getInitialValueForObj(*Obj, *L.getType(), TLI); | ||||
5389 | if (!InitialVal || !Union(*InitialVal)) | ||||
5390 | return false; | ||||
5391 | |||||
5392 | LLVM_DEBUG(dbgs() << "Underlying object amenable to load-store "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Underlying object amenable to load-store " "propagation, checking accesses next.\n"; } } while (false) | ||||
5393 | "propagation, checking accesses next.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "Underlying object amenable to load-store " "propagation, checking accesses next.\n"; } } while (false); | ||||
5394 | |||||
5395 | auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) { | ||||
5396 | LLVM_DEBUG(dbgs() << " - visit access " << Acc << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << " - visit access " << Acc << "\n"; } } while (false); | ||||
5397 | if (Acc.isWrittenValueYetUndetermined()) | ||||
5398 | return true; | ||||
5399 | Value *Content = Acc.getWrittenValue(); | ||||
5400 | if (!Content) | ||||
5401 | return false; | ||||
5402 | Value *CastedContent = | ||||
5403 | AA::getWithType(*Content, *AA.getAssociatedType()); | ||||
5404 | if (!CastedContent) | ||||
5405 | return false; | ||||
5406 | if (IsExact) | ||||
5407 | return UnionWrapper(*CastedContent, *Obj); | ||||
5408 | if (auto *C = dyn_cast<Constant>(CastedContent)) | ||||
5409 | if (C->isNullValue() || C->isAllOnesValue() || isa<UndefValue>(C)) | ||||
5410 | return UnionWrapper(*CastedContent, *Obj); | ||||
5411 | return false; | ||||
5412 | }; | ||||
5413 | |||||
5414 | auto &PI = A.getAAFor<AAPointerInfo>(AA, IRPosition::value(*Obj), | ||||
5415 | DepClassTy::REQUIRED); | ||||
5416 | if (!PI.forallInterferingWrites(A, AA, L, CheckAccess)) | ||||
5417 | return false; | ||||
5418 | } | ||||
5419 | return true; | ||||
5420 | } | ||||
5421 | }; | ||||
5422 | |||||
5423 | struct AAValueSimplifyArgument final : AAValueSimplifyImpl { | ||||
5424 | AAValueSimplifyArgument(const IRPosition &IRP, Attributor &A) | ||||
5425 | : AAValueSimplifyImpl(IRP, A) {} | ||||
5426 | |||||
5427 | void initialize(Attributor &A) override { | ||||
5428 | AAValueSimplifyImpl::initialize(A); | ||||
5429 | if (!getAnchorScope() || getAnchorScope()->isDeclaration()) | ||||
5430 | indicatePessimisticFixpoint(); | ||||
5431 | if (hasAttr({Attribute::InAlloca, Attribute::Preallocated, | ||||
5432 | Attribute::StructRet, Attribute::Nest, Attribute::ByVal}, | ||||
5433 | /* IgnoreSubsumingPositions */ true)) | ||||
5434 | indicatePessimisticFixpoint(); | ||||
5435 | |||||
5436 | // FIXME: This is a hack to prevent us from propagating function poiner in | ||||
5437 | // the new pass manager CGSCC pass as it creates call edges the | ||||
5438 | // CallGraphUpdater cannot handle yet. | ||||
5439 | Value &V = getAssociatedValue(); | ||||
5440 | if (V.getType()->isPointerTy() && | ||||
5441 | V.getType()->getPointerElementType()->isFunctionTy() && | ||||
5442 | !A.isModulePass()) | ||||
5443 | indicatePessimisticFixpoint(); | ||||
5444 | } | ||||
5445 | |||||
5446 | /// See AbstractAttribute::updateImpl(...). | ||||
5447 | ChangeStatus updateImpl(Attributor &A) override { | ||||
5448 | // Byval is only replacable if it is readonly otherwise we would write into | ||||
5449 | // the replaced value and not the copy that byval creates implicitly. | ||||
5450 | Argument *Arg = getAssociatedArgument(); | ||||
5451 | if (Arg->hasByValAttr()) { | ||||
5452 | // TODO: We probably need to verify synchronization is not an issue, e.g., | ||||
5453 | // there is no race by not copying a constant byval. | ||||
5454 | bool IsKnown; | ||||
5455 | if (!AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown)) | ||||
5456 | return indicatePessimisticFixpoint(); | ||||
5457 | } | ||||
5458 | |||||
5459 | auto Before = SimplifiedAssociatedValue; | ||||
5460 | |||||
5461 | auto PredForCallSite = [&](AbstractCallSite ACS) { | ||||
5462 | const IRPosition &ACSArgPos = | ||||
5463 | IRPosition::callsite_argument(ACS, getCallSiteArgNo()); | ||||
5464 | // Check if a coresponding argument was found or if it is on not | ||||
5465 | // associated (which can happen for callback calls). | ||||
5466 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) | ||||
5467 | return false; | ||||
5468 | |||||
5469 | // Simplify the argument operand explicitly and check if the result is | ||||
5470 | // valid in the current scope. This avoids refering to simplified values | ||||
5471 | // in other functions, e.g., we don't want to say a an argument in a | ||||
5472 | // static function is actually an argument in a different function. | ||||
5473 | bool UsedAssumedInformation = false; | ||||
5474 | Optional<Constant *> SimpleArgOp = | ||||
5475 | A.getAssumedConstant(ACSArgPos, *this, UsedAssumedInformation); | ||||
5476 | if (!SimpleArgOp.hasValue()) | ||||
5477 | return true; | ||||
5478 | if (!SimpleArgOp.getValue()) | ||||
5479 | return false; | ||||
5480 | if (!AA::isDynamicallyUnique(A, *this, **SimpleArgOp)) | ||||
5481 | return false; | ||||
5482 | return unionAssumed(*SimpleArgOp); | ||||
5483 | }; | ||||
5484 | |||||
5485 | // Generate a answer specific to a call site context. | ||||
5486 | bool Success; | ||||
5487 | bool UsedAssumedInformation = false; | ||||
5488 | if (hasCallBaseContext() && | ||||
5489 | getCallBaseContext()->getCalledFunction() == Arg->getParent()) | ||||
5490 | Success = PredForCallSite( | ||||
5491 | AbstractCallSite(&getCallBaseContext()->getCalledOperandUse())); | ||||
5492 | else | ||||
5493 | Success = A.checkForAllCallSites(PredForCallSite, *this, true, | ||||
5494 | UsedAssumedInformation); | ||||
5495 | |||||
5496 | if (!Success) | ||||
5497 | if (!askSimplifiedValueForOtherAAs(A)) | ||||
5498 | return indicatePessimisticFixpoint(); | ||||
5499 | |||||
5500 | // If a candicate was found in this update, return CHANGED. | ||||
5501 | return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED | ||||
5502 | : ChangeStatus ::CHANGED; | ||||
5503 | } | ||||
5504 | |||||
5505 | /// See AbstractAttribute::trackStatistics() | ||||
5506 | void trackStatistics() const override { | ||||
5507 | STATS_DECLTRACK_ARG_ATTR(value_simplify){ static llvm::Statistic NumIRArguments_value_simplify = {"attributor" , "NumIRArguments_value_simplify", ("Number of " "arguments" " marked '" "value_simplify" "'")};; ++(NumIRArguments_value_simplify); } | ||||
5508 | } | ||||
5509 | }; | ||||
5510 | |||||
5511 | struct AAValueSimplifyReturned : AAValueSimplifyImpl { | ||||
5512 | AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A) | ||||
5513 | : AAValueSimplifyImpl(IRP, A) {} | ||||
5514 | |||||
5515 | /// See AAValueSimplify::getAssumedSimplifiedValue() | ||||
5516 | Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override { | ||||
5517 | if (!isValidState()) | ||||
5518 | return nullptr; | ||||
5519 | return SimplifiedAssociatedValue; | ||||
5520 | } | ||||
5521 | |||||
5522 | /// See AbstractAttribute::updateImpl(...). | ||||
5523 | ChangeStatus updateImpl(Attributor &A) override { | ||||
5524 | auto Before = SimplifiedAssociatedValue; | ||||
5525 | |||||
5526 | auto PredForReturned = [&](Value &V) { | ||||
5527 | return checkAndUpdate(A, *this, | ||||
5528 | IRPosition::value(V, getCallBaseContext())); | ||||
5529 | }; | ||||
5530 | |||||
5531 | if (!A.checkForAllReturnedValues(PredForReturned, *this)) | ||||
5532 | if (!askSimplifiedValueForOtherAAs(A)) | ||||
5533 | return indicatePessimisticFixpoint(); | ||||
5534 | |||||
5535 | // If a candicate was found in this update, return CHANGED. | ||||
5536 | return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED | ||||
5537 | : ChangeStatus ::CHANGED; | ||||
5538 | } | ||||
5539 | |||||
5540 | ChangeStatus manifest(Attributor &A) override { | ||||
5541 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
5542 | |||||
5543 | if (auto *NewV = getReplacementValue(A)) { | ||||
5544 | auto PredForReturned = | ||||
5545 | [&](Value &, const SmallSetVector<ReturnInst *, 4> &RetInsts) { | ||||
5546 | for (ReturnInst *RI : RetInsts) { | ||||
5547 | Value *ReturnedVal = RI->getReturnValue(); | ||||
5548 | if (ReturnedVal == NewV || isa<UndefValue>(ReturnedVal)) | ||||
5549 | return true; | ||||
5550 | assert(RI->getFunction() == getAnchorScope() &&(static_cast <bool> (RI->getFunction() == getAnchorScope () && "ReturnInst in wrong function!") ? void (0) : __assert_fail ("RI->getFunction() == getAnchorScope() && \"ReturnInst in wrong function!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5551, __extension__ __PRETTY_FUNCTION__)) | ||||
5551 | "ReturnInst in wrong function!")(static_cast <bool> (RI->getFunction() == getAnchorScope () && "ReturnInst in wrong function!") ? void (0) : __assert_fail ("RI->getFunction() == getAnchorScope() && \"ReturnInst in wrong function!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5551, __extension__ __PRETTY_FUNCTION__)); | ||||
5552 | LLVM_DEBUG(dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[ValueSimplify] " << *ReturnedVal << " -> " << *NewV << " in " << *RI << " :: " << *this << "\n"; } } while (false) | ||||
5553 | << "[ValueSimplify] " << *ReturnedVal << " -> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[ValueSimplify] " << *ReturnedVal << " -> " << *NewV << " in " << *RI << " :: " << *this << "\n"; } } while (false) | ||||
5554 | << *NewV << " in " << *RI << " :: " << *this << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[ValueSimplify] " << *ReturnedVal << " -> " << *NewV << " in " << *RI << " :: " << *this << "\n"; } } while (false); | ||||
5555 | if (A.changeUseAfterManifest(RI->getOperandUse(0), *NewV)) | ||||
5556 | Changed = ChangeStatus::CHANGED; | ||||
5557 | } | ||||
5558 | return true; | ||||
5559 | }; | ||||
5560 | A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this); | ||||
5561 | } | ||||
5562 | |||||
5563 | return Changed | AAValueSimplify::manifest(A); | ||||
5564 | } | ||||
5565 | |||||
5566 | /// See AbstractAttribute::trackStatistics() | ||||
5567 | void trackStatistics() const override { | ||||
5568 | STATS_DECLTRACK_FNRET_ATTR(value_simplify){ static llvm::Statistic NumIRFunctionReturn_value_simplify = {"attributor", "NumIRFunctionReturn_value_simplify", ("Number of " "function returns" " marked '" "value_simplify" "'")};; ++(NumIRFunctionReturn_value_simplify ); } | ||||
5569 | } | ||||
5570 | }; | ||||
5571 | |||||
5572 | struct AAValueSimplifyFloating : AAValueSimplifyImpl { | ||||
5573 | AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A) | ||||
5574 | : AAValueSimplifyImpl(IRP, A) {} | ||||
5575 | |||||
5576 | /// See AbstractAttribute::initialize(...). | ||||
5577 | void initialize(Attributor &A) override { | ||||
5578 | AAValueSimplifyImpl::initialize(A); | ||||
5579 | Value &V = getAnchorValue(); | ||||
5580 | |||||
5581 | // TODO: add other stuffs | ||||
5582 | if (isa<Constant>(V)) | ||||
5583 | indicatePessimisticFixpoint(); | ||||
5584 | } | ||||
5585 | |||||
5586 | /// Check if \p Cmp is a comparison we can simplify. | ||||
5587 | /// | ||||
5588 | /// We handle multiple cases, one in which at least one operand is an | ||||
5589 | /// (assumed) nullptr. If so, try to simplify it using AANonNull on the other | ||||
5590 | /// operand. Return true if successful, in that case SimplifiedAssociatedValue | ||||
5591 | /// will be updated. | ||||
5592 | bool handleCmp(Attributor &A, CmpInst &Cmp) { | ||||
5593 | auto Union = [&](Value &V) { | ||||
5594 | SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice( | ||||
5595 | SimplifiedAssociatedValue, &V, V.getType()); | ||||
5596 | return SimplifiedAssociatedValue != Optional<Value *>(nullptr); | ||||
5597 | }; | ||||
5598 | |||||
5599 | Value *LHS = Cmp.getOperand(0); | ||||
5600 | Value *RHS = Cmp.getOperand(1); | ||||
5601 | |||||
5602 | // Simplify the operands first. | ||||
5603 | bool UsedAssumedInformation = false; | ||||
5604 | const auto &SimplifiedLHS = | ||||
5605 | A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), | ||||
5606 | *this, UsedAssumedInformation); | ||||
5607 | if (!SimplifiedLHS.hasValue()) | ||||
5608 | return true; | ||||
5609 | if (!SimplifiedLHS.getValue()) | ||||
5610 | return false; | ||||
5611 | LHS = *SimplifiedLHS; | ||||
5612 | |||||
5613 | const auto &SimplifiedRHS = | ||||
5614 | A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), | ||||
5615 | *this, UsedAssumedInformation); | ||||
5616 | if (!SimplifiedRHS.hasValue()) | ||||
5617 | return true; | ||||
5618 | if (!SimplifiedRHS.getValue()) | ||||
5619 | return false; | ||||
5620 | RHS = *SimplifiedRHS; | ||||
5621 | |||||
5622 | LLVMContext &Ctx = Cmp.getContext(); | ||||
5623 | // Handle the trivial case first in which we don't even need to think about | ||||
5624 | // null or non-null. | ||||
5625 | if (LHS == RHS && (Cmp.isTrueWhenEqual() || Cmp.isFalseWhenEqual())) { | ||||
5626 | Constant *NewVal = | ||||
5627 | ConstantInt::get(Type::getInt1Ty(Ctx), Cmp.isTrueWhenEqual()); | ||||
5628 | if (!Union(*NewVal)) | ||||
5629 | return false; | ||||
5630 | if (!UsedAssumedInformation) | ||||
5631 | indicateOptimisticFixpoint(); | ||||
5632 | return true; | ||||
5633 | } | ||||
5634 | |||||
5635 | // From now on we only handle equalities (==, !=). | ||||
5636 | ICmpInst *ICmp = dyn_cast<ICmpInst>(&Cmp); | ||||
5637 | if (!ICmp || !ICmp->isEquality()) | ||||
5638 | return false; | ||||
5639 | |||||
5640 | bool LHSIsNull = isa<ConstantPointerNull>(LHS); | ||||
5641 | bool RHSIsNull = isa<ConstantPointerNull>(RHS); | ||||
5642 | if (!LHSIsNull && !RHSIsNull) | ||||
5643 | return false; | ||||
5644 | |||||
5645 | // Left is the nullptr ==/!= non-nullptr case. We'll use AANonNull on the | ||||
5646 | // non-nullptr operand and if we assume it's non-null we can conclude the | ||||
5647 | // result of the comparison. | ||||
5648 | assert((LHSIsNull || RHSIsNull) &&(static_cast <bool> ((LHSIsNull || RHSIsNull) && "Expected nullptr versus non-nullptr comparison at this point" ) ? void (0) : __assert_fail ("(LHSIsNull || RHSIsNull) && \"Expected nullptr versus non-nullptr comparison at this point\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5649, __extension__ __PRETTY_FUNCTION__)) | ||||
5649 | "Expected nullptr versus non-nullptr comparison at this point")(static_cast <bool> ((LHSIsNull || RHSIsNull) && "Expected nullptr versus non-nullptr comparison at this point" ) ? void (0) : __assert_fail ("(LHSIsNull || RHSIsNull) && \"Expected nullptr versus non-nullptr comparison at this point\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5649, __extension__ __PRETTY_FUNCTION__)); | ||||
5650 | |||||
5651 | // The index is the operand that we assume is not null. | ||||
5652 | unsigned PtrIdx = LHSIsNull; | ||||
5653 | auto &PtrNonNullAA = A.getAAFor<AANonNull>( | ||||
5654 | *this, IRPosition::value(*ICmp->getOperand(PtrIdx)), | ||||
5655 | DepClassTy::REQUIRED); | ||||
5656 | if (!PtrNonNullAA.isAssumedNonNull()) | ||||
5657 | return false; | ||||
5658 | UsedAssumedInformation |= !PtrNonNullAA.isKnownNonNull(); | ||||
5659 | |||||
5660 | // The new value depends on the predicate, true for != and false for ==. | ||||
5661 | Constant *NewVal = ConstantInt::get( | ||||
5662 | Type::getInt1Ty(Ctx), ICmp->getPredicate() == CmpInst::ICMP_NE); | ||||
5663 | if (!Union(*NewVal)) | ||||
5664 | return false; | ||||
5665 | |||||
5666 | if (!UsedAssumedInformation) | ||||
5667 | indicateOptimisticFixpoint(); | ||||
5668 | |||||
5669 | return true; | ||||
5670 | } | ||||
5671 | |||||
5672 | bool updateWithLoad(Attributor &A, LoadInst &L) { | ||||
5673 | auto Union = [&](Value &V) { | ||||
5674 | SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice( | ||||
5675 | SimplifiedAssociatedValue, &V, L.getType()); | ||||
5676 | return SimplifiedAssociatedValue != Optional<Value *>(nullptr); | ||||
5677 | }; | ||||
5678 | return handleLoad(A, *this, L, Union); | ||||
5679 | } | ||||
5680 | |||||
5681 | /// Use the generic, non-optimistic InstSimplfy functionality if we managed to | ||||
5682 | /// simplify any operand of the instruction \p I. Return true if successful, | ||||
5683 | /// in that case SimplifiedAssociatedValue will be updated. | ||||
5684 | bool handleGenericInst(Attributor &A, Instruction &I) { | ||||
5685 | bool SomeSimplified = false; | ||||
5686 | bool UsedAssumedInformation = false; | ||||
5687 | |||||
5688 | SmallVector<Value *, 8> NewOps(I.getNumOperands()); | ||||
5689 | int Idx = 0; | ||||
5690 | for (Value *Op : I.operands()) { | ||||
5691 | const auto &SimplifiedOp = | ||||
5692 | A.getAssumedSimplified(IRPosition::value(*Op, getCallBaseContext()), | ||||
5693 | *this, UsedAssumedInformation); | ||||
5694 | // If we are not sure about any operand we are not sure about the entire | ||||
5695 | // instruction, we'll wait. | ||||
5696 | if (!SimplifiedOp.hasValue()) | ||||
5697 | return true; | ||||
5698 | |||||
5699 | if (SimplifiedOp.getValue()) | ||||
5700 | NewOps[Idx] = SimplifiedOp.getValue(); | ||||
5701 | else | ||||
5702 | NewOps[Idx] = Op; | ||||
5703 | |||||
5704 | SomeSimplified |= (NewOps[Idx] != Op); | ||||
5705 | ++Idx; | ||||
5706 | } | ||||
5707 | |||||
5708 | // We won't bother with the InstSimplify interface if we didn't simplify any | ||||
5709 | // operand ourselves. | ||||
5710 | if (!SomeSimplified) | ||||
5711 | return false; | ||||
5712 | |||||
5713 | InformationCache &InfoCache = A.getInfoCache(); | ||||
5714 | Function *F = I.getFunction(); | ||||
5715 | const auto *DT = | ||||
5716 | InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F); | ||||
5717 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | ||||
5718 | auto *AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F); | ||||
5719 | OptimizationRemarkEmitter *ORE = nullptr; | ||||
5720 | |||||
5721 | const DataLayout &DL = I.getModule()->getDataLayout(); | ||||
5722 | SimplifyQuery Q(DL, TLI, DT, AC, &I); | ||||
5723 | if (Value *SimplifiedI = | ||||
5724 | SimplifyInstructionWithOperands(&I, NewOps, Q, ORE)) { | ||||
5725 | SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice( | ||||
5726 | SimplifiedAssociatedValue, SimplifiedI, I.getType()); | ||||
5727 | return SimplifiedAssociatedValue != Optional<Value *>(nullptr); | ||||
5728 | } | ||||
5729 | return false; | ||||
5730 | } | ||||
5731 | |||||
5732 | /// See AbstractAttribute::updateImpl(...). | ||||
5733 | ChangeStatus updateImpl(Attributor &A) override { | ||||
5734 | auto Before = SimplifiedAssociatedValue; | ||||
5735 | |||||
5736 | auto VisitValueCB = [&](Value &V, const Instruction *CtxI, bool &, | ||||
5737 | bool Stripped) -> bool { | ||||
5738 | auto &AA = A.getAAFor<AAValueSimplify>( | ||||
5739 | *this, IRPosition::value(V, getCallBaseContext()), | ||||
5740 | DepClassTy::REQUIRED); | ||||
5741 | if (!Stripped && this == &AA) { | ||||
5742 | |||||
5743 | if (auto *I = dyn_cast<Instruction>(&V)) { | ||||
5744 | if (auto *LI = dyn_cast<LoadInst>(&V)) | ||||
5745 | if (updateWithLoad(A, *LI)) | ||||
5746 | return true; | ||||
5747 | if (auto *Cmp = dyn_cast<CmpInst>(&V)) | ||||
5748 | if (handleCmp(A, *Cmp)) | ||||
5749 | return true; | ||||
5750 | if (handleGenericInst(A, *I)) | ||||
5751 | return true; | ||||
5752 | } | ||||
5753 | // TODO: Look the instruction and check recursively. | ||||
5754 | |||||
5755 | LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << Vdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[ValueSimplify] Can't be stripped more : " << V << "\n"; } } while (false) | ||||
5756 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[ValueSimplify] Can't be stripped more : " << V << "\n"; } } while (false); | ||||
5757 | return false; | ||||
5758 | } | ||||
5759 | return checkAndUpdate(A, *this, | ||||
5760 | IRPosition::value(V, getCallBaseContext())); | ||||
5761 | }; | ||||
5762 | |||||
5763 | bool Dummy = false; | ||||
5764 | bool UsedAssumedInformation = false; | ||||
5765 | if (!genericValueTraversal<bool>(A, getIRPosition(), *this, Dummy, | ||||
5766 | VisitValueCB, getCtxI(), | ||||
5767 | UsedAssumedInformation, | ||||
5768 | /* UseValueSimplify */ false)) | ||||
5769 | if (!askSimplifiedValueForOtherAAs(A)) | ||||
5770 | return indicatePessimisticFixpoint(); | ||||
5771 | |||||
5772 | // If a candicate was found in this update, return CHANGED. | ||||
5773 | return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED | ||||
5774 | : ChangeStatus ::CHANGED; | ||||
5775 | } | ||||
5776 | |||||
5777 | /// See AbstractAttribute::trackStatistics() | ||||
5778 | void trackStatistics() const override { | ||||
5779 | STATS_DECLTRACK_FLOATING_ATTR(value_simplify){ static llvm::Statistic NumIRFloating_value_simplify = {"attributor" , "NumIRFloating_value_simplify", ("Number of floating values known to be '" "value_simplify" "'")};; ++(NumIRFloating_value_simplify); } | ||||
5780 | } | ||||
5781 | }; | ||||
5782 | |||||
5783 | struct AAValueSimplifyFunction : AAValueSimplifyImpl { | ||||
5784 | AAValueSimplifyFunction(const IRPosition &IRP, Attributor &A) | ||||
5785 | : AAValueSimplifyImpl(IRP, A) {} | ||||
5786 | |||||
5787 | /// See AbstractAttribute::initialize(...). | ||||
5788 | void initialize(Attributor &A) override { | ||||
5789 | SimplifiedAssociatedValue = nullptr; | ||||
5790 | indicateOptimisticFixpoint(); | ||||
5791 | } | ||||
5792 | /// See AbstractAttribute::initialize(...). | ||||
5793 | ChangeStatus updateImpl(Attributor &A) override { | ||||
5794 | llvm_unreachable(::llvm::llvm_unreachable_internal("AAValueSimplify(Function|CallSite)::updateImpl will not be called" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5795) | ||||
5795 | "AAValueSimplify(Function|CallSite)::updateImpl will not be called")::llvm::llvm_unreachable_internal("AAValueSimplify(Function|CallSite)::updateImpl will not be called" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5795); | ||||
5796 | } | ||||
5797 | /// See AbstractAttribute::trackStatistics() | ||||
5798 | void trackStatistics() const override { | ||||
5799 | STATS_DECLTRACK_FN_ATTR(value_simplify){ static llvm::Statistic NumIRFunction_value_simplify = {"attributor" , "NumIRFunction_value_simplify", ("Number of " "functions" " marked '" "value_simplify" "'")};; ++(NumIRFunction_value_simplify); } | ||||
5800 | } | ||||
5801 | }; | ||||
5802 | |||||
5803 | struct AAValueSimplifyCallSite : AAValueSimplifyFunction { | ||||
5804 | AAValueSimplifyCallSite(const IRPosition &IRP, Attributor &A) | ||||
5805 | : AAValueSimplifyFunction(IRP, A) {} | ||||
5806 | /// See AbstractAttribute::trackStatistics() | ||||
5807 | void trackStatistics() const override { | ||||
5808 | STATS_DECLTRACK_CS_ATTR(value_simplify){ static llvm::Statistic NumIRCS_value_simplify = {"attributor" , "NumIRCS_value_simplify", ("Number of " "call site" " marked '" "value_simplify" "'")};; ++(NumIRCS_value_simplify); } | ||||
5809 | } | ||||
5810 | }; | ||||
5811 | |||||
5812 | struct AAValueSimplifyCallSiteReturned : AAValueSimplifyImpl { | ||||
5813 | AAValueSimplifyCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
5814 | : AAValueSimplifyImpl(IRP, A) {} | ||||
5815 | |||||
5816 | void initialize(Attributor &A) override { | ||||
5817 | AAValueSimplifyImpl::initialize(A); | ||||
5818 | if (!getAssociatedFunction()) | ||||
5819 | indicatePessimisticFixpoint(); | ||||
5820 | } | ||||
5821 | |||||
5822 | /// See AbstractAttribute::updateImpl(...). | ||||
5823 | ChangeStatus updateImpl(Attributor &A) override { | ||||
5824 | auto Before = SimplifiedAssociatedValue; | ||||
5825 | auto &RetAA = A.getAAFor<AAReturnedValues>( | ||||
5826 | *this, IRPosition::function(*getAssociatedFunction()), | ||||
5827 | DepClassTy::REQUIRED); | ||||
5828 | auto PredForReturned = | ||||
5829 | [&](Value &RetVal, const SmallSetVector<ReturnInst *, 4> &RetInsts) { | ||||
5830 | bool UsedAssumedInformation = false; | ||||
5831 | Optional<Value *> CSRetVal = A.translateArgumentToCallSiteContent( | ||||
5832 | &RetVal, *cast<CallBase>(getCtxI()), *this, | ||||
5833 | UsedAssumedInformation); | ||||
5834 | SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice( | ||||
5835 | SimplifiedAssociatedValue, CSRetVal, getAssociatedType()); | ||||
5836 | return SimplifiedAssociatedValue != Optional<Value *>(nullptr); | ||||
5837 | }; | ||||
5838 | if (!RetAA.checkForAllReturnedValuesAndReturnInsts(PredForReturned)) | ||||
5839 | if (!askSimplifiedValueForOtherAAs(A)) | ||||
5840 | return indicatePessimisticFixpoint(); | ||||
5841 | return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED | ||||
5842 | : ChangeStatus ::CHANGED; | ||||
5843 | } | ||||
5844 | |||||
5845 | void trackStatistics() const override { | ||||
5846 | STATS_DECLTRACK_CSRET_ATTR(value_simplify){ static llvm::Statistic NumIRCSReturn_value_simplify = {"attributor" , "NumIRCSReturn_value_simplify", ("Number of " "call site returns" " marked '" "value_simplify" "'")};; ++(NumIRCSReturn_value_simplify ); } | ||||
5847 | } | ||||
5848 | }; | ||||
5849 | |||||
5850 | struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating { | ||||
5851 | AAValueSimplifyCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
5852 | : AAValueSimplifyFloating(IRP, A) {} | ||||
5853 | |||||
5854 | /// See AbstractAttribute::manifest(...). | ||||
5855 | ChangeStatus manifest(Attributor &A) override { | ||||
5856 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
5857 | |||||
5858 | if (auto *NewV = getReplacementValue(A)) { | ||||
5859 | Use &U = cast<CallBase>(&getAnchorValue()) | ||||
5860 | ->getArgOperandUse(getCallSiteArgNo()); | ||||
5861 | if (A.changeUseAfterManifest(U, *NewV)) | ||||
5862 | Changed = ChangeStatus::CHANGED; | ||||
5863 | } | ||||
5864 | |||||
5865 | return Changed | AAValueSimplify::manifest(A); | ||||
5866 | } | ||||
5867 | |||||
5868 | void trackStatistics() const override { | ||||
5869 | STATS_DECLTRACK_CSARG_ATTR(value_simplify){ static llvm::Statistic NumIRCSArguments_value_simplify = {"attributor" , "NumIRCSArguments_value_simplify", ("Number of " "call site arguments" " marked '" "value_simplify" "'")};; ++(NumIRCSArguments_value_simplify ); } | ||||
5870 | } | ||||
5871 | }; | ||||
5872 | |||||
5873 | /// ----------------------- Heap-To-Stack Conversion --------------------------- | ||||
5874 | struct AAHeapToStackFunction final : public AAHeapToStack { | ||||
5875 | |||||
5876 | struct AllocationInfo { | ||||
5877 | /// The call that allocates the memory. | ||||
5878 | CallBase *const CB; | ||||
5879 | |||||
5880 | /// The library function id for the allocation. | ||||
5881 | LibFunc LibraryFunctionId = NotLibFunc; | ||||
5882 | |||||
5883 | /// The status wrt. a rewrite. | ||||
5884 | enum { | ||||
5885 | STACK_DUE_TO_USE, | ||||
5886 | STACK_DUE_TO_FREE, | ||||
5887 | INVALID, | ||||
5888 | } Status = STACK_DUE_TO_USE; | ||||
5889 | |||||
5890 | /// Flag to indicate if we encountered a use that might free this allocation | ||||
5891 | /// but which is not in the deallocation infos. | ||||
5892 | bool HasPotentiallyFreeingUnknownUses = false; | ||||
5893 | |||||
5894 | /// The set of free calls that use this allocation. | ||||
5895 | SmallPtrSet<CallBase *, 1> PotentialFreeCalls{}; | ||||
5896 | }; | ||||
5897 | |||||
5898 | struct DeallocationInfo { | ||||
5899 | /// The call that deallocates the memory. | ||||
5900 | CallBase *const CB; | ||||
5901 | |||||
5902 | /// Flag to indicate if we don't know all objects this deallocation might | ||||
5903 | /// free. | ||||
5904 | bool MightFreeUnknownObjects = false; | ||||
5905 | |||||
5906 | /// The set of allocation calls that are potentially freed. | ||||
5907 | SmallPtrSet<CallBase *, 1> PotentialAllocationCalls{}; | ||||
5908 | }; | ||||
5909 | |||||
5910 | AAHeapToStackFunction(const IRPosition &IRP, Attributor &A) | ||||
5911 | : AAHeapToStack(IRP, A) {} | ||||
5912 | |||||
5913 | ~AAHeapToStackFunction() { | ||||
5914 | // Ensure we call the destructor so we release any memory allocated in the | ||||
5915 | // sets. | ||||
5916 | for (auto &It : AllocationInfos) | ||||
5917 | It.getSecond()->~AllocationInfo(); | ||||
5918 | for (auto &It : DeallocationInfos) | ||||
5919 | It.getSecond()->~DeallocationInfo(); | ||||
5920 | } | ||||
5921 | |||||
5922 | void initialize(Attributor &A) override { | ||||
5923 | AAHeapToStack::initialize(A); | ||||
5924 | |||||
5925 | const Function *F = getAnchorScope(); | ||||
5926 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | ||||
5927 | |||||
5928 | auto AllocationIdentifierCB = [&](Instruction &I) { | ||||
5929 | CallBase *CB = dyn_cast<CallBase>(&I); | ||||
5930 | if (!CB) | ||||
5931 | return true; | ||||
5932 | if (isFreeCall(CB, TLI)) { | ||||
5933 | DeallocationInfos[CB] = new (A.Allocator) DeallocationInfo{CB}; | ||||
5934 | return true; | ||||
5935 | } | ||||
5936 | // To do heap to stack, we need to know that the allocation itself is | ||||
5937 | // removable once uses are rewritten, and that we can initialize the | ||||
5938 | // alloca to the same pattern as the original allocation result. | ||||
5939 | if (isAllocationFn(CB, TLI) && isAllocRemovable(CB, TLI)) { | ||||
5940 | auto *I8Ty = Type::getInt8Ty(CB->getParent()->getContext()); | ||||
5941 | if (nullptr != getInitialValueOfAllocation(CB, TLI, I8Ty)) { | ||||
5942 | AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB}; | ||||
5943 | AllocationInfos[CB] = AI; | ||||
5944 | TLI->getLibFunc(*CB, AI->LibraryFunctionId); | ||||
5945 | } | ||||
5946 | } | ||||
5947 | return true; | ||||
5948 | }; | ||||
5949 | |||||
5950 | bool UsedAssumedInformation = false; | ||||
5951 | bool Success = A.checkForAllCallLikeInstructions( | ||||
5952 | AllocationIdentifierCB, *this, UsedAssumedInformation, | ||||
5953 | /* CheckBBLivenessOnly */ false, | ||||
5954 | /* CheckPotentiallyDead */ true); | ||||
5955 | (void)Success; | ||||
5956 | assert(Success && "Did not expect the call base visit callback to fail!")(static_cast <bool> (Success && "Did not expect the call base visit callback to fail!" ) ? void (0) : __assert_fail ("Success && \"Did not expect the call base visit callback to fail!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5956, __extension__ __PRETTY_FUNCTION__)); | ||||
5957 | } | ||||
5958 | |||||
5959 | const std::string getAsStr() const override { | ||||
5960 | unsigned NumH2SMallocs = 0, NumInvalidMallocs = 0; | ||||
5961 | for (const auto &It : AllocationInfos) { | ||||
5962 | if (It.second->Status == AllocationInfo::INVALID) | ||||
5963 | ++NumInvalidMallocs; | ||||
5964 | else | ||||
5965 | ++NumH2SMallocs; | ||||
5966 | } | ||||
5967 | return "[H2S] Mallocs Good/Bad: " + std::to_string(NumH2SMallocs) + "/" + | ||||
5968 | std::to_string(NumInvalidMallocs); | ||||
5969 | } | ||||
5970 | |||||
5971 | /// See AbstractAttribute::trackStatistics(). | ||||
5972 | void trackStatistics() const override { | ||||
5973 | STATS_DECL(static llvm::Statistic NumIRFunction_MallocCalls = {"attributor" , "NumIRFunction_MallocCalls", "Number of malloc/calloc/aligned_alloc calls converted to allocas" };; | ||||
5974 | MallocCalls, Function,static llvm::Statistic NumIRFunction_MallocCalls = {"attributor" , "NumIRFunction_MallocCalls", "Number of malloc/calloc/aligned_alloc calls converted to allocas" };; | ||||
5975 | "Number of malloc/calloc/aligned_alloc calls converted to allocas")static llvm::Statistic NumIRFunction_MallocCalls = {"attributor" , "NumIRFunction_MallocCalls", "Number of malloc/calloc/aligned_alloc calls converted to allocas" };;; | ||||
5976 | for (auto &It : AllocationInfos) | ||||
5977 | if (It.second->Status != AllocationInfo::INVALID) | ||||
5978 | ++BUILD_STAT_NAME(MallocCalls, Function)NumIRFunction_MallocCalls; | ||||
5979 | } | ||||
5980 | |||||
5981 | bool isAssumedHeapToStack(const CallBase &CB) const override { | ||||
5982 | if (isValidState()) | ||||
5983 | if (AllocationInfo *AI = AllocationInfos.lookup(&CB)) | ||||
5984 | return AI->Status != AllocationInfo::INVALID; | ||||
5985 | return false; | ||||
5986 | } | ||||
5987 | |||||
5988 | bool isAssumedHeapToStackRemovedFree(CallBase &CB) const override { | ||||
5989 | if (!isValidState()) | ||||
5990 | return false; | ||||
5991 | |||||
5992 | for (auto &It : AllocationInfos) { | ||||
5993 | AllocationInfo &AI = *It.second; | ||||
5994 | if (AI.Status == AllocationInfo::INVALID) | ||||
5995 | continue; | ||||
5996 | |||||
5997 | if (AI.PotentialFreeCalls.count(&CB)) | ||||
5998 | return true; | ||||
5999 | } | ||||
6000 | |||||
6001 | return false; | ||||
6002 | } | ||||
6003 | |||||
6004 | ChangeStatus manifest(Attributor &A) override { | ||||
6005 | assert(getState().isValidState() &&(static_cast <bool> (getState().isValidState() && "Attempted to manifest an invalid state!") ? void (0) : __assert_fail ("getState().isValidState() && \"Attempted to manifest an invalid state!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6006, __extension__ __PRETTY_FUNCTION__)) | ||||
6006 | "Attempted to manifest an invalid state!")(static_cast <bool> (getState().isValidState() && "Attempted to manifest an invalid state!") ? void (0) : __assert_fail ("getState().isValidState() && \"Attempted to manifest an invalid state!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6006, __extension__ __PRETTY_FUNCTION__)); | ||||
6007 | |||||
6008 | ChangeStatus HasChanged = ChangeStatus::UNCHANGED; | ||||
6009 | Function *F = getAnchorScope(); | ||||
6010 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | ||||
6011 | |||||
6012 | for (auto &It : AllocationInfos) { | ||||
6013 | AllocationInfo &AI = *It.second; | ||||
6014 | if (AI.Status == AllocationInfo::INVALID) | ||||
6015 | continue; | ||||
6016 | |||||
6017 | for (CallBase *FreeCall : AI.PotentialFreeCalls) { | ||||
6018 | LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "H2S: Removing free call: " << *FreeCall << "\n"; } } while (false); | ||||
6019 | A.deleteAfterManifest(*FreeCall); | ||||
6020 | HasChanged = ChangeStatus::CHANGED; | ||||
6021 | } | ||||
6022 | |||||
6023 | LLVM_DEBUG(dbgs() << "H2S: Removing malloc-like call: " << *AI.CBdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "H2S: Removing malloc-like call: " << *AI.CB << "\n"; } } while (false) | ||||
6024 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "H2S: Removing malloc-like call: " << *AI.CB << "\n"; } } while (false); | ||||
6025 | |||||
6026 | auto Remark = [&](OptimizationRemark OR) { | ||||
6027 | LibFunc IsAllocShared; | ||||
6028 | if (TLI->getLibFunc(*AI.CB, IsAllocShared)) | ||||
6029 | if (IsAllocShared == LibFunc___kmpc_alloc_shared) | ||||
6030 | return OR << "Moving globalized variable to the stack."; | ||||
6031 | return OR << "Moving memory allocation from the heap to the stack."; | ||||
6032 | }; | ||||
6033 | if (AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared) | ||||
6034 | A.emitRemark<OptimizationRemark>(AI.CB, "OMP110", Remark); | ||||
6035 | else | ||||
6036 | A.emitRemark<OptimizationRemark>(AI.CB, "HeapToStack", Remark); | ||||
6037 | |||||
6038 | const DataLayout &DL = A.getInfoCache().getDL(); | ||||
6039 | Value *Size; | ||||
6040 | Optional<APInt> SizeAPI = getSize(A, *this, AI); | ||||
6041 | if (SizeAPI.hasValue()) { | ||||
6042 | Size = ConstantInt::get(AI.CB->getContext(), *SizeAPI); | ||||
6043 | } else { | ||||
6044 | LLVMContext &Ctx = AI.CB->getContext(); | ||||
6045 | ObjectSizeOpts Opts; | ||||
6046 | ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, Opts); | ||||
6047 | SizeOffsetEvalType SizeOffsetPair = Eval.compute(AI.CB); | ||||
6048 | assert(SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown() &&(static_cast <bool> (SizeOffsetPair != ObjectSizeOffsetEvaluator ::unknown() && cast<ConstantInt>(SizeOffsetPair .second)->isZero()) ? void (0) : __assert_fail ("SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown() && cast<ConstantInt>(SizeOffsetPair.second)->isZero()" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6049, __extension__ __PRETTY_FUNCTION__)) | ||||
6049 | cast<ConstantInt>(SizeOffsetPair.second)->isZero())(static_cast <bool> (SizeOffsetPair != ObjectSizeOffsetEvaluator ::unknown() && cast<ConstantInt>(SizeOffsetPair .second)->isZero()) ? void (0) : __assert_fail ("SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown() && cast<ConstantInt>(SizeOffsetPair.second)->isZero()" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6049, __extension__ __PRETTY_FUNCTION__)); | ||||
6050 | Size = SizeOffsetPair.first; | ||||
6051 | } | ||||
6052 | |||||
6053 | Align Alignment(1); | ||||
6054 | if (MaybeAlign RetAlign = AI.CB->getRetAlign()) | ||||
6055 | Alignment = max(Alignment, RetAlign); | ||||
6056 | if (Value *Align = getAllocAlignment(AI.CB, TLI)) { | ||||
6057 | Optional<APInt> AlignmentAPI = getAPInt(A, *this, *Align); | ||||
6058 | assert(AlignmentAPI.hasValue() &&(static_cast <bool> (AlignmentAPI.hasValue() && "Expected an alignment during manifest!") ? void (0) : __assert_fail ("AlignmentAPI.hasValue() && \"Expected an alignment during manifest!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6059, __extension__ __PRETTY_FUNCTION__)) | ||||
6059 | "Expected an alignment during manifest!")(static_cast <bool> (AlignmentAPI.hasValue() && "Expected an alignment during manifest!") ? void (0) : __assert_fail ("AlignmentAPI.hasValue() && \"Expected an alignment during manifest!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6059, __extension__ __PRETTY_FUNCTION__)); | ||||
6060 | Alignment = | ||||
6061 | max(Alignment, MaybeAlign(AlignmentAPI.getValue().getZExtValue())); | ||||
6062 | } | ||||
6063 | |||||
6064 | // TODO: Hoist the alloca towards the function entry. | ||||
6065 | unsigned AS = DL.getAllocaAddrSpace(); | ||||
6066 | Instruction *Alloca = new AllocaInst(Type::getInt8Ty(F->getContext()), AS, | ||||
6067 | Size, Alignment, "", AI.CB); | ||||
6068 | |||||
6069 | if (Alloca->getType() != AI.CB->getType()) | ||||
6070 | Alloca = BitCastInst::CreatePointerBitCastOrAddrSpaceCast( | ||||
6071 | Alloca, AI.CB->getType(), "malloc_cast", AI.CB); | ||||
6072 | |||||
6073 | auto *I8Ty = Type::getInt8Ty(F->getContext()); | ||||
6074 | auto *InitVal = getInitialValueOfAllocation(AI.CB, TLI, I8Ty); | ||||
6075 | assert(InitVal &&(static_cast <bool> (InitVal && "Must be able to materialize initial memory state of allocation" ) ? void (0) : __assert_fail ("InitVal && \"Must be able to materialize initial memory state of allocation\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6076, __extension__ __PRETTY_FUNCTION__)) | ||||
6076 | "Must be able to materialize initial memory state of allocation")(static_cast <bool> (InitVal && "Must be able to materialize initial memory state of allocation" ) ? void (0) : __assert_fail ("InitVal && \"Must be able to materialize initial memory state of allocation\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6076, __extension__ __PRETTY_FUNCTION__)); | ||||
6077 | |||||
6078 | A.changeValueAfterManifest(*AI.CB, *Alloca); | ||||
6079 | |||||
6080 | if (auto *II = dyn_cast<InvokeInst>(AI.CB)) { | ||||
6081 | auto *NBB = II->getNormalDest(); | ||||
6082 | BranchInst::Create(NBB, AI.CB->getParent()); | ||||
6083 | A.deleteAfterManifest(*AI.CB); | ||||
6084 | } else { | ||||
6085 | A.deleteAfterManifest(*AI.CB); | ||||
6086 | } | ||||
6087 | |||||
6088 | // Initialize the alloca with the same value as used by the allocation | ||||
6089 | // function. We can skip undef as the initial value of an alloc is | ||||
6090 | // undef, and the memset would simply end up being DSEd. | ||||
6091 | if (!isa<UndefValue>(InitVal)) { | ||||
6092 | IRBuilder<> Builder(Alloca->getNextNode()); | ||||
6093 | // TODO: Use alignment above if align!=1 | ||||
6094 | Builder.CreateMemSet(Alloca, InitVal, Size, None); | ||||
6095 | } | ||||
6096 | HasChanged = ChangeStatus::CHANGED; | ||||
6097 | } | ||||
6098 | |||||
6099 | return HasChanged; | ||||
6100 | } | ||||
6101 | |||||
6102 | Optional<APInt> getAPInt(Attributor &A, const AbstractAttribute &AA, | ||||
6103 | Value &V) { | ||||
6104 | bool UsedAssumedInformation = false; | ||||
6105 | Optional<Constant *> SimpleV = | ||||
6106 | A.getAssumedConstant(V, AA, UsedAssumedInformation); | ||||
6107 | if (!SimpleV.hasValue()) | ||||
6108 | return APInt(64, 0); | ||||
6109 | if (auto *CI = dyn_cast_or_null<ConstantInt>(SimpleV.getValue())) | ||||
6110 | return CI->getValue(); | ||||
6111 | return llvm::None; | ||||
6112 | } | ||||
6113 | |||||
6114 | Optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA, | ||||
6115 | AllocationInfo &AI) { | ||||
6116 | auto Mapper = [&](const Value *V) -> const Value * { | ||||
6117 | bool UsedAssumedInformation = false; | ||||
6118 | if (Optional<Constant *> SimpleV = | ||||
6119 | A.getAssumedConstant(*V, AA, UsedAssumedInformation)) | ||||
6120 | if (*SimpleV) | ||||
6121 | return *SimpleV; | ||||
6122 | return V; | ||||
6123 | }; | ||||
6124 | |||||
6125 | const Function *F = getAnchorScope(); | ||||
6126 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | ||||
6127 | return getAllocSize(AI.CB, TLI, Mapper); | ||||
6128 | } | ||||
6129 | |||||
6130 | /// Collection of all malloc-like calls in a function with associated | ||||
6131 | /// information. | ||||
6132 | DenseMap<CallBase *, AllocationInfo *> AllocationInfos; | ||||
6133 | |||||
6134 | /// Collection of all free-like calls in a function with associated | ||||
6135 | /// information. | ||||
6136 | DenseMap<CallBase *, DeallocationInfo *> DeallocationInfos; | ||||
6137 | |||||
6138 | ChangeStatus updateImpl(Attributor &A) override; | ||||
6139 | }; | ||||
6140 | |||||
6141 | ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) { | ||||
6142 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
6143 | const Function *F = getAnchorScope(); | ||||
6144 | const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F); | ||||
6145 | |||||
6146 | const auto &LivenessAA = | ||||
6147 | A.getAAFor<AAIsDead>(*this, IRPosition::function(*F), DepClassTy::NONE); | ||||
6148 | |||||
6149 | MustBeExecutedContextExplorer &Explorer = | ||||
6150 | A.getInfoCache().getMustBeExecutedContextExplorer(); | ||||
6151 | |||||
6152 | bool StackIsAccessibleByOtherThreads = | ||||
6153 | A.getInfoCache().stackIsAccessibleByOtherThreads(); | ||||
6154 | |||||
6155 | // Flag to ensure we update our deallocation information at most once per | ||||
6156 | // updateImpl call and only if we use the free check reasoning. | ||||
6157 | bool HasUpdatedFrees = false; | ||||
6158 | |||||
6159 | auto UpdateFrees = [&]() { | ||||
6160 | HasUpdatedFrees = true; | ||||
6161 | |||||
6162 | for (auto &It : DeallocationInfos) { | ||||
6163 | DeallocationInfo &DI = *It.second; | ||||
6164 | // For now we cannot use deallocations that have unknown inputs, skip | ||||
6165 | // them. | ||||
6166 | if (DI.MightFreeUnknownObjects) | ||||
6167 | continue; | ||||
6168 | |||||
6169 | // No need to analyze dead calls, ignore them instead. | ||||
6170 | bool UsedAssumedInformation = false; | ||||
6171 | if (A.isAssumedDead(*DI.CB, this, &LivenessAA, UsedAssumedInformation, | ||||
6172 | /* CheckBBLivenessOnly */ true)) | ||||
6173 | continue; | ||||
6174 | |||||
6175 | // Use the optimistic version to get the freed objects, ignoring dead | ||||
6176 | // branches etc. | ||||
6177 | SmallVector<Value *, 8> Objects; | ||||
6178 | if (!AA::getAssumedUnderlyingObjects(A, *DI.CB->getArgOperand(0), Objects, | ||||
6179 | *this, DI.CB, | ||||
6180 | UsedAssumedInformation)) { | ||||
6181 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n" ; } } while (false) | ||||
6182 | dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n" ; } } while (false) | ||||
6183 | << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n" ; } } while (false); | ||||
6184 | DI.MightFreeUnknownObjects = true; | ||||
6185 | continue; | ||||
6186 | } | ||||
6187 | |||||
6188 | // Check each object explicitly. | ||||
6189 | for (auto *Obj : Objects) { | ||||
6190 | // Free of null and undef can be ignored as no-ops (or UB in the latter | ||||
6191 | // case). | ||||
6192 | if (isa<ConstantPointerNull>(Obj) || isa<UndefValue>(Obj)) | ||||
6193 | continue; | ||||
6194 | |||||
6195 | CallBase *ObjCB = dyn_cast<CallBase>(Obj); | ||||
6196 | if (!ObjCB) { | ||||
6197 | LLVM_DEBUG(dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Free of a non-call object: " << *Obj << "\n"; } } while (false) | ||||
6198 | << "[H2S] Free of a non-call object: " << *Obj << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Free of a non-call object: " << *Obj << "\n"; } } while (false); | ||||
6199 | DI.MightFreeUnknownObjects = true; | ||||
6200 | continue; | ||||
6201 | } | ||||
6202 | |||||
6203 | AllocationInfo *AI = AllocationInfos.lookup(ObjCB); | ||||
6204 | if (!AI) { | ||||
6205 | LLVM_DEBUG(dbgs() << "[H2S] Free of a non-allocation object: " << *Objdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Free of a non-allocation object: " << *Obj << "\n"; } } while (false) | ||||
6206 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Free of a non-allocation object: " << *Obj << "\n"; } } while (false); | ||||
6207 | DI.MightFreeUnknownObjects = true; | ||||
6208 | continue; | ||||
6209 | } | ||||
6210 | |||||
6211 | DI.PotentialAllocationCalls.insert(ObjCB); | ||||
6212 | } | ||||
6213 | } | ||||
6214 | }; | ||||
6215 | |||||
6216 | auto FreeCheck = [&](AllocationInfo &AI) { | ||||
6217 | // If the stack is not accessible by other threads, the "must-free" logic | ||||
6218 | // doesn't apply as the pointer could be shared and needs to be places in | ||||
6219 | // "shareable" memory. | ||||
6220 | if (!StackIsAccessibleByOtherThreads) { | ||||
6221 | auto &NoSyncAA = | ||||
6222 | A.getAAFor<AANoSync>(*this, getIRPosition(), DepClassTy::OPTIONAL); | ||||
6223 | if (!NoSyncAA.isAssumedNoSync()) { | ||||
6224 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] found an escaping use, stack is not accessible by " "other threads and function is not nosync:\n"; } } while (false ) | ||||
6225 | dbgs() << "[H2S] found an escaping use, stack is not accessible by "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] found an escaping use, stack is not accessible by " "other threads and function is not nosync:\n"; } } while (false ) | ||||
6226 | "other threads and function is not nosync:\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] found an escaping use, stack is not accessible by " "other threads and function is not nosync:\n"; } } while (false ); | ||||
6227 | return false; | ||||
6228 | } | ||||
6229 | } | ||||
6230 | if (!HasUpdatedFrees) | ||||
6231 | UpdateFrees(); | ||||
6232 | |||||
6233 | // TODO: Allow multi exit functions that have different free calls. | ||||
6234 | if (AI.PotentialFreeCalls.size() != 1) { | ||||
6235 | LLVM_DEBUG(dbgs() << "[H2S] did not find one free call but "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] did not find one free call but " << AI.PotentialFreeCalls.size() << "\n"; } } while (false) | ||||
6236 | << AI.PotentialFreeCalls.size() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] did not find one free call but " << AI.PotentialFreeCalls.size() << "\n"; } } while (false); | ||||
6237 | return false; | ||||
6238 | } | ||||
6239 | CallBase *UniqueFree = *AI.PotentialFreeCalls.begin(); | ||||
6240 | DeallocationInfo *DI = DeallocationInfos.lookup(UniqueFree); | ||||
6241 | if (!DI) { | ||||
6242 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call was not known as deallocation call " << *UniqueFree << "\n"; } } while (false) | ||||
6243 | dbgs() << "[H2S] unique free call was not known as deallocation call "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call was not known as deallocation call " << *UniqueFree << "\n"; } } while (false) | ||||
6244 | << *UniqueFree << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call was not known as deallocation call " << *UniqueFree << "\n"; } } while (false); | ||||
6245 | return false; | ||||
6246 | } | ||||
6247 | if (DI->MightFreeUnknownObjects) { | ||||
6248 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call might free unknown allocations\n" ; } } while (false) | ||||
6249 | dbgs() << "[H2S] unique free call might free unknown allocations\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call might free unknown allocations\n" ; } } while (false); | ||||
6250 | return false; | ||||
6251 | } | ||||
6252 | if (DI->PotentialAllocationCalls.size() > 1) { | ||||
6253 | LLVM_DEBUG(dbgs() << "[H2S] unique free call might free "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call might free " << DI->PotentialAllocationCalls.size() << " different allocations\n" ; } } while (false) | ||||
6254 | << DI->PotentialAllocationCalls.size()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call might free " << DI->PotentialAllocationCalls.size() << " different allocations\n" ; } } while (false) | ||||
6255 | << " different allocations\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call might free " << DI->PotentialAllocationCalls.size() << " different allocations\n" ; } } while (false); | ||||
6256 | return false; | ||||
6257 | } | ||||
6258 | if (*DI->PotentialAllocationCalls.begin() != AI.CB) { | ||||
6259 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call not known to free this allocation but " << **DI->PotentialAllocationCalls.begin() << "\n" ; } } while (false) | ||||
6260 | dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call not known to free this allocation but " << **DI->PotentialAllocationCalls.begin() << "\n" ; } } while (false) | ||||
6261 | << "[H2S] unique free call not known to free this allocation but "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call not known to free this allocation but " << **DI->PotentialAllocationCalls.begin() << "\n" ; } } while (false) | ||||
6262 | << **DI->PotentialAllocationCalls.begin() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call not known to free this allocation but " << **DI->PotentialAllocationCalls.begin() << "\n" ; } } while (false); | ||||
6263 | return false; | ||||
6264 | } | ||||
6265 | Instruction *CtxI = isa<InvokeInst>(AI.CB) ? AI.CB : AI.CB->getNextNode(); | ||||
6266 | if (!Explorer.findInContextOf(UniqueFree, CtxI)) { | ||||
6267 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call might not be executed with the allocation " << *UniqueFree << "\n"; } } while (false) | ||||
6268 | dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call might not be executed with the allocation " << *UniqueFree << "\n"; } } while (false) | ||||
6269 | << "[H2S] unique free call might not be executed with the allocation "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call might not be executed with the allocation " << *UniqueFree << "\n"; } } while (false) | ||||
6270 | << *UniqueFree << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] unique free call might not be executed with the allocation " << *UniqueFree << "\n"; } } while (false); | ||||
6271 | return false; | ||||
6272 | } | ||||
6273 | return true; | ||||
6274 | }; | ||||
6275 | |||||
6276 | auto UsesCheck = [&](AllocationInfo &AI) { | ||||
6277 | bool ValidUsesOnly = true; | ||||
6278 | |||||
6279 | auto Pred = [&](const Use &U, bool &Follow) -> bool { | ||||
6280 | Instruction *UserI = cast<Instruction>(U.getUser()); | ||||
6281 | if (isa<LoadInst>(UserI)) | ||||
6282 | return true; | ||||
6283 | if (auto *SI = dyn_cast<StoreInst>(UserI)) { | ||||
6284 | if (SI->getValueOperand() == U.get()) { | ||||
6285 | LLVM_DEBUG(dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] escaping store to memory: " << *UserI << "\n"; } } while (false) | ||||
6286 | << "[H2S] escaping store to memory: " << *UserI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] escaping store to memory: " << *UserI << "\n"; } } while (false); | ||||
6287 | ValidUsesOnly = false; | ||||
6288 | } else { | ||||
6289 | // A store into the malloc'ed memory is fine. | ||||
6290 | } | ||||
6291 | return true; | ||||
6292 | } | ||||
6293 | if (auto *CB = dyn_cast<CallBase>(UserI)) { | ||||
6294 | if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd()) | ||||
6295 | return true; | ||||
6296 | if (DeallocationInfos.count(CB)) { | ||||
6297 | AI.PotentialFreeCalls.insert(CB); | ||||
6298 | return true; | ||||
6299 | } | ||||
6300 | |||||
6301 | unsigned ArgNo = CB->getArgOperandNo(&U); | ||||
6302 | |||||
6303 | const auto &NoCaptureAA = A.getAAFor<AANoCapture>( | ||||
6304 | *this, IRPosition::callsite_argument(*CB, ArgNo), | ||||
6305 | DepClassTy::OPTIONAL); | ||||
6306 | |||||
6307 | // If a call site argument use is nofree, we are fine. | ||||
6308 | const auto &ArgNoFreeAA = A.getAAFor<AANoFree>( | ||||
6309 | *this, IRPosition::callsite_argument(*CB, ArgNo), | ||||
6310 | DepClassTy::OPTIONAL); | ||||
6311 | |||||
6312 | bool MaybeCaptured = !NoCaptureAA.isAssumedNoCapture(); | ||||
6313 | bool MaybeFreed = !ArgNoFreeAA.isAssumedNoFree(); | ||||
6314 | if (MaybeCaptured || | ||||
6315 | (AI.LibraryFunctionId != LibFunc___kmpc_alloc_shared && | ||||
6316 | MaybeFreed)) { | ||||
6317 | AI.HasPotentiallyFreeingUnknownUses |= MaybeFreed; | ||||
6318 | |||||
6319 | // Emit a missed remark if this is missed OpenMP globalization. | ||||
6320 | auto Remark = [&](OptimizationRemarkMissed ORM) { | ||||
6321 | return ORM | ||||
6322 | << "Could not move globalized variable to the stack. " | ||||
6323 | "Variable is potentially captured in call. Mark " | ||||
6324 | "parameter as `__attribute__((noescape))` to override."; | ||||
6325 | }; | ||||
6326 | |||||
6327 | if (ValidUsesOnly && | ||||
6328 | AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared) | ||||
6329 | A.emitRemark<OptimizationRemarkMissed>(CB, "OMP113", Remark); | ||||
6330 | |||||
6331 | LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Bad user: " << *UserI << "\n"; } } while (false); | ||||
6332 | ValidUsesOnly = false; | ||||
6333 | } | ||||
6334 | return true; | ||||
6335 | } | ||||
6336 | |||||
6337 | if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) || | ||||
6338 | isa<PHINode>(UserI) || isa<SelectInst>(UserI)) { | ||||
6339 | Follow = true; | ||||
6340 | return true; | ||||
6341 | } | ||||
6342 | // Unknown user for which we can not track uses further (in a way that | ||||
6343 | // makes sense). | ||||
6344 | LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Unknown user: " << *UserI << "\n"; } } while (false); | ||||
6345 | ValidUsesOnly = false; | ||||
6346 | return true; | ||||
6347 | }; | ||||
6348 | if (!A.checkForAllUses(Pred, *this, *AI.CB)) | ||||
6349 | return false; | ||||
6350 | return ValidUsesOnly; | ||||
6351 | }; | ||||
6352 | |||||
6353 | // The actual update starts here. We look at all allocations and depending on | ||||
6354 | // their status perform the appropriate check(s). | ||||
6355 | for (auto &It : AllocationInfos) { | ||||
6356 | AllocationInfo &AI = *It.second; | ||||
6357 | if (AI.Status == AllocationInfo::INVALID) | ||||
6358 | continue; | ||||
6359 | |||||
6360 | if (Value *Align = getAllocAlignment(AI.CB, TLI)) { | ||||
6361 | Optional<APInt> APAlign = getAPInt(A, *this, *Align); | ||||
6362 | if (!APAlign) { | ||||
6363 | // Can't generate an alloca which respects the required alignment | ||||
6364 | // on the allocation. | ||||
6365 | LLVM_DEBUG(dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CBdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CB << "\n"; } } while (false) | ||||
6366 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CB << "\n"; } } while (false); | ||||
6367 | AI.Status = AllocationInfo::INVALID; | ||||
6368 | Changed = ChangeStatus::CHANGED; | ||||
6369 | continue; | ||||
6370 | } else { | ||||
6371 | if (APAlign->ugt(llvm::Value::MaximumAlignment) || !APAlign->isPowerOf2()) { | ||||
6372 | LLVM_DEBUG(dbgs() << "[H2S] Invalid allocation alignment: " << APAlign << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[H2S] Invalid allocation alignment: " << APAlign << "\n"; } } while (false); | ||||
6373 | AI.Status = AllocationInfo::INVALID; | ||||
6374 | Changed = ChangeStatus::CHANGED; | ||||
6375 | continue; | ||||
6376 | } | ||||
6377 | } | ||||
6378 | } | ||||
6379 | |||||
6380 | if (MaxHeapToStackSize != -1) { | ||||
6381 | Optional<APInt> Size = getSize(A, *this, AI); | ||||
6382 | if (!Size.hasValue() || Size.getValue().ugt(MaxHeapToStackSize)) { | ||||
6383 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (!Size.hasValue()) dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n"; else dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. " << MaxHeapToStackSize << "\n"; }; } } while (false) | ||||
6384 | if (!Size.hasValue())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (!Size.hasValue()) dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n"; else dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. " << MaxHeapToStackSize << "\n"; }; } } while (false) | ||||
6385 | dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (!Size.hasValue()) dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n"; else dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. " << MaxHeapToStackSize << "\n"; }; } } while (false) | ||||
6386 | elsedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (!Size.hasValue()) dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n"; else dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. " << MaxHeapToStackSize << "\n"; }; } } while (false) | ||||
6387 | dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (!Size.hasValue()) dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n"; else dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. " << MaxHeapToStackSize << "\n"; }; } } while (false) | ||||
6388 | << MaxHeapToStackSize << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (!Size.hasValue()) dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n"; else dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. " << MaxHeapToStackSize << "\n"; }; } } while (false) | ||||
6389 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { if (!Size.hasValue()) dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n"; else dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. " << MaxHeapToStackSize << "\n"; }; } } while (false); | ||||
6390 | |||||
6391 | AI.Status = AllocationInfo::INVALID; | ||||
6392 | Changed = ChangeStatus::CHANGED; | ||||
6393 | continue; | ||||
6394 | } | ||||
6395 | } | ||||
6396 | |||||
6397 | switch (AI.Status) { | ||||
6398 | case AllocationInfo::STACK_DUE_TO_USE: | ||||
6399 | if (UsesCheck(AI)) | ||||
6400 | continue; | ||||
6401 | AI.Status = AllocationInfo::STACK_DUE_TO_FREE; | ||||
6402 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||
6403 | case AllocationInfo::STACK_DUE_TO_FREE: | ||||
6404 | if (FreeCheck(AI)) | ||||
6405 | continue; | ||||
6406 | AI.Status = AllocationInfo::INVALID; | ||||
6407 | Changed = ChangeStatus::CHANGED; | ||||
6408 | continue; | ||||
6409 | case AllocationInfo::INVALID: | ||||
6410 | llvm_unreachable("Invalid allocations should never reach this point!")::llvm::llvm_unreachable_internal("Invalid allocations should never reach this point!" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6410); | ||||
6411 | }; | ||||
6412 | } | ||||
6413 | |||||
6414 | return Changed; | ||||
6415 | } | ||||
6416 | |||||
6417 | /// ----------------------- Privatizable Pointers ------------------------------ | ||||
6418 | struct AAPrivatizablePtrImpl : public AAPrivatizablePtr { | ||||
6419 | AAPrivatizablePtrImpl(const IRPosition &IRP, Attributor &A) | ||||
6420 | : AAPrivatizablePtr(IRP, A), PrivatizableType(llvm::None) {} | ||||
6421 | |||||
6422 | ChangeStatus indicatePessimisticFixpoint() override { | ||||
6423 | AAPrivatizablePtr::indicatePessimisticFixpoint(); | ||||
6424 | PrivatizableType = nullptr; | ||||
6425 | return ChangeStatus::CHANGED; | ||||
6426 | } | ||||
6427 | |||||
6428 | /// Identify the type we can chose for a private copy of the underlying | ||||
6429 | /// argument. None means it is not clear yet, nullptr means there is none. | ||||
6430 | virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0; | ||||
6431 | |||||
6432 | /// Return a privatizable type that encloses both T0 and T1. | ||||
6433 | /// TODO: This is merely a stub for now as we should manage a mapping as well. | ||||
6434 | Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) { | ||||
6435 | if (!T0.hasValue()) | ||||
6436 | return T1; | ||||
6437 | if (!T1.hasValue()) | ||||
6438 | return T0; | ||||
6439 | if (T0 == T1) | ||||
6440 | return T0; | ||||
6441 | return nullptr; | ||||
6442 | } | ||||
6443 | |||||
6444 | Optional<Type *> getPrivatizableType() const override { | ||||
6445 | return PrivatizableType; | ||||
6446 | } | ||||
6447 | |||||
6448 | const std::string getAsStr() const override { | ||||
6449 | return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]"; | ||||
6450 | } | ||||
6451 | |||||
6452 | protected: | ||||
6453 | Optional<Type *> PrivatizableType; | ||||
6454 | }; | ||||
6455 | |||||
6456 | // TODO: Do this for call site arguments (probably also other values) as well. | ||||
6457 | |||||
6458 | struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl { | ||||
6459 | AAPrivatizablePtrArgument(const IRPosition &IRP, Attributor &A) | ||||
6460 | : AAPrivatizablePtrImpl(IRP, A) {} | ||||
6461 | |||||
6462 | /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) | ||||
6463 | Optional<Type *> identifyPrivatizableType(Attributor &A) override { | ||||
6464 | // If this is a byval argument and we know all the call sites (so we can | ||||
6465 | // rewrite them), there is no need to check them explicitly. | ||||
6466 | bool UsedAssumedInformation = false; | ||||
6467 | if (getIRPosition().hasAttr(Attribute::ByVal) && | ||||
6468 | A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this, | ||||
6469 | true, UsedAssumedInformation)) | ||||
6470 | return getAssociatedValue().getType()->getPointerElementType(); | ||||
6471 | |||||
6472 | Optional<Type *> Ty; | ||||
6473 | unsigned ArgNo = getIRPosition().getCallSiteArgNo(); | ||||
6474 | |||||
6475 | // Make sure the associated call site argument has the same type at all call | ||||
6476 | // sites and it is an allocation we know is safe to privatize, for now that | ||||
6477 | // means we only allow alloca instructions. | ||||
6478 | // TODO: We can additionally analyze the accesses in the callee to create | ||||
6479 | // the type from that information instead. That is a little more | ||||
6480 | // involved and will be done in a follow up patch. | ||||
6481 | auto CallSiteCheck = [&](AbstractCallSite ACS) { | ||||
6482 | IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo); | ||||
6483 | // Check if a coresponding argument was found or if it is one not | ||||
6484 | // associated (which can happen for callback calls). | ||||
6485 | if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID) | ||||
6486 | return false; | ||||
6487 | |||||
6488 | // Check that all call sites agree on a type. | ||||
6489 | auto &PrivCSArgAA = | ||||
6490 | A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos, DepClassTy::REQUIRED); | ||||
6491 | Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType(); | ||||
6492 | |||||
6493 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; if (CSTy.hasValue() && CSTy.getValue()) CSTy.getValue()->print(dbgs()); else if ( CSTy.hasValue()) dbgs() << "<nullptr>"; else dbgs () << "<none>"; }; } } while (false) | ||||
6494 | dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; if (CSTy.hasValue() && CSTy.getValue()) CSTy.getValue()->print(dbgs()); else if ( CSTy.hasValue()) dbgs() << "<nullptr>"; else dbgs () << "<none>"; }; } } while (false) | ||||
6495 | if (CSTy.hasValue() && CSTy.getValue())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; if (CSTy.hasValue() && CSTy.getValue()) CSTy.getValue()->print(dbgs()); else if ( CSTy.hasValue()) dbgs() << "<nullptr>"; else dbgs () << "<none>"; }; } } while (false) | ||||
6496 | CSTy.getValue()->print(dbgs());do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; if (CSTy.hasValue() && CSTy.getValue()) CSTy.getValue()->print(dbgs()); else if ( CSTy.hasValue()) dbgs() << "<nullptr>"; else dbgs () << "<none>"; }; } } while (false) | ||||
6497 | else if (CSTy.hasValue())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; if (CSTy.hasValue() && CSTy.getValue()) CSTy.getValue()->print(dbgs()); else if ( CSTy.hasValue()) dbgs() << "<nullptr>"; else dbgs () << "<none>"; }; } } while (false) | ||||
6498 | dbgs() << "<nullptr>";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; if (CSTy.hasValue() && CSTy.getValue()) CSTy.getValue()->print(dbgs()); else if ( CSTy.hasValue()) dbgs() << "<nullptr>"; else dbgs () << "<none>"; }; } } while (false) | ||||
6499 | elsedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; if (CSTy.hasValue() && CSTy.getValue()) CSTy.getValue()->print(dbgs()); else if ( CSTy.hasValue()) dbgs() << "<nullptr>"; else dbgs () << "<none>"; }; } } while (false) | ||||
6500 | dbgs() << "<none>";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; if (CSTy.hasValue() && CSTy.getValue()) CSTy.getValue()->print(dbgs()); else if ( CSTy.hasValue()) dbgs() << "<nullptr>"; else dbgs () << "<none>"; }; } } while (false) | ||||
6501 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: "; if (CSTy.hasValue() && CSTy.getValue()) CSTy.getValue()->print(dbgs()); else if ( CSTy.hasValue()) dbgs() << "<nullptr>"; else dbgs () << "<none>"; }; } } while (false); | ||||
6502 | |||||
6503 | Ty = combineTypes(Ty, CSTy); | ||||
6504 | |||||
6505 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false) | ||||
6506 | dbgs() << " : New Type: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false) | ||||
6507 | if (Ty.hasValue() && Ty.getValue())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false) | ||||
6508 | Ty.getValue()->print(dbgs());do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false) | ||||
6509 | else if (Ty.hasValue())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false) | ||||
6510 | dbgs() << "<nullptr>";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false) | ||||
6511 | elsedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false) | ||||
6512 | dbgs() << "<none>";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false) | ||||
6513 | dbgs() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false) | ||||
6514 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << " : New Type: "; if (Ty.hasValue () && Ty.getValue()) Ty.getValue()->print(dbgs()); else if (Ty.hasValue()) dbgs() << "<nullptr>"; else dbgs() << "<none>"; dbgs() << "\n"; }; } } while (false); | ||||
6515 | |||||
6516 | return !Ty.hasValue() || Ty.getValue(); | ||||
6517 | }; | ||||
6518 | |||||
6519 | if (!A.checkForAllCallSites(CallSiteCheck, *this, true, | ||||
6520 | UsedAssumedInformation)) | ||||
6521 | return nullptr; | ||||
6522 | return Ty; | ||||
6523 | } | ||||
6524 | |||||
6525 | /// See AbstractAttribute::updateImpl(...). | ||||
6526 | ChangeStatus updateImpl(Attributor &A) override { | ||||
6527 | PrivatizableType = identifyPrivatizableType(A); | ||||
6528 | if (!PrivatizableType.hasValue()) | ||||
6529 | return ChangeStatus::UNCHANGED; | ||||
6530 | if (!PrivatizableType.getValue()) | ||||
6531 | return indicatePessimisticFixpoint(); | ||||
6532 | |||||
6533 | // The dependence is optional so we don't give up once we give up on the | ||||
6534 | // alignment. | ||||
6535 | A.getAAFor<AAAlign>(*this, IRPosition::value(getAssociatedValue()), | ||||
6536 | DepClassTy::OPTIONAL); | ||||
6537 | |||||
6538 | // Avoid arguments with padding for now. | ||||
6539 | if (!getIRPosition().hasAttr(Attribute::ByVal) && | ||||
6540 | !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(), | ||||
6541 | A.getInfoCache().getDL())) { | ||||
6542 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] Padding detected\n" ; } } while (false); | ||||
6543 | return indicatePessimisticFixpoint(); | ||||
6544 | } | ||||
6545 | |||||
6546 | // Collect the types that will replace the privatizable type in the function | ||||
6547 | // signature. | ||||
6548 | SmallVector<Type *, 16> ReplacementTypes; | ||||
6549 | identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); | ||||
6550 | |||||
6551 | // Verify callee and caller agree on how the promoted argument would be | ||||
6552 | // passed. | ||||
6553 | Function &Fn = *getIRPosition().getAnchorScope(); | ||||
6554 | const auto *TTI = | ||||
6555 | A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn); | ||||
6556 | if (!TTI) { | ||||
6557 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Missing TTI for function "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] Missing TTI for function " << Fn.getName() << "\n"; } } while (false) | ||||
6558 | << Fn.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] Missing TTI for function " << Fn.getName() << "\n"; } } while (false); | ||||
6559 | return indicatePessimisticFixpoint(); | ||||
6560 | } | ||||
6561 | |||||
6562 | auto CallSiteCheck = [&](AbstractCallSite ACS) { | ||||
6563 | CallBase *CB = ACS.getInstruction(); | ||||
6564 | return TTI->areTypesABICompatible( | ||||
6565 | CB->getCaller(), CB->getCalledFunction(), ReplacementTypes); | ||||
6566 | }; | ||||
6567 | bool UsedAssumedInformation = false; | ||||
6568 | if (!A.checkForAllCallSites(CallSiteCheck, *this, true, | ||||
6569 | UsedAssumedInformation)) { | ||||
6570 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for " << Fn.getName() << "\n"; } } while (false) | ||||
6571 | dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for " << Fn.getName() << "\n"; } } while (false) | ||||
6572 | << Fn.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for " << Fn.getName() << "\n"; } } while (false); | ||||
6573 | return indicatePessimisticFixpoint(); | ||||
6574 | } | ||||
6575 | |||||
6576 | // Register a rewrite of the argument. | ||||
6577 | Argument *Arg = getAssociatedArgument(); | ||||
6578 | if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) { | ||||
6579 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n" ; } } while (false); | ||||
6580 | return indicatePessimisticFixpoint(); | ||||
6581 | } | ||||
6582 | |||||
6583 | unsigned ArgNo = Arg->getArgNo(); | ||||
6584 | |||||
6585 | // Helper to check if for the given call site the associated argument is | ||||
6586 | // passed to a callback where the privatization would be different. | ||||
6587 | auto IsCompatiblePrivArgOfCallback = [&](CallBase &CB) { | ||||
6588 | SmallVector<const Use *, 4> CallbackUses; | ||||
6589 | AbstractCallSite::getCallbackUses(CB, CallbackUses); | ||||
6590 | for (const Use *U : CallbackUses) { | ||||
6591 | AbstractCallSite CBACS(U); | ||||
6592 | assert(CBACS && CBACS.isCallbackCall())(static_cast <bool> (CBACS && CBACS.isCallbackCall ()) ? void (0) : __assert_fail ("CBACS && CBACS.isCallbackCall()" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6592, __extension__ __PRETTY_FUNCTION__)); | ||||
6593 | for (Argument &CBArg : CBACS.getCalledFunction()->args()) { | ||||
6594 | int CBArgNo = CBACS.getCallArgOperandNo(CBArg); | ||||
6595 | |||||
6596 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6597 | dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6598 | << "[AAPrivatizablePtr] Argument " << *Argdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6599 | << "check if can be privatized in the context of its parent ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6600 | << Arg->getParent()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6601 | << ")\n[AAPrivatizablePtr] because it is an argument in a "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6602 | "callback ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6603 | << CBArgNo << "@" << CBACS.getCalledFunction()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6604 | << ")\n[AAPrivatizablePtr] " << CBArg << " : "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6605 | << CBACS.getCallArgOperand(CBArg) << " vs "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6606 | << CB.getArgOperand(ArgNo) << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6607 | << "[AAPrivatizablePtr] " << CBArg << " : "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6608 | << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false) | ||||
6609 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << "check if can be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ")\n[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperand(CBArg) << " vs " << CB.getArgOperand(ArgNo) << "\n" << "[AAPrivatizablePtr] " << CBArg << " : " << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n"; }; } } while (false); | ||||
6610 | |||||
6611 | if (CBArgNo != int(ArgNo)) | ||||
6612 | continue; | ||||
6613 | const auto &CBArgPrivAA = A.getAAFor<AAPrivatizablePtr>( | ||||
6614 | *this, IRPosition::argument(CBArg), DepClassTy::REQUIRED); | ||||
6615 | if (CBArgPrivAA.isValidState()) { | ||||
6616 | auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType(); | ||||
6617 | if (!CBArgPrivTy.hasValue()) | ||||
6618 | continue; | ||||
6619 | if (CBArgPrivTy.getValue() == PrivatizableType) | ||||
6620 | continue; | ||||
6621 | } | ||||
6622 | |||||
6623 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6624 | dbgs() << "[AAPrivatizablePtr] Argument " << *Argdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6625 | << " cannot be privatized in the context of its parent ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6626 | << Arg->getParent()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6627 | << ")\n[AAPrivatizablePtr] because it is an argument in a "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6628 | "callback ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6629 | << CBArgNo << "@" << CBACS.getCalledFunction()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6630 | << ").\n[AAPrivatizablePtr] for which the argument "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6631 | "privatization is not compatible.\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6632 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "callback (" << CBArgNo << "@" << CBACS.getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false); | ||||
6633 | return false; | ||||
6634 | } | ||||
6635 | } | ||||
6636 | return true; | ||||
6637 | }; | ||||
6638 | |||||
6639 | // Helper to check if for the given call site the associated argument is | ||||
6640 | // passed to a direct call where the privatization would be different. | ||||
6641 | auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) { | ||||
6642 | CallBase *DC = cast<CallBase>(ACS.getInstruction()); | ||||
6643 | int DCArgNo = ACS.getCallArgOperandNo(ArgNo); | ||||
6644 | assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->arg_size() &&(static_cast <bool> (DCArgNo >= 0 && unsigned (DCArgNo) < DC->arg_size() && "Expected a direct call operand for callback call operand" ) ? void (0) : __assert_fail ("DCArgNo >= 0 && unsigned(DCArgNo) < DC->arg_size() && \"Expected a direct call operand for callback call operand\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6645, __extension__ __PRETTY_FUNCTION__)) | ||||
6645 | "Expected a direct call operand for callback call operand")(static_cast <bool> (DCArgNo >= 0 && unsigned (DCArgNo) < DC->arg_size() && "Expected a direct call operand for callback call operand" ) ? void (0) : __assert_fail ("DCArgNo >= 0 && unsigned(DCArgNo) < DC->arg_size() && \"Expected a direct call operand for callback call operand\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6645, __extension__ __PRETTY_FUNCTION__)); | ||||
6646 | |||||
6647 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " check if be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << DCArgNo << "@" << DC ->getCalledFunction()->getName() << ").\n"; }; } } while (false) | ||||
6648 | dbgs() << "[AAPrivatizablePtr] Argument " << *Argdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " check if be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << DCArgNo << "@" << DC ->getCalledFunction()->getName() << ").\n"; }; } } while (false) | ||||
6649 | << " check if be privatized in the context of its parent ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " check if be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << DCArgNo << "@" << DC ->getCalledFunction()->getName() << ").\n"; }; } } while (false) | ||||
6650 | << Arg->getParent()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " check if be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << DCArgNo << "@" << DC ->getCalledFunction()->getName() << ").\n"; }; } } while (false) | ||||
6651 | << ")\n[AAPrivatizablePtr] because it is an argument in a "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " check if be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << DCArgNo << "@" << DC ->getCalledFunction()->getName() << ").\n"; }; } } while (false) | ||||
6652 | "direct call of ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " check if be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << DCArgNo << "@" << DC ->getCalledFunction()->getName() << ").\n"; }; } } while (false) | ||||
6653 | << DCArgNo << "@" << DC->getCalledFunction()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " check if be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << DCArgNo << "@" << DC ->getCalledFunction()->getName() << ").\n"; }; } } while (false) | ||||
6654 | << ").\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " check if be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << DCArgNo << "@" << DC ->getCalledFunction()->getName() << ").\n"; }; } } while (false) | ||||
6655 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " check if be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << DCArgNo << "@" << DC ->getCalledFunction()->getName() << ").\n"; }; } } while (false); | ||||
6656 | |||||
6657 | Function *DCCallee = DC->getCalledFunction(); | ||||
6658 | if (unsigned(DCArgNo) < DCCallee->arg_size()) { | ||||
6659 | const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>( | ||||
6660 | *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)), | ||||
6661 | DepClassTy::REQUIRED); | ||||
6662 | if (DCArgPrivAA.isValidState()) { | ||||
6663 | auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType(); | ||||
6664 | if (!DCArgPrivTy.hasValue()) | ||||
6665 | return true; | ||||
6666 | if (DCArgPrivTy.getValue() == PrivatizableType) | ||||
6667 | return true; | ||||
6668 | } | ||||
6669 | } | ||||
6670 | |||||
6671 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6672 | dbgs() << "[AAPrivatizablePtr] Argument " << *Argdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6673 | << " cannot be privatized in the context of its parent ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6674 | << Arg->getParent()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6675 | << ")\n[AAPrivatizablePtr] because it is an argument in a "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6676 | "direct call of ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6677 | << ACS.getInstruction()->getCalledFunction()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6678 | << ").\n[AAPrivatizablePtr] for which the argument "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6679 | "privatization is not compatible.\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false) | ||||
6680 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { { dbgs() << "[AAPrivatizablePtr] Argument " << *Arg << " cannot be privatized in the context of its parent (" << Arg->getParent()->getName() << ")\n[AAPrivatizablePtr] because it is an argument in a " "direct call of (" << ACS.getInstruction()->getCalledFunction ()->getName() << ").\n[AAPrivatizablePtr] for which the argument " "privatization is not compatible.\n"; }; } } while (false); | ||||
6681 | return false; | ||||
6682 | }; | ||||
6683 | |||||
6684 | // Helper to check if the associated argument is used at the given abstract | ||||
6685 | // call site in a way that is incompatible with the privatization assumed | ||||
6686 | // here. | ||||
6687 | auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) { | ||||
6688 | if (ACS.isDirectCall()) | ||||
6689 | return IsCompatiblePrivArgOfCallback(*ACS.getInstruction()); | ||||
6690 | if (ACS.isCallbackCall()) | ||||
6691 | return IsCompatiblePrivArgOfDirectCS(ACS); | ||||
6692 | return false; | ||||
6693 | }; | ||||
6694 | |||||
6695 | if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true, | ||||
6696 | UsedAssumedInformation)) | ||||
6697 | return indicatePessimisticFixpoint(); | ||||
6698 | |||||
6699 | return ChangeStatus::UNCHANGED; | ||||
6700 | } | ||||
6701 | |||||
6702 | /// Given a type to private \p PrivType, collect the constituates (which are | ||||
6703 | /// used) in \p ReplacementTypes. | ||||
6704 | static void | ||||
6705 | identifyReplacementTypes(Type *PrivType, | ||||
6706 | SmallVectorImpl<Type *> &ReplacementTypes) { | ||||
6707 | // TODO: For now we expand the privatization type to the fullest which can | ||||
6708 | // lead to dead arguments that need to be removed later. | ||||
6709 | assert(PrivType && "Expected privatizable type!")(static_cast <bool> (PrivType && "Expected privatizable type!" ) ? void (0) : __assert_fail ("PrivType && \"Expected privatizable type!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6709, __extension__ __PRETTY_FUNCTION__)); | ||||
6710 | |||||
6711 | // Traverse the type, extract constituate types on the outermost level. | ||||
6712 | if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { | ||||
6713 | for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) | ||||
6714 | ReplacementTypes.push_back(PrivStructType->getElementType(u)); | ||||
6715 | } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { | ||||
6716 | ReplacementTypes.append(PrivArrayType->getNumElements(), | ||||
6717 | PrivArrayType->getElementType()); | ||||
6718 | } else { | ||||
6719 | ReplacementTypes.push_back(PrivType); | ||||
6720 | } | ||||
6721 | } | ||||
6722 | |||||
6723 | /// Initialize \p Base according to the type \p PrivType at position \p IP. | ||||
6724 | /// The values needed are taken from the arguments of \p F starting at | ||||
6725 | /// position \p ArgNo. | ||||
6726 | static void createInitialization(Type *PrivType, Value &Base, Function &F, | ||||
6727 | unsigned ArgNo, Instruction &IP) { | ||||
6728 | assert(PrivType && "Expected privatizable type!")(static_cast <bool> (PrivType && "Expected privatizable type!" ) ? void (0) : __assert_fail ("PrivType && \"Expected privatizable type!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6728, __extension__ __PRETTY_FUNCTION__)); | ||||
6729 | |||||
6730 | IRBuilder<NoFolder> IRB(&IP); | ||||
6731 | const DataLayout &DL = F.getParent()->getDataLayout(); | ||||
6732 | |||||
6733 | // Traverse the type, build GEPs and stores. | ||||
6734 | if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { | ||||
6735 | const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); | ||||
6736 | for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { | ||||
6737 | Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo(); | ||||
6738 | Value *Ptr = | ||||
6739 | constructPointer(PointeeTy, PrivType, &Base, | ||||
6740 | PrivStructLayout->getElementOffset(u), IRB, DL); | ||||
6741 | new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); | ||||
6742 | } | ||||
6743 | } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { | ||||
6744 | Type *PointeeTy = PrivArrayType->getElementType(); | ||||
6745 | Type *PointeePtrTy = PointeeTy->getPointerTo(); | ||||
6746 | uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy); | ||||
6747 | for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { | ||||
6748 | Value *Ptr = constructPointer(PointeePtrTy, PrivType, &Base, | ||||
6749 | u * PointeeTySize, IRB, DL); | ||||
6750 | new StoreInst(F.getArg(ArgNo + u), Ptr, &IP); | ||||
6751 | } | ||||
6752 | } else { | ||||
6753 | new StoreInst(F.getArg(ArgNo), &Base, &IP); | ||||
6754 | } | ||||
6755 | } | ||||
6756 | |||||
6757 | /// Extract values from \p Base according to the type \p PrivType at the | ||||
6758 | /// call position \p ACS. The values are appended to \p ReplacementValues. | ||||
6759 | void createReplacementValues(Align Alignment, Type *PrivType, | ||||
6760 | AbstractCallSite ACS, Value *Base, | ||||
6761 | SmallVectorImpl<Value *> &ReplacementValues) { | ||||
6762 | assert(Base && "Expected base value!")(static_cast <bool> (Base && "Expected base value!" ) ? void (0) : __assert_fail ("Base && \"Expected base value!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6762, __extension__ __PRETTY_FUNCTION__)); | ||||
6763 | assert(PrivType && "Expected privatizable type!")(static_cast <bool> (PrivType && "Expected privatizable type!" ) ? void (0) : __assert_fail ("PrivType && \"Expected privatizable type!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6763, __extension__ __PRETTY_FUNCTION__)); | ||||
6764 | Instruction *IP = ACS.getInstruction(); | ||||
6765 | |||||
6766 | IRBuilder<NoFolder> IRB(IP); | ||||
6767 | const DataLayout &DL = IP->getModule()->getDataLayout(); | ||||
6768 | |||||
6769 | Type *PrivPtrType = PrivType->getPointerTo(); | ||||
6770 | if (Base->getType() != PrivPtrType) | ||||
6771 | Base = BitCastInst::CreatePointerBitCastOrAddrSpaceCast( | ||||
6772 | Base, PrivPtrType, "", ACS.getInstruction()); | ||||
6773 | |||||
6774 | // Traverse the type, build GEPs and loads. | ||||
6775 | if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) { | ||||
6776 | const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType); | ||||
6777 | for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) { | ||||
6778 | Type *PointeeTy = PrivStructType->getElementType(u); | ||||
6779 | Value *Ptr = | ||||
6780 | constructPointer(PointeeTy->getPointerTo(), PrivType, Base, | ||||
6781 | PrivStructLayout->getElementOffset(u), IRB, DL); | ||||
6782 | LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP); | ||||
6783 | L->setAlignment(Alignment); | ||||
6784 | ReplacementValues.push_back(L); | ||||
6785 | } | ||||
6786 | } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) { | ||||
6787 | Type *PointeeTy = PrivArrayType->getElementType(); | ||||
6788 | uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy); | ||||
6789 | Type *PointeePtrTy = PointeeTy->getPointerTo(); | ||||
6790 | for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) { | ||||
6791 | Value *Ptr = constructPointer(PointeePtrTy, PrivType, Base, | ||||
6792 | u * PointeeTySize, IRB, DL); | ||||
6793 | LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP); | ||||
6794 | L->setAlignment(Alignment); | ||||
6795 | ReplacementValues.push_back(L); | ||||
6796 | } | ||||
6797 | } else { | ||||
6798 | LoadInst *L = new LoadInst(PrivType, Base, "", IP); | ||||
6799 | L->setAlignment(Alignment); | ||||
6800 | ReplacementValues.push_back(L); | ||||
6801 | } | ||||
6802 | } | ||||
6803 | |||||
6804 | /// See AbstractAttribute::manifest(...) | ||||
6805 | ChangeStatus manifest(Attributor &A) override { | ||||
6806 | if (!PrivatizableType.hasValue()) | ||||
6807 | return ChangeStatus::UNCHANGED; | ||||
6808 | assert(PrivatizableType.getValue() && "Expected privatizable type!")(static_cast <bool> (PrivatizableType.getValue() && "Expected privatizable type!") ? void (0) : __assert_fail ("PrivatizableType.getValue() && \"Expected privatizable type!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 6808, __extension__ __PRETTY_FUNCTION__)); | ||||
6809 | |||||
6810 | // Collect all tail calls in the function as we cannot allow new allocas to | ||||
6811 | // escape into tail recursion. | ||||
6812 | // TODO: Be smarter about new allocas escaping into tail calls. | ||||
6813 | SmallVector<CallInst *, 16> TailCalls; | ||||
6814 | bool UsedAssumedInformation = false; | ||||
6815 | if (!A.checkForAllInstructions( | ||||
6816 | [&](Instruction &I) { | ||||
6817 | CallInst &CI = cast<CallInst>(I); | ||||
6818 | if (CI.isTailCall()) | ||||
6819 | TailCalls.push_back(&CI); | ||||
6820 | return true; | ||||
6821 | }, | ||||
6822 | *this, {Instruction::Call}, UsedAssumedInformation)) | ||||
6823 | return ChangeStatus::UNCHANGED; | ||||
6824 | |||||
6825 | Argument *Arg = getAssociatedArgument(); | ||||
6826 | // Query AAAlign attribute for alignment of associated argument to | ||||
6827 | // determine the best alignment of loads. | ||||
6828 | const auto &AlignAA = | ||||
6829 | A.getAAFor<AAAlign>(*this, IRPosition::value(*Arg), DepClassTy::NONE); | ||||
6830 | |||||
6831 | // Callback to repair the associated function. A new alloca is placed at the | ||||
6832 | // beginning and initialized with the values passed through arguments. The | ||||
6833 | // new alloca replaces the use of the old pointer argument. | ||||
6834 | Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB = | ||||
6835 | [=](const Attributor::ArgumentReplacementInfo &ARI, | ||||
6836 | Function &ReplacementFn, Function::arg_iterator ArgIt) { | ||||
6837 | BasicBlock &EntryBB = ReplacementFn.getEntryBlock(); | ||||
6838 | Instruction *IP = &*EntryBB.getFirstInsertionPt(); | ||||
6839 | const DataLayout &DL = IP->getModule()->getDataLayout(); | ||||
6840 | unsigned AS = DL.getAllocaAddrSpace(); | ||||
6841 | Instruction *AI = new AllocaInst(PrivatizableType.getValue(), AS, | ||||
6842 | Arg->getName() + ".priv", IP); | ||||
6843 | createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn, | ||||
6844 | ArgIt->getArgNo(), *IP); | ||||
6845 | |||||
6846 | if (AI->getType() != Arg->getType()) | ||||
6847 | AI = BitCastInst::CreatePointerBitCastOrAddrSpaceCast( | ||||
6848 | AI, Arg->getType(), "", IP); | ||||
6849 | Arg->replaceAllUsesWith(AI); | ||||
6850 | |||||
6851 | for (CallInst *CI : TailCalls) | ||||
6852 | CI->setTailCall(false); | ||||
6853 | }; | ||||
6854 | |||||
6855 | // Callback to repair a call site of the associated function. The elements | ||||
6856 | // of the privatizable type are loaded prior to the call and passed to the | ||||
6857 | // new function version. | ||||
6858 | Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB = | ||||
6859 | [=, &AlignAA](const Attributor::ArgumentReplacementInfo &ARI, | ||||
6860 | AbstractCallSite ACS, | ||||
6861 | SmallVectorImpl<Value *> &NewArgOperands) { | ||||
6862 | // When no alignment is specified for the load instruction, | ||||
6863 | // natural alignment is assumed. | ||||
6864 | createReplacementValues( | ||||
6865 | assumeAligned(AlignAA.getAssumedAlign()), | ||||
6866 | PrivatizableType.getValue(), ACS, | ||||
6867 | ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()), | ||||
6868 | NewArgOperands); | ||||
6869 | }; | ||||
6870 | |||||
6871 | // Collect the types that will replace the privatizable type in the function | ||||
6872 | // signature. | ||||
6873 | SmallVector<Type *, 16> ReplacementTypes; | ||||
6874 | identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes); | ||||
6875 | |||||
6876 | // Register a rewrite of the argument. | ||||
6877 | if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes, | ||||
6878 | std::move(FnRepairCB), | ||||
6879 | std::move(ACSRepairCB))) | ||||
6880 | return ChangeStatus::CHANGED; | ||||
6881 | return ChangeStatus::UNCHANGED; | ||||
6882 | } | ||||
6883 | |||||
6884 | /// See AbstractAttribute::trackStatistics() | ||||
6885 | void trackStatistics() const override { | ||||
6886 | STATS_DECLTRACK_ARG_ATTR(privatizable_ptr){ static llvm::Statistic NumIRArguments_privatizable_ptr = {"attributor" , "NumIRArguments_privatizable_ptr", ("Number of " "arguments" " marked '" "privatizable_ptr" "'")};; ++(NumIRArguments_privatizable_ptr ); }; | ||||
6887 | } | ||||
6888 | }; | ||||
6889 | |||||
6890 | struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl { | ||||
6891 | AAPrivatizablePtrFloating(const IRPosition &IRP, Attributor &A) | ||||
6892 | : AAPrivatizablePtrImpl(IRP, A) {} | ||||
6893 | |||||
6894 | /// See AbstractAttribute::initialize(...). | ||||
6895 | virtual void initialize(Attributor &A) override { | ||||
6896 | // TODO: We can privatize more than arguments. | ||||
6897 | indicatePessimisticFixpoint(); | ||||
6898 | } | ||||
6899 | |||||
6900 | ChangeStatus updateImpl(Attributor &A) override { | ||||
6901 | llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"::llvm::llvm_unreachable_internal("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::" "updateImpl will not be called", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 6902) | ||||
6902 | "updateImpl will not be called")::llvm::llvm_unreachable_internal("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::" "updateImpl will not be called", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 6902); | ||||
6903 | } | ||||
6904 | |||||
6905 | /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...) | ||||
6906 | Optional<Type *> identifyPrivatizableType(Attributor &A) override { | ||||
6907 | Value *Obj = getUnderlyingObject(&getAssociatedValue()); | ||||
6908 | if (!Obj) { | ||||
6909 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] No underlying object found!\n" ; } } while (false); | ||||
6910 | return nullptr; | ||||
6911 | } | ||||
6912 | |||||
6913 | if (auto *AI = dyn_cast<AllocaInst>(Obj)) | ||||
6914 | if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) | ||||
6915 | if (CI->isOne()) | ||||
6916 | return AI->getAllocatedType(); | ||||
6917 | if (auto *Arg = dyn_cast<Argument>(Obj)) { | ||||
6918 | auto &PrivArgAA = A.getAAFor<AAPrivatizablePtr>( | ||||
6919 | *this, IRPosition::argument(*Arg), DepClassTy::REQUIRED); | ||||
6920 | if (PrivArgAA.isAssumedPrivatizablePtr()) | ||||
6921 | return Obj->getType()->getPointerElementType(); | ||||
6922 | } | ||||
6923 | |||||
6924 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] Underlying object neither valid " "alloca nor privatizable argument: " << *Obj << "!\n" ; } } while (false) | ||||
6925 | "alloca nor privatizable argument: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] Underlying object neither valid " "alloca nor privatizable argument: " << *Obj << "!\n" ; } } while (false) | ||||
6926 | << *Obj << "!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] Underlying object neither valid " "alloca nor privatizable argument: " << *Obj << "!\n" ; } } while (false); | ||||
6927 | return nullptr; | ||||
6928 | } | ||||
6929 | |||||
6930 | /// See AbstractAttribute::trackStatistics() | ||||
6931 | void trackStatistics() const override { | ||||
6932 | STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr){ static llvm::Statistic NumIRFloating_privatizable_ptr = {"attributor" , "NumIRFloating_privatizable_ptr", ("Number of floating values known to be '" "privatizable_ptr" "'")};; ++(NumIRFloating_privatizable_ptr ); }; | ||||
6933 | } | ||||
6934 | }; | ||||
6935 | |||||
6936 | struct AAPrivatizablePtrCallSiteArgument final | ||||
6937 | : public AAPrivatizablePtrFloating { | ||||
6938 | AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
6939 | : AAPrivatizablePtrFloating(IRP, A) {} | ||||
6940 | |||||
6941 | /// See AbstractAttribute::initialize(...). | ||||
6942 | void initialize(Attributor &A) override { | ||||
6943 | if (getIRPosition().hasAttr(Attribute::ByVal)) | ||||
6944 | indicateOptimisticFixpoint(); | ||||
6945 | } | ||||
6946 | |||||
6947 | /// See AbstractAttribute::updateImpl(...). | ||||
6948 | ChangeStatus updateImpl(Attributor &A) override { | ||||
6949 | PrivatizableType = identifyPrivatizableType(A); | ||||
6950 | if (!PrivatizableType.hasValue()) | ||||
6951 | return ChangeStatus::UNCHANGED; | ||||
6952 | if (!PrivatizableType.getValue()) | ||||
6953 | return indicatePessimisticFixpoint(); | ||||
6954 | |||||
6955 | const IRPosition &IRP = getIRPosition(); | ||||
6956 | auto &NoCaptureAA = | ||||
6957 | A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::REQUIRED); | ||||
6958 | if (!NoCaptureAA.isAssumedNoCapture()) { | ||||
6959 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n" ; } } while (false); | ||||
6960 | return indicatePessimisticFixpoint(); | ||||
6961 | } | ||||
6962 | |||||
6963 | auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP, DepClassTy::REQUIRED); | ||||
6964 | if (!NoAliasAA.isAssumedNoAlias()) { | ||||
6965 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] pointer might alias!\n" ; } } while (false); | ||||
6966 | return indicatePessimisticFixpoint(); | ||||
6967 | } | ||||
6968 | |||||
6969 | bool IsKnown; | ||||
6970 | if (!AA::isAssumedReadOnly(A, IRP, *this, IsKnown)) { | ||||
6971 | LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPrivatizablePtr] pointer is written!\n" ; } } while (false); | ||||
6972 | return indicatePessimisticFixpoint(); | ||||
6973 | } | ||||
6974 | |||||
6975 | return ChangeStatus::UNCHANGED; | ||||
6976 | } | ||||
6977 | |||||
6978 | /// See AbstractAttribute::trackStatistics() | ||||
6979 | void trackStatistics() const override { | ||||
6980 | STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr){ static llvm::Statistic NumIRCSArguments_privatizable_ptr = { "attributor", "NumIRCSArguments_privatizable_ptr", ("Number of " "call site arguments" " marked '" "privatizable_ptr" "'")};; ++(NumIRCSArguments_privatizable_ptr); }; | ||||
6981 | } | ||||
6982 | }; | ||||
6983 | |||||
6984 | struct AAPrivatizablePtrCallSiteReturned final | ||||
6985 | : public AAPrivatizablePtrFloating { | ||||
6986 | AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
6987 | : AAPrivatizablePtrFloating(IRP, A) {} | ||||
6988 | |||||
6989 | /// See AbstractAttribute::initialize(...). | ||||
6990 | void initialize(Attributor &A) override { | ||||
6991 | // TODO: We can privatize more than arguments. | ||||
6992 | indicatePessimisticFixpoint(); | ||||
6993 | } | ||||
6994 | |||||
6995 | /// See AbstractAttribute::trackStatistics() | ||||
6996 | void trackStatistics() const override { | ||||
6997 | STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr){ static llvm::Statistic NumIRCSReturn_privatizable_ptr = {"attributor" , "NumIRCSReturn_privatizable_ptr", ("Number of " "call site returns" " marked '" "privatizable_ptr" "'")};; ++(NumIRCSReturn_privatizable_ptr ); }; | ||||
6998 | } | ||||
6999 | }; | ||||
7000 | |||||
7001 | struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating { | ||||
7002 | AAPrivatizablePtrReturned(const IRPosition &IRP, Attributor &A) | ||||
7003 | : AAPrivatizablePtrFloating(IRP, A) {} | ||||
7004 | |||||
7005 | /// See AbstractAttribute::initialize(...). | ||||
7006 | void initialize(Attributor &A) override { | ||||
7007 | // TODO: We can privatize more than arguments. | ||||
7008 | indicatePessimisticFixpoint(); | ||||
7009 | } | ||||
7010 | |||||
7011 | /// See AbstractAttribute::trackStatistics() | ||||
7012 | void trackStatistics() const override { | ||||
7013 | STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr){ static llvm::Statistic NumIRFunctionReturn_privatizable_ptr = {"attributor", "NumIRFunctionReturn_privatizable_ptr", ("Number of " "function returns" " marked '" "privatizable_ptr" "'")};; ++ (NumIRFunctionReturn_privatizable_ptr); }; | ||||
7014 | } | ||||
7015 | }; | ||||
7016 | |||||
7017 | /// -------------------- Memory Behavior Attributes ---------------------------- | ||||
7018 | /// Includes read-none, read-only, and write-only. | ||||
7019 | /// ---------------------------------------------------------------------------- | ||||
7020 | struct AAMemoryBehaviorImpl : public AAMemoryBehavior { | ||||
7021 | AAMemoryBehaviorImpl(const IRPosition &IRP, Attributor &A) | ||||
7022 | : AAMemoryBehavior(IRP, A) {} | ||||
7023 | |||||
7024 | /// See AbstractAttribute::initialize(...). | ||||
7025 | void initialize(Attributor &A) override { | ||||
7026 | intersectAssumedBits(BEST_STATE); | ||||
7027 | getKnownStateFromValue(getIRPosition(), getState()); | ||||
7028 | AAMemoryBehavior::initialize(A); | ||||
7029 | } | ||||
7030 | |||||
7031 | /// Return the memory behavior information encoded in the IR for \p IRP. | ||||
7032 | static void getKnownStateFromValue(const IRPosition &IRP, | ||||
7033 | BitIntegerState &State, | ||||
7034 | bool IgnoreSubsumingPositions = false) { | ||||
7035 | SmallVector<Attribute, 2> Attrs; | ||||
7036 | IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); | ||||
7037 | for (const Attribute &Attr : Attrs) { | ||||
7038 | switch (Attr.getKindAsEnum()) { | ||||
7039 | case Attribute::ReadNone: | ||||
7040 | State.addKnownBits(NO_ACCESSES); | ||||
7041 | break; | ||||
7042 | case Attribute::ReadOnly: | ||||
7043 | State.addKnownBits(NO_WRITES); | ||||
7044 | break; | ||||
7045 | case Attribute::WriteOnly: | ||||
7046 | State.addKnownBits(NO_READS); | ||||
7047 | break; | ||||
7048 | default: | ||||
7049 | llvm_unreachable("Unexpected attribute!")::llvm::llvm_unreachable_internal("Unexpected attribute!", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 7049); | ||||
7050 | } | ||||
7051 | } | ||||
7052 | |||||
7053 | if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) { | ||||
7054 | if (!I->mayReadFromMemory()) | ||||
7055 | State.addKnownBits(NO_READS); | ||||
7056 | if (!I->mayWriteToMemory()) | ||||
7057 | State.addKnownBits(NO_WRITES); | ||||
7058 | } | ||||
7059 | } | ||||
7060 | |||||
7061 | /// See AbstractAttribute::getDeducedAttributes(...). | ||||
7062 | void getDeducedAttributes(LLVMContext &Ctx, | ||||
7063 | SmallVectorImpl<Attribute> &Attrs) const override { | ||||
7064 | assert(Attrs.size() == 0)(static_cast <bool> (Attrs.size() == 0) ? void (0) : __assert_fail ("Attrs.size() == 0", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 7064, __extension__ __PRETTY_FUNCTION__)); | ||||
7065 | if (isAssumedReadNone()) | ||||
7066 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); | ||||
7067 | else if (isAssumedReadOnly()) | ||||
7068 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly)); | ||||
7069 | else if (isAssumedWriteOnly()) | ||||
7070 | Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly)); | ||||
7071 | assert(Attrs.size() <= 1)(static_cast <bool> (Attrs.size() <= 1) ? void (0) : __assert_fail ("Attrs.size() <= 1", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 7071, __extension__ __PRETTY_FUNCTION__)); | ||||
7072 | } | ||||
7073 | |||||
7074 | /// See AbstractAttribute::manifest(...). | ||||
7075 | ChangeStatus manifest(Attributor &A) override { | ||||
7076 | if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true)) | ||||
7077 | return ChangeStatus::UNCHANGED; | ||||
7078 | |||||
7079 | const IRPosition &IRP = getIRPosition(); | ||||
7080 | |||||
7081 | // Check if we would improve the existing attributes first. | ||||
7082 | SmallVector<Attribute, 4> DeducedAttrs; | ||||
7083 | getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); | ||||
7084 | if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { | ||||
7085 | return IRP.hasAttr(Attr.getKindAsEnum(), | ||||
7086 | /* IgnoreSubsumingPositions */ true); | ||||
7087 | })) | ||||
7088 | return ChangeStatus::UNCHANGED; | ||||
7089 | |||||
7090 | // Clear existing attributes. | ||||
7091 | IRP.removeAttrs(AttrKinds); | ||||
7092 | |||||
7093 | // Use the generic manifest method. | ||||
7094 | return IRAttribute::manifest(A); | ||||
7095 | } | ||||
7096 | |||||
7097 | /// See AbstractState::getAsStr(). | ||||
7098 | const std::string getAsStr() const override { | ||||
7099 | if (isAssumedReadNone()) | ||||
7100 | return "readnone"; | ||||
7101 | if (isAssumedReadOnly()) | ||||
7102 | return "readonly"; | ||||
7103 | if (isAssumedWriteOnly()) | ||||
7104 | return "writeonly"; | ||||
7105 | return "may-read/write"; | ||||
7106 | } | ||||
7107 | |||||
7108 | /// The set of IR attributes AAMemoryBehavior deals with. | ||||
7109 | static const Attribute::AttrKind AttrKinds[3]; | ||||
7110 | }; | ||||
7111 | |||||
7112 | const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = { | ||||
7113 | Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly}; | ||||
7114 | |||||
7115 | /// Memory behavior attribute for a floating value. | ||||
7116 | struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl { | ||||
7117 | AAMemoryBehaviorFloating(const IRPosition &IRP, Attributor &A) | ||||
7118 | : AAMemoryBehaviorImpl(IRP, A) {} | ||||
7119 | |||||
7120 | /// See AbstractAttribute::updateImpl(...). | ||||
7121 | ChangeStatus updateImpl(Attributor &A) override; | ||||
7122 | |||||
7123 | /// See AbstractAttribute::trackStatistics() | ||||
7124 | void trackStatistics() const override { | ||||
7125 | if (isAssumedReadNone()) | ||||
7126 | STATS_DECLTRACK_FLOATING_ATTR(readnone){ static llvm::Statistic NumIRFloating_readnone = {"attributor" , "NumIRFloating_readnone", ("Number of floating values known to be '" "readnone" "'")};; ++(NumIRFloating_readnone); } | ||||
7127 | else if (isAssumedReadOnly()) | ||||
7128 | STATS_DECLTRACK_FLOATING_ATTR(readonly){ static llvm::Statistic NumIRFloating_readonly = {"attributor" , "NumIRFloating_readonly", ("Number of floating values known to be '" "readonly" "'")};; ++(NumIRFloating_readonly); } | ||||
7129 | else if (isAssumedWriteOnly()) | ||||
7130 | STATS_DECLTRACK_FLOATING_ATTR(writeonly){ static llvm::Statistic NumIRFloating_writeonly = {"attributor" , "NumIRFloating_writeonly", ("Number of floating values known to be '" "writeonly" "'")};; ++(NumIRFloating_writeonly); } | ||||
7131 | } | ||||
7132 | |||||
7133 | private: | ||||
7134 | /// Return true if users of \p UserI might access the underlying | ||||
7135 | /// variable/location described by \p U and should therefore be analyzed. | ||||
7136 | bool followUsersOfUseIn(Attributor &A, const Use &U, | ||||
7137 | const Instruction *UserI); | ||||
7138 | |||||
7139 | /// Update the state according to the effect of use \p U in \p UserI. | ||||
7140 | void analyzeUseIn(Attributor &A, const Use &U, const Instruction *UserI); | ||||
7141 | }; | ||||
7142 | |||||
7143 | /// Memory behavior attribute for function argument. | ||||
7144 | struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating { | ||||
7145 | AAMemoryBehaviorArgument(const IRPosition &IRP, Attributor &A) | ||||
7146 | : AAMemoryBehaviorFloating(IRP, A) {} | ||||
7147 | |||||
7148 | /// See AbstractAttribute::initialize(...). | ||||
7149 | void initialize(Attributor &A) override { | ||||
7150 | intersectAssumedBits(BEST_STATE); | ||||
7151 | const IRPosition &IRP = getIRPosition(); | ||||
7152 | // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we | ||||
7153 | // can query it when we use has/getAttr. That would allow us to reuse the | ||||
7154 | // initialize of the base class here. | ||||
7155 | bool HasByVal = | ||||
7156 | IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true); | ||||
7157 | getKnownStateFromValue(IRP, getState(), | ||||
7158 | /* IgnoreSubsumingPositions */ HasByVal); | ||||
7159 | |||||
7160 | // Initialize the use vector with all direct uses of the associated value. | ||||
7161 | Argument *Arg = getAssociatedArgument(); | ||||
7162 | if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent()))) | ||||
7163 | indicatePessimisticFixpoint(); | ||||
7164 | } | ||||
7165 | |||||
7166 | ChangeStatus manifest(Attributor &A) override { | ||||
7167 | // TODO: Pointer arguments are not supported on vectors of pointers yet. | ||||
7168 | if (!getAssociatedValue().getType()->isPointerTy()) | ||||
7169 | return ChangeStatus::UNCHANGED; | ||||
7170 | |||||
7171 | // TODO: From readattrs.ll: "inalloca parameters are always | ||||
7172 | // considered written" | ||||
7173 | if (hasAttr({Attribute::InAlloca, Attribute::Preallocated})) { | ||||
7174 | removeKnownBits(NO_WRITES); | ||||
7175 | removeAssumedBits(NO_WRITES); | ||||
7176 | } | ||||
7177 | return AAMemoryBehaviorFloating::manifest(A); | ||||
7178 | } | ||||
7179 | |||||
7180 | /// See AbstractAttribute::trackStatistics() | ||||
7181 | void trackStatistics() const override { | ||||
7182 | if (isAssumedReadNone()) | ||||
7183 | STATS_DECLTRACK_ARG_ATTR(readnone){ static llvm::Statistic NumIRArguments_readnone = {"attributor" , "NumIRArguments_readnone", ("Number of " "arguments" " marked '" "readnone" "'")};; ++(NumIRArguments_readnone); } | ||||
7184 | else if (isAssumedReadOnly()) | ||||
7185 | STATS_DECLTRACK_ARG_ATTR(readonly){ static llvm::Statistic NumIRArguments_readonly = {"attributor" , "NumIRArguments_readonly", ("Number of " "arguments" " marked '" "readonly" "'")};; ++(NumIRArguments_readonly); } | ||||
7186 | else if (isAssumedWriteOnly()) | ||||
7187 | STATS_DECLTRACK_ARG_ATTR(writeonly){ static llvm::Statistic NumIRArguments_writeonly = {"attributor" , "NumIRArguments_writeonly", ("Number of " "arguments" " marked '" "writeonly" "'")};; ++(NumIRArguments_writeonly); } | ||||
7188 | } | ||||
7189 | }; | ||||
7190 | |||||
7191 | struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument { | ||||
7192 | AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
7193 | : AAMemoryBehaviorArgument(IRP, A) {} | ||||
7194 | |||||
7195 | /// See AbstractAttribute::initialize(...). | ||||
7196 | void initialize(Attributor &A) override { | ||||
7197 | // If we don't have an associated attribute this is either a variadic call | ||||
7198 | // or an indirect call, either way, nothing to do here. | ||||
7199 | Argument *Arg = getAssociatedArgument(); | ||||
7200 | if (!Arg) { | ||||
7201 | indicatePessimisticFixpoint(); | ||||
7202 | return; | ||||
7203 | } | ||||
7204 | if (Arg->hasByValAttr()) { | ||||
7205 | addKnownBits(NO_WRITES); | ||||
7206 | removeKnownBits(NO_READS); | ||||
7207 | removeAssumedBits(NO_READS); | ||||
7208 | } | ||||
7209 | AAMemoryBehaviorArgument::initialize(A); | ||||
7210 | if (getAssociatedFunction()->isDeclaration()) | ||||
7211 | indicatePessimisticFixpoint(); | ||||
7212 | } | ||||
7213 | |||||
7214 | /// See AbstractAttribute::updateImpl(...). | ||||
7215 | ChangeStatus updateImpl(Attributor &A) override { | ||||
7216 | // TODO: Once we have call site specific value information we can provide | ||||
7217 | // call site specific liveness liveness information and then it makes | ||||
7218 | // sense to specialize attributes for call sites arguments instead of | ||||
7219 | // redirecting requests to the callee argument. | ||||
7220 | Argument *Arg = getAssociatedArgument(); | ||||
7221 | const IRPosition &ArgPos = IRPosition::argument(*Arg); | ||||
7222 | auto &ArgAA = | ||||
7223 | A.getAAFor<AAMemoryBehavior>(*this, ArgPos, DepClassTy::REQUIRED); | ||||
7224 | return clampStateAndIndicateChange(getState(), ArgAA.getState()); | ||||
7225 | } | ||||
7226 | |||||
7227 | /// See AbstractAttribute::trackStatistics() | ||||
7228 | void trackStatistics() const override { | ||||
7229 | if (isAssumedReadNone()) | ||||
7230 | STATS_DECLTRACK_CSARG_ATTR(readnone){ static llvm::Statistic NumIRCSArguments_readnone = {"attributor" , "NumIRCSArguments_readnone", ("Number of " "call site arguments" " marked '" "readnone" "'")};; ++(NumIRCSArguments_readnone) ; } | ||||
7231 | else if (isAssumedReadOnly()) | ||||
7232 | STATS_DECLTRACK_CSARG_ATTR(readonly){ static llvm::Statistic NumIRCSArguments_readonly = {"attributor" , "NumIRCSArguments_readonly", ("Number of " "call site arguments" " marked '" "readonly" "'")};; ++(NumIRCSArguments_readonly) ; } | ||||
7233 | else if (isAssumedWriteOnly()) | ||||
7234 | STATS_DECLTRACK_CSARG_ATTR(writeonly){ static llvm::Statistic NumIRCSArguments_writeonly = {"attributor" , "NumIRCSArguments_writeonly", ("Number of " "call site arguments" " marked '" "writeonly" "'")};; ++(NumIRCSArguments_writeonly ); } | ||||
7235 | } | ||||
7236 | }; | ||||
7237 | |||||
7238 | /// Memory behavior attribute for a call site return position. | ||||
7239 | struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating { | ||||
7240 | AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
7241 | : AAMemoryBehaviorFloating(IRP, A) {} | ||||
7242 | |||||
7243 | /// See AbstractAttribute::initialize(...). | ||||
7244 | void initialize(Attributor &A) override { | ||||
7245 | AAMemoryBehaviorImpl::initialize(A); | ||||
7246 | Function *F = getAssociatedFunction(); | ||||
7247 | if (!F || F->isDeclaration()) | ||||
7248 | indicatePessimisticFixpoint(); | ||||
7249 | } | ||||
7250 | |||||
7251 | /// See AbstractAttribute::manifest(...). | ||||
7252 | ChangeStatus manifest(Attributor &A) override { | ||||
7253 | // We do not annotate returned values. | ||||
7254 | return ChangeStatus::UNCHANGED; | ||||
7255 | } | ||||
7256 | |||||
7257 | /// See AbstractAttribute::trackStatistics() | ||||
7258 | void trackStatistics() const override {} | ||||
7259 | }; | ||||
7260 | |||||
7261 | /// An AA to represent the memory behavior function attributes. | ||||
7262 | struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl { | ||||
7263 | AAMemoryBehaviorFunction(const IRPosition &IRP, Attributor &A) | ||||
7264 | : AAMemoryBehaviorImpl(IRP, A) {} | ||||
7265 | |||||
7266 | /// See AbstractAttribute::updateImpl(Attributor &A). | ||||
7267 | virtual ChangeStatus updateImpl(Attributor &A) override; | ||||
7268 | |||||
7269 | /// See AbstractAttribute::manifest(...). | ||||
7270 | ChangeStatus manifest(Attributor &A) override { | ||||
7271 | Function &F = cast<Function>(getAnchorValue()); | ||||
7272 | if (isAssumedReadNone()) { | ||||
7273 | F.removeFnAttr(Attribute::ArgMemOnly); | ||||
7274 | F.removeFnAttr(Attribute::InaccessibleMemOnly); | ||||
7275 | F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly); | ||||
7276 | } | ||||
7277 | return AAMemoryBehaviorImpl::manifest(A); | ||||
7278 | } | ||||
7279 | |||||
7280 | /// See AbstractAttribute::trackStatistics() | ||||
7281 | void trackStatistics() const override { | ||||
7282 | if (isAssumedReadNone()) | ||||
7283 | STATS_DECLTRACK_FN_ATTR(readnone){ static llvm::Statistic NumIRFunction_readnone = {"attributor" , "NumIRFunction_readnone", ("Number of " "functions" " marked '" "readnone" "'")};; ++(NumIRFunction_readnone); } | ||||
7284 | else if (isAssumedReadOnly()) | ||||
7285 | STATS_DECLTRACK_FN_ATTR(readonly){ static llvm::Statistic NumIRFunction_readonly = {"attributor" , "NumIRFunction_readonly", ("Number of " "functions" " marked '" "readonly" "'")};; ++(NumIRFunction_readonly); } | ||||
7286 | else if (isAssumedWriteOnly()) | ||||
7287 | STATS_DECLTRACK_FN_ATTR(writeonly){ static llvm::Statistic NumIRFunction_writeonly = {"attributor" , "NumIRFunction_writeonly", ("Number of " "functions" " marked '" "writeonly" "'")};; ++(NumIRFunction_writeonly); } | ||||
7288 | } | ||||
7289 | }; | ||||
7290 | |||||
7291 | /// AAMemoryBehavior attribute for call sites. | ||||
7292 | struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl { | ||||
7293 | AAMemoryBehaviorCallSite(const IRPosition &IRP, Attributor &A) | ||||
7294 | : AAMemoryBehaviorImpl(IRP, A) {} | ||||
7295 | |||||
7296 | /// See AbstractAttribute::initialize(...). | ||||
7297 | void initialize(Attributor &A) override { | ||||
7298 | AAMemoryBehaviorImpl::initialize(A); | ||||
7299 | Function *F = getAssociatedFunction(); | ||||
7300 | if (!F || F->isDeclaration()) | ||||
7301 | indicatePessimisticFixpoint(); | ||||
7302 | } | ||||
7303 | |||||
7304 | /// See AbstractAttribute::updateImpl(...). | ||||
7305 | ChangeStatus updateImpl(Attributor &A) override { | ||||
7306 | // TODO: Once we have call site specific value information we can provide | ||||
7307 | // call site specific liveness liveness information and then it makes | ||||
7308 | // sense to specialize attributes for call sites arguments instead of | ||||
7309 | // redirecting requests to the callee argument. | ||||
7310 | Function *F = getAssociatedFunction(); | ||||
7311 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
7312 | auto &FnAA = | ||||
7313 | A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::REQUIRED); | ||||
7314 | return clampStateAndIndicateChange(getState(), FnAA.getState()); | ||||
7315 | } | ||||
7316 | |||||
7317 | /// See AbstractAttribute::trackStatistics() | ||||
7318 | void trackStatistics() const override { | ||||
7319 | if (isAssumedReadNone()) | ||||
7320 | STATS_DECLTRACK_CS_ATTR(readnone){ static llvm::Statistic NumIRCS_readnone = {"attributor", "NumIRCS_readnone" , ("Number of " "call site" " marked '" "readnone" "'")};; ++ (NumIRCS_readnone); } | ||||
7321 | else if (isAssumedReadOnly()) | ||||
7322 | STATS_DECLTRACK_CS_ATTR(readonly){ static llvm::Statistic NumIRCS_readonly = {"attributor", "NumIRCS_readonly" , ("Number of " "call site" " marked '" "readonly" "'")};; ++ (NumIRCS_readonly); } | ||||
7323 | else if (isAssumedWriteOnly()) | ||||
7324 | STATS_DECLTRACK_CS_ATTR(writeonly){ static llvm::Statistic NumIRCS_writeonly = {"attributor", "NumIRCS_writeonly" , ("Number of " "call site" " marked '" "writeonly" "'")};; ++ (NumIRCS_writeonly); } | ||||
7325 | } | ||||
7326 | }; | ||||
7327 | |||||
7328 | ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) { | ||||
7329 | |||||
7330 | // The current assumed state used to determine a change. | ||||
7331 | auto AssumedState = getAssumed(); | ||||
7332 | |||||
7333 | auto CheckRWInst = [&](Instruction &I) { | ||||
7334 | // If the instruction has an own memory behavior state, use it to restrict | ||||
7335 | // the local state. No further analysis is required as the other memory | ||||
7336 | // state is as optimistic as it gets. | ||||
7337 | if (const auto *CB = dyn_cast<CallBase>(&I)) { | ||||
7338 | const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>( | ||||
7339 | *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED); | ||||
7340 | intersectAssumedBits(MemBehaviorAA.getAssumed()); | ||||
7341 | return !isAtFixpoint(); | ||||
7342 | } | ||||
7343 | |||||
7344 | // Remove access kind modifiers if necessary. | ||||
7345 | if (I.mayReadFromMemory()) | ||||
7346 | removeAssumedBits(NO_READS); | ||||
7347 | if (I.mayWriteToMemory()) | ||||
7348 | removeAssumedBits(NO_WRITES); | ||||
7349 | return !isAtFixpoint(); | ||||
7350 | }; | ||||
7351 | |||||
7352 | bool UsedAssumedInformation = false; | ||||
7353 | if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this, | ||||
7354 | UsedAssumedInformation)) | ||||
7355 | return indicatePessimisticFixpoint(); | ||||
7356 | |||||
7357 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED | ||||
7358 | : ChangeStatus::UNCHANGED; | ||||
7359 | } | ||||
7360 | |||||
7361 | ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) { | ||||
7362 | |||||
7363 | const IRPosition &IRP = getIRPosition(); | ||||
7364 | const IRPosition &FnPos = IRPosition::function_scope(IRP); | ||||
7365 | AAMemoryBehavior::StateType &S = getState(); | ||||
7366 | |||||
7367 | // First, check the function scope. We take the known information and we avoid | ||||
7368 | // work if the assumed information implies the current assumed information for | ||||
7369 | // this attribute. This is a valid for all but byval arguments. | ||||
7370 | Argument *Arg = IRP.getAssociatedArgument(); | ||||
7371 | AAMemoryBehavior::base_t FnMemAssumedState = | ||||
7372 | AAMemoryBehavior::StateType::getWorstState(); | ||||
7373 | if (!Arg || !Arg->hasByValAttr()) { | ||||
7374 | const auto &FnMemAA = | ||||
7375 | A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::OPTIONAL); | ||||
7376 | FnMemAssumedState = FnMemAA.getAssumed(); | ||||
7377 | S.addKnownBits(FnMemAA.getKnown()); | ||||
7378 | if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed()) | ||||
7379 | return ChangeStatus::UNCHANGED; | ||||
7380 | } | ||||
7381 | |||||
7382 | // The current assumed state used to determine a change. | ||||
7383 | auto AssumedState = S.getAssumed(); | ||||
7384 | |||||
7385 | // Make sure the value is not captured (except through "return"), if | ||||
7386 | // it is, any information derived would be irrelevant anyway as we cannot | ||||
7387 | // check the potential aliases introduced by the capture. However, no need | ||||
7388 | // to fall back to anythign less optimistic than the function state. | ||||
7389 | const auto &ArgNoCaptureAA = | ||||
7390 | A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::OPTIONAL); | ||||
7391 | if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) { | ||||
7392 | S.intersectAssumedBits(FnMemAssumedState); | ||||
7393 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED | ||||
7394 | : ChangeStatus::UNCHANGED; | ||||
7395 | } | ||||
7396 | |||||
7397 | // Visit and expand uses until all are analyzed or a fixpoint is reached. | ||||
7398 | auto UsePred = [&](const Use &U, bool &Follow) -> bool { | ||||
7399 | Instruction *UserI = cast<Instruction>(U.getUser()); | ||||
7400 | LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserIdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserI << " \n"; } } while (false) | ||||
7401 | << " \n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserI << " \n"; } } while (false); | ||||
7402 | |||||
7403 | // Droppable users, e.g., llvm::assume does not actually perform any action. | ||||
7404 | if (UserI->isDroppable()) | ||||
7405 | return true; | ||||
7406 | |||||
7407 | // Check if the users of UserI should also be visited. | ||||
7408 | Follow = followUsersOfUseIn(A, U, UserI); | ||||
7409 | |||||
7410 | // If UserI might touch memory we analyze the use in detail. | ||||
7411 | if (UserI->mayReadOrWriteMemory()) | ||||
7412 | analyzeUseIn(A, U, UserI); | ||||
7413 | |||||
7414 | return !isAtFixpoint(); | ||||
7415 | }; | ||||
7416 | |||||
7417 | if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) | ||||
7418 | return indicatePessimisticFixpoint(); | ||||
7419 | |||||
7420 | return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED | ||||
7421 | : ChangeStatus::UNCHANGED; | ||||
7422 | } | ||||
7423 | |||||
7424 | bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use &U, | ||||
7425 | const Instruction *UserI) { | ||||
7426 | // The loaded value is unrelated to the pointer argument, no need to | ||||
7427 | // follow the users of the load. | ||||
7428 | if (isa<LoadInst>(UserI)) | ||||
7429 | return false; | ||||
7430 | |||||
7431 | // By default we follow all uses assuming UserI might leak information on U, | ||||
7432 | // we have special handling for call sites operands though. | ||||
7433 | const auto *CB = dyn_cast<CallBase>(UserI); | ||||
7434 | if (!CB || !CB->isArgOperand(&U)) | ||||
7435 | return true; | ||||
7436 | |||||
7437 | // If the use is a call argument known not to be captured, the users of | ||||
7438 | // the call do not need to be visited because they have to be unrelated to | ||||
7439 | // the input. Note that this check is not trivial even though we disallow | ||||
7440 | // general capturing of the underlying argument. The reason is that the | ||||
7441 | // call might the argument "through return", which we allow and for which we | ||||
7442 | // need to check call users. | ||||
7443 | if (U.get()->getType()->isPointerTy()) { | ||||
7444 | unsigned ArgNo = CB->getArgOperandNo(&U); | ||||
7445 | const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>( | ||||
7446 | *this, IRPosition::callsite_argument(*CB, ArgNo), DepClassTy::OPTIONAL); | ||||
7447 | return !ArgNoCaptureAA.isAssumedNoCapture(); | ||||
7448 | } | ||||
7449 | |||||
7450 | return true; | ||||
7451 | } | ||||
7452 | |||||
7453 | void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use &U, | ||||
7454 | const Instruction *UserI) { | ||||
7455 | assert(UserI->mayReadOrWriteMemory())(static_cast <bool> (UserI->mayReadOrWriteMemory()) ? void (0) : __assert_fail ("UserI->mayReadOrWriteMemory()" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 7455, __extension__ __PRETTY_FUNCTION__)); | ||||
7456 | |||||
7457 | switch (UserI->getOpcode()) { | ||||
7458 | default: | ||||
7459 | // TODO: Handle all atomics and other side-effect operations we know of. | ||||
7460 | break; | ||||
7461 | case Instruction::Load: | ||||
7462 | // Loads cause the NO_READS property to disappear. | ||||
7463 | removeAssumedBits(NO_READS); | ||||
7464 | return; | ||||
7465 | |||||
7466 | case Instruction::Store: | ||||
7467 | // Stores cause the NO_WRITES property to disappear if the use is the | ||||
7468 | // pointer operand. Note that while capturing was taken care of somewhere | ||||
7469 | // else we need to deal with stores of the value that is not looked through. | ||||
7470 | if (cast<StoreInst>(UserI)->getPointerOperand() == U.get()) | ||||
7471 | removeAssumedBits(NO_WRITES); | ||||
7472 | else | ||||
7473 | indicatePessimisticFixpoint(); | ||||
7474 | return; | ||||
7475 | |||||
7476 | case Instruction::Call: | ||||
7477 | case Instruction::CallBr: | ||||
7478 | case Instruction::Invoke: { | ||||
7479 | // For call sites we look at the argument memory behavior attribute (this | ||||
7480 | // could be recursive!) in order to restrict our own state. | ||||
7481 | const auto *CB = cast<CallBase>(UserI); | ||||
7482 | |||||
7483 | // Give up on operand bundles. | ||||
7484 | if (CB->isBundleOperand(&U)) { | ||||
7485 | indicatePessimisticFixpoint(); | ||||
7486 | return; | ||||
7487 | } | ||||
7488 | |||||
7489 | // Calling a function does read the function pointer, maybe write it if the | ||||
7490 | // function is self-modifying. | ||||
7491 | if (CB->isCallee(&U)) { | ||||
7492 | removeAssumedBits(NO_READS); | ||||
7493 | break; | ||||
7494 | } | ||||
7495 | |||||
7496 | // Adjust the possible access behavior based on the information on the | ||||
7497 | // argument. | ||||
7498 | IRPosition Pos; | ||||
7499 | if (U.get()->getType()->isPointerTy()) | ||||
7500 | Pos = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U)); | ||||
7501 | else | ||||
7502 | Pos = IRPosition::callsite_function(*CB); | ||||
7503 | const auto &MemBehaviorAA = | ||||
7504 | A.getAAFor<AAMemoryBehavior>(*this, Pos, DepClassTy::OPTIONAL); | ||||
7505 | // "assumed" has at most the same bits as the MemBehaviorAA assumed | ||||
7506 | // and at least "known". | ||||
7507 | intersectAssumedBits(MemBehaviorAA.getAssumed()); | ||||
7508 | return; | ||||
7509 | } | ||||
7510 | }; | ||||
7511 | |||||
7512 | // Generally, look at the "may-properties" and adjust the assumed state if we | ||||
7513 | // did not trigger special handling before. | ||||
7514 | if (UserI->mayReadFromMemory()) | ||||
7515 | removeAssumedBits(NO_READS); | ||||
7516 | if (UserI->mayWriteToMemory()) | ||||
7517 | removeAssumedBits(NO_WRITES); | ||||
7518 | } | ||||
7519 | |||||
7520 | /// -------------------- Memory Locations Attributes --------------------------- | ||||
7521 | /// Includes read-none, argmemonly, inaccessiblememonly, | ||||
7522 | /// inaccessiblememorargmemonly | ||||
7523 | /// ---------------------------------------------------------------------------- | ||||
7524 | |||||
7525 | std::string AAMemoryLocation::getMemoryLocationsAsStr( | ||||
7526 | AAMemoryLocation::MemoryLocationsKind MLK) { | ||||
7527 | if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS)) | ||||
7528 | return "all memory"; | ||||
7529 | if (MLK == AAMemoryLocation::NO_LOCATIONS) | ||||
7530 | return "no memory"; | ||||
7531 | std::string S = "memory:"; | ||||
7532 | if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM)) | ||||
7533 | S += "stack,"; | ||||
7534 | if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM)) | ||||
7535 | S += "constant,"; | ||||
7536 | if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM)) | ||||
7537 | S += "internal global,"; | ||||
7538 | if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM)) | ||||
7539 | S += "external global,"; | ||||
7540 | if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM)) | ||||
7541 | S += "argument,"; | ||||
7542 | if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM)) | ||||
7543 | S += "inaccessible,"; | ||||
7544 | if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM)) | ||||
7545 | S += "malloced,"; | ||||
7546 | if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM)) | ||||
7547 | S += "unknown,"; | ||||
7548 | S.pop_back(); | ||||
7549 | return S; | ||||
7550 | } | ||||
7551 | |||||
7552 | struct AAMemoryLocationImpl : public AAMemoryLocation { | ||||
7553 | |||||
7554 | AAMemoryLocationImpl(const IRPosition &IRP, Attributor &A) | ||||
7555 | : AAMemoryLocation(IRP, A), Allocator(A.Allocator) { | ||||
7556 | for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u) | ||||
7557 | AccessKind2Accesses[u] = nullptr; | ||||
7558 | } | ||||
7559 | |||||
7560 | ~AAMemoryLocationImpl() { | ||||
7561 | // The AccessSets are allocated via a BumpPtrAllocator, we call | ||||
7562 | // the destructor manually. | ||||
7563 | for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u) | ||||
7564 | if (AccessKind2Accesses[u]) | ||||
7565 | AccessKind2Accesses[u]->~AccessSet(); | ||||
7566 | } | ||||
7567 | |||||
7568 | /// See AbstractAttribute::initialize(...). | ||||
7569 | void initialize(Attributor &A) override { | ||||
7570 | intersectAssumedBits(BEST_STATE); | ||||
7571 | getKnownStateFromValue(A, getIRPosition(), getState()); | ||||
7572 | AAMemoryLocation::initialize(A); | ||||
7573 | } | ||||
7574 | |||||
7575 | /// Return the memory behavior information encoded in the IR for \p IRP. | ||||
7576 | static void getKnownStateFromValue(Attributor &A, const IRPosition &IRP, | ||||
7577 | BitIntegerState &State, | ||||
7578 | bool IgnoreSubsumingPositions = false) { | ||||
7579 | // For internal functions we ignore `argmemonly` and | ||||
7580 | // `inaccessiblememorargmemonly` as we might break it via interprocedural | ||||
7581 | // constant propagation. It is unclear if this is the best way but it is | ||||
7582 | // unlikely this will cause real performance problems. If we are deriving | ||||
7583 | // attributes for the anchor function we even remove the attribute in | ||||
7584 | // addition to ignoring it. | ||||
7585 | bool UseArgMemOnly = true; | ||||
7586 | Function *AnchorFn = IRP.getAnchorScope(); | ||||
7587 | if (AnchorFn && A.isRunOn(*AnchorFn)) | ||||
7588 | UseArgMemOnly = !AnchorFn->hasLocalLinkage(); | ||||
7589 | |||||
7590 | SmallVector<Attribute, 2> Attrs; | ||||
7591 | IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions); | ||||
7592 | for (const Attribute &Attr : Attrs) { | ||||
7593 | switch (Attr.getKindAsEnum()) { | ||||
7594 | case Attribute::ReadNone: | ||||
7595 | State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM); | ||||
7596 | break; | ||||
7597 | case Attribute::InaccessibleMemOnly: | ||||
7598 | State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true)); | ||||
7599 | break; | ||||
7600 | case Attribute::ArgMemOnly: | ||||
7601 | if (UseArgMemOnly) | ||||
7602 | State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true)); | ||||
7603 | else | ||||
7604 | IRP.removeAttrs({Attribute::ArgMemOnly}); | ||||
7605 | break; | ||||
7606 | case Attribute::InaccessibleMemOrArgMemOnly: | ||||
7607 | if (UseArgMemOnly) | ||||
7608 | State.addKnownBits(inverseLocation( | ||||
7609 | NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true)); | ||||
7610 | else | ||||
7611 | IRP.removeAttrs({Attribute::InaccessibleMemOrArgMemOnly}); | ||||
7612 | break; | ||||
7613 | default: | ||||
7614 | llvm_unreachable("Unexpected attribute!")::llvm::llvm_unreachable_internal("Unexpected attribute!", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 7614); | ||||
7615 | } | ||||
7616 | } | ||||
7617 | } | ||||
7618 | |||||
7619 | /// See AbstractAttribute::getDeducedAttributes(...). | ||||
7620 | void getDeducedAttributes(LLVMContext &Ctx, | ||||
7621 | SmallVectorImpl<Attribute> &Attrs) const override { | ||||
7622 | assert(Attrs.size() == 0)(static_cast <bool> (Attrs.size() == 0) ? void (0) : __assert_fail ("Attrs.size() == 0", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 7622, __extension__ __PRETTY_FUNCTION__)); | ||||
7623 | if (isAssumedReadNone()) { | ||||
7624 | Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone)); | ||||
7625 | } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) { | ||||
7626 | if (isAssumedInaccessibleMemOnly()) | ||||
7627 | Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly)); | ||||
7628 | else if (isAssumedArgMemOnly()) | ||||
7629 | Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly)); | ||||
7630 | else if (isAssumedInaccessibleOrArgMemOnly()) | ||||
7631 | Attrs.push_back( | ||||
7632 | Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly)); | ||||
7633 | } | ||||
7634 | assert(Attrs.size() <= 1)(static_cast <bool> (Attrs.size() <= 1) ? void (0) : __assert_fail ("Attrs.size() <= 1", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 7634, __extension__ __PRETTY_FUNCTION__)); | ||||
7635 | } | ||||
7636 | |||||
7637 | /// See AbstractAttribute::manifest(...). | ||||
7638 | ChangeStatus manifest(Attributor &A) override { | ||||
7639 | const IRPosition &IRP = getIRPosition(); | ||||
7640 | |||||
7641 | // Check if we would improve the existing attributes first. | ||||
7642 | SmallVector<Attribute, 4> DeducedAttrs; | ||||
7643 | getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs); | ||||
7644 | if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) { | ||||
7645 | return IRP.hasAttr(Attr.getKindAsEnum(), | ||||
7646 | /* IgnoreSubsumingPositions */ true); | ||||
7647 | })) | ||||
7648 | return ChangeStatus::UNCHANGED; | ||||
7649 | |||||
7650 | // Clear existing attributes. | ||||
7651 | IRP.removeAttrs(AttrKinds); | ||||
7652 | if (isAssumedReadNone()) | ||||
7653 | IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds); | ||||
7654 | |||||
7655 | // Use the generic manifest method. | ||||
7656 | return IRAttribute::manifest(A); | ||||
7657 | } | ||||
7658 | |||||
7659 | /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...). | ||||
7660 | bool checkForAllAccessesToMemoryKind( | ||||
7661 | function_ref<bool(const Instruction *, const Value *, AccessKind, | ||||
7662 | MemoryLocationsKind)> | ||||
7663 | Pred, | ||||
7664 | MemoryLocationsKind RequestedMLK) const override { | ||||
7665 | if (!isValidState()) | ||||
7666 | return false; | ||||
7667 | |||||
7668 | MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation(); | ||||
7669 | if (AssumedMLK == NO_LOCATIONS) | ||||
7670 | return true; | ||||
7671 | |||||
7672 | unsigned Idx = 0; | ||||
7673 | for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; | ||||
7674 | CurMLK *= 2, ++Idx) { | ||||
7675 | if (CurMLK & RequestedMLK) | ||||
7676 | continue; | ||||
7677 | |||||
7678 | if (const AccessSet *Accesses = AccessKind2Accesses[Idx]) | ||||
7679 | for (const AccessInfo &AI : *Accesses) | ||||
7680 | if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK)) | ||||
7681 | return false; | ||||
7682 | } | ||||
7683 | |||||
7684 | return true; | ||||
7685 | } | ||||
7686 | |||||
7687 | ChangeStatus indicatePessimisticFixpoint() override { | ||||
7688 | // If we give up and indicate a pessimistic fixpoint this instruction will | ||||
7689 | // become an access for all potential access kinds: | ||||
7690 | // TODO: Add pointers for argmemonly and globals to improve the results of | ||||
7691 | // checkForAllAccessesToMemoryKind. | ||||
7692 | bool Changed = false; | ||||
7693 | MemoryLocationsKind KnownMLK = getKnown(); | ||||
7694 | Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()); | ||||
7695 | for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) | ||||
7696 | if (!(CurMLK & KnownMLK)) | ||||
7697 | updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed, | ||||
7698 | getAccessKindFromInst(I)); | ||||
7699 | return AAMemoryLocation::indicatePessimisticFixpoint(); | ||||
7700 | } | ||||
7701 | |||||
7702 | protected: | ||||
7703 | /// Helper struct to tie together an instruction that has a read or write | ||||
7704 | /// effect with the pointer it accesses (if any). | ||||
7705 | struct AccessInfo { | ||||
7706 | |||||
7707 | /// The instruction that caused the access. | ||||
7708 | const Instruction *I; | ||||
7709 | |||||
7710 | /// The base pointer that is accessed, or null if unknown. | ||||
7711 | const Value *Ptr; | ||||
7712 | |||||
7713 | /// The kind of access (read/write/read+write). | ||||
7714 | AccessKind Kind; | ||||
7715 | |||||
7716 | bool operator==(const AccessInfo &RHS) const { | ||||
7717 | return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind; | ||||
7718 | } | ||||
7719 | bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const { | ||||
7720 | if (LHS.I != RHS.I) | ||||
7721 | return LHS.I < RHS.I; | ||||
7722 | if (LHS.Ptr != RHS.Ptr) | ||||
7723 | return LHS.Ptr < RHS.Ptr; | ||||
7724 | if (LHS.Kind != RHS.Kind) | ||||
7725 | return LHS.Kind < RHS.Kind; | ||||
7726 | return false; | ||||
7727 | } | ||||
7728 | }; | ||||
7729 | |||||
7730 | /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the | ||||
7731 | /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind. | ||||
7732 | using AccessSet = SmallSet<AccessInfo, 2, AccessInfo>; | ||||
7733 | AccessSet *AccessKind2Accesses[llvm::CTLog2<VALID_STATE>()]; | ||||
7734 | |||||
7735 | /// Categorize the pointer arguments of CB that might access memory in | ||||
7736 | /// AccessedLoc and update the state and access map accordingly. | ||||
7737 | void | ||||
7738 | categorizeArgumentPointerLocations(Attributor &A, CallBase &CB, | ||||
7739 | AAMemoryLocation::StateType &AccessedLocs, | ||||
7740 | bool &Changed); | ||||
7741 | |||||
7742 | /// Return the kind(s) of location that may be accessed by \p V. | ||||
7743 | AAMemoryLocation::MemoryLocationsKind | ||||
7744 | categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed); | ||||
7745 | |||||
7746 | /// Return the access kind as determined by \p I. | ||||
7747 | AccessKind getAccessKindFromInst(const Instruction *I) { | ||||
7748 | AccessKind AK = READ_WRITE; | ||||
7749 | if (I) { | ||||
7750 | AK = I->mayReadFromMemory() ? READ : NONE; | ||||
7751 | AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE)); | ||||
7752 | } | ||||
7753 | return AK; | ||||
7754 | } | ||||
7755 | |||||
7756 | /// Update the state \p State and the AccessKind2Accesses given that \p I is | ||||
7757 | /// an access of kind \p AK to a \p MLK memory location with the access | ||||
7758 | /// pointer \p Ptr. | ||||
7759 | void updateStateAndAccessesMap(AAMemoryLocation::StateType &State, | ||||
7760 | MemoryLocationsKind MLK, const Instruction *I, | ||||
7761 | const Value *Ptr, bool &Changed, | ||||
7762 | AccessKind AK = READ_WRITE) { | ||||
7763 | |||||
7764 | assert(isPowerOf2_32(MLK) && "Expected a single location set!")(static_cast <bool> (isPowerOf2_32(MLK) && "Expected a single location set!" ) ? void (0) : __assert_fail ("isPowerOf2_32(MLK) && \"Expected a single location set!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 7764, __extension__ __PRETTY_FUNCTION__)); | ||||
7765 | auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)]; | ||||
7766 | if (!Accesses) | ||||
7767 | Accesses = new (Allocator) AccessSet(); | ||||
7768 | Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second; | ||||
7769 | State.removeAssumedBits(MLK); | ||||
7770 | } | ||||
7771 | |||||
7772 | /// Determine the underlying locations kinds for \p Ptr, e.g., globals or | ||||
7773 | /// arguments, and update the state and access map accordingly. | ||||
7774 | void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr, | ||||
7775 | AAMemoryLocation::StateType &State, bool &Changed); | ||||
7776 | |||||
7777 | /// Used to allocate access sets. | ||||
7778 | BumpPtrAllocator &Allocator; | ||||
7779 | |||||
7780 | /// The set of IR attributes AAMemoryLocation deals with. | ||||
7781 | static const Attribute::AttrKind AttrKinds[4]; | ||||
7782 | }; | ||||
7783 | |||||
7784 | const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = { | ||||
7785 | Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly, | ||||
7786 | Attribute::InaccessibleMemOrArgMemOnly}; | ||||
7787 | |||||
7788 | void AAMemoryLocationImpl::categorizePtrValue( | ||||
7789 | Attributor &A, const Instruction &I, const Value &Ptr, | ||||
7790 | AAMemoryLocation::StateType &State, bool &Changed) { | ||||
7791 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize pointer locations for " << Ptr << " [" << getMemoryLocationsAsStr( State.getAssumed()) << "]\n"; } } while (false) | ||||
7792 | << Ptr << " ["do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize pointer locations for " << Ptr << " [" << getMemoryLocationsAsStr( State.getAssumed()) << "]\n"; } } while (false) | ||||
7793 | << getMemoryLocationsAsStr(State.getAssumed()) << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize pointer locations for " << Ptr << " [" << getMemoryLocationsAsStr( State.getAssumed()) << "]\n"; } } while (false); | ||||
7794 | |||||
7795 | SmallVector<Value *, 8> Objects; | ||||
7796 | bool UsedAssumedInformation = false; | ||||
7797 | if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, *this, &I, | ||||
7798 | UsedAssumedInformation, | ||||
7799 | /* Intraprocedural */ true)) { | ||||
7800 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n" ; } } while (false) | ||||
7801 | dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n" ; } } while (false); | ||||
7802 | updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed, | ||||
7803 | getAccessKindFromInst(&I)); | ||||
7804 | return; | ||||
7805 | } | ||||
7806 | |||||
7807 | for (Value *Obj : Objects) { | ||||
7808 | // TODO: recognize the TBAA used for constant accesses. | ||||
7809 | MemoryLocationsKind MLK = NO_LOCATIONS; | ||||
7810 | if (isa<UndefValue>(Obj)) | ||||
7811 | continue; | ||||
7812 | if (isa<Argument>(Obj)) { | ||||
7813 | // TODO: For now we do not treat byval arguments as local copies performed | ||||
7814 | // on the call edge, though, we should. To make that happen we need to | ||||
7815 | // teach various passes, e.g., DSE, about the copy effect of a byval. That | ||||
7816 | // would also allow us to mark functions only accessing byval arguments as | ||||
7817 | // readnone again, atguably their acceses have no effect outside of the | ||||
7818 | // function, like accesses to allocas. | ||||
7819 | MLK = NO_ARGUMENT_MEM; | ||||
7820 | } else if (auto *GV = dyn_cast<GlobalValue>(Obj)) { | ||||
7821 | // Reading constant memory is not treated as a read "effect" by the | ||||
7822 | // function attr pass so we won't neither. Constants defined by TBAA are | ||||
7823 | // similar. (We know we do not write it because it is constant.) | ||||
7824 | if (auto *GVar = dyn_cast<GlobalVariable>(GV)) | ||||
7825 | if (GVar->isConstant()) | ||||
7826 | continue; | ||||
7827 | |||||
7828 | if (GV->hasLocalLinkage()) | ||||
7829 | MLK = NO_GLOBAL_INTERNAL_MEM; | ||||
7830 | else | ||||
7831 | MLK = NO_GLOBAL_EXTERNAL_MEM; | ||||
7832 | } else if (isa<ConstantPointerNull>(Obj) && | ||||
7833 | !NullPointerIsDefined(getAssociatedFunction(), | ||||
7834 | Ptr.getType()->getPointerAddressSpace())) { | ||||
7835 | continue; | ||||
7836 | } else if (isa<AllocaInst>(Obj)) { | ||||
7837 | MLK = NO_LOCAL_MEM; | ||||
7838 | } else if (const auto *CB = dyn_cast<CallBase>(Obj)) { | ||||
7839 | const auto &NoAliasAA = A.getAAFor<AANoAlias>( | ||||
7840 | *this, IRPosition::callsite_returned(*CB), DepClassTy::OPTIONAL); | ||||
7841 | if (NoAliasAA.isAssumedNoAlias()) | ||||
7842 | MLK = NO_MALLOCED_MEM; | ||||
7843 | else | ||||
7844 | MLK = NO_UNKOWN_MEM; | ||||
7845 | } else { | ||||
7846 | MLK = NO_UNKOWN_MEM; | ||||
7847 | } | ||||
7848 | |||||
7849 | assert(MLK != NO_LOCATIONS && "No location specified!")(static_cast <bool> (MLK != NO_LOCATIONS && "No location specified!" ) ? void (0) : __assert_fail ("MLK != NO_LOCATIONS && \"No location specified!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 7849, __extension__ __PRETTY_FUNCTION__)); | ||||
7850 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value can be categorized: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Ptr value can be categorized: " << *Obj << " -> " << getMemoryLocationsAsStr (MLK) << "\n"; } } while (false) | ||||
7851 | << *Obj << " -> " << getMemoryLocationsAsStr(MLK)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Ptr value can be categorized: " << *Obj << " -> " << getMemoryLocationsAsStr (MLK) << "\n"; } } while (false) | ||||
7852 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Ptr value can be categorized: " << *Obj << " -> " << getMemoryLocationsAsStr (MLK) << "\n"; } } while (false); | ||||
7853 | updateStateAndAccessesMap(getState(), MLK, &I, Obj, Changed, | ||||
7854 | getAccessKindFromInst(&I)); | ||||
7855 | } | ||||
7856 | |||||
7857 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: " << getMemoryLocationsAsStr(State.getAssumed()) << "\n"; } } while (false) | ||||
7858 | dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: " << getMemoryLocationsAsStr(State.getAssumed()) << "\n"; } } while (false) | ||||
7859 | << getMemoryLocationsAsStr(State.getAssumed()) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: " << getMemoryLocationsAsStr(State.getAssumed()) << "\n"; } } while (false); | ||||
7860 | } | ||||
7861 | |||||
7862 | void AAMemoryLocationImpl::categorizeArgumentPointerLocations( | ||||
7863 | Attributor &A, CallBase &CB, AAMemoryLocation::StateType &AccessedLocs, | ||||
7864 | bool &Changed) { | ||||
7865 | for (unsigned ArgNo = 0, E = CB.arg_size(); ArgNo < E; ++ArgNo) { | ||||
7866 | |||||
7867 | // Skip non-pointer arguments. | ||||
7868 | const Value *ArgOp = CB.getArgOperand(ArgNo); | ||||
7869 | if (!ArgOp->getType()->isPtrOrPtrVectorTy()) | ||||
7870 | continue; | ||||
7871 | |||||
7872 | // Skip readnone arguments. | ||||
7873 | const IRPosition &ArgOpIRP = IRPosition::callsite_argument(CB, ArgNo); | ||||
7874 | const auto &ArgOpMemLocationAA = | ||||
7875 | A.getAAFor<AAMemoryBehavior>(*this, ArgOpIRP, DepClassTy::OPTIONAL); | ||||
7876 | |||||
7877 | if (ArgOpMemLocationAA.isAssumedReadNone()) | ||||
7878 | continue; | ||||
7879 | |||||
7880 | // Categorize potentially accessed pointer arguments as if there was an | ||||
7881 | // access instruction with them as pointer. | ||||
7882 | categorizePtrValue(A, CB, *ArgOp, AccessedLocs, Changed); | ||||
7883 | } | ||||
7884 | } | ||||
7885 | |||||
7886 | AAMemoryLocation::MemoryLocationsKind | ||||
7887 | AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I, | ||||
7888 | bool &Changed) { | ||||
7889 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize accessed locations for " << I << "\n"; } } while (false) | ||||
7890 | << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize accessed locations for " << I << "\n"; } } while (false); | ||||
7891 | |||||
7892 | AAMemoryLocation::StateType AccessedLocs; | ||||
7893 | AccessedLocs.intersectAssumedBits(NO_LOCATIONS); | ||||
7894 | |||||
7895 | if (auto *CB = dyn_cast<CallBase>(&I)) { | ||||
7896 | |||||
7897 | // First check if we assume any memory is access is visible. | ||||
7898 | const auto &CBMemLocationAA = A.getAAFor<AAMemoryLocation>( | ||||
7899 | *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL); | ||||
7900 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << Ido { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize call site: " << I << " [" << CBMemLocationAA << "]\n" ; } } while (false) | ||||
7901 | << " [" << CBMemLocationAA << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize call site: " << I << " [" << CBMemLocationAA << "]\n" ; } } while (false); | ||||
7902 | |||||
7903 | if (CBMemLocationAA.isAssumedReadNone()) | ||||
7904 | return NO_LOCATIONS; | ||||
7905 | |||||
7906 | if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) { | ||||
7907 | updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr, | ||||
7908 | Changed, getAccessKindFromInst(&I)); | ||||
7909 | return AccessedLocs.getAssumed(); | ||||
7910 | } | ||||
7911 | |||||
7912 | uint32_t CBAssumedNotAccessedLocs = | ||||
7913 | CBMemLocationAA.getAssumedNotAccessedLocation(); | ||||
7914 | |||||
7915 | // Set the argmemonly and global bit as we handle them separately below. | ||||
7916 | uint32_t CBAssumedNotAccessedLocsNoArgMem = | ||||
7917 | CBAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM; | ||||
7918 | |||||
7919 | for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) { | ||||
7920 | if (CBAssumedNotAccessedLocsNoArgMem & CurMLK) | ||||
7921 | continue; | ||||
7922 | updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed, | ||||
7923 | getAccessKindFromInst(&I)); | ||||
7924 | } | ||||
7925 | |||||
7926 | // Now handle global memory if it might be accessed. This is slightly tricky | ||||
7927 | // as NO_GLOBAL_MEM has multiple bits set. | ||||
7928 | bool HasGlobalAccesses = ((~CBAssumedNotAccessedLocs) & NO_GLOBAL_MEM); | ||||
7929 | if (HasGlobalAccesses) { | ||||
7930 | auto AccessPred = [&](const Instruction *, const Value *Ptr, | ||||
7931 | AccessKind Kind, MemoryLocationsKind MLK) { | ||||
7932 | updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed, | ||||
7933 | getAccessKindFromInst(&I)); | ||||
7934 | return true; | ||||
7935 | }; | ||||
7936 | if (!CBMemLocationAA.checkForAllAccessesToMemoryKind( | ||||
7937 | AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false))) | ||||
7938 | return AccessedLocs.getWorstState(); | ||||
7939 | } | ||||
7940 | |||||
7941 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed state before argument handling: " << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"; } } while (false) | ||||
7942 | dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed state before argument handling: " << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"; } } while (false) | ||||
7943 | << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed state before argument handling: " << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"; } } while (false); | ||||
7944 | |||||
7945 | // Now handle argument memory if it might be accessed. | ||||
7946 | bool HasArgAccesses = ((~CBAssumedNotAccessedLocs) & NO_ARGUMENT_MEM); | ||||
7947 | if (HasArgAccesses) | ||||
7948 | categorizeArgumentPointerLocations(A, *CB, AccessedLocs, Changed); | ||||
7949 | |||||
7950 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed state after argument handling: " << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"; } } while (false) | ||||
7951 | dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed state after argument handling: " << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"; } } while (false) | ||||
7952 | << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed state after argument handling: " << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n"; } } while (false); | ||||
7953 | |||||
7954 | return AccessedLocs.getAssumed(); | ||||
7955 | } | ||||
7956 | |||||
7957 | if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) { | ||||
7958 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: " << I << " [" << *Ptr << "]\n"; } } while (false) | ||||
7959 | dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: " << I << " [" << *Ptr << "]\n"; } } while (false) | ||||
7960 | << I << " [" << *Ptr << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: " << I << " [" << *Ptr << "]\n"; } } while (false); | ||||
7961 | categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed); | ||||
7962 | return AccessedLocs.getAssumed(); | ||||
7963 | } | ||||
7964 | |||||
7965 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Failed to categorize instruction: " << I << "\n"; } } while (false) | ||||
7966 | << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Failed to categorize instruction: " << I << "\n"; } } while (false); | ||||
7967 | updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed, | ||||
7968 | getAccessKindFromInst(&I)); | ||||
7969 | return AccessedLocs.getAssumed(); | ||||
7970 | } | ||||
7971 | |||||
7972 | /// An AA to represent the memory behavior function attributes. | ||||
7973 | struct AAMemoryLocationFunction final : public AAMemoryLocationImpl { | ||||
7974 | AAMemoryLocationFunction(const IRPosition &IRP, Attributor &A) | ||||
7975 | : AAMemoryLocationImpl(IRP, A) {} | ||||
7976 | |||||
7977 | /// See AbstractAttribute::updateImpl(Attributor &A). | ||||
7978 | virtual ChangeStatus updateImpl(Attributor &A) override { | ||||
7979 | |||||
7980 | const auto &MemBehaviorAA = | ||||
7981 | A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE); | ||||
7982 | if (MemBehaviorAA.isAssumedReadNone()) { | ||||
7983 | if (MemBehaviorAA.isKnownReadNone()) | ||||
7984 | return indicateOptimisticFixpoint(); | ||||
7985 | assert(isAssumedReadNone() &&(static_cast <bool> (isAssumedReadNone() && "AAMemoryLocation was not read-none but AAMemoryBehavior was!" ) ? void (0) : __assert_fail ("isAssumedReadNone() && \"AAMemoryLocation was not read-none but AAMemoryBehavior was!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 7986, __extension__ __PRETTY_FUNCTION__)) | ||||
7986 | "AAMemoryLocation was not read-none but AAMemoryBehavior was!")(static_cast <bool> (isAssumedReadNone() && "AAMemoryLocation was not read-none but AAMemoryBehavior was!" ) ? void (0) : __assert_fail ("isAssumedReadNone() && \"AAMemoryLocation was not read-none but AAMemoryBehavior was!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 7986, __extension__ __PRETTY_FUNCTION__)); | ||||
7987 | A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL); | ||||
7988 | return ChangeStatus::UNCHANGED; | ||||
7989 | } | ||||
7990 | |||||
7991 | // The current assumed state used to determine a change. | ||||
7992 | auto AssumedState = getAssumed(); | ||||
7993 | bool Changed = false; | ||||
7994 | |||||
7995 | auto CheckRWInst = [&](Instruction &I) { | ||||
7996 | MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed); | ||||
7997 | LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << Ido { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed locations for " << I << ": " << getMemoryLocationsAsStr(MLK ) << "\n"; } } while (false) | ||||
7998 | << ": " << getMemoryLocationsAsStr(MLK) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAMemoryLocation] Accessed locations for " << I << ": " << getMemoryLocationsAsStr(MLK ) << "\n"; } } while (false); | ||||
7999 | removeAssumedBits(inverseLocation(MLK, false, false)); | ||||
8000 | // Stop once only the valid bit set in the *not assumed location*, thus | ||||
8001 | // once we don't actually exclude any memory locations in the state. | ||||
8002 | return getAssumedNotAccessedLocation() != VALID_STATE; | ||||
8003 | }; | ||||
8004 | |||||
8005 | bool UsedAssumedInformation = false; | ||||
8006 | if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this, | ||||
8007 | UsedAssumedInformation)) | ||||
8008 | return indicatePessimisticFixpoint(); | ||||
8009 | |||||
8010 | Changed |= AssumedState != getAssumed(); | ||||
8011 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | ||||
8012 | } | ||||
8013 | |||||
8014 | /// See AbstractAttribute::trackStatistics() | ||||
8015 | void trackStatistics() const override { | ||||
8016 | if (isAssumedReadNone()) | ||||
8017 | STATS_DECLTRACK_FN_ATTR(readnone){ static llvm::Statistic NumIRFunction_readnone = {"attributor" , "NumIRFunction_readnone", ("Number of " "functions" " marked '" "readnone" "'")};; ++(NumIRFunction_readnone); } | ||||
8018 | else if (isAssumedArgMemOnly()) | ||||
8019 | STATS_DECLTRACK_FN_ATTR(argmemonly){ static llvm::Statistic NumIRFunction_argmemonly = {"attributor" , "NumIRFunction_argmemonly", ("Number of " "functions" " marked '" "argmemonly" "'")};; ++(NumIRFunction_argmemonly); } | ||||
8020 | else if (isAssumedInaccessibleMemOnly()) | ||||
8021 | STATS_DECLTRACK_FN_ATTR(inaccessiblememonly){ static llvm::Statistic NumIRFunction_inaccessiblememonly = { "attributor", "NumIRFunction_inaccessiblememonly", ("Number of " "functions" " marked '" "inaccessiblememonly" "'")};; ++(NumIRFunction_inaccessiblememonly ); } | ||||
8022 | else if (isAssumedInaccessibleOrArgMemOnly()) | ||||
8023 | STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly){ static llvm::Statistic NumIRFunction_inaccessiblememorargmemonly = {"attributor", "NumIRFunction_inaccessiblememorargmemonly" , ("Number of " "functions" " marked '" "inaccessiblememorargmemonly" "'")};; ++(NumIRFunction_inaccessiblememorargmemonly); } | ||||
8024 | } | ||||
8025 | }; | ||||
8026 | |||||
8027 | /// AAMemoryLocation attribute for call sites. | ||||
8028 | struct AAMemoryLocationCallSite final : AAMemoryLocationImpl { | ||||
8029 | AAMemoryLocationCallSite(const IRPosition &IRP, Attributor &A) | ||||
8030 | : AAMemoryLocationImpl(IRP, A) {} | ||||
8031 | |||||
8032 | /// See AbstractAttribute::initialize(...). | ||||
8033 | void initialize(Attributor &A) override { | ||||
8034 | AAMemoryLocationImpl::initialize(A); | ||||
8035 | Function *F = getAssociatedFunction(); | ||||
8036 | if (!F || F->isDeclaration()) | ||||
8037 | indicatePessimisticFixpoint(); | ||||
8038 | } | ||||
8039 | |||||
8040 | /// See AbstractAttribute::updateImpl(...). | ||||
8041 | ChangeStatus updateImpl(Attributor &A) override { | ||||
8042 | // TODO: Once we have call site specific value information we can provide | ||||
8043 | // call site specific liveness liveness information and then it makes | ||||
8044 | // sense to specialize attributes for call sites arguments instead of | ||||
8045 | // redirecting requests to the callee argument. | ||||
8046 | Function *F = getAssociatedFunction(); | ||||
8047 | const IRPosition &FnPos = IRPosition::function(*F); | ||||
8048 | auto &FnAA = | ||||
8049 | A.getAAFor<AAMemoryLocation>(*this, FnPos, DepClassTy::REQUIRED); | ||||
8050 | bool Changed = false; | ||||
8051 | auto AccessPred = [&](const Instruction *I, const Value *Ptr, | ||||
8052 | AccessKind Kind, MemoryLocationsKind MLK) { | ||||
8053 | updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed, | ||||
8054 | getAccessKindFromInst(I)); | ||||
8055 | return true; | ||||
8056 | }; | ||||
8057 | if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS)) | ||||
8058 | return indicatePessimisticFixpoint(); | ||||
8059 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | ||||
8060 | } | ||||
8061 | |||||
8062 | /// See AbstractAttribute::trackStatistics() | ||||
8063 | void trackStatistics() const override { | ||||
8064 | if (isAssumedReadNone()) | ||||
8065 | STATS_DECLTRACK_CS_ATTR(readnone){ static llvm::Statistic NumIRCS_readnone = {"attributor", "NumIRCS_readnone" , ("Number of " "call site" " marked '" "readnone" "'")};; ++ (NumIRCS_readnone); } | ||||
8066 | } | ||||
8067 | }; | ||||
8068 | |||||
8069 | /// ------------------ Value Constant Range Attribute ------------------------- | ||||
8070 | |||||
8071 | struct AAValueConstantRangeImpl : AAValueConstantRange { | ||||
8072 | using StateType = IntegerRangeState; | ||||
8073 | AAValueConstantRangeImpl(const IRPosition &IRP, Attributor &A) | ||||
8074 | : AAValueConstantRange(IRP, A) {} | ||||
8075 | |||||
8076 | /// See AbstractAttribute::initialize(..). | ||||
8077 | void initialize(Attributor &A) override { | ||||
8078 | if (A.hasSimplificationCallback(getIRPosition())) { | ||||
8079 | indicatePessimisticFixpoint(); | ||||
8080 | return; | ||||
8081 | } | ||||
8082 | |||||
8083 | // Intersect a range given by SCEV. | ||||
8084 | intersectKnown(getConstantRangeFromSCEV(A, getCtxI())); | ||||
8085 | |||||
8086 | // Intersect a range given by LVI. | ||||
8087 | intersectKnown(getConstantRangeFromLVI(A, getCtxI())); | ||||
8088 | } | ||||
8089 | |||||
8090 | /// See AbstractAttribute::getAsStr(). | ||||
8091 | const std::string getAsStr() const override { | ||||
8092 | std::string Str; | ||||
8093 | llvm::raw_string_ostream OS(Str); | ||||
8094 | OS << "range(" << getBitWidth() << ")<"; | ||||
8095 | getKnown().print(OS); | ||||
8096 | OS << " / "; | ||||
8097 | getAssumed().print(OS); | ||||
8098 | OS << ">"; | ||||
8099 | return OS.str(); | ||||
8100 | } | ||||
8101 | |||||
8102 | /// Helper function to get a SCEV expr for the associated value at program | ||||
8103 | /// point \p I. | ||||
8104 | const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const { | ||||
8105 | if (!getAnchorScope()) | ||||
8106 | return nullptr; | ||||
8107 | |||||
8108 | ScalarEvolution *SE = | ||||
8109 | A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( | ||||
8110 | *getAnchorScope()); | ||||
8111 | |||||
8112 | LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>( | ||||
8113 | *getAnchorScope()); | ||||
8114 | |||||
8115 | if (!SE || !LI) | ||||
8116 | return nullptr; | ||||
8117 | |||||
8118 | const SCEV *S = SE->getSCEV(&getAssociatedValue()); | ||||
8119 | if (!I) | ||||
8120 | return S; | ||||
8121 | |||||
8122 | return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent())); | ||||
8123 | } | ||||
8124 | |||||
8125 | /// Helper function to get a range from SCEV for the associated value at | ||||
8126 | /// program point \p I. | ||||
8127 | ConstantRange getConstantRangeFromSCEV(Attributor &A, | ||||
8128 | const Instruction *I = nullptr) const { | ||||
8129 | if (!getAnchorScope()) | ||||
8130 | return getWorstState(getBitWidth()); | ||||
8131 | |||||
8132 | ScalarEvolution *SE = | ||||
8133 | A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>( | ||||
8134 | *getAnchorScope()); | ||||
8135 | |||||
8136 | const SCEV *S = getSCEV(A, I); | ||||
8137 | if (!SE || !S) | ||||
8138 | return getWorstState(getBitWidth()); | ||||
8139 | |||||
8140 | return SE->getUnsignedRange(S); | ||||
8141 | } | ||||
8142 | |||||
8143 | /// Helper function to get a range from LVI for the associated value at | ||||
8144 | /// program point \p I. | ||||
8145 | ConstantRange | ||||
8146 | getConstantRangeFromLVI(Attributor &A, | ||||
8147 | const Instruction *CtxI = nullptr) const { | ||||
8148 | if (!getAnchorScope()) | ||||
8149 | return getWorstState(getBitWidth()); | ||||
8150 | |||||
8151 | LazyValueInfo *LVI = | ||||
8152 | A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>( | ||||
8153 | *getAnchorScope()); | ||||
8154 | |||||
8155 | if (!LVI || !CtxI) | ||||
8156 | return getWorstState(getBitWidth()); | ||||
8157 | return LVI->getConstantRange(&getAssociatedValue(), | ||||
8158 | const_cast<Instruction *>(CtxI)); | ||||
8159 | } | ||||
8160 | |||||
8161 | /// Return true if \p CtxI is valid for querying outside analyses. | ||||
8162 | /// This basically makes sure we do not ask intra-procedural analysis | ||||
8163 | /// about a context in the wrong function or a context that violates | ||||
8164 | /// dominance assumptions they might have. The \p AllowAACtxI flag indicates | ||||
8165 | /// if the original context of this AA is OK or should be considered invalid. | ||||
8166 | bool isValidCtxInstructionForOutsideAnalysis(Attributor &A, | ||||
8167 | const Instruction *CtxI, | ||||
8168 | bool AllowAACtxI) const { | ||||
8169 | if (!CtxI || (!AllowAACtxI && CtxI == getCtxI())) | ||||
8170 | return false; | ||||
8171 | |||||
8172 | // Our context might be in a different function, neither intra-procedural | ||||
8173 | // analysis (ScalarEvolution nor LazyValueInfo) can handle that. | ||||
8174 | if (!AA::isValidInScope(getAssociatedValue(), CtxI->getFunction())) | ||||
8175 | return false; | ||||
8176 | |||||
8177 | // If the context is not dominated by the value there are paths to the | ||||
8178 | // context that do not define the value. This cannot be handled by | ||||
8179 | // LazyValueInfo so we need to bail. | ||||
8180 | if (auto *I = dyn_cast<Instruction>(&getAssociatedValue())) { | ||||
8181 | InformationCache &InfoCache = A.getInfoCache(); | ||||
8182 | const DominatorTree *DT = | ||||
8183 | InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>( | ||||
8184 | *I->getFunction()); | ||||
8185 | return DT && DT->dominates(I, CtxI); | ||||
8186 | } | ||||
8187 | |||||
8188 | return true; | ||||
8189 | } | ||||
8190 | |||||
8191 | /// See AAValueConstantRange::getKnownConstantRange(..). | ||||
8192 | ConstantRange | ||||
8193 | getKnownConstantRange(Attributor &A, | ||||
8194 | const Instruction *CtxI = nullptr) const override { | ||||
8195 | if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI, | ||||
8196 | /* AllowAACtxI */ false)) | ||||
8197 | return getKnown(); | ||||
8198 | |||||
8199 | ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); | ||||
8200 | ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); | ||||
8201 | return getKnown().intersectWith(SCEVR).intersectWith(LVIR); | ||||
8202 | } | ||||
8203 | |||||
8204 | /// See AAValueConstantRange::getAssumedConstantRange(..). | ||||
8205 | ConstantRange | ||||
8206 | getAssumedConstantRange(Attributor &A, | ||||
8207 | const Instruction *CtxI = nullptr) const override { | ||||
8208 | // TODO: Make SCEV use Attributor assumption. | ||||
8209 | // We may be able to bound a variable range via assumptions in | ||||
8210 | // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to | ||||
8211 | // evolve to x^2 + x, then we can say that y is in [2, 12]. | ||||
8212 | if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI, | ||||
8213 | /* AllowAACtxI */ false)) | ||||
8214 | return getAssumed(); | ||||
8215 | |||||
8216 | ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI); | ||||
8217 | ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI); | ||||
8218 | return getAssumed().intersectWith(SCEVR).intersectWith(LVIR); | ||||
8219 | } | ||||
8220 | |||||
8221 | /// Helper function to create MDNode for range metadata. | ||||
8222 | static MDNode * | ||||
8223 | getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx, | ||||
8224 | const ConstantRange &AssumedConstantRange) { | ||||
8225 | Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get( | ||||
8226 | Ty, AssumedConstantRange.getLower())), | ||||
8227 | ConstantAsMetadata::get(ConstantInt::get( | ||||
8228 | Ty, AssumedConstantRange.getUpper()))}; | ||||
8229 | return MDNode::get(Ctx, LowAndHigh); | ||||
8230 | } | ||||
8231 | |||||
8232 | /// Return true if \p Assumed is included in \p KnownRanges. | ||||
8233 | static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) { | ||||
8234 | |||||
8235 | if (Assumed.isFullSet()) | ||||
8236 | return false; | ||||
8237 | |||||
8238 | if (!KnownRanges) | ||||
8239 | return true; | ||||
8240 | |||||
8241 | // If multiple ranges are annotated in IR, we give up to annotate assumed | ||||
8242 | // range for now. | ||||
8243 | |||||
8244 | // TODO: If there exists a known range which containts assumed range, we | ||||
8245 | // can say assumed range is better. | ||||
8246 | if (KnownRanges->getNumOperands() > 2) | ||||
8247 | return false; | ||||
8248 | |||||
8249 | ConstantInt *Lower = | ||||
8250 | mdconst::extract<ConstantInt>(KnownRanges->getOperand(0)); | ||||
8251 | ConstantInt *Upper = | ||||
8252 | mdconst::extract<ConstantInt>(KnownRanges->getOperand(1)); | ||||
8253 | |||||
8254 | ConstantRange Known(Lower->getValue(), Upper->getValue()); | ||||
8255 | return Known.contains(Assumed) && Known != Assumed; | ||||
8256 | } | ||||
8257 | |||||
8258 | /// Helper function to set range metadata. | ||||
8259 | static bool | ||||
8260 | setRangeMetadataIfisBetterRange(Instruction *I, | ||||
8261 | const ConstantRange &AssumedConstantRange) { | ||||
8262 | auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range); | ||||
8263 | if (isBetterRange(AssumedConstantRange, OldRangeMD)) { | ||||
8264 | if (!AssumedConstantRange.isEmptySet()) { | ||||
8265 | I->setMetadata(LLVMContext::MD_range, | ||||
8266 | getMDNodeForConstantRange(I->getType(), I->getContext(), | ||||
8267 | AssumedConstantRange)); | ||||
8268 | return true; | ||||
8269 | } | ||||
8270 | } | ||||
8271 | return false; | ||||
8272 | } | ||||
8273 | |||||
8274 | /// See AbstractAttribute::manifest() | ||||
8275 | ChangeStatus manifest(Attributor &A) override { | ||||
8276 | ChangeStatus Changed = ChangeStatus::UNCHANGED; | ||||
8277 | ConstantRange AssumedConstantRange = getAssumedConstantRange(A); | ||||
8278 | assert(!AssumedConstantRange.isFullSet() && "Invalid state")(static_cast <bool> (!AssumedConstantRange.isFullSet() && "Invalid state") ? void (0) : __assert_fail ("!AssumedConstantRange.isFullSet() && \"Invalid state\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 8278, __extension__ __PRETTY_FUNCTION__)); | ||||
8279 | |||||
8280 | auto &V = getAssociatedValue(); | ||||
8281 | if (!AssumedConstantRange.isEmptySet() && | ||||
8282 | !AssumedConstantRange.isSingleElement()) { | ||||
8283 | if (Instruction *I = dyn_cast<Instruction>(&V)) { | ||||
8284 | assert(I == getCtxI() && "Should not annotate an instruction which is "(static_cast <bool> (I == getCtxI() && "Should not annotate an instruction which is " "not the context instruction") ? void (0) : __assert_fail ("I == getCtxI() && \"Should not annotate an instruction which is \" \"not the context instruction\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 8285, __extension__ __PRETTY_FUNCTION__)) | ||||
8285 | "not the context instruction")(static_cast <bool> (I == getCtxI() && "Should not annotate an instruction which is " "not the context instruction") ? void (0) : __assert_fail ("I == getCtxI() && \"Should not annotate an instruction which is \" \"not the context instruction\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 8285, __extension__ __PRETTY_FUNCTION__)); | ||||
8286 | if (isa<CallInst>(I) || isa<LoadInst>(I)) | ||||
8287 | if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange)) | ||||
8288 | Changed = ChangeStatus::CHANGED; | ||||
8289 | } | ||||
8290 | } | ||||
8291 | |||||
8292 | return Changed; | ||||
8293 | } | ||||
8294 | }; | ||||
8295 | |||||
8296 | struct AAValueConstantRangeArgument final | ||||
8297 | : AAArgumentFromCallSiteArguments< | ||||
8298 | AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState, | ||||
8299 | true /* BridgeCallBaseContext */> { | ||||
8300 | using Base = AAArgumentFromCallSiteArguments< | ||||
8301 | AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState, | ||||
8302 | true /* BridgeCallBaseContext */>; | ||||
8303 | AAValueConstantRangeArgument(const IRPosition &IRP, Attributor &A) | ||||
8304 | : Base(IRP, A) {} | ||||
8305 | |||||
8306 | /// See AbstractAttribute::initialize(..). | ||||
8307 | void initialize(Attributor &A) override { | ||||
8308 | if (!getAnchorScope() || getAnchorScope()->isDeclaration()) { | ||||
8309 | indicatePessimisticFixpoint(); | ||||
8310 | } else { | ||||
8311 | Base::initialize(A); | ||||
8312 | } | ||||
8313 | } | ||||
8314 | |||||
8315 | /// See AbstractAttribute::trackStatistics() | ||||
8316 | void trackStatistics() const override { | ||||
8317 | STATS_DECLTRACK_ARG_ATTR(value_range){ static llvm::Statistic NumIRArguments_value_range = {"attributor" , "NumIRArguments_value_range", ("Number of " "arguments" " marked '" "value_range" "'")};; ++(NumIRArguments_value_range); } | ||||
8318 | } | ||||
8319 | }; | ||||
8320 | |||||
8321 | struct AAValueConstantRangeReturned | ||||
8322 | : AAReturnedFromReturnedValues<AAValueConstantRange, | ||||
8323 | AAValueConstantRangeImpl, | ||||
8324 | AAValueConstantRangeImpl::StateType, | ||||
8325 | /* PropogateCallBaseContext */ true> { | ||||
8326 | using Base = | ||||
8327 | AAReturnedFromReturnedValues<AAValueConstantRange, | ||||
8328 | AAValueConstantRangeImpl, | ||||
8329 | AAValueConstantRangeImpl::StateType, | ||||
8330 | /* PropogateCallBaseContext */ true>; | ||||
8331 | AAValueConstantRangeReturned(const IRPosition &IRP, Attributor &A) | ||||
8332 | : Base(IRP, A) {} | ||||
8333 | |||||
8334 | /// See AbstractAttribute::initialize(...). | ||||
8335 | void initialize(Attributor &A) override {} | ||||
8336 | |||||
8337 | /// See AbstractAttribute::trackStatistics() | ||||
8338 | void trackStatistics() const override { | ||||
8339 | STATS_DECLTRACK_FNRET_ATTR(value_range){ static llvm::Statistic NumIRFunctionReturn_value_range = {"attributor" , "NumIRFunctionReturn_value_range", ("Number of " "function returns" " marked '" "value_range" "'")};; ++(NumIRFunctionReturn_value_range ); } | ||||
8340 | } | ||||
8341 | }; | ||||
8342 | |||||
8343 | struct AAValueConstantRangeFloating : AAValueConstantRangeImpl { | ||||
8344 | AAValueConstantRangeFloating(const IRPosition &IRP, Attributor &A) | ||||
8345 | : AAValueConstantRangeImpl(IRP, A) {} | ||||
8346 | |||||
8347 | /// See AbstractAttribute::initialize(...). | ||||
8348 | void initialize(Attributor &A) override { | ||||
8349 | AAValueConstantRangeImpl::initialize(A); | ||||
8350 | if (isAtFixpoint()) | ||||
8351 | return; | ||||
8352 | |||||
8353 | Value &V = getAssociatedValue(); | ||||
8354 | |||||
8355 | if (auto *C = dyn_cast<ConstantInt>(&V)) { | ||||
8356 | unionAssumed(ConstantRange(C->getValue())); | ||||
8357 | indicateOptimisticFixpoint(); | ||||
8358 | return; | ||||
8359 | } | ||||
8360 | |||||
8361 | if (isa<UndefValue>(&V)) { | ||||
8362 | // Collapse the undef state to 0. | ||||
8363 | unionAssumed(ConstantRange(APInt(getBitWidth(), 0))); | ||||
8364 | indicateOptimisticFixpoint(); | ||||
8365 | return; | ||||
8366 | } | ||||
8367 | |||||
8368 | if (isa<CallBase>(&V)) | ||||
8369 | return; | ||||
8370 | |||||
8371 | if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V)) | ||||
8372 | return; | ||||
8373 | |||||
8374 | // If it is a load instruction with range metadata, use it. | ||||
8375 | if (LoadInst *LI = dyn_cast<LoadInst>(&V)) | ||||
8376 | if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) { | ||||
8377 | intersectKnown(getConstantRangeFromMetadata(*RangeMD)); | ||||
8378 | return; | ||||
8379 | } | ||||
8380 | |||||
8381 | // We can work with PHI and select instruction as we traverse their operands | ||||
8382 | // during update. | ||||
8383 | if (isa<SelectInst>(V) || isa<PHINode>(V)) | ||||
8384 | return; | ||||
8385 | |||||
8386 | // Otherwise we give up. | ||||
8387 | indicatePessimisticFixpoint(); | ||||
8388 | |||||
8389 | LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAValueConstantRange] We give up: " << getAssociatedValue() << "\n"; } } while (false ) | ||||
8390 | << getAssociatedValue() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAValueConstantRange] We give up: " << getAssociatedValue() << "\n"; } } while (false ); | ||||
8391 | } | ||||
8392 | |||||
8393 | bool calculateBinaryOperator( | ||||
8394 | Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T, | ||||
8395 | const Instruction *CtxI, | ||||
8396 | SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { | ||||
8397 | Value *LHS = BinOp->getOperand(0); | ||||
8398 | Value *RHS = BinOp->getOperand(1); | ||||
8399 | |||||
8400 | // Simplify the operands first. | ||||
8401 | bool UsedAssumedInformation = false; | ||||
8402 | const auto &SimplifiedLHS = | ||||
8403 | A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), | ||||
8404 | *this, UsedAssumedInformation); | ||||
8405 | if (!SimplifiedLHS.hasValue()) | ||||
8406 | return true; | ||||
8407 | if (!SimplifiedLHS.getValue()) | ||||
8408 | return false; | ||||
8409 | LHS = *SimplifiedLHS; | ||||
8410 | |||||
8411 | const auto &SimplifiedRHS = | ||||
8412 | A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), | ||||
8413 | *this, UsedAssumedInformation); | ||||
8414 | if (!SimplifiedRHS.hasValue()) | ||||
8415 | return true; | ||||
8416 | if (!SimplifiedRHS.getValue()) | ||||
8417 | return false; | ||||
8418 | RHS = *SimplifiedRHS; | ||||
8419 | |||||
8420 | // TODO: Allow non integers as well. | ||||
8421 | if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) | ||||
8422 | return false; | ||||
8423 | |||||
8424 | auto &LHSAA = A.getAAFor<AAValueConstantRange>( | ||||
8425 | *this, IRPosition::value(*LHS, getCallBaseContext()), | ||||
8426 | DepClassTy::REQUIRED); | ||||
8427 | QuerriedAAs.push_back(&LHSAA); | ||||
8428 | auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); | ||||
8429 | |||||
8430 | auto &RHSAA = A.getAAFor<AAValueConstantRange>( | ||||
8431 | *this, IRPosition::value(*RHS, getCallBaseContext()), | ||||
8432 | DepClassTy::REQUIRED); | ||||
8433 | QuerriedAAs.push_back(&RHSAA); | ||||
8434 | auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); | ||||
8435 | |||||
8436 | auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange); | ||||
8437 | |||||
8438 | T.unionAssumed(AssumedRange); | ||||
8439 | |||||
8440 | // TODO: Track a known state too. | ||||
8441 | |||||
8442 | return T.isValidState(); | ||||
8443 | } | ||||
8444 | |||||
8445 | bool calculateCastInst( | ||||
8446 | Attributor &A, CastInst *CastI, IntegerRangeState &T, | ||||
8447 | const Instruction *CtxI, | ||||
8448 | SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { | ||||
8449 | assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!")(static_cast <bool> (CastI->getNumOperands() == 1 && "Expected cast to be unary!") ? void (0) : __assert_fail ("CastI->getNumOperands() == 1 && \"Expected cast to be unary!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 8449, __extension__ __PRETTY_FUNCTION__)); | ||||
8450 | // TODO: Allow non integers as well. | ||||
8451 | Value *OpV = CastI->getOperand(0); | ||||
8452 | |||||
8453 | // Simplify the operand first. | ||||
8454 | bool UsedAssumedInformation = false; | ||||
8455 | const auto &SimplifiedOpV = | ||||
8456 | A.getAssumedSimplified(IRPosition::value(*OpV, getCallBaseContext()), | ||||
8457 | *this, UsedAssumedInformation); | ||||
8458 | if (!SimplifiedOpV.hasValue()) | ||||
8459 | return true; | ||||
8460 | if (!SimplifiedOpV.getValue()) | ||||
8461 | return false; | ||||
8462 | OpV = *SimplifiedOpV; | ||||
8463 | |||||
8464 | if (!OpV->getType()->isIntegerTy()) | ||||
8465 | return false; | ||||
8466 | |||||
8467 | auto &OpAA = A.getAAFor<AAValueConstantRange>( | ||||
8468 | *this, IRPosition::value(*OpV, getCallBaseContext()), | ||||
8469 | DepClassTy::REQUIRED); | ||||
8470 | QuerriedAAs.push_back(&OpAA); | ||||
8471 | T.unionAssumed( | ||||
8472 | OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth())); | ||||
8473 | return T.isValidState(); | ||||
8474 | } | ||||
8475 | |||||
8476 | bool | ||||
8477 | calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T, | ||||
8478 | const Instruction *CtxI, | ||||
8479 | SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) { | ||||
8480 | Value *LHS = CmpI->getOperand(0); | ||||
8481 | Value *RHS = CmpI->getOperand(1); | ||||
8482 | |||||
8483 | // Simplify the operands first. | ||||
8484 | bool UsedAssumedInformation = false; | ||||
8485 | const auto &SimplifiedLHS = | ||||
8486 | A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), | ||||
8487 | *this, UsedAssumedInformation); | ||||
8488 | if (!SimplifiedLHS.hasValue()) | ||||
8489 | return true; | ||||
8490 | if (!SimplifiedLHS.getValue()) | ||||
8491 | return false; | ||||
8492 | LHS = *SimplifiedLHS; | ||||
8493 | |||||
8494 | const auto &SimplifiedRHS = | ||||
8495 | A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), | ||||
8496 | *this, UsedAssumedInformation); | ||||
8497 | if (!SimplifiedRHS.hasValue()) | ||||
8498 | return true; | ||||
8499 | if (!SimplifiedRHS.getValue()) | ||||
8500 | return false; | ||||
8501 | RHS = *SimplifiedRHS; | ||||
8502 | |||||
8503 | // TODO: Allow non integers as well. | ||||
8504 | if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) | ||||
8505 | return false; | ||||
8506 | |||||
8507 | auto &LHSAA = A.getAAFor<AAValueConstantRange>( | ||||
8508 | *this, IRPosition::value(*LHS, getCallBaseContext()), | ||||
8509 | DepClassTy::REQUIRED); | ||||
8510 | QuerriedAAs.push_back(&LHSAA); | ||||
8511 | auto &RHSAA = A.getAAFor<AAValueConstantRange>( | ||||
8512 | *this, IRPosition::value(*RHS, getCallBaseContext()), | ||||
8513 | DepClassTy::REQUIRED); | ||||
8514 | QuerriedAAs.push_back(&RHSAA); | ||||
8515 | auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI); | ||||
8516 | auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI); | ||||
8517 | |||||
8518 | // If one of them is empty set, we can't decide. | ||||
8519 | if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet()) | ||||
8520 | return true; | ||||
8521 | |||||
8522 | bool MustTrue = false, MustFalse = false; | ||||
8523 | |||||
8524 | auto AllowedRegion = | ||||
8525 | ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange); | ||||
8526 | |||||
8527 | if (AllowedRegion.intersectWith(LHSAARange).isEmptySet()) | ||||
8528 | MustFalse = true; | ||||
8529 | |||||
8530 | if (LHSAARange.icmp(CmpI->getPredicate(), RHSAARange)) | ||||
8531 | MustTrue = true; | ||||
8532 | |||||
8533 | assert((!MustTrue || !MustFalse) &&(static_cast <bool> ((!MustTrue || !MustFalse) && "Either MustTrue or MustFalse should be false!") ? void (0) : __assert_fail ("(!MustTrue || !MustFalse) && \"Either MustTrue or MustFalse should be false!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 8534, __extension__ __PRETTY_FUNCTION__)) | ||||
8534 | "Either MustTrue or MustFalse should be false!")(static_cast <bool> ((!MustTrue || !MustFalse) && "Either MustTrue or MustFalse should be false!") ? void (0) : __assert_fail ("(!MustTrue || !MustFalse) && \"Either MustTrue or MustFalse should be false!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 8534, __extension__ __PRETTY_FUNCTION__)); | ||||
8535 | |||||
8536 | if (MustTrue) | ||||
8537 | T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1))); | ||||
8538 | else if (MustFalse) | ||||
8539 | T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0))); | ||||
8540 | else | ||||
8541 | T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true)); | ||||
8542 | |||||
8543 | LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAAdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA << " " << RHSAA << "\n"; } } while (false) | ||||
8544 | << " " << RHSAA << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA << " " << RHSAA << "\n"; } } while (false); | ||||
8545 | |||||
8546 | // TODO: Track a known state too. | ||||
8547 | return T.isValidState(); | ||||
8548 | } | ||||
8549 | |||||
8550 | /// See AbstractAttribute::updateImpl(...). | ||||
8551 | ChangeStatus updateImpl(Attributor &A) override { | ||||
8552 | auto VisitValueCB = [&](Value &V, const Instruction *CtxI, | ||||
8553 | IntegerRangeState &T, bool Stripped) -> bool { | ||||
8554 | Instruction *I = dyn_cast<Instruction>(&V); | ||||
8555 | if (!I || isa<CallBase>(I)) { | ||||
8556 | |||||
8557 | // Simplify the operand first. | ||||
8558 | bool UsedAssumedInformation = false; | ||||
8559 | const auto &SimplifiedOpV = | ||||
8560 | A.getAssumedSimplified(IRPosition::value(V, getCallBaseContext()), | ||||
8561 | *this, UsedAssumedInformation); | ||||
8562 | if (!SimplifiedOpV.hasValue()) | ||||
8563 | return true; | ||||
8564 | if (!SimplifiedOpV.getValue()) | ||||
8565 | return false; | ||||
8566 | Value *VPtr = *SimplifiedOpV; | ||||
8567 | |||||
8568 | // If the value is not instruction, we query AA to Attributor. | ||||
8569 | const auto &AA = A.getAAFor<AAValueConstantRange>( | ||||
8570 | *this, IRPosition::value(*VPtr, getCallBaseContext()), | ||||
8571 | DepClassTy::REQUIRED); | ||||
8572 | |||||
8573 | // Clamp operator is not used to utilize a program point CtxI. | ||||
8574 | T.unionAssumed(AA.getAssumedConstantRange(A, CtxI)); | ||||
8575 | |||||
8576 | return T.isValidState(); | ||||
8577 | } | ||||
8578 | |||||
8579 | SmallVector<const AAValueConstantRange *, 4> QuerriedAAs; | ||||
8580 | if (auto *BinOp = dyn_cast<BinaryOperator>(I)) { | ||||
8581 | if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs)) | ||||
8582 | return false; | ||||
8583 | } else if (auto *CmpI = dyn_cast<CmpInst>(I)) { | ||||
8584 | if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs)) | ||||
8585 | return false; | ||||
8586 | } else if (auto *CastI = dyn_cast<CastInst>(I)) { | ||||
8587 | if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs)) | ||||
8588 | return false; | ||||
8589 | } else { | ||||
8590 | // Give up with other instructions. | ||||
8591 | // TODO: Add other instructions | ||||
8592 | |||||
8593 | T.indicatePessimisticFixpoint(); | ||||
8594 | return false; | ||||
8595 | } | ||||
8596 | |||||
8597 | // Catch circular reasoning in a pessimistic way for now. | ||||
8598 | // TODO: Check how the range evolves and if we stripped anything, see also | ||||
8599 | // AADereferenceable or AAAlign for similar situations. | ||||
8600 | for (const AAValueConstantRange *QueriedAA : QuerriedAAs) { | ||||
8601 | if (QueriedAA != this) | ||||
8602 | continue; | ||||
8603 | // If we are in a stady state we do not need to worry. | ||||
8604 | if (T.getAssumed() == getState().getAssumed()) | ||||
8605 | continue; | ||||
8606 | T.indicatePessimisticFixpoint(); | ||||
8607 | } | ||||
8608 | |||||
8609 | return T.isValidState(); | ||||
8610 | }; | ||||
8611 | |||||
8612 | IntegerRangeState T(getBitWidth()); | ||||
8613 | |||||
8614 | bool UsedAssumedInformation = false; | ||||
8615 | if (!genericValueTraversal<IntegerRangeState>(A, getIRPosition(), *this, T, | ||||
8616 | VisitValueCB, getCtxI(), | ||||
8617 | UsedAssumedInformation, | ||||
8618 | /* UseValueSimplify */ false)) | ||||
8619 | return indicatePessimisticFixpoint(); | ||||
8620 | |||||
8621 | // Ensure that long def-use chains can't cause circular reasoning either by | ||||
8622 | // introducing a cutoff below. | ||||
8623 | if (clampStateAndIndicateChange(getState(), T) == ChangeStatus::UNCHANGED) | ||||
8624 | return ChangeStatus::UNCHANGED; | ||||
8625 | if (++NumChanges > MaxNumChanges) { | ||||
8626 | LLVM_DEBUG(dbgs() << "[AAValueConstantRange] performed " << NumChangesdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAValueConstantRange] performed " << NumChanges << " but only " << MaxNumChanges << " are allowed to avoid cyclic reasoning."; } } while (false) | ||||
8627 | << " but only " << MaxNumChangesdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAValueConstantRange] performed " << NumChanges << " but only " << MaxNumChanges << " are allowed to avoid cyclic reasoning."; } } while (false) | ||||
8628 | << " are allowed to avoid cyclic reasoning.")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAValueConstantRange] performed " << NumChanges << " but only " << MaxNumChanges << " are allowed to avoid cyclic reasoning."; } } while (false); | ||||
8629 | return indicatePessimisticFixpoint(); | ||||
8630 | } | ||||
8631 | return ChangeStatus::CHANGED; | ||||
8632 | } | ||||
8633 | |||||
8634 | /// See AbstractAttribute::trackStatistics() | ||||
8635 | void trackStatistics() const override { | ||||
8636 | STATS_DECLTRACK_FLOATING_ATTR(value_range){ static llvm::Statistic NumIRFloating_value_range = {"attributor" , "NumIRFloating_value_range", ("Number of floating values known to be '" "value_range" "'")};; ++(NumIRFloating_value_range); } | ||||
8637 | } | ||||
8638 | |||||
8639 | /// Tracker to bail after too many widening steps of the constant range. | ||||
8640 | int NumChanges = 0; | ||||
8641 | |||||
8642 | /// Upper bound for the number of allowed changes (=widening steps) for the | ||||
8643 | /// constant range before we give up. | ||||
8644 | static constexpr int MaxNumChanges = 5; | ||||
8645 | }; | ||||
8646 | |||||
8647 | struct AAValueConstantRangeFunction : AAValueConstantRangeImpl { | ||||
8648 | AAValueConstantRangeFunction(const IRPosition &IRP, Attributor &A) | ||||
8649 | : AAValueConstantRangeImpl(IRP, A) {} | ||||
8650 | |||||
8651 | /// See AbstractAttribute::initialize(...). | ||||
8652 | ChangeStatus updateImpl(Attributor &A) override { | ||||
8653 | llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "::llvm::llvm_unreachable_internal("AAValueConstantRange(Function|CallSite)::updateImpl will " "not be called", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 8654) | ||||
8654 | "not be called")::llvm::llvm_unreachable_internal("AAValueConstantRange(Function|CallSite)::updateImpl will " "not be called", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 8654); | ||||
8655 | } | ||||
8656 | |||||
8657 | /// See AbstractAttribute::trackStatistics() | ||||
8658 | void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range){ static llvm::Statistic NumIRFunction_value_range = {"attributor" , "NumIRFunction_value_range", ("Number of " "functions" " marked '" "value_range" "'")};; ++(NumIRFunction_value_range); } } | ||||
8659 | }; | ||||
8660 | |||||
8661 | struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction { | ||||
8662 | AAValueConstantRangeCallSite(const IRPosition &IRP, Attributor &A) | ||||
8663 | : AAValueConstantRangeFunction(IRP, A) {} | ||||
8664 | |||||
8665 | /// See AbstractAttribute::trackStatistics() | ||||
8666 | void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range){ static llvm::Statistic NumIRCS_value_range = {"attributor", "NumIRCS_value_range", ("Number of " "call site" " marked '" "value_range" "'")};; ++(NumIRCS_value_range); } } | ||||
8667 | }; | ||||
8668 | |||||
8669 | struct AAValueConstantRangeCallSiteReturned | ||||
8670 | : AACallSiteReturnedFromReturned<AAValueConstantRange, | ||||
8671 | AAValueConstantRangeImpl, | ||||
8672 | AAValueConstantRangeImpl::StateType, | ||||
8673 | /* IntroduceCallBaseContext */ true> { | ||||
8674 | AAValueConstantRangeCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
8675 | : AACallSiteReturnedFromReturned<AAValueConstantRange, | ||||
8676 | AAValueConstantRangeImpl, | ||||
8677 | AAValueConstantRangeImpl::StateType, | ||||
8678 | /* IntroduceCallBaseContext */ true>(IRP, | ||||
8679 | A) { | ||||
8680 | } | ||||
8681 | |||||
8682 | /// See AbstractAttribute::initialize(...). | ||||
8683 | void initialize(Attributor &A) override { | ||||
8684 | // If it is a load instruction with range metadata, use the metadata. | ||||
8685 | if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue())) | ||||
8686 | if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range)) | ||||
8687 | intersectKnown(getConstantRangeFromMetadata(*RangeMD)); | ||||
8688 | |||||
8689 | AAValueConstantRangeImpl::initialize(A); | ||||
8690 | } | ||||
8691 | |||||
8692 | /// See AbstractAttribute::trackStatistics() | ||||
8693 | void trackStatistics() const override { | ||||
8694 | STATS_DECLTRACK_CSRET_ATTR(value_range){ static llvm::Statistic NumIRCSReturn_value_range = {"attributor" , "NumIRCSReturn_value_range", ("Number of " "call site returns" " marked '" "value_range" "'")};; ++(NumIRCSReturn_value_range ); } | ||||
8695 | } | ||||
8696 | }; | ||||
8697 | struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating { | ||||
8698 | AAValueConstantRangeCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
8699 | : AAValueConstantRangeFloating(IRP, A) {} | ||||
8700 | |||||
8701 | /// See AbstractAttribute::manifest() | ||||
8702 | ChangeStatus manifest(Attributor &A) override { | ||||
8703 | return ChangeStatus::UNCHANGED; | ||||
8704 | } | ||||
8705 | |||||
8706 | /// See AbstractAttribute::trackStatistics() | ||||
8707 | void trackStatistics() const override { | ||||
8708 | STATS_DECLTRACK_CSARG_ATTR(value_range){ static llvm::Statistic NumIRCSArguments_value_range = {"attributor" , "NumIRCSArguments_value_range", ("Number of " "call site arguments" " marked '" "value_range" "'")};; ++(NumIRCSArguments_value_range ); } | ||||
8709 | } | ||||
8710 | }; | ||||
8711 | |||||
8712 | /// ------------------ Potential Values Attribute ------------------------- | ||||
8713 | |||||
8714 | struct AAPotentialValuesImpl : AAPotentialValues { | ||||
8715 | using StateType = PotentialConstantIntValuesState; | ||||
8716 | |||||
8717 | AAPotentialValuesImpl(const IRPosition &IRP, Attributor &A) | ||||
8718 | : AAPotentialValues(IRP, A) {} | ||||
8719 | |||||
8720 | /// See AbstractAttribute::initialize(..). | ||||
8721 | void initialize(Attributor &A) override { | ||||
8722 | if (A.hasSimplificationCallback(getIRPosition())) | ||||
8723 | indicatePessimisticFixpoint(); | ||||
8724 | else | ||||
8725 | AAPotentialValues::initialize(A); | ||||
8726 | } | ||||
8727 | |||||
8728 | /// See AbstractAttribute::getAsStr(). | ||||
8729 | const std::string getAsStr() const override { | ||||
8730 | std::string Str; | ||||
8731 | llvm::raw_string_ostream OS(Str); | ||||
8732 | OS << getState(); | ||||
8733 | return OS.str(); | ||||
8734 | } | ||||
8735 | |||||
8736 | /// See AbstractAttribute::updateImpl(...). | ||||
8737 | ChangeStatus updateImpl(Attributor &A) override { | ||||
8738 | return indicatePessimisticFixpoint(); | ||||
8739 | } | ||||
8740 | }; | ||||
8741 | |||||
8742 | struct AAPotentialValuesArgument final | ||||
8743 | : AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl, | ||||
8744 | PotentialConstantIntValuesState> { | ||||
8745 | using Base = | ||||
8746 | AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl, | ||||
8747 | PotentialConstantIntValuesState>; | ||||
8748 | AAPotentialValuesArgument(const IRPosition &IRP, Attributor &A) | ||||
8749 | : Base(IRP, A) {} | ||||
8750 | |||||
8751 | /// See AbstractAttribute::initialize(..). | ||||
8752 | void initialize(Attributor &A) override { | ||||
8753 | if (!getAnchorScope() || getAnchorScope()->isDeclaration()) { | ||||
8754 | indicatePessimisticFixpoint(); | ||||
8755 | } else { | ||||
8756 | Base::initialize(A); | ||||
8757 | } | ||||
8758 | } | ||||
8759 | |||||
8760 | /// See AbstractAttribute::trackStatistics() | ||||
8761 | void trackStatistics() const override { | ||||
8762 | STATS_DECLTRACK_ARG_ATTR(potential_values){ static llvm::Statistic NumIRArguments_potential_values = {"attributor" , "NumIRArguments_potential_values", ("Number of " "arguments" " marked '" "potential_values" "'")};; ++(NumIRArguments_potential_values ); } | ||||
8763 | } | ||||
8764 | }; | ||||
8765 | |||||
8766 | struct AAPotentialValuesReturned | ||||
8767 | : AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl> { | ||||
8768 | using Base = | ||||
8769 | AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl>; | ||||
8770 | AAPotentialValuesReturned(const IRPosition &IRP, Attributor &A) | ||||
8771 | : Base(IRP, A) {} | ||||
8772 | |||||
8773 | /// See AbstractAttribute::trackStatistics() | ||||
8774 | void trackStatistics() const override { | ||||
8775 | STATS_DECLTRACK_FNRET_ATTR(potential_values){ static llvm::Statistic NumIRFunctionReturn_potential_values = {"attributor", "NumIRFunctionReturn_potential_values", ("Number of " "function returns" " marked '" "potential_values" "'")};; ++ (NumIRFunctionReturn_potential_values); } | ||||
8776 | } | ||||
8777 | }; | ||||
8778 | |||||
8779 | struct AAPotentialValuesFloating : AAPotentialValuesImpl { | ||||
8780 | AAPotentialValuesFloating(const IRPosition &IRP, Attributor &A) | ||||
8781 | : AAPotentialValuesImpl(IRP, A) {} | ||||
8782 | |||||
8783 | /// See AbstractAttribute::initialize(..). | ||||
8784 | void initialize(Attributor &A) override { | ||||
8785 | AAPotentialValuesImpl::initialize(A); | ||||
8786 | if (isAtFixpoint()) | ||||
8787 | return; | ||||
8788 | |||||
8789 | Value &V = getAssociatedValue(); | ||||
8790 | |||||
8791 | if (auto *C = dyn_cast<ConstantInt>(&V)) { | ||||
8792 | unionAssumed(C->getValue()); | ||||
8793 | indicateOptimisticFixpoint(); | ||||
8794 | return; | ||||
8795 | } | ||||
8796 | |||||
8797 | if (isa<UndefValue>(&V)) { | ||||
8798 | unionAssumedWithUndef(); | ||||
8799 | indicateOptimisticFixpoint(); | ||||
8800 | return; | ||||
8801 | } | ||||
8802 | |||||
8803 | if (isa<BinaryOperator>(&V) || isa<ICmpInst>(&V) || isa<CastInst>(&V)) | ||||
8804 | return; | ||||
8805 | |||||
8806 | if (isa<SelectInst>(V) || isa<PHINode>(V) || isa<LoadInst>(V)) | ||||
8807 | return; | ||||
8808 | |||||
8809 | indicatePessimisticFixpoint(); | ||||
8810 | |||||
8811 | LLVM_DEBUG(dbgs() << "[AAPotentialValues] We give up: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPotentialValues] We give up: " << getAssociatedValue() << "\n"; } } while (false ) | ||||
8812 | << getAssociatedValue() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AAPotentialValues] We give up: " << getAssociatedValue() << "\n"; } } while (false ); | ||||
8813 | } | ||||
8814 | |||||
8815 | static bool calculateICmpInst(const ICmpInst *ICI, const APInt &LHS, | ||||
8816 | const APInt &RHS) { | ||||
8817 | return ICmpInst::compare(LHS, RHS, ICI->getPredicate()); | ||||
8818 | } | ||||
8819 | |||||
8820 | static APInt calculateCastInst(const CastInst *CI, const APInt &Src, | ||||
8821 | uint32_t ResultBitWidth) { | ||||
8822 | Instruction::CastOps CastOp = CI->getOpcode(); | ||||
8823 | switch (CastOp) { | ||||
8824 | default: | ||||
8825 | llvm_unreachable("unsupported or not integer cast")::llvm::llvm_unreachable_internal("unsupported or not integer cast" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 8825); | ||||
8826 | case Instruction::Trunc: | ||||
8827 | return Src.trunc(ResultBitWidth); | ||||
8828 | case Instruction::SExt: | ||||
8829 | return Src.sext(ResultBitWidth); | ||||
8830 | case Instruction::ZExt: | ||||
8831 | return Src.zext(ResultBitWidth); | ||||
8832 | case Instruction::BitCast: | ||||
8833 | return Src; | ||||
8834 | } | ||||
8835 | } | ||||
8836 | |||||
8837 | static APInt calculateBinaryOperator(const BinaryOperator *BinOp, | ||||
8838 | const APInt &LHS, const APInt &RHS, | ||||
8839 | bool &SkipOperation, bool &Unsupported) { | ||||
8840 | Instruction::BinaryOps BinOpcode = BinOp->getOpcode(); | ||||
8841 | // Unsupported is set to true when the binary operator is not supported. | ||||
8842 | // SkipOperation is set to true when UB occur with the given operand pair | ||||
8843 | // (LHS, RHS). | ||||
8844 | // TODO: we should look at nsw and nuw keywords to handle operations | ||||
8845 | // that create poison or undef value. | ||||
8846 | switch (BinOpcode) { | ||||
8847 | default: | ||||
8848 | Unsupported = true; | ||||
8849 | return LHS; | ||||
8850 | case Instruction::Add: | ||||
8851 | return LHS + RHS; | ||||
8852 | case Instruction::Sub: | ||||
8853 | return LHS - RHS; | ||||
8854 | case Instruction::Mul: | ||||
8855 | return LHS * RHS; | ||||
8856 | case Instruction::UDiv: | ||||
8857 | if (RHS.isZero()) { | ||||
8858 | SkipOperation = true; | ||||
8859 | return LHS; | ||||
8860 | } | ||||
8861 | return LHS.udiv(RHS); | ||||
8862 | case Instruction::SDiv: | ||||
8863 | if (RHS.isZero()) { | ||||
8864 | SkipOperation = true; | ||||
8865 | return LHS; | ||||
8866 | } | ||||
8867 | return LHS.sdiv(RHS); | ||||
8868 | case Instruction::URem: | ||||
8869 | if (RHS.isZero()) { | ||||
8870 | SkipOperation = true; | ||||
8871 | return LHS; | ||||
8872 | } | ||||
8873 | return LHS.urem(RHS); | ||||
8874 | case Instruction::SRem: | ||||
8875 | if (RHS.isZero()) { | ||||
8876 | SkipOperation = true; | ||||
8877 | return LHS; | ||||
8878 | } | ||||
8879 | return LHS.srem(RHS); | ||||
8880 | case Instruction::Shl: | ||||
8881 | return LHS.shl(RHS); | ||||
8882 | case Instruction::LShr: | ||||
8883 | return LHS.lshr(RHS); | ||||
8884 | case Instruction::AShr: | ||||
8885 | return LHS.ashr(RHS); | ||||
8886 | case Instruction::And: | ||||
8887 | return LHS & RHS; | ||||
8888 | case Instruction::Or: | ||||
8889 | return LHS | RHS; | ||||
8890 | case Instruction::Xor: | ||||
8891 | return LHS ^ RHS; | ||||
8892 | } | ||||
8893 | } | ||||
8894 | |||||
8895 | bool calculateBinaryOperatorAndTakeUnion(const BinaryOperator *BinOp, | ||||
8896 | const APInt &LHS, const APInt &RHS) { | ||||
8897 | bool SkipOperation = false; | ||||
8898 | bool Unsupported = false; | ||||
8899 | APInt Result = | ||||
8900 | calculateBinaryOperator(BinOp, LHS, RHS, SkipOperation, Unsupported); | ||||
8901 | if (Unsupported) | ||||
8902 | return false; | ||||
8903 | // If SkipOperation is true, we can ignore this operand pair (L, R). | ||||
8904 | if (!SkipOperation) | ||||
8905 | unionAssumed(Result); | ||||
8906 | return isValidState(); | ||||
8907 | } | ||||
8908 | |||||
8909 | ChangeStatus updateWithICmpInst(Attributor &A, ICmpInst *ICI) { | ||||
8910 | auto AssumedBefore = getAssumed(); | ||||
8911 | Value *LHS = ICI->getOperand(0); | ||||
8912 | Value *RHS = ICI->getOperand(1); | ||||
8913 | |||||
8914 | // Simplify the operands first. | ||||
8915 | bool UsedAssumedInformation = false; | ||||
8916 | const auto &SimplifiedLHS = | ||||
8917 | A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), | ||||
8918 | *this, UsedAssumedInformation); | ||||
8919 | if (!SimplifiedLHS.hasValue()) | ||||
8920 | return ChangeStatus::UNCHANGED; | ||||
8921 | if (!SimplifiedLHS.getValue()) | ||||
8922 | return indicatePessimisticFixpoint(); | ||||
8923 | LHS = *SimplifiedLHS; | ||||
8924 | |||||
8925 | const auto &SimplifiedRHS = | ||||
8926 | A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), | ||||
8927 | *this, UsedAssumedInformation); | ||||
8928 | if (!SimplifiedRHS.hasValue()) | ||||
8929 | return ChangeStatus::UNCHANGED; | ||||
8930 | if (!SimplifiedRHS.getValue()) | ||||
8931 | return indicatePessimisticFixpoint(); | ||||
8932 | RHS = *SimplifiedRHS; | ||||
8933 | |||||
8934 | if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) | ||||
8935 | return indicatePessimisticFixpoint(); | ||||
8936 | |||||
8937 | auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS), | ||||
8938 | DepClassTy::REQUIRED); | ||||
8939 | if (!LHSAA.isValidState()) | ||||
8940 | return indicatePessimisticFixpoint(); | ||||
8941 | |||||
8942 | auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS), | ||||
8943 | DepClassTy::REQUIRED); | ||||
8944 | if (!RHSAA.isValidState()) | ||||
8945 | return indicatePessimisticFixpoint(); | ||||
8946 | |||||
8947 | const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet(); | ||||
8948 | const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet(); | ||||
8949 | |||||
8950 | // TODO: make use of undef flag to limit potential values aggressively. | ||||
8951 | bool MaybeTrue = false, MaybeFalse = false; | ||||
8952 | const APInt Zero(RHS->getType()->getIntegerBitWidth(), 0); | ||||
8953 | if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) { | ||||
8954 | // The result of any comparison between undefs can be soundly replaced | ||||
8955 | // with undef. | ||||
8956 | unionAssumedWithUndef(); | ||||
8957 | } else if (LHSAA.undefIsContained()) { | ||||
8958 | for (const APInt &R : RHSAAPVS) { | ||||
8959 | bool CmpResult = calculateICmpInst(ICI, Zero, R); | ||||
8960 | MaybeTrue |= CmpResult; | ||||
8961 | MaybeFalse |= !CmpResult; | ||||
8962 | if (MaybeTrue & MaybeFalse) | ||||
8963 | return indicatePessimisticFixpoint(); | ||||
8964 | } | ||||
8965 | } else if (RHSAA.undefIsContained()) { | ||||
8966 | for (const APInt &L : LHSAAPVS) { | ||||
8967 | bool CmpResult = calculateICmpInst(ICI, L, Zero); | ||||
8968 | MaybeTrue |= CmpResult; | ||||
8969 | MaybeFalse |= !CmpResult; | ||||
8970 | if (MaybeTrue & MaybeFalse) | ||||
8971 | return indicatePessimisticFixpoint(); | ||||
8972 | } | ||||
8973 | } else { | ||||
8974 | for (const APInt &L : LHSAAPVS) { | ||||
8975 | for (const APInt &R : RHSAAPVS) { | ||||
8976 | bool CmpResult = calculateICmpInst(ICI, L, R); | ||||
8977 | MaybeTrue |= CmpResult; | ||||
8978 | MaybeFalse |= !CmpResult; | ||||
8979 | if (MaybeTrue & MaybeFalse) | ||||
8980 | return indicatePessimisticFixpoint(); | ||||
8981 | } | ||||
8982 | } | ||||
8983 | } | ||||
8984 | if (MaybeTrue) | ||||
8985 | unionAssumed(APInt(/* numBits */ 1, /* val */ 1)); | ||||
8986 | if (MaybeFalse) | ||||
8987 | unionAssumed(APInt(/* numBits */ 1, /* val */ 0)); | ||||
8988 | return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED | ||||
8989 | : ChangeStatus::CHANGED; | ||||
8990 | } | ||||
8991 | |||||
8992 | ChangeStatus updateWithSelectInst(Attributor &A, SelectInst *SI) { | ||||
8993 | auto AssumedBefore = getAssumed(); | ||||
8994 | Value *LHS = SI->getTrueValue(); | ||||
8995 | Value *RHS = SI->getFalseValue(); | ||||
8996 | |||||
8997 | // Simplify the operands first. | ||||
8998 | bool UsedAssumedInformation = false; | ||||
8999 | const auto &SimplifiedLHS = | ||||
9000 | A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), | ||||
9001 | *this, UsedAssumedInformation); | ||||
9002 | if (!SimplifiedLHS.hasValue()) | ||||
9003 | return ChangeStatus::UNCHANGED; | ||||
9004 | if (!SimplifiedLHS.getValue()) | ||||
9005 | return indicatePessimisticFixpoint(); | ||||
9006 | LHS = *SimplifiedLHS; | ||||
9007 | |||||
9008 | const auto &SimplifiedRHS = | ||||
9009 | A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), | ||||
9010 | *this, UsedAssumedInformation); | ||||
9011 | if (!SimplifiedRHS.hasValue()) | ||||
9012 | return ChangeStatus::UNCHANGED; | ||||
9013 | if (!SimplifiedRHS.getValue()) | ||||
9014 | return indicatePessimisticFixpoint(); | ||||
9015 | RHS = *SimplifiedRHS; | ||||
9016 | |||||
9017 | if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) | ||||
9018 | return indicatePessimisticFixpoint(); | ||||
9019 | |||||
9020 | Optional<Constant *> C = A.getAssumedConstant(*SI->getCondition(), *this, | ||||
9021 | UsedAssumedInformation); | ||||
9022 | |||||
9023 | // Check if we only need one operand. | ||||
9024 | bool OnlyLeft = false, OnlyRight = false; | ||||
9025 | if (C.hasValue() && *C && (*C)->isOneValue()) | ||||
9026 | OnlyLeft = true; | ||||
9027 | else if (C.hasValue() && *C && (*C)->isZeroValue()) | ||||
9028 | OnlyRight = true; | ||||
9029 | |||||
9030 | const AAPotentialValues *LHSAA = nullptr, *RHSAA = nullptr; | ||||
9031 | if (!OnlyRight) { | ||||
9032 | LHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS), | ||||
9033 | DepClassTy::REQUIRED); | ||||
9034 | if (!LHSAA->isValidState()) | ||||
9035 | return indicatePessimisticFixpoint(); | ||||
9036 | } | ||||
9037 | if (!OnlyLeft) { | ||||
9038 | RHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS), | ||||
9039 | DepClassTy::REQUIRED); | ||||
9040 | if (!RHSAA->isValidState()) | ||||
9041 | return indicatePessimisticFixpoint(); | ||||
9042 | } | ||||
9043 | |||||
9044 | if (!LHSAA || !RHSAA) { | ||||
9045 | // select (true/false), lhs, rhs | ||||
9046 | auto *OpAA = LHSAA ? LHSAA : RHSAA; | ||||
9047 | |||||
9048 | if (OpAA->undefIsContained()) | ||||
9049 | unionAssumedWithUndef(); | ||||
9050 | else | ||||
9051 | unionAssumed(*OpAA); | ||||
9052 | |||||
9053 | } else if (LHSAA->undefIsContained() && RHSAA->undefIsContained()) { | ||||
9054 | // select i1 *, undef , undef => undef | ||||
9055 | unionAssumedWithUndef(); | ||||
9056 | } else { | ||||
9057 | unionAssumed(*LHSAA); | ||||
9058 | unionAssumed(*RHSAA); | ||||
9059 | } | ||||
9060 | return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED | ||||
9061 | : ChangeStatus::CHANGED; | ||||
9062 | } | ||||
9063 | |||||
9064 | ChangeStatus updateWithCastInst(Attributor &A, CastInst *CI) { | ||||
9065 | auto AssumedBefore = getAssumed(); | ||||
9066 | if (!CI->isIntegerCast()) | ||||
9067 | return indicatePessimisticFixpoint(); | ||||
9068 | assert(CI->getNumOperands() == 1 && "Expected cast to be unary!")(static_cast <bool> (CI->getNumOperands() == 1 && "Expected cast to be unary!") ? void (0) : __assert_fail ("CI->getNumOperands() == 1 && \"Expected cast to be unary!\"" , "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 9068, __extension__ __PRETTY_FUNCTION__)); | ||||
9069 | uint32_t ResultBitWidth = CI->getDestTy()->getIntegerBitWidth(); | ||||
9070 | Value *Src = CI->getOperand(0); | ||||
9071 | |||||
9072 | // Simplify the operand first. | ||||
9073 | bool UsedAssumedInformation = false; | ||||
9074 | const auto &SimplifiedSrc = | ||||
9075 | A.getAssumedSimplified(IRPosition::value(*Src, getCallBaseContext()), | ||||
9076 | *this, UsedAssumedInformation); | ||||
9077 | if (!SimplifiedSrc.hasValue()) | ||||
9078 | return ChangeStatus::UNCHANGED; | ||||
9079 | if (!SimplifiedSrc.getValue()) | ||||
9080 | return indicatePessimisticFixpoint(); | ||||
9081 | Src = *SimplifiedSrc; | ||||
9082 | |||||
9083 | auto &SrcAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*Src), | ||||
9084 | DepClassTy::REQUIRED); | ||||
9085 | if (!SrcAA.isValidState()) | ||||
9086 | return indicatePessimisticFixpoint(); | ||||
9087 | const DenseSet<APInt> &SrcAAPVS = SrcAA.getAssumedSet(); | ||||
9088 | if (SrcAA.undefIsContained()) | ||||
9089 | unionAssumedWithUndef(); | ||||
9090 | else { | ||||
9091 | for (const APInt &S : SrcAAPVS) { | ||||
9092 | APInt T = calculateCastInst(CI, S, ResultBitWidth); | ||||
9093 | unionAssumed(T); | ||||
9094 | } | ||||
9095 | } | ||||
9096 | return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED | ||||
9097 | : ChangeStatus::CHANGED; | ||||
9098 | } | ||||
9099 | |||||
9100 | ChangeStatus updateWithBinaryOperator(Attributor &A, BinaryOperator *BinOp) { | ||||
9101 | auto AssumedBefore = getAssumed(); | ||||
9102 | Value *LHS = BinOp->getOperand(0); | ||||
9103 | Value *RHS = BinOp->getOperand(1); | ||||
9104 | |||||
9105 | // Simplify the operands first. | ||||
9106 | bool UsedAssumedInformation = false; | ||||
9107 | const auto &SimplifiedLHS = | ||||
9108 | A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()), | ||||
9109 | *this, UsedAssumedInformation); | ||||
9110 | if (!SimplifiedLHS.hasValue()) | ||||
9111 | return ChangeStatus::UNCHANGED; | ||||
9112 | if (!SimplifiedLHS.getValue()) | ||||
9113 | return indicatePessimisticFixpoint(); | ||||
9114 | LHS = *SimplifiedLHS; | ||||
9115 | |||||
9116 | const auto &SimplifiedRHS = | ||||
9117 | A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()), | ||||
9118 | *this, UsedAssumedInformation); | ||||
9119 | if (!SimplifiedRHS.hasValue()) | ||||
9120 | return ChangeStatus::UNCHANGED; | ||||
9121 | if (!SimplifiedRHS.getValue()) | ||||
9122 | return indicatePessimisticFixpoint(); | ||||
9123 | RHS = *SimplifiedRHS; | ||||
9124 | |||||
9125 | if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy()) | ||||
9126 | return indicatePessimisticFixpoint(); | ||||
9127 | |||||
9128 | auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS), | ||||
9129 | DepClassTy::REQUIRED); | ||||
9130 | if (!LHSAA.isValidState()) | ||||
9131 | return indicatePessimisticFixpoint(); | ||||
9132 | |||||
9133 | auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS), | ||||
9134 | DepClassTy::REQUIRED); | ||||
9135 | if (!RHSAA.isValidState()) | ||||
9136 | return indicatePessimisticFixpoint(); | ||||
9137 | |||||
9138 | const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet(); | ||||
9139 | const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet(); | ||||
9140 | const APInt Zero = APInt(LHS->getType()->getIntegerBitWidth(), 0); | ||||
9141 | |||||
9142 | // TODO: make use of undef flag to limit potential values aggressively. | ||||
9143 | if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) { | ||||
9144 | if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, Zero)) | ||||
9145 | return indicatePessimisticFixpoint(); | ||||
9146 | } else if (LHSAA.undefIsContained()) { | ||||
9147 | for (const APInt &R : RHSAAPVS) { | ||||
9148 | if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, R)) | ||||
9149 | return indicatePessimisticFixpoint(); | ||||
9150 | } | ||||
9151 | } else if (RHSAA.undefIsContained()) { | ||||
9152 | for (const APInt &L : LHSAAPVS) { | ||||
9153 | if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, Zero)) | ||||
9154 | return indicatePessimisticFixpoint(); | ||||
9155 | } | ||||
9156 | } else { | ||||
9157 | for (const APInt &L : LHSAAPVS) { | ||||
9158 | for (const APInt &R : RHSAAPVS) { | ||||
9159 | if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, R)) | ||||
9160 | return indicatePessimisticFixpoint(); | ||||
9161 | } | ||||
9162 | } | ||||
9163 | } | ||||
9164 | return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED | ||||
9165 | : ChangeStatus::CHANGED; | ||||
9166 | } | ||||
9167 | |||||
9168 | ChangeStatus updateWithPHINode(Attributor &A, PHINode *PHI) { | ||||
9169 | auto AssumedBefore = getAssumed(); | ||||
9170 | for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) { | ||||
9171 | Value *IncomingValue = PHI->getIncomingValue(u); | ||||
9172 | |||||
9173 | // Simplify the operand first. | ||||
9174 | bool UsedAssumedInformation = false; | ||||
9175 | const auto &SimplifiedIncomingValue = A.getAssumedSimplified( | ||||
9176 | IRPosition::value(*IncomingValue, getCallBaseContext()), *this, | ||||
9177 | UsedAssumedInformation); | ||||
9178 | if (!SimplifiedIncomingValue.hasValue()) | ||||
9179 | continue; | ||||
9180 | if (!SimplifiedIncomingValue.getValue()) | ||||
9181 | return indicatePessimisticFixpoint(); | ||||
9182 | IncomingValue = *SimplifiedIncomingValue; | ||||
9183 | |||||
9184 | auto &PotentialValuesAA = A.getAAFor<AAPotentialValues>( | ||||
9185 | *this, IRPosition::value(*IncomingValue), DepClassTy::REQUIRED); | ||||
9186 | if (!PotentialValuesAA.isValidState()) | ||||
9187 | return indicatePessimisticFixpoint(); | ||||
9188 | if (PotentialValuesAA.undefIsContained()) | ||||
9189 | unionAssumedWithUndef(); | ||||
9190 | else | ||||
9191 | unionAssumed(PotentialValuesAA.getAssumed()); | ||||
9192 | } | ||||
9193 | return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED | ||||
9194 | : ChangeStatus::CHANGED; | ||||
9195 | } | ||||
9196 | |||||
9197 | ChangeStatus updateWithLoad(Attributor &A, LoadInst &L) { | ||||
9198 | if (!L.getType()->isIntegerTy()) | ||||
9199 | return indicatePessimisticFixpoint(); | ||||
9200 | |||||
9201 | auto Union = [&](Value &V) { | ||||
9202 | if (isa<UndefValue>(V)) { | ||||
9203 | unionAssumedWithUndef(); | ||||
9204 | return true; | ||||
9205 | } | ||||
9206 | if (ConstantInt *CI = dyn_cast<ConstantInt>(&V)) { | ||||
9207 | unionAssumed(CI->getValue()); | ||||
9208 | return true; | ||||
9209 | } | ||||
9210 | return false; | ||||
9211 | }; | ||||
9212 | auto AssumedBefore = getAssumed(); | ||||
9213 | |||||
9214 | if (!AAValueSimplifyImpl::handleLoad(A, *this, L, Union)) | ||||
9215 | return indicatePessimisticFixpoint(); | ||||
9216 | |||||
9217 | return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED | ||||
9218 | : ChangeStatus::CHANGED; | ||||
9219 | } | ||||
9220 | |||||
9221 | /// See AbstractAttribute::updateImpl(...). | ||||
9222 | ChangeStatus updateImpl(Attributor &A) override { | ||||
9223 | Value &V = getAssociatedValue(); | ||||
9224 | Instruction *I = dyn_cast<Instruction>(&V); | ||||
9225 | |||||
9226 | if (auto *ICI = dyn_cast<ICmpInst>(I)) | ||||
9227 | return updateWithICmpInst(A, ICI); | ||||
9228 | |||||
9229 | if (auto *SI = dyn_cast<SelectInst>(I)) | ||||
9230 | return updateWithSelectInst(A, SI); | ||||
9231 | |||||
9232 | if (auto *CI = dyn_cast<CastInst>(I)) | ||||
9233 | return updateWithCastInst(A, CI); | ||||
9234 | |||||
9235 | if (auto *BinOp = dyn_cast<BinaryOperator>(I)) | ||||
9236 | return updateWithBinaryOperator(A, BinOp); | ||||
9237 | |||||
9238 | if (auto *PHI = dyn_cast<PHINode>(I)) | ||||
9239 | return updateWithPHINode(A, PHI); | ||||
9240 | |||||
9241 | if (auto *L = dyn_cast<LoadInst>(I)) | ||||
9242 | return updateWithLoad(A, *L); | ||||
9243 | |||||
9244 | return indicatePessimisticFixpoint(); | ||||
9245 | } | ||||
9246 | |||||
9247 | /// See AbstractAttribute::trackStatistics() | ||||
9248 | void trackStatistics() const override { | ||||
9249 | STATS_DECLTRACK_FLOATING_ATTR(potential_values){ static llvm::Statistic NumIRFloating_potential_values = {"attributor" , "NumIRFloating_potential_values", ("Number of floating values known to be '" "potential_values" "'")};; ++(NumIRFloating_potential_values ); } | ||||
9250 | } | ||||
9251 | }; | ||||
9252 | |||||
9253 | struct AAPotentialValuesFunction : AAPotentialValuesImpl { | ||||
9254 | AAPotentialValuesFunction(const IRPosition &IRP, Attributor &A) | ||||
9255 | : AAPotentialValuesImpl(IRP, A) {} | ||||
9256 | |||||
9257 | /// See AbstractAttribute::initialize(...). | ||||
9258 | ChangeStatus updateImpl(Attributor &A) override { | ||||
9259 | llvm_unreachable("AAPotentialValues(Function|CallSite)::updateImpl will "::llvm::llvm_unreachable_internal("AAPotentialValues(Function|CallSite)::updateImpl will " "not be called", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 9260) | ||||
9260 | "not be called")::llvm::llvm_unreachable_internal("AAPotentialValues(Function|CallSite)::updateImpl will " "not be called", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 9260); | ||||
9261 | } | ||||
9262 | |||||
9263 | /// See AbstractAttribute::trackStatistics() | ||||
9264 | void trackStatistics() const override { | ||||
9265 | STATS_DECLTRACK_FN_ATTR(potential_values){ static llvm::Statistic NumIRFunction_potential_values = {"attributor" , "NumIRFunction_potential_values", ("Number of " "functions" " marked '" "potential_values" "'")};; ++(NumIRFunction_potential_values ); } | ||||
9266 | } | ||||
9267 | }; | ||||
9268 | |||||
9269 | struct AAPotentialValuesCallSite : AAPotentialValuesFunction { | ||||
9270 | AAPotentialValuesCallSite(const IRPosition &IRP, Attributor &A) | ||||
9271 | : AAPotentialValuesFunction(IRP, A) {} | ||||
9272 | |||||
9273 | /// See AbstractAttribute::trackStatistics() | ||||
9274 | void trackStatistics() const override { | ||||
9275 | STATS_DECLTRACK_CS_ATTR(potential_values){ static llvm::Statistic NumIRCS_potential_values = {"attributor" , "NumIRCS_potential_values", ("Number of " "call site" " marked '" "potential_values" "'")};; ++(NumIRCS_potential_values); } | ||||
9276 | } | ||||
9277 | }; | ||||
9278 | |||||
9279 | struct AAPotentialValuesCallSiteReturned | ||||
9280 | : AACallSiteReturnedFromReturned<AAPotentialValues, AAPotentialValuesImpl> { | ||||
9281 | AAPotentialValuesCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
9282 | : AACallSiteReturnedFromReturned<AAPotentialValues, | ||||
9283 | AAPotentialValuesImpl>(IRP, A) {} | ||||
9284 | |||||
9285 | /// See AbstractAttribute::trackStatistics() | ||||
9286 | void trackStatistics() const override { | ||||
9287 | STATS_DECLTRACK_CSRET_ATTR(potential_values){ static llvm::Statistic NumIRCSReturn_potential_values = {"attributor" , "NumIRCSReturn_potential_values", ("Number of " "call site returns" " marked '" "potential_values" "'")};; ++(NumIRCSReturn_potential_values ); } | ||||
9288 | } | ||||
9289 | }; | ||||
9290 | |||||
9291 | struct AAPotentialValuesCallSiteArgument : AAPotentialValuesFloating { | ||||
9292 | AAPotentialValuesCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
9293 | : AAPotentialValuesFloating(IRP, A) {} | ||||
9294 | |||||
9295 | /// See AbstractAttribute::initialize(..). | ||||
9296 | void initialize(Attributor &A) override { | ||||
9297 | AAPotentialValuesImpl::initialize(A); | ||||
9298 | if (isAtFixpoint()) | ||||
9299 | return; | ||||
9300 | |||||
9301 | Value &V = getAssociatedValue(); | ||||
9302 | |||||
9303 | if (auto *C = dyn_cast<ConstantInt>(&V)) { | ||||
9304 | unionAssumed(C->getValue()); | ||||
9305 | indicateOptimisticFixpoint(); | ||||
9306 | return; | ||||
9307 | } | ||||
9308 | |||||
9309 | if (isa<UndefValue>(&V)) { | ||||
9310 | unionAssumedWithUndef(); | ||||
9311 | indicateOptimisticFixpoint(); | ||||
9312 | return; | ||||
9313 | } | ||||
9314 | } | ||||
9315 | |||||
9316 | /// See AbstractAttribute::updateImpl(...). | ||||
9317 | ChangeStatus updateImpl(Attributor &A) override { | ||||
9318 | Value &V = getAssociatedValue(); | ||||
9319 | auto AssumedBefore = getAssumed(); | ||||
9320 | auto &AA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(V), | ||||
9321 | DepClassTy::REQUIRED); | ||||
9322 | const auto &S = AA.getAssumed(); | ||||
9323 | unionAssumed(S); | ||||
9324 | return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED | ||||
9325 | : ChangeStatus::CHANGED; | ||||
9326 | } | ||||
9327 | |||||
9328 | /// See AbstractAttribute::trackStatistics() | ||||
9329 | void trackStatistics() const override { | ||||
9330 | STATS_DECLTRACK_CSARG_ATTR(potential_values){ static llvm::Statistic NumIRCSArguments_potential_values = { "attributor", "NumIRCSArguments_potential_values", ("Number of " "call site arguments" " marked '" "potential_values" "'")};; ++(NumIRCSArguments_potential_values); } | ||||
9331 | } | ||||
9332 | }; | ||||
9333 | |||||
9334 | /// ------------------------ NoUndef Attribute --------------------------------- | ||||
9335 | struct AANoUndefImpl : AANoUndef { | ||||
9336 | AANoUndefImpl(const IRPosition &IRP, Attributor &A) : AANoUndef(IRP, A) {} | ||||
9337 | |||||
9338 | /// See AbstractAttribute::initialize(...). | ||||
9339 | void initialize(Attributor &A) override { | ||||
9340 | if (getIRPosition().hasAttr({Attribute::NoUndef})) { | ||||
9341 | indicateOptimisticFixpoint(); | ||||
9342 | return; | ||||
9343 | } | ||||
9344 | Value &V = getAssociatedValue(); | ||||
9345 | if (isa<UndefValue>(V)) | ||||
9346 | indicatePessimisticFixpoint(); | ||||
9347 | else if (isa<FreezeInst>(V)) | ||||
9348 | indicateOptimisticFixpoint(); | ||||
9349 | else if (getPositionKind() != IRPosition::IRP_RETURNED && | ||||
9350 | isGuaranteedNotToBeUndefOrPoison(&V)) | ||||
9351 | indicateOptimisticFixpoint(); | ||||
9352 | else | ||||
9353 | AANoUndef::initialize(A); | ||||
9354 | } | ||||
9355 | |||||
9356 | /// See followUsesInMBEC | ||||
9357 | bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I, | ||||
9358 | AANoUndef::StateType &State) { | ||||
9359 | const Value *UseV = U->get(); | ||||
9360 | const DominatorTree *DT = nullptr; | ||||
9361 | AssumptionCache *AC = nullptr; | ||||
9362 | InformationCache &InfoCache = A.getInfoCache(); | ||||
9363 | if (Function *F = getAnchorScope()) { | ||||
9364 | DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F); | ||||
9365 | AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F); | ||||
9366 | } | ||||
9367 | State.setKnown(isGuaranteedNotToBeUndefOrPoison(UseV, AC, I, DT)); | ||||
9368 | bool TrackUse = false; | ||||
9369 | // Track use for instructions which must produce undef or poison bits when | ||||
9370 | // at least one operand contains such bits. | ||||
9371 | if (isa<CastInst>(*I) || isa<GetElementPtrInst>(*I)) | ||||
9372 | TrackUse = true; | ||||
9373 | return TrackUse; | ||||
9374 | } | ||||
9375 | |||||
9376 | /// See AbstractAttribute::getAsStr(). | ||||
9377 | const std::string getAsStr() const override { | ||||
9378 | return getAssumed() ? "noundef" : "may-undef-or-poison"; | ||||
9379 | } | ||||
9380 | |||||
9381 | ChangeStatus manifest(Attributor &A) override { | ||||
9382 | // We don't manifest noundef attribute for dead positions because the | ||||
9383 | // associated values with dead positions would be replaced with undef | ||||
9384 | // values. | ||||
9385 | bool UsedAssumedInformation = false; | ||||
9386 | if (A.isAssumedDead(getIRPosition(), nullptr, nullptr, | ||||
9387 | UsedAssumedInformation)) | ||||
9388 | return ChangeStatus::UNCHANGED; | ||||
9389 | // A position whose simplified value does not have any value is | ||||
9390 | // considered to be dead. We don't manifest noundef in such positions for | ||||
9391 | // the same reason above. | ||||
9392 | if (!A.getAssumedSimplified(getIRPosition(), *this, UsedAssumedInformation) | ||||
9393 | .hasValue()) | ||||
9394 | return ChangeStatus::UNCHANGED; | ||||
9395 | return AANoUndef::manifest(A); | ||||
9396 | } | ||||
9397 | }; | ||||
9398 | |||||
9399 | struct AANoUndefFloating : public AANoUndefImpl { | ||||
9400 | AANoUndefFloating(const IRPosition &IRP, Attributor &A) | ||||
9401 | : AANoUndefImpl(IRP, A) {} | ||||
9402 | |||||
9403 | /// See AbstractAttribute::initialize(...). | ||||
9404 | void initialize(Attributor &A) override { | ||||
9405 | AANoUndefImpl::initialize(A); | ||||
9406 | if (!getState().isAtFixpoint()) | ||||
9407 | if (Instruction *CtxI = getCtxI()) | ||||
9408 | followUsesInMBEC(*this, A, getState(), *CtxI); | ||||
9409 | } | ||||
9410 | |||||
9411 | /// See AbstractAttribute::updateImpl(...). | ||||
9412 | ChangeStatus updateImpl(Attributor &A) override { | ||||
9413 | auto VisitValueCB = [&](Value &V, const Instruction *CtxI, | ||||
9414 | AANoUndef::StateType &T, bool Stripped) -> bool { | ||||
9415 | const auto &AA = A.getAAFor<AANoUndef>(*this, IRPosition::value(V), | ||||
9416 | DepClassTy::REQUIRED); | ||||
9417 | if (!Stripped && this == &AA) { | ||||
9418 | T.indicatePessimisticFixpoint(); | ||||
9419 | } else { | ||||
9420 | const AANoUndef::StateType &S = | ||||
9421 | static_cast<const AANoUndef::StateType &>(AA.getState()); | ||||
9422 | T ^= S; | ||||
9423 | } | ||||
9424 | return T.isValidState(); | ||||
9425 | }; | ||||
9426 | |||||
9427 | StateType T; | ||||
9428 | bool UsedAssumedInformation = false; | ||||
9429 | if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T, | ||||
9430 | VisitValueCB, getCtxI(), | ||||
9431 | UsedAssumedInformation)) | ||||
9432 | return indicatePessimisticFixpoint(); | ||||
9433 | |||||
9434 | return clampStateAndIndicateChange(getState(), T); | ||||
9435 | } | ||||
9436 | |||||
9437 | /// See AbstractAttribute::trackStatistics() | ||||
9438 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef){ static llvm::Statistic NumIRFunctionReturn_noundef = {"attributor" , "NumIRFunctionReturn_noundef", ("Number of " "function returns" " marked '" "noundef" "'")};; ++(NumIRFunctionReturn_noundef ); } } | ||||
9439 | }; | ||||
9440 | |||||
9441 | struct AANoUndefReturned final | ||||
9442 | : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl> { | ||||
9443 | AANoUndefReturned(const IRPosition &IRP, Attributor &A) | ||||
9444 | : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl>(IRP, A) {} | ||||
9445 | |||||
9446 | /// See AbstractAttribute::trackStatistics() | ||||
9447 | void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef){ static llvm::Statistic NumIRFunctionReturn_noundef = {"attributor" , "NumIRFunctionReturn_noundef", ("Number of " "function returns" " marked '" "noundef" "'")};; ++(NumIRFunctionReturn_noundef ); } } | ||||
9448 | }; | ||||
9449 | |||||
9450 | struct AANoUndefArgument final | ||||
9451 | : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl> { | ||||
9452 | AANoUndefArgument(const IRPosition &IRP, Attributor &A) | ||||
9453 | : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl>(IRP, A) {} | ||||
9454 | |||||
9455 | /// See AbstractAttribute::trackStatistics() | ||||
9456 | void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noundef){ static llvm::Statistic NumIRArguments_noundef = {"attributor" , "NumIRArguments_noundef", ("Number of " "arguments" " marked '" "noundef" "'")};; ++(NumIRArguments_noundef); } } | ||||
9457 | }; | ||||
9458 | |||||
9459 | struct AANoUndefCallSiteArgument final : AANoUndefFloating { | ||||
9460 | AANoUndefCallSiteArgument(const IRPosition &IRP, Attributor &A) | ||||
9461 | : AANoUndefFloating(IRP, A) {} | ||||
9462 | |||||
9463 | /// See AbstractAttribute::trackStatistics() | ||||
9464 | void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noundef){ static llvm::Statistic NumIRCSArguments_noundef = {"attributor" , "NumIRCSArguments_noundef", ("Number of " "call site arguments" " marked '" "noundef" "'")};; ++(NumIRCSArguments_noundef); } } | ||||
9465 | }; | ||||
9466 | |||||
9467 | struct AANoUndefCallSiteReturned final | ||||
9468 | : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl> { | ||||
9469 | AANoUndefCallSiteReturned(const IRPosition &IRP, Attributor &A) | ||||
9470 | : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl>(IRP, A) {} | ||||
9471 | |||||
9472 | /// See AbstractAttribute::trackStatistics() | ||||
9473 | void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noundef){ static llvm::Statistic NumIRCSReturn_noundef = {"attributor" , "NumIRCSReturn_noundef", ("Number of " "call site returns" " marked '" "noundef" "'")};; ++(NumIRCSReturn_noundef); } } | ||||
9474 | }; | ||||
9475 | |||||
9476 | struct AACallEdgesImpl : public AACallEdges { | ||||
9477 | AACallEdgesImpl(const IRPosition &IRP, Attributor &A) : AACallEdges(IRP, A) {} | ||||
9478 | |||||
9479 | virtual const SetVector<Function *> &getOptimisticEdges() const override { | ||||
9480 | return CalledFunctions; | ||||
9481 | } | ||||
9482 | |||||
9483 | virtual bool hasUnknownCallee() const override { return HasUnknownCallee; } | ||||
9484 | |||||
9485 | virtual bool hasNonAsmUnknownCallee() const override { | ||||
9486 | return HasUnknownCalleeNonAsm; | ||||
9487 | } | ||||
9488 | |||||
9489 | const std::string getAsStr() const override { | ||||
9490 | return "CallEdges[" + std::to_string(HasUnknownCallee) + "," + | ||||
9491 | std::to_string(CalledFunctions.size()) + "]"; | ||||
9492 | } | ||||
9493 | |||||
9494 | void trackStatistics() const override {} | ||||
9495 | |||||
9496 | protected: | ||||
9497 | void addCalledFunction(Function *Fn, ChangeStatus &Change) { | ||||
9498 | if (CalledFunctions.insert(Fn)) { | ||||
9499 | Change = ChangeStatus::CHANGED; | ||||
9500 | LLVM_DEBUG(dbgs() << "[AACallEdges] New call edge: " << Fn->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AACallEdges] New call edge: " << Fn->getName() << "\n"; } } while (false) | ||||
9501 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AACallEdges] New call edge: " << Fn->getName() << "\n"; } } while (false); | ||||
9502 | } | ||||
9503 | } | ||||
9504 | |||||
9505 | void setHasUnknownCallee(bool NonAsm, ChangeStatus &Change) { | ||||
9506 | if (!HasUnknownCallee) | ||||
9507 | Change = ChangeStatus::CHANGED; | ||||
9508 | if (NonAsm && !HasUnknownCalleeNonAsm) | ||||
9509 | Change = ChangeStatus::CHANGED; | ||||
9510 | HasUnknownCalleeNonAsm |= NonAsm; | ||||
9511 | HasUnknownCallee = true; | ||||
9512 | } | ||||
9513 | |||||
9514 | private: | ||||
9515 | /// Optimistic set of functions that might be called by this position. | ||||
9516 | SetVector<Function *> CalledFunctions; | ||||
9517 | |||||
9518 | /// Is there any call with a unknown callee. | ||||
9519 | bool HasUnknownCallee = false; | ||||
9520 | |||||
9521 | /// Is there any call with a unknown callee, excluding any inline asm. | ||||
9522 | bool HasUnknownCalleeNonAsm = false; | ||||
9523 | }; | ||||
9524 | |||||
9525 | struct AACallEdgesCallSite : public AACallEdgesImpl { | ||||
9526 | AACallEdgesCallSite(const IRPosition &IRP, Attributor &A) | ||||
9527 | : AACallEdgesImpl(IRP, A) {} | ||||
9528 | /// See AbstractAttribute::updateImpl(...). | ||||
9529 | ChangeStatus updateImpl(Attributor &A) override { | ||||
9530 | ChangeStatus Change = ChangeStatus::UNCHANGED; | ||||
9531 | |||||
9532 | auto VisitValue = [&](Value &V, const Instruction *CtxI, bool &HasUnknown, | ||||
9533 | bool Stripped) -> bool { | ||||
9534 | if (Function *Fn = dyn_cast<Function>(&V)) { | ||||
9535 | addCalledFunction(Fn, Change); | ||||
9536 | } else { | ||||
9537 | LLVM_DEBUG(dbgs() << "[AACallEdges] Unrecognized value: " << V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("attributor")) { dbgs() << "[AACallEdges] Unrecognized value: " << V << "\n"; } } while (false); | ||||
9538 | setHasUnknownCallee(true, Change); | ||||
9539 | } | ||||
9540 | |||||
9541 | // Explore all values. | ||||
9542 | return true; | ||||
9543 | }; | ||||
9544 | |||||
9545 | // Process any value that we might call. | ||||
9546 | auto ProcessCalledOperand = [&](Value *V) { | ||||
9547 | bool DummyValue = false; | ||||
9548 | bool UsedAssumedInformation = false; | ||||
9549 | if (!genericValueTraversal<bool>(A, IRPosition::value(*V), *this, | ||||
9550 | DummyValue, VisitValue, nullptr, | ||||
9551 | UsedAssumedInformation, false)) { | ||||
9552 | // If we haven't gone through all values, assume that there are unknown | ||||
9553 | // callees. | ||||
9554 | setHasUnknownCallee(true, Change); | ||||
9555 | } | ||||
9556 | }; | ||||
9557 | |||||
9558 | CallBase *CB = cast<CallBase>(getCtxI()); | ||||
9559 | |||||
9560 | if (CB->isInlineAsm()) { | ||||
9561 | setHasUnknownCallee(false, Change); | ||||
9562 | return Change; | ||||
9563 | } | ||||
9564 | |||||
9565 | // Process callee metadata if available. | ||||
9566 | if (auto *MD = getCtxI()->getMetadata(LLVMContext::MD_callees)) { | ||||
9567 | for (auto &Op : MD->operands()) { | ||||
9568 | Function *Callee = mdconst::dyn_extract_or_null<Function>(Op); | ||||
9569 | if (Callee) | ||||
9570 | addCalledFunction(Callee, Change); | ||||
9571 | } | ||||
9572 | return Change; | ||||
9573 | } | ||||
9574 | |||||
9575 | // The most simple case. | ||||
9576 | ProcessCalledOperand(CB->getCalledOperand()); | ||||
9577 | |||||
9578 | // Process callback functions. | ||||
9579 | SmallVector<const Use *, 4u> CallbackUses; | ||||
9580 | AbstractCallSite::getCallbackUses(*CB, CallbackUses); | ||||
9581 | for (const Use *U : CallbackUses) | ||||
9582 | ProcessCalledOperand(U->get()); | ||||
9583 | |||||
9584 | return Change; | ||||
9585 | } | ||||
9586 | }; | ||||
9587 | |||||
9588 | struct AACallEdgesFunction : public AACallEdgesImpl { | ||||
9589 | AACallEdgesFunction(const IRPosition &IRP, Attributor &A) | ||||
9590 | : AACallEdgesImpl(IRP, A) {} | ||||
9591 | |||||
9592 | /// See AbstractAttribute::updateImpl(...). | ||||
9593 | ChangeStatus updateImpl(Attributor &A) override { | ||||
9594 | ChangeStatus Change = ChangeStatus::UNCHANGED; | ||||
9595 | |||||
9596 | auto ProcessCallInst = [&](Instruction &Inst) { | ||||
9597 | CallBase &CB = cast<CallBase>(Inst); | ||||
9598 | |||||
9599 | auto &CBEdges = A.getAAFor<AACallEdges>( | ||||
9600 | *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED); | ||||
9601 | if (CBEdges.hasNonAsmUnknownCallee()) | ||||
9602 | setHasUnknownCallee(true, Change); | ||||
9603 | if (CBEdges.hasUnknownCallee()) | ||||
9604 | setHasUnknownCallee(false, Change); | ||||
9605 | |||||
9606 | for (Function *F : CBEdges.getOptimisticEdges()) | ||||
9607 | addCalledFunction(F, Change); | ||||
9608 | |||||
9609 | return true; | ||||
9610 | }; | ||||
9611 | |||||
9612 | // Visit all callable instructions. | ||||
9613 | bool UsedAssumedInformation = false; | ||||
9614 | if (!A.checkForAllCallLikeInstructions(ProcessCallInst, *this, | ||||
9615 | UsedAssumedInformation, | ||||
9616 | /* CheckBBLivenessOnly */ true)) { | ||||
9617 | // If we haven't looked at all call like instructions, assume that there | ||||
9618 | // are unknown callees. | ||||
9619 | setHasUnknownCallee(true, Change); | ||||
9620 | } | ||||
9621 | |||||
9622 | return Change; | ||||
9623 | } | ||||
9624 | }; | ||||
9625 | |||||
9626 | struct AAFunctionReachabilityFunction : public AAFunctionReachability { | ||||
9627 | private: | ||||
9628 | struct QuerySet { | ||||
9629 | void markReachable(const Function &Fn) { | ||||
9630 | Reachable.insert(&Fn); | ||||
9631 | Unreachable.erase(&Fn); | ||||
9632 | } | ||||
9633 | |||||
9634 | /// If there is no information about the function None is returned. | ||||
9635 | Optional<bool> isCachedReachable(const Function &Fn) { | ||||
9636 | // Assume that we can reach the function. | ||||
9637 | // TODO: Be more specific with the unknown callee. | ||||
9638 | if (CanReachUnknownCallee) | ||||
9639 | return true; | ||||
9640 | |||||
9641 | if (Reachable.count(&Fn)) | ||||
9642 | return true; | ||||
9643 | |||||
9644 | if (Unreachable.count(&Fn)) | ||||
9645 | return false; | ||||
9646 | |||||
9647 | return llvm::None; | ||||
9648 | } | ||||
9649 | |||||
9650 | /// Set of functions that we know for sure is reachable. | ||||
9651 | DenseSet<const Function *> Reachable; | ||||
9652 | |||||
9653 | /// Set of functions that are unreachable, but might become reachable. | ||||
9654 | DenseSet<const Function *> Unreachable; | ||||
9655 | |||||
9656 | /// If we can reach a function with a call to a unknown function we assume | ||||
9657 | /// that we can reach any function. | ||||
9658 | bool CanReachUnknownCallee = false; | ||||
9659 | }; | ||||
9660 | |||||
9661 | struct QueryResolver : public QuerySet { | ||||
9662 | ChangeStatus update(Attributor &A, const AAFunctionReachability &AA, | ||||
9663 | ArrayRef<const AACallEdges *> AAEdgesList) { | ||||
9664 | ChangeStatus Change = ChangeStatus::UNCHANGED; | ||||
9665 | |||||
9666 | for (auto *AAEdges : AAEdgesList) { | ||||
9667 | if (AAEdges->hasUnknownCallee()) { | ||||
9668 | if (!CanReachUnknownCallee) | ||||
9669 | Change = ChangeStatus::CHANGED; | ||||
9670 | CanReachUnknownCallee = true; | ||||
9671 | return Change; | ||||
9672 | } | ||||
9673 | } | ||||
9674 | |||||
9675 | for (const Function *Fn : make_early_inc_range(Unreachable)) { | ||||
9676 | if (checkIfReachable(A, AA, AAEdgesList, *Fn)) { | ||||
9677 | Change = ChangeStatus::CHANGED; | ||||
9678 | markReachable(*Fn); | ||||
9679 | } | ||||
9680 | } | ||||
9681 | return Change; | ||||
9682 | } | ||||
9683 | |||||
9684 | bool isReachable(Attributor &A, AAFunctionReachability &AA, | ||||
9685 | ArrayRef<const AACallEdges *> AAEdgesList, | ||||
9686 | const Function &Fn) { | ||||
9687 | Optional<bool> Cached = isCachedReachable(Fn); | ||||
9688 | if (Cached.hasValue()) | ||||
9689 | return Cached.getValue(); | ||||
9690 | |||||
9691 | // The query was not cached, thus it is new. We need to request an update | ||||
9692 | // explicitly to make sure this the information is properly run to a | ||||
9693 | // fixpoint. | ||||
9694 | A.registerForUpdate(AA); | ||||
9695 | |||||
9696 | // We need to assume that this function can't reach Fn to prevent | ||||
9697 | // an infinite loop if this function is recursive. | ||||
9698 | Unreachable.insert(&Fn); | ||||
9699 | |||||
9700 | bool Result = checkIfReachable(A, AA, AAEdgesList, Fn); | ||||
9701 | if (Result) | ||||
9702 | markReachable(Fn); | ||||
9703 | return Result; | ||||
9704 | } | ||||
9705 | |||||
9706 | bool checkIfReachable(Attributor &A, const AAFunctionReachability &AA, | ||||
9707 | ArrayRef<const AACallEdges *> AAEdgesList, | ||||
9708 | const Function &Fn) const { | ||||
9709 | |||||
9710 | // Handle the most trivial case first. | ||||
9711 | for (auto *AAEdges : AAEdgesList) { | ||||
9712 | const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges(); | ||||
9713 | |||||
9714 | if (Edges.count(const_cast<Function *>(&Fn))) | ||||
9715 | return true; | ||||
9716 | } | ||||
9717 | |||||
9718 | SmallVector<const AAFunctionReachability *, 8> Deps; | ||||
9719 | for (auto &AAEdges : AAEdgesList) { | ||||
9720 | const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges(); | ||||
9721 | |||||
9722 | for (Function *Edge : Edges) { | ||||
9723 | // We don't need a dependency if the result is reachable. | ||||
9724 | const AAFunctionReachability &EdgeReachability = | ||||
9725 | A.getAAFor<AAFunctionReachability>( | ||||
9726 | AA, IRPosition::function(*Edge), DepClassTy::NONE); | ||||
9727 | Deps.push_back(&EdgeReachability); | ||||
9728 | |||||
9729 | if (EdgeReachability.canReach(A, Fn)) | ||||
9730 | return true; | ||||
9731 | } | ||||
9732 | } | ||||
9733 | |||||
9734 | // The result is false for now, set dependencies and leave. | ||||
9735 | for (auto *Dep : Deps) | ||||
9736 | A.recordDependence(*Dep, AA, DepClassTy::REQUIRED); | ||||
9737 | |||||
9738 | return false; | ||||
9739 | } | ||||
9740 | }; | ||||
9741 | |||||
9742 | /// Get call edges that can be reached by this instruction. | ||||
9743 | bool getReachableCallEdges(Attributor &A, const AAReachability &Reachability, | ||||
9744 | const Instruction &Inst, | ||||
9745 | SmallVector<const AACallEdges *> &Result) const { | ||||
9746 | // Determine call like instructions that we can reach from the inst. | ||||
9747 | auto CheckCallBase = [&](Instruction &CBInst) { | ||||
9748 | if (!Reachability.isAssumedReachable(A, Inst, CBInst)) | ||||
9749 | return true; | ||||
9750 | |||||
9751 | auto &CB = cast<CallBase>(CBInst); | ||||
9752 | const AACallEdges &AAEdges = A.getAAFor<AACallEdges>( | ||||
9753 | *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED); | ||||
9754 | |||||
9755 | Result.push_back(&AAEdges); | ||||
9756 | return true; | ||||
9757 | }; | ||||
9758 | |||||
9759 | bool UsedAssumedInformation = false; | ||||
9760 | return A.checkForAllCallLikeInstructions(CheckCallBase, *this, | ||||
9761 | UsedAssumedInformation, | ||||
9762 | /* CheckBBLivenessOnly */ true); | ||||
9763 | } | ||||
9764 | |||||
9765 | public: | ||||
9766 | AAFunctionReachabilityFunction(const IRPosition &IRP, Attributor &A) | ||||
9767 | : AAFunctionReachability(IRP, A) {} | ||||
9768 | |||||
9769 | bool canReach(Attributor &A, const Function &Fn) const override { | ||||
9770 | if (!isValidState()) | ||||
9771 | return true; | ||||
9772 | |||||
9773 | const AACallEdges &AAEdges = | ||||
9774 | A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED); | ||||
9775 | |||||
9776 | // Attributor returns attributes as const, so this function has to be | ||||
9777 | // const for users of this attribute to use it without having to do | ||||
9778 | // a const_cast. | ||||
9779 | // This is a hack for us to be able to cache queries. | ||||
9780 | auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this); | ||||
9781 | bool Result = NonConstThis->WholeFunction.isReachable(A, *NonConstThis, | ||||
9782 | {&AAEdges}, Fn); | ||||
9783 | |||||
9784 | return Result; | ||||
9785 | } | ||||
9786 | |||||
9787 | /// Can \p CB reach \p Fn | ||||
9788 | bool canReach(Attributor &A, CallBase &CB, | ||||
9789 | const Function &Fn) const override { | ||||
9790 | if (!isValidState()) | ||||
9791 | return true; | ||||
9792 | |||||
9793 | const AACallEdges &AAEdges = A.getAAFor<AACallEdges>( | ||||
9794 | *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED); | ||||
9795 | |||||
9796 | // Attributor returns attributes as const, so this function has to be | ||||
9797 | // const for users of this attribute to use it without having to do | ||||
9798 | // a const_cast. | ||||
9799 | // This is a hack for us to be able to cache queries. | ||||
9800 | auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this); | ||||
9801 | QueryResolver &CBQuery = NonConstThis->CBQueries[&CB]; | ||||
9802 | |||||
9803 | bool Result = CBQuery.isReachable(A, *NonConstThis, {&AAEdges}, Fn); | ||||
9804 | |||||
9805 | return Result; | ||||
9806 | } | ||||
9807 | |||||
9808 | bool instructionCanReach(Attributor &A, const Instruction &Inst, | ||||
9809 | const Function &Fn, | ||||
9810 | bool UseBackwards) const override { | ||||
9811 | if (!isValidState()) | ||||
9812 | return true; | ||||
9813 | |||||
9814 | if (UseBackwards) | ||||
9815 | return AA::isPotentiallyReachable(A, Inst, Fn, *this, nullptr); | ||||
9816 | |||||
9817 | const auto &Reachability = A.getAAFor<AAReachability>( | ||||
9818 | *this, IRPosition::function(*getAssociatedFunction()), | ||||
9819 | DepClassTy::REQUIRED); | ||||
9820 | |||||
9821 | SmallVector<const AACallEdges *> CallEdges; | ||||
9822 | bool AllKnown = getReachableCallEdges(A, Reachability, Inst, CallEdges); | ||||
9823 | // Attributor returns attributes as const, so this function has to be | ||||
9824 | // const for users of this attribute to use it without having to do | ||||
9825 | // a const_cast. | ||||
9826 | // This is a hack for us to be able to cache queries. | ||||
9827 | auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this); | ||||
9828 | QueryResolver &InstQSet = NonConstThis->InstQueries[&Inst]; | ||||
9829 | if (!AllKnown) | ||||
9830 | InstQSet.CanReachUnknownCallee = true; | ||||
9831 | |||||
9832 | return InstQSet.isReachable(A, *NonConstThis, CallEdges, Fn); | ||||
9833 | } | ||||
9834 | |||||
9835 | /// See AbstractAttribute::updateImpl(...). | ||||
9836 | ChangeStatus updateImpl(Attributor &A) override { | ||||
9837 | const AACallEdges &AAEdges = | ||||
9838 | A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED); | ||||
9839 | ChangeStatus Change = ChangeStatus::UNCHANGED; | ||||
9840 | |||||
9841 | Change |= WholeFunction.update(A, *this, {&AAEdges}); | ||||
9842 | |||||
9843 | for (auto &CBPair : CBQueries) { | ||||
9844 | const AACallEdges &AAEdges = A.getAAFor<AACallEdges>( | ||||
9845 | *this, IRPosition::callsite_function(*CBPair.first), | ||||
9846 | DepClassTy::REQUIRED); | ||||
9847 | |||||
9848 | Change |= CBPair.second.update(A, *this, {&AAEdges}); | ||||
9849 | } | ||||
9850 | |||||
9851 | // Update the Instruction queries. | ||||
9852 | if (!InstQueries.empty()) { | ||||
9853 | const AAReachability *Reachability = &A.getAAFor<AAReachability>( | ||||
9854 | *this, IRPosition::function(*getAssociatedFunction()), | ||||
9855 | DepClassTy::REQUIRED); | ||||
9856 | |||||
9857 | // Check for local callbases first. | ||||
9858 | for (auto &InstPair : InstQueries) { | ||||
9859 | SmallVector<const AACallEdges *> CallEdges; | ||||
9860 | bool AllKnown = | ||||
9861 | getReachableCallEdges(A, *Reachability, *InstPair.first, CallEdges); | ||||
9862 | // Update will return change if we this effects any queries. | ||||
9863 | if (!AllKnown) | ||||
9864 | InstPair.second.CanReachUnknownCallee = true; | ||||
9865 | Change |= InstPair.second.update(A, *this, CallEdges); | ||||
9866 | } | ||||
9867 | } | ||||
9868 | |||||
9869 | return Change; | ||||
9870 | } | ||||
9871 | |||||
9872 | const std::string getAsStr() const override { | ||||
9873 | size_t QueryCount = | ||||
9874 | WholeFunction.Reachable.size() + WholeFunction.Unreachable.size(); | ||||
9875 | |||||
9876 | return "FunctionReachability [" + | ||||
9877 | std::to_string(WholeFunction.Reachable.size()) + "," + | ||||
9878 | std::to_string(QueryCount) + "]"; | ||||
9879 | } | ||||
9880 | |||||
9881 | void trackStatistics() const override {} | ||||
9882 | |||||
9883 | private: | ||||
9884 | bool canReachUnknownCallee() const override { | ||||
9885 | return WholeFunction.CanReachUnknownCallee; | ||||
9886 | } | ||||
9887 | |||||
9888 | /// Used to answer if a the whole function can reacha a specific function. | ||||
9889 | QueryResolver WholeFunction; | ||||
9890 | |||||
9891 | /// Used to answer if a call base inside this function can reach a specific | ||||
9892 | /// function. | ||||
9893 | DenseMap<const CallBase *, QueryResolver> CBQueries; | ||||
9894 | |||||
9895 | /// This is for instruction queries than scan "forward". | ||||
9896 | DenseMap<const Instruction *, QueryResolver> InstQueries; | ||||
9897 | }; | ||||
9898 | |||||
9899 | /// ---------------------- Assumption Propagation ------------------------------ | ||||
9900 | struct AAAssumptionInfoImpl : public AAAssumptionInfo { | ||||
9901 | AAAssumptionInfoImpl(const IRPosition &IRP, Attributor &A, | ||||
9902 | const DenseSet<StringRef> &Known) | ||||
9903 | : AAAssumptionInfo(IRP, A, Known) {} | ||||
9904 | |||||
9905 | bool hasAssumption(const StringRef Assumption) const override { | ||||
9906 | return isValidState() && setContains(Assumption); | ||||
9907 | } | ||||
9908 | |||||
9909 | /// See AbstractAttribute::getAsStr() | ||||
9910 | const std::string getAsStr() const override { | ||||
9911 | const SetContents &Known = getKnown(); | ||||
9912 | const SetContents &Assumed = getAssumed(); | ||||
9913 | |||||
9914 | const std::string KnownStr = | ||||
9915 | llvm::join(Known.getSet().begin(), Known.getSet().end(), ","); | ||||
9916 | const std::string AssumedStr = | ||||
9917 | (Assumed.isUniversal()) | ||||
9918 | ? "Universal" | ||||
9919 | : llvm::join(Assumed.getSet().begin(), Assumed.getSet().end(), ","); | ||||
9920 | |||||
9921 | return "Known [" + KnownStr + "]," + " Assumed [" + AssumedStr + "]"; | ||||
9922 | } | ||||
9923 | }; | ||||
9924 | |||||
9925 | /// Propagates assumption information from parent functions to all of their | ||||
9926 | /// successors. An assumption can be propagated if the containing function | ||||
9927 | /// dominates the called function. | ||||
9928 | /// | ||||
9929 | /// We start with a "known" set of assumptions already valid for the associated | ||||
9930 | /// function and an "assumed" set that initially contains all possible | ||||
9931 | /// assumptions. The assumed set is inter-procedurally updated by narrowing its | ||||
9932 | /// contents as concrete values are known. The concrete values are seeded by the | ||||
9933 | /// first nodes that are either entries into the call graph, or contains no | ||||
9934 | /// assumptions. Each node is updated as the intersection of the assumed state | ||||
9935 | /// with all of its predecessors. | ||||
9936 | struct AAAssumptionInfoFunction final : AAAssumptionInfoImpl { | ||||
9937 | AAAssumptionInfoFunction(const IRPosition &IRP, Attributor &A) | ||||
9938 | : AAAssumptionInfoImpl(IRP, A, | ||||
9939 | getAssumptions(*IRP.getAssociatedFunction())) {} | ||||
9940 | |||||
9941 | /// See AbstractAttribute::manifest(...). | ||||
9942 | ChangeStatus manifest(Attributor &A) override { | ||||
9943 | const auto &Assumptions = getKnown(); | ||||
9944 | |||||
9945 | // Don't manifest a universal set if it somehow made it here. | ||||
9946 | if (Assumptions.isUniversal()) | ||||
9947 | return ChangeStatus::UNCHANGED; | ||||
9948 | |||||
9949 | Function *AssociatedFunction = getAssociatedFunction(); | ||||
9950 | |||||
9951 | bool Changed = addAssumptions(*AssociatedFunction, Assumptions.getSet()); | ||||
9952 | |||||
9953 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | ||||
9954 | } | ||||
9955 | |||||
9956 | /// See AbstractAttribute::updateImpl(...). | ||||
9957 | ChangeStatus updateImpl(Attributor &A) override { | ||||
9958 | bool Changed = false; | ||||
9959 | |||||
9960 | auto CallSitePred = [&](AbstractCallSite ACS) { | ||||
9961 | const auto &AssumptionAA = A.getAAFor<AAAssumptionInfo>( | ||||
9962 | *this, IRPosition::callsite_function(*ACS.getInstruction()), | ||||
9963 | DepClassTy::REQUIRED); | ||||
9964 | // Get the set of assumptions shared by all of this function's callers. | ||||
9965 | Changed |= getIntersection(AssumptionAA.getAssumed()); | ||||
9966 | return !getAssumed().empty() || !getKnown().empty(); | ||||
9967 | }; | ||||
9968 | |||||
9969 | bool UsedAssumedInformation = false; | ||||
9970 | // Get the intersection of all assumptions held by this node's predecessors. | ||||
9971 | // If we don't know all the call sites then this is either an entry into the | ||||
9972 | // call graph or an empty node. This node is known to only contain its own | ||||
9973 | // assumptions and can be propagated to its successors. | ||||
9974 | if (!A.checkForAllCallSites(CallSitePred, *this, true, | ||||
9975 | UsedAssumedInformation)) | ||||
9976 | return indicatePessimisticFixpoint(); | ||||
9977 | |||||
9978 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | ||||
9979 | } | ||||
9980 | |||||
9981 | void trackStatistics() const override {} | ||||
9982 | }; | ||||
9983 | |||||
9984 | /// Assumption Info defined for call sites. | ||||
9985 | struct AAAssumptionInfoCallSite final : AAAssumptionInfoImpl { | ||||
9986 | |||||
9987 | AAAssumptionInfoCallSite(const IRPosition &IRP, Attributor &A) | ||||
9988 | : AAAssumptionInfoImpl(IRP, A, getInitialAssumptions(IRP)) {} | ||||
9989 | |||||
9990 | /// See AbstractAttribute::initialize(...). | ||||
9991 | void initialize(Attributor &A) override { | ||||
9992 | const IRPosition &FnPos = IRPosition::function(*getAnchorScope()); | ||||
9993 | A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED); | ||||
9994 | } | ||||
9995 | |||||
9996 | /// See AbstractAttribute::manifest(...). | ||||
9997 | ChangeStatus manifest(Attributor &A) override { | ||||
9998 | // Don't manifest a universal set if it somehow made it here. | ||||
9999 | if (getKnown().isUniversal()) | ||||
10000 | return ChangeStatus::UNCHANGED; | ||||
10001 | |||||
10002 | CallBase &AssociatedCall = cast<CallBase>(getAssociatedValue()); | ||||
10003 | bool Changed = addAssumptions(AssociatedCall, getAssumed().getSet()); | ||||
10004 | |||||
10005 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | ||||
10006 | } | ||||
10007 | |||||
10008 | /// See AbstractAttribute::updateImpl(...). | ||||
10009 | ChangeStatus updateImpl(Attributor &A) override { | ||||
10010 | const IRPosition &FnPos = IRPosition::function(*getAnchorScope()); | ||||
10011 | auto &AssumptionAA = | ||||
10012 | A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED); | ||||
10013 | bool Changed = getIntersection(AssumptionAA.getAssumed()); | ||||
10014 | return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; | ||||
10015 | } | ||||
10016 | |||||
10017 | /// See AbstractAttribute::trackStatistics() | ||||
10018 | void trackStatistics() const override {} | ||||
10019 | |||||
10020 | private: | ||||
10021 | /// Helper to initialized the known set as all the assumptions this call and | ||||
10022 | /// the callee contain. | ||||
10023 | DenseSet<StringRef> getInitialAssumptions(const IRPosition &IRP) { | ||||
10024 | const CallBase &CB = cast<CallBase>(IRP.getAssociatedValue()); | ||||
10025 | auto Assumptions = getAssumptions(CB); | ||||
10026 | if (Function *F = IRP.getAssociatedFunction()) | ||||
10027 | set_union(Assumptions, getAssumptions(*F)); | ||||
10028 | if (Function *F = IRP.getAssociatedFunction()) | ||||
10029 | set_union(Assumptions, getAssumptions(*F)); | ||||
10030 | return Assumptions; | ||||
10031 | } | ||||
10032 | }; | ||||
10033 | |||||
10034 | AACallGraphNode *AACallEdgeIterator::operator*() const { | ||||
10035 | return static_cast<AACallGraphNode *>(const_cast<AACallEdges *>( | ||||
10036 | &A.getOrCreateAAFor<AACallEdges>(IRPosition::function(**I)))); | ||||
10037 | } | ||||
10038 | |||||
10039 | void AttributorCallGraph::print() { llvm::WriteGraph(outs(), this); } | ||||
10040 | |||||
10041 | const char AAReturnedValues::ID = 0; | ||||
10042 | const char AANoUnwind::ID = 0; | ||||
10043 | const char AANoSync::ID = 0; | ||||
10044 | const char AANoFree::ID = 0; | ||||
10045 | const char AANonNull::ID = 0; | ||||
10046 | const char AANoRecurse::ID = 0; | ||||
10047 | const char AAWillReturn::ID = 0; | ||||
10048 | const char AAUndefinedBehavior::ID = 0; | ||||
10049 | const char AANoAlias::ID = 0; | ||||
10050 | const char AAReachability::ID = 0; | ||||
10051 | const char AANoReturn::ID = 0; | ||||
10052 | const char AAIsDead::ID = 0; | ||||
10053 | const char AADereferenceable::ID = 0; | ||||
10054 | const char AAAlign::ID = 0; | ||||
10055 | const char AANoCapture::ID = 0; | ||||
10056 | const char AAValueSimplify::ID = 0; | ||||
10057 | const char AAHeapToStack::ID = 0; | ||||
10058 | const char AAPrivatizablePtr::ID = 0; | ||||
10059 | const char AAMemoryBehavior::ID = 0; | ||||
10060 | const char AAMemoryLocation::ID = 0; | ||||
10061 | const char AAValueConstantRange::ID = 0; | ||||
10062 | const char AAPotentialValues::ID = 0; | ||||
10063 | const char AANoUndef::ID = 0; | ||||
10064 | const char AACallEdges::ID = 0; | ||||
10065 | const char AAFunctionReachability::ID = 0; | ||||
10066 | const char AAPointerInfo::ID = 0; | ||||
10067 | const char AAAssumptionInfo::ID = 0; | ||||
10068 | |||||
10069 | // Macro magic to create the static generator function for attributes that | ||||
10070 | // follow the naming scheme. | ||||
10071 | |||||
10072 | #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \ | ||||
10073 | case IRPosition::PK: \ | ||||
10074 | llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!")::llvm::llvm_unreachable_internal("Cannot create " #CLASS " for a " POS_NAME " position!", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp" , 10074); | ||||
10075 | |||||
10076 | #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \ | ||||
10077 | case IRPosition::PK: \ | ||||
10078 | AA = new (A.Allocator) CLASS##SUFFIX(IRP, A); \ | ||||
10079 | ++NumAAs; \ | ||||
10080 | break; | ||||
10081 | |||||
10082 | #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ | ||||
10083 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ | ||||
10084 | CLASS *AA = nullptr; \ | ||||
10085 | switch (IRP.getPositionKind()) { \ | ||||
10086 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ | ||||
10087 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ | ||||
10088 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ | ||||
10089 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ | ||||
10090 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ | ||||
10091 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ | ||||
10092 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ | ||||
10093 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ | ||||
10094 | } \ | ||||
10095 | return *AA; \ | ||||
10096 | } | ||||
10097 | |||||
10098 | #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ | ||||
10099 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ | ||||
10100 | CLASS *AA = nullptr; \ | ||||
10101 | switch (IRP.getPositionKind()) { \ | ||||
10102 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ | ||||
10103 | SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \ | ||||
10104 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ | ||||
10105 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ | ||||
10106 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ | ||||
10107 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ | ||||
10108 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ | ||||
10109 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ | ||||
10110 | } \ | ||||
10111 | return *AA; \ | ||||
10112 | } | ||||
10113 | |||||
10114 | #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ | ||||
10115 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ | ||||
10116 | CLASS *AA = nullptr; \ | ||||
10117 | switch (IRP.getPositionKind()) { \ | ||||
10118 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ | ||||
10119 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ | ||||
10120 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ | ||||
10121 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ | ||||
10122 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ | ||||
10123 | SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \ | ||||
10124 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ | ||||
10125 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ | ||||
10126 | } \ | ||||
10127 | return *AA; \ | ||||
10128 | } | ||||
10129 | |||||
10130 | #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ | ||||
10131 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ | ||||
10132 | CLASS *AA = nullptr; \ | ||||
10133 | switch (IRP.getPositionKind()) { \ | ||||
10134 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ | ||||
10135 | SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \ | ||||
10136 | SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \ | ||||
10137 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ | ||||
10138 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \ | ||||
10139 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \ | ||||
10140 | SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \ | ||||
10141 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ | ||||
10142 | } \ | ||||
10143 | return *AA; \ | ||||
10144 | } | ||||
10145 | |||||
10146 | #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \ | ||||
10147 | CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \ | ||||
10148 | CLASS *AA = nullptr; \ | ||||
10149 | switch (IRP.getPositionKind()) { \ | ||||
10150 | SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \ | ||||
10151 | SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \ | ||||
10152 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \ | ||||
10153 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \ | ||||
10154 | SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \ | ||||
10155 | SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \ | ||||
10156 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \ | ||||
10157 | SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \ | ||||
10158 | } \ | ||||
10159 | return *AA; \ | ||||
10160 | } | ||||
10161 | |||||
10162 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind) | ||||
10163 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync) | ||||
10164 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse) | ||||
10165 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn) | ||||
10166 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn) | ||||
10167 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues) | ||||
10168 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation) | ||||
10169 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AACallEdges) | ||||
10170 | CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAssumptionInfo) | ||||
10171 | |||||
10172 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull) | ||||
10173 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias) | ||||
10174 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr) | ||||
10175 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable) | ||||
10176 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign) | ||||
10177 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture) | ||||
10178 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange) | ||||
10179 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialValues) | ||||
10180 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUndef) | ||||
10181 | CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPointerInfo) | ||||
10182 | |||||
10183 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify) | ||||
10184 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead) | ||||
10185 | CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree) | ||||
10186 | |||||
10187 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack) | ||||
10188 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability) | ||||
10189 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior) | ||||
10190 | CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAFunctionReachability) | ||||
10191 | |||||
10192 | CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior) | ||||
10193 | |||||
10194 | #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION | ||||
10195 | #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION | ||||
10196 | #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION | ||||
10197 | #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION | ||||
10198 | #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION | ||||
10199 | #undef SWITCH_PK_CREATE | ||||
10200 | #undef SWITCH_PK_INV |