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 void EnsureFunctionExists(Module &M, const char *Name,
25 ArgIt ArgBegin, ArgIt ArgEnd,
27 // Insert a correctly-typed definition now.
28 std::vector<const Type *> ParamTys;
29 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
30 ParamTys.push_back(I->getType());
31 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
34 /// ReplaceCallWith - This function is used when we want to lower an intrinsic
35 /// call to a call of an external function. This handles hard cases such as
36 /// when there was already a prototype for the external function, and if that
37 /// prototype doesn't match the arguments we expect to pass in.
38 template <class ArgIt>
39 static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
40 ArgIt ArgBegin, ArgIt ArgEnd,
41 const Type *RetTy, Constant *&FCache) {
43 // If we haven't already looked up this function, check to see if the
44 // program already contains a function with this name.
45 Module *M = CI->getParent()->getParent()->getParent();
46 // Get or insert the definition now.
47 std::vector<const Type *> ParamTys;
48 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
49 ParamTys.push_back((*I)->getType());
50 FCache = M->getOrInsertFunction(NewFn,
51 FunctionType::get(RetTy, ParamTys, false));
54 std::vector<Value*> Operands(ArgBegin, ArgEnd);
55 CallInst *NewCI = new CallInst(FCache, Operands, CI->getName(), CI);
57 CI->replaceAllUsesWith(NewCI);
61 void IntrinsicLowering::AddPrototypes(Module &M) {
62 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
63 if (I->isExternal() && !I->use_empty())
64 switch (I->getIntrinsicID()) {
66 case Intrinsic::setjmp:
67 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
70 case Intrinsic::longjmp:
71 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
74 case Intrinsic::siglongjmp:
75 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
78 case Intrinsic::memcpy_i32:
79 case Intrinsic::memcpy_i64:
80 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
81 I->arg_begin()->getType());
83 case Intrinsic::memmove_i32:
84 case Intrinsic::memmove_i64:
85 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
86 I->arg_begin()->getType());
88 case Intrinsic::memset_i32:
89 case Intrinsic::memset_i64:
90 M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty),
91 PointerType::get(Type::Int8Ty),
92 Type::Int32Ty, (--(--I->arg_end()))->getType(),
95 case Intrinsic::isunordered_f32:
96 case Intrinsic::isunordered_f64:
97 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
100 case Intrinsic::sqrt_f32:
101 case Intrinsic::sqrt_f64:
102 if(I->arg_begin()->getType() == Type::FloatTy)
103 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
106 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
112 /// LowerBSWAP - Emit the code to lower bswap of V before the specified
114 static Value *LowerBSWAP(Value *V, Instruction *IP) {
115 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
117 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
120 default: assert(0 && "Unhandled type size of value to byteswap!");
122 Value *Tmp1 = new ShiftInst(Instruction::Shl, V,
123 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
124 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
125 ConstantInt::get(Type::Int8Ty,8),"bswap.1",IP);
126 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
130 Value *Tmp4 = new ShiftInst(Instruction::Shl, V,
131 ConstantInt::get(Type::Int8Ty,24),"bswap.4", IP);
132 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
133 ConstantInt::get(Type::Int8Ty,8),"bswap.3",IP);
134 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
135 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
136 Value *Tmp1 = new ShiftInst(Instruction::LShr, V,
137 ConstantInt::get(Type::Int8Ty,24),"bswap.1", IP);
138 Tmp3 = BinaryOperator::createAnd(Tmp3,
139 ConstantInt::get(Type::Int32Ty, 0xFF0000),
141 Tmp2 = BinaryOperator::createAnd(Tmp2,
142 ConstantInt::get(Type::Int32Ty, 0xFF00),
144 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
145 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
146 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
150 Value *Tmp8 = new ShiftInst(Instruction::Shl, V,
151 ConstantInt::get(Type::Int8Ty,56),"bswap.8", IP);
152 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
153 ConstantInt::get(Type::Int8Ty,40),"bswap.7", IP);
154 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
155 ConstantInt::get(Type::Int8Ty,24),"bswap.6", IP);
156 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
157 ConstantInt::get(Type::Int8Ty,8),"bswap.5", IP);
158 Value* Tmp4 = new ShiftInst(Instruction::LShr, V,
159 ConstantInt::get(Type::Int8Ty,8),"bswap.4", IP);
160 Value* Tmp3 = new ShiftInst(Instruction::LShr, V,
161 ConstantInt::get(Type::Int8Ty,24),"bswap.3", IP);
162 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
163 ConstantInt::get(Type::Int8Ty,40),"bswap.2", IP);
164 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
165 ConstantInt::get(Type::Int8Ty,56),"bswap.1", IP);
166 Tmp7 = BinaryOperator::createAnd(Tmp7,
167 ConstantInt::get(Type::Int64Ty,
168 0xFF000000000000ULL),
170 Tmp6 = BinaryOperator::createAnd(Tmp6,
171 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
173 Tmp5 = BinaryOperator::createAnd(Tmp5,
174 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
176 Tmp4 = BinaryOperator::createAnd(Tmp4,
177 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
179 Tmp3 = BinaryOperator::createAnd(Tmp3,
180 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
182 Tmp2 = BinaryOperator::createAnd(Tmp2,
183 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
185 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
186 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
187 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
188 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
189 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
190 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
191 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
198 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
200 static Value *LowerCTPOP(Value *V, Instruction *IP) {
201 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
203 static const uint64_t MaskValues[6] = {
204 0x5555555555555555ULL, 0x3333333333333333ULL,
205 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
206 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
209 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
211 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
212 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
213 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
214 Value *VShift = new ShiftInst(Instruction::LShr, V,
215 ConstantInt::get(Type::Int8Ty, i), "ctpop.sh", IP);
216 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
217 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
223 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
225 static Value *LowerCTLZ(Value *V, Instruction *IP) {
227 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
228 for (unsigned i = 1; i != BitSize; i <<= 1) {
229 Value *ShVal = ConstantInt::get(Type::Int8Ty, i);
230 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
231 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
234 V = BinaryOperator::createNot(V, "", IP);
235 return LowerCTPOP(V, IP);
240 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
241 Function *Callee = CI->getCalledFunction();
242 assert(Callee && "Cannot lower an indirect call!");
244 switch (Callee->getIntrinsicID()) {
245 case Intrinsic::not_intrinsic:
246 cerr << "Cannot lower a call to a non-intrinsic function '"
247 << Callee->getName() << "'!\n";
250 cerr << "Error: Code generator does not support intrinsic function '"
251 << Callee->getName() << "'!\n";
254 // The setjmp/longjmp intrinsics should only exist in the code if it was
255 // never optimized (ie, right out of the CFE), or if it has been hacked on
256 // by the lowerinvoke pass. In both cases, the right thing to do is to
257 // convert the call to an explicit setjmp or longjmp call.
258 case Intrinsic::setjmp: {
259 static Constant *SetjmpFCache = 0;
260 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
261 Type::Int32Ty, SetjmpFCache);
262 if (CI->getType() != Type::VoidTy)
263 CI->replaceAllUsesWith(V);
266 case Intrinsic::sigsetjmp:
267 if (CI->getType() != Type::VoidTy)
268 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
271 case Intrinsic::longjmp: {
272 static Constant *LongjmpFCache = 0;
273 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
274 Type::VoidTy, LongjmpFCache);
278 case Intrinsic::siglongjmp: {
279 // Insert the call to abort
280 static Constant *AbortFCache = 0;
281 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
282 Type::VoidTy, AbortFCache);
285 case Intrinsic::ctpop_i8:
286 case Intrinsic::ctpop_i16:
287 case Intrinsic::ctpop_i32:
288 case Intrinsic::ctpop_i64:
289 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
292 case Intrinsic::bswap_i16:
293 case Intrinsic::bswap_i32:
294 case Intrinsic::bswap_i64:
295 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
298 case Intrinsic::ctlz_i8:
299 case Intrinsic::ctlz_i16:
300 case Intrinsic::ctlz_i32:
301 case Intrinsic::ctlz_i64:
302 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
305 case Intrinsic::cttz_i8:
306 case Intrinsic::cttz_i16:
307 case Intrinsic::cttz_i32:
308 case Intrinsic::cttz_i64: {
309 // cttz(x) -> ctpop(~X & (X-1))
310 Value *Src = CI->getOperand(1);
311 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
312 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
313 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
314 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
315 CI->replaceAllUsesWith(Src);
319 case Intrinsic::stacksave:
320 case Intrinsic::stackrestore: {
321 static bool Warned = false;
323 cerr << "WARNING: this target does not support the llvm.stack"
324 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
325 "save" : "restore") << " intrinsic.\n";
327 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
328 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
332 case Intrinsic::returnaddress:
333 case Intrinsic::frameaddress:
334 cerr << "WARNING: this target does not support the llvm."
335 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
336 "return" : "frame") << "address intrinsic.\n";
337 CI->replaceAllUsesWith(ConstantPointerNull::get(
338 cast<PointerType>(CI->getType())));
341 case Intrinsic::prefetch:
342 break; // Simply strip out prefetches on unsupported architectures
344 case Intrinsic::pcmarker:
345 break; // Simply strip out pcmarker on unsupported architectures
346 case Intrinsic::readcyclecounter: {
347 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
348 << "ter intrinsic. It is being lowered to a constant 0\n";
349 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
353 case Intrinsic::dbg_stoppoint:
354 case Intrinsic::dbg_region_start:
355 case Intrinsic::dbg_region_end:
356 case Intrinsic::dbg_func_start:
357 case Intrinsic::dbg_declare:
358 break; // Simply strip out debugging intrinsics
360 case Intrinsic::memcpy_i32: {
361 static Constant *MemcpyFCache = 0;
362 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
363 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
366 case Intrinsic::memcpy_i64: {
367 static Constant *MemcpyFCache = 0;
368 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
369 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
372 case Intrinsic::memmove_i32: {
373 static Constant *MemmoveFCache = 0;
374 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
375 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
378 case Intrinsic::memmove_i64: {
379 static Constant *MemmoveFCache = 0;
380 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
381 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
384 case Intrinsic::memset_i32: {
385 static Constant *MemsetFCache = 0;
386 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
387 (*(CI->op_begin()+1))->getType(), MemsetFCache);
389 case Intrinsic::memset_i64: {
390 static Constant *MemsetFCache = 0;
391 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
392 (*(CI->op_begin()+1))->getType(), MemsetFCache);
395 case Intrinsic::isunordered_f32:
396 case Intrinsic::isunordered_f64: {
397 Value *L = CI->getOperand(1);
398 Value *R = CI->getOperand(2);
400 Value *LIsNan = new FCmpInst(FCmpInst::FCMP_ONE, L, L, "LIsNan", CI);
401 Value *RIsNan = new FCmpInst(FCmpInst::FCMP_ONE, R, R, "RIsNan", CI);
402 CI->replaceAllUsesWith(
403 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
407 case Intrinsic::sqrt_f32: {
408 static Constant *sqrtfFCache = 0;
409 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
410 Type::FloatTy, sqrtfFCache);
413 case Intrinsic::sqrt_f64: {
414 static Constant *sqrtFCache = 0;
415 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
416 Type::DoubleTy, sqrtFCache);
421 assert(CI->use_empty() &&
422 "Lowering should have eliminated any uses of the intrinsic call!");
423 CI->eraseFromParent();