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 | |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 14 | #include "TargetInfo.h" |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
| 16 | #include "CodeGenModule.h" |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 17 | #include "CGObjCRuntime.h" |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 1f32999 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 19 | #include "clang/AST/APValue.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Chris Lattner | 6b15cdc | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetBuiltins.h" |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 29 | static void EmitMemoryBarrier(CodeGenFunction &CGF, |
| 30 | bool LoadLoad, bool LoadStore, |
| 31 | bool StoreLoad, bool StoreStore, |
| 32 | bool Device) { |
| 33 | Value *True = llvm::ConstantInt::getTrue(CGF.getLLVMContext()); |
| 34 | Value *False = llvm::ConstantInt::getFalse(CGF.getLLVMContext()); |
| 35 | Value *C[5] = { LoadLoad ? True : False, |
| 36 | LoadStore ? True : False, |
| 37 | StoreLoad ? True : False, |
| 38 | StoreStore ? True : False, |
| 39 | Device ? True : False }; |
| 40 | CGF.Builder.CreateCall(CGF.CGM.getIntrinsic(Intrinsic::memory_barrier), |
| 41 | C, C + 5); |
| 42 | } |
| 43 | |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 44 | static Value *EmitCastToInt(CodeGenFunction &CGF, |
| 45 | const llvm::Type *ToType, Value *Val) { |
| 46 | if (Val->getType()->isPointerTy()) { |
| 47 | return CGF.Builder.CreatePtrToInt(Val, ToType); |
| 48 | } |
| 49 | assert(Val->getType()->isIntegerTy() && |
| 50 | "Used a non-integer and non-pointer type with atomic builtin"); |
| 51 | assert(Val->getType()->getScalarSizeInBits() <= |
| 52 | ToType->getScalarSizeInBits() && "Integer type too small"); |
| 53 | return CGF.Builder.CreateSExtOrBitCast(Val, ToType); |
| 54 | } |
| 55 | |
| 56 | static Value *EmitCastFromInt(CodeGenFunction &CGF, QualType ToQualType, |
| 57 | Value *Val) { |
| 58 | const llvm::Type *ToType = CGF.ConvertType(ToQualType); |
| 59 | if (ToType->isPointerTy()) { |
| 60 | return CGF.Builder.CreateIntToPtr(Val, ToType); |
| 61 | } |
| 62 | assert(Val->getType()->isIntegerTy() && |
| 63 | "Used a non-integer and non-pointer type with atomic builtin"); |
| 64 | assert(Val->getType()->getScalarSizeInBits() >= |
| 65 | ToType->getScalarSizeInBits() && "Integer type too small"); |
| 66 | return CGF.Builder.CreateTruncOrBitCast(Val, ToType); |
| 67 | } |
| 68 | |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 69 | // The atomic builtins are also full memory barriers. This is a utility for |
| 70 | // wrapping a call to the builtins with memory barriers. |
| 71 | static Value *EmitCallWithBarrier(CodeGenFunction &CGF, Value *Fn, |
| 72 | Value **ArgBegin, Value **ArgEnd) { |
| 73 | // FIXME: We need a target hook for whether this applies to device memory or |
| 74 | // not. |
| 75 | bool Device = true; |
| 76 | |
| 77 | // Create barriers both before and after the call. |
| 78 | EmitMemoryBarrier(CGF, true, true, true, true, Device); |
| 79 | Value *Result = CGF.Builder.CreateCall(Fn, ArgBegin, ArgEnd); |
| 80 | EmitMemoryBarrier(CGF, true, true, true, true, Device); |
| 81 | return Result; |
| 82 | } |
| 83 | |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 84 | /// Utility to insert an atomic instruction based on Instrinsic::ID |
| 85 | /// and the expression node. |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 86 | static RValue EmitBinaryAtomic(CodeGenFunction &CGF, |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 87 | Intrinsic::ID Id, const CallExpr *E) { |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 88 | const llvm::Type *ValueType = |
| 89 | llvm::IntegerType::get(CGF.getLLVMContext(), |
| 90 | CGF.getContext().getTypeSize(E->getType())); |
| 91 | const llvm::Type *PtrType = ValueType->getPointerTo(); |
| 92 | const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType }; |
| 93 | Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2); |
| 94 | |
| 95 | Value *Args[2] = { CGF.Builder.CreateBitCast(CGF.EmitScalarExpr(E->getArg(0)), |
| 96 | PtrType), |
| 97 | EmitCastToInt(CGF, ValueType, |
| 98 | CGF.EmitScalarExpr(E->getArg(1))) }; |
| 99 | return RValue::get(EmitCastFromInt(CGF, E->getType(), |
| 100 | EmitCallWithBarrier(CGF, AtomF, Args, |
| 101 | Args + 2))); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /// Utility to insert an atomic instruction based Instrinsic::ID and |
| 105 | // the expression node, where the return value is the result of the |
| 106 | // operation. |
Chris Lattner | 420b118 | 2010-05-06 05:35:16 +0000 | [diff] [blame] | 107 | static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 108 | Intrinsic::ID Id, const CallExpr *E, |
| 109 | Instruction::BinaryOps Op) { |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 110 | const llvm::Type *ValueType = |
| 111 | llvm::IntegerType::get(CGF.getLLVMContext(), |
| 112 | CGF.getContext().getTypeSize(E->getType())); |
| 113 | const llvm::Type *PtrType = ValueType->getPointerTo(); |
| 114 | const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType }; |
| 115 | Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2); |
| 116 | |
| 117 | Value *Args[2] = { CGF.Builder.CreateBitCast(CGF.EmitScalarExpr(E->getArg(0)), |
| 118 | PtrType), |
| 119 | EmitCastToInt(CGF, ValueType, |
| 120 | CGF.EmitScalarExpr(E->getArg(1))) }; |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 121 | Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 2); |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 122 | return RValue::get(EmitCastFromInt(CGF, E->getType(), |
| 123 | CGF.Builder.CreateBinOp(Op, Result, |
| 124 | Args[1]))); |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Chris Lattner | 420b118 | 2010-05-06 05:35:16 +0000 | [diff] [blame] | 127 | /// EmitFAbs - Emit a call to fabs/fabsf/fabsl, depending on the type of ValTy, |
| 128 | /// which must be a scalar floating point type. |
| 129 | static Value *EmitFAbs(CodeGenFunction &CGF, Value *V, QualType ValTy) { |
| 130 | const BuiltinType *ValTyP = ValTy->getAs<BuiltinType>(); |
| 131 | assert(ValTyP && "isn't scalar fp type!"); |
| 132 | |
| 133 | StringRef FnName; |
| 134 | switch (ValTyP->getKind()) { |
| 135 | default: assert(0 && "Isn't a scalar fp type!"); |
| 136 | case BuiltinType::Float: FnName = "fabsf"; break; |
| 137 | case BuiltinType::Double: FnName = "fabs"; break; |
| 138 | case BuiltinType::LongDouble: FnName = "fabsl"; break; |
| 139 | } |
| 140 | |
| 141 | // The prototype is something that takes and returns whatever V's type is. |
| 142 | std::vector<const llvm::Type*> Args; |
| 143 | Args.push_back(V->getType()); |
| 144 | llvm::FunctionType *FT = llvm::FunctionType::get(V->getType(), Args, false); |
| 145 | llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction(FT, FnName); |
| 146 | |
| 147 | return CGF.Builder.CreateCall(Fn, V, "abs"); |
| 148 | } |
| 149 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 151 | unsigned BuiltinID, const CallExpr *E) { |
Chris Lattner | 564ea2a | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 152 | // See if we can constant fold this builtin. If so, don't emit it at all. |
Anders Carlsson | f35d35a | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 153 | Expr::EvalResult Result; |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 154 | if (E->Evaluate(Result, CGM.getContext())) { |
Anders Carlsson | f35d35a | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 155 | if (Result.Val.isInt()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | return RValue::get(llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 157 | Result.Val.getInt())); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 158 | else if (Result.Val.isFloat()) |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 159 | return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat())); |
Chris Lattner | 1f32999 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 160 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 564ea2a | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 162 | switch (BuiltinID) { |
| 163 | default: break; // Handle intrinsics and libm functions below. |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 164 | case Builtin::BI__builtin___CFStringMakeConstantString: |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 165 | case Builtin::BI__builtin___NSStringMakeConstantString: |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 166 | return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0)); |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 167 | case Builtin::BI__builtin_stdarg_start: |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 168 | case Builtin::BI__builtin_va_start: |
| 169 | case Builtin::BI__builtin_va_end: { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 170 | Value *ArgValue = EmitVAListRef(E->getArg(0)); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 171 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 172 | if (ArgValue->getType() != DestType) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | ArgValue = Builder.CreateBitCast(ArgValue, DestType, |
Daniel Dunbar | b27ffbe | 2009-07-26 09:28:40 +0000 | [diff] [blame] | 174 | ArgValue->getName().data()); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 175 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ? |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 177 | Intrinsic::vaend : Intrinsic::vastart; |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 178 | return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 179 | } |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 180 | case Builtin::BI__builtin_va_copy: { |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 181 | Value *DstPtr = EmitVAListRef(E->getArg(0)); |
| 182 | Value *SrcPtr = EmitVAListRef(E->getArg(1)); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 183 | |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 184 | const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 185 | |
| 186 | DstPtr = Builder.CreateBitCast(DstPtr, Type); |
| 187 | SrcPtr = Builder.CreateBitCast(SrcPtr, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 189 | DstPtr, SrcPtr)); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 190 | } |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 191 | case Builtin::BI__builtin_abs: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 193 | |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 194 | Value *NegOp = Builder.CreateNeg(ArgValue, "neg"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | Value *CmpResult = |
| 196 | Builder.CreateICmpSGE(ArgValue, |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 197 | llvm::Constant::getNullValue(ArgValue->getType()), |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 198 | "abscond"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | Value *Result = |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 200 | Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 202 | return RValue::get(Result); |
| 203 | } |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 204 | case Builtin::BI__builtin_ctz: |
| 205 | case Builtin::BI__builtin_ctzl: |
| 206 | case Builtin::BI__builtin_ctzll: { |
| 207 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 209 | const llvm::Type *ArgType = ArgValue->getType(); |
| 210 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
| 211 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 213 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 214 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 215 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 216 | "cast"); |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 217 | return RValue::get(Result); |
| 218 | } |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 219 | case Builtin::BI__builtin_clz: |
| 220 | case Builtin::BI__builtin_clzl: |
| 221 | case Builtin::BI__builtin_clzll: { |
| 222 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 224 | const llvm::Type *ArgType = ArgValue->getType(); |
| 225 | Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1); |
| 226 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 228 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 229 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 230 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 231 | "cast"); |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 232 | return RValue::get(Result); |
| 233 | } |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 234 | case Builtin::BI__builtin_ffs: |
| 235 | case Builtin::BI__builtin_ffsl: |
| 236 | case Builtin::BI__builtin_ffsll: { |
| 237 | // ffs(x) -> x ? cttz(x) + 1 : 0 |
| 238 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 240 | const llvm::Type *ArgType = ArgValue->getType(); |
| 241 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 243 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 245 | llvm::ConstantInt::get(ArgType, 1), "tmp"); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 246 | Value *Zero = llvm::Constant::getNullValue(ArgType); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 247 | Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); |
| 248 | Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs"); |
| 249 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 250 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 251 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 252 | return RValue::get(Result); |
| 253 | } |
| 254 | case Builtin::BI__builtin_parity: |
| 255 | case Builtin::BI__builtin_parityl: |
| 256 | case Builtin::BI__builtin_parityll: { |
| 257 | // parity(x) -> ctpop(x) & 1 |
| 258 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 260 | const llvm::Type *ArgType = ArgValue->getType(); |
| 261 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 263 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 264 | Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1), |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 266 | "tmp"); |
| 267 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 268 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 269 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 270 | return RValue::get(Result); |
| 271 | } |
| 272 | case Builtin::BI__builtin_popcount: |
| 273 | case Builtin::BI__builtin_popcountl: |
| 274 | case Builtin::BI__builtin_popcountll: { |
| 275 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 277 | const llvm::Type *ArgType = ArgValue->getType(); |
| 278 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 280 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 281 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 282 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 283 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 284 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 285 | return RValue::get(Result); |
| 286 | } |
Fariborz Jahanian | e42b8a5 | 2010-07-26 23:11:03 +0000 | [diff] [blame] | 287 | case Builtin::BI__builtin_expect: { |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 288 | // FIXME: pass expect through to LLVM |
Fariborz Jahanian | e42b8a5 | 2010-07-26 23:11:03 +0000 | [diff] [blame] | 289 | if (E->getArg(1)->HasSideEffects(getContext())) |
| 290 | (void)EmitScalarExpr(E->getArg(1)); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 291 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Fariborz Jahanian | e42b8a5 | 2010-07-26 23:11:03 +0000 | [diff] [blame] | 292 | } |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 293 | case Builtin::BI__builtin_bswap32: |
| 294 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 295 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 296 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 297 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 298 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | } |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 300 | case Builtin::BI__builtin_object_size: { |
Mike Stump | b16d32f | 2009-10-26 23:39:48 +0000 | [diff] [blame] | 301 | // We pass this builtin onto the optimizer so that it can |
| 302 | // figure out the object size in more complex cases. |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 303 | const llvm::Type *ResType[] = { |
| 304 | ConvertType(E->getType()) |
| 305 | }; |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 306 | |
| 307 | // LLVM only supports 0 and 2, make sure that we pass along that |
| 308 | // as a boolean. |
| 309 | Value *Ty = EmitScalarExpr(E->getArg(1)); |
| 310 | ConstantInt *CI = dyn_cast<ConstantInt>(Ty); |
| 311 | assert(CI); |
| 312 | uint64_t val = CI->getZExtValue(); |
| 313 | CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1); |
| 314 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 315 | Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1); |
| 316 | return RValue::get(Builder.CreateCall2(F, |
| 317 | EmitScalarExpr(E->getArg(0)), |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 318 | CI)); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 319 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 320 | case Builtin::BI__builtin_prefetch: { |
| 321 | Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); |
| 322 | // FIXME: Technically these constants should of type 'int', yes? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 324 | llvm::ConstantInt::get(Int32Ty, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 326 | llvm::ConstantInt::get(Int32Ty, 3); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 327 | Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); |
| 328 | return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 329 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 330 | case Builtin::BI__builtin_trap: { |
| 331 | Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); |
| 332 | return RValue::get(Builder.CreateCall(F)); |
| 333 | } |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 334 | case Builtin::BI__builtin_unreachable: { |
Mike Stump | fba565d | 2009-12-16 03:07:12 +0000 | [diff] [blame] | 335 | if (CatchUndefined && HaveInsertPoint()) |
| 336 | EmitBranch(getTrapBB()); |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 337 | Value *V = Builder.CreateUnreachable(); |
| 338 | Builder.ClearInsertionPoint(); |
| 339 | return RValue::get(V); |
| 340 | } |
| 341 | |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 342 | case Builtin::BI__builtin_powi: |
| 343 | case Builtin::BI__builtin_powif: |
| 344 | case Builtin::BI__builtin_powil: { |
| 345 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 346 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 347 | const llvm::Type *ArgType = Base->getType(); |
| 348 | Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 349 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 350 | } |
| 351 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 352 | case Builtin::BI__builtin_isgreater: |
| 353 | case Builtin::BI__builtin_isgreaterequal: |
| 354 | case Builtin::BI__builtin_isless: |
| 355 | case Builtin::BI__builtin_islessequal: |
| 356 | case Builtin::BI__builtin_islessgreater: |
| 357 | case Builtin::BI__builtin_isunordered: { |
| 358 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 359 | // floating point values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 361 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 363 | switch (BuiltinID) { |
| 364 | default: assert(0 && "Unknown ordered comparison"); |
| 365 | case Builtin::BI__builtin_isgreater: |
| 366 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 367 | break; |
| 368 | case Builtin::BI__builtin_isgreaterequal: |
| 369 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 370 | break; |
| 371 | case Builtin::BI__builtin_isless: |
| 372 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 373 | break; |
| 374 | case Builtin::BI__builtin_islessequal: |
| 375 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 376 | break; |
| 377 | case Builtin::BI__builtin_islessgreater: |
| 378 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 379 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | case Builtin::BI__builtin_isunordered: |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 381 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 382 | break; |
| 383 | } |
| 384 | // ZExt bool to int type. |
| 385 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 386 | "tmp")); |
| 387 | } |
Eli Friedman | d613989 | 2009-09-01 04:19:44 +0000 | [diff] [blame] | 388 | case Builtin::BI__builtin_isnan: { |
| 389 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 390 | V = Builder.CreateFCmpUNO(V, V, "cmp"); |
| 391 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp")); |
| 392 | } |
Chris Lattner | 420b118 | 2010-05-06 05:35:16 +0000 | [diff] [blame] | 393 | |
| 394 | case Builtin::BI__builtin_isinf: { |
| 395 | // isinf(x) --> fabs(x) == infinity |
| 396 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 397 | V = EmitFAbs(*this, V, E->getArg(0)->getType()); |
| 398 | |
| 399 | V = Builder.CreateFCmpOEQ(V, ConstantFP::getInfinity(V->getType()),"isinf"); |
| 400 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp")); |
| 401 | } |
Chris Lattner | 58ae5b4 | 2010-05-06 06:13:53 +0000 | [diff] [blame] | 402 | |
| 403 | // TODO: BI__builtin_isinf_sign |
| 404 | // isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 |
Benjamin Kramer | 6349ce9 | 2010-05-19 11:24:26 +0000 | [diff] [blame] | 405 | |
| 406 | case Builtin::BI__builtin_isnormal: { |
| 407 | // isnormal(x) --> x == x && fabsf(x) < infinity && fabsf(x) >= float_min |
| 408 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 409 | Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq"); |
| 410 | |
| 411 | Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType()); |
| 412 | Value *IsLessThanInf = |
| 413 | Builder.CreateFCmpULT(Abs, ConstantFP::getInfinity(V->getType()),"isinf"); |
| 414 | APFloat Smallest = APFloat::getSmallestNormalized( |
| 415 | getContext().getFloatTypeSemantics(E->getArg(0)->getType())); |
| 416 | Value *IsNormal = |
| 417 | Builder.CreateFCmpUGE(Abs, ConstantFP::get(V->getContext(), Smallest), |
| 418 | "isnormal"); |
| 419 | V = Builder.CreateAnd(Eq, IsLessThanInf, "and"); |
| 420 | V = Builder.CreateAnd(V, IsNormal, "and"); |
| 421 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()))); |
| 422 | } |
| 423 | |
Chris Lattner | ed07415 | 2010-05-06 06:04:13 +0000 | [diff] [blame] | 424 | case Builtin::BI__builtin_isfinite: { |
| 425 | // isfinite(x) --> x == x && fabs(x) != infinity; } |
| 426 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 427 | Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq"); |
| 428 | |
| 429 | Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType()); |
| 430 | Value *IsNotInf = |
| 431 | Builder.CreateFCmpUNE(Abs, ConstantFP::getInfinity(V->getType()),"isinf"); |
| 432 | |
| 433 | V = Builder.CreateAnd(Eq, IsNotInf, "and"); |
| 434 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()))); |
| 435 | } |
Benjamin Kramer | 7867f1a | 2010-06-14 10:30:41 +0000 | [diff] [blame] | 436 | |
| 437 | case Builtin::BI__builtin_fpclassify: { |
| 438 | Value *V = EmitScalarExpr(E->getArg(5)); |
| 439 | const llvm::Type *Ty = ConvertType(E->getArg(5)->getType()); |
| 440 | |
| 441 | // Create Result |
| 442 | BasicBlock *Begin = Builder.GetInsertBlock(); |
| 443 | BasicBlock *End = createBasicBlock("fpclassify_end", this->CurFn); |
| 444 | Builder.SetInsertPoint(End); |
| 445 | PHINode *Result = |
| 446 | Builder.CreatePHI(ConvertType(E->getArg(0)->getType()), |
| 447 | "fpclassify_result"); |
| 448 | |
| 449 | // if (V==0) return FP_ZERO |
| 450 | Builder.SetInsertPoint(Begin); |
| 451 | Value *IsZero = Builder.CreateFCmpOEQ(V, Constant::getNullValue(Ty), |
| 452 | "iszero"); |
| 453 | Value *ZeroLiteral = EmitScalarExpr(E->getArg(4)); |
| 454 | BasicBlock *NotZero = createBasicBlock("fpclassify_not_zero", this->CurFn); |
| 455 | Builder.CreateCondBr(IsZero, End, NotZero); |
| 456 | Result->addIncoming(ZeroLiteral, Begin); |
| 457 | |
| 458 | // if (V != V) return FP_NAN |
| 459 | Builder.SetInsertPoint(NotZero); |
| 460 | Value *IsNan = Builder.CreateFCmpUNO(V, V, "cmp"); |
| 461 | Value *NanLiteral = EmitScalarExpr(E->getArg(0)); |
| 462 | BasicBlock *NotNan = createBasicBlock("fpclassify_not_nan", this->CurFn); |
| 463 | Builder.CreateCondBr(IsNan, End, NotNan); |
| 464 | Result->addIncoming(NanLiteral, NotZero); |
| 465 | |
| 466 | // if (fabs(V) == infinity) return FP_INFINITY |
| 467 | Builder.SetInsertPoint(NotNan); |
| 468 | Value *VAbs = EmitFAbs(*this, V, E->getArg(5)->getType()); |
| 469 | Value *IsInf = |
| 470 | Builder.CreateFCmpOEQ(VAbs, ConstantFP::getInfinity(V->getType()), |
| 471 | "isinf"); |
| 472 | Value *InfLiteral = EmitScalarExpr(E->getArg(1)); |
| 473 | BasicBlock *NotInf = createBasicBlock("fpclassify_not_inf", this->CurFn); |
| 474 | Builder.CreateCondBr(IsInf, End, NotInf); |
| 475 | Result->addIncoming(InfLiteral, NotNan); |
| 476 | |
| 477 | // if (fabs(V) >= MIN_NORMAL) return FP_NORMAL else FP_SUBNORMAL |
| 478 | Builder.SetInsertPoint(NotInf); |
| 479 | APFloat Smallest = APFloat::getSmallestNormalized( |
| 480 | getContext().getFloatTypeSemantics(E->getArg(5)->getType())); |
| 481 | Value *IsNormal = |
| 482 | Builder.CreateFCmpUGE(VAbs, ConstantFP::get(V->getContext(), Smallest), |
| 483 | "isnormal"); |
| 484 | Value *NormalResult = |
| 485 | Builder.CreateSelect(IsNormal, EmitScalarExpr(E->getArg(2)), |
| 486 | EmitScalarExpr(E->getArg(3))); |
| 487 | Builder.CreateBr(End); |
| 488 | Result->addIncoming(NormalResult, NotInf); |
| 489 | |
| 490 | // return Result |
| 491 | Builder.SetInsertPoint(End); |
| 492 | return RValue::get(Result); |
| 493 | } |
Chris Lattner | ed07415 | 2010-05-06 06:04:13 +0000 | [diff] [blame] | 494 | |
Eli Friedman | b52fe9c | 2009-06-02 07:10:30 +0000 | [diff] [blame] | 495 | case Builtin::BIalloca: |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 496 | case Builtin::BI__builtin_alloca: { |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 497 | Value *Size = EmitScalarExpr(E->getArg(0)); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 498 | return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp")); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 499 | } |
Eli Friedman | e6dddfd | 2010-01-23 19:00:10 +0000 | [diff] [blame] | 500 | case Builtin::BIbzero: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 501 | case Builtin::BI__builtin_bzero: { |
| 502 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 503 | Value *SizeVal = EmitScalarExpr(E->getArg(1)); |
| 504 | Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()), |
| 505 | Address, |
| 506 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0), |
| 507 | SizeVal, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 508 | llvm::ConstantInt::get(Int32Ty, 1), |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 509 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 510 | return RValue::get(Address); |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 511 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 512 | case Builtin::BImemcpy: |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 513 | case Builtin::BI__builtin_memcpy: { |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 514 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 515 | Value *SrcAddr = EmitScalarExpr(E->getArg(1)); |
| 516 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 517 | Builder.CreateCall5(CGM.getMemCpyFn(Address->getType(), SrcAddr->getType(), |
| 518 | SizeVal->getType()), |
| 519 | Address, SrcAddr, SizeVal, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 520 | llvm::ConstantInt::get(Int32Ty, 1), |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 521 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 522 | return RValue::get(Address); |
| 523 | } |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 524 | |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 525 | case Builtin::BI__builtin_objc_memmove_collectable: { |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 526 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 527 | Value *SrcAddr = EmitScalarExpr(E->getArg(1)); |
| 528 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 529 | CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, |
| 530 | Address, SrcAddr, SizeVal); |
| 531 | return RValue::get(Address); |
| 532 | } |
| 533 | |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 534 | case Builtin::BImemmove: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 535 | case Builtin::BI__builtin_memmove: { |
| 536 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 537 | Value *SrcAddr = EmitScalarExpr(E->getArg(1)); |
| 538 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 539 | Builder.CreateCall5(CGM.getMemMoveFn(Address->getType(), SrcAddr->getType(), |
| 540 | SizeVal->getType()), |
| 541 | Address, SrcAddr, SizeVal, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 542 | llvm::ConstantInt::get(Int32Ty, 1), |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 543 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 544 | return RValue::get(Address); |
| 545 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 546 | case Builtin::BImemset: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 547 | case Builtin::BI__builtin_memset: { |
| 548 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 549 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 550 | Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()), |
| 551 | Address, |
| 552 | Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), |
| 553 | llvm::Type::getInt8Ty(VMContext)), |
| 554 | SizeVal, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 555 | llvm::ConstantInt::get(Int32Ty, 1), |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 556 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 557 | return RValue::get(Address); |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 558 | } |
John McCall | fb17a56 | 2010-03-03 10:30:05 +0000 | [diff] [blame] | 559 | case Builtin::BI__builtin_dwarf_cfa: { |
| 560 | // The offset in bytes from the first argument to the CFA. |
| 561 | // |
| 562 | // Why on earth is this in the frontend? Is there any reason at |
| 563 | // all that the backend can't reasonably determine this while |
| 564 | // lowering llvm.eh.dwarf.cfa()? |
| 565 | // |
| 566 | // TODO: If there's a satisfactory reason, add a target hook for |
| 567 | // this instead of hard-coding 0, which is correct for most targets. |
| 568 | int32_t Offset = 0; |
| 569 | |
| 570 | Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa, 0, 0); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 571 | return RValue::get(Builder.CreateCall(F, |
| 572 | llvm::ConstantInt::get(Int32Ty, Offset))); |
John McCall | fb17a56 | 2010-03-03 10:30:05 +0000 | [diff] [blame] | 573 | } |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 574 | case Builtin::BI__builtin_return_address: { |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 575 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 576 | Depth = Builder.CreateIntCast(Depth, Int32Ty, false, "tmp"); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 577 | Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 578 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 579 | } |
| 580 | case Builtin::BI__builtin_frame_address: { |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 581 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 582 | Depth = Builder.CreateIntCast(Depth, Int32Ty, false, "tmp"); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 583 | Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 584 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 585 | } |
Eli Friedman | 3b660ef | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 586 | case Builtin::BI__builtin_extract_return_addr: { |
John McCall | 492c4f9 | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 587 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 588 | Value *Result = getTargetHooks().decodeReturnAddress(*this, Address); |
| 589 | return RValue::get(Result); |
| 590 | } |
| 591 | case Builtin::BI__builtin_frob_return_addr: { |
| 592 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 593 | Value *Result = getTargetHooks().encodeReturnAddress(*this, Address); |
| 594 | return RValue::get(Result); |
Eli Friedman | 3b660ef | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 595 | } |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 596 | case Builtin::BI__builtin_dwarf_sp_column: { |
| 597 | const llvm::IntegerType *Ty |
| 598 | = cast<llvm::IntegerType>(ConvertType(E->getType())); |
| 599 | int Column = getTargetHooks().getDwarfEHStackPointer(CGM); |
| 600 | if (Column == -1) { |
| 601 | CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column"); |
| 602 | return RValue::get(llvm::UndefValue::get(Ty)); |
| 603 | } |
| 604 | return RValue::get(llvm::ConstantInt::get(Ty, Column, true)); |
| 605 | } |
| 606 | case Builtin::BI__builtin_init_dwarf_reg_size_table: { |
| 607 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 608 | if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address)) |
| 609 | CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table"); |
| 610 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
| 611 | } |
John McCall | 7ada111 | 2010-03-03 05:38:58 +0000 | [diff] [blame] | 612 | case Builtin::BI__builtin_eh_return: { |
| 613 | Value *Int = EmitScalarExpr(E->getArg(0)); |
| 614 | Value *Ptr = EmitScalarExpr(E->getArg(1)); |
| 615 | |
| 616 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType()); |
| 617 | assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) && |
| 618 | "LLVM's __builtin_eh_return only supports 32- and 64-bit variants"); |
| 619 | Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32 |
| 620 | ? Intrinsic::eh_return_i32 |
| 621 | : Intrinsic::eh_return_i64, |
| 622 | 0, 0); |
| 623 | Builder.CreateCall2(F, Int, Ptr); |
| 624 | Value *V = Builder.CreateUnreachable(); |
| 625 | Builder.ClearInsertionPoint(); |
| 626 | return RValue::get(V); |
| 627 | } |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 628 | case Builtin::BI__builtin_unwind_init: { |
| 629 | Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0); |
| 630 | return RValue::get(Builder.CreateCall(F)); |
| 631 | } |
John McCall | 5e11085 | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 632 | case Builtin::BI__builtin_extend_pointer: { |
| 633 | // Extends a pointer to the size of an _Unwind_Word, which is |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 634 | // uint64_t on all platforms. Generally this gets poked into a |
| 635 | // register and eventually used as an address, so if the |
| 636 | // addressing registers are wider than pointers and the platform |
| 637 | // doesn't implicitly ignore high-order bits when doing |
| 638 | // addressing, we need to make sure we zext / sext based on |
| 639 | // the platform's expectations. |
John McCall | 5e11085 | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 640 | // |
| 641 | // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 642 | |
| 643 | LLVMContext &C = CGM.getLLVMContext(); |
| 644 | |
| 645 | // Cast the pointer to intptr_t. |
John McCall | 5e11085 | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 646 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 647 | const llvm::IntegerType *IntPtrTy = CGM.getTargetData().getIntPtrType(C); |
| 648 | Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast"); |
| 649 | |
| 650 | // If that's 64 bits, we're done. |
| 651 | if (IntPtrTy->getBitWidth() == 64) |
| 652 | return RValue::get(Result); |
| 653 | |
| 654 | // Otherwise, ask the codegen data what to do. |
John McCall | 492c4f9 | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 655 | if (getTargetHooks().extendPointerWithSExt()) |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 656 | return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext")); |
| 657 | else |
| 658 | return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext")); |
John McCall | 5e11085 | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 659 | } |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 660 | case Builtin::BI__builtin_setjmp: { |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 661 | // Buffer is a void**. |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 662 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 663 | |
| 664 | // Store the frame pointer to the setjmp buffer. |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 665 | Value *FrameAddr = |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 666 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::frameaddress), |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 667 | ConstantInt::get(Int32Ty, 0)); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 668 | Builder.CreateStore(FrameAddr, Buf); |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 669 | |
Jim Grosbach | 6d172e2 | 2010-05-27 23:54:20 +0000 | [diff] [blame] | 670 | // Store the stack pointer to the setjmp buffer. |
| 671 | Value *StackAddr = |
| 672 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave)); |
| 673 | Value *StackSaveSlot = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 674 | Builder.CreateGEP(Buf, ConstantInt::get(Int32Ty, 2)); |
Jim Grosbach | 6d172e2 | 2010-05-27 23:54:20 +0000 | [diff] [blame] | 675 | Builder.CreateStore(StackAddr, StackSaveSlot); |
| 676 | |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 677 | // Call LLVM's EH setjmp, which is lightweight. |
| 678 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp); |
| 679 | Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext)); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 680 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 681 | } |
| 682 | case Builtin::BI__builtin_longjmp: { |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 683 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 684 | Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext)); |
| 685 | |
| 686 | // Call LLVM's EH longjmp, which is lightweight. |
| 687 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf); |
| 688 | |
| 689 | // longjmp doesn't return; mark this as unreachable |
| 690 | Value *V = Builder.CreateUnreachable(); |
| 691 | Builder.ClearInsertionPoint(); |
| 692 | return RValue::get(V); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 693 | } |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 694 | case Builtin::BI__sync_fetch_and_add: |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 695 | case Builtin::BI__sync_fetch_and_sub: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 696 | case Builtin::BI__sync_fetch_and_or: |
| 697 | case Builtin::BI__sync_fetch_and_and: |
| 698 | case Builtin::BI__sync_fetch_and_xor: |
| 699 | case Builtin::BI__sync_add_and_fetch: |
| 700 | case Builtin::BI__sync_sub_and_fetch: |
| 701 | case Builtin::BI__sync_and_and_fetch: |
| 702 | case Builtin::BI__sync_or_and_fetch: |
| 703 | case Builtin::BI__sync_xor_and_fetch: |
| 704 | case Builtin::BI__sync_val_compare_and_swap: |
| 705 | case Builtin::BI__sync_bool_compare_and_swap: |
| 706 | case Builtin::BI__sync_lock_test_and_set: |
| 707 | case Builtin::BI__sync_lock_release: |
| 708 | assert(0 && "Shouldn't make it through sema"); |
| 709 | case Builtin::BI__sync_fetch_and_add_1: |
| 710 | case Builtin::BI__sync_fetch_and_add_2: |
| 711 | case Builtin::BI__sync_fetch_and_add_4: |
| 712 | case Builtin::BI__sync_fetch_and_add_8: |
| 713 | case Builtin::BI__sync_fetch_and_add_16: |
| 714 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); |
| 715 | case Builtin::BI__sync_fetch_and_sub_1: |
| 716 | case Builtin::BI__sync_fetch_and_sub_2: |
| 717 | case Builtin::BI__sync_fetch_and_sub_4: |
| 718 | case Builtin::BI__sync_fetch_and_sub_8: |
| 719 | case Builtin::BI__sync_fetch_and_sub_16: |
Mon P Wang | 09b6bf5 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 720 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 721 | case Builtin::BI__sync_fetch_and_or_1: |
| 722 | case Builtin::BI__sync_fetch_and_or_2: |
| 723 | case Builtin::BI__sync_fetch_and_or_4: |
| 724 | case Builtin::BI__sync_fetch_and_or_8: |
| 725 | case Builtin::BI__sync_fetch_and_or_16: |
| 726 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 727 | case Builtin::BI__sync_fetch_and_and_1: |
| 728 | case Builtin::BI__sync_fetch_and_and_2: |
| 729 | case Builtin::BI__sync_fetch_and_and_4: |
| 730 | case Builtin::BI__sync_fetch_and_and_8: |
| 731 | case Builtin::BI__sync_fetch_and_and_16: |
| 732 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 733 | case Builtin::BI__sync_fetch_and_xor_1: |
| 734 | case Builtin::BI__sync_fetch_and_xor_2: |
| 735 | case Builtin::BI__sync_fetch_and_xor_4: |
| 736 | case Builtin::BI__sync_fetch_and_xor_8: |
| 737 | case Builtin::BI__sync_fetch_and_xor_16: |
| 738 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 740 | // Clang extensions: not overloaded yet. |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 741 | case Builtin::BI__sync_fetch_and_min: |
| 742 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 743 | case Builtin::BI__sync_fetch_and_max: |
| 744 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 745 | case Builtin::BI__sync_fetch_and_umin: |
| 746 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 747 | case Builtin::BI__sync_fetch_and_umax: |
| 748 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 749 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 750 | case Builtin::BI__sync_add_and_fetch_1: |
| 751 | case Builtin::BI__sync_add_and_fetch_2: |
| 752 | case Builtin::BI__sync_add_and_fetch_4: |
| 753 | case Builtin::BI__sync_add_and_fetch_8: |
| 754 | case Builtin::BI__sync_add_and_fetch_16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 756 | llvm::Instruction::Add); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 757 | case Builtin::BI__sync_sub_and_fetch_1: |
| 758 | case Builtin::BI__sync_sub_and_fetch_2: |
| 759 | case Builtin::BI__sync_sub_and_fetch_4: |
| 760 | case Builtin::BI__sync_sub_and_fetch_8: |
| 761 | case Builtin::BI__sync_sub_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 762 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, |
| 763 | llvm::Instruction::Sub); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 764 | case Builtin::BI__sync_and_and_fetch_1: |
| 765 | case Builtin::BI__sync_and_and_fetch_2: |
| 766 | case Builtin::BI__sync_and_and_fetch_4: |
| 767 | case Builtin::BI__sync_and_and_fetch_8: |
| 768 | case Builtin::BI__sync_and_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 769 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, |
| 770 | llvm::Instruction::And); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 771 | case Builtin::BI__sync_or_and_fetch_1: |
| 772 | case Builtin::BI__sync_or_and_fetch_2: |
| 773 | case Builtin::BI__sync_or_and_fetch_4: |
| 774 | case Builtin::BI__sync_or_and_fetch_8: |
| 775 | case Builtin::BI__sync_or_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 776 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, |
| 777 | llvm::Instruction::Or); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 778 | case Builtin::BI__sync_xor_and_fetch_1: |
| 779 | case Builtin::BI__sync_xor_and_fetch_2: |
| 780 | case Builtin::BI__sync_xor_and_fetch_4: |
| 781 | case Builtin::BI__sync_xor_and_fetch_8: |
| 782 | case Builtin::BI__sync_xor_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 783 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, |
| 784 | llvm::Instruction::Xor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 786 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 787 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 788 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 789 | case Builtin::BI__sync_val_compare_and_swap_8: |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 790 | case Builtin::BI__sync_val_compare_and_swap_16: { |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 791 | const llvm::Type *ValueType = |
| 792 | llvm::IntegerType::get(CGF.getLLVMContext(), |
| 793 | CGF.getContext().getTypeSize(E->getType())); |
| 794 | const llvm::Type *PtrType = ValueType->getPointerTo(); |
| 795 | const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType }; |
| 796 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, |
| 797 | IntrinsicTypes, 2); |
| 798 | |
| 799 | Value *Args[3] = { Builder.CreateBitCast(CGF.EmitScalarExpr(E->getArg(0)), |
| 800 | PtrType), |
| 801 | EmitCastToInt(CGF, ValueType, |
| 802 | CGF.EmitScalarExpr(E->getArg(1))), |
| 803 | EmitCastToInt(CGF, ValueType, |
| 804 | CGF.EmitScalarExpr(E->getArg(2))) }; |
| 805 | return RValue::get(EmitCastFromInt(CGF, E->getType(), |
| 806 | EmitCallWithBarrier(CGF, AtomF, Args, |
| 807 | Args + 3))); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 808 | } |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 810 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 811 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 812 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 813 | case Builtin::BI__sync_bool_compare_and_swap_8: |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 814 | case Builtin::BI__sync_bool_compare_and_swap_16: { |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 815 | const llvm::Type *ValueType = |
| 816 | llvm::IntegerType::get( |
| 817 | CGF.getLLVMContext(), |
| 818 | CGF.getContext().getTypeSize(E->getArg(1)->getType())); |
| 819 | const llvm::Type *PtrType = ValueType->getPointerTo(); |
| 820 | const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType }; |
| 821 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, |
| 822 | IntrinsicTypes, 2); |
| 823 | |
| 824 | Value *Args[3] = { Builder.CreateBitCast(CGF.EmitScalarExpr(E->getArg(0)), |
| 825 | PtrType), |
| 826 | EmitCastToInt(CGF, ValueType, |
| 827 | CGF.EmitScalarExpr(E->getArg(1))), |
| 828 | EmitCastToInt(CGF, ValueType, |
| 829 | CGF.EmitScalarExpr(E->getArg(2))) }; |
| 830 | Value *OldVal = Args[1]; |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 831 | Value *PrevVal = EmitCallWithBarrier(*this, AtomF, Args, Args + 3); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 832 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 833 | // zext bool to int. |
| 834 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 835 | } |
| 836 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 837 | case Builtin::BI__sync_lock_test_and_set_1: |
| 838 | case Builtin::BI__sync_lock_test_and_set_2: |
| 839 | case Builtin::BI__sync_lock_test_and_set_4: |
| 840 | case Builtin::BI__sync_lock_test_and_set_8: |
| 841 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 842 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 843 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 844 | case Builtin::BI__sync_lock_release_1: |
| 845 | case Builtin::BI__sync_lock_release_2: |
| 846 | case Builtin::BI__sync_lock_release_4: |
| 847 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 848 | case Builtin::BI__sync_lock_release_16: { |
| 849 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 850 | const llvm::Type *ElTy = |
| 851 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Daniel Dunbar | 007b567 | 2009-11-29 21:11:47 +0000 | [diff] [blame] | 852 | llvm::StoreInst *Store = |
| 853 | Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr); |
| 854 | Store->setVolatile(true); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 855 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 856 | } |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 857 | |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 858 | case Builtin::BI__sync_synchronize: { |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 859 | // We assume like gcc appears to, that this only applies to cached memory. |
| 860 | EmitMemoryBarrier(*this, true, true, true, true, false); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 861 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Tanya Lattner | 0b57164 | 2010-01-16 01:21:14 +0000 | [diff] [blame] | 864 | case Builtin::BI__builtin_llvm_memory_barrier: { |
| 865 | Value *C[5] = { |
| 866 | EmitScalarExpr(E->getArg(0)), |
| 867 | EmitScalarExpr(E->getArg(1)), |
| 868 | EmitScalarExpr(E->getArg(2)), |
| 869 | EmitScalarExpr(E->getArg(3)), |
| 870 | EmitScalarExpr(E->getArg(4)) |
| 871 | }; |
| 872 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
| 873 | return RValue::get(0); |
| 874 | } |
| 875 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 876 | // Library functions with special handling. |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 877 | case Builtin::BIsqrt: |
| 878 | case Builtin::BIsqrtf: |
| 879 | case Builtin::BIsqrtl: { |
John McCall | beb4128 | 2010-04-07 08:20:20 +0000 | [diff] [blame] | 880 | // TODO: there is currently no set of optimizer flags |
| 881 | // sufficient for us to rewrite sqrt to @llvm.sqrt. |
| 882 | // -fmath-errno=0 is not good enough; we need finiteness. |
| 883 | // We could probably precondition the call with an ult |
| 884 | // against 0, but is that worth the complexity? |
| 885 | break; |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | case Builtin::BIpow: |
| 889 | case Builtin::BIpowf: |
| 890 | case Builtin::BIpowl: { |
| 891 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 892 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 893 | break; |
| 894 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 895 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 896 | const llvm::Type *ArgType = Base->getType(); |
| 897 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 898 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 899 | } |
Eli Friedman | ba68b08 | 2010-03-06 02:17:52 +0000 | [diff] [blame] | 900 | |
| 901 | case Builtin::BI__builtin_signbit: |
| 902 | case Builtin::BI__builtin_signbitf: |
| 903 | case Builtin::BI__builtin_signbitl: { |
| 904 | LLVMContext &C = CGM.getLLVMContext(); |
| 905 | |
| 906 | Value *Arg = EmitScalarExpr(E->getArg(0)); |
| 907 | const llvm::Type *ArgTy = Arg->getType(); |
| 908 | if (ArgTy->isPPC_FP128Ty()) |
| 909 | break; // FIXME: I'm not sure what the right implementation is here. |
| 910 | int ArgWidth = ArgTy->getPrimitiveSizeInBits(); |
| 911 | const llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth); |
| 912 | Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy); |
| 913 | Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy); |
| 914 | Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp); |
| 915 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 916 | } |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 917 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 918 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 919 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 920 | // that function. |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 921 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 922 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 923 | return EmitCall(E->getCallee()->getType(), |
| 924 | CGM.getBuiltinLibFunction(FD, BuiltinID), |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 925 | ReturnValueSlot(), |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 926 | E->arg_begin(), E->arg_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 928 | // See if we have a target specific intrinsic. |
Dale Johannesen | a6f80ef | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 929 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 930 | Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic; |
| 931 | if (const char *Prefix = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | llvm::Triple::getArchTypePrefix(Target.getTriple().getArch())) |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 933 | IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 935 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 936 | SmallVector<Value*, 16> Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 938 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 939 | const llvm::FunctionType *FTy = F->getFunctionType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 940 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 941 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 942 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 944 | // If the intrinsic arg type is different from the builtin arg type |
| 945 | // we need to do a bit cast. |
| 946 | const llvm::Type *PTy = FTy->getParamType(i); |
| 947 | if (PTy != ArgValue->getType()) { |
| 948 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 949 | "Must be able to losslessly bit cast to param"); |
| 950 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 951 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 953 | Args.push_back(ArgValue); |
| 954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 956 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 957 | QualType BuiltinRetType = E->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 959 | const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 960 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 962 | if (RetTy != V->getType()) { |
| 963 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 964 | "Must be able to losslessly bit cast result type"); |
| 965 | V = Builder.CreateBitCast(V, RetTy); |
| 966 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 968 | return RValue::get(V); |
| 969 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 971 | // See if we have a target specific builtin that needs to be lowered. |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 972 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 973 | return RValue::get(V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 975 | ErrorUnsupported(E, "builtin function"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 977 | // Unknown builtin, for now just dump it out and return undef. |
| 978 | if (hasAggregateLLVMType(E->getType())) |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 979 | return RValue::getAggregate(CreateMemTemp(E->getType())); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 980 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 982 | |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 983 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 984 | const CallExpr *E) { |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 985 | switch (Target.getTriple().getArch()) { |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 986 | case llvm::Triple::arm: |
| 987 | case llvm::Triple::thumb: |
| 988 | return EmitARMBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 989 | case llvm::Triple::x86: |
| 990 | case llvm::Triple::x86_64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 991 | return EmitX86BuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 992 | case llvm::Triple::ppc: |
| 993 | case llvm::Triple::ppc64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 994 | return EmitPPCBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 995 | default: |
| 996 | return 0; |
| 997 | } |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1000 | const llvm::VectorType *GetNeonType(LLVMContext &C, unsigned type, bool q) { |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1001 | switch (type) { |
| 1002 | default: break; |
| 1003 | case 0: |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1004 | case 5: return llvm::VectorType::get(llvm::Type::getInt8Ty(C), 8 << (int)q); |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1005 | case 6: |
| 1006 | case 7: |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1007 | case 1: return llvm::VectorType::get(llvm::Type::getInt16Ty(C),4 << (int)q); |
| 1008 | case 2: return llvm::VectorType::get(llvm::Type::getInt32Ty(C),2 << (int)q); |
| 1009 | case 3: return llvm::VectorType::get(llvm::Type::getInt64Ty(C),1 << (int)q); |
| 1010 | case 4: return llvm::VectorType::get(llvm::Type::getFloatTy(C),2 << (int)q); |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1011 | }; |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Nate Begeman | cd480ad | 2010-08-06 01:24:57 +0000 | [diff] [blame] | 1015 | Value *CodeGenFunction::EmitNeonSplat(Value *V, Constant *C, bool widen) { |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1016 | unsigned nElts = cast<llvm::VectorType>(V->getType())->getNumElements(); |
Nate Begeman | cd480ad | 2010-08-06 01:24:57 +0000 | [diff] [blame] | 1017 | if (widen) |
| 1018 | nElts <<= 1; |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1019 | SmallVector<Constant*, 16> Indices(nElts, C); |
| 1020 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1021 | return Builder.CreateShuffleVector(V, V, SV, "lane"); |
| 1022 | } |
| 1023 | |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1024 | Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl<Value*> &Ops, |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1025 | const char *name, bool splat, |
| 1026 | unsigned shift, bool rightshift) { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1027 | unsigned j = 0; |
| 1028 | for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end(); |
| 1029 | ai != ae; ++ai, ++j) |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1030 | if (shift > 0 && shift == j) |
| 1031 | Ops[j] = EmitNeonShiftVector(Ops[j], ai->getType(), rightshift); |
| 1032 | else |
| 1033 | Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1034 | |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1035 | if (splat) { |
| 1036 | Ops[j-1] = EmitNeonSplat(Ops[j-1], cast<Constant>(Ops[j])); |
| 1037 | Ops.resize(j); |
| 1038 | } |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1039 | return Builder.CreateCall(F, Ops.begin(), Ops.end(), name); |
| 1040 | } |
| 1041 | |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1042 | Value *CodeGenFunction::EmitNeonShiftVector(Value *V, const llvm::Type *Ty, |
| 1043 | bool neg) { |
| 1044 | ConstantInt *CI = cast<ConstantInt>(V); |
| 1045 | int SV = CI->getSExtValue(); |
| 1046 | |
| 1047 | const llvm::VectorType *VTy = cast<llvm::VectorType>(Ty); |
| 1048 | llvm::Constant *C = ConstantInt::get(VTy->getElementType(), neg ? -SV : SV); |
| 1049 | SmallVector<llvm::Constant*, 16> CV(VTy->getNumElements(), C); |
| 1050 | return llvm::ConstantVector::get(CV.begin(), CV.size()); |
| 1051 | } |
| 1052 | |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1053 | /// GetPointeeAlignment - Given an expression with a pointer type, find the |
| 1054 | /// alignment of the type referenced by the pointer. Skip over implicit |
| 1055 | /// casts. |
| 1056 | static Value *GetPointeeAlignment(CodeGenFunction &CGF, const Expr *Addr) { |
| 1057 | unsigned Align = 1; |
| 1058 | // Check if the type is a pointer. The implicit cast operand might not be. |
| 1059 | while (Addr->getType()->isPointerType()) { |
| 1060 | QualType PtTy = Addr->getType()->getPointeeType(); |
| 1061 | unsigned NewA = CGF.getContext().getTypeAlignInChars(PtTy).getQuantity(); |
| 1062 | if (NewA > Align) |
| 1063 | Align = NewA; |
| 1064 | |
| 1065 | // If the address is an implicit cast, repeat with the cast operand. |
| 1066 | if (const ImplicitCastExpr *CastAddr = dyn_cast<ImplicitCastExpr>(Addr)) { |
| 1067 | Addr = CastAddr->getSubExpr(); |
| 1068 | continue; |
| 1069 | } |
| 1070 | break; |
| 1071 | } |
| 1072 | return llvm::ConstantInt::get(CGF.Int32Ty, Align); |
| 1073 | } |
| 1074 | |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 1075 | Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, |
| 1076 | const CallExpr *E) { |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1077 | if (BuiltinID == ARM::BI__clear_cache) { |
Rafael Espindola | 79ba509 | 2010-06-07 17:26:50 +0000 | [diff] [blame] | 1078 | const FunctionDecl *FD = E->getDirectCallee(); |
| 1079 | Value *a = EmitScalarExpr(E->getArg(0)); |
| 1080 | Value *b = EmitScalarExpr(E->getArg(1)); |
| 1081 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType()); |
| 1082 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 1083 | llvm::StringRef Name = FD->getName(); |
| 1084 | return Builder.CreateCall2(CGM.CreateRuntimeFunction(FTy, Name), |
| 1085 | a, b); |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 1086 | } |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1087 | |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1088 | llvm::SmallVector<Value*, 4> Ops; |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1089 | for (unsigned i = 0, e = E->getNumArgs() - 1; i != e; i++) |
| 1090 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 1091 | |
| 1092 | llvm::APSInt Result; |
| 1093 | const Expr *Arg = E->getArg(E->getNumArgs()-1); |
| 1094 | if (!Arg->isIntegerConstantExpr(Result, getContext())) |
| 1095 | return 0; |
| 1096 | |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 1097 | if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f || |
| 1098 | BuiltinID == ARM::BI__builtin_arm_vcvtr_d) { |
| 1099 | // Determine the overloaded type of this builtin. |
| 1100 | const llvm::Type *Ty; |
| 1101 | if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f) |
| 1102 | Ty = llvm::Type::getFloatTy(VMContext); |
| 1103 | else |
| 1104 | Ty = llvm::Type::getDoubleTy(VMContext); |
| 1105 | |
| 1106 | // Determine whether this is an unsigned conversion or not. |
| 1107 | bool usgn = Result.getZExtValue() == 1; |
| 1108 | unsigned Int = usgn ? Intrinsic::arm_vcvtru : Intrinsic::arm_vcvtr; |
| 1109 | |
| 1110 | // Call the appropriate intrinsic. |
| 1111 | Function *F = CGM.getIntrinsic(Int, &Ty, 1); |
| 1112 | return Builder.CreateCall(F, Ops.begin(), Ops.end(), "vcvtr"); |
| 1113 | } |
| 1114 | |
| 1115 | // Determine the type of this overloaded NEON intrinsic. |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1116 | unsigned type = Result.getZExtValue(); |
| 1117 | bool usgn = type & 0x08; |
| 1118 | bool quad = type & 0x10; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 1119 | bool poly = (type & 0x7) == 5 || (type & 0x7) == 6; |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1120 | bool splat = false; |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1121 | |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1122 | const llvm::VectorType *VTy = GetNeonType(VMContext, type & 0x7, quad); |
| 1123 | const llvm::Type *Ty = VTy; |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1124 | if (!Ty) |
| 1125 | return 0; |
| 1126 | |
| 1127 | unsigned Int; |
| 1128 | switch (BuiltinID) { |
| 1129 | default: return 0; |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1130 | case ARM::BI__builtin_neon_vaba_v: |
Bob Wilson | 47a88fa | 2010-09-03 01:27:09 +0000 | [diff] [blame] | 1131 | case ARM::BI__builtin_neon_vabaq_v: { |
| 1132 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1133 | SmallVector<Value*, 2> Args; |
| 1134 | Args.push_back(Ops[1]); |
| 1135 | Args.push_back(Ops[2]); |
| 1136 | Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds; |
| 1137 | Ops[1] = EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Args, "vaba"); |
| 1138 | return Builder.CreateAdd(Ops[0], Ops[1], "vaba"); |
| 1139 | } |
| 1140 | case ARM::BI__builtin_neon_vabal_v: { |
| 1141 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1142 | SmallVector<Value*, 2> Args; |
| 1143 | Args.push_back(Ops[1]); |
| 1144 | Args.push_back(Ops[2]); |
| 1145 | Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds; |
| 1146 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1147 | Ops[1] = EmitNeonCall(CGM.getIntrinsic(Int, &DTy, 1), Args, "vabal"); |
| 1148 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1149 | return Builder.CreateAdd(Ops[0], Ops[1], "vabal"); |
| 1150 | } |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1151 | case ARM::BI__builtin_neon_vabd_v: |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1152 | case ARM::BI__builtin_neon_vabdq_v: |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1153 | Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1154 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabd"); |
Bob Wilson | 47a88fa | 2010-09-03 01:27:09 +0000 | [diff] [blame] | 1155 | case ARM::BI__builtin_neon_vabdl_v: { |
| 1156 | Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds; |
| 1157 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1158 | Ops[0] = EmitNeonCall(CGM.getIntrinsic(Int, &DTy, 1), Ops, "vabdl"); |
| 1159 | return Builder.CreateZExt(Ops[0], Ty, "vabdl"); |
| 1160 | } |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1161 | case ARM::BI__builtin_neon_vabs_v: |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1162 | case ARM::BI__builtin_neon_vabsq_v: |
| 1163 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vabs, &Ty, 1), |
| 1164 | Ops, "vabs"); |
| 1165 | case ARM::BI__builtin_neon_vaddhn_v: |
| 1166 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vaddhn, &Ty, 1), |
| 1167 | Ops, "vaddhn"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1168 | case ARM::BI__builtin_neon_vaddl_v: { |
| 1169 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1170 | Ops[0] = Builder.CreateBitCast(Ops[0], DTy); |
| 1171 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | 54160c6 | 2010-08-29 05:14:28 +0000 | [diff] [blame] | 1172 | if (usgn) { |
| 1173 | Ops[0] = Builder.CreateZExt(Ops[0], Ty); |
| 1174 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1175 | } else { |
| 1176 | Ops[0] = Builder.CreateSExt(Ops[0], Ty); |
| 1177 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1178 | } |
| 1179 | return Builder.CreateAdd(Ops[0], Ops[1], "vaddl"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1180 | } |
| 1181 | case ARM::BI__builtin_neon_vaddw_v: { |
| 1182 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1183 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1184 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | 54160c6 | 2010-08-29 05:14:28 +0000 | [diff] [blame] | 1185 | if (usgn) |
| 1186 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1187 | else |
| 1188 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1189 | return Builder.CreateAdd(Ops[0], Ops[1], "vaddw"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1190 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1191 | case ARM::BI__builtin_neon_vcale_v: |
| 1192 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1193 | case ARM::BI__builtin_neon_vcage_v: { |
| 1194 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacged, &Ty, 1); |
| 1195 | return EmitNeonCall(F, Ops, "vcage"); |
| 1196 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1197 | case ARM::BI__builtin_neon_vcaleq_v: |
| 1198 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1199 | case ARM::BI__builtin_neon_vcageq_v: { |
| 1200 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgeq, &Ty, 1); |
| 1201 | return EmitNeonCall(F, Ops, "vcage"); |
| 1202 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1203 | case ARM::BI__builtin_neon_vcalt_v: |
| 1204 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1205 | case ARM::BI__builtin_neon_vcagt_v: { |
| 1206 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtd, &Ty, 1); |
| 1207 | return EmitNeonCall(F, Ops, "vcagt"); |
| 1208 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1209 | case ARM::BI__builtin_neon_vcaltq_v: |
| 1210 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1211 | case ARM::BI__builtin_neon_vcagtq_v: { |
| 1212 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtq, &Ty, 1); |
| 1213 | return EmitNeonCall(F, Ops, "vcagt"); |
| 1214 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1215 | case ARM::BI__builtin_neon_vcls_v: |
| 1216 | case ARM::BI__builtin_neon_vclsq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1217 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcls, &Ty, 1); |
| 1218 | return EmitNeonCall(F, Ops, "vcls"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1219 | } |
| 1220 | case ARM::BI__builtin_neon_vclz_v: |
| 1221 | case ARM::BI__builtin_neon_vclzq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1222 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vclz, &Ty, 1); |
| 1223 | return EmitNeonCall(F, Ops, "vclz"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1224 | } |
| 1225 | case ARM::BI__builtin_neon_vcnt_v: |
| 1226 | case ARM::BI__builtin_neon_vcntq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1227 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcnt, &Ty, 1); |
| 1228 | return EmitNeonCall(F, Ops, "vcnt"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1229 | } |
| 1230 | // FIXME: intrinsics for f16<->f32 convert missing from ARM target. |
| 1231 | case ARM::BI__builtin_neon_vcvt_f32_v: |
| 1232 | case ARM::BI__builtin_neon_vcvtq_f32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1233 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1234 | Ty = GetNeonType(VMContext, 4, quad); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1235 | return usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt") |
| 1236 | : Builder.CreateSIToFP(Ops[0], Ty, "vcvt"); |
| 1237 | } |
| 1238 | case ARM::BI__builtin_neon_vcvt_s32_v: |
| 1239 | case ARM::BI__builtin_neon_vcvt_u32_v: |
| 1240 | case ARM::BI__builtin_neon_vcvtq_s32_v: |
| 1241 | case ARM::BI__builtin_neon_vcvtq_u32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1242 | Ops[0] = Builder.CreateBitCast(Ops[0], GetNeonType(VMContext, 4, quad)); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1243 | return usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt") |
| 1244 | : Builder.CreateFPToSI(Ops[0], Ty, "vcvt"); |
| 1245 | } |
| 1246 | case ARM::BI__builtin_neon_vcvt_n_f32_v: |
| 1247 | case ARM::BI__builtin_neon_vcvtq_n_f32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1248 | const llvm::Type *Tys[2] = { GetNeonType(VMContext, 4, quad), Ty }; |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1249 | Int = usgn ? Intrinsic::arm_neon_vcvtfxu2fp : Intrinsic::arm_neon_vcvtfxs2fp; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1250 | Function *F = CGM.getIntrinsic(Int, Tys, 2); |
| 1251 | return EmitNeonCall(F, Ops, "vcvt_n"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1252 | } |
| 1253 | case ARM::BI__builtin_neon_vcvt_n_s32_v: |
| 1254 | case ARM::BI__builtin_neon_vcvt_n_u32_v: |
| 1255 | case ARM::BI__builtin_neon_vcvtq_n_s32_v: |
| 1256 | case ARM::BI__builtin_neon_vcvtq_n_u32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1257 | const llvm::Type *Tys[2] = { Ty, GetNeonType(VMContext, 4, quad) }; |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1258 | Int = usgn ? Intrinsic::arm_neon_vcvtfp2fxu : Intrinsic::arm_neon_vcvtfp2fxs; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1259 | Function *F = CGM.getIntrinsic(Int, Tys, 2); |
| 1260 | return EmitNeonCall(F, Ops, "vcvt_n"); |
| 1261 | } |
Nate Begeman | cd480ad | 2010-08-06 01:24:57 +0000 | [diff] [blame] | 1262 | case ARM::BI__builtin_neon_vdup_lane_v: |
| 1263 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1264 | return EmitNeonSplat(Ops[0], cast<Constant>(Ops[1])); |
| 1265 | case ARM::BI__builtin_neon_vdupq_lane_v: |
| 1266 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1267 | return EmitNeonSplat(Ops[0], cast<Constant>(Ops[1]), true); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1268 | case ARM::BI__builtin_neon_vext_v: |
| 1269 | case ARM::BI__builtin_neon_vextq_v: { |
| 1270 | ConstantInt *C = dyn_cast<ConstantInt>(Ops[2]); |
| 1271 | int CV = C->getSExtValue(); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1272 | SmallVector<Constant*, 16> Indices; |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1273 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1274 | Indices.push_back(ConstantInt::get(Int32Ty, i+CV)); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1275 | |
| 1276 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1277 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1278 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1279 | return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vext"); |
| 1280 | } |
Nate Begeman | 95450f6 | 2010-06-09 05:30:26 +0000 | [diff] [blame] | 1281 | case ARM::BI__builtin_neon_vget_lane_i8: |
| 1282 | case ARM::BI__builtin_neon_vget_lane_i16: |
| 1283 | case ARM::BI__builtin_neon_vget_lane_i32: |
| 1284 | case ARM::BI__builtin_neon_vget_lane_i64: |
| 1285 | case ARM::BI__builtin_neon_vget_lane_f32: |
| 1286 | case ARM::BI__builtin_neon_vgetq_lane_i8: |
| 1287 | case ARM::BI__builtin_neon_vgetq_lane_i16: |
| 1288 | case ARM::BI__builtin_neon_vgetq_lane_i32: |
| 1289 | case ARM::BI__builtin_neon_vgetq_lane_i64: |
| 1290 | case ARM::BI__builtin_neon_vgetq_lane_f32: |
Nate Begeman | df98e1d | 2010-06-09 18:04:15 +0000 | [diff] [blame] | 1291 | return Builder.CreateExtractElement(Ops[0], EmitScalarExpr(E->getArg(1)), |
| 1292 | "vget_lane"); |
| 1293 | case ARM::BI__builtin_neon_vhadd_v: |
| 1294 | case ARM::BI__builtin_neon_vhaddq_v: |
| 1295 | Int = usgn ? Intrinsic::arm_neon_vhaddu : Intrinsic::arm_neon_vhadds; |
| 1296 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhadd"); |
| 1297 | case ARM::BI__builtin_neon_vhsub_v: |
| 1298 | case ARM::BI__builtin_neon_vhsubq_v: |
| 1299 | Int = usgn ? Intrinsic::arm_neon_vhsubu : Intrinsic::arm_neon_vhsubs; |
| 1300 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhsub"); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1301 | case ARM::BI__builtin_neon_vld1_v: |
| 1302 | case ARM::BI__builtin_neon_vld1q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1303 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1304 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vld1, &Ty, 1), |
| 1305 | Ops, "vld1"); |
| 1306 | case ARM::BI__builtin_neon_vld1_lane_v: |
| 1307 | case ARM::BI__builtin_neon_vld1q_lane_v: |
| 1308 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1309 | Ty = llvm::PointerType::getUnqual(VTy->getElementType()); |
| 1310 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1311 | Ops[0] = Builder.CreateLoad(Ops[0]); |
| 1312 | return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vld1_lane"); |
| 1313 | case ARM::BI__builtin_neon_vld1_dup_v: |
| 1314 | case ARM::BI__builtin_neon_vld1q_dup_v: { |
| 1315 | Value *V = UndefValue::get(Ty); |
| 1316 | Ty = llvm::PointerType::getUnqual(VTy->getElementType()); |
| 1317 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1318 | Ops[0] = Builder.CreateLoad(Ops[0]); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1319 | llvm::Constant *CI = ConstantInt::get(Int32Ty, 0); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1320 | Ops[0] = Builder.CreateInsertElement(V, Ops[0], CI); |
| 1321 | return EmitNeonSplat(Ops[0], CI); |
| 1322 | } |
| 1323 | case ARM::BI__builtin_neon_vld2_v: |
| 1324 | case ARM::BI__builtin_neon_vld2q_v: { |
| 1325 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2, &Ty, 1); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1326 | Value *Align = GetPointeeAlignment(*this, E->getArg(1)); |
| 1327 | Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld2"); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1328 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1329 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1330 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1331 | } |
| 1332 | case ARM::BI__builtin_neon_vld3_v: |
| 1333 | case ARM::BI__builtin_neon_vld3q_v: { |
| 1334 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3, &Ty, 1); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1335 | Value *Align = GetPointeeAlignment(*this, E->getArg(1)); |
| 1336 | Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld3"); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1337 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1338 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1339 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1340 | } |
| 1341 | case ARM::BI__builtin_neon_vld4_v: |
| 1342 | case ARM::BI__builtin_neon_vld4q_v: { |
| 1343 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4, &Ty, 1); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1344 | Value *Align = GetPointeeAlignment(*this, E->getArg(1)); |
| 1345 | Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld4"); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1346 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1347 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1348 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1349 | } |
| 1350 | case ARM::BI__builtin_neon_vld2_lane_v: |
| 1351 | case ARM::BI__builtin_neon_vld2q_lane_v: { |
| 1352 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2lane, &Ty, 1); |
| 1353 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
| 1354 | Ops[3] = Builder.CreateBitCast(Ops[3], Ty); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1355 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(1))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1356 | Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld2_lane"); |
| 1357 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1358 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1359 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1360 | } |
| 1361 | case ARM::BI__builtin_neon_vld3_lane_v: |
| 1362 | case ARM::BI__builtin_neon_vld3q_lane_v: { |
| 1363 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3lane, &Ty, 1); |
| 1364 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
| 1365 | Ops[3] = Builder.CreateBitCast(Ops[3], Ty); |
| 1366 | Ops[4] = Builder.CreateBitCast(Ops[4], Ty); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1367 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(1))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1368 | Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane"); |
| 1369 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1370 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1371 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1372 | } |
| 1373 | case ARM::BI__builtin_neon_vld4_lane_v: |
| 1374 | case ARM::BI__builtin_neon_vld4q_lane_v: { |
| 1375 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4lane, &Ty, 1); |
| 1376 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
| 1377 | Ops[3] = Builder.CreateBitCast(Ops[3], Ty); |
| 1378 | Ops[4] = Builder.CreateBitCast(Ops[4], Ty); |
| 1379 | Ops[5] = Builder.CreateBitCast(Ops[5], Ty); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1380 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(1))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1381 | Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane"); |
| 1382 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1383 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1384 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1385 | } |
| 1386 | case ARM::BI__builtin_neon_vld2_dup_v: |
| 1387 | case ARM::BI__builtin_neon_vld3_dup_v: |
| 1388 | case ARM::BI__builtin_neon_vld4_dup_v: { |
| 1389 | switch (BuiltinID) { |
| 1390 | case ARM::BI__builtin_neon_vld2_dup_v: |
| 1391 | Int = Intrinsic::arm_neon_vld2lane; |
| 1392 | break; |
| 1393 | case ARM::BI__builtin_neon_vld3_dup_v: |
| 1394 | Int = Intrinsic::arm_neon_vld2lane; |
| 1395 | break; |
| 1396 | case ARM::BI__builtin_neon_vld4_dup_v: |
| 1397 | Int = Intrinsic::arm_neon_vld2lane; |
| 1398 | break; |
| 1399 | default: assert(0 && "unknown vld_dup intrinsic?"); |
| 1400 | } |
| 1401 | Function *F = CGM.getIntrinsic(Int, &Ty, 1); |
| 1402 | const llvm::StructType *STy = cast<llvm::StructType>(F->getReturnType()); |
| 1403 | |
| 1404 | SmallVector<Value*, 6> Args; |
| 1405 | Args.push_back(Ops[1]); |
| 1406 | Args.append(STy->getNumElements(), UndefValue::get(Ty)); |
| 1407 | |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1408 | llvm::Constant *CI = ConstantInt::get(Int32Ty, 0); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1409 | Args.push_back(CI); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1410 | Args.push_back(GetPointeeAlignment(*this, E->getArg(1))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1411 | |
| 1412 | Ops[1] = Builder.CreateCall(F, Args.begin(), Args.end(), "vld_dup"); |
| 1413 | // splat lane 0 to all elts in each vector of the result. |
| 1414 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1415 | Value *Val = Builder.CreateExtractValue(Ops[1], i); |
| 1416 | Value *Elt = Builder.CreateBitCast(Val, Ty); |
| 1417 | Elt = EmitNeonSplat(Elt, CI); |
| 1418 | Elt = Builder.CreateBitCast(Elt, Val->getType()); |
| 1419 | Ops[1] = Builder.CreateInsertValue(Ops[1], Elt, i); |
| 1420 | } |
| 1421 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1422 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1423 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1424 | } |
Nate Begeman | df98e1d | 2010-06-09 18:04:15 +0000 | [diff] [blame] | 1425 | case ARM::BI__builtin_neon_vmax_v: |
| 1426 | case ARM::BI__builtin_neon_vmaxq_v: |
| 1427 | Int = usgn ? Intrinsic::arm_neon_vmaxu : Intrinsic::arm_neon_vmaxs; |
| 1428 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmax"); |
| 1429 | case ARM::BI__builtin_neon_vmin_v: |
| 1430 | case ARM::BI__builtin_neon_vminq_v: |
| 1431 | Int = usgn ? Intrinsic::arm_neon_vminu : Intrinsic::arm_neon_vmins; |
| 1432 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmin"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1433 | case ARM::BI__builtin_neon_vmlal_lane_v: { |
| 1434 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1435 | Ops[2] = Builder.CreateBitCast(Ops[2], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1436 | Ops[2] = EmitNeonSplat(Ops[2], cast<Constant>(Ops[3])); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1437 | } |
| 1438 | case ARM::BI__builtin_neon_vmlal_v: { |
| 1439 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1440 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1441 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
| 1442 | Ops[2] = Builder.CreateBitCast(Ops[2], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1443 | if (usgn) { |
| 1444 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1445 | Ops[2] = Builder.CreateZExt(Ops[2], Ty); |
| 1446 | } else { |
| 1447 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1448 | Ops[2] = Builder.CreateSExt(Ops[2], Ty); |
| 1449 | } |
| 1450 | Ops[1] = Builder.CreateMul(Ops[1], Ops[2]); |
| 1451 | return Builder.CreateAdd(Ops[0], Ops[1], "vmlal"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1452 | } |
| 1453 | case ARM::BI__builtin_neon_vmlsl_lane_v: { |
| 1454 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1455 | Ops[2] = Builder.CreateBitCast(Ops[2], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1456 | Ops[2] = EmitNeonSplat(Ops[2], cast<Constant>(Ops[3])); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1457 | } |
| 1458 | case ARM::BI__builtin_neon_vmlsl_v: { |
| 1459 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1460 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1461 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
| 1462 | Ops[2] = Builder.CreateBitCast(Ops[2], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1463 | if (usgn) { |
| 1464 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1465 | Ops[2] = Builder.CreateZExt(Ops[2], Ty); |
| 1466 | } else { |
| 1467 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1468 | Ops[2] = Builder.CreateSExt(Ops[2], Ty); |
| 1469 | } |
| 1470 | Ops[1] = Builder.CreateMul(Ops[1], Ops[2]); |
| 1471 | return Builder.CreateSub(Ops[0], Ops[1], "vmlsl"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1472 | } |
| 1473 | case ARM::BI__builtin_neon_vmovl_v: { |
| 1474 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1475 | Ops[0] = Builder.CreateBitCast(Ops[0], DTy); |
Bob Wilson | 7cea322 | 2010-08-20 03:36:08 +0000 | [diff] [blame] | 1476 | if (usgn) |
| 1477 | return Builder.CreateZExt(Ops[0], Ty, "vmovl"); |
| 1478 | return Builder.CreateSExt(Ops[0], Ty, "vmovl"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1479 | } |
| 1480 | case ARM::BI__builtin_neon_vmovn_v: { |
| 1481 | const llvm::Type *QTy = llvm::VectorType::getExtendedElementVectorType(VTy); |
| 1482 | Ops[0] = Builder.CreateBitCast(Ops[0], QTy); |
Bob Wilson | 3b6081b | 2010-08-30 19:57:13 +0000 | [diff] [blame] | 1483 | return Builder.CreateTrunc(Ops[0], Ty, "vmovn"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1484 | } |
| 1485 | case ARM::BI__builtin_neon_vmull_lane_v: { |
| 1486 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1487 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1488 | Ops[1] = EmitNeonSplat(Ops[1], cast<Constant>(Ops[2])); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1489 | } |
| 1490 | case ARM::BI__builtin_neon_vmull_v: { |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1491 | if (poly) |
| 1492 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vmullp, &Ty, 1), |
| 1493 | Ops, "vmull"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1494 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1495 | Ops[0] = Builder.CreateBitCast(Ops[0], DTy); |
| 1496 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1497 | if (usgn) { |
| 1498 | Ops[0] = Builder.CreateZExt(Ops[0], Ty); |
| 1499 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1500 | } else { |
| 1501 | Ops[0] = Builder.CreateSExt(Ops[0], Ty); |
| 1502 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1503 | } |
| 1504 | return Builder.CreateMul(Ops[0], Ops[1], "vmull"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1505 | } |
Nate Begeman | df98e1d | 2010-06-09 18:04:15 +0000 | [diff] [blame] | 1506 | case ARM::BI__builtin_neon_vpadal_v: |
| 1507 | case ARM::BI__builtin_neon_vpadalq_v: |
| 1508 | Int = usgn ? Intrinsic::arm_neon_vpadalu : Intrinsic::arm_neon_vpadals; |
| 1509 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpadal"); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1510 | case ARM::BI__builtin_neon_vpadd_v: |
| 1511 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vpadd, &Ty, 1), |
| 1512 | Ops, "vpadd"); |
| 1513 | case ARM::BI__builtin_neon_vpaddl_v: |
| 1514 | case ARM::BI__builtin_neon_vpaddlq_v: |
| 1515 | Int = usgn ? Intrinsic::arm_neon_vpaddlu : Intrinsic::arm_neon_vpaddls; |
| 1516 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpaddl"); |
| 1517 | case ARM::BI__builtin_neon_vpmax_v: |
| 1518 | Int = usgn ? Intrinsic::arm_neon_vpmaxu : Intrinsic::arm_neon_vpmaxs; |
| 1519 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmax"); |
| 1520 | case ARM::BI__builtin_neon_vpmin_v: |
| 1521 | Int = usgn ? Intrinsic::arm_neon_vpminu : Intrinsic::arm_neon_vpmins; |
| 1522 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmin"); |
| 1523 | case ARM::BI__builtin_neon_vqabs_v: |
| 1524 | case ARM::BI__builtin_neon_vqabsq_v: |
| 1525 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqabs, &Ty, 1), |
| 1526 | Ops, "vqabs"); |
| 1527 | case ARM::BI__builtin_neon_vqadd_v: |
| 1528 | case ARM::BI__builtin_neon_vqaddq_v: |
| 1529 | Int = usgn ? Intrinsic::arm_neon_vqaddu : Intrinsic::arm_neon_vqadds; |
| 1530 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqadd"); |
| 1531 | case ARM::BI__builtin_neon_vqdmlal_lane_v: |
| 1532 | splat = true; |
| 1533 | case ARM::BI__builtin_neon_vqdmlal_v: |
| 1534 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlal, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1535 | Ops, "vqdmlal", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1536 | case ARM::BI__builtin_neon_vqdmlsl_lane_v: |
| 1537 | splat = true; |
| 1538 | case ARM::BI__builtin_neon_vqdmlsl_v: |
| 1539 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlsl, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1540 | Ops, "vqdmlsl", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1541 | case ARM::BI__builtin_neon_vqdmulh_lane_v: |
| 1542 | case ARM::BI__builtin_neon_vqdmulhq_lane_v: |
| 1543 | splat = true; |
| 1544 | case ARM::BI__builtin_neon_vqdmulh_v: |
| 1545 | case ARM::BI__builtin_neon_vqdmulhq_v: |
| 1546 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmulh, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1547 | Ops, "vqdmulh", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1548 | case ARM::BI__builtin_neon_vqdmull_lane_v: |
| 1549 | splat = true; |
| 1550 | case ARM::BI__builtin_neon_vqdmull_v: |
| 1551 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmull, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1552 | Ops, "vqdmull", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1553 | case ARM::BI__builtin_neon_vqmovn_v: |
| 1554 | Int = usgn ? Intrinsic::arm_neon_vqmovnu : Intrinsic::arm_neon_vqmovns; |
| 1555 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqmovn"); |
| 1556 | case ARM::BI__builtin_neon_vqmovun_v: |
| 1557 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqmovnsu, &Ty, 1), |
| 1558 | Ops, "vqdmull"); |
| 1559 | case ARM::BI__builtin_neon_vqneg_v: |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1560 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqneg, &Ty, 1), |
| 1561 | Ops, "vqneg"); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1562 | case ARM::BI__builtin_neon_vqrdmulh_lane_v: |
| 1563 | case ARM::BI__builtin_neon_vqrdmulhq_lane_v: |
| 1564 | splat = true; |
| 1565 | case ARM::BI__builtin_neon_vqrdmulh_v: |
| 1566 | case ARM::BI__builtin_neon_vqrdmulhq_v: |
| 1567 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrdmulh, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1568 | Ops, "vqrdmulh", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1569 | case ARM::BI__builtin_neon_vqrshl_v: |
| 1570 | case ARM::BI__builtin_neon_vqrshlq_v: |
| 1571 | Int = usgn ? Intrinsic::arm_neon_vqrshiftu : Intrinsic::arm_neon_vqrshifts; |
| 1572 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshl"); |
| 1573 | case ARM::BI__builtin_neon_vqrshrn_n_v: |
| 1574 | Int = usgn ? Intrinsic::arm_neon_vqrshiftnu : Intrinsic::arm_neon_vqrshiftns; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1575 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshrn_n", false, |
| 1576 | 1, true); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1577 | case ARM::BI__builtin_neon_vqrshrun_n_v: |
| 1578 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrshiftnsu, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1579 | Ops, "vqrshrun_n", false, 1, true); |
| 1580 | case ARM::BI__builtin_neon_vqshl_v: |
| 1581 | case ARM::BI__builtin_neon_vqshlq_v: |
| 1582 | Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts; |
| 1583 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl"); |
| 1584 | case ARM::BI__builtin_neon_vqshl_n_v: |
| 1585 | case ARM::BI__builtin_neon_vqshlq_n_v: |
| 1586 | Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts; |
| 1587 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl_n", false, |
| 1588 | 1, false); |
| 1589 | case ARM::BI__builtin_neon_vqshlu_n_v: |
| 1590 | case ARM::BI__builtin_neon_vqshluq_n_v: |
| 1591 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftsu, &Ty, 1), |
| 1592 | Ops, "vqshlu", 1, false); |
| 1593 | case ARM::BI__builtin_neon_vqshrn_n_v: |
| 1594 | Int = usgn ? Intrinsic::arm_neon_vqshiftnu : Intrinsic::arm_neon_vqshiftns; |
| 1595 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshrn_n", false, |
| 1596 | 1, true); |
| 1597 | case ARM::BI__builtin_neon_vqshrun_n_v: |
| 1598 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftnsu, &Ty, 1), |
| 1599 | Ops, "vqshrun_n", false, 1, true); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1600 | case ARM::BI__builtin_neon_vqsub_v: |
| 1601 | case ARM::BI__builtin_neon_vqsubq_v: |
| 1602 | Int = usgn ? Intrinsic::arm_neon_vqsubu : Intrinsic::arm_neon_vqsubs; |
| 1603 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqsub"); |
| 1604 | case ARM::BI__builtin_neon_vraddhn_v: |
| 1605 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vraddhn, &Ty, 1), |
| 1606 | Ops, "vraddhn"); |
| 1607 | case ARM::BI__builtin_neon_vrecpe_v: |
| 1608 | case ARM::BI__builtin_neon_vrecpeq_v: |
| 1609 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecpe, &Ty, 1), |
| 1610 | Ops, "vrecpe"); |
| 1611 | case ARM::BI__builtin_neon_vrecps_v: |
| 1612 | case ARM::BI__builtin_neon_vrecpsq_v: |
| 1613 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecps, &Ty, 1), |
| 1614 | Ops, "vrecps"); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1615 | case ARM::BI__builtin_neon_vrhadd_v: |
| 1616 | case ARM::BI__builtin_neon_vrhaddq_v: |
| 1617 | Int = usgn ? Intrinsic::arm_neon_vrhaddu : Intrinsic::arm_neon_vrhadds; |
| 1618 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrhadd"); |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1619 | case ARM::BI__builtin_neon_vrshl_v: |
| 1620 | case ARM::BI__builtin_neon_vrshlq_v: |
| 1621 | Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts; |
| 1622 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshl"); |
| 1623 | case ARM::BI__builtin_neon_vrshrn_n_v: |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1624 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrshiftn, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1625 | Ops, "vrshrn_n", false, 1, true); |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1626 | case ARM::BI__builtin_neon_vrshr_n_v: |
| 1627 | case ARM::BI__builtin_neon_vrshrq_n_v: |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1628 | Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1629 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshr_n", false, |
| 1630 | 1, true); |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1631 | case ARM::BI__builtin_neon_vrsqrte_v: |
| 1632 | case ARM::BI__builtin_neon_vrsqrteq_v: |
| 1633 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrte, &Ty, 1), |
| 1634 | Ops, "vrsqrte"); |
| 1635 | case ARM::BI__builtin_neon_vrsqrts_v: |
| 1636 | case ARM::BI__builtin_neon_vrsqrtsq_v: |
| 1637 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrts, &Ty, 1), |
| 1638 | Ops, "vrsqrts"); |
| 1639 | case ARM::BI__builtin_neon_vrsra_n_v: |
| 1640 | case ARM::BI__builtin_neon_vrsraq_n_v: |
| 1641 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1642 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1643 | Ops[2] = EmitNeonShiftVector(Ops[2], Ty, true); |
| 1644 | Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts; |
| 1645 | Ops[1] = Builder.CreateCall2(CGM.getIntrinsic(Int, &Ty, 1), Ops[1], Ops[2]); |
| 1646 | return Builder.CreateAdd(Ops[0], Ops[1], "vrsra_n"); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1647 | case ARM::BI__builtin_neon_vrsubhn_v: |
| 1648 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsubhn, &Ty, 1), |
| 1649 | Ops, "vrsubhn"); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1650 | case ARM::BI__builtin_neon_vset_lane_i8: |
| 1651 | case ARM::BI__builtin_neon_vset_lane_i16: |
| 1652 | case ARM::BI__builtin_neon_vset_lane_i32: |
| 1653 | case ARM::BI__builtin_neon_vset_lane_i64: |
| 1654 | case ARM::BI__builtin_neon_vset_lane_f32: |
| 1655 | case ARM::BI__builtin_neon_vsetq_lane_i8: |
| 1656 | case ARM::BI__builtin_neon_vsetq_lane_i16: |
| 1657 | case ARM::BI__builtin_neon_vsetq_lane_i32: |
| 1658 | case ARM::BI__builtin_neon_vsetq_lane_i64: |
| 1659 | case ARM::BI__builtin_neon_vsetq_lane_f32: |
| 1660 | Ops.push_back(EmitScalarExpr(E->getArg(2))); |
| 1661 | return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vset_lane"); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1662 | case ARM::BI__builtin_neon_vshl_v: |
| 1663 | case ARM::BI__builtin_neon_vshlq_v: |
| 1664 | Int = usgn ? Intrinsic::arm_neon_vshiftu : Intrinsic::arm_neon_vshifts; |
| 1665 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshl"); |
| 1666 | case ARM::BI__builtin_neon_vshll_n_v: |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1667 | Int = usgn ? Intrinsic::arm_neon_vshiftlu : Intrinsic::arm_neon_vshiftls; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1668 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshll", false, 1); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1669 | case ARM::BI__builtin_neon_vshl_n_v: |
| 1670 | case ARM::BI__builtin_neon_vshlq_n_v: |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1671 | Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false); |
| 1672 | return Builder.CreateShl(Builder.CreateBitCast(Ops[0],Ty), Ops[1], "vshl_n"); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1673 | case ARM::BI__builtin_neon_vshrn_n_v: |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1674 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftn, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1675 | Ops, "vshrn_n", false, 1, true); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1676 | case ARM::BI__builtin_neon_vshr_n_v: |
| 1677 | case ARM::BI__builtin_neon_vshrq_n_v: |
| 1678 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1679 | Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1680 | if (usgn) |
| 1681 | return Builder.CreateLShr(Ops[0], Ops[1], "vshr_n"); |
| 1682 | else |
| 1683 | return Builder.CreateAShr(Ops[0], Ops[1], "vshr_n"); |
| 1684 | case ARM::BI__builtin_neon_vsri_n_v: |
| 1685 | case ARM::BI__builtin_neon_vsriq_n_v: |
| 1686 | poly = true; |
| 1687 | case ARM::BI__builtin_neon_vsli_n_v: |
| 1688 | case ARM::BI__builtin_neon_vsliq_n_v: |
| 1689 | Ops[2] = EmitNeonShiftVector(Ops[2], Ty, poly); |
| 1690 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftins, &Ty, 1), |
| 1691 | Ops, "vsli_n"); |
| 1692 | case ARM::BI__builtin_neon_vsra_n_v: |
| 1693 | case ARM::BI__builtin_neon_vsraq_n_v: |
| 1694 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1695 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1696 | Ops[2] = EmitNeonShiftVector(Ops[2], Ty, false); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1697 | if (usgn) |
| 1698 | Ops[1] = Builder.CreateLShr(Ops[1], Ops[2], "vsra_n"); |
| 1699 | else |
| 1700 | Ops[1] = Builder.CreateAShr(Ops[1], Ops[2], "vsra_n"); |
| 1701 | return Builder.CreateAdd(Ops[0], Ops[1]); |
| 1702 | case ARM::BI__builtin_neon_vst1_v: |
| 1703 | case ARM::BI__builtin_neon_vst1q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1704 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1705 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst1, &Ty, 1), |
| 1706 | Ops, ""); |
| 1707 | case ARM::BI__builtin_neon_vst1_lane_v: |
| 1708 | case ARM::BI__builtin_neon_vst1q_lane_v: |
| 1709 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1710 | Ops[1] = Builder.CreateExtractElement(Ops[1], Ops[2]); |
| 1711 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1712 | return Builder.CreateStore(Ops[1], Builder.CreateBitCast(Ops[0], Ty)); |
| 1713 | case ARM::BI__builtin_neon_vst2_v: |
| 1714 | case ARM::BI__builtin_neon_vst2q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1715 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1716 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2, &Ty, 1), |
| 1717 | Ops, ""); |
| 1718 | case ARM::BI__builtin_neon_vst2_lane_v: |
| 1719 | case ARM::BI__builtin_neon_vst2q_lane_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1720 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1721 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2lane, &Ty, 1), |
| 1722 | Ops, ""); |
| 1723 | case ARM::BI__builtin_neon_vst3_v: |
| 1724 | case ARM::BI__builtin_neon_vst3q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1725 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1726 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3, &Ty, 1), |
| 1727 | Ops, ""); |
| 1728 | case ARM::BI__builtin_neon_vst3_lane_v: |
| 1729 | case ARM::BI__builtin_neon_vst3q_lane_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1730 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1731 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3lane, &Ty, 1), |
| 1732 | Ops, ""); |
| 1733 | case ARM::BI__builtin_neon_vst4_v: |
| 1734 | case ARM::BI__builtin_neon_vst4q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1735 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1736 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4, &Ty, 1), |
| 1737 | Ops, ""); |
| 1738 | case ARM::BI__builtin_neon_vst4_lane_v: |
| 1739 | case ARM::BI__builtin_neon_vst4q_lane_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1740 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1741 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4lane, &Ty, 1), |
| 1742 | Ops, ""); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1743 | case ARM::BI__builtin_neon_vsubhn_v: |
| 1744 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vsubhn, &Ty, 1), |
| 1745 | Ops, "vsubhn"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1746 | case ARM::BI__builtin_neon_vsubl_v: { |
| 1747 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1748 | Ops[0] = Builder.CreateBitCast(Ops[0], DTy); |
| 1749 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | 54160c6 | 2010-08-29 05:14:28 +0000 | [diff] [blame] | 1750 | if (usgn) { |
| 1751 | Ops[0] = Builder.CreateZExt(Ops[0], Ty); |
| 1752 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1753 | } else { |
| 1754 | Ops[0] = Builder.CreateSExt(Ops[0], Ty); |
| 1755 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1756 | } |
| 1757 | return Builder.CreateSub(Ops[0], Ops[1], "vsubl"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1758 | } |
| 1759 | case ARM::BI__builtin_neon_vsubw_v: { |
| 1760 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1761 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1762 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | 54160c6 | 2010-08-29 05:14:28 +0000 | [diff] [blame] | 1763 | if (usgn) |
| 1764 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1765 | else |
| 1766 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1767 | return Builder.CreateSub(Ops[0], Ops[1], "vsubw"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1768 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1769 | case ARM::BI__builtin_neon_vtbl1_v: |
| 1770 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl1), |
| 1771 | Ops, "vtbl1"); |
| 1772 | case ARM::BI__builtin_neon_vtbl2_v: |
| 1773 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl2), |
| 1774 | Ops, "vtbl2"); |
| 1775 | case ARM::BI__builtin_neon_vtbl3_v: |
| 1776 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl3), |
| 1777 | Ops, "vtbl3"); |
| 1778 | case ARM::BI__builtin_neon_vtbl4_v: |
| 1779 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl4), |
| 1780 | Ops, "vtbl4"); |
| 1781 | case ARM::BI__builtin_neon_vtbx1_v: |
| 1782 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx1), |
| 1783 | Ops, "vtbx1"); |
| 1784 | case ARM::BI__builtin_neon_vtbx2_v: |
| 1785 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx2), |
| 1786 | Ops, "vtbx2"); |
| 1787 | case ARM::BI__builtin_neon_vtbx3_v: |
| 1788 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx3), |
| 1789 | Ops, "vtbx3"); |
| 1790 | case ARM::BI__builtin_neon_vtbx4_v: |
| 1791 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx4), |
| 1792 | Ops, "vtbx4"); |
| 1793 | case ARM::BI__builtin_neon_vtst_v: |
| 1794 | case ARM::BI__builtin_neon_vtstq_v: { |
| 1795 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1796 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1797 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1]); |
| 1798 | Ops[0] = Builder.CreateICmp(ICmpInst::ICMP_NE, Ops[0], |
| 1799 | ConstantAggregateZero::get(Ty)); |
| 1800 | return Builder.CreateSExt(Ops[0], Ty, "vtst"); |
| 1801 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1802 | case ARM::BI__builtin_neon_vtrn_v: |
| 1803 | case ARM::BI__builtin_neon_vtrnq_v: { |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1804 | Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty)); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1805 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1806 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1807 | Value *SV; |
| 1808 | |
| 1809 | for (unsigned vi = 0; vi != 2; ++vi) { |
| 1810 | SmallVector<Constant*, 16> Indices; |
| 1811 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1812 | Indices.push_back(ConstantInt::get(Int32Ty, i+vi)); |
| 1813 | Indices.push_back(ConstantInt::get(Int32Ty, i+e+vi)); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1814 | } |
| 1815 | Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi); |
| 1816 | SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1817 | SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vtrn"); |
| 1818 | SV = Builder.CreateStore(SV, Addr); |
| 1819 | } |
| 1820 | return SV; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1821 | } |
| 1822 | case ARM::BI__builtin_neon_vuzp_v: |
| 1823 | case ARM::BI__builtin_neon_vuzpq_v: { |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1824 | Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty)); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1825 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1826 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1827 | Value *SV; |
| 1828 | |
| 1829 | for (unsigned vi = 0; vi != 2; ++vi) { |
| 1830 | SmallVector<Constant*, 16> Indices; |
| 1831 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1832 | Indices.push_back(ConstantInt::get(Int32Ty, 2*i+vi)); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1833 | |
| 1834 | Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi); |
| 1835 | SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1836 | SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vuzp"); |
| 1837 | SV = Builder.CreateStore(SV, Addr); |
| 1838 | } |
| 1839 | return SV; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1840 | } |
| 1841 | case ARM::BI__builtin_neon_vzip_v: |
| 1842 | case ARM::BI__builtin_neon_vzipq_v: { |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1843 | Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty)); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1844 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1845 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1846 | Value *SV; |
| 1847 | |
| 1848 | for (unsigned vi = 0; vi != 2; ++vi) { |
| 1849 | SmallVector<Constant*, 16> Indices; |
| 1850 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) { |
Daniel Dunbar | e361cc3 | 2010-08-26 00:55:57 +0000 | [diff] [blame] | 1851 | Indices.push_back(ConstantInt::get(Int32Ty, (i + vi*e) >> 1)); |
| 1852 | Indices.push_back(ConstantInt::get(Int32Ty, ((i + vi*e) >> 1)+e)); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1853 | } |
| 1854 | Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi); |
| 1855 | SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1856 | SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vzip"); |
| 1857 | SV = Builder.CreateStore(SV, Addr); |
| 1858 | } |
| 1859 | return SV; |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1860 | } |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 1861 | } |
| 1862 | } |
| 1863 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1864 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 1865 | const CallExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1867 | llvm::SmallVector<Value*, 4> Ops; |
| 1868 | |
| 1869 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 1870 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 1871 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 1872 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 1873 | default: return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1874 | case X86::BI__builtin_ia32_pslldi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1875 | case X86::BI__builtin_ia32_psllqi128: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | case X86::BI__builtin_ia32_psllwi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1877 | case X86::BI__builtin_ia32_psradi128: |
| 1878 | case X86::BI__builtin_ia32_psrawi128: |
| 1879 | case X86::BI__builtin_ia32_psrldi128: |
| 1880 | case X86::BI__builtin_ia32_psrlqi128: |
| 1881 | case X86::BI__builtin_ia32_psrlwi128: { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1882 | Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext"); |
| 1883 | const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 2); |
| 1884 | llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1885 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1886 | Ops[1], Zero, "insert"); |
| 1887 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 1888 | const char *name = 0; |
| 1889 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1891 | switch (BuiltinID) { |
| 1892 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 1893 | case X86::BI__builtin_ia32_pslldi128: |
| 1894 | name = "pslldi"; |
| 1895 | ID = Intrinsic::x86_sse2_psll_d; |
| 1896 | break; |
| 1897 | case X86::BI__builtin_ia32_psllqi128: |
| 1898 | name = "psllqi"; |
| 1899 | ID = Intrinsic::x86_sse2_psll_q; |
| 1900 | break; |
| 1901 | case X86::BI__builtin_ia32_psllwi128: |
| 1902 | name = "psllwi"; |
| 1903 | ID = Intrinsic::x86_sse2_psll_w; |
| 1904 | break; |
| 1905 | case X86::BI__builtin_ia32_psradi128: |
| 1906 | name = "psradi"; |
| 1907 | ID = Intrinsic::x86_sse2_psra_d; |
| 1908 | break; |
| 1909 | case X86::BI__builtin_ia32_psrawi128: |
| 1910 | name = "psrawi"; |
| 1911 | ID = Intrinsic::x86_sse2_psra_w; |
| 1912 | break; |
| 1913 | case X86::BI__builtin_ia32_psrldi128: |
| 1914 | name = "psrldi"; |
| 1915 | ID = Intrinsic::x86_sse2_psrl_d; |
| 1916 | break; |
| 1917 | case X86::BI__builtin_ia32_psrlqi128: |
| 1918 | name = "psrlqi"; |
| 1919 | ID = Intrinsic::x86_sse2_psrl_q; |
| 1920 | break; |
| 1921 | case X86::BI__builtin_ia32_psrlwi128: |
| 1922 | name = "psrlwi"; |
| 1923 | ID = Intrinsic::x86_sse2_psrl_w; |
| 1924 | break; |
| 1925 | } |
| 1926 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1927 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | case X86::BI__builtin_ia32_pslldi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1930 | case X86::BI__builtin_ia32_psllqi: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | case X86::BI__builtin_ia32_psllwi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1932 | case X86::BI__builtin_ia32_psradi: |
| 1933 | case X86::BI__builtin_ia32_psrawi: |
| 1934 | case X86::BI__builtin_ia32_psrldi: |
| 1935 | case X86::BI__builtin_ia32_psrlqi: |
| 1936 | case X86::BI__builtin_ia32_psrlwi: { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1937 | Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext"); |
| 1938 | const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 1); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1939 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1940 | const char *name = 0; |
| 1941 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1943 | switch (BuiltinID) { |
| 1944 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 1945 | case X86::BI__builtin_ia32_pslldi: |
| 1946 | name = "pslldi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1947 | ID = Intrinsic::x86_mmx_pslli_d; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1948 | break; |
| 1949 | case X86::BI__builtin_ia32_psllqi: |
| 1950 | name = "psllqi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1951 | ID = Intrinsic::x86_mmx_pslli_q; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1952 | break; |
| 1953 | case X86::BI__builtin_ia32_psllwi: |
| 1954 | name = "psllwi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1955 | ID = Intrinsic::x86_mmx_pslli_w; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1956 | break; |
| 1957 | case X86::BI__builtin_ia32_psradi: |
| 1958 | name = "psradi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1959 | ID = Intrinsic::x86_mmx_psrai_d; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1960 | break; |
| 1961 | case X86::BI__builtin_ia32_psrawi: |
| 1962 | name = "psrawi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1963 | ID = Intrinsic::x86_mmx_psrai_w; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1964 | break; |
| 1965 | case X86::BI__builtin_ia32_psrldi: |
| 1966 | name = "psrldi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1967 | ID = Intrinsic::x86_mmx_psrli_d; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1968 | break; |
| 1969 | case X86::BI__builtin_ia32_psrlqi: |
| 1970 | name = "psrlqi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1971 | ID = Intrinsic::x86_mmx_psrli_q; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1972 | break; |
| 1973 | case X86::BI__builtin_ia32_psrlwi: |
| 1974 | name = "psrlwi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1975 | ID = Intrinsic::x86_mmx_psrli_w; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1976 | break; |
| 1977 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 1978 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1980 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 1981 | case X86::BI__builtin_ia32_cmpps: { |
| 1982 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 1983 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 1984 | } |
| 1985 | case X86::BI__builtin_ia32_cmpss: { |
| 1986 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 1987 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 1988 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1989 | case X86::BI__builtin_ia32_ldmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1990 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1991 | Value *One = llvm::ConstantInt::get(Int32Ty, 1); |
| 1992 | Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1993 | Builder.CreateStore(Ops[0], Tmp); |
| 1994 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 1995 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1996 | } |
| 1997 | case X86::BI__builtin_ia32_stmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1998 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1999 | Value *One = llvm::ConstantInt::get(Int32Ty, 1); |
| 2000 | Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2001 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 2002 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2003 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 2004 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 2005 | case X86::BI__builtin_ia32_cmppd: { |
| 2006 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 2007 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 2008 | } |
| 2009 | case X86::BI__builtin_ia32_cmpsd: { |
| 2010 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 2011 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 2012 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2013 | case X86::BI__builtin_ia32_storehps: |
| 2014 | case X86::BI__builtin_ia32_storelps: { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2015 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(Int64Ty); |
| 2016 | llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2018 | // cast val v2i64 |
| 2019 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2021 | // extract (0, 1) |
| 2022 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2023 | llvm::Value *Idx = llvm::ConstantInt::get(Int32Ty, Index); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2024 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 2025 | |
| 2026 | // cast pointer to i64 & store |
| 2027 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 2028 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 2029 | } |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 2030 | case X86::BI__builtin_ia32_palignr: { |
Eric Christopher | e57aa9e | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 2031 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 2032 | |
| 2033 | // If palignr is shifting the pair of input vectors less than 9 bytes, |
| 2034 | // emit a shuffle instruction. |
| 2035 | if (shiftVal <= 8) { |
Eric Christopher | e57aa9e | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 2036 | llvm::SmallVector<llvm::Constant*, 8> Indices; |
| 2037 | for (unsigned i = 0; i != 8; ++i) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2038 | Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i)); |
Eric Christopher | e57aa9e | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 2039 | |
| 2040 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 2041 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 2042 | } |
| 2043 | |
| 2044 | // If palignr is shifting the pair of input vectors more than 8 but less |
| 2045 | // than 16 bytes, emit a logical right shift of the destination. |
| 2046 | if (shiftVal < 16) { |
| 2047 | // MMX has these as 1 x i64 vectors for some odd optimization reasons. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2048 | const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 1); |
Eric Christopher | e57aa9e | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 2049 | |
| 2050 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 2051 | Ops[1] = llvm::ConstantInt::get(VecTy, (shiftVal-8) * 8); |
| 2052 | |
| 2053 | // create i32 constant |
| 2054 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_mmx_psrl_q); |
| 2055 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 2056 | } |
| 2057 | |
| 2058 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 2059 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Nate Begeman | c3420ff | 2009-12-14 05:15:02 +0000 | [diff] [blame] | 2060 | } |
| 2061 | case X86::BI__builtin_ia32_palignr128: { |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2062 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 2063 | |
| 2064 | // If palignr is shifting the pair of input vectors less than 17 bytes, |
| 2065 | // emit a shuffle instruction. |
| 2066 | if (shiftVal <= 16) { |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2067 | llvm::SmallVector<llvm::Constant*, 16> Indices; |
| 2068 | for (unsigned i = 0; i != 16; ++i) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2069 | Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i)); |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2070 | |
| 2071 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 2072 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 2073 | } |
| 2074 | |
| 2075 | // If palignr is shifting the pair of input vectors more than 16 but less |
| 2076 | // than 32 bytes, emit a logical right shift of the destination. |
| 2077 | if (shiftVal < 32) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2078 | const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2); |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2079 | |
| 2080 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2081 | Ops[1] = llvm::ConstantInt::get(Int32Ty, (shiftVal-16) * 8); |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2082 | |
| 2083 | // create i32 constant |
| 2084 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq); |
| 2085 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 2086 | } |
| 2087 | |
| 2088 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 2089 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 2090 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 2095 | const CallExpr *E) { |
Chris Lattner | dd17394 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 2096 | llvm::SmallVector<Value*, 4> Ops; |
| 2097 | |
| 2098 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 2099 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 2100 | |
| 2101 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 2102 | |
| 2103 | switch (BuiltinID) { |
| 2104 | default: return 0; |
| 2105 | |
Anton Korobeynikov | 4d3a7b0 | 2010-06-19 09:47:18 +0000 | [diff] [blame] | 2106 | // vec_ld, vec_lvsl, vec_lvsr |
| 2107 | case PPC::BI__builtin_altivec_lvx: |
| 2108 | case PPC::BI__builtin_altivec_lvxl: |
| 2109 | case PPC::BI__builtin_altivec_lvebx: |
| 2110 | case PPC::BI__builtin_altivec_lvehx: |
| 2111 | case PPC::BI__builtin_altivec_lvewx: |
| 2112 | case PPC::BI__builtin_altivec_lvsl: |
| 2113 | case PPC::BI__builtin_altivec_lvsr: |
| 2114 | { |
| 2115 | Ops[1] = Builder.CreateBitCast(Ops[1], llvm::Type::getInt8PtrTy(VMContext)); |
| 2116 | |
| 2117 | Ops[0] = Builder.CreateGEP(Ops[1], Ops[0], "tmp"); |
| 2118 | Ops.pop_back(); |
| 2119 | |
| 2120 | switch (BuiltinID) { |
| 2121 | default: assert(0 && "Unsupported ld/lvsl/lvsr intrinsic!"); |
| 2122 | case PPC::BI__builtin_altivec_lvx: |
| 2123 | ID = Intrinsic::ppc_altivec_lvx; |
| 2124 | break; |
| 2125 | case PPC::BI__builtin_altivec_lvxl: |
| 2126 | ID = Intrinsic::ppc_altivec_lvxl; |
| 2127 | break; |
| 2128 | case PPC::BI__builtin_altivec_lvebx: |
| 2129 | ID = Intrinsic::ppc_altivec_lvebx; |
| 2130 | break; |
| 2131 | case PPC::BI__builtin_altivec_lvehx: |
| 2132 | ID = Intrinsic::ppc_altivec_lvehx; |
| 2133 | break; |
| 2134 | case PPC::BI__builtin_altivec_lvewx: |
| 2135 | ID = Intrinsic::ppc_altivec_lvewx; |
| 2136 | break; |
| 2137 | case PPC::BI__builtin_altivec_lvsl: |
| 2138 | ID = Intrinsic::ppc_altivec_lvsl; |
| 2139 | break; |
| 2140 | case PPC::BI__builtin_altivec_lvsr: |
| 2141 | ID = Intrinsic::ppc_altivec_lvsr; |
| 2142 | break; |
| 2143 | } |
| 2144 | llvm::Function *F = CGM.getIntrinsic(ID); |
| 2145 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), ""); |
| 2146 | } |
| 2147 | |
Chris Lattner | dd17394 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 2148 | // vec_st |
| 2149 | case PPC::BI__builtin_altivec_stvx: |
| 2150 | case PPC::BI__builtin_altivec_stvxl: |
| 2151 | case PPC::BI__builtin_altivec_stvebx: |
| 2152 | case PPC::BI__builtin_altivec_stvehx: |
| 2153 | case PPC::BI__builtin_altivec_stvewx: |
| 2154 | { |
| 2155 | Ops[2] = Builder.CreateBitCast(Ops[2], llvm::Type::getInt8PtrTy(VMContext)); |
Anton Korobeynikov | 4d3a7b0 | 2010-06-19 09:47:18 +0000 | [diff] [blame] | 2156 | Ops[1] = Builder.CreateGEP(Ops[2], Ops[1], "tmp"); |
Chris Lattner | dd17394 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 2157 | Ops.pop_back(); |
| 2158 | |
| 2159 | switch (BuiltinID) { |
Anton Korobeynikov | 4d3a7b0 | 2010-06-19 09:47:18 +0000 | [diff] [blame] | 2160 | default: assert(0 && "Unsupported st intrinsic!"); |
Chris Lattner | dd17394 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 2161 | case PPC::BI__builtin_altivec_stvx: |
| 2162 | ID = Intrinsic::ppc_altivec_stvx; |
| 2163 | break; |
| 2164 | case PPC::BI__builtin_altivec_stvxl: |
| 2165 | ID = Intrinsic::ppc_altivec_stvxl; |
| 2166 | break; |
| 2167 | case PPC::BI__builtin_altivec_stvebx: |
| 2168 | ID = Intrinsic::ppc_altivec_stvebx; |
| 2169 | break; |
| 2170 | case PPC::BI__builtin_altivec_stvehx: |
| 2171 | ID = Intrinsic::ppc_altivec_stvehx; |
| 2172 | break; |
| 2173 | case PPC::BI__builtin_altivec_stvewx: |
| 2174 | ID = Intrinsic::ppc_altivec_stvewx; |
| 2175 | break; |
| 2176 | } |
| 2177 | llvm::Function *F = CGM.getIntrinsic(ID); |
| 2178 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), ""); |
| 2179 | } |
| 2180 | } |
Daniel Dunbar | b0b8438 | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 2181 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2182 | } |