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