Bug Summary

File:llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp
Warning:line 1619, column 13
2nd function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name AMDGPULibCalls.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Target/AMDGPU -I /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/lib/Target/AMDGPU -I include -I /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-25-232935-20746-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp
1//===- AMDGPULibCalls.cpp -------------------------------------------------===//
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/// \file
10/// This file does AMD library function optimizations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AMDGPU.h"
15#include "AMDGPULibFunc.h"
16#include "GCNSubtarget.h"
17#include "llvm/Analysis/AliasAnalysis.h"
18#include "llvm/Analysis/Loads.h"
19#include "llvm/IR/IntrinsicsAMDGPU.h"
20#include "llvm/IR/IRBuilder.h"
21#include "llvm/InitializePasses.h"
22#include "llvm/Target/TargetMachine.h"
23
24#define DEBUG_TYPE"amdgpu-simplifylib" "amdgpu-simplifylib"
25
26using namespace llvm;
27
28static cl::opt<bool> EnablePreLink("amdgpu-prelink",
29 cl::desc("Enable pre-link mode optimizations"),
30 cl::init(false),
31 cl::Hidden);
32
33static cl::list<std::string> UseNative("amdgpu-use-native",
34 cl::desc("Comma separated list of functions to replace with native, or all"),
35 cl::CommaSeparated, cl::ValueOptional,
36 cl::Hidden);
37
38#define MATH_PInumbers::pi numbers::pi
39#define MATH_Enumbers::e numbers::e
40#define MATH_SQRT2numbers::sqrt2 numbers::sqrt2
41#define MATH_SQRT1_2numbers::inv_sqrt2 numbers::inv_sqrt2
42
43namespace llvm {
44
45class AMDGPULibCalls {
46private:
47
48 typedef llvm::AMDGPULibFunc FuncInfo;
49
50 const TargetMachine *TM;
51
52 // -fuse-native.
53 bool AllNative = false;
54
55 bool useNativeFunc(const StringRef F) const;
56
57 // Return a pointer (pointer expr) to the function if function definition with
58 // "FuncName" exists. It may create a new function prototype in pre-link mode.
59 FunctionCallee getFunction(Module *M, const FuncInfo &fInfo);
60
61 bool parseFunctionName(const StringRef &FMangledName, FuncInfo &FInfo);
62
63 bool TDOFold(CallInst *CI, const FuncInfo &FInfo);
64
65 /* Specialized optimizations */
66
67 // recip (half or native)
68 bool fold_recip(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
69
70 // divide (half or native)
71 bool fold_divide(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
72
73 // pow/powr/pown
74 bool fold_pow(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
75
76 // rootn
77 bool fold_rootn(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
78
79 // fma/mad
80 bool fold_fma_mad(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
81
82 // -fuse-native for sincos
83 bool sincosUseNative(CallInst *aCI, const FuncInfo &FInfo);
84
85 // evaluate calls if calls' arguments are constants.
86 bool evaluateScalarMathFunc(const FuncInfo &FInfo, double& Res0,
87 double& Res1, Constant *copr0, Constant *copr1, Constant *copr2);
88 bool evaluateCall(CallInst *aCI, const FuncInfo &FInfo);
89
90 // sqrt
91 bool fold_sqrt(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
92
93 // sin/cos
94 bool fold_sincos(CallInst * CI, IRBuilder<> &B, AliasAnalysis * AA);
95
96 // __read_pipe/__write_pipe
97 bool fold_read_write_pipe(CallInst *CI, IRBuilder<> &B,
98 const FuncInfo &FInfo);
99
100 // llvm.amdgcn.wavefrontsize
101 bool fold_wavefrontsize(CallInst *CI, IRBuilder<> &B);
102
103 // Get insertion point at entry.
104 BasicBlock::iterator getEntryIns(CallInst * UI);
105 // Insert an Alloc instruction.
106 AllocaInst* insertAlloca(CallInst * UI, IRBuilder<> &B, const char *prefix);
107 // Get a scalar native builtin single argument FP function
108 FunctionCallee getNativeFunction(Module *M, const FuncInfo &FInfo);
109
110protected:
111 CallInst *CI;
112
113 bool isUnsafeMath(const CallInst *CI) const;
114
115 void replaceCall(Value *With) {
116 CI->replaceAllUsesWith(With);
117 CI->eraseFromParent();
118 }
119
120public:
121 AMDGPULibCalls(const TargetMachine *TM_ = nullptr) : TM(TM_) {}
122
123 bool fold(CallInst *CI, AliasAnalysis *AA = nullptr);
124
125 void initNativeFuncs();
126
127 // Replace a normal math function call with that native version
128 bool useNative(CallInst *CI);
129};
130
131} // end llvm namespace
132
133namespace {
134
135 class AMDGPUSimplifyLibCalls : public FunctionPass {
136
137 AMDGPULibCalls Simplifier;
138
139 public:
140 static char ID; // Pass identification
141
142 AMDGPUSimplifyLibCalls(const TargetMachine *TM = nullptr)
143 : FunctionPass(ID), Simplifier(TM) {
144 initializeAMDGPUSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
145 }
146
147 void getAnalysisUsage(AnalysisUsage &AU) const override {
148 AU.addRequired<AAResultsWrapperPass>();
149 }
150
151 bool runOnFunction(Function &M) override;
152 };
153
154 class AMDGPUUseNativeCalls : public FunctionPass {
155
156 AMDGPULibCalls Simplifier;
157
158 public:
159 static char ID; // Pass identification
160
161 AMDGPUUseNativeCalls() : FunctionPass(ID) {
162 initializeAMDGPUUseNativeCallsPass(*PassRegistry::getPassRegistry());
163 Simplifier.initNativeFuncs();
164 }
165
166 bool runOnFunction(Function &F) override;
167 };
168
169} // end anonymous namespace.
170
171char AMDGPUSimplifyLibCalls::ID = 0;
172char AMDGPUUseNativeCalls::ID = 0;
173
174INITIALIZE_PASS_BEGIN(AMDGPUSimplifyLibCalls, "amdgpu-simplifylib",static void *initializeAMDGPUSimplifyLibCallsPassOnce(PassRegistry
&Registry) {
175 "Simplify well-known AMD library calls", false, false)static void *initializeAMDGPUSimplifyLibCallsPassOnce(PassRegistry
&Registry) {
176INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)initializeAAResultsWrapperPassPass(Registry);
177INITIALIZE_PASS_END(AMDGPUSimplifyLibCalls, "amdgpu-simplifylib",PassInfo *PI = new PassInfo( "Simplify well-known AMD library calls"
, "amdgpu-simplifylib", &AMDGPUSimplifyLibCalls::ID, PassInfo
::NormalCtor_t(callDefaultCtor<AMDGPUSimplifyLibCalls>)
, false, false); Registry.registerPass(*PI, true); return PI;
} static llvm::once_flag InitializeAMDGPUSimplifyLibCallsPassFlag
; void llvm::initializeAMDGPUSimplifyLibCallsPass(PassRegistry
&Registry) { llvm::call_once(InitializeAMDGPUSimplifyLibCallsPassFlag
, initializeAMDGPUSimplifyLibCallsPassOnce, std::ref(Registry
)); }
178 "Simplify well-known AMD library calls", false, false)PassInfo *PI = new PassInfo( "Simplify well-known AMD library calls"
, "amdgpu-simplifylib", &AMDGPUSimplifyLibCalls::ID, PassInfo
::NormalCtor_t(callDefaultCtor<AMDGPUSimplifyLibCalls>)
, false, false); Registry.registerPass(*PI, true); return PI;
} static llvm::once_flag InitializeAMDGPUSimplifyLibCallsPassFlag
; void llvm::initializeAMDGPUSimplifyLibCallsPass(PassRegistry
&Registry) { llvm::call_once(InitializeAMDGPUSimplifyLibCallsPassFlag
, initializeAMDGPUSimplifyLibCallsPassOnce, std::ref(Registry
)); }
179
180INITIALIZE_PASS(AMDGPUUseNativeCalls, "amdgpu-usenative",static void *initializeAMDGPUUseNativeCallsPassOnce(PassRegistry
&Registry) { PassInfo *PI = new PassInfo( "Replace builtin math calls with that native versions."
, "amdgpu-usenative", &AMDGPUUseNativeCalls::ID, PassInfo
::NormalCtor_t(callDefaultCtor<AMDGPUUseNativeCalls>), false
, false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeAMDGPUUseNativeCallsPassFlag; void
llvm::initializeAMDGPUUseNativeCallsPass(PassRegistry &Registry
) { llvm::call_once(InitializeAMDGPUUseNativeCallsPassFlag, initializeAMDGPUUseNativeCallsPassOnce
, std::ref(Registry)); }
181 "Replace builtin math calls with that native versions.",static void *initializeAMDGPUUseNativeCallsPassOnce(PassRegistry
&Registry) { PassInfo *PI = new PassInfo( "Replace builtin math calls with that native versions."
, "amdgpu-usenative", &AMDGPUUseNativeCalls::ID, PassInfo
::NormalCtor_t(callDefaultCtor<AMDGPUUseNativeCalls>), false
, false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeAMDGPUUseNativeCallsPassFlag; void
llvm::initializeAMDGPUUseNativeCallsPass(PassRegistry &Registry
) { llvm::call_once(InitializeAMDGPUUseNativeCallsPassFlag, initializeAMDGPUUseNativeCallsPassOnce
, std::ref(Registry)); }
182 false, false)static void *initializeAMDGPUUseNativeCallsPassOnce(PassRegistry
&Registry) { PassInfo *PI = new PassInfo( "Replace builtin math calls with that native versions."
, "amdgpu-usenative", &AMDGPUUseNativeCalls::ID, PassInfo
::NormalCtor_t(callDefaultCtor<AMDGPUUseNativeCalls>), false
, false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeAMDGPUUseNativeCallsPassFlag; void
llvm::initializeAMDGPUUseNativeCallsPass(PassRegistry &Registry
) { llvm::call_once(InitializeAMDGPUUseNativeCallsPassFlag, initializeAMDGPUUseNativeCallsPassOnce
, std::ref(Registry)); }
183
184template <typename IRB>
185static CallInst *CreateCallEx(IRB &B, FunctionCallee Callee, Value *Arg,
186 const Twine &Name = "") {
187 CallInst *R = B.CreateCall(Callee, Arg, Name);
188 if (Function *F = dyn_cast<Function>(Callee.getCallee()))
189 R->setCallingConv(F->getCallingConv());
190 return R;
191}
192
193template <typename IRB>
194static CallInst *CreateCallEx2(IRB &B, FunctionCallee Callee, Value *Arg1,
195 Value *Arg2, const Twine &Name = "") {
196 CallInst *R = B.CreateCall(Callee, {Arg1, Arg2}, Name);
197 if (Function *F = dyn_cast<Function>(Callee.getCallee()))
198 R->setCallingConv(F->getCallingConv());
199 return R;
200}
201
202// Data structures for table-driven optimizations.
203// FuncTbl works for both f32 and f64 functions with 1 input argument
204
205struct TableEntry {
206 double result;
207 double input;
208};
209
210/* a list of {result, input} */
211static const TableEntry tbl_acos[] = {
212 {MATH_PInumbers::pi / 2.0, 0.0},
213 {MATH_PInumbers::pi / 2.0, -0.0},
214 {0.0, 1.0},
215 {MATH_PInumbers::pi, -1.0}
216};
217static const TableEntry tbl_acosh[] = {
218 {0.0, 1.0}
219};
220static const TableEntry tbl_acospi[] = {
221 {0.5, 0.0},
222 {0.5, -0.0},
223 {0.0, 1.0},
224 {1.0, -1.0}
225};
226static const TableEntry tbl_asin[] = {
227 {0.0, 0.0},
228 {-0.0, -0.0},
229 {MATH_PInumbers::pi / 2.0, 1.0},
230 {-MATH_PInumbers::pi / 2.0, -1.0}
231};
232static const TableEntry tbl_asinh[] = {
233 {0.0, 0.0},
234 {-0.0, -0.0}
235};
236static const TableEntry tbl_asinpi[] = {
237 {0.0, 0.0},
238 {-0.0, -0.0},
239 {0.5, 1.0},
240 {-0.5, -1.0}
241};
242static const TableEntry tbl_atan[] = {
243 {0.0, 0.0},
244 {-0.0, -0.0},
245 {MATH_PInumbers::pi / 4.0, 1.0},
246 {-MATH_PInumbers::pi / 4.0, -1.0}
247};
248static const TableEntry tbl_atanh[] = {
249 {0.0, 0.0},
250 {-0.0, -0.0}
251};
252static const TableEntry tbl_atanpi[] = {
253 {0.0, 0.0},
254 {-0.0, -0.0},
255 {0.25, 1.0},
256 {-0.25, -1.0}
257};
258static const TableEntry tbl_cbrt[] = {
259 {0.0, 0.0},
260 {-0.0, -0.0},
261 {1.0, 1.0},
262 {-1.0, -1.0},
263};
264static const TableEntry tbl_cos[] = {
265 {1.0, 0.0},
266 {1.0, -0.0}
267};
268static const TableEntry tbl_cosh[] = {
269 {1.0, 0.0},
270 {1.0, -0.0}
271};
272static const TableEntry tbl_cospi[] = {
273 {1.0, 0.0},
274 {1.0, -0.0}
275};
276static const TableEntry tbl_erfc[] = {
277 {1.0, 0.0},
278 {1.0, -0.0}
279};
280static const TableEntry tbl_erf[] = {
281 {0.0, 0.0},
282 {-0.0, -0.0}
283};
284static const TableEntry tbl_exp[] = {
285 {1.0, 0.0},
286 {1.0, -0.0},
287 {MATH_Enumbers::e, 1.0}
288};
289static const TableEntry tbl_exp2[] = {
290 {1.0, 0.0},
291 {1.0, -0.0},
292 {2.0, 1.0}
293};
294static const TableEntry tbl_exp10[] = {
295 {1.0, 0.0},
296 {1.0, -0.0},
297 {10.0, 1.0}
298};
299static const TableEntry tbl_expm1[] = {
300 {0.0, 0.0},
301 {-0.0, -0.0}
302};
303static const TableEntry tbl_log[] = {
304 {0.0, 1.0},
305 {1.0, MATH_Enumbers::e}
306};
307static const TableEntry tbl_log2[] = {
308 {0.0, 1.0},
309 {1.0, 2.0}
310};
311static const TableEntry tbl_log10[] = {
312 {0.0, 1.0},
313 {1.0, 10.0}
314};
315static const TableEntry tbl_rsqrt[] = {
316 {1.0, 1.0},
317 {MATH_SQRT1_2numbers::inv_sqrt2, 2.0}
318};
319static const TableEntry tbl_sin[] = {
320 {0.0, 0.0},
321 {-0.0, -0.0}
322};
323static const TableEntry tbl_sinh[] = {
324 {0.0, 0.0},
325 {-0.0, -0.0}
326};
327static const TableEntry tbl_sinpi[] = {
328 {0.0, 0.0},
329 {-0.0, -0.0}
330};
331static const TableEntry tbl_sqrt[] = {
332 {0.0, 0.0},
333 {1.0, 1.0},
334 {MATH_SQRT2numbers::sqrt2, 2.0}
335};
336static const TableEntry tbl_tan[] = {
337 {0.0, 0.0},
338 {-0.0, -0.0}
339};
340static const TableEntry tbl_tanh[] = {
341 {0.0, 0.0},
342 {-0.0, -0.0}
343};
344static const TableEntry tbl_tanpi[] = {
345 {0.0, 0.0},
346 {-0.0, -0.0}
347};
348static const TableEntry tbl_tgamma[] = {
349 {1.0, 1.0},
350 {1.0, 2.0},
351 {2.0, 3.0},
352 {6.0, 4.0}
353};
354
355static bool HasNative(AMDGPULibFunc::EFuncId id) {
356 switch(id) {
357 case AMDGPULibFunc::EI_DIVIDE:
358 case AMDGPULibFunc::EI_COS:
359 case AMDGPULibFunc::EI_EXP:
360 case AMDGPULibFunc::EI_EXP2:
361 case AMDGPULibFunc::EI_EXP10:
362 case AMDGPULibFunc::EI_LOG:
363 case AMDGPULibFunc::EI_LOG2:
364 case AMDGPULibFunc::EI_LOG10:
365 case AMDGPULibFunc::EI_POWR:
366 case AMDGPULibFunc::EI_RECIP:
367 case AMDGPULibFunc::EI_RSQRT:
368 case AMDGPULibFunc::EI_SIN:
369 case AMDGPULibFunc::EI_SINCOS:
370 case AMDGPULibFunc::EI_SQRT:
371 case AMDGPULibFunc::EI_TAN:
372 return true;
373 default:;
374 }
375 return false;
376}
377
378struct TableRef {
379 size_t size;
380 const TableEntry *table; // variable size: from 0 to (size - 1)
381
382 TableRef() : size(0), table(nullptr) {}
383
384 template <size_t N>
385 TableRef(const TableEntry (&tbl)[N]) : size(N), table(&tbl[0]) {}
386};
387
388static TableRef getOptTable(AMDGPULibFunc::EFuncId id) {
389 switch(id) {
390 case AMDGPULibFunc::EI_ACOS: return TableRef(tbl_acos);
391 case AMDGPULibFunc::EI_ACOSH: return TableRef(tbl_acosh);
392 case AMDGPULibFunc::EI_ACOSPI: return TableRef(tbl_acospi);
393 case AMDGPULibFunc::EI_ASIN: return TableRef(tbl_asin);
394 case AMDGPULibFunc::EI_ASINH: return TableRef(tbl_asinh);
395 case AMDGPULibFunc::EI_ASINPI: return TableRef(tbl_asinpi);
396 case AMDGPULibFunc::EI_ATAN: return TableRef(tbl_atan);
397 case AMDGPULibFunc::EI_ATANH: return TableRef(tbl_atanh);
398 case AMDGPULibFunc::EI_ATANPI: return TableRef(tbl_atanpi);
399 case AMDGPULibFunc::EI_CBRT: return TableRef(tbl_cbrt);
400 case AMDGPULibFunc::EI_NCOS:
401 case AMDGPULibFunc::EI_COS: return TableRef(tbl_cos);
402 case AMDGPULibFunc::EI_COSH: return TableRef(tbl_cosh);
403 case AMDGPULibFunc::EI_COSPI: return TableRef(tbl_cospi);
404 case AMDGPULibFunc::EI_ERFC: return TableRef(tbl_erfc);
405 case AMDGPULibFunc::EI_ERF: return TableRef(tbl_erf);
406 case AMDGPULibFunc::EI_EXP: return TableRef(tbl_exp);
407 case AMDGPULibFunc::EI_NEXP2:
408 case AMDGPULibFunc::EI_EXP2: return TableRef(tbl_exp2);
409 case AMDGPULibFunc::EI_EXP10: return TableRef(tbl_exp10);
410 case AMDGPULibFunc::EI_EXPM1: return TableRef(tbl_expm1);
411 case AMDGPULibFunc::EI_LOG: return TableRef(tbl_log);
412 case AMDGPULibFunc::EI_NLOG2:
413 case AMDGPULibFunc::EI_LOG2: return TableRef(tbl_log2);
414 case AMDGPULibFunc::EI_LOG10: return TableRef(tbl_log10);
415 case AMDGPULibFunc::EI_NRSQRT:
416 case AMDGPULibFunc::EI_RSQRT: return TableRef(tbl_rsqrt);
417 case AMDGPULibFunc::EI_NSIN:
418 case AMDGPULibFunc::EI_SIN: return TableRef(tbl_sin);
419 case AMDGPULibFunc::EI_SINH: return TableRef(tbl_sinh);
420 case AMDGPULibFunc::EI_SINPI: return TableRef(tbl_sinpi);
421 case AMDGPULibFunc::EI_NSQRT:
422 case AMDGPULibFunc::EI_SQRT: return TableRef(tbl_sqrt);
423 case AMDGPULibFunc::EI_TAN: return TableRef(tbl_tan);
424 case AMDGPULibFunc::EI_TANH: return TableRef(tbl_tanh);
425 case AMDGPULibFunc::EI_TANPI: return TableRef(tbl_tanpi);
426 case AMDGPULibFunc::EI_TGAMMA: return TableRef(tbl_tgamma);
427 default:;
428 }
429 return TableRef();
430}
431
432static inline int getVecSize(const AMDGPULibFunc& FInfo) {
433 return FInfo.getLeads()[0].VectorSize;
434}
435
436static inline AMDGPULibFunc::EType getArgType(const AMDGPULibFunc& FInfo) {
437 return (AMDGPULibFunc::EType)FInfo.getLeads()[0].ArgType;
438}
439
440FunctionCallee AMDGPULibCalls::getFunction(Module *M, const FuncInfo &fInfo) {
441 // If we are doing PreLinkOpt, the function is external. So it is safe to
442 // use getOrInsertFunction() at this stage.
443
444 return EnablePreLink ? AMDGPULibFunc::getOrInsertFunction(M, fInfo)
445 : AMDGPULibFunc::getFunction(M, fInfo);
446}
447
448bool AMDGPULibCalls::parseFunctionName(const StringRef &FMangledName,
449 FuncInfo &FInfo) {
450 return AMDGPULibFunc::parse(FMangledName, FInfo);
451}
452
453bool AMDGPULibCalls::isUnsafeMath(const CallInst *CI) const {
454 if (auto Op = dyn_cast<FPMathOperator>(CI))
455 if (Op->isFast())
456 return true;
457 const Function *F = CI->getParent()->getParent();
458 Attribute Attr = F->getFnAttribute("unsafe-fp-math");
459 return Attr.getValueAsBool();
460}
461
462bool AMDGPULibCalls::useNativeFunc(const StringRef F) const {
463 return AllNative || llvm::is_contained(UseNative, F);
464}
465
466void AMDGPULibCalls::initNativeFuncs() {
467 AllNative = useNativeFunc("all") ||
468 (UseNative.getNumOccurrences() && UseNative.size() == 1 &&
469 UseNative.begin()->empty());
470}
471
472bool AMDGPULibCalls::sincosUseNative(CallInst *aCI, const FuncInfo &FInfo) {
473 bool native_sin = useNativeFunc("sin");
474 bool native_cos = useNativeFunc("cos");
475
476 if (native_sin && native_cos) {
477 Module *M = aCI->getModule();
478 Value *opr0 = aCI->getArgOperand(0);
479
480 AMDGPULibFunc nf;
481 nf.getLeads()[0].ArgType = FInfo.getLeads()[0].ArgType;
482 nf.getLeads()[0].VectorSize = FInfo.getLeads()[0].VectorSize;
483
484 nf.setPrefix(AMDGPULibFunc::NATIVE);
485 nf.setId(AMDGPULibFunc::EI_SIN);
486 FunctionCallee sinExpr = getFunction(M, nf);
487
488 nf.setPrefix(AMDGPULibFunc::NATIVE);
489 nf.setId(AMDGPULibFunc::EI_COS);
490 FunctionCallee cosExpr = getFunction(M, nf);
491 if (sinExpr && cosExpr) {
492 Value *sinval = CallInst::Create(sinExpr, opr0, "splitsin", aCI);
493 Value *cosval = CallInst::Create(cosExpr, opr0, "splitcos", aCI);
494 new StoreInst(cosval, aCI->getArgOperand(1), aCI);
495
496 DEBUG_WITH_TYPE("usenative", dbgs() << "<useNative> replace " << *aCIdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("usenative")) { dbgs() << "<useNative> replace "
<< *aCI << " with native version of sin/cos"; } }
while (false)
497 << " with native version of sin/cos")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("usenative")) { dbgs() << "<useNative> replace "
<< *aCI << " with native version of sin/cos"; } }
while (false)
;
498
499 replaceCall(sinval);
500 return true;
501 }
502 }
503 return false;
504}
505
506bool AMDGPULibCalls::useNative(CallInst *aCI) {
507 CI = aCI;
508 Function *Callee = aCI->getCalledFunction();
509
510 FuncInfo FInfo;
511 if (!parseFunctionName(Callee->getName(), FInfo) || !FInfo.isMangled() ||
512 FInfo.getPrefix() != AMDGPULibFunc::NOPFX ||
513 getArgType(FInfo) == AMDGPULibFunc::F64 || !HasNative(FInfo.getId()) ||
514 !(AllNative || useNativeFunc(FInfo.getName()))) {
515 return false;
516 }
517
518 if (FInfo.getId() == AMDGPULibFunc::EI_SINCOS)
519 return sincosUseNative(aCI, FInfo);
520
521 FInfo.setPrefix(AMDGPULibFunc::NATIVE);
522 FunctionCallee F = getFunction(aCI->getModule(), FInfo);
523 if (!F)
524 return false;
525
526 aCI->setCalledFunction(F);
527 DEBUG_WITH_TYPE("usenative", dbgs() << "<useNative> replace " << *aCIdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("usenative")) { dbgs() << "<useNative> replace "
<< *aCI << " with native version"; } } while (false
)
528 << " with native version")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("usenative")) { dbgs() << "<useNative> replace "
<< *aCI << " with native version"; } } while (false
)
;
529 return true;
530}
531
532// Clang emits call of __read_pipe_2 or __read_pipe_4 for OpenCL read_pipe
533// builtin, with appended type size and alignment arguments, where 2 or 4
534// indicates the original number of arguments. The library has optimized version
535// of __read_pipe_2/__read_pipe_4 when the type size and alignment has the same
536// power of 2 value. This function transforms __read_pipe_2 to __read_pipe_2_N
537// for such cases where N is the size in bytes of the type (N = 1, 2, 4, 8, ...,
538// 128). The same for __read_pipe_4, write_pipe_2, and write_pipe_4.
539bool AMDGPULibCalls::fold_read_write_pipe(CallInst *CI, IRBuilder<> &B,
540 const FuncInfo &FInfo) {
541 auto *Callee = CI->getCalledFunction();
542 if (!Callee->isDeclaration())
543 return false;
544
545 assert(Callee->hasName() && "Invalid read_pipe/write_pipe function")(static_cast <bool> (Callee->hasName() && "Invalid read_pipe/write_pipe function"
) ? void (0) : __assert_fail ("Callee->hasName() && \"Invalid read_pipe/write_pipe function\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 545, __extension__
__PRETTY_FUNCTION__))
;
546 auto *M = Callee->getParent();
547 auto &Ctx = M->getContext();
548 std::string Name = std::string(Callee->getName());
549 auto NumArg = CI->arg_size();
550 if (NumArg != 4 && NumArg != 6)
551 return false;
552 auto *PacketSize = CI->getArgOperand(NumArg - 2);
553 auto *PacketAlign = CI->getArgOperand(NumArg - 1);
554 if (!isa<ConstantInt>(PacketSize) || !isa<ConstantInt>(PacketAlign))
555 return false;
556 unsigned Size = cast<ConstantInt>(PacketSize)->getZExtValue();
557 Align Alignment = cast<ConstantInt>(PacketAlign)->getAlignValue();
558 if (Alignment != Size)
559 return false;
560
561 Type *PtrElemTy;
562 if (Size <= 8)
563 PtrElemTy = Type::getIntNTy(Ctx, Size * 8);
564 else
565 PtrElemTy = FixedVectorType::get(Type::getInt64Ty(Ctx), Size / 8);
566 unsigned PtrArgLoc = CI->arg_size() - 3;
567 auto PtrArg = CI->getArgOperand(PtrArgLoc);
568 unsigned PtrArgAS = PtrArg->getType()->getPointerAddressSpace();
569 auto *PtrTy = llvm::PointerType::get(PtrElemTy, PtrArgAS);
570
571 SmallVector<llvm::Type *, 6> ArgTys;
572 for (unsigned I = 0; I != PtrArgLoc; ++I)
573 ArgTys.push_back(CI->getArgOperand(I)->getType());
574 ArgTys.push_back(PtrTy);
575
576 Name = Name + "_" + std::to_string(Size);
577 auto *FTy = FunctionType::get(Callee->getReturnType(),
578 ArrayRef<Type *>(ArgTys), false);
579 AMDGPULibFunc NewLibFunc(Name, FTy);
580 FunctionCallee F = AMDGPULibFunc::getOrInsertFunction(M, NewLibFunc);
581 if (!F)
582 return false;
583
584 auto *BCast = B.CreatePointerCast(PtrArg, PtrTy);
585 SmallVector<Value *, 6> Args;
586 for (unsigned I = 0; I != PtrArgLoc; ++I)
587 Args.push_back(CI->getArgOperand(I));
588 Args.push_back(BCast);
589
590 auto *NCI = B.CreateCall(F, Args);
591 NCI->setAttributes(CI->getAttributes());
592 CI->replaceAllUsesWith(NCI);
593 CI->dropAllReferences();
594 CI->eraseFromParent();
595
596 return true;
597}
598
599// This function returns false if no change; return true otherwise.
600bool AMDGPULibCalls::fold(CallInst *CI, AliasAnalysis *AA) {
601 this->CI = CI;
602 Function *Callee = CI->getCalledFunction();
603
604 // Ignore indirect calls.
605 if (Callee == nullptr)
12
Taking false branch
606 return false;
607
608 BasicBlock *BB = CI->getParent();
609 LLVMContext &Context = CI->getParent()->getContext();
610 IRBuilder<> B(Context);
611
612 // Set the builder to the instruction after the call.
613 B.SetInsertPoint(BB, CI->getIterator());
614
615 // Copy fast flags from the original call.
616 if (const FPMathOperator *FPOp
13.1
'FPOp' is null
= dyn_cast<const FPMathOperator>(CI))
13
Assuming 'CI' is not a 'FPMathOperator'
14
Taking false branch
617 B.setFastMathFlags(FPOp->getFastMathFlags());
618
619 switch (Callee->getIntrinsicID()) {
15
Control jumps to the 'default' case at line 620
620 default:
621 break;
16
Execution continues on line 626
622 case Intrinsic::amdgcn_wavefrontsize:
623 return !EnablePreLink && fold_wavefrontsize(CI, B);
624 }
625
626 FuncInfo FInfo;
627 if (!parseFunctionName(Callee->getName(), FInfo))
17
Assuming the condition is false
18
Taking false branch
628 return false;
629
630 // Further check the number of arguments to see if they match.
631 if (CI->arg_size() != FInfo.getNumArgs())
19
Assuming the condition is false
20
Taking false branch
632 return false;
633
634 if (TDOFold(CI, FInfo))
635 return true;
636
637 // Under unsafe-math, evaluate calls if possible.
638 // According to Brian Sumner, we can do this for all f32 function calls
639 // using host's double function calls.
640 if (isUnsafeMath(CI) && evaluateCall(CI, FInfo))
21
Assuming the condition is true
22
Calling 'AMDGPULibCalls::evaluateCall'
641 return true;
642
643 // Specialized optimizations for each function call
644 switch (FInfo.getId()) {
645 case AMDGPULibFunc::EI_RECIP:
646 // skip vector function
647 assert ((FInfo.getPrefix() == AMDGPULibFunc::NATIVE ||(static_cast <bool> ((FInfo.getPrefix() == AMDGPULibFunc
::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) &&
"recip must be an either native or half function") ? void (0
) : __assert_fail ("(FInfo.getPrefix() == AMDGPULibFunc::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) && \"recip must be an either native or half function\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 649, __extension__
__PRETTY_FUNCTION__))
648 FInfo.getPrefix() == AMDGPULibFunc::HALF) &&(static_cast <bool> ((FInfo.getPrefix() == AMDGPULibFunc
::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) &&
"recip must be an either native or half function") ? void (0
) : __assert_fail ("(FInfo.getPrefix() == AMDGPULibFunc::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) && \"recip must be an either native or half function\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 649, __extension__
__PRETTY_FUNCTION__))
649 "recip must be an either native or half function")(static_cast <bool> ((FInfo.getPrefix() == AMDGPULibFunc
::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) &&
"recip must be an either native or half function") ? void (0
) : __assert_fail ("(FInfo.getPrefix() == AMDGPULibFunc::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) && \"recip must be an either native or half function\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 649, __extension__
__PRETTY_FUNCTION__))
;
650 return (getVecSize(FInfo) != 1) ? false : fold_recip(CI, B, FInfo);
651
652 case AMDGPULibFunc::EI_DIVIDE:
653 // skip vector function
654 assert ((FInfo.getPrefix() == AMDGPULibFunc::NATIVE ||(static_cast <bool> ((FInfo.getPrefix() == AMDGPULibFunc
::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) &&
"divide must be an either native or half function") ? void (
0) : __assert_fail ("(FInfo.getPrefix() == AMDGPULibFunc::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) && \"divide must be an either native or half function\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 656, __extension__
__PRETTY_FUNCTION__))
655 FInfo.getPrefix() == AMDGPULibFunc::HALF) &&(static_cast <bool> ((FInfo.getPrefix() == AMDGPULibFunc
::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) &&
"divide must be an either native or half function") ? void (
0) : __assert_fail ("(FInfo.getPrefix() == AMDGPULibFunc::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) && \"divide must be an either native or half function\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 656, __extension__
__PRETTY_FUNCTION__))
656 "divide must be an either native or half function")(static_cast <bool> ((FInfo.getPrefix() == AMDGPULibFunc
::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) &&
"divide must be an either native or half function") ? void (
0) : __assert_fail ("(FInfo.getPrefix() == AMDGPULibFunc::NATIVE || FInfo.getPrefix() == AMDGPULibFunc::HALF) && \"divide must be an either native or half function\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 656, __extension__
__PRETTY_FUNCTION__))
;
657 return (getVecSize(FInfo) != 1) ? false : fold_divide(CI, B, FInfo);
658
659 case AMDGPULibFunc::EI_POW:
660 case AMDGPULibFunc::EI_POWR:
661 case AMDGPULibFunc::EI_POWN:
662 return fold_pow(CI, B, FInfo);
663
664 case AMDGPULibFunc::EI_ROOTN:
665 // skip vector function
666 return (getVecSize(FInfo) != 1) ? false : fold_rootn(CI, B, FInfo);
667
668 case AMDGPULibFunc::EI_FMA:
669 case AMDGPULibFunc::EI_MAD:
670 case AMDGPULibFunc::EI_NFMA:
671 // skip vector function
672 return (getVecSize(FInfo) != 1) ? false : fold_fma_mad(CI, B, FInfo);
673
674 case AMDGPULibFunc::EI_SQRT:
675 return isUnsafeMath(CI) && fold_sqrt(CI, B, FInfo);
676 case AMDGPULibFunc::EI_COS:
677 case AMDGPULibFunc::EI_SIN:
678 if ((getArgType(FInfo) == AMDGPULibFunc::F32 ||
679 getArgType(FInfo) == AMDGPULibFunc::F64)
680 && (FInfo.getPrefix() == AMDGPULibFunc::NOPFX))
681 return fold_sincos(CI, B, AA);
682
683 break;
684 case AMDGPULibFunc::EI_READ_PIPE_2:
685 case AMDGPULibFunc::EI_READ_PIPE_4:
686 case AMDGPULibFunc::EI_WRITE_PIPE_2:
687 case AMDGPULibFunc::EI_WRITE_PIPE_4:
688 return fold_read_write_pipe(CI, B, FInfo);
689
690 default:
691 break;
692 }
693
694 return false;
695}
696
697bool AMDGPULibCalls::TDOFold(CallInst *CI, const FuncInfo &FInfo) {
698 // Table-Driven optimization
699 const TableRef tr = getOptTable(FInfo.getId());
700 if (tr.size==0)
701 return false;
702
703 int const sz = (int)tr.size;
704 const TableEntry * const ftbl = tr.table;
705 Value *opr0 = CI->getArgOperand(0);
706
707 if (getVecSize(FInfo) > 1) {
708 if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(opr0)) {
709 SmallVector<double, 0> DVal;
710 for (int eltNo = 0; eltNo < getVecSize(FInfo); ++eltNo) {
711 ConstantFP *eltval = dyn_cast<ConstantFP>(
712 CV->getElementAsConstant((unsigned)eltNo));
713 assert(eltval && "Non-FP arguments in math function!")(static_cast <bool> (eltval && "Non-FP arguments in math function!"
) ? void (0) : __assert_fail ("eltval && \"Non-FP arguments in math function!\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 713, __extension__
__PRETTY_FUNCTION__))
;
714 bool found = false;
715 for (int i=0; i < sz; ++i) {
716 if (eltval->isExactlyValue(ftbl[i].input)) {
717 DVal.push_back(ftbl[i].result);
718 found = true;
719 break;
720 }
721 }
722 if (!found) {
723 // This vector constants not handled yet.
724 return false;
725 }
726 }
727 LLVMContext &context = CI->getParent()->getParent()->getContext();
728 Constant *nval;
729 if (getArgType(FInfo) == AMDGPULibFunc::F32) {
730 SmallVector<float, 0> FVal;
731 for (unsigned i = 0; i < DVal.size(); ++i) {
732 FVal.push_back((float)DVal[i]);
733 }
734 ArrayRef<float> tmp(FVal);
735 nval = ConstantDataVector::get(context, tmp);
736 } else { // F64
737 ArrayRef<double> tmp(DVal);
738 nval = ConstantDataVector::get(context, tmp);
739 }
740 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *nval << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *nval << "\n"; } } while
(false)
;
741 replaceCall(nval);
742 return true;
743 }
744 } else {
745 // Scalar version
746 if (ConstantFP *CF = dyn_cast<ConstantFP>(opr0)) {
747 for (int i = 0; i < sz; ++i) {
748 if (CF->isExactlyValue(ftbl[i].input)) {
749 Value *nval = ConstantFP::get(CF->getType(), ftbl[i].result);
750 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *nval << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *nval << "\n"; } } while
(false)
;
751 replaceCall(nval);
752 return true;
753 }
754 }
755 }
756 }
757
758 return false;
759}
760
761// [native_]half_recip(c) ==> 1.0/c
762bool AMDGPULibCalls::fold_recip(CallInst *CI, IRBuilder<> &B,
763 const FuncInfo &FInfo) {
764 Value *opr0 = CI->getArgOperand(0);
765 if (ConstantFP *CF = dyn_cast<ConstantFP>(opr0)) {
766 // Just create a normal div. Later, InstCombine will be able
767 // to compute the divide into a constant (avoid check float infinity
768 // or subnormal at this point).
769 Value *nval = B.CreateFDiv(ConstantFP::get(CF->getType(), 1.0),
770 opr0,
771 "recip2div");
772 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *nval << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *nval << "\n"; } } while
(false)
;
773 replaceCall(nval);
774 return true;
775 }
776 return false;
777}
778
779// [native_]half_divide(x, c) ==> x/c
780bool AMDGPULibCalls::fold_divide(CallInst *CI, IRBuilder<> &B,
781 const FuncInfo &FInfo) {
782 Value *opr0 = CI->getArgOperand(0);
783 Value *opr1 = CI->getArgOperand(1);
784 ConstantFP *CF0 = dyn_cast<ConstantFP>(opr0);
785 ConstantFP *CF1 = dyn_cast<ConstantFP>(opr1);
786
787 if ((CF0 && CF1) || // both are constants
788 (CF1 && (getArgType(FInfo) == AMDGPULibFunc::F32)))
789 // CF1 is constant && f32 divide
790 {
791 Value *nval1 = B.CreateFDiv(ConstantFP::get(opr1->getType(), 1.0),
792 opr1, "__div2recip");
793 Value *nval = B.CreateFMul(opr0, nval1, "__div2mul");
794 replaceCall(nval);
795 return true;
796 }
797 return false;
798}
799
800namespace llvm {
801static double log2(double V) {
802#if _XOPEN_SOURCE700 >= 600 || defined(_ISOC99_SOURCE1) || _POSIX_C_SOURCE200809L >= 200112L
803 return ::log2(V);
804#else
805 return log(V) / numbers::ln2;
806#endif
807}
808}
809
810bool AMDGPULibCalls::fold_pow(CallInst *CI, IRBuilder<> &B,
811 const FuncInfo &FInfo) {
812 assert((FInfo.getId() == AMDGPULibFunc::EI_POW ||(static_cast <bool> ((FInfo.getId() == AMDGPULibFunc::EI_POW
|| FInfo.getId() == AMDGPULibFunc::EI_POWR || FInfo.getId() ==
AMDGPULibFunc::EI_POWN) && "fold_pow: encounter a wrong function call"
) ? void (0) : __assert_fail ("(FInfo.getId() == AMDGPULibFunc::EI_POW || FInfo.getId() == AMDGPULibFunc::EI_POWR || FInfo.getId() == AMDGPULibFunc::EI_POWN) && \"fold_pow: encounter a wrong function call\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 815, __extension__
__PRETTY_FUNCTION__))
813 FInfo.getId() == AMDGPULibFunc::EI_POWR ||(static_cast <bool> ((FInfo.getId() == AMDGPULibFunc::EI_POW
|| FInfo.getId() == AMDGPULibFunc::EI_POWR || FInfo.getId() ==
AMDGPULibFunc::EI_POWN) && "fold_pow: encounter a wrong function call"
) ? void (0) : __assert_fail ("(FInfo.getId() == AMDGPULibFunc::EI_POW || FInfo.getId() == AMDGPULibFunc::EI_POWR || FInfo.getId() == AMDGPULibFunc::EI_POWN) && \"fold_pow: encounter a wrong function call\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 815, __extension__
__PRETTY_FUNCTION__))
814 FInfo.getId() == AMDGPULibFunc::EI_POWN) &&(static_cast <bool> ((FInfo.getId() == AMDGPULibFunc::EI_POW
|| FInfo.getId() == AMDGPULibFunc::EI_POWR || FInfo.getId() ==
AMDGPULibFunc::EI_POWN) && "fold_pow: encounter a wrong function call"
) ? void (0) : __assert_fail ("(FInfo.getId() == AMDGPULibFunc::EI_POW || FInfo.getId() == AMDGPULibFunc::EI_POWR || FInfo.getId() == AMDGPULibFunc::EI_POWN) && \"fold_pow: encounter a wrong function call\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 815, __extension__
__PRETTY_FUNCTION__))
815 "fold_pow: encounter a wrong function call")(static_cast <bool> ((FInfo.getId() == AMDGPULibFunc::EI_POW
|| FInfo.getId() == AMDGPULibFunc::EI_POWR || FInfo.getId() ==
AMDGPULibFunc::EI_POWN) && "fold_pow: encounter a wrong function call"
) ? void (0) : __assert_fail ("(FInfo.getId() == AMDGPULibFunc::EI_POW || FInfo.getId() == AMDGPULibFunc::EI_POWR || FInfo.getId() == AMDGPULibFunc::EI_POWN) && \"fold_pow: encounter a wrong function call\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 815, __extension__
__PRETTY_FUNCTION__))
;
816
817 Value *opr0, *opr1;
818 ConstantFP *CF;
819 ConstantInt *CINT;
820 ConstantAggregateZero *CZero;
821 Type *eltType;
822
823 opr0 = CI->getArgOperand(0);
824 opr1 = CI->getArgOperand(1);
825 CZero = dyn_cast<ConstantAggregateZero>(opr1);
826 if (getVecSize(FInfo) == 1) {
827 eltType = opr0->getType();
828 CF = dyn_cast<ConstantFP>(opr1);
829 CINT = dyn_cast<ConstantInt>(opr1);
830 } else {
831 VectorType *VTy = dyn_cast<VectorType>(opr0->getType());
832 assert(VTy && "Oprand of vector function should be of vectortype")(static_cast <bool> (VTy && "Oprand of vector function should be of vectortype"
) ? void (0) : __assert_fail ("VTy && \"Oprand of vector function should be of vectortype\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 832, __extension__
__PRETTY_FUNCTION__))
;
833 eltType = VTy->getElementType();
834 ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(opr1);
835
836 // Now, only Handle vector const whose elements have the same value.
837 CF = CDV ? dyn_cast_or_null<ConstantFP>(CDV->getSplatValue()) : nullptr;
838 CINT = CDV ? dyn_cast_or_null<ConstantInt>(CDV->getSplatValue()) : nullptr;
839 }
840
841 // No unsafe math , no constant argument, do nothing
842 if (!isUnsafeMath(CI) && !CF && !CINT && !CZero)
843 return false;
844
845 // 0x1111111 means that we don't do anything for this call.
846 int ci_opr1 = (CINT ? (int)CINT->getSExtValue() : 0x1111111);
847
848 if ((CF && CF->isZero()) || (CINT && ci_opr1 == 0) || CZero) {
849 // pow/powr/pown(x, 0) == 1
850 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> 1\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> 1\n"; } } while (false)
;
851 Constant *cnval = ConstantFP::get(eltType, 1.0);
852 if (getVecSize(FInfo) > 1) {
853 cnval = ConstantDataVector::getSplat(getVecSize(FInfo), cnval);
854 }
855 replaceCall(cnval);
856 return true;
857 }
858 if ((CF && CF->isExactlyValue(1.0)) || (CINT && ci_opr1 == 1)) {
859 // pow/powr/pown(x, 1.0) = x
860 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr0 << "\n"; } } while
(false)
;
861 replaceCall(opr0);
862 return true;
863 }
864 if ((CF && CF->isExactlyValue(2.0)) || (CINT && ci_opr1 == 2)) {
865 // pow/powr/pown(x, 2.0) = x*x
866 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << " * " << *opr0do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr0 << " * " <<
*opr0 << "\n"; } } while (false)
867 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr0 << " * " <<
*opr0 << "\n"; } } while (false)
;
868 Value *nval = B.CreateFMul(opr0, opr0, "__pow2");
869 replaceCall(nval);
870 return true;
871 }
872 if ((CF && CF->isExactlyValue(-1.0)) || (CINT && ci_opr1 == -1)) {
873 // pow/powr/pown(x, -1.0) = 1.0/x
874 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> 1 / " << *opr0 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> 1 / " << *opr0 << "\n"; } }
while (false)
;
875 Constant *cnval = ConstantFP::get(eltType, 1.0);
876 if (getVecSize(FInfo) > 1) {
877 cnval = ConstantDataVector::getSplat(getVecSize(FInfo), cnval);
878 }
879 Value *nval = B.CreateFDiv(cnval, opr0, "__powrecip");
880 replaceCall(nval);
881 return true;
882 }
883
884 Module *M = CI->getModule();
885 if (CF && (CF->isExactlyValue(0.5) || CF->isExactlyValue(-0.5))) {
886 // pow[r](x, [-]0.5) = sqrt(x)
887 bool issqrt = CF->isExactlyValue(0.5);
888 if (FunctionCallee FPExpr =
889 getFunction(M, AMDGPULibFunc(issqrt ? AMDGPULibFunc::EI_SQRT
890 : AMDGPULibFunc::EI_RSQRT,
891 FInfo))) {
892 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << FInfo.getName().c_str() <<
"(" << *opr0 << ")\n"; } } while (false)
893 << FInfo.getName().c_str() << "(" << *opr0 << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << FInfo.getName().c_str() <<
"(" << *opr0 << ")\n"; } } while (false)
;
894 Value *nval = CreateCallEx(B,FPExpr, opr0, issqrt ? "__pow2sqrt"
895 : "__pow2rsqrt");
896 replaceCall(nval);
897 return true;
898 }
899 }
900
901 if (!isUnsafeMath(CI))
902 return false;
903
904 // Unsafe Math optimization
905
906 // Remember that ci_opr1 is set if opr1 is integral
907 if (CF) {
908 double dval = (getArgType(FInfo) == AMDGPULibFunc::F32)
909 ? (double)CF->getValueAPF().convertToFloat()
910 : CF->getValueAPF().convertToDouble();
911 int ival = (int)dval;
912 if ((double)ival == dval) {
913 ci_opr1 = ival;
914 } else
915 ci_opr1 = 0x11111111;
916 }
917
918 // pow/powr/pown(x, c) = [1/](x*x*..x); where
919 // trunc(c) == c && the number of x == c && |c| <= 12
920 unsigned abs_opr1 = (ci_opr1 < 0) ? -ci_opr1 : ci_opr1;
921 if (abs_opr1 <= 12) {
922 Constant *cnval;
923 Value *nval;
924 if (abs_opr1 == 0) {
925 cnval = ConstantFP::get(eltType, 1.0);
926 if (getVecSize(FInfo) > 1) {
927 cnval = ConstantDataVector::getSplat(getVecSize(FInfo), cnval);
928 }
929 nval = cnval;
930 } else {
931 Value *valx2 = nullptr;
932 nval = nullptr;
933 while (abs_opr1 > 0) {
934 valx2 = valx2 ? B.CreateFMul(valx2, valx2, "__powx2") : opr0;
935 if (abs_opr1 & 1) {
936 nval = nval ? B.CreateFMul(nval, valx2, "__powprod") : valx2;
937 }
938 abs_opr1 >>= 1;
939 }
940 }
941
942 if (ci_opr1 < 0) {
943 cnval = ConstantFP::get(eltType, 1.0);
944 if (getVecSize(FInfo) > 1) {
945 cnval = ConstantDataVector::getSplat(getVecSize(FInfo), cnval);
946 }
947 nval = B.CreateFDiv(cnval, nval, "__1powprod");
948 }
949 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << ((ci_opr1 < 0) ? "1/prod("
: "prod(") << *opr0 << ")\n"; } } while (false)
950 << ((ci_opr1 < 0) ? "1/prod(" : "prod(") << *opr0do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << ((ci_opr1 < 0) ? "1/prod("
: "prod(") << *opr0 << ")\n"; } } while (false)
951 << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << ((ci_opr1 < 0) ? "1/prod("
: "prod(") << *opr0 << ")\n"; } } while (false)
;
952 replaceCall(nval);
953 return true;
954 }
955
956 // powr ---> exp2(y * log2(x))
957 // pown/pow ---> powr(fabs(x), y) | (x & ((int)y << 31))
958 FunctionCallee ExpExpr =
959 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_EXP2, FInfo));
960 if (!ExpExpr)
961 return false;
962
963 bool needlog = false;
964 bool needabs = false;
965 bool needcopysign = false;
966 Constant *cnval = nullptr;
967 if (getVecSize(FInfo) == 1) {
968 CF = dyn_cast<ConstantFP>(opr0);
969
970 if (CF) {
971 double V = (getArgType(FInfo) == AMDGPULibFunc::F32)
972 ? (double)CF->getValueAPF().convertToFloat()
973 : CF->getValueAPF().convertToDouble();
974
975 V = log2(std::abs(V));
976 cnval = ConstantFP::get(eltType, V);
977 needcopysign = (FInfo.getId() != AMDGPULibFunc::EI_POWR) &&
978 CF->isNegative();
979 } else {
980 needlog = true;
981 needcopysign = needabs = FInfo.getId() != AMDGPULibFunc::EI_POWR &&
982 (!CF || CF->isNegative());
983 }
984 } else {
985 ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(opr0);
986
987 if (!CDV) {
988 needlog = true;
989 needcopysign = needabs = FInfo.getId() != AMDGPULibFunc::EI_POWR;
990 } else {
991 assert ((int)CDV->getNumElements() == getVecSize(FInfo) &&(static_cast <bool> ((int)CDV->getNumElements() == getVecSize
(FInfo) && "Wrong vector size detected") ? void (0) :
__assert_fail ("(int)CDV->getNumElements() == getVecSize(FInfo) && \"Wrong vector size detected\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 992, __extension__
__PRETTY_FUNCTION__))
992 "Wrong vector size detected")(static_cast <bool> ((int)CDV->getNumElements() == getVecSize
(FInfo) && "Wrong vector size detected") ? void (0) :
__assert_fail ("(int)CDV->getNumElements() == getVecSize(FInfo) && \"Wrong vector size detected\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 992, __extension__
__PRETTY_FUNCTION__))
;
993
994 SmallVector<double, 0> DVal;
995 for (int i=0; i < getVecSize(FInfo); ++i) {
996 double V = (getArgType(FInfo) == AMDGPULibFunc::F32)
997 ? (double)CDV->getElementAsFloat(i)
998 : CDV->getElementAsDouble(i);
999 if (V < 0.0) needcopysign = true;
1000 V = log2(std::abs(V));
1001 DVal.push_back(V);
1002 }
1003 if (getArgType(FInfo) == AMDGPULibFunc::F32) {
1004 SmallVector<float, 0> FVal;
1005 for (unsigned i=0; i < DVal.size(); ++i) {
1006 FVal.push_back((float)DVal[i]);
1007 }
1008 ArrayRef<float> tmp(FVal);
1009 cnval = ConstantDataVector::get(M->getContext(), tmp);
1010 } else {
1011 ArrayRef<double> tmp(DVal);
1012 cnval = ConstantDataVector::get(M->getContext(), tmp);
1013 }
1014 }
1015 }
1016
1017 if (needcopysign && (FInfo.getId() == AMDGPULibFunc::EI_POW)) {
1018 // We cannot handle corner cases for a general pow() function, give up
1019 // unless y is a constant integral value. Then proceed as if it were pown.
1020 if (getVecSize(FInfo) == 1) {
1021 if (const ConstantFP *CF = dyn_cast<ConstantFP>(opr1)) {
1022 double y = (getArgType(FInfo) == AMDGPULibFunc::F32)
1023 ? (double)CF->getValueAPF().convertToFloat()
1024 : CF->getValueAPF().convertToDouble();
1025 if (y != (double)(int64_t)y)
1026 return false;
1027 } else
1028 return false;
1029 } else {
1030 if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(opr1)) {
1031 for (int i=0; i < getVecSize(FInfo); ++i) {
1032 double y = (getArgType(FInfo) == AMDGPULibFunc::F32)
1033 ? (double)CDV->getElementAsFloat(i)
1034 : CDV->getElementAsDouble(i);
1035 if (y != (double)(int64_t)y)
1036 return false;
1037 }
1038 } else
1039 return false;
1040 }
1041 }
1042
1043 Value *nval;
1044 if (needabs) {
1045 FunctionCallee AbsExpr =
1046 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_FABS, FInfo));
1047 if (!AbsExpr)
1048 return false;
1049 nval = CreateCallEx(B, AbsExpr, opr0, "__fabs");
1050 } else {
1051 nval = cnval ? cnval : opr0;
1052 }
1053 if (needlog) {
1054 FunctionCallee LogExpr =
1055 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_LOG2, FInfo));
1056 if (!LogExpr)
1057 return false;
1058 nval = CreateCallEx(B,LogExpr, nval, "__log2");
1059 }
1060
1061 if (FInfo.getId() == AMDGPULibFunc::EI_POWN) {
1062 // convert int(32) to fp(f32 or f64)
1063 opr1 = B.CreateSIToFP(opr1, nval->getType(), "pownI2F");
1064 }
1065 nval = B.CreateFMul(opr1, nval, "__ylogx");
1066 nval = CreateCallEx(B,ExpExpr, nval, "__exp2");
1067
1068 if (needcopysign) {
1069 Value *opr_n;
1070 Type* rTy = opr0->getType();
1071 Type* nTyS = eltType->isDoubleTy() ? B.getInt64Ty() : B.getInt32Ty();
1072 Type *nTy = nTyS;
1073 if (const auto *vTy = dyn_cast<FixedVectorType>(rTy))
1074 nTy = FixedVectorType::get(nTyS, vTy);
1075 unsigned size = nTy->getScalarSizeInBits();
1076 opr_n = CI->getArgOperand(1);
1077 if (opr_n->getType()->isIntegerTy())
1078 opr_n = B.CreateZExtOrBitCast(opr_n, nTy, "__ytou");
1079 else
1080 opr_n = B.CreateFPToSI(opr1, nTy, "__ytou");
1081
1082 Value *sign = B.CreateShl(opr_n, size-1, "__yeven");
1083 sign = B.CreateAnd(B.CreateBitCast(opr0, nTy), sign, "__pow_sign");
1084 nval = B.CreateOr(B.CreateBitCast(nval, nTy), sign);
1085 nval = B.CreateBitCast(nval, opr0->getType());
1086 }
1087
1088 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << "exp2(" << *opr1 <<
" * log2(" << *opr0 << "))\n"; } } while (false)
1089 << "exp2(" << *opr1 << " * log2(" << *opr0 << "))\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << "exp2(" << *opr1 <<
" * log2(" << *opr0 << "))\n"; } } while (false)
;
1090 replaceCall(nval);
1091
1092 return true;
1093}
1094
1095bool AMDGPULibCalls::fold_rootn(CallInst *CI, IRBuilder<> &B,
1096 const FuncInfo &FInfo) {
1097 Value *opr0 = CI->getArgOperand(0);
1098 Value *opr1 = CI->getArgOperand(1);
1099
1100 ConstantInt *CINT = dyn_cast<ConstantInt>(opr1);
1101 if (!CINT) {
1102 return false;
1103 }
1104 int ci_opr1 = (int)CINT->getSExtValue();
1105 if (ci_opr1 == 1) { // rootn(x, 1) = x
1106 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr0 << "\n"; } } while
(false)
;
1107 replaceCall(opr0);
1108 return true;
1109 }
1110 if (ci_opr1 == 2) { // rootn(x, 2) = sqrt(x)
1111 Module *M = CI->getModule();
1112 if (FunctionCallee FPExpr =
1113 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
1114 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> sqrt(" << *opr0 << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> sqrt(" << *opr0 << ")\n"; }
} while (false)
;
1115 Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2sqrt");
1116 replaceCall(nval);
1117 return true;
1118 }
1119 } else if (ci_opr1 == 3) { // rootn(x, 3) = cbrt(x)
1120 Module *M = CI->getModule();
1121 if (FunctionCallee FPExpr =
1122 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_CBRT, FInfo))) {
1123 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> cbrt(" << *opr0 << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> cbrt(" << *opr0 << ")\n"; }
} while (false)
;
1124 Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2cbrt");
1125 replaceCall(nval);
1126 return true;
1127 }
1128 } else if (ci_opr1 == -1) { // rootn(x, -1) = 1.0/x
1129 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> 1.0 / " << *opr0 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> 1.0 / " << *opr0 << "\n"; }
} while (false)
;
1130 Value *nval = B.CreateFDiv(ConstantFP::get(opr0->getType(), 1.0),
1131 opr0,
1132 "__rootn2div");
1133 replaceCall(nval);
1134 return true;
1135 } else if (ci_opr1 == -2) { // rootn(x, -2) = rsqrt(x)
1136 Module *M = CI->getModule();
1137 if (FunctionCallee FPExpr =
1138 getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_RSQRT, FInfo))) {
1139 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> rsqrt(" << *opr0do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> rsqrt(" << *opr0 << ")\n"; }
} while (false)
1140 << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> rsqrt(" << *opr0 << ")\n"; }
} while (false)
;
1141 Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2rsqrt");
1142 replaceCall(nval);
1143 return true;
1144 }
1145 }
1146 return false;
1147}
1148
1149bool AMDGPULibCalls::fold_fma_mad(CallInst *CI, IRBuilder<> &B,
1150 const FuncInfo &FInfo) {
1151 Value *opr0 = CI->getArgOperand(0);
1152 Value *opr1 = CI->getArgOperand(1);
1153 Value *opr2 = CI->getArgOperand(2);
1154
1155 ConstantFP *CF0 = dyn_cast<ConstantFP>(opr0);
1156 ConstantFP *CF1 = dyn_cast<ConstantFP>(opr1);
1157 if ((CF0 && CF0->isZero()) || (CF1 && CF1->isZero())) {
1158 // fma/mad(a, b, c) = c if a=0 || b=0
1159 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr2 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr2 << "\n"; } } while
(false)
;
1160 replaceCall(opr2);
1161 return true;
1162 }
1163 if (CF0 && CF0->isExactlyValue(1.0f)) {
1164 // fma/mad(a, b, c) = b+c if a=1
1165 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr1 << " + " << *opr2do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr1 << " + " <<
*opr2 << "\n"; } } while (false)
1166 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr1 << " + " <<
*opr2 << "\n"; } } while (false)
;
1167 Value *nval = B.CreateFAdd(opr1, opr2, "fmaadd");
1168 replaceCall(nval);
1169 return true;
1170 }
1171 if (CF1 && CF1->isExactlyValue(1.0f)) {
1172 // fma/mad(a, b, c) = a+c if b=1
1173 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << " + " << *opr2do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr0 << " + " <<
*opr2 << "\n"; } } while (false)
1174 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr0 << " + " <<
*opr2 << "\n"; } } while (false)
;
1175 Value *nval = B.CreateFAdd(opr0, opr2, "fmaadd");
1176 replaceCall(nval);
1177 return true;
1178 }
1179 if (ConstantFP *CF = dyn_cast<ConstantFP>(opr2)) {
1180 if (CF->isZero()) {
1181 // fma/mad(a, b, c) = a*b if c=0
1182 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << " * "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr0 << " * " <<
*opr1 << "\n"; } } while (false)
1183 << *opr1 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << *opr0 << " * " <<
*opr1 << "\n"; } } while (false)
;
1184 Value *nval = B.CreateFMul(opr0, opr1, "fmamul");
1185 replaceCall(nval);
1186 return true;
1187 }
1188 }
1189
1190 return false;
1191}
1192
1193// Get a scalar native builtin single argument FP function
1194FunctionCallee AMDGPULibCalls::getNativeFunction(Module *M,
1195 const FuncInfo &FInfo) {
1196 if (getArgType(FInfo) == AMDGPULibFunc::F64 || !HasNative(FInfo.getId()))
1197 return nullptr;
1198 FuncInfo nf = FInfo;
1199 nf.setPrefix(AMDGPULibFunc::NATIVE);
1200 return getFunction(M, nf);
1201}
1202
1203// fold sqrt -> native_sqrt (x)
1204bool AMDGPULibCalls::fold_sqrt(CallInst *CI, IRBuilder<> &B,
1205 const FuncInfo &FInfo) {
1206 if (getArgType(FInfo) == AMDGPULibFunc::F32 && (getVecSize(FInfo) == 1) &&
1207 (FInfo.getPrefix() != AMDGPULibFunc::NATIVE)) {
1208 if (FunctionCallee FPExpr = getNativeFunction(
1209 CI->getModule(), AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
1210 Value *opr0 = CI->getArgOperand(0);
1211 LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << "sqrt(" << *opr0 <<
")\n"; } } while (false)
1212 << "sqrt(" << *opr0 << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: " << *
CI << " ---> " << "sqrt(" << *opr0 <<
")\n"; } } while (false)
;
1213 Value *nval = CreateCallEx(B,FPExpr, opr0, "__sqrt");
1214 replaceCall(nval);
1215 return true;
1216 }
1217 }
1218 return false;
1219}
1220
1221// fold sin, cos -> sincos.
1222bool AMDGPULibCalls::fold_sincos(CallInst *CI, IRBuilder<> &B,
1223 AliasAnalysis *AA) {
1224 AMDGPULibFunc fInfo;
1225 if (!AMDGPULibFunc::parse(CI->getCalledFunction()->getName(), fInfo))
1226 return false;
1227
1228 assert(fInfo.getId() == AMDGPULibFunc::EI_SIN ||(static_cast <bool> (fInfo.getId() == AMDGPULibFunc::EI_SIN
|| fInfo.getId() == AMDGPULibFunc::EI_COS) ? void (0) : __assert_fail
("fInfo.getId() == AMDGPULibFunc::EI_SIN || fInfo.getId() == AMDGPULibFunc::EI_COS"
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 1229, __extension__
__PRETTY_FUNCTION__))
1229 fInfo.getId() == AMDGPULibFunc::EI_COS)(static_cast <bool> (fInfo.getId() == AMDGPULibFunc::EI_SIN
|| fInfo.getId() == AMDGPULibFunc::EI_COS) ? void (0) : __assert_fail
("fInfo.getId() == AMDGPULibFunc::EI_SIN || fInfo.getId() == AMDGPULibFunc::EI_COS"
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 1229, __extension__
__PRETTY_FUNCTION__))
;
1230 bool const isSin = fInfo.getId() == AMDGPULibFunc::EI_SIN;
1231
1232 Value *CArgVal = CI->getArgOperand(0);
1233 BasicBlock * const CBB = CI->getParent();
1234
1235 int const MaxScan = 30;
1236 bool Changed = false;
1237
1238 { // fold in load value.
1239 LoadInst *LI = dyn_cast<LoadInst>(CArgVal);
1240 if (LI && LI->getParent() == CBB) {
1241 BasicBlock::iterator BBI = LI->getIterator();
1242 Value *AvailableVal = FindAvailableLoadedValue(LI, CBB, BBI, MaxScan, AA);
1243 if (AvailableVal) {
1244 Changed = true;
1245 CArgVal->replaceAllUsesWith(AvailableVal);
1246 if (CArgVal->getNumUses() == 0)
1247 LI->eraseFromParent();
1248 CArgVal = CI->getArgOperand(0);
1249 }
1250 }
1251 }
1252
1253 Module *M = CI->getModule();
1254 fInfo.setId(isSin ? AMDGPULibFunc::EI_COS : AMDGPULibFunc::EI_SIN);
1255 std::string const PairName = fInfo.mangle();
1256
1257 CallInst *UI = nullptr;
1258 for (User* U : CArgVal->users()) {
1259 CallInst *XI = dyn_cast_or_null<CallInst>(U);
1260 if (!XI || XI == CI || XI->getParent() != CBB)
1261 continue;
1262
1263 Function *UCallee = XI->getCalledFunction();
1264 if (!UCallee || !UCallee->getName().equals(PairName))
1265 continue;
1266
1267 BasicBlock::iterator BBI = CI->getIterator();
1268 if (BBI == CI->getParent()->begin())
1269 break;
1270 --BBI;
1271 for (int I = MaxScan; I > 0 && BBI != CBB->begin(); --BBI, --I) {
1272 if (cast<Instruction>(BBI) == XI) {
1273 UI = XI;
1274 break;
1275 }
1276 }
1277 if (UI) break;
1278 }
1279
1280 if (!UI)
1281 return Changed;
1282
1283 // Merge the sin and cos.
1284
1285 // for OpenCL 2.0 we have only generic implementation of sincos
1286 // function.
1287 AMDGPULibFunc nf(AMDGPULibFunc::EI_SINCOS, fInfo);
1288 nf.getLeads()[0].PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AMDGPUAS::FLAT_ADDRESS);
1289 FunctionCallee Fsincos = getFunction(M, nf);
1290 if (!Fsincos)
1291 return Changed;
1292
1293 BasicBlock::iterator ItOld = B.GetInsertPoint();
1294 AllocaInst *Alloc = insertAlloca(UI, B, "__sincos_");
1295 B.SetInsertPoint(UI);
1296
1297 Value *P = Alloc;
1298 Type *PTy = Fsincos.getFunctionType()->getParamType(1);
1299 // The allocaInst allocates the memory in private address space. This need
1300 // to be bitcasted to point to the address space of cos pointer type.
1301 // In OpenCL 2.0 this is generic, while in 1.2 that is private.
1302 if (PTy->getPointerAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
1303 P = B.CreateAddrSpaceCast(Alloc, PTy);
1304 CallInst *Call = CreateCallEx2(B, Fsincos, UI->getArgOperand(0), P);
1305
1306 LLVM_DEBUG(errs() << "AMDIC: fold_sincos (" << *CI << ", " << *UI << ") with "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: fold_sincos ("
<< *CI << ", " << *UI << ") with " <<
*Call << "\n"; } } while (false)
1307 << *Call << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: fold_sincos ("
<< *CI << ", " << *UI << ") with " <<
*Call << "\n"; } } while (false)
;
1308
1309 if (!isSin) { // CI->cos, UI->sin
1310 B.SetInsertPoint(&*ItOld);
1311 UI->replaceAllUsesWith(&*Call);
1312 Instruction *Reload = B.CreateLoad(Alloc->getAllocatedType(), Alloc);
1313 CI->replaceAllUsesWith(Reload);
1314 UI->eraseFromParent();
1315 CI->eraseFromParent();
1316 } else { // CI->sin, UI->cos
1317 Instruction *Reload = B.CreateLoad(Alloc->getAllocatedType(), Alloc);
1318 UI->replaceAllUsesWith(Reload);
1319 CI->replaceAllUsesWith(Call);
1320 UI->eraseFromParent();
1321 CI->eraseFromParent();
1322 }
1323 return true;
1324}
1325
1326bool AMDGPULibCalls::fold_wavefrontsize(CallInst *CI, IRBuilder<> &B) {
1327 if (!TM)
1328 return false;
1329
1330 StringRef CPU = TM->getTargetCPU();
1331 StringRef Features = TM->getTargetFeatureString();
1332 if ((CPU.empty() || CPU.equals_insensitive("generic")) &&
1333 (Features.empty() || !Features.contains_insensitive("wavefrontsize")))
1334 return false;
1335
1336 Function *F = CI->getParent()->getParent();
1337 const GCNSubtarget &ST = TM->getSubtarget<GCNSubtarget>(*F);
1338 unsigned N = ST.getWavefrontSize();
1339
1340 LLVM_DEBUG(errs() << "AMDIC: fold_wavefrontsize (" << *CI << ") with "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: fold_wavefrontsize ("
<< *CI << ") with " << N << "\n"; } }
while (false)
1341 << N << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { errs() << "AMDIC: fold_wavefrontsize ("
<< *CI << ") with " << N << "\n"; } }
while (false)
;
1342
1343 CI->replaceAllUsesWith(ConstantInt::get(B.getInt32Ty(), N));
1344 CI->eraseFromParent();
1345 return true;
1346}
1347
1348// Get insertion point at entry.
1349BasicBlock::iterator AMDGPULibCalls::getEntryIns(CallInst * UI) {
1350 Function * Func = UI->getParent()->getParent();
1351 BasicBlock * BB = &Func->getEntryBlock();
1352 assert(BB && "Entry block not found!")(static_cast <bool> (BB && "Entry block not found!"
) ? void (0) : __assert_fail ("BB && \"Entry block not found!\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 1352, __extension__
__PRETTY_FUNCTION__))
;
1353 BasicBlock::iterator ItNew = BB->begin();
1354 return ItNew;
1355}
1356
1357// Insert a AllocsInst at the beginning of function entry block.
1358AllocaInst* AMDGPULibCalls::insertAlloca(CallInst *UI, IRBuilder<> &B,
1359 const char *prefix) {
1360 BasicBlock::iterator ItNew = getEntryIns(UI);
1361 Function *UCallee = UI->getCalledFunction();
1362 Type *RetType = UCallee->getReturnType();
1363 B.SetInsertPoint(&*ItNew);
1364 AllocaInst *Alloc =
1365 B.CreateAlloca(RetType, nullptr, std::string(prefix) + UI->getName());
1366 Alloc->setAlignment(
1367 Align(UCallee->getParent()->getDataLayout().getTypeAllocSize(RetType)));
1368 return Alloc;
1369}
1370
1371bool AMDGPULibCalls::evaluateScalarMathFunc(const FuncInfo &FInfo,
1372 double& Res0, double& Res1,
1373 Constant *copr0, Constant *copr1,
1374 Constant *copr2) {
1375 // By default, opr0/opr1/opr3 holds values of float/double type.
1376 // If they are not float/double, each function has to its
1377 // operand separately.
1378 double opr0=0.0, opr1=0.0, opr2=0.0;
1379 ConstantFP *fpopr0 = dyn_cast_or_null<ConstantFP>(copr0);
1380 ConstantFP *fpopr1 = dyn_cast_or_null<ConstantFP>(copr1);
1381 ConstantFP *fpopr2 = dyn_cast_or_null<ConstantFP>(copr2);
1382 if (fpopr0) {
1383 opr0 = (getArgType(FInfo) == AMDGPULibFunc::F64)
1384 ? fpopr0->getValueAPF().convertToDouble()
1385 : (double)fpopr0->getValueAPF().convertToFloat();
1386 }
1387
1388 if (fpopr1) {
1389 opr1 = (getArgType(FInfo) == AMDGPULibFunc::F64)
1390 ? fpopr1->getValueAPF().convertToDouble()
1391 : (double)fpopr1->getValueAPF().convertToFloat();
1392 }
1393
1394 if (fpopr2) {
1395 opr2 = (getArgType(FInfo) == AMDGPULibFunc::F64)
1396 ? fpopr2->getValueAPF().convertToDouble()
1397 : (double)fpopr2->getValueAPF().convertToFloat();
1398 }
1399
1400 switch (FInfo.getId()) {
1401 default : return false;
1402
1403 case AMDGPULibFunc::EI_ACOS:
1404 Res0 = acos(opr0);
1405 return true;
1406
1407 case AMDGPULibFunc::EI_ACOSH:
1408 // acosh(x) == log(x + sqrt(x*x - 1))
1409 Res0 = log(opr0 + sqrt(opr0*opr0 - 1.0));
1410 return true;
1411
1412 case AMDGPULibFunc::EI_ACOSPI:
1413 Res0 = acos(opr0) / MATH_PInumbers::pi;
1414 return true;
1415
1416 case AMDGPULibFunc::EI_ASIN:
1417 Res0 = asin(opr0);
1418 return true;
1419
1420 case AMDGPULibFunc::EI_ASINH:
1421 // asinh(x) == log(x + sqrt(x*x + 1))
1422 Res0 = log(opr0 + sqrt(opr0*opr0 + 1.0));
1423 return true;
1424
1425 case AMDGPULibFunc::EI_ASINPI:
1426 Res0 = asin(opr0) / MATH_PInumbers::pi;
1427 return true;
1428
1429 case AMDGPULibFunc::EI_ATAN:
1430 Res0 = atan(opr0);
1431 return true;
1432
1433 case AMDGPULibFunc::EI_ATANH:
1434 // atanh(x) == (log(x+1) - log(x-1))/2;
1435 Res0 = (log(opr0 + 1.0) - log(opr0 - 1.0))/2.0;
1436 return true;
1437
1438 case AMDGPULibFunc::EI_ATANPI:
1439 Res0 = atan(opr0) / MATH_PInumbers::pi;
1440 return true;
1441
1442 case AMDGPULibFunc::EI_CBRT:
1443 Res0 = (opr0 < 0.0) ? -pow(-opr0, 1.0/3.0) : pow(opr0, 1.0/3.0);
1444 return true;
1445
1446 case AMDGPULibFunc::EI_COS:
1447 Res0 = cos(opr0);
1448 return true;
1449
1450 case AMDGPULibFunc::EI_COSH:
1451 Res0 = cosh(opr0);
1452 return true;
1453
1454 case AMDGPULibFunc::EI_COSPI:
1455 Res0 = cos(MATH_PInumbers::pi * opr0);
1456 return true;
1457
1458 case AMDGPULibFunc::EI_EXP:
1459 Res0 = exp(opr0);
1460 return true;
1461
1462 case AMDGPULibFunc::EI_EXP2:
1463 Res0 = pow(2.0, opr0);
1464 return true;
1465
1466 case AMDGPULibFunc::EI_EXP10:
1467 Res0 = pow(10.0, opr0);
1468 return true;
1469
1470 case AMDGPULibFunc::EI_EXPM1:
1471 Res0 = exp(opr0) - 1.0;
1472 return true;
1473
1474 case AMDGPULibFunc::EI_LOG:
1475 Res0 = log(opr0);
1476 return true;
1477
1478 case AMDGPULibFunc::EI_LOG2:
1479 Res0 = log(opr0) / log(2.0);
1480 return true;
1481
1482 case AMDGPULibFunc::EI_LOG10:
1483 Res0 = log(opr0) / log(10.0);
1484 return true;
1485
1486 case AMDGPULibFunc::EI_RSQRT:
1487 Res0 = 1.0 / sqrt(opr0);
1488 return true;
1489
1490 case AMDGPULibFunc::EI_SIN:
1491 Res0 = sin(opr0);
1492 return true;
1493
1494 case AMDGPULibFunc::EI_SINH:
1495 Res0 = sinh(opr0);
1496 return true;
1497
1498 case AMDGPULibFunc::EI_SINPI:
1499 Res0 = sin(MATH_PInumbers::pi * opr0);
1500 return true;
1501
1502 case AMDGPULibFunc::EI_SQRT:
1503 Res0 = sqrt(opr0);
1504 return true;
1505
1506 case AMDGPULibFunc::EI_TAN:
1507 Res0 = tan(opr0);
1508 return true;
1509
1510 case AMDGPULibFunc::EI_TANH:
1511 Res0 = tanh(opr0);
1512 return true;
1513
1514 case AMDGPULibFunc::EI_TANPI:
1515 Res0 = tan(MATH_PInumbers::pi * opr0);
1516 return true;
1517
1518 case AMDGPULibFunc::EI_RECIP:
1519 Res0 = 1.0 / opr0;
1520 return true;
1521
1522 // two-arg functions
1523 case AMDGPULibFunc::EI_DIVIDE:
1524 Res0 = opr0 / opr1;
1525 return true;
1526
1527 case AMDGPULibFunc::EI_POW:
1528 case AMDGPULibFunc::EI_POWR:
1529 Res0 = pow(opr0, opr1);
1530 return true;
1531
1532 case AMDGPULibFunc::EI_POWN: {
1533 if (ConstantInt *iopr1 = dyn_cast_or_null<ConstantInt>(copr1)) {
1534 double val = (double)iopr1->getSExtValue();
1535 Res0 = pow(opr0, val);
1536 return true;
1537 }
1538 return false;
1539 }
1540
1541 case AMDGPULibFunc::EI_ROOTN: {
1542 if (ConstantInt *iopr1 = dyn_cast_or_null<ConstantInt>(copr1)) {
1543 double val = (double)iopr1->getSExtValue();
1544 Res0 = pow(opr0, 1.0 / val);
1545 return true;
1546 }
1547 return false;
1548 }
1549
1550 // with ptr arg
1551 case AMDGPULibFunc::EI_SINCOS:
1552 Res0 = sin(opr0);
1553 Res1 = cos(opr0);
1554 return true;
1555
1556 // three-arg functions
1557 case AMDGPULibFunc::EI_FMA:
1558 case AMDGPULibFunc::EI_MAD:
1559 Res0 = opr0 * opr1 + opr2;
1560 return true;
1561 }
1562
1563 return false;
1564}
1565
1566bool AMDGPULibCalls::evaluateCall(CallInst *aCI, const FuncInfo &FInfo) {
1567 int numArgs = (int)aCI->arg_size();
1568 if (numArgs > 3)
23
Assuming 'numArgs' is <= 3
24
Taking false branch
1569 return false;
1570
1571 Constant *copr0 = nullptr;
1572 Constant *copr1 = nullptr;
1573 Constant *copr2 = nullptr;
1574 if (numArgs > 0) {
25
Assuming 'numArgs' is <= 0
26
Taking false branch
1575 if ((copr0 = dyn_cast<Constant>(aCI->getArgOperand(0))) == nullptr)
1576 return false;
1577 }
1578
1579 if (numArgs
26.1
'numArgs' is <= 1
> 1) {
27
Taking false branch
1580 if ((copr1 = dyn_cast<Constant>(aCI->getArgOperand(1))) == nullptr) {
1581 if (FInfo.getId() != AMDGPULibFunc::EI_SINCOS)
1582 return false;
1583 }
1584 }
1585
1586 if (numArgs
27.1
'numArgs' is <= 2
> 2) {
28
Taking false branch
1587 if ((copr2 = dyn_cast<Constant>(aCI->getArgOperand(2))) == nullptr)
1588 return false;
1589 }
1590
1591 // At this point, all arguments to aCI are constants.
1592
1593 // max vector size is 16, and sincos will generate two results.
1594 double DVal0[16], DVal1[16];
1595 bool hasTwoResults = (FInfo.getId() == AMDGPULibFunc::EI_SINCOS);
29
Assuming the condition is false
1596 if (getVecSize(FInfo) == 1) {
30
Assuming the condition is false
31
Taking false branch
1597 if (!evaluateScalarMathFunc(FInfo, DVal0[0],
1598 DVal1[0], copr0, copr1, copr2)) {
1599 return false;
1600 }
1601 } else {
1602 ConstantDataVector *CDV0 = dyn_cast_or_null<ConstantDataVector>(copr0);
32
Assuming null pointer is passed into cast
1603 ConstantDataVector *CDV1 = dyn_cast_or_null<ConstantDataVector>(copr1);
33
Assuming null pointer is passed into cast
1604 ConstantDataVector *CDV2 = dyn_cast_or_null<ConstantDataVector>(copr2);
34
Assuming null pointer is passed into cast
1605 for (int i=0; i < getVecSize(FInfo); ++i) {
35
Assuming the condition is false
36
Loop condition is false. Execution continues on line 1616
1606 Constant *celt0 = CDV0 ? CDV0->getElementAsConstant(i) : nullptr;
1607 Constant *celt1 = CDV1 ? CDV1->getElementAsConstant(i) : nullptr;
1608 Constant *celt2 = CDV2 ? CDV2->getElementAsConstant(i) : nullptr;
1609 if (!evaluateScalarMathFunc(FInfo, DVal0[i],
1610 DVal1[i], celt0, celt1, celt2)) {
1611 return false;
1612 }
1613 }
1614 }
1615
1616 LLVMContext &context = CI->getParent()->getParent()->getContext();
1617 Constant *nval0, *nval1;
1618 if (getVecSize(FInfo) == 1) {
37
Assuming the condition is true
38
Taking true branch
1619 nval0 = ConstantFP::get(CI->getType(), DVal0[0]);
39
2nd function call argument is an uninitialized value
1620 if (hasTwoResults)
1621 nval1 = ConstantFP::get(CI->getType(), DVal1[0]);
1622 } else {
1623 if (getArgType(FInfo) == AMDGPULibFunc::F32) {
1624 SmallVector <float, 0> FVal0, FVal1;
1625 for (int i=0; i < getVecSize(FInfo); ++i)
1626 FVal0.push_back((float)DVal0[i]);
1627 ArrayRef<float> tmp0(FVal0);
1628 nval0 = ConstantDataVector::get(context, tmp0);
1629 if (hasTwoResults) {
1630 for (int i=0; i < getVecSize(FInfo); ++i)
1631 FVal1.push_back((float)DVal1[i]);
1632 ArrayRef<float> tmp1(FVal1);
1633 nval1 = ConstantDataVector::get(context, tmp1);
1634 }
1635 } else {
1636 ArrayRef<double> tmp0(DVal0);
1637 nval0 = ConstantDataVector::get(context, tmp0);
1638 if (hasTwoResults) {
1639 ArrayRef<double> tmp1(DVal1);
1640 nval1 = ConstantDataVector::get(context, tmp1);
1641 }
1642 }
1643 }
1644
1645 if (hasTwoResults) {
1646 // sincos
1647 assert(FInfo.getId() == AMDGPULibFunc::EI_SINCOS &&(static_cast <bool> (FInfo.getId() == AMDGPULibFunc::EI_SINCOS
&& "math function with ptr arg not supported yet") ?
void (0) : __assert_fail ("FInfo.getId() == AMDGPULibFunc::EI_SINCOS && \"math function with ptr arg not supported yet\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 1648, __extension__
__PRETTY_FUNCTION__))
1648 "math function with ptr arg not supported yet")(static_cast <bool> (FInfo.getId() == AMDGPULibFunc::EI_SINCOS
&& "math function with ptr arg not supported yet") ?
void (0) : __assert_fail ("FInfo.getId() == AMDGPULibFunc::EI_SINCOS && \"math function with ptr arg not supported yet\""
, "llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp", 1648, __extension__
__PRETTY_FUNCTION__))
;
1649 new StoreInst(nval1, aCI->getArgOperand(1), aCI);
1650 }
1651
1652 replaceCall(nval0);
1653 return true;
1654}
1655
1656// Public interface to the Simplify LibCalls pass.
1657FunctionPass *llvm::createAMDGPUSimplifyLibCallsPass(const TargetMachine *TM) {
1658 return new AMDGPUSimplifyLibCalls(TM);
1659}
1660
1661FunctionPass *llvm::createAMDGPUUseNativeCallsPass() {
1662 return new AMDGPUUseNativeCalls();
1663}
1664
1665bool AMDGPUSimplifyLibCalls::runOnFunction(Function &F) {
1666 if (skipFunction(F))
1667 return false;
1668
1669 bool Changed = false;
1670 auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
1671
1672 LLVM_DEBUG(dbgs() << "AMDIC: process function ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { dbgs() << "AMDIC: process function "
; F.printAsOperand(dbgs(), false, F.getParent()); dbgs() <<
'\n';; } } while (false)
1673 F.printAsOperand(dbgs(), false, F.getParent()); dbgs() << '\n';)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { dbgs() << "AMDIC: process function "
; F.printAsOperand(dbgs(), false, F.getParent()); dbgs() <<
'\n';; } } while (false)
;
1674
1675 for (auto &BB : F) {
1676 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ) {
1677 // Ignore non-calls.
1678 CallInst *CI = dyn_cast<CallInst>(I);
1679 ++I;
1680 // Ignore intrinsics that do not become real instructions.
1681 if (!CI || isa<DbgInfoIntrinsic>(CI) || CI->isLifetimeStartOrEnd())
1682 continue;
1683
1684 // Ignore indirect calls.
1685 Function *Callee = CI->getCalledFunction();
1686 if (Callee == nullptr)
1687 continue;
1688
1689 LLVM_DEBUG(dbgs() << "AMDIC: try folding " << *CI << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { dbgs() << "AMDIC: try folding "
<< *CI << "\n"; dbgs().flush(); } } while (false
)
1690 dbgs().flush())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { dbgs() << "AMDIC: try folding "
<< *CI << "\n"; dbgs().flush(); } } while (false
)
;
1691 if(Simplifier.fold(CI, AA))
1692 Changed = true;
1693 }
1694 }
1695 return Changed;
1696}
1697
1698PreservedAnalyses AMDGPUSimplifyLibCallsPass::run(Function &F,
1699 FunctionAnalysisManager &AM) {
1700 AMDGPULibCalls Simplifier(&TM);
1701 Simplifier.initNativeFuncs();
1702
1703 bool Changed = false;
1704 auto AA = &AM.getResult<AAManager>(F);
1705
1706 LLVM_DEBUG(dbgs() << "AMDIC: process function ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { dbgs() << "AMDIC: process function "
; F.printAsOperand(dbgs(), false, F.getParent()); dbgs() <<
'\n';; } } while (false)
1
Assuming 'DebugFlag' is false
2
Loop condition is false. Exiting loop
1707 F.printAsOperand(dbgs(), false, F.getParent()); dbgs() << '\n';)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { dbgs() << "AMDIC: process function "
; F.printAsOperand(dbgs(), false, F.getParent()); dbgs() <<
'\n';; } } while (false)
;
1708
1709 for (auto &BB : F) {
1710 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E;) {
3
Loop condition is true. Entering loop body
1711 // Ignore non-calls.
1712 CallInst *CI = dyn_cast<CallInst>(I);
1713 ++I;
1714 // Ignore intrinsics that do not become real instructions.
1715 if (!CI || isa<DbgInfoIntrinsic>(CI) || CI->isLifetimeStartOrEnd())
4
Assuming 'CI' is non-null
5
Assuming 'CI' is not a 'DbgInfoIntrinsic'
6
Assuming the condition is false
7
Taking false branch
1716 continue;
1717
1718 // Ignore indirect calls.
1719 Function *Callee = CI->getCalledFunction();
1720 if (Callee == nullptr)
1721 continue;
1722
1723 LLVM_DEBUG(dbgs() << "AMDIC: try folding " << *CI << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { dbgs() << "AMDIC: try folding "
<< *CI << "\n"; dbgs().flush(); } } while (false
)
8
Taking false branch
9
Assuming 'DebugFlag' is false
10
Loop condition is false. Exiting loop
1724 dbgs().flush())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("amdgpu-simplifylib")) { dbgs() << "AMDIC: try folding "
<< *CI << "\n"; dbgs().flush(); } } while (false
)
;
1725 if (Simplifier.fold(CI, AA))
11
Calling 'AMDGPULibCalls::fold'
1726 Changed = true;
1727 }
1728 }
1729 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1730}
1731
1732bool AMDGPUUseNativeCalls::runOnFunction(Function &F) {
1733 if (skipFunction(F) || UseNative.empty())
1734 return false;
1735
1736 bool Changed = false;
1737 for (auto &BB : F) {
1738 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ) {
1739 // Ignore non-calls.
1740 CallInst *CI = dyn_cast<CallInst>(I);
1741 ++I;
1742 if (!CI) continue;
1743
1744 // Ignore indirect calls.
1745 Function *Callee = CI->getCalledFunction();
1746 if (Callee == nullptr)
1747 continue;
1748
1749 if (Simplifier.useNative(CI))
1750 Changed = true;
1751 }
1752 }
1753 return Changed;
1754}
1755
1756PreservedAnalyses AMDGPUUseNativeCallsPass::run(Function &F,
1757 FunctionAnalysisManager &AM) {
1758 if (UseNative.empty())
1759 return PreservedAnalyses::all();
1760
1761 AMDGPULibCalls Simplifier;
1762 Simplifier.initNativeFuncs();
1763
1764 bool Changed = false;
1765 for (auto &BB : F) {
1766 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E;) {
1767 // Ignore non-calls.
1768 CallInst *CI = dyn_cast<CallInst>(I);
1769 ++I;
1770 if (!CI)
1771 continue;
1772
1773 // Ignore indirect calls.
1774 Function *Callee = CI->getCalledFunction();
1775 if (Callee == nullptr)
1776 continue;
1777
1778 if (Simplifier.useNative(CI))
1779 Changed = true;
1780 }
1781 }
1782 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1783}