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,
42 const unsigned *castOpcodes,
43 const Type *RetTy, Function *&FCache) {
45 // If we haven't already looked up this function, check to see if the
46 // program already contains a function with this name.
47 Module *M = CI->getParent()->getParent()->getParent();
48 FCache = M->getNamedFunction(NewFn);
50 // It doesn't already exist in the program, insert a new definition now.
51 std::vector<const Type *> ParamTys;
52 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
53 ParamTys.push_back((*I)->getType());
54 FCache = M->getOrInsertFunction(NewFn,
55 FunctionType::get(RetTy, ParamTys, false));
59 const FunctionType *FT = FCache->getFunctionType();
60 std::vector<Value*> Operands;
62 for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams();
65 if (Arg->getType() != FT->getParamType(ArgNo))
66 if (castOpcodes[ArgNo])
67 Arg = CastInst::create(Instruction::CastOps(castOpcodes[ArgNo]),
68 Arg, FT->getParamType(ArgNo), Arg->getName(), CI);
70 Arg = CastInst::createInferredCast(Arg, FT->getParamType(ArgNo),
72 Operands.push_back(Arg);
74 // Pass nulls into any additional arguments...
75 for (; ArgNo != FT->getNumParams(); ++ArgNo)
76 Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
78 std::string Name = CI->getName(); CI->setName("");
79 if (FT->getReturnType() == Type::VoidTy) Name.clear();
80 CallInst *NewCI = new CallInst(FCache, Operands, Name, CI);
81 if (!CI->use_empty()) {
83 if (CI->getType() != NewCI->getType())
84 V = CastInst::createInferredCast(NewCI, CI->getType(), Name, CI);
85 CI->replaceAllUsesWith(V);
90 void IntrinsicLowering::AddPrototypes(Module &M) {
91 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
92 if (I->isExternal() && !I->use_empty())
93 switch (I->getIntrinsicID()) {
95 case Intrinsic::setjmp:
96 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
99 case Intrinsic::longjmp:
100 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
103 case Intrinsic::siglongjmp:
104 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
107 case Intrinsic::memcpy_i32:
108 case Intrinsic::memcpy_i64:
109 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
110 I->arg_begin()->getType());
112 case Intrinsic::memmove_i32:
113 case Intrinsic::memmove_i64:
114 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
115 I->arg_begin()->getType());
117 case Intrinsic::memset_i32:
118 case Intrinsic::memset_i64:
119 M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
120 PointerType::get(Type::SByteTy),
121 Type::IntTy, (--(--I->arg_end()))->getType(),
124 case Intrinsic::isunordered_f32:
125 case Intrinsic::isunordered_f64:
126 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
129 case Intrinsic::sqrt_f32:
130 case Intrinsic::sqrt_f64:
131 if(I->arg_begin()->getType() == Type::FloatTy)
132 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
135 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
141 /// LowerBSWAP - Emit the code to lower bswap of V before the specified
143 static Value *LowerBSWAP(Value *V, Instruction *IP) {
144 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
146 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
149 default: assert(0 && "Unhandled type size of value to byteswap!");
151 Value *Tmp1 = new ShiftInst(Instruction::Shl, V,
152 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
153 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
154 ConstantInt::get(Type::UByteTy,8),"bswap.1",IP);
155 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
159 Value *Tmp4 = new ShiftInst(Instruction::Shl, V,
160 ConstantInt::get(Type::UByteTy,24),"bswap.4", IP);
161 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
162 ConstantInt::get(Type::UByteTy,8),"bswap.3",IP);
163 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
164 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
165 Value *Tmp1 = new ShiftInst(Instruction::LShr, V,
166 ConstantInt::get(Type::UByteTy,24),"bswap.1", IP);
167 Tmp3 = BinaryOperator::createAnd(Tmp3,
168 ConstantInt::get(Type::UIntTy, 0xFF0000),
170 Tmp2 = BinaryOperator::createAnd(Tmp2,
171 ConstantInt::get(Type::UIntTy, 0xFF00),
173 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
174 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
175 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
179 Value *Tmp8 = new ShiftInst(Instruction::Shl, V,
180 ConstantInt::get(Type::UByteTy,56),"bswap.8", IP);
181 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
182 ConstantInt::get(Type::UByteTy,40),"bswap.7", IP);
183 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
184 ConstantInt::get(Type::UByteTy,24),"bswap.6", IP);
185 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
186 ConstantInt::get(Type::UByteTy,8),"bswap.5", IP);
187 Value* Tmp4 = new ShiftInst(Instruction::LShr, V,
188 ConstantInt::get(Type::UByteTy,8),"bswap.4", IP);
189 Value* Tmp3 = new ShiftInst(Instruction::LShr, V,
190 ConstantInt::get(Type::UByteTy,24),"bswap.3", IP);
191 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
192 ConstantInt::get(Type::UByteTy,40),"bswap.2", IP);
193 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
194 ConstantInt::get(Type::UByteTy,56),"bswap.1", IP);
195 Tmp7 = BinaryOperator::createAnd(Tmp7,
196 ConstantInt::get(Type::ULongTy,
197 0xFF000000000000ULL),
199 Tmp6 = BinaryOperator::createAnd(Tmp6,
200 ConstantInt::get(Type::ULongTy, 0xFF0000000000ULL),
202 Tmp5 = BinaryOperator::createAnd(Tmp5,
203 ConstantInt::get(Type::ULongTy, 0xFF00000000ULL),
205 Tmp4 = BinaryOperator::createAnd(Tmp4,
206 ConstantInt::get(Type::ULongTy, 0xFF000000ULL),
208 Tmp3 = BinaryOperator::createAnd(Tmp3,
209 ConstantInt::get(Type::ULongTy, 0xFF0000ULL),
211 Tmp2 = BinaryOperator::createAnd(Tmp2,
212 ConstantInt::get(Type::ULongTy, 0xFF00ULL),
214 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
215 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
216 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
217 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
218 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
219 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
220 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
227 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
229 static Value *LowerCTPOP(Value *V, Instruction *IP) {
230 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
232 static const uint64_t MaskValues[6] = {
233 0x5555555555555555ULL, 0x3333333333333333ULL,
234 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
235 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
238 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
240 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
241 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
242 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
243 Value *VShift = new ShiftInst(Instruction::LShr, V,
244 ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
245 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
246 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
252 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
254 static Value *LowerCTLZ(Value *V, Instruction *IP) {
256 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
257 for (unsigned i = 1; i != BitSize; i <<= 1) {
258 Value *ShVal = ConstantInt::get(Type::UByteTy, i);
259 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
260 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
263 V = BinaryOperator::createNot(V, "", IP);
264 return LowerCTPOP(V, IP);
269 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
270 Function *Callee = CI->getCalledFunction();
271 assert(Callee && "Cannot lower an indirect call!");
273 switch (Callee->getIntrinsicID()) {
274 case Intrinsic::not_intrinsic:
275 cerr << "Cannot lower a call to a non-intrinsic function '"
276 << Callee->getName() << "'!\n";
279 cerr << "Error: Code generator does not support intrinsic function '"
280 << Callee->getName() << "'!\n";
283 // The setjmp/longjmp intrinsics should only exist in the code if it was
284 // never optimized (ie, right out of the CFE), or if it has been hacked on
285 // by the lowerinvoke pass. In both cases, the right thing to do is to
286 // convert the call to an explicit setjmp or longjmp call.
287 case Intrinsic::setjmp: {
288 static Function *SetjmpFCache = 0;
289 static const unsigned castOpcodes[] = { Instruction::BitCast };
290 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
291 castOpcodes, Type::IntTy, SetjmpFCache);
292 if (CI->getType() != Type::VoidTy)
293 CI->replaceAllUsesWith(V);
296 case Intrinsic::sigsetjmp:
297 if (CI->getType() != Type::VoidTy)
298 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
301 case Intrinsic::longjmp: {
302 static Function *LongjmpFCache = 0;
303 static const unsigned castOpcodes[] =
304 { Instruction::BitCast, 0 };
305 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
306 castOpcodes, Type::VoidTy, LongjmpFCache);
310 case Intrinsic::siglongjmp: {
311 // Insert the call to abort
312 static Function *AbortFCache = 0;
313 static const unsigned castOpcodes[] =
314 { Instruction::BitCast, 0 };
315 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
316 castOpcodes, Type::VoidTy, AbortFCache);
319 case Intrinsic::ctpop_i8:
320 case Intrinsic::ctpop_i16:
321 case Intrinsic::ctpop_i32:
322 case Intrinsic::ctpop_i64:
323 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
326 case Intrinsic::bswap_i16:
327 case Intrinsic::bswap_i32:
328 case Intrinsic::bswap_i64:
329 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
332 case Intrinsic::ctlz_i8:
333 case Intrinsic::ctlz_i16:
334 case Intrinsic::ctlz_i32:
335 case Intrinsic::ctlz_i64:
336 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
339 case Intrinsic::cttz_i8:
340 case Intrinsic::cttz_i16:
341 case Intrinsic::cttz_i32:
342 case Intrinsic::cttz_i64: {
343 // cttz(x) -> ctpop(~X & (X-1))
344 Value *Src = CI->getOperand(1);
345 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
346 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
347 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
348 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
349 CI->replaceAllUsesWith(Src);
353 case Intrinsic::stacksave:
354 case Intrinsic::stackrestore: {
355 static bool Warned = false;
357 cerr << "WARNING: this target does not support the llvm.stack"
358 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
359 "save" : "restore") << " intrinsic.\n";
361 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
362 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
366 case Intrinsic::returnaddress:
367 case Intrinsic::frameaddress:
368 cerr << "WARNING: this target does not support the llvm."
369 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
370 "return" : "frame") << "address intrinsic.\n";
371 CI->replaceAllUsesWith(ConstantPointerNull::get(
372 cast<PointerType>(CI->getType())));
375 case Intrinsic::prefetch:
376 break; // Simply strip out prefetches on unsupported architectures
378 case Intrinsic::pcmarker:
379 break; // Simply strip out pcmarker on unsupported architectures
380 case Intrinsic::readcyclecounter: {
381 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
382 << "ter intrinsic. It is being lowered to a constant 0\n";
383 CI->replaceAllUsesWith(ConstantInt::get(Type::ULongTy, 0));
387 case Intrinsic::dbg_stoppoint:
388 case Intrinsic::dbg_region_start:
389 case Intrinsic::dbg_region_end:
390 case Intrinsic::dbg_func_start:
391 case Intrinsic::dbg_declare:
392 break; // Simply strip out debugging intrinsics
394 case Intrinsic::memcpy_i32: {
395 // The memcpy intrinsic take an extra alignment argument that the memcpy
396 // libc function does not.
397 static unsigned opcodes[] =
398 { Instruction::BitCast, Instruction::BitCast, Instruction::BitCast };
400 // if (target_is_64_bit) opcodes[2] = Instruction::ZExt;
401 // else opcodes[2] = Instruction::BitCast;
402 static Function *MemcpyFCache = 0;
403 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
404 opcodes, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
407 case Intrinsic::memcpy_i64: {
408 static unsigned opcodes[] =
409 { Instruction::BitCast, Instruction::BitCast, Instruction::Trunc };
411 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
412 // else opcodes[2] = Instruction::Trunc;
413 static Function *MemcpyFCache = 0;
414 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
415 opcodes, (*(CI->op_begin()+1))->getType(), MemcpyFCache);
418 case Intrinsic::memmove_i32: {
419 // The memmove intrinsic take an extra alignment argument that the memmove
420 // libc function does not.
421 static unsigned opcodes[] =
422 { Instruction::BitCast, Instruction::BitCast, Instruction::BitCast };
424 // if (target_is_64_bit) opcodes[2] = Instruction::ZExt;
425 // else opcodes[2] = Instruction::BitCast;
426 static Function *MemmoveFCache = 0;
427 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
428 opcodes, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
431 case Intrinsic::memmove_i64: {
432 // The memmove intrinsic take an extra alignment argument that the memmove
433 // libc function does not.
434 static const unsigned opcodes[] =
435 { Instruction::BitCast, Instruction::BitCast, Instruction::Trunc };
436 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
437 // else opcodes[2] = Instruction::Trunc;
438 static Function *MemmoveFCache = 0;
439 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
440 opcodes, (*(CI->op_begin()+1))->getType(), MemmoveFCache);
443 case Intrinsic::memset_i32: {
444 // The memset intrinsic take an extra alignment argument that the memset
445 // libc function does not.
446 static const unsigned opcodes[] =
447 { Instruction::BitCast, Instruction::ZExt, Instruction::ZExt, 0 };
448 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
449 // else opcodes[2] = Instruction::ZExt;
450 static Function *MemsetFCache = 0;
451 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
452 opcodes, (*(CI->op_begin()+1))->getType(), MemsetFCache);
454 case Intrinsic::memset_i64: {
455 // The memset intrinsic take an extra alignment argument that the memset
456 // libc function does not.
457 static const unsigned opcodes[] =
458 { Instruction::BitCast, Instruction::ZExt, Instruction::Trunc, 0 };
459 // if (target_is_64_bit) opcodes[2] = Instruction::BitCast;
460 // else opcodes[2] = Instruction::Trunc;
461 static Function *MemsetFCache = 0;
462 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
463 opcodes, (*(CI->op_begin()+1))->getType(), MemsetFCache);
466 case Intrinsic::isunordered_f32:
467 case Intrinsic::isunordered_f64: {
468 Value *L = CI->getOperand(1);
469 Value *R = CI->getOperand(2);
471 Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
472 Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
473 CI->replaceAllUsesWith(
474 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
478 case Intrinsic::sqrt_f32: {
479 static const unsigned opcodes[] = { 0 };
480 static Function *sqrtfFCache = 0;
481 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
482 opcodes, Type::FloatTy, sqrtfFCache);
485 case Intrinsic::sqrt_f64: {
486 static const unsigned opcodes[] = { 0 };
487 static Function *sqrtFCache = 0;
488 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
489 opcodes, Type::DoubleTy, sqrtFCache);
494 assert(CI->use_empty() &&
495 "Lowering should have eliminated any uses of the intrinsic call!");
496 CI->eraseFromParent();