1 //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the IntrinsicLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Constants.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Module.h"
17 #include "llvm/Type.h"
18 #include "llvm/CodeGen/IntrinsicLowering.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/IRBuilder.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/ADT/SmallVector.h"
26 template <class ArgIt>
27 static void EnsureFunctionExists(Module &M, const char *Name,
28 ArgIt ArgBegin, ArgIt ArgEnd,
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));
37 static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
39 const char *DName, const char *LDName) {
40 // Insert definitions for all the floating point types.
41 switch((int)Fn->arg_begin()->getType()->getTypeID()) {
43 EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
44 Type::getFloatTy(M.getContext()));
46 case Type::DoubleTyID:
47 EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
48 Type::getDoubleTy(M.getContext()));
50 case Type::X86_FP80TyID:
52 case Type::PPC_FP128TyID:
53 EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
54 Fn->arg_begin()->getType());
59 /// 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.
63 template <class ArgIt>
64 static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
65 ArgIt ArgBegin, ArgIt ArgEnd,
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));
77 IRBuilder<> Builder(CI->getParent(), CI);
78 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
79 CallInst *NewCI = Builder.CreateCall(FCache, Args.begin(), Args.end());
80 NewCI->setName(CI->getName());
82 CI->replaceAllUsesWith(NewCI);
86 // VisualStudio defines setjmp as _setjmp
87 #if defined(_MSC_VER) && defined(setjmp)
88 #define setjmp_undefined_for_visual_studio
92 void IntrinsicLowering::AddPrototypes(Module &M) {
93 LLVMContext &Context = M.getContext();
94 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
95 if (I->isDeclaration() && !I->use_empty())
96 switch (I->getIntrinsicID()) {
98 case Intrinsic::setjmp:
99 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
100 Type::getInt32Ty(M.getContext()));
102 case Intrinsic::longjmp:
103 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
104 Type::getVoidTy(M.getContext()));
106 case Intrinsic::siglongjmp:
107 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
108 Type::getVoidTy(M.getContext()));
110 case Intrinsic::memcpy:
111 M.getOrInsertFunction("memcpy",
112 Type::getInt8PtrTy(Context),
113 Type::getInt8PtrTy(Context),
114 Type::getInt8PtrTy(Context),
115 TD.getIntPtrType(Context), (Type *)0);
117 case Intrinsic::memmove:
118 M.getOrInsertFunction("memmove",
119 Type::getInt8PtrTy(Context),
120 Type::getInt8PtrTy(Context),
121 Type::getInt8PtrTy(Context),
122 TD.getIntPtrType(Context), (Type *)0);
124 case Intrinsic::memset:
125 M.getOrInsertFunction("memset",
126 Type::getInt8PtrTy(Context),
127 Type::getInt8PtrTy(Context),
128 Type::getInt32Ty(M.getContext()),
129 TD.getIntPtrType(Context), (Type *)0);
131 case Intrinsic::sqrt:
132 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
135 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
138 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
141 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
144 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
146 case Intrinsic::log2:
147 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
149 case Intrinsic::log10:
150 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
153 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
155 case Intrinsic::exp2:
156 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
161 /// LowerBSWAP - Emit the code to lower bswap of V before the specified
163 static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
164 assert(V->getType()->isIntegerTy() && "Can't bswap a non-integer type!");
166 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
168 IRBuilder<> Builder(IP->getParent(), IP);
171 default: llvm_unreachable("Unhandled type size of value to byteswap!");
173 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
175 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
177 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
181 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
183 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
185 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
187 Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
189 Tmp3 = Builder.CreateAnd(Tmp3,
190 ConstantInt::get(Type::getInt32Ty(Context), 0xFF0000),
192 Tmp2 = Builder.CreateAnd(Tmp2,
193 ConstantInt::get(Type::getInt32Ty(Context), 0xFF00),
195 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
196 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
197 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
201 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
203 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
205 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
207 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
209 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
211 Value* Tmp3 = Builder.CreateLShr(V,
212 ConstantInt::get(V->getType(), 24),
214 Value* Tmp2 = Builder.CreateLShr(V,
215 ConstantInt::get(V->getType(), 40),
217 Value* Tmp1 = Builder.CreateLShr(V,
218 ConstantInt::get(V->getType(), 56),
220 Tmp7 = Builder.CreateAnd(Tmp7,
221 ConstantInt::get(Type::getInt64Ty(Context),
222 0xFF000000000000ULL),
224 Tmp6 = Builder.CreateAnd(Tmp6,
225 ConstantInt::get(Type::getInt64Ty(Context),
228 Tmp5 = Builder.CreateAnd(Tmp5,
229 ConstantInt::get(Type::getInt64Ty(Context),
232 Tmp4 = Builder.CreateAnd(Tmp4,
233 ConstantInt::get(Type::getInt64Ty(Context),
236 Tmp3 = Builder.CreateAnd(Tmp3,
237 ConstantInt::get(Type::getInt64Ty(Context),
240 Tmp2 = Builder.CreateAnd(Tmp2,
241 ConstantInt::get(Type::getInt64Ty(Context),
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");
257 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
259 static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
260 assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
262 static const uint64_t MaskValues[6] = {
263 0x5555555555555555ULL, 0x3333333333333333ULL,
264 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
265 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
268 IRBuilder<> Builder(IP->getParent(), IP);
270 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
271 unsigned WordSize = (BitSize + 63) / 64;
272 Value *Count = ConstantInt::get(V->getType(), 0);
274 for (unsigned n = 0; n < WordSize; ++n) {
275 Value *PartValue = V;
276 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
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),
283 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
284 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
286 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
288 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
297 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
299 static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
301 IRBuilder<> Builder(IP->getParent(), IP);
303 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
304 for (unsigned i = 1; i < BitSize; i <<= 1) {
305 Value *ShVal = ConstantInt::get(V->getType(), i);
306 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
307 V = Builder.CreateOr(V, ShVal, "ctlz.step");
310 V = Builder.CreateNot(V);
311 return LowerCTPOP(Context, V, IP);
314 static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
316 const char *LDname) {
317 switch (CI->getOperand(1)->getType()->getTypeID()) {
318 default: llvm_unreachable("Invalid type in intrinsic");
319 case Type::FloatTyID:
320 ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(),
321 Type::getFloatTy(CI->getContext()));
323 case Type::DoubleTyID:
324 ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(),
325 Type::getDoubleTy(CI->getContext()));
327 case Type::X86_FP80TyID:
328 case Type::FP128TyID:
329 case Type::PPC_FP128TyID:
330 ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(),
331 CI->getOperand(1)->getType());
336 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
337 IRBuilder<> Builder(CI->getParent(), CI);
338 LLVMContext &Context = CI->getContext();
340 const Function *Callee = CI->getCalledFunction();
341 assert(Callee && "Cannot lower an indirect call!");
343 switch (Callee->getIntrinsicID()) {
344 case Intrinsic::not_intrinsic:
345 report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
346 Callee->getName() + "'!");
348 report_fatal_error("Code generator does not support intrinsic function '"+
349 Callee->getName()+"'!");
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: {
356 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(),
357 Type::getInt32Ty(Context));
358 if (!CI->getType()->isVoidTy())
359 CI->replaceAllUsesWith(V);
362 case Intrinsic::sigsetjmp:
363 if (!CI->getType()->isVoidTy())
364 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
367 case Intrinsic::longjmp: {
368 ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(),
369 Type::getVoidTy(Context));
373 case Intrinsic::siglongjmp: {
374 // Insert the call to abort
375 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
376 Type::getVoidTy(Context));
379 case Intrinsic::ctpop:
380 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(1), CI));
383 case Intrinsic::bswap:
384 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(1), CI));
387 case Intrinsic::ctlz:
388 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(1), CI));
391 case Intrinsic::cttz: {
392 // cttz(x) -> ctpop(~X & (X-1))
393 Value *Src = CI->getOperand(1);
394 Value *NotSrc = Builder.CreateNot(Src);
395 NotSrc->setName(Src->getName() + ".not");
396 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
397 SrcM1 = Builder.CreateSub(Src, SrcM1);
398 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
399 CI->replaceAllUsesWith(Src);
403 case Intrinsic::stacksave:
404 case Intrinsic::stackrestore: {
406 errs() << "WARNING: this target does not support the llvm.stack"
407 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
408 "save" : "restore") << " intrinsic.\n";
410 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
411 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
415 case Intrinsic::returnaddress:
416 case Intrinsic::frameaddress:
417 errs() << "WARNING: this target does not support the llvm."
418 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
419 "return" : "frame") << "address intrinsic.\n";
420 CI->replaceAllUsesWith(ConstantPointerNull::get(
421 cast<PointerType>(CI->getType())));
424 case Intrinsic::prefetch:
425 break; // Simply strip out prefetches on unsupported architectures
427 case Intrinsic::pcmarker:
428 break; // Simply strip out pcmarker on unsupported architectures
429 case Intrinsic::readcyclecounter: {
430 errs() << "WARNING: this target does not support the llvm.readcyclecoun"
431 << "ter intrinsic. It is being lowered to a constant 0\n";
432 CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
436 case Intrinsic::dbg_declare:
437 break; // Simply strip out debugging intrinsics
439 case Intrinsic::eh_exception:
440 case Intrinsic::eh_selector:
441 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
444 case Intrinsic::eh_typeid_for:
445 // Return something different to eh_selector.
446 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
449 case Intrinsic::var_annotation:
450 break; // Strip out annotate intrinsic
452 case Intrinsic::memcpy: {
453 const IntegerType *IntPtr = TD.getIntPtrType(Context);
454 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
455 /* isSigned */ false);
457 Ops[0] = CI->getOperand(1);
458 Ops[1] = CI->getOperand(2);
460 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType());
463 case Intrinsic::memmove: {
464 const IntegerType *IntPtr = TD.getIntPtrType(Context);
465 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
466 /* isSigned */ false);
468 Ops[0] = CI->getOperand(1);
469 Ops[1] = CI->getOperand(2);
471 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType());
474 case Intrinsic::memset: {
475 const IntegerType *IntPtr = TD.getIntPtrType(Context);
476 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
477 /* isSigned */ false);
479 Ops[0] = CI->getOperand(1);
480 // Extend the amount to i32.
481 Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::getInt32Ty(Context),
482 /* isSigned */ false);
484 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType());
487 case Intrinsic::sqrt: {
488 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
491 case Intrinsic::log: {
492 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
495 case Intrinsic::log2: {
496 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
499 case Intrinsic::log10: {
500 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
503 case Intrinsic::exp: {
504 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
507 case Intrinsic::exp2: {
508 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
511 case Intrinsic::pow: {
512 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
515 case Intrinsic::flt_rounds:
516 // Lower to "round to the nearest"
517 if (!CI->getType()->isVoidTy())
518 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
520 case Intrinsic::invariant_start:
521 case Intrinsic::lifetime_start:
522 // Discard region information.
523 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
525 case Intrinsic::invariant_end:
526 case Intrinsic::lifetime_end:
527 // Discard region information.
531 assert(CI->use_empty() &&
532 "Lowering should have eliminated any uses of the intrinsic call!");
533 CI->eraseFromParent();