blob: 16030f80d7867146fb2c90505155347add621861 [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 Bernierd0a1eb72015-03-24 16:35:39 -04005#include "test/unittests/compiler/instruction-selector-unittest.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006
Ben Murdoch4a90d5f2016-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 Bernierd0a1eb72015-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 Murdoch4a90d5f2016-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 Bernierd0a1eb72015-03-24 16:35:39 -040031 out << "=== Schedule before instruction selection ===" << std::endl
32 << *schedule;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033 }
Ben Murdoch4a90d5f2016-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 Bernierd0a1eb72015-03-24 16:35:39 -040037 InstructionBlocks* instruction_blocks =
38 InstructionSequence::InstructionBlocksFor(test_->zone(), schedule);
Ben Murdoch4a90d5f2016-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 Murdoch4a90d5f2016-03-22 12:00:34 +000042 InstructionSelector selector(test_->zone(), node_count, &linkage, &sequence,
Ben Murdoch097c5b22016-05-18 11:27:45 +010043 schedule, &source_position_table, nullptr,
Ben Murdoch4a90d5f2016-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 Bernierd0a1eb72015-03-24 16:35:39 -040048 PrintableInstructionSequence printable = {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049 RegisterConfiguration::ArchDefault(RegisterConfiguration::TURBOFAN),
50 &sequence};
Emily Bernierd0a1eb72015-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 Murdoch4a90d5f2016-03-22 12:00:34 +000055 s.virtual_registers_ = selector.GetVirtualRegistersForTesting();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056 // Map virtual registers.
Ben Murdoch4a90d5f2016-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 Murdoch4a90d5f2016-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 Murdoch4a90d5f2016-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 Murdoch4a90d5f2016-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 Murdoch4a90d5f2016-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 Bernierd0a1eb72015-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 Murdoch4a90d5f2016-03-22 12:00:34 +0000126 return unallocated->fixed_register_index() == reg.code();
Emily Bernierd0a1eb72015-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 Murdoch4a90d5f2016-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 Murdoch097c5b22016-05-18 11:27:45 +0100151 Handle<SharedFunctionInfo>());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000152}
153
154
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155// -----------------------------------------------------------------------------
156// Return.
157
158
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400159TARGET_TEST_F(InstructionSelectorTest, ReturnFloat32Constant) {
160 const float kValue = 4.2f;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 StreamBuilder m(this, MachineType::Float32());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400162 m.Return(m.Float32Constant(kValue));
163 Stream s = m.Build(kAllInstructions);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000164 ASSERT_EQ(3U, s.size());
Emily Bernierd0a1eb72015-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 Murdoch4a90d5f2016-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 Murdoch4a90d5f2016-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 Murdoch4a90d5f2016-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 Murdoch4a90d5f2016-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
202
203TARGET_TEST_F(InstructionSelectorTest, TruncateFloat64ToInt32WithParameter) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204 StreamBuilder m(this, MachineType::Int32(), MachineType::Float64());
205 m.Return(
206 m.TruncateFloat64ToInt32(TruncationMode::kJavaScript, m.Parameter(0)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207 Stream s = m.Build(kAllInstructions);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 ASSERT_EQ(4U, s.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 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
221TARGET_TEST_F(InstructionSelectorTest, DoubleParameter) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000222 StreamBuilder m(this, MachineType::Float64(), MachineType::Float64());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 Node* param = m.Parameter(0);
224 m.Return(param);
225 Stream s = m.Build(kAllInstructions);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400226 EXPECT_TRUE(s.IsDouble(param));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227}
228
229
230TARGET_TEST_F(InstructionSelectorTest, ReferenceParameter) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000231 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232 Node* param = m.Parameter(0);
233 m.Return(param);
234 Stream s = m.Build(kAllInstructions);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400235 EXPECT_TRUE(s.IsReference(param));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236}
237
238
239// -----------------------------------------------------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000240// FinishRegion.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241
242
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000243TARGET_TEST_F(InstructionSelectorTest, FinishRegion) {
244 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 Node* param = m.Parameter(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000246 Node* finish =
247 m.AddNode(m.common()->FinishRegion(), param, m.graph()->start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 m.Return(finish);
249 Stream s = m.Build(kAllInstructions);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250 ASSERT_EQ(4U, s.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 EXPECT_EQ(kArchNop, s[0]->arch_opcode());
252 ASSERT_EQ(1U, s[0]->OutputCount());
253 ASSERT_TRUE(s[0]->Output()->IsUnallocated());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400254 EXPECT_EQ(s.ToVreg(param), s.ToVreg(s[0]->Output()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 EXPECT_EQ(kArchNop, s[1]->arch_opcode());
256 ASSERT_EQ(1U, s[1]->InputCount());
257 ASSERT_TRUE(s[1]->InputAt(0)->IsUnallocated());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400258 EXPECT_EQ(s.ToVreg(param), s.ToVreg(s[1]->InputAt(0)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 ASSERT_EQ(1U, s[1]->OutputCount());
260 ASSERT_TRUE(s[1]->Output()->IsUnallocated());
261 EXPECT_TRUE(UnallocatedOperand::cast(s[1]->Output())->HasSameAsInputPolicy());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400262 EXPECT_EQ(s.ToVreg(finish), s.ToVreg(s[1]->Output()));
263 EXPECT_TRUE(s.IsReference(finish));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264}
265
266
267// -----------------------------------------------------------------------------
268// Phi.
269
270
271typedef InstructionSelectorTestWithParam<MachineType>
272 InstructionSelectorPhiTest;
273
274
275TARGET_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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280 RawMachineLabel a, b, c;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000287 Node* phi = m.Phi(type.representation(), param0, param1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288 m.Return(phi);
289 Stream s = m.Build(kAllInstructions);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400290 EXPECT_EQ(s.IsDouble(phi), s.IsDouble(param0));
291 EXPECT_EQ(s.IsDouble(phi), s.IsDouble(param1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292}
293
294
295TARGET_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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000300 RawMachineLabel a, b, c;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000307 Node* phi = m.Phi(type.representation(), param0, param1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 m.Return(phi);
309 Stream s = m.Build(kAllInstructions);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400310 EXPECT_EQ(s.IsReference(phi), s.IsReference(param0));
311 EXPECT_EQ(s.IsReference(phi), s.IsReference(param1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312}
313
314
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315INSTANTIATE_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()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323
324
325// -----------------------------------------------------------------------------
326// ValueEffect.
327
328
329TARGET_TEST_F(InstructionSelectorTest, ValueEffect) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000330 StreamBuilder m1(this, MachineType::Int32(), MachineType::Pointer());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331 Node* p1 = m1.Parameter(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000332 m1.Return(m1.Load(MachineType::Int32(), p1, m1.Int32Constant(0)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333 Stream s1 = m1.Build(kAllInstructions);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000334 StreamBuilder m2(this, MachineType::Int32(), MachineType::Pointer());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335 Node* p2 = m2.Parameter(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000336 m2.Return(m2.AddNode(
337 m2.machine()->Load(MachineType::Int32()), p2, m2.Int32Constant(0),
338 m2.AddNode(m2.common()->BeginRegion(), m2.graph()->start())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339 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.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400354
355
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356TARGET_TEST_F(InstructionSelectorTest, CallJSFunctionWithDeopt) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000357 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged(),
358 MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359
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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000366 ZoneVector<MachineType> int32_type(1, MachineType::Int32(), zone());
367 ZoneVector<MachineType> empty_types(zone());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000369 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000388 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407TARGET_TEST_F(InstructionSelectorTest, CallStubWithDeopt) {
408 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged(),
409 MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000410
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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000418 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());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000422 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());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 Node* context_sentinel = m.Int32Constant(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000435 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());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000440
441 // Build the call.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000442 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445 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 +
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000463 5 + // Frame state deopt id + one input for each value in frame state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000464 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());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400476 EXPECT_EQ(OutputFrameStateCombine::kPushOutput,
477 desc_before->state_combine().kind());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000478 EXPECT_EQ(1u, desc_before->parameters_count());
479 EXPECT_EQ(1u, desc_before->locals_count());
480 EXPECT_EQ(1u, desc_before->stack_count());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000481 EXPECT_EQ(43, s.ToInt32(call_instr->InputAt(3)));
482 EXPECT_EQ(0, s.ToInt32(call_instr->InputAt(4))); // This should be a context.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400483 // We inserted 0 here.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000484 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));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000495
496 // Function.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000497 EXPECT_EQ(s.ToVreg(function_node), s.ToVreg(call_instr->InputAt(7)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000498 // Context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000499 EXPECT_EQ(s.ToVreg(context), s.ToVreg(call_instr->InputAt(8)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500
501 EXPECT_EQ(kArchRet, s[index++]->arch_opcode());
502
503 EXPECT_EQ(index, s.size());
504}
505
506
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000507TARGET_TEST_F(InstructionSelectorTest, CallStubWithDeoptRecursiveFrameState) {
508 StreamBuilder m(this, MachineType::AnyTagged(), MachineType::AnyTagged(),
509 MachineType::AnyTagged(), MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000510
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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000518 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528
529 // Build frame state for the state before the call.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000530 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());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 Node* parameters2 =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000543 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000553
554 // Build the call.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000555 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000576 6 + // One input for each value in frame state + context.
577 5 + // One input for each value in the parent frame state + context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578 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);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400588 FrameStateDescriptor* desc_before_outer = desc_before->outer_state();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589 EXPECT_EQ(bailout_id_before, desc_before->bailout_id());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400590 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.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000594 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));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597 // Context:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000598 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));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400604 // 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());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000608 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));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000619
620 // Function.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000621 EXPECT_EQ(s.ToVreg(function_node), s.ToVreg(call_instr->InputAt(13)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622 // Context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000623 EXPECT_EQ(s.ToVreg(context2), s.ToVreg(call_instr->InputAt(14)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 // 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