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, |
Chris Lattner | 780a2eb | 2010-09-21 23:35:30 +0000 | [diff] [blame^] | 38 | StoreStore ? True : False, |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 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: { |
Chris Lattner | 780a2eb | 2010-09-21 23:35:30 +0000 | [diff] [blame^] | 791 | llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0)); |
| 792 | unsigned AddrSpace = |
| 793 | cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();; |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 794 | const llvm::Type *ValueType = |
| 795 | llvm::IntegerType::get(CGF.getLLVMContext(), |
| 796 | CGF.getContext().getTypeSize(E->getType())); |
Chris Lattner | 780a2eb | 2010-09-21 23:35:30 +0000 | [diff] [blame^] | 797 | const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace); |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 798 | const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType }; |
| 799 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, |
| 800 | IntrinsicTypes, 2); |
| 801 | |
Chris Lattner | 780a2eb | 2010-09-21 23:35:30 +0000 | [diff] [blame^] | 802 | Value *Args[3] = { Builder.CreateBitCast(DestPtr, PtrType), |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 803 | EmitCastToInt(CGF, ValueType, |
| 804 | CGF.EmitScalarExpr(E->getArg(1))), |
| 805 | EmitCastToInt(CGF, ValueType, |
| 806 | CGF.EmitScalarExpr(E->getArg(2))) }; |
| 807 | return RValue::get(EmitCastFromInt(CGF, E->getType(), |
| 808 | EmitCallWithBarrier(CGF, AtomF, Args, |
| 809 | Args + 3))); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 810 | } |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 811 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 812 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 813 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 814 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 815 | case Builtin::BI__sync_bool_compare_and_swap_8: |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 816 | case Builtin::BI__sync_bool_compare_and_swap_16: { |
Chris Lattner | f2b9527 | 2010-09-21 23:24:52 +0000 | [diff] [blame] | 817 | llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0)); |
| 818 | unsigned AddrSpace = |
| 819 | cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();; |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 820 | const llvm::Type *ValueType = |
Chris Lattner | f2b9527 | 2010-09-21 23:24:52 +0000 | [diff] [blame] | 821 | llvm::IntegerType::get(CGF.getLLVMContext(), |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 822 | CGF.getContext().getTypeSize(E->getArg(1)->getType())); |
Chris Lattner | f2b9527 | 2010-09-21 23:24:52 +0000 | [diff] [blame] | 823 | const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace); |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 824 | const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType }; |
| 825 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, |
| 826 | IntrinsicTypes, 2); |
| 827 | |
Chris Lattner | f2b9527 | 2010-09-21 23:24:52 +0000 | [diff] [blame] | 828 | Value *Args[3] = { Builder.CreateBitCast(DestPtr, PtrType), |
Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 829 | EmitCastToInt(CGF, ValueType, |
| 830 | CGF.EmitScalarExpr(E->getArg(1))), |
| 831 | EmitCastToInt(CGF, ValueType, |
| 832 | CGF.EmitScalarExpr(E->getArg(2))) }; |
| 833 | Value *OldVal = Args[1]; |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 834 | Value *PrevVal = EmitCallWithBarrier(*this, AtomF, Args, Args + 3); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 835 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 836 | // zext bool to int. |
| 837 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 838 | } |
| 839 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 840 | case Builtin::BI__sync_lock_test_and_set_1: |
| 841 | case Builtin::BI__sync_lock_test_and_set_2: |
| 842 | case Builtin::BI__sync_lock_test_and_set_4: |
| 843 | case Builtin::BI__sync_lock_test_and_set_8: |
| 844 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 845 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 846 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 847 | case Builtin::BI__sync_lock_release_1: |
| 848 | case Builtin::BI__sync_lock_release_2: |
| 849 | case Builtin::BI__sync_lock_release_4: |
| 850 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 851 | case Builtin::BI__sync_lock_release_16: { |
| 852 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 853 | const llvm::Type *ElTy = |
| 854 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Daniel Dunbar | 007b567 | 2009-11-29 21:11:47 +0000 | [diff] [blame] | 855 | llvm::StoreInst *Store = |
| 856 | Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr); |
| 857 | Store->setVolatile(true); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 858 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 859 | } |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 860 | |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 861 | case Builtin::BI__sync_synchronize: { |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 862 | // We assume like gcc appears to, that this only applies to cached memory. |
| 863 | EmitMemoryBarrier(*this, true, true, true, true, false); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 864 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 865 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | |
Tanya Lattner | 0b57164 | 2010-01-16 01:21:14 +0000 | [diff] [blame] | 867 | case Builtin::BI__builtin_llvm_memory_barrier: { |
| 868 | Value *C[5] = { |
| 869 | EmitScalarExpr(E->getArg(0)), |
| 870 | EmitScalarExpr(E->getArg(1)), |
| 871 | EmitScalarExpr(E->getArg(2)), |
| 872 | EmitScalarExpr(E->getArg(3)), |
| 873 | EmitScalarExpr(E->getArg(4)) |
| 874 | }; |
| 875 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
| 876 | return RValue::get(0); |
| 877 | } |
| 878 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 879 | // Library functions with special handling. |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 880 | case Builtin::BIsqrt: |
| 881 | case Builtin::BIsqrtf: |
| 882 | case Builtin::BIsqrtl: { |
John McCall | beb4128 | 2010-04-07 08:20:20 +0000 | [diff] [blame] | 883 | // TODO: there is currently no set of optimizer flags |
| 884 | // sufficient for us to rewrite sqrt to @llvm.sqrt. |
| 885 | // -fmath-errno=0 is not good enough; we need finiteness. |
| 886 | // We could probably precondition the call with an ult |
| 887 | // against 0, but is that worth the complexity? |
| 888 | break; |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | case Builtin::BIpow: |
| 892 | case Builtin::BIpowf: |
| 893 | case Builtin::BIpowl: { |
| 894 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 895 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 896 | break; |
| 897 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 898 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 899 | const llvm::Type *ArgType = Base->getType(); |
| 900 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 901 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 902 | } |
Eli Friedman | ba68b08 | 2010-03-06 02:17:52 +0000 | [diff] [blame] | 903 | |
| 904 | case Builtin::BI__builtin_signbit: |
| 905 | case Builtin::BI__builtin_signbitf: |
| 906 | case Builtin::BI__builtin_signbitl: { |
| 907 | LLVMContext &C = CGM.getLLVMContext(); |
| 908 | |
| 909 | Value *Arg = EmitScalarExpr(E->getArg(0)); |
| 910 | const llvm::Type *ArgTy = Arg->getType(); |
| 911 | if (ArgTy->isPPC_FP128Ty()) |
| 912 | break; // FIXME: I'm not sure what the right implementation is here. |
| 913 | int ArgWidth = ArgTy->getPrimitiveSizeInBits(); |
| 914 | const llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth); |
| 915 | Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy); |
| 916 | Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy); |
| 917 | Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp); |
| 918 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 919 | } |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 920 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 922 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 923 | // that function. |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 924 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 925 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 926 | return EmitCall(E->getCallee()->getType(), |
| 927 | CGM.getBuiltinLibFunction(FD, BuiltinID), |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 928 | ReturnValueSlot(), |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 929 | E->arg_begin(), E->arg_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 931 | // See if we have a target specific intrinsic. |
Dale Johannesen | a6f80ef | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 932 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 933 | Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic; |
| 934 | if (const char *Prefix = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | llvm::Triple::getArchTypePrefix(Target.getTriple().getArch())) |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 936 | IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name); |
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 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 939 | SmallVector<Value*, 16> Args; |
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 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 942 | const llvm::FunctionType *FTy = F->getFunctionType(); |
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 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 945 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 947 | // If the intrinsic arg type is different from the builtin arg type |
| 948 | // we need to do a bit cast. |
| 949 | const llvm::Type *PTy = FTy->getParamType(i); |
| 950 | if (PTy != ArgValue->getType()) { |
| 951 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 952 | "Must be able to losslessly bit cast to param"); |
| 953 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 956 | Args.push_back(ArgValue); |
| 957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 959 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 960 | QualType BuiltinRetType = E->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 962 | const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 963 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 965 | if (RetTy != V->getType()) { |
| 966 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 967 | "Must be able to losslessly bit cast result type"); |
| 968 | V = Builder.CreateBitCast(V, RetTy); |
| 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 | return RValue::get(V); |
| 972 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 974 | // 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] | 975 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 976 | return RValue::get(V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 978 | ErrorUnsupported(E, "builtin function"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 980 | // Unknown builtin, for now just dump it out and return undef. |
| 981 | if (hasAggregateLLVMType(E->getType())) |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 982 | return RValue::getAggregate(CreateMemTemp(E->getType())); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 983 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 985 | |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 986 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 987 | const CallExpr *E) { |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 988 | switch (Target.getTriple().getArch()) { |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 989 | case llvm::Triple::arm: |
| 990 | case llvm::Triple::thumb: |
| 991 | return EmitARMBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 992 | case llvm::Triple::x86: |
| 993 | case llvm::Triple::x86_64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 994 | return EmitX86BuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 995 | case llvm::Triple::ppc: |
| 996 | case llvm::Triple::ppc64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 997 | return EmitPPCBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 998 | default: |
| 999 | return 0; |
| 1000 | } |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1003 | const llvm::VectorType *GetNeonType(LLVMContext &C, unsigned type, bool q) { |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1004 | switch (type) { |
| 1005 | default: break; |
| 1006 | case 0: |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1007 | 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] | 1008 | case 6: |
| 1009 | case 7: |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1010 | case 1: return llvm::VectorType::get(llvm::Type::getInt16Ty(C),4 << (int)q); |
| 1011 | case 2: return llvm::VectorType::get(llvm::Type::getInt32Ty(C),2 << (int)q); |
| 1012 | case 3: return llvm::VectorType::get(llvm::Type::getInt64Ty(C),1 << (int)q); |
| 1013 | 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] | 1014 | }; |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
Nate Begeman | cd480ad | 2010-08-06 01:24:57 +0000 | [diff] [blame] | 1018 | Value *CodeGenFunction::EmitNeonSplat(Value *V, Constant *C, bool widen) { |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1019 | unsigned nElts = cast<llvm::VectorType>(V->getType())->getNumElements(); |
Nate Begeman | cd480ad | 2010-08-06 01:24:57 +0000 | [diff] [blame] | 1020 | if (widen) |
| 1021 | nElts <<= 1; |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1022 | SmallVector<Constant*, 16> Indices(nElts, C); |
| 1023 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1024 | return Builder.CreateShuffleVector(V, V, SV, "lane"); |
| 1025 | } |
| 1026 | |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1027 | Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl<Value*> &Ops, |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1028 | const char *name, bool splat, |
| 1029 | unsigned shift, bool rightshift) { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1030 | unsigned j = 0; |
| 1031 | for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end(); |
| 1032 | ai != ae; ++ai, ++j) |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1033 | if (shift > 0 && shift == j) |
| 1034 | Ops[j] = EmitNeonShiftVector(Ops[j], ai->getType(), rightshift); |
| 1035 | else |
| 1036 | Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1037 | |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1038 | if (splat) { |
| 1039 | Ops[j-1] = EmitNeonSplat(Ops[j-1], cast<Constant>(Ops[j])); |
| 1040 | Ops.resize(j); |
| 1041 | } |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1042 | return Builder.CreateCall(F, Ops.begin(), Ops.end(), name); |
| 1043 | } |
| 1044 | |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1045 | Value *CodeGenFunction::EmitNeonShiftVector(Value *V, const llvm::Type *Ty, |
| 1046 | bool neg) { |
| 1047 | ConstantInt *CI = cast<ConstantInt>(V); |
| 1048 | int SV = CI->getSExtValue(); |
| 1049 | |
| 1050 | const llvm::VectorType *VTy = cast<llvm::VectorType>(Ty); |
| 1051 | llvm::Constant *C = ConstantInt::get(VTy->getElementType(), neg ? -SV : SV); |
| 1052 | SmallVector<llvm::Constant*, 16> CV(VTy->getNumElements(), C); |
| 1053 | return llvm::ConstantVector::get(CV.begin(), CV.size()); |
| 1054 | } |
| 1055 | |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1056 | /// GetPointeeAlignment - Given an expression with a pointer type, find the |
| 1057 | /// alignment of the type referenced by the pointer. Skip over implicit |
| 1058 | /// casts. |
| 1059 | static Value *GetPointeeAlignment(CodeGenFunction &CGF, const Expr *Addr) { |
| 1060 | unsigned Align = 1; |
| 1061 | // Check if the type is a pointer. The implicit cast operand might not be. |
| 1062 | while (Addr->getType()->isPointerType()) { |
| 1063 | QualType PtTy = Addr->getType()->getPointeeType(); |
| 1064 | unsigned NewA = CGF.getContext().getTypeAlignInChars(PtTy).getQuantity(); |
| 1065 | if (NewA > Align) |
| 1066 | Align = NewA; |
| 1067 | |
| 1068 | // If the address is an implicit cast, repeat with the cast operand. |
| 1069 | if (const ImplicitCastExpr *CastAddr = dyn_cast<ImplicitCastExpr>(Addr)) { |
| 1070 | Addr = CastAddr->getSubExpr(); |
| 1071 | continue; |
| 1072 | } |
| 1073 | break; |
| 1074 | } |
| 1075 | return llvm::ConstantInt::get(CGF.Int32Ty, Align); |
| 1076 | } |
| 1077 | |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 1078 | Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, |
| 1079 | const CallExpr *E) { |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1080 | if (BuiltinID == ARM::BI__clear_cache) { |
Rafael Espindola | 79ba509 | 2010-06-07 17:26:50 +0000 | [diff] [blame] | 1081 | const FunctionDecl *FD = E->getDirectCallee(); |
| 1082 | Value *a = EmitScalarExpr(E->getArg(0)); |
| 1083 | Value *b = EmitScalarExpr(E->getArg(1)); |
| 1084 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType()); |
| 1085 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 1086 | llvm::StringRef Name = FD->getName(); |
| 1087 | return Builder.CreateCall2(CGM.CreateRuntimeFunction(FTy, Name), |
| 1088 | a, b); |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 1089 | } |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1090 | |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1091 | llvm::SmallVector<Value*, 4> Ops; |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1092 | for (unsigned i = 0, e = E->getNumArgs() - 1; i != e; i++) |
| 1093 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 1094 | |
| 1095 | llvm::APSInt Result; |
| 1096 | const Expr *Arg = E->getArg(E->getNumArgs()-1); |
| 1097 | if (!Arg->isIntegerConstantExpr(Result, getContext())) |
| 1098 | return 0; |
| 1099 | |
Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 1100 | if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f || |
| 1101 | BuiltinID == ARM::BI__builtin_arm_vcvtr_d) { |
| 1102 | // Determine the overloaded type of this builtin. |
| 1103 | const llvm::Type *Ty; |
| 1104 | if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f) |
| 1105 | Ty = llvm::Type::getFloatTy(VMContext); |
| 1106 | else |
| 1107 | Ty = llvm::Type::getDoubleTy(VMContext); |
| 1108 | |
| 1109 | // Determine whether this is an unsigned conversion or not. |
| 1110 | bool usgn = Result.getZExtValue() == 1; |
| 1111 | unsigned Int = usgn ? Intrinsic::arm_vcvtru : Intrinsic::arm_vcvtr; |
| 1112 | |
| 1113 | // Call the appropriate intrinsic. |
| 1114 | Function *F = CGM.getIntrinsic(Int, &Ty, 1); |
| 1115 | return Builder.CreateCall(F, Ops.begin(), Ops.end(), "vcvtr"); |
| 1116 | } |
| 1117 | |
| 1118 | // Determine the type of this overloaded NEON intrinsic. |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1119 | unsigned type = Result.getZExtValue(); |
| 1120 | bool usgn = type & 0x08; |
| 1121 | bool quad = type & 0x10; |
Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 1122 | bool poly = (type & 0x7) == 5 || (type & 0x7) == 6; |
Nate Begeman | d075c01 | 2010-06-10 00:17:56 +0000 | [diff] [blame] | 1123 | bool splat = false; |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1124 | |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1125 | const llvm::VectorType *VTy = GetNeonType(VMContext, type & 0x7, quad); |
| 1126 | const llvm::Type *Ty = VTy; |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 1127 | if (!Ty) |
| 1128 | return 0; |
| 1129 | |
| 1130 | unsigned Int; |
| 1131 | switch (BuiltinID) { |
| 1132 | default: return 0; |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1133 | case ARM::BI__builtin_neon_vaba_v: |
Bob Wilson | 47a88fa | 2010-09-03 01:27:09 +0000 | [diff] [blame] | 1134 | case ARM::BI__builtin_neon_vabaq_v: { |
| 1135 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1136 | SmallVector<Value*, 2> Args; |
| 1137 | Args.push_back(Ops[1]); |
| 1138 | Args.push_back(Ops[2]); |
| 1139 | Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds; |
| 1140 | Ops[1] = EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Args, "vaba"); |
| 1141 | return Builder.CreateAdd(Ops[0], Ops[1], "vaba"); |
| 1142 | } |
| 1143 | case ARM::BI__builtin_neon_vabal_v: { |
| 1144 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1145 | SmallVector<Value*, 2> Args; |
| 1146 | Args.push_back(Ops[1]); |
| 1147 | Args.push_back(Ops[2]); |
| 1148 | Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds; |
| 1149 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1150 | Ops[1] = EmitNeonCall(CGM.getIntrinsic(Int, &DTy, 1), Args, "vabal"); |
| 1151 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1152 | return Builder.CreateAdd(Ops[0], Ops[1], "vabal"); |
| 1153 | } |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1154 | case ARM::BI__builtin_neon_vabd_v: |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1155 | case ARM::BI__builtin_neon_vabdq_v: |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1156 | Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1157 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabd"); |
Bob Wilson | 47a88fa | 2010-09-03 01:27:09 +0000 | [diff] [blame] | 1158 | case ARM::BI__builtin_neon_vabdl_v: { |
| 1159 | Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds; |
| 1160 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1161 | Ops[0] = EmitNeonCall(CGM.getIntrinsic(Int, &DTy, 1), Ops, "vabdl"); |
| 1162 | return Builder.CreateZExt(Ops[0], Ty, "vabdl"); |
| 1163 | } |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 1164 | case ARM::BI__builtin_neon_vabs_v: |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1165 | case ARM::BI__builtin_neon_vabsq_v: |
| 1166 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vabs, &Ty, 1), |
| 1167 | Ops, "vabs"); |
| 1168 | case ARM::BI__builtin_neon_vaddhn_v: |
| 1169 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vaddhn, &Ty, 1), |
| 1170 | Ops, "vaddhn"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1171 | case ARM::BI__builtin_neon_vaddl_v: { |
| 1172 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1173 | Ops[0] = Builder.CreateBitCast(Ops[0], DTy); |
| 1174 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | 54160c6 | 2010-08-29 05:14:28 +0000 | [diff] [blame] | 1175 | if (usgn) { |
| 1176 | Ops[0] = Builder.CreateZExt(Ops[0], Ty); |
| 1177 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1178 | } else { |
| 1179 | Ops[0] = Builder.CreateSExt(Ops[0], Ty); |
| 1180 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1181 | } |
| 1182 | return Builder.CreateAdd(Ops[0], Ops[1], "vaddl"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1183 | } |
| 1184 | case ARM::BI__builtin_neon_vaddw_v: { |
| 1185 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1186 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1187 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | 54160c6 | 2010-08-29 05:14:28 +0000 | [diff] [blame] | 1188 | if (usgn) |
| 1189 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1190 | else |
| 1191 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1192 | return Builder.CreateAdd(Ops[0], Ops[1], "vaddw"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1193 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1194 | case ARM::BI__builtin_neon_vcale_v: |
| 1195 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1196 | case ARM::BI__builtin_neon_vcage_v: { |
| 1197 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacged, &Ty, 1); |
| 1198 | return EmitNeonCall(F, Ops, "vcage"); |
| 1199 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1200 | case ARM::BI__builtin_neon_vcaleq_v: |
| 1201 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1202 | case ARM::BI__builtin_neon_vcageq_v: { |
| 1203 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgeq, &Ty, 1); |
| 1204 | return EmitNeonCall(F, Ops, "vcage"); |
| 1205 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1206 | case ARM::BI__builtin_neon_vcalt_v: |
| 1207 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1208 | case ARM::BI__builtin_neon_vcagt_v: { |
| 1209 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtd, &Ty, 1); |
| 1210 | return EmitNeonCall(F, Ops, "vcagt"); |
| 1211 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1212 | case ARM::BI__builtin_neon_vcaltq_v: |
| 1213 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1214 | case ARM::BI__builtin_neon_vcagtq_v: { |
| 1215 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtq, &Ty, 1); |
| 1216 | return EmitNeonCall(F, Ops, "vcagt"); |
| 1217 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1218 | case ARM::BI__builtin_neon_vcls_v: |
| 1219 | case ARM::BI__builtin_neon_vclsq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1220 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcls, &Ty, 1); |
| 1221 | return EmitNeonCall(F, Ops, "vcls"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1222 | } |
| 1223 | case ARM::BI__builtin_neon_vclz_v: |
| 1224 | case ARM::BI__builtin_neon_vclzq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1225 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vclz, &Ty, 1); |
| 1226 | return EmitNeonCall(F, Ops, "vclz"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1227 | } |
| 1228 | case ARM::BI__builtin_neon_vcnt_v: |
| 1229 | case ARM::BI__builtin_neon_vcntq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1230 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcnt, &Ty, 1); |
| 1231 | return EmitNeonCall(F, Ops, "vcnt"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1232 | } |
| 1233 | // FIXME: intrinsics for f16<->f32 convert missing from ARM target. |
| 1234 | case ARM::BI__builtin_neon_vcvt_f32_v: |
| 1235 | case ARM::BI__builtin_neon_vcvtq_f32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1236 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1237 | Ty = GetNeonType(VMContext, 4, quad); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1238 | return usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt") |
| 1239 | : Builder.CreateSIToFP(Ops[0], Ty, "vcvt"); |
| 1240 | } |
| 1241 | case ARM::BI__builtin_neon_vcvt_s32_v: |
| 1242 | case ARM::BI__builtin_neon_vcvt_u32_v: |
| 1243 | case ARM::BI__builtin_neon_vcvtq_s32_v: |
| 1244 | case ARM::BI__builtin_neon_vcvtq_u32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1245 | Ops[0] = Builder.CreateBitCast(Ops[0], GetNeonType(VMContext, 4, quad)); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1246 | return usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt") |
| 1247 | : Builder.CreateFPToSI(Ops[0], Ty, "vcvt"); |
| 1248 | } |
| 1249 | case ARM::BI__builtin_neon_vcvt_n_f32_v: |
| 1250 | case ARM::BI__builtin_neon_vcvtq_n_f32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1251 | const llvm::Type *Tys[2] = { GetNeonType(VMContext, 4, quad), Ty }; |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1252 | Int = usgn ? Intrinsic::arm_neon_vcvtfxu2fp : Intrinsic::arm_neon_vcvtfxs2fp; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1253 | Function *F = CGM.getIntrinsic(Int, Tys, 2); |
| 1254 | return EmitNeonCall(F, Ops, "vcvt_n"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1255 | } |
| 1256 | case ARM::BI__builtin_neon_vcvt_n_s32_v: |
| 1257 | case ARM::BI__builtin_neon_vcvt_n_u32_v: |
| 1258 | case ARM::BI__builtin_neon_vcvtq_n_s32_v: |
| 1259 | case ARM::BI__builtin_neon_vcvtq_n_u32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1260 | const llvm::Type *Tys[2] = { Ty, GetNeonType(VMContext, 4, quad) }; |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1261 | Int = usgn ? Intrinsic::arm_neon_vcvtfp2fxu : Intrinsic::arm_neon_vcvtfp2fxs; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1262 | Function *F = CGM.getIntrinsic(Int, Tys, 2); |
| 1263 | return EmitNeonCall(F, Ops, "vcvt_n"); |
| 1264 | } |
Nate Begeman | cd480ad | 2010-08-06 01:24:57 +0000 | [diff] [blame] | 1265 | case ARM::BI__builtin_neon_vdup_lane_v: |
| 1266 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1267 | return EmitNeonSplat(Ops[0], cast<Constant>(Ops[1])); |
| 1268 | case ARM::BI__builtin_neon_vdupq_lane_v: |
| 1269 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1270 | return EmitNeonSplat(Ops[0], cast<Constant>(Ops[1]), true); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1271 | case ARM::BI__builtin_neon_vext_v: |
| 1272 | case ARM::BI__builtin_neon_vextq_v: { |
| 1273 | ConstantInt *C = dyn_cast<ConstantInt>(Ops[2]); |
| 1274 | int CV = C->getSExtValue(); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1275 | SmallVector<Constant*, 16> Indices; |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1276 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1277 | Indices.push_back(ConstantInt::get(Int32Ty, i+CV)); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1278 | |
| 1279 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1280 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1281 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1282 | return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vext"); |
| 1283 | } |
Nate Begeman | 95450f6 | 2010-06-09 05:30:26 +0000 | [diff] [blame] | 1284 | case ARM::BI__builtin_neon_vget_lane_i8: |
| 1285 | case ARM::BI__builtin_neon_vget_lane_i16: |
| 1286 | case ARM::BI__builtin_neon_vget_lane_i32: |
| 1287 | case ARM::BI__builtin_neon_vget_lane_i64: |
| 1288 | case ARM::BI__builtin_neon_vget_lane_f32: |
| 1289 | case ARM::BI__builtin_neon_vgetq_lane_i8: |
| 1290 | case ARM::BI__builtin_neon_vgetq_lane_i16: |
| 1291 | case ARM::BI__builtin_neon_vgetq_lane_i32: |
| 1292 | case ARM::BI__builtin_neon_vgetq_lane_i64: |
| 1293 | case ARM::BI__builtin_neon_vgetq_lane_f32: |
Nate Begeman | df98e1d | 2010-06-09 18:04:15 +0000 | [diff] [blame] | 1294 | return Builder.CreateExtractElement(Ops[0], EmitScalarExpr(E->getArg(1)), |
| 1295 | "vget_lane"); |
| 1296 | case ARM::BI__builtin_neon_vhadd_v: |
| 1297 | case ARM::BI__builtin_neon_vhaddq_v: |
| 1298 | Int = usgn ? Intrinsic::arm_neon_vhaddu : Intrinsic::arm_neon_vhadds; |
| 1299 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhadd"); |
| 1300 | case ARM::BI__builtin_neon_vhsub_v: |
| 1301 | case ARM::BI__builtin_neon_vhsubq_v: |
| 1302 | Int = usgn ? Intrinsic::arm_neon_vhsubu : Intrinsic::arm_neon_vhsubs; |
| 1303 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhsub"); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1304 | case ARM::BI__builtin_neon_vld1_v: |
| 1305 | case ARM::BI__builtin_neon_vld1q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1306 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1307 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vld1, &Ty, 1), |
| 1308 | Ops, "vld1"); |
| 1309 | case ARM::BI__builtin_neon_vld1_lane_v: |
| 1310 | case ARM::BI__builtin_neon_vld1q_lane_v: |
| 1311 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1312 | Ty = llvm::PointerType::getUnqual(VTy->getElementType()); |
| 1313 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1314 | Ops[0] = Builder.CreateLoad(Ops[0]); |
| 1315 | return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vld1_lane"); |
| 1316 | case ARM::BI__builtin_neon_vld1_dup_v: |
| 1317 | case ARM::BI__builtin_neon_vld1q_dup_v: { |
| 1318 | Value *V = UndefValue::get(Ty); |
| 1319 | Ty = llvm::PointerType::getUnqual(VTy->getElementType()); |
| 1320 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1321 | Ops[0] = Builder.CreateLoad(Ops[0]); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1322 | llvm::Constant *CI = ConstantInt::get(Int32Ty, 0); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1323 | Ops[0] = Builder.CreateInsertElement(V, Ops[0], CI); |
| 1324 | return EmitNeonSplat(Ops[0], CI); |
| 1325 | } |
| 1326 | case ARM::BI__builtin_neon_vld2_v: |
| 1327 | case ARM::BI__builtin_neon_vld2q_v: { |
| 1328 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2, &Ty, 1); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1329 | Value *Align = GetPointeeAlignment(*this, E->getArg(1)); |
| 1330 | Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld2"); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1331 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1332 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1333 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1334 | } |
| 1335 | case ARM::BI__builtin_neon_vld3_v: |
| 1336 | case ARM::BI__builtin_neon_vld3q_v: { |
| 1337 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3, &Ty, 1); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1338 | Value *Align = GetPointeeAlignment(*this, E->getArg(1)); |
| 1339 | Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld3"); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1340 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1341 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1342 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1343 | } |
| 1344 | case ARM::BI__builtin_neon_vld4_v: |
| 1345 | case ARM::BI__builtin_neon_vld4q_v: { |
| 1346 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4, &Ty, 1); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1347 | Value *Align = GetPointeeAlignment(*this, E->getArg(1)); |
| 1348 | Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld4"); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1349 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1350 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1351 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1352 | } |
| 1353 | case ARM::BI__builtin_neon_vld2_lane_v: |
| 1354 | case ARM::BI__builtin_neon_vld2q_lane_v: { |
| 1355 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2lane, &Ty, 1); |
| 1356 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
| 1357 | Ops[3] = Builder.CreateBitCast(Ops[3], Ty); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1358 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(1))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1359 | Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld2_lane"); |
| 1360 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1361 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1362 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1363 | } |
| 1364 | case ARM::BI__builtin_neon_vld3_lane_v: |
| 1365 | case ARM::BI__builtin_neon_vld3q_lane_v: { |
| 1366 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3lane, &Ty, 1); |
| 1367 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
| 1368 | Ops[3] = Builder.CreateBitCast(Ops[3], Ty); |
| 1369 | Ops[4] = Builder.CreateBitCast(Ops[4], Ty); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1370 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(1))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1371 | Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane"); |
| 1372 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1373 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1374 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1375 | } |
| 1376 | case ARM::BI__builtin_neon_vld4_lane_v: |
| 1377 | case ARM::BI__builtin_neon_vld4q_lane_v: { |
| 1378 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4lane, &Ty, 1); |
| 1379 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
| 1380 | Ops[3] = Builder.CreateBitCast(Ops[3], Ty); |
| 1381 | Ops[4] = Builder.CreateBitCast(Ops[4], Ty); |
| 1382 | Ops[5] = Builder.CreateBitCast(Ops[5], Ty); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1383 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(1))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1384 | Ops[1] = Builder.CreateCall(F, Ops.begin() + 1, Ops.end(), "vld3_lane"); |
| 1385 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1386 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1387 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1388 | } |
| 1389 | case ARM::BI__builtin_neon_vld2_dup_v: |
| 1390 | case ARM::BI__builtin_neon_vld3_dup_v: |
| 1391 | case ARM::BI__builtin_neon_vld4_dup_v: { |
| 1392 | switch (BuiltinID) { |
| 1393 | case ARM::BI__builtin_neon_vld2_dup_v: |
| 1394 | Int = Intrinsic::arm_neon_vld2lane; |
| 1395 | break; |
| 1396 | case ARM::BI__builtin_neon_vld3_dup_v: |
| 1397 | Int = Intrinsic::arm_neon_vld2lane; |
| 1398 | break; |
| 1399 | case ARM::BI__builtin_neon_vld4_dup_v: |
| 1400 | Int = Intrinsic::arm_neon_vld2lane; |
| 1401 | break; |
| 1402 | default: assert(0 && "unknown vld_dup intrinsic?"); |
| 1403 | } |
| 1404 | Function *F = CGM.getIntrinsic(Int, &Ty, 1); |
| 1405 | const llvm::StructType *STy = cast<llvm::StructType>(F->getReturnType()); |
| 1406 | |
| 1407 | SmallVector<Value*, 6> Args; |
| 1408 | Args.push_back(Ops[1]); |
| 1409 | Args.append(STy->getNumElements(), UndefValue::get(Ty)); |
| 1410 | |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1411 | llvm::Constant *CI = ConstantInt::get(Int32Ty, 0); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1412 | Args.push_back(CI); |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1413 | Args.push_back(GetPointeeAlignment(*this, E->getArg(1))); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1414 | |
| 1415 | Ops[1] = Builder.CreateCall(F, Args.begin(), Args.end(), "vld_dup"); |
| 1416 | // splat lane 0 to all elts in each vector of the result. |
| 1417 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1418 | Value *Val = Builder.CreateExtractValue(Ops[1], i); |
| 1419 | Value *Elt = Builder.CreateBitCast(Val, Ty); |
| 1420 | Elt = EmitNeonSplat(Elt, CI); |
| 1421 | Elt = Builder.CreateBitCast(Elt, Val->getType()); |
| 1422 | Ops[1] = Builder.CreateInsertValue(Ops[1], Elt, i); |
| 1423 | } |
| 1424 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1425 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1426 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1427 | } |
Nate Begeman | df98e1d | 2010-06-09 18:04:15 +0000 | [diff] [blame] | 1428 | case ARM::BI__builtin_neon_vmax_v: |
| 1429 | case ARM::BI__builtin_neon_vmaxq_v: |
| 1430 | Int = usgn ? Intrinsic::arm_neon_vmaxu : Intrinsic::arm_neon_vmaxs; |
| 1431 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmax"); |
| 1432 | case ARM::BI__builtin_neon_vmin_v: |
| 1433 | case ARM::BI__builtin_neon_vminq_v: |
| 1434 | Int = usgn ? Intrinsic::arm_neon_vminu : Intrinsic::arm_neon_vmins; |
| 1435 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmin"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1436 | case ARM::BI__builtin_neon_vmlal_lane_v: { |
| 1437 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1438 | Ops[2] = Builder.CreateBitCast(Ops[2], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1439 | Ops[2] = EmitNeonSplat(Ops[2], cast<Constant>(Ops[3])); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1440 | } |
| 1441 | case ARM::BI__builtin_neon_vmlal_v: { |
| 1442 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1443 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1444 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
| 1445 | Ops[2] = Builder.CreateBitCast(Ops[2], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1446 | if (usgn) { |
| 1447 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1448 | Ops[2] = Builder.CreateZExt(Ops[2], Ty); |
| 1449 | } else { |
| 1450 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1451 | Ops[2] = Builder.CreateSExt(Ops[2], Ty); |
| 1452 | } |
| 1453 | Ops[1] = Builder.CreateMul(Ops[1], Ops[2]); |
| 1454 | return Builder.CreateAdd(Ops[0], Ops[1], "vmlal"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1455 | } |
| 1456 | case ARM::BI__builtin_neon_vmlsl_lane_v: { |
| 1457 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1458 | Ops[2] = Builder.CreateBitCast(Ops[2], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1459 | Ops[2] = EmitNeonSplat(Ops[2], cast<Constant>(Ops[3])); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1460 | } |
| 1461 | case ARM::BI__builtin_neon_vmlsl_v: { |
| 1462 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1463 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1464 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
| 1465 | Ops[2] = Builder.CreateBitCast(Ops[2], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1466 | if (usgn) { |
| 1467 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1468 | Ops[2] = Builder.CreateZExt(Ops[2], Ty); |
| 1469 | } else { |
| 1470 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1471 | Ops[2] = Builder.CreateSExt(Ops[2], Ty); |
| 1472 | } |
| 1473 | Ops[1] = Builder.CreateMul(Ops[1], Ops[2]); |
| 1474 | return Builder.CreateSub(Ops[0], Ops[1], "vmlsl"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1475 | } |
| 1476 | case ARM::BI__builtin_neon_vmovl_v: { |
| 1477 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1478 | Ops[0] = Builder.CreateBitCast(Ops[0], DTy); |
Bob Wilson | 7cea322 | 2010-08-20 03:36:08 +0000 | [diff] [blame] | 1479 | if (usgn) |
| 1480 | return Builder.CreateZExt(Ops[0], Ty, "vmovl"); |
| 1481 | return Builder.CreateSExt(Ops[0], Ty, "vmovl"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1482 | } |
| 1483 | case ARM::BI__builtin_neon_vmovn_v: { |
| 1484 | const llvm::Type *QTy = llvm::VectorType::getExtendedElementVectorType(VTy); |
| 1485 | Ops[0] = Builder.CreateBitCast(Ops[0], QTy); |
Bob Wilson | 3b6081b | 2010-08-30 19:57:13 +0000 | [diff] [blame] | 1486 | return Builder.CreateTrunc(Ops[0], Ty, "vmovn"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1487 | } |
| 1488 | case ARM::BI__builtin_neon_vmull_lane_v: { |
| 1489 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1490 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1491 | Ops[1] = EmitNeonSplat(Ops[1], cast<Constant>(Ops[2])); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1492 | } |
| 1493 | case ARM::BI__builtin_neon_vmull_v: { |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1494 | if (poly) |
| 1495 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vmullp, &Ty, 1), |
| 1496 | Ops, "vmull"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1497 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1498 | Ops[0] = Builder.CreateBitCast(Ops[0], DTy); |
| 1499 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | bf346e9 | 2010-09-01 23:20:27 +0000 | [diff] [blame] | 1500 | if (usgn) { |
| 1501 | Ops[0] = Builder.CreateZExt(Ops[0], Ty); |
| 1502 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1503 | } else { |
| 1504 | Ops[0] = Builder.CreateSExt(Ops[0], Ty); |
| 1505 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1506 | } |
| 1507 | return Builder.CreateMul(Ops[0], Ops[1], "vmull"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1508 | } |
Nate Begeman | df98e1d | 2010-06-09 18:04:15 +0000 | [diff] [blame] | 1509 | case ARM::BI__builtin_neon_vpadal_v: |
| 1510 | case ARM::BI__builtin_neon_vpadalq_v: |
| 1511 | Int = usgn ? Intrinsic::arm_neon_vpadalu : Intrinsic::arm_neon_vpadals; |
| 1512 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpadal"); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1513 | case ARM::BI__builtin_neon_vpadd_v: |
| 1514 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vpadd, &Ty, 1), |
| 1515 | Ops, "vpadd"); |
| 1516 | case ARM::BI__builtin_neon_vpaddl_v: |
| 1517 | case ARM::BI__builtin_neon_vpaddlq_v: |
| 1518 | Int = usgn ? Intrinsic::arm_neon_vpaddlu : Intrinsic::arm_neon_vpaddls; |
| 1519 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpaddl"); |
| 1520 | case ARM::BI__builtin_neon_vpmax_v: |
| 1521 | Int = usgn ? Intrinsic::arm_neon_vpmaxu : Intrinsic::arm_neon_vpmaxs; |
| 1522 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmax"); |
| 1523 | case ARM::BI__builtin_neon_vpmin_v: |
| 1524 | Int = usgn ? Intrinsic::arm_neon_vpminu : Intrinsic::arm_neon_vpmins; |
| 1525 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpmin"); |
| 1526 | case ARM::BI__builtin_neon_vqabs_v: |
| 1527 | case ARM::BI__builtin_neon_vqabsq_v: |
| 1528 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqabs, &Ty, 1), |
| 1529 | Ops, "vqabs"); |
| 1530 | case ARM::BI__builtin_neon_vqadd_v: |
| 1531 | case ARM::BI__builtin_neon_vqaddq_v: |
| 1532 | Int = usgn ? Intrinsic::arm_neon_vqaddu : Intrinsic::arm_neon_vqadds; |
| 1533 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqadd"); |
| 1534 | case ARM::BI__builtin_neon_vqdmlal_lane_v: |
| 1535 | splat = true; |
| 1536 | case ARM::BI__builtin_neon_vqdmlal_v: |
| 1537 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlal, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1538 | Ops, "vqdmlal", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1539 | case ARM::BI__builtin_neon_vqdmlsl_lane_v: |
| 1540 | splat = true; |
| 1541 | case ARM::BI__builtin_neon_vqdmlsl_v: |
| 1542 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmlsl, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1543 | Ops, "vqdmlsl", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1544 | case ARM::BI__builtin_neon_vqdmulh_lane_v: |
| 1545 | case ARM::BI__builtin_neon_vqdmulhq_lane_v: |
| 1546 | splat = true; |
| 1547 | case ARM::BI__builtin_neon_vqdmulh_v: |
| 1548 | case ARM::BI__builtin_neon_vqdmulhq_v: |
| 1549 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmulh, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1550 | Ops, "vqdmulh", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1551 | case ARM::BI__builtin_neon_vqdmull_lane_v: |
| 1552 | splat = true; |
| 1553 | case ARM::BI__builtin_neon_vqdmull_v: |
| 1554 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqdmull, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1555 | Ops, "vqdmull", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1556 | case ARM::BI__builtin_neon_vqmovn_v: |
| 1557 | Int = usgn ? Intrinsic::arm_neon_vqmovnu : Intrinsic::arm_neon_vqmovns; |
| 1558 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqmovn"); |
| 1559 | case ARM::BI__builtin_neon_vqmovun_v: |
| 1560 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqmovnsu, &Ty, 1), |
| 1561 | Ops, "vqdmull"); |
| 1562 | case ARM::BI__builtin_neon_vqneg_v: |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1563 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqneg, &Ty, 1), |
| 1564 | Ops, "vqneg"); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1565 | case ARM::BI__builtin_neon_vqrdmulh_lane_v: |
| 1566 | case ARM::BI__builtin_neon_vqrdmulhq_lane_v: |
| 1567 | splat = true; |
| 1568 | case ARM::BI__builtin_neon_vqrdmulh_v: |
| 1569 | case ARM::BI__builtin_neon_vqrdmulhq_v: |
| 1570 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrdmulh, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1571 | Ops, "vqrdmulh", splat); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1572 | case ARM::BI__builtin_neon_vqrshl_v: |
| 1573 | case ARM::BI__builtin_neon_vqrshlq_v: |
| 1574 | Int = usgn ? Intrinsic::arm_neon_vqrshiftu : Intrinsic::arm_neon_vqrshifts; |
| 1575 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshl"); |
| 1576 | case ARM::BI__builtin_neon_vqrshrn_n_v: |
| 1577 | Int = usgn ? Intrinsic::arm_neon_vqrshiftnu : Intrinsic::arm_neon_vqrshiftns; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1578 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqrshrn_n", false, |
| 1579 | 1, true); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1580 | case ARM::BI__builtin_neon_vqrshrun_n_v: |
| 1581 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqrshiftnsu, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1582 | Ops, "vqrshrun_n", false, 1, true); |
| 1583 | case ARM::BI__builtin_neon_vqshl_v: |
| 1584 | case ARM::BI__builtin_neon_vqshlq_v: |
| 1585 | Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts; |
| 1586 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl"); |
| 1587 | case ARM::BI__builtin_neon_vqshl_n_v: |
| 1588 | case ARM::BI__builtin_neon_vqshlq_n_v: |
| 1589 | Int = usgn ? Intrinsic::arm_neon_vqshiftu : Intrinsic::arm_neon_vqshifts; |
| 1590 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshl_n", false, |
| 1591 | 1, false); |
| 1592 | case ARM::BI__builtin_neon_vqshlu_n_v: |
| 1593 | case ARM::BI__builtin_neon_vqshluq_n_v: |
| 1594 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftsu, &Ty, 1), |
| 1595 | Ops, "vqshlu", 1, false); |
| 1596 | case ARM::BI__builtin_neon_vqshrn_n_v: |
| 1597 | Int = usgn ? Intrinsic::arm_neon_vqshiftnu : Intrinsic::arm_neon_vqshiftns; |
| 1598 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqshrn_n", false, |
| 1599 | 1, true); |
| 1600 | case ARM::BI__builtin_neon_vqshrun_n_v: |
| 1601 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vqshiftnsu, &Ty, 1), |
| 1602 | Ops, "vqshrun_n", false, 1, true); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1603 | case ARM::BI__builtin_neon_vqsub_v: |
| 1604 | case ARM::BI__builtin_neon_vqsubq_v: |
| 1605 | Int = usgn ? Intrinsic::arm_neon_vqsubu : Intrinsic::arm_neon_vqsubs; |
| 1606 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vqsub"); |
| 1607 | case ARM::BI__builtin_neon_vraddhn_v: |
| 1608 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vraddhn, &Ty, 1), |
| 1609 | Ops, "vraddhn"); |
| 1610 | case ARM::BI__builtin_neon_vrecpe_v: |
| 1611 | case ARM::BI__builtin_neon_vrecpeq_v: |
| 1612 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecpe, &Ty, 1), |
| 1613 | Ops, "vrecpe"); |
| 1614 | case ARM::BI__builtin_neon_vrecps_v: |
| 1615 | case ARM::BI__builtin_neon_vrecpsq_v: |
| 1616 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrecps, &Ty, 1), |
| 1617 | Ops, "vrecps"); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1618 | case ARM::BI__builtin_neon_vrhadd_v: |
| 1619 | case ARM::BI__builtin_neon_vrhaddq_v: |
| 1620 | Int = usgn ? Intrinsic::arm_neon_vrhaddu : Intrinsic::arm_neon_vrhadds; |
| 1621 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrhadd"); |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1622 | case ARM::BI__builtin_neon_vrshl_v: |
| 1623 | case ARM::BI__builtin_neon_vrshlq_v: |
| 1624 | Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts; |
| 1625 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshl"); |
| 1626 | case ARM::BI__builtin_neon_vrshrn_n_v: |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1627 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrshiftn, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1628 | Ops, "vrshrn_n", false, 1, true); |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1629 | case ARM::BI__builtin_neon_vrshr_n_v: |
| 1630 | case ARM::BI__builtin_neon_vrshrq_n_v: |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1631 | Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1632 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vrshr_n", false, |
| 1633 | 1, true); |
Nate Begeman | 5af93ef | 2010-06-12 06:06:07 +0000 | [diff] [blame] | 1634 | case ARM::BI__builtin_neon_vrsqrte_v: |
| 1635 | case ARM::BI__builtin_neon_vrsqrteq_v: |
| 1636 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrte, &Ty, 1), |
| 1637 | Ops, "vrsqrte"); |
| 1638 | case ARM::BI__builtin_neon_vrsqrts_v: |
| 1639 | case ARM::BI__builtin_neon_vrsqrtsq_v: |
| 1640 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsqrts, &Ty, 1), |
| 1641 | Ops, "vrsqrts"); |
| 1642 | case ARM::BI__builtin_neon_vrsra_n_v: |
| 1643 | case ARM::BI__builtin_neon_vrsraq_n_v: |
| 1644 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1645 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1646 | Ops[2] = EmitNeonShiftVector(Ops[2], Ty, true); |
| 1647 | Int = usgn ? Intrinsic::arm_neon_vrshiftu : Intrinsic::arm_neon_vrshifts; |
| 1648 | Ops[1] = Builder.CreateCall2(CGM.getIntrinsic(Int, &Ty, 1), Ops[1], Ops[2]); |
| 1649 | return Builder.CreateAdd(Ops[0], Ops[1], "vrsra_n"); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1650 | case ARM::BI__builtin_neon_vrsubhn_v: |
| 1651 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vrsubhn, &Ty, 1), |
| 1652 | Ops, "vrsubhn"); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1653 | case ARM::BI__builtin_neon_vset_lane_i8: |
| 1654 | case ARM::BI__builtin_neon_vset_lane_i16: |
| 1655 | case ARM::BI__builtin_neon_vset_lane_i32: |
| 1656 | case ARM::BI__builtin_neon_vset_lane_i64: |
| 1657 | case ARM::BI__builtin_neon_vset_lane_f32: |
| 1658 | case ARM::BI__builtin_neon_vsetq_lane_i8: |
| 1659 | case ARM::BI__builtin_neon_vsetq_lane_i16: |
| 1660 | case ARM::BI__builtin_neon_vsetq_lane_i32: |
| 1661 | case ARM::BI__builtin_neon_vsetq_lane_i64: |
| 1662 | case ARM::BI__builtin_neon_vsetq_lane_f32: |
| 1663 | Ops.push_back(EmitScalarExpr(E->getArg(2))); |
| 1664 | return Builder.CreateInsertElement(Ops[1], Ops[0], Ops[2], "vset_lane"); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1665 | case ARM::BI__builtin_neon_vshl_v: |
| 1666 | case ARM::BI__builtin_neon_vshlq_v: |
| 1667 | Int = usgn ? Intrinsic::arm_neon_vshiftu : Intrinsic::arm_neon_vshifts; |
| 1668 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshl"); |
| 1669 | case ARM::BI__builtin_neon_vshll_n_v: |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1670 | Int = usgn ? Intrinsic::arm_neon_vshiftlu : Intrinsic::arm_neon_vshiftls; |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1671 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vshll", false, 1); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1672 | case ARM::BI__builtin_neon_vshl_n_v: |
| 1673 | case ARM::BI__builtin_neon_vshlq_n_v: |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1674 | Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false); |
| 1675 | return Builder.CreateShl(Builder.CreateBitCast(Ops[0],Ty), Ops[1], "vshl_n"); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1676 | case ARM::BI__builtin_neon_vshrn_n_v: |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1677 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftn, &Ty, 1), |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1678 | Ops, "vshrn_n", false, 1, true); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1679 | case ARM::BI__builtin_neon_vshr_n_v: |
| 1680 | case ARM::BI__builtin_neon_vshrq_n_v: |
| 1681 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1682 | Ops[1] = EmitNeonShiftVector(Ops[1], Ty, false); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1683 | if (usgn) |
| 1684 | return Builder.CreateLShr(Ops[0], Ops[1], "vshr_n"); |
| 1685 | else |
| 1686 | return Builder.CreateAShr(Ops[0], Ops[1], "vshr_n"); |
| 1687 | case ARM::BI__builtin_neon_vsri_n_v: |
| 1688 | case ARM::BI__builtin_neon_vsriq_n_v: |
| 1689 | poly = true; |
| 1690 | case ARM::BI__builtin_neon_vsli_n_v: |
| 1691 | case ARM::BI__builtin_neon_vsliq_n_v: |
| 1692 | Ops[2] = EmitNeonShiftVector(Ops[2], Ty, poly); |
| 1693 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vshiftins, &Ty, 1), |
| 1694 | Ops, "vsli_n"); |
| 1695 | case ARM::BI__builtin_neon_vsra_n_v: |
| 1696 | case ARM::BI__builtin_neon_vsraq_n_v: |
| 1697 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1698 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1699 | Ops[2] = EmitNeonShiftVector(Ops[2], Ty, false); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1700 | if (usgn) |
| 1701 | Ops[1] = Builder.CreateLShr(Ops[1], Ops[2], "vsra_n"); |
| 1702 | else |
| 1703 | Ops[1] = Builder.CreateAShr(Ops[1], Ops[2], "vsra_n"); |
| 1704 | return Builder.CreateAdd(Ops[0], Ops[1]); |
| 1705 | case ARM::BI__builtin_neon_vst1_v: |
| 1706 | case ARM::BI__builtin_neon_vst1q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1707 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1708 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst1, &Ty, 1), |
| 1709 | Ops, ""); |
| 1710 | case ARM::BI__builtin_neon_vst1_lane_v: |
| 1711 | case ARM::BI__builtin_neon_vst1q_lane_v: |
| 1712 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1713 | Ops[1] = Builder.CreateExtractElement(Ops[1], Ops[2]); |
| 1714 | Ty = llvm::PointerType::getUnqual(Ops[1]->getType()); |
| 1715 | return Builder.CreateStore(Ops[1], Builder.CreateBitCast(Ops[0], Ty)); |
| 1716 | case ARM::BI__builtin_neon_vst2_v: |
| 1717 | case ARM::BI__builtin_neon_vst2q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1718 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1719 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2, &Ty, 1), |
| 1720 | Ops, ""); |
| 1721 | case ARM::BI__builtin_neon_vst2_lane_v: |
| 1722 | case ARM::BI__builtin_neon_vst2q_lane_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1723 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1724 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2lane, &Ty, 1), |
| 1725 | Ops, ""); |
| 1726 | case ARM::BI__builtin_neon_vst3_v: |
| 1727 | case ARM::BI__builtin_neon_vst3q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1728 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1729 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3, &Ty, 1), |
| 1730 | Ops, ""); |
| 1731 | case ARM::BI__builtin_neon_vst3_lane_v: |
| 1732 | case ARM::BI__builtin_neon_vst3q_lane_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1733 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1734 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3lane, &Ty, 1), |
| 1735 | Ops, ""); |
| 1736 | case ARM::BI__builtin_neon_vst4_v: |
| 1737 | case ARM::BI__builtin_neon_vst4q_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1738 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1739 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4, &Ty, 1), |
| 1740 | Ops, ""); |
| 1741 | case ARM::BI__builtin_neon_vst4_lane_v: |
| 1742 | case ARM::BI__builtin_neon_vst4q_lane_v: |
Bob Wilson | 06b6c58 | 2010-08-27 17:14:29 +0000 | [diff] [blame] | 1743 | Ops.push_back(GetPointeeAlignment(*this, E->getArg(0))); |
Nate Begeman | 464ccb6 | 2010-06-11 22:57:12 +0000 | [diff] [blame] | 1744 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4lane, &Ty, 1), |
| 1745 | Ops, ""); |
Nate Begeman | 548f7da | 2010-06-10 18:11:55 +0000 | [diff] [blame] | 1746 | case ARM::BI__builtin_neon_vsubhn_v: |
| 1747 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vsubhn, &Ty, 1), |
| 1748 | Ops, "vsubhn"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1749 | case ARM::BI__builtin_neon_vsubl_v: { |
| 1750 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1751 | Ops[0] = Builder.CreateBitCast(Ops[0], DTy); |
| 1752 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | 54160c6 | 2010-08-29 05:14:28 +0000 | [diff] [blame] | 1753 | if (usgn) { |
| 1754 | Ops[0] = Builder.CreateZExt(Ops[0], Ty); |
| 1755 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1756 | } else { |
| 1757 | Ops[0] = Builder.CreateSExt(Ops[0], Ty); |
| 1758 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1759 | } |
| 1760 | return Builder.CreateSub(Ops[0], Ops[1], "vsubl"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1761 | } |
| 1762 | case ARM::BI__builtin_neon_vsubw_v: { |
| 1763 | const llvm::Type *DTy =llvm::VectorType::getTruncatedElementVectorType(VTy); |
| 1764 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1765 | Ops[1] = Builder.CreateBitCast(Ops[1], DTy); |
Bob Wilson | 54160c6 | 2010-08-29 05:14:28 +0000 | [diff] [blame] | 1766 | if (usgn) |
| 1767 | Ops[1] = Builder.CreateZExt(Ops[1], Ty); |
| 1768 | else |
| 1769 | Ops[1] = Builder.CreateSExt(Ops[1], Ty); |
| 1770 | return Builder.CreateSub(Ops[0], Ops[1], "vsubw"); |
Bob Wilson | 2235941 | 2010-09-02 22:37:30 +0000 | [diff] [blame] | 1771 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1772 | case ARM::BI__builtin_neon_vtbl1_v: |
| 1773 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl1), |
| 1774 | Ops, "vtbl1"); |
| 1775 | case ARM::BI__builtin_neon_vtbl2_v: |
| 1776 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl2), |
| 1777 | Ops, "vtbl2"); |
| 1778 | case ARM::BI__builtin_neon_vtbl3_v: |
| 1779 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl3), |
| 1780 | Ops, "vtbl3"); |
| 1781 | case ARM::BI__builtin_neon_vtbl4_v: |
| 1782 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl4), |
| 1783 | Ops, "vtbl4"); |
| 1784 | case ARM::BI__builtin_neon_vtbx1_v: |
| 1785 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx1), |
| 1786 | Ops, "vtbx1"); |
| 1787 | case ARM::BI__builtin_neon_vtbx2_v: |
| 1788 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx2), |
| 1789 | Ops, "vtbx2"); |
| 1790 | case ARM::BI__builtin_neon_vtbx3_v: |
| 1791 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx3), |
| 1792 | Ops, "vtbx3"); |
| 1793 | case ARM::BI__builtin_neon_vtbx4_v: |
| 1794 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx4), |
| 1795 | Ops, "vtbx4"); |
| 1796 | case ARM::BI__builtin_neon_vtst_v: |
| 1797 | case ARM::BI__builtin_neon_vtstq_v: { |
| 1798 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1799 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1800 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1]); |
| 1801 | Ops[0] = Builder.CreateICmp(ICmpInst::ICMP_NE, Ops[0], |
| 1802 | ConstantAggregateZero::get(Ty)); |
| 1803 | return Builder.CreateSExt(Ops[0], Ty, "vtst"); |
| 1804 | } |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1805 | case ARM::BI__builtin_neon_vtrn_v: |
| 1806 | case ARM::BI__builtin_neon_vtrnq_v: { |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1807 | Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty)); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1808 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1809 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1810 | Value *SV; |
| 1811 | |
| 1812 | for (unsigned vi = 0; vi != 2; ++vi) { |
| 1813 | SmallVector<Constant*, 16> Indices; |
| 1814 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1815 | Indices.push_back(ConstantInt::get(Int32Ty, i+vi)); |
| 1816 | Indices.push_back(ConstantInt::get(Int32Ty, i+e+vi)); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1817 | } |
| 1818 | Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi); |
| 1819 | SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1820 | SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vtrn"); |
| 1821 | SV = Builder.CreateStore(SV, Addr); |
| 1822 | } |
| 1823 | return SV; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1824 | } |
| 1825 | case ARM::BI__builtin_neon_vuzp_v: |
| 1826 | case ARM::BI__builtin_neon_vuzpq_v: { |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1827 | Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty)); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1828 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1829 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1830 | Value *SV; |
| 1831 | |
| 1832 | for (unsigned vi = 0; vi != 2; ++vi) { |
| 1833 | SmallVector<Constant*, 16> Indices; |
| 1834 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1835 | Indices.push_back(ConstantInt::get(Int32Ty, 2*i+vi)); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1836 | |
| 1837 | Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi); |
| 1838 | SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1839 | SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vuzp"); |
| 1840 | SV = Builder.CreateStore(SV, Addr); |
| 1841 | } |
| 1842 | return SV; |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1843 | } |
| 1844 | case ARM::BI__builtin_neon_vzip_v: |
| 1845 | case ARM::BI__builtin_neon_vzipq_v: { |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1846 | Ops[0] = Builder.CreateBitCast(Ops[0], llvm::PointerType::getUnqual(Ty)); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1847 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1848 | Ops[2] = Builder.CreateBitCast(Ops[2], Ty); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1849 | Value *SV; |
| 1850 | |
| 1851 | for (unsigned vi = 0; vi != 2; ++vi) { |
| 1852 | SmallVector<Constant*, 16> Indices; |
| 1853 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) { |
Daniel Dunbar | e361cc3 | 2010-08-26 00:55:57 +0000 | [diff] [blame] | 1854 | Indices.push_back(ConstantInt::get(Int32Ty, (i + vi*e) >> 1)); |
| 1855 | Indices.push_back(ConstantInt::get(Int32Ty, ((i + vi*e) >> 1)+e)); |
Nate Begeman | 4be5430 | 2010-06-20 23:05:28 +0000 | [diff] [blame] | 1856 | } |
| 1857 | Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi); |
| 1858 | SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1859 | SV = Builder.CreateShuffleVector(Ops[1], Ops[2], SV, "vzip"); |
| 1860 | SV = Builder.CreateStore(SV, Addr); |
| 1861 | } |
| 1862 | return SV; |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1863 | } |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 1864 | } |
| 1865 | } |
| 1866 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 1868 | const CallExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1870 | llvm::SmallVector<Value*, 4> Ops; |
| 1871 | |
| 1872 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 1873 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 1874 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 1875 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 1876 | default: return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | case X86::BI__builtin_ia32_pslldi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1878 | case X86::BI__builtin_ia32_psllqi128: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | case X86::BI__builtin_ia32_psllwi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1880 | case X86::BI__builtin_ia32_psradi128: |
| 1881 | case X86::BI__builtin_ia32_psrawi128: |
| 1882 | case X86::BI__builtin_ia32_psrldi128: |
| 1883 | case X86::BI__builtin_ia32_psrlqi128: |
| 1884 | case X86::BI__builtin_ia32_psrlwi128: { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1885 | Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext"); |
| 1886 | const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 2); |
| 1887 | llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1888 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1889 | Ops[1], Zero, "insert"); |
| 1890 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 1891 | const char *name = 0; |
| 1892 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1894 | switch (BuiltinID) { |
| 1895 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 1896 | case X86::BI__builtin_ia32_pslldi128: |
| 1897 | name = "pslldi"; |
| 1898 | ID = Intrinsic::x86_sse2_psll_d; |
| 1899 | break; |
| 1900 | case X86::BI__builtin_ia32_psllqi128: |
| 1901 | name = "psllqi"; |
| 1902 | ID = Intrinsic::x86_sse2_psll_q; |
| 1903 | break; |
| 1904 | case X86::BI__builtin_ia32_psllwi128: |
| 1905 | name = "psllwi"; |
| 1906 | ID = Intrinsic::x86_sse2_psll_w; |
| 1907 | break; |
| 1908 | case X86::BI__builtin_ia32_psradi128: |
| 1909 | name = "psradi"; |
| 1910 | ID = Intrinsic::x86_sse2_psra_d; |
| 1911 | break; |
| 1912 | case X86::BI__builtin_ia32_psrawi128: |
| 1913 | name = "psrawi"; |
| 1914 | ID = Intrinsic::x86_sse2_psra_w; |
| 1915 | break; |
| 1916 | case X86::BI__builtin_ia32_psrldi128: |
| 1917 | name = "psrldi"; |
| 1918 | ID = Intrinsic::x86_sse2_psrl_d; |
| 1919 | break; |
| 1920 | case X86::BI__builtin_ia32_psrlqi128: |
| 1921 | name = "psrlqi"; |
| 1922 | ID = Intrinsic::x86_sse2_psrl_q; |
| 1923 | break; |
| 1924 | case X86::BI__builtin_ia32_psrlwi128: |
| 1925 | name = "psrlwi"; |
| 1926 | ID = Intrinsic::x86_sse2_psrl_w; |
| 1927 | break; |
| 1928 | } |
| 1929 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | case X86::BI__builtin_ia32_pslldi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1933 | case X86::BI__builtin_ia32_psllqi: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | case X86::BI__builtin_ia32_psllwi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1935 | case X86::BI__builtin_ia32_psradi: |
| 1936 | case X86::BI__builtin_ia32_psrawi: |
| 1937 | case X86::BI__builtin_ia32_psrldi: |
| 1938 | case X86::BI__builtin_ia32_psrlqi: |
| 1939 | case X86::BI__builtin_ia32_psrlwi: { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1940 | Ops[1] = Builder.CreateZExt(Ops[1], Int64Ty, "zext"); |
| 1941 | const llvm::Type *Ty = llvm::VectorType::get(Int64Ty, 1); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1942 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1943 | const char *name = 0; |
| 1944 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1946 | switch (BuiltinID) { |
| 1947 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 1948 | case X86::BI__builtin_ia32_pslldi: |
| 1949 | name = "pslldi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1950 | ID = Intrinsic::x86_mmx_pslli_d; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1951 | break; |
| 1952 | case X86::BI__builtin_ia32_psllqi: |
| 1953 | name = "psllqi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1954 | ID = Intrinsic::x86_mmx_pslli_q; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1955 | break; |
| 1956 | case X86::BI__builtin_ia32_psllwi: |
| 1957 | name = "psllwi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1958 | ID = Intrinsic::x86_mmx_pslli_w; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | case X86::BI__builtin_ia32_psradi: |
| 1961 | name = "psradi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1962 | ID = Intrinsic::x86_mmx_psrai_d; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1963 | break; |
| 1964 | case X86::BI__builtin_ia32_psrawi: |
| 1965 | name = "psrawi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1966 | ID = Intrinsic::x86_mmx_psrai_w; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1967 | break; |
| 1968 | case X86::BI__builtin_ia32_psrldi: |
| 1969 | name = "psrldi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1970 | ID = Intrinsic::x86_mmx_psrli_d; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1971 | break; |
| 1972 | case X86::BI__builtin_ia32_psrlqi: |
| 1973 | name = "psrlqi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1974 | ID = Intrinsic::x86_mmx_psrli_q; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1975 | break; |
| 1976 | case X86::BI__builtin_ia32_psrlwi: |
| 1977 | name = "psrlwi"; |
Bill Wendling | c6c8b33 | 2010-09-17 23:46:16 +0000 | [diff] [blame] | 1978 | ID = Intrinsic::x86_mmx_psrli_w; |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1979 | break; |
| 1980 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 1981 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1983 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 1984 | case X86::BI__builtin_ia32_cmpps: { |
| 1985 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 1986 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 1987 | } |
| 1988 | case X86::BI__builtin_ia32_cmpss: { |
| 1989 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 1990 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 1991 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1992 | case X86::BI__builtin_ia32_ldmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1993 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1994 | Value *One = llvm::ConstantInt::get(Int32Ty, 1); |
| 1995 | Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1996 | Builder.CreateStore(Ops[0], Tmp); |
| 1997 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 1998 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1999 | } |
| 2000 | case X86::BI__builtin_ia32_stmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 2001 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2002 | Value *One = llvm::ConstantInt::get(Int32Ty, 1); |
| 2003 | Value *Tmp = Builder.CreateAlloca(Int32Ty, One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2004 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 2005 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2006 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 2007 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 2008 | case X86::BI__builtin_ia32_cmppd: { |
| 2009 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 2010 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 2011 | } |
| 2012 | case X86::BI__builtin_ia32_cmpsd: { |
| 2013 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 2014 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 2015 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2016 | case X86::BI__builtin_ia32_storehps: |
| 2017 | case X86::BI__builtin_ia32_storelps: { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2018 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(Int64Ty); |
| 2019 | llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2); |
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 | // cast val v2i64 |
| 2022 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2024 | // extract (0, 1) |
| 2025 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2026 | llvm::Value *Idx = llvm::ConstantInt::get(Int32Ty, Index); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 2027 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 2028 | |
| 2029 | // cast pointer to i64 & store |
| 2030 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 2031 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 2032 | } |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 2033 | case X86::BI__builtin_ia32_palignr: { |
Eric Christopher | e57aa9e | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 2034 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 2035 | |
| 2036 | // If palignr is shifting the pair of input vectors less than 9 bytes, |
| 2037 | // emit a shuffle instruction. |
| 2038 | if (shiftVal <= 8) { |
Eric Christopher | e57aa9e | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 2039 | llvm::SmallVector<llvm::Constant*, 8> Indices; |
| 2040 | for (unsigned i = 0; i != 8; ++i) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2041 | Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i)); |
Eric Christopher | e57aa9e | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 2042 | |
| 2043 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 2044 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 2045 | } |
| 2046 | |
| 2047 | // If palignr is shifting the pair of input vectors more than 8 but less |
| 2048 | // than 16 bytes, emit a logical right shift of the destination. |
| 2049 | if (shiftVal < 16) { |
| 2050 | // 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] | 2051 | const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 1); |
Eric Christopher | e57aa9e | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 2052 | |
| 2053 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 2054 | Ops[1] = llvm::ConstantInt::get(VecTy, (shiftVal-8) * 8); |
| 2055 | |
| 2056 | // create i32 constant |
| 2057 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_mmx_psrl_q); |
| 2058 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 2059 | } |
| 2060 | |
| 2061 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 2062 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Nate Begeman | c3420ff | 2009-12-14 05:15:02 +0000 | [diff] [blame] | 2063 | } |
| 2064 | case X86::BI__builtin_ia32_palignr128: { |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2065 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 2066 | |
| 2067 | // If palignr is shifting the pair of input vectors less than 17 bytes, |
| 2068 | // emit a shuffle instruction. |
| 2069 | if (shiftVal <= 16) { |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2070 | llvm::SmallVector<llvm::Constant*, 16> Indices; |
| 2071 | for (unsigned i = 0; i != 16; ++i) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2072 | Indices.push_back(llvm::ConstantInt::get(Int32Ty, shiftVal + i)); |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2073 | |
| 2074 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 2075 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 2076 | } |
| 2077 | |
| 2078 | // If palignr is shifting the pair of input vectors more than 16 but less |
| 2079 | // than 32 bytes, emit a logical right shift of the destination. |
| 2080 | if (shiftVal < 32) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2081 | const llvm::Type *VecTy = llvm::VectorType::get(Int64Ty, 2); |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2082 | |
| 2083 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2084 | Ops[1] = llvm::ConstantInt::get(Int32Ty, (shiftVal-16) * 8); |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 2085 | |
| 2086 | // create i32 constant |
| 2087 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq); |
| 2088 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 2089 | } |
| 2090 | |
| 2091 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 2092 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 2093 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 2094 | } |
| 2095 | } |
| 2096 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 2098 | const CallExpr *E) { |
Chris Lattner | dd17394 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 2099 | llvm::SmallVector<Value*, 4> Ops; |
| 2100 | |
| 2101 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 2102 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 2103 | |
| 2104 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 2105 | |
| 2106 | switch (BuiltinID) { |
| 2107 | default: return 0; |
| 2108 | |
Anton Korobeynikov | 4d3a7b0 | 2010-06-19 09:47:18 +0000 | [diff] [blame] | 2109 | // vec_ld, vec_lvsl, vec_lvsr |
| 2110 | case PPC::BI__builtin_altivec_lvx: |
| 2111 | case PPC::BI__builtin_altivec_lvxl: |
| 2112 | case PPC::BI__builtin_altivec_lvebx: |
| 2113 | case PPC::BI__builtin_altivec_lvehx: |
| 2114 | case PPC::BI__builtin_altivec_lvewx: |
| 2115 | case PPC::BI__builtin_altivec_lvsl: |
| 2116 | case PPC::BI__builtin_altivec_lvsr: |
| 2117 | { |
| 2118 | Ops[1] = Builder.CreateBitCast(Ops[1], llvm::Type::getInt8PtrTy(VMContext)); |
| 2119 | |
| 2120 | Ops[0] = Builder.CreateGEP(Ops[1], Ops[0], "tmp"); |
| 2121 | Ops.pop_back(); |
| 2122 | |
| 2123 | switch (BuiltinID) { |
| 2124 | default: assert(0 && "Unsupported ld/lvsl/lvsr intrinsic!"); |
| 2125 | case PPC::BI__builtin_altivec_lvx: |
| 2126 | ID = Intrinsic::ppc_altivec_lvx; |
| 2127 | break; |
| 2128 | case PPC::BI__builtin_altivec_lvxl: |
| 2129 | ID = Intrinsic::ppc_altivec_lvxl; |
| 2130 | break; |
| 2131 | case PPC::BI__builtin_altivec_lvebx: |
| 2132 | ID = Intrinsic::ppc_altivec_lvebx; |
| 2133 | break; |
| 2134 | case PPC::BI__builtin_altivec_lvehx: |
| 2135 | ID = Intrinsic::ppc_altivec_lvehx; |
| 2136 | break; |
| 2137 | case PPC::BI__builtin_altivec_lvewx: |
| 2138 | ID = Intrinsic::ppc_altivec_lvewx; |
| 2139 | break; |
| 2140 | case PPC::BI__builtin_altivec_lvsl: |
| 2141 | ID = Intrinsic::ppc_altivec_lvsl; |
| 2142 | break; |
| 2143 | case PPC::BI__builtin_altivec_lvsr: |
| 2144 | ID = Intrinsic::ppc_altivec_lvsr; |
| 2145 | break; |
| 2146 | } |
| 2147 | llvm::Function *F = CGM.getIntrinsic(ID); |
| 2148 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), ""); |
| 2149 | } |
| 2150 | |
Chris Lattner | dd17394 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 2151 | // vec_st |
| 2152 | case PPC::BI__builtin_altivec_stvx: |
| 2153 | case PPC::BI__builtin_altivec_stvxl: |
| 2154 | case PPC::BI__builtin_altivec_stvebx: |
| 2155 | case PPC::BI__builtin_altivec_stvehx: |
| 2156 | case PPC::BI__builtin_altivec_stvewx: |
| 2157 | { |
| 2158 | Ops[2] = Builder.CreateBitCast(Ops[2], llvm::Type::getInt8PtrTy(VMContext)); |
Anton Korobeynikov | 4d3a7b0 | 2010-06-19 09:47:18 +0000 | [diff] [blame] | 2159 | Ops[1] = Builder.CreateGEP(Ops[2], Ops[1], "tmp"); |
Chris Lattner | dd17394 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 2160 | Ops.pop_back(); |
| 2161 | |
| 2162 | switch (BuiltinID) { |
Anton Korobeynikov | 4d3a7b0 | 2010-06-19 09:47:18 +0000 | [diff] [blame] | 2163 | default: assert(0 && "Unsupported st intrinsic!"); |
Chris Lattner | dd17394 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 2164 | case PPC::BI__builtin_altivec_stvx: |
| 2165 | ID = Intrinsic::ppc_altivec_stvx; |
| 2166 | break; |
| 2167 | case PPC::BI__builtin_altivec_stvxl: |
| 2168 | ID = Intrinsic::ppc_altivec_stvxl; |
| 2169 | break; |
| 2170 | case PPC::BI__builtin_altivec_stvebx: |
| 2171 | ID = Intrinsic::ppc_altivec_stvebx; |
| 2172 | break; |
| 2173 | case PPC::BI__builtin_altivec_stvehx: |
| 2174 | ID = Intrinsic::ppc_altivec_stvehx; |
| 2175 | break; |
| 2176 | case PPC::BI__builtin_altivec_stvewx: |
| 2177 | ID = Intrinsic::ppc_altivec_stvewx; |
| 2178 | break; |
| 2179 | } |
| 2180 | llvm::Function *F = CGM.getIntrinsic(ID); |
| 2181 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), ""); |
| 2182 | } |
| 2183 | } |
Daniel Dunbar | b0b8438 | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 2184 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | } |