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