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