Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: src/interpreter/bytecode-register-allocator.cc

Issue 1651133002: [interpreter] Move temporary register allocator into own file. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Incorporate review comments from rmcilroy. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/interpreter/bytecode-register-allocator.h ('k') | src/interpreter/bytecodes.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "src/interpreter/bytecode-register-allocator.h" 5 #include "src/interpreter/bytecode-register-allocator.h"
6 6
7 #include "src/interpreter/bytecode-array-builder.h" 7 #include "src/interpreter/bytecode-array-builder.h"
8 8
9 namespace v8 { 9 namespace v8 {
10 namespace internal { 10 namespace internal {
11 namespace interpreter { 11 namespace interpreter {
12 12
13 TemporaryRegisterAllocator::TemporaryRegisterAllocator(Zone* zone,
14 int allocation_base)
15 : free_temporaries_(zone),
16 allocation_base_(allocation_base),
17 allocation_count_(0) {}
18
19 Register TemporaryRegisterAllocator::first_temporary_register() const {
20 DCHECK(allocation_count() > 0);
21 return Register(allocation_base());
22 }
23
24 Register TemporaryRegisterAllocator::last_temporary_register() const {
25 DCHECK(allocation_count() > 0);
26 return Register(allocation_base() + allocation_count() - 1);
27 }
28
29 int TemporaryRegisterAllocator::AllocateTemporaryRegister() {
30 allocation_count_ += 1;
31 return allocation_base() + allocation_count() - 1;
32 }
33
34 int TemporaryRegisterAllocator::BorrowTemporaryRegister() {
35 if (free_temporaries_.empty()) {
36 return AllocateTemporaryRegister();
37 } else {
38 auto pos = free_temporaries_.begin();
39 int retval = *pos;
40 free_temporaries_.erase(pos);
41 return retval;
42 }
43 }
44
45 int TemporaryRegisterAllocator::BorrowTemporaryRegisterNotInRange(
46 int start_index, int end_index) {
47 if (free_temporaries_.empty()) {
48 int next_allocation = allocation_base() + allocation_count();
49 while (next_allocation >= start_index && next_allocation <= end_index) {
50 free_temporaries_.insert(AllocateTemporaryRegister());
51 next_allocation += 1;
52 }
53 return AllocateTemporaryRegister();
54 }
55
56 ZoneSet<int>::iterator index = free_temporaries_.lower_bound(start_index);
57 if (index == free_temporaries_.begin()) {
58 // If start_index is the first free register, check for a register
59 // greater than end_index.
60 index = free_temporaries_.upper_bound(end_index);
61 if (index == free_temporaries_.end()) {
62 return AllocateTemporaryRegister();
63 }
64 } else {
65 // If there is a free register < start_index
66 index--;
67 }
68
69 int retval = *index;
70 free_temporaries_.erase(index);
71 return retval;
72 }
73
74 int TemporaryRegisterAllocator::PrepareForConsecutiveTemporaryRegisters(
75 size_t count) {
76 if (count == 0) {
77 return -1;
78 }
79
80 // TODO(oth): replace use of set<> here for free_temporaries with a
81 // more efficient structure. And/or partition into two searches -
82 // one before the translation window and one after.
83
84 // A run will require at least |count| free temporaries.
85 while (free_temporaries_.size() < count) {
86 free_temporaries_.insert(AllocateTemporaryRegister());
87 }
88
89 // Search within existing temporaries for a run.
90 auto start = free_temporaries_.begin();
91 size_t run_length = 0;
92 for (auto run_end = start; run_end != free_temporaries_.end(); run_end++) {
93 int expected = *start + static_cast<int>(run_length);
94 if (*run_end != expected) {
95 start = run_end;
96 run_length = 0;
97 }
98 Register reg_start(*start);
99 Register reg_expected(expected);
100 if (RegisterTranslator::DistanceToTranslationWindow(reg_start) > 0 &&
101 RegisterTranslator::DistanceToTranslationWindow(reg_expected) <= 0) {
102 // Run straddles the lower edge of the translation window. Registers
103 // after the start of this boundary are displaced by the register
104 // translator to provide a hole for translation. Runs either side
105 // of the boundary are fine.
106 start = run_end;
107 run_length = 0;
108 }
109 if (++run_length == count) {
110 return *start;
111 }
112 }
113
114 // Continue run if possible across existing last temporary.
115 if (allocation_count_ > 0 && (start == free_temporaries_.end() ||
116 *start + static_cast<int>(run_length) !=
117 last_temporary_register().index() + 1)) {
118 run_length = 0;
119 }
120
121 // Pad temporaries if extended run would cross translation boundary.
122 Register reg_first(*start);
123 Register reg_last(*start + static_cast<int>(count) - 1);
124 DCHECK_GT(RegisterTranslator::DistanceToTranslationWindow(reg_first),
125 RegisterTranslator::DistanceToTranslationWindow(reg_last));
126 while (RegisterTranslator::DistanceToTranslationWindow(reg_first) > 0 &&
127 RegisterTranslator::DistanceToTranslationWindow(reg_last) <= 0) {
128 auto pos_insert_pair =
129 free_temporaries_.insert(AllocateTemporaryRegister());
130 reg_first = Register(*pos_insert_pair.first);
131 reg_last = Register(reg_first.index() + static_cast<int>(count) - 1);
132 run_length = 0;
133 }
134
135 // Ensure enough registers for run.
136 while (run_length++ < count) {
137 free_temporaries_.insert(AllocateTemporaryRegister());
138 }
139
140 int run_start =
141 last_temporary_register().index() - static_cast<int>(count) + 1;
142 DCHECK(RegisterTranslator::DistanceToTranslationWindow(Register(run_start)) <=
143 0 ||
144 RegisterTranslator::DistanceToTranslationWindow(
145 Register(run_start + static_cast<int>(count) - 1)) > 0);
146 return run_start;
147 }
148
149 bool TemporaryRegisterAllocator::RegisterIsLive(Register reg) const {
150 if (allocation_count_ > 0) {
151 DCHECK(reg >= first_temporary_register() &&
152 reg <= last_temporary_register());
153 return free_temporaries_.find(reg.index()) == free_temporaries_.end();
154 } else {
155 return false;
156 }
157 }
158
159 void TemporaryRegisterAllocator::BorrowConsecutiveTemporaryRegister(
160 int reg_index) {
161 DCHECK(free_temporaries_.find(reg_index) != free_temporaries_.end());
162 free_temporaries_.erase(reg_index);
163 }
164
165 void TemporaryRegisterAllocator::ReturnTemporaryRegister(int reg_index) {
166 DCHECK(free_temporaries_.find(reg_index) == free_temporaries_.end());
167 free_temporaries_.insert(reg_index);
168 }
169
13 BytecodeRegisterAllocator::BytecodeRegisterAllocator( 170 BytecodeRegisterAllocator::BytecodeRegisterAllocator(
14 BytecodeArrayBuilder* builder) 171 Zone* zone, TemporaryRegisterAllocator* allocator)
15 : builder_(builder), 172 : base_allocator_(allocator),
16 allocated_(builder->zone()), 173 allocated_(zone),
17 next_consecutive_register_(-1), 174 next_consecutive_register_(-1),
18 next_consecutive_count_(-1) {} 175 next_consecutive_count_(-1) {}
19 176
20
21 BytecodeRegisterAllocator::~BytecodeRegisterAllocator() { 177 BytecodeRegisterAllocator::~BytecodeRegisterAllocator() {
22 for (auto i = allocated_.rbegin(); i != allocated_.rend(); i++) { 178 for (auto i = allocated_.rbegin(); i != allocated_.rend(); i++) {
23 builder_->ReturnTemporaryRegister(*i); 179 base_allocator()->ReturnTemporaryRegister(*i);
24 } 180 }
25 allocated_.clear(); 181 allocated_.clear();
26 } 182 }
27 183
28 184
29 Register BytecodeRegisterAllocator::NewRegister() { 185 Register BytecodeRegisterAllocator::NewRegister() {
30 int allocated = -1; 186 int allocated = -1;
31 if (next_consecutive_count_ <= 0) { 187 if (next_consecutive_count_ <= 0) {
32 allocated = builder_->BorrowTemporaryRegister(); 188 allocated = base_allocator()->BorrowTemporaryRegister();
33 } else { 189 } else {
34 allocated = builder_->BorrowTemporaryRegisterNotInRange( 190 allocated = base_allocator()->BorrowTemporaryRegisterNotInRange(
35 next_consecutive_register_, 191 next_consecutive_register_,
36 next_consecutive_register_ + next_consecutive_count_ - 1); 192 next_consecutive_register_ + next_consecutive_count_ - 1);
37 } 193 }
38 allocated_.push_back(allocated); 194 allocated_.push_back(allocated);
39 return Register(allocated); 195 return Register(allocated);
40 } 196 }
41 197
42 198
43 bool BytecodeRegisterAllocator::RegisterIsAllocatedInThisScope( 199 bool BytecodeRegisterAllocator::RegisterIsAllocatedInThisScope(
44 Register reg) const { 200 Register reg) const {
45 for (auto i = allocated_.begin(); i != allocated_.end(); i++) { 201 for (auto i = allocated_.begin(); i != allocated_.end(); i++) {
46 if (*i == reg.index()) return true; 202 if (*i == reg.index()) return true;
47 } 203 }
48 return false; 204 return false;
49 } 205 }
50 206
51 207
52 void BytecodeRegisterAllocator::PrepareForConsecutiveAllocations(size_t count) { 208 void BytecodeRegisterAllocator::PrepareForConsecutiveAllocations(size_t count) {
53 if (static_cast<int>(count) > next_consecutive_count_) { 209 if (static_cast<int>(count) > next_consecutive_count_) {
54 next_consecutive_register_ = 210 next_consecutive_register_ =
55 builder_->PrepareForConsecutiveTemporaryRegisters(count); 211 base_allocator()->PrepareForConsecutiveTemporaryRegisters(count);
56 next_consecutive_count_ = static_cast<int>(count); 212 next_consecutive_count_ = static_cast<int>(count);
57 } 213 }
58 } 214 }
59 215
60 216
61 Register BytecodeRegisterAllocator::NextConsecutiveRegister() { 217 Register BytecodeRegisterAllocator::NextConsecutiveRegister() {
62 DCHECK_GE(next_consecutive_register_, 0); 218 DCHECK_GE(next_consecutive_register_, 0);
63 DCHECK_GT(next_consecutive_count_, 0); 219 DCHECK_GT(next_consecutive_count_, 0);
64 builder_->BorrowConsecutiveTemporaryRegister(next_consecutive_register_); 220 base_allocator()->BorrowConsecutiveTemporaryRegister(
221 next_consecutive_register_);
65 allocated_.push_back(next_consecutive_register_); 222 allocated_.push_back(next_consecutive_register_);
66 next_consecutive_count_--; 223 next_consecutive_count_--;
67 return Register(next_consecutive_register_++); 224 return Register(next_consecutive_register_++);
68 } 225 }
69 226
70 } // namespace interpreter 227 } // namespace interpreter
71 } // namespace internal 228 } // namespace internal
72 } // namespace v8 229 } // namespace v8
OLDNEW
« no previous file with comments | « src/interpreter/bytecode-register-allocator.h ('k') | src/interpreter/bytecodes.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698