blob: 4229610b1cd06b5e9afd1dbb6a46180de5a4d24b [file] [log] [blame]
Ben Murdoch61f157c2016-09-16 13:49:30 +01001// 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
10namespace v8 {
11namespace internal {
12namespace 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.
18class 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 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 int fixed_register_count, int parameter_count,
35 Handle<FixedArray> handler_table) override;
36
37 private:
38 static const uint32_t kInvalidEquivalenceId = kMaxUInt32;
39
40 class RegisterInfo;
41
42 // TemporaryRegisterObserver interface.
43 void TemporaryRegisterFreeEvent(Register reg) override;
44
45 // Helpers for BytecodePipelineStage interface.
46 void FlushState();
47 void WriteToNextStage(BytecodeNode* node) const;
48 void WriteToNextStage(BytecodeNode* node,
49 const BytecodeSourceInfo& output_info) const;
50
51 // Update internal state for register transfer from |input| to
52 // |output| using |source_info| as source position information if
53 // any bytecodes are emitted due to transfer.
54 void RegisterTransfer(RegisterInfo* input, RegisterInfo* output,
55 const BytecodeSourceInfo& source_info);
56
57 // Emit a register transfer bytecode from |input| to |output|.
58 void OutputRegisterTransfer(
59 RegisterInfo* input, RegisterInfo* output,
60 const BytecodeSourceInfo& source_info = BytecodeSourceInfo());
61
62 // Emits a Nop to preserve source position information in the
63 // bytecode pipeline.
64 void EmitNopForSourceInfo(const BytecodeSourceInfo& source_info) const;
65
66 // Handlers for bytecode nodes for register to register transfers.
67 void DoLdar(const BytecodeNode* const node);
68 void DoMov(const BytecodeNode* const node);
69 void DoStar(const BytecodeNode* const node);
70
71 // Operand processing methods for bytecodes other than those
72 // performing register to register transfers.
73 void PrepareOperands(BytecodeNode* const node);
74 void PrepareAccumulator(BytecodeNode* const node);
75 void PrepareRegisterOperands(BytecodeNode* const node);
76
77 void PrepareRegisterOutputOperand(RegisterInfo* reg_info);
78 void PrepareRegisterRangeOutputOperand(Register start, int count);
79 void PrepareRegisterInputOperand(BytecodeNode* const node, Register reg,
80 int operand_index);
81 void PrepareRegisterRangeInputOperand(Register start, int count);
82
83 Register GetEquivalentRegisterForInputOperand(Register reg);
84
85 static Register GetRegisterInputOperand(int index, Bytecode bytecode,
86 const uint32_t* operands,
87 int operand_count);
88 static Register GetRegisterOutputOperand(int index, Bytecode bytecode,
89 const uint32_t* operands,
90 int operand_count);
91
92 void CreateMaterializedEquivalent(RegisterInfo* info);
93 RegisterInfo* GetMaterializedEquivalent(RegisterInfo* info);
94 RegisterInfo* GetMaterializedEquivalentNotAccumulator(RegisterInfo* info);
95 void Materialize(RegisterInfo* info);
96 void AddToEquivalenceSet(RegisterInfo* set_member,
97 RegisterInfo* non_set_member);
98
99 // Methods for finding and creating metadata for each register.
100 RegisterInfo* GetOrCreateRegisterInfo(Register reg);
101 RegisterInfo* GetRegisterInfo(Register reg);
102 RegisterInfo* NewRegisterInfo(Register reg);
103 void GrowRegisterMap(Register reg);
104
105 bool RegisterIsTemporary(Register reg) const {
106 return reg >= temporary_base_;
107 }
108
109 bool RegisterIsObservable(Register reg) const {
110 return reg != accumulator_ && !RegisterIsTemporary(reg);
111 }
112
113 static Register OperandToRegister(uint32_t operand) {
114 return Register::FromOperand(static_cast<int32_t>(operand));
115 }
116
117 size_t GetRegisterInfoTableIndex(Register reg) const {
118 return static_cast<size_t>(reg.index() + register_info_table_offset_);
119 }
120
121 Register RegisterFromRegisterInfoTableIndex(size_t index) const {
122 return Register(static_cast<int>(index) - register_info_table_offset_);
123 }
124
125 uint32_t NextEquivalenceId() {
126 equivalence_id_++;
127 CHECK_NE(equivalence_id_, kInvalidEquivalenceId);
128 return equivalence_id_;
129 }
130
131 Zone* zone() { return zone_; }
132
133 const Register accumulator_;
134 RegisterInfo* accumulator_info_;
135 const Register temporary_base_;
136
137 // Direct mapping to register info.
138 ZoneVector<RegisterInfo*> register_info_table_;
139 int register_info_table_offset_;
140
141 // Counter for equivalence sets identifiers.
142 int equivalence_id_;
143
144 BytecodePipelineStage* next_stage_;
145 bool flush_required_;
146 Zone* zone_;
147
148 DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterOptimizer);
149};
150
151} // namespace interpreter
152} // namespace internal
153} // namespace v8
154
155#endif // V8_INTERPRETER_BYTECODE_REGISTER_OPTIMIZER_H_