blob: 866c58780b9cc2498cffd5561c81544ac37793de [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
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000016#include "clang/Basic/TargetInfo.h"
Chris Lattner1f329992008-10-06 06:09:18 +000017#include "clang/AST/APValue.h"
Chris Lattnerbef20ac2007-08-31 04:31:45 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/Decl.h"
Chris Lattner6b15cdc2009-06-14 01:05:48 +000020#include "clang/Basic/TargetBuiltins.h"
Anders Carlsson793680e2007-10-12 23:56:29 +000021#include "llvm/Intrinsics.h"
Anders Carlsson022012e2007-08-20 18:05:56 +000022using namespace clang;
23using namespace CodeGen;
Anders Carlssonca6fcfa2007-12-09 21:20:04 +000024using namespace llvm;
25
Daniel Dunbar0002d232009-04-07 00:55:51 +000026/// Utility to insert an atomic instruction based on Instrinsic::ID
27/// and the expression node.
Mike Stump1eb44332009-09-09 15:08:12 +000028static RValue EmitBinaryAtomic(CodeGenFunction& CGF,
Mon P Wang1ffe2812008-05-09 22:40:52 +000029 Intrinsic::ID Id, const CallExpr *E) {
Mon P Wangc5004512008-07-31 03:28:23 +000030 const llvm::Type *ResType[2];
Daniel Dunbar0002d232009-04-07 00:55:51 +000031 ResType[0] = CGF.ConvertType(E->getType());
32 ResType[1] = CGF.ConvertType(E->getArg(0)->getType());
33 Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2);
Mike Stump1eb44332009-09-09 15:08:12 +000034 return RValue::get(CGF.Builder.CreateCall2(AtomF,
35 CGF.EmitScalarExpr(E->getArg(0)),
Daniel Dunbar0002d232009-04-07 00:55:51 +000036 CGF.EmitScalarExpr(E->getArg(1))));
37}
38
39/// Utility to insert an atomic instruction based Instrinsic::ID and
40// the expression node, where the return value is the result of the
41// operation.
Mike Stump1eb44332009-09-09 15:08:12 +000042static RValue EmitBinaryAtomicPost(CodeGenFunction& CGF,
Daniel Dunbar0002d232009-04-07 00:55:51 +000043 Intrinsic::ID Id, const CallExpr *E,
44 Instruction::BinaryOps Op) {
45 const llvm::Type *ResType[2];
46 ResType[0] = CGF.ConvertType(E->getType());
47 ResType[1] = CGF.ConvertType(E->getArg(0)->getType());
48 Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2);
49 Value *Ptr = CGF.EmitScalarExpr(E->getArg(0));
50 Value *Operand = CGF.EmitScalarExpr(E->getArg(1));
51 Value *Result = CGF.Builder.CreateCall2(AtomF, Ptr, Operand);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Chris Lattnereebd9d22009-05-13 04:37:52 +000053 if (Id == Intrinsic::atomic_load_nand)
54 Result = CGF.Builder.CreateNot(Result);
Mike Stump1eb44332009-09-09 15:08:12 +000055
56
Daniel Dunbar0002d232009-04-07 00:55:51 +000057 return RValue::get(CGF.Builder.CreateBinOp(Op, Result, Operand));
Mon P Wang1ffe2812008-05-09 22:40:52 +000058}
59
Mike Stump1eb44332009-09-09 15:08:12 +000060RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
Daniel Dunbaref2abfe2009-02-16 22:43:43 +000061 unsigned BuiltinID, const CallExpr *E) {
Chris Lattner564ea2a2008-10-06 06:56:41 +000062 // See if we can constant fold this builtin. If so, don't emit it at all.
Anders Carlssonf35d35a2008-12-01 02:31:41 +000063 Expr::EvalResult Result;
Chris Lattner6ee7aa12008-11-16 21:24:15 +000064 if (E->Evaluate(Result, CGM.getContext())) {
Anders Carlssonf35d35a2008-12-01 02:31:41 +000065 if (Result.Val.isInt())
Mike Stump1eb44332009-09-09 15:08:12 +000066 return RValue::get(llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +000067 Result.Val.getInt()));
Eli Friedman3941b182009-01-25 01:54:01 +000068 else if (Result.Val.isFloat())
Owen Andersonbc0a2222009-07-27 21:00:51 +000069 return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat()));
Chris Lattner1f329992008-10-06 06:09:18 +000070 }
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattner564ea2a2008-10-06 06:56:41 +000072 switch (BuiltinID) {
73 default: break; // Handle intrinsics and libm functions below.
Chris Lattner506ff882008-10-06 07:26:43 +000074 case Builtin::BI__builtin___CFStringMakeConstantString:
Anders Carlssone9352cc2009-04-08 04:48:15 +000075 return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0));
Chris Lattner6a705f02008-07-09 17:28:44 +000076 case Builtin::BI__builtin_stdarg_start:
Anders Carlsson793680e2007-10-12 23:56:29 +000077 case Builtin::BI__builtin_va_start:
78 case Builtin::BI__builtin_va_end: {
Daniel Dunbar07855702009-02-11 22:25:55 +000079 Value *ArgValue = EmitVAListRef(E->getArg(0));
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +000080 const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson793680e2007-10-12 23:56:29 +000081 if (ArgValue->getType() != DestType)
Mike Stump1eb44332009-09-09 15:08:12 +000082 ArgValue = Builder.CreateBitCast(ArgValue, DestType,
Daniel Dunbarb27ffbe2009-07-26 09:28:40 +000083 ArgValue->getName().data());
Anders Carlsson793680e2007-10-12 23:56:29 +000084
Mike Stump1eb44332009-09-09 15:08:12 +000085 Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ?
Chris Lattner6a705f02008-07-09 17:28:44 +000086 Intrinsic::vaend : Intrinsic::vastart;
Chris Lattner7acda7c2007-12-18 00:25:38 +000087 return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue));
Anders Carlsson793680e2007-10-12 23:56:29 +000088 }
Anders Carlssona28ef8b2008-02-09 20:26:43 +000089 case Builtin::BI__builtin_va_copy: {
Eli Friedman4fd0aa52009-01-20 17:46:04 +000090 Value *DstPtr = EmitVAListRef(E->getArg(0));
91 Value *SrcPtr = EmitVAListRef(E->getArg(1));
Anders Carlssona28ef8b2008-02-09 20:26:43 +000092
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +000093 const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlssona28ef8b2008-02-09 20:26:43 +000094
95 DstPtr = Builder.CreateBitCast(DstPtr, Type);
96 SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
Mike Stump1eb44332009-09-09 15:08:12 +000097 return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy),
Chris Lattner3eae03e2008-05-06 00:56:42 +000098 DstPtr, SrcPtr));
Anders Carlssona28ef8b2008-02-09 20:26:43 +000099 }
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000100 case Builtin::BI__builtin_abs: {
Mike Stump1eb44332009-09-09 15:08:12 +0000101 Value *ArgValue = EmitScalarExpr(E->getArg(0));
102
Chris Lattner9a847f52008-07-23 06:53:34 +0000103 Value *NegOp = Builder.CreateNeg(ArgValue, "neg");
Mike Stump1eb44332009-09-09 15:08:12 +0000104 Value *CmpResult =
105 Builder.CreateICmpSGE(ArgValue,
Owen Andersonc9c88b42009-07-31 20:28:54 +0000106 llvm::Constant::getNullValue(ArgValue->getType()),
Chris Lattner9a847f52008-07-23 06:53:34 +0000107 "abscond");
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Value *Result =
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000109 Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs");
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Anders Carlssonc2251dc2007-11-20 19:05:17 +0000111 return RValue::get(Result);
112 }
Anders Carlsson3a31d602008-02-06 07:19:27 +0000113 case Builtin::BI__builtin_ctz:
114 case Builtin::BI__builtin_ctzl:
115 case Builtin::BI__builtin_ctzll: {
116 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Anders Carlsson3a31d602008-02-06 07:19:27 +0000118 const llvm::Type *ArgType = ArgValue->getType();
119 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
120
Mike Stump1eb44332009-09-09 15:08:12 +0000121 const llvm::Type *ResultType = ConvertType(E->getType());
Anders Carlsson3a31d602008-02-06 07:19:27 +0000122 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
123 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000124 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
125 "cast");
Anders Carlsson3a31d602008-02-06 07:19:27 +0000126 return RValue::get(Result);
127 }
Eli Friedmanf4e85332008-05-27 15:32:46 +0000128 case Builtin::BI__builtin_clz:
129 case Builtin::BI__builtin_clzl:
130 case Builtin::BI__builtin_clzll: {
131 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Eli Friedmanf4e85332008-05-27 15:32:46 +0000133 const llvm::Type *ArgType = ArgValue->getType();
134 Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1);
135
Mike Stump1eb44332009-09-09 15:08:12 +0000136 const llvm::Type *ResultType = ConvertType(E->getType());
Eli Friedmanf4e85332008-05-27 15:32:46 +0000137 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
138 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000139 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
140 "cast");
Eli Friedmanf4e85332008-05-27 15:32:46 +0000141 return RValue::get(Result);
142 }
Daniel Dunbar04b29002008-07-21 17:19:41 +0000143 case Builtin::BI__builtin_ffs:
144 case Builtin::BI__builtin_ffsl:
145 case Builtin::BI__builtin_ffsll: {
146 // ffs(x) -> x ? cttz(x) + 1 : 0
147 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Daniel Dunbar04b29002008-07-21 17:19:41 +0000149 const llvm::Type *ArgType = ArgValue->getType();
150 Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Daniel Dunbar04b29002008-07-21 17:19:41 +0000152 const llvm::Type *ResultType = ConvertType(E->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000153 Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"),
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000154 llvm::ConstantInt::get(ArgType, 1), "tmp");
Owen Andersonc9c88b42009-07-31 20:28:54 +0000155 Value *Zero = llvm::Constant::getNullValue(ArgType);
Daniel Dunbar04b29002008-07-21 17:19:41 +0000156 Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
157 Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
158 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000159 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
160 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000161 return RValue::get(Result);
162 }
163 case Builtin::BI__builtin_parity:
164 case Builtin::BI__builtin_parityl:
165 case Builtin::BI__builtin_parityll: {
166 // parity(x) -> ctpop(x) & 1
167 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Daniel Dunbar04b29002008-07-21 17:19:41 +0000169 const llvm::Type *ArgType = ArgValue->getType();
170 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Daniel Dunbar04b29002008-07-21 17:19:41 +0000172 const llvm::Type *ResultType = ConvertType(E->getType());
173 Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000174 Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1),
Daniel Dunbar04b29002008-07-21 17:19:41 +0000175 "tmp");
176 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000177 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
178 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000179 return RValue::get(Result);
180 }
181 case Builtin::BI__builtin_popcount:
182 case Builtin::BI__builtin_popcountl:
183 case Builtin::BI__builtin_popcountll: {
184 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Daniel Dunbar04b29002008-07-21 17:19:41 +0000186 const llvm::Type *ArgType = ArgValue->getType();
187 Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Daniel Dunbar04b29002008-07-21 17:19:41 +0000189 const llvm::Type *ResultType = ConvertType(E->getType());
190 Value *Result = Builder.CreateCall(F, ArgValue, "tmp");
191 if (Result->getType() != ResultType)
Duncan Sandseac73e52009-11-16 13:11:21 +0000192 Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
193 "cast");
Daniel Dunbar04b29002008-07-21 17:19:41 +0000194 return RValue::get(Result);
195 }
Chris Lattner1feedd82007-12-13 07:34:23 +0000196 case Builtin::BI__builtin_expect:
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000197 // FIXME: pass expect through to LLVM
Chris Lattner1feedd82007-12-13 07:34:23 +0000198 return RValue::get(EmitScalarExpr(E->getArg(0)));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000199 case Builtin::BI__builtin_bswap32:
200 case Builtin::BI__builtin_bswap64: {
Chris Lattner1feedd82007-12-13 07:34:23 +0000201 Value *ArgValue = EmitScalarExpr(E->getArg(0));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000202 const llvm::Type *ArgType = ArgValue->getType();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000203 Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
Chris Lattner1feedd82007-12-13 07:34:23 +0000204 return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
Mike Stump1eb44332009-09-09 15:08:12 +0000205 }
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000206 case Builtin::BI__builtin_object_size: {
Mike Stumpb16d32f2009-10-26 23:39:48 +0000207 // We pass this builtin onto the optimizer so that it can
208 // figure out the object size in more complex cases.
Mike Stumpc4c90452009-10-27 22:09:17 +0000209 const llvm::Type *ResType[] = {
210 ConvertType(E->getType())
211 };
Eric Christopherfee667f2009-12-23 03:49:37 +0000212
213 // LLVM only supports 0 and 2, make sure that we pass along that
214 // as a boolean.
215 Value *Ty = EmitScalarExpr(E->getArg(1));
216 ConstantInt *CI = dyn_cast<ConstantInt>(Ty);
217 assert(CI);
218 uint64_t val = CI->getZExtValue();
219 CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1);
220
Mike Stumpc4c90452009-10-27 22:09:17 +0000221 Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1);
222 return RValue::get(Builder.CreateCall2(F,
223 EmitScalarExpr(E->getArg(0)),
Eric Christopherfee667f2009-12-23 03:49:37 +0000224 CI));
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000225 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000226 case Builtin::BI__builtin_prefetch: {
227 Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
228 // FIXME: Technically these constants should of type 'int', yes?
Mike Stump1eb44332009-09-09 15:08:12 +0000229 RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
Owen Anderson0032b272009-08-13 21:57:51 +0000230 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000231 Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
Owen Anderson0032b272009-08-13 21:57:51 +0000232 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 3);
Daniel Dunbar4493f792008-07-21 22:59:13 +0000233 Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0);
234 return RValue::get(Builder.CreateCall3(F, Address, RW, Locality));
Anders Carlssondf4852a2007-12-02 21:58:10 +0000235 }
Daniel Dunbar4493f792008-07-21 22:59:13 +0000236 case Builtin::BI__builtin_trap: {
237 Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0);
238 return RValue::get(Builder.CreateCall(F));
239 }
Chris Lattner21190d52009-09-21 03:09:59 +0000240 case Builtin::BI__builtin_unreachable: {
Mike Stumpfba565d2009-12-16 03:07:12 +0000241 if (CatchUndefined && HaveInsertPoint())
242 EmitBranch(getTrapBB());
Chris Lattner21190d52009-09-21 03:09:59 +0000243 Value *V = Builder.CreateUnreachable();
244 Builder.ClearInsertionPoint();
245 return RValue::get(V);
246 }
247
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000248 case Builtin::BI__builtin_powi:
249 case Builtin::BI__builtin_powif:
250 case Builtin::BI__builtin_powil: {
251 Value *Base = EmitScalarExpr(E->getArg(0));
252 Value *Exponent = EmitScalarExpr(E->getArg(1));
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000253 const llvm::Type *ArgType = Base->getType();
254 Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1);
Daniel Dunbara933c3c2008-07-21 18:44:41 +0000255 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
256 }
257
Chris Lattnerfe23e212007-12-20 00:44:32 +0000258 case Builtin::BI__builtin_isgreater:
259 case Builtin::BI__builtin_isgreaterequal:
260 case Builtin::BI__builtin_isless:
261 case Builtin::BI__builtin_islessequal:
262 case Builtin::BI__builtin_islessgreater:
263 case Builtin::BI__builtin_isunordered: {
264 // Ordered comparisons: we know the arguments to these are matching scalar
265 // floating point values.
Mike Stump1eb44332009-09-09 15:08:12 +0000266 Value *LHS = EmitScalarExpr(E->getArg(0));
Chris Lattnerfe23e212007-12-20 00:44:32 +0000267 Value *RHS = EmitScalarExpr(E->getArg(1));
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattnerfe23e212007-12-20 00:44:32 +0000269 switch (BuiltinID) {
270 default: assert(0 && "Unknown ordered comparison");
271 case Builtin::BI__builtin_isgreater:
272 LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
273 break;
274 case Builtin::BI__builtin_isgreaterequal:
275 LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
276 break;
277 case Builtin::BI__builtin_isless:
278 LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
279 break;
280 case Builtin::BI__builtin_islessequal:
281 LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
282 break;
283 case Builtin::BI__builtin_islessgreater:
284 LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
285 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000286 case Builtin::BI__builtin_isunordered:
Chris Lattnerfe23e212007-12-20 00:44:32 +0000287 LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
288 break;
289 }
290 // ZExt bool to int type.
291 return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()),
292 "tmp"));
293 }
Eli Friedmand6139892009-09-01 04:19:44 +0000294 case Builtin::BI__builtin_isnan: {
295 Value *V = EmitScalarExpr(E->getArg(0));
296 V = Builder.CreateFCmpUNO(V, V, "cmp");
297 return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp"));
298 }
Eli Friedmanb52fe9c2009-06-02 07:10:30 +0000299 case Builtin::BIalloca:
Chris Lattner9e800e32008-06-16 17:15:14 +0000300 case Builtin::BI__builtin_alloca: {
301 // FIXME: LLVM IR Should allow alloca with an i64 size!
302 Value *Size = EmitScalarExpr(E->getArg(0));
Owen Anderson0032b272009-08-13 21:57:51 +0000303 Size = Builder.CreateIntCast(Size, llvm::Type::getInt32Ty(VMContext), false, "tmp");
304 return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp"));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000305 }
306 case Builtin::BI__builtin_bzero: {
307 Value *Address = EmitScalarExpr(E->getArg(0));
308 Builder.CreateCall4(CGM.getMemSetFn(), Address,
Owen Anderson0032b272009-08-13 21:57:51 +0000309 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0),
Daniel Dunbar1caae952008-07-22 00:26:45 +0000310 EmitScalarExpr(E->getArg(1)),
Owen Anderson0032b272009-08-13 21:57:51 +0000311 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000312 return RValue::get(Address);
Chris Lattner9e800e32008-06-16 17:15:14 +0000313 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000314 case Builtin::BImemcpy:
Eli Friedmand4b32e42008-05-19 23:27:48 +0000315 case Builtin::BI__builtin_memcpy: {
Daniel Dunbar1caae952008-07-22 00:26:45 +0000316 Value *Address = EmitScalarExpr(E->getArg(0));
317 Builder.CreateCall4(CGM.getMemCpyFn(), Address,
318 EmitScalarExpr(E->getArg(1)),
319 EmitScalarExpr(E->getArg(2)),
Owen Anderson0032b272009-08-13 21:57:51 +0000320 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000321 return RValue::get(Address);
322 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000323 case Builtin::BImemmove:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000324 case Builtin::BI__builtin_memmove: {
325 Value *Address = EmitScalarExpr(E->getArg(0));
326 Builder.CreateCall4(CGM.getMemMoveFn(), Address,
327 EmitScalarExpr(E->getArg(1)),
328 EmitScalarExpr(E->getArg(2)),
Owen Anderson0032b272009-08-13 21:57:51 +0000329 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000330 return RValue::get(Address);
331 }
Eli Friedmane6ec2052009-12-17 00:14:28 +0000332 case Builtin::BImemset:
Daniel Dunbar1caae952008-07-22 00:26:45 +0000333 case Builtin::BI__builtin_memset: {
334 Value *Address = EmitScalarExpr(E->getArg(0));
335 Builder.CreateCall4(CGM.getMemSetFn(), Address,
Daniel Dunbar62c29c62009-02-16 21:52:05 +0000336 Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
Owen Anderson0032b272009-08-13 21:57:51 +0000337 llvm::Type::getInt8Ty(VMContext)),
Daniel Dunbar1caae952008-07-22 00:26:45 +0000338 EmitScalarExpr(E->getArg(2)),
Owen Anderson0032b272009-08-13 21:57:51 +0000339 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1));
Daniel Dunbar1caae952008-07-22 00:26:45 +0000340 return RValue::get(Address);
Eli Friedmand4b32e42008-05-19 23:27:48 +0000341 }
Eli Friedman256f77e2008-05-20 08:59:34 +0000342 case Builtin::BI__builtin_return_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000343 Value *Depth = EmitScalarExpr(E->getArg(0));
344 Depth = Builder.CreateIntCast(Depth,
345 llvm::Type::getInt32Ty(VMContext),
346 false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000347 Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000348 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000349 }
350 case Builtin::BI__builtin_frame_address: {
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000351 Value *Depth = EmitScalarExpr(E->getArg(0));
352 Depth = Builder.CreateIntCast(Depth,
353 llvm::Type::getInt32Ty(VMContext),
354 false, "tmp");
Eli Friedman256f77e2008-05-20 08:59:34 +0000355 Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0);
Anton Korobeynikov83c2a982009-12-27 14:27:22 +0000356 return RValue::get(Builder.CreateCall(F, Depth));
Eli Friedman256f77e2008-05-20 08:59:34 +0000357 }
Eli Friedman3b660ef2009-05-03 19:23:23 +0000358 case Builtin::BI__builtin_extract_return_addr: {
359 // FIXME: There should be a target hook for this
360 return RValue::get(EmitScalarExpr(E->getArg(0)));
361 }
Eli Friedmana6d75c02009-06-02 09:37:50 +0000362 case Builtin::BI__builtin_unwind_init: {
363 Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0);
364 return RValue::get(Builder.CreateCall(F));
365 }
366#if 0
367 // FIXME: Finish/enable when LLVM backend support stabilizes
368 case Builtin::BI__builtin_setjmp: {
369 Value *Buf = EmitScalarExpr(E->getArg(0));
370 // Store the frame pointer to the buffer
371 Value *FrameAddrF = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0);
372 Value *FrameAddr =
373 Builder.CreateCall(FrameAddrF,
Owen Anderson0032b272009-08-13 21:57:51 +0000374 Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)));
Eli Friedmana6d75c02009-06-02 09:37:50 +0000375 Builder.CreateStore(FrameAddr, Buf);
376 // Call the setjmp intrinsic
377 Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp, 0, 0);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000378 const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext);
Eli Friedmana6d75c02009-06-02 09:37:50 +0000379 Buf = Builder.CreateBitCast(Buf, DestType);
380 return RValue::get(Builder.CreateCall(F, Buf));
381 }
382 case Builtin::BI__builtin_longjmp: {
383 Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp, 0, 0);
384 Value *Buf = EmitScalarExpr(E->getArg(0));
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000385 const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext);
Eli Friedmana6d75c02009-06-02 09:37:50 +0000386 Buf = Builder.CreateBitCast(Buf, DestType);
387 return RValue::get(Builder.CreateCall(F, Buf));
388 }
389#endif
Mon P Wang1ffe2812008-05-09 22:40:52 +0000390 case Builtin::BI__sync_fetch_and_add:
Mon P Wang1ffe2812008-05-09 22:40:52 +0000391 case Builtin::BI__sync_fetch_and_sub:
Chris Lattner5caa3702009-05-08 06:58:22 +0000392 case Builtin::BI__sync_fetch_and_or:
393 case Builtin::BI__sync_fetch_and_and:
394 case Builtin::BI__sync_fetch_and_xor:
395 case Builtin::BI__sync_add_and_fetch:
396 case Builtin::BI__sync_sub_and_fetch:
397 case Builtin::BI__sync_and_and_fetch:
398 case Builtin::BI__sync_or_and_fetch:
399 case Builtin::BI__sync_xor_and_fetch:
400 case Builtin::BI__sync_val_compare_and_swap:
401 case Builtin::BI__sync_bool_compare_and_swap:
402 case Builtin::BI__sync_lock_test_and_set:
403 case Builtin::BI__sync_lock_release:
404 assert(0 && "Shouldn't make it through sema");
405 case Builtin::BI__sync_fetch_and_add_1:
406 case Builtin::BI__sync_fetch_and_add_2:
407 case Builtin::BI__sync_fetch_and_add_4:
408 case Builtin::BI__sync_fetch_and_add_8:
409 case Builtin::BI__sync_fetch_and_add_16:
410 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E);
411 case Builtin::BI__sync_fetch_and_sub_1:
412 case Builtin::BI__sync_fetch_and_sub_2:
413 case Builtin::BI__sync_fetch_and_sub_4:
414 case Builtin::BI__sync_fetch_and_sub_8:
415 case Builtin::BI__sync_fetch_and_sub_16:
Mon P Wang09b6bf52008-06-25 08:21:36 +0000416 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E);
Chris Lattner5caa3702009-05-08 06:58:22 +0000417 case Builtin::BI__sync_fetch_and_or_1:
418 case Builtin::BI__sync_fetch_and_or_2:
419 case Builtin::BI__sync_fetch_and_or_4:
420 case Builtin::BI__sync_fetch_and_or_8:
421 case Builtin::BI__sync_fetch_and_or_16:
422 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E);
423 case Builtin::BI__sync_fetch_and_and_1:
424 case Builtin::BI__sync_fetch_and_and_2:
425 case Builtin::BI__sync_fetch_and_and_4:
426 case Builtin::BI__sync_fetch_and_and_8:
427 case Builtin::BI__sync_fetch_and_and_16:
428 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E);
429 case Builtin::BI__sync_fetch_and_xor_1:
430 case Builtin::BI__sync_fetch_and_xor_2:
431 case Builtin::BI__sync_fetch_and_xor_4:
432 case Builtin::BI__sync_fetch_and_xor_8:
433 case Builtin::BI__sync_fetch_and_xor_16:
434 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E);
Chris Lattnereebd9d22009-05-13 04:37:52 +0000435 case Builtin::BI__sync_fetch_and_nand_1:
436 case Builtin::BI__sync_fetch_and_nand_2:
437 case Builtin::BI__sync_fetch_and_nand_4:
438 case Builtin::BI__sync_fetch_and_nand_8:
439 case Builtin::BI__sync_fetch_and_nand_16:
440 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E);
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Chris Lattner5caa3702009-05-08 06:58:22 +0000442 // Clang extensions: not overloaded yet.
Mon P Wang1ffe2812008-05-09 22:40:52 +0000443 case Builtin::BI__sync_fetch_and_min:
444 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E);
445 case Builtin::BI__sync_fetch_and_max:
446 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E);
447 case Builtin::BI__sync_fetch_and_umin:
448 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E);
449 case Builtin::BI__sync_fetch_and_umax:
450 return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000451
Chris Lattner5caa3702009-05-08 06:58:22 +0000452 case Builtin::BI__sync_add_and_fetch_1:
453 case Builtin::BI__sync_add_and_fetch_2:
454 case Builtin::BI__sync_add_and_fetch_4:
455 case Builtin::BI__sync_add_and_fetch_8:
456 case Builtin::BI__sync_add_and_fetch_16:
Mike Stump1eb44332009-09-09 15:08:12 +0000457 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E,
Daniel Dunbar0002d232009-04-07 00:55:51 +0000458 llvm::Instruction::Add);
Chris Lattner5caa3702009-05-08 06:58:22 +0000459 case Builtin::BI__sync_sub_and_fetch_1:
460 case Builtin::BI__sync_sub_and_fetch_2:
461 case Builtin::BI__sync_sub_and_fetch_4:
462 case Builtin::BI__sync_sub_and_fetch_8:
463 case Builtin::BI__sync_sub_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000464 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E,
465 llvm::Instruction::Sub);
Chris Lattner5caa3702009-05-08 06:58:22 +0000466 case Builtin::BI__sync_and_and_fetch_1:
467 case Builtin::BI__sync_and_and_fetch_2:
468 case Builtin::BI__sync_and_and_fetch_4:
469 case Builtin::BI__sync_and_and_fetch_8:
470 case Builtin::BI__sync_and_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000471 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E,
472 llvm::Instruction::And);
Chris Lattner5caa3702009-05-08 06:58:22 +0000473 case Builtin::BI__sync_or_and_fetch_1:
474 case Builtin::BI__sync_or_and_fetch_2:
475 case Builtin::BI__sync_or_and_fetch_4:
476 case Builtin::BI__sync_or_and_fetch_8:
477 case Builtin::BI__sync_or_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000478 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E,
479 llvm::Instruction::Or);
Chris Lattner5caa3702009-05-08 06:58:22 +0000480 case Builtin::BI__sync_xor_and_fetch_1:
481 case Builtin::BI__sync_xor_and_fetch_2:
482 case Builtin::BI__sync_xor_and_fetch_4:
483 case Builtin::BI__sync_xor_and_fetch_8:
484 case Builtin::BI__sync_xor_and_fetch_16:
Daniel Dunbar0002d232009-04-07 00:55:51 +0000485 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E,
486 llvm::Instruction::Xor);
Chris Lattnereebd9d22009-05-13 04:37:52 +0000487 case Builtin::BI__sync_nand_and_fetch_1:
488 case Builtin::BI__sync_nand_and_fetch_2:
489 case Builtin::BI__sync_nand_and_fetch_4:
490 case Builtin::BI__sync_nand_and_fetch_8:
491 case Builtin::BI__sync_nand_and_fetch_16:
492 return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E,
493 llvm::Instruction::And);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattner5caa3702009-05-08 06:58:22 +0000495 case Builtin::BI__sync_val_compare_and_swap_1:
496 case Builtin::BI__sync_val_compare_and_swap_2:
497 case Builtin::BI__sync_val_compare_and_swap_4:
498 case Builtin::BI__sync_val_compare_and_swap_8:
499 case Builtin::BI__sync_val_compare_and_swap_16:
500 {
Mon P Wangc5004512008-07-31 03:28:23 +0000501 const llvm::Type *ResType[2];
502 ResType[0]= ConvertType(E->getType());
503 ResType[1] = ConvertType(E->getArg(0)->getType());
504 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2);
Mike Stump1eb44332009-09-09 15:08:12 +0000505 return RValue::get(Builder.CreateCall3(AtomF,
Anders Carlsson95844462008-08-30 16:17:45 +0000506 EmitScalarExpr(E->getArg(0)),
507 EmitScalarExpr(E->getArg(1)),
508 EmitScalarExpr(E->getArg(2))));
Anders Carlsson89799cf2007-10-29 02:59:40 +0000509 }
Daniel Dunbar0002d232009-04-07 00:55:51 +0000510
Chris Lattner5caa3702009-05-08 06:58:22 +0000511 case Builtin::BI__sync_bool_compare_and_swap_1:
512 case Builtin::BI__sync_bool_compare_and_swap_2:
513 case Builtin::BI__sync_bool_compare_and_swap_4:
514 case Builtin::BI__sync_bool_compare_and_swap_8:
515 case Builtin::BI__sync_bool_compare_and_swap_16:
516 {
Daniel Dunbar0002d232009-04-07 00:55:51 +0000517 const llvm::Type *ResType[2];
Chris Lattner5caa3702009-05-08 06:58:22 +0000518 ResType[0]= ConvertType(E->getArg(1)->getType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000519 ResType[1] = llvm::PointerType::getUnqual(ResType[0]);
Daniel Dunbar0002d232009-04-07 00:55:51 +0000520 Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2);
521 Value *OldVal = EmitScalarExpr(E->getArg(1));
Mike Stump1eb44332009-09-09 15:08:12 +0000522 Value *PrevVal = Builder.CreateCall3(AtomF,
Daniel Dunbar0002d232009-04-07 00:55:51 +0000523 EmitScalarExpr(E->getArg(0)),
524 OldVal,
525 EmitScalarExpr(E->getArg(2)));
526 Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal);
527 // zext bool to int.
528 return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
529 }
530
Chris Lattner5caa3702009-05-08 06:58:22 +0000531 case Builtin::BI__sync_lock_test_and_set_1:
532 case Builtin::BI__sync_lock_test_and_set_2:
533 case Builtin::BI__sync_lock_test_and_set_4:
534 case Builtin::BI__sync_lock_test_and_set_8:
535 case Builtin::BI__sync_lock_test_and_set_16:
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000536 return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E);
Chris Lattner5caa3702009-05-08 06:58:22 +0000537 case Builtin::BI__sync_lock_release_1:
538 case Builtin::BI__sync_lock_release_2:
539 case Builtin::BI__sync_lock_release_4:
540 case Builtin::BI__sync_lock_release_8:
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000541 case Builtin::BI__sync_lock_release_16: {
542 Value *Ptr = EmitScalarExpr(E->getArg(0));
543 const llvm::Type *ElTy =
544 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar007b5672009-11-29 21:11:47 +0000545 llvm::StoreInst *Store =
546 Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr);
547 Store->setVolatile(true);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000548 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000549 }
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000550
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000551 case Builtin::BI__sync_synchronize: {
552 Value *C[5];
Owen Anderson0032b272009-08-13 21:57:51 +0000553 C[0] = C[1] = C[2] = C[3] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 1);
554 C[4] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000555 Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5);
Daniel Dunbareb4f81e2009-05-27 23:45:33 +0000556 return RValue::get(0);
Chris Lattnerf58cd9b2009-05-13 04:46:13 +0000557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000559 // Library functions with special handling.
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000560 case Builtin::BIsqrt:
561 case Builtin::BIsqrtf:
562 case Builtin::BIsqrtl: {
563 // Rewrite sqrt to intrinsic if allowed.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000564 if (!FD->hasAttr<ConstAttr>())
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000565 break;
566 Value *Arg0 = EmitScalarExpr(E->getArg(0));
567 const llvm::Type *ArgType = Arg0->getType();
568 Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1);
569 return RValue::get(Builder.CreateCall(F, Arg0, "tmp"));
570 }
571
572 case Builtin::BIpow:
573 case Builtin::BIpowf:
574 case Builtin::BIpowl: {
575 // Rewrite sqrt to intrinsic if allowed.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000576 if (!FD->hasAttr<ConstAttr>())
Daniel Dunbaref2abfe2009-02-16 22:43:43 +0000577 break;
578 Value *Base = EmitScalarExpr(E->getArg(0));
579 Value *Exponent = EmitScalarExpr(E->getArg(1));
580 const llvm::Type *ArgType = Base->getType();
581 Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1);
582 return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
583 }
Nate Begeman7ea2e3f2008-05-15 07:38:03 +0000584 }
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000586 // If this is an alias for a libm function (e.g. __builtin_sin) turn it into
587 // that function.
Douglas Gregor3e41d602009-02-13 23:20:09 +0000588 if (getContext().BuiltinInfo.isLibFunction(BuiltinID) ||
589 getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
Anders Carlsson31777a22009-12-24 19:08:58 +0000590 return EmitCall(E->getCallee()->getType(),
591 CGM.getBuiltinLibFunction(FD, BuiltinID),
Anders Carlssond2490a92009-12-24 20:40:36 +0000592 ReturnValueSlot(),
Anders Carlsson31777a22009-12-24 19:08:58 +0000593 E->arg_begin(), E->arg_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000595 // See if we have a target specific intrinsic.
Dale Johannesena6f80ef2009-02-05 01:50:47 +0000596 const char *Name = getContext().BuiltinInfo.GetName(BuiltinID);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000597 Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
598 if (const char *Prefix =
Mike Stump1eb44332009-09-09 15:08:12 +0000599 llvm::Triple::getArchTypePrefix(Target.getTriple().getArch()))
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000600 IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000602 if (IntrinsicID != Intrinsic::not_intrinsic) {
603 SmallVector<Value*, 16> Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000605 Function *F = CGM.getIntrinsic(IntrinsicID);
606 const llvm::FunctionType *FTy = F->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000608 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
609 Value *ArgValue = EmitScalarExpr(E->getArg(i));
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000611 // If the intrinsic arg type is different from the builtin arg type
612 // we need to do a bit cast.
613 const llvm::Type *PTy = FTy->getParamType(i);
614 if (PTy != ArgValue->getType()) {
615 assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
616 "Must be able to losslessly bit cast to param");
617 ArgValue = Builder.CreateBitCast(ArgValue, PTy);
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000620 Args.push_back(ArgValue);
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Jay Foadbeaaccd2009-05-21 09:52:38 +0000623 Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size());
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000624 QualType BuiltinRetType = E->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Owen Anderson0032b272009-08-13 21:57:51 +0000626 const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000627 if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType);
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000629 if (RetTy != V->getType()) {
630 assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
631 "Must be able to losslessly bit cast result type");
632 V = Builder.CreateBitCast(V, RetTy);
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000635 return RValue::get(V);
636 }
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000638 // See if we have a target specific builtin that needs to be lowered.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000639 if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E))
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000640 return RValue::get(V);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Daniel Dunbar488e9932008-08-16 00:56:44 +0000642 ErrorUnsupported(E, "builtin function");
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000644 // Unknown builtin, for now just dump it out and return undef.
645 if (hasAggregateLLVMType(E->getType()))
646 return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType())));
Owen Anderson03e20502009-07-30 23:11:26 +0000647 return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
Mike Stump1eb44332009-09-09 15:08:12 +0000648}
Anders Carlsson564f1de2007-12-09 23:17:02 +0000649
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000650Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID,
651 const CallExpr *E) {
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000652 switch (Target.getTriple().getArch()) {
653 case llvm::Triple::x86:
654 case llvm::Triple::x86_64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000655 return EmitX86BuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000656 case llvm::Triple::ppc:
657 case llvm::Triple::ppc64:
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000658 return EmitPPCBuiltinExpr(BuiltinID, E);
Daniel Dunbar55cc2ed2009-08-24 09:54:37 +0000659 default:
660 return 0;
661 }
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +0000662}
663
Mike Stump1eb44332009-09-09 15:08:12 +0000664Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +0000665 const CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000667 llvm::SmallVector<Value*, 4> Ops;
668
669 for (unsigned i = 0, e = E->getNumArgs(); i != e; i++)
670 Ops.push_back(EmitScalarExpr(E->getArg(i)));
671
Anders Carlsson564f1de2007-12-09 23:17:02 +0000672 switch (BuiltinID) {
Anders Carlsson46a26b02007-12-09 23:39:18 +0000673 default: return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000674 case X86::BI__builtin_ia32_pslldi128:
Nate Begemane7722102008-04-14 04:49:57 +0000675 case X86::BI__builtin_ia32_psllqi128:
Mike Stump1eb44332009-09-09 15:08:12 +0000676 case X86::BI__builtin_ia32_psllwi128:
Nate Begemane7722102008-04-14 04:49:57 +0000677 case X86::BI__builtin_ia32_psradi128:
678 case X86::BI__builtin_ia32_psrawi128:
679 case X86::BI__builtin_ia32_psrldi128:
680 case X86::BI__builtin_ia32_psrlqi128:
681 case X86::BI__builtin_ia32_psrlwi128: {
Owen Anderson0032b272009-08-13 21:57:51 +0000682 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext");
683 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 2);
684 llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Owen Anderson03e20502009-07-30 23:11:26 +0000685 Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty),
Nate Begemane7722102008-04-14 04:49:57 +0000686 Ops[1], Zero, "insert");
687 Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast");
688 const char *name = 0;
689 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Nate Begemane7722102008-04-14 04:49:57 +0000691 switch (BuiltinID) {
692 default: assert(0 && "Unsupported shift intrinsic!");
693 case X86::BI__builtin_ia32_pslldi128:
694 name = "pslldi";
695 ID = Intrinsic::x86_sse2_psll_d;
696 break;
697 case X86::BI__builtin_ia32_psllqi128:
698 name = "psllqi";
699 ID = Intrinsic::x86_sse2_psll_q;
700 break;
701 case X86::BI__builtin_ia32_psllwi128:
702 name = "psllwi";
703 ID = Intrinsic::x86_sse2_psll_w;
704 break;
705 case X86::BI__builtin_ia32_psradi128:
706 name = "psradi";
707 ID = Intrinsic::x86_sse2_psra_d;
708 break;
709 case X86::BI__builtin_ia32_psrawi128:
710 name = "psrawi";
711 ID = Intrinsic::x86_sse2_psra_w;
712 break;
713 case X86::BI__builtin_ia32_psrldi128:
714 name = "psrldi";
715 ID = Intrinsic::x86_sse2_psrl_d;
716 break;
717 case X86::BI__builtin_ia32_psrlqi128:
718 name = "psrlqi";
719 ID = Intrinsic::x86_sse2_psrl_q;
720 break;
721 case X86::BI__builtin_ia32_psrlwi128:
722 name = "psrlwi";
723 ID = Intrinsic::x86_sse2_psrl_w;
724 break;
725 }
726 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +0000727 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Nate Begemane7722102008-04-14 04:49:57 +0000728 }
Mike Stump1eb44332009-09-09 15:08:12 +0000729 case X86::BI__builtin_ia32_pslldi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000730 case X86::BI__builtin_ia32_psllqi:
Mike Stump1eb44332009-09-09 15:08:12 +0000731 case X86::BI__builtin_ia32_psllwi:
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000732 case X86::BI__builtin_ia32_psradi:
733 case X86::BI__builtin_ia32_psrawi:
734 case X86::BI__builtin_ia32_psrldi:
735 case X86::BI__builtin_ia32_psrlqi:
736 case X86::BI__builtin_ia32_psrlwi: {
Owen Anderson0032b272009-08-13 21:57:51 +0000737 Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext");
738 const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 1);
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000739 Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast");
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000740 const char *name = 0;
741 Intrinsic::ID ID = Intrinsic::not_intrinsic;
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000743 switch (BuiltinID) {
744 default: assert(0 && "Unsupported shift intrinsic!");
745 case X86::BI__builtin_ia32_pslldi:
746 name = "pslldi";
747 ID = Intrinsic::x86_mmx_psll_d;
748 break;
749 case X86::BI__builtin_ia32_psllqi:
750 name = "psllqi";
751 ID = Intrinsic::x86_mmx_psll_q;
752 break;
753 case X86::BI__builtin_ia32_psllwi:
754 name = "psllwi";
755 ID = Intrinsic::x86_mmx_psll_w;
756 break;
757 case X86::BI__builtin_ia32_psradi:
758 name = "psradi";
759 ID = Intrinsic::x86_mmx_psra_d;
760 break;
761 case X86::BI__builtin_ia32_psrawi:
762 name = "psrawi";
763 ID = Intrinsic::x86_mmx_psra_w;
764 break;
765 case X86::BI__builtin_ia32_psrldi:
766 name = "psrldi";
767 ID = Intrinsic::x86_mmx_psrl_d;
768 break;
769 case X86::BI__builtin_ia32_psrlqi:
770 name = "psrlqi";
771 ID = Intrinsic::x86_mmx_psrl_q;
772 break;
773 case X86::BI__builtin_ia32_psrlwi:
774 name = "psrlwi";
775 ID = Intrinsic::x86_mmx_psrl_w;
776 break;
777 }
Chris Lattner7acda7c2007-12-18 00:25:38 +0000778 llvm::Function *F = CGM.getIntrinsic(ID);
Mike Stump1eb44332009-09-09 15:08:12 +0000779 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name);
Anders Carlsson2929cfa2007-12-14 17:48:24 +0000780 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +0000781 case X86::BI__builtin_ia32_cmpps: {
782 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps);
783 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps");
784 }
785 case X86::BI__builtin_ia32_cmpss: {
786 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss);
787 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000788 }
Nate Begemane7722102008-04-14 04:49:57 +0000789 case X86::BI__builtin_ia32_ldmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000790 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Owen Anderson0032b272009-08-13 21:57:51 +0000791 Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1);
792 Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +0000793 Builder.CreateStore(Ops[0], Tmp);
794 return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000795 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +0000796 }
797 case X86::BI__builtin_ia32_stmxcsr: {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000798 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Owen Anderson0032b272009-08-13 21:57:51 +0000799 Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1);
800 Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp");
Nate Begemane7722102008-04-14 04:49:57 +0000801 One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr),
Chris Lattner3eae03e2008-05-06 00:56:42 +0000802 Builder.CreateBitCast(Tmp, PtrTy));
Nate Begemane7722102008-04-14 04:49:57 +0000803 return Builder.CreateLoad(Tmp, "stmxcsr");
804 }
Anders Carlsson79dcf5f2009-05-18 19:16:46 +0000805 case X86::BI__builtin_ia32_cmppd: {
806 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd);
807 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd");
808 }
809 case X86::BI__builtin_ia32_cmpsd: {
810 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd);
811 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd");
Anders Carlssoncc8b7f92007-12-16 22:33:50 +0000812 }
Nate Begemane7722102008-04-14 04:49:57 +0000813 case X86::BI__builtin_ia32_storehps:
814 case X86::BI__builtin_ia32_storelps: {
Owen Anderson0032b272009-08-13 21:57:51 +0000815 const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000816 llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy);
817 llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2);
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Nate Begemane7722102008-04-14 04:49:57 +0000819 // cast val v2i64
820 Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast");
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Nate Begemane7722102008-04-14 04:49:57 +0000822 // extract (0, 1)
823 unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1;
Owen Anderson0032b272009-08-13 21:57:51 +0000824 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Index);
Nate Begemane7722102008-04-14 04:49:57 +0000825 Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract");
826
827 // cast pointer to i64 & store
828 Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy);
829 return Builder.CreateStore(Ops[1], Ops[0]);
830 }
Eric Christopher91b59272009-12-01 05:00:51 +0000831 case X86::BI__builtin_ia32_palignr: {
Nate Begemanc3420ff2009-12-14 05:15:02 +0000832 Function *F = CGM.getIntrinsic(Intrinsic::x86_ssse3_palign_r);
833 return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size());
834 }
835 case X86::BI__builtin_ia32_palignr128: {
Nate Begemance5818a2009-12-14 04:57:03 +0000836 unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue();
837
838 // If palignr is shifting the pair of input vectors less than 17 bytes,
839 // emit a shuffle instruction.
840 if (shiftVal <= 16) {
841 const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext);
842
843 llvm::SmallVector<llvm::Constant*, 16> Indices;
844 for (unsigned i = 0; i != 16; ++i)
845 Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i));
846
847 Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size());
848 return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr");
849 }
850
851 // If palignr is shifting the pair of input vectors more than 16 but less
852 // than 32 bytes, emit a logical right shift of the destination.
853 if (shiftVal < 32) {
854 const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext);
855 const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2);
856 const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext);
857
858 Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast");
859 Ops[1] = llvm::ConstantInt::get(IntTy, (shiftVal-16) * 8);
860
861 // create i32 constant
862 llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq);
863 return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr");
864 }
865
866 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
867 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Eric Christopher91b59272009-12-01 05:00:51 +0000868 }
Anders Carlsson564f1de2007-12-09 23:17:02 +0000869 }
870}
871
Mike Stump1eb44332009-09-09 15:08:12 +0000872Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
Chris Lattner1feedd82007-12-13 07:34:23 +0000873 const CallExpr *E) {
Daniel Dunbarb0b84382009-12-18 20:58:47 +0000874 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000875}