blob: 69ae7684c4816019b99d8cfcd2ea78f9d23aa0bd [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Emily Bernier958fae72015-03-24 16:35:39 -04005#include "test/unittests/compiler/instruction-selector-unittest.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006
Ben Murdoch014dc512016-03-22 12:00:34 +00007#include "src/code-factory.h"
8#include "src/compiler/graph.h"
9#include "src/compiler/schedule.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/flags.h"
Emily Bernier958fae72015-03-24 16:35:39 -040011#include "test/unittests/compiler/compiler-test-utils.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012
13namespace v8 {
14namespace internal {
15namespace compiler {
16
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017
18InstructionSelectorTest::InstructionSelectorTest() : rng_(FLAG_random_seed) {}
19
20
21InstructionSelectorTest::~InstructionSelectorTest() {}
22
23
24InstructionSelectorTest::Stream InstructionSelectorTest::StreamBuilder::Build(
25 InstructionSelector::Features features,
Ben Murdoch014dc512016-03-22 12:00:34 +000026 InstructionSelectorTest::StreamBuilderMode mode,
27 InstructionSelector::SourcePositionMode source_position_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028 Schedule* schedule = Export();
29 if (FLAG_trace_turbo) {
30 OFStream out(stdout);
Emily Bernier958fae72015-03-24 16:35:39 -040031 out << "=== Schedule before instruction selection ===" << std::endl
32 << *schedule;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033 }
Ben Murdoch014dc512016-03-22 12:00:34 +000034 size_t const node_count = graph()->NodeCount();
35 EXPECT_NE(0u, node_count);
36 Linkage linkage(call_descriptor());
Emily Bernier958fae72015-03-24 16:35:39 -040037 InstructionBlocks* instruction_blocks =
38 InstructionSequence::InstructionBlocksFor(test_->zone(), schedule);
Ben Murdoch014dc512016-03-22 12:00:34 +000039 InstructionSequence sequence(test_->isolate(), test_->zone(),
40 instruction_blocks);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 SourcePositionTable source_position_table(graph());
Ben Murdoch014dc512016-03-22 12:00:34 +000042 InstructionSelector selector(test_->zone(), node_count, &linkage, &sequence,
Ben Murdoch109988c2016-05-18 11:27:45 +010043 schedule, &source_position_table, nullptr,
Ben Murdoch014dc512016-03-22 12:00:34 +000044 source_position_mode, features);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 selector.SelectInstructions();
46 if (FLAG_trace_turbo) {
47 OFStream out(stdout);
Emily Bernier958fae72015-03-24 16:35:39 -040048 PrintableInstructionSequence printable = {
Ben Murdoch014dc512016-03-22 12:00:34 +000049 RegisterConfiguration::ArchDefault(RegisterConfiguration::TURBOFAN),
50 &sequence};
Emily Bernier958fae72015-03-24 16:35:39 -040051 out << "=== Code sequence after instruction selection ===" << std::endl
52 << printable;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 }
54 Stream s;
Ben Murdoch014dc512016-03-22 12:00:34 +000055 s.virtual_registers_ = selector.GetVirtualRegistersForTesting();
Emily Bernier958fae72015-03-24 16:35:39 -040056 // Map virtual registers.
Ben Murdoch014dc512016-03-22 12:00:34 +000057 for (Instruction* const instr : sequence) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 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()) {
Ben Murdoch014dc512016-03-22 12:00:34 +000077 int vreg = ConstantOperand::cast(output)->virtual_register();
78 s.constants_.insert(std::make_pair(vreg, sequence.GetConstant(vreg)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 }
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()) {
Ben Murdoch014dc512016-03-22 12:00:34 +000085 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 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091 }
92 }
93 s.instructions_.push_back(instr);
94 }
Ben Murdoch014dc512016-03-22 12:00:34 +000095 for (auto i : s.virtual_registers_) {
96 int const virtual_register = i.second;
97 if (sequence.IsFloat(virtual_register)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 EXPECT_FALSE(sequence.IsReference(virtual_register));
99 s.doubles_.insert(virtual_register);
100 }
101 if (sequence.IsReference(virtual_register)) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000102 EXPECT_FALSE(sequence.IsFloat(virtual_register));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 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
Emily Bernier958fae72015-03-24 16:35:39 -0400114int 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
121bool 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;
Ben Murdoch014dc512016-03-22 12:00:34 +0000126 return unallocated->fixed_register_index() == reg.code();
Emily Bernier958fae72015-03-24 16:35:39 -0400127}
128
129
130bool 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
138bool 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
Ben Murdoch014dc512016-03-22 12:00:34 +0000146const FrameStateFunctionInfo*
147InstructionSelectorTest::StreamBuilder::GetFrameStateFunctionInfo(
148 int parameter_count, int local_count) {
149 return common()->CreateFrameStateFunctionInfo(
150 FrameStateType::kJavaScriptFunction, parameter_count, local_count,
Ben Murdoch109988c2016-05-18 11:27:45 +0100151 Handle<SharedFunctionInfo>());
Ben Murdoch014dc512016-03-22 12:00:34 +0000152}
153
154
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155// -----------------------------------------------------------------------------
156// Return.
157
158
Emily Bernier958fae72015-03-24 16:35:39 -0400159TARGET_TEST_F(InstructionSelectorTest, ReturnFloat32Constant) {
160 const float kValue = 4.2f;
Ben Murdoch014dc512016-03-22 12:00:34 +0000161 StreamBuilder m(this, MachineType::Float32());
Emily Bernier958fae72015-03-24 16:35:39 -0400162 m.Return(m.Float32Constant(kValue));
163 Stream s = m.Build(kAllInstructions);
Ben Murdoch014dc512016-03-22 12:00:34 +0000164 ASSERT_EQ(3U, s.size());
Emily Bernier958fae72015-03-24 16:35:39 -0400165 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173TARGET_TEST_F(InstructionSelectorTest, ReturnParameter) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000174 StreamBuilder m(this, MachineType::Int32(), MachineType::Int32());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175 m.Return(m.Parameter(0));
176 Stream s = m.Build(kAllInstructions);
Ben Murdoch014dc512016-03-22 12:00:34 +0000177 ASSERT_EQ(3U, s.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 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
185TARGET_TEST_F(InstructionSelectorTest, ReturnZero) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000186 StreamBuilder m(this, MachineType::Int32());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 m.Return(m.Int32Constant(0));
188 Stream s = m.Build(kAllInstructions);
Ben Murdoch014dc512016-03-22 12:00:34 +0000189 ASSERT_EQ(3U, s.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 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
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100202TARGET_TEST_F(InstructionSelectorTest, TruncateFloat64ToWord32WithParameter) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000203 StreamBuilder m(this, MachineType::Int32(), MachineType::Float64());
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100204 m.Return(m.TruncateFloat64ToWord32(m.Parameter(0)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 Stream s = m.Build(kAllInstructions);
Ben Murdoch014dc512016-03-22 12:00:34 +0000206 ASSERT_EQ(4U, s.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
208 EXPECT_EQ(kArchTruncateDoubleToI, s[1]->arch_opcode());
209 EXPECT_EQ(1U, s[1]->InputCount());
210 EXPECT_EQ(1U, s[1]->OutputCount());
211 EXPECT_EQ(kArchRet, s[2]->arch_opcode());
212}
213
214
215// -----------------------------------------------------------------------------
216// Parameters.
217
218
219TARGET_TEST_F(InstructionSelectorTest, DoubleParameter) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000220 StreamBuilder m(this, MachineType::Float64(), MachineType::Float64());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 Node* param = m.Parameter(0);
222 m.Return(param);
223 Stream s = m.Build(kAllInstructions);
Emily Bernier958fae72015-03-24 16:35:39 -0400224 EXPECT_TRUE(s.IsDouble(param));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225}
226
227
228TARGET_TEST_F(InstructionSelectorTest, ReferenceParameter) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000229 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 Node* param = m.Parameter(0);
231 m.Return(param);
232 Stream s = m.Build(kAllInstructions);
Emily Bernier958fae72015-03-24 16:35:39 -0400233 EXPECT_TRUE(s.IsReference(param));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234}
235
236
237// -----------------------------------------------------------------------------
Ben Murdoch014dc512016-03-22 12:00:34 +0000238// FinishRegion.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239
240
Ben Murdoch014dc512016-03-22 12:00:34 +0000241TARGET_TEST_F(InstructionSelectorTest, FinishRegion) {
242 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243 Node* param = m.Parameter(0);
Ben Murdoch014dc512016-03-22 12:00:34 +0000244 Node* finish =
245 m.AddNode(m.common()->FinishRegion(), param, m.graph()->start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 m.Return(finish);
247 Stream s = m.Build(kAllInstructions);
Ben Murdoch014dc512016-03-22 12:00:34 +0000248 ASSERT_EQ(4U, s.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
250 ASSERT_EQ(1U, s[0]->OutputCount());
251 ASSERT_TRUE(s[0]->Output()->IsUnallocated());
Emily Bernier958fae72015-03-24 16:35:39 -0400252 EXPECT_EQ(s.ToVreg(param), s.ToVreg(s[0]->Output()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 EXPECT_EQ(kArchNop, s[1]->arch_opcode());
254 ASSERT_EQ(1U, s[1]->InputCount());
255 ASSERT_TRUE(s[1]->InputAt(0)->IsUnallocated());
Emily Bernier958fae72015-03-24 16:35:39 -0400256 EXPECT_EQ(s.ToVreg(param), s.ToVreg(s[1]->InputAt(0)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 ASSERT_EQ(1U, s[1]->OutputCount());
258 ASSERT_TRUE(s[1]->Output()->IsUnallocated());
259 EXPECT_TRUE(UnallocatedOperand::cast(s[1]->Output())->HasSameAsInputPolicy());
Emily Bernier958fae72015-03-24 16:35:39 -0400260 EXPECT_EQ(s.ToVreg(finish), s.ToVreg(s[1]->Output()));
261 EXPECT_TRUE(s.IsReference(finish));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262}
263
264
265// -----------------------------------------------------------------------------
266// Phi.
267
268
269typedef InstructionSelectorTestWithParam<MachineType>
270 InstructionSelectorPhiTest;
271
272
273TARGET_TEST_P(InstructionSelectorPhiTest, Doubleness) {
274 const MachineType type = GetParam();
275 StreamBuilder m(this, type, type, type);
276 Node* param0 = m.Parameter(0);
277 Node* param1 = m.Parameter(1);
Ben Murdoch014dc512016-03-22 12:00:34 +0000278 RawMachineLabel a, b, c;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 m.Branch(m.Int32Constant(0), &a, &b);
280 m.Bind(&a);
281 m.Goto(&c);
282 m.Bind(&b);
283 m.Goto(&c);
284 m.Bind(&c);
Ben Murdoch014dc512016-03-22 12:00:34 +0000285 Node* phi = m.Phi(type.representation(), param0, param1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 m.Return(phi);
287 Stream s = m.Build(kAllInstructions);
Emily Bernier958fae72015-03-24 16:35:39 -0400288 EXPECT_EQ(s.IsDouble(phi), s.IsDouble(param0));
289 EXPECT_EQ(s.IsDouble(phi), s.IsDouble(param1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290}
291
292
293TARGET_TEST_P(InstructionSelectorPhiTest, Referenceness) {
294 const MachineType type = GetParam();
295 StreamBuilder m(this, type, type, type);
296 Node* param0 = m.Parameter(0);
297 Node* param1 = m.Parameter(1);
Ben Murdoch014dc512016-03-22 12:00:34 +0000298 RawMachineLabel a, b, c;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 m.Branch(m.Int32Constant(1), &a, &b);
300 m.Bind(&a);
301 m.Goto(&c);
302 m.Bind(&b);
303 m.Goto(&c);
304 m.Bind(&c);
Ben Murdoch014dc512016-03-22 12:00:34 +0000305 Node* phi = m.Phi(type.representation(), param0, param1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 m.Return(phi);
307 Stream s = m.Build(kAllInstructions);
Emily Bernier958fae72015-03-24 16:35:39 -0400308 EXPECT_EQ(s.IsReference(phi), s.IsReference(param0));
309 EXPECT_EQ(s.IsReference(phi), s.IsReference(param1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310}
311
312
Ben Murdoch014dc512016-03-22 12:00:34 +0000313INSTANTIATE_TEST_CASE_P(
314 InstructionSelectorTest, InstructionSelectorPhiTest,
315 ::testing::Values(MachineType::Float64(), MachineType::Int8(),
316 MachineType::Uint8(), MachineType::Int16(),
317 MachineType::Uint16(), MachineType::Int32(),
318 MachineType::Uint32(), MachineType::Int64(),
319 MachineType::Uint64(), MachineType::Pointer(),
320 MachineType::AnyTagged()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321
322
323// -----------------------------------------------------------------------------
324// ValueEffect.
325
326
327TARGET_TEST_F(InstructionSelectorTest, ValueEffect) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000328 StreamBuilder m1(this, MachineType::Int32(), MachineType::Pointer());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 Node* p1 = m1.Parameter(0);
Ben Murdoch014dc512016-03-22 12:00:34 +0000330 m1.Return(m1.Load(MachineType::Int32(), p1, m1.Int32Constant(0)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331 Stream s1 = m1.Build(kAllInstructions);
Ben Murdoch014dc512016-03-22 12:00:34 +0000332 StreamBuilder m2(this, MachineType::Int32(), MachineType::Pointer());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333 Node* p2 = m2.Parameter(0);
Ben Murdoch014dc512016-03-22 12:00:34 +0000334 m2.Return(m2.AddNode(
335 m2.machine()->Load(MachineType::Int32()), p2, m2.Int32Constant(0),
336 m2.AddNode(m2.common()->BeginRegion(), m2.graph()->start())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337 Stream s2 = m2.Build(kAllInstructions);
338 EXPECT_LE(3U, s1.size());
339 ASSERT_EQ(s1.size(), s2.size());
340 TRACED_FORRANGE(size_t, i, 0, s1.size() - 1) {
341 const Instruction* i1 = s1[i];
342 const Instruction* i2 = s2[i];
343 EXPECT_EQ(i1->arch_opcode(), i2->arch_opcode());
344 EXPECT_EQ(i1->InputCount(), i2->InputCount());
345 EXPECT_EQ(i1->OutputCount(), i2->OutputCount());
346 }
347}
348
349
350// -----------------------------------------------------------------------------
351// Calls with deoptimization.
Emily Bernier958fae72015-03-24 16:35:39 -0400352
353
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354TARGET_TEST_F(InstructionSelectorTest, CallJSFunctionWithDeopt) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000355 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged(),
356 MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357
358 BailoutId bailout_id(42);
359
360 Node* function_node = m.Parameter(0);
361 Node* receiver = m.Parameter(1);
362 Node* context = m.Parameter(2);
363
Ben Murdoch014dc512016-03-22 12:00:34 +0000364 ZoneVector<MachineType> int32_type(1, MachineType::Int32(), zone());
365 ZoneVector<MachineType> empty_types(zone());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000366
Ben Murdoch014dc512016-03-22 12:00:34 +0000367 CallDescriptor* descriptor = Linkage::GetJSCallDescriptor(
368 zone(), false, 1, CallDescriptor::kNeedsFrameState);
369
370 // Build frame state for the state before the call.
371 Node* parameters =
372 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(1));
373 Node* locals = m.AddNode(m.common()->TypedStateValues(&empty_types));
374 Node* stack = m.AddNode(m.common()->TypedStateValues(&empty_types));
375 Node* context_sentinel = m.Int32Constant(0);
376 Node* state_node = m.AddNode(
377 m.common()->FrameState(bailout_id, OutputFrameStateCombine::Push(),
378 m.GetFrameStateFunctionInfo(1, 0)),
379 parameters, locals, stack, context_sentinel, function_node,
380 m.UndefinedConstant());
381
382 // Build the call.
383 Node* args[] = {receiver, m.UndefinedConstant(), m.Int32Constant(1), context};
384 Node* call =
385 m.CallNWithFrameState(descriptor, function_node, args, state_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 m.Return(call);
387
388 Stream s = m.Build(kAllExceptNopInstructions);
389
390 // Skip until kArchCallJSFunction.
391 size_t index = 0;
392 for (; index < s.size() && s[index]->arch_opcode() != kArchCallJSFunction;
393 index++) {
394 }
395 // Now we should have two instructions: call and return.
396 ASSERT_EQ(index + 2, s.size());
397
398 EXPECT_EQ(kArchCallJSFunction, s[index++]->arch_opcode());
399 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
400
401 // TODO(jarin) Check deoptimization table.
402}
403
404
Ben Murdoch014dc512016-03-22 12:00:34 +0000405TARGET_TEST_F(InstructionSelectorTest, CallStubWithDeopt) {
406 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged(),
407 MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408
409 BailoutId bailout_id_before(42);
410
411 // Some arguments for the call node.
412 Node* function_node = m.Parameter(0);
413 Node* receiver = m.Parameter(1);
414 Node* context = m.Int32Constant(1); // Context is ignored.
415
Ben Murdoch014dc512016-03-22 12:00:34 +0000416 ZoneVector<MachineType> int32_type(1, MachineType::Int32(), zone());
417 ZoneVector<MachineType> float64_type(1, MachineType::Float64(), zone());
418 ZoneVector<MachineType> tagged_type(1, MachineType::AnyTagged(), zone());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000419
Ben Murdoch014dc512016-03-22 12:00:34 +0000420 Callable callable = CodeFactory::ToObject(isolate());
421 CallDescriptor* descriptor = Linkage::GetStubCallDescriptor(
422 isolate(), zone(), callable.descriptor(), 1,
423 CallDescriptor::kNeedsFrameState, Operator::kNoProperties);
424
425 // Build frame state for the state before the call.
426 Node* parameters =
427 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(43));
428 Node* locals = m.AddNode(m.common()->TypedStateValues(&float64_type),
429 m.Float64Constant(0.5));
430 Node* stack = m.AddNode(m.common()->TypedStateValues(&tagged_type),
431 m.UndefinedConstant());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000432 Node* context_sentinel = m.Int32Constant(0);
Ben Murdoch014dc512016-03-22 12:00:34 +0000433 Node* state_node = m.AddNode(
434 m.common()->FrameState(bailout_id_before, OutputFrameStateCombine::Push(),
435 m.GetFrameStateFunctionInfo(1, 1)),
436 parameters, locals, stack, context_sentinel, function_node,
437 m.UndefinedConstant());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438
439 // Build the call.
Ben Murdoch014dc512016-03-22 12:00:34 +0000440 Node* args[] = {function_node, receiver, context};
441 Node* stub_code = m.HeapConstant(callable.code());
442 Node* call = m.CallNWithFrameState(descriptor, stub_code, args, state_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443 m.Return(call);
444
445 Stream s = m.Build(kAllExceptNopInstructions);
446
447 // Skip until kArchCallJSFunction.
448 size_t index = 0;
449 for (; index < s.size() && s[index]->arch_opcode() != kArchCallCodeObject;
450 index++) {
451 }
452 // Now we should have two instructions: call, return.
453 ASSERT_EQ(index + 2, s.size());
454
455 // Check the call instruction
456 const Instruction* call_instr = s[index++];
457 EXPECT_EQ(kArchCallCodeObject, call_instr->arch_opcode());
458 size_t num_operands =
459 1 + // Code object.
460 1 +
Ben Murdoch014dc512016-03-22 12:00:34 +0000461 5 + // Frame state deopt id + one input for each value in frame state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 1 + // Function.
463 1; // Context.
464 ASSERT_EQ(num_operands, call_instr->InputCount());
465
466 // Code object.
467 EXPECT_TRUE(call_instr->InputAt(0)->IsImmediate());
468
469 // Deoptimization id.
470 int32_t deopt_id_before = s.ToInt32(call_instr->InputAt(1));
471 FrameStateDescriptor* desc_before =
472 s.GetFrameStateDescriptor(deopt_id_before);
473 EXPECT_EQ(bailout_id_before, desc_before->bailout_id());
Emily Bernier958fae72015-03-24 16:35:39 -0400474 EXPECT_EQ(OutputFrameStateCombine::kPushOutput,
475 desc_before->state_combine().kind());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476 EXPECT_EQ(1u, desc_before->parameters_count());
477 EXPECT_EQ(1u, desc_before->locals_count());
478 EXPECT_EQ(1u, desc_before->stack_count());
Ben Murdoch014dc512016-03-22 12:00:34 +0000479 EXPECT_EQ(43, s.ToInt32(call_instr->InputAt(3)));
480 EXPECT_EQ(0, s.ToInt32(call_instr->InputAt(4))); // This should be a context.
Emily Bernier958fae72015-03-24 16:35:39 -0400481 // We inserted 0 here.
Ben Murdoch014dc512016-03-22 12:00:34 +0000482 EXPECT_EQ(0.5, s.ToFloat64(call_instr->InputAt(5)));
483 EXPECT_TRUE(s.ToHeapObject(call_instr->InputAt(6))->IsUndefined());
484 EXPECT_EQ(MachineType::AnyTagged(),
485 desc_before->GetType(0)); // function is always
486 // tagged/any.
487 EXPECT_EQ(MachineType::Int32(), desc_before->GetType(1));
488 EXPECT_EQ(MachineType::AnyTagged(),
489 desc_before->GetType(2)); // context is always
490 // tagged/any.
491 EXPECT_EQ(MachineType::Float64(), desc_before->GetType(3));
492 EXPECT_EQ(MachineType::AnyTagged(), desc_before->GetType(4));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000493
494 // Function.
Ben Murdoch014dc512016-03-22 12:00:34 +0000495 EXPECT_EQ(s.ToVreg(function_node), s.ToVreg(call_instr->InputAt(7)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000496 // Context.
Ben Murdoch014dc512016-03-22 12:00:34 +0000497 EXPECT_EQ(s.ToVreg(context), s.ToVreg(call_instr->InputAt(8)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000498
499 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
500
501 EXPECT_EQ(index, s.size());
502}
503
504
Ben Murdoch014dc512016-03-22 12:00:34 +0000505TARGET_TEST_F(InstructionSelectorTest, CallStubWithDeoptRecursiveFrameState) {
506 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged(),
507 MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508
509 BailoutId bailout_id_before(42);
510 BailoutId bailout_id_parent(62);
511
512 // Some arguments for the call node.
513 Node* function_node = m.Parameter(0);
514 Node* receiver = m.Parameter(1);
515 Node* context = m.Int32Constant(66);
Ben Murdoch014dc512016-03-22 12:00:34 +0000516 Node* context2 = m.Int32Constant(46);
517
518 ZoneVector<MachineType> int32_type(1, MachineType::Int32(), zone());
519 ZoneVector<MachineType> int32x2_type(2, MachineType::Int32(), zone());
520 ZoneVector<MachineType> float64_type(1, MachineType::Float64(), zone());
521
522 Callable callable = CodeFactory::ToObject(isolate());
523 CallDescriptor* descriptor = Linkage::GetStubCallDescriptor(
524 isolate(), zone(), callable.descriptor(), 1,
525 CallDescriptor::kNeedsFrameState, Operator::kNoProperties);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000526
527 // Build frame state for the state before the call.
Ben Murdoch014dc512016-03-22 12:00:34 +0000528 Node* parameters =
529 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(63));
530 Node* locals =
531 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(64));
532 Node* stack =
533 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(65));
534 Node* frame_state_parent = m.AddNode(
535 m.common()->FrameState(bailout_id_parent,
536 OutputFrameStateCombine::Ignore(),
537 m.GetFrameStateFunctionInfo(1, 1)),
538 parameters, locals, stack, context, function_node, m.UndefinedConstant());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000540 Node* parameters2 =
Ben Murdoch014dc512016-03-22 12:00:34 +0000541 m.AddNode(m.common()->TypedStateValues(&int32_type), m.Int32Constant(43));
542 Node* locals2 = m.AddNode(m.common()->TypedStateValues(&float64_type),
543 m.Float64Constant(0.25));
544 Node* stack2 = m.AddNode(m.common()->TypedStateValues(&int32x2_type),
545 m.Int32Constant(44), m.Int32Constant(45));
546 Node* state_node = m.AddNode(
547 m.common()->FrameState(bailout_id_before, OutputFrameStateCombine::Push(),
548 m.GetFrameStateFunctionInfo(1, 1)),
549 parameters2, locals2, stack2, context2, function_node,
550 frame_state_parent);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000551
552 // Build the call.
Ben Murdoch014dc512016-03-22 12:00:34 +0000553 Node* args[] = {function_node, receiver, context2};
554 Node* stub_code = m.HeapConstant(callable.code());
555 Node* call = m.CallNWithFrameState(descriptor, stub_code, args, state_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556 m.Return(call);
557
558 Stream s = m.Build(kAllExceptNopInstructions);
559
560 // Skip until kArchCallJSFunction.
561 size_t index = 0;
562 for (; index < s.size() && s[index]->arch_opcode() != kArchCallCodeObject;
563 index++) {
564 }
565 // Now we should have three instructions: call, return.
566 EXPECT_EQ(index + 2, s.size());
567
568 // Check the call instruction
569 const Instruction* call_instr = s[index++];
570 EXPECT_EQ(kArchCallCodeObject, call_instr->arch_opcode());
571 size_t num_operands =
572 1 + // Code object.
573 1 + // Frame state deopt id
Ben Murdoch014dc512016-03-22 12:00:34 +0000574 6 + // One input for each value in frame state + context.
575 5 + // One input for each value in the parent frame state + context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000576 1 + // Function.
577 1; // Context.
578 EXPECT_EQ(num_operands, call_instr->InputCount());
579 // Code object.
580 EXPECT_TRUE(call_instr->InputAt(0)->IsImmediate());
581
582 // Deoptimization id.
583 int32_t deopt_id_before = s.ToInt32(call_instr->InputAt(1));
584 FrameStateDescriptor* desc_before =
585 s.GetFrameStateDescriptor(deopt_id_before);
Emily Bernier958fae72015-03-24 16:35:39 -0400586 FrameStateDescriptor* desc_before_outer = desc_before->outer_state();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000587 EXPECT_EQ(bailout_id_before, desc_before->bailout_id());
Emily Bernier958fae72015-03-24 16:35:39 -0400588 EXPECT_EQ(1u, desc_before_outer->parameters_count());
589 EXPECT_EQ(1u, desc_before_outer->locals_count());
590 EXPECT_EQ(1u, desc_before_outer->stack_count());
591 // Values from parent environment.
Ben Murdoch014dc512016-03-22 12:00:34 +0000592 EXPECT_EQ(MachineType::AnyTagged(), desc_before->GetType(0));
593 EXPECT_EQ(63, s.ToInt32(call_instr->InputAt(3)));
594 EXPECT_EQ(MachineType::Int32(), desc_before_outer->GetType(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000595 // Context:
Ben Murdoch014dc512016-03-22 12:00:34 +0000596 EXPECT_EQ(66, s.ToInt32(call_instr->InputAt(4)));
597 EXPECT_EQ(MachineType::AnyTagged(), desc_before_outer->GetType(2));
598 EXPECT_EQ(64, s.ToInt32(call_instr->InputAt(5)));
599 EXPECT_EQ(MachineType::Int32(), desc_before_outer->GetType(3));
600 EXPECT_EQ(65, s.ToInt32(call_instr->InputAt(6)));
601 EXPECT_EQ(MachineType::Int32(), desc_before_outer->GetType(4));
Emily Bernier958fae72015-03-24 16:35:39 -0400602 // Values from the nested frame.
603 EXPECT_EQ(1u, desc_before->parameters_count());
604 EXPECT_EQ(1u, desc_before->locals_count());
605 EXPECT_EQ(2u, desc_before->stack_count());
Ben Murdoch014dc512016-03-22 12:00:34 +0000606 EXPECT_EQ(MachineType::AnyTagged(), desc_before->GetType(0));
607 EXPECT_EQ(43, s.ToInt32(call_instr->InputAt(8)));
608 EXPECT_EQ(MachineType::Int32(), desc_before->GetType(1));
609 EXPECT_EQ(46, s.ToInt32(call_instr->InputAt(9)));
610 EXPECT_EQ(MachineType::AnyTagged(), desc_before->GetType(2));
611 EXPECT_EQ(0.25, s.ToFloat64(call_instr->InputAt(10)));
612 EXPECT_EQ(MachineType::Float64(), desc_before->GetType(3));
613 EXPECT_EQ(44, s.ToInt32(call_instr->InputAt(11)));
614 EXPECT_EQ(MachineType::Int32(), desc_before->GetType(4));
615 EXPECT_EQ(45, s.ToInt32(call_instr->InputAt(12)));
616 EXPECT_EQ(MachineType::Int32(), desc_before->GetType(5));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000617
618 // Function.
Ben Murdoch014dc512016-03-22 12:00:34 +0000619 EXPECT_EQ(s.ToVreg(function_node), s.ToVreg(call_instr->InputAt(13)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000620 // Context.
Ben Murdoch014dc512016-03-22 12:00:34 +0000621 EXPECT_EQ(s.ToVreg(context2), s.ToVreg(call_instr->InputAt(14)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622 // Continuation.
623
624 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
625 EXPECT_EQ(index, s.size());
626}
627
628} // namespace compiler
629} // namespace internal
630} // namespace v8