Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 1 | //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Builtin calls as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 1f32999 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 17 | #include "clang/AST/APValue.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Chris Lattner | 6b15cdc | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetBuiltins.h" |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 21 | #include "llvm/Intrinsics.h" |
Anders Carlsson | 022012e | 2007-08-20 18:05:56 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
Anders Carlsson | ca6fcfa | 2007-12-09 21:20:04 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 26 | /// Utility to insert an atomic instruction based on Instrinsic::ID |
| 27 | /// and the expression node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | static RValue EmitBinaryAtomic(CodeGenFunction& CGF, |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 29 | Intrinsic::ID Id, const CallExpr *E) { |
Mon P Wang | c500451 | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 30 | const llvm::Type *ResType[2]; |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 31 | ResType[0] = CGF.ConvertType(E->getType()); |
| 32 | ResType[1] = CGF.ConvertType(E->getArg(0)->getType()); |
| 33 | Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | return RValue::get(CGF.Builder.CreateCall2(AtomF, |
| 35 | CGF.EmitScalarExpr(E->getArg(0)), |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 36 | CGF.EmitScalarExpr(E->getArg(1)))); |
| 37 | } |
| 38 | |
| 39 | /// Utility to insert an atomic instruction based Instrinsic::ID and |
| 40 | // the expression node, where the return value is the result of the |
| 41 | // operation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | static RValue EmitBinaryAtomicPost(CodeGenFunction& CGF, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 43 | Intrinsic::ID Id, const CallExpr *E, |
| 44 | Instruction::BinaryOps Op) { |
| 45 | const llvm::Type *ResType[2]; |
| 46 | ResType[0] = CGF.ConvertType(E->getType()); |
| 47 | ResType[1] = CGF.ConvertType(E->getArg(0)->getType()); |
| 48 | Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2); |
| 49 | Value *Ptr = CGF.EmitScalarExpr(E->getArg(0)); |
| 50 | Value *Operand = CGF.EmitScalarExpr(E->getArg(1)); |
| 51 | Value *Result = CGF.Builder.CreateCall2(AtomF, Ptr, Operand); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 53 | if (Id == Intrinsic::atomic_load_nand) |
| 54 | Result = CGF.Builder.CreateNot(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
| 56 | |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 57 | return RValue::get(CGF.Builder.CreateBinOp(Op, Result, Operand)); |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 61 | unsigned BuiltinID, const CallExpr *E) { |
Chris Lattner | 564ea2a | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 62 | // See if we can constant fold this builtin. If so, don't emit it at all. |
Anders Carlsson | f35d35a | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 63 | Expr::EvalResult Result; |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 64 | if (E->Evaluate(Result, CGM.getContext())) { |
Anders Carlsson | f35d35a | 2008-12-01 02:31:41 +0000 | [diff] [blame] | 65 | if (Result.Val.isInt()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | return RValue::get(llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 67 | Result.Val.getInt())); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 68 | else if (Result.Val.isFloat()) |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 69 | return RValue::get(ConstantFP::get(VMContext, Result.Val.getFloat())); |
Chris Lattner | 1f32999 | 2008-10-06 06:09:18 +0000 | [diff] [blame] | 70 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 564ea2a | 2008-10-06 06:56:41 +0000 | [diff] [blame] | 72 | switch (BuiltinID) { |
| 73 | default: break; // Handle intrinsics and libm functions below. |
Chris Lattner | 506ff88 | 2008-10-06 07:26:43 +0000 | [diff] [blame] | 74 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 75 | return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0)); |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 76 | case Builtin::BI__builtin_stdarg_start: |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 77 | case Builtin::BI__builtin_va_start: |
| 78 | case Builtin::BI__builtin_va_end: { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 79 | Value *ArgValue = EmitVAListRef(E->getArg(0)); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 80 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 81 | if (ArgValue->getType() != DestType) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | ArgValue = Builder.CreateBitCast(ArgValue, DestType, |
Daniel Dunbar | b27ffbe | 2009-07-26 09:28:40 +0000 | [diff] [blame] | 83 | ArgValue->getName().data()); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 84 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ? |
Chris Lattner | 6a705f0 | 2008-07-09 17:28:44 +0000 | [diff] [blame] | 86 | Intrinsic::vaend : Intrinsic::vastart; |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 87 | return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 88 | } |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 89 | case Builtin::BI__builtin_va_copy: { |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 90 | Value *DstPtr = EmitVAListRef(E->getArg(0)); |
| 91 | Value *SrcPtr = EmitVAListRef(E->getArg(1)); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 92 | |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 93 | const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 94 | |
| 95 | DstPtr = Builder.CreateBitCast(DstPtr, Type); |
| 96 | SrcPtr = Builder.CreateBitCast(SrcPtr, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 98 | DstPtr, SrcPtr)); |
Anders Carlsson | a28ef8b | 2008-02-09 20:26:43 +0000 | [diff] [blame] | 99 | } |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 100 | case Builtin::BI__builtin_abs: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
| 102 | |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 103 | Value *NegOp = Builder.CreateNeg(ArgValue, "neg"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | Value *CmpResult = |
| 105 | Builder.CreateICmpSGE(ArgValue, |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 106 | llvm::Constant::getNullValue(ArgValue->getType()), |
Chris Lattner | 9a847f5 | 2008-07-23 06:53:34 +0000 | [diff] [blame] | 107 | "abscond"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | Value *Result = |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 109 | Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Anders Carlsson | c2251dc | 2007-11-20 19:05:17 +0000 | [diff] [blame] | 111 | return RValue::get(Result); |
| 112 | } |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 113 | case Builtin::BI__builtin_ctz: |
| 114 | case Builtin::BI__builtin_ctzl: |
| 115 | case Builtin::BI__builtin_ctzll: { |
| 116 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 118 | const llvm::Type *ArgType = ArgValue->getType(); |
| 119 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
| 120 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 122 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 123 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame^] | 124 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 125 | "cast"); |
Anders Carlsson | 3a31d60 | 2008-02-06 07:19:27 +0000 | [diff] [blame] | 126 | return RValue::get(Result); |
| 127 | } |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin_clz: |
| 129 | case Builtin::BI__builtin_clzl: |
| 130 | case Builtin::BI__builtin_clzll: { |
| 131 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 133 | const llvm::Type *ArgType = ArgValue->getType(); |
| 134 | Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1); |
| 135 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 137 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 138 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame^] | 139 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 140 | "cast"); |
Eli Friedman | f4e8533 | 2008-05-27 15:32:46 +0000 | [diff] [blame] | 141 | return RValue::get(Result); |
| 142 | } |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 143 | case Builtin::BI__builtin_ffs: |
| 144 | case Builtin::BI__builtin_ffsl: |
| 145 | case Builtin::BI__builtin_ffsll: { |
| 146 | // ffs(x) -> x ? cttz(x) + 1 : 0 |
| 147 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 149 | const llvm::Type *ArgType = ArgValue->getType(); |
| 150 | Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 152 | const llvm::Type *ResultType = ConvertType(E->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 154 | llvm::ConstantInt::get(ArgType, 1), "tmp"); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 155 | Value *Zero = llvm::Constant::getNullValue(ArgType); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 156 | Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); |
| 157 | Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs"); |
| 158 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame^] | 159 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 160 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 161 | return RValue::get(Result); |
| 162 | } |
| 163 | case Builtin::BI__builtin_parity: |
| 164 | case Builtin::BI__builtin_parityl: |
| 165 | case Builtin::BI__builtin_parityll: { |
| 166 | // parity(x) -> ctpop(x) & 1 |
| 167 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 169 | const llvm::Type *ArgType = ArgValue->getType(); |
| 170 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 172 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 173 | Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1), |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 175 | "tmp"); |
| 176 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame^] | 177 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 178 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 179 | return RValue::get(Result); |
| 180 | } |
| 181 | case Builtin::BI__builtin_popcount: |
| 182 | case Builtin::BI__builtin_popcountl: |
| 183 | case Builtin::BI__builtin_popcountll: { |
| 184 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 186 | const llvm::Type *ArgType = ArgValue->getType(); |
| 187 | Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 189 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 190 | Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); |
| 191 | if (Result->getType() != ResultType) |
Duncan Sands | eac73e5 | 2009-11-16 13:11:21 +0000 | [diff] [blame^] | 192 | Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, |
| 193 | "cast"); |
Daniel Dunbar | 04b2900 | 2008-07-21 17:19:41 +0000 | [diff] [blame] | 194 | return RValue::get(Result); |
| 195 | } |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 196 | case Builtin::BI__builtin_expect: |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 197 | // FIXME: pass expect through to LLVM |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 198 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 199 | case Builtin::BI__builtin_bswap32: |
| 200 | case Builtin::BI__builtin_bswap64: { |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 201 | Value *ArgValue = EmitScalarExpr(E->getArg(0)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 202 | const llvm::Type *ArgType = ArgValue->getType(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 203 | Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 204 | return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | } |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 206 | case Builtin::BI__builtin_object_size: { |
Mike Stump | 3ada2df | 2009-11-09 22:40:09 +0000 | [diff] [blame] | 207 | #if 1 |
Mike Stump | b16d32f | 2009-10-26 23:39:48 +0000 | [diff] [blame] | 208 | // We pass this builtin onto the optimizer so that it can |
| 209 | // figure out the object size in more complex cases. |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 210 | const llvm::Type *ResType[] = { |
| 211 | ConvertType(E->getType()) |
| 212 | }; |
| 213 | Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1); |
| 214 | return RValue::get(Builder.CreateCall2(F, |
| 215 | EmitScalarExpr(E->getArg(0)), |
| 216 | EmitScalarExpr(E->getArg(1)))); |
Mike Stump | b16d32f | 2009-10-26 23:39:48 +0000 | [diff] [blame] | 217 | #else |
Mike Stump | 3ada2df | 2009-11-09 22:40:09 +0000 | [diff] [blame] | 218 | // FIXME: Remove after testing. |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 219 | llvm::APSInt TypeArg = E->getArg(1)->EvaluateAsInt(CGM.getContext()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 220 | const llvm::Type *ResType = ConvertType(E->getType()); |
| 221 | // bool UseSubObject = TypeArg.getZExtValue() & 1; |
| 222 | bool UseMinimum = TypeArg.getZExtValue() & 2; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 223 | return RValue::get( |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 224 | llvm::ConstantInt::get(ResType, UseMinimum ? 0 : -1LL)); |
Mike Stump | b16d32f | 2009-10-26 23:39:48 +0000 | [diff] [blame] | 225 | #endif |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 226 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 227 | case Builtin::BI__builtin_prefetch: { |
| 228 | Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); |
| 229 | // FIXME: Technically these constants should of type 'int', yes? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 231 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 233 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 3); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 234 | Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); |
| 235 | return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 236 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 237 | case Builtin::BI__builtin_trap: { |
| 238 | Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); |
| 239 | return RValue::get(Builder.CreateCall(F)); |
| 240 | } |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 241 | case Builtin::BI__builtin_unreachable: { |
| 242 | Value *V = Builder.CreateUnreachable(); |
| 243 | Builder.ClearInsertionPoint(); |
| 244 | return RValue::get(V); |
| 245 | } |
| 246 | |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 247 | case Builtin::BI__builtin_powi: |
| 248 | case Builtin::BI__builtin_powif: |
| 249 | case Builtin::BI__builtin_powil: { |
| 250 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 251 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 252 | const llvm::Type *ArgType = Base->getType(); |
| 253 | Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 254 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 255 | } |
| 256 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 257 | case Builtin::BI__builtin_isgreater: |
| 258 | case Builtin::BI__builtin_isgreaterequal: |
| 259 | case Builtin::BI__builtin_isless: |
| 260 | case Builtin::BI__builtin_islessequal: |
| 261 | case Builtin::BI__builtin_islessgreater: |
| 262 | case Builtin::BI__builtin_isunordered: { |
| 263 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 264 | // floating point values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 266 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 268 | switch (BuiltinID) { |
| 269 | default: assert(0 && "Unknown ordered comparison"); |
| 270 | case Builtin::BI__builtin_isgreater: |
| 271 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 272 | break; |
| 273 | case Builtin::BI__builtin_isgreaterequal: |
| 274 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 275 | break; |
| 276 | case Builtin::BI__builtin_isless: |
| 277 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 278 | break; |
| 279 | case Builtin::BI__builtin_islessequal: |
| 280 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 281 | break; |
| 282 | case Builtin::BI__builtin_islessgreater: |
| 283 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 284 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | case Builtin::BI__builtin_isunordered: |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 286 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 287 | break; |
| 288 | } |
| 289 | // ZExt bool to int type. |
| 290 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 291 | "tmp")); |
| 292 | } |
Eli Friedman | d613989 | 2009-09-01 04:19:44 +0000 | [diff] [blame] | 293 | case Builtin::BI__builtin_isnan: { |
| 294 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 295 | V = Builder.CreateFCmpUNO(V, V, "cmp"); |
| 296 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp")); |
| 297 | } |
Eli Friedman | b52fe9c | 2009-06-02 07:10:30 +0000 | [diff] [blame] | 298 | case Builtin::BIalloca: |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 299 | case Builtin::BI__builtin_alloca: { |
| 300 | // FIXME: LLVM IR Should allow alloca with an i64 size! |
| 301 | Value *Size = EmitScalarExpr(E->getArg(0)); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 302 | Size = Builder.CreateIntCast(Size, llvm::Type::getInt32Ty(VMContext), false, "tmp"); |
| 303 | return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp")); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 304 | } |
| 305 | case Builtin::BI__builtin_bzero: { |
| 306 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 307 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 308 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0), |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 309 | EmitScalarExpr(E->getArg(1)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 310 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 311 | return RValue::get(Address); |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 312 | } |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 313 | case Builtin::BI__builtin_memcpy: { |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 314 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 315 | Builder.CreateCall4(CGM.getMemCpyFn(), Address, |
| 316 | EmitScalarExpr(E->getArg(1)), |
| 317 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 318 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 319 | return RValue::get(Address); |
| 320 | } |
| 321 | case Builtin::BI__builtin_memmove: { |
| 322 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 323 | Builder.CreateCall4(CGM.getMemMoveFn(), Address, |
| 324 | EmitScalarExpr(E->getArg(1)), |
| 325 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 326 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 327 | return RValue::get(Address); |
| 328 | } |
| 329 | case Builtin::BI__builtin_memset: { |
| 330 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 331 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Daniel Dunbar | 62c29c6 | 2009-02-16 21:52:05 +0000 | [diff] [blame] | 332 | Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 333 | llvm::Type::getInt8Ty(VMContext)), |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 334 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 335 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 336 | return RValue::get(Address); |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 337 | } |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 338 | case Builtin::BI__builtin_return_address: { |
| 339 | Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); |
| 340 | return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); |
| 341 | } |
| 342 | case Builtin::BI__builtin_frame_address: { |
| 343 | Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
| 344 | return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); |
| 345 | } |
Eli Friedman | 3b660ef | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 346 | case Builtin::BI__builtin_extract_return_addr: { |
| 347 | // FIXME: There should be a target hook for this |
| 348 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
| 349 | } |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 350 | case Builtin::BI__builtin_unwind_init: { |
| 351 | Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0); |
| 352 | return RValue::get(Builder.CreateCall(F)); |
| 353 | } |
| 354 | #if 0 |
| 355 | // FIXME: Finish/enable when LLVM backend support stabilizes |
| 356 | case Builtin::BI__builtin_setjmp: { |
| 357 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
| 358 | // Store the frame pointer to the buffer |
| 359 | Value *FrameAddrF = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
| 360 | Value *FrameAddr = |
| 361 | Builder.CreateCall(FrameAddrF, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 362 | Constant::getNullValue(llvm::Type::getInt32Ty(VMContext))); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 363 | Builder.CreateStore(FrameAddr, Buf); |
| 364 | // Call the setjmp intrinsic |
| 365 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp, 0, 0); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 366 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 367 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 368 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 369 | } |
| 370 | case Builtin::BI__builtin_longjmp: { |
| 371 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp, 0, 0); |
| 372 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 373 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 374 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 375 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 376 | } |
| 377 | #endif |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 378 | case Builtin::BI__sync_fetch_and_add: |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 379 | case Builtin::BI__sync_fetch_and_sub: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 380 | case Builtin::BI__sync_fetch_and_or: |
| 381 | case Builtin::BI__sync_fetch_and_and: |
| 382 | case Builtin::BI__sync_fetch_and_xor: |
| 383 | case Builtin::BI__sync_add_and_fetch: |
| 384 | case Builtin::BI__sync_sub_and_fetch: |
| 385 | case Builtin::BI__sync_and_and_fetch: |
| 386 | case Builtin::BI__sync_or_and_fetch: |
| 387 | case Builtin::BI__sync_xor_and_fetch: |
| 388 | case Builtin::BI__sync_val_compare_and_swap: |
| 389 | case Builtin::BI__sync_bool_compare_and_swap: |
| 390 | case Builtin::BI__sync_lock_test_and_set: |
| 391 | case Builtin::BI__sync_lock_release: |
| 392 | assert(0 && "Shouldn't make it through sema"); |
| 393 | case Builtin::BI__sync_fetch_and_add_1: |
| 394 | case Builtin::BI__sync_fetch_and_add_2: |
| 395 | case Builtin::BI__sync_fetch_and_add_4: |
| 396 | case Builtin::BI__sync_fetch_and_add_8: |
| 397 | case Builtin::BI__sync_fetch_and_add_16: |
| 398 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); |
| 399 | case Builtin::BI__sync_fetch_and_sub_1: |
| 400 | case Builtin::BI__sync_fetch_and_sub_2: |
| 401 | case Builtin::BI__sync_fetch_and_sub_4: |
| 402 | case Builtin::BI__sync_fetch_and_sub_8: |
| 403 | case Builtin::BI__sync_fetch_and_sub_16: |
Mon P Wang | 09b6bf5 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 404 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 405 | case Builtin::BI__sync_fetch_and_or_1: |
| 406 | case Builtin::BI__sync_fetch_and_or_2: |
| 407 | case Builtin::BI__sync_fetch_and_or_4: |
| 408 | case Builtin::BI__sync_fetch_and_or_8: |
| 409 | case Builtin::BI__sync_fetch_and_or_16: |
| 410 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 411 | case Builtin::BI__sync_fetch_and_and_1: |
| 412 | case Builtin::BI__sync_fetch_and_and_2: |
| 413 | case Builtin::BI__sync_fetch_and_and_4: |
| 414 | case Builtin::BI__sync_fetch_and_and_8: |
| 415 | case Builtin::BI__sync_fetch_and_and_16: |
| 416 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 417 | case Builtin::BI__sync_fetch_and_xor_1: |
| 418 | case Builtin::BI__sync_fetch_and_xor_2: |
| 419 | case Builtin::BI__sync_fetch_and_xor_4: |
| 420 | case Builtin::BI__sync_fetch_and_xor_8: |
| 421 | case Builtin::BI__sync_fetch_and_xor_16: |
| 422 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 423 | case Builtin::BI__sync_fetch_and_nand_1: |
| 424 | case Builtin::BI__sync_fetch_and_nand_2: |
| 425 | case Builtin::BI__sync_fetch_and_nand_4: |
| 426 | case Builtin::BI__sync_fetch_and_nand_8: |
| 427 | case Builtin::BI__sync_fetch_and_nand_16: |
| 428 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 430 | // Clang extensions: not overloaded yet. |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 431 | case Builtin::BI__sync_fetch_and_min: |
| 432 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 433 | case Builtin::BI__sync_fetch_and_max: |
| 434 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 435 | case Builtin::BI__sync_fetch_and_umin: |
| 436 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 437 | case Builtin::BI__sync_fetch_and_umax: |
| 438 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 440 | case Builtin::BI__sync_add_and_fetch_1: |
| 441 | case Builtin::BI__sync_add_and_fetch_2: |
| 442 | case Builtin::BI__sync_add_and_fetch_4: |
| 443 | case Builtin::BI__sync_add_and_fetch_8: |
| 444 | case Builtin::BI__sync_add_and_fetch_16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 446 | llvm::Instruction::Add); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 447 | case Builtin::BI__sync_sub_and_fetch_1: |
| 448 | case Builtin::BI__sync_sub_and_fetch_2: |
| 449 | case Builtin::BI__sync_sub_and_fetch_4: |
| 450 | case Builtin::BI__sync_sub_and_fetch_8: |
| 451 | case Builtin::BI__sync_sub_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 452 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, |
| 453 | llvm::Instruction::Sub); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 454 | case Builtin::BI__sync_and_and_fetch_1: |
| 455 | case Builtin::BI__sync_and_and_fetch_2: |
| 456 | case Builtin::BI__sync_and_and_fetch_4: |
| 457 | case Builtin::BI__sync_and_and_fetch_8: |
| 458 | case Builtin::BI__sync_and_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 459 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, |
| 460 | llvm::Instruction::And); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 461 | case Builtin::BI__sync_or_and_fetch_1: |
| 462 | case Builtin::BI__sync_or_and_fetch_2: |
| 463 | case Builtin::BI__sync_or_and_fetch_4: |
| 464 | case Builtin::BI__sync_or_and_fetch_8: |
| 465 | case Builtin::BI__sync_or_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 466 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, |
| 467 | llvm::Instruction::Or); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 468 | case Builtin::BI__sync_xor_and_fetch_1: |
| 469 | case Builtin::BI__sync_xor_and_fetch_2: |
| 470 | case Builtin::BI__sync_xor_and_fetch_4: |
| 471 | case Builtin::BI__sync_xor_and_fetch_8: |
| 472 | case Builtin::BI__sync_xor_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 473 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, |
| 474 | llvm::Instruction::Xor); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 475 | case Builtin::BI__sync_nand_and_fetch_1: |
| 476 | case Builtin::BI__sync_nand_and_fetch_2: |
| 477 | case Builtin::BI__sync_nand_and_fetch_4: |
| 478 | case Builtin::BI__sync_nand_and_fetch_8: |
| 479 | case Builtin::BI__sync_nand_and_fetch_16: |
| 480 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E, |
| 481 | llvm::Instruction::And); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 483 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 484 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 485 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 486 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 487 | case Builtin::BI__sync_val_compare_and_swap_16: |
| 488 | { |
Mon P Wang | c500451 | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 489 | const llvm::Type *ResType[2]; |
| 490 | ResType[0]= ConvertType(E->getType()); |
| 491 | ResType[1] = ConvertType(E->getArg(0)->getType()); |
| 492 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | return RValue::get(Builder.CreateCall3(AtomF, |
Anders Carlsson | 9584446 | 2008-08-30 16:17:45 +0000 | [diff] [blame] | 494 | EmitScalarExpr(E->getArg(0)), |
| 495 | EmitScalarExpr(E->getArg(1)), |
| 496 | EmitScalarExpr(E->getArg(2)))); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 497 | } |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 499 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 500 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 501 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 502 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 503 | case Builtin::BI__sync_bool_compare_and_swap_16: |
| 504 | { |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 505 | const llvm::Type *ResType[2]; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 506 | ResType[0]= ConvertType(E->getArg(1)->getType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 507 | ResType[1] = llvm::PointerType::getUnqual(ResType[0]); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 508 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
| 509 | Value *OldVal = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | Value *PrevVal = Builder.CreateCall3(AtomF, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 511 | EmitScalarExpr(E->getArg(0)), |
| 512 | OldVal, |
| 513 | EmitScalarExpr(E->getArg(2))); |
| 514 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 515 | // zext bool to int. |
| 516 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 517 | } |
| 518 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 519 | case Builtin::BI__sync_lock_test_and_set_1: |
| 520 | case Builtin::BI__sync_lock_test_and_set_2: |
| 521 | case Builtin::BI__sync_lock_test_and_set_4: |
| 522 | case Builtin::BI__sync_lock_test_and_set_8: |
| 523 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 524 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 525 | case Builtin::BI__sync_lock_release_1: |
| 526 | case Builtin::BI__sync_lock_release_2: |
| 527 | case Builtin::BI__sync_lock_release_4: |
| 528 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 529 | case Builtin::BI__sync_lock_release_16: { |
| 530 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 531 | const llvm::Type *ElTy = |
| 532 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 533 | Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr, true); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 534 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 535 | } |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 536 | |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 537 | case Builtin::BI__sync_synchronize: { |
| 538 | Value *C[5]; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 539 | C[0] = C[1] = C[2] = C[3] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 1); |
| 540 | C[4] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 541 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 542 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 543 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 545 | // Library functions with special handling. |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 546 | case Builtin::BIsqrt: |
| 547 | case Builtin::BIsqrtf: |
| 548 | case Builtin::BIsqrtl: { |
| 549 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 550 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 551 | break; |
| 552 | Value *Arg0 = EmitScalarExpr(E->getArg(0)); |
| 553 | const llvm::Type *ArgType = Arg0->getType(); |
| 554 | Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1); |
| 555 | return RValue::get(Builder.CreateCall(F, Arg0, "tmp")); |
| 556 | } |
| 557 | |
| 558 | case Builtin::BIpow: |
| 559 | case Builtin::BIpowf: |
| 560 | case Builtin::BIpowl: { |
| 561 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 562 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 563 | break; |
| 564 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 565 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 566 | const llvm::Type *ArgType = Base->getType(); |
| 567 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 568 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 569 | } |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 570 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 572 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 573 | // that function. |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 574 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 575 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Daniel Dunbar | 34771b5 | 2009-09-14 04:33:21 +0000 | [diff] [blame] | 576 | return EmitCall(CGM.getBuiltinLibFunction(FD, BuiltinID), |
Anders Carlsson | 9864771 | 2009-05-27 01:22:39 +0000 | [diff] [blame] | 577 | E->getCallee()->getType(), E->arg_begin(), |
| 578 | E->arg_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 580 | // See if we have a target specific intrinsic. |
Dale Johannesen | a6f80ef | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 581 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 582 | Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic; |
| 583 | if (const char *Prefix = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | llvm::Triple::getArchTypePrefix(Target.getTriple().getArch())) |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 585 | IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 587 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 588 | SmallVector<Value*, 16> Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 590 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 591 | const llvm::FunctionType *FTy = F->getFunctionType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 593 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 594 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 596 | // If the intrinsic arg type is different from the builtin arg type |
| 597 | // we need to do a bit cast. |
| 598 | const llvm::Type *PTy = FTy->getParamType(i); |
| 599 | if (PTy != ArgValue->getType()) { |
| 600 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 601 | "Must be able to losslessly bit cast to param"); |
| 602 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 603 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 605 | Args.push_back(ArgValue); |
| 606 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 608 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 609 | QualType BuiltinRetType = E->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 611 | const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 612 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 614 | if (RetTy != V->getType()) { |
| 615 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 616 | "Must be able to losslessly bit cast result type"); |
| 617 | V = Builder.CreateBitCast(V, RetTy); |
| 618 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 620 | return RValue::get(V); |
| 621 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 623 | // See if we have a target specific builtin that needs to be lowered. |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 624 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 625 | return RValue::get(V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 627 | ErrorUnsupported(E, "builtin function"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 629 | // Unknown builtin, for now just dump it out and return undef. |
| 630 | if (hasAggregateLLVMType(E->getType())) |
| 631 | return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType()))); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 632 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 634 | |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 635 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 636 | const CallExpr *E) { |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 637 | switch (Target.getTriple().getArch()) { |
| 638 | case llvm::Triple::x86: |
| 639 | case llvm::Triple::x86_64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 640 | return EmitX86BuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 641 | case llvm::Triple::ppc: |
| 642 | case llvm::Triple::ppc64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 643 | return EmitPPCBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 644 | default: |
| 645 | return 0; |
| 646 | } |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 650 | const CallExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 652 | llvm::SmallVector<Value*, 4> Ops; |
| 653 | |
| 654 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 655 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 656 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 657 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 658 | default: return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | case X86::BI__builtin_ia32_pslldi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 660 | case X86::BI__builtin_ia32_psllqi128: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | case X86::BI__builtin_ia32_psllwi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 662 | case X86::BI__builtin_ia32_psradi128: |
| 663 | case X86::BI__builtin_ia32_psrawi128: |
| 664 | case X86::BI__builtin_ia32_psrldi128: |
| 665 | case X86::BI__builtin_ia32_psrlqi128: |
| 666 | case X86::BI__builtin_ia32_psrlwi128: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 667 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 668 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 2); |
| 669 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 670 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 671 | Ops[1], Zero, "insert"); |
| 672 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 673 | const char *name = 0; |
| 674 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 676 | switch (BuiltinID) { |
| 677 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 678 | case X86::BI__builtin_ia32_pslldi128: |
| 679 | name = "pslldi"; |
| 680 | ID = Intrinsic::x86_sse2_psll_d; |
| 681 | break; |
| 682 | case X86::BI__builtin_ia32_psllqi128: |
| 683 | name = "psllqi"; |
| 684 | ID = Intrinsic::x86_sse2_psll_q; |
| 685 | break; |
| 686 | case X86::BI__builtin_ia32_psllwi128: |
| 687 | name = "psllwi"; |
| 688 | ID = Intrinsic::x86_sse2_psll_w; |
| 689 | break; |
| 690 | case X86::BI__builtin_ia32_psradi128: |
| 691 | name = "psradi"; |
| 692 | ID = Intrinsic::x86_sse2_psra_d; |
| 693 | break; |
| 694 | case X86::BI__builtin_ia32_psrawi128: |
| 695 | name = "psrawi"; |
| 696 | ID = Intrinsic::x86_sse2_psra_w; |
| 697 | break; |
| 698 | case X86::BI__builtin_ia32_psrldi128: |
| 699 | name = "psrldi"; |
| 700 | ID = Intrinsic::x86_sse2_psrl_d; |
| 701 | break; |
| 702 | case X86::BI__builtin_ia32_psrlqi128: |
| 703 | name = "psrlqi"; |
| 704 | ID = Intrinsic::x86_sse2_psrl_q; |
| 705 | break; |
| 706 | case X86::BI__builtin_ia32_psrlwi128: |
| 707 | name = "psrlwi"; |
| 708 | ID = Intrinsic::x86_sse2_psrl_w; |
| 709 | break; |
| 710 | } |
| 711 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 713 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | case X86::BI__builtin_ia32_pslldi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 715 | case X86::BI__builtin_ia32_psllqi: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | case X86::BI__builtin_ia32_psllwi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 717 | case X86::BI__builtin_ia32_psradi: |
| 718 | case X86::BI__builtin_ia32_psrawi: |
| 719 | case X86::BI__builtin_ia32_psrldi: |
| 720 | case X86::BI__builtin_ia32_psrlqi: |
| 721 | case X86::BI__builtin_ia32_psrlwi: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 722 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 723 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 1); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 724 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 725 | const char *name = 0; |
| 726 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 728 | switch (BuiltinID) { |
| 729 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 730 | case X86::BI__builtin_ia32_pslldi: |
| 731 | name = "pslldi"; |
| 732 | ID = Intrinsic::x86_mmx_psll_d; |
| 733 | break; |
| 734 | case X86::BI__builtin_ia32_psllqi: |
| 735 | name = "psllqi"; |
| 736 | ID = Intrinsic::x86_mmx_psll_q; |
| 737 | break; |
| 738 | case X86::BI__builtin_ia32_psllwi: |
| 739 | name = "psllwi"; |
| 740 | ID = Intrinsic::x86_mmx_psll_w; |
| 741 | break; |
| 742 | case X86::BI__builtin_ia32_psradi: |
| 743 | name = "psradi"; |
| 744 | ID = Intrinsic::x86_mmx_psra_d; |
| 745 | break; |
| 746 | case X86::BI__builtin_ia32_psrawi: |
| 747 | name = "psrawi"; |
| 748 | ID = Intrinsic::x86_mmx_psra_w; |
| 749 | break; |
| 750 | case X86::BI__builtin_ia32_psrldi: |
| 751 | name = "psrldi"; |
| 752 | ID = Intrinsic::x86_mmx_psrl_d; |
| 753 | break; |
| 754 | case X86::BI__builtin_ia32_psrlqi: |
| 755 | name = "psrlqi"; |
| 756 | ID = Intrinsic::x86_mmx_psrl_q; |
| 757 | break; |
| 758 | case X86::BI__builtin_ia32_psrlwi: |
| 759 | name = "psrlwi"; |
| 760 | ID = Intrinsic::x86_mmx_psrl_w; |
| 761 | break; |
| 762 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 763 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 765 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 766 | case X86::BI__builtin_ia32_cmpps: { |
| 767 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 768 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 769 | } |
| 770 | case X86::BI__builtin_ia32_cmpss: { |
| 771 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 772 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 773 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 774 | case X86::BI__builtin_ia32_ldmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 775 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 776 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 777 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 778 | Builder.CreateStore(Ops[0], Tmp); |
| 779 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 780 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 781 | } |
| 782 | case X86::BI__builtin_ia32_stmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 783 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 784 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 785 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 786 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 787 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 788 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 789 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 790 | case X86::BI__builtin_ia32_cmppd: { |
| 791 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 792 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 793 | } |
| 794 | case X86::BI__builtin_ia32_cmpsd: { |
| 795 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 796 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 797 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 798 | case X86::BI__builtin_ia32_storehps: |
| 799 | case X86::BI__builtin_ia32_storelps: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 800 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 801 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 802 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 803 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 804 | // cast val v2i64 |
| 805 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 807 | // extract (0, 1) |
| 808 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 809 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Index); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 810 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 811 | |
| 812 | // cast pointer to i64 & store |
| 813 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 814 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 815 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 820 | const CallExpr *E) { |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 821 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 822 | default: return 0; |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 823 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | } |