bytecode-peephole-optimizer.h 2.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright 2015 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.

#ifndef V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_
#define V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_

#include "src/interpreter/bytecode-pipeline.h"

namespace v8 {
namespace internal {
namespace interpreter {

class ConstantArrayBuilder;

// An optimization stage for performing peephole optimizations on
// generated bytecode. The optimizer may buffer one bytecode
// internally.
class BytecodePeepholeOptimizer final : public BytecodePipelineStage,
                                        public ZoneObject {
 public:
  BytecodePeepholeOptimizer(ConstantArrayBuilder* constant_array_builder,
                            BytecodePipelineStage* next_stage);

25
  // BytecodePipelineStage interface.
26
  void Write(BytecodeNode* node) override;
27 28 29 30 31 32
  void WriteJump(BytecodeNode* node, BytecodeLabel* label) override;
  void BindLabel(BytecodeLabel* label) override;
  void BindLabel(const BytecodeLabel& target, BytecodeLabel* label) override;
  Handle<BytecodeArray> ToBytecodeArray(
      int fixed_register_count, int parameter_count,
      Handle<FixedArray> handler_table) override;
33 34

 private:
35
  BytecodeNode* OptimizeAndEmitLast(BytecodeNode* current);
36
  BytecodeNode* Optimize(BytecodeNode* current);
37
  void Flush();
38

39
  void TryToRemoveLastExpressionPosition(const BytecodeNode* const current);
40
  bool TransformCurrentBytecode(BytecodeNode* const current);
41
  bool TransformLastAndCurrentBytecodes(BytecodeNode* const current);
42 43
  bool CanElideCurrent(const BytecodeNode* const current) const;
  bool CanElideLast(const BytecodeNode* const current) const;
44 45
  bool CanElideLastBasedOnSourcePosition(
      const BytecodeNode* const current) const;
46

47 48 49 50
  // Simple substitution methods.
  bool RemoveToBooleanFromJump(BytecodeNode* const current);
  bool RemoveToBooleanFromLogicalNot(BytecodeNode* const current);

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  void InvalidateLast();
  bool LastIsValid() const;
  void SetLast(const BytecodeNode* const node);

  bool LastBytecodePutsNameInAccumulator() const;

  Handle<Object> GetConstantForIndexOperand(const BytecodeNode* const node,
                                            int index) const;

  ConstantArrayBuilder* constant_array_builder_;
  BytecodePipelineStage* next_stage_;
  BytecodeNode last_;

  DISALLOW_COPY_AND_ASSIGN(BytecodePeepholeOptimizer);
};

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

#endif  // V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_