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