summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/interpreter/bytecode-peephole-optimizer-unittest.cc
blob: 7d139f4b565fb94b669c9b666980c8576db76832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/v8.h"

#include "src/factory.h"
#include "src/interpreter/bytecode-label.h"
#include "src/interpreter/bytecode-peephole-optimizer.h"
#include "src/objects-inl.h"
#include "src/objects.h"
#include "test/unittests/test-utils.h"

namespace v8 {
namespace internal {
namespace interpreter {

class BytecodePeepholeOptimizerTest : public BytecodePipelineStage,
                                      public TestWithIsolateAndZone {
 public:
  BytecodePeepholeOptimizerTest()
      : peephole_optimizer_(this),
        last_written_(BytecodeNode::Illegal(BytecodeSourceInfo())) {}
  ~BytecodePeepholeOptimizerTest() override {}

  void Reset() {
    last_written_ = BytecodeNode::Illegal(BytecodeSourceInfo());
    write_count_ = 0;
  }

  void Write(BytecodeNode* node) override {
    write_count_++;
    last_written_ = *node;
  }

  void WriteJump(BytecodeNode* node, BytecodeLabel* label) override {
    write_count_++;
    last_written_ = *node;
  }

  void BindLabel(BytecodeLabel* label) override {}
  void BindLabel(const BytecodeLabel& target, BytecodeLabel* label) override {}
  Handle<BytecodeArray> ToBytecodeArray(
      Isolate* isolate, int fixed_register_count, int parameter_count,
      Handle<FixedArray> handle_table) override {
    return Handle<BytecodeArray>();
  }

  void Flush() {
    optimizer()->ToBytecodeArray(isolate(), 0, 0,
                                 factory()->empty_fixed_array());
  }

  BytecodePeepholeOptimizer* optimizer() { return &peephole_optimizer_; }

  int write_count() const { return write_count_; }
  const BytecodeNode& last_written() const { return last_written_; }

 private:
  BytecodePeepholeOptimizer peephole_optimizer_;

  int write_count_ = 0;
  BytecodeNode last_written_;
};

// Sanity tests.

TEST_F(BytecodePeepholeOptimizerTest, FlushOnJump) {
  CHECK_EQ(write_count(), 0);

  BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1);
  optimizer()->Write(&add);
  CHECK_EQ(write_count(), 0);

  BytecodeLabel target;
  BytecodeNode jump(Bytecode::kJump, 0);
  optimizer()->WriteJump(&jump, &target);
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(jump, last_written());
}

TEST_F(BytecodePeepholeOptimizerTest, FlushOnBind) {
  CHECK_EQ(write_count(), 0);

  BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1);
  optimizer()->Write(&add);
  CHECK_EQ(write_count(), 0);

  BytecodeLabel target;
  optimizer()->BindLabel(&target);
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(add, last_written());
}

// Nop elimination tests.

TEST_F(BytecodePeepholeOptimizerTest, ElideEmptyNop) {
  BytecodeNode nop(Bytecode::kNop);
  optimizer()->Write(&nop);
  BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1);
  optimizer()->Write(&add);
  Flush();
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(add, last_written());
}

TEST_F(BytecodePeepholeOptimizerTest, ElideExpressionNop) {
  BytecodeSourceInfo source_info(3, false);
  BytecodeNode nop(Bytecode::kNop, source_info);
  optimizer()->Write(&nop);
  BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1);
  optimizer()->Write(&add);
  Flush();
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(add, last_written());
}

TEST_F(BytecodePeepholeOptimizerTest, KeepStatementNop) {
  BytecodeSourceInfo source_info_statement(3, true);
  BytecodeNode nop(Bytecode::kNop, source_info_statement);
  optimizer()->Write(&nop);
  BytecodeSourceInfo source_info_expression(3, false);
  BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1,
                   source_info_expression);
  optimizer()->Write(&add);
  Flush();
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(add, last_written());
}

// Tests covering BytecodePeepholeOptimizer::UpdateCurrentBytecode().

TEST_F(BytecodePeepholeOptimizerTest, KeepJumpIfToBooleanTrue) {
  BytecodeNode first(Bytecode::kLdaNull);
  BytecodeNode second(Bytecode::kJumpIfToBooleanTrue, 3);
  BytecodeLabel label;
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->WriteJump(&second, &label);
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(last_written(), second);
}

TEST_F(BytecodePeepholeOptimizerTest, ElideJumpIfToBooleanTrue) {
  BytecodeNode first(Bytecode::kLdaTrue);
  BytecodeNode second(Bytecode::kJumpIfToBooleanTrue, 3);
  BytecodeLabel label;
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->WriteJump(&second, &label);
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(last_written(), second);
}

TEST_F(BytecodePeepholeOptimizerTest, KeepToBooleanLogicalNot) {
  BytecodeNode first(Bytecode::kLdaNull);
  BytecodeNode second(Bytecode::kToBooleanLogicalNot);
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), first);
  Flush();
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(last_written(), second);
}

