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. |
| 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); |
| 34 | return RValue::get(CGF.Builder.CreateCall2(AtomF, |
| 35 | CGF.EmitScalarExpr(E->getArg(0)), |
| 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. |
| 42 | static RValue EmitBinaryAtomicPost(CodeGenFunction& CGF, |
| 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); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 52 | |
| 53 | if (Id == Intrinsic::atomic_load_nand) |
| 54 | Result = CGF.Builder.CreateNot(Result); |
| 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 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 60 | RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, |
| 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()) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 66 | return RValue::get(VMContext.getConstantInt(Result.Val.getInt())); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 67 | else if (Result.Val.isFloat()) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 68 | return RValue::get(VMContext.getConstantFP(Result.Val.getFloat())); |
Chris Lattner | 1f32999 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 564ea2a | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 71 | switch (BuiltinID) { |
| 72 | default: break; // Handle intrinsics and libm functions below. |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 73 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 74 | return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0)); |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 75 | case Builtin::BI__builtin_stdarg_start: |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 76 | case Builtin::BI__builtin_va_start: |
| 77 | case Builtin::BI__builtin_va_end: { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 78 | Value *ArgValue = EmitVAListRef(E->getArg(0)); |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 79 | const llvm::Type *DestType = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 80 | VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 81 | if (ArgValue->getType() != DestType) |
| 82 | ArgValue = Builder.CreateBitCast(ArgValue, DestType, |
| 83 | ArgValue->getNameStart()); |
| 84 | |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 85 | Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ? |
| 86 | Intrinsic::vaend : Intrinsic::vastart; |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 87 | return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 88 | } |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 89 | case Builtin::BI__builtin_va_copy: { |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 90 | Value *DstPtr = EmitVAListRef(E->getArg(0)); |
| 91 | Value *SrcPtr = EmitVAListRef(E->getArg(1)); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 92 | |
| 93 | const llvm::Type *Type = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 94 | VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); |
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); |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 98 | return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy), |
| 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: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 102 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 104 | Value *NegOp = Builder.CreateNeg(ArgValue, "neg"); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 105 | Value *CmpResult = |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 106 | Builder.CreateICmpSGE(ArgValue, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 107 | VMContext.getNullValue(ArgValue->getType()), |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 108 | "abscond"); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +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"); |
| 111 | |
| 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)); |
| 118 | |
| 119 | const llvm::Type *ArgType = ArgValue->getType(); |
| 120 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
| 121 | |
| 122 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 123 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 124 | if (Result->getType() != ResultType) |
| 125 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 126 | return RValue::get(Result); |
| 127 | } |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin_clz: |
| 129 | case Builtin::BI__builtin_clzl: |
| 130 | case Builtin::BI__builtin_clzll: { |
| 131 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 132 | |
| 133 | const llvm::Type *ArgType = ArgValue->getType(); |
| 134 | Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1); |
| 135 | |
| 136 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 137 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 138 | if (Result->getType() != ResultType) |
| 139 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 140 | return RValue::get(Result); |
| 141 | } |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 142 | case Builtin::BI__builtin_ffs: |
| 143 | case Builtin::BI__builtin_ffsl: |
| 144 | case Builtin::BI__builtin_ffsll: { |
| 145 | // ffs(x) -> x ? cttz(x) + 1 : 0 |
| 146 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 147 | |
| 148 | const llvm::Type *ArgType = ArgValue->getType(); |
| 149 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
| 150 | |
| 151 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 152 | Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 153 | VMContext.getConstantInt(ArgType, 1), "tmp"); |
| 154 | Value *Zero = VMContext.getNullValue(ArgType); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 155 | Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); |
| 156 | Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs"); |
| 157 | if (Result->getType() != ResultType) |
| 158 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 159 | return RValue::get(Result); |
| 160 | } |
| 161 | case Builtin::BI__builtin_parity: |
| 162 | case Builtin::BI__builtin_parityl: |
| 163 | case Builtin::BI__builtin_parityll: { |
| 164 | // parity(x) -> ctpop(x) & 1 |
| 165 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 166 | |
| 167 | const llvm::Type *ArgType = ArgValue->getType(); |
| 168 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
| 169 | |
| 170 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 171 | Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 172 | Value *Result = Builder.CreateAnd(Tmp, VMContext.getConstantInt(ArgType, 1), |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 173 | "tmp"); |
| 174 | if (Result->getType() != ResultType) |
| 175 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 176 | return RValue::get(Result); |
| 177 | } |
| 178 | case Builtin::BI__builtin_popcount: |
| 179 | case Builtin::BI__builtin_popcountl: |
| 180 | case Builtin::BI__builtin_popcountll: { |
| 181 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 182 | |
| 183 | const llvm::Type *ArgType = ArgValue->getType(); |
| 184 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
| 185 | |
| 186 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 187 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 188 | if (Result->getType() != ResultType) |
| 189 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 190 | return RValue::get(Result); |
| 191 | } |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 192 | case Builtin::BI__builtin_expect: |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 193 | // FIXME: pass expect through to LLVM |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 194 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 195 | case Builtin::BI__builtin_bswap32: |
| 196 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 197 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 198 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 199 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 200 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 201 | } |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 202 | case Builtin::BI__builtin_object_size: { |
| 203 | // FIXME: Implement. For now we just always fail and pretend we |
| 204 | // don't know the object size. |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 205 | llvm::APSInt TypeArg = E->getArg(1)->EvaluateAsInt(CGM.getContext()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 206 | const llvm::Type *ResType = ConvertType(E->getType()); |
| 207 | // bool UseSubObject = TypeArg.getZExtValue() & 1; |
| 208 | bool UseMinimum = TypeArg.getZExtValue() & 2; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 209 | return RValue::get( |
| 210 | VMContext.getConstantInt(ResType, UseMinimum ? 0 : -1LL)); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 211 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 212 | case Builtin::BI__builtin_prefetch: { |
| 213 | Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); |
| 214 | // FIXME: Technically these constants should of type 'int', yes? |
| 215 | RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 216 | VMContext.getConstantInt(llvm::Type::Int32Ty, 0); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 217 | Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 218 | VMContext.getConstantInt(llvm::Type::Int32Ty, 3); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 219 | Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); |
| 220 | return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 221 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 222 | case Builtin::BI__builtin_trap: { |
| 223 | Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); |
| 224 | return RValue::get(Builder.CreateCall(F)); |
| 225 | } |
| 226 | |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 227 | case Builtin::BI__builtin_powi: |
| 228 | case Builtin::BI__builtin_powif: |
| 229 | case Builtin::BI__builtin_powil: { |
| 230 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 231 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 232 | const llvm::Type *ArgType = Base->getType(); |
| 233 | Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 234 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 235 | } |
| 236 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 237 | case Builtin::BI__builtin_isgreater: |
| 238 | case Builtin::BI__builtin_isgreaterequal: |
| 239 | case Builtin::BI__builtin_isless: |
| 240 | case Builtin::BI__builtin_islessequal: |
| 241 | case Builtin::BI__builtin_islessgreater: |
| 242 | case Builtin::BI__builtin_isunordered: { |
| 243 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 244 | // floating point values. |
| 245 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
| 246 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
| 247 | |
| 248 | switch (BuiltinID) { |
| 249 | default: assert(0 && "Unknown ordered comparison"); |
| 250 | case Builtin::BI__builtin_isgreater: |
| 251 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 252 | break; |
| 253 | case Builtin::BI__builtin_isgreaterequal: |
| 254 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 255 | break; |
| 256 | case Builtin::BI__builtin_isless: |
| 257 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 258 | break; |
| 259 | case Builtin::BI__builtin_islessequal: |
| 260 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 261 | break; |
| 262 | case Builtin::BI__builtin_islessgreater: |
| 263 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 264 | break; |
| 265 | case Builtin::BI__builtin_isunordered: |
| 266 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 267 | break; |
| 268 | } |
| 269 | // ZExt bool to int type. |
| 270 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 271 | "tmp")); |
| 272 | } |
Eli Friedman | b52fe9c | 2009-06-02 07:10:30 +0000 | [diff] [blame] | 273 | case Builtin::BIalloca: |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 274 | case Builtin::BI__builtin_alloca: { |
| 275 | // FIXME: LLVM IR Should allow alloca with an i64 size! |
| 276 | Value *Size = EmitScalarExpr(E->getArg(0)); |
| 277 | Size = Builder.CreateIntCast(Size, llvm::Type::Int32Ty, false, "tmp"); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 278 | return RValue::get(Builder.CreateAlloca(llvm::Type::Int8Ty, Size, "tmp")); |
| 279 | } |
| 280 | case Builtin::BI__builtin_bzero: { |
| 281 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 282 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 283 | VMContext.getConstantInt(llvm::Type::Int8Ty, 0), |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 284 | EmitScalarExpr(E->getArg(1)), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 285 | VMContext.getConstantInt(llvm::Type::Int32Ty, 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 286 | return RValue::get(Address); |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 287 | } |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 288 | case Builtin::BI__builtin_memcpy: { |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 289 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 290 | Builder.CreateCall4(CGM.getMemCpyFn(), Address, |
| 291 | EmitScalarExpr(E->getArg(1)), |
| 292 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 293 | VMContext.getConstantInt(llvm::Type::Int32Ty, 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 294 | return RValue::get(Address); |
| 295 | } |
| 296 | case Builtin::BI__builtin_memmove: { |
| 297 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 298 | Builder.CreateCall4(CGM.getMemMoveFn(), Address, |
| 299 | EmitScalarExpr(E->getArg(1)), |
| 300 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 301 | VMContext.getConstantInt(llvm::Type::Int32Ty, 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 302 | return RValue::get(Address); |
| 303 | } |
| 304 | case Builtin::BI__builtin_memset: { |
| 305 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 306 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Daniel Dunbar | 62c29c6 | 2009-02-16 21:52:05 +0000 | [diff] [blame] | 307 | Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), |
| 308 | llvm::Type::Int8Ty), |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 309 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 310 | VMContext.getConstantInt(llvm::Type::Int32Ty, 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 311 | return RValue::get(Address); |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 312 | } |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 313 | case Builtin::BI__builtin_return_address: { |
| 314 | Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); |
| 315 | return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); |
| 316 | } |
| 317 | case Builtin::BI__builtin_frame_address: { |
| 318 | Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
| 319 | return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); |
| 320 | } |
Eli Friedman | 3b660ef | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 321 | case Builtin::BI__builtin_extract_return_addr: { |
| 322 | // FIXME: There should be a target hook for this |
| 323 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
| 324 | } |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 325 | case Builtin::BI__builtin_unwind_init: { |
| 326 | Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0); |
| 327 | return RValue::get(Builder.CreateCall(F)); |
| 328 | } |
| 329 | #if 0 |
| 330 | // FIXME: Finish/enable when LLVM backend support stabilizes |
| 331 | case Builtin::BI__builtin_setjmp: { |
| 332 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
| 333 | // Store the frame pointer to the buffer |
| 334 | Value *FrameAddrF = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
| 335 | Value *FrameAddr = |
| 336 | Builder.CreateCall(FrameAddrF, |
| 337 | Constant::getNullValue(llvm::Type::Int32Ty)); |
| 338 | Builder.CreateStore(FrameAddr, Buf); |
| 339 | // Call the setjmp intrinsic |
| 340 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp, 0, 0); |
| 341 | const llvm::Type *DestType = |
| 342 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 343 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 344 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 345 | } |
| 346 | case Builtin::BI__builtin_longjmp: { |
| 347 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp, 0, 0); |
| 348 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
| 349 | const llvm::Type *DestType = |
| 350 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 351 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 352 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 353 | } |
| 354 | #endif |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 355 | case Builtin::BI__sync_fetch_and_add: |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 356 | case Builtin::BI__sync_fetch_and_sub: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 357 | case Builtin::BI__sync_fetch_and_or: |
| 358 | case Builtin::BI__sync_fetch_and_and: |
| 359 | case Builtin::BI__sync_fetch_and_xor: |
| 360 | case Builtin::BI__sync_add_and_fetch: |
| 361 | case Builtin::BI__sync_sub_and_fetch: |
| 362 | case Builtin::BI__sync_and_and_fetch: |
| 363 | case Builtin::BI__sync_or_and_fetch: |
| 364 | case Builtin::BI__sync_xor_and_fetch: |
| 365 | case Builtin::BI__sync_val_compare_and_swap: |
| 366 | case Builtin::BI__sync_bool_compare_and_swap: |
| 367 | case Builtin::BI__sync_lock_test_and_set: |
| 368 | case Builtin::BI__sync_lock_release: |
| 369 | assert(0 && "Shouldn't make it through sema"); |
| 370 | case Builtin::BI__sync_fetch_and_add_1: |
| 371 | case Builtin::BI__sync_fetch_and_add_2: |
| 372 | case Builtin::BI__sync_fetch_and_add_4: |
| 373 | case Builtin::BI__sync_fetch_and_add_8: |
| 374 | case Builtin::BI__sync_fetch_and_add_16: |
| 375 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); |
| 376 | case Builtin::BI__sync_fetch_and_sub_1: |
| 377 | case Builtin::BI__sync_fetch_and_sub_2: |
| 378 | case Builtin::BI__sync_fetch_and_sub_4: |
| 379 | case Builtin::BI__sync_fetch_and_sub_8: |
| 380 | case Builtin::BI__sync_fetch_and_sub_16: |
Mon P Wang | 09b6bf5 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 381 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 382 | case Builtin::BI__sync_fetch_and_or_1: |
| 383 | case Builtin::BI__sync_fetch_and_or_2: |
| 384 | case Builtin::BI__sync_fetch_and_or_4: |
| 385 | case Builtin::BI__sync_fetch_and_or_8: |
| 386 | case Builtin::BI__sync_fetch_and_or_16: |
| 387 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 388 | case Builtin::BI__sync_fetch_and_and_1: |
| 389 | case Builtin::BI__sync_fetch_and_and_2: |
| 390 | case Builtin::BI__sync_fetch_and_and_4: |
| 391 | case Builtin::BI__sync_fetch_and_and_8: |
| 392 | case Builtin::BI__sync_fetch_and_and_16: |
| 393 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 394 | case Builtin::BI__sync_fetch_and_xor_1: |
| 395 | case Builtin::BI__sync_fetch_and_xor_2: |
| 396 | case Builtin::BI__sync_fetch_and_xor_4: |
| 397 | case Builtin::BI__sync_fetch_and_xor_8: |
| 398 | case Builtin::BI__sync_fetch_and_xor_16: |
| 399 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 400 | case Builtin::BI__sync_fetch_and_nand_1: |
| 401 | case Builtin::BI__sync_fetch_and_nand_2: |
| 402 | case Builtin::BI__sync_fetch_and_nand_4: |
| 403 | case Builtin::BI__sync_fetch_and_nand_8: |
| 404 | case Builtin::BI__sync_fetch_and_nand_16: |
| 405 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E); |
| 406 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 407 | // Clang extensions: not overloaded yet. |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 408 | case Builtin::BI__sync_fetch_and_min: |
| 409 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 410 | case Builtin::BI__sync_fetch_and_max: |
| 411 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 412 | case Builtin::BI__sync_fetch_and_umin: |
| 413 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 414 | case Builtin::BI__sync_fetch_and_umax: |
| 415 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 417 | case Builtin::BI__sync_add_and_fetch_1: |
| 418 | case Builtin::BI__sync_add_and_fetch_2: |
| 419 | case Builtin::BI__sync_add_and_fetch_4: |
| 420 | case Builtin::BI__sync_add_and_fetch_8: |
| 421 | case Builtin::BI__sync_add_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 422 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, |
| 423 | llvm::Instruction::Add); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 424 | case Builtin::BI__sync_sub_and_fetch_1: |
| 425 | case Builtin::BI__sync_sub_and_fetch_2: |
| 426 | case Builtin::BI__sync_sub_and_fetch_4: |
| 427 | case Builtin::BI__sync_sub_and_fetch_8: |
| 428 | case Builtin::BI__sync_sub_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 429 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, |
| 430 | llvm::Instruction::Sub); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 431 | case Builtin::BI__sync_and_and_fetch_1: |
| 432 | case Builtin::BI__sync_and_and_fetch_2: |
| 433 | case Builtin::BI__sync_and_and_fetch_4: |
| 434 | case Builtin::BI__sync_and_and_fetch_8: |
| 435 | case Builtin::BI__sync_and_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 436 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, |
| 437 | llvm::Instruction::And); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 438 | case Builtin::BI__sync_or_and_fetch_1: |
| 439 | case Builtin::BI__sync_or_and_fetch_2: |
| 440 | case Builtin::BI__sync_or_and_fetch_4: |
| 441 | case Builtin::BI__sync_or_and_fetch_8: |
| 442 | case Builtin::BI__sync_or_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 443 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, |
| 444 | llvm::Instruction::Or); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 445 | case Builtin::BI__sync_xor_and_fetch_1: |
| 446 | case Builtin::BI__sync_xor_and_fetch_2: |
| 447 | case Builtin::BI__sync_xor_and_fetch_4: |
| 448 | case Builtin::BI__sync_xor_and_fetch_8: |
| 449 | case Builtin::BI__sync_xor_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 450 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, |
| 451 | llvm::Instruction::Xor); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 452 | case Builtin::BI__sync_nand_and_fetch_1: |
| 453 | case Builtin::BI__sync_nand_and_fetch_2: |
| 454 | case Builtin::BI__sync_nand_and_fetch_4: |
| 455 | case Builtin::BI__sync_nand_and_fetch_8: |
| 456 | case Builtin::BI__sync_nand_and_fetch_16: |
| 457 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E, |
| 458 | llvm::Instruction::And); |
| 459 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 460 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 461 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 462 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 463 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 464 | case Builtin::BI__sync_val_compare_and_swap_16: |
| 465 | { |
Mon P Wang | c500451 | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 466 | const llvm::Type *ResType[2]; |
| 467 | ResType[0]= ConvertType(E->getType()); |
| 468 | ResType[1] = ConvertType(E->getArg(0)->getType()); |
| 469 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
Anders Carlsson | 9584446 | 2008-08-30 16:17:45 +0000 | [diff] [blame] | 470 | return RValue::get(Builder.CreateCall3(AtomF, |
| 471 | EmitScalarExpr(E->getArg(0)), |
| 472 | EmitScalarExpr(E->getArg(1)), |
| 473 | EmitScalarExpr(E->getArg(2)))); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 474 | } |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 476 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 477 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 478 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 479 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 480 | case Builtin::BI__sync_bool_compare_and_swap_16: |
| 481 | { |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 482 | const llvm::Type *ResType[2]; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 483 | ResType[0]= ConvertType(E->getArg(1)->getType()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 484 | ResType[1] = VMContext.getPointerTypeUnqual(ResType[0]); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 485 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
| 486 | Value *OldVal = EmitScalarExpr(E->getArg(1)); |
| 487 | Value *PrevVal = Builder.CreateCall3(AtomF, |
| 488 | EmitScalarExpr(E->getArg(0)), |
| 489 | OldVal, |
| 490 | EmitScalarExpr(E->getArg(2))); |
| 491 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 492 | // zext bool to int. |
| 493 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 494 | } |
| 495 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 496 | case Builtin::BI__sync_lock_test_and_set_1: |
| 497 | case Builtin::BI__sync_lock_test_and_set_2: |
| 498 | case Builtin::BI__sync_lock_test_and_set_4: |
| 499 | case Builtin::BI__sync_lock_test_and_set_8: |
| 500 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 501 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 502 | case Builtin::BI__sync_lock_release_1: |
| 503 | case Builtin::BI__sync_lock_release_2: |
| 504 | case Builtin::BI__sync_lock_release_4: |
| 505 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 506 | case Builtin::BI__sync_lock_release_16: { |
| 507 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 508 | const llvm::Type *ElTy = |
| 509 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 510 | Builder.CreateStore(VMContext.getNullValue(ElTy), Ptr, true); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 511 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 512 | } |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 513 | |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 514 | case Builtin::BI__sync_synchronize: { |
| 515 | Value *C[5]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 516 | C[0] = C[1] = C[2] = C[3] = VMContext.getConstantInt(llvm::Type::Int1Ty, 1); |
| 517 | C[4] = VMContext.getConstantInt(llvm::Type::Int1Ty, 0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 518 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 519 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 522 | // Library functions with special handling. |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 523 | case Builtin::BIsqrt: |
| 524 | case Builtin::BIsqrtf: |
| 525 | case Builtin::BIsqrtl: { |
| 526 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 527 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 528 | break; |
| 529 | Value *Arg0 = EmitScalarExpr(E->getArg(0)); |
| 530 | const llvm::Type *ArgType = Arg0->getType(); |
| 531 | Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1); |
| 532 | return RValue::get(Builder.CreateCall(F, Arg0, "tmp")); |
| 533 | } |
| 534 | |
| 535 | case Builtin::BIpow: |
| 536 | case Builtin::BIpowf: |
| 537 | case Builtin::BIpowl: { |
| 538 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 539 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 540 | break; |
| 541 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 542 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 543 | const llvm::Type *ArgType = Base->getType(); |
| 544 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 545 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 546 | } |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 547 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 548 | |
| 549 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 550 | // that function. |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 551 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 552 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Anders Carlsson | 9864771 | 2009-05-27 01:22:39 +0000 | [diff] [blame] | 553 | return EmitCall(CGM.getBuiltinLibFunction(BuiltinID), |
| 554 | E->getCallee()->getType(), E->arg_begin(), |
| 555 | E->arg_end()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 556 | |
| 557 | // See if we have a target specific intrinsic. |
Dale Johannesen | a6f80ef | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 558 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
| 559 | Intrinsic::ID IntrinsicID = |
| 560 | Intrinsic::getIntrinsicForGCCBuiltin(Target.getTargetPrefix(), Name); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 561 | |
| 562 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 563 | SmallVector<Value*, 16> Args; |
| 564 | |
| 565 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 566 | const llvm::FunctionType *FTy = F->getFunctionType(); |
| 567 | |
| 568 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 569 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
| 570 | |
| 571 | // If the intrinsic arg type is different from the builtin arg type |
| 572 | // we need to do a bit cast. |
| 573 | const llvm::Type *PTy = FTy->getParamType(i); |
| 574 | if (PTy != ArgValue->getType()) { |
| 575 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 576 | "Must be able to losslessly bit cast to param"); |
| 577 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 578 | } |
| 579 | |
| 580 | Args.push_back(ArgValue); |
| 581 | } |
| 582 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 583 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 584 | QualType BuiltinRetType = E->getType(); |
| 585 | |
| 586 | const llvm::Type *RetTy = llvm::Type::VoidTy; |
| 587 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
| 588 | |
| 589 | if (RetTy != V->getType()) { |
| 590 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 591 | "Must be able to losslessly bit cast result type"); |
| 592 | V = Builder.CreateBitCast(V, RetTy); |
| 593 | } |
| 594 | |
| 595 | return RValue::get(V); |
| 596 | } |
| 597 | |
| 598 | // 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] | 599 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 600 | return RValue::get(V); |
| 601 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 602 | ErrorUnsupported(E, "builtin function"); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 603 | |
| 604 | // Unknown builtin, for now just dump it out and return undef. |
| 605 | if (hasAggregateLLVMType(E->getType())) |
| 606 | return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType()))); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 607 | return RValue::get(VMContext.getUndef(ConvertType(E->getType()))); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 608 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 609 | |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 610 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 611 | const CallExpr *E) { |
| 612 | const char *TargetPrefix = Target.getTargetPrefix(); |
| 613 | if (strcmp(TargetPrefix, "x86") == 0) |
| 614 | return EmitX86BuiltinExpr(BuiltinID, E); |
| 615 | else if (strcmp(TargetPrefix, "ppc") == 0) |
| 616 | return EmitPPCBuiltinExpr(BuiltinID, E); |
| 617 | return 0; |
| 618 | } |
| 619 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 620 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
| 621 | const CallExpr *E) { |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 622 | |
| 623 | llvm::SmallVector<Value*, 4> Ops; |
| 624 | |
| 625 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 626 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 627 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 628 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 629 | default: return 0; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 630 | case X86::BI__builtin_ia32_pslldi128: |
| 631 | case X86::BI__builtin_ia32_psllqi128: |
| 632 | case X86::BI__builtin_ia32_psllwi128: |
| 633 | case X86::BI__builtin_ia32_psradi128: |
| 634 | case X86::BI__builtin_ia32_psrawi128: |
| 635 | case X86::BI__builtin_ia32_psrldi128: |
| 636 | case X86::BI__builtin_ia32_psrlqi128: |
| 637 | case X86::BI__builtin_ia32_psrlwi128: { |
| 638 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 639 | const llvm::Type *Ty = VMContext.getVectorType(llvm::Type::Int64Ty, 2); |
| 640 | llvm::Value *Zero = VMContext.getConstantInt(llvm::Type::Int32Ty, 0); |
| 641 | Ops[1] = Builder.CreateInsertElement(VMContext.getUndef(Ty), |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 642 | Ops[1], Zero, "insert"); |
| 643 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 644 | const char *name = 0; |
| 645 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 646 | |
| 647 | switch (BuiltinID) { |
| 648 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 649 | case X86::BI__builtin_ia32_pslldi128: |
| 650 | name = "pslldi"; |
| 651 | ID = Intrinsic::x86_sse2_psll_d; |
| 652 | break; |
| 653 | case X86::BI__builtin_ia32_psllqi128: |
| 654 | name = "psllqi"; |
| 655 | ID = Intrinsic::x86_sse2_psll_q; |
| 656 | break; |
| 657 | case X86::BI__builtin_ia32_psllwi128: |
| 658 | name = "psllwi"; |
| 659 | ID = Intrinsic::x86_sse2_psll_w; |
| 660 | break; |
| 661 | case X86::BI__builtin_ia32_psradi128: |
| 662 | name = "psradi"; |
| 663 | ID = Intrinsic::x86_sse2_psra_d; |
| 664 | break; |
| 665 | case X86::BI__builtin_ia32_psrawi128: |
| 666 | name = "psrawi"; |
| 667 | ID = Intrinsic::x86_sse2_psra_w; |
| 668 | break; |
| 669 | case X86::BI__builtin_ia32_psrldi128: |
| 670 | name = "psrldi"; |
| 671 | ID = Intrinsic::x86_sse2_psrl_d; |
| 672 | break; |
| 673 | case X86::BI__builtin_ia32_psrlqi128: |
| 674 | name = "psrlqi"; |
| 675 | ID = Intrinsic::x86_sse2_psrl_q; |
| 676 | break; |
| 677 | case X86::BI__builtin_ia32_psrlwi128: |
| 678 | name = "psrlwi"; |
| 679 | ID = Intrinsic::x86_sse2_psrl_w; |
| 680 | break; |
| 681 | } |
| 682 | llvm::Function *F = CGM.getIntrinsic(ID); |
| 683 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 684 | } |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 685 | case X86::BI__builtin_ia32_pslldi: |
| 686 | case X86::BI__builtin_ia32_psllqi: |
| 687 | case X86::BI__builtin_ia32_psllwi: |
| 688 | case X86::BI__builtin_ia32_psradi: |
| 689 | case X86::BI__builtin_ia32_psrawi: |
| 690 | case X86::BI__builtin_ia32_psrldi: |
| 691 | case X86::BI__builtin_ia32_psrlqi: |
| 692 | case X86::BI__builtin_ia32_psrlwi: { |
| 693 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 694 | const llvm::Type *Ty = VMContext.getVectorType(llvm::Type::Int64Ty, 1); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 695 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 696 | const char *name = 0; |
| 697 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 698 | |
| 699 | switch (BuiltinID) { |
| 700 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 701 | case X86::BI__builtin_ia32_pslldi: |
| 702 | name = "pslldi"; |
| 703 | ID = Intrinsic::x86_mmx_psll_d; |
| 704 | break; |
| 705 | case X86::BI__builtin_ia32_psllqi: |
| 706 | name = "psllqi"; |
| 707 | ID = Intrinsic::x86_mmx_psll_q; |
| 708 | break; |
| 709 | case X86::BI__builtin_ia32_psllwi: |
| 710 | name = "psllwi"; |
| 711 | ID = Intrinsic::x86_mmx_psll_w; |
| 712 | break; |
| 713 | case X86::BI__builtin_ia32_psradi: |
| 714 | name = "psradi"; |
| 715 | ID = Intrinsic::x86_mmx_psra_d; |
| 716 | break; |
| 717 | case X86::BI__builtin_ia32_psrawi: |
| 718 | name = "psrawi"; |
| 719 | ID = Intrinsic::x86_mmx_psra_w; |
| 720 | break; |
| 721 | case X86::BI__builtin_ia32_psrldi: |
| 722 | name = "psrldi"; |
| 723 | ID = Intrinsic::x86_mmx_psrl_d; |
| 724 | break; |
| 725 | case X86::BI__builtin_ia32_psrlqi: |
| 726 | name = "psrlqi"; |
| 727 | ID = Intrinsic::x86_mmx_psrl_q; |
| 728 | break; |
| 729 | case X86::BI__builtin_ia32_psrlwi: |
| 730 | name = "psrlwi"; |
| 731 | ID = Intrinsic::x86_mmx_psrl_w; |
| 732 | break; |
| 733 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 734 | llvm::Function *F = CGM.getIntrinsic(ID); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 735 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 736 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 737 | case X86::BI__builtin_ia32_cmpps: { |
| 738 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 739 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 740 | } |
| 741 | case X86::BI__builtin_ia32_cmpss: { |
| 742 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 743 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 744 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 745 | case X86::BI__builtin_ia32_ldmxcsr: { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 746 | llvm::Type *PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); |
| 747 | Value *One = VMContext.getConstantInt(llvm::Type::Int32Ty, 1); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 748 | Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 749 | Builder.CreateStore(Ops[0], Tmp); |
| 750 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 751 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 752 | } |
| 753 | case X86::BI__builtin_ia32_stmxcsr: { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 754 | llvm::Type *PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); |
| 755 | Value *One = VMContext.getConstantInt(llvm::Type::Int32Ty, 1); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 756 | Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 757 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 758 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 759 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 760 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 761 | case X86::BI__builtin_ia32_cmppd: { |
| 762 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 763 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 764 | } |
| 765 | case X86::BI__builtin_ia32_cmpsd: { |
| 766 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 767 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 768 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 769 | case X86::BI__builtin_ia32_storehps: |
| 770 | case X86::BI__builtin_ia32_storelps: { |
| 771 | const llvm::Type *EltTy = llvm::Type::Int64Ty; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 772 | llvm::Type *PtrTy = VMContext.getPointerTypeUnqual(EltTy); |
| 773 | llvm::Type *VecTy = VMContext.getVectorType(EltTy, 2); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 774 | |
| 775 | // cast val v2i64 |
| 776 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
| 777 | |
| 778 | // extract (0, 1) |
| 779 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 780 | llvm::Value *Idx = VMContext.getConstantInt(llvm::Type::Int32Ty, Index); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 781 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 782 | |
| 783 | // cast pointer to i64 & store |
| 784 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 785 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 786 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 790 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
| 791 | const CallExpr *E) { |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 792 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 793 | default: return 0; |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 794 | } |
| 795 | } |