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.
5 #include "test/unittests/compiler/instruction-selector-unittest.h"
7 #include "src/compiler/graph.h"
8 #include "src/compiler/schedule.h"
10 #include "test/unittests/compiler/compiler-test-utils.h"
18 typedef RawMachineAssembler::Label MLabel;
23 InstructionSelectorTest::InstructionSelectorTest() : rng_(FLAG_random_seed) {}
26 InstructionSelectorTest::~InstructionSelectorTest() {}
29 InstructionSelectorTest::Stream InstructionSelectorTest::StreamBuilder::Build(
30 InstructionSelector::Features features,
31 InstructionSelectorTest::StreamBuilderMode mode) {
32 Schedule* schedule = Export();
33 if (FLAG_trace_turbo) {
35 out << "=== Schedule before instruction selection ===" << std::endl
38 size_t const node_count = graph()->NodeCount();
39 EXPECT_NE(0u, node_count);
40 Linkage linkage(call_descriptor());
41 InstructionBlocks* instruction_blocks =
42 InstructionSequence::InstructionBlocksFor(test_->zone(), schedule);
43 InstructionSequence sequence(test_->isolate(), test_->zone(),
45 SourcePositionTable source_position_table(graph());
46 InstructionSelector selector(test_->zone(), node_count, &linkage, &sequence,
47 schedule, &source_position_table, features);
48 selector.SelectInstructions();
49 if (FLAG_trace_turbo) {
51 PrintableInstructionSequence printable = {
52 RegisterConfiguration::ArchDefault(), &sequence};
53 out << "=== Code sequence after instruction selection ===" << std::endl
57 s.virtual_registers_ = selector.GetVirtualRegistersForTesting();
58 // Map virtual registers.
59 for (Instruction* const instr : sequence) {
60 if (instr->opcode() < 0) continue;
61 if (mode == kTargetInstructions) {
62 switch (instr->arch_opcode()) {
66 TARGET_ARCH_OPCODE_LIST(CASE)
72 if (mode == kAllExceptNopInstructions && instr->arch_opcode() == kArchNop) {
75 for (size_t i = 0; i < instr->OutputCount(); ++i) {
76 InstructionOperand* output = instr->OutputAt(i);
77 EXPECT_NE(InstructionOperand::IMMEDIATE, output->kind());
78 if (output->IsConstant()) {
79 s.constants_.insert(std::make_pair(
80 output->index(), sequence.GetConstant(output->index())));
83 for (size_t i = 0; i < instr->InputCount(); ++i) {
84 InstructionOperand* input = instr->InputAt(i);
85 EXPECT_NE(InstructionOperand::CONSTANT, input->kind());
86 if (input->IsImmediate()) {
87 s.immediates_.insert(std::make_pair(
88 input->index(), sequence.GetImmediate(input->index())));
91 s.instructions_.push_back(instr);
93 for (auto i : s.virtual_registers_) {
94 int const virtual_register = i.second;
95 if (sequence.IsDouble(virtual_register)) {
96 EXPECT_FALSE(sequence.IsReference(virtual_register));
97 s.doubles_.insert(virtual_register);
99 if (sequence.IsReference(virtual_register)) {
100 EXPECT_FALSE(sequence.IsDouble(virtual_register));
101 s.references_.insert(virtual_register);
104 for (int i = 0; i < sequence.GetFrameStateDescriptorCount(); i++) {
105 s.deoptimization_entries_.push_back(sequence.GetFrameStateDescriptor(
106 InstructionSequence::StateId::FromInt(i)));
112 int InstructionSelectorTest::Stream::ToVreg(const Node* node) const {
113 VirtualRegisters::const_iterator i = virtual_registers_.find(node->id());
114 CHECK(i != virtual_registers_.end());
119 bool InstructionSelectorTest::Stream::IsFixed(const InstructionOperand* operand,
120 Register reg) const {
121 if (!operand->IsUnallocated()) return false;
122 const UnallocatedOperand* unallocated = UnallocatedOperand::cast(operand);
123 if (!unallocated->HasFixedRegisterPolicy()) return false;
124 const int index = Register::ToAllocationIndex(reg);
125 return unallocated->fixed_register_index() == index;
129 bool InstructionSelectorTest::Stream::IsSameAsFirst(
130 const InstructionOperand* operand) const {
131 if (!operand->IsUnallocated()) return false;
132 const UnallocatedOperand* unallocated = UnallocatedOperand::cast(operand);
133 return unallocated->HasSameAsInputPolicy();
137 bool InstructionSelectorTest::Stream::IsUsedAtStart(
138 const InstructionOperand* operand) const {
139 if (!operand->IsUnallocated()) return false;
140 const UnallocatedOperand* unallocated = UnallocatedOperand::cast(operand);
141 return unallocated->IsUsedAtStart();
145 // -----------------------------------------------------------------------------
149 TARGET_TEST_F(InstructionSelectorTest, ReturnFloat32Constant) {
150 const float kValue = 4.2f;
151 StreamBuilder m(this, kMachFloat32);
152 m.Return(m.Float32Constant(kValue));
153 Stream s = m.Build(kAllInstructions);
154 ASSERT_EQ(3U, s.size());
155 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
156 ASSERT_EQ(InstructionOperand::CONSTANT, s[0]->OutputAt(0)->kind());
157 EXPECT_FLOAT_EQ(kValue, s.ToFloat32(s[0]->OutputAt(0)));
158 EXPECT_EQ(kArchRet, s[1]->arch_opcode());
159 EXPECT_EQ(1U, s[1]->InputCount());
163 TARGET_TEST_F(InstructionSelectorTest, ReturnParameter) {
164 StreamBuilder m(this, kMachInt32, kMachInt32);
165 m.Return(m.Parameter(0));
166 Stream s = m.Build(kAllInstructions);
167 ASSERT_EQ(3U, s.size());
168 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
169 ASSERT_EQ(1U, s[0]->OutputCount());
170 EXPECT_EQ(kArchRet, s[1]->arch_opcode());
171 EXPECT_EQ(1U, s[1]->InputCount());
175 TARGET_TEST_F(InstructionSelectorTest, ReturnZero) {
176 StreamBuilder m(this, kMachInt32);
177 m.Return(m.Int32Constant(0));
178 Stream s = m.Build(kAllInstructions);
179 ASSERT_EQ(3U, s.size());
180 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
181 ASSERT_EQ(1U, s[0]->OutputCount());
182 EXPECT_EQ(InstructionOperand::CONSTANT, s[0]->OutputAt(0)->kind());
183 EXPECT_EQ(0, s.ToInt32(s[0]->OutputAt(0)));
184 EXPECT_EQ(kArchRet, s[1]->arch_opcode());
185 EXPECT_EQ(1U, s[1]->InputCount());
189 // -----------------------------------------------------------------------------
193 TARGET_TEST_F(InstructionSelectorTest, TruncateFloat64ToInt32WithParameter) {
194 StreamBuilder m(this, kMachInt32, kMachFloat64);
195 m.Return(m.TruncateFloat64ToInt32(m.Parameter(0)));
196 Stream s = m.Build(kAllInstructions);
197 ASSERT_EQ(4U, s.size());
198 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
199 EXPECT_EQ(kArchTruncateDoubleToI, s[1]->arch_opcode());
200 EXPECT_EQ(1U, s[1]->InputCount());
201 EXPECT_EQ(1U, s[1]->OutputCount());
202 EXPECT_EQ(kArchRet, s[2]->arch_opcode());
206 // -----------------------------------------------------------------------------
210 TARGET_TEST_F(InstructionSelectorTest, DoubleParameter) {
211 StreamBuilder m(this, kMachFloat64, kMachFloat64);
212 Node* param = m.Parameter(0);
214 Stream s = m.Build(kAllInstructions);
215 EXPECT_TRUE(s.IsDouble(param));
219 TARGET_TEST_F(InstructionSelectorTest, ReferenceParameter) {
220 StreamBuilder m(this, kMachAnyTagged, kMachAnyTagged);
221 Node* param = m.Parameter(0);
223 Stream s = m.Build(kAllInstructions);
224 EXPECT_TRUE(s.IsReference(param));
228 // -----------------------------------------------------------------------------
232 TARGET_TEST_F(InstructionSelectorTest, Finish) {
233 StreamBuilder m(this, kMachAnyTagged, kMachAnyTagged);
234 Node* param = m.Parameter(0);
235 Node* finish = m.NewNode(m.common()->Finish(1), param, m.graph()->start());
237 Stream s = m.Build(kAllInstructions);
238 ASSERT_EQ(4U, s.size());
239 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
240 ASSERT_EQ(1U, s[0]->OutputCount());
241 ASSERT_TRUE(s[0]->Output()->IsUnallocated());
242 EXPECT_EQ(s.ToVreg(param), s.ToVreg(s[0]->Output()));
243 EXPECT_EQ(kArchNop, s[1]->arch_opcode());
244 ASSERT_EQ(1U, s[1]->InputCount());
245 ASSERT_TRUE(s[1]->InputAt(0)->IsUnallocated());
246 EXPECT_EQ(s.ToVreg(param), s.ToVreg(s[1]->InputAt(0)));
247 ASSERT_EQ(1U, s[1]->OutputCount());
248 ASSERT_TRUE(s[1]->Output()->IsUnallocated());
249 EXPECT_TRUE(UnallocatedOperand::cast(s[1]->Output())->HasSameAsInputPolicy());
250 EXPECT_EQ(s.ToVreg(finish), s.ToVreg(s[1]->Output()));
251 EXPECT_TRUE(s.IsReference(finish));
255 // -----------------------------------------------------------------------------
259 typedef InstructionSelectorTestWithParam<MachineType>
260 InstructionSelectorPhiTest;
263 TARGET_TEST_P(InstructionSelectorPhiTest, Doubleness) {
264 const MachineType type = GetParam();
265 StreamBuilder m(this, type, type, type);
266 Node* param0 = m.Parameter(0);
267 Node* param1 = m.Parameter(1);
269 m.Branch(m.Int32Constant(0), &a, &b);
275 Node* phi = m.Phi(type, param0, param1);
277 Stream s = m.Build(kAllInstructions);
278 EXPECT_EQ(s.IsDouble(phi), s.IsDouble(param0));
279 EXPECT_EQ(s.IsDouble(phi), s.IsDouble(param1));
283 TARGET_TEST_P(InstructionSelectorPhiTest, Referenceness) {
284 const MachineType type = GetParam();
285 StreamBuilder m(this, type, type, type);
286 Node* param0 = m.Parameter(0);
287 Node* param1 = m.Parameter(1);
289 m.Branch(m.Int32Constant(1), &a, &b);
295 Node* phi = m.Phi(type, param0, param1);
297 Stream s = m.Build(kAllInstructions);
298 EXPECT_EQ(s.IsReference(phi), s.IsReference(param0));
299 EXPECT_EQ(s.IsReference(phi), s.IsReference(param1));
303 INSTANTIATE_TEST_CASE_P(InstructionSelectorTest, InstructionSelectorPhiTest,
304 ::testing::Values(kMachFloat64, kMachInt8, kMachUint8,
305 kMachInt16, kMachUint16, kMachInt32,
306 kMachUint32, kMachInt64, kMachUint64,
307 kMachPtr, kMachAnyTagged));
310 // -----------------------------------------------------------------------------
314 TARGET_TEST_F(InstructionSelectorTest, ValueEffect) {
315 StreamBuilder m1(this, kMachInt32, kMachPtr);
316 Node* p1 = m1.Parameter(0);
317 m1.Return(m1.Load(kMachInt32, p1, m1.Int32Constant(0)));
318 Stream s1 = m1.Build(kAllInstructions);
319 StreamBuilder m2(this, kMachInt32, kMachPtr);
320 Node* p2 = m2.Parameter(0);
321 m2.Return(m2.NewNode(m2.machine()->Load(kMachInt32), p2, m2.Int32Constant(0),
322 m2.NewNode(m2.common()->ValueEffect(1), p2)));
323 Stream s2 = m2.Build(kAllInstructions);
324 EXPECT_LE(3U, s1.size());
325 ASSERT_EQ(s1.size(), s2.size());
326 TRACED_FORRANGE(size_t, i, 0, s1.size() - 1) {
327 const Instruction* i1 = s1[i];
328 const Instruction* i2 = s2[i];
329 EXPECT_EQ(i1->arch_opcode(), i2->arch_opcode());
330 EXPECT_EQ(i1->InputCount(), i2->InputCount());
331 EXPECT_EQ(i1->OutputCount(), i2->OutputCount());
336 // -----------------------------------------------------------------------------
337 // Calls with deoptimization.
340 TARGET_TEST_F(InstructionSelectorTest, CallJSFunctionWithDeopt) {
341 StreamBuilder m(this, kMachAnyTagged, kMachAnyTagged, kMachAnyTagged,
344 BailoutId bailout_id(42);
346 Node* function_node = m.Parameter(0);
347 Node* receiver = m.Parameter(1);
348 Node* context = m.Parameter(2);
350 ZoneVector<MachineType> int32_type(1, kMachInt32, zone());
351 ZoneVector<MachineType> empty_types(zone());
354 m.NewNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(1));
355 Node* locals = m.NewNode(m.common()->TypedStateValues(&empty_types));
356 Node* stack = m.NewNode(m.common()->TypedStateValues(&empty_types));
357 Node* context_dummy = m.Int32Constant(0);
359 Node* state_node = m.NewNode(
360 m.common()->FrameState(JS_FRAME, bailout_id,
361 OutputFrameStateCombine::Push()),
362 parameters, locals, stack, context_dummy, m.UndefinedConstant());
363 Node* call = m.CallJS0(function_node, receiver, context, state_node);
366 Stream s = m.Build(kAllExceptNopInstructions);
368 // Skip until kArchCallJSFunction.
370 for (; index < s.size() && s[index]->arch_opcode() != kArchCallJSFunction;
373 // Now we should have two instructions: call and return.
374 ASSERT_EQ(index + 2, s.size());
376 EXPECT_EQ(kArchCallJSFunction, s[index++]->arch_opcode());
377 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
379 // TODO(jarin) Check deoptimization table.
383 TARGET_TEST_F(InstructionSelectorTest, CallFunctionStubWithDeopt) {
384 StreamBuilder m(this, kMachAnyTagged, kMachAnyTagged, kMachAnyTagged,
387 BailoutId bailout_id_before(42);
389 // Some arguments for the call node.
390 Node* function_node = m.Parameter(0);
391 Node* receiver = m.Parameter(1);
392 Node* context = m.Int32Constant(1); // Context is ignored.
394 ZoneVector<MachineType> int32_type(1, kMachInt32, zone());
395 ZoneVector<MachineType> float64_type(1, kMachFloat64, zone());
396 ZoneVector<MachineType> tagged_type(1, kMachAnyTagged, zone());
398 // Build frame state for the state before the call.
400 m.NewNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(43));
401 Node* locals = m.NewNode(m.common()->TypedStateValues(&float64_type),
402 m.Float64Constant(0.5));
403 Node* stack = m.NewNode(m.common()->TypedStateValues(&tagged_type),
404 m.UndefinedConstant());
406 Node* context_sentinel = m.Int32Constant(0);
407 Node* frame_state_before = m.NewNode(
408 m.common()->FrameState(JS_FRAME, bailout_id_before,
409 OutputFrameStateCombine::Push()),
410 parameters, locals, stack, context_sentinel, m.UndefinedConstant());
413 Node* call = m.CallFunctionStub0(function_node, receiver, context,
414 frame_state_before, CALL_AS_METHOD);
418 Stream s = m.Build(kAllExceptNopInstructions);
420 // Skip until kArchCallJSFunction.
422 for (; index < s.size() && s[index]->arch_opcode() != kArchCallCodeObject;
425 // Now we should have two instructions: call, return.
426 ASSERT_EQ(index + 2, s.size());
428 // Check the call instruction
429 const Instruction* call_instr = s[index++];
430 EXPECT_EQ(kArchCallCodeObject, call_instr->arch_opcode());
431 size_t num_operands =
434 4 + // Frame state deopt id + one input for each value in frame state.
437 ASSERT_EQ(num_operands, call_instr->InputCount());
440 EXPECT_TRUE(call_instr->InputAt(0)->IsImmediate());
442 // Deoptimization id.
443 int32_t deopt_id_before = s.ToInt32(call_instr->InputAt(1));
444 FrameStateDescriptor* desc_before =
445 s.GetFrameStateDescriptor(deopt_id_before);
446 EXPECT_EQ(bailout_id_before, desc_before->bailout_id());
447 EXPECT_EQ(OutputFrameStateCombine::kPushOutput,
448 desc_before->state_combine().kind());
449 EXPECT_EQ(1u, desc_before->parameters_count());
450 EXPECT_EQ(1u, desc_before->locals_count());
451 EXPECT_EQ(1u, desc_before->stack_count());
452 EXPECT_EQ(43, s.ToInt32(call_instr->InputAt(2)));
453 EXPECT_EQ(0, s.ToInt32(call_instr->InputAt(3))); // This should be a context.
454 // We inserted 0 here.
455 EXPECT_EQ(0.5, s.ToFloat64(call_instr->InputAt(4)));
456 EXPECT_TRUE(s.ToHeapObject(call_instr->InputAt(5))->IsUndefined());
457 EXPECT_EQ(kMachInt32, desc_before->GetType(0));
458 EXPECT_EQ(kMachAnyTagged, desc_before->GetType(1)); // context is always
460 EXPECT_EQ(kMachFloat64, desc_before->GetType(2));
461 EXPECT_EQ(kMachAnyTagged, desc_before->GetType(3));
464 EXPECT_EQ(s.ToVreg(function_node), s.ToVreg(call_instr->InputAt(6)));
466 EXPECT_EQ(s.ToVreg(context), s.ToVreg(call_instr->InputAt(7)));
468 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
470 EXPECT_EQ(index, s.size());
474 TARGET_TEST_F(InstructionSelectorTest,
475 CallFunctionStubDeoptRecursiveFrameState) {
476 StreamBuilder m(this, kMachAnyTagged, kMachAnyTagged, kMachAnyTagged,
479 BailoutId bailout_id_before(42);
480 BailoutId bailout_id_parent(62);
482 // Some arguments for the call node.
483 Node* function_node = m.Parameter(0);
484 Node* receiver = m.Parameter(1);
485 Node* context = m.Int32Constant(66);
487 ZoneVector<MachineType> int32_type(1, kMachInt32, zone());
488 ZoneVector<MachineType> int32x2_type(2, kMachInt32, zone());
489 ZoneVector<MachineType> float64_type(1, kMachFloat64, zone());
491 // Build frame state for the state before the call.
493 m.NewNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(63));
495 m.NewNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(64));
497 m.NewNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(65));
498 Node* frame_state_parent =
499 m.NewNode(m.common()->FrameState(JS_FRAME, bailout_id_parent,
500 OutputFrameStateCombine::Ignore()),
501 parameters, locals, stack, context, m.UndefinedConstant());
503 Node* context2 = m.Int32Constant(46);
505 m.NewNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(43));
506 Node* locals2 = m.NewNode(m.common()->TypedStateValues(&float64_type),
507 m.Float64Constant(0.25));
508 Node* stack2 = m.NewNode(m.common()->TypedStateValues(&int32x2_type),
509 m.Int32Constant(44), m.Int32Constant(45));
510 Node* frame_state_before =
511 m.NewNode(m.common()->FrameState(JS_FRAME, bailout_id_before,
512 OutputFrameStateCombine::Push()),
513 parameters2, locals2, stack2, context2, frame_state_parent);
516 Node* call = m.CallFunctionStub0(function_node, receiver, context2,
517 frame_state_before, CALL_AS_METHOD);
521 Stream s = m.Build(kAllExceptNopInstructions);
523 // Skip until kArchCallJSFunction.
525 for (; index < s.size() && s[index]->arch_opcode() != kArchCallCodeObject;
528 // Now we should have three instructions: call, return.
529 EXPECT_EQ(index + 2, s.size());
531 // Check the call instruction
532 const Instruction* call_instr = s[index++];
533 EXPECT_EQ(kArchCallCodeObject, call_instr->arch_opcode());
534 size_t num_operands =
536 1 + // Frame state deopt id
537 5 + // One input for each value in frame state + context.
538 4 + // One input for each value in the parent frame state + context.
541 EXPECT_EQ(num_operands, call_instr->InputCount());
543 EXPECT_TRUE(call_instr->InputAt(0)->IsImmediate());
545 // Deoptimization id.
546 int32_t deopt_id_before = s.ToInt32(call_instr->InputAt(1));
547 FrameStateDescriptor* desc_before =
548 s.GetFrameStateDescriptor(deopt_id_before);
549 FrameStateDescriptor* desc_before_outer = desc_before->outer_state();
550 EXPECT_EQ(bailout_id_before, desc_before->bailout_id());
551 EXPECT_EQ(1u, desc_before_outer->parameters_count());
552 EXPECT_EQ(1u, desc_before_outer->locals_count());
553 EXPECT_EQ(1u, desc_before_outer->stack_count());
554 // Values from parent environment.
555 EXPECT_EQ(63, s.ToInt32(call_instr->InputAt(2)));
556 EXPECT_EQ(kMachInt32, desc_before_outer->GetType(0));
558 EXPECT_EQ(66, s.ToInt32(call_instr->InputAt(3)));
559 EXPECT_EQ(kMachAnyTagged, desc_before_outer->GetType(1));
560 EXPECT_EQ(64, s.ToInt32(call_instr->InputAt(4)));
561 EXPECT_EQ(kMachInt32, desc_before_outer->GetType(2));
562 EXPECT_EQ(65, s.ToInt32(call_instr->InputAt(5)));
563 EXPECT_EQ(kMachInt32, desc_before_outer->GetType(3));
564 // Values from the nested frame.
565 EXPECT_EQ(1u, desc_before->parameters_count());
566 EXPECT_EQ(1u, desc_before->locals_count());
567 EXPECT_EQ(2u, desc_before->stack_count());
568 EXPECT_EQ(43, s.ToInt32(call_instr->InputAt(6)));
569 EXPECT_EQ(kMachInt32, desc_before->GetType(0));
570 EXPECT_EQ(46, s.ToInt32(call_instr->InputAt(7)));
571 EXPECT_EQ(kMachAnyTagged, desc_before->GetType(1));
572 EXPECT_EQ(0.25, s.ToFloat64(call_instr->InputAt(8)));
573 EXPECT_EQ(kMachFloat64, desc_before->GetType(2));
574 EXPECT_EQ(44, s.ToInt32(call_instr->InputAt(9)));
575 EXPECT_EQ(kMachInt32, desc_before->GetType(3));
576 EXPECT_EQ(45, s.ToInt32(call_instr->InputAt(10)));
577 EXPECT_EQ(kMachInt32, desc_before->GetType(4));
580 EXPECT_EQ(s.ToVreg(function_node), s.ToVreg(call_instr->InputAt(11)));
582 EXPECT_EQ(s.ToVreg(context2), s.ToVreg(call_instr->InputAt(12)));
585 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
586 EXPECT_EQ(index, s.size());
589 } // namespace compiler
590 } // namespace internal