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