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 | } |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 195 | case Builtin::BI__builtin_expect: |
| 196 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 197 | case Builtin::BI__builtin_bswap32: |
| 198 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 199 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 200 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 201 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 202 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 203 | } |
Anders Carlsson | c8ee798 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 204 | case Builtin::BI__builtin_inff: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 205 | APFloat f(APFloat::IEEEsingle, APFloat::fcInfinity, false); |
Chris Lattner | 2c62a1a | 2008-05-04 18:23:51 +0000 | [diff] [blame] | 206 | return RValue::get(ConstantFP::get(f)); |
Anders Carlsson | c8ee798 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 207 | } |
Anders Carlsson | 478547c | 2008-02-01 16:06:34 +0000 | [diff] [blame] | 208 | case Builtin::BI__builtin_huge_val: |
Anders Carlsson | c8ee798 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 209 | case Builtin::BI__builtin_inf: |
| 210 | // FIXME: mapping long double onto double. |
| 211 | case Builtin::BI__builtin_infl: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 212 | APFloat f(APFloat::IEEEdouble, APFloat::fcInfinity, false); |
Chris Lattner | 2c62a1a | 2008-05-04 18:23:51 +0000 | [diff] [blame] | 213 | return RValue::get(ConstantFP::get(f)); |
Anders Carlsson | c8ee798 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 215 | case Builtin::BI__builtin_isgreater: |
| 216 | case Builtin::BI__builtin_isgreaterequal: |
| 217 | case Builtin::BI__builtin_isless: |
| 218 | case Builtin::BI__builtin_islessequal: |
| 219 | case Builtin::BI__builtin_islessgreater: |
| 220 | case Builtin::BI__builtin_isunordered: { |
| 221 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 222 | // floating point values. |
| 223 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
| 224 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
| 225 | |
| 226 | switch (BuiltinID) { |
| 227 | default: assert(0 && "Unknown ordered comparison"); |
| 228 | case Builtin::BI__builtin_isgreater: |
| 229 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 230 | break; |
| 231 | case Builtin::BI__builtin_isgreaterequal: |
| 232 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 233 | break; |
| 234 | case Builtin::BI__builtin_isless: |
| 235 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 236 | break; |
| 237 | case Builtin::BI__builtin_islessequal: |
| 238 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 239 | break; |
| 240 | case Builtin::BI__builtin_islessgreater: |
| 241 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 242 | break; |
| 243 | case Builtin::BI__builtin_isunordered: |
| 244 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 245 | break; |
| 246 | } |
| 247 | // ZExt bool to int type. |
| 248 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 249 | "tmp")); |
| 250 | } |
Nate Begeman | 4efe459 | 2008-02-06 00:02:50 +0000 | [diff] [blame] | 251 | case Builtin::BI__builtin_alloca: |
| 252 | return RValue::get(Builder.CreateAlloca(llvm::Type::Int8Ty, |
| 253 | EmitScalarExpr(E->getArg(0)), |
| 254 | "tmp")); |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame^] | 255 | case Builtin::BI__sync_fetch_and_add: |
| 256 | return EmitBinaryAtomic(*this, Intrinsic::atomic_las, E); |
| 257 | case Builtin::BI__sync_fetch_and_sub: |
| 258 | return EmitBinaryAtomic(*this, Intrinsic::atomic_lss, E); |
| 259 | case Builtin::BI__sync_fetch_and_min: |
| 260 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 261 | case Builtin::BI__sync_fetch_and_max: |
| 262 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 263 | case Builtin::BI__sync_fetch_and_umin: |
| 264 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 265 | case Builtin::BI__sync_fetch_and_umax: |
| 266 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
| 267 | case Builtin::BI__sync_fetch_and_and: |
| 268 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 269 | case Builtin::BI__sync_fetch_and_or: |
| 270 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 271 | case Builtin::BI__sync_fetch_and_xor: |
| 272 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
| 273 | case Builtin::BI__sync_val_compare_and_swap: { |
| 274 | Value *Args[3]; |
| 275 | Args[0]= EmitScalarExpr(E->getArg(0)); |
| 276 | Args[1] = EmitScalarExpr(E->getArg(1)); |
| 277 | Args[2] = EmitScalarExpr(E->getArg(2)); |
| 278 | const llvm::Type *ResType = ConvertType(E->getType()); |
| 279 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_lcs, &ResType, 1); |
| 280 | return RValue::get(Builder.CreateCall(AtomF, &Args[0], &Args[1]+2)); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 281 | } |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame^] | 282 | case Builtin::BI__sync_lock_test_and_set: |
| 283 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); } |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 284 | return RValue::get(0); |
| 285 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 287 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
| 288 | const CallExpr *E) { |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 289 | |
| 290 | llvm::SmallVector<Value*, 4> Ops; |
| 291 | |
| 292 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 293 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 294 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 295 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 296 | default: return 0; |
| 297 | case X86::BI__builtin_ia32_mulps: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 298 | return Builder.CreateMul(Ops[0], Ops[1], "mulps"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 299 | case X86::BI__builtin_ia32_mulpd: |
| 300 | return Builder.CreateMul(Ops[0], Ops[1], "mulpd"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 301 | case X86::BI__builtin_ia32_pand: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 302 | case X86::BI__builtin_ia32_pand128: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 303 | return Builder.CreateAnd(Ops[0], Ops[1], "pand"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 304 | case X86::BI__builtin_ia32_por: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 305 | case X86::BI__builtin_ia32_por128: |
| 306 | return Builder.CreateOr(Ops[0], Ops[1], "por"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 307 | case X86::BI__builtin_ia32_pxor: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 308 | case X86::BI__builtin_ia32_pxor128: |
| 309 | return Builder.CreateXor(Ops[0], Ops[1], "pxor"); |
| 310 | case X86::BI__builtin_ia32_pandn: |
| 311 | case X86::BI__builtin_ia32_pandn128: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 312 | Ops[0] = Builder.CreateNot(Ops[0], "tmp"); |
| 313 | return Builder.CreateAnd(Ops[0], Ops[1], "pandn"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 314 | case X86::BI__builtin_ia32_paddb: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 315 | case X86::BI__builtin_ia32_paddb128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 316 | case X86::BI__builtin_ia32_paddd: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 317 | case X86::BI__builtin_ia32_paddd128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 318 | case X86::BI__builtin_ia32_paddq: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 319 | case X86::BI__builtin_ia32_paddq128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 320 | case X86::BI__builtin_ia32_paddw: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 321 | case X86::BI__builtin_ia32_paddw128: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 322 | case X86::BI__builtin_ia32_addps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 323 | case X86::BI__builtin_ia32_addpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 324 | return Builder.CreateAdd(Ops[0], Ops[1], "add"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 325 | case X86::BI__builtin_ia32_psubb: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 326 | case X86::BI__builtin_ia32_psubb128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 327 | case X86::BI__builtin_ia32_psubd: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 328 | case X86::BI__builtin_ia32_psubd128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 329 | case X86::BI__builtin_ia32_psubq: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 330 | case X86::BI__builtin_ia32_psubq128: |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 331 | case X86::BI__builtin_ia32_psubw: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 332 | case X86::BI__builtin_ia32_psubw128: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 333 | case X86::BI__builtin_ia32_subps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 334 | case X86::BI__builtin_ia32_subpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 335 | return Builder.CreateSub(Ops[0], Ops[1], "sub"); |
| 336 | case X86::BI__builtin_ia32_divps: |
| 337 | return Builder.CreateFDiv(Ops[0], Ops[1], "divps"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 338 | case X86::BI__builtin_ia32_divpd: |
| 339 | return Builder.CreateFDiv(Ops[0], Ops[1], "divpd"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 340 | case X86::BI__builtin_ia32_pmullw: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 341 | case X86::BI__builtin_ia32_pmullw128: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 342 | return Builder.CreateMul(Ops[0], Ops[1], "pmul"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 343 | case X86::BI__builtin_ia32_punpckhbw: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 344 | 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] | 345 | "punpckhbw"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 346 | case X86::BI__builtin_ia32_punpckhbw128: |
| 347 | return EmitShuffleVector(Ops[0], Ops[1], 8, 24, 9, 25, 10, 26, 11, 27, |
| 348 | 12, 28, 13, 29, 14, 30, 15, 31, |
| 349 | "punpckhbw"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 350 | case X86::BI__builtin_ia32_punpckhwd: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 351 | return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhwd"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 352 | case X86::BI__builtin_ia32_punpckhwd128: |
| 353 | return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15, |
| 354 | "punpckhwd"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 355 | case X86::BI__builtin_ia32_punpckhdq: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 356 | return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhdq"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 357 | case X86::BI__builtin_ia32_punpckhdq128: |
| 358 | return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhdq"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 359 | case X86::BI__builtin_ia32_punpcklbw: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 360 | 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] | 361 | "punpcklbw"); |
| 362 | case X86::BI__builtin_ia32_punpcklwd: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 363 | return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpcklwd"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 364 | case X86::BI__builtin_ia32_punpckldq: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 365 | return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpckldq"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 366 | case X86::BI__builtin_ia32_punpckldq128: |
| 367 | return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpckldq"); |
| 368 | case X86::BI__builtin_ia32_pslldi128: |
| 369 | case X86::BI__builtin_ia32_psllqi128: |
| 370 | case X86::BI__builtin_ia32_psllwi128: |
| 371 | case X86::BI__builtin_ia32_psradi128: |
| 372 | case X86::BI__builtin_ia32_psrawi128: |
| 373 | case X86::BI__builtin_ia32_psrldi128: |
| 374 | case X86::BI__builtin_ia32_psrlqi128: |
| 375 | case X86::BI__builtin_ia32_psrlwi128: { |
| 376 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); |
| 377 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 2); |
| 378 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 379 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
| 380 | Ops[1], Zero, "insert"); |
| 381 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 382 | const char *name = 0; |
| 383 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 384 | |
| 385 | switch (BuiltinID) { |
| 386 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 387 | case X86::BI__builtin_ia32_pslldi128: |
| 388 | name = "pslldi"; |
| 389 | ID = Intrinsic::x86_sse2_psll_d; |
| 390 | break; |
| 391 | case X86::BI__builtin_ia32_psllqi128: |
| 392 | name = "psllqi"; |
| 393 | ID = Intrinsic::x86_sse2_psll_q; |
| 394 | break; |
| 395 | case X86::BI__builtin_ia32_psllwi128: |
| 396 | name = "psllwi"; |
| 397 | ID = Intrinsic::x86_sse2_psll_w; |
| 398 | break; |
| 399 | case X86::BI__builtin_ia32_psradi128: |
| 400 | name = "psradi"; |
| 401 | ID = Intrinsic::x86_sse2_psra_d; |
| 402 | break; |
| 403 | case X86::BI__builtin_ia32_psrawi128: |
| 404 | name = "psrawi"; |
| 405 | ID = Intrinsic::x86_sse2_psra_w; |
| 406 | break; |
| 407 | case X86::BI__builtin_ia32_psrldi128: |
| 408 | name = "psrldi"; |
| 409 | ID = Intrinsic::x86_sse2_psrl_d; |
| 410 | break; |
| 411 | case X86::BI__builtin_ia32_psrlqi128: |
| 412 | name = "psrlqi"; |
| 413 | ID = Intrinsic::x86_sse2_psrl_q; |
| 414 | break; |
| 415 | case X86::BI__builtin_ia32_psrlwi128: |
| 416 | name = "psrlwi"; |
| 417 | ID = Intrinsic::x86_sse2_psrl_w; |
| 418 | break; |
| 419 | } |
| 420 | llvm::Function *F = CGM.getIntrinsic(ID); |
| 421 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 422 | } |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 423 | case X86::BI__builtin_ia32_pslldi: |
| 424 | case X86::BI__builtin_ia32_psllqi: |
| 425 | case X86::BI__builtin_ia32_psllwi: |
| 426 | case X86::BI__builtin_ia32_psradi: |
| 427 | case X86::BI__builtin_ia32_psrawi: |
| 428 | case X86::BI__builtin_ia32_psrldi: |
| 429 | case X86::BI__builtin_ia32_psrlqi: |
| 430 | case X86::BI__builtin_ia32_psrlwi: { |
| 431 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); |
| 432 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1); |
| 433 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 434 | const char *name = 0; |
| 435 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 436 | |
| 437 | switch (BuiltinID) { |
| 438 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 439 | case X86::BI__builtin_ia32_pslldi: |
| 440 | name = "pslldi"; |
| 441 | ID = Intrinsic::x86_mmx_psll_d; |
| 442 | break; |
| 443 | case X86::BI__builtin_ia32_psllqi: |
| 444 | name = "psllqi"; |
| 445 | ID = Intrinsic::x86_mmx_psll_q; |
| 446 | break; |
| 447 | case X86::BI__builtin_ia32_psllwi: |
| 448 | name = "psllwi"; |
| 449 | ID = Intrinsic::x86_mmx_psll_w; |
| 450 | break; |
| 451 | case X86::BI__builtin_ia32_psradi: |
| 452 | name = "psradi"; |
| 453 | ID = Intrinsic::x86_mmx_psra_d; |
| 454 | break; |
| 455 | case X86::BI__builtin_ia32_psrawi: |
| 456 | name = "psrawi"; |
| 457 | ID = Intrinsic::x86_mmx_psra_w; |
| 458 | break; |
| 459 | case X86::BI__builtin_ia32_psrldi: |
| 460 | name = "psrldi"; |
| 461 | ID = Intrinsic::x86_mmx_psrl_d; |
| 462 | break; |
| 463 | case X86::BI__builtin_ia32_psrlqi: |
| 464 | name = "psrlqi"; |
| 465 | ID = Intrinsic::x86_mmx_psrl_q; |
| 466 | break; |
| 467 | case X86::BI__builtin_ia32_psrlwi: |
| 468 | name = "psrlwi"; |
| 469 | ID = Intrinsic::x86_mmx_psrl_w; |
| 470 | break; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 471 | case X86::BI__builtin_ia32_pslldi128: |
| 472 | name = "pslldi"; |
| 473 | ID = Intrinsic::x86_sse2_psll_d; |
| 474 | break; |
| 475 | case X86::BI__builtin_ia32_psllqi128: |
| 476 | name = "psllqi"; |
| 477 | ID = Intrinsic::x86_sse2_psll_q; |
| 478 | break; |
| 479 | case X86::BI__builtin_ia32_psllwi128: |
| 480 | name = "psllwi"; |
| 481 | ID = Intrinsic::x86_sse2_psll_w; |
| 482 | break; |
| 483 | case X86::BI__builtin_ia32_psradi128: |
| 484 | name = "psradi"; |
| 485 | ID = Intrinsic::x86_sse2_psra_d; |
| 486 | break; |
| 487 | case X86::BI__builtin_ia32_psrawi128: |
| 488 | name = "psrawi"; |
| 489 | ID = Intrinsic::x86_sse2_psra_w; |
| 490 | break; |
| 491 | case X86::BI__builtin_ia32_psrldi128: |
| 492 | name = "psrldi"; |
| 493 | ID = Intrinsic::x86_sse2_psrl_d; |
| 494 | break; |
| 495 | case X86::BI__builtin_ia32_psrlqi128: |
| 496 | name = "psrlqi"; |
| 497 | ID = Intrinsic::x86_sse2_psrl_q; |
| 498 | break; |
| 499 | case X86::BI__builtin_ia32_psrlwi128: |
| 500 | name = "psrlwi"; |
| 501 | ID = Intrinsic::x86_sse2_psrl_w; |
| 502 | break; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 503 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 504 | llvm::Function *F = CGM.getIntrinsic(ID); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 505 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 506 | } |
| 507 | case X86::BI__builtin_ia32_pshufd: { |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 508 | unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 509 | return EmitShuffleVector(Ops[0], Ops[0], |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 510 | i & 0x3, (i & 0xc) >> 2, |
| 511 | (i & 0x30) >> 4, (i & 0xc0) >> 6, |
| 512 | "pshufd"); |
| 513 | } |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 514 | case X86::BI__builtin_ia32_vec_init_v4hi: |
| 515 | case X86::BI__builtin_ia32_vec_init_v8qi: |
| 516 | case X86::BI__builtin_ia32_vec_init_v2si: |
| 517 | return EmitVector(&Ops[0], Ops.size()); |
| 518 | case X86::BI__builtin_ia32_vec_ext_v2si: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 519 | case X86::BI__builtin_ia32_vec_ext_v2di: |
| 520 | case X86::BI__builtin_ia32_vec_ext_v4sf: |
| 521 | case X86::BI__builtin_ia32_vec_ext_v4si: |
| 522 | case X86::BI__builtin_ia32_vec_ext_v2df: |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 523 | return Builder.CreateExtractElement(Ops[0], Ops[1], "result"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 524 | case X86::BI__builtin_ia32_cmpordss: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 525 | case X86::BI__builtin_ia32_cmpordsd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 526 | case X86::BI__builtin_ia32_cmpunordss: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 527 | case X86::BI__builtin_ia32_cmpunordsd: |
| 528 | case X86::BI__builtin_ia32_cmpeqss: |
| 529 | case X86::BI__builtin_ia32_cmpeqsd: |
| 530 | case X86::BI__builtin_ia32_cmpltss: |
| 531 | case X86::BI__builtin_ia32_cmpltsd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 532 | case X86::BI__builtin_ia32_cmpless: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 533 | case X86::BI__builtin_ia32_cmplesd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 534 | case X86::BI__builtin_ia32_cmpneqss: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 535 | case X86::BI__builtin_ia32_cmpneqsd: |
| 536 | case X86::BI__builtin_ia32_cmpnltss: |
| 537 | case X86::BI__builtin_ia32_cmpnltsd: |
| 538 | case X86::BI__builtin_ia32_cmpnless: |
| 539 | case X86::BI__builtin_ia32_cmpnlesd: { |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 540 | unsigned i = 0; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 541 | const char *name = 0; |
| 542 | switch (BuiltinID) { |
| 543 | default: assert(0 && "Unknown compare builtin!"); |
| 544 | case X86::BI__builtin_ia32_cmpeqss: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 545 | case X86::BI__builtin_ia32_cmpeqsd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 546 | i = 0; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 547 | name = "cmpeq"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 548 | break; |
| 549 | case X86::BI__builtin_ia32_cmpltss: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 550 | case X86::BI__builtin_ia32_cmpltsd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 551 | i = 1; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 552 | name = "cmplt"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 553 | break; |
| 554 | case X86::BI__builtin_ia32_cmpless: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 555 | case X86::BI__builtin_ia32_cmplesd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 556 | i = 2; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 557 | name = "cmple"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 558 | break; |
| 559 | case X86::BI__builtin_ia32_cmpunordss: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 560 | case X86::BI__builtin_ia32_cmpunordsd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 561 | i = 3; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 562 | name = "cmpunord"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 563 | break; |
| 564 | case X86::BI__builtin_ia32_cmpneqss: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 565 | case X86::BI__builtin_ia32_cmpneqsd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 566 | i = 4; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 567 | name = "cmpneq"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 568 | break; |
| 569 | case X86::BI__builtin_ia32_cmpnltss: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 570 | case X86::BI__builtin_ia32_cmpnltsd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 571 | i = 5; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 572 | name = "cmpntl"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 573 | break; |
| 574 | case X86::BI__builtin_ia32_cmpnless: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 575 | case X86::BI__builtin_ia32_cmpnlesd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 576 | i = 6; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 577 | name = "cmpnle"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 578 | break; |
| 579 | case X86::BI__builtin_ia32_cmpordss: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 580 | case X86::BI__builtin_ia32_cmpordsd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 581 | i = 7; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 582 | name = "cmpord"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 583 | break; |
| 584 | } |
| 585 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 586 | llvm::Function *F; |
| 587 | if (cast<llvm::VectorType>(Ops[0]->getType())->getElementType() == |
| 588 | llvm::Type::FloatTy) |
| 589 | F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 590 | else |
| 591 | F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 592 | |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 593 | Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i)); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 594 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 595 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 596 | case X86::BI__builtin_ia32_ldmxcsr: { |
| 597 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 598 | Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
| 599 | Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 600 | Builder.CreateStore(Ops[0], Tmp); |
| 601 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 602 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 603 | } |
| 604 | case X86::BI__builtin_ia32_stmxcsr: { |
| 605 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 606 | Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
| 607 | Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 608 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 609 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 610 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 611 | } |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 612 | case X86::BI__builtin_ia32_cmpordps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 613 | case X86::BI__builtin_ia32_cmpordpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 614 | case X86::BI__builtin_ia32_cmpunordps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 615 | case X86::BI__builtin_ia32_cmpunordpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 616 | case X86::BI__builtin_ia32_cmpeqps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 617 | case X86::BI__builtin_ia32_cmpeqpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 618 | case X86::BI__builtin_ia32_cmpltps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 619 | case X86::BI__builtin_ia32_cmpltpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 620 | case X86::BI__builtin_ia32_cmpleps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 621 | case X86::BI__builtin_ia32_cmplepd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 622 | case X86::BI__builtin_ia32_cmpneqps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 623 | case X86::BI__builtin_ia32_cmpneqpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 624 | case X86::BI__builtin_ia32_cmpngtps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 625 | case X86::BI__builtin_ia32_cmpngtpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 626 | case X86::BI__builtin_ia32_cmpnltps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 627 | case X86::BI__builtin_ia32_cmpnltpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 628 | case X86::BI__builtin_ia32_cmpgtps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 629 | case X86::BI__builtin_ia32_cmpgtpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 630 | case X86::BI__builtin_ia32_cmpgeps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 631 | case X86::BI__builtin_ia32_cmpgepd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 632 | case X86::BI__builtin_ia32_cmpngeps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 633 | case X86::BI__builtin_ia32_cmpngepd: |
| 634 | case X86::BI__builtin_ia32_cmpnleps: |
| 635 | case X86::BI__builtin_ia32_cmpnlepd: { |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 636 | unsigned i = 0; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 637 | const char *name = 0; |
| 638 | bool ShouldSwap = false; |
| 639 | switch (BuiltinID) { |
| 640 | default: assert(0 && "Unknown compare builtin!"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 641 | case X86::BI__builtin_ia32_cmpeqps: |
| 642 | case X86::BI__builtin_ia32_cmpeqpd: i = 0; name = "cmpeq"; break; |
| 643 | case X86::BI__builtin_ia32_cmpltps: |
| 644 | case X86::BI__builtin_ia32_cmpltpd: i = 1; name = "cmplt"; break; |
| 645 | case X86::BI__builtin_ia32_cmpleps: |
| 646 | case X86::BI__builtin_ia32_cmplepd: i = 2; name = "cmple"; break; |
| 647 | case X86::BI__builtin_ia32_cmpunordps: |
| 648 | case X86::BI__builtin_ia32_cmpunordpd: i = 3; name = "cmpunord"; break; |
| 649 | case X86::BI__builtin_ia32_cmpneqps: |
| 650 | case X86::BI__builtin_ia32_cmpneqpd: i = 4; name = "cmpneq"; break; |
| 651 | case X86::BI__builtin_ia32_cmpnltps: |
| 652 | case X86::BI__builtin_ia32_cmpnltpd: i = 5; name = "cmpntl"; break; |
| 653 | case X86::BI__builtin_ia32_cmpnleps: |
| 654 | case X86::BI__builtin_ia32_cmpnlepd: i = 6; name = "cmpnle"; break; |
| 655 | case X86::BI__builtin_ia32_cmpordps: |
| 656 | case X86::BI__builtin_ia32_cmpordpd: i = 7; name = "cmpord"; break; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 657 | case X86::BI__builtin_ia32_cmpgtps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 658 | case X86::BI__builtin_ia32_cmpgtpd: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 659 | ShouldSwap = true; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 660 | i = 1; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 661 | name = "cmpgt"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 662 | break; |
| 663 | case X86::BI__builtin_ia32_cmpgeps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 664 | case X86::BI__builtin_ia32_cmpgepd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 665 | i = 2; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 666 | name = "cmpge"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 667 | ShouldSwap = true; |
| 668 | break; |
| 669 | case X86::BI__builtin_ia32_cmpngtps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 670 | case X86::BI__builtin_ia32_cmpngtpd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 671 | i = 5; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 672 | name = "cmpngt"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 673 | ShouldSwap = true; |
| 674 | break; |
| 675 | case X86::BI__builtin_ia32_cmpngeps: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 676 | case X86::BI__builtin_ia32_cmpngepd: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 677 | i = 6; |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 678 | name = "cmpnge"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 679 | ShouldSwap = true; |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | if (ShouldSwap) |
| 684 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 685 | |
| 686 | llvm::Function *F; |
| 687 | if (cast<llvm::VectorType>(Ops[0]->getType())->getElementType() == |
| 688 | llvm::Type::FloatTy) |
| 689 | F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 690 | else |
| 691 | F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 692 | |
| 693 | Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i)); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 694 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 695 | } |
| 696 | case X86::BI__builtin_ia32_movss: |
| 697 | return EmitShuffleVector(Ops[0], Ops[1], 4, 1, 2, 3, "movss"); |
Torok Edwin | 730f2c1 | 2008-04-04 06:16:25 +0000 | [diff] [blame] | 698 | case X86::BI__builtin_ia32_shufps: { |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 699 | unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue(); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 700 | return EmitShuffleVector(Ops[0], Ops[1], |
| 701 | i & 0x3, (i & 0xc) >> 2, |
| 702 | ((i & 0x30) >> 4) + 4, |
| 703 | ((i & 0x60) >> 6) + 4, "shufps"); |
Torok Edwin | 730f2c1 | 2008-04-04 06:16:25 +0000 | [diff] [blame] | 704 | } |
Nate Begeman | dbebb73 | 2008-04-03 19:58:06 +0000 | [diff] [blame] | 705 | case X86::BI__builtin_ia32_punpcklbw128: |
| 706 | return EmitShuffleVector(Ops[0], Ops[1], 0, 16, 1, 17, 2, 18, 3, 19, |
| 707 | 4, 20, 5, 21, 6, 22, 7, 23, |
| 708 | "punpcklbw"); |
| 709 | case X86::BI__builtin_ia32_punpcklwd128: |
| 710 | return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11, |
| 711 | "punpcklwd"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 712 | case X86::BI__builtin_ia32_movlhps: |
| 713 | return EmitShuffleVector(Ops[0], Ops[1], 0, 1, 4, 5, "movlhps"); |
| 714 | case X86::BI__builtin_ia32_movhlps: |
| 715 | return EmitShuffleVector(Ops[0], Ops[1], 6, 7, 2, 3, "movhlps"); |
| 716 | case X86::BI__builtin_ia32_unpckhps: |
| 717 | return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "unpckhps"); |
| 718 | case X86::BI__builtin_ia32_unpcklps: |
| 719 | return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "unpcklps"); |
| 720 | case X86::BI__builtin_ia32_movqv4si: { |
| 721 | llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 2); |
| 722 | return Builder.CreateBitCast(Ops[0], Ty); |
| 723 | } |
| 724 | case X86::BI__builtin_ia32_loadlps: |
| 725 | case X86::BI__builtin_ia32_loadhps: { |
| 726 | // FIXME: This should probably be represented as |
| 727 | // shuffle (dst, (v4f32 (insert undef, (load i64), 0)), shuf mask hi/lo) |
| 728 | const llvm::Type *EltTy = llvm::Type::DoubleTy; |
| 729 | const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 730 | const llvm::Type *OrigTy = Ops[0]->getType(); |
| 731 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_loadlps ? 0 : 1; |
| 732 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index); |
| 733 | Ops[1] = Builder.CreateBitCast(Ops[1], llvm::PointerType::getUnqual(EltTy)); |
| 734 | Ops[1] = Builder.CreateLoad(Ops[1], "tmp"); |
| 735 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 736 | Ops[0] = Builder.CreateInsertElement(Ops[0], Ops[1], Idx, "loadps"); |
| 737 | return Builder.CreateBitCast(Ops[0], OrigTy, "loadps"); |
| 738 | } |
| 739 | case X86::BI__builtin_ia32_storehps: |
| 740 | case X86::BI__builtin_ia32_storelps: { |
| 741 | const llvm::Type *EltTy = llvm::Type::Int64Ty; |
| 742 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 743 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 744 | |
| 745 | // cast val v2i64 |
| 746 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
| 747 | |
| 748 | // extract (0, 1) |
| 749 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
| 750 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index); |
| 751 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 752 | |
| 753 | // cast pointer to i64 & store |
| 754 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 755 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 756 | } |
| 757 | case X86::BI__builtin_ia32_loadlv4si: { |
| 758 | // load i64 |
| 759 | const llvm::Type *EltTy = llvm::Type::Int64Ty; |
| 760 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 761 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 762 | Ops[0] = Builder.CreateLoad(Ops[0], "load"); |
| 763 | |
| 764 | // scalar to vector: insert i64 into 2 x i64 undef |
| 765 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 766 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 767 | Ops[0] = Builder.CreateInsertElement(llvm::UndefValue::get(VecTy), |
| 768 | Ops[0], Zero, "s2v"); |
| 769 | |
| 770 | // shuffle into zero vector. |
| 771 | std::vector<llvm::Constant *>Elts; |
| 772 | Elts.resize(2, llvm::ConstantInt::get(EltTy, 0)); |
| 773 | llvm::Value *ZV = ConstantVector::get(Elts); |
| 774 | Ops[0] = EmitShuffleVector(ZV, Ops[0], 2, 1, "loadl"); |
| 775 | |
| 776 | // bitcast to result. |
| 777 | return Builder.CreateBitCast(Ops[0], |
| 778 | llvm::VectorType::get(llvm::Type::Int32Ty, 4)); |
| 779 | } |
| 780 | case X86::BI__builtin_ia32_andps: |
| 781 | case X86::BI__builtin_ia32_andpd: |
| 782 | case X86::BI__builtin_ia32_andnps: |
| 783 | case X86::BI__builtin_ia32_andnpd: |
| 784 | case X86::BI__builtin_ia32_orps: |
| 785 | case X86::BI__builtin_ia32_orpd: |
| 786 | case X86::BI__builtin_ia32_xorpd: |
| 787 | case X86::BI__builtin_ia32_xorps: { |
| 788 | const llvm::Type *ITy = llvm::VectorType::get(llvm::Type::Int32Ty, 4); |
| 789 | const llvm::Type *FTy = Ops[0]->getType(); |
| 790 | Ops[0] = Builder.CreateBitCast(Ops[0], ITy, "bitcast"); |
| 791 | Ops[1] = Builder.CreateBitCast(Ops[1], ITy, "bitcast"); |
| 792 | switch (BuiltinID) { |
| 793 | case X86::BI__builtin_ia32_andps: |
| 794 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andps"); |
| 795 | break; |
| 796 | case X86::BI__builtin_ia32_andpd: |
| 797 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andpd"); |
| 798 | break; |
| 799 | case X86::BI__builtin_ia32_andnps: |
| 800 | Ops[0] = Builder.CreateNot(Ops[0], "not"); |
| 801 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andnps"); |
| 802 | break; |
| 803 | case X86::BI__builtin_ia32_andnpd: |
| 804 | Ops[0] = Builder.CreateNot(Ops[0], "not"); |
| 805 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andnpd"); |
| 806 | break; |
| 807 | case X86::BI__builtin_ia32_orps: |
| 808 | Ops[0] = Builder.CreateOr(Ops[0], Ops[1], "orps"); |
| 809 | break; |
| 810 | case X86::BI__builtin_ia32_orpd: |
| 811 | Ops[0] = Builder.CreateOr(Ops[0], Ops[1], "orpd"); |
| 812 | break; |
| 813 | case X86::BI__builtin_ia32_xorps: |
| 814 | Ops[0] = Builder.CreateXor(Ops[0], Ops[1], "xorps"); |
| 815 | break; |
| 816 | case X86::BI__builtin_ia32_xorpd: |
| 817 | Ops[0] = Builder.CreateXor(Ops[0], Ops[1], "xorpd"); |
| 818 | break; |
| 819 | } |
| 820 | return Builder.CreateBitCast(Ops[0], FTy, "bitcast"); |
| 821 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 825 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
| 826 | const CallExpr *E) { |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 827 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 828 | default: return 0; |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 829 | } |
| 830 | } |