blob: 51879daa7c7e56fa077d4f6f11b9b425a901025c [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000018#include "llvm/Type.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
20#include "llvm/Support/Streams.h"
Reid Spencer6addf2c2007-01-29 17:42:06 +000021#include "llvm/Target/TargetData.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000022using namespace llvm;
23
Chris Lattner0979ca72004-05-09 04:29:57 +000024template <class ArgIt>
Chris Lattnerb76efb72007-01-07 08:12:01 +000025static void EnsureFunctionExists(Module &M, const char *Name,
26 ArgIt ArgBegin, ArgIt ArgEnd,
27 const Type *RetTy) {
28 // Insert a correctly-typed definition now.
Chris Lattner0979ca72004-05-09 04:29:57 +000029 std::vector<const Type *> ParamTys;
30 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
31 ParamTys.push_back(I->getType());
Chris Lattnerb76efb72007-01-07 08:12:01 +000032 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
Chris Lattner0979ca72004-05-09 04:29:57 +000033}
34
Chris Lattner588e72d2004-02-15 22:16:39 +000035/// ReplaceCallWith - This function is used when we want to lower an intrinsic
36/// call to a call of an external function. This handles hard cases such as
37/// when there was already a prototype for the external function, and if that
38/// prototype doesn't match the arguments we expect to pass in.
39template <class ArgIt>
40static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattnerb76efb72007-01-07 08:12:01 +000041 ArgIt ArgBegin, ArgIt ArgEnd,
42 const Type *RetTy, Constant *&FCache) {
Chris Lattner588e72d2004-02-15 22:16:39 +000043 if (!FCache) {
44 // If we haven't already looked up this function, check to see if the
45 // program already contains a function with this name.
46 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattnerb76efb72007-01-07 08:12:01 +000047 // Get or insert the definition now.
48 std::vector<const Type *> ParamTys;
49 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
50 ParamTys.push_back((*I)->getType());
51 FCache = M->getOrInsertFunction(NewFn,
52 FunctionType::get(RetTy, ParamTys, false));
Chris Lattner588e72d2004-02-15 22:16:39 +000053 }
Chris Lattner588e72d2004-02-15 22:16:39 +000054
Chris Lattnerb76efb72007-01-07 08:12:01 +000055 std::vector<Value*> Operands(ArgBegin, ArgEnd);
56 CallInst *NewCI = new CallInst(FCache, Operands, CI->getName(), CI);
57 if (!CI->use_empty())
58 CI->replaceAllUsesWith(NewCI);
Chris Lattner02348ca2004-06-11 02:54:02 +000059 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000060}
61
Chris Lattnerb71fd782006-11-15 18:00:10 +000062void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattner0979ca72004-05-09 04:29:57 +000063 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000064 if (I->isDeclaration() && !I->use_empty())
Chris Lattner0979ca72004-05-09 04:29:57 +000065 switch (I->getIntrinsicID()) {
66 default: break;
67 case Intrinsic::setjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000068 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Reid Spencer47857812006-12-31 05:55:36 +000069 Type::Int32Ty);
Chris Lattner0979ca72004-05-09 04:29:57 +000070 break;
71 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000072 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
73 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000074 break;
75 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000076 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
77 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000078 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000079 case Intrinsic::memcpy_i32:
80 case Intrinsic::memcpy_i64:
Reid Spencer1e9126b2007-01-28 22:28:00 +000081 M.getOrInsertFunction("memcpy", PointerType::get(Type::Int8Ty),
82 PointerType::get(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +000083 PointerType::get(Type::Int8Ty),
84 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +000085 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000086 case Intrinsic::memmove_i32:
87 case Intrinsic::memmove_i64:
Reid Spencer1e9126b2007-01-28 22:28:00 +000088 M.getOrInsertFunction("memmove", PointerType::get(Type::Int8Ty),
89 PointerType::get(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +000090 PointerType::get(Type::Int8Ty),
91 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +000092 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000093 case Intrinsic::memset_i32:
94 case Intrinsic::memset_i64:
Reid Spencer47857812006-12-31 05:55:36 +000095 M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +000096 PointerType::get(Type::Int8Ty), Type::Int32Ty,
97 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +000098 break;
Reid Spencer0b118202006-01-16 21:12:35 +000099 case Intrinsic::sqrt_f32:
100 case Intrinsic::sqrt_f64:
Alkis Evlogimenosb1beff02005-04-30 07:13:31 +0000101 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner1f243e92005-05-08 19:46:29 +0000102 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
103 Type::FloatTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000104 else
Chris Lattner1f243e92005-05-08 19:46:29 +0000105 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
106 Type::DoubleTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000107 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000108 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000109}
Chris Lattner588e72d2004-02-15 22:16:39 +0000110
Nate Begemane5981812006-01-16 07:57:00 +0000111/// LowerBSWAP - Emit the code to lower bswap of V before the specified
112/// instruction IP.
113static Value *LowerBSWAP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000114 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
Nate Begemane5981812006-01-16 07:57:00 +0000115
Nate Begemane5981812006-01-16 07:57:00 +0000116 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
117
118 switch(BitSize) {
119 default: assert(0 && "Unhandled type size of value to byteswap!");
120 case 16: {
Reid Spencer1b19cd32007-02-02 14:09:34 +0000121 Value *Tmp1 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000122 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000123 Value *Tmp2 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000124 ConstantInt::get(V->getType(),8),"bswap.1",IP);
Nate Begemane5981812006-01-16 07:57:00 +0000125 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
126 break;
127 }
128 case 32: {
Reid Spencer1b19cd32007-02-02 14:09:34 +0000129 Value *Tmp4 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000130 ConstantInt::get(V->getType(),24),"bswap.4", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000131 Value *Tmp3 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000132 ConstantInt::get(V->getType(),8),"bswap.3",IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000133 Value *Tmp2 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000134 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000135 Value *Tmp1 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000136 ConstantInt::get(V->getType(),24),"bswap.1", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000137 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000138 ConstantInt::get(Type::Int32Ty, 0xFF0000),
Nate Begemane5981812006-01-16 07:57:00 +0000139 "bswap.and3", IP);
140 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000141 ConstantInt::get(Type::Int32Ty, 0xFF00),
Nate Begemane5981812006-01-16 07:57:00 +0000142 "bswap.and2", IP);
143 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
144 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
145 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
146 break;
147 }
148 case 64: {
Reid Spencer1b19cd32007-02-02 14:09:34 +0000149 Value *Tmp8 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000150 ConstantInt::get(V->getType(),56),"bswap.8", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000151 Value *Tmp7 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000152 ConstantInt::get(V->getType(),40),"bswap.7", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000153 Value *Tmp6 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000154 ConstantInt::get(V->getType(),24),"bswap.6", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000155 Value *Tmp5 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000156 ConstantInt::get(V->getType(),8),"bswap.5", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000157 Value* Tmp4 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000158 ConstantInt::get(V->getType(),8),"bswap.4", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000159 Value* Tmp3 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000160 ConstantInt::get(V->getType(),24),"bswap.3", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000161 Value* Tmp2 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000162 ConstantInt::get(V->getType(),40),"bswap.2", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000163 Value* Tmp1 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000164 ConstantInt::get(V->getType(),56),"bswap.1", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000165 Tmp7 = BinaryOperator::createAnd(Tmp7,
Reid Spencer47857812006-12-31 05:55:36 +0000166 ConstantInt::get(Type::Int64Ty,
Reid Spencerb83eb642006-10-20 07:07:24 +0000167 0xFF000000000000ULL),
168 "bswap.and7", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000169 Tmp6 = BinaryOperator::createAnd(Tmp6,
Reid Spencer47857812006-12-31 05:55:36 +0000170 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000171 "bswap.and6", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000172 Tmp5 = BinaryOperator::createAnd(Tmp5,
Reid Spencer47857812006-12-31 05:55:36 +0000173 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000174 "bswap.and5", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000175 Tmp4 = BinaryOperator::createAnd(Tmp4,
Reid Spencer47857812006-12-31 05:55:36 +0000176 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000177 "bswap.and4", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000178 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000179 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000180 "bswap.and3", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000181 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000182 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000183 "bswap.and2", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000184 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
185 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
186 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
187 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
188 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
189 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
190 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
191 break;
192 }
193 }
Nate Begemane5981812006-01-16 07:57:00 +0000194 return V;
195}
196
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000197/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000198/// instruction IP.
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000199static Value *LowerCTPOP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000200 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000201
202 static const uint64_t MaskValues[6] = {
203 0x5555555555555555ULL, 0x3333333333333333ULL,
204 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
205 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
206 };
207
Chris Lattner98cf45b2005-05-11 20:24:12 +0000208 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Reid Spencer3822ff52006-11-08 06:47:33 +0000209
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000210 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
Chris Lattner3bb7e3f2006-12-12 05:19:46 +0000211 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000212 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000213 Value *VShift = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000214 ConstantInt::get(V->getType(), i), "ctpop.sh", IP);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000215 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
216 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
217 }
218
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000219 return V;
220}
221
Chris Lattner98cf45b2005-05-11 20:24:12 +0000222/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000223/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000224static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000225
226 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
227 for (unsigned i = 1; i != BitSize; i <<= 1) {
Reid Spencer832254e2007-02-02 02:16:23 +0000228 Value *ShVal = ConstantInt::get(V->getType(), i);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000229 ShVal = BinaryOperator::createLShr(V, ShVal, "ctlz.sh", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000230 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
231 }
232
Chris Lattner98cf45b2005-05-11 20:24:12 +0000233 V = BinaryOperator::createNot(V, "", IP);
234 return LowerCTPOP(V, IP);
235}
236
Nate Begemane5981812006-01-16 07:57:00 +0000237
238
Chris Lattnerb71fd782006-11-15 18:00:10 +0000239void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000240 Function *Callee = CI->getCalledFunction();
241 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000242
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000243 switch (Callee->getIntrinsicID()) {
244 case Intrinsic::not_intrinsic:
Bill Wendlinge8156192006-12-07 01:30:32 +0000245 cerr << "Cannot lower a call to a non-intrinsic function '"
246 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000247 abort();
248 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000249 cerr << "Error: Code generator does not support intrinsic function '"
250 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000251 abort();
252
Chris Lattner588e72d2004-02-15 22:16:39 +0000253 // The setjmp/longjmp intrinsics should only exist in the code if it was
254 // never optimized (ie, right out of the CFE), or if it has been hacked on
255 // by the lowerinvoke pass. In both cases, the right thing to do is to
256 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000257 case Intrinsic::setjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000258 static Constant *SetjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000259 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000260 Type::Int32Ty, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000261 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000262 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000263 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000264 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000265 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000266 if (CI->getType() != Type::VoidTy)
267 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
268 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000269
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000270 case Intrinsic::longjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000271 static Constant *LongjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000272 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000273 Type::VoidTy, LongjmpFCache);
Chris Lattner9b700f72004-02-15 22:24:51 +0000274 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000275 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000276
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000277 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000278 // Insert the call to abort
Chris Lattnerb76efb72007-01-07 08:12:01 +0000279 static Constant *AbortFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000280 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000281 Type::VoidTy, AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000282 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000283 }
Reid Spencer0b118202006-01-16 21:12:35 +0000284 case Intrinsic::ctpop_i8:
285 case Intrinsic::ctpop_i16:
286 case Intrinsic::ctpop_i32:
287 case Intrinsic::ctpop_i64:
288 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
289 break;
290
Nate Begemane5981812006-01-16 07:57:00 +0000291 case Intrinsic::bswap_i16:
292 case Intrinsic::bswap_i32:
293 case Intrinsic::bswap_i64:
294 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
295 break;
296
Reid Spencer0b118202006-01-16 21:12:35 +0000297 case Intrinsic::ctlz_i8:
298 case Intrinsic::ctlz_i16:
299 case Intrinsic::ctlz_i32:
300 case Intrinsic::ctlz_i64:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000301 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000302 break;
Nate Begemane5981812006-01-16 07:57:00 +0000303
Reid Spencer0b118202006-01-16 21:12:35 +0000304 case Intrinsic::cttz_i8:
305 case Intrinsic::cttz_i16:
306 case Intrinsic::cttz_i32:
307 case Intrinsic::cttz_i64: {
Chris Lattnera8011722005-05-11 20:02:14 +0000308 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000309 Value *Src = CI->getOperand(1);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000310 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnera8011722005-05-11 20:02:14 +0000311 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
312 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
313 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000314 CI->replaceAllUsesWith(Src);
315 break;
316 }
Chris Lattner77b13302004-01-05 05:36:30 +0000317
Chris Lattner0c067bc2006-01-13 02:22:08 +0000318 case Intrinsic::stacksave:
319 case Intrinsic::stackrestore: {
320 static bool Warned = false;
321 if (!Warned)
Bill Wendlinge8156192006-12-07 01:30:32 +0000322 cerr << "WARNING: this target does not support the llvm.stack"
323 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
324 "save" : "restore") << " intrinsic.\n";
Chris Lattner0c067bc2006-01-13 02:22:08 +0000325 Warned = true;
326 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
327 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
328 break;
329 }
330
Chris Lattnercf899082004-02-14 02:47:17 +0000331 case Intrinsic::returnaddress:
332 case Intrinsic::frameaddress:
Bill Wendlinge8156192006-12-07 01:30:32 +0000333 cerr << "WARNING: this target does not support the llvm."
334 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
335 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000336 CI->replaceAllUsesWith(ConstantPointerNull::get(
337 cast<PointerType>(CI->getType())));
338 break;
339
Chris Lattner0942b7c2005-02-28 19:27:23 +0000340 case Intrinsic::prefetch:
341 break; // Simply strip out prefetches on unsupported architectures
342
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000343 case Intrinsic::pcmarker:
344 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000345 case Intrinsic::readcyclecounter: {
Bill Wendlinge8156192006-12-07 01:30:32 +0000346 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
347 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencer47857812006-12-31 05:55:36 +0000348 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000349 break;
350 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000351
Chris Lattner77b13302004-01-05 05:36:30 +0000352 case Intrinsic::dbg_stoppoint:
353 case Intrinsic::dbg_region_start:
354 case Intrinsic::dbg_region_end:
355 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000356 case Intrinsic::dbg_declare:
Chris Lattner77b13302004-01-05 05:36:30 +0000357 break; // Simply strip out debugging intrinsics
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000358
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000359 case Intrinsic::memcpy_i32:
Reid Spencer3da59db2006-11-27 01:05:10 +0000360 case Intrinsic::memcpy_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000361 static Constant *MemcpyFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000362 Value *Size = CI->getOperand(3);
363 const Type *IntPtr = TD.getIntPtrType();
364 if (Size->getType()->getPrimitiveSizeInBits() <
365 IntPtr->getPrimitiveSizeInBits())
366 Size = new ZExtInst(Size, IntPtr, "", CI);
367 else if (Size->getType()->getPrimitiveSizeInBits() >
368 IntPtr->getPrimitiveSizeInBits())
369 Size = new TruncInst(Size, IntPtr, "", CI);
370 Value *Ops[3];
371 Ops[0] = CI->getOperand(1);
372 Ops[1] = CI->getOperand(2);
373 Ops[2] = Size;
374 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
375 MemcpyFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000376 break;
377 }
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000378 case Intrinsic::memmove_i32:
Chris Lattner03dd4652006-03-03 00:00:25 +0000379 case Intrinsic::memmove_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000380 static Constant *MemmoveFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000381 Value *Size = CI->getOperand(3);
382 const Type *IntPtr = TD.getIntPtrType();
383 if (Size->getType()->getPrimitiveSizeInBits() <
384 IntPtr->getPrimitiveSizeInBits())
385 Size = new ZExtInst(Size, IntPtr, "", CI);
386 else if (Size->getType()->getPrimitiveSizeInBits() >
387 IntPtr->getPrimitiveSizeInBits())
388 Size = new TruncInst(Size, IntPtr, "", CI);
389 Value *Ops[3];
390 Ops[0] = CI->getOperand(1);
391 Ops[1] = CI->getOperand(2);
392 Ops[2] = Size;
393 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
394 MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000395 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000396 }
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000397 case Intrinsic::memset_i32:
Chris Lattner03dd4652006-03-03 00:00:25 +0000398 case Intrinsic::memset_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000399 static Constant *MemsetFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000400 Value *Size = CI->getOperand(3);
Chris Lattner7d6f77d2007-02-06 06:07:51 +0000401 const Type *IntPtr = TD.getIntPtrType();
402 if (Size->getType()->getPrimitiveSizeInBits() <
403 IntPtr->getPrimitiveSizeInBits())
404 Size = new ZExtInst(Size, IntPtr, "", CI);
405 else if (Size->getType()->getPrimitiveSizeInBits() >
406 IntPtr->getPrimitiveSizeInBits())
407 Size = new TruncInst(Size, IntPtr, "", CI);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000408 Value *Ops[3];
409 Ops[0] = CI->getOperand(1);
410 // Extend the amount to i32.
411 Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI);
412 Ops[2] = Size;
413 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
414 MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000415 break;
416 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000417 case Intrinsic::sqrt_f32: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000418 static Constant *sqrtfFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000419 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000420 Type::FloatTy, sqrtfFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000421 break;
422 }
423 case Intrinsic::sqrt_f64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000424 static Constant *sqrtFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000425 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000426 Type::DoubleTy, sqrtFCache);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000427 break;
428 }
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000429 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000430
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000431 assert(CI->use_empty() &&
432 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000433 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000434}