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