1 //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the IntrinsicLowering class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/IntrinsicLowering.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/IR/CallSite.h" 16 #include "llvm/IR/Constants.h" 17 #include "llvm/IR/DataLayout.h" 18 #include "llvm/IR/DerivedTypes.h" 19 #include "llvm/IR/IRBuilder.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/IR/Type.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/raw_ostream.h" 24 using namespace llvm; 25 26 template <class ArgIt> 27 static void EnsureFunctionExists(Module &M, const char *Name, 28 ArgIt ArgBegin, ArgIt ArgEnd, 29 Type *RetTy) { 30 // Insert a correctly-typed definition now. 31 std::vector<Type *> ParamTys; 32 for (ArgIt I = ArgBegin; I != ArgEnd; ++I) 33 ParamTys.push_back(I->getType()); 34 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false)); 35 } 36 37 static void EnsureFPIntrinsicsExist(Module &M, Function &Fn, 38 const char *FName, 39 const char *DName, const char *LDName) { 40 // Insert definitions for all the floating point types. 41 switch((int)Fn.arg_begin()->getType()->getTypeID()) { 42 case Type::FloatTyID: 43 EnsureFunctionExists(M, FName, Fn.arg_begin(), Fn.arg_end(), 44 Type::getFloatTy(M.getContext())); 45 break; 46 case Type::DoubleTyID: 47 EnsureFunctionExists(M, DName, Fn.arg_begin(), Fn.arg_end(), 48 Type::getDoubleTy(M.getContext())); 49 break; 50 case Type::X86_FP80TyID: 51 case Type::FP128TyID: 52 case Type::PPC_FP128TyID: 53 EnsureFunctionExists(M, LDName, Fn.arg_begin(), Fn.arg_end(), 54 Fn.arg_begin()->getType()); 55 break; 56 } 57 } 58 59 /// This function is used when we want to lower an intrinsic call to a call of 60 /// an external function. This handles hard cases such as when there was already 61 /// a prototype for the external function, but that prototype doesn't match the 62 /// arguments we expect to pass in. 63 template <class ArgIt> 64 static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, 65 ArgIt ArgBegin, ArgIt ArgEnd, 66 Type *RetTy) { 67 // If we haven't already looked up this function, check to see if the 68 // program already contains a function with this name. 69 Module *M = CI->getModule(); 70 // Get or insert the definition now. 71 std::vector<Type *> ParamTys; 72 for (ArgIt I = ArgBegin; I != ArgEnd; ++I) 73 ParamTys.push_back((*I)->getType()); 74 Constant* FCache = M->getOrInsertFunction(NewFn, 75 FunctionType::get(RetTy, ParamTys, false)); 76 77 IRBuilder<> Builder(CI->getParent(), CI->getIterator()); 78 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd); 79 CallInst *NewCI = Builder.CreateCall(FCache, Args); 80 NewCI->setName(CI->getName()); 81 if (!CI->use_empty()) 82 CI->replaceAllUsesWith(NewCI); 83 return NewCI; 84 } 85 86 // VisualStudio defines setjmp as _setjmp 87 #if defined(_MSC_VER) && defined(setjmp) && \ 88 !defined(setjmp_undefined_for_msvc) 89 # pragma push_macro("setjmp") 90 # undef setjmp 91 # define setjmp_undefined_for_msvc 92 #endif 93 94 void IntrinsicLowering::AddPrototypes(Module &M) { 95 LLVMContext &Context = M.getContext(); 96 for (auto &F : M) 97 if (F.isDeclaration() && !F.use_empty()) 98 switch (F.getIntrinsicID()) { 99 default: break; 100 case Intrinsic::setjmp: 101 EnsureFunctionExists(M, "setjmp", F.arg_begin(), F.arg_end(), 102 Type::getInt32Ty(M.getContext())); 103 break; 104 case Intrinsic::longjmp: 105 EnsureFunctionExists(M, "longjmp", F.arg_begin(), F.arg_end(), 106 Type::getVoidTy(M.getContext())); 107 break; 108 case Intrinsic::siglongjmp: 109 EnsureFunctionExists(M, "abort", F.arg_end(), F.arg_end(), 110 Type::getVoidTy(M.getContext())); 111 break; 112 case Intrinsic::memcpy: 113 M.getOrInsertFunction("memcpy", 114 Type::getInt8PtrTy(Context), 115 Type::getInt8PtrTy(Context), 116 Type::getInt8PtrTy(Context), 117 DL.getIntPtrType(Context)); 118 break; 119 case Intrinsic::memmove: 120 M.getOrInsertFunction("memmove", 121 Type::getInt8PtrTy(Context), 122 Type::getInt8PtrTy(Context), 123 Type::getInt8PtrTy(Context), 124 DL.getIntPtrType(Context)); 125 break; 126 case Intrinsic::memset: 127 M.getOrInsertFunction("memset", 128 Type::getInt8PtrTy(Context), 129 Type::getInt8PtrTy(Context), 130 Type::getInt32Ty(M.getContext()), 131 DL.getIntPtrType(Context)); 132 break; 133 case Intrinsic::sqrt: 134 EnsureFPIntrinsicsExist(M, F, "sqrtf", "sqrt", "sqrtl"); 135 break; 136 case Intrinsic::sin: 137 EnsureFPIntrinsicsExist(M, F, "sinf", "sin", "sinl"); 138 break; 139 case Intrinsic::cos: 140 EnsureFPIntrinsicsExist(M, F, "cosf", "cos", "cosl"); 141 break; 142 case Intrinsic::pow: 143 EnsureFPIntrinsicsExist(M, F, "powf", "pow", "powl"); 144 break; 145 case Intrinsic::log: 146 EnsureFPIntrinsicsExist(M, F, "logf", "log", "logl"); 147 break; 148 case Intrinsic::log2: 149 EnsureFPIntrinsicsExist(M, F, "log2f", "log2", "log2l"); 150 break; 151 case Intrinsic::log10: 152 EnsureFPIntrinsicsExist(M, F, "log10f", "log10", "log10l"); 153 break; 154 case Intrinsic::exp: 155 EnsureFPIntrinsicsExist(M, F, "expf", "exp", "expl"); 156 break; 157 case Intrinsic::exp2: 158 EnsureFPIntrinsicsExist(M, F, "exp2f", "exp2", "exp2l"); 159 break; 160 } 161 } 162 163 /// Emit the code to lower bswap of V before the specified instruction IP. 164 static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) { 165 assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!"); 166 167 unsigned BitSize = V->getType()->getScalarSizeInBits(); 168 169 IRBuilder<> Builder(IP); 170 171 switch(BitSize) { 172 default: llvm_unreachable("Unhandled type size of value to byteswap!"); 173 case 16: { 174 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8), 175 "bswap.2"); 176 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8), 177 "bswap.1"); 178 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16"); 179 break; 180 } 181 case 32: { 182 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24), 183 "bswap.4"); 184 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8), 185 "bswap.3"); 186 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8), 187 "bswap.2"); 188 Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24), 189 "bswap.1"); 190 Tmp3 = Builder.CreateAnd(Tmp3, 191 ConstantInt::get(V->getType(), 0xFF0000), 192 "bswap.and3"); 193 Tmp2 = Builder.CreateAnd(Tmp2, 194 ConstantInt::get(V->getType(), 0xFF00), 195 "bswap.and2"); 196 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1"); 197 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2"); 198 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32"); 199 break; 200 } 201 case 64: { 202 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56), 203 "bswap.8"); 204 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40), 205 "bswap.7"); 206 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24), 207 "bswap.6"); 208 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8), 209 "bswap.5"); 210 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8), 211 "bswap.4"); 212 Value* Tmp3 = Builder.CreateLShr(V, 213 ConstantInt::get(V->getType(), 24), 214 "bswap.3"); 215 Value* Tmp2 = Builder.CreateLShr(V, 216 ConstantInt::get(V->getType(), 40), 217 "bswap.2"); 218 Value* Tmp1 = Builder.CreateLShr(V, 219 ConstantInt::get(V->getType(), 56), 220 "bswap.1"); 221 Tmp7 = Builder.CreateAnd(Tmp7, 222 ConstantInt::get(V->getType(), 223 0xFF000000000000ULL), 224 "bswap.and7"); 225 Tmp6 = Builder.CreateAnd(Tmp6, 226 ConstantInt::get(V->getType(), 227 0xFF0000000000ULL), 228 "bswap.and6"); 229 Tmp5 = Builder.CreateAnd(Tmp5, 230 ConstantInt::get(V->getType(), 231 0xFF00000000ULL), 232 "bswap.and5"); 233 Tmp4 = Builder.CreateAnd(Tmp4, 234 ConstantInt::get(V->getType(), 235 0xFF000000ULL), 236 "bswap.and4"); 237 Tmp3 = Builder.CreateAnd(Tmp3, 238 ConstantInt::get(V->getType(), 239 0xFF0000ULL), 240 "bswap.and3"); 241 Tmp2 = Builder.CreateAnd(Tmp2, 242 ConstantInt::get(V->getType(), 243 0xFF00ULL), 244 "bswap.and2"); 245 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1"); 246 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2"); 247 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3"); 248 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4"); 249 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5"); 250 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6"); 251 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64"); 252 break; 253 } 254 } 255 return V; 256 } 257 258 /// Emit the code to lower ctpop of V before the specified instruction IP. 259 static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) { 260 assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!"); 261 262 static const uint64_t MaskValues[6] = { 263 0x5555555555555555ULL, 0x3333333333333333ULL, 264 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, 265 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL 266 }; 267 268 IRBuilder<> Builder(IP); 269 270 unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); 271 unsigned WordSize = (BitSize + 63) / 64; 272 Value *Count = ConstantInt::get(V->getType(), 0); 273 274 for (unsigned n = 0; n < WordSize; ++n) { 275 Value *PartValue = V; 276 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize); 277 i <<= 1, ++ct) { 278 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]); 279 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1"); 280 Value *VShift = Builder.CreateLShr(PartValue, 281 ConstantInt::get(V->getType(), i), 282 "ctpop.sh"); 283 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2"); 284 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step"); 285 } 286 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part"); 287 if (BitSize > 64) { 288 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64), 289 "ctpop.part.sh"); 290 BitSize -= 64; 291 } 292 } 293 294 return Count; 295 } 296 297 /// Emit the code to lower ctlz of V before the specified instruction IP. 298 static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) { 299 300 IRBuilder<> Builder(IP); 301 302 unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); 303 for (unsigned i = 1; i < BitSize; i <<= 1) { 304 Value *ShVal = ConstantInt::get(V->getType(), i); 305 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh"); 306 V = Builder.CreateOr(V, ShVal, "ctlz.step"); 307 } 308 309 V = Builder.CreateNot(V); 310 return LowerCTPOP(Context, V, IP); 311 } 312 313 static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname, 314 const char *Dname, 315 const char *LDname) { 316 CallSite CS(CI); 317 switch (CI->getArgOperand(0)->getType()->getTypeID()) { 318 default: llvm_unreachable("Invalid type in intrinsic"); 319 case Type::FloatTyID: 320 ReplaceCallWith(Fname, CI, CS.arg_begin(), CS.arg_end(), 321 Type::getFloatTy(CI->getContext())); 322 break; 323 case Type::DoubleTyID: 324 ReplaceCallWith(Dname, CI, CS.arg_begin(), CS.arg_end(), 325 Type::getDoubleTy(CI->getContext())); 326 break; 327 case Type::X86_FP80TyID: 328 case Type::FP128TyID: 329 case Type::PPC_FP128TyID: 330 ReplaceCallWith(LDname, CI, CS.arg_begin(), CS.arg_end(), 331 CI->getArgOperand(0)->getType()); 332 break; 333 } 334 } 335 336 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { 337 IRBuilder<> Builder(CI); 338 LLVMContext &Context = CI->getContext(); 339 340 const Function *Callee = CI->getCalledFunction(); 341 assert(Callee && "Cannot lower an indirect call!"); 342 343 CallSite CS(CI); 344 switch (Callee->getIntrinsicID()) { 345 case Intrinsic::not_intrinsic: 346 report_fatal_error("Cannot lower a call to a non-intrinsic function '"+ 347 Callee->getName() + "'!"); 348 default: 349 report_fatal_error("Code generator does not support intrinsic function '"+ 350 Callee->getName()+"'!"); 351 352 case Intrinsic::expect: { 353 // Just replace __builtin_expect(exp, c) with EXP. 354 Value *V = CI->getArgOperand(0); 355 CI->replaceAllUsesWith(V); 356 break; 357 } 358 359 // The setjmp/longjmp intrinsics should only exist in the code if it was 360 // never optimized (ie, right out of the CFE), or if it has been hacked on 361 // by the lowerinvoke pass. In both cases, the right thing to do is to 362 // convert the call to an explicit setjmp or longjmp call. 363 case Intrinsic::setjmp: { 364 Value *V = ReplaceCallWith("setjmp", CI, CS.arg_begin(), CS.arg_end(), 365 Type::getInt32Ty(Context)); 366 if (!CI->getType()->isVoidTy()) 367 CI->replaceAllUsesWith(V); 368 break; 369 } 370 case Intrinsic::sigsetjmp: 371 if (!CI->getType()->isVoidTy()) 372 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); 373 break; 374 375 case Intrinsic::longjmp: { 376 ReplaceCallWith("longjmp", CI, CS.arg_begin(), CS.arg_end(), 377 Type::getVoidTy(Context)); 378 break; 379 } 380 381 case Intrinsic::siglongjmp: { 382 // Insert the call to abort 383 ReplaceCallWith("abort", CI, CS.arg_end(), CS.arg_end(), 384 Type::getVoidTy(Context)); 385 break; 386 } 387 case Intrinsic::ctpop: 388 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI)); 389 break; 390 391 case Intrinsic::bswap: 392 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getArgOperand(0), CI)); 393 break; 394 395 case Intrinsic::ctlz: 396 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getArgOperand(0), CI)); 397 break; 398 399 case Intrinsic::cttz: { 400 // cttz(x) -> ctpop(~X & (X-1)) 401 Value *Src = CI->getArgOperand(0); 402 Value *NotSrc = Builder.CreateNot(Src); 403 NotSrc->setName(Src->getName() + ".not"); 404 Value *SrcM1 = ConstantInt::get(Src->getType(), 1); 405 SrcM1 = Builder.CreateSub(Src, SrcM1); 406 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI); 407 CI->replaceAllUsesWith(Src); 408 break; 409 } 410 411 case Intrinsic::stacksave: 412 case Intrinsic::stackrestore: { 413 if (!Warned) 414 errs() << "WARNING: this target does not support the llvm.stack" 415 << (Callee->getIntrinsicID() == Intrinsic::stacksave ? 416 "save" : "restore") << " intrinsic.\n"; 417 Warned = true; 418 if (Callee->getIntrinsicID() == Intrinsic::stacksave) 419 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); 420 break; 421 } 422 423 case Intrinsic::get_dynamic_area_offset: 424 errs() << "WARNING: this target does not support the custom llvm.get." 425 "dynamic.area.offset. It is being lowered to a constant 0\n"; 426 // Just lower it to a constant 0 because for most targets 427 // @llvm.get.dynamic.area.offset is lowered to zero. 428 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0)); 429 break; 430 case Intrinsic::returnaddress: 431 case Intrinsic::frameaddress: 432 errs() << "WARNING: this target does not support the llvm." 433 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? 434 "return" : "frame") << "address intrinsic.\n"; 435 CI->replaceAllUsesWith( 436 ConstantPointerNull::get(cast<PointerType>(CI->getType()))); 437 break; 438 case Intrinsic::addressofreturnaddress: 439 errs() << "WARNING: this target does not support the " 440 "llvm.addressofreturnaddress intrinsic.\n"; 441 CI->replaceAllUsesWith( 442 ConstantPointerNull::get(cast<PointerType>(CI->getType()))); 443 break; 444 445 case Intrinsic::prefetch: 446 break; // Simply strip out prefetches on unsupported architectures 447 448 case Intrinsic::pcmarker: 449 break; // Simply strip out pcmarker on unsupported architectures 450 case Intrinsic::readcyclecounter: { 451 errs() << "WARNING: this target does not support the llvm.readcyclecoun" 452 << "ter intrinsic. It is being lowered to a constant 0\n"; 453 CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0)); 454 break; 455 } 456 457 case Intrinsic::dbg_declare: 458 case Intrinsic::dbg_label: 459 break; // Simply strip out debugging intrinsics 460 461 case Intrinsic::eh_typeid_for: 462 // Return something different to eh_selector. 463 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1)); 464 break; 465 466 case Intrinsic::annotation: 467 case Intrinsic::ptr_annotation: 468 // Just drop the annotation, but forward the value 469 CI->replaceAllUsesWith(CI->getOperand(0)); 470 break; 471 472 case Intrinsic::assume: 473 case Intrinsic::var_annotation: 474 break; // Strip out these intrinsics 475 476 case Intrinsic::memcpy: { 477 Type *IntPtr = DL.getIntPtrType(Context); 478 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr, 479 /* isSigned */ false); 480 Value *Ops[3]; 481 Ops[0] = CI->getArgOperand(0); 482 Ops[1] = CI->getArgOperand(1); 483 Ops[2] = Size; 484 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getArgOperand(0)->getType()); 485 break; 486 } 487 case Intrinsic::memmove: { 488 Type *IntPtr = DL.getIntPtrType(Context); 489 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr, 490 /* isSigned */ false); 491 Value *Ops[3]; 492 Ops[0] = CI->getArgOperand(0); 493 Ops[1] = CI->getArgOperand(1); 494 Ops[2] = Size; 495 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getArgOperand(0)->getType()); 496 break; 497 } 498 case Intrinsic::memset: { 499 Value *Op0 = CI->getArgOperand(0); 500 Type *IntPtr = DL.getIntPtrType(Op0->getType()); 501 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr, 502 /* isSigned */ false); 503 Value *Ops[3]; 504 Ops[0] = Op0; 505 // Extend the amount to i32. 506 Ops[1] = Builder.CreateIntCast(CI->getArgOperand(1), 507 Type::getInt32Ty(Context), 508 /* isSigned */ false); 509 Ops[2] = Size; 510 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getArgOperand(0)->getType()); 511 break; 512 } 513 case Intrinsic::sqrt: { 514 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl"); 515 break; 516 } 517 case Intrinsic::log: { 518 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl"); 519 break; 520 } 521 case Intrinsic::log2: { 522 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l"); 523 break; 524 } 525 case Intrinsic::log10: { 526 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l"); 527 break; 528 } 529 case Intrinsic::exp: { 530 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl"); 531 break; 532 } 533 case Intrinsic::exp2: { 534 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l"); 535 break; 536 } 537 case Intrinsic::pow: { 538 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl"); 539 break; 540 } 541 case Intrinsic::sin: { 542 ReplaceFPIntrinsicWithCall(CI, "sinf", "sin", "sinl"); 543 break; 544 } 545 case Intrinsic::cos: { 546 ReplaceFPIntrinsicWithCall(CI, "cosf", "cos", "cosl"); 547 break; 548 } 549 case Intrinsic::floor: { 550 ReplaceFPIntrinsicWithCall(CI, "floorf", "floor", "floorl"); 551 break; 552 } 553 case Intrinsic::ceil: { 554 ReplaceFPIntrinsicWithCall(CI, "ceilf", "ceil", "ceill"); 555 break; 556 } 557 case Intrinsic::trunc: { 558 ReplaceFPIntrinsicWithCall(CI, "truncf", "trunc", "truncl"); 559 break; 560 } 561 case Intrinsic::round: { 562 ReplaceFPIntrinsicWithCall(CI, "roundf", "round", "roundl"); 563 break; 564 } 565 case Intrinsic::copysign: { 566 ReplaceFPIntrinsicWithCall(CI, "copysignf", "copysign", "copysignl"); 567 break; 568 } 569 case Intrinsic::flt_rounds: 570 // Lower to "round to the nearest" 571 if (!CI->getType()->isVoidTy()) 572 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1)); 573 break; 574 case Intrinsic::invariant_start: 575 case Intrinsic::lifetime_start: 576 // Discard region information. 577 CI->replaceAllUsesWith(UndefValue::get(CI->getType())); 578 break; 579 case Intrinsic::invariant_end: 580 case Intrinsic::lifetime_end: 581 // Discard region information. 582 break; 583 } 584 585 assert(CI->use_empty() && 586 "Lowering should have eliminated any uses of the intrinsic call!"); 587 CI->eraseFromParent(); 588 } 589 590 bool IntrinsicLowering::LowerToByteSwap(CallInst *CI) { 591 // Verify this is a simple bswap. 592 if (CI->getNumArgOperands() != 1 || 593 CI->getType() != CI->getArgOperand(0)->getType() || 594 !CI->getType()->isIntegerTy()) 595 return false; 596 597 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 598 if (!Ty) 599 return false; 600 601 // Okay, we can do this xform, do so now. 602 Module *M = CI->getModule(); 603 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Ty); 604 605 Value *Op = CI->getArgOperand(0); 606 Op = CallInst::Create(Int, Op, CI->getName(), CI); 607 608 CI->replaceAllUsesWith(Op); 609 CI->eraseFromParent(); 610 return true; 611 } 612