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