blob: e3bbdb26eaf765460e65f68f35ca9e945b53fb93 [file] [log] [blame]
Chris Lattner3b66ecb2003-12-28 08:19:41 +00001//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3b66ecb2003-12-28 08:19:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3b66ecb2003-12-28 08:19:41 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerb71fd782006-11-15 18:00:10 +000010// This file implements the IntrinsicLowering class.
Chris Lattner3b66ecb2003-12-28 08:19:41 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnercf899082004-02-14 02:47:17 +000014#include "llvm/Constants.h"
Chris Lattner5fe51cc2004-02-12 17:01:09 +000015#include "llvm/DerivedTypes.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000016#include "llvm/Module.h"
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000017#include "llvm/Type.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000018#include "llvm/CodeGen/IntrinsicLowering.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000019#include "llvm/Support/ErrorHandling.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000020#include "llvm/Support/IRBuilder.h"
21#include "llvm/Support/raw_ostream.h"
Reid Spencer6addf2c2007-01-29 17:42:06 +000022#include "llvm/Target/TargetData.h"
Chris Lattner990b8492007-02-13 06:01:22 +000023#include "llvm/ADT/SmallVector.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000024using namespace llvm;
25
Chris Lattner0979ca72004-05-09 04:29:57 +000026template <class ArgIt>
Chris Lattnerb76efb72007-01-07 08:12:01 +000027static void EnsureFunctionExists(Module &M, const char *Name,
28 ArgIt ArgBegin, ArgIt ArgEnd,
29 const Type *RetTy) {
30 // Insert a correctly-typed definition now.
Chris Lattner0979ca72004-05-09 04:29:57 +000031 std::vector<const Type *> ParamTys;
32 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
33 ParamTys.push_back(I->getType());
Chris Lattnerb76efb72007-01-07 08:12:01 +000034 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
Chris Lattner0979ca72004-05-09 04:29:57 +000035}
36
Chris Lattner556b4a62009-02-07 22:37:06 +000037static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
38 const char *FName,
39 const char *DName, const char *LDName) {
Dale Johannesenc4342ea2008-09-22 19:51:58 +000040 // Insert definitions for all the floating point types.
Chris Lattner556b4a62009-02-07 22:37:06 +000041 switch((int)Fn->arg_begin()->getType()->getTypeID()) {
Dale Johannesenc4342ea2008-09-22 19:51:58 +000042 case Type::FloatTyID:
Chris Lattner556b4a62009-02-07 22:37:06 +000043 EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +000044 Type::getFloatTy(M.getContext()));
Chris Lattner556b4a62009-02-07 22:37:06 +000045 break;
Dale Johannesenc4342ea2008-09-22 19:51:58 +000046 case Type::DoubleTyID:
Chris Lattner556b4a62009-02-07 22:37:06 +000047 EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +000048 Type::getDoubleTy(M.getContext()));
Chris Lattner556b4a62009-02-07 22:37:06 +000049 break;
Dale Johannesenc4342ea2008-09-22 19:51:58 +000050 case Type::X86_FP80TyID:
51 case Type::FP128TyID:
52 case Type::PPC_FP128TyID:
Chris Lattner556b4a62009-02-07 22:37:06 +000053 EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
54 Fn->arg_begin()->getType());
55 break;
Dale Johannesenc4342ea2008-09-22 19:51:58 +000056 }
57}
58
Chris Lattner588e72d2004-02-15 22:16:39 +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,
Chris Lattnerb76efb72007-01-07 08:12:01 +000065 ArgIt ArgBegin, ArgIt ArgEnd,
Owen Andersonb41b5e02009-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));
Chris Lattner588e72d2004-02-15 22:16:39 +000076
Jay Foade1e20142009-05-12 20:27:44 +000077 IRBuilder<> Builder(CI->getParent(), CI);
David Greene52eec542007-08-01 03:43:44 +000078 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Jay Foade1e20142009-05-12 20:27:44 +000079 CallInst *NewCI = Builder.CreateCall(FCache, Args.begin(), Args.end());
80 NewCI->setName(CI->getName());
Chris Lattnerb76efb72007-01-07 08:12:01 +000081 if (!CI->use_empty())
82 CI->replaceAllUsesWith(NewCI);
Chris Lattner02348ca2004-06-11 02:54:02 +000083 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000084}
85
Chris Lattnerb71fd782006-11-15 18:00:10 +000086void IntrinsicLowering::AddPrototypes(Module &M) {
Owen Anderson1d0be152009-08-13 21:58:54 +000087 LLVMContext &Context = M.getContext();
Chris Lattner0979ca72004-05-09 04:29:57 +000088 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000089 if (I->isDeclaration() && !I->use_empty())
Chris Lattner0979ca72004-05-09 04:29:57 +000090 switch (I->getIntrinsicID()) {
91 default: break;
92 case Intrinsic::setjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000093 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +000094 Type::getInt32Ty(M.getContext()));
Chris Lattner0979ca72004-05-09 04:29:57 +000095 break;
96 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000097 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +000098 Type::getVoidTy(M.getContext()));
Chris Lattner0979ca72004-05-09 04:29:57 +000099 break;
100 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +0000101 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000102 Type::getVoidTy(M.getContext()));
Chris Lattner0979ca72004-05-09 04:29:57 +0000103 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000104 case Intrinsic::memcpy:
Owen Anderson1d0be152009-08-13 21:58:54 +0000105 M.getOrInsertFunction("memcpy",
106 PointerType::getUnqual(Type::getInt8Ty(Context)),
107 PointerType::getUnqual(Type::getInt8Ty(Context)),
108 PointerType::getUnqual(Type::getInt8Ty(Context)),
109 TD.getIntPtrType(Context), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000110 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000111 case Intrinsic::memmove:
Owen Anderson1d0be152009-08-13 21:58:54 +0000112 M.getOrInsertFunction("memmove",
113 PointerType::getUnqual(Type::getInt8Ty(Context)),
114 PointerType::getUnqual(Type::getInt8Ty(Context)),
115 PointerType::getUnqual(Type::getInt8Ty(Context)),
116 TD.getIntPtrType(Context), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000117 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000118 case Intrinsic::memset:
Owen Anderson1d0be152009-08-13 21:58:54 +0000119 M.getOrInsertFunction("memset",
120 PointerType::getUnqual(Type::getInt8Ty(Context)),
121 PointerType::getUnqual(Type::getInt8Ty(Context)),
122 Type::getInt32Ty(M.getContext()),
123 TD.getIntPtrType(Context), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000124 break;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000125 case Intrinsic::sqrt:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000126 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000127 break;
Dan Gohmanc4c96602007-10-15 22:07:31 +0000128 case Intrinsic::sin:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000129 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000130 break;
131 case Intrinsic::cos:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000132 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000133 break;
134 case Intrinsic::pow:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000135 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000136 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000137 case Intrinsic::log:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000138 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000139 break;
140 case Intrinsic::log2:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000141 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000142 break;
143 case Intrinsic::log10:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000144 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000145 break;
146 case Intrinsic::exp:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000147 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000148 break;
149 case Intrinsic::exp2:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000150 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000151 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000152 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000153}
Chris Lattner588e72d2004-02-15 22:16:39 +0000154
Nate Begemane5981812006-01-16 07:57:00 +0000155/// LowerBSWAP - Emit the code to lower bswap of V before the specified
156/// instruction IP.
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000157static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000158 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
Nate Begemane5981812006-01-16 07:57:00 +0000159
Nate Begemane5981812006-01-16 07:57:00 +0000160 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
161
Jay Foade1e20142009-05-12 20:27:44 +0000162 IRBuilder<> Builder(IP->getParent(), IP);
163
Nate Begemane5981812006-01-16 07:57:00 +0000164 switch(BitSize) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000165 default: llvm_unreachable("Unhandled type size of value to byteswap!");
Nate Begemane5981812006-01-16 07:57:00 +0000166 case 16: {
Owen Andersoneed707b2009-07-24 23:12:02 +0000167 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foade1e20142009-05-12 20:27:44 +0000168 "bswap.2");
Owen Andersoneed707b2009-07-24 23:12:02 +0000169 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foade1e20142009-05-12 20:27:44 +0000170 "bswap.1");
171 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
Nate Begemane5981812006-01-16 07:57:00 +0000172 break;
173 }
174 case 32: {
Owen Andersoneed707b2009-07-24 23:12:02 +0000175 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
Jay Foade1e20142009-05-12 20:27:44 +0000176 "bswap.4");
Owen Andersoneed707b2009-07-24 23:12:02 +0000177 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foade1e20142009-05-12 20:27:44 +0000178 "bswap.3");
Owen Andersoneed707b2009-07-24 23:12:02 +0000179 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foade1e20142009-05-12 20:27:44 +0000180 "bswap.2");
Owen Andersoneed707b2009-07-24 23:12:02 +0000181 Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
Jay Foade1e20142009-05-12 20:27:44 +0000182 "bswap.1");
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000183 Tmp3 = Builder.CreateAnd(Tmp3,
Owen Anderson1d0be152009-08-13 21:58:54 +0000184 ConstantInt::get(Type::getInt32Ty(Context), 0xFF0000),
Jay Foade1e20142009-05-12 20:27:44 +0000185 "bswap.and3");
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000186 Tmp2 = Builder.CreateAnd(Tmp2,
Owen Anderson1d0be152009-08-13 21:58:54 +0000187 ConstantInt::get(Type::getInt32Ty(Context), 0xFF00),
Jay Foade1e20142009-05-12 20:27:44 +0000188 "bswap.and2");
189 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
190 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
191 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
Nate Begemane5981812006-01-16 07:57:00 +0000192 break;
193 }
194 case 64: {
Owen Andersoneed707b2009-07-24 23:12:02 +0000195 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
Jay Foade1e20142009-05-12 20:27:44 +0000196 "bswap.8");
Owen Andersoneed707b2009-07-24 23:12:02 +0000197 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
Jay Foade1e20142009-05-12 20:27:44 +0000198 "bswap.7");
Owen Andersoneed707b2009-07-24 23:12:02 +0000199 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
Jay Foade1e20142009-05-12 20:27:44 +0000200 "bswap.6");
Owen Andersoneed707b2009-07-24 23:12:02 +0000201 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
Jay Foade1e20142009-05-12 20:27:44 +0000202 "bswap.5");
Owen Andersoneed707b2009-07-24 23:12:02 +0000203 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
Jay Foade1e20142009-05-12 20:27:44 +0000204 "bswap.4");
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000205 Value* Tmp3 = Builder.CreateLShr(V,
Owen Andersoneed707b2009-07-24 23:12:02 +0000206 ConstantInt::get(V->getType(), 24),
Jay Foade1e20142009-05-12 20:27:44 +0000207 "bswap.3");
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000208 Value* Tmp2 = Builder.CreateLShr(V,
Owen Andersoneed707b2009-07-24 23:12:02 +0000209 ConstantInt::get(V->getType(), 40),
Jay Foade1e20142009-05-12 20:27:44 +0000210 "bswap.2");
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000211 Value* Tmp1 = Builder.CreateLShr(V,
Owen Andersoneed707b2009-07-24 23:12:02 +0000212 ConstantInt::get(V->getType(), 56),
Jay Foade1e20142009-05-12 20:27:44 +0000213 "bswap.1");
214 Tmp7 = Builder.CreateAnd(Tmp7,
Owen Anderson1d0be152009-08-13 21:58:54 +0000215 ConstantInt::get(Type::getInt64Ty(Context),
Jay Foade1e20142009-05-12 20:27:44 +0000216 0xFF000000000000ULL),
217 "bswap.and7");
218 Tmp6 = Builder.CreateAnd(Tmp6,
Owen Anderson1d0be152009-08-13 21:58:54 +0000219 ConstantInt::get(Type::getInt64Ty(Context),
Jay Foade1e20142009-05-12 20:27:44 +0000220 0xFF0000000000ULL),
221 "bswap.and6");
222 Tmp5 = Builder.CreateAnd(Tmp5,
Owen Anderson1d0be152009-08-13 21:58:54 +0000223 ConstantInt::get(Type::getInt64Ty(Context),
224 0xFF00000000ULL),
Jay Foade1e20142009-05-12 20:27:44 +0000225 "bswap.and5");
226 Tmp4 = Builder.CreateAnd(Tmp4,
Owen Anderson1d0be152009-08-13 21:58:54 +0000227 ConstantInt::get(Type::getInt64Ty(Context),
228 0xFF000000ULL),
Jay Foade1e20142009-05-12 20:27:44 +0000229 "bswap.and4");
230 Tmp3 = Builder.CreateAnd(Tmp3,
Owen Anderson1d0be152009-08-13 21:58:54 +0000231 ConstantInt::get(Type::getInt64Ty(Context),
232 0xFF0000ULL),
Jay Foade1e20142009-05-12 20:27:44 +0000233 "bswap.and3");
234 Tmp2 = Builder.CreateAnd(Tmp2,
Owen Anderson1d0be152009-08-13 21:58:54 +0000235 ConstantInt::get(Type::getInt64Ty(Context),
236 0xFF00ULL),
Jay Foade1e20142009-05-12 20:27:44 +0000237 "bswap.and2");
238 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
239 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
240 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
241 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
242 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
243 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
244 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
Nate Begemane5981812006-01-16 07:57:00 +0000245 break;
246 }
247 }
Nate Begemane5981812006-01-16 07:57:00 +0000248 return V;
249}
250
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000251/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000252/// instruction IP.
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000253static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000254 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000255
256 static const uint64_t MaskValues[6] = {
257 0x5555555555555555ULL, 0x3333333333333333ULL,
258 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
259 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
260 };
261
Jay Foade1e20142009-05-12 20:27:44 +0000262 IRBuilder<> Builder(IP->getParent(), IP);
263
Chris Lattner98cf45b2005-05-11 20:24:12 +0000264 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000265 unsigned WordSize = (BitSize + 63) / 64;
Owen Andersoneed707b2009-07-24 23:12:02 +0000266 Value *Count = ConstantInt::get(V->getType(), 0);
Reid Spencer3822ff52006-11-08 06:47:33 +0000267
Zhou Sheng02031c02007-06-02 04:10:33 +0000268 for (unsigned n = 0; n < WordSize; ++n) {
269 Value *PartValue = V;
270 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
271 i <<= 1, ++ct) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000272 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Jay Foade1e20142009-05-12 20:27:44 +0000273 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
274 Value *VShift = Builder.CreateLShr(PartValue,
Owen Andersoneed707b2009-07-24 23:12:02 +0000275 ConstantInt::get(V->getType(), i),
Jay Foade1e20142009-05-12 20:27:44 +0000276 "ctpop.sh");
277 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
278 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
Zhou Sheng02031c02007-06-02 04:10:33 +0000279 }
Jay Foade1e20142009-05-12 20:27:44 +0000280 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
Zhou Sheng02031c02007-06-02 04:10:33 +0000281 if (BitSize > 64) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000282 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
Jay Foade1e20142009-05-12 20:27:44 +0000283 "ctpop.part.sh");
Zhou Sheng02031c02007-06-02 04:10:33 +0000284 BitSize -= 64;
285 }
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000286 }
287
Chris Lattner914ce452007-08-06 16:36:18 +0000288 return Count;
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000289}
290
Chris Lattner98cf45b2005-05-11 20:24:12 +0000291/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000292/// instruction IP.
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000293static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000294
Jay Foade1e20142009-05-12 20:27:44 +0000295 IRBuilder<> Builder(IP->getParent(), IP);
296
Chris Lattner98cf45b2005-05-11 20:24:12 +0000297 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000298 for (unsigned i = 1; i < BitSize; i <<= 1) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000299 Value *ShVal = ConstantInt::get(V->getType(), i);
Jay Foade1e20142009-05-12 20:27:44 +0000300 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
301 V = Builder.CreateOr(V, ShVal, "ctlz.step");
Chris Lattner98cf45b2005-05-11 20:24:12 +0000302 }
303
Jay Foade1e20142009-05-12 20:27:44 +0000304 V = Builder.CreateNot(V);
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000305 return LowerCTPOP(Context, V, IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000306}
307
Owen Andersonb41b5e02009-06-26 20:33:47 +0000308static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
309 const char *Dname,
Dale Johannesenf74185b2008-09-22 20:51:30 +0000310 const char *LDname) {
311 switch (CI->getOperand(1)->getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000312 default: llvm_unreachable("Invalid type in intrinsic");
Dale Johannesenf74185b2008-09-22 20:51:30 +0000313 case Type::FloatTyID:
Jay Foade1e20142009-05-12 20:27:44 +0000314 ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000315 Type::getFloatTy(CI->getContext()));
Dale Johannesenf74185b2008-09-22 20:51:30 +0000316 break;
317 case Type::DoubleTyID:
Jay Foade1e20142009-05-12 20:27:44 +0000318 ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000319 Type::getDoubleTy(CI->getContext()));
Dale Johannesenf74185b2008-09-22 20:51:30 +0000320 break;
321 case Type::X86_FP80TyID:
322 case Type::FP128TyID:
323 case Type::PPC_FP128TyID:
Jay Foade1e20142009-05-12 20:27:44 +0000324 ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Andersonb41b5e02009-06-26 20:33:47 +0000325 CI->getOperand(1)->getType());
Dale Johannesenf74185b2008-09-22 20:51:30 +0000326 break;
327 }
328}
Reid Spenceraddd11d2007-04-04 23:48:25 +0000329
Chris Lattnerb71fd782006-11-15 18:00:10 +0000330void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Jay Foade1e20142009-05-12 20:27:44 +0000331 IRBuilder<> Builder(CI->getParent(), CI);
Owen Andersone922c022009-07-22 00:24:57 +0000332 LLVMContext &Context = CI->getContext();
Jay Foade1e20142009-05-12 20:27:44 +0000333
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000334 Function *Callee = CI->getCalledFunction();
335 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000336
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000337 switch (Callee->getIntrinsicID()) {
338 case Intrinsic::not_intrinsic:
Torok Edwin7d696d82009-07-11 13:10:19 +0000339 llvm_report_error("Cannot lower a call to a non-intrinsic function '"+
340 Callee->getName() + "'!");
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000341 default:
Torok Edwin7d696d82009-07-11 13:10:19 +0000342 llvm_report_error("Code generator does not support intrinsic function '"+
343 Callee->getName()+"'!");
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000344
Chris Lattner588e72d2004-02-15 22:16:39 +0000345 // The setjmp/longjmp intrinsics should only exist in the code if it was
346 // never optimized (ie, right out of the CFE), or if it has been hacked on
347 // by the lowerinvoke pass. In both cases, the right thing to do is to
348 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000349 case Intrinsic::setjmp: {
Jay Foade1e20142009-05-12 20:27:44 +0000350 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000351 Type::getInt32Ty(Context));
352 if (CI->getType() != Type::getVoidTy(Context))
Chris Lattner9b700f72004-02-15 22:24:51 +0000353 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000354 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000355 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000356 case Intrinsic::sigsetjmp:
Owen Anderson1d0be152009-08-13 21:58:54 +0000357 if (CI->getType() != Type::getVoidTy(Context))
Owen Andersona7235ea2009-07-31 20:28:14 +0000358 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Chris Lattner9b700f72004-02-15 22:24:51 +0000359 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000360
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000361 case Intrinsic::longjmp: {
Jay Foade1e20142009-05-12 20:27:44 +0000362 ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000363 Type::getVoidTy(Context));
Chris Lattner9b700f72004-02-15 22:24:51 +0000364 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000365 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000366
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000367 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000368 // Insert the call to abort
Reid Spencer3da59db2006-11-27 01:05:10 +0000369 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000370 Type::getVoidTy(Context));
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000371 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000372 }
Reid Spencere9391fd2007-04-01 07:35:23 +0000373 case Intrinsic::ctpop:
Owen Andersone922c022009-07-22 00:24:57 +0000374 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(1), CI));
Reid Spencer0b118202006-01-16 21:12:35 +0000375 break;
376
Reid Spencere9391fd2007-04-01 07:35:23 +0000377 case Intrinsic::bswap:
Owen Andersone922c022009-07-22 00:24:57 +0000378 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(1), CI));
Nate Begemane5981812006-01-16 07:57:00 +0000379 break;
380
Reid Spencere9391fd2007-04-01 07:35:23 +0000381 case Intrinsic::ctlz:
Owen Andersone922c022009-07-22 00:24:57 +0000382 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000383 break;
Nate Begemane5981812006-01-16 07:57:00 +0000384
Reid Spencere9391fd2007-04-01 07:35:23 +0000385 case Intrinsic::cttz: {
Chris Lattnera8011722005-05-11 20:02:14 +0000386 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000387 Value *Src = CI->getOperand(1);
Jay Foade1e20142009-05-12 20:27:44 +0000388 Value *NotSrc = Builder.CreateNot(Src);
389 NotSrc->setName(Src->getName() + ".not");
Owen Andersoneed707b2009-07-24 23:12:02 +0000390 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Jay Foade1e20142009-05-12 20:27:44 +0000391 SrcM1 = Builder.CreateSub(Src, SrcM1);
Owen Andersone922c022009-07-22 00:24:57 +0000392 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000393 CI->replaceAllUsesWith(Src);
394 break;
395 }
Chris Lattner77b13302004-01-05 05:36:30 +0000396
Chris Lattner0c067bc2006-01-13 02:22:08 +0000397 case Intrinsic::stacksave:
398 case Intrinsic::stackrestore: {
Chris Lattner0c067bc2006-01-13 02:22:08 +0000399 if (!Warned)
Chris Lattner45cfe542009-08-23 06:03:38 +0000400 errs() << "WARNING: this target does not support the llvm.stack"
401 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
Bill Wendlinge8156192006-12-07 01:30:32 +0000402 "save" : "restore") << " intrinsic.\n";
Chris Lattner0c067bc2006-01-13 02:22:08 +0000403 Warned = true;
404 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
Owen Andersona7235ea2009-07-31 20:28:14 +0000405 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Chris Lattner0c067bc2006-01-13 02:22:08 +0000406 break;
407 }
408
Chris Lattnercf899082004-02-14 02:47:17 +0000409 case Intrinsic::returnaddress:
410 case Intrinsic::frameaddress:
Chris Lattner45cfe542009-08-23 06:03:38 +0000411 errs() << "WARNING: this target does not support the llvm."
412 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
Bill Wendlinge8156192006-12-07 01:30:32 +0000413 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000414 CI->replaceAllUsesWith(ConstantPointerNull::get(
415 cast<PointerType>(CI->getType())));
416 break;
417
Chris Lattner0942b7c2005-02-28 19:27:23 +0000418 case Intrinsic::prefetch:
419 break; // Simply strip out prefetches on unsupported architectures
420
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000421 case Intrinsic::pcmarker:
422 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000423 case Intrinsic::readcyclecounter: {
Chris Lattner45cfe542009-08-23 06:03:38 +0000424 errs() << "WARNING: this target does not support the llvm.readcyclecoun"
425 << "ter intrinsic. It is being lowered to a constant 0\n";
Owen Anderson1d0be152009-08-13 21:58:54 +0000426 CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000427 break;
428 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000429
Chris Lattner77b13302004-01-05 05:36:30 +0000430 case Intrinsic::dbg_stoppoint:
431 case Intrinsic::dbg_region_start:
432 case Intrinsic::dbg_region_end:
433 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000434 case Intrinsic::dbg_declare:
Duncan Sandsf664e412007-07-06 14:46:23 +0000435 break; // Simply strip out debugging intrinsics
436
Jim Laskeyb180aa12007-02-21 22:53:45 +0000437 case Intrinsic::eh_exception:
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000438 case Intrinsic::eh_selector_i32:
439 case Intrinsic::eh_selector_i64:
Owen Andersona7235ea2009-07-31 20:28:14 +0000440 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
Duncan Sandsf664e412007-07-06 14:46:23 +0000441 break;
442
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000443 case Intrinsic::eh_typeid_for_i32:
444 case Intrinsic::eh_typeid_for_i64:
Duncan Sandsf664e412007-07-06 14:46:23 +0000445 // Return something different to eh_selector.
Owen Andersoneed707b2009-07-24 23:12:02 +0000446 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
Duncan Sandsf664e412007-07-06 14:46:23 +0000447 break;
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000448
Tanya Lattner24e5aad2007-06-15 22:26:58 +0000449 case Intrinsic::var_annotation:
450 break; // Strip out annotate intrinsic
451
Chris Lattner824b9582008-11-21 16:42:48 +0000452 case Intrinsic::memcpy: {
Owen Anderson1d0be152009-08-13 21:58:54 +0000453 const IntegerType *IntPtr = TD.getIntPtrType(Context);
Jay Foade1e20142009-05-12 20:27:44 +0000454 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
455 /* isSigned */ false);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000456 Value *Ops[3];
457 Ops[0] = CI->getOperand(1);
458 Ops[1] = CI->getOperand(2);
459 Ops[2] = Size;
Owen Andersonb41b5e02009-06-26 20:33:47 +0000460 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Reid Spencer3da59db2006-11-27 01:05:10 +0000461 break;
462 }
Chris Lattner824b9582008-11-21 16:42:48 +0000463 case Intrinsic::memmove: {
Owen Anderson1d0be152009-08-13 21:58:54 +0000464 const IntegerType *IntPtr = TD.getIntPtrType(Context);
Jay Foade1e20142009-05-12 20:27:44 +0000465 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
466 /* isSigned */ false);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000467 Value *Ops[3];
468 Ops[0] = CI->getOperand(1);
469 Ops[1] = CI->getOperand(2);
470 Ops[2] = Size;
Owen Andersonb41b5e02009-06-26 20:33:47 +0000471 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Chris Lattner2751e762004-02-12 18:11:20 +0000472 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000473 }
Chris Lattner824b9582008-11-21 16:42:48 +0000474 case Intrinsic::memset: {
Owen Anderson1d0be152009-08-13 21:58:54 +0000475 const IntegerType *IntPtr = TD.getIntPtrType(Context);
Jay Foade1e20142009-05-12 20:27:44 +0000476 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
477 /* isSigned */ false);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000478 Value *Ops[3];
479 Ops[0] = CI->getOperand(1);
480 // Extend the amount to i32.
Owen Anderson1d0be152009-08-13 21:58:54 +0000481 Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::getInt32Ty(Context),
Jay Foade1e20142009-05-12 20:27:44 +0000482 /* isSigned */ false);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000483 Ops[2] = Size;
Owen Andersonb41b5e02009-06-26 20:33:47 +0000484 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Chris Lattnercf899082004-02-14 02:47:17 +0000485 break;
486 }
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000487 case Intrinsic::sqrt: {
Owen Andersonb41b5e02009-06-26 20:33:47 +0000488 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
Dale Johannesen4292d1c2007-09-28 18:06:58 +0000489 break;
490 }
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000491 case Intrinsic::log: {
Owen Andersonb41b5e02009-06-26 20:33:47 +0000492 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000493 break;
494 }
495 case Intrinsic::log2: {
Owen Andersonb41b5e02009-06-26 20:33:47 +0000496 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000497 break;
498 }
499 case Intrinsic::log10: {
Owen Andersonb41b5e02009-06-26 20:33:47 +0000500 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000501 break;
502 }
503 case Intrinsic::exp: {
Owen Andersonb41b5e02009-06-26 20:33:47 +0000504 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000505 break;
506 }
507 case Intrinsic::exp2: {
Owen Andersonb41b5e02009-06-26 20:33:47 +0000508 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000509 break;
510 }
511 case Intrinsic::pow: {
Owen Andersonb41b5e02009-06-26 20:33:47 +0000512 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000513 break;
514 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +0000515 case Intrinsic::flt_rounds:
516 // Lower to "round to the nearest"
Owen Anderson1d0be152009-08-13 21:58:54 +0000517 if (CI->getType() != Type::getVoidTy(Context))
Owen Andersoneed707b2009-07-24 23:12:02 +0000518 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
Anton Korobeynikov917c2a62007-11-15 23:25:33 +0000519 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000520 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000521
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000522 assert(CI->use_empty() &&
523 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000524 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000525}