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