blob: 6d669c2dd2a87b5f7a0c5b51e3814fb65325cf2d [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) {
Chris Lattnera1aa9e32010-10-01 23:43:16 +000046 if (Val->getType()->isPointerTy())
Chandler Carruthdb4325b2010-07-18 07:23:17 +000047 return CGF.Builder.CreatePtrToInt(Val, ToType);
Chris Lattnera1aa9e32010-10-01 23:43:16 +000048
Chandler Carruthdb4325b2010-07-18 07:23:17 +000049 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()));
Chris Lattnera1aa9e32010-10-01 23:43:16 +0000163 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 Lattner46c55912010-10-02 00:09:12 +0000946 // Find out if any arguments are required to be integer constant
947 // expressions.
948 unsigned ICEArguments = 0;
949 ASTContext::GetBuiltinTypeError Error;
950 getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
951 assert(Error == ASTContext::GE_None && "Should not codegen an error");
952
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000953 Function *F = CGM.getIntrinsic(IntrinsicID);
954 const llvm::FunctionType *FTy = F->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000956 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattner46c55912010-10-02 00:09:12 +0000957 Value *ArgValue;
958 // If this is a normal argument, just emit it as a scalar.
959 if ((ICEArguments & (1 << i)) == 0) {
960 ArgValue = EmitScalarExpr(E->getArg(i));
961 } else {
962 // If this is required to be a constant, constant fold it so that we
963 // know that the generated intrinsic gets a ConstantInt.
964 llvm::APSInt Result;
965 bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result,getContext());
966 assert(IsConst && "Constant arg isn't actually constant?");
967 (void)IsConst;
968 ArgValue = llvm::ConstantInt::get(VMContext, Result);
969 }
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000971 // If the intrinsic arg type is different from the builtin arg type
972 // we need to do a bit cast.
973 const llvm::Type *PTy = FTy->getParamType(i);
974 if (PTy != ArgValue->getType()) {
975 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
976 "Must be able to losslessly bit cast to param");
977 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000980 Args.push_back(ArgValue);
981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Jay Foadbeaaccd2009-05-21 09:52:38 +0000983 Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size());
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000984 QualType BuiltinRetType = E->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Owen Anderson0032b272009-08-13 21:57:51 +0000986 const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000987 if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000989 if (RetTy != V->getType()) {
990 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
991 "Must be able to losslessly bit cast result type");
992 V = Builder.CreateBitCast(V, RetTy);
993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000995 return RValue::get(V);
996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000998 // See if we have a target specific builtin that needs to be lowered.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000999 if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E))
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001000 return RValue::get(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Daniel Dunbar488e9932008-08-16 00:56:44 +00001002 ErrorUnsupported(E, "builtin function");
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001004 // Unknown builtin, for now just dump it out and return undef.
1005 if (hasAggregateLLVMType(E->getType()))
Daniel Dunbar195337d2010-02-09 02:48:28 +00001006 return RValue::getAggregate(CreateMemTemp(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +00001007 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
Mike Stump1eb44332009-09-09 15:08:12 +00001008}
Anders Carlsson564f1de2007-12-09 23:17:02 +00001009
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001010Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID,
1011 const CallExpr *E) {
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001012 switch (Target.getTriple().getArch()) {
Chris Lattner2752c012010-03-03 19:03:45 +00001013 case llvm::Triple::arm:
1014 case llvm::Triple::thumb:
1015 return EmitARMBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001016 case llvm::Triple::x86:
1017 case llvm::Triple::x86_64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001018 return EmitX86BuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001019 case llvm::Triple::ppc:
1020 case llvm::Triple::ppc64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001021 return EmitPPCBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001022 default:
1023 return 0;
1024 }
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001025}
1026
Nate Begeman4be54302010-06-20 23:05:28 +00001027const llvm::VectorType *GetNeonType(LLVMContext &C, unsigned type, bool q) {
Nate Begeman998622c2010-06-07 16:01:56 +00001028 switch (type) {
1029 default: break;
1030 case 0:
Nate Begeman4be54302010-06-20 23:05:28 +00001031 case 5: return llvm::VectorType::get(llvm::Type::getInt8Ty(C), 8 << (int)q);
Nate Begeman998622c2010-06-07 16:01:56 +00001032 case 6:
1033 case 7:
Nate Begeman4be54302010-06-20 23:05:28 +00001034 case 1: return llvm::VectorType::get(llvm::Type::getInt16Ty(C),4 << (int)q);
1035 case 2: return llvm::VectorType::get(llvm::Type::getInt32Ty(C),2 << (int)q);
1036 case 3: return llvm::VectorType::get(llvm::Type::getInt64Ty(C),1 << (int)q);
1037 case 4: return llvm::VectorType::get(llvm::Type::getFloatTy(C),2 << (int)q);
Nate Begeman998622c2010-06-07 16:01:56 +00001038 };
1039 return 0;
1040}
1041
Nate Begemancd480ad2010-08-06 01:24:57 +00001042Value *CodeGenFunction::EmitNeonSplat(Value *V, Constant *C, bool widen) {
Nate Begemand075c012010-06-10 00:17:56 +00001043 unsigned nElts = cast<llvm::VectorType>(V->getType())->getNumElements();
Nate Begemancd480ad2010-08-06 01:24:57 +00001044 if (widen)
1045 nElts <<= 1;
Nate Begemand075c012010-06-10 00:17:56 +00001046 SmallVector<Constant*, 16> Indices(nElts, C);
1047 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1048 return Builder.CreateShuffleVector(V, V, SV, "lane");
1049}
1050
Nate Begeman30d91712010-06-08 06:03:01 +00001051Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl<Value*> &Ops,
Nate Begeman61eecf52010-06-14 05:21:25 +00001052 const char *name, bool splat,
1053 unsigned shift, bool rightshift) {
Nate Begeman30d91712010-06-08 06:03:01 +00001054 unsigned j = 0;
1055 for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
1056 ai != ae; ++ai, ++j)
Nate Begeman61eecf52010-06-14 05:21:25 +00001057 if (shift > 0 && shift == j)
1058 Ops[j] = EmitNeonShiftVector(Ops[j], ai->getType(), rightshift);
1059 else
1060 Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name);
Nate Begeman30d91712010-06-08 06:03:01 +00001061
Nate Begemand075c012010-06-10 00:17:56 +00001062 if (splat) {
1063 Ops[j-1] = EmitNeonSplat(Ops[j-1], cast<Constant>(Ops[j]));
1064 Ops.resize(j);
1065 }
Nate Begeman30d91712010-06-08 06:03:01 +00001066 return Builder.CreateCall(F, Ops.begin(), Ops.end(), name);
1067}
1068
Nate Begeman464ccb62010-06-11 22:57:12 +00001069Value *CodeGenFunction::EmitNeonShiftVector(Value *V, const llvm::Type *Ty,
1070 bool neg) {
1071 ConstantInt *CI = cast<ConstantInt>(V);
1072 int SV = CI->getSExtValue();
1073
1074 const llvm::VectorType *VTy = cast<llvm::VectorType>(Ty);
1075 llvm::Constant *C = ConstantInt::get(VTy->getElementType(), neg ? -SV : SV);
1076 SmallVector<llvm::Constant*, 16> CV(VTy->getNumElements(), C);
1077 return llvm::ConstantVector::get(CV.begin(), CV.size());
1078}
1079
Bob Wilson06b6c582010-08-27 17:14:29 +00001080/// GetPointeeAlignment - Given an expression with a pointer type, find the
1081/// alignment of the type referenced by the pointer. Skip over implicit
1082/// casts.
1083static Value *GetPointeeAlignment(CodeGenFunction &CGF, const Expr *Addr) {
1084 unsigned Align = 1;
1085 // Check if the type is a pointer. The implicit cast operand might not be.
1086 while (Addr->getType()->isPointerType()) {
1087 QualType PtTy = Addr->getType()->getPointeeType();
1088 unsigned NewA = CGF.getContext().getTypeAlignInChars(PtTy).getQuantity();
1089 if (NewA > Align)
1090 Align = NewA;
1091
1092 // If the address is an implicit cast, repeat with the cast operand.
1093 if (const ImplicitCastExpr *CastAddr = dyn_cast<ImplicitCastExpr>(Addr)) {
1094 Addr = CastAddr->getSubExpr();
1095 continue;
1096 }
1097 break;
1098 }
1099 return llvm::ConstantInt::get(CGF.Int32Ty, Align);
1100}
1101
Chris Lattner2752c012010-03-03 19:03:45 +00001102Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
1103 const CallExpr *E) {
Rafael Espindolae140af32010-06-09 03:48:40 +00001104 if (BuiltinID == ARM::BI__clear_cache) {
Rafael Espindola79ba5092010-06-07 17:26:50 +00001105 const FunctionDecl *FD = E->getDirectCallee();
1106 Value *a = EmitScalarExpr(E->getArg(0));
1107 Value *b = EmitScalarExpr(E->getArg(1));
1108 const llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType());
1109 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
1110 llvm::StringRef Name = FD->getName();
1111 return Builder.CreateCall2(CGM.CreateRuntimeFunction(FTy, Name),
1112 a, b);
Chris Lattner2752c012010-03-03 19:03:45 +00001113 }
Rafael Espindolae140af32010-06-09 03:48:40 +00001114
Nate Begemand075c012010-06-10 00:17:56 +00001115 llvm::SmallVector<Value*, 4> Ops;
Rafael Espindolae140af32010-06-09 03:48:40 +00001116 for (unsigned i = 0, e = E->getNumArgs() - 1; i != e; i++)
1117 Ops.push_back(EmitScalarExpr(E->getArg(i)));
1118
1119 llvm::APSInt Result;
1120 const Expr *Arg = E->getArg(E->getNumArgs()-1);
1121 if (!Arg->isIntegerConstantExpr(Result, getContext()))
1122 return 0;
1123
Nate Begeman99c40bb2010-08-03 21:32:34 +00001124 if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f ||
1125 BuiltinID == ARM::BI__builtin_arm_vcvtr_d) {
1126 // Determine the overloaded type of this builtin.
1127 const llvm::Type *Ty;
1128 if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f)
1129 Ty = llvm::Type::getFloatTy(VMContext);
1130 else
1131 Ty = llvm::Type::getDoubleTy(VMContext);
1132
1133 // Determine whether this is an unsigned conversion or not.
1134 bool usgn = Result.getZExtValue() == 1;
1135 unsigned Int = usgn ? Intrinsic::arm_vcvtru : Intrinsic::arm_vcvtr;
1136
1137 // Call the appropriate intrinsic.
1138 Function *F = CGM.getIntrinsic(Int, &Ty, 1);
1139 return Builder.CreateCall(F, Ops.begin(), Ops.end(), "vcvtr");
1140 }
1141
1142 // Determine the type of this overloaded NEON intrinsic.
Rafael Espindolae140af32010-06-09 03:48:40 +00001143 unsigned type = Result.getZExtValue();
1144 bool usgn = type & 0x08;
1145 bool quad = type & 0x10;
Nate Begeman0d15c532010-06-13 04:47:52 +00001146 bool poly = (type & 0x7) == 5 || (type & 0x7) == 6;
Nate Begemand075c012010-06-10 00:17:56 +00001147 bool splat = false;
Rafael Espindolae140af32010-06-09 03:48:40 +00001148
Nate Begeman4be54302010-06-20 23:05:28 +00001149 const llvm::VectorType *VTy = GetNeonType(VMContext, type & 0x7, quad);
1150 const llvm::Type *Ty = VTy;
Rafael Espindolae140af32010-06-09 03:48:40 +00001151 if (!Ty)
1152 return 0;
1153
1154 unsigned Int;
1155 switch (BuiltinID) {
1156 default: return 0;
Nate Begeman998622c2010-06-07 16:01:56 +00001157 case ARM::BI__builtin_neon_vaba_v:
Bob Wilson47a88fa2010-09-03 01:27:09 +00001158 case ARM::BI__builtin_neon_vabaq_v: {
1159 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1160 SmallVector<Value*, 2> Args;
1161 Args.push_back(Ops[1]);
1162 Args.push_back(Ops[2]);
1163 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
1164 Ops[1] = EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Args, "vaba");
1165 return Builder.CreateAdd(Ops[0], Ops[1], "vaba");
1166 }
1167 case ARM::BI__builtin_neon_vabal_v: {
1168 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1169 SmallVector<Value*, 2> Args;
1170 Args.push_back(Ops[1]);
1171 Args.push_back(Ops[2]);
1172 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
1173 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1174 Ops[1] = EmitNeonCall(CGM.getIntrinsic(Int, &DTy, 1), Args, "vabal");
1175 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1176 return Builder.CreateAdd(Ops[0], Ops[1], "vabal");
1177 }
Nate Begeman998622c2010-06-07 16:01:56 +00001178 case ARM::BI__builtin_neon_vabd_v:
Nate Begeman30d91712010-06-08 06:03:01 +00001179 case ARM::BI__builtin_neon_vabdq_v:
Nate Begeman998622c2010-06-07 16:01:56 +00001180 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
Nate Begeman30d91712010-06-08 06:03:01 +00001181 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabd");
Bob Wilson47a88fa2010-09-03 01:27:09 +00001182 case ARM::BI__builtin_neon_vabdl_v: {
1183 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
1184 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1185 Ops[0] = EmitNeonCall(CGM.getIntrinsic(Int, &DTy, 1), Ops, "vabdl");
1186 return Builder.CreateZExt(Ops[0], Ty, "vabdl");
1187 }
Nate Begeman998622c2010-06-07 16:01:56 +00001188 case ARM::BI__builtin_neon_vabs_v:
Nate Begeman548f7da2010-06-10 18:11:55 +00001189 case ARM::BI__builtin_neon_vabsq_v:
1190 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vabs, &Ty, 1),
1191 Ops, "vabs");
1192 case ARM::BI__builtin_neon_vaddhn_v:
1193 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vaddhn, &Ty, 1),
1194 Ops, "vaddhn");
Bob Wilson22359412010-09-02 22:37:30 +00001195 case ARM::BI__builtin_neon_vaddl_v: {
1196 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1197 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
1198 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilson54160c62010-08-29 05:14:28 +00001199 if (usgn) {
1200 Ops[0] = Builder.CreateZExt(Ops[0], Ty);
1201 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1202 } else {
1203 Ops[0] = Builder.CreateSExt(Ops[0], Ty);
1204 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1205 }
1206 return Builder.CreateAdd(Ops[0], Ops[1], "vaddl");
Bob Wilson22359412010-09-02 22:37:30 +00001207 }
1208 case ARM::BI__builtin_neon_vaddw_v: {
1209 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1210 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1211 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilson54160c62010-08-29 05:14:28 +00001212 if (usgn)
1213 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1214 else
1215 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1216 return Builder.CreateAdd(Ops[0], Ops[1], "vaddw");
Bob Wilson22359412010-09-02 22:37:30 +00001217 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001218 case ARM::BI__builtin_neon_vcale_v:
1219 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001220 case ARM::BI__builtin_neon_vcage_v: {
1221 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacged, &Ty, 1);
1222 return EmitNeonCall(F, Ops, "vcage");
1223 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001224 case ARM::BI__builtin_neon_vcaleq_v:
1225 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001226 case ARM::BI__builtin_neon_vcageq_v: {
1227 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgeq, &Ty, 1);
1228 return EmitNeonCall(F, Ops, "vcage");
1229 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001230 case ARM::BI__builtin_neon_vcalt_v:
1231 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001232 case ARM::BI__builtin_neon_vcagt_v: {
1233 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtd, &Ty, 1);
1234 return EmitNeonCall(F, Ops, "vcagt");
1235 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001236 case ARM::BI__builtin_neon_vcaltq_v:
1237 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001238 case ARM::BI__builtin_neon_vcagtq_v: {
1239 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtq, &Ty, 1);
1240 return EmitNeonCall(F, Ops, "vcagt");
1241 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001242 case ARM::BI__builtin_neon_vcls_v:
1243 case ARM::BI__builtin_neon_vclsq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001244 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcls, &Ty, 1);
1245 return EmitNeonCall(F, Ops, "vcls");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001246 }
1247 case ARM::BI__builtin_neon_vclz_v:
1248 case ARM::BI__builtin_neon_vclzq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001249 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vclz, &Ty, 1);
1250 return EmitNeonCall(F, Ops, "vclz");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001251 }
1252 case ARM::BI__builtin_neon_vcnt_v:
1253 case ARM::BI__builtin_neon_vcntq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001254 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcnt, &Ty, 1);
1255 return EmitNeonCall(F, Ops, "vcnt");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001256 }
1257 // FIXME: intrinsics for f16<->f32 convert missing from ARM target.
1258 case ARM::BI__builtin_neon_vcvt_f32_v:
1259 case ARM::BI__builtin_neon_vcvtq_f32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001260 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1261 Ty = GetNeonType(VMContext, 4, quad);
Nate Begeman9eb65a52010-06-08 00:17:19 +00001262 return usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt")
1263 : Builder.CreateSIToFP(Ops[0], Ty, "vcvt");
1264 }
1265 case ARM::BI__builtin_neon_vcvt_s32_v:
1266 case ARM::BI__builtin_neon_vcvt_u32_v:
1267 case ARM::BI__builtin_neon_vcvtq_s32_v:
1268 case ARM::BI__builtin_neon_vcvtq_u32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001269 Ops[0] = Builder.CreateBitCast(Ops[0], GetNeonType(VMContext, 4, quad));
Nate Begeman9eb65a52010-06-08 00:17:19 +00001270 return usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt")
1271 : Builder.CreateFPToSI(Ops[0], Ty, "vcvt");
1272 }
1273 case ARM::BI__builtin_neon_vcvt_n_f32_v:
1274 case ARM::BI__builtin_neon_vcvtq_n_f32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001275 const llvm::Type *Tys[2] = { GetNeonType(VMContext, 4, quad), Ty };
Nate Begeman9eb65a52010-06-08 00:17:19 +00001276 Int = usgn ? Intrinsic::arm_neon_vcvtfxu2fp : Intrinsic::arm_neon_vcvtfxs2fp;
Nate Begeman30d91712010-06-08 06:03:01 +00001277 Function *F = CGM.getIntrinsic(Int, Tys, 2);
1278 return EmitNeonCall(F, Ops, "vcvt_n");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001279 }
1280 case ARM::BI__builtin_neon_vcvt_n_s32_v:
1281 case ARM::BI__builtin_neon_vcvt_n_u32_v:
1282 case ARM::BI__builtin_neon_vcvtq_n_s32_v:
1283 case ARM::BI__builtin_neon_vcvtq_n_u32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001284 const llvm::Type *Tys[2] = { Ty, GetNeonType(VMContext, 4, quad) };
Nate Begeman9eb65a52010-06-08 00:17:19 +00001285 Int = usgn ? Intrinsic::arm_neon_vcvtfp2fxu : Intrinsic::arm_neon_vcvtfp2fxs;
Nate Begeman30d91712010-06-08 06:03:01 +00001286 Function *F = CGM.getIntrinsic(Int, Tys, 2);
1287 return EmitNeonCall(F, Ops, "vcvt_n");
1288 }
Nate Begemancd480ad2010-08-06 01:24:57 +00001289 case ARM::BI__builtin_neon_vdup_lane_v:
1290 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1291 return EmitNeonSplat(Ops[0], cast<Constant>(Ops[1]));
1292 case ARM::BI__builtin_neon_vdupq_lane_v:
1293 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1294 return EmitNeonSplat(Ops[0], cast<Constant>(Ops[1]), true);
Nate Begeman30d91712010-06-08 06:03:01 +00001295 case ARM::BI__builtin_neon_vext_v:
1296 case ARM::BI__builtin_neon_vextq_v: {
1297 ConstantInt *C = dyn_cast<ConstantInt>(Ops[2]);
1298 int CV = C->getSExtValue();
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001299 SmallVector<Constant*, 16> Indices;
Nate Begeman4be54302010-06-20 23:05:28 +00001300 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
Chris Lattner77b89b82010-06-27 07:15:29 +00001301 Indices.push_back(ConstantInt::get(Int32Ty, i+CV));
Nate Begeman30d91712010-06-08 06:03:01 +00001302
1303 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1304 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1305 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001306 return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vext");
1307 }
Nate Begeman95450f62010-06-09 05:30:26 +00001308 case ARM::BI__builtin_neon_vget_lane_i8:
1309 case ARM::BI__builtin_neon_vget_lane_i16:
1310 case ARM::BI__builtin_neon_vget_lane_i32:
1311 case ARM::BI__builtin_neon_vget_lane_i64:
1312 case ARM::BI__builtin_neon_vget_lane_f32:
1313 case ARM::BI__builtin_neon_vgetq_lane_i8:
1314 case ARM::BI__builtin_neon_vgetq_lane_i16:
1315 case ARM::BI__builtin_neon_vgetq_lane_i32:
1316 case ARM::BI__builtin_neon_vgetq_lane_i64:
1317 case ARM::BI__builtin_neon_vgetq_lane_f32:
Nate Begemandf98e1d2010-06-09 18:04:15 +00001318 return Builder.CreateExtractElement(Ops[0], EmitScalarExpr(E->getArg(1)),
1319 "vget_lane");
1320 case ARM::BI__builtin_neon_vhadd_v:
1321 case ARM::BI__builtin_neon_vhaddq_v:
1322 Int = usgn ? Intrinsic::arm_neon_vhaddu : Intrinsic::arm_neon_vhadds;
1323 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhadd");
1324 case ARM::BI__builtin_neon_vhsub_v:
1325 case ARM::BI__builtin_neon_vhsubq_v:
1326 Int = usgn ? Intrinsic::arm_neon_vhsubu : Intrinsic::arm_neon_vhsubs;
1327 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhsub");
Nate Begeman4be54302010-06-20 23:05:28 +00001328 case ARM::BI__builtin_neon_vld1_v:
1329 case ARM::BI__builtin_neon_vld1q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001330 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman4be54302010-06-20 23:05:28 +00001331 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vld1, &Ty, 1),
1332 Ops, "vld1");
1333 case ARM::BI__builtin_neon_vld1_lane_v:
1334 case ARM::BI__builtin_neon_vld1q_lane_v:
1335 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1336 Ty = llvm::PointerType::getUnqual(VTy->getElementType());
1337 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1338 Ops[0] = Builder.CreateLoad(Ops[0]);
1339 return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vld1_lane");
1340 case ARM::BI__builtin_neon_vld1_dup_v:
1341 case ARM::BI__builtin_neon_vld1q_dup_v: {
1342 Value *V = UndefValue::get(Ty);
1343 Ty = llvm::PointerType::getUnqual(VTy->getElementType());
1344 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1345 Ops[0] = Builder.CreateLoad(Ops[0]);
Chris Lattner77b89b82010-06-27 07:15:29 +00001346 llvm::Constant *CI = ConstantInt::get(Int32Ty, 0);
Nate Begeman4be54302010-06-20 23:05:28 +00001347 Ops[0] = Builder.CreateInsertElement(V, Ops[0], CI);
1348 return EmitNeonSplat(Ops[0], CI);
1349 }
1350 case ARM::BI__builtin_neon_vld2_v:
1351 case ARM::BI__builtin_neon_vld2q_v: {
1352 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2, &Ty, 1);
Bob Wilson06b6c582010-08-27 17:14:29 +00001353 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1354 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld2");
Nate Begeman4be54302010-06-20 23:05:28 +00001355 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1356 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1357 return Builder.CreateStore(Ops[1], Ops[0]);
1358 }
1359 case ARM::BI__builtin_neon_vld3_v:
1360 case ARM::BI__builtin_neon_vld3q_v: {
1361 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3, &Ty, 1);
Bob Wilson06b6c582010-08-27 17:14:29 +00001362 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1363 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld3");
Nate Begeman4be54302010-06-20 23:05:28 +00001364 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1365 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1366 return Builder.CreateStore(Ops[1], Ops[0]);
1367 }
1368 case ARM::BI__builtin_neon_vld4_v:
1369 case ARM::BI__builtin_neon_vld4q_v: {
1370 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4, &Ty, 1);
Bob Wilson06b6c582010-08-27 17:14:29 +00001371 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1372 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld4");
Nate Begeman4be54302010-06-20 23:05:28 +00001373 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1374 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1375 return Builder.CreateStore(Ops[1], Ops[0]);
1376 }
1377 case ARM::BI__builtin_neon_vld2_lane_v:
1378 case ARM::BI__builtin_neon_vld2q_lane_v: {
1379 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2lane, &Ty, 1);
1380 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
1381 Ops[3] = Builder.CreateBitCast(Ops[3], Ty);
Bob Wilson06b6c582010-08-27 17:14:29 +00001382 Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001383 Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld2_lane");
1384 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1385 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1386 return Builder.CreateStore(Ops[1], Ops[0]);
1387 }
1388 case ARM::BI__builtin_neon_vld3_lane_v:
1389 case ARM::BI__builtin_neon_vld3q_lane_v: {
1390 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3lane, &Ty, 1);
1391 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
1392 Ops[3] = Builder.CreateBitCast(Ops[3], Ty);
1393 Ops[4] = Builder.CreateBitCast(Ops[4], Ty);
Bob Wilson06b6c582010-08-27 17:14:29 +00001394 Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001395 Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane");
1396 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1397 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1398 return Builder.CreateStore(Ops[1], Ops[0]);
1399 }
1400 case ARM::BI__builtin_neon_vld4_lane_v:
1401 case ARM::BI__builtin_neon_vld4q_lane_v: {
1402 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4lane, &Ty, 1);
1403 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
1404 Ops[3] = Builder.CreateBitCast(Ops[3], Ty);
1405 Ops[4] = Builder.CreateBitCast(Ops[4], Ty);
1406 Ops[5] = Builder.CreateBitCast(Ops[5], Ty);
Bob Wilson06b6c582010-08-27 17:14:29 +00001407 Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001408 Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane");
1409 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1410 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1411 return Builder.CreateStore(Ops[1], Ops[0]);
1412 }
1413 case ARM::BI__builtin_neon_vld2_dup_v:
1414 case ARM::BI__builtin_neon_vld3_dup_v:
1415 case ARM::BI__builtin_neon_vld4_dup_v: {
1416 switch (BuiltinID) {
1417 case ARM::BI__builtin_neon_vld2_dup_v:
1418 Int = Intrinsic::arm_neon_vld2lane;
1419 break;
1420 case ARM::BI__builtin_neon_vld3_dup_v:
1421 Int = Intrinsic::arm_neon_vld2lane;
1422 break;
1423 case ARM::BI__builtin_neon_vld4_dup_v:
1424 Int = Intrinsic::arm_neon_vld2lane;
1425 break;
1426 default: assert(0 && "unknown vld_dup intrinsic?");
1427 }
1428 Function *F = CGM.getIntrinsic(Int, &Ty, 1);
1429 const llvm::StructType *STy = cast<llvm::StructType>(F->getReturnType());
1430
1431 SmallVector<Value*, 6> Args;
1432 Args.push_back(Ops[1]);
1433 Args.append(STy->getNumElements(), UndefValue::get(Ty));
1434
Chris Lattner77b89b82010-06-27 07:15:29 +00001435 llvm::Constant *CI = ConstantInt::get(Int32Ty, 0);
Nate Begeman4be54302010-06-20 23:05:28 +00001436 Args.push_back(CI);
Bob Wilson06b6c582010-08-27 17:14:29 +00001437 Args.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001438
1439 Ops[1] = Builder.CreateCall(F, Args.begin(), Args.end(), "vld_dup");
1440 // splat lane 0 to all elts in each vector of the result.
1441 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1442 Value *Val = Builder.CreateExtractValue(Ops[1], i);
1443 Value *Elt = Builder.CreateBitCast(Val, Ty);
1444 Elt = EmitNeonSplat(Elt, CI);
1445 Elt = Builder.CreateBitCast(Elt, Val->getType());
1446 Ops[1] = Builder.CreateInsertValue(Ops[1], Elt, i);
1447 }
1448 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1449 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1450 return Builder.CreateStore(Ops[1], Ops[0]);
1451 }
Nate Begemandf98e1d2010-06-09 18:04:15 +00001452 case ARM::BI__builtin_neon_vmax_v:
1453 case ARM::BI__builtin_neon_vmaxq_v:
1454 Int = usgn ? Intrinsic::arm_neon_vmaxu : Intrinsic::arm_neon_vmaxs;
1455 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmax");
1456 case ARM::BI__builtin_neon_vmin_v:
1457 case ARM::BI__builtin_neon_vminq_v:
1458 Int = usgn ? Intrinsic::arm_neon_vminu : Intrinsic::arm_neon_vmins;
1459 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmin");
Bob Wilson22359412010-09-02 22:37:30 +00001460 case ARM::BI__builtin_neon_vmlal_lane_v: {
1461 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1462 Ops[2] = Builder.CreateBitCast(Ops[2], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001463 Ops[2] = EmitNeonSplat(Ops[2], cast<Constant>(Ops[3]));
Bob Wilson22359412010-09-02 22:37:30 +00001464 }
1465 case ARM::BI__builtin_neon_vmlal_v: {
1466 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1467 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1468 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
1469 Ops[2] = Builder.CreateBitCast(Ops[2], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001470 if (usgn) {
1471 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1472 Ops[2] = Builder.CreateZExt(Ops[2], Ty);
1473 } else {
1474 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1475 Ops[2] = Builder.CreateSExt(Ops[2], Ty);
1476 }
1477 Ops[1] = Builder.CreateMul(Ops[1], Ops[2]);
1478 return Builder.CreateAdd(Ops[0], Ops[1], "vmlal");
Bob Wilson22359412010-09-02 22:37:30 +00001479 }
1480 case ARM::BI__builtin_neon_vmlsl_lane_v: {
1481 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1482 Ops[2] = Builder.CreateBitCast(Ops[2], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001483 Ops[2] = EmitNeonSplat(Ops[2], cast<Constant>(Ops[3]));
Bob Wilson22359412010-09-02 22:37:30 +00001484 }
1485 case ARM::BI__builtin_neon_vmlsl_v: {
1486 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1487 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1488 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
1489 Ops[2] = Builder.CreateBitCast(Ops[2], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001490 if (usgn) {
1491 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1492 Ops[2] = Builder.CreateZExt(Ops[2], Ty);
1493 } else {
1494 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1495 Ops[2] = Builder.CreateSExt(Ops[2], Ty);
1496 }
1497 Ops[1] = Builder.CreateMul(Ops[1], Ops[2]);
1498 return Builder.CreateSub(Ops[0], Ops[1], "vmlsl");
Bob Wilson22359412010-09-02 22:37:30 +00001499 }
1500 case ARM::BI__builtin_neon_vmovl_v: {
1501 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1502 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
Bob Wilson7cea3222010-08-20 03:36:08 +00001503 if (usgn)
1504 return Builder.CreateZExt(Ops[0], Ty, "vmovl");
1505 return Builder.CreateSExt(Ops[0], Ty, "vmovl");
Bob Wilson22359412010-09-02 22:37:30 +00001506 }
1507 case ARM::BI__builtin_neon_vmovn_v: {
1508 const llvm::Type *QTy = llvm::VectorType::getExtendedElementVectorType(VTy);
1509 Ops[0] = Builder.CreateBitCast(Ops[0], QTy);
Bob Wilson3b6081b2010-08-30 19:57:13 +00001510 return Builder.CreateTrunc(Ops[0], Ty, "vmovn");
Bob Wilson22359412010-09-02 22:37:30 +00001511 }
1512 case ARM::BI__builtin_neon_vmull_lane_v: {
1513 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1514 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001515 Ops[1] = EmitNeonSplat(Ops[1], cast<Constant>(Ops[2]));
Bob Wilson22359412010-09-02 22:37:30 +00001516 }
1517 case ARM::BI__builtin_neon_vmull_v: {
Bob Wilsonbf346e92010-09-01 23:20:27 +00001518 if (poly)
1519 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vmullp, &Ty, 1),
1520 Ops, "vmull");
Bob Wilson22359412010-09-02 22:37:30 +00001521 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1522 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
1523 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilsonbf346e92010-09-01 23:20:27 +00001524 if (usgn) {
1525 Ops[0] = Builder.CreateZExt(Ops[0], Ty);
1526 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1527 } else {
1528 Ops[0] = Builder.CreateSExt(Ops[0], Ty);
1529 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1530 }
1531 return Builder.CreateMul(Ops[0], Ops[1], "vmull");
Bob Wilson22359412010-09-02 22:37:30 +00001532 }
Nate Begemandf98e1d2010-06-09 18:04:15 +00001533 case ARM::BI__builtin_neon_vpadal_v:
1534 case ARM::BI__builtin_neon_vpadalq_v:
1535 Int = usgn ? Intrinsic::arm_neon_vpadalu : Intrinsic::arm_neon_vpadals;
1536 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpadal");
Nate Begeman548f7da2010-06-10 18:11:55 +00001537 case ARM::BI__builtin_neon_vpadd_v:
1538 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vpadd, &Ty, 1),
1539 Ops, "vpadd");
1540 case ARM::BI__builtin_neon_vpaddl_v:
1541 case ARM::BI__builtin_neon_vpaddlq_v:
1542 Int = usgn ? Intrinsic::arm_neon_vpaddlu : Intrinsic::arm_neon_vpaddls;
1543 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpaddl");
1544 case ARM::BI__builtin_neon_vpmax_v:
1545 Int = usgn ? Intrinsic::arm_neon_vpmaxu : Intrinsic::arm_neon_vpmaxs;
1546 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmax");
1547 case ARM::BI__builtin_neon_vpmin_v:
1548 Int = usgn ? Intrinsic::arm_neon_vpminu : Intrinsic::arm_neon_vpmins;
1549 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmin");
1550 case ARM::BI__builtin_neon_vqabs_v:
1551 case ARM::BI__builtin_neon_vqabsq_v:
1552 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqabs, &Ty, 1),
1553 Ops, "vqabs");
1554 case ARM::BI__builtin_neon_vqadd_v:
1555 case ARM::BI__builtin_neon_vqaddq_v:
1556 Int = usgn ? Intrinsic::arm_neon_vqaddu : Intrinsic::arm_neon_vqadds;
1557 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqadd");
1558 case ARM::BI__builtin_neon_vqdmlal_lane_v:
1559 splat = true;
1560 case ARM::BI__builtin_neon_vqdmlal_v:
1561 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlal, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001562 Ops, "vqdmlal", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001563 case ARM::BI__builtin_neon_vqdmlsl_lane_v:
1564 splat = true;
1565 case ARM::BI__builtin_neon_vqdmlsl_v:
1566 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlsl, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001567 Ops, "vqdmlsl", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001568 case ARM::BI__builtin_neon_vqdmulh_lane_v:
1569 case ARM::BI__builtin_neon_vqdmulhq_lane_v:
1570 splat = true;
1571 case ARM::BI__builtin_neon_vqdmulh_v:
1572 case ARM::BI__builtin_neon_vqdmulhq_v:
1573 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmulh, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001574 Ops, "vqdmulh", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001575 case ARM::BI__builtin_neon_vqdmull_lane_v:
1576 splat = true;
1577 case ARM::BI__builtin_neon_vqdmull_v:
1578 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmull, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001579 Ops, "vqdmull", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001580 case ARM::BI__builtin_neon_vqmovn_v:
1581 Int = usgn ? Intrinsic::arm_neon_vqmovnu : Intrinsic::arm_neon_vqmovns;
1582 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqmovn");
1583 case ARM::BI__builtin_neon_vqmovun_v:
1584 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqmovnsu, &Ty, 1),
1585 Ops, "vqdmull");
1586 case ARM::BI__builtin_neon_vqneg_v:
Nate Begeman61eecf52010-06-14 05:21:25 +00001587 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqneg, &Ty, 1),
1588 Ops, "vqneg");
Nate Begeman548f7da2010-06-10 18:11:55 +00001589 case ARM::BI__builtin_neon_vqrdmulh_lane_v:
1590 case ARM::BI__builtin_neon_vqrdmulhq_lane_v:
1591 splat = true;
1592 case ARM::BI__builtin_neon_vqrdmulh_v:
1593 case ARM::BI__builtin_neon_vqrdmulhq_v:
1594 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrdmulh, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001595 Ops, "vqrdmulh", splat);
Nate Begeman548f7da2010-06-10 18:11:55 +00001596 case ARM::BI__builtin_neon_vqrshl_v:
1597 case ARM::BI__builtin_neon_vqrshlq_v:
1598 Int = usgn ? Intrinsic::arm_neon_vqrshiftu : Intrinsic::arm_neon_vqrshifts;
1599 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshl");
1600 case ARM::BI__builtin_neon_vqrshrn_n_v:
1601 Int = usgn ? Intrinsic::arm_neon_vqrshiftnu : Intrinsic::arm_neon_vqrshiftns;
Nate Begeman61eecf52010-06-14 05:21:25 +00001602 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshrn_n", false,
1603 1, true);
Nate Begeman548f7da2010-06-10 18:11:55 +00001604 case ARM::BI__builtin_neon_vqrshrun_n_v:
1605 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrshiftnsu, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001606 Ops, "vqrshrun_n", false, 1, true);
1607 case ARM::BI__builtin_neon_vqshl_v:
1608 case ARM::BI__builtin_neon_vqshlq_v:
1609 Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts;
1610 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl");
1611 case ARM::BI__builtin_neon_vqshl_n_v:
1612 case ARM::BI__builtin_neon_vqshlq_n_v:
1613 Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts;
1614 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl_n", false,
1615 1, false);
1616 case ARM::BI__builtin_neon_vqshlu_n_v:
1617 case ARM::BI__builtin_neon_vqshluq_n_v:
1618 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftsu, &Ty, 1),
1619 Ops, "vqshlu", 1, false);
1620 case ARM::BI__builtin_neon_vqshrn_n_v:
1621 Int = usgn ? Intrinsic::arm_neon_vqshiftnu : Intrinsic::arm_neon_vqshiftns;
1622 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshrn_n", false,
1623 1, true);
1624 case ARM::BI__builtin_neon_vqshrun_n_v:
1625 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftnsu, &Ty, 1),
1626 Ops, "vqshrun_n", false, 1, true);
Nate Begeman464ccb62010-06-11 22:57:12 +00001627 case ARM::BI__builtin_neon_vqsub_v:
1628 case ARM::BI__builtin_neon_vqsubq_v:
1629 Int = usgn ? Intrinsic::arm_neon_vqsubu : Intrinsic::arm_neon_vqsubs;
1630 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqsub");
1631 case ARM::BI__builtin_neon_vraddhn_v:
1632 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vraddhn, &Ty, 1),
1633 Ops, "vraddhn");
1634 case ARM::BI__builtin_neon_vrecpe_v:
1635 case ARM::BI__builtin_neon_vrecpeq_v:
1636 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecpe, &Ty, 1),
1637 Ops, "vrecpe");
1638 case ARM::BI__builtin_neon_vrecps_v:
1639 case ARM::BI__builtin_neon_vrecpsq_v:
1640 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecps, &Ty, 1),
1641 Ops, "vrecps");
Nate Begeman464ccb62010-06-11 22:57:12 +00001642 case ARM::BI__builtin_neon_vrhadd_v:
1643 case ARM::BI__builtin_neon_vrhaddq_v:
1644 Int = usgn ? Intrinsic::arm_neon_vrhaddu : Intrinsic::arm_neon_vrhadds;
1645 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrhadd");
Nate Begeman5af93ef2010-06-12 06:06:07 +00001646 case ARM::BI__builtin_neon_vrshl_v:
1647 case ARM::BI__builtin_neon_vrshlq_v:
1648 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
1649 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshl");
1650 case ARM::BI__builtin_neon_vrshrn_n_v:
Nate Begeman5af93ef2010-06-12 06:06:07 +00001651 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrshiftn, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001652 Ops, "vrshrn_n", false, 1, true);
Nate Begeman5af93ef2010-06-12 06:06:07 +00001653 case ARM::BI__builtin_neon_vrshr_n_v:
1654 case ARM::BI__builtin_neon_vrshrq_n_v:
Nate Begeman5af93ef2010-06-12 06:06:07 +00001655 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
Nate Begeman61eecf52010-06-14 05:21:25 +00001656 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshr_n", false,
1657 1, true);
Nate Begeman5af93ef2010-06-12 06:06:07 +00001658 case ARM::BI__builtin_neon_vrsqrte_v:
1659 case ARM::BI__builtin_neon_vrsqrteq_v:
1660 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrte, &Ty, 1),
1661 Ops, "vrsqrte");
1662 case ARM::BI__builtin_neon_vrsqrts_v:
1663 case ARM::BI__builtin_neon_vrsqrtsq_v:
1664 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrts, &Ty, 1),
1665 Ops, "vrsqrts");
1666 case ARM::BI__builtin_neon_vrsra_n_v:
1667 case ARM::BI__builtin_neon_vrsraq_n_v:
1668 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1669 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1670 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, true);
1671 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
1672 Ops[1] = Builder.CreateCall2(CGM.getIntrinsic(Int, &Ty, 1), Ops[1], Ops[2]);
1673 return Builder.CreateAdd(Ops[0], Ops[1], "vrsra_n");
Nate Begeman464ccb62010-06-11 22:57:12 +00001674 case ARM::BI__builtin_neon_vrsubhn_v:
1675 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsubhn, &Ty, 1),
1676 Ops, "vrsubhn");
Nate Begeman548f7da2010-06-10 18:11:55 +00001677 case ARM::BI__builtin_neon_vset_lane_i8:
1678 case ARM::BI__builtin_neon_vset_lane_i16:
1679 case ARM::BI__builtin_neon_vset_lane_i32:
1680 case ARM::BI__builtin_neon_vset_lane_i64:
1681 case ARM::BI__builtin_neon_vset_lane_f32:
1682 case ARM::BI__builtin_neon_vsetq_lane_i8:
1683 case ARM::BI__builtin_neon_vsetq_lane_i16:
1684 case ARM::BI__builtin_neon_vsetq_lane_i32:
1685 case ARM::BI__builtin_neon_vsetq_lane_i64:
1686 case ARM::BI__builtin_neon_vsetq_lane_f32:
1687 Ops.push_back(EmitScalarExpr(E->getArg(2)));
1688 return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vset_lane");
Nate Begeman464ccb62010-06-11 22:57:12 +00001689 case ARM::BI__builtin_neon_vshl_v:
1690 case ARM::BI__builtin_neon_vshlq_v:
1691 Int = usgn ? Intrinsic::arm_neon_vshiftu : Intrinsic::arm_neon_vshifts;
1692 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshl");
1693 case ARM::BI__builtin_neon_vshll_n_v:
Nate Begeman464ccb62010-06-11 22:57:12 +00001694 Int = usgn ? Intrinsic::arm_neon_vshiftlu : Intrinsic::arm_neon_vshiftls;
Nate Begeman61eecf52010-06-14 05:21:25 +00001695 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshll", false, 1);
Nate Begeman464ccb62010-06-11 22:57:12 +00001696 case ARM::BI__builtin_neon_vshl_n_v:
1697 case ARM::BI__builtin_neon_vshlq_n_v:
Nate Begeman61eecf52010-06-14 05:21:25 +00001698 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false);
1699 return Builder.CreateShl(Builder.CreateBitCast(Ops[0],Ty), Ops[1], "vshl_n");
Nate Begeman464ccb62010-06-11 22:57:12 +00001700 case ARM::BI__builtin_neon_vshrn_n_v:
Nate Begeman464ccb62010-06-11 22:57:12 +00001701 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftn, &Ty, 1),
Nate Begeman61eecf52010-06-14 05:21:25 +00001702 Ops, "vshrn_n", false, 1, true);
Nate Begeman464ccb62010-06-11 22:57:12 +00001703 case ARM::BI__builtin_neon_vshr_n_v:
1704 case ARM::BI__builtin_neon_vshrq_n_v:
1705 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
Nate Begeman61eecf52010-06-14 05:21:25 +00001706 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false);
Nate Begeman464ccb62010-06-11 22:57:12 +00001707 if (usgn)
1708 return Builder.CreateLShr(Ops[0], Ops[1], "vshr_n");
1709 else
1710 return Builder.CreateAShr(Ops[0], Ops[1], "vshr_n");
1711 case ARM::BI__builtin_neon_vsri_n_v:
1712 case ARM::BI__builtin_neon_vsriq_n_v:
1713 poly = true;
1714 case ARM::BI__builtin_neon_vsli_n_v:
1715 case ARM::BI__builtin_neon_vsliq_n_v:
1716 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, poly);
1717 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftins, &Ty, 1),
1718 Ops, "vsli_n");
1719 case ARM::BI__builtin_neon_vsra_n_v:
1720 case ARM::BI__builtin_neon_vsraq_n_v:
1721 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1722 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman61eecf52010-06-14 05:21:25 +00001723 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, false);
Nate Begeman464ccb62010-06-11 22:57:12 +00001724 if (usgn)
1725 Ops[1] = Builder.CreateLShr(Ops[1], Ops[2], "vsra_n");
1726 else
1727 Ops[1] = Builder.CreateAShr(Ops[1], Ops[2], "vsra_n");
1728 return Builder.CreateAdd(Ops[0], Ops[1]);
1729 case ARM::BI__builtin_neon_vst1_v:
1730 case ARM::BI__builtin_neon_vst1q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001731 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001732 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst1, &Ty, 1),
1733 Ops, "");
1734 case ARM::BI__builtin_neon_vst1_lane_v:
1735 case ARM::BI__builtin_neon_vst1q_lane_v:
1736 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1737 Ops[1] = Builder.CreateExtractElement(Ops[1], Ops[2]);
1738 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1739 return Builder.CreateStore(Ops[1], Builder.CreateBitCast(Ops[0], Ty));
1740 case ARM::BI__builtin_neon_vst2_v:
1741 case ARM::BI__builtin_neon_vst2q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001742 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001743 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2, &Ty, 1),
1744 Ops, "");
1745 case ARM::BI__builtin_neon_vst2_lane_v:
1746 case ARM::BI__builtin_neon_vst2q_lane_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001747 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001748 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2lane, &Ty, 1),
1749 Ops, "");
1750 case ARM::BI__builtin_neon_vst3_v:
1751 case ARM::BI__builtin_neon_vst3q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001752 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001753 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3, &Ty, 1),
1754 Ops, "");
1755 case ARM::BI__builtin_neon_vst3_lane_v:
1756 case ARM::BI__builtin_neon_vst3q_lane_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001757 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001758 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3lane, &Ty, 1),
1759 Ops, "");
1760 case ARM::BI__builtin_neon_vst4_v:
1761 case ARM::BI__builtin_neon_vst4q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001762 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001763 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4, &Ty, 1),
1764 Ops, "");
1765 case ARM::BI__builtin_neon_vst4_lane_v:
1766 case ARM::BI__builtin_neon_vst4q_lane_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001767 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001768 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4lane, &Ty, 1),
1769 Ops, "");
Nate Begeman548f7da2010-06-10 18:11:55 +00001770 case ARM::BI__builtin_neon_vsubhn_v:
1771 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vsubhn, &Ty, 1),
1772 Ops, "vsubhn");
Bob Wilson22359412010-09-02 22:37:30 +00001773 case ARM::BI__builtin_neon_vsubl_v: {
1774 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1775 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
1776 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilson54160c62010-08-29 05:14:28 +00001777 if (usgn) {
1778 Ops[0] = Builder.CreateZExt(Ops[0], Ty);
1779 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1780 } else {
1781 Ops[0] = Builder.CreateSExt(Ops[0], Ty);
1782 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1783 }
1784 return Builder.CreateSub(Ops[0], Ops[1], "vsubl");
Bob Wilson22359412010-09-02 22:37:30 +00001785 }
1786 case ARM::BI__builtin_neon_vsubw_v: {
1787 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1788 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1789 Ops[1] = Builder.CreateBitCast(Ops[1], DTy);
Bob Wilson54160c62010-08-29 05:14:28 +00001790 if (usgn)
1791 Ops[1] = Builder.CreateZExt(Ops[1], Ty);
1792 else
1793 Ops[1] = Builder.CreateSExt(Ops[1], Ty);
1794 return Builder.CreateSub(Ops[0], Ops[1], "vsubw");
Bob Wilson22359412010-09-02 22:37:30 +00001795 }
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001796 case ARM::BI__builtin_neon_vtbl1_v:
1797 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl1),
1798 Ops, "vtbl1");
1799 case ARM::BI__builtin_neon_vtbl2_v:
1800 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl2),
1801 Ops, "vtbl2");
1802 case ARM::BI__builtin_neon_vtbl3_v:
1803 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl3),
1804 Ops, "vtbl3");
1805 case ARM::BI__builtin_neon_vtbl4_v:
1806 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl4),
1807 Ops, "vtbl4");
1808 case ARM::BI__builtin_neon_vtbx1_v:
1809 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx1),
1810 Ops, "vtbx1");
1811 case ARM::BI__builtin_neon_vtbx2_v:
1812 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx2),
1813 Ops, "vtbx2");
1814 case ARM::BI__builtin_neon_vtbx3_v:
1815 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx3),
1816 Ops, "vtbx3");
1817 case ARM::BI__builtin_neon_vtbx4_v:
1818 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx4),
1819 Ops, "vtbx4");
1820 case ARM::BI__builtin_neon_vtst_v:
1821 case ARM::BI__builtin_neon_vtstq_v: {
1822 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1823 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1824 Ops[0] = Builder.CreateAnd(Ops[0], Ops[1]);
1825 Ops[0] = Builder.CreateICmp(ICmpInst::ICMP_NE, Ops[0],
1826 ConstantAggregateZero::get(Ty));
1827 return Builder.CreateSExt(Ops[0], Ty, "vtst");
1828 }
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001829 case ARM::BI__builtin_neon_vtrn_v:
1830 case ARM::BI__builtin_neon_vtrnq_v: {
Nate Begeman4be54302010-06-20 23:05:28 +00001831 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001832 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001833 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001834 Value *SV;
1835
1836 for (unsigned vi = 0; vi != 2; ++vi) {
1837 SmallVector<Constant*, 16> Indices;
1838 for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001839 Indices.push_back(ConstantInt::get(Int32Ty, i+vi));
1840 Indices.push_back(ConstantInt::get(Int32Ty, i+e+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, "vtrn");
1845 SV = Builder.CreateStore(SV, Addr);
1846 }
1847 return SV;
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001848 }
1849 case ARM::BI__builtin_neon_vuzp_v:
1850 case ARM::BI__builtin_neon_vuzpq_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)
Chris Lattner77b89b82010-06-27 07:15:29 +00001859 Indices.push_back(ConstantInt::get(Int32Ty, 2*i+vi));
Nate Begeman4be54302010-06-20 23:05:28 +00001860
1861 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
1862 SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1863 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vuzp");
1864 SV = Builder.CreateStore(SV, Addr);
1865 }
1866 return SV;
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001867 }
1868 case ARM::BI__builtin_neon_vzip_v:
1869 case ARM::BI__builtin_neon_vzipq_v: {
Nate Begeman4be54302010-06-20 23:05:28 +00001870 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001871 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001872 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001873 Value *SV;
1874
1875 for (unsigned vi = 0; vi != 2; ++vi) {
1876 SmallVector<Constant*, 16> Indices;
1877 for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
Daniel Dunbare361cc32010-08-26 00:55:57 +00001878 Indices.push_back(ConstantInt::get(Int32Ty, (i + vi*e) >> 1));
1879 Indices.push_back(ConstantInt::get(Int32Ty, ((i + vi*e) >> 1)+e));
Nate Begeman4be54302010-06-20 23:05:28 +00001880 }
1881 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
1882 SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1883 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vzip");
1884 SV = Builder.CreateStore(SV, Addr);
1885 }
1886 return SV;
Nate Begeman9eb65a52010-06-08 00:17:19 +00001887 }
Chris Lattner2752c012010-03-03 19:03:45 +00001888 }
1889}
1890
Bill Wendlingaa51e512010-10-09 08:47:25 +00001891llvm::Value *CodeGenFunction::
1892BuildVector(const llvm::SmallVectorImpl<llvm::Value*> &Ops) {
1893 assert((Ops.size() & (Ops.size() - 1)) == 0 &&
1894 "Not a power-of-two sized vector!");
1895 bool AllConstants = true;
1896 for (unsigned i = 0, e = Ops.size(); i != e && AllConstants; ++i)
1897 AllConstants &= isa<Constant>(Ops[i]);
1898
1899 // If this is a constant vector, create a ConstantVector.
1900 if (AllConstants) {
1901 std::vector<llvm::Constant*> CstOps;
1902 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1903 CstOps.push_back(cast<Constant>(Ops[i]));
1904 return llvm::ConstantVector::get(CstOps);
1905 }
1906
1907 // Otherwise, insertelement the values to build the vector.
1908 Value *Result =
1909 llvm::UndefValue::get(llvm::VectorType::get(Ops[0]->getType(), Ops.size()));
1910
1911 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1912 Result = Builder.CreateInsertElement(Result, Ops[i],
1913 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), i));
1914
1915 return Result;
1916}
1917
Mike Stump1eb44332009-09-09 15:08:12 +00001918Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +00001919 const CallExpr *E) {
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001920 llvm::SmallVector<Value*, 4> Ops;
1921
Chris Lattner46c55912010-10-02 00:09:12 +00001922 // Find out if any arguments are required to be integer constant expressions.
1923 unsigned ICEArguments = 0;
1924 ASTContext::GetBuiltinTypeError Error;
1925 getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
1926 assert(Error == ASTContext::GE_None && "Should not codegen an error");
1927
1928 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) {
1929 // If this is a normal argument, just emit it as a scalar.
1930 if ((ICEArguments & (1 << i)) == 0) {
1931 Ops.push_back(EmitScalarExpr(E->getArg(i)));
1932 continue;
1933 }
1934
1935 // If this is required to be a constant, constant fold it so that we know
1936 // that the generated intrinsic gets a ConstantInt.
1937 llvm::APSInt Result;
1938 bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result, getContext());
1939 assert(IsConst && "Constant arg isn't actually constant?"); (void)IsConst;
1940 Ops.push_back(llvm::ConstantInt::get(VMContext, Result));
1941 }
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001942
Anders Carlsson564f1de2007-12-09 23:17:02 +00001943 switch (BuiltinID) {
Anders Carlsson46a26b02007-12-09 23:39:18 +00001944 default: return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001945 case X86::BI__builtin_ia32_pslldi128:
Nate Begemane7722102008-04-14 04:49:57 +00001946 case X86::BI__builtin_ia32_psllqi128:
Mike Stump1eb44332009-09-09 15:08:12 +00001947 case X86::BI__builtin_ia32_psllwi128:
Nate Begemane7722102008-04-14 04:49:57 +00001948 case X86::BI__builtin_ia32_psradi128:
1949 case X86::BI__builtin_ia32_psrawi128:
1950 case X86::BI__builtin_ia32_psrldi128:
1951 case X86::BI__builtin_ia32_psrlqi128:
1952 case X86::BI__builtin_ia32_psrlwi128: {
Chris Lattner77b89b82010-06-27 07:15:29 +00001953 Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext");
1954 const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 2);
1955 llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
Owen Anderson03e20502009-07-30 23:11:26 +00001956 Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty),
Nate Begemane7722102008-04-14 04:49:57 +00001957 Ops[1], Zero, "insert");
1958 Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast");
1959 const char *name = 0;
1960 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Nate Begemane7722102008-04-14 04:49:57 +00001962 switch (BuiltinID) {
1963 default: assert(0 && "Unsupported shift intrinsic!");
1964 case X86::BI__builtin_ia32_pslldi128:
1965 name = "pslldi";
1966 ID = Intrinsic::x86_sse2_psll_d;
1967 break;
1968 case X86::BI__builtin_ia32_psllqi128:
1969 name = "psllqi";
1970 ID = Intrinsic::x86_sse2_psll_q;
1971 break;
1972 case X86::BI__builtin_ia32_psllwi128:
1973 name = "psllwi";
1974 ID = Intrinsic::x86_sse2_psll_w;
1975 break;
1976 case X86::BI__builtin_ia32_psradi128:
1977 name = "psradi";
1978 ID = Intrinsic::x86_sse2_psra_d;
1979 break;
1980 case X86::BI__builtin_ia32_psrawi128:
1981 name = "psrawi";
1982 ID = Intrinsic::x86_sse2_psra_w;
1983 break;
1984 case X86::BI__builtin_ia32_psrldi128:
1985 name = "psrldi";
1986 ID = Intrinsic::x86_sse2_psrl_d;
1987 break;
1988 case X86::BI__builtin_ia32_psrlqi128:
1989 name = "psrlqi";
1990 ID = Intrinsic::x86_sse2_psrl_q;
1991 break;
1992 case X86::BI__builtin_ia32_psrlwi128:
1993 name = "psrlwi";
1994 ID = Intrinsic::x86_sse2_psrl_w;
1995 break;
1996 }
1997 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001998 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Nate Begemane7722102008-04-14 04:49:57 +00001999 }
Bill Wendlingaa51e512010-10-09 08:47:25 +00002000 case X86::BI__builtin_ia32_vec_init_v8qi:
2001 case X86::BI__builtin_ia32_vec_init_v4hi:
2002 case X86::BI__builtin_ia32_vec_init_v2si:
2003 return Builder.CreateBitCast(BuildVector(Ops),
2004 llvm::Type::getX86_MMXTy(VMContext));
Mike Stump1eb44332009-09-09 15:08:12 +00002005 case X86::BI__builtin_ia32_pslldi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002006 case X86::BI__builtin_ia32_psllqi:
Mike Stump1eb44332009-09-09 15:08:12 +00002007 case X86::BI__builtin_ia32_psllwi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002008 case X86::BI__builtin_ia32_psradi:
2009 case X86::BI__builtin_ia32_psrawi:
2010 case X86::BI__builtin_ia32_psrldi:
2011 case X86::BI__builtin_ia32_psrlqi:
2012 case X86::BI__builtin_ia32_psrlwi: {
Chris Lattner77b89b82010-06-27 07:15:29 +00002013 Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext");
2014 const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 1);
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002015 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002016 const char *name = 0;
2017 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002019 switch (BuiltinID) {
2020 default: assert(0 && "Unsupported shift intrinsic!");
2021 case X86::BI__builtin_ia32_pslldi:
2022 name = "pslldi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00002023 ID = Intrinsic::x86_mmx_psll_d;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002024 break;
2025 case X86::BI__builtin_ia32_psllqi:
2026 name = "psllqi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00002027 ID = Intrinsic::x86_mmx_psll_q;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002028 break;
2029 case X86::BI__builtin_ia32_psllwi:
2030 name = "psllwi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00002031 ID = Intrinsic::x86_mmx_psll_w;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002032 break;
2033 case X86::BI__builtin_ia32_psradi:
2034 name = "psradi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00002035 ID = Intrinsic::x86_mmx_psra_d;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002036 break;
2037 case X86::BI__builtin_ia32_psrawi:
2038 name = "psrawi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00002039 ID = Intrinsic::x86_mmx_psra_w;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002040 break;
2041 case X86::BI__builtin_ia32_psrldi:
2042 name = "psrldi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00002043 ID = Intrinsic::x86_mmx_psrl_d;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002044 break;
2045 case X86::BI__builtin_ia32_psrlqi:
2046 name = "psrlqi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00002047 ID = Intrinsic::x86_mmx_psrl_q;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002048 break;
2049 case X86::BI__builtin_ia32_psrlwi:
2050 name = "psrlwi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00002051 ID = Intrinsic::x86_mmx_psrl_w;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002052 break;
2053 }
Chris Lattner7acda7c2007-12-18 00:25:38 +00002054 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00002055 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Anders Carlsson2929cfa2007-12-14 17:48:24 +00002056 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +00002057 case X86::BI__builtin_ia32_cmpps: {
2058 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps);
2059 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps");
2060 }
2061 case X86::BI__builtin_ia32_cmpss: {
2062 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss);
2063 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +00002064 }
Nate Begemane7722102008-04-14 04:49:57 +00002065 case X86::BI__builtin_ia32_ldmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002066 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00002067 Value *One = llvm::ConstantInt::get(Int32Ty, 1);
2068 Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +00002069 Builder.CreateStore(Ops[0], Tmp);
2070 return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +00002071 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +00002072 }
2073 case X86::BI__builtin_ia32_stmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002074 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00002075 Value *One = llvm::ConstantInt::get(Int32Ty, 1);
2076 Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +00002077 One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +00002078 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +00002079 return Builder.CreateLoad(Tmp, "stmxcsr");
2080 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +00002081 case X86::BI__builtin_ia32_cmppd: {
2082 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd);
2083 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd");
2084 }
2085 case X86::BI__builtin_ia32_cmpsd: {
2086 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd);
2087 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +00002088 }
Nate Begemane7722102008-04-14 04:49:57 +00002089 case X86::BI__builtin_ia32_storehps:
2090 case X86::BI__builtin_ia32_storelps: {
Chris Lattner77b89b82010-06-27 07:15:29 +00002091 llvm::Type *PtrTy = llvm::PointerType::getUnqual(Int64Ty);
2092 llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00002093
Nate Begemane7722102008-04-14 04:49:57 +00002094 // cast val v2i64
2095 Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast");
Mike Stump1eb44332009-09-09 15:08:12 +00002096
Nate Begemane7722102008-04-14 04:49:57 +00002097 // extract (0, 1)
2098 unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1;
Chris Lattner77b89b82010-06-27 07:15:29 +00002099 llvm::Value *Idx = llvm::ConstantInt::get(Int32Ty, Index);
Nate Begemane7722102008-04-14 04:49:57 +00002100 Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract");
2101
2102 // cast pointer to i64 & store
2103 Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy);
2104 return Builder.CreateStore(Ops[1], Ops[0]);
2105 }
Bill Wendling28cab382010-09-28 01:28:56 +00002106 case X86::BI__builtin_ia32_palignr: {
2107 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
2108
2109 // If palignr is shifting the pair of input vectors less than 9 bytes,
2110 // emit a shuffle instruction.
2111 if (shiftVal <= 8) {
2112 llvm::SmallVector<llvm::Constant*, 8> Indices;
2113 for (unsigned i = 0; i != 8; ++i)
2114 Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i));
2115
2116 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
2117 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
2118 }
2119
2120 // If palignr is shifting the pair of input vectors more than 8 but less
2121 // than 16 bytes, emit a logical right shift of the destination.
2122 if (shiftVal < 16) {
2123 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
2124 const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 1);
2125
2126 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
2127 Ops[1] = llvm::ConstantInt::get(VecTy, (shiftVal-8) * 8);
2128
2129 // create i32 constant
2130 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_mmx_psrl_q);
2131 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
2132 }
2133
2134 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
2135 return llvm::Constant::getNullValue(ConvertType(E->getType()));
2136 }
Nate Begemanc3420ff2009-12-14 05:15:02 +00002137 case X86::BI__builtin_ia32_palignr128: {
Nate Begemance5818a2009-12-14 04:57:03 +00002138 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
2139
2140 // If palignr is shifting the pair of input vectors less than 17 bytes,
2141 // emit a shuffle instruction.
2142 if (shiftVal <= 16) {
Nate Begemance5818a2009-12-14 04:57:03 +00002143 llvm::SmallVector<llvm::Constant*, 16> Indices;
2144 for (unsigned i = 0; i != 16; ++i)
Chris Lattner77b89b82010-06-27 07:15:29 +00002145 Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i));
Nate Begemance5818a2009-12-14 04:57:03 +00002146
2147 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
2148 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
2149 }
2150
2151 // If palignr is shifting the pair of input vectors more than 16 but less
2152 // than 32 bytes, emit a logical right shift of the destination.
2153 if (shiftVal < 32) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002154 const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2);
Nate Begemance5818a2009-12-14 04:57:03 +00002155
2156 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
Chris Lattner77b89b82010-06-27 07:15:29 +00002157 Ops[1] = llvm::ConstantInt::get(Int32Ty, (shiftVal-16) * 8);
Nate Begemance5818a2009-12-14 04:57:03 +00002158
2159 // create i32 constant
2160 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq);
2161 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
2162 }
2163
2164 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
2165 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Eric Christopher91b59272009-12-01 05:00:51 +00002166 }
Anders Carlsson564f1de2007-12-09 23:17:02 +00002167 }
2168}
2169
Mike Stump1eb44332009-09-09 15:08:12 +00002170Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +00002171 const CallExpr *E) {
Chris Lattnerdd173942010-04-14 03:54:58 +00002172 llvm::SmallVector<Value*, 4> Ops;
2173
2174 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
2175 Ops.push_back(EmitScalarExpr(E->getArg(i)));
2176
2177 Intrinsic::ID ID = Intrinsic::not_intrinsic;
2178
2179 switch (BuiltinID) {
2180 default: return 0;
2181
Anton Korobeynikov4d3a7b02010-06-19 09:47:18 +00002182 // vec_ld, vec_lvsl, vec_lvsr
2183 case PPC::BI__builtin_altivec_lvx:
2184 case PPC::BI__builtin_altivec_lvxl:
2185 case PPC::BI__builtin_altivec_lvebx:
2186 case PPC::BI__builtin_altivec_lvehx:
2187 case PPC::BI__builtin_altivec_lvewx:
2188 case PPC::BI__builtin_altivec_lvsl:
2189 case PPC::BI__builtin_altivec_lvsr:
2190 {
2191 Ops[1] = Builder.CreateBitCast(Ops[1], llvm::Type::getInt8PtrTy(VMContext));
2192
2193 Ops[0] = Builder.CreateGEP(Ops[1], Ops[0], "tmp");
2194 Ops.pop_back();
2195
2196 switch (BuiltinID) {
2197 default: assert(0 && "Unsupported ld/lvsl/lvsr intrinsic!");
2198 case PPC::BI__builtin_altivec_lvx:
2199 ID = Intrinsic::ppc_altivec_lvx;
2200 break;
2201 case PPC::BI__builtin_altivec_lvxl:
2202 ID = Intrinsic::ppc_altivec_lvxl;
2203 break;
2204 case PPC::BI__builtin_altivec_lvebx:
2205 ID = Intrinsic::ppc_altivec_lvebx;
2206 break;
2207 case PPC::BI__builtin_altivec_lvehx:
2208 ID = Intrinsic::ppc_altivec_lvehx;
2209 break;
2210 case PPC::BI__builtin_altivec_lvewx:
2211 ID = Intrinsic::ppc_altivec_lvewx;
2212 break;
2213 case PPC::BI__builtin_altivec_lvsl:
2214 ID = Intrinsic::ppc_altivec_lvsl;
2215 break;
2216 case PPC::BI__builtin_altivec_lvsr:
2217 ID = Intrinsic::ppc_altivec_lvsr;
2218 break;
2219 }
2220 llvm::Function *F = CGM.getIntrinsic(ID);
2221 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "");
2222 }
2223
Chris Lattnerdd173942010-04-14 03:54:58 +00002224 // vec_st
2225 case PPC::BI__builtin_altivec_stvx:
2226 case PPC::BI__builtin_altivec_stvxl:
2227 case PPC::BI__builtin_altivec_stvebx:
2228 case PPC::BI__builtin_altivec_stvehx:
2229 case PPC::BI__builtin_altivec_stvewx:
2230 {
2231 Ops[2] = Builder.CreateBitCast(Ops[2], llvm::Type::getInt8PtrTy(VMContext));
Anton Korobeynikov4d3a7b02010-06-19 09:47:18 +00002232 Ops[1] = Builder.CreateGEP(Ops[2], Ops[1], "tmp");
Chris Lattnerdd173942010-04-14 03:54:58 +00002233 Ops.pop_back();
2234
2235 switch (BuiltinID) {
Anton Korobeynikov4d3a7b02010-06-19 09:47:18 +00002236 default: assert(0 && "Unsupported st intrinsic!");
Chris Lattnerdd173942010-04-14 03:54:58 +00002237 case PPC::BI__builtin_altivec_stvx:
2238 ID = Intrinsic::ppc_altivec_stvx;
2239 break;
2240 case PPC::BI__builtin_altivec_stvxl:
2241 ID = Intrinsic::ppc_altivec_stvxl;
2242 break;
2243 case PPC::BI__builtin_altivec_stvebx:
2244 ID = Intrinsic::ppc_altivec_stvebx;
2245 break;
2246 case PPC::BI__builtin_altivec_stvehx:
2247 ID = Intrinsic::ppc_altivec_stvehx;
2248 break;
2249 case PPC::BI__builtin_altivec_stvewx:
2250 ID = Intrinsic::ppc_altivec_stvewx;
2251 break;
2252 }
2253 llvm::Function *F = CGM.getIntrinsic(ID);
2254 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "");
2255 }
2256 }
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002257 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002258}