blob: 2c286608266b6decee37edcfee6b12ae98db21fb [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the IntrinsicLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
16#include "llvm/Module.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Type.h"
18#include "llvm/CodeGen/IntrinsicLowering.h"
Jay Foade4094882009-05-12 20:27:44 +000019#include "llvm/Support/IRBuilder.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000020#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/ADT/SmallVector.h"
23using namespace llvm;
24
25template <class ArgIt>
26static void EnsureFunctionExists(Module &M, const char *Name,
27 ArgIt ArgBegin, ArgIt ArgEnd,
28 const Type *RetTy) {
29 // Insert a correctly-typed definition now.
30 std::vector<const Type *> ParamTys;
31 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
32 ParamTys.push_back(I->getType());
33 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
34}
35
Chris Lattnerba443852009-02-07 22:37:06 +000036static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
37 const char *FName,
38 const char *DName, const char *LDName) {
Dale Johannesen4f35283a2008-09-22 19:51:58 +000039 // Insert definitions for all the floating point types.
Chris Lattnerba443852009-02-07 22:37:06 +000040 switch((int)Fn->arg_begin()->getType()->getTypeID()) {
Dale Johannesen4f35283a2008-09-22 19:51:58 +000041 case Type::FloatTyID:
Chris Lattnerba443852009-02-07 22:37:06 +000042 EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
Dale Johannesen4f35283a2008-09-22 19:51:58 +000043 Type::FloatTy);
Chris Lattnerba443852009-02-07 22:37:06 +000044 break;
Dale Johannesen4f35283a2008-09-22 19:51:58 +000045 case Type::DoubleTyID:
Chris Lattnerba443852009-02-07 22:37:06 +000046 EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
Dale Johannesen4f35283a2008-09-22 19:51:58 +000047 Type::DoubleTy);
Chris Lattnerba443852009-02-07 22:37:06 +000048 break;
Dale Johannesen4f35283a2008-09-22 19:51:58 +000049 case Type::X86_FP80TyID:
50 case Type::FP128TyID:
51 case Type::PPC_FP128TyID:
Chris Lattnerba443852009-02-07 22:37:06 +000052 EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
53 Fn->arg_begin()->getType());
54 break;
Dale Johannesen4f35283a2008-09-22 19:51:58 +000055 }
56}
57
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058/// ReplaceCallWith - This function is used when we want to lower an intrinsic
59/// call to a call of an external function. This handles hard cases such as
60/// when there was already a prototype for the external function, and if that
61/// prototype doesn't match the arguments we expect to pass in.
62template <class ArgIt>
63static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
64 ArgIt ArgBegin, ArgIt ArgEnd,
Owen Anderson41d6d6c2009-06-26 20:33:47 +000065 const Type *RetTy) {
66 // If we haven't already looked up this function, check to see if the
67 // program already contains a function with this name.
68 Module *M = CI->getParent()->getParent()->getParent();
69 // Get or insert the definition now.
70 std::vector<const Type *> ParamTys;
71 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
72 ParamTys.push_back((*I)->getType());
73 Constant* FCache = M->getOrInsertFunction(NewFn,
74 FunctionType::get(RetTy, ParamTys, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075
Jay Foade4094882009-05-12 20:27:44 +000076 IRBuilder<> Builder(CI->getParent(), CI);
David Greeneb1c4a7b2007-08-01 03:43:44 +000077 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Jay Foade4094882009-05-12 20:27:44 +000078 CallInst *NewCI = Builder.CreateCall(FCache, Args.begin(), Args.end());
79 NewCI->setName(CI->getName());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 if (!CI->use_empty())
81 CI->replaceAllUsesWith(NewCI);
82 return NewCI;
83}
84
85void IntrinsicLowering::AddPrototypes(Module &M) {
86 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
87 if (I->isDeclaration() && !I->use_empty())
88 switch (I->getIntrinsicID()) {
89 default: break;
90 case Intrinsic::setjmp:
91 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
92 Type::Int32Ty);
93 break;
94 case Intrinsic::longjmp:
95 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
96 Type::VoidTy);
97 break;
98 case Intrinsic::siglongjmp:
99 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
100 Type::VoidTy);
101 break;
Chris Lattner82c2e432008-11-21 16:42:48 +0000102 case Intrinsic::memcpy:
Christopher Lambbb2f2222007-12-17 01:12:55 +0000103 M.getOrInsertFunction("memcpy", PointerType::getUnqual(Type::Int8Ty),
104 PointerType::getUnqual(Type::Int8Ty),
105 PointerType::getUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 TD.getIntPtrType(), (Type *)0);
107 break;
Chris Lattner82c2e432008-11-21 16:42:48 +0000108 case Intrinsic::memmove:
Christopher Lambbb2f2222007-12-17 01:12:55 +0000109 M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty),
110 PointerType::getUnqual(Type::Int8Ty),
111 PointerType::getUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 TD.getIntPtrType(), (Type *)0);
113 break;
Chris Lattner82c2e432008-11-21 16:42:48 +0000114 case Intrinsic::memset:
Christopher Lambbb2f2222007-12-17 01:12:55 +0000115 M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty),
116 PointerType::getUnqual(Type::Int8Ty),
117 Type::Int32Ty,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 TD.getIntPtrType(), (Type *)0);
119 break;
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000120 case Intrinsic::sqrt:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000121 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 break;
Dan Gohman02f9ed92007-10-15 22:07:31 +0000123 case Intrinsic::sin:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000124 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000125 break;
126 case Intrinsic::cos:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000127 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000128 break;
129 case Intrinsic::pow:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000130 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000131 break;
Dale Johannesen92b33082008-09-04 00:47:13 +0000132 case Intrinsic::log:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000133 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000134 break;
135 case Intrinsic::log2:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000136 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000137 break;
138 case Intrinsic::log10:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000139 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000140 break;
141 case Intrinsic::exp:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000142 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000143 break;
144 case Intrinsic::exp2:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000145 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000146 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 }
148}
149
150/// LowerBSWAP - Emit the code to lower bswap of V before the specified
151/// instruction IP.
152static Value *LowerBSWAP(Value *V, Instruction *IP) {
153 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
154
155 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
156
Jay Foade4094882009-05-12 20:27:44 +0000157 IRBuilder<> Builder(IP->getParent(), IP);
158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 switch(BitSize) {
Edwin Török675d5622009-07-11 20:10:48 +0000160 default: LLVM_UNREACHABLE("Unhandled type size of value to byteswap!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 case 16: {
Jay Foade4094882009-05-12 20:27:44 +0000162 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
163 "bswap.2");
164 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
165 "bswap.1");
166 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 break;
168 }
169 case 32: {
Jay Foade4094882009-05-12 20:27:44 +0000170 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
171 "bswap.4");
172 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
173 "bswap.3");
174 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
175 "bswap.2");
176 Value *Tmp1 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 24),
177 "bswap.1");
178 Tmp3 = Builder.CreateAnd(Tmp3, ConstantInt::get(Type::Int32Ty, 0xFF0000),
179 "bswap.and3");
180 Tmp2 = Builder.CreateAnd(Tmp2, ConstantInt::get(Type::Int32Ty, 0xFF00),
181 "bswap.and2");
182 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
183 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
184 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 break;
186 }
187 case 64: {
Jay Foade4094882009-05-12 20:27:44 +0000188 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
189 "bswap.8");
190 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
191 "bswap.7");
192 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
193 "bswap.6");
194 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
195 "bswap.5");
196 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
197 "bswap.4");
198 Value* Tmp3 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 24),
199 "bswap.3");
200 Value* Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 40),
201 "bswap.2");
202 Value* Tmp1 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 56),
203 "bswap.1");
204 Tmp7 = Builder.CreateAnd(Tmp7,
205 ConstantInt::get(Type::Int64Ty,
206 0xFF000000000000ULL),
207 "bswap.and7");
208 Tmp6 = Builder.CreateAnd(Tmp6,
209 ConstantInt::get(Type::Int64Ty,
210 0xFF0000000000ULL),
211 "bswap.and6");
212 Tmp5 = Builder.CreateAnd(Tmp5,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Jay Foade4094882009-05-12 20:27:44 +0000214 "bswap.and5");
215 Tmp4 = Builder.CreateAnd(Tmp4,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Jay Foade4094882009-05-12 20:27:44 +0000217 "bswap.and4");
218 Tmp3 = Builder.CreateAnd(Tmp3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Jay Foade4094882009-05-12 20:27:44 +0000220 "bswap.and3");
221 Tmp2 = Builder.CreateAnd(Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Jay Foade4094882009-05-12 20:27:44 +0000223 "bswap.and2");
224 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
225 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
226 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
227 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
228 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
229 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
230 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 break;
232 }
233 }
234 return V;
235}
236
237/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
238/// instruction IP.
239static Value *LowerCTPOP(Value *V, Instruction *IP) {
240 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
241
242 static const uint64_t MaskValues[6] = {
243 0x5555555555555555ULL, 0x3333333333333333ULL,
244 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
245 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
246 };
247
Jay Foade4094882009-05-12 20:27:44 +0000248 IRBuilder<> Builder(IP->getParent(), IP);
249
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
251 unsigned WordSize = (BitSize + 63) / 64;
252 Value *Count = ConstantInt::get(V->getType(), 0);
253
254 for (unsigned n = 0; n < WordSize; ++n) {
255 Value *PartValue = V;
256 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
257 i <<= 1, ++ct) {
258 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Jay Foade4094882009-05-12 20:27:44 +0000259 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
260 Value *VShift = Builder.CreateLShr(PartValue,
261 ConstantInt::get(V->getType(), i),
262 "ctpop.sh");
263 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
264 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 }
Jay Foade4094882009-05-12 20:27:44 +0000266 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 if (BitSize > 64) {
Jay Foade4094882009-05-12 20:27:44 +0000268 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
269 "ctpop.part.sh");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 BitSize -= 64;
271 }
272 }
273
Chris Lattnerf2cf7f02007-08-06 16:36:18 +0000274 return Count;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275}
276
277/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
278/// instruction IP.
279static Value *LowerCTLZ(Value *V, Instruction *IP) {
280
Jay Foade4094882009-05-12 20:27:44 +0000281 IRBuilder<> Builder(IP->getParent(), IP);
282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
284 for (unsigned i = 1; i < BitSize; i <<= 1) {
285 Value *ShVal = ConstantInt::get(V->getType(), i);
Jay Foade4094882009-05-12 20:27:44 +0000286 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
287 V = Builder.CreateOr(V, ShVal, "ctlz.step");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 }
289
Jay Foade4094882009-05-12 20:27:44 +0000290 V = Builder.CreateNot(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 return LowerCTPOP(V, IP);
292}
293
294/// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes
295/// three integer arguments. The first argument is the Value from which the
296/// bits will be selected. It may be of any bit width. The second and third
297/// arguments specify a range of bits to select with the second argument
298/// specifying the low bit and the third argument specifying the high bit. Both
299/// must be type i32. The result is the corresponding selected bits from the
300/// Value in the same width as the Value (first argument). If the low bit index
301/// is higher than the high bit index then the inverse selection is done and
302/// the bits are returned in inverse order.
303/// @brief Lowering of llvm.part.select intrinsic.
304static Instruction *LowerPartSelect(CallInst *CI) {
Owen Andersonbaf982b2009-07-08 20:50:47 +0000305 IRBuilder<> Builder(*CI->getParent()->getContext());
Jay Foade4094882009-05-12 20:27:44 +0000306
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 // Make sure we're dealing with a part select intrinsic here
308 Function *F = CI->getCalledFunction();
309 const FunctionType *FT = F->getFunctionType();
310 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
311 FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
312 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
313 return CI;
314
315 // Get the intrinsic implementation function by converting all the . to _
316 // in the intrinsic's function name and then reconstructing the function
317 // declaration.
318 std::string Name(F->getName());
319 for (unsigned i = 4; i < Name.length(); ++i)
320 if (Name[i] == '.')
321 Name[i] = '_';
322 Module* M = F->getParent();
323 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Duncan Sands19d161f2009-03-07 15:45:40 +0000324 F->setLinkage(GlobalValue::WeakAnyLinkage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
326 // If we haven't defined the impl function yet, do so now
327 if (F->isDeclaration()) {
328
329 // Get the arguments to the function
330 Function::arg_iterator args = F->arg_begin();
331 Value* Val = args++; Val->setName("Val");
332 Value* Lo = args++; Lo->setName("Lo");
Gabor Greifd6da1d02008-04-06 20:25:17 +0000333 Value* Hi = args++; Hi->setName("High");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334
335 // We want to select a range of bits here such that [Hi, Lo] is shifted
336 // down to the low bits. However, it is quite possible that Hi is smaller
337 // than Lo in which case the bits have to be reversed.
338
339 // Create the blocks we will need for the two cases (forward, reverse)
Gabor Greifd6da1d02008-04-06 20:25:17 +0000340 BasicBlock* CurBB = BasicBlock::Create("entry", F);
341 BasicBlock *RevSize = BasicBlock::Create("revsize", CurBB->getParent());
342 BasicBlock *FwdSize = BasicBlock::Create("fwdsize", CurBB->getParent());
343 BasicBlock *Compute = BasicBlock::Create("compute", CurBB->getParent());
344 BasicBlock *Reverse = BasicBlock::Create("reverse", CurBB->getParent());
345 BasicBlock *RsltBlk = BasicBlock::Create("result", CurBB->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346
Jay Foade4094882009-05-12 20:27:44 +0000347 Builder.SetInsertPoint(CurBB);
348
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 // Cast Hi and Lo to the size of Val so the widths are all the same
350 if (Hi->getType() != Val->getType())
Jay Foade4094882009-05-12 20:27:44 +0000351 Hi = Builder.CreateIntCast(Hi, Val->getType(), /* isSigned */ false,
352 "tmp");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 if (Lo->getType() != Val->getType())
Jay Foade4094882009-05-12 20:27:44 +0000354 Lo = Builder.CreateIntCast(Lo, Val->getType(), /* isSigned */ false,
355 "tmp");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356
357 // Compute a few things that both cases will need, up front.
358 Constant* Zero = ConstantInt::get(Val->getType(), 0);
359 Constant* One = ConstantInt::get(Val->getType(), 1);
360 Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
361
362 // Compare the Hi and Lo bit positions. This is used to determine
363 // which case we have (forward or reverse)
Jay Foade4094882009-05-12 20:27:44 +0000364 Value *Cmp = Builder.CreateICmpULT(Hi, Lo, "less");
365 Builder.CreateCondBr(Cmp, RevSize, FwdSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366
Jay Foade4094882009-05-12 20:27:44 +0000367 // First, compute the number of bits in the forward case.
368 Builder.SetInsertPoint(FwdSize);
369 Value* FBitSize = Builder.CreateSub(Hi, Lo, "fbits");
370 Builder.CreateBr(Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371
372 // Second, compute the number of bits in the reverse case.
Jay Foade4094882009-05-12 20:27:44 +0000373 Builder.SetInsertPoint(RevSize);
374 Value* RBitSize = Builder.CreateSub(Lo, Hi, "rbits");
375 Builder.CreateBr(Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
377 // Now, compute the bit range. Start by getting the bitsize and the shift
378 // amount (either Hi or Lo) from PHI nodes. Then we compute a mask for
379 // the number of bits we want in the range. We shift the bits down to the
380 // least significant bits, apply the mask to zero out unwanted high bits,
381 // and we have computed the "forward" result. It may still need to be
382 // reversed.
Jay Foade4094882009-05-12 20:27:44 +0000383 Builder.SetInsertPoint(Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384
385 // Get the BitSize from one of the two subtractions
Jay Foade4094882009-05-12 20:27:44 +0000386 PHINode *BitSize = Builder.CreatePHI(Val->getType(), "bits");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 BitSize->reserveOperandSpace(2);
388 BitSize->addIncoming(FBitSize, FwdSize);
389 BitSize->addIncoming(RBitSize, RevSize);
390
391 // Get the ShiftAmount as the smaller of Hi/Lo
Jay Foade4094882009-05-12 20:27:44 +0000392 PHINode *ShiftAmt = Builder.CreatePHI(Val->getType(), "shiftamt");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 ShiftAmt->reserveOperandSpace(2);
394 ShiftAmt->addIncoming(Lo, FwdSize);
395 ShiftAmt->addIncoming(Hi, RevSize);
396
397 // Increment the bit size
Jay Foade4094882009-05-12 20:27:44 +0000398 Value *BitSizePlusOne = Builder.CreateAdd(BitSize, One, "bits");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399
400 // Create a Mask to zero out the high order bits.
Jay Foade4094882009-05-12 20:27:44 +0000401 Value* Mask = Builder.CreateShl(AllOnes, BitSizePlusOne, "mask");
402 Mask = Builder.CreateNot(Mask, "mask");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403
404 // Shift the bits down and apply the mask
Jay Foade4094882009-05-12 20:27:44 +0000405 Value* FRes = Builder.CreateLShr(Val, ShiftAmt, "fres");
406 FRes = Builder.CreateAnd(FRes, Mask, "fres");
407 Builder.CreateCondBr(Cmp, Reverse, RsltBlk);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408
409 // In the Reverse block we have the mask already in FRes but we must reverse
410 // it by shifting FRes bits right and putting them in RRes by shifting them
411 // in from left.
Jay Foade4094882009-05-12 20:27:44 +0000412 Builder.SetInsertPoint(Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413
414 // First set up our loop counters
Jay Foade4094882009-05-12 20:27:44 +0000415 PHINode *Count = Builder.CreatePHI(Val->getType(), "count");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 Count->reserveOperandSpace(2);
417 Count->addIncoming(BitSizePlusOne, Compute);
418
419 // Next, get the value that we are shifting.
Jay Foade4094882009-05-12 20:27:44 +0000420 PHINode *BitsToShift = Builder.CreatePHI(Val->getType(), "val");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 BitsToShift->reserveOperandSpace(2);
422 BitsToShift->addIncoming(FRes, Compute);
423
424 // Finally, get the result of the last computation
Jay Foade4094882009-05-12 20:27:44 +0000425 PHINode *RRes = Builder.CreatePHI(Val->getType(), "rres");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 RRes->reserveOperandSpace(2);
427 RRes->addIncoming(Zero, Compute);
428
429 // Decrement the counter
Jay Foade4094882009-05-12 20:27:44 +0000430 Value *Decr = Builder.CreateSub(Count, One, "decr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 Count->addIncoming(Decr, Reverse);
432
433 // Compute the Bit that we want to move
Jay Foade4094882009-05-12 20:27:44 +0000434 Value *Bit = Builder.CreateAnd(BitsToShift, One, "bit");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435
436 // Compute the new value for next iteration.
Jay Foade4094882009-05-12 20:27:44 +0000437 Value *NewVal = Builder.CreateLShr(BitsToShift, One, "rshift");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 BitsToShift->addIncoming(NewVal, Reverse);
439
440 // Shift the bit into the low bits of the result.
Jay Foade4094882009-05-12 20:27:44 +0000441 Value *NewRes = Builder.CreateShl(RRes, One, "lshift");
442 NewRes = Builder.CreateOr(NewRes, Bit, "addbit");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 RRes->addIncoming(NewRes, Reverse);
444
445 // Terminate loop if we've moved all the bits.
Jay Foade4094882009-05-12 20:27:44 +0000446 Value *Cond = Builder.CreateICmpEQ(Decr, Zero, "cond");
447 Builder.CreateCondBr(Cond, RsltBlk, Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448
449 // Finally, in the result block, select one of the two results with a PHI
450 // node and return the result;
Jay Foade4094882009-05-12 20:27:44 +0000451 Builder.SetInsertPoint(RsltBlk);
452 PHINode *BitSelect = Builder.CreatePHI(Val->getType(), "part_select");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 BitSelect->reserveOperandSpace(2);
454 BitSelect->addIncoming(FRes, Compute);
455 BitSelect->addIncoming(NewRes, Reverse);
Jay Foade4094882009-05-12 20:27:44 +0000456 Builder.CreateRet(BitSelect);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 }
458
459 // Return a call to the implementation function
Jay Foade4094882009-05-12 20:27:44 +0000460 Builder.SetInsertPoint(CI->getParent(), CI);
461 CallInst *NewCI = Builder.CreateCall3(F, CI->getOperand(1),
462 CI->getOperand(2), CI->getOperand(3));
463 NewCI->setName(CI->getName());
464 return NewCI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465}
466
467/// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes
468/// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High)
469/// The first two arguments can be any bit width. The result is the same width
470/// as %Value. The operation replaces bits between %Low and %High with the value
471/// in %Replacement. If %Replacement is not the same width, it is truncated or
472/// zero extended as appropriate to fit the bits being replaced. If %Low is
473/// greater than %High then the inverse set of bits are replaced.
474/// @brief Lowering of llvm.bit.part.set intrinsic.
475static Instruction *LowerPartSet(CallInst *CI) {
Owen Andersonbaf982b2009-07-08 20:50:47 +0000476 IRBuilder<> Builder(*CI->getParent()->getContext());
Jay Foade4094882009-05-12 20:27:44 +0000477
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 // Make sure we're dealing with a part select intrinsic here
479 Function *F = CI->getCalledFunction();
480 const FunctionType *FT = F->getFunctionType();
481 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
482 FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() ||
483 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
484 !FT->getParamType(3)->isInteger())
485 return CI;
486
487 // Get the intrinsic implementation function by converting all the . to _
488 // in the intrinsic's function name and then reconstructing the function
489 // declaration.
490 std::string Name(F->getName());
491 for (unsigned i = 4; i < Name.length(); ++i)
492 if (Name[i] == '.')
493 Name[i] = '_';
494 Module* M = F->getParent();
495 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Duncan Sands19d161f2009-03-07 15:45:40 +0000496 F->setLinkage(GlobalValue::WeakAnyLinkage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497
498 // If we haven't defined the impl function yet, do so now
499 if (F->isDeclaration()) {
500 // Get the arguments for the function.
501 Function::arg_iterator args = F->arg_begin();
502 Value* Val = args++; Val->setName("Val");
503 Value* Rep = args++; Rep->setName("Rep");
504 Value* Lo = args++; Lo->setName("Lo");
505 Value* Hi = args++; Hi->setName("Hi");
506
507 // Get some types we need
508 const IntegerType* ValTy = cast<IntegerType>(Val->getType());
509 const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 uint32_t RepBits = RepTy->getBitWidth();
511
512 // Constant Definitions
513 ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
514 ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
515 ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
516 ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1);
517 ConstantInt* ValOne = ConstantInt::get(ValTy, 1);
518 ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0);
519 ConstantInt* ValZero = ConstantInt::get(ValTy, 0);
520
521 // Basic blocks we fill in below.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000522 BasicBlock* entry = BasicBlock::Create("entry", F, 0);
523 BasicBlock* large = BasicBlock::Create("large", F, 0);
524 BasicBlock* small = BasicBlock::Create("small", F, 0);
525 BasicBlock* reverse = BasicBlock::Create("reverse", F, 0);
526 BasicBlock* result = BasicBlock::Create("result", F, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527
528 // BASIC BLOCK: entry
Jay Foade4094882009-05-12 20:27:44 +0000529 Builder.SetInsertPoint(entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 // First, get the number of bits that we're placing as an i32
Jay Foade4094882009-05-12 20:27:44 +0000531 Value* is_forward = Builder.CreateICmpULT(Lo, Hi);
532 Value* Hi_pn = Builder.CreateSelect(is_forward, Hi, Lo);
533 Value* Lo_pn = Builder.CreateSelect(is_forward, Lo, Hi);
534 Value* NumBits = Builder.CreateSub(Hi_pn, Lo_pn);
535 NumBits = Builder.CreateAdd(NumBits, One);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 // Now, convert Lo and Hi to ValTy bit width
Jay Foade4094882009-05-12 20:27:44 +0000537 Lo = Builder.CreateIntCast(Lo_pn, ValTy, /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 // Determine if the replacement bits are larger than the number of bits we
539 // are replacing and deal with it.
Jay Foade4094882009-05-12 20:27:44 +0000540 Value* is_large = Builder.CreateICmpULT(NumBits, RepBitWidth);
541 Builder.CreateCondBr(is_large, large, small);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542
543 // BASIC BLOCK: large
Jay Foade4094882009-05-12 20:27:44 +0000544 Builder.SetInsertPoint(large);
545 Value* MaskBits = Builder.CreateSub(RepBitWidth, NumBits);
546 MaskBits = Builder.CreateIntCast(MaskBits, RepMask->getType(),
547 /* isSigned */ false);
548 Value* Mask1 = Builder.CreateLShr(RepMask, MaskBits);
549 Value* Rep2 = Builder.CreateAnd(Mask1, Rep);
550 Builder.CreateBr(small);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551
552 // BASIC BLOCK: small
Jay Foade4094882009-05-12 20:27:44 +0000553 Builder.SetInsertPoint(small);
554 PHINode* Rep3 = Builder.CreatePHI(RepTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 Rep3->reserveOperandSpace(2);
556 Rep3->addIncoming(Rep2, large);
557 Rep3->addIncoming(Rep, entry);
Jay Foade4094882009-05-12 20:27:44 +0000558 Value* Rep4 = Builder.CreateIntCast(Rep3, ValTy, /* isSigned */ false);
559 Builder.CreateCondBr(is_forward, result, reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560
561 // BASIC BLOCK: reverse (reverses the bits of the replacement)
Jay Foade4094882009-05-12 20:27:44 +0000562 Builder.SetInsertPoint(reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 // Set up our loop counter as a PHI so we can decrement on each iteration.
564 // We will loop for the number of bits in the replacement value.
Jay Foade4094882009-05-12 20:27:44 +0000565 PHINode *Count = Builder.CreatePHI(Type::Int32Ty, "count");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 Count->reserveOperandSpace(2);
567 Count->addIncoming(NumBits, small);
568
569 // Get the value that we are shifting bits out of as a PHI because
570 // we'll change this with each iteration.
Jay Foade4094882009-05-12 20:27:44 +0000571 PHINode *BitsToShift = Builder.CreatePHI(Val->getType(), "val");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 BitsToShift->reserveOperandSpace(2);
573 BitsToShift->addIncoming(Rep4, small);
574
575 // Get the result of the last computation or zero on first iteration
Jay Foade4094882009-05-12 20:27:44 +0000576 PHINode *RRes = Builder.CreatePHI(Val->getType(), "rres");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 RRes->reserveOperandSpace(2);
578 RRes->addIncoming(ValZero, small);
579
580 // Decrement the loop counter by one
Jay Foade4094882009-05-12 20:27:44 +0000581 Value *Decr = Builder.CreateSub(Count, One);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 Count->addIncoming(Decr, reverse);
583
584 // Get the bit that we want to move into the result
Jay Foade4094882009-05-12 20:27:44 +0000585 Value *Bit = Builder.CreateAnd(BitsToShift, ValOne);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586
587 // Compute the new value of the bits to shift for the next iteration.
Jay Foade4094882009-05-12 20:27:44 +0000588 Value *NewVal = Builder.CreateLShr(BitsToShift, ValOne);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 BitsToShift->addIncoming(NewVal, reverse);
590
591 // Shift the bit we extracted into the low bit of the result.
Jay Foade4094882009-05-12 20:27:44 +0000592 Value *NewRes = Builder.CreateShl(RRes, ValOne);
593 NewRes = Builder.CreateOr(NewRes, Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 RRes->addIncoming(NewRes, reverse);
595
596 // Terminate loop if we've moved all the bits.
Jay Foade4094882009-05-12 20:27:44 +0000597 Value *Cond = Builder.CreateICmpEQ(Decr, Zero);
598 Builder.CreateCondBr(Cond, result, reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599
600 // BASIC BLOCK: result
Jay Foade4094882009-05-12 20:27:44 +0000601 Builder.SetInsertPoint(result);
602 PHINode *Rplcmnt = Builder.CreatePHI(Val->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 Rplcmnt->reserveOperandSpace(2);
604 Rplcmnt->addIncoming(NewRes, reverse);
605 Rplcmnt->addIncoming(Rep4, small);
Jay Foade4094882009-05-12 20:27:44 +0000606 Value* t0 = Builder.CreateIntCast(NumBits, ValTy, /* isSigned */ false);
607 Value* t1 = Builder.CreateShl(ValMask, Lo);
608 Value* t2 = Builder.CreateNot(t1);
609 Value* t3 = Builder.CreateShl(t1, t0);
610 Value* t4 = Builder.CreateOr(t2, t3);
611 Value* t5 = Builder.CreateAnd(t4, Val);
612 Value* t6 = Builder.CreateShl(Rplcmnt, Lo);
613 Value* Rslt = Builder.CreateOr(t5, t6, "part_set");
614 Builder.CreateRet(Rslt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 }
616
617 // Return a call to the implementation function
Jay Foade4094882009-05-12 20:27:44 +0000618 Builder.SetInsertPoint(CI->getParent(), CI);
619 CallInst *NewCI = Builder.CreateCall4(F, CI->getOperand(1),
620 CI->getOperand(2), CI->getOperand(3),
621 CI->getOperand(4));
622 NewCI->setName(CI->getName());
623 return NewCI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624}
625
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000626static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
627 const char *Dname,
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000628 const char *LDname) {
629 switch (CI->getOperand(1)->getType()->getTypeID()) {
Edwin Törökced9ff82009-07-11 13:10:19 +0000630 default: LLVM_UNREACHABLE( "Invalid type in intrinsic");
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000631 case Type::FloatTyID:
Jay Foade4094882009-05-12 20:27:44 +0000632 ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000633 Type::FloatTy);
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000634 break;
635 case Type::DoubleTyID:
Jay Foade4094882009-05-12 20:27:44 +0000636 ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000637 Type::DoubleTy);
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000638 break;
639 case Type::X86_FP80TyID:
640 case Type::FP128TyID:
641 case Type::PPC_FP128TyID:
Jay Foade4094882009-05-12 20:27:44 +0000642 ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000643 CI->getOperand(1)->getType());
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000644 break;
645 }
646}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647
648void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Jay Foade4094882009-05-12 20:27:44 +0000649 IRBuilder<> Builder(CI->getParent(), CI);
650
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 Function *Callee = CI->getCalledFunction();
652 assert(Callee && "Cannot lower an indirect call!");
653
654 switch (Callee->getIntrinsicID()) {
655 case Intrinsic::not_intrinsic:
Edwin Törökced9ff82009-07-11 13:10:19 +0000656 llvm_report_error("Cannot lower a call to a non-intrinsic function '"+
657 Callee->getName() + "'!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 default:
Edwin Törökced9ff82009-07-11 13:10:19 +0000659 llvm_report_error("Code generator does not support intrinsic function '"+
660 Callee->getName()+"'!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661
662 // The setjmp/longjmp intrinsics should only exist in the code if it was
663 // never optimized (ie, right out of the CFE), or if it has been hacked on
664 // by the lowerinvoke pass. In both cases, the right thing to do is to
665 // convert the call to an explicit setjmp or longjmp call.
666 case Intrinsic::setjmp: {
Jay Foade4094882009-05-12 20:27:44 +0000667 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000668 Type::Int32Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 if (CI->getType() != Type::VoidTy)
670 CI->replaceAllUsesWith(V);
671 break;
672 }
673 case Intrinsic::sigsetjmp:
674 if (CI->getType() != Type::VoidTy)
675 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
676 break;
677
678 case Intrinsic::longjmp: {
Jay Foade4094882009-05-12 20:27:44 +0000679 ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000680 Type::VoidTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 break;
682 }
683
684 case Intrinsic::siglongjmp: {
685 // Insert the call to abort
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000687 Type::VoidTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 break;
689 }
690 case Intrinsic::ctpop:
691 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
692 break;
693
694 case Intrinsic::bswap:
695 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
696 break;
697
698 case Intrinsic::ctlz:
699 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
700 break;
701
702 case Intrinsic::cttz: {
703 // cttz(x) -> ctpop(~X & (X-1))
704 Value *Src = CI->getOperand(1);
Jay Foade4094882009-05-12 20:27:44 +0000705 Value *NotSrc = Builder.CreateNot(Src);
706 NotSrc->setName(Src->getName() + ".not");
Gabor Greifd6da1d02008-04-06 20:25:17 +0000707 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Jay Foade4094882009-05-12 20:27:44 +0000708 SrcM1 = Builder.CreateSub(Src, SrcM1);
709 Src = LowerCTPOP(Builder.CreateAnd(NotSrc, SrcM1), CI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 CI->replaceAllUsesWith(Src);
711 break;
712 }
713
714 case Intrinsic::part_select:
715 CI->replaceAllUsesWith(LowerPartSelect(CI));
716 break;
717
718 case Intrinsic::part_set:
719 CI->replaceAllUsesWith(LowerPartSet(CI));
720 break;
721
722 case Intrinsic::stacksave:
723 case Intrinsic::stackrestore: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 if (!Warned)
725 cerr << "WARNING: this target does not support the llvm.stack"
726 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
727 "save" : "restore") << " intrinsic.\n";
728 Warned = true;
729 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
730 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
731 break;
732 }
733
734 case Intrinsic::returnaddress:
735 case Intrinsic::frameaddress:
736 cerr << "WARNING: this target does not support the llvm."
737 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
738 "return" : "frame") << "address intrinsic.\n";
739 CI->replaceAllUsesWith(ConstantPointerNull::get(
740 cast<PointerType>(CI->getType())));
741 break;
742
743 case Intrinsic::prefetch:
744 break; // Simply strip out prefetches on unsupported architectures
745
746 case Intrinsic::pcmarker:
747 break; // Simply strip out pcmarker on unsupported architectures
748 case Intrinsic::readcyclecounter: {
749 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
750 << "ter intrinsic. It is being lowered to a constant 0\n";
751 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
752 break;
753 }
754
755 case Intrinsic::dbg_stoppoint:
756 case Intrinsic::dbg_region_start:
757 case Intrinsic::dbg_region_end:
758 case Intrinsic::dbg_func_start:
759 case Intrinsic::dbg_declare:
760 break; // Simply strip out debugging intrinsics
761
762 case Intrinsic::eh_exception:
Anton Korobeynikov94c46a02007-09-07 11:39:35 +0000763 case Intrinsic::eh_selector_i32:
764 case Intrinsic::eh_selector_i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
766 break;
767
Anton Korobeynikov94c46a02007-09-07 11:39:35 +0000768 case Intrinsic::eh_typeid_for_i32:
769 case Intrinsic::eh_typeid_for_i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770 // Return something different to eh_selector.
771 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
772 break;
773
774 case Intrinsic::var_annotation:
775 break; // Strip out annotate intrinsic
776
Chris Lattner82c2e432008-11-21 16:42:48 +0000777 case Intrinsic::memcpy: {
Jay Foad4c4839d2009-05-11 11:32:25 +0000778 const IntegerType *IntPtr = TD.getIntPtrType();
Jay Foade4094882009-05-12 20:27:44 +0000779 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
780 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 Value *Ops[3];
782 Ops[0] = CI->getOperand(1);
783 Ops[1] = CI->getOperand(2);
784 Ops[2] = Size;
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000785 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 break;
787 }
Chris Lattner82c2e432008-11-21 16:42:48 +0000788 case Intrinsic::memmove: {
Jay Foad4c4839d2009-05-11 11:32:25 +0000789 const IntegerType *IntPtr = TD.getIntPtrType();
Jay Foade4094882009-05-12 20:27:44 +0000790 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
791 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 Value *Ops[3];
793 Ops[0] = CI->getOperand(1);
794 Ops[1] = CI->getOperand(2);
795 Ops[2] = Size;
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000796 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 break;
798 }
Chris Lattner82c2e432008-11-21 16:42:48 +0000799 case Intrinsic::memset: {
Jay Foad4c4839d2009-05-11 11:32:25 +0000800 const IntegerType *IntPtr = TD.getIntPtrType();
Jay Foade4094882009-05-12 20:27:44 +0000801 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
802 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 Value *Ops[3];
804 Ops[0] = CI->getOperand(1);
805 // Extend the amount to i32.
Jay Foade4094882009-05-12 20:27:44 +0000806 Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::Int32Ty,
807 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 Ops[2] = Size;
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000809 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 break;
811 }
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000812 case Intrinsic::sqrt: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000813 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
Dale Johannesen3b5303b2007-09-28 18:06:58 +0000814 break;
815 }
Dale Johannesen92b33082008-09-04 00:47:13 +0000816 case Intrinsic::log: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000817 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000818 break;
819 }
820 case Intrinsic::log2: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000821 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000822 break;
823 }
824 case Intrinsic::log10: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000825 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000826 break;
827 }
828 case Intrinsic::exp: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000829 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000830 break;
831 }
832 case Intrinsic::exp2: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000833 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000834 break;
835 }
836 case Intrinsic::pow: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000837 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000838 break;
839 }
Anton Korobeynikovc915e272007-11-15 23:25:33 +0000840 case Intrinsic::flt_rounds:
841 // Lower to "round to the nearest"
842 if (CI->getType() != Type::VoidTy)
843 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
844 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 }
846
847 assert(CI->use_empty() &&
848 "Lowering should have eliminated any uses of the intrinsic call!");
849 CI->eraseFromParent();
850}