OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_ | 5 #ifndef V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_ |
6 #define V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_ | 6 #define V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_ |
7 | 7 |
8 #include "src/interpreter/bytecode-pipeline.h" | 8 #include "src/interpreter/bytecode-pipeline.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 namespace interpreter { | 12 namespace interpreter { |
13 | 13 |
14 class ConstantArrayBuilder; | 14 class ConstantArrayBuilder; |
15 | 15 |
| 16 #define PEEPHOLE_NON_JUMP_ACTION_LIST(V) \ |
| 17 V(DefaultAction) \ |
| 18 V(UpdateLastAction) \ |
| 19 V(ElideCurrentAction) \ |
| 20 V(ElideCurrentIfOperand0MatchesAction) \ |
| 21 V(ElideCurrentIfLoadingNameConstantAction) \ |
| 22 V(ElideLastAction) \ |
| 23 V(ChangeBytecodeAction) \ |
| 24 V(TransformLdaStarToLdrLdarAction) \ |
| 25 V(TransformLdaSmiBinaryOpToBinaryOpWithSmiAction) \ |
| 26 V(TransformLdaZeroBinaryOpToBinaryOpWithZeroAction) |
| 27 |
| 28 #define PEEPHOLE_JUMP_ACTION_LIST(V) \ |
| 29 V(DefaultJumpAction) \ |
| 30 V(UpdateLastJumpAction) \ |
| 31 V(ChangeJumpBytecodeAction) \ |
| 32 V(ElideLastBeforeJumpAction) |
| 33 |
| 34 #define PEEPHOLE_ACTION_LIST(V) \ |
| 35 PEEPHOLE_NON_JUMP_ACTION_LIST(V) \ |
| 36 PEEPHOLE_JUMP_ACTION_LIST(V) |
| 37 |
| 38 // Actions to take when a pair of bytes is encountered. A handler |
| 39 // exists for each action. |
| 40 enum class PeepholeAction : uint8_t { |
| 41 #define DECLARE_PEEPHOLE_ACTION(Action) k##Action, |
| 42 PEEPHOLE_ACTION_LIST(DECLARE_PEEPHOLE_ACTION) |
| 43 #undef DECLARE_PEEPHOLE_ACTION |
| 44 }; |
| 45 |
| 46 // Tuple of action to take when pair of bytecodes is encountered and |
| 47 // optional data to invoke handler with. |
| 48 struct PeepholeActionAndData final { |
| 49 // Action to take when tuple of bytecodes encountered. |
| 50 PeepholeAction action; |
| 51 |
| 52 // Replacement bytecode (if valid). |
| 53 Bytecode bytecode; |
| 54 }; |
| 55 |
16 // An optimization stage for performing peephole optimizations on | 56 // An optimization stage for performing peephole optimizations on |
17 // generated bytecode. The optimizer may buffer one bytecode | 57 // generated bytecode. The optimizer may buffer one bytecode |
18 // internally. | 58 // internally. |
19 class BytecodePeepholeOptimizer final : public BytecodePipelineStage, | 59 class BytecodePeepholeOptimizer final : public BytecodePipelineStage, |
20 public ZoneObject { | 60 public ZoneObject { |
21 public: | 61 public: |
22 BytecodePeepholeOptimizer(ConstantArrayBuilder* constant_array_builder, | 62 BytecodePeepholeOptimizer(ConstantArrayBuilder* constant_array_builder, |
23 BytecodePipelineStage* next_stage); | 63 BytecodePipelineStage* next_stage); |
24 | 64 |
25 // BytecodePipelineStage interface. | 65 // BytecodePipelineStage interface. |
26 void Write(BytecodeNode* node) override; | 66 void Write(BytecodeNode* node) override; |
27 void WriteJump(BytecodeNode* node, BytecodeLabel* label) override; | 67 void WriteJump(BytecodeNode* node, BytecodeLabel* label) override; |
28 void BindLabel(BytecodeLabel* label) override; | 68 void BindLabel(BytecodeLabel* label) override; |
29 void BindLabel(const BytecodeLabel& target, BytecodeLabel* label) override; | 69 void BindLabel(const BytecodeLabel& target, BytecodeLabel* label) override; |
30 Handle<BytecodeArray> ToBytecodeArray( | 70 Handle<BytecodeArray> ToBytecodeArray( |
31 int fixed_register_count, int parameter_count, | 71 int fixed_register_count, int parameter_count, |
32 Handle<FixedArray> handler_table) override; | 72 Handle<FixedArray> handler_table) override; |
33 | 73 |
34 private: | 74 private: |
35 BytecodeNode* OptimizeAndEmitLast(BytecodeNode* current); | 75 #define DECLARE_ACTION(Action) \ |
36 BytecodeNode* Optimize(BytecodeNode* current); | 76 void Action(BytecodeNode* const node, \ |
| 77 const PeepholeActionAndData* const action_data = nullptr); |
| 78 PEEPHOLE_ACTION_LIST(DECLARE_ACTION) |
| 79 #undef DECLARE_ACTION |
| 80 |
| 81 void Optimize(BytecodeNode* const node); |
37 void Flush(); | 82 void Flush(); |
38 | |
39 void TryToRemoveLastExpressionPosition(const BytecodeNode* const current); | |
40 bool TransformCurrentBytecode(BytecodeNode* const current); | |
41 bool TransformLastAndCurrentBytecodes(BytecodeNode* const current); | |
42 bool CanElideCurrent(const BytecodeNode* const current) const; | |
43 bool CanElideLast(const BytecodeNode* const current) const; | |
44 bool CanElideLastBasedOnSourcePosition( | 83 bool CanElideLastBasedOnSourcePosition( |
45 const BytecodeNode* const current) const; | 84 const BytecodeNode* const current) const; |
46 | |
47 // Simple substitution methods. | |
48 bool RemoveToBooleanFromJump(BytecodeNode* const current); | |
49 bool RemoveToBooleanFromLogicalNot(BytecodeNode* const current); | |
50 | |
51 void InvalidateLast(); | 85 void InvalidateLast(); |
52 bool LastIsValid() const; | 86 bool LastIsValid() const; |
53 void SetLast(const BytecodeNode* const node); | 87 void SetLast(const BytecodeNode* const node); |
54 | 88 |
55 bool LastBytecodePutsNameInAccumulator() const; | |
56 | |
57 Handle<Object> GetConstantForIndexOperand(const BytecodeNode* const node, | 89 Handle<Object> GetConstantForIndexOperand(const BytecodeNode* const node, |
58 int index) const; | 90 int index) const; |
59 | 91 |
| 92 BytecodePipelineStage* next_stage() const { return next_stage_; } |
| 93 BytecodeNode* last() { return &last_; } |
| 94 |
60 ConstantArrayBuilder* constant_array_builder_; | 95 ConstantArrayBuilder* constant_array_builder_; |
61 BytecodePipelineStage* next_stage_; | 96 BytecodePipelineStage* next_stage_; |
62 BytecodeNode last_; | 97 BytecodeNode last_; |
63 | 98 |
64 DISALLOW_COPY_AND_ASSIGN(BytecodePeepholeOptimizer); | 99 DISALLOW_COPY_AND_ASSIGN(BytecodePeepholeOptimizer); |
65 }; | 100 }; |
66 | 101 |
67 } // namespace interpreter | 102 } // namespace interpreter |
68 } // namespace internal | 103 } // namespace internal |
69 } // namespace v8 | 104 } // namespace v8 |
70 | 105 |
71 #endif // V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_ | 106 #endif // V8_INTERPRETER_BYTECODE_PEEPHOLE_OPTIMIZER_H_ |
OLD | NEW |