blob: b5a03fc54d53e4aff1595a63a7803cf73067123b [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//
10// This file implements the default intrinsic lowering implementation.
11//
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,
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 }
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))
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();
Chris Lattner02348ca2004-06-11 02:54:02 +000075 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;
Chris Lattner588e72d2004-02-15 22:16:39 +000083}
84
Chris Lattner0979ca72004-05-09 04:29:57 +000085void 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 Lattner1f243e92005-05-08 19:46:29 +000091 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
92 Type::IntTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000093 break;
94 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000095 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
96 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000097 break;
98 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000099 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
100 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +0000101 break;
Chris Lattner03dd4652006-03-03 00:00:25 +0000102 case Intrinsic::memcpy_i32:
103 case Intrinsic::memcpy_i64:
Chris Lattnere4d5c442005-03-15 04:54:21 +0000104 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
105 I->arg_begin()->getType());
Chris Lattner0979ca72004-05-09 04:29:57 +0000106 break;
Chris Lattner03dd4652006-03-03 00:00:25 +0000107 case Intrinsic::memmove_i32:
108 case Intrinsic::memmove_i64:
Chris Lattnere4d5c442005-03-15 04:54:21 +0000109 EnsureFunctionExists(M, "memmove", 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::memset_i32:
113 case Intrinsic::memset_i64:
Chris Lattner1f243e92005-05-08 19:46:29 +0000114 M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
115 PointerType::get(Type::SByteTy),
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000116 Type::IntTy, (--(--I->arg_end()))->getType(),
117 (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000118 break;
Reid Spencer0b118202006-01-16 21:12:35 +0000119 case Intrinsic::isunordered_f32:
120 case Intrinsic::isunordered_f64:
Chris Lattner1f243e92005-05-08 19:46:29 +0000121 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
122 Type::BoolTy);
Chris Lattner02348ca2004-06-11 02:54:02 +0000123 break;
Reid Spencer0b118202006-01-16 21:12:35 +0000124 case Intrinsic::sqrt_f32:
125 case Intrinsic::sqrt_f64:
Alkis Evlogimenosb1beff02005-04-30 07:13:31 +0000126 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner1f243e92005-05-08 19:46:29 +0000127 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
128 Type::FloatTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000129 else
Chris Lattner1f243e92005-05-08 19:46:29 +0000130 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
131 Type::DoubleTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000132 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000133 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000134}
Chris Lattner588e72d2004-02-15 22:16:39 +0000135
Nate Begemane5981812006-01-16 07:57:00 +0000136/// LowerBSWAP - Emit the code to lower bswap of V before the specified
137/// instruction IP.
138static Value *LowerBSWAP(Value *V, Instruction *IP) {
139 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
140
141 const Type *DestTy = V->getType();
142
143 // Force to unsigned so that the shift rights are logical.
144 if (DestTy->isSigned())
145 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
146
147 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);
154 Value *Tmp2 = new ShiftInst(Instruction::Shr, V,
155 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,
163 ConstantInt::get(Type::UByteTy,8),"bswap.3",IP);
164 Value *Tmp2 = new ShiftInst(Instruction::Shr, V,
165 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
166 Value *Tmp1 = new ShiftInst(Instruction::Shr, V,
167 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,
187 ConstantInt::get(Type::UByteTy,8),"bswap.5",IP);
188 Value *Tmp4 = new ShiftInst(Instruction::Shr, V,
189 ConstantInt::get(Type::UByteTy,8),"bswap.4",IP);
190 Value *Tmp3 = new ShiftInst(Instruction::Shr, V,
191 ConstantInt::get(Type::UByteTy,24),"bswap.3", IP);
192 Value *Tmp2 = new ShiftInst(Instruction::Shr, V,
193 ConstantInt::get(Type::UByteTy,40),"bswap.2", IP);
194 Value *Tmp1 = new ShiftInst(Instruction::Shr, V,
195 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 }
225
226 if (V->getType() != DestTy)
227 V = new CastInst(V, DestTy, V->getName(), IP);
228 return V;
229}
230
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000231/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000232/// instruction IP.
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000233static Value *LowerCTPOP(Value *V, Instruction *IP) {
234 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000235
236 static const uint64_t MaskValues[6] = {
237 0x5555555555555555ULL, 0x3333333333333333ULL,
238 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
239 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
240 };
241
242 const Type *DestTy = V->getType();
243
244 // Force to unsigned so that the shift rights are logical.
245 if (DestTy->isSigned())
246 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
247
Chris Lattner98cf45b2005-05-11 20:24:12 +0000248 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000249 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
250 Value *MaskCst =
Reid Spencerb83eb642006-10-20 07:07:24 +0000251 ConstantExpr::getCast(ConstantInt::get(Type::ULongTy, MaskValues[ct]),
252 V->getType());
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000253 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Jeff Cohen00b168892005-07-27 06:12:32 +0000254 Value *VShift = new ShiftInst(Instruction::Shr, V,
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000255 ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
256 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
257 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
258 }
259
260 if (V->getType() != DestTy)
261 V = new CastInst(V, DestTy, V->getName(), IP);
262 return V;
263}
264
Chris Lattner98cf45b2005-05-11 20:24:12 +0000265/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000266/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000267static Value *LowerCTLZ(Value *V, Instruction *IP) {
268 const Type *DestTy = V->getType();
269
270 // Force to unsigned so that the shift rights are logical.
271 if (DestTy->isSigned())
272 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
273
274 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
275 for (unsigned i = 1; i != BitSize; i <<= 1) {
276 Value *ShVal = ConstantInt::get(Type::UByteTy, i);
277 ShVal = new ShiftInst(Instruction::Shr, V, ShVal, "ctlz.sh", IP);
278 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
279 }
280
281 if (V->getType() != DestTy)
282 V = new CastInst(V, DestTy, V->getName(), IP);
283
284 V = BinaryOperator::createNot(V, "", IP);
285 return LowerCTPOP(V, IP);
286}
287
Nate Begemane5981812006-01-16 07:57:00 +0000288
289
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000290void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
291 Function *Callee = CI->getCalledFunction();
292 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000293
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000294 switch (Callee->getIntrinsicID()) {
295 case Intrinsic::not_intrinsic:
296 std::cerr << "Cannot lower a call to a non-intrinsic function '"
297 << Callee->getName() << "'!\n";
298 abort();
299 default:
300 std::cerr << "Error: Code generator does not support intrinsic function '"
301 << Callee->getName() << "'!\n";
302 abort();
303
Chris Lattner588e72d2004-02-15 22:16:39 +0000304 // The setjmp/longjmp intrinsics should only exist in the code if it was
305 // never optimized (ie, right out of the CFE), or if it has been hacked on
306 // by the lowerinvoke pass. In both cases, the right thing to do is to
307 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000308 case Intrinsic::setjmp: {
309 static Function *SetjmpFCache = 0;
310 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
311 Type::IntTy, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000312 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000313 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000314 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000315 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000316 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000317 if (CI->getType() != Type::VoidTy)
318 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
319 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000320
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000321 case Intrinsic::longjmp: {
Chris Lattner9b700f72004-02-15 22:24:51 +0000322 static Function *LongjmpFCache = 0;
323 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
324 Type::VoidTy, LongjmpFCache);
325 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000326 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000327
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000328 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000329 // Insert the call to abort
Chris Lattner588e72d2004-02-15 22:16:39 +0000330 static Function *AbortFCache = 0;
331 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy,
332 AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000333 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000334 }
Reid Spencer0b118202006-01-16 21:12:35 +0000335 case Intrinsic::ctpop_i8:
336 case Intrinsic::ctpop_i16:
337 case Intrinsic::ctpop_i32:
338 case Intrinsic::ctpop_i64:
339 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
340 break;
341
Nate Begemane5981812006-01-16 07:57:00 +0000342 case Intrinsic::bswap_i16:
343 case Intrinsic::bswap_i32:
344 case Intrinsic::bswap_i64:
345 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
346 break;
347
Reid Spencer0b118202006-01-16 21:12:35 +0000348 case Intrinsic::ctlz_i8:
349 case Intrinsic::ctlz_i16:
350 case Intrinsic::ctlz_i32:
351 case Intrinsic::ctlz_i64:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000352 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000353 break;
Nate Begemane5981812006-01-16 07:57:00 +0000354
Reid Spencer0b118202006-01-16 21:12:35 +0000355 case Intrinsic::cttz_i8:
356 case Intrinsic::cttz_i16:
357 case Intrinsic::cttz_i32:
358 case Intrinsic::cttz_i64: {
Chris Lattnera8011722005-05-11 20:02:14 +0000359 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000360 Value *Src = CI->getOperand(1);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000361 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnera8011722005-05-11 20:02:14 +0000362 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
363 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
364 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000365 CI->replaceAllUsesWith(Src);
366 break;
367 }
Chris Lattner77b13302004-01-05 05:36:30 +0000368
Chris Lattner0c067bc2006-01-13 02:22:08 +0000369 case Intrinsic::stacksave:
370 case Intrinsic::stackrestore: {
371 static bool Warned = false;
372 if (!Warned)
373 std::cerr << "WARNING: this target does not support the llvm.stack"
374 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
375 "save" : "restore") << " intrinsic.\n";
376 Warned = true;
377 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
378 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
379 break;
380 }
381
Chris Lattnercf899082004-02-14 02:47:17 +0000382 case Intrinsic::returnaddress:
383 case Intrinsic::frameaddress:
Chris Lattnercc42d2c2004-02-14 04:52:06 +0000384 std::cerr << "WARNING: this target does not support the llvm."
Misha Brukmanedf128a2005-04-21 22:36:52 +0000385 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
Chris Lattnercc42d2c2004-02-14 04:52:06 +0000386 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000387 CI->replaceAllUsesWith(ConstantPointerNull::get(
388 cast<PointerType>(CI->getType())));
389 break;
390
Chris Lattner0942b7c2005-02-28 19:27:23 +0000391 case Intrinsic::prefetch:
392 break; // Simply strip out prefetches on unsupported architectures
393
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000394 case Intrinsic::pcmarker:
395 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000396 case Intrinsic::readcyclecounter: {
Chris Lattner0c067bc2006-01-13 02:22:08 +0000397 std::cerr << "WARNING: this target does not support the llvm.readcyclecoun"
398 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencerb83eb642006-10-20 07:07:24 +0000399 CI->replaceAllUsesWith(ConstantInt::get(Type::ULongTy, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000400 break;
401 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000402
Chris Lattner77b13302004-01-05 05:36:30 +0000403 case Intrinsic::dbg_stoppoint:
404 case Intrinsic::dbg_region_start:
405 case Intrinsic::dbg_region_end:
406 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000407 case Intrinsic::dbg_declare:
Chris Lattner77b13302004-01-05 05:36:30 +0000408 break; // Simply strip out debugging intrinsics
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000409
Chris Lattner03dd4652006-03-03 00:00:25 +0000410 case Intrinsic::memcpy_i32:
411 case Intrinsic::memcpy_i64: {
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000412 // The memcpy intrinsic take an extra alignment argument that the memcpy
413 // libc function does not.
Chris Lattner588e72d2004-02-15 22:16:39 +0000414 static Function *MemcpyFCache = 0;
415 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
416 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000417 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000418 }
Chris Lattner03dd4652006-03-03 00:00:25 +0000419 case Intrinsic::memmove_i32:
420 case Intrinsic::memmove_i64: {
Chris Lattnercf899082004-02-14 02:47:17 +0000421 // The memmove intrinsic take an extra alignment argument that the memmove
Chris Lattner2751e762004-02-12 18:11:20 +0000422 // libc function does not.
Chris Lattner588e72d2004-02-15 22:16:39 +0000423 static Function *MemmoveFCache = 0;
424 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
425 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000426 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000427 }
Chris Lattner03dd4652006-03-03 00:00:25 +0000428 case Intrinsic::memset_i32:
429 case Intrinsic::memset_i64: {
Chris Lattnercf899082004-02-14 02:47:17 +0000430 // The memset intrinsic take an extra alignment argument that the memset
431 // libc function does not.
Chris Lattner588e72d2004-02-15 22:16:39 +0000432 static Function *MemsetFCache = 0;
433 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
434 (*(CI->op_begin()+1))->getType(), MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000435 break;
436 }
Reid Spencer0b118202006-01-16 21:12:35 +0000437 case Intrinsic::isunordered_f32:
438 case Intrinsic::isunordered_f64: {
Alkis Evlogimenosd0656fc2005-03-01 02:07:58 +0000439 Value *L = CI->getOperand(1);
440 Value *R = CI->getOperand(2);
441
442 Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
443 Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
444 CI->replaceAllUsesWith(
445 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
446 "isunordered", CI));
Alkis Evlogimenos96853722004-06-12 19:19:14 +0000447 break;
448 }
Reid Spencer0b118202006-01-16 21:12:35 +0000449 case Intrinsic::sqrt_f32:
450 case Intrinsic::sqrt_f64: {
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000451 static Function *sqrtFCache = 0;
452 static Function *sqrtfFCache = 0;
453 if(CI->getType() == Type::FloatTy)
454 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
455 Type::FloatTy, sqrtfFCache);
456 else
457 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
458 Type::DoubleTy, sqrtFCache);
459 break;
460 }
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000461 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000462
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000463 assert(CI->use_empty() &&
464 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000465 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000466}