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" |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 20 | #include "clang/AST/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()) |
| 66 | return RValue::get(llvm::ConstantInt::get(Result.Val.getInt())); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 67 | else if (Result.Val.isFloat()) |
| 68 | return RValue::get(llvm::ConstantFP::get(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 = |
| 80 | llvm::PointerType::getUnqual(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 = |
| 94 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 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 = |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 106 | Builder.CreateICmpSGE(ArgValue, Constant::getNullValue(ArgValue->getType()), |
| 107 | "abscond"); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 108 | Value *Result = |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 109 | Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); |
| 110 | |
| 111 | return RValue::get(Result); |
| 112 | } |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 113 | case Builtin::BI__builtin_ctz: |
| 114 | case Builtin::BI__builtin_ctzl: |
| 115 | case Builtin::BI__builtin_ctzll: { |
| 116 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 117 | |
| 118 | const llvm::Type *ArgType = ArgValue->getType(); |
| 119 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
| 120 | |
| 121 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 122 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 123 | if (Result->getType() != ResultType) |
| 124 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 125 | return RValue::get(Result); |
| 126 | } |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 127 | case Builtin::BI__builtin_clz: |
| 128 | case Builtin::BI__builtin_clzl: |
| 129 | case Builtin::BI__builtin_clzll: { |
| 130 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 131 | |
| 132 | const llvm::Type *ArgType = ArgValue->getType(); |
| 133 | Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1); |
| 134 | |
| 135 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 136 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 137 | if (Result->getType() != ResultType) |
| 138 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 139 | return RValue::get(Result); |
| 140 | } |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 141 | case Builtin::BI__builtin_ffs: |
| 142 | case Builtin::BI__builtin_ffsl: |
| 143 | case Builtin::BI__builtin_ffsll: { |
| 144 | // ffs(x) -> x ? cttz(x) + 1 : 0 |
| 145 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 146 | |
| 147 | const llvm::Type *ArgType = ArgValue->getType(); |
| 148 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
| 149 | |
| 150 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 151 | Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), |
| 152 | ConstantInt::get(ArgType, 1), "tmp"); |
| 153 | Value *Zero = llvm::Constant::getNullValue(ArgType); |
| 154 | Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); |
| 155 | Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs"); |
| 156 | if (Result->getType() != ResultType) |
| 157 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 158 | return RValue::get(Result); |
| 159 | } |
| 160 | case Builtin::BI__builtin_parity: |
| 161 | case Builtin::BI__builtin_parityl: |
| 162 | case Builtin::BI__builtin_parityll: { |
| 163 | // parity(x) -> ctpop(x) & 1 |
| 164 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 165 | |
| 166 | const llvm::Type *ArgType = ArgValue->getType(); |
| 167 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
| 168 | |
| 169 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 170 | Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp"); |
| 171 | Value *Result = Builder.CreateAnd(Tmp, ConstantInt::get(ArgType, 1), |
| 172 | "tmp"); |
| 173 | if (Result->getType() != ResultType) |
| 174 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 175 | return RValue::get(Result); |
| 176 | } |
| 177 | case Builtin::BI__builtin_popcount: |
| 178 | case Builtin::BI__builtin_popcountl: |
| 179 | case Builtin::BI__builtin_popcountll: { |
| 180 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 181 | |
| 182 | const llvm::Type *ArgType = ArgValue->getType(); |
| 183 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
| 184 | |
| 185 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 186 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 187 | if (Result->getType() != ResultType) |
| 188 | Result = Builder.CreateIntCast(Result, ResultType, "cast"); |
| 189 | return RValue::get(Result); |
| 190 | } |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 191 | case Builtin::BI__builtin_expect: |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 192 | // FIXME: pass expect through to LLVM |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 193 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 194 | case Builtin::BI__builtin_bswap32: |
| 195 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 196 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 197 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 198 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 199 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 200 | } |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 201 | case Builtin::BI__builtin_object_size: { |
| 202 | // FIXME: Implement. For now we just always fail and pretend we |
| 203 | // don't know the object size. |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 204 | llvm::APSInt TypeArg = E->getArg(1)->EvaluateAsInt(CGM.getContext()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 205 | const llvm::Type *ResType = ConvertType(E->getType()); |
| 206 | // bool UseSubObject = TypeArg.getZExtValue() & 1; |
| 207 | bool UseMinimum = TypeArg.getZExtValue() & 2; |
| 208 | return RValue::get(ConstantInt::get(ResType, UseMinimum ? 0 : -1LL)); |
| 209 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 210 | case Builtin::BI__builtin_prefetch: { |
| 211 | Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); |
| 212 | // FIXME: Technically these constants should of type 'int', yes? |
| 213 | RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : |
| 214 | ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 215 | Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : |
| 216 | ConstantInt::get(llvm::Type::Int32Ty, 3); |
| 217 | Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); |
| 218 | return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 219 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 220 | case Builtin::BI__builtin_trap: { |
| 221 | Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); |
| 222 | return RValue::get(Builder.CreateCall(F)); |
| 223 | } |
| 224 | |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 225 | case Builtin::BI__builtin_powi: |
| 226 | case Builtin::BI__builtin_powif: |
| 227 | case Builtin::BI__builtin_powil: { |
| 228 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 229 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 230 | const llvm::Type *ArgType = Base->getType(); |
| 231 | Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 232 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 233 | } |
| 234 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 235 | case Builtin::BI__builtin_isgreater: |
| 236 | case Builtin::BI__builtin_isgreaterequal: |
| 237 | case Builtin::BI__builtin_isless: |
| 238 | case Builtin::BI__builtin_islessequal: |
| 239 | case Builtin::BI__builtin_islessgreater: |
| 240 | case Builtin::BI__builtin_isunordered: { |
| 241 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 242 | // floating point values. |
| 243 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
| 244 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
| 245 | |
| 246 | switch (BuiltinID) { |
| 247 | default: assert(0 && "Unknown ordered comparison"); |
| 248 | case Builtin::BI__builtin_isgreater: |
| 249 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 250 | break; |
| 251 | case Builtin::BI__builtin_isgreaterequal: |
| 252 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 253 | break; |
| 254 | case Builtin::BI__builtin_isless: |
| 255 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 256 | break; |
| 257 | case Builtin::BI__builtin_islessequal: |
| 258 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 259 | break; |
| 260 | case Builtin::BI__builtin_islessgreater: |
| 261 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 262 | break; |
| 263 | case Builtin::BI__builtin_isunordered: |
| 264 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 265 | break; |
| 266 | } |
| 267 | // ZExt bool to int type. |
| 268 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 269 | "tmp")); |
| 270 | } |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 271 | case Builtin::BI__builtin_alloca: { |
| 272 | // FIXME: LLVM IR Should allow alloca with an i64 size! |
| 273 | Value *Size = EmitScalarExpr(E->getArg(0)); |
| 274 | Size = Builder.CreateIntCast(Size, llvm::Type::Int32Ty, false, "tmp"); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 275 | return RValue::get(Builder.CreateAlloca(llvm::Type::Int8Ty, Size, "tmp")); |
| 276 | } |
| 277 | case Builtin::BI__builtin_bzero: { |
| 278 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 279 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
| 280 | llvm::ConstantInt::get(llvm::Type::Int8Ty, 0), |
| 281 | EmitScalarExpr(E->getArg(1)), |
| 282 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); |
| 283 | return RValue::get(Address); |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 284 | } |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 285 | case Builtin::BI__builtin_memcpy: { |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 286 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 287 | Builder.CreateCall4(CGM.getMemCpyFn(), Address, |
| 288 | EmitScalarExpr(E->getArg(1)), |
| 289 | EmitScalarExpr(E->getArg(2)), |
| 290 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); |
| 291 | return RValue::get(Address); |
| 292 | } |
| 293 | case Builtin::BI__builtin_memmove: { |
| 294 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 295 | Builder.CreateCall4(CGM.getMemMoveFn(), Address, |
| 296 | EmitScalarExpr(E->getArg(1)), |
| 297 | EmitScalarExpr(E->getArg(2)), |
| 298 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); |
| 299 | return RValue::get(Address); |
| 300 | } |
| 301 | case Builtin::BI__builtin_memset: { |
| 302 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 303 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Daniel Dunbar | 62c29c6 | 2009-02-16 21:52:05 +0000 | [diff] [blame] | 304 | Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), |
| 305 | llvm::Type::Int8Ty), |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 306 | EmitScalarExpr(E->getArg(2)), |
| 307 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); |
| 308 | return RValue::get(Address); |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 309 | } |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 310 | case Builtin::BI__builtin_return_address: { |
| 311 | Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); |
| 312 | return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); |
| 313 | } |
| 314 | case Builtin::BI__builtin_frame_address: { |
| 315 | Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
| 316 | return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); |
| 317 | } |
Eli Friedman | 3b660ef | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 318 | case Builtin::BI__builtin_extract_return_addr: { |
| 319 | // FIXME: There should be a target hook for this |
| 320 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
| 321 | } |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 322 | case Builtin::BI__sync_fetch_and_add: |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 323 | case Builtin::BI__sync_fetch_and_sub: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 324 | case Builtin::BI__sync_fetch_and_or: |
| 325 | case Builtin::BI__sync_fetch_and_and: |
| 326 | case Builtin::BI__sync_fetch_and_xor: |
| 327 | case Builtin::BI__sync_add_and_fetch: |
| 328 | case Builtin::BI__sync_sub_and_fetch: |
| 329 | case Builtin::BI__sync_and_and_fetch: |
| 330 | case Builtin::BI__sync_or_and_fetch: |
| 331 | case Builtin::BI__sync_xor_and_fetch: |
| 332 | case Builtin::BI__sync_val_compare_and_swap: |
| 333 | case Builtin::BI__sync_bool_compare_and_swap: |
| 334 | case Builtin::BI__sync_lock_test_and_set: |
| 335 | case Builtin::BI__sync_lock_release: |
| 336 | assert(0 && "Shouldn't make it through sema"); |
| 337 | case Builtin::BI__sync_fetch_and_add_1: |
| 338 | case Builtin::BI__sync_fetch_and_add_2: |
| 339 | case Builtin::BI__sync_fetch_and_add_4: |
| 340 | case Builtin::BI__sync_fetch_and_add_8: |
| 341 | case Builtin::BI__sync_fetch_and_add_16: |
| 342 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); |
| 343 | case Builtin::BI__sync_fetch_and_sub_1: |
| 344 | case Builtin::BI__sync_fetch_and_sub_2: |
| 345 | case Builtin::BI__sync_fetch_and_sub_4: |
| 346 | case Builtin::BI__sync_fetch_and_sub_8: |
| 347 | case Builtin::BI__sync_fetch_and_sub_16: |
Mon P Wang | 09b6bf5 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 348 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 349 | case Builtin::BI__sync_fetch_and_or_1: |
| 350 | case Builtin::BI__sync_fetch_and_or_2: |
| 351 | case Builtin::BI__sync_fetch_and_or_4: |
| 352 | case Builtin::BI__sync_fetch_and_or_8: |
| 353 | case Builtin::BI__sync_fetch_and_or_16: |
| 354 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 355 | case Builtin::BI__sync_fetch_and_and_1: |
| 356 | case Builtin::BI__sync_fetch_and_and_2: |
| 357 | case Builtin::BI__sync_fetch_and_and_4: |
| 358 | case Builtin::BI__sync_fetch_and_and_8: |
| 359 | case Builtin::BI__sync_fetch_and_and_16: |
| 360 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 361 | case Builtin::BI__sync_fetch_and_xor_1: |
| 362 | case Builtin::BI__sync_fetch_and_xor_2: |
| 363 | case Builtin::BI__sync_fetch_and_xor_4: |
| 364 | case Builtin::BI__sync_fetch_and_xor_8: |
| 365 | case Builtin::BI__sync_fetch_and_xor_16: |
| 366 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 367 | case Builtin::BI__sync_fetch_and_nand_1: |
| 368 | case Builtin::BI__sync_fetch_and_nand_2: |
| 369 | case Builtin::BI__sync_fetch_and_nand_4: |
| 370 | case Builtin::BI__sync_fetch_and_nand_8: |
| 371 | case Builtin::BI__sync_fetch_and_nand_16: |
| 372 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E); |
| 373 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 374 | // Clang extensions: not overloaded yet. |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 375 | case Builtin::BI__sync_fetch_and_min: |
| 376 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 377 | case Builtin::BI__sync_fetch_and_max: |
| 378 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 379 | case Builtin::BI__sync_fetch_and_umin: |
| 380 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 381 | case Builtin::BI__sync_fetch_and_umax: |
| 382 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 383 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 384 | case Builtin::BI__sync_add_and_fetch_1: |
| 385 | case Builtin::BI__sync_add_and_fetch_2: |
| 386 | case Builtin::BI__sync_add_and_fetch_4: |
| 387 | case Builtin::BI__sync_add_and_fetch_8: |
| 388 | case Builtin::BI__sync_add_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 389 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, |
| 390 | llvm::Instruction::Add); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 391 | case Builtin::BI__sync_sub_and_fetch_1: |
| 392 | case Builtin::BI__sync_sub_and_fetch_2: |
| 393 | case Builtin::BI__sync_sub_and_fetch_4: |
| 394 | case Builtin::BI__sync_sub_and_fetch_8: |
| 395 | case Builtin::BI__sync_sub_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 396 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, |
| 397 | llvm::Instruction::Sub); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 398 | case Builtin::BI__sync_and_and_fetch_1: |
| 399 | case Builtin::BI__sync_and_and_fetch_2: |
| 400 | case Builtin::BI__sync_and_and_fetch_4: |
| 401 | case Builtin::BI__sync_and_and_fetch_8: |
| 402 | case Builtin::BI__sync_and_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 403 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, |
| 404 | llvm::Instruction::And); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 405 | case Builtin::BI__sync_or_and_fetch_1: |
| 406 | case Builtin::BI__sync_or_and_fetch_2: |
| 407 | case Builtin::BI__sync_or_and_fetch_4: |
| 408 | case Builtin::BI__sync_or_and_fetch_8: |
| 409 | case Builtin::BI__sync_or_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 410 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, |
| 411 | llvm::Instruction::Or); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 412 | case Builtin::BI__sync_xor_and_fetch_1: |
| 413 | case Builtin::BI__sync_xor_and_fetch_2: |
| 414 | case Builtin::BI__sync_xor_and_fetch_4: |
| 415 | case Builtin::BI__sync_xor_and_fetch_8: |
| 416 | case Builtin::BI__sync_xor_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 417 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, |
| 418 | llvm::Instruction::Xor); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 419 | case Builtin::BI__sync_nand_and_fetch_1: |
| 420 | case Builtin::BI__sync_nand_and_fetch_2: |
| 421 | case Builtin::BI__sync_nand_and_fetch_4: |
| 422 | case Builtin::BI__sync_nand_and_fetch_8: |
| 423 | case Builtin::BI__sync_nand_and_fetch_16: |
| 424 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E, |
| 425 | llvm::Instruction::And); |
| 426 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 427 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 428 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 429 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 430 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 431 | case Builtin::BI__sync_val_compare_and_swap_16: |
| 432 | { |
Mon P Wang | c500451 | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 433 | const llvm::Type *ResType[2]; |
| 434 | ResType[0]= ConvertType(E->getType()); |
| 435 | ResType[1] = ConvertType(E->getArg(0)->getType()); |
| 436 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
Anders Carlsson | 9584446 | 2008-08-30 16:17:45 +0000 | [diff] [blame] | 437 | return RValue::get(Builder.CreateCall3(AtomF, |
| 438 | EmitScalarExpr(E->getArg(0)), |
| 439 | EmitScalarExpr(E->getArg(1)), |
| 440 | EmitScalarExpr(E->getArg(2)))); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 441 | } |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 443 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 444 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 445 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 446 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 447 | case Builtin::BI__sync_bool_compare_and_swap_16: |
| 448 | { |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 449 | const llvm::Type *ResType[2]; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 450 | ResType[0]= ConvertType(E->getArg(1)->getType()); |
| 451 | ResType[1] = llvm::PointerType::getUnqual(ResType[0]); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 452 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
| 453 | Value *OldVal = EmitScalarExpr(E->getArg(1)); |
| 454 | Value *PrevVal = Builder.CreateCall3(AtomF, |
| 455 | EmitScalarExpr(E->getArg(0)), |
| 456 | OldVal, |
| 457 | EmitScalarExpr(E->getArg(2))); |
| 458 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 459 | // zext bool to int. |
| 460 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 461 | } |
| 462 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 463 | case Builtin::BI__sync_lock_test_and_set_1: |
| 464 | case Builtin::BI__sync_lock_test_and_set_2: |
| 465 | case Builtin::BI__sync_lock_test_and_set_4: |
| 466 | case Builtin::BI__sync_lock_test_and_set_8: |
| 467 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 468 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 469 | case Builtin::BI__sync_lock_release_1: |
| 470 | case Builtin::BI__sync_lock_release_2: |
| 471 | case Builtin::BI__sync_lock_release_4: |
| 472 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 473 | case Builtin::BI__sync_lock_release_16: { |
| 474 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 475 | const llvm::Type *ElTy = |
| 476 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
| 477 | Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr, true); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame^] | 478 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 479 | } |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 480 | |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 481 | case Builtin::BI__sync_synchronize: { |
| 482 | Value *C[5]; |
| 483 | C[0] = C[1] = C[2] = C[3] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 1); |
| 484 | C[4] = ConstantInt::get(llvm::Type::Int1Ty, 0); |
| 485 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame^] | 486 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 489 | // Library functions with special handling. |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 490 | case Builtin::BIsqrt: |
| 491 | case Builtin::BIsqrtf: |
| 492 | case Builtin::BIsqrtl: { |
| 493 | // Rewrite sqrt to intrinsic if allowed. |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 494 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 495 | break; |
| 496 | Value *Arg0 = EmitScalarExpr(E->getArg(0)); |
| 497 | const llvm::Type *ArgType = Arg0->getType(); |
| 498 | Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1); |
| 499 | return RValue::get(Builder.CreateCall(F, Arg0, "tmp")); |
| 500 | } |
| 501 | |
| 502 | case Builtin::BIpow: |
| 503 | case Builtin::BIpowf: |
| 504 | case Builtin::BIpowl: { |
| 505 | // Rewrite sqrt to intrinsic if allowed. |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 506 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 507 | break; |
| 508 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 509 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 510 | const llvm::Type *ArgType = Base->getType(); |
| 511 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 512 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 513 | } |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 514 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 515 | |
| 516 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 517 | // that function. |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 518 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 519 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Anders Carlsson | 9864771 | 2009-05-27 01:22:39 +0000 | [diff] [blame] | 520 | return EmitCall(CGM.getBuiltinLibFunction(BuiltinID), |
| 521 | E->getCallee()->getType(), E->arg_begin(), |
| 522 | E->arg_end()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 523 | |
| 524 | // See if we have a target specific intrinsic. |
Dale Johannesen | a6f80ef | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 525 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
| 526 | Intrinsic::ID IntrinsicID = |
| 527 | Intrinsic::getIntrinsicForGCCBuiltin(Target.getTargetPrefix(), Name); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 528 | |
| 529 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 530 | SmallVector<Value*, 16> Args; |
| 531 | |
| 532 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 533 | const llvm::FunctionType *FTy = F->getFunctionType(); |
| 534 | |
| 535 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 536 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
| 537 | |
| 538 | // If the intrinsic arg type is different from the builtin arg type |
| 539 | // we need to do a bit cast. |
| 540 | const llvm::Type *PTy = FTy->getParamType(i); |
| 541 | if (PTy != ArgValue->getType()) { |
| 542 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 543 | "Must be able to losslessly bit cast to param"); |
| 544 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 545 | } |
| 546 | |
| 547 | Args.push_back(ArgValue); |
| 548 | } |
| 549 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 550 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 551 | QualType BuiltinRetType = E->getType(); |
| 552 | |
| 553 | const llvm::Type *RetTy = llvm::Type::VoidTy; |
| 554 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
| 555 | |
| 556 | if (RetTy != V->getType()) { |
| 557 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 558 | "Must be able to losslessly bit cast result type"); |
| 559 | V = Builder.CreateBitCast(V, RetTy); |
| 560 | } |
| 561 | |
| 562 | return RValue::get(V); |
| 563 | } |
| 564 | |
| 565 | // 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] | 566 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 567 | return RValue::get(V); |
| 568 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 569 | ErrorUnsupported(E, "builtin function"); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 570 | |
| 571 | // Unknown builtin, for now just dump it out and return undef. |
| 572 | if (hasAggregateLLVMType(E->getType())) |
| 573 | return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType()))); |
| 574 | return RValue::get(UndefValue::get(ConvertType(E->getType()))); |
| 575 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 576 | |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 577 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 578 | const CallExpr *E) { |
| 579 | const char *TargetPrefix = Target.getTargetPrefix(); |
| 580 | if (strcmp(TargetPrefix, "x86") == 0) |
| 581 | return EmitX86BuiltinExpr(BuiltinID, E); |
| 582 | else if (strcmp(TargetPrefix, "ppc") == 0) |
| 583 | return EmitPPCBuiltinExpr(BuiltinID, E); |
| 584 | return 0; |
| 585 | } |
| 586 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 587 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
| 588 | const CallExpr *E) { |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 589 | |
| 590 | llvm::SmallVector<Value*, 4> Ops; |
| 591 | |
| 592 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 593 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 594 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 595 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 596 | default: return 0; |
| 597 | case X86::BI__builtin_ia32_mulps: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 598 | return Builder.CreateMul(Ops[0], Ops[1], "mulps"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 599 | case X86::BI__builtin_ia32_mulpd: |
| 600 | return Builder.CreateMul(Ops[0], Ops[1], "mulpd"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 601 | case X86::BI__builtin_ia32_pand: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 602 | case X86::BI__builtin_ia32_pand128: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 603 | return Builder.CreateAnd(Ops[0], Ops[1], "pand"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 604 | case X86::BI__builtin_ia32_por: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 605 | case X86::BI__builtin_ia32_por128: |
| 606 | return Builder.CreateOr(Ops[0], Ops[1], "por"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 607 | case X86::BI__builtin_ia32_pxor: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 608 | case X86::BI__builtin_ia32_pxor128: |
| 609 | return Builder.CreateXor(Ops[0], Ops[1], "pxor"); |
| 610 | case X86::BI__builtin_ia32_pandn: |
| 611 | case X86::BI__builtin_ia32_pandn128: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 612 | Ops[0] = Builder.CreateNot(Ops[0], "tmp"); |
| 613 | return Builder.CreateAnd(Ops[0], Ops[1], "pandn"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 614 | case X86::BI__builtin_ia32_paddb: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 615 | case X86::BI__builtin_ia32_paddb128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 616 | case X86::BI__builtin_ia32_paddd: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 617 | case X86::BI__builtin_ia32_paddd128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 618 | case X86::BI__builtin_ia32_paddq: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 619 | case X86::BI__builtin_ia32_paddq128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 620 | case X86::BI__builtin_ia32_paddw: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 621 | case X86::BI__builtin_ia32_paddw128: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 622 | case X86::BI__builtin_ia32_addps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 623 | case X86::BI__builtin_ia32_addpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 624 | return Builder.CreateAdd(Ops[0], Ops[1], "add"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 625 | case X86::BI__builtin_ia32_psubb: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 626 | case X86::BI__builtin_ia32_psubb128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 627 | case X86::BI__builtin_ia32_psubd: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 628 | case X86::BI__builtin_ia32_psubd128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 629 | case X86::BI__builtin_ia32_psubq: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 630 | case X86::BI__builtin_ia32_psubq128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 631 | case X86::BI__builtin_ia32_psubw: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 632 | case X86::BI__builtin_ia32_psubw128: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 633 | case X86::BI__builtin_ia32_subps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 634 | case X86::BI__builtin_ia32_subpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 635 | return Builder.CreateSub(Ops[0], Ops[1], "sub"); |
| 636 | case X86::BI__builtin_ia32_divps: |
| 637 | return Builder.CreateFDiv(Ops[0], Ops[1], "divps"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 638 | case X86::BI__builtin_ia32_divpd: |
| 639 | return Builder.CreateFDiv(Ops[0], Ops[1], "divpd"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 640 | case X86::BI__builtin_ia32_pmullw: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 641 | case X86::BI__builtin_ia32_pmullw128: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 642 | return Builder.CreateMul(Ops[0], Ops[1], "pmul"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 643 | case X86::BI__builtin_ia32_punpckhbw: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 644 | return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15, |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 645 | "punpckhbw"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 646 | case X86::BI__builtin_ia32_punpckhbw128: |
| 647 | return EmitShuffleVector(Ops[0], Ops[1], 8, 24, 9, 25, 10, 26, 11, 27, |
| 648 | 12, 28, 13, 29, 14, 30, 15, 31, |
| 649 | "punpckhbw"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 650 | case X86::BI__builtin_ia32_punpckhwd: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 651 | return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhwd"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 652 | case X86::BI__builtin_ia32_punpckhwd128: |
| 653 | return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15, |
| 654 | "punpckhwd"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 655 | case X86::BI__builtin_ia32_punpckhdq: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 656 | return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhdq"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 657 | case X86::BI__builtin_ia32_punpckhdq128: |
| 658 | return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhdq"); |
Nate Begeman | abfaf99 | 2008-07-25 20:17:24 +0000 | [diff] [blame] | 659 | case X86::BI__builtin_ia32_punpckhqdq128: |
| 660 | return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhqdq"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 661 | case X86::BI__builtin_ia32_punpcklbw: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 662 | return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11, |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 663 | "punpcklbw"); |
| 664 | case X86::BI__builtin_ia32_punpcklwd: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 665 | return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpcklwd"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 666 | case X86::BI__builtin_ia32_punpckldq: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 667 | return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpckldq"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 668 | case X86::BI__builtin_ia32_punpckldq128: |
| 669 | return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpckldq"); |
Nate Begeman | abfaf99 | 2008-07-25 20:17:24 +0000 | [diff] [blame] | 670 | case X86::BI__builtin_ia32_punpcklqdq128: |
| 671 | return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpcklqdq"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 672 | case X86::BI__builtin_ia32_pslldi128: |
| 673 | case X86::BI__builtin_ia32_psllqi128: |
| 674 | case X86::BI__builtin_ia32_psllwi128: |
| 675 | case X86::BI__builtin_ia32_psradi128: |
| 676 | case X86::BI__builtin_ia32_psrawi128: |
| 677 | case X86::BI__builtin_ia32_psrldi128: |
| 678 | case X86::BI__builtin_ia32_psrlqi128: |
| 679 | case X86::BI__builtin_ia32_psrlwi128: { |
| 680 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); |
| 681 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 2); |
| 682 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 683 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
| 684 | Ops[1], Zero, "insert"); |
| 685 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 686 | const char *name = 0; |
| 687 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 688 | |
| 689 | switch (BuiltinID) { |
| 690 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 691 | case X86::BI__builtin_ia32_pslldi128: |
| 692 | name = "pslldi"; |
| 693 | ID = Intrinsic::x86_sse2_psll_d; |
| 694 | break; |
| 695 | case X86::BI__builtin_ia32_psllqi128: |
| 696 | name = "psllqi"; |
| 697 | ID = Intrinsic::x86_sse2_psll_q; |
| 698 | break; |
| 699 | case X86::BI__builtin_ia32_psllwi128: |
| 700 | name = "psllwi"; |
| 701 | ID = Intrinsic::x86_sse2_psll_w; |
| 702 | break; |
| 703 | case X86::BI__builtin_ia32_psradi128: |
| 704 | name = "psradi"; |
| 705 | ID = Intrinsic::x86_sse2_psra_d; |
| 706 | break; |
| 707 | case X86::BI__builtin_ia32_psrawi128: |
| 708 | name = "psrawi"; |
| 709 | ID = Intrinsic::x86_sse2_psra_w; |
| 710 | break; |
| 711 | case X86::BI__builtin_ia32_psrldi128: |
| 712 | name = "psrldi"; |
| 713 | ID = Intrinsic::x86_sse2_psrl_d; |
| 714 | break; |
| 715 | case X86::BI__builtin_ia32_psrlqi128: |
| 716 | name = "psrlqi"; |
| 717 | ID = Intrinsic::x86_sse2_psrl_q; |
| 718 | break; |
| 719 | case X86::BI__builtin_ia32_psrlwi128: |
| 720 | name = "psrlwi"; |
| 721 | ID = Intrinsic::x86_sse2_psrl_w; |
| 722 | break; |
| 723 | } |
| 724 | llvm::Function *F = CGM.getIntrinsic(ID); |
| 725 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 726 | } |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 727 | case X86::BI__builtin_ia32_pslldi: |
| 728 | case X86::BI__builtin_ia32_psllqi: |
| 729 | case X86::BI__builtin_ia32_psllwi: |
| 730 | case X86::BI__builtin_ia32_psradi: |
| 731 | case X86::BI__builtin_ia32_psrawi: |
| 732 | case X86::BI__builtin_ia32_psrldi: |
| 733 | case X86::BI__builtin_ia32_psrlqi: |
| 734 | case X86::BI__builtin_ia32_psrlwi: { |
| 735 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); |
| 736 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1); |
| 737 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 738 | const char *name = 0; |
| 739 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 740 | |
| 741 | switch (BuiltinID) { |
| 742 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 743 | case X86::BI__builtin_ia32_pslldi: |
| 744 | name = "pslldi"; |
| 745 | ID = Intrinsic::x86_mmx_psll_d; |
| 746 | break; |
| 747 | case X86::BI__builtin_ia32_psllqi: |
| 748 | name = "psllqi"; |
| 749 | ID = Intrinsic::x86_mmx_psll_q; |
| 750 | break; |
| 751 | case X86::BI__builtin_ia32_psllwi: |
| 752 | name = "psllwi"; |
| 753 | ID = Intrinsic::x86_mmx_psll_w; |
| 754 | break; |
| 755 | case X86::BI__builtin_ia32_psradi: |
| 756 | name = "psradi"; |
| 757 | ID = Intrinsic::x86_mmx_psra_d; |
| 758 | break; |
| 759 | case X86::BI__builtin_ia32_psrawi: |
| 760 | name = "psrawi"; |
| 761 | ID = Intrinsic::x86_mmx_psra_w; |
| 762 | break; |
| 763 | case X86::BI__builtin_ia32_psrldi: |
| 764 | name = "psrldi"; |
| 765 | ID = Intrinsic::x86_mmx_psrl_d; |
| 766 | break; |
| 767 | case X86::BI__builtin_ia32_psrlqi: |
| 768 | name = "psrlqi"; |
| 769 | ID = Intrinsic::x86_mmx_psrl_q; |
| 770 | break; |
| 771 | case X86::BI__builtin_ia32_psrlwi: |
| 772 | name = "psrlwi"; |
| 773 | ID = Intrinsic::x86_mmx_psrl_w; |
| 774 | break; |
| 775 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 776 | llvm::Function *F = CGM.getIntrinsic(ID); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 777 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 778 | } |
Anders Carlsson | 4ee51b4 | 2008-12-22 04:54:32 +0000 | [diff] [blame] | 779 | case X86::BI__builtin_ia32_pshufw: { |
| 780 | unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); |
| 781 | return EmitShuffleVector(Ops[0], Ops[0], |
| 782 | i & 0x3, (i & 0xc) >> 2, |
| 783 | (i & 0x30) >> 4, (i & 0xc0) >> 6, |
| 784 | "pshufw"); |
| 785 | } |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 786 | case X86::BI__builtin_ia32_pshuflw: { |
| 787 | unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); |
| 788 | return EmitShuffleVector(Ops[0], Ops[0], |
| 789 | i & 0x3, (i & 0xc) >> 2, |
| 790 | (i & 0x30) >> 4, (i & 0xc0) >> 6, 4, 5, 6, 7, |
| 791 | "pshuflw"); |
| 792 | } |
| 793 | case X86::BI__builtin_ia32_pshufhw: { |
| 794 | unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); |
| 795 | return EmitShuffleVector(Ops[0], Ops[0], 0, 1, 2, 3, |
| 796 | 4 + (i & 0x3), 4 + ((i & 0xc) >> 2), |
| 797 | 4 + ((i & 0x30) >> 4), 4 + ((i & 0xc0) >> 6), |
| 798 | "pshufhw"); |
| 799 | } |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 800 | case X86::BI__builtin_ia32_pshufd: { |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 801 | unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 802 | return EmitShuffleVector(Ops[0], Ops[0], |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 803 | i & 0x3, (i & 0xc) >> 2, |
| 804 | (i & 0x30) >> 4, (i & 0xc0) >> 6, |
| 805 | "pshufd"); |
| 806 | } |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 807 | case X86::BI__builtin_ia32_vec_init_v4hi: |
| 808 | case X86::BI__builtin_ia32_vec_init_v8qi: |
| 809 | case X86::BI__builtin_ia32_vec_init_v2si: |
| 810 | return EmitVector(&Ops[0], Ops.size()); |
| 811 | case X86::BI__builtin_ia32_vec_ext_v2si: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 812 | case X86::BI__builtin_ia32_vec_ext_v2di: |
| 813 | case X86::BI__builtin_ia32_vec_ext_v4sf: |
| 814 | case X86::BI__builtin_ia32_vec_ext_v4si: |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 815 | case X86::BI__builtin_ia32_vec_ext_v8hi: |
| 816 | case X86::BI__builtin_ia32_vec_ext_v4hi: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 817 | case X86::BI__builtin_ia32_vec_ext_v2df: |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 818 | return Builder.CreateExtractElement(Ops[0], Ops[1], "result"); |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 819 | case X86::BI__builtin_ia32_cmpps: { |
| 820 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 821 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 822 | } |
| 823 | case X86::BI__builtin_ia32_cmpss: { |
| 824 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 825 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 826 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 827 | case X86::BI__builtin_ia32_ldmxcsr: { |
| 828 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 829 | Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
| 830 | Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 831 | Builder.CreateStore(Ops[0], Tmp); |
| 832 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 833 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 834 | } |
| 835 | case X86::BI__builtin_ia32_stmxcsr: { |
| 836 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 837 | Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
| 838 | Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 839 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 840 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 841 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 842 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 843 | case X86::BI__builtin_ia32_cmppd: { |
| 844 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 845 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 846 | } |
| 847 | case X86::BI__builtin_ia32_cmpsd: { |
| 848 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 849 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 850 | } |
| 851 | case X86::BI__builtin_ia32_movss: |
| 852 | return EmitShuffleVector(Ops[0], Ops[1], 4, 1, 2, 3, "movss"); |
Torok Edwin | 730f2c1 | 2008-04-04 06:16:25 +0000 | [diff] [blame] | 853 | case X86::BI__builtin_ia32_shufps: { |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 854 | unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue(); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 855 | return EmitShuffleVector(Ops[0], Ops[1], |
| 856 | i & 0x3, (i & 0xc) >> 2, |
| 857 | ((i & 0x30) >> 4) + 4, |
Mon P Wang | 09b6bf5 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 858 | ((i & 0xc0) >> 6) + 4, "shufps"); |
Torok Edwin | 730f2c1 | 2008-04-04 06:16:25 +0000 | [diff] [blame] | 859 | } |
Mon P Wang | 6c5df7f | 2008-07-18 00:14:09 +0000 | [diff] [blame] | 860 | case X86::BI__builtin_ia32_shufpd: { |
| 861 | unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue(); |
Chris Lattner | f29c8ad | 2009-04-18 07:01:54 +0000 | [diff] [blame] | 862 | return EmitShuffleVector(Ops[0], Ops[1], i & 1, |
| 863 | ((i & 2) >> 1)+2, "shufpd"); |
Mon P Wang | 6c5df7f | 2008-07-18 00:14:09 +0000 | [diff] [blame] | 864 | } |
Nate Begeman | dbebb73 | 2008-04-03 19:58:06 +0000 | [diff] [blame] | 865 | case X86::BI__builtin_ia32_punpcklbw128: |
| 866 | return EmitShuffleVector(Ops[0], Ops[1], 0, 16, 1, 17, 2, 18, 3, 19, |
| 867 | 4, 20, 5, 21, 6, 22, 7, 23, |
| 868 | "punpcklbw"); |
| 869 | case X86::BI__builtin_ia32_punpcklwd128: |
| 870 | return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11, |
| 871 | "punpcklwd"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 872 | case X86::BI__builtin_ia32_movlhps: |
| 873 | return EmitShuffleVector(Ops[0], Ops[1], 0, 1, 4, 5, "movlhps"); |
| 874 | case X86::BI__builtin_ia32_movhlps: |
| 875 | return EmitShuffleVector(Ops[0], Ops[1], 6, 7, 2, 3, "movhlps"); |
| 876 | case X86::BI__builtin_ia32_unpckhps: |
| 877 | return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "unpckhps"); |
| 878 | case X86::BI__builtin_ia32_unpcklps: |
| 879 | return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "unpcklps"); |
Nate Begeman | abfaf99 | 2008-07-25 20:17:24 +0000 | [diff] [blame] | 880 | case X86::BI__builtin_ia32_unpckhpd: |
| 881 | return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "unpckhpd"); |
| 882 | case X86::BI__builtin_ia32_unpcklpd: |
| 883 | return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "unpcklpd"); |
| 884 | case X86::BI__builtin_ia32_movsd: |
| 885 | return EmitShuffleVector(Ops[0], Ops[1], 2, 1, "movsd"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 886 | case X86::BI__builtin_ia32_loadlps: |
| 887 | case X86::BI__builtin_ia32_loadhps: { |
| 888 | // FIXME: This should probably be represented as |
| 889 | // shuffle (dst, (v4f32 (insert undef, (load i64), 0)), shuf mask hi/lo) |
| 890 | const llvm::Type *EltTy = llvm::Type::DoubleTy; |
| 891 | const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 892 | const llvm::Type *OrigTy = Ops[0]->getType(); |
| 893 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_loadlps ? 0 : 1; |
| 894 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index); |
| 895 | Ops[1] = Builder.CreateBitCast(Ops[1], llvm::PointerType::getUnqual(EltTy)); |
| 896 | Ops[1] = Builder.CreateLoad(Ops[1], "tmp"); |
| 897 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 898 | Ops[0] = Builder.CreateInsertElement(Ops[0], Ops[1], Idx, "loadps"); |
| 899 | return Builder.CreateBitCast(Ops[0], OrigTy, "loadps"); |
| 900 | } |
Mon P Wang | 7f57b00 | 2008-09-30 05:31:27 +0000 | [diff] [blame] | 901 | case X86::BI__builtin_ia32_loadlpd: |
| 902 | case X86::BI__builtin_ia32_loadhpd: { |
| 903 | Ops[1] = Builder.CreateLoad(Ops[1], "tmp"); |
| 904 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_loadlpd ? 0 : 1; |
| 905 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index); |
| 906 | return Builder.CreateInsertElement(Ops[0], Ops[1], Idx, "loadpd"); |
| 907 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 908 | case X86::BI__builtin_ia32_storehps: |
| 909 | case X86::BI__builtin_ia32_storelps: { |
| 910 | const llvm::Type *EltTy = llvm::Type::Int64Ty; |
| 911 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 912 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 913 | |
| 914 | // cast val v2i64 |
| 915 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
| 916 | |
| 917 | // extract (0, 1) |
| 918 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
| 919 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index); |
| 920 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 921 | |
| 922 | // cast pointer to i64 & store |
| 923 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 924 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 925 | } |
| 926 | case X86::BI__builtin_ia32_loadlv4si: { |
| 927 | // load i64 |
| 928 | const llvm::Type *EltTy = llvm::Type::Int64Ty; |
| 929 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 930 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 931 | Ops[0] = Builder.CreateLoad(Ops[0], "load"); |
| 932 | |
| 933 | // scalar to vector: insert i64 into 2 x i64 undef |
| 934 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 935 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 936 | Ops[0] = Builder.CreateInsertElement(llvm::UndefValue::get(VecTy), |
| 937 | Ops[0], Zero, "s2v"); |
| 938 | |
| 939 | // shuffle into zero vector. |
| 940 | std::vector<llvm::Constant *>Elts; |
| 941 | Elts.resize(2, llvm::ConstantInt::get(EltTy, 0)); |
| 942 | llvm::Value *ZV = ConstantVector::get(Elts); |
| 943 | Ops[0] = EmitShuffleVector(ZV, Ops[0], 2, 1, "loadl"); |
| 944 | |
| 945 | // bitcast to result. |
| 946 | return Builder.CreateBitCast(Ops[0], |
| 947 | llvm::VectorType::get(llvm::Type::Int32Ty, 4)); |
| 948 | } |
Nate Begeman | 2451250 | 2008-05-16 18:59:42 +0000 | [diff] [blame] | 949 | case X86::BI__builtin_ia32_vec_set_v4hi: |
| 950 | case X86::BI__builtin_ia32_vec_set_v8hi: |
| 951 | return Builder.CreateInsertElement(Ops[0], Ops[1], Ops[2], "pinsrw"); |
Mon P Wang | bc4f403 | 2008-10-15 06:18:34 +0000 | [diff] [blame] | 952 | case X86::BI__builtin_ia32_vec_set_v4si: |
| 953 | return Builder.CreateInsertElement(Ops[0], Ops[1], Ops[2], "pinsrd"); |
| 954 | case X86::BI__builtin_ia32_vec_set_v2di: |
| 955 | return Builder.CreateInsertElement(Ops[0], Ops[1], Ops[2], "pinsrq"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 956 | case X86::BI__builtin_ia32_andps: |
| 957 | case X86::BI__builtin_ia32_andpd: |
| 958 | case X86::BI__builtin_ia32_andnps: |
| 959 | case X86::BI__builtin_ia32_andnpd: |
| 960 | case X86::BI__builtin_ia32_orps: |
| 961 | case X86::BI__builtin_ia32_orpd: |
| 962 | case X86::BI__builtin_ia32_xorpd: |
| 963 | case X86::BI__builtin_ia32_xorps: { |
| 964 | const llvm::Type *ITy = llvm::VectorType::get(llvm::Type::Int32Ty, 4); |
| 965 | const llvm::Type *FTy = Ops[0]->getType(); |
| 966 | Ops[0] = Builder.CreateBitCast(Ops[0], ITy, "bitcast"); |
| 967 | Ops[1] = Builder.CreateBitCast(Ops[1], ITy, "bitcast"); |
| 968 | switch (BuiltinID) { |
| 969 | case X86::BI__builtin_ia32_andps: |
| 970 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andps"); |
| 971 | break; |
| 972 | case X86::BI__builtin_ia32_andpd: |
| 973 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andpd"); |
| 974 | break; |
| 975 | case X86::BI__builtin_ia32_andnps: |
| 976 | Ops[0] = Builder.CreateNot(Ops[0], "not"); |
| 977 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andnps"); |
| 978 | break; |
| 979 | case X86::BI__builtin_ia32_andnpd: |
| 980 | Ops[0] = Builder.CreateNot(Ops[0], "not"); |
| 981 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andnpd"); |
| 982 | break; |
| 983 | case X86::BI__builtin_ia32_orps: |
| 984 | Ops[0] = Builder.CreateOr(Ops[0], Ops[1], "orps"); |
| 985 | break; |
| 986 | case X86::BI__builtin_ia32_orpd: |
| 987 | Ops[0] = Builder.CreateOr(Ops[0], Ops[1], "orpd"); |
| 988 | break; |
| 989 | case X86::BI__builtin_ia32_xorps: |
| 990 | Ops[0] = Builder.CreateXor(Ops[0], Ops[1], "xorps"); |
| 991 | break; |
| 992 | case X86::BI__builtin_ia32_xorpd: |
| 993 | Ops[0] = Builder.CreateXor(Ops[0], Ops[1], "xorpd"); |
| 994 | break; |
| 995 | } |
| 996 | return Builder.CreateBitCast(Ops[0], FTy, "bitcast"); |
| 997 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 1001 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
| 1002 | const CallExpr *E) { |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 1003 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 1004 | default: return 0; |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |