xref: /openbsd-src/gnu/llvm/llvm/lib/CodeGen/IntrinsicLowering.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file implements the IntrinsicLowering class.
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick 
1309467b48Spatrick #include "llvm/CodeGen/IntrinsicLowering.h"
1409467b48Spatrick #include "llvm/ADT/SmallVector.h"
1509467b48Spatrick #include "llvm/IR/Constants.h"
1609467b48Spatrick #include "llvm/IR/DataLayout.h"
1709467b48Spatrick #include "llvm/IR/DerivedTypes.h"
1809467b48Spatrick #include "llvm/IR/IRBuilder.h"
1909467b48Spatrick #include "llvm/IR/Module.h"
2009467b48Spatrick #include "llvm/IR/Type.h"
2109467b48Spatrick #include "llvm/Support/ErrorHandling.h"
2209467b48Spatrick #include "llvm/Support/raw_ostream.h"
2309467b48Spatrick using namespace llvm;
2409467b48Spatrick 
2509467b48Spatrick /// This function is used when we want to lower an intrinsic call to a call of
2609467b48Spatrick /// an external function. This handles hard cases such as when there was already
2709467b48Spatrick /// a prototype for the external function, but that prototype doesn't match the
2809467b48Spatrick /// arguments we expect to pass in.
2909467b48Spatrick template <class ArgIt>
ReplaceCallWith(const char * NewFn,CallInst * CI,ArgIt ArgBegin,ArgIt ArgEnd,Type * RetTy)3009467b48Spatrick static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
3109467b48Spatrick                                  ArgIt ArgBegin, ArgIt ArgEnd,
3209467b48Spatrick                                  Type *RetTy) {
3309467b48Spatrick   // If we haven't already looked up this function, check to see if the
3409467b48Spatrick   // program already contains a function with this name.
3509467b48Spatrick   Module *M = CI->getModule();
3609467b48Spatrick   // Get or insert the definition now.
3709467b48Spatrick   std::vector<Type *> ParamTys;
3809467b48Spatrick   for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
3909467b48Spatrick     ParamTys.push_back((*I)->getType());
4009467b48Spatrick   FunctionCallee FCache =
4109467b48Spatrick       M->getOrInsertFunction(NewFn, FunctionType::get(RetTy, ParamTys, false));
4209467b48Spatrick 
4309467b48Spatrick   IRBuilder<> Builder(CI->getParent(), CI->getIterator());
4409467b48Spatrick   SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
4509467b48Spatrick   CallInst *NewCI = Builder.CreateCall(FCache, Args);
4609467b48Spatrick   NewCI->setName(CI->getName());
4709467b48Spatrick   if (!CI->use_empty())
4809467b48Spatrick     CI->replaceAllUsesWith(NewCI);
4909467b48Spatrick   return NewCI;
5009467b48Spatrick }
5109467b48Spatrick 
5209467b48Spatrick /// Emit the code to lower bswap of V before the specified instruction IP.
LowerBSWAP(LLVMContext & Context,Value * V,Instruction * IP)5309467b48Spatrick static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
5409467b48Spatrick   assert(V->getType()->isIntOrIntVectorTy() && "Can't bswap a non-integer type!");
5509467b48Spatrick 
5609467b48Spatrick   unsigned BitSize = V->getType()->getScalarSizeInBits();
5709467b48Spatrick 
5809467b48Spatrick   IRBuilder<> Builder(IP);
5909467b48Spatrick 
6009467b48Spatrick   switch(BitSize) {
6109467b48Spatrick   default: llvm_unreachable("Unhandled type size of value to byteswap!");
6209467b48Spatrick   case 16: {
6309467b48Spatrick     Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
6409467b48Spatrick                                     "bswap.2");
6509467b48Spatrick     Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
6609467b48Spatrick                                      "bswap.1");
6709467b48Spatrick     V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
6809467b48Spatrick     break;
6909467b48Spatrick   }
7009467b48Spatrick   case 32: {
7109467b48Spatrick     Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
7209467b48Spatrick                                     "bswap.4");
7309467b48Spatrick     Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
7409467b48Spatrick                                     "bswap.3");
7509467b48Spatrick     Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
7609467b48Spatrick                                      "bswap.2");
7709467b48Spatrick     Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
7809467b48Spatrick                                      "bswap.1");
7909467b48Spatrick     Tmp3 = Builder.CreateAnd(Tmp3,
8009467b48Spatrick                          ConstantInt::get(V->getType(), 0xFF0000),
8109467b48Spatrick                              "bswap.and3");
8209467b48Spatrick     Tmp2 = Builder.CreateAnd(Tmp2,
8309467b48Spatrick                            ConstantInt::get(V->getType(), 0xFF00),
8409467b48Spatrick                              "bswap.and2");
8509467b48Spatrick     Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
8609467b48Spatrick     Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
8709467b48Spatrick     V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
8809467b48Spatrick     break;
8909467b48Spatrick   }
9009467b48Spatrick   case 64: {
9109467b48Spatrick     Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
9209467b48Spatrick                                     "bswap.8");
9309467b48Spatrick     Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
9409467b48Spatrick                                     "bswap.7");
9509467b48Spatrick     Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
9609467b48Spatrick                                     "bswap.6");
9709467b48Spatrick     Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
9809467b48Spatrick                                     "bswap.5");
9909467b48Spatrick     Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
10009467b48Spatrick                                      "bswap.4");
10109467b48Spatrick     Value* Tmp3 = Builder.CreateLShr(V,
10209467b48Spatrick                                      ConstantInt::get(V->getType(), 24),
10309467b48Spatrick                                      "bswap.3");
10409467b48Spatrick     Value* Tmp2 = Builder.CreateLShr(V,
10509467b48Spatrick                                      ConstantInt::get(V->getType(), 40),
10609467b48Spatrick                                      "bswap.2");
10709467b48Spatrick     Value* Tmp1 = Builder.CreateLShr(V,
10809467b48Spatrick                                      ConstantInt::get(V->getType(), 56),
10909467b48Spatrick                                      "bswap.1");
11009467b48Spatrick     Tmp7 = Builder.CreateAnd(Tmp7,
11109467b48Spatrick                              ConstantInt::get(V->getType(),
11209467b48Spatrick                                               0xFF000000000000ULL),
11309467b48Spatrick                              "bswap.and7");
11409467b48Spatrick     Tmp6 = Builder.CreateAnd(Tmp6,
11509467b48Spatrick                              ConstantInt::get(V->getType(),
11609467b48Spatrick                                               0xFF0000000000ULL),
11709467b48Spatrick                              "bswap.and6");
11809467b48Spatrick     Tmp5 = Builder.CreateAnd(Tmp5,
11909467b48Spatrick                         ConstantInt::get(V->getType(),
12009467b48Spatrick                              0xFF00000000ULL),
12109467b48Spatrick                              "bswap.and5");
12209467b48Spatrick     Tmp4 = Builder.CreateAnd(Tmp4,
12309467b48Spatrick                         ConstantInt::get(V->getType(),
12409467b48Spatrick                              0xFF000000ULL),
12509467b48Spatrick                              "bswap.and4");
12609467b48Spatrick     Tmp3 = Builder.CreateAnd(Tmp3,
12709467b48Spatrick                              ConstantInt::get(V->getType(),
12809467b48Spatrick                              0xFF0000ULL),
12909467b48Spatrick                              "bswap.and3");
13009467b48Spatrick     Tmp2 = Builder.CreateAnd(Tmp2,
13109467b48Spatrick                              ConstantInt::get(V->getType(),
13209467b48Spatrick                              0xFF00ULL),
13309467b48Spatrick                              "bswap.and2");
13409467b48Spatrick     Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
13509467b48Spatrick     Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
13609467b48Spatrick     Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
13709467b48Spatrick     Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
13809467b48Spatrick     Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
13909467b48Spatrick     Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
14009467b48Spatrick     V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
14109467b48Spatrick     break;
14209467b48Spatrick   }
14309467b48Spatrick   }
14409467b48Spatrick   return V;
14509467b48Spatrick }
14609467b48Spatrick 
14709467b48Spatrick /// Emit the code to lower ctpop of V before the specified instruction IP.
LowerCTPOP(LLVMContext & Context,Value * V,Instruction * IP)14809467b48Spatrick static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
14909467b48Spatrick   assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
15009467b48Spatrick 
15109467b48Spatrick   static const uint64_t MaskValues[6] = {
15209467b48Spatrick     0x5555555555555555ULL, 0x3333333333333333ULL,
15309467b48Spatrick     0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
15409467b48Spatrick     0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
15509467b48Spatrick   };
15609467b48Spatrick 
15709467b48Spatrick   IRBuilder<> Builder(IP);
15809467b48Spatrick 
15909467b48Spatrick   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
16009467b48Spatrick   unsigned WordSize = (BitSize + 63) / 64;
16109467b48Spatrick   Value *Count = ConstantInt::get(V->getType(), 0);
16209467b48Spatrick 
16309467b48Spatrick   for (unsigned n = 0; n < WordSize; ++n) {
16409467b48Spatrick     Value *PartValue = V;
16509467b48Spatrick     for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
16609467b48Spatrick          i <<= 1, ++ct) {
16709467b48Spatrick       Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
16809467b48Spatrick       Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
16909467b48Spatrick       Value *VShift = Builder.CreateLShr(PartValue,
17009467b48Spatrick                                         ConstantInt::get(V->getType(), i),
17109467b48Spatrick                                          "ctpop.sh");
17209467b48Spatrick       Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
17309467b48Spatrick       PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
17409467b48Spatrick     }
17509467b48Spatrick     Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
17609467b48Spatrick     if (BitSize > 64) {
17709467b48Spatrick       V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
17809467b48Spatrick                              "ctpop.part.sh");
17909467b48Spatrick       BitSize -= 64;
18009467b48Spatrick     }
18109467b48Spatrick   }
18209467b48Spatrick 
18309467b48Spatrick   return Count;
18409467b48Spatrick }
18509467b48Spatrick 
18609467b48Spatrick /// Emit the code to lower ctlz of V before the specified instruction IP.
LowerCTLZ(LLVMContext & Context,Value * V,Instruction * IP)18709467b48Spatrick static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
18809467b48Spatrick 
18909467b48Spatrick   IRBuilder<> Builder(IP);
19009467b48Spatrick 
19109467b48Spatrick   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
19209467b48Spatrick   for (unsigned i = 1; i < BitSize; i <<= 1) {
19309467b48Spatrick     Value *ShVal = ConstantInt::get(V->getType(), i);
19409467b48Spatrick     ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
19509467b48Spatrick     V = Builder.CreateOr(V, ShVal, "ctlz.step");
19609467b48Spatrick   }
19709467b48Spatrick 
19809467b48Spatrick   V = Builder.CreateNot(V);
19909467b48Spatrick   return LowerCTPOP(Context, V, IP);
20009467b48Spatrick }
20109467b48Spatrick 
ReplaceFPIntrinsicWithCall(CallInst * CI,const char * Fname,const char * Dname,const char * LDname)20209467b48Spatrick static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
20309467b48Spatrick                                        const char *Dname,
20409467b48Spatrick                                        const char *LDname) {
20509467b48Spatrick   switch (CI->getArgOperand(0)->getType()->getTypeID()) {
20609467b48Spatrick   default: llvm_unreachable("Invalid type in intrinsic");
20709467b48Spatrick   case Type::FloatTyID:
208097a140dSpatrick     ReplaceCallWith(Fname, CI, CI->arg_begin(), CI->arg_end(),
20909467b48Spatrick                     Type::getFloatTy(CI->getContext()));
21009467b48Spatrick     break;
21109467b48Spatrick   case Type::DoubleTyID:
212097a140dSpatrick     ReplaceCallWith(Dname, CI, CI->arg_begin(), CI->arg_end(),
21309467b48Spatrick                     Type::getDoubleTy(CI->getContext()));
21409467b48Spatrick     break;
21509467b48Spatrick   case Type::X86_FP80TyID:
21609467b48Spatrick   case Type::FP128TyID:
21709467b48Spatrick   case Type::PPC_FP128TyID:
218097a140dSpatrick     ReplaceCallWith(LDname, CI, CI->arg_begin(), CI->arg_end(),
21909467b48Spatrick                     CI->getArgOperand(0)->getType());
22009467b48Spatrick     break;
22109467b48Spatrick   }
22209467b48Spatrick }
22309467b48Spatrick 
LowerIntrinsicCall(CallInst * CI)22409467b48Spatrick void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
22509467b48Spatrick   IRBuilder<> Builder(CI);
22609467b48Spatrick   LLVMContext &Context = CI->getContext();
22709467b48Spatrick 
22809467b48Spatrick   const Function *Callee = CI->getCalledFunction();
22909467b48Spatrick   assert(Callee && "Cannot lower an indirect call!");
23009467b48Spatrick 
23109467b48Spatrick   switch (Callee->getIntrinsicID()) {
23209467b48Spatrick   case Intrinsic::not_intrinsic:
23309467b48Spatrick     report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
23409467b48Spatrick                       Callee->getName() + "'!");
23509467b48Spatrick   default:
23609467b48Spatrick     report_fatal_error("Code generator does not support intrinsic function '"+
23709467b48Spatrick                       Callee->getName()+"'!");
23809467b48Spatrick 
23909467b48Spatrick   case Intrinsic::expect: {
24009467b48Spatrick     // Just replace __builtin_expect(exp, c) with EXP.
24109467b48Spatrick     Value *V = CI->getArgOperand(0);
24209467b48Spatrick     CI->replaceAllUsesWith(V);
24309467b48Spatrick     break;
24409467b48Spatrick   }
24509467b48Spatrick 
24609467b48Spatrick   case Intrinsic::ctpop:
24709467b48Spatrick     CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI));
24809467b48Spatrick     break;
24909467b48Spatrick 
25009467b48Spatrick   case Intrinsic::bswap:
25109467b48Spatrick     CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getArgOperand(0), CI));
25209467b48Spatrick     break;
25309467b48Spatrick 
25409467b48Spatrick   case Intrinsic::ctlz:
25509467b48Spatrick     CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getArgOperand(0), CI));
25609467b48Spatrick     break;
25709467b48Spatrick 
25809467b48Spatrick   case Intrinsic::cttz: {
25909467b48Spatrick     // cttz(x) -> ctpop(~X & (X-1))
26009467b48Spatrick     Value *Src = CI->getArgOperand(0);
26109467b48Spatrick     Value *NotSrc = Builder.CreateNot(Src);
26209467b48Spatrick     NotSrc->setName(Src->getName() + ".not");
26309467b48Spatrick     Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
26409467b48Spatrick     SrcM1 = Builder.CreateSub(Src, SrcM1);
26509467b48Spatrick     Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
26609467b48Spatrick     CI->replaceAllUsesWith(Src);
26709467b48Spatrick     break;
26809467b48Spatrick   }
26909467b48Spatrick 
27009467b48Spatrick   case Intrinsic::stacksave:
27109467b48Spatrick   case Intrinsic::stackrestore: {
27209467b48Spatrick     if (!Warned)
27309467b48Spatrick       errs() << "WARNING: this target does not support the llvm.stack"
27409467b48Spatrick              << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
27509467b48Spatrick                "save" : "restore") << " intrinsic.\n";
27609467b48Spatrick     Warned = true;
27709467b48Spatrick     if (Callee->getIntrinsicID() == Intrinsic::stacksave)
27809467b48Spatrick       CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
27909467b48Spatrick     break;
28009467b48Spatrick   }
28109467b48Spatrick 
28209467b48Spatrick   case Intrinsic::get_dynamic_area_offset:
28309467b48Spatrick     errs() << "WARNING: this target does not support the custom llvm.get."
28409467b48Spatrick               "dynamic.area.offset.  It is being lowered to a constant 0\n";
28509467b48Spatrick     // Just lower it to a constant 0 because for most targets
28609467b48Spatrick     // @llvm.get.dynamic.area.offset is lowered to zero.
28709467b48Spatrick     CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0));
28809467b48Spatrick     break;
28909467b48Spatrick   case Intrinsic::returnaddress:
29009467b48Spatrick   case Intrinsic::frameaddress:
29109467b48Spatrick     errs() << "WARNING: this target does not support the llvm."
29209467b48Spatrick            << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
29309467b48Spatrick              "return" : "frame") << "address intrinsic.\n";
29409467b48Spatrick     CI->replaceAllUsesWith(
29509467b48Spatrick         ConstantPointerNull::get(cast<PointerType>(CI->getType())));
29609467b48Spatrick     break;
29709467b48Spatrick   case Intrinsic::addressofreturnaddress:
29809467b48Spatrick     errs() << "WARNING: this target does not support the "
29909467b48Spatrick               "llvm.addressofreturnaddress intrinsic.\n";
30009467b48Spatrick     CI->replaceAllUsesWith(
30109467b48Spatrick         ConstantPointerNull::get(cast<PointerType>(CI->getType())));
30209467b48Spatrick     break;
30309467b48Spatrick 
30409467b48Spatrick   case Intrinsic::prefetch:
30509467b48Spatrick     break;    // Simply strip out prefetches on unsupported architectures
30609467b48Spatrick 
30709467b48Spatrick   case Intrinsic::pcmarker:
30809467b48Spatrick     break;    // Simply strip out pcmarker on unsupported architectures
30909467b48Spatrick   case Intrinsic::readcyclecounter: {
31009467b48Spatrick     errs() << "WARNING: this target does not support the llvm.readcyclecoun"
31109467b48Spatrick            << "ter intrinsic.  It is being lowered to a constant 0\n";
31209467b48Spatrick     CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
31309467b48Spatrick     break;
31409467b48Spatrick   }
31509467b48Spatrick 
31609467b48Spatrick   case Intrinsic::dbg_declare:
31709467b48Spatrick   case Intrinsic::dbg_label:
31809467b48Spatrick     break;    // Simply strip out debugging intrinsics
31909467b48Spatrick 
32009467b48Spatrick   case Intrinsic::eh_typeid_for:
32109467b48Spatrick     // Return something different to eh_selector.
32209467b48Spatrick     CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
32309467b48Spatrick     break;
32409467b48Spatrick 
32509467b48Spatrick   case Intrinsic::annotation:
32609467b48Spatrick   case Intrinsic::ptr_annotation:
32709467b48Spatrick     // Just drop the annotation, but forward the value
32809467b48Spatrick     CI->replaceAllUsesWith(CI->getOperand(0));
32909467b48Spatrick     break;
33009467b48Spatrick 
33109467b48Spatrick   case Intrinsic::assume:
33273471bf0Spatrick   case Intrinsic::experimental_noalias_scope_decl:
33309467b48Spatrick   case Intrinsic::var_annotation:
33409467b48Spatrick     break;   // Strip out these intrinsics
33509467b48Spatrick 
33609467b48Spatrick   case Intrinsic::memcpy: {
33709467b48Spatrick     Type *IntPtr = DL.getIntPtrType(Context);
33809467b48Spatrick     Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
33909467b48Spatrick                                         /* isSigned */ false);
34009467b48Spatrick     Value *Ops[3];
34109467b48Spatrick     Ops[0] = CI->getArgOperand(0);
34209467b48Spatrick     Ops[1] = CI->getArgOperand(1);
34309467b48Spatrick     Ops[2] = Size;
34409467b48Spatrick     ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
34509467b48Spatrick     break;
34609467b48Spatrick   }
34709467b48Spatrick   case Intrinsic::memmove: {
34809467b48Spatrick     Type *IntPtr = DL.getIntPtrType(Context);
34909467b48Spatrick     Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
35009467b48Spatrick                                         /* isSigned */ false);
35109467b48Spatrick     Value *Ops[3];
35209467b48Spatrick     Ops[0] = CI->getArgOperand(0);
35309467b48Spatrick     Ops[1] = CI->getArgOperand(1);
35409467b48Spatrick     Ops[2] = Size;
35509467b48Spatrick     ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
35609467b48Spatrick     break;
35709467b48Spatrick   }
35809467b48Spatrick   case Intrinsic::memset: {
35909467b48Spatrick     Value *Op0 = CI->getArgOperand(0);
36009467b48Spatrick     Type *IntPtr = DL.getIntPtrType(Op0->getType());
36109467b48Spatrick     Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
36209467b48Spatrick                                         /* isSigned */ false);
36309467b48Spatrick     Value *Ops[3];
36409467b48Spatrick     Ops[0] = Op0;
36509467b48Spatrick     // Extend the amount to i32.
36609467b48Spatrick     Ops[1] = Builder.CreateIntCast(CI->getArgOperand(1),
36709467b48Spatrick                                    Type::getInt32Ty(Context),
36809467b48Spatrick                                    /* isSigned */ false);
36909467b48Spatrick     Ops[2] = Size;
37009467b48Spatrick     ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
37109467b48Spatrick     break;
37209467b48Spatrick   }
37309467b48Spatrick   case Intrinsic::sqrt: {
37409467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
37509467b48Spatrick     break;
37609467b48Spatrick   }
37709467b48Spatrick   case Intrinsic::log: {
37809467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
37909467b48Spatrick     break;
38009467b48Spatrick   }
38109467b48Spatrick   case Intrinsic::log2: {
38209467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
38309467b48Spatrick     break;
38409467b48Spatrick   }
38509467b48Spatrick   case Intrinsic::log10: {
38609467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
38709467b48Spatrick     break;
38809467b48Spatrick   }
38909467b48Spatrick   case Intrinsic::exp: {
39009467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
39109467b48Spatrick     break;
39209467b48Spatrick   }
39309467b48Spatrick   case Intrinsic::exp2: {
39409467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
39509467b48Spatrick     break;
39609467b48Spatrick   }
39709467b48Spatrick   case Intrinsic::pow: {
39809467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
39909467b48Spatrick     break;
40009467b48Spatrick   }
40109467b48Spatrick   case Intrinsic::sin: {
40209467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "sinf", "sin", "sinl");
40309467b48Spatrick     break;
40409467b48Spatrick   }
40509467b48Spatrick   case Intrinsic::cos: {
40609467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "cosf", "cos", "cosl");
40709467b48Spatrick     break;
40809467b48Spatrick   }
40909467b48Spatrick   case Intrinsic::floor: {
41009467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "floorf", "floor", "floorl");
41109467b48Spatrick     break;
41209467b48Spatrick   }
41309467b48Spatrick   case Intrinsic::ceil: {
41409467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "ceilf", "ceil", "ceill");
41509467b48Spatrick     break;
41609467b48Spatrick   }
41709467b48Spatrick   case Intrinsic::trunc: {
41809467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "truncf", "trunc", "truncl");
41909467b48Spatrick     break;
42009467b48Spatrick   }
42109467b48Spatrick   case Intrinsic::round: {
42209467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "roundf", "round", "roundl");
42309467b48Spatrick     break;
42409467b48Spatrick   }
425097a140dSpatrick   case Intrinsic::roundeven: {
426097a140dSpatrick     ReplaceFPIntrinsicWithCall(CI, "roundevenf", "roundeven", "roundevenl");
427097a140dSpatrick     break;
428097a140dSpatrick   }
42909467b48Spatrick   case Intrinsic::copysign: {
43009467b48Spatrick     ReplaceFPIntrinsicWithCall(CI, "copysignf", "copysign", "copysignl");
43109467b48Spatrick     break;
43209467b48Spatrick   }
433*d415bd75Srobert   case Intrinsic::get_rounding:
43409467b48Spatrick      // Lower to "round to the nearest"
43509467b48Spatrick      if (!CI->getType()->isVoidTy())
43609467b48Spatrick        CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
43709467b48Spatrick      break;
43809467b48Spatrick   case Intrinsic::invariant_start:
43909467b48Spatrick   case Intrinsic::lifetime_start:
44009467b48Spatrick     // Discard region information.
44109467b48Spatrick     CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
44209467b48Spatrick     break;
44309467b48Spatrick   case Intrinsic::invariant_end:
44409467b48Spatrick   case Intrinsic::lifetime_end:
44509467b48Spatrick     // Discard region information.
44609467b48Spatrick     break;
44709467b48Spatrick   }
44809467b48Spatrick 
44909467b48Spatrick   assert(CI->use_empty() &&
45009467b48Spatrick          "Lowering should have eliminated any uses of the intrinsic call!");
45109467b48Spatrick   CI->eraseFromParent();
45209467b48Spatrick }
45309467b48Spatrick 
LowerToByteSwap(CallInst * CI)45409467b48Spatrick bool IntrinsicLowering::LowerToByteSwap(CallInst *CI) {
45509467b48Spatrick   // Verify this is a simple bswap.
456*d415bd75Srobert   if (CI->arg_size() != 1 || CI->getType() != CI->getArgOperand(0)->getType() ||
45709467b48Spatrick       !CI->getType()->isIntegerTy())
45809467b48Spatrick     return false;
45909467b48Spatrick 
46009467b48Spatrick   IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
46109467b48Spatrick   if (!Ty)
46209467b48Spatrick     return false;
46309467b48Spatrick 
46409467b48Spatrick   // Okay, we can do this xform, do so now.
46509467b48Spatrick   Module *M = CI->getModule();
46609467b48Spatrick   Function *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Ty);
46709467b48Spatrick 
46809467b48Spatrick   Value *Op = CI->getArgOperand(0);
46909467b48Spatrick   Op = CallInst::Create(Int, Op, CI->getName(), CI);
47009467b48Spatrick 
47109467b48Spatrick   CI->replaceAllUsesWith(Op);
47209467b48Spatrick   CI->eraseFromParent();
47309467b48Spatrick   return true;
47409467b48Spatrick }
475