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