Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 1 | //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 7 | // |
| 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 Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 1f32999 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 17 | #include "clang/AST/APValue.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Chris Lattner | 6b15cdc | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetBuiltins.h" |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 21 | #include "llvm/Intrinsics.h" |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 26 | /// Utility to insert an atomic instruction based on Instrinsic::ID |
| 27 | /// and the expression node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | static RValue EmitBinaryAtomic(CodeGenFunction& CGF, |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 29 | Intrinsic::ID Id, const CallExpr *E) { |
Mon P Wang | c500451 | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 30 | const llvm::Type *ResType[2]; |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 31 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | return RValue::get(CGF.Builder.CreateCall2(AtomF, |
| 35 | CGF.EmitScalarExpr(E->getArg(0)), |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 36 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | static RValue EmitBinaryAtomicPost(CodeGenFunction& CGF, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 43 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 53 | if (Id == Intrinsic::atomic_load_nand) |
| 54 | Result = CGF.Builder.CreateNot(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
| 56 | |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 57 | return RValue::get(CGF.Builder.CreateBinOp(Op, Result, Operand)); |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 61 | unsigned BuiltinID, const CallExpr *E) { |
Chris Lattner | 564ea2a | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 62 | // See if we can constant fold this builtin. If so, don't emit it at all. |
Anders Carlsson | f35d35a | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 63 | Expr::EvalResult Result; |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 64 | if (E->Evaluate(Result, CGM.getContext())) { |
Anders Carlsson | f35d35a | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 65 | if (Result.Val.isInt()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | return RValue::get(llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 67 | Result.Val.getInt())); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 68 | else if (Result.Val.isFloat()) |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 69 | return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat())); |
Chris Lattner | 1f32999 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 70 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 564ea2a | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 72 | switch (BuiltinID) { |
| 73 | default: break; // Handle intrinsics and libm functions below. |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 74 | case Builtin::BI__builtin___CFStringMakeConstantString: |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 75 | case Builtin::BI__builtin___NSStringMakeConstantString: |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 76 | return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0)); |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 77 | case Builtin::BI__builtin_stdarg_start: |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 78 | case Builtin::BI__builtin_va_start: |
| 79 | case Builtin::BI__builtin_va_end: { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 80 | Value *ArgValue = EmitVAListRef(E->getArg(0)); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 81 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 82 | if (ArgValue->getType() != DestType) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | ArgValue = Builder.CreateBitCast(ArgValue, DestType, |
Daniel Dunbar | b27ffbe | 2009-07-26 09:28:40 +0000 | [diff] [blame] | 84 | ArgValue->getName().data()); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 85 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ? |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 87 | Intrinsic::vaend : Intrinsic::vastart; |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 88 | return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 89 | } |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 90 | case Builtin::BI__builtin_va_copy: { |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 91 | Value *DstPtr = EmitVAListRef(E->getArg(0)); |
| 92 | Value *SrcPtr = EmitVAListRef(E->getArg(1)); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 93 | |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 94 | const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 95 | |
| 96 | DstPtr = Builder.CreateBitCast(DstPtr, Type); |
| 97 | SrcPtr = Builder.CreateBitCast(SrcPtr, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 99 | DstPtr, SrcPtr)); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 100 | } |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 101 | case Builtin::BI__builtin_abs: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 103 | |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 104 | Value *NegOp = Builder.CreateNeg(ArgValue, "neg"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | Value *CmpResult = |
| 106 | Builder.CreateICmpSGE(ArgValue, |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 107 | llvm::Constant::getNullValue(ArgValue->getType()), |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 108 | "abscond"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | Value *Result = |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 110 | Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 112 | return RValue::get(Result); |
| 113 | } |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 114 | case Builtin::BI__builtin_ctz: |
| 115 | case Builtin::BI__builtin_ctzl: |
| 116 | case Builtin::BI__builtin_ctzll: { |
| 117 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 119 | const llvm::Type *ArgType = ArgValue->getType(); |
| 120 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
| 121 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 123 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 124 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 125 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 126 | "cast"); |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 127 | return RValue::get(Result); |
| 128 | } |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 129 | case Builtin::BI__builtin_clz: |
| 130 | case Builtin::BI__builtin_clzl: |
| 131 | case Builtin::BI__builtin_clzll: { |
| 132 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 134 | const llvm::Type *ArgType = ArgValue->getType(); |
| 135 | Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1); |
| 136 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 138 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 139 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 140 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 141 | "cast"); |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 142 | return RValue::get(Result); |
| 143 | } |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 144 | case Builtin::BI__builtin_ffs: |
| 145 | case Builtin::BI__builtin_ffsl: |
| 146 | case Builtin::BI__builtin_ffsll: { |
| 147 | // ffs(x) -> x ? cttz(x) + 1 : 0 |
| 148 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 150 | const llvm::Type *ArgType = ArgValue->getType(); |
| 151 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 153 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 155 | llvm::ConstantInt::get(ArgType, 1), "tmp"); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 156 | Value *Zero = llvm::Constant::getNullValue(ArgType); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 157 | Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); |
| 158 | Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs"); |
| 159 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 160 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 161 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 162 | return RValue::get(Result); |
| 163 | } |
| 164 | case Builtin::BI__builtin_parity: |
| 165 | case Builtin::BI__builtin_parityl: |
| 166 | case Builtin::BI__builtin_parityll: { |
| 167 | // parity(x) -> ctpop(x) & 1 |
| 168 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 170 | const llvm::Type *ArgType = ArgValue->getType(); |
| 171 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 173 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 174 | Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1), |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 176 | "tmp"); |
| 177 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 178 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 179 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 180 | return RValue::get(Result); |
| 181 | } |
| 182 | case Builtin::BI__builtin_popcount: |
| 183 | case Builtin::BI__builtin_popcountl: |
| 184 | case Builtin::BI__builtin_popcountll: { |
| 185 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 187 | const llvm::Type *ArgType = ArgValue->getType(); |
| 188 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 190 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 191 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 192 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 193 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 194 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 195 | return RValue::get(Result); |
| 196 | } |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 197 | case Builtin::BI__builtin_expect: |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 198 | // FIXME: pass expect through to LLVM |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 199 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 200 | case Builtin::BI__builtin_bswap32: |
| 201 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 202 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 203 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 204 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 205 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | } |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 207 | case Builtin::BI__builtin_object_size: { |
Mike Stump | b16d32f | 2009-10-26 23:39:48 +0000 | [diff] [blame] | 208 | // We pass this builtin onto the optimizer so that it can |
| 209 | // figure out the object size in more complex cases. |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 210 | const llvm::Type *ResType[] = { |
| 211 | ConvertType(E->getType()) |
| 212 | }; |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 213 | |
| 214 | // LLVM only supports 0 and 2, make sure that we pass along that |
| 215 | // as a boolean. |
| 216 | Value *Ty = EmitScalarExpr(E->getArg(1)); |
| 217 | ConstantInt *CI = dyn_cast<ConstantInt>(Ty); |
| 218 | assert(CI); |
| 219 | uint64_t val = CI->getZExtValue(); |
| 220 | CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1); |
| 221 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 222 | Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1); |
| 223 | return RValue::get(Builder.CreateCall2(F, |
| 224 | EmitScalarExpr(E->getArg(0)), |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 225 | CI)); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 226 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 227 | case Builtin::BI__builtin_prefetch: { |
| 228 | Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); |
| 229 | // FIXME: Technically these constants should of type 'int', yes? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 231 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 233 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 3); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 234 | Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); |
| 235 | return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 236 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 237 | case Builtin::BI__builtin_trap: { |
| 238 | Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); |
| 239 | return RValue::get(Builder.CreateCall(F)); |
| 240 | } |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 241 | case Builtin::BI__builtin_unreachable: { |
Mike Stump | fba565d | 2009-12-16 03:07:12 +0000 | [diff] [blame] | 242 | if (CatchUndefined && HaveInsertPoint()) |
| 243 | EmitBranch(getTrapBB()); |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 244 | Value *V = Builder.CreateUnreachable(); |
| 245 | Builder.ClearInsertionPoint(); |
| 246 | return RValue::get(V); |
| 247 | } |
| 248 | |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 249 | case Builtin::BI__builtin_powi: |
| 250 | case Builtin::BI__builtin_powif: |
| 251 | case Builtin::BI__builtin_powil: { |
| 252 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 253 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 254 | const llvm::Type *ArgType = Base->getType(); |
| 255 | Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 256 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 257 | } |
| 258 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 259 | case Builtin::BI__builtin_isgreater: |
| 260 | case Builtin::BI__builtin_isgreaterequal: |
| 261 | case Builtin::BI__builtin_isless: |
| 262 | case Builtin::BI__builtin_islessequal: |
| 263 | case Builtin::BI__builtin_islessgreater: |
| 264 | case Builtin::BI__builtin_isunordered: { |
| 265 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 266 | // floating point values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 268 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 270 | switch (BuiltinID) { |
| 271 | default: assert(0 && "Unknown ordered comparison"); |
| 272 | case Builtin::BI__builtin_isgreater: |
| 273 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 274 | break; |
| 275 | case Builtin::BI__builtin_isgreaterequal: |
| 276 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 277 | break; |
| 278 | case Builtin::BI__builtin_isless: |
| 279 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 280 | break; |
| 281 | case Builtin::BI__builtin_islessequal: |
| 282 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 283 | break; |
| 284 | case Builtin::BI__builtin_islessgreater: |
| 285 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 286 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | case Builtin::BI__builtin_isunordered: |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 288 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 289 | break; |
| 290 | } |
| 291 | // ZExt bool to int type. |
| 292 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 293 | "tmp")); |
| 294 | } |
Eli Friedman | d613989 | 2009-09-01 04:19:44 +0000 | [diff] [blame] | 295 | case Builtin::BI__builtin_isnan: { |
| 296 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 297 | V = Builder.CreateFCmpUNO(V, V, "cmp"); |
| 298 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp")); |
| 299 | } |
Eli Friedman | b52fe9c | 2009-06-02 07:10:30 +0000 | [diff] [blame] | 300 | case Builtin::BIalloca: |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 301 | case Builtin::BI__builtin_alloca: { |
| 302 | // FIXME: LLVM IR Should allow alloca with an i64 size! |
| 303 | Value *Size = EmitScalarExpr(E->getArg(0)); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 304 | Size = Builder.CreateIntCast(Size, llvm::Type::getInt32Ty(VMContext), false, "tmp"); |
| 305 | return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp")); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 306 | } |
Eli Friedman | e6dddfd | 2010-01-23 19:00:10 +0000 | [diff] [blame] | 307 | case Builtin::BIbzero: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 308 | case Builtin::BI__builtin_bzero: { |
| 309 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 310 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 311 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0), |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 312 | EmitScalarExpr(E->getArg(1)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 313 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 314 | return RValue::get(Address); |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 315 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 316 | case Builtin::BImemcpy: |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 317 | case Builtin::BI__builtin_memcpy: { |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 318 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 319 | Builder.CreateCall4(CGM.getMemCpyFn(), Address, |
| 320 | EmitScalarExpr(E->getArg(1)), |
| 321 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 322 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 323 | return RValue::get(Address); |
| 324 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 325 | case Builtin::BImemmove: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 326 | case Builtin::BI__builtin_memmove: { |
| 327 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 328 | Builder.CreateCall4(CGM.getMemMoveFn(), Address, |
| 329 | EmitScalarExpr(E->getArg(1)), |
| 330 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 331 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 332 | return RValue::get(Address); |
| 333 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 334 | case Builtin::BImemset: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 335 | case Builtin::BI__builtin_memset: { |
| 336 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 337 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Daniel Dunbar | 62c29c6 | 2009-02-16 21:52:05 +0000 | [diff] [blame] | 338 | Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 339 | llvm::Type::getInt8Ty(VMContext)), |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 340 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 341 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 342 | return RValue::get(Address); |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 343 | } |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 344 | case Builtin::BI__builtin_return_address: { |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 345 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 346 | Depth = Builder.CreateIntCast(Depth, |
| 347 | llvm::Type::getInt32Ty(VMContext), |
| 348 | false, "tmp"); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 349 | Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 350 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 351 | } |
| 352 | case Builtin::BI__builtin_frame_address: { |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 353 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 354 | Depth = Builder.CreateIntCast(Depth, |
| 355 | llvm::Type::getInt32Ty(VMContext), |
| 356 | false, "tmp"); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 357 | Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 358 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 359 | } |
Eli Friedman | 3b660ef | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 360 | case Builtin::BI__builtin_extract_return_addr: { |
| 361 | // FIXME: There should be a target hook for this |
| 362 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
| 363 | } |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 364 | case Builtin::BI__builtin_unwind_init: { |
| 365 | Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0); |
| 366 | return RValue::get(Builder.CreateCall(F)); |
| 367 | } |
John McCall | 5e11085 | 2010-03-02 02:31:24 +0000 | [diff] [blame^] | 368 | case Builtin::BI__builtin_extend_pointer: { |
| 369 | // Extends a pointer to the size of an _Unwind_Word, which is |
| 370 | // generally a uint64_t. Generally this gets poked directly into |
| 371 | // a register (or a "register" depending on platform) and then |
| 372 | // called, so if the pointer is shorter than a word we need to |
| 373 | // zext / sext based on the platform's expectations for pointers |
| 374 | // in registers. |
| 375 | // |
| 376 | // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html |
| 377 | // |
| 378 | // FIXME: ptrtoint always zexts; use a target hook if we start |
| 379 | // supporting targets where this matters. |
| 380 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 381 | const llvm::Type *Ty = CGM.getTypes().ConvertType(E->getType()); |
| 382 | return RValue::get(Builder.CreatePtrToInt(Ptr, Ty)); |
| 383 | } |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 384 | #if 0 |
| 385 | // FIXME: Finish/enable when LLVM backend support stabilizes |
| 386 | case Builtin::BI__builtin_setjmp: { |
| 387 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
| 388 | // Store the frame pointer to the buffer |
| 389 | Value *FrameAddrF = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
| 390 | Value *FrameAddr = |
| 391 | Builder.CreateCall(FrameAddrF, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 392 | Constant::getNullValue(llvm::Type::getInt32Ty(VMContext))); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 393 | Builder.CreateStore(FrameAddr, Buf); |
| 394 | // Call the setjmp intrinsic |
| 395 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp, 0, 0); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 396 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 397 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 398 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 399 | } |
| 400 | case Builtin::BI__builtin_longjmp: { |
| 401 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp, 0, 0); |
| 402 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 403 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 404 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 405 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 406 | } |
| 407 | #endif |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 408 | case Builtin::BI__sync_fetch_and_add: |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 409 | case Builtin::BI__sync_fetch_and_sub: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 410 | case Builtin::BI__sync_fetch_and_or: |
| 411 | case Builtin::BI__sync_fetch_and_and: |
| 412 | case Builtin::BI__sync_fetch_and_xor: |
| 413 | case Builtin::BI__sync_add_and_fetch: |
| 414 | case Builtin::BI__sync_sub_and_fetch: |
| 415 | case Builtin::BI__sync_and_and_fetch: |
| 416 | case Builtin::BI__sync_or_and_fetch: |
| 417 | case Builtin::BI__sync_xor_and_fetch: |
| 418 | case Builtin::BI__sync_val_compare_and_swap: |
| 419 | case Builtin::BI__sync_bool_compare_and_swap: |
| 420 | case Builtin::BI__sync_lock_test_and_set: |
| 421 | case Builtin::BI__sync_lock_release: |
| 422 | assert(0 && "Shouldn't make it through sema"); |
| 423 | case Builtin::BI__sync_fetch_and_add_1: |
| 424 | case Builtin::BI__sync_fetch_and_add_2: |
| 425 | case Builtin::BI__sync_fetch_and_add_4: |
| 426 | case Builtin::BI__sync_fetch_and_add_8: |
| 427 | case Builtin::BI__sync_fetch_and_add_16: |
| 428 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); |
| 429 | case Builtin::BI__sync_fetch_and_sub_1: |
| 430 | case Builtin::BI__sync_fetch_and_sub_2: |
| 431 | case Builtin::BI__sync_fetch_and_sub_4: |
| 432 | case Builtin::BI__sync_fetch_and_sub_8: |
| 433 | case Builtin::BI__sync_fetch_and_sub_16: |
Mon P Wang | 09b6bf5 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 434 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 435 | case Builtin::BI__sync_fetch_and_or_1: |
| 436 | case Builtin::BI__sync_fetch_and_or_2: |
| 437 | case Builtin::BI__sync_fetch_and_or_4: |
| 438 | case Builtin::BI__sync_fetch_and_or_8: |
| 439 | case Builtin::BI__sync_fetch_and_or_16: |
| 440 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 441 | case Builtin::BI__sync_fetch_and_and_1: |
| 442 | case Builtin::BI__sync_fetch_and_and_2: |
| 443 | case Builtin::BI__sync_fetch_and_and_4: |
| 444 | case Builtin::BI__sync_fetch_and_and_8: |
| 445 | case Builtin::BI__sync_fetch_and_and_16: |
| 446 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 447 | case Builtin::BI__sync_fetch_and_xor_1: |
| 448 | case Builtin::BI__sync_fetch_and_xor_2: |
| 449 | case Builtin::BI__sync_fetch_and_xor_4: |
| 450 | case Builtin::BI__sync_fetch_and_xor_8: |
| 451 | case Builtin::BI__sync_fetch_and_xor_16: |
| 452 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 453 | case Builtin::BI__sync_fetch_and_nand_1: |
| 454 | case Builtin::BI__sync_fetch_and_nand_2: |
| 455 | case Builtin::BI__sync_fetch_and_nand_4: |
| 456 | case Builtin::BI__sync_fetch_and_nand_8: |
| 457 | case Builtin::BI__sync_fetch_and_nand_16: |
| 458 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 460 | // Clang extensions: not overloaded yet. |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 461 | case Builtin::BI__sync_fetch_and_min: |
| 462 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 463 | case Builtin::BI__sync_fetch_and_max: |
| 464 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 465 | case Builtin::BI__sync_fetch_and_umin: |
| 466 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 467 | case Builtin::BI__sync_fetch_and_umax: |
| 468 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 470 | case Builtin::BI__sync_add_and_fetch_1: |
| 471 | case Builtin::BI__sync_add_and_fetch_2: |
| 472 | case Builtin::BI__sync_add_and_fetch_4: |
| 473 | case Builtin::BI__sync_add_and_fetch_8: |
| 474 | case Builtin::BI__sync_add_and_fetch_16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 476 | llvm::Instruction::Add); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 477 | case Builtin::BI__sync_sub_and_fetch_1: |
| 478 | case Builtin::BI__sync_sub_and_fetch_2: |
| 479 | case Builtin::BI__sync_sub_and_fetch_4: |
| 480 | case Builtin::BI__sync_sub_and_fetch_8: |
| 481 | case Builtin::BI__sync_sub_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 482 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, |
| 483 | llvm::Instruction::Sub); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 484 | case Builtin::BI__sync_and_and_fetch_1: |
| 485 | case Builtin::BI__sync_and_and_fetch_2: |
| 486 | case Builtin::BI__sync_and_and_fetch_4: |
| 487 | case Builtin::BI__sync_and_and_fetch_8: |
| 488 | case Builtin::BI__sync_and_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 489 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, |
| 490 | llvm::Instruction::And); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 491 | case Builtin::BI__sync_or_and_fetch_1: |
| 492 | case Builtin::BI__sync_or_and_fetch_2: |
| 493 | case Builtin::BI__sync_or_and_fetch_4: |
| 494 | case Builtin::BI__sync_or_and_fetch_8: |
| 495 | case Builtin::BI__sync_or_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 496 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, |
| 497 | llvm::Instruction::Or); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 498 | case Builtin::BI__sync_xor_and_fetch_1: |
| 499 | case Builtin::BI__sync_xor_and_fetch_2: |
| 500 | case Builtin::BI__sync_xor_and_fetch_4: |
| 501 | case Builtin::BI__sync_xor_and_fetch_8: |
| 502 | case Builtin::BI__sync_xor_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 503 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, |
| 504 | llvm::Instruction::Xor); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 505 | case Builtin::BI__sync_nand_and_fetch_1: |
| 506 | case Builtin::BI__sync_nand_and_fetch_2: |
| 507 | case Builtin::BI__sync_nand_and_fetch_4: |
| 508 | case Builtin::BI__sync_nand_and_fetch_8: |
| 509 | case Builtin::BI__sync_nand_and_fetch_16: |
| 510 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E, |
| 511 | llvm::Instruction::And); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 513 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 514 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 515 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 516 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 517 | case Builtin::BI__sync_val_compare_and_swap_16: |
| 518 | { |
Mon P Wang | c500451 | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 519 | const llvm::Type *ResType[2]; |
| 520 | ResType[0]= ConvertType(E->getType()); |
| 521 | ResType[1] = ConvertType(E->getArg(0)->getType()); |
| 522 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | return RValue::get(Builder.CreateCall3(AtomF, |
Anders Carlsson | 9584446 | 2008-08-30 16:17:45 +0000 | [diff] [blame] | 524 | EmitScalarExpr(E->getArg(0)), |
| 525 | EmitScalarExpr(E->getArg(1)), |
| 526 | EmitScalarExpr(E->getArg(2)))); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 527 | } |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 529 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 530 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 531 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 532 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 533 | case Builtin::BI__sync_bool_compare_and_swap_16: |
| 534 | { |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 535 | const llvm::Type *ResType[2]; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 536 | ResType[0]= ConvertType(E->getArg(1)->getType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 537 | ResType[1] = llvm::PointerType::getUnqual(ResType[0]); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 538 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
| 539 | Value *OldVal = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | Value *PrevVal = Builder.CreateCall3(AtomF, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 541 | EmitScalarExpr(E->getArg(0)), |
| 542 | OldVal, |
| 543 | EmitScalarExpr(E->getArg(2))); |
| 544 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 545 | // zext bool to int. |
| 546 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 547 | } |
| 548 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 549 | case Builtin::BI__sync_lock_test_and_set_1: |
| 550 | case Builtin::BI__sync_lock_test_and_set_2: |
| 551 | case Builtin::BI__sync_lock_test_and_set_4: |
| 552 | case Builtin::BI__sync_lock_test_and_set_8: |
| 553 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 554 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 555 | case Builtin::BI__sync_lock_release_1: |
| 556 | case Builtin::BI__sync_lock_release_2: |
| 557 | case Builtin::BI__sync_lock_release_4: |
| 558 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 559 | case Builtin::BI__sync_lock_release_16: { |
| 560 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 561 | const llvm::Type *ElTy = |
| 562 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Daniel Dunbar | 007b567 | 2009-11-29 21:11:47 +0000 | [diff] [blame] | 563 | llvm::StoreInst *Store = |
| 564 | Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr); |
| 565 | Store->setVolatile(true); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 566 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 567 | } |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 568 | |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 569 | case Builtin::BI__sync_synchronize: { |
| 570 | Value *C[5]; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 571 | C[0] = C[1] = C[2] = C[3] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 1); |
| 572 | C[4] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 573 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 574 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Tanya Lattner | 0b57164 | 2010-01-16 01:21:14 +0000 | [diff] [blame] | 577 | case Builtin::BI__builtin_llvm_memory_barrier: { |
| 578 | Value *C[5] = { |
| 579 | EmitScalarExpr(E->getArg(0)), |
| 580 | EmitScalarExpr(E->getArg(1)), |
| 581 | EmitScalarExpr(E->getArg(2)), |
| 582 | EmitScalarExpr(E->getArg(3)), |
| 583 | EmitScalarExpr(E->getArg(4)) |
| 584 | }; |
| 585 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
| 586 | return RValue::get(0); |
| 587 | } |
| 588 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 589 | // Library functions with special handling. |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 590 | case Builtin::BIsqrt: |
| 591 | case Builtin::BIsqrtf: |
| 592 | case Builtin::BIsqrtl: { |
| 593 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 594 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 595 | break; |
| 596 | Value *Arg0 = EmitScalarExpr(E->getArg(0)); |
| 597 | const llvm::Type *ArgType = Arg0->getType(); |
| 598 | Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1); |
| 599 | return RValue::get(Builder.CreateCall(F, Arg0, "tmp")); |
| 600 | } |
| 601 | |
| 602 | case Builtin::BIpow: |
| 603 | case Builtin::BIpowf: |
| 604 | case Builtin::BIpowl: { |
| 605 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 606 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 607 | break; |
| 608 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 609 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 610 | const llvm::Type *ArgType = Base->getType(); |
| 611 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 612 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 613 | } |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 614 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 616 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 617 | // that function. |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 618 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 619 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 620 | return EmitCall(E->getCallee()->getType(), |
| 621 | CGM.getBuiltinLibFunction(FD, BuiltinID), |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 622 | ReturnValueSlot(), |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 623 | E->arg_begin(), E->arg_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 625 | // See if we have a target specific intrinsic. |
Dale Johannesen | a6f80ef | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 626 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 627 | Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic; |
| 628 | if (const char *Prefix = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | llvm::Triple::getArchTypePrefix(Target.getTriple().getArch())) |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 630 | IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 632 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 633 | SmallVector<Value*, 16> Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 635 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 636 | const llvm::FunctionType *FTy = F->getFunctionType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 638 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 639 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 641 | // If the intrinsic arg type is different from the builtin arg type |
| 642 | // we need to do a bit cast. |
| 643 | const llvm::Type *PTy = FTy->getParamType(i); |
| 644 | if (PTy != ArgValue->getType()) { |
| 645 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 646 | "Must be able to losslessly bit cast to param"); |
| 647 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 648 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 650 | Args.push_back(ArgValue); |
| 651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 653 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 654 | QualType BuiltinRetType = E->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 656 | const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 657 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 659 | if (RetTy != V->getType()) { |
| 660 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 661 | "Must be able to losslessly bit cast result type"); |
| 662 | V = Builder.CreateBitCast(V, RetTy); |
| 663 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 665 | return RValue::get(V); |
| 666 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 668 | // See if we have a target specific builtin that needs to be lowered. |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 669 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 670 | return RValue::get(V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 672 | ErrorUnsupported(E, "builtin function"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 674 | // Unknown builtin, for now just dump it out and return undef. |
| 675 | if (hasAggregateLLVMType(E->getType())) |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 676 | return RValue::getAggregate(CreateMemTemp(E->getType())); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 677 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 679 | |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 680 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 681 | const CallExpr *E) { |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 682 | switch (Target.getTriple().getArch()) { |
| 683 | case llvm::Triple::x86: |
| 684 | case llvm::Triple::x86_64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 685 | return EmitX86BuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 686 | case llvm::Triple::ppc: |
| 687 | case llvm::Triple::ppc64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 688 | return EmitPPCBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 689 | default: |
| 690 | return 0; |
| 691 | } |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 695 | const CallExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 697 | llvm::SmallVector<Value*, 4> Ops; |
| 698 | |
| 699 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 700 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 701 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 702 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 703 | default: return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | case X86::BI__builtin_ia32_pslldi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 705 | case X86::BI__builtin_ia32_psllqi128: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | case X86::BI__builtin_ia32_psllwi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 707 | case X86::BI__builtin_ia32_psradi128: |
| 708 | case X86::BI__builtin_ia32_psrawi128: |
| 709 | case X86::BI__builtin_ia32_psrldi128: |
| 710 | case X86::BI__builtin_ia32_psrlqi128: |
| 711 | case X86::BI__builtin_ia32_psrlwi128: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 712 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 713 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 2); |
| 714 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 715 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 716 | Ops[1], Zero, "insert"); |
| 717 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 718 | const char *name = 0; |
| 719 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 721 | switch (BuiltinID) { |
| 722 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 723 | case X86::BI__builtin_ia32_pslldi128: |
| 724 | name = "pslldi"; |
| 725 | ID = Intrinsic::x86_sse2_psll_d; |
| 726 | break; |
| 727 | case X86::BI__builtin_ia32_psllqi128: |
| 728 | name = "psllqi"; |
| 729 | ID = Intrinsic::x86_sse2_psll_q; |
| 730 | break; |
| 731 | case X86::BI__builtin_ia32_psllwi128: |
| 732 | name = "psllwi"; |
| 733 | ID = Intrinsic::x86_sse2_psll_w; |
| 734 | break; |
| 735 | case X86::BI__builtin_ia32_psradi128: |
| 736 | name = "psradi"; |
| 737 | ID = Intrinsic::x86_sse2_psra_d; |
| 738 | break; |
| 739 | case X86::BI__builtin_ia32_psrawi128: |
| 740 | name = "psrawi"; |
| 741 | ID = Intrinsic::x86_sse2_psra_w; |
| 742 | break; |
| 743 | case X86::BI__builtin_ia32_psrldi128: |
| 744 | name = "psrldi"; |
| 745 | ID = Intrinsic::x86_sse2_psrl_d; |
| 746 | break; |
| 747 | case X86::BI__builtin_ia32_psrlqi128: |
| 748 | name = "psrlqi"; |
| 749 | ID = Intrinsic::x86_sse2_psrl_q; |
| 750 | break; |
| 751 | case X86::BI__builtin_ia32_psrlwi128: |
| 752 | name = "psrlwi"; |
| 753 | ID = Intrinsic::x86_sse2_psrl_w; |
| 754 | break; |
| 755 | } |
| 756 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 758 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | case X86::BI__builtin_ia32_pslldi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 760 | case X86::BI__builtin_ia32_psllqi: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | case X86::BI__builtin_ia32_psllwi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 762 | case X86::BI__builtin_ia32_psradi: |
| 763 | case X86::BI__builtin_ia32_psrawi: |
| 764 | case X86::BI__builtin_ia32_psrldi: |
| 765 | case X86::BI__builtin_ia32_psrlqi: |
| 766 | case X86::BI__builtin_ia32_psrlwi: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 767 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 768 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 1); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 769 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 770 | const char *name = 0; |
| 771 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 773 | switch (BuiltinID) { |
| 774 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 775 | case X86::BI__builtin_ia32_pslldi: |
| 776 | name = "pslldi"; |
| 777 | ID = Intrinsic::x86_mmx_psll_d; |
| 778 | break; |
| 779 | case X86::BI__builtin_ia32_psllqi: |
| 780 | name = "psllqi"; |
| 781 | ID = Intrinsic::x86_mmx_psll_q; |
| 782 | break; |
| 783 | case X86::BI__builtin_ia32_psllwi: |
| 784 | name = "psllwi"; |
| 785 | ID = Intrinsic::x86_mmx_psll_w; |
| 786 | break; |
| 787 | case X86::BI__builtin_ia32_psradi: |
| 788 | name = "psradi"; |
| 789 | ID = Intrinsic::x86_mmx_psra_d; |
| 790 | break; |
| 791 | case X86::BI__builtin_ia32_psrawi: |
| 792 | name = "psrawi"; |
| 793 | ID = Intrinsic::x86_mmx_psra_w; |
| 794 | break; |
| 795 | case X86::BI__builtin_ia32_psrldi: |
| 796 | name = "psrldi"; |
| 797 | ID = Intrinsic::x86_mmx_psrl_d; |
| 798 | break; |
| 799 | case X86::BI__builtin_ia32_psrlqi: |
| 800 | name = "psrlqi"; |
| 801 | ID = Intrinsic::x86_mmx_psrl_q; |
| 802 | break; |
| 803 | case X86::BI__builtin_ia32_psrlwi: |
| 804 | name = "psrlwi"; |
| 805 | ID = Intrinsic::x86_mmx_psrl_w; |
| 806 | break; |
| 807 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 808 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 810 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 811 | case X86::BI__builtin_ia32_cmpps: { |
| 812 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 813 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 814 | } |
| 815 | case X86::BI__builtin_ia32_cmpss: { |
| 816 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 817 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 818 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 819 | case X86::BI__builtin_ia32_ldmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 820 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 821 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 822 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 823 | Builder.CreateStore(Ops[0], Tmp); |
| 824 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 825 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 826 | } |
| 827 | case X86::BI__builtin_ia32_stmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 828 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 829 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 830 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 831 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 832 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 833 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 834 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 835 | case X86::BI__builtin_ia32_cmppd: { |
| 836 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 837 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 838 | } |
| 839 | case X86::BI__builtin_ia32_cmpsd: { |
| 840 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 841 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 842 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 843 | case X86::BI__builtin_ia32_storehps: |
| 844 | case X86::BI__builtin_ia32_storelps: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 845 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 846 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 847 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 849 | // cast val v2i64 |
| 850 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 852 | // extract (0, 1) |
| 853 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 854 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Index); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 855 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 856 | |
| 857 | // cast pointer to i64 & store |
| 858 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 859 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 860 | } |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 861 | case X86::BI__builtin_ia32_palignr: { |
Nate Begeman | c3420ff | 2009-12-14 05:15:02 +0000 | [diff] [blame] | 862 | Function *F = CGM.getIntrinsic(Intrinsic::x86_ssse3_palign_r); |
| 863 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size()); |
| 864 | } |
| 865 | case X86::BI__builtin_ia32_palignr128: { |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 866 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 867 | |
| 868 | // If palignr is shifting the pair of input vectors less than 17 bytes, |
| 869 | // emit a shuffle instruction. |
| 870 | if (shiftVal <= 16) { |
| 871 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 872 | |
| 873 | llvm::SmallVector<llvm::Constant*, 16> Indices; |
| 874 | for (unsigned i = 0; i != 16; ++i) |
| 875 | Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i)); |
| 876 | |
| 877 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 878 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 879 | } |
| 880 | |
| 881 | // If palignr is shifting the pair of input vectors more than 16 but less |
| 882 | // than 32 bytes, emit a logical right shift of the destination. |
| 883 | if (shiftVal < 32) { |
| 884 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
| 885 | const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 886 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 887 | |
| 888 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 889 | Ops[1] = llvm::ConstantInt::get(IntTy, (shiftVal-16) * 8); |
| 890 | |
| 891 | // create i32 constant |
| 892 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq); |
| 893 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 894 | } |
| 895 | |
| 896 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 897 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 898 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 903 | const CallExpr *E) { |
Daniel Dunbar | b0b8438 | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 904 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | } |