Bug Summary

File:src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis/ValueTracking.h
Warning:line 282, column 49
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name AttributorAttributes.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/IPO/AttributorAttributes.cpp

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/IPO/AttributorAttributes.cpp

1//===- AttributorAttributes.cpp - Attributes for Attributor deduction -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// See the Attributor.h file comment and the class descriptions in that file for
10// more information.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/IPO/Attributor.h"
15
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/SCCIterator.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/Analysis/AliasAnalysis.h"
21#include "llvm/Analysis/AssumeBundleQueries.h"
22#include "llvm/Analysis/AssumptionCache.h"
23#include "llvm/Analysis/CaptureTracking.h"
24#include "llvm/Analysis/InstructionSimplify.h"
25#include "llvm/Analysis/LazyValueInfo.h"
26#include "llvm/Analysis/MemoryBuiltins.h"
27#include "llvm/Analysis/OptimizationRemarkEmitter.h"
28#include "llvm/Analysis/ScalarEvolution.h"
29#include "llvm/Analysis/TargetTransformInfo.h"
30#include "llvm/Analysis/ValueTracking.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/IRBuilder.h"
33#include "llvm/IR/Instruction.h"
34#include "llvm/IR/Instructions.h"
35#include "llvm/IR/IntrinsicInst.h"
36#include "llvm/IR/NoFolder.h"
37#include "llvm/Support/Alignment.h"
38#include "llvm/Support/Casting.h"
39#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/ErrorHandling.h"
41#include "llvm/Support/FileSystem.h"
42#include "llvm/Support/raw_ostream.h"
43#include "llvm/Transforms/IPO/ArgumentPromotion.h"
44#include "llvm/Transforms/Utils/Local.h"
45#include <cassert>
46
47using namespace llvm;
48
49#define DEBUG_TYPE"attributor" "attributor"
50
51static cl::opt<bool> ManifestInternal(
52 "attributor-manifest-internal", cl::Hidden,
53 cl::desc("Manifest Attributor internal string attributes."),
54 cl::init(false));
55
56static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
57 cl::Hidden);
58
59template <>
60unsigned llvm::PotentialConstantIntValuesState::MaxPotentialValues = 0;
61
62static cl::opt<unsigned, true> MaxPotentialValues(
63 "attributor-max-potential-values", cl::Hidden,
64 cl::desc("Maximum number of potential values to be "
65 "tracked for each position."),
66 cl::location(llvm::PotentialConstantIntValuesState::MaxPotentialValues),
67 cl::init(7));
68
69STATISTIC(NumAAs, "Number of abstract attributes created")static llvm::Statistic NumAAs = {"attributor", "NumAAs", "Number of abstract attributes created"
}
;
70
71// Some helper macros to deal with statistics tracking.
72//
73// Usage:
74// For simple IR attribute tracking overload trackStatistics in the abstract
75// attribute and choose the right STATS_DECLTRACK_********* macro,
76// e.g.,:
77// void trackStatistics() const override {
78// STATS_DECLTRACK_ARG_ATTR(returned)
79// }
80// If there is a single "increment" side one can use the macro
81// STATS_DECLTRACK with a custom message. If there are multiple increment
82// sides, STATS_DECL and STATS_TRACK can also be used separately.
83//
84#define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME)("Number of " "TYPE" " marked '" "NAME" "'") \
85 ("Number of " #TYPE " marked '" #NAME "'")
86#define BUILD_STAT_NAME(NAME, TYPE)NumIRTYPE_NAME NumIR##TYPE##_##NAME
87#define STATS_DECL_(NAME, MSG)static llvm::Statistic NAME = {"attributor", "NAME", MSG}; STATISTIC(NAME, MSG)static llvm::Statistic NAME = {"attributor", "NAME", MSG};
88#define STATS_DECL(NAME, TYPE, MSG)static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME"
, MSG};;
\
89 STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG)static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME"
, MSG};
;
90#define STATS_TRACK(NAME, TYPE)++(NumIRTYPE_NAME); ++(BUILD_STAT_NAME(NAME, TYPE)NumIRTYPE_NAME);
91#define STATS_DECLTRACK(NAME, TYPE, MSG){ static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME"
, MSG};; ++(NumIRTYPE_NAME); }
\
92 { \
93 STATS_DECL(NAME, TYPE, MSG)static llvm::Statistic NumIRTYPE_NAME = {"attributor", "NumIRTYPE_NAME"
, MSG};;
\
94 STATS_TRACK(NAME, TYPE)++(NumIRTYPE_NAME); \
95 }
96#define STATS_DECLTRACK_ARG_ATTR(NAME){ static llvm::Statistic NumIRArguments_NAME = {"attributor",
"NumIRArguments_NAME", ("Number of " "arguments" " marked '"
"NAME" "'")};; ++(NumIRArguments_NAME); }
\
97 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); }
98#define STATS_DECLTRACK_CSARG_ATTR(NAME){ static llvm::Statistic NumIRCSArguments_NAME = {"attributor"
, "NumIRCSArguments_NAME", ("Number of " "call site arguments"
" marked '" "NAME" "'")};; ++(NumIRCSArguments_NAME); }
\
99 STATS_DECLTRACK(NAME, CSArguments, \{ static llvm::Statistic NumIRCSArguments_NAME = {"attributor"
, "NumIRCSArguments_NAME", ("Number of " "call site arguments"
" marked '" "NAME" "'")};; ++(NumIRCSArguments_NAME); }
100 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); }
101#define STATS_DECLTRACK_FN_ATTR(NAME){ static llvm::Statistic NumIRFunction_NAME = {"attributor", "NumIRFunction_NAME"
, ("Number of " "functions" " marked '" "NAME" "'")};; ++(NumIRFunction_NAME
); }
\
102 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
); }
103#define STATS_DECLTRACK_CS_ATTR(NAME){ static llvm::Statistic NumIRCS_NAME = {"attributor", "NumIRCS_NAME"
, ("Number of " "call site" " marked '" "NAME" "'")};; ++(NumIRCS_NAME
); }
\
104 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
); }
105#define STATS_DECLTRACK_FNRET_ATTR(NAME){ static llvm::Statistic NumIRFunctionReturn_NAME = {"attributor"
, "NumIRFunctionReturn_NAME", ("Number of " "function returns"
" marked '" "NAME" "'")};; ++(NumIRFunctionReturn_NAME); }
\
106 STATS_DECLTRACK(NAME, FunctionReturn, \{ static llvm::Statistic NumIRFunctionReturn_NAME = {"attributor"
, "NumIRFunctionReturn_NAME", ("Number of " "function returns"
" marked '" "NAME" "'")};; ++(NumIRFunctionReturn_NAME); }
107 BUILD_STAT_MSG_IR_ATTR(function returns, NAME)){ static llvm::Statistic NumIRFunctionReturn_NAME = {"attributor"
, "NumIRFunctionReturn_NAME", ("Number of " "function returns"
" marked '" "NAME" "'")};; ++(NumIRFunctionReturn_NAME); }
108#define STATS_DECLTRACK_CSRET_ATTR(NAME){ static llvm::Statistic NumIRCSReturn_NAME = {"attributor", "NumIRCSReturn_NAME"
, ("Number of " "call site returns" " marked '" "NAME" "'")};
; ++(NumIRCSReturn_NAME); }
\
109 STATS_DECLTRACK(NAME, CSReturn, \{ static llvm::Statistic NumIRCSReturn_NAME = {"attributor", "NumIRCSReturn_NAME"
, ("Number of " "call site returns" " marked '" "NAME" "'")};
; ++(NumIRCSReturn_NAME); }
110 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); }
111#define STATS_DECLTRACK_FLOATING_ATTR(NAME){ static llvm::Statistic NumIRFloating_NAME = {"attributor", "NumIRFloating_NAME"
, ("Number of floating values known to be '" "NAME" "'")};; ++
(NumIRFloating_NAME); }
\
112 STATS_DECLTRACK(NAME, Floating, \{ static llvm::Statistic NumIRFloating_NAME = {"attributor", "NumIRFloating_NAME"
, ("Number of floating values known to be '" #NAME "'")};; ++
(NumIRFloating_NAME); }
113 ("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); }
114
115// Specialization of the operator<< for abstract attributes subclasses. This
116// disambiguates situations where multiple operators are applicable.
117namespace llvm {
118#define PIPE_OPERATOR(CLASS) \
119 raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \
120 return OS << static_cast<const AbstractAttribute &>(AA); \
121 }
122
123PIPE_OPERATOR(AAIsDead)
124PIPE_OPERATOR(AANoUnwind)
125PIPE_OPERATOR(AANoSync)
126PIPE_OPERATOR(AANoRecurse)
127PIPE_OPERATOR(AAWillReturn)
128PIPE_OPERATOR(AANoReturn)
129PIPE_OPERATOR(AAReturnedValues)
130PIPE_OPERATOR(AANonNull)
131PIPE_OPERATOR(AANoAlias)
132PIPE_OPERATOR(AADereferenceable)
133PIPE_OPERATOR(AAAlign)
134PIPE_OPERATOR(AANoCapture)
135PIPE_OPERATOR(AAValueSimplify)
136PIPE_OPERATOR(AANoFree)
137PIPE_OPERATOR(AAHeapToStack)
138PIPE_OPERATOR(AAReachability)
139PIPE_OPERATOR(AAMemoryBehavior)
140PIPE_OPERATOR(AAMemoryLocation)
141PIPE_OPERATOR(AAValueConstantRange)
142PIPE_OPERATOR(AAPrivatizablePtr)
143PIPE_OPERATOR(AAUndefinedBehavior)
144PIPE_OPERATOR(AAPotentialValues)
145PIPE_OPERATOR(AANoUndef)
146PIPE_OPERATOR(AACallEdges)
147PIPE_OPERATOR(AAFunctionReachability)
148PIPE_OPERATOR(AAPointerInfo)
149
150#undef PIPE_OPERATOR
151
152template <>
153ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
154 const DerefState &R) {
155 ChangeStatus CS0 =
156 clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
157 ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
158 return CS0 | CS1;
159}
160
161} // namespace llvm
162
163/// Get pointer operand of memory accessing instruction. If \p I is
164/// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
165/// is set to false and the instruction is volatile, return nullptr.
166static const Value *getPointerOperand(const Instruction *I,
167 bool AllowVolatile) {
168 if (!AllowVolatile && I->isVolatile())
169 return nullptr;
170
171 if (auto *LI = dyn_cast<LoadInst>(I)) {
172 return LI->getPointerOperand();
173 }
174
175 if (auto *SI = dyn_cast<StoreInst>(I)) {
176 return SI->getPointerOperand();
177 }
178
179 if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
180 return CXI->getPointerOperand();
181 }
182
183 if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
184 return RMWI->getPointerOperand();
185 }
186
187 return nullptr;
188}
189
190/// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
191/// advanced by \p Offset bytes. To aid later analysis the method tries to build
192/// getelement pointer instructions that traverse the natural type of \p Ptr if
193/// possible. If that fails, the remaining offset is adjusted byte-wise, hence
194/// through a cast to i8*.
195///
196/// TODO: This could probably live somewhere more prominantly if it doesn't
197/// already exist.
198static Value *constructPointer(Type *ResTy, Type *PtrElemTy, Value *Ptr,
199 int64_t Offset, IRBuilder<NoFolder> &IRB,
200 const DataLayout &DL) {
201 assert(Offset >= 0 && "Negative offset not supported yet!")((void)0);
202 LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offsetdo { } while (false)
203 << "-bytes as " << *ResTy << "\n")do { } while (false);
204
205 if (Offset) {
206 SmallVector<Value *, 4> Indices;
207 std::string GEPName = Ptr->getName().str() + ".0";
208
209 // Add 0 index to look through the pointer.
210 assert((uint64_t)Offset < DL.getTypeAllocSize(PtrElemTy) &&((void)0)
211 "Offset out of bounds")((void)0);
212 Indices.push_back(Constant::getNullValue(IRB.getInt32Ty()));
213
214 Type *Ty = PtrElemTy;
215 do {
216 auto *STy = dyn_cast<StructType>(Ty);
217 if (!STy)
218 // Non-aggregate type, we cast and make byte-wise progress now.
219 break;
220
221 const StructLayout *SL = DL.getStructLayout(STy);
222 if (int64_t(SL->getSizeInBytes()) < Offset)
223 break;
224
225 uint64_t Idx = SL->getElementContainingOffset(Offset);
226 assert(Idx < STy->getNumElements() && "Offset calculation error!")((void)0);
227 uint64_t Rem = Offset - SL->getElementOffset(Idx);
228 Ty = STy->getElementType(Idx);
229
230 LLVM_DEBUG(errs() << "Ty: " << *Ty << " Offset: " << Offsetdo { } while (false)
231 << " Idx: " << Idx << " Rem: " << Rem << "\n")do { } while (false);
232
233 GEPName += "." + std::to_string(Idx);
234 Indices.push_back(ConstantInt::get(IRB.getInt32Ty(), Idx));
235 Offset = Rem;
236 } while (Offset);
237
238 // Create a GEP for the indices collected above.
239 Ptr = IRB.CreateGEP(PtrElemTy, Ptr, Indices, GEPName);
240
241 // If an offset is left we use byte-wise adjustment.
242 if (Offset) {
243 Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
244 Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt32(Offset),
245 GEPName + ".b" + Twine(Offset));
246 }
247 }
248
249 // Ensure the result has the requested type.
250 Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast");
251
252 LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n")do { } while (false);
253 return Ptr;
254}
255
256/// Recursively visit all values that might become \p IRP at some point. This
257/// will be done by looking through cast instructions, selects, phis, and calls
258/// with the "returned" attribute. Once we cannot look through the value any
259/// further, the callback \p VisitValueCB is invoked and passed the current
260/// value, the \p State, and a flag to indicate if we stripped anything.
261/// Stripped means that we unpacked the value associated with \p IRP at least
262/// once. Note that the value used for the callback may still be the value
263/// associated with \p IRP (due to PHIs). To limit how much effort is invested,
264/// we will never visit more values than specified by \p MaxValues.
265template <typename StateTy>
266static bool genericValueTraversal(
267 Attributor &A, IRPosition IRP, const AbstractAttribute &QueryingAA,
268 StateTy &State,
269 function_ref<bool(Value &, const Instruction *, StateTy &, bool)>
270 VisitValueCB,
271 const Instruction *CtxI, bool UseValueSimplify = true, int MaxValues = 16,
272 function_ref<Value *(Value *)> StripCB = nullptr) {
273
274 const AAIsDead *LivenessAA = nullptr;
275 if (IRP.getAnchorScope())
276 LivenessAA = &A.getAAFor<AAIsDead>(
277 QueryingAA,
278 IRPosition::function(*IRP.getAnchorScope(), IRP.getCallBaseContext()),
279 DepClassTy::NONE);
280 bool AnyDead = false;
281
282 Value *InitialV = &IRP.getAssociatedValue();
283 using Item = std::pair<Value *, const Instruction *>;
284 SmallSet<Item, 16> Visited;
285 SmallVector<Item, 16> Worklist;
286 Worklist.push_back({InitialV, CtxI});
287
288 int Iteration = 0;
289 do {
290 Item I = Worklist.pop_back_val();
291 Value *V = I.first;
292 CtxI = I.second;
293 if (StripCB)
294 V = StripCB(V);
295
296 // Check if we should process the current value. To prevent endless
297 // recursion keep a record of the values we followed!
298 if (!Visited.insert(I).second)
299 continue;
300
301 // Make sure we limit the compile time for complex expressions.
302 if (Iteration++ >= MaxValues)
303 return false;
304
305 // Explicitly look through calls with a "returned" attribute if we do
306 // not have a pointer as stripPointerCasts only works on them.
307 Value *NewV = nullptr;
308 if (V->getType()->isPointerTy()) {
309 NewV = V->stripPointerCasts();
310 } else {
311 auto *CB = dyn_cast<CallBase>(V);
312 if (CB && CB->getCalledFunction()) {
313 for (Argument &Arg : CB->getCalledFunction()->args())
314 if (Arg.hasReturnedAttr()) {
315 NewV = CB->getArgOperand(Arg.getArgNo());
316 break;
317 }
318 }
319 }
320 if (NewV && NewV != V) {
321 Worklist.push_back({NewV, CtxI});
322 continue;
323 }
324
325 // Look through select instructions, visit assumed potential values.
326 if (auto *SI = dyn_cast<SelectInst>(V)) {
327 bool UsedAssumedInformation = false;
328 Optional<Constant *> C = A.getAssumedConstant(
329 *SI->getCondition(), QueryingAA, UsedAssumedInformation);
330 bool NoValueYet = !C.hasValue();
331 if (NoValueYet || isa_and_nonnull<UndefValue>(*C))
332 continue;
333 if (auto *CI = dyn_cast_or_null<ConstantInt>(*C)) {
334 if (CI->isZero())
335 Worklist.push_back({SI->getFalseValue(), CtxI});
336 else
337 Worklist.push_back({SI->getTrueValue(), CtxI});
338 continue;
339 }
340 // We could not simplify the condition, assume both values.(
341 Worklist.push_back({SI->getTrueValue(), CtxI});
342 Worklist.push_back({SI->getFalseValue(), CtxI});
343 continue;
344 }
345
346 // Look through phi nodes, visit all live operands.
347 if (auto *PHI = dyn_cast<PHINode>(V)) {
348 assert(LivenessAA &&((void)0)
349 "Expected liveness in the presence of instructions!")((void)0);
350 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
351 BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
352 bool UsedAssumedInformation = false;
353 if (A.isAssumedDead(*IncomingBB->getTerminator(), &QueryingAA,
354 LivenessAA, UsedAssumedInformation,
355 /* CheckBBLivenessOnly */ true)) {
356 AnyDead = true;
357 continue;
358 }
359 Worklist.push_back(
360 {PHI->getIncomingValue(u), IncomingBB->getTerminator()});
361 }
362 continue;
363 }
364
365 if (UseValueSimplify && !isa<Constant>(V)) {
366 bool UsedAssumedInformation = false;
367 Optional<Value *> SimpleV =
368 A.getAssumedSimplified(*V, QueryingAA, UsedAssumedInformation);
369 if (!SimpleV.hasValue())
370 continue;
371 if (!SimpleV.getValue())
372 return false;
373 Value *NewV = SimpleV.getValue();
374 if (NewV != V) {
375 Worklist.push_back({NewV, CtxI});
376 continue;
377 }
378 }
379
380 // Once a leaf is reached we inform the user through the callback.
381 if (!VisitValueCB(*V, CtxI, State, Iteration > 1))
382 return false;
383 } while (!Worklist.empty());
384
385 // If we actually used liveness information so we have to record a dependence.
386 if (AnyDead)
387 A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
388
389 // All values have been visited.
390 return true;
391}
392
393bool AA::getAssumedUnderlyingObjects(Attributor &A, const Value &Ptr,
394 SmallVectorImpl<Value *> &Objects,
395 const AbstractAttribute &QueryingAA,
396 const Instruction *CtxI) {
397 auto StripCB = [&](Value *V) { return getUnderlyingObject(V); };
398 SmallPtrSet<Value *, 8> SeenObjects;
399 auto VisitValueCB = [&SeenObjects](Value &Val, const Instruction *,
400 SmallVectorImpl<Value *> &Objects,
401 bool) -> bool {
402 if (SeenObjects.insert(&Val).second)
403 Objects.push_back(&Val);
404 return true;
405 };
406 if (!genericValueTraversal<decltype(Objects)>(
407 A, IRPosition::value(Ptr), QueryingAA, Objects, VisitValueCB, CtxI,
408 true, 32, StripCB))
409 return false;
410 return true;
411}
412
413const Value *stripAndAccumulateMinimalOffsets(
414 Attributor &A, const AbstractAttribute &QueryingAA, const Value *Val,
415 const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
416 bool UseAssumed = false) {
417
418 auto AttributorAnalysis = [&](Value &V, APInt &ROffset) -> bool {
419 const IRPosition &Pos = IRPosition::value(V);
420 // Only track dependence if we are going to use the assumed info.
421 const AAValueConstantRange &ValueConstantRangeAA =
422 A.getAAFor<AAValueConstantRange>(QueryingAA, Pos,
423 UseAssumed ? DepClassTy::OPTIONAL
424 : DepClassTy::NONE);
425 ConstantRange Range = UseAssumed ? ValueConstantRangeAA.getAssumed()
426 : ValueConstantRangeAA.getKnown();
427 // We can only use the lower part of the range because the upper part can
428 // be higher than what the value can really be.
429 ROffset = Range.getSignedMin();
430 return true;
431 };
432
433 return Val->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds,
434 AttributorAnalysis);
435}
436
437static const Value *getMinimalBaseOfAccsesPointerOperand(
438 Attributor &A, const AbstractAttribute &QueryingAA, const Instruction *I,
439 int64_t &BytesOffset, const DataLayout &DL, bool AllowNonInbounds = false) {
440 const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false);
441 if (!Ptr)
442 return nullptr;
443 APInt OffsetAPInt(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
444 const Value *Base = stripAndAccumulateMinimalOffsets(
445 A, QueryingAA, Ptr, DL, OffsetAPInt, AllowNonInbounds);
446
447 BytesOffset = OffsetAPInt.getSExtValue();
448 return Base;
449}
450
451static const Value *
452getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset,
453 const DataLayout &DL,
454 bool AllowNonInbounds = false) {
455 const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false);
456 if (!Ptr)
457 return nullptr;
458
459 return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL,
460 AllowNonInbounds);
461}
462
463/// Clamp the information known for all returned values of a function
464/// (identified by \p QueryingAA) into \p S.
465template <typename AAType, typename StateType = typename AAType::StateType>
466static void clampReturnedValueStates(
467 Attributor &A, const AAType &QueryingAA, StateType &S,
468 const IRPosition::CallBaseContext *CBContext = nullptr) {
469 LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "do { } while (false)
470 << QueryingAA << " into " << S << "\n")do { } while (false);
471
472 assert((QueryingAA.getIRPosition().getPositionKind() ==((void)0)
473 IRPosition::IRP_RETURNED ||((void)0)
474 QueryingAA.getIRPosition().getPositionKind() ==((void)0)
475 IRPosition::IRP_CALL_SITE_RETURNED) &&((void)0)
476 "Can only clamp returned value states for a function returned or call "((void)0)
477 "site returned position!")((void)0);
478
479 // Use an optional state as there might not be any return values and we want
480 // to join (IntegerState::operator&) the state of all there are.
481 Optional<StateType> T;
482
483 // Callback for each possibly returned value.
484 auto CheckReturnValue = [&](Value &RV) -> bool {
485 const IRPosition &RVPos = IRPosition::value(RV, CBContext);
486 const AAType &AA =
487 A.getAAFor<AAType>(QueryingAA, RVPos, DepClassTy::REQUIRED);
488 LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()do { } while (false)
489 << " @ " << RVPos << "\n")do { } while (false);
490 const StateType &AAS = AA.getState();
491 if (T.hasValue())
492 *T &= AAS;
493 else
494 T = AAS;
495 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << Tdo { } while (false)
496 << "\n")do { } while (false);
497 return T->isValidState();
498 };
499
500 if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
501 S.indicatePessimisticFixpoint();
502 else if (T.hasValue())
503 S ^= *T;
504}
505
506/// Helper class for generic deduction: return value -> returned position.
507template <typename AAType, typename BaseType,
508 typename StateType = typename BaseType::StateType,
509 bool PropagateCallBaseContext = false>
510struct AAReturnedFromReturnedValues : public BaseType {
511 AAReturnedFromReturnedValues(const IRPosition &IRP, Attributor &A)
512 : BaseType(IRP, A) {}
513
514 /// See AbstractAttribute::updateImpl(...).
515 ChangeStatus updateImpl(Attributor &A) override {
516 StateType S(StateType::getBestState(this->getState()));
517 clampReturnedValueStates<AAType, StateType>(
518 A, *this, S,
519 PropagateCallBaseContext ? this->getCallBaseContext() : nullptr);
520 // TODO: If we know we visited all returned values, thus no are assumed
521 // dead, we can take the known information from the state T.
522 return clampStateAndIndicateChange<StateType>(this->getState(), S);
523 }
524};
525
526/// Clamp the information known at all call sites for a given argument
527/// (identified by \p QueryingAA) into \p S.
528template <typename AAType, typename StateType = typename AAType::StateType>
529static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
530 StateType &S) {
531 LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "do { } while (false)
532 << QueryingAA << " into " << S << "\n")do { } while (false);
533
534 assert(QueryingAA.getIRPosition().getPositionKind() ==((void)0)
535 IRPosition::IRP_ARGUMENT &&((void)0)
536 "Can only clamp call site argument states for an argument position!")((void)0);
537
538 // Use an optional state as there might not be any return values and we want
539 // to join (IntegerState::operator&) the state of all there are.
540 Optional<StateType> T;
541
542 // The argument number which is also the call site argument number.
543 unsigned ArgNo = QueryingAA.getIRPosition().getCallSiteArgNo();
544
545 auto CallSiteCheck = [&](AbstractCallSite ACS) {
546 const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
547 // Check if a coresponding argument was found or if it is on not associated
548 // (which can happen for callback calls).
549 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
550 return false;
551
552 const AAType &AA =
553 A.getAAFor<AAType>(QueryingAA, ACSArgPos, DepClassTy::REQUIRED);
554 LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()do { } while (false)
555 << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n")do { } while (false);
556 const StateType &AAS = AA.getState();
557 if (T.hasValue())
558 *T &= AAS;
559 else
560 T = AAS;
561 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << Tdo { } while (false)
562 << "\n")do { } while (false);
563 return T->isValidState();
564 };
565
566 bool AllCallSitesKnown;
567 if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
568 AllCallSitesKnown))
569 S.indicatePessimisticFixpoint();
570 else if (T.hasValue())
571 S ^= *T;
572}
573
574/// This function is the bridge between argument position and the call base
575/// context.
576template <typename AAType, typename BaseType,
577 typename StateType = typename AAType::StateType>
578bool getArgumentStateFromCallBaseContext(Attributor &A,
579 BaseType &QueryingAttribute,
580 IRPosition &Pos, StateType &State) {
581 assert((Pos.getPositionKind() == IRPosition::IRP_ARGUMENT) &&((void)0)
582 "Expected an 'argument' position !")((void)0);
583 const CallBase *CBContext = Pos.getCallBaseContext();
584 if (!CBContext)
585 return false;
586
587 int ArgNo = Pos.getCallSiteArgNo();
588 assert(ArgNo >= 0 && "Invalid Arg No!")((void)0);
589
590 const auto &AA = A.getAAFor<AAType>(
591 QueryingAttribute, IRPosition::callsite_argument(*CBContext, ArgNo),
592 DepClassTy::REQUIRED);
593 const StateType &CBArgumentState =
594 static_cast<const StateType &>(AA.getState());
595
596 LLVM_DEBUG(dbgs() << "[Attributor] Briding Call site context to argument"do { } while (false)
597 << "Position:" << Pos << "CB Arg state:" << CBArgumentStatedo { } while (false)
598 << "\n")do { } while (false);
599
600 // NOTE: If we want to do call site grouping it should happen here.
601 State ^= CBArgumentState;
602 return true;
603}
604
605/// Helper class for generic deduction: call site argument -> argument position.
606template <typename AAType, typename BaseType,
607 typename StateType = typename AAType::StateType,
608 bool BridgeCallBaseContext = false>
609struct AAArgumentFromCallSiteArguments : public BaseType {
610 AAArgumentFromCallSiteArguments(const IRPosition &IRP, Attributor &A)
611 : BaseType(IRP, A) {}
612
613 /// See AbstractAttribute::updateImpl(...).
614 ChangeStatus updateImpl(Attributor &A) override {
615 StateType S = StateType::getBestState(this->getState());
616
617 if (BridgeCallBaseContext) {
618 bool Success =
619 getArgumentStateFromCallBaseContext<AAType, BaseType, StateType>(
620 A, *this, this->getIRPosition(), S);
621 if (Success)
622 return clampStateAndIndicateChange<StateType>(this->getState(), S);
623 }
624 clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
625
626 // TODO: If we know we visited all incoming values, thus no are assumed
627 // dead, we can take the known information from the state T.
628 return clampStateAndIndicateChange<StateType>(this->getState(), S);
629 }
630};
631
632/// Helper class for generic replication: function returned -> cs returned.
633template <typename AAType, typename BaseType,
634 typename StateType = typename BaseType::StateType,
635 bool IntroduceCallBaseContext = false>
636struct AACallSiteReturnedFromReturned : public BaseType {
637 AACallSiteReturnedFromReturned(const IRPosition &IRP, Attributor &A)
638 : BaseType(IRP, A) {}
639
640 /// See AbstractAttribute::updateImpl(...).
641 ChangeStatus updateImpl(Attributor &A) override {
642 assert(this->getIRPosition().getPositionKind() ==((void)0)
643 IRPosition::IRP_CALL_SITE_RETURNED &&((void)0)
644 "Can only wrap function returned positions for call site returned "((void)0)
645 "positions!")((void)0);
646 auto &S = this->getState();
647
648 const Function *AssociatedFunction =
649 this->getIRPosition().getAssociatedFunction();
650 if (!AssociatedFunction)
651 return S.indicatePessimisticFixpoint();
652
653 CallBase &CBContext = static_cast<CallBase &>(this->getAnchorValue());
654 if (IntroduceCallBaseContext)
655 LLVM_DEBUG(dbgs() << "[Attributor] Introducing call base context:"do { } while (false)
656 << CBContext << "\n")do { } while (false);
657
658 IRPosition FnPos = IRPosition::returned(
659 *AssociatedFunction, IntroduceCallBaseContext ? &CBContext : nullptr);
660 const AAType &AA = A.getAAFor<AAType>(*this, FnPos, DepClassTy::REQUIRED);
661 return clampStateAndIndicateChange(S, AA.getState());
662 }
663};
664
665/// Helper function to accumulate uses.
666template <class AAType, typename StateType = typename AAType::StateType>
667static void followUsesInContext(AAType &AA, Attributor &A,
668 MustBeExecutedContextExplorer &Explorer,
669 const Instruction *CtxI,
670 SetVector<const Use *> &Uses,
671 StateType &State) {
672 auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
673 for (unsigned u = 0; u < Uses.size(); ++u) {
674 const Use *U = Uses[u];
675 if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
676 bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
677 if (Found && AA.followUseInMBEC(A, U, UserI, State))
678 for (const Use &Us : UserI->uses())
679 Uses.insert(&Us);
680 }
681 }
682}
683
684/// Use the must-be-executed-context around \p I to add information into \p S.
685/// The AAType class is required to have `followUseInMBEC` method with the
686/// following signature and behaviour:
687///
688/// bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I)
689/// U - Underlying use.
690/// I - The user of the \p U.
691/// Returns true if the value should be tracked transitively.
692///
693template <class AAType, typename StateType = typename AAType::StateType>
694static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S,
695 Instruction &CtxI) {
696
697 // Container for (transitive) uses of the associated value.
698 SetVector<const Use *> Uses;
699 for (const Use &U : AA.getIRPosition().getAssociatedValue().uses())
700 Uses.insert(&U);
701
702 MustBeExecutedContextExplorer &Explorer =
703 A.getInfoCache().getMustBeExecutedContextExplorer();
704
705 followUsesInContext<AAType>(AA, A, Explorer, &CtxI, Uses, S);
706
707 if (S.isAtFixpoint())
708 return;
709
710 SmallVector<const BranchInst *, 4> BrInsts;
711 auto Pred = [&](const Instruction *I) {
712 if (const BranchInst *Br = dyn_cast<BranchInst>(I))
713 if (Br->isConditional())
714 BrInsts.push_back(Br);
715 return true;
716 };
717
718 // Here, accumulate conditional branch instructions in the context. We
719 // explore the child paths and collect the known states. The disjunction of
720 // those states can be merged to its own state. Let ParentState_i be a state
721 // to indicate the known information for an i-th branch instruction in the
722 // context. ChildStates are created for its successors respectively.
723 //
724 // ParentS_1 = ChildS_{1, 1} /\ ChildS_{1, 2} /\ ... /\ ChildS_{1, n_1}
725 // ParentS_2 = ChildS_{2, 1} /\ ChildS_{2, 2} /\ ... /\ ChildS_{2, n_2}
726 // ...
727 // ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m}
728 //
729 // Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m
730 //
731 // FIXME: Currently, recursive branches are not handled. For example, we
732 // can't deduce that ptr must be dereferenced in below function.
733 //
734 // void f(int a, int c, int *ptr) {
735 // if(a)
736 // if (b) {
737 // *ptr = 0;
738 // } else {
739 // *ptr = 1;
740 // }
741 // else {
742 // if (b) {
743 // *ptr = 0;
744 // } else {
745 // *ptr = 1;
746 // }
747 // }
748 // }
749
750 Explorer.checkForAllContext(&CtxI, Pred);
751 for (const BranchInst *Br : BrInsts) {
752 StateType ParentState;
753
754 // The known state of the parent state is a conjunction of children's
755 // known states so it is initialized with a best state.
756 ParentState.indicateOptimisticFixpoint();
757
758 for (const BasicBlock *BB : Br->successors()) {
759 StateType ChildState;
760
761 size_t BeforeSize = Uses.size();
762 followUsesInContext(AA, A, Explorer, &BB->front(), Uses, ChildState);
763
764 // Erase uses which only appear in the child.
765 for (auto It = Uses.begin() + BeforeSize; It != Uses.end();)
766 It = Uses.erase(It);
767
768 ParentState &= ChildState;
769 }
770
771 // Use only known state.
772 S += ParentState;
773 }
774}
775
776/// ------------------------ PointerInfo ---------------------------------------
777
778namespace llvm {
779namespace AA {
780namespace PointerInfo {
781
782/// An access kind description as used by AAPointerInfo.
783struct OffsetAndSize;
784
785struct State;
786
787} // namespace PointerInfo
788} // namespace AA
789
790/// Helper for AA::PointerInfo::Acccess DenseMap/Set usage.
791template <>
792struct DenseMapInfo<AAPointerInfo::Access> : DenseMapInfo<Instruction *> {
793 using Access = AAPointerInfo::Access;
794 static inline Access getEmptyKey();
795 static inline Access getTombstoneKey();
796 static unsigned getHashValue(const Access &A);
797 static bool isEqual(const Access &LHS, const Access &RHS);
798};
799
800/// Helper that allows OffsetAndSize as a key in a DenseMap.
801template <>
802struct DenseMapInfo<AA::PointerInfo ::OffsetAndSize>
803 : DenseMapInfo<std::pair<int64_t, int64_t>> {};
804
805/// Helper for AA::PointerInfo::Acccess DenseMap/Set usage ignoring everythign
806/// but the instruction
807struct AccessAsInstructionInfo : DenseMapInfo<Instruction *> {
808 using Base = DenseMapInfo<Instruction *>;
809 using Access = AAPointerInfo::Access;
810 static inline Access getEmptyKey();
811 static inline Access getTombstoneKey();
812 static unsigned getHashValue(const Access &A);
813 static bool isEqual(const Access &LHS, const Access &RHS);
814};
815
816} // namespace llvm
817
818/// Helper to represent an access offset and size, with logic to deal with
819/// uncertainty and check for overlapping accesses.
820struct AA::PointerInfo::OffsetAndSize : public std::pair<int64_t, int64_t> {
821 using BaseTy = std::pair<int64_t, int64_t>;
822 OffsetAndSize(int64_t Offset, int64_t Size) : BaseTy(Offset, Size) {}
823 OffsetAndSize(const BaseTy &P) : BaseTy(P) {}
824 int64_t getOffset() const { return first; }
825 int64_t getSize() const { return second; }
826 static OffsetAndSize getUnknown() { return OffsetAndSize(Unknown, Unknown); }
827
828 /// Return true if this offset and size pair might describe an address that
829 /// overlaps with \p OAS.
830 bool mayOverlap(const OffsetAndSize &OAS) const {
831 // Any unknown value and we are giving up -> overlap.
832 if (OAS.getOffset() == OffsetAndSize::Unknown ||
833 OAS.getSize() == OffsetAndSize::Unknown ||
834 getOffset() == OffsetAndSize::Unknown ||
835 getSize() == OffsetAndSize::Unknown)
836 return true;
837
838 // Check if one offset point is in the other interval [offset, offset+size].
839 return OAS.getOffset() + OAS.getSize() > getOffset() &&
840 OAS.getOffset() < getOffset() + getSize();
841 }
842
843 /// Constant used to represent unknown offset or sizes.
844 static constexpr int64_t Unknown = 1 << 31;
845};
846
847/// Implementation of the DenseMapInfo.
848///
849///{
850inline llvm::AccessAsInstructionInfo::Access
851llvm::AccessAsInstructionInfo::getEmptyKey() {
852 return Access(Base::getEmptyKey(), nullptr, AAPointerInfo::AK_READ, nullptr);
853}
854inline llvm::AccessAsInstructionInfo::Access
855llvm::AccessAsInstructionInfo::getTombstoneKey() {
856 return Access(Base::getTombstoneKey(), nullptr, AAPointerInfo::AK_READ,
857 nullptr);
858}
859unsigned llvm::AccessAsInstructionInfo::getHashValue(
860 const llvm::AccessAsInstructionInfo::Access &A) {
861 return Base::getHashValue(A.getRemoteInst());
862}
863bool llvm::AccessAsInstructionInfo::isEqual(
864 const llvm::AccessAsInstructionInfo::Access &LHS,
865 const llvm::AccessAsInstructionInfo::Access &RHS) {
866 return LHS.getRemoteInst() == RHS.getRemoteInst();
867}
868inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
869llvm::DenseMapInfo<AAPointerInfo::Access>::getEmptyKey() {
870 return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_READ,
871 nullptr);
872}
873inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
874llvm::DenseMapInfo<AAPointerInfo::Access>::getTombstoneKey() {
875 return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_WRITE,
876 nullptr);
877}
878
879unsigned llvm::DenseMapInfo<AAPointerInfo::Access>::getHashValue(
880 const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &A) {
881 return detail::combineHashValue(
882 DenseMapInfo<Instruction *>::getHashValue(A.getRemoteInst()),
883 (A.isWrittenValueYetUndetermined()
884 ? ~0
885 : DenseMapInfo<Value *>::getHashValue(A.getWrittenValue()))) +
886 A.getKind();
887}
888
889bool llvm::DenseMapInfo<AAPointerInfo::Access>::isEqual(
890 const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &LHS,
891 const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &RHS) {
892 return LHS == RHS;
893}
894///}
895
896/// A type to track pointer/struct usage and accesses for AAPointerInfo.
897struct AA::PointerInfo::State : public AbstractState {
898
899 /// Return the best possible representable state.
900 static State getBestState(const State &SIS) { return State(); }
901
902 /// Return the worst possible representable state.
903 static State getWorstState(const State &SIS) {
904 State R;
905 R.indicatePessimisticFixpoint();
906 return R;
907 }
908
909 State() {}
910 State(const State &SIS) : AccessBins(SIS.AccessBins) {}
911 State(State &&SIS) : AccessBins(std::move(SIS.AccessBins)) {}
912
913 const State &getAssumed() const { return *this; }
914
915 /// See AbstractState::isValidState().
916 bool isValidState() const override { return BS.isValidState(); }
917
918 /// See AbstractState::isAtFixpoint().
919 bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
920
921 /// See AbstractState::indicateOptimisticFixpoint().
922 ChangeStatus indicateOptimisticFixpoint() override {
923 BS.indicateOptimisticFixpoint();
924 return ChangeStatus::UNCHANGED;
925 }
926
927 /// See AbstractState::indicatePessimisticFixpoint().
928 ChangeStatus indicatePessimisticFixpoint() override {
929 BS.indicatePessimisticFixpoint();
930 return ChangeStatus::CHANGED;
931 }
932
933 State &operator=(const State &R) {
934 if (this == &R)
935 return *this;
936 BS = R.BS;
937 AccessBins = R.AccessBins;
938 return *this;
939 }
940
941 State &operator=(State &&R) {
942 if (this == &R)
943 return *this;
944 std::swap(BS, R.BS);
945 std::swap(AccessBins, R.AccessBins);
946 return *this;
947 }
948
949 bool operator==(const State &R) const {
950 if (BS != R.BS)
951 return false;
952 if (AccessBins.size() != R.AccessBins.size())
953 return false;
954 auto It = begin(), RIt = R.begin(), E = end();
955 while (It != E) {
956 if (It->getFirst() != RIt->getFirst())
957 return false;
958 auto &Accs = It->getSecond();
959 auto &RAccs = RIt->getSecond();
960 if (Accs.size() != RAccs.size())
961 return false;
962 auto AccIt = Accs.begin(), RAccIt = RAccs.begin(), AccE = Accs.end();
963 while (AccIt != AccE) {
964 if (*AccIt != *RAccIt)
965 return false;
966 ++AccIt;
967 ++RAccIt;
968 }
969 ++It;
970 ++RIt;
971 }
972 return true;
973 }
974 bool operator!=(const State &R) const { return !(*this == R); }
975
976 /// We store accesses in a set with the instruction as key.
977 using Accesses = DenseSet<AAPointerInfo::Access, AccessAsInstructionInfo>;
978
979 /// We store all accesses in bins denoted by their offset and size.
980 using AccessBinsTy = DenseMap<OffsetAndSize, Accesses>;
981
982 AccessBinsTy::const_iterator begin() const { return AccessBins.begin(); }
983 AccessBinsTy::const_iterator end() const { return AccessBins.end(); }
984
985protected:
986 /// The bins with all the accesses for the associated pointer.
987 DenseMap<OffsetAndSize, Accesses> AccessBins;
988
989 /// Add a new access to the state at offset \p Offset and with size \p Size.
990 /// The access is associated with \p I, writes \p Content (if anything), and
991 /// is of kind \p Kind.
992 /// \Returns CHANGED, if the state changed, UNCHANGED otherwise.
993 ChangeStatus addAccess(int64_t Offset, int64_t Size, Instruction &I,
994 Optional<Value *> Content,
995 AAPointerInfo::AccessKind Kind, Type *Ty,
996 Instruction *RemoteI = nullptr,
997 Accesses *BinPtr = nullptr) {
998 OffsetAndSize Key{Offset, Size};
999 Accesses &Bin = BinPtr ? *BinPtr : AccessBins[Key];
1000 AAPointerInfo::Access Acc(&I, RemoteI ? RemoteI : &I, Content, Kind, Ty);
1001 // Check if we have an access for this instruction in this bin, if not,
1002 // simply add it.
1003 auto It = Bin.find(Acc);
1004 if (It == Bin.end()) {
1005 Bin.insert(Acc);
1006 return ChangeStatus::CHANGED;
1007 }
1008 // If the existing access is the same as then new one, nothing changed.
1009 AAPointerInfo::Access Before = *It;
1010 // The new one will be combined with the existing one.
1011 *It &= Acc;
1012 return *It == Before ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED;
1013 }
1014
1015 /// See AAPointerInfo::forallInterferingAccesses.
1016 bool forallInterferingAccesses(
1017 Instruction &I,
1018 function_ref<bool(const AAPointerInfo::Access &, bool)> CB) const {
1019 if (!isValidState())
1020 return false;
1021 // First find the offset and size of I.
1022 OffsetAndSize OAS(-1, -1);
1023 for (auto &It : AccessBins) {
1024 for (auto &Access : It.getSecond()) {
1025 if (Access.getRemoteInst() == &I) {
1026 OAS = It.getFirst();
1027 break;
1028 }
1029 }
1030 if (OAS.getSize() != -1)
1031 break;
1032 }
1033 if (OAS.getSize() == -1)
1034 return true;
1035
1036 // Now that we have an offset and size, find all overlapping ones and use
1037 // the callback on the accesses.
1038 for (auto &It : AccessBins) {
1039 OffsetAndSize ItOAS = It.getFirst();
1040 if (!OAS.mayOverlap(ItOAS))
1041 continue;
1042 for (auto &Access : It.getSecond())
1043 if (!CB(Access, OAS == ItOAS))
1044 return false;
1045 }
1046 return true;
1047 }
1048
1049private:
1050 /// State to track fixpoint and validity.
1051 BooleanState BS;
1052};
1053
1054struct AAPointerInfoImpl
1055 : public StateWrapper<AA::PointerInfo::State, AAPointerInfo> {
1056 using BaseTy = StateWrapper<AA::PointerInfo::State, AAPointerInfo>;
1057 AAPointerInfoImpl(const IRPosition &IRP, Attributor &A) : BaseTy(IRP) {}
1058
1059 /// See AbstractAttribute::initialize(...).
1060 void initialize(Attributor &A) override { AAPointerInfo::initialize(A); }
1061
1062 /// See AbstractAttribute::getAsStr().
1063 const std::string getAsStr() const override {
1064 return std::string("PointerInfo ") +
1065 (isValidState() ? (std::string("#") +
1066 std::to_string(AccessBins.size()) + " bins")
1067 : "<invalid>");
1068 }
1069
1070 /// See AbstractAttribute::manifest(...).
1071 ChangeStatus manifest(Attributor &A) override {
1072 return AAPointerInfo::manifest(A);
1073 }
1074
1075 bool forallInterferingAccesses(
1076 LoadInst &LI, function_ref<bool(const AAPointerInfo::Access &, bool)> CB)
1077 const override {
1078 return State::forallInterferingAccesses(LI, CB);
1079 }
1080 bool forallInterferingAccesses(
1081 StoreInst &SI, function_ref<bool(const AAPointerInfo::Access &, bool)> CB)
1082 const override {
1083 return State::forallInterferingAccesses(SI, CB);
1084 }
1085
1086 ChangeStatus translateAndAddCalleeState(Attributor &A,
1087 const AAPointerInfo &CalleeAA,
1088 int64_t CallArgOffset, CallBase &CB) {
1089 using namespace AA::PointerInfo;
1090 if (!CalleeAA.getState().isValidState() || !isValidState())
1091 return indicatePessimisticFixpoint();
1092
1093 const auto &CalleeImplAA = static_cast<const AAPointerInfoImpl &>(CalleeAA);
1094 bool IsByval = CalleeImplAA.getAssociatedArgument()->hasByValAttr();
1095
1096 // Combine the accesses bin by bin.
1097 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1098 for (auto &It : CalleeImplAA.getState()) {
1099 OffsetAndSize OAS = OffsetAndSize::getUnknown();
1100 if (CallArgOffset != OffsetAndSize::Unknown)
1101 OAS = OffsetAndSize(It.first.getOffset() + CallArgOffset,
1102 It.first.getSize());
1103 Accesses &Bin = AccessBins[OAS];
1104 for (const AAPointerInfo::Access &RAcc : It.second) {
1105 if (IsByval && !RAcc.isRead())
1106 continue;
1107 bool UsedAssumedInformation = false;
1108 Optional<Value *> Content = A.translateArgumentToCallSiteContent(
1109 RAcc.getContent(), CB, *this, UsedAssumedInformation);
1110 AccessKind AK =
1111 AccessKind(RAcc.getKind() & (IsByval ? AccessKind::AK_READ
1112 : AccessKind::AK_READ_WRITE));
1113 Changed =
1114 Changed | addAccess(OAS.getOffset(), OAS.getSize(), CB, Content, AK,
1115 RAcc.getType(), RAcc.getRemoteInst(), &Bin);
1116 }
1117 }
1118 return Changed;
1119 }
1120
1121 /// Statistic tracking for all AAPointerInfo implementations.
1122 /// See AbstractAttribute::trackStatistics().
1123 void trackPointerInfoStatistics(const IRPosition &IRP) const {}
1124};
1125
1126struct AAPointerInfoFloating : public AAPointerInfoImpl {
1127 using AccessKind = AAPointerInfo::AccessKind;
1128 AAPointerInfoFloating(const IRPosition &IRP, Attributor &A)
1129 : AAPointerInfoImpl(IRP, A) {}
1130
1131 /// See AbstractAttribute::initialize(...).
1132 void initialize(Attributor &A) override { AAPointerInfoImpl::initialize(A); }
1133
1134 /// Deal with an access and signal if it was handled successfully.
1135 bool handleAccess(Attributor &A, Instruction &I, Value &Ptr,
1136 Optional<Value *> Content, AccessKind Kind, int64_t Offset,
1137 ChangeStatus &Changed, Type *Ty,
1138 int64_t Size = AA::PointerInfo::OffsetAndSize::Unknown) {
1139 using namespace AA::PointerInfo;
1140 // No need to find a size if one is given or the offset is unknown.
1141 if (Offset != OffsetAndSize::Unknown && Size == OffsetAndSize::Unknown &&
1142 Ty) {
1143 const DataLayout &DL = A.getDataLayout();
1144 TypeSize AccessSize = DL.getTypeStoreSize(Ty);
1145 if (!AccessSize.isScalable())
1146 Size = AccessSize.getFixedSize();
1147 }
1148 Changed = Changed | addAccess(Offset, Size, I, Content, Kind, Ty);
1149 return true;
1150 };
1151
1152 /// Helper struct, will support ranges eventually.
1153 struct OffsetInfo {
1154 int64_t Offset = AA::PointerInfo::OffsetAndSize::Unknown;
1155
1156 bool operator==(const OffsetInfo &OI) const { return Offset == OI.Offset; }
1157 };
1158
1159 /// See AbstractAttribute::updateImpl(...).
1160 ChangeStatus updateImpl(Attributor &A) override {
1161 using namespace AA::PointerInfo;
1162 State S = getState();
1163 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1164 Value &AssociatedValue = getAssociatedValue();
1165
1166 const DataLayout &DL = A.getDataLayout();
1167 DenseMap<Value *, OffsetInfo> OffsetInfoMap;
1168 OffsetInfoMap[&AssociatedValue] = OffsetInfo{0};
1169
1170 auto HandlePassthroughUser = [&](Value *Usr, OffsetInfo &PtrOI,
1171 bool &Follow) {
1172 OffsetInfo &UsrOI = OffsetInfoMap[Usr];
1173 UsrOI = PtrOI;
1174 Follow = true;
1175 return true;
1176 };
1177
1178 auto UsePred = [&](const Use &U, bool &Follow) -> bool {
1179 Value *CurPtr = U.get();
1180 User *Usr = U.getUser();
1181 LLVM_DEBUG(dbgs() << "[AAPointerInfo] Analyze " << *CurPtr << " in "do { } while (false)
1182 << *Usr << "\n")do { } while (false);
1183
1184 OffsetInfo &PtrOI = OffsetInfoMap[CurPtr];
1185
1186 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Usr)) {
1187 if (CE->isCast())
1188 return HandlePassthroughUser(Usr, PtrOI, Follow);
1189 if (CE->isCompare())
1190 return true;
1191 if (!CE->isGEPWithNoNotionalOverIndexing()) {
1192 LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled constant user " << *CEdo { } while (false)
1193 << "\n")do { } while (false);
1194 return false;
1195 }
1196 }
1197 if (auto *GEP = dyn_cast<GEPOperator>(Usr)) {
1198 OffsetInfo &UsrOI = OffsetInfoMap[Usr];
1199 UsrOI = PtrOI;
1200
1201 // TODO: Use range information.
1202 if (PtrOI.Offset == OffsetAndSize::Unknown ||
1203 !GEP->hasAllConstantIndices()) {
1204 UsrOI.Offset = OffsetAndSize::Unknown;
1205 Follow = true;
1206 return true;
1207 }
1208
1209 SmallVector<Value *, 8> Indices;
1210 for (Use &Idx : llvm::make_range(GEP->idx_begin(), GEP->idx_end())) {
1211 if (auto *CIdx = dyn_cast<ConstantInt>(Idx)) {
1212 Indices.push_back(CIdx);
1213 continue;
1214 }
1215
1216 LLVM_DEBUG(dbgs() << "[AAPointerInfo] Non constant GEP index " << *GEPdo { } while (false)
1217 << " : " << *Idx << "\n")do { } while (false);
1218 return false;
1219 }
1220 UsrOI.Offset = PtrOI.Offset +
1221 DL.getIndexedOffsetInType(
1222 CurPtr->getType()->getPointerElementType(), Indices);
1223 Follow = true;
1224 return true;
1225 }
1226 if (isa<CastInst>(Usr) || isa<SelectInst>(Usr))
1227 return HandlePassthroughUser(Usr, PtrOI, Follow);
1228
1229 // For PHIs we need to take care of the recurrence explicitly as the value
1230 // might change while we iterate through a loop. For now, we give up if
1231 // the PHI is not invariant.
1232 if (isa<PHINode>(Usr)) {
1233 // Check if the PHI is invariant (so far).
1234 OffsetInfo &UsrOI = OffsetInfoMap[Usr];
1235 if (UsrOI == PtrOI)
1236 return true;
1237
1238 // Check if the PHI operand has already an unknown offset as we can't
1239 // improve on that anymore.
1240 if (PtrOI.Offset == OffsetAndSize::Unknown) {
1241 UsrOI = PtrOI;
1242 Follow = true;
1243 return true;
1244 }
1245
1246 // Check if the PHI operand is not dependent on the PHI itself.
1247 APInt Offset(DL.getIndexTypeSizeInBits(AssociatedValue.getType()), 0);
1248 if (&AssociatedValue == CurPtr->stripAndAccumulateConstantOffsets(
1249 DL, Offset, /* AllowNonInbounds */ true)) {
1250 if (Offset != PtrOI.Offset) {
1251 LLVM_DEBUG(dbgs()do { } while (false)
1252 << "[AAPointerInfo] PHI operand pointer offset mismatch "do { } while (false)
1253 << *CurPtr << " in " << *Usr << "\n")do { } while (false);
1254 return false;
1255 }
1256 return HandlePassthroughUser(Usr, PtrOI, Follow);
1257 }
1258
1259 // TODO: Approximate in case we know the direction of the recurrence.
1260 LLVM_DEBUG(dbgs() << "[AAPointerInfo] PHI operand is too complex "do { } while (false)
1261 << *CurPtr << " in " << *Usr << "\n")do { } while (false);
1262 UsrOI = PtrOI;
1263 UsrOI.Offset = OffsetAndSize::Unknown;
1264 Follow = true;
1265 return true;
1266 }
1267
1268 if (auto *LoadI = dyn_cast<LoadInst>(Usr))
1269 return handleAccess(A, *LoadI, *CurPtr, /* Content */ nullptr,
1270 AccessKind::AK_READ, PtrOI.Offset, Changed,
1271 LoadI->getType());
1272 if (auto *StoreI = dyn_cast<StoreInst>(Usr)) {
1273 if (StoreI->getValueOperand() == CurPtr) {
1274 LLVM_DEBUG(dbgs() << "[AAPointerInfo] Escaping use in store "do { } while (false)
1275 << *StoreI << "\n")do { } while (false);
1276 return false;
1277 }
1278 bool UsedAssumedInformation = false;
1279 Optional<Value *> Content = A.getAssumedSimplified(
1280 *StoreI->getValueOperand(), *this, UsedAssumedInformation);
1281 return handleAccess(A, *StoreI, *CurPtr, Content, AccessKind::AK_WRITE,
1282 PtrOI.Offset, Changed,
1283 StoreI->getValueOperand()->getType());
1284 }
1285 if (auto *CB = dyn_cast<CallBase>(Usr)) {
1286 if (CB->isLifetimeStartOrEnd())
1287 return true;
1288 if (CB->isArgOperand(&U)) {
1289 unsigned ArgNo = CB->getArgOperandNo(&U);
1290 const auto &CSArgPI = A.getAAFor<AAPointerInfo>(
1291 *this, IRPosition::callsite_argument(*CB, ArgNo),
1292 DepClassTy::REQUIRED);
1293 Changed = translateAndAddCalleeState(A, CSArgPI, PtrOI.Offset, *CB) |
1294 Changed;
1295 return true;
1296 }
1297 LLVM_DEBUG(dbgs() << "[AAPointerInfo] Call user not handled " << *CBdo { } while (false)
1298 << "\n")do { } while (false);
1299 // TODO: Allow some call uses
1300 return false;
1301 }
1302
1303 LLVM_DEBUG(dbgs() << "[AAPointerInfo] User not handled " << *Usr << "\n")do { } while (false);
1304 return false;
1305 };
1306 if (!A.checkForAllUses(UsePred, *this, AssociatedValue,
1307 /* CheckBBLivenessOnly */ true))
1308 return indicatePessimisticFixpoint();
1309
1310 LLVM_DEBUG({do { } while (false)
1311 dbgs() << "Accesses by bin after update:\n";do { } while (false)
1312 for (auto &It : AccessBins) {do { } while (false)
1313 dbgs() << "[" << It.first.getOffset() << "-"do { } while (false)
1314 << It.first.getOffset() + It.first.getSize()do { } while (false)
1315 << "] : " << It.getSecond().size() << "\n";do { } while (false)
1316 for (auto &Acc : It.getSecond()) {do { } while (false)
1317 dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst()do { } while (false)
1318 << "\n";do { } while (false)
1319 if (Acc.getLocalInst() != Acc.getRemoteInst())do { } while (false)
1320 dbgs() << " --> "do { } while (false)
1321 << *Acc.getRemoteInst() << "\n";do { } while (false)
1322 if (!Acc.isWrittenValueYetUndetermined())do { } while (false)
1323 dbgs() << " - " << Acc.getWrittenValue() << "\n";do { } while (false)
1324 }do { } while (false)
1325 }do { } while (false)
1326 })do { } while (false);
1327
1328 return Changed;
1329 }
1330
1331 /// See AbstractAttribute::trackStatistics()
1332 void trackStatistics() const override {
1333 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1334 }
1335};
1336
1337struct AAPointerInfoReturned final : AAPointerInfoImpl {
1338 AAPointerInfoReturned(const IRPosition &IRP, Attributor &A)
1339 : AAPointerInfoImpl(IRP, A) {}
1340
1341 /// See AbstractAttribute::updateImpl(...).
1342 ChangeStatus updateImpl(Attributor &A) override {
1343 return indicatePessimisticFixpoint();
1344 }
1345
1346 /// See AbstractAttribute::trackStatistics()
1347 void trackStatistics() const override {
1348 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1349 }
1350};
1351
1352struct AAPointerInfoArgument final : AAPointerInfoFloating {
1353 AAPointerInfoArgument(const IRPosition &IRP, Attributor &A)
1354 : AAPointerInfoFloating(IRP, A) {}
1355
1356 /// See AbstractAttribute::initialize(...).
1357 void initialize(Attributor &A) override {
1358 AAPointerInfoFloating::initialize(A);
1359 if (getAnchorScope()->isDeclaration())
1360 indicatePessimisticFixpoint();
1361 }
1362
1363 /// See AbstractAttribute::trackStatistics()
1364 void trackStatistics() const override {
1365 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1366 }
1367};
1368
1369struct AAPointerInfoCallSiteArgument final : AAPointerInfoFloating {
1370 AAPointerInfoCallSiteArgument(const IRPosition &IRP, Attributor &A)
1371 : AAPointerInfoFloating(IRP, A) {}
1372
1373 /// See AbstractAttribute::updateImpl(...).
1374 ChangeStatus updateImpl(Attributor &A) override {
1375 using namespace AA::PointerInfo;
1376 // We handle memory intrinsics explicitly, at least the first (=
1377 // destination) and second (=source) arguments as we know how they are
1378 // accessed.
1379 if (auto *MI = dyn_cast_or_null<MemIntrinsic>(getCtxI())) {
1380 ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength());
1381 int64_t LengthVal = OffsetAndSize::Unknown;
1382 if (Length)
1383 LengthVal = Length->getSExtValue();
1384 Value &Ptr = getAssociatedValue();
1385 unsigned ArgNo = getIRPosition().getCallSiteArgNo();
1386 ChangeStatus Changed;
1387 if (ArgNo == 0) {
1388 handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_WRITE, 0, Changed,
1389 nullptr, LengthVal);
1390 } else if (ArgNo == 1) {
1391 handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_READ, 0, Changed,
1392 nullptr, LengthVal);
1393 } else {
1394 LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled memory intrinsic "do { } while (false)
1395 << *MI << "\n")do { } while (false);
1396 return indicatePessimisticFixpoint();
1397 }
1398 return Changed;
1399 }
1400
1401 // TODO: Once we have call site specific value information we can provide
1402 // call site specific liveness information and then it makes
1403 // sense to specialize attributes for call sites arguments instead of
1404 // redirecting requests to the callee argument.
1405 Argument *Arg = getAssociatedArgument();
1406 if (!Arg)
1407 return indicatePessimisticFixpoint();
1408 const IRPosition &ArgPos = IRPosition::argument(*Arg);
1409 auto &ArgAA =
1410 A.getAAFor<AAPointerInfo>(*this, ArgPos, DepClassTy::REQUIRED);
1411 return translateAndAddCalleeState(A, ArgAA, 0, *cast<CallBase>(getCtxI()));
1412 }
1413
1414 /// See AbstractAttribute::trackStatistics()
1415 void trackStatistics() const override {
1416 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1417 }
1418};
1419
1420struct AAPointerInfoCallSiteReturned final : AAPointerInfoFloating {
1421 AAPointerInfoCallSiteReturned(const IRPosition &IRP, Attributor &A)
1422 : AAPointerInfoFloating(IRP, A) {}
1423
1424 /// See AbstractAttribute::trackStatistics()
1425 void trackStatistics() const override {
1426 AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
1427 }
1428};
1429
1430/// -----------------------NoUnwind Function Attribute--------------------------
1431
1432struct AANoUnwindImpl : AANoUnwind {
1433 AANoUnwindImpl(const IRPosition &IRP, Attributor &A) : AANoUnwind(IRP, A) {}
1434
1435 const std::string getAsStr() const override {
1436 return getAssumed() ? "nounwind" : "may-unwind";
1437 }
1438
1439 /// See AbstractAttribute::updateImpl(...).
1440 ChangeStatus updateImpl(Attributor &A) override {
1441 auto Opcodes = {
1442 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
1443 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet,
1444 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
1445
1446 auto CheckForNoUnwind = [&](Instruction &I) {
1447 if (!I.mayThrow())
1448 return true;
1449
1450 if (const auto *CB = dyn_cast<CallBase>(&I)) {
1451 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
1452 *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
1453 return NoUnwindAA.isAssumedNoUnwind();
1454 }
1455 return false;
1456 };
1457
1458 bool UsedAssumedInformation = false;
1459 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes,
1460 UsedAssumedInformation))
1461 return indicatePessimisticFixpoint();
1462
1463 return ChangeStatus::UNCHANGED;
1464 }
1465};
1466
1467struct AANoUnwindFunction final : public AANoUnwindImpl {
1468 AANoUnwindFunction(const IRPosition &IRP, Attributor &A)
1469 : AANoUnwindImpl(IRP, A) {}
1470
1471 /// See AbstractAttribute::trackStatistics()
1472 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind){ static llvm::Statistic NumIRFunction_nounwind = {"attributor"
, "NumIRFunction_nounwind", ("Number of " "functions" " marked '"
"nounwind" "'")};; ++(NumIRFunction_nounwind); }
}
1473};
1474
1475/// NoUnwind attribute deduction for a call sites.
1476struct AANoUnwindCallSite final : AANoUnwindImpl {
1477 AANoUnwindCallSite(const IRPosition &IRP, Attributor &A)
1478 : AANoUnwindImpl(IRP, A) {}
1479
1480 /// See AbstractAttribute::initialize(...).
1481 void initialize(Attributor &A) override {
1482 AANoUnwindImpl::initialize(A);
1483 Function *F = getAssociatedFunction();
1484 if (!F || F->isDeclaration())
1485 indicatePessimisticFixpoint();
1486 }
1487
1488 /// See AbstractAttribute::updateImpl(...).
1489 ChangeStatus updateImpl(Attributor &A) override {
1490 // TODO: Once we have call site specific value information we can provide
1491 // call site specific liveness information and then it makes
1492 // sense to specialize attributes for call sites arguments instead of
1493 // redirecting requests to the callee argument.
1494 Function *F = getAssociatedFunction();
1495 const IRPosition &FnPos = IRPosition::function(*F);
1496 auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::REQUIRED);
1497 return clampStateAndIndicateChange(getState(), FnAA.getState());
1498 }
1499
1500 /// See AbstractAttribute::trackStatistics()
1501 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind){ static llvm::Statistic NumIRCS_nounwind = {"attributor", "NumIRCS_nounwind"
, ("Number of " "call site" " marked '" "nounwind" "'")};; ++
(NumIRCS_nounwind); }
; }
1502};
1503
1504/// --------------------- Function Return Values -------------------------------
1505
1506/// "Attribute" that collects all potential returned values and the return
1507/// instructions that they arise from.
1508///
1509/// If there is a unique returned value R, the manifest method will:
1510/// - mark R with the "returned" attribute, if R is an argument.
1511class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
1512
1513 /// Mapping of values potentially returned by the associated function to the
1514 /// return instructions that might return them.
1515 MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
1516
1517 /// State flags
1518 ///
1519 ///{
1520 bool IsFixed = false;
1521 bool IsValidState = true;
1522 ///}
1523
1524public:
1525 AAReturnedValuesImpl(const IRPosition &IRP, Attributor &A)
1526 : AAReturnedValues(IRP, A) {}
1527
1528 /// See AbstractAttribute::initialize(...).
1529 void initialize(Attributor &A) override {
1530 // Reset the state.
1531 IsFixed = false;
1532 IsValidState = true;
1533 ReturnedValues.clear();
1534
1535 Function *F = getAssociatedFunction();
1536 if (!F || F->isDeclaration()) {
1537 indicatePessimisticFixpoint();
1538 return;
1539 }
1540 assert(!F->getReturnType()->isVoidTy() &&((void)0)
1541 "Did not expect a void return type!")((void)0);
1542
1543 // The map from instruction opcodes to those instructions in the function.
1544 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
1545
1546 // Look through all arguments, if one is marked as returned we are done.
1547 for (Argument &Arg : F->args()) {
1548 if (Arg.hasReturnedAttr()) {
1549 auto &ReturnInstSet = ReturnedValues[&Arg];
1550 if (auto *Insts = OpcodeInstMap.lookup(Instruction::Ret))
1551 for (Instruction *RI : *Insts)
1552 ReturnInstSet.insert(cast<ReturnInst>(RI));
1553
1554 indicateOptimisticFixpoint();
1555 return;
1556 }
1557 }
1558
1559 if (!A.isFunctionIPOAmendable(*F))
1560 indicatePessimisticFixpoint();
1561 }
1562
1563 /// See AbstractAttribute::manifest(...).
1564 ChangeStatus manifest(Attributor &A) override;
1565
1566 /// See AbstractAttribute::getState(...).
1567 AbstractState &getState() override { return *this; }
1568
1569 /// See AbstractAttribute::getState(...).
1570 const AbstractState &getState() const override { return *this; }
1571
1572 /// See AbstractAttribute::updateImpl(Attributor &A).
1573 ChangeStatus updateImpl(Attributor &A) override;
1574
1575 llvm::iterator_range<iterator> returned_values() override {
1576 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1577 }
1578
1579 llvm::iterator_range<const_iterator> returned_values() const override {
1580 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1581 }
1582
1583 /// Return the number of potential return values, -1 if unknown.
1584 size_t getNumReturnValues() const override {
1585 return isValidState() ? ReturnedValues.size() : -1;
1586 }
1587
1588 /// Return an assumed unique return value if a single candidate is found. If
1589 /// there cannot be one, return a nullptr. If it is not clear yet, return the
1590 /// Optional::NoneType.
1591 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
1592
1593 /// See AbstractState::checkForAllReturnedValues(...).
1594 bool checkForAllReturnedValuesAndReturnInsts(
1595 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
1596 const override;
1597
1598 /// Pretty print the attribute similar to the IR representation.
1599 const std::string getAsStr() const override;
1600
1601 /// See AbstractState::isAtFixpoint().
1602 bool isAtFixpoint() const override { return IsFixed; }
1603
1604 /// See AbstractState::isValidState().
1605 bool isValidState() const override { return IsValidState; }
1606
1607 /// See AbstractState::indicateOptimisticFixpoint(...).
1608 ChangeStatus indicateOptimisticFixpoint() override {
1609 IsFixed = true;
1610 return ChangeStatus::UNCHANGED;
1611 }
1612
1613 ChangeStatus indicatePessimisticFixpoint() override {
1614 IsFixed = true;
1615 IsValidState = false;
1616 return ChangeStatus::CHANGED;
1617 }
1618};
1619
1620ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
1621 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1622
1623 // Bookkeeping.
1624 assert(isValidState())((void)0);
1625 STATS_DECLTRACK(KnownReturnValues, FunctionReturn,{ static llvm::Statistic NumIRFunctionReturn_KnownReturnValues
= {"attributor", "NumIRFunctionReturn_KnownReturnValues", "Number of function with known return values"
};; ++(NumIRFunctionReturn_KnownReturnValues); }
1626 "Number of function with known return values"){ static llvm::Statistic NumIRFunctionReturn_KnownReturnValues
= {"attributor", "NumIRFunctionReturn_KnownReturnValues", "Number of function with known return values"
};; ++(NumIRFunctionReturn_KnownReturnValues); }
;
1627
1628 // Check if we have an assumed unique return value that we could manifest.
1629 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
1630
1631 if (!UniqueRV.hasValue() || !UniqueRV.getValue())
1632 return Changed;
1633
1634 // Bookkeeping.
1635 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,{ static llvm::Statistic NumIRFunctionReturn_UniqueReturnValue
= {"attributor", "NumIRFunctionReturn_UniqueReturnValue", "Number of function with unique return"
};; ++(NumIRFunctionReturn_UniqueReturnValue); }
1636 "Number of function with unique return"){ static llvm::Statistic NumIRFunctionReturn_UniqueReturnValue
= {"attributor", "NumIRFunctionReturn_UniqueReturnValue", "Number of function with unique return"
};; ++(NumIRFunctionReturn_UniqueReturnValue); }
;
1637 // If the assumed unique return value is an argument, annotate it.
1638 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
1639 if (UniqueRVArg->getType()->canLosslesslyBitCastTo(
1640 getAssociatedFunction()->getReturnType())) {
1641 getIRPosition() = IRPosition::argument(*UniqueRVArg);
1642 Changed = IRAttribute::manifest(A);
1643 }
1644 }
1645 return Changed;
1646}
1647
1648const std::string AAReturnedValuesImpl::getAsStr() const {
1649 return (isAtFixpoint() ? "returns(#" : "may-return(#") +
1650 (isValidState() ? std::to_string(getNumReturnValues()) : "?") + ")";
1651}
1652
1653Optional<Value *>
1654AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
1655 // If checkForAllReturnedValues provides a unique value, ignoring potential
1656 // undef values that can also be present, it is assumed to be the actual
1657 // return value and forwarded to the caller of this method. If there are
1658 // multiple, a nullptr is returned indicating there cannot be a unique
1659 // returned value.
1660 Optional<Value *> UniqueRV;
1661 Type *Ty = getAssociatedFunction()->getReturnType();
1662
1663 auto Pred = [&](Value &RV) -> bool {
1664 UniqueRV = AA::combineOptionalValuesInAAValueLatice(UniqueRV, &RV, Ty);
1665 return UniqueRV != Optional<Value *>(nullptr);
1666 };
1667
1668 if (!A.checkForAllReturnedValues(Pred, *this))
1669 UniqueRV = nullptr;
1670
1671 return UniqueRV;
1672}
1673
1674bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
1675 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
1676 const {
1677 if (!isValidState())
1678 return false;
1679
1680 // Check all returned values but ignore call sites as long as we have not
1681 // encountered an overdefined one during an update.
1682 for (auto &It : ReturnedValues) {
1683 Value *RV = It.first;
1684 if (!Pred(*RV, It.second))
1685 return false;
1686 }
1687
1688 return true;
1689}
1690
1691ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
1692 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1693
1694 auto ReturnValueCB = [&](Value &V, const Instruction *CtxI, ReturnInst &Ret,
1695 bool) -> bool {
1696 bool UsedAssumedInformation = false;
1697 Optional<Value *> SimpleRetVal =
1698 A.getAssumedSimplified(V, *this, UsedAssumedInformation);
1699 if (!SimpleRetVal.hasValue())
1700 return true;
1701 if (!SimpleRetVal.getValue())
1702 return false;
1703 Value *RetVal = *SimpleRetVal;
1704 assert(AA::isValidInScope(*RetVal, Ret.getFunction()) &&((void)0)
1705 "Assumed returned value should be valid in function scope!")((void)0);
1706 if (ReturnedValues[RetVal].insert(&Ret))
1707 Changed = ChangeStatus::CHANGED;
1708 return true;
1709 };
1710
1711 auto ReturnInstCB = [&](Instruction &I) {
1712 ReturnInst &Ret = cast<ReturnInst>(I);
1713 return genericValueTraversal<ReturnInst>(
1714 A, IRPosition::value(*Ret.getReturnValue()), *this, Ret, ReturnValueCB,
1715 &I);
1716 };
1717
1718 // Discover returned values from all live returned instructions in the
1719 // associated function.
1720 bool UsedAssumedInformation = false;
1721 if (!A.checkForAllInstructions(ReturnInstCB, *this, {Instruction::Ret},
1722 UsedAssumedInformation))
1723 return indicatePessimisticFixpoint();
1724 return Changed;
1725}
1726
1727struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1728 AAReturnedValuesFunction(const IRPosition &IRP, Attributor &A)
1729 : AAReturnedValuesImpl(IRP, A) {}
1730
1731 /// See AbstractAttribute::trackStatistics()
1732 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned){ static llvm::Statistic NumIRArguments_returned = {"attributor"
, "NumIRArguments_returned", ("Number of " "arguments" " marked '"
"returned" "'")};; ++(NumIRArguments_returned); }
}
1733};
1734
1735/// Returned values information for a call sites.
1736struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1737 AAReturnedValuesCallSite(const IRPosition &IRP, Attributor &A)
1738 : AAReturnedValuesImpl(IRP, A) {}
1739
1740 /// See AbstractAttribute::initialize(...).
1741 void initialize(Attributor &A) override {
1742 // TODO: Once we have call site specific value information we can provide
1743 // call site specific liveness information and then it makes
1744 // sense to specialize attributes for call sites instead of
1745 // redirecting requests to the callee.
1746 llvm_unreachable("Abstract attributes for returned values are not "__builtin_unreachable()
1747 "supported for call sites yet!")__builtin_unreachable();
1748 }
1749
1750 /// See AbstractAttribute::updateImpl(...).
1751 ChangeStatus updateImpl(Attributor &A) override {
1752 return indicatePessimisticFixpoint();
1753 }
1754
1755 /// See AbstractAttribute::trackStatistics()
1756 void trackStatistics() const override {}
1757};
1758
1759/// ------------------------ NoSync Function Attribute -------------------------
1760
1761struct AANoSyncImpl : AANoSync {
1762 AANoSyncImpl(const IRPosition &IRP, Attributor &A) : AANoSync(IRP, A) {}
1763
1764 const std::string getAsStr() const override {
1765 return getAssumed() ? "nosync" : "may-sync";
1766 }
1767
1768 /// See AbstractAttribute::updateImpl(...).
1769 ChangeStatus updateImpl(Attributor &A) override;
1770
1771 /// Helper function used to determine whether an instruction is non-relaxed
1772 /// atomic. In other words, if an atomic instruction does not have unordered
1773 /// or monotonic ordering
1774 static bool isNonRelaxedAtomic(Instruction *I);
1775
1776 /// Helper function specific for intrinsics which are potentially volatile
1777 static bool isNoSyncIntrinsic(Instruction *I);
1778};
1779
1780bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
1781 if (!I->isAtomic())
1782 return false;
1783
1784 if (auto *FI = dyn_cast<FenceInst>(I))
1785 // All legal orderings for fence are stronger than monotonic.
1786 return FI->getSyncScopeID() != SyncScope::SingleThread;
1787 else if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I)) {
1788 // Unordered is not a legal ordering for cmpxchg.
1789 return (AI->getSuccessOrdering() != AtomicOrdering::Monotonic ||
1790 AI->getFailureOrdering() != AtomicOrdering::Monotonic);
1791 }
1792
1793 AtomicOrdering Ordering;
1794 switch (I->getOpcode()) {
1795 case Instruction::AtomicRMW:
1796 Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1797 break;
1798 case Instruction::Store:
1799 Ordering = cast<StoreInst>(I)->getOrdering();
1800 break;
1801 case Instruction::Load:
1802 Ordering = cast<LoadInst>(I)->getOrdering();
1803 break;
1804 default:
1805 llvm_unreachable(__builtin_unreachable()
1806 "New atomic operations need to be known in the attributor.")__builtin_unreachable();
1807 }
1808
1809 return (Ordering != AtomicOrdering::Unordered &&
1810 Ordering != AtomicOrdering::Monotonic);
1811}
1812
1813/// Return true if this intrinsic is nosync. This is only used for intrinsics
1814/// which would be nosync except that they have a volatile flag. All other
1815/// intrinsics are simply annotated with the nosync attribute in Intrinsics.td.
1816bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
1817 if (auto *MI = dyn_cast<MemIntrinsic>(I))
1818 return !MI->isVolatile();
1819 return false;
1820}
1821
1822ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
1823
1824 auto CheckRWInstForNoSync = [&](Instruction &I) {
1825 /// We are looking for volatile instructions or Non-Relaxed atomics.
1826
1827 if (const auto *CB = dyn_cast<CallBase>(&I)) {
1828 if (CB->hasFnAttr(Attribute::NoSync))
1829 return true;
1830
1831 if (isNoSyncIntrinsic(&I))
1832 return true;
1833
1834 const auto &NoSyncAA = A.getAAFor<AANoSync>(
1835 *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
1836 return NoSyncAA.isAssumedNoSync();
1837 }
1838
1839 if (!I.isVolatile() && !isNonRelaxedAtomic(&I))
1840 return true;
1841
1842 return false;
1843 };
1844
1845 auto CheckForNoSync = [&](Instruction &I) {
1846 // At this point we handled all read/write effects and they are all
1847 // nosync, so they can be skipped.
1848 if (I.mayReadOrWriteMemory())
1849 return true;
1850
1851 // non-convergent and readnone imply nosync.
1852 return !cast<CallBase>(I).isConvergent();
1853 };
1854
1855 bool UsedAssumedInformation = false;
1856 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this,
1857 UsedAssumedInformation) ||
1858 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this,
1859 UsedAssumedInformation))
1860 return indicatePessimisticFixpoint();
1861
1862 return ChangeStatus::UNCHANGED;
1863}
1864
1865struct AANoSyncFunction final : public AANoSyncImpl {
1866 AANoSyncFunction(const IRPosition &IRP, Attributor &A)
1867 : AANoSyncImpl(IRP, A) {}
1868
1869 /// See AbstractAttribute::trackStatistics()
1870 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync){ static llvm::Statistic NumIRFunction_nosync = {"attributor"
, "NumIRFunction_nosync", ("Number of " "functions" " marked '"
"nosync" "'")};; ++(NumIRFunction_nosync); }
}
1871};
1872
1873/// NoSync attribute deduction for a call sites.
1874struct AANoSyncCallSite final : AANoSyncImpl {
1875 AANoSyncCallSite(const IRPosition &IRP, Attributor &A)
1876 : AANoSyncImpl(IRP, A) {}
1877
1878 /// See AbstractAttribute::initialize(...).
1879 void initialize(Attributor &A) override {
1880 AANoSyncImpl::initialize(A);
1881 Function *F = getAssociatedFunction();
1882 if (!F || F->isDeclaration())
1883 indicatePessimisticFixpoint();
1884 }
1885
1886 /// See AbstractAttribute::updateImpl(...).
1887 ChangeStatus updateImpl(Attributor &A) override {
1888 // TODO: Once we have call site specific value information we can provide
1889 // call site specific liveness information and then it makes
1890 // sense to specialize attributes for call sites arguments instead of
1891 // redirecting requests to the callee argument.
1892 Function *F = getAssociatedFunction();
1893 const IRPosition &FnPos = IRPosition::function(*F);
1894 auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos, DepClassTy::REQUIRED);
1895 return clampStateAndIndicateChange(getState(), FnAA.getState());
1896 }
1897
1898 /// See AbstractAttribute::trackStatistics()
1899 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync){ static llvm::Statistic NumIRCS_nosync = {"attributor", "NumIRCS_nosync"
, ("Number of " "call site" " marked '" "nosync" "'")};; ++(NumIRCS_nosync
); }
; }
1900};
1901
1902/// ------------------------ No-Free Attributes ----------------------------
1903
1904struct AANoFreeImpl : public AANoFree {
1905 AANoFreeImpl(const IRPosition &IRP, Attributor &A) : AANoFree(IRP, A) {}
1906
1907 /// See AbstractAttribute::updateImpl(...).
1908 ChangeStatus updateImpl(Attributor &A) override {
1909 auto CheckForNoFree = [&](Instruction &I) {
1910 const auto &CB = cast<CallBase>(I);
1911 if (CB.hasFnAttr(Attribute::NoFree))
1912 return true;
1913
1914 const auto &NoFreeAA = A.getAAFor<AANoFree>(
1915 *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
1916 return NoFreeAA.isAssumedNoFree();
1917 };
1918
1919 bool UsedAssumedInformation = false;
1920 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this,
1921 UsedAssumedInformation))
1922 return indicatePessimisticFixpoint();
1923 return ChangeStatus::UNCHANGED;
1924 }
1925
1926 /// See AbstractAttribute::getAsStr().
1927 const std::string getAsStr() const override {
1928 return getAssumed() ? "nofree" : "may-free";
1929 }
1930};
1931
1932struct AANoFreeFunction final : public AANoFreeImpl {
1933 AANoFreeFunction(const IRPosition &IRP, Attributor &A)
1934 : AANoFreeImpl(IRP, A) {}
1935
1936 /// See AbstractAttribute::trackStatistics()
1937 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree){ static llvm::Statistic NumIRFunction_nofree = {"attributor"
, "NumIRFunction_nofree", ("Number of " "functions" " marked '"
"nofree" "'")};; ++(NumIRFunction_nofree); }
}
1938};
1939
1940/// NoFree attribute deduction for a call sites.
1941struct AANoFreeCallSite final : AANoFreeImpl {
1942 AANoFreeCallSite(const IRPosition &IRP, Attributor &A)
1943 : AANoFreeImpl(IRP, A) {}
1944
1945 /// See AbstractAttribute::initialize(...).
1946 void initialize(Attributor &A) override {
1947 AANoFreeImpl::initialize(A);
1948 Function *F = getAssociatedFunction();
1949 if (!F || F->isDeclaration())
1950 indicatePessimisticFixpoint();
1951 }
1952
1953 /// See AbstractAttribute::updateImpl(...).
1954 ChangeStatus updateImpl(Attributor &A) override {
1955 // TODO: Once we have call site specific value information we can provide
1956 // call site specific liveness information and then it makes
1957 // sense to specialize attributes for call sites arguments instead of
1958 // redirecting requests to the callee argument.
1959 Function *F = getAssociatedFunction();
1960 const IRPosition &FnPos = IRPosition::function(*F);
1961 auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos, DepClassTy::REQUIRED);
1962 return clampStateAndIndicateChange(getState(), FnAA.getState());
1963 }
1964
1965 /// See AbstractAttribute::trackStatistics()
1966 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree){ static llvm::Statistic NumIRCS_nofree = {"attributor", "NumIRCS_nofree"
, ("Number of " "call site" " marked '" "nofree" "'")};; ++(NumIRCS_nofree
); }
; }
1967};
1968
1969/// NoFree attribute for floating values.
1970struct AANoFreeFloating : AANoFreeImpl {
1971 AANoFreeFloating(const IRPosition &IRP, Attributor &A)
1972 : AANoFreeImpl(IRP, A) {}
1973
1974 /// See AbstractAttribute::trackStatistics()
1975 void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree){ static llvm::Statistic NumIRFloating_nofree = {"attributor"
, "NumIRFloating_nofree", ("Number of floating values known to be '"
"nofree" "'")};; ++(NumIRFloating_nofree); }
}
1976
1977 /// See Abstract Attribute::updateImpl(...).
1978 ChangeStatus updateImpl(Attributor &A) override {
1979 const IRPosition &IRP = getIRPosition();
1980
1981 const auto &NoFreeAA = A.getAAFor<AANoFree>(
1982 *this, IRPosition::function_scope(IRP), DepClassTy::OPTIONAL);
1983 if (NoFreeAA.isAssumedNoFree())
1984 return ChangeStatus::UNCHANGED;
1985
1986 Value &AssociatedValue = getIRPosition().getAssociatedValue();
1987 auto Pred = [&](const Use &U, bool &Follow) -> bool {
1988 Instruction *UserI = cast<Instruction>(U.getUser());
1989 if (auto *CB = dyn_cast<CallBase>(UserI)) {
1990 if (CB->isBundleOperand(&U))
1991 return false;
1992 if (!CB->isArgOperand(&U))
1993 return true;
1994 unsigned ArgNo = CB->getArgOperandNo(&U);
1995
1996 const auto &NoFreeArg = A.getAAFor<AANoFree>(
1997 *this, IRPosition::callsite_argument(*CB, ArgNo),
1998 DepClassTy::REQUIRED);
1999 return NoFreeArg.isAssumedNoFree();
2000 }
2001
2002 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
2003 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
2004 Follow = true;
2005 return true;
2006 }
2007 if (isa<StoreInst>(UserI) || isa<LoadInst>(UserI) ||
2008 isa<ReturnInst>(UserI))
2009 return true;
2010
2011 // Unknown user.
2012 return false;
2013 };
2014 if (!A.checkForAllUses(Pred, *this, AssociatedValue))
2015 return indicatePessimisticFixpoint();
2016
2017 return ChangeStatus::UNCHANGED;
2018 }
2019};
2020
2021/// NoFree attribute for a call site argument.
2022struct AANoFreeArgument final : AANoFreeFloating {
2023 AANoFreeArgument(const IRPosition &IRP, Attributor &A)
2024 : AANoFreeFloating(IRP, A) {}
2025
2026 /// See AbstractAttribute::trackStatistics()
2027 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree){ static llvm::Statistic NumIRArguments_nofree = {"attributor"
, "NumIRArguments_nofree", ("Number of " "arguments" " marked '"
"nofree" "'")};; ++(NumIRArguments_nofree); }
}
2028};
2029
2030/// NoFree attribute for call site arguments.
2031struct AANoFreeCallSiteArgument final : AANoFreeFloating {
2032 AANoFreeCallSiteArgument(const IRPosition &IRP, Attributor &A)
2033 : AANoFreeFloating(IRP, A) {}
2034
2035 /// See AbstractAttribute::updateImpl(...).
2036 ChangeStatus updateImpl(Attributor &A) override {
2037 // TODO: Once we have call site specific value information we can provide
2038 // call site specific liveness information and then it makes
2039 // sense to specialize attributes for call sites arguments instead of
2040 // redirecting requests to the callee argument.
2041 Argument *Arg = getAssociatedArgument();
2042 if (!Arg)
2043 return indicatePessimisticFixpoint();
2044 const IRPosition &ArgPos = IRPosition::argument(*Arg);
2045 auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos, DepClassTy::REQUIRED);
2046 return clampStateAndIndicateChange(getState(), ArgAA.getState());
2047 }
2048
2049 /// See AbstractAttribute::trackStatistics()
2050 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree){ static llvm::Statistic NumIRCSArguments_nofree = {"attributor"
, "NumIRCSArguments_nofree", ("Number of " "call site arguments"
" marked '" "nofree" "'")};; ++(NumIRCSArguments_nofree); }
};
2051};
2052
2053/// NoFree attribute for function return value.
2054struct AANoFreeReturned final : AANoFreeFloating {
2055 AANoFreeReturned(const IRPosition &IRP, Attributor &A)
2056 : AANoFreeFloating(IRP, A) {
2057 llvm_unreachable("NoFree is not applicable to function returns!")__builtin_unreachable();
2058 }
2059
2060 /// See AbstractAttribute::initialize(...).
2061 void initialize(Attributor &A) override {
2062 llvm_unreachable("NoFree is not applicable to function returns!")__builtin_unreachable();
2063 }
2064
2065 /// See AbstractAttribute::updateImpl(...).
2066 ChangeStatus updateImpl(Attributor &A) override {
2067 llvm_unreachable("NoFree is not applicable to function returns!")__builtin_unreachable();
2068 }
2069
2070 /// See AbstractAttribute::trackStatistics()
2071 void trackStatistics() const override {}
2072};
2073
2074/// NoFree attribute deduction for a call site return value.
2075struct AANoFreeCallSiteReturned final : AANoFreeFloating {
2076 AANoFreeCallSiteReturned(const IRPosition &IRP, Attributor &A)
2077 : AANoFreeFloating(IRP, A) {}
2078
2079 ChangeStatus manifest(Attributor &A) override {
2080 return ChangeStatus::UNCHANGED;
2081 }
2082 /// See AbstractAttribute::trackStatistics()
2083 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree){ static llvm::Statistic NumIRCSReturn_nofree = {"attributor"
, "NumIRCSReturn_nofree", ("Number of " "call site returns" " marked '"
"nofree" "'")};; ++(NumIRCSReturn_nofree); }
}
2084};
2085
2086/// ------------------------ NonNull Argument Attribute ------------------------
2087static int64_t getKnownNonNullAndDerefBytesForUse(
2088 Attributor &A, const AbstractAttribute &QueryingAA, Value &AssociatedValue,
2089 const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
2090 TrackUse = false;
2091
2092 const Value *UseV = U->get();
2093 if (!UseV->getType()->isPointerTy())
2094 return 0;
2095
2096 // We need to follow common pointer manipulation uses to the accesses they
2097 // feed into. We can try to be smart to avoid looking through things we do not
2098 // like for now, e.g., non-inbounds GEPs.
2099 if (isa<CastInst>(I)) {
2100 TrackUse = true;
2101 return 0;
2102 }
2103
2104 if (isa<GetElementPtrInst>(I)) {
2105 TrackUse = true;
2106 return 0;
2107 }
2108
2109 Type *PtrTy = UseV->getType();
2110 const Function *F = I->getFunction();
2111 bool NullPointerIsDefined =
2112 F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
2113 const DataLayout &DL = A.getInfoCache().getDL();
2114 if (const auto *CB = dyn_cast<CallBase>(I)) {
2115 if (CB->isBundleOperand(U)) {
2116 if (RetainedKnowledge RK = getKnowledgeFromUse(
2117 U, {Attribute::NonNull, Attribute::Dereferenceable})) {
2118 IsNonNull |=
2119 (RK.AttrKind == Attribute::NonNull || !NullPointerIsDefined);
2120 return RK.ArgValue;
2121 }
2122 return 0;
2123 }
2124
2125 if (CB->isCallee(U)) {
2126 IsNonNull |= !NullPointerIsDefined;
2127 return 0;
2128 }
2129
2130 unsigned ArgNo = CB->getArgOperandNo(U);
2131 IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
2132 // As long as we only use known information there is no need to track
2133 // dependences here.
2134 auto &DerefAA =
2135 A.getAAFor<AADereferenceable>(QueryingAA, IRP, DepClassTy::NONE);
2136 IsNonNull |= DerefAA.isKnownNonNull();
2137 return DerefAA.getKnownDereferenceableBytes();
2138 }
2139
2140 int64_t Offset;
2141 const Value *Base =
2142 getMinimalBaseOfAccsesPointerOperand(A, QueryingAA, I, Offset, DL);
2143 if (Base) {
2144 if (Base == &AssociatedValue &&
2145 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
2146 int64_t DerefBytes =
2147 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset;
2148
2149 IsNonNull |= !NullPointerIsDefined;
2150 return std::max(int64_t(0), DerefBytes);
2151 }
2152 }
2153
2154 /// Corner case when an offset is 0.
2155 Base = getBasePointerOfAccessPointerOperand(I, Offset, DL,
2156 /*AllowNonInbounds*/ true);
2157 if (Base) {
2158 if (Offset == 0 && Base == &AssociatedValue &&
2159 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
2160 int64_t DerefBytes =
2161 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType());
2162 IsNonNull |= !NullPointerIsDefined;
2163 return std::max(int64_t(0), DerefBytes);
2164 }
2165 }
2166
2167 return 0;
2168}
2169
2170struct AANonNullImpl : AANonNull {
2171 AANonNullImpl(const IRPosition &IRP, Attributor &A)
2172 : AANonNull(IRP, A),
2173 NullIsDefined(NullPointerIsDefined(
2174 getAnchorScope(),
2175 getAssociatedValue().getType()->getPointerAddressSpace())) {}
2176
2177 /// See AbstractAttribute::initialize(...).
2178 void initialize(Attributor &A) override {
2179 Value &V = getAssociatedValue();
2180 if (!NullIsDefined &&
2181 hasAttr({Attribute::NonNull, Attribute::Dereferenceable},
2182 /* IgnoreSubsumingPositions */ false, &A)) {
2183 indicateOptimisticFixpoint();
2184 return;
2185 }
2186
2187 if (isa<ConstantPointerNull>(V)) {
2188 indicatePessimisticFixpoint();
2189 return;
2190 }
2191
2192 AANonNull::initialize(A);
2193
2194 bool CanBeNull, CanBeFreed;
2195 if (V.getPointerDereferenceableBytes(A.getDataLayout(), CanBeNull,
2196 CanBeFreed)) {
2197 if (!CanBeNull) {
2198 indicateOptimisticFixpoint();
2199 return;
2200 }
2201 }
2202
2203 if (isa<GlobalValue>(&getAssociatedValue())) {
2204 indicatePessimisticFixpoint();
2205 return;
2206 }
2207
2208 if (Instruction *CtxI = getCtxI())
2209 followUsesInMBEC(*this, A, getState(), *CtxI);
2210 }
2211
2212 /// See followUsesInMBEC
2213 bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
2214 AANonNull::StateType &State) {
2215 bool IsNonNull = false;
2216 bool TrackUse = false;
2217 getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
2218 IsNonNull, TrackUse);
2219 State.setKnown(IsNonNull);
2220 return TrackUse;
2221 }
2222
2223 /// See AbstractAttribute::getAsStr().
2224 const std::string getAsStr() const override {
2225 return getAssumed() ? "nonnull" : "may-null";
2226 }
2227
2228 /// Flag to determine if the underlying value can be null and still allow
2229 /// valid accesses.
2230 const bool NullIsDefined;
2231};
2232
2233/// NonNull attribute for a floating value.
2234struct AANonNullFloating : public AANonNullImpl {
2235 AANonNullFloating(const IRPosition &IRP, Attributor &A)
2236 : AANonNullImpl(IRP, A) {}
2237
2238 /// See AbstractAttribute::updateImpl(...).
2239 ChangeStatus updateImpl(Attributor &A) override {
2240 const DataLayout &DL = A.getDataLayout();
2241
2242 DominatorTree *DT = nullptr;
2243 AssumptionCache *AC = nullptr;
2244 InformationCache &InfoCache = A.getInfoCache();
2245 if (const Function *Fn = getAnchorScope()) {
2246 DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
2247 AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*Fn);
2248 }
2249
2250 auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
2251 AANonNull::StateType &T, bool Stripped) -> bool {
2252 const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V),
2253 DepClassTy::REQUIRED);
2254 if (!Stripped && this == &AA) {
2255 if (!isKnownNonZero(&V, DL, 0, AC, CtxI, DT))
2256 T.indicatePessimisticFixpoint();
2257 } else {
2258 // Use abstract attribute information.
2259 const AANonNull::StateType &NS = AA.getState();
2260 T ^= NS;
2261 }
2262 return T.isValidState();
2263 };
2264
2265 StateType T;
2266 if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
2267 VisitValueCB, getCtxI()))
2268 return indicatePessimisticFixpoint();
2269
2270 return clampStateAndIndicateChange(getState(), T);
2271 }
2272
2273 /// See AbstractAttribute::trackStatistics()
2274 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
); }
}
2275};
2276
2277/// NonNull attribute for function return value.
2278struct AANonNullReturned final
2279 : AAReturnedFromReturnedValues<AANonNull, AANonNull> {
2280 AANonNullReturned(const IRPosition &IRP, Attributor &A)
2281 : AAReturnedFromReturnedValues<AANonNull, AANonNull>(IRP, A) {}
2282
2283 /// See AbstractAttribute::getAsStr().
2284 const std::string getAsStr() const override {
2285 return getAssumed() ? "nonnull" : "may-null";
2286 }
2287
2288 /// See AbstractAttribute::trackStatistics()
2289 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
); }
}
2290};
2291
2292/// NonNull attribute for function argument.
2293struct AANonNullArgument final
2294 : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> {
2295 AANonNullArgument(const IRPosition &IRP, Attributor &A)
2296 : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP, A) {}
2297
2298 /// See AbstractAttribute::trackStatistics()
2299 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull){ static llvm::Statistic NumIRArguments_nonnull = {"attributor"
, "NumIRArguments_nonnull", ("Number of " "arguments" " marked '"
"nonnull" "'")};; ++(NumIRArguments_nonnull); }
}
2300};
2301
2302struct AANonNullCallSiteArgument final : AANonNullFloating {
2303 AANonNullCallSiteArgument(const IRPosition &IRP, Attributor &A)
2304 : AANonNullFloating(IRP, A) {}
2305
2306 /// See AbstractAttribute::trackStatistics()
2307 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); }
}
2308};
2309
2310/// NonNull attribute for a call site return position.
2311struct AANonNullCallSiteReturned final
2312 : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> {
2313 AANonNullCallSiteReturned(const IRPosition &IRP, Attributor &A)
2314 : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP, A) {}
2315
2316 /// See AbstractAttribute::trackStatistics()
2317 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); }
}
2318};
2319
2320/// ------------------------ No-Recurse Attributes ----------------------------
2321
2322struct AANoRecurseImpl : public AANoRecurse {
2323 AANoRecurseImpl(const IRPosition &IRP, Attributor &A) : AANoRecurse(IRP, A) {}
2324
2325 /// See AbstractAttribute::getAsStr()
2326 const std::string getAsStr() const override {
2327 return getAssumed() ? "norecurse" : "may-recurse";
2328 }
2329};
2330
2331struct AANoRecurseFunction final : AANoRecurseImpl {
2332 AANoRecurseFunction(const IRPosition &IRP, Attributor &A)
2333 : AANoRecurseImpl(IRP, A) {}
2334
2335 /// See AbstractAttribute::initialize(...).
2336 void initialize(Attributor &A) override {
2337 AANoRecurseImpl::initialize(A);
2338 if (const Function *F = getAnchorScope())
2339 if (A.getInfoCache().getSccSize(*F) != 1)
2340 indicatePessimisticFixpoint();
2341 }
2342
2343 /// See AbstractAttribute::updateImpl(...).
2344 ChangeStatus updateImpl(Attributor &A) override {
2345
2346 // If all live call sites are known to be no-recurse, we are as well.
2347 auto CallSitePred = [&](AbstractCallSite ACS) {
2348 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
2349 *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
2350 DepClassTy::NONE);
2351 return NoRecurseAA.isKnownNoRecurse();
2352 };
2353 bool AllCallSitesKnown;
2354 if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) {
2355 // If we know all call sites and all are known no-recurse, we are done.
2356 // If all known call sites, which might not be all that exist, are known
2357 // to be no-recurse, we are not done but we can continue to assume
2358 // no-recurse. If one of the call sites we have not visited will become
2359 // live, another update is triggered.
2360 if (AllCallSitesKnown)
2361 indicateOptimisticFixpoint();
2362 return ChangeStatus::UNCHANGED;
2363 }
2364
2365 // If the above check does not hold anymore we look at the calls.
2366 auto CheckForNoRecurse = [&](Instruction &I) {
2367 const auto &CB = cast<CallBase>(I);
2368 if (CB.hasFnAttr(Attribute::NoRecurse))
2369 return true;
2370
2371 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
2372 *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
2373 if (!NoRecurseAA.isAssumedNoRecurse())
2374 return false;
2375
2376 // Recursion to the same function
2377 if (CB.getCalledFunction() == getAnchorScope())
2378 return false;
2379
2380 return true;
2381 };
2382
2383 bool UsedAssumedInformation = false;
2384 if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this,
2385 UsedAssumedInformation))
2386 return indicatePessimisticFixpoint();
2387 return ChangeStatus::UNCHANGED;
2388 }
2389
2390 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse){ static llvm::Statistic NumIRFunction_norecurse = {"attributor"
, "NumIRFunction_norecurse", ("Number of " "functions" " marked '"
"norecurse" "'")};; ++(NumIRFunction_norecurse); }
}
2391};
2392
2393/// NoRecurse attribute deduction for a call sites.
2394struct AANoRecurseCallSite final : AANoRecurseImpl {
2395 AANoRecurseCallSite(const IRPosition &IRP, Attributor &A)
2396 : AANoRecurseImpl(IRP, A) {}
2397
2398 /// See AbstractAttribute::initialize(...).
2399 void initialize(Attributor &A) override {
2400 AANoRecurseImpl::initialize(A);
2401 Function *F = getAssociatedFunction();
2402 if (!F || F->isDeclaration())
2403 indicatePessimisticFixpoint();
2404 }
2405
2406 /// See AbstractAttribute::updateImpl(...).
2407 ChangeStatus updateImpl(Attributor &A) override {
2408 // TODO: Once we have call site specific value information we can provide
2409 // call site specific liveness information and then it makes
2410 // sense to specialize attributes for call sites arguments instead of
2411 // redirecting requests to the callee argument.
2412 Function *F = getAssociatedFunction();
2413 const IRPosition &FnPos = IRPosition::function(*F);
2414 auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos, DepClassTy::REQUIRED);
2415 return clampStateAndIndicateChange(getState(), FnAA.getState());
2416 }
2417
2418 /// See AbstractAttribute::trackStatistics()
2419 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); }
; }
2420};
2421
2422/// -------------------- Undefined-Behavior Attributes ------------------------
2423
2424struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
2425 AAUndefinedBehaviorImpl(const IRPosition &IRP, Attributor &A)
2426 : AAUndefinedBehavior(IRP, A) {}
2427
2428 /// See AbstractAttribute::updateImpl(...).
2429 // through a pointer (i.e. also branches etc.)
2430 ChangeStatus updateImpl(Attributor &A) override {
2431 const size_t UBPrevSize = KnownUBInsts.size();
2432 const size_t NoUBPrevSize = AssumedNoUBInsts.size();
2433
2434 auto InspectMemAccessInstForUB = [&](Instruction &I) {
2435 // Skip instructions that are already saved.
2436 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2437 return true;
2438
2439 // If we reach here, we know we have an instruction
2440 // that accesses memory through a pointer operand,
2441 // for which getPointerOperand() should give it to us.
2442 Value *PtrOp =
2443 const_cast<Value *>(getPointerOperand(&I, /* AllowVolatile */ true));
2444 assert(PtrOp &&((void)0)
2445 "Expected pointer operand of memory accessing instruction")((void)0);
2446
2447 // Either we stopped and the appropriate action was taken,
2448 // or we got back a simplified value to continue.
2449 Optional<Value *> SimplifiedPtrOp = stopOnUndefOrAssumed(A, PtrOp, &I);
2450 if (!SimplifiedPtrOp.hasValue() || !SimplifiedPtrOp.getValue())
2451 return true;
2452 const Value *PtrOpVal = SimplifiedPtrOp.getValue();
2453
2454 // A memory access through a pointer is considered UB
2455 // only if the pointer has constant null value.
2456 // TODO: Expand it to not only check constant values.
2457 if (!isa<ConstantPointerNull>(PtrOpVal)) {
2458 AssumedNoUBInsts.insert(&I);
2459 return true;
2460 }
2461 const Type *PtrTy = PtrOpVal->getType();
2462
2463 // Because we only consider instructions inside functions,
2464 // assume that a parent function exists.
2465 const Function *F = I.getFunction();
2466
2467 // A memory access using constant null pointer is only considered UB
2468 // if null pointer is _not_ defined for the target platform.
2469 if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
2470 AssumedNoUBInsts.insert(&I);
2471 else
2472 KnownUBInsts.insert(&I);
2473 return true;
2474 };
2475
2476 auto InspectBrInstForUB = [&](Instruction &I) {
2477 // A conditional branch instruction is considered UB if it has `undef`
2478 // condition.
2479
2480 // Skip instructions that are already saved.
2481 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2482 return true;
2483
2484 // We know we have a branch instruction.
2485 auto *BrInst = cast<BranchInst>(&I);
2486
2487 // Unconditional branches are never considered UB.
2488 if (BrInst->isUnconditional())
2489 return true;
2490
2491 // Either we stopped and the appropriate action was taken,
2492 // or we got back a simplified value to continue.
2493 Optional<Value *> SimplifiedCond =
2494 stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
2495 if (!SimplifiedCond.hasValue() || !SimplifiedCond.getValue())
2496 return true;
2497 AssumedNoUBInsts.insert(&I);
2498 return true;
2499 };
2500
2501 auto InspectCallSiteForUB = [&](Instruction &I) {
2502 // Check whether a callsite always cause UB or not
2503
2504 // Skip instructions that are already saved.
2505 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2506 return true;
2507
2508 // Check nonnull and noundef argument attribute violation for each
2509 // callsite.
2510 CallBase &CB = cast<CallBase>(I);
2511 Function *Callee = CB.getCalledFunction();
2512 if (!Callee)
2513 return true;
2514 for (unsigned idx = 0; idx < CB.getNumArgOperands(); idx++) {
2515 // If current argument is known to be simplified to null pointer and the
2516 // corresponding argument position is known to have nonnull attribute,
2517 // the argument is poison. Furthermore, if the argument is poison and
2518 // the position is known to have noundef attriubte, this callsite is
2519 // considered UB.
2520 if (idx >= Callee->arg_size())
2521 break;
2522 Value *ArgVal = CB.getArgOperand(idx);
2523 if (!ArgVal)
2524 continue;
2525 // Here, we handle three cases.
2526 // (1) Not having a value means it is dead. (we can replace the value
2527 // with undef)
2528 // (2) Simplified to undef. The argument violate noundef attriubte.
2529 // (3) Simplified to null pointer where known to be nonnull.
2530 // The argument is a poison value and violate noundef attribute.
2531 IRPosition CalleeArgumentIRP = IRPosition::callsite_argument(CB, idx);
2532 auto &NoUndefAA =
2533 A.getAAFor<AANoUndef>(*this, CalleeArgumentIRP, DepClassTy::NONE);
2534 if (!NoUndefAA.isKnownNoUndef())
2535 continue;
2536 bool UsedAssumedInformation = false;
2537 Optional<Value *> SimplifiedVal = A.getAssumedSimplified(
2538 IRPosition::value(*ArgVal), *this, UsedAssumedInformation);
2539 if (UsedAssumedInformation)
2540 continue;
2541 if (SimplifiedVal.hasValue() && !SimplifiedVal.getValue())
2542 return true;
2543 if (!SimplifiedVal.hasValue() ||
2544 isa<UndefValue>(*SimplifiedVal.getValue())) {
2545 KnownUBInsts.insert(&I);
2546 continue;
2547 }
2548 if (!ArgVal->getType()->isPointerTy() ||
2549 !isa<ConstantPointerNull>(*SimplifiedVal.getValue()))
2550 continue;
2551 auto &NonNullAA =
2552 A.getAAFor<AANonNull>(*this, CalleeArgumentIRP, DepClassTy::NONE);
2553 if (NonNullAA.isKnownNonNull())
2554 KnownUBInsts.insert(&I);
2555 }
2556 return true;
2557 };
2558
2559 auto InspectReturnInstForUB =
2560 [&](Value &V, const SmallSetVector<ReturnInst *, 4> RetInsts) {
2561 // Check if a return instruction always cause UB or not
2562 // Note: It is guaranteed that the returned position of the anchor
2563 // scope has noundef attribute when this is called.
2564 // We also ensure the return position is not "assumed dead"
2565 // because the returned value was then potentially simplified to
2566 // `undef` in AAReturnedValues without removing the `noundef`
2567 // attribute yet.
2568
2569 // When the returned position has noundef attriubte, UB occur in the
2570 // following cases.
2571 // (1) Returned value is known to be undef.
2572 // (2) The value is known to be a null pointer and the returned
2573 // position has nonnull attribute (because the returned value is
2574 // poison).
2575 bool FoundUB = false;
2576 if (isa<UndefValue>(V)) {
2577 FoundUB = true;
2578 } else {
2579 if (isa<ConstantPointerNull>(V)) {
2580 auto &NonNullAA = A.getAAFor<AANonNull>(
2581 *this, IRPosition::returned(*getAnchorScope()),
2582 DepClassTy::NONE);
2583 if (NonNullAA.isKnownNonNull())
2584 FoundUB = true;
2585 }
2586 }
2587
2588 if (FoundUB)
2589 for (ReturnInst *RI : RetInsts)
2590 KnownUBInsts.insert(RI);
2591 return true;
2592 };
2593
2594 bool UsedAssumedInformation = false;
2595 A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
2596 {Instruction::Load, Instruction::Store,
2597 Instruction::AtomicCmpXchg,
2598 Instruction::AtomicRMW},
2599 UsedAssumedInformation,
2600 /* CheckBBLivenessOnly */ true);
2601 A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
2602 UsedAssumedInformation,
2603 /* CheckBBLivenessOnly */ true);
2604 A.checkForAllCallLikeInstructions(InspectCallSiteForUB, *this,
2605 UsedAssumedInformation);
2606
2607 // If the returned position of the anchor scope has noundef attriubte, check
2608 // all returned instructions.
2609 if (!getAnchorScope()->getReturnType()->isVoidTy()) {
2610 const IRPosition &ReturnIRP = IRPosition::returned(*getAnchorScope());
2611 if (!A.isAssumedDead(ReturnIRP, this, nullptr, UsedAssumedInformation)) {
2612 auto &RetPosNoUndefAA =
2613 A.getAAFor<AANoUndef>(*this, ReturnIRP, DepClassTy::NONE);
2614 if (RetPosNoUndefAA.isKnownNoUndef())
2615 A.checkForAllReturnedValuesAndReturnInsts(InspectReturnInstForUB,
2616 *this);
2617 }
2618 }
2619
2620 if (NoUBPrevSize != AssumedNoUBInsts.size() ||
2621 UBPrevSize != KnownUBInsts.size())
2622 return ChangeStatus::CHANGED;
2623 return ChangeStatus::UNCHANGED;
2624 }
2625
2626 bool isKnownToCauseUB(Instruction *I) const override {
2627 return KnownUBInsts.count(I);
2628 }
2629
2630 bool isAssumedToCauseUB(Instruction *I) const override {
2631 // In simple words, if an instruction is not in the assumed to _not_
2632 // cause UB, then it is assumed UB (that includes those
2633 // in the KnownUBInsts set). The rest is boilerplate
2634 // is to ensure that it is one of the instructions we test
2635 // for UB.
2636
2637 switch (I->getOpcode()) {
2638 case Instruction::Load:
2639 case Instruction::Store:
2640 case Instruction::AtomicCmpXchg:
2641 case Instruction::AtomicRMW:
2642 return !AssumedNoUBInsts.count(I);
2643 case Instruction::Br: {
2644 auto BrInst = cast<BranchInst>(I);
2645 if (BrInst->isUnconditional())
2646 return false;
2647 return !AssumedNoUBInsts.count(I);
2648 } break;
2649 default:
2650 return false;
2651 }
2652 return false;
2653 }
2654
2655 ChangeStatus manifest(Attributor &A) override {
2656 if (KnownUBInsts.empty())
2657 return ChangeStatus::UNCHANGED;
2658 for (Instruction *I : KnownUBInsts)
2659 A.changeToUnreachableAfterManifest(I);
2660 return ChangeStatus::CHANGED;
2661 }
2662
2663 /// See AbstractAttribute::getAsStr()
2664 const std::string getAsStr() const override {
2665 return getAssumed() ? "undefined-behavior" : "no-ub";
2666 }
2667
2668 /// Note: The correctness of this analysis depends on the fact that the
2669 /// following 2 sets will stop changing after some point.
2670 /// "Change" here means that their size changes.
2671 /// The size of each set is monotonically increasing
2672 /// (we only add items to them) and it is upper bounded by the number of
2673 /// instructions in the processed function (we can never save more
2674 /// elements in either set than this number). Hence, at some point,
2675 /// they will stop increasing.
2676 /// Consequently, at some point, both sets will have stopped
2677 /// changing, effectively making the analysis reach a fixpoint.
2678
2679 /// Note: These 2 sets are disjoint and an instruction can be considered
2680 /// one of 3 things:
2681 /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
2682 /// the KnownUBInsts set.
2683 /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
2684 /// has a reason to assume it).
2685 /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
2686 /// could not find a reason to assume or prove that it can cause UB,
2687 /// hence it assumes it doesn't. We have a set for these instructions
2688 /// so that we don't reprocess them in every update.
2689 /// Note however that instructions in this set may cause UB.
2690
2691protected:
2692 /// A set of all live instructions _known_ to cause UB.
2693 SmallPtrSet<Instruction *, 8> KnownUBInsts;
2694
2695private:
2696 /// A set of all the (live) instructions that are assumed to _not_ cause UB.
2697 SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
2698
2699 // Should be called on updates in which if we're processing an instruction
2700 // \p I that depends on a value \p V, one of the following has to happen:
2701 // - If the value is assumed, then stop.
2702 // - If the value is known but undef, then consider it UB.
2703 // - Otherwise, do specific processing with the simplified value.
2704 // We return None in the first 2 cases to signify that an appropriate
2705 // action was taken and the caller should stop.
2706 // Otherwise, we return the simplified value that the caller should
2707 // use for specific processing.
2708 Optional<Value *> stopOnUndefOrAssumed(Attributor &A, Value *V,
2709 Instruction *I) {
2710 bool UsedAssumedInformation = false;
2711 Optional<Value *> SimplifiedV = A.getAssumedSimplified(
2712 IRPosition::value(*V), *this, UsedAssumedInformation);
2713 if (!UsedAssumedInformation) {
2714 // Don't depend on assumed values.
2715 if (!SimplifiedV.hasValue()) {
2716 // If it is known (which we tested above) but it doesn't have a value,
2717 // then we can assume `undef` and hence the instruction is UB.
2718 KnownUBInsts.insert(I);
2719 return llvm::None;
2720 }
2721 if (!SimplifiedV.getValue())
2722 return nullptr;
2723 V = *SimplifiedV;
2724 }
2725 if (isa<UndefValue>(V)) {
2726 KnownUBInsts.insert(I);
2727 return llvm::None;
2728 }
2729 return V;
2730 }
2731};
2732
2733struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
2734 AAUndefinedBehaviorFunction(const IRPosition &IRP, Attributor &A)
2735 : AAUndefinedBehaviorImpl(IRP, A) {}
2736
2737 /// See AbstractAttribute::trackStatistics()
2738 void trackStatistics() const override {
2739 STATS_DECL(UndefinedBehaviorInstruction, Instruction,static llvm::Statistic NumIRInstruction_UndefinedBehaviorInstruction
= {"attributor", "NumIRInstruction_UndefinedBehaviorInstruction"
, "Number of instructions known to have UB"};;
2740 "Number of instructions known to have UB")static llvm::Statistic NumIRInstruction_UndefinedBehaviorInstruction
= {"attributor", "NumIRInstruction_UndefinedBehaviorInstruction"
, "Number of instructions known to have UB"};;
;
2741 BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction)NumIRInstruction_UndefinedBehaviorInstruction +=
2742 KnownUBInsts.size();
2743 }
2744};
2745
2746/// ------------------------ Will-Return Attributes ----------------------------
2747
2748// Helper function that checks whether a function has any cycle which we don't
2749// know if it is bounded or not.
2750// Loops with maximum trip count are considered bounded, any other cycle not.
2751static bool mayContainUnboundedCycle(Function &F, Attributor &A) {
2752 ScalarEvolution *SE =
2753 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F);
2754 LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F);
2755 // If either SCEV or LoopInfo is not available for the function then we assume
2756 // any cycle to be unbounded cycle.
2757 // We use scc_iterator which uses Tarjan algorithm to find all the maximal
2758 // SCCs.To detect if there's a cycle, we only need to find the maximal ones.
2759 if (!SE || !LI) {
2760 for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI)
2761 if (SCCI.hasCycle())
2762 return true;
2763 return false;
2764 }
2765
2766 // If there's irreducible control, the function may contain non-loop cycles.
2767 if (mayContainIrreducibleControl(F, LI))
2768 return true;
2769
2770 // Any loop that does not have a max trip count is considered unbounded cycle.
2771 for (auto *L : LI->getLoopsInPreorder()) {
2772 if (!SE->getSmallConstantMaxTripCount(L))
2773 return true;
2774 }
2775 return false;
2776}
2777
2778struct AAWillReturnImpl : public AAWillReturn {
2779 AAWillReturnImpl(const IRPosition &IRP, Attributor &A)
2780 : AAWillReturn(IRP, A) {}
2781
2782 /// See AbstractAttribute::initialize(...).
2783 void initialize(Attributor &A) override {
2784 AAWillReturn::initialize(A);
2785
2786 if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ true)) {
2787 indicateOptimisticFixpoint();
2788 return;
2789 }
2790 }
2791
2792 /// Check for `mustprogress` and `readonly` as they imply `willreturn`.
2793 bool isImpliedByMustprogressAndReadonly(Attributor &A, bool KnownOnly) {
2794 // Check for `mustprogress` in the scope and the associated function which
2795 // might be different if this is a call site.
2796 if ((!getAnchorScope() || !getAnchorScope()->mustProgress()) &&
2797 (!getAssociatedFunction() || !getAssociatedFunction()->mustProgress()))
2798 return false;
2799
2800 const auto &MemAA =
2801 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
2802 if (!MemAA.isAssumedReadOnly())
2803 return false;
2804 if (KnownOnly && !MemAA.isKnownReadOnly())
2805 return false;
2806 if (!MemAA.isKnownReadOnly())
2807 A.recordDependence(MemAA, *this, DepClassTy::OPTIONAL);
2808
2809 return true;
2810 }
2811
2812 /// See AbstractAttribute::updateImpl(...).
2813 ChangeStatus updateImpl(Attributor &A) override {
2814 if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
2815 return ChangeStatus::UNCHANGED;
2816
2817 auto CheckForWillReturn = [&](Instruction &I) {
2818 IRPosition IPos = IRPosition::callsite_function(cast<CallBase>(I));
2819 const auto &WillReturnAA =
2820 A.getAAFor<AAWillReturn>(*this, IPos, DepClassTy::REQUIRED);
2821 if (WillReturnAA.isKnownWillReturn())
2822 return true;
2823 if (!WillReturnAA.isAssumedWillReturn())
2824 return false;
2825 const auto &NoRecurseAA =
2826 A.getAAFor<AANoRecurse>(*this, IPos, DepClassTy::REQUIRED);
2827 return NoRecurseAA.isAssumedNoRecurse();
2828 };
2829
2830 bool UsedAssumedInformation = false;
2831 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this,
2832 UsedAssumedInformation))
2833 return indicatePessimisticFixpoint();
2834
2835 return ChangeStatus::UNCHANGED;
2836 }
2837
2838 /// See AbstractAttribute::getAsStr()
2839 const std::string getAsStr() const override {
2840 return getAssumed() ? "willreturn" : "may-noreturn";
2841 }
2842};
2843
2844struct AAWillReturnFunction final : AAWillReturnImpl {
2845 AAWillReturnFunction(const IRPosition &IRP, Attributor &A)
2846 : AAWillReturnImpl(IRP, A) {}
2847
2848 /// See AbstractAttribute::initialize(...).
2849 void initialize(Attributor &A) override {
2850 AAWillReturnImpl::initialize(A);
2851
2852 Function *F = getAnchorScope();
2853 if (!F || F->isDeclaration() || mayContainUnboundedCycle(*F, A))
2854 indicatePessimisticFixpoint();
2855 }
2856
2857 /// See AbstractAttribute::trackStatistics()
2858 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn){ static llvm::Statistic NumIRFunction_willreturn = {"attributor"
, "NumIRFunction_willreturn", ("Number of " "functions" " marked '"
"willreturn" "'")};; ++(NumIRFunction_willreturn); }
}
2859};
2860
2861/// WillReturn attribute deduction for a call sites.
2862struct AAWillReturnCallSite final : AAWillReturnImpl {
2863 AAWillReturnCallSite(const IRPosition &IRP, Attributor &A)
2864 : AAWillReturnImpl(IRP, A) {}
2865
2866 /// See AbstractAttribute::initialize(...).
2867 void initialize(Attributor &A) override {
2868 AAWillReturnImpl::initialize(A);
2869 Function *F = getAssociatedFunction();
2870 if (!F || !A.isFunctionIPOAmendable(*F))
2871 indicatePessimisticFixpoint();
2872 }
2873
2874 /// See AbstractAttribute::updateImpl(...).
2875 ChangeStatus updateImpl(Attributor &A) override {
2876 if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
2877 return ChangeStatus::UNCHANGED;
2878
2879 // TODO: Once we have call site specific value information we can provide
2880 // call site specific liveness information and then it makes
2881 // sense to specialize attributes for call sites arguments instead of
2882 // redirecting requests to the callee argument.
2883 Function *F = getAssociatedFunction();
2884 const IRPosition &FnPos = IRPosition::function(*F);
2885 auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos, DepClassTy::REQUIRED);
2886 return clampStateAndIndicateChange(getState(), FnAA.getState());
2887 }
2888
2889 /// See AbstractAttribute::trackStatistics()
2890 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); }
; }
2891};
2892
2893/// -------------------AAReachability Attribute--------------------------
2894
2895struct AAReachabilityImpl : AAReachability {
2896 AAReachabilityImpl(const IRPosition &IRP, Attributor &A)
2897 : AAReachability(IRP, A) {}
2898
2899 const std::string getAsStr() const override {
2900 // TODO: Return the number of reachable queries.
2901 return "reachable";
2902 }
2903
2904 /// See AbstractAttribute::updateImpl(...).
2905 ChangeStatus updateImpl(Attributor &A) override {
2906 return ChangeStatus::UNCHANGED;
2907 }
2908};
2909
2910struct AAReachabilityFunction final : public AAReachabilityImpl {
2911 AAReachabilityFunction(const IRPosition &IRP, Attributor &A)
2912 : AAReachabilityImpl(IRP, A) {}
2913
2914 /// See AbstractAttribute::trackStatistics()
2915 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable){ static llvm::Statistic NumIRFunction_reachable = {"attributor"
, "NumIRFunction_reachable", ("Number of " "functions" " marked '"
"reachable" "'")};; ++(NumIRFunction_reachable); }
; }
2916};
2917
2918/// ------------------------ NoAlias Argument Attribute ------------------------
2919
2920struct AANoAliasImpl : AANoAlias {
2921 AANoAliasImpl(const IRPosition &IRP, Attributor &A) : AANoAlias(IRP, A) {
2922 assert(getAssociatedType()->isPointerTy() &&((void)0)
2923 "Noalias is a pointer attribute")((void)0);
2924 }
2925
2926 const std::string getAsStr() const override {
2927 return getAssumed() ? "noalias" : "may-alias";
2928 }
2929};
2930
2931/// NoAlias attribute for a floating value.
2932struct AANoAliasFloating final : AANoAliasImpl {
2933 AANoAliasFloating(const IRPosition &IRP, Attributor &A)
2934 : AANoAliasImpl(IRP, A) {}
2935
2936 /// See AbstractAttribute::initialize(...).
2937 void initialize(Attributor &A) override {
2938 AANoAliasImpl::initialize(A);
2939 Value *Val = &getAssociatedValue();
2940 do {
2941 CastInst *CI = dyn_cast<CastInst>(Val);
2942 if (!CI)
2943 break;
2944 Value *Base = CI->getOperand(0);
2945 if (!Base->hasOneUse())
2946 break;
2947 Val = Base;
2948 } while (true);
2949
2950 if (!Val->getType()->isPointerTy()) {
2951 indicatePessimisticFixpoint();
2952 return;
2953 }
2954
2955 if (isa<AllocaInst>(Val))
2956 indicateOptimisticFixpoint();
2957 else if (isa<ConstantPointerNull>(Val) &&
2958 !NullPointerIsDefined(getAnchorScope(),
2959 Val->getType()->getPointerAddressSpace()))
2960 indicateOptimisticFixpoint();
2961 else if (Val != &getAssociatedValue()) {
2962 const auto &ValNoAliasAA = A.getAAFor<AANoAlias>(
2963 *this, IRPosition::value(*Val), DepClassTy::OPTIONAL);
2964 if (ValNoAliasAA.isKnownNoAlias())
2965 indicateOptimisticFixpoint();
2966 }
2967 }
2968
2969 /// See AbstractAttribute::updateImpl(...).
2970 ChangeStatus updateImpl(Attributor &A) override {
2971 // TODO: Implement this.
2972 return indicatePessimisticFixpoint();
2973 }
2974
2975 /// See AbstractAttribute::trackStatistics()
2976 void trackStatistics() const override {
2977 STATS_DECLTRACK_FLOATING_ATTR(noalias){ static llvm::Statistic NumIRFloating_noalias = {"attributor"
, "NumIRFloating_noalias", ("Number of floating values known to be '"
"noalias" "'")};; ++(NumIRFloating_noalias); }
2978 }
2979};
2980
2981/// NoAlias attribute for an argument.
2982struct AANoAliasArgument final
2983 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
2984 using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
2985 AANoAliasArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
2986
2987 /// See AbstractAttribute::initialize(...).
2988 void initialize(Attributor &A) override {
2989 Base::initialize(A);
2990 // See callsite argument attribute and callee argument attribute.
2991 if (hasAttr({Attribute::ByVal}))
2992 indicateOptimisticFixpoint();
2993 }
2994
2995 /// See AbstractAttribute::update(...).
2996 ChangeStatus updateImpl(Attributor &A) override {
2997 // We have to make sure no-alias on the argument does not break
2998 // synchronization when this is a callback argument, see also [1] below.
2999 // If synchronization cannot be affected, we delegate to the base updateImpl
3000 // function, otherwise we give up for now.
3001
3002 // If the function is no-sync, no-alias cannot break synchronization.
3003 const auto &NoSyncAA =
3004 A.getAAFor<AANoSync>(*this, IRPosition::function_scope(getIRPosition()),
3005 DepClassTy::OPTIONAL);
3006 if (NoSyncAA.isAssumedNoSync())
3007 return Base::updateImpl(A);
3008
3009 // If the argument is read-only, no-alias cannot break synchronization.
3010 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
3011 *this, getIRPosition(), DepClassTy::OPTIONAL);
3012 if (MemBehaviorAA.isAssumedReadOnly())
3013 return Base::updateImpl(A);
3014
3015 // If the argument is never passed through callbacks, no-alias cannot break
3016 // synchronization.
3017 bool AllCallSitesKnown;
3018 if (A.checkForAllCallSites(
3019 [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
3020 true, AllCallSitesKnown))
3021 return Base::updateImpl(A);
3022
3023 // TODO: add no-alias but make sure it doesn't break synchronization by
3024 // introducing fake uses. See:
3025 // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
3026 // International Workshop on OpenMP 2018,
3027 // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
3028
3029 return indicatePessimisticFixpoint();
3030 }
3031
3032 /// See AbstractAttribute::trackStatistics()
3033 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias){ static llvm::Statistic NumIRArguments_noalias = {"attributor"
, "NumIRArguments_noalias", ("Number of " "arguments" " marked '"
"noalias" "'")};; ++(NumIRArguments_noalias); }
}
3034};
3035
3036struct AANoAliasCallSiteArgument final : AANoAliasImpl {
3037 AANoAliasCallSiteArgument(const IRPosition &IRP, Attributor &A)
3038 : AANoAliasImpl(IRP, A) {}
3039
3040 /// See AbstractAttribute::initialize(...).
3041 void initialize(Attributor &A) override {
3042 // See callsite argument attribute and callee argument attribute.
3043 const auto &CB = cast<CallBase>(getAnchorValue());
3044 if (CB.paramHasAttr(getCallSiteArgNo(), Attribute::NoAlias))
3045 indicateOptimisticFixpoint();
3046 Value &Val = getAssociatedValue();
3047 if (isa<ConstantPointerNull>(Val) &&
3048 !NullPointerIsDefined(getAnchorScope(),
3049 Val.getType()->getPointerAddressSpace()))
3050 indicateOptimisticFixpoint();
3051 }
3052
3053 /// Determine if the underlying value may alias with the call site argument
3054 /// \p OtherArgNo of \p ICS (= the underlying call site).
3055 bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
3056 const AAMemoryBehavior &MemBehaviorAA,
3057 const CallBase &CB, unsigned OtherArgNo) {
3058 // We do not need to worry about aliasing with the underlying IRP.
3059 if (this->getCalleeArgNo() == (int)OtherArgNo)
3060 return false;
3061
3062 // If it is not a pointer or pointer vector we do not alias.
3063 const Value *ArgOp = CB.getArgOperand(OtherArgNo);
3064 if (!ArgOp->getType()->isPtrOrPtrVectorTy())
3065 return false;
3066
3067 auto &CBArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
3068 *this, IRPosition::callsite_argument(CB, OtherArgNo), DepClassTy::NONE);
3069
3070 // If the argument is readnone, there is no read-write aliasing.
3071 if (CBArgMemBehaviorAA.isAssumedReadNone()) {
3072 A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
3073 return false;
3074 }
3075
3076 // If the argument is readonly and the underlying value is readonly, there
3077 // is no read-write aliasing.
3078 bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
3079 if (CBArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
3080 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3081 A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
3082 return false;
3083 }
3084
3085 // We have to utilize actual alias analysis queries so we need the object.
3086 if (!AAR)
3087 AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
3088
3089 // Try to rule it out at the call site.
3090 bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
3091 LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "do { } while (false)
3092 "callsite arguments: "do { } while (false)
3093 << getAssociatedValue() << " " << *ArgOp << " => "do { } while (false)
3094 << (IsAliasing ? "" : "no-") << "alias \n")do { } while (false);
3095
3096 return IsAliasing;
3097 }
3098
3099 bool
3100 isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
3101 const AAMemoryBehavior &MemBehaviorAA,
3102 const AANoAlias &NoAliasAA) {
3103 // We can deduce "noalias" if the following conditions hold.
3104 // (i) Associated value is assumed to be noalias in the definition.
3105 // (ii) Associated value is assumed to be no-capture in all the uses
3106 // possibly executed before this callsite.
3107 // (iii) There is no other pointer argument which could alias with the
3108 // value.
3109
3110 bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
3111 if (!AssociatedValueIsNoAliasAtDef) {
3112 LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()do { } while (false)
3113 << " is not no-alias at the definition\n")do { } while (false);
3114 return false;
3115 }
3116
3117 A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL);
3118
3119 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
3120 const Function *ScopeFn = VIRP.getAnchorScope();
3121 auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, VIRP, DepClassTy::NONE);
3122 // Check whether the value is captured in the scope using AANoCapture.
3123 // Look at CFG and check only uses possibly executed before this
3124 // callsite.
3125 auto UsePred = [&](const Use &U, bool &Follow) -> bool {
3126 Instruction *UserI = cast<Instruction>(U.getUser());
3127
3128 // If UserI is the curr instruction and there is a single potential use of
3129 // the value in UserI we allow the use.
3130 // TODO: We should inspect the operands and allow those that cannot alias
3131 // with the value.
3132 if (UserI == getCtxI() && UserI->getNumOperands() == 1)
3133 return true;
3134
3135 if (ScopeFn) {
3136 const auto &ReachabilityAA = A.getAAFor<AAReachability>(
3137 *this, IRPosition::function(*ScopeFn), DepClassTy::OPTIONAL);
3138
3139 if (!ReachabilityAA.isAssumedReachable(A, *UserI, *getCtxI()))
3140 return true;
3141
3142 if (auto *CB = dyn_cast<CallBase>(UserI)) {
3143 if (CB->isArgOperand(&U)) {
3144
3145 unsigned ArgNo = CB->getArgOperandNo(&U);
3146
3147 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
3148 *this, IRPosition::callsite_argument(*CB, ArgNo),
3149 DepClassTy::OPTIONAL);
3150
3151 if (NoCaptureAA.isAssumedNoCapture())
3152 return true;
3153 }
3154 }
3155 }
3156
3157 // For cases which can potentially have more users
3158 if (isa<GetElementPtrInst>(U) || isa<BitCastInst>(U) || isa<PHINode>(U) ||
3159 isa<SelectInst>(U)) {
3160 Follow = true;
3161 return true;
3162 }
3163
3164 LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n")do { } while (false);
3165 return false;
3166 };
3167
3168 if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
3169 if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) {
3170 LLVM_DEBUG(do { } while (false)
3171 dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()do { } while (false)
3172 << " cannot be noalias as it is potentially captured\n")do { } while (false);
3173 return false;
3174 }
3175 }
3176 A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
3177
3178 // Check there is no other pointer argument which could alias with the
3179 // value passed at this call site.
3180 // TODO: AbstractCallSite
3181 const auto &CB = cast<CallBase>(getAnchorValue());
3182 for (unsigned OtherArgNo = 0; OtherArgNo < CB.getNumArgOperands();
3183 OtherArgNo++)
3184 if (mayAliasWithArgument(A, AAR, MemBehaviorAA, CB, OtherArgNo))
3185 return false;
3186
3187 return true;
3188 }
3189
3190 /// See AbstractAttribute::updateImpl(...).
3191 ChangeStatus updateImpl(Attributor &A) override {
3192 // If the argument is readnone we are done as there are no accesses via the
3193 // argument.
3194 auto &MemBehaviorAA =
3195 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
3196 if (MemBehaviorAA.isAssumedReadNone()) {
3197 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3198 return ChangeStatus::UNCHANGED;
3199 }
3200
3201 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
3202 const auto &NoAliasAA =
3203 A.getAAFor<AANoAlias>(*this, VIRP, DepClassTy::NONE);
3204
3205 AAResults *AAR = nullptr;
3206 if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
3207 NoAliasAA)) {
3208 LLVM_DEBUG(do { } while (false)
3209 dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n")do { } while (false);
3210 return ChangeStatus::UNCHANGED;
3211 }
3212
3213 return indicatePessimisticFixpoint();
3214 }
3215
3216 /// See AbstractAttribute::trackStatistics()
3217 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias){ static llvm::Statistic NumIRCSArguments_noalias = {"attributor"
, "NumIRCSArguments_noalias", ("Number of " "call site arguments"
" marked '" "noalias" "'")};; ++(NumIRCSArguments_noalias); }
}
3218};
3219
3220/// NoAlias attribute for function return value.
3221struct AANoAliasReturned final : AANoAliasImpl {
3222 AANoAliasReturned(const IRPosition &IRP, Attributor &A)
3223 : AANoAliasImpl(IRP, A) {}
3224
3225 /// See AbstractAttribute::initialize(...).
3226 void initialize(Attributor &A) override {
3227 AANoAliasImpl::initialize(A);
3228 Function *F = getAssociatedFunction();
3229 if (!F || F->isDeclaration())
3230 indicatePessimisticFixpoint();
3231 }
3232
3233 /// See AbstractAttribute::updateImpl(...).
3234 virtual ChangeStatus updateImpl(Attributor &A) override {
3235
3236 auto CheckReturnValue = [&](Value &RV) -> bool {
3237 if (Constant *C = dyn_cast<Constant>(&RV))
3238 if (C->isNullValue() || isa<UndefValue>(C))
3239 return true;
3240
3241 /// For now, we can only deduce noalias if we have call sites.
3242 /// FIXME: add more support.
3243 if (!isa<CallBase>(&RV))
3244 return false;
3245
3246 const IRPosition &RVPos = IRPosition::value(RV);
3247 const auto &NoAliasAA =
3248 A.getAAFor<AANoAlias>(*this, RVPos, DepClassTy::REQUIRED);
3249 if (!NoAliasAA.isAssumedNoAlias())
3250 return false;
3251
3252 const auto &NoCaptureAA =
3253 A.getAAFor<AANoCapture>(*this, RVPos, DepClassTy::REQUIRED);
3254 return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
3255 };
3256
3257 if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
3258 return indicatePessimisticFixpoint();
3259
3260 return ChangeStatus::UNCHANGED;
3261 }
3262
3263 /// See AbstractAttribute::trackStatistics()
3264 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias){ static llvm::Statistic NumIRFunctionReturn_noalias = {"attributor"
, "NumIRFunctionReturn_noalias", ("Number of " "function returns"
" marked '" "noalias" "'")};; ++(NumIRFunctionReturn_noalias
); }
}
3265};
3266
3267/// NoAlias attribute deduction for a call site return value.
3268struct AANoAliasCallSiteReturned final : AANoAliasImpl {
3269 AANoAliasCallSiteReturned(const IRPosition &IRP, Attributor &A)
3270 : AANoAliasImpl(IRP, A) {}
3271
3272 /// See AbstractAttribute::initialize(...).
3273 void initialize(Attributor &A) override {
3274 AANoAliasImpl::initialize(A);
3275 Function *F = getAssociatedFunction();
3276 if (!F || F->isDeclaration())
3277 indicatePessimisticFixpoint();
3278 }
3279
3280 /// See AbstractAttribute::updateImpl(...).
3281 ChangeStatus updateImpl(Attributor &A) override {
3282 // TODO: Once we have call site specific value information we can provide
3283 // call site specific liveness information and then it makes
3284 // sense to specialize attributes for call sites arguments instead of
3285 // redirecting requests to the callee argument.
3286 Function *F = getAssociatedFunction();
3287 const IRPosition &FnPos = IRPosition::returned(*F);
3288 auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos, DepClassTy::REQUIRED);
3289 return clampStateAndIndicateChange(getState(), FnAA.getState());
3290 }
3291
3292 /// See AbstractAttribute::trackStatistics()
3293 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias){ static llvm::Statistic NumIRCSReturn_noalias = {"attributor"
, "NumIRCSReturn_noalias", ("Number of " "call site returns" " marked '"
"noalias" "'")};; ++(NumIRCSReturn_noalias); }
; }
3294};
3295
3296/// -------------------AAIsDead Function Attribute-----------------------
3297
3298struct AAIsDeadValueImpl : public AAIsDead {
3299 AAIsDeadValueImpl(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
3300
3301 /// See AAIsDead::isAssumedDead().
3302 bool isAssumedDead() const override { return isAssumed(IS_DEAD); }
3303
3304 /// See AAIsDead::isKnownDead().
3305 bool isKnownDead() const override { return isKnown(IS_DEAD); }
3306
3307 /// See AAIsDead::isAssumedDead(BasicBlock *).
3308 bool isAssumedDead(const BasicBlock *BB) const override { return false; }
3309
3310 /// See AAIsDead::isKnownDead(BasicBlock *).
3311 bool isKnownDead(const BasicBlock *BB) const override { return false; }
3312
3313 /// See AAIsDead::isAssumedDead(Instruction *I).
3314 bool isAssumedDead(const Instruction *I) const override {
3315 return I == getCtxI() && isAssumedDead();
3316 }
3317
3318 /// See AAIsDead::isKnownDead(Instruction *I).
3319 bool isKnownDead(const Instruction *I) const override {
3320 return isAssumedDead(I) && isKnownDead();
3321 }
3322
3323 /// See AbstractAttribute::getAsStr().
3324 const std::string getAsStr() const override {
3325 return isAssumedDead() ? "assumed-dead" : "assumed-live";
3326 }
3327
3328 /// Check if all uses are assumed dead.
3329 bool areAllUsesAssumedDead(Attributor &A, Value &V) {
3330 // Callers might not check the type, void has no uses.
3331 if (V.getType()->isVoidTy())
3332 return true;
3333
3334 // If we replace a value with a constant there are no uses left afterwards.
3335 if (!isa<Constant>(V)) {
3336 bool UsedAssumedInformation = false;
3337 Optional<Constant *> C =
3338 A.getAssumedConstant(V, *this, UsedAssumedInformation);
3339 if (!C.hasValue() || *C)
3340 return true;
3341 }
3342
3343 auto UsePred = [&](const Use &U, bool &Follow) { return false; };
3344 // Explicitly set the dependence class to required because we want a long
3345 // chain of N dependent instructions to be considered live as soon as one is
3346 // without going through N update cycles. This is not required for
3347 // correctness.
3348 return A.checkForAllUses(UsePred, *this, V, /* CheckBBLivenessOnly */ false,
3349 DepClassTy::REQUIRED);
3350 }
3351
3352 /// Determine if \p I is assumed to be side-effect free.
3353 bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
3354 if (!I || wouldInstructionBeTriviallyDead(I))
3355 return true;
3356
3357 auto *CB = dyn_cast<CallBase>(I);
3358 if (!CB || isa<IntrinsicInst>(CB))
3359 return false;
3360
3361 const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
3362 const auto &NoUnwindAA =
3363 A.getAndUpdateAAFor<AANoUnwind>(*this, CallIRP, DepClassTy::NONE);
3364 if (!NoUnwindAA.isAssumedNoUnwind())
3365 return false;
3366 if (!NoUnwindAA.isKnownNoUnwind())
3367 A.recordDependence(NoUnwindAA, *this, DepClassTy::OPTIONAL);
3368
3369 const auto &MemBehaviorAA =
3370 A.getAndUpdateAAFor<AAMemoryBehavior>(*this, CallIRP, DepClassTy::NONE);
3371 if (MemBehaviorAA.isAssumedReadOnly()) {
3372 if (!MemBehaviorAA.isKnownReadOnly())
3373 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
3374 return true;
3375 }
3376 return false;
3377 }
3378};
3379
3380struct AAIsDeadFloating : public AAIsDeadValueImpl {
3381 AAIsDeadFloating(const IRPosition &IRP, Attributor &A)
3382 : AAIsDeadValueImpl(IRP, A) {}
3383
3384 /// See AbstractAttribute::initialize(...).
3385 void initialize(Attributor &A) override {
3386 if (isa<UndefValue>(getAssociatedValue())) {
3387 indicatePessimisticFixpoint();
3388 return;
3389 }
3390
3391 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3392 if (!isAssumedSideEffectFree(A, I)) {
3393 if (!isa_and_nonnull<StoreInst>(I))
3394 indicatePessimisticFixpoint();
3395 else
3396 removeAssumedBits(HAS_NO_EFFECT);
3397 }
3398 }
3399
3400 bool isDeadStore(Attributor &A, StoreInst &SI) {
3401 bool UsedAssumedInformation = false;
3402 SmallSetVector<Value *, 4> PotentialCopies;
3403 if (!AA::getPotentialCopiesOfStoredValue(A, SI, PotentialCopies, *this,
3404 UsedAssumedInformation))
3405 return false;
3406 return llvm::all_of(PotentialCopies, [&](Value *V) {
3407 return A.isAssumedDead(IRPosition::value(*V), this, nullptr,
3408 UsedAssumedInformation);
3409 });
3410 }
3411
3412 /// See AbstractAttribute::updateImpl(...).
3413 ChangeStatus updateImpl(Attributor &A) override {
3414 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3415 if (auto *SI = dyn_cast_or_null<StoreInst>(I)) {
3416 if (!isDeadStore(A, *SI))
3417 return indicatePessimisticFixpoint();
3418 } else {
3419 if (!isAssumedSideEffectFree(A, I))
3420 return indicatePessimisticFixpoint();
3421 if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3422 return indicatePessimisticFixpoint();
3423 }
3424 return ChangeStatus::UNCHANGED;
3425 }
3426
3427 /// See AbstractAttribute::manifest(...).
3428 ChangeStatus manifest(Attributor &A) override {
3429 Value &V = getAssociatedValue();
3430 if (auto *I = dyn_cast<Instruction>(&V)) {
3431 // If we get here we basically know the users are all dead. We check if
3432 // isAssumedSideEffectFree returns true here again because it might not be
3433 // the case and only the users are dead but the instruction (=call) is
3434 // still needed.
3435 if (isa<StoreInst>(I) ||
3436 (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I))) {
3437 A.deleteAfterManifest(*I);
3438 return ChangeStatus::CHANGED;
3439 }
3440 }
3441 if (V.use_empty())
3442 return ChangeStatus::UNCHANGED;
3443
3444 bool UsedAssumedInformation = false;
3445 Optional<Constant *> C =
3446 A.getAssumedConstant(V, *this, UsedAssumedInformation);
3447 if (C.hasValue() && C.getValue())
3448 return ChangeStatus::UNCHANGED;
3449
3450 // Replace the value with undef as it is dead but keep droppable uses around
3451 // as they provide information we don't want to give up on just yet.
3452 UndefValue &UV = *UndefValue::get(V.getType());
3453 bool AnyChange =
3454 A.changeValueAfterManifest(V, UV, /* ChangeDropppable */ false);
3455 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3456 }
3457
3458 /// See AbstractAttribute::trackStatistics()
3459 void trackStatistics() const override {
3460 STATS_DECLTRACK_FLOATING_ATTR(IsDead){ static llvm::Statistic NumIRFloating_IsDead = {"attributor"
, "NumIRFloating_IsDead", ("Number of floating values known to be '"
"IsDead" "'")};; ++(NumIRFloating_IsDead); }
3461 }
3462};
3463
3464struct AAIsDeadArgument : public AAIsDeadFloating {
3465 AAIsDeadArgument(const IRPosition &IRP, Attributor &A)
3466 : AAIsDeadFloating(IRP, A) {}
3467
3468 /// See AbstractAttribute::initialize(...).
3469 void initialize(Attributor &A) override {
3470 if (!A.isFunctionIPOAmendable(*getAnchorScope()))
3471 indicatePessimisticFixpoint();
3472 }
3473
3474 /// See AbstractAttribute::manifest(...).
3475 ChangeStatus manifest(Attributor &A) override {
3476 ChangeStatus Changed = AAIsDeadFloating::manifest(A);
3477 Argument &Arg = *getAssociatedArgument();
3478 if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
3479 if (A.registerFunctionSignatureRewrite(
3480 Arg, /* ReplacementTypes */ {},
3481 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
3482 Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) {
3483 Arg.dropDroppableUses();
3484 return ChangeStatus::CHANGED;
3485 }
3486 return Changed;
3487 }
3488
3489 /// See AbstractAttribute::trackStatistics()
3490 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead){ static llvm::Statistic NumIRArguments_IsDead = {"attributor"
, "NumIRArguments_IsDead", ("Number of " "arguments" " marked '"
"IsDead" "'")};; ++(NumIRArguments_IsDead); }
}
3491};
3492
3493struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
3494 AAIsDeadCallSiteArgument(const IRPosition &IRP, Attributor &A)
3495 : AAIsDeadValueImpl(IRP, A) {}
3496
3497 /// See AbstractAttribute::initialize(...).
3498 void initialize(Attributor &A) override {
3499 if (isa<UndefValue>(getAssociatedValue()))
3500 indicatePessimisticFixpoint();
3501 }
3502
3503 /// See AbstractAttribute::updateImpl(...).
3504 ChangeStatus updateImpl(Attributor &A) override {
3505 // TODO: Once we have call site specific value information we can provide
3506 // call site specific liveness information and then it makes
3507 // sense to specialize attributes for call sites arguments instead of
3508 // redirecting requests to the callee argument.
3509 Argument *Arg = getAssociatedArgument();
3510 if (!Arg)
3511 return indicatePessimisticFixpoint();
3512 const IRPosition &ArgPos = IRPosition::argument(*Arg);
3513 auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos, DepClassTy::REQUIRED);
3514 return clampStateAndIndicateChange(getState(), ArgAA.getState());
3515 }
3516
3517 /// See AbstractAttribute::manifest(...).
3518 ChangeStatus manifest(Attributor &A) override {
3519 CallBase &CB = cast<CallBase>(getAnchorValue());
3520 Use &U = CB.getArgOperandUse(getCallSiteArgNo());
3521 assert(!isa<UndefValue>(U.get()) &&((void)0)
3522 "Expected undef values to be filtered out!")((void)0);
3523 UndefValue &UV = *UndefValue::get(U->getType());
3524 if (A.changeUseAfterManifest(U, UV))
3525 return ChangeStatus::CHANGED;
3526 return ChangeStatus::UNCHANGED;
3527 }
3528
3529 /// See AbstractAttribute::trackStatistics()
3530 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); }
}
3531};
3532
3533struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
3534 AAIsDeadCallSiteReturned(const IRPosition &IRP, Attributor &A)
3535 : AAIsDeadFloating(IRP, A), IsAssumedSideEffectFree(true) {}
3536
3537 /// See AAIsDead::isAssumedDead().
3538 bool isAssumedDead() const override {
3539 return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
3540 }
3541
3542 /// See AbstractAttribute::initialize(...).
3543 void initialize(Attributor &A) override {
3544 if (isa<UndefValue>(getAssociatedValue())) {
3545 indicatePessimisticFixpoint();
3546 return;
3547 }
3548
3549 // We track this separately as a secondary state.
3550 IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
3551 }
3552
3553 /// See AbstractAttribute::updateImpl(...).
3554 ChangeStatus updateImpl(Attributor &A) override {
3555 ChangeStatus Changed = ChangeStatus::UNCHANGED;
3556 if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
3557 IsAssumedSideEffectFree = false;
3558 Changed = ChangeStatus::CHANGED;
3559 }
3560 if (!areAllUsesAssumedDead(A, getAssociatedValue()))
3561 return indicatePessimisticFixpoint();
3562 return Changed;
3563 }
3564
3565 /// See AbstractAttribute::trackStatistics()
3566 void trackStatistics() const override {
3567 if (IsAssumedSideEffectFree)
3568 STATS_DECLTRACK_CSRET_ATTR(IsDead){ static llvm::Statistic NumIRCSReturn_IsDead = {"attributor"
, "NumIRCSReturn_IsDead", ("Number of " "call site returns" " marked '"
"IsDead" "'")};; ++(NumIRCSReturn_IsDead); }
3569 else
3570 STATS_DECLTRACK_CSRET_ATTR(UnusedResult){ static llvm::Statistic NumIRCSReturn_UnusedResult = {"attributor"
, "NumIRCSReturn_UnusedResult", ("Number of " "call site returns"
" marked '" "UnusedResult" "'")};; ++(NumIRCSReturn_UnusedResult
); }
3571 }
3572
3573 /// See AbstractAttribute::getAsStr().
3574 const std::string getAsStr() const override {
3575 return isAssumedDead()
3576 ? "assumed-dead"
3577 : (getAssumed() ? "assumed-dead-users" : "assumed-live");
3578 }
3579
3580private:
3581 bool IsAssumedSideEffectFree;
3582};
3583
3584struct AAIsDeadReturned : public AAIsDeadValueImpl {
3585 AAIsDeadReturned(const IRPosition &IRP, Attributor &A)
3586 : AAIsDeadValueImpl(IRP, A) {}
3587
3588 /// See AbstractAttribute::updateImpl(...).
3589 ChangeStatus updateImpl(Attributor &A) override {
3590
3591 bool UsedAssumedInformation = false;
3592 A.checkForAllInstructions([](Instruction &) { return true; }, *this,
3593 {Instruction::Ret}, UsedAssumedInformation);
3594
3595 auto PredForCallSite = [&](AbstractCallSite ACS) {
3596 if (ACS.isCallbackCall() || !ACS.getInstruction())
3597 return false;
3598 return areAllUsesAssumedDead(A, *ACS.getInstruction());
3599 };
3600
3601 bool AllCallSitesKnown;
3602 if (!A.checkForAllCallSites(PredForCallSite, *this, true,
3603 AllCallSitesKnown))
3604 return indicatePessimisticFixpoint();
3605
3606 return ChangeStatus::UNCHANGED;
3607 }
3608
3609 /// See AbstractAttribute::manifest(...).
3610 ChangeStatus manifest(Attributor &A) override {
3611 // TODO: Rewrite the signature to return void?
3612 bool AnyChange = false;
3613 UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
3614 auto RetInstPred = [&](Instruction &I) {
3615 ReturnInst &RI = cast<ReturnInst>(I);
3616 if (!isa<UndefValue>(RI.getReturnValue()))
3617 AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
3618 return true;
3619 };
3620 bool UsedAssumedInformation = false;
3621 A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret},
3622 UsedAssumedInformation);
3623 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3624 }
3625
3626 /// See AbstractAttribute::trackStatistics()
3627 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);
}
}
3628};
3629
3630struct AAIsDeadFunction : public AAIsDead {
3631 AAIsDeadFunction(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
3632
3633 /// See AbstractAttribute::initialize(...).
3634 void initialize(Attributor &A) override {
3635 const Function *F = getAnchorScope();
3636 if (F && !F->isDeclaration()) {
3637 // We only want to compute liveness once. If the function is not part of
3638 // the SCC, skip it.
3639 if (A.isRunOn(*const_cast<Function *>(F))) {
3640 ToBeExploredFrom.insert(&F->getEntryBlock().front());
3641 assumeLive(A, F->getEntryBlock());
3642 } else {
3643 indicatePessimisticFixpoint();
3644 }
3645 }
3646 }
3647
3648 /// See AbstractAttribute::getAsStr().
3649 const std::string getAsStr() const override {
3650 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
3651 std::to_string(getAnchorScope()->size()) + "][#TBEP " +
3652 std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
3653 std::to_string(KnownDeadEnds.size()) + "]";
3654 }
3655
3656 /// See AbstractAttribute::manifest(...).
3657 ChangeStatus manifest(Attributor &A) override {
3658 assert(getState().isValidState() &&((void)0)
3659 "Attempted to manifest an invalid state!")((void)0);
3660
3661 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
3662 Function &F = *getAnchorScope();
3663
3664 if (AssumedLiveBlocks.empty()) {
3665 A.deleteAfterManifest(F);
3666 return ChangeStatus::CHANGED;
3667 }
3668
3669 // Flag to determine if we can change an invoke to a call assuming the
3670 // callee is nounwind. This is not possible if the personality of the
3671 // function allows to catch asynchronous exceptions.
3672 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
3673
3674 KnownDeadEnds.set_union(ToBeExploredFrom);
3675 for (const Instruction *DeadEndI : KnownDeadEnds) {
3676 auto *CB = dyn_cast<CallBase>(DeadEndI);
3677 if (!CB)
3678 continue;
3679 const auto &NoReturnAA = A.getAndUpdateAAFor<AANoReturn>(
3680 *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
3681 bool MayReturn = !NoReturnAA.isAssumedNoReturn();
3682 if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
3683 continue;
3684
3685 if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
3686 A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
3687 else
3688 A.changeToUnreachableAfterManifest(
3689 const_cast<Instruction *>(DeadEndI->getNextNode()));
3690 HasChanged = ChangeStatus::CHANGED;
3691 }
3692
3693 STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.")static llvm::Statistic NumIRBasicBlock_AAIsDead = {"attributor"
, "NumIRBasicBlock_AAIsDead", "Number of dead basic blocks deleted."
};;
;
3694 for (BasicBlock &BB : F)
3695 if (!AssumedLiveBlocks.count(&BB)) {
3696 A.deleteAfterManifest(BB);
3697 ++BUILD_STAT_NAME(AAIsDead, BasicBlock)NumIRBasicBlock_AAIsDead;
3698 }
3699
3700 return HasChanged;
3701 }
3702
3703 /// See AbstractAttribute::updateImpl(...).
3704 ChangeStatus updateImpl(Attributor &A) override;
3705
3706 bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const override {
3707 return !AssumedLiveEdges.count(std::make_pair(From, To));
3708 }
3709
3710 /// See AbstractAttribute::trackStatistics()
3711 void trackStatistics() const override {}
3712
3713 /// Returns true if the function is assumed dead.
3714 bool isAssumedDead() const override { return false; }
3715
3716 /// See AAIsDead::isKnownDead().
3717 bool isKnownDead() const override { return false; }
3718
3719 /// See AAIsDead::isAssumedDead(BasicBlock *).
3720 bool isAssumedDead(const BasicBlock *BB) const override {
3721 assert(BB->getParent() == getAnchorScope() &&((void)0)
3722 "BB must be in the same anchor scope function.")((void)0);
3723
3724 if (!getAssumed())
3725 return false;
3726 return !AssumedLiveBlocks.count(BB);
3727 }
3728
3729 /// See AAIsDead::isKnownDead(BasicBlock *).
3730 bool isKnownDead(const BasicBlock *BB) const override {
3731 return getKnown() && isAssumedDead(BB);
3732 }
3733
3734 /// See AAIsDead::isAssumed(Instruction *I).
3735 bool isAssumedDead(const Instruction *I) const override {
3736 assert(I->getParent()->getParent() == getAnchorScope() &&((void)0)
3737 "Instruction must be in the same anchor scope function.")((void)0);
3738
3739 if (!getAssumed())
3740 return false;
3741
3742 // If it is not in AssumedLiveBlocks then it for sure dead.
3743 // Otherwise, it can still be after noreturn call in a live block.
3744 if (!AssumedLiveBlocks.count(I->getParent()))
3745 return true;
3746
3747 // If it is not after a liveness barrier it is live.
3748 const Instruction *PrevI = I->getPrevNode();
3749 while (PrevI) {
3750 if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
3751 return true;
3752 PrevI = PrevI->getPrevNode();
3753 }
3754 return false;
3755 }
3756
3757 /// See AAIsDead::isKnownDead(Instruction *I).
3758 bool isKnownDead(const Instruction *I) const override {
3759 return getKnown() && isAssumedDead(I);
3760 }
3761
3762 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
3763 /// that internal function called from \p BB should now be looked at.
3764 bool assumeLive(Attributor &A, const BasicBlock &BB) {
3765 if (!AssumedLiveBlocks.insert(&BB).second)
3766 return false;
3767
3768 // We assume that all of BB is (probably) live now and if there are calls to
3769 // internal functions we will assume that those are now live as well. This
3770 // is a performance optimization for blocks with calls to a lot of internal
3771 // functions. It can however cause dead functions to be treated as live.
3772 for (const Instruction &I : BB)
3773 if (const auto *CB = dyn_cast<CallBase>(&I))
3774 if (const Function *F = CB->getCalledFunction())
3775 if (F->hasLocalLinkage())
3776 A.markLiveInternalFunction(*F);
3777 return true;
3778 }
3779
3780 /// Collection of instructions that need to be explored again, e.g., we
3781 /// did assume they do not transfer control to (one of their) successors.
3782 SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
3783
3784 /// Collection of instructions that are known to not transfer control.
3785 SmallSetVector<const Instruction *, 8> KnownDeadEnds;
3786
3787 /// Collection of all assumed live edges
3788 DenseSet<std::pair<const BasicBlock *, const BasicBlock *>> AssumedLiveEdges;
3789
3790 /// Collection of all assumed live BasicBlocks.
3791 DenseSet<const BasicBlock *> AssumedLiveBlocks;
3792};
3793
3794static bool
3795identifyAliveSuccessors(Attributor &A, const CallBase &CB,
3796 AbstractAttribute &AA,
3797 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3798 const IRPosition &IPos = IRPosition::callsite_function(CB);
3799
3800 const auto &NoReturnAA =
3801 A.getAndUpdateAAFor<AANoReturn>(AA, IPos, DepClassTy::OPTIONAL);
3802 if (NoReturnAA.isAssumedNoReturn())
3803 return !NoReturnAA.isKnownNoReturn();
3804 if (CB.isTerminator())
3805 AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
3806 else
3807 AliveSuccessors.push_back(CB.getNextNode());
3808 return false;
3809}
3810
3811static bool
3812identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
3813 AbstractAttribute &AA,
3814 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3815 bool UsedAssumedInformation =
3816 identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
3817
3818 // First, determine if we can change an invoke to a call assuming the
3819 // callee is nounwind. This is not possible if the personality of the
3820 // function allows to catch asynchronous exceptions.
3821 if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
3822 AliveSuccessors.push_back(&II.getUnwindDest()->front());
3823 } else {
3824 const IRPosition &IPos = IRPosition::callsite_function(II);
3825 const auto &AANoUnw =
3826 A.getAndUpdateAAFor<AANoUnwind>(AA, IPos, DepClassTy::OPTIONAL);
3827 if (AANoUnw.isAssumedNoUnwind()) {
3828 UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
3829 } else {
3830 AliveSuccessors.push_back(&II.getUnwindDest()->front());
3831 }
3832 }
3833 return UsedAssumedInformation;
3834}
3835
3836static bool
3837identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
3838 AbstractAttribute &AA,
3839 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3840 bool UsedAssumedInformation = false;
3841 if (BI.getNumSuccessors() == 1) {
3842 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3843 } else {
3844 Optional<Constant *> C =
3845 A.getAssumedConstant(*BI.getCondition(), AA, UsedAssumedInformation);
3846 if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
3847 // No value yet, assume both edges are dead.
3848 } else if (isa_and_nonnull<ConstantInt>(*C)) {
3849 const BasicBlock *SuccBB =
3850 BI.getSuccessor(1 - cast<ConstantInt>(*C)->getValue().getZExtValue());
3851 AliveSuccessors.push_back(&SuccBB->front());
3852 } else {
3853 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3854 AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
3855 UsedAssumedInformation = false;
3856 }
3857 }
3858 return UsedAssumedInformation;
3859}
3860
3861static bool
3862identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
3863 AbstractAttribute &AA,
3864 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3865 bool UsedAssumedInformation = false;
3866 Optional<Constant *> C =
3867 A.getAssumedConstant(*SI.getCondition(), AA, UsedAssumedInformation);
3868 if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
3869 // No value yet, assume all edges are dead.
3870 } else if (isa_and_nonnull<ConstantInt>(C.getValue())) {
3871 for (auto &CaseIt : SI.cases()) {
3872 if (CaseIt.getCaseValue() == C.getValue()) {
3873 AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
3874 return UsedAssumedInformation;
3875 }
3876 }
3877 AliveSuccessors.push_back(&SI.getDefaultDest()->front());
3878 return UsedAssumedInformation;
3879 } else {
3880 for (const BasicBlock *SuccBB : successors(SI.getParent()))
3881 AliveSuccessors.push_back(&SuccBB->front());
3882 }
3883 return UsedAssumedInformation;
3884}
3885
3886ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
3887 ChangeStatus Change = ChangeStatus::UNCHANGED;
3888
3889 LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"do { } while (false)
3890 << getAnchorScope()->size() << "] BBs and "do { } while (false)
3891 << ToBeExploredFrom.size() << " exploration points and "do { } while (false)
3892 << KnownDeadEnds.size() << " known dead ends\n")do { } while (false);
3893
3894 // Copy and clear the list of instructions we need to explore from. It is
3895 // refilled with instructions the next update has to look at.
3896 SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
3897 ToBeExploredFrom.end());
3898 decltype(ToBeExploredFrom) NewToBeExploredFrom;
3899
3900 SmallVector<const Instruction *, 8> AliveSuccessors;
3901 while (!Worklist.empty()) {
3902 const Instruction *I = Worklist.pop_back_val();
3903 LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n")do { } while (false);
3904
3905 // Fast forward for uninteresting instructions. We could look for UB here
3906 // though.
3907 while (!I->isTerminator() && !isa<CallBase>(I))
3908 I = I->getNextNode();
3909
3910 AliveSuccessors.clear();
3911
3912 bool UsedAssumedInformation = false;
3913 switch (I->getOpcode()) {
3914 // TODO: look for (assumed) UB to backwards propagate "deadness".
3915 default:
3916 assert(I->isTerminator() &&((void)0)
3917 "Expected non-terminators to be handled already!")((void)0);
3918 for (const BasicBlock *SuccBB : successors(I->getParent()))
3919 AliveSuccessors.push_back(&SuccBB->front());
3920 break;
3921 case Instruction::Call:
3922 UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
3923 *this, AliveSuccessors);
3924 break;
3925 case Instruction::Invoke:
3926 UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
3927 *this, AliveSuccessors);
3928 break;
3929 case Instruction::Br:
3930 UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
3931 *this, AliveSuccessors);
3932 break;
3933 case Instruction::Switch:
3934 UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
3935 *this, AliveSuccessors);
3936 break;
3937 }
3938
3939 if (UsedAssumedInformation) {
3940 NewToBeExploredFrom.insert(I);
3941 } else if (AliveSuccessors.empty() ||
3942 (I->isTerminator() &&
3943 AliveSuccessors.size() < I->getNumSuccessors())) {
3944 if (KnownDeadEnds.insert(I))
3945 Change = ChangeStatus::CHANGED;
3946 }
3947
3948 LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "do { } while (false)
3949 << AliveSuccessors.size() << " UsedAssumedInformation: "do { } while (false)
3950 << UsedAssumedInformation << "\n")do { } while (false);
3951
3952 for (const Instruction *AliveSuccessor : AliveSuccessors) {
3953 if (!I->isTerminator()) {
3954 assert(AliveSuccessors.size() == 1 &&((void)0)
3955 "Non-terminator expected to have a single successor!")((void)0);
3956 Worklist.push_back(AliveSuccessor);
3957 } else {
3958 // record the assumed live edge
3959 auto Edge = std::make_pair(I->getParent(), AliveSuccessor->getParent());
3960 if (AssumedLiveEdges.insert(Edge).second)
3961 Change = ChangeStatus::CHANGED;
3962 if (assumeLive(A, *AliveSuccessor->getParent()))
3963 Worklist.push_back(AliveSuccessor);
3964 }
3965 }
3966 }
3967
3968 // Check if the content of ToBeExploredFrom changed, ignore the order.
3969 if (NewToBeExploredFrom.size() != ToBeExploredFrom.size() ||
3970 llvm::any_of(NewToBeExploredFrom, [&](const Instruction *I) {
3971 return !ToBeExploredFrom.count(I);
3972 })) {
3973 Change = ChangeStatus::CHANGED;
3974 ToBeExploredFrom = std::move(NewToBeExploredFrom);
3975 }
3976
3977 // If we know everything is live there is no need to query for liveness.
3978 // Instead, indicating a pessimistic fixpoint will cause the state to be
3979 // "invalid" and all queries to be answered conservatively without lookups.
3980 // To be in this state we have to (1) finished the exploration and (3) not
3981 // discovered any non-trivial dead end and (2) not ruled unreachable code
3982 // dead.
3983 if (ToBeExploredFrom.empty() &&
3984 getAnchorScope()->size() == AssumedLiveBlocks.size() &&
3985 llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
3986 return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
3987 }))
3988 return indicatePessimisticFixpoint();
3989 return Change;
3990}
3991
3992/// Liveness information for a call sites.
3993struct AAIsDeadCallSite final : AAIsDeadFunction {
3994 AAIsDeadCallSite(const IRPosition &IRP, Attributor &A)
3995 : AAIsDeadFunction(IRP, A) {}
3996
3997 /// See AbstractAttribute::initialize(...).
3998 void initialize(Attributor &A) override {
3999 // TODO: Once we have call site specific value information we can provide
4000 // call site specific liveness information and then it makes
4001 // sense to specialize attributes for call sites instead of
4002 // redirecting requests to the callee.
4003 llvm_unreachable("Abstract attributes for liveness are not "__builtin_unreachable()
4004 "supported for call sites yet!")__builtin_unreachable();
4005 }
4006
4007 /// See AbstractAttribute::updateImpl(...).
4008 ChangeStatus updateImpl(Attributor &A) override {
4009 return indicatePessimisticFixpoint();
4010 }
4011
4012 /// See AbstractAttribute::trackStatistics()
4013 void trackStatistics() const override {}
4014};
4015
4016/// -------------------- Dereferenceable Argument Attribute --------------------
4017
4018struct AADereferenceableImpl : AADereferenceable {
4019 AADereferenceableImpl(const IRPosition &IRP, Attributor &A)
4020 : AADereferenceable(IRP, A) {}
4021 using StateType = DerefState;
4022
4023 /// See AbstractAttribute::initialize(...).
4024 void initialize(Attributor &A) override {
4025 SmallVector<Attribute, 4> Attrs;
4026 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
4027 Attrs, /* IgnoreSubsumingPositions */ false, &A);
4028 for (const Attribute &Attr : Attrs)
4029 takeKnownDerefBytesMaximum(Attr.getValueAsInt());
4030
4031 const IRPosition &IRP = this->getIRPosition();
4032 NonNullAA = &A.getAAFor<AANonNull>(*this, IRP, DepClassTy::NONE);
4033
4034 bool CanBeNull, CanBeFreed;
4035 takeKnownDerefBytesMaximum(
4036 IRP.getAssociatedValue().getPointerDereferenceableBytes(
4037 A.getDataLayout(), CanBeNull, CanBeFreed));
4038
4039 bool IsFnInterface = IRP.isFnInterfaceKind();
4040 Function *FnScope = IRP.getAnchorScope();
4041 if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope))) {
4042 indicatePessimisticFixpoint();
4043 return;
4044 }
4045
4046 if (Instruction *CtxI = getCtxI())
4047 followUsesInMBEC(*this, A, getState(), *CtxI);
4048 }
4049
4050 /// See AbstractAttribute::getState()
4051 /// {
4052 StateType &getState() override { return *this; }
4053 const StateType &getState() const override { return *this; }
4054 /// }
4055
4056 /// Helper function for collecting accessed bytes in must-be-executed-context
4057 void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I,
4058 DerefState &State) {
4059 const Value *UseV = U->get();
4060 if (!UseV->getType()->isPointerTy())
4061 return;
4062
4063 Type *PtrTy = UseV->getType();
4064 const DataLayout &DL = A.getDataLayout();
4065 int64_t Offset;
4066 if (const Value *Base = getBasePointerOfAccessPointerOperand(
4067 I, Offset, DL, /*AllowNonInbounds*/ true)) {
4068 if (Base == &getAssociatedValue() &&
4069 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
4070 uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType());
4071 State.addAccessedBytes(Offset, Size);
4072 }
4073 }
4074 }
4075
4076 /// See followUsesInMBEC
4077 bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
4078 AADereferenceable::StateType &State) {
4079 bool IsNonNull = false;
4080 bool TrackUse = false;
4081 int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
4082 A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
4083 LLVM_DEBUG(dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytesdo { } while (false)
4084 << " for instruction " << *I << "\n")do { } while (false);
4085
4086 addAccessedBytesForUse(A, U, I, State);
4087 State.takeKnownDerefBytesMaximum(DerefBytes);
4088 return TrackUse;
4089 }
4090
4091 /// See AbstractAttribute::manifest(...).
4092 ChangeStatus manifest(Attributor &A) override {
4093 ChangeStatus Change = AADereferenceable::manifest(A);
4094 if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
4095 removeAttrs({Attribute::DereferenceableOrNull});
4096 return ChangeStatus::CHANGED;
4097 }
4098 return Change;
4099 }
4100
4101 void getDeducedAttributes(LLVMContext &Ctx,
4102 SmallVectorImpl<Attribute> &Attrs) const override {
4103 // TODO: Add *_globally support
4104 if (isAssumedNonNull())
4105 Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
4106 Ctx, getAssumedDereferenceableBytes()));
4107 else
4108 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
4109 Ctx, getAssumedDereferenceableBytes()));
4110 }
4111
4112 /// See AbstractAttribute::getAsStr().
4113 const std::string getAsStr() const override {
4114 if (!getAssumedDereferenceableBytes())
4115 return "unknown-dereferenceable";
4116 return std::string("dereferenceable") +
4117 (isAssumedNonNull() ? "" : "_or_null") +
4118 (isAssumedGlobal() ? "_globally" : "") + "<" +
4119 std::to_string(getKnownDereferenceableBytes()) + "-" +
4120 std::to_string(getAssumedDereferenceableBytes()) + ">";
4121 }
4122};
4123
4124/// Dereferenceable attribute for a floating value.
4125struct AADereferenceableFloating : AADereferenceableImpl {
4126 AADereferenceableFloating(const IRPosition &IRP, Attributor &A)
4127 : AADereferenceableImpl(IRP, A) {}
4128
4129 /// See AbstractAttribute::updateImpl(...).
4130 ChangeStatus updateImpl(Attributor &A) override {
4131 const DataLayout &DL = A.getDataLayout();
4132
4133 auto VisitValueCB = [&](const Value &V, const Instruction *, DerefState &T,
4134 bool Stripped) -> bool {
4135 unsigned IdxWidth =
4136 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
4137 APInt Offset(IdxWidth, 0);
4138 const Value *Base =
4139 stripAndAccumulateMinimalOffsets(A, *this, &V, DL, Offset, false);
4140
4141 const auto &AA = A.getAAFor<AADereferenceable>(
4142 *this, IRPosition::value(*Base), DepClassTy::REQUIRED);
4143 int64_t DerefBytes = 0;
4144 if (!Stripped && this == &AA) {
4145 // Use IR information if we did not strip anything.
4146 // TODO: track globally.
4147 bool CanBeNull, CanBeFreed;
4148 DerefBytes =
4149 Base->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
4150 T.GlobalState.indicatePessimisticFixpoint();
4151 } else {
4152 const DerefState &DS = AA.getState();
4153 DerefBytes = DS.DerefBytesState.getAssumed();
4154 T.GlobalState &= DS.GlobalState;
4155 }
4156
4157 // For now we do not try to "increase" dereferenceability due to negative
4158 // indices as we first have to come up with code to deal with loops and
4159 // for overflows of the dereferenceable bytes.
4160 int64_t OffsetSExt = Offset.getSExtValue();
4161 if (OffsetSExt < 0)
4162 OffsetSExt = 0;
4163
4164 T.takeAssumedDerefBytesMinimum(
4165 std::max(int64_t(0), DerefBytes - OffsetSExt));
4166
4167 if (this == &AA) {
4168 if (!Stripped) {
4169 // If nothing was stripped IR information is all we got.
4170 T.takeKnownDerefBytesMaximum(
4171 std::max(int64_t(0), DerefBytes - OffsetSExt));
4172 T.indicatePessimisticFixpoint();
4173 } else if (OffsetSExt > 0) {
4174 // If something was stripped but there is circular reasoning we look
4175 // for the offset. If it is positive we basically decrease the
4176 // dereferenceable bytes in a circluar loop now, which will simply
4177 // drive them down to the known value in a very slow way which we
4178 // can accelerate.
4179 T.indicatePessimisticFixpoint();
4180 }
4181 }
4182
4183 return T.isValidState();
4184 };
4185
4186 DerefState T;
4187 if (!genericValueTraversal<DerefState>(A, getIRPosition(), *this, T,
4188 VisitValueCB, getCtxI()))
4189 return indicatePessimisticFixpoint();
4190
4191 return clampStateAndIndicateChange(getState(), T);
4192 }
4193
4194 /// See AbstractAttribute::trackStatistics()
4195 void trackStatistics() const override {
4196 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable){ static llvm::Statistic NumIRFloating_dereferenceable = {"attributor"
, "NumIRFloating_dereferenceable", ("Number of floating values known to be '"
"dereferenceable" "'")};; ++(NumIRFloating_dereferenceable);
}
4197 }
4198};
4199
4200/// Dereferenceable attribute for a return value.
4201struct AADereferenceableReturned final
4202 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
4203 AADereferenceableReturned(const IRPosition &IRP, Attributor &A)
4204 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
4205 IRP, A) {}
4206
4207 /// See AbstractAttribute::trackStatistics()
4208 void trackStatistics() const override {
4209 STATS_DECLTRACK_FNRET_ATTR(dereferenceable){ static llvm::Statistic NumIRFunctionReturn_dereferenceable =
{"attributor", "NumIRFunctionReturn_dereferenceable", ("Number of "
"function returns" " marked '" "dereferenceable" "'")};; ++(
NumIRFunctionReturn_dereferenceable); }
4210 }
4211};
4212
4213/// Dereferenceable attribute for an argument
4214struct AADereferenceableArgument final
4215 : AAArgumentFromCallSiteArguments<AADereferenceable,
4216 AADereferenceableImpl> {
4217 using Base =
4218 AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl>;
4219 AADereferenceableArgument(const IRPosition &IRP, Attributor &A)
4220 : Base(IRP, A) {}
4221
4222 /// See AbstractAttribute::trackStatistics()
4223 void trackStatistics() const override {
4224 STATS_DECLTRACK_ARG_ATTR(dereferenceable){ static llvm::Statistic NumIRArguments_dereferenceable = {"attributor"
, "NumIRArguments_dereferenceable", ("Number of " "arguments"
" marked '" "dereferenceable" "'")};; ++(NumIRArguments_dereferenceable
); }
4225 }
4226};
4227
4228/// Dereferenceable attribute for a call site argument.
4229struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
4230 AADereferenceableCallSiteArgument(const IRPosition &IRP, Attributor &A)
4231 : AADereferenceableFloating(IRP, A) {}
4232
4233 /// See AbstractAttribute::trackStatistics()
4234 void trackStatistics() const override {
4235 STATS_DECLTRACK_CSARG_ATTR(dereferenceable){ static llvm::Statistic NumIRCSArguments_dereferenceable = {
"attributor", "NumIRCSArguments_dereferenceable", ("Number of "
"call site arguments" " marked '" "dereferenceable" "'")};; ++
(NumIRCSArguments_dereferenceable); }
4236 }
4237};
4238
4239/// Dereferenceable attribute deduction for a call site return value.
4240struct AADereferenceableCallSiteReturned final
4241 : AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl> {
4242 using Base =
4243 AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl>;
4244 AADereferenceableCallSiteReturned(const IRPosition &IRP, Attributor &A)
4245 : Base(IRP, A) {}
4246
4247 /// See AbstractAttribute::trackStatistics()
4248 void trackStatistics() const override {
4249 STATS_DECLTRACK_CS_ATTR(dereferenceable){ static llvm::Statistic NumIRCS_dereferenceable = {"attributor"
, "NumIRCS_dereferenceable", ("Number of " "call site" " marked '"
"dereferenceable" "'")};; ++(NumIRCS_dereferenceable); }
;
4250 }
4251};
4252
4253// ------------------------ Align Argument Attribute ------------------------
4254
4255static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA,
4256 Value &AssociatedValue, const Use *U,
4257 const Instruction *I, bool &TrackUse) {
4258 // We need to follow common pointer manipulation uses to the accesses they
4259 // feed into.
4260 if (isa<CastInst>(I)) {
1
Assuming 'I' is not a 'CastInst'
2
Taking false branch
4261 // Follow all but ptr2int casts.
4262 TrackUse = !isa<PtrToIntInst>(I);
4263 return 0;
4264 }
4265 if (auto *GEP
3.1
'GEP' is null
3.1
'GEP' is null
3.1
'GEP' is null
3.1
'GEP' is null
= dyn_cast<GetElementPtrInst>(I)) {
3
Assuming 'I' is not a 'GetElementPtrInst'
4
Taking false branch
4266 if (GEP->hasAllConstantIndices())
4267 TrackUse = true;
4268 return 0;
4269 }
4270
4271 MaybeAlign MA;
4272 if (const auto *CB
5.1
'CB' is null
5.1
'CB' is null
5.1
'CB' is null
5.1
'CB' is null
= dyn_cast<CallBase>(I)) {
5
Assuming 'I' is not a 'CallBase'
6
Taking false branch
4273 if (CB->isBundleOperand(U) || CB->isCallee(U))
4274 return 0;
4275
4276 unsigned ArgNo = CB->getArgOperandNo(U);
4277 IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
4278 // As long as we only use known information there is no need to track
4279 // dependences here.
4280 auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, DepClassTy::NONE);
4281 MA = MaybeAlign(AlignAA.getKnownAlign());
4282 }
4283
4284 const DataLayout &DL = A.getDataLayout();
4285 const Value *UseV = U->get();
7
'UseV' initialized here
4286 if (auto *SI
8.1
'SI' is non-null
8.1
'SI' is non-null
8.1
'SI' is non-null
8.1
'SI' is non-null
= dyn_cast<StoreInst>(I)) {
8
Assuming 'I' is a 'StoreInst'
9
Taking true branch
4287 if (SI->getPointerOperand() == UseV)
10
Assuming pointer value is null
11
Taking true branch
4288 MA = SI->getAlign();
4289 } else if (auto *LI = dyn_cast<LoadInst>(I)) {
4290 if (LI->getPointerOperand() == UseV)
4291 MA = LI->getAlign();
4292 }
4293
4294 if (!MA || *MA <= QueryingAA.getKnownAlign())
12
Calling 'Optional::operator bool'
20
Returning from 'Optional::operator bool'
21
Calling 'operator<='
24
Returning from 'operator<='
25
Taking false branch
4295 return 0;
4296
4297 unsigned Alignment = MA->value();
4298 int64_t Offset;
4299
4300 if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
26
Passing null pointer value via 1st parameter 'Ptr'
27
Calling 'GetPointerBaseWithConstantOffset'
4301 if (Base == &AssociatedValue) {
4302 // BasePointerAddr + Offset = Alignment * Q for some integer Q.
4303 // So we can say that the maximum power of two which is a divisor of
4304 // gcd(Offset, Alignment) is an alignment.
4305
4306 uint32_t gcd =
4307 greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
4308 Alignment = llvm::PowerOf2Floor(gcd);
4309 }
4310 }
4311
4312 return Alignment;
4313}
4314
4315struct AAAlignImpl : AAAlign {
4316 AAAlignImpl(const IRPosition &IRP, Attributor &A) : AAAlign(IRP, A) {}
4317
4318 /// See AbstractAttribute::initialize(...).
4319 void initialize(Attributor &A) override {
4320 SmallVector<Attribute, 4> Attrs;
4321 getAttrs({Attribute::Alignment}, Attrs);
4322 for (const Attribute &Attr : Attrs)
4323 takeKnownMaximum(Attr.getValueAsInt());
4324
4325 Value &V = getAssociatedValue();
4326 // TODO: This is a HACK to avoid getPointerAlignment to introduce a ptr2int
4327 // use of the function pointer. This was caused by D73131. We want to
4328 // avoid this for function pointers especially because we iterate
4329 // their uses and int2ptr is not handled. It is not a correctness
4330 // problem though!
4331 if (!V.getType()->getPointerElementType()->isFunctionTy())
4332 takeKnownMaximum(V.getPointerAlignment(A.getDataLayout()).value());
4333
4334 if (getIRPosition().isFnInterfaceKind() &&
4335 (!getAnchorScope() ||
4336 !A.isFunctionIPOAmendable(*getAssociatedFunction()))) {
4337 indicatePessimisticFixpoint();
4338 return;
4339 }
4340
4341 if (Instruction *CtxI = getCtxI())
4342 followUsesInMBEC(*this, A, getState(), *CtxI);
4343 }
4344
4345 /// See AbstractAttribute::manifest(...).
4346 ChangeStatus manifest(Attributor &A) override {
4347 ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
4348
4349 // Check for users that allow alignment annotations.
4350 Value &AssociatedValue = getAssociatedValue();
4351 for (const Use &U : AssociatedValue.uses()) {
4352 if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
4353 if (SI->getPointerOperand() == &AssociatedValue)
4354 if (SI->getAlignment() < getAssumedAlign()) {
4355 STATS_DECLTRACK(AAAlign, Store,{ static llvm::Statistic NumIRStore_AAAlign = {"attributor", "NumIRStore_AAAlign"
, "Number of times alignment added to a store"};; ++(NumIRStore_AAAlign
); }
4356 "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
); }
;
4357 SI->setAlignment(Align(getAssumedAlign()));
4358 LoadStoreChanged = ChangeStatus::CHANGED;
4359 }
4360 } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
4361 if (LI->getPointerOperand() == &AssociatedValue)
4362 if (LI->getAlignment() < getAssumedAlign()) {
4363 LI->setAlignment(Align(getAssumedAlign()));
4364 STATS_DECLTRACK(AAAlign, Load,{ static llvm::Statistic NumIRLoad_AAAlign = {"attributor", "NumIRLoad_AAAlign"
, "Number of times alignment added to a load"};; ++(NumIRLoad_AAAlign
); }
4365 "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
); }
;
4366 LoadStoreChanged = ChangeStatus::CHANGED;
4367 }
4368 }
4369 }
4370
4371 ChangeStatus Changed = AAAlign::manifest(A);
4372
4373 Align InheritAlign =
4374 getAssociatedValue().getPointerAlignment(A.getDataLayout());
4375 if (InheritAlign >= getAssumedAlign())
4376 return LoadStoreChanged;
4377 return Changed | LoadStoreChanged;
4378 }
4379
4380 // TODO: Provide a helper to determine the implied ABI alignment and check in
4381 // the existing manifest method and a new one for AAAlignImpl that value
4382 // to avoid making the alignment explicit if it did not improve.
4383
4384 /// See AbstractAttribute::getDeducedAttributes
4385 virtual void
4386 getDeducedAttributes(LLVMContext &Ctx,
4387 SmallVectorImpl<Attribute> &Attrs) const override {
4388 if (getAssumedAlign() > 1)
4389 Attrs.emplace_back(
4390 Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
4391 }
4392
4393 /// See followUsesInMBEC
4394 bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
4395 AAAlign::StateType &State) {
4396 bool TrackUse = false;
4397
4398 unsigned int KnownAlign =
4399 getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
4400 State.takeKnownMaximum(KnownAlign);
4401
4402 return TrackUse;
4403 }
4404
4405 /// See AbstractAttribute::getAsStr().
4406 const std::string getAsStr() const override {
4407 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
4408 "-" + std::to_string(getAssumedAlign()) + ">")
4409 : "unknown-align";
4410 }
4411};
4412
4413/// Align attribute for a floating value.
4414struct AAAlignFloating : AAAlignImpl {
4415 AAAlignFloating(const IRPosition &IRP, Attributor &A) : AAAlignImpl(IRP, A) {}
4416
4417 /// See AbstractAttribute::updateImpl(...).
4418 ChangeStatus updateImpl(Attributor &A) override {
4419 const DataLayout &DL = A.getDataLayout();
4420
4421 auto VisitValueCB = [&](Value &V, const Instruction *,
4422 AAAlign::StateType &T, bool Stripped) -> bool {
4423 const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V),
4424 DepClassTy::REQUIRED);
4425 if (!Stripped && this == &AA) {
4426 int64_t Offset;
4427 unsigned Alignment = 1;
4428 if (const Value *Base =
4429 GetPointerBaseWithConstantOffset(&V, Offset, DL)) {
4430 Align PA = Base->getPointerAlignment(DL);
4431 // BasePointerAddr + Offset = Alignment * Q for some integer Q.
4432 // So we can say that the maximum power of two which is a divisor of
4433 // gcd(Offset, Alignment) is an alignment.
4434
4435 uint32_t gcd = greatestCommonDivisor(uint32_t(abs((int32_t)Offset)),
4436 uint32_t(PA.value()));
4437 Alignment = llvm::PowerOf2Floor(gcd);
4438 } else {
4439 Alignment = V.getPointerAlignment(DL).value();
4440 }
4441 // Use only IR information if we did not strip anything.
4442 T.takeKnownMaximum(Alignment);
4443 T.indicatePessimisticFixpoint();
4444 } else {
4445 // Use abstract attribute information.
4446 const AAAlign::StateType &DS = AA.getState();
4447 T ^= DS;
4448 }
4449 return T.isValidState();
4450 };
4451
4452 StateType T;
4453 if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
4454 VisitValueCB, getCtxI()))
4455 return indicatePessimisticFixpoint();
4456
4457 // TODO: If we know we visited all incoming values, thus no are assumed
4458 // dead, we can take the known information from the state T.
4459 return clampStateAndIndicateChange(getState(), T);
4460 }
4461
4462 /// See AbstractAttribute::trackStatistics()
4463 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); }
}
4464};
4465
4466/// Align attribute for function return value.
4467struct AAAlignReturned final
4468 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
4469 using Base = AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>;
4470 AAAlignReturned(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
4471
4472 /// See AbstractAttribute::initialize(...).
4473 void initialize(Attributor &A) override {
4474 Base::initialize(A);
4475 Function *F = getAssociatedFunction();
4476 if (!F || F->isDeclaration())
4477 indicatePessimisticFixpoint();
4478 }
4479
4480 /// See AbstractAttribute::trackStatistics()
4481 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
); }
}
4482};
4483
4484/// Align attribute for function argument.
4485struct AAAlignArgument final
4486 : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
4487 using Base = AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>;
4488 AAAlignArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
4489
4490 /// See AbstractAttribute::manifest(...).
4491 ChangeStatus manifest(Attributor &A) override {
4492 // If the associated argument is involved in a must-tail call we give up
4493 // because we would need to keep the argument alignments of caller and
4494 // callee in-sync. Just does not seem worth the trouble right now.
4495 if (A.getInfoCache().isInvolvedInMustTailCall(*getAssociatedArgument()))
4496 return ChangeStatus::UNCHANGED;
4497 return Base::manifest(A);
4498 }
4499
4500 /// See AbstractAttribute::trackStatistics()
4501 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned){ static llvm::Statistic NumIRArguments_aligned = {"attributor"
, "NumIRArguments_aligned", ("Number of " "arguments" " marked '"
"aligned" "'")};; ++(NumIRArguments_aligned); }
}
4502};
4503
4504struct AAAlignCallSiteArgument final : AAAlignFloating {
4505 AAAlignCallSiteArgument(const IRPosition &IRP, Attributor &A)
4506 : AAAlignFloating(IRP, A) {}
4507
4508 /// See AbstractAttribute::manifest(...).
4509 ChangeStatus manifest(Attributor &A) override {
4510 // If the associated argument is involved in a must-tail call we give up
4511 // because we would need to keep the argument alignments of caller and
4512 // callee in-sync. Just does not seem worth the trouble right now.
4513 if (Argument *Arg = getAssociatedArgument())
4514 if (A.getInfoCache().isInvolvedInMustTailCall(*Arg))
4515 return ChangeStatus::UNCHANGED;
4516 ChangeStatus Changed = AAAlignImpl::manifest(A);
4517 Align InheritAlign =
4518 getAssociatedValue().getPointerAlignment(A.getDataLayout());
4519 if (InheritAlign >= getAssumedAlign())
4520 Changed = ChangeStatus::UNCHANGED;
4521 return Changed;
4522 }
4523
4524 /// See AbstractAttribute::updateImpl(Attributor &A).
4525 ChangeStatus updateImpl(Attributor &A) override {
4526 ChangeStatus Changed = AAAlignFloating::updateImpl(A);
4527 if (Argument *Arg = getAssociatedArgument()) {
4528 // We only take known information from the argument
4529 // so we do not need to track a dependence.
4530 const auto &ArgAlignAA = A.getAAFor<AAAlign>(
4531 *this, IRPosition::argument(*Arg), DepClassTy::NONE);
4532 takeKnownMaximum(ArgAlignAA.getKnownAlign());
4533 }
4534 return Changed;
4535 }
4536
4537 /// See AbstractAttribute::trackStatistics()
4538 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); }
}
4539};
4540
4541/// Align attribute deduction for a call site return value.
4542struct AAAlignCallSiteReturned final
4543 : AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl> {
4544 using Base = AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl>;
4545 AAAlignCallSiteReturned(const IRPosition &IRP, Attributor &A)
4546 : Base(IRP, A) {}
4547
4548 /// See AbstractAttribute::initialize(...).
4549 void initialize(Attributor &A) override {
4550 Base::initialize(A);
4551 Function *F = getAssociatedFunction();
4552 if (!F || F->isDeclaration())
4553 indicatePessimisticFixpoint();
4554 }
4555
4556 /// See AbstractAttribute::trackStatistics()
4557 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
); }
; }
4558};
4559
4560/// ------------------ Function No-Return Attribute ----------------------------
4561struct AANoReturnImpl : public AANoReturn {
4562 AANoReturnImpl(const IRPosition &IRP, Attributor &A) : AANoReturn(IRP, A) {}
4563
4564 /// See AbstractAttribute::initialize(...).
4565 void initialize(Attributor &A) override {
4566 AANoReturn::initialize(A);
4567 Function *F = getAssociatedFunction();
4568 if (!F || F->isDeclaration())
4569 indicatePessimisticFixpoint();
4570 }
4571
4572 /// See AbstractAttribute::getAsStr().
4573 const std::string getAsStr() const override {
4574 return getAssumed() ? "noreturn" : "may-return";
4575 }
4576
4577 /// See AbstractAttribute::updateImpl(Attributor &A).
4578 virtual ChangeStatus updateImpl(Attributor &A) override {
4579 auto CheckForNoReturn = [](Instruction &) { return false; };
4580 bool UsedAssumedInformation = false;
4581 if (!A.checkForAllInstructions(CheckForNoReturn, *this,
4582 {(unsigned)Instruction::Ret},
4583 UsedAssumedInformation))
4584 return indicatePessimisticFixpoint();
4585 return ChangeStatus::UNCHANGED;
4586 }
4587};
4588
4589struct AANoReturnFunction final : AANoReturnImpl {
4590 AANoReturnFunction(const IRPosition &IRP, Attributor &A)
4591 : AANoReturnImpl(IRP, A) {}
4592
4593 /// See AbstractAttribute::trackStatistics()
4594 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn){ static llvm::Statistic NumIRFunction_noreturn = {"attributor"
, "NumIRFunction_noreturn", ("Number of " "functions" " marked '"
"noreturn" "'")};; ++(NumIRFunction_noreturn); }
}
4595};
4596
4597/// NoReturn attribute deduction for a call sites.
4598struct AANoReturnCallSite final : AANoReturnImpl {
4599 AANoReturnCallSite(const IRPosition &IRP, Attributor &A)
4600 : AANoReturnImpl(IRP, A) {}
4601
4602 /// See AbstractAttribute::initialize(...).
4603 void initialize(Attributor &A) override {
4604 AANoReturnImpl::initialize(A);
4605 if (Function *F = getAssociatedFunction()) {
4606 const IRPosition &FnPos = IRPosition::function(*F);
4607 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4608 if (!FnAA.isAssumedNoReturn())
4609 indicatePessimisticFixpoint();
4610 }
4611 }
4612
4613 /// See AbstractAttribute::updateImpl(...).
4614 ChangeStatus updateImpl(Attributor &A) override {
4615 // TODO: Once we have call site specific value information we can provide
4616 // call site specific liveness information and then it makes
4617 // sense to specialize attributes for call sites arguments instead of
4618 // redirecting requests to the callee argument.
4619 Function *F = getAssociatedFunction();
4620 const IRPosition &FnPos = IRPosition::function(*F);
4621 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
4622 return clampStateAndIndicateChange(getState(), FnAA.getState());
4623 }
4624
4625 /// See AbstractAttribute::trackStatistics()
4626 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); }
; }
4627};
4628
4629/// ----------------------- Variable Capturing ---------------------------------
4630
4631/// A class to hold the state of for no-capture attributes.
4632struct AANoCaptureImpl : public AANoCapture {
4633 AANoCaptureImpl(const IRPosition &IRP, Attributor &A) : AANoCapture(IRP, A) {}
4634
4635 /// See AbstractAttribute::initialize(...).
4636 void initialize(Attributor &A) override {
4637 if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
4638 indicateOptimisticFixpoint();
4639 return;
4640 }
4641 Function *AnchorScope = getAnchorScope();
4642 if (isFnInterfaceKind() &&
4643 (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) {
4644 indicatePessimisticFixpoint();
4645 return;
4646 }
4647
4648 // You cannot "capture" null in the default address space.
4649 if (isa<ConstantPointerNull>(getAssociatedValue()) &&
4650 getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
4651 indicateOptimisticFixpoint();
4652 return;
4653 }
4654
4655 const Function *F =
4656 isArgumentPosition() ? getAssociatedFunction() : AnchorScope;
4657
4658 // Check what state the associated function can actually capture.
4659 if (F)
4660 determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
4661 else
4662 indicatePessimisticFixpoint();
4663 }
4664
4665 /// See AbstractAttribute::updateImpl(...).
4666 ChangeStatus updateImpl(Attributor &A) override;
4667
4668 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
4669 virtual void
4670 getDeducedAttributes(LLVMContext &Ctx,
4671 SmallVectorImpl<Attribute> &Attrs) const override {
4672 if (!isAssumedNoCaptureMaybeReturned())
4673 return;
4674
4675 if (isArgumentPosition()) {
4676 if (isAssumedNoCapture())
4677 Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
4678 else if (ManifestInternal)
4679 Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
4680 }
4681 }
4682
4683 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
4684 /// depending on the ability of the function associated with \p IRP to capture
4685 /// state in memory and through "returning/throwing", respectively.
4686 static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
4687 const Function &F,
4688 BitIntegerState &State) {
4689 // TODO: Once we have memory behavior attributes we should use them here.
4690
4691 // If we know we cannot communicate or write to memory, we do not care about
4692 // ptr2int anymore.
4693 if (F.onlyReadsMemory() && F.doesNotThrow() &&
4694 F.getReturnType()->isVoidTy()) {
4695 State.addKnownBits(NO_CAPTURE);
4696 return;
4697 }
4698
4699 // A function cannot capture state in memory if it only reads memory, it can
4700 // however return/throw state and the state might be influenced by the
4701 // pointer value, e.g., loading from a returned pointer might reveal a bit.
4702 if (F.onlyReadsMemory())
4703 State.addKnownBits(NOT_CAPTURED_IN_MEM);
4704
4705 // A function cannot communicate state back if it does not through
4706 // exceptions and doesn not return values.
4707 if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
4708 State.addKnownBits(NOT_CAPTURED_IN_RET);
4709
4710 // Check existing "returned" attributes.
4711 int ArgNo = IRP.getCalleeArgNo();
4712 if (F.doesNotThrow() && ArgNo >= 0) {
4713 for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
4714 if (F.hasParamAttribute(u, Attribute::Returned)) {
4715 if (u == unsigned(ArgNo))
4716 State.removeAssumedBits(NOT_CAPTURED_IN_RET);
4717 else if (F.onlyReadsMemory())
4718 State.addKnownBits(NO_CAPTURE);
4719 else
4720 State.addKnownBits(NOT_CAPTURED_IN_RET);
4721 break;
4722 }
4723 }
4724 }
4725
4726 /// See AbstractState::getAsStr().
4727 const std::string getAsStr() const override {
4728 if (isKnownNoCapture())
4729 return "known not-captured";
4730 if (isAssumedNoCapture())
4731 return "assumed not-captured";
4732 if (isKnownNoCaptureMaybeReturned())
4733 return "known not-captured-maybe-returned";
4734 if (isAssumedNoCaptureMaybeReturned())
4735 return "assumed not-captured-maybe-returned";
4736 return "assumed-captured";
4737 }
4738};
4739
4740/// Attributor-aware capture tracker.
4741struct AACaptureUseTracker final : public CaptureTracker {
4742
4743 /// Create a capture tracker that can lookup in-flight abstract attributes
4744 /// through the Attributor \p A.
4745 ///
4746 /// If a use leads to a potential capture, \p CapturedInMemory is set and the
4747 /// search is stopped. If a use leads to a return instruction,
4748 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
4749 /// If a use leads to a ptr2int which may capture the value,
4750 /// \p CapturedInInteger is set. If a use is found that is currently assumed
4751 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
4752 /// set. All values in \p PotentialCopies are later tracked as well. For every
4753 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
4754 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
4755 /// conservatively set to true.
4756 AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
4757 const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
4758 SmallSetVector<Value *, 4> &PotentialCopies,
4759 unsigned &RemainingUsesToExplore)
4760 : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
4761 PotentialCopies(PotentialCopies),
4762 RemainingUsesToExplore(RemainingUsesToExplore) {}
4763
4764 /// Determine if \p V maybe captured. *Also updates the state!*
4765 bool valueMayBeCaptured(const Value *V) {
4766 if (V->getType()->isPointerTy()) {
4767 PointerMayBeCaptured(V, this);
4768 } else {
4769 State.indicatePessimisticFixpoint();
4770 }
4771 return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4772 }
4773
4774 /// See CaptureTracker::tooManyUses().
4775 void tooManyUses() override {
4776 State.removeAssumedBits(AANoCapture::NO_CAPTURE);
4777 }
4778
4779 bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
4780 if (CaptureTracker::isDereferenceableOrNull(O, DL))
4781 return true;
4782 const auto &DerefAA = A.getAAFor<AADereferenceable>(
4783 NoCaptureAA, IRPosition::value(*O), DepClassTy::OPTIONAL);
4784 return DerefAA.getAssumedDereferenceableBytes();
4785 }
4786
4787 /// See CaptureTracker::captured(...).
4788 bool captured(const Use *U) override {
4789 Instruction *UInst = cast<Instruction>(U->getUser());
4790 LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInstdo { } while (false)
4791 << "\n")do { } while (false);
4792
4793 // Because we may reuse the tracker multiple times we keep track of the
4794 // number of explored uses ourselves as well.
4795 if (RemainingUsesToExplore-- == 0) {
4796 LLVM_DEBUG(dbgs() << " - too many uses to explore!\n")do { } while (false);
4797 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4798 /* Return */ true);
4799 }
4800
4801 // Deal with ptr2int by following uses.
4802 if (isa<PtrToIntInst>(UInst)) {
4803 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n")do { } while (false);
4804 return valueMayBeCaptured(UInst);
4805 }
4806
4807 // For stores we check if we can follow the value through memory or not.
4808 if (auto *SI = dyn_cast<StoreInst>(UInst)) {
4809 if (SI->isVolatile())
4810 return isCapturedIn(/* Memory */ true, /* Integer */ false,
4811 /* Return */ false);
4812 bool UsedAssumedInformation = false;
4813 if (!AA::getPotentialCopiesOfStoredValue(
4814 A, *SI, PotentialCopies, NoCaptureAA, UsedAssumedInformation))
4815 return isCapturedIn(/* Memory */ true, /* Integer */ false,
4816 /* Return */ false);
4817 // Not captured directly, potential copies will be checked.
4818 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4819 /* Return */ false);
4820 }
4821
4822 // Explicitly catch return instructions.
4823 if (isa<ReturnInst>(UInst)) {
4824 if (UInst->getFunction() == NoCaptureAA.getAnchorScope())
4825 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4826 /* Return */ true);
4827 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4828 /* Return */ true);
4829 }
4830
4831 // For now we only use special logic for call sites. However, the tracker
4832 // itself knows about a lot of other non-capturing cases already.
4833 auto *CB = dyn_cast<CallBase>(UInst);
4834 if (!CB || !CB->isArgOperand(U))
4835 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4836 /* Return */ true);
4837
4838 unsigned ArgNo = CB->getArgOperandNo(U);
4839 const IRPosition &CSArgPos = IRPosition::callsite_argument(*CB, ArgNo);
4840 // If we have a abstract no-capture attribute for the argument we can use
4841 // it to justify a non-capture attribute here. This allows recursion!
4842 auto &ArgNoCaptureAA =
4843 A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos, DepClassTy::REQUIRED);
4844 if (ArgNoCaptureAA.isAssumedNoCapture())
4845 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4846 /* Return */ false);
4847 if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
4848 addPotentialCopy(*CB);
4849 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4850 /* Return */ false);
4851 }
4852
4853 // Lastly, we could not find a reason no-capture can be assumed so we don't.
4854 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4855 /* Return */ true);
4856 }
4857
4858 /// Register \p CS as potential copy of the value we are checking.
4859 void addPotentialCopy(CallBase &CB) { PotentialCopies.insert(&CB); }
4860
4861 /// See CaptureTracker::shouldExplore(...).
4862 bool shouldExplore(const Use *U) override {
4863 // Check liveness and ignore droppable users.
4864 bool UsedAssumedInformation = false;
4865 return !U->getUser()->isDroppable() &&
4866 !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA,
4867 UsedAssumedInformation);
4868 }
4869
4870 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
4871 /// \p CapturedInRet, then return the appropriate value for use in the
4872 /// CaptureTracker::captured() interface.
4873 bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
4874 bool CapturedInRet) {
4875 LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "do { } while (false)
4876 << CapturedInInt << "|Ret " << CapturedInRet << "]\n")do { } while (false);
4877 if (CapturedInMem)
4878 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
4879 if (CapturedInInt)
4880 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
4881 if (CapturedInRet)
4882 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
4883 return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4884 }
4885
4886private:
4887 /// The attributor providing in-flight abstract attributes.
4888 Attributor &A;
4889
4890 /// The abstract attribute currently updated.
4891 AANoCapture &NoCaptureAA;
4892
4893 /// The abstract liveness state.
4894 const AAIsDead &IsDeadAA;
4895
4896 /// The state currently updated.
4897 AANoCapture::StateType &State;
4898
4899 /// Set of potential copies of the tracked value.
4900 SmallSetVector<Value *, 4> &PotentialCopies;
4901
4902 /// Global counter to limit the number of explored uses.
4903 unsigned &RemainingUsesToExplore;
4904};
4905
4906ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
4907 const IRPosition &IRP = getIRPosition();
4908 Value *V = isArgumentPosition() ? IRP.getAssociatedArgument()
4909 : &IRP.getAssociatedValue();
4910 if (!V)
4911 return indicatePessimisticFixpoint();
4912
4913 const Function *F =
4914 isArgumentPosition() ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
4915 assert(F && "Expected a function!")((void)0);
4916 const IRPosition &FnPos = IRPosition::function(*F);
4917 const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos, DepClassTy::NONE);
4918
4919 AANoCapture::StateType T;
4920
4921 // Readonly means we cannot capture through memory.
4922 const auto &FnMemAA =
4923 A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::NONE);
4924 if (FnMemAA.isAssumedReadOnly()) {
4925 T.addKnownBits(NOT_CAPTURED_IN_MEM);
4926 if (FnMemAA.isKnownReadOnly())
4927 addKnownBits(NOT_CAPTURED_IN_MEM);
4928 else
4929 A.recordDependence(FnMemAA, *this, DepClassTy::OPTIONAL);
4930 }
4931
4932 // Make sure all returned values are different than the underlying value.
4933 // TODO: we could do this in a more sophisticated way inside
4934 // AAReturnedValues, e.g., track all values that escape through returns
4935 // directly somehow.
4936 auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
4937 bool SeenConstant = false;
4938 for (auto &It : RVAA.returned_values()) {
4939 if (isa<Constant>(It.first)) {
4940 if (SeenConstant)
4941 return false;
4942 SeenConstant = true;
4943 } else if (!isa<Argument>(It.first) ||
4944 It.first == getAssociatedArgument())
4945 return false;
4946 }
4947 return true;
4948 };
4949
4950 const auto &NoUnwindAA =
4951 A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::OPTIONAL);
4952 if (NoUnwindAA.isAssumedNoUnwind()) {
4953 bool IsVoidTy = F->getReturnType()->isVoidTy();
4954 const AAReturnedValues *RVAA =
4955 IsVoidTy ? nullptr
4956 : &A.getAAFor<AAReturnedValues>(*this, FnPos,
4957
4958 DepClassTy::OPTIONAL);
4959 if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
4960 T.addKnownBits(NOT_CAPTURED_IN_RET);
4961 if (T.isKnown(NOT_CAPTURED_IN_MEM))
4962 return ChangeStatus::UNCHANGED;
4963 if (NoUnwindAA.isKnownNoUnwind() &&
4964 (IsVoidTy || RVAA->getState().isAtFixpoint())) {
4965 addKnownBits(NOT_CAPTURED_IN_RET);
4966 if (isKnown(NOT_CAPTURED_IN_MEM))
4967 return indicateOptimisticFixpoint();
4968 }
4969 }
4970 }
4971
4972 // Use the CaptureTracker interface and logic with the specialized tracker,
4973 // defined in AACaptureUseTracker, that can look at in-flight abstract
4974 // attributes and directly updates the assumed state.
4975 SmallSetVector<Value *, 4> PotentialCopies;
4976 unsigned RemainingUsesToExplore =
4977 getDefaultMaxUsesToExploreForCaptureTracking();
4978 AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
4979 RemainingUsesToExplore);
4980
4981 // Check all potential copies of the associated value until we can assume
4982 // none will be captured or we have to assume at least one might be.
4983 unsigned Idx = 0;
4984 PotentialCopies.insert(V);
4985 while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
4986 Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
4987
4988 AANoCapture::StateType &S = getState();
4989 auto Assumed = S.getAssumed();
4990 S.intersectAssumedBits(T.getAssumed());
4991 if (!isAssumedNoCaptureMaybeReturned())
4992 return indicatePessimisticFixpoint();
4993 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
4994 : ChangeStatus::CHANGED;
4995}
4996
4997/// NoCapture attribute for function arguments.
4998struct AANoCaptureArgument final : AANoCaptureImpl {
4999 AANoCaptureArgument(const IRPosition &IRP, Attributor &A)
5000 : AANoCaptureImpl(IRP, A) {}
5001
5002 /// See AbstractAttribute::trackStatistics()
5003 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture){ static llvm::Statistic NumIRArguments_nocapture = {"attributor"
, "NumIRArguments_nocapture", ("Number of " "arguments" " marked '"
"nocapture" "'")};; ++(NumIRArguments_nocapture); }
}
5004};
5005
5006/// NoCapture attribute for call site arguments.
5007struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
5008 AANoCaptureCallSiteArgument(const IRPosition &IRP, Attributor &A)
5009 : AANoCaptureImpl(IRP, A) {}
5010
5011 /// See AbstractAttribute::initialize(...).
5012 void initialize(Attributor &A) override {
5013 if (Argument *Arg = getAssociatedArgument())
5014 if (Arg->hasByValAttr())
5015 indicateOptimisticFixpoint();
5016 AANoCaptureImpl::initialize(A);
5017 }
5018
5019 /// See AbstractAttribute::updateImpl(...).
5020 ChangeStatus updateImpl(Attributor &A) override {
5021 // TODO: Once we have call site specific value information we can provide
5022 // call site specific liveness information and then it makes
5023 // sense to specialize attributes for call sites arguments instead of
5024 // redirecting requests to the callee argument.
5025 Argument *Arg = getAssociatedArgument();
5026 if (!Arg)
5027 return indicatePessimisticFixpoint();
5028 const IRPosition &ArgPos = IRPosition::argument(*Arg);
5029 auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos, DepClassTy::REQUIRED);
5030 return clampStateAndIndicateChange(getState(), ArgAA.getState());
5031 }
5032
5033 /// See AbstractAttribute::trackStatistics()
5034 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
); }
};
5035};
5036
5037/// NoCapture attribute for floating values.
5038struct AANoCaptureFloating final : AANoCaptureImpl {
5039 AANoCaptureFloating(const IRPosition &IRP, Attributor &A)
5040 : AANoCaptureImpl(IRP, A) {}
5041
5042 /// See AbstractAttribute::trackStatistics()
5043 void trackStatistics() const override {
5044 STATS_DECLTRACK_FLOATING_ATTR(nocapture){ static llvm::Statistic NumIRFloating_nocapture = {"attributor"
, "NumIRFloating_nocapture", ("Number of floating values known to be '"
"nocapture" "'")};; ++(NumIRFloating_nocapture); }
5045 }
5046};
5047
5048/// NoCapture attribute for function return value.
5049struct AANoCaptureReturned final : AANoCaptureImpl {
5050 AANoCaptureReturned(const IRPosition &IRP, Attributor &A)
5051 : AANoCaptureImpl(IRP, A) {
5052 llvm_unreachable("NoCapture is not applicable to function returns!")__builtin_unreachable();
5053 }
5054
5055 /// See AbstractAttribute::initialize(...).
5056 void initialize(Attributor &A) override {
5057 llvm_unreachable("NoCapture is not applicable to function returns!")__builtin_unreachable();
5058 }
5059
5060 /// See AbstractAttribute::updateImpl(...).
5061 ChangeStatus updateImpl(Attributor &A) override {
5062 llvm_unreachable("NoCapture is not applicable to function returns!")__builtin_unreachable();
5063 }
5064
5065 /// See AbstractAttribute::trackStatistics()
5066 void trackStatistics() const override {}
5067};
5068
5069/// NoCapture attribute deduction for a call site return value.
5070struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
5071 AANoCaptureCallSiteReturned(const IRPosition &IRP, Attributor &A)
5072 : AANoCaptureImpl(IRP, A) {}
5073
5074 /// See AbstractAttribute::initialize(...).
5075 void initialize(Attributor &A) override {
5076 const Function *F = getAnchorScope();
5077 // Check what state the associated function can actually capture.
5078 determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
5079 }
5080
5081 /// See AbstractAttribute::trackStatistics()
5082 void trackStatistics() const override {
5083 STATS_DECLTRACK_CSRET_ATTR(nocapture){ static llvm::Statistic NumIRCSReturn_nocapture = {"attributor"
, "NumIRCSReturn_nocapture", ("Number of " "call site returns"
" marked '" "nocapture" "'")};; ++(NumIRCSReturn_nocapture);
}
5084 }
5085};
5086
5087/// ------------------ Value Simplify Attribute ----------------------------
5088
5089bool ValueSimplifyStateType::unionAssumed(Optional<Value *> Other) {
5090 // FIXME: Add a typecast support.
5091 SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5092 SimplifiedAssociatedValue, Other, Ty);
5093 if (SimplifiedAssociatedValue == Optional<Value *>(nullptr))
5094 return false;
5095
5096 LLVM_DEBUG({do { } while (false)
5097 if (SimplifiedAssociatedValue.hasValue())do { } while (false)
5098 dbgs() << "[ValueSimplify] is assumed to be "do { } while (false)
5099 << **SimplifiedAssociatedValue << "\n";do { } while (false)
5100 elsedo { } while (false)
5101 dbgs() << "[ValueSimplify] is assumed to be <none>\n";do { } while (false)
5102 })do { } while (false);
5103 return true;
5104}
5105
5106struct AAValueSimplifyImpl : AAValueSimplify {
5107 AAValueSimplifyImpl(const IRPosition &IRP, Attributor &A)
5108 : AAValueSimplify(IRP, A) {}
5109
5110 /// See AbstractAttribute::initialize(...).
5111 void initialize(Attributor &A) override {
5112 if (getAssociatedValue().getType()->isVoidTy())
5113 indicatePessimisticFixpoint();
5114 if (A.hasSimplificationCallback(getIRPosition()))
5115 indicatePessimisticFixpoint();
5116 }
5117
5118 /// See AbstractAttribute::getAsStr().
5119 const std::string getAsStr() const override {
5120 LLVM_DEBUG({do { } while (false)
5121 errs() << "SAV: " << SimplifiedAssociatedValue << " ";do { } while (false)
5122 if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue)do { } while (false)
5123 errs() << "SAV: " << **SimplifiedAssociatedValue << " ";do { } while (false)
5124 })do { } while (false);
5125 return isValidState() ? (isAtFixpoint() ? "simplified" : "maybe-simple")
5126 : "not-simple";
5127 }
5128
5129 /// See AbstractAttribute::trackStatistics()
5130 void trackStatistics() const override {}
5131
5132 /// See AAValueSimplify::getAssumedSimplifiedValue()
5133 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
5134 return SimplifiedAssociatedValue;
5135 }
5136
5137 /// Return a value we can use as replacement for the associated one, or
5138 /// nullptr if we don't have one that makes sense.
5139 Value *getReplacementValue(Attributor &A) const {
5140 Value *NewV;
5141 NewV = SimplifiedAssociatedValue.hasValue()
5142 ? SimplifiedAssociatedValue.getValue()
5143 : UndefValue::get(getAssociatedType());
5144 if (!NewV)
5145 return nullptr;
5146 NewV = AA::getWithType(*NewV, *getAssociatedType());
5147 if (!NewV || NewV == &getAssociatedValue())
5148 return nullptr;
5149 const Instruction *CtxI = getCtxI();
5150 if (CtxI && !AA::isValidAtPosition(*NewV, *CtxI, A.getInfoCache()))
5151 return nullptr;
5152 if (!CtxI && !AA::isValidInScope(*NewV, getAnchorScope()))
5153 return nullptr;
5154 return NewV;
5155 }
5156
5157 /// Helper function for querying AAValueSimplify and updating candicate.
5158 /// \param IRP The value position we are trying to unify with SimplifiedValue
5159 bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
5160 const IRPosition &IRP, bool Simplify = true) {
5161 bool UsedAssumedInformation = false;
5162 Optional<Value *> QueryingValueSimplified = &IRP.getAssociatedValue();
5163 if (Simplify)
5164 QueryingValueSimplified =
5165 A.getAssumedSimplified(IRP, QueryingAA, UsedAssumedInformation);
5166 return unionAssumed(QueryingValueSimplified);
5167 }
5168
5169 /// Returns a candidate is found or not
5170 template <typename AAType> bool askSimplifiedValueFor(Attributor &A) {
5171 if (!getAssociatedValue().getType()->isIntegerTy())
5172 return false;
5173
5174 // This will also pass the call base context.
5175 const auto &AA =
5176 A.getAAFor<AAType>(*this, getIRPosition(), DepClassTy::NONE);
5177
5178 Optional<ConstantInt *> COpt = AA.getAssumedConstantInt(A);
5179
5180 if (!COpt.hasValue()) {
5181 SimplifiedAssociatedValue = llvm::None;
5182 A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
5183 return true;
5184 }
5185 if (auto *C = COpt.getValue()) {
5186 SimplifiedAssociatedValue = C;
5187 A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
5188 return true;
5189 }
5190 return false;
5191 }
5192
5193 bool askSimplifiedValueForOtherAAs(Attributor &A) {
5194 if (askSimplifiedValueFor<AAValueConstantRange>(A))
5195 return true;
5196 if (askSimplifiedValueFor<AAPotentialValues>(A))
5197 return true;
5198 return false;
5199 }
5200
5201 /// See AbstractAttribute::manifest(...).
5202 ChangeStatus manifest(Attributor &A) override {
5203 ChangeStatus Changed = ChangeStatus::UNCHANGED;
5204 if (getAssociatedValue().user_empty())
5205 return Changed;
5206
5207 if (auto *NewV = getReplacementValue(A)) {
5208 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << getAssociatedValue() << " -> "do { } while (false)
5209 << *NewV << " :: " << *this << "\n")do { } while (false);
5210 if (A.changeValueAfterManifest(getAssociatedValue(), *NewV))
5211 Changed = ChangeStatus::CHANGED;
5212 }
5213
5214 return Changed | AAValueSimplify::manifest(A);
5215 }
5216
5217 /// See AbstractState::indicatePessimisticFixpoint(...).
5218 ChangeStatus indicatePessimisticFixpoint() override {
5219 SimplifiedAssociatedValue = &getAssociatedValue();
5220 return AAValueSimplify::indicatePessimisticFixpoint();
5221 }
5222
5223 static bool handleLoad(Attributor &A, const AbstractAttribute &AA,
5224 LoadInst &L, function_ref<bool(Value &)> Union) {
5225 auto UnionWrapper = [&](Value &V, Value &Obj) {
5226 if (isa<AllocaInst>(Obj))
5227 return Union(V);
5228 if (!AA::isDynamicallyUnique(A, AA, V))
5229 return false;
5230 if (!AA::isValidAtPosition(V, L, A.getInfoCache()))
5231 return false;
5232 return Union(V);
5233 };
5234
5235 Value &Ptr = *L.getPointerOperand();
5236 SmallVector<Value *, 8> Objects;
5237 if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, AA, &L))
5238 return false;
5239
5240 for (Value *Obj : Objects) {
5241 LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n")do { } while (false);
5242 if (isa<UndefValue>(Obj))
5243 continue;
5244 if (isa<ConstantPointerNull>(Obj)) {
5245 // A null pointer access can be undefined but any offset from null may
5246 // be OK. We do not try to optimize the latter.
5247 bool UsedAssumedInformation = false;
5248 if (!NullPointerIsDefined(L.getFunction(),
5249 Ptr.getType()->getPointerAddressSpace()) &&
5250 A.getAssumedSimplified(Ptr, AA, UsedAssumedInformation) == Obj)
5251 continue;
5252 return false;
5253 }
5254 if (!isa<AllocaInst>(Obj) && !isa<GlobalVariable>(Obj))
5255 return false;
5256 Constant *InitialVal = AA::getInitialValueForObj(*Obj, *L.getType());
5257 if (!InitialVal || !Union(*InitialVal))
5258 return false;
5259
5260 LLVM_DEBUG(dbgs() << "Underlying object amenable to load-store "do { } while (false)
5261 "propagation, checking accesses next.\n")do { } while (false);
5262
5263 auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
5264 LLVM_DEBUG(dbgs() << " - visit access " << Acc << "\n")do { } while (false);
5265 if (!Acc.isWrite())
5266 return true;
5267 if (Acc.isWrittenValueYetUndetermined())
5268 return true;
5269 Value *Content = Acc.getWrittenValue();
5270 if (!Content)
5271 return false;
5272 Value *CastedContent =
5273 AA::getWithType(*Content, *AA.getAssociatedType());
5274 if (!CastedContent)
5275 return false;
5276 if (IsExact)
5277 return UnionWrapper(*CastedContent, *Obj);
5278 if (auto *C = dyn_cast<Constant>(CastedContent))
5279 if (C->isNullValue() || C->isAllOnesValue() || isa<UndefValue>(C))
5280 return UnionWrapper(*CastedContent, *Obj);
5281 return false;
5282 };
5283
5284 auto &PI = A.getAAFor<AAPointerInfo>(AA, IRPosition::value(*Obj),
5285 DepClassTy::REQUIRED);
5286 if (!PI.forallInterferingAccesses(L, CheckAccess))
5287 return false;
5288 }
5289 return true;
5290 }
5291};
5292
5293struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
5294 AAValueSimplifyArgument(const IRPosition &IRP, Attributor &A)
5295 : AAValueSimplifyImpl(IRP, A) {}
5296
5297 void initialize(Attributor &A) override {
5298 AAValueSimplifyImpl::initialize(A);
5299 if (!getAnchorScope() || getAnchorScope()->isDeclaration())
5300 indicatePessimisticFixpoint();
5301 if (hasAttr({Attribute::InAlloca, Attribute::Preallocated,
5302 Attribute::StructRet, Attribute::Nest, Attribute::ByVal},
5303 /* IgnoreSubsumingPositions */ true))
5304 indicatePessimisticFixpoint();
5305
5306 // FIXME: This is a hack to prevent us from propagating function poiner in
5307 // the new pass manager CGSCC pass as it creates call edges the
5308 // CallGraphUpdater cannot handle yet.
5309 Value &V = getAssociatedValue();
5310 if (V.getType()->isPointerTy() &&
5311 V.getType()->getPointerElementType()->isFunctionTy() &&
5312 !A.isModulePass())
5313 indicatePessimisticFixpoint();
5314 }
5315
5316 /// See AbstractAttribute::updateImpl(...).
5317 ChangeStatus updateImpl(Attributor &A) override {
5318 // Byval is only replacable if it is readonly otherwise we would write into
5319 // the replaced value and not the copy that byval creates implicitly.
5320 Argument *Arg = getAssociatedArgument();
5321 if (Arg->hasByValAttr()) {
5322 // TODO: We probably need to verify synchronization is not an issue, e.g.,
5323 // there is no race by not copying a constant byval.
5324 const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(),
5325 DepClassTy::REQUIRED);
5326 if (!MemAA.isAssumedReadOnly())
5327 return indicatePessimisticFixpoint();
5328 }
5329
5330 auto Before = SimplifiedAssociatedValue;
5331
5332 auto PredForCallSite = [&](AbstractCallSite ACS) {
5333 const IRPosition &ACSArgPos =
5334 IRPosition::callsite_argument(ACS, getCallSiteArgNo());
5335 // Check if a coresponding argument was found or if it is on not
5336 // associated (which can happen for callback calls).
5337 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
5338 return false;
5339
5340 // Simplify the argument operand explicitly and check if the result is
5341 // valid in the current scope. This avoids refering to simplified values
5342 // in other functions, e.g., we don't want to say a an argument in a
5343 // static function is actually an argument in a different function.
5344 bool UsedAssumedInformation = false;
5345 Optional<Constant *> SimpleArgOp =
5346 A.getAssumedConstant(ACSArgPos, *this, UsedAssumedInformation);
5347 if (!SimpleArgOp.hasValue())
5348 return true;
5349 if (!SimpleArgOp.getValue())
5350 return false;
5351 if (!AA::isDynamicallyUnique(A, *this, **SimpleArgOp))
5352 return false;
5353 return unionAssumed(*SimpleArgOp);
5354 };
5355
5356 // Generate a answer specific to a call site context.
5357 bool Success;
5358 bool AllCallSitesKnown;
5359 if (hasCallBaseContext() &&
5360 getCallBaseContext()->getCalledFunction() == Arg->getParent())
5361 Success = PredForCallSite(
5362 AbstractCallSite(&getCallBaseContext()->getCalledOperandUse()));
5363 else
5364 Success = A.checkForAllCallSites(PredForCallSite, *this, true,
5365 AllCallSitesKnown);
5366
5367 if (!Success)
5368 if (!askSimplifiedValueForOtherAAs(A))
5369 return indicatePessimisticFixpoint();
5370
5371 // If a candicate was found in this update, return CHANGED.
5372 return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5373 : ChangeStatus ::CHANGED;
5374 }
5375
5376 /// See AbstractAttribute::trackStatistics()
5377 void trackStatistics() const override {
5378 STATS_DECLTRACK_ARG_ATTR(value_simplify){ static llvm::Statistic NumIRArguments_value_simplify = {"attributor"
, "NumIRArguments_value_simplify", ("Number of " "arguments" " marked '"
"value_simplify" "'")};; ++(NumIRArguments_value_simplify); }
5379 }
5380};
5381
5382struct AAValueSimplifyReturned : AAValueSimplifyImpl {
5383 AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A)
5384 : AAValueSimplifyImpl(IRP, A) {}
5385
5386 /// See AAValueSimplify::getAssumedSimplifiedValue()
5387 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
5388 if (!isValidState())
5389 return nullptr;
5390 return SimplifiedAssociatedValue;
5391 }
5392
5393 /// See AbstractAttribute::updateImpl(...).
5394 ChangeStatus updateImpl(Attributor &A) override {
5395 auto Before = SimplifiedAssociatedValue;
5396
5397 auto PredForReturned = [&](Value &V) {
5398 return checkAndUpdate(A, *this,
5399 IRPosition::value(V, getCallBaseContext()));
5400 };
5401
5402 if (!A.checkForAllReturnedValues(PredForReturned, *this))
5403 if (!askSimplifiedValueForOtherAAs(A))
5404 return indicatePessimisticFixpoint();
5405
5406 // If a candicate was found in this update, return CHANGED.
5407 return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5408 : ChangeStatus ::CHANGED;
5409 }
5410
5411 ChangeStatus manifest(Attributor &A) override {
5412 ChangeStatus Changed = ChangeStatus::UNCHANGED;
5413
5414 if (auto *NewV = getReplacementValue(A)) {
5415 auto PredForReturned =
5416 [&](Value &, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
5417 for (ReturnInst *RI : RetInsts) {
5418 Value *ReturnedVal = RI->getReturnValue();
5419 if (ReturnedVal == NewV || isa<UndefValue>(ReturnedVal))
5420 return true;
5421 assert(RI->getFunction() == getAnchorScope() &&((void)0)
5422 "ReturnInst in wrong function!")((void)0);
5423 LLVM_DEBUG(dbgs()do { } while (false)
5424 << "[ValueSimplify] " << *ReturnedVal << " -> "do { } while (false)
5425 << *NewV << " in " << *RI << " :: " << *this << "\n")do { } while (false);
5426 if (A.changeUseAfterManifest(RI->getOperandUse(0), *NewV))
5427 Changed = ChangeStatus::CHANGED;
5428 }
5429 return true;
5430 };
5431 A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this);
5432 }
5433
5434 return Changed | AAValueSimplify::manifest(A);
5435 }
5436
5437 /// See AbstractAttribute::trackStatistics()
5438 void trackStatistics() const override {
5439 STATS_DECLTRACK_FNRET_ATTR(value_simplify){ static llvm::Statistic NumIRFunctionReturn_value_simplify =
{"attributor", "NumIRFunctionReturn_value_simplify", ("Number of "
"function returns" " marked '" "value_simplify" "'")};; ++(NumIRFunctionReturn_value_simplify
); }
5440 }
5441};
5442
5443struct AAValueSimplifyFloating : AAValueSimplifyImpl {
5444 AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A)
5445 : AAValueSimplifyImpl(IRP, A) {}
5446
5447 /// See AbstractAttribute::initialize(...).
5448 void initialize(Attributor &A) override {
5449 AAValueSimplifyImpl::initialize(A);
5450 Value &V = getAnchorValue();
5451
5452 // TODO: add other stuffs
5453 if (isa<Constant>(V))
5454 indicatePessimisticFixpoint();
5455 }
5456
5457 /// Check if \p Cmp is a comparison we can simplify.
5458 ///
5459 /// We handle multiple cases, one in which at least one operand is an
5460 /// (assumed) nullptr. If so, try to simplify it using AANonNull on the other
5461 /// operand. Return true if successful, in that case SimplifiedAssociatedValue
5462 /// will be updated.
5463 bool handleCmp(Attributor &A, CmpInst &Cmp) {
5464 auto Union = [&](Value &V) {
5465 SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5466 SimplifiedAssociatedValue, &V, V.getType());
5467 return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5468 };
5469
5470 Value *LHS = Cmp.getOperand(0);
5471 Value *RHS = Cmp.getOperand(1);
5472
5473 // Simplify the operands first.
5474 bool UsedAssumedInformation = false;
5475 const auto &SimplifiedLHS =
5476 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
5477 *this, UsedAssumedInformation);
5478 if (!SimplifiedLHS.hasValue())
5479 return true;
5480 if (!SimplifiedLHS.getValue())
5481 return false;
5482 LHS = *SimplifiedLHS;
5483
5484 const auto &SimplifiedRHS =
5485 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
5486 *this, UsedAssumedInformation);
5487 if (!SimplifiedRHS.hasValue())
5488 return true;
5489 if (!SimplifiedRHS.getValue())
5490 return false;
5491 RHS = *SimplifiedRHS;
5492
5493 LLVMContext &Ctx = Cmp.getContext();
5494 // Handle the trivial case first in which we don't even need to think about
5495 // null or non-null.
5496 if (LHS == RHS && (Cmp.isTrueWhenEqual() || Cmp.isFalseWhenEqual())) {
5497 Constant *NewVal =
5498 ConstantInt::get(Type::getInt1Ty(Ctx), Cmp.isTrueWhenEqual());
5499 if (!Union(*NewVal))
5500 return false;
5501 if (!UsedAssumedInformation)
5502 indicateOptimisticFixpoint();
5503 return true;
5504 }
5505
5506 // From now on we only handle equalities (==, !=).
5507 ICmpInst *ICmp = dyn_cast<ICmpInst>(&Cmp);
5508 if (!ICmp || !ICmp->isEquality())
5509 return false;
5510
5511 bool LHSIsNull = isa<ConstantPointerNull>(LHS);
5512 bool RHSIsNull = isa<ConstantPointerNull>(RHS);
5513 if (!LHSIsNull && !RHSIsNull)
5514 return false;
5515
5516 // Left is the nullptr ==/!= non-nullptr case. We'll use AANonNull on the
5517 // non-nullptr operand and if we assume it's non-null we can conclude the
5518 // result of the comparison.
5519 assert((LHSIsNull || RHSIsNull) &&((void)0)
5520 "Expected nullptr versus non-nullptr comparison at this point")((void)0);
5521
5522 // The index is the operand that we assume is not null.
5523 unsigned PtrIdx = LHSIsNull;
5524 auto &PtrNonNullAA = A.getAAFor<AANonNull>(
5525 *this, IRPosition::value(*ICmp->getOperand(PtrIdx)),
5526 DepClassTy::REQUIRED);
5527 if (!PtrNonNullAA.isAssumedNonNull())
5528 return false;
5529 UsedAssumedInformation |= !PtrNonNullAA.isKnownNonNull();
5530
5531 // The new value depends on the predicate, true for != and false for ==.
5532 Constant *NewVal = ConstantInt::get(
5533 Type::getInt1Ty(Ctx), ICmp->getPredicate() == CmpInst::ICMP_NE);
5534 if (!Union(*NewVal))
5535 return false;
5536
5537 if (!UsedAssumedInformation)
5538 indicateOptimisticFixpoint();
5539
5540 return true;
5541 }
5542
5543 bool updateWithLoad(Attributor &A, LoadInst &L) {
5544 auto Union = [&](Value &V) {
5545 SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5546 SimplifiedAssociatedValue, &V, L.getType());
5547 return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5548 };
5549 return handleLoad(A, *this, L, Union);
5550 }
5551
5552 /// Use the generic, non-optimistic InstSimplfy functionality if we managed to
5553 /// simplify any operand of the instruction \p I. Return true if successful,
5554 /// in that case SimplifiedAssociatedValue will be updated.
5555 bool handleGenericInst(Attributor &A, Instruction &I) {
5556 bool SomeSimplified = false;
5557 bool UsedAssumedInformation = false;
5558
5559 SmallVector<Value *, 8> NewOps(I.getNumOperands());
5560 int Idx = 0;
5561 for (Value *Op : I.operands()) {
5562 const auto &SimplifiedOp =
5563 A.getAssumedSimplified(IRPosition::value(*Op, getCallBaseContext()),
5564 *this, UsedAssumedInformation);
5565 // If we are not sure about any operand we are not sure about the entire
5566 // instruction, we'll wait.
5567 if (!SimplifiedOp.hasValue())
5568 return true;
5569
5570 if (SimplifiedOp.getValue())
5571 NewOps[Idx] = SimplifiedOp.getValue();
5572 else
5573 NewOps[Idx] = Op;
5574
5575 SomeSimplified |= (NewOps[Idx] != Op);
5576 ++Idx;
5577 }
5578
5579 // We won't bother with the InstSimplify interface if we didn't simplify any
5580 // operand ourselves.
5581 if (!SomeSimplified)
5582 return false;
5583
5584 InformationCache &InfoCache = A.getInfoCache();
5585 Function *F = I.getFunction();
5586 const auto *DT =
5587 InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
5588 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5589 auto *AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
5590 OptimizationRemarkEmitter *ORE = nullptr;
5591
5592 const DataLayout &DL = I.getModule()->getDataLayout();
5593 SimplifyQuery Q(DL, TLI, DT, AC, &I);
5594 if (Value *SimplifiedI =
5595 SimplifyInstructionWithOperands(&I, NewOps, Q, ORE)) {
5596 SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5597 SimplifiedAssociatedValue, SimplifiedI, I.getType());
5598 return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5599 }
5600 return false;
5601 }
5602
5603 /// See AbstractAttribute::updateImpl(...).
5604 ChangeStatus updateImpl(Attributor &A) override {
5605 auto Before = SimplifiedAssociatedValue;
5606
5607 auto VisitValueCB = [&](Value &V, const Instruction *CtxI, bool &,
5608 bool Stripped) -> bool {
5609 auto &AA = A.getAAFor<AAValueSimplify>(
5610 *this, IRPosition::value(V, getCallBaseContext()),
5611 DepClassTy::REQUIRED);
5612 if (!Stripped && this == &AA) {
5613
5614 if (auto *I = dyn_cast<Instruction>(&V)) {
5615 if (auto *LI = dyn_cast<LoadInst>(&V))
5616 if (updateWithLoad(A, *LI))
5617 return true;
5618 if (auto *Cmp = dyn_cast<CmpInst>(&V))
5619 if (handleCmp(A, *Cmp))
5620 return true;
5621 if (handleGenericInst(A, *I))
5622 return true;
5623 }
5624 // TODO: Look the instruction and check recursively.
5625
5626 LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << Vdo { } while (false)
5627 << "\n")do { } while (false);
5628 return false;
5629 }
5630 return checkAndUpdate(A, *this,
5631 IRPosition::value(V, getCallBaseContext()));
5632 };
5633
5634 bool Dummy = false;
5635 if (!genericValueTraversal<bool>(A, getIRPosition(), *this, Dummy,
5636 VisitValueCB, getCtxI(),
5637 /* UseValueSimplify */ false))
5638 if (!askSimplifiedValueForOtherAAs(A))
5639 return indicatePessimisticFixpoint();
5640
5641 // If a candicate was found in this update, return CHANGED.
5642 return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5643 : ChangeStatus ::CHANGED;
5644 }
5645
5646 /// See AbstractAttribute::trackStatistics()
5647 void trackStatistics() const override {
5648 STATS_DECLTRACK_FLOATING_ATTR(value_simplify){ static llvm::Statistic NumIRFloating_value_simplify = {"attributor"
, "NumIRFloating_value_simplify", ("Number of floating values known to be '"
"value_simplify" "'")};; ++(NumIRFloating_value_simplify); }
5649 }
5650};
5651
5652struct AAValueSimplifyFunction : AAValueSimplifyImpl {
5653 AAValueSimplifyFunction(const IRPosition &IRP, Attributor &A)
5654 : AAValueSimplifyImpl(IRP, A) {}
5655
5656 /// See AbstractAttribute::initialize(...).
5657 void initialize(Attributor &A) override {
5658 SimplifiedAssociatedValue = nullptr;
5659 indicateOptimisticFixpoint();
5660 }
5661 /// See AbstractAttribute::initialize(...).
5662 ChangeStatus updateImpl(Attributor &A) override {
5663 llvm_unreachable(__builtin_unreachable()
5664 "AAValueSimplify(Function|CallSite)::updateImpl will not be called")__builtin_unreachable();
5665 }
5666 /// See AbstractAttribute::trackStatistics()
5667 void trackStatistics() const override {
5668 STATS_DECLTRACK_FN_ATTR(value_simplify){ static llvm::Statistic NumIRFunction_value_simplify = {"attributor"
, "NumIRFunction_value_simplify", ("Number of " "functions" " marked '"
"value_simplify" "'")};; ++(NumIRFunction_value_simplify); }
5669 }
5670};
5671
5672struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
5673 AAValueSimplifyCallSite(const IRPosition &IRP, Attributor &A)
5674 : AAValueSimplifyFunction(IRP, A) {}
5675 /// See AbstractAttribute::trackStatistics()
5676 void trackStatistics() const override {
5677 STATS_DECLTRACK_CS_ATTR(value_simplify){ static llvm::Statistic NumIRCS_value_simplify = {"attributor"
, "NumIRCS_value_simplify", ("Number of " "call site" " marked '"
"value_simplify" "'")};; ++(NumIRCS_value_simplify); }
5678 }
5679};
5680
5681struct AAValueSimplifyCallSiteReturned : AAValueSimplifyImpl {
5682 AAValueSimplifyCallSiteReturned(const IRPosition &IRP, Attributor &A)
5683 : AAValueSimplifyImpl(IRP, A) {}
5684
5685 void initialize(Attributor &A) override {
5686 AAValueSimplifyImpl::initialize(A);
5687 if (!getAssociatedFunction())
5688 indicatePessimisticFixpoint();
5689 }
5690
5691 /// See AbstractAttribute::updateImpl(...).
5692 ChangeStatus updateImpl(Attributor &A) override {
5693 auto Before = SimplifiedAssociatedValue;
5694 auto &RetAA = A.getAAFor<AAReturnedValues>(
5695 *this, IRPosition::function(*getAssociatedFunction()),
5696 DepClassTy::REQUIRED);
5697 auto PredForReturned =
5698 [&](Value &RetVal, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
5699 bool UsedAssumedInformation = false;
5700 Optional<Value *> CSRetVal = A.translateArgumentToCallSiteContent(
5701 &RetVal, *cast<CallBase>(getCtxI()), *this,
5702 UsedAssumedInformation);
5703 SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
5704 SimplifiedAssociatedValue, CSRetVal, getAssociatedType());
5705 return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
5706 };
5707 if (!RetAA.checkForAllReturnedValuesAndReturnInsts(PredForReturned))
5708 if (!askSimplifiedValueForOtherAAs(A))
5709 return indicatePessimisticFixpoint();
5710 return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
5711 : ChangeStatus ::CHANGED;
5712 }
5713
5714 void trackStatistics() const override {
5715 STATS_DECLTRACK_CSRET_ATTR(value_simplify){ static llvm::Statistic NumIRCSReturn_value_simplify = {"attributor"
, "NumIRCSReturn_value_simplify", ("Number of " "call site returns"
" marked '" "value_simplify" "'")};; ++(NumIRCSReturn_value_simplify
); }
5716 }
5717};
5718
5719struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
5720 AAValueSimplifyCallSiteArgument(const IRPosition &IRP, Attributor &A)
5721 : AAValueSimplifyFloating(IRP, A) {}
5722
5723 /// See AbstractAttribute::manifest(...).
5724 ChangeStatus manifest(Attributor &A) override {
5725 ChangeStatus Changed = ChangeStatus::UNCHANGED;
5726
5727 if (auto *NewV = getReplacementValue(A)) {
5728 Use &U = cast<CallBase>(&getAnchorValue())
5729 ->getArgOperandUse(getCallSiteArgNo());
5730 if (A.changeUseAfterManifest(U, *NewV))
5731 Changed = ChangeStatus::CHANGED;
5732 }
5733
5734 return Changed | AAValueSimplify::manifest(A);
5735 }
5736
5737 void trackStatistics() const override {
5738 STATS_DECLTRACK_CSARG_ATTR(value_simplify){ static llvm::Statistic NumIRCSArguments_value_simplify = {"attributor"
, "NumIRCSArguments_value_simplify", ("Number of " "call site arguments"
" marked '" "value_simplify" "'")};; ++(NumIRCSArguments_value_simplify
); }
5739 }
5740};
5741
5742/// ----------------------- Heap-To-Stack Conversion ---------------------------
5743struct AAHeapToStackFunction final : public AAHeapToStack {
5744
5745 struct AllocationInfo {
5746 /// The call that allocates the memory.
5747 CallBase *const CB;
5748
5749 /// The kind of allocation.
5750 const enum class AllocationKind {
5751 MALLOC,
5752 CALLOC,
5753 ALIGNED_ALLOC,
5754 } Kind;
5755
5756 /// The library function id for the allocation.
5757 LibFunc LibraryFunctionId = NotLibFunc;
5758
5759 /// The status wrt. a rewrite.
5760 enum {
5761 STACK_DUE_TO_USE,
5762 STACK_DUE_TO_FREE,
5763 INVALID,
5764 } Status = STACK_DUE_TO_USE;
5765
5766 /// Flag to indicate if we encountered a use that might free this allocation
5767 /// but which is not in the deallocation infos.
5768 bool HasPotentiallyFreeingUnknownUses = false;
5769
5770 /// The set of free calls that use this allocation.
5771 SmallPtrSet<CallBase *, 1> PotentialFreeCalls{};
5772 };
5773
5774 struct DeallocationInfo {
5775 /// The call that deallocates the memory.
5776 CallBase *const CB;
5777
5778 /// Flag to indicate if we don't know all objects this deallocation might
5779 /// free.
5780 bool MightFreeUnknownObjects = false;
5781
5782 /// The set of allocation calls that are potentially freed.
5783 SmallPtrSet<CallBase *, 1> PotentialAllocationCalls{};
5784 };
5785
5786 AAHeapToStackFunction(const IRPosition &IRP, Attributor &A)
5787 : AAHeapToStack(IRP, A) {}
5788
5789 ~AAHeapToStackFunction() {
5790 // Ensure we call the destructor so we release any memory allocated in the
5791 // sets.
5792 for (auto &It : AllocationInfos)
5793 It.getSecond()->~AllocationInfo();
5794 for (auto &It : DeallocationInfos)
5795 It.getSecond()->~DeallocationInfo();
5796 }
5797
5798 void initialize(Attributor &A) override {
5799 AAHeapToStack::initialize(A);
5800
5801 const Function *F = getAnchorScope();
5802 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5803
5804 auto AllocationIdentifierCB = [&](Instruction &I) {
5805 CallBase *CB = dyn_cast<CallBase>(&I);
5806 if (!CB)
5807 return true;
5808 if (isFreeCall(CB, TLI)) {
5809 DeallocationInfos[CB] = new (A.Allocator) DeallocationInfo{CB};
5810 return true;
5811 }
5812 bool IsMalloc = isMallocLikeFn(CB, TLI);
5813 bool IsAlignedAllocLike = !IsMalloc && isAlignedAllocLikeFn(CB, TLI);
5814 bool IsCalloc =
5815 !IsMalloc && !IsAlignedAllocLike && isCallocLikeFn(CB, TLI);
5816 if (!IsMalloc && !IsAlignedAllocLike && !IsCalloc)
5817 return true;
5818 auto Kind =
5819 IsMalloc ? AllocationInfo::AllocationKind::MALLOC
5820 : (IsCalloc ? AllocationInfo::AllocationKind::CALLOC
5821 : AllocationInfo::AllocationKind::ALIGNED_ALLOC);
5822
5823 AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB, Kind};
5824 AllocationInfos[CB] = AI;
5825 TLI->getLibFunc(*CB, AI->LibraryFunctionId);
5826 return true;
5827 };
5828
5829 bool UsedAssumedInformation = false;
5830 bool Success = A.checkForAllCallLikeInstructions(
5831 AllocationIdentifierCB, *this, UsedAssumedInformation,
5832 /* CheckBBLivenessOnly */ false,
5833 /* CheckPotentiallyDead */ true);
5834 (void)Success;
5835 assert(Success && "Did not expect the call base visit callback to fail!")((void)0);
5836 }
5837
5838 const std::string getAsStr() const override {
5839 unsigned NumH2SMallocs = 0, NumInvalidMallocs = 0;
5840 for (const auto &It : AllocationInfos) {
5841 if (It.second->Status == AllocationInfo::INVALID)
5842 ++NumInvalidMallocs;
5843 else
5844 ++NumH2SMallocs;
5845 }
5846 return "[H2S] Mallocs Good/Bad: " + std::to_string(NumH2SMallocs) + "/" +
5847 std::to_string(NumInvalidMallocs);
5848 }
5849
5850 /// See AbstractAttribute::trackStatistics().
5851 void trackStatistics() const override {
5852 STATS_DECL(static llvm::Statistic NumIRFunction_MallocCalls = {"attributor"
, "NumIRFunction_MallocCalls", "Number of malloc/calloc/aligned_alloc calls converted to allocas"
};;
5853 MallocCalls, Function,static llvm::Statistic NumIRFunction_MallocCalls = {"attributor"
, "NumIRFunction_MallocCalls", "Number of malloc/calloc/aligned_alloc calls converted to allocas"
};;
5854 "Number of malloc/calloc/aligned_alloc calls converted to allocas")static llvm::Statistic NumIRFunction_MallocCalls = {"attributor"
, "NumIRFunction_MallocCalls", "Number of malloc/calloc/aligned_alloc calls converted to allocas"
};;
;
5855 for (auto &It : AllocationInfos)
5856 if (It.second->Status != AllocationInfo::INVALID)
5857 ++BUILD_STAT_NAME(MallocCalls, Function)NumIRFunction_MallocCalls;
5858 }
5859
5860 bool isAssumedHeapToStack(const CallBase &CB) const override {
5861 if (isValidState())
5862 if (AllocationInfo *AI = AllocationInfos.lookup(&CB))
5863 return AI->Status != AllocationInfo::INVALID;
5864 return false;
5865 }
5866
5867 bool isAssumedHeapToStackRemovedFree(CallBase &CB) const override {
5868 if (!isValidState())
5869 return false;
5870
5871 for (auto &It : AllocationInfos) {
5872 AllocationInfo &AI = *It.second;
5873 if (AI.Status == AllocationInfo::INVALID)
5874 continue;
5875
5876 if (AI.PotentialFreeCalls.count(&CB))
5877 return true;
5878 }
5879
5880 return false;
5881 }
5882
5883 ChangeStatus manifest(Attributor &A) override {
5884 assert(getState().isValidState() &&((void)0)
5885 "Attempted to manifest an invalid state!")((void)0);
5886
5887 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
5888 Function *F = getAnchorScope();
5889 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5890
5891 for (auto &It : AllocationInfos) {
5892 AllocationInfo &AI = *It.second;
5893 if (AI.Status == AllocationInfo::INVALID)
5894 continue;
5895
5896 for (CallBase *FreeCall : AI.PotentialFreeCalls) {
5897 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n")do { } while (false);
5898 A.deleteAfterManifest(*FreeCall);
5899 HasChanged = ChangeStatus::CHANGED;
5900 }
5901
5902 LLVM_DEBUG(dbgs() << "H2S: Removing malloc-like call: " << *AI.CBdo { } while (false)
5903 << "\n")do { } while (false);
5904
5905 auto Remark = [&](OptimizationRemark OR) {
5906 LibFunc IsAllocShared;
5907 if (TLI->getLibFunc(*AI.CB, IsAllocShared))
5908 if (IsAllocShared == LibFunc___kmpc_alloc_shared)
5909 return OR << "Moving globalized variable to the stack.";
5910 return OR << "Moving memory allocation from the heap to the stack.";
5911 };
5912 if (AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
5913 A.emitRemark<OptimizationRemark>(AI.CB, "OMP110", Remark);
5914 else
5915 A.emitRemark<OptimizationRemark>(AI.CB, "HeapToStack", Remark);
5916
5917 Value *Size;
5918 Optional<APInt> SizeAPI = getSize(A, *this, AI);
5919 if (SizeAPI.hasValue()) {
5920 Size = ConstantInt::get(AI.CB->getContext(), *SizeAPI);
5921 } else if (AI.Kind == AllocationInfo::AllocationKind::CALLOC) {
5922 auto *Num = AI.CB->getOperand(0);
5923 auto *SizeT = AI.CB->getOperand(1);
5924 IRBuilder<> B(AI.CB);
5925 Size = B.CreateMul(Num, SizeT, "h2s.calloc.size");
5926 } else if (AI.Kind == AllocationInfo::AllocationKind::ALIGNED_ALLOC) {
5927 Size = AI.CB->getOperand(1);
5928 } else {
5929 Size = AI.CB->getOperand(0);
5930 }
5931
5932 Align Alignment(1);
5933 if (AI.Kind == AllocationInfo::AllocationKind::ALIGNED_ALLOC) {
5934 Optional<APInt> AlignmentAPI =
5935 getAPInt(A, *this, *AI.CB->getArgOperand(0));
5936 assert(AlignmentAPI.hasValue() &&((void)0)
5937 "Expected an alignment during manifest!")((void)0);
5938 Alignment =
5939 max(Alignment, MaybeAlign(AlignmentAPI.getValue().getZExtValue()));
5940 }
5941
5942 unsigned AS = cast<PointerType>(AI.CB->getType())->getAddressSpace();
5943 Instruction *Alloca =
5944 new AllocaInst(Type::getInt8Ty(F->getContext()), AS, Size, Alignment,
5945 "", AI.CB->getNextNode());
5946
5947 if (Alloca->getType() != AI.CB->getType())
5948 Alloca = new BitCastInst(Alloca, AI.CB->getType(), "malloc_bc",
5949 Alloca->getNextNode());
5950
5951 A.changeValueAfterManifest(*AI.CB, *Alloca);
5952
5953 if (auto *II = dyn_cast<InvokeInst>(AI.CB)) {
5954 auto *NBB = II->getNormalDest();
5955 BranchInst::Create(NBB, AI.CB->getParent());
5956 A.deleteAfterManifest(*AI.CB);
5957 } else {
5958 A.deleteAfterManifest(*AI.CB);
5959 }
5960
5961 // Zero out the allocated memory if it was a calloc.
5962 if (AI.Kind == AllocationInfo::AllocationKind::CALLOC) {
5963 auto *BI = new BitCastInst(Alloca, AI.CB->getType(), "calloc_bc",
5964 Alloca->getNextNode());
5965 Value *Ops[] = {
5966 BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size,
5967 ConstantInt::get(Type::getInt1Ty(F->getContext()), false)};
5968
5969 Type *Tys[] = {BI->getType(), AI.CB->getOperand(0)->getType()};
5970 Module *M = F->getParent();
5971 Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
5972 CallInst::Create(Fn, Ops, "", BI->getNextNode());
5973 }
5974 HasChanged = ChangeStatus::CHANGED;
5975 }
5976
5977 return HasChanged;
5978 }
5979
5980 Optional<APInt> getAPInt(Attributor &A, const AbstractAttribute &AA,
5981 Value &V) {
5982 bool UsedAssumedInformation = false;
5983 Optional<Constant *> SimpleV =
5984 A.getAssumedConstant(V, AA, UsedAssumedInformation);
5985 if (!SimpleV.hasValue())
5986 return APInt(64, 0);
5987 if (auto *CI = dyn_cast_or_null<ConstantInt>(SimpleV.getValue()))
5988 return CI->getValue();
5989 return llvm::None;
5990 }
5991
5992 Optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA,
5993 AllocationInfo &AI) {
5994
5995 if (AI.Kind == AllocationInfo::AllocationKind::MALLOC)
5996 return getAPInt(A, AA, *AI.CB->getArgOperand(0));
5997
5998 if (AI.Kind == AllocationInfo::AllocationKind::ALIGNED_ALLOC)
5999 // Only if the alignment is also constant we return a size.
6000 return getAPInt(A, AA, *AI.CB->getArgOperand(0)).hasValue()
6001 ? getAPInt(A, AA, *AI.CB->getArgOperand(1))
6002 : llvm::None;
6003
6004 assert(AI.Kind == AllocationInfo::AllocationKind::CALLOC &&((void)0)
6005 "Expected only callocs are left")((void)0);
6006 Optional<APInt> Num = getAPInt(A, AA, *AI.CB->getArgOperand(0));
6007 Optional<APInt> Size = getAPInt(A, AA, *AI.CB->getArgOperand(1));
6008 if (!Num.hasValue() || !Size.hasValue())
6009 return llvm::None;
6010 bool Overflow = false;
6011 Size = Size.getValue().umul_ov(Num.getValue(), Overflow);
6012 return Overflow ? llvm::None : Size;
6013 }
6014
6015 /// Collection of all malloc-like calls in a function with associated
6016 /// information.
6017 DenseMap<CallBase *, AllocationInfo *> AllocationInfos;
6018
6019 /// Collection of all free-like calls in a function with associated
6020 /// information.
6021 DenseMap<CallBase *, DeallocationInfo *> DeallocationInfos;
6022
6023 ChangeStatus updateImpl(Attributor &A) override;
6024};
6025
6026ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) {
6027 ChangeStatus Changed = ChangeStatus::UNCHANGED;
6028 const Function *F = getAnchorScope();
6029
6030 const auto &LivenessAA =
6031 A.getAAFor<AAIsDead>(*this, IRPosition::function(*F), DepClassTy::NONE);
6032
6033 MustBeExecutedContextExplorer &Explorer =
6034 A.getInfoCache().getMustBeExecutedContextExplorer();
6035
6036 bool StackIsAccessibleByOtherThreads =
6037 A.getInfoCache().stackIsAccessibleByOtherThreads();
6038
6039 // Flag to ensure we update our deallocation information at most once per
6040 // updateImpl call and only if we use the free check reasoning.
6041 bool HasUpdatedFrees = false;
6042
6043 auto UpdateFrees = [&]() {
6044 HasUpdatedFrees = true;
6045
6046 for (auto &It : DeallocationInfos) {
6047 DeallocationInfo &DI = *It.second;
6048 // For now we cannot use deallocations that have unknown inputs, skip
6049 // them.
6050 if (DI.MightFreeUnknownObjects)
6051 continue;
6052
6053 // No need to analyze dead calls, ignore them instead.
6054 bool UsedAssumedInformation = false;
6055 if (A.isAssumedDead(*DI.CB, this, &LivenessAA, UsedAssumedInformation,
6056 /* CheckBBLivenessOnly */ true))
6057 continue;
6058
6059 // Use the optimistic version to get the freed objects, ignoring dead
6060 // branches etc.
6061 SmallVector<Value *, 8> Objects;
6062 if (!AA::getAssumedUnderlyingObjects(A, *DI.CB->getArgOperand(0), Objects,
6063 *this, DI.CB)) {
6064 LLVM_DEBUG(do { } while (false)
6065 dbgs()do { } while (false)
6066 << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n")do { } while (false);
6067 DI.MightFreeUnknownObjects = true;
6068 continue;
6069 }
6070
6071 // Check each object explicitly.
6072 for (auto *Obj : Objects) {
6073 // Free of null and undef can be ignored as no-ops (or UB in the latter
6074 // case).
6075 if (isa<ConstantPointerNull>(Obj) || isa<UndefValue>(Obj))
6076 continue;
6077
6078 CallBase *ObjCB = dyn_cast<CallBase>(Obj);
6079 if (!ObjCB) {
6080 LLVM_DEBUG(dbgs()do { } while (false)
6081 << "[H2S] Free of a non-call object: " << *Obj << "\n")do { } while (false);
6082 DI.MightFreeUnknownObjects = true;
6083 continue;
6084 }
6085
6086 AllocationInfo *AI = AllocationInfos.lookup(ObjCB);
6087 if (!AI) {
6088 LLVM_DEBUG(dbgs() << "[H2S] Free of a non-allocation object: " << *Objdo { } while (false)
6089 << "\n")do { } while (false);
6090 DI.MightFreeUnknownObjects = true;
6091 continue;
6092 }
6093
6094 DI.PotentialAllocationCalls.insert(ObjCB);
6095 }
6096 }
6097 };
6098
6099 auto FreeCheck = [&](AllocationInfo &AI) {
6100 // If the stack is not accessible by other threads, the "must-free" logic
6101 // doesn't apply as the pointer could be shared and needs to be places in
6102 // "shareable" memory.
6103 if (!StackIsAccessibleByOtherThreads) {
6104 auto &NoSyncAA =
6105 A.getAAFor<AANoSync>(*this, getIRPosition(), DepClassTy::OPTIONAL);
6106 if (!NoSyncAA.isAssumedNoSync()) {
6107 LLVM_DEBUG(do { } while (false)
6108 dbgs() << "[H2S] found an escaping use, stack is not accessible by "do { } while (false)
6109 "other threads and function is not nosync:\n")do { } while (false);
6110 return false;
6111 }
6112 }
6113 if (!HasUpdatedFrees)
6114 UpdateFrees();
6115
6116 // TODO: Allow multi exit functions that have different free calls.
6117 if (AI.PotentialFreeCalls.size() != 1) {
6118 LLVM_DEBUG(dbgs() << "[H2S] did not find one free call but "do { } while (false)
6119 << AI.PotentialFreeCalls.size() << "\n")do { } while (false);
6120 return false;
6121 }
6122 CallBase *UniqueFree = *AI.PotentialFreeCalls.begin();
6123 DeallocationInfo *DI = DeallocationInfos.lookup(UniqueFree);
6124 if (!DI) {
6125 LLVM_DEBUG(do { } while (false)
6126 dbgs() << "[H2S] unique free call was not known as deallocation call "do { } while (false)
6127 << *UniqueFree << "\n")do { } while (false);
6128 return false;
6129 }
6130 if (DI->MightFreeUnknownObjects) {
6131 LLVM_DEBUG(do { } while (false)
6132 dbgs() << "[H2S] unique free call might free unknown allocations\n")do { } while (false);
6133 return false;
6134 }
6135 if (DI->PotentialAllocationCalls.size() > 1) {
6136 LLVM_DEBUG(dbgs() << "[H2S] unique free call might free "do { } while (false)
6137 << DI->PotentialAllocationCalls.size()do { } while (false)
6138 << " different allocations\n")do { } while (false);
6139 return false;
6140 }
6141 if (*DI->PotentialAllocationCalls.begin() != AI.CB) {
6142 LLVM_DEBUG(do { } while (false)
6143 dbgs()do { } while (false)
6144 << "[H2S] unique free call not known to free this allocation but "do { } while (false)
6145 << **DI->PotentialAllocationCalls.begin() << "\n")do { } while (false);
6146 return false;
6147 }
6148 Instruction *CtxI = isa<InvokeInst>(AI.CB) ? AI.CB : AI.CB->getNextNode();
6149 if (!Explorer.findInContextOf(UniqueFree, CtxI)) {
6150 LLVM_DEBUG(do { } while (false)
6151 dbgs()do { } while (false)
6152 << "[H2S] unique free call might not be executed with the allocation "do { } while (false)
6153 << *UniqueFree << "\n")do { } while (false);
6154 return false;
6155 }
6156 return true;
6157 };
6158
6159 auto UsesCheck = [&](AllocationInfo &AI) {
6160 bool ValidUsesOnly = true;
6161
6162 auto Pred = [&](const Use &U, bool &Follow) -> bool {
6163 Instruction *UserI = cast<Instruction>(U.getUser());
6164 if (isa<LoadInst>(UserI))
6165 return true;
6166 if (auto *SI = dyn_cast<StoreInst>(UserI)) {
6167 if (SI->getValueOperand() == U.get()) {
6168 LLVM_DEBUG(dbgs()do { } while (false)
6169 << "[H2S] escaping store to memory: " << *UserI << "\n")do { } while (false);
6170 ValidUsesOnly = false;
6171 } else {
6172 // A store into the malloc'ed memory is fine.
6173 }
6174 return true;
6175 }
6176 if (auto *CB = dyn_cast<CallBase>(UserI)) {
6177 if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
6178 return true;
6179 if (DeallocationInfos.count(CB)) {
6180 AI.PotentialFreeCalls.insert(CB);
6181 return true;
6182 }
6183
6184 unsigned ArgNo = CB->getArgOperandNo(&U);
6185
6186 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
6187 *this, IRPosition::callsite_argument(*CB, ArgNo),
6188 DepClassTy::OPTIONAL);
6189
6190 // If a call site argument use is nofree, we are fine.
6191 const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
6192 *this, IRPosition::callsite_argument(*CB, ArgNo),
6193 DepClassTy::OPTIONAL);
6194
6195 bool MaybeCaptured = !NoCaptureAA.isAssumedNoCapture();
6196 bool MaybeFreed = !ArgNoFreeAA.isAssumedNoFree();
6197 if (MaybeCaptured ||
6198 (AI.LibraryFunctionId != LibFunc___kmpc_alloc_shared &&
6199 MaybeFreed)) {
6200 AI.HasPotentiallyFreeingUnknownUses |= MaybeFreed;
6201
6202 // Emit a missed remark if this is missed OpenMP globalization.
6203 auto Remark = [&](OptimizationRemarkMissed ORM) {
6204 return ORM
6205 << "Could not move globalized variable to the stack. "
6206 "Variable is potentially captured in call. Mark "
6207 "parameter as `__attribute__((noescape))` to override.";
6208 };
6209
6210 if (ValidUsesOnly &&
6211 AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
6212 A.emitRemark<OptimizationRemarkMissed>(AI.CB, "OMP113", Remark);
6213
6214 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n")do { } while (false);
6215 ValidUsesOnly = false;
6216 }
6217 return true;
6218 }
6219
6220 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
6221 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
6222 Follow = true;
6223 return true;
6224 }
6225 // Unknown user for which we can not track uses further (in a way that
6226 // makes sense).
6227 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n")do { } while (false);
6228 ValidUsesOnly = false;
6229 return true;
6230 };
6231 if (!A.checkForAllUses(Pred, *this, *AI.CB))
6232 return false;
6233 return ValidUsesOnly;
6234 };
6235
6236 // The actual update starts here. We look at all allocations and depending on
6237 // their status perform the appropriate check(s).
6238 for (auto &It : AllocationInfos) {
6239 AllocationInfo &AI = *It.second;
6240 if (AI.Status == AllocationInfo::INVALID)
6241 continue;
6242
6243 if (MaxHeapToStackSize == -1) {
6244 if (AI.Kind == AllocationInfo::AllocationKind::ALIGNED_ALLOC)
6245 if (!getAPInt(A, *this, *AI.CB->getArgOperand(0)).hasValue()) {
6246 LLVM_DEBUG(dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CBdo { } while (false)
6247 << "\n")do { } while (false);
6248 AI.Status = AllocationInfo::INVALID;
6249 Changed = ChangeStatus::CHANGED;
6250 continue;
6251 }
6252 } else {
6253 Optional<APInt> Size = getSize(A, *this, AI);
6254 if (!Size.hasValue() || Size.getValue().ugt(MaxHeapToStackSize)) {
6255 LLVM_DEBUG({do { } while (false)
6256 if (!Size.hasValue())do { } while (false)
6257 dbgs() << "[H2S] Unknown allocation size (or alignment): " << *AI.CBdo { } while (false)
6258 << "\n";do { } while (false)
6259 elsedo { } while (false)
6260 dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. "do { } while (false)
6261 << MaxHeapToStackSize << "\n";do { } while (false)
6262 })do { } while (false);
6263
6264 AI.Status = AllocationInfo::INVALID;
6265 Changed = ChangeStatus::CHANGED;
6266 continue;
6267 }
6268 }
6269
6270 switch (AI.Status) {
6271 case AllocationInfo::STACK_DUE_TO_USE:
6272 if (UsesCheck(AI))
6273 continue;
6274 AI.Status = AllocationInfo::STACK_DUE_TO_FREE;
6275 LLVM_FALLTHROUGH[[gnu::fallthrough]];
6276 case AllocationInfo::STACK_DUE_TO_FREE:
6277 if (FreeCheck(AI))
6278 continue;
6279 AI.Status = AllocationInfo::INVALID;
6280 Changed = ChangeStatus::CHANGED;
6281 continue;
6282 case AllocationInfo::INVALID:
6283 llvm_unreachable("Invalid allocations should never reach this point!")__builtin_unreachable();
6284 };
6285 }
6286
6287 return Changed;
6288}
6289
6290/// ----------------------- Privatizable Pointers ------------------------------
6291struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
6292 AAPrivatizablePtrImpl(const IRPosition &IRP, Attributor &A)
6293 : AAPrivatizablePtr(IRP, A), PrivatizableType(llvm::None) {}
6294
6295 ChangeStatus indicatePessimisticFixpoint() override {
6296 AAPrivatizablePtr::indicatePessimisticFixpoint();
6297 PrivatizableType = nullptr;
6298 return ChangeStatus::CHANGED;
6299 }
6300
6301 /// Identify the type we can chose for a private copy of the underlying
6302 /// argument. None means it is not clear yet, nullptr means there is none.
6303 virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
6304
6305 /// Return a privatizable type that encloses both T0 and T1.
6306 /// TODO: This is merely a stub for now as we should manage a mapping as well.
6307 Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
6308 if (!T0.hasValue())
6309 return T1;
6310 if (!T1.hasValue())
6311 return T0;
6312 if (T0 == T1)
6313 return T0;
6314 return nullptr;
6315 }
6316
6317 Optional<Type *> getPrivatizableType() const override {
6318 return PrivatizableType;
6319 }
6320
6321 const std::string getAsStr() const override {
6322 return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
6323 }
6324
6325protected:
6326 Optional<Type *> PrivatizableType;
6327};
6328
6329// TODO: Do this for call site arguments (probably also other values) as well.
6330
6331struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
6332 AAPrivatizablePtrArgument(const IRPosition &IRP, Attributor &A)
6333 : AAPrivatizablePtrImpl(IRP, A) {}
6334
6335 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
6336 Optional<Type *> identifyPrivatizableType(Attributor &A) override {
6337 // If this is a byval argument and we know all the call sites (so we can
6338 // rewrite them), there is no need to check them explicitly.
6339 bool AllCallSitesKnown;
6340 if (getIRPosition().hasAttr(Attribute::ByVal) &&
6341 A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
6342 true, AllCallSitesKnown))
6343 return getAssociatedValue().getType()->getPointerElementType();
6344
6345 Optional<Type *> Ty;
6346 unsigned ArgNo = getIRPosition().getCallSiteArgNo();
6347
6348 // Make sure the associated call site argument has the same type at all call
6349 // sites and it is an allocation we know is safe to privatize, for now that
6350 // means we only allow alloca instructions.
6351 // TODO: We can additionally analyze the accesses in the callee to create
6352 // the type from that information instead. That is a little more
6353 // involved and will be done in a follow up patch.
6354 auto CallSiteCheck = [&](AbstractCallSite ACS) {
6355 IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
6356 // Check if a coresponding argument was found or if it is one not
6357 // associated (which can happen for callback calls).
6358 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
6359 return false;
6360
6361 // Check that all call sites agree on a type.
6362 auto &PrivCSArgAA =
6363 A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos, DepClassTy::REQUIRED);
6364 Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
6365
6366 LLVM_DEBUG({do { } while (false)
6367 dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";do { } while (false)
6368 if (CSTy.hasValue() && CSTy.getValue())do { } while (false)
6369 CSTy.getValue()->print(dbgs());do { } while (false)
6370 else if (CSTy.hasValue())do { } while (false)
6371 dbgs() << "<nullptr>";do { } while (false)
6372 elsedo { } while (false)
6373 dbgs() << "<none>";do { } while (false)
6374 })do { } while (false);
6375
6376 Ty = combineTypes(Ty, CSTy);
6377
6378 LLVM_DEBUG({do { } while (false)
6379 dbgs() << " : New Type: ";do { } while (false)
6380 if (Ty.hasValue() && Ty.getValue())do { } while (false)
6381 Ty.getValue()->print(dbgs());do { } while (false)
6382 else if (Ty.hasValue())do { } while (false)
6383 dbgs() << "<nullptr>";do { } while (false)
6384 elsedo { } while (false)
6385 dbgs() << "<none>";do { } while (false)
6386 dbgs() << "\n";do { } while (false)
6387 })do { } while (false);
6388
6389 return !Ty.hasValue() || Ty.getValue();
6390 };
6391
6392 if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown))
6393 return nullptr;
6394 return Ty;
6395 }
6396
6397 /// See AbstractAttribute::updateImpl(...).
6398 ChangeStatus updateImpl(Attributor &A) override {
6399 PrivatizableType = identifyPrivatizableType(A);
6400 if (!PrivatizableType.hasValue())
6401 return ChangeStatus::UNCHANGED;
6402 if (!PrivatizableType.getValue())
6403 return indicatePessimisticFixpoint();
6404
6405 // The dependence is optional so we don't give up once we give up on the
6406 // alignment.
6407 A.getAAFor<AAAlign>(*this, IRPosition::value(getAssociatedValue()),
6408 DepClassTy::OPTIONAL);
6409
6410 // Avoid arguments with padding for now.
6411 if (!getIRPosition().hasAttr(Attribute::ByVal) &&
6412 !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
6413 A.getInfoCache().getDL())) {
6414 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n")do { } while (false);
6415 return indicatePessimisticFixpoint();
6416 }
6417
6418 // Verify callee and caller agree on how the promoted argument would be
6419 // passed.
6420 // TODO: The use of the ArgumentPromotion interface here is ugly, we need a
6421 // specialized form of TargetTransformInfo::areFunctionArgsABICompatible
6422 // which doesn't require the arguments ArgumentPromotion wanted to pass.
6423 Function &Fn = *getIRPosition().getAnchorScope();
6424 SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy;
6425 ArgsToPromote.insert(getAssociatedArgument());
6426 const auto *TTI =
6427 A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
6428 if (!TTI ||
6429 !ArgumentPromotionPass::areFunctionArgsABICompatible(
6430 Fn, *TTI, ArgsToPromote, Dummy) ||
6431 ArgsToPromote.empty()) {
6432 LLVM_DEBUG(do { } while (false)
6433 dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "do { } while (false)
6434 << Fn.getName() << "\n")do { } while (false);
6435 return indicatePessimisticFixpoint();
6436 }
6437
6438 // Collect the types that will replace the privatizable type in the function
6439 // signature.
6440 SmallVector<Type *, 16> ReplacementTypes;
6441 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
6442
6443 // Register a rewrite of the argument.
6444 Argument *Arg = getAssociatedArgument();
6445 if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
6446 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n")do { } while (false);
6447 return indicatePessimisticFixpoint();
6448 }
6449
6450 unsigned ArgNo = Arg->getArgNo();
6451
6452 // Helper to check if for the given call site the associated argument is
6453 // passed to a callback where the privatization would be different.
6454 auto IsCompatiblePrivArgOfCallback = [&](CallBase &CB) {
6455 SmallVector<const Use *, 4> CallbackUses;
6456 AbstractCallSite::getCallbackUses(CB, CallbackUses);
6457 for (const Use *U : CallbackUses) {
6458 AbstractCallSite CBACS(U);
6459 assert(CBACS && CBACS.isCallbackCall())((void)0);
6460 for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
6461 int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
6462
6463 LLVM_DEBUG({do { } while (false)
6464 dbgs()do { } while (false)
6465 << "[AAPrivatizablePtr] Argument " << *Argdo { } while (false)
6466 << "check if can be privatized in the context of its parent ("do { } while (false)
6467 << Arg->getParent()->getName()do { } while (false)
6468 << ")\n[AAPrivatizablePtr] because it is an argument in a "do { } while (false)
6469 "callback ("do { } while (false)
6470 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()do { } while (false)
6471 << ")\n[AAPrivatizablePtr] " << CBArg << " : "do { } while (false)
6472 << CBACS.getCallArgOperand(CBArg) << " vs "do { } while (false)
6473 << CB.getArgOperand(ArgNo) << "\n"do { } while (false)
6474 << "[AAPrivatizablePtr] " << CBArg << " : "do { } while (false)
6475 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";do { } while (false)
6476 })do { } while (false);
6477
6478 if (CBArgNo != int(ArgNo))
6479 continue;
6480 const auto &CBArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
6481 *this, IRPosition::argument(CBArg), DepClassTy::REQUIRED);
6482 if (CBArgPrivAA.isValidState()) {
6483 auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
6484 if (!CBArgPrivTy.hasValue())
6485 continue;
6486 if (CBArgPrivTy.getValue() == PrivatizableType)
6487 continue;
6488 }
6489
6490 LLVM_DEBUG({do { } while (false)
6491 dbgs() << "[AAPrivatizablePtr] Argument " << *Argdo { } while (false)
6492 << " cannot be privatized in the context of its parent ("do { } while (false)
6493 << Arg->getParent()->getName()do { } while (false)
6494 << ")\n[AAPrivatizablePtr] because it is an argument in a "do { } while (false)
6495 "callback ("do { } while (false)
6496 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()do { } while (false)
6497 << ").\n[AAPrivatizablePtr] for which the argument "do { } while (false)
6498 "privatization is not compatible.\n";do { } while (false)
6499 })do { } while (false);
6500 return false;
6501 }
6502 }
6503 return true;
6504 };
6505
6506 // Helper to check if for the given call site the associated argument is
6507 // passed to a direct call where the privatization would be different.
6508 auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
6509 CallBase *DC = cast<CallBase>(ACS.getInstruction());
6510 int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
6511 assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() &&((void)0)
6512 "Expected a direct call operand for callback call operand")((void)0);
6513
6514 LLVM_DEBUG({do { } while (false)
6515 dbgs() << "[AAPrivatizablePtr] Argument " << *Argdo { } while (false)
6516 << " check if be privatized in the context of its parent ("do { } while (false)
6517 << Arg->getParent()->getName()do { } while (false)
6518 << ")\n[AAPrivatizablePtr] because it is an argument in a "do { } while (false)
6519 "direct call of ("do { } while (false)
6520 << DCArgNo << "@" << DC->getCalledFunction()->getName()do { } while (false)
6521 << ").\n";do { } while (false)
6522 })do { } while (false);
6523
6524 Function *DCCallee = DC->getCalledFunction();
6525 if (unsigned(DCArgNo) < DCCallee->arg_size()) {
6526 const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
6527 *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)),
6528 DepClassTy::REQUIRED);
6529 if (DCArgPrivAA.isValidState()) {
6530 auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
6531 if (!DCArgPrivTy.hasValue())
6532 return true;
6533 if (DCArgPrivTy.getValue() == PrivatizableType)
6534 return true;
6535 }
6536 }
6537
6538 LLVM_DEBUG({do { } while (false)
6539 dbgs() << "[AAPrivatizablePtr] Argument " << *Argdo { } while (false)
6540 << " cannot be privatized in the context of its parent ("do { } while (false)
6541 << Arg->getParent()->getName()do { } while (false)
6542 << ")\n[AAPrivatizablePtr] because it is an argument in a "do { } while (false)
6543 "direct call of ("do { } while (false)
6544 << ACS.getInstruction()->getCalledFunction()->getName()do { } while (false)
6545 << ").\n[AAPrivatizablePtr] for which the argument "do { } while (false)
6546 "privatization is not compatible.\n";do { } while (false)
6547 })do { } while (false);
6548 return false;
6549 };
6550
6551 // Helper to check if the associated argument is used at the given abstract
6552 // call site in a way that is incompatible with the privatization assumed
6553 // here.
6554 auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
6555 if (ACS.isDirectCall())
6556 return IsCompatiblePrivArgOfCallback(*ACS.getInstruction());
6557 if (ACS.isCallbackCall())
6558 return IsCompatiblePrivArgOfDirectCS(ACS);
6559 return false;
6560 };
6561
6562 bool AllCallSitesKnown;
6563 if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
6564 AllCallSitesKnown))
6565 return indicatePessimisticFixpoint();
6566
6567 return ChangeStatus::UNCHANGED;
6568 }
6569
6570 /// Given a type to private \p PrivType, collect the constituates (which are
6571 /// used) in \p ReplacementTypes.
6572 static void
6573 identifyReplacementTypes(Type *PrivType,
6574 SmallVectorImpl<Type *> &ReplacementTypes) {
6575 // TODO: For now we expand the privatization type to the fullest which can
6576 // lead to dead arguments that need to be removed later.
6577 assert(PrivType && "Expected privatizable type!")((void)0);
6578
6579 // Traverse the type, extract constituate types on the outermost level.
6580 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6581 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
6582 ReplacementTypes.push_back(PrivStructType->getElementType(u));
6583 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6584 ReplacementTypes.append(PrivArrayType->getNumElements(),
6585 PrivArrayType->getElementType());
6586 } else {
6587 ReplacementTypes.push_back(PrivType);
6588 }
6589 }
6590
6591 /// Initialize \p Base according to the type \p PrivType at position \p IP.
6592 /// The values needed are taken from the arguments of \p F starting at
6593 /// position \p ArgNo.
6594 static void createInitialization(Type *PrivType, Value &Base, Function &F,
6595 unsigned ArgNo, Instruction &IP) {
6596 assert(PrivType && "Expected privatizable type!")((void)0);
6597
6598 IRBuilder<NoFolder> IRB(&IP);
6599 const DataLayout &DL = F.getParent()->getDataLayout();
6600
6601 // Traverse the type, build GEPs and stores.
6602 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6603 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
6604 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
6605 Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
6606 Value *Ptr =
6607 constructPointer(PointeeTy, PrivType, &Base,
6608 PrivStructLayout->getElementOffset(u), IRB, DL);
6609 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
6610 }
6611 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6612 Type *PointeeTy = PrivArrayType->getElementType();
6613 Type *PointeePtrTy = PointeeTy->getPointerTo();
6614 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
6615 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
6616 Value *Ptr = constructPointer(PointeePtrTy, PrivType, &Base,
6617 u * PointeeTySize, IRB, DL);
6618 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
6619 }
6620 } else {
6621 new StoreInst(F.getArg(ArgNo), &Base, &IP);
6622 }
6623 }
6624
6625 /// Extract values from \p Base according to the type \p PrivType at the
6626 /// call position \p ACS. The values are appended to \p ReplacementValues.
6627 void createReplacementValues(Align Alignment, Type *PrivType,
6628 AbstractCallSite ACS, Value *Base,
6629 SmallVectorImpl<Value *> &ReplacementValues) {
6630 assert(Base && "Expected base value!")((void)0);
6631 assert(PrivType && "Expected privatizable type!")((void)0);
6632 Instruction *IP = ACS.getInstruction();
6633
6634 IRBuilder<NoFolder> IRB(IP);
6635 const DataLayout &DL = IP->getModule()->getDataLayout();
6636
6637 if (Base->getType()->getPointerElementType() != PrivType)
6638 Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(),
6639 "", ACS.getInstruction());
6640
6641 // Traverse the type, build GEPs and loads.
6642 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
6643 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
6644 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
6645 Type *PointeeTy = PrivStructType->getElementType(u);
6646 Value *Ptr =
6647 constructPointer(PointeeTy->getPointerTo(), PrivType, Base,
6648 PrivStructLayout->getElementOffset(u), IRB, DL);
6649 LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
6650 L->setAlignment(Alignment);
6651 ReplacementValues.push_back(L);
6652 }
6653 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
6654 Type *PointeeTy = PrivArrayType->getElementType();
6655 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
6656 Type *PointeePtrTy = PointeeTy->getPointerTo();
6657 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
6658 Value *Ptr = constructPointer(PointeePtrTy, PrivType, Base,
6659 u * PointeeTySize, IRB, DL);
6660 LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
6661 L->setAlignment(Alignment);
6662 ReplacementValues.push_back(L);
6663 }
6664 } else {
6665 LoadInst *L = new LoadInst(PrivType, Base, "", IP);
6666 L->setAlignment(Alignment);
6667 ReplacementValues.push_back(L);
6668 }
6669 }
6670
6671 /// See AbstractAttribute::manifest(...)
6672 ChangeStatus manifest(Attributor &A) override {
6673 if (!PrivatizableType.hasValue())
6674 return ChangeStatus::UNCHANGED;
6675 assert(PrivatizableType.getValue() && "Expected privatizable type!")((void)0);
6676
6677 // Collect all tail calls in the function as we cannot allow new allocas to
6678 // escape into tail recursion.
6679 // TODO: Be smarter about new allocas escaping into tail calls.
6680 SmallVector<CallInst *, 16> TailCalls;
6681 bool UsedAssumedInformation = false;
6682 if (!A.checkForAllInstructions(
6683 [&](Instruction &I) {
6684 CallInst &CI = cast<CallInst>(I);
6685 if (CI.isTailCall())
6686 TailCalls.push_back(&CI);
6687 return true;
6688 },
6689 *this, {Instruction::Call}, UsedAssumedInformation))
6690 return ChangeStatus::UNCHANGED;
6691
6692 Argument *Arg = getAssociatedArgument();
6693 // Query AAAlign attribute for alignment of associated argument to
6694 // determine the best alignment of loads.
6695 const auto &AlignAA =
6696 A.getAAFor<AAAlign>(*this, IRPosition::value(*Arg), DepClassTy::NONE);
6697
6698 // Callback to repair the associated function. A new alloca is placed at the
6699 // beginning and initialized with the values passed through arguments. The
6700 // new alloca replaces the use of the old pointer argument.
6701 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
6702 [=](const Attributor::ArgumentReplacementInfo &ARI,
6703 Function &ReplacementFn, Function::arg_iterator ArgIt) {
6704 BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
6705 Instruction *IP = &*EntryBB.getFirstInsertionPt();
6706 Instruction *AI = new AllocaInst(PrivatizableType.getValue(), 0,
6707 Arg->getName() + ".priv", IP);
6708 createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
6709 ArgIt->getArgNo(), *IP);
6710
6711 if (AI->getType() != Arg->getType())
6712 AI =
6713 BitCastInst::CreateBitOrPointerCast(AI, Arg->getType(), "", IP);
6714 Arg->replaceAllUsesWith(AI);
6715
6716 for (CallInst *CI : TailCalls)
6717 CI->setTailCall(false);
6718 };
6719
6720 // Callback to repair a call site of the associated function. The elements
6721 // of the privatizable type are loaded prior to the call and passed to the
6722 // new function version.
6723 Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
6724 [=, &AlignAA](const Attributor::ArgumentReplacementInfo &ARI,
6725 AbstractCallSite ACS,
6726 SmallVectorImpl<Value *> &NewArgOperands) {
6727 // When no alignment is specified for the load instruction,
6728 // natural alignment is assumed.
6729 createReplacementValues(
6730 assumeAligned(AlignAA.getAssumedAlign()),
6731 PrivatizableType.getValue(), ACS,
6732 ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
6733 NewArgOperands);
6734 };
6735
6736 // Collect the types that will replace the privatizable type in the function
6737 // signature.
6738 SmallVector<Type *, 16> ReplacementTypes;
6739 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
6740
6741 // Register a rewrite of the argument.
6742 if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
6743 std::move(FnRepairCB),
6744 std::move(ACSRepairCB)))
6745 return ChangeStatus::CHANGED;
6746 return ChangeStatus::UNCHANGED;
6747 }
6748
6749 /// See AbstractAttribute::trackStatistics()
6750 void trackStatistics() const override {
6751 STATS_DECLTRACK_ARG_ATTR(privatizable_ptr){ static llvm::Statistic NumIRArguments_privatizable_ptr = {"attributor"
, "NumIRArguments_privatizable_ptr", ("Number of " "arguments"
" marked '" "privatizable_ptr" "'")};; ++(NumIRArguments_privatizable_ptr
); }
;
6752 }
6753};
6754
6755struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
6756 AAPrivatizablePtrFloating(const IRPosition &IRP, Attributor &A)
6757 : AAPrivatizablePtrImpl(IRP, A) {}
6758
6759 /// See AbstractAttribute::initialize(...).
6760 virtual void initialize(Attributor &A) override {
6761 // TODO: We can privatize more than arguments.
6762 indicatePessimisticFixpoint();
6763 }
6764
6765 ChangeStatus updateImpl(Attributor &A) override {
6766 llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"__builtin_unreachable()
6767 "updateImpl will not be called")__builtin_unreachable();
6768 }
6769
6770 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
6771 Optional<Type *> identifyPrivatizableType(Attributor &A) override {
6772 Value *Obj = getUnderlyingObject(&getAssociatedValue());
6773 if (!Obj) {
6774 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n")do { } while (false);
6775 return nullptr;
6776 }
6777
6778 if (auto *AI = dyn_cast<AllocaInst>(Obj))
6779 if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
6780 if (CI->isOne())
6781 return Obj->getType()->getPointerElementType();
6782 if (auto *Arg = dyn_cast<Argument>(Obj)) {
6783 auto &PrivArgAA = A.getAAFor<AAPrivatizablePtr>(
6784 *this, IRPosition::argument(*Arg), DepClassTy::REQUIRED);
6785 if (PrivArgAA.isAssumedPrivatizablePtr())
6786 return Obj->getType()->getPointerElementType();
6787 }
6788
6789 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "do { } while (false)
6790 "alloca nor privatizable argument: "do { } while (false)
6791 << *Obj << "!\n")do { } while (false);
6792 return nullptr;
6793 }
6794
6795 /// See AbstractAttribute::trackStatistics()
6796 void trackStatistics() const override {
6797 STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr){ static llvm::Statistic NumIRFloating_privatizable_ptr = {"attributor"
, "NumIRFloating_privatizable_ptr", ("Number of floating values known to be '"
"privatizable_ptr" "'")};; ++(NumIRFloating_privatizable_ptr
); }
;
6798 }
6799};
6800
6801struct AAPrivatizablePtrCallSiteArgument final
6802 : public AAPrivatizablePtrFloating {
6803 AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP, Attributor &A)
6804 : AAPrivatizablePtrFloating(IRP, A) {}
6805
6806 /// See AbstractAttribute::initialize(...).
6807 void initialize(Attributor &A) override {
6808 if (getIRPosition().hasAttr(Attribute::ByVal))
6809 indicateOptimisticFixpoint();
6810 }
6811
6812 /// See AbstractAttribute::updateImpl(...).
6813 ChangeStatus updateImpl(Attributor &A) override {
6814 PrivatizableType = identifyPrivatizableType(A);
6815 if (!PrivatizableType.hasValue())
6816 return ChangeStatus::UNCHANGED;
6817 if (!PrivatizableType.getValue())
6818 return indicatePessimisticFixpoint();
6819
6820 const IRPosition &IRP = getIRPosition();
6821 auto &NoCaptureAA =
6822 A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::REQUIRED);
6823 if (!NoCaptureAA.isAssumedNoCapture()) {
6824 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n")do { } while (false);
6825 return indicatePessimisticFixpoint();
6826 }
6827
6828 auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP, DepClassTy::REQUIRED);
6829 if (!NoAliasAA.isAssumedNoAlias()) {
6830 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n")do { } while (false);
6831 return indicatePessimisticFixpoint();
6832 }
6833
6834 const auto &MemBehaviorAA =
6835 A.getAAFor<AAMemoryBehavior>(*this, IRP, DepClassTy::REQUIRED);
6836 if (!MemBehaviorAA.isAssumedReadOnly()) {
6837 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n")do { } while (false);
6838 return indicatePessimisticFixpoint();
6839 }
6840
6841 return ChangeStatus::UNCHANGED;
6842 }
6843
6844 /// See AbstractAttribute::trackStatistics()
6845 void trackStatistics() const override {
6846 STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr){ static llvm::Statistic NumIRCSArguments_privatizable_ptr = {
"attributor", "NumIRCSArguments_privatizable_ptr", ("Number of "
"call site arguments" " marked '" "privatizable_ptr" "'")};;
++(NumIRCSArguments_privatizable_ptr); }
;
6847 }
6848};
6849
6850struct AAPrivatizablePtrCallSiteReturned final
6851 : public AAPrivatizablePtrFloating {
6852 AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP, Attributor &A)
6853 : AAPrivatizablePtrFloating(IRP, A) {}
6854
6855 /// See AbstractAttribute::initialize(...).
6856 void initialize(Attributor &A) override {
6857 // TODO: We can privatize more than arguments.
6858 indicatePessimisticFixpoint();
6859 }
6860
6861 /// See AbstractAttribute::trackStatistics()
6862 void trackStatistics() const override {
6863 STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr){ static llvm::Statistic NumIRCSReturn_privatizable_ptr = {"attributor"
, "NumIRCSReturn_privatizable_ptr", ("Number of " "call site returns"
" marked '" "privatizable_ptr" "'")};; ++(NumIRCSReturn_privatizable_ptr
); }
;
6864 }
6865};
6866
6867struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
6868 AAPrivatizablePtrReturned(const IRPosition &IRP, Attributor &A)
6869 : AAPrivatizablePtrFloating(IRP, A) {}
6870
6871 /// See AbstractAttribute::initialize(...).
6872 void initialize(Attributor &A) override {
6873 // TODO: We can privatize more than arguments.
6874 indicatePessimisticFixpoint();
6875 }
6876
6877 /// See AbstractAttribute::trackStatistics()
6878 void trackStatistics() const override {
6879 STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr){ static llvm::Statistic NumIRFunctionReturn_privatizable_ptr
= {"attributor", "NumIRFunctionReturn_privatizable_ptr", ("Number of "
"function returns" " marked '" "privatizable_ptr" "'")};; ++
(NumIRFunctionReturn_privatizable_ptr); }
;
6880 }
6881};
6882
6883/// -------------------- Memory Behavior Attributes ----------------------------
6884/// Includes read-none, read-only, and write-only.
6885/// ----------------------------------------------------------------------------
6886struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
6887 AAMemoryBehaviorImpl(const IRPosition &IRP, Attributor &A)
6888 : AAMemoryBehavior(IRP, A) {}
6889
6890 /// See AbstractAttribute::initialize(...).
6891 void initialize(Attributor &A) override {
6892 intersectAssumedBits(BEST_STATE);
6893 getKnownStateFromValue(getIRPosition(), getState());
6894 AAMemoryBehavior::initialize(A);
6895 }
6896
6897 /// Return the memory behavior information encoded in the IR for \p IRP.
6898 static void getKnownStateFromValue(const IRPosition &IRP,
6899 BitIntegerState &State,
6900 bool IgnoreSubsumingPositions = false) {
6901 SmallVector<Attribute, 2> Attrs;
6902 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
6903 for (const Attribute &Attr : Attrs) {
6904 switch (Attr.getKindAsEnum()) {
6905 case Attribute::ReadNone:
6906 State.addKnownBits(NO_ACCESSES);
6907 break;
6908 case Attribute::ReadOnly:
6909 State.addKnownBits(NO_WRITES);
6910 break;
6911 case Attribute::WriteOnly:
6912 State.addKnownBits(NO_READS);
6913 break;
6914 default:
6915 llvm_unreachable("Unexpected attribute!")__builtin_unreachable();
6916 }
6917 }
6918
6919 if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
6920 if (!I->mayReadFromMemory())
6921 State.addKnownBits(NO_READS);
6922 if (!I->mayWriteToMemory())
6923 State.addKnownBits(NO_WRITES);
6924 }
6925 }
6926
6927 /// See AbstractAttribute::getDeducedAttributes(...).
6928 void getDeducedAttributes(LLVMContext &Ctx,
6929 SmallVectorImpl<Attribute> &Attrs) const override {
6930 assert(Attrs.size() == 0)((void)0);
6931 if (isAssumedReadNone())
6932 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
6933 else if (isAssumedReadOnly())
6934 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
6935 else if (isAssumedWriteOnly())
6936 Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
6937 assert(Attrs.size() <= 1)((void)0);
6938 }
6939
6940 /// See AbstractAttribute::manifest(...).
6941 ChangeStatus manifest(Attributor &A) override {
6942 if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true))
6943 return ChangeStatus::UNCHANGED;
6944
6945 const IRPosition &IRP = getIRPosition();
6946
6947 // Check if we would improve the existing attributes first.
6948 SmallVector<Attribute, 4> DeducedAttrs;
6949 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
6950 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
6951 return IRP.hasAttr(Attr.getKindAsEnum(),
6952 /* IgnoreSubsumingPositions */ true);
6953 }))
6954 return ChangeStatus::UNCHANGED;
6955
6956 // Clear existing attributes.
6957 IRP.removeAttrs(AttrKinds);
6958
6959 // Use the generic manifest method.
6960 return IRAttribute::manifest(A);
6961 }
6962
6963 /// See AbstractState::getAsStr().
6964 const std::string getAsStr() const override {
6965 if (isAssumedReadNone())
6966 return "readnone";
6967 if (isAssumedReadOnly())
6968 return "readonly";
6969 if (isAssumedWriteOnly())
6970 return "writeonly";
6971 return "may-read/write";
6972 }
6973
6974 /// The set of IR attributes AAMemoryBehavior deals with.
6975 static const Attribute::AttrKind AttrKinds[3];
6976};
6977
6978const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
6979 Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
6980
6981/// Memory behavior attribute for a floating value.
6982struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
6983 AAMemoryBehaviorFloating(const IRPosition &IRP, Attributor &A)
6984 : AAMemoryBehaviorImpl(IRP, A) {}
6985
6986 /// See AbstractAttribute::updateImpl(...).
6987 ChangeStatus updateImpl(Attributor &A) override;
6988
6989 /// See AbstractAttribute::trackStatistics()
6990 void trackStatistics() const override {
6991 if (isAssumedReadNone())
6992 STATS_DECLTRACK_FLOATING_ATTR(readnone){ static llvm::Statistic NumIRFloating_readnone = {"attributor"
, "NumIRFloating_readnone", ("Number of floating values known to be '"
"readnone" "'")};; ++(NumIRFloating_readnone); }
6993 else if (isAssumedReadOnly())
6994 STATS_DECLTRACK_FLOATING_ATTR(readonly){ static llvm::Statistic NumIRFloating_readonly = {"attributor"
, "NumIRFloating_readonly", ("Number of floating values known to be '"
"readonly" "'")};; ++(NumIRFloating_readonly); }
6995 else if (isAssumedWriteOnly())
6996 STATS_DECLTRACK_FLOATING_ATTR(writeonly){ static llvm::Statistic NumIRFloating_writeonly = {"attributor"
, "NumIRFloating_writeonly", ("Number of floating values known to be '"
"writeonly" "'")};; ++(NumIRFloating_writeonly); }
6997 }
6998
6999private:
7000 /// Return true if users of \p UserI might access the underlying
7001 /// variable/location described by \p U and should therefore be analyzed.
7002 bool followUsersOfUseIn(Attributor &A, const Use &U,
7003 const Instruction *UserI);
7004
7005 /// Update the state according to the effect of use \p U in \p UserI.
7006 void analyzeUseIn(Attributor &A, const Use &U, const Instruction *UserI);
7007};
7008
7009/// Memory behavior attribute for function argument.
7010struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
7011 AAMemoryBehaviorArgument(const IRPosition &IRP, Attributor &A)
7012 : AAMemoryBehaviorFloating(IRP, A) {}
7013
7014 /// See AbstractAttribute::initialize(...).
7015 void initialize(Attributor &A) override {
7016 intersectAssumedBits(BEST_STATE);
7017 const IRPosition &IRP = getIRPosition();
7018 // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
7019 // can query it when we use has/getAttr. That would allow us to reuse the
7020 // initialize of the base class here.
7021 bool HasByVal =
7022 IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
7023 getKnownStateFromValue(IRP, getState(),
7024 /* IgnoreSubsumingPositions */ HasByVal);
7025
7026 // Initialize the use vector with all direct uses of the associated value.
7027 Argument *Arg = getAssociatedArgument();
7028 if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent())))
7029 indicatePessimisticFixpoint();
7030 }
7031
7032 ChangeStatus manifest(Attributor &A) override {
7033 // TODO: Pointer arguments are not supported on vectors of pointers yet.
7034 if (!getAssociatedValue().getType()->isPointerTy())
7035 return ChangeStatus::UNCHANGED;
7036
7037 // TODO: From readattrs.ll: "inalloca parameters are always
7038 // considered written"
7039 if (hasAttr({Attribute::InAlloca, Attribute::Preallocated})) {
7040 removeKnownBits(NO_WRITES);
7041 removeAssumedBits(NO_WRITES);
7042 }
7043 return AAMemoryBehaviorFloating::manifest(A);
7044 }
7045
7046 /// See AbstractAttribute::trackStatistics()
7047 void trackStatistics() const override {
7048 if (isAssumedReadNone())
7049 STATS_DECLTRACK_ARG_ATTR(readnone){ static llvm::Statistic NumIRArguments_readnone = {"attributor"
, "NumIRArguments_readnone", ("Number of " "arguments" " marked '"
"readnone" "'")};; ++(NumIRArguments_readnone); }
7050 else if (isAssumedReadOnly())
7051 STATS_DECLTRACK_ARG_ATTR(readonly){ static llvm::Statistic NumIRArguments_readonly = {"attributor"
, "NumIRArguments_readonly", ("Number of " "arguments" " marked '"
"readonly" "'")};; ++(NumIRArguments_readonly); }
7052 else if (isAssumedWriteOnly())
7053 STATS_DECLTRACK_ARG_ATTR(writeonly){ static llvm::Statistic NumIRArguments_writeonly = {"attributor"
, "NumIRArguments_writeonly", ("Number of " "arguments" " marked '"
"writeonly" "'")};; ++(NumIRArguments_writeonly); }
7054 }
7055};
7056
7057struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
7058 AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP, Attributor &A)
7059 : AAMemoryBehaviorArgument(IRP, A) {}
7060
7061 /// See AbstractAttribute::initialize(...).
7062 void initialize(Attributor &A) override {
7063 // If we don't have an associated attribute this is either a variadic call
7064 // or an indirect call, either way, nothing to do here.
7065 Argument *Arg = getAssociatedArgument();
7066 if (!Arg) {
7067 indicatePessimisticFixpoint();
7068 return;
7069 }
7070 if (Arg->hasByValAttr()) {
7071 addKnownBits(NO_WRITES);
7072 removeKnownBits(NO_READS);
7073 removeAssumedBits(NO_READS);
7074 }
7075 AAMemoryBehaviorArgument::initialize(A);
7076 if (getAssociatedFunction()->isDeclaration())
7077 indicatePessimisticFixpoint();
7078 }
7079
7080 /// See AbstractAttribute::updateImpl(...).
7081 ChangeStatus updateImpl(Attributor &A) override {
7082 // TODO: Once we have call site specific value information we can provide
7083 // call site specific liveness liveness information and then it makes
7084 // sense to specialize attributes for call sites arguments instead of
7085 // redirecting requests to the callee argument.
7086 Argument *Arg = getAssociatedArgument();
7087 const IRPosition &ArgPos = IRPosition::argument(*Arg);
7088 auto &ArgAA =
7089 A.getAAFor<AAMemoryBehavior>(*this, ArgPos, DepClassTy::REQUIRED);
7090 return clampStateAndIndicateChange(getState(), ArgAA.getState());
7091 }
7092
7093 /// See AbstractAttribute::trackStatistics()
7094 void trackStatistics() const override {
7095 if (isAssumedReadNone())
7096 STATS_DECLTRACK_CSARG_ATTR(readnone){ static llvm::Statistic NumIRCSArguments_readnone = {"attributor"
, "NumIRCSArguments_readnone", ("Number of " "call site arguments"
" marked '" "readnone" "'")};; ++(NumIRCSArguments_readnone)
; }
7097 else if (isAssumedReadOnly())
7098 STATS_DECLTRACK_CSARG_ATTR(readonly){ static llvm::Statistic NumIRCSArguments_readonly = {"attributor"
, "NumIRCSArguments_readonly", ("Number of " "call site arguments"
" marked '" "readonly" "'")};; ++(NumIRCSArguments_readonly)
; }
7099 else if (isAssumedWriteOnly())
7100 STATS_DECLTRACK_CSARG_ATTR(writeonly){ static llvm::Statistic NumIRCSArguments_writeonly = {"attributor"
, "NumIRCSArguments_writeonly", ("Number of " "call site arguments"
" marked '" "writeonly" "'")};; ++(NumIRCSArguments_writeonly
); }
7101 }
7102};
7103
7104/// Memory behavior attribute for a call site return position.
7105struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
7106 AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP, Attributor &A)
7107 : AAMemoryBehaviorFloating(IRP, A) {}
7108
7109 /// See AbstractAttribute::initialize(...).
7110 void initialize(Attributor &A) override {
7111 AAMemoryBehaviorImpl::initialize(A);
7112 Function *F = getAssociatedFunction();
7113 if (!F || F->isDeclaration())
7114 indicatePessimisticFixpoint();
7115 }
7116
7117 /// See AbstractAttribute::manifest(...).
7118 ChangeStatus manifest(Attributor &A) override {
7119 // We do not annotate returned values.
7120 return ChangeStatus::UNCHANGED;
7121 }
7122
7123 /// See AbstractAttribute::trackStatistics()
7124 void trackStatistics() const override {}
7125};
7126
7127/// An AA to represent the memory behavior function attributes.
7128struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
7129 AAMemoryBehaviorFunction(const IRPosition &IRP, Attributor &A)
7130 : AAMemoryBehaviorImpl(IRP, A) {}
7131
7132 /// See AbstractAttribute::updateImpl(Attributor &A).
7133 virtual ChangeStatus updateImpl(Attributor &A) override;
7134
7135 /// See AbstractAttribute::manifest(...).
7136 ChangeStatus manifest(Attributor &A) override {
7137 Function &F = cast<Function>(getAnchorValue());
7138 if (isAssumedReadNone()) {
7139 F.removeFnAttr(Attribute::ArgMemOnly);
7140 F.removeFnAttr(Attribute::InaccessibleMemOnly);
7141 F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
7142 }
7143 return AAMemoryBehaviorImpl::manifest(A);
7144 }
7145
7146 /// See AbstractAttribute::trackStatistics()
7147 void trackStatistics() const override {
7148 if (isAssumedReadNone())
7149 STATS_DECLTRACK_FN_ATTR(readnone){ static llvm::Statistic NumIRFunction_readnone = {"attributor"
, "NumIRFunction_readnone", ("Number of " "functions" " marked '"
"readnone" "'")};; ++(NumIRFunction_readnone); }
7150 else if (isAssumedReadOnly())
7151 STATS_DECLTRACK_FN_ATTR(readonly){ static llvm::Statistic NumIRFunction_readonly = {"attributor"
, "NumIRFunction_readonly", ("Number of " "functions" " marked '"
"readonly" "'")};; ++(NumIRFunction_readonly); }
7152 else if (isAssumedWriteOnly())
7153 STATS_DECLTRACK_FN_ATTR(writeonly){ static llvm::Statistic NumIRFunction_writeonly = {"attributor"
, "NumIRFunction_writeonly", ("Number of " "functions" " marked '"
"writeonly" "'")};; ++(NumIRFunction_writeonly); }
7154 }
7155};
7156
7157/// AAMemoryBehavior attribute for call sites.
7158struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
7159 AAMemoryBehaviorCallSite(const IRPosition &IRP, Attributor &A)
7160 : AAMemoryBehaviorImpl(IRP, A) {}
7161
7162 /// See AbstractAttribute::initialize(...).
7163 void initialize(Attributor &A) override {
7164 AAMemoryBehaviorImpl::initialize(A);
7165 Function *F = getAssociatedFunction();
7166 if (!F || F->isDeclaration())
7167 indicatePessimisticFixpoint();
7168 }
7169
7170 /// See AbstractAttribute::updateImpl(...).
7171 ChangeStatus updateImpl(Attributor &A) override {
7172 // TODO: Once we have call site specific value information we can provide
7173 // call site specific liveness liveness information and then it makes
7174 // sense to specialize attributes for call sites arguments instead of
7175 // redirecting requests to the callee argument.
7176 Function *F = getAssociatedFunction();
7177 const IRPosition &FnPos = IRPosition::function(*F);
7178 auto &FnAA =
7179 A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::REQUIRED);
7180 return clampStateAndIndicateChange(getState(), FnAA.getState());
7181 }
7182
7183 /// See AbstractAttribute::trackStatistics()
7184 void trackStatistics() const override {
7185 if (isAssumedReadNone())
7186 STATS_DECLTRACK_CS_ATTR(readnone){ static llvm::Statistic NumIRCS_readnone = {"attributor", "NumIRCS_readnone"
, ("Number of " "call site" " marked '" "readnone" "'")};; ++
(NumIRCS_readnone); }
7187 else if (isAssumedReadOnly())
7188 STATS_DECLTRACK_CS_ATTR(readonly){ static llvm::Statistic NumIRCS_readonly = {"attributor", "NumIRCS_readonly"
, ("Number of " "call site" " marked '" "readonly" "'")};; ++
(NumIRCS_readonly); }
7189 else if (isAssumedWriteOnly())
7190 STATS_DECLTRACK_CS_ATTR(writeonly){ static llvm::Statistic NumIRCS_writeonly = {"attributor", "NumIRCS_writeonly"
, ("Number of " "call site" " marked '" "writeonly" "'")};; ++
(NumIRCS_writeonly); }
7191 }
7192};
7193
7194ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
7195
7196 // The current assumed state used to determine a change.
7197 auto AssumedState = getAssumed();
7198
7199 auto CheckRWInst = [&](Instruction &I) {
7200 // If the instruction has an own memory behavior state, use it to restrict
7201 // the local state. No further analysis is required as the other memory
7202 // state is as optimistic as it gets.
7203 if (const auto *CB = dyn_cast<CallBase>(&I)) {
7204 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
7205 *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
7206 intersectAssumedBits(MemBehaviorAA.getAssumed());
7207 return !isAtFixpoint();
7208 }
7209
7210 // Remove access kind modifiers if necessary.
7211 if (I.mayReadFromMemory())
7212 removeAssumedBits(NO_READS);
7213 if (I.mayWriteToMemory())
7214 removeAssumedBits(NO_WRITES);
7215 return !isAtFixpoint();
7216 };
7217
7218 bool UsedAssumedInformation = false;
7219 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
7220 UsedAssumedInformation))
7221 return indicatePessimisticFixpoint();
7222
7223 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7224 : ChangeStatus::UNCHANGED;
7225}
7226
7227ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
7228
7229 const IRPosition &IRP = getIRPosition();
7230 const IRPosition &FnPos = IRPosition::function_scope(IRP);
7231 AAMemoryBehavior::StateType &S = getState();
7232
7233 // First, check the function scope. We take the known information and we avoid
7234 // work if the assumed information implies the current assumed information for
7235 // this attribute. This is a valid for all but byval arguments.
7236 Argument *Arg = IRP.getAssociatedArgument();
7237 AAMemoryBehavior::base_t FnMemAssumedState =
7238 AAMemoryBehavior::StateType::getWorstState();
7239 if (!Arg || !Arg->hasByValAttr()) {
7240 const auto &FnMemAA =
7241 A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::OPTIONAL);
7242 FnMemAssumedState = FnMemAA.getAssumed();
7243 S.addKnownBits(FnMemAA.getKnown());
7244 if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
7245 return ChangeStatus::UNCHANGED;
7246 }
7247
7248 // The current assumed state used to determine a change.
7249 auto AssumedState = S.getAssumed();
7250
7251 // Make sure the value is not captured (except through "return"), if
7252 // it is, any information derived would be irrelevant anyway as we cannot
7253 // check the potential aliases introduced by the capture. However, no need
7254 // to fall back to anythign less optimistic than the function state.
7255 const auto &ArgNoCaptureAA =
7256 A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::OPTIONAL);
7257 if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
7258 S.intersectAssumedBits(FnMemAssumedState);
7259 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7260 : ChangeStatus::UNCHANGED;
7261 }
7262
7263 // Visit and expand uses until all are analyzed or a fixpoint is reached.
7264 auto UsePred = [&](const Use &U, bool &Follow) -> bool {
7265 Instruction *UserI = cast<Instruction>(U.getUser());
7266 LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserIdo { } while (false)
7267 << " \n")do { } while (false);
7268
7269 // Droppable users, e.g., llvm::assume does not actually perform any action.
7270 if (UserI->isDroppable())
7271 return true;
7272
7273 // Check if the users of UserI should also be visited.
7274 Follow = followUsersOfUseIn(A, U, UserI);
7275
7276 // If UserI might touch memory we analyze the use in detail.
7277 if (UserI->mayReadOrWriteMemory())
7278 analyzeUseIn(A, U, UserI);
7279
7280 return !isAtFixpoint();
7281 };
7282
7283 if (!A.checkForAllUses(UsePred, *this, getAssociatedValue()))
7284 return indicatePessimisticFixpoint();
7285
7286 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
7287 : ChangeStatus::UNCHANGED;
7288}
7289
7290bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use &U,
7291 const Instruction *UserI) {
7292 // The loaded value is unrelated to the pointer argument, no need to
7293 // follow the users of the load.
7294 if (isa<LoadInst>(UserI))
7295 return false;
7296
7297 // By default we follow all uses assuming UserI might leak information on U,
7298 // we have special handling for call sites operands though.
7299 const auto *CB = dyn_cast<CallBase>(UserI);
7300 if (!CB || !CB->isArgOperand(&U))
7301 return true;
7302
7303 // If the use is a call argument known not to be captured, the users of
7304 // the call do not need to be visited because they have to be unrelated to
7305 // the input. Note that this check is not trivial even though we disallow
7306 // general capturing of the underlying argument. The reason is that the
7307 // call might the argument "through return", which we allow and for which we
7308 // need to check call users.
7309 if (U.get()->getType()->isPointerTy()) {
7310 unsigned ArgNo = CB->getArgOperandNo(&U);
7311 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
7312 *this, IRPosition::callsite_argument(*CB, ArgNo), DepClassTy::OPTIONAL);
7313 return !ArgNoCaptureAA.isAssumedNoCapture();
7314 }
7315
7316 return true;
7317}
7318
7319void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use &U,
7320 const Instruction *UserI) {
7321 assert(UserI->mayReadOrWriteMemory())((void)0);
7322
7323 switch (UserI->getOpcode()) {
7324 default:
7325 // TODO: Handle all atomics and other side-effect operations we know of.
7326 break;
7327 case Instruction::Load:
7328 // Loads cause the NO_READS property to disappear.
7329 removeAssumedBits(NO_READS);
7330 return;
7331
7332 case Instruction::Store:
7333 // Stores cause the NO_WRITES property to disappear if the use is the
7334 // pointer operand. Note that we do assume that capturing was taken care of
7335 // somewhere else.
7336 if (cast<StoreInst>(UserI)->getPointerOperand() == U.get())
7337 removeAssumedBits(NO_WRITES);
7338 return;
7339
7340 case Instruction::Call:
7341 case Instruction::CallBr:
7342 case Instruction::Invoke: {
7343 // For call sites we look at the argument memory behavior attribute (this
7344 // could be recursive!) in order to restrict our own state.
7345 const auto *CB = cast<CallBase>(UserI);
7346
7347 // Give up on operand bundles.
7348 if (CB->isBundleOperand(&U)) {
7349 indicatePessimisticFixpoint();
7350 return;
7351 }
7352
7353 // Calling a function does read the function pointer, maybe write it if the
7354 // function is self-modifying.
7355 if (CB->isCallee(&U)) {
7356 removeAssumedBits(NO_READS);
7357 break;
7358 }
7359
7360 // Adjust the possible access behavior based on the information on the
7361 // argument.
7362 IRPosition Pos;
7363 if (U.get()->getType()->isPointerTy())
7364 Pos = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
7365 else
7366 Pos = IRPosition::callsite_function(*CB);
7367 const auto &MemBehaviorAA =
7368 A.getAAFor<AAMemoryBehavior>(*this, Pos, DepClassTy::OPTIONAL);
7369 // "assumed" has at most the same bits as the MemBehaviorAA assumed
7370 // and at least "known".
7371 intersectAssumedBits(MemBehaviorAA.getAssumed());
7372 return;
7373 }
7374 };
7375
7376 // Generally, look at the "may-properties" and adjust the assumed state if we
7377 // did not trigger special handling before.
7378 if (UserI->mayReadFromMemory())
7379 removeAssumedBits(NO_READS);
7380 if (UserI->mayWriteToMemory())
7381 removeAssumedBits(NO_WRITES);
7382}
7383
7384/// -------------------- Memory Locations Attributes ---------------------------
7385/// Includes read-none, argmemonly, inaccessiblememonly,
7386/// inaccessiblememorargmemonly
7387/// ----------------------------------------------------------------------------
7388
7389std::string AAMemoryLocation::getMemoryLocationsAsStr(
7390 AAMemoryLocation::MemoryLocationsKind MLK) {
7391 if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS))
7392 return "all memory";
7393 if (MLK == AAMemoryLocation::NO_LOCATIONS)
7394 return "no memory";
7395 std::string S = "memory:";
7396 if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM))
7397 S += "stack,";
7398 if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM))
7399 S += "constant,";
7400 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM))
7401 S += "internal global,";
7402 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM))
7403 S += "external global,";
7404 if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM))
7405 S += "argument,";
7406 if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM))
7407 S += "inaccessible,";
7408 if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM))
7409 S += "malloced,";
7410 if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM))
7411 S += "unknown,";
7412 S.pop_back();
7413 return S;
7414}
7415
7416namespace {
7417struct AAMemoryLocationImpl : public AAMemoryLocation {
7418
7419 AAMemoryLocationImpl(const IRPosition &IRP, Attributor &A)
7420 : AAMemoryLocation(IRP, A), Allocator(A.Allocator) {
7421 for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
7422 AccessKind2Accesses[u] = nullptr;
7423 }
7424
7425 ~AAMemoryLocationImpl() {
7426 // The AccessSets are allocated via a BumpPtrAllocator, we call
7427 // the destructor manually.
7428 for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
7429 if (AccessKind2Accesses[u])
7430 AccessKind2Accesses[u]->~AccessSet();
7431 }
7432
7433 /// See AbstractAttribute::initialize(...).
7434 void initialize(Attributor &A) override {
7435 intersectAssumedBits(BEST_STATE);
7436 getKnownStateFromValue(A, getIRPosition(), getState());
7437 AAMemoryLocation::initialize(A);
7438 }
7439
7440 /// Return the memory behavior information encoded in the IR for \p IRP.
7441 static void getKnownStateFromValue(Attributor &A, const IRPosition &IRP,
7442 BitIntegerState &State,
7443 bool IgnoreSubsumingPositions = false) {
7444 // For internal functions we ignore `argmemonly` and
7445 // `inaccessiblememorargmemonly` as we might break it via interprocedural
7446 // constant propagation. It is unclear if this is the best way but it is
7447 // unlikely this will cause real performance problems. If we are deriving
7448 // attributes for the anchor function we even remove the attribute in
7449 // addition to ignoring it.
7450 bool UseArgMemOnly = true;
7451 Function *AnchorFn = IRP.getAnchorScope();
7452 if (AnchorFn && A.isRunOn(*AnchorFn))
7453 UseArgMemOnly = !AnchorFn->hasLocalLinkage();
7454
7455 SmallVector<Attribute, 2> Attrs;
7456 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
7457 for (const Attribute &Attr : Attrs) {
7458 switch (Attr.getKindAsEnum()) {
7459 case Attribute::ReadNone:
7460 State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM);
7461 break;
7462 case Attribute::InaccessibleMemOnly:
7463 State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
7464 break;
7465 case Attribute::ArgMemOnly:
7466 if (UseArgMemOnly)
7467 State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true));
7468 else
7469 IRP.removeAttrs({Attribute::ArgMemOnly});
7470 break;
7471 case Attribute::InaccessibleMemOrArgMemOnly:
7472 if (UseArgMemOnly)
7473 State.addKnownBits(inverseLocation(
7474 NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
7475 else
7476 IRP.removeAttrs({Attribute::InaccessibleMemOrArgMemOnly});
7477 break;
7478 default:
7479 llvm_unreachable("Unexpected attribute!")__builtin_unreachable();
7480 }
7481 }
7482 }
7483
7484 /// See AbstractAttribute::getDeducedAttributes(...).
7485 void getDeducedAttributes(LLVMContext &Ctx,
7486 SmallVectorImpl<Attribute> &Attrs) const override {
7487 assert(Attrs.size() == 0)((void)0);
7488 if (isAssumedReadNone()) {
7489 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
7490 } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) {
7491 if (isAssumedInaccessibleMemOnly())
7492 Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly));
7493 else if (isAssumedArgMemOnly())
7494 Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly));
7495 else if (isAssumedInaccessibleOrArgMemOnly())
7496 Attrs.push_back(
7497 Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly));
7498 }
7499 assert(Attrs.size() <= 1)((void)0);
7500 }
7501
7502 /// See AbstractAttribute::manifest(...).
7503 ChangeStatus manifest(Attributor &A) override {
7504 const IRPosition &IRP = getIRPosition();
7505
7506 // Check if we would improve the existing attributes first.
7507 SmallVector<Attribute, 4> DeducedAttrs;
7508 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
7509 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
7510 return IRP.hasAttr(Attr.getKindAsEnum(),
7511 /* IgnoreSubsumingPositions */ true);
7512 }))
7513 return ChangeStatus::UNCHANGED;
7514
7515 // Clear existing attributes.
7516 IRP.removeAttrs(AttrKinds);
7517 if (isAssumedReadNone())
7518 IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds);
7519
7520 // Use the generic manifest method.
7521 return IRAttribute::manifest(A);
7522 }
7523
7524 /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...).
7525 bool checkForAllAccessesToMemoryKind(
7526 function_ref<bool(const Instruction *, const Value *, AccessKind,
7527 MemoryLocationsKind)>
7528 Pred,
7529 MemoryLocationsKind RequestedMLK) const override {
7530 if (!isValidState())
7531 return false;
7532
7533 MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation();
7534 if (AssumedMLK == NO_LOCATIONS)
7535 return true;
7536
7537 unsigned Idx = 0;
7538 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS;
7539 CurMLK *= 2, ++Idx) {
7540 if (CurMLK & RequestedMLK)
7541 continue;
7542
7543 if (const AccessSet *Accesses = AccessKind2Accesses[Idx])
7544 for (const AccessInfo &AI : *Accesses)
7545 if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK))
7546 return false;
7547 }
7548
7549 return true;
7550 }
7551
7552 ChangeStatus indicatePessimisticFixpoint() override {
7553 // If we give up and indicate a pessimistic fixpoint this instruction will
7554 // become an access for all potential access kinds:
7555 // TODO: Add pointers for argmemonly and globals to improve the results of
7556 // checkForAllAccessesToMemoryKind.
7557 bool Changed = false;
7558 MemoryLocationsKind KnownMLK = getKnown();
7559 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
7560 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
7561 if (!(CurMLK & KnownMLK))
7562 updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed,
7563 getAccessKindFromInst(I));
7564 return AAMemoryLocation::indicatePessimisticFixpoint();
7565 }
7566
7567protected:
7568 /// Helper struct to tie together an instruction that has a read or write
7569 /// effect with the pointer it accesses (if any).
7570 struct AccessInfo {
7571
7572 /// The instruction that caused the access.
7573 const Instruction *I;
7574
7575 /// The base pointer that is accessed, or null if unknown.
7576 const Value *Ptr;
7577
7578 /// The kind of access (read/write/read+write).
7579 AccessKind Kind;
7580
7581 bool operator==(const AccessInfo &RHS) const {
7582 return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind;
7583 }
7584 bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const {
7585 if (LHS.I != RHS.I)
7586 return LHS.I < RHS.I;
7587 if (LHS.Ptr != RHS.Ptr)
7588 return LHS.Ptr < RHS.Ptr;
7589 if (LHS.Kind != RHS.Kind)
7590 return LHS.Kind < RHS.Kind;
7591 return false;
7592 }
7593 };
7594
7595 /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the
7596 /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind.
7597 using AccessSet = SmallSet<AccessInfo, 2, AccessInfo>;
7598 AccessSet *AccessKind2Accesses[llvm::CTLog2<VALID_STATE>()];
7599
7600 /// Categorize the pointer arguments of CB that might access memory in
7601 /// AccessedLoc and update the state and access map accordingly.
7602 void
7603 categorizeArgumentPointerLocations(Attributor &A, CallBase &CB,
7604 AAMemoryLocation::StateType &AccessedLocs,
7605 bool &Changed);
7606
7607 /// Return the kind(s) of location that may be accessed by \p V.
7608 AAMemoryLocation::MemoryLocationsKind
7609 categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
7610
7611 /// Return the access kind as determined by \p I.
7612 AccessKind getAccessKindFromInst(const Instruction *I) {
7613 AccessKind AK = READ_WRITE;
7614 if (I) {
7615 AK = I->mayReadFromMemory() ? READ : NONE;
7616 AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE));
7617 }
7618 return AK;
7619 }
7620
7621 /// Update the state \p State and the AccessKind2Accesses given that \p I is
7622 /// an access of kind \p AK to a \p MLK memory location with the access
7623 /// pointer \p Ptr.
7624 void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
7625 MemoryLocationsKind MLK, const Instruction *I,
7626 const Value *Ptr, bool &Changed,
7627 AccessKind AK = READ_WRITE) {
7628
7629 assert(isPowerOf2_32(MLK) && "Expected a single location set!")((void)0);
7630 auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)];
7631 if (!Accesses)
7632 Accesses = new (Allocator) AccessSet();
7633 Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second;
7634 State.removeAssumedBits(MLK);
7635 }
7636
7637 /// Determine the underlying locations kinds for \p Ptr, e.g., globals or
7638 /// arguments, and update the state and access map accordingly.
7639 void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr,
7640 AAMemoryLocation::StateType &State, bool &Changed);
7641
7642 /// Used to allocate access sets.
7643 BumpPtrAllocator &Allocator;
7644
7645 /// The set of IR attributes AAMemoryLocation deals with.
7646 static const Attribute::AttrKind AttrKinds[4];
7647};
7648
7649const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = {
7650 Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly,
7651 Attribute::InaccessibleMemOrArgMemOnly};
7652
7653void AAMemoryLocationImpl::categorizePtrValue(
7654 Attributor &A, const Instruction &I, const Value &Ptr,
7655 AAMemoryLocation::StateType &State, bool &Changed) {
7656 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "do { } while (false)
7657 << Ptr << " ["do { } while (false)
7658 << getMemoryLocationsAsStr(State.getAssumed()) << "]\n")do { } while (false);
7659
7660 SmallVector<Value *, 8> Objects;
7661 if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, *this, &I)) {
7662 LLVM_DEBUG(do { } while (false)
7663 dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n")do { } while (false);
7664 updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed,
7665 getAccessKindFromInst(&I));
7666 return;
7667 }
7668
7669 for (Value *Obj : Objects) {
7670 // TODO: recognize the TBAA used for constant accesses.
7671 MemoryLocationsKind MLK = NO_LOCATIONS;
7672 assert(!isa<GEPOperator>(Obj) && "GEPs should have been stripped.")((void)0);
7673 if (isa<UndefValue>(Obj))
7674 continue;
7675 if (auto *Arg = dyn_cast<Argument>(Obj)) {
7676 if (Arg->hasByValAttr())
7677 MLK = NO_LOCAL_MEM;
7678 else
7679 MLK = NO_ARGUMENT_MEM;
7680 } else if (auto *GV = dyn_cast<GlobalValue>(Obj)) {
7681 // Reading constant memory is not treated as a read "effect" by the
7682 // function attr pass so we won't neither. Constants defined by TBAA are
7683 // similar. (We know we do not write it because it is constant.)
7684 if (auto *GVar = dyn_cast<GlobalVariable>(GV))
7685 if (GVar->isConstant())
7686 continue;
7687
7688 if (GV->hasLocalLinkage())
7689 MLK = NO_GLOBAL_INTERNAL_MEM;
7690 else
7691 MLK = NO_GLOBAL_EXTERNAL_MEM;
7692 } else if (isa<ConstantPointerNull>(Obj) &&
7693 !NullPointerIsDefined(getAssociatedFunction(),
7694 Ptr.getType()->getPointerAddressSpace())) {
7695 continue;
7696 } else if (isa<AllocaInst>(Obj)) {
7697 MLK = NO_LOCAL_MEM;
7698 } else if (const auto *CB = dyn_cast<CallBase>(Obj)) {
7699 const auto &NoAliasAA = A.getAAFor<AANoAlias>(
7700 *this, IRPosition::callsite_returned(*CB), DepClassTy::OPTIONAL);
7701 if (NoAliasAA.isAssumedNoAlias())
7702 MLK = NO_MALLOCED_MEM;
7703 else
7704 MLK = NO_UNKOWN_MEM;
7705 } else {
7706 MLK = NO_UNKOWN_MEM;
7707 }
7708
7709 assert(MLK != NO_LOCATIONS && "No location specified!")((void)0);
7710 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value can be categorized: "do { } while (false)
7711 << *Obj << " -> " << getMemoryLocationsAsStr(MLK)do { } while (false)
7712 << "\n")do { } while (false);
7713 updateStateAndAccessesMap(getState(), MLK, &I, Obj, Changed,
7714 getAccessKindFromInst(&I));
7715 }
7716
7717 LLVM_DEBUG(do { } while (false)
7718 dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: "do { } while (false)
7719 << getMemoryLocationsAsStr(State.getAssumed()) << "\n")do { } while (false);
7720}
7721
7722void AAMemoryLocationImpl::categorizeArgumentPointerLocations(
7723 Attributor &A, CallBase &CB, AAMemoryLocation::StateType &AccessedLocs,
7724 bool &Changed) {
7725 for (unsigned ArgNo = 0, E = CB.getNumArgOperands(); ArgNo < E; ++ArgNo) {
7726
7727 // Skip non-pointer arguments.
7728 const Value *ArgOp = CB.getArgOperand(ArgNo);
7729 if (!ArgOp->getType()->isPtrOrPtrVectorTy())
7730 continue;
7731
7732 // Skip readnone arguments.
7733 const IRPosition &ArgOpIRP = IRPosition::callsite_argument(CB, ArgNo);
7734 const auto &ArgOpMemLocationAA =
7735 A.getAAFor<AAMemoryBehavior>(*this, ArgOpIRP, DepClassTy::OPTIONAL);
7736
7737 if (ArgOpMemLocationAA.isAssumedReadNone())
7738 continue;
7739
7740 // Categorize potentially accessed pointer arguments as if there was an
7741 // access instruction with them as pointer.
7742 categorizePtrValue(A, CB, *ArgOp, AccessedLocs, Changed);
7743 }
7744}
7745
7746AAMemoryLocation::MemoryLocationsKind
7747AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
7748 bool &Changed) {
7749 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "do { } while (false)
7750 << I << "\n")do { } while (false);
7751
7752 AAMemoryLocation::StateType AccessedLocs;
7753 AccessedLocs.intersectAssumedBits(NO_LOCATIONS);
7754
7755 if (auto *CB = dyn_cast<CallBase>(&I)) {
7756
7757 // First check if we assume any memory is access is visible.
7758 const auto &CBMemLocationAA = A.getAAFor<AAMemoryLocation>(
7759 *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
7760 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << Ido { } while (false)
7761 << " [" << CBMemLocationAA << "]\n")do { } while (false);
7762
7763 if (CBMemLocationAA.isAssumedReadNone())
7764 return NO_LOCATIONS;
7765
7766 if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) {
7767 updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr,
7768 Changed, getAccessKindFromInst(&I));
7769 return AccessedLocs.getAssumed();
7770 }
7771
7772 uint32_t CBAssumedNotAccessedLocs =
7773 CBMemLocationAA.getAssumedNotAccessedLocation();
7774
7775 // Set the argmemonly and global bit as we handle them separately below.
7776 uint32_t CBAssumedNotAccessedLocsNoArgMem =
7777 CBAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM;
7778
7779 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
7780 if (CBAssumedNotAccessedLocsNoArgMem & CurMLK)
7781 continue;
7782 updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed,
7783 getAccessKindFromInst(&I));
7784 }
7785
7786 // Now handle global memory if it might be accessed. This is slightly tricky
7787 // as NO_GLOBAL_MEM has multiple bits set.
7788 bool HasGlobalAccesses = ((~CBAssumedNotAccessedLocs) & NO_GLOBAL_MEM);
7789 if (HasGlobalAccesses) {
7790 auto AccessPred = [&](const Instruction *, const Value *Ptr,
7791 AccessKind Kind, MemoryLocationsKind MLK) {
7792 updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed,
7793 getAccessKindFromInst(&I));
7794 return true;
7795 };
7796 if (!CBMemLocationAA.checkForAllAccessesToMemoryKind(
7797 AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false)))
7798 return AccessedLocs.getWorstState();
7799 }
7800
7801 LLVM_DEBUG(do { } while (false)
7802 dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "do { } while (false)
7803 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n")do { } while (false);
7804
7805 // Now handle argument memory if it might be accessed.
7806 bool HasArgAccesses = ((~CBAssumedNotAccessedLocs) & NO_ARGUMENT_MEM);
7807 if (HasArgAccesses)
7808 categorizeArgumentPointerLocations(A, *CB, AccessedLocs, Changed);
7809
7810 LLVM_DEBUG(do { } while (false)
7811 dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "do { } while (false)
7812 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n")do { } while (false);
7813
7814 return AccessedLocs.getAssumed();
7815 }
7816
7817 if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) {
7818 LLVM_DEBUG(do { } while (false)
7819 dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "do { } while (false)
7820 << I << " [" << *Ptr << "]\n")do { } while (false);
7821 categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed);
7822 return AccessedLocs.getAssumed();
7823 }
7824
7825 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "do { } while (false)
7826 << I << "\n")do { } while (false);
7827 updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed,
7828 getAccessKindFromInst(&I));
7829 return AccessedLocs.getAssumed();
7830}
7831
7832/// An AA to represent the memory behavior function attributes.
7833struct AAMemoryLocationFunction final : public AAMemoryLocationImpl {
7834 AAMemoryLocationFunction(const IRPosition &IRP, Attributor &A)
7835 : AAMemoryLocationImpl(IRP, A) {}
7836
7837 /// See AbstractAttribute::updateImpl(Attributor &A).
7838 virtual ChangeStatus updateImpl(Attributor &A) override {
7839
7840 const auto &MemBehaviorAA =
7841 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
7842 if (MemBehaviorAA.isAssumedReadNone()) {
7843 if (MemBehaviorAA.isKnownReadNone())
7844 return indicateOptimisticFixpoint();
7845 assert(isAssumedReadNone() &&((void)0)
7846 "AAMemoryLocation was not read-none but AAMemoryBehavior was!")((void)0);
7847 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
7848 return ChangeStatus::UNCHANGED;
7849 }
7850
7851 // The current assumed state used to determine a change.
7852 auto AssumedState = getAssumed();
7853 bool Changed = false;
7854
7855 auto CheckRWInst = [&](Instruction &I) {
7856 MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed);
7857 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << Ido { } while (false)
7858 << ": " << getMemoryLocationsAsStr(MLK) << "\n")do { } while (false);
7859 removeAssumedBits(inverseLocation(MLK, false, false));
7860 // Stop once only the valid bit set in the *not assumed location*, thus
7861 // once we don't actually exclude any memory locations in the state.
7862 return getAssumedNotAccessedLocation() != VALID_STATE;
7863 };
7864
7865 bool UsedAssumedInformation = false;
7866 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
7867 UsedAssumedInformation))
7868 return indicatePessimisticFixpoint();
7869
7870 Changed |= AssumedState != getAssumed();
7871 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
7872 }
7873
7874 /// See AbstractAttribute::trackStatistics()
7875 void trackStatistics() const override {
7876 if (isAssumedReadNone())
7877 STATS_DECLTRACK_FN_ATTR(readnone){ static llvm::Statistic NumIRFunction_readnone = {"attributor"
, "NumIRFunction_readnone", ("Number of " "functions" " marked '"
"readnone" "'")};; ++(NumIRFunction_readnone); }
7878 else if (isAssumedArgMemOnly())
7879 STATS_DECLTRACK_FN_ATTR(argmemonly){ static llvm::Statistic NumIRFunction_argmemonly = {"attributor"
, "NumIRFunction_argmemonly", ("Number of " "functions" " marked '"
"argmemonly" "'")};; ++(NumIRFunction_argmemonly); }
7880 else if (isAssumedInaccessibleMemOnly())
7881 STATS_DECLTRACK_FN_ATTR(inaccessiblememonly){ static llvm::Statistic NumIRFunction_inaccessiblememonly = {
"attributor", "NumIRFunction_inaccessiblememonly", ("Number of "
"functions" " marked '" "inaccessiblememonly" "'")};; ++(NumIRFunction_inaccessiblememonly
); }
7882 else if (isAssumedInaccessibleOrArgMemOnly())
7883 STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly){ static llvm::Statistic NumIRFunction_inaccessiblememorargmemonly
= {"attributor", "NumIRFunction_inaccessiblememorargmemonly"
, ("Number of " "functions" " marked '" "inaccessiblememorargmemonly"
"'")};; ++(NumIRFunction_inaccessiblememorargmemonly); }
7884 }
7885};
7886
7887/// AAMemoryLocation attribute for call sites.
7888struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
7889 AAMemoryLocationCallSite(const IRPosition &IRP, Attributor &A)
7890 : AAMemoryLocationImpl(IRP, A) {}
7891
7892 /// See AbstractAttribute::initialize(...).
7893 void initialize(Attributor &A) override {
7894 AAMemoryLocationImpl::initialize(A);
7895 Function *F = getAssociatedFunction();
7896 if (!F || F->isDeclaration())
7897 indicatePessimisticFixpoint();
7898 }
7899
7900 /// See AbstractAttribute::updateImpl(...).
7901 ChangeStatus updateImpl(Attributor &A) override {
7902 // TODO: Once we have call site specific value information we can provide
7903 // call site specific liveness liveness information and then it makes
7904 // sense to specialize attributes for call sites arguments instead of
7905 // redirecting requests to the callee argument.
7906 Function *F = getAssociatedFunction();
7907 const IRPosition &FnPos = IRPosition::function(*F);
7908 auto &FnAA =
7909 A.getAAFor<AAMemoryLocation>(*this, FnPos, DepClassTy::REQUIRED);
7910 bool Changed = false;
7911 auto AccessPred = [&](const Instruction *I, const Value *Ptr,
7912 AccessKind Kind, MemoryLocationsKind MLK) {
7913 updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed,
7914 getAccessKindFromInst(I));
7915 return true;
7916 };
7917 if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))
7918 return indicatePessimisticFixpoint();
7919 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
7920 }
7921
7922 /// See AbstractAttribute::trackStatistics()
7923 void trackStatistics() const override {
7924 if (isAssumedReadNone())
7925 STATS_DECLTRACK_CS_ATTR(readnone){ static llvm::Statistic NumIRCS_readnone = {"attributor", "NumIRCS_readnone"
, ("Number of " "call site" " marked '" "readnone" "'")};; ++
(NumIRCS_readnone); }
7926 }
7927};
7928
7929/// ------------------ Value Constant Range Attribute -------------------------
7930
7931struct AAValueConstantRangeImpl : AAValueConstantRange {
7932 using StateType = IntegerRangeState;
7933 AAValueConstantRangeImpl(const IRPosition &IRP, Attributor &A)
7934 : AAValueConstantRange(IRP, A) {}
7935
7936 /// See AbstractAttribute::initialize(..).
7937 void initialize(Attributor &A) override {
7938 if (A.hasSimplificationCallback(getIRPosition())) {
7939 indicatePessimisticFixpoint();
7940 return;
7941 }
7942
7943 // Intersect a range given by SCEV.
7944 intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
7945
7946 // Intersect a range given by LVI.
7947 intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
7948 }
7949
7950 /// See AbstractAttribute::getAsStr().
7951 const std::string getAsStr() const override {
7952 std::string Str;
7953 llvm::raw_string_ostream OS(Str);
7954 OS << "range(" << getBitWidth() << ")<";
7955 getKnown().print(OS);
7956 OS << " / ";
7957 getAssumed().print(OS);
7958 OS << ">";
7959 return OS.str();
7960 }
7961
7962 /// Helper function to get a SCEV expr for the associated value at program
7963 /// point \p I.
7964 const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
7965 if (!getAnchorScope())
7966 return nullptr;
7967
7968 ScalarEvolution *SE =
7969 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
7970 *getAnchorScope());
7971
7972 LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
7973 *getAnchorScope());
7974
7975 if (!SE || !LI)
7976 return nullptr;
7977
7978 const SCEV *S = SE->getSCEV(&getAssociatedValue());
7979 if (!I)
7980 return S;
7981
7982 return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
7983 }
7984
7985 /// Helper function to get a range from SCEV for the associated value at
7986 /// program point \p I.
7987 ConstantRange getConstantRangeFromSCEV(Attributor &A,
7988 const Instruction *I = nullptr) const {
7989 if (!getAnchorScope())
7990 return getWorstState(getBitWidth());
7991
7992 ScalarEvolution *SE =
7993 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
7994 *getAnchorScope());
7995
7996 const SCEV *S = getSCEV(A, I);
7997 if (!SE || !S)
7998 return getWorstState(getBitWidth());
7999
8000 return SE->getUnsignedRange(S);
8001 }
8002
8003 /// Helper function to get a range from LVI for the associated value at
8004 /// program point \p I.
8005 ConstantRange
8006 getConstantRangeFromLVI(Attributor &A,
8007 const Instruction *CtxI = nullptr) const {
8008 if (!getAnchorScope())
8009 return getWorstState(getBitWidth());
8010
8011 LazyValueInfo *LVI =
8012 A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
8013 *getAnchorScope());
8014
8015 if (!LVI || !CtxI)
8016 return getWorstState(getBitWidth());
8017 return LVI->getConstantRange(&getAssociatedValue(),
8018 const_cast<Instruction *>(CtxI));
8019 }
8020
8021 /// Return true if \p CtxI is valid for querying outside analyses.
8022 /// This basically makes sure we do not ask intra-procedural analysis
8023 /// about a context in the wrong function or a context that violates
8024 /// dominance assumptions they might have. The \p AllowAACtxI flag indicates
8025 /// if the original context of this AA is OK or should be considered invalid.
8026 bool isValidCtxInstructionForOutsideAnalysis(Attributor &A,
8027 const Instruction *CtxI,
8028 bool AllowAACtxI) const {
8029 if (!CtxI || (!AllowAACtxI && CtxI == getCtxI()))
8030 return false;
8031
8032 // Our context might be in a different function, neither intra-procedural
8033 // analysis (ScalarEvolution nor LazyValueInfo) can handle that.
8034 if (!AA::isValidInScope(getAssociatedValue(), CtxI->getFunction()))
8035 return false;
8036
8037 // If the context is not dominated by the value there are paths to the
8038 // context that do not define the value. This cannot be handled by
8039 // LazyValueInfo so we need to bail.
8040 if (auto *I = dyn_cast<Instruction>(&getAssociatedValue())) {
8041 InformationCache &InfoCache = A.getInfoCache();
8042 const DominatorTree *DT =
8043 InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
8044 *I->getFunction());
8045 return DT && DT->dominates(I, CtxI);
8046 }
8047
8048 return true;
8049 }
8050
8051 /// See AAValueConstantRange::getKnownConstantRange(..).
8052 ConstantRange
8053 getKnownConstantRange(Attributor &A,
8054 const Instruction *CtxI = nullptr) const override {
8055 if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
8056 /* AllowAACtxI */ false))
8057 return getKnown();
8058
8059 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
8060 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
8061 return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
8062 }
8063
8064 /// See AAValueConstantRange::getAssumedConstantRange(..).
8065 ConstantRange
8066 getAssumedConstantRange(Attributor &A,
8067 const Instruction *CtxI = nullptr) const override {
8068 // TODO: Make SCEV use Attributor assumption.
8069 // We may be able to bound a variable range via assumptions in
8070 // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
8071 // evolve to x^2 + x, then we can say that y is in [2, 12].
8072 if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
8073 /* AllowAACtxI */ false))
8074 return getAssumed();
8075
8076 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
8077 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
8078 return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
8079 }
8080
8081 /// Helper function to create MDNode for range metadata.
8082 static MDNode *
8083 getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
8084 const ConstantRange &AssumedConstantRange) {
8085 Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
8086 Ty, AssumedConstantRange.getLower())),
8087 ConstantAsMetadata::get(ConstantInt::get(
8088 Ty, AssumedConstantRange.getUpper()))};
8089 return MDNode::get(Ctx, LowAndHigh);
8090 }
8091
8092 /// Return true if \p Assumed is included in \p KnownRanges.
8093 static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
8094
8095 if (Assumed.isFullSet())
8096 return false;
8097
8098 if (!KnownRanges)
8099 return true;
8100
8101 // If multiple ranges are annotated in IR, we give up to annotate assumed
8102 // range for now.
8103
8104 // TODO: If there exists a known range which containts assumed range, we
8105 // can say assumed range is better.
8106 if (KnownRanges->getNumOperands() > 2)
8107 return false;
8108
8109 ConstantInt *Lower =
8110 mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
8111 ConstantInt *Upper =
8112 mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
8113
8114 ConstantRange Known(Lower->getValue(), Upper->getValue());
8115 return Known.contains(Assumed) && Known != Assumed;
8116 }
8117
8118 /// Helper function to set range metadata.
8119 static bool
8120 setRangeMetadataIfisBetterRange(Instruction *I,
8121 const ConstantRange &AssumedConstantRange) {
8122 auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
8123 if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
8124 if (!AssumedConstantRange.isEmptySet()) {
8125 I->setMetadata(LLVMContext::MD_range,
8126 getMDNodeForConstantRange(I->getType(), I->getContext(),
8127 AssumedConstantRange));
8128 return true;
8129 }
8130 }
8131 return false;
8132 }
8133
8134 /// See AbstractAttribute::manifest()
8135 ChangeStatus manifest(Attributor &A) override {
8136 ChangeStatus Changed = ChangeStatus::UNCHANGED;
8137 ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
8138 assert(!AssumedConstantRange.isFullSet() && "Invalid state")((void)0);
8139
8140 auto &V = getAssociatedValue();
8141 if (!AssumedConstantRange.isEmptySet() &&
8142 !AssumedConstantRange.isSingleElement()) {
8143 if (Instruction *I = dyn_cast<Instruction>(&V)) {
8144 assert(I == getCtxI() && "Should not annotate an instruction which is "((void)0)
8145 "not the context instruction")((void)0);
8146 if (isa<CallInst>(I) || isa<LoadInst>(I))
8147 if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
8148 Changed = ChangeStatus::CHANGED;
8149 }
8150 }
8151
8152 return Changed;
8153 }
8154};
8155
8156struct AAValueConstantRangeArgument final
8157 : AAArgumentFromCallSiteArguments<
8158 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
8159 true /* BridgeCallBaseContext */> {
8160 using Base = AAArgumentFromCallSiteArguments<
8161 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
8162 true /* BridgeCallBaseContext */>;
8163 AAValueConstantRangeArgument(const IRPosition &IRP, Attributor &A)
8164 : Base(IRP, A) {}
8165
8166 /// See AbstractAttribute::initialize(..).
8167 void initialize(Attributor &A) override {
8168 if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
8169 indicatePessimisticFixpoint();
8170 } else {
8171 Base::initialize(A);
8172 }
8173 }
8174
8175 /// See AbstractAttribute::trackStatistics()
8176 void trackStatistics() const override {
8177 STATS_DECLTRACK_ARG_ATTR(value_range){ static llvm::Statistic NumIRArguments_value_range = {"attributor"
, "NumIRArguments_value_range", ("Number of " "arguments" " marked '"
"value_range" "'")};; ++(NumIRArguments_value_range); }
8178 }
8179};
8180
8181struct AAValueConstantRangeReturned
8182 : AAReturnedFromReturnedValues<AAValueConstantRange,
8183 AAValueConstantRangeImpl,
8184 AAValueConstantRangeImpl::StateType,
8185 /* PropogateCallBaseContext */ true> {
8186 using Base =
8187 AAReturnedFromReturnedValues<AAValueConstantRange,
8188 AAValueConstantRangeImpl,
8189 AAValueConstantRangeImpl::StateType,
8190 /* PropogateCallBaseContext */ true>;
8191 AAValueConstantRangeReturned(const IRPosition &IRP, Attributor &A)
8192 : Base(IRP, A) {}
8193
8194 /// See AbstractAttribute::initialize(...).
8195 void initialize(Attributor &A) override {}
8196
8197 /// See AbstractAttribute::trackStatistics()
8198 void trackStatistics() const override {
8199 STATS_DECLTRACK_FNRET_ATTR(value_range){ static llvm::Statistic NumIRFunctionReturn_value_range = {"attributor"
, "NumIRFunctionReturn_value_range", ("Number of " "function returns"
" marked '" "value_range" "'")};; ++(NumIRFunctionReturn_value_range
); }
8200 }
8201};
8202
8203struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
8204 AAValueConstantRangeFloating(const IRPosition &IRP, Attributor &A)
8205 : AAValueConstantRangeImpl(IRP, A) {}
8206
8207 /// See AbstractAttribute::initialize(...).
8208 void initialize(Attributor &A) override {
8209 AAValueConstantRangeImpl::initialize(A);
8210 if (isAtFixpoint())
8211 return;
8212
8213 Value &V = getAssociatedValue();
8214
8215 if (auto *C = dyn_cast<ConstantInt>(&V)) {
8216 unionAssumed(ConstantRange(C->getValue()));
8217 indicateOptimisticFixpoint();
8218 return;
8219 }
8220
8221 if (isa<UndefValue>(&V)) {
8222 // Collapse the undef state to 0.
8223 unionAssumed(ConstantRange(APInt(getBitWidth(), 0)));
8224 indicateOptimisticFixpoint();
8225 return;
8226 }
8227
8228 if (isa<CallBase>(&V))
8229 return;
8230
8231 if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V))
8232 return;
8233
8234 // If it is a load instruction with range metadata, use it.
8235 if (LoadInst *LI = dyn_cast<LoadInst>(&V))
8236 if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
8237 intersectKnown(getConstantRangeFromMetadata(*RangeMD));
8238 return;
8239 }
8240
8241 // We can work with PHI and select instruction as we traverse their operands
8242 // during update.
8243 if (isa<SelectInst>(V) || isa<PHINode>(V))
8244 return;
8245
8246 // Otherwise we give up.
8247 indicatePessimisticFixpoint();
8248
8249 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "do { } while (false)
8250 << getAssociatedValue() << "\n")do { } while (false);
8251 }
8252
8253 bool calculateBinaryOperator(
8254 Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
8255 const Instruction *CtxI,
8256 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8257 Value *LHS = BinOp->getOperand(0);
8258 Value *RHS = BinOp->getOperand(1);
8259
8260 // Simplify the operands first.
8261 bool UsedAssumedInformation = false;
8262 const auto &SimplifiedLHS =
8263 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8264 *this, UsedAssumedInformation);
8265 if (!SimplifiedLHS.hasValue())
8266 return true;
8267 if (!SimplifiedLHS.getValue())
8268 return false;
8269 LHS = *SimplifiedLHS;
8270
8271 const auto &SimplifiedRHS =
8272 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8273 *this, UsedAssumedInformation);
8274 if (!SimplifiedRHS.hasValue())
8275 return true;
8276 if (!SimplifiedRHS.getValue())
8277 return false;
8278 RHS = *SimplifiedRHS;
8279
8280 // TODO: Allow non integers as well.
8281 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8282 return false;
8283
8284 auto &LHSAA = A.getAAFor<AAValueConstantRange>(
8285 *this, IRPosition::value(*LHS, getCallBaseContext()),
8286 DepClassTy::REQUIRED);
8287 QuerriedAAs.push_back(&LHSAA);
8288 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
8289
8290 auto &RHSAA = A.getAAFor<AAValueConstantRange>(
8291 *this, IRPosition::value(*RHS, getCallBaseContext()),
8292 DepClassTy::REQUIRED);
8293 QuerriedAAs.push_back(&RHSAA);
8294 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
8295
8296 auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
8297
8298 T.unionAssumed(AssumedRange);
8299
8300 // TODO: Track a known state too.
8301
8302 return T.isValidState();
8303 }
8304
8305 bool calculateCastInst(
8306 Attributor &A, CastInst *CastI, IntegerRangeState &T,
8307 const Instruction *CtxI,
8308 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8309 assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!")((void)0);
8310 // TODO: Allow non integers as well.
8311 Value *OpV = CastI->getOperand(0);
8312
8313 // Simplify the operand first.
8314 bool UsedAssumedInformation = false;
8315 const auto &SimplifiedOpV =
8316 A.getAssumedSimplified(IRPosition::value(*OpV, getCallBaseContext()),
8317 *this, UsedAssumedInformation);
8318 if (!SimplifiedOpV.hasValue())
8319 return true;
8320 if (!SimplifiedOpV.getValue())
8321 return false;
8322 OpV = *SimplifiedOpV;
8323
8324 if (!OpV->getType()->isIntegerTy())
8325 return false;
8326
8327 auto &OpAA = A.getAAFor<AAValueConstantRange>(
8328 *this, IRPosition::value(*OpV, getCallBaseContext()),
8329 DepClassTy::REQUIRED);
8330 QuerriedAAs.push_back(&OpAA);
8331 T.unionAssumed(
8332 OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
8333 return T.isValidState();
8334 }
8335
8336 bool
8337 calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
8338 const Instruction *CtxI,
8339 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
8340 Value *LHS = CmpI->getOperand(0);
8341 Value *RHS = CmpI->getOperand(1);
8342
8343 // Simplify the operands first.
8344 bool UsedAssumedInformation = false;
8345 const auto &SimplifiedLHS =
8346 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8347 *this, UsedAssumedInformation);
8348 if (!SimplifiedLHS.hasValue())
8349 return true;
8350 if (!SimplifiedLHS.getValue())
8351 return false;
8352 LHS = *SimplifiedLHS;
8353
8354 const auto &SimplifiedRHS =
8355 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8356 *this, UsedAssumedInformation);
8357 if (!SimplifiedRHS.hasValue())
8358 return true;
8359 if (!SimplifiedRHS.getValue())
8360 return false;
8361 RHS = *SimplifiedRHS;
8362
8363 // TODO: Allow non integers as well.
8364 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8365 return false;
8366
8367 auto &LHSAA = A.getAAFor<AAValueConstantRange>(
8368 *this, IRPosition::value(*LHS, getCallBaseContext()),
8369 DepClassTy::REQUIRED);
8370 QuerriedAAs.push_back(&LHSAA);
8371 auto &RHSAA = A.getAAFor<AAValueConstantRange>(
8372 *this, IRPosition::value(*RHS, getCallBaseContext()),
8373 DepClassTy::REQUIRED);
8374 QuerriedAAs.push_back(&RHSAA);
8375 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
8376 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
8377
8378 // If one of them is empty set, we can't decide.
8379 if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
8380 return true;
8381
8382 bool MustTrue = false, MustFalse = false;
8383
8384 auto AllowedRegion =
8385 ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
8386
8387 if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
8388 MustFalse = true;
8389
8390 if (LHSAARange.icmp(CmpI->getPredicate(), RHSAARange))
8391 MustTrue = true;
8392
8393 assert((!MustTrue || !MustFalse) &&((void)0)
8394 "Either MustTrue or MustFalse should be false!")((void)0);
8395
8396 if (MustTrue)
8397 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
8398 else if (MustFalse)
8399 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
8400 else
8401 T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
8402
8403 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAAdo { } while (false)
8404 << " " << RHSAA << "\n")do { } while (false);
8405
8406 // TODO: Track a known state too.
8407 return T.isValidState();
8408 }
8409
8410 /// See AbstractAttribute::updateImpl(...).
8411 ChangeStatus updateImpl(Attributor &A) override {
8412 auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
8413 IntegerRangeState &T, bool Stripped) -> bool {
8414 Instruction *I = dyn_cast<Instruction>(&V);
8415 if (!I || isa<CallBase>(I)) {
8416
8417 // Simplify the operand first.
8418 bool UsedAssumedInformation = false;
8419 const auto &SimplifiedOpV =
8420 A.getAssumedSimplified(IRPosition::value(V, getCallBaseContext()),
8421 *this, UsedAssumedInformation);
8422 if (!SimplifiedOpV.hasValue())
8423 return true;
8424 if (!SimplifiedOpV.getValue())
8425 return false;
8426 Value *VPtr = *SimplifiedOpV;
8427
8428 // If the value is not instruction, we query AA to Attributor.
8429 const auto &AA = A.getAAFor<AAValueConstantRange>(
8430 *this, IRPosition::value(*VPtr, getCallBaseContext()),
8431 DepClassTy::REQUIRED);
8432
8433 // Clamp operator is not used to utilize a program point CtxI.
8434 T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
8435
8436 return T.isValidState();
8437 }
8438
8439 SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
8440 if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
8441 if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
8442 return false;
8443 } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
8444 if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
8445 return false;
8446 } else if (auto *CastI = dyn_cast<CastInst>(I)) {
8447 if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
8448 return false;
8449 } else {
8450 // Give up with other instructions.
8451 // TODO: Add other instructions
8452
8453 T.indicatePessimisticFixpoint();
8454 return false;
8455 }
8456
8457 // Catch circular reasoning in a pessimistic way for now.
8458 // TODO: Check how the range evolves and if we stripped anything, see also
8459 // AADereferenceable or AAAlign for similar situations.
8460 for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
8461 if (QueriedAA != this)
8462 continue;
8463 // If we are in a stady state we do not need to worry.
8464 if (T.getAssumed() == getState().getAssumed())
8465 continue;
8466 T.indicatePessimisticFixpoint();
8467 }
8468
8469 return T.isValidState();
8470 };
8471
8472 IntegerRangeState T(getBitWidth());
8473
8474 if (!genericValueTraversal<IntegerRangeState>(A, getIRPosition(), *this, T,
8475 VisitValueCB, getCtxI(),
8476 /* UseValueSimplify */ false))
8477 return indicatePessimisticFixpoint();
8478
8479 return clampStateAndIndicateChange(getState(), T);
8480 }
8481
8482 /// See AbstractAttribute::trackStatistics()
8483 void trackStatistics() const override {
8484 STATS_DECLTRACK_FLOATING_ATTR(value_range){ static llvm::Statistic NumIRFloating_value_range = {"attributor"
, "NumIRFloating_value_range", ("Number of floating values known to be '"
"value_range" "'")};; ++(NumIRFloating_value_range); }
8485 }
8486};
8487
8488struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
8489 AAValueConstantRangeFunction(const IRPosition &IRP, Attributor &A)
8490 : AAValueConstantRangeImpl(IRP, A) {}
8491
8492 /// See AbstractAttribute::initialize(...).
8493 ChangeStatus updateImpl(Attributor &A) override {
8494 llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "__builtin_unreachable()
8495 "not be called")__builtin_unreachable();
8496 }
8497
8498 /// See AbstractAttribute::trackStatistics()
8499 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range){ static llvm::Statistic NumIRFunction_value_range = {"attributor"
, "NumIRFunction_value_range", ("Number of " "functions" " marked '"
"value_range" "'")};; ++(NumIRFunction_value_range); }
}
8500};
8501
8502struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
8503 AAValueConstantRangeCallSite(const IRPosition &IRP, Attributor &A)
8504 : AAValueConstantRangeFunction(IRP, A) {}
8505
8506 /// See AbstractAttribute::trackStatistics()
8507 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range){ static llvm::Statistic NumIRCS_value_range = {"attributor",
"NumIRCS_value_range", ("Number of " "call site" " marked '"
"value_range" "'")};; ++(NumIRCS_value_range); }
}
8508};
8509
8510struct AAValueConstantRangeCallSiteReturned
8511 : AACallSiteReturnedFromReturned<AAValueConstantRange,
8512 AAValueConstantRangeImpl,
8513 AAValueConstantRangeImpl::StateType,
8514 /* IntroduceCallBaseContext */ true> {
8515 AAValueConstantRangeCallSiteReturned(const IRPosition &IRP, Attributor &A)
8516 : AACallSiteReturnedFromReturned<AAValueConstantRange,
8517 AAValueConstantRangeImpl,
8518 AAValueConstantRangeImpl::StateType,
8519 /* IntroduceCallBaseContext */ true>(IRP,
8520 A) {
8521 }
8522
8523 /// See AbstractAttribute::initialize(...).
8524 void initialize(Attributor &A) override {
8525 // If it is a load instruction with range metadata, use the metadata.
8526 if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
8527 if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
8528 intersectKnown(getConstantRangeFromMetadata(*RangeMD));
8529
8530 AAValueConstantRangeImpl::initialize(A);
8531 }
8532
8533 /// See AbstractAttribute::trackStatistics()
8534 void trackStatistics() const override {
8535 STATS_DECLTRACK_CSRET_ATTR(value_range){ static llvm::Statistic NumIRCSReturn_value_range = {"attributor"
, "NumIRCSReturn_value_range", ("Number of " "call site returns"
" marked '" "value_range" "'")};; ++(NumIRCSReturn_value_range
); }
8536 }
8537};
8538struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
8539 AAValueConstantRangeCallSiteArgument(const IRPosition &IRP, Attributor &A)
8540 : AAValueConstantRangeFloating(IRP, A) {}
8541
8542 /// See AbstractAttribute::manifest()
8543 ChangeStatus manifest(Attributor &A) override {
8544 return ChangeStatus::UNCHANGED;
8545 }
8546
8547 /// See AbstractAttribute::trackStatistics()
8548 void trackStatistics() const override {
8549 STATS_DECLTRACK_CSARG_ATTR(value_range){ static llvm::Statistic NumIRCSArguments_value_range = {"attributor"
, "NumIRCSArguments_value_range", ("Number of " "call site arguments"
" marked '" "value_range" "'")};; ++(NumIRCSArguments_value_range
); }
8550 }
8551};
8552
8553/// ------------------ Potential Values Attribute -------------------------
8554
8555struct AAPotentialValuesImpl : AAPotentialValues {
8556 using StateType = PotentialConstantIntValuesState;
8557
8558 AAPotentialValuesImpl(const IRPosition &IRP, Attributor &A)
8559 : AAPotentialValues(IRP, A) {}
8560
8561 /// See AbstractAttribute::initialize(..).
8562 void initialize(Attributor &A) override {
8563 if (A.hasSimplificationCallback(getIRPosition()))
8564 indicatePessimisticFixpoint();
8565 else
8566 AAPotentialValues::initialize(A);
8567 }
8568
8569 /// See AbstractAttribute::getAsStr().
8570 const std::string getAsStr() const override {
8571 std::string Str;
8572 llvm::raw_string_ostream OS(Str);
8573 OS << getState();
8574 return OS.str();
8575 }
8576
8577 /// See AbstractAttribute::updateImpl(...).
8578 ChangeStatus updateImpl(Attributor &A) override {
8579 return indicatePessimisticFixpoint();
8580 }
8581};
8582
8583struct AAPotentialValuesArgument final
8584 : AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
8585 PotentialConstantIntValuesState> {
8586 using Base =
8587 AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
8588 PotentialConstantIntValuesState>;
8589 AAPotentialValuesArgument(const IRPosition &IRP, Attributor &A)
8590 : Base(IRP, A) {}
8591
8592 /// See AbstractAttribute::initialize(..).
8593 void initialize(Attributor &A) override {
8594 if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
8595 indicatePessimisticFixpoint();
8596 } else {
8597 Base::initialize(A);
8598 }
8599 }
8600
8601 /// See AbstractAttribute::trackStatistics()
8602 void trackStatistics() const override {
8603 STATS_DECLTRACK_ARG_ATTR(potential_values){ static llvm::Statistic NumIRArguments_potential_values = {"attributor"
, "NumIRArguments_potential_values", ("Number of " "arguments"
" marked '" "potential_values" "'")};; ++(NumIRArguments_potential_values
); }
8604 }
8605};
8606
8607struct AAPotentialValuesReturned
8608 : AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl> {
8609 using Base =
8610 AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl>;
8611 AAPotentialValuesReturned(const IRPosition &IRP, Attributor &A)
8612 : Base(IRP, A) {}
8613
8614 /// See AbstractAttribute::trackStatistics()
8615 void trackStatistics() const override {
8616 STATS_DECLTRACK_FNRET_ATTR(potential_values){ static llvm::Statistic NumIRFunctionReturn_potential_values
= {"attributor", "NumIRFunctionReturn_potential_values", ("Number of "
"function returns" " marked '" "potential_values" "'")};; ++
(NumIRFunctionReturn_potential_values); }
8617 }
8618};
8619
8620struct AAPotentialValuesFloating : AAPotentialValuesImpl {
8621 AAPotentialValuesFloating(const IRPosition &IRP, Attributor &A)
8622 : AAPotentialValuesImpl(IRP, A) {}
8623
8624 /// See AbstractAttribute::initialize(..).
8625 void initialize(Attributor &A) override {
8626 AAPotentialValuesImpl::initialize(A);
8627 if (isAtFixpoint())
8628 return;
8629
8630 Value &V = getAssociatedValue();
8631
8632 if (auto *C = dyn_cast<ConstantInt>(&V)) {
8633 unionAssumed(C->getValue());
8634 indicateOptimisticFixpoint();
8635 return;
8636 }
8637
8638 if (isa<UndefValue>(&V)) {
8639 unionAssumedWithUndef();
8640 indicateOptimisticFixpoint();
8641 return;
8642 }
8643
8644 if (isa<BinaryOperator>(&V) || isa<ICmpInst>(&V) || isa<CastInst>(&V))
8645 return;
8646
8647 if (isa<SelectInst>(V) || isa<PHINode>(V) || isa<LoadInst>(V))
8648 return;
8649
8650 indicatePessimisticFixpoint();
8651
8652 LLVM_DEBUG(dbgs() << "[AAPotentialValues] We give up: "do { } while (false)
8653 << getAssociatedValue() << "\n")do { } while (false);
8654 }
8655
8656 static bool calculateICmpInst(const ICmpInst *ICI, const APInt &LHS,
8657 const APInt &RHS) {
8658 ICmpInst::Predicate Pred = ICI->getPredicate();
8659 switch (Pred) {
8660 case ICmpInst::ICMP_UGT:
8661 return LHS.ugt(RHS);
8662 case ICmpInst::ICMP_SGT:
8663 return LHS.sgt(RHS);
8664 case ICmpInst::ICMP_EQ:
8665 return LHS.eq(RHS);
8666 case ICmpInst::ICMP_UGE:
8667 return LHS.uge(RHS);
8668 case ICmpInst::ICMP_SGE:
8669 return LHS.sge(RHS);
8670 case ICmpInst::ICMP_ULT:
8671 return LHS.ult(RHS);
8672 case ICmpInst::ICMP_SLT:
8673 return LHS.slt(RHS);
8674 case ICmpInst::ICMP_NE:
8675 return LHS.ne(RHS);
8676 case ICmpInst::ICMP_ULE:
8677 return LHS.ule(RHS);
8678 case ICmpInst::ICMP_SLE:
8679 return LHS.sle(RHS);
8680 default:
8681 llvm_unreachable("Invalid ICmp predicate!")__builtin_unreachable();
8682 }
8683 }
8684
8685 static APInt calculateCastInst(const CastInst *CI, const APInt &Src,
8686 uint32_t ResultBitWidth) {
8687 Instruction::CastOps CastOp = CI->getOpcode();
8688 switch (CastOp) {
8689 default:
8690 llvm_unreachable("unsupported or not integer cast")__builtin_unreachable();
8691 case Instruction::Trunc:
8692 return Src.trunc(ResultBitWidth);
8693 case Instruction::SExt:
8694 return Src.sext(ResultBitWidth);
8695 case Instruction::ZExt:
8696 return Src.zext(ResultBitWidth);
8697 case Instruction::BitCast:
8698 return Src;
8699 }
8700 }
8701
8702 static APInt calculateBinaryOperator(const BinaryOperator *BinOp,
8703 const APInt &LHS, const APInt &RHS,
8704 bool &SkipOperation, bool &Unsupported) {
8705 Instruction::BinaryOps BinOpcode = BinOp->getOpcode();
8706 // Unsupported is set to true when the binary operator is not supported.
8707 // SkipOperation is set to true when UB occur with the given operand pair
8708 // (LHS, RHS).
8709 // TODO: we should look at nsw and nuw keywords to handle operations
8710 // that create poison or undef value.
8711 switch (BinOpcode) {
8712 default:
8713 Unsupported = true;
8714 return LHS;
8715 case Instruction::Add:
8716 return LHS + RHS;
8717 case Instruction::Sub:
8718 return LHS - RHS;
8719 case Instruction::Mul:
8720 return LHS * RHS;
8721 case Instruction::UDiv:
8722 if (RHS.isNullValue()) {
8723 SkipOperation = true;
8724 return LHS;
8725 }
8726 return LHS.udiv(RHS);
8727 case Instruction::SDiv:
8728 if (RHS.isNullValue()) {
8729 SkipOperation = true;
8730 return LHS;
8731 }
8732 return LHS.sdiv(RHS);
8733 case Instruction::URem:
8734 if (RHS.isNullValue()) {
8735 SkipOperation = true;
8736 return LHS;
8737 }
8738 return LHS.urem(RHS);
8739 case Instruction::SRem:
8740 if (RHS.isNullValue()) {
8741 SkipOperation = true;
8742 return LHS;
8743 }
8744 return LHS.srem(RHS);
8745 case Instruction::Shl:
8746 return LHS.shl(RHS);
8747 case Instruction::LShr:
8748 return LHS.lshr(RHS);
8749 case Instruction::AShr:
8750 return LHS.ashr(RHS);
8751 case Instruction::And:
8752 return LHS & RHS;
8753 case Instruction::Or:
8754 return LHS | RHS;
8755 case Instruction::Xor:
8756 return LHS ^ RHS;
8757 }
8758 }
8759
8760 bool calculateBinaryOperatorAndTakeUnion(const BinaryOperator *BinOp,
8761 const APInt &LHS, const APInt &RHS) {
8762 bool SkipOperation = false;
8763 bool Unsupported = false;
8764 APInt Result =
8765 calculateBinaryOperator(BinOp, LHS, RHS, SkipOperation, Unsupported);
8766 if (Unsupported)
8767 return false;
8768 // If SkipOperation is true, we can ignore this operand pair (L, R).
8769 if (!SkipOperation)
8770 unionAssumed(Result);
8771 return isValidState();
8772 }
8773
8774 ChangeStatus updateWithICmpInst(Attributor &A, ICmpInst *ICI) {
8775 auto AssumedBefore = getAssumed();
8776 Value *LHS = ICI->getOperand(0);
8777 Value *RHS = ICI->getOperand(1);
8778
8779 // Simplify the operands first.
8780 bool UsedAssumedInformation = false;
8781 const auto &SimplifiedLHS =
8782 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8783 *this, UsedAssumedInformation);
8784 if (!SimplifiedLHS.hasValue())
8785 return ChangeStatus::UNCHANGED;
8786 if (!SimplifiedLHS.getValue())
8787 return indicatePessimisticFixpoint();
8788 LHS = *SimplifiedLHS;
8789
8790 const auto &SimplifiedRHS =
8791 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8792 *this, UsedAssumedInformation);
8793 if (!SimplifiedRHS.hasValue())
8794 return ChangeStatus::UNCHANGED;
8795 if (!SimplifiedRHS.getValue())
8796 return indicatePessimisticFixpoint();
8797 RHS = *SimplifiedRHS;
8798
8799 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8800 return indicatePessimisticFixpoint();
8801
8802 auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
8803 DepClassTy::REQUIRED);
8804 if (!LHSAA.isValidState())
8805 return indicatePessimisticFixpoint();
8806
8807 auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
8808 DepClassTy::REQUIRED);
8809 if (!RHSAA.isValidState())
8810 return indicatePessimisticFixpoint();
8811
8812 const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
8813 const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
8814
8815 // TODO: make use of undef flag to limit potential values aggressively.
8816 bool MaybeTrue = false, MaybeFalse = false;
8817 const APInt Zero(RHS->getType()->getIntegerBitWidth(), 0);
8818 if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
8819 // The result of any comparison between undefs can be soundly replaced
8820 // with undef.
8821 unionAssumedWithUndef();
8822 } else if (LHSAA.undefIsContained()) {
8823 for (const APInt &R : RHSAAPVS) {
8824 bool CmpResult = calculateICmpInst(ICI, Zero, R);
8825 MaybeTrue |= CmpResult;
8826 MaybeFalse |= !CmpResult;
8827 if (MaybeTrue & MaybeFalse)
8828 return indicatePessimisticFixpoint();
8829 }
8830 } else if (RHSAA.undefIsContained()) {
8831 for (const APInt &L : LHSAAPVS) {
8832 bool CmpResult = calculateICmpInst(ICI, L, Zero);
8833 MaybeTrue |= CmpResult;
8834 MaybeFalse |= !CmpResult;
8835 if (MaybeTrue & MaybeFalse)
8836 return indicatePessimisticFixpoint();
8837 }
8838 } else {
8839 for (const APInt &L : LHSAAPVS) {
8840 for (const APInt &R : RHSAAPVS) {
8841 bool CmpResult = calculateICmpInst(ICI, L, R);
8842 MaybeTrue |= CmpResult;
8843 MaybeFalse |= !CmpResult;
8844 if (MaybeTrue & MaybeFalse)
8845 return indicatePessimisticFixpoint();
8846 }
8847 }
8848 }
8849 if (MaybeTrue)
8850 unionAssumed(APInt(/* numBits */ 1, /* val */ 1));
8851 if (MaybeFalse)
8852 unionAssumed(APInt(/* numBits */ 1, /* val */ 0));
8853 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
8854 : ChangeStatus::CHANGED;
8855 }
8856
8857 ChangeStatus updateWithSelectInst(Attributor &A, SelectInst *SI) {
8858 auto AssumedBefore = getAssumed();
8859 Value *LHS = SI->getTrueValue();
8860 Value *RHS = SI->getFalseValue();
8861
8862 // Simplify the operands first.
8863 bool UsedAssumedInformation = false;
8864 const auto &SimplifiedLHS =
8865 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8866 *this, UsedAssumedInformation);
8867 if (!SimplifiedLHS.hasValue())
8868 return ChangeStatus::UNCHANGED;
8869 if (!SimplifiedLHS.getValue())
8870 return indicatePessimisticFixpoint();
8871 LHS = *SimplifiedLHS;
8872
8873 const auto &SimplifiedRHS =
8874 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8875 *this, UsedAssumedInformation);
8876 if (!SimplifiedRHS.hasValue())
8877 return ChangeStatus::UNCHANGED;
8878 if (!SimplifiedRHS.getValue())
8879 return indicatePessimisticFixpoint();
8880 RHS = *SimplifiedRHS;
8881
8882 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8883 return indicatePessimisticFixpoint();
8884
8885 Optional<Constant *> C = A.getAssumedConstant(*SI->getCondition(), *this,
8886 UsedAssumedInformation);
8887
8888 // Check if we only need one operand.
8889 bool OnlyLeft = false, OnlyRight = false;
8890 if (C.hasValue() && *C && (*C)->isOneValue())
8891 OnlyLeft = true;
8892 else if (C.hasValue() && *C && (*C)->isZeroValue())
8893 OnlyRight = true;
8894
8895 const AAPotentialValues *LHSAA = nullptr, *RHSAA = nullptr;
8896 if (!OnlyRight) {
8897 LHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
8898 DepClassTy::REQUIRED);
8899 if (!LHSAA->isValidState())
8900 return indicatePessimisticFixpoint();
8901 }
8902 if (!OnlyLeft) {
8903 RHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
8904 DepClassTy::REQUIRED);
8905 if (!RHSAA->isValidState())
8906 return indicatePessimisticFixpoint();
8907 }
8908
8909 if (!LHSAA || !RHSAA) {
8910 // select (true/false), lhs, rhs
8911 auto *OpAA = LHSAA ? LHSAA : RHSAA;
8912
8913 if (OpAA->undefIsContained())
8914 unionAssumedWithUndef();
8915 else
8916 unionAssumed(*OpAA);
8917
8918 } else if (LHSAA->undefIsContained() && RHSAA->undefIsContained()) {
8919 // select i1 *, undef , undef => undef
8920 unionAssumedWithUndef();
8921 } else {
8922 unionAssumed(*LHSAA);
8923 unionAssumed(*RHSAA);
8924 }
8925 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
8926 : ChangeStatus::CHANGED;
8927 }
8928
8929 ChangeStatus updateWithCastInst(Attributor &A, CastInst *CI) {
8930 auto AssumedBefore = getAssumed();
8931 if (!CI->isIntegerCast())
8932 return indicatePessimisticFixpoint();
8933 assert(CI->getNumOperands() == 1 && "Expected cast to be unary!")((void)0);
8934 uint32_t ResultBitWidth = CI->getDestTy()->getIntegerBitWidth();
8935 Value *Src = CI->getOperand(0);
8936
8937 // Simplify the operand first.
8938 bool UsedAssumedInformation = false;
8939 const auto &SimplifiedSrc =
8940 A.getAssumedSimplified(IRPosition::value(*Src, getCallBaseContext()),
8941 *this, UsedAssumedInformation);
8942 if (!SimplifiedSrc.hasValue())
8943 return ChangeStatus::UNCHANGED;
8944 if (!SimplifiedSrc.getValue())
8945 return indicatePessimisticFixpoint();
8946 Src = *SimplifiedSrc;
8947
8948 auto &SrcAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*Src),
8949 DepClassTy::REQUIRED);
8950 if (!SrcAA.isValidState())
8951 return indicatePessimisticFixpoint();
8952 const DenseSet<APInt> &SrcAAPVS = SrcAA.getAssumedSet();
8953 if (SrcAA.undefIsContained())
8954 unionAssumedWithUndef();
8955 else {
8956 for (const APInt &S : SrcAAPVS) {
8957 APInt T = calculateCastInst(CI, S, ResultBitWidth);
8958 unionAssumed(T);
8959 }
8960 }
8961 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
8962 : ChangeStatus::CHANGED;
8963 }
8964
8965 ChangeStatus updateWithBinaryOperator(Attributor &A, BinaryOperator *BinOp) {
8966 auto AssumedBefore = getAssumed();
8967 Value *LHS = BinOp->getOperand(0);
8968 Value *RHS = BinOp->getOperand(1);
8969
8970 // Simplify the operands first.
8971 bool UsedAssumedInformation = false;
8972 const auto &SimplifiedLHS =
8973 A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
8974 *this, UsedAssumedInformation);
8975 if (!SimplifiedLHS.hasValue())
8976 return ChangeStatus::UNCHANGED;
8977 if (!SimplifiedLHS.getValue())
8978 return indicatePessimisticFixpoint();
8979 LHS = *SimplifiedLHS;
8980
8981 const auto &SimplifiedRHS =
8982 A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
8983 *this, UsedAssumedInformation);
8984 if (!SimplifiedRHS.hasValue())
8985 return ChangeStatus::UNCHANGED;
8986 if (!SimplifiedRHS.getValue())
8987 return indicatePessimisticFixpoint();
8988 RHS = *SimplifiedRHS;
8989
8990 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
8991 return indicatePessimisticFixpoint();
8992
8993 auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
8994 DepClassTy::REQUIRED);
8995 if (!LHSAA.isValidState())
8996 return indicatePessimisticFixpoint();
8997
8998 auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
8999 DepClassTy::REQUIRED);
9000 if (!RHSAA.isValidState())
9001 return indicatePessimisticFixpoint();
9002
9003 const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
9004 const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
9005 const APInt Zero = APInt(LHS->getType()->getIntegerBitWidth(), 0);
9006
9007 // TODO: make use of undef flag to limit potential values aggressively.
9008 if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
9009 if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, Zero))
9010 return indicatePessimisticFixpoint();
9011 } else if (LHSAA.undefIsContained()) {
9012 for (const APInt &R : RHSAAPVS) {
9013 if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, R))
9014 return indicatePessimisticFixpoint();
9015 }
9016 } else if (RHSAA.undefIsContained()) {
9017 for (const APInt &L : LHSAAPVS) {
9018 if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, Zero))
9019 return indicatePessimisticFixpoint();
9020 }
9021 } else {
9022 for (const APInt &L : LHSAAPVS) {
9023 for (const APInt &R : RHSAAPVS) {
9024 if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, R))
9025 return indicatePessimisticFixpoint();
9026 }
9027 }
9028 }
9029 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9030 : ChangeStatus::CHANGED;
9031 }
9032
9033 ChangeStatus updateWithPHINode(Attributor &A, PHINode *PHI) {
9034 auto AssumedBefore = getAssumed();
9035 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
9036 Value *IncomingValue = PHI->getIncomingValue(u);
9037
9038 // Simplify the operand first.
9039 bool UsedAssumedInformation = false;
9040 const auto &SimplifiedIncomingValue = A.getAssumedSimplified(
9041 IRPosition::value(*IncomingValue, getCallBaseContext()), *this,
9042 UsedAssumedInformation);
9043 if (!SimplifiedIncomingValue.hasValue())
9044 continue;
9045 if (!SimplifiedIncomingValue.getValue())
9046 return indicatePessimisticFixpoint();
9047 IncomingValue = *SimplifiedIncomingValue;
9048
9049 auto &PotentialValuesAA = A.getAAFor<AAPotentialValues>(
9050 *this, IRPosition::value(*IncomingValue), DepClassTy::REQUIRED);
9051 if (!PotentialValuesAA.isValidState())
9052 return indicatePessimisticFixpoint();
9053 if (PotentialValuesAA.undefIsContained())
9054 unionAssumedWithUndef();
9055 else
9056 unionAssumed(PotentialValuesAA.getAssumed());
9057 }
9058 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9059 : ChangeStatus::CHANGED;
9060 }
9061
9062 ChangeStatus updateWithLoad(Attributor &A, LoadInst &L) {
9063 if (!L.getType()->isIntegerTy())
9064 return indicatePessimisticFixpoint();
9065
9066 auto Union = [&](Value &V) {
9067 if (isa<UndefValue>(V)) {
9068 unionAssumedWithUndef();
9069 return true;
9070 }
9071 if (ConstantInt *CI = dyn_cast<ConstantInt>(&V)) {
9072 unionAssumed(CI->getValue());
9073 return true;
9074 }
9075 return false;
9076 };
9077 auto AssumedBefore = getAssumed();
9078
9079 if (!AAValueSimplifyImpl::handleLoad(A, *this, L, Union))
9080 return indicatePessimisticFixpoint();
9081
9082 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9083 : ChangeStatus::CHANGED;
9084 }
9085
9086 /// See AbstractAttribute::updateImpl(...).
9087 ChangeStatus updateImpl(Attributor &A) override {
9088 Value &V = getAssociatedValue();
9089 Instruction *I = dyn_cast<Instruction>(&V);
9090
9091 if (auto *ICI = dyn_cast<ICmpInst>(I))
9092 return updateWithICmpInst(A, ICI);
9093
9094 if (auto *SI = dyn_cast<SelectInst>(I))
9095 return updateWithSelectInst(A, SI);
9096
9097 if (auto *CI = dyn_cast<CastInst>(I))
9098 return updateWithCastInst(A, CI);
9099
9100 if (auto *BinOp = dyn_cast<BinaryOperator>(I))
9101 return updateWithBinaryOperator(A, BinOp);
9102
9103 if (auto *PHI = dyn_cast<PHINode>(I))
9104 return updateWithPHINode(A, PHI);
9105
9106 if (auto *L = dyn_cast<LoadInst>(I))
9107 return updateWithLoad(A, *L);
9108
9109 return indicatePessimisticFixpoint();
9110 }
9111
9112 /// See AbstractAttribute::trackStatistics()
9113 void trackStatistics() const override {
9114 STATS_DECLTRACK_FLOATING_ATTR(potential_values){ static llvm::Statistic NumIRFloating_potential_values = {"attributor"
, "NumIRFloating_potential_values", ("Number of floating values known to be '"
"potential_values" "'")};; ++(NumIRFloating_potential_values
); }
9115 }
9116};
9117
9118struct AAPotentialValuesFunction : AAPotentialValuesImpl {
9119 AAPotentialValuesFunction(const IRPosition &IRP, Attributor &A)
9120 : AAPotentialValuesImpl(IRP, A) {}
9121
9122 /// See AbstractAttribute::initialize(...).
9123 ChangeStatus updateImpl(Attributor &A) override {
9124 llvm_unreachable("AAPotentialValues(Function|CallSite)::updateImpl will "__builtin_unreachable()
9125 "not be called")__builtin_unreachable();
9126 }
9127
9128 /// See AbstractAttribute::trackStatistics()
9129 void trackStatistics() const override {
9130 STATS_DECLTRACK_FN_ATTR(potential_values){ static llvm::Statistic NumIRFunction_potential_values = {"attributor"
, "NumIRFunction_potential_values", ("Number of " "functions"
" marked '" "potential_values" "'")};; ++(NumIRFunction_potential_values
); }
9131 }
9132};
9133
9134struct AAPotentialValuesCallSite : AAPotentialValuesFunction {
9135 AAPotentialValuesCallSite(const IRPosition &IRP, Attributor &A)
9136 : AAPotentialValuesFunction(IRP, A) {}
9137
9138 /// See AbstractAttribute::trackStatistics()
9139 void trackStatistics() const override {
9140 STATS_DECLTRACK_CS_ATTR(potential_values){ static llvm::Statistic NumIRCS_potential_values = {"attributor"
, "NumIRCS_potential_values", ("Number of " "call site" " marked '"
"potential_values" "'")};; ++(NumIRCS_potential_values); }
9141 }
9142};
9143
9144struct AAPotentialValuesCallSiteReturned
9145 : AACallSiteReturnedFromReturned<AAPotentialValues, AAPotentialValuesImpl> {
9146 AAPotentialValuesCallSiteReturned(const IRPosition &IRP, Attributor &A)
9147 : AACallSiteReturnedFromReturned<AAPotentialValues,
9148 AAPotentialValuesImpl>(IRP, A) {}
9149
9150 /// See AbstractAttribute::trackStatistics()
9151 void trackStatistics() const override {
9152 STATS_DECLTRACK_CSRET_ATTR(potential_values){ static llvm::Statistic NumIRCSReturn_potential_values = {"attributor"
, "NumIRCSReturn_potential_values", ("Number of " "call site returns"
" marked '" "potential_values" "'")};; ++(NumIRCSReturn_potential_values
); }
9153 }
9154};
9155
9156struct AAPotentialValuesCallSiteArgument : AAPotentialValuesFloating {
9157 AAPotentialValuesCallSiteArgument(const IRPosition &IRP, Attributor &A)
9158 : AAPotentialValuesFloating(IRP, A) {}
9159
9160 /// See AbstractAttribute::initialize(..).
9161 void initialize(Attributor &A) override {
9162 AAPotentialValuesImpl::initialize(A);
9163 if (isAtFixpoint())
9164 return;
9165
9166 Value &V = getAssociatedValue();
9167
9168 if (auto *C = dyn_cast<ConstantInt>(&V)) {
9169 unionAssumed(C->getValue());
9170 indicateOptimisticFixpoint();
9171 return;
9172 }
9173
9174 if (isa<UndefValue>(&V)) {
9175 unionAssumedWithUndef();
9176 indicateOptimisticFixpoint();
9177 return;
9178 }
9179 }
9180
9181 /// See AbstractAttribute::updateImpl(...).
9182 ChangeStatus updateImpl(Attributor &A) override {
9183 Value &V = getAssociatedValue();
9184 auto AssumedBefore = getAssumed();
9185 auto &AA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(V),
9186 DepClassTy::REQUIRED);
9187 const auto &S = AA.getAssumed();
9188 unionAssumed(S);
9189 return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
9190 : ChangeStatus::CHANGED;
9191 }
9192
9193 /// See AbstractAttribute::trackStatistics()
9194 void trackStatistics() const override {
9195 STATS_DECLTRACK_CSARG_ATTR(potential_values){ static llvm::Statistic NumIRCSArguments_potential_values = {
"attributor", "NumIRCSArguments_potential_values", ("Number of "
"call site arguments" " marked '" "potential_values" "'")};;
++(NumIRCSArguments_potential_values); }
9196 }
9197};
9198
9199/// ------------------------ NoUndef Attribute ---------------------------------
9200struct AANoUndefImpl : AANoUndef {
9201 AANoUndefImpl(const IRPosition &IRP, Attributor &A) : AANoUndef(IRP, A) {}
9202
9203 /// See AbstractAttribute::initialize(...).
9204 void initialize(Attributor &A) override {
9205 if (getIRPosition().hasAttr({Attribute::NoUndef})) {
9206 indicateOptimisticFixpoint();
9207 return;
9208 }
9209 Value &V = getAssociatedValue();
9210 if (isa<UndefValue>(V))
9211 indicatePessimisticFixpoint();
9212 else if (isa<FreezeInst>(V))
9213 indicateOptimisticFixpoint();
9214 else if (getPositionKind() != IRPosition::IRP_RETURNED &&
9215 isGuaranteedNotToBeUndefOrPoison(&V))
9216 indicateOptimisticFixpoint();
9217 else
9218 AANoUndef::initialize(A);
9219 }
9220
9221 /// See followUsesInMBEC
9222 bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
9223 AANoUndef::StateType &State) {
9224 const Value *UseV = U->get();
9225 const DominatorTree *DT = nullptr;
9226 AssumptionCache *AC = nullptr;
9227 InformationCache &InfoCache = A.getInfoCache();
9228 if (Function *F = getAnchorScope()) {
9229 DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
9230 AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
9231 }
9232 State.setKnown(isGuaranteedNotToBeUndefOrPoison(UseV, AC, I, DT));
9233 bool TrackUse = false;
9234 // Track use for instructions which must produce undef or poison bits when
9235 // at least one operand contains such bits.
9236 if (isa<CastInst>(*I) || isa<GetElementPtrInst>(*I))
9237 TrackUse = true;
9238 return TrackUse;
9239 }
9240
9241 /// See AbstractAttribute::getAsStr().
9242 const std::string getAsStr() const override {
9243 return getAssumed() ? "noundef" : "may-undef-or-poison";
9244 }
9245
9246 ChangeStatus manifest(Attributor &A) override {
9247 // We don't manifest noundef attribute for dead positions because the
9248 // associated values with dead positions would be replaced with undef
9249 // values.
9250 bool UsedAssumedInformation = false;
9251 if (A.isAssumedDead(getIRPosition(), nullptr, nullptr,
9252 UsedAssumedInformation))
9253 return ChangeStatus::UNCHANGED;
9254 // A position whose simplified value does not have any value is
9255 // considered to be dead. We don't manifest noundef in such positions for
9256 // the same reason above.
9257 if (!A.getAssumedSimplified(getIRPosition(), *this, UsedAssumedInformation)
9258 .hasValue())
9259 return ChangeStatus::UNCHANGED;
9260 return AANoUndef::manifest(A);
9261 }
9262};
9263
9264struct AANoUndefFloating : public AANoUndefImpl {
9265 AANoUndefFloating(const IRPosition &IRP, Attributor &A)
9266 : AANoUndefImpl(IRP, A) {}
9267
9268 /// See AbstractAttribute::initialize(...).
9269 void initialize(Attributor &A) override {
9270 AANoUndefImpl::initialize(A);
9271 if (!getState().isAtFixpoint())
9272 if (Instruction *CtxI = getCtxI())
9273 followUsesInMBEC(*this, A, getState(), *CtxI);
9274 }
9275
9276 /// See AbstractAttribute::updateImpl(...).
9277 ChangeStatus updateImpl(Attributor &A) override {
9278 auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
9279 AANoUndef::StateType &T, bool Stripped) -> bool {
9280 const auto &AA = A.getAAFor<AANoUndef>(*this, IRPosition::value(V),
9281 DepClassTy::REQUIRED);
9282 if (!Stripped && this == &AA) {
9283 T.indicatePessimisticFixpoint();
9284 } else {
9285 const AANoUndef::StateType &S =
9286 static_cast<const AANoUndef::StateType &>(AA.getState());
9287 T ^= S;
9288 }
9289 return T.isValidState();
9290 };
9291
9292 StateType T;
9293 if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
9294 VisitValueCB, getCtxI()))
9295 return indicatePessimisticFixpoint();
9296
9297 return clampStateAndIndicateChange(getState(), T);
9298 }
9299
9300 /// See AbstractAttribute::trackStatistics()
9301 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef){ static llvm::Statistic NumIRFunctionReturn_noundef = {"attributor"
, "NumIRFunctionReturn_noundef", ("Number of " "function returns"
" marked '" "noundef" "'")};; ++(NumIRFunctionReturn_noundef
); }
}
9302};
9303
9304struct AANoUndefReturned final
9305 : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl> {
9306 AANoUndefReturned(const IRPosition &IRP, Attributor &A)
9307 : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl>(IRP, A) {}
9308
9309 /// See AbstractAttribute::trackStatistics()
9310 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef){ static llvm::Statistic NumIRFunctionReturn_noundef = {"attributor"
, "NumIRFunctionReturn_noundef", ("Number of " "function returns"
" marked '" "noundef" "'")};; ++(NumIRFunctionReturn_noundef
); }
}
9311};
9312
9313struct AANoUndefArgument final
9314 : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl> {
9315 AANoUndefArgument(const IRPosition &IRP, Attributor &A)
9316 : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl>(IRP, A) {}
9317
9318 /// See AbstractAttribute::trackStatistics()
9319 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noundef){ static llvm::Statistic NumIRArguments_noundef = {"attributor"
, "NumIRArguments_noundef", ("Number of " "arguments" " marked '"
"noundef" "'")};; ++(NumIRArguments_noundef); }
}
9320};
9321
9322struct AANoUndefCallSiteArgument final : AANoUndefFloating {
9323 AANoUndefCallSiteArgument(const IRPosition &IRP, Attributor &A)
9324 : AANoUndefFloating(IRP, A) {}
9325
9326 /// See AbstractAttribute::trackStatistics()
9327 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noundef){ static llvm::Statistic NumIRCSArguments_noundef = {"attributor"
, "NumIRCSArguments_noundef", ("Number of " "call site arguments"
" marked '" "noundef" "'")};; ++(NumIRCSArguments_noundef); }
}
9328};
9329
9330struct AANoUndefCallSiteReturned final
9331 : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl> {
9332 AANoUndefCallSiteReturned(const IRPosition &IRP, Attributor &A)
9333 : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl>(IRP, A) {}
9334
9335 /// See AbstractAttribute::trackStatistics()
9336 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noundef){ static llvm::Statistic NumIRCSReturn_noundef = {"attributor"
, "NumIRCSReturn_noundef", ("Number of " "call site returns" " marked '"
"noundef" "'")};; ++(NumIRCSReturn_noundef); }
}
9337};
9338
9339struct AACallEdgesFunction : public AACallEdges {
9340 AACallEdgesFunction(const IRPosition &IRP, Attributor &A)
9341 : AACallEdges(IRP, A) {}
9342
9343 /// See AbstractAttribute::updateImpl(...).
9344 ChangeStatus updateImpl(Attributor &A) override {
9345 ChangeStatus Change = ChangeStatus::UNCHANGED;
9346 bool OldHasUnknownCallee = HasUnknownCallee;
9347 bool OldHasUnknownCalleeNonAsm = HasUnknownCalleeNonAsm;
9348
9349 auto AddCalledFunction = [&](Function *Fn) {
9350 if (CalledFunctions.insert(Fn)) {
9351 Change = ChangeStatus::CHANGED;
9352 LLVM_DEBUG(dbgs() << "[AACallEdges] New call edge: " << Fn->getName()do { } while (false)
9353 << "\n")do { } while (false);
9354 }
9355 };
9356
9357 auto VisitValue = [&](Value &V, const Instruction *CtxI, bool &HasUnknown,
9358 bool Stripped) -> bool {
9359 if (Function *Fn = dyn_cast<Function>(&V)) {
9360 AddCalledFunction(Fn);
9361 } else {
9362 LLVM_DEBUG(dbgs() << "[AACallEdges] Unrecognized value: " << V << "\n")do { } while (false);
9363 HasUnknown = true;
9364 HasUnknownCalleeNonAsm = true;
9365 }
9366
9367 // Explore all values.
9368 return true;
9369 };
9370
9371 // Process any value that we might call.
9372 auto ProcessCalledOperand = [&](Value *V, Instruction *Ctx) {
9373 if (!genericValueTraversal<bool>(A, IRPosition::value(*V), *this,
9374 HasUnknownCallee, VisitValue, nullptr,
9375 false)) {
9376 // If we haven't gone through all values, assume that there are unknown
9377 // callees.
9378 HasUnknownCallee = true;
9379 HasUnknownCalleeNonAsm = true;
9380 }
9381 };
9382
9383 auto ProcessCallInst = [&](Instruction &Inst) {
9384 CallBase &CB = static_cast<CallBase &>(Inst);
9385 if (CB.isInlineAsm()) {
9386 HasUnknownCallee = true;
9387 return true;
9388 }
9389
9390 // Process callee metadata if available.
9391 if (auto *MD = Inst.getMetadata(LLVMContext::MD_callees)) {
9392 for (auto &Op : MD->operands()) {
9393 Function *Callee = mdconst::extract_or_null<Function>(Op);
9394 if (Callee)
9395 AddCalledFunction(Callee);
9396 }
9397 // Callees metadata grantees that the called function is one of its
9398 // operands, So we are done.
9399 return true;
9400 }
9401
9402 // The most simple case.
9403 ProcessCalledOperand(CB.getCalledOperand(), &Inst);
9404
9405 // Process callback functions.
9406 SmallVector<const Use *, 4u> CallbackUses;
9407 AbstractCallSite::getCallbackUses(CB, CallbackUses);
9408 for (const Use *U : CallbackUses)
9409 ProcessCalledOperand(U->get(), &Inst);
9410
9411 return true;
9412 };
9413
9414 // Visit all callable instructions.
9415 bool UsedAssumedInformation = false;
9416 if (!A.checkForAllCallLikeInstructions(ProcessCallInst, *this,
9417 UsedAssumedInformation)) {
9418 // If we haven't looked at all call like instructions, assume that there
9419 // are unknown callees.
9420 HasUnknownCallee = true;
9421 HasUnknownCalleeNonAsm = true;
9422 }
9423
9424 // Track changes.
9425 if (OldHasUnknownCallee != HasUnknownCallee ||
9426 OldHasUnknownCalleeNonAsm != HasUnknownCalleeNonAsm)
9427 Change = ChangeStatus::CHANGED;
9428
9429 return Change;
9430 }
9431
9432 virtual const SetVector<Function *> &getOptimisticEdges() const override {
9433 return CalledFunctions;
9434 };
9435
9436 virtual bool hasUnknownCallee() const override { return HasUnknownCallee; }
9437
9438 virtual bool hasNonAsmUnknownCallee() const override {
9439 return HasUnknownCalleeNonAsm;
9440 }
9441
9442 const std::string getAsStr() const override {
9443 return "CallEdges[" + std::to_string(HasUnknownCallee) + "," +
9444 std::to_string(CalledFunctions.size()) + "]";
9445 }
9446
9447 void trackStatistics() const override {}
9448
9449 /// Optimistic set of functions that might be called by this function.
9450 SetVector<Function *> CalledFunctions;
9451
9452 /// Is there any call with a unknown callee.
9453 bool HasUnknownCallee = false;
9454
9455 /// Is there any call with a unknown callee, excluding any inline asm.
9456 bool HasUnknownCalleeNonAsm = false;
9457};
9458
9459struct AAFunctionReachabilityFunction : public AAFunctionReachability {
9460 AAFunctionReachabilityFunction(const IRPosition &IRP, Attributor &A)
9461 : AAFunctionReachability(IRP, A) {}
9462
9463 bool canReach(Attributor &A, Function *Fn) const override {
9464 // Assume that we can reach any function if we can reach a call with
9465 // unknown callee.
9466 if (CanReachUnknownCallee)
9467 return true;
9468
9469 if (ReachableQueries.count(Fn))
9470 return true;
9471
9472 if (UnreachableQueries.count(Fn))
9473 return false;
9474
9475 const AACallEdges &AAEdges =
9476 A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
9477
9478 const SetVector<Function *> &Edges = AAEdges.getOptimisticEdges();
9479 bool Result = checkIfReachable(A, Edges, Fn);
9480
9481 // Attributor returns attributes as const, so this function has to be
9482 // const for users of this attribute to use it without having to do
9483 // a const_cast.
9484 // This is a hack for us to be able to cache queries.
9485 auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
9486
9487 if (Result)
9488 NonConstThis->ReachableQueries.insert(Fn);
9489 else
9490 NonConstThis->UnreachableQueries.insert(Fn);
9491
9492 return Result;
9493 }
9494
9495 /// See AbstractAttribute::updateImpl(...).
9496 ChangeStatus updateImpl(Attributor &A) override {
9497 if (CanReachUnknownCallee)
9498 return ChangeStatus::UNCHANGED;
9499
9500 const AACallEdges &AAEdges =
9501 A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
9502 const SetVector<Function *> &Edges = AAEdges.getOptimisticEdges();
9503 ChangeStatus Change = ChangeStatus::UNCHANGED;
9504
9505 if (AAEdges.hasUnknownCallee()) {
9506 bool OldCanReachUnknown = CanReachUnknownCallee;
9507 CanReachUnknownCallee = true;
9508 return OldCanReachUnknown ? ChangeStatus::UNCHANGED
9509 : ChangeStatus::CHANGED;
9510 }
9511
9512 // Check if any of the unreachable functions become reachable.
9513 for (auto Current = UnreachableQueries.begin();
9514 Current != UnreachableQueries.end();) {
9515 if (!checkIfReachable(A, Edges, *Current)) {
9516 Current++;
9517 continue;
9518 }
9519 ReachableQueries.insert(*Current);
9520 UnreachableQueries.erase(*Current++);
9521 Change = ChangeStatus::CHANGED;
9522 }
9523
9524 return Change;
9525 }
9526
9527 const std::string getAsStr() const override {
9528 size_t QueryCount = ReachableQueries.size() + UnreachableQueries.size();
9529
9530 return "FunctionReachability [" + std::to_string(ReachableQueries.size()) +
9531 "," + std::to_string(QueryCount) + "]";
9532 }
9533
9534 void trackStatistics() const override {}
9535
9536private:
9537 bool canReachUnknownCallee() const override { return CanReachUnknownCallee; }
9538
9539 bool checkIfReachable(Attributor &A, const SetVector<Function *> &Edges,
9540 Function *Fn) const {
9541 if (Edges.count(Fn))
9542 return true;
9543
9544 for (Function *Edge : Edges) {
9545 // We don't need a dependency if the result is reachable.
9546 const AAFunctionReachability &EdgeReachability =
9547 A.getAAFor<AAFunctionReachability>(*this, IRPosition::function(*Edge),
9548 DepClassTy::NONE);
9549
9550 if (EdgeReachability.canReach(A, Fn))
9551 return true;
9552 }
9553 for (Function *Fn : Edges)
9554 A.getAAFor<AAFunctionReachability>(*this, IRPosition::function(*Fn),
9555 DepClassTy::REQUIRED);
9556
9557 return false;
9558 }
9559
9560 /// Set of functions that we know for sure is reachable.
9561 SmallPtrSet<Function *, 8> ReachableQueries;
9562
9563 /// Set of functions that are unreachable, but might become reachable.
9564 SmallPtrSet<Function *, 8> UnreachableQueries;
9565
9566 /// If we can reach a function with a call to a unknown function we assume
9567 /// that we can reach any function.
9568 bool CanReachUnknownCallee = false;
9569};
9570
9571} // namespace
9572
9573AACallGraphNode *AACallEdgeIterator::operator*() const {
9574 return static_cast<AACallGraphNode *>(const_cast<AACallEdges *>(
9575 &A.getOrCreateAAFor<AACallEdges>(IRPosition::function(**I))));
9576}
9577
9578void AttributorCallGraph::print() { llvm::WriteGraph(outs(), this); }
9579
9580const char AAReturnedValues::ID = 0;
9581const char AANoUnwind::ID = 0;
9582const char AANoSync::ID = 0;
9583const char AANoFree::ID = 0;
9584const char AANonNull::ID = 0;
9585const char AANoRecurse::ID = 0;
9586const char AAWillReturn::ID = 0;
9587const char AAUndefinedBehavior::ID = 0;
9588const char AANoAlias::ID = 0;
9589const char AAReachability::ID = 0;
9590const char AANoReturn::ID = 0;
9591const char AAIsDead::ID = 0;
9592const char AADereferenceable::ID = 0;
9593const char AAAlign::ID = 0;
9594const char AANoCapture::ID = 0;
9595const char AAValueSimplify::ID = 0;
9596const char AAHeapToStack::ID = 0;
9597const char AAPrivatizablePtr::ID = 0;
9598const char AAMemoryBehavior::ID = 0;
9599const char AAMemoryLocation::ID = 0;
9600const char AAValueConstantRange::ID = 0;
9601const char AAPotentialValues::ID = 0;
9602const char AANoUndef::ID = 0;
9603const char AACallEdges::ID = 0;
9604const char AAFunctionReachability::ID = 0;
9605const char AAPointerInfo::ID = 0;
9606
9607// Macro magic to create the static generator function for attributes that
9608// follow the naming scheme.
9609
9610#define SWITCH_PK_INV(CLASS, PK, POS_NAME) \
9611 case IRPosition::PK: \
9612 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!")__builtin_unreachable();
9613
9614#define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \
9615 case IRPosition::PK: \
9616 AA = new (A.Allocator) CLASS##SUFFIX(IRP, A); \
9617 ++NumAAs; \
9618 break;
9619
9620#define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
9621 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
9622 CLASS *AA = nullptr; \
9623 switch (IRP.getPositionKind()) { \
9624 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
9625 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
9626 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
9627 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
9628 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
9629 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
9630 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
9631 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
9632 } \
9633 return *AA; \
9634 }
9635
9636#define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
9637 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
9638 CLASS *AA = nullptr; \
9639 switch (IRP.getPositionKind()) { \
9640 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
9641 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \
9642 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
9643 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
9644 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
9645 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
9646 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
9647 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
9648 } \
9649 return *AA; \
9650 }
9651
9652#define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
9653 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
9654 CLASS *AA = nullptr; \
9655 switch (IRP.getPositionKind()) { \
9656 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
9657 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
9658 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
9659 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
9660 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
9661 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
9662 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
9663 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
9664 } \
9665 return *AA; \
9666 }
9667
9668#define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
9669 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
9670 CLASS *AA = nullptr; \
9671 switch (IRP.getPositionKind()) { \
9672 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
9673 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
9674 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
9675 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
9676 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
9677 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
9678 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
9679 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
9680 } \
9681 return *AA; \
9682 }
9683
9684#define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
9685 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
9686 CLASS *AA = nullptr; \
9687 switch (IRP.getPositionKind()) { \
9688 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
9689 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
9690 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
9691 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
9692 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
9693 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
9694 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
9695 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
9696 } \
9697 return *AA; \
9698 }
9699
9700CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
9701CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
9702CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
9703CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
9704CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
9705CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
9706CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation)
9707
9708CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
9709CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
9710CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
9711CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
9712CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
9713CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
9714CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
9715CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialValues)
9716CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUndef)
9717CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPointerInfo)
9718
9719CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
9720CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
9721CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
9722
9723CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
9724CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
9725CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
9726CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AACallEdges)
9727CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAFunctionReachability)
9728
9729CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
9730
9731#undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
9732#undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
9733#undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
9734#undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
9735#undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
9736#undef SWITCH_PK_CREATE
9737#undef SWITCH_PK_INV

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT/Optional.h

1//===- Optional.h - Simple variant for passing optional values --*- C++ -*-===//
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// This file provides Optional, a template class modeled in the spirit of
10// OCaml's 'opt' variant. The idea is to strongly type whether or not
11// a value can be optional.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_OPTIONAL_H
16#define LLVM_ADT_OPTIONAL_H
17
18#include "llvm/ADT/Hashing.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/STLForwardCompat.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/type_traits.h"
23#include <cassert>
24#include <memory>
25#include <new>
26#include <utility>
27
28namespace llvm {
29
30class raw_ostream;
31
32namespace optional_detail {
33
34/// Storage for any type.
35//
36// The specialization condition intentionally uses
37// llvm::is_trivially_copy_constructible instead of
38// std::is_trivially_copy_constructible. GCC versions prior to 7.4 may
39// instantiate the copy constructor of `T` when
40// std::is_trivially_copy_constructible is instantiated. This causes
41// compilation to fail if we query the trivially copy constructible property of
42// a class which is not copy constructible.
43//
44// The current implementation of OptionalStorage insists that in order to use
45// the trivial specialization, the value_type must be trivially copy
46// constructible and trivially copy assignable due to =default implementations
47// of the copy/move constructor/assignment. It does not follow that this is
48// necessarily the case std::is_trivially_copyable is true (hence the expanded
49// specialization condition).
50//
51// The move constructible / assignable conditions emulate the remaining behavior
52// of std::is_trivially_copyable.
53template <typename T, bool = (llvm::is_trivially_copy_constructible<T>::value &&
54 std::is_trivially_copy_assignable<T>::value &&
55 (std::is_trivially_move_constructible<T>::value ||
56 !std::is_move_constructible<T>::value) &&
57 (std::is_trivially_move_assignable<T>::value ||
58 !std::is_move_assignable<T>::value))>
59class OptionalStorage {
60 union {
61 char empty;
62 T value;
63 };
64 bool hasVal;
65
66public:
67 ~OptionalStorage() { reset(); }
68
69 constexpr OptionalStorage() noexcept : empty(), hasVal(false) {}
70
71 constexpr OptionalStorage(OptionalStorage const &other) : OptionalStorage() {
72 if (other.hasValue()) {
73 emplace(other.value);
74 }
75 }
76 constexpr OptionalStorage(OptionalStorage &&other) : OptionalStorage() {
77 if (other.hasValue()) {
78 emplace(std::move(other.value));
79 }
80 }
81
82 template <class... Args>
83 constexpr explicit OptionalStorage(in_place_t, Args &&... args)
84 : value(std::forward<Args>(args)...), hasVal(true) {}
85
86 void reset() noexcept {
87 if (hasVal) {
88 value.~T();
89 hasVal = false;
90 }
91 }
92
93 constexpr bool hasValue() const noexcept { return hasVal; }
94
95 T &getValue() LLVM_LVALUE_FUNCTION& noexcept {
96 assert(hasVal)((void)0);
97 return value;
98 }
99 constexpr T const &getValue() const LLVM_LVALUE_FUNCTION& noexcept {
100 assert(hasVal)((void)0);
101 return value;
102 }
103#if LLVM_HAS_RVALUE_REFERENCE_THIS1
104 T &&getValue() && noexcept {
105 assert(hasVal)((void)0);
106 return std::move(value);
107 }
108#endif
109
110 template <class... Args> void emplace(Args &&... args) {
111 reset();
112 ::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
113 hasVal = true;
114 }
115
116 OptionalStorage &operator=(T const &y) {
117 if (hasValue()) {
118 value = y;
119 } else {
120 ::new ((void *)std::addressof(value)) T(y);
121 hasVal = true;
122 }
123 return *this;
124 }
125 OptionalStorage &operator=(T &&y) {
126 if (hasValue()) {
127 value = std::move(y);
128 } else {
129 ::new ((void *)std::addressof(value)) T(std::move(y));
130 hasVal = true;
131 }
132 return *this;
133 }
134
135 OptionalStorage &operator=(OptionalStorage const &other) {
136 if (other.hasValue()) {
137 if (hasValue()) {
138 value = other.value;
139 } else {
140 ::new ((void *)std::addressof(value)) T(other.value);
141 hasVal = true;
142 }
143 } else {
144 reset();
145 }
146 return *this;
147 }
148
149 OptionalStorage &operator=(OptionalStorage &&other) {
150 if (other.hasValue()) {
151 if (hasValue()) {
152 value = std::move(other.value);
153 } else {
154 ::new ((void *)std::addressof(value)) T(std::move(other.value));
155 hasVal = true;
156 }
157 } else {
158 reset();
159 }
160 return *this;
161 }
162};
163
164template <typename T> class OptionalStorage<T, true> {
165 union {
166 char empty;
167 T value;
168 };
169 bool hasVal = false;
170
171public:
172 ~OptionalStorage() = default;
173
174 constexpr OptionalStorage() noexcept : empty{} {}
175
176 constexpr OptionalStorage(OptionalStorage const &other) = default;
177 constexpr OptionalStorage(OptionalStorage &&other) = default;
178
179 OptionalStorage &operator=(OptionalStorage const &other) = default;
180 OptionalStorage &operator=(OptionalStorage &&other) = default;
181
182 template <class... Args>
183 constexpr explicit OptionalStorage(in_place_t, Args &&... args)
184 : value(std::forward<Args>(args)...), hasVal(true) {}
185
186 void reset() noexcept {
187 if (hasVal) {
188 value.~T();
189 hasVal = false;
190 }
191 }
192
193 constexpr bool hasValue() const noexcept { return hasVal; }
15
Returning the value 1, which participates in a condition later
194
195 T &getValue() LLVM_LVALUE_FUNCTION& noexcept {
196 assert(hasVal)((void)0);
197 return value;
198 }
199 constexpr T const &getValue() const LLVM_LVALUE_FUNCTION& noexcept {
200 assert(hasVal)((void)0);
201 return value;
202 }
203#if LLVM_HAS_RVALUE_REFERENCE_THIS1
204 T &&getValue() && noexcept {
205 assert(hasVal)((void)0);
206 return std::move(value);
207 }
208#endif
209
210 template <class... Args> void emplace(Args &&... args) {
211 reset();
212 ::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
213 hasVal = true;
214 }
215
216 OptionalStorage &operator=(T const &y) {
217 if (hasValue()) {
218 value = y;
219 } else {
220 ::new ((void *)std::addressof(value)) T(y);
221 hasVal = true;
222 }
223 return *this;
224 }
225 OptionalStorage &operator=(T &&y) {
226 if (hasValue()) {
227 value = std::move(y);
228 } else {
229 ::new ((void *)std::addressof(value)) T(std::move(y));
230 hasVal = true;
231 }
232 return *this;
233 }
234};
235
236} // namespace optional_detail
237
238template <typename T> class Optional {
239 optional_detail::OptionalStorage<T> Storage;
240
241public:
242 using value_type = T;
243
244 constexpr Optional() {}
245 constexpr Optional(NoneType) {}
246
247 constexpr Optional(const T &y) : Storage(in_place, y) {}
248 constexpr Optional(const Optional &O) = default;
249
250 constexpr Optional(T &&y) : Storage(in_place, std::move(y)) {}
251 constexpr Optional(Optional &&O) = default;
252
253 template <typename... ArgTypes>
254 constexpr Optional(in_place_t, ArgTypes &&...Args)
255 : Storage(in_place, std::forward<ArgTypes>(Args)...) {}
256
257 Optional &operator=(T &&y) {
258 Storage = std::move(y);
259 return *this;
260 }
261 Optional &operator=(Optional &&O) = default;
262
263 /// Create a new object by constructing it in place with the given arguments.
264 template <typename... ArgTypes> void emplace(ArgTypes &&... Args) {
265 Storage.emplace(std::forward<ArgTypes>(Args)...);
266 }
267
268 static constexpr Optional create(const T *y) {
269 return y ? Optional(*y) : Optional();
270 }
271
272 Optional &operator=(const T &y) {
273 Storage = y;
274 return *this;
275 }
276 Optional &operator=(const Optional &O) = default;
277
278 void reset() { Storage.reset(); }
279
280 constexpr const T *getPointer() const { return &Storage.getValue(); }
281 T *getPointer() { return &Storage.getValue(); }
282 constexpr const T &getValue() const LLVM_LVALUE_FUNCTION& {
283 return Storage.getValue();
284 }
285 T &getValue() LLVM_LVALUE_FUNCTION& { return Storage.getValue(); }
286
287 constexpr explicit operator bool() const { return hasValue(); }
13
Calling 'Optional::hasValue'
18
Returning from 'Optional::hasValue'
19
Returning the value 1, which participates in a condition later
288 constexpr bool hasValue() const { return Storage.hasValue(); }
14
Calling 'OptionalStorage::hasValue'
16
Returning from 'OptionalStorage::hasValue'
17
Returning the value 1, which participates in a condition later
289 constexpr const T *operator->() const { return getPointer(); }
290 T *operator->() { return getPointer(); }
291 constexpr const T &operator*() const LLVM_LVALUE_FUNCTION& {
292 return getValue();
293 }
294 T &operator*() LLVM_LVALUE_FUNCTION& { return getValue(); }
295
296 template <typename U>
297 constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION& {
298 return hasValue() ? getValue() : std::forward<U>(value);
299 }
300
301 /// Apply a function to the value if present; otherwise return None.
302 template <class Function>
303 auto map(const Function &F) const LLVM_LVALUE_FUNCTION&
304 -> Optional<decltype(F(getValue()))> {
305 if (*this) return F(getValue());
306 return None;
307 }
308
309#if LLVM_HAS_RVALUE_REFERENCE_THIS1
310 T &&getValue() && { return std::move(Storage.getValue()); }
311 T &&operator*() && { return std::move(Storage.getValue()); }
312
313 template <typename U>
314 T getValueOr(U &&value) && {
315 return hasValue() ? std::move(getValue()) : std::forward<U>(value);
316 }
317
318 /// Apply a function to the value if present; otherwise return None.
319 template <class Function>
320 auto map(const Function &F) &&
321 -> Optional<decltype(F(std::move(*this).getValue()))> {
322 if (*this) return F(std::move(*this).getValue());
323 return None;
324 }
325#endif
326};
327
328template <class T> llvm::hash_code hash_value(const Optional<T> &O) {
329 return O ? hash_combine(true, *O) : hash_value(false);
330}
331
332template <typename T, typename U>
333constexpr bool operator==(const Optional<T> &X, const Optional<U> &Y) {
334 if (X && Y)
335 return *X == *Y;
336 return X.hasValue() == Y.hasValue();
337}
338
339template <typename T, typename U>
340constexpr bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
341 return !(X == Y);
342}
343
344template <typename T, typename U>
345constexpr bool operator<(const Optional<T> &X, const Optional<U> &Y) {
346 if (X && Y)
347 return *X < *Y;
348 return X.hasValue() < Y.hasValue();
349}
350
351template <typename T, typename U>
352constexpr bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
353 return !(Y < X);
354}
355
356template <typename T, typename U>
357constexpr bool operator>(const Optional<T> &X, const Optional<U> &Y) {
358 return Y < X;
359}
360
361template <typename T, typename U>
362constexpr bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
363 return !(X < Y);
364}
365
366template <typename T>
367constexpr bool operator==(const Optional<T> &X, NoneType) {
368 return !X;
369}
370
371template <typename T>
372constexpr bool operator==(NoneType, const Optional<T> &X) {
373 return X == None;
374}
375
376template <typename T>
377constexpr bool operator!=(const Optional<T> &X, NoneType) {
378 return !(X == None);
379}
380
381template <typename T>
382constexpr bool operator!=(NoneType, const Optional<T> &X) {
383 return X != None;
384}
385
386template <typename T> constexpr bool operator<(const Optional<T> &, NoneType) {
387 return false;
388}
389
390template <typename T> constexpr bool operator<(NoneType, const Optional<T> &X) {
391 return X.hasValue();
392}
393
394template <typename T>
395constexpr bool operator<=(const Optional<T> &X, NoneType) {
396 return !(None < X);
397}
398
399template <typename T>
400constexpr bool operator<=(NoneType, const Optional<T> &X) {
401 return !(X < None);
402}
403
404template <typename T> constexpr bool operator>(const Optional<T> &X, NoneType) {
405 return None < X;
406}
407
408template <typename T> constexpr bool operator>(NoneType, const Optional<T> &X) {
409 return X < None;
410}
411
412template <typename T>
413constexpr bool operator>=(const Optional<T> &X, NoneType) {
414 return None <= X;
415}
416
417template <typename T>
418constexpr bool operator>=(NoneType, const Optional<T> &X) {
419 return X <= None;
420}
421
422template <typename T>
423constexpr bool operator==(const Optional<T> &X, const T &Y) {
424 return X && *X == Y;
425}
426
427template <typename T>
428constexpr bool operator==(const T &X, const Optional<T> &Y) {
429 return Y && X == *Y;
430}
431
432template <typename T>
433constexpr bool operator!=(const Optional<T> &X, const T &Y) {
434 return !(X == Y);
435}
436
437template <typename T>
438constexpr bool operator!=(const T &X, const Optional<T> &Y) {
439 return !(X == Y);
440}
441
442template <typename T>
443constexpr bool operator<(const Optional<T> &X, const T &Y) {
444 return !X || *X < Y;
445}
446
447template <typename T>
448constexpr bool operator<(const T &X, const Optional<T> &Y) {
449 return Y && X < *Y;
450}
451
452template <typename T>
453constexpr bool operator<=(const Optional<T> &X, const T &Y) {
454 return !(Y < X);
455}
456
457template <typename T>
458constexpr bool operator<=(const T &X, const Optional<T> &Y) {
459 return !(Y < X);
460}
461
462template <typename T>
463constexpr bool operator>(const Optional<T> &X, const T &Y) {
464 return Y < X;
465}
466
467template <typename T>
468constexpr bool operator>(const T &X, const Optional<T> &Y) {
469 return Y < X;
470}
471
472template <typename T>
473constexpr bool operator>=(const Optional<T> &X, const T &Y) {
474 return !(X < Y);
475}
476
477template <typename T>
478constexpr bool operator>=(const T &X, const Optional<T> &Y) {
479 return !(X < Y);
480}
481
482raw_ostream &operator<<(raw_ostream &OS, NoneType);
483
484template <typename T, typename = decltype(std::declval<raw_ostream &>()
485 << std::declval<const T &>())>
486raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) {
487 if (O)
488 OS << *O;
489 else
490 OS << None;
491 return OS;
492}
493
494} // end namespace llvm
495
496#endif // LLVM_ADT_OPTIONAL_H

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support/Alignment.h

1//===-- llvm/Support/Alignment.h - Useful alignment functions ---*- C++ -*-===//
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// This file contains types to represent alignments.
10// They are instrumented to guarantee some invariants are preserved and prevent
11// invalid manipulations.
12//
13// - Align represents an alignment in bytes, it is always set and always a valid
14// power of two, its minimum value is 1 which means no alignment requirements.
15//
16// - MaybeAlign is an optional type, it may be undefined or set. When it's set
17// you can get the underlying Align type by using the getValue() method.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_SUPPORT_ALIGNMENT_H_
22#define LLVM_SUPPORT_ALIGNMENT_H_
23
24#include "llvm/ADT/Optional.h"
25#include "llvm/Support/MathExtras.h"
26#include <cassert>
27#ifndef NDEBUG1
28#include <string>
29#endif // NDEBUG
30
31namespace llvm {
32
33#define ALIGN_CHECK_ISPOSITIVE(decl) \
34 assert(decl > 0 && (#decl " should be defined"))((void)0)
35
36/// This struct is a compact representation of a valid (non-zero power of two)
37/// alignment.
38/// It is suitable for use as static global constants.
39struct Align {
40private:
41 uint8_t ShiftValue = 0; /// The log2 of the required alignment.
42 /// ShiftValue is less than 64 by construction.
43
44 friend struct MaybeAlign;
45 friend unsigned Log2(Align);
46 friend bool operator==(Align Lhs, Align Rhs);
47 friend bool operator!=(Align Lhs, Align Rhs);
48 friend bool operator<=(Align Lhs, Align Rhs);
49 friend bool operator>=(Align Lhs, Align Rhs);
50 friend bool operator<(Align Lhs, Align Rhs);
51 friend bool operator>(Align Lhs, Align Rhs);
52 friend unsigned encode(struct MaybeAlign A);
53 friend struct MaybeAlign decodeMaybeAlign(unsigned Value);
54
55 /// A trivial type to allow construction of constexpr Align.
56 /// This is currently needed to workaround a bug in GCC 5.3 which prevents
57 /// definition of constexpr assign operators.
58 /// https://stackoverflow.com/questions/46756288/explicitly-defaulted-function-cannot-be-declared-as-constexpr-because-the-implic
59 /// FIXME: Remove this, make all assign operators constexpr and introduce user
60 /// defined literals when we don't have to support GCC 5.3 anymore.
61 /// https://llvm.org/docs/GettingStarted.html#getting-a-modern-host-c-toolchain
62 struct LogValue {
63 uint8_t Log;
64 };
65
66public:
67 /// Default is byte-aligned.
68 constexpr Align() = default;
69 /// Do not perform checks in case of copy/move construct/assign, because the
70 /// checks have been performed when building `Other`.
71 constexpr Align(const Align &Other) = default;
72 constexpr Align(Align &&Other) = default;
73 Align &operator=(const Align &Other) = default;
74 Align &operator=(Align &&Other) = default;
75
76 explicit Align(uint64_t Value) {
77 assert(Value > 0 && "Value must not be 0")((void)0);
78 assert(llvm::isPowerOf2_64(Value) && "Alignment is not a power of 2")((void)0);
79 ShiftValue = Log2_64(Value);
80 assert(ShiftValue < 64 && "Broken invariant")((void)0);
81 }
82
83 /// This is a hole in the type system and should not be abused.
84 /// Needed to interact with C for instance.
85 uint64_t value() const { return uint64_t(1) << ShiftValue; }
86
87 /// Allow constructions of constexpr Align.
88 template <size_t kValue> constexpr static LogValue Constant() {
89 return LogValue{static_cast<uint8_t>(CTLog2<kValue>())};
90 }
91
92 /// Allow constructions of constexpr Align from types.
93 /// Compile time equivalent to Align(alignof(T)).
94 template <typename T> constexpr static LogValue Of() {
95 return Constant<std::alignment_of<T>::value>();
96 }
97
98 /// Constexpr constructor from LogValue type.
99 constexpr Align(LogValue CA) : ShiftValue(CA.Log) {}
100};
101
102/// Treats the value 0 as a 1, so Align is always at least 1.
103inline Align assumeAligned(uint64_t Value) {
104 return Value ? Align(Value) : Align();
105}
106
107/// This struct is a compact representation of a valid (power of two) or
108/// undefined (0) alignment.
109struct MaybeAlign : public llvm::Optional<Align> {
110private:
111 using UP = llvm::Optional<Align>;
112
113public:
114 /// Default is undefined.
115 MaybeAlign() = default;
116 /// Do not perform checks in case of copy/move construct/assign, because the
117 /// checks have been performed when building `Other`.
118 MaybeAlign(const MaybeAlign &Other) = default;
119 MaybeAlign &operator=(const MaybeAlign &Other) = default;
120 MaybeAlign(MaybeAlign &&Other) = default;
121 MaybeAlign &operator=(MaybeAlign &&Other) = default;
122
123 /// Use llvm::Optional<Align> constructor.
124 using UP::UP;
125
126 explicit MaybeAlign(uint64_t Value) {
127 assert((Value == 0 || llvm::isPowerOf2_64(Value)) &&((void)0)
128 "Alignment is neither 0 nor a power of 2")((void)0);
129 if (Value)
130 emplace(Value);
131 }
132
133 /// For convenience, returns a valid alignment or 1 if undefined.
134 Align valueOrOne() const { return hasValue() ? getValue() : Align(); }
135};
136
137/// Checks that SizeInBytes is a multiple of the alignment.
138inline bool isAligned(Align Lhs, uint64_t SizeInBytes) {
139 return SizeInBytes % Lhs.value() == 0;
140}
141
142/// Checks that Addr is a multiple of the alignment.
143inline bool isAddrAligned(Align Lhs, const void *Addr) {
144 return isAligned(Lhs, reinterpret_cast<uintptr_t>(Addr));
145}
146
147/// Returns a multiple of A needed to store `Size` bytes.
148inline uint64_t alignTo(uint64_t Size, Align A) {
149 const uint64_t Value = A.value();
150 // The following line is equivalent to `(Size + Value - 1) / Value * Value`.
151
152 // The division followed by a multiplication can be thought of as a right
153 // shift followed by a left shift which zeros out the extra bits produced in
154 // the bump; `~(Value - 1)` is a mask where all those bits being zeroed out
155 // are just zero.
156
157 // Most compilers can generate this code but the pattern may be missed when
158 // multiple functions gets inlined.
159 return (Size + Value - 1) & ~(Value - 1U);
160}
161
162/// If non-zero \p Skew is specified, the return value will be a minimal integer
163/// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for
164/// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p
165/// Skew mod \p A'.
166///
167/// Examples:
168/// \code
169/// alignTo(5, Align(8), 7) = 7
170/// alignTo(17, Align(8), 1) = 17
171/// alignTo(~0LL, Align(8), 3) = 3
172/// \endcode
173inline uint64_t alignTo(uint64_t Size, Align A, uint64_t Skew) {
174 const uint64_t Value = A.value();
175 Skew %= Value;
176 return ((Size + Value - 1 - Skew) & ~(Value - 1U)) + Skew;
177}
178
179/// Returns a multiple of A needed to store `Size` bytes.
180/// Returns `Size` if current alignment is undefined.
181inline uint64_t alignTo(uint64_t Size, MaybeAlign A) {
182 return A ? alignTo(Size, A.getValue()) : Size;
183}
184
185/// Aligns `Addr` to `Alignment` bytes, rounding up.
186inline uintptr_t alignAddr(const void *Addr, Align Alignment) {
187 uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr);
188 assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >=((void)0)
189 ArithAddr &&((void)0)
190 "Overflow")((void)0);
191 return alignTo(ArithAddr, Alignment);
192}
193
194/// Returns the offset to the next integer (mod 2**64) that is greater than
195/// or equal to \p Value and is a multiple of \p Align.
196inline uint64_t offsetToAlignment(uint64_t Value, Align Alignment) {
197 return alignTo(Value, Alignment) - Value;
198}
199
200/// Returns the necessary adjustment for aligning `Addr` to `Alignment`
201/// bytes, rounding up.
202inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) {
203 return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment);
204}
205
206/// Returns the log2 of the alignment.
207inline unsigned Log2(Align A) { return A.ShiftValue; }
208
209/// Returns the alignment that satisfies both alignments.
210/// Same semantic as MinAlign.
211inline Align commonAlignment(Align A, Align B) { return std::min(A, B); }
212
213/// Returns the alignment that satisfies both alignments.
214/// Same semantic as MinAlign.
215inline Align commonAlignment(Align A, uint64_t Offset) {
216 return Align(MinAlign(A.value(), Offset));
217}
218
219/// Returns the alignment that satisfies both alignments.
220/// Same semantic as MinAlign.
221inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) {
222 return A && B ? commonAlignment(*A, *B) : A ? A : B;
223}
224
225/// Returns the alignment that satisfies both alignments.
226/// Same semantic as MinAlign.
227inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) {
228 return MaybeAlign(MinAlign((*A).value(), Offset));
229}
230
231/// Returns a representation of the alignment that encodes undefined as 0.
232inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; }
233
234/// Dual operation of the encode function above.
235inline MaybeAlign decodeMaybeAlign(unsigned Value) {
236 if (Value == 0)
237 return MaybeAlign();
238 Align Out;
239 Out.ShiftValue = Value - 1;
240 return Out;
241}
242
243/// Returns a representation of the alignment, the encoded value is positive by
244/// definition.
245inline unsigned encode(Align A) { return encode(MaybeAlign(A)); }
246
247/// Comparisons between Align and scalars. Rhs must be positive.
248inline bool operator==(Align Lhs, uint64_t Rhs) {
249 ALIGN_CHECK_ISPOSITIVE(Rhs);
250 return Lhs.value() == Rhs;
251}
252inline bool operator!=(Align Lhs, uint64_t Rhs) {
253 ALIGN_CHECK_ISPOSITIVE(Rhs);
254 return Lhs.value() != Rhs;
255}
256inline bool operator<=(Align Lhs, uint64_t Rhs) {
257 ALIGN_CHECK_ISPOSITIVE(Rhs);
258 return Lhs.value() <= Rhs;
22
Assuming the condition is false
23
Returning zero, which participates in a condition later
259}
260inline bool operator>=(Align Lhs, uint64_t Rhs) {
261 ALIGN_CHECK_ISPOSITIVE(Rhs);
262 return Lhs.value() >= Rhs;
263}
264inline bool operator<(Align Lhs, uint64_t Rhs) {
265 ALIGN_CHECK_ISPOSITIVE(Rhs);
266 return Lhs.value() < Rhs;
267}
268inline bool operator>(Align Lhs, uint64_t Rhs) {
269 ALIGN_CHECK_ISPOSITIVE(Rhs);
270 return Lhs.value() > Rhs;
271}
272
273/// Comparisons between MaybeAlign and scalars.
274inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) {
275 return Lhs ? (*Lhs).value() == Rhs : Rhs == 0;
276}
277inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) {
278 return Lhs ? (*Lhs).value() != Rhs : Rhs != 0;
279}
280
281/// Comparisons operators between Align.
282inline bool operator==(Align Lhs, Align Rhs) {
283 return Lhs.ShiftValue == Rhs.ShiftValue;
284}
285inline bool operator!=(Align Lhs, Align Rhs) {
286 return Lhs.ShiftValue != Rhs.ShiftValue;
287}
288inline bool operator<=(Align Lhs, Align Rhs) {
289 return Lhs.ShiftValue <= Rhs.ShiftValue;
290}
291inline bool operator>=(Align Lhs, Align Rhs) {
292 return Lhs.ShiftValue >= Rhs.ShiftValue;
293}
294inline bool operator<(Align Lhs, Align Rhs) {
295 return Lhs.ShiftValue < Rhs.ShiftValue;
296}
297inline bool operator>(Align Lhs, Align Rhs) {
298 return Lhs.ShiftValue > Rhs.ShiftValue;
299}
300
301// Don't allow relational comparisons with MaybeAlign.
302bool operator<=(Align Lhs, MaybeAlign Rhs) = delete;
303bool operator>=(Align Lhs, MaybeAlign Rhs) = delete;
304bool operator<(Align Lhs, MaybeAlign Rhs) = delete;
305bool operator>(Align Lhs, MaybeAlign Rhs) = delete;
306
307bool operator<=(MaybeAlign Lhs, Align Rhs) = delete;
308bool operator>=(MaybeAlign Lhs, Align Rhs) = delete;
309bool operator<(MaybeAlign Lhs, Align Rhs) = delete;
310bool operator>(MaybeAlign Lhs, Align Rhs) = delete;
311
312bool operator<=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
313bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
314bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
315bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
316
317inline Align operator*(Align Lhs, uint64_t Rhs) {
318 assert(Rhs > 0 && "Rhs must be positive")((void)0);
319 return Align(Lhs.value() * Rhs);
320}
321
322inline MaybeAlign operator*(MaybeAlign Lhs, uint64_t Rhs) {
323 assert(Rhs > 0 && "Rhs must be positive")((void)0);
324 return Lhs ? Lhs.getValue() * Rhs : MaybeAlign();
325}
326
327inline Align operator/(Align Lhs, uint64_t Divisor) {
328 assert(llvm::isPowerOf2_64(Divisor) &&((void)0)
329 "Divisor must be positive and a power of 2")((void)0);
330 assert(Lhs != 1 && "Can't halve byte alignment")((void)0);
331 return Align(Lhs.value() / Divisor);
332}
333
334inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) {
335 assert(llvm::isPowerOf2_64(Divisor) &&((void)0)
336 "Divisor must be positive and a power of 2")((void)0);
337 return Lhs ? Lhs.getValue() / Divisor : MaybeAlign();
338}
339
340inline Align max(MaybeAlign Lhs, Align Rhs) {
341 return Lhs && *Lhs > Rhs ? *Lhs : Rhs;
342}
343
344inline Align max(Align Lhs, MaybeAlign Rhs) {
345 return Rhs && *Rhs > Lhs ? *Rhs : Lhs;
346}
347
348#ifndef NDEBUG1
349// For usage in LLVM_DEBUG macros.
350inline std::string DebugStr(const Align &A) {
351 return std::to_string(A.value());
352}
353// For usage in LLVM_DEBUG macros.
354inline std::string DebugStr(const MaybeAlign &MA) {
355 if (MA)
356 return std::to_string(MA->value());
357 return "None";
358}
359#endif // NDEBUG
360
361#undef ALIGN_CHECK_ISPOSITIVE
362
363} // namespace llvm
364
365#endif // LLVM_SUPPORT_ALIGNMENT_H_

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis/ValueTracking.h

1//===- llvm/Analysis/ValueTracking.h - Walk computations --------*- C++ -*-===//
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// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_VALUETRACKING_H
15#define LLVM_ANALYSIS_VALUETRACKING_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/Optional.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/InstrTypes.h"
23#include "llvm/IR/Intrinsics.h"
24#include "llvm/IR/Operator.h"
25#include <cassert>
26#include <cstdint>
27
28namespace llvm {
29
30class AddOperator;
31class AllocaInst;
32class APInt;
33class AssumptionCache;
34class DominatorTree;
35class GEPOperator;
36class IntrinsicInst;
37class LoadInst;
38class WithOverflowInst;
39struct KnownBits;
40class Loop;
41class LoopInfo;
42class MDNode;
43class OptimizationRemarkEmitter;
44class StringRef;
45class TargetLibraryInfo;
46class Value;
47
48constexpr unsigned MaxAnalysisRecursionDepth = 6;
49
50 /// Determine which bits of V are known to be either zero or one and return
51 /// them in the KnownZero/KnownOne bit sets.
52 ///
53 /// This function is defined on values with integer type, values with pointer
54 /// type, and vectors of integers. In the case
55 /// where V is a vector, the known zero and known one values are the
56 /// same width as the vector element, and the bit is set only if it is true
57 /// for all of the elements in the vector.
58 void computeKnownBits(const Value *V, KnownBits &Known,
59 const DataLayout &DL, unsigned Depth = 0,
60 AssumptionCache *AC = nullptr,
61 const Instruction *CxtI = nullptr,
62 const DominatorTree *DT = nullptr,
63 OptimizationRemarkEmitter *ORE = nullptr,
64 bool UseInstrInfo = true);
65
66 /// Determine which bits of V are known to be either zero or one and return
67 /// them in the KnownZero/KnownOne bit sets.
68 ///
69 /// This function is defined on values with integer type, values with pointer
70 /// type, and vectors of integers. In the case
71 /// where V is a vector, the known zero and known one values are the
72 /// same width as the vector element, and the bit is set only if it is true
73 /// for all of the demanded elements in the vector.
74 void computeKnownBits(const Value *V, const APInt &DemandedElts,
75 KnownBits &Known, const DataLayout &DL,
76 unsigned Depth = 0, AssumptionCache *AC = nullptr,
77 const Instruction *CxtI = nullptr,
78 const DominatorTree *DT = nullptr,
79 OptimizationRemarkEmitter *ORE = nullptr,
80 bool UseInstrInfo = true);
81
82 /// Returns the known bits rather than passing by reference.
83 KnownBits computeKnownBits(const Value *V, const DataLayout &DL,
84 unsigned Depth = 0, AssumptionCache *AC = nullptr,
85 const Instruction *CxtI = nullptr,
86 const DominatorTree *DT = nullptr,
87 OptimizationRemarkEmitter *ORE = nullptr,
88 bool UseInstrInfo = true);
89
90 /// Returns the known bits rather than passing by reference.
91 KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
92 const DataLayout &DL, unsigned Depth = 0,
93 AssumptionCache *AC = nullptr,
94 const Instruction *CxtI = nullptr,
95 const DominatorTree *DT = nullptr,
96 OptimizationRemarkEmitter *ORE = nullptr,
97 bool UseInstrInfo = true);
98
99 /// Compute known bits from the range metadata.
100 /// \p KnownZero the set of bits that are known to be zero
101 /// \p KnownOne the set of bits that are known to be one
102 void computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
103 KnownBits &Known);
104
105 /// Return true if LHS and RHS have no common bits set.
106 bool haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
107 const DataLayout &DL,
108 AssumptionCache *AC = nullptr,
109 const Instruction *CxtI = nullptr,
110 const DominatorTree *DT = nullptr,
111 bool UseInstrInfo = true);
112
113 /// Return true if the given value is known to have exactly one bit set when
114 /// defined. For vectors return true if every element is known to be a power
115 /// of two when defined. Supports values with integer or pointer type and
116 /// vectors of integers. If 'OrZero' is set, then return true if the given
117 /// value is either a power of two or zero.
118 bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
119 bool OrZero = false, unsigned Depth = 0,
120 AssumptionCache *AC = nullptr,
121 const Instruction *CxtI = nullptr,
122 const DominatorTree *DT = nullptr,
123 bool UseInstrInfo = true);
124
125 bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI);
126
127 /// Return true if the given value is known to be non-zero when defined. For
128 /// vectors, return true if every element is known to be non-zero when
129 /// defined. For pointers, if the context instruction and dominator tree are
130 /// specified, perform context-sensitive analysis and return true if the
131 /// pointer couldn't possibly be null at the specified instruction.
132 /// Supports values with integer or pointer type and vectors of integers.
133 bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth = 0,
134 AssumptionCache *AC = nullptr,
135 const Instruction *CxtI = nullptr,
136 const DominatorTree *DT = nullptr,
137 bool UseInstrInfo = true);
138
139 /// Return true if the two given values are negation.
140 /// Currently can recoginze Value pair:
141 /// 1: <X, Y> if X = sub (0, Y) or Y = sub (0, X)
142 /// 2: <X, Y> if X = sub (A, B) and Y = sub (B, A)
143 bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW = false);
144
145 /// Returns true if the give value is known to be non-negative.
146 bool isKnownNonNegative(const Value *V, const DataLayout &DL,
147 unsigned Depth = 0,
148 AssumptionCache *AC = nullptr,
149 const Instruction *CxtI = nullptr,
150 const DominatorTree *DT = nullptr,
151 bool UseInstrInfo = true);
152
153 /// Returns true if the given value is known be positive (i.e. non-negative
154 /// and non-zero).
155 bool isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth = 0,
156 AssumptionCache *AC = nullptr,
157 const Instruction *CxtI = nullptr,
158 const DominatorTree *DT = nullptr,
159 bool UseInstrInfo = true);
160
161 /// Returns true if the given value is known be negative (i.e. non-positive
162 /// and non-zero).
163 bool isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth = 0,
164 AssumptionCache *AC = nullptr,
165 const Instruction *CxtI = nullptr,
166 const DominatorTree *DT = nullptr,
167 bool UseInstrInfo = true);
168
169 /// Return true if the given values are known to be non-equal when defined.
170 /// Supports scalar integer types only.
171 bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL,
172 AssumptionCache *AC = nullptr,
173 const Instruction *CxtI = nullptr,
174 const DominatorTree *DT = nullptr,
175 bool UseInstrInfo = true);
176
177 /// Return true if 'V & Mask' is known to be zero. We use this predicate to
178 /// simplify operations downstream. Mask is known to be zero for bits that V
179 /// cannot have.
180 ///
181 /// This function is defined on values with integer type, values with pointer
182 /// type, and vectors of integers. In the case
183 /// where V is a vector, the mask, known zero, and known one values are the
184 /// same width as the vector element, and the bit is set only if it is true
185 /// for all of the elements in the vector.
186 bool MaskedValueIsZero(const Value *V, const APInt &Mask,
187 const DataLayout &DL,
188 unsigned Depth = 0, AssumptionCache *AC = nullptr,
189 const Instruction *CxtI = nullptr,
190 const DominatorTree *DT = nullptr,
191 bool UseInstrInfo = true);
192
193 /// Return the number of times the sign bit of the register is replicated into
194 /// the other bits. We know that at least 1 bit is always equal to the sign
195 /// bit (itself), but other cases can give us information. For example,
196 /// immediately after an "ashr X, 2", we know that the top 3 bits are all
197 /// equal to each other, so we return 3. For vectors, return the number of
198 /// sign bits for the vector element with the mininum number of known sign
199 /// bits.
200 unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL,
201 unsigned Depth = 0, AssumptionCache *AC = nullptr,
202 const Instruction *CxtI = nullptr,
203 const DominatorTree *DT = nullptr,
204 bool UseInstrInfo = true);
205
206 /// This function computes the integer multiple of Base that equals V. If
207 /// successful, it returns true and returns the multiple in Multiple. If
208 /// unsuccessful, it returns false. Also, if V can be simplified to an
209 /// integer, then the simplified V is returned in Val. Look through sext only
210 /// if LookThroughSExt=true.
211 bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
212 bool LookThroughSExt = false,
213 unsigned Depth = 0);
214
215 /// Map a call instruction to an intrinsic ID. Libcalls which have equivalent
216 /// intrinsics are treated as-if they were intrinsics.
217 Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB,
218 const TargetLibraryInfo *TLI);
219
220 /// Return true if we can prove that the specified FP value is never equal to
221 /// -0.0.
222 bool CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI,
223 unsigned Depth = 0);
224
225 /// Return true if we can prove that the specified FP value is either NaN or
226 /// never less than -0.0.
227 ///
228 /// NaN --> true
229 /// +0 --> true
230 /// -0 --> true
231 /// x > +0 --> true
232 /// x < -0 --> false
233 bool CannotBeOrderedLessThanZero(const Value *V, const TargetLibraryInfo *TLI);
234
235 /// Return true if the floating-point scalar value is not an infinity or if
236 /// the floating-point vector value has no infinities. Return false if a value
237 /// could ever be infinity.
238 bool isKnownNeverInfinity(const Value *V, const TargetLibraryInfo *TLI,
239 unsigned Depth = 0);
240
241 /// Return true if the floating-point scalar value is not a NaN or if the
242 /// floating-point vector value has no NaN elements. Return false if a value
243 /// could ever be NaN.
244 bool isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI,
245 unsigned Depth = 0);
246
247 /// Return true if we can prove that the specified FP value's sign bit is 0.
248 ///
249 /// NaN --> true/false (depending on the NaN's sign bit)
250 /// +0 --> true
251 /// -0 --> false
252 /// x > +0 --> true
253 /// x < -0 --> false
254 bool SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI);
255
256 /// If the specified value can be set by repeating the same byte in memory,
257 /// return the i8 value that it is represented with. This is true for all i8
258 /// values obviously, but is also true for i32 0, i32 -1, i16 0xF0F0, double
259 /// 0.0 etc. If the value can't be handled with a repeated byte store (e.g.
260 /// i16 0x1234), return null. If the value is entirely undef and padding,
261 /// return undef.
262 Value *isBytewiseValue(Value *V, const DataLayout &DL);
263
264 /// Given an aggregate and an sequence of indices, see if the scalar value
265 /// indexed is already around as a register, for example if it were inserted
266 /// directly into the aggregate.
267 ///
268 /// If InsertBefore is not null, this function will duplicate (modified)
269 /// insertvalues when a part of a nested struct is extracted.
270 Value *FindInsertedValue(Value *V,
271 ArrayRef<unsigned> idx_range,
272 Instruction *InsertBefore = nullptr);
273
274 /// Analyze the specified pointer to see if it can be expressed as a base
275 /// pointer plus a constant offset. Return the base and offset to the caller.
276 ///
277 /// This is a wrapper around Value::stripAndAccumulateConstantOffsets that
278 /// creates and later unpacks the required APInt.
279 inline Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
280 const DataLayout &DL,
281 bool AllowNonInbounds = true) {
282 APInt OffsetAPInt(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
30
Called C++ object pointer is null
283 Value *Base =
284 Ptr->stripAndAccumulateConstantOffsets(DL, OffsetAPInt, AllowNonInbounds);
285
286 Offset = OffsetAPInt.getSExtValue();
287 return Base;
288 }
289 inline const Value *
290 GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
291 const DataLayout &DL,
292 bool AllowNonInbounds = true) {
293 return GetPointerBaseWithConstantOffset(const_cast<Value *>(Ptr), Offset, DL,
28
Passing null pointer value via 1st parameter 'Ptr'
29
Calling 'GetPointerBaseWithConstantOffset'
294 AllowNonInbounds);
295 }
296
297 /// Returns true if the GEP is based on a pointer to a string (array of
298 // \p CharSize integers) and is indexing into this string.
299 bool isGEPBasedOnPointerToString(const GEPOperator *GEP,
300 unsigned CharSize = 8);
301
302 /// Represents offset+length into a ConstantDataArray.
303 struct ConstantDataArraySlice {
304 /// ConstantDataArray pointer. nullptr indicates a zeroinitializer (a valid
305 /// initializer, it just doesn't fit the ConstantDataArray interface).
306 const ConstantDataArray *Array;
307
308 /// Slice starts at this Offset.
309 uint64_t Offset;
310
311 /// Length of the slice.
312 uint64_t Length;
313
314 /// Moves the Offset and adjusts Length accordingly.
315 void move(uint64_t Delta) {
316 assert(Delta < Length)((void)0);
317 Offset += Delta;
318 Length -= Delta;
319 }
320
321 /// Convenience accessor for elements in the slice.
322 uint64_t operator[](unsigned I) const {
323 return Array==nullptr ? 0 : Array->getElementAsInteger(I + Offset);
324 }
325 };
326
327 /// Returns true if the value \p V is a pointer into a ConstantDataArray.
328 /// If successful \p Slice will point to a ConstantDataArray info object
329 /// with an appropriate offset.
330 bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice,
331 unsigned ElementSize, uint64_t Offset = 0);
332
333 /// This function computes the length of a null-terminated C string pointed to
334 /// by V. If successful, it returns true and returns the string in Str. If
335 /// unsuccessful, it returns false. This does not include the trailing null
336 /// character by default. If TrimAtNul is set to false, then this returns any
337 /// trailing null characters as well as any other characters that come after
338 /// it.
339 bool getConstantStringInfo(const Value *V, StringRef &Str,
340 uint64_t Offset = 0, bool TrimAtNul = true);
341
342 /// If we can compute the length of the string pointed to by the specified
343 /// pointer, return 'len+1'. If we can't, return 0.
344 uint64_t GetStringLength(const Value *V, unsigned CharSize = 8);
345
346 /// This function returns call pointer argument that is considered the same by
347 /// aliasing rules. You CAN'T use it to replace one value with another. If
348 /// \p MustPreserveNullness is true, the call must preserve the nullness of
349 /// the pointer.
350 const Value *getArgumentAliasingToReturnedPointer(const CallBase *Call,
351 bool MustPreserveNullness);
352 inline Value *
353 getArgumentAliasingToReturnedPointer(CallBase *Call,
354 bool MustPreserveNullness) {
355 return const_cast<Value *>(getArgumentAliasingToReturnedPointer(
356 const_cast<const CallBase *>(Call), MustPreserveNullness));
357 }
358
359 /// {launder,strip}.invariant.group returns pointer that aliases its argument,
360 /// and it only captures pointer by returning it.
361 /// These intrinsics are not marked as nocapture, because returning is
362 /// considered as capture. The arguments are not marked as returned neither,
363 /// because it would make it useless. If \p MustPreserveNullness is true,
364 /// the intrinsic must preserve the nullness of the pointer.
365 bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
366 const CallBase *Call, bool MustPreserveNullness);
367
368 /// This method strips off any GEP address adjustments and pointer casts from
369 /// the specified value, returning the original object being addressed. Note
370 /// that the returned value has pointer type if the specified value does. If
371 /// the MaxLookup value is non-zero, it limits the number of instructions to
372 /// be stripped off.
373 const Value *getUnderlyingObject(const Value *V, unsigned MaxLookup = 6);
374 inline Value *getUnderlyingObject(Value *V, unsigned MaxLookup = 6) {
375 // Force const to avoid infinite recursion.
376 const Value *VConst = V;
377 return const_cast<Value *>(getUnderlyingObject(VConst, MaxLookup));
378 }
379
380 /// This method is similar to getUnderlyingObject except that it can
381 /// look through phi and select instructions and return multiple objects.
382 ///
383 /// If LoopInfo is passed, loop phis are further analyzed. If a pointer
384 /// accesses different objects in each iteration, we don't look through the
385 /// phi node. E.g. consider this loop nest:
386 ///
387 /// int **A;
388 /// for (i)
389 /// for (j) {
390 /// A[i][j] = A[i-1][j] * B[j]
391 /// }
392 ///
393 /// This is transformed by Load-PRE to stash away A[i] for the next iteration
394 /// of the outer loop:
395 ///
396 /// Curr = A[0]; // Prev_0
397 /// for (i: 1..N) {
398 /// Prev = Curr; // Prev = PHI (Prev_0, Curr)
399 /// Curr = A[i];
400 /// for (j: 0..N) {
401 /// Curr[j] = Prev[j] * B[j]
402 /// }
403 /// }
404 ///
405 /// Since A[i] and A[i-1] are independent pointers, getUnderlyingObjects
406 /// should not assume that Curr and Prev share the same underlying object thus
407 /// it shouldn't look through the phi above.
408 void getUnderlyingObjects(const Value *V,
409 SmallVectorImpl<const Value *> &Objects,
410 LoopInfo *LI = nullptr, unsigned MaxLookup = 6);
411
412 /// This is a wrapper around getUnderlyingObjects and adds support for basic
413 /// ptrtoint+arithmetic+inttoptr sequences.
414 bool getUnderlyingObjectsForCodeGen(const Value *V,
415 SmallVectorImpl<Value *> &Objects);
416
417 /// Returns unique alloca where the value comes from, or nullptr.
418 /// If OffsetZero is true check that V points to the begining of the alloca.
419 AllocaInst *findAllocaForValue(Value *V, bool OffsetZero = false);
420 inline const AllocaInst *findAllocaForValue(const Value *V,
421 bool OffsetZero = false) {
422 return findAllocaForValue(const_cast<Value *>(V), OffsetZero);
423 }
424
425 /// Return true if the only users of this pointer are lifetime markers.
426 bool onlyUsedByLifetimeMarkers(const Value *V);
427
428 /// Return true if the only users of this pointer are lifetime markers or
429 /// droppable instructions.
430 bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V);
431
432 /// Return true if speculation of the given load must be suppressed to avoid
433 /// ordering or interfering with an active sanitizer. If not suppressed,
434 /// dereferenceability and alignment must be proven separately. Note: This
435 /// is only needed for raw reasoning; if you use the interface below
436 /// (isSafeToSpeculativelyExecute), this is handled internally.
437 bool mustSuppressSpeculation(const LoadInst &LI);
438
439 /// Return true if the instruction does not have any effects besides
440 /// calculating the result and does not have undefined behavior.
441 ///
442 /// This method never returns true for an instruction that returns true for
443 /// mayHaveSideEffects; however, this method also does some other checks in
444 /// addition. It checks for undefined behavior, like dividing by zero or
445 /// loading from an invalid pointer (but not for undefined results, like a
446 /// shift with a shift amount larger than the width of the result). It checks
447 /// for malloc and alloca because speculatively executing them might cause a
448 /// memory leak. It also returns false for instructions related to control
449 /// flow, specifically terminators and PHI nodes.
450 ///
451 /// If the CtxI is specified this method performs context-sensitive analysis
452 /// and returns true if it is safe to execute the instruction immediately
453 /// before the CtxI.
454 ///
455 /// If the CtxI is NOT specified this method only looks at the instruction
456 /// itself and its operands, so if this method returns true, it is safe to
457 /// move the instruction as long as the correct dominance relationships for
458 /// the operands and users hold.
459 ///
460 /// This method can return true for instructions that read memory;
461 /// for such instructions, moving them may change the resulting value.
462 bool isSafeToSpeculativelyExecute(const Value *V,
463 const Instruction *CtxI = nullptr,
464 const DominatorTree *DT = nullptr,
465 const TargetLibraryInfo *TLI = nullptr);
466
467 /// Returns true if the result or effects of the given instructions \p I
468 /// depend on or influence global memory.
469 /// Memory dependence arises for example if the instruction reads from
470 /// memory or may produce effects or undefined behaviour. Memory dependent
471 /// instructions generally cannot be reorderd with respect to other memory
472 /// dependent instructions or moved into non-dominated basic blocks.
473 /// Instructions which just compute a value based on the values of their
474 /// operands are not memory dependent.
475 bool mayBeMemoryDependent(const Instruction &I);
476
477 /// Return true if it is an intrinsic that cannot be speculated but also
478 /// cannot trap.
479 bool isAssumeLikeIntrinsic(const Instruction *I);
480
481 /// Return true if it is valid to use the assumptions provided by an
482 /// assume intrinsic, I, at the point in the control-flow identified by the
483 /// context instruction, CxtI.
484 bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI,
485 const DominatorTree *DT = nullptr);
486
487 enum class OverflowResult {
488 /// Always overflows in the direction of signed/unsigned min value.
489 AlwaysOverflowsLow,
490 /// Always overflows in the direction of signed/unsigned max value.
491 AlwaysOverflowsHigh,
492 /// May or may not overflow.
493 MayOverflow,
494 /// Never overflows.
495 NeverOverflows,
496 };
497
498 OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
499 const Value *RHS,
500 const DataLayout &DL,
501 AssumptionCache *AC,
502 const Instruction *CxtI,
503 const DominatorTree *DT,
504 bool UseInstrInfo = true);
505 OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
506 const DataLayout &DL,
507 AssumptionCache *AC,
508 const Instruction *CxtI,
509 const DominatorTree *DT,
510 bool UseInstrInfo = true);
511 OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
512 const Value *RHS,
513 const DataLayout &DL,
514 AssumptionCache *AC,
515 const Instruction *CxtI,
516 const DominatorTree *DT,
517 bool UseInstrInfo = true);
518 OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS,
519 const DataLayout &DL,
520 AssumptionCache *AC = nullptr,
521 const Instruction *CxtI = nullptr,
522 const DominatorTree *DT = nullptr);
523 /// This version also leverages the sign bit of Add if known.
524 OverflowResult computeOverflowForSignedAdd(const AddOperator *Add,
525 const DataLayout &DL,
526 AssumptionCache *AC = nullptr,
527 const Instruction *CxtI = nullptr,
528 const DominatorTree *DT = nullptr);
529 OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS,
530 const DataLayout &DL,
531 AssumptionCache *AC,
532 const Instruction *CxtI,
533 const DominatorTree *DT);
534 OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS,
535 const DataLayout &DL,
536 AssumptionCache *AC,
537 const Instruction *CxtI,
538 const DominatorTree *DT);
539
540 /// Returns true if the arithmetic part of the \p WO 's result is
541 /// used only along the paths control dependent on the computation
542 /// not overflowing, \p WO being an <op>.with.overflow intrinsic.
543 bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO,
544 const DominatorTree &DT);
545
546
547 /// Determine the possible constant range of an integer or vector of integer
548 /// value. This is intended as a cheap, non-recursive check.
549 ConstantRange computeConstantRange(const Value *V, bool UseInstrInfo = true,
550 AssumptionCache *AC = nullptr,
551 const Instruction *CtxI = nullptr,
552 unsigned Depth = 0);
553
554 /// Return true if this function can prove that the instruction I will
555 /// always transfer execution to one of its successors (including the next
556 /// instruction that follows within a basic block). E.g. this is not
557 /// guaranteed for function calls that could loop infinitely.
558 ///
559 /// In other words, this function returns false for instructions that may
560 /// transfer execution or fail to transfer execution in a way that is not
561 /// captured in the CFG nor in the sequence of instructions within a basic
562 /// block.
563 ///
564 /// Undefined behavior is assumed not to happen, so e.g. division is
565 /// guaranteed to transfer execution to the following instruction even
566 /// though division by zero might cause undefined behavior.
567 bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I);
568
569 /// Returns true if this block does not contain a potential implicit exit.
570 /// This is equivelent to saying that all instructions within the basic block
571 /// are guaranteed to transfer execution to their successor within the basic
572 /// block. This has the same assumptions w.r.t. undefined behavior as the
573 /// instruction variant of this function.
574 bool isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB);
575
576 /// Return true if this function can prove that the instruction I
577 /// is executed for every iteration of the loop L.
578 ///
579 /// Note that this currently only considers the loop header.
580 bool isGuaranteedToExecuteForEveryIteration(const Instruction *I,
581 const Loop *L);
582
583 /// Return true if I yields poison or raises UB if any of its operands is
584 /// poison.
585 /// Formally, given I = `r = op v1 v2 .. vN`, propagatesPoison returns true
586 /// if, for all i, r is evaluated to poison or op raises UB if vi = poison.
587 /// If vi is a vector or an aggregate and r is a single value, any poison
588 /// element in vi should make r poison or raise UB.
589 /// To filter out operands that raise UB on poison, you can use
590 /// getGuaranteedNonPoisonOp.
591 bool propagatesPoison(const Operator *I);
592
593 /// Insert operands of I into Ops such that I will trigger undefined behavior
594 /// if I is executed and that operand has a poison value.
595 void getGuaranteedNonPoisonOps(const Instruction *I,
596 SmallPtrSetImpl<const Value *> &Ops);
597 /// Insert operands of I into Ops such that I will trigger undefined behavior
598 /// if I is executed and that operand is not a well-defined value
599 /// (i.e. has undef bits or poison).
600 void getGuaranteedWellDefinedOps(const Instruction *I,
601 SmallPtrSetImpl<const Value *> &Ops);
602
603 /// Return true if the given instruction must trigger undefined behavior
604 /// when I is executed with any operands which appear in KnownPoison holding
605 /// a poison value at the point of execution.
606 bool mustTriggerUB(const Instruction *I,
607 const SmallSet<const Value *, 16>& KnownPoison);
608
609 /// Return true if this function can prove that if Inst is executed
610 /// and yields a poison value or undef bits, then that will trigger
611 /// undefined behavior.
612 ///
613 /// Note that this currently only considers the basic block that is
614 /// the parent of Inst.
615 bool programUndefinedIfUndefOrPoison(const Instruction *Inst);
616 bool programUndefinedIfPoison(const Instruction *Inst);
617
618 /// canCreateUndefOrPoison returns true if Op can create undef or poison from
619 /// non-undef & non-poison operands.
620 /// For vectors, canCreateUndefOrPoison returns true if there is potential
621 /// poison or undef in any element of the result when vectors without
622 /// undef/poison poison are given as operands.
623 /// For example, given `Op = shl <2 x i32> %x, <0, 32>`, this function returns
624 /// true. If Op raises immediate UB but never creates poison or undef
625 /// (e.g. sdiv I, 0), canCreatePoison returns false.
626 ///
627 /// canCreatePoison returns true if Op can create poison from non-poison
628 /// operands.
629 bool canCreateUndefOrPoison(const Operator *Op);
630 bool canCreatePoison(const Operator *Op);
631
632 /// Return true if V is poison given that ValAssumedPoison is already poison.
633 /// For example, if ValAssumedPoison is `icmp X, 10` and V is `icmp X, 5`,
634 /// impliesPoison returns true.
635 bool impliesPoison(const Value *ValAssumedPoison, const Value *V);
636
637 /// Return true if this function can prove that V does not have undef bits
638 /// and is never poison. If V is an aggregate value or vector, check whether
639 /// all elements (except padding) are not undef or poison.
640 /// Note that this is different from canCreateUndefOrPoison because the
641 /// function assumes Op's operands are not poison/undef.
642 ///
643 /// If CtxI and DT are specified this method performs flow-sensitive analysis
644 /// and returns true if it is guaranteed to be never undef or poison
645 /// immediately before the CtxI.
646 bool isGuaranteedNotToBeUndefOrPoison(const Value *V,
647 AssumptionCache *AC = nullptr,
648 const Instruction *CtxI = nullptr,
649 const DominatorTree *DT = nullptr,
650 unsigned Depth = 0);
651 bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC = nullptr,
652 const Instruction *CtxI = nullptr,
653 const DominatorTree *DT = nullptr,
654 unsigned Depth = 0);
655
656 /// Specific patterns of select instructions we can match.
657 enum SelectPatternFlavor {
658 SPF_UNKNOWN = 0,
659 SPF_SMIN, /// Signed minimum
660 SPF_UMIN, /// Unsigned minimum
661 SPF_SMAX, /// Signed maximum
662 SPF_UMAX, /// Unsigned maximum
663 SPF_FMINNUM, /// Floating point minnum
664 SPF_FMAXNUM, /// Floating point maxnum
665 SPF_ABS, /// Absolute value
666 SPF_NABS /// Negated absolute value
667 };
668
669 /// Behavior when a floating point min/max is given one NaN and one
670 /// non-NaN as input.
671 enum SelectPatternNaNBehavior {
672 SPNB_NA = 0, /// NaN behavior not applicable.
673 SPNB_RETURNS_NAN, /// Given one NaN input, returns the NaN.
674 SPNB_RETURNS_OTHER, /// Given one NaN input, returns the non-NaN.
675 SPNB_RETURNS_ANY /// Given one NaN input, can return either (or
676 /// it has been determined that no operands can
677 /// be NaN).
678 };
679
680 struct SelectPatternResult {
681 SelectPatternFlavor Flavor;
682 SelectPatternNaNBehavior NaNBehavior; /// Only applicable if Flavor is
683 /// SPF_FMINNUM or SPF_FMAXNUM.
684 bool Ordered; /// When implementing this min/max pattern as
685 /// fcmp; select, does the fcmp have to be
686 /// ordered?
687
688 /// Return true if \p SPF is a min or a max pattern.
689 static bool isMinOrMax(SelectPatternFlavor SPF) {
690 return SPF != SPF_UNKNOWN && SPF != SPF_ABS && SPF != SPF_NABS;
691 }
692 };
693
694 /// Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind
695 /// and providing the out parameter results if we successfully match.
696 ///
697 /// For ABS/NABS, LHS will be set to the input to the abs idiom. RHS will be
698 /// the negation instruction from the idiom.
699 ///
700 /// If CastOp is not nullptr, also match MIN/MAX idioms where the type does
701 /// not match that of the original select. If this is the case, the cast
702 /// operation (one of Trunc,SExt,Zext) that must be done to transform the
703 /// type of LHS and RHS into the type of V is returned in CastOp.
704 ///
705 /// For example:
706 /// %1 = icmp slt i32 %a, i32 4
707 /// %2 = sext i32 %a to i64
708 /// %3 = select i1 %1, i64 %2, i64 4
709 ///
710 /// -> LHS = %a, RHS = i32 4, *CastOp = Instruction::SExt
711 ///
712 SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
713 Instruction::CastOps *CastOp = nullptr,
714 unsigned Depth = 0);
715
716 inline SelectPatternResult
717 matchSelectPattern(const Value *V, const Value *&LHS, const Value *&RHS) {
718 Value *L = const_cast<Value *>(LHS);
719 Value *R = const_cast<Value *>(RHS);
720 auto Result = matchSelectPattern(const_cast<Value *>(V), L, R);
721 LHS = L;
722 RHS = R;
723 return Result;
724 }
725
726 /// Determine the pattern that a select with the given compare as its
727 /// predicate and given values as its true/false operands would match.
728 SelectPatternResult matchDecomposedSelectPattern(
729 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
730 Instruction::CastOps *CastOp = nullptr, unsigned Depth = 0);
731
732 /// Return the canonical comparison predicate for the specified
733 /// minimum/maximum flavor.
734 CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF,
735 bool Ordered = false);
736
737 /// Return the inverse minimum/maximum flavor of the specified flavor.
738 /// For example, signed minimum is the inverse of signed maximum.
739 SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF);
740
741 Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID);
742
743 /// Return the canonical inverse comparison predicate for the specified
744 /// minimum/maximum flavor.
745 CmpInst::Predicate getInverseMinMaxPred(SelectPatternFlavor SPF);
746
747 /// Return the minimum or maximum constant value for the specified integer
748 /// min/max flavor and type.
749 APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth);
750
751 /// Check if the values in \p VL are select instructions that can be converted
752 /// to a min or max (vector) intrinsic. Returns the intrinsic ID, if such a
753 /// conversion is possible, together with a bool indicating whether all select
754 /// conditions are only used by the selects. Otherwise return
755 /// Intrinsic::not_intrinsic.
756 std::pair<Intrinsic::ID, bool>
757 canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL);
758
759 /// Attempt to match a simple first order recurrence cycle of the form:
760 /// %iv = phi Ty [%Start, %Entry], [%Inc, %backedge]
761 /// %inc = binop %iv, %step
762 /// OR
763 /// %iv = phi Ty [%Start, %Entry], [%Inc, %backedge]
764 /// %inc = binop %step, %iv
765 ///
766 /// A first order recurrence is a formula with the form: X_n = f(X_(n-1))
767 ///
768 /// A couple of notes on subtleties in that definition:
769 /// * The Step does not have to be loop invariant. In math terms, it can
770 /// be a free variable. We allow recurrences with both constant and
771 /// variable coefficients. Callers may wish to filter cases where Step
772 /// does not dominate P.
773 /// * For non-commutative operators, we will match both forms. This
774 /// results in some odd recurrence structures. Callers may wish to filter
775 /// out recurrences where the phi is not the LHS of the returned operator.
776 /// * Because of the structure matched, the caller can assume as a post
777 /// condition of the match the presence of a Loop with P's parent as it's
778 /// header *except* in unreachable code. (Dominance decays in unreachable
779 /// code.)
780 ///
781 /// NOTE: This is intentional simple. If you want the ability to analyze
782 /// non-trivial loop conditons, see ScalarEvolution instead.
783 bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
784 Value *&Start, Value *&Step);
785
786 /// Analogous to the above, but starting from the binary operator
787 bool matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P,
788 Value *&Start, Value *&Step);
789
790 /// Return true if RHS is known to be implied true by LHS. Return false if
791 /// RHS is known to be implied false by LHS. Otherwise, return None if no
792 /// implication can be made.
793 /// A & B must be i1 (boolean) values or a vector of such values. Note that
794 /// the truth table for implication is the same as <=u on i1 values (but not
795 /// <=s!). The truth table for both is:
796 /// | T | F (B)
797 /// T | T | F
798 /// F | T | T
799 /// (A)
800 Optional<bool> isImpliedCondition(const Value *LHS, const Value *RHS,
801 const DataLayout &DL, bool LHSIsTrue = true,
802 unsigned Depth = 0);
803 Optional<bool> isImpliedCondition(const Value *LHS,
804 CmpInst::Predicate RHSPred,
805 const Value *RHSOp0, const Value *RHSOp1,
806 const DataLayout &DL, bool LHSIsTrue = true,
807 unsigned Depth = 0);
808
809 /// Return the boolean condition value in the context of the given instruction
810 /// if it is known based on dominating conditions.
811 Optional<bool> isImpliedByDomCondition(const Value *Cond,
812 const Instruction *ContextI,
813 const DataLayout &DL);
814 Optional<bool> isImpliedByDomCondition(CmpInst::Predicate Pred,
815 const Value *LHS, const Value *RHS,
816 const Instruction *ContextI,
817 const DataLayout &DL);
818
819 /// If Ptr1 is provably equal to Ptr2 plus a constant offset, return that
820 /// offset. For example, Ptr1 might be &A[42], and Ptr2 might be &A[40]. In
821 /// this case offset would be -8.
822 Optional<int64_t> isPointerOffset(const Value *Ptr1, const Value *Ptr2,
823 const DataLayout &DL);
824} // end namespace llvm
825
826#endif // LLVM_ANALYSIS_VALUETRACKING_H