Anders Carlsson | 4986530 | 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 | 959e5be | 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 | 4986530 | 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 | 6f6c648 | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 4986530 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 18 | #include "clang/AST/Builtins.h" |
| 19 | #include "clang/AST/Expr.h" |
Anders Carlsson | e1449c1 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 20 | #include "clang/AST/TargetBuiltins.h" |
Chris Lattner | 783a78f | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 22 | #include "llvm/Function.h" |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Anders Carlsson | 4986530 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
Anders Carlsson | 6f6c648 | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | 783a78f | 2007-08-26 04:17:05 +0000 | [diff] [blame] | 28 | RValue CodeGenFunction::EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E) { |
| 29 | switch (BuiltinID) { |
Anders Carlsson | 6f6c648 | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 30 | default: { |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 31 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID)) |
Nate Begeman | 33a5043 | 2008-01-17 18:06:09 +0000 | [diff] [blame] | 32 | return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID), |
Eli Friedman | 261f4ad | 2008-01-30 01:32:06 +0000 | [diff] [blame^] | 33 | E->getCallee()->getType(), E->arg_begin(), |
| 34 | E->getNumArgs()); |
Anders Carlsson | 6f6c648 | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 35 | |
| 36 | // See if we have a target specific intrinsic. |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 37 | Intrinsic::ID IntrinsicID; |
Anders Carlsson | 6f6c648 | 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 | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 45 | SmallVector<Value*, 16> Args; |
Anders Carlsson | 6f6c648 | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 47 | Function *F = CGM.getIntrinsic(IntrinsicID); |
Anders Carlsson | 6f6c648 | 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 | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 51 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Anders Carlsson | 6f6c648 | 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 | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 65 | Value *V = Builder.CreateCall(F, &Args[0], &Args[0] + Args.size()); |
Anders Carlsson | 6f6c648 | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 66 | QualType BuiltinRetType = E->getType(); |
| 67 | |
Chris Lattner | 9d37e13 | 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 | 6f6c648 | 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 | 6f6c648 | 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 | e1449c1 | 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 | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 81 | Value *V = 0; |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 82 | |
Anders Carlsson | e1449c1 | 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 | e8f4963 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 91 | WarnUnsupported(E, "builtin function"); |
Chris Lattner | 783a78f | 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 | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 96 | return RValue::get(UndefValue::get(ConvertType(E->getType()))); |
Anders Carlsson | 6f6c648 | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | 783a78f | 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 | 783e143 | 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 | 783a78f | 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 | cebb8d6 | 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 | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 117 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 118 | const llvm::Type *DestType = |
| 119 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Anders Carlsson | cebb8d6 | 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 | 9d37e13 | 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 | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 126 | return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 127 | } |
Anders Carlsson | 4043840 | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin_classify_type: { |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 129 | APSInt Result(32); |
Anders Carlsson | 4043840 | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 130 | if (!E->isBuiltinClassifyType(Result)) |
| 131 | assert(0 && "Expr not __builtin_classify_type!"); |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 132 | return RValue::get(ConstantInt::get(Result)); |
Anders Carlsson | 4986530 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 133 | } |
Anders Carlsson | 783e143 | 2007-11-01 00:39:26 +0000 | [diff] [blame] | 134 | case Builtin::BI__builtin_constant_p: { |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 135 | APSInt Result(32); |
Anders Carlsson | 783e143 | 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 | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 138 | return RValue::get(ConstantInt::get(Result)); |
Anders Carlsson | 783e143 | 2007-11-01 00:39:26 +0000 | [diff] [blame] | 139 | } |
Anders Carlsson | e79dbe2 | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 140 | case Builtin::BI__builtin_abs: { |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 141 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | e79dbe2 | 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 | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 145 | Value *CmpResult = |
Anders Carlsson | e79dbe2 | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 146 | Builder.CreateICmpSGE(ArgValue, NegOp->getOperand(0), "abscond"); |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 147 | Value *Result = |
Anders Carlsson | e79dbe2 | 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 | 9d37e13 | 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 | 5611800 | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 154 | case Builtin::BI__builtin_bswap32: |
| 155 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 156 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | 5611800 | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 157 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 158 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 159 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Anders Carlsson | 5611800 | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 160 | } |
Anders Carlsson | a8897f0 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 161 | case Builtin::BI__builtin_inff: { |
Chris Lattner | 9d37e13 | 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 | a8897f0 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 164 | } |
| 165 | case Builtin::BI__builtin_inf: |
| 166 | // FIXME: mapping long double onto double. |
| 167 | case Builtin::BI__builtin_infl: { |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 168 | APFloat f(APFloat::IEEEdouble, APFloat::fcInfinity, false); |
| 169 | return RValue::get(ConstantFP::get(llvm::Type::DoubleTy, f)); |
Anders Carlsson | a8897f0 | 2007-12-05 07:22:48 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | d1f26b3 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 171 | case Builtin::BI__builtin_isgreater: |
| 172 | case Builtin::BI__builtin_isgreaterequal: |
| 173 | case Builtin::BI__builtin_isless: |
| 174 | case Builtin::BI__builtin_islessequal: |
| 175 | case Builtin::BI__builtin_islessgreater: |
| 176 | case Builtin::BI__builtin_isunordered: { |
| 177 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 178 | // floating point values. |
| 179 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
| 180 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
| 181 | |
| 182 | switch (BuiltinID) { |
| 183 | default: assert(0 && "Unknown ordered comparison"); |
| 184 | case Builtin::BI__builtin_isgreater: |
| 185 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 186 | break; |
| 187 | case Builtin::BI__builtin_isgreaterequal: |
| 188 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 189 | break; |
| 190 | case Builtin::BI__builtin_isless: |
| 191 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 192 | break; |
| 193 | case Builtin::BI__builtin_islessequal: |
| 194 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 195 | break; |
| 196 | case Builtin::BI__builtin_islessgreater: |
| 197 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 198 | break; |
| 199 | case Builtin::BI__builtin_isunordered: |
| 200 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 201 | break; |
| 202 | } |
| 203 | // ZExt bool to int type. |
| 204 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 205 | "tmp")); |
| 206 | } |
Anders Carlsson | 4043840 | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 207 | } |
Anders Carlsson | 4986530 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 208 | return RValue::get(0); |
| 209 | } |
Anders Carlsson | e1449c1 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 211 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
| 212 | const CallExpr *E) { |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 213 | |
| 214 | llvm::SmallVector<Value*, 4> Ops; |
| 215 | |
| 216 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 217 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 218 | |
Anders Carlsson | e1449c1 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 219 | switch (BuiltinID) { |
Anders Carlsson | 0a35a81 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 220 | default: return 0; |
| 221 | case X86::BI__builtin_ia32_mulps: |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 222 | return Builder.CreateMul(Ops[0], Ops[1], "mulps"); |
Anders Carlsson | 17b80c1 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 223 | case X86::BI__builtin_ia32_pand: |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 224 | return Builder.CreateAnd(Ops[0], Ops[1], "pand"); |
Anders Carlsson | 17b80c1 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 225 | case X86::BI__builtin_ia32_por: |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 226 | return Builder.CreateAnd(Ops[0], Ops[1], "por"); |
Anders Carlsson | 17b80c1 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 227 | case X86::BI__builtin_ia32_pxor: |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 228 | return Builder.CreateAnd(Ops[0], Ops[1], "pxor"); |
Chris Lattner | 7037629 | 2007-12-11 04:33:00 +0000 | [diff] [blame] | 229 | case X86::BI__builtin_ia32_pandn: { |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 230 | Ops[0] = Builder.CreateNot(Ops[0], "tmp"); |
| 231 | return Builder.CreateAnd(Ops[0], Ops[1], "pandn"); |
Chris Lattner | 7037629 | 2007-12-11 04:33:00 +0000 | [diff] [blame] | 232 | } |
Anders Carlsson | 17b80c1 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 233 | case X86::BI__builtin_ia32_paddb: |
| 234 | case X86::BI__builtin_ia32_paddd: |
| 235 | case X86::BI__builtin_ia32_paddq: |
| 236 | case X86::BI__builtin_ia32_paddw: |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 237 | case X86::BI__builtin_ia32_addps: |
| 238 | return Builder.CreateAdd(Ops[0], Ops[1], "add"); |
Anders Carlsson | 17b80c1 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 239 | case X86::BI__builtin_ia32_psubb: |
| 240 | case X86::BI__builtin_ia32_psubd: |
| 241 | case X86::BI__builtin_ia32_psubq: |
| 242 | case X86::BI__builtin_ia32_psubw: |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 243 | case X86::BI__builtin_ia32_subps: |
| 244 | return Builder.CreateSub(Ops[0], Ops[1], "sub"); |
| 245 | case X86::BI__builtin_ia32_divps: |
| 246 | return Builder.CreateFDiv(Ops[0], Ops[1], "divps"); |
Anders Carlsson | 820cb74 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 247 | case X86::BI__builtin_ia32_pmullw: |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 248 | return Builder.CreateMul(Ops[0], Ops[1], "pmul"); |
Anders Carlsson | 820cb74 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 249 | case X86::BI__builtin_ia32_punpckhbw: |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 250 | return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15, |
Anders Carlsson | 820cb74 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 251 | "punpckhbw"); |
| 252 | case X86::BI__builtin_ia32_punpckhwd: |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 253 | return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhwd"); |
Anders Carlsson | 820cb74 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 254 | case X86::BI__builtin_ia32_punpckhdq: |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 255 | return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhdq"); |
Anders Carlsson | 820cb74 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 256 | case X86::BI__builtin_ia32_punpcklbw: |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 257 | return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11, |
Anders Carlsson | 820cb74 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 258 | "punpcklbw"); |
| 259 | case X86::BI__builtin_ia32_punpcklwd: |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 260 | return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpcklwd"); |
Anders Carlsson | 820cb74 | 2007-12-11 03:09:22 +0000 | [diff] [blame] | 261 | case X86::BI__builtin_ia32_punpckldq: |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 262 | return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpckldq"); |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 263 | case X86::BI__builtin_ia32_pslldi: |
| 264 | case X86::BI__builtin_ia32_psllqi: |
| 265 | case X86::BI__builtin_ia32_psllwi: |
| 266 | case X86::BI__builtin_ia32_psradi: |
| 267 | case X86::BI__builtin_ia32_psrawi: |
| 268 | case X86::BI__builtin_ia32_psrldi: |
| 269 | case X86::BI__builtin_ia32_psrlqi: |
| 270 | case X86::BI__builtin_ia32_psrlwi: { |
| 271 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); |
| 272 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1); |
| 273 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 274 | const char *name = 0; |
| 275 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 276 | |
| 277 | switch (BuiltinID) { |
| 278 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 279 | case X86::BI__builtin_ia32_pslldi: |
| 280 | name = "pslldi"; |
| 281 | ID = Intrinsic::x86_mmx_psll_d; |
| 282 | break; |
| 283 | case X86::BI__builtin_ia32_psllqi: |
| 284 | name = "psllqi"; |
| 285 | ID = Intrinsic::x86_mmx_psll_q; |
| 286 | break; |
| 287 | case X86::BI__builtin_ia32_psllwi: |
| 288 | name = "psllwi"; |
| 289 | ID = Intrinsic::x86_mmx_psll_w; |
| 290 | break; |
| 291 | case X86::BI__builtin_ia32_psradi: |
| 292 | name = "psradi"; |
| 293 | ID = Intrinsic::x86_mmx_psra_d; |
| 294 | break; |
| 295 | case X86::BI__builtin_ia32_psrawi: |
| 296 | name = "psrawi"; |
| 297 | ID = Intrinsic::x86_mmx_psra_w; |
| 298 | break; |
| 299 | case X86::BI__builtin_ia32_psrldi: |
| 300 | name = "psrldi"; |
| 301 | ID = Intrinsic::x86_mmx_psrl_d; |
| 302 | break; |
| 303 | case X86::BI__builtin_ia32_psrlqi: |
| 304 | name = "psrlqi"; |
| 305 | ID = Intrinsic::x86_mmx_psrl_q; |
| 306 | break; |
| 307 | case X86::BI__builtin_ia32_psrlwi: |
| 308 | name = "psrlwi"; |
| 309 | ID = Intrinsic::x86_mmx_psrl_w; |
| 310 | break; |
| 311 | } |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 312 | llvm::Function *F = CGM.getIntrinsic(ID); |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 313 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 314 | } |
| 315 | case X86::BI__builtin_ia32_pshufd: { |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 316 | unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); |
Anders Carlsson | 04c4b75 | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 317 | return EmitShuffleVector(Ops[0], Ops[0], |
Anders Carlsson | 17b80c1 | 2007-12-11 02:25:54 +0000 | [diff] [blame] | 318 | i & 0x3, (i & 0xc) >> 2, |
| 319 | (i & 0x30) >> 4, (i & 0xc0) >> 6, |
| 320 | "pshufd"); |
| 321 | } |
Anders Carlsson | 68b8be9 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 322 | case X86::BI__builtin_ia32_vec_init_v4hi: |
| 323 | case X86::BI__builtin_ia32_vec_init_v8qi: |
| 324 | case X86::BI__builtin_ia32_vec_init_v2si: |
| 325 | return EmitVector(&Ops[0], Ops.size()); |
| 326 | case X86::BI__builtin_ia32_vec_ext_v2si: |
| 327 | return Builder.CreateExtractElement(Ops[0], Ops[1], "result"); |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 328 | case X86::BI__builtin_ia32_cmpordss: |
| 329 | case X86::BI__builtin_ia32_cmpunordss: |
| 330 | case X86::BI__builtin_ia32_cmpeqss: |
| 331 | case X86::BI__builtin_ia32_cmpltss: |
| 332 | case X86::BI__builtin_ia32_cmpless: |
| 333 | case X86::BI__builtin_ia32_cmpneqss: |
| 334 | case X86::BI__builtin_ia32_cmpnltss: |
| 335 | case X86::BI__builtin_ia32_cmpnless: { |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 336 | unsigned i = 0; |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 337 | const char *name = 0; |
| 338 | switch (BuiltinID) { |
| 339 | default: assert(0 && "Unknown compare builtin!"); |
| 340 | case X86::BI__builtin_ia32_cmpeqss: |
| 341 | i = 0; |
| 342 | name = "cmpeqss"; |
| 343 | break; |
| 344 | case X86::BI__builtin_ia32_cmpltss: |
| 345 | i = 1; |
| 346 | name = "cmpltss"; |
| 347 | break; |
| 348 | case X86::BI__builtin_ia32_cmpless: |
| 349 | i = 2; |
| 350 | name = "cmpless"; |
| 351 | break; |
| 352 | case X86::BI__builtin_ia32_cmpunordss: |
| 353 | i = 3; |
| 354 | name = "cmpunordss"; |
| 355 | break; |
| 356 | case X86::BI__builtin_ia32_cmpneqss: |
| 357 | i = 4; |
| 358 | name = "cmpneqss"; |
| 359 | break; |
| 360 | case X86::BI__builtin_ia32_cmpnltss: |
| 361 | i = 5; |
| 362 | name = "cmpntlss"; |
| 363 | break; |
| 364 | case X86::BI__builtin_ia32_cmpnless: |
| 365 | i = 6; |
| 366 | name = "cmpnless"; |
| 367 | break; |
| 368 | case X86::BI__builtin_ia32_cmpordss: |
| 369 | i = 7; |
| 370 | name = "cmpordss"; |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i)); |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 375 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 376 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 377 | } |
| 378 | case X86::BI__builtin_ia32_cmpordps: |
| 379 | case X86::BI__builtin_ia32_cmpunordps: |
| 380 | case X86::BI__builtin_ia32_cmpeqps: |
| 381 | case X86::BI__builtin_ia32_cmpltps: |
| 382 | case X86::BI__builtin_ia32_cmpleps: |
| 383 | case X86::BI__builtin_ia32_cmpneqps: |
| 384 | case X86::BI__builtin_ia32_cmpngtps: |
| 385 | case X86::BI__builtin_ia32_cmpnltps: |
| 386 | case X86::BI__builtin_ia32_cmpgtps: |
| 387 | case X86::BI__builtin_ia32_cmpgeps: |
| 388 | case X86::BI__builtin_ia32_cmpngeps: |
| 389 | case X86::BI__builtin_ia32_cmpnleps: { |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 390 | unsigned i = 0; |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 391 | const char *name = 0; |
| 392 | bool ShouldSwap = false; |
| 393 | switch (BuiltinID) { |
| 394 | default: assert(0 && "Unknown compare builtin!"); |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 395 | case X86::BI__builtin_ia32_cmpeqps: i = 0; name = "cmpeqps"; break; |
| 396 | case X86::BI__builtin_ia32_cmpltps: i = 1; name = "cmpltps"; break; |
| 397 | case X86::BI__builtin_ia32_cmpleps: i = 2; name = "cmpleps"; break; |
| 398 | case X86::BI__builtin_ia32_cmpunordps: i = 3; name = "cmpunordps"; break; |
| 399 | case X86::BI__builtin_ia32_cmpneqps: i = 4; name = "cmpneqps"; break; |
| 400 | case X86::BI__builtin_ia32_cmpnltps: i = 5; name = "cmpntlps"; break; |
| 401 | case X86::BI__builtin_ia32_cmpnleps: i = 6; name = "cmpnleps"; break; |
| 402 | case X86::BI__builtin_ia32_cmpordps: i = 7; name = "cmpordps"; break; |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 403 | case X86::BI__builtin_ia32_cmpgtps: |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 404 | ShouldSwap = true; |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 405 | i = 1; |
| 406 | name = "cmpgtps"; |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 407 | break; |
| 408 | case X86::BI__builtin_ia32_cmpgeps: |
| 409 | i = 2; |
| 410 | name = "cmpgeps"; |
| 411 | ShouldSwap = true; |
| 412 | break; |
| 413 | case X86::BI__builtin_ia32_cmpngtps: |
| 414 | i = 5; |
| 415 | name = "cmpngtps"; |
| 416 | ShouldSwap = true; |
| 417 | break; |
| 418 | case X86::BI__builtin_ia32_cmpngeps: |
| 419 | i = 6; |
| 420 | name = "cmpngeps"; |
| 421 | ShouldSwap = true; |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | if (ShouldSwap) |
| 426 | std::swap(Ops[0], Ops[1]); |
| 427 | |
| 428 | Ops.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, i)); |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 429 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 430 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
| 431 | } |
| 432 | case X86::BI__builtin_ia32_movss: |
| 433 | return EmitShuffleVector(Ops[0], Ops[1], 4, 1, 2, 3, "movss"); |
| 434 | case X86::BI__builtin_ia32_shufps: |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 435 | unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue(); |
Anders Carlsson | 1ad775f | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 436 | return EmitShuffleVector(Ops[0], Ops[1], |
| 437 | i & 0x3, (i & 0xc) >> 2, |
| 438 | ((i & 0x30) >> 4) + 4, |
| 439 | ((i & 0x60) >> 6) + 4, "shufps"); |
Anders Carlsson | e1449c1 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
Chris Lattner | 9d37e13 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 443 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
| 444 | const CallExpr *E) { |
Anders Carlsson | e1449c1 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 445 | switch (BuiltinID) { |
Anders Carlsson | 0a35a81 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 446 | default: return 0; |
Anders Carlsson | e1449c1 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 447 | } |
| 448 | } |