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"
21 #include "llvm/Target/TargetData.h"
24 template <class ArgIt>
25 static void EnsureFunctionExists(Module &M, const char *Name,
26 ArgIt ArgBegin, ArgIt ArgEnd,
28 // Insert a correctly-typed definition now.
29 std::vector<const Type *> ParamTys;
30 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
31 ParamTys.push_back(I->getType());
32 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 Type *RetTy, Constant *&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 // Get or insert the definition now.
48 std::vector<const Type *> ParamTys;
49 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
50 ParamTys.push_back((*I)->getType());
51 FCache = M->getOrInsertFunction(NewFn,
52 FunctionType::get(RetTy, ParamTys, false));
55 std::vector<Value*> Operands(ArgBegin, ArgEnd);
56 CallInst *NewCI = new CallInst(FCache, Operands, CI->getName(), CI);
58 CI->replaceAllUsesWith(NewCI);
62 void IntrinsicLowering::AddPrototypes(Module &M) {
63 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
64 if (I->isDeclaration() && !I->use_empty())
65 switch (I->getIntrinsicID()) {
67 case Intrinsic::setjmp:
68 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
71 case Intrinsic::longjmp:
72 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
75 case Intrinsic::siglongjmp:
76 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
79 case Intrinsic::memcpy_i32:
80 case Intrinsic::memcpy_i64:
81 M.getOrInsertFunction("memcpy", PointerType::get(Type::Int8Ty),
82 PointerType::get(Type::Int8Ty),
83 PointerType::get(Type::Int8Ty),
84 TD.getIntPtrType(), (Type *)0);
86 case Intrinsic::memmove_i32:
87 case Intrinsic::memmove_i64:
88 M.getOrInsertFunction("memmove", PointerType::get(Type::Int8Ty),
89 PointerType::get(Type::Int8Ty),
90 PointerType::get(Type::Int8Ty),
91 TD.getIntPtrType(), (Type *)0);
93 case Intrinsic::memset_i32:
94 case Intrinsic::memset_i64:
95 M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty),
96 PointerType::get(Type::Int8Ty), Type::Int32Ty,
97 TD.getIntPtrType(), (Type *)0);
99 case Intrinsic::sqrt_f32:
100 case Intrinsic::sqrt_f64:
101 if(I->arg_begin()->getType() == Type::FloatTy)
102 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
105 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
111 /// LowerBSWAP - Emit the code to lower bswap of V before the specified
113 static Value *LowerBSWAP(Value *V, Instruction *IP) {
114 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
116 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
119 default: assert(0 && "Unhandled type size of value to byteswap!");
121 Value *Tmp1 = new ShiftInst(Instruction::Shl, V,
122 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
123 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
124 ConstantInt::get(Type::Int8Ty,8),"bswap.1",IP);
125 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
129 Value *Tmp4 = new ShiftInst(Instruction::Shl, V,
130 ConstantInt::get(Type::Int8Ty,24),"bswap.4", IP);
131 Value *Tmp3 = new ShiftInst(Instruction::Shl, V,
132 ConstantInt::get(Type::Int8Ty,8),"bswap.3",IP);
133 Value *Tmp2 = new ShiftInst(Instruction::LShr, V,
134 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP);
135 Value *Tmp1 = new ShiftInst(Instruction::LShr, V,
136 ConstantInt::get(Type::Int8Ty,24),"bswap.1", IP);
137 Tmp3 = BinaryOperator::createAnd(Tmp3,
138 ConstantInt::get(Type::Int32Ty, 0xFF0000),
140 Tmp2 = BinaryOperator::createAnd(Tmp2,
141 ConstantInt::get(Type::Int32Ty, 0xFF00),
143 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
144 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
145 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
149 Value *Tmp8 = new ShiftInst(Instruction::Shl, V,
150 ConstantInt::get(Type::Int8Ty,56),"bswap.8", IP);
151 Value *Tmp7 = new ShiftInst(Instruction::Shl, V,
152 ConstantInt::get(Type::Int8Ty,40),"bswap.7", IP);
153 Value *Tmp6 = new ShiftInst(Instruction::Shl, V,
154 ConstantInt::get(Type::Int8Ty,24),"bswap.6", IP);
155 Value *Tmp5 = new ShiftInst(Instruction::Shl, V,
156 ConstantInt::get(Type::Int8Ty,8),"bswap.5", IP);
157 Value* Tmp4 = new ShiftInst(Instruction::LShr, V,
158 ConstantInt::get(Type::Int8Ty,8),"bswap.4", IP);
159 Value* Tmp3 = new ShiftInst(Instruction::LShr, V,
160 ConstantInt::get(Type::Int8Ty,24),"bswap.3", IP);
161 Value* Tmp2 = new ShiftInst(Instruction::LShr, V,
162 ConstantInt::get(Type::Int8Ty,40),"bswap.2", IP);
163 Value* Tmp1 = new ShiftInst(Instruction::LShr, V,
164 ConstantInt::get(Type::Int8Ty,56),"bswap.1", IP);
165 Tmp7 = BinaryOperator::createAnd(Tmp7,
166 ConstantInt::get(Type::Int64Ty,
167 0xFF000000000000ULL),
169 Tmp6 = BinaryOperator::createAnd(Tmp6,
170 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
172 Tmp5 = BinaryOperator::createAnd(Tmp5,
173 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
175 Tmp4 = BinaryOperator::createAnd(Tmp4,
176 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
178 Tmp3 = BinaryOperator::createAnd(Tmp3,
179 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
181 Tmp2 = BinaryOperator::createAnd(Tmp2,
182 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
184 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
185 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
186 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
187 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
188 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
189 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
190 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
197 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
199 static Value *LowerCTPOP(Value *V, Instruction *IP) {
200 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
202 static const uint64_t MaskValues[6] = {
203 0x5555555555555555ULL, 0x3333333333333333ULL,
204 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
205 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
208 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
210 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
211 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
212 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
213 Value *VShift = new ShiftInst(Instruction::LShr, V,
214 ConstantInt::get(Type::Int8Ty, i), "ctpop.sh", IP);
215 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
216 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
222 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
224 static Value *LowerCTLZ(Value *V, Instruction *IP) {
226 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
227 for (unsigned i = 1; i != BitSize; i <<= 1) {
228 Value *ShVal = ConstantInt::get(Type::Int8Ty, i);
229 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
230 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
233 V = BinaryOperator::createNot(V, "", IP);
234 return LowerCTPOP(V, IP);
239 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
240 Function *Callee = CI->getCalledFunction();
241 assert(Callee && "Cannot lower an indirect call!");
243 switch (Callee->getIntrinsicID()) {
244 case Intrinsic::not_intrinsic:
245 cerr << "Cannot lower a call to a non-intrinsic function '"
246 << Callee->getName() << "'!\n";
249 cerr << "Error: Code generator does not support intrinsic function '"
250 << Callee->getName() << "'!\n";
253 // The setjmp/longjmp intrinsics should only exist in the code if it was
254 // never optimized (ie, right out of the CFE), or if it has been hacked on
255 // by the lowerinvoke pass. In both cases, the right thing to do is to
256 // convert the call to an explicit setjmp or longjmp call.
257 case Intrinsic::setjmp: {
258 static Constant *SetjmpFCache = 0;
259 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
260 Type::Int32Ty, SetjmpFCache);
261 if (CI->getType() != Type::VoidTy)
262 CI->replaceAllUsesWith(V);
265 case Intrinsic::sigsetjmp:
266 if (CI->getType() != Type::VoidTy)
267 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
270 case Intrinsic::longjmp: {
271 static Constant *LongjmpFCache = 0;
272 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
273 Type::VoidTy, LongjmpFCache);
277 case Intrinsic::siglongjmp: {
278 // Insert the call to abort
279 static Constant *AbortFCache = 0;
280 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
281 Type::VoidTy, AbortFCache);
284 case Intrinsic::ctpop_i8:
285 case Intrinsic::ctpop_i16:
286 case Intrinsic::ctpop_i32:
287 case Intrinsic::ctpop_i64:
288 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
291 case Intrinsic::bswap_i16:
292 case Intrinsic::bswap_i32:
293 case Intrinsic::bswap_i64:
294 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
297 case Intrinsic::ctlz_i8:
298 case Intrinsic::ctlz_i16:
299 case Intrinsic::ctlz_i32:
300 case Intrinsic::ctlz_i64:
301 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
304 case Intrinsic::cttz_i8:
305 case Intrinsic::cttz_i16:
306 case Intrinsic::cttz_i32:
307 case Intrinsic::cttz_i64: {
308 // cttz(x) -> ctpop(~X & (X-1))
309 Value *Src = CI->getOperand(1);
310 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
311 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
312 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
313 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
314 CI->replaceAllUsesWith(Src);
318 case Intrinsic::stacksave:
319 case Intrinsic::stackrestore: {
320 static bool Warned = false;
322 cerr << "WARNING: this target does not support the llvm.stack"
323 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
324 "save" : "restore") << " intrinsic.\n";
326 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
327 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
331 case Intrinsic::returnaddress:
332 case Intrinsic::frameaddress:
333 cerr << "WARNING: this target does not support the llvm."
334 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
335 "return" : "frame") << "address intrinsic.\n";
336 CI->replaceAllUsesWith(ConstantPointerNull::get(
337 cast<PointerType>(CI->getType())));
340 case Intrinsic::prefetch:
341 break; // Simply strip out prefetches on unsupported architectures
343 case Intrinsic::pcmarker:
344 break; // Simply strip out pcmarker on unsupported architectures
345 case Intrinsic::readcyclecounter: {
346 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
347 << "ter intrinsic. It is being lowered to a constant 0\n";
348 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
352 case Intrinsic::dbg_stoppoint:
353 case Intrinsic::dbg_region_start:
354 case Intrinsic::dbg_region_end:
355 case Intrinsic::dbg_func_start:
356 case Intrinsic::dbg_declare:
357 break; // Simply strip out debugging intrinsics
359 case Intrinsic::memcpy_i32: {
360 static Constant *MemcpyFCache = 0;
361 Value * Size = cast<Value>(CI->op_end()-1);
362 if (Size->getType() != TD.getIntPtrType())
363 Size->replaceAllUsesWith(new ZExtInst(Size, TD.getIntPtrType()));
364 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
365 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
368 case Intrinsic::memcpy_i64: {
369 static Constant *MemcpyFCache = 0;
370 Value * Size = cast<Value>(CI->op_end()-1);
371 if (Size->getType() != TD.getIntPtrType())
372 Size->replaceAllUsesWith(new TruncInst(Size, TD.getIntPtrType()));
373 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
374 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
377 case Intrinsic::memmove_i32: {
378 static Constant *MemmoveFCache = 0;
379 Value * Size = cast<Value>(CI->op_end()-1);
380 if (Size->getType() != TD.getIntPtrType())
381 Size->replaceAllUsesWith(new ZExtInst(Size, TD.getIntPtrType()));
382 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
383 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
386 case Intrinsic::memmove_i64: {
387 static Constant *MemmoveFCache = 0;
388 Value * Size = cast<Value>(CI->op_end()-1);
389 if (Size->getType() != TD.getIntPtrType())
390 Size->replaceAllUsesWith(new TruncInst(Size, TD.getIntPtrType()));
391 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
392 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
395 case Intrinsic::memset_i32: {
396 static Constant *MemsetFCache = 0;
397 Value * Size = cast<Value>(CI->op_end()-1);
398 if (Size->getType() != TD.getIntPtrType())
399 Size->replaceAllUsesWith(new ZExtInst(Size, TD.getIntPtrType()));
400 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
401 (*(CI->op_begin()+1))->getType(), MemsetFCache);
403 case Intrinsic::memset_i64: {
404 static Constant *MemsetFCache = 0;
405 Value * Size = cast<Value>(CI->op_end()-1);
406 if (Size->getType() != TD.getIntPtrType())
407 Size->replaceAllUsesWith(new TruncInst(Size, TD.getIntPtrType()));
408 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
409 (*(CI->op_begin()+1))->getType(), MemsetFCache);
412 case Intrinsic::sqrt_f32: {
413 static Constant *sqrtfFCache = 0;
414 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
415 Type::FloatTy, sqrtfFCache);
418 case Intrinsic::sqrt_f64: {
419 static Constant *sqrtFCache = 0;
420 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
421 Type::DoubleTy, sqrtFCache);
426 assert(CI->use_empty() &&
427 "Lowering should have eliminated any uses of the intrinsic call!");
428 CI->eraseFromParent();