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