blob: 72a1d1485b49118e5a2d7039705f915617e881a3 [file] [log] [blame]
Chris Lattnerbcdadf32004-06-20 07:49:54 +00001//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerbcdadf32004-06-20 07:49:54 +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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerbcdadf32004-06-20 07:49:54 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattner2775aba2006-11-15 18:00:10 +000010// This file implements the IntrinsicLowering class.
Chris Lattnerbcdadf32004-06-20 07:49:54 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbcdadf32004-06-20 07:49:54 +000014#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
16#include "llvm/Module.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Andrew Lenharth5e177822005-05-03 17:19:30 +000018#include "llvm/Type.h"
Bill Wendling3f6f0fd2006-11-28 02:08:17 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
20#include "llvm/Support/Streams.h"
Chris Lattnerbcdadf32004-06-20 07:49:54 +000021using namespace llvm;
22
23template <class ArgIt>
Chris Lattner34acba42007-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 Lattnerbcdadf32004-06-20 07:49:54 +000028 std::vector<const Type *> ParamTys;
29 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
30 ParamTys.push_back(I->getType());
Chris Lattner34acba42007-01-07 08:12:01 +000031 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
Chris Lattnerbcdadf32004-06-20 07:49:54 +000032}
33
34/// 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 Lattner34acba42007-01-07 08:12:01 +000040 ArgIt ArgBegin, ArgIt ArgEnd,
41 const Type *RetTy, Constant *&FCache) {
Chris Lattnerbcdadf32004-06-20 07:49:54 +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 Lattner34acba42007-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 Lattnerbcdadf32004-06-20 07:49:54 +000052 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +000053
Chris Lattner34acba42007-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 Lattnerbcdadf32004-06-20 07:49:54 +000058 return NewCI;
59}
60
Chris Lattner2775aba2006-11-15 18:00:10 +000061void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattnerbcdadf32004-06-20 07:49:54 +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 Lattner9acd3142005-05-08 19:46:29 +000067 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Reid Spencere63b6512006-12-31 05:55:36 +000068 Type::Int32Ty);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000069 break;
70 case Intrinsic::longjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +000071 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
72 Type::VoidTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000073 break;
74 case Intrinsic::siglongjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +000075 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
76 Type::VoidTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000077 break;
Chris Lattner093c1592006-03-03 00:00:25 +000078 case Intrinsic::memcpy_i32:
79 case Intrinsic::memcpy_i64:
Chris Lattner531f9e92005-03-15 04:54:21 +000080 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
81 I->arg_begin()->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +000082 break;
Chris Lattner093c1592006-03-03 00:00:25 +000083 case Intrinsic::memmove_i32:
84 case Intrinsic::memmove_i64:
Chris Lattner531f9e92005-03-15 04:54:21 +000085 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
86 I->arg_begin()->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +000087 break;
Chris Lattner093c1592006-03-03 00:00:25 +000088 case Intrinsic::memset_i32:
89 case Intrinsic::memset_i64:
Reid Spencere63b6512006-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 Cohen11e26b52005-10-23 04:37:20 +000093 (Type *)0);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000094 break;
Reid Spencerb4f9a6f2006-01-16 21:12:35 +000095 case Intrinsic::sqrt_f32:
96 case Intrinsic::sqrt_f64:
Alkis Evlogimenosd7e534b2005-04-30 07:13:31 +000097 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner9acd3142005-05-08 19:46:29 +000098 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
99 Type::FloatTy);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000100 else
Chris Lattner9acd3142005-05-08 19:46:29 +0000101 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
102 Type::DoubleTy);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000103 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000104 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000105}
106
Nate Begeman7d831fa2006-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 Lattner03c49532007-01-15 02:27:26 +0000110 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
Nate Begeman7d831fa2006-01-16 07:57:00 +0000111
Nate Begeman7d831fa2006-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 Spencere63b6512006-12-31 05:55:36 +0000118 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000119 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000120 ConstantInt::get(Type::Int8Ty,8),"bswap.1",IP);
Nate Begeman7d831fa2006-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 Spencere63b6512006-12-31 05:55:36 +0000126 ConstantInt::get(Type::Int8Ty,24),"bswap.4", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000127 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000128 ConstantInt::get(Type::Int8Ty,8),"bswap.3",IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000129 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000130 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000131 Value *Tmp1 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000132 ConstantInt::get(Type::Int8Ty,24),"bswap.1", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000133 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencere63b6512006-12-31 05:55:36 +0000134 ConstantInt::get(Type::Int32Ty, 0xFF0000),
Nate Begeman7d831fa2006-01-16 07:57:00 +0000135 "bswap.and3", IP);
136 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencere63b6512006-12-31 05:55:36 +0000137 ConstantInt::get(Type::Int32Ty, 0xFF00),
Nate Begeman7d831fa2006-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 Spencere63b6512006-12-31 05:55:36 +0000146 ConstantInt::get(Type::Int8Ty,56),"bswap.8", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000147 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000148 ConstantInt::get(Type::Int8Ty,40),"bswap.7", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000149 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000150 ConstantInt::get(Type::Int8Ty,24),"bswap.6", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000151 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000152 ConstantInt::get(Type::Int8Ty,8),"bswap.5", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000153 Value* Tmp4 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000154 ConstantInt::get(Type::Int8Ty,8),"bswap.4", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000155 Value* Tmp3 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000156 ConstantInt::get(Type::Int8Ty,24),"bswap.3", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000157 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000158 ConstantInt::get(Type::Int8Ty,40),"bswap.2", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000159 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000160 ConstantInt::get(Type::Int8Ty,56),"bswap.1", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000161 Tmp7 = BinaryOperator::createAnd(Tmp7,
Reid Spencere63b6512006-12-31 05:55:36 +0000162 ConstantInt::get(Type::Int64Ty,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000163 0xFF000000000000ULL),
164 "bswap.and7", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000165 Tmp6 = BinaryOperator::createAnd(Tmp6,
Reid Spencere63b6512006-12-31 05:55:36 +0000166 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000167 "bswap.and6", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000168 Tmp5 = BinaryOperator::createAnd(Tmp5,
Reid Spencere63b6512006-12-31 05:55:36 +0000169 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000170 "bswap.and5", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000171 Tmp4 = BinaryOperator::createAnd(Tmp4,
Reid Spencere63b6512006-12-31 05:55:36 +0000172 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000173 "bswap.and4", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000174 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencere63b6512006-12-31 05:55:36 +0000175 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000176 "bswap.and3", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000177 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencere63b6512006-12-31 05:55:36 +0000178 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000179 "bswap.and2", IP);
Nate Begeman7d831fa2006-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 Begeman7d831fa2006-01-16 07:57:00 +0000190 return V;
191}
192
Chris Lattner9ec975a2005-05-11 19:42:05 +0000193/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begeman7d831fa2006-01-16 07:57:00 +0000194/// instruction IP.
Chris Lattner9ec975a2005-05-11 19:42:05 +0000195static Value *LowerCTPOP(Value *V, Instruction *IP) {
Chris Lattner03c49532007-01-15 02:27:26 +0000196 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner9ec975a2005-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 Lattner991ce362005-05-11 20:24:12 +0000204 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Reid Spencerfdff9382006-11-08 06:47:33 +0000205
Chris Lattner9ec975a2005-05-11 19:42:05 +0000206 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
Chris Lattner6801f022006-12-12 05:19:46 +0000207 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Chris Lattner9ec975a2005-05-11 19:42:05 +0000208 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000209 Value *VShift = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000210 ConstantInt::get(Type::Int8Ty, i), "ctpop.sh", IP);
Chris Lattner9ec975a2005-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 Lattner9ec975a2005-05-11 19:42:05 +0000215 return V;
216}
217
Chris Lattner991ce362005-05-11 20:24:12 +0000218/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begeman7d831fa2006-01-16 07:57:00 +0000219/// instruction IP.
Chris Lattner991ce362005-05-11 20:24:12 +0000220static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner991ce362005-05-11 20:24:12 +0000221
222 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
223 for (unsigned i = 1; i != BitSize; i <<= 1) {
Reid Spencere63b6512006-12-31 05:55:36 +0000224 Value *ShVal = ConstantInt::get(Type::Int8Ty, i);
Reid Spencerfdff9382006-11-08 06:47:33 +0000225 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
Chris Lattner991ce362005-05-11 20:24:12 +0000226 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
227 }
228
Chris Lattner991ce362005-05-11 20:24:12 +0000229 V = BinaryOperator::createNot(V, "", IP);
230 return LowerCTPOP(V, IP);
231}
232
Nate Begeman7d831fa2006-01-16 07:57:00 +0000233
234
Chris Lattner2775aba2006-11-15 18:00:10 +0000235void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000236 Function *Callee = CI->getCalledFunction();
237 assert(Callee && "Cannot lower an indirect call!");
Misha Brukman835702a2005-04-21 22:36:52 +0000238
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000239 switch (Callee->getIntrinsicID()) {
240 case Intrinsic::not_intrinsic:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000241 cerr << "Cannot lower a call to a non-intrinsic function '"
242 << Callee->getName() << "'!\n";
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000243 abort();
244 default:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000245 cerr << "Error: Code generator does not support intrinsic function '"
246 << Callee->getName() << "'!\n";
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000247 abort();
248
249 // 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.
253 case Intrinsic::setjmp: {
Chris Lattner34acba42007-01-07 08:12:01 +0000254 static Constant *SetjmpFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000255 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000256 Type::Int32Ty, SetjmpFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000257 if (CI->getType() != Type::VoidTy)
258 CI->replaceAllUsesWith(V);
259 break;
260 }
Misha Brukman835702a2005-04-21 22:36:52 +0000261 case Intrinsic::sigsetjmp:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000262 if (CI->getType() != Type::VoidTy)
263 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
264 break;
265
266 case Intrinsic::longjmp: {
Chris Lattner34acba42007-01-07 08:12:01 +0000267 static Constant *LongjmpFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000268 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000269 Type::VoidTy, LongjmpFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000270 break;
271 }
272
273 case Intrinsic::siglongjmp: {
274 // Insert the call to abort
Chris Lattner34acba42007-01-07 08:12:01 +0000275 static Constant *AbortFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000276 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000277 Type::VoidTy, AbortFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000278 break;
279 }
Reid Spencerb4f9a6f2006-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 Begeman7d831fa2006-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 Spencerb4f9a6f2006-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 Lattner991ce362005-05-11 20:24:12 +0000297 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth5e177822005-05-03 17:19:30 +0000298 break;
Nate Begeman7d831fa2006-01-16 07:57:00 +0000299
Reid Spencerb4f9a6f2006-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 Lattnerfe5759b2005-05-11 20:02:14 +0000304 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth5e177822005-05-03 17:19:30 +0000305 Value *Src = CI->getOperand(1);
Chris Lattner9ec975a2005-05-11 19:42:05 +0000306 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnerfe5759b2005-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 Lenharth5e177822005-05-03 17:19:30 +0000310 CI->replaceAllUsesWith(Src);
311 break;
312 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000313
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000314 case Intrinsic::stacksave:
315 case Intrinsic::stackrestore: {
316 static bool Warned = false;
317 if (!Warned)
Bill Wendlingf3baad32006-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 Lattner3b2b0af2006-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 Lattnerbcdadf32004-06-20 07:49:54 +0000327 case Intrinsic::returnaddress:
328 case Intrinsic::frameaddress:
Bill Wendlingf3baad32006-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 Lattnerbcdadf32004-06-20 07:49:54 +0000332 CI->replaceAllUsesWith(ConstantPointerNull::get(
333 cast<PointerType>(CI->getType())));
334 break;
335
Chris Lattnerc87e03a2005-02-28 19:27:23 +0000336 case Intrinsic::prefetch:
337 break; // Simply strip out prefetches on unsupported architectures
338
Andrew Lenharthb4427912005-03-28 20:05:49 +0000339 case Intrinsic::pcmarker:
340 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000341 case Intrinsic::readcyclecounter: {
Bill Wendlingf3baad32006-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 Spencere63b6512006-12-31 05:55:36 +0000344 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000345 break;
346 }
Andrew Lenharthb4427912005-03-28 20:05:49 +0000347
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000348 case Intrinsic::dbg_stoppoint:
349 case Intrinsic::dbg_region_start:
350 case Intrinsic::dbg_region_end:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000351 case Intrinsic::dbg_func_start:
Jim Laskeya8bdac82006-03-23 18:06:46 +0000352 case Intrinsic::dbg_declare:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000353 break; // Simply strip out debugging intrinsics
354
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000355 case Intrinsic::memcpy_i32: {
Chris Lattner34acba42007-01-07 08:12:01 +0000356 static Constant *MemcpyFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000357 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000358 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000359 break;
360 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000361 case Intrinsic::memcpy_i64: {
Chris Lattner34acba42007-01-07 08:12:01 +0000362 static Constant *MemcpyFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000363 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000364 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000365 break;
366 }
367 case Intrinsic::memmove_i32: {
Chris Lattner34acba42007-01-07 08:12:01 +0000368 static Constant *MemmoveFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000369 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000370 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000371 break;
372 }
Chris Lattner093c1592006-03-03 00:00:25 +0000373 case Intrinsic::memmove_i64: {
Chris Lattner34acba42007-01-07 08:12:01 +0000374 static Constant *MemmoveFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000375 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000376 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000377 break;
378 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000379 case Intrinsic::memset_i32: {
Chris Lattner34acba42007-01-07 08:12:01 +0000380 static Constant *MemsetFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000381 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000382 (*(CI->op_begin()+1))->getType(), MemsetFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000383 }
Chris Lattner093c1592006-03-03 00:00:25 +0000384 case Intrinsic::memset_i64: {
Chris Lattner34acba42007-01-07 08:12:01 +0000385 static Constant *MemsetFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000386 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000387 (*(CI->op_begin()+1))->getType(), MemsetFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000388 break;
389 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000390 case Intrinsic::sqrt_f32: {
Chris Lattner34acba42007-01-07 08:12:01 +0000391 static Constant *sqrtfFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000392 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000393 Type::FloatTy, sqrtfFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000394 break;
395 }
396 case Intrinsic::sqrt_f64: {
Chris Lattner34acba42007-01-07 08:12:01 +0000397 static Constant *sqrtFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000398 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000399 Type::DoubleTy, sqrtFCache);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000400 break;
401 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000402 }
Misha Brukman835702a2005-04-21 22:36:52 +0000403
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000404 assert(CI->use_empty() &&
405 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000406 CI->eraseFromParent();
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000407}