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 | b16d32f | 2009-10-26 23:39:48 +0000 | [diff] [blame] | 207 | // We pass this builtin onto the optimizer so that it can |
| 208 | // figure out the object size in more complex cases. |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 209 | const llvm::Type *ResType[] = { |
| 210 | ConvertType(E->getType()) |
| 211 | }; |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 212 | |
| 213 | // LLVM only supports 0 and 2, make sure that we pass along that |
| 214 | // as a boolean. |
| 215 | Value *Ty = EmitScalarExpr(E->getArg(1)); |
| 216 | ConstantInt *CI = dyn_cast<ConstantInt>(Ty); |
| 217 | assert(CI); |
| 218 | uint64_t val = CI->getZExtValue(); |
| 219 | CI = ConstantInt::get(llvm::Type::getInt1Ty(VMContext), (val & 0x2) >> 1); |
| 220 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 221 | Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType, 1); |
| 222 | return RValue::get(Builder.CreateCall2(F, |
| 223 | EmitScalarExpr(E->getArg(0)), |
Eric Christopher | fee667f | 2009-12-23 03:49:37 +0000 | [diff] [blame] | 224 | CI)); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 225 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 226 | case Builtin::BI__builtin_prefetch: { |
| 227 | Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); |
| 228 | // FIXME: Technically these constants should of type 'int', yes? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 230 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 232 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 3); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 233 | Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); |
| 234 | return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); |
Anders Carlsson | df4852a | 2007-12-02 21:58:10 +0000 | [diff] [blame] | 235 | } |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 236 | case Builtin::BI__builtin_trap: { |
| 237 | Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); |
| 238 | return RValue::get(Builder.CreateCall(F)); |
| 239 | } |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 240 | case Builtin::BI__builtin_unreachable: { |
Mike Stump | fba565d | 2009-12-16 03:07:12 +0000 | [diff] [blame] | 241 | if (CatchUndefined && HaveInsertPoint()) |
| 242 | EmitBranch(getTrapBB()); |
Chris Lattner | 21190d5 | 2009-09-21 03:09:59 +0000 | [diff] [blame] | 243 | Value *V = Builder.CreateUnreachable(); |
| 244 | Builder.ClearInsertionPoint(); |
| 245 | return RValue::get(V); |
| 246 | } |
| 247 | |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 248 | case Builtin::BI__builtin_powi: |
| 249 | case Builtin::BI__builtin_powif: |
| 250 | case Builtin::BI__builtin_powil: { |
| 251 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 252 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 253 | const llvm::Type *ArgType = Base->getType(); |
| 254 | Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); |
Daniel Dunbar | a933c3c | 2008-07-21 18:44:41 +0000 | [diff] [blame] | 255 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 256 | } |
| 257 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 258 | case Builtin::BI__builtin_isgreater: |
| 259 | case Builtin::BI__builtin_isgreaterequal: |
| 260 | case Builtin::BI__builtin_isless: |
| 261 | case Builtin::BI__builtin_islessequal: |
| 262 | case Builtin::BI__builtin_islessgreater: |
| 263 | case Builtin::BI__builtin_isunordered: { |
| 264 | // Ordered comparisons: we know the arguments to these are matching scalar |
| 265 | // floating point values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | Value *LHS = EmitScalarExpr(E->getArg(0)); |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 267 | Value *RHS = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 269 | switch (BuiltinID) { |
| 270 | default: assert(0 && "Unknown ordered comparison"); |
| 271 | case Builtin::BI__builtin_isgreater: |
| 272 | LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); |
| 273 | break; |
| 274 | case Builtin::BI__builtin_isgreaterequal: |
| 275 | LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); |
| 276 | break; |
| 277 | case Builtin::BI__builtin_isless: |
| 278 | LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); |
| 279 | break; |
| 280 | case Builtin::BI__builtin_islessequal: |
| 281 | LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); |
| 282 | break; |
| 283 | case Builtin::BI__builtin_islessgreater: |
| 284 | LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); |
| 285 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | case Builtin::BI__builtin_isunordered: |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 287 | LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); |
| 288 | break; |
| 289 | } |
| 290 | // ZExt bool to int type. |
| 291 | return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), |
| 292 | "tmp")); |
| 293 | } |
Eli Friedman | d613989 | 2009-09-01 04:19:44 +0000 | [diff] [blame] | 294 | case Builtin::BI__builtin_isnan: { |
| 295 | Value *V = EmitScalarExpr(E->getArg(0)); |
| 296 | V = Builder.CreateFCmpUNO(V, V, "cmp"); |
| 297 | return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp")); |
| 298 | } |
Eli Friedman | b52fe9c | 2009-06-02 07:10:30 +0000 | [diff] [blame] | 299 | case Builtin::BIalloca: |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 300 | case Builtin::BI__builtin_alloca: { |
| 301 | // FIXME: LLVM IR Should allow alloca with an i64 size! |
| 302 | Value *Size = EmitScalarExpr(E->getArg(0)); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 303 | Size = Builder.CreateIntCast(Size, llvm::Type::getInt32Ty(VMContext), false, "tmp"); |
| 304 | return RValue::get(Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), Size, "tmp")); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 305 | } |
| 306 | case Builtin::BI__builtin_bzero: { |
| 307 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 308 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 309 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0), |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 310 | EmitScalarExpr(E->getArg(1)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 311 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 312 | return RValue::get(Address); |
Chris Lattner | 9e800e3 | 2008-06-16 17:15:14 +0000 | [diff] [blame] | 313 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 314 | case Builtin::BImemcpy: |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 315 | case Builtin::BI__builtin_memcpy: { |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 316 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 317 | Builder.CreateCall4(CGM.getMemCpyFn(), Address, |
| 318 | EmitScalarExpr(E->getArg(1)), |
| 319 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 320 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 321 | return RValue::get(Address); |
| 322 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 323 | case Builtin::BImemmove: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 324 | case Builtin::BI__builtin_memmove: { |
| 325 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 326 | Builder.CreateCall4(CGM.getMemMoveFn(), Address, |
| 327 | EmitScalarExpr(E->getArg(1)), |
| 328 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 329 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 330 | return RValue::get(Address); |
| 331 | } |
Eli Friedman | e6ec205 | 2009-12-17 00:14:28 +0000 | [diff] [blame] | 332 | case Builtin::BImemset: |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 333 | case Builtin::BI__builtin_memset: { |
| 334 | Value *Address = EmitScalarExpr(E->getArg(0)); |
| 335 | Builder.CreateCall4(CGM.getMemSetFn(), Address, |
Daniel Dunbar | 62c29c6 | 2009-02-16 21:52:05 +0000 | [diff] [blame] | 336 | Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 337 | llvm::Type::getInt8Ty(VMContext)), |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 338 | EmitScalarExpr(E->getArg(2)), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 339 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)); |
Daniel Dunbar | 1caae95 | 2008-07-22 00:26:45 +0000 | [diff] [blame] | 340 | return RValue::get(Address); |
Eli Friedman | d4b32e4 | 2008-05-19 23:27:48 +0000 | [diff] [blame] | 341 | } |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 342 | case Builtin::BI__builtin_return_address: { |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame^] | 343 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 344 | Depth = Builder.CreateIntCast(Depth, |
| 345 | llvm::Type::getInt32Ty(VMContext), |
| 346 | false, "tmp"); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 347 | Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame^] | 348 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 349 | } |
| 350 | case Builtin::BI__builtin_frame_address: { |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame^] | 351 | Value *Depth = EmitScalarExpr(E->getArg(0)); |
| 352 | Depth = Builder.CreateIntCast(Depth, |
| 353 | llvm::Type::getInt32Ty(VMContext), |
| 354 | false, "tmp"); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 355 | Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
Anton Korobeynikov | 83c2a98 | 2009-12-27 14:27:22 +0000 | [diff] [blame^] | 356 | return RValue::get(Builder.CreateCall(F, Depth)); |
Eli Friedman | 256f77e | 2008-05-20 08:59:34 +0000 | [diff] [blame] | 357 | } |
Eli Friedman | 3b660ef | 2009-05-03 19:23:23 +0000 | [diff] [blame] | 358 | case Builtin::BI__builtin_extract_return_addr: { |
| 359 | // FIXME: There should be a target hook for this |
| 360 | return RValue::get(EmitScalarExpr(E->getArg(0))); |
| 361 | } |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 362 | case Builtin::BI__builtin_unwind_init: { |
| 363 | Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0); |
| 364 | return RValue::get(Builder.CreateCall(F)); |
| 365 | } |
| 366 | #if 0 |
| 367 | // FIXME: Finish/enable when LLVM backend support stabilizes |
| 368 | case Builtin::BI__builtin_setjmp: { |
| 369 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
| 370 | // Store the frame pointer to the buffer |
| 371 | Value *FrameAddrF = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); |
| 372 | Value *FrameAddr = |
| 373 | Builder.CreateCall(FrameAddrF, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 374 | Constant::getNullValue(llvm::Type::getInt32Ty(VMContext))); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 375 | Builder.CreateStore(FrameAddr, Buf); |
| 376 | // Call the setjmp intrinsic |
| 377 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp, 0, 0); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 378 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 379 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 380 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 381 | } |
| 382 | case Builtin::BI__builtin_longjmp: { |
| 383 | Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp, 0, 0); |
| 384 | Value *Buf = EmitScalarExpr(E->getArg(0)); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 385 | const llvm::Type *DestType = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | a6d75c0 | 2009-06-02 09:37:50 +0000 | [diff] [blame] | 386 | Buf = Builder.CreateBitCast(Buf, DestType); |
| 387 | return RValue::get(Builder.CreateCall(F, Buf)); |
| 388 | } |
| 389 | #endif |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 390 | case Builtin::BI__sync_fetch_and_add: |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 391 | case Builtin::BI__sync_fetch_and_sub: |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 392 | case Builtin::BI__sync_fetch_and_or: |
| 393 | case Builtin::BI__sync_fetch_and_and: |
| 394 | case Builtin::BI__sync_fetch_and_xor: |
| 395 | case Builtin::BI__sync_add_and_fetch: |
| 396 | case Builtin::BI__sync_sub_and_fetch: |
| 397 | case Builtin::BI__sync_and_and_fetch: |
| 398 | case Builtin::BI__sync_or_and_fetch: |
| 399 | case Builtin::BI__sync_xor_and_fetch: |
| 400 | case Builtin::BI__sync_val_compare_and_swap: |
| 401 | case Builtin::BI__sync_bool_compare_and_swap: |
| 402 | case Builtin::BI__sync_lock_test_and_set: |
| 403 | case Builtin::BI__sync_lock_release: |
| 404 | assert(0 && "Shouldn't make it through sema"); |
| 405 | case Builtin::BI__sync_fetch_and_add_1: |
| 406 | case Builtin::BI__sync_fetch_and_add_2: |
| 407 | case Builtin::BI__sync_fetch_and_add_4: |
| 408 | case Builtin::BI__sync_fetch_and_add_8: |
| 409 | case Builtin::BI__sync_fetch_and_add_16: |
| 410 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); |
| 411 | case Builtin::BI__sync_fetch_and_sub_1: |
| 412 | case Builtin::BI__sync_fetch_and_sub_2: |
| 413 | case Builtin::BI__sync_fetch_and_sub_4: |
| 414 | case Builtin::BI__sync_fetch_and_sub_8: |
| 415 | case Builtin::BI__sync_fetch_and_sub_16: |
Mon P Wang | 09b6bf5 | 2008-06-25 08:21:36 +0000 | [diff] [blame] | 416 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 417 | case Builtin::BI__sync_fetch_and_or_1: |
| 418 | case Builtin::BI__sync_fetch_and_or_2: |
| 419 | case Builtin::BI__sync_fetch_and_or_4: |
| 420 | case Builtin::BI__sync_fetch_and_or_8: |
| 421 | case Builtin::BI__sync_fetch_and_or_16: |
| 422 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); |
| 423 | case Builtin::BI__sync_fetch_and_and_1: |
| 424 | case Builtin::BI__sync_fetch_and_and_2: |
| 425 | case Builtin::BI__sync_fetch_and_and_4: |
| 426 | case Builtin::BI__sync_fetch_and_and_8: |
| 427 | case Builtin::BI__sync_fetch_and_and_16: |
| 428 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); |
| 429 | case Builtin::BI__sync_fetch_and_xor_1: |
| 430 | case Builtin::BI__sync_fetch_and_xor_2: |
| 431 | case Builtin::BI__sync_fetch_and_xor_4: |
| 432 | case Builtin::BI__sync_fetch_and_xor_8: |
| 433 | case Builtin::BI__sync_fetch_and_xor_16: |
| 434 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 435 | case Builtin::BI__sync_fetch_and_nand_1: |
| 436 | case Builtin::BI__sync_fetch_and_nand_2: |
| 437 | case Builtin::BI__sync_fetch_and_nand_4: |
| 438 | case Builtin::BI__sync_fetch_and_nand_8: |
| 439 | case Builtin::BI__sync_fetch_and_nand_16: |
| 440 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 442 | // Clang extensions: not overloaded yet. |
Mon P Wang | 1ffe281 | 2008-05-09 22:40:52 +0000 | [diff] [blame] | 443 | case Builtin::BI__sync_fetch_and_min: |
| 444 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); |
| 445 | case Builtin::BI__sync_fetch_and_max: |
| 446 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); |
| 447 | case Builtin::BI__sync_fetch_and_umin: |
| 448 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); |
| 449 | case Builtin::BI__sync_fetch_and_umax: |
| 450 | return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 452 | case Builtin::BI__sync_add_and_fetch_1: |
| 453 | case Builtin::BI__sync_add_and_fetch_2: |
| 454 | case Builtin::BI__sync_add_and_fetch_4: |
| 455 | case Builtin::BI__sync_add_and_fetch_8: |
| 456 | case Builtin::BI__sync_add_and_fetch_16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 458 | llvm::Instruction::Add); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 459 | case Builtin::BI__sync_sub_and_fetch_1: |
| 460 | case Builtin::BI__sync_sub_and_fetch_2: |
| 461 | case Builtin::BI__sync_sub_and_fetch_4: |
| 462 | case Builtin::BI__sync_sub_and_fetch_8: |
| 463 | case Builtin::BI__sync_sub_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 464 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, |
| 465 | llvm::Instruction::Sub); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 466 | case Builtin::BI__sync_and_and_fetch_1: |
| 467 | case Builtin::BI__sync_and_and_fetch_2: |
| 468 | case Builtin::BI__sync_and_and_fetch_4: |
| 469 | case Builtin::BI__sync_and_and_fetch_8: |
| 470 | case Builtin::BI__sync_and_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 471 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, |
| 472 | llvm::Instruction::And); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 473 | case Builtin::BI__sync_or_and_fetch_1: |
| 474 | case Builtin::BI__sync_or_and_fetch_2: |
| 475 | case Builtin::BI__sync_or_and_fetch_4: |
| 476 | case Builtin::BI__sync_or_and_fetch_8: |
| 477 | case Builtin::BI__sync_or_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 478 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, |
| 479 | llvm::Instruction::Or); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 480 | case Builtin::BI__sync_xor_and_fetch_1: |
| 481 | case Builtin::BI__sync_xor_and_fetch_2: |
| 482 | case Builtin::BI__sync_xor_and_fetch_4: |
| 483 | case Builtin::BI__sync_xor_and_fetch_8: |
| 484 | case Builtin::BI__sync_xor_and_fetch_16: |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 485 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, |
| 486 | llvm::Instruction::Xor); |
Chris Lattner | eebd9d2 | 2009-05-13 04:37:52 +0000 | [diff] [blame] | 487 | case Builtin::BI__sync_nand_and_fetch_1: |
| 488 | case Builtin::BI__sync_nand_and_fetch_2: |
| 489 | case Builtin::BI__sync_nand_and_fetch_4: |
| 490 | case Builtin::BI__sync_nand_and_fetch_8: |
| 491 | case Builtin::BI__sync_nand_and_fetch_16: |
| 492 | return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E, |
| 493 | llvm::Instruction::And); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 495 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 496 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 497 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 498 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 499 | case Builtin::BI__sync_val_compare_and_swap_16: |
| 500 | { |
Mon P Wang | c500451 | 2008-07-31 03:28:23 +0000 | [diff] [blame] | 501 | const llvm::Type *ResType[2]; |
| 502 | ResType[0]= ConvertType(E->getType()); |
| 503 | ResType[1] = ConvertType(E->getArg(0)->getType()); |
| 504 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | return RValue::get(Builder.CreateCall3(AtomF, |
Anders Carlsson | 9584446 | 2008-08-30 16:17:45 +0000 | [diff] [blame] | 506 | EmitScalarExpr(E->getArg(0)), |
| 507 | EmitScalarExpr(E->getArg(1)), |
| 508 | EmitScalarExpr(E->getArg(2)))); |
Anders Carlsson | 89799cf | 2007-10-29 02:59:40 +0000 | [diff] [blame] | 509 | } |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 511 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 512 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 513 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 514 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 515 | case Builtin::BI__sync_bool_compare_and_swap_16: |
| 516 | { |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 517 | const llvm::Type *ResType[2]; |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 518 | ResType[0]= ConvertType(E->getArg(1)->getType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 519 | ResType[1] = llvm::PointerType::getUnqual(ResType[0]); |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 520 | Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); |
| 521 | Value *OldVal = EmitScalarExpr(E->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | Value *PrevVal = Builder.CreateCall3(AtomF, |
Daniel Dunbar | 0002d23 | 2009-04-07 00:55:51 +0000 | [diff] [blame] | 523 | EmitScalarExpr(E->getArg(0)), |
| 524 | OldVal, |
| 525 | EmitScalarExpr(E->getArg(2))); |
| 526 | Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); |
| 527 | // zext bool to int. |
| 528 | return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); |
| 529 | } |
| 530 | |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 531 | case Builtin::BI__sync_lock_test_and_set_1: |
| 532 | case Builtin::BI__sync_lock_test_and_set_2: |
| 533 | case Builtin::BI__sync_lock_test_and_set_4: |
| 534 | case Builtin::BI__sync_lock_test_and_set_8: |
| 535 | case Builtin::BI__sync_lock_test_and_set_16: |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 536 | return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); |
Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 537 | case Builtin::BI__sync_lock_release_1: |
| 538 | case Builtin::BI__sync_lock_release_2: |
| 539 | case Builtin::BI__sync_lock_release_4: |
| 540 | case Builtin::BI__sync_lock_release_8: |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 541 | case Builtin::BI__sync_lock_release_16: { |
| 542 | Value *Ptr = EmitScalarExpr(E->getArg(0)); |
| 543 | const llvm::Type *ElTy = |
| 544 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Daniel Dunbar | 007b567 | 2009-11-29 21:11:47 +0000 | [diff] [blame] | 545 | llvm::StoreInst *Store = |
| 546 | Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr); |
| 547 | Store->setVolatile(true); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 548 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 549 | } |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 550 | |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 551 | case Builtin::BI__sync_synchronize: { |
| 552 | Value *C[5]; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 553 | C[0] = C[1] = C[2] = C[3] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 1); |
| 554 | C[4] = llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 555 | Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); |
Daniel Dunbar | eb4f81e | 2009-05-27 23:45:33 +0000 | [diff] [blame] | 556 | return RValue::get(0); |
Chris Lattner | f58cd9b | 2009-05-13 04:46:13 +0000 | [diff] [blame] | 557 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 559 | // Library functions with special handling. |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 560 | case Builtin::BIsqrt: |
| 561 | case Builtin::BIsqrtf: |
| 562 | case Builtin::BIsqrtl: { |
| 563 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 564 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 565 | break; |
| 566 | Value *Arg0 = EmitScalarExpr(E->getArg(0)); |
| 567 | const llvm::Type *ArgType = Arg0->getType(); |
| 568 | Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1); |
| 569 | return RValue::get(Builder.CreateCall(F, Arg0, "tmp")); |
| 570 | } |
| 571 | |
| 572 | case Builtin::BIpow: |
| 573 | case Builtin::BIpowf: |
| 574 | case Builtin::BIpowl: { |
| 575 | // Rewrite sqrt to intrinsic if allowed. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 576 | if (!FD->hasAttr<ConstAttr>()) |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 577 | break; |
| 578 | Value *Base = EmitScalarExpr(E->getArg(0)); |
| 579 | Value *Exponent = EmitScalarExpr(E->getArg(1)); |
| 580 | const llvm::Type *ArgType = Base->getType(); |
| 581 | Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); |
| 582 | return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); |
| 583 | } |
Nate Begeman | 7ea2e3f | 2008-05-15 07:38:03 +0000 | [diff] [blame] | 584 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 586 | // If this is an alias for a libm function (e.g. __builtin_sin) turn it into |
| 587 | // that function. |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 588 | if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || |
| 589 | getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 590 | return EmitCall(E->getCallee()->getType(), |
| 591 | CGM.getBuiltinLibFunction(FD, BuiltinID), |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 592 | ReturnValueSlot(), |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 593 | E->arg_begin(), E->arg_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 595 | // See if we have a target specific intrinsic. |
Dale Johannesen | a6f80ef | 2009-02-05 01:50:47 +0000 | [diff] [blame] | 596 | const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 597 | Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic; |
| 598 | if (const char *Prefix = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | llvm::Triple::getArchTypePrefix(Target.getTriple().getArch())) |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 600 | IntrinsicID = Intrinsic::getIntrinsicForGCCBuiltin(Prefix, Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 602 | if (IntrinsicID != Intrinsic::not_intrinsic) { |
| 603 | SmallVector<Value*, 16> Args; |
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 | Function *F = CGM.getIntrinsic(IntrinsicID); |
| 606 | const llvm::FunctionType *FTy = F->getFunctionType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 608 | for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { |
| 609 | Value *ArgValue = EmitScalarExpr(E->getArg(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 611 | // If the intrinsic arg type is different from the builtin arg type |
| 612 | // we need to do a bit cast. |
| 613 | const llvm::Type *PTy = FTy->getParamType(i); |
| 614 | if (PTy != ArgValue->getType()) { |
| 615 | assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && |
| 616 | "Must be able to losslessly bit cast to param"); |
| 617 | ArgValue = Builder.CreateBitCast(ArgValue, PTy); |
| 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 | Args.push_back(ArgValue); |
| 621 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 623 | Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 624 | QualType BuiltinRetType = E->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 625 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 626 | const llvm::Type *RetTy = llvm::Type::getVoidTy(VMContext); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 627 | if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); |
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 | if (RetTy != V->getType()) { |
| 630 | assert(V->getType()->canLosslesslyBitCastTo(RetTy) && |
| 631 | "Must be able to losslessly bit cast result type"); |
| 632 | V = Builder.CreateBitCast(V, RetTy); |
| 633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 635 | return RValue::get(V); |
| 636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 638 | // 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] | 639 | if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 640 | return RValue::get(V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 642 | ErrorUnsupported(E, "builtin function"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 644 | // Unknown builtin, for now just dump it out and return undef. |
| 645 | if (hasAggregateLLVMType(E->getType())) |
| 646 | return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType()))); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 647 | return RValue::get(llvm::UndefValue::get(ConvertType(E->getType()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 649 | |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 650 | Value *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, |
| 651 | const CallExpr *E) { |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 652 | switch (Target.getTriple().getArch()) { |
| 653 | case llvm::Triple::x86: |
| 654 | case llvm::Triple::x86_64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 655 | return EmitX86BuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 656 | case llvm::Triple::ppc: |
| 657 | case llvm::Triple::ppc64: |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 658 | return EmitPPCBuiltinExpr(BuiltinID, E); |
Daniel Dunbar | 55cc2ed | 2009-08-24 09:54:37 +0000 | [diff] [blame] | 659 | default: |
| 660 | return 0; |
| 661 | } |
Daniel Dunbar | f02e9dd | 2008-10-10 00:24:54 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 665 | const CallExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 667 | llvm::SmallVector<Value*, 4> Ops; |
| 668 | |
| 669 | for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) |
| 670 | Ops.push_back(EmitScalarExpr(E->getArg(i))); |
| 671 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 672 | switch (BuiltinID) { |
Anders Carlsson | 46a26b0 | 2007-12-09 23:39:18 +0000 | [diff] [blame] | 673 | default: return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | case X86::BI__builtin_ia32_pslldi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 675 | case X86::BI__builtin_ia32_psllqi128: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | case X86::BI__builtin_ia32_psllwi128: |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 677 | case X86::BI__builtin_ia32_psradi128: |
| 678 | case X86::BI__builtin_ia32_psrawi128: |
| 679 | case X86::BI__builtin_ia32_psrldi128: |
| 680 | case X86::BI__builtin_ia32_psrlqi128: |
| 681 | case X86::BI__builtin_ia32_psrlwi128: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 682 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 683 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 2); |
| 684 | llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 685 | Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 686 | Ops[1], Zero, "insert"); |
| 687 | Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); |
| 688 | const char *name = 0; |
| 689 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 691 | switch (BuiltinID) { |
| 692 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 693 | case X86::BI__builtin_ia32_pslldi128: |
| 694 | name = "pslldi"; |
| 695 | ID = Intrinsic::x86_sse2_psll_d; |
| 696 | break; |
| 697 | case X86::BI__builtin_ia32_psllqi128: |
| 698 | name = "psllqi"; |
| 699 | ID = Intrinsic::x86_sse2_psll_q; |
| 700 | break; |
| 701 | case X86::BI__builtin_ia32_psllwi128: |
| 702 | name = "psllwi"; |
| 703 | ID = Intrinsic::x86_sse2_psll_w; |
| 704 | break; |
| 705 | case X86::BI__builtin_ia32_psradi128: |
| 706 | name = "psradi"; |
| 707 | ID = Intrinsic::x86_sse2_psra_d; |
| 708 | break; |
| 709 | case X86::BI__builtin_ia32_psrawi128: |
| 710 | name = "psrawi"; |
| 711 | ID = Intrinsic::x86_sse2_psra_w; |
| 712 | break; |
| 713 | case X86::BI__builtin_ia32_psrldi128: |
| 714 | name = "psrldi"; |
| 715 | ID = Intrinsic::x86_sse2_psrl_d; |
| 716 | break; |
| 717 | case X86::BI__builtin_ia32_psrlqi128: |
| 718 | name = "psrlqi"; |
| 719 | ID = Intrinsic::x86_sse2_psrl_q; |
| 720 | break; |
| 721 | case X86::BI__builtin_ia32_psrlwi128: |
| 722 | name = "psrlwi"; |
| 723 | ID = Intrinsic::x86_sse2_psrl_w; |
| 724 | break; |
| 725 | } |
| 726 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 728 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | case X86::BI__builtin_ia32_pslldi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 730 | case X86::BI__builtin_ia32_psllqi: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | case X86::BI__builtin_ia32_psllwi: |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 732 | case X86::BI__builtin_ia32_psradi: |
| 733 | case X86::BI__builtin_ia32_psrawi: |
| 734 | case X86::BI__builtin_ia32_psrldi: |
| 735 | case X86::BI__builtin_ia32_psrlqi: |
| 736 | case X86::BI__builtin_ia32_psrlwi: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 737 | Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::getInt64Ty(VMContext), "zext"); |
| 738 | const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(VMContext), 1); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 739 | Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 740 | const char *name = 0; |
| 741 | Intrinsic::ID ID = Intrinsic::not_intrinsic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 743 | switch (BuiltinID) { |
| 744 | default: assert(0 && "Unsupported shift intrinsic!"); |
| 745 | case X86::BI__builtin_ia32_pslldi: |
| 746 | name = "pslldi"; |
| 747 | ID = Intrinsic::x86_mmx_psll_d; |
| 748 | break; |
| 749 | case X86::BI__builtin_ia32_psllqi: |
| 750 | name = "psllqi"; |
| 751 | ID = Intrinsic::x86_mmx_psll_q; |
| 752 | break; |
| 753 | case X86::BI__builtin_ia32_psllwi: |
| 754 | name = "psllwi"; |
| 755 | ID = Intrinsic::x86_mmx_psll_w; |
| 756 | break; |
| 757 | case X86::BI__builtin_ia32_psradi: |
| 758 | name = "psradi"; |
| 759 | ID = Intrinsic::x86_mmx_psra_d; |
| 760 | break; |
| 761 | case X86::BI__builtin_ia32_psrawi: |
| 762 | name = "psrawi"; |
| 763 | ID = Intrinsic::x86_mmx_psra_w; |
| 764 | break; |
| 765 | case X86::BI__builtin_ia32_psrldi: |
| 766 | name = "psrldi"; |
| 767 | ID = Intrinsic::x86_mmx_psrl_d; |
| 768 | break; |
| 769 | case X86::BI__builtin_ia32_psrlqi: |
| 770 | name = "psrlqi"; |
| 771 | ID = Intrinsic::x86_mmx_psrl_q; |
| 772 | break; |
| 773 | case X86::BI__builtin_ia32_psrlwi: |
| 774 | name = "psrlwi"; |
| 775 | ID = Intrinsic::x86_mmx_psrl_w; |
| 776 | break; |
| 777 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 778 | llvm::Function *F = CGM.getIntrinsic(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); |
Anders Carlsson | 2929cfa | 2007-12-14 17:48:24 +0000 | [diff] [blame] | 780 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 781 | case X86::BI__builtin_ia32_cmpps: { |
| 782 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); |
| 783 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); |
| 784 | } |
| 785 | case X86::BI__builtin_ia32_cmpss: { |
| 786 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); |
| 787 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 788 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 789 | case X86::BI__builtin_ia32_ldmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 790 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 791 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 792 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 793 | Builder.CreateStore(Ops[0], Tmp); |
| 794 | return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 795 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 796 | } |
| 797 | case X86::BI__builtin_ia32_stmxcsr: { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 798 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 799 | Value *One = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1); |
| 800 | Value *Tmp = Builder.CreateAlloca(llvm::Type::getInt32Ty(VMContext), One, "tmp"); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 801 | One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 802 | Builder.CreateBitCast(Tmp, PtrTy)); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 803 | return Builder.CreateLoad(Tmp, "stmxcsr"); |
| 804 | } |
Anders Carlsson | 79dcf5f | 2009-05-18 19:16:46 +0000 | [diff] [blame] | 805 | case X86::BI__builtin_ia32_cmppd: { |
| 806 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); |
| 807 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); |
| 808 | } |
| 809 | case X86::BI__builtin_ia32_cmpsd: { |
| 810 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); |
| 811 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); |
Anders Carlsson | cc8b7f9 | 2007-12-16 22:33:50 +0000 | [diff] [blame] | 812 | } |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 813 | case X86::BI__builtin_ia32_storehps: |
| 814 | case X86::BI__builtin_ia32_storelps: { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 815 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 816 | llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); |
| 817 | llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 819 | // cast val v2i64 |
| 820 | Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 821 | |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 822 | // extract (0, 1) |
| 823 | unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 824 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Index); |
Nate Begeman | e772210 | 2008-04-14 04:49:57 +0000 | [diff] [blame] | 825 | Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); |
| 826 | |
| 827 | // cast pointer to i64 & store |
| 828 | Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); |
| 829 | return Builder.CreateStore(Ops[1], Ops[0]); |
| 830 | } |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 831 | case X86::BI__builtin_ia32_palignr: { |
Nate Begeman | c3420ff | 2009-12-14 05:15:02 +0000 | [diff] [blame] | 832 | Function *F = CGM.getIntrinsic(Intrinsic::x86_ssse3_palign_r); |
| 833 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size()); |
| 834 | } |
| 835 | case X86::BI__builtin_ia32_palignr128: { |
Nate Begeman | ce5818a | 2009-12-14 04:57:03 +0000 | [diff] [blame] | 836 | unsigned shiftVal = cast<llvm::ConstantInt>(Ops[2])->getZExtValue(); |
| 837 | |
| 838 | // If palignr is shifting the pair of input vectors less than 17 bytes, |
| 839 | // emit a shuffle instruction. |
| 840 | if (shiftVal <= 16) { |
| 841 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 842 | |
| 843 | llvm::SmallVector<llvm::Constant*, 16> Indices; |
| 844 | for (unsigned i = 0; i != 16; ++i) |
| 845 | Indices.push_back(llvm::ConstantInt::get(IntTy, shiftVal + i)); |
| 846 | |
| 847 | Value* SV = llvm::ConstantVector::get(Indices.begin(), Indices.size()); |
| 848 | return Builder.CreateShuffleVector(Ops[1], Ops[0], SV, "palignr"); |
| 849 | } |
| 850 | |
| 851 | // If palignr is shifting the pair of input vectors more than 16 but less |
| 852 | // than 32 bytes, emit a logical right shift of the destination. |
| 853 | if (shiftVal < 32) { |
| 854 | const llvm::Type *EltTy = llvm::Type::getInt64Ty(VMContext); |
| 855 | const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); |
| 856 | const llvm::Type *IntTy = llvm::Type::getInt32Ty(VMContext); |
| 857 | |
| 858 | Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); |
| 859 | Ops[1] = llvm::ConstantInt::get(IntTy, (shiftVal-16) * 8); |
| 860 | |
| 861 | // create i32 constant |
| 862 | llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_psrl_dq); |
| 863 | return Builder.CreateCall(F, &Ops[0], &Ops[0] + 2, "palignr"); |
| 864 | } |
| 865 | |
| 866 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 867 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Eric Christopher | 91b5927 | 2009-12-01 05:00:51 +0000 | [diff] [blame] | 868 | } |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, |
Chris Lattner | 1feedd8 | 2007-12-13 07:34:23 +0000 | [diff] [blame] | 873 | const CallExpr *E) { |
Daniel Dunbar | b0b8438 | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 874 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | } |