blob: 7c2008233ca1b9252b5ff38c4d8b2db3cf960891 [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//
10// This file implements the default intrinsic lowering implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/IntrinsicLowering.h"
15#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Module.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Andrew Lenharth5e177822005-05-03 17:19:30 +000019#include "llvm/Type.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000020#include <iostream>
21
Chris Lattnerbcdadf32004-06-20 07:49:54 +000022using namespace llvm;
23
24template <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
36/// 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,
43 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 }
57 }
58
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))
66 Arg = new CastInst(Arg, FT->getParamType(ArgNo), Arg->getName(), CI);
67 Operands.push_back(Arg);
68 }
69 // Pass nulls into any additional arguments...
70 for (; ArgNo != FT->getNumParams(); ++ArgNo)
71 Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
72
73 std::string Name = CI->getName(); CI->setName("");
74 if (FT->getReturnType() == Type::VoidTy) Name.clear();
75 CallInst *NewCI = new CallInst(FCache, Operands, Name, CI);
76 if (!CI->use_empty()) {
77 Value *V = NewCI;
78 if (CI->getType() != NewCI->getType())
79 V = new CastInst(NewCI, CI->getType(), Name, CI);
80 CI->replaceAllUsesWith(V);
81 }
82 return NewCI;
83}
84
85void DefaultIntrinsicLowering::AddPrototypes(Module &M) {
86 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
87 if (I->isExternal() && !I->use_empty())
88 switch (I->getIntrinsicID()) {
89 default: break;
90 case Intrinsic::setjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +000091 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
92 Type::IntTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000093 break;
94 case Intrinsic::longjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +000095 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
96 Type::VoidTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000097 break;
98 case Intrinsic::siglongjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +000099 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
100 Type::VoidTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000101 break;
102 case Intrinsic::memcpy:
Chris Lattner531f9e92005-03-15 04:54:21 +0000103 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
104 I->arg_begin()->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000105 break;
106 case Intrinsic::memmove:
Chris Lattner531f9e92005-03-15 04:54:21 +0000107 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
108 I->arg_begin()->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000109 break;
110 case Intrinsic::memset:
Chris Lattner9acd3142005-05-08 19:46:29 +0000111 M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
112 PointerType::get(Type::SByteTy),
Jeff Cohen11e26b52005-10-23 04:37:20 +0000113 Type::IntTy, (--(--I->arg_end()))->getType(),
114 (Type *)0);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000115 break;
116 case Intrinsic::isunordered:
Chris Lattner9acd3142005-05-08 19:46:29 +0000117 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
118 Type::BoolTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000119 break;
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000120 case Intrinsic::sqrt:
Alkis Evlogimenosd7e534b2005-04-30 07:13:31 +0000121 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner9acd3142005-05-08 19:46:29 +0000122 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
123 Type::FloatTy);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000124 else
Chris Lattner9acd3142005-05-08 19:46:29 +0000125 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
126 Type::DoubleTy);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000127 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000128 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000129}
130
Nate Begeman7d831fa2006-01-16 07:57:00 +0000131/// LowerBSWAP - Emit the code to lower bswap of V before the specified
132/// instruction IP.
133static Value *LowerBSWAP(Value *V, Instruction *IP) {
134 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
135
136 const Type *DestTy = V->getType();
137
138 // Force to unsigned so that the shift rights are logical.
139 if (DestTy->isSigned())
140 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
141
142 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
143
144 switch(BitSize) {
145 default: assert(0 && "Unhandled type size of value to byteswap!");
146 case 16: {
147 Value *Tmp1 = new ShiftInst(Instruction::Shl, V,
148 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
149 Value *Tmp2 = new ShiftInst(Instruction::Shr, V,
150 ConstantInt::get(Type::UByteTy,8),"bswap.1",IP);
151 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
152 break;
153 }
154 case 32: {
155 Value *Tmp4 = new ShiftInst(Instruction::Shl, V,
156 ConstantInt::get(Type::UByteTy,24),"bswap.4", IP);
157 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
158 ConstantInt::get(Type::UByteTy,8),"bswap.3",IP);
159 Value *Tmp2 = new ShiftInst(Instruction::Shr, V,
160 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
161 Value *Tmp1 = new ShiftInst(Instruction::Shr, V,
162 ConstantInt::get(Type::UByteTy,24),"bswap.1", IP);
163 Tmp3 = BinaryOperator::createAnd(Tmp3,
164 ConstantUInt::get(Type::UIntTy, 0xFF0000),
165 "bswap.and3", IP);
166 Tmp2 = BinaryOperator::createAnd(Tmp2,
167 ConstantUInt::get(Type::UIntTy, 0xFF00),
168 "bswap.and2", IP);
169 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
170 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
171 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
172 break;
173 }
174 case 64: {
175 Value *Tmp8 = new ShiftInst(Instruction::Shl, V,
176 ConstantInt::get(Type::UByteTy,56),"bswap.8", IP);
177 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
178 ConstantInt::get(Type::UByteTy,40),"bswap.7", IP);
179 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
180 ConstantInt::get(Type::UByteTy,24),"bswap.6", IP);
181 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
182 ConstantInt::get(Type::UByteTy,8),"bswap.5",IP);
183 Value *Tmp4 = new ShiftInst(Instruction::Shr, V,
184 ConstantInt::get(Type::UByteTy,8),"bswap.4",IP);
185 Value *Tmp3 = new ShiftInst(Instruction::Shr, V,
186 ConstantInt::get(Type::UByteTy,24),"bswap.3", IP);
187 Value *Tmp2 = new ShiftInst(Instruction::Shr, V,
188 ConstantInt::get(Type::UByteTy,40),"bswap.2", IP);
189 Value *Tmp1 = new ShiftInst(Instruction::Shr, V,
190 ConstantInt::get(Type::UByteTy,56),"bswap.1", IP);
191 Tmp7 = BinaryOperator::createAnd(Tmp7,
192 ConstantUInt::get(Type::ULongTy, 0xFF000000000000ULL),
193 "bswap.and7", IP);
194 Tmp6 = BinaryOperator::createAnd(Tmp6,
195 ConstantUInt::get(Type::ULongTy, 0xFF0000000000ULL),
196 "bswap.and6", IP);
197 Tmp5 = BinaryOperator::createAnd(Tmp5,
198 ConstantUInt::get(Type::ULongTy, 0xFF00000000ULL),
199 "bswap.and5", IP);
200 Tmp4 = BinaryOperator::createAnd(Tmp4,
201 ConstantUInt::get(Type::ULongTy, 0xFF000000ULL),
202 "bswap.and4", IP);
203 Tmp3 = BinaryOperator::createAnd(Tmp3,
204 ConstantUInt::get(Type::ULongTy, 0xFF0000ULL),
205 "bswap.and3", IP);
206 Tmp2 = BinaryOperator::createAnd(Tmp2,
207 ConstantUInt::get(Type::ULongTy, 0xFF00ULL),
208 "bswap.and2", IP);
209 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
210 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
211 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
212 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
213 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
214 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
215 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
216 break;
217 }
218 }
219
220 if (V->getType() != DestTy)
221 V = new CastInst(V, DestTy, V->getName(), IP);
222 return V;
223}
224
Chris Lattner9ec975a2005-05-11 19:42:05 +0000225/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begeman7d831fa2006-01-16 07:57:00 +0000226/// instruction IP.
Chris Lattner9ec975a2005-05-11 19:42:05 +0000227static Value *LowerCTPOP(Value *V, Instruction *IP) {
228 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000229
230 static const uint64_t MaskValues[6] = {
231 0x5555555555555555ULL, 0x3333333333333333ULL,
232 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
233 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
234 };
235
236 const Type *DestTy = V->getType();
237
238 // Force to unsigned so that the shift rights are logical.
239 if (DestTy->isSigned())
240 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
241
Chris Lattner991ce362005-05-11 20:24:12 +0000242 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Chris Lattner9ec975a2005-05-11 19:42:05 +0000243 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
244 Value *MaskCst =
245 ConstantExpr::getCast(ConstantUInt::get(Type::ULongTy,
246 MaskValues[ct]), V->getType());
247 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000248 Value *VShift = new ShiftInst(Instruction::Shr, V,
Chris Lattner9ec975a2005-05-11 19:42:05 +0000249 ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
250 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
251 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
252 }
253
254 if (V->getType() != DestTy)
255 V = new CastInst(V, DestTy, V->getName(), IP);
256 return V;
257}
258
Chris Lattner991ce362005-05-11 20:24:12 +0000259/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begeman7d831fa2006-01-16 07:57:00 +0000260/// instruction IP.
Chris Lattner991ce362005-05-11 20:24:12 +0000261static Value *LowerCTLZ(Value *V, Instruction *IP) {
262 const Type *DestTy = V->getType();
263
264 // Force to unsigned so that the shift rights are logical.
265 if (DestTy->isSigned())
266 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
267
268 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
269 for (unsigned i = 1; i != BitSize; i <<= 1) {
270 Value *ShVal = ConstantInt::get(Type::UByteTy, i);
271 ShVal = new ShiftInst(Instruction::Shr, V, ShVal, "ctlz.sh", IP);
272 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
273 }
274
275 if (V->getType() != DestTy)
276 V = new CastInst(V, DestTy, V->getName(), IP);
277
278 V = BinaryOperator::createNot(V, "", IP);
279 return LowerCTPOP(V, IP);
280}
281
Nate Begeman7d831fa2006-01-16 07:57:00 +0000282
283
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000284void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
285 Function *Callee = CI->getCalledFunction();
286 assert(Callee && "Cannot lower an indirect call!");
Misha Brukman835702a2005-04-21 22:36:52 +0000287
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000288 switch (Callee->getIntrinsicID()) {
289 case Intrinsic::not_intrinsic:
290 std::cerr << "Cannot lower a call to a non-intrinsic function '"
291 << Callee->getName() << "'!\n";
292 abort();
293 default:
294 std::cerr << "Error: Code generator does not support intrinsic function '"
295 << Callee->getName() << "'!\n";
296 abort();
297
298 // The setjmp/longjmp intrinsics should only exist in the code if it was
299 // never optimized (ie, right out of the CFE), or if it has been hacked on
300 // by the lowerinvoke pass. In both cases, the right thing to do is to
301 // convert the call to an explicit setjmp or longjmp call.
302 case Intrinsic::setjmp: {
303 static Function *SetjmpFCache = 0;
304 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
305 Type::IntTy, SetjmpFCache);
306 if (CI->getType() != Type::VoidTy)
307 CI->replaceAllUsesWith(V);
308 break;
309 }
Misha Brukman835702a2005-04-21 22:36:52 +0000310 case Intrinsic::sigsetjmp:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000311 if (CI->getType() != Type::VoidTy)
312 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
313 break;
314
315 case Intrinsic::longjmp: {
316 static Function *LongjmpFCache = 0;
317 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
318 Type::VoidTy, LongjmpFCache);
319 break;
320 }
321
322 case Intrinsic::siglongjmp: {
323 // Insert the call to abort
324 static Function *AbortFCache = 0;
325 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy,
326 AbortFCache);
327 break;
328 }
Nate Begeman7d831fa2006-01-16 07:57:00 +0000329
330 case Intrinsic::bswap_i16:
331 case Intrinsic::bswap_i32:
332 case Intrinsic::bswap_i64:
333 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
334 break;
335
Chris Lattner9ec975a2005-05-11 19:42:05 +0000336 case Intrinsic::ctpop:
337 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
Andrew Lenharth5e177822005-05-03 17:19:30 +0000338 break;
Chris Lattner9ec975a2005-05-11 19:42:05 +0000339
Chris Lattner991ce362005-05-11 20:24:12 +0000340 case Intrinsic::ctlz:
341 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth5e177822005-05-03 17:19:30 +0000342 break;
Nate Begeman7d831fa2006-01-16 07:57:00 +0000343
Andrew Lenharth5e177822005-05-03 17:19:30 +0000344 case Intrinsic::cttz: {
Chris Lattnerfe5759b2005-05-11 20:02:14 +0000345 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth5e177822005-05-03 17:19:30 +0000346 Value *Src = CI->getOperand(1);
Chris Lattner9ec975a2005-05-11 19:42:05 +0000347 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnerfe5759b2005-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 Lenharth5e177822005-05-03 17:19:30 +0000351 CI->replaceAllUsesWith(Src);
352 break;
353 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000354
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000355 case Intrinsic::stacksave:
356 case Intrinsic::stackrestore: {
357 static bool Warned = false;
358 if (!Warned)
359 std::cerr << "WARNING: this target does not support the llvm.stack"
360 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
361 "save" : "restore") << " intrinsic.\n";
362 Warned = true;
363 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
364 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
365 break;
366 }
367
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000368 case Intrinsic::returnaddress:
369 case Intrinsic::frameaddress:
370 std::cerr << "WARNING: this target does not support the llvm."
Misha Brukman835702a2005-04-21 22:36:52 +0000371 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000372 "return" : "frame") << "address intrinsic.\n";
373 CI->replaceAllUsesWith(ConstantPointerNull::get(
374 cast<PointerType>(CI->getType())));
375 break;
376
Chris Lattnerc87e03a2005-02-28 19:27:23 +0000377 case Intrinsic::prefetch:
378 break; // Simply strip out prefetches on unsupported architectures
379
Andrew Lenharthb4427912005-03-28 20:05:49 +0000380 case Intrinsic::pcmarker:
381 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000382 case Intrinsic::readcyclecounter: {
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000383 std::cerr << "WARNING: this target does not support the llvm.readcyclecoun"
384 << "ter intrinsic. It is being lowered to a constant 0\n";
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000385 CI->replaceAllUsesWith(ConstantUInt::get(Type::ULongTy, 0));
386 break;
387 }
Andrew Lenharthb4427912005-03-28 20:05:49 +0000388
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000389 case Intrinsic::dbg_stoppoint:
390 case Intrinsic::dbg_region_start:
391 case Intrinsic::dbg_region_end:
392 case Intrinsic::dbg_declare:
393 case Intrinsic::dbg_func_start:
394 if (CI->getType() != Type::VoidTy)
395 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
396 break; // Simply strip out debugging intrinsics
397
398 case Intrinsic::memcpy: {
399 // The memcpy intrinsic take an extra alignment argument that the memcpy
400 // libc function does not.
401 static Function *MemcpyFCache = 0;
402 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
403 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
404 break;
405 }
406 case Intrinsic::memmove: {
407 // The memmove intrinsic take an extra alignment argument that the memmove
408 // libc function does not.
409 static Function *MemmoveFCache = 0;
410 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
411 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
412 break;
413 }
414 case Intrinsic::memset: {
415 // The memset intrinsic take an extra alignment argument that the memset
416 // libc function does not.
417 static Function *MemsetFCache = 0;
418 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
419 (*(CI->op_begin()+1))->getType(), MemsetFCache);
420 break;
421 }
422 case Intrinsic::isunordered: {
Alkis Evlogimenosb3846f42005-03-01 02:07:58 +0000423 Value *L = CI->getOperand(1);
424 Value *R = CI->getOperand(2);
425
426 Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
427 Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
428 CI->replaceAllUsesWith(
429 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
430 "isunordered", CI));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000431 break;
432 }
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000433 case Intrinsic::sqrt: {
434 static Function *sqrtFCache = 0;
435 static Function *sqrtfFCache = 0;
436 if(CI->getType() == Type::FloatTy)
437 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
438 Type::FloatTy, sqrtfFCache);
439 else
440 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
441 Type::DoubleTy, sqrtFCache);
442 break;
443 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000444 }
Misha Brukman835702a2005-04-21 22:36:52 +0000445
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000446 assert(CI->use_empty() &&
447 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000448 CI->eraseFromParent();
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000449}