Anders Carlsson | 1d8e521 | 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 | 5b12ab8 | 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 | 1d8e521 | 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 | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 14 | #include "TargetInfo.h" |
Anders Carlsson | 1d8e521 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
| 16 | #include "CodeGenModule.h" |
Anders Carlsson | a020c43 | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | a1518b1 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 18 | #include "clang/AST/APValue.h" |
Chris Lattner | 1eec660 | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Chris Lattner | 5abdec7 | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetBuiltins.h" |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 22 | #include "llvm/Intrinsics.h" |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 1d8e521 | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
Anders Carlsson | a020c43 | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Daniel Dunbar | 4ff562d | 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 | 4fab57d | 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 | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 60 | static RValue EmitBinaryAtomic(CodeGenFunction &CGF, |
Mon P Wang | b84407d | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 61 | Intrinsic::ID Id, const CallExpr *E) { |
Daniel Dunbar | 4ff562d | 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 | b160a0d | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 64 | const llvm::Type *ResType[2]; |
Daniel Dunbar | 4fab57d | 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 | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 68 | return RValue::get(EmitCallWithBarrier(CGF, AtomF, Args, Args + 2)); |
Daniel Dunbar | 4fab57d | 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 | 43660c5 | 2010-05-06 05:35:16 +0000 | [diff] [blame] | 74 | static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF, |
Daniel Dunbar | 4fab57d | 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 | 4ff562d | 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 | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 84 | return RValue::get(CGF.Builder.CreateBinOp(Op, Result, Args[1])); |
Mon P Wang | b84407d | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 85 | } |
| 86 | |
John McCall | 515c3c5 | 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 | 43660c5 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 116 | unsigned BuiltinID, const CallExpr *E) { |
Chris Lattner | 24355b5 | 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 | c968790 | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 118 | Expr::EvalResult Result; |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 119 | if (E->Evaluate(Result, CGM.getContext())) { |
Anders Carlsson | c968790 | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 120 | if (Result.Val.isInt()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | return RValue::get(llvm::ConstantInt::get(VMContext, |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 122 | Result.Val.getInt())); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 123 | else if (Result.Val.isFloat()) |
Owen Anderson | e05f2ed | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 124 | return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat())); |
Chris Lattner | a1518b1 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 125 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 24355b5 | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 127 | switch (BuiltinID) { |
| 128 | default: break; // Handle intrinsics and libm functions below. |
Chris Lattner | a97132a | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 129 | case Builtin::BI__builtin___CFStringMakeConstantString: |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 130 | case Builtin::BI__builtin___NSStringMakeConstantString: |
Anders Carlsson | 80f97ab | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 131 | return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0)); |
Chris Lattner | 0bf6791 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 132 | case Builtin::BI__builtin_stdarg_start: |
Anders Carlsson | 24ebce6 | 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 | e9fcadd2 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 135 | Value *ArgValue = EmitVAListRef(E->getArg(0)); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 136 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 137 | if (ArgValue->getType() != DestType) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | ArgValue = Builder.CreateBitCast(ArgValue, DestType, |
Daniel Dunbar | e59313a | 2009-07-26 09:28:40 +0000 | [diff] [blame] | 139 | ArgValue->getName().data()); |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 140 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ? |
Chris Lattner | 0bf6791 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 142 | Intrinsic::vaend : Intrinsic::vastart; |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 143 | return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 144 | } |
Anders Carlsson | c0b0e59 | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 145 | case Builtin::BI__builtin_va_copy: { |
Eli Friedman | ddea0ad | 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 | c0b0e59 | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 148 | |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 149 | const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | c0b0e59 | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 150 | |
| 151 | DstPtr = Builder.CreateBitCast(DstPtr, Type); |
| 152 | SrcPtr = Builder.CreateBitCast(SrcPtr, Type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy), |
Chris Lattner | dbcc2ca | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 154 | DstPtr, SrcPtr)); |
Anders Carlsson | c0b0e59 | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 155 | } |
Anders Carlsson | 4f8eb12 | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 156 | case Builtin::BI__builtin_abs: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 158 | |
Chris Lattner | 28ee5b3 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 159 | Value *NegOp = Builder.CreateNeg(ArgValue, "neg"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | Value *CmpResult = |
| 161 | Builder.CreateICmpSGE(ArgValue, |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 162 | llvm::Constant::getNullValue(ArgValue->getType()), |
Chris Lattner | 28ee5b3 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 163 | "abscond"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | Value *Result = |
Anders Carlsson | 4f8eb12 | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 165 | Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Anders Carlsson | 4f8eb12 | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 167 | return RValue::get(Result); |
| 168 | } |
Anders Carlsson | 093f1a0 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Anders Carlsson | 093f1a0 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Anders Carlsson | 093f1a0 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 178 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 179 | if (Result->getType() != ResultType) |
Duncan Sands | 7876dad | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 180 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 181 | "cast"); |
Anders Carlsson | 093f1a0 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 182 | return RValue::get(Result); |
| 183 | } |
Eli Friedman | 5e2281e | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Eli Friedman | 5e2281e | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Eli Friedman | 5e2281e | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 193 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 194 | if (Result->getType() != ResultType) |
Duncan Sands | 7876dad | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 195 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 196 | "cast"); |
Eli Friedman | 5e2281e | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 197 | return RValue::get(Result); |
| 198 | } |
Daniel Dunbar | d93abc3 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Daniel Dunbar | d93abc3 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 208 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 210 | llvm::ConstantInt::get(ArgType, 1), "tmp"); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 211 | Value *Zero = llvm::Constant::getNullValue(ArgType); |
Daniel Dunbar | d93abc3 | 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 | 7876dad | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 215 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 216 | "cast"); |
Daniel Dunbar | d93abc3 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Daniel Dunbar | d93abc3 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Daniel Dunbar | d93abc3 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1), |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 231 | "tmp"); |
| 232 | if (Result->getType() != ResultType) |
Duncan Sands | 7876dad | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 233 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 234 | "cast"); |
Daniel Dunbar | d93abc3 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Daniel Dunbar | d93abc3 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Daniel Dunbar | d93abc3 | 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 | 7876dad | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 248 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 249 | "cast"); |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 250 | return RValue::get(Result); |
| 251 | } |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 252 | case Builtin::BI__builtin_expect: |
Daniel Dunbar | c2f6796 | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 253 | // FIXME: pass expect through to LLVM |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 254 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Anders Carlsson | ef93b9d | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 255 | case Builtin::BI__builtin_bswap32: |
| 256 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 257 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | ef93b9d | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 258 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 259 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 260 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | } |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 262 | case Builtin::BI__builtin_object_size: { |
Mike Stump | 7a484dd | 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 | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 265 | const llvm::Type *ResType[] = { |
| 266 | ConvertType(E->getType()) |
| 267 | }; |
Eric Christopher | c879156 | 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 | 876387b | 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 | c879156 | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 280 | CI)); |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 281 | } |
Daniel Dunbar | b725726 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 286 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 288 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 3); |
Daniel Dunbar | b725726 | 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 | ef93b9d | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 291 | } |
Daniel Dunbar | b725726 | 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 | bf20638 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 296 | case Builtin::BI__builtin_unreachable: { |
Mike Stump | b85ffb6 | 2009-12-16 03:07:12 +0000 | [diff] [blame] | 297 | if (CatchUndefined && HaveInsertPoint()) |
| 298 | EmitBranch(getTrapBB()); |
Chris Lattner | bf20638 | 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 | c2f6796 | 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 | c2f6796 | 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 | c2f6796 | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 311 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 312 | } |
| 313 | |
Chris Lattner | 6c9ffe9 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
Chris Lattner | 6c9ffe9 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 323 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 6c9ffe9 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | case Builtin::BI__builtin_isunordered: |
Chris Lattner | 6c9ffe9 | 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 | 1c277d0 | 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 | 43660c5 | 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 | 3628326 | 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 | fdb61d7 | 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 | dbff4bf | 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 | f6bd150 | 2009-06-02 07:10:30 +0000 | [diff] [blame] | 399 | case Builtin::BIalloca: |
Chris Lattner | 22b9ff4 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 400 | case Builtin::BI__builtin_alloca: { |
| 401 | // FIXME: LLVM IR Should allow alloca with an i64 size! |
| 402 | Value *Size = EmitScalarExpr(E->getArg(0)); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 403 | Size = Builder.CreateIntCast(Size, llvm::Type::getInt32Ty(VMContext), false, "tmp"); |
| 404 | return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp")); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 405 | } |
Eli Friedman | d6ef69a | 2010-01-23 19:00:10 +0000 | [diff] [blame] | 406 | case Builtin::BIbzero: |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 407 | case Builtin::BI__builtin_bzero: { |
| 408 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | cc2ab0c | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 409 | Value *SizeVal = EmitScalarExpr(E->getArg(1)); |
| 410 | Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()), |
| 411 | Address, |
| 412 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0), |
| 413 | SizeVal, |
| 414 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1), |
| 415 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 416 | return RValue::get(Address); |
Chris Lattner | 22b9ff4 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 417 | } |
Eli Friedman | 7f4933f | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 418 | case Builtin::BImemcpy: |
Eli Friedman | a3a4068 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 419 | case Builtin::BI__builtin_memcpy: { |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 420 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | cc2ab0c | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 421 | Value *SrcAddr = EmitScalarExpr(E->getArg(1)); |
| 422 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 423 | Builder.CreateCall5(CGM.getMemCpyFn(Address->getType(), SrcAddr->getType(), |
| 424 | SizeVal->getType()), |
| 425 | Address, SrcAddr, SizeVal, |
| 426 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1), |
| 427 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 428 | return RValue::get(Address); |
| 429 | } |
Eli Friedman | 7f4933f | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 430 | case Builtin::BImemmove: |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 431 | case Builtin::BI__builtin_memmove: { |
| 432 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | cc2ab0c | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 433 | Value *SrcAddr = EmitScalarExpr(E->getArg(1)); |
| 434 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 435 | Builder.CreateCall5(CGM.getMemMoveFn(Address->getType(), SrcAddr->getType(), |
| 436 | SizeVal->getType()), |
| 437 | Address, SrcAddr, SizeVal, |
| 438 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1), |
| 439 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 440 | return RValue::get(Address); |
| 441 | } |
Eli Friedman | 7f4933f | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 442 | case Builtin::BImemset: |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 443 | case Builtin::BI__builtin_memset: { |
| 444 | Value *Address = EmitScalarExpr(E->getArg(0)); |
Mon P Wang | cc2ab0c | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 445 | Value *SizeVal = EmitScalarExpr(E->getArg(2)); |
| 446 | Builder.CreateCall5(CGM.getMemSetFn(Address->getType(), SizeVal->getType()), |
| 447 | Address, |
| 448 | Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), |
| 449 | llvm::Type::getInt8Ty(VMContext)), |
| 450 | SizeVal, |
| 451 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1), |
| 452 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0)); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 453 | return RValue::get(Address); |
Eli Friedman | a3a4068 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 454 | } |
John McCall | 515c3c5 | 2010-03-03 10:30:05 +0000 | [diff] [blame] | 455 | case Builtin::BI__builtin_dwarf_cfa: { |
| 456 | // The offset in bytes from the first argument to the CFA. |
| 457 | // |
| 458 | // Why on earth is this in the frontend? Is there any reason at |
| 459 | // all that the backend can't reasonably determine this while |
| 460 | // lowering llvm.eh.dwarf.cfa()? |
| 461 | // |
| 462 | // TODO: If there's a satisfactory reason, add a target hook for |
| 463 | // this instead of hard-coding 0, which is correct for most targets. |
| 464 | int32_t Offset = 0; |
| 465 | |
| 466 | Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa, 0, 0); |
| 467 | return RValue::get(Builder.CreateCall(F, getInt32(VMContext, Offset))); |
| 468 | } |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 469 | case Builtin::BI__builtin_return_address: { |
Anton Korobeynikov | 73d50b9 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 470 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 471 | Depth = Builder.CreateIntCast(Depth, |
| 472 | llvm::Type::getInt32Ty(VMContext), |
| 473 | false, "tmp"); |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 474 | Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); |
Anton Korobeynikov | 73d50b9 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 475 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 476 | } |
| 477 | case Builtin::BI__builtin_frame_address: { |
Anton Korobeynikov | 73d50b9 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 478 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 479 | Depth = Builder.CreateIntCast(Depth, |
| 480 | llvm::Type::getInt32Ty(VMContext), |
| 481 | false, "tmp"); |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 482 | Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
Anton Korobeynikov | 73d50b9 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 483 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 484 | } |
Eli Friedman | 5b73b5e | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 485 | case Builtin::BI__builtin_extract_return_addr: { |
John McCall | d4f4b7f | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 486 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 487 | Value *Result = getTargetHooks().decodeReturnAddress(*this, Address); |
| 488 | return RValue::get(Result); |
| 489 | } |
| 490 | case Builtin::BI__builtin_frob_return_addr: { |
| 491 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 492 | Value *Result = getTargetHooks().encodeReturnAddress(*this, Address); |
| 493 | return RValue::get(Result); |
Eli Friedman | 5b73b5e | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 494 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 495 | case Builtin::BI__builtin_dwarf_sp_column: { |
| 496 | const llvm::IntegerType *Ty |
| 497 | = cast<llvm::IntegerType>(ConvertType(E->getType())); |
| 498 | int Column = getTargetHooks().getDwarfEHStackPointer(CGM); |
| 499 | if (Column == -1) { |
| 500 | CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column"); |
| 501 | return RValue::get(llvm::UndefValue::get(Ty)); |
| 502 | } |
| 503 | return RValue::get(llvm::ConstantInt::get(Ty, Column, true)); |
| 504 | } |
| 505 | case Builtin::BI__builtin_init_dwarf_reg_size_table: { |
| 506 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 507 | if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address)) |
| 508 | CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table"); |
| 509 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
| 510 | } |
John McCall | 66769f8 | 2010-03-03 05:38:58 +0000 | [diff] [blame] | 511 | case Builtin::BI__builtin_eh_return: { |
| 512 | Value *Int = EmitScalarExpr(E->getArg(0)); |
| 513 | Value *Ptr = EmitScalarExpr(E->getArg(1)); |
| 514 | |
| 515 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType()); |
| 516 | assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) && |
| 517 | "LLVM's __builtin_eh_return only supports 32- and 64-bit variants"); |
| 518 | Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32 |
| 519 | ? Intrinsic::eh_return_i32 |
| 520 | : Intrinsic::eh_return_i64, |
| 521 | 0, 0); |
| 522 | Builder.CreateCall2(F, Int, Ptr); |
| 523 | Value *V = Builder.CreateUnreachable(); |
| 524 | Builder.ClearInsertionPoint(); |
| 525 | return RValue::get(V); |
| 526 | } |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 527 | case Builtin::BI__builtin_unwind_init: { |
| 528 | Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0); |
| 529 | return RValue::get(Builder.CreateCall(F)); |
| 530 | } |
John McCall | 4b613fa | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 531 | case Builtin::BI__builtin_extend_pointer: { |
| 532 | // Extends a pointer to the size of an _Unwind_Word, which is |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 533 | // uint64_t on all platforms. Generally this gets poked into a |
| 534 | // register and eventually used as an address, so if the |
| 535 | // addressing registers are wider than pointers and the platform |
| 536 | // doesn't implicitly ignore high-order bits when doing |
| 537 | // addressing, we need to make sure we zext / sext based on |
| 538 | // the platform's expectations. |
John McCall | 4b613fa | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 539 | // |
| 540 | // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 541 | |
| 542 | LLVMContext &C = CGM.getLLVMContext(); |
| 543 | |
| 544 | // Cast the pointer to intptr_t. |
John McCall | 4b613fa | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 545 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 546 | const llvm::IntegerType *IntPtrTy = CGM.getTargetData().getIntPtrType(C); |
| 547 | Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast"); |
| 548 | |
| 549 | // If that's 64 bits, we're done. |
| 550 | if (IntPtrTy->getBitWidth() == 64) |
| 551 | return RValue::get(Result); |
| 552 | |
| 553 | // Otherwise, ask the codegen data what to do. |
| 554 | const llvm::IntegerType *Int64Ty = llvm::IntegerType::get(C, 64); |
John McCall | d4f4b7f | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 555 | if (getTargetHooks().extendPointerWithSExt()) |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 556 | return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext")); |
| 557 | else |
| 558 | return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext")); |
John McCall | 4b613fa | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 559 | } |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 560 | #if 0 |
| 561 | // FIXME: Finish/enable when LLVM backend support stabilizes |
| 562 | case Builtin::BI__builtin_setjmp: { |
| 563 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
| 564 | // Store the frame pointer to the buffer |
| 565 | Value *FrameAddrF = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
| 566 | Value *FrameAddr = |
| 567 | Builder.CreateCall(FrameAddrF, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 568 | Constant::getNullValue(llvm::Type::getInt32Ty(VMContext))); |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 569 | Builder.CreateStore(FrameAddr, Buf); |
| 570 | // Call the setjmp intrinsic |
| 571 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp, 0, 0); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 572 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 573 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 574 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 575 | } |
| 576 | case Builtin::BI__builtin_longjmp: { |
| 577 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp, 0, 0); |
| 578 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 579 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 580 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 581 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 582 | } |
| 583 | #endif |
Mon P Wang | b84407d | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 584 | case Builtin::BI__sync_fetch_and_add: |
Mon P Wang | b84407d | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 585 | case Builtin::BI__sync_fetch_and_sub: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 586 | case Builtin::BI__sync_fetch_and_or: |
| 587 | case Builtin::BI__sync_fetch_and_and: |
| 588 | case Builtin::BI__sync_fetch_and_xor: |
| 589 | case Builtin::BI__sync_add_and_fetch: |
| 590 | case Builtin::BI__sync_sub_and_fetch: |
| 591 | case Builtin::BI__sync_and_and_fetch: |
| 592 | case Builtin::BI__sync_or_and_fetch: |
| 593 | case Builtin::BI__sync_xor_and_fetch: |
| 594 | case Builtin::BI__sync_val_compare_and_swap: |
| 595 | case Builtin::BI__sync_bool_compare_and_swap: |
| 596 | case Builtin::BI__sync_lock_test_and_set: |
| 597 | case Builtin::BI__sync_lock_release: |
| 598 | assert(0 && "Shouldn't make it through sema"); |
| 599 | case Builtin::BI__sync_fetch_and_add_1: |
| 600 | case Builtin::BI__sync_fetch_and_add_2: |
| 601 | case Builtin::BI__sync_fetch_and_add_4: |
| 602 | case Builtin::BI__sync_fetch_and_add_8: |
| 603 | case Builtin::BI__sync_fetch_and_add_16: |
| 604 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); |
| 605 | case Builtin::BI__sync_fetch_and_sub_1: |
| 606 | case Builtin::BI__sync_fetch_and_sub_2: |
| 607 | case Builtin::BI__sync_fetch_and_sub_4: |
| 608 | case Builtin::BI__sync_fetch_and_sub_8: |
| 609 | case Builtin::BI__sync_fetch_and_sub_16: |
Mon P Wang | 28898b2 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 610 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 611 | case Builtin::BI__sync_fetch_and_or_1: |
| 612 | case Builtin::BI__sync_fetch_and_or_2: |
| 613 | case Builtin::BI__sync_fetch_and_or_4: |
| 614 | case Builtin::BI__sync_fetch_and_or_8: |
| 615 | case Builtin::BI__sync_fetch_and_or_16: |
| 616 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 617 | case Builtin::BI__sync_fetch_and_and_1: |
| 618 | case Builtin::BI__sync_fetch_and_and_2: |
| 619 | case Builtin::BI__sync_fetch_and_and_4: |
| 620 | case Builtin::BI__sync_fetch_and_and_8: |
| 621 | case Builtin::BI__sync_fetch_and_and_16: |
| 622 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 623 | case Builtin::BI__sync_fetch_and_xor_1: |
| 624 | case Builtin::BI__sync_fetch_and_xor_2: |
| 625 | case Builtin::BI__sync_fetch_and_xor_4: |
| 626 | case Builtin::BI__sync_fetch_and_xor_8: |
| 627 | case Builtin::BI__sync_fetch_and_xor_16: |
| 628 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 630 | // Clang extensions: not overloaded yet. |
Mon P Wang | b84407d | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 631 | case Builtin::BI__sync_fetch_and_min: |
| 632 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 633 | case Builtin::BI__sync_fetch_and_max: |
| 634 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 635 | case Builtin::BI__sync_fetch_and_umin: |
| 636 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 637 | case Builtin::BI__sync_fetch_and_umax: |
| 638 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 639 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 640 | case Builtin::BI__sync_add_and_fetch_1: |
| 641 | case Builtin::BI__sync_add_and_fetch_2: |
| 642 | case Builtin::BI__sync_add_and_fetch_4: |
| 643 | case Builtin::BI__sync_add_and_fetch_8: |
| 644 | case Builtin::BI__sync_add_and_fetch_16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 646 | llvm::Instruction::Add); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 647 | case Builtin::BI__sync_sub_and_fetch_1: |
| 648 | case Builtin::BI__sync_sub_and_fetch_2: |
| 649 | case Builtin::BI__sync_sub_and_fetch_4: |
| 650 | case Builtin::BI__sync_sub_and_fetch_8: |
| 651 | case Builtin::BI__sync_sub_and_fetch_16: |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 652 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, |
| 653 | llvm::Instruction::Sub); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 654 | case Builtin::BI__sync_and_and_fetch_1: |
| 655 | case Builtin::BI__sync_and_and_fetch_2: |
| 656 | case Builtin::BI__sync_and_and_fetch_4: |
| 657 | case Builtin::BI__sync_and_and_fetch_8: |
| 658 | case Builtin::BI__sync_and_and_fetch_16: |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 659 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, |
| 660 | llvm::Instruction::And); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 661 | case Builtin::BI__sync_or_and_fetch_1: |
| 662 | case Builtin::BI__sync_or_and_fetch_2: |
| 663 | case Builtin::BI__sync_or_and_fetch_4: |
| 664 | case Builtin::BI__sync_or_and_fetch_8: |
| 665 | case Builtin::BI__sync_or_and_fetch_16: |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 666 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, |
| 667 | llvm::Instruction::Or); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 668 | case Builtin::BI__sync_xor_and_fetch_1: |
| 669 | case Builtin::BI__sync_xor_and_fetch_2: |
| 670 | case Builtin::BI__sync_xor_and_fetch_4: |
| 671 | case Builtin::BI__sync_xor_and_fetch_8: |
| 672 | case Builtin::BI__sync_xor_and_fetch_16: |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 673 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, |
| 674 | llvm::Instruction::Xor); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 676 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 677 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 678 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 679 | case Builtin::BI__sync_val_compare_and_swap_8: |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 680 | case Builtin::BI__sync_val_compare_and_swap_16: { |
Mon P Wang | b160a0d | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 681 | const llvm::Type *ResType[2]; |
| 682 | ResType[0]= ConvertType(E->getType()); |
| 683 | ResType[1] = ConvertType(E->getArg(0)->getType()); |
| 684 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 685 | Value *Args[3] = { EmitScalarExpr(E->getArg(0)), |
| 686 | EmitScalarExpr(E->getArg(1)), |
| 687 | EmitScalarExpr(E->getArg(2)) }; |
| 688 | return RValue::get(EmitCallWithBarrier(*this, AtomF, Args, Args + 3)); |
Anders Carlsson | ceced4c | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 689 | } |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 690 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 691 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 692 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 693 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 694 | case Builtin::BI__sync_bool_compare_and_swap_8: |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 695 | case Builtin::BI__sync_bool_compare_and_swap_16: { |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 696 | const llvm::Type *ResType[2]; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 697 | ResType[0]= ConvertType(E->getArg(1)->getType()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 698 | ResType[1] = llvm::PointerType::getUnqual(ResType[0]); |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 699 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
| 700 | Value *OldVal = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 701 | Value *Args[3] = { EmitScalarExpr(E->getArg(0)), |
| 702 | OldVal, |
| 703 | EmitScalarExpr(E->getArg(2)) }; |
| 704 | Value *PrevVal = EmitCallWithBarrier(*this, AtomF, Args, Args + 3); |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 705 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 706 | // zext bool to int. |
| 707 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 708 | } |
| 709 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 710 | case Builtin::BI__sync_lock_test_and_set_1: |
| 711 | case Builtin::BI__sync_lock_test_and_set_2: |
| 712 | case Builtin::BI__sync_lock_test_and_set_4: |
| 713 | case Builtin::BI__sync_lock_test_and_set_8: |
| 714 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 6c59132 | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 715 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 716 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 717 | case Builtin::BI__sync_lock_release_1: |
| 718 | case Builtin::BI__sync_lock_release_2: |
| 719 | case Builtin::BI__sync_lock_release_4: |
| 720 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | afde259 | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 721 | case Builtin::BI__sync_lock_release_16: { |
| 722 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 723 | const llvm::Type *ElTy = |
| 724 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Daniel Dunbar | 1dd0164 | 2009-11-29 21:11:47 +0000 | [diff] [blame] | 725 | llvm::StoreInst *Store = |
| 726 | Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr); |
| 727 | Store->setVolatile(true); |
Daniel Dunbar | 16f422e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 728 | return RValue::get(0); |
Chris Lattner | afde259 | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 729 | } |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 730 | |
Chris Lattner | afde259 | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 731 | case Builtin::BI__sync_synchronize: { |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame] | 732 | // We assume like gcc appears to, that this only applies to cached memory. |
| 733 | EmitMemoryBarrier(*this, true, true, true, true, false); |
Daniel Dunbar | 16f422e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 734 | return RValue::get(0); |
Chris Lattner | afde259 | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 735 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | |
Tanya Lattner | dbfd160 | 2010-01-16 01:21:14 +0000 | [diff] [blame] | 737 | case Builtin::BI__builtin_llvm_memory_barrier: { |
| 738 | Value *C[5] = { |
| 739 | EmitScalarExpr(E->getArg(0)), |
| 740 | EmitScalarExpr(E->getArg(1)), |
| 741 | EmitScalarExpr(E->getArg(2)), |
| 742 | EmitScalarExpr(E->getArg(3)), |
| 743 | EmitScalarExpr(E->getArg(4)) |
| 744 | }; |
| 745 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
| 746 | return RValue::get(0); |
| 747 | } |
| 748 | |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 749 | // Library functions with special handling. |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 750 | case Builtin::BIsqrt: |
| 751 | case Builtin::BIsqrtf: |
| 752 | case Builtin::BIsqrtl: { |
John McCall | 8586bfd | 2010-04-07 08:20:20 +0000 | [diff] [blame] | 753 | // TODO: there is currently no set of optimizer flags |
| 754 | // sufficient for us to rewrite sqrt to @llvm.sqrt. |
| 755 | // -fmath-errno=0 is not good enough; we need finiteness. |
| 756 | // We could probably precondition the call with an ult |
| 757 | // against 0, but is that worth the complexity? |
| 758 | break; |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | case Builtin::BIpow: |
| 762 | case Builtin::BIpowf: |
| 763 | case Builtin::BIpowl: { |
| 764 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 765 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 766 | break; |
| 767 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 768 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 769 | const llvm::Type *ArgType = Base->getType(); |
| 770 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 771 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 772 | } |
Eli Friedman | 99d20f8 | 2010-03-06 02:17:52 +0000 | [diff] [blame] | 773 | |
| 774 | case Builtin::BI__builtin_signbit: |
| 775 | case Builtin::BI__builtin_signbitf: |
| 776 | case Builtin::BI__builtin_signbitl: { |
| 777 | LLVMContext &C = CGM.getLLVMContext(); |
| 778 | |
| 779 | Value *Arg = EmitScalarExpr(E->getArg(0)); |
| 780 | const llvm::Type *ArgTy = Arg->getType(); |
| 781 | if (ArgTy->isPPC_FP128Ty()) |
| 782 | break; // FIXME: I'm not sure what the right implementation is here. |
| 783 | int ArgWidth = ArgTy->getPrimitiveSizeInBits(); |
| 784 | const llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth); |
| 785 | Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy); |
| 786 | Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy); |
| 787 | Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp); |
| 788 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 789 | } |
Nate Begeman | 6c59132 | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 790 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 792 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 793 | // that function. |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 794 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 795 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 796 | return EmitCall(E->getCallee()->getType(), |
| 797 | CGM.getBuiltinLibFunction(FD, BuiltinID), |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 798 | ReturnValueSlot(), |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 799 | E->arg_begin(), E->arg_end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 801 | // See if we have a target specific intrinsic. |
Dale Johannesen | 621c351 | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 802 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 803 | Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic; |
| 804 | if (const char *Prefix = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | llvm::Triple::getArchTypePrefix(Target.getTriple().getArch())) |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 806 | IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 808 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 809 | SmallVector<Value*, 16> Args; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 811 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 812 | const llvm::FunctionType *FTy = F->getFunctionType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 814 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 815 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 817 | // If the intrinsic arg type is different from the builtin arg type |
| 818 | // we need to do a bit cast. |
| 819 | const llvm::Type *PTy = FTy->getParamType(i); |
| 820 | if (PTy != ArgValue->getType()) { |
| 821 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 822 | "Must be able to losslessly bit cast to param"); |
| 823 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 824 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 826 | Args.push_back(ArgValue); |
| 827 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 829 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 830 | QualType BuiltinRetType = E->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 832 | const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 833 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 835 | if (RetTy != V->getType()) { |
| 836 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 837 | "Must be able to losslessly bit cast result type"); |
| 838 | V = Builder.CreateBitCast(V, RetTy); |
| 839 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 841 | return RValue::get(V); |
| 842 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 844 | // See if we have a target specific builtin that needs to be lowered. |
Daniel Dunbar | eca513d | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 845 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 846 | return RValue::get(V); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 848 | ErrorUnsupported(E, "builtin function"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 850 | // Unknown builtin, for now just dump it out and return undef. |
| 851 | if (hasAggregateLLVMType(E->getType())) |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 852 | return RValue::getAggregate(CreateMemTemp(E->getType())); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 853 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | } |
Anders Carlsson | 895af08 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 855 | |
Daniel Dunbar | eca513d | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 856 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 857 | const CallExpr *E) { |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 858 | switch (Target.getTriple().getArch()) { |
Chris Lattner | 5cc15e0 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 859 | case llvm::Triple::arm: |
| 860 | case llvm::Triple::thumb: |
| 861 | return EmitARMBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 862 | case llvm::Triple::x86: |
| 863 | case llvm::Triple::x86_64: |
Daniel Dunbar | eca513d | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 864 | return EmitX86BuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 865 | case llvm::Triple::ppc: |
| 866 | case llvm::Triple::ppc64: |
Daniel Dunbar | eca513d | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 867 | return EmitPPCBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 868 | default: |
| 869 | return 0; |
| 870 | } |
Daniel Dunbar | eca513d | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Chris Lattner | 5cc15e0 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 873 | Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, |
| 874 | const CallExpr *E) { |
| 875 | switch (BuiltinID) { |
| 876 | default: return 0; |
| 877 | |
| 878 | case ARM::BI__builtin_thread_pointer: { |
| 879 | Value *AtomF = CGM.getIntrinsic(Intrinsic::arm_thread_pointer, 0, 0); |
| 880 | return Builder.CreateCall(AtomF); |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 886 | const CallExpr *E) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 888 | llvm::SmallVector<Value*, 4> Ops; |
| 889 | |
| 890 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 891 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 892 | |
Anders Carlsson | 895af08 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 893 | switch (BuiltinID) { |
Anders Carlsson | 92c4e44 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 894 | default: return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | case X86::BI__builtin_ia32_pslldi128: |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 896 | case X86::BI__builtin_ia32_psllqi128: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | case X86::BI__builtin_ia32_psllwi128: |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 898 | case X86::BI__builtin_ia32_psradi128: |
| 899 | case X86::BI__builtin_ia32_psrawi128: |
| 900 | case X86::BI__builtin_ia32_psrldi128: |
| 901 | case X86::BI__builtin_ia32_psrlqi128: |
| 902 | case X86::BI__builtin_ia32_psrlwi128: { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 903 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 904 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 2); |
| 905 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 906 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 907 | Ops[1], Zero, "insert"); |
| 908 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 909 | const char *name = 0; |
| 910 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 912 | switch (BuiltinID) { |
| 913 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 914 | case X86::BI__builtin_ia32_pslldi128: |
| 915 | name = "pslldi"; |
| 916 | ID = Intrinsic::x86_sse2_psll_d; |
| 917 | break; |
| 918 | case X86::BI__builtin_ia32_psllqi128: |
| 919 | name = "psllqi"; |
| 920 | ID = Intrinsic::x86_sse2_psll_q; |
| 921 | break; |
| 922 | case X86::BI__builtin_ia32_psllwi128: |
| 923 | name = "psllwi"; |
| 924 | ID = Intrinsic::x86_sse2_psll_w; |
| 925 | break; |
| 926 | case X86::BI__builtin_ia32_psradi128: |
| 927 | name = "psradi"; |
| 928 | ID = Intrinsic::x86_sse2_psra_d; |
| 929 | break; |
| 930 | case X86::BI__builtin_ia32_psrawi128: |
| 931 | name = "psrawi"; |
| 932 | ID = Intrinsic::x86_sse2_psra_w; |
| 933 | break; |
| 934 | case X86::BI__builtin_ia32_psrldi128: |
| 935 | name = "psrldi"; |
| 936 | ID = Intrinsic::x86_sse2_psrl_d; |
| 937 | break; |
| 938 | case X86::BI__builtin_ia32_psrlqi128: |
| 939 | name = "psrlqi"; |
| 940 | ID = Intrinsic::x86_sse2_psrl_q; |
| 941 | break; |
| 942 | case X86::BI__builtin_ia32_psrlwi128: |
| 943 | name = "psrlwi"; |
| 944 | ID = Intrinsic::x86_sse2_psrl_w; |
| 945 | break; |
| 946 | } |
| 947 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 949 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | case X86::BI__builtin_ia32_pslldi: |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 951 | case X86::BI__builtin_ia32_psllqi: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | case X86::BI__builtin_ia32_psllwi: |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 953 | case X86::BI__builtin_ia32_psradi: |
| 954 | case X86::BI__builtin_ia32_psrawi: |
| 955 | case X86::BI__builtin_ia32_psrldi: |
| 956 | case X86::BI__builtin_ia32_psrlqi: |
| 957 | case X86::BI__builtin_ia32_psrlwi: { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 958 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 959 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 1); |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 960 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 961 | const char *name = 0; |
| 962 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 964 | switch (BuiltinID) { |
| 965 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 966 | case X86::BI__builtin_ia32_pslldi: |
| 967 | name = "pslldi"; |
| 968 | ID = Intrinsic::x86_mmx_psll_d; |
| 969 | break; |
| 970 | case X86::BI__builtin_ia32_psllqi: |
| 971 | name = "psllqi"; |
| 972 | ID = Intrinsic::x86_mmx_psll_q; |
| 973 | break; |
| 974 | case X86::BI__builtin_ia32_psllwi: |
| 975 | name = "psllwi"; |
| 976 | ID = Intrinsic::x86_mmx_psll_w; |
| 977 | break; |
| 978 | case X86::BI__builtin_ia32_psradi: |
| 979 | name = "psradi"; |
| 980 | ID = Intrinsic::x86_mmx_psra_d; |
| 981 | break; |
| 982 | case X86::BI__builtin_ia32_psrawi: |
| 983 | name = "psrawi"; |
| 984 | ID = Intrinsic::x86_mmx_psra_w; |
| 985 | break; |
| 986 | case X86::BI__builtin_ia32_psrldi: |
| 987 | name = "psrldi"; |
| 988 | ID = Intrinsic::x86_mmx_psrl_d; |
| 989 | break; |
| 990 | case X86::BI__builtin_ia32_psrlqi: |
| 991 | name = "psrlqi"; |
| 992 | ID = Intrinsic::x86_mmx_psrl_q; |
| 993 | break; |
| 994 | case X86::BI__builtin_ia32_psrlwi: |
| 995 | name = "psrlwi"; |
| 996 | ID = Intrinsic::x86_mmx_psrl_w; |
| 997 | break; |
| 998 | } |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 999 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 1001 | } |
Anders Carlsson | 2081200 | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 1002 | case X86::BI__builtin_ia32_cmpps: { |
| 1003 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 1004 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 1005 | } |
| 1006 | case X86::BI__builtin_ia32_cmpss: { |
| 1007 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 1008 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | ca6bcae | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 1009 | } |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1010 | case X86::BI__builtin_ia32_ldmxcsr: { |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1011 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1012 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 1013 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1014 | Builder.CreateStore(Ops[0], Tmp); |
| 1015 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | dbcc2ca | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 1016 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1017 | } |
| 1018 | case X86::BI__builtin_ia32_stmxcsr: { |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1019 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1020 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 1021 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1022 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | dbcc2ca | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 1023 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1024 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 1025 | } |
Anders Carlsson | 2081200 | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 1026 | case X86::BI__builtin_ia32_cmppd: { |
| 1027 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 1028 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 1029 | } |
| 1030 | case X86::BI__builtin_ia32_cmpsd: { |
| 1031 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 1032 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | ca6bcae | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 1033 | } |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1034 | case X86::BI__builtin_ia32_storehps: |
| 1035 | case X86::BI__builtin_ia32_storelps: { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1036 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1037 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 1038 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1040 | // cast val v2i64 |
| 1041 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1043 | // extract (0, 1) |
| 1044 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1045 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Index); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 1046 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 1047 | |
| 1048 | // cast pointer to i64 & store |
| 1049 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 1050 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 1051 | } |
Eric Christopher | c377c81 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 1052 | case X86::BI__builtin_ia32_palignr: { |
Eric Christopher | 1bbc708 | 2010-04-15 01:43:08 +0000 | [diff] [blame] | 1053 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 1054 | |
| 1055 | // If palignr is shifting the pair of input vectors less than 9 bytes, |
| 1056 | // emit a shuffle instruction. |
| 1057 | if (shiftVal <= 8) { |
| 1058 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 1059 | |
| 1060 | llvm::SmallVector<llvm::Constant*, 8> Indices; |
| 1061 | for (unsigned i = 0; i != 8; ++i) |
| 1062 | Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i)); |
| 1063 | |
| 1064 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1065 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 1066 | } |
| 1067 | |
| 1068 | // If palignr is shifting the pair of input vectors more than 8 but less |
| 1069 | // than 16 bytes, emit a logical right shift of the destination. |
| 1070 | if (shiftVal < 16) { |
| 1071 | // MMX has these as 1 x i64 vectors for some odd optimization reasons. |
| 1072 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
| 1073 | const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 1); |
| 1074 | |
| 1075 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 1076 | Ops[1] = llvm::ConstantInt::get(VecTy, (shiftVal-8) * 8); |
| 1077 | |
| 1078 | // create i32 constant |
| 1079 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_mmx_psrl_q); |
| 1080 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 1081 | } |
| 1082 | |
| 1083 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 1084 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Nate Begeman | 67dfd42 | 2009-12-14 05:15:02 +0000 | [diff] [blame] | 1085 | } |
| 1086 | case X86::BI__builtin_ia32_palignr128: { |
Nate Begeman | 72ec6bc | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 1087 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 1088 | |
| 1089 | // If palignr is shifting the pair of input vectors less than 17 bytes, |
| 1090 | // emit a shuffle instruction. |
| 1091 | if (shiftVal <= 16) { |
| 1092 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 1093 | |
| 1094 | llvm::SmallVector<llvm::Constant*, 16> Indices; |
| 1095 | for (unsigned i = 0; i != 16; ++i) |
| 1096 | Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i)); |
| 1097 | |
| 1098 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1099 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 1100 | } |
| 1101 | |
| 1102 | // If palignr is shifting the pair of input vectors more than 16 but less |
| 1103 | // than 32 bytes, emit a logical right shift of the destination. |
| 1104 | if (shiftVal < 32) { |
| 1105 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
| 1106 | const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 1107 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 1108 | |
| 1109 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 1110 | Ops[1] = llvm::ConstantInt::get(IntTy, (shiftVal-16) * 8); |
| 1111 | |
| 1112 | // create i32 constant |
| 1113 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq); |
| 1114 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 1115 | } |
| 1116 | |
| 1117 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 1118 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Eric Christopher | c377c81 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 1119 | } |
Anders Carlsson | 895af08 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 1124 | const CallExpr *E) { |
Chris Lattner | dad4062 | 2010-04-14 03:54:58 +0000 | [diff] [blame] | 1125 | llvm::SmallVector<Value*, 4> Ops; |
| 1126 | |
| 1127 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 1128 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 1129 | |
| 1130 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
| 1131 | |
| 1132 | switch (BuiltinID) { |
| 1133 | default: return 0; |
| 1134 | |
| 1135 | // vec_st |
| 1136 | case PPC::BI__builtin_altivec_stvx: |
| 1137 | case PPC::BI__builtin_altivec_stvxl: |
| 1138 | case PPC::BI__builtin_altivec_stvebx: |
| 1139 | case PPC::BI__builtin_altivec_stvehx: |
| 1140 | case PPC::BI__builtin_altivec_stvewx: |
| 1141 | { |
| 1142 | Ops[2] = Builder.CreateBitCast(Ops[2], llvm::Type::getInt8PtrTy(VMContext)); |
| 1143 | Ops[1] = !isa<Constant>(Ops[1]) || !cast<Constant>(Ops[1])->isNullValue() |
| 1144 | ? Builder.CreateGEP(Ops[2], Ops[1], "tmp") : Ops[2]; |
| 1145 | Ops.pop_back(); |
| 1146 | |
| 1147 | switch (BuiltinID) { |
| 1148 | default: assert(0 && "Unsupported vavg intrinsic!"); |
| 1149 | case PPC::BI__builtin_altivec_stvx: |
| 1150 | ID = Intrinsic::ppc_altivec_stvx; |
| 1151 | break; |
| 1152 | case PPC::BI__builtin_altivec_stvxl: |
| 1153 | ID = Intrinsic::ppc_altivec_stvxl; |
| 1154 | break; |
| 1155 | case PPC::BI__builtin_altivec_stvebx: |
| 1156 | ID = Intrinsic::ppc_altivec_stvebx; |
| 1157 | break; |
| 1158 | case PPC::BI__builtin_altivec_stvehx: |
| 1159 | ID = Intrinsic::ppc_altivec_stvehx; |
| 1160 | break; |
| 1161 | case PPC::BI__builtin_altivec_stvewx: |
| 1162 | ID = Intrinsic::ppc_altivec_stvewx; |
| 1163 | break; |
| 1164 | } |
| 1165 | llvm::Function *F = CGM.getIntrinsic(ID); |
| 1166 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), ""); |
| 1167 | } |
| 1168 | } |
Daniel Dunbar | 91b640a | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 1169 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | } |