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