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