blob: 443b7c089f56f85dca282fcb1915594bb2321dfb [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"
Fariborz Jahanian55bcace2010-06-15 22:44:06 +000017#include "CGObjCRuntime.h"
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000018#include "clang/Basic/TargetInfo.h"
Chris Lattner1f329992008-10-06 06:09:18 +000019#include "clang/AST/APValue.h"
Chris Lattnerbef20ac2007-08-31 04:31:45 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000021#include "clang/AST/Decl.h"
Chris Lattner6b15cdc2009-06-14 01:05:48 +000022#include "clang/Basic/TargetBuiltins.h"
Anders Carlsson793680e2007-10-12 23:56:29 +000023#include "llvm/Intrinsics.h"
John McCalld0b76ca2010-03-02 03:50:12 +000024#include "llvm/Target/TargetData.h"
Anders Carlsson022012e2007-08-20 18:05:56 +000025using namespace clang;
26using namespace CodeGen;
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000027using namespace llvm;
28
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000029static void EmitMemoryBarrier(CodeGenFunction &CGF,
30 bool LoadLoad, bool LoadStore,
31 bool StoreLoad, bool StoreStore,
32 bool Device) {
33 Value *True = llvm::ConstantInt::getTrue(CGF.getLLVMContext());
34 Value *False = llvm::ConstantInt::getFalse(CGF.getLLVMContext());
35 Value *C[5] = { LoadLoad ? True : False,
36 LoadStore ? True : False,
37 StoreLoad ? True : False,
Chris Lattner780a2eb2010-09-21 23:35:30 +000038 StoreStore ? True : False,
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000039 Device ? True : False };
40 CGF.Builder.CreateCall(CGF.CGM.getIntrinsic(Intrinsic::memory_barrier),
41 C, C + 5);
42}
43
Chandler Carruthdb4325b2010-07-18 07:23:17 +000044static Value *EmitCastToInt(CodeGenFunction &CGF,
45 const llvm::Type *ToType, Value *Val) {
46 if (Val->getType()->isPointerTy()) {
47 return CGF.Builder.CreatePtrToInt(Val, ToType);
48 }
49 assert(Val->getType()->isIntegerTy() &&
50 "Used a non-integer and non-pointer type with atomic builtin");
51 assert(Val->getType()->getScalarSizeInBits() <=
52 ToType->getScalarSizeInBits() && "Integer type too small");
53 return CGF.Builder.CreateSExtOrBitCast(Val, ToType);
54}
55
56static Value *EmitCastFromInt(CodeGenFunction &CGF, QualType ToQualType,
57 Value *Val) {
58 const llvm::Type *ToType = CGF.ConvertType(ToQualType);
59 if (ToType->isPointerTy()) {
60 return CGF.Builder.CreateIntToPtr(Val, ToType);
61 }
62 assert(Val->getType()->isIntegerTy() &&
63 "Used a non-integer and non-pointer type with atomic builtin");
64 assert(Val->getType()->getScalarSizeInBits() >=
65 ToType->getScalarSizeInBits() && "Integer type too small");
66 return CGF.Builder.CreateTruncOrBitCast(Val, ToType);
67}
68
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000069// The atomic builtins are also full memory barriers. This is a utility for
70// wrapping a call to the builtins with memory barriers.
71static Value *EmitCallWithBarrier(CodeGenFunction &CGF, Value *Fn,
72 Value **ArgBegin, Value **ArgEnd) {
73 // FIXME: We need a target hook for whether this applies to device memory or
74 // not.
75 bool Device = true;
76
77 // Create barriers both before and after the call.
78 EmitMemoryBarrier(CGF, true, true, true, true, Device);
79 Value *Result = CGF.Builder.CreateCall(Fn, ArgBegin, ArgEnd);
80 EmitMemoryBarrier(CGF, true, true, true, true, Device);
81 return Result;
82}
83
Daniel Dunbar0002d232009-04-07 00:55:51 +000084/// Utility to insert an atomic instruction based on Instrinsic::ID
85/// and the expression node.
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000086static RValue EmitBinaryAtomic(CodeGenFunction &CGF,
Mon P Wang1ffe2812008-05-09 22:40:52 +000087 Intrinsic::ID Id, const CallExpr *E) {
Chris Lattner4f209442010-09-21 23:40:48 +000088 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
89 unsigned AddrSpace =
90 cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
Chandler Carruthdb4325b2010-07-18 07:23:17 +000091 const llvm::Type *ValueType =
92 llvm::IntegerType::get(CGF.getLLVMContext(),
93 CGF.getContext().getTypeSize(E->getType()));
Chris Lattner4f209442010-09-21 23:40:48 +000094 const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace);
Chandler Carruthdb4325b2010-07-18 07:23:17 +000095 const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType };
96 Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2);
97
Chris Lattner4f209442010-09-21 23:40:48 +000098 Value *Args[2] = { CGF.Builder.CreateBitCast(DestPtr, PtrType),
Chandler Carruthdb4325b2010-07-18 07:23:17 +000099 EmitCastToInt(CGF, ValueType,
100 CGF.EmitScalarExpr(E->getArg(1))) };
101 return RValue::get(EmitCastFromInt(CGF, E->getType(),
102 EmitCallWithBarrier(CGF, AtomF, Args,
103 Args + 2)));
Daniel Dunbar0002d232009-04-07 00:55:51 +0000104}
105
106/// Utility to insert an atomic instruction based Instrinsic::ID and
107// the expression node, where the return value is the result of the
108// operation.
Chris Lattner420b1182010-05-06 05:35:16 +0000109static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF,
Daniel Dunbar0002d232009-04-07 00:55:51 +0000110 Intrinsic::ID Id, const CallExpr *E,
111 Instruction::BinaryOps Op) {
Chris Lattner4f209442010-09-21 23:40:48 +0000112 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
113 unsigned AddrSpace =
114 cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
115
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000116 const llvm::Type *ValueType =
117 llvm::IntegerType::get(CGF.getLLVMContext(),
118 CGF.getContext().getTypeSize(E->getType()));
Chris Lattner4f209442010-09-21 23:40:48 +0000119 const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace);
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000120 const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType };
121 Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2);
122
Chris Lattner4f209442010-09-21 23:40:48 +0000123 Value *Args[2] = { CGF.Builder.CreateBitCast(DestPtr, PtrType),
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000124 EmitCastToInt(CGF, ValueType,
125 CGF.EmitScalarExpr(E->getArg(1))) };
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000126 Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 2);
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000127 return RValue::get(EmitCastFromInt(CGF, E->getType(),
128 CGF.Builder.CreateBinOp(Op, Result,
129 Args[1])));
Mon P Wang1ffe2812008-05-09 22:40:52 +0000130}
131
Chris Lattner420b1182010-05-06 05:35:16 +0000132/// EmitFAbs - Emit a call to fabs/fabsf/fabsl, depending on the type of ValTy,
133/// which must be a scalar floating point type.
134static Value *EmitFAbs(CodeGenFunction &CGF, Value *V, QualType ValTy) {
135 const BuiltinType *ValTyP = ValTy->getAs<BuiltinType>();
136 assert(ValTyP && "isn't scalar fp type!");
137
138 StringRef FnName;
139 switch (ValTyP->getKind()) {
140 default: assert(0 && "Isn't a scalar fp type!");
141 case BuiltinType::Float: FnName = "fabsf"; break;
142 case BuiltinType::Double: FnName = "fabs"; break;
143 case BuiltinType::LongDouble: FnName = "fabsl"; break;
144 }
145
146 // The prototype is something that takes and returns whatever V's type is.
147 std::vector<const llvm::Type*> Args;
148 Args.push_back(V->getType());
149 llvm::FunctionType *FT = llvm::FunctionType::get(V->getType(), Args, false);
150 llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction(FT, FnName);
151
152 return CGF.Builder.CreateCall(Fn, V, "abs");
153}
154
Mike Stump1eb44332009-09-09 15:08:12 +0000155RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000156 unsigned BuiltinID, const CallExpr *E) {
Chris Lattner564ea2a2008-10-06 06:56:41 +0000157 // See if we can constant fold this builtin. If so, don't emit it at all.
Anders Carlssonf35d35a2008-12-01 02:31:41 +0000158 Expr::EvalResult Result;
Chris Lattner6ee7aa12008-11-16 21:24:15 +0000159 if (E->Evaluate(Result, CGM.getContext())) {
Anders Carlssonf35d35a2008-12-01 02:31:41 +0000160 if (Result.Val.isInt())
Mike Stump1eb44332009-09-09 15:08:12 +0000161 return RValue::get(llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000162 Result.Val.getInt()));
Eli Friedman3941b182009-01-25 01:54:01 +0000163 else if (Result.Val.isFloat())
Owen Andersonbc0a2222009-07-27 21:00:51 +0000164 return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat()));
Chris Lattner1f329992008-10-06 06:09:18 +0000165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chris Lattner564ea2a2008-10-06 06:56:41 +0000167 switch (BuiltinID) {
168 default: break; // Handle intrinsics and libm functions below.
Chris Lattner506ff882008-10-06 07:26:43 +0000169 case Builtin::BI__builtin___CFStringMakeConstantString:
David Chisnall0d13f6f2010-01-23 02:40:42 +0000170 case Builtin::BI__builtin___NSStringMakeConstantString:
Anders Carlssone9352cc2009-04-08 04:48:15 +0000171 return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0));
Chris Lattner6a705f02008-07-09 17:28:44 +0000172 case Builtin::BI__builtin_stdarg_start:
Anders Carlsson793680e2007-10-12 23:56:29 +0000173 case Builtin::BI__builtin_va_start:
174 case Builtin::BI__builtin_va_end: {
Daniel Dunbar07855702009-02-11 22:25:55 +0000175 Value *ArgValue = EmitVAListRef(E->getArg(0));
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000176 const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson793680e2007-10-12 23:56:29 +0000177 if (ArgValue->getType() != DestType)
Mike Stump1eb44332009-09-09 15:08:12 +0000178 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
Daniel Dunbarb27ffbe2009-07-26 09:28:40 +0000179 ArgValue->getName().data());
Anders Carlsson793680e2007-10-12 23:56:29 +0000180
Mike Stump1eb44332009-09-09 15:08:12 +0000181 Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ?
Chris Lattner6a705f02008-07-09 17:28:44 +0000182 Intrinsic::vaend : Intrinsic::vastart;
Chris Lattner7acda7c2007-12-18 00:25:38 +0000183 return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue));
Anders Carlsson793680e2007-10-12 23:56:29 +0000184 }
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000185 case Builtin::BI__builtin_va_copy: {
Eli Friedman4fd0aa52009-01-20 17:46:04 +0000186 Value *DstPtr = EmitVAListRef(E->getArg(0));
187 Value *SrcPtr = EmitVAListRef(E->getArg(1));
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000188
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000189 const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000190
191 DstPtr = Builder.CreateBitCast(DstPtr, Type);
192 SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000193 return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000194 DstPtr, SrcPtr));
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000195 }
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000196 case Builtin::BI__builtin_abs: {
Mike Stump1eb44332009-09-09 15:08:12 +0000197 Value *ArgValue = EmitScalarExpr(E->getArg(0));
198
Chris Lattner9a847f52008-07-23 06:53:34 +0000199 Value *NegOp = Builder.CreateNeg(ArgValue, "neg");
Mike Stump1eb44332009-09-09 15:08:12 +0000200 Value *CmpResult =
201 Builder.CreateICmpSGE(ArgValue,
Owen Andersonc9c88b42009-07-31 20:28:54 +0000202 llvm::Constant::getNullValue(ArgValue->getType()),
Chris Lattner9a847f52008-07-23 06:53:34 +0000203 "abscond");
Mike Stump1eb44332009-09-09 15:08:12 +0000204 Value *Result =
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000205 Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000207 return RValue::get(Result);
208 }
Anders Carlsson3a31d602008-02-06 07:19:27 +0000209 case Builtin::BI__builtin_ctz:
210 case Builtin::BI__builtin_ctzl:
211 case Builtin::BI__builtin_ctzll: {
212 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Anders Carlsson3a31d602008-02-06 07:19:27 +0000214 const llvm::Type *ArgType = ArgValue->getType();
215 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
216
Mike Stump1eb44332009-09-09 15:08:12 +0000217 const llvm::Type *ResultType = ConvertType(E->getType());
Anders Carlsson3a31d602008-02-06 07:19:27 +0000218 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
219 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000220 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
221 "cast");
Anders Carlsson3a31d602008-02-06 07:19:27 +0000222 return RValue::get(Result);
223 }
Eli Friedmanf4e85332008-05-27 15:32:46 +0000224 case Builtin::BI__builtin_clz:
225 case Builtin::BI__builtin_clzl:
226 case Builtin::BI__builtin_clzll: {
227 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Eli Friedmanf4e85332008-05-27 15:32:46 +0000229 const llvm::Type *ArgType = ArgValue->getType();
230 Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1);
231
Mike Stump1eb44332009-09-09 15:08:12 +0000232 const llvm::Type *ResultType = ConvertType(E->getType());
Eli Friedmanf4e85332008-05-27 15:32:46 +0000233 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
234 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000235 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
236 "cast");
Eli Friedmanf4e85332008-05-27 15:32:46 +0000237 return RValue::get(Result);
238 }
Daniel Dunbar04b29002008-07-21 17:19:41 +0000239 case Builtin::BI__builtin_ffs:
240 case Builtin::BI__builtin_ffsl:
241 case Builtin::BI__builtin_ffsll: {
242 // ffs(x) -> x ? cttz(x) + 1 : 0
243 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Daniel Dunbar04b29002008-07-21 17:19:41 +0000245 const llvm::Type *ArgType = ArgValue->getType();
246 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Daniel Dunbar04b29002008-07-21 17:19:41 +0000248 const llvm::Type *ResultType = ConvertType(E->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000249 Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"),
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000250 llvm::ConstantInt::get(ArgType, 1), "tmp");
Owen Andersonc9c88b42009-07-31 20:28:54 +0000251 Value *Zero = llvm::Constant::getNullValue(ArgType);
Daniel Dunbar04b29002008-07-21 17:19:41 +0000252 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
253 Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
254 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000255 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
256 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000257 return RValue::get(Result);
258 }
259 case Builtin::BI__builtin_parity:
260 case Builtin::BI__builtin_parityl:
261 case Builtin::BI__builtin_parityll: {
262 // parity(x) -> ctpop(x) & 1
263 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Daniel Dunbar04b29002008-07-21 17:19:41 +0000265 const llvm::Type *ArgType = ArgValue->getType();
266 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Daniel Dunbar04b29002008-07-21 17:19:41 +0000268 const llvm::Type *ResultType = ConvertType(E->getType());
269 Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000270 Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1),
Daniel Dunbar04b29002008-07-21 17:19:41 +0000271 "tmp");
272 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000273 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
274 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000275 return RValue::get(Result);
276 }
277 case Builtin::BI__builtin_popcount:
278 case Builtin::BI__builtin_popcountl:
279 case Builtin::BI__builtin_popcountll: {
280 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Daniel Dunbar04b29002008-07-21 17:19:41 +0000282 const llvm::Type *ArgType = ArgValue->getType();
283 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Daniel Dunbar04b29002008-07-21 17:19:41 +0000285 const llvm::Type *ResultType = ConvertType(E->getType());
286 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
287 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000288 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
289 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000290 return RValue::get(Result);
291 }
Fariborz Jahaniane42b8a52010-07-26 23:11:03 +0000292 case Builtin::BI__builtin_expect: {
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000293 // FIXME: pass expect through to LLVM
Fariborz Jahaniane42b8a52010-07-26 23:11:03 +0000294 if (E->getArg(1)->HasSideEffects(getContext()))
295 (void)EmitScalarExpr(E->getArg(1));
Chris Lattner1feedd82007-12-13 07:34:23 +0000296 return RValue::get(EmitScalarExpr(E->getArg(0)));
Fariborz Jahaniane42b8a52010-07-26 23:11:03 +0000297 }
Anders Carlssondf4852a2007-12-02 21:58:10 +0000298 case Builtin::BI__builtin_bswap32:
299 case Builtin::BI__builtin_bswap64: {
Chris Lattner1feedd82007-12-13 07:34:23 +0000300 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000301 const llvm::Type *ArgType = ArgValue->getType();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000302 Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
Chris Lattner1feedd82007-12-13 07:34:23 +0000303 return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
Mike Stump1eb44332009-09-09 15:08:12 +0000304 }
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000305 case Builtin::BI__builtin_object_size: {
Mike Stumpb16d32f2009-10-26 23:39:48 +0000306 // We pass this builtin onto the optimizer so that it can
307 // figure out the object size in more complex cases.
Mike Stumpc4c90452009-10-27 22:09:17 +0000308 const llvm::Type *ResType[] = {
309 ConvertType(E->getType())
310 };
Eric Christopherfee667f2009-12-23 03:49:37 +0000311
312 // LLVM only supports 0 and 2, make sure that we pass along that
313 // as a boolean.
314 Value *Ty = EmitScalarExpr(E->getArg(1));
315 ConstantInt *CI = dyn_cast<ConstantInt>(Ty);
316 assert(CI);
317 uint64_t val = CI->getZExtValue();
318 CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1);
319
Mike Stumpc4c90452009-10-27 22:09:17 +0000320 Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1);
321 return RValue::get(Builder.CreateCall2(F,
322 EmitScalarExpr(E->getArg(0)),
Eric Christopherfee667f2009-12-23 03:49:37 +0000323 CI));
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000324 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000325 case Builtin::BI__builtin_prefetch: {
326 Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
327 // FIXME: Technically these constants should of type 'int', yes?
Mike Stump1eb44332009-09-09 15:08:12 +0000328 RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
Chris Lattner77b89b82010-06-27 07:15:29 +0000329 llvm::ConstantInt::get(Int32Ty, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000330 Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
Chris Lattner77b89b82010-06-27 07:15:29 +0000331 llvm::ConstantInt::get(Int32Ty, 3);
Daniel Dunbar4493f792008-07-21 22:59:13 +0000332 Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0);
333 return RValue::get(Builder.CreateCall3(F, Address, RW, Locality));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000334 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000335 case Builtin::BI__builtin_trap: {
336 Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0);
337 return RValue::get(Builder.CreateCall(F));
338 }
Chris Lattner21190d52009-09-21 03:09:59 +0000339 case Builtin::BI__builtin_unreachable: {
Mike Stumpfba565d2009-12-16 03:07:12 +0000340 if (CatchUndefined && HaveInsertPoint())
341 EmitBranch(getTrapBB());
Chris Lattner21190d52009-09-21 03:09:59 +0000342 Value *V = Builder.CreateUnreachable();
343 Builder.ClearInsertionPoint();
344 return RValue::get(V);
345 }
346
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000347 case Builtin::BI__builtin_powi:
348 case Builtin::BI__builtin_powif:
349 case Builtin::BI__builtin_powil: {
350 Value *Base = EmitScalarExpr(E->getArg(0));
351 Value *Exponent = EmitScalarExpr(E->getArg(1));
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000352 const llvm::Type *ArgType = Base->getType();
353 Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1);
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000354 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
355 }
356
Chris Lattnerfe23e212007-12-20 00:44:32 +0000357 case Builtin::BI__builtin_isgreater:
358 case Builtin::BI__builtin_isgreaterequal:
359 case Builtin::BI__builtin_isless:
360 case Builtin::BI__builtin_islessequal:
361 case Builtin::BI__builtin_islessgreater:
362 case Builtin::BI__builtin_isunordered: {
363 // Ordered comparisons: we know the arguments to these are matching scalar
364 // floating point values.
Mike Stump1eb44332009-09-09 15:08:12 +0000365 Value *LHS = EmitScalarExpr(E->getArg(0));
Chris Lattnerfe23e212007-12-20 00:44:32 +0000366 Value *RHS = EmitScalarExpr(E->getArg(1));
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattnerfe23e212007-12-20 00:44:32 +0000368 switch (BuiltinID) {
369 default: assert(0 && "Unknown ordered comparison");
370 case Builtin::BI__builtin_isgreater:
371 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
372 break;
373 case Builtin::BI__builtin_isgreaterequal:
374 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
375 break;
376 case Builtin::BI__builtin_isless:
377 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
378 break;
379 case Builtin::BI__builtin_islessequal:
380 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
381 break;
382 case Builtin::BI__builtin_islessgreater:
383 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
384 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000385 case Builtin::BI__builtin_isunordered:
Chris Lattnerfe23e212007-12-20 00:44:32 +0000386 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
387 break;
388 }
389 // ZExt bool to int type.
390 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()),
391 "tmp"));
392 }
Eli Friedmand6139892009-09-01 04:19:44 +0000393 case Builtin::BI__builtin_isnan: {
394 Value *V = EmitScalarExpr(E->getArg(0));
395 V = Builder.CreateFCmpUNO(V, V, "cmp");
396 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp"));
397 }
Chris Lattner420b1182010-05-06 05:35:16 +0000398
399 case Builtin::BI__builtin_isinf: {
400 // isinf(x) --> fabs(x) == infinity
401 Value *V = EmitScalarExpr(E->getArg(0));
402 V = EmitFAbs(*this, V, E->getArg(0)->getType());
403
404 V = Builder.CreateFCmpOEQ(V, ConstantFP::getInfinity(V->getType()),"isinf");
405 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp"));
406 }
Chris Lattner58ae5b42010-05-06 06:13:53 +0000407
408 // TODO: BI__builtin_isinf_sign
409 // isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0
Benjamin Kramer6349ce92010-05-19 11:24:26 +0000410
411 case Builtin::BI__builtin_isnormal: {
412 // isnormal(x) --> x == x && fabsf(x) < infinity && fabsf(x) >= float_min
413 Value *V = EmitScalarExpr(E->getArg(0));
414 Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
415
416 Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType());
417 Value *IsLessThanInf =
418 Builder.CreateFCmpULT(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
419 APFloat Smallest = APFloat::getSmallestNormalized(
420 getContext().getFloatTypeSemantics(E->getArg(0)->getType()));
421 Value *IsNormal =
422 Builder.CreateFCmpUGE(Abs, ConstantFP::get(V->getContext(), Smallest),
423 "isnormal");
424 V = Builder.CreateAnd(Eq, IsLessThanInf, "and");
425 V = Builder.CreateAnd(V, IsNormal, "and");
426 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
427 }
428
Chris Lattnered074152010-05-06 06:04:13 +0000429 case Builtin::BI__builtin_isfinite: {
430 // isfinite(x) --> x == x && fabs(x) != infinity; }
431 Value *V = EmitScalarExpr(E->getArg(0));
432 Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
433
434 Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType());
435 Value *IsNotInf =
436 Builder.CreateFCmpUNE(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
437
438 V = Builder.CreateAnd(Eq, IsNotInf, "and");
439 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
440 }
Benjamin Kramer7867f1a2010-06-14 10:30:41 +0000441
442 case Builtin::BI__builtin_fpclassify: {
443 Value *V = EmitScalarExpr(E->getArg(5));
444 const llvm::Type *Ty = ConvertType(E->getArg(5)->getType());
445
446 // Create Result
447 BasicBlock *Begin = Builder.GetInsertBlock();
448 BasicBlock *End = createBasicBlock("fpclassify_end", this->CurFn);
449 Builder.SetInsertPoint(End);
450 PHINode *Result =
451 Builder.CreatePHI(ConvertType(E->getArg(0)->getType()),
452 "fpclassify_result");
453
454 // if (V==0) return FP_ZERO
455 Builder.SetInsertPoint(Begin);
456 Value *IsZero = Builder.CreateFCmpOEQ(V, Constant::getNullValue(Ty),
457 "iszero");
458 Value *ZeroLiteral = EmitScalarExpr(E->getArg(4));
459 BasicBlock *NotZero = createBasicBlock("fpclassify_not_zero", this->CurFn);
460 Builder.CreateCondBr(IsZero, End, NotZero);
461 Result->addIncoming(ZeroLiteral, Begin);
462
463 // if (V != V) return FP_NAN
464 Builder.SetInsertPoint(NotZero);
465 Value *IsNan = Builder.CreateFCmpUNO(V, V, "cmp");
466 Value *NanLiteral = EmitScalarExpr(E->getArg(0));
467 BasicBlock *NotNan = createBasicBlock("fpclassify_not_nan", this->CurFn);
468 Builder.CreateCondBr(IsNan, End, NotNan);
469 Result->addIncoming(NanLiteral, NotZero);
470
471 // if (fabs(V) == infinity) return FP_INFINITY
472 Builder.SetInsertPoint(NotNan);
473 Value *VAbs = EmitFAbs(*this, V, E->getArg(5)->getType());
474 Value *IsInf =
475 Builder.CreateFCmpOEQ(VAbs, ConstantFP::getInfinity(V->getType()),
476 "isinf");
477 Value *InfLiteral = EmitScalarExpr(E->getArg(1));
478 BasicBlock *NotInf = createBasicBlock("fpclassify_not_inf", this->CurFn);
479 Builder.CreateCondBr(IsInf, End, NotInf);
480 Result->addIncoming(InfLiteral, NotNan);
481
482 // if (fabs(V) >= MIN_NORMAL) return FP_NORMAL else FP_SUBNORMAL
483 Builder.SetInsertPoint(NotInf);
484 APFloat Smallest = APFloat::getSmallestNormalized(
485 getContext().getFloatTypeSemantics(E->getArg(5)->getType()));
486 Value *IsNormal =
487 Builder.CreateFCmpUGE(VAbs, ConstantFP::get(V->getContext(), Smallest),
488 "isnormal");
489 Value *NormalResult =
490 Builder.CreateSelect(IsNormal, EmitScalarExpr(E->getArg(2)),
491 EmitScalarExpr(E->getArg(3)));
492 Builder.CreateBr(End);
493 Result->addIncoming(NormalResult, NotInf);
494
495 // return Result
496 Builder.SetInsertPoint(End);
497 return RValue::get(Result);
498 }
Chris Lattnered074152010-05-06 06:04:13 +0000499
Eli Friedmanb52fe9c2009-06-02 07:10:30 +0000500 case Builtin::BIalloca:
Chris Lattner9e800e32008-06-16 17:15:14 +0000501 case Builtin::BI__builtin_alloca: {
Chris Lattner9e800e32008-06-16 17:15:14 +0000502 Value *Size = EmitScalarExpr(E->getArg(0));
Owen Anderson0032b272009-08-13 21:57:51 +0000503 return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp"));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000504 }
Eli Friedmane6dddfd2010-01-23 19:00:10 +0000505 case Builtin::BIbzero:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000506 case Builtin::BI__builtin_bzero: {
507 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000508 Value *SizeVal = EmitScalarExpr(E->getArg(1));
509 Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()),
510 Address,
511 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0),
512 SizeVal,
Chris Lattner77b89b82010-06-27 07:15:29 +0000513 llvm::ConstantInt::get(Int32Ty, 1),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000514 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000515 return RValue::get(Address);
Chris Lattner9e800e32008-06-16 17:15:14 +0000516 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000517 case Builtin::BImemcpy:
Eli Friedmand4b32e42008-05-19 23:27:48 +0000518 case Builtin::BI__builtin_memcpy: {
Daniel Dunbar1caae952008-07-22 00:26:45 +0000519 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000520 Value *SrcAddr = EmitScalarExpr(E->getArg(1));
521 Value *SizeVal = EmitScalarExpr(E->getArg(2));
522 Builder.CreateCall5(CGM.getMemCpyFn(Address->getType(), SrcAddr->getType(),
523 SizeVal->getType()),
524 Address, SrcAddr, SizeVal,
Chris Lattner77b89b82010-06-27 07:15:29 +0000525 llvm::ConstantInt::get(Int32Ty, 1),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000526 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000527 return RValue::get(Address);
528 }
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000529
Fariborz Jahanian8e2eab22010-06-16 16:22:04 +0000530 case Builtin::BI__builtin_objc_memmove_collectable: {
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000531 Value *Address = EmitScalarExpr(E->getArg(0));
532 Value *SrcAddr = EmitScalarExpr(E->getArg(1));
533 Value *SizeVal = EmitScalarExpr(E->getArg(2));
534 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this,
535 Address, SrcAddr, SizeVal);
536 return RValue::get(Address);
537 }
538
Eli Friedmane6ec2052009-12-17 00:14:28 +0000539 case Builtin::BImemmove:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000540 case Builtin::BI__builtin_memmove: {
541 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000542 Value *SrcAddr = EmitScalarExpr(E->getArg(1));
543 Value *SizeVal = EmitScalarExpr(E->getArg(2));
544 Builder.CreateCall5(CGM.getMemMoveFn(Address->getType(), SrcAddr->getType(),
545 SizeVal->getType()),
546 Address, SrcAddr, SizeVal,
Chris Lattner77b89b82010-06-27 07:15:29 +0000547 llvm::ConstantInt::get(Int32Ty, 1),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000548 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000549 return RValue::get(Address);
550 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000551 case Builtin::BImemset:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000552 case Builtin::BI__builtin_memset: {
553 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000554 Value *SizeVal = EmitScalarExpr(E->getArg(2));
555 Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()),
556 Address,
557 Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
558 llvm::Type::getInt8Ty(VMContext)),
559 SizeVal,
Chris Lattner77b89b82010-06-27 07:15:29 +0000560 llvm::ConstantInt::get(Int32Ty, 1),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000561 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000562 return RValue::get(Address);
Eli Friedmand4b32e42008-05-19 23:27:48 +0000563 }
John McCallfb17a562010-03-03 10:30:05 +0000564 case Builtin::BI__builtin_dwarf_cfa: {
565 // The offset in bytes from the first argument to the CFA.
566 //
567 // Why on earth is this in the frontend? Is there any reason at
568 // all that the backend can't reasonably determine this while
569 // lowering llvm.eh.dwarf.cfa()?
570 //
571 // TODO: If there's a satisfactory reason, add a target hook for
572 // this instead of hard-coding 0, which is correct for most targets.
573 int32_t Offset = 0;
574
575 Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa, 0, 0);
Chris Lattner77b89b82010-06-27 07:15:29 +0000576 return RValue::get(Builder.CreateCall(F,
577 llvm::ConstantInt::get(Int32Ty, Offset)));
John McCallfb17a562010-03-03 10:30:05 +0000578 }
Eli Friedman256f77e2008-05-20 08:59:34 +0000579 case Builtin::BI__builtin_return_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000580 Value *Depth = EmitScalarExpr(E->getArg(0));
Chris Lattner77b89b82010-06-27 07:15:29 +0000581 Depth = Builder.CreateIntCast(Depth, Int32Ty, false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000582 Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000583 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000584 }
585 case Builtin::BI__builtin_frame_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000586 Value *Depth = EmitScalarExpr(E->getArg(0));
Chris Lattner77b89b82010-06-27 07:15:29 +0000587 Depth = Builder.CreateIntCast(Depth, Int32Ty, false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000588 Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000589 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000590 }
Eli Friedman3b660ef2009-05-03 19:23:23 +0000591 case Builtin::BI__builtin_extract_return_addr: {
John McCall492c4f92010-03-03 04:15:11 +0000592 Value *Address = EmitScalarExpr(E->getArg(0));
593 Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);
594 return RValue::get(Result);
595 }
596 case Builtin::BI__builtin_frob_return_addr: {
597 Value *Address = EmitScalarExpr(E->getArg(0));
598 Value *Result = getTargetHooks().encodeReturnAddress(*this, Address);
599 return RValue::get(Result);
Eli Friedman3b660ef2009-05-03 19:23:23 +0000600 }
John McCall6374c332010-03-06 00:35:14 +0000601 case Builtin::BI__builtin_dwarf_sp_column: {
602 const llvm::IntegerType *Ty
603 = cast<llvm::IntegerType>(ConvertType(E->getType()));
604 int Column = getTargetHooks().getDwarfEHStackPointer(CGM);
605 if (Column == -1) {
606 CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column");
607 return RValue::get(llvm::UndefValue::get(Ty));
608 }
609 return RValue::get(llvm::ConstantInt::get(Ty, Column, true));
610 }
611 case Builtin::BI__builtin_init_dwarf_reg_size_table: {
612 Value *Address = EmitScalarExpr(E->getArg(0));
613 if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address))
614 CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table");
615 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
616 }
John McCall7ada1112010-03-03 05:38:58 +0000617 case Builtin::BI__builtin_eh_return: {
618 Value *Int = EmitScalarExpr(E->getArg(0));
619 Value *Ptr = EmitScalarExpr(E->getArg(1));
620
621 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType());
622 assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) &&
623 "LLVM's __builtin_eh_return only supports 32- and 64-bit variants");
624 Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32
625 ? Intrinsic::eh_return_i32
626 : Intrinsic::eh_return_i64,
627 0, 0);
628 Builder.CreateCall2(F, Int, Ptr);
629 Value *V = Builder.CreateUnreachable();
630 Builder.ClearInsertionPoint();
631 return RValue::get(V);
632 }
Eli Friedmana6d75c02009-06-02 09:37:50 +0000633 case Builtin::BI__builtin_unwind_init: {
634 Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0);
635 return RValue::get(Builder.CreateCall(F));
636 }
John McCall5e110852010-03-02 02:31:24 +0000637 case Builtin::BI__builtin_extend_pointer: {
638 // Extends a pointer to the size of an _Unwind_Word, which is
John McCalld0b76ca2010-03-02 03:50:12 +0000639 // uint64_t on all platforms. Generally this gets poked into a
640 // register and eventually used as an address, so if the
641 // addressing registers are wider than pointers and the platform
642 // doesn't implicitly ignore high-order bits when doing
643 // addressing, we need to make sure we zext / sext based on
644 // the platform's expectations.
John McCall5e110852010-03-02 02:31:24 +0000645 //
646 // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html
John McCalld0b76ca2010-03-02 03:50:12 +0000647
648 LLVMContext &C = CGM.getLLVMContext();
649
650 // Cast the pointer to intptr_t.
John McCall5e110852010-03-02 02:31:24 +0000651 Value *Ptr = EmitScalarExpr(E->getArg(0));
John McCalld0b76ca2010-03-02 03:50:12 +0000652 const llvm::IntegerType *IntPtrTy = CGM.getTargetData().getIntPtrType(C);
653 Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast");
654
655 // If that's 64 bits, we're done.
656 if (IntPtrTy->getBitWidth() == 64)
657 return RValue::get(Result);
658
659 // Otherwise, ask the codegen data what to do.
John McCall492c4f92010-03-03 04:15:11 +0000660 if (getTargetHooks().extendPointerWithSExt())
John McCalld0b76ca2010-03-02 03:50:12 +0000661 return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext"));
662 else
663 return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext"));
John McCall5e110852010-03-02 02:31:24 +0000664 }
Eli Friedmana6d75c02009-06-02 09:37:50 +0000665 case Builtin::BI__builtin_setjmp: {
John McCall78673d92010-05-27 18:47:06 +0000666 // Buffer is a void**.
Eli Friedmana6d75c02009-06-02 09:37:50 +0000667 Value *Buf = EmitScalarExpr(E->getArg(0));
John McCall78673d92010-05-27 18:47:06 +0000668
669 // Store the frame pointer to the setjmp buffer.
Eli Friedmana6d75c02009-06-02 09:37:50 +0000670 Value *FrameAddr =
John McCall78673d92010-05-27 18:47:06 +0000671 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::frameaddress),
Chris Lattner77b89b82010-06-27 07:15:29 +0000672 ConstantInt::get(Int32Ty, 0));
Eli Friedmana6d75c02009-06-02 09:37:50 +0000673 Builder.CreateStore(FrameAddr, Buf);
John McCall78673d92010-05-27 18:47:06 +0000674
Jim Grosbach6d172e22010-05-27 23:54:20 +0000675 // Store the stack pointer to the setjmp buffer.
676 Value *StackAddr =
677 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));
678 Value *StackSaveSlot =
Chris Lattner77b89b82010-06-27 07:15:29 +0000679 Builder.CreateGEP(Buf, ConstantInt::get(Int32Ty, 2));
Jim Grosbach6d172e22010-05-27 23:54:20 +0000680 Builder.CreateStore(StackAddr, StackSaveSlot);
681
John McCall78673d92010-05-27 18:47:06 +0000682 // Call LLVM's EH setjmp, which is lightweight.
683 Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp);
684 Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext));
Eli Friedmana6d75c02009-06-02 09:37:50 +0000685 return RValue::get(Builder.CreateCall(F, Buf));
686 }
687 case Builtin::BI__builtin_longjmp: {
Eli Friedmana6d75c02009-06-02 09:37:50 +0000688 Value *Buf = EmitScalarExpr(E->getArg(0));
John McCall78673d92010-05-27 18:47:06 +0000689 Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext));
690
691 // Call LLVM's EH longjmp, which is lightweight.
692 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf);
693
694 // longjmp doesn't return; mark this as unreachable
695 Value *V = Builder.CreateUnreachable();
696 Builder.ClearInsertionPoint();
697 return RValue::get(V);
Eli Friedmana6d75c02009-06-02 09:37:50 +0000698 }
Mon P Wang1ffe2812008-05-09 22:40:52 +0000699 case Builtin::BI__sync_fetch_and_add:
Mon P Wang1ffe2812008-05-09 22:40:52 +0000700 case Builtin::BI__sync_fetch_and_sub:
Chris Lattner5caa3702009-05-08 06:58:22 +0000701 case Builtin::BI__sync_fetch_and_or:
702 case Builtin::BI__sync_fetch_and_and:
703 case Builtin::BI__sync_fetch_and_xor:
704 case Builtin::BI__sync_add_and_fetch:
705 case Builtin::BI__sync_sub_and_fetch:
706 case Builtin::BI__sync_and_and_fetch:
707 case Builtin::BI__sync_or_and_fetch:
708 case Builtin::BI__sync_xor_and_fetch:
709 case Builtin::BI__sync_val_compare_and_swap:
710 case Builtin::BI__sync_bool_compare_and_swap:
711 case Builtin::BI__sync_lock_test_and_set:
712 case Builtin::BI__sync_lock_release:
713 assert(0 && "Shouldn't make it through sema");
714 case Builtin::BI__sync_fetch_and_add_1:
715 case Builtin::BI__sync_fetch_and_add_2:
716 case Builtin::BI__sync_fetch_and_add_4:
717 case Builtin::BI__sync_fetch_and_add_8:
718 case Builtin::BI__sync_fetch_and_add_16:
719 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E);
720 case Builtin::BI__sync_fetch_and_sub_1:
721 case Builtin::BI__sync_fetch_and_sub_2:
722 case Builtin::BI__sync_fetch_and_sub_4:
723 case Builtin::BI__sync_fetch_and_sub_8:
724 case Builtin::BI__sync_fetch_and_sub_16:
Mon P Wang09b6bf52008-06-25 08:21:36 +0000725 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E);
Chris Lattner5caa3702009-05-08 06:58:22 +0000726 case Builtin::BI__sync_fetch_and_or_1:
727 case Builtin::BI__sync_fetch_and_or_2:
728 case Builtin::BI__sync_fetch_and_or_4:
729 case Builtin::BI__sync_fetch_and_or_8:
730 case Builtin::BI__sync_fetch_and_or_16:
731 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E);
732 case Builtin::BI__sync_fetch_and_and_1:
733 case Builtin::BI__sync_fetch_and_and_2:
734 case Builtin::BI__sync_fetch_and_and_4:
735 case Builtin::BI__sync_fetch_and_and_8:
736 case Builtin::BI__sync_fetch_and_and_16:
737 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E);
738 case Builtin::BI__sync_fetch_and_xor_1:
739 case Builtin::BI__sync_fetch_and_xor_2:
740 case Builtin::BI__sync_fetch_and_xor_4:
741 case Builtin::BI__sync_fetch_and_xor_8:
742 case Builtin::BI__sync_fetch_and_xor_16:
743 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattner5caa3702009-05-08 06:58:22 +0000745 // Clang extensions: not overloaded yet.
Mon P Wang1ffe2812008-05-09 22:40:52 +0000746 case Builtin::BI__sync_fetch_and_min:
747 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E);
748 case Builtin::BI__sync_fetch_and_max:
749 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E);
750 case Builtin::BI__sync_fetch_and_umin:
751 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E);
752 case Builtin::BI__sync_fetch_and_umax:
753 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000754
Chris Lattner5caa3702009-05-08 06:58:22 +0000755 case Builtin::BI__sync_add_and_fetch_1:
756 case Builtin::BI__sync_add_and_fetch_2:
757 case Builtin::BI__sync_add_and_fetch_4:
758 case Builtin::BI__sync_add_and_fetch_8:
759 case Builtin::BI__sync_add_and_fetch_16:
Mike Stump1eb44332009-09-09 15:08:12 +0000760 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E,
Daniel Dunbar0002d232009-04-07 00:55:51 +0000761 llvm::Instruction::Add);
Chris Lattner5caa3702009-05-08 06:58:22 +0000762 case Builtin::BI__sync_sub_and_fetch_1:
763 case Builtin::BI__sync_sub_and_fetch_2:
764 case Builtin::BI__sync_sub_and_fetch_4:
765 case Builtin::BI__sync_sub_and_fetch_8:
766 case Builtin::BI__sync_sub_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000767 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E,
768 llvm::Instruction::Sub);
Chris Lattner5caa3702009-05-08 06:58:22 +0000769 case Builtin::BI__sync_and_and_fetch_1:
770 case Builtin::BI__sync_and_and_fetch_2:
771 case Builtin::BI__sync_and_and_fetch_4:
772 case Builtin::BI__sync_and_and_fetch_8:
773 case Builtin::BI__sync_and_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000774 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E,
775 llvm::Instruction::And);
Chris Lattner5caa3702009-05-08 06:58:22 +0000776 case Builtin::BI__sync_or_and_fetch_1:
777 case Builtin::BI__sync_or_and_fetch_2:
778 case Builtin::BI__sync_or_and_fetch_4:
779 case Builtin::BI__sync_or_and_fetch_8:
780 case Builtin::BI__sync_or_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000781 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E,
782 llvm::Instruction::Or);
Chris Lattner5caa3702009-05-08 06:58:22 +0000783 case Builtin::BI__sync_xor_and_fetch_1:
784 case Builtin::BI__sync_xor_and_fetch_2:
785 case Builtin::BI__sync_xor_and_fetch_4:
786 case Builtin::BI__sync_xor_and_fetch_8:
787 case Builtin::BI__sync_xor_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000788 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E,
789 llvm::Instruction::Xor);
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Chris Lattner5caa3702009-05-08 06:58:22 +0000791 case Builtin::BI__sync_val_compare_and_swap_1:
792 case Builtin::BI__sync_val_compare_and_swap_2:
793 case Builtin::BI__sync_val_compare_and_swap_4:
794 case Builtin::BI__sync_val_compare_and_swap_8:
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000795 case Builtin::BI__sync_val_compare_and_swap_16: {
Chris Lattner780a2eb2010-09-21 23:35:30 +0000796 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
797 unsigned AddrSpace =
Chris Lattner4f209442010-09-21 23:40:48 +0000798 cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000799 const llvm::Type *ValueType =
800 llvm::IntegerType::get(CGF.getLLVMContext(),
801 CGF.getContext().getTypeSize(E->getType()));
Chris Lattner780a2eb2010-09-21 23:35:30 +0000802 const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace);
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000803 const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType };
804 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap,
805 IntrinsicTypes, 2);
806
Chris Lattner780a2eb2010-09-21 23:35:30 +0000807 Value *Args[3] = { Builder.CreateBitCast(DestPtr, PtrType),
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000808 EmitCastToInt(CGF, ValueType,
809 CGF.EmitScalarExpr(E->getArg(1))),
810 EmitCastToInt(CGF, ValueType,
811 CGF.EmitScalarExpr(E->getArg(2))) };
812 return RValue::get(EmitCastFromInt(CGF, E->getType(),
813 EmitCallWithBarrier(CGF, AtomF, Args,
814 Args + 3)));
Anders Carlsson89799cf2007-10-29 02:59:40 +0000815 }
Daniel Dunbar0002d232009-04-07 00:55:51 +0000816
Chris Lattner5caa3702009-05-08 06:58:22 +0000817 case Builtin::BI__sync_bool_compare_and_swap_1:
818 case Builtin::BI__sync_bool_compare_and_swap_2:
819 case Builtin::BI__sync_bool_compare_and_swap_4:
820 case Builtin::BI__sync_bool_compare_and_swap_8:
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000821 case Builtin::BI__sync_bool_compare_and_swap_16: {
Chris Lattnerf2b95272010-09-21 23:24:52 +0000822 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
823 unsigned AddrSpace =
Chris Lattner4f209442010-09-21 23:40:48 +0000824 cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000825 const llvm::Type *ValueType =
Chris Lattnerf2b95272010-09-21 23:24:52 +0000826 llvm::IntegerType::get(CGF.getLLVMContext(),
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000827 CGF.getContext().getTypeSize(E->getArg(1)->getType()));
Chris Lattnerf2b95272010-09-21 23:24:52 +0000828 const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace);
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000829 const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType };
830 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap,
831 IntrinsicTypes, 2);
832
Chris Lattnerf2b95272010-09-21 23:24:52 +0000833 Value *Args[3] = { Builder.CreateBitCast(DestPtr, PtrType),
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000834 EmitCastToInt(CGF, ValueType,
835 CGF.EmitScalarExpr(E->getArg(1))),
836 EmitCastToInt(CGF, ValueType,
837 CGF.EmitScalarExpr(E->getArg(2))) };
838 Value *OldVal = Args[1];
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000839 Value *PrevVal = EmitCallWithBarrier(*this, AtomF, Args, Args + 3);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000840 Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal);
841 // zext bool to int.
842 return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
843 }
844
Chris Lattner5caa3702009-05-08 06:58:22 +0000845 case Builtin::BI__sync_lock_test_and_set_1:
846 case Builtin::BI__sync_lock_test_and_set_2:
847 case Builtin::BI__sync_lock_test_and_set_4:
848 case Builtin::BI__sync_lock_test_and_set_8:
849 case Builtin::BI__sync_lock_test_and_set_16:
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000850 return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E);
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000851
Chris Lattner5caa3702009-05-08 06:58:22 +0000852 case Builtin::BI__sync_lock_release_1:
853 case Builtin::BI__sync_lock_release_2:
854 case Builtin::BI__sync_lock_release_4:
855 case Builtin::BI__sync_lock_release_8:
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000856 case Builtin::BI__sync_lock_release_16: {
857 Value *Ptr = EmitScalarExpr(E->getArg(0));
858 const llvm::Type *ElTy =
859 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar007b5672009-11-29 21:11:47 +0000860 llvm::StoreInst *Store =
861 Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr);
862 Store->setVolatile(true);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000863 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000864 }
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000865
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000866 case Builtin::BI__sync_synchronize: {
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000867 // We assume like gcc appears to, that this only applies to cached memory.
868 EmitMemoryBarrier(*this, true, true, true, true, false);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000869 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000870 }
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Tanya Lattner0b571642010-01-16 01:21:14 +0000872 case Builtin::BI__builtin_llvm_memory_barrier: {
873 Value *C[5] = {
874 EmitScalarExpr(E->getArg(0)),
875 EmitScalarExpr(E->getArg(1)),
876 EmitScalarExpr(E->getArg(2)),
877 EmitScalarExpr(E->getArg(3)),
878 EmitScalarExpr(E->getArg(4))
879 };
880 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5);
881 return RValue::get(0);
882 }
883
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000884 // Library functions with special handling.
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000885 case Builtin::BIsqrt:
886 case Builtin::BIsqrtf:
887 case Builtin::BIsqrtl: {
John McCallbeb41282010-04-07 08:20:20 +0000888 // TODO: there is currently no set of optimizer flags
889 // sufficient for us to rewrite sqrt to @llvm.sqrt.
890 // -fmath-errno=0 is not good enough; we need finiteness.
891 // We could probably precondition the call with an ult
892 // against 0, but is that worth the complexity?
893 break;
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000894 }
895
896 case Builtin::BIpow:
897 case Builtin::BIpowf:
898 case Builtin::BIpowl: {
899 // Rewrite sqrt to intrinsic if allowed.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000900 if (!FD->hasAttr<ConstAttr>())
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000901 break;
902 Value *Base = EmitScalarExpr(E->getArg(0));
903 Value *Exponent = EmitScalarExpr(E->getArg(1));
904 const llvm::Type *ArgType = Base->getType();
905 Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1);
906 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
907 }
Eli Friedmanba68b082010-03-06 02:17:52 +0000908
909 case Builtin::BI__builtin_signbit:
910 case Builtin::BI__builtin_signbitf:
911 case Builtin::BI__builtin_signbitl: {
912 LLVMContext &C = CGM.getLLVMContext();
913
914 Value *Arg = EmitScalarExpr(E->getArg(0));
915 const llvm::Type *ArgTy = Arg->getType();
916 if (ArgTy->isPPC_FP128Ty())
917 break; // FIXME: I'm not sure what the right implementation is here.
918 int ArgWidth = ArgTy->getPrimitiveSizeInBits();
919 const llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth);
920 Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy);
921 Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy);
922 Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp);
923 return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
924 }
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000927 // If this is an alias for a libm function (e.g. __builtin_sin) turn it into
928 // that function.
Douglas Gregor3e41d602009-02-13 23:20:09 +0000929 if (getContext().BuiltinInfo.isLibFunction(BuiltinID) ||
930 getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
Anders Carlsson31777a22009-12-24 19:08:58 +0000931 return EmitCall(E->getCallee()->getType(),
932 CGM.getBuiltinLibFunction(FD, BuiltinID),
Anders Carlssond2490a92009-12-24 20:40:36 +0000933 ReturnValueSlot(),
Anders Carlsson31777a22009-12-24 19:08:58 +0000934 E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000936 // See if we have a target specific intrinsic.
Dale Johannesena6f80ef2009-02-05 01:50:47 +0000937 const char *Name = getContext().BuiltinInfo.GetName(BuiltinID);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000938 Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
939 if (const char *Prefix =
Mike Stump1eb44332009-09-09 15:08:12 +0000940 llvm::Triple::getArchTypePrefix(Target.getTriple().getArch()))
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000941 IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000943 if (IntrinsicID != Intrinsic::not_intrinsic) {
944 SmallVector<Value*, 16> Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000946 Function *F = CGM.getIntrinsic(IntrinsicID);
947 const llvm::FunctionType *FTy = F->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000949 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
950 Value *ArgValue = EmitScalarExpr(E->getArg(i));
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000952 // If the intrinsic arg type is different from the builtin arg type
953 // we need to do a bit cast.
954 const llvm::Type *PTy = FTy->getParamType(i);
955 if (PTy != ArgValue->getType()) {
956 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
957 "Must be able to losslessly bit cast to param");
958 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
959 }
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000961 Args.push_back(ArgValue);
962 }
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Jay Foadbeaaccd2009-05-21 09:52:38 +0000964 Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size());
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000965 QualType BuiltinRetType = E->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Owen Anderson0032b272009-08-13 21:57:51 +0000967 const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000968 if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000970 if (RetTy != V->getType()) {
971 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
972 "Must be able to losslessly bit cast result type");
973 V = Builder.CreateBitCast(V, RetTy);
974 }
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000976 return RValue::get(V);
977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000979 // See if we have a target specific builtin that needs to be lowered.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000980 if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E))
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000981 return RValue::get(V);
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Daniel Dunbar488e9932008-08-16 00:56:44 +0000983 ErrorUnsupported(E, "builtin function");
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000985 // Unknown builtin, for now just dump it out and return undef.
986 if (hasAggregateLLVMType(E->getType()))
Daniel Dunbar195337d2010-02-09 02:48:28 +0000987 return RValue::getAggregate(CreateMemTemp(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +0000988 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
Mike Stump1eb44332009-09-09 15:08:12 +0000989}
Anders Carlsson564f1de2007-12-09 23:17:02 +0000990
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000991Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID,
992 const CallExpr *E) {
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000993 switch (Target.getTriple().getArch()) {
Chris Lattner2752c012010-03-03 19:03:45 +0000994 case llvm::Triple::arm:
995 case llvm::Triple::thumb:
996 return EmitARMBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000997 case llvm::Triple::x86:
998 case llvm::Triple::x86_64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000999 return EmitX86BuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001000 case llvm::Triple::ppc:
1001 case llvm::Triple::ppc64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001002 return EmitPPCBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001003 default:
1004 return 0;
1005 }
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001006}
1007
Nate Begeman4be54302010-06-20 23:05:28 +00001008const llvm::VectorType *GetNeonType(LLVMContext &C, unsigned type, bool q) {
Nate Begeman998622c2010-06-07 16:01:56 +00001009 switch (type) {
1010 default: break;
1011 case 0:
Nate Begeman4be54302010-06-20 23:05:28 +00001012 case 5: return llvm::VectorType::get(llvm::Type::getInt8Ty(C), 8 << (int)q);
Nate Begeman998622c2010-06-07 16:01:56 +00001013 case 6:
1014 case 7:
Nate Begeman4be54302010-06-20 23:05:28 +00001015 case 1: return llvm::VectorType::get(llvm::Type::getInt16Ty(C),4 << (int)q);
1016 case 2: return llvm::VectorType::get(llvm::Type::getInt32Ty(C),2 << (int)q);
1017 case 3: return llvm::VectorType::get(llvm::Type::getInt64Ty(C),1 << (int)q);
1018 case 4: return llvm::VectorType::get(llvm::Type::getFloatTy(C),2 << (int)q);
Nate Begeman998622c2010-06-07 16:01:56 +00001019 };
1020 return 0;
1021}
1022
Nate Begemancd480ad2010-08-06 01:24:57 +00001023Value *CodeGenFunction::EmitNeonSplat(Value *V, Constant *C, bool widen) {
Nate Begemand075c012010-06-10 00:17:56 +00001024 unsigned nElts = cast<llvm::VectorType>(V->getType())->getNumElements();
Nate Begemancd480ad2010-08-06 01:24:57 +00001025 if (widen)
1026 nElts <<= 1;
Nate Begemand075c012010-06-10 00:17:56 +00001027 SmallVector<Constant*, 16> Indices(nElts, C);
1028 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1029 return Builder.CreateShuffleVector(V, V, SV, "lane");
1030}
1031
Nate Begeman30d91712010-06-08 06:03:01 +00001032Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl<Value*> &Ops,
Nate Begeman61eecf52010-06-14 05:21:25 +00001033 const char *name, bool splat,
1034 unsigned shift, bool rightshift) {
Nate Begeman30d91712010-06-08 06:03:01 +00001035 unsigned j = 0;
1036 for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
1037 ai != ae; ++ai, ++j)
Nate Begeman61eecf52010-06-14 05:21:25 +00001038 if (shift > 0 && shift == j)
1039 Ops[j] = EmitNeonShiftVector(Ops[j], ai->getType(), rightshift);
1040 else
1041 Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name);
Nate Begeman30d91712010-06-08 06:03:01 +00001042
Nate Begemand075c012010-06-10 00:17:56 +00001043 if (splat) {
1044 Ops[j-1] = EmitNeonSplat(Ops[j-1], cast<Constant>(Ops[j]));
1045 Ops.resize(j);
1046 }
Nate Begeman30d91712010-06-08 06:03:01 +00001047 return Builder.CreateCall(F, Ops.begin(), Ops.end(), name);
1048}
1049
Nate Begeman464ccb62010-06-11 22:57:12 +00001050Value *CodeGenFunction::EmitNeonShiftVector(Value *V, const llvm::Type *Ty,
1051 bool neg) {
1052 ConstantInt *CI = cast<ConstantInt>(V);
1053 int SV = CI->getSExtValue();
1054
1055 const llvm::VectorType *VTy = cast<llvm::VectorType>(Ty);
1056 llvm::Constant *C = ConstantInt::get(VTy->getElementType(), neg ? -SV : SV);
1057 SmallVector<llvm::Constant*, 16> CV(VTy->getNumElements(), C);
1058 return llvm::ConstantVector::get(CV.begin(), CV.size());
1059}
1060
Bob Wilson06b6c582010-08-27 17:14:29 +00001061/// GetPointeeAlignment - Given an expression with a pointer type, find the
1062/// alignment of the type referenced by the pointer. Skip over implicit
1063/// casts.
1064static Value *GetPointeeAlignment(CodeGenFunction &CGF, const Expr *Addr) {
1065 unsigned Align = 1;
1066 // Check if the type is a pointer. The implicit cast operand might not be.
1067 while (Addr->getType()->isPointerType()) {
1068 QualType PtTy = Addr->getType()->getPointeeType();
1069 unsigned NewA = CGF.getContext().getTypeAlignInChars(PtTy).getQuantity();
1070 if (NewA > Align)
1071 Align = NewA;
1072
1073 // If the address is an implicit cast, repeat with the cast operand.
1074 if (const ImplicitCastExpr *CastAddr = dyn_cast<ImplicitCastExpr>(Addr)) {
1075 Addr = CastAddr->getSubExpr();
1076 continue;
1077 }
1078 break;
1079 }
1080 return llvm::ConstantInt::get(CGF.Int32Ty, Align);
1081}
1082
Chris Lattner2752c012010-03-03 19:03:45 +00001083Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
1084 const CallExpr *E) {
Rafael Espindolae140af32010-06-09 03:48:40 +00001085 if (BuiltinID == ARM::BI__clear_cache) {
Rafael Espindola79ba5092010-06-07 17:26:50 +00001086 const FunctionDecl *FD = E->getDirectCallee();
1087 Value *a = EmitScalarExpr(E->getArg(0));
1088 Value *b = EmitScalarExpr(E->getArg(1));
1089 const llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType());
1090 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
1091 llvm::StringRef Name = FD->getName();
1092 return Builder.CreateCall2(CGM.CreateRuntimeFunction(FTy, Name),
1093 a, b);
Chris Lattner2752c012010-03-03 19:03:45 +00001094 }
Rafael Espindolae140af32010-06-09 03:48:40 +00001095
Nate Begemand075c012010-06-10 00:17:56 +00001096 llvm::SmallVector<Value*, 4> Ops;
Rafael Espindolae140af32010-06-09 03:48:40 +00001097 for (unsigned i = 0, e = E->getNumArgs() - 1; i != e; i++)
1098 Ops.push_back(EmitScalarExpr(E->getArg(i)));
1099
1100 llvm::APSInt Result;
1101 const Expr *Arg = E->getArg(E->getNumArgs()-1);
1102 if (!Arg->isIntegerConstantExpr(Result, getContext()))
1103 return 0;
1104
Nate Begeman99c40bb2010-08-03 21:32:34 +00001105 if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f ||
1106 BuiltinID == ARM::BI__builtin_arm_vcvtr_d) {
1107 // Determine the overloaded type of this builtin.
1108 const llvm::Type *Ty;
1109 if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f)
1110 Ty = llvm::Type::getFloatTy(VMContext);
1111 else
1112 Ty = llvm::Type::getDoubleTy(VMContext);
1113
1114 // Determine whether this is an unsigned conversion or not.
1115 bool usgn = Result.getZExtValue() == 1;
1116 unsigned Int = usgn ? Intrinsic::arm_vcvtru : Intrinsic::arm_vcvtr;
1117
1118 // Call the appropriate intrinsic.
1119 Function *F = CGM.getIntrinsic(Int, &Ty, 1);
1120 return Builder.CreateCall(F, Ops.begin(), Ops.end(), "vcvtr");
1121 }
1122
1123 // Determine the type of this overloaded NEON intrinsic.
Rafael Espindolae140af32010-06-09 03:48:40 +00001124 unsigned type = Result.getZExtValue();
1125 bool usgn = type & 0x08;
1126 bool quad = type & 0x10;
Nate Begeman0d15c532010-06-13 04:47:52 +00001127 bool poly = (type & 0x7) == 5 || (type & 0x7) == 6;
Nate Begemand075c012010-06-10 00:17:56 +00001128 bool splat = false;
Rafael Espindolae140af32010-06-09 03:48:40 +00001129
Nate Begeman4be54302010-06-20 23:05:28 +00001130 const llvm::VectorType *VTy = GetNeonType(VMContext, type & 0x7, quad);
1131 const llvm::Type *Ty = VTy;
Rafael Espindolae140af32010-06-09 03:48:40 +00001132 if (!Ty)
1133 return 0;
1134
1135 unsigned Int;
1136 switch (BuiltinID) {
1137 default: return 0;
Nate Begeman998622c2010-06-07 16:01:56 +00001138 case ARM::BI__builtin_neon_vaba_v:
Bob Wilson47a88fa2010-09-03 01:27:09 +00001139 case ARM::BI__builtin_neon_vabaq_v: {
1140 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1141 SmallVector<Value*, 2> Args;
1142 Args.push_back(Ops[1]);
1143 Args.push_back(Ops[2]);
1144 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
1145 Ops[1] = EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Args, "vaba");
1146 return Builder.CreateAdd(Ops[0], Ops[1], "vaba");
1147 }
1148 case ARM::BI__builtin_neon_vabal_v: {
1149 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1150 SmallVector<Value*, 2> Args;
1151 Args.push_back(Ops[1]);
1152 Args.push_back(Ops[2]);
1153 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
1154 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1155 Ops[1] = EmitNeonCall(CGM.getIntrinsic(Int, &DTy, 1), Args, "vabal");
1156 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1157 return Builder.CreateAdd(Ops[0], Ops[1], "vabal");
1158 }
Nate Begeman998622c2010-06-07 16:01:56 +00001159 case ARM::BI__builtin_neon_vabd_v:
Nate Begeman30d91712010-06-08 06:03:01 +00001160 case ARM::BI__builtin_neon_vabdq_v:
Nate Begeman998622c2010-06-07 16:01:56 +00001161 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
Nate Begeman30d91712010-06-08 06:03:01 +00001162 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabd");
Bob Wilson47a88fa2010-09-03 01:27:09 +00001163 case ARM::BI__builtin_neon_vabdl_v: {
1164 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
1165 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1166 Ops[0] = EmitNeonCall(CGM.getIntrinsic(Int, &DTy, 1), Ops, "vabdl");
1167 return Builder.CreateZExt(Ops[0], Ty, "vabdl");
1168 }
Nate Begeman998622c2010-06-07 16:01:56 +00001169 case ARM::BI__builtin_neon_vabs_v:
Nate Begeman548f7da2010-06-10 18:11:55 +00001170 case ARM::BI__builtin_neon_vabsq_v:
1171 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vabs, &Ty, 1),
1172 Ops, "vabs");
1173 case ARM::BI__builtin_neon_vaddhn_v:
1174 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vaddhn, &Ty, 1),
1175 Ops, "vaddhn");
Bob Wilson22359412010-09-02 22:37:30 +00001176 case ARM::BI__builtin_neon_vaddl_v: {
1177 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1178 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
1179 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilson54160c62010-08-29 05:14:28 +00001180 if (usgn) {
1181 Ops[0] = Builder.CreateZExt(Ops[0], Ty);
1182 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1183 } else {
1184 Ops[0] = Builder.CreateSExt(Ops[0], Ty);
1185 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1186 }
1187 return Builder.CreateAdd(Ops[0], Ops[1], "vaddl");
Bob Wilson22359412010-09-02 22:37:30 +00001188 }
1189 case ARM::BI__builtin_neon_vaddw_v: {
1190 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1191 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1192 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilson54160c62010-08-29 05:14:28 +00001193 if (usgn)
1194 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1195 else
1196 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1197 return Builder.CreateAdd(Ops[0], Ops[1], "vaddw");
Bob Wilson22359412010-09-02 22:37:30 +00001198 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001199 case ARM::BI__builtin_neon_vcale_v:
1200 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001201 case ARM::BI__builtin_neon_vcage_v: {
1202 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacged, &Ty, 1);
1203 return EmitNeonCall(F, Ops, "vcage");
1204 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001205 case ARM::BI__builtin_neon_vcaleq_v:
1206 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001207 case ARM::BI__builtin_neon_vcageq_v: {
1208 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgeq, &Ty, 1);
1209 return EmitNeonCall(F, Ops, "vcage");
1210 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001211 case ARM::BI__builtin_neon_vcalt_v:
1212 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001213 case ARM::BI__builtin_neon_vcagt_v: {
1214 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtd, &Ty, 1);
1215 return EmitNeonCall(F, Ops, "vcagt");
1216 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001217 case ARM::BI__builtin_neon_vcaltq_v:
1218 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001219 case ARM::BI__builtin_neon_vcagtq_v: {
1220 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtq, &Ty, 1);
1221 return EmitNeonCall(F, Ops, "vcagt");
1222 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001223 case ARM::BI__builtin_neon_vcls_v:
1224 case ARM::BI__builtin_neon_vclsq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001225 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcls, &Ty, 1);
1226 return EmitNeonCall(F, Ops, "vcls");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001227 }
1228 case ARM::BI__builtin_neon_vclz_v:
1229 case ARM::BI__builtin_neon_vclzq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001230 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vclz, &Ty, 1);
1231 return EmitNeonCall(F, Ops, "vclz");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001232 }
1233 case ARM::BI__builtin_neon_vcnt_v:
1234 case ARM::BI__builtin_neon_vcntq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001235 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcnt, &Ty, 1);
1236 return EmitNeonCall(F, Ops, "vcnt");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001237 }
1238 // FIXME: intrinsics for f16<->f32 convert missing from ARM target.
1239 case ARM::BI__builtin_neon_vcvt_f32_v:
1240 case ARM::BI__builtin_neon_vcvtq_f32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001241 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1242 Ty = GetNeonType(VMContext, 4, quad);
Nate Begeman9eb65a52010-06-08 00:17:19 +00001243 return usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt")
1244 : Builder.CreateSIToFP(Ops[0], Ty, "vcvt");
1245 }
1246 case ARM::BI__builtin_neon_vcvt_s32_v:
1247 case ARM::BI__builtin_neon_vcvt_u32_v:
1248 case ARM::BI__builtin_neon_vcvtq_s32_v:
1249 case ARM::BI__builtin_neon_vcvtq_u32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001250 Ops[0] = Builder.CreateBitCast(Ops[0], GetNeonType(VMContext, 4, quad));
Nate Begeman9eb65a52010-06-08 00:17:19 +00001251 return usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt")
1252 : Builder.CreateFPToSI(Ops[0], Ty, "vcvt");
1253 }
1254 case ARM::BI__builtin_neon_vcvt_n_f32_v:
1255 case ARM::BI__builtin_neon_vcvtq_n_f32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001256 const llvm::Type *Tys[2] = { GetNeonType(VMContext, 4, quad), Ty };
Nate Begeman9eb65a52010-06-08 00:17:19 +00001257 Int = usgn ? Intrinsic::arm_neon_vcvtfxu2fp : Intrinsic::arm_neon_vcvtfxs2fp;
Nate Begeman30d91712010-06-08 06:03:01 +00001258 Function *F = CGM.getIntrinsic(Int, Tys, 2);
1259 return EmitNeonCall(F, Ops, "vcvt_n");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001260 }
1261 case ARM::BI__builtin_neon_vcvt_n_s32_v:
1262 case ARM::BI__builtin_neon_vcvt_n_u32_v:
1263 case ARM::BI__builtin_neon_vcvtq_n_s32_v:
1264 case ARM::BI__builtin_neon_vcvtq_n_u32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001265 const llvm::Type *Tys[2] = { Ty, GetNeonType(VMContext, 4, quad) };
Nate Begeman9eb65a52010-06-08 00:17:19 +00001266 Int = usgn ? Intrinsic::arm_neon_vcvtfp2fxu : Intrinsic::arm_neon_vcvtfp2fxs;
Nate Begeman30d91712010-06-08 06:03:01 +00001267 Function *F = CGM.getIntrinsic(Int, Tys, 2);
1268 return EmitNeonCall(F, Ops, "vcvt_n");
1269 }
Nate Begemancd480ad2010-08-06 01:24:57 +00001270 case ARM::BI__builtin_neon_vdup_lane_v:
1271 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1272 return EmitNeonSplat(Ops[0], cast<Constant>(Ops[1]));
1273 case ARM::BI__builtin_neon_vdupq_lane_v:
1274 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1275 return EmitNeonSplat(Ops[0], cast<Constant>(Ops[1]), true);
Nate Begeman30d91712010-06-08 06:03:01 +00001276 case ARM::BI__builtin_neon_vext_v:
1277 case ARM::BI__builtin_neon_vextq_v: {
1278 ConstantInt *C = dyn_cast<ConstantInt>(Ops[2]);
1279 int CV = C->getSExtValue();
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001280 SmallVector<Constant*, 16> Indices;
Nate Begeman4be54302010-06-20 23:05:28 +00001281 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
Chris Lattner77b89b82010-06-27 07:15:29 +00001282 Indices.push_back(ConstantInt::get(Int32Ty, i+CV));
Nate Begeman30d91712010-06-08 06:03:01 +00001283
1284 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1285 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1286 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001287 return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vext");
1288 }
Nate Begeman95450f62010-06-09 05:30:26 +00001289 case ARM::BI__builtin_neon_vget_lane_i8:
1290 case ARM::BI__builtin_neon_vget_lane_i16:
1291 case ARM::BI__builtin_neon_vget_lane_i32:
1292 case ARM::BI__builtin_neon_vget_lane_i64:
1293 case ARM::BI__builtin_neon_vget_lane_f32:
1294 case ARM::BI__builtin_neon_vgetq_lane_i8:
1295 case ARM::BI__builtin_neon_vgetq_lane_i16:
1296 case ARM::BI__builtin_neon_vgetq_lane_i32:
1297 case ARM::BI__builtin_neon_vgetq_lane_i64:
1298 case ARM::BI__builtin_neon_vgetq_lane_f32:
Nate Begemandf98e1d2010-06-09 18:04:15 +00001299 return Builder.CreateExtractElement(Ops[0], EmitScalarExpr(E->getArg(1)),
1300 "vget_lane");
1301 case ARM::BI__builtin_neon_vhadd_v:
1302 case ARM::BI__builtin_neon_vhaddq_v:
1303 Int = usgn ? Intrinsic::arm_neon_vhaddu : Intrinsic::arm_neon_vhadds;
1304 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhadd");
1305 case ARM::BI__builtin_neon_vhsub_v:
1306 case ARM::BI__builtin_neon_vhsubq_v:
1307 Int = usgn ? Intrinsic::arm_neon_vhsubu : Intrinsic::arm_neon_vhsubs;
1308 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhsub");
Nate Begeman4be54302010-06-20 23:05:28 +00001309 case ARM::BI__builtin_neon_vld1_v:
1310 case ARM::BI__builtin_neon_vld1q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001311 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman4be54302010-06-20 23:05:28 +00001312 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vld1, &Ty, 1),
1313 Ops, "vld1");
1314 case ARM::BI__builtin_neon_vld1_lane_v:
1315 case ARM::BI__builtin_neon_vld1q_lane_v:
1316 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1317 Ty = llvm::PointerType::getUnqual(VTy->getElementType());
1318 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1319 Ops[0] = Builder.CreateLoad(Ops[0]);
1320 return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vld1_lane");
1321 case ARM::BI__builtin_neon_vld1_dup_v:
1322 case ARM::BI__builtin_neon_vld1q_dup_v: {
1323 Value *V = UndefValue::get(Ty);
1324 Ty = llvm::PointerType::getUnqual(VTy->getElementType());
1325 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1326 Ops[0] = Builder.CreateLoad(Ops[0]);
Chris Lattner77b89b82010-06-27 07:15:29 +00001327 llvm::Constant *CI = ConstantInt::get(Int32Ty, 0);
Nate Begeman4be54302010-06-20 23:05:28 +00001328 Ops[0] = Builder.CreateInsertElement(V, Ops[0], CI);
1329 return EmitNeonSplat(Ops[0], CI);
1330 }
1331 case ARM::BI__builtin_neon_vld2_v:
1332 case ARM::BI__builtin_neon_vld2q_v: {
1333 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2, &Ty, 1);
Bob Wilson06b6c582010-08-27 17:14:29 +00001334 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1335 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld2");
Nate Begeman4be54302010-06-20 23:05:28 +00001336 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1337 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1338 return Builder.CreateStore(Ops[1], Ops[0]);
1339 }
1340 case ARM::BI__builtin_neon_vld3_v:
1341 case ARM::BI__builtin_neon_vld3q_v: {
1342 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3, &Ty, 1);
Bob Wilson06b6c582010-08-27 17:14:29 +00001343 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1344 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld3");
Nate Begeman4be54302010-06-20 23:05:28 +00001345 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1346 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1347 return Builder.CreateStore(Ops[1], Ops[0]);
1348 }
1349 case ARM::BI__builtin_neon_vld4_v:
1350 case ARM::BI__builtin_neon_vld4q_v: {
1351 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4, &Ty, 1);
Bob Wilson06b6c582010-08-27 17:14:29 +00001352 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1353 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld4");
Nate Begeman4be54302010-06-20 23:05:28 +00001354 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1355 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1356 return Builder.CreateStore(Ops[1], Ops[0]);
1357 }
1358 case ARM::BI__builtin_neon_vld2_lane_v:
1359 case ARM::BI__builtin_neon_vld2q_lane_v: {
1360 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2lane, &Ty, 1);
1361 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
1362 Ops[3] = Builder.CreateBitCast(Ops[3], Ty);
Bob Wilson06b6c582010-08-27 17:14:29 +00001363 Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001364 Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld2_lane");
1365 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1366 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1367 return Builder.CreateStore(Ops[1], Ops[0]);
1368 }
1369 case ARM::BI__builtin_neon_vld3_lane_v:
1370 case ARM::BI__builtin_neon_vld3q_lane_v: {
1371 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3lane, &Ty, 1);
1372 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
1373 Ops[3] = Builder.CreateBitCast(Ops[3], Ty);
1374 Ops[4] = Builder.CreateBitCast(Ops[4], Ty);
Bob Wilson06b6c582010-08-27 17:14:29 +00001375 Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001376 Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane");
1377 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1378 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1379 return Builder.CreateStore(Ops[1], Ops[0]);
1380 }
1381 case ARM::BI__builtin_neon_vld4_lane_v:
1382 case ARM::BI__builtin_neon_vld4q_lane_v: {
1383 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4lane, &Ty, 1);
1384 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
1385 Ops[3] = Builder.CreateBitCast(Ops[3], Ty);
1386 Ops[4] = Builder.CreateBitCast(Ops[4], Ty);
1387 Ops[5] = Builder.CreateBitCast(Ops[5], Ty);
Bob Wilson06b6c582010-08-27 17:14:29 +00001388 Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001389 Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane");
1390 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1391 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1392 return Builder.CreateStore(Ops[1], Ops[0]);
1393 }
1394 case ARM::BI__builtin_neon_vld2_dup_v:
1395 case ARM::BI__builtin_neon_vld3_dup_v:
1396 case ARM::BI__builtin_neon_vld4_dup_v: {
1397 switch (BuiltinID) {
1398 case ARM::BI__builtin_neon_vld2_dup_v:
1399 Int = Intrinsic::arm_neon_vld2lane;
1400 break;
1401 case ARM::BI__builtin_neon_vld3_dup_v:
1402 Int = Intrinsic::arm_neon_vld2lane;
1403 break;
1404 case ARM::BI__builtin_neon_vld4_dup_v:
1405 Int = Intrinsic::arm_neon_vld2lane;
1406 break;
1407 default: assert(0 && "unknown vld_dup intrinsic?");
1408 }
1409 Function *F = CGM.getIntrinsic(Int, &Ty, 1);
1410 const llvm::StructType *STy = cast<llvm::StructType>(F->getReturnType());
1411
1412 SmallVector<Value*, 6> Args;
1413 Args.push_back(Ops[1]);
1414 Args.append(STy->getNumElements(), UndefValue::get(Ty));
1415
Chris Lattner77b89b82010-06-27 07:15:29 +00001416 llvm::Constant *CI = ConstantInt::get(Int32Ty, 0);
Nate Begeman4be54302010-06-20 23:05:28 +00001417 Args.push_back(CI);
Bob Wilson06b6c582010-08-27 17:14:29 +00001418 Args.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001419
1420 Ops[1] = Builder.CreateCall(F, Args.begin(), Args.end(), "vld_dup");
1421 // splat lane 0 to all elts in each vector of the result.
1422 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1423 Value *Val = Builder.CreateExtractValue(Ops[1], i);
1424 Value *Elt = Builder.CreateBitCast(Val, Ty);
1425 Elt = EmitNeonSplat(Elt, CI);
1426 Elt = Builder.CreateBitCast(Elt, Val->getType());
1427 Ops[1] = Builder.CreateInsertValue(Ops[1], Elt, i);
1428 }
1429 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1430 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1431 return Builder.CreateStore(Ops[1], Ops[0]);
1432 }
Nate Begemandf98e1d2010-06-09 18:04:15 +00001433 case ARM::BI__builtin_neon_vmax_v:
1434 case ARM::BI__builtin_neon_vmaxq_v:
1435 Int = usgn ? Intrinsic::arm_neon_vmaxu : Intrinsic::arm_neon_vmaxs;
1436 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmax");
1437 case ARM::BI__builtin_neon_vmin_v:
1438 case ARM::BI__builtin_neon_vminq_v:
1439 Int = usgn ? Intrinsic::arm_neon_vminu : Intrinsic::arm_neon_vmins;
1440 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmin");
Bob Wilson22359412010-09-02 22:37:30 +00001441 case ARM::BI__builtin_neon_vmlal_lane_v: {
1442 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1443 Ops[2] = Builder.CreateBitCast(Ops[2], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001444 Ops[2] = EmitNeonSplat(Ops[2], cast<Constant>(Ops[3]));
Bob Wilson22359412010-09-02 22:37:30 +00001445 }
1446 case ARM::BI__builtin_neon_vmlal_v: {
1447 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1448 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1449 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
1450 Ops[2] = Builder.CreateBitCast(Ops[2], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001451 if (usgn) {
1452 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1453 Ops[2] = Builder.CreateZExt(Ops[2], Ty);
1454 } else {
1455 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1456 Ops[2] = Builder.CreateSExt(Ops[2], Ty);
1457 }
1458 Ops[1] = Builder.CreateMul(Ops[1], Ops[2]);
1459 return Builder.CreateAdd(Ops[0], Ops[1], "vmlal");
Bob Wilson22359412010-09-02 22:37:30 +00001460 }
1461 case ARM::BI__builtin_neon_vmlsl_lane_v: {
1462 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1463 Ops[2] = Builder.CreateBitCast(Ops[2], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001464 Ops[2] = EmitNeonSplat(Ops[2], cast<Constant>(Ops[3]));
Bob Wilson22359412010-09-02 22:37:30 +00001465 }
1466 case ARM::BI__builtin_neon_vmlsl_v: {
1467 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1468 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1469 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
1470 Ops[2] = Builder.CreateBitCast(Ops[2], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001471 if (usgn) {
1472 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1473 Ops[2] = Builder.CreateZExt(Ops[2], Ty);
1474 } else {
1475 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1476 Ops[2] = Builder.CreateSExt(Ops[2], Ty);
1477 }
1478 Ops[1] = Builder.CreateMul(Ops[1], Ops[2]);
1479 return Builder.CreateSub(Ops[0], Ops[1], "vmlsl");
Bob Wilson22359412010-09-02 22:37:30 +00001480 }
1481 case ARM::BI__builtin_neon_vmovl_v: {
1482 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1483 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
Bob Wilson7cea3222010-08-20 03:36:08 +00001484 if (usgn)
1485 return Builder.CreateZExt(Ops[0], Ty, "vmovl");
1486 return Builder.CreateSExt(Ops[0], Ty, "vmovl");
Bob Wilson22359412010-09-02 22:37:30 +00001487 }
1488 case ARM::BI__builtin_neon_vmovn_v: {
1489 const llvm::Type *QTy = llvm::VectorType::getExtendedElementVectorType(VTy);
1490 Ops[0] = Builder.CreateBitCast(Ops[0], QTy);
Bob Wilson3b6081b2010-08-30 19:57:13 +00001491 return Builder.CreateTrunc(Ops[0], Ty, "vmovn");
Bob Wilson22359412010-09-02 22:37:30 +00001492 }
1493 case ARM::BI__builtin_neon_vmull_lane_v: {
1494 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1495 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001496 Ops[1] = EmitNeonSplat(Ops[1], cast<Constant>(Ops[2]));
Bob Wilson22359412010-09-02 22:37:30 +00001497 }
1498 case ARM::BI__builtin_neon_vmull_v: {
Bob Wilsonbf346e92010-09-01 23:20:27 +00001499 if (poly)
1500 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vmullp, &Ty, 1),
1501 Ops, "vmull");
Bob Wilson22359412010-09-02 22:37:30 +00001502 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1503 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
1504 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001505 if (usgn) {
1506 Ops[0] = Builder.CreateZExt(Ops[0], Ty);
1507 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1508 } else {
1509 Ops[0] = Builder.CreateSExt(Ops[0], Ty);
1510 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1511 }
1512 return Builder.CreateMul(Ops[0], Ops[1], "vmull");
Bob Wilson22359412010-09-02 22:37:30 +00001513 }
Nate Begemandf98e1d2010-06-09 18:04:15 +00001514 case ARM::BI__builtin_neon_vpadal_v:
1515 case ARM::BI__builtin_neon_vpadalq_v:
1516 Int = usgn ? Intrinsic::arm_neon_vpadalu : Intrinsic::arm_neon_vpadals;
1517 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpadal");
Nate Begeman548f7da2010-06-10 18:11:55 +00001518 case ARM::BI__builtin_neon_vpadd_v:
1519 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vpadd, &Ty, 1),
1520 Ops, "vpadd");
1521 case ARM::BI__builtin_neon_vpaddl_v:
1522 case ARM::BI__builtin_neon_vpaddlq_v:
1523 Int = usgn ? Intrinsic::arm_neon_vpaddlu : Intrinsic::arm_neon_vpaddls;
1524 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpaddl");
1525 case ARM::BI__builtin_neon_vpmax_v:
1526 Int = usgn ? Intrinsic::arm_neon_vpmaxu : Intrinsic::arm_neon_vpmaxs;
1527 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmax");
1528 case ARM::BI__builtin_neon_vpmin_v:
1529 Int = usgn ? Intrinsic::arm_neon_vpminu : Intrinsic::arm_neon_vpmins;
1530 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmin");
1531 case ARM::BI__builtin_neon_vqabs_v:
1532 case ARM::BI__builtin_neon_vqabsq_v:
1533 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqabs, &Ty, 1),
1534 Ops, "vqabs");
1535 case ARM::BI__builtin_neon_vqadd_v:
1536 case ARM::BI__builtin_neon_vqaddq_v:
1537 Int = usgn ? Intrinsic::arm_neon_vqaddu : Intrinsic::arm_neon_vqadds;
1538 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqadd");
1539 case ARM::BI__builtin_neon_vqdmlal_lane_v:
1540 splat = true;
1541 case ARM::BI__builtin_neon_vqdmlal_v:
1542 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlal, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001543 Ops, "vqdmlal", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001544 case ARM::BI__builtin_neon_vqdmlsl_lane_v:
1545 splat = true;
1546 case ARM::BI__builtin_neon_vqdmlsl_v:
1547 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlsl, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001548 Ops, "vqdmlsl", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001549 case ARM::BI__builtin_neon_vqdmulh_lane_v:
1550 case ARM::BI__builtin_neon_vqdmulhq_lane_v:
1551 splat = true;
1552 case ARM::BI__builtin_neon_vqdmulh_v:
1553 case ARM::BI__builtin_neon_vqdmulhq_v:
1554 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmulh, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001555 Ops, "vqdmulh", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001556 case ARM::BI__builtin_neon_vqdmull_lane_v:
1557 splat = true;
1558 case ARM::BI__builtin_neon_vqdmull_v:
1559 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmull, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001560 Ops, "vqdmull", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001561 case ARM::BI__builtin_neon_vqmovn_v:
1562 Int = usgn ? Intrinsic::arm_neon_vqmovnu : Intrinsic::arm_neon_vqmovns;
1563 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqmovn");
1564 case ARM::BI__builtin_neon_vqmovun_v:
1565 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqmovnsu, &Ty, 1),
1566 Ops, "vqdmull");
1567 case ARM::BI__builtin_neon_vqneg_v:
Nate Begeman61eecf52010-06-14 05:21:25 +00001568 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqneg, &Ty, 1),
1569 Ops, "vqneg");
Nate Begeman548f7da2010-06-10 18:11:55 +00001570 case ARM::BI__builtin_neon_vqrdmulh_lane_v:
1571 case ARM::BI__builtin_neon_vqrdmulhq_lane_v:
1572 splat = true;
1573 case ARM::BI__builtin_neon_vqrdmulh_v:
1574 case ARM::BI__builtin_neon_vqrdmulhq_v:
1575 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrdmulh, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001576 Ops, "vqrdmulh", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001577 case ARM::BI__builtin_neon_vqrshl_v:
1578 case ARM::BI__builtin_neon_vqrshlq_v:
1579 Int = usgn ? Intrinsic::arm_neon_vqrshiftu : Intrinsic::arm_neon_vqrshifts;
1580 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshl");
1581 case ARM::BI__builtin_neon_vqrshrn_n_v:
1582 Int = usgn ? Intrinsic::arm_neon_vqrshiftnu : Intrinsic::arm_neon_vqrshiftns;
Nate Begeman61eecf52010-06-14 05:21:25 +00001583 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshrn_n", false,
1584 1, true);
Nate Begeman548f7da2010-06-10 18:11:55 +00001585 case ARM::BI__builtin_neon_vqrshrun_n_v:
1586 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrshiftnsu, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001587 Ops, "vqrshrun_n", false, 1, true);
1588 case ARM::BI__builtin_neon_vqshl_v:
1589 case ARM::BI__builtin_neon_vqshlq_v:
1590 Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts;
1591 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl");
1592 case ARM::BI__builtin_neon_vqshl_n_v:
1593 case ARM::BI__builtin_neon_vqshlq_n_v:
1594 Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts;
1595 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl_n", false,
1596 1, false);
1597 case ARM::BI__builtin_neon_vqshlu_n_v:
1598 case ARM::BI__builtin_neon_vqshluq_n_v:
1599 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftsu, &Ty, 1),
1600 Ops, "vqshlu", 1, false);
1601 case ARM::BI__builtin_neon_vqshrn_n_v:
1602 Int = usgn ? Intrinsic::arm_neon_vqshiftnu : Intrinsic::arm_neon_vqshiftns;
1603 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshrn_n", false,
1604 1, true);
1605 case ARM::BI__builtin_neon_vqshrun_n_v:
1606 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftnsu, &Ty, 1),
1607 Ops, "vqshrun_n", false, 1, true);
Nate Begeman464ccb62010-06-11 22:57:12 +00001608 case ARM::BI__builtin_neon_vqsub_v:
1609 case ARM::BI__builtin_neon_vqsubq_v:
1610 Int = usgn ? Intrinsic::arm_neon_vqsubu : Intrinsic::arm_neon_vqsubs;
1611 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqsub");
1612 case ARM::BI__builtin_neon_vraddhn_v:
1613 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vraddhn, &Ty, 1),
1614 Ops, "vraddhn");
1615 case ARM::BI__builtin_neon_vrecpe_v:
1616 case ARM::BI__builtin_neon_vrecpeq_v:
1617 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecpe, &Ty, 1),
1618 Ops, "vrecpe");
1619 case ARM::BI__builtin_neon_vrecps_v:
1620 case ARM::BI__builtin_neon_vrecpsq_v:
1621 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecps, &Ty, 1),
1622 Ops, "vrecps");
Nate Begeman464ccb62010-06-11 22:57:12 +00001623 case ARM::BI__builtin_neon_vrhadd_v:
1624 case ARM::BI__builtin_neon_vrhaddq_v:
1625 Int = usgn ? Intrinsic::arm_neon_vrhaddu : Intrinsic::arm_neon_vrhadds;
1626 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrhadd");
Nate Begeman5af93ef2010-06-12 06:06:07 +00001627 case ARM::BI__builtin_neon_vrshl_v:
1628 case ARM::BI__builtin_neon_vrshlq_v:
1629 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
1630 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshl");
1631 case ARM::BI__builtin_neon_vrshrn_n_v:
Nate Begeman5af93ef2010-06-12 06:06:07 +00001632 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrshiftn, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001633 Ops, "vrshrn_n", false, 1, true);
Nate Begeman5af93ef2010-06-12 06:06:07 +00001634 case ARM::BI__builtin_neon_vrshr_n_v:
1635 case ARM::BI__builtin_neon_vrshrq_n_v:
Nate Begeman5af93ef2010-06-12 06:06:07 +00001636 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
Nate Begeman61eecf52010-06-14 05:21:25 +00001637 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshr_n", false,
1638 1, true);
Nate Begeman5af93ef2010-06-12 06:06:07 +00001639 case ARM::BI__builtin_neon_vrsqrte_v:
1640 case ARM::BI__builtin_neon_vrsqrteq_v:
1641 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrte, &Ty, 1),
1642 Ops, "vrsqrte");
1643 case ARM::BI__builtin_neon_vrsqrts_v:
1644 case ARM::BI__builtin_neon_vrsqrtsq_v:
1645 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrts, &Ty, 1),
1646 Ops, "vrsqrts");
1647 case ARM::BI__builtin_neon_vrsra_n_v:
1648 case ARM::BI__builtin_neon_vrsraq_n_v:
1649 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1650 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1651 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, true);
1652 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
1653 Ops[1] = Builder.CreateCall2(CGM.getIntrinsic(Int, &Ty, 1), Ops[1], Ops[2]);
1654 return Builder.CreateAdd(Ops[0], Ops[1], "vrsra_n");
Nate Begeman464ccb62010-06-11 22:57:12 +00001655 case ARM::BI__builtin_neon_vrsubhn_v:
1656 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsubhn, &Ty, 1),
1657 Ops, "vrsubhn");
Nate Begeman548f7da2010-06-10 18:11:55 +00001658 case ARM::BI__builtin_neon_vset_lane_i8:
1659 case ARM::BI__builtin_neon_vset_lane_i16:
1660 case ARM::BI__builtin_neon_vset_lane_i32:
1661 case ARM::BI__builtin_neon_vset_lane_i64:
1662 case ARM::BI__builtin_neon_vset_lane_f32:
1663 case ARM::BI__builtin_neon_vsetq_lane_i8:
1664 case ARM::BI__builtin_neon_vsetq_lane_i16:
1665 case ARM::BI__builtin_neon_vsetq_lane_i32:
1666 case ARM::BI__builtin_neon_vsetq_lane_i64:
1667 case ARM::BI__builtin_neon_vsetq_lane_f32:
1668 Ops.push_back(EmitScalarExpr(E->getArg(2)));
1669 return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vset_lane");
Nate Begeman464ccb62010-06-11 22:57:12 +00001670 case ARM::BI__builtin_neon_vshl_v:
1671 case ARM::BI__builtin_neon_vshlq_v:
1672 Int = usgn ? Intrinsic::arm_neon_vshiftu : Intrinsic::arm_neon_vshifts;
1673 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshl");
1674 case ARM::BI__builtin_neon_vshll_n_v:
Nate Begeman464ccb62010-06-11 22:57:12 +00001675 Int = usgn ? Intrinsic::arm_neon_vshiftlu : Intrinsic::arm_neon_vshiftls;
Nate Begeman61eecf52010-06-14 05:21:25 +00001676 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshll", false, 1);
Nate Begeman464ccb62010-06-11 22:57:12 +00001677 case ARM::BI__builtin_neon_vshl_n_v:
1678 case ARM::BI__builtin_neon_vshlq_n_v:
Nate Begeman61eecf52010-06-14 05:21:25 +00001679 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false);
1680 return Builder.CreateShl(Builder.CreateBitCast(Ops[0],Ty), Ops[1], "vshl_n");
Nate Begeman464ccb62010-06-11 22:57:12 +00001681 case ARM::BI__builtin_neon_vshrn_n_v:
Nate Begeman464ccb62010-06-11 22:57:12 +00001682 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftn, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001683 Ops, "vshrn_n", false, 1, true);
Nate Begeman464ccb62010-06-11 22:57:12 +00001684 case ARM::BI__builtin_neon_vshr_n_v:
1685 case ARM::BI__builtin_neon_vshrq_n_v:
1686 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
Nate Begeman61eecf52010-06-14 05:21:25 +00001687 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false);
Nate Begeman464ccb62010-06-11 22:57:12 +00001688 if (usgn)
1689 return Builder.CreateLShr(Ops[0], Ops[1], "vshr_n");
1690 else
1691 return Builder.CreateAShr(Ops[0], Ops[1], "vshr_n");
1692 case ARM::BI__builtin_neon_vsri_n_v:
1693 case ARM::BI__builtin_neon_vsriq_n_v:
1694 poly = true;
1695 case ARM::BI__builtin_neon_vsli_n_v:
1696 case ARM::BI__builtin_neon_vsliq_n_v:
1697 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, poly);
1698 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftins, &Ty, 1),
1699 Ops, "vsli_n");
1700 case ARM::BI__builtin_neon_vsra_n_v:
1701 case ARM::BI__builtin_neon_vsraq_n_v:
1702 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1703 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman61eecf52010-06-14 05:21:25 +00001704 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, false);
Nate Begeman464ccb62010-06-11 22:57:12 +00001705 if (usgn)
1706 Ops[1] = Builder.CreateLShr(Ops[1], Ops[2], "vsra_n");
1707 else
1708 Ops[1] = Builder.CreateAShr(Ops[1], Ops[2], "vsra_n");
1709 return Builder.CreateAdd(Ops[0], Ops[1]);
1710 case ARM::BI__builtin_neon_vst1_v:
1711 case ARM::BI__builtin_neon_vst1q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001712 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001713 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst1, &Ty, 1),
1714 Ops, "");
1715 case ARM::BI__builtin_neon_vst1_lane_v:
1716 case ARM::BI__builtin_neon_vst1q_lane_v:
1717 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1718 Ops[1] = Builder.CreateExtractElement(Ops[1], Ops[2]);
1719 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1720 return Builder.CreateStore(Ops[1], Builder.CreateBitCast(Ops[0], Ty));
1721 case ARM::BI__builtin_neon_vst2_v:
1722 case ARM::BI__builtin_neon_vst2q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001723 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001724 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2, &Ty, 1),
1725 Ops, "");
1726 case ARM::BI__builtin_neon_vst2_lane_v:
1727 case ARM::BI__builtin_neon_vst2q_lane_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001728 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001729 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2lane, &Ty, 1),
1730 Ops, "");
1731 case ARM::BI__builtin_neon_vst3_v:
1732 case ARM::BI__builtin_neon_vst3q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001733 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001734 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3, &Ty, 1),
1735 Ops, "");
1736 case ARM::BI__builtin_neon_vst3_lane_v:
1737 case ARM::BI__builtin_neon_vst3q_lane_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001738 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001739 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3lane, &Ty, 1),
1740 Ops, "");
1741 case ARM::BI__builtin_neon_vst4_v:
1742 case ARM::BI__builtin_neon_vst4q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001743 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001744 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4, &Ty, 1),
1745 Ops, "");
1746 case ARM::BI__builtin_neon_vst4_lane_v:
1747 case ARM::BI__builtin_neon_vst4q_lane_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001748 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001749 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4lane, &Ty, 1),
1750 Ops, "");
Nate Begeman548f7da2010-06-10 18:11:55 +00001751 case ARM::BI__builtin_neon_vsubhn_v:
1752 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vsubhn, &Ty, 1),
1753 Ops, "vsubhn");
Bob Wilson22359412010-09-02 22:37:30 +00001754 case ARM::BI__builtin_neon_vsubl_v: {
1755 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1756 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
1757 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilson54160c62010-08-29 05:14:28 +00001758 if (usgn) {
1759 Ops[0] = Builder.CreateZExt(Ops[0], Ty);
1760 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1761 } else {
1762 Ops[0] = Builder.CreateSExt(Ops[0], Ty);
1763 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1764 }
1765 return Builder.CreateSub(Ops[0], Ops[1], "vsubl");
Bob Wilson22359412010-09-02 22:37:30 +00001766 }
1767 case ARM::BI__builtin_neon_vsubw_v: {
1768 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1769 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1770 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilson54160c62010-08-29 05:14:28 +00001771 if (usgn)
1772 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1773 else
1774 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1775 return Builder.CreateSub(Ops[0], Ops[1], "vsubw");
Bob Wilson22359412010-09-02 22:37:30 +00001776 }
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001777 case ARM::BI__builtin_neon_vtbl1_v:
1778 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl1),
1779 Ops, "vtbl1");
1780 case ARM::BI__builtin_neon_vtbl2_v:
1781 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl2),
1782 Ops, "vtbl2");
1783 case ARM::BI__builtin_neon_vtbl3_v:
1784 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl3),
1785 Ops, "vtbl3");
1786 case ARM::BI__builtin_neon_vtbl4_v:
1787 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl4),
1788 Ops, "vtbl4");
1789 case ARM::BI__builtin_neon_vtbx1_v:
1790 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx1),
1791 Ops, "vtbx1");
1792 case ARM::BI__builtin_neon_vtbx2_v:
1793 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx2),
1794 Ops, "vtbx2");
1795 case ARM::BI__builtin_neon_vtbx3_v:
1796 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx3),
1797 Ops, "vtbx3");
1798 case ARM::BI__builtin_neon_vtbx4_v:
1799 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx4),
1800 Ops, "vtbx4");
1801 case ARM::BI__builtin_neon_vtst_v:
1802 case ARM::BI__builtin_neon_vtstq_v: {
1803 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1804 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1805 Ops[0] = Builder.CreateAnd(Ops[0], Ops[1]);
1806 Ops[0] = Builder.CreateICmp(ICmpInst::ICMP_NE, Ops[0],
1807 ConstantAggregateZero::get(Ty));
1808 return Builder.CreateSExt(Ops[0], Ty, "vtst");
1809 }
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001810 case ARM::BI__builtin_neon_vtrn_v:
1811 case ARM::BI__builtin_neon_vtrnq_v: {
Nate Begeman4be54302010-06-20 23:05:28 +00001812 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001813 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001814 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001815 Value *SV;
1816
1817 for (unsigned vi = 0; vi != 2; ++vi) {
1818 SmallVector<Constant*, 16> Indices;
1819 for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001820 Indices.push_back(ConstantInt::get(Int32Ty, i+vi));
1821 Indices.push_back(ConstantInt::get(Int32Ty, i+e+vi));
Nate Begeman4be54302010-06-20 23:05:28 +00001822 }
1823 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
1824 SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1825 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vtrn");
1826 SV = Builder.CreateStore(SV, Addr);
1827 }
1828 return SV;
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001829 }
1830 case ARM::BI__builtin_neon_vuzp_v:
1831 case ARM::BI__builtin_neon_vuzpq_v: {
Nate Begeman4be54302010-06-20 23:05:28 +00001832 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001833 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001834 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001835 Value *SV;
1836
1837 for (unsigned vi = 0; vi != 2; ++vi) {
1838 SmallVector<Constant*, 16> Indices;
1839 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
Chris Lattner77b89b82010-06-27 07:15:29 +00001840 Indices.push_back(ConstantInt::get(Int32Ty, 2*i+vi));
Nate Begeman4be54302010-06-20 23:05:28 +00001841
1842 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
1843 SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1844 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vuzp");
1845 SV = Builder.CreateStore(SV, Addr);
1846 }
1847 return SV;
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001848 }
1849 case ARM::BI__builtin_neon_vzip_v:
1850 case ARM::BI__builtin_neon_vzipq_v: {
Nate Begeman4be54302010-06-20 23:05:28 +00001851 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001852 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001853 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001854 Value *SV;
1855
1856 for (unsigned vi = 0; vi != 2; ++vi) {
1857 SmallVector<Constant*, 16> Indices;
1858 for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
Daniel Dunbare361cc32010-08-26 00:55:57 +00001859 Indices.push_back(ConstantInt::get(Int32Ty, (i + vi*e) >> 1));
1860 Indices.push_back(ConstantInt::get(Int32Ty, ((i + vi*e) >> 1)+e));
Nate Begeman4be54302010-06-20 23:05:28 +00001861 }
1862 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
1863 SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1864 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vzip");
1865 SV = Builder.CreateStore(SV, Addr);
1866 }
1867 return SV;
Nate Begeman9eb65a52010-06-08 00:17:19 +00001868 }
Chris Lattner2752c012010-03-03 19:03:45 +00001869 }
1870}
1871
Mike Stump1eb44332009-09-09 15:08:12 +00001872Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +00001873 const CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001875 llvm::SmallVector<Value*, 4> Ops;
1876
1877 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
1878 Ops.push_back(EmitScalarExpr(E->getArg(i)));
1879
Anders Carlsson564f1de2007-12-09 23:17:02 +00001880 switch (BuiltinID) {
Anders Carlsson46a26b02007-12-09 23:39:18 +00001881 default: return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001882 case X86::BI__builtin_ia32_pslldi128:
Nate Begemane7722102008-04-14 04:49:57 +00001883 case X86::BI__builtin_ia32_psllqi128:
Mike Stump1eb44332009-09-09 15:08:12 +00001884 case X86::BI__builtin_ia32_psllwi128:
Nate Begemane7722102008-04-14 04:49:57 +00001885 case X86::BI__builtin_ia32_psradi128:
1886 case X86::BI__builtin_ia32_psrawi128:
1887 case X86::BI__builtin_ia32_psrldi128:
1888 case X86::BI__builtin_ia32_psrlqi128:
1889 case X86::BI__builtin_ia32_psrlwi128: {
Chris Lattner77b89b82010-06-27 07:15:29 +00001890 Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext");
1891 const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 2);
1892 llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
Owen Anderson03e20502009-07-30 23:11:26 +00001893 Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty),
Nate Begemane7722102008-04-14 04:49:57 +00001894 Ops[1], Zero, "insert");
1895 Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast");
1896 const char *name = 0;
1897 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Nate Begemane7722102008-04-14 04:49:57 +00001899 switch (BuiltinID) {
1900 default: assert(0 && "Unsupported shift intrinsic!");
1901 case X86::BI__builtin_ia32_pslldi128:
1902 name = "pslldi";
1903 ID = Intrinsic::x86_sse2_psll_d;
1904 break;
1905 case X86::BI__builtin_ia32_psllqi128:
1906 name = "psllqi";
1907 ID = Intrinsic::x86_sse2_psll_q;
1908 break;
1909 case X86::BI__builtin_ia32_psllwi128:
1910 name = "psllwi";
1911 ID = Intrinsic::x86_sse2_psll_w;
1912 break;
1913 case X86::BI__builtin_ia32_psradi128:
1914 name = "psradi";
1915 ID = Intrinsic::x86_sse2_psra_d;
1916 break;
1917 case X86::BI__builtin_ia32_psrawi128:
1918 name = "psrawi";
1919 ID = Intrinsic::x86_sse2_psra_w;
1920 break;
1921 case X86::BI__builtin_ia32_psrldi128:
1922 name = "psrldi";
1923 ID = Intrinsic::x86_sse2_psrl_d;
1924 break;
1925 case X86::BI__builtin_ia32_psrlqi128:
1926 name = "psrlqi";
1927 ID = Intrinsic::x86_sse2_psrl_q;
1928 break;
1929 case X86::BI__builtin_ia32_psrlwi128:
1930 name = "psrlwi";
1931 ID = Intrinsic::x86_sse2_psrl_w;
1932 break;
1933 }
1934 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001935 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Nate Begemane7722102008-04-14 04:49:57 +00001936 }
Mike Stump1eb44332009-09-09 15:08:12 +00001937 case X86::BI__builtin_ia32_pslldi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001938 case X86::BI__builtin_ia32_psllqi:
Mike Stump1eb44332009-09-09 15:08:12 +00001939 case X86::BI__builtin_ia32_psllwi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001940 case X86::BI__builtin_ia32_psradi:
1941 case X86::BI__builtin_ia32_psrawi:
1942 case X86::BI__builtin_ia32_psrldi:
1943 case X86::BI__builtin_ia32_psrlqi:
1944 case X86::BI__builtin_ia32_psrlwi: {
Chris Lattner77b89b82010-06-27 07:15:29 +00001945 Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext");
1946 const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 1);
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001947 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001948 const char *name = 0;
1949 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001951 switch (BuiltinID) {
1952 default: assert(0 && "Unsupported shift intrinsic!");
1953 case X86::BI__builtin_ia32_pslldi:
1954 name = "pslldi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001955 ID = Intrinsic::x86_mmx_psll_d;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001956 break;
1957 case X86::BI__builtin_ia32_psllqi:
1958 name = "psllqi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001959 ID = Intrinsic::x86_mmx_psll_q;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001960 break;
1961 case X86::BI__builtin_ia32_psllwi:
1962 name = "psllwi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001963 ID = Intrinsic::x86_mmx_psll_w;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001964 break;
1965 case X86::BI__builtin_ia32_psradi:
1966 name = "psradi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001967 ID = Intrinsic::x86_mmx_psra_d;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001968 break;
1969 case X86::BI__builtin_ia32_psrawi:
1970 name = "psrawi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001971 ID = Intrinsic::x86_mmx_psra_w;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001972 break;
1973 case X86::BI__builtin_ia32_psrldi:
1974 name = "psrldi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001975 ID = Intrinsic::x86_mmx_psrl_d;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001976 break;
1977 case X86::BI__builtin_ia32_psrlqi:
1978 name = "psrlqi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001979 ID = Intrinsic::x86_mmx_psrl_q;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001980 break;
1981 case X86::BI__builtin_ia32_psrlwi:
1982 name = "psrlwi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001983 ID = Intrinsic::x86_mmx_psrl_w;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001984 break;
1985 }
Chris Lattner7acda7c2007-12-18 00:25:38 +00001986 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001987 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001988 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +00001989 case X86::BI__builtin_ia32_cmpps: {
1990 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps);
1991 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps");
1992 }
1993 case X86::BI__builtin_ia32_cmpss: {
1994 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss);
1995 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +00001996 }
Nate Begemane7722102008-04-14 04:49:57 +00001997 case X86::BI__builtin_ia32_ldmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001998 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00001999 Value *One = llvm::ConstantInt::get(Int32Ty, 1);
2000 Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +00002001 Builder.CreateStore(Ops[0], Tmp);
2002 return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +00002003 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +00002004 }
2005 case X86::BI__builtin_ia32_stmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002006 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00002007 Value *One = llvm::ConstantInt::get(Int32Ty, 1);
2008 Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +00002009 One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +00002010 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +00002011 return Builder.CreateLoad(Tmp, "stmxcsr");
2012 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +00002013 case X86::BI__builtin_ia32_cmppd: {
2014 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd);
2015 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd");
2016 }
2017 case X86::BI__builtin_ia32_cmpsd: {
2018 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd);
2019 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +00002020 }
Nate Begemane7722102008-04-14 04:49:57 +00002021 case X86::BI__builtin_ia32_storehps:
2022 case X86::BI__builtin_ia32_storelps: {
Chris Lattner77b89b82010-06-27 07:15:29 +00002023 llvm::Type *PtrTy = llvm::PointerType::getUnqual(Int64Ty);
2024 llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00002025
Nate Begemane7722102008-04-14 04:49:57 +00002026 // cast val v2i64
2027 Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast");
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Nate Begemane7722102008-04-14 04:49:57 +00002029 // extract (0, 1)
2030 unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1;
Chris Lattner77b89b82010-06-27 07:15:29 +00002031 llvm::Value *Idx = llvm::ConstantInt::get(Int32Ty, Index);
Nate Begemane7722102008-04-14 04:49:57 +00002032 Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract");
2033
2034 // cast pointer to i64 & store
2035 Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy);
2036 return Builder.CreateStore(Ops[1], Ops[0]);
2037 }
Bill Wendling28cab382010-09-28 01:28:56 +00002038 case X86::BI__builtin_ia32_palignr: {
2039 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
2040
2041 // If palignr is shifting the pair of input vectors less than 9 bytes,
2042 // emit a shuffle instruction.
2043 if (shiftVal <= 8) {
2044 llvm::SmallVector<llvm::Constant*, 8> Indices;
2045 for (unsigned i = 0; i != 8; ++i)
2046 Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i));
2047
2048 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
2049 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
2050 }
2051
2052 // If palignr is shifting the pair of input vectors more than 8 but less
2053 // than 16 bytes, emit a logical right shift of the destination.
2054 if (shiftVal < 16) {
2055 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
2056 const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 1);
2057
2058 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
2059 Ops[1] = llvm::ConstantInt::get(VecTy, (shiftVal-8) * 8);
2060
2061 // create i32 constant
2062 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_mmx_psrl_q);
2063 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
2064 }
2065
2066 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
2067 return llvm::Constant::getNullValue(ConvertType(E->getType()));
2068 }
Nate Begemanc3420ff2009-12-14 05:15:02 +00002069 case X86::BI__builtin_ia32_palignr128: {
Nate Begemance5818a2009-12-14 04:57:03 +00002070 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
2071
2072 // If palignr is shifting the pair of input vectors less than 17 bytes,
2073 // emit a shuffle instruction.
2074 if (shiftVal <= 16) {
Nate Begemance5818a2009-12-14 04:57:03 +00002075 llvm::SmallVector<llvm::Constant*, 16> Indices;
2076 for (unsigned i = 0; i != 16; ++i)
Chris Lattner77b89b82010-06-27 07:15:29 +00002077 Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i));
Nate Begemance5818a2009-12-14 04:57:03 +00002078
2079 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
2080 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
2081 }
2082
2083 // If palignr is shifting the pair of input vectors more than 16 but less
2084 // than 32 bytes, emit a logical right shift of the destination.
2085 if (shiftVal < 32) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002086 const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2);
Nate Begemance5818a2009-12-14 04:57:03 +00002087
2088 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
Chris Lattner77b89b82010-06-27 07:15:29 +00002089 Ops[1] = llvm::ConstantInt::get(Int32Ty, (shiftVal-16) * 8);
Nate Begemance5818a2009-12-14 04:57:03 +00002090
2091 // create i32 constant
2092 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq);
2093 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
2094 }
2095
2096 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
2097 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Eric Christopher91b59272009-12-01 05:00:51 +00002098 }
Anders Carlsson564f1de2007-12-09 23:17:02 +00002099 }
2100}
2101
Mike Stump1eb44332009-09-09 15:08:12 +00002102Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +00002103 const CallExpr *E) {
Chris Lattnerdd173942010-04-14 03:54:58 +00002104 llvm::SmallVector<Value*, 4> Ops;
2105
2106 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
2107 Ops.push_back(EmitScalarExpr(E->getArg(i)));
2108
2109 Intrinsic::ID ID = Intrinsic::not_intrinsic;
2110
2111 switch (BuiltinID) {
2112 default: return 0;
2113
Anton Korobeynikov4d3a7b02010-06-19 09:47:18 +00002114 // vec_ld, vec_lvsl, vec_lvsr
2115 case PPC::BI__builtin_altivec_lvx:
2116 case PPC::BI__builtin_altivec_lvxl:
2117 case PPC::BI__builtin_altivec_lvebx:
2118 case PPC::BI__builtin_altivec_lvehx:
2119 case PPC::BI__builtin_altivec_lvewx:
2120 case PPC::BI__builtin_altivec_lvsl:
2121 case PPC::BI__builtin_altivec_lvsr:
2122 {
2123 Ops[1] = Builder.CreateBitCast(Ops[1], llvm::Type::getInt8PtrTy(VMContext));
2124
2125 Ops[0] = Builder.CreateGEP(Ops[1], Ops[0], "tmp");
2126 Ops.pop_back();
2127
2128 switch (BuiltinID) {
2129 default: assert(0 && "Unsupported ld/lvsl/lvsr intrinsic!");
2130 case PPC::BI__builtin_altivec_lvx:
2131 ID = Intrinsic::ppc_altivec_lvx;
2132 break;
2133 case PPC::BI__builtin_altivec_lvxl:
2134 ID = Intrinsic::ppc_altivec_lvxl;
2135 break;
2136 case PPC::BI__builtin_altivec_lvebx:
2137 ID = Intrinsic::ppc_altivec_lvebx;
2138 break;
2139 case PPC::BI__builtin_altivec_lvehx:
2140 ID = Intrinsic::ppc_altivec_lvehx;
2141 break;
2142 case PPC::BI__builtin_altivec_lvewx:
2143 ID = Intrinsic::ppc_altivec_lvewx;
2144 break;
2145 case PPC::BI__builtin_altivec_lvsl:
2146 ID = Intrinsic::ppc_altivec_lvsl;
2147 break;
2148 case PPC::BI__builtin_altivec_lvsr:
2149 ID = Intrinsic::ppc_altivec_lvsr;
2150 break;
2151 }
2152 llvm::Function *F = CGM.getIntrinsic(ID);
2153 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "");
2154 }
2155
Chris Lattnerdd173942010-04-14 03:54:58 +00002156 // vec_st
2157 case PPC::BI__builtin_altivec_stvx:
2158 case PPC::BI__builtin_altivec_stvxl:
2159 case PPC::BI__builtin_altivec_stvebx:
2160 case PPC::BI__builtin_altivec_stvehx:
2161 case PPC::BI__builtin_altivec_stvewx:
2162 {
2163 Ops[2] = Builder.CreateBitCast(Ops[2], llvm::Type::getInt8PtrTy(VMContext));
Anton Korobeynikov4d3a7b02010-06-19 09:47:18 +00002164 Ops[1] = Builder.CreateGEP(Ops[2], Ops[1], "tmp");
Chris Lattnerdd173942010-04-14 03:54:58 +00002165 Ops.pop_back();
2166
2167 switch (BuiltinID) {
Anton Korobeynikov4d3a7b02010-06-19 09:47:18 +00002168 default: assert(0 && "Unsupported st intrinsic!");
Chris Lattnerdd173942010-04-14 03:54:58 +00002169 case PPC::BI__builtin_altivec_stvx:
2170 ID = Intrinsic::ppc_altivec_stvx;
2171 break;
2172 case PPC::BI__builtin_altivec_stvxl:
2173 ID = Intrinsic::ppc_altivec_stvxl;
2174 break;
2175 case PPC::BI__builtin_altivec_stvebx:
2176 ID = Intrinsic::ppc_altivec_stvebx;
2177 break;
2178 case PPC::BI__builtin_altivec_stvehx:
2179 ID = Intrinsic::ppc_altivec_stvehx;
2180 break;
2181 case PPC::BI__builtin_altivec_stvewx:
2182 ID = Intrinsic::ppc_altivec_stvewx;
2183 break;
2184 }
2185 llvm::Function *F = CGM.getIntrinsic(ID);
2186 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "");
2187 }
2188 }
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002189 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002190}