IntrinsicLowering.cpp revision 51b8d54922350b7e1c2cd5a5183ef2c5f5d1b1d5
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 default intrinsic lowering implementation.
11//
12//===----------------------------------------------------------------------===//
13
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"
20#include <iostream>
21
22using namespace llvm;
23
24template <class ArgIt>
25static Function *EnsureFunctionExists(Module &M, const char *Name,
26                                      ArgIt ArgBegin, ArgIt ArgEnd,
27                                      const Type *RetTy) {
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));
34}
35
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.
40template <class ArgIt>
41static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
42                                 ArgIt ArgBegin, ArgIt ArgEnd,
43                                 const Type *RetTy, Function *&FCache) {
44  if (!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);
49    if (!FCache) {
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));
56    }
57   }
58
59  const FunctionType *FT = FCache->getFunctionType();
60  std::vector<Value*> Operands;
61  unsigned ArgNo = 0;
62  for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams();
63       ++I, ++ArgNo) {
64    Value *Arg = *I;
65    if (Arg->getType() != FT->getParamType(ArgNo))
66      Arg = new CastInst(Arg, FT->getParamType(ArgNo), Arg->getName(), CI);
67    Operands.push_back(Arg);
68  }
69  // Pass nulls into any additional arguments...
70  for (; ArgNo != FT->getNumParams(); ++ArgNo)
71    Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
72
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()) {
77    Value *V = NewCI;
78    if (CI->getType() != NewCI->getType())
79      V = new CastInst(NewCI, CI->getType(), Name, CI);
80    CI->replaceAllUsesWith(V);
81  }
82  return NewCI;
83}
84
85void 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()) {
89      default: break;
90      case Intrinsic::setjmp:
91        EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
92                             Type::IntTy);
93        break;
94      case Intrinsic::longjmp:
95        EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
96                             Type::VoidTy);
97        break;
98      case Intrinsic::siglongjmp:
99        EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
100                             Type::VoidTy);
101        break;
102      case Intrinsic::memcpy:
103        EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
104                             I->arg_begin()->getType());
105        break;
106      case Intrinsic::memmove:
107        EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
108                             I->arg_begin()->getType());
109        break;
110      case Intrinsic::memset:
111        M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
112                              PointerType::get(Type::SByteTy),
113                              Type::IntTy, (--(--I->arg_end()))->getType(),
114                              (Type *)0);
115        break;
116      case Intrinsic::isunordered:
117        EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
118                             Type::BoolTy);
119        break;
120      case Intrinsic::sqrt:
121        if(I->arg_begin()->getType() == Type::FloatTy)
122          EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
123                               Type::FloatTy);
124        else
125          EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
126                               Type::DoubleTy);
127        break;
128      }
129}
130
131/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
132/// instruction.
133static Value *LowerCTPOP(Value *V, Instruction *IP) {
134  assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
135
136  static const uint64_t MaskValues[6] = {
137    0x5555555555555555ULL, 0x3333333333333333ULL,
138    0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
139    0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
140  };
141
142  const Type *DestTy = V->getType();
143
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);
147
148  unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
149  for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
150    Value *MaskCst =
151      ConstantExpr::getCast(ConstantUInt::get(Type::ULongTy,
152                                              MaskValues[ct]), V->getType());
153    Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
154    Value *VShift = new ShiftInst(Instruction::Shr, V,
155                      ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
156    Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
157    V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
158  }
159
160  if (V->getType() != DestTy)
161    V = new CastInst(V, DestTy, V->getName(), IP);
162  return V;
163}
164
165/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
166/// instruction.
167static Value *LowerCTLZ(Value *V, Instruction *IP) {
168  const Type *DestTy = V->getType();
169
170  // Force to unsigned so that the shift rights are logical.
171  if (DestTy->isSigned())
172    V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
173
174  unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
175  for (unsigned i = 1; i != BitSize; i <<= 1) {
176    Value *ShVal = ConstantInt::get(Type::UByteTy, i);
177    ShVal = new ShiftInst(Instruction::Shr, V, ShVal, "ctlz.sh", IP);
178    V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
179  }
180
181  if (V->getType() != DestTy)
182    V = new CastInst(V, DestTy, V->getName(), IP);
183
184  V = BinaryOperator::createNot(V, "", IP);
185  return LowerCTPOP(V, IP);
186}
187
188void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
189  Function *Callee = CI->getCalledFunction();
190  assert(Callee && "Cannot lower an indirect call!");
191
192  switch (Callee->getIntrinsicID()) {
193  case Intrinsic::not_intrinsic:
194    std::cerr << "Cannot lower a call to a non-intrinsic function '"
195              << Callee->getName() << "'!\n";
196    abort();
197  default:
198    std::cerr << "Error: Code generator does not support intrinsic function '"
199              << Callee->getName() << "'!\n";
200    abort();
201
202    // The setjmp/longjmp intrinsics should only exist in the code if it was
203    // never optimized (ie, right out of the CFE), or if it has been hacked on
204    // by the lowerinvoke pass.  In both cases, the right thing to do is to
205    // convert the call to an explicit setjmp or longjmp call.
206  case Intrinsic::setjmp: {
207    static Function *SetjmpFCache = 0;
208    Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
209                               Type::IntTy, SetjmpFCache);
210    if (CI->getType() != Type::VoidTy)
211      CI->replaceAllUsesWith(V);
212    break;
213  }
214  case Intrinsic::sigsetjmp:
215     if (CI->getType() != Type::VoidTy)
216       CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
217     break;
218
219  case Intrinsic::longjmp: {
220    static Function *LongjmpFCache = 0;
221    ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
222                    Type::VoidTy, LongjmpFCache);
223    break;
224  }
225
226  case Intrinsic::siglongjmp: {
227    // Insert the call to abort
228    static Function *AbortFCache = 0;
229    ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy,
230                    AbortFCache);
231    break;
232  }
233  case Intrinsic::ctpop:
234    CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
235    break;
236
237  case Intrinsic::ctlz:
238    CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
239    break;
240  case Intrinsic::cttz: {
241    // cttz(x) -> ctpop(~X & (X-1))
242    Value *Src = CI->getOperand(1);
243    Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
244    Value *SrcM1  = ConstantInt::get(Src->getType(), 1);
245    SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
246    Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
247    CI->replaceAllUsesWith(Src);
248    break;
249  }
250
251  case Intrinsic::returnaddress:
252  case Intrinsic::frameaddress:
253    std::cerr << "WARNING: this target does not support the llvm."
254              << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
255                  "return" : "frame") << "address intrinsic.\n";
256    CI->replaceAllUsesWith(ConstantPointerNull::get(
257                                            cast<PointerType>(CI->getType())));
258    break;
259
260  case Intrinsic::prefetch:
261    break;    // Simply strip out prefetches on unsupported architectures
262
263  case Intrinsic::pcmarker:
264    break;    // Simply strip out pcmarker on unsupported architectures
265  case Intrinsic::readcyclecounter: {
266    std::cerr << "WARNING: this target does not support the llvm.readcyclecounter"
267              << " intrinsic.  It is being lowered to a constant 0\n";
268    CI->replaceAllUsesWith(ConstantUInt::get(Type::ULongTy, 0));
269    break;
270  }
271
272  case Intrinsic::dbg_stoppoint:
273  case Intrinsic::dbg_region_start:
274  case Intrinsic::dbg_region_end:
275  case Intrinsic::dbg_declare:
276  case Intrinsic::dbg_func_start:
277    if (CI->getType() != Type::VoidTy)
278      CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
279    break;    // Simply strip out debugging intrinsics
280
281  case Intrinsic::memcpy: {
282    // The memcpy intrinsic take an extra alignment argument that the memcpy
283    // libc function does not.
284    static Function *MemcpyFCache = 0;
285    ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
286                    (*(CI->op_begin()+1))->getType(), MemcpyFCache);
287    break;
288  }
289  case Intrinsic::memmove: {
290    // The memmove intrinsic take an extra alignment argument that the memmove
291    // libc function does not.
292    static Function *MemmoveFCache = 0;
293    ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
294                    (*(CI->op_begin()+1))->getType(), MemmoveFCache);
295    break;
296  }
297  case Intrinsic::memset: {
298    // The memset intrinsic take an extra alignment argument that the memset
299    // libc function does not.
300    static Function *MemsetFCache = 0;
301    ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
302                    (*(CI->op_begin()+1))->getType(), MemsetFCache);
303    break;
304  }
305  case Intrinsic::isunordered: {
306    Value *L = CI->getOperand(1);
307    Value *R = CI->getOperand(2);
308
309    Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
310    Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
311    CI->replaceAllUsesWith(
312      BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
313                             "isunordered", CI));
314    break;
315  }
316  case Intrinsic::sqrt: {
317    static Function *sqrtFCache = 0;
318    static Function *sqrtfFCache = 0;
319    if(CI->getType() == Type::FloatTy)
320      ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
321                      Type::FloatTy, sqrtfFCache);
322    else
323      ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
324                      Type::DoubleTy, sqrtFCache);
325    break;
326  }
327  }
328
329  assert(CI->use_empty() &&
330         "Lowering should have eliminated any uses of the intrinsic call!");
331  CI->eraseFromParent();
332}
333