blob: 6db44c4f5ffe0deee76046b8ad3adb892a55e0e2 [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>
24static Function *EnsureFunctionExists(Module &M, const char *Name,
25 ArgIt ArgBegin, ArgIt ArgEnd,
26 const Type *RetTy) {
27 if (Function *F = M.getNamedFunction(Name)) return F;
28 // It doesn't already exist in the program, insert a new definition now.
29 std::vector<const Type *> ParamTys;
30 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
31 ParamTys.push_back(I->getType());
32 return M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
33}
34
35/// 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,
Reid Spencer6156b022006-12-21 08:28:31 +000041 ArgIt ArgBegin, ArgIt ArgEnd, bool isSigned,
Chris Lattnerbcdadf32004-06-20 07:49:54 +000042 const Type *RetTy, Function *&FCache) {
43 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();
47 FCache = M->getNamedFunction(NewFn);
48 if (!FCache) {
49 // It doesn't already exist in the program, insert a new definition now.
50 std::vector<const Type *> ParamTys;
51 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
52 ParamTys.push_back((*I)->getType());
53 FCache = M->getOrInsertFunction(NewFn,
54 FunctionType::get(RetTy, ParamTys, false));
55 }
56 }
57
58 const FunctionType *FT = FCache->getFunctionType();
59 std::vector<Value*> Operands;
60 unsigned ArgNo = 0;
Reid Spencer6156b022006-12-21 08:28:31 +000061 for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams();
Chris Lattnerbcdadf32004-06-20 07:49:54 +000062 ++I, ++ArgNo) {
63 Value *Arg = *I;
Reid Spencer6156b022006-12-21 08:28:31 +000064 if (Arg->getType() != FT->getParamType(ArgNo)) {
65 Instruction::CastOps opcode = CastInst::getCastOpcode(Arg, isSigned,
66 FT->getParamType(ArgNo), isSigned);
67 Arg = CastInst::create(opcode, Arg, FT->getParamType(ArgNo),
68 Arg->getName(), CI);
69 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +000070 Operands.push_back(Arg);
71 }
72 // Pass nulls into any additional arguments...
73 for (; ArgNo != FT->getNumParams(); ++ArgNo)
74 Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
75
76 std::string Name = CI->getName(); CI->setName("");
77 if (FT->getReturnType() == Type::VoidTy) Name.clear();
78 CallInst *NewCI = new CallInst(FCache, Operands, Name, CI);
79 if (!CI->use_empty()) {
80 Value *V = NewCI;
Reid Spencer668d90f2006-12-18 08:47:13 +000081 if (CI->getType() != NewCI->getType()) {
Reid Spencer6156b022006-12-21 08:28:31 +000082 Instruction::CastOps opcode = CastInst::getCastOpcode(NewCI, isSigned,
83 CI->getType(), isSigned);
Reid Spencer668d90f2006-12-18 08:47:13 +000084 V = CastInst::create(opcode, NewCI, CI->getType(), Name, CI);
85 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +000086 CI->replaceAllUsesWith(V);
87 }
88 return NewCI;
89}
90
Chris Lattner2775aba2006-11-15 18:00:10 +000091void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattnerbcdadf32004-06-20 07:49:54 +000092 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
93 if (I->isExternal() && !I->use_empty())
94 switch (I->getIntrinsicID()) {
95 default: break;
96 case Intrinsic::setjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +000097 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
98 Type::IntTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000099 break;
100 case Intrinsic::longjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +0000101 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
102 Type::VoidTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000103 break;
104 case Intrinsic::siglongjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +0000105 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
106 Type::VoidTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000107 break;
Chris Lattner093c1592006-03-03 00:00:25 +0000108 case Intrinsic::memcpy_i32:
109 case Intrinsic::memcpy_i64:
Chris Lattner531f9e92005-03-15 04:54:21 +0000110 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
111 I->arg_begin()->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000112 break;
Chris Lattner093c1592006-03-03 00:00:25 +0000113 case Intrinsic::memmove_i32:
114 case Intrinsic::memmove_i64:
Chris Lattner531f9e92005-03-15 04:54:21 +0000115 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
116 I->arg_begin()->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000117 break;
Chris Lattner093c1592006-03-03 00:00:25 +0000118 case Intrinsic::memset_i32:
119 case Intrinsic::memset_i64:
Chris Lattner9acd3142005-05-08 19:46:29 +0000120 M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
121 PointerType::get(Type::SByteTy),
Jeff Cohen11e26b52005-10-23 04:37:20 +0000122 Type::IntTy, (--(--I->arg_end()))->getType(),
123 (Type *)0);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000124 break;
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000125 case Intrinsic::isunordered_f32:
126 case Intrinsic::isunordered_f64:
Chris Lattner9acd3142005-05-08 19:46:29 +0000127 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
128 Type::BoolTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000129 break;
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000130 case Intrinsic::sqrt_f32:
131 case Intrinsic::sqrt_f64:
Alkis Evlogimenosd7e534b2005-04-30 07:13:31 +0000132 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner9acd3142005-05-08 19:46:29 +0000133 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
134 Type::FloatTy);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000135 else
Chris Lattner9acd3142005-05-08 19:46:29 +0000136 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
137 Type::DoubleTy);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000138 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000139 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000140}
141
Nate Begeman7d831fa2006-01-16 07:57:00 +0000142/// LowerBSWAP - Emit the code to lower bswap of V before the specified
143/// instruction IP.
144static Value *LowerBSWAP(Value *V, Instruction *IP) {
145 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
146
Nate Begeman7d831fa2006-01-16 07:57:00 +0000147 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
148
149 switch(BitSize) {
150 default: assert(0 && "Unhandled type size of value to byteswap!");
151 case 16: {
152 Value *Tmp1 = new ShiftInst(Instruction::Shl, V,
153 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000154 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
Nate Begeman7d831fa2006-01-16 07:57:00 +0000155 ConstantInt::get(Type::UByteTy,8),"bswap.1",IP);
156 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
157 break;
158 }
159 case 32: {
160 Value *Tmp4 = new ShiftInst(Instruction::Shl, V,
161 ConstantInt::get(Type::UByteTy,24),"bswap.4", IP);
162 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
Reid Spencerfdff9382006-11-08 06:47:33 +0000163 ConstantInt::get(Type::UByteTy,8),"bswap.3",IP);
164 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
165 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
166 Value *Tmp1 = new ShiftInst(Instruction::LShr, V,
Nate Begeman7d831fa2006-01-16 07:57:00 +0000167 ConstantInt::get(Type::UByteTy,24),"bswap.1", IP);
168 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000169 ConstantInt::get(Type::UIntTy, 0xFF0000),
Nate Begeman7d831fa2006-01-16 07:57:00 +0000170 "bswap.and3", IP);
171 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000172 ConstantInt::get(Type::UIntTy, 0xFF00),
Nate Begeman7d831fa2006-01-16 07:57:00 +0000173 "bswap.and2", IP);
174 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
175 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
176 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
177 break;
178 }
179 case 64: {
180 Value *Tmp8 = new ShiftInst(Instruction::Shl, V,
181 ConstantInt::get(Type::UByteTy,56),"bswap.8", IP);
182 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
183 ConstantInt::get(Type::UByteTy,40),"bswap.7", IP);
184 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
185 ConstantInt::get(Type::UByteTy,24),"bswap.6", IP);
186 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
Reid Spencerfdff9382006-11-08 06:47:33 +0000187 ConstantInt::get(Type::UByteTy,8),"bswap.5", IP);
188 Value* Tmp4 = new ShiftInst(Instruction::LShr, V,
189 ConstantInt::get(Type::UByteTy,8),"bswap.4", IP);
190 Value* Tmp3 = new ShiftInst(Instruction::LShr, V,
Nate Begeman7d831fa2006-01-16 07:57:00 +0000191 ConstantInt::get(Type::UByteTy,24),"bswap.3", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000192 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
Nate Begeman7d831fa2006-01-16 07:57:00 +0000193 ConstantInt::get(Type::UByteTy,40),"bswap.2", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000194 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
Nate Begeman7d831fa2006-01-16 07:57:00 +0000195 ConstantInt::get(Type::UByteTy,56),"bswap.1", IP);
196 Tmp7 = BinaryOperator::createAnd(Tmp7,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000197 ConstantInt::get(Type::ULongTy,
198 0xFF000000000000ULL),
199 "bswap.and7", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000200 Tmp6 = BinaryOperator::createAnd(Tmp6,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000201 ConstantInt::get(Type::ULongTy, 0xFF0000000000ULL),
202 "bswap.and6", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000203 Tmp5 = BinaryOperator::createAnd(Tmp5,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000204 ConstantInt::get(Type::ULongTy, 0xFF00000000ULL),
205 "bswap.and5", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000206 Tmp4 = BinaryOperator::createAnd(Tmp4,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000207 ConstantInt::get(Type::ULongTy, 0xFF000000ULL),
208 "bswap.and4", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000209 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000210 ConstantInt::get(Type::ULongTy, 0xFF0000ULL),
211 "bswap.and3", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000212 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000213 ConstantInt::get(Type::ULongTy, 0xFF00ULL),
214 "bswap.and2", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000215 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
216 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
217 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
218 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
219 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
220 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
221 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
222 break;
223 }
224 }
Nate Begeman7d831fa2006-01-16 07:57:00 +0000225 return V;
226}
227
Chris Lattner9ec975a2005-05-11 19:42:05 +0000228/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begeman7d831fa2006-01-16 07:57:00 +0000229/// instruction IP.
Chris Lattner9ec975a2005-05-11 19:42:05 +0000230static Value *LowerCTPOP(Value *V, Instruction *IP) {
231 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000232
233 static const uint64_t MaskValues[6] = {
234 0x5555555555555555ULL, 0x3333333333333333ULL,
235 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
236 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
237 };
238
Chris Lattner991ce362005-05-11 20:24:12 +0000239 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Reid Spencerfdff9382006-11-08 06:47:33 +0000240
Chris Lattner9ec975a2005-05-11 19:42:05 +0000241 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
Chris Lattner6801f022006-12-12 05:19:46 +0000242 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Chris Lattner9ec975a2005-05-11 19:42:05 +0000243 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000244 Value *VShift = new ShiftInst(Instruction::LShr, V,
Chris Lattner9ec975a2005-05-11 19:42:05 +0000245 ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
246 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
247 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
248 }
249
Chris Lattner9ec975a2005-05-11 19:42:05 +0000250 return V;
251}
252
Chris Lattner991ce362005-05-11 20:24:12 +0000253/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begeman7d831fa2006-01-16 07:57:00 +0000254/// instruction IP.
Chris Lattner991ce362005-05-11 20:24:12 +0000255static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner991ce362005-05-11 20:24:12 +0000256
257 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
258 for (unsigned i = 1; i != BitSize; i <<= 1) {
259 Value *ShVal = ConstantInt::get(Type::UByteTy, i);
Reid Spencerfdff9382006-11-08 06:47:33 +0000260 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
Chris Lattner991ce362005-05-11 20:24:12 +0000261 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
262 }
263
Chris Lattner991ce362005-05-11 20:24:12 +0000264 V = BinaryOperator::createNot(V, "", IP);
265 return LowerCTPOP(V, IP);
266}
267
Nate Begeman7d831fa2006-01-16 07:57:00 +0000268
269
Chris Lattner2775aba2006-11-15 18:00:10 +0000270void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000271 Function *Callee = CI->getCalledFunction();
272 assert(Callee && "Cannot lower an indirect call!");
Misha Brukman835702a2005-04-21 22:36:52 +0000273
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000274 switch (Callee->getIntrinsicID()) {
275 case Intrinsic::not_intrinsic:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000276 cerr << "Cannot lower a call to a non-intrinsic function '"
277 << Callee->getName() << "'!\n";
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000278 abort();
279 default:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000280 cerr << "Error: Code generator does not support intrinsic function '"
281 << Callee->getName() << "'!\n";
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000282 abort();
283
284 // The setjmp/longjmp intrinsics should only exist in the code if it was
285 // never optimized (ie, right out of the CFE), or if it has been hacked on
286 // by the lowerinvoke pass. In both cases, the right thing to do is to
287 // convert the call to an explicit setjmp or longjmp call.
288 case Intrinsic::setjmp: {
289 static Function *SetjmpFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000290 static const unsigned castOpcodes[] = { Instruction::BitCast };
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000291 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000292 castOpcodes, Type::IntTy, SetjmpFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000293 if (CI->getType() != Type::VoidTy)
294 CI->replaceAllUsesWith(V);
295 break;
296 }
Misha Brukman835702a2005-04-21 22:36:52 +0000297 case Intrinsic::sigsetjmp:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000298 if (CI->getType() != Type::VoidTy)
299 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
300 break;
301
302 case Intrinsic::longjmp: {
303 static Function *LongjmpFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000304 static const unsigned castOpcodes[] =
305 { Instruction::BitCast, 0 };
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000306 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000307 castOpcodes, Type::VoidTy, LongjmpFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000308 break;
309 }
310
311 case Intrinsic::siglongjmp: {
312 // Insert the call to abort
313 static Function *AbortFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000314 static const unsigned castOpcodes[] =
315 { Instruction::BitCast, 0 };
316 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
317 castOpcodes, Type::VoidTy, AbortFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000318 break;
319 }
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000320 case Intrinsic::ctpop_i8:
321 case Intrinsic::ctpop_i16:
322 case Intrinsic::ctpop_i32:
323 case Intrinsic::ctpop_i64:
324 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
325 break;
326
Nate Begeman7d831fa2006-01-16 07:57:00 +0000327 case Intrinsic::bswap_i16:
328 case Intrinsic::bswap_i32:
329 case Intrinsic::bswap_i64:
330 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
331 break;
332
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000333 case Intrinsic::ctlz_i8:
334 case Intrinsic::ctlz_i16:
335 case Intrinsic::ctlz_i32:
336 case Intrinsic::ctlz_i64:
Chris Lattner991ce362005-05-11 20:24:12 +0000337 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth5e177822005-05-03 17:19:30 +0000338 break;
Nate Begeman7d831fa2006-01-16 07:57:00 +0000339
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000340 case Intrinsic::cttz_i8:
341 case Intrinsic::cttz_i16:
342 case Intrinsic::cttz_i32:
343 case Intrinsic::cttz_i64: {
Chris Lattnerfe5759b2005-05-11 20:02:14 +0000344 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth5e177822005-05-03 17:19:30 +0000345 Value *Src = CI->getOperand(1);
Chris Lattner9ec975a2005-05-11 19:42:05 +0000346 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnerfe5759b2005-05-11 20:02:14 +0000347 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
348 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
349 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth5e177822005-05-03 17:19:30 +0000350 CI->replaceAllUsesWith(Src);
351 break;
352 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000353
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000354 case Intrinsic::stacksave:
355 case Intrinsic::stackrestore: {
356 static bool Warned = false;
357 if (!Warned)
Bill Wendlingf3baad32006-12-07 01:30:32 +0000358 cerr << "WARNING: this target does not support the llvm.stack"
359 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
360 "save" : "restore") << " intrinsic.\n";
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000361 Warned = true;
362 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
363 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
364 break;
365 }
366
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000367 case Intrinsic::returnaddress:
368 case Intrinsic::frameaddress:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000369 cerr << "WARNING: this target does not support the llvm."
370 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
371 "return" : "frame") << "address intrinsic.\n";
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000372 CI->replaceAllUsesWith(ConstantPointerNull::get(
373 cast<PointerType>(CI->getType())));
374 break;
375
Chris Lattnerc87e03a2005-02-28 19:27:23 +0000376 case Intrinsic::prefetch:
377 break; // Simply strip out prefetches on unsupported architectures
378
Andrew Lenharthb4427912005-03-28 20:05:49 +0000379 case Intrinsic::pcmarker:
380 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000381 case Intrinsic::readcyclecounter: {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000382 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
383 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencere0fc4df2006-10-20 07:07:24 +0000384 CI->replaceAllUsesWith(ConstantInt::get(Type::ULongTy, 0));
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000385 break;
386 }
Andrew Lenharthb4427912005-03-28 20:05:49 +0000387
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000388 case Intrinsic::dbg_stoppoint:
389 case Intrinsic::dbg_region_start:
390 case Intrinsic::dbg_region_end:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000391 case Intrinsic::dbg_func_start:
Jim Laskeya8bdac82006-03-23 18:06:46 +0000392 case Intrinsic::dbg_declare:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000393 break; // Simply strip out debugging intrinsics
394
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000395 case Intrinsic::memcpy_i32: {
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000396 static Function *MemcpyFCache = 0;
397 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer6156b022006-12-21 08:28:31 +0000398 false, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000399 break;
400 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000401 case Intrinsic::memcpy_i64: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000402 static Function *MemcpyFCache = 0;
403 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer6156b022006-12-21 08:28:31 +0000404 false, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000405 break;
406 }
407 case Intrinsic::memmove_i32: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000408 static Function *MemmoveFCache = 0;
409 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer6156b022006-12-21 08:28:31 +0000410 false, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000411 break;
412 }
Chris Lattner093c1592006-03-03 00:00:25 +0000413 case Intrinsic::memmove_i64: {
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000414 static Function *MemmoveFCache = 0;
415 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer6156b022006-12-21 08:28:31 +0000416 false, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000417 break;
418 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000419 case Intrinsic::memset_i32: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000420 static Function *MemsetFCache = 0;
421 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer6156b022006-12-21 08:28:31 +0000422 true, (*(CI->op_begin()+1))->getType(), MemsetFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000423 }
Chris Lattner093c1592006-03-03 00:00:25 +0000424 case Intrinsic::memset_i64: {
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000425 static Function *MemsetFCache = 0;
426 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer6156b022006-12-21 08:28:31 +0000427 true, (*(CI->op_begin()+1))->getType(), MemsetFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000428 break;
429 }
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000430 case Intrinsic::isunordered_f32:
431 case Intrinsic::isunordered_f64: {
Alkis Evlogimenosb3846f42005-03-01 02:07:58 +0000432 Value *L = CI->getOperand(1);
433 Value *R = CI->getOperand(2);
434
435 Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
436 Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
437 CI->replaceAllUsesWith(
438 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
439 "isunordered", CI));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000440 break;
441 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000442 case Intrinsic::sqrt_f32: {
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000443 static Function *sqrtfFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000444 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
Reid Spencer6156b022006-12-21 08:28:31 +0000445 false, Type::FloatTy, sqrtfFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000446 break;
447 }
448 case Intrinsic::sqrt_f64: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000449 static Function *sqrtFCache = 0;
450 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
Reid Spencer6156b022006-12-21 08:28:31 +0000451 false, Type::DoubleTy, sqrtFCache);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000452 break;
453 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000454 }
Misha Brukman835702a2005-04-21 22:36:52 +0000455
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000456 assert(CI->use_empty() &&
457 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000458 CI->eraseFromParent();
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000459}