Bug Summary

File:llvm/lib/Transforms/IPO/Attributor.cpp
Warning:line 4971, column 27
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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