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:
103 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
104 I->arg_begin()->getType());
106 case Intrinsic::memmove:
107 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
108 I->arg_begin()->getType());
110 case Intrinsic::memset:
111 M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
112 PointerType::get(Type::SByteTy),
113 Type::IntTy, (--(--I->arg_end()))->getType(), 0);
115 case Intrinsic::isunordered:
116 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
119 case Intrinsic::sqrt:
120 if(I->arg_begin()->getType() == Type::FloatTy)
121 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
124 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
130 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
132 static Value *LowerCTPOP(Value *V, Instruction *IP) {
133 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
134 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
136 static const uint64_t MaskValues[6] = {
137 0x5555555555555555ULL, 0x3333333333333333ULL,
138 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
139 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
142 const Type *DestTy = V->getType();
144 // Force to unsigned so that the shift rights are logical.
145 if (DestTy->isSigned())
146 V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
148 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
150 ConstantExpr::getCast(ConstantUInt::get(Type::ULongTy,
151 MaskValues[ct]), V->getType());
152 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
153 Value *VShift = new ShiftInst(Instruction::Shr, V,
154 ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
155 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
156 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
159 if (V->getType() != DestTy)
160 V = new CastInst(V, DestTy, V->getName(), IP);
164 void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
165 Function *Callee = CI->getCalledFunction();
166 assert(Callee && "Cannot lower an indirect call!");
168 switch (Callee->getIntrinsicID()) {
169 case Intrinsic::not_intrinsic:
170 std::cerr << "Cannot lower a call to a non-intrinsic function '"
171 << Callee->getName() << "'!\n";
174 std::cerr << "Error: Code generator does not support intrinsic function '"
175 << Callee->getName() << "'!\n";
178 // The setjmp/longjmp intrinsics should only exist in the code if it was
179 // never optimized (ie, right out of the CFE), or if it has been hacked on
180 // by the lowerinvoke pass. In both cases, the right thing to do is to
181 // convert the call to an explicit setjmp or longjmp call.
182 case Intrinsic::setjmp: {
183 static Function *SetjmpFCache = 0;
184 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
185 Type::IntTy, SetjmpFCache);
186 if (CI->getType() != Type::VoidTy)
187 CI->replaceAllUsesWith(V);
190 case Intrinsic::sigsetjmp:
191 if (CI->getType() != Type::VoidTy)
192 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
195 case Intrinsic::longjmp: {
196 static Function *LongjmpFCache = 0;
197 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
198 Type::VoidTy, LongjmpFCache);
202 case Intrinsic::siglongjmp: {
203 // Insert the call to abort
204 static Function *AbortFCache = 0;
205 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy,
209 case Intrinsic::ctpop:
210 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
213 case Intrinsic::ctlz: {
214 Value *Src = CI->getOperand(1);
216 switch (CI->getOperand(0)->getType()->getTypeID())
219 case Type::ULongTyID:
220 SA = ConstantUInt::get(Type::UByteTy, 32);
221 Src = BinaryOperator::createOr(Src, new ShiftInst(Instruction::Shr, Src,
222 SA, "", CI), "", CI);
225 SA = ConstantUInt::get(Type::UByteTy, 16);
226 Src = BinaryOperator::createOr(Src, new ShiftInst(Instruction::Shr,
229 case Type::ShortTyID:
230 case Type::UShortTyID:
231 SA = ConstantUInt::get(Type::UByteTy, 8);
232 Src = BinaryOperator::createOr(Src, new ShiftInst(Instruction::Shr,
236 SA = ConstantUInt::get(Type::UByteTy, 1);
237 Src = BinaryOperator::createOr(Src, new ShiftInst(Instruction::Shr, Src,
238 SA, "", CI), "", CI);
239 SA = ConstantUInt::get(Type::UByteTy, 2);
240 Src = BinaryOperator::createOr(Src, new ShiftInst(Instruction::Shr, Src,
241 SA, "", CI), "", CI);
242 SA = ConstantUInt::get(Type::UByteTy, 4);
243 Src = BinaryOperator::createOr(Src, new ShiftInst(Instruction::Shr, Src,
244 SA, "", CI), "", CI);
246 Src = BinaryOperator::createNot(Src, "", CI);
249 Src = LowerCTPOP(Src, CI);
250 CI->replaceAllUsesWith(Src);
253 case Intrinsic::cttz: {
254 // cttz(x) -> ctpop(~X & (X-1))
255 Value *Src = CI->getOperand(1);
256 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
257 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
258 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
259 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
260 CI->replaceAllUsesWith(Src);
264 case Intrinsic::returnaddress:
265 case Intrinsic::frameaddress:
266 std::cerr << "WARNING: this target does not support the llvm."
267 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
268 "return" : "frame") << "address intrinsic.\n";
269 CI->replaceAllUsesWith(ConstantPointerNull::get(
270 cast<PointerType>(CI->getType())));
273 case Intrinsic::prefetch:
274 break; // Simply strip out prefetches on unsupported architectures
276 case Intrinsic::pcmarker:
277 break; // Simply strip out pcmarker on unsupported architectures
279 case Intrinsic::dbg_stoppoint:
280 case Intrinsic::dbg_region_start:
281 case Intrinsic::dbg_region_end:
282 case Intrinsic::dbg_declare:
283 case Intrinsic::dbg_func_start:
284 if (CI->getType() != Type::VoidTy)
285 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
286 break; // Simply strip out debugging intrinsics
288 case Intrinsic::memcpy: {
289 // The memcpy intrinsic take an extra alignment argument that the memcpy
290 // libc function does not.
291 static Function *MemcpyFCache = 0;
292 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
293 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
296 case Intrinsic::memmove: {
297 // The memmove intrinsic take an extra alignment argument that the memmove
298 // libc function does not.
299 static Function *MemmoveFCache = 0;
300 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
301 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
304 case Intrinsic::memset: {
305 // The memset intrinsic take an extra alignment argument that the memset
306 // libc function does not.
307 static Function *MemsetFCache = 0;
308 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
309 (*(CI->op_begin()+1))->getType(), MemsetFCache);
312 case Intrinsic::isunordered: {
313 Value *L = CI->getOperand(1);
314 Value *R = CI->getOperand(2);
316 Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
317 Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
318 CI->replaceAllUsesWith(
319 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
323 case Intrinsic::sqrt: {
324 static Function *sqrtFCache = 0;
325 static Function *sqrtfFCache = 0;
326 if(CI->getType() == Type::FloatTy)
327 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
328 Type::FloatTy, sqrtfFCache);
330 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
331 Type::DoubleTy, sqrtFCache);
336 assert(CI->use_empty() &&
337 "Lowering should have eliminated any uses of the intrinsic call!");
338 CI->eraseFromParent();