TEST_F(BytecodePeepholeOptimizerTest, ElideToBooleanLogicalNot) {
  BytecodeNode first(Bytecode::kLdaTrue);
  BytecodeNode second(Bytecode::kToBooleanLogicalNot);
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), first);
  Flush();
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(last_written().bytecode(), Bytecode::kLogicalNot);
}

// Tests covering BytecodePeepholeOptimizer::CanElideCurrent().

TEST_F(BytecodePeepholeOptimizerTest, StarRxLdarRy) {
  BytecodeNode first(Bytecode::kStar, Register(0).ToOperand());
  BytecodeNode second(Bytecode::kLdar, Register(1).ToOperand());
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), first);
  Flush();
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(last_written(), second);
}

TEST_F(BytecodePeepholeOptimizerTest, StarRxLdarRx) {
  BytecodeLabel label;
  BytecodeNode first(Bytecode::kStar, Register(0).ToOperand());
  BytecodeNode second(Bytecode::kLdar, Register(0).ToOperand());
  optimizer()->Write(&first);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 0);
  Flush();
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), first);
}

TEST_F(BytecodePeepholeOptimizerTest, StarRxLdarRxStatement) {
  BytecodeNode first(Bytecode::kStar, Register(0).ToOperand());
  BytecodeSourceInfo source_info(3, true);
  BytecodeNode second(Bytecode::kLdar, Register(0).ToOperand(), source_info);
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), first);
  Flush();
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(last_written().bytecode(), Bytecode::kNop);
  CHECK_EQ(last_written().source_info(), source_info);
}

TEST_F(BytecodePeepholeOptimizerTest, StarRxLdarRxStatementStarRy) {
  BytecodeLabel label;
  BytecodeNode first(Bytecode::kStar, Register(0).ToOperand());
  BytecodeSourceInfo source_info(0, true);
  BytecodeNode second(Bytecode::kLdar, Register(0).ToOperand(), source_info);
  BytecodeNode third(Bytecode::kStar, Register(3).ToOperand());
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), first);
  optimizer()->Write(&third);
  CHECK_EQ(write_count(), 1);
  Flush();
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(last_written(), third);
}

TEST_F(BytecodePeepholeOptimizerTest, LdarToName) {
  BytecodeNode first(Bytecode::kLdar, Register(0).ToOperand());
  BytecodeNode second(Bytecode::kToName, Register(0).ToOperand());
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), first);
  Flush();
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(last_written(), second);
}

TEST_F(BytecodePeepholeOptimizerTest, TypeOfToName) {
  BytecodeNode first(Bytecode::kTypeOf);
  BytecodeNode second(Bytecode::kToName, Register(0).ToOperand());
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), first);
  Flush();
  CHECK_EQ(write_count(), 2);
  CHECK_EQ(last_written(), second);
  CHECK_EQ(last_written().bytecode(), Bytecode::kStar);
}

// Tests covering BytecodePeepholeOptimizer::CanElideLast().

TEST_F(BytecodePeepholeOptimizerTest, LdaTrueLdaFalse) {
  BytecodeNode first(Bytecode::kLdaTrue);
  BytecodeNode second(Bytecode::kLdaFalse);
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 0);
  Flush();
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), second);
}

TEST_F(BytecodePeepholeOptimizerTest, LdaTrueStatementLdaFalse) {
  BytecodeSourceInfo source_info(3, true);
  BytecodeNode first(Bytecode::kLdaTrue, source_info);
  BytecodeNode second(Bytecode::kLdaFalse);
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 0);
  Flush();
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), second);
  CHECK(second.source_info().is_statement());
  CHECK_EQ(second.source_info().source_position(), 3);
}

TEST_F(BytecodePeepholeOptimizerTest, NopStackCheck) {
  BytecodeNode first(Bytecode::kNop);
  BytecodeNode second(Bytecode::kStackCheck);
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 0);
  Flush();
  CHECK_EQ(write_count(), 1);
  CHECK_EQ(last_written(), second);
}

TEST_F(BytecodePeepholeOptimizerTest, NopStatementStackCheck) {
  BytecodeSourceInfo source_info(3, true);
  BytecodeNode first(Bytecode::kNop, source_info);
  BytecodeNode second(Bytecode::kStackCheck);
  optimizer()->Write(&first);
  CHECK_EQ(write_count(), 0);
  optimizer()->Write(&second);
  CHECK_EQ(write_count(), 0);
  Flush();
  CHECK_EQ(write_count(), 1);
  BytecodeNode expected(Bytecode::kStackCheck, source_info);
  CHECK_EQ(last_written(), expected);
}

// Tests covering BytecodePeepholeOptimizer::UpdateLastAndCurrentBytecodes().

