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