blob: 4225f26e75b1b3f3dc74b1b0748720865ed902d8 [file] [log] [blame]
Anders Carlsson49865302007-08-20 18:05:56 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Anders Carlsson and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Builtin calls as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Anders Carlsson6f6c6482007-12-09 21:20:04 +000016#include "clang/Basic/TargetInfo.h"
Chris Lattnerab862cc2007-08-31 04:31:45 +000017#include "clang/AST/ASTContext.h"
Anders Carlsson49865302007-08-20 18:05:56 +000018#include "clang/AST/Builtins.h"
19#include "clang/AST/Expr.h"
Anders Carlssone1449c12007-12-09 23:17:02 +000020#include "clang/AST/TargetBuiltins.h"
Chris Lattner783a78f2007-08-26 04:17:05 +000021#include "llvm/Constants.h"
Chris Lattner02c60f52007-08-31 04:44:06 +000022#include "llvm/Function.h"
Anders Carlssoncebb8d62007-10-12 23:56:29 +000023#include "llvm/Intrinsics.h"
Anders Carlsson49865302007-08-20 18:05:56 +000024using namespace clang;
25using namespace CodeGen;
Anders Carlsson6f6c6482007-12-09 21:20:04 +000026using namespace llvm;
27
Chris Lattner783a78f2007-08-26 04:17:05 +000028RValue CodeGenFunction::EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E) {
29 switch (BuiltinID) {
Anders Carlsson6f6c6482007-12-09 21:20:04 +000030 default: {
Chris Lattner02c60f52007-08-31 04:44:06 +000031 if (getContext().BuiltinInfo.isLibFunction(BuiltinID))
32 return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID), E);
Anders Carlsson6f6c6482007-12-09 21:20:04 +000033
34 // See if we have a target specific intrinsic.
Chris Lattner9d37e132007-12-13 07:34:23 +000035 Intrinsic::ID IntrinsicID;
Anders Carlsson6f6c6482007-12-09 21:20:04 +000036 const char *TargetPrefix = Target.getTargetPrefix();
37 const char *BuiltinName = getContext().BuiltinInfo.GetName(BuiltinID);
38#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
39#include "llvm/Intrinsics.gen"
40#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
41
42 if (IntrinsicID != Intrinsic::not_intrinsic) {
Chris Lattner9d37e132007-12-13 07:34:23 +000043 SmallVector<Value*, 16> Args;
Anders Carlsson6f6c6482007-12-09 21:20:04 +000044
Chris Lattner9d37e132007-12-13 07:34:23 +000045 Function *F = Intrinsic::getDeclaration(&CGM.getModule(), IntrinsicID);
Anders Carlsson6f6c6482007-12-09 21:20:04 +000046 const llvm::FunctionType *FTy = F->getFunctionType();
47
48 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattner9d37e132007-12-13 07:34:23 +000049 Value *ArgValue = EmitScalarExpr(E->getArg(i));
Anders Carlsson6f6c6482007-12-09 21:20:04 +000050
51 // If the intrinsic arg type is different from the builtin arg type
52 // we need to do a bit cast.
53 const llvm::Type *PTy = FTy->getParamType(i);
54 if (PTy != ArgValue->getType()) {
55 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
56 "Must be able to losslessly bit cast to param");
57 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
58 }
59
60 Args.push_back(ArgValue);
61 }
62
Chris Lattner9d37e132007-12-13 07:34:23 +000063 Value *V = Builder.CreateCall(F, &Args[0], &Args[0] + Args.size());
Anders Carlsson6f6c6482007-12-09 21:20:04 +000064 QualType BuiltinRetType = E->getType();
65
Chris Lattner9d37e132007-12-13 07:34:23 +000066 const llvm::Type *RetTy = llvm::Type::VoidTy;
67 if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType);
Anders Carlsson6f6c6482007-12-09 21:20:04 +000068
69 if (RetTy != V->getType()) {
70 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
71 "Must be able to losslessly bit cast result type");
Anders Carlsson6f6c6482007-12-09 21:20:04 +000072 V = Builder.CreateBitCast(V, RetTy);
73 }
74
75 return RValue::get(V);
76 }
Anders Carlssone1449c12007-12-09 23:17:02 +000077
78 // See if we have a target specific builtin that needs to be lowered.
Chris Lattner9d37e132007-12-13 07:34:23 +000079 Value *V = 0;
Chris Lattner02c60f52007-08-31 04:44:06 +000080
Anders Carlssone1449c12007-12-09 23:17:02 +000081 if (strcmp(TargetPrefix, "x86") == 0)
82 V = EmitX86BuiltinExpr(BuiltinID, E);
83 else if (strcmp(TargetPrefix, "ppc") == 0)
84 V = EmitPPCBuiltinExpr(BuiltinID, E);
85
86 if (V)
87 return RValue::get(V);
88
Chris Lattnere8f49632007-12-02 01:49:16 +000089 WarnUnsupported(E, "builtin function");
Chris Lattner783a78f2007-08-26 04:17:05 +000090
91 // Unknown builtin, for now just dump it out and return undef.
92 if (hasAggregateLLVMType(E->getType()))
93 return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType())));
Chris Lattner9d37e132007-12-13 07:34:23 +000094 return RValue::get(UndefValue::get(ConvertType(E->getType())));
Anders Carlsson6f6c6482007-12-09 21:20:04 +000095 }
Chris Lattner783a78f2007-08-26 04:17:05 +000096 case Builtin::BI__builtin___CFStringMakeConstantString: {
97 const Expr *Arg = E->getArg(0);
98
Anders Carlsson783e1432007-11-01 00:39:26 +000099 while (1) {
100 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Arg))
101 Arg = PE->getSubExpr();
102 else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Arg))
103 Arg = CE->getSubExpr();
104 else
105 break;
106 }
Chris Lattner783a78f2007-08-26 04:17:05 +0000107
108 const StringLiteral *Literal = cast<StringLiteral>(Arg);
109 std::string S(Literal->getStrData(), Literal->getByteLength());
110
111 return RValue::get(CGM.GetAddrOfConstantCFString(S));
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000112 }
113 case Builtin::BI__builtin_va_start:
114 case Builtin::BI__builtin_va_end: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000115 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000116 const llvm::Type *DestType = llvm::PointerType::get(llvm::Type::Int8Ty);
117 if (ArgValue->getType() != DestType)
118 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
119 ArgValue->getNameStart());
120
Chris Lattner9d37e132007-12-13 07:34:23 +0000121 Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_start) ?
122 Intrinsic::vastart : Intrinsic::vaend;
123 Value *F = Intrinsic::getDeclaration(&CGM.getModule(), inst);
124 Value *V = Builder.CreateCall(F, ArgValue);
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000125
126 return RValue::get(V);
127 }
Anders Carlsson40438402007-10-29 02:59:40 +0000128 case Builtin::BI__builtin_classify_type: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000129 APSInt Result(32);
Anders Carlsson40438402007-10-29 02:59:40 +0000130 if (!E->isBuiltinClassifyType(Result))
131 assert(0 && "Expr not __builtin_classify_type!");
Chris Lattner9d37e132007-12-13 07:34:23 +0000132 return RValue::get(ConstantInt::get(Result));
Anders Carlsson49865302007-08-20 18:05:56 +0000133 }
Anders Carlsson783e1432007-11-01 00:39:26 +0000134 case Builtin::BI__builtin_constant_p: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000135 APSInt Result(32);
Anders Carlsson783e1432007-11-01 00:39:26 +0000136 // FIXME: Analyze the parameter and check if it is a constant.
137 Result = 0;
Chris Lattner9d37e132007-12-13 07:34:23 +0000138 return RValue::get(ConstantInt::get(Result));
Anders Carlsson783e1432007-11-01 00:39:26 +0000139 }
Anders Carlssone79dbe22007-11-20 19:05:17 +0000140 case Builtin::BI__builtin_abs: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000141 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssone79dbe22007-11-20 19:05:17 +0000142
143 llvm::BinaryOperator *NegOp =
144 Builder.CreateNeg(ArgValue, (ArgValue->getName() + "neg").c_str());
Chris Lattner9d37e132007-12-13 07:34:23 +0000145 Value *CmpResult =
Anders Carlssone79dbe22007-11-20 19:05:17 +0000146 Builder.CreateICmpSGE(ArgValue, NegOp->getOperand(0), "abscond");
Chris Lattner9d37e132007-12-13 07:34:23 +0000147 Value *Result =
Anders Carlssone79dbe22007-11-20 19:05:17 +0000148 Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
149
150 return RValue::get(Result);
151 }
Chris Lattner9d37e132007-12-13 07:34:23 +0000152 case Builtin::BI__builtin_expect:
153 return RValue::get(EmitScalarExpr(E->getArg(0)));
Anders Carlsson56118002007-12-02 21:58:10 +0000154 case Builtin::BI__builtin_bswap32:
155 case Builtin::BI__builtin_bswap64: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000156 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlsson56118002007-12-02 21:58:10 +0000157 const llvm::Type *ArgType = ArgValue->getType();
Chris Lattner9d37e132007-12-13 07:34:23 +0000158 Value *F = Intrinsic::getDeclaration(&CGM.getModule(), Intrinsic::bswap,
159 &ArgType, 1);
160 return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
Anders Carlsson56118002007-12-02 21:58:10 +0000161 }
Anders Carlssona8897f02007-12-05 07:22:48 +0000162 case Builtin::BI__builtin_inff: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000163 APFloat f(APFloat::IEEEsingle, APFloat::fcInfinity, false);
164 return RValue::get(ConstantFP::get(llvm::Type::FloatTy, f));
Anders Carlssona8897f02007-12-05 07:22:48 +0000165 }
166 case Builtin::BI__builtin_inf:
167 // FIXME: mapping long double onto double.
168 case Builtin::BI__builtin_infl: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000169 APFloat f(APFloat::IEEEdouble, APFloat::fcInfinity, false);
170 return RValue::get(ConstantFP::get(llvm::Type::DoubleTy, f));
Anders Carlssona8897f02007-12-05 07:22:48 +0000171 }
Anders Carlsson40438402007-10-29 02:59:40 +0000172 }
Anders Carlsson49865302007-08-20 18:05:56 +0000173 return RValue::get(0);
174}
Anders Carlssone1449c12007-12-09 23:17:02 +0000175
Chris Lattner9d37e132007-12-13 07:34:23 +0000176Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
177 const CallExpr *E) {
Anders Carlsson04c4b752007-12-14 17:48:24 +0000178
179 llvm::SmallVector<Value*, 4> Ops;
180
181 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
182 Ops.push_back(EmitScalarExpr(E->getArg(i)));
183
Anders Carlssone1449c12007-12-09 23:17:02 +0000184 switch (BuiltinID) {
Anders Carlsson0a35a812007-12-09 23:39:18 +0000185 default: return 0;
186 case X86::BI__builtin_ia32_mulps:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000187 return Builder.CreateMul(Ops[0], Ops[1], "mulps");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000188 case X86::BI__builtin_ia32_pand:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000189 return Builder.CreateAnd(Ops[0], Ops[1], "pand");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000190 case X86::BI__builtin_ia32_por:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000191 return Builder.CreateAnd(Ops[0], Ops[1], "por");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000192 case X86::BI__builtin_ia32_pxor:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000193 return Builder.CreateAnd(Ops[0], Ops[1], "pxor");
Chris Lattner70376292007-12-11 04:33:00 +0000194 case X86::BI__builtin_ia32_pandn: {
Anders Carlsson04c4b752007-12-14 17:48:24 +0000195 Ops[0] = Builder.CreateNot(Ops[0], "tmp");
196 return Builder.CreateAnd(Ops[0], Ops[1], "pandn");
Chris Lattner70376292007-12-11 04:33:00 +0000197 }
Anders Carlsson17b80c12007-12-11 02:25:54 +0000198 case X86::BI__builtin_ia32_paddb:
199 case X86::BI__builtin_ia32_paddd:
200 case X86::BI__builtin_ia32_paddq:
201 case X86::BI__builtin_ia32_paddw:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000202 return Builder.CreateAdd(Ops[0], Ops[1], "padd");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000203 case X86::BI__builtin_ia32_psubb:
204 case X86::BI__builtin_ia32_psubd:
205 case X86::BI__builtin_ia32_psubq:
206 case X86::BI__builtin_ia32_psubw:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000207 return Builder.CreateSub(Ops[0], Ops[1], "psub");
Anders Carlsson820cb742007-12-11 03:09:22 +0000208 case X86::BI__builtin_ia32_pmullw:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000209 return Builder.CreateMul(Ops[0], Ops[1], "pmul");
Anders Carlsson820cb742007-12-11 03:09:22 +0000210 case X86::BI__builtin_ia32_punpckhbw:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000211 return EmitShuffleVector(Ops[0], Ops[1],
Anders Carlsson820cb742007-12-11 03:09:22 +0000212 4, 12, 5, 13, 6, 14, 7, 15,
213 "punpckhbw");
214 case X86::BI__builtin_ia32_punpckhwd:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000215 return EmitShuffleVector(Ops[0], Ops[1],
Anders Carlsson820cb742007-12-11 03:09:22 +0000216 2, 6, 3, 7,
217 "punpckhwd");
218 case X86::BI__builtin_ia32_punpckhdq:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000219 return EmitShuffleVector(Ops[0], Ops[1],
Anders Carlsson820cb742007-12-11 03:09:22 +0000220 1, 3,
221 "punpckhdq");
222 case X86::BI__builtin_ia32_punpcklbw:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000223 return EmitShuffleVector(Ops[0], Ops[1],
Anders Carlsson820cb742007-12-11 03:09:22 +0000224 0, 8, 1, 9, 2, 10, 3, 11,
225 "punpcklbw");
226 case X86::BI__builtin_ia32_punpcklwd:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000227 return EmitShuffleVector(Ops[0], Ops[1],
Anders Carlsson820cb742007-12-11 03:09:22 +0000228 0, 4, 1, 5,
229 "punpcklwd");
230 case X86::BI__builtin_ia32_punpckldq:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000231 return EmitShuffleVector(Ops[0], Ops[1],
Anders Carlsson820cb742007-12-11 03:09:22 +0000232 0, 2,
233 "punpckldq");
Anders Carlsson04c4b752007-12-14 17:48:24 +0000234 case X86::BI__builtin_ia32_pslldi:
235 case X86::BI__builtin_ia32_psllqi:
236 case X86::BI__builtin_ia32_psllwi:
237 case X86::BI__builtin_ia32_psradi:
238 case X86::BI__builtin_ia32_psrawi:
239 case X86::BI__builtin_ia32_psrldi:
240 case X86::BI__builtin_ia32_psrlqi:
241 case X86::BI__builtin_ia32_psrlwi: {
242 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext");
243 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1);
244 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000245
Anders Carlsson04c4b752007-12-14 17:48:24 +0000246 const char *name = 0;
247 Intrinsic::ID ID = Intrinsic::not_intrinsic;
248
249 switch (BuiltinID) {
250 default: assert(0 && "Unsupported shift intrinsic!");
251 case X86::BI__builtin_ia32_pslldi:
252 name = "pslldi";
253 ID = Intrinsic::x86_mmx_psll_d;
254 break;
255 case X86::BI__builtin_ia32_psllqi:
256 name = "psllqi";
257 ID = Intrinsic::x86_mmx_psll_q;
258 break;
259 case X86::BI__builtin_ia32_psllwi:
260 name = "psllwi";
261 ID = Intrinsic::x86_mmx_psll_w;
262 break;
263 case X86::BI__builtin_ia32_psradi:
264 name = "psradi";
265 ID = Intrinsic::x86_mmx_psra_d;
266 break;
267 case X86::BI__builtin_ia32_psrawi:
268 name = "psrawi";
269 ID = Intrinsic::x86_mmx_psra_w;
270 break;
271 case X86::BI__builtin_ia32_psrldi:
272 name = "psrldi";
273 ID = Intrinsic::x86_mmx_psrl_d;
274 break;
275 case X86::BI__builtin_ia32_psrlqi:
276 name = "psrlqi";
277 ID = Intrinsic::x86_mmx_psrl_q;
278 break;
279 case X86::BI__builtin_ia32_psrlwi:
280 name = "psrlwi";
281 ID = Intrinsic::x86_mmx_psrl_w;
282 break;
283 }
284
285 llvm::Function *F = Intrinsic::getDeclaration(&CGM.getModule(), ID);
286 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
287 }
288 case X86::BI__builtin_ia32_pshufd: {
289 int i = cast<ConstantInt>(Ops[1])->getZExtValue();
290
291 return EmitShuffleVector(Ops[0], Ops[0],
Anders Carlsson17b80c12007-12-11 02:25:54 +0000292 i & 0x3, (i & 0xc) >> 2,
293 (i & 0x30) >> 4, (i & 0xc0) >> 6,
294 "pshufd");
295 }
Anders Carlssone1449c12007-12-09 23:17:02 +0000296 }
297}
298
Chris Lattner9d37e132007-12-13 07:34:23 +0000299Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
300 const CallExpr *E) {
Anders Carlssone1449c12007-12-09 23:17:02 +0000301 switch (BuiltinID) {
Anders Carlsson0a35a812007-12-09 23:39:18 +0000302 default: return 0;
Anders Carlssone1449c12007-12-09 23:17:02 +0000303 }
304}