blob: 64162f8ea2d2f7d8f56c8906d260e72c7713196d [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"
17#include "llvm/Instructions.h"
18#include "llvm/Type.h"
19#include "llvm/CodeGen/IntrinsicLowering.h"
20#include "llvm/Support/Streams.h"
21#include "llvm/Target/TargetData.h"
22#include "llvm/ADT/SmallVector.h"
Owen Anderson1636de92007-09-07 04:06:50 +000023#include "llvm/ADT/STLExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
26template <class ArgIt>
27static void EnsureFunctionExists(Module &M, const char *Name,
28 ArgIt ArgBegin, ArgIt ArgEnd,
29 const Type *RetTy) {
30 // Insert a correctly-typed definition now.
31 std::vector<const Type *> ParamTys;
32 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
33 ParamTys.push_back(I->getType());
34 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
35}
36
Dale Johannesen4f35283a2008-09-22 19:51:58 +000037static void EnsureFPIntrinsicsExist(Module &M, Module::iterator I,
38 const char *FName,
39 const char *DName, const char *LDName) {
40 // Insert definitions for all the floating point types.
41 switch((int)I->arg_begin()->getType()->getTypeID()) {
42 case Type::FloatTyID:
43 EnsureFunctionExists(M, FName, I->arg_begin(), I->arg_end(),
44 Type::FloatTy);
45 case Type::DoubleTyID:
46 EnsureFunctionExists(M, DName, I->arg_begin(), I->arg_end(),
47 Type::DoubleTy);
48 case Type::X86_FP80TyID:
49 case Type::FP128TyID:
50 case Type::PPC_FP128TyID:
51 EnsureFunctionExists(M, LDName, I->arg_begin(), I->arg_end(),
52 I->arg_begin()->getType());
53 }
54}
55
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056/// ReplaceCallWith - This function is used when we want to lower an intrinsic
57/// call to a call of an external function. This handles hard cases such as
58/// when there was already a prototype for the external function, and if that
59/// prototype doesn't match the arguments we expect to pass in.
60template <class ArgIt>
61static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
62 ArgIt ArgBegin, ArgIt ArgEnd,
63 const Type *RetTy, Constant *&FCache) {
64 if (!FCache) {
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 FCache = M->getOrInsertFunction(NewFn,
73 FunctionType::get(RetTy, ParamTys, false));
74 }
75
David Greeneb1c4a7b2007-08-01 03:43:44 +000076 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Gabor Greifd6da1d02008-04-06 20:25:17 +000077 CallInst *NewCI = CallInst::Create(FCache, Args.begin(), Args.end(),
78 CI->getName(), CI);
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;
101 case Intrinsic::memcpy_i32:
102 case Intrinsic::memcpy_i64:
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;
108 case Intrinsic::memmove_i32:
109 case Intrinsic::memmove_i64:
Christopher Lambbb2f2222007-12-17 01:12:55 +0000110 M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty),
111 PointerType::getUnqual(Type::Int8Ty),
112 PointerType::getUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 TD.getIntPtrType(), (Type *)0);
114 break;
115 case Intrinsic::memset_i32:
116 case Intrinsic::memset_i64:
Christopher Lambbb2f2222007-12-17 01:12:55 +0000117 M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty),
118 PointerType::getUnqual(Type::Int8Ty),
119 Type::Int32Ty,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 TD.getIntPtrType(), (Type *)0);
121 break;
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000122 case Intrinsic::sqrt:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000123 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 break;
Dan Gohman02f9ed92007-10-15 22:07:31 +0000125 case Intrinsic::sin:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000126 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000127 break;
128 case Intrinsic::cos:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000129 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000130 break;
131 case Intrinsic::pow:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000132 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
Dan Gohman02f9ed92007-10-15 22:07:31 +0000133 break;
Dale Johannesen92b33082008-09-04 00:47:13 +0000134 case Intrinsic::log:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000135 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000136 break;
137 case Intrinsic::log2:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000138 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000139 break;
140 case Intrinsic::log10:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000141 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000142 break;
143 case Intrinsic::exp:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000144 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
Dale Johannesen92b33082008-09-04 00:47:13 +0000145 break;
146 case Intrinsic::exp2:
Dale Johannesen4f35283a2008-09-22 19:51:58 +0000147 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
Dale Johannesen92b33082008-09-04 00:47:13 +0000148 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 }
150}
151
152/// LowerBSWAP - Emit the code to lower bswap of V before the specified
153/// instruction IP.
154static Value *LowerBSWAP(Value *V, Instruction *IP) {
155 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
156
157 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
158
159 switch(BitSize) {
160 default: assert(0 && "Unhandled type size of value to byteswap!");
161 case 16: {
Gabor Greifa645dd32008-05-16 19:29:10 +0000162 Value *Tmp1 = BinaryOperator::CreateShl(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000164 Value *Tmp2 = BinaryOperator::CreateLShr(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 ConstantInt::get(V->getType(),8),"bswap.1",IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000166 V = BinaryOperator::CreateOr(Tmp1, Tmp2, "bswap.i16", IP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 break;
168 }
169 case 32: {
Gabor Greifa645dd32008-05-16 19:29:10 +0000170 Value *Tmp4 = BinaryOperator::CreateShl(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 ConstantInt::get(V->getType(),24),"bswap.4", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000172 Value *Tmp3 = BinaryOperator::CreateShl(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 ConstantInt::get(V->getType(),8),"bswap.3",IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000174 Value *Tmp2 = BinaryOperator::CreateLShr(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000176 Value *Tmp1 = BinaryOperator::CreateLShr(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 ConstantInt::get(V->getType(),24),"bswap.1", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000178 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 ConstantInt::get(Type::Int32Ty, 0xFF0000),
180 "bswap.and3", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000181 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 ConstantInt::get(Type::Int32Ty, 0xFF00),
183 "bswap.and2", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000184 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or1", IP);
185 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or2", IP);
186 V = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.i32", IP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 break;
188 }
189 case 64: {
Gabor Greifa645dd32008-05-16 19:29:10 +0000190 Value *Tmp8 = BinaryOperator::CreateShl(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 ConstantInt::get(V->getType(),56),"bswap.8", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000192 Value *Tmp7 = BinaryOperator::CreateShl(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 ConstantInt::get(V->getType(),40),"bswap.7", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000194 Value *Tmp6 = BinaryOperator::CreateShl(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 ConstantInt::get(V->getType(),24),"bswap.6", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000196 Value *Tmp5 = BinaryOperator::CreateShl(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 ConstantInt::get(V->getType(),8),"bswap.5", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000198 Value* Tmp4 = BinaryOperator::CreateLShr(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 ConstantInt::get(V->getType(),8),"bswap.4", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000200 Value* Tmp3 = BinaryOperator::CreateLShr(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 ConstantInt::get(V->getType(),24),"bswap.3", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000202 Value* Tmp2 = BinaryOperator::CreateLShr(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 ConstantInt::get(V->getType(),40),"bswap.2", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000204 Value* Tmp1 = BinaryOperator::CreateLShr(V,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 ConstantInt::get(V->getType(),56),"bswap.1", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000206 Tmp7 = BinaryOperator::CreateAnd(Tmp7,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 ConstantInt::get(Type::Int64Ty,
208 0xFF000000000000ULL),
209 "bswap.and7", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000210 Tmp6 = BinaryOperator::CreateAnd(Tmp6,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
212 "bswap.and6", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000213 Tmp5 = BinaryOperator::CreateAnd(Tmp5,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
215 "bswap.and5", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000216 Tmp4 = BinaryOperator::CreateAnd(Tmp4,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
218 "bswap.and4", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000219 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
221 "bswap.and3", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000222 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
224 "bswap.and2", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000225 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp7, "bswap.or1", IP);
226 Tmp6 = BinaryOperator::CreateOr(Tmp6, Tmp5, "bswap.or2", IP);
227 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or3", IP);
228 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or4", IP);
229 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp6, "bswap.or5", IP);
230 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.or6", IP);
231 V = BinaryOperator::CreateOr(Tmp8, Tmp4, "bswap.i64", IP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 break;
233 }
234 }
235 return V;
236}
237
238/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
239/// instruction IP.
240static Value *LowerCTPOP(Value *V, Instruction *IP) {
241 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
242
243 static const uint64_t MaskValues[6] = {
244 0x5555555555555555ULL, 0x3333333333333333ULL,
245 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
246 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
247 };
248
249 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]);
Gabor Greifa645dd32008-05-16 19:29:10 +0000258 Value *LHS = BinaryOperator::CreateAnd(
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 PartValue, MaskCst, "cppop.and1", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000260 Value *VShift = BinaryOperator::CreateLShr(PartValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 ConstantInt::get(V->getType(), i), "ctpop.sh", IP);
Gabor Greifa645dd32008-05-16 19:29:10 +0000262 Value *RHS = BinaryOperator::CreateAnd(VShift, MaskCst, "cppop.and2", IP);
263 PartValue = BinaryOperator::CreateAdd(LHS, RHS, "ctpop.step", IP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 }
Gabor Greifa645dd32008-05-16 19:29:10 +0000265 Count = BinaryOperator::CreateAdd(PartValue, Count, "ctpop.part", IP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 if (BitSize > 64) {
Gabor Greifa645dd32008-05-16 19:29:10 +0000267 V = BinaryOperator::CreateLShr(V, ConstantInt::get(V->getType(), 64),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 "ctpop.part.sh", IP);
269 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
280 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
281 for (unsigned i = 1; i < BitSize; i <<= 1) {
282 Value *ShVal = ConstantInt::get(V->getType(), i);
Gabor Greifa645dd32008-05-16 19:29:10 +0000283 ShVal = BinaryOperator::CreateLShr(V, ShVal, "ctlz.sh", IP);
284 V = BinaryOperator::CreateOr(V, ShVal, "ctlz.step", IP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 }
286
Gabor Greifa645dd32008-05-16 19:29:10 +0000287 V = BinaryOperator::CreateNot(V, "", IP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 return LowerCTPOP(V, IP);
289}
290
291/// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes
292/// three integer arguments. The first argument is the Value from which the
293/// bits will be selected. It may be of any bit width. The second and third
294/// arguments specify a range of bits to select with the second argument
295/// specifying the low bit and the third argument specifying the high bit. Both
296/// must be type i32. The result is the corresponding selected bits from the
297/// Value in the same width as the Value (first argument). If the low bit index
298/// is higher than the high bit index then the inverse selection is done and
299/// the bits are returned in inverse order.
300/// @brief Lowering of llvm.part.select intrinsic.
301static Instruction *LowerPartSelect(CallInst *CI) {
302 // Make sure we're dealing with a part select intrinsic here
303 Function *F = CI->getCalledFunction();
304 const FunctionType *FT = F->getFunctionType();
305 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
306 FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
307 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
308 return CI;
309
310 // Get the intrinsic implementation function by converting all the . to _
311 // in the intrinsic's function name and then reconstructing the function
312 // declaration.
313 std::string Name(F->getName());
314 for (unsigned i = 4; i < Name.length(); ++i)
315 if (Name[i] == '.')
316 Name[i] = '_';
317 Module* M = F->getParent();
318 F = cast<Function>(M->getOrInsertFunction(Name, FT));
319 F->setLinkage(GlobalValue::WeakLinkage);
320
321 // If we haven't defined the impl function yet, do so now
322 if (F->isDeclaration()) {
323
324 // Get the arguments to the function
325 Function::arg_iterator args = F->arg_begin();
326 Value* Val = args++; Val->setName("Val");
327 Value* Lo = args++; Lo->setName("Lo");
Gabor Greifd6da1d02008-04-06 20:25:17 +0000328 Value* Hi = args++; Hi->setName("High");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329
330 // We want to select a range of bits here such that [Hi, Lo] is shifted
331 // down to the low bits. However, it is quite possible that Hi is smaller
332 // than Lo in which case the bits have to be reversed.
333
334 // Create the blocks we will need for the two cases (forward, reverse)
Gabor Greifd6da1d02008-04-06 20:25:17 +0000335 BasicBlock* CurBB = BasicBlock::Create("entry", F);
336 BasicBlock *RevSize = BasicBlock::Create("revsize", CurBB->getParent());
337 BasicBlock *FwdSize = BasicBlock::Create("fwdsize", CurBB->getParent());
338 BasicBlock *Compute = BasicBlock::Create("compute", CurBB->getParent());
339 BasicBlock *Reverse = BasicBlock::Create("reverse", CurBB->getParent());
340 BasicBlock *RsltBlk = BasicBlock::Create("result", CurBB->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341
342 // Cast Hi and Lo to the size of Val so the widths are all the same
343 if (Hi->getType() != Val->getType())
Gabor Greifa645dd32008-05-16 19:29:10 +0000344 Hi = CastInst::CreateIntegerCast(Hi, Val->getType(), false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 "tmp", CurBB);
346 if (Lo->getType() != Val->getType())
Gabor Greifa645dd32008-05-16 19:29:10 +0000347 Lo = CastInst::CreateIntegerCast(Lo, Val->getType(), false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 "tmp", CurBB);
349
350 // Compute a few things that both cases will need, up front.
351 Constant* Zero = ConstantInt::get(Val->getType(), 0);
352 Constant* One = ConstantInt::get(Val->getType(), 1);
353 Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
354
355 // Compare the Hi and Lo bit positions. This is used to determine
356 // which case we have (forward or reverse)
357 ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Hi, Lo, "less",CurBB);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000358 BranchInst::Create(RevSize, FwdSize, Cmp, CurBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359
360 // First, copmute the number of bits in the forward case.
361 Instruction* FBitSize =
Gabor Greifa645dd32008-05-16 19:29:10 +0000362 BinaryOperator::CreateSub(Hi, Lo,"fbits", FwdSize);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000363 BranchInst::Create(Compute, FwdSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364
365 // Second, compute the number of bits in the reverse case.
366 Instruction* RBitSize =
Gabor Greifa645dd32008-05-16 19:29:10 +0000367 BinaryOperator::CreateSub(Lo, Hi, "rbits", RevSize);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000368 BranchInst::Create(Compute, RevSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369
370 // Now, compute the bit range. Start by getting the bitsize and the shift
371 // amount (either Hi or Lo) from PHI nodes. Then we compute a mask for
372 // the number of bits we want in the range. We shift the bits down to the
373 // least significant bits, apply the mask to zero out unwanted high bits,
374 // and we have computed the "forward" result. It may still need to be
375 // reversed.
376
377 // Get the BitSize from one of the two subtractions
Gabor Greifd6da1d02008-04-06 20:25:17 +0000378 PHINode *BitSize = PHINode::Create(Val->getType(), "bits", Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 BitSize->reserveOperandSpace(2);
380 BitSize->addIncoming(FBitSize, FwdSize);
381 BitSize->addIncoming(RBitSize, RevSize);
382
383 // Get the ShiftAmount as the smaller of Hi/Lo
Gabor Greifd6da1d02008-04-06 20:25:17 +0000384 PHINode *ShiftAmt = PHINode::Create(Val->getType(), "shiftamt", Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 ShiftAmt->reserveOperandSpace(2);
386 ShiftAmt->addIncoming(Lo, FwdSize);
387 ShiftAmt->addIncoming(Hi, RevSize);
388
389 // Increment the bit size
390 Instruction *BitSizePlusOne =
Gabor Greifa645dd32008-05-16 19:29:10 +0000391 BinaryOperator::CreateAdd(BitSize, One, "bits", Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392
393 // Create a Mask to zero out the high order bits.
394 Instruction* Mask =
Gabor Greifa645dd32008-05-16 19:29:10 +0000395 BinaryOperator::CreateShl(AllOnes, BitSizePlusOne, "mask", Compute);
396 Mask = BinaryOperator::CreateNot(Mask, "mask", Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397
398 // Shift the bits down and apply the mask
399 Instruction* FRes =
Gabor Greifa645dd32008-05-16 19:29:10 +0000400 BinaryOperator::CreateLShr(Val, ShiftAmt, "fres", Compute);
401 FRes = BinaryOperator::CreateAnd(FRes, Mask, "fres", Compute);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000402 BranchInst::Create(Reverse, RsltBlk, Cmp, Compute);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403
404 // In the Reverse block we have the mask already in FRes but we must reverse
405 // it by shifting FRes bits right and putting them in RRes by shifting them
406 // in from left.
407
408 // First set up our loop counters
Gabor Greifd6da1d02008-04-06 20:25:17 +0000409 PHINode *Count = PHINode::Create(Val->getType(), "count", Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 Count->reserveOperandSpace(2);
411 Count->addIncoming(BitSizePlusOne, Compute);
412
413 // Next, get the value that we are shifting.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000414 PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 BitsToShift->reserveOperandSpace(2);
416 BitsToShift->addIncoming(FRes, Compute);
417
418 // Finally, get the result of the last computation
Gabor Greifd6da1d02008-04-06 20:25:17 +0000419 PHINode *RRes = PHINode::Create(Val->getType(), "rres", Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 RRes->reserveOperandSpace(2);
421 RRes->addIncoming(Zero, Compute);
422
423 // Decrement the counter
Gabor Greifa645dd32008-05-16 19:29:10 +0000424 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "decr", Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 Count->addIncoming(Decr, Reverse);
426
427 // Compute the Bit that we want to move
428 Instruction *Bit =
Gabor Greifa645dd32008-05-16 19:29:10 +0000429 BinaryOperator::CreateAnd(BitsToShift, One, "bit", Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430
431 // Compute the new value for next iteration.
432 Instruction *NewVal =
Gabor Greifa645dd32008-05-16 19:29:10 +0000433 BinaryOperator::CreateLShr(BitsToShift, One, "rshift", Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 BitsToShift->addIncoming(NewVal, Reverse);
435
436 // Shift the bit into the low bits of the result.
437 Instruction *NewRes =
Gabor Greifa645dd32008-05-16 19:29:10 +0000438 BinaryOperator::CreateShl(RRes, One, "lshift", Reverse);
439 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "addbit", Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 RRes->addIncoming(NewRes, Reverse);
441
442 // Terminate loop if we've moved all the bits.
443 ICmpInst *Cond =
444 new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000445 BranchInst::Create(RsltBlk, Reverse, Cond, Reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446
447 // Finally, in the result block, select one of the two results with a PHI
448 // node and return the result;
449 CurBB = RsltBlk;
Gabor Greifd6da1d02008-04-06 20:25:17 +0000450 PHINode *BitSelect = PHINode::Create(Val->getType(), "part_select", CurBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 BitSelect->reserveOperandSpace(2);
452 BitSelect->addIncoming(FRes, Compute);
453 BitSelect->addIncoming(NewRes, Reverse);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000454 ReturnInst::Create(BitSelect, CurBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 }
456
457 // Return a call to the implementation function
458 Value *Args[] = {
459 CI->getOperand(1),
460 CI->getOperand(2),
461 CI->getOperand(3)
462 };
Gabor Greifd6da1d02008-04-06 20:25:17 +0000463 return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI);
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) {
475 // Make sure we're dealing with a part select intrinsic here
476 Function *F = CI->getCalledFunction();
477 const FunctionType *FT = F->getFunctionType();
478 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
479 FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() ||
480 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
481 !FT->getParamType(3)->isInteger())
482 return CI;
483
484 // Get the intrinsic implementation function by converting all the . to _
485 // in the intrinsic's function name and then reconstructing the function
486 // declaration.
487 std::string Name(F->getName());
488 for (unsigned i = 4; i < Name.length(); ++i)
489 if (Name[i] == '.')
490 Name[i] = '_';
491 Module* M = F->getParent();
492 F = cast<Function>(M->getOrInsertFunction(Name, FT));
493 F->setLinkage(GlobalValue::WeakLinkage);
494
495 // If we haven't defined the impl function yet, do so now
496 if (F->isDeclaration()) {
497 // Get the arguments for the function.
498 Function::arg_iterator args = F->arg_begin();
499 Value* Val = args++; Val->setName("Val");
500 Value* Rep = args++; Rep->setName("Rep");
501 Value* Lo = args++; Lo->setName("Lo");
502 Value* Hi = args++; Hi->setName("Hi");
503
504 // Get some types we need
505 const IntegerType* ValTy = cast<IntegerType>(Val->getType());
506 const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
507 uint32_t ValBits = ValTy->getBitWidth();
508 uint32_t RepBits = RepTy->getBitWidth();
509
510 // Constant Definitions
511 ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
512 ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
513 ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
514 ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1);
515 ConstantInt* ValOne = ConstantInt::get(ValTy, 1);
516 ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0);
517 ConstantInt* ValZero = ConstantInt::get(ValTy, 0);
518
519 // Basic blocks we fill in below.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000520 BasicBlock* entry = BasicBlock::Create("entry", F, 0);
521 BasicBlock* large = BasicBlock::Create("large", F, 0);
522 BasicBlock* small = BasicBlock::Create("small", F, 0);
523 BasicBlock* reverse = BasicBlock::Create("reverse", F, 0);
524 BasicBlock* result = BasicBlock::Create("result", F, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525
526 // BASIC BLOCK: entry
527 // First, get the number of bits that we're placing as an i32
528 ICmpInst* is_forward =
529 new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000530 SelectInst* Hi_pn = SelectInst::Create(is_forward, Hi, Lo, "", entry);
531 SelectInst* Lo_pn = SelectInst::Create(is_forward, Lo, Hi, "", entry);
Gabor Greifa645dd32008-05-16 19:29:10 +0000532 BinaryOperator* NumBits = BinaryOperator::CreateSub(Hi_pn, Lo_pn, "",entry);
533 NumBits = BinaryOperator::CreateAdd(NumBits, One, "", entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 // Now, convert Lo and Hi to ValTy bit width
535 if (ValBits > 32) {
536 Lo = new ZExtInst(Lo_pn, ValTy, "", entry);
537 } else if (ValBits < 32) {
538 Lo = new TruncInst(Lo_pn, ValTy, "", entry);
539 }
540 // Determine if the replacement bits are larger than the number of bits we
541 // are replacing and deal with it.
542 ICmpInst* is_large =
543 new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000544 BranchInst::Create(large, small, is_large, entry);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545
546 // BASIC BLOCK: large
547 Instruction* MaskBits =
Gabor Greifa645dd32008-05-16 19:29:10 +0000548 BinaryOperator::CreateSub(RepBitWidth, NumBits, "", large);
549 MaskBits = CastInst::CreateIntegerCast(MaskBits, RepMask->getType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 false, "", large);
551 BinaryOperator* Mask1 =
Gabor Greifa645dd32008-05-16 19:29:10 +0000552 BinaryOperator::CreateLShr(RepMask, MaskBits, "", large);
553 BinaryOperator* Rep2 = BinaryOperator::CreateAnd(Mask1, Rep, "", large);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000554 BranchInst::Create(small, large);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555
556 // BASIC BLOCK: small
Gabor Greifd6da1d02008-04-06 20:25:17 +0000557 PHINode* Rep3 = PHINode::Create(RepTy, "", small);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 Rep3->reserveOperandSpace(2);
559 Rep3->addIncoming(Rep2, large);
560 Rep3->addIncoming(Rep, entry);
561 Value* Rep4 = Rep3;
562 if (ValBits > RepBits)
563 Rep4 = new ZExtInst(Rep3, ValTy, "", small);
564 else if (ValBits < RepBits)
565 Rep4 = new TruncInst(Rep3, ValTy, "", small);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000566 BranchInst::Create(result, reverse, is_forward, small);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567
568 // BASIC BLOCK: reverse (reverses the bits of the replacement)
569 // Set up our loop counter as a PHI so we can decrement on each iteration.
570 // We will loop for the number of bits in the replacement value.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000571 PHINode *Count = PHINode::Create(Type::Int32Ty, "count", reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 Count->reserveOperandSpace(2);
573 Count->addIncoming(NumBits, small);
574
575 // Get the value that we are shifting bits out of as a PHI because
576 // we'll change this with each iteration.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000577 PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 BitsToShift->reserveOperandSpace(2);
579 BitsToShift->addIncoming(Rep4, small);
580
581 // Get the result of the last computation or zero on first iteration
Gabor Greifd6da1d02008-04-06 20:25:17 +0000582 PHINode *RRes = PHINode::Create(Val->getType(), "rres", reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 RRes->reserveOperandSpace(2);
584 RRes->addIncoming(ValZero, small);
585
586 // Decrement the loop counter by one
Gabor Greifa645dd32008-05-16 19:29:10 +0000587 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "", reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 Count->addIncoming(Decr, reverse);
589
590 // Get the bit that we want to move into the result
Gabor Greifa645dd32008-05-16 19:29:10 +0000591 Value *Bit = BinaryOperator::CreateAnd(BitsToShift, ValOne, "", reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592
593 // Compute the new value of the bits to shift for the next iteration.
Gabor Greifa645dd32008-05-16 19:29:10 +0000594 Value *NewVal = BinaryOperator::CreateLShr(BitsToShift, ValOne,"", reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 BitsToShift->addIncoming(NewVal, reverse);
596
597 // Shift the bit we extracted into the low bit of the result.
Gabor Greifa645dd32008-05-16 19:29:10 +0000598 Instruction *NewRes = BinaryOperator::CreateShl(RRes, ValOne, "", reverse);
599 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "", reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 RRes->addIncoming(NewRes, reverse);
601
602 // Terminate loop if we've moved all the bits.
603 ICmpInst *Cond = new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "", reverse);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000604 BranchInst::Create(result, reverse, Cond, reverse);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605
606 // BASIC BLOCK: result
Gabor Greifd6da1d02008-04-06 20:25:17 +0000607 PHINode *Rplcmnt = PHINode::Create(Val->getType(), "", result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 Rplcmnt->reserveOperandSpace(2);
609 Rplcmnt->addIncoming(NewRes, reverse);
610 Rplcmnt->addIncoming(Rep4, small);
Gabor Greifa645dd32008-05-16 19:29:10 +0000611 Value* t0 = CastInst::CreateIntegerCast(NumBits,ValTy,false,"",result);
612 Value* t1 = BinaryOperator::CreateShl(ValMask, Lo, "", result);
613 Value* t2 = BinaryOperator::CreateNot(t1, "", result);
614 Value* t3 = BinaryOperator::CreateShl(t1, t0, "", result);
615 Value* t4 = BinaryOperator::CreateOr(t2, t3, "", result);
616 Value* t5 = BinaryOperator::CreateAnd(t4, Val, "", result);
617 Value* t6 = BinaryOperator::CreateShl(Rplcmnt, Lo, "", result);
618 Value* Rslt = BinaryOperator::CreateOr(t5, t6, "part_set", result);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000619 ReturnInst::Create(Rslt, result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 }
621
622 // Return a call to the implementation function
623 Value *Args[] = {
624 CI->getOperand(1),
625 CI->getOperand(2),
626 CI->getOperand(3),
627 CI->getOperand(4)
628 };
Gabor Greifd6da1d02008-04-06 20:25:17 +0000629 return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630}
631
632
633void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
634 Function *Callee = CI->getCalledFunction();
635 assert(Callee && "Cannot lower an indirect call!");
636
637 switch (Callee->getIntrinsicID()) {
638 case Intrinsic::not_intrinsic:
639 cerr << "Cannot lower a call to a non-intrinsic function '"
640 << Callee->getName() << "'!\n";
641 abort();
642 default:
643 cerr << "Error: Code generator does not support intrinsic function '"
644 << Callee->getName() << "'!\n";
645 abort();
646
647 // The setjmp/longjmp intrinsics should only exist in the code if it was
648 // never optimized (ie, right out of the CFE), or if it has been hacked on
649 // by the lowerinvoke pass. In both cases, the right thing to do is to
650 // convert the call to an explicit setjmp or longjmp call.
651 case Intrinsic::setjmp: {
652 static Constant *SetjmpFCache = 0;
653 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
654 Type::Int32Ty, SetjmpFCache);
655 if (CI->getType() != Type::VoidTy)
656 CI->replaceAllUsesWith(V);
657 break;
658 }
659 case Intrinsic::sigsetjmp:
660 if (CI->getType() != Type::VoidTy)
661 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
662 break;
663
664 case Intrinsic::longjmp: {
665 static Constant *LongjmpFCache = 0;
666 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
667 Type::VoidTy, LongjmpFCache);
668 break;
669 }
670
671 case Intrinsic::siglongjmp: {
672 // Insert the call to abort
673 static Constant *AbortFCache = 0;
674 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
675 Type::VoidTy, AbortFCache);
676 break;
677 }
678 case Intrinsic::ctpop:
679 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
680 break;
681
682 case Intrinsic::bswap:
683 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
684 break;
685
686 case Intrinsic::ctlz:
687 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
688 break;
689
690 case Intrinsic::cttz: {
691 // cttz(x) -> ctpop(~X & (X-1))
692 Value *Src = CI->getOperand(1);
Gabor Greifa645dd32008-05-16 19:29:10 +0000693 Value *NotSrc = BinaryOperator::CreateNot(Src, Src->getName()+".not", CI);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000694 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Gabor Greifa645dd32008-05-16 19:29:10 +0000695 SrcM1 = BinaryOperator::CreateSub(Src, SrcM1, "", CI);
696 Src = LowerCTPOP(BinaryOperator::CreateAnd(NotSrc, SrcM1, "", CI), CI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 CI->replaceAllUsesWith(Src);
698 break;
699 }
700
701 case Intrinsic::part_select:
702 CI->replaceAllUsesWith(LowerPartSelect(CI));
703 break;
704
705 case Intrinsic::part_set:
706 CI->replaceAllUsesWith(LowerPartSet(CI));
707 break;
708
709 case Intrinsic::stacksave:
710 case Intrinsic::stackrestore: {
711 static bool Warned = false;
712 if (!Warned)
713 cerr << "WARNING: this target does not support the llvm.stack"
714 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
715 "save" : "restore") << " intrinsic.\n";
716 Warned = true;
717 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
718 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
719 break;
720 }
721
722 case Intrinsic::returnaddress:
723 case Intrinsic::frameaddress:
724 cerr << "WARNING: this target does not support the llvm."
725 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
726 "return" : "frame") << "address intrinsic.\n";
727 CI->replaceAllUsesWith(ConstantPointerNull::get(
728 cast<PointerType>(CI->getType())));
729 break;
730
731 case Intrinsic::prefetch:
732 break; // Simply strip out prefetches on unsupported architectures
733
734 case Intrinsic::pcmarker:
735 break; // Simply strip out pcmarker on unsupported architectures
736 case Intrinsic::readcyclecounter: {
737 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
738 << "ter intrinsic. It is being lowered to a constant 0\n";
739 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
740 break;
741 }
742
743 case Intrinsic::dbg_stoppoint:
744 case Intrinsic::dbg_region_start:
745 case Intrinsic::dbg_region_end:
746 case Intrinsic::dbg_func_start:
747 case Intrinsic::dbg_declare:
748 break; // Simply strip out debugging intrinsics
749
750 case Intrinsic::eh_exception:
Anton Korobeynikov94c46a02007-09-07 11:39:35 +0000751 case Intrinsic::eh_selector_i32:
752 case Intrinsic::eh_selector_i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
754 break;
755
Anton Korobeynikov94c46a02007-09-07 11:39:35 +0000756 case Intrinsic::eh_typeid_for_i32:
757 case Intrinsic::eh_typeid_for_i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 // Return something different to eh_selector.
759 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
760 break;
761
762 case Intrinsic::var_annotation:
763 break; // Strip out annotate intrinsic
764
765 case Intrinsic::memcpy_i32:
766 case Intrinsic::memcpy_i64: {
767 static Constant *MemcpyFCache = 0;
768 Value *Size = CI->getOperand(3);
769 const Type *IntPtr = TD.getIntPtrType();
770 if (Size->getType()->getPrimitiveSizeInBits() <
771 IntPtr->getPrimitiveSizeInBits())
772 Size = new ZExtInst(Size, IntPtr, "", CI);
773 else if (Size->getType()->getPrimitiveSizeInBits() >
774 IntPtr->getPrimitiveSizeInBits())
775 Size = new TruncInst(Size, IntPtr, "", CI);
776 Value *Ops[3];
777 Ops[0] = CI->getOperand(1);
778 Ops[1] = CI->getOperand(2);
779 Ops[2] = Size;
780 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
781 MemcpyFCache);
782 break;
783 }
784 case Intrinsic::memmove_i32:
785 case Intrinsic::memmove_i64: {
786 static Constant *MemmoveFCache = 0;
787 Value *Size = CI->getOperand(3);
788 const Type *IntPtr = TD.getIntPtrType();
789 if (Size->getType()->getPrimitiveSizeInBits() <
790 IntPtr->getPrimitiveSizeInBits())
791 Size = new ZExtInst(Size, IntPtr, "", CI);
792 else if (Size->getType()->getPrimitiveSizeInBits() >
793 IntPtr->getPrimitiveSizeInBits())
794 Size = new TruncInst(Size, IntPtr, "", CI);
795 Value *Ops[3];
796 Ops[0] = CI->getOperand(1);
797 Ops[1] = CI->getOperand(2);
798 Ops[2] = Size;
799 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
800 MemmoveFCache);
801 break;
802 }
803 case Intrinsic::memset_i32:
804 case Intrinsic::memset_i64: {
805 static Constant *MemsetFCache = 0;
806 Value *Size = CI->getOperand(3);
807 const Type *IntPtr = TD.getIntPtrType();
808 if (Size->getType()->getPrimitiveSizeInBits() <
809 IntPtr->getPrimitiveSizeInBits())
810 Size = new ZExtInst(Size, IntPtr, "", CI);
811 else if (Size->getType()->getPrimitiveSizeInBits() >
812 IntPtr->getPrimitiveSizeInBits())
813 Size = new TruncInst(Size, IntPtr, "", CI);
814 Value *Ops[3];
815 Ops[0] = CI->getOperand(1);
816 // Extend the amount to i32.
817 Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI);
818 Ops[2] = Size;
819 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
820 MemsetFCache);
821 break;
822 }
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000823 case Intrinsic::sqrt: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 static Constant *sqrtfFCache = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825 static Constant *sqrtFCache = 0;
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000826 static Constant *sqrtLDCache = 0;
827 switch (CI->getOperand(1)->getType()->getTypeID()) {
828 default: assert(0 && "Invalid type in sqrt"); abort();
829 case Type::FloatTyID:
830 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
831 Type::FloatTy, sqrtfFCache);
832 break;
833 case Type::DoubleTyID:
834 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 Type::DoubleTy, sqrtFCache);
Dale Johannesenc339d8e2007-10-02 17:43:59 +0000836 break;
837 case Type::X86_FP80TyID:
838 case Type::FP128TyID:
839 case Type::PPC_FP128TyID:
840 ReplaceCallWith("sqrtl", CI, CI->op_begin()+1, CI->op_end(),
841 CI->getOperand(1)->getType(), sqrtLDCache);
842 break;
843 }
Dale Johannesen3b5303b2007-09-28 18:06:58 +0000844 break;
845 }
Dale Johannesen92b33082008-09-04 00:47:13 +0000846 case Intrinsic::log: {
847 static Constant *logfFCache = 0;
848 static Constant *logFCache = 0;
849 static Constant *logLDCache = 0;
850 switch (CI->getOperand(1)->getType()->getTypeID()) {
851 default: assert(0 && "Invalid type in log"); abort();
852 case Type::FloatTyID:
853 ReplaceCallWith("logf", CI, CI->op_begin()+1, CI->op_end(),
854 Type::FloatTy, logfFCache);
855 break;
856 case Type::DoubleTyID:
857 ReplaceCallWith("log", CI, CI->op_begin()+1, CI->op_end(),
858 Type::DoubleTy, logFCache);
859 break;
860 case Type::X86_FP80TyID:
861 case Type::FP128TyID:
862 case Type::PPC_FP128TyID:
863 ReplaceCallWith("logl", CI, CI->op_begin()+1, CI->op_end(),
864 CI->getOperand(1)->getType(), logLDCache);
865 break;
866 }
867 break;
868 }
869 case Intrinsic::log2: {
870 static Constant *log2fFCache = 0;
871 static Constant *log2FCache = 0;
872 static Constant *log2LDCache = 0;
873 switch (CI->getOperand(1)->getType()->getTypeID()) {
874 default: assert(0 && "Invalid type in log2"); abort();
875 case Type::FloatTyID:
876 ReplaceCallWith("log2f", CI, CI->op_begin()+1, CI->op_end(),
877 Type::FloatTy, log2fFCache);
878 break;
879 case Type::DoubleTyID:
880 ReplaceCallWith("log2", CI, CI->op_begin()+1, CI->op_end(),
881 Type::DoubleTy, log2FCache);
882 break;
883 case Type::X86_FP80TyID:
884 case Type::FP128TyID:
885 case Type::PPC_FP128TyID:
886 ReplaceCallWith("log2l", CI, CI->op_begin()+1, CI->op_end(),
887 CI->getOperand(1)->getType(), log2LDCache);
888 break;
889 }
890 break;
891 }
892 case Intrinsic::log10: {
893 static Constant *log10fFCache = 0;
894 static Constant *log10FCache = 0;
895 static Constant *log10LDCache = 0;
896 switch (CI->getOperand(1)->getType()->getTypeID()) {
897 default: assert(0 && "Invalid type in log10"); abort();
898 case Type::FloatTyID:
899 ReplaceCallWith("log10f", CI, CI->op_begin()+1, CI->op_end(),
900 Type::FloatTy, log10fFCache);
901 break;
902 case Type::DoubleTyID:
903 ReplaceCallWith("log10", CI, CI->op_begin()+1, CI->op_end(),
904 Type::DoubleTy, log10FCache);
905 break;
906 case Type::X86_FP80TyID:
907 case Type::FP128TyID:
908 case Type::PPC_FP128TyID:
909 ReplaceCallWith("log10l", CI, CI->op_begin()+1, CI->op_end(),
910 CI->getOperand(1)->getType(), log10LDCache);
911 break;
912 }
913 break;
914 }
915 case Intrinsic::exp: {
916 static Constant *expfFCache = 0;
917 static Constant *expFCache = 0;
918 static Constant *expLDCache = 0;
919 switch (CI->getOperand(1)->getType()->getTypeID()) {
920 default: assert(0 && "Invalid type in exp"); abort();
921 case Type::FloatTyID:
922 ReplaceCallWith("expf", CI, CI->op_begin()+1, CI->op_end(),
923 Type::FloatTy, expfFCache);
924 break;
925 case Type::DoubleTyID:
926 ReplaceCallWith("exp", CI, CI->op_begin()+1, CI->op_end(),
927 Type::DoubleTy, expFCache);
928 break;
929 case Type::X86_FP80TyID:
930 case Type::FP128TyID:
931 case Type::PPC_FP128TyID:
932 ReplaceCallWith("expl", CI, CI->op_begin()+1, CI->op_end(),
933 CI->getOperand(1)->getType(), expLDCache);
934 break;
935 }
936 break;
937 }
938 case Intrinsic::exp2: {
939 static Constant *exp2fFCache = 0;
940 static Constant *exp2FCache = 0;
941 static Constant *exp2LDCache = 0;
942 switch (CI->getOperand(1)->getType()->getTypeID()) {
943 default: assert(0 && "Invalid type in exp2"); abort();
944 case Type::FloatTyID:
945 ReplaceCallWith("exp2f", CI, CI->op_begin()+1, CI->op_end(),
946 Type::FloatTy, exp2fFCache);
947 break;
948 case Type::DoubleTyID:
949 ReplaceCallWith("exp2", CI, CI->op_begin()+1, CI->op_end(),
950 Type::DoubleTy, exp2FCache);
951 break;
952 case Type::X86_FP80TyID:
953 case Type::FP128TyID:
954 case Type::PPC_FP128TyID:
955 ReplaceCallWith("exp2l", CI, CI->op_begin()+1, CI->op_end(),
956 CI->getOperand(1)->getType(), exp2LDCache);
957 break;
958 }
959 break;
960 }
961 case Intrinsic::pow: {
962 static Constant *powfFCache = 0;
963 static Constant *powFCache = 0;
964 static Constant *powLDCache = 0;
965 switch (CI->getOperand(1)->getType()->getTypeID()) {
966 default: assert(0 && "Invalid type in pow"); abort();
967 case Type::FloatTyID:
968 ReplaceCallWith("powf", CI, CI->op_begin()+1, CI->op_end(),
969 Type::FloatTy, powfFCache);
970 break;
971 case Type::DoubleTyID:
972 ReplaceCallWith("pow", CI, CI->op_begin()+1, CI->op_end(),
973 Type::DoubleTy, powFCache);
974 break;
975 case Type::X86_FP80TyID:
976 case Type::FP128TyID:
977 case Type::PPC_FP128TyID:
978 ReplaceCallWith("powl", CI, CI->op_begin()+1, CI->op_end(),
979 CI->getOperand(1)->getType(), powLDCache);
980 break;
981 }
982 break;
983 }
Anton Korobeynikovc915e272007-11-15 23:25:33 +0000984 case Intrinsic::flt_rounds:
985 // Lower to "round to the nearest"
986 if (CI->getType() != Type::VoidTy)
987 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
988 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 }
990
991 assert(CI->use_empty() &&
992 "Lowering should have eliminated any uses of the intrinsic call!");
993 CI->eraseFromParent();
994}