IntrinsicLowering.cpp revision 832254e1c2387c0cbeb0a820b8315fbe85cb003a
1//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
2//
3//                     The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the IntrinsicLowering class.
11//
12//===----------------------------------------------------------------------===//
13
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"
22using namespace llvm;
23
24template <class ArgIt>
25static void EnsureFunctionExists(Module &M, const char *Name,
26                                 ArgIt ArgBegin, ArgIt ArgEnd,
27                                 const Type *RetTy) {
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));
33}
34
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.
39template <class ArgIt>
40static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
41                                 ArgIt ArgBegin, ArgIt ArgEnd,
42                                 const Type *RetTy, Constant *&FCache) {
43  if (!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));
53  }
54
55  std::vector<Value*> Operands(ArgBegin, ArgEnd);
56  CallInst *NewCI = new CallInst(FCache, Operands, CI->getName(), CI);
57  if (!CI->use_empty())
58    CI->replaceAllUsesWith(NewCI);
59  return NewCI;
60}
61
62void 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()) {
66      default: break;
67      case Intrinsic::setjmp:
68        EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
69                             Type::Int32Ty);
70        break;
71      case Intrinsic::longjmp:
72        EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
73                             Type::VoidTy);
74        break;
75      case Intrinsic::siglongjmp:
76        EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
77                             Type::VoidTy);
78        break;
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);
85        break;
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);
92        break;
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);
98        break;
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(),
103                               Type::FloatTy);
104        else
105          EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
106                               Type::DoubleTy);
107        break;
108      }
109}
110
111/// LowerBSWAP - Emit the code to lower bswap of V before the specified
112/// instruction IP.
113static Value *LowerBSWAP(Value *V, Instruction *IP) {
114  assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
115
116  unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
117
118  switch(BitSize) {
119  default: assert(0 && "Unhandled type size of value to byteswap!");
120  case 16: {
121    Value *Tmp1 = BinaryOperator::create(Instruction::Shl, V,
122                                ConstantInt::get(V->getType(),8),"bswap.2",IP);
123    Value *Tmp2 = BinaryOperator::create(Instruction::LShr, V,
124                                ConstantInt::get(V->getType(),8),"bswap.1",IP);
125    V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
126    break;
127  }
128  case 32: {
129    Value *Tmp4 = BinaryOperator::create(Instruction::Shl, V,
130                              ConstantInt::get(V->getType(),24),"bswap.4", IP);
131    Value *Tmp3 = BinaryOperator::create(Instruction::Shl, V,
132                              ConstantInt::get(V->getType(),8),"bswap.3",IP);
133    Value *Tmp2 = BinaryOperator::create(Instruction::LShr, V,
134                              ConstantInt::get(V->getType(),8),"bswap.2",IP);
135    Value *Tmp1 = BinaryOperator::create(Instruction::LShr, V,
136                              ConstantInt::get(V->getType(),24),"bswap.1", IP);
137    Tmp3 = BinaryOperator::createAnd(Tmp3,
138                                     ConstantInt::get(Type::Int32Ty, 0xFF0000),
139                                     "bswap.and3", IP);
140    Tmp2 = BinaryOperator::createAnd(Tmp2,
141                                     ConstantInt::get(Type::Int32Ty, 0xFF00),
142                                     "bswap.and2", IP);
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);
146    break;
147  }
148  case 64: {
149    Value *Tmp8 = BinaryOperator::create(Instruction::Shl, V,
150                              ConstantInt::get(V->getType(),56),"bswap.8", IP);
151    Value *Tmp7 = BinaryOperator::create(Instruction::Shl, V,
152                              ConstantInt::get(V->getType(),40),"bswap.7", IP);
153    Value *Tmp6 = BinaryOperator::create(Instruction::Shl, V,
154                              ConstantInt::get(V->getType(),24),"bswap.6", IP);
155    Value *Tmp5 = BinaryOperator::create(Instruction::Shl, V,
156                              ConstantInt::get(V->getType(),8),"bswap.5", IP);
157    Value* Tmp4 = BinaryOperator::create(Instruction::LShr, V,
158                              ConstantInt::get(V->getType(),8),"bswap.4", IP);
159    Value* Tmp3 = BinaryOperator::create(Instruction::LShr, V,
160                              ConstantInt::get(V->getType(),24),"bswap.3", IP);
161    Value* Tmp2 = BinaryOperator::create(Instruction::LShr, V,
162                              ConstantInt::get(V->getType(),40),"bswap.2", IP);
163    Value* Tmp1 = BinaryOperator::create(Instruction::LShr, V,
164                              ConstantInt::get(V->getType(),56),"bswap.1", IP);
165    Tmp7 = BinaryOperator::createAnd(Tmp7,
166                             ConstantInt::get(Type::Int64Ty,
167                               0xFF000000000000ULL),
168                             "bswap.and7", IP);
169    Tmp6 = BinaryOperator::createAnd(Tmp6,
170                             ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
171                             "bswap.and6", IP);
172    Tmp5 = BinaryOperator::createAnd(Tmp5,
173                             ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
174                             "bswap.and5", IP);
175    Tmp4 = BinaryOperator::createAnd(Tmp4,
176                             ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
177                             "bswap.and4", IP);
178    Tmp3 = BinaryOperator::createAnd(Tmp3,
179                             ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
180                             "bswap.and3", IP);
181    Tmp2 = BinaryOperator::createAnd(Tmp2,
182                             ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
183                             "bswap.and2", IP);
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);
191    break;
192  }
193  }
194  return V;
195}
196
197/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
198/// instruction IP.
199static Value *LowerCTPOP(Value *V, Instruction *IP) {
200  assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
201
202  static const uint64_t MaskValues[6] = {
203    0x5555555555555555ULL, 0x3333333333333333ULL,
204    0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
205    0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
206  };
207
208  unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
209
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 = BinaryOperator::create(Instruction::LShr, V,
214                      ConstantInt::get(V->getType(), i), "ctpop.sh", IP);
215    Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
216    V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
217  }
218
219  return V;
220}
221
222/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
223/// instruction IP.
224static Value *LowerCTLZ(Value *V, Instruction *IP) {
225
226  unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
227  for (unsigned i = 1; i != BitSize; i <<= 1) {
228    Value *ShVal = ConstantInt::get(V->getType(), i);
229    ShVal = BinaryOperator::create(Instruction::LShr, V, ShVal, "ctlz.sh", IP);
230    V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
231  }
232
233  V = BinaryOperator::createNot(V, "", IP);
234  return LowerCTPOP(V, IP);
235}
236
237
238
239void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
240  Function *Callee = CI->getCalledFunction();
241  assert(Callee && "Cannot lower an indirect call!");
242
243  switch (Callee->getIntrinsicID()) {
244  case Intrinsic::not_intrinsic:
245    cerr << "Cannot lower a call to a non-intrinsic function '"
246         << Callee->getName() << "'!\n";
247    abort();
248  default:
249    cerr << "Error: Code generator does not support intrinsic function '"
250         << Callee->getName() << "'!\n";
251    abort();
252
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);
263    break;
264  }
265  case Intrinsic::sigsetjmp:
266     if (CI->getType() != Type::VoidTy)
267       CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
268     break;
269
270  case Intrinsic::longjmp: {
271    static Constant *LongjmpFCache = 0;
272    ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
273                    Type::VoidTy, LongjmpFCache);
274    break;
275  }
276
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);
282    break;
283  }
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));
289    break;
290
291  case Intrinsic::bswap_i16:
292  case Intrinsic::bswap_i32:
293  case Intrinsic::bswap_i64:
294    CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
295    break;
296
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));
302    break;
303
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);
315    break;
316  }
317
318  case Intrinsic::stacksave:
319  case Intrinsic::stackrestore: {
320    static bool Warned = false;
321    if (!Warned)
322      cerr << "WARNING: this target does not support the llvm.stack"
323           << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
324               "save" : "restore") << " intrinsic.\n";
325    Warned = true;
326    if (Callee->getIntrinsicID() == Intrinsic::stacksave)
327      CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
328    break;
329  }
330
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())));
338    break;
339
340  case Intrinsic::prefetch:
341    break;    // Simply strip out prefetches on unsupported architectures
342
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));
349    break;
350  }
351
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
358
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);
366    break;
367  }
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);
375    break;
376  }
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);
384    break;
385  }
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);
393    break;
394  }
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);
402  }
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);
410    break;
411  }
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);
416    break;
417  }
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);
422    break;
423  }
424  }
425
426  assert(CI->use_empty() &&
427         "Lowering should have eliminated any uses of the intrinsic call!");
428  CI->eraseFromParent();
429}
430