680793023f319c76b40be406768dbeb02152b288
[platform/upstream/nodejs.git] / deps / v8 / test / unittests / compiler / simplified-operator-unittest.cc
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/compiler/opcodes.h"
6 #include "src/compiler/operator.h"
7 #include "src/compiler/operator-properties.h"
8 #include "src/compiler/simplified-operator.h"
9 #include "src/types-inl.h"
10 #include "test/unittests/test-utils.h"
11
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15
16 // -----------------------------------------------------------------------------
17 // Pure operators.
18
19
20 namespace {
21
22 struct PureOperator {
23   const Operator* (SimplifiedOperatorBuilder::*constructor)();
24   IrOpcode::Value opcode;
25   Operator::Properties properties;
26   int value_input_count;
27 };
28
29
30 std::ostream& operator<<(std::ostream& os, const PureOperator& pop) {
31   return os << IrOpcode::Mnemonic(pop.opcode);
32 }
33
34
35 const PureOperator kPureOperators[] = {
36 #define PURE(Name, properties, input_count)              \
37   {                                                      \
38     &SimplifiedOperatorBuilder::Name, IrOpcode::k##Name, \
39         Operator::kPure | properties, input_count        \
40   }
41     PURE(AnyToBoolean, Operator::kNoProperties, 1),
42     PURE(BooleanNot, Operator::kNoProperties, 1),
43     PURE(BooleanToNumber, Operator::kNoProperties, 1),
44     PURE(NumberEqual, Operator::kCommutative, 2),
45     PURE(NumberLessThan, Operator::kNoProperties, 2),
46     PURE(NumberLessThanOrEqual, Operator::kNoProperties, 2),
47     PURE(NumberAdd, Operator::kCommutative, 2),
48     PURE(NumberSubtract, Operator::kNoProperties, 2),
49     PURE(NumberMultiply, Operator::kCommutative, 2),
50     PURE(NumberDivide, Operator::kNoProperties, 2),
51     PURE(NumberModulus, Operator::kNoProperties, 2),
52     PURE(NumberToInt32, Operator::kNoProperties, 1),
53     PURE(NumberToUint32, Operator::kNoProperties, 1),
54     PURE(PlainPrimitiveToNumber, Operator::kNoProperties, 1),
55     PURE(StringEqual, Operator::kCommutative, 2),
56     PURE(StringLessThan, Operator::kNoProperties, 2),
57     PURE(StringLessThanOrEqual, Operator::kNoProperties, 2),
58     PURE(StringAdd, Operator::kNoProperties, 2),
59     PURE(ChangeTaggedToInt32, Operator::kNoProperties, 1),
60     PURE(ChangeTaggedToUint32, Operator::kNoProperties, 1),
61     PURE(ChangeTaggedToFloat64, Operator::kNoProperties, 1),
62     PURE(ChangeInt32ToTagged, Operator::kNoProperties, 1),
63     PURE(ChangeUint32ToTagged, Operator::kNoProperties, 1),
64     PURE(ChangeFloat64ToTagged, Operator::kNoProperties, 1),
65     PURE(ChangeBoolToBit, Operator::kNoProperties, 1),
66     PURE(ChangeBitToBool, Operator::kNoProperties, 1),
67     PURE(ObjectIsSmi, Operator::kNoProperties, 1),
68     PURE(ObjectIsNonNegativeSmi, Operator::kNoProperties, 1)
69 #undef PURE
70 };
71
72 }  // namespace
73
74
75 class SimplifiedPureOperatorTest
76     : public TestWithZone,
77       public ::testing::WithParamInterface<PureOperator> {};
78
79
80 TEST_P(SimplifiedPureOperatorTest, InstancesAreGloballyShared) {
81   const PureOperator& pop = GetParam();
82   SimplifiedOperatorBuilder simplified1(zone());
83   SimplifiedOperatorBuilder simplified2(zone());
84   EXPECT_EQ((simplified1.*pop.constructor)(), (simplified2.*pop.constructor)());
85 }
86
87
88 TEST_P(SimplifiedPureOperatorTest, NumberOfInputsAndOutputs) {
89   SimplifiedOperatorBuilder simplified(zone());
90   const PureOperator& pop = GetParam();
91   const Operator* op = (simplified.*pop.constructor)();
92
93   EXPECT_EQ(pop.value_input_count, op->ValueInputCount());
94   EXPECT_EQ(0, op->EffectInputCount());
95   EXPECT_EQ(0, op->ControlInputCount());
96   EXPECT_EQ(pop.value_input_count, OperatorProperties::GetTotalInputCount(op));
97
98   EXPECT_EQ(1, op->ValueOutputCount());
99   EXPECT_EQ(0, op->EffectOutputCount());
100   EXPECT_EQ(0, op->ControlOutputCount());
101 }
102
103
104 TEST_P(SimplifiedPureOperatorTest, OpcodeIsCorrect) {
105   SimplifiedOperatorBuilder simplified(zone());
106   const PureOperator& pop = GetParam();
107   const Operator* op = (simplified.*pop.constructor)();
108   EXPECT_EQ(pop.opcode, op->opcode());
109 }
110
111
112 TEST_P(SimplifiedPureOperatorTest, Properties) {
113   SimplifiedOperatorBuilder simplified(zone());
114   const PureOperator& pop = GetParam();
115   const Operator* op = (simplified.*pop.constructor)();
116   EXPECT_EQ(pop.properties, op->properties() & pop.properties);
117 }
118
119 INSTANTIATE_TEST_CASE_P(SimplifiedOperatorTest, SimplifiedPureOperatorTest,
120                         ::testing::ValuesIn(kPureOperators));
121
122
123 // -----------------------------------------------------------------------------
124 // Buffer access operators.
125
126
127 namespace {
128
129 const ExternalArrayType kExternalArrayTypes[] = {
130     kExternalUint8Array,   kExternalInt8Array,   kExternalUint16Array,
131     kExternalInt16Array,   kExternalUint32Array, kExternalInt32Array,
132     kExternalFloat32Array, kExternalFloat64Array};
133
134 }  // namespace
135
136
137 class SimplifiedBufferAccessOperatorTest
138     : public TestWithZone,
139       public ::testing::WithParamInterface<ExternalArrayType> {};
140
141
142 TEST_P(SimplifiedBufferAccessOperatorTest, InstancesAreGloballyShared) {
143   BufferAccess const access(GetParam());
144   SimplifiedOperatorBuilder simplified1(zone());
145   SimplifiedOperatorBuilder simplified2(zone());
146   EXPECT_EQ(simplified1.LoadBuffer(access), simplified2.LoadBuffer(access));
147   EXPECT_EQ(simplified1.StoreBuffer(access), simplified2.StoreBuffer(access));
148 }
149
150
151 TEST_P(SimplifiedBufferAccessOperatorTest, LoadBuffer) {
152   SimplifiedOperatorBuilder simplified(zone());
153   BufferAccess const access(GetParam());
154   const Operator* op = simplified.LoadBuffer(access);
155
156   EXPECT_EQ(IrOpcode::kLoadBuffer, op->opcode());
157   EXPECT_EQ(Operator::kNoThrow | Operator::kNoWrite, op->properties());
158   EXPECT_EQ(access, BufferAccessOf(op));
159
160   EXPECT_EQ(3, op->ValueInputCount());
161   EXPECT_EQ(1, op->EffectInputCount());
162   EXPECT_EQ(1, op->ControlInputCount());
163   EXPECT_EQ(5, OperatorProperties::GetTotalInputCount(op));
164
165   EXPECT_EQ(1, op->ValueOutputCount());
166   EXPECT_EQ(1, op->EffectOutputCount());
167   EXPECT_EQ(0, op->ControlOutputCount());
168 }
169
170
171 TEST_P(SimplifiedBufferAccessOperatorTest, StoreBuffer) {
172   SimplifiedOperatorBuilder simplified(zone());
173   BufferAccess const access(GetParam());
174   const Operator* op = simplified.StoreBuffer(access);
175
176   EXPECT_EQ(IrOpcode::kStoreBuffer, op->opcode());
177   EXPECT_EQ(Operator::kNoRead | Operator::kNoThrow, op->properties());
178   EXPECT_EQ(access, BufferAccessOf(op));
179
180   EXPECT_EQ(4, op->ValueInputCount());
181   EXPECT_EQ(1, op->EffectInputCount());
182   EXPECT_EQ(1, op->ControlInputCount());
183   EXPECT_EQ(6, OperatorProperties::GetTotalInputCount(op));
184
185   EXPECT_EQ(0, op->ValueOutputCount());
186   EXPECT_EQ(1, op->EffectOutputCount());
187   EXPECT_EQ(0, op->ControlOutputCount());
188 }
189
190
191 INSTANTIATE_TEST_CASE_P(SimplifiedOperatorTest,
192                         SimplifiedBufferAccessOperatorTest,
193                         ::testing::ValuesIn(kExternalArrayTypes));
194
195
196 // -----------------------------------------------------------------------------
197 // Element access operators.
198
199
200 namespace {
201
202 const ElementAccess kElementAccesses[] = {
203     {kTaggedBase, FixedArray::kHeaderSize, Type::Any(), kMachAnyTagged},
204     {kUntaggedBase, 0, Type::Any(), kMachInt8},
205     {kUntaggedBase, 0, Type::Any(), kMachInt16},
206     {kUntaggedBase, 0, Type::Any(), kMachInt32},
207     {kUntaggedBase, 0, Type::Any(), kMachUint8},
208     {kUntaggedBase, 0, Type::Any(), kMachUint16},
209     {kUntaggedBase, 0, Type::Any(), kMachUint32},
210     {kUntaggedBase, 0, Type::Signed32(), kMachInt8},
211     {kUntaggedBase, 0, Type::Unsigned32(), kMachUint8},
212     {kUntaggedBase, 0, Type::Signed32(), kMachInt16},
213     {kUntaggedBase, 0, Type::Unsigned32(), kMachUint16},
214     {kUntaggedBase, 0, Type::Signed32(), kMachInt32},
215     {kUntaggedBase, 0, Type::Unsigned32(), kMachUint32},
216     {kUntaggedBase, 0, Type::Number(), kRepFloat32},
217     {kUntaggedBase, 0, Type::Number(), kRepFloat64},
218     {kTaggedBase, FixedTypedArrayBase::kDataOffset, Type::Signed32(),
219      kMachInt8},
220     {kTaggedBase, FixedTypedArrayBase::kDataOffset, Type::Unsigned32(),
221      kMachUint8},
222     {kTaggedBase, FixedTypedArrayBase::kDataOffset, Type::Signed32(),
223      kMachInt16},
224     {kTaggedBase, FixedTypedArrayBase::kDataOffset, Type::Unsigned32(),
225      kMachUint16},
226     {kTaggedBase, FixedTypedArrayBase::kDataOffset, Type::Signed32(),
227      kMachInt32},
228     {kTaggedBase, FixedTypedArrayBase::kDataOffset, Type::Unsigned32(),
229      kMachUint32},
230     {kTaggedBase, FixedTypedArrayBase::kDataOffset, Type::Number(),
231      kRepFloat32},
232     {kTaggedBase, FixedTypedArrayBase::kDataOffset, Type::Number(),
233      kRepFloat64}};
234
235 }  // namespace
236
237
238 class SimplifiedElementAccessOperatorTest
239     : public TestWithZone,
240       public ::testing::WithParamInterface<ElementAccess> {};
241
242
243 TEST_P(SimplifiedElementAccessOperatorTest, LoadElement) {
244   SimplifiedOperatorBuilder simplified(zone());
245   const ElementAccess& access = GetParam();
246   const Operator* op = simplified.LoadElement(access);
247
248   EXPECT_EQ(IrOpcode::kLoadElement, op->opcode());
249   EXPECT_EQ(Operator::kNoThrow | Operator::kNoWrite, op->properties());
250   EXPECT_EQ(access, ElementAccessOf(op));
251
252   EXPECT_EQ(2, op->ValueInputCount());
253   EXPECT_EQ(1, op->EffectInputCount());
254   EXPECT_EQ(1, op->ControlInputCount());
255   EXPECT_EQ(4, OperatorProperties::GetTotalInputCount(op));
256
257   EXPECT_EQ(1, op->ValueOutputCount());
258   EXPECT_EQ(1, op->EffectOutputCount());
259   EXPECT_EQ(0, op->ControlOutputCount());
260 }
261
262
263 TEST_P(SimplifiedElementAccessOperatorTest, StoreElement) {
264   SimplifiedOperatorBuilder simplified(zone());
265   const ElementAccess& access = GetParam();
266   const Operator* op = simplified.StoreElement(access);
267
268   EXPECT_EQ(IrOpcode::kStoreElement, op->opcode());
269   EXPECT_EQ(Operator::kNoRead | Operator::kNoThrow, op->properties());
270   EXPECT_EQ(access, ElementAccessOf(op));
271
272   EXPECT_EQ(3, op->ValueInputCount());
273   EXPECT_EQ(1, op->EffectInputCount());
274   EXPECT_EQ(1, op->ControlInputCount());
275   EXPECT_EQ(5, OperatorProperties::GetTotalInputCount(op));
276
277   EXPECT_EQ(0, op->ValueOutputCount());
278   EXPECT_EQ(1, op->EffectOutputCount());
279   EXPECT_EQ(0, op->ControlOutputCount());
280 }
281
282
283 INSTANTIATE_TEST_CASE_P(SimplifiedOperatorTest,
284                         SimplifiedElementAccessOperatorTest,
285                         ::testing::ValuesIn(kElementAccesses));
286
287 }  // namespace compiler
288 }  // namespace internal
289 }  // namespace v8