Bug Summary

File:llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Warning:line 5893, 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-14~++20220125101009+ceec4383681c/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.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-14~++20220125101009+ceec4383681c/llvm/lib/Transforms/IPO -I include -I /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/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-14/lib/clang/14.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-14~++20220125101009+ceec4383681c/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -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-14~++20220125101009+ceec4383681c/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -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-01-25-232935-20746-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/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/SCCIterator.h"
18#include "llvm/ADT/SetOperations.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/Analysis/AliasAnalysis.h"
22#include "llvm/Analysis/AssumeBundleQueries.h"
23#include "llvm/Analysis/AssumptionCache.h"
24#include "llvm/Analysis/CaptureTracking.h"
25#include "llvm/Analysis/InstructionSimplify.h"
26#include "llvm/Analysis/LazyValueInfo.h"
27#include "llvm/Analysis/MemoryBuiltins.h"
28#include "llvm/Analysis/OptimizationRemarkEmitter.h"
29#include "llvm/Analysis/ScalarEvolution.h"
30#include "llvm/Analysis/TargetTransformInfo.h"
31#include "llvm/Analysis/ValueTracking.h"
32#include "llvm/IR/Assumptions.h"
33#include "llvm/IR/Constants.h"
34#include "llvm/IR/IRBuilder.h"
35#include "llvm/IR/Instruction.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/NoFolder.h"
39#include "llvm/Support/Alignment.h"
40#include "llvm/Support/Casting.h"
41#include "llvm/Support/CommandLine.h"
42#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/FileSystem.h"
44#include "llvm/Support/raw_ostream.h"
45#include "llvm/Transforms/IPO/ArgumentPromotion.h"
46#include "llvm/Transforms/Utils/Local.h"
47#include <cassert>
48
49using namespace llvm;
50
51#define DEBUG_TYPE"attributor" "attributor"
52
53static cl::opt<bool> ManifestInternal(
54 "attributor-manifest-internal", cl::Hidden,
55 cl::desc("Manifest Attributor internal string attributes."),
56 cl::init(false));
57
58static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
59 cl::Hidden);
60
61template <>
62unsigned llvm::PotentialConstantIntValuesState::MaxPotentialValues = 0;
63
64static cl::opt<unsigned, true> MaxPotentialValues(
65 "attributor-max-potential-values", cl::Hidden,
66 cl::desc("Maximum number of potential values to be "
67 "tracked for each position."),
68 cl::location(llvm::PotentialConstantIntValuesState::MaxPotentialValues),
69 cl::init(7));
70
71STATISTIC(NumAAs, "Number of abstract attributes created")static llvm::Statistic NumAAs = {"attributor", "NumAAs", "Number of abstract attributes created"
}
;
72
73// Some helper macros to deal with statistics tracking.
74//
75// Usage:
76// For simple IR attribute tracking overload trackStatistics in the abstract
77// attribute and choose the right STATS_DECLTRACK_********* macro,
78// e.g.,:
79// void trackStatistics() const override {
80// STATS_DECLTRACK_ARG_ATTR(returned)
81// }
82// If there is a single "increment" side one can use the macro
83// STATS_DECLTRACK with a custom message. If there are multiple increment
84// sides, STATS_DECL and STATS_TRACK can also be used separately.
85//
86#define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)("Number of " "TYPE" " marked '" "NAME" "'") \
87 ("Number of " #TYPE " marked '" #NAME "'")
88#define BUILD_STAT_NAME(NAME, TYPE)NumIRTYPE_NAME NumIR##TYPE##_##NAME
89#define STATS_DECL_(NAME, MSG)static llvm::Statistic NAME = {"attributor", "NAME", MSG}; STATISTIC(NAME, MSG)static llvm::Statistic NAME = {"attributor", "NAME", MSG};
90#define STATS_DECL(NAME, TYPE, MSG)static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME"
, MSG};;
\
91 STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG)static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME"
, MSG};
;
92#define STATS_TRACK(NAME, TYPE)++(NumIRTYPE_NAME); ++(BUILD_STAT_NAME(NAME, TYPE)NumIRTYPE_NAME);
93#define STATS_DECLTRACK(NAME, TYPE, MSG){ static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME"
, MSG};; ++(NumIRTYPE_NAME); }
\
94 { \
95 STATS_DECL(NAME, TYPE, MSG)static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME"
, MSG};;
\
96 STATS_TRACK(NAME, TYPE)++(NumIRTYPE_NAME); \
97 }
98#define STATS_DECLTRACK_ARG_ATTR(NAME){ static llvm::Statistic NumIRArguments_NAME = {"attributor",
"NumIRArguments_NAME", ("Number of " "arguments" " marked '"
"NAME" "'")};; ++(NumIRArguments_NAME); }
\
99 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); }
100#define STATS_DECLTRACK_CSARG_ATTR(NAME){ static llvm::Statistic NumIRCSArguments_NAME = {"attributor"
, "NumIRCSArguments_NAME", ("Number of " "call site arguments"
" marked '" "NAME" "'")};; ++(NumIRCSArguments_NAME); }
\
101 STATS_DECLTRACK(NAME, CSArguments, \{ static llvm::Statistic NumIRCSArguments_NAME = {"attributor"
, "NumIRCSArguments_NAME", ("Number of " "call site arguments"
" marked '" "NAME" "'")};; ++(NumIRCSArguments_NAME); }
102 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); }
103#define STATS_DECLTRACK_FN_ATTR(NAME){ static llvm::Statistic NumIRFunction_NAME = {"attributor", "NumIRFunction_NAME"
, ("Number of " "functions" " marked '" "NAME" "'")};; ++(NumIRFunction_NAME
); }
\
104 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
); }
105#define STATS_DECLTRACK_CS_ATTR(NAME){ static llvm::Statistic NumIRCS_NAME = {"attributor", "NumIRCS_NAME"
, ("Number of " "call site" " marked '" "NAME" "'")};; ++(NumIRCS_NAME
); }
\
106 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
); }
107#define STATS_DECLTRACK_FNRET_ATTR(NAME){ static llvm::Statistic NumIRFunctionReturn_NAME = {"attributor"
, "NumIRFunctionReturn_NAME", ("Number of " "function returns"
" marked '" "NAME" "'")};; ++(NumIRFunctionReturn_NAME); }
\
108 STATS_DECLTRACK(NAME, FunctionReturn, \{ static llvm::Statistic NumIRFunctionReturn_NAME = {"attributor"
, "NumIRFunctionReturn_NAME", ("Number of " "function returns"
" marked '" "NAME" "'")};; ++(NumIRFunctionReturn_NAME); }
109 BUILD_STAT_MSG_IR_ATTR(function returns, NAME)){ static llvm::Statistic NumIRFunctionReturn_NAME = {"attributor"
, "NumIRFunctionReturn_NAME", ("Number of " "function returns"
" marked '" "NAME" "'")};; ++(NumIRFunctionReturn_NAME); }
110#define STATS_DECLTRACK_CSRET_ATTR(NAME){ static llvm::Statistic NumIRCSReturn_NAME = {"attributor", "NumIRCSReturn_NAME"
, ("Number of " "call site returns" " marked '" "NAME" "'")};
; ++(NumIRCSReturn_NAME); }
\
111 STATS_DECLTRACK(NAME, CSReturn, \{ static llvm::Statistic NumIRCSReturn_NAME = {"attributor", "NumIRCSReturn_NAME"
, ("Number of " "call site returns" " marked '" "NAME" "'")};
; ++(NumIRCSReturn_NAME); }
112 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); }
113#define STATS_DECLTRACK_FLOATING_ATTR(NAME){ static llvm::Statistic NumIRFloating_NAME = {"attributor", "NumIRFloating_NAME"
, ("Number of floating values known to be '" "NAME" "'")};; ++
(NumIRFloating_NAME); }
\
114 STATS_DECLTRACK(NAME, Floating, \{ static llvm::Statistic NumIRFloating_NAME = {"attributor", "NumIRFloating_NAME"
, ("Number of floating values known to be '" #NAME "'")};; ++
(NumIRFloating_NAME); }
115 ("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); }
116
117// Specialization of the operator<< for abstract attributes subclasses. This
118// disambiguates situations where multiple operators are applicable.
119namespace llvm {
120#define PIPE_OPERATOR(CLASS) \
121 raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \
122 return OS << static_cast<const AbstractAttribute &>(AA); \
123 }
124
125PIPE_OPERATOR(AAIsDead)
126PIPE_OPERATOR(AANoUnwind)
127PIPE_OPERATOR(AANoSync)
128PIPE_OPERATOR(AANoRecurse)
129PIPE_OPERATOR(AAWillReturn)
130PIPE_OPERATOR(AANoReturn)
131PIPE_OPERATOR(AAReturnedValues)
132PIPE_OPERATOR(AANonNull)
133PIPE_OPERATOR(AANoAlias)
134PIPE_OPERATOR(AADereferenceable)
135PIPE_OPERATOR(AAAlign)
136PIPE_OPERATOR(AANoCapture)
137PIPE_OPERATOR(AAValueSimplify)
138PIPE_OPERATOR(AANoFree)
139PIPE_OPERATOR(AAHeapToStack)
140PIPE_OPERATOR(AAReachability)
141PIPE_OPERATOR(AAMemoryBehavior)
142PIPE_OPERATOR(AAMemoryLocation)
143PIPE_OPERATOR(AAValueConstantRange)
144PIPE_OPERATOR(AAPrivatizablePtr)
145PIPE_OPERATOR(AAUndefinedBehavior)
146PIPE_OPERATOR(AAPotentialValues)
147PIPE_OPERATOR(AANoUndef)
148PIPE_OPERATOR(AACallEdges)
149PIPE_OPERATOR(AAFunctionReachability)
150PIPE_OPERATOR(AAPointerInfo)
151PIPE_OPERATOR(AAAssumptionInfo)
152
153#undef PIPE_OPERATOR
154
155template <>
156ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
157 const DerefState &R) {
158 ChangeStatus CS0 =
159 clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
160 ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
161 return CS0 | CS1;
162}
163
164} // namespace llvm
165
166/// Get pointer operand of memory accessing instruction. If \p I is
167/// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
168/// is set to false and the instruction is volatile, return nullptr.
169static const Value *getPointerOperand(const Instruction *I,
170 bool AllowVolatile) {
171 if (!AllowVolatile && I->isVolatile())
172 return nullptr;
173
174 if (auto *LI = dyn_cast<LoadInst>(I)) {
175 return LI->getPointerOperand();
176 }
177
178 if (auto *SI = dyn_cast<StoreInst>(I)) {
179 return SI->getPointerOperand();
180 }
181
182 if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
183 return CXI->getPointerOperand();
184 }
185
186 if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
187 return RMWI->getPointerOperand();
188 }
189
190 return nullptr;
191}
192
193/// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
194/// advanced by \p Offset bytes. To aid later analysis the method tries to build
195/// getelement pointer instructions that traverse the natural type of \p Ptr if
196/// possible. If that fails, the remaining offset is adjusted byte-wise, hence
197/// through a cast to i8*.
198///
199/// TODO: This could probably live somewhere more prominantly if it doesn't
200/// already exist.
201static Value *constructPointer(Type *ResTy, Type *PtrElemTy, Value *Ptr,
202 int64_t Offset, IRBuilder<NoFolder> &IRB,
203 const DataLayout &DL) {
204 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", 204, __extension__
__PRETTY_FUNCTION__))
;
205 LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offsetdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "Construct pointer: " <<
*Ptr << " + " << Offset << "-bytes as " <<
*ResTy << "\n"; } } while (false)
206 << "-bytes as " << *ResTy << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "Construct pointer: " <<
*Ptr << " + " << Offset << "-bytes as " <<
*ResTy << "\n"; } } while (false)
;
207
208 if (Offset) {
209 Type *Ty = PtrElemTy;
210 APInt IntOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), Offset);
211 SmallVector<APInt> IntIndices = DL.getGEPIndicesForOffset(Ty, IntOffset);
212
213 SmallVector<Value *, 4> ValIndices;
214 std::string GEPName = Ptr->getName().str();
215 for (const APInt &Index : IntIndices) {
216 ValIndices.push_back(IRB.getInt(Index));
217 GEPName += "." + std::to_string(Index.getZExtValue());
218 }
219
220 // Create a GEP for the indices collected above.
221 Ptr = IRB.CreateGEP(PtrElemTy, Ptr, ValIndices, GEPName);
222
223 // If an offset is left we use byte-wise adjustment.
224 if (IntOffset != 0) {
225 Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
226 Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt(IntOffset),
227 GEPName + ".b" + Twine(IntOffset.getZExtValue()));
228 }
229 }
230
231 // Ensure the result has the requested type.
232 Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast");
233
234 LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "Constructed pointer: " <<
*Ptr << "\n"; } } while (false)
;
235 return Ptr;
236}
237
238/// Recursively visit all values that might become \p IRP at some point. This
239/// will be done by looking through cast instructions, selects, phis, and calls
240/// with the "returned" attribute. Once we cannot look through the value any
241/// further, the callback \p VisitValueCB is invoked and passed the current
242/// value, the \p State, and a flag to indicate if we stripped anything.
243/// Stripped means that we unpacked the value associated with \p IRP at least
244/// once. Note that the value used for the callback may still be the value
245/// associated with \p IRP (due to PHIs). To limit how much effort is invested,
246/// we will never visit more values than specified by \p MaxValues.
247template <typename StateTy>
248static bool genericValueTraversal(
249 Attributor &A, IRPosition IRP, const AbstractAttribute &QueryingAA,
250 StateTy &State,
251 function_ref<bool(Value &, const Instruction *, StateTy &, bool)>
252 VisitValueCB,
253 const Instruction *CtxI, bool UseValueSimplify = true, int MaxValues = 16,
254 function_ref<Value *(Value *)> StripCB = nullptr) {
255
256 const AAIsDead *LivenessAA = nullptr;
257 if (IRP.getAnchorScope())
258 LivenessAA = &A.getAAFor<AAIsDead>(
259 QueryingAA,
260 IRPosition::function(*IRP.getAnchorScope(), IRP.getCallBaseContext()),
261 DepClassTy::NONE);
262 bool AnyDead = false;
263
264 Value *InitialV = &IRP.getAssociatedValue();
265 using Item = std::pair<Value *, const Instruction *>;
266 SmallSet<Item, 16> Visited;
267 SmallVector<Item, 16> Worklist;
268 Worklist.push_back({InitialV, CtxI});
269
270 int Iteration = 0;
271 do {
272 Item I = Worklist.pop_back_val();
273 Value *V = I.first;
274 CtxI = I.second;
275 if (StripCB)
276 V = StripCB(V);
277
278 // Check if we should process the current value. To prevent endless
279 // recursion keep a record of the values we followed!
280 if (!Visited.insert(I).second)
281 continue;
282
283 // Make sure we limit the compile time for complex expressions.
284 if (Iteration++ >= MaxValues)
285 return false;
286
287 // Explicitly look through calls with a "returned" attribute if we do
288 // not have a pointer as stripPointerCasts only works on them.
289 Value *NewV = nullptr;
290 if (V->getType()->isPointerTy()) {
291 NewV = V->stripPointerCasts();
292 } else {
293 auto *CB = dyn_cast<CallBase>(V);
294 if (CB && CB->getCalledFunction()) {
295 for (Argument &Arg : CB->getCalledFunction()->args())
296 if (Arg.hasReturnedAttr()) {
297 NewV = CB->getArgOperand(Arg.getArgNo());
298 break;
299 }
300 }
301 }
302 if (NewV && NewV != V) {
303 Worklist.push_back({NewV, CtxI});
304 continue;
305 }
306
307 // Look through select instructions, visit assumed potential values.
308 if (auto *SI = dyn_cast<SelectInst>(V)) {
309 bool UsedAssumedInformation = false;
310 Optional<Constant *> C = A.getAssumedConstant(
311 *SI->getCondition(), QueryingAA, UsedAssumedInformation);
312 bool NoValueYet = !C.hasValue();
313 if (NoValueYet || isa_and_nonnull<UndefValue>(*C))
314 continue;
315 if (auto *CI = dyn_cast_or_null<ConstantInt>(*C)) {
316 if (CI->isZero())
317 Worklist.push_back({SI->getFalseValue(), CtxI});
318 else
319 Worklist.push_back({SI->getTrueValue(), CtxI});
320 continue;
321 }
322 // We could not simplify the condition, assume both values.(
323 Worklist.push_back({SI->getTrueValue(), CtxI});
324 Worklist.push_back({SI->getFalseValue(), CtxI});
325 continue;
326 }
327
328 // Look through phi nodes, visit all live operands.
329 if (auto *PHI = dyn_cast<PHINode>(V)) {
330 assert(LivenessAA &&(static_cast <bool> (LivenessAA && "Expected liveness in the presence of instructions!"
) ? void (0) : __assert_fail ("LivenessAA && \"Expected liveness in the presence of instructions!\""
, "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 331, __extension__
__PRETTY_FUNCTION__))
331 "Expected liveness in the presence of instructions!")(static_cast <bool> (LivenessAA && "Expected liveness in the presence of instructions!"
) ? void (0) : __assert_fail ("LivenessAA && \"Expected liveness in the presence of instructions!\""
, "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 331, __extension__
__PRETTY_FUNCTION__))
;
332 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
333 BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
334 bool UsedAssumedInformation = false;
335 if (A.isAssumedDead(*IncomingBB->getTerminator(), &QueryingAA,
336 LivenessAA, UsedAssumedInformation,
337 /* CheckBBLivenessOnly */ true)) {
338 AnyDead = true;
339 continue;
340 }
341 Worklist.push_back(
342 {PHI->getIncomingValue(u), IncomingBB->getTerminator()});
343 }
344 continue;
345 }
346
347 if (UseValueSimplify && !isa<Constant>(V)) {
348 bool UsedAssumedInformation = false;
349 Optional<Value *> SimpleV =
350 A.getAssumedSimplified(*V, QueryingAA, UsedAssumedInformation);
351 if (!SimpleV.hasValue())
352 continue;
353 if (!SimpleV.getValue())
354 return false;
355 Value *NewV = SimpleV.getValue();
356 if (NewV != V) {
357 Worklist.push_back({NewV, CtxI});
358 continue;
359 }
360 }
361
362 // Once a leaf is reached we inform the user through the callback.
363 if (!VisitValueCB(*V, CtxI, State, Iteration > 1))
364 return false;
365 } while (!Worklist.empty());
366
367 // If we actually used liveness information so we have to record a dependence.
368 if (AnyDead)
369 A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
370
371 // All values have been visited.
372 return true;
373}
374
375bool AA::getAssumedUnderlyingObjects(Attributor &A, const Value &Ptr,
376 SmallVectorImpl<Value *> &Objects,
377 const AbstractAttribute &QueryingAA,
378 const Instruction *CtxI) {
379 auto StripCB = [&](Value *V) { return getUnderlyingObject(V); };
380 SmallPtrSet<Value *, 8> SeenObjects;
381 auto VisitValueCB = [&SeenObjects](Value &Val, const Instruction *,
382 SmallVectorImpl<Value *> &Objects,
383 bool) -> bool {
384 if (SeenObjects.insert(&Val).second)
385 Objects.push_back(&Val);
386 return true;
387 };
388 if (!genericValueTraversal<decltype(Objects)>(
389 A, IRPosition::value(Ptr), QueryingAA, Objects, VisitValueCB, CtxI,
390 true, 32, StripCB))
391 return false;
392 return true;
393}
394
395const Value *stripAndAccumulateMinimalOffsets(
396 Attributor &A, const AbstractAttribute &QueryingAA, const Value *Val,
397 const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
398 bool UseAssumed = false) {
399
400 auto AttributorAnalysis = [&](Value &V, APInt &ROffset) -> bool {
401 const IRPosition &Pos = IRPosition::value(V);
402 // Only track dependence if we are going to use the assumed info.
403 const AAValueConstantRange &ValueConstantRangeAA =
404 A.getAAFor<AAValueConstantRange>(QueryingAA, Pos,
405 UseAssumed ? DepClassTy::OPTIONAL
406 : DepClassTy::NONE);
407 ConstantRange Range = UseAssumed ? ValueConstantRangeAA.getAssumed()
408 : ValueConstantRangeAA.getKnown();
409 // We can only use the lower part of the range because the upper part can
410 // be higher than what the value can really be.
411 ROffset = Range.getSignedMin();
412 return true;
413 };
414
415 return Val->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds,
416 /* AllowInvariant */ false,
417 AttributorAnalysis);
418}
419
420static const Value *
421getMinimalBaseOfPointer(Attributor &A, const AbstractAttribute &QueryingAA,
422 const Value *Ptr, int64_t &BytesOffset,
423 const DataLayout &DL, bool AllowNonInbounds = false) {
424 APInt OffsetAPInt(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
425 const Value *Base = stripAndAccumulateMinimalOffsets(
426 A, QueryingAA, Ptr, DL, OffsetAPInt, AllowNonInbounds);
427
428 BytesOffset = OffsetAPInt.getSExtValue();
429 return Base;
430}
431
432/// Clamp the information known for all returned values of a function
433/// (identified by \p QueryingAA) into \p S.
434template <typename AAType, typename StateType = typename AAType::StateType>
435static void clampReturnedValueStates(
436 Attributor &A, const AAType &QueryingAA, StateType &S,
437 const IRPosition::CallBaseContext *CBContext = nullptr) {
438 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)
439 << QueryingAA << " into " << S << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[Attributor] Clamp return value states for "
<< QueryingAA << " into " << S << "\n"
; } } while (false)
;
440
441 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", 446, __extension__
__PRETTY_FUNCTION__))
442 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", 446, __extension__
__PRETTY_FUNCTION__))
443 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", 446, __extension__
__PRETTY_FUNCTION__))
444 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", 446, __extension__
__PRETTY_FUNCTION__))
445 "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", 446, __extension__
__PRETTY_FUNCTION__))
446 "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", 446, __extension__
__PRETTY_FUNCTION__))
;
447
448 // Use an optional state as there might not be any return values and we want
449 // to join (IntegerState::operator&) the state of all there are.
450 Optional<StateType> T;
451
452 // Callback for each possibly returned value.
453 auto CheckReturnValue = [&](Value &RV) -> bool {
454 const IRPosition &RVPos = IRPosition::value(RV, CBContext);
455 const AAType &AA =
456 A.getAAFor<AAType>(QueryingAA, RVPos, DepClassTy::REQUIRED);
457 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)
458 << " @ " << RVPos << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[Attributor] RV: " <<
RV << " AA: " << AA.getAsStr() << " @ " <<
RVPos << "\n"; } } while (false)
;
459 const StateType &AAS = AA.getState();
460 if (T.hasValue())
461 *T &= AAS;
462 else
463 T = AAS;
464 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)
465 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[Attributor] AA State: " <<
AAS << " RV State: " << T << "\n"; } } while
(false)
;
466 return T->isValidState();
467 };
468
469 if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
470 S.indicatePessimisticFixpoint();
471 else if (T.hasValue())
472 S ^= *T;
473}
474
475namespace {
476/// Helper class for generic deduction: return value -> returned position.
477template <typename AAType, typename BaseType,
478 typename StateType = typename BaseType::StateType,
479 bool PropagateCallBaseContext = false>
480struct AAReturnedFromReturnedValues : public BaseType {
481 AAReturnedFromReturnedValues(const IRPosition &IRP, Attributor &A)
482 : BaseType(IRP, A) {}
483
484 /// See AbstractAttribute::updateImpl(...).
485 ChangeStatus updateImpl(Attributor &A) override {
486 StateType S(StateType::getBestState(this->getState()));
487 clampReturnedValueStates<AAType, StateType>(
488 A, *this, S,
489 PropagateCallBaseContext ? this->getCallBaseContext() : nullptr);
490 // TODO: If we know we visited all returned values, thus no are assumed
491 // dead, we can take the known information from the state T.
492 return clampStateAndIndicateChange<StateType>(this->getState(), S);
493 }
494};
495
496/// Clamp the information known at all call sites for a given argument
497/// (identified by \p QueryingAA) into \p S.
498template <typename AAType, typename StateType = typename AAType::StateType>
499static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
500 StateType &S) {
501 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)
502 << 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)
;
503
504 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", 506, __extension__
__PRETTY_FUNCTION__))
505 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", 506, __extension__
__PRETTY_FUNCTION__))
506 "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", 506, __extension__
__PRETTY_FUNCTION__))
;
507
508 // Use an optional state as there might not be any return values and we want
509 // to join (IntegerState::operator&) the state of all there are.
510 Optional<StateType> T;
511
512 // The argument number which is also the call site argument number.
513 unsigned ArgNo = QueryingAA.getIRPosition().getCallSiteArgNo();
514
515 auto CallSiteCheck = [&](AbstractCallSite ACS) {
516 const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
517 // Check if a coresponding argument was found or if it is on not associated
518 // (which can happen for callback calls).
519 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
520 return false;
521
522 const AAType &AA =
523 A.getAAFor<AAType>(QueryingAA, ACSArgPos, DepClassTy::REQUIRED);
524 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)
525 << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[Attributor] ACS: " <<
*ACS.getInstruction() << " AA: " << AA.getAsStr(
) << " @" << ACSArgPos << "\n"; } } while (
false)
;
526 const StateType &AAS = AA.getState();
527 if (T.hasValue())
528 *T &= AAS;
529 else
530 T = AAS;
531 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)
532 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[Attributor] AA State: " <<
AAS << " CSA State: " << T << "\n"; } } while
(false)
;
533 return T->isValidState();
534 };
535
536 bool AllCallSitesKnown;
537 if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
538 AllCallSitesKnown))
539 S.indicatePessimisticFixpoint();
540 else if (T.hasValue())
541 S ^= *T;
542}
543
544/// This function is the bridge between argument position and the call base
545/// context.
546template <typename AAType, typename BaseType,
547 typename StateType = typename AAType::StateType>
548bool getArgumentStateFromCallBaseContext(Attributor &A,
549 BaseType &QueryingAttribute,
550 IRPosition &Pos, StateType &State) {
551 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", 552, __extension__
__PRETTY_FUNCTION__))
552 "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", 552, __extension__
__PRETTY_FUNCTION__))
;
553 const CallBase *CBContext = Pos.getCallBaseContext();
554 if (!CBContext)
555 return false;
556
557 int ArgNo = Pos.getCallSiteArgNo();
558 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", 558, __extension__
__PRETTY_FUNCTION__))
;
559
560 const auto &AA = A.getAAFor<AAType>(
561 QueryingAttribute, IRPosition::callsite_argument(*CBContext, ArgNo),
562 DepClassTy::REQUIRED);
563 const StateType &CBArgumentState =
564 static_cast<const StateType &>(AA.getState());
565
566 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)
567 << "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)
568 << "\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)
;
569
570 // NOTE: If we want to do call site grouping it should happen here.
571 State ^= CBArgumentState;
572 return true;
573}
574
575/// Helper class for generic deduction: call site argument -> argument position.
576template <typename AAType, typename BaseType,
577 typename StateType = typename AAType::StateType,
578 bool BridgeCallBaseContext = false>
579struct AAArgumentFromCallSiteArguments : public BaseType {
580 AAArgumentFromCallSiteArguments(const IRPosition &IRP, Attributor &A)
581 : BaseType(IRP, A) {}
582
583 /// See AbstractAttribute::updateImpl(...).
584 ChangeStatus updateImpl(Attributor &A) override {
585 StateType S = StateType::getBestState(this->getState());
586
587 if (BridgeCallBaseContext) {
588 bool Success =
589 getArgumentStateFromCallBaseContext<AAType, BaseType, StateType>(
590 A, *this, this->getIRPosition(), S);
591 if (Success)
592 return clampStateAndIndicateChange<StateType>(this->getState(), S);
593 }
594 clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
595
596 // TODO: If we know we visited all incoming values, thus no are assumed
597 // dead, we can take the known information from the state T.
598 return clampStateAndIndicateChange<StateType>(this->getState(), S);
599 }
600};
601
602/// Helper class for generic replication: function returned -> cs returned.
603template <typename AAType, typename BaseType,
604 typename StateType = typename BaseType::StateType,
605 bool IntroduceCallBaseContext = false>
606struct AACallSiteReturnedFromReturned : public BaseType {
607 AACallSiteReturnedFromReturned(const IRPosition &IRP, Attributor &A)
608 : BaseType(IRP, A) {}
609
610 /// See AbstractAttribute::updateImpl(...).
611 ChangeStatus updateImpl(Attributor &A) override {
612 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", 615, __extension__
__PRETTY_FUNCTION__))
613 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", 615, __extension__
__PRETTY_FUNCTION__))
614 "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", 615, __extension__
__PRETTY_FUNCTION__))
615 "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", 615, __extension__
__PRETTY_FUNCTION__))
;
616 auto &S = this->getState();
617
618 const Function *AssociatedFunction =
619 this->getIRPosition().getAssociatedFunction();
620 if (!AssociatedFunction)
621 return S.indicatePessimisticFixpoint();
622
623 CallBase &CBContext = static_cast<CallBase &>(this->getAnchorValue());
624 if (IntroduceCallBaseContext)
625 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)
626 << CBContext << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[Attributor] Introducing call base context:"
<< CBContext << "\n"; } } while (false)
;
627
628 IRPosition FnPos = IRPosition::returned(
629 *AssociatedFunction, IntroduceCallBaseContext ? &CBContext : nullptr);
630 const AAType &AA = A.getAAFor<AAType>(*this, FnPos, DepClassTy::REQUIRED);
631 return clampStateAndIndicateChange(S, AA.getState());
632 }
633};
634} // namespace
635
636/// Helper function to accumulate uses.
637template <class AAType, typename StateType = typename AAType::StateType>
638static void followUsesInContext(AAType &AA, Attributor &A,
639 MustBeExecutedContextExplorer &Explorer,
640 const Instruction *CtxI,
641 SetVector<const Use *> &Uses,
642 StateType &State) {
643 auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
644 for (unsigned u = 0; u < Uses.size(); ++u) {
645 const Use *U = Uses[u];
646 if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
647 bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
648 if (Found && AA.followUseInMBEC(A, U, UserI, State))
649 for (const Use &Us : UserI->uses())
650 Uses.insert(&Us);
651 }
652 }
653}
654
655/// Use the must-be-executed-context around \p I to add information into \p S.
656/// The AAType class is required to have `followUseInMBEC` method with the
657/// following signature and behaviour:
658///
659/// bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I)
660/// U - Underlying use.
661/// I - The user of the \p U.
662/// Returns true if the value should be tracked transitively.
663///
664template <class AAType, typename StateType = typename AAType::StateType>
665static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S,
666 Instruction &CtxI) {
667
668 // Container for (transitive) uses of the associated value.
669 SetVector<const Use *> Uses;
670 for (const Use &U : AA.getIRPosition().getAssociatedValue().uses())
671 Uses.insert(&U);
672
673 MustBeExecutedContextExplorer &Explorer =
674 A.getInfoCache().getMustBeExecutedContextExplorer();
675
676 followUsesInContext<AAType>(AA, A, Explorer, &CtxI, Uses, S);
677
678 if (S.isAtFixpoint())
679 return;
680
681 SmallVector<const BranchInst *, 4> BrInsts;
682 auto Pred = [&](const Instruction *I) {
683 if (const BranchInst *Br = dyn_cast<BranchInst>(I))
684 if (Br->isConditional())
685 BrInsts.push_back(Br);
686 return true;
687 };
688
689 // Here, accumulate conditional branch instructions in the context. We
690 // explore the child paths and collect the known states. The disjunction of
691 // those states can be merged to its own state. Let ParentState_i be a state
692 // to indicate the known information for an i-th branch instruction in the
693 // context. ChildStates are created for its successors respectively.
694 //
695 // ParentS_1 = ChildS_{1, 1} /\ ChildS_{1, 2} /\ ... /\ ChildS_{1, n_1}
696 // ParentS_2 = ChildS_{2, 1} /\ ChildS_{2, 2} /\ ... /\ ChildS_{2, n_2}
697 // ...
698 // ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m}
699 //
700 // Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m
701 //
702 // FIXME: Currently, recursive branches are not handled. For example, we
703 // can't deduce that ptr must be dereferenced in below function.
704 //
705 // void f(int a, int c, int *ptr) {
706 // if(a)
707 // if (b) {
708 // *ptr = 0;
709 // } else {
710 // *ptr = 1;
711 // }
712 // else {
713 // if (b) {
714 // *ptr = 0;
715 // } else {
716 // *ptr = 1;
717 // }
718 // }
719 // }
720
721 Explorer.checkForAllContext(&CtxI, Pred);
722 for (const BranchInst *Br : BrInsts) {
723 StateType ParentState;
724
725 // The known state of the parent state is a conjunction of children's
726 // known states so it is initialized with a best state.
727 ParentState.indicateOptimisticFixpoint();
728
729 for (const BasicBlock *BB : Br->successors()) {
730 StateType ChildState;
731
732 size_t BeforeSize = Uses.size();
733 followUsesInContext(AA, A, Explorer, &BB->front(), Uses, ChildState);
734
735 // Erase uses which only appear in the child.
736 for (auto It = Uses.begin() + BeforeSize; It != Uses.end();)
737 It = Uses.erase(It);
738
739 ParentState &= ChildState;
740 }
741
742 // Use only known state.
743 S += ParentState;
744 }
745}
746
747/// ------------------------ PointerInfo ---------------------------------------
748
749namespace llvm {
750namespace AA {
751namespace PointerInfo {
752
753/// An access kind description as used by AAPointerInfo.
754struct OffsetAndSize;
755
756struct State;
757
758} // namespace PointerInfo
759} // namespace AA
760
761/// Helper for AA::PointerInfo::Acccess DenseMap/Set usage.
762template <>
763struct DenseMapInfo<AAPointerInfo::Access> : DenseMapInfo<Instruction *> {
764 using Access = AAPointerInfo::Access;
765 static inline Access getEmptyKey();
766 static inline Access getTombstoneKey();
767 static unsigned getHashValue(const Access &A);
768 static bool isEqual(const Access &LHS, const Access &RHS);
769};
770
771/// Helper that allows OffsetAndSize as a key in a DenseMap.
772template <>
773struct DenseMapInfo<AA::PointerInfo ::OffsetAndSize>
774 : DenseMapInfo<std::pair<int64_t, int64_t>> {};
775
776/// Helper for AA::PointerInfo::Acccess DenseMap/Set usage ignoring everythign
777/// but the instruction
778struct AccessAsInstructionInfo : DenseMapInfo<Instruction *> {
779 using Base = DenseMapInfo<Instruction *>;
780 using Access = AAPointerInfo::Access;
781 static inline Access getEmptyKey();
782 static inline Access getTombstoneKey();
783 static unsigned getHashValue(const Access &A);
784 static bool isEqual(const Access &LHS, const Access &RHS);
785};
786
787} // namespace llvm
788
789/// Helper to represent an access offset and size, with logic to deal with
790/// uncertainty and check for overlapping accesses.
791struct AA::PointerInfo::OffsetAndSize : public std::pair<int64_t, int64_t> {
792 using BaseTy = std::pair<int64_t, int64_t>;
793 OffsetAndSize(int64_t Offset, int64_t Size) : BaseTy(Offset, Size) {}
794 OffsetAndSize(const BaseTy &P) : BaseTy(P) {}
795 int64_t getOffset() const { return first; }
796 int64_t getSize() const { return second; }
797 static OffsetAndSize getUnknown() { return OffsetAndSize(Unknown, Unknown); }
798
799 /// Return true if offset or size are unknown.
800 bool offsetOrSizeAreUnknown() const {
801 return getOffset() == OffsetAndSize::Unknown ||
802 getSize() == OffsetAndSize::Unknown;
803 }
804
805 /// Return true if this offset and size pair might describe an address that
806 /// overlaps with \p OAS.
807 bool mayOverlap(const OffsetAndSize &OAS) const {
808 // Any unknown value and we are giving up -> overlap.
809 if (offsetOrSizeAreUnknown() || OAS.offsetOrSizeAreUnknown())
810 return true;
811
812 // Check if one offset point is in the other interval [offset, offset+size].
813 return OAS.getOffset() + OAS.getSize() > getOffset() &&
814 OAS.getOffset() < getOffset() + getSize();
815 }
816
817 /// Constant used to represent unknown offset or sizes.
818 static constexpr int64_t Unknown = 1 << 31;
819};
820
821/// Implementation of the DenseMapInfo.
822///
823///{
824inline llvm::AccessAsInstructionInfo::Access
825llvm::AccessAsInstructionInfo::getEmptyKey() {
826 return Access(Base::getEmptyKey(), nullptr, AAPointerInfo::AK_READ, nullptr);
827}
828inline llvm::AccessAsInstructionInfo::Access
829llvm::AccessAsInstructionInfo::getTombstoneKey() {
830 return Access(Base::getTombstoneKey(), nullptr, AAPointerInfo::AK_READ,
831 nullptr);
832}
833unsigned llvm::AccessAsInstructionInfo::getHashValue(
834 const llvm::AccessAsInstructionInfo::Access &A) {
835 return Base::getHashValue(A.getRemoteInst());
836}
837bool llvm::AccessAsInstructionInfo::isEqual(
838 const llvm::AccessAsInstructionInfo::Access &LHS,
839 const llvm::AccessAsInstructionInfo::Access &RHS) {
840 return LHS.getRemoteInst() == RHS.getRemoteInst();
841}
842inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
843llvm::DenseMapInfo<AAPointerInfo::Access>::getEmptyKey() {
844 return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_READ,
845 nullptr);
846}
847inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
848llvm::DenseMapInfo<AAPointerInfo::Access>::getTombstoneKey() {
849 return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_WRITE,
850 nullptr);
851}
852
853unsigned llvm::DenseMapInfo<AAPointerInfo::Access>::getHashValue(
854 const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &A) {
855 return detail::combineHashValue(
856 DenseMapInfo<Instruction *>::getHashValue(A.getRemoteInst()),
857 (A.isWrittenValueYetUndetermined()
858 ? ~0
859 : DenseMapInfo<Value *>::getHashValue(A.getWrittenValue()))) +
860 A.getKind();
861}
862
863bool llvm::DenseMapInfo<AAPointerInfo::Access>::isEqual(
864 const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &LHS,
865 const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &RHS) {
866 return LHS == RHS;
867}
868///}
869
870/// A type to track pointer/struct usage and accesses for AAPointerInfo.
871struct AA::PointerInfo::State : public AbstractState {
872
873 /// Return the best possible representable state.
874 static State getBestState(const State &SIS) { return State(); }
875
876 /// Return the worst possible representable state.
877 static State getWorstState(const State &SIS) {
878 State R;
879 R.indicatePessimisticFixpoint();
880 return R;
881 }
882
883 State() {}
884 State(const State &SIS) : AccessBins(SIS.AccessBins) {}
885 State(State &&SIS) : AccessBins(std::move(SIS.AccessBins)) {}
886
887 const State &getAssumed() const { return *this; }
888
889 /// See AbstractState::isValidState().
890 bool isValidState() const override { return BS.isValidState(); }
891
892 /// See AbstractState::isAtFixpoint().
893 bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
894
895 /// See AbstractState::indicateOptimisticFixpoint().
896 ChangeStatus indicateOptimisticFixpoint() override {
897 BS.indicateOptimisticFixpoint();
898 return ChangeStatus::UNCHANGED;
899 }
900
901 /// See AbstractState::indicatePessimisticFixpoint().
902 ChangeStatus indicatePessimisticFixpoint() override {
903 BS.indicatePessimisticFixpoint();
904 return ChangeStatus::CHANGED;
905 }
906
907 State &operator=(const State &R) {
908 if (this == &R)
909 return *this;
910 BS = R.BS;
911 AccessBins = R.AccessBins;
912 return *this;
913 }
914
915 State &operator=(State &&R) {
916 if (this == &R)
917 return *this;
918 std::swap(BS, R.BS);
919 std::swap(AccessBins, R.AccessBins);
920 return *this;
921 }
922
923 bool operator==(const State &R) const {
924 if (BS != R.BS)
925 return false;
926 if (AccessBins.size() != R.AccessBins.size())
927 return false;
928 auto It = begin(), RIt = R.begin(), E = end();
929 while (It != E) {
930 if (It->getFirst() != RIt->getFirst())
931 return false;
932 auto &Accs = It->getSecond();
933 auto &RAccs = RIt->getSecond();
934 if (Accs.size() != RAccs.size())
935 return false;
936 auto AccIt = Accs.begin(), RAccIt = RAccs.begin(), AccE = Accs.end();
937 while (AccIt != AccE) {
938 if (*AccIt != *RAccIt)
939 return false;
940 ++AccIt;
941 ++RAccIt;
942 }
943 ++It;
944 ++RIt;
945 }
946 return true;
947 }
948 bool operator!=(const State &R) const { return !(*this == R); }
949
950 /// We store accesses in a set with the instruction as key.
951 using Accesses = DenseSet<AAPointerInfo::Access, AccessAsInstructionInfo>;
952
953 /// We store all accesses in bins denoted by their offset and size.
954 using AccessBinsTy = DenseMap<OffsetAndSize, Accesses>;
955
956 AccessBinsTy::const_iterator begin() const { return AccessBins.begin(); }
957 AccessBinsTy::const_iterator end() const { return AccessBins.end(); }
958
959protected:
960 /// The bins with all the accesses for the associated pointer.
961 DenseMap<OffsetAndSize, Accesses> AccessBins;
962
963 /// Add a new access to the state at offset \p Offset and with size \p Size.
964 /// The access is associated with \p I, writes \p Content (if anything), and
965 /// is of kind \p Kind.
966 /// \Returns CHANGED, if the state changed, UNCHANGED otherwise.
967 ChangeStatus addAccess(int64_t Offset, int64_t Size, Instruction &I,
968 Optional<Value *> Content,
969 AAPointerInfo::AccessKind Kind, Type *Ty,
970 Instruction *RemoteI = nullptr,
971 Accesses *BinPtr = nullptr) {
972 OffsetAndSize Key{Offset, Size};
973 Accesses &Bin = BinPtr ? *BinPtr : AccessBins[Key];
974 AAPointerInfo::Access Acc(&I, RemoteI ? RemoteI : &I, Content, Kind, Ty);
975 // Check if we have an access for this instruction in this bin, if not,
976 // simply add it.
977 auto It = Bin.find(Acc);
978 if (It == Bin.end()) {
979 Bin.insert(Acc);
980 return ChangeStatus::CHANGED;
981 }
982 // If the existing access is the same as then new one, nothing changed.
983 AAPointerInfo::Access Before = *It;
984 // The new one will be combined with the existing one.
985 *It &= Acc;
986 return *It == Before ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED;
987 }
988
989 /// See AAPointerInfo::forallInterferingAccesses.
990 bool forallInterferingAccesses(
991 Instruction &I,
992 function_ref<bool(const AAPointerInfo::Access &, bool)> CB) const {
993 if (!isValidState())
994 return false;
995 // First find the offset and size of I.
996 OffsetAndSize OAS(-1, -1);
997 for (auto &It : AccessBins) {
998 for (auto &Access : It.getSecond()) {
999 if (Access.getRemoteInst() == &I) {
1000 OAS = It.getFirst();
1001 break;
1002 }
1003 }
1004 if (OAS.getSize() != -1)
1005 break;
1006 }
1007 if (OAS.getSize() == -1)
1008 return true;
1009
1010 // Now that we have an offset and size, find all overlapping ones and use
1011 // the callback on the accesses.
1012 for (auto &It : AccessBins) {
1013 OffsetAndSize ItOAS = It.getFirst();
1014 if (!OAS.mayOverlap(ItOAS))
1015 continue;
1016 bool IsExact = OAS == ItOAS && !OAS.offsetOrSizeAreUnknown();
1017 for (auto &Access : It.getSecond())
1018 if (!CB(Access, IsExact))
1019 return false;
1020 }
1021 return true;
1022 }
1023
1024private:
1025 /// State to track fixpoint and validity.
1026 BooleanState BS;
1027};
1028
1029namespace {
1030struct AAPointerInfoImpl
1031 : public StateWrapper<AA::PointerInfo::State, AAPointerInfo> {
1032 using BaseTy = StateWrapper<AA::PointerInfo::State, AAPointerInfo>;
1033 AAPointerInfoImpl(const IRPosition &IRP, Attributor &A) : BaseTy(IRP) {}
1034
1035 /// See AbstractAttribute::initialize(...).
1036 void initialize(Attributor &A) override { AAPointerInfo::initialize(A); }
1037
1038 /// See AbstractAttribute::getAsStr().
1039 const std::string getAsStr() const override {
1040 return std::string("PointerInfo ") +
1041 (isValidState() ? (std::string("#") +
1042 std::to_string(AccessBins.size()) + " bins")
1043 : "<invalid>");
1044 }
1045
1046 /// See AbstractAttribute::manifest(...).
1047 ChangeStatus manifest(Attributor &A) override {
1048 return AAPointerInfo::manifest(A);
1049 }
1050
1051 bool forallInterferingAccesses(
1052 LoadInst &LI, function_ref<bool(const AAPointerInfo::Access &, bool)> CB)
1053 const override {
1054 return State::forallInterferingAccesses(LI, CB);
1055 }
1056 bool forallInterferingAccesses(
1057 StoreInst &SI, function_ref<bool(const AAPointerInfo::Access &, bool)> CB)
1058 const override {
1059 return State::forallInterferingAccesses(SI, CB);
1060 }
1061
1062 ChangeStatus translateAndAddCalleeState(Attributor &A,
1063 const AAPointerInfo &CalleeAA,
1064 int64_t CallArgOffset, CallBase &CB) {
1065 using namespace AA::PointerInfo;
1066 if (!CalleeAA.getState().isValidState() || !isValidState())
1067 return indicatePessimisticFixpoint();
1068
1069 const auto &CalleeImplAA = static_cast<const AAPointerInfoImpl &>(CalleeAA);
1070 bool IsByval = CalleeImplAA.getAssociatedArgument()->hasByValAttr();
1071
1072 // Combine the accesses bin by bin.
1073 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1074 for (auto &It : CalleeImplAA.getState()) {
1075 OffsetAndSize OAS = OffsetAndSize::getUnknown();
1076 if (CallArgOffset != OffsetAndSize::Unknown)
1077 OAS = OffsetAndSize(It.first.getOffset() + CallArgOffset,
1078 It.first.getSize());
1079 Accesses &Bin = AccessBins[OAS];
1080 for (const AAPointerInfo::Access &RAcc : It.second) {
1081 if (IsByval && !RAcc.isRead())
1082 continue;
1083 bool UsedAssumedInformation = false;
1084 Optional<Value *> Content = A.translateArgumentToCallSiteContent(
1085 RAcc.getContent(), CB, *this, UsedAssumedInformation);
1086 AccessKind AK =
1087 AccessKind(RAcc.getKind() & (IsByval ? AccessKind::AK_READ
1088 : AccessKind::AK_READ_WRITE));
1089 Changed =
1090 Changed | addAccess(OAS.getOffset(), OAS.getSize(), CB, Content, AK,
1091 RAcc.getType(), RAcc.getRemoteInst(), &Bin);
1092 }
1093 }
1094 return Changed;
1095 }
1096
1097 /// Statistic tracking for all AAPointerInfo implementations.
1098 /// See AbstractAttribute::trackStatistics().
1099 void trackPointerInfoStatistics(const IRPosition &IRP) const {}
1100};
1101
1102struct AAPointerInfoFloating : public AAPointerInfoImpl {
1103 using AccessKind = AAPointerInfo::AccessKind;
1104 AAPointerInfoFloating(const IRPosition &IRP, Attributor &A)
1105 : AAPointerInfoImpl(IRP, A) {}
1106
1107 /// See AbstractAttribute::initialize(...).
1108 void initialize(Attributor &A) override { AAPointerInfoImpl::initialize(A); }
1109
1110 /// Deal with an access and signal if it was handled successfully.
1111 bool handleAccess(Attributor &A, Instruction &I, Value &Ptr,
1112 Optional<Value *> Content, AccessKind Kind, int64_t Offset,
1113 ChangeStatus &Changed, Type *Ty,
1114 int64_t Size = AA::PointerInfo::OffsetAndSize::Unknown) {
1115 using namespace AA::PointerInfo;
1116 // No need to find a size if one is given or the offset is unknown.
1117 if (Offset != OffsetAndSize::Unknown && Size == OffsetAndSize::Unknown &&
1118 Ty) {
1119 const DataLayout &DL = A.getDataLayout();
1120 TypeSize AccessSize = DL.getTypeStoreSize(Ty);
1121 if (!AccessSize.isScalable())
1122 Size = AccessSize.getFixedSize();
1123 }
1124 Changed = Changed | addAccess(Offset, Size, I, Content, Kind, Ty);
1125 return true;
1126 };
1127
1128 /// Helper struct, will support ranges eventually.
1129 struct OffsetInfo {
1130 int64_t Offset = AA::PointerInfo::OffsetAndSize::Unknown;
1131
1132 bool operator==(const OffsetInfo &OI) const { return Offset == OI.Offset; }
1133 };
1134
1135 /// See AbstractAttribute::updateImpl(...).
1136 ChangeStatus updateImpl(Attributor &A) override {
1137 using namespace AA::PointerInfo;
1138 State S = getState();
1139 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1140 Value &AssociatedValue = getAssociatedValue();
1141
1142 const DataLayout &DL = A.getDataLayout();
1143 DenseMap<Value *, OffsetInfo> OffsetInfoMap;
1144 OffsetInfoMap[&AssociatedValue] = OffsetInfo{0};
1145
1146 auto HandlePassthroughUser = [&](Value *Usr, OffsetInfo &PtrOI,
1147 bool &Follow) {
1148 OffsetInfo &UsrOI = OffsetInfoMap[Usr];
1149 UsrOI = PtrOI;
1150 Follow = true;
1151 return true;
1152 };
1153
1154 const auto *TLI = getAnchorScope()
1155 ? A.getInfoCache().getTargetLibraryInfoForFunction(
1156 *getAnchorScope())
1157 : nullptr;
1158 auto UsePred = [&](const Use &U, bool &Follow) -> bool {
1159 Value *CurPtr = U.get();
1160 User *Usr = U.getUser();
1161 LLVM_DEBUG(dbgs() << "[AAPointerInfo] Analyze " << *CurPtr << " in "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] Analyze " <<
*CurPtr << " in " << *Usr << "\n"; } } while
(false)
1162 << *Usr << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] Analyze " <<
*CurPtr << " in " << *Usr << "\n"; } } while
(false)
;
1163 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", 1164, __extension__
__PRETTY_FUNCTION__))
1164 "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", 1164, __extension__
__PRETTY_FUNCTION__))
;
1165
1166 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Usr)) {
1167 if (CE->isCast())
1168 return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow);
1169 if (CE->isCompare())
1170 return true;
1171 if (!isa<GEPOperator>(CE)) {
1172 LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled constant user " << *CEdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] Unhandled constant user "
<< *CE << "\n"; } } while (false)
1173 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] Unhandled constant user "
<< *CE << "\n"; } } while (false)
;
1174 return false;
1175 }
1176 }
1177 if (auto *GEP = dyn_cast<GEPOperator>(Usr)) {
1178 // Note the order here, the Usr access might change the map, CurPtr is
1179 // already in it though.
1180 OffsetInfo &UsrOI = OffsetInfoMap[Usr];
1181 OffsetInfo &PtrOI = OffsetInfoMap[CurPtr];
1182 UsrOI = PtrOI;
1183
1184 // TODO: Use range information.
1185 if (PtrOI.Offset == OffsetAndSize::Unknown ||
1186 !GEP->hasAllConstantIndices()) {
1187 UsrOI.Offset = OffsetAndSize::Unknown;
1188 Follow = true;
1189 return true;
1190 }
1191
1192 SmallVector<Value *, 8> Indices;
1193 for (Use &Idx : GEP->indices()) {
1194 if (auto *CIdx = dyn_cast<ConstantInt>(Idx)) {
1195 Indices.push_back(CIdx);
1196 continue;
1197 }
1198
1199 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)
1200 << " : " << *Idx << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] Non constant GEP index "
<< *GEP << " : " << *Idx << "\n"; } }
while (false)
;
1201 return false;
1202 }
1203 UsrOI.Offset = PtrOI.Offset +
1204 DL.getIndexedOffsetInType(
1205 GEP->getSourceElementType(), Indices);
1206 Follow = true;
1207 return true;
1208 }
1209 if (isa<CastInst>(Usr) || isa<SelectInst>(Usr))
1210 return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow);
1211
1212 // For PHIs we need to take care of the recurrence explicitly as the value
1213 // might change while we iterate through a loop. For now, we give up if
1214 // the PHI is not invariant.
1215 if (isa<PHINode>(Usr)) {
1216 // Note the order here, the Usr access might change the map, CurPtr is
1217 // already in it though.
1218 OffsetInfo &UsrOI = OffsetInfoMap[Usr];
1219 OffsetInfo &PtrOI = OffsetInfoMap[CurPtr];
1220 // Check if the PHI is invariant (so far).
1221 if (UsrOI == PtrOI)
1222 return true;
1223
1224 // Check if the PHI operand has already an unknown offset as we can't
1225 // improve on that anymore.
1226 if (PtrOI.Offset == OffsetAndSize::Unknown) {
1227 UsrOI = PtrOI;
1228 Follow = true;
1229 return true;
1230 }
1231
1232 // Check if the PHI operand is not dependent on the PHI itself.
1233 // TODO: This is not great as we look at the pointer type. However, it
1234 // is unclear where the Offset size comes from with typeless pointers.
1235 APInt Offset(
1236 DL.getIndexSizeInBits(CurPtr->getType()->getPointerAddressSpace()),
1237 0);
1238 if (&AssociatedValue == CurPtr->stripAndAccumulateConstantOffsets(
1239 DL, Offset, /* AllowNonInbounds */ true)) {
1240 if (Offset != PtrOI.Offset) {
1241 LLVM_DEBUG(dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] PHI operand pointer offset mismatch "
<< *CurPtr << " in " << *Usr << "\n"
; } } while (false)
1242 << "[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)
1243 << *CurPtr << " in " << *Usr << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] PHI operand pointer offset mismatch "
<< *CurPtr << " in " << *Usr << "\n"
; } } while (false)
;
1244 return false;
1245 }
1246 return HandlePassthroughUser(Usr, PtrOI, Follow);
1247 }
1248
1249 // TODO: Approximate in case we know the direction of the recurrence.
1250 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)
1251 << *CurPtr << " in " << *Usr << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] PHI operand is too complex "
<< *CurPtr << " in " << *Usr << "\n"
; } } while (false)
;
1252 UsrOI = PtrOI;
1253 UsrOI.Offset = OffsetAndSize::Unknown;
1254 Follow = true;
1255 return true;
1256 }
1257
1258 if (auto *LoadI = dyn_cast<LoadInst>(Usr))
1259 return handleAccess(A, *LoadI, *CurPtr, /* Content */ nullptr,
1260 AccessKind::AK_READ, OffsetInfoMap[CurPtr].Offset,
1261 Changed, LoadI->getType());
1262 if (auto *StoreI = dyn_cast<StoreInst>(Usr)) {
1263 if (StoreI->getValueOperand() == CurPtr) {
1264 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)
1265 << *StoreI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] Escaping use in store "
<< *StoreI << "\n"; } } while (false)
;
1266 return false;
1267 }
1268 bool UsedAssumedInformation = false;
1269 Optional<Value *> Content = A.getAssumedSimplified(
1270 *StoreI->getValueOperand(), *this, UsedAssumedInformation);
1271 return handleAccess(A, *StoreI, *CurPtr, Content, AccessKind::AK_WRITE,
1272 OffsetInfoMap[CurPtr].Offset, Changed,
1273 StoreI->getValueOperand()->getType());
1274 }
1275 if (auto *CB = dyn_cast<CallBase>(Usr)) {
1276 if (CB->isLifetimeStartOrEnd())
1277 return true;
1278 if (TLI && isFreeCall(CB, TLI))
1279 return true;
1280 if (CB->isArgOperand(&U)) {
1281 unsigned ArgNo = CB->getArgOperandNo(&U);
1282 const auto &CSArgPI = A.getAAFor<AAPointerInfo>(
1283 *this, IRPosition::callsite_argument(*CB, ArgNo),
1284 DepClassTy::REQUIRED);
1285 Changed = translateAndAddCalleeState(
1286 A, CSArgPI, OffsetInfoMap[CurPtr].Offset, *CB) |
1287 Changed;
1288 return true;
1289 }
1290 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)
1291 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] Call user not handled "
<< *CB << "\n"; } } while (false)
;
1292 // TODO: Allow some call uses
1293 return false;
1294 }
1295
1296 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)
;
1297 return false;
1298 };
1299 auto EquivalentUseCB = [&](const Use &OldU, const Use &NewU) {
1300 if (OffsetInfoMap.count(NewU))
1301 return OffsetInfoMap[NewU] == OffsetInfoMap[OldU];
1302 OffsetInfoMap[NewU] = OffsetInfoMap[OldU];
1303 return true;
1304 };
1305 if (!A.checkForAllUses(UsePred, *this, AssociatedValue,
1306 /* CheckBBLivenessOnly */ true, DepClassTy::OPTIONAL,
1307 EquivalentUseCB))
1308 return indicatePessimisticFixpoint();
1309
1310 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)
1311 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)
1312 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)
1313 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)
1314 << 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)
1315 << "] : " << 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)
1316 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)
1317 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)
1318 << "\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)
1319 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)
1320 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)
1321 << *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)
1322 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)
1323 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)
1324 }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)
1325 }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)
1326 })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)
;
1327
1328 return Changed;
1329 }
1330
1331 /// See AbstractAttribute::trackStatistics()
1332 void trackStatistics() const override {
1333 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1334 }
1335};
1336
1337struct AAPointerInfoReturned final : AAPointerInfoImpl {
1338 AAPointerInfoReturned(const IRPosition &IRP, Attributor &A)
1339 : AAPointerInfoImpl(IRP, A) {}
1340
1341 /// See AbstractAttribute::updateImpl(...).
1342 ChangeStatus updateImpl(Attributor &A) override {
1343 return indicatePessimisticFixpoint();
1344 }
1345
1346 /// See AbstractAttribute::trackStatistics()
1347 void trackStatistics() const override {
1348 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1349 }
1350};
1351
1352struct AAPointerInfoArgument final : AAPointerInfoFloating {
1353 AAPointerInfoArgument(const IRPosition &IRP, Attributor &A)
1354 : AAPointerInfoFloating(IRP, A) {}
1355
1356 /// See AbstractAttribute::initialize(...).
1357 void initialize(Attributor &A) override {
1358 AAPointerInfoFloating::initialize(A);
1359 if (getAnchorScope()->isDeclaration())
1360 indicatePessimisticFixpoint();
1361 }
1362
1363 /// See AbstractAttribute::trackStatistics()
1364 void trackStatistics() const override {
1365 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1366 }
1367};
1368
1369struct AAPointerInfoCallSiteArgument final : AAPointerInfoFloating {
1370 AAPointerInfoCallSiteArgument(const IRPosition &IRP, Attributor &A)
1371 : AAPointerInfoFloating(IRP, A) {}
1372
1373 /// See AbstractAttribute::updateImpl(...).
1374 ChangeStatus updateImpl(Attributor &A) override {
1375 using namespace AA::PointerInfo;
1376 // We handle memory intrinsics explicitly, at least the first (=
1377 // destination) and second (=source) arguments as we know how they are
1378 // accessed.
1379 if (auto *MI = dyn_cast_or_null<MemIntrinsic>(getCtxI())) {
1380 ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength());
1381 int64_t LengthVal = OffsetAndSize::Unknown;
1382 if (Length)
1383 LengthVal = Length->getSExtValue();
1384 Value &Ptr = getAssociatedValue();
1385 unsigned ArgNo = getIRPosition().getCallSiteArgNo();
1386 ChangeStatus Changed;
1387 if (ArgNo == 0) {
1388 handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_WRITE, 0, Changed,
1389 nullptr, LengthVal);
1390 } else if (ArgNo == 1) {
1391 handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_READ, 0, Changed,
1392 nullptr, LengthVal);
1393 } else {
1394 LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled memory intrinsic "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] Unhandled memory intrinsic "
<< *MI << "\n"; } } while (false)
1395 << *MI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAPointerInfo] Unhandled memory intrinsic "
<< *MI << "\n"; } } while (false)
;
1396 return indicatePessimisticFixpoint();
1397 }
1398 return Changed;
1399 }
1400
1401 // TODO: Once we have call site specific value information we can provide
1402 // call site specific liveness information and then it makes
1403 // sense to specialize attributes for call sites arguments instead of
1404 // redirecting requests to the callee argument.
1405 Argument *Arg = getAssociatedArgument();
1406 if (!Arg)
1407 return indicatePessimisticFixpoint();
1408 const IRPosition &ArgPos = IRPosition::argument(*Arg);
1409 auto &ArgAA =
1410 A.getAAFor<AAPointerInfo>(*this, ArgPos, DepClassTy::REQUIRED);
1411 return translateAndAddCalleeState(A, ArgAA, 0, *cast<CallBase>(getCtxI()));
1412 }
1413
1414 /// See AbstractAttribute::trackStatistics()
1415 void trackStatistics() const override {
1416 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1417 }
1418};
1419
1420struct AAPointerInfoCallSiteReturned final : AAPointerInfoFloating {
1421 AAPointerInfoCallSiteReturned(const IRPosition &IRP, Attributor &A)
1422 : AAPointerInfoFloating(IRP, A) {}
1423
1424 /// See AbstractAttribute::trackStatistics()
1425 void trackStatistics() const override {
1426 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1427 }
1428};
1429
1430/// -----------------------NoUnwind Function Attribute--------------------------
1431
1432struct AANoUnwindImpl : AANoUnwind {
1433 AANoUnwindImpl(const IRPosition &IRP, Attributor &A) : AANoUnwind(IRP, A) {}
1434
1435 const std::string getAsStr() const override {
1436 return getAssumed() ? "nounwind" : "may-unwind";
1437 }
1438
1439 /// See AbstractAttribute::updateImpl(...).
1440 ChangeStatus updateImpl(Attributor &A) override {
1441 auto Opcodes = {
1442 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
1443 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet,
1444 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
1445
1446 auto CheckForNoUnwind = [&](Instruction &I) {
1447 if (!I.mayThrow())
1448 return true;
1449
1450 if (const auto *CB = dyn_cast<CallBase>(&I)) {
1451 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
1452 *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
1453 return NoUnwindAA.isAssumedNoUnwind();
1454 }
1455 return false;
1456 };
1457
1458 bool UsedAssumedInformation = false;
1459 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes,
1460 UsedAssumedInformation))
1461 return indicatePessimisticFixpoint();
1462
1463 return ChangeStatus::UNCHANGED;
1464 }
1465};
1466
1467struct AANoUnwindFunction final : public AANoUnwindImpl {
1468 AANoUnwindFunction(const IRPosition &IRP, Attributor &A)
1469 : AANoUnwindImpl(IRP, A) {}
1470
1471 /// See AbstractAttribute::trackStatistics()
1472 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind){ static llvm::Statistic NumIRFunction_nounwind = {"attributor"
, "NumIRFunction_nounwind", ("Number of " "functions" " marked '"
"nounwind" "'")};; ++(NumIRFunction_nounwind); }
}
1473};
1474
1475/// NoUnwind attribute deduction for a call sites.
1476struct AANoUnwindCallSite final : AANoUnwindImpl {
1477 AANoUnwindCallSite(const IRPosition &IRP, Attributor &A)
1478 : AANoUnwindImpl(IRP, A) {}
1479
1480 /// See AbstractAttribute::initialize(...).
1481 void initialize(Attributor &A) override {
1482 AANoUnwindImpl::initialize(A);
1483 Function *F = getAssociatedFunction();
1484 if (!F || F->isDeclaration())
1485 indicatePessimisticFixpoint();
1486 }
1487
1488 /// See AbstractAttribute::updateImpl(...).
1489 ChangeStatus updateImpl(Attributor &A) override {
1490 // TODO: Once we have call site specific value information we can provide
1491 // call site specific liveness information and then it makes
1492 // sense to specialize attributes for call sites arguments instead of
1493 // redirecting requests to the callee argument.
1494 Function *F = getAssociatedFunction();
1495 const IRPosition &FnPos = IRPosition::function(*F);
1496 auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::REQUIRED);
1497 return clampStateAndIndicateChange(getState(), FnAA.getState());
1498 }
1499
1500 /// See AbstractAttribute::trackStatistics()
1501 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); }
; }
1502};
1503
1504/// --------------------- Function Return Values -------------------------------
1505
1506/// "Attribute" that collects all potential returned values and the return
1507/// instructions that they arise from.
1508///
1509/// If there is a unique returned value R, the manifest method will:
1510/// - mark R with the "returned" attribute, if R is an argument.
1511class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
1512
1513 /// Mapping of values potentially returned by the associated function to the
1514 /// return instructions that might return them.
1515 MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
1516
1517 /// State flags
1518 ///
1519 ///{
1520 bool IsFixed = false;
1521 bool IsValidState = true;
1522 ///}
1523
1524public:
1525 AAReturnedValuesImpl(const IRPosition &IRP, Attributor &A)
1526 : AAReturnedValues(IRP, A) {}
1527
1528 /// See AbstractAttribute::initialize(...).
1529 void initialize(Attributor &A) override {
1530 // Reset the state.
1531 IsFixed = false;
1532 IsValidState = true;
1533 ReturnedValues.clear();
1534
1535 Function *F = getAssociatedFunction();
1536 if (!F || F->isDeclaration()) {
1537 indicatePessimisticFixpoint();
1538 return;
1539 }
1540 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", 1541, __extension__
__PRETTY_FUNCTION__))
1541 "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", 1541, __extension__
__PRETTY_FUNCTION__))
;
1542
1543 // The map from instruction opcodes to those instructions in the function.
1544 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
1545
1546 // Look through all arguments, if one is marked as returned we are done.
1547 for (Argument &Arg : F->args()) {
1548 if (Arg.hasReturnedAttr()) {
1549 auto &ReturnInstSet = ReturnedValues[&Arg];
1550 if (auto *Insts = OpcodeInstMap.lookup(Instruction::Ret))
1551 for (Instruction *RI : *Insts)
1552 ReturnInstSet.insert(cast<ReturnInst>(RI));
1553
1554 indicateOptimisticFixpoint();
1555 return;
1556 }
1557 }
1558
1559 if (!A.isFunctionIPOAmendable(*F))
1560 indicatePessimisticFixpoint();
1561 }
1562
1563 /// See AbstractAttribute::manifest(...).
1564 ChangeStatus manifest(Attributor &A) override;
1565
1566 /// See AbstractAttribute::getState(...).
1567 AbstractState &getState() override { return *this; }
1568
1569 /// See AbstractAttribute::getState(...).
1570 const AbstractState &getState() const override { return *this; }
1571
1572 /// See AbstractAttribute::updateImpl(Attributor &A).
1573 ChangeStatus updateImpl(Attributor &A) override;
1574
1575 llvm::iterator_range<iterator> returned_values() override {
1576 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1577 }
1578
1579 llvm::iterator_range<const_iterator> returned_values() const override {
1580 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1581 }
1582
1583 /// Return the number of potential return values, -1 if unknown.
1584 size_t getNumReturnValues() const override {
1585 return isValidState() ? ReturnedValues.size() : -1;
1586 }
1587
1588 /// Return an assumed unique return value if a single candidate is found. If
1589 /// there cannot be one, return a nullptr. If it is not clear yet, return the
1590 /// Optional::NoneType.
1591 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
1592
1593 /// See AbstractState::checkForAllReturnedValues(...).
1594 bool checkForAllReturnedValuesAndReturnInsts(
1595 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
1596 const override;
1597
1598 /// Pretty print the attribute similar to the IR representation.
1599 const std::string getAsStr() const override;
1600
1601 /// See AbstractState::isAtFixpoint().
1602 bool isAtFixpoint() const override { return IsFixed; }
1603
1604 /// See AbstractState::isValidState().
1605 bool isValidState() const override { return IsValidState; }
1606
1607 /// See AbstractState::indicateOptimisticFixpoint(...).
1608 ChangeStatus indicateOptimisticFixpoint() override {
1609 IsFixed = true;
1610 return ChangeStatus::UNCHANGED;
1611 }
1612
1613 ChangeStatus indicatePessimisticFixpoint() override {
1614 IsFixed = true;
1615 IsValidState = false;
1616 return ChangeStatus::CHANGED;
1617 }
1618};
1619
1620ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
1621 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1622
1623 // Bookkeeping.
1624 assert(isValidState())(static_cast <bool> (isValidState()) ? void (0) : __assert_fail
("isValidState()", "llvm/lib/Transforms/IPO/AttributorAttributes.cpp"
, 1624, __extension__ __PRETTY_FUNCTION__))
;
1625 STATS_DECLTRACK(KnownReturnValues, FunctionReturn,{ static llvm::Statistic NumIRFunctionReturn_KnownReturnValues
= {"attributor", "NumIRFunctionReturn_KnownReturnValues", "Number of function with known return values"
};; ++(NumIRFunctionReturn_KnownReturnValues); }
1626 "Number of function with known return values"){ static llvm::Statistic NumIRFunctionReturn_KnownReturnValues
= {"attributor", "NumIRFunctionReturn_KnownReturnValues", "Number of function with known return values"
};; ++(NumIRFunctionReturn_KnownReturnValues); }
;
1627
1628 // Check if we have an assumed unique return value that we could manifest.
1629 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
1630
1631 if (!UniqueRV.hasValue() || !UniqueRV.getValue())
1632 return Changed;
1633
1634 // Bookkeeping.
1635 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,{ static llvm::Statistic NumIRFunctionReturn_UniqueReturnValue
= {"attributor", "NumIRFunctionReturn_UniqueReturnValue", "Number of function with unique return"
};; ++(NumIRFunctionReturn_UniqueReturnValue); }
1636 "Number of function with unique return"){ static llvm::Statistic NumIRFunctionReturn_UniqueReturnValue
= {"attributor", "NumIRFunctionReturn_UniqueReturnValue", "Number of function with unique return"
};; ++(NumIRFunctionReturn_UniqueReturnValue); }
;
1637 // If the assumed unique return value is an argument, annotate it.
1638 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
1639 if (UniqueRVArg->getType()->canLosslesslyBitCastTo(
1640 getAssociatedFunction()->getReturnType())) {
1641 getIRPosition() = IRPosition::argument(*UniqueRVArg);
1642 Changed = IRAttribute::manifest(A);
1643 }
1644 }
1645 return Changed;
1646}
1647
1648const std::string AAReturnedValuesImpl::getAsStr() const {
1649 return (isAtFixpoint() ? "returns(#" : "may-return(#") +
1650 (isValidState() ? std::to_string(getNumReturnValues()) : "?") + ")";
1651}
1652
1653Optional<Value *>
1654AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
1655 // If checkForAllReturnedValues provides a unique value, ignoring potential
1656 // undef values that can also be present, it is assumed to be the actual
1657 // return value and forwarded to the caller of this method. If there are
1658 // multiple, a nullptr is returned indicating there cannot be a unique
1659 // returned value.
1660 Optional<Value *> UniqueRV;
1661 Type *Ty = getAssociatedFunction()->getReturnType();
1662
1663 auto Pred = [&](Value &RV) -> bool {
1664 UniqueRV = AA::combineOptionalValuesInAAValueLatice(UniqueRV, &RV, Ty);
1665 return UniqueRV != Optional<Value *>(nullptr);
1666 };
1667
1668 if (!A.checkForAllReturnedValues(Pred, *this))
1669 UniqueRV = nullptr;
1670
1671 return UniqueRV;
1672}
1673
1674bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
1675 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
1676 const {
1677 if (!isValidState())
1678 return false;
1679
1680 // Check all returned values but ignore call sites as long as we have not
1681 // encountered an overdefined one during an update.
1682 for (auto &It : ReturnedValues) {
1683 Value *RV = It.first;
1684 if (!Pred(*RV, It.second))
1685 return false;
1686 }
1687
1688 return true;
1689}
1690
1691ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
1692 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1693
1694 auto ReturnValueCB = [&](Value &V, const Instruction *CtxI, ReturnInst &Ret,
1695 bool) -> bool {
1696 bool UsedAssumedInformation = false;
1697 Optional<Value *> SimpleRetVal =
1698 A.getAssumedSimplified(V, *this, UsedAssumedInformation);
1699 if (!SimpleRetVal.hasValue())
1700 return true;
1701 if (!SimpleRetVal.getValue())
1702 return false;
1703 Value *RetVal = *SimpleRetVal;
1704 assert(AA::isValidInScope(*RetVal, Ret.getFunction()) &&(static_cast <bool> (AA::isValidInScope(*RetVal, Ret.getFunction
()) && "Assumed returned value should be valid in function scope!"
) ? void (0) : __assert_fail ("AA::isValidInScope(*RetVal, Ret.getFunction()) && \"Assumed returned value should be valid in function scope!\""
, "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1705, __extension__
__PRETTY_FUNCTION__))
1705 "Assumed returned value should be valid in function scope!")(static_cast <bool> (AA::isValidInScope(*RetVal, Ret.getFunction
()) && "Assumed returned value should be valid in function scope!"
) ? void (0) : __assert_fail ("AA::isValidInScope(*RetVal, Ret.getFunction()) && \"Assumed returned value should be valid in function scope!\""
, "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1705, __extension__
__PRETTY_FUNCTION__))
;
1706 if (ReturnedValues[RetVal].insert(&Ret))
1707 Changed = ChangeStatus::CHANGED;
1708 return true;
1709 };
1710
1711 auto ReturnInstCB = [&](Instruction &I) {
1712 ReturnInst &Ret = cast<ReturnInst>(I);
1713 return genericValueTraversal<ReturnInst>(
1714 A, IRPosition::value(*Ret.getReturnValue()), *this, Ret, ReturnValueCB,
1715 &I);
1716 };
1717
1718 // Discover returned values from all live returned instructions in the
1719 // associated function.
1720 bool UsedAssumedInformation = false;
1721 if (!A.checkForAllInstructions(ReturnInstCB, *this, {Instruction::Ret},
1722 UsedAssumedInformation))
1723 return indicatePessimisticFixpoint();
1724 return Changed;
1725}
1726
1727struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1728 AAReturnedValuesFunction(const IRPosition &IRP, Attributor &A)
1729 : AAReturnedValuesImpl(IRP, A) {}
1730
1731 /// See AbstractAttribute::trackStatistics()
1732 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned){ static llvm::Statistic NumIRArguments_returned = {"attributor"
, "NumIRArguments_returned", ("Number of " "arguments" " marked '"
"returned" "'")};; ++(NumIRArguments_returned); }
}
1733};
1734
1735/// Returned values information for a call sites.
1736struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1737 AAReturnedValuesCallSite(const IRPosition &IRP, Attributor &A)
1738 : AAReturnedValuesImpl(IRP, A) {}
1739
1740 /// See AbstractAttribute::initialize(...).
1741 void initialize(Attributor &A) override {
1742 // TODO: Once we have call site specific value information we can provide
1743 // call site specific liveness information and then it makes
1744 // sense to specialize attributes for call sites instead of
1745 // redirecting requests to the callee.
1746 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"
, 1747)
1747 "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"
, 1747)
;
1748 }
1749
1750 /// See AbstractAttribute::updateImpl(...).
1751 ChangeStatus updateImpl(Attributor &A) override {
1752 return indicatePessimisticFixpoint();
1753 }
1754
1755 /// See AbstractAttribute::trackStatistics()
1756 void trackStatistics() const override {}
1757};
1758
1759/// ------------------------ NoSync Function Attribute -------------------------
1760
1761struct AANoSyncImpl : AANoSync {
1762 AANoSyncImpl(const IRPosition &IRP, Attributor &A) : AANoSync(IRP, A) {}
1763
1764 const std::string getAsStr() const override {
1765 return getAssumed() ? "nosync" : "may-sync";
1766 }
1767
1768 /// See AbstractAttribute::updateImpl(...).
1769 ChangeStatus updateImpl(Attributor &A) override;
1770
1771 /// Helper function used to determine whether an instruction is non-relaxed
1772 /// atomic. In other words, if an atomic instruction does not have unordered
1773 /// or monotonic ordering
1774 static bool isNonRelaxedAtomic(Instruction *I);
1775
1776 /// Helper function specific for intrinsics which are potentially volatile
1777 static bool isNoSyncIntrinsic(Instruction *I);
1778};
1779
1780bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
1781 if (!I->isAtomic())
1782 return false;
1783
1784 if (auto *FI = dyn_cast<FenceInst>(I))
1785 // All legal orderings for fence are stronger than monotonic.
1786 return FI->getSyncScopeID() != SyncScope::SingleThread;
1787 else if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I)) {
1788 // Unordered is not a legal ordering for cmpxchg.
1789 return (AI->getSuccessOrdering() != AtomicOrdering::Monotonic ||
1790 AI->getFailureOrdering() != AtomicOrdering::Monotonic);
1791 }
1792
1793 AtomicOrdering Ordering;
1794 switch (I->getOpcode()) {
1795 case Instruction::AtomicRMW:
1796 Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1797 break;
1798 case Instruction::Store:
1799 Ordering = cast<StoreInst>(I)->getOrdering();
1800 break;
1801 case Instruction::Load:
1802 Ordering = cast<LoadInst>(I)->getOrdering();
1803 break;
1804 default:
1805 llvm_unreachable(::llvm::llvm_unreachable_internal("New atomic operations need to be known in the attributor."
, "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 1806)
1806 "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", 1806)
;
1807 }
1808
1809 return (Ordering != AtomicOrdering::Unordered &&
1810 Ordering != AtomicOrdering::Monotonic);
1811}
1812
1813/// Return true if this intrinsic is nosync. This is only used for intrinsics
1814/// which would be nosync except that they have a volatile flag. All other
1815/// intrinsics are simply annotated with the nosync attribute in Intrinsics.td.
1816bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
1817 if (auto *MI = dyn_cast<MemIntrinsic>(I))
1818 return !MI->isVolatile();
1819 return false;
1820}
1821
1822ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
1823
1824 auto CheckRWInstForNoSync = [&](Instruction &I) {
1825 /// We are looking for volatile instructions or Non-Relaxed atomics.
1826
1827 if (const auto *CB = dyn_cast<CallBase>(&I)) {
1828 if (CB->hasFnAttr(Attribute::NoSync))
1829 return true;
1830
1831 if (isNoSyncIntrinsic(&I))
1832 return true;
1833
1834 const auto &NoSyncAA = A.getAAFor<AANoSync>(
1835 *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
1836 return NoSyncAA.isAssumedNoSync();
1837 }
1838
1839 if (!I.isVolatile() && !isNonRelaxedAtomic(&I))
1840 return true;
1841
1842 return false;
1843 };
1844
1845 auto CheckForNoSync = [&](Instruction &I) {
1846 // At this point we handled all read/write effects and they are all
1847 // nosync, so they can be skipped.
1848 if (I.mayReadOrWriteMemory())
1849 return true;
1850
1851 // non-convergent and readnone imply nosync.
1852 return !cast<CallBase>(I).isConvergent();
1853 };
1854
1855 bool UsedAssumedInformation = false;
1856 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this,
1857 UsedAssumedInformation) ||
1858 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this,
1859 UsedAssumedInformation))
1860 return indicatePessimisticFixpoint();
1861
1862 return ChangeStatus::UNCHANGED;
1863}
1864
1865struct AANoSyncFunction final : public AANoSyncImpl {
1866 AANoSyncFunction(const IRPosition &IRP, Attributor &A)
1867 : AANoSyncImpl(IRP, A) {}
1868
1869 /// See AbstractAttribute::trackStatistics()
1870 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync){ static llvm::Statistic NumIRFunction_nosync = {"attributor"
, "NumIRFunction_nosync", ("Number of " "functions" " marked '"
"nosync" "'")};; ++(NumIRFunction_nosync); }
}
1871};
1872
1873/// NoSync attribute deduction for a call sites.
1874struct AANoSyncCallSite final : AANoSyncImpl {
1875 AANoSyncCallSite(const IRPosition &IRP, Attributor &A)
1876 : AANoSyncImpl(IRP, A) {}
1877
1878 /// See AbstractAttribute::initialize(...).
1879 void initialize(Attributor &A) override {
1880 AANoSyncImpl::initialize(A);
1881 Function *F = getAssociatedFunction();
1882 if (!F || F->isDeclaration())
1883 indicatePessimisticFixpoint();
1884 }
1885
1886 /// See AbstractAttribute::updateImpl(...).
1887 ChangeStatus updateImpl(Attributor &A) override {
1888 // TODO: Once we have call site specific value information we can provide
1889 // call site specific liveness information and then it makes
1890 // sense to specialize attributes for call sites arguments instead of
1891 // redirecting requests to the callee argument.
1892 Function *F = getAssociatedFunction();
1893 const IRPosition &FnPos = IRPosition::function(*F);
1894 auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos, DepClassTy::REQUIRED);
1895 return clampStateAndIndicateChange(getState(), FnAA.getState());
1896 }
1897
1898 /// See AbstractAttribute::trackStatistics()
1899 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
); }
; }
1900};
1901
1902/// ------------------------ No-Free Attributes ----------------------------
1903
1904struct AANoFreeImpl : public AANoFree {
1905 AANoFreeImpl(const IRPosition &IRP, Attributor &A) : AANoFree(IRP, A) {}
1906
1907 /// See AbstractAttribute::updateImpl(...).
1908 ChangeStatus updateImpl(Attributor &A) override {
1909 auto CheckForNoFree = [&](Instruction &I) {
1910 const auto &CB = cast<CallBase>(I);
1911 if (CB.hasFnAttr(Attribute::NoFree))
1912 return true;
1913
1914 const auto &NoFreeAA = A.getAAFor<AANoFree>(
1915 *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
1916 return NoFreeAA.isAssumedNoFree();
1917 };
1918
1919 bool UsedAssumedInformation = false;
1920 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this,
1921 UsedAssumedInformation))
1922 return indicatePessimisticFixpoint();
1923 return ChangeStatus::UNCHANGED;
1924 }
1925
1926 /// See AbstractAttribute::getAsStr().
1927 const std::string getAsStr() const override {
1928 return getAssumed() ? "nofree" : "may-free";
1929 }
1930};
1931
1932struct AANoFreeFunction final : public AANoFreeImpl {
1933 AANoFreeFunction(const IRPosition &IRP, Attributor &A)
1934 : AANoFreeImpl(IRP, A) {}
1935
1936 /// See AbstractAttribute::trackStatistics()
1937 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree){ static llvm::Statistic NumIRFunction_nofree = {"attributor"
, "NumIRFunction_nofree", ("Number of " "functions" " marked '"
"nofree" "'")};; ++(NumIRFunction_nofree); }
}
1938};
1939
1940/// NoFree attribute deduction for a call sites.
1941struct AANoFreeCallSite final : AANoFreeImpl {
1942 AANoFreeCallSite(const IRPosition &IRP, Attributor &A)
1943 : AANoFreeImpl(IRP, A) {}
1944
1945 /// See AbstractAttribute::initialize(...).
1946 void initialize(Attributor &A) override {
1947 AANoFreeImpl::initialize(A);
1948 Function *F = getAssociatedFunction();
1949 if (!F || F->isDeclaration())
1950 indicatePessimisticFixpoint();
1951 }
1952
1953 /// See AbstractAttribute::updateImpl(...).
1954 ChangeStatus updateImpl(Attributor &A) override {
1955 // TODO: Once we have call site specific value information we can provide
1956 // call site specific liveness information and then it makes
1957 // sense to specialize attributes for call sites arguments instead of
1958 // redirecting requests to the callee argument.
1959 Function *F = getAssociatedFunction();
1960 const IRPosition &FnPos = IRPosition::function(*F);
1961 auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos, DepClassTy::REQUIRED);
1962 return clampStateAndIndicateChange(getState(), FnAA.getState());
1963 }
1964
1965 /// See AbstractAttribute::trackStatistics()
1966 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
); }
; }
1967};
1968
1969/// NoFree attribute for floating values.
1970struct AANoFreeFloating : AANoFreeImpl {
1971 AANoFreeFloating(const IRPosition &IRP, Attributor &A)
1972 : AANoFreeImpl(IRP, A) {}
1973
1974 /// See AbstractAttribute::trackStatistics()
1975 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); }
}
1976
1977 /// See Abstract Attribute::updateImpl(...).
1978 ChangeStatus updateImpl(Attributor &A) override {
1979 const IRPosition &IRP = getIRPosition();
1980
1981 const auto &NoFreeAA = A.getAAFor<AANoFree>(
1982 *this, IRPosition::function_scope(IRP), DepClassTy::OPTIONAL);
1983 if (NoFreeAA.isAssumedNoFree())
1984 return ChangeStatus::UNCHANGED;
1985
1986 Value &AssociatedValue = getIRPosition().getAssociatedValue();
1987 auto Pred = [&](const Use &U, bool &Follow) -> bool {
1988 Instruction *UserI = cast<Instruction>(U.getUser());
1989 if (auto *CB = dyn_cast<CallBase>(UserI)) {
1990 if (CB->isBundleOperand(&U))
1991 return false;
1992 if (!CB->isArgOperand(&U))
1993 return true;
1994 unsigned ArgNo = CB->getArgOperandNo(&U);
1995
1996 const auto &NoFreeArg = A.getAAFor<AANoFree>(
1997 *this, IRPosition::callsite_argument(*CB, ArgNo),
1998 DepClassTy::REQUIRED);
1999 return NoFreeArg.isAssumedNoFree();
2000 }
2001
2002 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
2003 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
2004 Follow = true;
2005 return true;
2006 }
2007 if (isa<StoreInst>(UserI) || isa<LoadInst>(UserI) ||
2008 isa<ReturnInst>(UserI))
2009 return true;
2010
2011 // Unknown user.
2012 return false;
2013 };
2014 if (!A.checkForAllUses(Pred, *this, AssociatedValue))
2015 return indicatePessimisticFixpoint();
2016
2017 return ChangeStatus::UNCHANGED;
2018 }
2019};
2020
2021/// NoFree attribute for a call site argument.
2022struct AANoFreeArgument final : AANoFreeFloating {
2023 AANoFreeArgument(const IRPosition &IRP, Attributor &A)
2024 : AANoFreeFloating(IRP, A) {}
2025
2026 /// See AbstractAttribute::trackStatistics()
2027 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree){ static llvm::Statistic NumIRArguments_nofree = {"attributor"
, "NumIRArguments_nofree", ("Number of " "arguments" " marked '"
"nofree" "'")};; ++(NumIRArguments_nofree); }
}
2028};
2029
2030/// NoFree attribute for call site arguments.
2031struct AANoFreeCallSiteArgument final : AANoFreeFloating {
2032 AANoFreeCallSiteArgument(const IRPosition &IRP, Attributor &A)
2033 : AANoFreeFloating(IRP, A) {}
2034
2035 /// See AbstractAttribute::updateImpl(...).
2036 ChangeStatus updateImpl(Attributor &A) override {
2037 // TODO: Once we have call site specific value information we can provide
2038 // call site specific liveness information and then it makes
2039 // sense to specialize attributes for call sites arguments instead of
2040 // redirecting requests to the callee argument.
2041 Argument *Arg = getAssociatedArgument();
2042 if (!Arg)
2043 return indicatePessimisticFixpoint();
2044 const IRPosition &ArgPos = IRPosition::argument(*Arg);
2045 auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos, DepClassTy::REQUIRED);
2046 return clampStateAndIndicateChange(getState(), ArgAA.getState());
2047 }
2048
2049 /// See AbstractAttribute::trackStatistics()
2050 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); }
};
2051};
2052
2053/// NoFree attribute for function return value.
2054struct AANoFreeReturned final : AANoFreeFloating {
2055 AANoFreeReturned(const IRPosition &IRP, Attributor &A)
2056 : AANoFreeFloating(IRP, A) {
2057 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", 2057)
;
2058 }
2059
2060 /// See AbstractAttribute::initialize(...).
2061 void initialize(Attributor &A) override {
2062 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", 2062)
;
2063 }
2064
2065 /// See AbstractAttribute::updateImpl(...).
2066 ChangeStatus updateImpl(Attributor &A) override {
2067 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", 2067)
;
2068 }
2069
2070 /// See AbstractAttribute::trackStatistics()
2071 void trackStatistics() const override {}
2072};
2073
2074/// NoFree attribute deduction for a call site return value.
2075struct AANoFreeCallSiteReturned final : AANoFreeFloating {
2076 AANoFreeCallSiteReturned(const IRPosition &IRP, Attributor &A)
2077 : AANoFreeFloating(IRP, A) {}
2078
2079 ChangeStatus manifest(Attributor &A) override {
2080 return ChangeStatus::UNCHANGED;
2081 }
2082 /// See AbstractAttribute::trackStatistics()
2083 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); }
}
2084};
2085
2086/// ------------------------ NonNull Argument Attribute ------------------------
2087static int64_t getKnownNonNullAndDerefBytesForUse(
2088 Attributor &A, const AbstractAttribute &QueryingAA, Value &AssociatedValue,
2089 const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
2090 TrackUse = false;
2091
2092 const Value *UseV = U->get();
2093 if (!UseV->getType()->isPointerTy())
2094 return 0;
2095
2096 // We need to follow common pointer manipulation uses to the accesses they
2097 // feed into. We can try to be smart to avoid looking through things we do not
2098 // like for now, e.g., non-inbounds GEPs.
2099 if (isa<CastInst>(I)) {
2100 TrackUse = true;
2101 return 0;
2102 }
2103
2104 if (isa<GetElementPtrInst>(I)) {
2105 TrackUse = true;
2106 return 0;
2107 }
2108
2109 Type *PtrTy = UseV->getType();
2110 const Function *F = I->getFunction();
2111 bool NullPointerIsDefined =
2112 F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
2113 const DataLayout &DL = A.getInfoCache().getDL();
2114 if (const auto *CB = dyn_cast<CallBase>(I)) {
2115 if (CB->isBundleOperand(U)) {
2116 if (RetainedKnowledge RK = getKnowledgeFromUse(
2117 U, {Attribute::NonNull, Attribute::Dereferenceable})) {
2118 IsNonNull |=
2119 (RK.AttrKind == Attribute::NonNull || !NullPointerIsDefined);
2120 return RK.ArgValue;
2121 }
2122 return 0;
2123 }
2124
2125 if (CB->isCallee(U)) {
2126 IsNonNull |= !NullPointerIsDefined;
2127 return 0;
2128 }
2129
2130 unsigned ArgNo = CB->getArgOperandNo(U);
2131 IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
2132 // As long as we only use known information there is no need to track
2133 // dependences here.
2134 auto &DerefAA =
2135 A.getAAFor<AADereferenceable>(QueryingAA, IRP, DepClassTy::NONE);
2136 IsNonNull |= DerefAA.isKnownNonNull();
2137 return DerefAA.getKnownDereferenceableBytes();
2138 }
2139
2140 Optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I);
2141 if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile())
2142 return 0;
2143
2144 int64_t Offset;
2145 const Value *Base =
2146 getMinimalBaseOfPointer(A, QueryingAA, Loc->Ptr, Offset, DL);
2147 if (Base && Base == &AssociatedValue) {
2148 int64_t DerefBytes = Loc->Size.getValue() + Offset;
2149 IsNonNull |= !NullPointerIsDefined;
2150 return std::max(int64_t(0), DerefBytes);
2151 }
2152
2153 /// Corner case when an offset is 0.
2154 Base = GetPointerBaseWithConstantOffset(Loc->Ptr, Offset, DL,
2155 /*AllowNonInbounds*/ true);
2156 if (Base && Base == &AssociatedValue && Offset == 0) {
2157 int64_t DerefBytes = Loc->Size.getValue();
2158 IsNonNull |= !NullPointerIsDefined;
2159 return std::max(int64_t(0), DerefBytes);
2160 }
2161
2162 return 0;
2163}
2164
2165struct AANonNullImpl : AANonNull {
2166 AANonNullImpl(const IRPosition &IRP, Attributor &A)
2167 : AANonNull(IRP, A),
2168 NullIsDefined(NullPointerIsDefined(
2169 getAnchorScope(),
2170 getAssociatedValue().getType()->getPointerAddressSpace())) {}
2171
2172 /// See AbstractAttribute::initialize(...).
2173 void initialize(Attributor &A) override {
2174 Value &V = getAssociatedValue();
2175 if (!NullIsDefined &&
2176 hasAttr({Attribute::NonNull, Attribute::Dereferenceable},
2177 /* IgnoreSubsumingPositions */ false, &A)) {
2178 indicateOptimisticFixpoint();
2179 return;
2180 }
2181
2182 if (isa<ConstantPointerNull>(V)) {
2183 indicatePessimisticFixpoint();
2184 return;
2185 }
2186
2187 AANonNull::initialize(A);
2188
2189 bool CanBeNull, CanBeFreed;
2190 if (V.getPointerDereferenceableBytes(A.getDataLayout(), CanBeNull,
2191 CanBeFreed)) {
2192 if (!CanBeNull) {
2193 indicateOptimisticFixpoint();
2194 return;
2195 }
2196 }
2197
2198 if (isa<GlobalValue>(&getAssociatedValue())) {
2199 indicatePessimisticFixpoint();
2200 return;
2201 }
2202
2203 if (Instruction *CtxI = getCtxI())
2204 followUsesInMBEC(*this, A, getState(), *CtxI);
2205 }
2206
2207 /// See followUsesInMBEC
2208 bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
2209 AANonNull::StateType &State) {
2210 bool IsNonNull = false;
2211 bool TrackUse = false;
2212 getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
2213 IsNonNull, TrackUse);
2214 State.setKnown(IsNonNull);
2215 return TrackUse;
2216 }
2217
2218 /// See AbstractAttribute::getAsStr().
2219 const std::string getAsStr() const override {
2220 return getAssumed() ? "nonnull" : "may-null";
2221 }
2222
2223 /// Flag to determine if the underlying value can be null and still allow
2224 /// valid accesses.
2225 const bool NullIsDefined;
2226};
2227
2228/// NonNull attribute for a floating value.
2229struct AANonNullFloating : public AANonNullImpl {
2230 AANonNullFloating(const IRPosition &IRP, Attributor &A)
2231 : AANonNullImpl(IRP, A) {}
2232
2233 /// See AbstractAttribute::updateImpl(...).
2234 ChangeStatus updateImpl(Attributor &A) override {
2235 const DataLayout &DL = A.getDataLayout();
2236
2237 DominatorTree *DT = nullptr;
2238 AssumptionCache *AC = nullptr;
2239 InformationCache &InfoCache = A.getInfoCache();
2240 if (const Function *Fn = getAnchorScope()) {
2241 DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
2242 AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*Fn);
2243 }
2244
2245 auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
2246 AANonNull::StateType &T, bool Stripped) -> bool {
2247 const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V),
2248 DepClassTy::REQUIRED);
2249 if (!Stripped && this == &AA) {
2250 if (!isKnownNonZero(&V, DL, 0, AC, CtxI, DT))
2251 T.indicatePessimisticFixpoint();
2252 } else {
2253 // Use abstract attribute information.
2254 const AANonNull::StateType &NS = AA.getState();
2255 T ^= NS;
2256 }
2257 return T.isValidState();
2258 };
2259
2260 StateType T;
2261 if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
2262 VisitValueCB, getCtxI()))
2263 return indicatePessimisticFixpoint();
2264
2265 return clampStateAndIndicateChange(getState(), T);
2266 }
2267
2268 /// See AbstractAttribute::trackStatistics()
2269 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
); }
}
2270};
2271
2272/// NonNull attribute for function return value.
2273struct AANonNullReturned final
2274 : AAReturnedFromReturnedValues<AANonNull, AANonNull> {
2275 AANonNullReturned(const IRPosition &IRP, Attributor &A)
2276 : AAReturnedFromReturnedValues<AANonNull, AANonNull>(IRP, A) {}
2277
2278 /// See AbstractAttribute::getAsStr().
2279 const std::string getAsStr() const override {
2280 return getAssumed() ? "nonnull" : "may-null";
2281 }
2282
2283 /// See AbstractAttribute::trackStatistics()
2284 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
); }
}
2285};
2286
2287/// NonNull attribute for function argument.
2288struct AANonNullArgument final
2289 : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> {
2290 AANonNullArgument(const IRPosition &IRP, Attributor &A)
2291 : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP, A) {}
2292
2293 /// See AbstractAttribute::trackStatistics()
2294 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull){ static llvm::Statistic NumIRArguments_nonnull = {"attributor"
, "NumIRArguments_nonnull", ("Number of " "arguments" " marked '"
"nonnull" "'")};; ++(NumIRArguments_nonnull); }
}
2295};
2296
2297struct AANonNullCallSiteArgument final : AANonNullFloating {
2298 AANonNullCallSiteArgument(const IRPosition &IRP, Attributor &A)
2299 : AANonNullFloating(IRP, A) {}
2300
2301 /// See AbstractAttribute::trackStatistics()
2302 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); }
}
2303};
2304
2305/// NonNull attribute for a call site return position.
2306struct AANonNullCallSiteReturned final
2307 : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> {
2308 AANonNullCallSiteReturned(const IRPosition &IRP, Attributor &A)
2309 : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP, A) {}
2310
2311 /// See AbstractAttribute::trackStatistics()
2312 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); }
}
2313};
2314
2315/// ------------------------ No-Recurse Attributes ----------------------------
2316
2317struct AANoRecurseImpl : public AANoRecurse {
2318 AANoRecurseImpl(const IRPosition &IRP, Attributor &A) : AANoRecurse(IRP, A) {}
2319
2320 /// See AbstractAttribute::getAsStr()
2321 const std::string getAsStr() const override {
2322 return getAssumed() ? "norecurse" : "may-recurse";
2323 }
2324};
2325
2326struct AANoRecurseFunction final : AANoRecurseImpl {
2327 AANoRecurseFunction(const IRPosition &IRP, Attributor &A)
2328 : AANoRecurseImpl(IRP, A) {}
2329
2330 /// See AbstractAttribute::initialize(...).
2331 void initialize(Attributor &A) override {
2332 AANoRecurseImpl::initialize(A);
2333 // TODO: We should build a call graph ourselves to enable this in the module
2334 // pass as well.
2335 if (const Function *F = getAnchorScope())
2336 if (A.getInfoCache().getSccSize(*F) != 1)
2337 indicatePessimisticFixpoint();
2338 }
2339
2340 /// See AbstractAttribute::updateImpl(...).
2341 ChangeStatus updateImpl(Attributor &A) override {
2342
2343 // If all live call sites are known to be no-recurse, we are as well.
2344 auto CallSitePred = [&](AbstractCallSite ACS) {
2345 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
2346 *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
2347 DepClassTy::NONE);
2348 return NoRecurseAA.isKnownNoRecurse();
2349 };
2350 bool AllCallSitesKnown;
2351 if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) {
2352 // If we know all call sites and all are known no-recurse, we are done.
2353 // If all known call sites, which might not be all that exist, are known
2354 // to be no-recurse, we are not done but we can continue to assume
2355 // no-recurse. If one of the call sites we have not visited will become
2356 // live, another update is triggered.
2357 if (AllCallSitesKnown)
2358 indicateOptimisticFixpoint();
2359 return ChangeStatus::UNCHANGED;
2360 }
2361
2362 // If the above check does not hold anymore we look at the calls.
2363 auto CheckForNoRecurse = [&](Instruction &I) {
2364 const auto &CB = cast<CallBase>(I);
2365 if (CB.hasFnAttr(Attribute::NoRecurse))
2366 return true;
2367
2368 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
2369 *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
2370 if (!NoRecurseAA.isAssumedNoRecurse())
2371 return false;
2372
2373 // Recursion to the same function
2374 if (CB.getCalledFunction() == getAnchorScope())
2375 return false;
2376
2377 return true;
2378 };
2379
2380 bool UsedAssumedInformation = false;
2381 if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this,
2382 UsedAssumedInformation))
2383 return indicatePessimisticFixpoint();
2384 return ChangeStatus::UNCHANGED;
2385 }
2386
2387 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse){ static llvm::Statistic NumIRFunction_norecurse = {"attributor"
, "NumIRFunction_norecurse", ("Number of " "functions" " marked '"
"norecurse" "'")};; ++(NumIRFunction_norecurse); }
}
2388};
2389
2390/// NoRecurse attribute deduction for a call sites.
2391struct AANoRecurseCallSite final : AANoRecurseImpl {
2392 AANoRecurseCallSite(const IRPosition &IRP, Attributor &A)
2393 : AANoRecurseImpl(IRP, A) {}
2394
2395 /// See AbstractAttribute::initialize(...).
2396 void initialize(Attributor &A) override {
2397 AANoRecurseImpl::initialize(A);
2398 Function *F = getAssociatedFunction();
2399 if (!F || F->isDeclaration())
2400 indicatePessimisticFixpoint();
2401 }
2402
2403 /// See AbstractAttribute::updateImpl(...).
2404 ChangeStatus updateImpl(Attributor &A) override {
2405 // TODO: Once we have call site specific value information we can provide
2406 // call site specific liveness information and then it makes
2407 // sense to specialize attributes for call sites arguments instead of
2408 // redirecting requests to the callee argument.
2409 Function *F = getAssociatedFunction();
2410 const IRPosition &FnPos = IRPosition::function(*F);
2411 auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos, DepClassTy::REQUIRED);
2412 return clampStateAndIndicateChange(getState(), FnAA.getState());
2413 }
2414
2415 /// See AbstractAttribute::trackStatistics()
2416 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); }
; }
2417};
2418
2419/// -------------------- Undefined-Behavior Attributes ------------------------
2420
2421struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
2422 AAUndefinedBehaviorImpl(const IRPosition &IRP, Attributor &A)
2423 : AAUndefinedBehavior(IRP, A) {}
2424
2425 /// See AbstractAttribute::updateImpl(...).
2426 // through a pointer (i.e. also branches etc.)
2427 ChangeStatus updateImpl(Attributor &A) override {
2428 const size_t UBPrevSize = KnownUBInsts.size();
2429 const size_t NoUBPrevSize = AssumedNoUBInsts.size();
2430
2431 auto InspectMemAccessInstForUB = [&](Instruction &I) {
2432 // Lang ref now states volatile store is not UB, let's skip them.
2433 if (I.isVolatile() && I.mayWriteToMemory())
2434 return true;
2435
2436 // Skip instructions that are already saved.
2437 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2438 return true;
2439
2440 // If we reach here, we know we have an instruction
2441 // that accesses memory through a pointer operand,
2442 // for which getPointerOperand() should give it to us.
2443 Value *PtrOp =
2444 const_cast<Value *>(getPointerOperand(&I, /* AllowVolatile */ true));
2445 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", 2446, __extension__
__PRETTY_FUNCTION__))
2446 "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", 2446, __extension__
__PRETTY_FUNCTION__))
;
2447
2448 // Either we stopped and the appropriate action was taken,
2449 // or we got back a simplified value to continue.
2450 Optional<Value *> SimplifiedPtrOp = stopOnUndefOrAssumed(A, PtrOp, &I);
2451 if (!SimplifiedPtrOp.hasValue() || !SimplifiedPtrOp.getValue())
2452 return true;
2453 const Value *PtrOpVal = SimplifiedPtrOp.getValue();
2454
2455 // A memory access through a pointer is considered UB
2456 // only if the pointer has constant null value.
2457 // TODO: Expand it to not only check constant values.
2458 if (!isa<ConstantPointerNull>(PtrOpVal)) {
2459 AssumedNoUBInsts.insert(&I);
2460 return true;
2461 }
2462 const Type *PtrTy = PtrOpVal->getType();
2463
2464 // Because we only consider instructions inside functions,
2465 // assume that a parent function exists.
2466 const Function *F = I.getFunction();
2467
2468 // A memory access using constant null pointer is only considered UB
2469 // if null pointer is _not_ defined for the target platform.
2470 if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
2471 AssumedNoUBInsts.insert(&I);
2472 else
2473 KnownUBInsts.insert(&I);
2474 return true;
2475 };
2476
2477 auto InspectBrInstForUB = [&](Instruction &I) {
2478 // A conditional branch instruction is considered UB if it has `undef`
2479 // condition.
2480
2481 // Skip instructions that are already saved.
2482 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2483 return true;
2484
2485 // We know we have a branch instruction.
2486 auto *BrInst = cast<BranchInst>(&I);
2487
2488 // Unconditional branches are never considered UB.
2489 if (BrInst->isUnconditional())
2490 return true;
2491
2492 // Either we stopped and the appropriate action was taken,
2493 // or we got back a simplified value to continue.
2494 Optional<Value *> SimplifiedCond =
2495 stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
2496 if (!SimplifiedCond.hasValue() || !SimplifiedCond.getValue())
2497 return true;
2498 AssumedNoUBInsts.insert(&I);
2499 return true;
2500 };
2501
2502 auto InspectCallSiteForUB = [&](Instruction &I) {
2503 // Check whether a callsite always cause UB or not
2504
2505 // Skip instructions that are already saved.
2506 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2507 return true;
2508
2509 // Check nonnull and noundef argument attribute violation for each
2510 // callsite.
2511 CallBase &CB = cast<CallBase>(I);
2512 Function *Callee = CB.getCalledFunction();
2513 if (!Callee)
2514 return true;
2515 for (unsigned idx = 0; idx < CB.arg_size(); idx++) {
2516 // If current argument is known to be simplified to null pointer and the
2517 // corresponding argument position is known to have nonnull attribute,
2518 // the argument is poison. Furthermore, if the argument is poison and
2519 // the position is known to have noundef attriubte, this callsite is
2520 // considered UB.
2521 if (idx >= Callee->arg_size())
2522 break;
2523 Value *ArgVal = CB.getArgOperand(idx);
2524 if (!ArgVal)
2525 continue;
2526 // Here, we handle three cases.
2527 // (1) Not having a value means it is dead. (we can replace the value
2528 // with undef)
2529 // (2) Simplified to undef. The argument violate noundef attriubte.
2530 // (3) Simplified to null pointer where known to be nonnull.
2531 // The argument is a poison value and violate noundef attribute.
2532 IRPosition CalleeArgumentIRP = IRPosition::callsite_argument(CB, idx);
2533 auto &NoUndefAA =
2534 A.getAAFor<AANoUndef>(*this, CalleeArgumentIRP, DepClassTy::NONE);
2535 if (!NoUndefAA.isKnownNoUndef())
2536 continue;
2537 bool UsedAssumedInformation = false;
2538 Optional<Value *> SimplifiedVal = A.getAssumedSimplified(
2539 IRPosition::value(*ArgVal), *this, UsedAssumedInformation);
2540 if (UsedAssumedInformation)
2541 continue;
2542 if (SimplifiedVal.hasValue() && !SimplifiedVal.getValue())
2543 return true;
2544 if (!SimplifiedVal.hasValue() ||
2545 isa<UndefValue>(*SimplifiedVal.getValue())) {
2546 KnownUBInsts.insert(&I);
2547 continue;
2548 }
2549 if (!ArgVal->getType()->isPointerTy() ||
2550 !isa<ConstantPointerNull>(*SimplifiedVal.getValue()))
2551 continue;
2552 auto &NonNullAA =
2553 A.getAAFor<AANonNull>(*this, CalleeArgumentIRP, DepClassTy::NONE);
2554 if (NonNullAA.isKnownNonNull())
2555 KnownUBInsts.insert(&I);
2556 }
2557 return true;
2558 };
2559
2560 auto InspectReturnInstForUB =
2561 [&](Value &V, const SmallSetVector<ReturnInst *, 4> RetInsts) {
2562 // Check if a return instruction always cause UB or not
2563 // Note: It is guaranteed that the returned position of the anchor
2564 // scope has noundef attribute when this is called.
2565 // We also ensure the return position is not "assumed dead"
2566 // because the returned value was then potentially simplified to
2567 // `undef` in AAReturnedValues without removing the `noundef`
2568 // attribute yet.
2569
2570 // When the returned position has noundef attriubte, UB occur in the
2571 // following cases.
2572 // (1) Returned value is known to be undef.
2573 // (2) The value is known to be a null pointer and the returned
2574 // position has nonnull attribute (because the returned value is
2575 // poison).
2576 bool FoundUB = false;
2577 if (isa<UndefValue>(V)) {
2578 FoundUB = true;
2579 } else {
2580 if (isa<ConstantPointerNull>(V)) {
2581 auto &NonNullAA = A.getAAFor<AANonNull>(
2582 *this, IRPosition::returned(*getAnchorScope()),
2583 DepClassTy::NONE);
2584 if (NonNullAA.isKnownNonNull())
2585 FoundUB = true;
2586 }
2587 }
2588
2589 if (FoundUB)
2590 for (ReturnInst *RI : RetInsts)
2591 KnownUBInsts.insert(RI);
2592 return true;
2593 };
2594
2595 bool UsedAssumedInformation = false;
2596 A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
2597 {Instruction::Load, Instruction::Store,
2598 Instruction::AtomicCmpXchg,
2599 Instruction::AtomicRMW},
2600 UsedAssumedInformation,
2601 /* CheckBBLivenessOnly */ true);
2602 A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
2603 UsedAssumedInformation,
2604 /* CheckBBLivenessOnly */ true);
2605 A.checkForAllCallLikeInstructions(InspectCallSiteForUB, *this,
2606 UsedAssumedInformation);
2607
2608 // If the returned position of the anchor scope has noundef attriubte, check
2609 // all returned instructions.
2610 if (!getAnchorScope()->getReturnType()->isVoidTy()) {
2611 const IRPosition &ReturnIRP = IRPosition::returned(*getAnchorScope());
2612 if (!A.isAssumedDead(ReturnIRP, this, nullptr, UsedAssumedInformation)) {
2613 auto &RetPosNoUndefAA =
2614 A.getAAFor<AANoUndef>(*this, ReturnIRP, DepClassTy::NONE);
2615 if (RetPosNoUndefAA.isKnownNoUndef())
2616 A.checkForAllReturnedValuesAndReturnInsts(InspectReturnInstForUB,
2617 *this);
2618 }
2619 }
2620
2621 if (NoUBPrevSize != AssumedNoUBInsts.size() ||
2622 UBPrevSize != KnownUBInsts.size())
2623 return ChangeStatus::CHANGED;
2624 return ChangeStatus::UNCHANGED;
2625 }
2626
2627 bool isKnownToCauseUB(Instruction *I) const override {
2628 return KnownUBInsts.count(I);
2629 }
2630
2631 bool isAssumedToCauseUB(Instruction *I) const override {
2632 // In simple words, if an instruction is not in the assumed to _not_
2633 // cause UB, then it is assumed UB (that includes those
2634 // in the KnownUBInsts set). The rest is boilerplate
2635 // is to ensure that it is one of the instructions we test
2636 // for UB.
2637
2638 switch (I->getOpcode()) {
2639 case Instruction::Load:
2640 case Instruction::Store:
2641 case Instruction::AtomicCmpXchg:
2642 case Instruction::AtomicRMW:
2643 return !AssumedNoUBInsts.count(I);
2644 case Instruction::Br: {
2645 auto BrInst = cast<BranchInst>(I);
2646 if (BrInst->isUnconditional())
2647 return false;
2648 return !AssumedNoUBInsts.count(I);
2649 } break;
2650 default:
2651 return false;
2652 }
2653 return false;
2654 }
2655
2656 ChangeStatus manifest(Attributor &A) override {
2657 if (KnownUBInsts.empty())
2658 return ChangeStatus::UNCHANGED;
2659 for (Instruction *I : KnownUBInsts)
2660 A.changeToUnreachableAfterManifest(I);
2661 return ChangeStatus::CHANGED;
2662 }
2663
2664 /// See AbstractAttribute::getAsStr()
2665 const std::string getAsStr() const override {
2666 return getAssumed() ? "undefined-behavior" : "no-ub";
2667 }
2668
2669 /// Note: The correctness of this analysis depends on the fact that the
2670 /// following 2 sets will stop changing after some point.
2671 /// "Change" here means that their size changes.
2672 /// The size of each set is monotonically increasing
2673 /// (we only add items to them) and it is upper bounded by the number of
2674 /// instructions in the processed function (we can never save more
2675 /// elements in either set than this number). Hence, at some point,
2676 /// they will stop increasing.
2677 /// Consequently, at some point, both sets will have stopped
2678 /// changing, effectively making the analysis reach a fixpoint.
2679
2680 /// Note: These 2 sets are disjoint and an instruction can be considered
2681 /// one of 3 things:
2682 /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
2683 /// the KnownUBInsts set.
2684 /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
2685 /// has a reason to assume it).
2686 /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
2687 /// could not find a reason to assume or prove that it can cause UB,
2688 /// hence it assumes it doesn't. We have a set for these instructions
2689 /// so that we don't reprocess them in every update.
2690 /// Note however that instructions in this set may cause UB.
2691
2692protected:
2693 /// A set of all live instructions _known_ to cause UB.
2694 SmallPtrSet<Instruction *, 8> KnownUBInsts;
2695
2696private:
2697 /// A set of all the (live) instructions that are assumed to _not_ cause UB.
2698 SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
2699
2700 // Should be called on updates in which if we're processing an instruction
2701 // \p I that depends on a value \p V, one of the following has to happen:
2702 // - If the value is assumed, then stop.
2703 // - If the value is known but undef, then consider it UB.
2704 // - Otherwise, do specific processing with the simplified value.
2705 // We return None in the first 2 cases to signify that an appropriate
2706 // action was taken and the caller should stop.
2707 // Otherwise, we return the simplified value that the caller should
2708 // use for specific processing.
2709 Optional<Value *> stopOnUndefOrAssumed(Attributor &A, Value *V,
2710 Instruction *I) {
2711 bool UsedAssumedInformation = false;
2712 Optional<Value *> SimplifiedV = A.getAssumedSimplified(
2713 IRPosition::value(*V), *this, UsedAssumedInformation);
2714 if (!UsedAssumedInformation) {
2715 // Don't depend on assumed values.
2716 if (!SimplifiedV.hasValue()) {
2717 // If it is known (which we tested above) but it doesn't have a value,
2718 // then we can assume `undef` and hence the instruction is UB.
2719 KnownUBInsts.insert(I);
2720 return llvm::None;
2721 }
2722 if (!SimplifiedV.getValue())
2723 return nullptr;
2724 V = *SimplifiedV;
2725 }
2726 if (isa<UndefValue>(V)) {
2727 KnownUBInsts.insert(I);
2728 return llvm::None;
2729 }
2730 return V;
2731 }
2732};
2733
2734struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
2735 AAUndefinedBehaviorFunction(const IRPosition &IRP, Attributor &A)
2736 : AAUndefinedBehaviorImpl(IRP, A) {}
2737
2738 /// See AbstractAttribute::trackStatistics()
2739 void trackStatistics() const override {
2740 STATS_DECL(UndefinedBehaviorInstruction, Instruction,static llvm::Statistic NumIRInstruction_UndefinedBehaviorInstruction
= {"attributor", "NumIRInstruction_UndefinedBehaviorInstruction"
, "Number of instructions known to have UB"};;
2741 "Number of instructions known to have UB")static llvm::Statistic NumIRInstruction_UndefinedBehaviorInstruction
= {"attributor", "NumIRInstruction_UndefinedBehaviorInstruction"
, "Number of instructions known to have UB"};;
;
2742 BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction)NumIRInstruction_UndefinedBehaviorInstruction +=
2743 KnownUBInsts.size();
2744 }
2745};
2746
2747/// ------------------------ Will-Return Attributes ----------------------------
2748
2749// Helper function that checks whether a function has any cycle which we don't
2750// know if it is bounded or not.
2751// Loops with maximum trip count are considered bounded, any other cycle not.
2752static bool mayContainUnboundedCycle(Function &F, Attributor &A) {
2753 ScalarEvolution *SE =
2754 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F);
2755 LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F);
2756 // If either SCEV or LoopInfo is not available for the function then we assume
2757 // any cycle to be unbounded cycle.
2758 // We use scc_iterator which uses Tarjan algorithm to find all the maximal
2759 // SCCs.To detect if there's a cycle, we only need to find the maximal ones.
2760 if (!SE || !LI) {
2761 for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI)
2762 if (SCCI.hasCycle())
2763 return true;
2764 return false;
2765 }
2766
2767 // If there's irreducible control, the function may contain non-loop cycles.
2768 if (mayContainIrreducibleControl(F, LI))
2769 return true;
2770
2771 // Any loop that does not have a max trip count is considered unbounded cycle.
2772 for (auto *L : LI->getLoopsInPreorder()) {
2773 if (!SE->getSmallConstantMaxTripCount(L))
2774 return true;
2775 }
2776 return false;
2777}
2778
2779struct AAWillReturnImpl : public AAWillReturn {
2780 AAWillReturnImpl(const IRPosition &IRP, Attributor &A)
2781 : AAWillReturn(IRP, A) {}
2782
2783 /// See AbstractAttribute::initialize(...).
2784 void initialize(Attributor &A) override {
2785 AAWillReturn::initialize(A);
2786
2787 if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ true)) {
2788 indicateOptimisticFixpoint();
2789 return;
2790 }
2791 }
2792
2793 /// Check for `mustprogress` and `readonly` as they imply `willreturn`.
2794 bool isImpliedByMustprogressAndReadonly(Attributor &A, bool KnownOnly) {
2795 // Check for `mustprogress` in the scope and the associated function which
2796 // might be different if this is a call site.
2797 if ((!getAnchorScope() || !getAnchorScope()->mustProgress()) &&
2798 (!getAssociatedFunction() || !getAssociatedFunction()->mustProgress()))
2799 return false;
2800
2801 const auto &MemAA =
2802 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
2803 if (!MemAA.isAssumedReadOnly())
2804 return false;
2805 if (KnownOnly && !MemAA.isKnownReadOnly())
2806 return false;
2807 if (!MemAA.isKnownReadOnly())
2808 A.recordDependence(MemAA, *this, DepClassTy::OPTIONAL);
2809
2810 return true;
2811 }
2812
2813 /// See AbstractAttribute::updateImpl(...).
2814 ChangeStatus updateImpl(Attributor &A) override {
2815 if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
2816 return ChangeStatus::UNCHANGED;
2817
2818 auto CheckForWillReturn = [&](Instruction &I) {
2819 IRPosition IPos = IRPosition::callsite_function(cast<CallBase>(I));
2820 const auto &WillReturnAA =
2821 A.getAAFor<AAWillReturn>(*this, IPos, DepClassTy::REQUIRED);
2822 if (WillReturnAA.isKnownWillReturn())
2823 return true;
2824 if (!WillReturnAA.isAssumedWillReturn())
2825 return false;
2826 const auto &NoRecurseAA =
2827 A.getAAFor<AANoRecurse>(*this, IPos, DepClassTy::REQUIRED);
2828 return NoRecurseAA.isAssumedNoRecurse();
2829 };
2830
2831 bool UsedAssumedInformation = false;
2832 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this,
2833 UsedAssumedInformation))
2834 return indicatePessimisticFixpoint();
2835
2836 return ChangeStatus::UNCHANGED;
2837 }
2838
2839 /// See AbstractAttribute::getAsStr()
2840 const std::string getAsStr() const override {
2841 return getAssumed() ? "willreturn" : "may-noreturn";
2842 }
2843};
2844
2845struct AAWillReturnFunction final : AAWillReturnImpl {
2846 AAWillReturnFunction(const IRPosition &IRP, Attributor &A)
2847 : AAWillReturnImpl(IRP, A) {}
2848
2849 /// See AbstractAttribute::initialize(...).
2850 void initialize(Attributor &A) override {
2851 AAWillReturnImpl::initialize(A);
2852
2853 Function *F = getAnchorScope();
2854 if (!F || F->isDeclaration() || mayContainUnboundedCycle(*F, A))
2855 indicatePessimisticFixpoint();
2856 }
2857
2858 /// See AbstractAttribute::trackStatistics()
2859 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn){ static llvm::Statistic NumIRFunction_willreturn = {"attributor"
, "NumIRFunction_willreturn", ("Number of " "functions" " marked '"
"willreturn" "'")};; ++(NumIRFunction_willreturn); }
}
2860};
2861
2862/// WillReturn attribute deduction for a call sites.
2863struct AAWillReturnCallSite final : AAWillReturnImpl {
2864 AAWillReturnCallSite(const IRPosition &IRP, Attributor &A)
2865 : AAWillReturnImpl(IRP, A) {}
2866
2867 /// See AbstractAttribute::initialize(...).
2868 void initialize(Attributor &A) override {
2869 AAWillReturnImpl::initialize(A);
2870 Function *F = getAssociatedFunction();
2871 if (!F || !A.isFunctionIPOAmendable(*F))
2872 indicatePessimisticFixpoint();
2873 }
2874
2875 /// See AbstractAttribute::updateImpl(...).
2876 ChangeStatus updateImpl(Attributor &A) override {
2877 if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
2878 return ChangeStatus::UNCHANGED;
2879
2880 // TODO: Once we have call site specific value information we can provide
2881 // call site specific liveness information and then it makes
2882 // sense to specialize attributes for call sites arguments instead of
2883 // redirecting requests to the callee argument.
2884 Function *F = getAssociatedFunction();
2885 const IRPosition &FnPos = IRPosition::function(*F);
2886 auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos, DepClassTy::REQUIRED);
2887 return clampStateAndIndicateChange(getState(), FnAA.getState());
2888 }
2889
2890 /// See AbstractAttribute::trackStatistics()
2891 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); }
; }
2892};
2893
2894/// -------------------AAReachability Attribute--------------------------
2895
2896struct AAReachabilityImpl : AAReachability {
2897 AAReachabilityImpl(const IRPosition &IRP, Attributor &A)
2898 : AAReachability(IRP, A) {}
2899
2900 const std::string getAsStr() const override {
2901 // TODO: Return the number of reachable queries.
2902 return "reachable";
2903 }
2904
2905 /// See AbstractAttribute::updateImpl(...).
2906 ChangeStatus updateImpl(Attributor &A) override {
2907 return ChangeStatus::UNCHANGED;
2908 }
2909};
2910
2911struct AAReachabilityFunction final : public AAReachabilityImpl {
2912 AAReachabilityFunction(const IRPosition &IRP, Attributor &A)
2913 : AAReachabilityImpl(IRP, A) {}
2914
2915 /// See AbstractAttribute::trackStatistics()
2916 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable){ static llvm::Statistic NumIRFunction_reachable = {"attributor"
, "NumIRFunction_reachable", ("Number of " "functions" " marked '"
"reachable" "'")};; ++(NumIRFunction_reachable); }
; }
2917};
2918
2919/// ------------------------ NoAlias Argument Attribute ------------------------
2920
2921struct AANoAliasImpl : AANoAlias {
2922 AANoAliasImpl(const IRPosition &IRP, Attributor &A) : AANoAlias(IRP, A) {
2923 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", 2924, __extension__
__PRETTY_FUNCTION__))
2924 "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", 2924, __extension__
__PRETTY_FUNCTION__))
;
2925 }
2926
2927 const std::string getAsStr() const override {
2928 return getAssumed() ? "noalias" : "may-alias";
2929 }
2930};
2931
2932/// NoAlias attribute for a floating value.
2933struct AANoAliasFloating final : AANoAliasImpl {
2934 AANoAliasFloating(const IRPosition &IRP, Attributor &A)
2935 : AANoAliasImpl(IRP, A) {}
2936
2937 /// See AbstractAttribute::initialize(...).
2938 void initialize(Attributor &A) override {
2939 AANoAliasImpl::initialize(A);
2940 Value *Val = &getAssociatedValue();
2941 do {
2942 CastInst *CI = dyn_cast<CastInst>(Val);
2943 if (!CI)
2944 break;
2945 Value *Base = CI->getOperand(0);
2946 if (!Base->hasOneUse())
2947 break;
2948 Val = Base;
2949 } while (true);
2950
2951 if (!Val->getType()->isPointerTy()) {
2952 indicatePessimisticFixpoint();
2953 return;
2954 }
2955
2956 if (isa<AllocaInst>(Val))
2957 indicateOptimisticFixpoint();
2958 else if (isa<ConstantPointerNull>(Val) &&
2959 !NullPointerIsDefined(getAnchorScope(),
2960 Val->getType()->getPointerAddressSpace()))
2961 indicateOptimisticFixpoint();
2962 else if (Val != &getAssociatedValue()) {
2963 const auto &ValNoAliasAA = A.getAAFor<AANoAlias>(
2964 *this, IRPosition::value(*Val), DepClassTy::OPTIONAL);
2965 if (ValNoAliasAA.isKnownNoAlias())
2966 indicateOptimisticFixpoint();
2967 }
2968 }
2969
2970 /// See AbstractAttribute::updateImpl(...).
2971 ChangeStatus updateImpl(Attributor &A) override {
2972 // TODO: Implement this.
2973 return indicatePessimisticFixpoint();
2974 }
2975
2976 /// See AbstractAttribute::trackStatistics()
2977 void trackStatistics() const override {
2978 STATS_DECLTRACK_FLOATING_ATTR(noalias){ static llvm::Statistic NumIRFloating_noalias = {"attributor"
, "NumIRFloating_noalias", ("Number of floating values known to be '"
"noalias" "'")};; ++(NumIRFloating_noalias); }
2979 }
2980};
2981
2982/// NoAlias attribute for an argument.
2983struct AANoAliasArgument final
2984 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
2985 using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
2986 AANoAliasArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
2987
2988 /// See AbstractAttribute::initialize(...).
2989 void initialize(Attributor &A) override {
2990 Base::initialize(A);
2991 // See callsite argument attribute and callee argument attribute.
2992 if (hasAttr({Attribute::ByVal}))
2993 indicateOptimisticFixpoint();
2994 }
2995
2996 /// See AbstractAttribute::update(...).
2997 ChangeStatus updateImpl(Attributor &A) override {
2998 // We have to make sure no-alias on the argument does not break
2999 // synchronization when this is a callback argument, see also [1] below.
3000 // If synchronization cannot be affected, we delegate to the base updateImpl
3001 // function, otherwise we give up for now.
3002
3003 // If the function is no-sync, no-alias cannot break synchronization.
3004 const auto &NoSyncAA =
3005 A.getAAFor<AANoSync>(*this, IRPosition::function_scope(getIRPosition()),
3006 DepClassTy::OPTIONAL);
3007 if (NoSyncAA.isAssumedNoSync())
3008 return Base::updateImpl(A);
3009
3010 // If the argument is read-only, no-alias cannot break synchronization.
3011 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
3012 *this, getIRPosition(), DepClassTy::OPTIONAL);
3013 if (MemBehaviorAA.isAssumedReadOnly())
3014 return Base::updateImpl(A);
3015
3016 // If the argument is never passed through callbacks, no-alias cannot break
3017 // synchronization.
3018 bool AllCallSitesKnown;
3019 if (A.checkForAllCallSites(
3020 [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
3021 true, AllCallSitesKnown))
3022 return Base::updateImpl(A);
3023
3024 // TODO: add no-alias but make sure it doesn't break synchronization by
3025 // introducing fake uses. See:
3026 // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
3027 // International Workshop on OpenMP 2018,
3028 // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
3029
3030 return indicatePessimisticFixpoint();
3031 }
3032
3033 /// See AbstractAttribute::trackStatistics()
3034 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias){ static llvm::Statistic NumIRArguments_noalias = {"attributor"
, "NumIRArguments_noalias", ("Number of " "arguments" " marked '"
"noalias" "'")};; ++(NumIRArguments_noalias); }
}
3035};
3036
3037struct AANoAliasCallSiteArgument final : AANoAliasImpl {
3038 AANoAliasCallSiteArgument(const IRPosition &IRP, Attributor &A)
3039 : AANoAliasImpl(IRP, A) {}
3040
3041 /// See AbstractAttribute::initialize(...).
3042 void initialize(Attributor &A) override {
3043 // See callsite argument attribute and callee argument attribute.
3044 const auto &CB = cast<CallBase>(getAnchorValue());
3045 if (CB.paramHasAttr(getCallSiteArgNo(), Attribute::NoAlias))
3046 indicateOptimisticFixpoint();
3047 Value &Val = getAssociatedValue();
3048 if (isa<ConstantPointerNull>(Val) &&
3049 !NullPointerIsDefined(getAnchorScope(),
3050 Val.getType()->getPointerAddressSpace()))
3051 indicateOptimisticFixpoint();
3052 }
3053
3054 /// Determine if the underlying value may alias with the call site argument
3055 /// \p OtherArgNo of \p ICS (= the underlying call site).
3056 bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
3057 const AAMemoryBehavior &MemBehaviorAA,
3058 const CallBase &CB, unsigned OtherArgNo) {
3059 // We do not need to worry about aliasing with the underlying IRP.
3060 if (this->getCalleeArgNo() == (int)OtherArgNo)
3061 return false;
3062
3063 // If it is not a pointer or pointer vector we do not alias.
3064 const Value *ArgOp = CB.getArgOperand(OtherArgNo);
3065 if (!ArgOp->getType()->isPtrOrPtrVectorTy())
3066 return false;
3067
3068 auto &CBArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
3069 *this, IRPosition::callsite_argument(CB, OtherArgNo), DepClassTy::NONE);
3070
3071 // If the argument is readnone, there is no read-write aliasing.
3072 if (CBArgMemBehaviorAA.isAssumedReadNone()) {
3073 A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
3074 return false;
3075 }
3076
3077 // If the argument is readonly and the underlying value is readonly, there
3078 // is no read-write aliasing.
3079 bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
3080 if (CBArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
3081 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3082 A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
3083 return false;
3084 }
3085
3086 // We have to utilize actual alias analysis queries so we need the object.
3087 if (!AAR)
3088 AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
3089
3090 // Try to rule it out at the call site.
3091 bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
3092 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)
3093 "callsite arguments: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[NoAliasCSArg] Check alias between "
"callsite arguments: " << getAssociatedValue() <<
" " << *ArgOp << " => " << (IsAliasing ?
"" : "no-") << "alias \n"; } } while (false)
3094 << getAssociatedValue() << " " << *ArgOp << " => "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[NoAliasCSArg] Check alias between "
"callsite arguments: " << getAssociatedValue() <<
" " << *ArgOp << " => " << (IsAliasing ?
"" : "no-") << "alias \n"; } } while (false)
3095 << (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)
;
3096
3097 return IsAliasing;
3098 }
3099
3100 bool
3101 isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
3102 const AAMemoryBehavior &MemBehaviorAA,
3103 const AANoAlias &NoAliasAA) {
3104 // We can deduce "noalias" if the following conditions hold.
3105 // (i) Associated value is assumed to be noalias in the definition.
3106 // (ii) Associated value is assumed to be no-capture in all the uses
3107 // possibly executed before this callsite.
3108 // (iii) There is no other pointer argument which could alias with the
3109 // value.
3110
3111 bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
3112 if (!AssociatedValueIsNoAliasAtDef) {
3113 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)
3114 << " 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)
;
3115 return false;
3116 }
3117
3118 A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL);
3119
3120 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
3121 const Function *ScopeFn = VIRP.getAnchorScope();
3122 auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, VIRP, DepClassTy::NONE);
3123 // Check whether the value is captured in the scope using AANoCapture.
3124 // Look at CFG and check only uses possibly executed before this
3125 // callsite.
3126 auto UsePred = [&](const Use &U, bool &Follow) -> bool {
3127 Instruction *UserI = cast<Instruction>(U.getUser());
3128
3129 // If UserI is the curr instruction and there is a single potential use of
3130 // the value in UserI we allow the use.
3131 // TODO: We should inspect the operands and allow those that cannot alias
3132 // with the value.
3133 if (UserI == getCtxI() && UserI->getNumOperands() == 1)
3134 return true;
3135
3136 if (ScopeFn) {
3137 const auto &ReachabilityAA = A.getAAFor<AAReachability>(
3138 *this, IRPosition::function(*ScopeFn), DepClassTy::OPTIONAL);
3139
3140 if (!ReachabilityAA.isAssumedReachable(A, *UserI, *getCtxI()))
3141 return true;
3142
3143 if (auto *CB = dyn_cast<CallBase>(UserI)) {
3144 if (CB->isArgOperand(&U)) {
3145
3146 unsigned ArgNo = CB->getArgOperandNo(&U);
3147
3148 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
3149 *this, IRPosition::callsite_argument(*CB, ArgNo),
3150 DepClassTy::OPTIONAL);
3151
3152 if (NoCaptureAA.isAssumedNoCapture())
3153 return true;
3154 }
3155 }
3156 }
3157
3158 // For cases which can potentially have more users
3159 if (isa<GetElementPtrInst>(U) || isa<BitCastInst>(U) || isa<PHINode>(U) ||
3160 isa<SelectInst>(U)) {
3161 Follow = true;
3162 return true;
3163 }
3164
3165 LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AANoAliasCSArg] Unknown user: "
<< *U << "\n"; } } while (false)
;
3166 return false;
3167 };
3168
3169 if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
3170 if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) {
3171 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AANoAliasCSArg] " <<
getAssociatedValue() << " cannot be noalias as it is potentially captured\n"
; } } while (false)
3172 dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AANoAliasCSArg] " <<
getAssociatedValue() << " cannot be noalias as it is potentially captured\n"
; } } while (false)
3173 << " 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)
;
3174 return false;
3175 }
3176 }
3177 A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
3178
3179 // Check there is no other pointer argument which could alias with the
3180 // value passed at this call site.
3181 // TODO: AbstractCallSite
3182 const auto &CB = cast<CallBase>(getAnchorValue());
3183 for (unsigned OtherArgNo = 0; OtherArgNo < CB.arg_size(); OtherArgNo++)
3184 if (mayAliasWithArgument(A, AAR, MemBehaviorAA, CB, OtherArgNo))
3185 return false;
3186
3187 return true;
3188 }
3189
3190 /// See AbstractAttribute::updateImpl(...).
3191 ChangeStatus updateImpl(Attributor &A) override {
3192 // If the argument is readnone we are done as there are no accesses via the
3193 // argument.
3194 auto &MemBehaviorAA =
3195 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
3196 if (MemBehaviorAA.isAssumedReadNone()) {
3197 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3198 return ChangeStatus::UNCHANGED;
3199 }
3200
3201 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
3202 const auto &NoAliasAA =
3203 A.getAAFor<AANoAlias>(*this, VIRP, DepClassTy::NONE);
3204
3205 AAResults *AAR = nullptr;
3206 if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
3207 NoAliasAA)) {
3208 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n"
; } } while (false)
3209 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)
;
3210 return ChangeStatus::UNCHANGED;
3211 }
3212
3213 return indicatePessimisticFixpoint();
3214 }
3215
3216 /// See AbstractAttribute::trackStatistics()
3217 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); }
}
3218};
3219
3220/// NoAlias attribute for function return value.
3221struct AANoAliasReturned final : AANoAliasImpl {
3222 AANoAliasReturned(const IRPosition &IRP, Attributor &A)
3223 : AANoAliasImpl(IRP, A) {}
3224
3225 /// See AbstractAttribute::initialize(...).
3226 void initialize(Attributor &A) override {
3227 AANoAliasImpl::initialize(A);
3228 Function *F = getAssociatedFunction();
3229 if (!F || F->isDeclaration())
3230 indicatePessimisticFixpoint();
3231 }
3232
3233 /// See AbstractAttribute::updateImpl(...).
3234 virtual ChangeStatus updateImpl(Attributor &A) override {
3235
3236 auto CheckReturnValue = [&](Value &RV) -> bool {
3237 if (Constant *C = dyn_cast<Constant>(&RV))
3238 if (C->isNullValue() || isa<UndefValue>(C))
3239 return true;
3240
3241 /// For now, we can only deduce noalias if we have call sites.
3242 /// FIXME: add more support.
3243 if (!isa<CallBase>(&RV))
3244 return false;
3245
3246 const IRPosition &RVPos = IRPosition::value(RV);
3247 const auto &NoAliasAA =
3248 A.getAAFor<AANoAlias>(*this, RVPos, DepClassTy::REQUIRED);
3249 if (!NoAliasAA.isAssumedNoAlias())
3250 return false;
3251
3252 const auto &NoCaptureAA =
3253 A.getAAFor<AANoCapture>(*this, RVPos, DepClassTy::REQUIRED);
3254 return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
3255 };
3256
3257 if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
3258 return indicatePessimisticFixpoint();
3259
3260 return ChangeStatus::UNCHANGED;
3261 }
3262
3263 /// See AbstractAttribute::trackStatistics()
3264 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
); }
}
3265};
3266
3267/// NoAlias attribute deduction for a call site return value.
3268struct AANoAliasCallSiteReturned final : AANoAliasImpl {
3269 AANoAliasCallSiteReturned(const IRPosition &IRP, Attributor &A)
3270 : AANoAliasImpl(IRP, A) {}
3271
3272 /// See AbstractAttribute::initialize(...).
3273 void initialize(Attributor &A) override {
3274 AANoAliasImpl::initialize(A);
3275 Function *F = getAssociatedFunction();
3276 if (!F || F->isDeclaration())
3277 indicatePessimisticFixpoint();
3278 }
3279
3280 /// See AbstractAttribute::updateImpl(...).
3281 ChangeStatus updateImpl(Attributor &A) override {
3282 // TODO: Once we have call site specific value information we can provide
3283 // call site specific liveness information and then it makes
3284 // sense to specialize attributes for call sites arguments instead of
3285 // redirecting requests to the callee argument.
3286 Function *F = getAssociatedFunction();
3287 const IRPosition &FnPos = IRPosition::returned(*F);
3288 auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos, DepClassTy::REQUIRED);
3289 return clampStateAndIndicateChange(getState(), FnAA.getState());
3290 }
3291
3292 /// See AbstractAttribute::trackStatistics()
3293 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); }
; }
3294};
3295
3296/// -------------------AAIsDead Function Attribute-----------------------
3297
3298struct AAIsDeadValueImpl : public AAIsDead {
3299 AAIsDeadValueImpl(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
3300
3301 /// See AAIsDead::isAssumedDead().
3302 bool isAssumedDead() const override { return isAssumed(IS_DEAD); }
3303
3304 /// See AAIsDead::isKnownDead().
3305 bool isKnownDead() const override { return isKnown(IS_DEAD); }
3306
3307 /// See AAIsDead::isAssumedDead(BasicBlock *).
3308 bool isAssumedDead(const BasicBlock *BB) const override { return false; }
3309
3310 /// See AAIsDead::isKnownDead(BasicBlock *).
3311 bool isKnownDead(const BasicBlock *BB) const override { return false; }
3312
3313 /// See AAIsDead::isAssumedDead(Instruction *I).
3314 bool isAssumedDead(const Instruction *I) const override {
3315 return I == getCtxI() && isAssumedDead();
3316 }
3317
3318 /// See AAIsDead::isKnownDead(Instruction *I).
3319 bool isKnownDead(const Instruction *I) const override {
3320 return isAssumedDead(I) && isKnownDead();
3321 }
3322
3323 /// See AbstractAttribute::getAsStr().
3324 const std::string getAsStr() const override {
3325 return isAssumedDead() ? "assumed-dead" : "assumed-live";
3326 }
3327
3328 /// Check if all uses are assumed dead.
3329 bool areAllUsesAssumedDead(Attributor &A, Value &V) {
3330 // Callers might not check the type, void has no uses.
3331 if (V.getType()->isVoidTy())
3332 return true;
3333
3334 // If we replace a value with a constant there are no uses left afterwards.
3335 if (!isa<Constant>(V)) {
3336 bool UsedAssumedInformation = false;
3337 Optional<Constant *> C =
3338 A.getAssumedConstant(V, *this, UsedAssumedInformation);
3339 if (!C.hasValue() || *C)
3340 return true;
3341 }
3342
3343 auto UsePred = [&](const Use &U, bool &Follow) { return false; };
3344 // Explicitly set the dependence class to required because we want a long
3345 // chain of N dependent instructions to be considered live as soon as one is
3346 // without going through N update cycles. This is not required for
3347 // correctness.
3348 return A.checkForAllUses(UsePred, *this, V, /* CheckBBLivenessOnly */ false,
3349 DepClassTy::REQUIRED);
3350 }
3351
3352 /// Determine if \p I is assumed to be side-effect free.
3353 bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
3354 if (!I || wouldInstructionBeTriviallyDead(I))
3355 return true;
3356
3357 auto *CB = dyn_cast<CallBase>(I);
3358 if (!CB || isa<IntrinsicInst>(CB))
3359 return false;
3360
3361 const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
3362 const auto &NoUnwindAA =
3363 A.getAndUpdateAAFor<AANoUnwind>(*this, CallIRP, DepClassTy::NONE);
3364 if (!NoUnwindAA.isAssumedNoUnwind())
3365 return false;
3366 if (!NoUnwindAA.isKnownNoUnwind())
3367 A.recordDependence(NoUnwindAA, *this, DepClassTy::OPTIONAL);
3368
3369 const auto &MemBehaviorAA =
3370 A.getAndUpdateAAFor<AAMemoryBehavior>(*this, CallIRP, DepClassTy::NONE);
3371 if (MemBehaviorAA.isAssumedReadOnly()) {
3372 if (!MemBehaviorAA.isKnownReadOnly())
3373 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3374 return true;
3375 }
3376 return false;
3377 }
3378};
3379
3380struct AAIsDeadFloating : public AAIsDeadValueImpl {
3381 AAIsDeadFloating(const IRPosition &IRP, Attributor &A)
3382 : AAIsDeadValueImpl(IRP, A) {}
3383
3384 /// See AbstractAttribute::initialize(...).
3385 void initialize(Attributor &A) override {
3386 if (isa<UndefValue>(getAssociatedValue())) {
3387 indicatePessimisticFixpoint();
3388 return;
3389 }
3390
3391 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3392 if (!isAssumedSideEffectFree(A, I)) {
3393 if (!isa_and_nonnull<StoreInst>(I))
3394 indicatePessimisticFixpoint();
3395 else
3396 removeAssumedBits(HAS_NO_EFFECT);
3397 }
3398 }
3399
3400 bool isDeadStore(Attributor &A, StoreInst &SI) {
3401 // Lang ref now states volatile store is not UB/dead, let's skip them.
3402 if (SI.isVolatile())
3403 return false;
3404
3405 bool UsedAssumedInformation = false;
3406 SmallSetVector<Value *, 4> PotentialCopies;
3407 if (!AA::getPotentialCopiesOfStoredValue(A, SI, PotentialCopies, *this,
3408 UsedAssumedInformation))
3409 return false;
3410 return llvm::all_of(PotentialCopies, [&](Value *V) {
3411 return A.isAssumedDead(IRPosition::value(*V), this, nullptr,
3412 UsedAssumedInformation);
3413 });
3414 }
3415
3416 /// See AbstractAttribute::updateImpl(...).
3417 ChangeStatus updateImpl(Attributor &A) override {
3418 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3419 if (auto *SI = dyn_cast_or_null<StoreInst>(I)) {
3420 if (!isDeadStore(A, *SI))
3421 return indicatePessimisticFixpoint();
3422 } else {
3423 if (!isAssumedSideEffectFree(A, I))
3424 return indicatePessimisticFixpoint();
3425 if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3426 return indicatePessimisticFixpoint();
3427 }
3428 return ChangeStatus::UNCHANGED;
3429 }
3430
3431 /// See AbstractAttribute::manifest(...).
3432 ChangeStatus manifest(Attributor &A) override {
3433 Value &V = getAssociatedValue();
3434 if (auto *I = dyn_cast<Instruction>(&V)) {
3435 // If we get here we basically know the users are all dead. We check if
3436 // isAssumedSideEffectFree returns true here again because it might not be
3437 // the case and only the users are dead but the instruction (=call) is
3438 // still needed.
3439 if (isa<StoreInst>(I) ||
3440 (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I))) {
3441 A.deleteAfterManifest(*I);
3442 return ChangeStatus::CHANGED;
3443 }
3444 }
3445 if (V.use_empty())
3446 return ChangeStatus::UNCHANGED;
3447
3448 bool UsedAssumedInformation = false;
3449 Optional<Constant *> C =
3450 A.getAssumedConstant(V, *this, UsedAssumedInformation);
3451 if (C.hasValue() && C.getValue())
3452 return ChangeStatus::UNCHANGED;
3453
3454 // Replace the value with undef as it is dead but keep droppable uses around
3455 // as they provide information we don't want to give up on just yet.
3456 UndefValue &UV = *UndefValue::get(V.getType());
3457 bool AnyChange =
3458 A.changeValueAfterManifest(V, UV, /* ChangeDropppable */ false);
3459 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3460 }
3461
3462 /// See AbstractAttribute::trackStatistics()
3463 void trackStatistics() const override {
3464 STATS_DECLTRACK_FLOATING_ATTR(IsDead){ static llvm::Statistic NumIRFloating_IsDead = {"attributor"
, "NumIRFloating_IsDead", ("Number of floating values known to be '"
"IsDead" "'")};; ++(NumIRFloating_IsDead); }
3465 }
3466};
3467
3468struct AAIsDeadArgument : public AAIsDeadFloating {
3469 AAIsDeadArgument(const IRPosition &IRP, Attributor &A)
3470 : AAIsDeadFloating(IRP, A) {}
3471
3472 /// See AbstractAttribute::initialize(...).
3473 void initialize(Attributor &A) override {
3474 if (!A.isFunctionIPOAmendable(*getAnchorScope()))
3475 indicatePessimisticFixpoint();
3476 }
3477
3478 /// See AbstractAttribute::manifest(...).
3479 ChangeStatus manifest(Attributor &A) override {
3480 ChangeStatus Changed = AAIsDeadFloating::manifest(A);
3481 Argument &Arg = *getAssociatedArgument();
3482 if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
3483 if (A.registerFunctionSignatureRewrite(
3484 Arg, /* ReplacementTypes */ {},
3485 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
3486 Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) {
3487 Arg.dropDroppableUses();
3488 return ChangeStatus::CHANGED;
3489 }
3490 return Changed;
3491 }
3492
3493 /// See AbstractAttribute::trackStatistics()
3494 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead){ static llvm::Statistic NumIRArguments_IsDead = {"attributor"
, "NumIRArguments_IsDead", ("Number of " "arguments" " marked '"
"IsDead" "'")};; ++(NumIRArguments_IsDead); }
}
3495};
3496
3497struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
3498 AAIsDeadCallSiteArgument(const IRPosition &IRP, Attributor &A)
3499 : AAIsDeadValueImpl(IRP, A) {}
3500
3501 /// See AbstractAttribute::initialize(...).
3502 void initialize(Attributor &A) override {
3503 if (isa<UndefValue>(getAssociatedValue()))
3504 indicatePessimisticFixpoint();
3505 }
3506
3507 /// See AbstractAttribute::updateImpl(...).
3508 ChangeStatus updateImpl(Attributor &A) override {
3509 // TODO: Once we have call site specific value information we can provide
3510 // call site specific liveness information and then it makes
3511 // sense to specialize attributes for call sites arguments instead of
3512 // redirecting requests to the callee argument.
3513 Argument *Arg = getAssociatedArgument();
3514 if (!Arg)
3515 return indicatePessimisticFixpoint();
3516 const IRPosition &ArgPos = IRPosition::argument(*Arg);
3517 auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos, DepClassTy::REQUIRED);
3518 return clampStateAndIndicateChange(getState(), ArgAA.getState());
3519 }
3520
3521 /// See AbstractAttribute::manifest(...).
3522 ChangeStatus manifest(Attributor &A) override {
3523 CallBase &CB = cast<CallBase>(getAnchorValue());
3524 Use &U = CB.getArgOperandUse(getCallSiteArgNo());
3525 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", 3526, __extension__
__PRETTY_FUNCTION__))
3526 "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", 3526, __extension__
__PRETTY_FUNCTION__))
;
3527 UndefValue &UV = *UndefValue::get(U->getType());
3528 if (A.changeUseAfterManifest(U, UV))
3529 return ChangeStatus::CHANGED;
3530 return ChangeStatus::UNCHANGED;
3531 }
3532
3533 /// See AbstractAttribute::trackStatistics()
3534 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); }
}
3535};
3536
3537struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
3538 AAIsDeadCallSiteReturned(const IRPosition &IRP, Attributor &A)
3539 : AAIsDeadFloating(IRP, A), IsAssumedSideEffectFree(true) {}
3540
3541 /// See AAIsDead::isAssumedDead().
3542 bool isAssumedDead() const override {
3543 return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
3544 }
3545
3546 /// See AbstractAttribute::initialize(...).
3547 void initialize(Attributor &A) override {
3548 if (isa<UndefValue>(getAssociatedValue())) {
3549 indicatePessimisticFixpoint();
3550 return;
3551 }
3552
3553 // We track this separately as a secondary state.
3554 IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
3555 }
3556
3557 /// See AbstractAttribute::updateImpl(...).
3558 ChangeStatus updateImpl(Attributor &A) override {
3559 ChangeStatus Changed = ChangeStatus::UNCHANGED;
3560 if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
3561 IsAssumedSideEffectFree = false;
3562 Changed = ChangeStatus::CHANGED;
3563 }
3564 if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3565 return indicatePessimisticFixpoint();
3566 return Changed;
3567 }
3568
3569 /// See AbstractAttribute::trackStatistics()
3570 void trackStatistics() const override {
3571 if (IsAssumedSideEffectFree)
3572 STATS_DECLTRACK_CSRET_ATTR(IsDead){ static llvm::Statistic NumIRCSReturn_IsDead = {"attributor"
, "NumIRCSReturn_IsDead", ("Number of " "call site returns" " marked '"
"IsDead" "'")};; ++(NumIRCSReturn_IsDead); }
3573 else
3574 STATS_DECLTRACK_CSRET_ATTR(UnusedResult){ static llvm::Statistic NumIRCSReturn_UnusedResult = {"attributor"
, "NumIRCSReturn_UnusedResult", ("Number of " "call site returns"
" marked '" "UnusedResult" "'")};; ++(NumIRCSReturn_UnusedResult
); }
3575 }
3576
3577 /// See AbstractAttribute::getAsStr().
3578 const std::string getAsStr() const override {
3579 return isAssumedDead()
3580 ? "assumed-dead"
3581 : (getAssumed() ? "assumed-dead-users" : "assumed-live");
3582 }
3583
3584private:
3585 bool IsAssumedSideEffectFree;
3586};
3587
3588struct AAIsDeadReturned : public AAIsDeadValueImpl {
3589 AAIsDeadReturned(const IRPosition &IRP, Attributor &A)
3590 : AAIsDeadValueImpl(IRP, A) {}
3591
3592 /// See AbstractAttribute::updateImpl(...).
3593 ChangeStatus updateImpl(Attributor &A) override {
3594
3595 bool UsedAssumedInformation = false;
3596 A.checkForAllInstructions([](Instruction &) { return true; }, *this,
3597 {Instruction::Ret}, UsedAssumedInformation);
3598
3599 auto PredForCallSite = [&](AbstractCallSite ACS) {
3600 if (ACS.isCallbackCall() || !ACS.getInstruction())
3601 return false;
3602 return areAllUsesAssumedDead(A, *ACS.getInstruction());
3603 };
3604
3605 bool AllCallSitesKnown;
3606 if (!A.checkForAllCallSites(PredForCallSite, *this, true,
3607 AllCallSitesKnown))
3608 return indicatePessimisticFixpoint();
3609
3610 return ChangeStatus::UNCHANGED;
3611 }
3612
3613 /// See AbstractAttribute::manifest(...).
3614 ChangeStatus manifest(Attributor &A) override {
3615 // TODO: Rewrite the signature to return void?
3616 bool AnyChange = false;
3617 UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
3618 auto RetInstPred = [&](Instruction &I) {
3619 ReturnInst &RI = cast<ReturnInst>(I);
3620 if (!isa<UndefValue>(RI.getReturnValue()))
3621 AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
3622 return true;
3623 };
3624 bool UsedAssumedInformation = false;
3625 A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret},
3626 UsedAssumedInformation);
3627 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3628 }
3629
3630 /// See AbstractAttribute::trackStatistics()
3631 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);
}
}
3632};
3633
3634struct AAIsDeadFunction : public AAIsDead {
3635 AAIsDeadFunction(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
3636
3637 /// See AbstractAttribute::initialize(...).
3638 void initialize(Attributor &A) override {
3639 const Function *F = getAnchorScope();
3640 if (F && !F->isDeclaration()) {
3641 // We only want to compute liveness once. If the function is not part of
3642 // the SCC, skip it.
3643 if (A.isRunOn(*const_cast<Function *>(F))) {
3644 ToBeExploredFrom.insert(&F->getEntryBlock().front());
3645 assumeLive(A, F->getEntryBlock());
3646 } else {
3647 indicatePessimisticFixpoint();
3648 }
3649 }
3650 }
3651
3652 /// See AbstractAttribute::getAsStr().
3653 const std::string getAsStr() const override {
3654 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
3655 std::to_string(getAnchorScope()->size()) + "][#TBEP " +
3656 std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
3657 std::to_string(KnownDeadEnds.size()) + "]";
3658 }
3659
3660 /// See AbstractAttribute::manifest(...).
3661 ChangeStatus manifest(Attributor &A) override {
3662 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", 3663, __extension__
__PRETTY_FUNCTION__))
3663 "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", 3663, __extension__
__PRETTY_FUNCTION__))
;
3664
3665 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
3666 Function &F = *getAnchorScope();
3667
3668 if (AssumedLiveBlocks.empty()) {
3669 A.deleteAfterManifest(F);
3670 return ChangeStatus::CHANGED;
3671 }
3672
3673 // Flag to determine if we can change an invoke to a call assuming the
3674 // callee is nounwind. This is not possible if the personality of the
3675 // function allows to catch asynchronous exceptions.
3676 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
3677
3678 KnownDeadEnds.set_union(ToBeExploredFrom);
3679 for (const Instruction *DeadEndI : KnownDeadEnds) {
3680 auto *CB = dyn_cast<CallBase>(DeadEndI);
3681 if (!CB)
3682 continue;
3683 const auto &NoReturnAA = A.getAndUpdateAAFor<AANoReturn>(
3684 *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
3685 bool MayReturn = !NoReturnAA.isAssumedNoReturn();
3686 if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
3687 continue;
3688
3689 if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
3690 A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
3691 else
3692 A.changeToUnreachableAfterManifest(
3693 const_cast<Instruction *>(DeadEndI->getNextNode()));
3694 HasChanged = ChangeStatus::CHANGED;
3695 }
3696
3697 STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.")static llvm::Statistic NumIRBasicBlock_AAIsDead = {"attributor"
, "NumIRBasicBlock_AAIsDead", "Number of dead basic blocks deleted."
};;
;
3698 for (BasicBlock &BB : F)
3699 if (!AssumedLiveBlocks.count(&BB)) {
3700 A.deleteAfterManifest(BB);
3701 ++BUILD_STAT_NAME(AAIsDead, BasicBlock)NumIRBasicBlock_AAIsDead;
3702 }
3703
3704 return HasChanged;
3705 }
3706
3707 /// See AbstractAttribute::updateImpl(...).
3708 ChangeStatus updateImpl(Attributor &A) override;
3709
3710 bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const override {
3711 return !AssumedLiveEdges.count(std::make_pair(From, To));
3712 }
3713
3714 /// See AbstractAttribute::trackStatistics()
3715 void trackStatistics() const override {}
3716
3717 /// Returns true if the function is assumed dead.
3718 bool isAssumedDead() const override { return false; }
3719
3720 /// See AAIsDead::isKnownDead().
3721 bool isKnownDead() const override { return false; }
3722
3723 /// See AAIsDead::isAssumedDead(BasicBlock *).
3724 bool isAssumedDead(const BasicBlock *BB) const override {
3725 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", 3726, __extension__
__PRETTY_FUNCTION__))
3726 "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", 3726, __extension__
__PRETTY_FUNCTION__))
;
3727
3728 if (!getAssumed())
3729 return false;
3730 return !AssumedLiveBlocks.count(BB);
3731 }
3732
3733 /// See AAIsDead::isKnownDead(BasicBlock *).
3734 bool isKnownDead(const BasicBlock *BB) const override {
3735 return getKnown() && isAssumedDead(BB);
3736 }
3737
3738 /// See AAIsDead::isAssumed(Instruction *I).
3739 bool isAssumedDead(const Instruction *I) const override {
3740 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", 3741, __extension__
__PRETTY_FUNCTION__))
3741 "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", 3741, __extension__
__PRETTY_FUNCTION__))
;
3742
3743 if (!getAssumed())
3744 return false;
3745
3746 // If it is not in AssumedLiveBlocks then it for sure dead.
3747 // Otherwise, it can still be after noreturn call in a live block.
3748 if (!AssumedLiveBlocks.count(I->getParent()))
3749 return true;
3750
3751 // If it is not after a liveness barrier it is live.
3752 const Instruction *PrevI = I->getPrevNode();
3753 while (PrevI) {
3754 if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
3755 return true;
3756 PrevI = PrevI->getPrevNode();
3757 }
3758 return false;
3759 }
3760
3761 /// See AAIsDead::isKnownDead(Instruction *I).
3762 bool isKnownDead(const Instruction *I) const override {
3763 return getKnown() && isAssumedDead(I);
3764 }
3765
3766 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
3767 /// that internal function called from \p BB should now be looked at.
3768 bool assumeLive(Attributor &A, const BasicBlock &BB) {
3769 if (!AssumedLiveBlocks.insert(&BB).second)
3770 return false;
3771
3772 // We assume that all of BB is (probably) live now and if there are calls to
3773 // internal functions we will assume that those are now live as well. This
3774 // is a performance optimization for blocks with calls to a lot of internal
3775 // functions. It can however cause dead functions to be treated as live.
3776 for (const Instruction &I : BB)
3777 if (const auto *CB = dyn_cast<CallBase>(&I))
3778 if (const Function *F = CB->getCalledFunction())
3779 if (F->hasLocalLinkage())
3780 A.markLiveInternalFunction(*F);
3781 return true;
3782 }
3783
3784 /// Collection of instructions that need to be explored again, e.g., we
3785 /// did assume they do not transfer control to (one of their) successors.
3786 SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
3787
3788 /// Collection of instructions that are known to not transfer control.
3789 SmallSetVector<const Instruction *, 8> KnownDeadEnds;
3790
3791 /// Collection of all assumed live edges
3792 DenseSet<std::pair<const BasicBlock *, const BasicBlock *>> AssumedLiveEdges;
3793
3794 /// Collection of all assumed live BasicBlocks.
3795 DenseSet<const BasicBlock *> AssumedLiveBlocks;
3796};
3797
3798static bool
3799identifyAliveSuccessors(Attributor &A, const CallBase &CB,
3800 AbstractAttribute &AA,
3801 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3802 const IRPosition &IPos = IRPosition::callsite_function(CB);
3803
3804 const auto &NoReturnAA =
3805 A.getAndUpdateAAFor<AANoReturn>(AA, IPos, DepClassTy::OPTIONAL);
3806 if (NoReturnAA.isAssumedNoReturn())
3807 return !NoReturnAA.isKnownNoReturn();
3808 if (CB.isTerminator())
3809 AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
3810 else
3811 AliveSuccessors.push_back(CB.getNextNode());
3812 return false;
3813}
3814
3815static bool
3816identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
3817 AbstractAttribute &AA,
3818 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3819 bool UsedAssumedInformation =
3820 identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
3821
3822 // First, determine if we can change an invoke to a call assuming the
3823 // callee is nounwind. This is not possible if the personality of the
3824 // function allows to catch asynchronous exceptions.
3825 if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
3826 AliveSuccessors.push_back(&II.getUnwindDest()->front());
3827 } else {
3828 const IRPosition &IPos = IRPosition::callsite_function(II);
3829 const auto &AANoUnw =
3830 A.getAndUpdateAAFor<AANoUnwind>(AA, IPos, DepClassTy::OPTIONAL);
3831 if (AANoUnw.isAssumedNoUnwind()) {
3832 UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
3833 } else {
3834 AliveSuccessors.push_back(&II.getUnwindDest()->front());
3835 }
3836 }
3837 return UsedAssumedInformation;
3838}
3839
3840static bool
3841identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
3842 AbstractAttribute &AA,
3843 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3844 bool UsedAssumedInformation = false;
3845 if (BI.getNumSuccessors() == 1) {
3846 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3847 } else {
3848 Optional<Constant *> C =
3849 A.getAssumedConstant(*BI.getCondition(), AA, UsedAssumedInformation);
3850 if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
3851 // No value yet, assume both edges are dead.
3852 } else if (isa_and_nonnull<ConstantInt>(*C)) {
3853 const BasicBlock *SuccBB =
3854 BI.getSuccessor(1 - cast<ConstantInt>(*C)->getValue().getZExtValue());
3855 AliveSuccessors.push_back(&SuccBB->front());
3856 } else {
3857 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3858 AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
3859 UsedAssumedInformation = false;
3860 }
3861 }
3862 return UsedAssumedInformation;
3863}
3864
3865static bool
3866identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
3867 AbstractAttribute &AA,
3868 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3869 bool UsedAssumedInformation = false;
3870 Optional<Constant *> C =
3871 A.getAssumedConstant(*SI.getCondition(), AA, UsedAssumedInformation);
3872 if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
3873 // No value yet, assume all edges are dead.
3874 } else if (isa_and_nonnull<ConstantInt>(C.getValue())) {
3875 for (auto &CaseIt : SI.cases()) {
3876 if (CaseIt.getCaseValue() == C.getValue()) {
3877 AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
3878 return UsedAssumedInformation;
3879 }
3880 }
3881 AliveSuccessors.push_back(&SI.getDefaultDest()->front());
3882 return UsedAssumedInformation;
3883 } else {
3884 for (const BasicBlock *SuccBB : successors(SI.getParent()))
3885 AliveSuccessors.push_back(&SuccBB->front());
3886 }
3887 return UsedAssumedInformation;
3888}
3889
3890ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
3891 ChangeStatus Change = ChangeStatus::UNCHANGED;
3892
3893 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)
3894 << 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)
3895 << 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)
3896 << 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)
;
3897
3898 // Copy and clear the list of instructions we need to explore from. It is
3899 // refilled with instructions the next update has to look at.
3900 SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
3901 ToBeExploredFrom.end());
3902 decltype(ToBeExploredFrom) NewToBeExploredFrom;
3903
3904 SmallVector<const Instruction *, 8> AliveSuccessors;
3905 while (!Worklist.empty()) {
3906 const Instruction *I = Worklist.pop_back_val();
3907 LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAIsDead] Exploration inst: "
<< *I << "\n"; } } while (false)
;
3908
3909 // Fast forward for uninteresting instructions. We could look for UB here
3910 // though.
3911 while (!I->isTerminator() && !isa<CallBase>(I))
3912 I = I->getNextNode();
3913
3914 AliveSuccessors.clear();
3915
3916 bool UsedAssumedInformation = false;
3917 switch (I->getOpcode()) {
3918 // TODO: look for (assumed) UB to backwards propagate "deadness".
3919 default:
3920 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", 3921, __extension__
__PRETTY_FUNCTION__))
3921 "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", 3921, __extension__
__PRETTY_FUNCTION__))
;
3922 for (const BasicBlock *SuccBB : successors(I->getParent()))
3923 AliveSuccessors.push_back(&SuccBB->front());
3924 break;
3925 case Instruction::Call:
3926 UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
3927 *this, AliveSuccessors);
3928 break;
3929 case Instruction::Invoke:
3930 UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
3931 *this, AliveSuccessors);
3932 break;
3933 case Instruction::Br:
3934 UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
3935 *this, AliveSuccessors);
3936 break;
3937 case Instruction::Switch:
3938 UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
3939 *this, AliveSuccessors);
3940 break;
3941 }
3942
3943 if (UsedAssumedInformation) {
3944 NewToBeExploredFrom.insert(I);
3945 } else if (AliveSuccessors.empty() ||
3946 (I->isTerminator() &&
3947 AliveSuccessors.size() < I->getNumSuccessors())) {
3948 if (KnownDeadEnds.insert(I))
3949 Change = ChangeStatus::CHANGED;
3950 }
3951
3952 LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAIsDead] #AliveSuccessors: "
<< AliveSuccessors.size() << " UsedAssumedInformation: "
<< UsedAssumedInformation << "\n"; } } while (false
)
3953 << AliveSuccessors.size() << " UsedAssumedInformation: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAIsDead] #AliveSuccessors: "
<< AliveSuccessors.size() << " UsedAssumedInformation: "
<< UsedAssumedInformation << "\n"; } } while (false
)
3954 << UsedAssumedInformation << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AAIsDead] #AliveSuccessors: "
<< AliveSuccessors.size() << " UsedAssumedInformation: "
<< UsedAssumedInformation << "\n"; } } while (false
)
;
3955
3956 for (const Instruction *AliveSuccessor : AliveSuccessors) {
3957 if (!I->isTerminator()) {
3958 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", 3959, __extension__
__PRETTY_FUNCTION__))
3959 "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", 3959, __extension__
__PRETTY_FUNCTION__))
;
3960 Worklist.push_back(AliveSuccessor);
3961 } else {
3962 // record the assumed live edge
3963 auto Edge = std::make_pair(I->getParent(), AliveSuccessor->getParent());
3964 if (AssumedLiveEdges.insert(Edge).second)
3965 Change = ChangeStatus::CHANGED;
3966 if (assumeLive(A, *AliveSuccessor->getParent()))
3967 Worklist.push_back(AliveSuccessor);
3968 }
3969 }
3970 }
3971
3972 // Check if the content of ToBeExploredFrom changed, ignore the order.
3973 if (NewToBeExploredFrom.size() != ToBeExploredFrom.size() ||
3974 llvm::any_of(NewToBeExploredFrom, [&](const Instruction *I) {
3975 return !ToBeExploredFrom.count(I);
3976 })) {
3977 Change = ChangeStatus::CHANGED;
3978 ToBeExploredFrom = std::move(NewToBeExploredFrom);
3979 }
3980
3981 // If we know everything is live there is no need to query for liveness.
3982 // Instead, indicating a pessimistic fixpoint will cause the state to be
3983 // "invalid" and all queries to be answered conservatively without lookups.
3984 // To be in this state we have to (1) finished the exploration and (3) not
3985 // discovered any non-trivial dead end and (2) not ruled unreachable code
3986 // dead.
3987 if (ToBeExploredFrom.empty() &&
3988 getAnchorScope()->size() == AssumedLiveBlocks.size() &&
3989 llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
3990 return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
3991 }))
3992 return indicatePessimisticFixpoint();
3993 return Change;
3994}
3995
3996/// Liveness information for a call sites.
3997struct AAIsDeadCallSite final : AAIsDeadFunction {
3998 AAIsDeadCallSite(const IRPosition &IRP, Attributor &A)
3999 : AAIsDeadFunction(IRP, A) {}
4000
4001 /// See AbstractAttribute::initialize(...).
4002 void initialize(Attributor &A) override {
4003 // TODO: Once we have call site specific value information we can provide
4004 // call site specific liveness information and then it makes
4005 // sense to specialize attributes for call sites instead of
4006 // redirecting requests to the callee.
4007 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"
, 4008)
4008 "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"
, 4008)
;
4009 }
4010
4011 /// See AbstractAttribute::updateImpl(...).
4012 ChangeStatus updateImpl(Attributor &A) override {
4013 return indicatePessimisticFixpoint();
4014 }
4015
4016 /// See AbstractAttribute::trackStatistics()
4017 void trackStatistics() const override {}
4018};
4019
4020/// -------------------- Dereferenceable Argument Attribute --------------------
4021
4022struct AADereferenceableImpl : AADereferenceable {
4023 AADereferenceableImpl(const IRPosition &IRP, Attributor &A)
4024 : AADereferenceable(IRP, A) {}
4025 using StateType = DerefState;
4026
4027 /// See AbstractAttribute::initialize(...).
4028 void initialize(Attributor &A) override {
4029 SmallVector<Attribute, 4> Attrs;
4030 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
4031 Attrs, /* IgnoreSubsumingPositions */ false, &A);
4032 for (const Attribute &Attr : Attrs)
4033 takeKnownDerefBytesMaximum(Attr.getValueAsInt());
4034
4035 const IRPosition &IRP = this->getIRPosition();
4036 NonNullAA = &A.getAAFor<AANonNull>(*this, IRP, DepClassTy::NONE);
4037
4038 bool CanBeNull, CanBeFreed;
4039 takeKnownDerefBytesMaximum(
4040 IRP.getAssociatedValue().getPointerDereferenceableBytes(
4041 A.getDataLayout(), CanBeNull, CanBeFreed));
4042
4043 bool IsFnInterface = IRP.isFnInterfaceKind();
4044 Function *FnScope = IRP.getAnchorScope();
4045 if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope))) {
4046 indicatePessimisticFixpoint();
4047 return;
4048 }
4049
4050 if (Instruction *CtxI = getCtxI())
4051 followUsesInMBEC(*this, A, getState(), *CtxI);
4052 }
4053
4054 /// See AbstractAttribute::getState()
4055 /// {
4056 StateType &getState() override { return *this; }
4057 const StateType &getState() const override { return *this; }
4058 /// }
4059
4060 /// Helper function for collecting accessed bytes in must-be-executed-context
4061 void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I,
4062 DerefState &State) {
4063 const Value *UseV = U->get();
4064 if (!UseV->getType()->isPointerTy())
4065 return;
4066
4067 Optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I);
4068 if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile())
4069 return;
4070
4071 int64_t Offset;
4072 const Value *Base = GetPointerBaseWithConstantOffset(
4073 Loc->Ptr, Offset, A.getDataLayout(), /*AllowNonInbounds*/ true);
4074 if (Base && Base == &getAssociatedValue())
4075 State.addAccessedBytes(Offset, Loc->Size.getValue());
4076 }
4077
4078 /// See followUsesInMBEC
4079 bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
4080 AADereferenceable::StateType &State) {
4081 bool IsNonNull = false;
4082 bool TrackUse = false;
4083 int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
4084 A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
4085 LLVM_DEBUG(dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytesdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AADereferenceable] Deref bytes: "
<< DerefBytes << " for instruction " << *I
<< "\n"; } } while (false)
4086 << " for instruction " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[AADereferenceable] Deref bytes: "
<< DerefBytes << " for instruction " << *I
<< "\n"; } } while (false)
;
4087
4088 addAccessedBytesForUse(A, U, I, State);
4089 State.takeKnownDerefBytesMaximum(DerefBytes);
4090 return TrackUse;
4091 }
4092
4093 /// See AbstractAttribute::manifest(...).
4094 ChangeStatus manifest(Attributor &A) override {
4095 ChangeStatus Change = AADereferenceable::manifest(A);
4096 if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
4097 removeAttrs({Attribute::DereferenceableOrNull});
4098 return ChangeStatus::CHANGED;
4099 }
4100 return Change;
4101 }
4102
4103 void getDeducedAttributes(LLVMContext &Ctx,
4104 SmallVectorImpl<Attribute> &Attrs) const override {
4105 // TODO: Add *_globally support
4106 if (isAssumedNonNull())
4107 Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
4108 Ctx, getAssumedDereferenceableBytes()));
4109 else
4110 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
4111 Ctx, getAssumedDereferenceableBytes()));
4112 }
4113
4114 /// See AbstractAttribute::getAsStr().
4115 const std::string getAsStr() const override {
4116 if (!getAssumedDereferenceableBytes())
4117 return "unknown-dereferenceable";
4118 return std::string("dereferenceable") +
4119 (isAssumedNonNull() ? "" : "_or_null") +
4120 (isAssumedGlobal() ? "_globally" : "") + "<" +
4121 std::to_string(getKnownDereferenceableBytes()) + "-" +
4122 std::to_string(getAssumedDereferenceableBytes()) + ">";
4123 }
4124};
4125
4126/// Dereferenceable attribute for a floating value.
4127struct AADereferenceableFloating : AADereferenceableImpl {
4128 AADereferenceableFloating(const IRPosition &IRP, Attributor &A)
4129 : AADereferenceableImpl(IRP, A) {}
4130
4131 /// See AbstractAttribute::updateImpl(...).
4132 ChangeStatus updateImpl(Attributor &A) override {
4133 const DataLayout &DL = A.getDataLayout();
4134
4135 auto VisitValueCB = [&](const Value &V, const Instruction *, DerefState &T,
4136 bool Stripped) -> bool {
4137 unsigned IdxWidth =
4138 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
4139 APInt Offset(IdxWidth, 0);
4140 const Value *Base =
4141 stripAndAccumulateMinimalOffsets(A, *this, &V, DL, Offset, false);
4142
4143 const auto &AA = A.getAAFor<AADereferenceable>(
4144 *this, IRPosition::value(*Base), DepClassTy::REQUIRED);
4145 int64_t DerefBytes = 0;
4146 if (!Stripped && this == &AA) {
4147 // Use IR information if we did not strip anything.
4148 // TODO: track globally.
4149 bool CanBeNull, CanBeFreed;
4150 DerefBytes =
4151 Base->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
4152 T.GlobalState.indicatePessimisticFixpoint();
4153 } else {
4154 const DerefState &DS = AA.getState();
4155 DerefBytes = DS.DerefBytesState.getAssumed();
4156 T.GlobalState &= DS.GlobalState;
4157 }
4158
4159 // For now we do not try to "increase" dereferenceability due to negative
4160 // indices as we first have to come up with code to deal with loops and
4161 // for overflows of the dereferenceable bytes.
4162 int64_t OffsetSExt = Offset.getSExtValue();
4163 if (OffsetSExt < 0)
4164 OffsetSExt = 0;
4165
4166 T.takeAssumedDerefBytesMinimum(
4167 std::max(int64_t(0), DerefBytes - OffsetSExt));
4168
4169 if (this == &AA) {
4170 if (!Stripped) {
4171 // If nothing was stripped IR information is all we got.
4172 T.takeKnownDerefBytesMaximum(
4173 std::max(int64_t(0), DerefBytes - OffsetSExt));
4174 T.indicatePessimisticFixpoint();
4175 } else if (OffsetSExt > 0) {
4176 // If something was stripped but there is circular reasoning we look
4177 // for the offset. If it is positive we basically decrease the
4178 // dereferenceable bytes in a circluar loop now, which will simply
4179 // drive them down to the known value in a very slow way which we
4180 // can accelerate.
4181 T.indicatePessimisticFixpoint();
4182 }
4183 }
4184
4185 return T.isValidState();
4186 };
4187
4188 DerefState T;
4189 if (!genericValueTraversal<DerefState>(A, getIRPosition(), *this, T,
4190 VisitValueCB, getCtxI()))
4191 return indicatePessimisticFixpoint();
4192
4193 return clampStateAndIndicateChange(getState(), T);
4194 }
4195
4196 /// See AbstractAttribute::trackStatistics()
4197 void trackStatistics() const override {
4198 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable){ static llvm::Statistic NumIRFloating_dereferenceable = {"attributor"
, "NumIRFloating_dereferenceable", ("Number of floating values known to be '"
"dereferenceable" "'")};; ++(NumIRFloating_dereferenceable);
}
4199 }
4200};
4201
4202/// Dereferenceable attribute for a return value.
4203struct AADereferenceableReturned final
4204 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
4205 AADereferenceableReturned(const IRPosition &IRP, Attributor &A)
4206 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
4207 IRP, A) {}
4208
4209 /// See AbstractAttribute::trackStatistics()
4210 void trackStatistics() const override {
4211 STATS_DECLTRACK_FNRET_ATTR(dereferenceable){ static llvm::Statistic NumIRFunctionReturn_dereferenceable =
{"attributor", "NumIRFunctionReturn_dereferenceable", ("Number of "
"function returns" " marked '" "dereferenceable" "'")};; ++(
NumIRFunctionReturn_dereferenceable); }
4212 }
4213};
4214
4215/// Dereferenceable attribute for an argument
4216struct AADereferenceableArgument final
4217 : AAArgumentFromCallSiteArguments<AADereferenceable,
4218 AADereferenceableImpl> {
4219 using Base =
4220 AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl>;
4221 AADereferenceableArgument(const IRPosition &IRP, Attributor &A)
4222 : Base(IRP, A) {}
4223
4224 /// See AbstractAttribute::trackStatistics()
4225 void trackStatistics() const override {
4226 STATS_DECLTRACK_ARG_ATTR(dereferenceable){ static llvm::Statistic NumIRArguments_dereferenceable = {"attributor"
, "NumIRArguments_dereferenceable", ("Number of " "arguments"
" marked '" "dereferenceable" "'")};; ++(NumIRArguments_dereferenceable
); }
4227 }
4228};
4229
4230/// Dereferenceable attribute for a call site argument.
4231struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
4232 AADereferenceableCallSiteArgument(const IRPosition &IRP, Attributor &A)
4233 : AADereferenceableFloating(IRP, A) {}
4234
4235 /// See AbstractAttribute::trackStatistics()
4236 void trackStatistics() const override {
4237 STATS_DECLTRACK_CSARG_ATTR(dereferenceable){ static llvm::Statistic NumIRCSArguments_dereferenceable = {
"attributor", "NumIRCSArguments_dereferenceable", ("Number of "
"call site arguments" " marked '" "dereferenceable" "'")};; ++
(NumIRCSArguments_dereferenceable); }
4238 }
4239};
4240
4241/// Dereferenceable attribute deduction for a call site return value.
4242struct AADereferenceableCallSiteReturned final
4243 : AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl> {
4244 using Base =
4245 AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl>;
4246 AADereferenceableCallSiteReturned(const IRPosition &IRP, Attributor &A)
4247 : Base(IRP, A) {}
4248
4249 /// See AbstractAttribute::trackStatistics()
4250 void trackStatistics() const override {
4251 STATS_DECLTRACK_CS_ATTR(dereferenceable){ static llvm::Statistic NumIRCS_dereferenceable = {"attributor"
, "NumIRCS_dereferenceable", ("Number of " "call site" " marked '"
"dereferenceable" "'")};; ++(NumIRCS_dereferenceable); }
;
4252 }
4253};
4254
4255// ------------------------ Align Argument Attribute ------------------------
4256
4257static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA,
4258 Value &AssociatedValue, const Use *U,
4259 const Instruction *I, bool &TrackUse) {
4260 // We need to follow common pointer manipulation uses to the accesses they
4261 // feed into.
4262 if (isa<CastInst>(I)) {
4263 // Follow all but ptr2int casts.
4264 TrackUse = !isa<PtrToIntInst>(I);
4265 return 0;
4266 }
4267 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
4268 if (GEP->hasAllConstantIndices())
4269 TrackUse = true;
4270 return 0;
4271 }
4272
4273 MaybeAlign MA;
4274 if (const auto *CB = dyn_cast<CallBase>(I)) {
4275 if (CB->isBundleOperand(U) || CB->isCallee(U))
4276 return 0;
4277
4278 unsigned ArgNo = CB->getArgOperandNo(U);
4279 IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
4280 // As long as we only use known information there is no need to track
4281 // dependences here.
4282 auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, DepClassTy::NONE);
4283 MA = MaybeAlign(AlignAA.getKnownAlign());
4284 }
4285
4286 const DataLayout &DL = A.getDataLayout();
4287 const Value *UseV = U->get();
4288 if (auto *SI = dyn_cast<StoreInst>(I)) {
4289 if (SI->getPointerOperand() == UseV)
4290 MA = SI->getAlign();
4291 } else if (auto *LI = dyn_cast<LoadInst>(I)) {
4292 if (LI->getPointerOperand() == UseV)
4293 MA = LI->getAlign();
4294 }
4295
4296 if (!MA || *MA <= QueryingAA.getKnownAlign())
4297 return 0;
4298
4299 unsigned Alignment = MA->value();
4300 int64_t Offset;
4301
4302 if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
4303 if (Base == &AssociatedValue) {
4304 // BasePointerAddr + Offset = Alignment * Q for some integer Q.
4305 // So we can say that the maximum power of two which is a divisor of
4306 // gcd(Offset, Alignment) is an alignment.
4307
4308 uint32_t gcd =
4309 greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
4310 Alignment = llvm::PowerOf2Floor(gcd);
4311 }
4312 }
4313
4314 return Alignment;
4315}
4316
4317struct AAAlignImpl : AAAlign {
4318 AAAlignImpl(const IRPosition &IRP, Attributor &A) : AAAlign(IRP, A) {}
4319
4320 /// See AbstractAttribute::initialize(...).
4321 void initialize(Attributor &A) override {
4322 SmallVector<Attribute, 4> Attrs;
4323 getAttrs({Attribute::Alignment}, Attrs);
4324 for (const Attribute &Attr : Attrs)
4325 takeKnownMaximum(Attr.getValueAsInt());
4326
4327 Value &V = getAssociatedValue();
4328 // TODO: This is a HACK to avoid getPointerAlignment to introduce a ptr2int
4329 // use of the function pointer. This was caused by D73131. We want to
4330 // avoid this for function pointers especially because we iterate
4331 // their uses and int2ptr is not handled. It is not a correctness
4332 // problem though!
4333 if (!V.getType()->getPointerElementType()->isFunctionTy())
4334 takeKnownMaximum(V.getPointerAlignment(A.getDataLayout()).value());
4335
4336 if (getIRPosition().isFnInterfaceKind() &&
4337 (!getAnchorScope() ||
4338 !A.isFunctionIPOAmendable(*getAssociatedFunction()))) {
4339 indicatePessimisticFixpoint();
4340 return;
4341 }
4342
4343 if (Instruction *CtxI = getCtxI())
4344 followUsesInMBEC(*this, A, getState(), *CtxI);
4345 }
4346
4347 /// See AbstractAttribute::manifest(...).
4348 ChangeStatus manifest(Attributor &A) override {
4349 ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
4350
4351 // Check for users that allow alignment annotations.
4352 Value &AssociatedValue = getAssociatedValue();
4353 for (const Use &U : AssociatedValue.uses()) {
4354 if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
4355 if (SI->getPointerOperand() == &AssociatedValue)
4356 if (SI->getAlignment() < getAssumedAlign()) {
4357 STATS_DECLTRACK(AAAlign, Store,{ static llvm::Statistic NumIRStore_AAAlign = {"attributor", "NumIRStore_AAAlign"
, "Number of times alignment added to a store"};; ++(NumIRStore_AAAlign
); }
4358 "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
); }
;
4359 SI->setAlignment(Align(getAssumedAlign()));
4360 LoadStoreChanged = ChangeStatus::CHANGED;
4361 }
4362 } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
4363 if (LI->getPointerOperand() == &AssociatedValue)
4364 if (LI->getAlignment() < getAssumedAlign()) {
4365 LI->setAlignment(Align(getAssumedAlign()));
4366 STATS_DECLTRACK(AAAlign, Load,{ static llvm::Statistic NumIRLoad_AAAlign = {"attributor", "NumIRLoad_AAAlign"
, "Number of times alignment added to a load"};; ++(NumIRLoad_AAAlign
); }
4367 "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
); }
;
4368 LoadStoreChanged = ChangeStatus::CHANGED;
4369 }
4370 }
4371 }
4372
4373 ChangeStatus Changed = AAAlign::manifest(A);
4374
4375 Align InheritAlign =
4376 getAssociatedValue().getPointerAlignment(A.getDataLayout());
4377 if (InheritAlign >= getAssumedAlign())
4378 return LoadStoreChanged;
4379 return Changed | LoadStoreChanged;
4380 }
4381
4382 // TODO: Provide a helper to determine the implied ABI alignment and check in
4383 // the existing manifest method and a new one for AAAlignImpl that value
4384 // to avoid making the alignment explicit if it did not improve.
4385
4386 /// See AbstractAttribute::getDeducedAttributes
4387 virtual void
4388 getDeducedAttributes(LLVMContext &Ctx,
4389 SmallVectorImpl<Attribute> &Attrs) const override {
4390 if (getAssumedAlign() > 1)
4391 Attrs.emplace_back(
4392 Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
4393 }
4394
4395 /// See followUsesInMBEC
4396 bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
4397 AAAlign::StateType &State) {
4398 bool TrackUse = false;
4399
4400 unsigned int KnownAlign =
4401 getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
4402 State.takeKnownMaximum(KnownAlign);
4403
4404 return TrackUse;
4405 }
4406
4407 /// See AbstractAttribute::getAsStr().
4408 const std::string getAsStr() const override {
4409 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
4410 "-" + std::to_string(getAssumedAlign()) + ">")
4411 : "unknown-align";
4412 }
4413};
4414
4415/// Align attribute for a floating value.
4416struct AAAlignFloating : AAAlignImpl {
4417 AAAlignFloating(const IRPosition &IRP, Attributor &A) : AAAlignImpl(IRP, A) {}
4418
4419 /// See AbstractAttribute::updateImpl(...).
4420 ChangeStatus updateImpl(Attributor &A) override {
4421 const DataLayout &DL = A.getDataLayout();
4422
4423 auto VisitValueCB = [&](Value &V, const Instruction *,
4424 AAAlign::StateType &T, bool Stripped) -> bool {
4425 const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V),
4426 DepClassTy::REQUIRED);
4427 if (!Stripped && this == &AA) {
4428 int64_t Offset;
4429 unsigned Alignment = 1;
4430 if (const Value *Base =
4431 GetPointerBaseWithConstantOffset(&V, Offset, DL)) {
4432 Align PA = Base->getPointerAlignment(DL);
4433 // BasePointerAddr + Offset = Alignment * Q for some integer Q.
4434 // So we can say that the maximum power of two which is a divisor of
4435 // gcd(Offset, Alignment) is an alignment.
4436
4437 uint32_t gcd = greatestCommonDivisor(uint32_t(abs((int32_t)Offset)),
4438 uint32_t(PA.value()));
4439 Alignment = llvm::PowerOf2Floor(gcd);
4440 } else {
4441 Alignment = V.getPointerAlignment(DL).value();
4442 }
4443 // Use only IR information if we did not strip anything.
4444 T.takeKnownMaximum(Alignment);
4445 T.indicatePessimisticFixpoint();
4446 } else {
4447 // Use abstract attribute information.
4448 const AAAlign::StateType &DS = AA.getState();
4449 T ^= DS;
4450 }
4451 return T.isValidState();
4452 };
4453
4454 StateType T;
4455 if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
4456 VisitValueCB, getCtxI()))
4457 return indicatePessimisticFixpoint();
4458
4459 // TODO: If we know we visited all incoming values, thus no are assumed
4460 // dead, we can take the known information from the state T.
4461 return clampStateAndIndicateChange(getState(), T);
4462 }
4463
4464 /// See AbstractAttribute::trackStatistics()
4465 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); }
}
4466};
4467
4468/// Align attribute for function return value.
4469struct AAAlignReturned final
4470 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
4471 using Base = AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>;
4472 AAAlignReturned(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
4473
4474 /// See AbstractAttribute::initialize(...).
4475 void initialize(Attributor &A) override {
4476 Base::initialize(A);
4477 Function *F = getAssociatedFunction();
4478 if (!F || F->isDeclaration())
4479 indicatePessimisticFixpoint();
4480 }
4481
4482 /// See AbstractAttribute::trackStatistics()
4483 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
); }
}
4484};
4485
4486/// Align attribute for function argument.
4487struct AAAlignArgument final
4488 : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
4489 using Base = AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>;
4490 AAAlignArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
4491
4492 /// See AbstractAttribute::manifest(...).
4493 ChangeStatus manifest(Attributor &A) override {
4494 // If the associated argument is involved in a must-tail call we give up
4495 // because we would need to keep the argument alignments of caller and
4496 // callee in-sync. Just does not seem worth the trouble right now.
4497 if (A.getInfoCache().isInvolvedInMustTailCall(*getAssociatedArgument()))
4498 return ChangeStatus::UNCHANGED;
4499 return Base::manifest(A);
4500 }
4501
4502 /// See AbstractAttribute::trackStatistics()
4503 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned){ static llvm::Statistic NumIRArguments_aligned = {"attributor"
, "NumIRArguments_aligned", ("Number of " "arguments" " marked '"
"aligned" "'")};; ++(NumIRArguments_aligned); }
}
4504};
4505
4506struct AAAlignCallSiteArgument final : AAAlignFloating {
4507 AAAlignCallSiteArgument(const IRPosition &IRP, Attributor &A)
4508 : AAAlignFloating(IRP, A) {}
4509
4510 /// See AbstractAttribute::manifest(...).
4511 ChangeStatus manifest(Attributor &A) override {
4512 // If the associated argument is involved in a must-tail call we give up
4513 // because we would need to keep the argument alignments of caller and
4514 // callee in-sync. Just does not seem worth the trouble right now.
4515 if (Argument *Arg = getAssociatedArgument())
4516 if (A.getInfoCache().isInvolvedInMustTailCall(*Arg))
4517 return ChangeStatus::UNCHANGED;
4518 ChangeStatus Changed = AAAlignImpl::manifest(A);
4519 Align InheritAlign =
4520 getAssociatedValue().getPointerAlignment(A.getDataLayout());
4521 if (InheritAlign >= getAssumedAlign())
4522 Changed = ChangeStatus::UNCHANGED;
4523 return Changed;
4524 }
4525
4526 /// See AbstractAttribute::updateImpl(Attributor &A).
4527 ChangeStatus updateImpl(Attributor &A) override {
4528 ChangeStatus Changed = AAAlignFloating::updateImpl(A);
4529 if (Argument *Arg = getAssociatedArgument()) {
4530 // We only take known information from the argument
4531 // so we do not need to track a dependence.
4532 const auto &ArgAlignAA = A.getAAFor<AAAlign>(
4533 *this, IRPosition::argument(*Arg), DepClassTy::NONE);
4534 takeKnownMaximum(ArgAlignAA.getKnownAlign());
4535 }
4536 return Changed;
4537 }
4538
4539 /// See AbstractAttribute::trackStatistics()
4540 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); }
}
4541};
4542
4543/// Align attribute deduction for a call site return value.
4544struct AAAlignCallSiteReturned final
4545 : AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl> {
4546 using Base = AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl>;
4547 AAAlignCallSiteReturned(const IRPosition &IRP, Attributor &A)
4548 : Base(IRP, A) {}
4549
4550 /// See AbstractAttribute::initialize(...).
4551 void initialize(Attributor &A) override {
4552 Base::initialize(A);
4553 Function *F = getAssociatedFunction();
4554 if (!F || F->isDeclaration())
4555 indicatePessimisticFixpoint();
4556 }
4557
4558 /// See AbstractAttribute::trackStatistics()
4559 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
); }
; }
4560};
4561
4562/// ------------------ Function No-Return Attribute ----------------------------
4563struct AANoReturnImpl : public AANoReturn {
4564 AANoReturnImpl(const IRPosition &IRP, Attributor &A) : AANoReturn(IRP, A) {}
4565
4566 /// See AbstractAttribute::initialize(...).
4567 void initialize(Attributor &A) override {
4568 AANoReturn::initialize(A);
4569 Function *F = getAssociatedFunction();
4570 if (!F || F->isDeclaration())
4571 indicatePessimisticFixpoint();
4572 }
4573
4574 /// See AbstractAttribute::getAsStr().
4575 const std::string getAsStr() const override {
4576 return getAssumed() ? "noreturn" : "may-return";
4577 }
4578
4579 /// See AbstractAttribute::updateImpl(Attributor &A).
4580 virtual ChangeStatus updateImpl(Attributor &A) override {
4581 auto CheckForNoReturn = [](Instruction &) { return false; };
4582 bool UsedAssumedInformation = false;
4583 if (!A.checkForAllInstructions(CheckForNoReturn, *this,
4584 {(unsigned)Instruction::Ret},
4585 UsedAssumedInformation))
4586 return indicatePessimisticFixpoint();
4587 return ChangeStatus::UNCHANGED;
4588 }
4589};
4590
4591struct AANoReturnFunction final : AANoReturnImpl {
4592 AANoReturnFunction(const IRPosition &IRP, Attributor &A)
4593 : AANoReturnImpl(IRP, A) {}
4594
4595 /// See AbstractAttribute::trackStatistics()
4596 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn){ static llvm::Statistic NumIRFunction_noreturn = {"attributor"
, "NumIRFunction_noreturn", ("Number of " "functions" " marked '"
"noreturn" "'")};; ++(NumIRFunction_noreturn); }
}
4597};
4598
4599/// NoReturn attribute deduction for a call sites.
4600struct AANoReturnCallSite final : AANoReturnImpl {
4601 AANoReturnCallSite(const IRPosition &IRP, Attributor &A)
4602 : AANoReturnImpl(IRP, A) {}
4603
4604 /// See AbstractAttribute::initialize(...).
4605 void initialize(Attributor &A) override {
4606 AANoReturnImpl::initialize(A);
4607 if (Function *F = getAssociatedFunction()) {
4608 const IRPosition &FnPos = IRPosition::function(*F);
4609 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4610 if (!FnAA.isAssumedNoReturn())
4611 indicatePessimisticFixpoint();
4612 }
4613 }
4614
4615 /// See AbstractAttribute::updateImpl(...).
4616 ChangeStatus updateImpl(Attributor &A) override {
4617 // TODO: Once we have call site specific value information we can provide
4618 // call site specific liveness information and then it makes
4619 // sense to specialize attributes for call sites arguments instead of
4620 // redirecting requests to the callee argument.
4621 Function *F = getAssociatedFunction();
4622 const IRPosition &FnPos = IRPosition::function(*F);
4623 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4624 return clampStateAndIndicateChange(getState(), FnAA.getState());
4625 }
4626
4627 /// See AbstractAttribute::trackStatistics()
4628 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); }
; }
4629};
4630
4631/// ----------------------- Variable Capturing ---------------------------------
4632
4633/// A class to hold the state of for no-capture attributes.
4634struct AANoCaptureImpl : public AANoCapture {
4635 AANoCaptureImpl(const IRPosition &IRP, Attributor &A) : AANoCapture(IRP, A) {}
4636
4637 /// See AbstractAttribute::initialize(...).
4638 void initialize(Attributor &A) override {
4639 if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
4640 indicateOptimisticFixpoint();
4641 return;
4642 }
4643 Function *AnchorScope = getAnchorScope();
4644 if (isFnInterfaceKind() &&
4645 (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) {
4646 indicatePessimisticFixpoint();
4647 return;
4648 }
4649
4650 // You cannot "capture" null in the default address space.
4651 if (isa<ConstantPointerNull>(getAssociatedValue()) &&
4652 getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
4653 indicateOptimisticFixpoint();
4654 return;
4655 }
4656
4657 const Function *F =
4658 isArgumentPosition() ? getAssociatedFunction() : AnchorScope;
4659
4660 // Check what state the associated function can actually capture.
4661 if (F)
4662 determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
4663 else
4664 indicatePessimisticFixpoint();
4665 }
4666
4667 /// See AbstractAttribute::updateImpl(...).
4668 ChangeStatus updateImpl(Attributor &A) override;
4669
4670 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
4671 virtual void
4672 getDeducedAttributes(LLVMContext &Ctx,
4673 SmallVectorImpl<Attribute> &Attrs) const override {
4674 if (!isAssumedNoCaptureMaybeReturned())
4675 return;
4676
4677 if (isArgumentPosition()) {
4678 if (isAssumedNoCapture())
4679 Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
4680 else if (ManifestInternal)
4681 Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
4682 }
4683 }
4684
4685 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
4686 /// depending on the ability of the function associated with \p IRP to capture
4687 /// state in memory and through "returning/throwing", respectively.
4688 static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
4689 const Function &F,
4690 BitIntegerState &State) {
4691 // TODO: Once we have memory behavior attributes we should use them here.
4692
4693 // If we know we cannot communicate or write to memory, we do not care about
4694 // ptr2int anymore.
4695 if (F.onlyReadsMemory() && F.doesNotThrow() &&
4696 F.getReturnType()->isVoidTy()) {
4697 State.addKnownBits(NO_CAPTURE);
4698 return;
4699 }
4700
4701 // A function cannot capture state in memory if it only reads memory, it can
4702 // however return/throw state and the state might be influenced by the
4703 // pointer value, e.g., loading from a returned pointer might reveal a bit.
4704 if (F.onlyReadsMemory())
4705 State.addKnownBits(NOT_CAPTURED_IN_MEM);
4706
4707 // A function cannot communicate state back if it does not through
4708 // exceptions and doesn not return values.
4709 if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
4710 State.addKnownBits(NOT_CAPTURED_IN_RET);
4711
4712 // Check existing "returned" attributes.
4713 int ArgNo = IRP.getCalleeArgNo();
4714 if (F.doesNotThrow() && ArgNo >= 0) {
4715 for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
4716 if (F.hasParamAttribute(u, Attribute::Returned)) {
4717 if (u == unsigned(ArgNo))
4718 State.removeAssumedBits(NOT_CAPTURED_IN_RET);
4719 else if (F.onlyReadsMemory())
4720 State.addKnownBits(NO_CAPTURE);
4721 else
4722 State.addKnownBits(NOT_CAPTURED_IN_RET);
4723 break;
4724 }
4725 }
4726 }
4727
4728 /// See AbstractState::getAsStr().
4729 const std::string getAsStr() const override {
4730 if (isKnownNoCapture())
4731 return "known not-captured";
4732 if (isAssumedNoCapture())
4733 return "assumed not-captured";
4734 if (isKnownNoCaptureMaybeReturned())
4735 return "known not-captured-maybe-returned";
4736 if (isAssumedNoCaptureMaybeReturned())
4737 return "assumed not-captured-maybe-returned";
4738 return "assumed-captured";
4739 }
4740};
4741
4742/// Attributor-aware capture tracker.
4743struct AACaptureUseTracker final : public CaptureTracker {
4744
4745 /// Create a capture tracker that can lookup in-flight abstract attributes
4746 /// through the Attributor \p A.
4747 ///
4748 /// If a use leads to a potential capture, \p CapturedInMemory is set and the
4749 /// search is stopped. If a use leads to a return instruction,
4750 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
4751 /// If a use leads to a ptr2int which may capture the value,
4752 /// \p CapturedInInteger is set. If a use is found that is currently assumed
4753 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
4754 /// set. All values in \p PotentialCopies are later tracked as well. For every
4755 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
4756 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
4757 /// conservatively set to true.
4758 AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
4759 const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
4760 SmallSetVector<Value *, 4> &PotentialCopies,
4761 unsigned &RemainingUsesToExplore)
4762 : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
4763 PotentialCopies(PotentialCopies),
4764 RemainingUsesToExplore(RemainingUsesToExplore) {}
4765
4766 /// Determine if \p V maybe captured. *Also updates the state!*
4767 bool valueMayBeCaptured(const Value *V) {
4768 if (V->getType()->isPointerTy()) {
4769 PointerMayBeCaptured(V, this);
4770 } else {
4771 State.indicatePessimisticFixpoint();
4772 }
4773 return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4774 }
4775
4776 /// See CaptureTracker::tooManyUses().
4777 void tooManyUses() override {
4778 State.removeAssumedBits(AANoCapture::NO_CAPTURE);
4779 }
4780
4781 bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
4782 if (CaptureTracker::isDereferenceableOrNull(O, DL))
4783 return true;
4784 const auto &DerefAA = A.getAAFor<AADereferenceable>(
4785 NoCaptureAA, IRPosition::value(*O), DepClassTy::OPTIONAL);
4786 return DerefAA.getAssumedDereferenceableBytes();
4787 }
4788
4789 /// See CaptureTracker::captured(...).
4790 bool captured(const Use *U) override {
4791 Instruction *UInst = cast<Instruction>(U->getUser());
4792 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)
4793 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "Check use: " << *U->
get() << " in " << *UInst << "\n"; } } while
(false)
;
4794
4795 // Because we may reuse the tracker multiple times we keep track of the
4796 // number of explored uses ourselves as well.
4797 if (RemainingUsesToExplore-- == 0) {
4798 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)
;
4799 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4800 /* Return */ true);
4801 }
4802
4803 // Deal with ptr2int by following uses.
4804 if (isa<PtrToIntInst>(UInst)) {
4805 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << " - ptr2int assume the worst!\n"
; } } while (false)
;
4806 return valueMayBeCaptured(UInst);
4807 }
4808
4809 // For stores we check if we can follow the value through memory or not.
4810 if (auto *SI = dyn_cast<StoreInst>(UInst)) {
4811 if (SI->isVolatile())
4812 return isCapturedIn(/* Memory */ true, /* Integer */ false,
4813 /* Return */ false);
4814 bool UsedAssumedInformation = false;
4815 if (!AA::getPotentialCopiesOfStoredValue(
4816 A, *SI, PotentialCopies, NoCaptureAA, UsedAssumedInformation))
4817 return isCapturedIn(/* Memory */ true, /* Integer */ false,
4818 /* Return */ false);
4819 // Not captured directly, potential copies will be checked.
4820 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4821 /* Return */ false);
4822 }
4823
4824 // Explicitly catch return instructions.
4825 if (isa<ReturnInst>(UInst)) {
4826 if (UInst->getFunction() == NoCaptureAA.getAnchorScope())
4827 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4828 /* Return */ true);
4829 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4830 /* Return */ true);
4831 }
4832
4833 // For now we only use special logic for call sites. However, the tracker
4834 // itself knows about a lot of other non-capturing cases already.
4835 auto *CB = dyn_cast<CallBase>(UInst);
4836 if (!CB || !CB->isArgOperand(U))
4837 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4838 /* Return */ true);
4839
4840 unsigned ArgNo = CB->getArgOperandNo(U);
4841 const IRPosition &CSArgPos = IRPosition::callsite_argument(*CB, ArgNo);
4842 // If we have a abstract no-capture attribute for the argument we can use
4843 // it to justify a non-capture attribute here. This allows recursion!
4844 auto &ArgNoCaptureAA =
4845 A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos, DepClassTy::REQUIRED);
4846 if (ArgNoCaptureAA.isAssumedNoCapture())
4847 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4848 /* Return */ false);
4849 if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
4850 addPotentialCopy(*CB);
4851 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4852 /* Return */ false);
4853 }
4854
4855 // Lastly, we could not find a reason no-capture can be assumed so we don't.
4856 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4857 /* Return */ true);
4858 }
4859
4860 /// Register \p CS as potential copy of the value we are checking.
4861 void addPotentialCopy(CallBase &CB) { PotentialCopies.insert(&CB); }
4862
4863 /// See CaptureTracker::shouldExplore(...).
4864 bool shouldExplore(const Use *U) override {
4865 // Check liveness and ignore droppable users.
4866 bool UsedAssumedInformation = false;
4867 return !U->getUser()->isDroppable() &&
4868 !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA,
4869 UsedAssumedInformation);
4870 }
4871
4872 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
4873 /// \p CapturedInRet, then return the appropriate value for use in the
4874 /// CaptureTracker::captured() interface.
4875 bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
4876 bool CapturedInRet) {
4877 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
)
4878 << CapturedInInt << "|Ret " << CapturedInRet << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << " - captures [Mem " <<
CapturedInMem << "|Int " << CapturedInInt <<
"|Ret " << CapturedInRet << "]\n"; } } while (false
)
;
4879 if (CapturedInMem)
4880 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
4881 if (CapturedInInt)
4882 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
4883 if (CapturedInRet)
4884 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
4885 return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4886 }
4887
4888private:
4889 /// The attributor providing in-flight abstract attributes.
4890 Attributor &A;
4891
4892 /// The abstract attribute currently updated.
4893 AANoCapture &NoCaptureAA;
4894
4895 /// The abstract liveness state.
4896 const AAIsDead &IsDeadAA;
4897
4898 /// The state currently updated.
4899 AANoCapture::StateType &State;
4900
4901 /// Set of potential copies of the tracked value.
4902 SmallSetVector<Value *, 4> &PotentialCopies;
4903
4904 /// Global counter to limit the number of explored uses.
4905 unsigned &RemainingUsesToExplore;
4906};
4907
4908ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
4909 const IRPosition &IRP = getIRPosition();
4910 Value *V = isArgumentPosition() ? IRP.getAssociatedArgument()
4911 : &IRP.getAssociatedValue();
4912 if (!V)
4913 return indicatePessimisticFixpoint();
4914
4915 const Function *F =
4916 isArgumentPosition() ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
4917 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", 4917, __extension__
__PRETTY_FUNCTION__))
;
4918 const IRPosition &FnPos = IRPosition::function(*F);
4919 const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos, DepClassTy::NONE);
4920
4921 AANoCapture::StateType T;
4922
4923 // Readonly means we cannot capture through memory.
4924 const auto &FnMemAA =
4925 A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::NONE);
4926 if (FnMemAA.isAssumedReadOnly()) {
4927 T.addKnownBits(NOT_CAPTURED_IN_MEM);
4928 if (FnMemAA.isKnownReadOnly())
4929 addKnownBits(NOT_CAPTURED_IN_MEM);
4930 else
4931 A.recordDependence(FnMemAA, *this, DepClassTy::OPTIONAL);
4932 }
4933
4934 // Make sure all returned values are different than the underlying value.
4935 // TODO: we could do this in a more sophisticated way inside
4936 // AAReturnedValues, e.g., track all values that escape through returns
4937 // directly somehow.
4938 auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
4939 bool SeenConstant = false;
4940 for (auto &It : RVAA.returned_values()) {
4941 if (isa<Constant>(It.first)) {
4942 if (SeenConstant)
4943 return false;
4944 SeenConstant = true;
4945 } else if (!isa<Argument>(It.first) ||
4946 It.first == getAssociatedArgument())
4947 return false;
4948 }
4949 return true;
4950 };
4951
4952 const auto &NoUnwindAA =
4953 A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::OPTIONAL);
4954 if (NoUnwindAA.isAssumedNoUnwind()) {
4955 bool IsVoidTy = F->getReturnType()->isVoidTy();
4956 const AAReturnedValues *RVAA =
4957 IsVoidTy ? nullptr
4958 : &A.getAAFor<AAReturnedValues>(*this, FnPos,
4959
4960 DepClassTy::OPTIONAL);
4961 if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
4962 T.addKnownBits(NOT_CAPTURED_IN_RET);
4963 if (T.isKnown(NOT_CAPTURED_IN_MEM))
4964 return ChangeStatus::UNCHANGED;
4965 if (NoUnwindAA.isKnownNoUnwind() &&
4966 (IsVoidTy || RVAA->getState().isAtFixpoint())) {
4967 addKnownBits(NOT_CAPTURED_IN_RET);
4968 if (isKnown(NOT_CAPTURED_IN_MEM))
4969 return indicateOptimisticFixpoint();
4970 }
4971 }
4972 }
4973
4974 // Use the CaptureTracker interface and logic with the specialized tracker,
4975 // defined in AACaptureUseTracker, that can look at in-flight abstract
4976 // attributes and directly updates the assumed state.
4977 SmallSetVector<Value *, 4> PotentialCopies;
4978 unsigned RemainingUsesToExplore =
4979 getDefaultMaxUsesToExploreForCaptureTracking();
4980 AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
4981 RemainingUsesToExplore);
4982
4983 // Check all potential copies of the associated value until we can assume
4984 // none will be captured or we have to assume at least one might be.
4985 unsigned Idx = 0;
4986 PotentialCopies.insert(V);
4987 while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
4988 Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
4989
4990 AANoCapture::StateType &S = getState();
4991 auto Assumed = S.getAssumed();
4992 S.intersectAssumedBits(T.getAssumed());
4993 if (!isAssumedNoCaptureMaybeReturned())
4994 return indicatePessimisticFixpoint();
4995 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
4996 : ChangeStatus::CHANGED;
4997}
4998
4999/// NoCapture attribute for function arguments.
5000struct AANoCaptureArgument final : AANoCaptureImpl {
5001 AANoCaptureArgument(const IRPosition &IRP, Attributor &A)
5002 : AANoCaptureImpl(IRP, A) {}
5003
5004 /// See AbstractAttribute::trackStatistics()
5005 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture){ static llvm::Statistic NumIRArguments_nocapture = {"attributor"
, "NumIRArguments_nocapture", ("Number of " "arguments" " marked '"
"nocapture" "'")};; ++(NumIRArguments_nocapture); }
}
5006};
5007
5008/// NoCapture attribute for call site arguments.
5009struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
5010 AANoCaptureCallSiteArgument(const IRPosition &IRP, Attributor &A)
5011 : AANoCaptureImpl(IRP, A) {}
5012
5013 /// See AbstractAttribute::initialize(...).
5014 void initialize(Attributor &A) override {
5015 if (Argument *Arg = getAssociatedArgument())
5016 if (Arg->hasByValAttr())
5017 indicateOptimisticFixpoint();
5018 AANoCaptureImpl::initialize(A);
5019 }
5020
5021 /// See AbstractAttribute::updateImpl(...).
5022 ChangeStatus updateImpl(Attributor &A) override {
5023 // TODO: Once we have call site specific value information we can provide
5024 // call site specific liveness information and then it makes
5025 // sense to specialize attributes for call sites arguments instead of
5026 // redirecting requests to the callee argument.
5027 Argument *Arg = getAssociatedArgument();
5028 if (!Arg)
5029 return indicatePessimisticFixpoint();
5030 const IRPosition &ArgPos = IRPosition::argument(*Arg);
5031 auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos, DepClassTy::REQUIRED);
5032 return clampStateAndIndicateChange(getState(), ArgAA.getState());
5033 }
5034
5035 /// See AbstractAttribute::trackStatistics()
5036 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
); }
};
5037};
5038
5039/// NoCapture attribute for floating values.
5040struct AANoCaptureFloating final : AANoCaptureImpl {
5041 AANoCaptureFloating(const IRPosition &IRP, Attributor &A)
5042 : AANoCaptureImpl(IRP, A) {}
5043
5044 /// See AbstractAttribute::trackStatistics()
5045 void trackStatistics() const override {
5046 STATS_DECLTRACK_FLOATING_ATTR(nocapture){ static llvm::Statistic NumIRFloating_nocapture = {"attributor"
, "NumIRFloating_nocapture", ("Number of floating values known to be '"
"nocapture" "'")};; ++(NumIRFloating_nocapture); }
5047 }
5048};
5049
5050/// NoCapture attribute for function return value.
5051struct AANoCaptureReturned final : AANoCaptureImpl {
5052 AANoCaptureReturned(const IRPosition &IRP, Attributor &A)
5053 : AANoCaptureImpl(IRP, A) {
5054 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", 5054)
;
5055 }
5056
5057 /// See AbstractAttribute::initialize(...).
5058 void initialize(Attributor &A) override {
5059 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", 5059)
;
5060 }
5061
5062 /// See AbstractAttribute::updateImpl(...).
5063 ChangeStatus updateImpl(Attributor &A) override {
5064 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", 5064)
;
5065 }
5066
5067 /// See AbstractAttribute::trackStatistics()
5068 void trackStatistics() const override {}
5069};
5070
5071/// NoCapture attribute deduction for a call site return value.
5072struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
5073 AANoCaptureCallSiteReturned(const IRPosition &IRP, Attributor &A)
5074 : AANoCaptureImpl(IRP, A) {}
5075
5076 /// See AbstractAttribute::initialize(...).
5077 void initialize(Attributor &A) override {
5078 const Function *F = getAnchorScope();
5079 // Check what state the associated function can actually capture.
5080 determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
5081 }
5082
5083 /// See AbstractAttribute::trackStatistics()
5084 void trackStatistics() const override {
5085 STATS_DECLTRACK_CSRET_ATTR(nocapture){ static llvm::Statistic NumIRCSReturn_nocapture = {"attributor"
, "NumIRCSReturn_nocapture", ("Number of " "call site returns"
" marked '" "nocapture" "'")};; ++(NumIRCSReturn_nocapture);
}
5086 }
5087};
5088} // namespace
5089
5090/// ------------------ Value Simplify Attribute ----------------------------
5091
5092bool ValueSimplifyStateType::unionAssumed(Optional<Value *> Other) {
5093 // FIXME: Add a typecast support.
5094 SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5095 SimplifiedAssociatedValue, Other, Ty);
5096 if (SimplifiedAssociatedValue == Optional<Value *>(nullptr))
5097 return false;
5098
5099 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)
5100 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)
5101 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)
5102 << **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)
5103 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)
5104 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)
5105 })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)
;
5106 return true;
5107}
5108
5109namespace {
5110struct AAValueSimplifyImpl : AAValueSimplify {
5111 AAValueSimplifyImpl(const IRPosition &IRP, Attributor &A)
5112 : AAValueSimplify(IRP, A) {}
5113
5114 /// See AbstractAttribute::initialize(...).
5115 void initialize(Attributor &A) override {
5116 if (getAssociatedValue().getType()->isVoidTy())
5117 indicatePessimisticFixpoint();
5118 if (A.hasSimplificationCallback(getIRPosition()))
5119 indicatePessimisticFixpoint();
5120 }
5121
5122 /// See AbstractAttribute::getAsStr().
5123 const std::string getAsStr() const override {
5124 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue
<< " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue
) errs() << "SAV: " << **SimplifiedAssociatedValue
<< " "; }; } } while (false)
5125 errs() << "SAV: " << SimplifiedAssociatedValue << " ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue
<< " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue
) errs() << "SAV: " << **SimplifiedAssociatedValue
<< " "; }; } } while (false)
5126 if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue
<< " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue
) errs() << "SAV: " << **SimplifiedAssociatedValue
<< " "; }; } } while (false)
5127 errs() << "SAV: " << **SimplifiedAssociatedValue << " ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue
<< " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue
) errs() << "SAV: " << **SimplifiedAssociatedValue
<< " "; }; } } while (false)
5128 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { { errs() << "SAV: " << SimplifiedAssociatedValue
<< " "; if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue
) errs() << "SAV: " << **SimplifiedAssociatedValue
<< " "; }; } } while (false)
;
5129 return isValidState() ? (isAtFixpoint() ? "simplified" : "maybe-simple")
5130 : "not-simple";
5131 }
5132
5133 /// See AbstractAttribute::trackStatistics()
5134 void trackStatistics() const override {}
5135
5136 /// See AAValueSimplify::getAssumedSimplifiedValue()
5137 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
5138 return SimplifiedAssociatedValue;
5139 }
5140
5141 /// Return a value we can use as replacement for the associated one, or
5142 /// nullptr if we don't have one that makes sense.
5143 Value *getReplacementValue(Attributor &A) const {
5144 Value *NewV;
5145 NewV = SimplifiedAssociatedValue.hasValue()
5146 ? SimplifiedAssociatedValue.getValue()
5147 : UndefValue::get(getAssociatedType());
5148 if (!NewV)
5149 return nullptr;
5150 NewV = AA::getWithType(*NewV, *getAssociatedType());
5151 if (!NewV || NewV == &getAssociatedValue())
5152 return nullptr;
5153 const Instruction *CtxI = getCtxI();
5154 if (CtxI && !AA::isValidAtPosition(*NewV, *CtxI, A.getInfoCache()))
5155 return nullptr;
5156 if (!CtxI && !AA::isValidInScope(*NewV, getAnchorScope()))
5157 return nullptr;
5158 return NewV;
5159 }
5160
5161 /// Helper function for querying AAValueSimplify and updating candicate.
5162 /// \param IRP The value position we are trying to unify with SimplifiedValue
5163 bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
5164 const IRPosition &IRP, bool Simplify = true) {
5165 bool UsedAssumedInformation = false;
5166 Optional<Value *> QueryingValueSimplified = &IRP.getAssociatedValue();
5167 if (Simplify)
5168 QueryingValueSimplified =
5169 A.getAssumedSimplified(IRP, QueryingAA, UsedAssumedInformation);
5170 return unionAssumed(QueryingValueSimplified);
5171 }
5172
5173 /// Returns a candidate is found or not
5174 template <typename AAType> bool askSimplifiedValueFor(Attributor &A) {
5175 if (!getAssociatedValue().getType()->isIntegerTy())
5176 return false;
5177
5178 // This will also pass the call base context.
5179 const auto &AA =
5180 A.getAAFor<AAType>(*this, getIRPosition(), DepClassTy::NONE);
5181
5182 Optional<ConstantInt *> COpt = AA.getAssumedConstantInt(A);
5183
5184 if (!COpt.hasValue()) {
5185 SimplifiedAssociatedValue = llvm::None;
5186 A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
5187 return true;
5188 }
5189 if (auto *C = COpt.getValue()) {
5190 SimplifiedAssociatedValue = C;
5191 A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
5192 return true;
5193 }
5194 return false;
5195 }
5196
5197 bool askSimplifiedValueForOtherAAs(Attributor &A) {
5198 if (askSimplifiedValueFor<AAValueConstantRange>(A))
5199 return true;
5200 if (askSimplifiedValueFor<AAPotentialValues>(A))
5201 return true;
5202 return false;
5203 }
5204
5205 /// See AbstractAttribute::manifest(...).
5206 ChangeStatus manifest(Attributor &A) override {
5207 ChangeStatus Changed = ChangeStatus::UNCHANGED;
5208 if (getAssociatedValue().user_empty())
5209 return Changed;
5210
5211 if (auto *NewV = getReplacementValue(A)) {
5212 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << getAssociatedValue() << " -> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[ValueSimplify] " <<
getAssociatedValue() << " -> " << *NewV <<
" :: " << *this << "\n"; } } while (false)
5213 << *NewV << " :: " << *this << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[ValueSimplify] " <<
getAssociatedValue() << " -> " << *NewV <<
" :: " << *this << "\n"; } } while (false)
;
5214 if (A.changeValueAfterManifest(getAssociatedValue(), *NewV))
5215 Changed = ChangeStatus::CHANGED;
5216 }
5217
5218 return Changed | AAValueSimplify::manifest(A);
5219 }
5220
5221 /// See AbstractState::indicatePessimisticFixpoint(...).
5222 ChangeStatus indicatePessimisticFixpoint() override {
5223 SimplifiedAssociatedValue = &getAssociatedValue();
5224 return AAValueSimplify::indicatePessimisticFixpoint();
5225 }
5226
5227 static bool handleLoad(Attributor &A, const AbstractAttribute &AA,
5228 LoadInst &L, function_ref<bool(Value &)> Union) {
5229 auto UnionWrapper = [&](Value &V, Value &Obj) {
5230 if (isa<AllocaInst>(Obj))
5231 return Union(V);
5232 if (!AA::isDynamicallyUnique(A, AA, V))
5233 return false;
5234 if (!AA::isValidAtPosition(V, L, A.getInfoCache()))
5235 return false;
5236 return Union(V);
5237 };
5238
5239 Value &Ptr = *L.getPointerOperand();
5240 SmallVector<Value *, 8> Objects;
5241 if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, AA, &L))
5242 return false;
5243
5244 const auto *TLI =
5245 A.getInfoCache().getTargetLibraryInfoForFunction(*L.getFunction());
5246 for (Value *Obj : Objects) {
5247 LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "Visit underlying object " <<
*Obj << "\n"; } } while (false)
;
5248 if (isa<UndefValue>(Obj))
5249 continue;
5250 if (isa<ConstantPointerNull>(Obj)) {
5251 // A null pointer access can be undefined but any offset from null may
5252 // be OK. We do not try to optimize the latter.
5253 bool UsedAssumedInformation = false;
5254 if (!NullPointerIsDefined(L.getFunction(),
5255 Ptr.getType()->getPointerAddressSpace()) &&
5256 A.getAssumedSimplified(Ptr, AA, UsedAssumedInformation) == Obj)
5257 continue;
5258 return false;
5259 }
5260 Constant *InitialVal = AA::getInitialValueForObj(*Obj, *L.getType(), TLI);
5261 if (!InitialVal || !Union(*InitialVal))
5262 return false;
5263
5264 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)
5265 "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)
;
5266
5267 auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
5268 LLVM_DEBUG(dbgs() << " - visit access " << Acc << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << " - visit access " <<
Acc << "\n"; } } while (false)
;
5269 if (!Acc.isWrite())
5270 return true;
5271 if (Acc.isWrittenValueYetUndetermined())
5272 return true;
5273 Value *Content = Acc.getWrittenValue();
5274 if (!Content)
5275 return false;
5276 Value *CastedContent =
5277 AA::getWithType(*Content, *AA.getAssociatedType());
5278 if (!CastedContent)
5279 return false;
5280 if (IsExact)
5281 return UnionWrapper(*CastedContent, *Obj);
5282 if (auto *C = dyn_cast<Constant>(CastedContent))
5283 if (C->isNullValue() || C->isAllOnesValue() || isa<UndefValue>(C))
5284 return UnionWrapper(*CastedContent, *Obj);
5285 return false;
5286 };
5287
5288 auto &PI = A.getAAFor<AAPointerInfo>(AA, IRPosition::value(*Obj),
5289 DepClassTy::REQUIRED);
5290 if (!PI.forallInterferingAccesses(L, CheckAccess))
5291 return false;
5292 }
5293 return true;
5294 }
5295};
5296
5297struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
5298 AAValueSimplifyArgument(const IRPosition &IRP, Attributor &A)
5299 : AAValueSimplifyImpl(IRP, A) {}
5300
5301 void initialize(Attributor &A) override {
5302 AAValueSimplifyImpl::initialize(A);
5303 if (!getAnchorScope() || getAnchorScope()->isDeclaration())
5304 indicatePessimisticFixpoint();
5305 if (hasAttr({Attribute::InAlloca, Attribute::Preallocated,
5306 Attribute::StructRet, Attribute::Nest, Attribute::ByVal},
5307 /* IgnoreSubsumingPositions */ true))
5308 indicatePessimisticFixpoint();
5309
5310 // FIXME: This is a hack to prevent us from propagating function poiner in
5311 // the new pass manager CGSCC pass as it creates call edges the
5312 // CallGraphUpdater cannot handle yet.
5313 Value &V = getAssociatedValue();
5314 if (V.getType()->isPointerTy() &&
5315 V.getType()->getPointerElementType()->isFunctionTy() &&
5316 !A.isModulePass())
5317 indicatePessimisticFixpoint();
5318 }
5319
5320 /// See AbstractAttribute::updateImpl(...).
5321 ChangeStatus updateImpl(Attributor &A) override {
5322 // Byval is only replacable if it is readonly otherwise we would write into
5323 // the replaced value and not the copy that byval creates implicitly.
5324 Argument *Arg = getAssociatedArgument();
5325 if (Arg->hasByValAttr()) {
5326 // TODO: We probably need to verify synchronization is not an issue, e.g.,
5327 // there is no race by not copying a constant byval.
5328 const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(),
5329 DepClassTy::REQUIRED);
5330 if (!MemAA.isAssumedReadOnly())
5331 return indicatePessimisticFixpoint();
5332 }
5333
5334 auto Before = SimplifiedAssociatedValue;
5335
5336 auto PredForCallSite = [&](AbstractCallSite ACS) {
5337 const IRPosition &ACSArgPos =
5338 IRPosition::callsite_argument(ACS, getCallSiteArgNo());
5339 // Check if a coresponding argument was found or if it is on not
5340 // associated (which can happen for callback calls).
5341 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
5342 return false;
5343
5344 // Simplify the argument operand explicitly and check if the result is
5345 // valid in the current scope. This avoids refering to simplified values
5346 // in other functions, e.g., we don't want to say a an argument in a
5347 // static function is actually an argument in a different function.
5348 bool UsedAssumedInformation = false;
5349 Optional<Constant *> SimpleArgOp =
5350 A.getAssumedConstant(ACSArgPos, *this, UsedAssumedInformation);
5351 if (!SimpleArgOp.hasValue())
5352 return true;
5353 if (!SimpleArgOp.getValue())
5354 return false;
5355 if (!AA::isDynamicallyUnique(A, *this, **SimpleArgOp))
5356 return false;
5357 return unionAssumed(*SimpleArgOp);
5358 };
5359
5360 // Generate a answer specific to a call site context.
5361 bool Success;
5362 bool AllCallSitesKnown;
5363 if (hasCallBaseContext() &&
5364 getCallBaseContext()->getCalledFunction() == Arg->getParent())
5365 Success = PredForCallSite(
5366 AbstractCallSite(&getCallBaseContext()->getCalledOperandUse()));
5367 else
5368 Success = A.checkForAllCallSites(PredForCallSite, *this, true,
5369 AllCallSitesKnown);
5370
5371 if (!Success)
5372 if (!askSimplifiedValueForOtherAAs(A))
5373 return indicatePessimisticFixpoint();
5374
5375 // If a candicate was found in this update, return CHANGED.
5376 return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5377 : ChangeStatus ::CHANGED;
5378 }
5379
5380 /// See AbstractAttribute::trackStatistics()
5381 void trackStatistics() const override {
5382 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); }
5383 }
5384};
5385
5386struct AAValueSimplifyReturned : AAValueSimplifyImpl {
5387 AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A)
5388 : AAValueSimplifyImpl(IRP, A) {}
5389
5390 /// See AAValueSimplify::getAssumedSimplifiedValue()
5391 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
5392 if (!isValidState())
5393 return nullptr;
5394 return SimplifiedAssociatedValue;
5395 }
5396
5397 /// See AbstractAttribute::updateImpl(...).
5398 ChangeStatus updateImpl(Attributor &A) override {
5399 auto Before = SimplifiedAssociatedValue;
5400
5401 auto PredForReturned = [&](Value &V) {
5402 return checkAndUpdate(A, *this,
5403 IRPosition::value(V, getCallBaseContext()));
5404 };
5405
5406 if (!A.checkForAllReturnedValues(PredForReturned, *this))
5407 if (!askSimplifiedValueForOtherAAs(A))
5408 return indicatePessimisticFixpoint();
5409
5410 // If a candicate was found in this update, return CHANGED.
5411 return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5412 : ChangeStatus ::CHANGED;
5413 }
5414
5415 ChangeStatus manifest(Attributor &A) override {
5416 ChangeStatus Changed = ChangeStatus::UNCHANGED;
5417
5418 if (auto *NewV = getReplacementValue(A)) {
5419 auto PredForReturned =
5420 [&](Value &, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
5421 for (ReturnInst *RI : RetInsts) {
5422 Value *ReturnedVal = RI->getReturnValue();
5423 if (ReturnedVal == NewV || isa<UndefValue>(ReturnedVal))
5424 return true;
5425 assert(RI->getFunction() == getAnchorScope() &&(static_cast <bool> (RI->getFunction() == getAnchorScope
() && "ReturnInst in wrong function!") ? void (0) : __assert_fail
("RI->getFunction() == getAnchorScope() && \"ReturnInst in wrong function!\""
, "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5426, __extension__
__PRETTY_FUNCTION__))
5426 "ReturnInst in wrong function!")(static_cast <bool> (RI->getFunction() == getAnchorScope
() && "ReturnInst in wrong function!") ? void (0) : __assert_fail
("RI->getFunction() == getAnchorScope() && \"ReturnInst in wrong function!\""
, "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5426, __extension__
__PRETTY_FUNCTION__))
;
5427 LLVM_DEBUG(dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[ValueSimplify] " <<
*ReturnedVal << " -> " << *NewV << " in "
<< *RI << " :: " << *this << "\n"; }
} while (false)
5428 << "[ValueSimplify] " << *ReturnedVal << " -> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[ValueSimplify] " <<
*ReturnedVal << " -> " << *NewV << " in "
<< *RI << " :: " << *this << "\n"; }
} while (false)
5429 << *NewV << " in " << *RI << " :: " << *this << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("attributor")) { dbgs() << "[ValueSimplify] " <<
*ReturnedVal << " -> " << *NewV << " in "
<< *RI << " :: " << *this << "\n"; }
} while (false)
;
5430 if (A.changeUseAfterManifest(RI->getOperandUse(0), *NewV))
5431 Changed = ChangeStatus::CHANGED;
5432 }
5433 return true;
5434 };
5435 A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this);
5436 }
5437
5438 return Changed | AAValueSimplify::manifest(A);
5439 }
5440
5441 /// See AbstractAttribute::trackStatistics()
5442 void trackStatistics() const override {
5443 STATS_DECLTRACK_FNRET_ATTR(value_simplify){ static llvm::Statistic NumIRFunctionReturn_value_simplify =
{"attributor", "NumIRFunctionReturn_value_simplify", ("Number of "
"function returns" " marked '" "value_simplify" "'")};; ++(NumIRFunctionReturn_value_simplify
); }
5444 }
5445};
5446
5447struct AAValueSimplifyFloating : AAValueSimplifyImpl {
5448 AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A)
5449 : AAValueSimplifyImpl(IRP, A) {}
5450
5451 /// See AbstractAttribute::initialize(...).
5452 void initialize(Attributor &A) override {
5453 AAValueSimplifyImpl::initialize(A);
5454 Value &V = getAnchorValue();
5455
5456 // TODO: add other stuffs
5457 if (isa<Constant>(V))
5458 indicatePessimisticFixpoint();
5459 }
5460
5461 /// Check if \p Cmp is a comparison we can simplify.
5462 ///
5463 /// We handle multiple cases, one in which at least one operand is an
5464 /// (assumed) nullptr. If so, try to simplify it using AANonNull on the other
5465 /// operand. Return true if successful, in that case SimplifiedAssociatedValue
5466 /// will be updated.
5467 bool handleCmp(Attributor &A, CmpInst &Cmp) {
5468 auto Union = [&](Value &V) {
5469 SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5470 SimplifiedAssociatedValue, &V, V.getType());
5471 return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5472 };
5473
5474 Value *LHS = Cmp.getOperand(0);
5475 Value *RHS = Cmp.getOperand(1);
5476
5477 // Simplify the operands first.
5478 bool UsedAssumedInformation = false;
5479 const auto &SimplifiedLHS =
5480 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
5481 *this, UsedAssumedInformation);
5482 if (!SimplifiedLHS.hasValue())
5483 return true;
5484 if (!SimplifiedLHS.getValue())
5485 return false;
5486 LHS = *SimplifiedLHS;
5487
5488 const auto &SimplifiedRHS =
5489 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
5490 *this, UsedAssumedInformation);
5491 if (!SimplifiedRHS.hasValue())
5492 return true;
5493 if (!SimplifiedRHS.getValue())
5494 return false;
5495 RHS = *SimplifiedRHS;
5496
5497 LLVMContext &Ctx = Cmp.getContext();
5498 // Handle the trivial case first in which we don't even need to think about
5499 // null or non-null.
5500 if (LHS == RHS && (Cmp.isTrueWhenEqual() || Cmp.isFalseWhenEqual())) {
5501 Constant *NewVal =
5502 ConstantInt::get(Type::getInt1Ty(Ctx), Cmp.isTrueWhenEqual());
5503 if (!Union(*NewVal))
5504 return false;
5505 if (!UsedAssumedInformation)
5506 indicateOptimisticFixpoint();
5507 return true;
5508 }
5509
5510 // From now on we only handle equalities (==, !=).
5511 ICmpInst *ICmp = dyn_cast<ICmpInst>(&Cmp);
5512 if (!ICmp || !ICmp->isEquality())
5513 return false;
5514
5515 bool LHSIsNull = isa<ConstantPointerNull>(LHS);
5516 bool RHSIsNull = isa<ConstantPointerNull>(RHS);
5517 if (!LHSIsNull && !RHSIsNull)
5518 return false;
5519
5520 // Left is the nullptr ==/!= non-nullptr case. We'll use AANonNull on the
5521 // non-nullptr operand and if we assume it's non-null we can conclude the
5522 // result of the comparison.
5523 assert((LHSIsNull || RHSIsNull) &&(static_cast <bool> ((LHSIsNull || RHSIsNull) &&
"Expected nullptr versus non-nullptr comparison at this point"
) ? void (0) : __assert_fail ("(LHSIsNull || RHSIsNull) && \"Expected nullptr versus non-nullptr comparison at this point\""
, "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5524, __extension__
__PRETTY_FUNCTION__))
5524 "Expected nullptr versus non-nullptr comparison at this point")(static_cast <bool> ((LHSIsNull || RHSIsNull) &&
"Expected nullptr versus non-nullptr comparison at this point"
) ? void (0) : __assert_fail ("(LHSIsNull || RHSIsNull) && \"Expected nullptr versus non-nullptr comparison at this point\""
, "llvm/lib/Transforms/IPO/AttributorAttributes.cpp", 5524, __extension__
__PRETTY_FUNCTION__))
;
5525
5526 // The index is the operand that we assume is not null.
5527 unsigned PtrIdx = LHSIsNull;
5528 auto &PtrNonNullAA = A.getAAFor<AANonNull>(