]>
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 DFGScoreBoard_h | |
27 | #define DFGScoreBoard_h | |
28 | ||
29 | #if ENABLE(DFG_JIT) | |
30 | ||
6fe7ccc8 A |
31 | #include "DFGGraph.h" |
32 | #include <wtf/BitVector.h> | |
14957cd0 A |
33 | #include <wtf/Vector.h> |
34 | ||
35 | namespace JSC { namespace DFG { | |
36 | ||
37 | // === ScoreBoard === | |
38 | // | |
39 | // This class is used in performing a virtual register allocation over the graph. | |
40 | // VirtualRegisters are allocated to nodes, with a used count for each virtual | |
41 | // register tracking the lifespan of the value; after the final use of a node | |
42 | // the VirtualRegister associated is freed such that it can be reused for | |
43 | // another node. | |
44 | class ScoreBoard { | |
45 | public: | |
81345200 A |
46 | ScoreBoard(unsigned nextMachineLocal) |
47 | : m_highWatermark(nextMachineLocal + 1) | |
14957cd0 | 48 | { |
81345200 A |
49 | m_used.fill(max(), nextMachineLocal); |
50 | m_free.reserveCapacity(nextMachineLocal); | |
14957cd0 A |
51 | } |
52 | ||
14957cd0 A |
53 | ~ScoreBoard() |
54 | { | |
6fe7ccc8 A |
55 | assertClear(); |
56 | } | |
57 | ||
ed1e77d3 A |
58 | void sortFree() |
59 | { | |
60 | std::sort(m_free.begin(), m_free.end()); | |
61 | } | |
62 | ||
6fe7ccc8 A |
63 | void assertClear() |
64 | { | |
ed1e77d3 A |
65 | if (ASSERT_DISABLED) |
66 | return; | |
67 | ||
6fe7ccc8 | 68 | // For every entry in the used list the use count of the virtual register should be zero, or max, due to it being a preserved local. |
14957cd0 | 69 | for (size_t i = 0; i < m_used.size(); ++i) |
ed1e77d3 | 70 | RELEASE_ASSERT(!m_used[i] || m_used[i] == max()); |
6fe7ccc8 | 71 | // For every entry in the free list, the use count should be zero. |
14957cd0 | 72 | for (size_t i = 0; i < m_free.size(); ++i) |
ed1e77d3 | 73 | RELEASE_ASSERT(!m_used[m_free[i]]); |
6fe7ccc8 A |
74 | // There must not be duplicates in the free list. |
75 | for (size_t i = 0; i < m_free.size(); ++i) { | |
76 | for (size_t j = i + 1; j < m_free.size(); ++j) | |
ed1e77d3 | 77 | RELEASE_ASSERT(m_free[i] != m_free[j]); |
6fe7ccc8 | 78 | } |
6fe7ccc8 | 79 | } |
14957cd0 A |
80 | |
81 | VirtualRegister allocate() | |
82 | { | |
83 | // Do we have any VirtualRegsiters in the free list, that were used by | |
84 | // prior nodes, but are now available? | |
85 | if (!m_free.isEmpty()) { | |
86 | uint32_t index = m_free.last(); | |
87 | m_free.removeLast(); | |
88 | // Use count must have hit zero for it to have been added to the free list! | |
89 | ASSERT(!m_used[index]); | |
6fe7ccc8 | 90 | m_highWatermark = std::max(m_highWatermark, static_cast<unsigned>(index) + 1); |
81345200 | 91 | return virtualRegisterForLocal(index); |
14957cd0 A |
92 | } |
93 | ||
94 | // Allocate a new VirtualRegister, and add a corresponding entry to m_used. | |
6fe7ccc8 | 95 | size_t next = m_used.size(); |
14957cd0 | 96 | m_used.append(0); |
6fe7ccc8 | 97 | m_highWatermark = std::max(m_highWatermark, static_cast<unsigned>(next) + 1); |
81345200 | 98 | return virtualRegisterForLocal(next); |
14957cd0 A |
99 | } |
100 | ||
81345200 A |
101 | // Increment the usecount for the VirtualRegister associated with 'child', |
102 | // if it reaches the node's refcount, free the VirtualRegister. | |
93a37866 | 103 | void use(Node* child) |
14957cd0 | 104 | { |
93a37866 | 105 | if (!child) |
14957cd0 A |
106 | return; |
107 | ||
108 | // Find the virtual register number for this child, increment its use count. | |
81345200 | 109 | uint32_t index = child->virtualRegister().toLocal(); |
6fe7ccc8 | 110 | ASSERT(m_used[index] != max()); |
93a37866 | 111 | if (child->refCount() == ++m_used[index]) { |
14957cd0 A |
112 | // If the use count in the scoreboard reaches the use count for the node, |
113 | // then this was its last use; the virtual register is now free. | |
114 | // Clear the use count & add to the free list. | |
115 | m_used[index] = 0; | |
116 | m_free.append(index); | |
117 | } | |
118 | } | |
6fe7ccc8 | 119 | void use(Edge child) |
14957cd0 | 120 | { |
93a37866 A |
121 | use(child.node()); |
122 | } | |
123 | ||
124 | void useIfHasResult(Edge child) | |
125 | { | |
126 | if (!child) | |
127 | return; | |
128 | if (!child->hasResult()) | |
129 | return; | |
130 | use(child); | |
14957cd0 A |
131 | } |
132 | ||
6fe7ccc8 A |
133 | unsigned highWatermark() |
134 | { | |
135 | return m_highWatermark; | |
136 | } | |
137 | ||
14957cd0 A |
138 | #ifndef NDEBUG |
139 | void dump() | |
140 | { | |
93a37866 | 141 | dataLogF(" USED: [ "); |
14957cd0 | 142 | for (unsigned i = 0; i < m_used.size(); ++i) { |
6fe7ccc8 | 143 | if (!m_free.contains(i)) { |
93a37866 | 144 | dataLogF("%d:", i); |
6fe7ccc8 | 145 | if (m_used[i] == max()) |
93a37866 | 146 | dataLogF("local "); |
6fe7ccc8 | 147 | else |
93a37866 | 148 | dataLogF("%d ", m_used[i]); |
6fe7ccc8 | 149 | } |
14957cd0 | 150 | } |
93a37866 | 151 | dataLogF("]\n"); |
14957cd0 | 152 | |
93a37866 | 153 | dataLogF(" FREE: [ "); |
14957cd0 | 154 | for (unsigned i = 0; i < m_used.size(); ++i) { |
6fe7ccc8 | 155 | if (m_free.contains(i) && m_used[i] != max()) { |
14957cd0 | 156 | ASSERT(!m_used[i]); |
93a37866 | 157 | dataLogF("%d ", i); |
14957cd0 A |
158 | } |
159 | } | |
93a37866 | 160 | dataLogF("]\n"); |
14957cd0 A |
161 | } |
162 | ||
163 | #endif | |
164 | ||
165 | private: | |
6fe7ccc8 A |
166 | static uint32_t max() { return std::numeric_limits<uint32_t>::max(); } |
167 | ||
6fe7ccc8 A |
168 | // The size of the span of virtual registers that this code block will use. |
169 | unsigned m_highWatermark; | |
14957cd0 A |
170 | |
171 | // For every virtual register that has been allocated (either currently alive, or in | |
172 | // the free list), we keep a count of the number of remaining uses until it is dead | |
173 | // (0, in the case of entries in the free list). Since there is an entry for every | |
174 | // allocated VirtualRegister, the length of this array conveniently provides the | |
175 | // next available VirtualRegister number. | |
176 | Vector<uint32_t, 64> m_used; | |
177 | // A free list of VirtualRegsiters no longer alive. | |
178 | Vector<uint32_t, 64> m_free; | |
179 | }; | |
180 | ||
181 | } } // namespace JSC::DFG | |
182 | ||
183 | #endif | |
184 | #endif |