1/*
2 * Copyright (C) 2016 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 "B3SSACalculator.h"
28
29#if ENABLE(B3_JIT)
30
31#include "B3BasicBlockInlines.h"
32#include <wtf/CommaPrinter.h>
33#include <wtf/ListDump.h>
34
35namespace JSC { namespace B3 {
36
37void SSACalculator::Variable::dump(PrintStream& out) const
38{
39 out.print("var", m_index);
40}
41
42void 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
54void SSACalculator::Def::dump(PrintStream& out) const
55{
56 out.print("def(", *m_variable, ", ", *m_block, ", ", pointerDump(m_value), ")");
57}
58
59SSACalculator::SSACalculator(Procedure& proc)
60 : m_data(proc.size())
61 , m_proc(proc)
62{
63}
64
65SSACalculator::~SSACalculator()
66{
67}
68
69void SSACalculator::reset()
70{
71 m_variables.clear();
72 m_defs.clear();
73 m_phis.clear();
74 for (unsigned blockIndex = m_data.size(); blockIndex--;) {
75 m_data[blockIndex].m_defs.clear();
76 m_data[blockIndex].m_phis.clear();
77 }
78}
79
80SSACalculator::Variable* SSACalculator::newVariable()
81{
82 return &m_variables.alloc(Variable(m_variables.size()));
83}
84
85SSACalculator::Def* SSACalculator::newDef(Variable* variable, BasicBlock* block, Value* 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
96SSACalculator::Def* SSACalculator::nonLocalReachingDef(BasicBlock* block, Variable* variable)
97{
98 return reachingDefAtTail(m_dominators->idom(block), variable);
99}
100
101SSACalculator::Def* SSACalculator::reachingDefAtTail(BasicBlock* startingBlock, Variable* variable)
102{
103 for (BasicBlock* block = startingBlock; block; block = m_dominators->idom(block)) {
104 if (Def* def = m_data[block].m_defs.get(variable)) {
105 for (BasicBlock* otherBlock = startingBlock; otherBlock != block; otherBlock = m_dominators->idom(otherBlock))
106 m_data[block].m_defs.add(variable, def);
107 return def;
108 }
109 }
110 return nullptr;
111}
112
113void SSACalculator::dump(PrintStream& out) const
114{
115 out.print("<Variables: [");
116 CommaPrinter comma;
117 for (unsigned i = 0; i < m_variables.size(); ++i) {
118 out.print(comma);
119 m_variables[i].dumpVerbose(out);
120 }
121 out.print("], Defs: [");
122 comma = CommaPrinter();
123 for (Def* def : const_cast<SSACalculator*>(this)->m_defs)
124 out.print(comma, *def);
125 out.print("], Phis: [");
126 comma = CommaPrinter();
127 for (Def* def : const_cast<SSACalculator*>(this)->m_phis)
128 out.print(comma, *def);
129 out.print("], Block data: [");
130 comma = CommaPrinter();
131 for (unsigned blockIndex = 0; blockIndex < m_proc.size(); ++blockIndex) {
132 BasicBlock* block = m_proc[blockIndex];
133 if (!block)
134 continue;
135
136 out.print(comma, *block, "=>(");
137 out.print("Defs: {");
138 CommaPrinter innerComma;
139 for (auto entry : m_data[block].m_defs)
140 out.print(innerComma, *entry.key, "->", *entry.value);
141 out.print("}, Phis: {");
142 innerComma = CommaPrinter();
143 for (Def* def : m_data[block].m_phis)
144 out.print(innerComma, *def);
145 out.print("})");
146 }
147 out.print("]>");
148}
149
150} } // namespace JSC::B3
151
152#endif // ENABLE(B3_JIT)
153
154