blob: 66d4e60c965e4c6c7f2a5e5992bf4c83adf12928 [file] [log] [blame]
Anders Carlsson022012e2007-08-20 18:05:56 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Anders Carlsson022012e2007-08-20 18:05:56 +00007//
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 Carlssonca6fcfa2007-12-09 21:20:04 +000016#include "clang/Basic/TargetInfo.h"
Chris Lattnerbef20ac2007-08-31 04:31:45 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000018#include "clang/AST/Decl.h"
Anders Carlsson564f1de2007-12-09 23:17:02 +000019#include "clang/AST/TargetBuiltins.h"
Anders Carlsson793680e2007-10-12 23:56:29 +000020#include "llvm/Intrinsics.h"
Anders Carlsson022012e2007-08-20 18:05:56 +000021using namespace clang;
22using namespace CodeGen;
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000023using namespace llvm;
24
Mon P Wang1ffe2812008-05-09 22:40:52 +000025/// Utility to insert an atomic instruction based Instrinsic::ID and
26// the expression node
27static RValue EmitBinaryAtomic(CodeGenFunction& CFG,
28 Intrinsic::ID Id, const CallExpr *E) {
Mon P Wangc5004512008-07-31 03:28:23 +000029 const llvm::Type *ResType[2];
30 ResType[0] = CFG.ConvertType(E->getType());
31 ResType[1] = CFG.ConvertType(E->getArg(0)->getType());
32 Value *AtomF = CFG.CGM.getIntrinsic(Id, ResType, 2);
Mon P Wang1ffe2812008-05-09 22:40:52 +000033 return RValue::get(CFG.Builder.CreateCall2(AtomF,
34 CFG.EmitScalarExpr(E->getArg(0)),
35 CFG.EmitScalarExpr(E->getArg(1))));
36}
37
Chris Lattner6de93ff2007-08-26 04:17:05 +000038RValue CodeGenFunction::EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E) {
39 switch (BuiltinID) {
Chris Lattnerb7cfe882008-06-30 18:32:54 +000040 default: break; // Handle intrinsics and libm functions below.
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000041
Chris Lattner6de93ff2007-08-26 04:17:05 +000042 case Builtin::BI__builtin___CFStringMakeConstantString: {
43 const Expr *Arg = E->getArg(0);
44
Anders Carlssond6a275f2007-11-01 00:39:26 +000045 while (1) {
46 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Arg))
47 Arg = PE->getSubExpr();
48 else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Arg))
49 Arg = CE->getSubExpr();
50 else
51 break;
52 }
Chris Lattner6de93ff2007-08-26 04:17:05 +000053
54 const StringLiteral *Literal = cast<StringLiteral>(Arg);
55 std::string S(Literal->getStrData(), Literal->getByteLength());
56
57 return RValue::get(CGM.GetAddrOfConstantCFString(S));
Anders Carlsson793680e2007-10-12 23:56:29 +000058 }
Chris Lattner6a705f02008-07-09 17:28:44 +000059 case Builtin::BI__builtin_stdarg_start:
Anders Carlsson793680e2007-10-12 23:56:29 +000060 case Builtin::BI__builtin_va_start:
61 case Builtin::BI__builtin_va_end: {
Chris Lattner1feedd82007-12-13 07:34:23 +000062 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Christopher Lambddc23f32007-12-17 01:11:20 +000063 const llvm::Type *DestType =
64 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Anders Carlsson793680e2007-10-12 23:56:29 +000065 if (ArgValue->getType() != DestType)
66 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
67 ArgValue->getNameStart());
68
Chris Lattner6a705f02008-07-09 17:28:44 +000069 Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ?
70 Intrinsic::vaend : Intrinsic::vastart;
Chris Lattner7acda7c2007-12-18 00:25:38 +000071 return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue));
Anders Carlsson793680e2007-10-12 23:56:29 +000072 }
Anders Carlssona28ef8b2008-02-09 20:26:43 +000073 case Builtin::BI__builtin_va_copy: {
74 // FIXME: This does not yet handle architectures where va_list is a struct.
75 Value *DstPtr = EmitScalarExpr(E->getArg(0));
76 Value *SrcValue = EmitScalarExpr(E->getArg(1));
77
78 Value *SrcPtr = CreateTempAlloca(SrcValue->getType(), "dst_ptr");
79
Anders Carlssona28ef8b2008-02-09 20:26:43 +000080 Builder.CreateStore(SrcValue, SrcPtr, false);
81
82 const llvm::Type *Type =
83 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
84
85 DstPtr = Builder.CreateBitCast(DstPtr, Type);
86 SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
Chris Lattner3eae03e2008-05-06 00:56:42 +000087 return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy),
88 DstPtr, SrcPtr));
Anders Carlssona28ef8b2008-02-09 20:26:43 +000089 }
Anders Carlsson89799cf2007-10-29 02:59:40 +000090 case Builtin::BI__builtin_classify_type: {
Chris Lattner1feedd82007-12-13 07:34:23 +000091 APSInt Result(32);
Anders Carlsson89799cf2007-10-29 02:59:40 +000092 if (!E->isBuiltinClassifyType(Result))
93 assert(0 && "Expr not __builtin_classify_type!");
Chris Lattner1feedd82007-12-13 07:34:23 +000094 return RValue::get(ConstantInt::get(Result));
Anders Carlsson022012e2007-08-20 18:05:56 +000095 }
Anders Carlssond6a275f2007-11-01 00:39:26 +000096 case Builtin::BI__builtin_constant_p: {
Chris Lattner1feedd82007-12-13 07:34:23 +000097 APSInt Result(32);
Anders Carlssond6a275f2007-11-01 00:39:26 +000098 // FIXME: Analyze the parameter and check if it is a constant.
99 Result = 0;
Chris Lattner1feedd82007-12-13 07:34:23 +0000100 return RValue::get(ConstantInt::get(Result));
Anders Carlssond6a275f2007-11-01 00:39:26 +0000101 }
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000102 case Builtin::BI__builtin_abs: {
Chris Lattner1feedd82007-12-13 07:34:23 +0000103 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000104
Chris Lattner9a847f52008-07-23 06:53:34 +0000105 Value *NegOp = Builder.CreateNeg(ArgValue, "neg");
Chris Lattner1feedd82007-12-13 07:34:23 +0000106 Value *CmpResult =
Chris Lattner9a847f52008-07-23 06:53:34 +0000107 Builder.CreateICmpSGE(ArgValue, Constant::getNullValue(ArgValue->getType()),
108 "abscond");
Chris Lattner1feedd82007-12-13 07:34:23 +0000109 Value *Result =
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000110 Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
111
112 return RValue::get(Result);
113 }
Anders Carlsson3a31d602008-02-06 07:19:27 +0000114 case Builtin::BI__builtin_ctz:
115 case Builtin::BI__builtin_ctzl:
116 case Builtin::BI__builtin_ctzll: {
117 Value *ArgValue = EmitScalarExpr(E->getArg(0));
118
119 const llvm::Type *ArgType = ArgValue->getType();
120 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
121
122 const llvm::Type *ResultType = ConvertType(E->getType());
123 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
124 if (Result->getType() != ResultType)
125 Result = Builder.CreateIntCast(Result, ResultType, "cast");
126 return RValue::get(Result);
127 }
Eli Friedmanf4e85332008-05-27 15:32:46 +0000128 case Builtin::BI__builtin_clz:
129 case Builtin::BI__builtin_clzl:
130 case Builtin::BI__builtin_clzll: {
131 Value *ArgValue = EmitScalarExpr(E->getArg(0));
132
133 const llvm::Type *ArgType = ArgValue->getType();
134 Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1);
135
136 const llvm::Type *ResultType = ConvertType(E->getType());
137 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
138 if (Result->getType() != ResultType)
139 Result = Builder.CreateIntCast(Result, ResultType, "cast");
140 return RValue::get(Result);
141 }
Daniel Dunbar04b29002008-07-21 17:19:41 +0000142 case Builtin::BI__builtin_ffs:
143 case Builtin::BI__builtin_ffsl:
144 case Builtin::BI__builtin_ffsll: {
145 // ffs(x) -> x ? cttz(x) + 1 : 0
146 Value *ArgValue = EmitScalarExpr(E->getArg(0));
147
148 const llvm::Type *ArgType = ArgValue->getType();
149 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
150
151 const llvm::Type *ResultType = ConvertType(E->getType());
152 Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"),
153 ConstantInt::get(ArgType, 1), "tmp");
154 Value *Zero = llvm::Constant::getNullValue(ArgType);
155 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
156 Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
157 if (Result->getType() != ResultType)
158 Result = Builder.CreateIntCast(Result, ResultType, "cast");
159 return RValue::get(Result);
160 }
161 case Builtin::BI__builtin_parity:
162 case Builtin::BI__builtin_parityl:
163 case Builtin::BI__builtin_parityll: {
164 // parity(x) -> ctpop(x) & 1
165 Value *ArgValue = EmitScalarExpr(E->getArg(0));
166
167 const llvm::Type *ArgType = ArgValue->getType();
168 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
169
170 const llvm::Type *ResultType = ConvertType(E->getType());
171 Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp");
172 Value *Result = Builder.CreateAnd(Tmp, ConstantInt::get(ArgType, 1),
173 "tmp");
174 if (Result->getType() != ResultType)
175 Result = Builder.CreateIntCast(Result, ResultType, "cast");
176 return RValue::get(Result);
177 }
178 case Builtin::BI__builtin_popcount:
179 case Builtin::BI__builtin_popcountl:
180 case Builtin::BI__builtin_popcountll: {
181 Value *ArgValue = EmitScalarExpr(E->getArg(0));
182
183 const llvm::Type *ArgType = ArgValue->getType();
184 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
185
186 const llvm::Type *ResultType = ConvertType(E->getType());
187 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
188 if (Result->getType() != ResultType)
189 Result = Builder.CreateIntCast(Result, ResultType, "cast");
190 return RValue::get(Result);
191 }
Chris Lattner1feedd82007-12-13 07:34:23 +0000192 case Builtin::BI__builtin_expect:
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000193 // FIXME: pass expect through to LLVM
Chris Lattner1feedd82007-12-13 07:34:23 +0000194 return RValue::get(EmitScalarExpr(E->getArg(0)));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000195 case Builtin::BI__builtin_bswap32:
196 case Builtin::BI__builtin_bswap64: {
Chris Lattner1feedd82007-12-13 07:34:23 +0000197 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000198 const llvm::Type *ArgType = ArgValue->getType();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000199 Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
Chris Lattner1feedd82007-12-13 07:34:23 +0000200 return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
Daniel Dunbar4493f792008-07-21 22:59:13 +0000201 }
202 case Builtin::BI__builtin_prefetch: {
203 Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
204 // FIXME: Technically these constants should of type 'int', yes?
205 RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
206 ConstantInt::get(llvm::Type::Int32Ty, 0);
207 Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
208 ConstantInt::get(llvm::Type::Int32Ty, 3);
209 Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0);
210 return RValue::get(Builder.CreateCall3(F, Address, RW, Locality));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000211 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000212 case Builtin::BI__builtin_trap: {
213 Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0);
214 return RValue::get(Builder.CreateCall(F));
215 }
216
Anders Carlsson478547c2008-02-01 16:06:34 +0000217 case Builtin::BI__builtin_huge_val:
Daniel Dunbar04b29002008-07-21 17:19:41 +0000218 case Builtin::BI__builtin_huge_valf:
219 case Builtin::BI__builtin_huge_vall:
Anders Carlssonc8ee7982007-12-05 07:22:48 +0000220 case Builtin::BI__builtin_inf:
Daniel Dunbar04b29002008-07-21 17:19:41 +0000221 case Builtin::BI__builtin_inff:
Anders Carlssonc8ee7982007-12-05 07:22:48 +0000222 case Builtin::BI__builtin_infl: {
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000223 const llvm::fltSemantics &Sem =
224 CGM.getContext().getFloatTypeSemantics(E->getType());
225 return RValue::get(ConstantFP::get(APFloat::getInf(Sem)));
226 }
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000227 case Builtin::BI__builtin_nan:
Daniel Dunbar04b29002008-07-21 17:19:41 +0000228 case Builtin::BI__builtin_nanf:
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000229 case Builtin::BI__builtin_nanl: {
230 // If this is __builtin_nan("") turn this into a simple nan, otherwise just
231 // call libm nan.
232 if (const StringLiteral *S =
233 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
234 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
235 const llvm::fltSemantics &Sem =
236 CGM.getContext().getFloatTypeSemantics(E->getType());
237 return RValue::get(ConstantFP::get(APFloat::getNaN(Sem)));
238 }
239 }
240 // Otherwise, call libm 'nan'.
241 break;
Anders Carlssonc8ee7982007-12-05 07:22:48 +0000242 }
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000243 case Builtin::BI__builtin_powi:
244 case Builtin::BI__builtin_powif:
245 case Builtin::BI__builtin_powil: {
246 Value *Base = EmitScalarExpr(E->getArg(0));
247 Value *Exponent = EmitScalarExpr(E->getArg(1));
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000248 const llvm::Type *ArgType = Base->getType();
249 Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1);
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000250 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
251 }
252
Chris Lattnerfe23e212007-12-20 00:44:32 +0000253 case Builtin::BI__builtin_isgreater:
254 case Builtin::BI__builtin_isgreaterequal:
255 case Builtin::BI__builtin_isless:
256 case Builtin::BI__builtin_islessequal:
257 case Builtin::BI__builtin_islessgreater:
258 case Builtin::BI__builtin_isunordered: {
259 // Ordered comparisons: we know the arguments to these are matching scalar
260 // floating point values.
261 Value *LHS = EmitScalarExpr(E->getArg(0));
262 Value *RHS = EmitScalarExpr(E->getArg(1));
263
264 switch (BuiltinID) {
265 default: assert(0 && "Unknown ordered comparison");
266 case Builtin::BI__builtin_isgreater:
267 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
268 break;
269 case Builtin::BI__builtin_isgreaterequal:
270 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
271 break;
272 case Builtin::BI__builtin_isless:
273 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
274 break;
275 case Builtin::BI__builtin_islessequal:
276 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
277 break;
278 case Builtin::BI__builtin_islessgreater:
279 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
280 break;
281 case Builtin::BI__builtin_isunordered:
282 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
283 break;
284 }
285 // ZExt bool to int type.
286 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()),
287 "tmp"));
288 }
Chris Lattner9e800e32008-06-16 17:15:14 +0000289 case Builtin::BI__builtin_alloca: {
290 // FIXME: LLVM IR Should allow alloca with an i64 size!
291 Value *Size = EmitScalarExpr(E->getArg(0));
292 Size = Builder.CreateIntCast(Size, llvm::Type::Int32Ty, false, "tmp");
Daniel Dunbar1caae952008-07-22 00:26:45 +0000293 return RValue::get(Builder.CreateAlloca(llvm::Type::Int8Ty, Size, "tmp"));
294 }
295 case Builtin::BI__builtin_bzero: {
296 Value *Address = EmitScalarExpr(E->getArg(0));
297 Builder.CreateCall4(CGM.getMemSetFn(), Address,
298 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
299 EmitScalarExpr(E->getArg(1)),
300 llvm::ConstantInt::get(llvm::Type::Int32Ty, 1));
301 return RValue::get(Address);
Chris Lattner9e800e32008-06-16 17:15:14 +0000302 }
Eli Friedmand4b32e42008-05-19 23:27:48 +0000303 case Builtin::BI__builtin_memcpy: {
Daniel Dunbar1caae952008-07-22 00:26:45 +0000304 Value *Address = EmitScalarExpr(E->getArg(0));
305 Builder.CreateCall4(CGM.getMemCpyFn(), Address,
306 EmitScalarExpr(E->getArg(1)),
307 EmitScalarExpr(E->getArg(2)),
308 llvm::ConstantInt::get(llvm::Type::Int32Ty, 1));
309 return RValue::get(Address);
310 }
311 case Builtin::BI__builtin_memmove: {
312 Value *Address = EmitScalarExpr(E->getArg(0));
313 Builder.CreateCall4(CGM.getMemMoveFn(), Address,
314 EmitScalarExpr(E->getArg(1)),
315 EmitScalarExpr(E->getArg(2)),
316 llvm::ConstantInt::get(llvm::Type::Int32Ty, 1));
317 return RValue::get(Address);
318 }
319 case Builtin::BI__builtin_memset: {
320 Value *Address = EmitScalarExpr(E->getArg(0));
321 Builder.CreateCall4(CGM.getMemSetFn(), Address,
322 EmitScalarExpr(E->getArg(1)),
323 EmitScalarExpr(E->getArg(2)),
324 llvm::ConstantInt::get(llvm::Type::Int32Ty, 1));
325 return RValue::get(Address);
Eli Friedmand4b32e42008-05-19 23:27:48 +0000326 }
Eli Friedman256f77e2008-05-20 08:59:34 +0000327 case Builtin::BI__builtin_return_address: {
328 Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0);
329 return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0))));
330 }
331 case Builtin::BI__builtin_frame_address: {
332 Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0);
333 return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0))));
334 }
Mon P Wang1ffe2812008-05-09 22:40:52 +0000335 case Builtin::BI__sync_fetch_and_add:
Mon P Wang09b6bf52008-06-25 08:21:36 +0000336 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E);
Mon P Wang1ffe2812008-05-09 22:40:52 +0000337 case Builtin::BI__sync_fetch_and_sub:
Mon P Wang09b6bf52008-06-25 08:21:36 +0000338 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E);
Mon P Wang1ffe2812008-05-09 22:40:52 +0000339 case Builtin::BI__sync_fetch_and_min:
340 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E);
341 case Builtin::BI__sync_fetch_and_max:
342 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E);
343 case Builtin::BI__sync_fetch_and_umin:
344 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E);
345 case Builtin::BI__sync_fetch_and_umax:
346 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E);
347 case Builtin::BI__sync_fetch_and_and:
348 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E);
349 case Builtin::BI__sync_fetch_and_or:
350 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E);
351 case Builtin::BI__sync_fetch_and_xor:
352 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E);
353 case Builtin::BI__sync_val_compare_and_swap: {
354 Value *Args[3];
355 Args[0]= EmitScalarExpr(E->getArg(0));
356 Args[1] = EmitScalarExpr(E->getArg(1));
357 Args[2] = EmitScalarExpr(E->getArg(2));
Mon P Wangc5004512008-07-31 03:28:23 +0000358 const llvm::Type *ResType[2];
359 ResType[0]= ConvertType(E->getType());
360 ResType[1] = ConvertType(E->getArg(0)->getType());
361 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2);
Mon P Wang1ffe2812008-05-09 22:40:52 +0000362 return RValue::get(Builder.CreateCall(AtomF, &Args[0], &Args[1]+2));
Anders Carlsson89799cf2007-10-29 02:59:40 +0000363 }
Mon P Wang1ffe2812008-05-09 22:40:52 +0000364 case Builtin::BI__sync_lock_test_and_set:
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000365 return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E);
366 }
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000367
368 // If this is an alias for a libm function (e.g. __builtin_sin) turn it into
369 // that function.
370 if (getContext().BuiltinInfo.isLibFunction(BuiltinID))
371 return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID),
372 E->getCallee()->getType(), E->arg_begin(),
373 E->arg_end());
374
375 // See if we have a target specific intrinsic.
376 Intrinsic::ID IntrinsicID;
377 const char *TargetPrefix = Target.getTargetPrefix();
378 const char *BuiltinName = getContext().BuiltinInfo.GetName(BuiltinID);
379#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
380#include "llvm/Intrinsics.gen"
381#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
382
383 if (IntrinsicID != Intrinsic::not_intrinsic) {
384 SmallVector<Value*, 16> Args;
385
386 Function *F = CGM.getIntrinsic(IntrinsicID);
387 const llvm::FunctionType *FTy = F->getFunctionType();
388
389 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
390 Value *ArgValue = EmitScalarExpr(E->getArg(i));
391
392 // If the intrinsic arg type is different from the builtin arg type
393 // we need to do a bit cast.
394 const llvm::Type *PTy = FTy->getParamType(i);
395 if (PTy != ArgValue->getType()) {
396 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
397 "Must be able to losslessly bit cast to param");
398 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
399 }
400
401 Args.push_back(ArgValue);
402 }
403
404 Value *V = Builder.CreateCall(F, &Args[0], &Args[0] + Args.size());
405 QualType BuiltinRetType = E->getType();
406
407 const llvm::Type *RetTy = llvm::Type::VoidTy;
408 if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType);
409
410 if (RetTy != V->getType()) {
411 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
412 "Must be able to losslessly bit cast result type");
413 V = Builder.CreateBitCast(V, RetTy);
414 }
415
416 return RValue::get(V);
417 }
418
419 // See if we have a target specific builtin that needs to be lowered.
420 Value *V = 0;
421
422 if (strcmp(TargetPrefix, "x86") == 0)
423 V = EmitX86BuiltinExpr(BuiltinID, E);
424 else if (strcmp(TargetPrefix, "ppc") == 0)
425 V = EmitPPCBuiltinExpr(BuiltinID, E);
426
427 if (V)
428 return RValue::get(V);
429
430 WarnUnsupported(E, "builtin function");
431
432 // Unknown builtin, for now just dump it out and return undef.
433 if (hasAggregateLLVMType(E->getType()))
434 return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType())));
435 return RValue::get(UndefValue::get(ConvertType(E->getType())));
436}
Anders Carlsson564f1de2007-12-09 23:17:02 +0000437
Chris Lattner1feedd82007-12-13 07:34:23 +0000438Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
439 const CallExpr *E) {
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000440
441 llvm::SmallVector<Value*, 4> Ops;
442
443 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
444 Ops.push_back(EmitScalarExpr(E->getArg(i)));
445
Anders Carlsson564f1de2007-12-09 23:17:02 +0000446 switch (BuiltinID) {
Anders Carlsson46a26b02007-12-09 23:39:18 +0000447 default: return 0;
448 case X86::BI__builtin_ia32_mulps:
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000449 return Builder.CreateMul(Ops[0], Ops[1], "mulps");
Nate Begemane7722102008-04-14 04:49:57 +0000450 case X86::BI__builtin_ia32_mulpd:
451 return Builder.CreateMul(Ops[0], Ops[1], "mulpd");
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000452 case X86::BI__builtin_ia32_pand:
Nate Begemane7722102008-04-14 04:49:57 +0000453 case X86::BI__builtin_ia32_pand128:
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000454 return Builder.CreateAnd(Ops[0], Ops[1], "pand");
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000455 case X86::BI__builtin_ia32_por:
Nate Begemane7722102008-04-14 04:49:57 +0000456 case X86::BI__builtin_ia32_por128:
457 return Builder.CreateOr(Ops[0], Ops[1], "por");
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000458 case X86::BI__builtin_ia32_pxor:
Nate Begemane7722102008-04-14 04:49:57 +0000459 case X86::BI__builtin_ia32_pxor128:
460 return Builder.CreateXor(Ops[0], Ops[1], "pxor");
461 case X86::BI__builtin_ia32_pandn:
462 case X86::BI__builtin_ia32_pandn128:
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000463 Ops[0] = Builder.CreateNot(Ops[0], "tmp");
464 return Builder.CreateAnd(Ops[0], Ops[1], "pandn");
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000465 case X86::BI__builtin_ia32_paddb:
Nate Begemane7722102008-04-14 04:49:57 +0000466 case X86::BI__builtin_ia32_paddb128:
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000467 case X86::BI__builtin_ia32_paddd:
Nate Begemane7722102008-04-14 04:49:57 +0000468 case X86::BI__builtin_ia32_paddd128:
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000469 case X86::BI__builtin_ia32_paddq:
Nate Begemane7722102008-04-14 04:49:57 +0000470 case X86::BI__builtin_ia32_paddq128:
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000471 case X86::BI__builtin_ia32_paddw:
Nate Begemane7722102008-04-14 04:49:57 +0000472 case X86::BI__builtin_ia32_paddw128:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000473 case X86::BI__builtin_ia32_addps:
Nate Begemane7722102008-04-14 04:49:57 +0000474 case X86::BI__builtin_ia32_addpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000475 return Builder.CreateAdd(Ops[0], Ops[1], "add");
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000476 case X86::BI__builtin_ia32_psubb:
Nate Begemane7722102008-04-14 04:49:57 +0000477 case X86::BI__builtin_ia32_psubb128:
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000478 case X86::BI__builtin_ia32_psubd:
Nate Begemane7722102008-04-14 04:49:57 +0000479 case X86::BI__builtin_ia32_psubd128:
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000480 case X86::BI__builtin_ia32_psubq:
Nate Begemane7722102008-04-14 04:49:57 +0000481 case X86::BI__builtin_ia32_psubq128:
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000482 case X86::BI__builtin_ia32_psubw:
Nate Begemane7722102008-04-14 04:49:57 +0000483 case X86::BI__builtin_ia32_psubw128:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000484 case X86::BI__builtin_ia32_subps:
Nate Begemane7722102008-04-14 04:49:57 +0000485 case X86::BI__builtin_ia32_subpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000486 return Builder.CreateSub(Ops[0], Ops[1], "sub");
487 case X86::BI__builtin_ia32_divps:
488 return Builder.CreateFDiv(Ops[0], Ops[1], "divps");
Nate Begemane7722102008-04-14 04:49:57 +0000489 case X86::BI__builtin_ia32_divpd:
490 return Builder.CreateFDiv(Ops[0], Ops[1], "divpd");
Anders Carlssondb832302007-12-11 03:09:22 +0000491 case X86::BI__builtin_ia32_pmullw:
Nate Begemane7722102008-04-14 04:49:57 +0000492 case X86::BI__builtin_ia32_pmullw128:
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000493 return Builder.CreateMul(Ops[0], Ops[1], "pmul");
Anders Carlssondb832302007-12-11 03:09:22 +0000494 case X86::BI__builtin_ia32_punpckhbw:
Chris Lattner7acda7c2007-12-18 00:25:38 +0000495 return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15,
Anders Carlssondb832302007-12-11 03:09:22 +0000496 "punpckhbw");
Nate Begemane7722102008-04-14 04:49:57 +0000497 case X86::BI__builtin_ia32_punpckhbw128:
498 return EmitShuffleVector(Ops[0], Ops[1], 8, 24, 9, 25, 10, 26, 11, 27,
499 12, 28, 13, 29, 14, 30, 15, 31,
500 "punpckhbw");
Anders Carlssondb832302007-12-11 03:09:22 +0000501 case X86::BI__builtin_ia32_punpckhwd:
Chris Lattner7acda7c2007-12-18 00:25:38 +0000502 return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhwd");
Nate Begemane7722102008-04-14 04:49:57 +0000503 case X86::BI__builtin_ia32_punpckhwd128:
504 return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15,
505 "punpckhwd");
Anders Carlssondb832302007-12-11 03:09:22 +0000506 case X86::BI__builtin_ia32_punpckhdq:
Chris Lattner7acda7c2007-12-18 00:25:38 +0000507 return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhdq");
Nate Begemane7722102008-04-14 04:49:57 +0000508 case X86::BI__builtin_ia32_punpckhdq128:
509 return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhdq");
Nate Begemanabfaf992008-07-25 20:17:24 +0000510 case X86::BI__builtin_ia32_punpckhqdq128:
511 return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhqdq");
Anders Carlssondb832302007-12-11 03:09:22 +0000512 case X86::BI__builtin_ia32_punpcklbw:
Chris Lattner7acda7c2007-12-18 00:25:38 +0000513 return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11,
Anders Carlssondb832302007-12-11 03:09:22 +0000514 "punpcklbw");
515 case X86::BI__builtin_ia32_punpcklwd:
Chris Lattner7acda7c2007-12-18 00:25:38 +0000516 return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpcklwd");
Anders Carlssondb832302007-12-11 03:09:22 +0000517 case X86::BI__builtin_ia32_punpckldq:
Chris Lattner7acda7c2007-12-18 00:25:38 +0000518 return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpckldq");
Nate Begemane7722102008-04-14 04:49:57 +0000519 case X86::BI__builtin_ia32_punpckldq128:
520 return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpckldq");
Nate Begemanabfaf992008-07-25 20:17:24 +0000521 case X86::BI__builtin_ia32_punpcklqdq128:
522 return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpcklqdq");
Nate Begemane7722102008-04-14 04:49:57 +0000523 case X86::BI__builtin_ia32_pslldi128:
524 case X86::BI__builtin_ia32_psllqi128:
525 case X86::BI__builtin_ia32_psllwi128:
526 case X86::BI__builtin_ia32_psradi128:
527 case X86::BI__builtin_ia32_psrawi128:
528 case X86::BI__builtin_ia32_psrldi128:
529 case X86::BI__builtin_ia32_psrlqi128:
530 case X86::BI__builtin_ia32_psrlwi128: {
531 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext");
532 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 2);
533 llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
534 Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty),
535 Ops[1], Zero, "insert");
536 Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast");
537 const char *name = 0;
538 Intrinsic::ID ID = Intrinsic::not_intrinsic;
539
540 switch (BuiltinID) {
541 default: assert(0 && "Unsupported shift intrinsic!");
542 case X86::BI__builtin_ia32_pslldi128:
543 name = "pslldi";
544 ID = Intrinsic::x86_sse2_psll_d;
545 break;
546 case X86::BI__builtin_ia32_psllqi128:
547 name = "psllqi";
548 ID = Intrinsic::x86_sse2_psll_q;
549 break;
550 case X86::BI__builtin_ia32_psllwi128:
551 name = "psllwi";
552 ID = Intrinsic::x86_sse2_psll_w;
553 break;
554 case X86::BI__builtin_ia32_psradi128:
555 name = "psradi";
556 ID = Intrinsic::x86_sse2_psra_d;
557 break;
558 case X86::BI__builtin_ia32_psrawi128:
559 name = "psrawi";
560 ID = Intrinsic::x86_sse2_psra_w;
561 break;
562 case X86::BI__builtin_ia32_psrldi128:
563 name = "psrldi";
564 ID = Intrinsic::x86_sse2_psrl_d;
565 break;
566 case X86::BI__builtin_ia32_psrlqi128:
567 name = "psrlqi";
568 ID = Intrinsic::x86_sse2_psrl_q;
569 break;
570 case X86::BI__builtin_ia32_psrlwi128:
571 name = "psrlwi";
572 ID = Intrinsic::x86_sse2_psrl_w;
573 break;
574 }
575 llvm::Function *F = CGM.getIntrinsic(ID);
576 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
577 }
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000578 case X86::BI__builtin_ia32_pslldi:
579 case X86::BI__builtin_ia32_psllqi:
580 case X86::BI__builtin_ia32_psllwi:
581 case X86::BI__builtin_ia32_psradi:
582 case X86::BI__builtin_ia32_psrawi:
583 case X86::BI__builtin_ia32_psrldi:
584 case X86::BI__builtin_ia32_psrlqi:
585 case X86::BI__builtin_ia32_psrlwi: {
586 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext");
587 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1);
588 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000589 const char *name = 0;
590 Intrinsic::ID ID = Intrinsic::not_intrinsic;
591
592 switch (BuiltinID) {
593 default: assert(0 && "Unsupported shift intrinsic!");
594 case X86::BI__builtin_ia32_pslldi:
595 name = "pslldi";
596 ID = Intrinsic::x86_mmx_psll_d;
597 break;
598 case X86::BI__builtin_ia32_psllqi:
599 name = "psllqi";
600 ID = Intrinsic::x86_mmx_psll_q;
601 break;
602 case X86::BI__builtin_ia32_psllwi:
603 name = "psllwi";
604 ID = Intrinsic::x86_mmx_psll_w;
605 break;
606 case X86::BI__builtin_ia32_psradi:
607 name = "psradi";
608 ID = Intrinsic::x86_mmx_psra_d;
609 break;
610 case X86::BI__builtin_ia32_psrawi:
611 name = "psrawi";
612 ID = Intrinsic::x86_mmx_psra_w;
613 break;
614 case X86::BI__builtin_ia32_psrldi:
615 name = "psrldi";
616 ID = Intrinsic::x86_mmx_psrl_d;
617 break;
618 case X86::BI__builtin_ia32_psrlqi:
619 name = "psrlqi";
620 ID = Intrinsic::x86_mmx_psrl_q;
621 break;
622 case X86::BI__builtin_ia32_psrlwi:
623 name = "psrlwi";
624 ID = Intrinsic::x86_mmx_psrl_w;
625 break;
626 }
Chris Lattner7acda7c2007-12-18 00:25:38 +0000627 llvm::Function *F = CGM.getIntrinsic(ID);
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000628 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
629 }
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000630 case X86::BI__builtin_ia32_pshuflw: {
631 unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue();
632 return EmitShuffleVector(Ops[0], Ops[0],
633 i & 0x3, (i & 0xc) >> 2,
634 (i & 0x30) >> 4, (i & 0xc0) >> 6, 4, 5, 6, 7,
635 "pshuflw");
636 }
637 case X86::BI__builtin_ia32_pshufhw: {
638 unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue();
639 return EmitShuffleVector(Ops[0], Ops[0], 0, 1, 2, 3,
640 4 + (i & 0x3), 4 + ((i & 0xc) >> 2),
641 4 + ((i & 0x30) >> 4), 4 + ((i & 0xc0) >> 6),
642 "pshufhw");
643 }
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000644 case X86::BI__builtin_ia32_pshufd: {
Chris Lattner7acda7c2007-12-18 00:25:38 +0000645 unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue();
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000646 return EmitShuffleVector(Ops[0], Ops[0],
Anders Carlsson4e4ee212007-12-11 02:25:54 +0000647 i & 0x3, (i & 0xc) >> 2,
648 (i & 0x30) >> 4, (i & 0xc0) >> 6,
649 "pshufd");
650 }
Anders Carlsson6086bbd2007-12-15 21:23:30 +0000651 case X86::BI__builtin_ia32_vec_init_v4hi:
652 case X86::BI__builtin_ia32_vec_init_v8qi:
653 case X86::BI__builtin_ia32_vec_init_v2si:
654 return EmitVector(&Ops[0], Ops.size());
655 case X86::BI__builtin_ia32_vec_ext_v2si:
Nate Begemane7722102008-04-14 04:49:57 +0000656 case X86::BI__builtin_ia32_vec_ext_v2di:
657 case X86::BI__builtin_ia32_vec_ext_v4sf:
658 case X86::BI__builtin_ia32_vec_ext_v4si:
659 case X86::BI__builtin_ia32_vec_ext_v2df:
Anders Carlsson6086bbd2007-12-15 21:23:30 +0000660 return Builder.CreateExtractElement(Ops[0], Ops[1], "result");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000661 case X86::BI__builtin_ia32_cmpordss:
Nate Begemane7722102008-04-14 04:49:57 +0000662 case X86::BI__builtin_ia32_cmpordsd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000663 case X86::BI__builtin_ia32_cmpunordss:
Nate Begemane7722102008-04-14 04:49:57 +0000664 case X86::BI__builtin_ia32_cmpunordsd:
665 case X86::BI__builtin_ia32_cmpeqss:
666 case X86::BI__builtin_ia32_cmpeqsd:
667 case X86::BI__builtin_ia32_cmpltss:
668 case X86::BI__builtin_ia32_cmpltsd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000669 case X86::BI__builtin_ia32_cmpless:
Nate Begemane7722102008-04-14 04:49:57 +0000670 case X86::BI__builtin_ia32_cmplesd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000671 case X86::BI__builtin_ia32_cmpneqss:
Nate Begemane7722102008-04-14 04:49:57 +0000672 case X86::BI__builtin_ia32_cmpneqsd:
673 case X86::BI__builtin_ia32_cmpnltss:
674 case X86::BI__builtin_ia32_cmpnltsd:
675 case X86::BI__builtin_ia32_cmpnless:
676 case X86::BI__builtin_ia32_cmpnlesd: {
Chris Lattner7acda7c2007-12-18 00:25:38 +0000677 unsigned i = 0;
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000678 const char *name = 0;
679 switch (BuiltinID) {
680 default: assert(0 && "Unknown compare builtin!");
681 case X86::BI__builtin_ia32_cmpeqss:
Nate Begemane7722102008-04-14 04:49:57 +0000682 case X86::BI__builtin_ia32_cmpeqsd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000683 i = 0;
Nate Begemane7722102008-04-14 04:49:57 +0000684 name = "cmpeq";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000685 break;
686 case X86::BI__builtin_ia32_cmpltss:
Nate Begemane7722102008-04-14 04:49:57 +0000687 case X86::BI__builtin_ia32_cmpltsd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000688 i = 1;
Nate Begemane7722102008-04-14 04:49:57 +0000689 name = "cmplt";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000690 break;
691 case X86::BI__builtin_ia32_cmpless:
Nate Begemane7722102008-04-14 04:49:57 +0000692 case X86::BI__builtin_ia32_cmplesd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000693 i = 2;
Nate Begemane7722102008-04-14 04:49:57 +0000694 name = "cmple";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000695 break;
696 case X86::BI__builtin_ia32_cmpunordss:
Nate Begemane7722102008-04-14 04:49:57 +0000697 case X86::BI__builtin_ia32_cmpunordsd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000698 i = 3;
Nate Begemane7722102008-04-14 04:49:57 +0000699 name = "cmpunord";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000700 break;
701 case X86::BI__builtin_ia32_cmpneqss:
Nate Begemane7722102008-04-14 04:49:57 +0000702 case X86::BI__builtin_ia32_cmpneqsd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000703 i = 4;
Nate Begemane7722102008-04-14 04:49:57 +0000704 name = "cmpneq";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000705 break;
706 case X86::BI__builtin_ia32_cmpnltss:
Nate Begemane7722102008-04-14 04:49:57 +0000707 case X86::BI__builtin_ia32_cmpnltsd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000708 i = 5;
Nate Begemane7722102008-04-14 04:49:57 +0000709 name = "cmpntl";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000710 break;
711 case X86::BI__builtin_ia32_cmpnless:
Nate Begemane7722102008-04-14 04:49:57 +0000712 case X86::BI__builtin_ia32_cmpnlesd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000713 i = 6;
Nate Begemane7722102008-04-14 04:49:57 +0000714 name = "cmpnle";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000715 break;
716 case X86::BI__builtin_ia32_cmpordss:
Nate Begemane7722102008-04-14 04:49:57 +0000717 case X86::BI__builtin_ia32_cmpordsd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000718 i = 7;
Nate Begemane7722102008-04-14 04:49:57 +0000719 name = "cmpord";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000720 break;
721 }
722
Nate Begemane7722102008-04-14 04:49:57 +0000723 llvm::Function *F;
724 if (cast<llvm::VectorType>(Ops[0]->getType())->getElementType() ==
725 llvm::Type::FloatTy)
726 F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss);
727 else
728 F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd);
729
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000730 Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i));
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000731 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
732 }
Nate Begemane7722102008-04-14 04:49:57 +0000733 case X86::BI__builtin_ia32_ldmxcsr: {
734 llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
735 Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
736 Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +0000737 Builder.CreateStore(Ops[0], Tmp);
738 return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000739 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +0000740 }
741 case X86::BI__builtin_ia32_stmxcsr: {
742 llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
743 Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
744 Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +0000745 One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000746 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +0000747 return Builder.CreateLoad(Tmp, "stmxcsr");
748 }
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000749 case X86::BI__builtin_ia32_cmpordps:
Nate Begemane7722102008-04-14 04:49:57 +0000750 case X86::BI__builtin_ia32_cmpordpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000751 case X86::BI__builtin_ia32_cmpunordps:
Nate Begemane7722102008-04-14 04:49:57 +0000752 case X86::BI__builtin_ia32_cmpunordpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000753 case X86::BI__builtin_ia32_cmpeqps:
Nate Begemane7722102008-04-14 04:49:57 +0000754 case X86::BI__builtin_ia32_cmpeqpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000755 case X86::BI__builtin_ia32_cmpltps:
Nate Begemane7722102008-04-14 04:49:57 +0000756 case X86::BI__builtin_ia32_cmpltpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000757 case X86::BI__builtin_ia32_cmpleps:
Nate Begemane7722102008-04-14 04:49:57 +0000758 case X86::BI__builtin_ia32_cmplepd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000759 case X86::BI__builtin_ia32_cmpneqps:
Nate Begemane7722102008-04-14 04:49:57 +0000760 case X86::BI__builtin_ia32_cmpneqpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000761 case X86::BI__builtin_ia32_cmpngtps:
Nate Begemane7722102008-04-14 04:49:57 +0000762 case X86::BI__builtin_ia32_cmpngtpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000763 case X86::BI__builtin_ia32_cmpnltps:
Nate Begemane7722102008-04-14 04:49:57 +0000764 case X86::BI__builtin_ia32_cmpnltpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000765 case X86::BI__builtin_ia32_cmpgtps:
Nate Begemane7722102008-04-14 04:49:57 +0000766 case X86::BI__builtin_ia32_cmpgtpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000767 case X86::BI__builtin_ia32_cmpgeps:
Nate Begemane7722102008-04-14 04:49:57 +0000768 case X86::BI__builtin_ia32_cmpgepd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000769 case X86::BI__builtin_ia32_cmpngeps:
Nate Begemane7722102008-04-14 04:49:57 +0000770 case X86::BI__builtin_ia32_cmpngepd:
771 case X86::BI__builtin_ia32_cmpnleps:
772 case X86::BI__builtin_ia32_cmpnlepd: {
Chris Lattner7acda7c2007-12-18 00:25:38 +0000773 unsigned i = 0;
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000774 const char *name = 0;
775 bool ShouldSwap = false;
776 switch (BuiltinID) {
777 default: assert(0 && "Unknown compare builtin!");
Nate Begemane7722102008-04-14 04:49:57 +0000778 case X86::BI__builtin_ia32_cmpeqps:
779 case X86::BI__builtin_ia32_cmpeqpd: i = 0; name = "cmpeq"; break;
780 case X86::BI__builtin_ia32_cmpltps:
781 case X86::BI__builtin_ia32_cmpltpd: i = 1; name = "cmplt"; break;
782 case X86::BI__builtin_ia32_cmpleps:
783 case X86::BI__builtin_ia32_cmplepd: i = 2; name = "cmple"; break;
784 case X86::BI__builtin_ia32_cmpunordps:
785 case X86::BI__builtin_ia32_cmpunordpd: i = 3; name = "cmpunord"; break;
786 case X86::BI__builtin_ia32_cmpneqps:
787 case X86::BI__builtin_ia32_cmpneqpd: i = 4; name = "cmpneq"; break;
788 case X86::BI__builtin_ia32_cmpnltps:
789 case X86::BI__builtin_ia32_cmpnltpd: i = 5; name = "cmpntl"; break;
790 case X86::BI__builtin_ia32_cmpnleps:
791 case X86::BI__builtin_ia32_cmpnlepd: i = 6; name = "cmpnle"; break;
792 case X86::BI__builtin_ia32_cmpordps:
793 case X86::BI__builtin_ia32_cmpordpd: i = 7; name = "cmpord"; break;
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000794 case X86::BI__builtin_ia32_cmpgtps:
Nate Begemane7722102008-04-14 04:49:57 +0000795 case X86::BI__builtin_ia32_cmpgtpd:
Chris Lattner7acda7c2007-12-18 00:25:38 +0000796 ShouldSwap = true;
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000797 i = 1;
Nate Begemane7722102008-04-14 04:49:57 +0000798 name = "cmpgt";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000799 break;
800 case X86::BI__builtin_ia32_cmpgeps:
Nate Begemane7722102008-04-14 04:49:57 +0000801 case X86::BI__builtin_ia32_cmpgepd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000802 i = 2;
Nate Begemane7722102008-04-14 04:49:57 +0000803 name = "cmpge";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000804 ShouldSwap = true;
805 break;
806 case X86::BI__builtin_ia32_cmpngtps:
Nate Begemane7722102008-04-14 04:49:57 +0000807 case X86::BI__builtin_ia32_cmpngtpd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000808 i = 5;
Nate Begemane7722102008-04-14 04:49:57 +0000809 name = "cmpngt";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000810 ShouldSwap = true;
811 break;
812 case X86::BI__builtin_ia32_cmpngeps:
Nate Begemane7722102008-04-14 04:49:57 +0000813 case X86::BI__builtin_ia32_cmpngepd:
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000814 i = 6;
Nate Begemane7722102008-04-14 04:49:57 +0000815 name = "cmpnge";
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000816 ShouldSwap = true;
817 break;
818 }
819
820 if (ShouldSwap)
821 std::swap(Ops[0], Ops[1]);
Nate Begemane7722102008-04-14 04:49:57 +0000822
823 llvm::Function *F;
824 if (cast<llvm::VectorType>(Ops[0]->getType())->getElementType() ==
825 llvm::Type::FloatTy)
826 F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps);
827 else
828 F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd);
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000829
830 Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i));
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000831 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
832 }
833 case X86::BI__builtin_ia32_movss:
834 return EmitShuffleVector(Ops[0], Ops[1], 4, 1, 2, 3, "movss");
Torok Edwin730f2c12008-04-04 06:16:25 +0000835 case X86::BI__builtin_ia32_shufps: {
Chris Lattner7acda7c2007-12-18 00:25:38 +0000836 unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue();
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000837 return EmitShuffleVector(Ops[0], Ops[1],
838 i & 0x3, (i & 0xc) >> 2,
839 ((i & 0x30) >> 4) + 4,
Mon P Wang09b6bf52008-06-25 08:21:36 +0000840 ((i & 0xc0) >> 6) + 4, "shufps");
Torok Edwin730f2c12008-04-04 06:16:25 +0000841 }
Mon P Wang6c5df7f2008-07-18 00:14:09 +0000842 case X86::BI__builtin_ia32_shufpd: {
843 unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue();
844 return EmitShuffleVector(Ops[0], Ops[1], i & 1, (i & 2) + 2, "shufpd");
845 }
Nate Begemandbebb732008-04-03 19:58:06 +0000846 case X86::BI__builtin_ia32_punpcklbw128:
847 return EmitShuffleVector(Ops[0], Ops[1], 0, 16, 1, 17, 2, 18, 3, 19,
848 4, 20, 5, 21, 6, 22, 7, 23,
849 "punpcklbw");
850 case X86::BI__builtin_ia32_punpcklwd128:
851 return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11,
852 "punpcklwd");
Nate Begemane7722102008-04-14 04:49:57 +0000853 case X86::BI__builtin_ia32_movlhps:
854 return EmitShuffleVector(Ops[0], Ops[1], 0, 1, 4, 5, "movlhps");
855 case X86::BI__builtin_ia32_movhlps:
856 return EmitShuffleVector(Ops[0], Ops[1], 6, 7, 2, 3, "movhlps");
857 case X86::BI__builtin_ia32_unpckhps:
858 return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "unpckhps");
859 case X86::BI__builtin_ia32_unpcklps:
860 return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "unpcklps");
Nate Begemanabfaf992008-07-25 20:17:24 +0000861 case X86::BI__builtin_ia32_unpckhpd:
862 return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "unpckhpd");
863 case X86::BI__builtin_ia32_unpcklpd:
864 return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "unpcklpd");
865 case X86::BI__builtin_ia32_movsd:
866 return EmitShuffleVector(Ops[0], Ops[1], 2, 1, "movsd");
Nate Begemane7722102008-04-14 04:49:57 +0000867 case X86::BI__builtin_ia32_movqv4si: {
868 llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 2);
869 return Builder.CreateBitCast(Ops[0], Ty);
870 }
871 case X86::BI__builtin_ia32_loadlps:
872 case X86::BI__builtin_ia32_loadhps: {
873 // FIXME: This should probably be represented as
874 // shuffle (dst, (v4f32 (insert undef, (load i64), 0)), shuf mask hi/lo)
875 const llvm::Type *EltTy = llvm::Type::DoubleTy;
876 const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2);
877 const llvm::Type *OrigTy = Ops[0]->getType();
878 unsigned Index = BuiltinID == X86::BI__builtin_ia32_loadlps ? 0 : 1;
879 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index);
880 Ops[1] = Builder.CreateBitCast(Ops[1], llvm::PointerType::getUnqual(EltTy));
881 Ops[1] = Builder.CreateLoad(Ops[1], "tmp");
882 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
883 Ops[0] = Builder.CreateInsertElement(Ops[0], Ops[1], Idx, "loadps");
884 return Builder.CreateBitCast(Ops[0], OrigTy, "loadps");
885 }
886 case X86::BI__builtin_ia32_storehps:
887 case X86::BI__builtin_ia32_storelps: {
888 const llvm::Type *EltTy = llvm::Type::Int64Ty;
889 llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy);
890 llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2);
891
892 // cast val v2i64
893 Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast");
894
895 // extract (0, 1)
896 unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1;
897 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index);
898 Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract");
899
900 // cast pointer to i64 & store
901 Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy);
902 return Builder.CreateStore(Ops[1], Ops[0]);
903 }
904 case X86::BI__builtin_ia32_loadlv4si: {
905 // load i64
906 const llvm::Type *EltTy = llvm::Type::Int64Ty;
907 llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy);
908 Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy);
909 Ops[0] = Builder.CreateLoad(Ops[0], "load");
910
911 // scalar to vector: insert i64 into 2 x i64 undef
912 llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2);
913 llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
914 Ops[0] = Builder.CreateInsertElement(llvm::UndefValue::get(VecTy),
915 Ops[0], Zero, "s2v");
916
917 // shuffle into zero vector.
918 std::vector<llvm::Constant *>Elts;
919 Elts.resize(2, llvm::ConstantInt::get(EltTy, 0));
920 llvm::Value *ZV = ConstantVector::get(Elts);
921 Ops[0] = EmitShuffleVector(ZV, Ops[0], 2, 1, "loadl");
922
923 // bitcast to result.
924 return Builder.CreateBitCast(Ops[0],
925 llvm::VectorType::get(llvm::Type::Int32Ty, 4));
926 }
Nate Begeman24512502008-05-16 18:59:42 +0000927 case X86::BI__builtin_ia32_vec_set_v4hi:
928 case X86::BI__builtin_ia32_vec_set_v8hi:
929 return Builder.CreateInsertElement(Ops[0], Ops[1], Ops[2], "pinsrw");
Nate Begemane7722102008-04-14 04:49:57 +0000930 case X86::BI__builtin_ia32_andps:
931 case X86::BI__builtin_ia32_andpd:
932 case X86::BI__builtin_ia32_andnps:
933 case X86::BI__builtin_ia32_andnpd:
934 case X86::BI__builtin_ia32_orps:
935 case X86::BI__builtin_ia32_orpd:
936 case X86::BI__builtin_ia32_xorpd:
937 case X86::BI__builtin_ia32_xorps: {
938 const llvm::Type *ITy = llvm::VectorType::get(llvm::Type::Int32Ty, 4);
939 const llvm::Type *FTy = Ops[0]->getType();
940 Ops[0] = Builder.CreateBitCast(Ops[0], ITy, "bitcast");
941 Ops[1] = Builder.CreateBitCast(Ops[1], ITy, "bitcast");
942 switch (BuiltinID) {
943 case X86::BI__builtin_ia32_andps:
944 Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andps");
945 break;
946 case X86::BI__builtin_ia32_andpd:
947 Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andpd");
948 break;
949 case X86::BI__builtin_ia32_andnps:
950 Ops[0] = Builder.CreateNot(Ops[0], "not");
951 Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andnps");
952 break;
953 case X86::BI__builtin_ia32_andnpd:
954 Ops[0] = Builder.CreateNot(Ops[0], "not");
955 Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andnpd");
956 break;
957 case X86::BI__builtin_ia32_orps:
958 Ops[0] = Builder.CreateOr(Ops[0], Ops[1], "orps");
959 break;
960 case X86::BI__builtin_ia32_orpd:
961 Ops[0] = Builder.CreateOr(Ops[0], Ops[1], "orpd");
962 break;
963 case X86::BI__builtin_ia32_xorps:
964 Ops[0] = Builder.CreateXor(Ops[0], Ops[1], "xorps");
965 break;
966 case X86::BI__builtin_ia32_xorpd:
967 Ops[0] = Builder.CreateXor(Ops[0], Ops[1], "xorpd");
968 break;
969 }
970 return Builder.CreateBitCast(Ops[0], FTy, "bitcast");
971 }
Anders Carlsson564f1de2007-12-09 23:17:02 +0000972 }
973}
974
Chris Lattner1feedd82007-12-13 07:34:23 +0000975Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
976 const CallExpr *E) {
Anders Carlsson564f1de2007-12-09 23:17:02 +0000977 switch (BuiltinID) {
Anders Carlsson46a26b02007-12-09 23:39:18 +0000978 default: return 0;
Anders Carlsson564f1de2007-12-09 23:17:02 +0000979 }
980}