Bug Summary

File:build/source/llvm/include/llvm/Analysis/ValueTracking.h
Warning:line 275, column 47
Called C++ object pointer is null

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/source/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/source/llvm/lib/Transforms/IPO -I include -I /build/source/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/source/build-llvm=build-llvm -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm=build-llvm -fcoverage-prefix-map=/build/source/= -source-date-epoch 1668078801 -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/source/build-llvm -fdebug-prefix-map=/build/source/build-llvm=build-llvm -fdebug-prefix-map=/build/source/= -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-11-10-135928-647445-1 -x c++ /build/source/llvm/lib/Transforms/IPO/AttributorAttributes.cpp

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