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 "test/unittests/compiler/instruction-selector-unittest.h"
6
7 #include "src/code-factory.h"
8 #include "src/compiler/graph.h"
9 #include "src/compiler/schedule.h"
10 #include "src/flags.h"
11 #include "test/unittests/compiler/compiler-test-utils.h"
12
13 namespace v8 {
14 namespace internal {
15 namespace compiler {
16
17
InstructionSelectorTest()18 InstructionSelectorTest::InstructionSelectorTest() : rng_(FLAG_random_seed) {}
19
20
~InstructionSelectorTest()21 InstructionSelectorTest::~InstructionSelectorTest() {}
22
23
Build(InstructionSelector::Features features,InstructionSelectorTest::StreamBuilderMode mode,InstructionSelector::SourcePositionMode source_position_mode)24 InstructionSelectorTest::Stream InstructionSelectorTest::StreamBuilder::Build(
25 InstructionSelector::Features features,
26 InstructionSelectorTest::StreamBuilderMode mode,
27 InstructionSelector::SourcePositionMode source_position_mode) {
28 Schedule* schedule = Export();
29 if (FLAG_trace_turbo) {
30 OFStream out(stdout);
31 out << "=== Schedule before instruction selection ===" << std::endl
32 << *schedule;
33 }
34 size_t const node_count = graph()->NodeCount();
35 EXPECT_NE(0u, node_count);
36 Linkage linkage(call_descriptor());
37 InstructionBlocks* instruction_blocks =
38 InstructionSequence::InstructionBlocksFor(test_->zone(), schedule);
39 InstructionSequence sequence(test_->isolate(), test_->zone(),
40 instruction_blocks);
41 SourcePositionTable source_position_table(graph());
42 InstructionSelector selector(test_->zone(), node_count, &linkage, &sequence,
43 schedule, &source_position_table,
44 source_position_mode, features);
45 selector.SelectInstructions();
46 if (FLAG_trace_turbo) {
47 OFStream out(stdout);
48 PrintableInstructionSequence printable = {
49 RegisterConfiguration::ArchDefault(RegisterConfiguration::TURBOFAN),
50 &sequence};
51 out << "=== Code sequence after instruction selection ===" << std::endl
52 << printable;
53 }
54 Stream s;
55 s.virtual_registers_ = selector.GetVirtualRegistersForTesting();
56 // Map virtual registers.
57 for (Instruction* const instr : sequence) {
58 if (instr->opcode() < 0) continue;
59 if (mode == kTargetInstructions) {
60 switch (instr->arch_opcode()) {
61 #define CASE(Name) \
62 case k##Name: \
63 break;
64 TARGET_ARCH_OPCODE_LIST(CASE)
65 #undef CASE
66 default:
67 continue;
68 }
69 }
70 if (mode == kAllExceptNopInstructions && instr->arch_opcode() == kArchNop) {
71 continue;
72 }
73 for (size_t i = 0; i < instr->OutputCount(); ++i) {
74 InstructionOperand* output = instr->OutputAt(i);
75 EXPECT_NE(InstructionOperand::IMMEDIATE, output->kind());
76 if (output->IsConstant()) {
77 int vreg = ConstantOperand::cast(output)->virtual_register();
78 s.constants_.insert(std::make_pair(vreg, sequence.GetConstant(vreg)));
79 }
80 }
81 for (size_t i = 0; i < instr->InputCount(); ++i) {
82 InstructionOperand* input = instr->InputAt(i);
83 EXPECT_NE(InstructionOperand::CONSTANT, input->kind());
84 if (input->IsImmediate()) {
85 auto imm = ImmediateOperand::cast(input);
86 if (imm->type() == ImmediateOperand::INDEXED) {
87 int index = imm->indexed_value();
88 s.immediates_.insert(
89 std::make_pair(index, sequence.GetImmediate(imm)));
90 }
91 }
92 }
93 s.instructions_.push_back(instr);
94 }
95 for (auto i : s.virtual_registers_) {
96 int const virtual_register = i.second;
97 if (sequence.IsFloat(virtual_register)) {
98 EXPECT_FALSE(sequence.IsReference(virtual_register));
99 s.doubles_.insert(virtual_register);
100 }
101 if (sequence.IsReference(virtual_register)) {
102 EXPECT_FALSE(sequence.IsFloat(virtual_register));
103 s.references_.insert(virtual_register);
104 }
105 }
106 for (int i = 0; i < sequence.GetFrameStateDescriptorCount(); i++) {
107 s.deoptimization_entries_.push_back(sequence.GetFrameStateDescriptor(
108 InstructionSequence::StateId::FromInt(i)));
109 }
110 return s;
111 }
112
113
ToVreg(const Node * node) const114 int InstructionSelectorTest::Stream::ToVreg(const Node* node) const {
115 VirtualRegisters::const_iterator i = virtual_registers_.find(node->id());
116 CHECK(i != virtual_registers_.end());
117 return i->second;
118 }
119
120
IsFixed(const InstructionOperand * operand,Register reg) const121 bool InstructionSelectorTest::Stream::IsFixed(const InstructionOperand* operand,
122 Register reg) const {
123 if (!operand->IsUnallocated()) return false;
124 const UnallocatedOperand* unallocated = UnallocatedOperand::cast(operand);
125 if (!unallocated->HasFixedRegisterPolicy()) return false;
126 return unallocated->fixed_register_index() == reg.code();
127 }
128
129
IsSameAsFirst(const InstructionOperand * operand) const130 bool InstructionSelectorTest::Stream::IsSameAsFirst(
131 const InstructionOperand* operand) const {
132 if (!operand->IsUnallocated()) return false;
133 const UnallocatedOperand* unallocated = UnallocatedOperand::cast(operand);
134 return unallocated->HasSameAsInputPolicy();
135 }
136
137
IsUsedAtStart(const InstructionOperand * operand) const138 bool InstructionSelectorTest::Stream::IsUsedAtStart(
139 const InstructionOperand* operand) const {
140 if (!operand->IsUnallocated()) return false;
141 const UnallocatedOperand* unallocated = UnallocatedOperand::cast(operand);
142 return unallocated->IsUsedAtStart();
143 }
144
145
146 const FrameStateFunctionInfo*
GetFrameStateFunctionInfo(int parameter_count,int local_count)147 InstructionSelectorTest::StreamBuilder::GetFrameStateFunctionInfo(
148 int parameter_count, int local_count) {
149 return common()->CreateFrameStateFunctionInfo(
150 FrameStateType::kJavaScriptFunction, parameter_count, local_count,
151 Handle<SharedFunctionInfo>(), CALL_MAINTAINS_NATIVE_CONTEXT);
152 }
153
154
155 // -----------------------------------------------------------------------------
156 // Return.
157
158
TARGET_TEST_F(InstructionSelectorTest,ReturnFloat32Constant)159 TARGET_TEST_F(InstructionSelectorTest, ReturnFloat32Constant) {
160 const float kValue = 4.2f;
161 StreamBuilder m(this, MachineType::Float32());
162 m.Return(m.Float32Constant(kValue));
163 Stream s = m.Build(kAllInstructions);
164 ASSERT_EQ(3U, s.size());
165 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
166 ASSERT_EQ(InstructionOperand::CONSTANT, s[0]->OutputAt(0)->kind());
167 EXPECT_FLOAT_EQ(kValue, s.ToFloat32(s[0]->OutputAt(0)));
168 EXPECT_EQ(kArchRet, s[1]->arch_opcode());
169 EXPECT_EQ(1U, s[1]->InputCount());
170 }
171
172
TARGET_TEST_F(InstructionSelectorTest,ReturnParameter)173 TARGET_TEST_F(InstructionSelectorTest, ReturnParameter) {
174 StreamBuilder m(this, MachineType::Int32(), MachineType::Int32());
175 m.Return(m.Parameter(0));
176 Stream s = m.Build(kAllInstructions);
177 ASSERT_EQ(3U, s.size());
178 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
179 ASSERT_EQ(1U, s[0]->OutputCount());
180 EXPECT_EQ(kArchRet, s[1]->arch_opcode());
181 EXPECT_EQ(1U, s[1]->InputCount());
182 }
183
184
TARGET_TEST_F(InstructionSelectorTest,ReturnZero)185 TARGET_TEST_F(InstructionSelectorTest, ReturnZero) {
186 StreamBuilder m(this, MachineType::Int32());
187 m.Return(m.Int32Constant(0));
188 Stream s = m.Build(kAllInstructions);
189 ASSERT_EQ(3U, s.size());
190 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
191 ASSERT_EQ(1U, s[0]->OutputCount());
192 EXPECT_EQ(InstructionOperand::CONSTANT, s[0]->OutputAt(0)->kind());
193 EXPECT_EQ(0, s.ToInt32(s[0]->OutputAt(0)));
194 EXPECT_EQ(kArchRet, s[1]->arch_opcode());
195 EXPECT_EQ(1U, s[1]->InputCount());
196 }
197
198
199 // -----------------------------------------------------------------------------
200 // Conversions.
201
202
TARGET_TEST_F(InstructionSelectorTest,TruncateFloat64ToInt32WithParameter)203 TARGET_TEST_F(InstructionSelectorTest, TruncateFloat64ToInt32WithParameter) {
204 StreamBuilder m(this, MachineType::Int32(), MachineType::Float64());
205 m.Return(
206 m.TruncateFloat64ToInt32(TruncationMode::kJavaScript, m.Parameter(0)));
207 Stream s = m.Build(kAllInstructions);
208 ASSERT_EQ(4U, s.size());
209 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
210 EXPECT_EQ(kArchTruncateDoubleToI, s[1]->arch_opcode());
211 EXPECT_EQ(1U, s[1]->InputCount());
212 EXPECT_EQ(1U, s[1]->OutputCount());
213 EXPECT_EQ(kArchRet, s[2]->arch_opcode());
214 }
215
216
217 // -----------------------------------------------------------------------------
218 // Parameters.
219
220
TARGET_TEST_F(InstructionSelectorTest,DoubleParameter)221 TARGET_TEST_F(InstructionSelectorTest, DoubleParameter) {
222 StreamBuilder m(this, MachineType::Float64(), MachineType::Float64());
223 Node* param = m.Parameter(0);
224 m.Return(param);
225 Stream s = m.Build(kAllInstructions);
226 EXPECT_TRUE(s.IsDouble(param));
227 }
228
229
TARGET_TEST_F(InstructionSelectorTest,ReferenceParameter)230 TARGET_TEST_F(InstructionSelectorTest, ReferenceParameter) {
231 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged());
232 Node* param = m.Parameter(0);
233 m.Return(param);
234 Stream s = m.Build(kAllInstructions);
235 EXPECT_TRUE(s.IsReference(param));
236 }
237
238
239 // -----------------------------------------------------------------------------
240 // FinishRegion.
241
242
TARGET_TEST_F(InstructionSelectorTest,FinishRegion)243 TARGET_TEST_F(InstructionSelectorTest, FinishRegion) {
244 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged());
245 Node* param = m.Parameter(0);
246 Node* finish =
247 m.AddNode(m.common()->FinishRegion(), param, m.graph()->start());
248 m.Return(finish);
249 Stream s = m.Build(kAllInstructions);
250 ASSERT_EQ(4U, s.size());
251 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
252 ASSERT_EQ(1U, s[0]->OutputCount());
253 ASSERT_TRUE(s[0]->Output()->IsUnallocated());
254 EXPECT_EQ(s.ToVreg(param), s.ToVreg(s[0]->Output()));
255 EXPECT_EQ(kArchNop, s[1]->arch_opcode());
256 ASSERT_EQ(1U, s[1]->InputCount());
257 ASSERT_TRUE(s[1]->InputAt(0)->IsUnallocated());
258 EXPECT_EQ(s.ToVreg(param), s.ToVreg(s[1]->InputAt(0)));
259 ASSERT_EQ(1U, s[1]->OutputCount());
260 ASSERT_TRUE(s[1]->Output()->IsUnallocated());
261 EXPECT_TRUE(UnallocatedOperand::cast(s[1]->Output())->HasSameAsInputPolicy());
262 EXPECT_EQ(s.ToVreg(finish), s.ToVreg(s[1]->Output()));
263 EXPECT_TRUE(s.IsReference(finish));
264 }
265
266
267 // -----------------------------------------------------------------------------
268 // Phi.
269
270
271 typedef InstructionSelectorTestWithParam<MachineType>
272 InstructionSelectorPhiTest;
273
274
TARGET_TEST_P(InstructionSelectorPhiTest,Doubleness)275 TARGET_TEST_P(InstructionSelectorPhiTest, Doubleness) {
276 const MachineType type = GetParam();
277 StreamBuilder m(this, type, type, type);
278 Node* param0 = m.Parameter(0);
279 Node* param1 = m.Parameter(1);
280 RawMachineLabel a, b, c;
281 m.Branch(m.Int32Constant(0), &a, &b);
282 m.Bind(&a);
283 m.Goto(&c);
284 m.Bind(&b);
285 m.Goto(&c);
286 m.Bind(&c);
287 Node* phi = m.Phi(type.representation(), param0, param1);
288 m.Return(phi);
289 Stream s = m.Build(kAllInstructions);
290 EXPECT_EQ(s.IsDouble(phi), s.IsDouble(param0));
291 EXPECT_EQ(s.IsDouble(phi), s.IsDouble(param1));
292 }
293
294
TARGET_TEST_P(InstructionSelectorPhiTest,Referenceness)295 TARGET_TEST_P(InstructionSelectorPhiTest, Referenceness) {
296 const MachineType type = GetParam();
297 StreamBuilder m(this, type, type, type);
298 Node* param0 = m.Parameter(0);
299 Node* param1 = m.Parameter(1);
300 RawMachineLabel a, b, c;
301 m.Branch(m.Int32Constant(1), &a, &b);
302 m.Bind(&a);
303 m.Goto(&c);
304 m.Bind(&b);
305 m.Goto(&c);
306 m.Bind(&c);
307 Node* phi = m.Phi(type.representation(), param0, param1);
308 m.Return(phi);
309 Stream s = m.Build(kAllInstructions);
310 EXPECT_EQ(s.IsReference(phi), s.IsReference(param0));
311 EXPECT_EQ(s.IsReference(phi), s.IsReference(param1));
312 }
313
314
315 INSTANTIATE_TEST_CASE_P(
316 InstructionSelectorTest, InstructionSelectorPhiTest,
317 ::testing::Values(MachineType::Float64(), MachineType::Int8(),
318 MachineType::Uint8(), MachineType::Int16(),
319 MachineType::Uint16(), MachineType::Int32(),
320 MachineType::Uint32(), MachineType::Int64(),
321 MachineType::Uint64(), MachineType::Pointer(),
322 MachineType::AnyTagged()));
323
324
325 // -----------------------------------------------------------------------------
326 // ValueEffect.
327
328
TARGET_TEST_F(InstructionSelectorTest,ValueEffect)329 TARGET_TEST_F(InstructionSelectorTest, ValueEffect) {
330 StreamBuilder m1(this, MachineType::Int32(), MachineType::Pointer());
331 Node* p1 = m1.Parameter(0);
332 m1.Return(m1.Load(MachineType::Int32(), p1, m1.Int32Constant(0)));
333 Stream s1 = m1.Build(kAllInstructions);
334 StreamBuilder m2(this, MachineType::Int32(), MachineType::Pointer());
335 Node* p2 = m2.Parameter(0);
336 m2.Return(m2.AddNode(
337 m2.machine()->Load(MachineType::Int32()), p2, m2.Int32Constant(0),
338 m2.AddNode(m2.common()->BeginRegion(), m2.graph()->start())));
339 Stream s2 = m2.Build(kAllInstructions);
340 EXPECT_LE(3U, s1.size());
341 ASSERT_EQ(s1.size(), s2.size());
342 TRACED_FORRANGE(size_t, i, 0, s1.size() - 1) {
343 const Instruction* i1 = s1[i];
344 const Instruction* i2 = s2[i];
345 EXPECT_EQ(i1->arch_opcode(), i2->arch_opcode());
346 EXPECT_EQ(i1->InputCount(), i2->InputCount());
347 EXPECT_EQ(i1->OutputCount(), i2->OutputCount());
348 }
349 }
350
351
352 // -----------------------------------------------------------------------------
353 // Calls with deoptimization.
354
355
TARGET_TEST_F(InstructionSelectorTest,CallJSFunctionWithDeopt)356 TARGET_TEST_F(InstructionSelectorTest, CallJSFunctionWithDeopt) {
357 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged(),
358 MachineType::AnyTagged(), MachineType::AnyTagged());
359
360 BailoutId bailout_id(42);
361
362 Node* function_node = m.Parameter(0);
363 Node* receiver = m.Parameter(1);
364 Node* context = m.Parameter(2);
365
366 ZoneVector<MachineType> int32_type(1, MachineType::Int32(), zone());
367 ZoneVector<MachineType> empty_types(zone());
368
369 CallDescriptor* descriptor = Linkage::GetJSCallDescriptor(
370 zone(), false, 1, CallDescriptor::kNeedsFrameState);
371
372 // Build frame state for the state before the call.
373 Node* parameters =
374 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(1));
375 Node* locals = m.AddNode(m.common()->TypedStateValues(&empty_types));
376 Node* stack = m.AddNode(m.common()->TypedStateValues(&empty_types));
377 Node* context_sentinel = m.Int32Constant(0);
378 Node* state_node = m.AddNode(
379 m.common()->FrameState(bailout_id, OutputFrameStateCombine::Push(),
380 m.GetFrameStateFunctionInfo(1, 0)),
381 parameters, locals, stack, context_sentinel, function_node,
382 m.UndefinedConstant());
383
384 // Build the call.
385 Node* args[] = {receiver, m.UndefinedConstant(), m.Int32Constant(1), context};
386 Node* call =
387 m.CallNWithFrameState(descriptor, function_node, args, state_node);
388 m.Return(call);
389
390 Stream s = m.Build(kAllExceptNopInstructions);
391
392 // Skip until kArchCallJSFunction.
393 size_t index = 0;
394 for (; index < s.size() && s[index]->arch_opcode() != kArchCallJSFunction;
395 index++) {
396 }
397 // Now we should have two instructions: call and return.
398 ASSERT_EQ(index + 2, s.size());
399
400 EXPECT_EQ(kArchCallJSFunction, s[index++]->arch_opcode());
401 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
402
403 // TODO(jarin) Check deoptimization table.
404 }
405
406
TARGET_TEST_F(InstructionSelectorTest,CallStubWithDeopt)407 TARGET_TEST_F(InstructionSelectorTest, CallStubWithDeopt) {
408 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged(),
409 MachineType::AnyTagged(), MachineType::AnyTagged());
410
411 BailoutId bailout_id_before(42);
412
413 // Some arguments for the call node.
414 Node* function_node = m.Parameter(0);
415 Node* receiver = m.Parameter(1);
416 Node* context = m.Int32Constant(1); // Context is ignored.
417
418 ZoneVector<MachineType> int32_type(1, MachineType::Int32(), zone());
419 ZoneVector<MachineType> float64_type(1, MachineType::Float64(), zone());
420 ZoneVector<MachineType> tagged_type(1, MachineType::AnyTagged(), zone());
421
422 Callable callable = CodeFactory::ToObject(isolate());
423 CallDescriptor* descriptor = Linkage::GetStubCallDescriptor(
424 isolate(), zone(), callable.descriptor(), 1,
425 CallDescriptor::kNeedsFrameState, Operator::kNoProperties);
426
427 // Build frame state for the state before the call.
428 Node* parameters =
429 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(43));
430 Node* locals = m.AddNode(m.common()->TypedStateValues(&float64_type),
431 m.Float64Constant(0.5));
432 Node* stack = m.AddNode(m.common()->TypedStateValues(&tagged_type),
433 m.UndefinedConstant());
434 Node* context_sentinel = m.Int32Constant(0);
435 Node* state_node = m.AddNode(
436 m.common()->FrameState(bailout_id_before, OutputFrameStateCombine::Push(),
437 m.GetFrameStateFunctionInfo(1, 1)),
438 parameters, locals, stack, context_sentinel, function_node,
439 m.UndefinedConstant());
440
441 // Build the call.
442 Node* args[] = {function_node, receiver, context};
443 Node* stub_code = m.HeapConstant(callable.code());
444 Node* call = m.CallNWithFrameState(descriptor, stub_code, args, state_node);
445 m.Return(call);
446
447 Stream s = m.Build(kAllExceptNopInstructions);
448
449 // Skip until kArchCallJSFunction.
450 size_t index = 0;
451 for (; index < s.size() && s[index]->arch_opcode() != kArchCallCodeObject;
452 index++) {
453 }
454 // Now we should have two instructions: call, return.
455 ASSERT_EQ(index + 2, s.size());
456
457 // Check the call instruction
458 const Instruction* call_instr = s[index++];
459 EXPECT_EQ(kArchCallCodeObject, call_instr->arch_opcode());
460 size_t num_operands =
461 1 + // Code object.
462 1 +
463 5 + // Frame state deopt id + one input for each value in frame state.
464 1 + // Function.
465 1; // Context.
466 ASSERT_EQ(num_operands, call_instr->InputCount());
467
468 // Code object.
469 EXPECT_TRUE(call_instr->InputAt(0)->IsImmediate());
470
471 // Deoptimization id.
472 int32_t deopt_id_before = s.ToInt32(call_instr->InputAt(1));
473 FrameStateDescriptor* desc_before =
474 s.GetFrameStateDescriptor(deopt_id_before);
475 EXPECT_EQ(bailout_id_before, desc_before->bailout_id());
476 EXPECT_EQ(OutputFrameStateCombine::kPushOutput,
477 desc_before->state_combine().kind());
478 EXPECT_EQ(1u, desc_before->parameters_count());
479 EXPECT_EQ(1u, desc_before->locals_count());
480 EXPECT_EQ(1u, desc_before->stack_count());
481 EXPECT_EQ(43, s.ToInt32(call_instr->InputAt(3)));
482 EXPECT_EQ(0, s.ToInt32(call_instr->InputAt(4))); // This should be a context.
483 // We inserted 0 here.
484 EXPECT_EQ(0.5, s.ToFloat64(call_instr->InputAt(5)));
485 EXPECT_TRUE(s.ToHeapObject(call_instr->InputAt(6))->IsUndefined());
486 EXPECT_EQ(MachineType::AnyTagged(),
487 desc_before->GetType(0)); // function is always
488 // tagged/any.
489 EXPECT_EQ(MachineType::Int32(), desc_before->GetType(1));
490 EXPECT_EQ(MachineType::AnyTagged(),
491 desc_before->GetType(2)); // context is always
492 // tagged/any.
493 EXPECT_EQ(MachineType::Float64(), desc_before->GetType(3));
494 EXPECT_EQ(MachineType::AnyTagged(), desc_before->GetType(4));
495
496 // Function.
497 EXPECT_EQ(s.ToVreg(function_node), s.ToVreg(call_instr->InputAt(7)));
498 // Context.
499 EXPECT_EQ(s.ToVreg(context), s.ToVreg(call_instr->InputAt(8)));
500
501 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
502
503 EXPECT_EQ(index, s.size());
504 }
505
506
TARGET_TEST_F(InstructionSelectorTest,CallStubWithDeoptRecursiveFrameState)507 TARGET_TEST_F(InstructionSelectorTest, CallStubWithDeoptRecursiveFrameState) {
508 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged(),
509 MachineType::AnyTagged(), MachineType::AnyTagged());
510
511 BailoutId bailout_id_before(42);
512 BailoutId bailout_id_parent(62);
513
514 // Some arguments for the call node.
515 Node* function_node = m.Parameter(0);
516 Node* receiver = m.Parameter(1);
517 Node* context = m.Int32Constant(66);
518 Node* context2 = m.Int32Constant(46);
519
520 ZoneVector<MachineType> int32_type(1, MachineType::Int32(), zone());
521 ZoneVector<MachineType> int32x2_type(2, MachineType::Int32(), zone());
522 ZoneVector<MachineType> float64_type(1, MachineType::Float64(), zone());
523
524 Callable callable = CodeFactory::ToObject(isolate());
525 CallDescriptor* descriptor = Linkage::GetStubCallDescriptor(
526 isolate(), zone(), callable.descriptor(), 1,
527 CallDescriptor::kNeedsFrameState, Operator::kNoProperties);
528
529 // Build frame state for the state before the call.
530 Node* parameters =
531 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(63));
532 Node* locals =
533 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(64));
534 Node* stack =
535 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(65));
536 Node* frame_state_parent = m.AddNode(
537 m.common()->FrameState(bailout_id_parent,
538 OutputFrameStateCombine::Ignore(),
539 m.GetFrameStateFunctionInfo(1, 1)),
540 parameters, locals, stack, context, function_node, m.UndefinedConstant());
541
542 Node* parameters2 =
543 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(43));
544 Node* locals2 = m.AddNode(m.common()->TypedStateValues(&float64_type),
545 m.Float64Constant(0.25));
546 Node* stack2 = m.AddNode(m.common()->TypedStateValues(&int32x2_type),
547 m.Int32Constant(44), m.Int32Constant(45));
548 Node* state_node = m.AddNode(
549 m.common()->FrameState(bailout_id_before, OutputFrameStateCombine::Push(),
550 m.GetFrameStateFunctionInfo(1, 1)),
551 parameters2, locals2, stack2, context2, function_node,
552 frame_state_parent);
553
554 // Build the call.
555 Node* args[] = {function_node, receiver, context2};
556 Node* stub_code = m.HeapConstant(callable.code());
557 Node* call = m.CallNWithFrameState(descriptor, stub_code, args, state_node);
558 m.Return(call);
559
560 Stream s = m.Build(kAllExceptNopInstructions);
561
562 // Skip until kArchCallJSFunction.
563 size_t index = 0;
564 for (; index < s.size() && s[index]->arch_opcode() != kArchCallCodeObject;
565 index++) {
566 }
567 // Now we should have three instructions: call, return.
568 EXPECT_EQ(index + 2, s.size());
569
570 // Check the call instruction
571 const Instruction* call_instr = s[index++];
572 EXPECT_EQ(kArchCallCodeObject, call_instr->arch_opcode());
573 size_t num_operands =
574 1 + // Code object.
575 1 + // Frame state deopt id
576 6 + // One input for each value in frame state + context.
577 5 + // One input for each value in the parent frame state + context.
578 1 + // Function.
579 1; // Context.
580 EXPECT_EQ(num_operands, call_instr->InputCount());
581 // Code object.
582 EXPECT_TRUE(call_instr->InputAt(0)->IsImmediate());
583
584 // Deoptimization id.
585 int32_t deopt_id_before = s.ToInt32(call_instr->InputAt(1));
586 FrameStateDescriptor* desc_before =
587 s.GetFrameStateDescriptor(deopt_id_before);
588 FrameStateDescriptor* desc_before_outer = desc_before->outer_state();
589 EXPECT_EQ(bailout_id_before, desc_before->bailout_id());
590 EXPECT_EQ(1u, desc_before_outer->parameters_count());
591 EXPECT_EQ(1u, desc_before_outer->locals_count());
592 EXPECT_EQ(1u, desc_before_outer->stack_count());
593 // Values from parent environment.
594 EXPECT_EQ(MachineType::AnyTagged(), desc_before->GetType(0));
595 EXPECT_EQ(63, s.ToInt32(call_instr->InputAt(3)));
596 EXPECT_EQ(MachineType::Int32(), desc_before_outer->GetType(1));
597 // Context:
598 EXPECT_EQ(66, s.ToInt32(call_instr->InputAt(4)));
599 EXPECT_EQ(MachineType::AnyTagged(), desc_before_outer->GetType(2));
600 EXPECT_EQ(64, s.ToInt32(call_instr->InputAt(5)));
601 EXPECT_EQ(MachineType::Int32(), desc_before_outer->GetType(3));
602 EXPECT_EQ(65, s.ToInt32(call_instr->InputAt(6)));
603 EXPECT_EQ(MachineType::Int32(), desc_before_outer->GetType(4));
604 // Values from the nested frame.
605 EXPECT_EQ(1u, desc_before->parameters_count());
606 EXPECT_EQ(1u, desc_before->locals_count());
607 EXPECT_EQ(2u, desc_before->stack_count());
608 EXPECT_EQ(MachineType::AnyTagged(), desc_before->GetType(0));
609 EXPECT_EQ(43, s.ToInt32(call_instr->InputAt(8)));
610 EXPECT_EQ(MachineType::Int32(), desc_before->GetType(1));
611 EXPECT_EQ(46, s.ToInt32(call_instr->InputAt(9)));
612 EXPECT_EQ(MachineType::AnyTagged(), desc_before->GetType(2));
613 EXPECT_EQ(0.25, s.ToFloat64(call_instr->InputAt(10)));
614 EXPECT_EQ(MachineType::Float64(), desc_before->GetType(3));
615 EXPECT_EQ(44, s.ToInt32(call_instr->InputAt(11)));
616 EXPECT_EQ(MachineType::Int32(), desc_before->GetType(4));
617 EXPECT_EQ(45, s.ToInt32(call_instr->InputAt(12)));
618 EXPECT_EQ(MachineType::Int32(), desc_before->GetType(5));
619
620 // Function.
621 EXPECT_EQ(s.ToVreg(function_node), s.ToVreg(call_instr->InputAt(13)));
622 // Context.
623 EXPECT_EQ(s.ToVreg(context2), s.ToVreg(call_instr->InputAt(14)));
624 // Continuation.
625
626 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
627 EXPECT_EQ(index, s.size());
628 }
629
630 } // namespace compiler
631 } // namespace internal
632 } // namespace v8
633