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