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" |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 1f32999 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 18 | #include "clang/AST/APValue.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Chris Lattner | 6b15cdc | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetBuiltins.h" |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 22 | #include "llvm/Intrinsics.h" |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 28 | static void EmitMemoryBarrier(CodeGenFunction &CGF, |
| 29 | bool LoadLoad, bool LoadStore, |
| 30 | bool StoreLoad, bool StoreStore, |
| 31 | bool Device) { |
| 32 | Value *True = llvm::ConstantInt::getTrue(CGF.getLLVMContext()); |
| 33 | Value *False = llvm::ConstantInt::getFalse(CGF.getLLVMContext()); |
| 34 | Value *C[5] = { LoadLoad ? True : False, |
| 35 | LoadStore ? True : False, |
| 36 | StoreLoad ? True : False, |
| 37 | StoreStore ? True : False, |
| 38 | Device ? True : False }; |
| 39 | CGF.Builder.CreateCall(CGF.CGM.getIntrinsic(Intrinsic::memory_barrier), |
| 40 | C, C + 5); |
| 41 | } |
| 42 | |
| 43 | // The atomic builtins are also full memory barriers. This is a utility for |
| 44 | // wrapping a call to the builtins with memory barriers. |
| 45 | static Value *EmitCallWithBarrier(CodeGenFunction &CGF, Value *Fn, |
| 46 | Value **ArgBegin, Value **ArgEnd) { |
| 47 | // FIXME: We need a target hook for whether this applies to device memory or |
| 48 | // not. |
| 49 | bool Device = true; |
| 50 | |
| 51 | // Create barriers both before and after the call. |
| 52 | EmitMemoryBarrier(CGF, true, true, true, true, Device); |
| 53 | Value *Result = CGF.Builder.CreateCall(Fn, ArgBegin, ArgEnd); |
| 54 | EmitMemoryBarrier(CGF, true, true, true, true, Device); |
| 55 | return Result; |
| 56 | } |
| 57 | |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 58 | /// Utility to insert an atomic instruction based on Instrinsic::ID |
| 59 | /// and the expression node. |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 60 | static RValue EmitBinaryAtomic(CodeGenFunction &CGF, |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 61 | Intrinsic::ID Id, const CallExpr *E) { |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 62 | Value *Args[2] = { CGF.EmitScalarExpr(E->getArg(0)), |
| 63 | CGF.EmitScalarExpr(E->getArg(1)) }; |
Mon P Wang | c500451 | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 64 | const llvm::Type *ResType[2]; |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 65 | ResType[0] = CGF.ConvertType(E->getType()); |
| 66 | ResType[1] = CGF.ConvertType(E->getArg(0)->getType()); |
| 67 | Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2); |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 68 | return RValue::get(EmitCallWithBarrier(CGF, AtomF, Args, Args + 2)); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// Utility to insert an atomic instruction based Instrinsic::ID and |
| 72 | // the expression node, where the return value is the result of the |
| 73 | // operation. |
Chris Lattner | 420b118 | 2010-05-06 05:35:16 +0000 | [diff] [blame] | 74 | static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 75 | Intrinsic::ID Id, const CallExpr *E, |
| 76 | Instruction::BinaryOps Op) { |
| 77 | const llvm::Type *ResType[2]; |
| 78 | ResType[0] = CGF.ConvertType(E->getType()); |
| 79 | ResType[1] = CGF.ConvertType(E->getArg(0)->getType()); |
| 80 | Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2); |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 81 | Value *Args[2] = { CGF.EmitScalarExpr(E->getArg(0)), |
| 82 | CGF.EmitScalarExpr(E->getArg(1)) }; |
| 83 | Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 2); |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 84 | return RValue::get(CGF.Builder.CreateBinOp(Op, Result, Args[1])); |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 85 | } |
| 86 | |
John McCall | fb17a56 | 2010-03-03 10:30:05 +0000 | [diff] [blame] | 87 | static llvm::ConstantInt *getInt32(llvm::LLVMContext &Context, int32_t Value) { |
| 88 | return llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), Value); |
| 89 | } |
| 90 | |
Chris Lattner | 420b118 | 2010-05-06 05:35:16 +0000 | [diff] [blame] | 91 | |
| 92 | /// EmitFAbs - Emit a call to fabs/fabsf/fabsl, depending on the type of ValTy, |
| 93 | /// which must be a scalar floating point type. |
| 94 | static Value *EmitFAbs(CodeGenFunction &CGF, Value *V, QualType ValTy) { |
| 95 | const BuiltinType *ValTyP = ValTy->getAs<BuiltinType>(); |
| 96 | assert(ValTyP && "isn't scalar fp type!"); |
| 97 | |
| 98 | StringRef FnName; |
| 99 | switch (ValTyP->getKind()) { |
| 100 | default: assert(0 && "Isn't a scalar fp type!"); |
| 101 | case BuiltinType::Float: FnName = "fabsf"; break; |
| 102 | case BuiltinType::Double: FnName = "fabs"; break; |
| 103 | case BuiltinType::LongDouble: FnName = "fabsl"; break; |
| 104 | } |
| 105 | |
| 106 | // The prototype is something that takes and returns whatever V's type is. |
| 107 | std::vector<const llvm::Type*> Args; |
| 108 | Args.push_back(V->getType()); |
| 109 | llvm::FunctionType *FT = llvm::FunctionType::get(V->getType(), Args, false); |
| 110 | llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction(FT, FnName); |
| 111 | |
| 112 | return CGF.Builder.CreateCall(Fn, V, "abs"); |
| 113 | } |
| 114 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 116 | unsigned BuiltinID, const CallExpr *E) { |
Chris Lattner | 564ea2a | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 117 | // 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] | 118 | Expr::EvalResult Result; |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 119 | if (E->Evaluate(Result, CGM.getContext())) { |
Anders Carlsson | f35d35a | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 120 | if (Result.Val.isInt()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | return RValue::get(llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 122 | Result.Val.getInt())); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 123 | else if (Result.Val.isFloat()) |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 124 | return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat())); |
Chris Lattner | 1f32999 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 564ea2a | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 127 | switch (BuiltinID) { |
| 128 | default: break; // Handle intrinsics and libm functions below. |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 129 | case Builtin::BI__builtin___CFStringMakeConstantString: |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 130 | case Builtin::BI__builtin___NSStringMakeConstantString: |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 131 | return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0)); |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 132 | case Builtin::BI__builtin_stdarg_start: |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 133 | case Builtin::BI__builtin_va_start: |
| 134 | case Builtin::BI__builtin_va_end: { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 135 | Value *ArgValue = EmitVAListRef(E->getArg(0)); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 136 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 137 | if (ArgValue->getType() != DestType) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | ArgValue = Builder.CreateBitCast(ArgValue, DestType, |
Daniel Dunbar | b27ffbe | 2009-07-26 09:28:40 +0000 | [diff] [blame] | 139 | ArgValue->getName().data()); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 140 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ? |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 142 | Intrinsic::vaend : Intrinsic::vastart; |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 143 | return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 144 | } |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 145 | case Builtin::BI__builtin_va_copy: { |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 146 | Value *DstPtr = EmitVAListRef(E->getArg(0)); |
| 147 | Value *SrcPtr = EmitVAListRef(E->getArg(1)); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 148 | |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 149 | const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 150 | |
| 151 | DstPtr = Builder.CreateBitCast(DstPtr, Type); |
| 152 | SrcPtr = Builder.CreateBitCast(SrcPtr, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 154 | DstPtr, SrcPtr)); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 155 | } |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 156 | case Builtin::BI__builtin_abs: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 158 | |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 159 | Value *NegOp = Builder.CreateNeg(ArgValue, "neg"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | Value *CmpResult = |
| 161 | Builder.CreateICmpSGE(ArgValue, |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 162 | llvm::Constant::getNullValue(ArgValue->getType()), |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 163 | "abscond"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | Value *Result = |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 165 | Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 167 | return RValue::get(Result); |
| 168 | } |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 169 | case Builtin::BI__builtin_ctz: |
| 170 | case Builtin::BI__builtin_ctzl: |
| 171 | case Builtin::BI__builtin_ctzll: { |
| 172 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 174 | const llvm::Type *ArgType = ArgValue->getType(); |
| 175 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
| 176 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 178 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 179 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 180 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 181 | "cast"); |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 182 | return RValue::get(Result); |
| 183 | } |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 184 | case Builtin::BI__builtin_clz: |
| 185 | case Builtin::BI__builtin_clzl: |
| 186 | case Builtin::BI__builtin_clzll: { |
| 187 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 189 | const llvm::Type *ArgType = ArgValue->getType(); |
| 190 | Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1); |
| 191 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 193 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 194 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 195 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 196 | "cast"); |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 197 | return RValue::get(Result); |
| 198 | } |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 199 | case Builtin::BI__builtin_ffs: |
| 200 | case Builtin::BI__builtin_ffsl: |
| 201 | case Builtin::BI__builtin_ffsll: { |
| 202 | // ffs(x) -> x ? cttz(x) + 1 : 0 |
| 203 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 205 | const llvm::Type *ArgType = ArgValue->getType(); |
| 206 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 208 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 210 | llvm::ConstantInt::get(ArgType, 1), "tmp"); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 211 | Value *Zero = llvm::Constant::getNullValue(ArgType); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 212 | Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); |
| 213 | Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs"); |
| 214 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 215 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 216 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 217 | return RValue::get(Result); |
| 218 | } |
| 219 | case Builtin::BI__builtin_parity: |
| 220 | case Builtin::BI__builtin_parityl: |
| 221 | case Builtin::BI__builtin_parityll: { |
| 222 | // parity(x) -> ctpop(x) & 1 |
| 223 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 225 | const llvm::Type *ArgType = ArgValue->getType(); |
| 226 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 228 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 229 | Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1), |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 231 | "tmp"); |
| 232 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 233 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 234 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 235 | return RValue::get(Result); |
| 236 | } |
| 237 | case Builtin::BI__builtin_popcount: |
| 238 | case Builtin::BI__builtin_popcountl: |
| 239 | case Builtin::BI__builtin_popcountll: { |
| 240 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 242 | const llvm::Type *ArgType = ArgValue->getType(); |
| 243 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 245 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 246 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 247 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 248 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 249 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 250 | return RValue::get(Result); |
| 251 | } |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 252 | case Builtin::BI__builtin_expect: |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 253 | // FIXME: pass expect through to LLVM |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 254 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 255 | case Builtin::BI__builtin_bswap32: |
| 256 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 257 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 258 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 259 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 260 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | } |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 262 | case Builtin::BI__builtin_object_size: { |
Mike Stump | b16d32f | 2009-10-26 23:39:48 +0000 | [diff] [blame] | 263 | // We pass this builtin onto the optimizer so that it can |
| 264 | // figure out the object size in more complex cases. |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 265 | const llvm::Type *ResType[] = { |
| 266 | ConvertType(E->getType()) |
| 267 | }; |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 268 | |
| 269 | // LLVM only supports 0 and 2, make sure that we pass along that |
| 270 | // as a boolean. |
| 271 | Value *Ty = EmitScalarExpr(E->getArg(1)); |
| 272 | ConstantInt *CI = dyn_cast<ConstantInt>(Ty); |
| 273 | assert(CI); |
| 274 | uint64_t val = CI->getZExtValue(); |
| 275 | CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1); |
| 276 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 277 | Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1); |
| 278 | return RValue::get(Builder.CreateCall2(F, |
| 279 | EmitScalarExpr(E->getArg(0)), |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 280 | CI)); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 281 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 282 | case Builtin::BI__builtin_prefetch: { |
| 283 | Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); |
| 284 | // FIXME: Technically these constants should of type 'int', yes? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 286 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 288 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 3); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 289 | Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); |
| 290 | return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 291 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 292 | case Builtin::BI__builtin_trap: { |
| 293 | Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); |
| 294 | return RValue::get(Builder.CreateCall(F)); |
| 295 | } |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 296 | case Builtin::BI__builtin_unreachable: { |
Mike Stump | fba565d | 2009-12-16 03:07:12 +0000 | [diff] [blame] | 297 | if (CatchUndefined && HaveInsertPoint()) |
| 298 | EmitBranch(getTrapBB()); |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 299 | Value *V = Builder.CreateUnreachable(); |
| 300 | Builder.ClearInsertionPoint(); |
| 301 | return RValue::get(V); |
| 302 | } |
| 303 | |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 304 | case Builtin::BI__builtin_powi: |
| 305 | case Builtin::BI__builtin_powif: |
| 306 | case Builtin::BI__builtin_powil: { |
| 307 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 308 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 309 | const llvm::Type *ArgType = Base->getType(); |
| 310 | Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 311 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 312 | } |
| 313 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 314 | case Builtin::BI__builtin_isgreater: |
| 315 | case Builtin::BI__builtin_isgreaterequal: |
| 316 | case Builtin::BI__builtin_isless: |
| 317 | case Builtin::BI__builtin_islessequal: |
| 318 | case Builtin::BI__builtin_islessgreater: |
| 319 | case Builtin::BI__builtin_isunordered: { |
| 320 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 321 | // floating point values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 323 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 325 | switch (BuiltinID) { |
| 326 | default: assert(0 && "Unknown ordered comparison"); |
| 327 | case Builtin::BI__builtin_isgreater: |
| 328 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 329 | break; |
| 330 | case Builtin::BI__builtin_isgreaterequal: |
| 331 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 332 | break; |
| 333 | case Builtin::BI__builtin_isless: |
| 334 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 335 | break; |
| 336 | case Builtin::BI__builtin_islessequal: |
| 337 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 338 | break; |
| 339 | case Builtin::BI__builtin_islessgreater: |
| 340 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 341 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | case Builtin::BI__builtin_isunordered: |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 343 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 344 | break; |
| 345 | } |
| 346 | // ZExt bool to int type. |
| 347 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 348 | "tmp")); |
| 349 | } |
Eli Friedman | d613989 | 2009-09-01 04:19:44 +0000 | [diff] [blame] | 350 | case Builtin::BI__builtin_isnan: { |
| 351 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 352 | V = Builder.CreateFCmpUNO(V, V, "cmp"); |
| 353 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp")); |
| 354 | } |
Chris Lattner | 420b118 | 2010-05-06 05:35:16 +0000 | [diff] [blame] | 355 | |
| 356 | case Builtin::BI__builtin_isinf: { |
| 357 | // isinf(x) --> fabs(x) == infinity |
| 358 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 359 | V = EmitFAbs(*this, V, E->getArg(0)->getType()); |
| 360 | |
| 361 | V = Builder.CreateFCmpOEQ(V, ConstantFP::getInfinity(V->getType()),"isinf"); |
| 362 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp")); |
| 363 | } |
Chris Lattner | 58ae5b4 | 2010-05-06 06:13:53 +0000 | [diff] [blame] | 364 | |
| 365 | // TODO: BI__builtin_isinf_sign |
| 366 | // isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 |
Benjamin Kramer | 6349ce9 | 2010-05-19 11:24:26 +0000 | [diff] [blame] | 367 | |
| 368 | case Builtin::BI__builtin_isnormal: { |
| 369 | // isnormal(x) --> x == x && fabsf(x) < infinity && fabsf(x) >= float_min |
| 370 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 371 | Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq"); |
| 372 | |
| 373 | Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType()); |
| 374 | Value *IsLessThanInf = |
| 375 | Builder.CreateFCmpULT(Abs, ConstantFP::getInfinity(V->getType()),"isinf"); |
| 376 | APFloat Smallest = APFloat::getSmallestNormalized( |
| 377 | getContext().getFloatTypeSemantics(E->getArg(0)->getType())); |
| 378 | Value *IsNormal = |
| 379 | Builder.CreateFCmpUGE(Abs, ConstantFP::get(V->getContext(), Smallest), |
| 380 | "isnormal"); |
| 381 | V = Builder.CreateAnd(Eq, IsLessThanInf, "and"); |
| 382 | V = Builder.CreateAnd(V, IsNormal, "and"); |
| 383 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()))); |
| 384 | } |
| 385 | |
Chris Lattner | ed07415 | 2010-05-06 06:04:13 +0000 | [diff] [blame] | 386 | case Builtin::BI__builtin_isfinite: { |
| 387 | // isfinite(x) --> x == x && fabs(x) != infinity; } |
| 388 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 389 | Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq"); |
| 390 | |
| 391 | Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType()); |
| 392 | Value *IsNotInf = |
| 393 | Builder.CreateFCmpUNE(Abs, ConstantFP::getInfinity(V->getType()),"isinf"); |
| 394 | |
| 395 | V = Builder.CreateAnd(Eq, IsNotInf, "and"); |
| 396 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()))); |
| 397 | } |
| 398 | |
Eli Friedman | b52fe9c | 2009-06-02 07:10:30 +0000 | [diff] [blame] | 399 | case Builtin::BIalloca: |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 400 | case Builtin::BI__builtin_alloca: { |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 401 | Value *Size = EmitScalarExpr(E->getArg(0)); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 402 | return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp")); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 403 | } |
Eli Friedman | e6dddfd | 2010-01-23 19:00:10 +0000 | [diff] [blame] | 404 | case Builtin::BIbzero: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 405 | case Builtin::BI__builtin_bzero: { |
| 406 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 407 | Value *SizeVal = EmitScalarExpr(E->getArg(1)); |
| 408 | Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()), |
| 409 | Address, |
| 410 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0), |
| 411 | SizeVal, |
| 412 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1), |
| 413 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 414 | return RValue::get(Address); |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 415 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 416 | case Builtin::BImemcpy: |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 417 | case Builtin::BI__builtin_memcpy: { |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 418 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 419 | Value *SrcAddr = EmitScalarExpr(E->getArg(1)); |
| 420 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 421 | Builder.CreateCall5(CGM.getMemCpyFn(Address->getType(), SrcAddr->getType(), |
| 422 | SizeVal->getType()), |
| 423 | Address, SrcAddr, SizeVal, |
| 424 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1), |
| 425 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 426 | return RValue::get(Address); |
| 427 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 428 | case Builtin::BImemmove: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 429 | case Builtin::BI__builtin_memmove: { |
| 430 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 431 | Value *SrcAddr = EmitScalarExpr(E->getArg(1)); |
| 432 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 433 | Builder.CreateCall5(CGM.getMemMoveFn(Address->getType(), SrcAddr->getType(), |
| 434 | SizeVal->getType()), |
| 435 | Address, SrcAddr, SizeVal, |
| 436 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1), |
| 437 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 438 | return RValue::get(Address); |
| 439 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 440 | case Builtin::BImemset: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 441 | case Builtin::BI__builtin_memset: { |
| 442 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 443 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 444 | Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()), |
| 445 | Address, |
| 446 | Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), |
| 447 | llvm::Type::getInt8Ty(VMContext)), |
| 448 | SizeVal, |
| 449 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1), |
| 450 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 451 | return RValue::get(Address); |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 452 | } |
John McCall | fb17a56 | 2010-03-03 10:30:05 +0000 | [diff] [blame] | 453 | case Builtin::BI__builtin_dwarf_cfa: { |
| 454 | // The offset in bytes from the first argument to the CFA. |
| 455 | // |
| 456 | // Why on earth is this in the frontend? Is there any reason at |
| 457 | // all that the backend can't reasonably determine this while |
| 458 | // lowering llvm.eh.dwarf.cfa()? |
| 459 | // |
| 460 | // TODO: If there's a satisfactory reason, add a target hook for |
| 461 | // this instead of hard-coding 0, which is correct for most targets. |
| 462 | int32_t Offset = 0; |
| 463 | |
| 464 | Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa, 0, 0); |
| 465 | return RValue::get(Builder.CreateCall(F, getInt32(VMContext, Offset))); |
| 466 | } |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 467 | case Builtin::BI__builtin_return_address: { |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 468 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 469 | Depth = Builder.CreateIntCast(Depth, |
| 470 | llvm::Type::getInt32Ty(VMContext), |
| 471 | false, "tmp"); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 472 | Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 473 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 474 | } |
| 475 | case Builtin::BI__builtin_frame_address: { |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 476 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 477 | Depth = Builder.CreateIntCast(Depth, |
| 478 | llvm::Type::getInt32Ty(VMContext), |
| 479 | false, "tmp"); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 480 | Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 481 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 482 | } |
Eli Friedman | 3b660ef | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 483 | case Builtin::BI__builtin_extract_return_addr: { |
John McCall | 492c4f9 | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 484 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 485 | Value *Result = getTargetHooks().decodeReturnAddress(*this, Address); |
| 486 | return RValue::get(Result); |
| 487 | } |
| 488 | case Builtin::BI__builtin_frob_return_addr: { |
| 489 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 490 | Value *Result = getTargetHooks().encodeReturnAddress(*this, Address); |
| 491 | return RValue::get(Result); |
Eli Friedman | 3b660ef | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 492 | } |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 493 | case Builtin::BI__builtin_dwarf_sp_column: { |
| 494 | const llvm::IntegerType *Ty |
| 495 | = cast<llvm::IntegerType>(ConvertType(E->getType())); |
| 496 | int Column = getTargetHooks().getDwarfEHStackPointer(CGM); |
| 497 | if (Column == -1) { |
| 498 | CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column"); |
| 499 | return RValue::get(llvm::UndefValue::get(Ty)); |
| 500 | } |
| 501 | return RValue::get(llvm::ConstantInt::get(Ty, Column, true)); |
| 502 | } |
| 503 | case Builtin::BI__builtin_init_dwarf_reg_size_table: { |
| 504 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 505 | if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address)) |
| 506 | CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table"); |
| 507 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
| 508 | } |
John McCall | 7ada111 | 2010-03-03 05:38:58 +0000 | [diff] [blame] | 509 | case Builtin::BI__builtin_eh_return: { |
| 510 | Value *Int = EmitScalarExpr(E->getArg(0)); |
| 511 | Value *Ptr = EmitScalarExpr(E->getArg(1)); |
| 512 | |
| 513 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType()); |
| 514 | assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) && |
| 515 | "LLVM's __builtin_eh_return only supports 32- and 64-bit variants"); |
| 516 | Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32 |
| 517 | ? Intrinsic::eh_return_i32 |
| 518 | : Intrinsic::eh_return_i64, |
| 519 | 0, 0); |
| 520 | Builder.CreateCall2(F, Int, Ptr); |
| 521 | Value *V = Builder.CreateUnreachable(); |
| 522 | Builder.ClearInsertionPoint(); |
| 523 | return RValue::get(V); |
| 524 | } |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 525 | case Builtin::BI__builtin_unwind_init: { |
| 526 | Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0); |
| 527 | return RValue::get(Builder.CreateCall(F)); |
| 528 | } |
John McCall | 5e11085 | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 529 | case Builtin::BI__builtin_extend_pointer: { |
| 530 | // Extends a pointer to the size of an _Unwind_Word, which is |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 531 | // uint64_t on all platforms. Generally this gets poked into a |
| 532 | // register and eventually used as an address, so if the |
| 533 | // addressing registers are wider than pointers and the platform |
| 534 | // doesn't implicitly ignore high-order bits when doing |
| 535 | // addressing, we need to make sure we zext / sext based on |
| 536 | // the platform's expectations. |
John McCall | 5e11085 | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 537 | // |
| 538 | // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 539 | |
| 540 | LLVMContext &C = CGM.getLLVMContext(); |
| 541 | |
| 542 | // Cast the pointer to intptr_t. |
John McCall | 5e11085 | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 543 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 544 | const llvm::IntegerType *IntPtrTy = CGM.getTargetData().getIntPtrType(C); |
| 545 | Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast"); |
| 546 | |
| 547 | // If that's 64 bits, we're done. |
| 548 | if (IntPtrTy->getBitWidth() == 64) |
| 549 | return RValue::get(Result); |
| 550 | |
| 551 | // Otherwise, ask the codegen data what to do. |
| 552 | const llvm::IntegerType *Int64Ty = llvm::IntegerType::get(C, 64); |
John McCall | 492c4f9 | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 553 | if (getTargetHooks().extendPointerWithSExt()) |
John McCall | d0b76ca | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 554 | return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext")); |
| 555 | else |
| 556 | return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext")); |
John McCall | 5e11085 | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 557 | } |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 558 | case Builtin::BI__builtin_setjmp: { |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 559 | // Buffer is a void**. |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 560 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 561 | |
| 562 | // Store the frame pointer to the setjmp buffer. |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 563 | Value *FrameAddr = |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 564 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::frameaddress), |
| 565 | ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0)); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 566 | Builder.CreateStore(FrameAddr, Buf); |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 567 | |
Jim Grosbach | 6d172e2 | 2010-05-27 23:54:20 +0000 | [diff] [blame] | 568 | // Store the stack pointer to the setjmp buffer. |
| 569 | Value *StackAddr = |
| 570 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave)); |
| 571 | Value *StackSaveSlot = |
| 572 | Builder.CreateGEP(Buf, ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 573 | 2)); |
| 574 | Builder.CreateStore(StackAddr, StackSaveSlot); |
| 575 | |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 576 | // Call LLVM's EH setjmp, which is lightweight. |
| 577 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp); |
| 578 | Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext)); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 579 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 580 | } |
| 581 | case Builtin::BI__builtin_longjmp: { |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 582 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
John McCall | 78673d9 | 2010-05-27 18:47:06 +0000 | [diff] [blame] | 583 | Buf = Builder.CreateBitCast(Buf, llvm::Type::getInt8PtrTy(VMContext)); |
| 584 | |
| 585 | // Call LLVM's EH longjmp, which is lightweight. |
| 586 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf); |
| 587 | |
| 588 | // longjmp doesn't return; mark this as unreachable |
| 589 | Value *V = Builder.CreateUnreachable(); |
| 590 | Builder.ClearInsertionPoint(); |
| 591 | return RValue::get(V); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 592 | } |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 593 | case Builtin::BI__sync_fetch_and_add: |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 594 | case Builtin::BI__sync_fetch_and_sub: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 595 | case Builtin::BI__sync_fetch_and_or: |
| 596 | case Builtin::BI__sync_fetch_and_and: |
| 597 | case Builtin::BI__sync_fetch_and_xor: |
| 598 | case Builtin::BI__sync_add_and_fetch: |
| 599 | case Builtin::BI__sync_sub_and_fetch: |
| 600 | case Builtin::BI__sync_and_and_fetch: |
| 601 | case Builtin::BI__sync_or_and_fetch: |
| 602 | case Builtin::BI__sync_xor_and_fetch: |
| 603 | case Builtin::BI__sync_val_compare_and_swap: |
| 604 | case Builtin::BI__sync_bool_compare_and_swap: |
| 605 | case Builtin::BI__sync_lock_test_and_set: |
| 606 | case Builtin::BI__sync_lock_release: |
| 607 | assert(0 && "Shouldn't make it through sema"); |
| 608 | case Builtin::BI__sync_fetch_and_add_1: |
| 609 | case Builtin::BI__sync_fetch_and_add_2: |
| 610 | case Builtin::BI__sync_fetch_and_add_4: |
| 611 | case Builtin::BI__sync_fetch_and_add_8: |
| 612 | case Builtin::BI__sync_fetch_and_add_16: |
| 613 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); |
| 614 | case Builtin::BI__sync_fetch_and_sub_1: |
| 615 | case Builtin::BI__sync_fetch_and_sub_2: |
| 616 | case Builtin::BI__sync_fetch_and_sub_4: |
| 617 | case Builtin::BI__sync_fetch_and_sub_8: |
| 618 | case Builtin::BI__sync_fetch_and_sub_16: |
Mon P Wang | 09b6bf5 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 619 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 620 | case Builtin::BI__sync_fetch_and_or_1: |
| 621 | case Builtin::BI__sync_fetch_and_or_2: |
| 622 | case Builtin::BI__sync_fetch_and_or_4: |
| 623 | case Builtin::BI__sync_fetch_and_or_8: |
| 624 | case Builtin::BI__sync_fetch_and_or_16: |
| 625 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 626 | case Builtin::BI__sync_fetch_and_and_1: |
| 627 | case Builtin::BI__sync_fetch_and_and_2: |
| 628 | case Builtin::BI__sync_fetch_and_and_4: |
| 629 | case Builtin::BI__sync_fetch_and_and_8: |
| 630 | case Builtin::BI__sync_fetch_and_and_16: |
| 631 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 632 | case Builtin::BI__sync_fetch_and_xor_1: |
| 633 | case Builtin::BI__sync_fetch_and_xor_2: |
| 634 | case Builtin::BI__sync_fetch_and_xor_4: |
| 635 | case Builtin::BI__sync_fetch_and_xor_8: |
| 636 | case Builtin::BI__sync_fetch_and_xor_16: |
| 637 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 639 | // Clang extensions: not overloaded yet. |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 640 | case Builtin::BI__sync_fetch_and_min: |
| 641 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 642 | case Builtin::BI__sync_fetch_and_max: |
| 643 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 644 | case Builtin::BI__sync_fetch_and_umin: |
| 645 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 646 | case Builtin::BI__sync_fetch_and_umax: |
| 647 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 649 | case Builtin::BI__sync_add_and_fetch_1: |
| 650 | case Builtin::BI__sync_add_and_fetch_2: |
| 651 | case Builtin::BI__sync_add_and_fetch_4: |
| 652 | case Builtin::BI__sync_add_and_fetch_8: |
| 653 | case Builtin::BI__sync_add_and_fetch_16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 655 | llvm::Instruction::Add); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 656 | case Builtin::BI__sync_sub_and_fetch_1: |
| 657 | case Builtin::BI__sync_sub_and_fetch_2: |
| 658 | case Builtin::BI__sync_sub_and_fetch_4: |
| 659 | case Builtin::BI__sync_sub_and_fetch_8: |
| 660 | case Builtin::BI__sync_sub_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 661 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, |
| 662 | llvm::Instruction::Sub); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 663 | case Builtin::BI__sync_and_and_fetch_1: |
| 664 | case Builtin::BI__sync_and_and_fetch_2: |
| 665 | case Builtin::BI__sync_and_and_fetch_4: |
| 666 | case Builtin::BI__sync_and_and_fetch_8: |
| 667 | case Builtin::BI__sync_and_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 668 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, |
| 669 | llvm::Instruction::And); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 670 | case Builtin::BI__sync_or_and_fetch_1: |
| 671 | case Builtin::BI__sync_or_and_fetch_2: |
| 672 | case Builtin::BI__sync_or_and_fetch_4: |
| 673 | case Builtin::BI__sync_or_and_fetch_8: |
| 674 | case Builtin::BI__sync_or_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 675 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, |
| 676 | llvm::Instruction::Or); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 677 | case Builtin::BI__sync_xor_and_fetch_1: |
| 678 | case Builtin::BI__sync_xor_and_fetch_2: |
| 679 | case Builtin::BI__sync_xor_and_fetch_4: |
| 680 | case Builtin::BI__sync_xor_and_fetch_8: |
| 681 | case Builtin::BI__sync_xor_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 682 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, |
| 683 | llvm::Instruction::Xor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 685 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 686 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 687 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 688 | case Builtin::BI__sync_val_compare_and_swap_8: |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 689 | case Builtin::BI__sync_val_compare_and_swap_16: { |
Mon P Wang | c500451 | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 690 | const llvm::Type *ResType[2]; |
| 691 | ResType[0]= ConvertType(E->getType()); |
| 692 | ResType[1] = ConvertType(E->getArg(0)->getType()); |
| 693 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 694 | Value *Args[3] = { EmitScalarExpr(E->getArg(0)), |
| 695 | EmitScalarExpr(E->getArg(1)), |
| 696 | EmitScalarExpr(E->getArg(2)) }; |
| 697 | return RValue::get(EmitCallWithBarrier(*this, AtomF, Args, Args + 3)); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 698 | } |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 700 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 701 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 702 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 703 | case Builtin::BI__sync_bool_compare_and_swap_8: |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 704 | case Builtin::BI__sync_bool_compare_and_swap_16: { |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 705 | const llvm::Type *ResType[2]; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 706 | ResType[0]= ConvertType(E->getArg(1)->getType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 707 | ResType[1] = llvm::PointerType::getUnqual(ResType[0]); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 708 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
| 709 | Value *OldVal = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 710 | Value *Args[3] = { EmitScalarExpr(E->getArg(0)), |
| 711 | OldVal, |
| 712 | EmitScalarExpr(E->getArg(2)) }; |
| 713 | Value *PrevVal = EmitCallWithBarrier(*this, AtomF, Args, Args + 3); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 714 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 715 | // zext bool to int. |
| 716 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 717 | } |
| 718 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 719 | case Builtin::BI__sync_lock_test_and_set_1: |
| 720 | case Builtin::BI__sync_lock_test_and_set_2: |
| 721 | case Builtin::BI__sync_lock_test_and_set_4: |
| 722 | case Builtin::BI__sync_lock_test_and_set_8: |
| 723 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 724 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 726 | case Builtin::BI__sync_lock_release_1: |
| 727 | case Builtin::BI__sync_lock_release_2: |
| 728 | case Builtin::BI__sync_lock_release_4: |
| 729 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 730 | case Builtin::BI__sync_lock_release_16: { |
| 731 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 732 | const llvm::Type *ElTy = |
| 733 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Daniel Dunbar | 007b567 | 2009-11-29 21:11:47 +0000 | [diff] [blame] | 734 | llvm::StoreInst *Store = |
| 735 | Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr); |
| 736 | Store->setVolatile(true); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 737 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 738 | } |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 739 | |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 740 | case Builtin::BI__sync_synchronize: { |
Daniel Dunbar | cb61a7b | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 741 | // We assume like gcc appears to, that this only applies to cached memory. |
| 742 | EmitMemoryBarrier(*this, true, true, true, true, false); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 743 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 744 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Tanya Lattner | 0b57164 | 2010-01-16 01:21:14 +0000 | [diff] [blame] | 746 | case Builtin::BI__builtin_llvm_memory_barrier: { |
| 747 | Value *C[5] = { |
| 748 | EmitScalarExpr(E->getArg(0)), |
| 749 | EmitScalarExpr(E->getArg(1)), |
| 750 | EmitScalarExpr(E->getArg(2)), |
| 751 | EmitScalarExpr(E->getArg(3)), |
| 752 | EmitScalarExpr(E->getArg(4)) |
| 753 | }; |
| 754 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
| 755 | return RValue::get(0); |
| 756 | } |
| 757 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 758 | // Library functions with special handling. |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 759 | case Builtin::BIsqrt: |
| 760 | case Builtin::BIsqrtf: |
| 761 | case Builtin::BIsqrtl: { |
John McCall | beb4128 | 2010-04-07 08:20:20 +0000 | [diff] [blame] | 762 | // TODO: there is currently no set of optimizer flags |
| 763 | // sufficient for us to rewrite sqrt to @llvm.sqrt. |
| 764 | // -fmath-errno=0 is not good enough; we need finiteness. |
| 765 | // We could probably precondition the call with an ult |
| 766 | // against 0, but is that worth the complexity? |
| 767 | break; |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | case Builtin::BIpow: |
| 771 | case Builtin::BIpowf: |
| 772 | case Builtin::BIpowl: { |
| 773 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 774 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 775 | break; |
| 776 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 777 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 778 | const llvm::Type *ArgType = Base->getType(); |
| 779 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 780 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 781 | } |
Eli Friedman | ba68b08 | 2010-03-06 02:17:52 +0000 | [diff] [blame] | 782 | |
| 783 | case Builtin::BI__builtin_signbit: |
| 784 | case Builtin::BI__builtin_signbitf: |
| 785 | case Builtin::BI__builtin_signbitl: { |
| 786 | LLVMContext &C = CGM.getLLVMContext(); |
| 787 | |
| 788 | Value *Arg = EmitScalarExpr(E->getArg(0)); |
| 789 | const llvm::Type *ArgTy = Arg->getType(); |
| 790 | if (ArgTy->isPPC_FP128Ty()) |
| 791 | break; // FIXME: I'm not sure what the right implementation is here. |
| 792 | int ArgWidth = ArgTy->getPrimitiveSizeInBits(); |
| 793 | const llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth); |
| 794 | Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy); |
| 795 | Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy); |
| 796 | Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp); |
| 797 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 798 | } |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 799 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 801 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 802 | // that function. |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 803 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 804 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 805 | return EmitCall(E->getCallee()->getType(), |
| 806 | CGM.getBuiltinLibFunction(FD, BuiltinID), |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 807 | ReturnValueSlot(), |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 808 | E->arg_begin(), E->arg_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 810 | // See if we have a target specific intrinsic. |
Dale Johannesen | a6f80ef | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 811 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 812 | Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic; |
| 813 | if (const char *Prefix = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 814 | llvm::Triple::getArchTypePrefix(Target.getTriple().getArch())) |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 815 | IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 817 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 818 | SmallVector<Value*, 16> Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 820 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 821 | const llvm::FunctionType *FTy = F->getFunctionType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 823 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 824 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 826 | // If the intrinsic arg type is different from the builtin arg type |
| 827 | // we need to do a bit cast. |
| 828 | const llvm::Type *PTy = FTy->getParamType(i); |
| 829 | if (PTy != ArgValue->getType()) { |
| 830 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 831 | "Must be able to losslessly bit cast to param"); |
| 832 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 835 | Args.push_back(ArgValue); |
| 836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 838 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 839 | QualType BuiltinRetType = E->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 841 | const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 842 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 844 | if (RetTy != V->getType()) { |
| 845 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 846 | "Must be able to losslessly bit cast result type"); |
| 847 | V = Builder.CreateBitCast(V, RetTy); |
| 848 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 850 | return RValue::get(V); |
| 851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 853 | // 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] | 854 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 855 | return RValue::get(V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 857 | ErrorUnsupported(E, "builtin function"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 859 | // Unknown builtin, for now just dump it out and return undef. |
| 860 | if (hasAggregateLLVMType(E->getType())) |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 861 | return RValue::getAggregate(CreateMemTemp(E->getType())); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 862 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 864 | |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 865 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 866 | const CallExpr *E) { |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 867 | switch (Target.getTriple().getArch()) { |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 868 | case llvm::Triple::arm: |
| 869 | case llvm::Triple::thumb: |
| 870 | return EmitARMBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 871 | case llvm::Triple::x86: |
| 872 | case llvm::Triple::x86_64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 873 | return EmitX86BuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 874 | case llvm::Triple::ppc: |
| 875 | case llvm::Triple::ppc64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 876 | return EmitPPCBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 877 | default: |
| 878 | return 0; |
| 879 | } |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 882 | const llvm::Type *GetNeonType(LLVMContext &Ctx, unsigned type, bool q) { |
| 883 | switch (type) { |
| 884 | default: break; |
| 885 | case 0: |
| 886 | case 5: return llvm::VectorType::get(llvm::Type::getInt8Ty(Ctx), 8 << q); |
| 887 | case 6: |
| 888 | case 7: |
| 889 | case 1: return llvm::VectorType::get(llvm::Type::getInt16Ty(Ctx), 4 << q); |
| 890 | case 2: return llvm::VectorType::get(llvm::Type::getInt32Ty(Ctx), 2 << q); |
| 891 | case 3: return llvm::VectorType::get(llvm::Type::getInt64Ty(Ctx), 1 << q); |
| 892 | case 4: return llvm::VectorType::get(llvm::Type::getFloatTy(Ctx), 2 << q); |
| 893 | }; |
| 894 | return 0; |
| 895 | } |
| 896 | |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 897 | Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl<Value*> &Ops, |
| 898 | const char *name) { |
| 899 | unsigned j = 0; |
| 900 | for (Function::const_arg_iterator ai = F->arg_begin(), ae = F->arg_end(); |
| 901 | ai != ae; ++ai, ++j) |
| 902 | Ops[j] = Builder.CreateBitCast(Ops[j], ai->getType(), name); |
| 903 | |
| 904 | return Builder.CreateCall(F, Ops.begin(), Ops.end(), name); |
| 905 | } |
| 906 | |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 907 | Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, |
| 908 | const CallExpr *E) { |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 909 | if (BuiltinID == ARM::BI__clear_cache) { |
Rafael Espindola | 79ba509 | 2010-06-07 17:26:50 +0000 | [diff] [blame] | 910 | const FunctionDecl *FD = E->getDirectCallee(); |
| 911 | Value *a = EmitScalarExpr(E->getArg(0)); |
| 912 | Value *b = EmitScalarExpr(E->getArg(1)); |
| 913 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FD->getType()); |
| 914 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 915 | llvm::StringRef Name = FD->getName(); |
| 916 | return Builder.CreateCall2(CGM.CreateRuntimeFunction(FTy, Name), |
| 917 | a, b); |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 918 | } |
Rafael Espindola | e140af3 | 2010-06-09 03:48:40 +0000 | [diff] [blame] | 919 | |
| 920 | llvm::SmallVector<Value*, 4> Ops; |
| 921 | // Determine the type of this overloaded NEON intrinsic. |
| 922 | assert(BuiltinID > ARM::BI__builtin_thread_pointer); |
| 923 | for (unsigned i = 0, e = E->getNumArgs() - 1; i != e; i++) |
| 924 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 925 | |
| 926 | llvm::APSInt Result; |
| 927 | const Expr *Arg = E->getArg(E->getNumArgs()-1); |
| 928 | if (!Arg->isIntegerConstantExpr(Result, getContext())) |
| 929 | return 0; |
| 930 | |
| 931 | unsigned type = Result.getZExtValue(); |
| 932 | bool usgn = type & 0x08; |
| 933 | bool quad = type & 0x10; |
| 934 | |
| 935 | const llvm::Type *Ty = GetNeonType(VMContext, type & 0x7, quad); |
| 936 | if (!Ty) |
| 937 | return 0; |
| 938 | |
| 939 | unsigned Int; |
| 940 | switch (BuiltinID) { |
| 941 | default: return 0; |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 942 | case ARM::BI__builtin_neon_vaba_v: |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 943 | case ARM::BI__builtin_neon_vabaq_v: |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 944 | Int = usgn ? Intrinsic::arm_neon_vabau : Intrinsic::arm_neon_vabas; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 945 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vaba"); |
| 946 | case ARM::BI__builtin_neon_vabal_v: |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 947 | Int = usgn ? Intrinsic::arm_neon_vabalu : Intrinsic::arm_neon_vabals; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 948 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabal"); |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 949 | case ARM::BI__builtin_neon_vabd_v: |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 950 | case ARM::BI__builtin_neon_vabdq_v: |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 951 | Int = usgn ? Intrinsic::arm_neon_vabdu : Intrinsic::arm_neon_vabds; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 952 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabd"); |
| 953 | case ARM::BI__builtin_neon_vabdl_v: |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 954 | Int = usgn ? Intrinsic::arm_neon_vabdlu : Intrinsic::arm_neon_vabdls; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 955 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vabdl"); |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 956 | case ARM::BI__builtin_neon_vabs_v: |
| 957 | case ARM::BI__builtin_neon_vabsq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 958 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vabs, &Ty, 1); |
| 959 | return EmitNeonCall(F, Ops, "vabs"); |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 960 | } |
| 961 | case ARM::BI__builtin_neon_vaddhn_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 962 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vaddhn, &Ty, 1); |
| 963 | return EmitNeonCall(F, Ops, "vaddhn"); |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 964 | } |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 965 | case ARM::BI__builtin_neon_vaddl_v: |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 966 | Int = usgn ? Intrinsic::arm_neon_vaddlu : Intrinsic::arm_neon_vaddls; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 967 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vaddl"); |
| 968 | case ARM::BI__builtin_neon_vaddw_v: |
Nate Begeman | 998622c | 2010-06-07 16:01:56 +0000 | [diff] [blame] | 969 | Int = usgn ? Intrinsic::arm_neon_vaddws : Intrinsic::arm_neon_vaddwu; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 970 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vaddw"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 971 | // FIXME: vbsl -> or ((0 & 1), (0 & 2)) in arm_neon.h |
| 972 | case ARM::BI__builtin_neon_vcale_v: |
| 973 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 974 | case ARM::BI__builtin_neon_vcage_v: { |
| 975 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacged, &Ty, 1); |
| 976 | return EmitNeonCall(F, Ops, "vcage"); |
| 977 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 978 | case ARM::BI__builtin_neon_vcaleq_v: |
| 979 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 980 | case ARM::BI__builtin_neon_vcageq_v: { |
| 981 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgeq, &Ty, 1); |
| 982 | return EmitNeonCall(F, Ops, "vcage"); |
| 983 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 984 | case ARM::BI__builtin_neon_vcalt_v: |
| 985 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 986 | case ARM::BI__builtin_neon_vcagt_v: { |
| 987 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtd, &Ty, 1); |
| 988 | return EmitNeonCall(F, Ops, "vcagt"); |
| 989 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 990 | case ARM::BI__builtin_neon_vcaltq_v: |
| 991 | std::swap(Ops[0], Ops[1]); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 992 | case ARM::BI__builtin_neon_vcagtq_v: { |
| 993 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vacgtq, &Ty, 1); |
| 994 | return EmitNeonCall(F, Ops, "vcagt"); |
| 995 | } |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 996 | case ARM::BI__builtin_neon_vcls_v: |
| 997 | case ARM::BI__builtin_neon_vclsq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 998 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcls, &Ty, 1); |
| 999 | return EmitNeonCall(F, Ops, "vcls"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1000 | } |
| 1001 | case ARM::BI__builtin_neon_vclz_v: |
| 1002 | case ARM::BI__builtin_neon_vclzq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1003 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vclz, &Ty, 1); |
| 1004 | return EmitNeonCall(F, Ops, "vclz"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1005 | } |
| 1006 | case ARM::BI__builtin_neon_vcnt_v: |
| 1007 | case ARM::BI__builtin_neon_vcntq_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1008 | Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vcnt, &Ty, 1); |
| 1009 | return EmitNeonCall(F, Ops, "vcnt"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1010 | } |
| 1011 | // FIXME: intrinsics for f16<->f32 convert missing from ARM target. |
| 1012 | case ARM::BI__builtin_neon_vcvt_f32_v: |
| 1013 | case ARM::BI__builtin_neon_vcvtq_f32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1014 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1015 | Ty = GetNeonType(VMContext, 4, quad); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1016 | return usgn ? Builder.CreateUIToFP(Ops[0], Ty, "vcvt") |
| 1017 | : Builder.CreateSIToFP(Ops[0], Ty, "vcvt"); |
| 1018 | } |
| 1019 | case ARM::BI__builtin_neon_vcvt_s32_v: |
| 1020 | case ARM::BI__builtin_neon_vcvt_u32_v: |
| 1021 | case ARM::BI__builtin_neon_vcvtq_s32_v: |
| 1022 | case ARM::BI__builtin_neon_vcvtq_u32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1023 | Ops[0] = Builder.CreateBitCast(Ops[0], GetNeonType(VMContext, 4, quad)); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1024 | return usgn ? Builder.CreateFPToUI(Ops[0], Ty, "vcvt") |
| 1025 | : Builder.CreateFPToSI(Ops[0], Ty, "vcvt"); |
| 1026 | } |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1027 | // FIXME: these intrinsics often do not work due to the fragility of bitcasts |
| 1028 | // coming and going during codegen. |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1029 | case ARM::BI__builtin_neon_vcvt_n_f32_v: |
| 1030 | case ARM::BI__builtin_neon_vcvtq_n_f32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1031 | const llvm::Type *Tys[2] = { GetNeonType(VMContext, 4, quad), Ty }; |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1032 | Int = usgn ? Intrinsic::arm_neon_vcvtfxu2fp : Intrinsic::arm_neon_vcvtfxs2fp; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1033 | Function *F = CGM.getIntrinsic(Int, Tys, 2); |
| 1034 | return EmitNeonCall(F, Ops, "vcvt_n"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1035 | } |
| 1036 | case ARM::BI__builtin_neon_vcvt_n_s32_v: |
| 1037 | case ARM::BI__builtin_neon_vcvt_n_u32_v: |
| 1038 | case ARM::BI__builtin_neon_vcvtq_n_s32_v: |
| 1039 | case ARM::BI__builtin_neon_vcvtq_n_u32_v: { |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1040 | const llvm::Type *Tys[2] = { Ty, GetNeonType(VMContext, 4, quad) }; |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1041 | Int = usgn ? Intrinsic::arm_neon_vcvtfp2fxu : Intrinsic::arm_neon_vcvtfp2fxs; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1042 | Function *F = CGM.getIntrinsic(Int, Tys, 2); |
| 1043 | return EmitNeonCall(F, Ops, "vcvt_n"); |
| 1044 | } |
| 1045 | case ARM::BI__builtin_neon_vext_v: |
| 1046 | case ARM::BI__builtin_neon_vextq_v: { |
| 1047 | ConstantInt *C = dyn_cast<ConstantInt>(Ops[2]); |
| 1048 | int CV = C->getSExtValue(); |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1049 | const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1050 | |
| 1051 | SmallVector<Constant*, 16> Indices; |
Nate Begeman | 30d9171 | 2010-06-08 06:03:01 +0000 | [diff] [blame] | 1052 | for (unsigned i = 0, e = cast<llvm::VectorType>(Ty)->getNumElements(); |
| 1053 | i != e; ++i) |
| 1054 | Indices.push_back(ConstantInt::get(I32Ty, i+CV)); |
| 1055 | |
| 1056 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1057 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1058 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1059 | return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vext"); |
| 1060 | } |
Nate Begeman | 95450f6 | 2010-06-09 05:30:26 +0000 | [diff] [blame] | 1061 | case ARM::BI__builtin_neon_vget_lane_i8: |
| 1062 | case ARM::BI__builtin_neon_vget_lane_i16: |
| 1063 | case ARM::BI__builtin_neon_vget_lane_i32: |
| 1064 | case ARM::BI__builtin_neon_vget_lane_i64: |
| 1065 | case ARM::BI__builtin_neon_vget_lane_f32: |
| 1066 | case ARM::BI__builtin_neon_vgetq_lane_i8: |
| 1067 | case ARM::BI__builtin_neon_vgetq_lane_i16: |
| 1068 | case ARM::BI__builtin_neon_vgetq_lane_i32: |
| 1069 | case ARM::BI__builtin_neon_vgetq_lane_i64: |
| 1070 | case ARM::BI__builtin_neon_vgetq_lane_f32: |
Nate Begeman | df98e1d | 2010-06-09 18:04:15 +0000 | [diff] [blame^] | 1071 | return Builder.CreateExtractElement(Ops[0], EmitScalarExpr(E->getArg(1)), |
| 1072 | "vget_lane"); |
| 1073 | case ARM::BI__builtin_neon_vhadd_v: |
| 1074 | case ARM::BI__builtin_neon_vhaddq_v: |
| 1075 | Int = usgn ? Intrinsic::arm_neon_vhaddu : Intrinsic::arm_neon_vhadds; |
| 1076 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhadd"); |
| 1077 | case ARM::BI__builtin_neon_vhsub_v: |
| 1078 | case ARM::BI__builtin_neon_vhsubq_v: |
| 1079 | Int = usgn ? Intrinsic::arm_neon_vhsubu : Intrinsic::arm_neon_vhsubs; |
| 1080 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vhsub"); |
| 1081 | // FIXME: vld* |
| 1082 | case ARM::BI__builtin_neon_vmax_v: |
| 1083 | case ARM::BI__builtin_neon_vmaxq_v: |
| 1084 | Int = usgn ? Intrinsic::arm_neon_vmaxu : Intrinsic::arm_neon_vmaxs; |
| 1085 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmax"); |
| 1086 | case ARM::BI__builtin_neon_vmin_v: |
| 1087 | case ARM::BI__builtin_neon_vminq_v: |
| 1088 | Int = usgn ? Intrinsic::arm_neon_vminu : Intrinsic::arm_neon_vmins; |
| 1089 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmin"); |
| 1090 | // FIXME: vmlal_lane -> splat, drop imm |
| 1091 | case ARM::BI__builtin_neon_vmlal_v: |
| 1092 | Int = usgn ? Intrinsic::arm_neon_vmlalu : Intrinsic::arm_neon_vmlals; |
| 1093 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmlal"); |
| 1094 | // FIXME: vmlal_n, vmla_n, vmlsl_n, vmls_n, vmull_n, vmul_n, |
| 1095 | // vqdmlal_n, vqdmlsl_n, vqdmulh_n, vqdmull_n, vqrdmulh_n -> splat,-_n |
| 1096 | case ARM::BI__builtin_neon_vmovl_v: |
| 1097 | Int = usgn ? Intrinsic::arm_neon_vmovlu : Intrinsic::arm_neon_vmovls; |
| 1098 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vmovl"); |
| 1099 | case ARM::BI__builtin_neon_vmovn_v: |
| 1100 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vmovn, &Ty, 1), |
| 1101 | Ops, "vmovn"); |
| 1102 | case ARM::BI__builtin_neon_vpadal_v: |
| 1103 | case ARM::BI__builtin_neon_vpadalq_v: |
| 1104 | Int = usgn ? Intrinsic::arm_neon_vpadalu : Intrinsic::arm_neon_vpadals; |
| 1105 | return EmitNeonCall(CGM.getIntrinsic(Int, &Ty, 1), Ops, "vpadal"); |
Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1106 | case ARM::BI__builtin_neon_vtbl1_v: |
| 1107 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl1), |
| 1108 | Ops, "vtbl1"); |
| 1109 | case ARM::BI__builtin_neon_vtbl2_v: |
| 1110 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl2), |
| 1111 | Ops, "vtbl2"); |
| 1112 | case ARM::BI__builtin_neon_vtbl3_v: |
| 1113 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl3), |
| 1114 | Ops, "vtbl3"); |
| 1115 | case ARM::BI__builtin_neon_vtbl4_v: |
| 1116 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbl4), |
| 1117 | Ops, "vtbl4"); |
| 1118 | case ARM::BI__builtin_neon_vtbx1_v: |
| 1119 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx1), |
| 1120 | Ops, "vtbx1"); |
| 1121 | case ARM::BI__builtin_neon_vtbx2_v: |
| 1122 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx2), |
| 1123 | Ops, "vtbx2"); |
| 1124 | case ARM::BI__builtin_neon_vtbx3_v: |
| 1125 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx3), |
| 1126 | Ops, "vtbx3"); |
| 1127 | case ARM::BI__builtin_neon_vtbx4_v: |
| 1128 | return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vtbx4), |
| 1129 | Ops, "vtbx4"); |
| 1130 | case ARM::BI__builtin_neon_vtst_v: |
| 1131 | case ARM::BI__builtin_neon_vtstq_v: { |
| 1132 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1133 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1134 | Ops[0] = Builder.CreateAnd(Ops[0], Ops[1]); |
| 1135 | Ops[0] = Builder.CreateICmp(ICmpInst::ICMP_NE, Ops[0], |
| 1136 | ConstantAggregateZero::get(Ty)); |
| 1137 | return Builder.CreateSExt(Ops[0], Ty, "vtst"); |
| 1138 | } |
| 1139 | // FIXME: transpose/zip/unzip don't currently match patterns for |
| 1140 | // the non-q variants, but emitting 2 shufflevectors seems like a hack. |
| 1141 | case ARM::BI__builtin_neon_vtrn_v: |
| 1142 | case ARM::BI__builtin_neon_vtrnq_v: { |
| 1143 | const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext); |
| 1144 | SmallVector<Constant*, 32> Indices; |
| 1145 | unsigned nElts = cast<llvm::VectorType>(Ty)->getNumElements(); |
| 1146 | for (unsigned vi = 0; vi != 2; ++vi) { |
| 1147 | for (unsigned i = 0; i != nElts; i += 2) { |
| 1148 | Indices.push_back(ConstantInt::get(I32Ty, i+vi)); |
| 1149 | Indices.push_back(ConstantInt::get(I32Ty, i+nElts+vi)); |
| 1150 | } |
| 1151 | } |
| 1152 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1153 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1154 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1155 | return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vtrn"); |
| 1156 | } |
| 1157 | case ARM::BI__builtin_neon_vuzp_v: |
| 1158 | case ARM::BI__builtin_neon_vuzpq_v: { |
| 1159 | const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext); |
| 1160 | SmallVector<Constant*, 32> Indices; |
| 1161 | unsigned nElts = cast<llvm::VectorType>(Ty)->getNumElements(); |
| 1162 | for (unsigned vi = 0; vi != 2; ++vi) |
| 1163 | for (unsigned i = 0; i != nElts; ++i) |
| 1164 | Indices.push_back(ConstantInt::get(I32Ty, 2*i+vi)); |
| 1165 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1166 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1167 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1168 | return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vuzp"); |
| 1169 | } |
| 1170 | case ARM::BI__builtin_neon_vzip_v: |
| 1171 | case ARM::BI__builtin_neon_vzipq_v: { |
| 1172 | const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext); |
| 1173 | SmallVector<Constant*, 32> Indices; |
| 1174 | unsigned nElts = cast<llvm::VectorType>(Ty)->getNumElements(); |
| 1175 | for (unsigned i = 0; i != nElts; ++i) { |
| 1176 | Indices.push_back(ConstantInt::get(I32Ty, i)); |
| 1177 | Indices.push_back(ConstantInt::get(I32Ty, i+nElts)); |
| 1178 | } |
| 1179 | Ops[0] = Builder.CreateBitCast(Ops[0], Ty); |
| 1180 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty); |
| 1181 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1182 | return Builder.CreateShuffleVector(Ops[0], Ops[1], SV, "vzip"); |
Nate Begeman | 9eb65a5 | 2010-06-08 00:17:19 +0000 | [diff] [blame] | 1183 | } |
Chris Lattner | 2752c01 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 1188 | const CallExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1190 | llvm::SmallVector<Value*, 4> Ops; |
| 1191 | |
| 1192 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 1193 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 1194 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 1195 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 1196 | default: return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | case X86::BI__builtin_ia32_pslldi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1198 | case X86::BI__builtin_ia32_psllqi128: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | case X86::BI__builtin_ia32_psllwi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1200 | case X86::BI__builtin_ia32_psradi128: |
| 1201 | case X86::BI__builtin_ia32_psrawi128: |
| 1202 | case X86::BI__builtin_ia32_psrldi128: |
| 1203 | case X86::BI__builtin_ia32_psrlqi128: |
| 1204 | case X86::BI__builtin_ia32_psrlwi128: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1205 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 1206 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 2); |
| 1207 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1208 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1209 | Ops[1], Zero, "insert"); |
| 1210 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 1211 | const char *name = 0; |
| 1212 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1214 | switch (BuiltinID) { |
| 1215 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 1216 | case X86::BI__builtin_ia32_pslldi128: |
| 1217 | name = "pslldi"; |
| 1218 | ID = Intrinsic::x86_sse2_psll_d; |
| 1219 | break; |
| 1220 | case X86::BI__builtin_ia32_psllqi128: |
| 1221 | name = "psllqi"; |
| 1222 | ID = Intrinsic::x86_sse2_psll_q; |
| 1223 | break; |
| 1224 | case X86::BI__builtin_ia32_psllwi128: |
| 1225 | name = "psllwi"; |
| 1226 | ID = Intrinsic::x86_sse2_psll_w; |
| 1227 | break; |
| 1228 | case X86::BI__builtin_ia32_psradi128: |
| 1229 | name = "psradi"; |
| 1230 | ID = Intrinsic::x86_sse2_psra_d; |
| 1231 | break; |
| 1232 | case X86::BI__builtin_ia32_psrawi128: |
| 1233 | name = "psrawi"; |
| 1234 | ID = Intrinsic::x86_sse2_psra_w; |
| 1235 | break; |
| 1236 | case X86::BI__builtin_ia32_psrldi128: |
| 1237 | name = "psrldi"; |
| 1238 | ID = Intrinsic::x86_sse2_psrl_d; |
| 1239 | break; |
| 1240 | case X86::BI__builtin_ia32_psrlqi128: |
| 1241 | name = "psrlqi"; |
| 1242 | ID = Intrinsic::x86_sse2_psrl_q; |
| 1243 | break; |
| 1244 | case X86::BI__builtin_ia32_psrlwi128: |
| 1245 | name = "psrlwi"; |
| 1246 | ID = Intrinsic::x86_sse2_psrl_w; |
| 1247 | break; |
| 1248 | } |
| 1249 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1250 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1251 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | case X86::BI__builtin_ia32_pslldi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1253 | case X86::BI__builtin_ia32_psllqi: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | case X86::BI__builtin_ia32_psllwi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1255 | case X86::BI__builtin_ia32_psradi: |
| 1256 | case X86::BI__builtin_ia32_psrawi: |
| 1257 | case X86::BI__builtin_ia32_psrldi: |
| 1258 | case X86::BI__builtin_ia32_psrlqi: |
| 1259 | case X86::BI__builtin_ia32_psrlwi: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1260 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 1261 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 1); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1262 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1263 | const char *name = 0; |
| 1264 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1266 | switch (BuiltinID) { |
| 1267 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 1268 | case X86::BI__builtin_ia32_pslldi: |
| 1269 | name = "pslldi"; |
| 1270 | ID = Intrinsic::x86_mmx_psll_d; |
| 1271 | break; |
| 1272 | case X86::BI__builtin_ia32_psllqi: |
| 1273 | name = "psllqi"; |
| 1274 | ID = Intrinsic::x86_mmx_psll_q; |
| 1275 | break; |
| 1276 | case X86::BI__builtin_ia32_psllwi: |
| 1277 | name = "psllwi"; |
| 1278 | ID = Intrinsic::x86_mmx_psll_w; |
| 1279 | break; |
| 1280 | case X86::BI__builtin_ia32_psradi: |
| 1281 | name = "psradi"; |
| 1282 | ID = Intrinsic::x86_mmx_psra_d; |
| 1283 | break; |
| 1284 | case X86::BI__builtin_ia32_psrawi: |
| 1285 | name = "psrawi"; |
| 1286 | ID = Intrinsic::x86_mmx_psra_w; |
| 1287 | break; |
| 1288 | case X86::BI__builtin_ia32_psrldi: |
| 1289 | name = "psrldi"; |
| 1290 | ID = Intrinsic::x86_mmx_psrl_d; |
| 1291 | break; |
| 1292 | case X86::BI__builtin_ia32_psrlqi: |
| 1293 | name = "psrlqi"; |
| 1294 | ID = Intrinsic::x86_mmx_psrl_q; |
| 1295 | break; |
| 1296 | case X86::BI__builtin_ia32_psrlwi: |
| 1297 | name = "psrlwi"; |
| 1298 | ID = Intrinsic::x86_mmx_psrl_w; |
| 1299 | break; |
| 1300 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 1301 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1303 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 1304 | case X86::BI__builtin_ia32_cmpps: { |
| 1305 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 1306 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 1307 | } |
| 1308 | case X86::BI__builtin_ia32_cmpss: { |
| 1309 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 1310 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 1311 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1312 | case X86::BI__builtin_ia32_ldmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1313 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1314 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 1315 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1316 | Builder.CreateStore(Ops[0], Tmp); |
| 1317 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 1318 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1319 | } |
| 1320 | case X86::BI__builtin_ia32_stmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1321 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1322 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 1323 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1324 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 1325 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1326 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 1327 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 1328 | case X86::BI__builtin_ia32_cmppd: { |
| 1329 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 1330 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 1331 | } |
| 1332 | case X86::BI__builtin_ia32_cmpsd: { |
| 1333 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 1334 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 1335 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1336 | case X86::BI__builtin_ia32_storehps: |
| 1337 | case X86::BI__builtin_ia32_storelps: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1338 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1339 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 1340 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1342 | // cast val v2i64 |
| 1343 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1345 | // extract (0, 1) |
| 1346 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1347 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Index); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1348 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 1349 | |
| 1350 | // cast pointer to i64 & store |
| 1351 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 1352 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1353 | } |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 1354 | case X86::BI__builtin_ia32_palignr: { |
Eric Christopher | e57aa9e | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 1355 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 1356 | |
| 1357 | // If palignr is shifting the pair of input vectors less than 9 bytes, |
| 1358 | // emit a shuffle instruction. |
| 1359 | if (shiftVal <= 8) { |
| 1360 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 1361 | |
| 1362 | llvm::SmallVector<llvm::Constant*, 8> Indices; |
| 1363 | for (unsigned i = 0; i != 8; ++i) |
| 1364 | Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i)); |
| 1365 | |
| 1366 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1367 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 1368 | } |
| 1369 | |
| 1370 | // If palignr is shifting the pair of input vectors more than 8 but less |
| 1371 | // than 16 bytes, emit a logical right shift of the destination. |
| 1372 | if (shiftVal < 16) { |
| 1373 | // MMX has these as 1 x i64 vectors for some odd optimization reasons. |
| 1374 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
| 1375 | const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 1); |
| 1376 | |
| 1377 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 1378 | Ops[1] = llvm::ConstantInt::get(VecTy, (shiftVal-8) * 8); |
| 1379 | |
| 1380 | // create i32 constant |
| 1381 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_mmx_psrl_q); |
| 1382 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 1383 | } |
| 1384 | |
| 1385 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 1386 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Nate Begeman | c3420ff | 2009-12-14 05:15:02 +0000 | [diff] [blame] | 1387 | } |
| 1388 | case X86::BI__builtin_ia32_palignr128: { |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 1389 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 1390 | |
| 1391 | // If palignr is shifting the pair of input vectors less than 17 bytes, |
| 1392 | // emit a shuffle instruction. |
| 1393 | if (shiftVal <= 16) { |
| 1394 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 1395 | |
| 1396 | llvm::SmallVector<llvm::Constant*, 16> Indices; |
| 1397 | for (unsigned i = 0; i != 16; ++i) |
| 1398 | Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i)); |
| 1399 | |
| 1400 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1401 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 1402 | } |
| 1403 | |
| 1404 | // If palignr is shifting the pair of input vectors more than 16 but less |
| 1405 | // than 32 bytes, emit a logical right shift of the destination. |
| 1406 | if (shiftVal < 32) { |
| 1407 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
| 1408 | const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 1409 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 1410 | |
| 1411 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 1412 | Ops[1] = llvm::ConstantInt::get(IntTy, (shiftVal-16) * 8); |
| 1413 | |
| 1414 | // create i32 constant |
| 1415 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq); |
| 1416 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 1417 | } |
| 1418 | |
| 1419 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 1420 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 1421 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 1426 | const CallExpr *E) { |
Chris Lattner | dd17394 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 1427 | llvm::SmallVector<Value*, 4> Ops; |
| 1428 | |
| 1429 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 1430 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 1431 | |
| 1432 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 1433 | |
| 1434 | switch (BuiltinID) { |
| 1435 | default: return 0; |
| 1436 | |
| 1437 | // vec_st |
| 1438 | case PPC::BI__builtin_altivec_stvx: |
| 1439 | case PPC::BI__builtin_altivec_stvxl: |
| 1440 | case PPC::BI__builtin_altivec_stvebx: |
| 1441 | case PPC::BI__builtin_altivec_stvehx: |
| 1442 | case PPC::BI__builtin_altivec_stvewx: |
| 1443 | { |
| 1444 | Ops[2] = Builder.CreateBitCast(Ops[2], llvm::Type::getInt8PtrTy(VMContext)); |
| 1445 | Ops[1] = !isa<Constant>(Ops[1]) || !cast<Constant>(Ops[1])->isNullValue() |
| 1446 | ? Builder.CreateGEP(Ops[2], Ops[1], "tmp") : Ops[2]; |
| 1447 | Ops.pop_back(); |
| 1448 | |
| 1449 | switch (BuiltinID) { |
| 1450 | default: assert(0 && "Unsupported vavg intrinsic!"); |
| 1451 | case PPC::BI__builtin_altivec_stvx: |
| 1452 | ID = Intrinsic::ppc_altivec_stvx; |
| 1453 | break; |
| 1454 | case PPC::BI__builtin_altivec_stvxl: |
| 1455 | ID = Intrinsic::ppc_altivec_stvxl; |
| 1456 | break; |
| 1457 | case PPC::BI__builtin_altivec_stvebx: |
| 1458 | ID = Intrinsic::ppc_altivec_stvebx; |
| 1459 | break; |
| 1460 | case PPC::BI__builtin_altivec_stvehx: |
| 1461 | ID = Intrinsic::ppc_altivec_stvehx; |
| 1462 | break; |
| 1463 | case PPC::BI__builtin_altivec_stvewx: |
| 1464 | ID = Intrinsic::ppc_altivec_stvewx; |
| 1465 | break; |
| 1466 | } |
| 1467 | llvm::Function *F = CGM.getIntrinsic(ID); |
| 1468 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), ""); |
| 1469 | } |
| 1470 | } |
Daniel Dunbar | b0b8438 | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 1471 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | } |