blob: ec6c2b968c08a23fbd5b166927f5f333b09917c8 [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 Lattner4b23f942007-12-18 00:25:38 +000045 Function *F = CGM.getIntrinsic(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));
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000116 const llvm::Type *DestType =
117 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000118 if (ArgValue->getType() != DestType)
119 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
120 ArgValue->getNameStart());
121
Chris Lattner9d37e132007-12-13 07:34:23 +0000122 Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_start) ?
123 Intrinsic::vastart : Intrinsic::vaend;
Chris Lattner4b23f942007-12-18 00:25:38 +0000124 return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue));
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000125 }
Anders Carlsson40438402007-10-29 02:59:40 +0000126 case Builtin::BI__builtin_classify_type: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000127 APSInt Result(32);
Anders Carlsson40438402007-10-29 02:59:40 +0000128 if (!E->isBuiltinClassifyType(Result))
129 assert(0 && "Expr not __builtin_classify_type!");
Chris Lattner9d37e132007-12-13 07:34:23 +0000130 return RValue::get(ConstantInt::get(Result));
Anders Carlsson49865302007-08-20 18:05:56 +0000131 }
Anders Carlsson783e1432007-11-01 00:39:26 +0000132 case Builtin::BI__builtin_constant_p: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000133 APSInt Result(32);
Anders Carlsson783e1432007-11-01 00:39:26 +0000134 // FIXME: Analyze the parameter and check if it is a constant.
135 Result = 0;
Chris Lattner9d37e132007-12-13 07:34:23 +0000136 return RValue::get(ConstantInt::get(Result));
Anders Carlsson783e1432007-11-01 00:39:26 +0000137 }
Anders Carlssone79dbe22007-11-20 19:05:17 +0000138 case Builtin::BI__builtin_abs: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000139 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssone79dbe22007-11-20 19:05:17 +0000140
141 llvm::BinaryOperator *NegOp =
142 Builder.CreateNeg(ArgValue, (ArgValue->getName() + "neg").c_str());
Chris Lattner9d37e132007-12-13 07:34:23 +0000143 Value *CmpResult =
Anders Carlssone79dbe22007-11-20 19:05:17 +0000144 Builder.CreateICmpSGE(ArgValue, NegOp->getOperand(0), "abscond");
Chris Lattner9d37e132007-12-13 07:34:23 +0000145 Value *Result =
Anders Carlssone79dbe22007-11-20 19:05:17 +0000146 Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
147
148 return RValue::get(Result);
149 }
Chris Lattner9d37e132007-12-13 07:34:23 +0000150 case Builtin::BI__builtin_expect:
151 return RValue::get(EmitScalarExpr(E->getArg(0)));
Anders Carlsson56118002007-12-02 21:58:10 +0000152 case Builtin::BI__builtin_bswap32:
153 case Builtin::BI__builtin_bswap64: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000154 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlsson56118002007-12-02 21:58:10 +0000155 const llvm::Type *ArgType = ArgValue->getType();
Chris Lattner4b23f942007-12-18 00:25:38 +0000156 Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
Chris Lattner9d37e132007-12-13 07:34:23 +0000157 return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
Anders Carlsson56118002007-12-02 21:58:10 +0000158 }
Anders Carlssona8897f02007-12-05 07:22:48 +0000159 case Builtin::BI__builtin_inff: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000160 APFloat f(APFloat::IEEEsingle, APFloat::fcInfinity, false);
161 return RValue::get(ConstantFP::get(llvm::Type::FloatTy, f));
Anders Carlssona8897f02007-12-05 07:22:48 +0000162 }
163 case Builtin::BI__builtin_inf:
164 // FIXME: mapping long double onto double.
165 case Builtin::BI__builtin_infl: {
Chris Lattner9d37e132007-12-13 07:34:23 +0000166 APFloat f(APFloat::IEEEdouble, APFloat::fcInfinity, false);
167 return RValue::get(ConstantFP::get(llvm::Type::DoubleTy, f));
Anders Carlssona8897f02007-12-05 07:22:48 +0000168 }
Chris Lattnerd1f26b32007-12-20 00:44:32 +0000169 case Builtin::BI__builtin_isgreater:
170 case Builtin::BI__builtin_isgreaterequal:
171 case Builtin::BI__builtin_isless:
172 case Builtin::BI__builtin_islessequal:
173 case Builtin::BI__builtin_islessgreater:
174 case Builtin::BI__builtin_isunordered: {
175 // Ordered comparisons: we know the arguments to these are matching scalar
176 // floating point values.
177 Value *LHS = EmitScalarExpr(E->getArg(0));
178 Value *RHS = EmitScalarExpr(E->getArg(1));
179
180 switch (BuiltinID) {
181 default: assert(0 && "Unknown ordered comparison");
182 case Builtin::BI__builtin_isgreater:
183 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
184 break;
185 case Builtin::BI__builtin_isgreaterequal:
186 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
187 break;
188 case Builtin::BI__builtin_isless:
189 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
190 break;
191 case Builtin::BI__builtin_islessequal:
192 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
193 break;
194 case Builtin::BI__builtin_islessgreater:
195 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
196 break;
197 case Builtin::BI__builtin_isunordered:
198 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
199 break;
200 }
201 // ZExt bool to int type.
202 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()),
203 "tmp"));
204 }
Anders Carlsson40438402007-10-29 02:59:40 +0000205 }
Anders Carlsson49865302007-08-20 18:05:56 +0000206 return RValue::get(0);
207}
Anders Carlssone1449c12007-12-09 23:17:02 +0000208
Chris Lattner9d37e132007-12-13 07:34:23 +0000209Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
210 const CallExpr *E) {
Anders Carlsson04c4b752007-12-14 17:48:24 +0000211
212 llvm::SmallVector<Value*, 4> Ops;
213
214 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
215 Ops.push_back(EmitScalarExpr(E->getArg(i)));
216
Anders Carlssone1449c12007-12-09 23:17:02 +0000217 switch (BuiltinID) {
Anders Carlsson0a35a812007-12-09 23:39:18 +0000218 default: return 0;
219 case X86::BI__builtin_ia32_mulps:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000220 return Builder.CreateMul(Ops[0], Ops[1], "mulps");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000221 case X86::BI__builtin_ia32_pand:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000222 return Builder.CreateAnd(Ops[0], Ops[1], "pand");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000223 case X86::BI__builtin_ia32_por:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000224 return Builder.CreateAnd(Ops[0], Ops[1], "por");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000225 case X86::BI__builtin_ia32_pxor:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000226 return Builder.CreateAnd(Ops[0], Ops[1], "pxor");
Chris Lattner70376292007-12-11 04:33:00 +0000227 case X86::BI__builtin_ia32_pandn: {
Anders Carlsson04c4b752007-12-14 17:48:24 +0000228 Ops[0] = Builder.CreateNot(Ops[0], "tmp");
229 return Builder.CreateAnd(Ops[0], Ops[1], "pandn");
Chris Lattner70376292007-12-11 04:33:00 +0000230 }
Anders Carlsson17b80c12007-12-11 02:25:54 +0000231 case X86::BI__builtin_ia32_paddb:
232 case X86::BI__builtin_ia32_paddd:
233 case X86::BI__builtin_ia32_paddq:
234 case X86::BI__builtin_ia32_paddw:
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000235 case X86::BI__builtin_ia32_addps:
236 return Builder.CreateAdd(Ops[0], Ops[1], "add");
Anders Carlsson17b80c12007-12-11 02:25:54 +0000237 case X86::BI__builtin_ia32_psubb:
238 case X86::BI__builtin_ia32_psubd:
239 case X86::BI__builtin_ia32_psubq:
240 case X86::BI__builtin_ia32_psubw:
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000241 case X86::BI__builtin_ia32_subps:
242 return Builder.CreateSub(Ops[0], Ops[1], "sub");
243 case X86::BI__builtin_ia32_divps:
244 return Builder.CreateFDiv(Ops[0], Ops[1], "divps");
Anders Carlsson820cb742007-12-11 03:09:22 +0000245 case X86::BI__builtin_ia32_pmullw:
Anders Carlsson04c4b752007-12-14 17:48:24 +0000246 return Builder.CreateMul(Ops[0], Ops[1], "pmul");
Anders Carlsson820cb742007-12-11 03:09:22 +0000247 case X86::BI__builtin_ia32_punpckhbw:
Chris Lattner4b23f942007-12-18 00:25:38 +0000248 return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15,
Anders Carlsson820cb742007-12-11 03:09:22 +0000249 "punpckhbw");
250 case X86::BI__builtin_ia32_punpckhwd:
Chris Lattner4b23f942007-12-18 00:25:38 +0000251 return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhwd");
Anders Carlsson820cb742007-12-11 03:09:22 +0000252 case X86::BI__builtin_ia32_punpckhdq:
Chris Lattner4b23f942007-12-18 00:25:38 +0000253 return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhdq");
Anders Carlsson820cb742007-12-11 03:09:22 +0000254 case X86::BI__builtin_ia32_punpcklbw:
Chris Lattner4b23f942007-12-18 00:25:38 +0000255 return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11,
Anders Carlsson820cb742007-12-11 03:09:22 +0000256 "punpcklbw");
257 case X86::BI__builtin_ia32_punpcklwd:
Chris Lattner4b23f942007-12-18 00:25:38 +0000258 return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpcklwd");
Anders Carlsson820cb742007-12-11 03:09:22 +0000259 case X86::BI__builtin_ia32_punpckldq:
Chris Lattner4b23f942007-12-18 00:25:38 +0000260 return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpckldq");
Anders Carlsson04c4b752007-12-14 17:48:24 +0000261 case X86::BI__builtin_ia32_pslldi:
262 case X86::BI__builtin_ia32_psllqi:
263 case X86::BI__builtin_ia32_psllwi:
264 case X86::BI__builtin_ia32_psradi:
265 case X86::BI__builtin_ia32_psrawi:
266 case X86::BI__builtin_ia32_psrldi:
267 case X86::BI__builtin_ia32_psrlqi:
268 case X86::BI__builtin_ia32_psrlwi: {
269 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext");
270 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1);
271 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson04c4b752007-12-14 17:48:24 +0000272 const char *name = 0;
273 Intrinsic::ID ID = Intrinsic::not_intrinsic;
274
275 switch (BuiltinID) {
276 default: assert(0 && "Unsupported shift intrinsic!");
277 case X86::BI__builtin_ia32_pslldi:
278 name = "pslldi";
279 ID = Intrinsic::x86_mmx_psll_d;
280 break;
281 case X86::BI__builtin_ia32_psllqi:
282 name = "psllqi";
283 ID = Intrinsic::x86_mmx_psll_q;
284 break;
285 case X86::BI__builtin_ia32_psllwi:
286 name = "psllwi";
287 ID = Intrinsic::x86_mmx_psll_w;
288 break;
289 case X86::BI__builtin_ia32_psradi:
290 name = "psradi";
291 ID = Intrinsic::x86_mmx_psra_d;
292 break;
293 case X86::BI__builtin_ia32_psrawi:
294 name = "psrawi";
295 ID = Intrinsic::x86_mmx_psra_w;
296 break;
297 case X86::BI__builtin_ia32_psrldi:
298 name = "psrldi";
299 ID = Intrinsic::x86_mmx_psrl_d;
300 break;
301 case X86::BI__builtin_ia32_psrlqi:
302 name = "psrlqi";
303 ID = Intrinsic::x86_mmx_psrl_q;
304 break;
305 case X86::BI__builtin_ia32_psrlwi:
306 name = "psrlwi";
307 ID = Intrinsic::x86_mmx_psrl_w;
308 break;
309 }
Chris Lattner4b23f942007-12-18 00:25:38 +0000310 llvm::Function *F = CGM.getIntrinsic(ID);
Anders Carlsson04c4b752007-12-14 17:48:24 +0000311 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
312 }
313 case X86::BI__builtin_ia32_pshufd: {
Chris Lattner4b23f942007-12-18 00:25:38 +0000314 unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue();
Anders Carlsson04c4b752007-12-14 17:48:24 +0000315 return EmitShuffleVector(Ops[0], Ops[0],
Anders Carlsson17b80c12007-12-11 02:25:54 +0000316 i & 0x3, (i & 0xc) >> 2,
317 (i & 0x30) >> 4, (i & 0xc0) >> 6,
318 "pshufd");
319 }
Anders Carlsson68b8be92007-12-15 21:23:30 +0000320 case X86::BI__builtin_ia32_vec_init_v4hi:
321 case X86::BI__builtin_ia32_vec_init_v8qi:
322 case X86::BI__builtin_ia32_vec_init_v2si:
323 return EmitVector(&Ops[0], Ops.size());
324 case X86::BI__builtin_ia32_vec_ext_v2si:
325 return Builder.CreateExtractElement(Ops[0], Ops[1], "result");
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000326 case X86::BI__builtin_ia32_cmpordss:
327 case X86::BI__builtin_ia32_cmpunordss:
328 case X86::BI__builtin_ia32_cmpeqss:
329 case X86::BI__builtin_ia32_cmpltss:
330 case X86::BI__builtin_ia32_cmpless:
331 case X86::BI__builtin_ia32_cmpneqss:
332 case X86::BI__builtin_ia32_cmpnltss:
333 case X86::BI__builtin_ia32_cmpnless: {
Chris Lattner4b23f942007-12-18 00:25:38 +0000334 unsigned i = 0;
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000335 const char *name = 0;
336 switch (BuiltinID) {
337 default: assert(0 && "Unknown compare builtin!");
338 case X86::BI__builtin_ia32_cmpeqss:
339 i = 0;
340 name = "cmpeqss";
341 break;
342 case X86::BI__builtin_ia32_cmpltss:
343 i = 1;
344 name = "cmpltss";
345 break;
346 case X86::BI__builtin_ia32_cmpless:
347 i = 2;
348 name = "cmpless";
349 break;
350 case X86::BI__builtin_ia32_cmpunordss:
351 i = 3;
352 name = "cmpunordss";
353 break;
354 case X86::BI__builtin_ia32_cmpneqss:
355 i = 4;
356 name = "cmpneqss";
357 break;
358 case X86::BI__builtin_ia32_cmpnltss:
359 i = 5;
360 name = "cmpntlss";
361 break;
362 case X86::BI__builtin_ia32_cmpnless:
363 i = 6;
364 name = "cmpnless";
365 break;
366 case X86::BI__builtin_ia32_cmpordss:
367 i = 7;
368 name = "cmpordss";
369 break;
370 }
371
372 Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i));
Chris Lattner4b23f942007-12-18 00:25:38 +0000373 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss);
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000374 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
375 }
376 case X86::BI__builtin_ia32_cmpordps:
377 case X86::BI__builtin_ia32_cmpunordps:
378 case X86::BI__builtin_ia32_cmpeqps:
379 case X86::BI__builtin_ia32_cmpltps:
380 case X86::BI__builtin_ia32_cmpleps:
381 case X86::BI__builtin_ia32_cmpneqps:
382 case X86::BI__builtin_ia32_cmpngtps:
383 case X86::BI__builtin_ia32_cmpnltps:
384 case X86::BI__builtin_ia32_cmpgtps:
385 case X86::BI__builtin_ia32_cmpgeps:
386 case X86::BI__builtin_ia32_cmpngeps:
387 case X86::BI__builtin_ia32_cmpnleps: {
Chris Lattner4b23f942007-12-18 00:25:38 +0000388 unsigned i = 0;
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000389 const char *name = 0;
390 bool ShouldSwap = false;
391 switch (BuiltinID) {
392 default: assert(0 && "Unknown compare builtin!");
Chris Lattner4b23f942007-12-18 00:25:38 +0000393 case X86::BI__builtin_ia32_cmpeqps: i = 0; name = "cmpeqps"; break;
394 case X86::BI__builtin_ia32_cmpltps: i = 1; name = "cmpltps"; break;
395 case X86::BI__builtin_ia32_cmpleps: i = 2; name = "cmpleps"; break;
396 case X86::BI__builtin_ia32_cmpunordps: i = 3; name = "cmpunordps"; break;
397 case X86::BI__builtin_ia32_cmpneqps: i = 4; name = "cmpneqps"; break;
398 case X86::BI__builtin_ia32_cmpnltps: i = 5; name = "cmpntlps"; break;
399 case X86::BI__builtin_ia32_cmpnleps: i = 6; name = "cmpnleps"; break;
400 case X86::BI__builtin_ia32_cmpordps: i = 7; name = "cmpordps"; break;
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000401 case X86::BI__builtin_ia32_cmpgtps:
Chris Lattner4b23f942007-12-18 00:25:38 +0000402 ShouldSwap = true;
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000403 i = 1;
404 name = "cmpgtps";
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000405 break;
406 case X86::BI__builtin_ia32_cmpgeps:
407 i = 2;
408 name = "cmpgeps";
409 ShouldSwap = true;
410 break;
411 case X86::BI__builtin_ia32_cmpngtps:
412 i = 5;
413 name = "cmpngtps";
414 ShouldSwap = true;
415 break;
416 case X86::BI__builtin_ia32_cmpngeps:
417 i = 6;
418 name = "cmpngeps";
419 ShouldSwap = true;
420 break;
421 }
422
423 if (ShouldSwap)
424 std::swap(Ops[0], Ops[1]);
425
426 Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i));
Chris Lattner4b23f942007-12-18 00:25:38 +0000427 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps);
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000428 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
429 }
430 case X86::BI__builtin_ia32_movss:
431 return EmitShuffleVector(Ops[0], Ops[1], 4, 1, 2, 3, "movss");
432 case X86::BI__builtin_ia32_shufps:
Chris Lattner4b23f942007-12-18 00:25:38 +0000433 unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue();
Anders Carlsson1ad775f2007-12-16 22:33:50 +0000434 return EmitShuffleVector(Ops[0], Ops[1],
435 i & 0x3, (i & 0xc) >> 2,
436 ((i & 0x30) >> 4) + 4,
437 ((i & 0x60) >> 6) + 4, "shufps");
Anders Carlssone1449c12007-12-09 23:17:02 +0000438 }
439}
440
Chris Lattner9d37e132007-12-13 07:34:23 +0000441Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
442 const CallExpr *E) {
Anders Carlssone1449c12007-12-09 23:17:02 +0000443 switch (BuiltinID) {
Anders Carlsson0a35a812007-12-09 23:39:18 +0000444 default: return 0;
Anders Carlssone1449c12007-12-09 23:17:02 +0000445 }
446}