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