blob: 72a1d1485b49118e5a2d7039705f915617e881a3 [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"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000021using namespace llvm;
22
Chris Lattner0979ca72004-05-09 04:29:57 +000023template <class ArgIt>
Chris Lattnerb76efb72007-01-07 08:12:01 +000024static void EnsureFunctionExists(Module &M, const char *Name,
25 ArgIt ArgBegin, ArgIt ArgEnd,
26 const Type *RetTy) {
27 // Insert a correctly-typed definition now.
Chris Lattner0979ca72004-05-09 04:29:57 +000028 std::vector<const Type *> ParamTys;
29 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
30 ParamTys.push_back(I->getType());
Chris Lattnerb76efb72007-01-07 08:12:01 +000031 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
Chris Lattner0979ca72004-05-09 04:29:57 +000032}
33
Chris Lattner588e72d2004-02-15 22:16:39 +000034/// ReplaceCallWith - This function is used when we want to lower an intrinsic
35/// call to a call of an external function. This handles hard cases such as
36/// when there was already a prototype for the external function, and if that
37/// prototype doesn't match the arguments we expect to pass in.
38template <class ArgIt>
39static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattnerb76efb72007-01-07 08:12:01 +000040 ArgIt ArgBegin, ArgIt ArgEnd,
41 const Type *RetTy, Constant *&FCache) {
Chris Lattner588e72d2004-02-15 22:16:39 +000042 if (!FCache) {
43 // If we haven't already looked up this function, check to see if the
44 // program already contains a function with this name.
45 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattnerb76efb72007-01-07 08:12:01 +000046 // Get or insert the definition now.
47 std::vector<const Type *> ParamTys;
48 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
49 ParamTys.push_back((*I)->getType());
50 FCache = M->getOrInsertFunction(NewFn,
51 FunctionType::get(RetTy, ParamTys, false));
Chris Lattner588e72d2004-02-15 22:16:39 +000052 }
Chris Lattner588e72d2004-02-15 22:16:39 +000053
Chris Lattnerb76efb72007-01-07 08:12:01 +000054 std::vector<Value*> Operands(ArgBegin, ArgEnd);
55 CallInst *NewCI = new CallInst(FCache, Operands, CI->getName(), CI);
56 if (!CI->use_empty())
57 CI->replaceAllUsesWith(NewCI);
Chris Lattner02348ca2004-06-11 02:54:02 +000058 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000059}
60
Chris Lattnerb71fd782006-11-15 18:00:10 +000061void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattner0979ca72004-05-09 04:29:57 +000062 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
63 if (I->isExternal() && !I->use_empty())
64 switch (I->getIntrinsicID()) {
65 default: break;
66 case Intrinsic::setjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000067 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Reid Spencer47857812006-12-31 05:55:36 +000068 Type::Int32Ty);
Chris Lattner0979ca72004-05-09 04:29:57 +000069 break;
70 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000071 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
72 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000073 break;
74 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000075 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
76 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000077 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000078 case Intrinsic::memcpy_i32:
79 case Intrinsic::memcpy_i64:
Chris Lattnere4d5c442005-03-15 04:54:21 +000080 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
81 I->arg_begin()->getType());
Chris Lattner0979ca72004-05-09 04:29:57 +000082 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000083 case Intrinsic::memmove_i32:
84 case Intrinsic::memmove_i64:
Chris Lattnere4d5c442005-03-15 04:54:21 +000085 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
86 I->arg_begin()->getType());
Chris Lattner0979ca72004-05-09 04:29:57 +000087 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000088 case Intrinsic::memset_i32:
89 case Intrinsic::memset_i64:
Reid Spencer47857812006-12-31 05:55:36 +000090 M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty),
91 PointerType::get(Type::Int8Ty),
92 Type::Int32Ty, (--(--I->arg_end()))->getType(),
Jeff Cohen66c5fd62005-10-23 04:37:20 +000093 (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +000094 break;
Reid Spencer0b118202006-01-16 21:12:35 +000095 case Intrinsic::sqrt_f32:
96 case Intrinsic::sqrt_f64:
Alkis Evlogimenosb1beff02005-04-30 07:13:31 +000097 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner1f243e92005-05-08 19:46:29 +000098 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
99 Type::FloatTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000100 else
Chris Lattner1f243e92005-05-08 19:46:29 +0000101 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
102 Type::DoubleTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000103 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000104 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000105}
Chris Lattner588e72d2004-02-15 22:16:39 +0000106
Nate Begemane5981812006-01-16 07:57:00 +0000107/// LowerBSWAP - Emit the code to lower bswap of V before the specified
108/// instruction IP.
109static Value *LowerBSWAP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000110 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
Nate Begemane5981812006-01-16 07:57:00 +0000111
Nate Begemane5981812006-01-16 07:57:00 +0000112 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
113
114 switch(BitSize) {
115 default: assert(0 && "Unhandled type size of value to byteswap!");
116 case 16: {
117 Value *Tmp1 = new ShiftInst(Instruction::Shl, V,
Reid Spencer47857812006-12-31 05:55:36 +0000118 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000119 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
Reid Spencer47857812006-12-31 05:55:36 +0000120 ConstantInt::get(Type::Int8Ty,8),"bswap.1",IP);
Nate Begemane5981812006-01-16 07:57:00 +0000121 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
122 break;
123 }
124 case 32: {
125 Value *Tmp4 = new ShiftInst(Instruction::Shl, V,
Reid Spencer47857812006-12-31 05:55:36 +0000126 ConstantInt::get(Type::Int8Ty,24),"bswap.4", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000127 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
Reid Spencer47857812006-12-31 05:55:36 +0000128 ConstantInt::get(Type::Int8Ty,8),"bswap.3",IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000129 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
Reid Spencer47857812006-12-31 05:55:36 +0000130 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000131 Value *Tmp1 = new ShiftInst(Instruction::LShr, V,
Reid Spencer47857812006-12-31 05:55:36 +0000132 ConstantInt::get(Type::Int8Ty,24),"bswap.1", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000133 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000134 ConstantInt::get(Type::Int32Ty, 0xFF0000),
Nate Begemane5981812006-01-16 07:57:00 +0000135 "bswap.and3", IP);
136 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000137 ConstantInt::get(Type::Int32Ty, 0xFF00),
Nate Begemane5981812006-01-16 07:57:00 +0000138 "bswap.and2", IP);
139 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
140 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
141 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
142 break;
143 }
144 case 64: {
145 Value *Tmp8 = new ShiftInst(Instruction::Shl, V,
Reid Spencer47857812006-12-31 05:55:36 +0000146 ConstantInt::get(Type::Int8Ty,56),"bswap.8", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000147 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
Reid Spencer47857812006-12-31 05:55:36 +0000148 ConstantInt::get(Type::Int8Ty,40),"bswap.7", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000149 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
Reid Spencer47857812006-12-31 05:55:36 +0000150 ConstantInt::get(Type::Int8Ty,24),"bswap.6", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000151 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
Reid Spencer47857812006-12-31 05:55:36 +0000152 ConstantInt::get(Type::Int8Ty,8),"bswap.5", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000153 Value* Tmp4 = new ShiftInst(Instruction::LShr, V,
Reid Spencer47857812006-12-31 05:55:36 +0000154 ConstantInt::get(Type::Int8Ty,8),"bswap.4", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000155 Value* Tmp3 = new ShiftInst(Instruction::LShr, V,
Reid Spencer47857812006-12-31 05:55:36 +0000156 ConstantInt::get(Type::Int8Ty,24),"bswap.3", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000157 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
Reid Spencer47857812006-12-31 05:55:36 +0000158 ConstantInt::get(Type::Int8Ty,40),"bswap.2", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000159 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
Reid Spencer47857812006-12-31 05:55:36 +0000160 ConstantInt::get(Type::Int8Ty,56),"bswap.1", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000161 Tmp7 = BinaryOperator::createAnd(Tmp7,
Reid Spencer47857812006-12-31 05:55:36 +0000162 ConstantInt::get(Type::Int64Ty,
Reid Spencerb83eb642006-10-20 07:07:24 +0000163 0xFF000000000000ULL),
164 "bswap.and7", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000165 Tmp6 = BinaryOperator::createAnd(Tmp6,
Reid Spencer47857812006-12-31 05:55:36 +0000166 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000167 "bswap.and6", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000168 Tmp5 = BinaryOperator::createAnd(Tmp5,
Reid Spencer47857812006-12-31 05:55:36 +0000169 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000170 "bswap.and5", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000171 Tmp4 = BinaryOperator::createAnd(Tmp4,
Reid Spencer47857812006-12-31 05:55:36 +0000172 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000173 "bswap.and4", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000174 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000175 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000176 "bswap.and3", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000177 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000178 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000179 "bswap.and2", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000180 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
181 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
182 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
183 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
184 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
185 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
186 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
187 break;
188 }
189 }
Nate Begemane5981812006-01-16 07:57:00 +0000190 return V;
191}
192
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000193/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000194/// instruction IP.
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000195static Value *LowerCTPOP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000196 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000197
198 static const uint64_t MaskValues[6] = {
199 0x5555555555555555ULL, 0x3333333333333333ULL,
200 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
201 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
202 };
203
Chris Lattner98cf45b2005-05-11 20:24:12 +0000204 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Reid Spencer3822ff52006-11-08 06:47:33 +0000205
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000206 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
Chris Lattner3bb7e3f2006-12-12 05:19:46 +0000207 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000208 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000209 Value *VShift = new ShiftInst(Instruction::LShr, V,
Reid Spencer47857812006-12-31 05:55:36 +0000210 ConstantInt::get(Type::Int8Ty, i), "ctpop.sh", IP);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000211 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
212 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
213 }
214
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000215 return V;
216}
217
Chris Lattner98cf45b2005-05-11 20:24:12 +0000218/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000219/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000220static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000221
222 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
223 for (unsigned i = 1; i != BitSize; i <<= 1) {
Reid Spencer47857812006-12-31 05:55:36 +0000224 Value *ShVal = ConstantInt::get(Type::Int8Ty, i);
Reid Spencer3822ff52006-11-08 06:47:33 +0000225 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000226 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
227 }
228
Chris Lattner98cf45b2005-05-11 20:24:12 +0000229 V = BinaryOperator::createNot(V, "", IP);
230 return LowerCTPOP(V, IP);
231}
232
Nate Begemane5981812006-01-16 07:57:00 +0000233
234
Chris Lattnerb71fd782006-11-15 18:00:10 +0000235void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000236 Function *Callee = CI->getCalledFunction();
237 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000238
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000239 switch (Callee->getIntrinsicID()) {
240 case Intrinsic::not_intrinsic:
Bill Wendlinge8156192006-12-07 01:30:32 +0000241 cerr << "Cannot lower a call to a non-intrinsic function '"
242 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000243 abort();
244 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000245 cerr << "Error: Code generator does not support intrinsic function '"
246 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000247 abort();
248
Chris Lattner588e72d2004-02-15 22:16:39 +0000249 // The setjmp/longjmp intrinsics should only exist in the code if it was
250 // never optimized (ie, right out of the CFE), or if it has been hacked on
251 // by the lowerinvoke pass. In both cases, the right thing to do is to
252 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000253 case Intrinsic::setjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000254 static Constant *SetjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000255 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000256 Type::Int32Ty, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000257 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000258 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000259 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000260 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000261 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000262 if (CI->getType() != Type::VoidTy)
263 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
264 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000265
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000266 case Intrinsic::longjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000267 static Constant *LongjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000268 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000269 Type::VoidTy, LongjmpFCache);
Chris Lattner9b700f72004-02-15 22:24:51 +0000270 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000271 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000272
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000273 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000274 // Insert the call to abort
Chris Lattnerb76efb72007-01-07 08:12:01 +0000275 static Constant *AbortFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000276 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000277 Type::VoidTy, AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000278 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000279 }
Reid Spencer0b118202006-01-16 21:12:35 +0000280 case Intrinsic::ctpop_i8:
281 case Intrinsic::ctpop_i16:
282 case Intrinsic::ctpop_i32:
283 case Intrinsic::ctpop_i64:
284 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
285 break;
286
Nate Begemane5981812006-01-16 07:57:00 +0000287 case Intrinsic::bswap_i16:
288 case Intrinsic::bswap_i32:
289 case Intrinsic::bswap_i64:
290 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
291 break;
292
Reid Spencer0b118202006-01-16 21:12:35 +0000293 case Intrinsic::ctlz_i8:
294 case Intrinsic::ctlz_i16:
295 case Intrinsic::ctlz_i32:
296 case Intrinsic::ctlz_i64:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000297 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000298 break;
Nate Begemane5981812006-01-16 07:57:00 +0000299
Reid Spencer0b118202006-01-16 21:12:35 +0000300 case Intrinsic::cttz_i8:
301 case Intrinsic::cttz_i16:
302 case Intrinsic::cttz_i32:
303 case Intrinsic::cttz_i64: {
Chris Lattnera8011722005-05-11 20:02:14 +0000304 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000305 Value *Src = CI->getOperand(1);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000306 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnera8011722005-05-11 20:02:14 +0000307 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
308 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
309 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000310 CI->replaceAllUsesWith(Src);
311 break;
312 }
Chris Lattner77b13302004-01-05 05:36:30 +0000313
Chris Lattner0c067bc2006-01-13 02:22:08 +0000314 case Intrinsic::stacksave:
315 case Intrinsic::stackrestore: {
316 static bool Warned = false;
317 if (!Warned)
Bill Wendlinge8156192006-12-07 01:30:32 +0000318 cerr << "WARNING: this target does not support the llvm.stack"
319 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
320 "save" : "restore") << " intrinsic.\n";
Chris Lattner0c067bc2006-01-13 02:22:08 +0000321 Warned = true;
322 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
323 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
324 break;
325 }
326
Chris Lattnercf899082004-02-14 02:47:17 +0000327 case Intrinsic::returnaddress:
328 case Intrinsic::frameaddress:
Bill Wendlinge8156192006-12-07 01:30:32 +0000329 cerr << "WARNING: this target does not support the llvm."
330 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
331 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000332 CI->replaceAllUsesWith(ConstantPointerNull::get(
333 cast<PointerType>(CI->getType())));
334 break;
335
Chris Lattner0942b7c2005-02-28 19:27:23 +0000336 case Intrinsic::prefetch:
337 break; // Simply strip out prefetches on unsupported architectures
338
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000339 case Intrinsic::pcmarker:
340 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000341 case Intrinsic::readcyclecounter: {
Bill Wendlinge8156192006-12-07 01:30:32 +0000342 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
343 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencer47857812006-12-31 05:55:36 +0000344 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000345 break;
346 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000347
Chris Lattner77b13302004-01-05 05:36:30 +0000348 case Intrinsic::dbg_stoppoint:
349 case Intrinsic::dbg_region_start:
350 case Intrinsic::dbg_region_end:
351 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000352 case Intrinsic::dbg_declare:
Chris Lattner77b13302004-01-05 05:36:30 +0000353 break; // Simply strip out debugging intrinsics
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000354
Reid Spencer3da59db2006-11-27 01:05:10 +0000355 case Intrinsic::memcpy_i32: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000356 static Constant *MemcpyFCache = 0;
Chris Lattner588e72d2004-02-15 22:16:39 +0000357 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattnerb76efb72007-01-07 08:12:01 +0000358 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000359 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000360 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000361 case Intrinsic::memcpy_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000362 static Constant *MemcpyFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000363 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattnerb76efb72007-01-07 08:12:01 +0000364 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000365 break;
366 }
367 case Intrinsic::memmove_i32: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000368 static Constant *MemmoveFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000369 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattnerb76efb72007-01-07 08:12:01 +0000370 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000371 break;
372 }
Chris Lattner03dd4652006-03-03 00:00:25 +0000373 case Intrinsic::memmove_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000374 static Constant *MemmoveFCache = 0;
Chris Lattner588e72d2004-02-15 22:16:39 +0000375 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattnerb76efb72007-01-07 08:12:01 +0000376 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000377 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000378 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000379 case Intrinsic::memset_i32: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000380 static Constant *MemsetFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000381 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattnerb76efb72007-01-07 08:12:01 +0000382 (*(CI->op_begin()+1))->getType(), MemsetFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000383 }
Chris Lattner03dd4652006-03-03 00:00:25 +0000384 case Intrinsic::memset_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000385 static Constant *MemsetFCache = 0;
Chris Lattner588e72d2004-02-15 22:16:39 +0000386 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattnerb76efb72007-01-07 08:12:01 +0000387 (*(CI->op_begin()+1))->getType(), MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000388 break;
389 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000390 case Intrinsic::sqrt_f32: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000391 static Constant *sqrtfFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000392 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000393 Type::FloatTy, sqrtfFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000394 break;
395 }
396 case Intrinsic::sqrt_f64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000397 static Constant *sqrtFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000398 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000399 Type::DoubleTy, sqrtFCache);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000400 break;
401 }
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000402 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000403
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000404 assert(CI->use_empty() &&
405 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000406 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000407}