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 | |
Chris Lattner | 6de93ff | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 28 | RValue CodeGenFunction::EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E) { |
| 29 | switch (BuiltinID) { |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 30 | default: { |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 31 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID)) |
Nate Begeman | eed5058 | 2008-01-17 18:06:09 +0000 | [diff] [blame] | 32 | return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID), |
Eli Friedman | 5193b8a | 2008-01-30 01:32:06 +0000 | [diff] [blame] | 33 | E->getCallee()->getType(), E->arg_begin(), |
| 34 | E->getNumArgs()); |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 35 | |
| 36 | // See if we have a target specific intrinsic. |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 37 | Intrinsic::ID IntrinsicID; |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 38 | const char *TargetPrefix = Target.getTargetPrefix(); |
| 39 | const char *BuiltinName = getContext().BuiltinInfo.GetName(BuiltinID); |
| 40 | #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
| 41 | #include "llvm/Intrinsics.gen" |
| 42 | #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
| 43 | |
| 44 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 45 | SmallVector<Value*, 16> Args; |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 47 | Function *F = CGM.getIntrinsic(IntrinsicID); |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 48 | const llvm::FunctionType *FTy = F->getFunctionType(); |
| 49 | |
| 50 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 51 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 52 | |
| 53 | // If the intrinsic arg type is different from the builtin arg type |
| 54 | // we need to do a bit cast. |
| 55 | const llvm::Type *PTy = FTy->getParamType(i); |
| 56 | if (PTy != ArgValue->getType()) { |
| 57 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 58 | "Must be able to losslessly bit cast to param"); |
| 59 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 60 | } |
| 61 | |
| 62 | Args.push_back(ArgValue); |
| 63 | } |
| 64 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 65 | Value *V = Builder.CreateCall(F, &Args[0], &Args[0] + Args.size()); |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 66 | QualType BuiltinRetType = E->getType(); |
| 67 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 68 | const llvm::Type *RetTy = llvm::Type::VoidTy; |
| 69 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 70 | |
| 71 | if (RetTy != V->getType()) { |
| 72 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 73 | "Must be able to losslessly bit cast result type"); |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 74 | V = Builder.CreateBitCast(V, RetTy); |
| 75 | } |
| 76 | |
| 77 | return RValue::get(V); |
| 78 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 79 | |
| 80 | // 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] | 81 | Value *V = 0; |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 82 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 83 | if (strcmp(TargetPrefix, "x86") == 0) |
| 84 | V = EmitX86BuiltinExpr(BuiltinID, E); |
| 85 | else if (strcmp(TargetPrefix, "ppc") == 0) |
| 86 | V = EmitPPCBuiltinExpr(BuiltinID, E); |
| 87 | |
| 88 | if (V) |
| 89 | return RValue::get(V); |
| 90 | |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 91 | WarnUnsupported(E, "builtin function"); |
Chris Lattner | 6de93ff | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 92 | |
| 93 | // Unknown builtin, for now just dump it out and return undef. |
| 94 | if (hasAggregateLLVMType(E->getType())) |
| 95 | return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType()))); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 96 | return RValue::get(UndefValue::get(ConvertType(E->getType()))); |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | 6de93ff | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 98 | case Builtin::BI__builtin___CFStringMakeConstantString: { |
| 99 | const Expr *Arg = E->getArg(0); |
| 100 | |
Anders Carlsson | d6a275f | 2007-11-01 00:39:26 +0000 | [diff] [blame] | 101 | while (1) { |
| 102 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Arg)) |
| 103 | Arg = PE->getSubExpr(); |
| 104 | else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 105 | Arg = CE->getSubExpr(); |
| 106 | else |
| 107 | break; |
| 108 | } |
Chris Lattner | 6de93ff | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 109 | |
| 110 | const StringLiteral *Literal = cast<StringLiteral>(Arg); |
| 111 | std::string S(Literal->getStrData(), Literal->getByteLength()); |
| 112 | |
| 113 | return RValue::get(CGM.GetAddrOfConstantCFString(S)); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 114 | } |
| 115 | case Builtin::BI__builtin_va_start: |
| 116 | case Builtin::BI__builtin_va_end: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 117 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 118 | const llvm::Type *DestType = |
| 119 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 120 | if (ArgValue->getType() != DestType) |
| 121 | ArgValue = Builder.CreateBitCast(ArgValue, DestType, |
| 122 | ArgValue->getNameStart()); |
| 123 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 124 | Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_start) ? |
| 125 | Intrinsic::vastart : Intrinsic::vaend; |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 126 | return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 127 | } |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin_classify_type: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 129 | APSInt Result(32); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 130 | if (!E->isBuiltinClassifyType(Result)) |
| 131 | assert(0 && "Expr not __builtin_classify_type!"); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 132 | return RValue::get(ConstantInt::get(Result)); |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 133 | } |
Anders Carlsson | d6a275f | 2007-11-01 00:39:26 +0000 | [diff] [blame] | 134 | case Builtin::BI__builtin_constant_p: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 135 | APSInt Result(32); |
Anders Carlsson | d6a275f | 2007-11-01 00:39:26 +0000 | [diff] [blame] | 136 | // FIXME: Analyze the parameter and check if it is a constant. |
| 137 | Result = 0; |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 138 | return RValue::get(ConstantInt::get(Result)); |
Anders Carlsson | d6a275f | 2007-11-01 00:39:26 +0000 | [diff] [blame] | 139 | } |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 140 | case Builtin::BI__builtin_abs: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 141 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 142 | |
| 143 | llvm::BinaryOperator *NegOp = |
| 144 | Builder.CreateNeg(ArgValue, (ArgValue->getName() + "neg").c_str()); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 145 | Value *CmpResult = |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 146 | Builder.CreateICmpSGE(ArgValue, NegOp->getOperand(0), "abscond"); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 147 | Value *Result = |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 148 | Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); |
| 149 | |
| 150 | return RValue::get(Result); |
| 151 | } |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 152 | case Builtin::BI__builtin_expect: |
| 153 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 154 | case Builtin::BI__builtin_bswap32: |
| 155 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 156 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 157 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 158 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 159 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 160 | } |
Anders Carlsson | c8ee798 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 161 | case Builtin::BI__builtin_inff: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 162 | APFloat f(APFloat::IEEEsingle, APFloat::fcInfinity, false); |
| 163 | return RValue::get(ConstantFP::get(llvm::Type::FloatTy, f)); |
Anders Carlsson | c8ee798 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 164 | } |
Anders Carlsson | 478547c | 2008-02-01 16:06:34 +0000 | [diff] [blame] | 165 | case Builtin::BI__builtin_huge_val: |
Anders Carlsson | c8ee798 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 166 | case Builtin::BI__builtin_inf: |
| 167 | // FIXME: mapping long double onto double. |
| 168 | case Builtin::BI__builtin_infl: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 169 | APFloat f(APFloat::IEEEdouble, APFloat::fcInfinity, false); |
| 170 | return RValue::get(ConstantFP::get(llvm::Type::DoubleTy, f)); |
Anders Carlsson | c8ee798 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 171 | } |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 172 | case Builtin::BI__builtin_isgreater: |
| 173 | case Builtin::BI__builtin_isgreaterequal: |
| 174 | case Builtin::BI__builtin_isless: |
| 175 | case Builtin::BI__builtin_islessequal: |
| 176 | case Builtin::BI__builtin_islessgreater: |
| 177 | case Builtin::BI__builtin_isunordered: { |
| 178 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 179 | // floating point values. |
| 180 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
| 181 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
| 182 | |
| 183 | switch (BuiltinID) { |
| 184 | default: assert(0 && "Unknown ordered comparison"); |
| 185 | case Builtin::BI__builtin_isgreater: |
| 186 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 187 | break; |
| 188 | case Builtin::BI__builtin_isgreaterequal: |
| 189 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 190 | break; |
| 191 | case Builtin::BI__builtin_isless: |
| 192 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 193 | break; |
| 194 | case Builtin::BI__builtin_islessequal: |
| 195 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 196 | break; |
| 197 | case Builtin::BI__builtin_islessgreater: |
| 198 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 199 | break; |
| 200 | case Builtin::BI__builtin_isunordered: |
| 201 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 202 | break; |
| 203 | } |
| 204 | // ZExt bool to int type. |
| 205 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 206 | "tmp")); |
| 207 | } |
Nate Begeman | 4efe459 | 2008-02-06 00:02:50 +0000 | [diff] [blame] | 208 | case Builtin::BI__builtin_alloca: |
| 209 | return RValue::get(Builder.CreateAlloca(llvm::Type::Int8Ty, |
| 210 | EmitScalarExpr(E->getArg(0)), |
| 211 | "tmp")); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 212 | } |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 213 | return RValue::get(0); |
| 214 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 216 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
| 217 | const CallExpr *E) { |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 218 | |
| 219 | llvm::SmallVector<Value*, 4> Ops; |
| 220 | |
| 221 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 222 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 223 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 224 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 225 | default: return 0; |
| 226 | case X86::BI__builtin_ia32_mulps: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 227 | return Builder.CreateMul(Ops[0], Ops[1], "mulps"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 228 | case X86::BI__builtin_ia32_pand: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 229 | return Builder.CreateAnd(Ops[0], Ops[1], "pand"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 230 | case X86::BI__builtin_ia32_por: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 231 | return Builder.CreateAnd(Ops[0], Ops[1], "por"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 232 | case X86::BI__builtin_ia32_pxor: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 233 | return Builder.CreateAnd(Ops[0], Ops[1], "pxor"); |
Chris Lattner | 8e7b692 | 2007-12-11 04:33:00 +0000 | [diff] [blame] | 234 | case X86::BI__builtin_ia32_pandn: { |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 235 | Ops[0] = Builder.CreateNot(Ops[0], "tmp"); |
| 236 | return Builder.CreateAnd(Ops[0], Ops[1], "pandn"); |
Chris Lattner | 8e7b692 | 2007-12-11 04:33:00 +0000 | [diff] [blame] | 237 | } |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 238 | case X86::BI__builtin_ia32_paddb: |
| 239 | case X86::BI__builtin_ia32_paddd: |
| 240 | case X86::BI__builtin_ia32_paddq: |
| 241 | case X86::BI__builtin_ia32_paddw: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 242 | case X86::BI__builtin_ia32_addps: |
| 243 | return Builder.CreateAdd(Ops[0], Ops[1], "add"); |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 244 | case X86::BI__builtin_ia32_psubb: |
| 245 | case X86::BI__builtin_ia32_psubd: |
| 246 | case X86::BI__builtin_ia32_psubq: |
| 247 | case X86::BI__builtin_ia32_psubw: |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 248 | case X86::BI__builtin_ia32_subps: |
| 249 | return Builder.CreateSub(Ops[0], Ops[1], "sub"); |
| 250 | case X86::BI__builtin_ia32_divps: |
| 251 | return Builder.CreateFDiv(Ops[0], Ops[1], "divps"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 252 | case X86::BI__builtin_ia32_pmullw: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 253 | return Builder.CreateMul(Ops[0], Ops[1], "pmul"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 254 | case X86::BI__builtin_ia32_punpckhbw: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 255 | 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] | 256 | "punpckhbw"); |
| 257 | case X86::BI__builtin_ia32_punpckhwd: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 258 | return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhwd"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 259 | case X86::BI__builtin_ia32_punpckhdq: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 260 | return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhdq"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 261 | case X86::BI__builtin_ia32_punpcklbw: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 262 | 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] | 263 | "punpcklbw"); |
| 264 | case X86::BI__builtin_ia32_punpcklwd: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 265 | return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpcklwd"); |
Anders Carlsson | db83230 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 266 | case X86::BI__builtin_ia32_punpckldq: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 267 | return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpckldq"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 268 | case X86::BI__builtin_ia32_pslldi: |
| 269 | case X86::BI__builtin_ia32_psllqi: |
| 270 | case X86::BI__builtin_ia32_psllwi: |
| 271 | case X86::BI__builtin_ia32_psradi: |
| 272 | case X86::BI__builtin_ia32_psrawi: |
| 273 | case X86::BI__builtin_ia32_psrldi: |
| 274 | case X86::BI__builtin_ia32_psrlqi: |
| 275 | case X86::BI__builtin_ia32_psrlwi: { |
| 276 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); |
| 277 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1); |
| 278 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 279 | const char *name = 0; |
| 280 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 281 | |
| 282 | switch (BuiltinID) { |
| 283 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 284 | case X86::BI__builtin_ia32_pslldi: |
| 285 | name = "pslldi"; |
| 286 | ID = Intrinsic::x86_mmx_psll_d; |
| 287 | break; |
| 288 | case X86::BI__builtin_ia32_psllqi: |
| 289 | name = "psllqi"; |
| 290 | ID = Intrinsic::x86_mmx_psll_q; |
| 291 | break; |
| 292 | case X86::BI__builtin_ia32_psllwi: |
| 293 | name = "psllwi"; |
| 294 | ID = Intrinsic::x86_mmx_psll_w; |
| 295 | break; |
| 296 | case X86::BI__builtin_ia32_psradi: |
| 297 | name = "psradi"; |
| 298 | ID = Intrinsic::x86_mmx_psra_d; |
| 299 | break; |
| 300 | case X86::BI__builtin_ia32_psrawi: |
| 301 | name = "psrawi"; |
| 302 | ID = Intrinsic::x86_mmx_psra_w; |
| 303 | break; |
| 304 | case X86::BI__builtin_ia32_psrldi: |
| 305 | name = "psrldi"; |
| 306 | ID = Intrinsic::x86_mmx_psrl_d; |
| 307 | break; |
| 308 | case X86::BI__builtin_ia32_psrlqi: |
| 309 | name = "psrlqi"; |
| 310 | ID = Intrinsic::x86_mmx_psrl_q; |
| 311 | break; |
| 312 | case X86::BI__builtin_ia32_psrlwi: |
| 313 | name = "psrlwi"; |
| 314 | ID = Intrinsic::x86_mmx_psrl_w; |
| 315 | break; |
| 316 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 317 | llvm::Function *F = CGM.getIntrinsic(ID); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 318 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 319 | } |
| 320 | case X86::BI__builtin_ia32_pshufd: { |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 321 | unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 322 | return EmitShuffleVector(Ops[0], Ops[0], |
Anders Carlsson | 4e4ee21 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 323 | i & 0x3, (i & 0xc) >> 2, |
| 324 | (i & 0x30) >> 4, (i & 0xc0) >> 6, |
| 325 | "pshufd"); |
| 326 | } |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 327 | case X86::BI__builtin_ia32_vec_init_v4hi: |
| 328 | case X86::BI__builtin_ia32_vec_init_v8qi: |
| 329 | case X86::BI__builtin_ia32_vec_init_v2si: |
| 330 | return EmitVector(&Ops[0], Ops.size()); |
| 331 | case X86::BI__builtin_ia32_vec_ext_v2si: |
| 332 | return Builder.CreateExtractElement(Ops[0], Ops[1], "result"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 333 | case X86::BI__builtin_ia32_cmpordss: |
| 334 | case X86::BI__builtin_ia32_cmpunordss: |
| 335 | case X86::BI__builtin_ia32_cmpeqss: |
| 336 | case X86::BI__builtin_ia32_cmpltss: |
| 337 | case X86::BI__builtin_ia32_cmpless: |
| 338 | case X86::BI__builtin_ia32_cmpneqss: |
| 339 | case X86::BI__builtin_ia32_cmpnltss: |
| 340 | case X86::BI__builtin_ia32_cmpnless: { |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 341 | unsigned i = 0; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 342 | const char *name = 0; |
| 343 | switch (BuiltinID) { |
| 344 | default: assert(0 && "Unknown compare builtin!"); |
| 345 | case X86::BI__builtin_ia32_cmpeqss: |
| 346 | i = 0; |
| 347 | name = "cmpeqss"; |
| 348 | break; |
| 349 | case X86::BI__builtin_ia32_cmpltss: |
| 350 | i = 1; |
| 351 | name = "cmpltss"; |
| 352 | break; |
| 353 | case X86::BI__builtin_ia32_cmpless: |
| 354 | i = 2; |
| 355 | name = "cmpless"; |
| 356 | break; |
| 357 | case X86::BI__builtin_ia32_cmpunordss: |
| 358 | i = 3; |
| 359 | name = "cmpunordss"; |
| 360 | break; |
| 361 | case X86::BI__builtin_ia32_cmpneqss: |
| 362 | i = 4; |
| 363 | name = "cmpneqss"; |
| 364 | break; |
| 365 | case X86::BI__builtin_ia32_cmpnltss: |
| 366 | i = 5; |
| 367 | name = "cmpntlss"; |
| 368 | break; |
| 369 | case X86::BI__builtin_ia32_cmpnless: |
| 370 | i = 6; |
| 371 | name = "cmpnless"; |
| 372 | break; |
| 373 | case X86::BI__builtin_ia32_cmpordss: |
| 374 | i = 7; |
| 375 | name = "cmpordss"; |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i)); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 380 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 381 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 382 | } |
| 383 | case X86::BI__builtin_ia32_cmpordps: |
| 384 | case X86::BI__builtin_ia32_cmpunordps: |
| 385 | case X86::BI__builtin_ia32_cmpeqps: |
| 386 | case X86::BI__builtin_ia32_cmpltps: |
| 387 | case X86::BI__builtin_ia32_cmpleps: |
| 388 | case X86::BI__builtin_ia32_cmpneqps: |
| 389 | case X86::BI__builtin_ia32_cmpngtps: |
| 390 | case X86::BI__builtin_ia32_cmpnltps: |
| 391 | case X86::BI__builtin_ia32_cmpgtps: |
| 392 | case X86::BI__builtin_ia32_cmpgeps: |
| 393 | case X86::BI__builtin_ia32_cmpngeps: |
| 394 | case X86::BI__builtin_ia32_cmpnleps: { |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 395 | unsigned i = 0; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 396 | const char *name = 0; |
| 397 | bool ShouldSwap = false; |
| 398 | switch (BuiltinID) { |
| 399 | default: assert(0 && "Unknown compare builtin!"); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 400 | case X86::BI__builtin_ia32_cmpeqps: i = 0; name = "cmpeqps"; break; |
| 401 | case X86::BI__builtin_ia32_cmpltps: i = 1; name = "cmpltps"; break; |
| 402 | case X86::BI__builtin_ia32_cmpleps: i = 2; name = "cmpleps"; break; |
| 403 | case X86::BI__builtin_ia32_cmpunordps: i = 3; name = "cmpunordps"; break; |
| 404 | case X86::BI__builtin_ia32_cmpneqps: i = 4; name = "cmpneqps"; break; |
| 405 | case X86::BI__builtin_ia32_cmpnltps: i = 5; name = "cmpntlps"; break; |
| 406 | case X86::BI__builtin_ia32_cmpnleps: i = 6; name = "cmpnleps"; break; |
| 407 | case X86::BI__builtin_ia32_cmpordps: i = 7; name = "cmpordps"; break; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 408 | case X86::BI__builtin_ia32_cmpgtps: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 409 | ShouldSwap = true; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 410 | i = 1; |
| 411 | name = "cmpgtps"; |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 412 | break; |
| 413 | case X86::BI__builtin_ia32_cmpgeps: |
| 414 | i = 2; |
| 415 | name = "cmpgeps"; |
| 416 | ShouldSwap = true; |
| 417 | break; |
| 418 | case X86::BI__builtin_ia32_cmpngtps: |
| 419 | i = 5; |
| 420 | name = "cmpngtps"; |
| 421 | ShouldSwap = true; |
| 422 | break; |
| 423 | case X86::BI__builtin_ia32_cmpngeps: |
| 424 | i = 6; |
| 425 | name = "cmpngeps"; |
| 426 | ShouldSwap = true; |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | if (ShouldSwap) |
| 431 | std::swap(Ops[0], Ops[1]); |
| 432 | |
| 433 | Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i)); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 434 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 435 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 436 | } |
| 437 | case X86::BI__builtin_ia32_movss: |
| 438 | return EmitShuffleVector(Ops[0], Ops[1], 4, 1, 2, 3, "movss"); |
| 439 | case X86::BI__builtin_ia32_shufps: |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 440 | unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue(); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 441 | return EmitShuffleVector(Ops[0], Ops[1], |
| 442 | i & 0x3, (i & 0xc) >> 2, |
| 443 | ((i & 0x30) >> 4) + 4, |
| 444 | ((i & 0x60) >> 6) + 4, "shufps"); |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 448 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
| 449 | const CallExpr *E) { |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 450 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 451 | default: return 0; |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 452 | } |
| 453 | } |