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