blob: f3eef1f4ae29b5fa32f54c31e73e994772d0226d [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//
Chris Lattner959e5be2007-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 Carlsson49865302007-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 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))
Nate Begeman33a50432008-01-17 18:06:09 +000032 return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID),
Eli Friedman261f4ad2008-01-30 01:32:06 +000033 E->getCallee()->getType(), E->arg_begin(),
34 E->getNumArgs());
Anders Carlsson6f6c6482007-12-09 21:20:04 +000035
36 // See if we have a target specific intrinsic.
Chris Lattner9d37e132007-12-13 07:34:23 +000037 Intrinsic::ID IntrinsicID;
Anders Carlsson6f6c6482007-12-09 21:20:04 +000038 const char *TargetPrefix = Target.getTargetPrefix();
39 const char *BuiltinName = getContext().BuiltinInfo.GetName(BuiltinID);
40#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
41#include "llvm/Intrinsics.gen"
42#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
43
44 if (IntrinsicID != Intrinsic::not_intrinsic) {
Chris Lattner9d37e132007-12-13 07:34:23 +000045 SmallVector<Value*, 16> Args;
Anders Carlsson6f6c6482007-12-09 21:20:04 +000046
Chris Lattner4b23f942007-12-18 00:25:38 +000047 Function *F = CGM.getIntrinsic(IntrinsicID);
Anders Carlsson6f6c6482007-12-09 21:20:04 +000048 const llvm::FunctionType *FTy = F->getFunctionType();
49
50 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattner9d37e132007-12-13 07:34:23 +000051 Value *ArgValue = EmitScalarExpr(E->getArg(i));
Anders Carlsson6f6c6482007-12-09 21:20:04 +000052
53 // If the intrinsic arg type is different from the builtin arg type
54 // we need to do a bit cast.
55 const llvm::Type *PTy = FTy->getParamType(i);
56 if (PTy != ArgValue->getType()) {
57 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
58 "Must be able to losslessly bit cast to param");
59 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
60 }
61
62 Args.push_back(ArgValue);
63 }
64
Chris Lattner9d37e132007-12-13 07:34:23 +000065 Value *V = Builder.CreateCall(F, &Args[0], &Args[0] + Args.size());
Anders Carlsson6f6c6482007-12-09 21:20:04 +000066 QualType BuiltinRetType = E->getType();
67
Chris Lattner9d37e132007-12-13 07:34:23 +000068 const llvm::Type *RetTy = llvm::Type::VoidTy;
69 if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType);
Anders Carlsson6f6c6482007-12-09 21:20:04 +000070
71 if (RetTy != V->getType()) {
72 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
73 "Must be able to losslessly bit cast result type");
Anders Carlsson6f6c6482007-12-09 21:20:04 +000074 V = Builder.CreateBitCast(V, RetTy);
75 }
76
77 return RValue::get(V);
78 }
Anders Carlssone1449c12007-12-09 23:17:02 +000079
80 // See if we have a target specific builtin that needs to be lowered.
Chris Lattner9d37e132007-12-13 07:34:23 +000081 Value *V = 0;
Chris Lattner02c60f52007-08-31 04:44:06 +000082
Anders Carlssone1449c12007-12-09 23:17:02 +000083 if (strcmp(TargetPrefix, "x86") == 0)
84 V = EmitX86BuiltinExpr(BuiltinID, E);
85 else if (strcmp(TargetPrefix, "ppc") == 0)
86 V = EmitPPCBuiltinExpr(BuiltinID, E);
87
88 if (V)
89 return RValue::get(V);
90
Chris Lattnere8f49632007-12-02 01:49:16 +000091 WarnUnsupported(E, "builtin function");
Chris Lattner783a78f2007-08-26 04:17:05 +000092
93 // Unknown builtin, for now just dump it out and return undef.
94 if (hasAggregateLLVMType(E->getType()))
95 return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType())));
Chris Lattner9d37e132007-12-13 07:34:23 +000096 return RValue::get(UndefValue::get(ConvertType(E->getType())));
Anders Carlsson6f6c6482007-12-09 21:20:04 +000097 }
Chris Lattner783a78f2007-08-26 04:17:05 +000098 case Builtin::BI__builtin___CFStringMakeConstantString: {
99 const Expr *Arg = E->getArg(0);
100
Anders Carlsson783e1432007-11-01 00:39:26 +0000101 while (1) {
102 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Arg))
103 Arg = PE->getSubExpr();
104 else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Arg))
105 Arg = CE->getSubExpr();
106 else
107 break;
108 }
Chris Lattner783a78f2007-08-26 04:17:05 +0000109
110 const StringLiteral *Literal = cast<StringLiteral>(Arg);
111 std::string S(Literal->getStrData(), Literal->getByteLength());
112
113 return RValue::get(CGM.GetAddrOfConstantCFString(S));
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000114 }
115 case Builtin::BI__builtin_va_start:
116 case Builtin::BI__builtin_va_end: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000117 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000118 const llvm::Type *DestType =
119 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000120 if (ArgValue->getType() != DestType)
121 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
122 ArgValue->getNameStart());
123
Chris Lattner9d37e132007-12-13 07:34:23 +0000124 Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_start) ?
125 Intrinsic::vastart : Intrinsic::vaend;
Chris Lattner4b23f942007-12-18 00:25:38 +0000126 return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue));
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000127 }
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 Lattner4b23f942007-12-18 00:25:38 +0000158 Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
Chris Lattner9d37e132007-12-13 07:34:23 +0000159 return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
Anders Carlsson56118002007-12-02 21:58:10 +0000160 }
Anders Carlssona8897f02007-12-05 07:22:48 +0000161 case Builtin::BI__builtin_inff: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000162 APFloat f(APFloat::IEEEsingle, APFloat::fcInfinity, false);
163 return RValue::get(ConstantFP::get(llvm::Type::FloatTy, f));
Anders Carlssona8897f02007-12-05 07:22:48 +0000164 }
Anders Carlsson8fc5f602008-02-01 16:06:34 +0000165 case Builtin::BI__builtin_huge_val:
Anders Carlssona8897f02007-12-05 07:22:48 +0000166 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 }
Chris Lattnerd1f26b32007-12-20 00:44:32 +0000172 case Builtin::BI__builtin_isgreater:
173 case Builtin::BI__builtin_isgreaterequal:
174 case Builtin::BI__builtin_isless:
175 case Builtin::BI__builtin_islessequal:
176 case Builtin::BI__builtin_islessgreater:
177 case Builtin::BI__builtin_isunordered: {
178 // Ordered comparisons: we know the arguments to these are matching scalar
179 // floating point values.
180 Value *LHS = EmitScalarExpr(E->getArg(0));
181 Value *RHS = EmitScalarExpr(E->getArg(1));
182
183 switch (BuiltinID) {
184 default: assert(0 && "Unknown ordered comparison");
185 case Builtin::BI__builtin_isgreater:
186 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
187 break;
188 case Builtin::BI__builtin_isgreaterequal:
189 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
190 break;
191 case Builtin::BI__builtin_isless:
192 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
193 break;
194 case Builtin::BI__builtin_islessequal:
195 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
196 break;
197 case Builtin::BI__builtin_islessgreater:
198 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
199 break;
200 case Builtin::BI__builtin_isunordered:
201 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
202 break;
203 }
204 // ZExt bool to int type.
205 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()),
206 "tmp"));
207 }
Anders Carlsson40438402007-10-29 02:59:40 +0000208 }
Anders Carlsson49865302007-08-20 18:05:56 +0000209 return RValue::get(0);
210}
Anders Carlssone1449c12007-12-09 23:17:02 +0000211
Chris Lattner9d37e132007-12-13 07:34:23 +0000212Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
213 const CallExpr *E) {
Anders Carlsson04c4b752007-12-14 17:48:24 +0000214
215 llvm::SmallVector<Value*, 4> Ops;
216
217 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
218 Ops.push_back(EmitScalarExpr(E->getArg(i)));
219
Anders Carlssone1449c12007-12-09 23:17:02 +0000220 switch (BuiltinID) {
Anders Carlsson0a35a812007-12-09 23:39:18 +0000221 default: return 0;
222 case X86::BI__builtin_ia32_mulps:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000223 return Builder.CreateMul(Ops[0], Ops[1], "mulps");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000224 case X86::BI__builtin_ia32_pand:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000225 return Builder.CreateAnd(Ops[0], Ops[1], "pand");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000226 case X86::BI__builtin_ia32_por:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000227 return Builder.CreateAnd(Ops[0], Ops[1], "por");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000228 case X86::BI__builtin_ia32_pxor:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000229 return Builder.CreateAnd(Ops[0], Ops[1], "pxor");
Chris Lattner70376292007-12-11 04:33:00 +0000230 case X86::BI__builtin_ia32_pandn: {
Anders Carlsson04c4b752007-12-14 17:48:24 +0000231 Ops[0] = Builder.CreateNot(Ops[0], "tmp");
232 return Builder.CreateAnd(Ops[0], Ops[1], "pandn");
Chris Lattner70376292007-12-11 04:33:00 +0000233 }
Anders Carlsson17b80c12007-12-11 02:25:54 +0000234 case X86::BI__builtin_ia32_paddb:
235 case X86::BI__builtin_ia32_paddd:
236 case X86::BI__builtin_ia32_paddq:
237 case X86::BI__builtin_ia32_paddw:
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000238 case X86::BI__builtin_ia32_addps:
239 return Builder.CreateAdd(Ops[0], Ops[1], "add");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000240 case X86::BI__builtin_ia32_psubb:
241 case X86::BI__builtin_ia32_psubd:
242 case X86::BI__builtin_ia32_psubq:
243 case X86::BI__builtin_ia32_psubw:
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000244 case X86::BI__builtin_ia32_subps:
245 return Builder.CreateSub(Ops[0], Ops[1], "sub");
246 case X86::BI__builtin_ia32_divps:
247 return Builder.CreateFDiv(Ops[0], Ops[1], "divps");
Anders Carlsson820cb742007-12-11 03:09:22 +0000248 case X86::BI__builtin_ia32_pmullw:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000249 return Builder.CreateMul(Ops[0], Ops[1], "pmul");
Anders Carlsson820cb742007-12-11 03:09:22 +0000250 case X86::BI__builtin_ia32_punpckhbw:
Chris Lattner4b23f942007-12-18 00:25:38 +0000251 return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15,
Anders Carlsson820cb742007-12-11 03:09:22 +0000252 "punpckhbw");
253 case X86::BI__builtin_ia32_punpckhwd:
Chris Lattner4b23f942007-12-18 00:25:38 +0000254 return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhwd");
Anders Carlsson820cb742007-12-11 03:09:22 +0000255 case X86::BI__builtin_ia32_punpckhdq:
Chris Lattner4b23f942007-12-18 00:25:38 +0000256 return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhdq");
Anders Carlsson820cb742007-12-11 03:09:22 +0000257 case X86::BI__builtin_ia32_punpcklbw:
Chris Lattner4b23f942007-12-18 00:25:38 +0000258 return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11,
Anders Carlsson820cb742007-12-11 03:09:22 +0000259 "punpcklbw");
260 case X86::BI__builtin_ia32_punpcklwd:
Chris Lattner4b23f942007-12-18 00:25:38 +0000261 return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpcklwd");
Anders Carlsson820cb742007-12-11 03:09:22 +0000262 case X86::BI__builtin_ia32_punpckldq:
Chris Lattner4b23f942007-12-18 00:25:38 +0000263 return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpckldq");
Anders Carlsson04c4b752007-12-14 17:48:24 +0000264 case X86::BI__builtin_ia32_pslldi:
265 case X86::BI__builtin_ia32_psllqi:
266 case X86::BI__builtin_ia32_psllwi:
267 case X86::BI__builtin_ia32_psradi:
268 case X86::BI__builtin_ia32_psrawi:
269 case X86::BI__builtin_ia32_psrldi:
270 case X86::BI__builtin_ia32_psrlqi:
271 case X86::BI__builtin_ia32_psrlwi: {
272 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext");
273 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1);
274 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson04c4b752007-12-14 17:48:24 +0000275 const char *name = 0;
276 Intrinsic::ID ID = Intrinsic::not_intrinsic;
277
278 switch (BuiltinID) {
279 default: assert(0 && "Unsupported shift intrinsic!");
280 case X86::BI__builtin_ia32_pslldi:
281 name = "pslldi";
282 ID = Intrinsic::x86_mmx_psll_d;
283 break;
284 case X86::BI__builtin_ia32_psllqi:
285 name = "psllqi";
286 ID = Intrinsic::x86_mmx_psll_q;
287 break;
288 case X86::BI__builtin_ia32_psllwi:
289 name = "psllwi";
290 ID = Intrinsic::x86_mmx_psll_w;
291 break;
292 case X86::BI__builtin_ia32_psradi:
293 name = "psradi";
294 ID = Intrinsic::x86_mmx_psra_d;
295 break;
296 case X86::BI__builtin_ia32_psrawi:
297 name = "psrawi";
298 ID = Intrinsic::x86_mmx_psra_w;
299 break;
300 case X86::BI__builtin_ia32_psrldi:
301 name = "psrldi";
302 ID = Intrinsic::x86_mmx_psrl_d;
303 break;
304 case X86::BI__builtin_ia32_psrlqi:
305 name = "psrlqi";
306 ID = Intrinsic::x86_mmx_psrl_q;
307 break;
308 case X86::BI__builtin_ia32_psrlwi:
309 name = "psrlwi";
310 ID = Intrinsic::x86_mmx_psrl_w;
311 break;
312 }
Chris Lattner4b23f942007-12-18 00:25:38 +0000313 llvm::Function *F = CGM.getIntrinsic(ID);
Anders Carlsson04c4b752007-12-14 17:48:24 +0000314 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
315 }
316 case X86::BI__builtin_ia32_pshufd: {
Chris Lattner4b23f942007-12-18 00:25:38 +0000317 unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue();
Anders Carlsson04c4b752007-12-14 17:48:24 +0000318 return EmitShuffleVector(Ops[0], Ops[0],
Anders Carlsson17b80c12007-12-11 02:25:54 +0000319 i & 0x3, (i & 0xc) >> 2,
320 (i & 0x30) >> 4, (i & 0xc0) >> 6,
321 "pshufd");
322 }
Anders Carlsson68b8be92007-12-15 21:23:30 +0000323 case X86::BI__builtin_ia32_vec_init_v4hi:
324 case X86::BI__builtin_ia32_vec_init_v8qi:
325 case X86::BI__builtin_ia32_vec_init_v2si:
326 return EmitVector(&Ops[0], Ops.size());
327 case X86::BI__builtin_ia32_vec_ext_v2si:
328 return Builder.CreateExtractElement(Ops[0], Ops[1], "result");
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000329 case X86::BI__builtin_ia32_cmpordss:
330 case X86::BI__builtin_ia32_cmpunordss:
331 case X86::BI__builtin_ia32_cmpeqss:
332 case X86::BI__builtin_ia32_cmpltss:
333 case X86::BI__builtin_ia32_cmpless:
334 case X86::BI__builtin_ia32_cmpneqss:
335 case X86::BI__builtin_ia32_cmpnltss:
336 case X86::BI__builtin_ia32_cmpnless: {
Chris Lattner4b23f942007-12-18 00:25:38 +0000337 unsigned i = 0;
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000338 const char *name = 0;
339 switch (BuiltinID) {
340 default: assert(0 && "Unknown compare builtin!");
341 case X86::BI__builtin_ia32_cmpeqss:
342 i = 0;
343 name = "cmpeqss";
344 break;
345 case X86::BI__builtin_ia32_cmpltss:
346 i = 1;
347 name = "cmpltss";
348 break;
349 case X86::BI__builtin_ia32_cmpless:
350 i = 2;
351 name = "cmpless";
352 break;
353 case X86::BI__builtin_ia32_cmpunordss:
354 i = 3;
355 name = "cmpunordss";
356 break;
357 case X86::BI__builtin_ia32_cmpneqss:
358 i = 4;
359 name = "cmpneqss";
360 break;
361 case X86::BI__builtin_ia32_cmpnltss:
362 i = 5;
363 name = "cmpntlss";
364 break;
365 case X86::BI__builtin_ia32_cmpnless:
366 i = 6;
367 name = "cmpnless";
368 break;
369 case X86::BI__builtin_ia32_cmpordss:
370 i = 7;
371 name = "cmpordss";
372 break;
373 }
374
375 Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i));
Chris Lattner4b23f942007-12-18 00:25:38 +0000376 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss);
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000377 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
378 }
379 case X86::BI__builtin_ia32_cmpordps:
380 case X86::BI__builtin_ia32_cmpunordps:
381 case X86::BI__builtin_ia32_cmpeqps:
382 case X86::BI__builtin_ia32_cmpltps:
383 case X86::BI__builtin_ia32_cmpleps:
384 case X86::BI__builtin_ia32_cmpneqps:
385 case X86::BI__builtin_ia32_cmpngtps:
386 case X86::BI__builtin_ia32_cmpnltps:
387 case X86::BI__builtin_ia32_cmpgtps:
388 case X86::BI__builtin_ia32_cmpgeps:
389 case X86::BI__builtin_ia32_cmpngeps:
390 case X86::BI__builtin_ia32_cmpnleps: {
Chris Lattner4b23f942007-12-18 00:25:38 +0000391 unsigned i = 0;
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000392 const char *name = 0;
393 bool ShouldSwap = false;
394 switch (BuiltinID) {
395 default: assert(0 && "Unknown compare builtin!");
Chris Lattner4b23f942007-12-18 00:25:38 +0000396 case X86::BI__builtin_ia32_cmpeqps: i = 0; name = "cmpeqps"; break;
397 case X86::BI__builtin_ia32_cmpltps: i = 1; name = "cmpltps"; break;
398 case X86::BI__builtin_ia32_cmpleps: i = 2; name = "cmpleps"; break;
399 case X86::BI__builtin_ia32_cmpunordps: i = 3; name = "cmpunordps"; break;
400 case X86::BI__builtin_ia32_cmpneqps: i = 4; name = "cmpneqps"; break;
401 case X86::BI__builtin_ia32_cmpnltps: i = 5; name = "cmpntlps"; break;
402 case X86::BI__builtin_ia32_cmpnleps: i = 6; name = "cmpnleps"; break;
403 case X86::BI__builtin_ia32_cmpordps: i = 7; name = "cmpordps"; break;
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000404 case X86::BI__builtin_ia32_cmpgtps:
Chris Lattner4b23f942007-12-18 00:25:38 +0000405 ShouldSwap = true;
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000406 i = 1;
407 name = "cmpgtps";
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000408 break;
409 case X86::BI__builtin_ia32_cmpgeps:
410 i = 2;
411 name = "cmpgeps";
412 ShouldSwap = true;
413 break;
414 case X86::BI__builtin_ia32_cmpngtps:
415 i = 5;
416 name = "cmpngtps";
417 ShouldSwap = true;
418 break;
419 case X86::BI__builtin_ia32_cmpngeps:
420 i = 6;
421 name = "cmpngeps";
422 ShouldSwap = true;
423 break;
424 }
425
426 if (ShouldSwap)
427 std::swap(Ops[0], Ops[1]);
428
429 Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i));
Chris Lattner4b23f942007-12-18 00:25:38 +0000430 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps);
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000431 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
432 }
433 case X86::BI__builtin_ia32_movss:
434 return EmitShuffleVector(Ops[0], Ops[1], 4, 1, 2, 3, "movss");
435 case X86::BI__builtin_ia32_shufps:
Chris Lattner4b23f942007-12-18 00:25:38 +0000436 unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue();
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000437 return EmitShuffleVector(Ops[0], Ops[1],
438 i & 0x3, (i & 0xc) >> 2,
439 ((i & 0x30) >> 4) + 4,
440 ((i & 0x60) >> 6) + 4, "shufps");
Anders Carlssone1449c12007-12-09 23:17:02 +0000441 }
442}
443
Chris Lattner9d37e132007-12-13 07:34:23 +0000444Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
445 const CallExpr *E) {
Anders Carlssone1449c12007-12-09 23:17:02 +0000446 switch (BuiltinID) {
Anders Carlsson0a35a812007-12-09 23:39:18 +0000447 default: return 0;
Anders Carlssone1449c12007-12-09 23:17:02 +0000448 }
449}