]>
git.saurik.com Git - apple/javascriptcore.git/blob - dfg/DFGScoreBoard.h
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef DFGScoreBoard_h
27 #define DFGScoreBoard_h
31 #include <dfg/DFGGraph.h>
32 #include <wtf/Vector.h>
34 namespace JSC
{ namespace DFG
{
38 // This class is used in performing a virtual register allocation over the graph.
39 // VirtualRegisters are allocated to nodes, with a used count for each virtual
40 // register tracking the lifespan of the value; after the final use of a node
41 // the VirtualRegister associated is freed such that it can be reused for
45 ScoreBoard(Graph
& graph
, uint32_t firstTemporary
)
47 , m_firstTemporary(firstTemporary
)
51 #if DFG_CONSISTENCY_CHECK
54 // Every VirtualRegister that was allocated should now be free.
55 ASSERT(m_used
.size() == m_free
.size());
56 // Every entry in the used list should be available in the free list.
57 for (size_t i
= 0; i
< m_used
.size(); ++i
)
58 ASSERT(m_free
.contains(i
));
59 // For every entry in the used list the use count of the virtual register should be zero.
60 for (size_t i
= 0; i
< m_free
.size(); ++i
)
65 VirtualRegister
allocate()
67 // Do we have any VirtualRegsiters in the free list, that were used by
68 // prior nodes, but are now available?
69 if (!m_free
.isEmpty()) {
70 uint32_t index
= m_free
.last();
72 // Use count must have hit zero for it to have been added to the free list!
73 ASSERT(!m_used
[index
]);
74 return (VirtualRegister
)(m_firstTemporary
+ index
);
77 // Allocate a new VirtualRegister, and add a corresponding entry to m_used.
78 size_t next
= allocatedCount();
80 return (VirtualRegister
)(m_firstTemporary
+ next
);
83 // Increment the usecount for the VirtualRegsiter associated with 'child',
84 // if it reaches the node's refcount, free the VirtualRegsiter.
85 void use(NodeIndex child
)
90 // Find the virtual register number for this child, increment its use count.
91 Node
& node
= m_graph
[child
];
92 uint32_t index
= node
.virtualRegister() - m_firstTemporary
;
93 if (node
.refCount() == ++m_used
[index
]) {
94 // If the use count in the scoreboard reaches the use count for the node,
95 // then this was its last use; the virtual register is now free.
96 // Clear the use count & add to the free list.
102 unsigned allocatedCount()
104 // m_used contains an entry for every allocated VirtualRegister.
105 return m_used
.size();
112 for (unsigned i
= 0; i
< m_used
.size(); ++i
) {
113 if (!m_free
.contains(i
))
114 printf("%d:%d ", m_firstTemporary
+ i
, m_used
[i
]);
119 for (unsigned i
= 0; i
< m_used
.size(); ++i
) {
120 if (m_free
.contains(i
)) {
122 printf("%d ", m_firstTemporary
+ i
);
131 // The graph, so we can get refCounts for nodes, to determine when values are dead.
133 // The first VirtualRegsiter to be used as a temporary.
134 uint32_t m_firstTemporary
;
136 // For every virtual register that has been allocated (either currently alive, or in
137 // the free list), we keep a count of the number of remaining uses until it is dead
138 // (0, in the case of entries in the free list). Since there is an entry for every
139 // allocated VirtualRegister, the length of this array conveniently provides the
140 // next available VirtualRegister number.
141 Vector
<uint32_t, 64> m_used
;
142 // A free list of VirtualRegsiters no longer alive.
143 Vector
<uint32_t, 64> m_free
;
146 } } // namespace JSC::DFG