Bug Summary

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