blob: 63bb5f21f8f158ac64f865ce9bc66269daf8f245 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the IntrinsicLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
16#include "llvm/Module.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Type.h"
18#include "llvm/CodeGen/IntrinsicLowering.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000019#include "llvm/Support/ErrorHandling.h"
Chris Lattner397f4562009-08-23 06:03:38 +000020#include "llvm/Support/IRBuilder.h"
21#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetData.h"
23#include "llvm/ADT/SmallVector.h"
24using namespace llvm;
25
26template <class ArgIt>
27static void EnsureFunctionExists(Module &M, const char *Name,
28 ArgIt ArgBegin, ArgIt ArgEnd,
29 const Type *RetTy) {
30 // Insert a correctly-typed definition now.
31 std::vector<const 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
Chris Lattnerba443852009-02-07 22:37:06 +000037static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
38 const char *FName,
39 const char *DName, const char *LDName) {
Dale Johannesen4f35283a2008-09-22 19:51:58 +000040 // Insert definitions for all the floating point types.
Chris Lattnerba443852009-02-07 22:37:06 +000041 switch((int)Fn->arg_begin()->getType()->getTypeID()) {
Dale Johannesen4f35283a2008-09-22 19:51:58 +000042 case Type::FloatTyID:
Chris Lattnerba443852009-02-07 22:37:06 +000043 EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
Owen Anderson35b47072009-08-13 21:58:54 +000044 Type::getFloatTy(M.getContext()));
Chris Lattnerba443852009-02-07 22:37:06 +000045 break;
Dale Johannesen4f35283a2008-09-22 19:51:58 +000046 case Type::DoubleTyID:
Chris Lattnerba443852009-02-07 22:37:06 +000047 EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
Owen Anderson35b47072009-08-13 21:58:54 +000048 Type::getDoubleTy(M.getContext()));
Chris Lattnerba443852009-02-07 22:37:06 +000049 break;
Dale Johannesen4f35283a2008-09-22 19:51:58 +000050 case Type::X86_FP80TyID:
51 case Type::FP128TyID:
52 case Type::PPC_FP128TyID:
Chris Lattnerba443852009-02-07 22:37:06 +000053 EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
54 Fn->arg_begin()->getType());
55 break;
Dale Johannesen4f35283a2008-09-22 19:51:58 +000056 }
57}
58
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059/// ReplaceCallWith - This function is used when we want to lower an intrinsic
60/// call to a call of an external function. This handles hard cases such as
61/// when there was already a prototype for the external function, and if that
62/// prototype doesn't match the arguments we expect to pass in.
63template <class ArgIt>
64static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
65 ArgIt ArgBegin, ArgIt ArgEnd,
Owen Anderson41d6d6c2009-06-26 20:33:47 +000066 const 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->getParent()->getParent()->getParent();
70 // Get or insert the definition now.
71 std::vector<const 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));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076
Jay Foade4094882009-05-12 20:27:44 +000077 IRBuilder<> Builder(CI->getParent(), CI);
David Greeneb1c4a7b2007-08-01 03:43:44 +000078 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Jay Foade4094882009-05-12 20:27:44 +000079 CallInst *NewCI = Builder.CreateCall(FCache, Args.begin(), Args.end());
80 NewCI->setName(CI->getName());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 if (!CI->use_empty())
82 CI->replaceAllUsesWith(NewCI);
83 return NewCI;
84}
85
Douglas Gregor491363c2010-05-11 06:17:44 +000086// VisualStudio defines setjmp as _setjmp
87#if defined(_MSC_VER) && defined(setjmp)
88#define setjmp_undefined_for_visual_studio
89#undef setjmp
90#endif
91
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092void IntrinsicLowering::AddPrototypes(Module &M) {
Owen Anderson35b47072009-08-13 21:58:54 +000093 LLVMContext &Context = M.getContext();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
95 if (I->isDeclaration() && !I->use_empty())
96 switch (I->getIntrinsicID()) {
97 default: break;
98 case Intrinsic::setjmp:
99 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Owen Anderson35b47072009-08-13 21:58:54 +0000100 Type::getInt32Ty(M.getContext()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 break;
102 case Intrinsic::longjmp:
103 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
Owen Anderson35b47072009-08-13 21:58:54 +0000104 Type::getVoidTy(M.getContext()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 break;
106 case Intrinsic::siglongjmp:
107 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
Owen Anderson35b47072009-08-13 21:58:54 +0000108 Type::getVoidTy(M.getContext()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 break;
Chris Lattner82c2e432008-11-21 16:42:48 +0000110 case Intrinsic::memcpy:
Owen Anderson35b47072009-08-13 21:58:54 +0000111 M.getOrInsertFunction("memcpy",
Duncan Sandsf2519d62009-10-06 15:40:36 +0000112 Type::getInt8PtrTy(Context),
113 Type::getInt8PtrTy(Context),
114 Type::getInt8PtrTy(Context),
Owen Anderson35b47072009-08-13 21:58:54 +0000115 TD.getIntPtrType(Context), (Type *)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 break;
Chris Lattner82c2e432008-11-21 16:42:48 +0000117 case Intrinsic::memmove:
Owen Anderson35b47072009-08-13 21:58:54 +0000118 M.getOrInsertFunction("memmove",
Duncan Sandsf2519d62009-10-06 15:40:36 +0000119 Type::getInt8PtrTy(Context),
120 Type::getInt8PtrTy(Context),
121 Type::getInt8PtrTy(Context),
Owen Anderson35b47072009-08-13 21:58:54 +0000122 TD.getIntPtrType(Context), (Type *)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 break;
Chris Lattner82c2e432008-11-21 16:42:48 +0000124 case Intrinsic::memset:
Owen Anderson35b47072009-08-13 21:58:54 +0000125 M.getOrInsertFunction("memset",
Duncan Sandsf2519d62009-10-06 15:40:36 +0000126 Type::getInt8PtrTy(Context),
127 Type::getInt8PtrTy(Context),
Owen Anderson35b47072009-08-13 21:58:54 +0000128 Type::getInt32Ty(M.getContext()),
129 TD.getIntPtrType(Context), (Type *)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 break;
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000131 case Intrinsic::sqrt:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000132 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 break;
Dan Gohman02f9ed92007-10-15 22:07:31 +0000134 case Intrinsic::sin:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000135 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000136 break;
137 case Intrinsic::cos:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000138 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000139 break;
140 case Intrinsic::pow:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000141 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000142 break;
Dale Johannesen92b33082008-09-04 00:47:13 +0000143 case Intrinsic::log:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000144 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000145 break;
146 case Intrinsic::log2:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000147 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000148 break;
149 case Intrinsic::log10:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000150 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000151 break;
152 case Intrinsic::exp:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000153 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000154 break;
155 case Intrinsic::exp2:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000156 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000157 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 }
159}
160
161/// LowerBSWAP - Emit the code to lower bswap of V before the specified
162/// instruction IP.
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000163static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
Duncan Sandse92dee12010-02-15 16:12:20 +0000164 assert(V->getType()->isIntegerTy() && "Can't bswap a non-integer type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
166 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
167
Jay Foade4094882009-05-12 20:27:44 +0000168 IRBuilder<> Builder(IP->getParent(), IP);
169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 switch(BitSize) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000171 default: llvm_unreachable("Unhandled type size of value to byteswap!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 case 16: {
Owen Andersoneacb44d2009-07-24 23:12:02 +0000173 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foade4094882009-05-12 20:27:44 +0000174 "bswap.2");
Owen Andersoneacb44d2009-07-24 23:12:02 +0000175 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foade4094882009-05-12 20:27:44 +0000176 "bswap.1");
177 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 break;
179 }
180 case 32: {
Owen Andersoneacb44d2009-07-24 23:12:02 +0000181 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
Jay Foade4094882009-05-12 20:27:44 +0000182 "bswap.4");
Owen Andersoneacb44d2009-07-24 23:12:02 +0000183 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foade4094882009-05-12 20:27:44 +0000184 "bswap.3");
Owen Andersoneacb44d2009-07-24 23:12:02 +0000185 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foade4094882009-05-12 20:27:44 +0000186 "bswap.2");
Owen Andersoneacb44d2009-07-24 23:12:02 +0000187 Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
Jay Foade4094882009-05-12 20:27:44 +0000188 "bswap.1");
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000189 Tmp3 = Builder.CreateAnd(Tmp3,
Owen Anderson35b47072009-08-13 21:58:54 +0000190 ConstantInt::get(Type::getInt32Ty(Context), 0xFF0000),
Jay Foade4094882009-05-12 20:27:44 +0000191 "bswap.and3");
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000192 Tmp2 = Builder.CreateAnd(Tmp2,
Owen Anderson35b47072009-08-13 21:58:54 +0000193 ConstantInt::get(Type::getInt32Ty(Context), 0xFF00),
Jay Foade4094882009-05-12 20:27:44 +0000194 "bswap.and2");
195 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
196 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
197 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 break;
199 }
200 case 64: {
Owen Andersoneacb44d2009-07-24 23:12:02 +0000201 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
Jay Foade4094882009-05-12 20:27:44 +0000202 "bswap.8");
Owen Andersoneacb44d2009-07-24 23:12:02 +0000203 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
Jay Foade4094882009-05-12 20:27:44 +0000204 "bswap.7");
Owen Andersoneacb44d2009-07-24 23:12:02 +0000205 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
Jay Foade4094882009-05-12 20:27:44 +0000206 "bswap.6");
Owen Andersoneacb44d2009-07-24 23:12:02 +0000207 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foade4094882009-05-12 20:27:44 +0000208 "bswap.5");
Owen Andersoneacb44d2009-07-24 23:12:02 +0000209 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foade4094882009-05-12 20:27:44 +0000210 "bswap.4");
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000211 Value* Tmp3 = Builder.CreateLShr(V,
Owen Andersoneacb44d2009-07-24 23:12:02 +0000212 ConstantInt::get(V->getType(), 24),
Jay Foade4094882009-05-12 20:27:44 +0000213 "bswap.3");
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000214 Value* Tmp2 = Builder.CreateLShr(V,
Owen Andersoneacb44d2009-07-24 23:12:02 +0000215 ConstantInt::get(V->getType(), 40),
Jay Foade4094882009-05-12 20:27:44 +0000216 "bswap.2");
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000217 Value* Tmp1 = Builder.CreateLShr(V,
Owen Andersoneacb44d2009-07-24 23:12:02 +0000218 ConstantInt::get(V->getType(), 56),
Jay Foade4094882009-05-12 20:27:44 +0000219 "bswap.1");
220 Tmp7 = Builder.CreateAnd(Tmp7,
Owen Anderson35b47072009-08-13 21:58:54 +0000221 ConstantInt::get(Type::getInt64Ty(Context),
Jay Foade4094882009-05-12 20:27:44 +0000222 0xFF000000000000ULL),
223 "bswap.and7");
224 Tmp6 = Builder.CreateAnd(Tmp6,
Owen Anderson35b47072009-08-13 21:58:54 +0000225 ConstantInt::get(Type::getInt64Ty(Context),
Jay Foade4094882009-05-12 20:27:44 +0000226 0xFF0000000000ULL),
227 "bswap.and6");
228 Tmp5 = Builder.CreateAnd(Tmp5,
Owen Anderson35b47072009-08-13 21:58:54 +0000229 ConstantInt::get(Type::getInt64Ty(Context),
230 0xFF00000000ULL),
Jay Foade4094882009-05-12 20:27:44 +0000231 "bswap.and5");
232 Tmp4 = Builder.CreateAnd(Tmp4,
Owen Anderson35b47072009-08-13 21:58:54 +0000233 ConstantInt::get(Type::getInt64Ty(Context),
234 0xFF000000ULL),
Jay Foade4094882009-05-12 20:27:44 +0000235 "bswap.and4");
236 Tmp3 = Builder.CreateAnd(Tmp3,
Owen Anderson35b47072009-08-13 21:58:54 +0000237 ConstantInt::get(Type::getInt64Ty(Context),
238 0xFF0000ULL),
Jay Foade4094882009-05-12 20:27:44 +0000239 "bswap.and3");
240 Tmp2 = Builder.CreateAnd(Tmp2,
Owen Anderson35b47072009-08-13 21:58:54 +0000241 ConstantInt::get(Type::getInt64Ty(Context),
242 0xFF00ULL),
Jay Foade4094882009-05-12 20:27:44 +0000243 "bswap.and2");
244 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
245 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
246 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
247 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
248 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
249 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
250 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 break;
252 }
253 }
254 return V;
255}
256
257/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
258/// instruction IP.
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000259static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
Duncan Sandse92dee12010-02-15 16:12:20 +0000260 assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261
262 static const uint64_t MaskValues[6] = {
263 0x5555555555555555ULL, 0x3333333333333333ULL,
264 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
265 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
266 };
267
Jay Foade4094882009-05-12 20:27:44 +0000268 IRBuilder<> Builder(IP->getParent(), IP);
269
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
271 unsigned WordSize = (BitSize + 63) / 64;
Owen Andersoneacb44d2009-07-24 23:12:02 +0000272 Value *Count = ConstantInt::get(V->getType(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273
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) {
Owen Andersoneacb44d2009-07-24 23:12:02 +0000278 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Jay Foade4094882009-05-12 20:27:44 +0000279 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
280 Value *VShift = Builder.CreateLShr(PartValue,
Owen Andersoneacb44d2009-07-24 23:12:02 +0000281 ConstantInt::get(V->getType(), i),
Jay Foade4094882009-05-12 20:27:44 +0000282 "ctpop.sh");
283 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
284 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 }
Jay Foade4094882009-05-12 20:27:44 +0000286 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 if (BitSize > 64) {
Owen Andersoneacb44d2009-07-24 23:12:02 +0000288 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
Jay Foade4094882009-05-12 20:27:44 +0000289 "ctpop.part.sh");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 BitSize -= 64;
291 }
292 }
293
Chris Lattnerf2cf7f02007-08-06 16:36:18 +0000294 return Count;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295}
296
297/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
298/// instruction IP.
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000299static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300
Jay Foade4094882009-05-12 20:27:44 +0000301 IRBuilder<> Builder(IP->getParent(), IP);
302
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
304 for (unsigned i = 1; i < BitSize; i <<= 1) {
Owen Andersoneacb44d2009-07-24 23:12:02 +0000305 Value *ShVal = ConstantInt::get(V->getType(), i);
Jay Foade4094882009-05-12 20:27:44 +0000306 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
307 V = Builder.CreateOr(V, ShVal, "ctlz.step");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 }
309
Jay Foade4094882009-05-12 20:27:44 +0000310 V = Builder.CreateNot(V);
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000311 return LowerCTPOP(Context, V, IP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312}
313
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000314static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
315 const char *Dname,
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000316 const char *LDname) {
Eric Christopherfbf918b2010-04-16 23:37:20 +0000317 switch (CI->getOperand(1)->getType()->getTypeID()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000318 default: llvm_unreachable("Invalid type in intrinsic");
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000319 case Type::FloatTyID:
Eric Christopherfbf918b2010-04-16 23:37:20 +0000320 ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson35b47072009-08-13 21:58:54 +0000321 Type::getFloatTy(CI->getContext()));
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000322 break;
323 case Type::DoubleTyID:
Eric Christopherfbf918b2010-04-16 23:37:20 +0000324 ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson35b47072009-08-13 21:58:54 +0000325 Type::getDoubleTy(CI->getContext()));
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000326 break;
327 case Type::X86_FP80TyID:
328 case Type::FP128TyID:
329 case Type::PPC_FP128TyID:
Eric Christopherfbf918b2010-04-16 23:37:20 +0000330 ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(),
331 CI->getOperand(1)->getType());
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000332 break;
333 }
334}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335
336void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Jay Foade4094882009-05-12 20:27:44 +0000337 IRBuilder<> Builder(CI->getParent(), CI);
Owen Anderson175b6542009-07-22 00:24:57 +0000338 LLVMContext &Context = CI->getContext();
Jay Foade4094882009-05-12 20:27:44 +0000339
Dan Gohman36c56d02010-04-15 01:51:59 +0000340 const Function *Callee = CI->getCalledFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 assert(Callee && "Cannot lower an indirect call!");
342
343 switch (Callee->getIntrinsicID()) {
344 case Intrinsic::not_intrinsic:
Chris Lattner8316f2d2010-04-07 22:58:41 +0000345 report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
Edwin Törökced9ff82009-07-11 13:10:19 +0000346 Callee->getName() + "'!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 default:
Chris Lattner8316f2d2010-04-07 22:58:41 +0000348 report_fatal_error("Code generator does not support intrinsic function '"+
Edwin Törökced9ff82009-07-11 13:10:19 +0000349 Callee->getName()+"'!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350
351 // The setjmp/longjmp intrinsics should only exist in the code if it was
352 // never optimized (ie, right out of the CFE), or if it has been hacked on
353 // by the lowerinvoke pass. In both cases, the right thing to do is to
354 // convert the call to an explicit setjmp or longjmp call.
355 case Intrinsic::setjmp: {
Eric Christopherfbf918b2010-04-16 23:37:20 +0000356 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson35b47072009-08-13 21:58:54 +0000357 Type::getInt32Ty(Context));
Benjamin Kramerf2052d52010-01-05 13:12:22 +0000358 if (!CI->getType()->isVoidTy())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 CI->replaceAllUsesWith(V);
360 break;
361 }
362 case Intrinsic::sigsetjmp:
Benjamin Kramerf2052d52010-01-05 13:12:22 +0000363 if (!CI->getType()->isVoidTy())
Owen Andersonaac28372009-07-31 20:28:14 +0000364 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 break;
366
367 case Intrinsic::longjmp: {
Eric Christopherfbf918b2010-04-16 23:37:20 +0000368 ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson35b47072009-08-13 21:58:54 +0000369 Type::getVoidTy(Context));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 break;
371 }
372
373 case Intrinsic::siglongjmp: {
374 // Insert the call to abort
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Owen Anderson35b47072009-08-13 21:58:54 +0000376 Type::getVoidTy(Context));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 break;
378 }
379 case Intrinsic::ctpop:
Eric Christopherfbf918b2010-04-16 23:37:20 +0000380 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(1), CI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 break;
382
383 case Intrinsic::bswap:
Eric Christopherfbf918b2010-04-16 23:37:20 +0000384 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(1), CI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 break;
386
387 case Intrinsic::ctlz:
Eric Christopherfbf918b2010-04-16 23:37:20 +0000388 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(1), CI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 break;
390
391 case Intrinsic::cttz: {
392 // cttz(x) -> ctpop(~X & (X-1))
Eric Christopherfbf918b2010-04-16 23:37:20 +0000393 Value *Src = CI->getOperand(1);
Jay Foade4094882009-05-12 20:27:44 +0000394 Value *NotSrc = Builder.CreateNot(Src);
395 NotSrc->setName(Src->getName() + ".not");
Owen Andersoneacb44d2009-07-24 23:12:02 +0000396 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Jay Foade4094882009-05-12 20:27:44 +0000397 SrcM1 = Builder.CreateSub(Src, SrcM1);
Owen Anderson175b6542009-07-22 00:24:57 +0000398 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 CI->replaceAllUsesWith(Src);
400 break;
401 }
402
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 case Intrinsic::stacksave:
404 case Intrinsic::stackrestore: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 if (!Warned)
Chris Lattner397f4562009-08-23 06:03:38 +0000406 errs() << "WARNING: this target does not support the llvm.stack"
407 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 "save" : "restore") << " intrinsic.\n";
409 Warned = true;
410 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
Owen Andersonaac28372009-07-31 20:28:14 +0000411 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 break;
413 }
414
415 case Intrinsic::returnaddress:
416 case Intrinsic::frameaddress:
Chris Lattner397f4562009-08-23 06:03:38 +0000417 errs() << "WARNING: this target does not support the llvm."
418 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 "return" : "frame") << "address intrinsic.\n";
420 CI->replaceAllUsesWith(ConstantPointerNull::get(
421 cast<PointerType>(CI->getType())));
422 break;
423
424 case Intrinsic::prefetch:
425 break; // Simply strip out prefetches on unsupported architectures
426
427 case Intrinsic::pcmarker:
428 break; // Simply strip out pcmarker on unsupported architectures
429 case Intrinsic::readcyclecounter: {
Chris Lattner397f4562009-08-23 06:03:38 +0000430 errs() << "WARNING: this target does not support the llvm.readcyclecoun"
431 << "ter intrinsic. It is being lowered to a constant 0\n";
Owen Anderson35b47072009-08-13 21:58:54 +0000432 CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 break;
434 }
435
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 case Intrinsic::dbg_declare:
437 break; // Simply strip out debugging intrinsics
438
439 case Intrinsic::eh_exception:
Duncan Sands4205cfe2009-10-14 16:11:37 +0000440 case Intrinsic::eh_selector:
Owen Andersonaac28372009-07-31 20:28:14 +0000441 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 break;
443
Duncan Sands4205cfe2009-10-14 16:11:37 +0000444 case Intrinsic::eh_typeid_for:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 // Return something different to eh_selector.
Owen Andersoneacb44d2009-07-24 23:12:02 +0000446 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 break;
448
449 case Intrinsic::var_annotation:
450 break; // Strip out annotate intrinsic
451
Chris Lattner82c2e432008-11-21 16:42:48 +0000452 case Intrinsic::memcpy: {
Owen Anderson35b47072009-08-13 21:58:54 +0000453 const IntegerType *IntPtr = TD.getIntPtrType(Context);
Eric Christopherfbf918b2010-04-16 23:37:20 +0000454 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
Jay Foade4094882009-05-12 20:27:44 +0000455 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 Value *Ops[3];
Eric Christopherfbf918b2010-04-16 23:37:20 +0000457 Ops[0] = CI->getOperand(1);
458 Ops[1] = CI->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 Ops[2] = Size;
Eric Christopherfbf918b2010-04-16 23:37:20 +0000460 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 break;
462 }
Chris Lattner82c2e432008-11-21 16:42:48 +0000463 case Intrinsic::memmove: {
Owen Anderson35b47072009-08-13 21:58:54 +0000464 const IntegerType *IntPtr = TD.getIntPtrType(Context);
Eric Christopherfbf918b2010-04-16 23:37:20 +0000465 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
Jay Foade4094882009-05-12 20:27:44 +0000466 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 Value *Ops[3];
Eric Christopherfbf918b2010-04-16 23:37:20 +0000468 Ops[0] = CI->getOperand(1);
469 Ops[1] = CI->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 Ops[2] = Size;
Eric Christopherfbf918b2010-04-16 23:37:20 +0000471 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 break;
473 }
Chris Lattner82c2e432008-11-21 16:42:48 +0000474 case Intrinsic::memset: {
Owen Anderson35b47072009-08-13 21:58:54 +0000475 const IntegerType *IntPtr = TD.getIntPtrType(Context);
Eric Christopherfbf918b2010-04-16 23:37:20 +0000476 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
Jay Foade4094882009-05-12 20:27:44 +0000477 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 Value *Ops[3];
Eric Christopherfbf918b2010-04-16 23:37:20 +0000479 Ops[0] = CI->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 // Extend the amount to i32.
Eric Christopherfbf918b2010-04-16 23:37:20 +0000481 Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::getInt32Ty(Context),
Jay Foade4094882009-05-12 20:27:44 +0000482 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 Ops[2] = Size;
Eric Christopherfbf918b2010-04-16 23:37:20 +0000484 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 break;
486 }
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000487 case Intrinsic::sqrt: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000488 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
Dale Johannesen3b5303b2007-09-28 18:06:58 +0000489 break;
490 }
Dale Johannesen92b33082008-09-04 00:47:13 +0000491 case Intrinsic::log: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000492 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000493 break;
494 }
495 case Intrinsic::log2: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000496 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000497 break;
498 }
499 case Intrinsic::log10: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000500 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000501 break;
502 }
503 case Intrinsic::exp: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000504 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000505 break;
506 }
507 case Intrinsic::exp2: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000508 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000509 break;
510 }
511 case Intrinsic::pow: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000512 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000513 break;
514 }
Anton Korobeynikovc915e272007-11-15 23:25:33 +0000515 case Intrinsic::flt_rounds:
516 // Lower to "round to the nearest"
Benjamin Kramerf2052d52010-01-05 13:12:22 +0000517 if (!CI->getType()->isVoidTy())
Owen Andersoneacb44d2009-07-24 23:12:02 +0000518 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
Anton Korobeynikovc915e272007-11-15 23:25:33 +0000519 break;
Duncan Sandsbb0ae062009-11-10 09:08:09 +0000520 case Intrinsic::invariant_start:
521 case Intrinsic::lifetime_start:
522 // Discard region information.
523 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
524 break;
525 case Intrinsic::invariant_end:
526 case Intrinsic::lifetime_end:
527 // Discard region information.
528 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 }
530
531 assert(CI->use_empty() &&
532 "Lowering should have eliminated any uses of the intrinsic call!");
533 CI->eraseFromParent();
534}