blob: 07cce76b9154322fad1459fcedf0725350bde9ef [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
John McCalld0b76ca2010-03-02 03:50:12 +000014#include "TargetInfo.h"
Anders Carlsson022012e2007-08-20 18:05:56 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner1f329992008-10-06 06:09:18 +000018#include "clang/AST/APValue.h"
Chris Lattnerbef20ac2007-08-31 04:31:45 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000020#include "clang/AST/Decl.h"
Chris Lattner6b15cdc2009-06-14 01:05:48 +000021#include "clang/Basic/TargetBuiltins.h"
Anders Carlsson793680e2007-10-12 23:56:29 +000022#include "llvm/Intrinsics.h"
John McCalld0b76ca2010-03-02 03:50:12 +000023#include "llvm/Target/TargetData.h"
Anders Carlsson022012e2007-08-20 18:05:56 +000024using namespace clang;
25using namespace CodeGen;
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000026using namespace llvm;
27
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000028static void EmitMemoryBarrier(CodeGenFunction &CGF,
29 bool LoadLoad, bool LoadStore,
30 bool StoreLoad, bool StoreStore,
31 bool Device) {
32 Value *True = llvm::ConstantInt::getTrue(CGF.getLLVMContext());
33 Value *False = llvm::ConstantInt::getFalse(CGF.getLLVMContext());
34 Value *C[5] = { LoadLoad ? True : False,
35 LoadStore ? True : False,
36 StoreLoad ? True : False,
37 StoreStore ? True : False,
38 Device ? True : False };
39 CGF.Builder.CreateCall(CGF.CGM.getIntrinsic(Intrinsic::memory_barrier),
40 C, C + 5);
41}
42
43// The atomic builtins are also full memory barriers. This is a utility for
44// wrapping a call to the builtins with memory barriers.
45static Value *EmitCallWithBarrier(CodeGenFunction &CGF, Value *Fn,
46 Value **ArgBegin, Value **ArgEnd) {
47 // FIXME: We need a target hook for whether this applies to device memory or
48 // not.
49 bool Device = true;
50
51 // Create barriers both before and after the call.
52 EmitMemoryBarrier(CGF, true, true, true, true, Device);
53 Value *Result = CGF.Builder.CreateCall(Fn, ArgBegin, ArgEnd);
54 EmitMemoryBarrier(CGF, true, true, true, true, Device);
55 return Result;
56}
57
Daniel Dunbar0002d232009-04-07 00:55:51 +000058/// Utility to insert an atomic instruction based on Instrinsic::ID
59/// and the expression node.
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000060static RValue EmitBinaryAtomic(CodeGenFunction &CGF,
Mon P Wang1ffe2812008-05-09 22:40:52 +000061 Intrinsic::ID Id, const CallExpr *E) {
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000062 Value *Args[2] = { CGF.EmitScalarExpr(E->getArg(0)),
63 CGF.EmitScalarExpr(E->getArg(1)) };
Mon P Wangc5004512008-07-31 03:28:23 +000064 const llvm::Type *ResType[2];
Daniel Dunbar0002d232009-04-07 00:55:51 +000065 ResType[0] = CGF.ConvertType(E->getType());
66 ResType[1] = CGF.ConvertType(E->getArg(0)->getType());
67 Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2);
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000068 return RValue::get(EmitCallWithBarrier(CGF, AtomF, Args, Args + 2));
Daniel Dunbar0002d232009-04-07 00:55:51 +000069}
70
71/// Utility to insert an atomic instruction based Instrinsic::ID and
72// the expression node, where the return value is the result of the
73// operation.
Chris Lattner420b1182010-05-06 05:35:16 +000074static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF,
Daniel Dunbar0002d232009-04-07 00:55:51 +000075 Intrinsic::ID Id, const CallExpr *E,
76 Instruction::BinaryOps Op) {
77 const llvm::Type *ResType[2];
78 ResType[0] = CGF.ConvertType(E->getType());
79 ResType[1] = CGF.ConvertType(E->getArg(0)->getType());
80 Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2);
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000081 Value *Args[2] = { CGF.EmitScalarExpr(E->getArg(0)),
82 CGF.EmitScalarExpr(E->getArg(1)) };
83 Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 2);
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000084 return RValue::get(CGF.Builder.CreateBinOp(Op, Result, Args[1]));
Mon P Wang1ffe2812008-05-09 22:40:52 +000085}
86
John McCallfb17a562010-03-03 10:30:05 +000087static llvm::ConstantInt *getInt32(llvm::LLVMContext &Context, int32_t Value) {
88 return llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), Value);
89}
90
Chris Lattner420b1182010-05-06 05:35:16 +000091
92/// EmitFAbs - Emit a call to fabs/fabsf/fabsl, depending on the type of ValTy,
93/// which must be a scalar floating point type.
94static Value *EmitFAbs(CodeGenFunction &CGF, Value *V, QualType ValTy) {
95 const BuiltinType *ValTyP = ValTy->getAs<BuiltinType>();
96 assert(ValTyP && "isn't scalar fp type!");
97
98 StringRef FnName;
99 switch (ValTyP->getKind()) {
100 default: assert(0 && "Isn't a scalar fp type!");
101 case BuiltinType::Float: FnName = "fabsf"; break;
102 case BuiltinType::Double: FnName = "fabs"; break;
103 case BuiltinType::LongDouble: FnName = "fabsl"; break;
104 }
105
106 // The prototype is something that takes and returns whatever V's type is.
107 std::vector<const llvm::Type*> Args;
108 Args.push_back(V->getType());
109 llvm::FunctionType *FT = llvm::FunctionType::get(V->getType(), Args, false);
110 llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction(FT, FnName);
111
112 return CGF.Builder.CreateCall(Fn, V, "abs");
113}
114
Mike Stump1eb44332009-09-09 15:08:12 +0000115RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000116 unsigned BuiltinID, const CallExpr *E) {
Chris Lattner564ea2a2008-10-06 06:56:41 +0000117 // See if we can constant fold this builtin. If so, don't emit it at all.
Anders Carlssonf35d35a2008-12-01 02:31:41 +0000118 Expr::EvalResult Result;
Chris Lattner6ee7aa12008-11-16 21:24:15 +0000119 if (E->Evaluate(Result, CGM.getContext())) {
Anders Carlssonf35d35a2008-12-01 02:31:41 +0000120 if (Result.Val.isInt())
Mike Stump1eb44332009-09-09 15:08:12 +0000121 return RValue::get(llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000122 Result.Val.getInt()));
Eli Friedman3941b182009-01-25 01:54:01 +0000123 else if (Result.Val.isFloat())
Owen Andersonbc0a2222009-07-27 21:00:51 +0000124 return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat()));
Chris Lattner1f329992008-10-06 06:09:18 +0000125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattner564ea2a2008-10-06 06:56:41 +0000127 switch (BuiltinID) {
128 default: break; // Handle intrinsics and libm functions below.
Chris Lattner506ff882008-10-06 07:26:43 +0000129 case Builtin::BI__builtin___CFStringMakeConstantString:
David Chisnall0d13f6f2010-01-23 02:40:42 +0000130 case Builtin::BI__builtin___NSStringMakeConstantString:
Anders Carlssone9352cc2009-04-08 04:48:15 +0000131 return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0));
Chris Lattner6a705f02008-07-09 17:28:44 +0000132 case Builtin::BI__builtin_stdarg_start:
Anders Carlsson793680e2007-10-12 23:56:29 +0000133 case Builtin::BI__builtin_va_start:
134 case Builtin::BI__builtin_va_end: {
Daniel Dunbar07855702009-02-11 22:25:55 +0000135 Value *ArgValue = EmitVAListRef(E->getArg(0));
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000136 const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson793680e2007-10-12 23:56:29 +0000137 if (ArgValue->getType() != DestType)
Mike Stump1eb44332009-09-09 15:08:12 +0000138 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
Daniel Dunbarb27ffbe2009-07-26 09:28:40 +0000139 ArgValue->getName().data());
Anders Carlsson793680e2007-10-12 23:56:29 +0000140
Mike Stump1eb44332009-09-09 15:08:12 +0000141 Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ?
Chris Lattner6a705f02008-07-09 17:28:44 +0000142 Intrinsic::vaend : Intrinsic::vastart;
Chris Lattner7acda7c2007-12-18 00:25:38 +0000143 return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue));
Anders Carlsson793680e2007-10-12 23:56:29 +0000144 }
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000145 case Builtin::BI__builtin_va_copy: {
Eli Friedman4fd0aa52009-01-20 17:46:04 +0000146 Value *DstPtr = EmitVAListRef(E->getArg(0));
147 Value *SrcPtr = EmitVAListRef(E->getArg(1));
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000148
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000149 const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000150
151 DstPtr = Builder.CreateBitCast(DstPtr, Type);
152 SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000153 return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000154 DstPtr, SrcPtr));
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000155 }
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000156 case Builtin::BI__builtin_abs: {
Mike Stump1eb44332009-09-09 15:08:12 +0000157 Value *ArgValue = EmitScalarExpr(E->getArg(0));
158
Chris Lattner9a847f52008-07-23 06:53:34 +0000159 Value *NegOp = Builder.CreateNeg(ArgValue, "neg");
Mike Stump1eb44332009-09-09 15:08:12 +0000160 Value *CmpResult =
161 Builder.CreateICmpSGE(ArgValue,
Owen Andersonc9c88b42009-07-31 20:28:54 +0000162 llvm::Constant::getNullValue(ArgValue->getType()),
Chris Lattner9a847f52008-07-23 06:53:34 +0000163 "abscond");
Mike Stump1eb44332009-09-09 15:08:12 +0000164 Value *Result =
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000165 Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000167 return RValue::get(Result);
168 }
Anders Carlsson3a31d602008-02-06 07:19:27 +0000169 case Builtin::BI__builtin_ctz:
170 case Builtin::BI__builtin_ctzl:
171 case Builtin::BI__builtin_ctzll: {
172 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Anders Carlsson3a31d602008-02-06 07:19:27 +0000174 const llvm::Type *ArgType = ArgValue->getType();
175 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
176
Mike Stump1eb44332009-09-09 15:08:12 +0000177 const llvm::Type *ResultType = ConvertType(E->getType());
Anders Carlsson3a31d602008-02-06 07:19:27 +0000178 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
179 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000180 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
181 "cast");
Anders Carlsson3a31d602008-02-06 07:19:27 +0000182 return RValue::get(Result);
183 }
Eli Friedmanf4e85332008-05-27 15:32:46 +0000184 case Builtin::BI__builtin_clz:
185 case Builtin::BI__builtin_clzl:
186 case Builtin::BI__builtin_clzll: {
187 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Eli Friedmanf4e85332008-05-27 15:32:46 +0000189 const llvm::Type *ArgType = ArgValue->getType();
190 Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1);
191
Mike Stump1eb44332009-09-09 15:08:12 +0000192 const llvm::Type *ResultType = ConvertType(E->getType());
Eli Friedmanf4e85332008-05-27 15:32:46 +0000193 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
194 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000195 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
196 "cast");
Eli Friedmanf4e85332008-05-27 15:32:46 +0000197 return RValue::get(Result);
198 }
Daniel Dunbar04b29002008-07-21 17:19:41 +0000199 case Builtin::BI__builtin_ffs:
200 case Builtin::BI__builtin_ffsl:
201 case Builtin::BI__builtin_ffsll: {
202 // ffs(x) -> x ? cttz(x) + 1 : 0
203 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Daniel Dunbar04b29002008-07-21 17:19:41 +0000205 const llvm::Type *ArgType = ArgValue->getType();
206 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Daniel Dunbar04b29002008-07-21 17:19:41 +0000208 const llvm::Type *ResultType = ConvertType(E->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000209 Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"),
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000210 llvm::ConstantInt::get(ArgType, 1), "tmp");
Owen Andersonc9c88b42009-07-31 20:28:54 +0000211 Value *Zero = llvm::Constant::getNullValue(ArgType);
Daniel Dunbar04b29002008-07-21 17:19:41 +0000212 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
213 Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
214 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000215 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
216 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000217 return RValue::get(Result);
218 }
219 case Builtin::BI__builtin_parity:
220 case Builtin::BI__builtin_parityl:
221 case Builtin::BI__builtin_parityll: {
222 // parity(x) -> ctpop(x) & 1
223 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Daniel Dunbar04b29002008-07-21 17:19:41 +0000225 const llvm::Type *ArgType = ArgValue->getType();
226 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Daniel Dunbar04b29002008-07-21 17:19:41 +0000228 const llvm::Type *ResultType = ConvertType(E->getType());
229 Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000230 Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1),
Daniel Dunbar04b29002008-07-21 17:19:41 +0000231 "tmp");
232 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000233 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
234 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000235 return RValue::get(Result);
236 }
237 case Builtin::BI__builtin_popcount:
238 case Builtin::BI__builtin_popcountl:
239 case Builtin::BI__builtin_popcountll: {
240 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Daniel Dunbar04b29002008-07-21 17:19:41 +0000242 const llvm::Type *ArgType = ArgValue->getType();
243 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Daniel Dunbar04b29002008-07-21 17:19:41 +0000245 const llvm::Type *ResultType = ConvertType(E->getType());
246 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
247 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000248 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
249 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000250 return RValue::get(Result);
251 }
Chris Lattner1feedd82007-12-13 07:34:23 +0000252 case Builtin::BI__builtin_expect:
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000253 // FIXME: pass expect through to LLVM
Chris Lattner1feedd82007-12-13 07:34:23 +0000254 return RValue::get(EmitScalarExpr(E->getArg(0)));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000255 case Builtin::BI__builtin_bswap32:
256 case Builtin::BI__builtin_bswap64: {
Chris Lattner1feedd82007-12-13 07:34:23 +0000257 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000258 const llvm::Type *ArgType = ArgValue->getType();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000259 Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
Chris Lattner1feedd82007-12-13 07:34:23 +0000260 return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
Mike Stump1eb44332009-09-09 15:08:12 +0000261 }
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000262 case Builtin::BI__builtin_object_size: {
Mike Stumpb16d32f2009-10-26 23:39:48 +0000263 // We pass this builtin onto the optimizer so that it can
264 // figure out the object size in more complex cases.
Mike Stumpc4c90452009-10-27 22:09:17 +0000265 const llvm::Type *ResType[] = {
266 ConvertType(E->getType())
267 };
Eric Christopherfee667f2009-12-23 03:49:37 +0000268
269 // LLVM only supports 0 and 2, make sure that we pass along that
270 // as a boolean.
271 Value *Ty = EmitScalarExpr(E->getArg(1));
272 ConstantInt *CI = dyn_cast<ConstantInt>(Ty);
273 assert(CI);
274 uint64_t val = CI->getZExtValue();
275 CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1);
276
Mike Stumpc4c90452009-10-27 22:09:17 +0000277 Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1);
278 return RValue::get(Builder.CreateCall2(F,
279 EmitScalarExpr(E->getArg(0)),
Eric Christopherfee667f2009-12-23 03:49:37 +0000280 CI));
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000281 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000282 case Builtin::BI__builtin_prefetch: {
283 Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
284 // FIXME: Technically these constants should of type 'int', yes?
Mike Stump1eb44332009-09-09 15:08:12 +0000285 RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
Owen Anderson0032b272009-08-13 21:57:51 +0000286 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000287 Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
Owen Anderson0032b272009-08-13 21:57:51 +0000288 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 3);
Daniel Dunbar4493f792008-07-21 22:59:13 +0000289 Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0);
290 return RValue::get(Builder.CreateCall3(F, Address, RW, Locality));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000291 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000292 case Builtin::BI__builtin_trap: {
293 Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0);
294 return RValue::get(Builder.CreateCall(F));
295 }
Chris Lattner21190d52009-09-21 03:09:59 +0000296 case Builtin::BI__builtin_unreachable: {
Mike Stumpfba565d2009-12-16 03:07:12 +0000297 if (CatchUndefined && HaveInsertPoint())
298 EmitBranch(getTrapBB());
Chris Lattner21190d52009-09-21 03:09:59 +0000299 Value *V = Builder.CreateUnreachable();
300 Builder.ClearInsertionPoint();
301 return RValue::get(V);
302 }
303
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000304 case Builtin::BI__builtin_powi:
305 case Builtin::BI__builtin_powif:
306 case Builtin::BI__builtin_powil: {
307 Value *Base = EmitScalarExpr(E->getArg(0));
308 Value *Exponent = EmitScalarExpr(E->getArg(1));
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000309 const llvm::Type *ArgType = Base->getType();
310 Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1);
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000311 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
312 }
313
Chris Lattnerfe23e212007-12-20 00:44:32 +0000314 case Builtin::BI__builtin_isgreater:
315 case Builtin::BI__builtin_isgreaterequal:
316 case Builtin::BI__builtin_isless:
317 case Builtin::BI__builtin_islessequal:
318 case Builtin::BI__builtin_islessgreater:
319 case Builtin::BI__builtin_isunordered: {
320 // Ordered comparisons: we know the arguments to these are matching scalar
321 // floating point values.
Mike Stump1eb44332009-09-09 15:08:12 +0000322 Value *LHS = EmitScalarExpr(E->getArg(0));
Chris Lattnerfe23e212007-12-20 00:44:32 +0000323 Value *RHS = EmitScalarExpr(E->getArg(1));
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Chris Lattnerfe23e212007-12-20 00:44:32 +0000325 switch (BuiltinID) {
326 default: assert(0 && "Unknown ordered comparison");
327 case Builtin::BI__builtin_isgreater:
328 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
329 break;
330 case Builtin::BI__builtin_isgreaterequal:
331 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
332 break;
333 case Builtin::BI__builtin_isless:
334 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
335 break;
336 case Builtin::BI__builtin_islessequal:
337 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
338 break;
339 case Builtin::BI__builtin_islessgreater:
340 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
341 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000342 case Builtin::BI__builtin_isunordered:
Chris Lattnerfe23e212007-12-20 00:44:32 +0000343 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
344 break;
345 }
346 // ZExt bool to int type.
347 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()),
348 "tmp"));
349 }
Eli Friedmand6139892009-09-01 04:19:44 +0000350 case Builtin::BI__builtin_isnan: {
351 Value *V = EmitScalarExpr(E->getArg(0));
352 V = Builder.CreateFCmpUNO(V, V, "cmp");
353 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp"));
354 }
Chris Lattner420b1182010-05-06 05:35:16 +0000355
356 case Builtin::BI__builtin_isinf: {
357 // isinf(x) --> fabs(x) == infinity
358 Value *V = EmitScalarExpr(E->getArg(0));
359 V = EmitFAbs(*this, V, E->getArg(0)->getType());
360
361 V = Builder.CreateFCmpOEQ(V, ConstantFP::getInfinity(V->getType()),"isinf");
362 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp"));
363 }
Chris Lattner58ae5b42010-05-06 06:13:53 +0000364
365 // TODO: BI__builtin_isinf_sign
366 // isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0
Benjamin Kramer6349ce92010-05-19 11:24:26 +0000367
368 case Builtin::BI__builtin_isnormal: {
369 // isnormal(x) --> x == x && fabsf(x) < infinity && fabsf(x) >= float_min
370 Value *V = EmitScalarExpr(E->getArg(0));
371 Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
372
373 Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType());
374 Value *IsLessThanInf =
375 Builder.CreateFCmpULT(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
376 APFloat Smallest = APFloat::getSmallestNormalized(
377 getContext().getFloatTypeSemantics(E->getArg(0)->getType()));
378 Value *IsNormal =
379 Builder.CreateFCmpUGE(Abs, ConstantFP::get(V->getContext(), Smallest),
380 "isnormal");
381 V = Builder.CreateAnd(Eq, IsLessThanInf, "and");
382 V = Builder.CreateAnd(V, IsNormal, "and");
383 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
384 }
385
Chris Lattnered074152010-05-06 06:04:13 +0000386 case Builtin::BI__builtin_isfinite: {
387 // isfinite(x) --> x == x && fabs(x) != infinity; }
388 Value *V = EmitScalarExpr(E->getArg(0));
389 Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
390
391 Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType());
392 Value *IsNotInf =
393 Builder.CreateFCmpUNE(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
394
395 V = Builder.CreateAnd(Eq, IsNotInf, "and");
396 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
397 }
398
Eli Friedmanb52fe9c2009-06-02 07:10:30 +0000399 case Builtin::BIalloca:
Chris Lattner9e800e32008-06-16 17:15:14 +0000400 case Builtin::BI__builtin_alloca: {
Chris Lattner9e800e32008-06-16 17:15:14 +0000401 Value *Size = EmitScalarExpr(E->getArg(0));
Owen Anderson0032b272009-08-13 21:57:51 +0000402 return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp"));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000403 }
Eli Friedmane6dddfd2010-01-23 19:00:10 +0000404 case Builtin::BIbzero:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000405 case Builtin::BI__builtin_bzero: {
406 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000407 Value *SizeVal = EmitScalarExpr(E->getArg(1));
408 Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()),
409 Address,
410 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0),
411 SizeVal,
412 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1),
413 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000414 return RValue::get(Address);
Chris Lattner9e800e32008-06-16 17:15:14 +0000415 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000416 case Builtin::BImemcpy:
Eli Friedmand4b32e42008-05-19 23:27:48 +0000417 case Builtin::BI__builtin_memcpy: {
Daniel Dunbar1caae952008-07-22 00:26:45 +0000418 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000419 Value *SrcAddr = EmitScalarExpr(E->getArg(1));
420 Value *SizeVal = EmitScalarExpr(E->getArg(2));
421 Builder.CreateCall5(CGM.getMemCpyFn(Address->getType(), SrcAddr->getType(),
422 SizeVal->getType()),
423 Address, SrcAddr, SizeVal,
424 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1),
425 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000426 return RValue::get(Address);
427 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000428 case Builtin::BImemmove:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000429 case Builtin::BI__builtin_memmove: {
430 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000431 Value *SrcAddr = EmitScalarExpr(E->getArg(1));
432 Value *SizeVal = EmitScalarExpr(E->getArg(2));
433 Builder.CreateCall5(CGM.getMemMoveFn(Address->getType(), SrcAddr->getType(),
434 SizeVal->getType()),
435 Address, SrcAddr, SizeVal,
436 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1),
437 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000438 return RValue::get(Address);
439 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000440 case Builtin::BImemset:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000441 case Builtin::BI__builtin_memset: {
442 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000443 Value *SizeVal = EmitScalarExpr(E->getArg(2));
444 Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()),
445 Address,
446 Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
447 llvm::Type::getInt8Ty(VMContext)),
448 SizeVal,
449 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1),
450 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000451 return RValue::get(Address);
Eli Friedmand4b32e42008-05-19 23:27:48 +0000452 }
John McCallfb17a562010-03-03 10:30:05 +0000453 case Builtin::BI__builtin_dwarf_cfa: {
454 // The offset in bytes from the first argument to the CFA.
455 //
456 // Why on earth is this in the frontend? Is there any reason at
457 // all that the backend can't reasonably determine this while
458 // lowering llvm.eh.dwarf.cfa()?
459 //
460 // TODO: If there's a satisfactory reason, add a target hook for
461 // this instead of hard-coding 0, which is correct for most targets.
462 int32_t Offset = 0;
463
464 Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa, 0, 0);
465 return RValue::get(Builder.CreateCall(F, getInt32(VMContext, Offset)));
466 }
Eli Friedman256f77e2008-05-20 08:59:34 +0000467 case Builtin::BI__builtin_return_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000468 Value *Depth = EmitScalarExpr(E->getArg(0));
469 Depth = Builder.CreateIntCast(Depth,
470 llvm::Type::getInt32Ty(VMContext),
471 false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000472 Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000473 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000474 }
475 case Builtin::BI__builtin_frame_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000476 Value *Depth = EmitScalarExpr(E->getArg(0));
477 Depth = Builder.CreateIntCast(Depth,
478 llvm::Type::getInt32Ty(VMContext),
479 false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000480 Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000481 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000482 }
Eli Friedman3b660ef2009-05-03 19:23:23 +0000483 case Builtin::BI__builtin_extract_return_addr: {
John McCall492c4f92010-03-03 04:15:11 +0000484 Value *Address = EmitScalarExpr(E->getArg(0));
485 Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);
486 return RValue::get(Result);
487 }
488 case Builtin::BI__builtin_frob_return_addr: {
489 Value *Address = EmitScalarExpr(E->getArg(0));
490 Value *Result = getTargetHooks().encodeReturnAddress(*this, Address);
491 return RValue::get(Result);
Eli Friedman3b660ef2009-05-03 19:23:23 +0000492 }
John McCall6374c332010-03-06 00:35:14 +0000493 case Builtin::BI__builtin_dwarf_sp_column: {
494 const llvm::IntegerType *Ty
495 = cast<llvm::IntegerType>(ConvertType(E->getType()));
496 int Column = getTargetHooks().getDwarfEHStackPointer(CGM);
497 if (Column == -1) {
498 CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column");
499 return RValue::get(llvm::UndefValue::get(Ty));
500 }
501 return RValue::get(llvm::ConstantInt::get(Ty, Column, true));
502 }
503 case Builtin::BI__builtin_init_dwarf_reg_size_table: {
504 Value *Address = EmitScalarExpr(E->getArg(0));
505 if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address))
506 CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table");
507 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
508 }
John McCall7ada1112010-03-03 05:38:58 +0000509 case Builtin::BI__builtin_eh_return: {
510 Value *Int = EmitScalarExpr(E->getArg(0));
511 Value *Ptr = EmitScalarExpr(E->getArg(1));
512
513 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType());
514 assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) &&
515 "LLVM's __builtin_eh_return only supports 32- and 64-bit variants");
516 Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32
517 ? Intrinsic::eh_return_i32
518 : Intrinsic::eh_return_i64,
519 0, 0);
520 Builder.CreateCall2(F, Int, Ptr);
521 Value *V = Builder.CreateUnreachable();
522 Builder.ClearInsertionPoint();
523 return RValue::get(V);
524 }
Eli Friedmana6d75c02009-06-02 09:37:50 +0000525 case Builtin::BI__builtin_unwind_init: {
526 Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0);
527 return RValue::get(Builder.CreateCall(F));
528 }
John McCall5e110852010-03-02 02:31:24 +0000529 case Builtin::BI__builtin_extend_pointer: {
530 // Extends a pointer to the size of an _Unwind_Word, which is
John McCalld0b76ca2010-03-02 03:50:12 +0000531 // uint64_t on all platforms. Generally this gets poked into a
532 // register and eventually used as an address, so if the
533 // addressing registers are wider than pointers and the platform
534 // doesn't implicitly ignore high-order bits when doing
535 // addressing, we need to make sure we zext / sext based on
536 // the platform's expectations.
John McCall5e110852010-03-02 02:31:24 +0000537 //
538 // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html
John McCalld0b76ca2010-03-02 03:50:12 +0000539
540 LLVMContext &C = CGM.getLLVMContext();
541
542 // Cast the pointer to intptr_t.
John McCall5e110852010-03-02 02:31:24 +0000543 Value *Ptr = EmitScalarExpr(E->getArg(0));
John McCalld0b76ca2010-03-02 03:50:12 +0000544 const llvm::IntegerType *IntPtrTy = CGM.getTargetData().getIntPtrType(C);
545 Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast");
546
547 // If that's 64 bits, we're done.
548 if (IntPtrTy->getBitWidth() == 64)
549 return RValue::get(Result);
550
551 // Otherwise, ask the codegen data what to do.
552 const llvm::IntegerType *Int64Ty = llvm::IntegerType::get(C, 64);
John McCall492c4f92010-03-03 04:15:11 +0000553 if (getTargetHooks().extendPointerWithSExt())
John McCalld0b76ca2010-03-02 03:50:12 +0000554 return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext"));
555 else
556 return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext"));
John McCall5e110852010-03-02 02:31:24 +0000557 }
Eli Friedmana6d75c02009-06-02 09:37:50 +0000558 case Builtin::BI__builtin_setjmp: {
John McCall78673d92010-05-27 18:47:06 +0000559 // Buffer is a void**.
Eli Friedmana6d75c02009-06-02 09:37:50 +0000560 Value *Buf = EmitScalarExpr(E->getArg(0));
John McCall78673d92010-05-27 18:47:06 +0000561
562 // Store the frame pointer to the setjmp buffer.
Eli Friedmana6d75c02009-06-02 09:37:50 +0000563 Value *FrameAddr =
John McCall78673d92010-05-27 18:47:06 +0000564 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::frameaddress),
565 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
Eli Friedmana6d75c02009-06-02 09:37:50 +0000566 Builder.CreateStore(FrameAddr, Buf);
John McCall78673d92010-05-27 18:47:06 +0000567
Jim Grosbach6d172e22010-05-27 23:54:20 +0000568 // Store the stack pointer to the setjmp buffer.
569 Value *StackAddr =
570 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));
571 Value *StackSaveSlot =
572 Builder.CreateGEP(Buf, ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
573 2));
574 Builder.CreateStore(StackAddr, StackSaveSlot);
575
John McCall78673d92010-05-27 18:47:06 +0000576 // Call LLVM's EH setjmp, which is lightweight.
577 Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp);
578 Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext));
Eli Friedmana6d75c02009-06-02 09:37:50 +0000579 return RValue::get(Builder.CreateCall(F, Buf));
580 }
581 case Builtin::BI__builtin_longjmp: {
Eli Friedmana6d75c02009-06-02 09:37:50 +0000582 Value *Buf = EmitScalarExpr(E->getArg(0));
John McCall78673d92010-05-27 18:47:06 +0000583 Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext));
584
585 // Call LLVM's EH longjmp, which is lightweight.
586 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf);
587
588 // longjmp doesn't return; mark this as unreachable
589 Value *V = Builder.CreateUnreachable();
590 Builder.ClearInsertionPoint();
591 return RValue::get(V);
Eli Friedmana6d75c02009-06-02 09:37:50 +0000592 }
Mon P Wang1ffe2812008-05-09 22:40:52 +0000593 case Builtin::BI__sync_fetch_and_add:
Mon P Wang1ffe2812008-05-09 22:40:52 +0000594 case Builtin::BI__sync_fetch_and_sub:
Chris Lattner5caa3702009-05-08 06:58:22 +0000595 case Builtin::BI__sync_fetch_and_or:
596 case Builtin::BI__sync_fetch_and_and:
597 case Builtin::BI__sync_fetch_and_xor:
598 case Builtin::BI__sync_add_and_fetch:
599 case Builtin::BI__sync_sub_and_fetch:
600 case Builtin::BI__sync_and_and_fetch:
601 case Builtin::BI__sync_or_and_fetch:
602 case Builtin::BI__sync_xor_and_fetch:
603 case Builtin::BI__sync_val_compare_and_swap:
604 case Builtin::BI__sync_bool_compare_and_swap:
605 case Builtin::BI__sync_lock_test_and_set:
606 case Builtin::BI__sync_lock_release:
607 assert(0 && "Shouldn't make it through sema");
608 case Builtin::BI__sync_fetch_and_add_1:
609 case Builtin::BI__sync_fetch_and_add_2:
610 case Builtin::BI__sync_fetch_and_add_4:
611 case Builtin::BI__sync_fetch_and_add_8:
612 case Builtin::BI__sync_fetch_and_add_16:
613 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E);
614 case Builtin::BI__sync_fetch_and_sub_1:
615 case Builtin::BI__sync_fetch_and_sub_2:
616 case Builtin::BI__sync_fetch_and_sub_4:
617 case Builtin::BI__sync_fetch_and_sub_8:
618 case Builtin::BI__sync_fetch_and_sub_16:
Mon P Wang09b6bf52008-06-25 08:21:36 +0000619 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E);
Chris Lattner5caa3702009-05-08 06:58:22 +0000620 case Builtin::BI__sync_fetch_and_or_1:
621 case Builtin::BI__sync_fetch_and_or_2:
622 case Builtin::BI__sync_fetch_and_or_4:
623 case Builtin::BI__sync_fetch_and_or_8:
624 case Builtin::BI__sync_fetch_and_or_16:
625 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E);
626 case Builtin::BI__sync_fetch_and_and_1:
627 case Builtin::BI__sync_fetch_and_and_2:
628 case Builtin::BI__sync_fetch_and_and_4:
629 case Builtin::BI__sync_fetch_and_and_8:
630 case Builtin::BI__sync_fetch_and_and_16:
631 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E);
632 case Builtin::BI__sync_fetch_and_xor_1:
633 case Builtin::BI__sync_fetch_and_xor_2:
634 case Builtin::BI__sync_fetch_and_xor_4:
635 case Builtin::BI__sync_fetch_and_xor_8:
636 case Builtin::BI__sync_fetch_and_xor_16:
637 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Chris Lattner5caa3702009-05-08 06:58:22 +0000639 // Clang extensions: not overloaded yet.
Mon P Wang1ffe2812008-05-09 22:40:52 +0000640 case Builtin::BI__sync_fetch_and_min:
641 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E);
642 case Builtin::BI__sync_fetch_and_max:
643 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E);
644 case Builtin::BI__sync_fetch_and_umin:
645 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E);
646 case Builtin::BI__sync_fetch_and_umax:
647 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000648
Chris Lattner5caa3702009-05-08 06:58:22 +0000649 case Builtin::BI__sync_add_and_fetch_1:
650 case Builtin::BI__sync_add_and_fetch_2:
651 case Builtin::BI__sync_add_and_fetch_4:
652 case Builtin::BI__sync_add_and_fetch_8:
653 case Builtin::BI__sync_add_and_fetch_16:
Mike Stump1eb44332009-09-09 15:08:12 +0000654 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E,
Daniel Dunbar0002d232009-04-07 00:55:51 +0000655 llvm::Instruction::Add);
Chris Lattner5caa3702009-05-08 06:58:22 +0000656 case Builtin::BI__sync_sub_and_fetch_1:
657 case Builtin::BI__sync_sub_and_fetch_2:
658 case Builtin::BI__sync_sub_and_fetch_4:
659 case Builtin::BI__sync_sub_and_fetch_8:
660 case Builtin::BI__sync_sub_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000661 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E,
662 llvm::Instruction::Sub);
Chris Lattner5caa3702009-05-08 06:58:22 +0000663 case Builtin::BI__sync_and_and_fetch_1:
664 case Builtin::BI__sync_and_and_fetch_2:
665 case Builtin::BI__sync_and_and_fetch_4:
666 case Builtin::BI__sync_and_and_fetch_8:
667 case Builtin::BI__sync_and_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000668 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E,
669 llvm::Instruction::And);
Chris Lattner5caa3702009-05-08 06:58:22 +0000670 case Builtin::BI__sync_or_and_fetch_1:
671 case Builtin::BI__sync_or_and_fetch_2:
672 case Builtin::BI__sync_or_and_fetch_4:
673 case Builtin::BI__sync_or_and_fetch_8:
674 case Builtin::BI__sync_or_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000675 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E,
676 llvm::Instruction::Or);
Chris Lattner5caa3702009-05-08 06:58:22 +0000677 case Builtin::BI__sync_xor_and_fetch_1:
678 case Builtin::BI__sync_xor_and_fetch_2:
679 case Builtin::BI__sync_xor_and_fetch_4:
680 case Builtin::BI__sync_xor_and_fetch_8:
681 case Builtin::BI__sync_xor_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000682 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E,
683 llvm::Instruction::Xor);
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Chris Lattner5caa3702009-05-08 06:58:22 +0000685 case Builtin::BI__sync_val_compare_and_swap_1:
686 case Builtin::BI__sync_val_compare_and_swap_2:
687 case Builtin::BI__sync_val_compare_and_swap_4:
688 case Builtin::BI__sync_val_compare_and_swap_8:
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000689 case Builtin::BI__sync_val_compare_and_swap_16: {
Mon P Wangc5004512008-07-31 03:28:23 +0000690 const llvm::Type *ResType[2];
691 ResType[0]= ConvertType(E->getType());
692 ResType[1] = ConvertType(E->getArg(0)->getType());
693 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2);
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000694 Value *Args[3] = { EmitScalarExpr(E->getArg(0)),
695 EmitScalarExpr(E->getArg(1)),
696 EmitScalarExpr(E->getArg(2)) };
697 return RValue::get(EmitCallWithBarrier(*this, AtomF, Args, Args + 3));
Anders Carlsson89799cf2007-10-29 02:59:40 +0000698 }
Daniel Dunbar0002d232009-04-07 00:55:51 +0000699
Chris Lattner5caa3702009-05-08 06:58:22 +0000700 case Builtin::BI__sync_bool_compare_and_swap_1:
701 case Builtin::BI__sync_bool_compare_and_swap_2:
702 case Builtin::BI__sync_bool_compare_and_swap_4:
703 case Builtin::BI__sync_bool_compare_and_swap_8:
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000704 case Builtin::BI__sync_bool_compare_and_swap_16: {
Daniel Dunbar0002d232009-04-07 00:55:51 +0000705 const llvm::Type *ResType[2];
Chris Lattner5caa3702009-05-08 06:58:22 +0000706 ResType[0]= ConvertType(E->getArg(1)->getType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000707 ResType[1] = llvm::PointerType::getUnqual(ResType[0]);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000708 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2);
709 Value *OldVal = EmitScalarExpr(E->getArg(1));
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000710 Value *Args[3] = { EmitScalarExpr(E->getArg(0)),
711 OldVal,
712 EmitScalarExpr(E->getArg(2)) };
713 Value *PrevVal = EmitCallWithBarrier(*this, AtomF, Args, Args + 3);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000714 Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal);
715 // zext bool to int.
716 return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
717 }
718
Chris Lattner5caa3702009-05-08 06:58:22 +0000719 case Builtin::BI__sync_lock_test_and_set_1:
720 case Builtin::BI__sync_lock_test_and_set_2:
721 case Builtin::BI__sync_lock_test_and_set_4:
722 case Builtin::BI__sync_lock_test_and_set_8:
723 case Builtin::BI__sync_lock_test_and_set_16:
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000724 return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E);
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000725
Chris Lattner5caa3702009-05-08 06:58:22 +0000726 case Builtin::BI__sync_lock_release_1:
727 case Builtin::BI__sync_lock_release_2:
728 case Builtin::BI__sync_lock_release_4:
729 case Builtin::BI__sync_lock_release_8:
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000730 case Builtin::BI__sync_lock_release_16: {
731 Value *Ptr = EmitScalarExpr(E->getArg(0));
732 const llvm::Type *ElTy =
733 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar007b5672009-11-29 21:11:47 +0000734 llvm::StoreInst *Store =
735 Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr);
736 Store->setVolatile(true);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000737 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000738 }
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000739
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000740 case Builtin::BI__sync_synchronize: {
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000741 // We assume like gcc appears to, that this only applies to cached memory.
742 EmitMemoryBarrier(*this, true, true, true, true, false);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000743 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000744 }
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Tanya Lattner0b571642010-01-16 01:21:14 +0000746 case Builtin::BI__builtin_llvm_memory_barrier: {
747 Value *C[5] = {
748 EmitScalarExpr(E->getArg(0)),
749 EmitScalarExpr(E->getArg(1)),
750 EmitScalarExpr(E->getArg(2)),
751 EmitScalarExpr(E->getArg(3)),
752 EmitScalarExpr(E->getArg(4))
753 };
754 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5);
755 return RValue::get(0);
756 }
757
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000758 // Library functions with special handling.
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000759 case Builtin::BIsqrt:
760 case Builtin::BIsqrtf:
761 case Builtin::BIsqrtl: {
John McCallbeb41282010-04-07 08:20:20 +0000762 // TODO: there is currently no set of optimizer flags
763 // sufficient for us to rewrite sqrt to @llvm.sqrt.
764 // -fmath-errno=0 is not good enough; we need finiteness.
765 // We could probably precondition the call with an ult
766 // against 0, but is that worth the complexity?
767 break;
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000768 }
769
770 case Builtin::BIpow:
771 case Builtin::BIpowf:
772 case Builtin::BIpowl: {
773 // Rewrite sqrt to intrinsic if allowed.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000774 if (!FD->hasAttr<ConstAttr>())
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000775 break;
776 Value *Base = EmitScalarExpr(E->getArg(0));
777 Value *Exponent = EmitScalarExpr(E->getArg(1));
778 const llvm::Type *ArgType = Base->getType();
779 Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1);
780 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
781 }
Eli Friedmanba68b082010-03-06 02:17:52 +0000782
783 case Builtin::BI__builtin_signbit:
784 case Builtin::BI__builtin_signbitf:
785 case Builtin::BI__builtin_signbitl: {
786 LLVMContext &C = CGM.getLLVMContext();
787
788 Value *Arg = EmitScalarExpr(E->getArg(0));
789 const llvm::Type *ArgTy = Arg->getType();
790 if (ArgTy->isPPC_FP128Ty())
791 break; // FIXME: I'm not sure what the right implementation is here.
792 int ArgWidth = ArgTy->getPrimitiveSizeInBits();
793 const llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth);
794 Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy);
795 Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy);
796 Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp);
797 return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
798 }
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000801 // If this is an alias for a libm function (e.g. __builtin_sin) turn it into
802 // that function.
Douglas Gregor3e41d602009-02-13 23:20:09 +0000803 if (getContext().BuiltinInfo.isLibFunction(BuiltinID) ||
804 getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
Anders Carlsson31777a22009-12-24 19:08:58 +0000805 return EmitCall(E->getCallee()->getType(),
806 CGM.getBuiltinLibFunction(FD, BuiltinID),
Anders Carlssond2490a92009-12-24 20:40:36 +0000807 ReturnValueSlot(),
Anders Carlsson31777a22009-12-24 19:08:58 +0000808 E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000810 // See if we have a target specific intrinsic.
Dale Johannesena6f80ef2009-02-05 01:50:47 +0000811 const char *Name = getContext().BuiltinInfo.GetName(BuiltinID);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000812 Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
813 if (const char *Prefix =
Mike Stump1eb44332009-09-09 15:08:12 +0000814 llvm::Triple::getArchTypePrefix(Target.getTriple().getArch()))
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000815 IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000817 if (IntrinsicID != Intrinsic::not_intrinsic) {
818 SmallVector<Value*, 16> Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000820 Function *F = CGM.getIntrinsic(IntrinsicID);
821 const llvm::FunctionType *FTy = F->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000823 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
824 Value *ArgValue = EmitScalarExpr(E->getArg(i));
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000826 // If the intrinsic arg type is different from the builtin arg type
827 // we need to do a bit cast.
828 const llvm::Type *PTy = FTy->getParamType(i);
829 if (PTy != ArgValue->getType()) {
830 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
831 "Must be able to losslessly bit cast to param");
832 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000835 Args.push_back(ArgValue);
836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Jay Foadbeaaccd2009-05-21 09:52:38 +0000838 Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size());
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000839 QualType BuiltinRetType = E->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Owen Anderson0032b272009-08-13 21:57:51 +0000841 const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000842 if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType);
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000844 if (RetTy != V->getType()) {
845 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
846 "Must be able to losslessly bit cast result type");
847 V = Builder.CreateBitCast(V, RetTy);
848 }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000850 return RValue::get(V);
851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000853 // See if we have a target specific builtin that needs to be lowered.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000854 if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E))
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000855 return RValue::get(V);
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Daniel Dunbar488e9932008-08-16 00:56:44 +0000857 ErrorUnsupported(E, "builtin function");
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000859 // Unknown builtin, for now just dump it out and return undef.
860 if (hasAggregateLLVMType(E->getType()))
Daniel Dunbar195337d2010-02-09 02:48:28 +0000861 return RValue::getAggregate(CreateMemTemp(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +0000862 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
Mike Stump1eb44332009-09-09 15:08:12 +0000863}
Anders Carlsson564f1de2007-12-09 23:17:02 +0000864
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000865Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID,
866 const CallExpr *E) {
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000867 switch (Target.getTriple().getArch()) {
Chris Lattner2752c012010-03-03 19:03:45 +0000868 case llvm::Triple::arm:
869 case llvm::Triple::thumb:
870 return EmitARMBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000871 case llvm::Triple::x86:
872 case llvm::Triple::x86_64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000873 return EmitX86BuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000874 case llvm::Triple::ppc:
875 case llvm::Triple::ppc64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000876 return EmitPPCBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000877 default:
878 return 0;
879 }
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000880}
881
Nate Begeman998622c2010-06-07 16:01:56 +0000882const llvm::Type *GetNeonType(LLVMContext &Ctx, unsigned type, bool q) {
883 switch (type) {
884 default: break;
885 case 0:
886 case 5: return llvm::VectorType::get(llvm::Type::getInt8Ty(Ctx), 8 << q);
887 case 6:
888 case 7:
889 case 1: return llvm::VectorType::get(llvm::Type::getInt16Ty(Ctx), 4 << q);
890 case 2: return llvm::VectorType::get(llvm::Type::getInt32Ty(Ctx), 2 << q);
891 case 3: return llvm::VectorType::get(llvm::Type::getInt64Ty(Ctx), 1 << q);
892 case 4: return llvm::VectorType::get(llvm::Type::getFloatTy(Ctx), 2 << q);
893 };
894 return 0;
895}
896
Nate Begemand075c012010-06-10 00:17:56 +0000897Value *CodeGenFunction::EmitNeonSplat(Value *V, Constant *C) {
898 unsigned nElts = cast<llvm::VectorType>(V->getType())->getNumElements();
899 SmallVector<Constant*, 16> Indices(nElts, C);
900 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
901 return Builder.CreateShuffleVector(V, V, SV, "lane");
902}
903
Nate Begeman30d91712010-06-08 06:03:01 +0000904Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl<Value*> &Ops,
Nate Begemand075c012010-06-10 00:17:56 +0000905 const char *name, bool splat) {
Nate Begeman30d91712010-06-08 06:03:01 +0000906 unsigned j = 0;
907 for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
908 ai != ae; ++ai, ++j)
909 Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name);
910
Nate Begemand075c012010-06-10 00:17:56 +0000911 if (splat) {
912 Ops[j-1] = EmitNeonSplat(Ops[j-1], cast<Constant>(Ops[j]));
913 Ops.resize(j);
914 }
Nate Begeman30d91712010-06-08 06:03:01 +0000915 return Builder.CreateCall(F, Ops.begin(), Ops.end(), name);
916}
917
Nate Begeman464ccb62010-06-11 22:57:12 +0000918Value *CodeGenFunction::EmitNeonShiftVector(Value *V, const llvm::Type *Ty,
919 bool neg) {
920 ConstantInt *CI = cast<ConstantInt>(V);
921 int SV = CI->getSExtValue();
922
923 const llvm::VectorType *VTy = cast<llvm::VectorType>(Ty);
924 llvm::Constant *C = ConstantInt::get(VTy->getElementType(), neg ? -SV : SV);
925 SmallVector<llvm::Constant*, 16> CV(VTy->getNumElements(), C);
926 return llvm::ConstantVector::get(CV.begin(), CV.size());
927}
928
Chris Lattner2752c012010-03-03 19:03:45 +0000929Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
930 const CallExpr *E) {
Rafael Espindolae140af32010-06-09 03:48:40 +0000931 if (BuiltinID == ARM::BI__clear_cache) {
Rafael Espindola79ba5092010-06-07 17:26:50 +0000932 const FunctionDecl *FD = E->getDirectCallee();
933 Value *a = EmitScalarExpr(E->getArg(0));
934 Value *b = EmitScalarExpr(E->getArg(1));
935 const llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType());
936 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
937 llvm::StringRef Name = FD->getName();
938 return Builder.CreateCall2(CGM.CreateRuntimeFunction(FTy, Name),
939 a, b);
Chris Lattner2752c012010-03-03 19:03:45 +0000940 }
Rafael Espindolae140af32010-06-09 03:48:40 +0000941
Rafael Espindolae140af32010-06-09 03:48:40 +0000942 // Determine the type of this overloaded NEON intrinsic.
943 assert(BuiltinID > ARM::BI__builtin_thread_pointer);
Nate Begemand075c012010-06-10 00:17:56 +0000944
945 llvm::SmallVector<Value*, 4> Ops;
Rafael Espindolae140af32010-06-09 03:48:40 +0000946 for (unsigned i = 0, e = E->getNumArgs() - 1; i != e; i++)
947 Ops.push_back(EmitScalarExpr(E->getArg(i)));
948
949 llvm::APSInt Result;
950 const Expr *Arg = E->getArg(E->getNumArgs()-1);
951 if (!Arg->isIntegerConstantExpr(Result, getContext()))
952 return 0;
953
954 unsigned type = Result.getZExtValue();
955 bool usgn = type & 0x08;
956 bool quad = type & 0x10;
Nate Begeman548f7da2010-06-10 18:11:55 +0000957 bool poly = type & 0x20;
Nate Begemand075c012010-06-10 00:17:56 +0000958 bool splat = false;
Rafael Espindolae140af32010-06-09 03:48:40 +0000959
960 const llvm::Type *Ty = GetNeonType(VMContext, type & 0x7, quad);
961 if (!Ty)
962 return 0;
963
964 unsigned Int;
965 switch (BuiltinID) {
966 default: return 0;
Nate Begeman998622c2010-06-07 16:01:56 +0000967 case ARM::BI__builtin_neon_vaba_v:
Nate Begeman30d91712010-06-08 06:03:01 +0000968 case ARM::BI__builtin_neon_vabaq_v:
Nate Begeman998622c2010-06-07 16:01:56 +0000969 Int = usgn ? Intrinsic::arm_neon_vabau : Intrinsic::arm_neon_vabas;
Nate Begeman30d91712010-06-08 06:03:01 +0000970 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vaba");
971 case ARM::BI__builtin_neon_vabal_v:
Nate Begeman998622c2010-06-07 16:01:56 +0000972 Int = usgn ? Intrinsic::arm_neon_vabalu : Intrinsic::arm_neon_vabals;
Nate Begeman30d91712010-06-08 06:03:01 +0000973 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabal");
Nate Begeman998622c2010-06-07 16:01:56 +0000974 case ARM::BI__builtin_neon_vabd_v:
Nate Begeman30d91712010-06-08 06:03:01 +0000975 case ARM::BI__builtin_neon_vabdq_v:
Nate Begeman998622c2010-06-07 16:01:56 +0000976 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
Nate Begeman30d91712010-06-08 06:03:01 +0000977 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabd");
978 case ARM::BI__builtin_neon_vabdl_v:
Nate Begeman998622c2010-06-07 16:01:56 +0000979 Int = usgn ? Intrinsic::arm_neon_vabdlu : Intrinsic::arm_neon_vabdls;
Nate Begeman30d91712010-06-08 06:03:01 +0000980 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabdl");
Nate Begeman998622c2010-06-07 16:01:56 +0000981 case ARM::BI__builtin_neon_vabs_v:
Nate Begeman548f7da2010-06-10 18:11:55 +0000982 case ARM::BI__builtin_neon_vabsq_v:
983 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vabs, &Ty, 1),
984 Ops, "vabs");
985 case ARM::BI__builtin_neon_vaddhn_v:
986 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vaddhn, &Ty, 1),
987 Ops, "vaddhn");
Nate Begeman30d91712010-06-08 06:03:01 +0000988 case ARM::BI__builtin_neon_vaddl_v:
Nate Begeman998622c2010-06-07 16:01:56 +0000989 Int = usgn ? Intrinsic::arm_neon_vaddlu : Intrinsic::arm_neon_vaddls;
Nate Begeman30d91712010-06-08 06:03:01 +0000990 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vaddl");
991 case ARM::BI__builtin_neon_vaddw_v:
Nate Begeman998622c2010-06-07 16:01:56 +0000992 Int = usgn ? Intrinsic::arm_neon_vaddws : Intrinsic::arm_neon_vaddwu;
Nate Begeman30d91712010-06-08 06:03:01 +0000993 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vaddw");
Nate Begeman9eb65a52010-06-08 00:17:19 +0000994 case ARM::BI__builtin_neon_vcale_v:
995 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +0000996 case ARM::BI__builtin_neon_vcage_v: {
997 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacged, &Ty, 1);
998 return EmitNeonCall(F, Ops, "vcage");
999 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001000 case ARM::BI__builtin_neon_vcaleq_v:
1001 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001002 case ARM::BI__builtin_neon_vcageq_v: {
1003 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgeq, &Ty, 1);
1004 return EmitNeonCall(F, Ops, "vcage");
1005 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001006 case ARM::BI__builtin_neon_vcalt_v:
1007 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001008 case ARM::BI__builtin_neon_vcagt_v: {
1009 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtd, &Ty, 1);
1010 return EmitNeonCall(F, Ops, "vcagt");
1011 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001012 case ARM::BI__builtin_neon_vcaltq_v:
1013 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001014 case ARM::BI__builtin_neon_vcagtq_v: {
1015 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtq, &Ty, 1);
1016 return EmitNeonCall(F, Ops, "vcagt");
1017 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001018 case ARM::BI__builtin_neon_vcls_v:
1019 case ARM::BI__builtin_neon_vclsq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001020 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcls, &Ty, 1);
1021 return EmitNeonCall(F, Ops, "vcls");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001022 }
1023 case ARM::BI__builtin_neon_vclz_v:
1024 case ARM::BI__builtin_neon_vclzq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001025 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vclz, &Ty, 1);
1026 return EmitNeonCall(F, Ops, "vclz");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001027 }
1028 case ARM::BI__builtin_neon_vcnt_v:
1029 case ARM::BI__builtin_neon_vcntq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001030 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcnt, &Ty, 1);
1031 return EmitNeonCall(F, Ops, "vcnt");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001032 }
1033 // FIXME: intrinsics for f16<->f32 convert missing from ARM target.
1034 case ARM::BI__builtin_neon_vcvt_f32_v:
1035 case ARM::BI__builtin_neon_vcvtq_f32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001036 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1037 Ty = GetNeonType(VMContext, 4, quad);
Nate Begeman9eb65a52010-06-08 00:17:19 +00001038 return usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt")
1039 : Builder.CreateSIToFP(Ops[0], Ty, "vcvt");
1040 }
1041 case ARM::BI__builtin_neon_vcvt_s32_v:
1042 case ARM::BI__builtin_neon_vcvt_u32_v:
1043 case ARM::BI__builtin_neon_vcvtq_s32_v:
1044 case ARM::BI__builtin_neon_vcvtq_u32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001045 Ops[0] = Builder.CreateBitCast(Ops[0], GetNeonType(VMContext, 4, quad));
Nate Begeman9eb65a52010-06-08 00:17:19 +00001046 return usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt")
1047 : Builder.CreateFPToSI(Ops[0], Ty, "vcvt");
1048 }
1049 case ARM::BI__builtin_neon_vcvt_n_f32_v:
1050 case ARM::BI__builtin_neon_vcvtq_n_f32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001051 const llvm::Type *Tys[2] = { GetNeonType(VMContext, 4, quad), Ty };
Nate Begeman9eb65a52010-06-08 00:17:19 +00001052 Int = usgn ? Intrinsic::arm_neon_vcvtfxu2fp : Intrinsic::arm_neon_vcvtfxs2fp;
Nate Begeman30d91712010-06-08 06:03:01 +00001053 Function *F = CGM.getIntrinsic(Int, Tys, 2);
1054 return EmitNeonCall(F, Ops, "vcvt_n");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001055 }
1056 case ARM::BI__builtin_neon_vcvt_n_s32_v:
1057 case ARM::BI__builtin_neon_vcvt_n_u32_v:
1058 case ARM::BI__builtin_neon_vcvtq_n_s32_v:
1059 case ARM::BI__builtin_neon_vcvtq_n_u32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001060 const llvm::Type *Tys[2] = { Ty, GetNeonType(VMContext, 4, quad) };
Nate Begeman9eb65a52010-06-08 00:17:19 +00001061 Int = usgn ? Intrinsic::arm_neon_vcvtfp2fxu : Intrinsic::arm_neon_vcvtfp2fxs;
Nate Begeman30d91712010-06-08 06:03:01 +00001062 Function *F = CGM.getIntrinsic(Int, Tys, 2);
1063 return EmitNeonCall(F, Ops, "vcvt_n");
1064 }
1065 case ARM::BI__builtin_neon_vext_v:
1066 case ARM::BI__builtin_neon_vextq_v: {
1067 ConstantInt *C = dyn_cast<ConstantInt>(Ops[2]);
1068 int CV = C->getSExtValue();
Nate Begeman30d91712010-06-08 06:03:01 +00001069 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001070
1071 SmallVector<Constant*, 16> Indices;
Nate Begeman30d91712010-06-08 06:03:01 +00001072 for (unsigned i = 0, e = cast<llvm::VectorType>(Ty)->getNumElements();
1073 i != e; ++i)
1074 Indices.push_back(ConstantInt::get(I32Ty, i+CV));
1075
1076 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1077 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1078 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001079 return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vext");
1080 }
Nate Begeman95450f62010-06-09 05:30:26 +00001081 case ARM::BI__builtin_neon_vget_lane_i8:
1082 case ARM::BI__builtin_neon_vget_lane_i16:
1083 case ARM::BI__builtin_neon_vget_lane_i32:
1084 case ARM::BI__builtin_neon_vget_lane_i64:
1085 case ARM::BI__builtin_neon_vget_lane_f32:
1086 case ARM::BI__builtin_neon_vgetq_lane_i8:
1087 case ARM::BI__builtin_neon_vgetq_lane_i16:
1088 case ARM::BI__builtin_neon_vgetq_lane_i32:
1089 case ARM::BI__builtin_neon_vgetq_lane_i64:
1090 case ARM::BI__builtin_neon_vgetq_lane_f32:
Nate Begemandf98e1d2010-06-09 18:04:15 +00001091 return Builder.CreateExtractElement(Ops[0], EmitScalarExpr(E->getArg(1)),
1092 "vget_lane");
1093 case ARM::BI__builtin_neon_vhadd_v:
1094 case ARM::BI__builtin_neon_vhaddq_v:
1095 Int = usgn ? Intrinsic::arm_neon_vhaddu : Intrinsic::arm_neon_vhadds;
1096 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhadd");
1097 case ARM::BI__builtin_neon_vhsub_v:
1098 case ARM::BI__builtin_neon_vhsubq_v:
1099 Int = usgn ? Intrinsic::arm_neon_vhsubu : Intrinsic::arm_neon_vhsubs;
1100 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhsub");
1101 // FIXME: vld*
1102 case ARM::BI__builtin_neon_vmax_v:
1103 case ARM::BI__builtin_neon_vmaxq_v:
1104 Int = usgn ? Intrinsic::arm_neon_vmaxu : Intrinsic::arm_neon_vmaxs;
1105 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmax");
1106 case ARM::BI__builtin_neon_vmin_v:
1107 case ARM::BI__builtin_neon_vminq_v:
1108 Int = usgn ? Intrinsic::arm_neon_vminu : Intrinsic::arm_neon_vmins;
1109 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmin");
Nate Begemand075c012010-06-10 00:17:56 +00001110 case ARM::BI__builtin_neon_vmlal_lane_v:
1111 splat = true;
Nate Begemandf98e1d2010-06-09 18:04:15 +00001112 case ARM::BI__builtin_neon_vmlal_v:
1113 Int = usgn ? Intrinsic::arm_neon_vmlalu : Intrinsic::arm_neon_vmlals;
Nate Begemand075c012010-06-10 00:17:56 +00001114 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmlal", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001115 case ARM::BI__builtin_neon_vmlsl_lane_v:
1116 splat = true;
1117 case ARM::BI__builtin_neon_vmlsl_v:
1118 Int = usgn ? Intrinsic::arm_neon_vmlslu : Intrinsic::arm_neon_vmlsls;
1119 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmlsl", splat);
Nate Begemandf98e1d2010-06-09 18:04:15 +00001120 case ARM::BI__builtin_neon_vmovl_v:
1121 Int = usgn ? Intrinsic::arm_neon_vmovlu : Intrinsic::arm_neon_vmovls;
1122 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmovl");
1123 case ARM::BI__builtin_neon_vmovn_v:
1124 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vmovn, &Ty, 1),
1125 Ops, "vmovn");
Nate Begeman548f7da2010-06-10 18:11:55 +00001126 case ARM::BI__builtin_neon_vmull_lane_v:
1127 splat = true;
1128 case ARM::BI__builtin_neon_vmull_v:
1129 Int = usgn ? Intrinsic::arm_neon_vmullu : Intrinsic::arm_neon_vmulls;
1130 Int = poly ? (unsigned)Intrinsic::arm_neon_vmullp : Int;
1131 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmlal", splat);
Nate Begemandf98e1d2010-06-09 18:04:15 +00001132 case ARM::BI__builtin_neon_vpadal_v:
1133 case ARM::BI__builtin_neon_vpadalq_v:
1134 Int = usgn ? Intrinsic::arm_neon_vpadalu : Intrinsic::arm_neon_vpadals;
1135 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpadal");
Nate Begeman548f7da2010-06-10 18:11:55 +00001136 case ARM::BI__builtin_neon_vpadd_v:
1137 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vpadd, &Ty, 1),
1138 Ops, "vpadd");
1139 case ARM::BI__builtin_neon_vpaddl_v:
1140 case ARM::BI__builtin_neon_vpaddlq_v:
1141 Int = usgn ? Intrinsic::arm_neon_vpaddlu : Intrinsic::arm_neon_vpaddls;
1142 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpaddl");
1143 case ARM::BI__builtin_neon_vpmax_v:
1144 Int = usgn ? Intrinsic::arm_neon_vpmaxu : Intrinsic::arm_neon_vpmaxs;
1145 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmax");
1146 case ARM::BI__builtin_neon_vpmin_v:
1147 Int = usgn ? Intrinsic::arm_neon_vpminu : Intrinsic::arm_neon_vpmins;
1148 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmin");
1149 case ARM::BI__builtin_neon_vqabs_v:
1150 case ARM::BI__builtin_neon_vqabsq_v:
1151 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqabs, &Ty, 1),
1152 Ops, "vqabs");
1153 case ARM::BI__builtin_neon_vqadd_v:
1154 case ARM::BI__builtin_neon_vqaddq_v:
1155 Int = usgn ? Intrinsic::arm_neon_vqaddu : Intrinsic::arm_neon_vqadds;
1156 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqadd");
1157 case ARM::BI__builtin_neon_vqdmlal_lane_v:
1158 splat = true;
1159 case ARM::BI__builtin_neon_vqdmlal_v:
1160 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlal, &Ty, 1),
1161 Ops, "vqdmlal");
1162 case ARM::BI__builtin_neon_vqdmlsl_lane_v:
1163 splat = true;
1164 case ARM::BI__builtin_neon_vqdmlsl_v:
1165 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlsl, &Ty, 1),
1166 Ops, "vqdmlsl");
1167 case ARM::BI__builtin_neon_vqdmulh_lane_v:
1168 case ARM::BI__builtin_neon_vqdmulhq_lane_v:
1169 splat = true;
1170 case ARM::BI__builtin_neon_vqdmulh_v:
1171 case ARM::BI__builtin_neon_vqdmulhq_v:
1172 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmulh, &Ty, 1),
1173 Ops, "vqdmulh");
1174 case ARM::BI__builtin_neon_vqdmull_lane_v:
1175 splat = true;
1176 case ARM::BI__builtin_neon_vqdmull_v:
1177 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmull, &Ty, 1),
1178 Ops, "vqdmull");
1179 case ARM::BI__builtin_neon_vqmovn_v:
1180 Int = usgn ? Intrinsic::arm_neon_vqmovnu : Intrinsic::arm_neon_vqmovns;
1181 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqmovn");
1182 case ARM::BI__builtin_neon_vqmovun_v:
1183 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqmovnsu, &Ty, 1),
1184 Ops, "vqdmull");
1185 case ARM::BI__builtin_neon_vqneg_v:
Nate Begeman464ccb62010-06-11 22:57:12 +00001186 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqneg, &Ty, 1),
1187 Ops, "vqneg");
Nate Begeman548f7da2010-06-10 18:11:55 +00001188 case ARM::BI__builtin_neon_vqrdmulh_lane_v:
1189 case ARM::BI__builtin_neon_vqrdmulhq_lane_v:
1190 splat = true;
1191 case ARM::BI__builtin_neon_vqrdmulh_v:
1192 case ARM::BI__builtin_neon_vqrdmulhq_v:
1193 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrdmulh, &Ty, 1),
1194 Ops, "vqrdmulh");
1195 case ARM::BI__builtin_neon_vqrshl_v:
1196 case ARM::BI__builtin_neon_vqrshlq_v:
1197 Int = usgn ? Intrinsic::arm_neon_vqrshiftu : Intrinsic::arm_neon_vqrshifts;
1198 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshl");
1199 case ARM::BI__builtin_neon_vqrshrn_n_v:
1200 Int = usgn ? Intrinsic::arm_neon_vqrshiftnu : Intrinsic::arm_neon_vqrshiftns;
1201 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshrn_n");
1202 case ARM::BI__builtin_neon_vqrshrun_n_v:
1203 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrshiftnsu, &Ty, 1),
1204 Ops, "vqrshrun_n");
Nate Begeman464ccb62010-06-11 22:57:12 +00001205 case ARM::BI__builtin_neon_vqsub_v:
1206 case ARM::BI__builtin_neon_vqsubq_v:
1207 Int = usgn ? Intrinsic::arm_neon_vqsubu : Intrinsic::arm_neon_vqsubs;
1208 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqsub");
1209 case ARM::BI__builtin_neon_vraddhn_v:
1210 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vraddhn, &Ty, 1),
1211 Ops, "vraddhn");
1212 case ARM::BI__builtin_neon_vrecpe_v:
1213 case ARM::BI__builtin_neon_vrecpeq_v:
1214 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecpe, &Ty, 1),
1215 Ops, "vrecpe");
1216 case ARM::BI__builtin_neon_vrecps_v:
1217 case ARM::BI__builtin_neon_vrecpsq_v:
1218 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecps, &Ty, 1),
1219 Ops, "vrecps");
Nate Begeman464ccb62010-06-11 22:57:12 +00001220 case ARM::BI__builtin_neon_vrhadd_v:
1221 case ARM::BI__builtin_neon_vrhaddq_v:
1222 Int = usgn ? Intrinsic::arm_neon_vrhaddu : Intrinsic::arm_neon_vrhadds;
1223 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrhadd");
Nate Begeman5af93ef2010-06-12 06:06:07 +00001224 case ARM::BI__builtin_neon_vrshl_v:
1225 case ARM::BI__builtin_neon_vrshlq_v:
1226 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
1227 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshl");
1228 case ARM::BI__builtin_neon_vrshrn_n_v:
1229 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, true);
1230 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrshiftn, &Ty, 1),
1231 Ops, "vrshrn_n");
1232 case ARM::BI__builtin_neon_vrshr_n_v:
1233 case ARM::BI__builtin_neon_vrshrq_n_v:
1234 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, true);
1235 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
1236 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshr_n");
1237 case ARM::BI__builtin_neon_vrsqrte_v:
1238 case ARM::BI__builtin_neon_vrsqrteq_v:
1239 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrte, &Ty, 1),
1240 Ops, "vrsqrte");
1241 case ARM::BI__builtin_neon_vrsqrts_v:
1242 case ARM::BI__builtin_neon_vrsqrtsq_v:
1243 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrts, &Ty, 1),
1244 Ops, "vrsqrts");
1245 case ARM::BI__builtin_neon_vrsra_n_v:
1246 case ARM::BI__builtin_neon_vrsraq_n_v:
1247 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1248 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1249 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, true);
1250 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
1251 Ops[1] = Builder.CreateCall2(CGM.getIntrinsic(Int, &Ty, 1), Ops[1], Ops[2]);
1252 return Builder.CreateAdd(Ops[0], Ops[1], "vrsra_n");
Nate Begeman464ccb62010-06-11 22:57:12 +00001253 case ARM::BI__builtin_neon_vrsubhn_v:
1254 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsubhn, &Ty, 1),
1255 Ops, "vrsubhn");
Nate Begeman548f7da2010-06-10 18:11:55 +00001256 case ARM::BI__builtin_neon_vset_lane_i8:
1257 case ARM::BI__builtin_neon_vset_lane_i16:
1258 case ARM::BI__builtin_neon_vset_lane_i32:
1259 case ARM::BI__builtin_neon_vset_lane_i64:
1260 case ARM::BI__builtin_neon_vset_lane_f32:
1261 case ARM::BI__builtin_neon_vsetq_lane_i8:
1262 case ARM::BI__builtin_neon_vsetq_lane_i16:
1263 case ARM::BI__builtin_neon_vsetq_lane_i32:
1264 case ARM::BI__builtin_neon_vsetq_lane_i64:
1265 case ARM::BI__builtin_neon_vsetq_lane_f32:
1266 Ops.push_back(EmitScalarExpr(E->getArg(2)));
1267 return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vset_lane");
Nate Begeman464ccb62010-06-11 22:57:12 +00001268 case ARM::BI__builtin_neon_vshl_v:
1269 case ARM::BI__builtin_neon_vshlq_v:
1270 Int = usgn ? Intrinsic::arm_neon_vshiftu : Intrinsic::arm_neon_vshifts;
1271 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshl");
1272 case ARM::BI__builtin_neon_vshll_n_v:
1273 Ops[1] = EmitNeonShiftVector(Ops[1], Ty);
1274 Int = usgn ? Intrinsic::arm_neon_vshiftlu : Intrinsic::arm_neon_vshiftls;
1275 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshll");
1276 case ARM::BI__builtin_neon_vshl_n_v:
1277 case ARM::BI__builtin_neon_vshlq_n_v:
1278 Ops[1] = EmitNeonShiftVector(Ops[1], Ty);
1279 return Builder.CreateShl(Builder.CreateBitCast(Ops[0],Ty), Ops[1],"vshl_n");
1280 case ARM::BI__builtin_neon_vshrn_n_v:
1281 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, true);
1282 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftn, &Ty, 1),
1283 Ops, "vshrn_n");
1284 case ARM::BI__builtin_neon_vshr_n_v:
1285 case ARM::BI__builtin_neon_vshrq_n_v:
1286 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1287 Ops[1] = EmitNeonShiftVector(Ops[1], Ty);
1288 if (usgn)
1289 return Builder.CreateLShr(Ops[0], Ops[1], "vshr_n");
1290 else
1291 return Builder.CreateAShr(Ops[0], Ops[1], "vshr_n");
1292 case ARM::BI__builtin_neon_vsri_n_v:
1293 case ARM::BI__builtin_neon_vsriq_n_v:
1294 poly = true;
1295 case ARM::BI__builtin_neon_vsli_n_v:
1296 case ARM::BI__builtin_neon_vsliq_n_v:
1297 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, poly);
1298 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftins, &Ty, 1),
1299 Ops, "vsli_n");
1300 case ARM::BI__builtin_neon_vsra_n_v:
1301 case ARM::BI__builtin_neon_vsraq_n_v:
1302 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1303 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1304 Ops[2] = EmitNeonShiftVector(Ops[2], Ty);
1305 if (usgn)
1306 Ops[1] = Builder.CreateLShr(Ops[1], Ops[2], "vsra_n");
1307 else
1308 Ops[1] = Builder.CreateAShr(Ops[1], Ops[2], "vsra_n");
1309 return Builder.CreateAdd(Ops[0], Ops[1]);
1310 case ARM::BI__builtin_neon_vst1_v:
1311 case ARM::BI__builtin_neon_vst1q_v:
1312 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst1, &Ty, 1),
1313 Ops, "");
1314 case ARM::BI__builtin_neon_vst1_lane_v:
1315 case ARM::BI__builtin_neon_vst1q_lane_v:
1316 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1317 Ops[1] = Builder.CreateExtractElement(Ops[1], Ops[2]);
1318 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1319 return Builder.CreateStore(Ops[1], Builder.CreateBitCast(Ops[0], Ty));
1320 case ARM::BI__builtin_neon_vst2_v:
1321 case ARM::BI__builtin_neon_vst2q_v:
1322 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2, &Ty, 1),
1323 Ops, "");
1324 case ARM::BI__builtin_neon_vst2_lane_v:
1325 case ARM::BI__builtin_neon_vst2q_lane_v:
1326 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2lane, &Ty, 1),
1327 Ops, "");
1328 case ARM::BI__builtin_neon_vst3_v:
1329 case ARM::BI__builtin_neon_vst3q_v:
1330 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3, &Ty, 1),
1331 Ops, "");
1332 case ARM::BI__builtin_neon_vst3_lane_v:
1333 case ARM::BI__builtin_neon_vst3q_lane_v:
1334 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3lane, &Ty, 1),
1335 Ops, "");
1336 case ARM::BI__builtin_neon_vst4_v:
1337 case ARM::BI__builtin_neon_vst4q_v:
1338 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4, &Ty, 1),
1339 Ops, "");
1340 case ARM::BI__builtin_neon_vst4_lane_v:
1341 case ARM::BI__builtin_neon_vst4q_lane_v:
1342 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4lane, &Ty, 1),
1343 Ops, "");
Nate Begeman548f7da2010-06-10 18:11:55 +00001344 case ARM::BI__builtin_neon_vsubhn_v:
1345 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vsubhn, &Ty, 1),
1346 Ops, "vsubhn");
1347 case ARM::BI__builtin_neon_vsubl_v:
1348 Int = usgn ? Intrinsic::arm_neon_vsublu : Intrinsic::arm_neon_vsubls;
1349 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vsubl");
1350 case ARM::BI__builtin_neon_vsubw_v:
1351 Int = usgn ? Intrinsic::arm_neon_vsubws : Intrinsic::arm_neon_vsubwu;
1352 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vsubw");
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001353 case ARM::BI__builtin_neon_vtbl1_v:
1354 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl1),
1355 Ops, "vtbl1");
1356 case ARM::BI__builtin_neon_vtbl2_v:
1357 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl2),
1358 Ops, "vtbl2");
1359 case ARM::BI__builtin_neon_vtbl3_v:
1360 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl3),
1361 Ops, "vtbl3");
1362 case ARM::BI__builtin_neon_vtbl4_v:
1363 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl4),
1364 Ops, "vtbl4");
1365 case ARM::BI__builtin_neon_vtbx1_v:
1366 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx1),
1367 Ops, "vtbx1");
1368 case ARM::BI__builtin_neon_vtbx2_v:
1369 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx2),
1370 Ops, "vtbx2");
1371 case ARM::BI__builtin_neon_vtbx3_v:
1372 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx3),
1373 Ops, "vtbx3");
1374 case ARM::BI__builtin_neon_vtbx4_v:
1375 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx4),
1376 Ops, "vtbx4");
1377 case ARM::BI__builtin_neon_vtst_v:
1378 case ARM::BI__builtin_neon_vtstq_v: {
1379 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1380 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1381 Ops[0] = Builder.CreateAnd(Ops[0], Ops[1]);
1382 Ops[0] = Builder.CreateICmp(ICmpInst::ICMP_NE, Ops[0],
1383 ConstantAggregateZero::get(Ty));
1384 return Builder.CreateSExt(Ops[0], Ty, "vtst");
1385 }
1386 // FIXME: transpose/zip/unzip don't currently match patterns for
1387 // the non-q variants, but emitting 2 shufflevectors seems like a hack.
1388 case ARM::BI__builtin_neon_vtrn_v:
1389 case ARM::BI__builtin_neon_vtrnq_v: {
1390 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext);
1391 SmallVector<Constant*, 32> Indices;
1392 unsigned nElts = cast<llvm::VectorType>(Ty)->getNumElements();
1393 for (unsigned vi = 0; vi != 2; ++vi) {
1394 for (unsigned i = 0; i != nElts; i += 2) {
1395 Indices.push_back(ConstantInt::get(I32Ty, i+vi));
1396 Indices.push_back(ConstantInt::get(I32Ty, i+nElts+vi));
1397 }
1398 }
1399 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1400 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1401 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1402 return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vtrn");
1403 }
1404 case ARM::BI__builtin_neon_vuzp_v:
1405 case ARM::BI__builtin_neon_vuzpq_v: {
1406 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext);
1407 SmallVector<Constant*, 32> Indices;
1408 unsigned nElts = cast<llvm::VectorType>(Ty)->getNumElements();
1409 for (unsigned vi = 0; vi != 2; ++vi)
1410 for (unsigned i = 0; i != nElts; ++i)
1411 Indices.push_back(ConstantInt::get(I32Ty, 2*i+vi));
1412 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1413 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1414 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1415 return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vuzp");
1416 }
1417 case ARM::BI__builtin_neon_vzip_v:
1418 case ARM::BI__builtin_neon_vzipq_v: {
1419 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext);
1420 SmallVector<Constant*, 32> Indices;
1421 unsigned nElts = cast<llvm::VectorType>(Ty)->getNumElements();
1422 for (unsigned i = 0; i != nElts; ++i) {
1423 Indices.push_back(ConstantInt::get(I32Ty, i));
1424 Indices.push_back(ConstantInt::get(I32Ty, i+nElts));
1425 }
1426 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1427 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1428 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1429 return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vzip");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001430 }
Chris Lattner2752c012010-03-03 19:03:45 +00001431 }
1432}
1433
Mike Stump1eb44332009-09-09 15:08:12 +00001434Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +00001435 const CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001437 llvm::SmallVector<Value*, 4> Ops;
1438
1439 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
1440 Ops.push_back(EmitScalarExpr(E->getArg(i)));
1441
Anders Carlsson564f1de2007-12-09 23:17:02 +00001442 switch (BuiltinID) {
Anders Carlsson46a26b02007-12-09 23:39:18 +00001443 default: return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001444 case X86::BI__builtin_ia32_pslldi128:
Nate Begemane7722102008-04-14 04:49:57 +00001445 case X86::BI__builtin_ia32_psllqi128:
Mike Stump1eb44332009-09-09 15:08:12 +00001446 case X86::BI__builtin_ia32_psllwi128:
Nate Begemane7722102008-04-14 04:49:57 +00001447 case X86::BI__builtin_ia32_psradi128:
1448 case X86::BI__builtin_ia32_psrawi128:
1449 case X86::BI__builtin_ia32_psrldi128:
1450 case X86::BI__builtin_ia32_psrlqi128:
1451 case X86::BI__builtin_ia32_psrlwi128: {
Owen Anderson0032b272009-08-13 21:57:51 +00001452 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext");
1453 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 2);
1454 llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Owen Anderson03e20502009-07-30 23:11:26 +00001455 Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty),
Nate Begemane7722102008-04-14 04:49:57 +00001456 Ops[1], Zero, "insert");
1457 Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast");
1458 const char *name = 0;
1459 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Nate Begemane7722102008-04-14 04:49:57 +00001461 switch (BuiltinID) {
1462 default: assert(0 && "Unsupported shift intrinsic!");
1463 case X86::BI__builtin_ia32_pslldi128:
1464 name = "pslldi";
1465 ID = Intrinsic::x86_sse2_psll_d;
1466 break;
1467 case X86::BI__builtin_ia32_psllqi128:
1468 name = "psllqi";
1469 ID = Intrinsic::x86_sse2_psll_q;
1470 break;
1471 case X86::BI__builtin_ia32_psllwi128:
1472 name = "psllwi";
1473 ID = Intrinsic::x86_sse2_psll_w;
1474 break;
1475 case X86::BI__builtin_ia32_psradi128:
1476 name = "psradi";
1477 ID = Intrinsic::x86_sse2_psra_d;
1478 break;
1479 case X86::BI__builtin_ia32_psrawi128:
1480 name = "psrawi";
1481 ID = Intrinsic::x86_sse2_psra_w;
1482 break;
1483 case X86::BI__builtin_ia32_psrldi128:
1484 name = "psrldi";
1485 ID = Intrinsic::x86_sse2_psrl_d;
1486 break;
1487 case X86::BI__builtin_ia32_psrlqi128:
1488 name = "psrlqi";
1489 ID = Intrinsic::x86_sse2_psrl_q;
1490 break;
1491 case X86::BI__builtin_ia32_psrlwi128:
1492 name = "psrlwi";
1493 ID = Intrinsic::x86_sse2_psrl_w;
1494 break;
1495 }
1496 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001497 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Nate Begemane7722102008-04-14 04:49:57 +00001498 }
Mike Stump1eb44332009-09-09 15:08:12 +00001499 case X86::BI__builtin_ia32_pslldi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001500 case X86::BI__builtin_ia32_psllqi:
Mike Stump1eb44332009-09-09 15:08:12 +00001501 case X86::BI__builtin_ia32_psllwi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001502 case X86::BI__builtin_ia32_psradi:
1503 case X86::BI__builtin_ia32_psrawi:
1504 case X86::BI__builtin_ia32_psrldi:
1505 case X86::BI__builtin_ia32_psrlqi:
1506 case X86::BI__builtin_ia32_psrlwi: {
Owen Anderson0032b272009-08-13 21:57:51 +00001507 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext");
1508 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 1);
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001509 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001510 const char *name = 0;
1511 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001513 switch (BuiltinID) {
1514 default: assert(0 && "Unsupported shift intrinsic!");
1515 case X86::BI__builtin_ia32_pslldi:
1516 name = "pslldi";
1517 ID = Intrinsic::x86_mmx_psll_d;
1518 break;
1519 case X86::BI__builtin_ia32_psllqi:
1520 name = "psllqi";
1521 ID = Intrinsic::x86_mmx_psll_q;
1522 break;
1523 case X86::BI__builtin_ia32_psllwi:
1524 name = "psllwi";
1525 ID = Intrinsic::x86_mmx_psll_w;
1526 break;
1527 case X86::BI__builtin_ia32_psradi:
1528 name = "psradi";
1529 ID = Intrinsic::x86_mmx_psra_d;
1530 break;
1531 case X86::BI__builtin_ia32_psrawi:
1532 name = "psrawi";
1533 ID = Intrinsic::x86_mmx_psra_w;
1534 break;
1535 case X86::BI__builtin_ia32_psrldi:
1536 name = "psrldi";
1537 ID = Intrinsic::x86_mmx_psrl_d;
1538 break;
1539 case X86::BI__builtin_ia32_psrlqi:
1540 name = "psrlqi";
1541 ID = Intrinsic::x86_mmx_psrl_q;
1542 break;
1543 case X86::BI__builtin_ia32_psrlwi:
1544 name = "psrlwi";
1545 ID = Intrinsic::x86_mmx_psrl_w;
1546 break;
1547 }
Chris Lattner7acda7c2007-12-18 00:25:38 +00001548 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001549 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001550 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +00001551 case X86::BI__builtin_ia32_cmpps: {
1552 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps);
1553 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps");
1554 }
1555 case X86::BI__builtin_ia32_cmpss: {
1556 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss);
1557 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +00001558 }
Nate Begemane7722102008-04-14 04:49:57 +00001559 case X86::BI__builtin_ia32_ldmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001560 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Owen Anderson0032b272009-08-13 21:57:51 +00001561 Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1);
1562 Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +00001563 Builder.CreateStore(Ops[0], Tmp);
1564 return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +00001565 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +00001566 }
1567 case X86::BI__builtin_ia32_stmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001568 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Owen Anderson0032b272009-08-13 21:57:51 +00001569 Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1);
1570 Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +00001571 One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +00001572 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +00001573 return Builder.CreateLoad(Tmp, "stmxcsr");
1574 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +00001575 case X86::BI__builtin_ia32_cmppd: {
1576 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd);
1577 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd");
1578 }
1579 case X86::BI__builtin_ia32_cmpsd: {
1580 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd);
1581 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +00001582 }
Nate Begemane7722102008-04-14 04:49:57 +00001583 case X86::BI__builtin_ia32_storehps:
1584 case X86::BI__builtin_ia32_storelps: {
Owen Anderson0032b272009-08-13 21:57:51 +00001585 const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001586 llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy);
1587 llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Nate Begemane7722102008-04-14 04:49:57 +00001589 // cast val v2i64
1590 Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast");
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Nate Begemane7722102008-04-14 04:49:57 +00001592 // extract (0, 1)
1593 unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1;
Owen Anderson0032b272009-08-13 21:57:51 +00001594 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Index);
Nate Begemane7722102008-04-14 04:49:57 +00001595 Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract");
1596
1597 // cast pointer to i64 & store
1598 Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy);
1599 return Builder.CreateStore(Ops[1], Ops[0]);
1600 }
Eric Christopher91b59272009-12-01 05:00:51 +00001601 case X86::BI__builtin_ia32_palignr: {
Eric Christophere57aa9e2010-04-15 01:43:08 +00001602 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
1603
1604 // If palignr is shifting the pair of input vectors less than 9 bytes,
1605 // emit a shuffle instruction.
1606 if (shiftVal <= 8) {
1607 const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext);
1608
1609 llvm::SmallVector<llvm::Constant*, 8> Indices;
1610 for (unsigned i = 0; i != 8; ++i)
1611 Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i));
1612
1613 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1614 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
1615 }
1616
1617 // If palignr is shifting the pair of input vectors more than 8 but less
1618 // than 16 bytes, emit a logical right shift of the destination.
1619 if (shiftVal < 16) {
1620 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
1621 const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext);
1622 const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 1);
1623
1624 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
1625 Ops[1] = llvm::ConstantInt::get(VecTy, (shiftVal-8) * 8);
1626
1627 // create i32 constant
1628 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_mmx_psrl_q);
1629 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
1630 }
1631
1632 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
1633 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Nate Begemanc3420ff2009-12-14 05:15:02 +00001634 }
1635 case X86::BI__builtin_ia32_palignr128: {
Nate Begemance5818a2009-12-14 04:57:03 +00001636 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
1637
1638 // If palignr is shifting the pair of input vectors less than 17 bytes,
1639 // emit a shuffle instruction.
1640 if (shiftVal <= 16) {
1641 const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext);
1642
1643 llvm::SmallVector<llvm::Constant*, 16> Indices;
1644 for (unsigned i = 0; i != 16; ++i)
1645 Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i));
1646
1647 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1648 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
1649 }
1650
1651 // If palignr is shifting the pair of input vectors more than 16 but less
1652 // than 32 bytes, emit a logical right shift of the destination.
1653 if (shiftVal < 32) {
1654 const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext);
1655 const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2);
1656 const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext);
1657
1658 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
1659 Ops[1] = llvm::ConstantInt::get(IntTy, (shiftVal-16) * 8);
1660
1661 // create i32 constant
1662 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq);
1663 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
1664 }
1665
1666 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
1667 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Eric Christopher91b59272009-12-01 05:00:51 +00001668 }
Anders Carlsson564f1de2007-12-09 23:17:02 +00001669 }
1670}
1671
Mike Stump1eb44332009-09-09 15:08:12 +00001672Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +00001673 const CallExpr *E) {
Chris Lattnerdd173942010-04-14 03:54:58 +00001674 llvm::SmallVector<Value*, 4> Ops;
1675
1676 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
1677 Ops.push_back(EmitScalarExpr(E->getArg(i)));
1678
1679 Intrinsic::ID ID = Intrinsic::not_intrinsic;
1680
1681 switch (BuiltinID) {
1682 default: return 0;
1683
1684 // vec_st
1685 case PPC::BI__builtin_altivec_stvx:
1686 case PPC::BI__builtin_altivec_stvxl:
1687 case PPC::BI__builtin_altivec_stvebx:
1688 case PPC::BI__builtin_altivec_stvehx:
1689 case PPC::BI__builtin_altivec_stvewx:
1690 {
1691 Ops[2] = Builder.CreateBitCast(Ops[2], llvm::Type::getInt8PtrTy(VMContext));
1692 Ops[1] = !isa<Constant>(Ops[1]) || !cast<Constant>(Ops[1])->isNullValue()
1693 ? Builder.CreateGEP(Ops[2], Ops[1], "tmp") : Ops[2];
1694 Ops.pop_back();
1695
1696 switch (BuiltinID) {
1697 default: assert(0 && "Unsupported vavg intrinsic!");
1698 case PPC::BI__builtin_altivec_stvx:
1699 ID = Intrinsic::ppc_altivec_stvx;
1700 break;
1701 case PPC::BI__builtin_altivec_stvxl:
1702 ID = Intrinsic::ppc_altivec_stvxl;
1703 break;
1704 case PPC::BI__builtin_altivec_stvebx:
1705 ID = Intrinsic::ppc_altivec_stvebx;
1706 break;
1707 case PPC::BI__builtin_altivec_stvehx:
1708 ID = Intrinsic::ppc_altivec_stvehx;
1709 break;
1710 case PPC::BI__builtin_altivec_stvewx:
1711 ID = Intrinsic::ppc_altivec_stvewx;
1712 break;
1713 }
1714 llvm::Function *F = CGM.getIntrinsic(ID);
1715 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "");
1716 }
1717 }
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001718 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001719}