blob: ba89052572a1a20c3d17019d2b2dff1391375f35 [file] [log] [blame]
Chris Lattnerbcdadf32004-06-20 07:49:54 +00001//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerbcdadf32004-06-20 07:49:54 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerbcdadf32004-06-20 07:49:54 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattner2775aba2006-11-15 18:00:10 +000010// This file implements the IntrinsicLowering class.
Chris Lattnerbcdadf32004-06-20 07:49:54 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbcdadf32004-06-20 07:49:54 +000014#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
16#include "llvm/Module.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Andrew Lenharth5e177822005-05-03 17:19:30 +000018#include "llvm/Type.h"
Bill Wendling3f6f0fd2006-11-28 02:08:17 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
20#include "llvm/Support/Streams.h"
Chris Lattnerbcdadf32004-06-20 07:49:54 +000021using namespace llvm;
22
23template <class ArgIt>
Chris Lattner34acba42007-01-07 08:12:01 +000024static void EnsureFunctionExists(Module &M, const char *Name,
25 ArgIt ArgBegin, ArgIt ArgEnd,
26 const Type *RetTy) {
27 // Insert a correctly-typed definition now.
Chris Lattnerbcdadf32004-06-20 07:49:54 +000028 std::vector<const Type *> ParamTys;
29 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
30 ParamTys.push_back(I->getType());
Chris Lattner34acba42007-01-07 08:12:01 +000031 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
Chris Lattnerbcdadf32004-06-20 07:49:54 +000032}
33
34/// ReplaceCallWith - This function is used when we want to lower an intrinsic
35/// call to a call of an external function. This handles hard cases such as
36/// when there was already a prototype for the external function, and if that
37/// prototype doesn't match the arguments we expect to pass in.
38template <class ArgIt>
39static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattner34acba42007-01-07 08:12:01 +000040 ArgIt ArgBegin, ArgIt ArgEnd,
41 const Type *RetTy, Constant *&FCache) {
Chris Lattnerbcdadf32004-06-20 07:49:54 +000042 if (!FCache) {
43 // If we haven't already looked up this function, check to see if the
44 // program already contains a function with this name.
45 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattner34acba42007-01-07 08:12:01 +000046 // Get or insert the definition now.
47 std::vector<const Type *> ParamTys;
48 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
49 ParamTys.push_back((*I)->getType());
50 FCache = M->getOrInsertFunction(NewFn,
51 FunctionType::get(RetTy, ParamTys, false));
Chris Lattnerbcdadf32004-06-20 07:49:54 +000052 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +000053
Chris Lattner34acba42007-01-07 08:12:01 +000054 std::vector<Value*> Operands(ArgBegin, ArgEnd);
55 CallInst *NewCI = new CallInst(FCache, Operands, CI->getName(), CI);
56 if (!CI->use_empty())
57 CI->replaceAllUsesWith(NewCI);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000058 return NewCI;
59}
60
Chris Lattner2775aba2006-11-15 18:00:10 +000061void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattnerbcdadf32004-06-20 07:49:54 +000062 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
63 if (I->isExternal() && !I->use_empty())
64 switch (I->getIntrinsicID()) {
65 default: break;
66 case Intrinsic::setjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +000067 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Reid Spencere63b6512006-12-31 05:55:36 +000068 Type::Int32Ty);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000069 break;
70 case Intrinsic::longjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +000071 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
72 Type::VoidTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000073 break;
74 case Intrinsic::siglongjmp:
Chris Lattner9acd3142005-05-08 19:46:29 +000075 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
76 Type::VoidTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000077 break;
Chris Lattner093c1592006-03-03 00:00:25 +000078 case Intrinsic::memcpy_i32:
79 case Intrinsic::memcpy_i64:
Chris Lattner531f9e92005-03-15 04:54:21 +000080 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
81 I->arg_begin()->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +000082 break;
Chris Lattner093c1592006-03-03 00:00:25 +000083 case Intrinsic::memmove_i32:
84 case Intrinsic::memmove_i64:
Chris Lattner531f9e92005-03-15 04:54:21 +000085 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
86 I->arg_begin()->getType());
Chris Lattnerbcdadf32004-06-20 07:49:54 +000087 break;
Chris Lattner093c1592006-03-03 00:00:25 +000088 case Intrinsic::memset_i32:
89 case Intrinsic::memset_i64:
Reid Spencere63b6512006-12-31 05:55:36 +000090 M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty),
91 PointerType::get(Type::Int8Ty),
92 Type::Int32Ty, (--(--I->arg_end()))->getType(),
Jeff Cohen11e26b52005-10-23 04:37:20 +000093 (Type *)0);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000094 break;
Reid Spencerb4f9a6f2006-01-16 21:12:35 +000095 case Intrinsic::isunordered_f32:
96 case Intrinsic::isunordered_f64:
Chris Lattner9acd3142005-05-08 19:46:29 +000097 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
98 Type::BoolTy);
Chris Lattnerbcdadf32004-06-20 07:49:54 +000099 break;
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000100 case Intrinsic::sqrt_f32:
101 case Intrinsic::sqrt_f64:
Alkis Evlogimenosd7e534b2005-04-30 07:13:31 +0000102 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner9acd3142005-05-08 19:46:29 +0000103 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
104 Type::FloatTy);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000105 else
Chris Lattner9acd3142005-05-08 19:46:29 +0000106 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
107 Type::DoubleTy);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000108 break;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000109 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000110}
111
Nate Begeman7d831fa2006-01-16 07:57:00 +0000112/// LowerBSWAP - Emit the code to lower bswap of V before the specified
113/// instruction IP.
114static Value *LowerBSWAP(Value *V, Instruction *IP) {
115 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
116
Nate Begeman7d831fa2006-01-16 07:57:00 +0000117 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
118
119 switch(BitSize) {
120 default: assert(0 && "Unhandled type size of value to byteswap!");
121 case 16: {
122 Value *Tmp1 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000123 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000124 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000125 ConstantInt::get(Type::Int8Ty,8),"bswap.1",IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000126 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
127 break;
128 }
129 case 32: {
130 Value *Tmp4 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000131 ConstantInt::get(Type::Int8Ty,24),"bswap.4", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000132 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000133 ConstantInt::get(Type::Int8Ty,8),"bswap.3",IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000134 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000135 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000136 Value *Tmp1 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000137 ConstantInt::get(Type::Int8Ty,24),"bswap.1", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000138 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencere63b6512006-12-31 05:55:36 +0000139 ConstantInt::get(Type::Int32Ty, 0xFF0000),
Nate Begeman7d831fa2006-01-16 07:57:00 +0000140 "bswap.and3", IP);
141 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencere63b6512006-12-31 05:55:36 +0000142 ConstantInt::get(Type::Int32Ty, 0xFF00),
Nate Begeman7d831fa2006-01-16 07:57:00 +0000143 "bswap.and2", IP);
144 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
145 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
146 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
147 break;
148 }
149 case 64: {
150 Value *Tmp8 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000151 ConstantInt::get(Type::Int8Ty,56),"bswap.8", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000152 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000153 ConstantInt::get(Type::Int8Ty,40),"bswap.7", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000154 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000155 ConstantInt::get(Type::Int8Ty,24),"bswap.6", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000156 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000157 ConstantInt::get(Type::Int8Ty,8),"bswap.5", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000158 Value* Tmp4 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000159 ConstantInt::get(Type::Int8Ty,8),"bswap.4", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000160 Value* Tmp3 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000161 ConstantInt::get(Type::Int8Ty,24),"bswap.3", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000162 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000163 ConstantInt::get(Type::Int8Ty,40),"bswap.2", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000164 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000165 ConstantInt::get(Type::Int8Ty,56),"bswap.1", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000166 Tmp7 = BinaryOperator::createAnd(Tmp7,
Reid Spencere63b6512006-12-31 05:55:36 +0000167 ConstantInt::get(Type::Int64Ty,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000168 0xFF000000000000ULL),
169 "bswap.and7", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000170 Tmp6 = BinaryOperator::createAnd(Tmp6,
Reid Spencere63b6512006-12-31 05:55:36 +0000171 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000172 "bswap.and6", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000173 Tmp5 = BinaryOperator::createAnd(Tmp5,
Reid Spencere63b6512006-12-31 05:55:36 +0000174 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000175 "bswap.and5", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000176 Tmp4 = BinaryOperator::createAnd(Tmp4,
Reid Spencere63b6512006-12-31 05:55:36 +0000177 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000178 "bswap.and4", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000179 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencere63b6512006-12-31 05:55:36 +0000180 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000181 "bswap.and3", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000182 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencere63b6512006-12-31 05:55:36 +0000183 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Reid Spencere0fc4df2006-10-20 07:07:24 +0000184 "bswap.and2", IP);
Nate Begeman7d831fa2006-01-16 07:57:00 +0000185 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
186 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
187 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
188 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
189 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
190 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
191 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
192 break;
193 }
194 }
Nate Begeman7d831fa2006-01-16 07:57:00 +0000195 return V;
196}
197
Chris Lattner9ec975a2005-05-11 19:42:05 +0000198/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begeman7d831fa2006-01-16 07:57:00 +0000199/// instruction IP.
Chris Lattner9ec975a2005-05-11 19:42:05 +0000200static Value *LowerCTPOP(Value *V, Instruction *IP) {
201 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000202
203 static const uint64_t MaskValues[6] = {
204 0x5555555555555555ULL, 0x3333333333333333ULL,
205 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
206 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
207 };
208
Chris Lattner991ce362005-05-11 20:24:12 +0000209 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Reid Spencerfdff9382006-11-08 06:47:33 +0000210
Chris Lattner9ec975a2005-05-11 19:42:05 +0000211 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
Chris Lattner6801f022006-12-12 05:19:46 +0000212 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Chris Lattner9ec975a2005-05-11 19:42:05 +0000213 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Reid Spencerfdff9382006-11-08 06:47:33 +0000214 Value *VShift = new ShiftInst(Instruction::LShr, V,
Reid Spencere63b6512006-12-31 05:55:36 +0000215 ConstantInt::get(Type::Int8Ty, i), "ctpop.sh", IP);
Chris Lattner9ec975a2005-05-11 19:42:05 +0000216 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
217 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
218 }
219
Chris Lattner9ec975a2005-05-11 19:42:05 +0000220 return V;
221}
222
Chris Lattner991ce362005-05-11 20:24:12 +0000223/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begeman7d831fa2006-01-16 07:57:00 +0000224/// instruction IP.
Chris Lattner991ce362005-05-11 20:24:12 +0000225static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner991ce362005-05-11 20:24:12 +0000226
227 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
228 for (unsigned i = 1; i != BitSize; i <<= 1) {
Reid Spencere63b6512006-12-31 05:55:36 +0000229 Value *ShVal = ConstantInt::get(Type::Int8Ty, i);
Reid Spencerfdff9382006-11-08 06:47:33 +0000230 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
Chris Lattner991ce362005-05-11 20:24:12 +0000231 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
232 }
233
Chris Lattner991ce362005-05-11 20:24:12 +0000234 V = BinaryOperator::createNot(V, "", IP);
235 return LowerCTPOP(V, IP);
236}
237
Nate Begeman7d831fa2006-01-16 07:57:00 +0000238
239
Chris Lattner2775aba2006-11-15 18:00:10 +0000240void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000241 Function *Callee = CI->getCalledFunction();
242 assert(Callee && "Cannot lower an indirect call!");
Misha Brukman835702a2005-04-21 22:36:52 +0000243
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000244 switch (Callee->getIntrinsicID()) {
245 case Intrinsic::not_intrinsic:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000246 cerr << "Cannot lower a call to a non-intrinsic function '"
247 << Callee->getName() << "'!\n";
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000248 abort();
249 default:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000250 cerr << "Error: Code generator does not support intrinsic function '"
251 << Callee->getName() << "'!\n";
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000252 abort();
253
254 // The setjmp/longjmp intrinsics should only exist in the code if it was
255 // never optimized (ie, right out of the CFE), or if it has been hacked on
256 // by the lowerinvoke pass. In both cases, the right thing to do is to
257 // convert the call to an explicit setjmp or longjmp call.
258 case Intrinsic::setjmp: {
Chris Lattner34acba42007-01-07 08:12:01 +0000259 static Constant *SetjmpFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000260 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000261 Type::Int32Ty, SetjmpFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000262 if (CI->getType() != Type::VoidTy)
263 CI->replaceAllUsesWith(V);
264 break;
265 }
Misha Brukman835702a2005-04-21 22:36:52 +0000266 case Intrinsic::sigsetjmp:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000267 if (CI->getType() != Type::VoidTy)
268 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
269 break;
270
271 case Intrinsic::longjmp: {
Chris Lattner34acba42007-01-07 08:12:01 +0000272 static Constant *LongjmpFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000273 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000274 Type::VoidTy, LongjmpFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000275 break;
276 }
277
278 case Intrinsic::siglongjmp: {
279 // Insert the call to abort
Chris Lattner34acba42007-01-07 08:12:01 +0000280 static Constant *AbortFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000281 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000282 Type::VoidTy, AbortFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000283 break;
284 }
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000285 case Intrinsic::ctpop_i8:
286 case Intrinsic::ctpop_i16:
287 case Intrinsic::ctpop_i32:
288 case Intrinsic::ctpop_i64:
289 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
290 break;
291
Nate Begeman7d831fa2006-01-16 07:57:00 +0000292 case Intrinsic::bswap_i16:
293 case Intrinsic::bswap_i32:
294 case Intrinsic::bswap_i64:
295 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
296 break;
297
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000298 case Intrinsic::ctlz_i8:
299 case Intrinsic::ctlz_i16:
300 case Intrinsic::ctlz_i32:
301 case Intrinsic::ctlz_i64:
Chris Lattner991ce362005-05-11 20:24:12 +0000302 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth5e177822005-05-03 17:19:30 +0000303 break;
Nate Begeman7d831fa2006-01-16 07:57:00 +0000304
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000305 case Intrinsic::cttz_i8:
306 case Intrinsic::cttz_i16:
307 case Intrinsic::cttz_i32:
308 case Intrinsic::cttz_i64: {
Chris Lattnerfe5759b2005-05-11 20:02:14 +0000309 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth5e177822005-05-03 17:19:30 +0000310 Value *Src = CI->getOperand(1);
Chris Lattner9ec975a2005-05-11 19:42:05 +0000311 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnerfe5759b2005-05-11 20:02:14 +0000312 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
313 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
314 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth5e177822005-05-03 17:19:30 +0000315 CI->replaceAllUsesWith(Src);
316 break;
317 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000318
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000319 case Intrinsic::stacksave:
320 case Intrinsic::stackrestore: {
321 static bool Warned = false;
322 if (!Warned)
Bill Wendlingf3baad32006-12-07 01:30:32 +0000323 cerr << "WARNING: this target does not support the llvm.stack"
324 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
325 "save" : "restore") << " intrinsic.\n";
Chris Lattner3b2b0af2006-01-13 02:22:08 +0000326 Warned = true;
327 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
328 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
329 break;
330 }
331
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000332 case Intrinsic::returnaddress:
333 case Intrinsic::frameaddress:
Bill Wendlingf3baad32006-12-07 01:30:32 +0000334 cerr << "WARNING: this target does not support the llvm."
335 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
336 "return" : "frame") << "address intrinsic.\n";
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000337 CI->replaceAllUsesWith(ConstantPointerNull::get(
338 cast<PointerType>(CI->getType())));
339 break;
340
Chris Lattnerc87e03a2005-02-28 19:27:23 +0000341 case Intrinsic::prefetch:
342 break; // Simply strip out prefetches on unsupported architectures
343
Andrew Lenharthb4427912005-03-28 20:05:49 +0000344 case Intrinsic::pcmarker:
345 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000346 case Intrinsic::readcyclecounter: {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000347 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
348 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencere63b6512006-12-31 05:55:36 +0000349 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000350 break;
351 }
Andrew Lenharthb4427912005-03-28 20:05:49 +0000352
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000353 case Intrinsic::dbg_stoppoint:
354 case Intrinsic::dbg_region_start:
355 case Intrinsic::dbg_region_end:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000356 case Intrinsic::dbg_func_start:
Jim Laskeya8bdac82006-03-23 18:06:46 +0000357 case Intrinsic::dbg_declare:
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000358 break; // Simply strip out debugging intrinsics
359
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000360 case Intrinsic::memcpy_i32: {
Chris Lattner34acba42007-01-07 08:12:01 +0000361 static Constant *MemcpyFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000362 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000363 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000364 break;
365 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000366 case Intrinsic::memcpy_i64: {
Chris Lattner34acba42007-01-07 08:12:01 +0000367 static Constant *MemcpyFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000368 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000369 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000370 break;
371 }
372 case Intrinsic::memmove_i32: {
Chris Lattner34acba42007-01-07 08:12:01 +0000373 static Constant *MemmoveFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000374 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000375 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000376 break;
377 }
Chris Lattner093c1592006-03-03 00:00:25 +0000378 case Intrinsic::memmove_i64: {
Chris Lattner34acba42007-01-07 08:12:01 +0000379 static Constant *MemmoveFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000380 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000381 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000382 break;
383 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000384 case Intrinsic::memset_i32: {
Chris Lattner34acba42007-01-07 08:12:01 +0000385 static Constant *MemsetFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000386 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000387 (*(CI->op_begin()+1))->getType(), MemsetFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000388 }
Chris Lattner093c1592006-03-03 00:00:25 +0000389 case Intrinsic::memset_i64: {
Chris Lattner34acba42007-01-07 08:12:01 +0000390 static Constant *MemsetFCache = 0;
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000391 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
Chris Lattner34acba42007-01-07 08:12:01 +0000392 (*(CI->op_begin()+1))->getType(), MemsetFCache);
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000393 break;
394 }
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000395 case Intrinsic::isunordered_f32:
396 case Intrinsic::isunordered_f64: {
Alkis Evlogimenosb3846f42005-03-01 02:07:58 +0000397 Value *L = CI->getOperand(1);
398 Value *R = CI->getOperand(2);
399
Reid Spencer266e42b2006-12-23 06:05:41 +0000400 Value *LIsNan = new FCmpInst(FCmpInst::FCMP_ONE, L, L, "LIsNan", CI);
401 Value *RIsNan = new FCmpInst(FCmpInst::FCMP_ONE, R, R, "RIsNan", CI);
Alkis Evlogimenosb3846f42005-03-01 02:07:58 +0000402 CI->replaceAllUsesWith(
403 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
404 "isunordered", CI));
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000405 break;
406 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000407 case Intrinsic::sqrt_f32: {
Chris Lattner34acba42007-01-07 08:12:01 +0000408 static Constant *sqrtfFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000409 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000410 Type::FloatTy, sqrtfFCache);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000411 break;
412 }
413 case Intrinsic::sqrt_f64: {
Chris Lattner34acba42007-01-07 08:12:01 +0000414 static Constant *sqrtFCache = 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000415 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattner34acba42007-01-07 08:12:01 +0000416 Type::DoubleTy, sqrtFCache);
Chris Lattner30fe4ac22005-04-30 04:07:50 +0000417 break;
418 }
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000419 }
Misha Brukman835702a2005-04-21 22:36:52 +0000420
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000421 assert(CI->use_empty() &&
422 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner9ec975a2005-05-11 19:42:05 +0000423 CI->eraseFromParent();
Chris Lattnerbcdadf32004-06-20 07:49:54 +0000424}