blob: 64a5c9e699e46f785b14de578afd8daee1a23650 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Target/TargetData.h"
21#include "llvm/ADT/SmallVector.h"
22using namespace llvm;
23
24template <class ArgIt>
25static void EnsureFunctionExists(Module &M, const char *Name,
26 ArgIt ArgBegin, ArgIt ArgEnd,
27 const Type *RetTy) {
28 // Insert a correctly-typed definition now.
29 std::vector<const Type *> ParamTys;
30 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
31 ParamTys.push_back(I->getType());
32 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
33}
34
Chris Lattnerba443852009-02-07 22:37:06 +000035static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
36 const char *FName,
37 const char *DName, const char *LDName) {
Dale Johannesen4f35283a2008-09-22 19:51:58 +000038 // Insert definitions for all the floating point types.
Chris Lattnerba443852009-02-07 22:37:06 +000039 switch((int)Fn->arg_begin()->getType()->getTypeID()) {
Dale Johannesen4f35283a2008-09-22 19:51:58 +000040 case Type::FloatTyID:
Chris Lattnerba443852009-02-07 22:37:06 +000041 EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
Dale Johannesen4f35283a2008-09-22 19:51:58 +000042 Type::FloatTy);
Chris Lattnerba443852009-02-07 22:37:06 +000043 break;
Dale Johannesen4f35283a2008-09-22 19:51:58 +000044 case Type::DoubleTyID:
Chris Lattnerba443852009-02-07 22:37:06 +000045 EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
Dale Johannesen4f35283a2008-09-22 19:51:58 +000046 Type::DoubleTy);
Chris Lattnerba443852009-02-07 22:37:06 +000047 break;
Dale Johannesen4f35283a2008-09-22 19:51:58 +000048 case Type::X86_FP80TyID:
49 case Type::FP128TyID:
50 case Type::PPC_FP128TyID:
Chris Lattnerba443852009-02-07 22:37:06 +000051 EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
52 Fn->arg_begin()->getType());
53 break;
Dale Johannesen4f35283a2008-09-22 19:51:58 +000054 }
55}
56
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057/// ReplaceCallWith - This function is used when we want to lower an intrinsic
58/// call to a call of an external function. This handles hard cases such as
59/// when there was already a prototype for the external function, and if that
60/// prototype doesn't match the arguments we expect to pass in.
61template <class ArgIt>
62static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
63 ArgIt ArgBegin, ArgIt ArgEnd,
Owen Anderson41d6d6c2009-06-26 20:33:47 +000064 const Type *RetTy) {
65 // If we haven't already looked up this function, check to see if the
66 // program already contains a function with this name.
67 Module *M = CI->getParent()->getParent()->getParent();
68 // Get or insert the definition now.
69 std::vector<const Type *> ParamTys;
70 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
71 ParamTys.push_back((*I)->getType());
72 Constant* FCache = M->getOrInsertFunction(NewFn,
73 FunctionType::get(RetTy, ParamTys, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
Jay Foade4094882009-05-12 20:27:44 +000075 IRBuilder<> Builder(CI->getParent(), CI);
David Greeneb1c4a7b2007-08-01 03:43:44 +000076 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Jay Foade4094882009-05-12 20:27:44 +000077 CallInst *NewCI = Builder.CreateCall(FCache, Args.begin(), Args.end());
78 NewCI->setName(CI->getName());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 if (!CI->use_empty())
80 CI->replaceAllUsesWith(NewCI);
81 return NewCI;
82}
83
84void IntrinsicLowering::AddPrototypes(Module &M) {
85 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
86 if (I->isDeclaration() && !I->use_empty())
87 switch (I->getIntrinsicID()) {
88 default: break;
89 case Intrinsic::setjmp:
90 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
91 Type::Int32Ty);
92 break;
93 case Intrinsic::longjmp:
94 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
95 Type::VoidTy);
96 break;
97 case Intrinsic::siglongjmp:
98 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
99 Type::VoidTy);
100 break;
Chris Lattner82c2e432008-11-21 16:42:48 +0000101 case Intrinsic::memcpy:
Christopher Lambbb2f2222007-12-17 01:12:55 +0000102 M.getOrInsertFunction("memcpy", PointerType::getUnqual(Type::Int8Ty),
103 PointerType::getUnqual(Type::Int8Ty),
104 PointerType::getUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 TD.getIntPtrType(), (Type *)0);
106 break;
Chris Lattner82c2e432008-11-21 16:42:48 +0000107 case Intrinsic::memmove:
Christopher Lambbb2f2222007-12-17 01:12:55 +0000108 M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty),
109 PointerType::getUnqual(Type::Int8Ty),
110 PointerType::getUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 TD.getIntPtrType(), (Type *)0);
112 break;
Chris Lattner82c2e432008-11-21 16:42:48 +0000113 case Intrinsic::memset:
Christopher Lambbb2f2222007-12-17 01:12:55 +0000114 M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty),
115 PointerType::getUnqual(Type::Int8Ty),
116 Type::Int32Ty,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 TD.getIntPtrType(), (Type *)0);
118 break;
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000119 case Intrinsic::sqrt:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000120 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 break;
Dan Gohman02f9ed92007-10-15 22:07:31 +0000122 case Intrinsic::sin:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000123 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000124 break;
125 case Intrinsic::cos:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000126 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000127 break;
128 case Intrinsic::pow:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000129 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000130 break;
Dale Johannesen92b33082008-09-04 00:47:13 +0000131 case Intrinsic::log:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000132 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000133 break;
134 case Intrinsic::log2:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000135 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000136 break;
137 case Intrinsic::log10:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000138 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000139 break;
140 case Intrinsic::exp:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000141 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000142 break;
143 case Intrinsic::exp2:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000144 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000145 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 }
147}
148
149/// LowerBSWAP - Emit the code to lower bswap of V before the specified
150/// instruction IP.
151static Value *LowerBSWAP(Value *V, Instruction *IP) {
152 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
153
154 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
155
Jay Foade4094882009-05-12 20:27:44 +0000156 IRBuilder<> Builder(IP->getParent(), IP);
157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 switch(BitSize) {
159 default: assert(0 && "Unhandled type size of value to byteswap!");
160 case 16: {
Jay Foade4094882009-05-12 20:27:44 +0000161 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
162 "bswap.2");
163 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
164 "bswap.1");
165 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 break;
167 }
168 case 32: {
Jay Foade4094882009-05-12 20:27:44 +0000169 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
170 "bswap.4");
171 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
172 "bswap.3");
173 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
174 "bswap.2");
175 Value *Tmp1 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 24),
176 "bswap.1");
177 Tmp3 = Builder.CreateAnd(Tmp3, ConstantInt::get(Type::Int32Ty, 0xFF0000),
178 "bswap.and3");
179 Tmp2 = Builder.CreateAnd(Tmp2, ConstantInt::get(Type::Int32Ty, 0xFF00),
180 "bswap.and2");
181 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
182 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
183 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 break;
185 }
186 case 64: {
Jay Foade4094882009-05-12 20:27:44 +0000187 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
188 "bswap.8");
189 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
190 "bswap.7");
191 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
192 "bswap.6");
193 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
194 "bswap.5");
195 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
196 "bswap.4");
197 Value* Tmp3 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 24),
198 "bswap.3");
199 Value* Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 40),
200 "bswap.2");
201 Value* Tmp1 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 56),
202 "bswap.1");
203 Tmp7 = Builder.CreateAnd(Tmp7,
204 ConstantInt::get(Type::Int64Ty,
205 0xFF000000000000ULL),
206 "bswap.and7");
207 Tmp6 = Builder.CreateAnd(Tmp6,
208 ConstantInt::get(Type::Int64Ty,
209 0xFF0000000000ULL),
210 "bswap.and6");
211 Tmp5 = Builder.CreateAnd(Tmp5,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Jay Foade4094882009-05-12 20:27:44 +0000213 "bswap.and5");
214 Tmp4 = Builder.CreateAnd(Tmp4,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Jay Foade4094882009-05-12 20:27:44 +0000216 "bswap.and4");
217 Tmp3 = Builder.CreateAnd(Tmp3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Jay Foade4094882009-05-12 20:27:44 +0000219 "bswap.and3");
220 Tmp2 = Builder.CreateAnd(Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Jay Foade4094882009-05-12 20:27:44 +0000222 "bswap.and2");
223 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
224 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
225 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
226 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
227 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
228 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
229 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 break;
231 }
232 }
233 return V;
234}
235
236/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
237/// instruction IP.
238static Value *LowerCTPOP(Value *V, Instruction *IP) {
239 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
240
241 static const uint64_t MaskValues[6] = {
242 0x5555555555555555ULL, 0x3333333333333333ULL,
243 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
244 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
245 };
246
Jay Foade4094882009-05-12 20:27:44 +0000247 IRBuilder<> Builder(IP->getParent(), IP);
248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
250 unsigned WordSize = (BitSize + 63) / 64;
251 Value *Count = ConstantInt::get(V->getType(), 0);
252
253 for (unsigned n = 0; n < WordSize; ++n) {
254 Value *PartValue = V;
255 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
256 i <<= 1, ++ct) {
257 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Jay Foade4094882009-05-12 20:27:44 +0000258 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
259 Value *VShift = Builder.CreateLShr(PartValue,
260 ConstantInt::get(V->getType(), i),
261 "ctpop.sh");
262 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
263 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 }
Jay Foade4094882009-05-12 20:27:44 +0000265 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 if (BitSize > 64) {
Jay Foade4094882009-05-12 20:27:44 +0000267 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
268 "ctpop.part.sh");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 BitSize -= 64;
270 }
271 }
272
Chris Lattnerf2cf7f02007-08-06 16:36:18 +0000273 return Count;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274}
275
276/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
277/// instruction IP.
278static Value *LowerCTLZ(Value *V, Instruction *IP) {
279
Jay Foade4094882009-05-12 20:27:44 +0000280 IRBuilder<> Builder(IP->getParent(), IP);
281
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
283 for (unsigned i = 1; i < BitSize; i <<= 1) {
284 Value *ShVal = ConstantInt::get(V->getType(), i);
Jay Foade4094882009-05-12 20:27:44 +0000285 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
286 V = Builder.CreateOr(V, ShVal, "ctlz.step");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 }
288
Jay Foade4094882009-05-12 20:27:44 +0000289 V = Builder.CreateNot(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 return LowerCTPOP(V, IP);
291}
292
293/// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes
294/// three integer arguments. The first argument is the Value from which the
295/// bits will be selected. It may be of any bit width. The second and third
296/// arguments specify a range of bits to select with the second argument
297/// specifying the low bit and the third argument specifying the high bit. Both
298/// must be type i32. The result is the corresponding selected bits from the
299/// Value in the same width as the Value (first argument). If the low bit index
300/// is higher than the high bit index then the inverse selection is done and
301/// the bits are returned in inverse order.
302/// @brief Lowering of llvm.part.select intrinsic.
303static Instruction *LowerPartSelect(CallInst *CI) {
Owen Andersonbaf982b2009-07-08 20:50:47 +0000304 IRBuilder<> Builder(*CI->getParent()->getContext());
Jay Foade4094882009-05-12 20:27:44 +0000305
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 // Make sure we're dealing with a part select intrinsic here
307 Function *F = CI->getCalledFunction();
308 const FunctionType *FT = F->getFunctionType();
309 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
310 FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
311 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
312 return CI;
313
314 // Get the intrinsic implementation function by converting all the . to _
315 // in the intrinsic's function name and then reconstructing the function
316 // declaration.
317 std::string Name(F->getName());
318 for (unsigned i = 4; i < Name.length(); ++i)
319 if (Name[i] == '.')
320 Name[i] = '_';
321 Module* M = F->getParent();
322 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Duncan Sands19d161f2009-03-07 15:45:40 +0000323 F->setLinkage(GlobalValue::WeakAnyLinkage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 // If we haven't defined the impl function yet, do so now
326 if (F->isDeclaration()) {
327
328 // Get the arguments to the function
329 Function::arg_iterator args = F->arg_begin();
330 Value* Val = args++; Val->setName("Val");
331 Value* Lo = args++; Lo->setName("Lo");
Gabor Greifd6da1d02008-04-06 20:25:17 +0000332 Value* Hi = args++; Hi->setName("High");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333
334 // We want to select a range of bits here such that [Hi, Lo] is shifted
335 // down to the low bits. However, it is quite possible that Hi is smaller
336 // than Lo in which case the bits have to be reversed.
337
338 // Create the blocks we will need for the two cases (forward, reverse)
Gabor Greifd6da1d02008-04-06 20:25:17 +0000339 BasicBlock* CurBB = BasicBlock::Create("entry", F);
340 BasicBlock *RevSize = BasicBlock::Create("revsize", CurBB->getParent());
341 BasicBlock *FwdSize = BasicBlock::Create("fwdsize", CurBB->getParent());
342 BasicBlock *Compute = BasicBlock::Create("compute", CurBB->getParent());
343 BasicBlock *Reverse = BasicBlock::Create("reverse", CurBB->getParent());
344 BasicBlock *RsltBlk = BasicBlock::Create("result", CurBB->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345
Jay Foade4094882009-05-12 20:27:44 +0000346 Builder.SetInsertPoint(CurBB);
347
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 // Cast Hi and Lo to the size of Val so the widths are all the same
349 if (Hi->getType() != Val->getType())
Jay Foade4094882009-05-12 20:27:44 +0000350 Hi = Builder.CreateIntCast(Hi, Val->getType(), /* isSigned */ false,
351 "tmp");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 if (Lo->getType() != Val->getType())
Jay Foade4094882009-05-12 20:27:44 +0000353 Lo = Builder.CreateIntCast(Lo, Val->getType(), /* isSigned */ false,
354 "tmp");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355
356 // Compute a few things that both cases will need, up front.
357 Constant* Zero = ConstantInt::get(Val->getType(), 0);
358 Constant* One = ConstantInt::get(Val->getType(), 1);
359 Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
360
361 // Compare the Hi and Lo bit positions. This is used to determine
362 // which case we have (forward or reverse)
Jay Foade4094882009-05-12 20:27:44 +0000363 Value *Cmp = Builder.CreateICmpULT(Hi, Lo, "less");
364 Builder.CreateCondBr(Cmp, RevSize, FwdSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365
Jay Foade4094882009-05-12 20:27:44 +0000366 // First, compute the number of bits in the forward case.
367 Builder.SetInsertPoint(FwdSize);
368 Value* FBitSize = Builder.CreateSub(Hi, Lo, "fbits");
369 Builder.CreateBr(Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370
371 // Second, compute the number of bits in the reverse case.
Jay Foade4094882009-05-12 20:27:44 +0000372 Builder.SetInsertPoint(RevSize);
373 Value* RBitSize = Builder.CreateSub(Lo, Hi, "rbits");
374 Builder.CreateBr(Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375
376 // Now, compute the bit range. Start by getting the bitsize and the shift
377 // amount (either Hi or Lo) from PHI nodes. Then we compute a mask for
378 // the number of bits we want in the range. We shift the bits down to the
379 // least significant bits, apply the mask to zero out unwanted high bits,
380 // and we have computed the "forward" result. It may still need to be
381 // reversed.
Jay Foade4094882009-05-12 20:27:44 +0000382 Builder.SetInsertPoint(Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383
384 // Get the BitSize from one of the two subtractions
Jay Foade4094882009-05-12 20:27:44 +0000385 PHINode *BitSize = Builder.CreatePHI(Val->getType(), "bits");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 BitSize->reserveOperandSpace(2);
387 BitSize->addIncoming(FBitSize, FwdSize);
388 BitSize->addIncoming(RBitSize, RevSize);
389
390 // Get the ShiftAmount as the smaller of Hi/Lo
Jay Foade4094882009-05-12 20:27:44 +0000391 PHINode *ShiftAmt = Builder.CreatePHI(Val->getType(), "shiftamt");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 ShiftAmt->reserveOperandSpace(2);
393 ShiftAmt->addIncoming(Lo, FwdSize);
394 ShiftAmt->addIncoming(Hi, RevSize);
395
396 // Increment the bit size
Jay Foade4094882009-05-12 20:27:44 +0000397 Value *BitSizePlusOne = Builder.CreateAdd(BitSize, One, "bits");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398
399 // Create a Mask to zero out the high order bits.
Jay Foade4094882009-05-12 20:27:44 +0000400 Value* Mask = Builder.CreateShl(AllOnes, BitSizePlusOne, "mask");
401 Mask = Builder.CreateNot(Mask, "mask");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402
403 // Shift the bits down and apply the mask
Jay Foade4094882009-05-12 20:27:44 +0000404 Value* FRes = Builder.CreateLShr(Val, ShiftAmt, "fres");
405 FRes = Builder.CreateAnd(FRes, Mask, "fres");
406 Builder.CreateCondBr(Cmp, Reverse, RsltBlk);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407
408 // In the Reverse block we have the mask already in FRes but we must reverse
409 // it by shifting FRes bits right and putting them in RRes by shifting them
410 // in from left.
Jay Foade4094882009-05-12 20:27:44 +0000411 Builder.SetInsertPoint(Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412
413 // First set up our loop counters
Jay Foade4094882009-05-12 20:27:44 +0000414 PHINode *Count = Builder.CreatePHI(Val->getType(), "count");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 Count->reserveOperandSpace(2);
416 Count->addIncoming(BitSizePlusOne, Compute);
417
418 // Next, get the value that we are shifting.
Jay Foade4094882009-05-12 20:27:44 +0000419 PHINode *BitsToShift = Builder.CreatePHI(Val->getType(), "val");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 BitsToShift->reserveOperandSpace(2);
421 BitsToShift->addIncoming(FRes, Compute);
422
423 // Finally, get the result of the last computation
Jay Foade4094882009-05-12 20:27:44 +0000424 PHINode *RRes = Builder.CreatePHI(Val->getType(), "rres");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 RRes->reserveOperandSpace(2);
426 RRes->addIncoming(Zero, Compute);
427
428 // Decrement the counter
Jay Foade4094882009-05-12 20:27:44 +0000429 Value *Decr = Builder.CreateSub(Count, One, "decr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 Count->addIncoming(Decr, Reverse);
431
432 // Compute the Bit that we want to move
Jay Foade4094882009-05-12 20:27:44 +0000433 Value *Bit = Builder.CreateAnd(BitsToShift, One, "bit");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434
435 // Compute the new value for next iteration.
Jay Foade4094882009-05-12 20:27:44 +0000436 Value *NewVal = Builder.CreateLShr(BitsToShift, One, "rshift");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 BitsToShift->addIncoming(NewVal, Reverse);
438
439 // Shift the bit into the low bits of the result.
Jay Foade4094882009-05-12 20:27:44 +0000440 Value *NewRes = Builder.CreateShl(RRes, One, "lshift");
441 NewRes = Builder.CreateOr(NewRes, Bit, "addbit");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 RRes->addIncoming(NewRes, Reverse);
443
444 // Terminate loop if we've moved all the bits.
Jay Foade4094882009-05-12 20:27:44 +0000445 Value *Cond = Builder.CreateICmpEQ(Decr, Zero, "cond");
446 Builder.CreateCondBr(Cond, RsltBlk, Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447
448 // Finally, in the result block, select one of the two results with a PHI
449 // node and return the result;
Jay Foade4094882009-05-12 20:27:44 +0000450 Builder.SetInsertPoint(RsltBlk);
451 PHINode *BitSelect = Builder.CreatePHI(Val->getType(), "part_select");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 BitSelect->reserveOperandSpace(2);
453 BitSelect->addIncoming(FRes, Compute);
454 BitSelect->addIncoming(NewRes, Reverse);
Jay Foade4094882009-05-12 20:27:44 +0000455 Builder.CreateRet(BitSelect);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 }
457
458 // Return a call to the implementation function
Jay Foade4094882009-05-12 20:27:44 +0000459 Builder.SetInsertPoint(CI->getParent(), CI);
460 CallInst *NewCI = Builder.CreateCall3(F, CI->getOperand(1),
461 CI->getOperand(2), CI->getOperand(3));
462 NewCI->setName(CI->getName());
463 return NewCI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464}
465
466/// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes
467/// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High)
468/// The first two arguments can be any bit width. The result is the same width
469/// as %Value. The operation replaces bits between %Low and %High with the value
470/// in %Replacement. If %Replacement is not the same width, it is truncated or
471/// zero extended as appropriate to fit the bits being replaced. If %Low is
472/// greater than %High then the inverse set of bits are replaced.
473/// @brief Lowering of llvm.bit.part.set intrinsic.
474static Instruction *LowerPartSet(CallInst *CI) {
Owen Andersonbaf982b2009-07-08 20:50:47 +0000475 IRBuilder<> Builder(*CI->getParent()->getContext());
Jay Foade4094882009-05-12 20:27:44 +0000476
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 // Make sure we're dealing with a part select intrinsic here
478 Function *F = CI->getCalledFunction();
479 const FunctionType *FT = F->getFunctionType();
480 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
481 FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() ||
482 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
483 !FT->getParamType(3)->isInteger())
484 return CI;
485
486 // Get the intrinsic implementation function by converting all the . to _
487 // in the intrinsic's function name and then reconstructing the function
488 // declaration.
489 std::string Name(F->getName());
490 for (unsigned i = 4; i < Name.length(); ++i)
491 if (Name[i] == '.')
492 Name[i] = '_';
493 Module* M = F->getParent();
494 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Duncan Sands19d161f2009-03-07 15:45:40 +0000495 F->setLinkage(GlobalValue::WeakAnyLinkage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496
497 // If we haven't defined the impl function yet, do so now
498 if (F->isDeclaration()) {
499 // Get the arguments for the function.
500 Function::arg_iterator args = F->arg_begin();
501 Value* Val = args++; Val->setName("Val");
502 Value* Rep = args++; Rep->setName("Rep");
503 Value* Lo = args++; Lo->setName("Lo");
504 Value* Hi = args++; Hi->setName("Hi");
505
506 // Get some types we need
507 const IntegerType* ValTy = cast<IntegerType>(Val->getType());
508 const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 uint32_t RepBits = RepTy->getBitWidth();
510
511 // Constant Definitions
512 ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
513 ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
514 ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
515 ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1);
516 ConstantInt* ValOne = ConstantInt::get(ValTy, 1);
517 ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0);
518 ConstantInt* ValZero = ConstantInt::get(ValTy, 0);
519
520 // Basic blocks we fill in below.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000521 BasicBlock* entry = BasicBlock::Create("entry", F, 0);
522 BasicBlock* large = BasicBlock::Create("large", F, 0);
523 BasicBlock* small = BasicBlock::Create("small", F, 0);
524 BasicBlock* reverse = BasicBlock::Create("reverse", F, 0);
525 BasicBlock* result = BasicBlock::Create("result", F, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526
527 // BASIC BLOCK: entry
Jay Foade4094882009-05-12 20:27:44 +0000528 Builder.SetInsertPoint(entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 // First, get the number of bits that we're placing as an i32
Jay Foade4094882009-05-12 20:27:44 +0000530 Value* is_forward = Builder.CreateICmpULT(Lo, Hi);
531 Value* Hi_pn = Builder.CreateSelect(is_forward, Hi, Lo);
532 Value* Lo_pn = Builder.CreateSelect(is_forward, Lo, Hi);
533 Value* NumBits = Builder.CreateSub(Hi_pn, Lo_pn);
534 NumBits = Builder.CreateAdd(NumBits, One);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 // Now, convert Lo and Hi to ValTy bit width
Jay Foade4094882009-05-12 20:27:44 +0000536 Lo = Builder.CreateIntCast(Lo_pn, ValTy, /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 // Determine if the replacement bits are larger than the number of bits we
538 // are replacing and deal with it.
Jay Foade4094882009-05-12 20:27:44 +0000539 Value* is_large = Builder.CreateICmpULT(NumBits, RepBitWidth);
540 Builder.CreateCondBr(is_large, large, small);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541
542 // BASIC BLOCK: large
Jay Foade4094882009-05-12 20:27:44 +0000543 Builder.SetInsertPoint(large);
544 Value* MaskBits = Builder.CreateSub(RepBitWidth, NumBits);
545 MaskBits = Builder.CreateIntCast(MaskBits, RepMask->getType(),
546 /* isSigned */ false);
547 Value* Mask1 = Builder.CreateLShr(RepMask, MaskBits);
548 Value* Rep2 = Builder.CreateAnd(Mask1, Rep);
549 Builder.CreateBr(small);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550
551 // BASIC BLOCK: small
Jay Foade4094882009-05-12 20:27:44 +0000552 Builder.SetInsertPoint(small);
553 PHINode* Rep3 = Builder.CreatePHI(RepTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 Rep3->reserveOperandSpace(2);
555 Rep3->addIncoming(Rep2, large);
556 Rep3->addIncoming(Rep, entry);
Jay Foade4094882009-05-12 20:27:44 +0000557 Value* Rep4 = Builder.CreateIntCast(Rep3, ValTy, /* isSigned */ false);
558 Builder.CreateCondBr(is_forward, result, reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559
560 // BASIC BLOCK: reverse (reverses the bits of the replacement)
Jay Foade4094882009-05-12 20:27:44 +0000561 Builder.SetInsertPoint(reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 // Set up our loop counter as a PHI so we can decrement on each iteration.
563 // We will loop for the number of bits in the replacement value.
Jay Foade4094882009-05-12 20:27:44 +0000564 PHINode *Count = Builder.CreatePHI(Type::Int32Ty, "count");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 Count->reserveOperandSpace(2);
566 Count->addIncoming(NumBits, small);
567
568 // Get the value that we are shifting bits out of as a PHI because
569 // we'll change this with each iteration.
Jay Foade4094882009-05-12 20:27:44 +0000570 PHINode *BitsToShift = Builder.CreatePHI(Val->getType(), "val");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 BitsToShift->reserveOperandSpace(2);
572 BitsToShift->addIncoming(Rep4, small);
573
574 // Get the result of the last computation or zero on first iteration
Jay Foade4094882009-05-12 20:27:44 +0000575 PHINode *RRes = Builder.CreatePHI(Val->getType(), "rres");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 RRes->reserveOperandSpace(2);
577 RRes->addIncoming(ValZero, small);
578
579 // Decrement the loop counter by one
Jay Foade4094882009-05-12 20:27:44 +0000580 Value *Decr = Builder.CreateSub(Count, One);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 Count->addIncoming(Decr, reverse);
582
583 // Get the bit that we want to move into the result
Jay Foade4094882009-05-12 20:27:44 +0000584 Value *Bit = Builder.CreateAnd(BitsToShift, ValOne);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585
586 // Compute the new value of the bits to shift for the next iteration.
Jay Foade4094882009-05-12 20:27:44 +0000587 Value *NewVal = Builder.CreateLShr(BitsToShift, ValOne);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 BitsToShift->addIncoming(NewVal, reverse);
589
590 // Shift the bit we extracted into the low bit of the result.
Jay Foade4094882009-05-12 20:27:44 +0000591 Value *NewRes = Builder.CreateShl(RRes, ValOne);
592 NewRes = Builder.CreateOr(NewRes, Bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 RRes->addIncoming(NewRes, reverse);
594
595 // Terminate loop if we've moved all the bits.
Jay Foade4094882009-05-12 20:27:44 +0000596 Value *Cond = Builder.CreateICmpEQ(Decr, Zero);
597 Builder.CreateCondBr(Cond, result, reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598
599 // BASIC BLOCK: result
Jay Foade4094882009-05-12 20:27:44 +0000600 Builder.SetInsertPoint(result);
601 PHINode *Rplcmnt = Builder.CreatePHI(Val->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 Rplcmnt->reserveOperandSpace(2);
603 Rplcmnt->addIncoming(NewRes, reverse);
604 Rplcmnt->addIncoming(Rep4, small);
Jay Foade4094882009-05-12 20:27:44 +0000605 Value* t0 = Builder.CreateIntCast(NumBits, ValTy, /* isSigned */ false);
606 Value* t1 = Builder.CreateShl(ValMask, Lo);
607 Value* t2 = Builder.CreateNot(t1);
608 Value* t3 = Builder.CreateShl(t1, t0);
609 Value* t4 = Builder.CreateOr(t2, t3);
610 Value* t5 = Builder.CreateAnd(t4, Val);
611 Value* t6 = Builder.CreateShl(Rplcmnt, Lo);
612 Value* Rslt = Builder.CreateOr(t5, t6, "part_set");
613 Builder.CreateRet(Rslt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 }
615
616 // Return a call to the implementation function
Jay Foade4094882009-05-12 20:27:44 +0000617 Builder.SetInsertPoint(CI->getParent(), CI);
618 CallInst *NewCI = Builder.CreateCall4(F, CI->getOperand(1),
619 CI->getOperand(2), CI->getOperand(3),
620 CI->getOperand(4));
621 NewCI->setName(CI->getName());
622 return NewCI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623}
624
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000625static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
626 const char *Dname,
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000627 const char *LDname) {
628 switch (CI->getOperand(1)->getType()->getTypeID()) {
629 default: assert(0 && "Invalid type in intrinsic"); abort();
630 case Type::FloatTyID:
Jay Foade4094882009-05-12 20:27:44 +0000631 ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000632 Type::FloatTy);
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000633 break;
634 case Type::DoubleTyID:
Jay Foade4094882009-05-12 20:27:44 +0000635 ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000636 Type::DoubleTy);
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000637 break;
638 case Type::X86_FP80TyID:
639 case Type::FP128TyID:
640 case Type::PPC_FP128TyID:
Jay Foade4094882009-05-12 20:27:44 +0000641 ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000642 CI->getOperand(1)->getType());
Dale Johannesen2c8e1082008-09-22 20:51:30 +0000643 break;
644 }
645}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646
647void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Jay Foade4094882009-05-12 20:27:44 +0000648 IRBuilder<> Builder(CI->getParent(), CI);
649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 Function *Callee = CI->getCalledFunction();
651 assert(Callee && "Cannot lower an indirect call!");
652
653 switch (Callee->getIntrinsicID()) {
654 case Intrinsic::not_intrinsic:
655 cerr << "Cannot lower a call to a non-intrinsic function '"
656 << Callee->getName() << "'!\n";
657 abort();
658 default:
659 cerr << "Error: Code generator does not support intrinsic function '"
660 << Callee->getName() << "'!\n";
661 abort();
662
663 // The setjmp/longjmp intrinsics should only exist in the code if it was
664 // never optimized (ie, right out of the CFE), or if it has been hacked on
665 // by the lowerinvoke pass. In both cases, the right thing to do is to
666 // convert the call to an explicit setjmp or longjmp call.
667 case Intrinsic::setjmp: {
Jay Foade4094882009-05-12 20:27:44 +0000668 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000669 Type::Int32Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 if (CI->getType() != Type::VoidTy)
671 CI->replaceAllUsesWith(V);
672 break;
673 }
674 case Intrinsic::sigsetjmp:
675 if (CI->getType() != Type::VoidTy)
676 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
677 break;
678
679 case Intrinsic::longjmp: {
Jay Foade4094882009-05-12 20:27:44 +0000680 ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000681 Type::VoidTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 break;
683 }
684
685 case Intrinsic::siglongjmp: {
686 // Insert the call to abort
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000688 Type::VoidTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 break;
690 }
691 case Intrinsic::ctpop:
692 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
693 break;
694
695 case Intrinsic::bswap:
696 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
697 break;
698
699 case Intrinsic::ctlz:
700 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
701 break;
702
703 case Intrinsic::cttz: {
704 // cttz(x) -> ctpop(~X & (X-1))
705 Value *Src = CI->getOperand(1);
Jay Foade4094882009-05-12 20:27:44 +0000706 Value *NotSrc = Builder.CreateNot(Src);
707 NotSrc->setName(Src->getName() + ".not");
Gabor Greifd6da1d02008-04-06 20:25:17 +0000708 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Jay Foade4094882009-05-12 20:27:44 +0000709 SrcM1 = Builder.CreateSub(Src, SrcM1);
710 Src = LowerCTPOP(Builder.CreateAnd(NotSrc, SrcM1), CI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 CI->replaceAllUsesWith(Src);
712 break;
713 }
714
715 case Intrinsic::part_select:
716 CI->replaceAllUsesWith(LowerPartSelect(CI));
717 break;
718
719 case Intrinsic::part_set:
720 CI->replaceAllUsesWith(LowerPartSet(CI));
721 break;
722
723 case Intrinsic::stacksave:
724 case Intrinsic::stackrestore: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 if (!Warned)
726 cerr << "WARNING: this target does not support the llvm.stack"
727 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
728 "save" : "restore") << " intrinsic.\n";
729 Warned = true;
730 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
731 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
732 break;
733 }
734
735 case Intrinsic::returnaddress:
736 case Intrinsic::frameaddress:
737 cerr << "WARNING: this target does not support the llvm."
738 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
739 "return" : "frame") << "address intrinsic.\n";
740 CI->replaceAllUsesWith(ConstantPointerNull::get(
741 cast<PointerType>(CI->getType())));
742 break;
743
744 case Intrinsic::prefetch:
745 break; // Simply strip out prefetches on unsupported architectures
746
747 case Intrinsic::pcmarker:
748 break; // Simply strip out pcmarker on unsupported architectures
749 case Intrinsic::readcyclecounter: {
750 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
751 << "ter intrinsic. It is being lowered to a constant 0\n";
752 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
753 break;
754 }
755
756 case Intrinsic::dbg_stoppoint:
757 case Intrinsic::dbg_region_start:
758 case Intrinsic::dbg_region_end:
759 case Intrinsic::dbg_func_start:
760 case Intrinsic::dbg_declare:
761 break; // Simply strip out debugging intrinsics
762
763 case Intrinsic::eh_exception:
Anton Korobeynikov94c46a02007-09-07 11:39:35 +0000764 case Intrinsic::eh_selector_i32:
765 case Intrinsic::eh_selector_i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
767 break;
768
Anton Korobeynikov94c46a02007-09-07 11:39:35 +0000769 case Intrinsic::eh_typeid_for_i32:
770 case Intrinsic::eh_typeid_for_i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 // Return something different to eh_selector.
772 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
773 break;
774
775 case Intrinsic::var_annotation:
776 break; // Strip out annotate intrinsic
777
Chris Lattner82c2e432008-11-21 16:42:48 +0000778 case Intrinsic::memcpy: {
Jay Foad4c4839d2009-05-11 11:32:25 +0000779 const IntegerType *IntPtr = TD.getIntPtrType();
Jay Foade4094882009-05-12 20:27:44 +0000780 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
781 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 Value *Ops[3];
783 Ops[0] = CI->getOperand(1);
784 Ops[1] = CI->getOperand(2);
785 Ops[2] = Size;
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000786 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787 break;
788 }
Chris Lattner82c2e432008-11-21 16:42:48 +0000789 case Intrinsic::memmove: {
Jay Foad4c4839d2009-05-11 11:32:25 +0000790 const IntegerType *IntPtr = TD.getIntPtrType();
Jay Foade4094882009-05-12 20:27:44 +0000791 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
792 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000793 Value *Ops[3];
794 Ops[0] = CI->getOperand(1);
795 Ops[1] = CI->getOperand(2);
796 Ops[2] = Size;
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000797 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 break;
799 }
Chris Lattner82c2e432008-11-21 16:42:48 +0000800 case Intrinsic::memset: {
Jay Foad4c4839d2009-05-11 11:32:25 +0000801 const IntegerType *IntPtr = TD.getIntPtrType();
Jay Foade4094882009-05-12 20:27:44 +0000802 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
803 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 Value *Ops[3];
805 Ops[0] = CI->getOperand(1);
806 // Extend the amount to i32.
Jay Foade4094882009-05-12 20:27:44 +0000807 Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::Int32Ty,
808 /* isSigned */ false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 Ops[2] = Size;
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000810 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 break;
812 }
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000813 case Intrinsic::sqrt: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000814 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
Dale Johannesen3b5303b2007-09-28 18:06:58 +0000815 break;
816 }
Dale Johannesen92b33082008-09-04 00:47:13 +0000817 case Intrinsic::log: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000818 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000819 break;
820 }
821 case Intrinsic::log2: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000822 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000823 break;
824 }
825 case Intrinsic::log10: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000826 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000827 break;
828 }
829 case Intrinsic::exp: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000830 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000831 break;
832 }
833 case Intrinsic::exp2: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000834 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000835 break;
836 }
837 case Intrinsic::pow: {
Owen Anderson41d6d6c2009-06-26 20:33:47 +0000838 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000839 break;
840 }
Anton Korobeynikovc915e272007-11-15 23:25:33 +0000841 case Intrinsic::flt_rounds:
842 // Lower to "round to the nearest"
843 if (CI->getType() != Type::VoidTy)
844 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
845 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 }
847
848 assert(CI->use_empty() &&
849 "Lowering should have eliminated any uses of the intrinsic call!");
850 CI->eraseFromParent();
851}