1// Copyright 2016 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#ifndef V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_
6#define V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_
7
8#include "src/base/compiler-specific.h"
9#include "src/globals.h"
10#include "src/interpreter/bytecode-peephole-table.h"
11#include "src/interpreter/bytecode-pipeline.h"
12
13namespace v8 {
14namespace internal {
15namespace interpreter {
16
17class BytecodePeepholeActionAndData;
18
19// An optimization stage for performing peephole optimizations on
20// generated bytecode. The optimizer may buffer one bytecode
21// internally.
22class V8_EXPORT_PRIVATE BytecodePeepholeOptimizer final
23    : public NON_EXPORTED_BASE(BytecodePipelineStage),
24      public NON_EXPORTED_BASE(ZoneObject) {
25 public:
26  explicit BytecodePeepholeOptimizer(BytecodePipelineStage* next_stage);
27
28  // BytecodePipelineStage interface.
29  void Write(BytecodeNode* node) override;
30  void WriteJump(BytecodeNode* node, BytecodeLabel* label) override;
31  void BindLabel(BytecodeLabel* label) override;
32  void BindLabel(const BytecodeLabel& target, BytecodeLabel* label) override;
33  Handle<BytecodeArray> ToBytecodeArray(
34      Isolate* isolate, int register_count, int parameter_count,
35      Handle<FixedArray> handler_table) override;
36
37 private:
38#define DECLARE_ACTION(Action)          \
39  void Action(BytecodeNode* const node, \
40              const PeepholeActionAndData* const action_data = nullptr);
41  PEEPHOLE_ACTION_LIST(DECLARE_ACTION)
42#undef DECLARE_ACTION
43
44  void ApplyPeepholeAction(BytecodeNode* const node);
45  void Flush();
46  bool CanElideLastBasedOnSourcePosition(
47      const BytecodeNode* const current) const;
48  void InvalidateLast();
49  bool LastIsValid() const;
50  void SetLast(const BytecodeNode* const node);
51
52  BytecodePipelineStage* next_stage() const { return next_stage_; }
53  BytecodeNode* last() { return &last_; }
54
55  BytecodePipelineStage* next_stage_;
56  BytecodeNode last_;
57
58  DISALLOW_COPY_AND_ASSIGN(BytecodePeepholeOptimizer);
59};
60
61}  // namespace interpreter
62}  // namespace internal
63}  // namespace v8
64
65#endif  // V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_
66