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