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 default intrinsic lowering implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/IntrinsicLowering.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Type.h"
24 template <class ArgIt>
25 static Function *EnsureFunctionExists(Module &M, const char *Name,
26 ArgIt ArgBegin, ArgIt ArgEnd,
28 if (Function *F = M.getNamedFunction(Name)) return F;
29 // It doesn't already exist in the program, insert a new definition now.
30 std::vector<const Type *> ParamTys;
31 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
32 ParamTys.push_back(I->getType());
33 return M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
36 /// ReplaceCallWith - This function is used when we want to lower an intrinsic
37 /// call to a call of an external function. This handles hard cases such as
38 /// when there was already a prototype for the external function, and if that
39 /// prototype doesn't match the arguments we expect to pass in.
40 template <class ArgIt>
41 static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
42 ArgIt ArgBegin, ArgIt ArgEnd,
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 Arg = new CastInst(Arg, FT->getParamType(ArgNo), Arg->getName(), CI);
67 Operands.push_back(Arg);
69 // Pass nulls into any additional arguments...
70 for (; ArgNo != FT->getNumParams(); ++ArgNo)
71 Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
73 std::string Name = CI->getName(); CI->setName("");
74 if (FT->getReturnType() == Type::VoidTy) Name.clear();
75 CallInst *NewCI = new CallInst(FCache, Operands, Name, CI);
76 if (!CI->use_empty()) {
78 if (CI->getType() != NewCI->getType())
79 V = new CastInst(NewCI, CI->getType(), Name, CI);
80 CI->replaceAllUsesWith(V);
85 void DefaultIntrinsicLowering::AddPrototypes(Module &M) {
86 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
87 if (I->isExternal() && !I->use_empty())
88 switch (I->getIntrinsicID()) {
90 case Intrinsic::setjmp:
91 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
94 case Intrinsic::longjmp:
95 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
98 case Intrinsic::siglongjmp:
99 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
102 case Intrinsic::memcpy_i32:
103 case Intrinsic::memcpy_i64:
104 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
105 I->arg_begin()->getType());
107 case Intrinsic::memmove_i32:
108 case Intrinsic::memmove_i64:
109 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
110 I->arg_begin()->getType());
112 case Intrinsic::memset_i32:
113 case Intrinsic::memset_i64:
114 M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
115 PointerType::get(Type::SByteTy),
116 Type::IntTy, (--(--I->arg_end()))->getType(),
119 case Intrinsic::isunordered_f32:
120 case Intrinsic::isunordered_f64:
121 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
124 case Intrinsic::sqrt_f32:
125 case Intrinsic::sqrt_f64:
126 if(I->arg_begin()->getType() == Type::FloatTy)
127 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
130 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
136 /// LowerBSWAP - Emit the code to lower bswap of V before the specified
138 static Value *LowerBSWAP(Value *V, Instruction *IP) {
139 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
141 const Type *DestTy = V->getType();
143 // Force to unsigned so that the shift rights are logical.
144 if (DestTy->isSigned())
145 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
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::UByteTy,8),"bswap.2",IP);
154 Value *Tmp2 = new ShiftInst(Instruction::Shr, V,
155 ConstantInt::get(Type::UByteTy,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::UByteTy,24),"bswap.4", IP);
162 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
163 ConstantInt::get(Type::UByteTy,8),"bswap.3",IP);
164 Value *Tmp2 = new ShiftInst(Instruction::Shr, V,
165 ConstantInt::get(Type::UByteTy,8),"bswap.2",IP);
166 Value *Tmp1 = new ShiftInst(Instruction::Shr, V,
167 ConstantInt::get(Type::UByteTy,24),"bswap.1", IP);
168 Tmp3 = BinaryOperator::createAnd(Tmp3,
169 ConstantUInt::get(Type::UIntTy, 0xFF0000),
171 Tmp2 = BinaryOperator::createAnd(Tmp2,
172 ConstantUInt::get(Type::UIntTy, 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::UByteTy,56),"bswap.8", IP);
182 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
183 ConstantInt::get(Type::UByteTy,40),"bswap.7", IP);
184 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
185 ConstantInt::get(Type::UByteTy,24),"bswap.6", IP);
186 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
187 ConstantInt::get(Type::UByteTy,8),"bswap.5",IP);
188 Value *Tmp4 = new ShiftInst(Instruction::Shr, V,
189 ConstantInt::get(Type::UByteTy,8),"bswap.4",IP);
190 Value *Tmp3 = new ShiftInst(Instruction::Shr, V,
191 ConstantInt::get(Type::UByteTy,24),"bswap.3", IP);
192 Value *Tmp2 = new ShiftInst(Instruction::Shr, V,
193 ConstantInt::get(Type::UByteTy,40),"bswap.2", IP);
194 Value *Tmp1 = new ShiftInst(Instruction::Shr, V,
195 ConstantInt::get(Type::UByteTy,56),"bswap.1", IP);
196 Tmp7 = BinaryOperator::createAnd(Tmp7,
197 ConstantUInt::get(Type::ULongTy, 0xFF000000000000ULL),
199 Tmp6 = BinaryOperator::createAnd(Tmp6,
200 ConstantUInt::get(Type::ULongTy, 0xFF0000000000ULL),
202 Tmp5 = BinaryOperator::createAnd(Tmp5,
203 ConstantUInt::get(Type::ULongTy, 0xFF00000000ULL),
205 Tmp4 = BinaryOperator::createAnd(Tmp4,
206 ConstantUInt::get(Type::ULongTy, 0xFF000000ULL),
208 Tmp3 = BinaryOperator::createAnd(Tmp3,
209 ConstantUInt::get(Type::ULongTy, 0xFF0000ULL),
211 Tmp2 = BinaryOperator::createAnd(Tmp2,
212 ConstantUInt::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);
225 if (V->getType() != DestTy)
226 V = new CastInst(V, DestTy, V->getName(), IP);
230 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
232 static Value *LowerCTPOP(Value *V, Instruction *IP) {
233 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
235 static const uint64_t MaskValues[6] = {
236 0x5555555555555555ULL, 0x3333333333333333ULL,
237 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
238 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
241 const Type *DestTy = V->getType();
243 // Force to unsigned so that the shift rights are logical.
244 if (DestTy->isSigned())
245 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
247 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
248 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
250 ConstantExpr::getCast(ConstantUInt::get(Type::ULongTy,
251 MaskValues[ct]), V->getType());
252 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
253 Value *VShift = new ShiftInst(Instruction::Shr, V,
254 ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
255 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
256 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
259 if (V->getType() != DestTy)
260 V = new CastInst(V, DestTy, V->getName(), IP);
264 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
266 static Value *LowerCTLZ(Value *V, Instruction *IP) {
267 const Type *DestTy = V->getType();
269 // Force to unsigned so that the shift rights are logical.
270 if (DestTy->isSigned())
271 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
273 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
274 for (unsigned i = 1; i != BitSize; i <<= 1) {
275 Value *ShVal = ConstantInt::get(Type::UByteTy, i);
276 ShVal = new ShiftInst(Instruction::Shr, V, ShVal, "ctlz.sh", IP);
277 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
280 if (V->getType() != DestTy)
281 V = new CastInst(V, DestTy, V->getName(), IP);
283 V = BinaryOperator::createNot(V, "", IP);
284 return LowerCTPOP(V, IP);
289 void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
290 Function *Callee = CI->getCalledFunction();
291 assert(Callee && "Cannot lower an indirect call!");
293 switch (Callee->getIntrinsicID()) {
294 case Intrinsic::not_intrinsic:
295 std::cerr << "Cannot lower a call to a non-intrinsic function '"
296 << Callee->getName() << "'!\n";
299 std::cerr << "Error: Code generator does not support intrinsic function '"
300 << Callee->getName() << "'!\n";
303 // The setjmp/longjmp intrinsics should only exist in the code if it was
304 // never optimized (ie, right out of the CFE), or if it has been hacked on
305 // by the lowerinvoke pass. In both cases, the right thing to do is to
306 // convert the call to an explicit setjmp or longjmp call.
307 case Intrinsic::setjmp: {
308 static Function *SetjmpFCache = 0;
309 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
310 Type::IntTy, SetjmpFCache);
311 if (CI->getType() != Type::VoidTy)
312 CI->replaceAllUsesWith(V);
315 case Intrinsic::sigsetjmp:
316 if (CI->getType() != Type::VoidTy)
317 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
320 case Intrinsic::longjmp: {
321 static Function *LongjmpFCache = 0;
322 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
323 Type::VoidTy, LongjmpFCache);
327 case Intrinsic::siglongjmp: {
328 // Insert the call to abort
329 static Function *AbortFCache = 0;
330 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy,
334 case Intrinsic::ctpop_i8:
335 case Intrinsic::ctpop_i16:
336 case Intrinsic::ctpop_i32:
337 case Intrinsic::ctpop_i64:
338 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
341 case Intrinsic::bswap_i16:
342 case Intrinsic::bswap_i32:
343 case Intrinsic::bswap_i64:
344 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
347 case Intrinsic::ctlz_i8:
348 case Intrinsic::ctlz_i16:
349 case Intrinsic::ctlz_i32:
350 case Intrinsic::ctlz_i64:
351 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
354 case Intrinsic::cttz_i8:
355 case Intrinsic::cttz_i16:
356 case Intrinsic::cttz_i32:
357 case Intrinsic::cttz_i64: {
358 // cttz(x) -> ctpop(~X & (X-1))
359 Value *Src = CI->getOperand(1);
360 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
361 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
362 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
363 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
364 CI->replaceAllUsesWith(Src);
368 case Intrinsic::stacksave:
369 case Intrinsic::stackrestore: {
370 static bool Warned = false;
372 std::cerr << "WARNING: this target does not support the llvm.stack"
373 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
374 "save" : "restore") << " intrinsic.\n";
376 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
377 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
381 case Intrinsic::returnaddress:
382 case Intrinsic::frameaddress:
383 std::cerr << "WARNING: this target does not support the llvm."
384 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
385 "return" : "frame") << "address intrinsic.\n";
386 CI->replaceAllUsesWith(ConstantPointerNull::get(
387 cast<PointerType>(CI->getType())));
390 case Intrinsic::prefetch:
391 break; // Simply strip out prefetches on unsupported architectures
393 case Intrinsic::pcmarker:
394 break; // Simply strip out pcmarker on unsupported architectures
395 case Intrinsic::readcyclecounter: {
396 std::cerr << "WARNING: this target does not support the llvm.readcyclecoun"
397 << "ter intrinsic. It is being lowered to a constant 0\n";
398 CI->replaceAllUsesWith(ConstantUInt::get(Type::ULongTy, 0));
402 case Intrinsic::dbg_stoppoint:
403 case Intrinsic::dbg_region_start:
404 case Intrinsic::dbg_region_end:
405 case Intrinsic::dbg_func_start:
406 if (CI->getType() != Type::VoidTy)
407 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
408 break; // Simply strip out debugging intrinsics
410 case Intrinsic::memcpy_i32:
411 case Intrinsic::memcpy_i64: {
412 // The memcpy intrinsic take an extra alignment argument that the memcpy
413 // libc function does not.
414 static Function *MemcpyFCache = 0;
415 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
416 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
419 case Intrinsic::memmove_i32:
420 case Intrinsic::memmove_i64: {
421 // The memmove intrinsic take an extra alignment argument that the memmove
422 // libc function does not.
423 static Function *MemmoveFCache = 0;
424 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
425 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
428 case Intrinsic::memset_i32:
429 case Intrinsic::memset_i64: {
430 // The memset intrinsic take an extra alignment argument that the memset
431 // libc function does not.
432 static Function *MemsetFCache = 0;
433 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
434 (*(CI->op_begin()+1))->getType(), MemsetFCache);
437 case Intrinsic::isunordered_f32:
438 case Intrinsic::isunordered_f64: {
439 Value *L = CI->getOperand(1);
440 Value *R = CI->getOperand(2);
442 Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
443 Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
444 CI->replaceAllUsesWith(
445 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
449 case Intrinsic::sqrt_f32:
450 case Intrinsic::sqrt_f64: {
451 static Function *sqrtFCache = 0;
452 static Function *sqrtfFCache = 0;
453 if(CI->getType() == Type::FloatTy)
454 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
455 Type::FloatTy, sqrtfFCache);
457 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
458 Type::DoubleTy, sqrtFCache);
463 assert(CI->use_empty() &&
464 "Lowering should have eliminated any uses of the intrinsic call!");
465 CI->eraseFromParent();