| // Copyright 2014 the V8 project authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "test/unittests/compiler/instruction-selector-unittest.h" |
| |
| #include "src/compiler/node-matchers.h" |
| |
| namespace v8 { |
| namespace internal { |
| namespace compiler { |
| |
| // ----------------------------------------------------------------------------- |
| // Conversions. |
| |
| |
| TEST_F(InstructionSelectorTest, ChangeFloat32ToFloat64WithParameter) { |
| StreamBuilder m(this, kMachFloat32, kMachFloat64); |
| m.Return(m.ChangeFloat32ToFloat64(m.Parameter(0))); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kSSECvtss2sd, s[0]->arch_opcode()); |
| EXPECT_EQ(1U, s[0]->InputCount()); |
| EXPECT_EQ(1U, s[0]->OutputCount()); |
| } |
| |
| |
| TEST_F(InstructionSelectorTest, ChangeInt32ToInt64WithParameter) { |
| StreamBuilder m(this, kMachInt64, kMachInt32); |
| m.Return(m.ChangeInt32ToInt64(m.Parameter(0))); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kX64Movsxlq, s[0]->arch_opcode()); |
| } |
| |
| |
| TEST_F(InstructionSelectorTest, ChangeUint32ToFloat64WithParameter) { |
| StreamBuilder m(this, kMachFloat64, kMachUint32); |
| m.Return(m.ChangeUint32ToFloat64(m.Parameter(0))); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kSSEUint32ToFloat64, s[0]->arch_opcode()); |
| } |
| |
| |
| TEST_F(InstructionSelectorTest, ChangeUint32ToUint64WithParameter) { |
| StreamBuilder m(this, kMachUint64, kMachUint32); |
| m.Return(m.ChangeUint32ToUint64(m.Parameter(0))); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kX64Movl, s[0]->arch_opcode()); |
| } |
| |
| |
| TEST_F(InstructionSelectorTest, TruncateFloat64ToFloat32WithParameter) { |
| StreamBuilder m(this, kMachFloat64, kMachFloat32); |
| m.Return(m.TruncateFloat64ToFloat32(m.Parameter(0))); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kSSECvtsd2ss, s[0]->arch_opcode()); |
| EXPECT_EQ(1U, s[0]->InputCount()); |
| EXPECT_EQ(1U, s[0]->OutputCount()); |
| } |
| |
| |
| TEST_F(InstructionSelectorTest, TruncateInt64ToInt32WithParameter) { |
| StreamBuilder m(this, kMachInt32, kMachInt64); |
| m.Return(m.TruncateInt64ToInt32(m.Parameter(0))); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kX64Movl, s[0]->arch_opcode()); |
| } |
| |
| |
| // ----------------------------------------------------------------------------- |
| // Loads and stores |
| |
| namespace { |
| |
| struct MemoryAccess { |
| MachineType type; |
| ArchOpcode load_opcode; |
| ArchOpcode store_opcode; |
| }; |
| |
| |
| std::ostream& operator<<(std::ostream& os, const MemoryAccess& memacc) { |
| return os << memacc.type; |
| } |
| |
| |
| static const MemoryAccess kMemoryAccesses[] = { |
| {kMachInt8, kX64Movsxbl, kX64Movb}, |
| {kMachUint8, kX64Movzxbl, kX64Movb}, |
| {kMachInt16, kX64Movsxwl, kX64Movw}, |
| {kMachUint16, kX64Movzxwl, kX64Movw}, |
| {kMachInt32, kX64Movl, kX64Movl}, |
| {kMachUint32, kX64Movl, kX64Movl}, |
| {kMachInt64, kX64Movq, kX64Movq}, |
| {kMachUint64, kX64Movq, kX64Movq}, |
| {kMachFloat32, kX64Movss, kX64Movss}, |
| {kMachFloat64, kX64Movsd, kX64Movsd}}; |
| |
| } // namespace |
| |
| |
| typedef InstructionSelectorTestWithParam<MemoryAccess> |
| InstructionSelectorMemoryAccessTest; |
| |
| |
| TEST_P(InstructionSelectorMemoryAccessTest, LoadWithParameters) { |
| const MemoryAccess memacc = GetParam(); |
| StreamBuilder m(this, memacc.type, kMachPtr, kMachInt32); |
| m.Return(m.Load(memacc.type, m.Parameter(0), m.Parameter(1))); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(memacc.load_opcode, s[0]->arch_opcode()); |
| EXPECT_EQ(2U, s[0]->InputCount()); |
| EXPECT_EQ(1U, s[0]->OutputCount()); |
| } |
| |
| |
| TEST_P(InstructionSelectorMemoryAccessTest, StoreWithParameters) { |
| const MemoryAccess memacc = GetParam(); |
| StreamBuilder m(this, kMachInt32, kMachPtr, kMachInt32, memacc.type); |
| m.Store(memacc.type, m.Parameter(0), m.Parameter(1), m.Parameter(2)); |
| m.Return(m.Int32Constant(0)); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(memacc.store_opcode, s[0]->arch_opcode()); |
| EXPECT_EQ(3U, s[0]->InputCount()); |
| EXPECT_EQ(0U, s[0]->OutputCount()); |
| } |
| |
| |
| INSTANTIATE_TEST_CASE_P(InstructionSelectorTest, |
| InstructionSelectorMemoryAccessTest, |
| ::testing::ValuesIn(kMemoryAccesses)); |
| |
| // ----------------------------------------------------------------------------- |
| // ChangeUint32ToUint64. |
| |
| |
| namespace { |
| |
| typedef Node* (RawMachineAssembler::*Constructor)(Node*, Node*); |
| |
| |
| struct BinaryOperation { |
| Constructor constructor; |
| const char* constructor_name; |
| }; |
| |
| |
| std::ostream& operator<<(std::ostream& os, const BinaryOperation& bop) { |
| return os << bop.constructor_name; |
| } |
| |
| |
| const BinaryOperation kWord32BinaryOperations[] = { |
| {&RawMachineAssembler::Word32And, "Word32And"}, |
| {&RawMachineAssembler::Word32Or, "Word32Or"}, |
| {&RawMachineAssembler::Word32Xor, "Word32Xor"}, |
| {&RawMachineAssembler::Word32Shl, "Word32Shl"}, |
| {&RawMachineAssembler::Word32Shr, "Word32Shr"}, |
| {&RawMachineAssembler::Word32Sar, "Word32Sar"}, |
| {&RawMachineAssembler::Word32Ror, "Word32Ror"}, |
| {&RawMachineAssembler::Word32Equal, "Word32Equal"}, |
| {&RawMachineAssembler::Int32Add, "Int32Add"}, |
| {&RawMachineAssembler::Int32Sub, "Int32Sub"}, |
| {&RawMachineAssembler::Int32Mul, "Int32Mul"}, |
| {&RawMachineAssembler::Int32MulHigh, "Int32MulHigh"}, |
| {&RawMachineAssembler::Int32Div, "Int32Div"}, |
| {&RawMachineAssembler::Int32LessThan, "Int32LessThan"}, |
| {&RawMachineAssembler::Int32LessThanOrEqual, "Int32LessThanOrEqual"}, |
| {&RawMachineAssembler::Int32Mod, "Int32Mod"}, |
| {&RawMachineAssembler::Uint32Div, "Uint32Div"}, |
| {&RawMachineAssembler::Uint32LessThan, "Uint32LessThan"}, |
| {&RawMachineAssembler::Uint32LessThanOrEqual, "Uint32LessThanOrEqual"}, |
| {&RawMachineAssembler::Uint32Mod, "Uint32Mod"}}; |
| |
| } // namespace |
| |
| |
| typedef InstructionSelectorTestWithParam<BinaryOperation> |
| InstructionSelectorChangeUint32ToUint64Test; |
| |
| |
| TEST_P(InstructionSelectorChangeUint32ToUint64Test, ChangeUint32ToUint64) { |
| const BinaryOperation& bop = GetParam(); |
| StreamBuilder m(this, kMachUint64, kMachInt32, kMachInt32); |
| Node* const p0 = m.Parameter(0); |
| Node* const p1 = m.Parameter(1); |
| m.Return(m.ChangeUint32ToUint64((m.*bop.constructor)(p0, p1))); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| } |
| |
| |
| INSTANTIATE_TEST_CASE_P(InstructionSelectorTest, |
| InstructionSelectorChangeUint32ToUint64Test, |
| ::testing::ValuesIn(kWord32BinaryOperations)); |
| |
| |
| // ----------------------------------------------------------------------------- |
| // TruncateInt64ToInt32. |
| |
| |
| TEST_F(InstructionSelectorTest, TruncateInt64ToInt32WithWord64Sar) { |
| StreamBuilder m(this, kMachInt32, kMachInt64); |
| Node* const p = m.Parameter(0); |
| Node* const t = m.TruncateInt64ToInt32(m.Word64Sar(p, m.Int64Constant(32))); |
| m.Return(t); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kX64Shr, s[0]->arch_opcode()); |
| ASSERT_EQ(2U, s[0]->InputCount()); |
| EXPECT_EQ(s.ToVreg(p), s.ToVreg(s[0]->InputAt(0))); |
| EXPECT_EQ(32, s.ToInt32(s[0]->InputAt(1))); |
| ASSERT_EQ(1U, s[0]->OutputCount()); |
| EXPECT_TRUE(s.IsSameAsFirst(s[0]->OutputAt(0))); |
| EXPECT_EQ(s.ToVreg(t), s.ToVreg(s[0]->OutputAt(0))); |
| } |
| |
| |
| TEST_F(InstructionSelectorTest, TruncateInt64ToInt32WithWord64Shr) { |
| StreamBuilder m(this, kMachInt32, kMachInt64); |
| Node* const p = m.Parameter(0); |
| Node* const t = m.TruncateInt64ToInt32(m.Word64Shr(p, m.Int64Constant(32))); |
| m.Return(t); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kX64Shr, s[0]->arch_opcode()); |
| ASSERT_EQ(2U, s[0]->InputCount()); |
| EXPECT_EQ(s.ToVreg(p), s.ToVreg(s[0]->InputAt(0))); |
| EXPECT_EQ(32, s.ToInt32(s[0]->InputAt(1))); |
| ASSERT_EQ(1U, s[0]->OutputCount()); |
| EXPECT_TRUE(s.IsSameAsFirst(s[0]->OutputAt(0))); |
| EXPECT_EQ(s.ToVreg(t), s.ToVreg(s[0]->OutputAt(0))); |
| } |
| |
| |
| // ----------------------------------------------------------------------------- |
| // Addition. |
| |
| |
| TEST_F(InstructionSelectorTest, Int32AddWithInt32AddWithParameters) { |
| StreamBuilder m(this, kMachInt32, kMachInt32, kMachInt32); |
| Node* const p0 = m.Parameter(0); |
| Node* const p1 = m.Parameter(1); |
| Node* const a0 = m.Int32Add(p0, p1); |
| m.Return(m.Int32Add(a0, p0)); |
| Stream s = m.Build(); |
| ASSERT_EQ(2U, s.size()); |
| EXPECT_EQ(kX64Add32, s[0]->arch_opcode()); |
| ASSERT_EQ(2U, s[0]->InputCount()); |
| EXPECT_EQ(s.ToVreg(p1), s.ToVreg(s[0]->InputAt(0))); |
| EXPECT_EQ(s.ToVreg(p0), s.ToVreg(s[0]->InputAt(1))); |
| } |
| |
| |
| // ----------------------------------------------------------------------------- |
| // Multiplication. |
| |
| |
| TEST_F(InstructionSelectorTest, Int32MulWithInt32MulWithParameters) { |
| StreamBuilder m(this, kMachInt32, kMachInt32, kMachInt32); |
| Node* const p0 = m.Parameter(0); |
| Node* const p1 = m.Parameter(1); |
| Node* const m0 = m.Int32Mul(p0, p1); |
| m.Return(m.Int32Mul(m0, p0)); |
| Stream s = m.Build(); |
| ASSERT_EQ(2U, s.size()); |
| EXPECT_EQ(kX64Imul32, s[0]->arch_opcode()); |
| ASSERT_EQ(2U, s[0]->InputCount()); |
| EXPECT_EQ(s.ToVreg(p1), s.ToVreg(s[0]->InputAt(0))); |
| EXPECT_EQ(s.ToVreg(p0), s.ToVreg(s[0]->InputAt(1))); |
| ASSERT_EQ(1U, s[0]->OutputCount()); |
| EXPECT_EQ(s.ToVreg(m0), s.ToVreg(s[0]->OutputAt(0))); |
| EXPECT_EQ(kX64Imul32, s[1]->arch_opcode()); |
| ASSERT_EQ(2U, s[1]->InputCount()); |
| EXPECT_EQ(s.ToVreg(p0), s.ToVreg(s[1]->InputAt(0))); |
| EXPECT_EQ(s.ToVreg(m0), s.ToVreg(s[1]->InputAt(1))); |
| } |
| |
| |
| TEST_F(InstructionSelectorTest, Int32MulHigh) { |
| StreamBuilder m(this, kMachInt32, kMachInt32, kMachInt32); |
| Node* const p0 = m.Parameter(0); |
| Node* const p1 = m.Parameter(1); |
| Node* const n = m.Int32MulHigh(p0, p1); |
| m.Return(n); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kX64ImulHigh32, s[0]->arch_opcode()); |
| ASSERT_EQ(2U, s[0]->InputCount()); |
| EXPECT_EQ(s.ToVreg(p0), s.ToVreg(s[0]->InputAt(0))); |
| EXPECT_TRUE(s.IsFixed(s[0]->InputAt(0), rax)); |
| EXPECT_EQ(s.ToVreg(p1), s.ToVreg(s[0]->InputAt(1))); |
| EXPECT_TRUE(!s.IsUsedAtStart(s[0]->InputAt(1))); |
| ASSERT_LE(1U, s[0]->OutputCount()); |
| EXPECT_EQ(s.ToVreg(n), s.ToVreg(s[0]->Output())); |
| EXPECT_TRUE(s.IsFixed(s[0]->OutputAt(0), rdx)); |
| } |
| |
| |
| TEST_F(InstructionSelectorTest, Uint32MulHigh) { |
| StreamBuilder m(this, kMachUint32, kMachUint32, kMachUint32); |
| Node* const p0 = m.Parameter(0); |
| Node* const p1 = m.Parameter(1); |
| Node* const n = m.Uint32MulHigh(p0, p1); |
| m.Return(n); |
| Stream s = m.Build(); |
| ASSERT_EQ(1U, s.size()); |
| EXPECT_EQ(kX64UmulHigh32, s[0]->arch_opcode()); |
| ASSERT_EQ(2U, s[0]->InputCount()); |
| EXPECT_EQ(s.ToVreg(p0), s.ToVreg(s[0]->InputAt(0))); |
| EXPECT_TRUE(s.IsFixed(s[0]->InputAt(0), rax)); |
| EXPECT_EQ(s.ToVreg(p1), s.ToVreg(s[0]->InputAt(1))); |
| EXPECT_TRUE(!s.IsUsedAtStart(s[0]->InputAt(1))); |
| ASSERT_LE(1U, s[0]->OutputCount()); |
| EXPECT_EQ(s.ToVreg(n), s.ToVreg(s[0]->Output())); |
| EXPECT_TRUE(s.IsFixed(s[0]->OutputAt(0), rdx)); |
| } |
| |
| } // namespace compiler |
| } // namespace internal |
| } // namespace v8 |