blob: 19c3fbb1fa595ffc8f22cf82b472cbde43c5c44b [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
John McCall26815d92010-10-27 20:58:56 +000044/// Emit the conversions required to turn the given value into an
45/// integer of the given size.
46static Value *EmitToInt(CodeGenFunction &CGF, llvm::Value *V,
47 QualType T, const llvm::IntegerType *IntType) {
48 V = CGF.EmitToMemory(V, T);
Chris Lattnera1aa9e32010-10-01 23:43:16 +000049
John McCall26815d92010-10-27 20:58:56 +000050 if (V->getType()->isPointerTy())
51 return CGF.Builder.CreatePtrToInt(V, IntType);
52
53 assert(V->getType() == IntType);
54 return V;
Chandler Carruthdb4325b2010-07-18 07:23:17 +000055}
56
John McCall26815d92010-10-27 20:58:56 +000057static Value *EmitFromInt(CodeGenFunction &CGF, llvm::Value *V,
58 QualType T, const llvm::Type *ResultType) {
59 V = CGF.EmitFromMemory(V, T);
60
61 if (ResultType->isPointerTy())
62 return CGF.Builder.CreateIntToPtr(V, ResultType);
63
64 assert(V->getType() == ResultType);
65 return V;
Chandler Carruthdb4325b2010-07-18 07:23:17 +000066}
67
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000068// The atomic builtins are also full memory barriers. This is a utility for
69// wrapping a call to the builtins with memory barriers.
70static Value *EmitCallWithBarrier(CodeGenFunction &CGF, Value *Fn,
71 Value **ArgBegin, Value **ArgEnd) {
72 // FIXME: We need a target hook for whether this applies to device memory or
73 // not.
74 bool Device = true;
75
76 // Create barriers both before and after the call.
77 EmitMemoryBarrier(CGF, true, true, true, true, Device);
78 Value *Result = CGF.Builder.CreateCall(Fn, ArgBegin, ArgEnd);
79 EmitMemoryBarrier(CGF, true, true, true, true, Device);
80 return Result;
81}
82
Daniel Dunbar0002d232009-04-07 00:55:51 +000083/// Utility to insert an atomic instruction based on Instrinsic::ID
84/// and the expression node.
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +000085static RValue EmitBinaryAtomic(CodeGenFunction &CGF,
Mon P Wang1ffe2812008-05-09 22:40:52 +000086 Intrinsic::ID Id, const CallExpr *E) {
John McCall26815d92010-10-27 20:58:56 +000087 QualType T = E->getType();
88 assert(E->getArg(0)->getType()->isPointerType());
89 assert(CGF.getContext().hasSameUnqualifiedType(T,
90 E->getArg(0)->getType()->getPointeeType()));
91 assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
92
Chris Lattner4f209442010-09-21 23:40:48 +000093 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
94 unsigned AddrSpace =
95 cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
John McCall789a1592010-10-26 22:09:15 +000096
John McCall26815d92010-10-27 20:58:56 +000097 const llvm::IntegerType *IntType =
98 llvm::IntegerType::get(CGF.getLLVMContext(),
99 CGF.getContext().getTypeSize(T));
100 const llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
101
102 const llvm::Type *IntrinsicTypes[2] = { IntType, IntPtrType };
103 llvm::Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2);
104
105 llvm::Value *Args[2];
106 Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
107 Args[1] = CGF.EmitScalarExpr(E->getArg(1));
108 const llvm::Type *ValueType = Args[1]->getType();
109 Args[1] = EmitToInt(CGF, Args[1], T, IntType);
110
111 llvm::Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 2);
112 Result = EmitFromInt(CGF, Result, T, ValueType);
113 return RValue::get(Result);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000114}
115
116/// Utility to insert an atomic instruction based Instrinsic::ID and
John McCall26815d92010-10-27 20:58:56 +0000117/// the expression node, where the return value is the result of the
118/// operation.
Chris Lattner420b1182010-05-06 05:35:16 +0000119static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF,
Daniel Dunbar0002d232009-04-07 00:55:51 +0000120 Intrinsic::ID Id, const CallExpr *E,
121 Instruction::BinaryOps Op) {
John McCall26815d92010-10-27 20:58:56 +0000122 QualType T = E->getType();
123 assert(E->getArg(0)->getType()->isPointerType());
124 assert(CGF.getContext().hasSameUnqualifiedType(T,
125 E->getArg(0)->getType()->getPointeeType()));
126 assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
127
Chris Lattner4f209442010-09-21 23:40:48 +0000128 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
129 unsigned AddrSpace =
130 cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
John McCall789a1592010-10-26 22:09:15 +0000131
John McCall26815d92010-10-27 20:58:56 +0000132 const llvm::IntegerType *IntType =
133 llvm::IntegerType::get(CGF.getLLVMContext(),
134 CGF.getContext().getTypeSize(T));
135 const llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
136
137 const llvm::Type *IntrinsicTypes[2] = { IntType, IntPtrType };
138 llvm::Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2);
139
140 llvm::Value *Args[2];
141 Args[1] = CGF.EmitScalarExpr(E->getArg(1));
142 const llvm::Type *ValueType = Args[1]->getType();
143 Args[1] = EmitToInt(CGF, Args[1], T, IntType);
144 Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
145
146 llvm::Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 2);
147 Result = CGF.Builder.CreateBinOp(Op, Result, Args[1]);
148 Result = EmitFromInt(CGF, Result, T, ValueType);
149 return RValue::get(Result);
Mon P Wang1ffe2812008-05-09 22:40:52 +0000150}
151
Chris Lattner420b1182010-05-06 05:35:16 +0000152/// EmitFAbs - Emit a call to fabs/fabsf/fabsl, depending on the type of ValTy,
153/// which must be a scalar floating point type.
154static Value *EmitFAbs(CodeGenFunction &CGF, Value *V, QualType ValTy) {
155 const BuiltinType *ValTyP = ValTy->getAs<BuiltinType>();
156 assert(ValTyP && "isn't scalar fp type!");
157
158 StringRef FnName;
159 switch (ValTyP->getKind()) {
160 default: assert(0 && "Isn't a scalar fp type!");
161 case BuiltinType::Float: FnName = "fabsf"; break;
162 case BuiltinType::Double: FnName = "fabs"; break;
163 case BuiltinType::LongDouble: FnName = "fabsl"; break;
164 }
165
166 // The prototype is something that takes and returns whatever V's type is.
167 std::vector<const llvm::Type*> Args;
168 Args.push_back(V->getType());
169 llvm::FunctionType *FT = llvm::FunctionType::get(V->getType(), Args, false);
170 llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction(FT, FnName);
171
172 return CGF.Builder.CreateCall(Fn, V, "abs");
173}
174
Mike Stump1eb44332009-09-09 15:08:12 +0000175RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000176 unsigned BuiltinID, const CallExpr *E) {
Chris Lattner564ea2a2008-10-06 06:56:41 +0000177 // See if we can constant fold this builtin. If so, don't emit it at all.
Anders Carlssonf35d35a2008-12-01 02:31:41 +0000178 Expr::EvalResult Result;
Chris Lattner6ee7aa12008-11-16 21:24:15 +0000179 if (E->Evaluate(Result, CGM.getContext())) {
Anders Carlssonf35d35a2008-12-01 02:31:41 +0000180 if (Result.Val.isInt())
Mike Stump1eb44332009-09-09 15:08:12 +0000181 return RValue::get(llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000182 Result.Val.getInt()));
Chris Lattnera1aa9e32010-10-01 23:43:16 +0000183 if (Result.Val.isFloat())
Owen Andersonbc0a2222009-07-27 21:00:51 +0000184 return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat()));
Chris Lattner1f329992008-10-06 06:09:18 +0000185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattner564ea2a2008-10-06 06:56:41 +0000187 switch (BuiltinID) {
188 default: break; // Handle intrinsics and libm functions below.
Chris Lattner506ff882008-10-06 07:26:43 +0000189 case Builtin::BI__builtin___CFStringMakeConstantString:
David Chisnall0d13f6f2010-01-23 02:40:42 +0000190 case Builtin::BI__builtin___NSStringMakeConstantString:
Anders Carlssone9352cc2009-04-08 04:48:15 +0000191 return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0));
Chris Lattner6a705f02008-07-09 17:28:44 +0000192 case Builtin::BI__builtin_stdarg_start:
Anders Carlsson793680e2007-10-12 23:56:29 +0000193 case Builtin::BI__builtin_va_start:
194 case Builtin::BI__builtin_va_end: {
Daniel Dunbar07855702009-02-11 22:25:55 +0000195 Value *ArgValue = EmitVAListRef(E->getArg(0));
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000196 const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson793680e2007-10-12 23:56:29 +0000197 if (ArgValue->getType() != DestType)
Mike Stump1eb44332009-09-09 15:08:12 +0000198 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
Daniel Dunbarb27ffbe2009-07-26 09:28:40 +0000199 ArgValue->getName().data());
Anders Carlsson793680e2007-10-12 23:56:29 +0000200
Mike Stump1eb44332009-09-09 15:08:12 +0000201 Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ?
Chris Lattner6a705f02008-07-09 17:28:44 +0000202 Intrinsic::vaend : Intrinsic::vastart;
Chris Lattner7acda7c2007-12-18 00:25:38 +0000203 return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue));
Anders Carlsson793680e2007-10-12 23:56:29 +0000204 }
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000205 case Builtin::BI__builtin_va_copy: {
Eli Friedman4fd0aa52009-01-20 17:46:04 +0000206 Value *DstPtr = EmitVAListRef(E->getArg(0));
207 Value *SrcPtr = EmitVAListRef(E->getArg(1));
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000208
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000209 const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000210
211 DstPtr = Builder.CreateBitCast(DstPtr, Type);
212 SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000213 return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000214 DstPtr, SrcPtr));
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000215 }
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000216 case Builtin::BI__builtin_abs: {
Mike Stump1eb44332009-09-09 15:08:12 +0000217 Value *ArgValue = EmitScalarExpr(E->getArg(0));
218
Chris Lattner9a847f52008-07-23 06:53:34 +0000219 Value *NegOp = Builder.CreateNeg(ArgValue, "neg");
Mike Stump1eb44332009-09-09 15:08:12 +0000220 Value *CmpResult =
221 Builder.CreateICmpSGE(ArgValue,
Owen Andersonc9c88b42009-07-31 20:28:54 +0000222 llvm::Constant::getNullValue(ArgValue->getType()),
Chris Lattner9a847f52008-07-23 06:53:34 +0000223 "abscond");
Mike Stump1eb44332009-09-09 15:08:12 +0000224 Value *Result =
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000225 Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000227 return RValue::get(Result);
228 }
Anders Carlsson3a31d602008-02-06 07:19:27 +0000229 case Builtin::BI__builtin_ctz:
230 case Builtin::BI__builtin_ctzl:
231 case Builtin::BI__builtin_ctzll: {
232 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Anders Carlsson3a31d602008-02-06 07:19:27 +0000234 const llvm::Type *ArgType = ArgValue->getType();
235 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
236
Mike Stump1eb44332009-09-09 15:08:12 +0000237 const llvm::Type *ResultType = ConvertType(E->getType());
Anders Carlsson3a31d602008-02-06 07:19:27 +0000238 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
239 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000240 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
241 "cast");
Anders Carlsson3a31d602008-02-06 07:19:27 +0000242 return RValue::get(Result);
243 }
Eli Friedmanf4e85332008-05-27 15:32:46 +0000244 case Builtin::BI__builtin_clz:
245 case Builtin::BI__builtin_clzl:
246 case Builtin::BI__builtin_clzll: {
247 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Eli Friedmanf4e85332008-05-27 15:32:46 +0000249 const llvm::Type *ArgType = ArgValue->getType();
250 Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1);
251
Mike Stump1eb44332009-09-09 15:08:12 +0000252 const llvm::Type *ResultType = ConvertType(E->getType());
Eli Friedmanf4e85332008-05-27 15:32:46 +0000253 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
254 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000255 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
256 "cast");
Eli Friedmanf4e85332008-05-27 15:32:46 +0000257 return RValue::get(Result);
258 }
Daniel Dunbar04b29002008-07-21 17:19:41 +0000259 case Builtin::BI__builtin_ffs:
260 case Builtin::BI__builtin_ffsl:
261 case Builtin::BI__builtin_ffsll: {
262 // ffs(x) -> x ? cttz(x) + 1 : 0
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::cttz, &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());
Mike Stump1eb44332009-09-09 15:08:12 +0000269 Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"),
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000270 llvm::ConstantInt::get(ArgType, 1), "tmp");
Owen Andersonc9c88b42009-07-31 20:28:54 +0000271 Value *Zero = llvm::Constant::getNullValue(ArgType);
Daniel Dunbar04b29002008-07-21 17:19:41 +0000272 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
273 Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
274 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000275 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
276 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000277 return RValue::get(Result);
278 }
279 case Builtin::BI__builtin_parity:
280 case Builtin::BI__builtin_parityl:
281 case Builtin::BI__builtin_parityll: {
282 // parity(x) -> ctpop(x) & 1
283 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Daniel Dunbar04b29002008-07-21 17:19:41 +0000285 const llvm::Type *ArgType = ArgValue->getType();
286 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Daniel Dunbar04b29002008-07-21 17:19:41 +0000288 const llvm::Type *ResultType = ConvertType(E->getType());
289 Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000290 Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1),
Daniel Dunbar04b29002008-07-21 17:19:41 +0000291 "tmp");
292 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000293 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
294 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000295 return RValue::get(Result);
296 }
297 case Builtin::BI__builtin_popcount:
298 case Builtin::BI__builtin_popcountl:
299 case Builtin::BI__builtin_popcountll: {
300 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Daniel Dunbar04b29002008-07-21 17:19:41 +0000302 const llvm::Type *ArgType = ArgValue->getType();
303 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Daniel Dunbar04b29002008-07-21 17:19:41 +0000305 const llvm::Type *ResultType = ConvertType(E->getType());
306 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
307 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000308 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
309 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000310 return RValue::get(Result);
311 }
Fariborz Jahaniane42b8a52010-07-26 23:11:03 +0000312 case Builtin::BI__builtin_expect: {
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000313 // FIXME: pass expect through to LLVM
Fariborz Jahaniane42b8a52010-07-26 23:11:03 +0000314 if (E->getArg(1)->HasSideEffects(getContext()))
315 (void)EmitScalarExpr(E->getArg(1));
Chris Lattner1feedd82007-12-13 07:34:23 +0000316 return RValue::get(EmitScalarExpr(E->getArg(0)));
Fariborz Jahaniane42b8a52010-07-26 23:11:03 +0000317 }
Anders Carlssondf4852a2007-12-02 21:58:10 +0000318 case Builtin::BI__builtin_bswap32:
319 case Builtin::BI__builtin_bswap64: {
Chris Lattner1feedd82007-12-13 07:34:23 +0000320 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000321 const llvm::Type *ArgType = ArgValue->getType();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000322 Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
Chris Lattner1feedd82007-12-13 07:34:23 +0000323 return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
Mike Stump1eb44332009-09-09 15:08:12 +0000324 }
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000325 case Builtin::BI__builtin_object_size: {
Mike Stumpb16d32f2009-10-26 23:39:48 +0000326 // We pass this builtin onto the optimizer so that it can
327 // figure out the object size in more complex cases.
Mike Stumpc4c90452009-10-27 22:09:17 +0000328 const llvm::Type *ResType[] = {
329 ConvertType(E->getType())
330 };
Eric Christopherfee667f2009-12-23 03:49:37 +0000331
332 // LLVM only supports 0 and 2, make sure that we pass along that
333 // as a boolean.
334 Value *Ty = EmitScalarExpr(E->getArg(1));
335 ConstantInt *CI = dyn_cast<ConstantInt>(Ty);
336 assert(CI);
337 uint64_t val = CI->getZExtValue();
338 CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1);
339
Mike Stumpc4c90452009-10-27 22:09:17 +0000340 Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1);
341 return RValue::get(Builder.CreateCall2(F,
342 EmitScalarExpr(E->getArg(0)),
Eric Christopherfee667f2009-12-23 03:49:37 +0000343 CI));
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000344 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000345 case Builtin::BI__builtin_prefetch: {
346 Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
347 // FIXME: Technically these constants should of type 'int', yes?
Mike Stump1eb44332009-09-09 15:08:12 +0000348 RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
Chris Lattner77b89b82010-06-27 07:15:29 +0000349 llvm::ConstantInt::get(Int32Ty, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000350 Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
Chris Lattner77b89b82010-06-27 07:15:29 +0000351 llvm::ConstantInt::get(Int32Ty, 3);
Daniel Dunbar4493f792008-07-21 22:59:13 +0000352 Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0);
353 return RValue::get(Builder.CreateCall3(F, Address, RW, Locality));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000354 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000355 case Builtin::BI__builtin_trap: {
356 Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0);
357 return RValue::get(Builder.CreateCall(F));
358 }
Chris Lattner21190d52009-09-21 03:09:59 +0000359 case Builtin::BI__builtin_unreachable: {
Mike Stumpfba565d2009-12-16 03:07:12 +0000360 if (CatchUndefined && HaveInsertPoint())
361 EmitBranch(getTrapBB());
Chris Lattner21190d52009-09-21 03:09:59 +0000362 Value *V = Builder.CreateUnreachable();
363 Builder.ClearInsertionPoint();
364 return RValue::get(V);
365 }
366
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000367 case Builtin::BI__builtin_powi:
368 case Builtin::BI__builtin_powif:
369 case Builtin::BI__builtin_powil: {
370 Value *Base = EmitScalarExpr(E->getArg(0));
371 Value *Exponent = EmitScalarExpr(E->getArg(1));
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000372 const llvm::Type *ArgType = Base->getType();
373 Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1);
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000374 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
375 }
376
Chris Lattnerfe23e212007-12-20 00:44:32 +0000377 case Builtin::BI__builtin_isgreater:
378 case Builtin::BI__builtin_isgreaterequal:
379 case Builtin::BI__builtin_isless:
380 case Builtin::BI__builtin_islessequal:
381 case Builtin::BI__builtin_islessgreater:
382 case Builtin::BI__builtin_isunordered: {
383 // Ordered comparisons: we know the arguments to these are matching scalar
384 // floating point values.
Mike Stump1eb44332009-09-09 15:08:12 +0000385 Value *LHS = EmitScalarExpr(E->getArg(0));
Chris Lattnerfe23e212007-12-20 00:44:32 +0000386 Value *RHS = EmitScalarExpr(E->getArg(1));
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Chris Lattnerfe23e212007-12-20 00:44:32 +0000388 switch (BuiltinID) {
389 default: assert(0 && "Unknown ordered comparison");
390 case Builtin::BI__builtin_isgreater:
391 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
392 break;
393 case Builtin::BI__builtin_isgreaterequal:
394 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
395 break;
396 case Builtin::BI__builtin_isless:
397 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
398 break;
399 case Builtin::BI__builtin_islessequal:
400 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
401 break;
402 case Builtin::BI__builtin_islessgreater:
403 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
404 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000405 case Builtin::BI__builtin_isunordered:
Chris Lattnerfe23e212007-12-20 00:44:32 +0000406 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
407 break;
408 }
409 // ZExt bool to int type.
410 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()),
411 "tmp"));
412 }
Eli Friedmand6139892009-09-01 04:19:44 +0000413 case Builtin::BI__builtin_isnan: {
414 Value *V = EmitScalarExpr(E->getArg(0));
415 V = Builder.CreateFCmpUNO(V, V, "cmp");
416 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp"));
417 }
Chris Lattner420b1182010-05-06 05:35:16 +0000418
419 case Builtin::BI__builtin_isinf: {
420 // isinf(x) --> fabs(x) == infinity
421 Value *V = EmitScalarExpr(E->getArg(0));
422 V = EmitFAbs(*this, V, E->getArg(0)->getType());
423
424 V = Builder.CreateFCmpOEQ(V, ConstantFP::getInfinity(V->getType()),"isinf");
425 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp"));
426 }
Chris Lattner58ae5b42010-05-06 06:13:53 +0000427
428 // TODO: BI__builtin_isinf_sign
429 // isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0
Benjamin Kramer6349ce92010-05-19 11:24:26 +0000430
431 case Builtin::BI__builtin_isnormal: {
432 // isnormal(x) --> x == x && fabsf(x) < infinity && fabsf(x) >= float_min
433 Value *V = EmitScalarExpr(E->getArg(0));
434 Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
435
436 Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType());
437 Value *IsLessThanInf =
438 Builder.CreateFCmpULT(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
439 APFloat Smallest = APFloat::getSmallestNormalized(
440 getContext().getFloatTypeSemantics(E->getArg(0)->getType()));
441 Value *IsNormal =
442 Builder.CreateFCmpUGE(Abs, ConstantFP::get(V->getContext(), Smallest),
443 "isnormal");
444 V = Builder.CreateAnd(Eq, IsLessThanInf, "and");
445 V = Builder.CreateAnd(V, IsNormal, "and");
446 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
447 }
448
Chris Lattnered074152010-05-06 06:04:13 +0000449 case Builtin::BI__builtin_isfinite: {
450 // isfinite(x) --> x == x && fabs(x) != infinity; }
451 Value *V = EmitScalarExpr(E->getArg(0));
452 Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
453
454 Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType());
455 Value *IsNotInf =
456 Builder.CreateFCmpUNE(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
457
458 V = Builder.CreateAnd(Eq, IsNotInf, "and");
459 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
460 }
Benjamin Kramer7867f1a2010-06-14 10:30:41 +0000461
462 case Builtin::BI__builtin_fpclassify: {
463 Value *V = EmitScalarExpr(E->getArg(5));
464 const llvm::Type *Ty = ConvertType(E->getArg(5)->getType());
465
466 // Create Result
467 BasicBlock *Begin = Builder.GetInsertBlock();
468 BasicBlock *End = createBasicBlock("fpclassify_end", this->CurFn);
469 Builder.SetInsertPoint(End);
470 PHINode *Result =
471 Builder.CreatePHI(ConvertType(E->getArg(0)->getType()),
472 "fpclassify_result");
473
474 // if (V==0) return FP_ZERO
475 Builder.SetInsertPoint(Begin);
476 Value *IsZero = Builder.CreateFCmpOEQ(V, Constant::getNullValue(Ty),
477 "iszero");
478 Value *ZeroLiteral = EmitScalarExpr(E->getArg(4));
479 BasicBlock *NotZero = createBasicBlock("fpclassify_not_zero", this->CurFn);
480 Builder.CreateCondBr(IsZero, End, NotZero);
481 Result->addIncoming(ZeroLiteral, Begin);
482
483 // if (V != V) return FP_NAN
484 Builder.SetInsertPoint(NotZero);
485 Value *IsNan = Builder.CreateFCmpUNO(V, V, "cmp");
486 Value *NanLiteral = EmitScalarExpr(E->getArg(0));
487 BasicBlock *NotNan = createBasicBlock("fpclassify_not_nan", this->CurFn);
488 Builder.CreateCondBr(IsNan, End, NotNan);
489 Result->addIncoming(NanLiteral, NotZero);
490
491 // if (fabs(V) == infinity) return FP_INFINITY
492 Builder.SetInsertPoint(NotNan);
493 Value *VAbs = EmitFAbs(*this, V, E->getArg(5)->getType());
494 Value *IsInf =
495 Builder.CreateFCmpOEQ(VAbs, ConstantFP::getInfinity(V->getType()),
496 "isinf");
497 Value *InfLiteral = EmitScalarExpr(E->getArg(1));
498 BasicBlock *NotInf = createBasicBlock("fpclassify_not_inf", this->CurFn);
499 Builder.CreateCondBr(IsInf, End, NotInf);
500 Result->addIncoming(InfLiteral, NotNan);
501
502 // if (fabs(V) >= MIN_NORMAL) return FP_NORMAL else FP_SUBNORMAL
503 Builder.SetInsertPoint(NotInf);
504 APFloat Smallest = APFloat::getSmallestNormalized(
505 getContext().getFloatTypeSemantics(E->getArg(5)->getType()));
506 Value *IsNormal =
507 Builder.CreateFCmpUGE(VAbs, ConstantFP::get(V->getContext(), Smallest),
508 "isnormal");
509 Value *NormalResult =
510 Builder.CreateSelect(IsNormal, EmitScalarExpr(E->getArg(2)),
511 EmitScalarExpr(E->getArg(3)));
512 Builder.CreateBr(End);
513 Result->addIncoming(NormalResult, NotInf);
514
515 // return Result
516 Builder.SetInsertPoint(End);
517 return RValue::get(Result);
518 }
Chris Lattnered074152010-05-06 06:04:13 +0000519
Eli Friedmanb52fe9c2009-06-02 07:10:30 +0000520 case Builtin::BIalloca:
Chris Lattner9e800e32008-06-16 17:15:14 +0000521 case Builtin::BI__builtin_alloca: {
Chris Lattner9e800e32008-06-16 17:15:14 +0000522 Value *Size = EmitScalarExpr(E->getArg(0));
Owen Anderson0032b272009-08-13 21:57:51 +0000523 return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp"));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000524 }
Eli Friedmane6dddfd2010-01-23 19:00:10 +0000525 case Builtin::BIbzero:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000526 case Builtin::BI__builtin_bzero: {
527 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000528 Value *SizeVal = EmitScalarExpr(E->getArg(1));
529 Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()),
530 Address,
531 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0),
532 SizeVal,
Chris Lattner77b89b82010-06-27 07:15:29 +0000533 llvm::ConstantInt::get(Int32Ty, 1),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000534 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000535 return RValue::get(Address);
Chris Lattner9e800e32008-06-16 17:15:14 +0000536 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000537 case Builtin::BImemcpy:
Eli Friedmand4b32e42008-05-19 23:27:48 +0000538 case Builtin::BI__builtin_memcpy: {
Daniel Dunbar1caae952008-07-22 00:26:45 +0000539 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000540 Value *SrcAddr = EmitScalarExpr(E->getArg(1));
541 Value *SizeVal = EmitScalarExpr(E->getArg(2));
542 Builder.CreateCall5(CGM.getMemCpyFn(Address->getType(), SrcAddr->getType(),
543 SizeVal->getType()),
544 Address, SrcAddr, SizeVal,
Chris Lattner77b89b82010-06-27 07:15:29 +0000545 llvm::ConstantInt::get(Int32Ty, 1),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000546 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000547 return RValue::get(Address);
548 }
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000549
Fariborz Jahanian8e2eab22010-06-16 16:22:04 +0000550 case Builtin::BI__builtin_objc_memmove_collectable: {
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000551 Value *Address = EmitScalarExpr(E->getArg(0));
552 Value *SrcAddr = EmitScalarExpr(E->getArg(1));
553 Value *SizeVal = EmitScalarExpr(E->getArg(2));
554 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this,
555 Address, SrcAddr, SizeVal);
556 return RValue::get(Address);
557 }
558
Eli Friedmane6ec2052009-12-17 00:14:28 +0000559 case Builtin::BImemmove:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000560 case Builtin::BI__builtin_memmove: {
561 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000562 Value *SrcAddr = EmitScalarExpr(E->getArg(1));
563 Value *SizeVal = EmitScalarExpr(E->getArg(2));
564 Builder.CreateCall5(CGM.getMemMoveFn(Address->getType(), SrcAddr->getType(),
565 SizeVal->getType()),
566 Address, SrcAddr, SizeVal,
Chris Lattner77b89b82010-06-27 07:15:29 +0000567 llvm::ConstantInt::get(Int32Ty, 1),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000568 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000569 return RValue::get(Address);
570 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000571 case Builtin::BImemset:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000572 case Builtin::BI__builtin_memset: {
573 Value *Address = EmitScalarExpr(E->getArg(0));
Mon P Wang3ecd7852010-04-04 03:10:52 +0000574 Value *SizeVal = EmitScalarExpr(E->getArg(2));
575 Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()),
576 Address,
577 Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
578 llvm::Type::getInt8Ty(VMContext)),
579 SizeVal,
Chris Lattner77b89b82010-06-27 07:15:29 +0000580 llvm::ConstantInt::get(Int32Ty, 1),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000581 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000582 return RValue::get(Address);
Eli Friedmand4b32e42008-05-19 23:27:48 +0000583 }
John McCallfb17a562010-03-03 10:30:05 +0000584 case Builtin::BI__builtin_dwarf_cfa: {
585 // The offset in bytes from the first argument to the CFA.
586 //
587 // Why on earth is this in the frontend? Is there any reason at
588 // all that the backend can't reasonably determine this while
589 // lowering llvm.eh.dwarf.cfa()?
590 //
591 // TODO: If there's a satisfactory reason, add a target hook for
592 // this instead of hard-coding 0, which is correct for most targets.
593 int32_t Offset = 0;
594
595 Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa, 0, 0);
Chris Lattner77b89b82010-06-27 07:15:29 +0000596 return RValue::get(Builder.CreateCall(F,
597 llvm::ConstantInt::get(Int32Ty, Offset)));
John McCallfb17a562010-03-03 10:30:05 +0000598 }
Eli Friedman256f77e2008-05-20 08:59:34 +0000599 case Builtin::BI__builtin_return_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000600 Value *Depth = EmitScalarExpr(E->getArg(0));
Chris Lattner77b89b82010-06-27 07:15:29 +0000601 Depth = Builder.CreateIntCast(Depth, Int32Ty, false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000602 Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000603 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000604 }
605 case Builtin::BI__builtin_frame_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000606 Value *Depth = EmitScalarExpr(E->getArg(0));
Chris Lattner77b89b82010-06-27 07:15:29 +0000607 Depth = Builder.CreateIntCast(Depth, Int32Ty, false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000608 Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000609 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000610 }
Eli Friedman3b660ef2009-05-03 19:23:23 +0000611 case Builtin::BI__builtin_extract_return_addr: {
John McCall492c4f92010-03-03 04:15:11 +0000612 Value *Address = EmitScalarExpr(E->getArg(0));
613 Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);
614 return RValue::get(Result);
615 }
616 case Builtin::BI__builtin_frob_return_addr: {
617 Value *Address = EmitScalarExpr(E->getArg(0));
618 Value *Result = getTargetHooks().encodeReturnAddress(*this, Address);
619 return RValue::get(Result);
Eli Friedman3b660ef2009-05-03 19:23:23 +0000620 }
John McCall6374c332010-03-06 00:35:14 +0000621 case Builtin::BI__builtin_dwarf_sp_column: {
622 const llvm::IntegerType *Ty
623 = cast<llvm::IntegerType>(ConvertType(E->getType()));
624 int Column = getTargetHooks().getDwarfEHStackPointer(CGM);
625 if (Column == -1) {
626 CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column");
627 return RValue::get(llvm::UndefValue::get(Ty));
628 }
629 return RValue::get(llvm::ConstantInt::get(Ty, Column, true));
630 }
631 case Builtin::BI__builtin_init_dwarf_reg_size_table: {
632 Value *Address = EmitScalarExpr(E->getArg(0));
633 if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address))
634 CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table");
635 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
636 }
John McCall7ada1112010-03-03 05:38:58 +0000637 case Builtin::BI__builtin_eh_return: {
638 Value *Int = EmitScalarExpr(E->getArg(0));
639 Value *Ptr = EmitScalarExpr(E->getArg(1));
640
641 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType());
642 assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) &&
643 "LLVM's __builtin_eh_return only supports 32- and 64-bit variants");
644 Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32
645 ? Intrinsic::eh_return_i32
646 : Intrinsic::eh_return_i64,
647 0, 0);
648 Builder.CreateCall2(F, Int, Ptr);
649 Value *V = Builder.CreateUnreachable();
650 Builder.ClearInsertionPoint();
651 return RValue::get(V);
652 }
Eli Friedmana6d75c02009-06-02 09:37:50 +0000653 case Builtin::BI__builtin_unwind_init: {
654 Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0);
655 return RValue::get(Builder.CreateCall(F));
656 }
John McCall5e110852010-03-02 02:31:24 +0000657 case Builtin::BI__builtin_extend_pointer: {
658 // Extends a pointer to the size of an _Unwind_Word, which is
John McCalld0b76ca2010-03-02 03:50:12 +0000659 // uint64_t on all platforms. Generally this gets poked into a
660 // register and eventually used as an address, so if the
661 // addressing registers are wider than pointers and the platform
662 // doesn't implicitly ignore high-order bits when doing
663 // addressing, we need to make sure we zext / sext based on
664 // the platform's expectations.
John McCall5e110852010-03-02 02:31:24 +0000665 //
666 // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html
John McCalld0b76ca2010-03-02 03:50:12 +0000667
668 LLVMContext &C = CGM.getLLVMContext();
669
670 // Cast the pointer to intptr_t.
John McCall5e110852010-03-02 02:31:24 +0000671 Value *Ptr = EmitScalarExpr(E->getArg(0));
John McCalld0b76ca2010-03-02 03:50:12 +0000672 const llvm::IntegerType *IntPtrTy = CGM.getTargetData().getIntPtrType(C);
673 Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast");
674
675 // If that's 64 bits, we're done.
676 if (IntPtrTy->getBitWidth() == 64)
677 return RValue::get(Result);
678
679 // Otherwise, ask the codegen data what to do.
John McCall492c4f92010-03-03 04:15:11 +0000680 if (getTargetHooks().extendPointerWithSExt())
John McCalld0b76ca2010-03-02 03:50:12 +0000681 return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext"));
682 else
683 return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext"));
John McCall5e110852010-03-02 02:31:24 +0000684 }
Eli Friedmana6d75c02009-06-02 09:37:50 +0000685 case Builtin::BI__builtin_setjmp: {
John McCall78673d92010-05-27 18:47:06 +0000686 // Buffer is a void**.
Eli Friedmana6d75c02009-06-02 09:37:50 +0000687 Value *Buf = EmitScalarExpr(E->getArg(0));
John McCall78673d92010-05-27 18:47:06 +0000688
689 // Store the frame pointer to the setjmp buffer.
Eli Friedmana6d75c02009-06-02 09:37:50 +0000690 Value *FrameAddr =
John McCall78673d92010-05-27 18:47:06 +0000691 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::frameaddress),
Chris Lattner77b89b82010-06-27 07:15:29 +0000692 ConstantInt::get(Int32Ty, 0));
Eli Friedmana6d75c02009-06-02 09:37:50 +0000693 Builder.CreateStore(FrameAddr, Buf);
John McCall78673d92010-05-27 18:47:06 +0000694
Jim Grosbach6d172e22010-05-27 23:54:20 +0000695 // Store the stack pointer to the setjmp buffer.
696 Value *StackAddr =
697 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));
698 Value *StackSaveSlot =
Chris Lattner77b89b82010-06-27 07:15:29 +0000699 Builder.CreateGEP(Buf, ConstantInt::get(Int32Ty, 2));
Jim Grosbach6d172e22010-05-27 23:54:20 +0000700 Builder.CreateStore(StackAddr, StackSaveSlot);
701
John McCall78673d92010-05-27 18:47:06 +0000702 // Call LLVM's EH setjmp, which is lightweight.
703 Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp);
704 Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext));
Eli Friedmana6d75c02009-06-02 09:37:50 +0000705 return RValue::get(Builder.CreateCall(F, Buf));
706 }
707 case Builtin::BI__builtin_longjmp: {
Eli Friedmana6d75c02009-06-02 09:37:50 +0000708 Value *Buf = EmitScalarExpr(E->getArg(0));
John McCall78673d92010-05-27 18:47:06 +0000709 Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext));
710
711 // Call LLVM's EH longjmp, which is lightweight.
712 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf);
713
714 // longjmp doesn't return; mark this as unreachable
715 Value *V = Builder.CreateUnreachable();
716 Builder.ClearInsertionPoint();
717 return RValue::get(V);
Eli Friedmana6d75c02009-06-02 09:37:50 +0000718 }
Mon P Wang1ffe2812008-05-09 22:40:52 +0000719 case Builtin::BI__sync_fetch_and_add:
Mon P Wang1ffe2812008-05-09 22:40:52 +0000720 case Builtin::BI__sync_fetch_and_sub:
Chris Lattner5caa3702009-05-08 06:58:22 +0000721 case Builtin::BI__sync_fetch_and_or:
722 case Builtin::BI__sync_fetch_and_and:
723 case Builtin::BI__sync_fetch_and_xor:
724 case Builtin::BI__sync_add_and_fetch:
725 case Builtin::BI__sync_sub_and_fetch:
726 case Builtin::BI__sync_and_and_fetch:
727 case Builtin::BI__sync_or_and_fetch:
728 case Builtin::BI__sync_xor_and_fetch:
729 case Builtin::BI__sync_val_compare_and_swap:
730 case Builtin::BI__sync_bool_compare_and_swap:
731 case Builtin::BI__sync_lock_test_and_set:
732 case Builtin::BI__sync_lock_release:
733 assert(0 && "Shouldn't make it through sema");
734 case Builtin::BI__sync_fetch_and_add_1:
735 case Builtin::BI__sync_fetch_and_add_2:
736 case Builtin::BI__sync_fetch_and_add_4:
737 case Builtin::BI__sync_fetch_and_add_8:
738 case Builtin::BI__sync_fetch_and_add_16:
739 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E);
740 case Builtin::BI__sync_fetch_and_sub_1:
741 case Builtin::BI__sync_fetch_and_sub_2:
742 case Builtin::BI__sync_fetch_and_sub_4:
743 case Builtin::BI__sync_fetch_and_sub_8:
744 case Builtin::BI__sync_fetch_and_sub_16:
Mon P Wang09b6bf52008-06-25 08:21:36 +0000745 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E);
Chris Lattner5caa3702009-05-08 06:58:22 +0000746 case Builtin::BI__sync_fetch_and_or_1:
747 case Builtin::BI__sync_fetch_and_or_2:
748 case Builtin::BI__sync_fetch_and_or_4:
749 case Builtin::BI__sync_fetch_and_or_8:
750 case Builtin::BI__sync_fetch_and_or_16:
751 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E);
752 case Builtin::BI__sync_fetch_and_and_1:
753 case Builtin::BI__sync_fetch_and_and_2:
754 case Builtin::BI__sync_fetch_and_and_4:
755 case Builtin::BI__sync_fetch_and_and_8:
756 case Builtin::BI__sync_fetch_and_and_16:
757 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E);
758 case Builtin::BI__sync_fetch_and_xor_1:
759 case Builtin::BI__sync_fetch_and_xor_2:
760 case Builtin::BI__sync_fetch_and_xor_4:
761 case Builtin::BI__sync_fetch_and_xor_8:
762 case Builtin::BI__sync_fetch_and_xor_16:
763 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattner5caa3702009-05-08 06:58:22 +0000765 // Clang extensions: not overloaded yet.
Mon P Wang1ffe2812008-05-09 22:40:52 +0000766 case Builtin::BI__sync_fetch_and_min:
767 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E);
768 case Builtin::BI__sync_fetch_and_max:
769 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E);
770 case Builtin::BI__sync_fetch_and_umin:
771 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E);
772 case Builtin::BI__sync_fetch_and_umax:
773 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000774
Chris Lattner5caa3702009-05-08 06:58:22 +0000775 case Builtin::BI__sync_add_and_fetch_1:
776 case Builtin::BI__sync_add_and_fetch_2:
777 case Builtin::BI__sync_add_and_fetch_4:
778 case Builtin::BI__sync_add_and_fetch_8:
779 case Builtin::BI__sync_add_and_fetch_16:
Mike Stump1eb44332009-09-09 15:08:12 +0000780 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E,
Daniel Dunbar0002d232009-04-07 00:55:51 +0000781 llvm::Instruction::Add);
Chris Lattner5caa3702009-05-08 06:58:22 +0000782 case Builtin::BI__sync_sub_and_fetch_1:
783 case Builtin::BI__sync_sub_and_fetch_2:
784 case Builtin::BI__sync_sub_and_fetch_4:
785 case Builtin::BI__sync_sub_and_fetch_8:
786 case Builtin::BI__sync_sub_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000787 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E,
788 llvm::Instruction::Sub);
Chris Lattner5caa3702009-05-08 06:58:22 +0000789 case Builtin::BI__sync_and_and_fetch_1:
790 case Builtin::BI__sync_and_and_fetch_2:
791 case Builtin::BI__sync_and_and_fetch_4:
792 case Builtin::BI__sync_and_and_fetch_8:
793 case Builtin::BI__sync_and_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000794 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E,
795 llvm::Instruction::And);
Chris Lattner5caa3702009-05-08 06:58:22 +0000796 case Builtin::BI__sync_or_and_fetch_1:
797 case Builtin::BI__sync_or_and_fetch_2:
798 case Builtin::BI__sync_or_and_fetch_4:
799 case Builtin::BI__sync_or_and_fetch_8:
800 case Builtin::BI__sync_or_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000801 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E,
802 llvm::Instruction::Or);
Chris Lattner5caa3702009-05-08 06:58:22 +0000803 case Builtin::BI__sync_xor_and_fetch_1:
804 case Builtin::BI__sync_xor_and_fetch_2:
805 case Builtin::BI__sync_xor_and_fetch_4:
806 case Builtin::BI__sync_xor_and_fetch_8:
807 case Builtin::BI__sync_xor_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000808 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E,
809 llvm::Instruction::Xor);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Chris Lattner5caa3702009-05-08 06:58:22 +0000811 case Builtin::BI__sync_val_compare_and_swap_1:
812 case Builtin::BI__sync_val_compare_and_swap_2:
813 case Builtin::BI__sync_val_compare_and_swap_4:
814 case Builtin::BI__sync_val_compare_and_swap_8:
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000815 case Builtin::BI__sync_val_compare_and_swap_16: {
John McCall26815d92010-10-27 20:58:56 +0000816 QualType T = E->getType();
Chris Lattner780a2eb2010-09-21 23:35:30 +0000817 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
818 unsigned AddrSpace =
Chris Lattner4f209442010-09-21 23:40:48 +0000819 cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
John McCall26815d92010-10-27 20:58:56 +0000820
821 const llvm::IntegerType *IntType =
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000822 llvm::IntegerType::get(CGF.getLLVMContext(),
John McCall26815d92010-10-27 20:58:56 +0000823 CGF.getContext().getTypeSize(T));
824 const llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
825 const llvm::Type *IntrinsicTypes[2] = { IntType, IntPtrType };
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000826 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap,
827 IntrinsicTypes, 2);
828
John McCall26815d92010-10-27 20:58:56 +0000829 Value *Args[3];
830 Args[0] = Builder.CreateBitCast(DestPtr, IntPtrType);
831 Args[1] = CGF.EmitScalarExpr(E->getArg(1));
832 const llvm::Type *ValueType = Args[1]->getType();
833 Args[1] = EmitToInt(CGF, Args[1], T, IntType);
834 Args[2] = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(2)), T, IntType);
835
836 Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 3);
837 Result = EmitFromInt(CGF, Result, T, ValueType);
838 return RValue::get(Result);
Anders Carlsson89799cf2007-10-29 02:59:40 +0000839 }
Daniel Dunbar0002d232009-04-07 00:55:51 +0000840
Chris Lattner5caa3702009-05-08 06:58:22 +0000841 case Builtin::BI__sync_bool_compare_and_swap_1:
842 case Builtin::BI__sync_bool_compare_and_swap_2:
843 case Builtin::BI__sync_bool_compare_and_swap_4:
844 case Builtin::BI__sync_bool_compare_and_swap_8:
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000845 case Builtin::BI__sync_bool_compare_and_swap_16: {
John McCall26815d92010-10-27 20:58:56 +0000846 QualType T = E->getArg(1)->getType();
Chris Lattnerf2b95272010-09-21 23:24:52 +0000847 llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
848 unsigned AddrSpace =
Chris Lattner4f209442010-09-21 23:40:48 +0000849 cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
John McCall26815d92010-10-27 20:58:56 +0000850
851 const llvm::IntegerType *IntType =
Chris Lattnerf2b95272010-09-21 23:24:52 +0000852 llvm::IntegerType::get(CGF.getLLVMContext(),
John McCall26815d92010-10-27 20:58:56 +0000853 CGF.getContext().getTypeSize(T));
854 const llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
855 const llvm::Type *IntrinsicTypes[2] = { IntType, IntPtrType };
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000856 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap,
857 IntrinsicTypes, 2);
858
John McCall26815d92010-10-27 20:58:56 +0000859 Value *Args[3];
860 Args[0] = Builder.CreateBitCast(DestPtr, IntPtrType);
861 Args[1] = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(1)), T, IntType);
862 Args[2] = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(2)), T, IntType);
863
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000864 Value *OldVal = Args[1];
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000865 Value *PrevVal = EmitCallWithBarrier(*this, AtomF, Args, Args + 3);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000866 Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal);
867 // zext bool to int.
John McCall26815d92010-10-27 20:58:56 +0000868 Result = Builder.CreateZExt(Result, ConvertType(E->getType()));
869 return RValue::get(Result);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000870 }
871
Chris Lattner5caa3702009-05-08 06:58:22 +0000872 case Builtin::BI__sync_lock_test_and_set_1:
873 case Builtin::BI__sync_lock_test_and_set_2:
874 case Builtin::BI__sync_lock_test_and_set_4:
875 case Builtin::BI__sync_lock_test_and_set_8:
876 case Builtin::BI__sync_lock_test_and_set_16:
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000877 return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E);
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000878
Chris Lattner5caa3702009-05-08 06:58:22 +0000879 case Builtin::BI__sync_lock_release_1:
880 case Builtin::BI__sync_lock_release_2:
881 case Builtin::BI__sync_lock_release_4:
882 case Builtin::BI__sync_lock_release_8:
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000883 case Builtin::BI__sync_lock_release_16: {
884 Value *Ptr = EmitScalarExpr(E->getArg(0));
885 const llvm::Type *ElTy =
886 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar007b5672009-11-29 21:11:47 +0000887 llvm::StoreInst *Store =
888 Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr);
889 Store->setVolatile(true);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000890 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000891 }
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000892
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000893 case Builtin::BI__sync_synchronize: {
Daniel Dunbarcb61a7b2010-03-20 07:04:11 +0000894 // We assume like gcc appears to, that this only applies to cached memory.
895 EmitMemoryBarrier(*this, true, true, true, true, false);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000896 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000897 }
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Tanya Lattner0b571642010-01-16 01:21:14 +0000899 case Builtin::BI__builtin_llvm_memory_barrier: {
900 Value *C[5] = {
901 EmitScalarExpr(E->getArg(0)),
902 EmitScalarExpr(E->getArg(1)),
903 EmitScalarExpr(E->getArg(2)),
904 EmitScalarExpr(E->getArg(3)),
905 EmitScalarExpr(E->getArg(4))
906 };
907 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5);
908 return RValue::get(0);
909 }
910
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000911 // Library functions with special handling.
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000912 case Builtin::BIsqrt:
913 case Builtin::BIsqrtf:
914 case Builtin::BIsqrtl: {
John McCallbeb41282010-04-07 08:20:20 +0000915 // TODO: there is currently no set of optimizer flags
916 // sufficient for us to rewrite sqrt to @llvm.sqrt.
917 // -fmath-errno=0 is not good enough; we need finiteness.
918 // We could probably precondition the call with an ult
919 // against 0, but is that worth the complexity?
920 break;
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000921 }
922
923 case Builtin::BIpow:
924 case Builtin::BIpowf:
925 case Builtin::BIpowl: {
926 // Rewrite sqrt to intrinsic if allowed.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000927 if (!FD->hasAttr<ConstAttr>())
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000928 break;
929 Value *Base = EmitScalarExpr(E->getArg(0));
930 Value *Exponent = EmitScalarExpr(E->getArg(1));
931 const llvm::Type *ArgType = Base->getType();
932 Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1);
933 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
934 }
Eli Friedmanba68b082010-03-06 02:17:52 +0000935
936 case Builtin::BI__builtin_signbit:
937 case Builtin::BI__builtin_signbitf:
938 case Builtin::BI__builtin_signbitl: {
939 LLVMContext &C = CGM.getLLVMContext();
940
941 Value *Arg = EmitScalarExpr(E->getArg(0));
942 const llvm::Type *ArgTy = Arg->getType();
943 if (ArgTy->isPPC_FP128Ty())
944 break; // FIXME: I'm not sure what the right implementation is here.
945 int ArgWidth = ArgTy->getPrimitiveSizeInBits();
946 const llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth);
947 Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy);
948 Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy);
949 Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp);
950 return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
951 }
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000952 }
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000954 // If this is an alias for a libm function (e.g. __builtin_sin) turn it into
955 // that function.
Douglas Gregor3e41d602009-02-13 23:20:09 +0000956 if (getContext().BuiltinInfo.isLibFunction(BuiltinID) ||
957 getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
Anders Carlsson31777a22009-12-24 19:08:58 +0000958 return EmitCall(E->getCallee()->getType(),
959 CGM.getBuiltinLibFunction(FD, BuiltinID),
Anders Carlssond2490a92009-12-24 20:40:36 +0000960 ReturnValueSlot(),
Anders Carlsson31777a22009-12-24 19:08:58 +0000961 E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000963 // See if we have a target specific intrinsic.
Dale Johannesena6f80ef2009-02-05 01:50:47 +0000964 const char *Name = getContext().BuiltinInfo.GetName(BuiltinID);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000965 Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
966 if (const char *Prefix =
Mike Stump1eb44332009-09-09 15:08:12 +0000967 llvm::Triple::getArchTypePrefix(Target.getTriple().getArch()))
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000968 IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000970 if (IntrinsicID != Intrinsic::not_intrinsic) {
971 SmallVector<Value*, 16> Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chris Lattner46c55912010-10-02 00:09:12 +0000973 // Find out if any arguments are required to be integer constant
974 // expressions.
975 unsigned ICEArguments = 0;
976 ASTContext::GetBuiltinTypeError Error;
977 getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
978 assert(Error == ASTContext::GE_None && "Should not codegen an error");
979
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000980 Function *F = CGM.getIntrinsic(IntrinsicID);
981 const llvm::FunctionType *FTy = F->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000983 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
Chris Lattner46c55912010-10-02 00:09:12 +0000984 Value *ArgValue;
985 // If this is a normal argument, just emit it as a scalar.
986 if ((ICEArguments & (1 << i)) == 0) {
987 ArgValue = EmitScalarExpr(E->getArg(i));
988 } else {
989 // If this is required to be a constant, constant fold it so that we
990 // know that the generated intrinsic gets a ConstantInt.
991 llvm::APSInt Result;
992 bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result,getContext());
993 assert(IsConst && "Constant arg isn't actually constant?");
994 (void)IsConst;
995 ArgValue = llvm::ConstantInt::get(VMContext, Result);
996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000998 // If the intrinsic arg type is different from the builtin arg type
999 // we need to do a bit cast.
1000 const llvm::Type *PTy = FTy->getParamType(i);
1001 if (PTy != ArgValue->getType()) {
1002 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
1003 "Must be able to losslessly bit cast to param");
1004 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
1005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001007 Args.push_back(ArgValue);
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Jay Foadbeaaccd2009-05-21 09:52:38 +00001010 Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size());
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001011 QualType BuiltinRetType = E->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Owen Anderson0032b272009-08-13 21:57:51 +00001013 const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001014 if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001016 if (RetTy != V->getType()) {
1017 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
1018 "Must be able to losslessly bit cast result type");
1019 V = Builder.CreateBitCast(V, RetTy);
1020 }
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001022 return RValue::get(V);
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001025 // See if we have a target specific builtin that needs to be lowered.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001026 if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E))
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001027 return RValue::get(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Daniel Dunbar488e9932008-08-16 00:56:44 +00001029 ErrorUnsupported(E, "builtin function");
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Chris Lattnerb7cfe882008-06-30 18:32:54 +00001031 // Unknown builtin, for now just dump it out and return undef.
1032 if (hasAggregateLLVMType(E->getType()))
Daniel Dunbar195337d2010-02-09 02:48:28 +00001033 return RValue::getAggregate(CreateMemTemp(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +00001034 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
Mike Stump1eb44332009-09-09 15:08:12 +00001035}
Anders Carlsson564f1de2007-12-09 23:17:02 +00001036
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001037Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID,
1038 const CallExpr *E) {
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001039 switch (Target.getTriple().getArch()) {
Chris Lattner2752c012010-03-03 19:03:45 +00001040 case llvm::Triple::arm:
1041 case llvm::Triple::thumb:
1042 return EmitARMBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001043 case llvm::Triple::x86:
1044 case llvm::Triple::x86_64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001045 return EmitX86BuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001046 case llvm::Triple::ppc:
1047 case llvm::Triple::ppc64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001048 return EmitPPCBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +00001049 default:
1050 return 0;
1051 }
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00001052}
1053
Nate Begeman4be54302010-06-20 23:05:28 +00001054const llvm::VectorType *GetNeonType(LLVMContext &C, unsigned type, bool q) {
Nate Begeman998622c2010-06-07 16:01:56 +00001055 switch (type) {
1056 default: break;
1057 case 0:
Nate Begeman4be54302010-06-20 23:05:28 +00001058 case 5: return llvm::VectorType::get(llvm::Type::getInt8Ty(C), 8 << (int)q);
Nate Begeman998622c2010-06-07 16:01:56 +00001059 case 6:
1060 case 7:
Nate Begeman4be54302010-06-20 23:05:28 +00001061 case 1: return llvm::VectorType::get(llvm::Type::getInt16Ty(C),4 << (int)q);
1062 case 2: return llvm::VectorType::get(llvm::Type::getInt32Ty(C),2 << (int)q);
1063 case 3: return llvm::VectorType::get(llvm::Type::getInt64Ty(C),1 << (int)q);
1064 case 4: return llvm::VectorType::get(llvm::Type::getFloatTy(C),2 << (int)q);
Nate Begeman998622c2010-06-07 16:01:56 +00001065 };
1066 return 0;
1067}
1068
Bob Wilsoncf556522010-12-07 22:40:02 +00001069Value *CodeGenFunction::EmitNeonSplat(Value *V, Constant *C) {
Nate Begemand075c012010-06-10 00:17:56 +00001070 unsigned nElts = cast<llvm::VectorType>(V->getType())->getNumElements();
1071 SmallVector<Constant*, 16> Indices(nElts, C);
1072 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1073 return Builder.CreateShuffleVector(V, V, SV, "lane");
1074}
1075
Nate Begeman30d91712010-06-08 06:03:01 +00001076Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl<Value*> &Ops,
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001077 const char *name,
Nate Begeman61eecf52010-06-14 05:21:25 +00001078 unsigned shift, bool rightshift) {
Nate Begeman30d91712010-06-08 06:03:01 +00001079 unsigned j = 0;
1080 for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end();
1081 ai != ae; ++ai, ++j)
Nate Begeman61eecf52010-06-14 05:21:25 +00001082 if (shift > 0 && shift == j)
1083 Ops[j] = EmitNeonShiftVector(Ops[j], ai->getType(), rightshift);
1084 else
1085 Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name);
Nate Begeman30d91712010-06-08 06:03:01 +00001086
1087 return Builder.CreateCall(F, Ops.begin(), Ops.end(), name);
1088}
1089
Nate Begeman464ccb62010-06-11 22:57:12 +00001090Value *CodeGenFunction::EmitNeonShiftVector(Value *V, const llvm::Type *Ty,
1091 bool neg) {
1092 ConstantInt *CI = cast<ConstantInt>(V);
1093 int SV = CI->getSExtValue();
1094
1095 const llvm::VectorType *VTy = cast<llvm::VectorType>(Ty);
1096 llvm::Constant *C = ConstantInt::get(VTy->getElementType(), neg ? -SV : SV);
1097 SmallVector<llvm::Constant*, 16> CV(VTy->getNumElements(), C);
1098 return llvm::ConstantVector::get(CV.begin(), CV.size());
1099}
1100
Bob Wilson06b6c582010-08-27 17:14:29 +00001101/// GetPointeeAlignment - Given an expression with a pointer type, find the
1102/// alignment of the type referenced by the pointer. Skip over implicit
1103/// casts.
1104static Value *GetPointeeAlignment(CodeGenFunction &CGF, const Expr *Addr) {
1105 unsigned Align = 1;
1106 // Check if the type is a pointer. The implicit cast operand might not be.
1107 while (Addr->getType()->isPointerType()) {
1108 QualType PtTy = Addr->getType()->getPointeeType();
1109 unsigned NewA = CGF.getContext().getTypeAlignInChars(PtTy).getQuantity();
1110 if (NewA > Align)
1111 Align = NewA;
1112
1113 // If the address is an implicit cast, repeat with the cast operand.
1114 if (const ImplicitCastExpr *CastAddr = dyn_cast<ImplicitCastExpr>(Addr)) {
1115 Addr = CastAddr->getSubExpr();
1116 continue;
1117 }
1118 break;
1119 }
1120 return llvm::ConstantInt::get(CGF.Int32Ty, Align);
1121}
1122
Chris Lattner2752c012010-03-03 19:03:45 +00001123Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
1124 const CallExpr *E) {
Rafael Espindolae140af32010-06-09 03:48:40 +00001125 if (BuiltinID == ARM::BI__clear_cache) {
Rafael Espindola79ba5092010-06-07 17:26:50 +00001126 const FunctionDecl *FD = E->getDirectCallee();
1127 Value *a = EmitScalarExpr(E->getArg(0));
1128 Value *b = EmitScalarExpr(E->getArg(1));
1129 const llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType());
1130 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
1131 llvm::StringRef Name = FD->getName();
1132 return Builder.CreateCall2(CGM.CreateRuntimeFunction(FTy, Name),
1133 a, b);
Chris Lattner2752c012010-03-03 19:03:45 +00001134 }
Rafael Espindolae140af32010-06-09 03:48:40 +00001135
Nate Begemand075c012010-06-10 00:17:56 +00001136 llvm::SmallVector<Value*, 4> Ops;
Rafael Espindolae140af32010-06-09 03:48:40 +00001137 for (unsigned i = 0, e = E->getNumArgs() - 1; i != e; i++)
1138 Ops.push_back(EmitScalarExpr(E->getArg(i)));
1139
1140 llvm::APSInt Result;
1141 const Expr *Arg = E->getArg(E->getNumArgs()-1);
1142 if (!Arg->isIntegerConstantExpr(Result, getContext()))
1143 return 0;
1144
Nate Begeman99c40bb2010-08-03 21:32:34 +00001145 if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f ||
1146 BuiltinID == ARM::BI__builtin_arm_vcvtr_d) {
1147 // Determine the overloaded type of this builtin.
1148 const llvm::Type *Ty;
1149 if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f)
1150 Ty = llvm::Type::getFloatTy(VMContext);
1151 else
1152 Ty = llvm::Type::getDoubleTy(VMContext);
1153
1154 // Determine whether this is an unsigned conversion or not.
1155 bool usgn = Result.getZExtValue() == 1;
1156 unsigned Int = usgn ? Intrinsic::arm_vcvtru : Intrinsic::arm_vcvtr;
1157
1158 // Call the appropriate intrinsic.
1159 Function *F = CGM.getIntrinsic(Int, &Ty, 1);
1160 return Builder.CreateCall(F, Ops.begin(), Ops.end(), "vcvtr");
1161 }
1162
1163 // Determine the type of this overloaded NEON intrinsic.
Rafael Espindolae140af32010-06-09 03:48:40 +00001164 unsigned type = Result.getZExtValue();
1165 bool usgn = type & 0x08;
1166 bool quad = type & 0x10;
Nate Begeman0d15c532010-06-13 04:47:52 +00001167 bool poly = (type & 0x7) == 5 || (type & 0x7) == 6;
Chandler Carruth9d8231a2010-12-08 01:29:17 +00001168 (void)poly; // Only used in assert()s.
Bob Wilson79653962010-12-03 17:10:22 +00001169 bool rightShift = false;
Rafael Espindolae140af32010-06-09 03:48:40 +00001170
Nate Begeman4be54302010-06-20 23:05:28 +00001171 const llvm::VectorType *VTy = GetNeonType(VMContext, type & 0x7, quad);
1172 const llvm::Type *Ty = VTy;
Rafael Espindolae140af32010-06-09 03:48:40 +00001173 if (!Ty)
1174 return 0;
1175
1176 unsigned Int;
1177 switch (BuiltinID) {
1178 default: return 0;
Nate Begeman998622c2010-06-07 16:01:56 +00001179 case ARM::BI__builtin_neon_vabd_v:
Nate Begeman30d91712010-06-08 06:03:01 +00001180 case ARM::BI__builtin_neon_vabdq_v:
Nate Begeman998622c2010-06-07 16:01:56 +00001181 Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds;
Nate Begeman30d91712010-06-08 06:03:01 +00001182 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabd");
Nate Begeman998622c2010-06-07 16:01:56 +00001183 case ARM::BI__builtin_neon_vabs_v:
Nate Begeman548f7da2010-06-10 18:11:55 +00001184 case ARM::BI__builtin_neon_vabsq_v:
1185 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vabs, &Ty, 1),
1186 Ops, "vabs");
1187 case ARM::BI__builtin_neon_vaddhn_v:
1188 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vaddhn, &Ty, 1),
1189 Ops, "vaddhn");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001190 case ARM::BI__builtin_neon_vcale_v:
1191 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001192 case ARM::BI__builtin_neon_vcage_v: {
Bob Wilsond1850352010-12-10 01:11:38 +00001193 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacged);
Nate Begeman30d91712010-06-08 06:03:01 +00001194 return EmitNeonCall(F, Ops, "vcage");
1195 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001196 case ARM::BI__builtin_neon_vcaleq_v:
1197 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001198 case ARM::BI__builtin_neon_vcageq_v: {
Bob Wilsond1850352010-12-10 01:11:38 +00001199 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgeq);
Nate Begeman30d91712010-06-08 06:03:01 +00001200 return EmitNeonCall(F, Ops, "vcage");
1201 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001202 case ARM::BI__builtin_neon_vcalt_v:
1203 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001204 case ARM::BI__builtin_neon_vcagt_v: {
Bob Wilsond1850352010-12-10 01:11:38 +00001205 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtd);
Nate Begeman30d91712010-06-08 06:03:01 +00001206 return EmitNeonCall(F, Ops, "vcagt");
1207 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001208 case ARM::BI__builtin_neon_vcaltq_v:
1209 std::swap(Ops[0], Ops[1]);
Nate Begeman30d91712010-06-08 06:03:01 +00001210 case ARM::BI__builtin_neon_vcagtq_v: {
Bob Wilsond1850352010-12-10 01:11:38 +00001211 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtq);
Nate Begeman30d91712010-06-08 06:03:01 +00001212 return EmitNeonCall(F, Ops, "vcagt");
1213 }
Nate Begeman9eb65a52010-06-08 00:17:19 +00001214 case ARM::BI__builtin_neon_vcls_v:
1215 case ARM::BI__builtin_neon_vclsq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001216 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcls, &Ty, 1);
1217 return EmitNeonCall(F, Ops, "vcls");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001218 }
1219 case ARM::BI__builtin_neon_vclz_v:
1220 case ARM::BI__builtin_neon_vclzq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001221 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vclz, &Ty, 1);
1222 return EmitNeonCall(F, Ops, "vclz");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001223 }
1224 case ARM::BI__builtin_neon_vcnt_v:
1225 case ARM::BI__builtin_neon_vcntq_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001226 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcnt, &Ty, 1);
1227 return EmitNeonCall(F, Ops, "vcnt");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001228 }
1229 // FIXME: intrinsics for f16<->f32 convert missing from ARM target.
1230 case ARM::BI__builtin_neon_vcvt_f32_v:
1231 case ARM::BI__builtin_neon_vcvtq_f32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001232 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1233 Ty = GetNeonType(VMContext, 4, quad);
Nate Begeman9eb65a52010-06-08 00:17:19 +00001234 return usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt")
1235 : Builder.CreateSIToFP(Ops[0], Ty, "vcvt");
1236 }
1237 case ARM::BI__builtin_neon_vcvt_s32_v:
1238 case ARM::BI__builtin_neon_vcvt_u32_v:
1239 case ARM::BI__builtin_neon_vcvtq_s32_v:
1240 case ARM::BI__builtin_neon_vcvtq_u32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001241 Ops[0] = Builder.CreateBitCast(Ops[0], GetNeonType(VMContext, 4, quad));
Nate Begeman9eb65a52010-06-08 00:17:19 +00001242 return usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt")
1243 : Builder.CreateFPToSI(Ops[0], Ty, "vcvt");
1244 }
1245 case ARM::BI__builtin_neon_vcvt_n_f32_v:
1246 case ARM::BI__builtin_neon_vcvtq_n_f32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001247 const llvm::Type *Tys[2] = { GetNeonType(VMContext, 4, quad), Ty };
Nate Begeman9eb65a52010-06-08 00:17:19 +00001248 Int = usgn ? Intrinsic::arm_neon_vcvtfxu2fp : Intrinsic::arm_neon_vcvtfxs2fp;
Nate Begeman30d91712010-06-08 06:03:01 +00001249 Function *F = CGM.getIntrinsic(Int, Tys, 2);
1250 return EmitNeonCall(F, Ops, "vcvt_n");
Nate Begeman9eb65a52010-06-08 00:17:19 +00001251 }
1252 case ARM::BI__builtin_neon_vcvt_n_s32_v:
1253 case ARM::BI__builtin_neon_vcvt_n_u32_v:
1254 case ARM::BI__builtin_neon_vcvtq_n_s32_v:
1255 case ARM::BI__builtin_neon_vcvtq_n_u32_v: {
Nate Begeman30d91712010-06-08 06:03:01 +00001256 const llvm::Type *Tys[2] = { Ty, GetNeonType(VMContext, 4, quad) };
Nate Begeman9eb65a52010-06-08 00:17:19 +00001257 Int = usgn ? Intrinsic::arm_neon_vcvtfp2fxu : Intrinsic::arm_neon_vcvtfp2fxs;
Nate Begeman30d91712010-06-08 06:03:01 +00001258 Function *F = CGM.getIntrinsic(Int, Tys, 2);
1259 return EmitNeonCall(F, Ops, "vcvt_n");
1260 }
1261 case ARM::BI__builtin_neon_vext_v:
1262 case ARM::BI__builtin_neon_vextq_v: {
1263 ConstantInt *C = dyn_cast<ConstantInt>(Ops[2]);
1264 int CV = C->getSExtValue();
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001265 SmallVector<Constant*, 16> Indices;
Nate Begeman4be54302010-06-20 23:05:28 +00001266 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
Chris Lattner77b89b82010-06-27 07:15:29 +00001267 Indices.push_back(ConstantInt::get(Int32Ty, i+CV));
Nate Begeman30d91712010-06-08 06:03:01 +00001268
1269 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1270 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1271 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001272 return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vext");
1273 }
Nate Begeman95450f62010-06-09 05:30:26 +00001274 case ARM::BI__builtin_neon_vget_lane_i8:
1275 case ARM::BI__builtin_neon_vget_lane_i16:
1276 case ARM::BI__builtin_neon_vget_lane_i32:
1277 case ARM::BI__builtin_neon_vget_lane_i64:
1278 case ARM::BI__builtin_neon_vget_lane_f32:
1279 case ARM::BI__builtin_neon_vgetq_lane_i8:
1280 case ARM::BI__builtin_neon_vgetq_lane_i16:
1281 case ARM::BI__builtin_neon_vgetq_lane_i32:
1282 case ARM::BI__builtin_neon_vgetq_lane_i64:
1283 case ARM::BI__builtin_neon_vgetq_lane_f32:
Nate Begemandf98e1d2010-06-09 18:04:15 +00001284 return Builder.CreateExtractElement(Ops[0], EmitScalarExpr(E->getArg(1)),
1285 "vget_lane");
1286 case ARM::BI__builtin_neon_vhadd_v:
1287 case ARM::BI__builtin_neon_vhaddq_v:
1288 Int = usgn ? Intrinsic::arm_neon_vhaddu : Intrinsic::arm_neon_vhadds;
1289 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhadd");
1290 case ARM::BI__builtin_neon_vhsub_v:
1291 case ARM::BI__builtin_neon_vhsubq_v:
1292 Int = usgn ? Intrinsic::arm_neon_vhsubu : Intrinsic::arm_neon_vhsubs;
1293 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhsub");
Nate Begeman4be54302010-06-20 23:05:28 +00001294 case ARM::BI__builtin_neon_vld1_v:
1295 case ARM::BI__builtin_neon_vld1q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001296 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman4be54302010-06-20 23:05:28 +00001297 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vld1, &Ty, 1),
1298 Ops, "vld1");
1299 case ARM::BI__builtin_neon_vld1_lane_v:
1300 case ARM::BI__builtin_neon_vld1q_lane_v:
1301 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1302 Ty = llvm::PointerType::getUnqual(VTy->getElementType());
1303 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1304 Ops[0] = Builder.CreateLoad(Ops[0]);
1305 return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vld1_lane");
1306 case ARM::BI__builtin_neon_vld1_dup_v:
1307 case ARM::BI__builtin_neon_vld1q_dup_v: {
1308 Value *V = UndefValue::get(Ty);
1309 Ty = llvm::PointerType::getUnqual(VTy->getElementType());
1310 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1311 Ops[0] = Builder.CreateLoad(Ops[0]);
Chris Lattner77b89b82010-06-27 07:15:29 +00001312 llvm::Constant *CI = ConstantInt::get(Int32Ty, 0);
Nate Begeman4be54302010-06-20 23:05:28 +00001313 Ops[0] = Builder.CreateInsertElement(V, Ops[0], CI);
1314 return EmitNeonSplat(Ops[0], CI);
1315 }
1316 case ARM::BI__builtin_neon_vld2_v:
1317 case ARM::BI__builtin_neon_vld2q_v: {
1318 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2, &Ty, 1);
Bob Wilson06b6c582010-08-27 17:14:29 +00001319 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1320 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld2");
Nate Begeman4be54302010-06-20 23:05:28 +00001321 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1322 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1323 return Builder.CreateStore(Ops[1], Ops[0]);
1324 }
1325 case ARM::BI__builtin_neon_vld3_v:
1326 case ARM::BI__builtin_neon_vld3q_v: {
1327 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3, &Ty, 1);
Bob Wilson06b6c582010-08-27 17:14:29 +00001328 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1329 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld3");
Nate Begeman4be54302010-06-20 23:05:28 +00001330 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1331 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1332 return Builder.CreateStore(Ops[1], Ops[0]);
1333 }
1334 case ARM::BI__builtin_neon_vld4_v:
1335 case ARM::BI__builtin_neon_vld4q_v: {
1336 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4, &Ty, 1);
Bob Wilson06b6c582010-08-27 17:14:29 +00001337 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1338 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld4");
Nate Begeman4be54302010-06-20 23:05:28 +00001339 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1340 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1341 return Builder.CreateStore(Ops[1], Ops[0]);
1342 }
1343 case ARM::BI__builtin_neon_vld2_lane_v:
1344 case ARM::BI__builtin_neon_vld2q_lane_v: {
1345 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2lane, &Ty, 1);
1346 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
1347 Ops[3] = Builder.CreateBitCast(Ops[3], Ty);
Bob Wilson06b6c582010-08-27 17:14:29 +00001348 Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001349 Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld2_lane");
1350 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1351 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1352 return Builder.CreateStore(Ops[1], Ops[0]);
1353 }
1354 case ARM::BI__builtin_neon_vld3_lane_v:
1355 case ARM::BI__builtin_neon_vld3q_lane_v: {
1356 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3lane, &Ty, 1);
1357 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
1358 Ops[3] = Builder.CreateBitCast(Ops[3], Ty);
1359 Ops[4] = Builder.CreateBitCast(Ops[4], Ty);
Bob Wilson06b6c582010-08-27 17:14:29 +00001360 Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001361 Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane");
1362 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1363 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1364 return Builder.CreateStore(Ops[1], Ops[0]);
1365 }
1366 case ARM::BI__builtin_neon_vld4_lane_v:
1367 case ARM::BI__builtin_neon_vld4q_lane_v: {
1368 Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4lane, &Ty, 1);
1369 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
1370 Ops[3] = Builder.CreateBitCast(Ops[3], Ty);
1371 Ops[4] = Builder.CreateBitCast(Ops[4], Ty);
1372 Ops[5] = Builder.CreateBitCast(Ops[5], Ty);
Bob Wilson06b6c582010-08-27 17:14:29 +00001373 Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001374 Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane");
1375 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1376 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1377 return Builder.CreateStore(Ops[1], Ops[0]);
1378 }
1379 case ARM::BI__builtin_neon_vld2_dup_v:
1380 case ARM::BI__builtin_neon_vld3_dup_v:
1381 case ARM::BI__builtin_neon_vld4_dup_v: {
Bob Wilsona0eb23b2010-12-10 22:54:58 +00001382 // Handle 64-bit elements as a special-case. There is no "dup" needed.
1383 if (VTy->getElementType()->getPrimitiveSizeInBits() == 64) {
1384 switch (BuiltinID) {
1385 case ARM::BI__builtin_neon_vld2_dup_v:
1386 Int = Intrinsic::arm_neon_vld2;
1387 break;
1388 case ARM::BI__builtin_neon_vld3_dup_v:
1389 Int = Intrinsic::arm_neon_vld2;
1390 break;
1391 case ARM::BI__builtin_neon_vld4_dup_v:
1392 Int = Intrinsic::arm_neon_vld2;
1393 break;
1394 default: assert(0 && "unknown vld_dup intrinsic?");
1395 }
1396 Function *F = CGM.getIntrinsic(Int, &Ty, 1);
1397 Value *Align = GetPointeeAlignment(*this, E->getArg(1));
1398 Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld_dup");
1399 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1400 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1401 return Builder.CreateStore(Ops[1], Ops[0]);
1402 }
Nate Begeman4be54302010-06-20 23:05:28 +00001403 switch (BuiltinID) {
1404 case ARM::BI__builtin_neon_vld2_dup_v:
1405 Int = Intrinsic::arm_neon_vld2lane;
1406 break;
1407 case ARM::BI__builtin_neon_vld3_dup_v:
1408 Int = Intrinsic::arm_neon_vld2lane;
1409 break;
1410 case ARM::BI__builtin_neon_vld4_dup_v:
1411 Int = Intrinsic::arm_neon_vld2lane;
1412 break;
1413 default: assert(0 && "unknown vld_dup intrinsic?");
1414 }
1415 Function *F = CGM.getIntrinsic(Int, &Ty, 1);
1416 const llvm::StructType *STy = cast<llvm::StructType>(F->getReturnType());
1417
1418 SmallVector<Value*, 6> Args;
1419 Args.push_back(Ops[1]);
1420 Args.append(STy->getNumElements(), UndefValue::get(Ty));
1421
Chris Lattner77b89b82010-06-27 07:15:29 +00001422 llvm::Constant *CI = ConstantInt::get(Int32Ty, 0);
Nate Begeman4be54302010-06-20 23:05:28 +00001423 Args.push_back(CI);
Bob Wilson06b6c582010-08-27 17:14:29 +00001424 Args.push_back(GetPointeeAlignment(*this, E->getArg(1)));
Nate Begeman4be54302010-06-20 23:05:28 +00001425
1426 Ops[1] = Builder.CreateCall(F, Args.begin(), Args.end(), "vld_dup");
1427 // splat lane 0 to all elts in each vector of the result.
1428 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1429 Value *Val = Builder.CreateExtractValue(Ops[1], i);
1430 Value *Elt = Builder.CreateBitCast(Val, Ty);
1431 Elt = EmitNeonSplat(Elt, CI);
1432 Elt = Builder.CreateBitCast(Elt, Val->getType());
1433 Ops[1] = Builder.CreateInsertValue(Ops[1], Elt, i);
1434 }
1435 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1436 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1437 return Builder.CreateStore(Ops[1], Ops[0]);
1438 }
Nate Begemandf98e1d2010-06-09 18:04:15 +00001439 case ARM::BI__builtin_neon_vmax_v:
1440 case ARM::BI__builtin_neon_vmaxq_v:
1441 Int = usgn ? Intrinsic::arm_neon_vmaxu : Intrinsic::arm_neon_vmaxs;
1442 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmax");
1443 case ARM::BI__builtin_neon_vmin_v:
1444 case ARM::BI__builtin_neon_vminq_v:
1445 Int = usgn ? Intrinsic::arm_neon_vminu : Intrinsic::arm_neon_vmins;
1446 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmin");
Bob Wilson22359412010-09-02 22:37:30 +00001447 case ARM::BI__builtin_neon_vmovl_v: {
1448 const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy);
1449 Ops[0] = Builder.CreateBitCast(Ops[0], DTy);
Bob Wilson7cea3222010-08-20 03:36:08 +00001450 if (usgn)
1451 return Builder.CreateZExt(Ops[0], Ty, "vmovl");
1452 return Builder.CreateSExt(Ops[0], Ty, "vmovl");
Bob Wilson22359412010-09-02 22:37:30 +00001453 }
1454 case ARM::BI__builtin_neon_vmovn_v: {
1455 const llvm::Type *QTy = llvm::VectorType::getExtendedElementVectorType(VTy);
1456 Ops[0] = Builder.CreateBitCast(Ops[0], QTy);
Bob Wilson3b6081b2010-08-30 19:57:13 +00001457 return Builder.CreateTrunc(Ops[0], Ty, "vmovn");
Bob Wilson22359412010-09-02 22:37:30 +00001458 }
Bob Wilson953d5132010-12-03 17:29:39 +00001459 case ARM::BI__builtin_neon_vmul_v:
Bob Wilson141e4892010-12-10 23:09:09 +00001460 case ARM::BI__builtin_neon_vmulq_v:
Bob Wilson953d5132010-12-03 17:29:39 +00001461 assert(poly && "vmul builtin only supported for polynomial types");
1462 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vmulp, &Ty, 1),
1463 Ops, "vmul");
Bob Wilsonc92b7722010-12-07 22:03:46 +00001464 case ARM::BI__builtin_neon_vmull_v:
1465 assert(poly && "vmull builtin only supported for polynomial types");
1466 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vmullp, &Ty, 1),
1467 Ops, "vmull");
Nate Begemandf98e1d2010-06-09 18:04:15 +00001468 case ARM::BI__builtin_neon_vpadal_v:
Bob Wilsonc1fa01b2010-12-10 05:51:07 +00001469 case ARM::BI__builtin_neon_vpadalq_v: {
Nate Begemandf98e1d2010-06-09 18:04:15 +00001470 Int = usgn ? Intrinsic::arm_neon_vpadalu : Intrinsic::arm_neon_vpadals;
Bob Wilsonc1fa01b2010-12-10 05:51:07 +00001471 // The source operand type has twice as many elements of half the size.
1472 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
1473 const llvm::Type *EltTy = llvm::IntegerType::get(VMContext, EltBits / 2);
1474 const llvm::Type *NarrowTy =
1475 llvm::VectorType::get(EltTy, VTy->getNumElements() * 2);
1476 const llvm::Type *Tys[2] = { Ty, NarrowTy };
1477 return EmitNeonCall(CGM.getIntrinsic(Int, Tys, 2), Ops, "vpadal");
1478 }
Nate Begeman548f7da2010-06-10 18:11:55 +00001479 case ARM::BI__builtin_neon_vpadd_v:
1480 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vpadd, &Ty, 1),
1481 Ops, "vpadd");
1482 case ARM::BI__builtin_neon_vpaddl_v:
Bob Wilsonc1fa01b2010-12-10 05:51:07 +00001483 case ARM::BI__builtin_neon_vpaddlq_v: {
Nate Begeman548f7da2010-06-10 18:11:55 +00001484 Int = usgn ? Intrinsic::arm_neon_vpaddlu : Intrinsic::arm_neon_vpaddls;
Bob Wilsonc1fa01b2010-12-10 05:51:07 +00001485 // The source operand type has twice as many elements of half the size.
1486 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
1487 const llvm::Type *EltTy = llvm::IntegerType::get(VMContext, EltBits / 2);
1488 const llvm::Type *NarrowTy =
1489 llvm::VectorType::get(EltTy, VTy->getNumElements() * 2);
1490 const llvm::Type *Tys[2] = { Ty, NarrowTy };
1491 return EmitNeonCall(CGM.getIntrinsic(Int, Tys, 2), Ops, "vpaddl");
1492 }
Nate Begeman548f7da2010-06-10 18:11:55 +00001493 case ARM::BI__builtin_neon_vpmax_v:
1494 Int = usgn ? Intrinsic::arm_neon_vpmaxu : Intrinsic::arm_neon_vpmaxs;
1495 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmax");
1496 case ARM::BI__builtin_neon_vpmin_v:
1497 Int = usgn ? Intrinsic::arm_neon_vpminu : Intrinsic::arm_neon_vpmins;
1498 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmin");
1499 case ARM::BI__builtin_neon_vqabs_v:
1500 case ARM::BI__builtin_neon_vqabsq_v:
1501 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqabs, &Ty, 1),
1502 Ops, "vqabs");
1503 case ARM::BI__builtin_neon_vqadd_v:
1504 case ARM::BI__builtin_neon_vqaddq_v:
1505 Int = usgn ? Intrinsic::arm_neon_vqaddu : Intrinsic::arm_neon_vqadds;
1506 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqadd");
Nate Begeman548f7da2010-06-10 18:11:55 +00001507 case ARM::BI__builtin_neon_vqdmlal_v:
1508 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlal, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001509 Ops, "vqdmlal");
Nate Begeman548f7da2010-06-10 18:11:55 +00001510 case ARM::BI__builtin_neon_vqdmlsl_v:
1511 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlsl, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001512 Ops, "vqdmlsl");
Nate Begeman548f7da2010-06-10 18:11:55 +00001513 case ARM::BI__builtin_neon_vqdmulh_v:
1514 case ARM::BI__builtin_neon_vqdmulhq_v:
1515 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmulh, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001516 Ops, "vqdmulh");
Nate Begeman548f7da2010-06-10 18:11:55 +00001517 case ARM::BI__builtin_neon_vqdmull_v:
1518 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmull, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001519 Ops, "vqdmull");
Nate Begeman548f7da2010-06-10 18:11:55 +00001520 case ARM::BI__builtin_neon_vqmovn_v:
1521 Int = usgn ? Intrinsic::arm_neon_vqmovnu : Intrinsic::arm_neon_vqmovns;
1522 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqmovn");
1523 case ARM::BI__builtin_neon_vqmovun_v:
1524 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqmovnsu, &Ty, 1),
1525 Ops, "vqdmull");
1526 case ARM::BI__builtin_neon_vqneg_v:
Bob Wilson30daefc2010-12-10 06:26:19 +00001527 case ARM::BI__builtin_neon_vqnegq_v:
Nate Begeman61eecf52010-06-14 05:21:25 +00001528 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqneg, &Ty, 1),
1529 Ops, "vqneg");
Nate Begeman548f7da2010-06-10 18:11:55 +00001530 case ARM::BI__builtin_neon_vqrdmulh_v:
1531 case ARM::BI__builtin_neon_vqrdmulhq_v:
1532 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrdmulh, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001533 Ops, "vqrdmulh");
Nate Begeman548f7da2010-06-10 18:11:55 +00001534 case ARM::BI__builtin_neon_vqrshl_v:
1535 case ARM::BI__builtin_neon_vqrshlq_v:
1536 Int = usgn ? Intrinsic::arm_neon_vqrshiftu : Intrinsic::arm_neon_vqrshifts;
1537 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshl");
1538 case ARM::BI__builtin_neon_vqrshrn_n_v:
1539 Int = usgn ? Intrinsic::arm_neon_vqrshiftnu : Intrinsic::arm_neon_vqrshiftns;
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001540 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshrn_n",
Nate Begeman61eecf52010-06-14 05:21:25 +00001541 1, true);
Nate Begeman548f7da2010-06-10 18:11:55 +00001542 case ARM::BI__builtin_neon_vqrshrun_n_v:
1543 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrshiftnsu, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001544 Ops, "vqrshrun_n", 1, true);
Nate Begeman61eecf52010-06-14 05:21:25 +00001545 case ARM::BI__builtin_neon_vqshl_v:
1546 case ARM::BI__builtin_neon_vqshlq_v:
1547 Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts;
1548 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl");
1549 case ARM::BI__builtin_neon_vqshl_n_v:
1550 case ARM::BI__builtin_neon_vqshlq_n_v:
1551 Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts;
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001552 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl_n",
Nate Begeman61eecf52010-06-14 05:21:25 +00001553 1, false);
1554 case ARM::BI__builtin_neon_vqshlu_n_v:
1555 case ARM::BI__builtin_neon_vqshluq_n_v:
1556 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftsu, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001557 Ops, "vqshlu", 1, false);
Nate Begeman61eecf52010-06-14 05:21:25 +00001558 case ARM::BI__builtin_neon_vqshrn_n_v:
1559 Int = usgn ? Intrinsic::arm_neon_vqshiftnu : Intrinsic::arm_neon_vqshiftns;
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001560 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshrn_n",
Nate Begeman61eecf52010-06-14 05:21:25 +00001561 1, true);
1562 case ARM::BI__builtin_neon_vqshrun_n_v:
1563 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftnsu, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001564 Ops, "vqshrun_n", 1, true);
Nate Begeman464ccb62010-06-11 22:57:12 +00001565 case ARM::BI__builtin_neon_vqsub_v:
1566 case ARM::BI__builtin_neon_vqsubq_v:
1567 Int = usgn ? Intrinsic::arm_neon_vqsubu : Intrinsic::arm_neon_vqsubs;
1568 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqsub");
1569 case ARM::BI__builtin_neon_vraddhn_v:
1570 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vraddhn, &Ty, 1),
1571 Ops, "vraddhn");
1572 case ARM::BI__builtin_neon_vrecpe_v:
1573 case ARM::BI__builtin_neon_vrecpeq_v:
1574 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecpe, &Ty, 1),
1575 Ops, "vrecpe");
1576 case ARM::BI__builtin_neon_vrecps_v:
1577 case ARM::BI__builtin_neon_vrecpsq_v:
1578 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecps, &Ty, 1),
1579 Ops, "vrecps");
Nate Begeman464ccb62010-06-11 22:57:12 +00001580 case ARM::BI__builtin_neon_vrhadd_v:
1581 case ARM::BI__builtin_neon_vrhaddq_v:
1582 Int = usgn ? Intrinsic::arm_neon_vrhaddu : Intrinsic::arm_neon_vrhadds;
1583 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrhadd");
Nate Begeman5af93ef2010-06-12 06:06:07 +00001584 case ARM::BI__builtin_neon_vrshl_v:
1585 case ARM::BI__builtin_neon_vrshlq_v:
1586 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
1587 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshl");
1588 case ARM::BI__builtin_neon_vrshrn_n_v:
Nate Begeman5af93ef2010-06-12 06:06:07 +00001589 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrshiftn, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001590 Ops, "vrshrn_n", 1, true);
Nate Begeman5af93ef2010-06-12 06:06:07 +00001591 case ARM::BI__builtin_neon_vrshr_n_v:
1592 case ARM::BI__builtin_neon_vrshrq_n_v:
Nate Begeman5af93ef2010-06-12 06:06:07 +00001593 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001594 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshr_n", 1, true);
Nate Begeman5af93ef2010-06-12 06:06:07 +00001595 case ARM::BI__builtin_neon_vrsqrte_v:
1596 case ARM::BI__builtin_neon_vrsqrteq_v:
1597 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrte, &Ty, 1),
1598 Ops, "vrsqrte");
1599 case ARM::BI__builtin_neon_vrsqrts_v:
1600 case ARM::BI__builtin_neon_vrsqrtsq_v:
1601 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrts, &Ty, 1),
1602 Ops, "vrsqrts");
1603 case ARM::BI__builtin_neon_vrsra_n_v:
1604 case ARM::BI__builtin_neon_vrsraq_n_v:
1605 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1606 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1607 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, true);
1608 Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts;
1609 Ops[1] = Builder.CreateCall2(CGM.getIntrinsic(Int, &Ty, 1), Ops[1], Ops[2]);
1610 return Builder.CreateAdd(Ops[0], Ops[1], "vrsra_n");
Nate Begeman464ccb62010-06-11 22:57:12 +00001611 case ARM::BI__builtin_neon_vrsubhn_v:
1612 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsubhn, &Ty, 1),
1613 Ops, "vrsubhn");
Nate Begeman548f7da2010-06-10 18:11:55 +00001614 case ARM::BI__builtin_neon_vset_lane_i8:
1615 case ARM::BI__builtin_neon_vset_lane_i16:
1616 case ARM::BI__builtin_neon_vset_lane_i32:
1617 case ARM::BI__builtin_neon_vset_lane_i64:
1618 case ARM::BI__builtin_neon_vset_lane_f32:
1619 case ARM::BI__builtin_neon_vsetq_lane_i8:
1620 case ARM::BI__builtin_neon_vsetq_lane_i16:
1621 case ARM::BI__builtin_neon_vsetq_lane_i32:
1622 case ARM::BI__builtin_neon_vsetq_lane_i64:
1623 case ARM::BI__builtin_neon_vsetq_lane_f32:
1624 Ops.push_back(EmitScalarExpr(E->getArg(2)));
1625 return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vset_lane");
Nate Begeman464ccb62010-06-11 22:57:12 +00001626 case ARM::BI__builtin_neon_vshl_v:
1627 case ARM::BI__builtin_neon_vshlq_v:
1628 Int = usgn ? Intrinsic::arm_neon_vshiftu : Intrinsic::arm_neon_vshifts;
1629 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshl");
1630 case ARM::BI__builtin_neon_vshll_n_v:
Nate Begeman464ccb62010-06-11 22:57:12 +00001631 Int = usgn ? Intrinsic::arm_neon_vshiftlu : Intrinsic::arm_neon_vshiftls;
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001632 return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshll", 1);
Nate Begeman464ccb62010-06-11 22:57:12 +00001633 case ARM::BI__builtin_neon_vshl_n_v:
1634 case ARM::BI__builtin_neon_vshlq_n_v:
Nate Begeman61eecf52010-06-14 05:21:25 +00001635 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false);
1636 return Builder.CreateShl(Builder.CreateBitCast(Ops[0],Ty), Ops[1], "vshl_n");
Nate Begeman464ccb62010-06-11 22:57:12 +00001637 case ARM::BI__builtin_neon_vshrn_n_v:
Nate Begeman464ccb62010-06-11 22:57:12 +00001638 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftn, &Ty, 1),
Bob Wilsondb3d4d02010-12-08 22:37:56 +00001639 Ops, "vshrn_n", 1, true);
Nate Begeman464ccb62010-06-11 22:57:12 +00001640 case ARM::BI__builtin_neon_vshr_n_v:
1641 case ARM::BI__builtin_neon_vshrq_n_v:
1642 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
Nate Begeman61eecf52010-06-14 05:21:25 +00001643 Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false);
Nate Begeman464ccb62010-06-11 22:57:12 +00001644 if (usgn)
1645 return Builder.CreateLShr(Ops[0], Ops[1], "vshr_n");
1646 else
1647 return Builder.CreateAShr(Ops[0], Ops[1], "vshr_n");
1648 case ARM::BI__builtin_neon_vsri_n_v:
1649 case ARM::BI__builtin_neon_vsriq_n_v:
Bob Wilson79653962010-12-03 17:10:22 +00001650 rightShift = true;
Nate Begeman464ccb62010-06-11 22:57:12 +00001651 case ARM::BI__builtin_neon_vsli_n_v:
1652 case ARM::BI__builtin_neon_vsliq_n_v:
Bob Wilson79653962010-12-03 17:10:22 +00001653 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, rightShift);
Nate Begeman464ccb62010-06-11 22:57:12 +00001654 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftins, &Ty, 1),
1655 Ops, "vsli_n");
1656 case ARM::BI__builtin_neon_vsra_n_v:
1657 case ARM::BI__builtin_neon_vsraq_n_v:
1658 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1659 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman61eecf52010-06-14 05:21:25 +00001660 Ops[2] = EmitNeonShiftVector(Ops[2], Ty, false);
Nate Begeman464ccb62010-06-11 22:57:12 +00001661 if (usgn)
1662 Ops[1] = Builder.CreateLShr(Ops[1], Ops[2], "vsra_n");
1663 else
1664 Ops[1] = Builder.CreateAShr(Ops[1], Ops[2], "vsra_n");
1665 return Builder.CreateAdd(Ops[0], Ops[1]);
1666 case ARM::BI__builtin_neon_vst1_v:
1667 case ARM::BI__builtin_neon_vst1q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001668 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001669 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst1, &Ty, 1),
1670 Ops, "");
1671 case ARM::BI__builtin_neon_vst1_lane_v:
1672 case ARM::BI__builtin_neon_vst1q_lane_v:
1673 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1674 Ops[1] = Builder.CreateExtractElement(Ops[1], Ops[2]);
1675 Ty = llvm::PointerType::getUnqual(Ops[1]->getType());
1676 return Builder.CreateStore(Ops[1], Builder.CreateBitCast(Ops[0], Ty));
1677 case ARM::BI__builtin_neon_vst2_v:
1678 case ARM::BI__builtin_neon_vst2q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001679 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001680 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2, &Ty, 1),
1681 Ops, "");
1682 case ARM::BI__builtin_neon_vst2_lane_v:
1683 case ARM::BI__builtin_neon_vst2q_lane_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001684 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001685 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2lane, &Ty, 1),
1686 Ops, "");
1687 case ARM::BI__builtin_neon_vst3_v:
1688 case ARM::BI__builtin_neon_vst3q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001689 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001690 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3, &Ty, 1),
1691 Ops, "");
1692 case ARM::BI__builtin_neon_vst3_lane_v:
1693 case ARM::BI__builtin_neon_vst3q_lane_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001694 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001695 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3lane, &Ty, 1),
1696 Ops, "");
1697 case ARM::BI__builtin_neon_vst4_v:
1698 case ARM::BI__builtin_neon_vst4q_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001699 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001700 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4, &Ty, 1),
1701 Ops, "");
1702 case ARM::BI__builtin_neon_vst4_lane_v:
1703 case ARM::BI__builtin_neon_vst4q_lane_v:
Bob Wilson06b6c582010-08-27 17:14:29 +00001704 Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));
Nate Begeman464ccb62010-06-11 22:57:12 +00001705 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4lane, &Ty, 1),
1706 Ops, "");
Nate Begeman548f7da2010-06-10 18:11:55 +00001707 case ARM::BI__builtin_neon_vsubhn_v:
1708 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vsubhn, &Ty, 1),
1709 Ops, "vsubhn");
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001710 case ARM::BI__builtin_neon_vtbl1_v:
1711 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl1),
1712 Ops, "vtbl1");
1713 case ARM::BI__builtin_neon_vtbl2_v:
1714 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl2),
1715 Ops, "vtbl2");
1716 case ARM::BI__builtin_neon_vtbl3_v:
1717 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl3),
1718 Ops, "vtbl3");
1719 case ARM::BI__builtin_neon_vtbl4_v:
1720 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl4),
1721 Ops, "vtbl4");
1722 case ARM::BI__builtin_neon_vtbx1_v:
1723 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx1),
1724 Ops, "vtbx1");
1725 case ARM::BI__builtin_neon_vtbx2_v:
1726 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx2),
1727 Ops, "vtbx2");
1728 case ARM::BI__builtin_neon_vtbx3_v:
1729 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx3),
1730 Ops, "vtbx3");
1731 case ARM::BI__builtin_neon_vtbx4_v:
1732 return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx4),
1733 Ops, "vtbx4");
1734 case ARM::BI__builtin_neon_vtst_v:
1735 case ARM::BI__builtin_neon_vtstq_v: {
1736 Ops[0] = Builder.CreateBitCast(Ops[0], Ty);
1737 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
1738 Ops[0] = Builder.CreateAnd(Ops[0], Ops[1]);
1739 Ops[0] = Builder.CreateICmp(ICmpInst::ICMP_NE, Ops[0],
1740 ConstantAggregateZero::get(Ty));
1741 return Builder.CreateSExt(Ops[0], Ty, "vtst");
1742 }
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001743 case ARM::BI__builtin_neon_vtrn_v:
1744 case ARM::BI__builtin_neon_vtrnq_v: {
Nate Begeman4be54302010-06-20 23:05:28 +00001745 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001746 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001747 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001748 Value *SV;
1749
1750 for (unsigned vi = 0; vi != 2; ++vi) {
1751 SmallVector<Constant*, 16> Indices;
1752 for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001753 Indices.push_back(ConstantInt::get(Int32Ty, i+vi));
1754 Indices.push_back(ConstantInt::get(Int32Ty, i+e+vi));
Nate Begeman4be54302010-06-20 23:05:28 +00001755 }
1756 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
1757 SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1758 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vtrn");
1759 SV = Builder.CreateStore(SV, Addr);
1760 }
1761 return SV;
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001762 }
1763 case ARM::BI__builtin_neon_vuzp_v:
1764 case ARM::BI__builtin_neon_vuzpq_v: {
Nate Begeman4be54302010-06-20 23:05:28 +00001765 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001766 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001767 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001768 Value *SV;
1769
1770 for (unsigned vi = 0; vi != 2; ++vi) {
1771 SmallVector<Constant*, 16> Indices;
1772 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
Chris Lattner77b89b82010-06-27 07:15:29 +00001773 Indices.push_back(ConstantInt::get(Int32Ty, 2*i+vi));
Nate Begeman4be54302010-06-20 23:05:28 +00001774
1775 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
1776 SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1777 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vuzp");
1778 SV = Builder.CreateStore(SV, Addr);
1779 }
1780 return SV;
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001781 }
1782 case ARM::BI__builtin_neon_vzip_v:
1783 case ARM::BI__builtin_neon_vzipq_v: {
Nate Begeman4be54302010-06-20 23:05:28 +00001784 Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty));
Nate Begeman1c2a88c2010-06-09 01:10:23 +00001785 Ops[1] = Builder.CreateBitCast(Ops[1], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001786 Ops[2] = Builder.CreateBitCast(Ops[2], Ty);
Nate Begeman4be54302010-06-20 23:05:28 +00001787 Value *SV;
1788
1789 for (unsigned vi = 0; vi != 2; ++vi) {
1790 SmallVector<Constant*, 16> Indices;
1791 for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) {
Daniel Dunbare361cc32010-08-26 00:55:57 +00001792 Indices.push_back(ConstantInt::get(Int32Ty, (i + vi*e) >> 1));
1793 Indices.push_back(ConstantInt::get(Int32Ty, ((i + vi*e) >> 1)+e));
Nate Begeman4be54302010-06-20 23:05:28 +00001794 }
1795 Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi);
1796 SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
1797 SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vzip");
1798 SV = Builder.CreateStore(SV, Addr);
1799 }
1800 return SV;
Nate Begeman9eb65a52010-06-08 00:17:19 +00001801 }
Chris Lattner2752c012010-03-03 19:03:45 +00001802 }
1803}
1804
Bill Wendlingaa51e512010-10-09 08:47:25 +00001805llvm::Value *CodeGenFunction::
1806BuildVector(const llvm::SmallVectorImpl<llvm::Value*> &Ops) {
1807 assert((Ops.size() & (Ops.size() - 1)) == 0 &&
1808 "Not a power-of-two sized vector!");
1809 bool AllConstants = true;
1810 for (unsigned i = 0, e = Ops.size(); i != e && AllConstants; ++i)
1811 AllConstants &= isa<Constant>(Ops[i]);
1812
1813 // If this is a constant vector, create a ConstantVector.
1814 if (AllConstants) {
1815 std::vector<llvm::Constant*> CstOps;
1816 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1817 CstOps.push_back(cast<Constant>(Ops[i]));
1818 return llvm::ConstantVector::get(CstOps);
1819 }
1820
1821 // Otherwise, insertelement the values to build the vector.
1822 Value *Result =
1823 llvm::UndefValue::get(llvm::VectorType::get(Ops[0]->getType(), Ops.size()));
1824
1825 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1826 Result = Builder.CreateInsertElement(Result, Ops[i],
1827 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), i));
1828
1829 return Result;
1830}
1831
Mike Stump1eb44332009-09-09 15:08:12 +00001832Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +00001833 const CallExpr *E) {
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001834 llvm::SmallVector<Value*, 4> Ops;
1835
Chris Lattner46c55912010-10-02 00:09:12 +00001836 // Find out if any arguments are required to be integer constant expressions.
1837 unsigned ICEArguments = 0;
1838 ASTContext::GetBuiltinTypeError Error;
1839 getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
1840 assert(Error == ASTContext::GE_None && "Should not codegen an error");
1841
1842 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) {
1843 // If this is a normal argument, just emit it as a scalar.
1844 if ((ICEArguments & (1 << i)) == 0) {
1845 Ops.push_back(EmitScalarExpr(E->getArg(i)));
1846 continue;
1847 }
1848
1849 // If this is required to be a constant, constant fold it so that we know
1850 // that the generated intrinsic gets a ConstantInt.
1851 llvm::APSInt Result;
1852 bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result, getContext());
1853 assert(IsConst && "Constant arg isn't actually constant?"); (void)IsConst;
1854 Ops.push_back(llvm::ConstantInt::get(VMContext, Result));
1855 }
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001856
Anders Carlsson564f1de2007-12-09 23:17:02 +00001857 switch (BuiltinID) {
Anders Carlsson46a26b02007-12-09 23:39:18 +00001858 default: return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001859 case X86::BI__builtin_ia32_pslldi128:
Nate Begemane7722102008-04-14 04:49:57 +00001860 case X86::BI__builtin_ia32_psllqi128:
Mike Stump1eb44332009-09-09 15:08:12 +00001861 case X86::BI__builtin_ia32_psllwi128:
Nate Begemane7722102008-04-14 04:49:57 +00001862 case X86::BI__builtin_ia32_psradi128:
1863 case X86::BI__builtin_ia32_psrawi128:
1864 case X86::BI__builtin_ia32_psrldi128:
1865 case X86::BI__builtin_ia32_psrlqi128:
1866 case X86::BI__builtin_ia32_psrlwi128: {
Chris Lattner77b89b82010-06-27 07:15:29 +00001867 Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext");
1868 const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 2);
1869 llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
Owen Anderson03e20502009-07-30 23:11:26 +00001870 Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty),
Nate Begemane7722102008-04-14 04:49:57 +00001871 Ops[1], Zero, "insert");
1872 Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast");
1873 const char *name = 0;
1874 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Nate Begemane7722102008-04-14 04:49:57 +00001876 switch (BuiltinID) {
1877 default: assert(0 && "Unsupported shift intrinsic!");
1878 case X86::BI__builtin_ia32_pslldi128:
1879 name = "pslldi";
1880 ID = Intrinsic::x86_sse2_psll_d;
1881 break;
1882 case X86::BI__builtin_ia32_psllqi128:
1883 name = "psllqi";
1884 ID = Intrinsic::x86_sse2_psll_q;
1885 break;
1886 case X86::BI__builtin_ia32_psllwi128:
1887 name = "psllwi";
1888 ID = Intrinsic::x86_sse2_psll_w;
1889 break;
1890 case X86::BI__builtin_ia32_psradi128:
1891 name = "psradi";
1892 ID = Intrinsic::x86_sse2_psra_d;
1893 break;
1894 case X86::BI__builtin_ia32_psrawi128:
1895 name = "psrawi";
1896 ID = Intrinsic::x86_sse2_psra_w;
1897 break;
1898 case X86::BI__builtin_ia32_psrldi128:
1899 name = "psrldi";
1900 ID = Intrinsic::x86_sse2_psrl_d;
1901 break;
1902 case X86::BI__builtin_ia32_psrlqi128:
1903 name = "psrlqi";
1904 ID = Intrinsic::x86_sse2_psrl_q;
1905 break;
1906 case X86::BI__builtin_ia32_psrlwi128:
1907 name = "psrlwi";
1908 ID = Intrinsic::x86_sse2_psrl_w;
1909 break;
1910 }
1911 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001912 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Nate Begemane7722102008-04-14 04:49:57 +00001913 }
Bill Wendlingaa51e512010-10-09 08:47:25 +00001914 case X86::BI__builtin_ia32_vec_init_v8qi:
1915 case X86::BI__builtin_ia32_vec_init_v4hi:
1916 case X86::BI__builtin_ia32_vec_init_v2si:
1917 return Builder.CreateBitCast(BuildVector(Ops),
1918 llvm::Type::getX86_MMXTy(VMContext));
Argyrios Kyrtzidis1944ec12010-10-10 03:19:11 +00001919 case X86::BI__builtin_ia32_vec_ext_v2si:
1920 return Builder.CreateExtractElement(Ops[0],
1921 llvm::ConstantInt::get(Ops[1]->getType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00001922 case X86::BI__builtin_ia32_pslldi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001923 case X86::BI__builtin_ia32_psllqi:
Mike Stump1eb44332009-09-09 15:08:12 +00001924 case X86::BI__builtin_ia32_psllwi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001925 case X86::BI__builtin_ia32_psradi:
1926 case X86::BI__builtin_ia32_psrawi:
1927 case X86::BI__builtin_ia32_psrldi:
1928 case X86::BI__builtin_ia32_psrlqi:
1929 case X86::BI__builtin_ia32_psrlwi: {
Chris Lattner77b89b82010-06-27 07:15:29 +00001930 Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext");
1931 const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 1);
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001932 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001933 const char *name = 0;
1934 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001936 switch (BuiltinID) {
1937 default: assert(0 && "Unsupported shift intrinsic!");
1938 case X86::BI__builtin_ia32_pslldi:
1939 name = "pslldi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001940 ID = Intrinsic::x86_mmx_psll_d;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001941 break;
1942 case X86::BI__builtin_ia32_psllqi:
1943 name = "psllqi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001944 ID = Intrinsic::x86_mmx_psll_q;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001945 break;
1946 case X86::BI__builtin_ia32_psllwi:
1947 name = "psllwi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001948 ID = Intrinsic::x86_mmx_psll_w;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001949 break;
1950 case X86::BI__builtin_ia32_psradi:
1951 name = "psradi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001952 ID = Intrinsic::x86_mmx_psra_d;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001953 break;
1954 case X86::BI__builtin_ia32_psrawi:
1955 name = "psrawi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001956 ID = Intrinsic::x86_mmx_psra_w;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001957 break;
1958 case X86::BI__builtin_ia32_psrldi:
1959 name = "psrldi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001960 ID = Intrinsic::x86_mmx_psrl_d;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001961 break;
1962 case X86::BI__builtin_ia32_psrlqi:
1963 name = "psrlqi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001964 ID = Intrinsic::x86_mmx_psrl_q;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001965 break;
1966 case X86::BI__builtin_ia32_psrlwi:
1967 name = "psrlwi";
Bill Wendlingdf4d4822010-09-27 21:22:25 +00001968 ID = Intrinsic::x86_mmx_psrl_w;
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001969 break;
1970 }
Chris Lattner7acda7c2007-12-18 00:25:38 +00001971 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001972 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Anders Carlsson2929cfa2007-12-14 17:48:24 +00001973 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +00001974 case X86::BI__builtin_ia32_cmpps: {
1975 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps);
1976 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps");
1977 }
1978 case X86::BI__builtin_ia32_cmpss: {
1979 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss);
1980 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +00001981 }
Nate Begemane7722102008-04-14 04:49:57 +00001982 case X86::BI__builtin_ia32_ldmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001983 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00001984 Value *One = llvm::ConstantInt::get(Int32Ty, 1);
1985 Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +00001986 Builder.CreateStore(Ops[0], Tmp);
1987 return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +00001988 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +00001989 }
1990 case X86::BI__builtin_ia32_stmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001991 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00001992 Value *One = llvm::ConstantInt::get(Int32Ty, 1);
1993 Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +00001994 One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +00001995 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +00001996 return Builder.CreateLoad(Tmp, "stmxcsr");
1997 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +00001998 case X86::BI__builtin_ia32_cmppd: {
1999 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd);
2000 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd");
2001 }
2002 case X86::BI__builtin_ia32_cmpsd: {
2003 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd);
2004 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +00002005 }
Nate Begemane7722102008-04-14 04:49:57 +00002006 case X86::BI__builtin_ia32_storehps:
2007 case X86::BI__builtin_ia32_storelps: {
Chris Lattner77b89b82010-06-27 07:15:29 +00002008 llvm::Type *PtrTy = llvm::PointerType::getUnqual(Int64Ty);
2009 llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Nate Begemane7722102008-04-14 04:49:57 +00002011 // cast val v2i64
2012 Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast");
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Nate Begemane7722102008-04-14 04:49:57 +00002014 // extract (0, 1)
2015 unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1;
Chris Lattner77b89b82010-06-27 07:15:29 +00002016 llvm::Value *Idx = llvm::ConstantInt::get(Int32Ty, Index);
Nate Begemane7722102008-04-14 04:49:57 +00002017 Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract");
2018
2019 // cast pointer to i64 & store
2020 Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy);
2021 return Builder.CreateStore(Ops[1], Ops[0]);
2022 }
Bill Wendling28cab382010-09-28 01:28:56 +00002023 case X86::BI__builtin_ia32_palignr: {
2024 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
2025
2026 // If palignr is shifting the pair of input vectors less than 9 bytes,
2027 // emit a shuffle instruction.
2028 if (shiftVal <= 8) {
2029 llvm::SmallVector<llvm::Constant*, 8> Indices;
2030 for (unsigned i = 0; i != 8; ++i)
2031 Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i));
2032
2033 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
2034 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
2035 }
2036
2037 // If palignr is shifting the pair of input vectors more than 8 but less
2038 // than 16 bytes, emit a logical right shift of the destination.
2039 if (shiftVal < 16) {
2040 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
2041 const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 1);
2042
2043 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
2044 Ops[1] = llvm::ConstantInt::get(VecTy, (shiftVal-8) * 8);
2045
2046 // create i32 constant
2047 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_mmx_psrl_q);
2048 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
2049 }
2050
2051 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
2052 return llvm::Constant::getNullValue(ConvertType(E->getType()));
2053 }
Nate Begemanc3420ff2009-12-14 05:15:02 +00002054 case X86::BI__builtin_ia32_palignr128: {
Nate Begemance5818a2009-12-14 04:57:03 +00002055 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
2056
2057 // If palignr is shifting the pair of input vectors less than 17 bytes,
2058 // emit a shuffle instruction.
2059 if (shiftVal <= 16) {
Nate Begemance5818a2009-12-14 04:57:03 +00002060 llvm::SmallVector<llvm::Constant*, 16> Indices;
2061 for (unsigned i = 0; i != 16; ++i)
Chris Lattner77b89b82010-06-27 07:15:29 +00002062 Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i));
Nate Begemance5818a2009-12-14 04:57:03 +00002063
2064 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
2065 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
2066 }
2067
2068 // If palignr is shifting the pair of input vectors more than 16 but less
2069 // than 32 bytes, emit a logical right shift of the destination.
2070 if (shiftVal < 32) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002071 const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2);
Nate Begemance5818a2009-12-14 04:57:03 +00002072
2073 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
Chris Lattner77b89b82010-06-27 07:15:29 +00002074 Ops[1] = llvm::ConstantInt::get(Int32Ty, (shiftVal-16) * 8);
Nate Begemance5818a2009-12-14 04:57:03 +00002075
2076 // create i32 constant
2077 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq);
2078 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
2079 }
2080
2081 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
2082 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Eric Christopher91b59272009-12-01 05:00:51 +00002083 }
Anders Carlsson564f1de2007-12-09 23:17:02 +00002084 }
2085}
2086
Mike Stump1eb44332009-09-09 15:08:12 +00002087Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +00002088 const CallExpr *E) {
Chris Lattnerdd173942010-04-14 03:54:58 +00002089 llvm::SmallVector<Value*, 4> Ops;
2090
2091 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
2092 Ops.push_back(EmitScalarExpr(E->getArg(i)));
2093
2094 Intrinsic::ID ID = Intrinsic::not_intrinsic;
2095
2096 switch (BuiltinID) {
2097 default: return 0;
2098
Anton Korobeynikov4d3a7b02010-06-19 09:47:18 +00002099 // vec_ld, vec_lvsl, vec_lvsr
2100 case PPC::BI__builtin_altivec_lvx:
2101 case PPC::BI__builtin_altivec_lvxl:
2102 case PPC::BI__builtin_altivec_lvebx:
2103 case PPC::BI__builtin_altivec_lvehx:
2104 case PPC::BI__builtin_altivec_lvewx:
2105 case PPC::BI__builtin_altivec_lvsl:
2106 case PPC::BI__builtin_altivec_lvsr:
2107 {
2108 Ops[1] = Builder.CreateBitCast(Ops[1], llvm::Type::getInt8PtrTy(VMContext));
2109
2110 Ops[0] = Builder.CreateGEP(Ops[1], Ops[0], "tmp");
2111 Ops.pop_back();
2112
2113 switch (BuiltinID) {
2114 default: assert(0 && "Unsupported ld/lvsl/lvsr intrinsic!");
2115 case PPC::BI__builtin_altivec_lvx:
2116 ID = Intrinsic::ppc_altivec_lvx;
2117 break;
2118 case PPC::BI__builtin_altivec_lvxl:
2119 ID = Intrinsic::ppc_altivec_lvxl;
2120 break;
2121 case PPC::BI__builtin_altivec_lvebx:
2122 ID = Intrinsic::ppc_altivec_lvebx;
2123 break;
2124 case PPC::BI__builtin_altivec_lvehx:
2125 ID = Intrinsic::ppc_altivec_lvehx;
2126 break;
2127 case PPC::BI__builtin_altivec_lvewx:
2128 ID = Intrinsic::ppc_altivec_lvewx;
2129 break;
2130 case PPC::BI__builtin_altivec_lvsl:
2131 ID = Intrinsic::ppc_altivec_lvsl;
2132 break;
2133 case PPC::BI__builtin_altivec_lvsr:
2134 ID = Intrinsic::ppc_altivec_lvsr;
2135 break;
2136 }
2137 llvm::Function *F = CGM.getIntrinsic(ID);
2138 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "");
2139 }
2140
Chris Lattnerdd173942010-04-14 03:54:58 +00002141 // vec_st
2142 case PPC::BI__builtin_altivec_stvx:
2143 case PPC::BI__builtin_altivec_stvxl:
2144 case PPC::BI__builtin_altivec_stvebx:
2145 case PPC::BI__builtin_altivec_stvehx:
2146 case PPC::BI__builtin_altivec_stvewx:
2147 {
2148 Ops[2] = Builder.CreateBitCast(Ops[2], llvm::Type::getInt8PtrTy(VMContext));
Anton Korobeynikov4d3a7b02010-06-19 09:47:18 +00002149 Ops[1] = Builder.CreateGEP(Ops[2], Ops[1], "tmp");
Chris Lattnerdd173942010-04-14 03:54:58 +00002150 Ops.pop_back();
2151
2152 switch (BuiltinID) {
Anton Korobeynikov4d3a7b02010-06-19 09:47:18 +00002153 default: assert(0 && "Unsupported st intrinsic!");
Chris Lattnerdd173942010-04-14 03:54:58 +00002154 case PPC::BI__builtin_altivec_stvx:
2155 ID = Intrinsic::ppc_altivec_stvx;
2156 break;
2157 case PPC::BI__builtin_altivec_stvxl:
2158 ID = Intrinsic::ppc_altivec_stvxl;
2159 break;
2160 case PPC::BI__builtin_altivec_stvebx:
2161 ID = Intrinsic::ppc_altivec_stvebx;
2162 break;
2163 case PPC::BI__builtin_altivec_stvehx:
2164 ID = Intrinsic::ppc_altivec_stvehx;
2165 break;
2166 case PPC::BI__builtin_altivec_stvewx:
2167 ID = Intrinsic::ppc_altivec_stvewx;
2168 break;
2169 }
2170 llvm::Function *F = CGM.getIntrinsic(ID);
2171 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "");
2172 }
2173 }
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002174 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002175}