OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 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_REGISTER_ALLOCATOR_H_ | 5 #ifndef V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |
6 #define V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ | 6 #define V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |
7 | 7 |
8 #include "src/interpreter/bytecode-register.h" | |
8 #include "src/interpreter/bytecodes.h" | 9 #include "src/interpreter/bytecodes.h" |
9 #include "src/zone/zone-containers.h" | 10 #include "src/zone/zone-containers.h" |
10 | 11 |
11 namespace v8 { | 12 namespace v8 { |
12 namespace internal { | 13 namespace internal { |
13 namespace interpreter { | 14 namespace interpreter { |
14 | 15 |
15 class BytecodeArrayBuilder; | 16 class BytecodeRegisterAllocator; |
Leszek Swirski
2016/09/28 15:27:25
Is this forward declaration useful?
rmcilroy
2016/09/30 08:37:54
Nope, removed.
| |
16 class Register; | |
17 class TemporaryRegisterObserver; | |
18 | 17 |
19 class TemporaryRegisterAllocator final { | 18 class RegisterList { |
20 public: | 19 public: |
21 TemporaryRegisterAllocator(Zone* zone, int start_index); | 20 RegisterList() : first_reg_index_(Register().index()), register_count_(0) {} |
21 RegisterList(int first_reg_index, int register_count) | |
22 : first_reg_index_(first_reg_index), register_count_(register_count) {} | |
22 | 23 |
23 // Borrow a temporary register. | 24 // Returns a new RegisterList which is a truncated version of this list, with |
24 int BorrowTemporaryRegister(); | 25 // |count| registers. |
26 const RegisterList Truncate(int new_count) { | |
27 DCHECK_GE(new_count, 0); | |
28 DCHECK_LT(new_count, register_count_); | |
29 return RegisterList(first_reg_index_, new_count); | |
30 } | |
25 | 31 |
26 // Borrow a temporary register from the register range outside of | 32 const Register operator[](size_t i) const { |
27 // |start_index| to |end_index|. | 33 DCHECK_LT(static_cast<int>(i), register_count_); |
28 int BorrowTemporaryRegisterNotInRange(int start_index, int end_index); | 34 return Register(first_reg_index_ + static_cast<int>(i)); |
35 } | |
29 | 36 |
30 // Return a temporary register when no longer used. | 37 const Register first_register() const { |
31 void ReturnTemporaryRegister(int reg_index); | 38 return (register_count() == 0) ? Register(0) : (*this)[0]; |
39 } | |
32 | 40 |
33 // Ensure a run of consecutive registers is available. Each register in | 41 int register_count() const { return register_count_; } |
34 // the range should be borrowed with BorrowConsecutiveTemporaryRegister(). | |
35 // Returns the start index of the run. | |
36 int PrepareForConsecutiveTemporaryRegisters(size_t count); | |
37 | |
38 // Borrow a register from a range prepared with | |
39 // PrepareForConsecutiveTemporaryRegisters(). | |
40 void BorrowConsecutiveTemporaryRegister(int reg_index); | |
41 | |
42 // Returns true if |reg| is a temporary register and is currently | |
43 // borrowed. | |
44 bool RegisterIsLive(Register reg) const; | |
45 | |
46 // Returns the first register in the range of temporary registers. | |
47 Register first_temporary_register() const; | |
48 | |
49 // Returns the last register in the range of temporary registers. | |
50 Register last_temporary_register() const; | |
51 | |
52 // Returns the start index of temporary register allocations. | |
53 int allocation_base() const { return allocation_base_; } | |
54 | |
55 // Returns the number of temporary register allocations made. | |
56 int allocation_count() const { return allocation_count_; } | |
57 | |
58 // Sets an observer for temporary register events. | |
59 void set_observer(TemporaryRegisterObserver* observer); | |
60 | 42 |
61 private: | 43 private: |
62 // Allocate a temporary register. | 44 int first_reg_index_; |
63 int AllocateTemporaryRegister(); | 45 int register_count_; |
64 | |
65 ZoneSet<int> free_temporaries_; | |
66 int allocation_base_; | |
67 int allocation_count_; | |
68 TemporaryRegisterObserver* observer_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterAllocator); | |
71 }; | 46 }; |
72 | 47 |
73 class TemporaryRegisterObserver { | 48 // A class that allows the allocation of contiguous temporary registers. |
74 public: | |
75 virtual ~TemporaryRegisterObserver() {} | |
76 virtual void TemporaryRegisterFreeEvent(Register reg) = 0; | |
77 }; | |
78 | |
79 // A class that allows the instantiator to allocate temporary registers that are | |
80 // cleaned up when scope is closed. | |
81 class BytecodeRegisterAllocator final { | 49 class BytecodeRegisterAllocator final { |
82 public: | 50 public: |
83 explicit BytecodeRegisterAllocator(Zone* zone, | 51 // Enables observation of register allocation and free events. |
84 TemporaryRegisterAllocator* allocator); | 52 class Observer { |
85 ~BytecodeRegisterAllocator(); | 53 public: |
86 Register NewRegister(); | 54 virtual ~Observer() {} |
55 virtual void RegisterAllocateEvent(Register reg) = 0; | |
56 virtual void RegisterListAllocateEvent(RegisterList reg_list) = 0; | |
57 virtual void RegisterListFreeEvent(RegisterList reg_list) = 0; | |
58 }; | |
87 | 59 |
88 // Ensure |count| consecutive allocations are available. | 60 explicit BytecodeRegisterAllocator(int start_index) |
89 void PrepareForConsecutiveAllocations(size_t count); | 61 : next_register_index_(start_index), |
62 max_register_count_(start_index), | |
63 observer_(nullptr) {} | |
64 ~BytecodeRegisterAllocator() {} | |
90 | 65 |
91 // Get the next consecutive allocation after calling | 66 // Returns a new register. |
92 // PrepareForConsecutiveAllocations. | 67 Register NewRegister() { |
93 Register NextConsecutiveRegister(); | 68 Register reg(next_register_index_++); |
69 max_register_count_ = std::max(next_register_index_, max_register_count_); | |
70 if (observer_) { | |
71 observer_->RegisterAllocateEvent(reg); | |
72 } | |
73 return reg; | |
74 } | |
94 | 75 |
95 // Returns true if |reg| is allocated in this allocator. | 76 // Returns a consecutive list of |count| new registers. |
96 bool RegisterIsAllocatedInThisScope(Register reg) const; | 77 RegisterList NewRegisterList(int count) { |
78 RegisterList reg_list(next_register_index_, count); | |
79 next_register_index_ += count; | |
80 max_register_count_ = std::max(next_register_index_, max_register_count_); | |
81 if (observer_) { | |
82 observer_->RegisterListAllocateEvent(reg_list); | |
83 } | |
84 return reg_list; | |
85 } | |
97 | 86 |
98 // Returns true if unused consecutive allocations remain. | 87 // Release all registers above |register_index|. |
99 bool HasConsecutiveAllocations() const { return next_consecutive_count_ > 0; } | 88 void ReleaseRegisters(int register_index) { |
89 if (observer_) { | |
90 observer_->RegisterListFreeEvent( | |
91 RegisterList(register_index, next_register_index_ - register_index)); | |
92 } | |
93 next_register_index_ = register_index; | |
94 } | |
95 | |
96 // Returns true if the register |reg| is a live register. | |
97 bool RegisterIsLive(Register reg) const { | |
98 return reg.index() < next_register_index_; | |
99 } | |
100 | |
101 void set_observer(Observer* observer) { observer_ = observer; } | |
102 | |
103 int next_register_index() const { return next_register_index_; } | |
104 int maximum_register_count() const { return max_register_count_; } | |
100 | 105 |
101 private: | 106 private: |
102 TemporaryRegisterAllocator* base_allocator() const { return base_allocator_; } | 107 int next_register_index_; |
103 | 108 int max_register_count_; |
104 TemporaryRegisterAllocator* base_allocator_; | 109 Observer* observer_; |
105 ZoneVector<int> allocated_; | |
106 int next_consecutive_register_; | |
107 int next_consecutive_count_; | |
108 | 110 |
109 DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterAllocator); | 111 DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterAllocator); |
110 }; | 112 }; |
111 | 113 |
112 } // namespace interpreter | 114 } // namespace interpreter |
113 } // namespace internal | 115 } // namespace internal |
114 } // namespace v8 | 116 } // namespace v8 |
115 | 117 |
116 | 118 |
117 #endif // V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ | 119 #endif // V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |
OLD | NEW |