]> git.saurik.com Git - apple/javascriptcore.git/blob - dfg/DFGSSACalculator.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / dfg / DFGSSACalculator.cpp
1 /*
2 * Copyright (C) 2014 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 #include "config.h"
27 #include "DFGSSACalculator.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "DFGBlockMapInlines.h"
32 #include <wtf/CommaPrinter.h>
33 #include <wtf/ListDump.h>
34
35 namespace JSC { namespace DFG {
36
37 void SSACalculator::Variable::dump(PrintStream& out) const
38 {
39 out.print("var", m_index);
40 }
41
42 void SSACalculator::Variable::dumpVerbose(PrintStream& out) const
43 {
44 dump(out);
45 if (!m_blocksWithDefs.isEmpty()) {
46 out.print("(defs: ");
47 CommaPrinter comma;
48 for (BasicBlock* block : m_blocksWithDefs)
49 out.print(comma, *block);
50 out.print(")");
51 }
52 }
53
54 void SSACalculator::Def::dump(PrintStream& out) const
55 {
56 out.print("def(", *m_variable, ", ", *m_block, ", ", m_value, ")");
57 }
58
59 SSACalculator::SSACalculator(Graph& graph)
60 : m_data(graph)
61 , m_graph(graph)
62 {
63 }
64
65 SSACalculator::~SSACalculator()
66 {
67 }
68
69 void SSACalculator::reset()
70 {
71 m_variables.clear();
72 m_defs.clear();
73 m_phis.clear();
74 for (BlockIndex blockIndex = m_data.size(); blockIndex--;) {
75 m_data[blockIndex].m_defs.clear();
76 m_data[blockIndex].m_phis.clear();
77 }
78 }
79
80 SSACalculator::Variable* SSACalculator::newVariable()
81 {
82 return &m_variables.alloc(Variable(m_variables.size()));
83 }
84
85 SSACalculator::Def* SSACalculator::newDef(Variable* variable, BasicBlock* block, Node* value)
86 {
87 Def* def = m_defs.add(Def(variable, block, value));
88 auto result = m_data[block].m_defs.add(variable, def);
89 if (result.isNewEntry)
90 variable->m_blocksWithDefs.append(block);
91 else
92 result.iterator->value = def;
93 return def;
94 }
95
96 SSACalculator::Def* SSACalculator::nonLocalReachingDef(BasicBlock* block, Variable* variable)
97 {
98 return reachingDefAtTail(m_graph.m_dominators.immediateDominatorOf(block), variable);
99 }
100
101 SSACalculator::Def* SSACalculator::reachingDefAtTail(BasicBlock* block, Variable* variable)
102 {
103 for (; block; block = m_graph.m_dominators.immediateDominatorOf(block)) {
104 if (Def* def = m_data[block].m_defs.get(variable))
105 return def;
106 }
107 return nullptr;
108 }
109
110 void SSACalculator::dump(PrintStream& out) const
111 {
112 out.print("<Variables: [");
113 CommaPrinter comma;
114 for (unsigned i = 0; i < m_variables.size(); ++i) {
115 out.print(comma);
116 m_variables[i].dumpVerbose(out);
117 }
118 out.print("], Defs: [");
119 comma = CommaPrinter();
120 for (Def* def : const_cast<SSACalculator*>(this)->m_defs)
121 out.print(comma, *def);
122 out.print("], Phis: [");
123 comma = CommaPrinter();
124 for (Def* def : const_cast<SSACalculator*>(this)->m_phis)
125 out.print(comma, *def);
126 out.print("], Block data: [");
127 comma = CommaPrinter();
128 for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex) {
129 BasicBlock* block = m_graph.block(blockIndex);
130 if (!block)
131 continue;
132
133 out.print(comma, *block, "=>(");
134 out.print("Defs: {");
135 CommaPrinter innerComma;
136 for (auto entry : m_data[block].m_defs)
137 out.print(innerComma, *entry.key, "->", *entry.value);
138 out.print("}, Phis: {");
139 innerComma = CommaPrinter();
140 for (Def* def : m_data[block].m_phis)
141 out.print(innerComma, *def);
142 out.print("})");
143 }
144 out.print("]>");
145 }
146
147 } } // namespace JSC::DFG
148
149 #endif // ENABLE(DFG_JIT)
150