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