IntrinsicLowering.cpp revision 86f3e0c24e8834e6ad5ac61f2459fb335549bc24
1010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
2010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//
3010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
4010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//
5010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// This file was developed by the LLVM research group and is distributed under
6010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// the University of Illinois Open Source License. See LICENSE.TXT for details.
7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//
8010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//===----------------------------------------------------------------------===//
9010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//
10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// This file implements the default intrinsic lowering implementation.
11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//
12010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//===----------------------------------------------------------------------===//
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
14010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/CodeGen/IntrinsicLowering.h"
15010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/Constants.h"
16010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/DerivedTypes.h"
17010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/Module.h"
18010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/Instructions.h"
19010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/Type.h"
20010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include <iostream>
21010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
22010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)using namespace llvm;
23010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
24010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)template <class ArgIt>
25010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)static Function *EnsureFunctionExists(Module &M, const char *Name,
26010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      ArgIt ArgBegin, ArgIt ArgEnd,
27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      const Type *RetTy) {
28010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (Function *F = M.getNamedFunction(Name)) return F;
29010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // It doesn't already exist in the program, insert a new definition now.
30010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  std::vector<const Type *> ParamTys;
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
32010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    ParamTys.push_back(I->getType());
33010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
35010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
36010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)/// ReplaceCallWith - This function is used when we want to lower an intrinsic
37010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)/// call to a call of an external function.  This handles hard cases such as
38010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)/// when there was already a prototype for the external function, and if that
39010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)/// prototype doesn't match the arguments we expect to pass in.
40010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)template <class ArgIt>
41010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
42010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                 ArgIt ArgBegin, ArgIt ArgEnd,
43010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                 const Type *RetTy, Function *&FCache) {
44010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!FCache) {
45010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // If we haven't already looked up this function, check to see if the
46010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // program already contains a function with this name.
47010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Module *M = CI->getParent()->getParent()->getParent();
48010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    FCache = M->getNamedFunction(NewFn);
49010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (!FCache) {
50010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      // It doesn't already exist in the program, insert a new definition now.
51010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      std::vector<const Type *> ParamTys;
52010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
53010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        ParamTys.push_back((*I)->getType());
54010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      FCache = M->getOrInsertFunction(NewFn,
55010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                     FunctionType::get(RetTy, ParamTys, false));
56010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
57010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)   }
58010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
59010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  const FunctionType *FT = FCache->getFunctionType();
60010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  std::vector<Value*> Operands;
61010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  unsigned ArgNo = 0;
62010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams();
63010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)       ++I, ++ArgNo) {
64010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Value *Arg = *I;
65010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (Arg->getType() != FT->getParamType(ArgNo))
66010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      Arg = new CastInst(Arg, FT->getParamType(ArgNo), Arg->getName(), CI);
67010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Operands.push_back(Arg);
68010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
69010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Pass nulls into any additional arguments...
70010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  for (; ArgNo != FT->getNumParams(); ++ArgNo)
71010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
72010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
73010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  std::string Name = CI->getName(); CI->setName("");
74010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (FT->getReturnType() == Type::VoidTy) Name.clear();
75010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  CallInst *NewCI = new CallInst(FCache, Operands, Name, CI);
76010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!CI->use_empty()) {
77010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Value *V = NewCI;
78010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (CI->getType() != NewCI->getType())
79010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      V = new CastInst(NewCI, CI->getType(), Name, CI);
80010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    CI->replaceAllUsesWith(V);
81010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
82010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return NewCI;
83010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
84010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
85010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void DefaultIntrinsicLowering::AddPrototypes(Module &M) {
86010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
87010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (I->isExternal() && !I->use_empty())
88010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      switch (I->getIntrinsicID()) {
89010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      default: break;
90010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      case Intrinsic::setjmp:
91010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
92010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                             Type::IntTy);
93010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        break;
94010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      case Intrinsic::longjmp:
95010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
96010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                             Type::VoidTy);
97010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        break;
98010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      case Intrinsic::siglongjmp:
99010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
100010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                             Type::VoidTy);
101010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        break;
102010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      case Intrinsic::memcpy:
103010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(),
104010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                             I->arg_begin()->getType());
105010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        break;
106010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      case Intrinsic::memmove:
107010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(),
108010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                             I->arg_begin()->getType());
109010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        break;
110010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      case Intrinsic::memset:
111010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy),
112010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                              PointerType::get(Type::SByteTy),
113010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                              Type::IntTy, (--(--I->arg_end()))->getType(), 0);
114010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        break;
115010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      case Intrinsic::isunordered:
116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(),
117010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                             Type::BoolTy);
118010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        break;
119010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      case Intrinsic::sqrt:
120010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        if(I->arg_begin()->getType() == Type::FloatTy)
121010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
122010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                               Type::FloatTy);
123010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        else
124010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
125010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                               Type::DoubleTy);
126010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        break;
127010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      }
128010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
129010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
130010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
131010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)/// instruction.
132010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)static Value *LowerCTPOP(Value *V, Instruction *IP) {
133010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
134010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
135010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
136010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  static const uint64_t MaskValues[6] = {
137010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    0x5555555555555555ULL, 0x3333333333333333ULL,
138010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
139010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
140010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  };
141010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
142010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  const Type *DestTy = V->getType();
143010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
144010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Force to unsigned so that the shift rights are logical.
145010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (DestTy->isSigned())
146010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP);
147010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
148010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
149010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Value *MaskCst =
150010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      ConstantExpr::getCast(ConstantUInt::get(Type::ULongTy,
151010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                              MaskValues[ct]), V->getType());
152010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
153010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Value *VShift = new ShiftInst(Instruction::Shr, V,
154010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                      ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP);
155010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
156010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
157010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
158010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
159010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (V->getType() != DestTy)
160010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    V = new CastInst(V, DestTy, V->getName(), IP);
161010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return V;
162010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
163010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
164010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
165010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  Function *Callee = CI->getCalledFunction();
166010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  assert(Callee && "Cannot lower an indirect call!");
167010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
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";
172    abort();
173  default:
174    std::cerr << "Error: Code generator does not support intrinsic function '"
175              << Callee->getName() << "'!\n";
176    abort();
177
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);
188    break;
189  }
190  case Intrinsic::sigsetjmp:
191     if (CI->getType() != Type::VoidTy)
192       CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
193     break;
194
195  case Intrinsic::longjmp: {
196    static Function *LongjmpFCache = 0;
197    ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
198                    Type::VoidTy, LongjmpFCache);
199    break;
200  }
201
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,
206                    AbortFCache);
207    break;
208  }
209  case Intrinsic::ctpop:
210    CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
211    break;
212
213  case Intrinsic::ctlz: {
214    Value *Src = CI->getOperand(1);
215    Value* SA;
216    switch (CI->getOperand(0)->getType()->getTypeID())
217    {
218    case Type::LongTyID:
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);
223    case Type::IntTyID:
224    case Type::UIntTyID:
225      SA = ConstantUInt::get(Type::UByteTy, 16);
226      Src = BinaryOperator::createOr(Src, new ShiftInst(Instruction::Shr,
227                                                        Src, SA, "", CI),
228                                     "", CI);
229    case Type::ShortTyID:
230    case Type::UShortTyID:
231      SA = ConstantUInt::get(Type::UByteTy, 8);
232      Src = BinaryOperator::createOr(Src, new ShiftInst(Instruction::Shr,
233                                                        Src, SA, "", CI),
234                                     "", CI);
235    default:
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);
245    };
246    Src = BinaryOperator::createNot(Src, "", CI);
247
248
249    Src = LowerCTPOP(Src, CI);
250    CI->replaceAllUsesWith(Src);
251    break;
252  }
253  case Intrinsic::cttz: {
254    Value *Src = CI->getOperand(1);
255    Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
256    Src = BinaryOperator::createAnd(NotSrc,
257                                    BinaryOperator::createSub(Src,
258                           ConstantUInt::get(CI->getOperand(0)->getType(), 1), "", CI));
259
260    Src = LowerCTPOP(Src, CI);
261    CI->replaceAllUsesWith(Src);
262    break;
263  }
264
265  case Intrinsic::returnaddress:
266  case Intrinsic::frameaddress:
267    std::cerr << "WARNING: this target does not support the llvm."
268              << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
269                  "return" : "frame") << "address intrinsic.\n";
270    CI->replaceAllUsesWith(ConstantPointerNull::get(
271                                            cast<PointerType>(CI->getType())));
272    break;
273
274  case Intrinsic::prefetch:
275    break;    // Simply strip out prefetches on unsupported architectures
276
277  case Intrinsic::pcmarker:
278    break;    // Simply strip out pcmarker on unsupported architectures
279
280  case Intrinsic::dbg_stoppoint:
281  case Intrinsic::dbg_region_start:
282  case Intrinsic::dbg_region_end:
283  case Intrinsic::dbg_declare:
284  case Intrinsic::dbg_func_start:
285    if (CI->getType() != Type::VoidTy)
286      CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
287    break;    // Simply strip out debugging intrinsics
288
289  case Intrinsic::memcpy: {
290    // The memcpy intrinsic take an extra alignment argument that the memcpy
291    // libc function does not.
292    static Function *MemcpyFCache = 0;
293    ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
294                    (*(CI->op_begin()+1))->getType(), MemcpyFCache);
295    break;
296  }
297  case Intrinsic::memmove: {
298    // The memmove intrinsic take an extra alignment argument that the memmove
299    // libc function does not.
300    static Function *MemmoveFCache = 0;
301    ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
302                    (*(CI->op_begin()+1))->getType(), MemmoveFCache);
303    break;
304  }
305  case Intrinsic::memset: {
306    // The memset intrinsic take an extra alignment argument that the memset
307    // libc function does not.
308    static Function *MemsetFCache = 0;
309    ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
310                    (*(CI->op_begin()+1))->getType(), MemsetFCache);
311    break;
312  }
313  case Intrinsic::isunordered: {
314    Value *L = CI->getOperand(1);
315    Value *R = CI->getOperand(2);
316
317    Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI);
318    Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI);
319    CI->replaceAllUsesWith(
320      BinaryOperator::create(Instruction::Or, LIsNan, RIsNan,
321                             "isunordered", CI));
322    break;
323  }
324  case Intrinsic::sqrt: {
325    static Function *sqrtFCache = 0;
326    static Function *sqrtfFCache = 0;
327    if(CI->getType() == Type::FloatTy)
328      ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
329                      Type::FloatTy, sqrtfFCache);
330    else
331      ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
332                      Type::DoubleTy, sqrtFCache);
333    break;
334  }
335  }
336
337  assert(CI->use_empty() &&
338         "Lowering should have eliminated any uses of the intrinsic call!");
339  CI->eraseFromParent();
340}
341