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.
27 #include "DFGVirtualRegisterAllocationPhase.h"
32 #include "DFGScoreBoard.h"
34 namespace JSC { namespace DFG {
36 class VirtualRegisterAllocationPhase: public Phase {
38 VirtualRegisterAllocationPhase(Graph& graph)
39 : Phase(graph, "virtual register allocation")
45 #if DFG_ENABLE(DEBUG_VERBOSE)
46 dataLog("Preserved vars: ");
47 m_graph.m_preservedVars.dump(WTF::dataFile());
50 ScoreBoard scoreBoard(m_graph, m_graph.m_preservedVars);
51 unsigned sizeExcludingPhiNodes = m_graph.m_blocks.last()->end;
52 for (size_t i = 0; i < sizeExcludingPhiNodes; ++i) {
53 Node& node = m_graph[i];
55 if (!node.shouldGenerate())
58 // GetLocal nodes are effectively phi nodes in the graph, referencing
59 // results from prior blocks.
60 if (node.op != GetLocal) {
61 // First, call use on all of the current node's children, then
62 // allocate a VirtualRegister for this node. We do so in this
63 // order so that if a child is on its last use, and a
64 // VirtualRegister is freed, then it may be reused for node.
65 if (node.op & NodeHasVarArgs) {
66 for (unsigned childIdx = node.firstChild(); childIdx < node.firstChild() + node.numChildren(); childIdx++)
67 scoreBoard.use(m_graph.m_varArgChildren[childIdx]);
69 scoreBoard.use(node.child1());
70 scoreBoard.use(node.child2());
71 scoreBoard.use(node.child3());
75 if (!node.hasResult())
78 node.setVirtualRegister(scoreBoard.allocate());
79 // 'mustGenerate' nodes have their useCount artificially elevated,
80 // call use now to account for this.
81 if (node.mustGenerate())
85 // 'm_numCalleeRegisters' is the number of locals and temporaries allocated
86 // for the function (and checked for on entry). Since we perform a new and
87 // different allocation of temporaries, more registers may now be required.
88 unsigned calleeRegisters = scoreBoard.highWatermark() + m_graph.m_parameterSlots;
89 if ((unsigned)codeBlock()->m_numCalleeRegisters < calleeRegisters)
90 codeBlock()->m_numCalleeRegisters = calleeRegisters;
91 #if DFG_ENABLE(DEBUG_VERBOSE)
92 dataLog("Num callee registers: %u\n", calleeRegisters);
97 void performVirtualRegisterAllocation(Graph& graph)
99 runPhase<VirtualRegisterAllocationPhase>(graph);
102 } } // namespace JSC::DFG
104 #endif // ENABLE(DFG_JIT)