blob: 2df839ff390bc14d11d79ba37ec299c3db91e500 [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 Lattner30483732004-06-20 07:49:54 +000014#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnercf899082004-02-14 02:47:17 +000015#include "llvm/Constants.h"
Chris Lattner5fe51cc2004-02-12 17:01:09 +000016#include "llvm/DerivedTypes.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000017#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000019#include "llvm/Type.h"
Reid Spencer954da372004-07-04 12:19:56 +000020#include <iostream>
21
Chris Lattner3b66ecb2003-12-28 08:19:41 +000022using namespace llvm;
23
Chris Lattner0979ca72004-05-09 04:29:57 +000024template <class ArgIt>
25static Function *EnsureFunctionExists(Module &M, const char *Name,
26 ArgIt ArgBegin, ArgIt ArgEnd,
27 const Type *RetTy) {
28 if (Function *F = M.getNamedFunction(Name)) return F;
29 // It doesn't already exist in the program, insert a new definition now.
30 std::vector<const Type *> ParamTys;
31 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
32 ParamTys.push_back(I->getType());
33 return M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
34}
35
Chris Lattner588e72d2004-02-15 22:16:39 +000036/// ReplaceCallWith - This function is used when we want to lower an intrinsic
37/// call to a call of an external function. This handles hard cases such as
38/// when there was already a prototype for the external function, and if that
39/// prototype doesn't match the arguments we expect to pass in.
40template <class ArgIt>
41static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
42 ArgIt ArgBegin, ArgIt ArgEnd,
Reid Spencer3da59db2006-11-27 01:05:10 +000043 const unsigned *castOpcodes,
Chris Lattner588e72d2004-02-15 22:16:39 +000044 const Type *RetTy, Function *&FCache) {
45 if (!FCache) {
46 // If we haven't already looked up this function, check to see if the
47 // program already contains a function with this name.
48 Module *M = CI->getParent()->getParent()->getParent();
49 FCache = M->getNamedFunction(NewFn);
50 if (!FCache) {
51 // It doesn't already exist in the program, insert a new definition now.
52 std::vector<const Type *> ParamTys;
53 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
54 ParamTys.push_back((*I)->getType());
55 FCache = M->getOrInsertFunction(NewFn,
56 FunctionType::get(RetTy, ParamTys, false));
57 }
Chris Lattner0979ca72004-05-09 04:29:57 +000058 }
Chris Lattner588e72d2004-02-15 22:16:39 +000059
60 const FunctionType *FT = FCache->getFunctionType();
61 std::vector<Value*> Operands;
62 unsigned ArgNo = 0;
63 for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams();
64 ++I, ++ArgNo) {
65 Value *Arg = *I;
66 if (Arg->getType() != FT->getParamType(ArgNo))
Reid Spencer3da59db2006-11-27 01:05:10 +000067 if (castOpcodes[ArgNo])
68 Arg = CastInst::create(Instruction::CastOps(castOpcodes[ArgNo]),
69 Arg, FT->getParamType(ArgNo), Arg->getName(), CI);
70 else
71 Arg = CastInst::createInferredCast(Arg, FT->getParamType(ArgNo),
72 Arg->getName(), CI);
Chris Lattner588e72d2004-02-15 22:16:39 +000073 Operands.push_back(Arg);
74 }
75 // Pass nulls into any additional arguments...
76 for (; ArgNo != FT->getNumParams(); ++ArgNo)
77 Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
78
79 std::string Name = CI->getName(); CI->setName("");
80 if (FT->getReturnType() == Type::VoidTy) Name.clear();
Chris Lattner02348ca2004-06-11 02:54:02 +000081 CallInst *NewCI = new CallInst(FCache, Operands, Name, CI);
82 if (!CI->use_empty()) {
83 Value *V = NewCI;
84 if (CI->getType() != NewCI->getType())
Reid Spencer3da59db2006-11-27 01:05:10 +000085 V = CastInst::createInferredCast(NewCI, CI->getType(), Name, CI);
Chris Lattner02348ca2004-06-11 02:54:02 +000086 CI->replaceAllUsesWith(V);
87 }
88 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000089}
90
Chris Lattnerb71fd782006-11-15 18:00:10 +000091void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattner0979ca72004-05-09 04:29:57 +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 Lattner1f243e92005-05-08 19:46:29 +000097 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
98 Type::IntTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000099 break;
100 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +0000101 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
102 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +0000103 break;
104 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +0000105 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
106 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +0000107 break;
Chris Lattner03dd4652006-03-03 00:00:25 +0000108 case Intrinsic::memcpy_i32:
109 case Intrinsic::memcpy_i64:
Chris Lattnere4d5c442005-03-15 04:54:21 +0000110 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
111 I->arg_begin()->getType());
Chris Lattner0979ca72004-05-09 04:29:57 +0000112 break;
Chris Lattner03dd4652006-03-03 00:00:25 +0000113 case Intrinsic::memmove_i32:
114 case Intrinsic::memmove_i64:
Chris Lattnere4d5c442005-03-15 04:54:21 +0000115 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
116 I->arg_begin()->getType());
Chris Lattner0979ca72004-05-09 04:29:57 +0000117 break;
Chris Lattner03dd4652006-03-03 00:00:25 +0000118 case Intrinsic::memset_i32:
119 case Intrinsic::memset_i64:
Chris Lattner1f243e92005-05-08 19:46:29 +0000120 M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
121 PointerType::get(Type::SByteTy),
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000122 Type::IntTy, (--(--I->arg_end()))->getType(),
123 (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000124 break;
Reid Spencer0b118202006-01-16 21:12:35 +0000125 case Intrinsic::isunordered_f32:
126 case Intrinsic::isunordered_f64:
Chris Lattner1f243e92005-05-08 19:46:29 +0000127 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
128 Type::BoolTy);
Chris Lattner02348ca2004-06-11 02:54:02 +0000129 break;
Reid Spencer0b118202006-01-16 21:12:35 +0000130 case Intrinsic::sqrt_f32:
131 case Intrinsic::sqrt_f64:
Alkis Evlogimenosb1beff02005-04-30 07:13:31 +0000132 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner1f243e92005-05-08 19:46:29 +0000133 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
134 Type::FloatTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000135 else
Chris Lattner1f243e92005-05-08 19:46:29 +0000136 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
137 Type::DoubleTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000138 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000139 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000140}
Chris Lattner588e72d2004-02-15 22:16:39 +0000141
Nate Begemane5981812006-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 Begemane5981812006-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 Spencer3822ff52006-11-08 06:47:33 +0000154 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
Nate Begemane5981812006-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 Spencer3822ff52006-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 Begemane5981812006-01-16 07:57:00 +0000167 ConstantInt::get(Type::UByteTy,24),"bswap.1", IP);
168 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencerb83eb642006-10-20 07:07:24 +0000169 ConstantInt::get(Type::UIntTy, 0xFF0000),
Nate Begemane5981812006-01-16 07:57:00 +0000170 "bswap.and3", IP);
171 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencerb83eb642006-10-20 07:07:24 +0000172 ConstantInt::get(Type::UIntTy, 0xFF00),
Nate Begemane5981812006-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 Spencer3822ff52006-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 Begemane5981812006-01-16 07:57:00 +0000191 ConstantInt::get(Type::UByteTy,24),"bswap.3", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000192 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
Nate Begemane5981812006-01-16 07:57:00 +0000193 ConstantInt::get(Type::UByteTy,40),"bswap.2", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000194 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
Nate Begemane5981812006-01-16 07:57:00 +0000195 ConstantInt::get(Type::UByteTy,56),"bswap.1", IP);
196 Tmp7 = BinaryOperator::createAnd(Tmp7,
Reid Spencerb83eb642006-10-20 07:07:24 +0000197 ConstantInt::get(Type::ULongTy,
198 0xFF000000000000ULL),
199 "bswap.and7", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000200 Tmp6 = BinaryOperator::createAnd(Tmp6,
Reid Spencerb83eb642006-10-20 07:07:24 +0000201 ConstantInt::get(Type::ULongTy, 0xFF0000000000ULL),
202 "bswap.and6", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000203 Tmp5 = BinaryOperator::createAnd(Tmp5,
Reid Spencerb83eb642006-10-20 07:07:24 +0000204 ConstantInt::get(Type::ULongTy, 0xFF00000000ULL),
205 "bswap.and5", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000206 Tmp4 = BinaryOperator::createAnd(Tmp4,
Reid Spencerb83eb642006-10-20 07:07:24 +0000207 ConstantInt::get(Type::ULongTy, 0xFF000000ULL),
208 "bswap.and4", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000209 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencerb83eb642006-10-20 07:07:24 +0000210 ConstantInt::get(Type::ULongTy, 0xFF0000ULL),
211 "bswap.and3", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000212 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencerb83eb642006-10-20 07:07:24 +0000213 ConstantInt::get(Type::ULongTy, 0xFF00ULL),
214 "bswap.and2", IP);
Nate Begemane5981812006-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 Begemane5981812006-01-16 07:57:00 +0000225 return V;
226}
227
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000228/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000229/// instruction IP.
Chris Lattner86f3e0c2005-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 Lattner86f3e0c2005-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 Lattner98cf45b2005-05-11 20:24:12 +0000239 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Reid Spencer3822ff52006-11-08 06:47:33 +0000240
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000241 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
242 Value *MaskCst =
Reid Spencerb83eb642006-10-20 07:07:24 +0000243 ConstantExpr::getCast(ConstantInt::get(Type::ULongTy, MaskValues[ct]),
244 V->getType());
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000245 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000246 Value *VShift = new ShiftInst(Instruction::LShr, V,
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000247 ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
248 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
249 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
250 }
251
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000252 return V;
253}
254
Chris Lattner98cf45b2005-05-11 20:24:12 +0000255/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000256/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000257static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000258
259 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
260 for (unsigned i = 1; i != BitSize; i <<= 1) {
261 Value *ShVal = ConstantInt::get(Type::UByteTy, i);
Reid Spencer3822ff52006-11-08 06:47:33 +0000262 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000263 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
264 }
265
Chris Lattner98cf45b2005-05-11 20:24:12 +0000266 V = BinaryOperator::createNot(V, "", IP);
267 return LowerCTPOP(V, IP);
268}
269
Nate Begemane5981812006-01-16 07:57:00 +0000270
271
Chris Lattnerb71fd782006-11-15 18:00:10 +0000272void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000273 Function *Callee = CI->getCalledFunction();
274 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000275
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000276 switch (Callee->getIntrinsicID()) {
277 case Intrinsic::not_intrinsic:
278 std::cerr << "Cannot lower a call to a non-intrinsic function '"
279 << Callee->getName() << "'!\n";
280 abort();
281 default:
282 std::cerr << "Error: Code generator does not support intrinsic function '"
283 << Callee->getName() << "'!\n";
284 abort();
285
Chris Lattner588e72d2004-02-15 22:16:39 +0000286 // The setjmp/longjmp intrinsics should only exist in the code if it was
287 // never optimized (ie, right out of the CFE), or if it has been hacked on
288 // by the lowerinvoke pass. In both cases, the right thing to do is to
289 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000290 case Intrinsic::setjmp: {
291 static Function *SetjmpFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000292 static const unsigned castOpcodes[] = { Instruction::BitCast };
Chris Lattner9b700f72004-02-15 22:24:51 +0000293 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Reid Spencer3da59db2006-11-27 01:05:10 +0000294 castOpcodes, Type::IntTy, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000295 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000296 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000297 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000298 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000299 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000300 if (CI->getType() != Type::VoidTy)
301 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
302 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000303
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000304 case Intrinsic::longjmp: {
Chris Lattner9b700f72004-02-15 22:24:51 +0000305 static Function *LongjmpFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000306 static const unsigned castOpcodes[] =
307 { Instruction::BitCast, 0 };
Chris Lattner9b700f72004-02-15 22:24:51 +0000308 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Reid Spencer3da59db2006-11-27 01:05:10 +0000309 castOpcodes, Type::VoidTy, LongjmpFCache);
Chris Lattner9b700f72004-02-15 22:24:51 +0000310 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000311 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000312
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000313 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000314 // Insert the call to abort
Chris Lattner588e72d2004-02-15 22:16:39 +0000315 static Function *AbortFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000316 static const unsigned castOpcodes[] =
317 { Instruction::BitCast, 0 };
318 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
319 castOpcodes, Type::VoidTy, AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000320 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000321 }
Reid Spencer0b118202006-01-16 21:12:35 +0000322 case Intrinsic::ctpop_i8:
323 case Intrinsic::ctpop_i16:
324 case Intrinsic::ctpop_i32:
325 case Intrinsic::ctpop_i64:
326 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
327 break;
328
Nate Begemane5981812006-01-16 07:57:00 +0000329 case Intrinsic::bswap_i16:
330 case Intrinsic::bswap_i32:
331 case Intrinsic::bswap_i64:
332 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
333 break;
334
Reid Spencer0b118202006-01-16 21:12:35 +0000335 case Intrinsic::ctlz_i8:
336 case Intrinsic::ctlz_i16:
337 case Intrinsic::ctlz_i32:
338 case Intrinsic::ctlz_i64:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000339 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000340 break;
Nate Begemane5981812006-01-16 07:57:00 +0000341
Reid Spencer0b118202006-01-16 21:12:35 +0000342 case Intrinsic::cttz_i8:
343 case Intrinsic::cttz_i16:
344 case Intrinsic::cttz_i32:
345 case Intrinsic::cttz_i64: {
Chris Lattnera8011722005-05-11 20:02:14 +0000346 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000347 Value *Src = CI->getOperand(1);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000348 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnera8011722005-05-11 20:02:14 +0000349 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
350 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
351 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000352 CI->replaceAllUsesWith(Src);
353 break;
354 }
Chris Lattner77b13302004-01-05 05:36:30 +0000355
Chris Lattner0c067bc2006-01-13 02:22:08 +0000356 case Intrinsic::stacksave:
357 case Intrinsic::stackrestore: {
358 static bool Warned = false;
359 if (!Warned)
360 std::cerr << "WARNING: this target does not support the llvm.stack"
361 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
362 "save" : "restore") << " intrinsic.\n";
363 Warned = true;
364 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
365 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
366 break;
367 }
368
Chris Lattnercf899082004-02-14 02:47:17 +0000369 case Intrinsic::returnaddress:
370 case Intrinsic::frameaddress:
Chris Lattnercc42d2c2004-02-14 04:52:06 +0000371 std::cerr << "WARNING: this target does not support the llvm."
Misha Brukmanedf128a2005-04-21 22:36:52 +0000372 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
Chris Lattnercc42d2c2004-02-14 04:52:06 +0000373 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000374 CI->replaceAllUsesWith(ConstantPointerNull::get(
375 cast<PointerType>(CI->getType())));
376 break;
377
Chris Lattner0942b7c2005-02-28 19:27:23 +0000378 case Intrinsic::prefetch:
379 break; // Simply strip out prefetches on unsupported architectures
380
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000381 case Intrinsic::pcmarker:
382 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000383 case Intrinsic::readcyclecounter: {
Chris Lattner0c067bc2006-01-13 02:22:08 +0000384 std::cerr << "WARNING: this target does not support the llvm.readcyclecoun"
385 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencerb83eb642006-10-20 07:07:24 +0000386 CI->replaceAllUsesWith(ConstantInt::get(Type::ULongTy, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000387 break;
388 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000389
Chris Lattner77b13302004-01-05 05:36:30 +0000390 case Intrinsic::dbg_stoppoint:
391 case Intrinsic::dbg_region_start:
392 case Intrinsic::dbg_region_end:
393 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000394 case Intrinsic::dbg_declare:
Chris Lattner77b13302004-01-05 05:36:30 +0000395 break; // Simply strip out debugging intrinsics
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000396
Reid Spencer3da59db2006-11-27 01:05:10 +0000397 case Intrinsic::memcpy_i32: {
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000398 // The memcpy intrinsic take an extra alignment argument that the memcpy
399 // libc function does not.
Reid Spencer3da59db2006-11-27 01:05:10 +0000400 static unsigned opcodes[] =
401 { Instruction::BitCast, Instruction::BitCast, Instruction::BitCast };
402 // FIXME:
403 // if (target_is_64_bit) opcodes[2] = Instruction::ZExt;
404 // else opcodes[2] = Instruction::BitCast;
Chris Lattner588e72d2004-02-15 22:16:39 +0000405 static Function *MemcpyFCache = 0;
406 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer3da59db2006-11-27 01:05:10 +0000407 opcodes, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000408 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000409 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000410 case Intrinsic::memcpy_i64: {
411 static unsigned opcodes[] =
412 { Instruction::BitCast, Instruction::BitCast, Instruction::Trunc };
413 // FIXME:
414 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
415 // else opcodes[2] = Instruction::Trunc;
416 static Function *MemcpyFCache = 0;
417 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
418 opcodes, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
419 break;
420 }
421 case Intrinsic::memmove_i32: {
422 // The memmove intrinsic take an extra alignment argument that the memmove
423 // libc function does not.
424 static unsigned opcodes[] =
425 { Instruction::BitCast, Instruction::BitCast, Instruction::BitCast };
426 // FIXME:
427 // if (target_is_64_bit) opcodes[2] = Instruction::ZExt;
428 // else opcodes[2] = Instruction::BitCast;
429 static Function *MemmoveFCache = 0;
430 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
431 opcodes, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
432 break;
433 }
Chris Lattner03dd4652006-03-03 00:00:25 +0000434 case Intrinsic::memmove_i64: {
Chris Lattnercf899082004-02-14 02:47:17 +0000435 // The memmove intrinsic take an extra alignment argument that the memmove
Chris Lattner2751e762004-02-12 18:11:20 +0000436 // libc function does not.
Reid Spencer3da59db2006-11-27 01:05:10 +0000437 static const unsigned opcodes[] =
438 { Instruction::BitCast, Instruction::BitCast, Instruction::Trunc };
439 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
440 // else opcodes[2] = Instruction::Trunc;
Chris Lattner588e72d2004-02-15 22:16:39 +0000441 static Function *MemmoveFCache = 0;
442 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer3da59db2006-11-27 01:05:10 +0000443 opcodes, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000444 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000445 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000446 case Intrinsic::memset_i32: {
447 // The memset intrinsic take an extra alignment argument that the memset
448 // libc function does not.
449 static const unsigned opcodes[] =
450 { Instruction::BitCast, Instruction::ZExt, Instruction::ZExt, 0 };
451 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
452 // else opcodes[2] = Instruction::ZExt;
453 static Function *MemsetFCache = 0;
454 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
455 opcodes, (*(CI->op_begin()+1))->getType(), MemsetFCache);
456 }
Chris Lattner03dd4652006-03-03 00:00:25 +0000457 case Intrinsic::memset_i64: {
Chris Lattnercf899082004-02-14 02:47:17 +0000458 // The memset intrinsic take an extra alignment argument that the memset
459 // libc function does not.
Reid Spencer3da59db2006-11-27 01:05:10 +0000460 static const unsigned opcodes[] =
461 { Instruction::BitCast, Instruction::ZExt, Instruction::Trunc, 0 };
462 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
463 // else opcodes[2] = Instruction::Trunc;
Chris Lattner588e72d2004-02-15 22:16:39 +0000464 static Function *MemsetFCache = 0;
465 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer3da59db2006-11-27 01:05:10 +0000466 opcodes, (*(CI->op_begin()+1))->getType(), MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000467 break;
468 }
Reid Spencer0b118202006-01-16 21:12:35 +0000469 case Intrinsic::isunordered_f32:
470 case Intrinsic::isunordered_f64: {
Alkis Evlogimenosd0656fc2005-03-01 02:07:58 +0000471 Value *L = CI->getOperand(1);
472 Value *R = CI->getOperand(2);
473
474 Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
475 Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
476 CI->replaceAllUsesWith(
477 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
478 "isunordered", CI));
Alkis Evlogimenos96853722004-06-12 19:19:14 +0000479 break;
480 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000481 case Intrinsic::sqrt_f32: {
482 static const unsigned opcodes[] = { 0 };
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000483 static Function *sqrtfFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000484 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
485 opcodes, Type::FloatTy, sqrtfFCache);
486 break;
487 }
488 case Intrinsic::sqrt_f64: {
489 static const unsigned opcodes[] = { 0 };
490 static Function *sqrtFCache = 0;
491 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
492 opcodes, Type::DoubleTy, sqrtFCache);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000493 break;
494 }
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000495 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000496
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000497 assert(CI->use_empty() &&
498 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000499 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000500}