blob: 55f1c3c0ba3c6c68734a086e2ae7f44925de1708 [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>
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
Chris Lattner588e72d2004-02-15 22:16:39 +000035/// ReplaceCallWith - This function is used when we want to lower an intrinsic
36/// call to a call of an external function. This handles hard cases such as
37/// when there was already a prototype for the external function, and if that
38/// prototype doesn't match the arguments we expect to pass in.
39template <class ArgIt>
40static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
41 ArgIt ArgBegin, ArgIt ArgEnd,
Reid Spencer3da59db2006-11-27 01:05:10 +000042 const unsigned *castOpcodes,
Chris Lattner588e72d2004-02-15 22:16:39 +000043 const Type *RetTy, Function *&FCache) {
44 if (!FCache) {
45 // If we haven't already looked up this function, check to see if the
46 // program already contains a function with this name.
47 Module *M = CI->getParent()->getParent()->getParent();
48 FCache = M->getNamedFunction(NewFn);
49 if (!FCache) {
50 // It doesn't already exist in the program, insert a new definition now.
51 std::vector<const Type *> ParamTys;
52 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
53 ParamTys.push_back((*I)->getType());
54 FCache = M->getOrInsertFunction(NewFn,
55 FunctionType::get(RetTy, ParamTys, false));
56 }
Chris Lattner0979ca72004-05-09 04:29:57 +000057 }
Chris Lattner588e72d2004-02-15 22:16:39 +000058
59 const FunctionType *FT = FCache->getFunctionType();
60 std::vector<Value*> Operands;
61 unsigned ArgNo = 0;
62 for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams();
63 ++I, ++ArgNo) {
64 Value *Arg = *I;
65 if (Arg->getType() != FT->getParamType(ArgNo))
Reid Spencer3da59db2006-11-27 01:05:10 +000066 if (castOpcodes[ArgNo])
67 Arg = CastInst::create(Instruction::CastOps(castOpcodes[ArgNo]),
68 Arg, FT->getParamType(ArgNo), Arg->getName(), CI);
69 else
70 Arg = CastInst::createInferredCast(Arg, FT->getParamType(ArgNo),
71 Arg->getName(), CI);
Chris Lattner588e72d2004-02-15 22:16:39 +000072 Operands.push_back(Arg);
73 }
74 // Pass nulls into any additional arguments...
75 for (; ArgNo != FT->getNumParams(); ++ArgNo)
76 Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
77
78 std::string Name = CI->getName(); CI->setName("");
79 if (FT->getReturnType() == Type::VoidTy) Name.clear();
Chris Lattner02348ca2004-06-11 02:54:02 +000080 CallInst *NewCI = new CallInst(FCache, Operands, Name, CI);
81 if (!CI->use_empty()) {
82 Value *V = NewCI;
83 if (CI->getType() != NewCI->getType())
Reid Spencer3da59db2006-11-27 01:05:10 +000084 V = CastInst::createInferredCast(NewCI, CI->getType(), Name, CI);
Chris Lattner02348ca2004-06-11 02:54:02 +000085 CI->replaceAllUsesWith(V);
86 }
87 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000088}
89
Chris Lattnerb71fd782006-11-15 18:00:10 +000090void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattner0979ca72004-05-09 04:29:57 +000091 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
92 if (I->isExternal() && !I->use_empty())
93 switch (I->getIntrinsicID()) {
94 default: break;
95 case Intrinsic::setjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000096 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
97 Type::IntTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000098 break;
99 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +0000100 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
101 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +0000102 break;
103 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +0000104 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
105 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +0000106 break;
Chris Lattner03dd4652006-03-03 00:00:25 +0000107 case Intrinsic::memcpy_i32:
108 case Intrinsic::memcpy_i64:
Chris Lattnere4d5c442005-03-15 04:54:21 +0000109 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
110 I->arg_begin()->getType());
Chris Lattner0979ca72004-05-09 04:29:57 +0000111 break;
Chris Lattner03dd4652006-03-03 00:00:25 +0000112 case Intrinsic::memmove_i32:
113 case Intrinsic::memmove_i64:
Chris Lattnere4d5c442005-03-15 04:54:21 +0000114 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
115 I->arg_begin()->getType());
Chris Lattner0979ca72004-05-09 04:29:57 +0000116 break;
Chris Lattner03dd4652006-03-03 00:00:25 +0000117 case Intrinsic::memset_i32:
118 case Intrinsic::memset_i64:
Chris Lattner1f243e92005-05-08 19:46:29 +0000119 M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
120 PointerType::get(Type::SByteTy),
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000121 Type::IntTy, (--(--I->arg_end()))->getType(),
122 (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000123 break;
Reid Spencer0b118202006-01-16 21:12:35 +0000124 case Intrinsic::isunordered_f32:
125 case Intrinsic::isunordered_f64:
Chris Lattner1f243e92005-05-08 19:46:29 +0000126 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
127 Type::BoolTy);
Chris Lattner02348ca2004-06-11 02:54:02 +0000128 break;
Reid Spencer0b118202006-01-16 21:12:35 +0000129 case Intrinsic::sqrt_f32:
130 case Intrinsic::sqrt_f64:
Alkis Evlogimenosb1beff02005-04-30 07:13:31 +0000131 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner1f243e92005-05-08 19:46:29 +0000132 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
133 Type::FloatTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000134 else
Chris Lattner1f243e92005-05-08 19:46:29 +0000135 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
136 Type::DoubleTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000137 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000138 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000139}
Chris Lattner588e72d2004-02-15 22:16:39 +0000140
Nate Begemane5981812006-01-16 07:57:00 +0000141/// LowerBSWAP - Emit the code to lower bswap of V before the specified
142/// instruction IP.
143static Value *LowerBSWAP(Value *V, Instruction *IP) {
144 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
145
Nate Begemane5981812006-01-16 07:57:00 +0000146 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
147
148 switch(BitSize) {
149 default: assert(0 && "Unhandled type size of value to byteswap!");
150 case 16: {
151 Value *Tmp1 = new ShiftInst(Instruction::Shl, V,
152 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000153 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
Nate Begemane5981812006-01-16 07:57:00 +0000154 ConstantInt::get(Type::UByteTy,8),"bswap.1",IP);
155 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
156 break;
157 }
158 case 32: {
159 Value *Tmp4 = new ShiftInst(Instruction::Shl, V,
160 ConstantInt::get(Type::UByteTy,24),"bswap.4", IP);
161 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
Reid Spencer3822ff52006-11-08 06:47:33 +0000162 ConstantInt::get(Type::UByteTy,8),"bswap.3",IP);
163 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
164 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
165 Value *Tmp1 = new ShiftInst(Instruction::LShr, V,
Nate Begemane5981812006-01-16 07:57:00 +0000166 ConstantInt::get(Type::UByteTy,24),"bswap.1", IP);
167 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencerb83eb642006-10-20 07:07:24 +0000168 ConstantInt::get(Type::UIntTy, 0xFF0000),
Nate Begemane5981812006-01-16 07:57:00 +0000169 "bswap.and3", IP);
170 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencerb83eb642006-10-20 07:07:24 +0000171 ConstantInt::get(Type::UIntTy, 0xFF00),
Nate Begemane5981812006-01-16 07:57:00 +0000172 "bswap.and2", IP);
173 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
174 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
175 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
176 break;
177 }
178 case 64: {
179 Value *Tmp8 = new ShiftInst(Instruction::Shl, V,
180 ConstantInt::get(Type::UByteTy,56),"bswap.8", IP);
181 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
182 ConstantInt::get(Type::UByteTy,40),"bswap.7", IP);
183 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
184 ConstantInt::get(Type::UByteTy,24),"bswap.6", IP);
185 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
Reid Spencer3822ff52006-11-08 06:47:33 +0000186 ConstantInt::get(Type::UByteTy,8),"bswap.5", IP);
187 Value* Tmp4 = new ShiftInst(Instruction::LShr, V,
188 ConstantInt::get(Type::UByteTy,8),"bswap.4", IP);
189 Value* Tmp3 = new ShiftInst(Instruction::LShr, V,
Nate Begemane5981812006-01-16 07:57:00 +0000190 ConstantInt::get(Type::UByteTy,24),"bswap.3", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000191 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
Nate Begemane5981812006-01-16 07:57:00 +0000192 ConstantInt::get(Type::UByteTy,40),"bswap.2", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000193 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
Nate Begemane5981812006-01-16 07:57:00 +0000194 ConstantInt::get(Type::UByteTy,56),"bswap.1", IP);
195 Tmp7 = BinaryOperator::createAnd(Tmp7,
Reid Spencerb83eb642006-10-20 07:07:24 +0000196 ConstantInt::get(Type::ULongTy,
197 0xFF000000000000ULL),
198 "bswap.and7", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000199 Tmp6 = BinaryOperator::createAnd(Tmp6,
Reid Spencerb83eb642006-10-20 07:07:24 +0000200 ConstantInt::get(Type::ULongTy, 0xFF0000000000ULL),
201 "bswap.and6", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000202 Tmp5 = BinaryOperator::createAnd(Tmp5,
Reid Spencerb83eb642006-10-20 07:07:24 +0000203 ConstantInt::get(Type::ULongTy, 0xFF00000000ULL),
204 "bswap.and5", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000205 Tmp4 = BinaryOperator::createAnd(Tmp4,
Reid Spencerb83eb642006-10-20 07:07:24 +0000206 ConstantInt::get(Type::ULongTy, 0xFF000000ULL),
207 "bswap.and4", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000208 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencerb83eb642006-10-20 07:07:24 +0000209 ConstantInt::get(Type::ULongTy, 0xFF0000ULL),
210 "bswap.and3", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000211 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencerb83eb642006-10-20 07:07:24 +0000212 ConstantInt::get(Type::ULongTy, 0xFF00ULL),
213 "bswap.and2", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000214 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
215 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
216 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
217 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
218 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
219 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
220 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
221 break;
222 }
223 }
Nate Begemane5981812006-01-16 07:57:00 +0000224 return V;
225}
226
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000227/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000228/// instruction IP.
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000229static Value *LowerCTPOP(Value *V, Instruction *IP) {
230 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000231
232 static const uint64_t MaskValues[6] = {
233 0x5555555555555555ULL, 0x3333333333333333ULL,
234 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
235 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
236 };
237
Chris Lattner98cf45b2005-05-11 20:24:12 +0000238 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Reid Spencer3822ff52006-11-08 06:47:33 +0000239
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000240 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
241 Value *MaskCst =
Reid Spencer15f46d62006-12-12 01:17:41 +0000242 ConstantExpr::getTruncOrBitCast(ConstantInt::get(Type::ULongTy, MaskValues[ct]),
Reid Spencerb83eb642006-10-20 07:07:24 +0000243 V->getType());
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000244 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Reid Spencer3822ff52006-11-08 06:47:33 +0000245 Value *VShift = new ShiftInst(Instruction::LShr, V,
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000246 ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
247 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
248 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
249 }
250
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000251 return V;
252}
253
Chris Lattner98cf45b2005-05-11 20:24:12 +0000254/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000255/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000256static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000257
258 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
259 for (unsigned i = 1; i != BitSize; i <<= 1) {
260 Value *ShVal = ConstantInt::get(Type::UByteTy, i);
Reid Spencer3822ff52006-11-08 06:47:33 +0000261 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000262 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
263 }
264
Chris Lattner98cf45b2005-05-11 20:24:12 +0000265 V = BinaryOperator::createNot(V, "", IP);
266 return LowerCTPOP(V, IP);
267}
268
Nate Begemane5981812006-01-16 07:57:00 +0000269
270
Chris Lattnerb71fd782006-11-15 18:00:10 +0000271void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000272 Function *Callee = CI->getCalledFunction();
273 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000274
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000275 switch (Callee->getIntrinsicID()) {
276 case Intrinsic::not_intrinsic:
Bill Wendlinge8156192006-12-07 01:30:32 +0000277 cerr << "Cannot lower a call to a non-intrinsic function '"
278 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000279 abort();
280 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000281 cerr << "Error: Code generator does not support intrinsic function '"
282 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000283 abort();
284
Chris Lattner588e72d2004-02-15 22:16:39 +0000285 // The setjmp/longjmp intrinsics should only exist in the code if it was
286 // never optimized (ie, right out of the CFE), or if it has been hacked on
287 // by the lowerinvoke pass. In both cases, the right thing to do is to
288 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000289 case Intrinsic::setjmp: {
290 static Function *SetjmpFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000291 static const unsigned castOpcodes[] = { Instruction::BitCast };
Chris Lattner9b700f72004-02-15 22:24:51 +0000292 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Reid Spencer3da59db2006-11-27 01:05:10 +0000293 castOpcodes, Type::IntTy, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000294 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000295 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000296 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000297 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000298 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000299 if (CI->getType() != Type::VoidTy)
300 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
301 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000302
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000303 case Intrinsic::longjmp: {
Chris Lattner9b700f72004-02-15 22:24:51 +0000304 static Function *LongjmpFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000305 static const unsigned castOpcodes[] =
306 { Instruction::BitCast, 0 };
Chris Lattner9b700f72004-02-15 22:24:51 +0000307 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Reid Spencer3da59db2006-11-27 01:05:10 +0000308 castOpcodes, Type::VoidTy, LongjmpFCache);
Chris Lattner9b700f72004-02-15 22:24:51 +0000309 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000310 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000311
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000312 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000313 // Insert the call to abort
Chris Lattner588e72d2004-02-15 22:16:39 +0000314 static Function *AbortFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000315 static const unsigned castOpcodes[] =
316 { Instruction::BitCast, 0 };
317 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
318 castOpcodes, Type::VoidTy, AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000319 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000320 }
Reid Spencer0b118202006-01-16 21:12:35 +0000321 case Intrinsic::ctpop_i8:
322 case Intrinsic::ctpop_i16:
323 case Intrinsic::ctpop_i32:
324 case Intrinsic::ctpop_i64:
325 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
326 break;
327
Nate Begemane5981812006-01-16 07:57:00 +0000328 case Intrinsic::bswap_i16:
329 case Intrinsic::bswap_i32:
330 case Intrinsic::bswap_i64:
331 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
332 break;
333
Reid Spencer0b118202006-01-16 21:12:35 +0000334 case Intrinsic::ctlz_i8:
335 case Intrinsic::ctlz_i16:
336 case Intrinsic::ctlz_i32:
337 case Intrinsic::ctlz_i64:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000338 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000339 break;
Nate Begemane5981812006-01-16 07:57:00 +0000340
Reid Spencer0b118202006-01-16 21:12:35 +0000341 case Intrinsic::cttz_i8:
342 case Intrinsic::cttz_i16:
343 case Intrinsic::cttz_i32:
344 case Intrinsic::cttz_i64: {
Chris Lattnera8011722005-05-11 20:02:14 +0000345 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000346 Value *Src = CI->getOperand(1);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000347 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnera8011722005-05-11 20:02:14 +0000348 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
349 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
350 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000351 CI->replaceAllUsesWith(Src);
352 break;
353 }
Chris Lattner77b13302004-01-05 05:36:30 +0000354
Chris Lattner0c067bc2006-01-13 02:22:08 +0000355 case Intrinsic::stacksave:
356 case Intrinsic::stackrestore: {
357 static bool Warned = false;
358 if (!Warned)
Bill Wendlinge8156192006-12-07 01:30:32 +0000359 cerr << "WARNING: this target does not support the llvm.stack"
360 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
361 "save" : "restore") << " intrinsic.\n";
Chris Lattner0c067bc2006-01-13 02:22:08 +0000362 Warned = true;
363 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
364 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
365 break;
366 }
367
Chris Lattnercf899082004-02-14 02:47:17 +0000368 case Intrinsic::returnaddress:
369 case Intrinsic::frameaddress:
Bill Wendlinge8156192006-12-07 01:30:32 +0000370 cerr << "WARNING: this target does not support the llvm."
371 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
372 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000373 CI->replaceAllUsesWith(ConstantPointerNull::get(
374 cast<PointerType>(CI->getType())));
375 break;
376
Chris Lattner0942b7c2005-02-28 19:27:23 +0000377 case Intrinsic::prefetch:
378 break; // Simply strip out prefetches on unsupported architectures
379
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000380 case Intrinsic::pcmarker:
381 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000382 case Intrinsic::readcyclecounter: {
Bill Wendlinge8156192006-12-07 01:30:32 +0000383 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
384 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencerb83eb642006-10-20 07:07:24 +0000385 CI->replaceAllUsesWith(ConstantInt::get(Type::ULongTy, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000386 break;
387 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000388
Chris Lattner77b13302004-01-05 05:36:30 +0000389 case Intrinsic::dbg_stoppoint:
390 case Intrinsic::dbg_region_start:
391 case Intrinsic::dbg_region_end:
392 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000393 case Intrinsic::dbg_declare:
Chris Lattner77b13302004-01-05 05:36:30 +0000394 break; // Simply strip out debugging intrinsics
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000395
Reid Spencer3da59db2006-11-27 01:05:10 +0000396 case Intrinsic::memcpy_i32: {
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000397 // The memcpy intrinsic take an extra alignment argument that the memcpy
398 // libc function does not.
Reid Spencer3da59db2006-11-27 01:05:10 +0000399 static unsigned opcodes[] =
400 { Instruction::BitCast, Instruction::BitCast, Instruction::BitCast };
401 // FIXME:
402 // if (target_is_64_bit) opcodes[2] = Instruction::ZExt;
403 // else opcodes[2] = Instruction::BitCast;
Chris Lattner588e72d2004-02-15 22:16:39 +0000404 static Function *MemcpyFCache = 0;
405 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer3da59db2006-11-27 01:05:10 +0000406 opcodes, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000407 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000408 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000409 case Intrinsic::memcpy_i64: {
410 static unsigned opcodes[] =
411 { Instruction::BitCast, Instruction::BitCast, Instruction::Trunc };
412 // FIXME:
413 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
414 // else opcodes[2] = Instruction::Trunc;
415 static Function *MemcpyFCache = 0;
416 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
417 opcodes, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
418 break;
419 }
420 case Intrinsic::memmove_i32: {
421 // The memmove intrinsic take an extra alignment argument that the memmove
422 // libc function does not.
423 static unsigned opcodes[] =
424 { Instruction::BitCast, Instruction::BitCast, Instruction::BitCast };
425 // FIXME:
426 // if (target_is_64_bit) opcodes[2] = Instruction::ZExt;
427 // else opcodes[2] = Instruction::BitCast;
428 static Function *MemmoveFCache = 0;
429 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
430 opcodes, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
431 break;
432 }
Chris Lattner03dd4652006-03-03 00:00:25 +0000433 case Intrinsic::memmove_i64: {
Chris Lattnercf899082004-02-14 02:47:17 +0000434 // The memmove intrinsic take an extra alignment argument that the memmove
Chris Lattner2751e762004-02-12 18:11:20 +0000435 // libc function does not.
Reid Spencer3da59db2006-11-27 01:05:10 +0000436 static const unsigned opcodes[] =
437 { Instruction::BitCast, Instruction::BitCast, Instruction::Trunc };
438 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
439 // else opcodes[2] = Instruction::Trunc;
Chris Lattner588e72d2004-02-15 22:16:39 +0000440 static Function *MemmoveFCache = 0;
441 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer3da59db2006-11-27 01:05:10 +0000442 opcodes, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000443 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000444 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000445 case Intrinsic::memset_i32: {
446 // The memset intrinsic take an extra alignment argument that the memset
447 // libc function does not.
448 static const unsigned opcodes[] =
449 { Instruction::BitCast, Instruction::ZExt, Instruction::ZExt, 0 };
450 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
451 // else opcodes[2] = Instruction::ZExt;
452 static Function *MemsetFCache = 0;
453 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
454 opcodes, (*(CI->op_begin()+1))->getType(), MemsetFCache);
455 }
Chris Lattner03dd4652006-03-03 00:00:25 +0000456 case Intrinsic::memset_i64: {
Chris Lattnercf899082004-02-14 02:47:17 +0000457 // The memset intrinsic take an extra alignment argument that the memset
458 // libc function does not.
Reid Spencer3da59db2006-11-27 01:05:10 +0000459 static const unsigned opcodes[] =
460 { Instruction::BitCast, Instruction::ZExt, Instruction::Trunc, 0 };
461 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
462 // else opcodes[2] = Instruction::Trunc;
Chris Lattner588e72d2004-02-15 22:16:39 +0000463 static Function *MemsetFCache = 0;
464 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Reid Spencer3da59db2006-11-27 01:05:10 +0000465 opcodes, (*(CI->op_begin()+1))->getType(), MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000466 break;
467 }
Reid Spencer0b118202006-01-16 21:12:35 +0000468 case Intrinsic::isunordered_f32:
469 case Intrinsic::isunordered_f64: {
Alkis Evlogimenosd0656fc2005-03-01 02:07:58 +0000470 Value *L = CI->getOperand(1);
471 Value *R = CI->getOperand(2);
472
473 Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
474 Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
475 CI->replaceAllUsesWith(
476 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
477 "isunordered", CI));
Alkis Evlogimenos96853722004-06-12 19:19:14 +0000478 break;
479 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000480 case Intrinsic::sqrt_f32: {
481 static const unsigned opcodes[] = { 0 };
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000482 static Function *sqrtfFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000483 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
484 opcodes, Type::FloatTy, sqrtfFCache);
485 break;
486 }
487 case Intrinsic::sqrt_f64: {
488 static const unsigned opcodes[] = { 0 };
489 static Function *sqrtFCache = 0;
490 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
491 opcodes, Type::DoubleTy, sqrtFCache);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000492 break;
493 }
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000494 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000495
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000496 assert(CI->use_empty() &&
497 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000498 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000499}