blob: df6ac7fc243fc4c35ad7cc534c47439b56f43e3b [file] [log] [blame]
Anders Carlsson022012e2007-08-20 18:05:56 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Anders Carlsson022012e2007-08-20 18:05:56 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Builtin calls as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
John McCalld0b76ca2010-03-02 03:50:12 +000014#include "TargetInfo.h"
Anders Carlsson022012e2007-08-20 18:05:56 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner1f329992008-10-06 06:09:18 +000018#include "clang/AST/APValue.h"
Chris Lattnerbef20ac2007-08-31 04:31:45 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000020#include "clang/AST/Decl.h"
Chris Lattner6b15cdc2009-06-14 01:05:48 +000021#include "clang/Basic/TargetBuiltins.h"
Anders Carlsson793680e2007-10-12 23:56:29 +000022#include "llvm/Intrinsics.h"
John McCalld0b76ca2010-03-02 03:50:12 +000023#include "llvm/Target/TargetData.h"
Anders Carlsson022012e2007-08-20 18:05:56 +000024using namespace clang;
25using namespace CodeGen;
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000026using namespace llvm;
27
Daniel Dunbar0002d232009-04-07 00:55:51 +000028/// Utility to insert an atomic instruction based on Instrinsic::ID
29/// and the expression node.
Mike Stump1eb44332009-09-09 15:08:12 +000030static RValue EmitBinaryAtomic(CodeGenFunction& CGF,
Mon P Wang1ffe2812008-05-09 22:40:52 +000031 Intrinsic::ID Id, const CallExpr *E) {
Mon P Wangc5004512008-07-31 03:28:23 +000032 const llvm::Type *ResType[2];
Daniel Dunbar0002d232009-04-07 00:55:51 +000033 ResType[0] = CGF.ConvertType(E->getType());
34 ResType[1] = CGF.ConvertType(E->getArg(0)->getType());
35 Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2);
Mike Stump1eb44332009-09-09 15:08:12 +000036 return RValue::get(CGF.Builder.CreateCall2(AtomF,
37 CGF.EmitScalarExpr(E->getArg(0)),
Daniel Dunbar0002d232009-04-07 00:55:51 +000038 CGF.EmitScalarExpr(E->getArg(1))));
39}
40
41/// Utility to insert an atomic instruction based Instrinsic::ID and
42// the expression node, where the return value is the result of the
43// operation.
Mike Stump1eb44332009-09-09 15:08:12 +000044static RValue EmitBinaryAtomicPost(CodeGenFunction& CGF,
Daniel Dunbar0002d232009-04-07 00:55:51 +000045 Intrinsic::ID Id, const CallExpr *E,
46 Instruction::BinaryOps Op) {
47 const llvm::Type *ResType[2];
48 ResType[0] = CGF.ConvertType(E->getType());
49 ResType[1] = CGF.ConvertType(E->getArg(0)->getType());
50 Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2);
51 Value *Ptr = CGF.EmitScalarExpr(E->getArg(0));
52 Value *Operand = CGF.EmitScalarExpr(E->getArg(1));
53 Value *Result = CGF.Builder.CreateCall2(AtomF, Ptr, Operand);
Mike Stump1eb44332009-09-09 15:08:12 +000054
Chris Lattnereebd9d22009-05-13 04:37:52 +000055 if (Id == Intrinsic::atomic_load_nand)
56 Result = CGF.Builder.CreateNot(Result);
Mike Stump1eb44332009-09-09 15:08:12 +000057
58
Daniel Dunbar0002d232009-04-07 00:55:51 +000059 return RValue::get(CGF.Builder.CreateBinOp(Op, Result, Operand));
Mon P Wang1ffe2812008-05-09 22:40:52 +000060}
61
John McCallfb17a562010-03-03 10:30:05 +000062static llvm::ConstantInt *getInt32(llvm::LLVMContext &Context, int32_t Value) {
63 return llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), Value);
64}
65
Mike Stump1eb44332009-09-09 15:08:12 +000066RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
Daniel Dunbaref2abfe2009-02-16 22:43:43 +000067 unsigned BuiltinID, const CallExpr *E) {
Chris Lattner564ea2a2008-10-06 06:56:41 +000068 // See if we can constant fold this builtin. If so, don't emit it at all.
Anders Carlssonf35d35a2008-12-01 02:31:41 +000069 Expr::EvalResult Result;
Chris Lattner6ee7aa12008-11-16 21:24:15 +000070 if (E->Evaluate(Result, CGM.getContext())) {
Anders Carlssonf35d35a2008-12-01 02:31:41 +000071 if (Result.Val.isInt())
Mike Stump1eb44332009-09-09 15:08:12 +000072 return RValue::get(llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +000073 Result.Val.getInt()));
Eli Friedman3941b182009-01-25 01:54:01 +000074 else if (Result.Val.isFloat())
Owen Andersonbc0a2222009-07-27 21:00:51 +000075 return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat()));
Chris Lattner1f329992008-10-06 06:09:18 +000076 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattner564ea2a2008-10-06 06:56:41 +000078 switch (BuiltinID) {
79 default: break; // Handle intrinsics and libm functions below.
Chris Lattner506ff882008-10-06 07:26:43 +000080 case Builtin::BI__builtin___CFStringMakeConstantString:
David Chisnall0d13f6f2010-01-23 02:40:42 +000081 case Builtin::BI__builtin___NSStringMakeConstantString:
Anders Carlssone9352cc2009-04-08 04:48:15 +000082 return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0));
Chris Lattner6a705f02008-07-09 17:28:44 +000083 case Builtin::BI__builtin_stdarg_start:
Anders Carlsson793680e2007-10-12 23:56:29 +000084 case Builtin::BI__builtin_va_start:
85 case Builtin::BI__builtin_va_end: {
Daniel Dunbar07855702009-02-11 22:25:55 +000086 Value *ArgValue = EmitVAListRef(E->getArg(0));
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +000087 const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson793680e2007-10-12 23:56:29 +000088 if (ArgValue->getType() != DestType)
Mike Stump1eb44332009-09-09 15:08:12 +000089 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
Daniel Dunbarb27ffbe2009-07-26 09:28:40 +000090 ArgValue->getName().data());
Anders Carlsson793680e2007-10-12 23:56:29 +000091
Mike Stump1eb44332009-09-09 15:08:12 +000092 Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ?
Chris Lattner6a705f02008-07-09 17:28:44 +000093 Intrinsic::vaend : Intrinsic::vastart;
Chris Lattner7acda7c2007-12-18 00:25:38 +000094 return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue));
Anders Carlsson793680e2007-10-12 23:56:29 +000095 }
Anders Carlssona28ef8b2008-02-09 20:26:43 +000096 case Builtin::BI__builtin_va_copy: {
Eli Friedman4fd0aa52009-01-20 17:46:04 +000097 Value *DstPtr = EmitVAListRef(E->getArg(0));
98 Value *SrcPtr = EmitVAListRef(E->getArg(1));
Anders Carlssona28ef8b2008-02-09 20:26:43 +000099
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000100 const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000101
102 DstPtr = Builder.CreateBitCast(DstPtr, Type);
103 SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000104 return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000105 DstPtr, SrcPtr));
Anders Carlssona28ef8b2008-02-09 20:26:43 +0000106 }
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000107 case Builtin::BI__builtin_abs: {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Value *ArgValue = EmitScalarExpr(E->getArg(0));
109
Chris Lattner9a847f52008-07-23 06:53:34 +0000110 Value *NegOp = Builder.CreateNeg(ArgValue, "neg");
Mike Stump1eb44332009-09-09 15:08:12 +0000111 Value *CmpResult =
112 Builder.CreateICmpSGE(ArgValue,
Owen Andersonc9c88b42009-07-31 20:28:54 +0000113 llvm::Constant::getNullValue(ArgValue->getType()),
Chris Lattner9a847f52008-07-23 06:53:34 +0000114 "abscond");
Mike Stump1eb44332009-09-09 15:08:12 +0000115 Value *Result =
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000116 Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000118 return RValue::get(Result);
119 }
Anders Carlsson3a31d602008-02-06 07:19:27 +0000120 case Builtin::BI__builtin_ctz:
121 case Builtin::BI__builtin_ctzl:
122 case Builtin::BI__builtin_ctzll: {
123 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Anders Carlsson3a31d602008-02-06 07:19:27 +0000125 const llvm::Type *ArgType = ArgValue->getType();
126 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
127
Mike Stump1eb44332009-09-09 15:08:12 +0000128 const llvm::Type *ResultType = ConvertType(E->getType());
Anders Carlsson3a31d602008-02-06 07:19:27 +0000129 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
130 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000131 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
132 "cast");
Anders Carlsson3a31d602008-02-06 07:19:27 +0000133 return RValue::get(Result);
134 }
Eli Friedmanf4e85332008-05-27 15:32:46 +0000135 case Builtin::BI__builtin_clz:
136 case Builtin::BI__builtin_clzl:
137 case Builtin::BI__builtin_clzll: {
138 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Eli Friedmanf4e85332008-05-27 15:32:46 +0000140 const llvm::Type *ArgType = ArgValue->getType();
141 Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1);
142
Mike Stump1eb44332009-09-09 15:08:12 +0000143 const llvm::Type *ResultType = ConvertType(E->getType());
Eli Friedmanf4e85332008-05-27 15:32:46 +0000144 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
145 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000146 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
147 "cast");
Eli Friedmanf4e85332008-05-27 15:32:46 +0000148 return RValue::get(Result);
149 }
Daniel Dunbar04b29002008-07-21 17:19:41 +0000150 case Builtin::BI__builtin_ffs:
151 case Builtin::BI__builtin_ffsl:
152 case Builtin::BI__builtin_ffsll: {
153 // ffs(x) -> x ? cttz(x) + 1 : 0
154 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbar04b29002008-07-21 17:19:41 +0000156 const llvm::Type *ArgType = ArgValue->getType();
157 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Daniel Dunbar04b29002008-07-21 17:19:41 +0000159 const llvm::Type *ResultType = ConvertType(E->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000160 Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"),
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000161 llvm::ConstantInt::get(ArgType, 1), "tmp");
Owen Andersonc9c88b42009-07-31 20:28:54 +0000162 Value *Zero = llvm::Constant::getNullValue(ArgType);
Daniel Dunbar04b29002008-07-21 17:19:41 +0000163 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
164 Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
165 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000166 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
167 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000168 return RValue::get(Result);
169 }
170 case Builtin::BI__builtin_parity:
171 case Builtin::BI__builtin_parityl:
172 case Builtin::BI__builtin_parityll: {
173 // parity(x) -> ctpop(x) & 1
174 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Daniel Dunbar04b29002008-07-21 17:19:41 +0000176 const llvm::Type *ArgType = ArgValue->getType();
177 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Daniel Dunbar04b29002008-07-21 17:19:41 +0000179 const llvm::Type *ResultType = ConvertType(E->getType());
180 Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000181 Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1),
Daniel Dunbar04b29002008-07-21 17:19:41 +0000182 "tmp");
183 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000184 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
185 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000186 return RValue::get(Result);
187 }
188 case Builtin::BI__builtin_popcount:
189 case Builtin::BI__builtin_popcountl:
190 case Builtin::BI__builtin_popcountll: {
191 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Daniel Dunbar04b29002008-07-21 17:19:41 +0000193 const llvm::Type *ArgType = ArgValue->getType();
194 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Daniel Dunbar04b29002008-07-21 17:19:41 +0000196 const llvm::Type *ResultType = ConvertType(E->getType());
197 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
198 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000199 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
200 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000201 return RValue::get(Result);
202 }
Chris Lattner1feedd82007-12-13 07:34:23 +0000203 case Builtin::BI__builtin_expect:
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000204 // FIXME: pass expect through to LLVM
Chris Lattner1feedd82007-12-13 07:34:23 +0000205 return RValue::get(EmitScalarExpr(E->getArg(0)));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000206 case Builtin::BI__builtin_bswap32:
207 case Builtin::BI__builtin_bswap64: {
Chris Lattner1feedd82007-12-13 07:34:23 +0000208 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000209 const llvm::Type *ArgType = ArgValue->getType();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000210 Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
Chris Lattner1feedd82007-12-13 07:34:23 +0000211 return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
Mike Stump1eb44332009-09-09 15:08:12 +0000212 }
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000213 case Builtin::BI__builtin_object_size: {
Mike Stumpb16d32f2009-10-26 23:39:48 +0000214 // We pass this builtin onto the optimizer so that it can
215 // figure out the object size in more complex cases.
Mike Stumpc4c90452009-10-27 22:09:17 +0000216 const llvm::Type *ResType[] = {
217 ConvertType(E->getType())
218 };
Eric Christopherfee667f2009-12-23 03:49:37 +0000219
220 // LLVM only supports 0 and 2, make sure that we pass along that
221 // as a boolean.
222 Value *Ty = EmitScalarExpr(E->getArg(1));
223 ConstantInt *CI = dyn_cast<ConstantInt>(Ty);
224 assert(CI);
225 uint64_t val = CI->getZExtValue();
226 CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1);
227
Mike Stumpc4c90452009-10-27 22:09:17 +0000228 Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1);
229 return RValue::get(Builder.CreateCall2(F,
230 EmitScalarExpr(E->getArg(0)),
Eric Christopherfee667f2009-12-23 03:49:37 +0000231 CI));
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000232 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000233 case Builtin::BI__builtin_prefetch: {
234 Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
235 // FIXME: Technically these constants should of type 'int', yes?
Mike Stump1eb44332009-09-09 15:08:12 +0000236 RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
Owen Anderson0032b272009-08-13 21:57:51 +0000237 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000238 Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
Owen Anderson0032b272009-08-13 21:57:51 +0000239 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 3);
Daniel Dunbar4493f792008-07-21 22:59:13 +0000240 Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0);
241 return RValue::get(Builder.CreateCall3(F, Address, RW, Locality));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000242 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000243 case Builtin::BI__builtin_trap: {
244 Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0);
245 return RValue::get(Builder.CreateCall(F));
246 }
Chris Lattner21190d52009-09-21 03:09:59 +0000247 case Builtin::BI__builtin_unreachable: {
Mike Stumpfba565d2009-12-16 03:07:12 +0000248 if (CatchUndefined && HaveInsertPoint())
249 EmitBranch(getTrapBB());
Chris Lattner21190d52009-09-21 03:09:59 +0000250 Value *V = Builder.CreateUnreachable();
251 Builder.ClearInsertionPoint();
252 return RValue::get(V);
253 }
254
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000255 case Builtin::BI__builtin_powi:
256 case Builtin::BI__builtin_powif:
257 case Builtin::BI__builtin_powil: {
258 Value *Base = EmitScalarExpr(E->getArg(0));
259 Value *Exponent = EmitScalarExpr(E->getArg(1));
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000260 const llvm::Type *ArgType = Base->getType();
261 Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1);
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000262 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
263 }
264
Chris Lattnerfe23e212007-12-20 00:44:32 +0000265 case Builtin::BI__builtin_isgreater:
266 case Builtin::BI__builtin_isgreaterequal:
267 case Builtin::BI__builtin_isless:
268 case Builtin::BI__builtin_islessequal:
269 case Builtin::BI__builtin_islessgreater:
270 case Builtin::BI__builtin_isunordered: {
271 // Ordered comparisons: we know the arguments to these are matching scalar
272 // floating point values.
Mike Stump1eb44332009-09-09 15:08:12 +0000273 Value *LHS = EmitScalarExpr(E->getArg(0));
Chris Lattnerfe23e212007-12-20 00:44:32 +0000274 Value *RHS = EmitScalarExpr(E->getArg(1));
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattnerfe23e212007-12-20 00:44:32 +0000276 switch (BuiltinID) {
277 default: assert(0 && "Unknown ordered comparison");
278 case Builtin::BI__builtin_isgreater:
279 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
280 break;
281 case Builtin::BI__builtin_isgreaterequal:
282 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
283 break;
284 case Builtin::BI__builtin_isless:
285 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
286 break;
287 case Builtin::BI__builtin_islessequal:
288 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
289 break;
290 case Builtin::BI__builtin_islessgreater:
291 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
292 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000293 case Builtin::BI__builtin_isunordered:
Chris Lattnerfe23e212007-12-20 00:44:32 +0000294 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
295 break;
296 }
297 // ZExt bool to int type.
298 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()),
299 "tmp"));
300 }
Eli Friedmand6139892009-09-01 04:19:44 +0000301 case Builtin::BI__builtin_isnan: {
302 Value *V = EmitScalarExpr(E->getArg(0));
303 V = Builder.CreateFCmpUNO(V, V, "cmp");
304 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp"));
305 }
Eli Friedmanb52fe9c2009-06-02 07:10:30 +0000306 case Builtin::BIalloca:
Chris Lattner9e800e32008-06-16 17:15:14 +0000307 case Builtin::BI__builtin_alloca: {
308 // FIXME: LLVM IR Should allow alloca with an i64 size!
309 Value *Size = EmitScalarExpr(E->getArg(0));
Owen Anderson0032b272009-08-13 21:57:51 +0000310 Size = Builder.CreateIntCast(Size, llvm::Type::getInt32Ty(VMContext), false, "tmp");
311 return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp"));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000312 }
Eli Friedmane6dddfd2010-01-23 19:00:10 +0000313 case Builtin::BIbzero:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000314 case Builtin::BI__builtin_bzero: {
315 Value *Address = EmitScalarExpr(E->getArg(0));
316 Builder.CreateCall4(CGM.getMemSetFn(), Address,
Owen Anderson0032b272009-08-13 21:57:51 +0000317 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0),
Daniel Dunbar1caae952008-07-22 00:26:45 +0000318 EmitScalarExpr(E->getArg(1)),
Owen Anderson0032b272009-08-13 21:57:51 +0000319 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000320 return RValue::get(Address);
Chris Lattner9e800e32008-06-16 17:15:14 +0000321 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000322 case Builtin::BImemcpy:
Eli Friedmand4b32e42008-05-19 23:27:48 +0000323 case Builtin::BI__builtin_memcpy: {
Daniel Dunbar1caae952008-07-22 00:26:45 +0000324 Value *Address = EmitScalarExpr(E->getArg(0));
325 Builder.CreateCall4(CGM.getMemCpyFn(), Address,
326 EmitScalarExpr(E->getArg(1)),
327 EmitScalarExpr(E->getArg(2)),
Owen Anderson0032b272009-08-13 21:57:51 +0000328 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000329 return RValue::get(Address);
330 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000331 case Builtin::BImemmove:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000332 case Builtin::BI__builtin_memmove: {
333 Value *Address = EmitScalarExpr(E->getArg(0));
334 Builder.CreateCall4(CGM.getMemMoveFn(), Address,
335 EmitScalarExpr(E->getArg(1)),
336 EmitScalarExpr(E->getArg(2)),
Owen Anderson0032b272009-08-13 21:57:51 +0000337 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000338 return RValue::get(Address);
339 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000340 case Builtin::BImemset:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000341 case Builtin::BI__builtin_memset: {
342 Value *Address = EmitScalarExpr(E->getArg(0));
343 Builder.CreateCall4(CGM.getMemSetFn(), Address,
Daniel Dunbar62c29c62009-02-16 21:52:05 +0000344 Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
Owen Anderson0032b272009-08-13 21:57:51 +0000345 llvm::Type::getInt8Ty(VMContext)),
Daniel Dunbar1caae952008-07-22 00:26:45 +0000346 EmitScalarExpr(E->getArg(2)),
Owen Anderson0032b272009-08-13 21:57:51 +0000347 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000348 return RValue::get(Address);
Eli Friedmand4b32e42008-05-19 23:27:48 +0000349 }
John McCallfb17a562010-03-03 10:30:05 +0000350 case Builtin::BI__builtin_dwarf_cfa: {
351 // The offset in bytes from the first argument to the CFA.
352 //
353 // Why on earth is this in the frontend? Is there any reason at
354 // all that the backend can't reasonably determine this while
355 // lowering llvm.eh.dwarf.cfa()?
356 //
357 // TODO: If there's a satisfactory reason, add a target hook for
358 // this instead of hard-coding 0, which is correct for most targets.
359 int32_t Offset = 0;
360
361 Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa, 0, 0);
362 return RValue::get(Builder.CreateCall(F, getInt32(VMContext, Offset)));
363 }
Eli Friedman256f77e2008-05-20 08:59:34 +0000364 case Builtin::BI__builtin_return_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000365 Value *Depth = EmitScalarExpr(E->getArg(0));
366 Depth = Builder.CreateIntCast(Depth,
367 llvm::Type::getInt32Ty(VMContext),
368 false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000369 Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000370 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000371 }
372 case Builtin::BI__builtin_frame_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000373 Value *Depth = EmitScalarExpr(E->getArg(0));
374 Depth = Builder.CreateIntCast(Depth,
375 llvm::Type::getInt32Ty(VMContext),
376 false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000377 Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000378 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000379 }
Eli Friedman3b660ef2009-05-03 19:23:23 +0000380 case Builtin::BI__builtin_extract_return_addr: {
John McCall492c4f92010-03-03 04:15:11 +0000381 Value *Address = EmitScalarExpr(E->getArg(0));
382 Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);
383 return RValue::get(Result);
384 }
385 case Builtin::BI__builtin_frob_return_addr: {
386 Value *Address = EmitScalarExpr(E->getArg(0));
387 Value *Result = getTargetHooks().encodeReturnAddress(*this, Address);
388 return RValue::get(Result);
Eli Friedman3b660ef2009-05-03 19:23:23 +0000389 }
John McCall7ada1112010-03-03 05:38:58 +0000390 case Builtin::BI__builtin_eh_return: {
391 Value *Int = EmitScalarExpr(E->getArg(0));
392 Value *Ptr = EmitScalarExpr(E->getArg(1));
393
394 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType());
395 assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) &&
396 "LLVM's __builtin_eh_return only supports 32- and 64-bit variants");
397 Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32
398 ? Intrinsic::eh_return_i32
399 : Intrinsic::eh_return_i64,
400 0, 0);
401 Builder.CreateCall2(F, Int, Ptr);
402 Value *V = Builder.CreateUnreachable();
403 Builder.ClearInsertionPoint();
404 return RValue::get(V);
405 }
Eli Friedmana6d75c02009-06-02 09:37:50 +0000406 case Builtin::BI__builtin_unwind_init: {
407 Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0);
408 return RValue::get(Builder.CreateCall(F));
409 }
John McCall5e110852010-03-02 02:31:24 +0000410 case Builtin::BI__builtin_extend_pointer: {
411 // Extends a pointer to the size of an _Unwind_Word, which is
John McCalld0b76ca2010-03-02 03:50:12 +0000412 // uint64_t on all platforms. Generally this gets poked into a
413 // register and eventually used as an address, so if the
414 // addressing registers are wider than pointers and the platform
415 // doesn't implicitly ignore high-order bits when doing
416 // addressing, we need to make sure we zext / sext based on
417 // the platform's expectations.
John McCall5e110852010-03-02 02:31:24 +0000418 //
419 // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html
John McCalld0b76ca2010-03-02 03:50:12 +0000420
421 LLVMContext &C = CGM.getLLVMContext();
422
423 // Cast the pointer to intptr_t.
John McCall5e110852010-03-02 02:31:24 +0000424 Value *Ptr = EmitScalarExpr(E->getArg(0));
John McCalld0b76ca2010-03-02 03:50:12 +0000425 const llvm::IntegerType *IntPtrTy = CGM.getTargetData().getIntPtrType(C);
426 Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast");
427
428 // If that's 64 bits, we're done.
429 if (IntPtrTy->getBitWidth() == 64)
430 return RValue::get(Result);
431
432 // Otherwise, ask the codegen data what to do.
433 const llvm::IntegerType *Int64Ty = llvm::IntegerType::get(C, 64);
John McCall492c4f92010-03-03 04:15:11 +0000434 if (getTargetHooks().extendPointerWithSExt())
John McCalld0b76ca2010-03-02 03:50:12 +0000435 return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext"));
436 else
437 return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext"));
John McCall5e110852010-03-02 02:31:24 +0000438 }
Eli Friedmana6d75c02009-06-02 09:37:50 +0000439#if 0
440 // FIXME: Finish/enable when LLVM backend support stabilizes
441 case Builtin::BI__builtin_setjmp: {
442 Value *Buf = EmitScalarExpr(E->getArg(0));
443 // Store the frame pointer to the buffer
444 Value *FrameAddrF = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0);
445 Value *FrameAddr =
446 Builder.CreateCall(FrameAddrF,
Owen Anderson0032b272009-08-13 21:57:51 +0000447 Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)));
Eli Friedmana6d75c02009-06-02 09:37:50 +0000448 Builder.CreateStore(FrameAddr, Buf);
449 // Call the setjmp intrinsic
450 Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp, 0, 0);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000451 const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext);
Eli Friedmana6d75c02009-06-02 09:37:50 +0000452 Buf = Builder.CreateBitCast(Buf, DestType);
453 return RValue::get(Builder.CreateCall(F, Buf));
454 }
455 case Builtin::BI__builtin_longjmp: {
456 Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp, 0, 0);
457 Value *Buf = EmitScalarExpr(E->getArg(0));
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000458 const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext);
Eli Friedmana6d75c02009-06-02 09:37:50 +0000459 Buf = Builder.CreateBitCast(Buf, DestType);
460 return RValue::get(Builder.CreateCall(F, Buf));
461 }
462#endif
Mon P Wang1ffe2812008-05-09 22:40:52 +0000463 case Builtin::BI__sync_fetch_and_add:
Mon P Wang1ffe2812008-05-09 22:40:52 +0000464 case Builtin::BI__sync_fetch_and_sub:
Chris Lattner5caa3702009-05-08 06:58:22 +0000465 case Builtin::BI__sync_fetch_and_or:
466 case Builtin::BI__sync_fetch_and_and:
467 case Builtin::BI__sync_fetch_and_xor:
468 case Builtin::BI__sync_add_and_fetch:
469 case Builtin::BI__sync_sub_and_fetch:
470 case Builtin::BI__sync_and_and_fetch:
471 case Builtin::BI__sync_or_and_fetch:
472 case Builtin::BI__sync_xor_and_fetch:
473 case Builtin::BI__sync_val_compare_and_swap:
474 case Builtin::BI__sync_bool_compare_and_swap:
475 case Builtin::BI__sync_lock_test_and_set:
476 case Builtin::BI__sync_lock_release:
477 assert(0 && "Shouldn't make it through sema");
478 case Builtin::BI__sync_fetch_and_add_1:
479 case Builtin::BI__sync_fetch_and_add_2:
480 case Builtin::BI__sync_fetch_and_add_4:
481 case Builtin::BI__sync_fetch_and_add_8:
482 case Builtin::BI__sync_fetch_and_add_16:
483 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E);
484 case Builtin::BI__sync_fetch_and_sub_1:
485 case Builtin::BI__sync_fetch_and_sub_2:
486 case Builtin::BI__sync_fetch_and_sub_4:
487 case Builtin::BI__sync_fetch_and_sub_8:
488 case Builtin::BI__sync_fetch_and_sub_16:
Mon P Wang09b6bf52008-06-25 08:21:36 +0000489 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E);
Chris Lattner5caa3702009-05-08 06:58:22 +0000490 case Builtin::BI__sync_fetch_and_or_1:
491 case Builtin::BI__sync_fetch_and_or_2:
492 case Builtin::BI__sync_fetch_and_or_4:
493 case Builtin::BI__sync_fetch_and_or_8:
494 case Builtin::BI__sync_fetch_and_or_16:
495 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E);
496 case Builtin::BI__sync_fetch_and_and_1:
497 case Builtin::BI__sync_fetch_and_and_2:
498 case Builtin::BI__sync_fetch_and_and_4:
499 case Builtin::BI__sync_fetch_and_and_8:
500 case Builtin::BI__sync_fetch_and_and_16:
501 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E);
502 case Builtin::BI__sync_fetch_and_xor_1:
503 case Builtin::BI__sync_fetch_and_xor_2:
504 case Builtin::BI__sync_fetch_and_xor_4:
505 case Builtin::BI__sync_fetch_and_xor_8:
506 case Builtin::BI__sync_fetch_and_xor_16:
507 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E);
Chris Lattnereebd9d22009-05-13 04:37:52 +0000508 case Builtin::BI__sync_fetch_and_nand_1:
509 case Builtin::BI__sync_fetch_and_nand_2:
510 case Builtin::BI__sync_fetch_and_nand_4:
511 case Builtin::BI__sync_fetch_and_nand_8:
512 case Builtin::BI__sync_fetch_and_nand_16:
513 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E);
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner5caa3702009-05-08 06:58:22 +0000515 // Clang extensions: not overloaded yet.
Mon P Wang1ffe2812008-05-09 22:40:52 +0000516 case Builtin::BI__sync_fetch_and_min:
517 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E);
518 case Builtin::BI__sync_fetch_and_max:
519 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E);
520 case Builtin::BI__sync_fetch_and_umin:
521 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E);
522 case Builtin::BI__sync_fetch_and_umax:
523 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000524
Chris Lattner5caa3702009-05-08 06:58:22 +0000525 case Builtin::BI__sync_add_and_fetch_1:
526 case Builtin::BI__sync_add_and_fetch_2:
527 case Builtin::BI__sync_add_and_fetch_4:
528 case Builtin::BI__sync_add_and_fetch_8:
529 case Builtin::BI__sync_add_and_fetch_16:
Mike Stump1eb44332009-09-09 15:08:12 +0000530 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E,
Daniel Dunbar0002d232009-04-07 00:55:51 +0000531 llvm::Instruction::Add);
Chris Lattner5caa3702009-05-08 06:58:22 +0000532 case Builtin::BI__sync_sub_and_fetch_1:
533 case Builtin::BI__sync_sub_and_fetch_2:
534 case Builtin::BI__sync_sub_and_fetch_4:
535 case Builtin::BI__sync_sub_and_fetch_8:
536 case Builtin::BI__sync_sub_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000537 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E,
538 llvm::Instruction::Sub);
Chris Lattner5caa3702009-05-08 06:58:22 +0000539 case Builtin::BI__sync_and_and_fetch_1:
540 case Builtin::BI__sync_and_and_fetch_2:
541 case Builtin::BI__sync_and_and_fetch_4:
542 case Builtin::BI__sync_and_and_fetch_8:
543 case Builtin::BI__sync_and_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000544 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E,
545 llvm::Instruction::And);
Chris Lattner5caa3702009-05-08 06:58:22 +0000546 case Builtin::BI__sync_or_and_fetch_1:
547 case Builtin::BI__sync_or_and_fetch_2:
548 case Builtin::BI__sync_or_and_fetch_4:
549 case Builtin::BI__sync_or_and_fetch_8:
550 case Builtin::BI__sync_or_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000551 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E,
552 llvm::Instruction::Or);
Chris Lattner5caa3702009-05-08 06:58:22 +0000553 case Builtin::BI__sync_xor_and_fetch_1:
554 case Builtin::BI__sync_xor_and_fetch_2:
555 case Builtin::BI__sync_xor_and_fetch_4:
556 case Builtin::BI__sync_xor_and_fetch_8:
557 case Builtin::BI__sync_xor_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000558 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E,
559 llvm::Instruction::Xor);
Chris Lattnereebd9d22009-05-13 04:37:52 +0000560 case Builtin::BI__sync_nand_and_fetch_1:
561 case Builtin::BI__sync_nand_and_fetch_2:
562 case Builtin::BI__sync_nand_and_fetch_4:
563 case Builtin::BI__sync_nand_and_fetch_8:
564 case Builtin::BI__sync_nand_and_fetch_16:
565 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E,
566 llvm::Instruction::And);
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattner5caa3702009-05-08 06:58:22 +0000568 case Builtin::BI__sync_val_compare_and_swap_1:
569 case Builtin::BI__sync_val_compare_and_swap_2:
570 case Builtin::BI__sync_val_compare_and_swap_4:
571 case Builtin::BI__sync_val_compare_and_swap_8:
572 case Builtin::BI__sync_val_compare_and_swap_16:
573 {
Mon P Wangc5004512008-07-31 03:28:23 +0000574 const llvm::Type *ResType[2];
575 ResType[0]= ConvertType(E->getType());
576 ResType[1] = ConvertType(E->getArg(0)->getType());
577 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2);
Mike Stump1eb44332009-09-09 15:08:12 +0000578 return RValue::get(Builder.CreateCall3(AtomF,
Anders Carlsson95844462008-08-30 16:17:45 +0000579 EmitScalarExpr(E->getArg(0)),
580 EmitScalarExpr(E->getArg(1)),
581 EmitScalarExpr(E->getArg(2))));
Anders Carlsson89799cf2007-10-29 02:59:40 +0000582 }
Daniel Dunbar0002d232009-04-07 00:55:51 +0000583
Chris Lattner5caa3702009-05-08 06:58:22 +0000584 case Builtin::BI__sync_bool_compare_and_swap_1:
585 case Builtin::BI__sync_bool_compare_and_swap_2:
586 case Builtin::BI__sync_bool_compare_and_swap_4:
587 case Builtin::BI__sync_bool_compare_and_swap_8:
588 case Builtin::BI__sync_bool_compare_and_swap_16:
589 {
Daniel Dunbar0002d232009-04-07 00:55:51 +0000590 const llvm::Type *ResType[2];
Chris Lattner5caa3702009-05-08 06:58:22 +0000591 ResType[0]= ConvertType(E->getArg(1)->getType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000592 ResType[1] = llvm::PointerType::getUnqual(ResType[0]);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000593 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2);
594 Value *OldVal = EmitScalarExpr(E->getArg(1));
Mike Stump1eb44332009-09-09 15:08:12 +0000595 Value *PrevVal = Builder.CreateCall3(AtomF,
Daniel Dunbar0002d232009-04-07 00:55:51 +0000596 EmitScalarExpr(E->getArg(0)),
597 OldVal,
598 EmitScalarExpr(E->getArg(2)));
599 Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal);
600 // zext bool to int.
601 return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
602 }
603
Chris Lattner5caa3702009-05-08 06:58:22 +0000604 case Builtin::BI__sync_lock_test_and_set_1:
605 case Builtin::BI__sync_lock_test_and_set_2:
606 case Builtin::BI__sync_lock_test_and_set_4:
607 case Builtin::BI__sync_lock_test_and_set_8:
608 case Builtin::BI__sync_lock_test_and_set_16:
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000609 return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E);
Chris Lattner5caa3702009-05-08 06:58:22 +0000610 case Builtin::BI__sync_lock_release_1:
611 case Builtin::BI__sync_lock_release_2:
612 case Builtin::BI__sync_lock_release_4:
613 case Builtin::BI__sync_lock_release_8:
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000614 case Builtin::BI__sync_lock_release_16: {
615 Value *Ptr = EmitScalarExpr(E->getArg(0));
616 const llvm::Type *ElTy =
617 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar007b5672009-11-29 21:11:47 +0000618 llvm::StoreInst *Store =
619 Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr);
620 Store->setVolatile(true);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000621 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000622 }
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000623
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000624 case Builtin::BI__sync_synchronize: {
625 Value *C[5];
Owen Anderson0032b272009-08-13 21:57:51 +0000626 C[0] = C[1] = C[2] = C[3] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 1);
627 C[4] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000628 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000629 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Tanya Lattner0b571642010-01-16 01:21:14 +0000632 case Builtin::BI__builtin_llvm_memory_barrier: {
633 Value *C[5] = {
634 EmitScalarExpr(E->getArg(0)),
635 EmitScalarExpr(E->getArg(1)),
636 EmitScalarExpr(E->getArg(2)),
637 EmitScalarExpr(E->getArg(3)),
638 EmitScalarExpr(E->getArg(4))
639 };
640 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5);
641 return RValue::get(0);
642 }
643
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000644 // Library functions with special handling.
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000645 case Builtin::BIsqrt:
646 case Builtin::BIsqrtf:
647 case Builtin::BIsqrtl: {
648 // Rewrite sqrt to intrinsic if allowed.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000649 if (!FD->hasAttr<ConstAttr>())
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000650 break;
651 Value *Arg0 = EmitScalarExpr(E->getArg(0));
652 const llvm::Type *ArgType = Arg0->getType();
653 Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1);
654 return RValue::get(Builder.CreateCall(F, Arg0, "tmp"));
655 }
656
657 case Builtin::BIpow:
658 case Builtin::BIpowf:
659 case Builtin::BIpowl: {
660 // Rewrite sqrt to intrinsic if allowed.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000661 if (!FD->hasAttr<ConstAttr>())
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000662 break;
663 Value *Base = EmitScalarExpr(E->getArg(0));
664 Value *Exponent = EmitScalarExpr(E->getArg(1));
665 const llvm::Type *ArgType = Base->getType();
666 Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1);
667 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
668 }
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000669 }
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000671 // If this is an alias for a libm function (e.g. __builtin_sin) turn it into
672 // that function.
Douglas Gregor3e41d602009-02-13 23:20:09 +0000673 if (getContext().BuiltinInfo.isLibFunction(BuiltinID) ||
674 getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
Anders Carlsson31777a22009-12-24 19:08:58 +0000675 return EmitCall(E->getCallee()->getType(),
676 CGM.getBuiltinLibFunction(FD, BuiltinID),
Anders Carlssond2490a92009-12-24 20:40:36 +0000677 ReturnValueSlot(),
Anders Carlsson31777a22009-12-24 19:08:58 +0000678 E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000680 // See if we have a target specific intrinsic.
Dale Johannesena6f80ef2009-02-05 01:50:47 +0000681 const char *Name = getContext().BuiltinInfo.GetName(BuiltinID);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000682 Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
683 if (const char *Prefix =
Mike Stump1eb44332009-09-09 15:08:12 +0000684 llvm::Triple::getArchTypePrefix(Target.getTriple().getArch()))
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000685 IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000687 if (IntrinsicID != Intrinsic::not_intrinsic) {
688 SmallVector<Value*, 16> Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000690 Function *F = CGM.getIntrinsic(IntrinsicID);
691 const llvm::FunctionType *FTy = F->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000693 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
694 Value *ArgValue = EmitScalarExpr(E->getArg(i));
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000696 // If the intrinsic arg type is different from the builtin arg type
697 // we need to do a bit cast.
698 const llvm::Type *PTy = FTy->getParamType(i);
699 if (PTy != ArgValue->getType()) {
700 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
701 "Must be able to losslessly bit cast to param");
702 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000705 Args.push_back(ArgValue);
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Jay Foadbeaaccd2009-05-21 09:52:38 +0000708 Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size());
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000709 QualType BuiltinRetType = E->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Owen Anderson0032b272009-08-13 21:57:51 +0000711 const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000712 if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType);
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000714 if (RetTy != V->getType()) {
715 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
716 "Must be able to losslessly bit cast result type");
717 V = Builder.CreateBitCast(V, RetTy);
718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000720 return RValue::get(V);
721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000723 // See if we have a target specific builtin that needs to be lowered.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000724 if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E))
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000725 return RValue::get(V);
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Daniel Dunbar488e9932008-08-16 00:56:44 +0000727 ErrorUnsupported(E, "builtin function");
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000729 // Unknown builtin, for now just dump it out and return undef.
730 if (hasAggregateLLVMType(E->getType()))
Daniel Dunbar195337d2010-02-09 02:48:28 +0000731 return RValue::getAggregate(CreateMemTemp(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +0000732 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
Mike Stump1eb44332009-09-09 15:08:12 +0000733}
Anders Carlsson564f1de2007-12-09 23:17:02 +0000734
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000735Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID,
736 const CallExpr *E) {
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000737 switch (Target.getTriple().getArch()) {
Chris Lattner2752c012010-03-03 19:03:45 +0000738 case llvm::Triple::arm:
739 case llvm::Triple::thumb:
740 return EmitARMBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000741 case llvm::Triple::x86:
742 case llvm::Triple::x86_64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000743 return EmitX86BuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000744 case llvm::Triple::ppc:
745 case llvm::Triple::ppc64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000746 return EmitPPCBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000747 default:
748 return 0;
749 }
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000750}
751
Chris Lattner2752c012010-03-03 19:03:45 +0000752Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
753 const CallExpr *E) {
754 switch (BuiltinID) {
755 default: return 0;
756
757 case ARM::BI__builtin_thread_pointer: {
758 Value *AtomF = CGM.getIntrinsic(Intrinsic::arm_thread_pointer, 0, 0);
759 return Builder.CreateCall(AtomF);
760 }
761 }
762}
763
Mike Stump1eb44332009-09-09 15:08:12 +0000764Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +0000765 const CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000767 llvm::SmallVector<Value*, 4> Ops;
768
769 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
770 Ops.push_back(EmitScalarExpr(E->getArg(i)));
771
Anders Carlsson564f1de2007-12-09 23:17:02 +0000772 switch (BuiltinID) {
Anders Carlsson46a26b02007-12-09 23:39:18 +0000773 default: return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000774 case X86::BI__builtin_ia32_pslldi128:
Nate Begemane7722102008-04-14 04:49:57 +0000775 case X86::BI__builtin_ia32_psllqi128:
Mike Stump1eb44332009-09-09 15:08:12 +0000776 case X86::BI__builtin_ia32_psllwi128:
Nate Begemane7722102008-04-14 04:49:57 +0000777 case X86::BI__builtin_ia32_psradi128:
778 case X86::BI__builtin_ia32_psrawi128:
779 case X86::BI__builtin_ia32_psrldi128:
780 case X86::BI__builtin_ia32_psrlqi128:
781 case X86::BI__builtin_ia32_psrlwi128: {
Owen Anderson0032b272009-08-13 21:57:51 +0000782 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext");
783 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 2);
784 llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Owen Anderson03e20502009-07-30 23:11:26 +0000785 Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty),
Nate Begemane7722102008-04-14 04:49:57 +0000786 Ops[1], Zero, "insert");
787 Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast");
788 const char *name = 0;
789 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Nate Begemane7722102008-04-14 04:49:57 +0000791 switch (BuiltinID) {
792 default: assert(0 && "Unsupported shift intrinsic!");
793 case X86::BI__builtin_ia32_pslldi128:
794 name = "pslldi";
795 ID = Intrinsic::x86_sse2_psll_d;
796 break;
797 case X86::BI__builtin_ia32_psllqi128:
798 name = "psllqi";
799 ID = Intrinsic::x86_sse2_psll_q;
800 break;
801 case X86::BI__builtin_ia32_psllwi128:
802 name = "psllwi";
803 ID = Intrinsic::x86_sse2_psll_w;
804 break;
805 case X86::BI__builtin_ia32_psradi128:
806 name = "psradi";
807 ID = Intrinsic::x86_sse2_psra_d;
808 break;
809 case X86::BI__builtin_ia32_psrawi128:
810 name = "psrawi";
811 ID = Intrinsic::x86_sse2_psra_w;
812 break;
813 case X86::BI__builtin_ia32_psrldi128:
814 name = "psrldi";
815 ID = Intrinsic::x86_sse2_psrl_d;
816 break;
817 case X86::BI__builtin_ia32_psrlqi128:
818 name = "psrlqi";
819 ID = Intrinsic::x86_sse2_psrl_q;
820 break;
821 case X86::BI__builtin_ia32_psrlwi128:
822 name = "psrlwi";
823 ID = Intrinsic::x86_sse2_psrl_w;
824 break;
825 }
826 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +0000827 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Nate Begemane7722102008-04-14 04:49:57 +0000828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829 case X86::BI__builtin_ia32_pslldi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000830 case X86::BI__builtin_ia32_psllqi:
Mike Stump1eb44332009-09-09 15:08:12 +0000831 case X86::BI__builtin_ia32_psllwi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000832 case X86::BI__builtin_ia32_psradi:
833 case X86::BI__builtin_ia32_psrawi:
834 case X86::BI__builtin_ia32_psrldi:
835 case X86::BI__builtin_ia32_psrlqi:
836 case X86::BI__builtin_ia32_psrlwi: {
Owen Anderson0032b272009-08-13 21:57:51 +0000837 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext");
838 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 1);
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000839 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000840 const char *name = 0;
841 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000843 switch (BuiltinID) {
844 default: assert(0 && "Unsupported shift intrinsic!");
845 case X86::BI__builtin_ia32_pslldi:
846 name = "pslldi";
847 ID = Intrinsic::x86_mmx_psll_d;
848 break;
849 case X86::BI__builtin_ia32_psllqi:
850 name = "psllqi";
851 ID = Intrinsic::x86_mmx_psll_q;
852 break;
853 case X86::BI__builtin_ia32_psllwi:
854 name = "psllwi";
855 ID = Intrinsic::x86_mmx_psll_w;
856 break;
857 case X86::BI__builtin_ia32_psradi:
858 name = "psradi";
859 ID = Intrinsic::x86_mmx_psra_d;
860 break;
861 case X86::BI__builtin_ia32_psrawi:
862 name = "psrawi";
863 ID = Intrinsic::x86_mmx_psra_w;
864 break;
865 case X86::BI__builtin_ia32_psrldi:
866 name = "psrldi";
867 ID = Intrinsic::x86_mmx_psrl_d;
868 break;
869 case X86::BI__builtin_ia32_psrlqi:
870 name = "psrlqi";
871 ID = Intrinsic::x86_mmx_psrl_q;
872 break;
873 case X86::BI__builtin_ia32_psrlwi:
874 name = "psrlwi";
875 ID = Intrinsic::x86_mmx_psrl_w;
876 break;
877 }
Chris Lattner7acda7c2007-12-18 00:25:38 +0000878 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +0000879 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000880 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +0000881 case X86::BI__builtin_ia32_cmpps: {
882 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps);
883 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps");
884 }
885 case X86::BI__builtin_ia32_cmpss: {
886 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss);
887 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000888 }
Nate Begemane7722102008-04-14 04:49:57 +0000889 case X86::BI__builtin_ia32_ldmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000890 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Owen Anderson0032b272009-08-13 21:57:51 +0000891 Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1);
892 Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +0000893 Builder.CreateStore(Ops[0], Tmp);
894 return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000895 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +0000896 }
897 case X86::BI__builtin_ia32_stmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000898 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Owen Anderson0032b272009-08-13 21:57:51 +0000899 Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1);
900 Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +0000901 One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000902 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +0000903 return Builder.CreateLoad(Tmp, "stmxcsr");
904 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +0000905 case X86::BI__builtin_ia32_cmppd: {
906 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd);
907 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd");
908 }
909 case X86::BI__builtin_ia32_cmpsd: {
910 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd);
911 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000912 }
Nate Begemane7722102008-04-14 04:49:57 +0000913 case X86::BI__builtin_ia32_storehps:
914 case X86::BI__builtin_ia32_storelps: {
Owen Anderson0032b272009-08-13 21:57:51 +0000915 const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000916 llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy);
917 llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2);
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Nate Begemane7722102008-04-14 04:49:57 +0000919 // cast val v2i64
920 Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast");
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Nate Begemane7722102008-04-14 04:49:57 +0000922 // extract (0, 1)
923 unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1;
Owen Anderson0032b272009-08-13 21:57:51 +0000924 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Index);
Nate Begemane7722102008-04-14 04:49:57 +0000925 Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract");
926
927 // cast pointer to i64 & store
928 Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy);
929 return Builder.CreateStore(Ops[1], Ops[0]);
930 }
Eric Christopher91b59272009-12-01 05:00:51 +0000931 case X86::BI__builtin_ia32_palignr: {
Nate Begemanc3420ff2009-12-14 05:15:02 +0000932 Function *F = CGM.getIntrinsic(Intrinsic::x86_ssse3_palign_r);
933 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size());
934 }
935 case X86::BI__builtin_ia32_palignr128: {
Nate Begemance5818a2009-12-14 04:57:03 +0000936 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
937
938 // If palignr is shifting the pair of input vectors less than 17 bytes,
939 // emit a shuffle instruction.
940 if (shiftVal <= 16) {
941 const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext);
942
943 llvm::SmallVector<llvm::Constant*, 16> Indices;
944 for (unsigned i = 0; i != 16; ++i)
945 Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i));
946
947 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
948 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
949 }
950
951 // If palignr is shifting the pair of input vectors more than 16 but less
952 // than 32 bytes, emit a logical right shift of the destination.
953 if (shiftVal < 32) {
954 const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext);
955 const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2);
956 const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext);
957
958 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
959 Ops[1] = llvm::ConstantInt::get(IntTy, (shiftVal-16) * 8);
960
961 // create i32 constant
962 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq);
963 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
964 }
965
966 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
967 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Eric Christopher91b59272009-12-01 05:00:51 +0000968 }
Anders Carlsson564f1de2007-12-09 23:17:02 +0000969 }
970}
971
Mike Stump1eb44332009-09-09 15:08:12 +0000972Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +0000973 const CallExpr *E) {
Daniel Dunbarb0b84382009-12-18 20:58:47 +0000974 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000975}