1 //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the IntrinsicLowering class.
12 //===----------------------------------------------------------------------===//
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"
23 template <class ArgIt>
24 static Function *EnsureFunctionExists(Module &M, const char *Name,
25 ArgIt ArgBegin, ArgIt ArgEnd,
27 if (Function *F = M.getNamedFunction(Name)) return F;
28 // It doesn't already exist in the program, insert a new definition now.
29 std::vector<const Type *> ParamTys;
30 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
31 ParamTys.push_back(I->getType());
32 return M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
35 /// ReplaceCallWith - This function is used when we want to lower an intrinsic
36 /// call to a call of an external function. This handles hard cases such as
37 /// when there was already a prototype for the external function, and if that
38 /// prototype doesn't match the arguments we expect to pass in.
39 template <class ArgIt>
40 static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
41 ArgIt ArgBegin, ArgIt ArgEnd, bool isSigned,
42 const Type *RetTy, Function *&FCache) {
44 // If we haven't already looked up this function, check to see if the
45 // program already contains a function with this name.
46 Module *M = CI->getParent()->getParent()->getParent();
47 FCache = M->getNamedFunction(NewFn);
49 // It doesn't already exist in the program, insert a new definition now.
50 std::vector<const Type *> ParamTys;
51 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
52 ParamTys.push_back((*I)->getType());
53 FCache = M->getOrInsertFunction(NewFn,
54 FunctionType::get(RetTy, ParamTys, false));
58 const FunctionType *FT = FCache->getFunctionType();
59 std::vector<Value*> Operands;
61 for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams();
64 if (Arg->getType() != FT->getParamType(ArgNo)) {
65 Instruction::CastOps opcode = CastInst::getCastOpcode(Arg, isSigned,
66 FT->getParamType(ArgNo), isSigned);
67 Arg = CastInst::create(opcode, Arg, FT->getParamType(ArgNo),
70 Operands.push_back(Arg);
72 // Pass nulls into any additional arguments...
73 for (; ArgNo != FT->getNumParams(); ++ArgNo)
74 Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
76 std::string Name = CI->getName(); CI->setName("");
77 if (FT->getReturnType() == Type::VoidTy) Name.clear();
78 CallInst *NewCI = new CallInst(FCache, Operands, Name, CI);
79 if (!CI->use_empty()) {
81 if (CI->getType() != NewCI->getType()) {
82 Instruction::CastOps opcode = CastInst::getCastOpcode(NewCI, isSigned,
83 CI->getType(), isSigned);
84 V = CastInst::create(opcode, NewCI, CI->getType(), Name, CI);
86 CI->replaceAllUsesWith(V);
91 void IntrinsicLowering::AddPrototypes(Module &M) {
92 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
93 if (I->isExternal() && !I->use_empty())
94 switch (I->getIntrinsicID()) {
96 case Intrinsic::setjmp:
97 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
100 case Intrinsic::longjmp:
101 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
104 case Intrinsic::siglongjmp:
105 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
108 case Intrinsic::memcpy_i32:
109 case Intrinsic::memcpy_i64:
110 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
111 I->arg_begin()->getType());
113 case Intrinsic::memmove_i32:
114 case Intrinsic::memmove_i64:
115 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
116 I->arg_begin()->getType());
118 case Intrinsic::memset_i32:
119 case Intrinsic::memset_i64:
120 M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty),
121 PointerType::get(Type::Int8Ty),
122 Type::Int32Ty, (--(--I->arg_end()))->getType(),
125 case Intrinsic::isunordered_f32:
126 case Intrinsic::isunordered_f64:
127 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
130 case Intrinsic::sqrt_f32:
131 case Intrinsic::sqrt_f64:
132 if(I->arg_begin()->getType() == Type::FloatTy)
133 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
136 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
142 /// LowerBSWAP - Emit the code to lower bswap of V before the specified
144 static Value *LowerBSWAP(Value *V, Instruction *IP) {
145 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
147 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
150 default: assert(0 && "Unhandled type size of value to byteswap!");
152 Value *Tmp1 = new ShiftInst(Instruction::Shl, V,
153 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
154 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
155 ConstantInt::get(Type::Int8Ty,8),"bswap.1",IP);
156 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
160 Value *Tmp4 = new ShiftInst(Instruction::Shl, V,
161 ConstantInt::get(Type::Int8Ty,24),"bswap.4", IP);
162 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
163 ConstantInt::get(Type::Int8Ty,8),"bswap.3",IP);
164 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
165 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
166 Value *Tmp1 = new ShiftInst(Instruction::LShr, V,
167 ConstantInt::get(Type::Int8Ty,24),"bswap.1", IP);
168 Tmp3 = BinaryOperator::createAnd(Tmp3,
169 ConstantInt::get(Type::Int32Ty, 0xFF0000),
171 Tmp2 = BinaryOperator::createAnd(Tmp2,
172 ConstantInt::get(Type::Int32Ty, 0xFF00),
174 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
175 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
176 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
180 Value *Tmp8 = new ShiftInst(Instruction::Shl, V,
181 ConstantInt::get(Type::Int8Ty,56),"bswap.8", IP);
182 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
183 ConstantInt::get(Type::Int8Ty,40),"bswap.7", IP);
184 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
185 ConstantInt::get(Type::Int8Ty,24),"bswap.6", IP);
186 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
187 ConstantInt::get(Type::Int8Ty,8),"bswap.5", IP);
188 Value* Tmp4 = new ShiftInst(Instruction::LShr, V,
189 ConstantInt::get(Type::Int8Ty,8),"bswap.4", IP);
190 Value* Tmp3 = new ShiftInst(Instruction::LShr, V,
191 ConstantInt::get(Type::Int8Ty,24),"bswap.3", IP);
192 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
193 ConstantInt::get(Type::Int8Ty,40),"bswap.2", IP);
194 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
195 ConstantInt::get(Type::Int8Ty,56),"bswap.1", IP);
196 Tmp7 = BinaryOperator::createAnd(Tmp7,
197 ConstantInt::get(Type::Int64Ty,
198 0xFF000000000000ULL),
200 Tmp6 = BinaryOperator::createAnd(Tmp6,
201 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
203 Tmp5 = BinaryOperator::createAnd(Tmp5,
204 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
206 Tmp4 = BinaryOperator::createAnd(Tmp4,
207 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
209 Tmp3 = BinaryOperator::createAnd(Tmp3,
210 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
212 Tmp2 = BinaryOperator::createAnd(Tmp2,
213 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
215 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
216 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
217 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
218 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
219 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
220 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
221 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
228 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
230 static Value *LowerCTPOP(Value *V, Instruction *IP) {
231 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
233 static const uint64_t MaskValues[6] = {
234 0x5555555555555555ULL, 0x3333333333333333ULL,
235 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
236 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
239 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
241 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
242 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
243 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
244 Value *VShift = new ShiftInst(Instruction::LShr, V,
245 ConstantInt::get(Type::Int8Ty, i), "ctpop.sh", IP);
246 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
247 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
253 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
255 static Value *LowerCTLZ(Value *V, Instruction *IP) {
257 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
258 for (unsigned i = 1; i != BitSize; i <<= 1) {
259 Value *ShVal = ConstantInt::get(Type::Int8Ty, i);
260 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
261 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
264 V = BinaryOperator::createNot(V, "", IP);
265 return LowerCTPOP(V, IP);
270 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
271 Function *Callee = CI->getCalledFunction();
272 assert(Callee && "Cannot lower an indirect call!");
274 switch (Callee->getIntrinsicID()) {
275 case Intrinsic::not_intrinsic:
276 cerr << "Cannot lower a call to a non-intrinsic function '"
277 << Callee->getName() << "'!\n";
280 cerr << "Error: Code generator does not support intrinsic function '"
281 << Callee->getName() << "'!\n";
284 // The setjmp/longjmp intrinsics should only exist in the code if it was
285 // never optimized (ie, right out of the CFE), or if it has been hacked on
286 // by the lowerinvoke pass. In both cases, the right thing to do is to
287 // convert the call to an explicit setjmp or longjmp call.
288 case Intrinsic::setjmp: {
289 static Function *SetjmpFCache = 0;
290 static const unsigned castOpcodes[] = { Instruction::BitCast };
291 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
292 castOpcodes, Type::Int32Ty, SetjmpFCache);
293 if (CI->getType() != Type::VoidTy)
294 CI->replaceAllUsesWith(V);
297 case Intrinsic::sigsetjmp:
298 if (CI->getType() != Type::VoidTy)
299 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
302 case Intrinsic::longjmp: {
303 static Function *LongjmpFCache = 0;
304 static const unsigned castOpcodes[] =
305 { Instruction::BitCast, 0 };
306 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
307 castOpcodes, Type::VoidTy, LongjmpFCache);
311 case Intrinsic::siglongjmp: {
312 // Insert the call to abort
313 static Function *AbortFCache = 0;
314 static const unsigned castOpcodes[] =
315 { Instruction::BitCast, 0 };
316 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
317 castOpcodes, Type::VoidTy, AbortFCache);
320 case Intrinsic::ctpop_i8:
321 case Intrinsic::ctpop_i16:
322 case Intrinsic::ctpop_i32:
323 case Intrinsic::ctpop_i64:
324 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
327 case Intrinsic::bswap_i16:
328 case Intrinsic::bswap_i32:
329 case Intrinsic::bswap_i64:
330 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
333 case Intrinsic::ctlz_i8:
334 case Intrinsic::ctlz_i16:
335 case Intrinsic::ctlz_i32:
336 case Intrinsic::ctlz_i64:
337 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
340 case Intrinsic::cttz_i8:
341 case Intrinsic::cttz_i16:
342 case Intrinsic::cttz_i32:
343 case Intrinsic::cttz_i64: {
344 // cttz(x) -> ctpop(~X & (X-1))
345 Value *Src = CI->getOperand(1);
346 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
347 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
348 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
349 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
350 CI->replaceAllUsesWith(Src);
354 case Intrinsic::stacksave:
355 case Intrinsic::stackrestore: {
356 static bool Warned = false;
358 cerr << "WARNING: this target does not support the llvm.stack"
359 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
360 "save" : "restore") << " intrinsic.\n";
362 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
363 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
367 case Intrinsic::returnaddress:
368 case Intrinsic::frameaddress:
369 cerr << "WARNING: this target does not support the llvm."
370 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
371 "return" : "frame") << "address intrinsic.\n";
372 CI->replaceAllUsesWith(ConstantPointerNull::get(
373 cast<PointerType>(CI->getType())));
376 case Intrinsic::prefetch:
377 break; // Simply strip out prefetches on unsupported architectures
379 case Intrinsic::pcmarker:
380 break; // Simply strip out pcmarker on unsupported architectures
381 case Intrinsic::readcyclecounter: {
382 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
383 << "ter intrinsic. It is being lowered to a constant 0\n";
384 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
388 case Intrinsic::dbg_stoppoint:
389 case Intrinsic::dbg_region_start:
390 case Intrinsic::dbg_region_end:
391 case Intrinsic::dbg_func_start:
392 case Intrinsic::dbg_declare:
393 break; // Simply strip out debugging intrinsics
395 case Intrinsic::memcpy_i32: {
396 static Function *MemcpyFCache = 0;
397 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
398 false, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
401 case Intrinsic::memcpy_i64: {
402 static Function *MemcpyFCache = 0;
403 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
404 false, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
407 case Intrinsic::memmove_i32: {
408 static Function *MemmoveFCache = 0;
409 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
410 false, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
413 case Intrinsic::memmove_i64: {
414 static Function *MemmoveFCache = 0;
415 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
416 false, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
419 case Intrinsic::memset_i32: {
420 static Function *MemsetFCache = 0;
421 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
422 true, (*(CI->op_begin()+1))->getType(), MemsetFCache);
424 case Intrinsic::memset_i64: {
425 static Function *MemsetFCache = 0;
426 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
427 true, (*(CI->op_begin()+1))->getType(), MemsetFCache);
430 case Intrinsic::isunordered_f32:
431 case Intrinsic::isunordered_f64: {
432 Value *L = CI->getOperand(1);
433 Value *R = CI->getOperand(2);
435 Value *LIsNan = new FCmpInst(FCmpInst::FCMP_ONE, L, L, "LIsNan", CI);
436 Value *RIsNan = new FCmpInst(FCmpInst::FCMP_ONE, R, R, "RIsNan", CI);
437 CI->replaceAllUsesWith(
438 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
442 case Intrinsic::sqrt_f32: {
443 static Function *sqrtfFCache = 0;
444 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
445 false, Type::FloatTy, sqrtfFCache);
448 case Intrinsic::sqrt_f64: {
449 static Function *sqrtFCache = 0;
450 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
451 false, Type::DoubleTy, sqrtFCache);
456 assert(CI->use_empty() &&
457 "Lowering should have eliminated any uses of the intrinsic call!");
458 CI->eraseFromParent();