Bug Summary

File:build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Warning:line 6020, column 9
Value stored to 'HasChanged' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name AttributorAttributes.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/build-llvm -resource-dir /usr/lib/llvm-15/lib/clang/15.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Transforms/IPO -I /build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/llvm/lib/Transforms/IPO -I include -I /build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-15/lib/clang/15.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-03-01-235733-118493-1 -x c++ /build/llvm-toolchain-snapshot-15~++20220301100735+026fe5ffc352/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
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
54using namespace llvm;
55
56#define DEBUG_TYPE"attributor" "attributor"
57
58static cl::opt<bool> ManifestInternal(
59 "attributor-manifest-internal", cl::Hidden,
60 cl::desc("Manifest Attributor internal string attributes."),
61 cl::init(false));
62
63static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
64 cl::Hidden);
65
66template <>
67unsigned llvm::PotentialConstantIntValuesState::MaxPotentialValues = 0;
68
69static 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
76static 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
82STATISTIC(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.
130namespace 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
136PIPE_OPERATOR(AAIsDead)
137PIPE_OPERATOR(AANoUnwind)
138PIPE_OPERATOR(AANoSync)
139PIPE_OPERATOR(AANoRecurse)
140PIPE_OPERATOR(AAWillReturn)
141PIPE_OPERATOR(AANoReturn)
142PIPE_OPERATOR(AAReturnedValues)
143PIPE_OPERATOR(AANonNull)
144PIPE_OPERATOR(AANoAlias)
145PIPE_OPERATOR(AADereferenceable)
146PIPE_OPERATOR(AAAlign)
147PIPE_OPERATOR(AANoCapture)
148PIPE_OPERATOR(AAValueSimplify)
149PIPE_OPERATOR(AANoFree)
150PIPE_OPERATOR(AAHeapToStack)
151PIPE_OPERATOR(AAReachability)
152PIPE_OPERATOR(AAMemoryBehavior)
153PIPE_OPERATOR(AAMemoryLocation)
154PIPE_OPERATOR(AAValueConstantRange)
155PIPE_OPERATOR(AAPrivatizablePtr)
156PIPE_OPERATOR(AAUndefinedBehavior)
157PIPE_OPERATOR(AAPotentialValues)
158PIPE_OPERATOR(AANoUndef)
159PIPE_OPERATOR(AACallEdges)
160PIPE_OPERATOR(AAFunctionReachability)
161PIPE_OPERATOR(AAPointerInfo)
162PIPE_OPERATOR(AAAssumptionInfo)
163
164#undef PIPE_OPERATOR
165
166template <>
167ChangeStatus 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.
180static 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.
212static 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.
261template <typename StateTy>
262static 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
421bool 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
443const 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
468static const Value *
469getMinimalBaseOfPointer(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.
482template <typename AAType, typename StateType = typename AAType::StateType>
483static 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
523namespace {
524/// Helper class for generic deduction: return value -> returned position.
525template <typename AAType, typename BaseType,
526 typename StateType = typename BaseType::StateType,
527 bool PropagateCallBaseContext = false>
528struct 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.
546template <typename AAType, typename StateType = typename AAType::StateType>
547static 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.
594template <typename AAType, typename BaseType,
595 typename StateType = typename AAType::StateType>
596bool 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.
624template <typename AAType, typename BaseType,
625 typename StateType = typename AAType::StateType,
626 bool BridgeCallBaseContext = false>
627struct 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.
651template <typename AAType, typename BaseType,
652 typename StateType = typename BaseType::StateType,
653 bool IntroduceCallBaseContext = false>
654struct 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.
685template <class AAType, typename StateType = typename AAType::StateType>
686static 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///
712template <class AAType, typename StateType = typename AAType::StateType>
713static 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
797namespace llvm {
798namespace AA {
799namespace PointerInfo {
800
801struct State;
802
803} // namespace PointerInfo
804} // namespace AA
805
806/// Helper for AA::PointerInfo::Acccess DenseMap/Set usage.
807template <>
808struct 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.
817template <>
818struct 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
823struct 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///{
837inline llvm::AccessAsInstructionInfo::Access
838llvm::AccessAsInstructionInfo::getEmptyKey() {
839 return Access(Base::getEmptyKey(), nullptr, AAPointerInfo::AK_READ, nullptr);
840}
841inline llvm::AccessAsInstructionInfo::Access
842llvm::AccessAsInstructionInfo::getTombstoneKey() {
843 return Access(Base::getTombstoneKey(), nullptr, AAPointerInfo::AK_READ,
844 nullptr);
845}
846unsigned llvm::AccessAsInstructionInfo::getHashValue(
847 const llvm::AccessAsInstructionInfo::Access &A) {
848 return Base::getHashValue(A.getRemoteInst());
849}
850bool llvm::AccessAsInstructionInfo::isEqual(
851 const llvm::AccessAsInstructionInfo::Access &LHS,
852 const llvm::AccessAsInstructionInfo::Access &RHS) {
853 return LHS.getRemoteInst() == RHS.getRemoteInst();
854}
855inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
856llvm::DenseMapInfo<AAPointerInfo::Access>::getEmptyKey() {
857 return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_READ,
858 nullptr);
859}
860inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
861llvm::DenseMapInfo<AAPointerInfo::Access>::getTombstoneKey() {
862 return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_WRITE,
863 nullptr);
864}
865
866unsigned 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
876bool 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.
884struct 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
972protected:
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
1049private:
1050 /// State to track fixpoint and validity.
1051 BooleanState BS;
1052};
1053
1054struct 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
1293struct 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 != OffsetAndSize::Unknown && Size == OffsetAndSize::Unknown &&
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
1527struct 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
1542struct 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
1559struct 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 = dyn_cast_or_null<MemIntrinsic>(getCtxI())) {
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
1610struct 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
1622struct 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
1657struct 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.
1666struct 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.
1701class 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
1714public:
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
1810ChangeStatus 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
1838const std::string AAReturnedValuesImpl::getAsStr() const {
1839 return (isAtFixpoint() ? "returns(#" : "may-return(#") +
1840 (isValidState() ? std::to_string(getNumReturnValues()) : "?") + ")";
1841}
1842
1843Optional<Value *>
1844AAReturnedValuesImpl::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
1864bool 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
1881ChangeStatus 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
1911struct 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.
1920struct 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
1945struct 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
1956bool 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.
1992bool AANoSync::isNoSyncIntrinsic(const Instruction *I) {
1993 if (auto *MI = dyn_cast<MemIntrinsic>(I))
1994 return !MI->isVolatile();
1995 return false;
1996}
1997
1998ChangeStatus 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
2024struct 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.
2033struct 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
2063struct 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
2091struct 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.
2100struct 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.
2129struct 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.
2181struct 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.
2190struct 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.
2213struct 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.
2234struct 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 ------------------------
2246static 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
2324struct 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.
2388struct 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.
2434struct 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.
2449struct 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
2458struct 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.
2467struct 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
2478struct 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
2487struct 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.
2526struct 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
2556struct 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
2826protected:
2827 /// A set of all live instructions _known_ to cause UB.
2828 SmallPtrSet<Instruction *, 8> KnownUBInsts;
2829
2830private:
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
2868struct 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.
2886static 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
2913struct 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
2973struct 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.
2991struct 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
3024struct 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
3043struct 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
3053struct 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.
3065struct 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.
3115struct 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
3168struct 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.
3352struct 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.
3399struct 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
3429struct 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
3505struct 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
3593struct 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
3622struct 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
3662struct 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
3709private:
3710 bool IsAssumedSideEffectFree = true;
3711};
3712
3713struct 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
3758struct 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
3926static bool
3927identifyAliveSuccessors(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
3943static bool
3944identifyAliveSuccessors(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
3968static bool
3969identifyAliveSuccessors(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
3993static bool
3994identifyAliveSuccessors(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
4018ChangeStatus 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.
4125struct 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
4150struct 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.
4255struct 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.
4333struct 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
4346struct 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.
4361struct 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.
4372struct 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
4387static 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
4447struct 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.
4546struct 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.
4601struct 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.
4619struct 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
4638struct 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.
4676struct 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 ----------------------------
4695struct 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
4723struct 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.
4732struct 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.
4766struct 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.
4875struct 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
5020private:
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
5040ChangeStatus 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.
5129struct 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.
5138struct 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.
5169struct 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.
5180struct 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.
5201struct 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
5220bool 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
5237struct 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
5423struct 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
5511struct AAValueSimplifyReturned : AAValueSimplifyImpl {
5512 AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A)
5513 : AAValueSimplifyImpl(IRP, A) {}
5514
5515 /// See AAValueSimplify::getAssumedSimplifiedValue()