TEST_F(BytecodePeepholeOptimizerTest, MergeLdaSmiWithBinaryOp) {
  Bytecode operator_replacement_pairs[][2] = {
      {Bytecode::kAdd, Bytecode::kAddSmi},
      {Bytecode::kSub, Bytecode::kSubSmi},
      {Bytecode::kBitwiseAnd, Bytecode::kBitwiseAndSmi},
      {Bytecode::kBitwiseOr, Bytecode::kBitwiseOrSmi},
      {Bytecode::kShiftLeft, Bytecode::kShiftLeftSmi},
      {Bytecode::kShiftRight, Bytecode::kShiftRightSmi}};

  for (auto operator_replacement : operator_replacement_pairs) {
    uint32_t imm_operand = 17;
    BytecodeSourceInfo source_info(3, true);
    BytecodeNode first(Bytecode::kLdaSmi, imm_operand, source_info);
    uint32_t reg_operand = Register(0).ToOperand();
    uint32_t idx_operand = 1;
    BytecodeNode second(operator_replacement[0], reg_operand, idx_operand);
    optimizer()->Write(&first);
    optimizer()->Write(&second);
    Flush();
    CHECK_EQ(write_count(), 1);
    CHECK_EQ(last_written().bytecode(), operator_replacement[1]);
    CHECK_EQ(last_written().operand_count(), 3);
    CHECK_EQ(last_written().operand(0), imm_operand);
    CHECK_EQ(last_written().operand(1), reg_operand);
    CHECK_EQ(last_written().operand(2), idx_operand);
    CHECK_EQ(last_written().source_info(), source_info);
    Reset();
  }
}

TEST_F(BytecodePeepholeOptimizerTest, NotMergingLdaSmiWithBinaryOp) {
  Bytecode operator_replacement_pairs[][2] = {
      {Bytecode::kAdd, Bytecode::kAddSmi},
      {Bytecode::kSub, Bytecode::kSubSmi},
      {Bytecode::kBitwiseAnd, Bytecode::kBitwiseAndSmi},
      {Bytecode::kBitwiseOr, Bytecode::kBitwiseOrSmi},
      {Bytecode::kShiftLeft, Bytecode::kShiftLeftSmi},
      {Bytecode::kShiftRight, Bytecode::kShiftRightSmi}};

  for (auto operator_replacement : operator_replacement_pairs) {
    uint32_t imm_operand = 17;
    BytecodeSourceInfo source_info(3, true);
    BytecodeNode first(Bytecode::kLdaSmi, imm_operand, source_info);
    uint32_t reg_operand = Register(0).ToOperand();
    source_info.MakeStatementPosition(4);
    BytecodeNode second(operator_replacement[0], reg_operand, 1, source_info);
    optimizer()->Write(&first);
    optimizer()->Write(&second);
    CHECK_EQ(last_written(), first);
    Flush();
    CHECK_EQ(last_written(), second);
    Reset();
  }
}

TEST_F(BytecodePeepholeOptimizerTest, MergeLdaZeroWithBinaryOp) {
  Bytecode operator_replacement_pairs[][2] = {
      {Bytecode::kAdd, Bytecode::kAddSmi},
      {Bytecode::kSub, Bytecode::kSubSmi},
      {Bytecode::kBitwiseAnd, Bytecode::kBitwiseAndSmi},
      {Bytecode::kBitwiseOr, Bytecode::kBitwiseOrSmi},
      {Bytecode::kShiftLeft, Bytecode::kShiftLeftSmi},
      {Bytecode::kShiftRight, Bytecode::kShiftRightSmi}};

  for (auto operator_replacement : operator_replacement_pairs) {
    BytecodeNode first(Bytecode::kLdaZero);
    uint32_t reg_operand = Register(0).ToOperand();
    uint32_t idx_operand = 1;
    BytecodeNode second(operator_replacement[0], reg_operand, idx_operand);
    optimizer()->Write(&first);
    optimizer()->Write(&second);
    Flush();
    CHECK_EQ(write_count(), 1);
    CHECK_EQ(last_written().bytecode(), operator_replacement[1]);
    CHECK_EQ(last_written().operand_count(), 3);
    CHECK_EQ(last_written().operand(0), 0u);
    CHECK_EQ(last_written().operand(1), reg_operand);
    CHECK_EQ(last_written().operand(2), idx_operand);
    Reset();
  }
}

TEST_F(BytecodePeepholeOptimizerTest, MergeLdaNullOrUndefinedWithCompareOp) {
  Bytecode first_bytecodes[] = {Bytecode::kLdaUndefined, Bytecode::kLdaNull};

  for (auto first_bytecode : first_bytecodes) {
    uint32_t reg_operand = Register(0).ToOperand();
    uint32_t idx_operand = 1;
    BytecodeNode first(first_bytecode);
    BytecodeNode second(Bytecode::kTestEqual, reg_operand, idx_operand);
    optimizer()->Write(&first);
    optimizer()->Write(&second);
    Flush();
    CHECK_EQ(write_count(), 1);
    CHECK_EQ(last_written().bytecode(), Bytecode::kTestUndetectable);
    CHECK_EQ(last_written().operand_count(), 1);
    CHECK_EQ(last_written().operand(0), reg_operand);
    Reset();
  }
}

}  // namespace interpreter
}  // namespace internal
}  // namespace v8