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