Line data Source code
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 <limits>
6 :
7 : #include "src/compiler/common-operator.h"
8 : #include "src/compiler/opcodes.h"
9 : #include "src/compiler/operator.h"
10 : #include "src/compiler/operator-properties.h"
11 : #include "test/unittests/test-utils.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 : namespace compiler {
16 : namespace common_operator_unittest {
17 :
18 : // -----------------------------------------------------------------------------
19 : // Shared operators.
20 :
21 :
22 : namespace {
23 :
24 : struct SharedOperator {
25 : const Operator* (CommonOperatorBuilder::*constructor)();
26 : IrOpcode::Value opcode;
27 : Operator::Properties properties;
28 : int value_input_count;
29 : int effect_input_count;
30 : int control_input_count;
31 : int value_output_count;
32 : int effect_output_count;
33 : int control_output_count;
34 : };
35 :
36 :
37 : std::ostream& operator<<(std::ostream& os, const SharedOperator& fop) {
38 86464 : return os << IrOpcode::Mnemonic(fop.opcode);
39 : }
40 :
41 : const SharedOperator kSharedOperators[] = {
42 : #define SHARED(Name, properties, value_input_count, effect_input_count, \
43 : control_input_count, value_output_count, effect_output_count, \
44 : control_output_count) \
45 : { \
46 : &CommonOperatorBuilder::Name, IrOpcode::k##Name, properties, \
47 : value_input_count, effect_input_count, control_input_count, \
48 : value_output_count, effect_output_count, control_output_count \
49 : }
50 : SHARED(Dead, Operator::kFoldable, 0, 0, 0, 1, 1, 1),
51 : SHARED(IfTrue, Operator::kKontrol, 0, 0, 1, 0, 0, 1),
52 : SHARED(IfFalse, Operator::kKontrol, 0, 0, 1, 0, 0, 1),
53 : SHARED(IfSuccess, Operator::kKontrol, 0, 0, 1, 0, 0, 1),
54 : SHARED(IfException, Operator::kKontrol, 0, 1, 1, 1, 1, 1),
55 : SHARED(Throw, Operator::kKontrol, 0, 1, 1, 0, 0, 1),
56 : SHARED(Terminate, Operator::kKontrol, 0, 1, 1, 0, 0, 1)
57 : #undef SHARED
58 : };
59 :
60 :
61 112 : class CommonSharedOperatorTest
62 : : public TestWithZone,
63 : public ::testing::WithParamInterface<SharedOperator> {};
64 :
65 : } // namespace
66 :
67 :
68 18556 : TEST_P(CommonSharedOperatorTest, InstancesAreGloballyShared) {
69 7 : const SharedOperator& sop = GetParam();
70 7 : CommonOperatorBuilder common1(zone());
71 7 : CommonOperatorBuilder common2(zone());
72 14 : EXPECT_EQ((common1.*sop.constructor)(), (common2.*sop.constructor)());
73 7 : }
74 :
75 :
76 18556 : TEST_P(CommonSharedOperatorTest, NumberOfInputsAndOutputs) {
77 7 : CommonOperatorBuilder common(zone());
78 7 : const SharedOperator& sop = GetParam();
79 7 : const Operator* op = (common.*sop.constructor)();
80 :
81 14 : EXPECT_EQ(sop.value_input_count, op->ValueInputCount());
82 14 : EXPECT_EQ(sop.effect_input_count, op->EffectInputCount());
83 14 : EXPECT_EQ(sop.control_input_count, op->ControlInputCount());
84 14 : EXPECT_EQ(
85 : sop.value_input_count + sop.effect_input_count + sop.control_input_count,
86 0 : OperatorProperties::GetTotalInputCount(op));
87 :
88 14 : EXPECT_EQ(sop.value_output_count, op->ValueOutputCount());
89 14 : EXPECT_EQ(sop.effect_output_count, op->EffectOutputCount());
90 14 : EXPECT_EQ(sop.control_output_count, op->ControlOutputCount());
91 7 : }
92 :
93 :
94 18556 : TEST_P(CommonSharedOperatorTest, OpcodeIsCorrect) {
95 7 : CommonOperatorBuilder common(zone());
96 7 : const SharedOperator& sop = GetParam();
97 7 : const Operator* op = (common.*sop.constructor)();
98 14 : EXPECT_EQ(sop.opcode, op->opcode());
99 7 : }
100 :
101 :
102 18556 : TEST_P(CommonSharedOperatorTest, Properties) {
103 7 : CommonOperatorBuilder common(zone());
104 7 : const SharedOperator& sop = GetParam();
105 7 : const Operator* op = (common.*sop.constructor)();
106 14 : EXPECT_EQ(sop.properties, op->properties());
107 7 : }
108 :
109 299536 : INSTANTIATE_TEST_SUITE_P(CommonOperatorTest, CommonSharedOperatorTest,
110 : ::testing::ValuesIn(kSharedOperators));
111 :
112 : // -----------------------------------------------------------------------------
113 : // Other operators.
114 :
115 :
116 : namespace {
117 :
118 : class CommonOperatorTest : public TestWithZone {
119 : public:
120 24 : CommonOperatorTest() : common_(zone()) {}
121 12 : ~CommonOperatorTest() override = default;
122 :
123 10382 : CommonOperatorBuilder* common() { return &common_; }
124 :
125 : private:
126 : CommonOperatorBuilder common_;
127 : };
128 :
129 :
130 : const int kArguments[] = {1, 5, 6, 42, 100, 10000, 65000};
131 :
132 :
133 : const size_t kCases[] = {3, 4, 100, 255, 1024, 65000};
134 :
135 :
136 : const float kFloatValues[] = {-std::numeric_limits<float>::infinity(),
137 : std::numeric_limits<float>::min(),
138 : -1.0f,
139 : -0.0f,
140 : 0.0f,
141 : 1.0f,
142 : std::numeric_limits<float>::max(),
143 : std::numeric_limits<float>::infinity(),
144 : std::numeric_limits<float>::quiet_NaN(),
145 : std::numeric_limits<float>::signaling_NaN()};
146 :
147 :
148 : const size_t kInputCounts[] = {3, 4, 100, 255, 1024, 65000};
149 :
150 :
151 : const int32_t kInt32Values[] = {
152 : std::numeric_limits<int32_t>::min(), -1914954528, -1698749618, -1578693386,
153 : -1577976073, -1573998034, -1529085059, -1499540537, -1299205097,
154 : -1090814845, -938186388, -806828902, -750927650, -520676892, -513661538,
155 : -453036354, -433622833, -282638793, -28375, -27788, -22770, -18806, -14173,
156 : -11956, -11200, -10212, -8160, -3751, -2758, -1522, -121, -120, -118, -117,
157 : -106, -84, -80, -74, -59, -52, -48, -39, -35, -17, -11, -10, -9, -7, -5, 0,
158 : 9, 12, 17, 23, 29, 31, 33, 35, 40, 47, 55, 56, 62, 64, 67, 68, 69, 74, 79,
159 : 84, 89, 90, 97, 104, 118, 124, 126, 127, 7278, 17787, 24136, 24202, 25570,
160 : 26680, 30242, 32399, 420886487, 642166225, 821912648, 822577803, 851385718,
161 : 1212241078, 1411419304, 1589626102, 1596437184, 1876245816, 1954730266,
162 : 2008792749, 2045320228, std::numeric_limits<int32_t>::max()};
163 :
164 :
165 : const BranchHint kBranchHints[] = {BranchHint::kNone, BranchHint::kTrue,
166 : BranchHint::kFalse};
167 :
168 : } // namespace
169 :
170 :
171 15444 : TEST_F(CommonOperatorTest, End) {
172 25 : TRACED_FOREACH(size_t, input_count, kInputCounts) {
173 12 : const Operator* const op = common()->End(input_count);
174 12 : EXPECT_EQ(IrOpcode::kEnd, op->opcode());
175 12 : EXPECT_EQ(Operator::kKontrol, op->properties());
176 12 : EXPECT_EQ(0, op->ValueInputCount());
177 12 : EXPECT_EQ(0, op->EffectInputCount());
178 12 : EXPECT_EQ(input_count, static_cast<uint32_t>(op->ControlInputCount()));
179 12 : EXPECT_EQ(input_count, static_cast<uint32_t>(
180 0 : OperatorProperties::GetTotalInputCount(op)));
181 12 : EXPECT_EQ(0, op->ValueOutputCount());
182 12 : EXPECT_EQ(0, op->EffectOutputCount());
183 12 : EXPECT_EQ(0, op->ControlOutputCount());
184 : }
185 1 : }
186 :
187 :
188 15444 : TEST_F(CommonOperatorTest, Return) {
189 29 : TRACED_FOREACH(int, input_count, kArguments) {
190 7 : const Operator* const op = common()->Return(input_count);
191 14 : EXPECT_EQ(IrOpcode::kReturn, op->opcode());
192 14 : EXPECT_EQ(Operator::kNoThrow, op->properties());
193 14 : EXPECT_EQ(input_count + 1, op->ValueInputCount());
194 14 : EXPECT_EQ(1, op->EffectInputCount());
195 14 : EXPECT_EQ(1, op->ControlInputCount());
196 14 : EXPECT_EQ(3 + input_count, OperatorProperties::GetTotalInputCount(op));
197 14 : EXPECT_EQ(0, op->ValueOutputCount());
198 14 : EXPECT_EQ(0, op->EffectOutputCount());
199 14 : EXPECT_EQ(1, op->ControlOutputCount());
200 : }
201 1 : }
202 :
203 :
204 15444 : TEST_F(CommonOperatorTest, Branch) {
205 13 : TRACED_FOREACH(BranchHint, hint, kBranchHints) {
206 6 : const Operator* const op = common()->Branch(hint);
207 6 : EXPECT_EQ(IrOpcode::kBranch, op->opcode());
208 6 : EXPECT_EQ(Operator::kKontrol, op->properties());
209 6 : EXPECT_EQ(hint, BranchHintOf(op));
210 6 : EXPECT_EQ(1, op->ValueInputCount());
211 6 : EXPECT_EQ(0, op->EffectInputCount());
212 6 : EXPECT_EQ(1, op->ControlInputCount());
213 6 : EXPECT_EQ(2, OperatorProperties::GetTotalInputCount(op));
214 6 : EXPECT_EQ(0, op->ValueOutputCount());
215 6 : EXPECT_EQ(0, op->EffectOutputCount());
216 6 : EXPECT_EQ(2, op->ControlOutputCount());
217 : }
218 1 : }
219 :
220 :
221 15444 : TEST_F(CommonOperatorTest, Switch) {
222 25 : TRACED_FOREACH(size_t, cases, kCases) {
223 6 : const Operator* const op = common()->Switch(cases);
224 12 : EXPECT_EQ(IrOpcode::kSwitch, op->opcode());
225 12 : EXPECT_EQ(Operator::kKontrol, op->properties());
226 12 : EXPECT_EQ(1, op->ValueInputCount());
227 12 : EXPECT_EQ(0, op->EffectInputCount());
228 12 : EXPECT_EQ(1, op->ControlInputCount());
229 12 : EXPECT_EQ(2, OperatorProperties::GetTotalInputCount(op));
230 12 : EXPECT_EQ(0, op->ValueOutputCount());
231 12 : EXPECT_EQ(0, op->EffectOutputCount());
232 12 : EXPECT_EQ(static_cast<int>(cases), op->ControlOutputCount());
233 : }
234 1 : }
235 :
236 :
237 15444 : TEST_F(CommonOperatorTest, IfValue) {
238 401 : TRACED_FOREACH(int32_t, value, kInt32Values) {
239 40100 : TRACED_FOREACH(int32_t, order, kInt32Values) {
240 10000 : const Operator* const op = common()->IfValue(value, order);
241 20000 : EXPECT_EQ(IrOpcode::kIfValue, op->opcode());
242 20000 : EXPECT_EQ(Operator::kKontrol, op->properties());
243 20000 : EXPECT_EQ(IfValueParameters(value, order), IfValueParametersOf(op));
244 20000 : EXPECT_EQ(0, op->ValueInputCount());
245 20000 : EXPECT_EQ(0, op->EffectInputCount());
246 20000 : EXPECT_EQ(1, op->ControlInputCount());
247 20000 : EXPECT_EQ(1, OperatorProperties::GetTotalInputCount(op));
248 20000 : EXPECT_EQ(0, op->ValueOutputCount());
249 20000 : EXPECT_EQ(0, op->EffectOutputCount());
250 20000 : EXPECT_EQ(1, op->ControlOutputCount());
251 : }
252 : }
253 1 : }
254 :
255 :
256 15444 : TEST_F(CommonOperatorTest, Select) {
257 : static const MachineRepresentation kMachineRepresentations[] = {
258 : MachineRepresentation::kBit, MachineRepresentation::kWord8,
259 : MachineRepresentation::kWord16, MachineRepresentation::kWord32,
260 : MachineRepresentation::kWord64, MachineRepresentation::kFloat32,
261 : MachineRepresentation::kFloat64, MachineRepresentation::kTagged};
262 :
263 :
264 33 : TRACED_FOREACH(MachineRepresentation, rep, kMachineRepresentations) {
265 104 : TRACED_FOREACH(BranchHint, hint, kBranchHints) {
266 48 : const Operator* const op = common()->Select(rep, hint);
267 48 : EXPECT_EQ(IrOpcode::kSelect, op->opcode());
268 48 : EXPECT_EQ(Operator::kPure, op->properties());
269 48 : EXPECT_EQ(rep, SelectParametersOf(op).representation());
270 48 : EXPECT_EQ(hint, SelectParametersOf(op).hint());
271 48 : EXPECT_EQ(3, op->ValueInputCount());
272 48 : EXPECT_EQ(0, op->EffectInputCount());
273 48 : EXPECT_EQ(0, op->ControlInputCount());
274 48 : EXPECT_EQ(3, OperatorProperties::GetTotalInputCount(op));
275 48 : EXPECT_EQ(1, op->ValueOutputCount());
276 48 : EXPECT_EQ(0, op->EffectOutputCount());
277 48 : EXPECT_EQ(0, op->ControlOutputCount());
278 : }
279 : }
280 1 : }
281 :
282 :
283 15444 : TEST_F(CommonOperatorTest, Float32Constant) {
284 41 : TRACED_FOREACH(float, value, kFloatValues) {
285 20 : const Operator* op = common()->Float32Constant(value);
286 10 : EXPECT_PRED2(base::bit_equal_to<float>(), value, OpParameter<float>(op));
287 20 : EXPECT_EQ(0, op->ValueInputCount());
288 20 : EXPECT_EQ(0, OperatorProperties::GetTotalInputCount(op));
289 20 : EXPECT_EQ(0, op->ControlOutputCount());
290 20 : EXPECT_EQ(0, op->EffectOutputCount());
291 20 : EXPECT_EQ(1, op->ValueOutputCount());
292 : }
293 41 : TRACED_FOREACH(float, v1, kFloatValues) {
294 410 : TRACED_FOREACH(float, v2, kFloatValues) {
295 100 : const Operator* op1 = common()->Float32Constant(v1);
296 100 : const Operator* op2 = common()->Float32Constant(v2);
297 200 : EXPECT_EQ(bit_cast<uint32_t>(v1) == bit_cast<uint32_t>(v2),
298 0 : op1->Equals(op2));
299 : }
300 : }
301 1 : }
302 :
303 :
304 15444 : TEST_F(CommonOperatorTest, Float64Constant) {
305 41 : TRACED_FOREACH(double, value, kFloatValues) {
306 20 : const Operator* op = common()->Float64Constant(value);
307 10 : EXPECT_PRED2(base::bit_equal_to<double>(), value, OpParameter<double>(op));
308 20 : EXPECT_EQ(0, op->ValueInputCount());
309 20 : EXPECT_EQ(0, OperatorProperties::GetTotalInputCount(op));
310 20 : EXPECT_EQ(0, op->ControlOutputCount());
311 20 : EXPECT_EQ(0, op->EffectOutputCount());
312 20 : EXPECT_EQ(1, op->ValueOutputCount());
313 : }
314 41 : TRACED_FOREACH(double, v1, kFloatValues) {
315 410 : TRACED_FOREACH(double, v2, kFloatValues) {
316 100 : const Operator* op1 = common()->Float64Constant(v1);
317 100 : const Operator* op2 = common()->Float64Constant(v2);
318 200 : EXPECT_EQ(bit_cast<uint64_t>(v1) == bit_cast<uint64_t>(v2),
319 0 : op1->Equals(op2));
320 : }
321 : }
322 1 : }
323 :
324 :
325 15444 : TEST_F(CommonOperatorTest, NumberConstant) {
326 41 : TRACED_FOREACH(double, value, kFloatValues) {
327 20 : const Operator* op = common()->NumberConstant(value);
328 10 : EXPECT_PRED2(base::bit_equal_to<double>(), value, OpParameter<double>(op));
329 20 : EXPECT_EQ(0, op->ValueInputCount());
330 20 : EXPECT_EQ(0, OperatorProperties::GetTotalInputCount(op));
331 20 : EXPECT_EQ(0, op->ControlOutputCount());
332 20 : EXPECT_EQ(0, op->EffectOutputCount());
333 20 : EXPECT_EQ(1, op->ValueOutputCount());
334 : }
335 41 : TRACED_FOREACH(double, v1, kFloatValues) {
336 410 : TRACED_FOREACH(double, v2, kFloatValues) {
337 100 : const Operator* op1 = common()->NumberConstant(v1);
338 100 : const Operator* op2 = common()->NumberConstant(v2);
339 200 : EXPECT_EQ(bit_cast<uint64_t>(v1) == bit_cast<uint64_t>(v2),
340 0 : op1->Equals(op2));
341 : }
342 : }
343 1 : }
344 :
345 :
346 15444 : TEST_F(CommonOperatorTest, BeginRegion) {
347 : {
348 : const Operator* op =
349 1 : common()->BeginRegion(RegionObservability::kObservable);
350 2 : EXPECT_EQ(1, op->EffectInputCount());
351 2 : EXPECT_EQ(1, OperatorProperties::GetTotalInputCount(op));
352 2 : EXPECT_EQ(0, op->ControlOutputCount());
353 2 : EXPECT_EQ(1, op->EffectOutputCount());
354 2 : EXPECT_EQ(0, op->ValueOutputCount());
355 : }
356 : {
357 : const Operator* op =
358 1 : common()->BeginRegion(RegionObservability::kNotObservable);
359 2 : EXPECT_EQ(1, op->EffectInputCount());
360 2 : EXPECT_EQ(1, OperatorProperties::GetTotalInputCount(op));
361 2 : EXPECT_EQ(0, op->ControlOutputCount());
362 2 : EXPECT_EQ(1, op->EffectOutputCount());
363 2 : EXPECT_EQ(0, op->ValueOutputCount());
364 : }
365 1 : }
366 :
367 15444 : TEST_F(CommonOperatorTest, FinishRegion) {
368 1 : const Operator* op = common()->FinishRegion();
369 2 : EXPECT_EQ(1, op->ValueInputCount());
370 2 : EXPECT_EQ(1, op->EffectInputCount());
371 2 : EXPECT_EQ(2, OperatorProperties::GetTotalInputCount(op));
372 2 : EXPECT_EQ(0, op->ControlOutputCount());
373 2 : EXPECT_EQ(1, op->EffectOutputCount());
374 2 : EXPECT_EQ(1, op->ValueOutputCount());
375 1 : }
376 :
377 15444 : TEST_F(CommonOperatorTest, Projection) {
378 17 : TRACED_FORRANGE(size_t, index, 0, 3) {
379 8 : const Operator* op = common()->Projection(index);
380 8 : EXPECT_EQ(index, ProjectionIndexOf(op));
381 8 : EXPECT_EQ(1, op->ValueInputCount());
382 8 : EXPECT_EQ(1, op->ControlInputCount());
383 8 : EXPECT_EQ(2, OperatorProperties::GetTotalInputCount(op));
384 8 : EXPECT_EQ(0, op->ControlOutputCount());
385 8 : EXPECT_EQ(0, op->EffectOutputCount());
386 8 : EXPECT_EQ(1, op->ValueOutputCount());
387 : }
388 1 : }
389 :
390 : } // namespace common_operator_unittest
391 : } // namespace compiler
392 : } // namespace internal
393 9264 : } // namespace v8
|