Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_REGISTER_OPTIMIZER_H_ | |
| 6 #define V8_INTERPRETER_BYTECODE_REGISTER_OPTIMIZER_H_ | |
| 7 | |
| 8 #include "src/interpreter/bytecode-pipeline.h" | |
| 9 | |
| 10 namespace v8 { | |
| 11 namespace internal { | |
| 12 namespace interpreter { | |
| 13 | |
| 14 // An optimization stage for eliminating unnecessary transfers between | |
| 15 // registers. The bytecode generator uses temporary registers | |
| 16 // liberally for correctness and convenience and this stage removes | |
| 17 // transfers that are not required and preserves correctness. | |
| 18 class BytecodeRegisterOptimizer final : public BytecodePipelineStage, | |
| 19 public TemporaryRegisterObserver, | |
| 20 public ZoneObject { | |
| 21 public: | |
| 22 BytecodeRegisterOptimizer(Zone* zone, | |
| 23 TemporaryRegisterAllocator* register_allocator, | |
| 24 int parameter_count, | |
| 25 BytecodePipelineStage* next_stage); | |
| 26 virtual ~BytecodeRegisterOptimizer() {} | |
| 27 | |
| 28 // BytecodePipelineStage interface. | |
| 29 size_t FlushForOffset() override; | |
| 30 void FlushBasicBlock() override; | |
| 31 void Write(BytecodeNode* node) override; | |
| 32 | |
| 33 private: | |
| 34 const uint32_t kInvalidEquivalenceId = kMaxUInt32; | |
| 35 | |
| 36 class RegisterInfo; | |
| 37 | |
| 38 // TemporaryRegisterObserver interface. | |
| 39 void TemporaryRegisterFreeEvent(Register reg) override; | |
| 40 | |
| 41 // Helpers for BytecodePipelineStage interface. | |
| 42 void FlushState(); | |
| 43 void WriteToNextStage(BytecodeNode* node) const; | |
| 44 void WriteToNextStage(BytecodeNode* node, | |
| 45 const BytecodeSourceInfo& output_info) const; | |
| 46 | |
| 47 // Update internal state for register transfer from |input| to | |
| 48 // |output| using |source_info| as source position information if | |
| 49 // any bytecodes are emitted due to transfer. | |
| 50 void RegisterTransfer(RegisterInfo* input, RegisterInfo* output, | |
| 51 const BytecodeSourceInfo& source_info); | |
| 52 | |
| 53 // Emit a register transfer bytecode from |input| to |output|. | |
| 54 void OutputRegisterTransfer( | |
| 55 RegisterInfo* input, RegisterInfo* output, | |
| 56 const BytecodeSourceInfo& source_info = BytecodeSourceInfo()); | |
| 57 | |
| 58 // Emits a Nop to preserve source position information in the | |
| 59 // bytecode pipeline. | |
| 60 void EmitNopForSourceInfo(const BytecodeSourceInfo& source_info) const; | |
| 61 | |
| 62 // Handlers for bytecode nodes for register to register transfers. | |
| 63 void DoLdar(const BytecodeNode* const node); | |
| 64 void DoMov(const BytecodeNode* const node); | |
| 65 void DoStar(const BytecodeNode* const node); | |
| 66 | |
| 67 // Operand processing methods for bytecodes other than those | |
| 68 // performing register to register transfers. | |
| 69 void PrepareOperands(BytecodeNode* const node); | |
| 70 void PrepareAccumulator(BytecodeNode* const node); | |
| 71 void PrepareRegisterOperands(BytecodeNode* const node); | |
| 72 | |
| 73 void PrepareRegisterOutputOperand(RegisterInfo* reg_info); | |
| 74 void PrepareRegisterRangeOutputOperand(Register start, int count); | |
| 75 void PrepareRegisterInputOperand(BytecodeNode* const node, Register reg, | |
| 76 int operand_index); | |
| 77 void PrepareRegisterRangeInputOperand(Register start, int count); | |
| 78 | |
| 79 Register GetEquivalentRegisterForInputOperand(Register reg); | |
| 80 | |
| 81 static Register GetRegisterInputOperand(int index, Bytecode bytecode, | |
| 82 const uint32_t* operands, | |
| 83 int operand_count); | |
| 84 static Register GetRegisterOutputOperand(int index, Bytecode bytecode, | |
| 85 const uint32_t* operands, | |
| 86 int operand_count); | |
| 87 | |
| 88 void CreateMaterializedEquivalentIfRequired(RegisterInfo* info); | |
| 89 RegisterInfo* GetMaterializedEquivalent(RegisterInfo* info); | |
| 90 RegisterInfo* GetMaterializedEquivalentNotAccumulator(RegisterInfo* info); | |
| 91 void Materialize(RegisterInfo* info); | |
| 92 | |
| 93 // Methods for finding and creating metadata for each register. | |
| 94 RegisterInfo* GetOrCreateRegisterInfo(Register reg); | |
| 95 RegisterInfo* GetRegisterInfo(Register reg); | |
| 96 RegisterInfo* NewRegisterInfo(Register reg); | |
| 97 void GrowRegisterMap(Register reg); | |
| 98 | |
| 99 bool RegisterIsTemporary(Register reg) const { | |
| 100 return reg >= temporary_base_; | |
| 101 } | |
| 102 | |
| 103 bool RegisterIsObservable(Register reg) const { | |
| 104 return reg != accumulator_ && !RegisterIsTemporary(reg); | |
| 105 } | |
| 106 | |
| 107 static Register OperandToRegister(uint32_t operand) { | |
| 108 return Register::FromOperand(static_cast<int32_t>(operand)); | |
| 109 } | |
| 110 | |
| 111 size_t GetRegisterInfoTableIndex(Register reg) const { | |
| 112 return static_cast<size_t>(reg.index() + register_info_table_offset_); | |
| 113 } | |
| 114 | |
| 115 Register RegisterFromRegisterInfoTableIndex(size_t index) const { | |
|
rmcilroy
2016/05/27 09:18:58
nit - RegisterFromTableIndex ?
| |
| 116 return Register(static_cast<int>(index) - register_info_table_offset_); | |
| 117 } | |
| 118 | |
| 119 uint32_t NextEquivalenceId() { | |
| 120 equivalence_id_++; | |
| 121 CHECK_NE(equivalence_id_, kInvalidEquivalenceId); | |
| 122 return equivalence_id_; | |
| 123 } | |
| 124 | |
| 125 Zone* zone() { return zone_; } | |
| 126 | |
| 127 const Register accumulator_; | |
| 128 RegisterInfo* accumulator_info_; | |
| 129 const Register temporary_base_; | |
| 130 | |
| 131 // Direct mapping to register info. | |
| 132 ZoneVector<RegisterInfo*> register_info_table_; | |
| 133 int register_info_table_offset_; | |
| 134 | |
| 135 // Counter for equivalence sets identifiers. | |
| 136 int equivalence_id_; | |
|
rmcilroy
2016/05/27 09:18:58
uint32_t (given kInvalidEquivalenceId is uint32_t)
| |
| 137 | |
| 138 BytecodePipelineStage* next_stage_; | |
| 139 bool flushed_; | |
| 140 Zone* zone_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterOptimizer); | |
| 143 }; | |
| 144 | |
| 145 } // namespace interpreter | |
| 146 } // namespace internal | |
| 147 } // namespace v8 | |
| 148 | |
| 149 #endif // V8_INTERPRETER_BYTECODE_REGISTER_OPTIMIZER_H_ | |
| OLD | NEW |