Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 1 | // Copyright 2015 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/interpreter/bytecode-pipeline.h" |
| 9 | |
| 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | namespace interpreter { |
| 13 | |
| 14 | class ConstantArrayBuilder; |
| 15 | |
| 16 | // An optimization stage for performing peephole optimizations on |
| 17 | // generated bytecode. The optimizer may buffer one bytecode |
| 18 | // internally. |
| 19 | class BytecodePeepholeOptimizer final : public BytecodePipelineStage, |
| 20 | public ZoneObject { |
| 21 | public: |
| 22 | BytecodePeepholeOptimizer(ConstantArrayBuilder* constant_array_builder, |
| 23 | BytecodePipelineStage* next_stage); |
| 24 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 25 | // BytecodePipelineStage interface. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 26 | void Write(BytecodeNode* node) override; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 27 | void WriteJump(BytecodeNode* node, BytecodeLabel* label) override; |
| 28 | void BindLabel(BytecodeLabel* label) override; |
| 29 | void BindLabel(const BytecodeLabel& target, BytecodeLabel* label) override; |
| 30 | Handle<BytecodeArray> ToBytecodeArray( |
| 31 | int fixed_register_count, int parameter_count, |
| 32 | Handle<FixedArray> handler_table) override; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 33 | |
| 34 | private: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 35 | BytecodeNode* OptimizeAndEmitLast(BytecodeNode* current); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 36 | BytecodeNode* Optimize(BytecodeNode* current); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 37 | void Flush(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 38 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 39 | void TryToRemoveLastExpressionPosition(const BytecodeNode* const current); |
| 40 | bool TransformCurrentBytecode(BytecodeNode* const current); |
| 41 | bool TransformLastAndCurrentBytecodes(BytecodeNode* const current); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 42 | bool CanElideCurrent(const BytecodeNode* const current) const; |
| 43 | bool CanElideLast(const BytecodeNode* const current) const; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 44 | bool CanElideLastBasedOnSourcePosition( |
| 45 | const BytecodeNode* const current) const; |
| 46 | |
| 47 | // Simple substitution methods. |
| 48 | bool RemoveToBooleanFromJump(BytecodeNode* const current); |
| 49 | bool RemoveToBooleanFromLogicalNot(BytecodeNode* const current); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 50 | |
| 51 | void InvalidateLast(); |
| 52 | bool LastIsValid() const; |
| 53 | void SetLast(const BytecodeNode* const node); |
| 54 | |
| 55 | bool LastBytecodePutsNameInAccumulator() const; |
| 56 | |
| 57 | Handle<Object> GetConstantForIndexOperand(const BytecodeNode* const node, |
| 58 | int index) const; |
| 59 | |
| 60 | ConstantArrayBuilder* constant_array_builder_; |
| 61 | BytecodePipelineStage* next_stage_; |
| 62 | BytecodeNode last_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(BytecodePeepholeOptimizer); |
| 65 | }; |
| 66 | |
| 67 | } // namespace interpreter |
| 68 | } // namespace internal |
| 69 | } // namespace v8 |
| 70 | |
| 71 | #endif // V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_ |