]>
Commit | Line | Data |
---|---|---|
14957cd0 A |
1 | /* |
2 | * Copyright (C) 2011 Apple Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | #ifndef DFGRegisterBank_h | |
27 | #define DFGRegisterBank_h | |
28 | ||
29 | #if ENABLE(DFG_JIT) | |
30 | ||
93a37866 | 31 | #include "DFGCommon.h" |
14957cd0 A |
32 | |
33 | namespace JSC { namespace DFG { | |
34 | ||
35 | // === RegisterBank === | |
36 | // | |
37 | // This class is used to implement the GPR and FPR register banks. | |
38 | // All registers have two pieces of state associated with them: | |
39 | // a lock count (used to indicate this register is already in use | |
40 | // in code generation of the current node, and cannot be spilled or | |
41 | // allocated as a temporary), and VirtualRegister 'name', recording | |
42 | // which value (if any) a machine register currently holds. | |
43 | // Either or both of these pieces of information may be valid for a | |
44 | // given register. A register may be: | |
45 | // | |
46 | // - unlocked, and unnamed: Available for allocation. | |
47 | // - locked, but unnamed: Already allocated as a temporary or | |
48 | // result for the current node. | |
49 | // - unlocked, but named: Contains the result of a prior operation, | |
50 | // not yet in use for this node, | |
51 | // - locked, but named: Contains the result of a prior operation, | |
52 | // already allocated as a operand to the | |
53 | // current operation. | |
54 | // | |
55 | // For every named register we also record a hint value indicating | |
56 | // the order in which registers should be selected to be spilled; | |
57 | // registers that can be more cheaply spilled and/or filled should | |
58 | // be selected first. | |
59 | // | |
60 | // Locking register is a strong retention mechanism; a locked register | |
61 | // will never be reallocated (this is used to ensure the operands to | |
62 | // the current node are in registers). Naming, conversely, in a weak | |
63 | // retention mechanism - allocating a register may force a named value | |
64 | // to be spilled. | |
65 | // | |
66 | // All named values must be given a hint that is greater than Min and | |
67 | // less than Max. | |
68 | template<class BankInfo> | |
69 | class RegisterBank { | |
70 | typedef typename BankInfo::RegisterType RegID; | |
71 | static const size_t NUM_REGS = BankInfo::numberOfRegisters; | |
72 | ||
73 | typedef uint32_t SpillHint; | |
74 | static const SpillHint SpillHintInvalid = 0xffffffff; | |
75 | ||
76 | public: | |
77 | RegisterBank() | |
14957cd0 A |
78 | { |
79 | } | |
80 | ||
6fe7ccc8 A |
81 | // Attempt to allocate a register - this function finds an unlocked |
82 | // register, locks it, and returns it. If none can be found, this | |
83 | // returns -1 (InvalidGPRReg or InvalidFPRReg). | |
84 | RegID tryAllocate() | |
85 | { | |
86 | VirtualRegister ignored; | |
87 | ||
93a37866 | 88 | for (uint32_t i = 0; i < NUM_REGS; ++i) { |
6fe7ccc8 A |
89 | if (!m_data[i].lockCount && m_data[i].name == InvalidVirtualRegister) |
90 | return allocateInternal(i, ignored); | |
91 | } | |
92 | ||
93 | return (RegID)-1; | |
94 | } | |
95 | ||
14957cd0 A |
96 | // Allocate a register - this function finds an unlocked register, |
97 | // locks it, and returns it. If any named registers exist, one | |
98 | // of these should be selected to be allocated. If all unlocked | |
99 | // registers are named, then one of the named registers will need | |
100 | // to be spilled. In this case the register selected to be spilled | |
101 | // will be one of the registers that has the lowest 'spillOrder' | |
102 | // cost associated with it. | |
103 | // | |
104 | // This method select the register to be allocated, and calls the | |
105 | // private 'allocateInternal' method to update internal data | |
106 | // structures accordingly. | |
107 | RegID allocate(VirtualRegister &spillMe) | |
108 | { | |
109 | uint32_t currentLowest = NUM_REGS; | |
110 | SpillHint currentSpillOrder = SpillHintInvalid; | |
111 | ||
14957cd0 A |
112 | // This loop is broken into two halves, looping from the last allocated |
113 | // register (the register returned last time this method was called) to | |
114 | // the maximum register value, then from 0 to the last allocated. | |
115 | // This implements a simple round-robin like approach to try to reduce | |
116 | // thrash, and minimize time spent scanning locked registers in allocation. | |
117 | // If a unlocked and unnamed register is found return it immediately. | |
118 | // Otherwise, find the first unlocked register with the lowest spillOrder. | |
93a37866 | 119 | for (uint32_t i = 0 ; i < NUM_REGS; ++i) { |
14957cd0 A |
120 | // (1) If the current register is locked, it is not a candidate. |
121 | if (m_data[i].lockCount) | |
122 | continue; | |
123 | // (2) If the current register's spill order is 0, pick this! – unassigned registers have spill order 0. | |
124 | SpillHint spillOrder = m_data[i].spillOrder; | |
125 | if (spillOrder == SpillHintInvalid) | |
126 | return allocateInternal(i, spillMe); | |
127 | // If this register is better (has a lower spill order value) than any prior | |
128 | // candidate, then record it. | |
129 | if (spillOrder < currentSpillOrder) { | |
130 | currentSpillOrder = spillOrder; | |
131 | currentLowest = i; | |
132 | } | |
133 | } | |
14957cd0 A |
134 | |
135 | // Deadlock check - this could only occur is all registers are locked! | |
136 | ASSERT(currentLowest != NUM_REGS && currentSpillOrder != SpillHintInvalid); | |
137 | // There were no available registers; currentLowest will need to be spilled. | |
138 | return allocateInternal(currentLowest, spillMe); | |
139 | } | |
140 | ||
6fe7ccc8 A |
141 | // Allocates the given register, even if this will force a spill. |
142 | VirtualRegister allocateSpecific(RegID reg) | |
143 | { | |
144 | unsigned index = BankInfo::toIndex(reg); | |
145 | ||
146 | ++m_data[index].lockCount; | |
147 | VirtualRegister name = nameAtIndex(index); | |
148 | if (name != InvalidVirtualRegister) | |
149 | releaseAtIndex(index); | |
150 | ||
151 | return name; | |
152 | } | |
153 | ||
14957cd0 A |
154 | // retain/release - these methods are used to associate/disassociate names |
155 | // with values in registers. retain should only be called on locked registers. | |
156 | void retain(RegID reg, VirtualRegister name, SpillHint spillOrder) | |
157 | { | |
158 | unsigned index = BankInfo::toIndex(reg); | |
159 | ||
160 | // SpillHint must be valid. | |
161 | ASSERT(spillOrder != SpillHintInvalid); | |
162 | // 'index' must be a valid, locked register. | |
163 | ASSERT(index < NUM_REGS); | |
164 | ASSERT(m_data[index].lockCount); | |
165 | // 'index' should not currently be named, the new name must be valid. | |
166 | ASSERT(m_data[index].name == InvalidVirtualRegister); | |
167 | ASSERT(name != InvalidVirtualRegister); | |
168 | // 'index' should not currently have a spillOrder. | |
169 | ASSERT(m_data[index].spillOrder == SpillHintInvalid); | |
170 | ||
171 | m_data[index].name = name; | |
172 | m_data[index].spillOrder = spillOrder; | |
173 | } | |
174 | void release(RegID reg) | |
175 | { | |
176 | releaseAtIndex(BankInfo::toIndex(reg)); | |
177 | } | |
178 | ||
179 | // lock/unlock register, ensures that they are not spilled. | |
180 | void lock(RegID reg) | |
181 | { | |
182 | unsigned index = BankInfo::toIndex(reg); | |
183 | ||
184 | ASSERT(index < NUM_REGS); | |
185 | ++m_data[index].lockCount; | |
186 | ASSERT(m_data[index].lockCount); | |
187 | } | |
188 | void unlock(RegID reg) | |
189 | { | |
190 | unsigned index = BankInfo::toIndex(reg); | |
191 | ||
192 | ASSERT(index < NUM_REGS); | |
193 | ASSERT(m_data[index].lockCount); | |
194 | --m_data[index].lockCount; | |
195 | } | |
196 | bool isLocked(RegID reg) const | |
197 | { | |
198 | return isLockedAtIndex(BankInfo::toIndex(reg)); | |
199 | } | |
200 | ||
201 | // Get the name (VirtualRegister) associated with the | |
202 | // given register (or InvalidVirtualRegister for none). | |
203 | VirtualRegister name(RegID reg) const | |
204 | { | |
205 | return nameAtIndex(BankInfo::toIndex(reg)); | |
206 | } | |
207 | ||
93a37866 A |
208 | bool isInUse(RegID reg) const |
209 | { | |
210 | return isLocked(reg) || name(reg) != InvalidVirtualRegister; | |
211 | } | |
212 | ||
14957cd0 A |
213 | #ifndef NDEBUG |
214 | void dump() | |
215 | { | |
216 | // For each register, print the VirtualRegister 'name'. | |
217 | for (uint32_t i =0; i < NUM_REGS; ++i) { | |
218 | if (m_data[i].name != InvalidVirtualRegister) | |
93a37866 | 219 | dataLogF("[%02d]", m_data[i].name); |
14957cd0 | 220 | else |
93a37866 | 221 | dataLogF("[--]"); |
14957cd0 | 222 | } |
93a37866 | 223 | dataLogF("\n"); |
14957cd0 A |
224 | } |
225 | #endif | |
226 | ||
227 | class iterator { | |
228 | friend class RegisterBank<BankInfo>; | |
229 | public: | |
230 | VirtualRegister name() const | |
231 | { | |
232 | return m_bank->nameAtIndex(m_index); | |
233 | } | |
234 | ||
235 | bool isLocked() const | |
236 | { | |
237 | return m_bank->isLockedAtIndex(m_index); | |
238 | } | |
239 | ||
240 | void release() const | |
241 | { | |
242 | m_bank->releaseAtIndex(m_index); | |
243 | } | |
244 | ||
245 | RegID regID() const | |
246 | { | |
247 | return BankInfo::toRegister(m_index); | |
248 | } | |
249 | ||
250 | #ifndef NDEBUG | |
251 | const char* debugName() const | |
252 | { | |
253 | return BankInfo::debugName(regID()); | |
254 | } | |
255 | #endif | |
256 | ||
257 | iterator& operator++() | |
258 | { | |
259 | ++m_index; | |
260 | return *this; | |
261 | } | |
262 | ||
263 | bool operator!=(const iterator& other) const | |
264 | { | |
265 | ASSERT(m_bank == other.m_bank); | |
266 | return m_index != other.m_index; | |
267 | } | |
268 | ||
269 | unsigned index() const | |
270 | { | |
271 | return m_index; | |
272 | } | |
273 | ||
274 | private: | |
275 | iterator(RegisterBank<BankInfo>* bank, unsigned index) | |
276 | : m_bank(bank) | |
277 | , m_index(index) | |
278 | { | |
279 | } | |
280 | ||
281 | RegisterBank<BankInfo>* m_bank; | |
282 | unsigned m_index; | |
283 | }; | |
284 | ||
285 | iterator begin() | |
286 | { | |
287 | return iterator(this, 0); | |
288 | } | |
289 | ||
290 | iterator end() | |
291 | { | |
292 | return iterator(this, NUM_REGS); | |
293 | } | |
294 | ||
295 | private: | |
296 | bool isLockedAtIndex(unsigned index) const | |
297 | { | |
298 | ASSERT(index < NUM_REGS); | |
299 | return m_data[index].lockCount; | |
300 | } | |
301 | ||
302 | VirtualRegister nameAtIndex(unsigned index) const | |
303 | { | |
304 | ASSERT(index < NUM_REGS); | |
305 | return m_data[index].name; | |
306 | } | |
307 | ||
308 | void releaseAtIndex(unsigned index) | |
309 | { | |
310 | // 'index' must be a valid register. | |
311 | ASSERT(index < NUM_REGS); | |
312 | // 'index' should currently be named. | |
313 | ASSERT(m_data[index].name != InvalidVirtualRegister); | |
314 | // 'index' should currently have a valid spill order. | |
315 | ASSERT(m_data[index].spillOrder != SpillHintInvalid); | |
316 | ||
317 | m_data[index].name = InvalidVirtualRegister; | |
318 | m_data[index].spillOrder = SpillHintInvalid; | |
319 | } | |
320 | ||
321 | // Used by 'allocate', above, to update inforamtion in the map. | |
322 | RegID allocateInternal(uint32_t i, VirtualRegister &spillMe) | |
323 | { | |
324 | // 'i' must be a valid, unlocked register. | |
325 | ASSERT(i < NUM_REGS && !m_data[i].lockCount); | |
326 | ||
327 | // Return the VirtualRegister of the named value currently stored in | |
328 | // the register being returned - or InvalidVirtualRegister if none. | |
329 | spillMe = m_data[i].name; | |
330 | ||
331 | // Clear any name/spillOrder currently associated with the register, | |
332 | m_data[i] = MapEntry(); | |
333 | // Mark the register as locked (with a lock count of 1). | |
334 | m_data[i].lockCount = 1; | |
335 | ||
14957cd0 A |
336 | return BankInfo::toRegister(i); |
337 | } | |
338 | ||
339 | // === MapEntry === | |
340 | // | |
341 | // This structure provides information for an individual machine register | |
342 | // being managed by the RegisterBank. For each register we track a lock | |
343 | // count, name and spillOrder hint. | |
344 | struct MapEntry { | |
345 | MapEntry() | |
346 | : name(InvalidVirtualRegister) | |
347 | , spillOrder(SpillHintInvalid) | |
348 | , lockCount(0) | |
349 | { | |
350 | } | |
351 | ||
352 | VirtualRegister name; | |
353 | SpillHint spillOrder; | |
354 | uint32_t lockCount; | |
355 | }; | |
356 | ||
357 | // Holds the current status of all registers. | |
358 | MapEntry m_data[NUM_REGS]; | |
14957cd0 A |
359 | }; |
360 | ||
361 | } } // namespace JSC::DFG | |
362 | ||
363 | #endif | |
364 | #endif |