LLVM  10.0.0
AMDGPULibCalls.cpp
Go to the documentation of this file.
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 "AMDGPUSubtarget.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/StringSet.h"
20 #include "llvm/Analysis/Loads.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/Module.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Support/Debug.h"
36 #include <cmath>
37 #include <vector>
38 
39 #define DEBUG_TYPE "amdgpu-simplifylib"
40 
41 using namespace llvm;
42 
43 static cl::opt<bool> EnablePreLink("amdgpu-prelink",
44  cl::desc("Enable pre-link mode optimizations"),
45  cl::init(false),
46  cl::Hidden);
47 
48 static cl::list<std::string> UseNative("amdgpu-use-native",
49  cl::desc("Comma separated list of functions to replace with native, or all"),
51  cl::Hidden);
52 
53 #define MATH_PI numbers::pi
54 #define MATH_E numbers::e
55 #define MATH_SQRT2 numbers::sqrt2
56 #define MATH_SQRT1_2 numbers::inv_sqrt2
57 
58 namespace llvm {
59 
61 private:
62 
64 
65  const TargetMachine *TM;
66 
67  // -fuse-native.
68  bool AllNative = false;
69 
70  bool useNativeFunc(const StringRef F) const;
71 
72  // Return a pointer (pointer expr) to the function if function defintion with
73  // "FuncName" exists. It may create a new function prototype in pre-link mode.
74  FunctionCallee getFunction(Module *M, const FuncInfo &fInfo);
75 
76  // Replace a normal function with its native version.
77  bool replaceWithNative(CallInst *CI, const FuncInfo &FInfo);
78 
79  bool parseFunctionName(const StringRef& FMangledName,
80  FuncInfo *FInfo=nullptr /*out*/);
81 
82  bool TDOFold(CallInst *CI, const FuncInfo &FInfo);
83 
84  /* Specialized optimizations */
85 
86  // recip (half or native)
87  bool fold_recip(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
88 
89  // divide (half or native)
90  bool fold_divide(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
91 
92  // pow/powr/pown
93  bool fold_pow(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
94 
95  // rootn
96  bool fold_rootn(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
97 
98  // fma/mad
99  bool fold_fma_mad(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
100 
101  // -fuse-native for sincos
102  bool sincosUseNative(CallInst *aCI, const FuncInfo &FInfo);
103 
104  // evaluate calls if calls' arguments are constants.
105  bool evaluateScalarMathFunc(FuncInfo &FInfo, double& Res0,
106  double& Res1, Constant *copr0, Constant *copr1, Constant *copr2);
107  bool evaluateCall(CallInst *aCI, FuncInfo &FInfo);
108 
109  // exp
110  bool fold_exp(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
111 
112  // exp2
113  bool fold_exp2(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
114 
115  // exp10
116  bool fold_exp10(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
117 
118  // log
119  bool fold_log(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
120 
121  // log2
122  bool fold_log2(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
123 
124  // log10
125  bool fold_log10(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
126 
127  // sqrt
128  bool fold_sqrt(CallInst *CI, IRBuilder<> &B, const FuncInfo &FInfo);
129 
130  // sin/cos
131  bool fold_sincos(CallInst * CI, IRBuilder<> &B, AliasAnalysis * AA);
132 
133  // __read_pipe/__write_pipe
134  bool fold_read_write_pipe(CallInst *CI, IRBuilder<> &B, FuncInfo &FInfo);
135 
136  // llvm.amdgcn.wavefrontsize
137  bool fold_wavefrontsize(CallInst *CI, IRBuilder<> &B);
138 
139  // Get insertion point at entry.
140  BasicBlock::iterator getEntryIns(CallInst * UI);
141  // Insert an Alloc instruction.
142  AllocaInst* insertAlloca(CallInst * UI, IRBuilder<> &B, const char *prefix);
143  // Get a scalar native builtin signle argument FP function
144  FunctionCallee getNativeFunction(Module *M, const FuncInfo &FInfo);
145 
146 protected:
148 
149  bool isUnsafeMath(const CallInst *CI) const;
150 
151  void replaceCall(Value *With) {
152  CI->replaceAllUsesWith(With);
153  CI->eraseFromParent();
154  }
155 
156 public:
157  AMDGPULibCalls(const TargetMachine *TM_ = nullptr) : TM(TM_) {}
158 
159  bool fold(CallInst *CI, AliasAnalysis *AA = nullptr);
160 
161  void initNativeFuncs();
162 
163  // Replace a normal math function call with that native version
164  bool useNative(CallInst *CI);
165 };
166 
167 } // end llvm namespace
168 
169 namespace {
170 
171  class AMDGPUSimplifyLibCalls : public FunctionPass {
172 
173  const TargetOptions Options;
174 
175  AMDGPULibCalls Simplifier;
176 
177  public:
178  static char ID; // Pass identification
179 
180  AMDGPUSimplifyLibCalls(const TargetOptions &Opt = TargetOptions(),
181  const TargetMachine *TM = nullptr)
182  : FunctionPass(ID), Options(Opt), Simplifier(TM) {
184  }
185 
186  void getAnalysisUsage(AnalysisUsage &AU) const override {
188  }
189 
190  bool runOnFunction(Function &M) override;
191  };
192 
193  class AMDGPUUseNativeCalls : public FunctionPass {
194 
195  AMDGPULibCalls Simplifier;
196 
197  public:
198  static char ID; // Pass identification
199 
200  AMDGPUUseNativeCalls() : FunctionPass(ID) {
202  Simplifier.initNativeFuncs();
203  }
204 
205  bool runOnFunction(Function &F) override;
206  };
207 
208 } // end anonymous namespace.
209 
211 char AMDGPUUseNativeCalls::ID = 0;
212 
213 INITIALIZE_PASS_BEGIN(AMDGPUSimplifyLibCalls, "amdgpu-simplifylib",
214  "Simplify well-known AMD library calls", false, false)
216 INITIALIZE_PASS_END(AMDGPUSimplifyLibCalls, "amdgpu-simplifylib",
217  "Simplify well-known AMD library calls", false, false)
218 
219 INITIALIZE_PASS(AMDGPUUseNativeCalls, "amdgpu-usenative",
220  "Replace builtin math calls with that native versions.",
221  false, false)
222 
223 template <typename IRB>
224 static CallInst *CreateCallEx(IRB &B, FunctionCallee Callee, Value *Arg,
225  const Twine &Name = "") {
226  CallInst *R = B.CreateCall(Callee, Arg, Name);
227  if (Function *F = dyn_cast<Function>(Callee.getCallee()))
228  R->setCallingConv(F->getCallingConv());
229  return R;
230 }
231 
232 template <typename IRB>
233 static CallInst *CreateCallEx2(IRB &B, FunctionCallee Callee, Value *Arg1,
234  Value *Arg2, const Twine &Name = "") {
235  CallInst *R = B.CreateCall(Callee, {Arg1, Arg2}, Name);
236  if (Function *F = dyn_cast<Function>(Callee.getCallee()))
237  R->setCallingConv(F->getCallingConv());
238  return R;
239 }
240 
241 // Data structures for table-driven optimizations.
242 // FuncTbl works for both f32 and f64 functions with 1 input argument
243 
244 struct TableEntry {
245  double result;
246  double input;
247 };
248 
249 /* a list of {result, input} */
250 static const TableEntry tbl_acos[] = {
251  {MATH_PI / 2.0, 0.0},
252  {MATH_PI / 2.0, -0.0},
253  {0.0, 1.0},
254  {MATH_PI, -1.0}
255 };
256 static const TableEntry tbl_acosh[] = {
257  {0.0, 1.0}
258 };
259 static const TableEntry tbl_acospi[] = {
260  {0.5, 0.0},
261  {0.5, -0.0},
262  {0.0, 1.0},
263  {1.0, -1.0}
264 };
265 static const TableEntry tbl_asin[] = {
266  {0.0, 0.0},
267  {-0.0, -0.0},
268  {MATH_PI / 2.0, 1.0},
269  {-MATH_PI / 2.0, -1.0}
270 };
271 static const TableEntry tbl_asinh[] = {
272  {0.0, 0.0},
273  {-0.0, -0.0}
274 };
275 static const TableEntry tbl_asinpi[] = {
276  {0.0, 0.0},
277  {-0.0, -0.0},
278  {0.5, 1.0},
279  {-0.5, -1.0}
280 };
281 static const TableEntry tbl_atan[] = {
282  {0.0, 0.0},
283  {-0.0, -0.0},
284  {MATH_PI / 4.0, 1.0},
285  {-MATH_PI / 4.0, -1.0}
286 };
287 static const TableEntry tbl_atanh[] = {
288  {0.0, 0.0},
289  {-0.0, -0.0}
290 };
291 static const TableEntry tbl_atanpi[] = {
292  {0.0, 0.0},
293  {-0.0, -0.0},
294  {0.25, 1.0},
295  {-0.25, -1.0}
296 };
297 static const TableEntry tbl_cbrt[] = {
298  {0.0, 0.0},
299  {-0.0, -0.0},
300  {1.0, 1.0},
301  {-1.0, -1.0},
302 };
303 static const TableEntry tbl_cos[] = {
304  {1.0, 0.0},
305  {1.0, -0.0}
306 };
307 static const TableEntry tbl_cosh[] = {
308  {1.0, 0.0},
309  {1.0, -0.0}
310 };
311 static const TableEntry tbl_cospi[] = {
312  {1.0, 0.0},
313  {1.0, -0.0}
314 };
315 static const TableEntry tbl_erfc[] = {
316  {1.0, 0.0},
317  {1.0, -0.0}
318 };
319 static const TableEntry tbl_erf[] = {
320  {0.0, 0.0},
321  {-0.0, -0.0}
322 };
323 static const TableEntry tbl_exp[] = {
324  {1.0, 0.0},
325  {1.0, -0.0},
326  {MATH_E, 1.0}
327 };
328 static const TableEntry tbl_exp2[] = {
329  {1.0, 0.0},
330  {1.0, -0.0},
331  {2.0, 1.0}
332 };
333 static const TableEntry tbl_exp10[] = {
334  {1.0, 0.0},
335  {1.0, -0.0},
336  {10.0, 1.0}
337 };
338 static const TableEntry tbl_expm1[] = {
339  {0.0, 0.0},
340  {-0.0, -0.0}
341 };
342 static const TableEntry tbl_log[] = {
343  {0.0, 1.0},
344  {1.0, MATH_E}
345 };
346 static const TableEntry tbl_log2[] = {
347  {0.0, 1.0},
348  {1.0, 2.0}
349 };
350 static const TableEntry tbl_log10[] = {
351  {0.0, 1.0},
352  {1.0, 10.0}
353 };
354 static const TableEntry tbl_rsqrt[] = {
355  {1.0, 1.0},
356  {MATH_SQRT1_2, 2.0}
357 };
358 static const TableEntry tbl_sin[] = {
359  {0.0, 0.0},
360  {-0.0, -0.0}
361 };
362 static const TableEntry tbl_sinh[] = {
363  {0.0, 0.0},
364  {-0.0, -0.0}
365 };
366 static const TableEntry tbl_sinpi[] = {
367  {0.0, 0.0},
368  {-0.0, -0.0}
369 };
370 static const TableEntry tbl_sqrt[] = {
371  {0.0, 0.0},
372  {1.0, 1.0},
373  {MATH_SQRT2, 2.0}
374 };
375 static const TableEntry tbl_tan[] = {
376  {0.0, 0.0},
377  {-0.0, -0.0}
378 };
379 static const TableEntry tbl_tanh[] = {
380  {0.0, 0.0},
381  {-0.0, -0.0}
382 };
383 static const TableEntry tbl_tanpi[] = {
384  {0.0, 0.0},
385  {-0.0, -0.0}
386 };
387 static const TableEntry tbl_tgamma[] = {
388  {1.0, 1.0},
389  {1.0, 2.0},
390  {2.0, 3.0},
391  {6.0, 4.0}
392 };
393 
395  switch(id) {
411  return true;
412  default:;
413  }
414  return false;
415 }
416 
417 struct TableRef {
418  size_t size;
419  const TableEntry *table; // variable size: from 0 to (size - 1)
420 
421  TableRef() : size(0), table(nullptr) {}
422 
423  template <size_t N>
424  TableRef(const TableEntry (&tbl)[N]) : size(N), table(&tbl[0]) {}
425 };
426 
428  switch(id) {
429  case AMDGPULibFunc::EI_ACOS: return TableRef(tbl_acos);
430  case AMDGPULibFunc::EI_ACOSH: return TableRef(tbl_acosh);
431  case AMDGPULibFunc::EI_ACOSPI: return TableRef(tbl_acospi);
432  case AMDGPULibFunc::EI_ASIN: return TableRef(tbl_asin);
433  case AMDGPULibFunc::EI_ASINH: return TableRef(tbl_asinh);
434  case AMDGPULibFunc::EI_ASINPI: return TableRef(tbl_asinpi);
435  case AMDGPULibFunc::EI_ATAN: return TableRef(tbl_atan);
436  case AMDGPULibFunc::EI_ATANH: return TableRef(tbl_atanh);
437  case AMDGPULibFunc::EI_ATANPI: return TableRef(tbl_atanpi);
438  case AMDGPULibFunc::EI_CBRT: return TableRef(tbl_cbrt);
440  case AMDGPULibFunc::EI_COS: return TableRef(tbl_cos);
441  case AMDGPULibFunc::EI_COSH: return TableRef(tbl_cosh);
442  case AMDGPULibFunc::EI_COSPI: return TableRef(tbl_cospi);
443  case AMDGPULibFunc::EI_ERFC: return TableRef(tbl_erfc);
444  case AMDGPULibFunc::EI_ERF: return TableRef(tbl_erf);
445  case AMDGPULibFunc::EI_EXP: return TableRef(tbl_exp);
447  case AMDGPULibFunc::EI_EXP2: return TableRef(tbl_exp2);
448  case AMDGPULibFunc::EI_EXP10: return TableRef(tbl_exp10);
449  case AMDGPULibFunc::EI_EXPM1: return TableRef(tbl_expm1);
450  case AMDGPULibFunc::EI_LOG: return TableRef(tbl_log);
452  case AMDGPULibFunc::EI_LOG2: return TableRef(tbl_log2);
453  case AMDGPULibFunc::EI_LOG10: return TableRef(tbl_log10);
455  case AMDGPULibFunc::EI_RSQRT: return TableRef(tbl_rsqrt);
457  case AMDGPULibFunc::EI_SIN: return TableRef(tbl_sin);
458  case AMDGPULibFunc::EI_SINH: return TableRef(tbl_sinh);
459  case AMDGPULibFunc::EI_SINPI: return TableRef(tbl_sinpi);
461  case AMDGPULibFunc::EI_SQRT: return TableRef(tbl_sqrt);
462  case AMDGPULibFunc::EI_TAN: return TableRef(tbl_tan);
463  case AMDGPULibFunc::EI_TANH: return TableRef(tbl_tanh);
464  case AMDGPULibFunc::EI_TANPI: return TableRef(tbl_tanpi);
465  case AMDGPULibFunc::EI_TGAMMA: return TableRef(tbl_tgamma);
466  default:;
467  }
468  return TableRef();
469 }
470 
471 static inline int getVecSize(const AMDGPULibFunc& FInfo) {
472  return FInfo.getLeads()[0].VectorSize;
473 }
474 
475 static inline AMDGPULibFunc::EType getArgType(const AMDGPULibFunc& FInfo) {
476  return (AMDGPULibFunc::EType)FInfo.getLeads()[0].ArgType;
477 }
478 
479 FunctionCallee AMDGPULibCalls::getFunction(Module *M, const FuncInfo &fInfo) {
480  // If we are doing PreLinkOpt, the function is external. So it is safe to
481  // use getOrInsertFunction() at this stage.
482 
484  : AMDGPULibFunc::getFunction(M, fInfo);
485 }
486 
487 bool AMDGPULibCalls::parseFunctionName(const StringRef& FMangledName,
488  FuncInfo *FInfo) {
489  return AMDGPULibFunc::parse(FMangledName, *FInfo);
490 }
491 
493  if (auto Op = dyn_cast<FPMathOperator>(CI))
494  if (Op->isFast())
495  return true;
496  const Function *F = CI->getParent()->getParent();
497  Attribute Attr = F->getFnAttribute("unsafe-fp-math");
498  return Attr.getValueAsString() == "true";
499 }
500 
501 bool AMDGPULibCalls::useNativeFunc(const StringRef F) const {
502  return AllNative ||
503  std::find(UseNative.begin(), UseNative.end(), F) != UseNative.end();
504 }
505 
507  AllNative = useNativeFunc("all") ||
508  (UseNative.getNumOccurrences() && UseNative.size() == 1 &&
509  UseNative.begin()->empty());
510 }
511 
512 bool AMDGPULibCalls::sincosUseNative(CallInst *aCI, const FuncInfo &FInfo) {
513  bool native_sin = useNativeFunc("sin");
514  bool native_cos = useNativeFunc("cos");
515 
516  if (native_sin && native_cos) {
517  Module *M = aCI->getModule();
518  Value *opr0 = aCI->getArgOperand(0);
519 
520  AMDGPULibFunc nf;
521  nf.getLeads()[0].ArgType = FInfo.getLeads()[0].ArgType;
522  nf.getLeads()[0].VectorSize = FInfo.getLeads()[0].VectorSize;
523 
526  FunctionCallee sinExpr = getFunction(M, nf);
527 
530  FunctionCallee cosExpr = getFunction(M, nf);
531  if (sinExpr && cosExpr) {
532  Value *sinval = CallInst::Create(sinExpr, opr0, "splitsin", aCI);
533  Value *cosval = CallInst::Create(cosExpr, opr0, "splitcos", aCI);
534  new StoreInst(cosval, aCI->getArgOperand(1), aCI);
535 
536  DEBUG_WITH_TYPE("usenative", dbgs() << "<useNative> replace " << *aCI
537  << " with native version of sin/cos");
538 
539  replaceCall(sinval);
540  return true;
541  }
542  }
543  return false;
544 }
545 
547  CI = aCI;
548  Function *Callee = aCI->getCalledFunction();
549 
550  FuncInfo FInfo;
551  if (!parseFunctionName(Callee->getName(), &FInfo) || !FInfo.isMangled() ||
552  FInfo.getPrefix() != AMDGPULibFunc::NOPFX ||
553  getArgType(FInfo) == AMDGPULibFunc::F64 || !HasNative(FInfo.getId()) ||
554  !(AllNative || useNativeFunc(FInfo.getName()))) {
555  return false;
556  }
557 
558  if (FInfo.getId() == AMDGPULibFunc::EI_SINCOS)
559  return sincosUseNative(aCI, FInfo);
560 
562  FunctionCallee F = getFunction(aCI->getModule(), FInfo);
563  if (!F)
564  return false;
565 
566  aCI->setCalledFunction(F);
567  DEBUG_WITH_TYPE("usenative", dbgs() << "<useNative> replace " << *aCI
568  << " with native version");
569  return true;
570 }
571 
572 // Clang emits call of __read_pipe_2 or __read_pipe_4 for OpenCL read_pipe
573 // builtin, with appended type size and alignment arguments, where 2 or 4
574 // indicates the original number of arguments. The library has optimized version
575 // of __read_pipe_2/__read_pipe_4 when the type size and alignment has the same
576 // power of 2 value. This function transforms __read_pipe_2 to __read_pipe_2_N
577 // for such cases where N is the size in bytes of the type (N = 1, 2, 4, 8, ...,
578 // 128). The same for __read_pipe_4, write_pipe_2, and write_pipe_4.
579 bool AMDGPULibCalls::fold_read_write_pipe(CallInst *CI, IRBuilder<> &B,
580  FuncInfo &FInfo) {
581  auto *Callee = CI->getCalledFunction();
582  if (!Callee->isDeclaration())
583  return false;
584 
585  assert(Callee->hasName() && "Invalid read_pipe/write_pipe function");
586  auto *M = Callee->getParent();
587  auto &Ctx = M->getContext();
588  std::string Name = Callee->getName();
589  auto NumArg = CI->getNumArgOperands();
590  if (NumArg != 4 && NumArg != 6)
591  return false;
592  auto *PacketSize = CI->getArgOperand(NumArg - 2);
593  auto *PacketAlign = CI->getArgOperand(NumArg - 1);
594  if (!isa<ConstantInt>(PacketSize) || !isa<ConstantInt>(PacketAlign))
595  return false;
596  unsigned Size = cast<ConstantInt>(PacketSize)->getZExtValue();
597  unsigned Align = cast<ConstantInt>(PacketAlign)->getZExtValue();
598  if (Size != Align || !isPowerOf2_32(Size))
599  return false;
600 
601  Type *PtrElemTy;
602  if (Size <= 8)
603  PtrElemTy = Type::getIntNTy(Ctx, Size * 8);
604  else
605  PtrElemTy = VectorType::get(Type::getInt64Ty(Ctx), Size / 8);
606  unsigned PtrArgLoc = CI->getNumArgOperands() - 3;
607  auto PtrArg = CI->getArgOperand(PtrArgLoc);
608  unsigned PtrArgAS = PtrArg->getType()->getPointerAddressSpace();
609  auto *PtrTy = llvm::PointerType::get(PtrElemTy, PtrArgAS);
610 
612  for (unsigned I = 0; I != PtrArgLoc; ++I)
613  ArgTys.push_back(CI->getArgOperand(I)->getType());
614  ArgTys.push_back(PtrTy);
615 
616  Name = Name + "_" + std::to_string(Size);
617  auto *FTy = FunctionType::get(Callee->getReturnType(),
618  ArrayRef<Type *>(ArgTys), false);
619  AMDGPULibFunc NewLibFunc(Name, FTy);
621  if (!F)
622  return false;
623 
624  auto *BCast = B.CreatePointerCast(PtrArg, PtrTy);
626  for (unsigned I = 0; I != PtrArgLoc; ++I)
627  Args.push_back(CI->getArgOperand(I));
628  Args.push_back(BCast);
629 
630  auto *NCI = B.CreateCall(F, Args);
631  NCI->setAttributes(CI->getAttributes());
632  CI->replaceAllUsesWith(NCI);
633  CI->dropAllReferences();
634  CI->eraseFromParent();
635 
636  return true;
637 }
638 
639 // This function returns false if no change; return true otherwise.
641  this->CI = CI;
642  Function *Callee = CI->getCalledFunction();
643 
644  // Ignore indirect calls.
645  if (Callee == 0) return false;
646 
647  BasicBlock *BB = CI->getParent();
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  switch (Callee->getIntrinsicID()) {
659  default:
660  break;
661  case Intrinsic::amdgcn_wavefrontsize:
662  return !EnablePreLink && fold_wavefrontsize(CI, B);
663  }
664 
665  FuncInfo FInfo;
666  if (!parseFunctionName(Callee->getName(), &FInfo))
667  return false;
668 
669  // Further check the number of arguments to see if they match.
670  if (CI->getNumArgOperands() != FInfo.getNumArgs())
671  return false;
672 
673  if (TDOFold(CI, FInfo))
674  return true;
675 
676  // Under unsafe-math, evaluate calls if possible.
677  // According to Brian Sumner, we can do this for all f32 function calls
678  // using host's double function calls.
679  if (isUnsafeMath(CI) && evaluateCall(CI, FInfo))
680  return true;
681 
682  // Specilized optimizations for each function call
683  switch (FInfo.getId()) {
685  // skip vector function
686  assert ((FInfo.getPrefix() == AMDGPULibFunc::NATIVE ||
687  FInfo.getPrefix() == AMDGPULibFunc::HALF) &&
688  "recip must be an either native or half function");
689  return (getVecSize(FInfo) != 1) ? false : fold_recip(CI, B, FInfo);
690 
692  // skip vector function
693  assert ((FInfo.getPrefix() == AMDGPULibFunc::NATIVE ||
694  FInfo.getPrefix() == AMDGPULibFunc::HALF) &&
695  "divide must be an either native or half function");
696  return (getVecSize(FInfo) != 1) ? false : fold_divide(CI, B, FInfo);
697 
701  return fold_pow(CI, B, FInfo);
702 
704  // skip vector function
705  return (getVecSize(FInfo) != 1) ? false : fold_rootn(CI, B, FInfo);
706 
710  // skip vector function
711  return (getVecSize(FInfo) != 1) ? false : fold_fma_mad(CI, B, FInfo);
712 
714  return isUnsafeMath(CI) && fold_sqrt(CI, B, FInfo);
717  if ((getArgType(FInfo) == AMDGPULibFunc::F32 ||
718  getArgType(FInfo) == AMDGPULibFunc::F64)
719  && (FInfo.getPrefix() == AMDGPULibFunc::NOPFX))
720  return fold_sincos(CI, B, AA);
721 
722  break;
727  return fold_read_write_pipe(CI, B, FInfo);
728 
729  default:
730  break;
731  }
732 
733  return false;
734 }
735 
736 bool AMDGPULibCalls::TDOFold(CallInst *CI, const FuncInfo &FInfo) {
737  // Table-Driven optimization
738  const TableRef tr = getOptTable(FInfo.getId());
739  if (tr.size==0)
740  return false;
741 
742  int const sz = (int)tr.size;
743  const TableEntry * const ftbl = tr.table;
744  Value *opr0 = CI->getArgOperand(0);
745 
746  if (getVecSize(FInfo) > 1) {
747  if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(opr0)) {
749  for (int eltNo = 0; eltNo < getVecSize(FInfo); ++eltNo) {
750  ConstantFP *eltval = dyn_cast<ConstantFP>(
751  CV->getElementAsConstant((unsigned)eltNo));
752  assert(eltval && "Non-FP arguments in math function!");
753  bool found = false;
754  for (int i=0; i < sz; ++i) {
755  if (eltval->isExactlyValue(ftbl[i].input)) {
756  DVal.push_back(ftbl[i].result);
757  found = true;
758  break;
759  }
760  }
761  if (!found) {
762  // This vector constants not handled yet.
763  return false;
764  }
765  }
766  LLVMContext &context = CI->getParent()->getParent()->getContext();
767  Constant *nval;
768  if (getArgType(FInfo) == AMDGPULibFunc::F32) {
770  for (unsigned i = 0; i < DVal.size(); ++i) {
771  FVal.push_back((float)DVal[i]);
772  }
773  ArrayRef<float> tmp(FVal);
774  nval = ConstantDataVector::get(context, tmp);
775  } else { // F64
776  ArrayRef<double> tmp(DVal);
777  nval = ConstantDataVector::get(context, tmp);
778  }
779  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *nval << "\n");
780  replaceCall(nval);
781  return true;
782  }
783  } else {
784  // Scalar version
785  if (ConstantFP *CF = dyn_cast<ConstantFP>(opr0)) {
786  for (int i = 0; i < sz; ++i) {
787  if (CF->isExactlyValue(ftbl[i].input)) {
788  Value *nval = ConstantFP::get(CF->getType(), ftbl[i].result);
789  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *nval << "\n");
790  replaceCall(nval);
791  return true;
792  }
793  }
794  }
795  }
796 
797  return false;
798 }
799 
800 bool AMDGPULibCalls::replaceWithNative(CallInst *CI, const FuncInfo &FInfo) {
801  Module *M = CI->getModule();
802  if (getArgType(FInfo) != AMDGPULibFunc::F32 ||
803  FInfo.getPrefix() != AMDGPULibFunc::NOPFX ||
804  !HasNative(FInfo.getId()))
805  return false;
806 
807  AMDGPULibFunc nf = FInfo;
809  if (FunctionCallee FPExpr = getFunction(M, nf)) {
810  LLVM_DEBUG(dbgs() << "AMDIC: " << *CI << " ---> ");
811 
812  CI->setCalledFunction(FPExpr);
813 
814  LLVM_DEBUG(dbgs() << *CI << '\n');
815 
816  return true;
817  }
818  return false;
819 }
820 
821 // [native_]half_recip(c) ==> 1.0/c
822 bool AMDGPULibCalls::fold_recip(CallInst *CI, IRBuilder<> &B,
823  const FuncInfo &FInfo) {
824  Value *opr0 = CI->getArgOperand(0);
825  if (ConstantFP *CF = dyn_cast<ConstantFP>(opr0)) {
826  // Just create a normal div. Later, InstCombine will be able
827  // to compute the divide into a constant (avoid check float infinity
828  // or subnormal at this point).
829  Value *nval = B.CreateFDiv(ConstantFP::get(CF->getType(), 1.0),
830  opr0,
831  "recip2div");
832  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *nval << "\n");
833  replaceCall(nval);
834  return true;
835  }
836  return false;
837 }
838 
839 // [native_]half_divide(x, c) ==> x/c
840 bool AMDGPULibCalls::fold_divide(CallInst *CI, IRBuilder<> &B,
841  const FuncInfo &FInfo) {
842  Value *opr0 = CI->getArgOperand(0);
843  Value *opr1 = CI->getArgOperand(1);
844  ConstantFP *CF0 = dyn_cast<ConstantFP>(opr0);
845  ConstantFP *CF1 = dyn_cast<ConstantFP>(opr1);
846 
847  if ((CF0 && CF1) || // both are constants
848  (CF1 && (getArgType(FInfo) == AMDGPULibFunc::F32)))
849  // CF1 is constant && f32 divide
850  {
851  Value *nval1 = B.CreateFDiv(ConstantFP::get(opr1->getType(), 1.0),
852  opr1, "__div2recip");
853  Value *nval = B.CreateFMul(opr0, nval1, "__div2mul");
854  replaceCall(nval);
855  return true;
856  }
857  return false;
858 }
859 
860 namespace llvm {
861 static double log2(double V) {
862 #if _XOPEN_SOURCE >= 600 || defined(_ISOC99_SOURCE) || _POSIX_C_SOURCE >= 200112L
863  return ::log2(V);
864 #else
865  return log(V) / numbers::ln2;
866 #endif
867 }
868 }
869 
870 bool AMDGPULibCalls::fold_pow(CallInst *CI, IRBuilder<> &B,
871  const FuncInfo &FInfo) {
872  assert((FInfo.getId() == AMDGPULibFunc::EI_POW ||
873  FInfo.getId() == AMDGPULibFunc::EI_POWR ||
874  FInfo.getId() == AMDGPULibFunc::EI_POWN) &&
875  "fold_pow: encounter a wrong function call");
876 
877  Value *opr0, *opr1;
878  ConstantFP *CF;
879  ConstantInt *CINT;
880  ConstantAggregateZero *CZero;
881  Type *eltType;
882 
883  opr0 = CI->getArgOperand(0);
884  opr1 = CI->getArgOperand(1);
885  CZero = dyn_cast<ConstantAggregateZero>(opr1);
886  if (getVecSize(FInfo) == 1) {
887  eltType = opr0->getType();
888  CF = dyn_cast<ConstantFP>(opr1);
889  CINT = dyn_cast<ConstantInt>(opr1);
890  } else {
891  VectorType *VTy = dyn_cast<VectorType>(opr0->getType());
892  assert(VTy && "Oprand of vector function should be of vectortype");
893  eltType = VTy->getElementType();
895 
896  // Now, only Handle vector const whose elements have the same value.
897  CF = CDV ? dyn_cast_or_null<ConstantFP>(CDV->getSplatValue()) : nullptr;
898  CINT = CDV ? dyn_cast_or_null<ConstantInt>(CDV->getSplatValue()) : nullptr;
899  }
900 
901  // No unsafe math , no constant argument, do nothing
902  if (!isUnsafeMath(CI) && !CF && !CINT && !CZero)
903  return false;
904 
905  // 0x1111111 means that we don't do anything for this call.
906  int ci_opr1 = (CINT ? (int)CINT->getSExtValue() : 0x1111111);
907 
908  if ((CF && CF->isZero()) || (CINT && ci_opr1 == 0) || CZero) {
909  // pow/powr/pown(x, 0) == 1
910  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> 1\n");
911  Constant *cnval = ConstantFP::get(eltType, 1.0);
912  if (getVecSize(FInfo) > 1) {
913  cnval = ConstantDataVector::getSplat(getVecSize(FInfo), cnval);
914  }
915  replaceCall(cnval);
916  return true;
917  }
918  if ((CF && CF->isExactlyValue(1.0)) || (CINT && ci_opr1 == 1)) {
919  // pow/powr/pown(x, 1.0) = x
920  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << "\n");
921  replaceCall(opr0);
922  return true;
923  }
924  if ((CF && CF->isExactlyValue(2.0)) || (CINT && ci_opr1 == 2)) {
925  // pow/powr/pown(x, 2.0) = x*x
926  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << " * " << *opr0
927  << "\n");
928  Value *nval = B.CreateFMul(opr0, opr0, "__pow2");
929  replaceCall(nval);
930  return true;
931  }
932  if ((CF && CF->isExactlyValue(-1.0)) || (CINT && ci_opr1 == -1)) {
933  // pow/powr/pown(x, -1.0) = 1.0/x
934  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> 1 / " << *opr0 << "\n");
935  Constant *cnval = ConstantFP::get(eltType, 1.0);
936  if (getVecSize(FInfo) > 1) {
937  cnval = ConstantDataVector::getSplat(getVecSize(FInfo), cnval);
938  }
939  Value *nval = B.CreateFDiv(cnval, opr0, "__powrecip");
940  replaceCall(nval);
941  return true;
942  }
943 
944  Module *M = CI->getModule();
945  if (CF && (CF->isExactlyValue(0.5) || CF->isExactlyValue(-0.5))) {
946  // pow[r](x, [-]0.5) = sqrt(x)
947  bool issqrt = CF->isExactlyValue(0.5);
948  if (FunctionCallee FPExpr =
949  getFunction(M, AMDGPULibFunc(issqrt ? AMDGPULibFunc::EI_SQRT
951  FInfo))) {
952  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
953  << FInfo.getName().c_str() << "(" << *opr0 << ")\n");
954  Value *nval = CreateCallEx(B,FPExpr, opr0, issqrt ? "__pow2sqrt"
955  : "__pow2rsqrt");
956  replaceCall(nval);
957  return true;
958  }
959  }
960 
961  if (!isUnsafeMath(CI))
962  return false;
963 
964  // Unsafe Math optimization
965 
966  // Remember that ci_opr1 is set if opr1 is integral
967  if (CF) {
968  double dval = (getArgType(FInfo) == AMDGPULibFunc::F32)
969  ? (double)CF->getValueAPF().convertToFloat()
970  : CF->getValueAPF().convertToDouble();
971  int ival = (int)dval;
972  if ((double)ival == dval) {
973  ci_opr1 = ival;
974  } else
975  ci_opr1 = 0x11111111;
976  }
977 
978  // pow/powr/pown(x, c) = [1/](x*x*..x); where
979  // trunc(c) == c && the number of x == c && |c| <= 12
980  unsigned abs_opr1 = (ci_opr1 < 0) ? -ci_opr1 : ci_opr1;
981  if (abs_opr1 <= 12) {
982  Constant *cnval;
983  Value *nval;
984  if (abs_opr1 == 0) {
985  cnval = ConstantFP::get(eltType, 1.0);
986  if (getVecSize(FInfo) > 1) {
987  cnval = ConstantDataVector::getSplat(getVecSize(FInfo), cnval);
988  }
989  nval = cnval;
990  } else {
991  Value *valx2 = nullptr;
992  nval = nullptr;
993  while (abs_opr1 > 0) {
994  valx2 = valx2 ? B.CreateFMul(valx2, valx2, "__powx2") : opr0;
995  if (abs_opr1 & 1) {
996  nval = nval ? B.CreateFMul(nval, valx2, "__powprod") : valx2;
997  }
998  abs_opr1 >>= 1;
999  }
1000  }
1001 
1002  if (ci_opr1 < 0) {
1003  cnval = ConstantFP::get(eltType, 1.0);
1004  if (getVecSize(FInfo) > 1) {
1005  cnval = ConstantDataVector::getSplat(getVecSize(FInfo), cnval);
1006  }
1007  nval = B.CreateFDiv(cnval, nval, "__1powprod");
1008  }
1009  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
1010  << ((ci_opr1 < 0) ? "1/prod(" : "prod(") << *opr0
1011  << ")\n");
1012  replaceCall(nval);
1013  return true;
1014  }
1015 
1016  // powr ---> exp2(y * log2(x))
1017  // pown/pow ---> powr(fabs(x), y) | (x & ((int)y << 31))
1018  FunctionCallee ExpExpr =
1019  getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_EXP2, FInfo));
1020  if (!ExpExpr)
1021  return false;
1022 
1023  bool needlog = false;
1024  bool needabs = false;
1025  bool needcopysign = false;
1026  Constant *cnval = nullptr;
1027  if (getVecSize(FInfo) == 1) {
1028  CF = dyn_cast<ConstantFP>(opr0);
1029 
1030  if (CF) {
1031  double V = (getArgType(FInfo) == AMDGPULibFunc::F32)
1032  ? (double)CF->getValueAPF().convertToFloat()
1033  : CF->getValueAPF().convertToDouble();
1034 
1035  V = log2(std::abs(V));
1036  cnval = ConstantFP::get(eltType, V);
1037  needcopysign = (FInfo.getId() != AMDGPULibFunc::EI_POWR) &&
1038  CF->isNegative();
1039  } else {
1040  needlog = true;
1041  needcopysign = needabs = FInfo.getId() != AMDGPULibFunc::EI_POWR &&
1042  (!CF || CF->isNegative());
1043  }
1044  } else {
1046 
1047  if (!CDV) {
1048  needlog = true;
1049  needcopysign = needabs = FInfo.getId() != AMDGPULibFunc::EI_POWR;
1050  } else {
1051  assert ((int)CDV->getNumElements() == getVecSize(FInfo) &&
1052  "Wrong vector size detected");
1053 
1055  for (int i=0; i < getVecSize(FInfo); ++i) {
1056  double V = (getArgType(FInfo) == AMDGPULibFunc::F32)
1057  ? (double)CDV->getElementAsFloat(i)
1058  : CDV->getElementAsDouble(i);
1059  if (V < 0.0) needcopysign = true;
1060  V = log2(std::abs(V));
1061  DVal.push_back(V);
1062  }
1063  if (getArgType(FInfo) == AMDGPULibFunc::F32) {
1064  SmallVector<float, 0> FVal;
1065  for (unsigned i=0; i < DVal.size(); ++i) {
1066  FVal.push_back((float)DVal[i]);
1067  }
1068  ArrayRef<float> tmp(FVal);
1069  cnval = ConstantDataVector::get(M->getContext(), tmp);
1070  } else {
1071  ArrayRef<double> tmp(DVal);
1072  cnval = ConstantDataVector::get(M->getContext(), tmp);
1073  }
1074  }
1075  }
1076 
1077  if (needcopysign && (FInfo.getId() == AMDGPULibFunc::EI_POW)) {
1078  // We cannot handle corner cases for a general pow() function, give up
1079  // unless y is a constant integral value. Then proceed as if it were pown.
1080  if (getVecSize(FInfo) == 1) {
1081  if (const ConstantFP *CF = dyn_cast<ConstantFP>(opr1)) {
1082  double y = (getArgType(FInfo) == AMDGPULibFunc::F32)
1083  ? (double)CF->getValueAPF().convertToFloat()
1084  : CF->getValueAPF().convertToDouble();
1085  if (y != (double)(int64_t)y)
1086  return false;
1087  } else
1088  return false;
1089  } else {
1090  if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(opr1)) {
1091  for (int i=0; i < getVecSize(FInfo); ++i) {
1092  double y = (getArgType(FInfo) == AMDGPULibFunc::F32)
1093  ? (double)CDV->getElementAsFloat(i)
1094  : CDV->getElementAsDouble(i);
1095  if (y != (double)(int64_t)y)
1096  return false;
1097  }
1098  } else
1099  return false;
1100  }
1101  }
1102 
1103  Value *nval;
1104  if (needabs) {
1105  FunctionCallee AbsExpr =
1106  getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_FABS, FInfo));
1107  if (!AbsExpr)
1108  return false;
1109  nval = CreateCallEx(B, AbsExpr, opr0, "__fabs");
1110  } else {
1111  nval = cnval ? cnval : opr0;
1112  }
1113  if (needlog) {
1114  FunctionCallee LogExpr =
1115  getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_LOG2, FInfo));
1116  if (!LogExpr)
1117  return false;
1118  nval = CreateCallEx(B,LogExpr, nval, "__log2");
1119  }
1120 
1121  if (FInfo.getId() == AMDGPULibFunc::EI_POWN) {
1122  // convert int(32) to fp(f32 or f64)
1123  opr1 = B.CreateSIToFP(opr1, nval->getType(), "pownI2F");
1124  }
1125  nval = B.CreateFMul(opr1, nval, "__ylogx");
1126  nval = CreateCallEx(B,ExpExpr, nval, "__exp2");
1127 
1128  if (needcopysign) {
1129  Value *opr_n;
1130  Type* rTy = opr0->getType();
1131  Type* nTyS = eltType->isDoubleTy() ? B.getInt64Ty() : B.getInt32Ty();
1132  Type *nTy = nTyS;
1133  if (const VectorType *vTy = dyn_cast<VectorType>(rTy))
1134  nTy = VectorType::get(nTyS, vTy->getNumElements());
1135  unsigned size = nTy->getScalarSizeInBits();
1136  opr_n = CI->getArgOperand(1);
1137  if (opr_n->getType()->isIntegerTy())
1138  opr_n = B.CreateZExtOrBitCast(opr_n, nTy, "__ytou");
1139  else
1140  opr_n = B.CreateFPToSI(opr1, nTy, "__ytou");
1141 
1142  Value *sign = B.CreateShl(opr_n, size-1, "__yeven");
1143  sign = B.CreateAnd(B.CreateBitCast(opr0, nTy), sign, "__pow_sign");
1144  nval = B.CreateOr(B.CreateBitCast(nval, nTy), sign);
1145  nval = B.CreateBitCast(nval, opr0->getType());
1146  }
1147 
1148  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
1149  << "exp2(" << *opr1 << " * log2(" << *opr0 << "))\n");
1150  replaceCall(nval);
1151 
1152  return true;
1153 }
1154 
1155 bool AMDGPULibCalls::fold_rootn(CallInst *CI, IRBuilder<> &B,
1156  const FuncInfo &FInfo) {
1157  Value *opr0 = CI->getArgOperand(0);
1158  Value *opr1 = CI->getArgOperand(1);
1159 
1160  ConstantInt *CINT = dyn_cast<ConstantInt>(opr1);
1161  if (!CINT) {
1162  return false;
1163  }
1164  int ci_opr1 = (int)CINT->getSExtValue();
1165  if (ci_opr1 == 1) { // rootn(x, 1) = x
1166  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << "\n");
1167  replaceCall(opr0);
1168  return true;
1169  }
1170  if (ci_opr1 == 2) { // rootn(x, 2) = sqrt(x)
1171  Module *M = CI->getModule();
1172  if (FunctionCallee FPExpr =
1173  getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_SQRT, FInfo))) {
1174  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> sqrt(" << *opr0 << ")\n");
1175  Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2sqrt");
1176  replaceCall(nval);
1177  return true;
1178  }
1179  } else if (ci_opr1 == 3) { // rootn(x, 3) = cbrt(x)
1180  Module *M = CI->getModule();
1181  if (FunctionCallee FPExpr =
1182  getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_CBRT, FInfo))) {
1183  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> cbrt(" << *opr0 << ")\n");
1184  Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2cbrt");
1185  replaceCall(nval);
1186  return true;
1187  }
1188  } else if (ci_opr1 == -1) { // rootn(x, -1) = 1.0/x
1189  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> 1.0 / " << *opr0 << "\n");
1190  Value *nval = B.CreateFDiv(ConstantFP::get(opr0->getType(), 1.0),
1191  opr0,
1192  "__rootn2div");
1193  replaceCall(nval);
1194  return true;
1195  } else if (ci_opr1 == -2) { // rootn(x, -2) = rsqrt(x)
1196  Module *M = CI->getModule();
1197  if (FunctionCallee FPExpr =
1198  getFunction(M, AMDGPULibFunc(AMDGPULibFunc::EI_RSQRT, FInfo))) {
1199  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> rsqrt(" << *opr0
1200  << ")\n");
1201  Value *nval = CreateCallEx(B,FPExpr, opr0, "__rootn2rsqrt");
1202  replaceCall(nval);
1203  return true;
1204  }
1205  }
1206  return false;
1207 }
1208 
1209 bool AMDGPULibCalls::fold_fma_mad(CallInst *CI, IRBuilder<> &B,
1210  const FuncInfo &FInfo) {
1211  Value *opr0 = CI->getArgOperand(0);
1212  Value *opr1 = CI->getArgOperand(1);
1213  Value *opr2 = CI->getArgOperand(2);
1214 
1215  ConstantFP *CF0 = dyn_cast<ConstantFP>(opr0);
1216  ConstantFP *CF1 = dyn_cast<ConstantFP>(opr1);
1217  if ((CF0 && CF0->isZero()) || (CF1 && CF1->isZero())) {
1218  // fma/mad(a, b, c) = c if a=0 || b=0
1219  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr2 << "\n");
1220  replaceCall(opr2);
1221  return true;
1222  }
1223  if (CF0 && CF0->isExactlyValue(1.0f)) {
1224  // fma/mad(a, b, c) = b+c if a=1
1225  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr1 << " + " << *opr2
1226  << "\n");
1227  Value *nval = B.CreateFAdd(opr1, opr2, "fmaadd");
1228  replaceCall(nval);
1229  return true;
1230  }
1231  if (CF1 && CF1->isExactlyValue(1.0f)) {
1232  // fma/mad(a, b, c) = a+c if b=1
1233  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << " + " << *opr2
1234  << "\n");
1235  Value *nval = B.CreateFAdd(opr0, opr2, "fmaadd");
1236  replaceCall(nval);
1237  return true;
1238  }
1239  if (ConstantFP *CF = dyn_cast<ConstantFP>(opr2)) {
1240  if (CF->isZero()) {
1241  // fma/mad(a, b, c) = a*b if c=0
1242  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> " << *opr0 << " * "
1243  << *opr1 << "\n");
1244  Value *nval = B.CreateFMul(opr0, opr1, "fmamul");
1245  replaceCall(nval);
1246  return true;
1247  }
1248  }
1249 
1250  return false;
1251 }
1252 
1253 // Get a scalar native builtin signle argument FP function
1254 FunctionCallee AMDGPULibCalls::getNativeFunction(Module *M,
1255  const FuncInfo &FInfo) {
1256  if (getArgType(FInfo) == AMDGPULibFunc::F64 || !HasNative(FInfo.getId()))
1257  return nullptr;
1258  FuncInfo nf = FInfo;
1259  nf.setPrefix(AMDGPULibFunc::NATIVE);
1260  return getFunction(M, nf);
1261 }
1262 
1263 // fold sqrt -> native_sqrt (x)
1264 bool AMDGPULibCalls::fold_sqrt(CallInst *CI, IRBuilder<> &B,
1265  const FuncInfo &FInfo) {
1266  if (getArgType(FInfo) == AMDGPULibFunc::F32 && (getVecSize(FInfo) == 1) &&
1267  (FInfo.getPrefix() != AMDGPULibFunc::NATIVE)) {
1268  if (FunctionCallee FPExpr = getNativeFunction(
1270  Value *opr0 = CI->getArgOperand(0);
1271  LLVM_DEBUG(errs() << "AMDIC: " << *CI << " ---> "
1272  << "sqrt(" << *opr0 << ")\n");
1273  Value *nval = CreateCallEx(B,FPExpr, opr0, "__sqrt");
1274  replaceCall(nval);
1275  return true;
1276  }
1277  }
1278  return false;
1279 }
1280 
1281 // fold sin, cos -> sincos.
1282 bool AMDGPULibCalls::fold_sincos(CallInst *CI, IRBuilder<> &B,
1283  AliasAnalysis *AA) {
1284  AMDGPULibFunc fInfo;
1285  if (!AMDGPULibFunc::parse(CI->getCalledFunction()->getName(), fInfo))
1286  return false;
1287 
1288  assert(fInfo.getId() == AMDGPULibFunc::EI_SIN ||
1289  fInfo.getId() == AMDGPULibFunc::EI_COS);
1290  bool const isSin = fInfo.getId() == AMDGPULibFunc::EI_SIN;
1291 
1292  Value *CArgVal = CI->getArgOperand(0);
1293  BasicBlock * const CBB = CI->getParent();
1294 
1295  int const MaxScan = 30;
1296 
1297  { // fold in load value.
1298  LoadInst *LI = dyn_cast<LoadInst>(CArgVal);
1299  if (LI && LI->getParent() == CBB) {
1300  BasicBlock::iterator BBI = LI->getIterator();
1301  Value *AvailableVal = FindAvailableLoadedValue(LI, CBB, BBI, MaxScan, AA);
1302  if (AvailableVal) {
1303  CArgVal->replaceAllUsesWith(AvailableVal);
1304  if (CArgVal->getNumUses() == 0)
1305  LI->eraseFromParent();
1306  CArgVal = CI->getArgOperand(0);
1307  }
1308  }
1309  }
1310 
1311  Module *M = CI->getModule();
1313  std::string const PairName = fInfo.mangle();
1314 
1315  CallInst *UI = nullptr;
1316  for (User* U : CArgVal->users()) {
1317  CallInst *XI = dyn_cast_or_null<CallInst>(U);
1318  if (!XI || XI == CI || XI->getParent() != CBB)
1319  continue;
1320 
1321  Function *UCallee = XI->getCalledFunction();
1322  if (!UCallee || !UCallee->getName().equals(PairName))
1323  continue;
1324 
1325  BasicBlock::iterator BBI = CI->getIterator();
1326  if (BBI == CI->getParent()->begin())
1327  break;
1328  --BBI;
1329  for (int I = MaxScan; I > 0 && BBI != CBB->begin(); --BBI, --I) {
1330  if (cast<Instruction>(BBI) == XI) {
1331  UI = XI;
1332  break;
1333  }
1334  }
1335  if (UI) break;
1336  }
1337 
1338  if (!UI) return false;
1339 
1340  // Merge the sin and cos.
1341 
1342  // for OpenCL 2.0 we have only generic implementation of sincos
1343  // function.
1346  FunctionCallee Fsincos = getFunction(M, nf);
1347  if (!Fsincos) return false;
1348 
1350  AllocaInst *Alloc = insertAlloca(UI, B, "__sincos_");
1351  B.SetInsertPoint(UI);
1352 
1353  Value *P = Alloc;
1354  Type *PTy = Fsincos.getFunctionType()->getParamType(1);
1355  // The allocaInst allocates the memory in private address space. This need
1356  // to be bitcasted to point to the address space of cos pointer type.
1357  // In OpenCL 2.0 this is generic, while in 1.2 that is private.
1359  P = B.CreateAddrSpaceCast(Alloc, PTy);
1360  CallInst *Call = CreateCallEx2(B, Fsincos, UI->getArgOperand(0), P);
1361 
1362  LLVM_DEBUG(errs() << "AMDIC: fold_sincos (" << *CI << ", " << *UI << ") with "
1363  << *Call << "\n");
1364 
1365  if (!isSin) { // CI->cos, UI->sin
1366  B.SetInsertPoint(&*ItOld);
1367  UI->replaceAllUsesWith(&*Call);
1368  Instruction *Reload = B.CreateLoad(Alloc->getAllocatedType(), Alloc);
1369  CI->replaceAllUsesWith(Reload);
1370  UI->eraseFromParent();
1371  CI->eraseFromParent();
1372  } else { // CI->sin, UI->cos
1373  Instruction *Reload = B.CreateLoad(Alloc->getAllocatedType(), Alloc);
1374  UI->replaceAllUsesWith(Reload);
1375  CI->replaceAllUsesWith(Call);
1376  UI->eraseFromParent();
1377  CI->eraseFromParent();
1378  }
1379  return true;
1380 }
1381 
1382 bool AMDGPULibCalls::fold_wavefrontsize(CallInst *CI, IRBuilder<> &B) {
1383  if (!TM)
1384  return false;
1385 
1386  StringRef CPU = TM->getTargetCPU();
1387  StringRef Features = TM->getTargetFeatureString();
1388  if ((CPU.empty() || CPU.equals_lower("generic")) &&
1389  (Features.empty() ||
1390  Features.find_lower("wavefrontsize") == StringRef::npos))
1391  return false;
1392 
1393  Function *F = CI->getParent()->getParent();
1394  const GCNSubtarget &ST = TM->getSubtarget<GCNSubtarget>(*F);
1395  unsigned N = ST.getWavefrontSize();
1396 
1397  LLVM_DEBUG(errs() << "AMDIC: fold_wavefrontsize (" << *CI << ") with "
1398  << N << "\n");
1399 
1401  CI->eraseFromParent();
1402  return true;
1403 }
1404 
1405 // Get insertion point at entry.
1406 BasicBlock::iterator AMDGPULibCalls::getEntryIns(CallInst * UI) {
1407  Function * Func = UI->getParent()->getParent();
1408  BasicBlock * BB = &Func->getEntryBlock();
1409  assert(BB && "Entry block not found!");
1410  BasicBlock::iterator ItNew = BB->begin();
1411  return ItNew;
1412 }
1413 
1414 // Insert a AllocsInst at the beginning of function entry block.
1415 AllocaInst* AMDGPULibCalls::insertAlloca(CallInst *UI, IRBuilder<> &B,
1416  const char *prefix) {
1417  BasicBlock::iterator ItNew = getEntryIns(UI);
1418  Function *UCallee = UI->getCalledFunction();
1419  Type *RetType = UCallee->getReturnType();
1420  B.SetInsertPoint(&*ItNew);
1421  AllocaInst *Alloc = B.CreateAlloca(RetType, 0,
1422  std::string(prefix) + UI->getName());
1423  Alloc->setAlignment(MaybeAlign(
1424  UCallee->getParent()->getDataLayout().getTypeAllocSize(RetType)));
1425  return Alloc;
1426 }
1427 
1428 bool AMDGPULibCalls::evaluateScalarMathFunc(FuncInfo &FInfo,
1429  double& Res0, double& Res1,
1430  Constant *copr0, Constant *copr1,
1431  Constant *copr2) {
1432  // By default, opr0/opr1/opr3 holds values of float/double type.
1433  // If they are not float/double, each function has to its
1434  // operand separately.
1435  double opr0=0.0, opr1=0.0, opr2=0.0;
1436  ConstantFP *fpopr0 = dyn_cast_or_null<ConstantFP>(copr0);
1437  ConstantFP *fpopr1 = dyn_cast_or_null<ConstantFP>(copr1);
1438  ConstantFP *fpopr2 = dyn_cast_or_null<ConstantFP>(copr2);
1439  if (fpopr0) {
1440  opr0 = (getArgType(FInfo) == AMDGPULibFunc::F64)
1441  ? fpopr0->getValueAPF().convertToDouble()
1442  : (double)fpopr0->getValueAPF().convertToFloat();
1443  }
1444 
1445  if (fpopr1) {
1446  opr1 = (getArgType(FInfo) == AMDGPULibFunc::F64)
1447  ? fpopr1->getValueAPF().convertToDouble()
1448  : (double)fpopr1->getValueAPF().convertToFloat();
1449  }
1450 
1451  if (fpopr2) {
1452  opr2 = (getArgType(FInfo) == AMDGPULibFunc::F64)
1453  ? fpopr2->getValueAPF().convertToDouble()
1454  : (double)fpopr2->getValueAPF().convertToFloat();
1455  }
1456 
1457  switch (FInfo.getId()) {
1458  default : return false;
1459 
1461  Res0 = acos(opr0);
1462  return true;
1463 
1465  // acosh(x) == log(x + sqrt(x*x - 1))
1466  Res0 = log(opr0 + sqrt(opr0*opr0 - 1.0));
1467  return true;
1468 
1470  Res0 = acos(opr0) / MATH_PI;
1471  return true;
1472 
1474  Res0 = asin(opr0);
1475  return true;
1476 
1478  // asinh(x) == log(x + sqrt(x*x + 1))
1479  Res0 = log(opr0 + sqrt(opr0*opr0 + 1.0));
1480  return true;
1481 
1483  Res0 = asin(opr0) / MATH_PI;
1484  return true;
1485 
1487  Res0 = atan(opr0);
1488  return true;
1489 
1491  // atanh(x) == (log(x+1) - log(x-1))/2;
1492  Res0 = (log(opr0 + 1.0) - log(opr0 - 1.0))/2.0;
1493  return true;
1494 
1496  Res0 = atan(opr0) / MATH_PI;
1497  return true;
1498 
1500  Res0 = (opr0 < 0.0) ? -pow(-opr0, 1.0/3.0) : pow(opr0, 1.0/3.0);
1501  return true;
1502 
1503  case AMDGPULibFunc::EI_COS:
1504  Res0 = cos(opr0);
1505  return true;
1506 
1508  Res0 = cosh(opr0);
1509  return true;
1510 
1512  Res0 = cos(MATH_PI * opr0);
1513  return true;
1514 
1515  case AMDGPULibFunc::EI_EXP:
1516  Res0 = exp(opr0);
1517  return true;
1518 
1520  Res0 = pow(2.0, opr0);
1521  return true;
1522 
1524  Res0 = pow(10.0, opr0);
1525  return true;
1526 
1528  Res0 = exp(opr0) - 1.0;
1529  return true;
1530 
1531  case AMDGPULibFunc::EI_LOG:
1532  Res0 = log(opr0);
1533  return true;
1534 
1536  Res0 = log(opr0) / log(2.0);
1537  return true;
1538 
1540  Res0 = log(opr0) / log(10.0);
1541  return true;
1542 
1544  Res0 = 1.0 / sqrt(opr0);
1545  return true;
1546 
1547  case AMDGPULibFunc::EI_SIN:
1548  Res0 = sin(opr0);
1549  return true;
1550 
1552  Res0 = sinh(opr0);
1553  return true;
1554 
1556  Res0 = sin(MATH_PI * opr0);
1557  return true;
1558 
1560  Res0 = sqrt(opr0);
1561  return true;
1562 
1563  case AMDGPULibFunc::EI_TAN:
1564  Res0 = tan(opr0);
1565  return true;
1566 
1568  Res0 = tanh(opr0);
1569  return true;
1570 
1572  Res0 = tan(MATH_PI * opr0);
1573  return true;
1574 
1576  Res0 = 1.0 / opr0;
1577  return true;
1578 
1579  // two-arg functions
1581  Res0 = opr0 / opr1;
1582  return true;
1583 
1584  case AMDGPULibFunc::EI_POW:
1586  Res0 = pow(opr0, opr1);
1587  return true;
1588 
1589  case AMDGPULibFunc::EI_POWN: {
1590  if (ConstantInt *iopr1 = dyn_cast_or_null<ConstantInt>(copr1)) {
1591  double val = (double)iopr1->getSExtValue();
1592  Res0 = pow(opr0, val);
1593  return true;
1594  }
1595  return false;
1596  }
1597 
1598  case AMDGPULibFunc::EI_ROOTN: {
1599  if (ConstantInt *iopr1 = dyn_cast_or_null<ConstantInt>(copr1)) {
1600  double val = (double)iopr1->getSExtValue();
1601  Res0 = pow(opr0, 1.0 / val);
1602  return true;
1603  }
1604  return false;
1605  }
1606 
1607  // with ptr arg
1609  Res0 = sin(opr0);
1610  Res1 = cos(opr0);
1611  return true;
1612 
1613  // three-arg functions
1614  case AMDGPULibFunc::EI_FMA:
1615  case AMDGPULibFunc::EI_MAD:
1616  Res0 = opr0 * opr1 + opr2;
1617  return true;
1618  }
1619 
1620  return false;
1621 }
1622 
1623 bool AMDGPULibCalls::evaluateCall(CallInst *aCI, FuncInfo &FInfo) {
1624  int numArgs = (int)aCI->getNumArgOperands();
1625  if (numArgs > 3)
1626  return false;
1627 
1628  Constant *copr0 = nullptr;
1629  Constant *copr1 = nullptr;
1630  Constant *copr2 = nullptr;
1631  if (numArgs > 0) {
1632  if ((copr0 = dyn_cast<Constant>(aCI->getArgOperand(0))) == nullptr)
1633  return false;
1634  }
1635 
1636  if (numArgs > 1) {
1637  if ((copr1 = dyn_cast<Constant>(aCI->getArgOperand(1))) == nullptr) {
1638  if (FInfo.getId() != AMDGPULibFunc::EI_SINCOS)
1639  return false;
1640  }
1641  }
1642 
1643  if (numArgs > 2) {
1644  if ((copr2 = dyn_cast<Constant>(aCI->getArgOperand(2))) == nullptr)
1645  return false;
1646  }
1647 
1648  // At this point, all arguments to aCI are constants.
1649 
1650  // max vector size is 16, and sincos will generate two results.
1651  double DVal0[16], DVal1[16];
1652  bool hasTwoResults = (FInfo.getId() == AMDGPULibFunc::EI_SINCOS);
1653  if (getVecSize(FInfo) == 1) {
1654  if (!evaluateScalarMathFunc(FInfo, DVal0[0],
1655  DVal1[0], copr0, copr1, copr2)) {
1656  return false;
1657  }
1658  } else {
1659  ConstantDataVector *CDV0 = dyn_cast_or_null<ConstantDataVector>(copr0);
1660  ConstantDataVector *CDV1 = dyn_cast_or_null<ConstantDataVector>(copr1);
1661  ConstantDataVector *CDV2 = dyn_cast_or_null<ConstantDataVector>(copr2);
1662  for (int i=0; i < getVecSize(FInfo); ++i) {
1663  Constant *celt0 = CDV0 ? CDV0->getElementAsConstant(i) : nullptr;
1664  Constant *celt1 = CDV1 ? CDV1->getElementAsConstant(i) : nullptr;
1665  Constant *celt2 = CDV2 ? CDV2->getElementAsConstant(i) : nullptr;
1666  if (!evaluateScalarMathFunc(FInfo, DVal0[i],
1667  DVal1[i], celt0, celt1, celt2)) {
1668  return false;
1669  }
1670  }
1671  }
1672 
1673  LLVMContext &context = CI->getParent()->getParent()->getContext();
1674  Constant *nval0, *nval1;
1675  if (getVecSize(FInfo) == 1) {
1676  nval0 = ConstantFP::get(CI->getType(), DVal0[0]);
1677  if (hasTwoResults)
1678  nval1 = ConstantFP::get(CI->getType(), DVal1[0]);
1679  } else {
1680  if (getArgType(FInfo) == AMDGPULibFunc::F32) {
1681  SmallVector <float, 0> FVal0, FVal1;
1682  for (int i=0; i < getVecSize(FInfo); ++i)
1683  FVal0.push_back((float)DVal0[i]);
1684  ArrayRef<float> tmp0(FVal0);
1685  nval0 = ConstantDataVector::get(context, tmp0);
1686  if (hasTwoResults) {
1687  for (int i=0; i < getVecSize(FInfo); ++i)
1688  FVal1.push_back((float)DVal1[i]);
1689  ArrayRef<float> tmp1(FVal1);
1690  nval1 = ConstantDataVector::get(context, tmp1);
1691  }
1692  } else {
1693  ArrayRef<double> tmp0(DVal0);
1694  nval0 = ConstantDataVector::get(context, tmp0);
1695  if (hasTwoResults) {
1696  ArrayRef<double> tmp1(DVal1);
1697  nval1 = ConstantDataVector::get(context, tmp1);
1698  }
1699  }
1700  }
1701 
1702  if (hasTwoResults) {
1703  // sincos
1704  assert(FInfo.getId() == AMDGPULibFunc::EI_SINCOS &&
1705  "math function with ptr arg not supported yet");
1706  new StoreInst(nval1, aCI->getArgOperand(1), aCI);
1707  }
1708 
1709  replaceCall(nval0);
1710  return true;
1711 }
1712 
1713 // Public interface to the Simplify LibCalls pass.
1715  const TargetMachine *TM) {
1716  return new AMDGPUSimplifyLibCalls(Opt, TM);
1717 }
1718 
1720  return new AMDGPUUseNativeCalls();
1721 }
1722 
1723 static bool setFastFlags(Function &F, const TargetOptions &Options) {
1724  AttrBuilder B;
1725 
1726  if (Options.UnsafeFPMath || Options.NoInfsFPMath)
1727  B.addAttribute("no-infs-fp-math", "true");
1728  if (Options.UnsafeFPMath || Options.NoNaNsFPMath)
1729  B.addAttribute("no-nans-fp-math", "true");
1730  if (Options.UnsafeFPMath) {
1731  B.addAttribute("less-precise-fpmad", "true");
1732  B.addAttribute("unsafe-fp-math", "true");
1733  }
1734 
1735  if (!B.hasAttributes())
1736  return false;
1737 
1739 
1740  return true;
1741 }
1742 
1744  if (skipFunction(F))
1745  return false;
1746 
1747  bool Changed = false;
1748  auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
1749 
1750  LLVM_DEBUG(dbgs() << "AMDIC: process function ";
1751  F.printAsOperand(dbgs(), false, F.getParent()); dbgs() << '\n';);
1752 
1753  if (!EnablePreLink)
1754  Changed |= setFastFlags(F, Options);
1755 
1756  for (auto &BB : F) {
1757  for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ) {
1758  // Ignore non-calls.
1759  CallInst *CI = dyn_cast<CallInst>(I);
1760  ++I;
1761  if (!CI) continue;
1762 
1763  // Ignore indirect calls.
1764  Function *Callee = CI->getCalledFunction();
1765  if (Callee == 0) continue;
1766 
1767  LLVM_DEBUG(dbgs() << "AMDIC: try folding " << *CI << "\n";
1768  dbgs().flush());
1769  if(Simplifier.fold(CI, AA))
1770  Changed = true;
1771  }
1772  }
1773  return Changed;
1774 }
1775 
1777  if (skipFunction(F) || UseNative.empty())
1778  return false;
1779 
1780  bool Changed = false;
1781  for (auto &BB : F) {
1782  for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ) {
1783  // Ignore non-calls.
1784  CallInst *CI = dyn_cast<CallInst>(I);
1785  ++I;
1786  if (!CI) continue;
1787 
1788  // Ignore indirect calls.
1789  Function *Callee = CI->getCalledFunction();
1790  if (Callee == 0) continue;
1791 
1792  if(Simplifier.useNative(CI))
1793  Changed = true;
1794  }
1795  }
1796  return Changed;
1797 }
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
Definition: Constants.h:762
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
static const TableEntry tbl_sqrt[]
static const TableEntry tbl_log10[]
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVMContext & Context
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1656
AMDGPU specific subclass of TargetSubtarget.
void dropAllReferences()
Drop all references to operands.
Definition: User.h:294
EFuncId getId() const
Value * CreateAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2106
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:140
Constant * getElementAsConstant(unsigned i) const
Return a Constant for a specified index&#39;s element.
Definition: Constants.cpp:2841
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve &#39;CreateLoad(Ty, Ptr, "...")&#39; correctly, instead of converting the string to &#39;bool...
Definition: IRBuilder.h:1669
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:188
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static const TableEntry tbl_cospi[]
amdgpu simplifylib
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
static const TableEntry tbl_cbrt[]
static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr)
void initializeAMDGPUSimplifyLibCallsPass(PassRegistry &)
This class represents a function call, abstracting a target machine&#39;s calling convention.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:637
bool isUnsafeMath(const CallInst *CI) const
float convertToFloat() const
Definition: APFloat.h:1118
F(f)
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:635
An instruction for reading from memory.
Definition: Instructions.h:169
static const TableEntry tbl_erf[]
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:181
AttrBuilder & addAttribute(Attribute::AttrKind Val)
Add an attribute to the builder.
void setPrefix(ENamePrefix PFX)
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
Address space for flat memory.
Definition: AMDGPU.h:271
static const TableEntry tbl_acosh[]
static FunctionCallee getOrInsertFunction(llvm::Module *M, const AMDGPULibFunc &fInfo)
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:273
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition: Debug.h:64
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:395
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1246
static const TableEntry tbl_atanh[]
std::string getName() const
Get unmangled name for mangled library function and name for unmangled library function.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
bool hasAttributes() const
Return true if the builder has IR-level attributes.
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Definition: IRBuilder.h:400
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, AliasAnalysis *AA=nullptr, bool *IsLoadCSE=nullptr, unsigned *NumScanedInst=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
Definition: Loads.cpp:361
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
Param * getLeads()
Get leading parameters for mangled lib functions.
static const TableEntry tbl_sinh[]
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
unsigned getNumArgs() const
INITIALIZE_PASS_BEGIN(AMDGPUSimplifyLibCalls, "amdgpu-simplifylib", "Simplify well-known AMD library calls", false, false) INITIALIZE_PASS_END(AMDGPUSimplifyLibCalls
All zero aggregate value.
Definition: Constants.h:340
void setId(EFuncId Id)
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2101
unsigned NoNaNsFPMath
NoNaNsFPMath - This flag is enabled when the -enable-no-nans-fp-math flag is specified on the command...
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
amdgpu Simplify well known AMD library false
static const TableEntry tbl_sinpi[]
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
Definition: InstrTypes.h:1341
bool isNegative() const
Return true if the sign bit is set.
Definition: Constants.h:308
static const TableEntry tbl_asinh[]
static const TableEntry tbl_tanpi[]
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
Value * CreateSIToFP(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2068
An instruction for storing to memory.
Definition: Instructions.h:331
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:430
Value * CreateZExtOrBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2111
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:132
static const TableEntry tbl_log2[]
constexpr double ln2
Definition: MathExtras.h:59
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1387
static AMDGPULibFunc::EType getArgType(const AMDGPULibFunc &FInfo)
const BasicBlock & getEntryBlock() const
Definition: Function.h:675
static bool runOnFunction(Function &F, bool PostInlining)
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
TableRef(const TableEntry(&tbl)[N])
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:168
bool fold(CallInst *CI, AliasAnalysis *AA=nullptr)
unsigned UnsafeFPMath
UnsafeFPMath - This flag is enabled when the -enable-unsafe-fp-math flag is specified on the command ...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Value * CreateFMul(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1476
static const TableEntry tbl_exp2[]
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:465
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
static Function * getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo)
Constant * getSplatValue() const
If this is a splat constant, meaning that all of the elements have the same value, return that value.
Definition: Constants.cpp:2878
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:486
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:263
static cl::opt< bool > EnablePreLink("amdgpu-prelink", cl::desc("Enable pre-link mode optimizations"), cl::init(false), cl::Hidden)
double convertToDouble() const
Definition: APFloat.h:1117
float getElementAsFloat(unsigned i) const
If this is an sequential container of floats, return the specified element as a float.
Definition: Constants.cpp:2829
static const TableEntry tbl_atan[]
static const TableEntry tbl_sin[]
bool isMangled() const
Represent the analysis usage information of a pass.
static const TableEntry tbl_rsqrt[]
static const TableEntry tbl_acospi[]
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1366
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:301
Wrapper class for AMDGPULIbFuncImpl.
static const TableEntry tbl_expm1[]
self_iterator getIterator()
Definition: ilist_node.h:81
static int getVecSize(const AMDGPULibFunc &FInfo)
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:2718
unsigned NoInfsFPMath
NoInfsFPMath - This flag is enabled when the -enable-no-infs-fp-math flag is specified on the command...
static double log2(double V)
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:223
static unsigned getEPtrKindFromAddrSpace(unsigned AS)
size_t size() const
Definition: SmallVector.h:52
void initializeAMDGPUUseNativeCallsPass(PassRegistry &)
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1203
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:4393
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
static const TableEntry tbl_atanpi[]
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:105
const APFloat & getValueAPF() const
Definition: Constants.h:302
unsigned getWavefrontSize() const
static const TableEntry tbl_exp10[]
AMDGPULibCalls(const TargetMachine *TM_=nullptr)
Iterator for intrusive lists based on ilist_node.
TargetPassConfig.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
static const TableEntry tbl_asinpi[]
static bool setFastFlags(Function &F, const TargetOptions &Options)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:134
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
Definition: Alignment.h:117
ENamePrefix getPrefix() const
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:246
Module.h This file contains the declarations for the Module class.
LLVM_NODISCARD size_t find_lower(char C, size_t From=0) const
Search for the first character C in the string, ignoring case.
Definition: StringRef.cpp:58
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition: Type.cpp:184
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:704
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:767
const TableEntry * table
AttributeList getAttributes() const
Return the parameter attributes for this call.
Definition: InstrTypes.h:1384
static const TableEntry tbl_log[]
void setAlignment(MaybeAlign Align)
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:193
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Class to represent vector types.
Definition: DerivedTypes.h:432
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:55
static const TableEntry tbl_cosh[]
amdgpu Simplify well known AMD library false FunctionCallee Callee
iterator_range< user_iterator > users()
Definition: Value.h:420
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:89
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1300
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2147
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:181
unsigned getNumUses() const
This method computes the number of uses of this Value.
Definition: Value.cpp:161
unsigned getNumArgOperands() const
Definition: InstrTypes.h:1244
static const TableEntry tbl_exp[]
static const size_t npos
Definition: StringRef.h:56
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:220
static const TableEntry tbl_tanh[]
bool isZero() const
Return true if the value is positive or negative zero.
Definition: Constants.h:305
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:614
double getElementAsDouble(unsigned i) const
If this is an sequential container of doubles, return the specified element as a double.
Definition: Constants.cpp:2835
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:215
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1305
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
#define I(x, y, z)
Definition: MD5.cpp:58
static TableRef getOptTable(AMDGPULibFunc::EFuncId id)
#define N
#define MATH_PI
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1233
static const TableEntry tbl_asin[]
Value * CreateFPToSI(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2054
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1388
FunctionType * getFunctionType()
Definition: DerivedTypes.h:187
static cl::list< std::string > UseNative("amdgpu-use-native", cl::desc("Comma separated list of functions to replace with native, or all"), cl::CommaSeparated, cl::ValueOptional, cl::Hidden)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2419
Address space for private memory.
Definition: AMDGPU.h:277
#define MATH_SQRT2
static const TableEntry tbl_tan[]
static const TableEntry tbl_cos[]
Value * CreateFAdd(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1426
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1361
static bool HasNative(AMDGPULibFunc::EFuncId id)
Value * CreateFDiv(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1501
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
void replaceCall(Value *With)
void addAttributes(unsigned i, const AttrBuilder &Attrs)
adds the attributes to the list of attributes.
Definition: Function.cpp:411
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
FunctionPass * createAMDGPUSimplifyLibCallsPass(const TargetOptions &, const TargetMachine *)
FunctionPass * createAMDGPUUseNativeCallsPass()
aarch64 promote const
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
LLVM Value Representation.
Definition: Value.h:74
amdgpu Simplify well known AMD library calls
#define MATH_SQRT1_2
static const TableEntry tbl_erfc[]
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Definition: IRBuilder.h:225
BasicBlock::iterator GetInsertPoint() const
Definition: IRBuilder.h:127
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:333
std::string mangle() const
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
Type * getElementType() const
Definition: DerivedTypes.h:399
bool isExactlyValue(const APFloat &V) const
We don&#39;t rely on operator== working on double values, as it returns true for things that are clearly ...
Definition: Constants.cpp:906
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
unsigned getNumElements() const
Return the number of elements in the array or vector.
Definition: Constants.cpp:2524
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:156
static Constant * get(LLVMContext &Context, ArrayRef< uint8_t > Elts)
get() constructors - Return a constant with vector type with an element count and element type matchi...
Definition: Constants.cpp:2663
#define LLVM_DEBUG(X)
Definition: Debug.h:122
bool isDoubleTy() const
Return true if this is &#39;double&#39;, a 64-bit IEEE fp type.
Definition: Type.h:150
static const TableEntry tbl_tgamma[]
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
bool useNative(CallInst *CI)
static const TableEntry tbl_acos[]
const BasicBlock * getParent() const
Definition: Instruction.h:66
an instruction to allocate memory on the stack
Definition: Instructions.h:59
#define MATH_E