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. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +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); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 94578cb | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 85 | if (Id == Intrinsic::atomic_load_nand) |
| 86 | Result = CGF.Builder.CreateNot(Result); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame^] | 88 | return RValue::get(CGF.Builder.CreateBinOp(Op, Result, Args[1])); |
Mon P Wang | b84407d | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 89 | } |
| 90 | |
John McCall | 515c3c5 | 2010-03-03 10:30:05 +0000 | [diff] [blame] | 91 | static llvm::ConstantInt *getInt32(llvm::LLVMContext &Context, int32_t Value) { |
| 92 | return llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), Value); |
| 93 | } |
| 94 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 96 | unsigned BuiltinID, const CallExpr *E) { |
Chris Lattner | 24355b5 | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 97 | // 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] | 98 | Expr::EvalResult Result; |
Chris Lattner | 67d7b92 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 99 | if (E->Evaluate(Result, CGM.getContext())) { |
Anders Carlsson | c968790 | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 100 | if (Result.Val.isInt()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | return RValue::get(llvm::ConstantInt::get(VMContext, |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 102 | Result.Val.getInt())); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 103 | else if (Result.Val.isFloat()) |
Owen Anderson | e05f2ed | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 104 | return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat())); |
Chris Lattner | a1518b1 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 105 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 24355b5 | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 107 | switch (BuiltinID) { |
| 108 | default: break; // Handle intrinsics and libm functions below. |
Chris Lattner | a97132a | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 109 | case Builtin::BI__builtin___CFStringMakeConstantString: |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 110 | case Builtin::BI__builtin___NSStringMakeConstantString: |
Anders Carlsson | 80f97ab | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 111 | return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0)); |
Chris Lattner | 0bf6791 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 112 | case Builtin::BI__builtin_stdarg_start: |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 113 | case Builtin::BI__builtin_va_start: |
| 114 | case Builtin::BI__builtin_va_end: { |
Daniel Dunbar | e9fcadd2 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 115 | Value *ArgValue = EmitVAListRef(E->getArg(0)); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 116 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 117 | if (ArgValue->getType() != DestType) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | ArgValue = Builder.CreateBitCast(ArgValue, DestType, |
Daniel Dunbar | e59313a | 2009-07-26 09:28:40 +0000 | [diff] [blame] | 119 | ArgValue->getName().data()); |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 120 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ? |
Chris Lattner | 0bf6791 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 122 | Intrinsic::vaend : Intrinsic::vastart; |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 123 | return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 124 | } |
Anders Carlsson | c0b0e59 | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 125 | case Builtin::BI__builtin_va_copy: { |
Eli Friedman | ddea0ad | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 126 | Value *DstPtr = EmitVAListRef(E->getArg(0)); |
| 127 | Value *SrcPtr = EmitVAListRef(E->getArg(1)); |
Anders Carlsson | c0b0e59 | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 128 | |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 129 | const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | c0b0e59 | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 130 | |
| 131 | DstPtr = Builder.CreateBitCast(DstPtr, Type); |
| 132 | SrcPtr = Builder.CreateBitCast(SrcPtr, Type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy), |
Chris Lattner | dbcc2ca | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 134 | DstPtr, SrcPtr)); |
Anders Carlsson | c0b0e59 | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 135 | } |
Anders Carlsson | 4f8eb12 | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 136 | case Builtin::BI__builtin_abs: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 138 | |
Chris Lattner | 28ee5b3 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 139 | Value *NegOp = Builder.CreateNeg(ArgValue, "neg"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | Value *CmpResult = |
| 141 | Builder.CreateICmpSGE(ArgValue, |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 142 | llvm::Constant::getNullValue(ArgValue->getType()), |
Chris Lattner | 28ee5b3 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 143 | "abscond"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | Value *Result = |
Anders Carlsson | 4f8eb12 | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 145 | Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Anders Carlsson | 4f8eb12 | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 147 | return RValue::get(Result); |
| 148 | } |
Anders Carlsson | 093f1a0 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 149 | case Builtin::BI__builtin_ctz: |
| 150 | case Builtin::BI__builtin_ctzl: |
| 151 | case Builtin::BI__builtin_ctzll: { |
| 152 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Anders Carlsson | 093f1a0 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 154 | const llvm::Type *ArgType = ArgValue->getType(); |
| 155 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
| 156 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Anders Carlsson | 093f1a0 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 158 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 159 | if (Result->getType() != ResultType) |
Duncan Sands | 7876dad | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 160 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 161 | "cast"); |
Anders Carlsson | 093f1a0 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 162 | return RValue::get(Result); |
| 163 | } |
Eli Friedman | 5e2281e | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 164 | case Builtin::BI__builtin_clz: |
| 165 | case Builtin::BI__builtin_clzl: |
| 166 | case Builtin::BI__builtin_clzll: { |
| 167 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Eli Friedman | 5e2281e | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 169 | const llvm::Type *ArgType = ArgValue->getType(); |
| 170 | Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1); |
| 171 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Eli Friedman | 5e2281e | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 173 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 174 | if (Result->getType() != ResultType) |
Duncan Sands | 7876dad | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 175 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 176 | "cast"); |
Eli Friedman | 5e2281e | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 177 | return RValue::get(Result); |
| 178 | } |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 179 | case Builtin::BI__builtin_ffs: |
| 180 | case Builtin::BI__builtin_ffsl: |
| 181 | case Builtin::BI__builtin_ffsll: { |
| 182 | // ffs(x) -> x ? cttz(x) + 1 : 0 |
| 183 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 185 | const llvm::Type *ArgType = ArgValue->getType(); |
| 186 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 188 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 190 | llvm::ConstantInt::get(ArgType, 1), "tmp"); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 191 | Value *Zero = llvm::Constant::getNullValue(ArgType); |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 192 | Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); |
| 193 | Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs"); |
| 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"); |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 197 | return RValue::get(Result); |
| 198 | } |
| 199 | case Builtin::BI__builtin_parity: |
| 200 | case Builtin::BI__builtin_parityl: |
| 201 | case Builtin::BI__builtin_parityll: { |
| 202 | // parity(x) -> ctpop(x) & 1 |
| 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::ctpop, &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()); |
| 209 | Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1), |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 211 | "tmp"); |
| 212 | if (Result->getType() != ResultType) |
Duncan Sands | 7876dad | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 213 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 214 | "cast"); |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 215 | return RValue::get(Result); |
| 216 | } |
| 217 | case Builtin::BI__builtin_popcount: |
| 218 | case Builtin::BI__builtin_popcountl: |
| 219 | case Builtin::BI__builtin_popcountll: { |
| 220 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 222 | const llvm::Type *ArgType = ArgValue->getType(); |
| 223 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
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 *ResultType = ConvertType(E->getType()); |
| 226 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 227 | if (Result->getType() != ResultType) |
Duncan Sands | 7876dad | 2009-11-16 13:11:21 +0000 | [diff] [blame] | 228 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 229 | "cast"); |
Daniel Dunbar | d93abc3 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 230 | return RValue::get(Result); |
| 231 | } |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 232 | case Builtin::BI__builtin_expect: |
Daniel Dunbar | c2f6796 | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 233 | // FIXME: pass expect through to LLVM |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 234 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Anders Carlsson | ef93b9d | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 235 | case Builtin::BI__builtin_bswap32: |
| 236 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 237 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | ef93b9d | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 238 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 239 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 240 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | } |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 242 | case Builtin::BI__builtin_object_size: { |
Mike Stump | 7a484dd | 2009-10-26 23:39:48 +0000 | [diff] [blame] | 243 | // We pass this builtin onto the optimizer so that it can |
| 244 | // figure out the object size in more complex cases. |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 245 | const llvm::Type *ResType[] = { |
| 246 | ConvertType(E->getType()) |
| 247 | }; |
Eric Christopher | c879156 | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 248 | |
| 249 | // LLVM only supports 0 and 2, make sure that we pass along that |
| 250 | // as a boolean. |
| 251 | Value *Ty = EmitScalarExpr(E->getArg(1)); |
| 252 | ConstantInt *CI = dyn_cast<ConstantInt>(Ty); |
| 253 | assert(CI); |
| 254 | uint64_t val = CI->getZExtValue(); |
| 255 | CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1); |
| 256 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 257 | Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1); |
| 258 | return RValue::get(Builder.CreateCall2(F, |
| 259 | EmitScalarExpr(E->getArg(0)), |
Eric Christopher | c879156 | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 260 | CI)); |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 261 | } |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 262 | case Builtin::BI__builtin_prefetch: { |
| 263 | Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); |
| 264 | // FIXME: Technically these constants should of type 'int', yes? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 266 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 268 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 3); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 269 | Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); |
| 270 | return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); |
Anders Carlsson | ef93b9d | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 271 | } |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 272 | case Builtin::BI__builtin_trap: { |
| 273 | Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); |
| 274 | return RValue::get(Builder.CreateCall(F)); |
| 275 | } |
Chris Lattner | bf20638 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 276 | case Builtin::BI__builtin_unreachable: { |
Mike Stump | b85ffb6 | 2009-12-16 03:07:12 +0000 | [diff] [blame] | 277 | if (CatchUndefined && HaveInsertPoint()) |
| 278 | EmitBranch(getTrapBB()); |
Chris Lattner | bf20638 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 279 | Value *V = Builder.CreateUnreachable(); |
| 280 | Builder.ClearInsertionPoint(); |
| 281 | return RValue::get(V); |
| 282 | } |
| 283 | |
Daniel Dunbar | c2f6796 | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 284 | case Builtin::BI__builtin_powi: |
| 285 | case Builtin::BI__builtin_powif: |
| 286 | case Builtin::BI__builtin_powil: { |
| 287 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 288 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | c2f6796 | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 289 | const llvm::Type *ArgType = Base->getType(); |
| 290 | Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); |
Daniel Dunbar | c2f6796 | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 291 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 292 | } |
| 293 | |
Chris Lattner | 6c9ffe9 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 294 | case Builtin::BI__builtin_isgreater: |
| 295 | case Builtin::BI__builtin_isgreaterequal: |
| 296 | case Builtin::BI__builtin_isless: |
| 297 | case Builtin::BI__builtin_islessequal: |
| 298 | case Builtin::BI__builtin_islessgreater: |
| 299 | case Builtin::BI__builtin_isunordered: { |
| 300 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 301 | // floating point values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
Chris Lattner | 6c9ffe9 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 303 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 6c9ffe9 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 305 | switch (BuiltinID) { |
| 306 | default: assert(0 && "Unknown ordered comparison"); |
| 307 | case Builtin::BI__builtin_isgreater: |
| 308 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 309 | break; |
| 310 | case Builtin::BI__builtin_isgreaterequal: |
| 311 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 312 | break; |
| 313 | case Builtin::BI__builtin_isless: |
| 314 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 315 | break; |
| 316 | case Builtin::BI__builtin_islessequal: |
| 317 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 318 | break; |
| 319 | case Builtin::BI__builtin_islessgreater: |
| 320 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 321 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | case Builtin::BI__builtin_isunordered: |
Chris Lattner | 6c9ffe9 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 323 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 324 | break; |
| 325 | } |
| 326 | // ZExt bool to int type. |
| 327 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 328 | "tmp")); |
| 329 | } |
Eli Friedman | 1c277d0 | 2009-09-01 04:19:44 +0000 | [diff] [blame] | 330 | case Builtin::BI__builtin_isnan: { |
| 331 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 332 | V = Builder.CreateFCmpUNO(V, V, "cmp"); |
| 333 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp")); |
| 334 | } |
Eli Friedman | f6bd150 | 2009-06-02 07:10:30 +0000 | [diff] [blame] | 335 | case Builtin::BIalloca: |
Chris Lattner | 22b9ff4 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 336 | case Builtin::BI__builtin_alloca: { |
| 337 | // FIXME: LLVM IR Should allow alloca with an i64 size! |
| 338 | Value *Size = EmitScalarExpr(E->getArg(0)); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 339 | Size = Builder.CreateIntCast(Size, llvm::Type::getInt32Ty(VMContext), false, "tmp"); |
| 340 | return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp")); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 341 | } |
Eli Friedman | d6ef69a | 2010-01-23 19:00:10 +0000 | [diff] [blame] | 342 | case Builtin::BIbzero: |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 343 | case Builtin::BI__builtin_bzero: { |
| 344 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 345 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 346 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0), |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 347 | EmitScalarExpr(E->getArg(1)), |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 348 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 349 | return RValue::get(Address); |
Chris Lattner | 22b9ff4 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 350 | } |
Eli Friedman | 7f4933f | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 351 | case Builtin::BImemcpy: |
Eli Friedman | a3a4068 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 352 | case Builtin::BI__builtin_memcpy: { |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 353 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 354 | Builder.CreateCall4(CGM.getMemCpyFn(), Address, |
| 355 | EmitScalarExpr(E->getArg(1)), |
| 356 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 357 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 358 | return RValue::get(Address); |
| 359 | } |
Eli Friedman | 7f4933f | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 360 | case Builtin::BImemmove: |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 361 | case Builtin::BI__builtin_memmove: { |
| 362 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 363 | Builder.CreateCall4(CGM.getMemMoveFn(), Address, |
| 364 | EmitScalarExpr(E->getArg(1)), |
| 365 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 366 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 367 | return RValue::get(Address); |
| 368 | } |
Eli Friedman | 7f4933f | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 369 | case Builtin::BImemset: |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 370 | case Builtin::BI__builtin_memset: { |
| 371 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 372 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Daniel Dunbar | e28b359 | 2009-02-16 21:52:05 +0000 | [diff] [blame] | 373 | Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 374 | llvm::Type::getInt8Ty(VMContext)), |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 375 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 376 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 327acd7 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 377 | return RValue::get(Address); |
Eli Friedman | a3a4068 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 378 | } |
John McCall | 515c3c5 | 2010-03-03 10:30:05 +0000 | [diff] [blame] | 379 | case Builtin::BI__builtin_dwarf_cfa: { |
| 380 | // The offset in bytes from the first argument to the CFA. |
| 381 | // |
| 382 | // Why on earth is this in the frontend? Is there any reason at |
| 383 | // all that the backend can't reasonably determine this while |
| 384 | // lowering llvm.eh.dwarf.cfa()? |
| 385 | // |
| 386 | // TODO: If there's a satisfactory reason, add a target hook for |
| 387 | // this instead of hard-coding 0, which is correct for most targets. |
| 388 | int32_t Offset = 0; |
| 389 | |
| 390 | Value *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa, 0, 0); |
| 391 | return RValue::get(Builder.CreateCall(F, getInt32(VMContext, Offset))); |
| 392 | } |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 393 | case Builtin::BI__builtin_return_address: { |
Anton Korobeynikov | 73d50b9 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 394 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 395 | Depth = Builder.CreateIntCast(Depth, |
| 396 | llvm::Type::getInt32Ty(VMContext), |
| 397 | false, "tmp"); |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 398 | Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); |
Anton Korobeynikov | 73d50b9 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 399 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 400 | } |
| 401 | case Builtin::BI__builtin_frame_address: { |
Anton Korobeynikov | 73d50b9 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 402 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 403 | Depth = Builder.CreateIntCast(Depth, |
| 404 | llvm::Type::getInt32Ty(VMContext), |
| 405 | false, "tmp"); |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 406 | Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
Anton Korobeynikov | 73d50b9 | 2009-12-27 14:27:22 +0000 | [diff] [blame] | 407 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 53e38bd | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 408 | } |
Eli Friedman | 5b73b5e | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 409 | case Builtin::BI__builtin_extract_return_addr: { |
John McCall | d4f4b7f | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 410 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 411 | Value *Result = getTargetHooks().decodeReturnAddress(*this, Address); |
| 412 | return RValue::get(Result); |
| 413 | } |
| 414 | case Builtin::BI__builtin_frob_return_addr: { |
| 415 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 416 | Value *Result = getTargetHooks().encodeReturnAddress(*this, Address); |
| 417 | return RValue::get(Result); |
Eli Friedman | 5b73b5e | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 418 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 419 | case Builtin::BI__builtin_dwarf_sp_column: { |
| 420 | const llvm::IntegerType *Ty |
| 421 | = cast<llvm::IntegerType>(ConvertType(E->getType())); |
| 422 | int Column = getTargetHooks().getDwarfEHStackPointer(CGM); |
| 423 | if (Column == -1) { |
| 424 | CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column"); |
| 425 | return RValue::get(llvm::UndefValue::get(Ty)); |
| 426 | } |
| 427 | return RValue::get(llvm::ConstantInt::get(Ty, Column, true)); |
| 428 | } |
| 429 | case Builtin::BI__builtin_init_dwarf_reg_size_table: { |
| 430 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 431 | if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address)) |
| 432 | CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table"); |
| 433 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
| 434 | } |
John McCall | 66769f8 | 2010-03-03 05:38:58 +0000 | [diff] [blame] | 435 | case Builtin::BI__builtin_eh_return: { |
| 436 | Value *Int = EmitScalarExpr(E->getArg(0)); |
| 437 | Value *Ptr = EmitScalarExpr(E->getArg(1)); |
| 438 | |
| 439 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType()); |
| 440 | assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) && |
| 441 | "LLVM's __builtin_eh_return only supports 32- and 64-bit variants"); |
| 442 | Value *F = CGM.getIntrinsic(IntTy->getBitWidth() == 32 |
| 443 | ? Intrinsic::eh_return_i32 |
| 444 | : Intrinsic::eh_return_i64, |
| 445 | 0, 0); |
| 446 | Builder.CreateCall2(F, Int, Ptr); |
| 447 | Value *V = Builder.CreateUnreachable(); |
| 448 | Builder.ClearInsertionPoint(); |
| 449 | return RValue::get(V); |
| 450 | } |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 451 | case Builtin::BI__builtin_unwind_init: { |
| 452 | Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0); |
| 453 | return RValue::get(Builder.CreateCall(F)); |
| 454 | } |
John McCall | 4b613fa | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 455 | case Builtin::BI__builtin_extend_pointer: { |
| 456 | // Extends a pointer to the size of an _Unwind_Word, which is |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 457 | // uint64_t on all platforms. Generally this gets poked into a |
| 458 | // register and eventually used as an address, so if the |
| 459 | // addressing registers are wider than pointers and the platform |
| 460 | // doesn't implicitly ignore high-order bits when doing |
| 461 | // addressing, we need to make sure we zext / sext based on |
| 462 | // the platform's expectations. |
John McCall | 4b613fa | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 463 | // |
| 464 | // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 465 | |
| 466 | LLVMContext &C = CGM.getLLVMContext(); |
| 467 | |
| 468 | // Cast the pointer to intptr_t. |
John McCall | 4b613fa | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 469 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 470 | const llvm::IntegerType *IntPtrTy = CGM.getTargetData().getIntPtrType(C); |
| 471 | Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast"); |
| 472 | |
| 473 | // If that's 64 bits, we're done. |
| 474 | if (IntPtrTy->getBitWidth() == 64) |
| 475 | return RValue::get(Result); |
| 476 | |
| 477 | // Otherwise, ask the codegen data what to do. |
| 478 | const llvm::IntegerType *Int64Ty = llvm::IntegerType::get(C, 64); |
John McCall | d4f4b7f | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 479 | if (getTargetHooks().extendPointerWithSExt()) |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 480 | return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext")); |
| 481 | else |
| 482 | return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext")); |
John McCall | 4b613fa | 2010-03-02 02:31:24 +0000 | [diff] [blame] | 483 | } |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 484 | #if 0 |
| 485 | // FIXME: Finish/enable when LLVM backend support stabilizes |
| 486 | case Builtin::BI__builtin_setjmp: { |
| 487 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
| 488 | // Store the frame pointer to the buffer |
| 489 | Value *FrameAddrF = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
| 490 | Value *FrameAddr = |
| 491 | Builder.CreateCall(FrameAddrF, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 492 | Constant::getNullValue(llvm::Type::getInt32Ty(VMContext))); |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 493 | Builder.CreateStore(FrameAddr, Buf); |
| 494 | // Call the setjmp intrinsic |
| 495 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp, 0, 0); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 496 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 497 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 498 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 499 | } |
| 500 | case Builtin::BI__builtin_longjmp: { |
| 501 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp, 0, 0); |
| 502 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 503 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | cb9d07c | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 504 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 505 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 506 | } |
| 507 | #endif |
Mon P Wang | b84407d | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 508 | case Builtin::BI__sync_fetch_and_add: |
Mon P Wang | b84407d | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 509 | case Builtin::BI__sync_fetch_and_sub: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 510 | case Builtin::BI__sync_fetch_and_or: |
| 511 | case Builtin::BI__sync_fetch_and_and: |
| 512 | case Builtin::BI__sync_fetch_and_xor: |
| 513 | case Builtin::BI__sync_add_and_fetch: |
| 514 | case Builtin::BI__sync_sub_and_fetch: |
| 515 | case Builtin::BI__sync_and_and_fetch: |
| 516 | case Builtin::BI__sync_or_and_fetch: |
| 517 | case Builtin::BI__sync_xor_and_fetch: |
| 518 | case Builtin::BI__sync_val_compare_and_swap: |
| 519 | case Builtin::BI__sync_bool_compare_and_swap: |
| 520 | case Builtin::BI__sync_lock_test_and_set: |
| 521 | case Builtin::BI__sync_lock_release: |
| 522 | assert(0 && "Shouldn't make it through sema"); |
| 523 | case Builtin::BI__sync_fetch_and_add_1: |
| 524 | case Builtin::BI__sync_fetch_and_add_2: |
| 525 | case Builtin::BI__sync_fetch_and_add_4: |
| 526 | case Builtin::BI__sync_fetch_and_add_8: |
| 527 | case Builtin::BI__sync_fetch_and_add_16: |
| 528 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); |
| 529 | case Builtin::BI__sync_fetch_and_sub_1: |
| 530 | case Builtin::BI__sync_fetch_and_sub_2: |
| 531 | case Builtin::BI__sync_fetch_and_sub_4: |
| 532 | case Builtin::BI__sync_fetch_and_sub_8: |
| 533 | case Builtin::BI__sync_fetch_and_sub_16: |
Mon P Wang | 28898b2 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 534 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 535 | case Builtin::BI__sync_fetch_and_or_1: |
| 536 | case Builtin::BI__sync_fetch_and_or_2: |
| 537 | case Builtin::BI__sync_fetch_and_or_4: |
| 538 | case Builtin::BI__sync_fetch_and_or_8: |
| 539 | case Builtin::BI__sync_fetch_and_or_16: |
| 540 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 541 | case Builtin::BI__sync_fetch_and_and_1: |
| 542 | case Builtin::BI__sync_fetch_and_and_2: |
| 543 | case Builtin::BI__sync_fetch_and_and_4: |
| 544 | case Builtin::BI__sync_fetch_and_and_8: |
| 545 | case Builtin::BI__sync_fetch_and_and_16: |
| 546 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 547 | case Builtin::BI__sync_fetch_and_xor_1: |
| 548 | case Builtin::BI__sync_fetch_and_xor_2: |
| 549 | case Builtin::BI__sync_fetch_and_xor_4: |
| 550 | case Builtin::BI__sync_fetch_and_xor_8: |
| 551 | case Builtin::BI__sync_fetch_and_xor_16: |
| 552 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
Chris Lattner | 94578cb | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 553 | case Builtin::BI__sync_fetch_and_nand_1: |
| 554 | case Builtin::BI__sync_fetch_and_nand_2: |
| 555 | case Builtin::BI__sync_fetch_and_nand_4: |
| 556 | case Builtin::BI__sync_fetch_and_nand_8: |
| 557 | case Builtin::BI__sync_fetch_and_nand_16: |
| 558 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 560 | // Clang extensions: not overloaded yet. |
Mon P Wang | b84407d | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 561 | case Builtin::BI__sync_fetch_and_min: |
| 562 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 563 | case Builtin::BI__sync_fetch_and_max: |
| 564 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 565 | case Builtin::BI__sync_fetch_and_umin: |
| 566 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 567 | case Builtin::BI__sync_fetch_and_umax: |
| 568 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 569 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 570 | case Builtin::BI__sync_add_and_fetch_1: |
| 571 | case Builtin::BI__sync_add_and_fetch_2: |
| 572 | case Builtin::BI__sync_add_and_fetch_4: |
| 573 | case Builtin::BI__sync_add_and_fetch_8: |
| 574 | case Builtin::BI__sync_add_and_fetch_16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 576 | llvm::Instruction::Add); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 577 | case Builtin::BI__sync_sub_and_fetch_1: |
| 578 | case Builtin::BI__sync_sub_and_fetch_2: |
| 579 | case Builtin::BI__sync_sub_and_fetch_4: |
| 580 | case Builtin::BI__sync_sub_and_fetch_8: |
| 581 | case Builtin::BI__sync_sub_and_fetch_16: |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 582 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, |
| 583 | llvm::Instruction::Sub); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 584 | case Builtin::BI__sync_and_and_fetch_1: |
| 585 | case Builtin::BI__sync_and_and_fetch_2: |
| 586 | case Builtin::BI__sync_and_and_fetch_4: |
| 587 | case Builtin::BI__sync_and_and_fetch_8: |
| 588 | case Builtin::BI__sync_and_and_fetch_16: |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 589 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, |
| 590 | llvm::Instruction::And); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 591 | case Builtin::BI__sync_or_and_fetch_1: |
| 592 | case Builtin::BI__sync_or_and_fetch_2: |
| 593 | case Builtin::BI__sync_or_and_fetch_4: |
| 594 | case Builtin::BI__sync_or_and_fetch_8: |
| 595 | case Builtin::BI__sync_or_and_fetch_16: |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 596 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, |
| 597 | llvm::Instruction::Or); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 598 | case Builtin::BI__sync_xor_and_fetch_1: |
| 599 | case Builtin::BI__sync_xor_and_fetch_2: |
| 600 | case Builtin::BI__sync_xor_and_fetch_4: |
| 601 | case Builtin::BI__sync_xor_and_fetch_8: |
| 602 | case Builtin::BI__sync_xor_and_fetch_16: |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 603 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, |
| 604 | llvm::Instruction::Xor); |
Chris Lattner | 94578cb | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 605 | case Builtin::BI__sync_nand_and_fetch_1: |
| 606 | case Builtin::BI__sync_nand_and_fetch_2: |
| 607 | case Builtin::BI__sync_nand_and_fetch_4: |
| 608 | case Builtin::BI__sync_nand_and_fetch_8: |
| 609 | case Builtin::BI__sync_nand_and_fetch_16: |
| 610 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E, |
| 611 | llvm::Instruction::And); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 613 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 614 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 615 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 616 | case Builtin::BI__sync_val_compare_and_swap_8: |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame^] | 617 | case Builtin::BI__sync_val_compare_and_swap_16: { |
Mon P Wang | b160a0d | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 618 | const llvm::Type *ResType[2]; |
| 619 | ResType[0]= ConvertType(E->getType()); |
| 620 | ResType[1] = ConvertType(E->getArg(0)->getType()); |
| 621 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame^] | 622 | Value *Args[3] = { EmitScalarExpr(E->getArg(0)), |
| 623 | EmitScalarExpr(E->getArg(1)), |
| 624 | EmitScalarExpr(E->getArg(2)) }; |
| 625 | return RValue::get(EmitCallWithBarrier(*this, AtomF, Args, Args + 3)); |
Anders Carlsson | ceced4c | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 626 | } |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 627 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 628 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 629 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 630 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 631 | case Builtin::BI__sync_bool_compare_and_swap_8: |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame^] | 632 | case Builtin::BI__sync_bool_compare_and_swap_16: { |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 633 | const llvm::Type *ResType[2]; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 634 | ResType[0]= ConvertType(E->getArg(1)->getType()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 635 | ResType[1] = llvm::PointerType::getUnqual(ResType[0]); |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 636 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
| 637 | Value *OldVal = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame^] | 638 | Value *Args[3] = { EmitScalarExpr(E->getArg(0)), |
| 639 | OldVal, |
| 640 | EmitScalarExpr(E->getArg(2)) }; |
| 641 | Value *PrevVal = EmitCallWithBarrier(*this, AtomF, Args, Args + 3); |
Daniel Dunbar | 4fab57d | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 642 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 643 | // zext bool to int. |
| 644 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 645 | } |
| 646 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 647 | case Builtin::BI__sync_lock_test_and_set_1: |
| 648 | case Builtin::BI__sync_lock_test_and_set_2: |
| 649 | case Builtin::BI__sync_lock_test_and_set_4: |
| 650 | case Builtin::BI__sync_lock_test_and_set_8: |
| 651 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 6c59132 | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 652 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame^] | 653 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 654 | case Builtin::BI__sync_lock_release_1: |
| 655 | case Builtin::BI__sync_lock_release_2: |
| 656 | case Builtin::BI__sync_lock_release_4: |
| 657 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | afde259 | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 658 | case Builtin::BI__sync_lock_release_16: { |
| 659 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 660 | const llvm::Type *ElTy = |
| 661 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Daniel Dunbar | 1dd0164 | 2009-11-29 21:11:47 +0000 | [diff] [blame] | 662 | llvm::StoreInst *Store = |
| 663 | Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr); |
| 664 | Store->setVolatile(true); |
Daniel Dunbar | 16f422e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 665 | return RValue::get(0); |
Chris Lattner | afde259 | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 666 | } |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 667 | |
Chris Lattner | afde259 | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 668 | case Builtin::BI__sync_synchronize: { |
Daniel Dunbar | 4ff562d | 2010-03-20 07:04:11 +0000 | [diff] [blame^] | 669 | // We assume like gcc appears to, that this only applies to cached memory. |
| 670 | EmitMemoryBarrier(*this, true, true, true, true, false); |
Daniel Dunbar | 16f422e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 671 | return RValue::get(0); |
Chris Lattner | afde259 | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 672 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
Tanya Lattner | dbfd160 | 2010-01-16 01:21:14 +0000 | [diff] [blame] | 674 | case Builtin::BI__builtin_llvm_memory_barrier: { |
| 675 | Value *C[5] = { |
| 676 | EmitScalarExpr(E->getArg(0)), |
| 677 | EmitScalarExpr(E->getArg(1)), |
| 678 | EmitScalarExpr(E->getArg(2)), |
| 679 | EmitScalarExpr(E->getArg(3)), |
| 680 | EmitScalarExpr(E->getArg(4)) |
| 681 | }; |
| 682 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
| 683 | return RValue::get(0); |
| 684 | } |
| 685 | |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 686 | // Library functions with special handling. |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 687 | case Builtin::BIsqrt: |
| 688 | case Builtin::BIsqrtf: |
| 689 | case Builtin::BIsqrtl: { |
| 690 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 691 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 692 | break; |
| 693 | Value *Arg0 = EmitScalarExpr(E->getArg(0)); |
| 694 | const llvm::Type *ArgType = Arg0->getType(); |
| 695 | Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1); |
| 696 | return RValue::get(Builder.CreateCall(F, Arg0, "tmp")); |
| 697 | } |
| 698 | |
| 699 | case Builtin::BIpow: |
| 700 | case Builtin::BIpowf: |
| 701 | case Builtin::BIpowl: { |
| 702 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 703 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | 8eb018a | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 704 | break; |
| 705 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 706 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 707 | const llvm::Type *ArgType = Base->getType(); |
| 708 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 709 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 710 | } |
Eli Friedman | 99d20f8 | 2010-03-06 02:17:52 +0000 | [diff] [blame] | 711 | |
| 712 | case Builtin::BI__builtin_signbit: |
| 713 | case Builtin::BI__builtin_signbitf: |
| 714 | case Builtin::BI__builtin_signbitl: { |
| 715 | LLVMContext &C = CGM.getLLVMContext(); |
| 716 | |
| 717 | Value *Arg = EmitScalarExpr(E->getArg(0)); |
| 718 | const llvm::Type *ArgTy = Arg->getType(); |
| 719 | if (ArgTy->isPPC_FP128Ty()) |
| 720 | break; // FIXME: I'm not sure what the right implementation is here. |
| 721 | int ArgWidth = ArgTy->getPrimitiveSizeInBits(); |
| 722 | const llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth); |
| 723 | Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy); |
| 724 | Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy); |
| 725 | Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp); |
| 726 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 727 | } |
Nate Begeman | 6c59132 | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 728 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 730 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 731 | // that function. |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 732 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 733 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 734 | return EmitCall(E->getCallee()->getType(), |
| 735 | CGM.getBuiltinLibFunction(FD, BuiltinID), |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 736 | ReturnValueSlot(), |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 737 | E->arg_begin(), E->arg_end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 739 | // See if we have a target specific intrinsic. |
Dale Johannesen | 621c351 | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 740 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 741 | Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic; |
| 742 | if (const char *Prefix = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | llvm::Triple::getArchTypePrefix(Target.getTriple().getArch())) |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 744 | IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 746 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 747 | SmallVector<Value*, 16> Args; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 749 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 750 | const llvm::FunctionType *FTy = F->getFunctionType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 752 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 753 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 755 | // If the intrinsic arg type is different from the builtin arg type |
| 756 | // we need to do a bit cast. |
| 757 | const llvm::Type *PTy = FTy->getParamType(i); |
| 758 | if (PTy != ArgValue->getType()) { |
| 759 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 760 | "Must be able to losslessly bit cast to param"); |
| 761 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 762 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 764 | Args.push_back(ArgValue); |
| 765 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 767 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 768 | QualType BuiltinRetType = E->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 770 | const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 771 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 773 | if (RetTy != V->getType()) { |
| 774 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 775 | "Must be able to losslessly bit cast result type"); |
| 776 | V = Builder.CreateBitCast(V, RetTy); |
| 777 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 779 | return RValue::get(V); |
| 780 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 782 | // 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] | 783 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 784 | return RValue::get(V); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 786 | ErrorUnsupported(E, "builtin function"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 788 | // Unknown builtin, for now just dump it out and return undef. |
| 789 | if (hasAggregateLLVMType(E->getType())) |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 790 | return RValue::getAggregate(CreateMemTemp(E->getType())); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 791 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | } |
Anders Carlsson | 895af08 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 793 | |
Daniel Dunbar | eca513d | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 794 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 795 | const CallExpr *E) { |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 796 | switch (Target.getTriple().getArch()) { |
Chris Lattner | 5cc15e0 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 797 | case llvm::Triple::arm: |
| 798 | case llvm::Triple::thumb: |
| 799 | return EmitARMBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 800 | case llvm::Triple::x86: |
| 801 | case llvm::Triple::x86_64: |
Daniel Dunbar | eca513d | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 802 | return EmitX86BuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 803 | case llvm::Triple::ppc: |
| 804 | case llvm::Triple::ppc64: |
Daniel Dunbar | eca513d | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 805 | return EmitPPCBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 576d90d | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 806 | default: |
| 807 | return 0; |
| 808 | } |
Daniel Dunbar | eca513d | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Chris Lattner | 5cc15e0 | 2010-03-03 19:03:45 +0000 | [diff] [blame] | 811 | Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, |
| 812 | const CallExpr *E) { |
| 813 | switch (BuiltinID) { |
| 814 | default: return 0; |
| 815 | |
| 816 | case ARM::BI__builtin_thread_pointer: { |
| 817 | Value *AtomF = CGM.getIntrinsic(Intrinsic::arm_thread_pointer, 0, 0); |
| 818 | return Builder.CreateCall(AtomF); |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 824 | const CallExpr *E) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 826 | llvm::SmallVector<Value*, 4> Ops; |
| 827 | |
| 828 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 829 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 830 | |
Anders Carlsson | 895af08 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 831 | switch (BuiltinID) { |
Anders Carlsson | 92c4e44 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 832 | default: return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | case X86::BI__builtin_ia32_pslldi128: |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 834 | case X86::BI__builtin_ia32_psllqi128: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | case X86::BI__builtin_ia32_psllwi128: |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 836 | case X86::BI__builtin_ia32_psradi128: |
| 837 | case X86::BI__builtin_ia32_psrawi128: |
| 838 | case X86::BI__builtin_ia32_psrldi128: |
| 839 | case X86::BI__builtin_ia32_psrlqi128: |
| 840 | case X86::BI__builtin_ia32_psrlwi128: { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 841 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 842 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 2); |
| 843 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 844 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 845 | Ops[1], Zero, "insert"); |
| 846 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 847 | const char *name = 0; |
| 848 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 850 | switch (BuiltinID) { |
| 851 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 852 | case X86::BI__builtin_ia32_pslldi128: |
| 853 | name = "pslldi"; |
| 854 | ID = Intrinsic::x86_sse2_psll_d; |
| 855 | break; |
| 856 | case X86::BI__builtin_ia32_psllqi128: |
| 857 | name = "psllqi"; |
| 858 | ID = Intrinsic::x86_sse2_psll_q; |
| 859 | break; |
| 860 | case X86::BI__builtin_ia32_psllwi128: |
| 861 | name = "psllwi"; |
| 862 | ID = Intrinsic::x86_sse2_psll_w; |
| 863 | break; |
| 864 | case X86::BI__builtin_ia32_psradi128: |
| 865 | name = "psradi"; |
| 866 | ID = Intrinsic::x86_sse2_psra_d; |
| 867 | break; |
| 868 | case X86::BI__builtin_ia32_psrawi128: |
| 869 | name = "psrawi"; |
| 870 | ID = Intrinsic::x86_sse2_psra_w; |
| 871 | break; |
| 872 | case X86::BI__builtin_ia32_psrldi128: |
| 873 | name = "psrldi"; |
| 874 | ID = Intrinsic::x86_sse2_psrl_d; |
| 875 | break; |
| 876 | case X86::BI__builtin_ia32_psrlqi128: |
| 877 | name = "psrlqi"; |
| 878 | ID = Intrinsic::x86_sse2_psrl_q; |
| 879 | break; |
| 880 | case X86::BI__builtin_ia32_psrlwi128: |
| 881 | name = "psrlwi"; |
| 882 | ID = Intrinsic::x86_sse2_psrl_w; |
| 883 | break; |
| 884 | } |
| 885 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 887 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | case X86::BI__builtin_ia32_pslldi: |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 889 | case X86::BI__builtin_ia32_psllqi: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | case X86::BI__builtin_ia32_psllwi: |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 891 | case X86::BI__builtin_ia32_psradi: |
| 892 | case X86::BI__builtin_ia32_psrawi: |
| 893 | case X86::BI__builtin_ia32_psrldi: |
| 894 | case X86::BI__builtin_ia32_psrlqi: |
| 895 | case X86::BI__builtin_ia32_psrlwi: { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 896 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 897 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 1); |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 898 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 899 | const char *name = 0; |
| 900 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 902 | switch (BuiltinID) { |
| 903 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 904 | case X86::BI__builtin_ia32_pslldi: |
| 905 | name = "pslldi"; |
| 906 | ID = Intrinsic::x86_mmx_psll_d; |
| 907 | break; |
| 908 | case X86::BI__builtin_ia32_psllqi: |
| 909 | name = "psllqi"; |
| 910 | ID = Intrinsic::x86_mmx_psll_q; |
| 911 | break; |
| 912 | case X86::BI__builtin_ia32_psllwi: |
| 913 | name = "psllwi"; |
| 914 | ID = Intrinsic::x86_mmx_psll_w; |
| 915 | break; |
| 916 | case X86::BI__builtin_ia32_psradi: |
| 917 | name = "psradi"; |
| 918 | ID = Intrinsic::x86_mmx_psra_d; |
| 919 | break; |
| 920 | case X86::BI__builtin_ia32_psrawi: |
| 921 | name = "psrawi"; |
| 922 | ID = Intrinsic::x86_mmx_psra_w; |
| 923 | break; |
| 924 | case X86::BI__builtin_ia32_psrldi: |
| 925 | name = "psrldi"; |
| 926 | ID = Intrinsic::x86_mmx_psrl_d; |
| 927 | break; |
| 928 | case X86::BI__builtin_ia32_psrlqi: |
| 929 | name = "psrlqi"; |
| 930 | ID = Intrinsic::x86_mmx_psrl_q; |
| 931 | break; |
| 932 | case X86::BI__builtin_ia32_psrlwi: |
| 933 | name = "psrlwi"; |
| 934 | ID = Intrinsic::x86_mmx_psrl_w; |
| 935 | break; |
| 936 | } |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 937 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Anders Carlsson | 4d3094a | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 939 | } |
Anders Carlsson | 2081200 | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 940 | case X86::BI__builtin_ia32_cmpps: { |
| 941 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 942 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 943 | } |
| 944 | case X86::BI__builtin_ia32_cmpss: { |
| 945 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 946 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | ca6bcae | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 947 | } |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 948 | case X86::BI__builtin_ia32_ldmxcsr: { |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 949 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 950 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 951 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 952 | Builder.CreateStore(Ops[0], Tmp); |
| 953 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | dbcc2ca | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 954 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 955 | } |
| 956 | case X86::BI__builtin_ia32_stmxcsr: { |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 957 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 958 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 959 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 960 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | dbcc2ca | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 961 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 962 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 963 | } |
Anders Carlsson | 2081200 | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 964 | case X86::BI__builtin_ia32_cmppd: { |
| 965 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 966 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 967 | } |
| 968 | case X86::BI__builtin_ia32_cmpsd: { |
| 969 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 970 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | ca6bcae | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 971 | } |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 972 | case X86::BI__builtin_ia32_storehps: |
| 973 | case X86::BI__builtin_ia32_storelps: { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 974 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 975 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 976 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 978 | // cast val v2i64 |
| 979 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 981 | // extract (0, 1) |
| 982 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 983 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Index); |
Nate Begeman | 91f40e3 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 984 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 985 | |
| 986 | // cast pointer to i64 & store |
| 987 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 988 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 989 | } |
Eric Christopher | c377c81 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 990 | case X86::BI__builtin_ia32_palignr: { |
Nate Begeman | 67dfd42 | 2009-12-14 05:15:02 +0000 | [diff] [blame] | 991 | Function *F = CGM.getIntrinsic(Intrinsic::x86_ssse3_palign_r); |
| 992 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size()); |
| 993 | } |
| 994 | case X86::BI__builtin_ia32_palignr128: { |
Nate Begeman | 72ec6bc | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 995 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 996 | |
| 997 | // If palignr is shifting the pair of input vectors less than 17 bytes, |
| 998 | // emit a shuffle instruction. |
| 999 | if (shiftVal <= 16) { |
| 1000 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 1001 | |
| 1002 | llvm::SmallVector<llvm::Constant*, 16> Indices; |
| 1003 | for (unsigned i = 0; i != 16; ++i) |
| 1004 | Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i)); |
| 1005 | |
| 1006 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 1007 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 1008 | } |
| 1009 | |
| 1010 | // If palignr is shifting the pair of input vectors more than 16 but less |
| 1011 | // than 32 bytes, emit a logical right shift of the destination. |
| 1012 | if (shiftVal < 32) { |
| 1013 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
| 1014 | const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 1015 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 1016 | |
| 1017 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 1018 | Ops[1] = llvm::ConstantInt::get(IntTy, (shiftVal-16) * 8); |
| 1019 | |
| 1020 | // create i32 constant |
| 1021 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq); |
| 1022 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 1023 | } |
| 1024 | |
| 1025 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 1026 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Eric Christopher | c377c81 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 1027 | } |
Anders Carlsson | 895af08 | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 13653d7 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 1032 | const CallExpr *E) { |
Daniel Dunbar | 91b640a | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 1033 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | } |