1/*
2 * Copyright (C) 2016-2017 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#pragma once
27
28#if ENABLE(B3_JIT)
29
30#include "B3Dominators.h"
31#include "B3ProcedureInlines.h"
32#include <wtf/Bag.h>
33#include <wtf/IndexMap.h>
34#include <wtf/SegmentedVector.h>
35
36namespace JSC { namespace B3 {
37
38// SSACalculator provides a reusable tool for building SSA's. It's modeled after
39// DFG::SSACalculator.
40
41class SSACalculator {
42public:
43 SSACalculator(Procedure&);
44 ~SSACalculator();
45
46 void reset();
47
48 class Variable {
49 public:
50 unsigned index() const { return m_index; }
51
52 void dump(PrintStream&) const;
53 void dumpVerbose(PrintStream&) const;
54
55 private:
56 friend class SSACalculator;
57
58 Variable()
59 : m_index(UINT_MAX)
60 {
61 }
62
63 Variable(unsigned index)
64 : m_index(index)
65 {
66 }
67
68 Vector<BasicBlock*, 4> m_blocksWithDefs;
69 unsigned m_index;
70 };
71
72 class Def {
73 public:
74 Variable* variable() const { return m_variable; }
75 BasicBlock* block() const { return m_block; }
76
77 Value* value() const { return m_value; }
78
79 void dump(PrintStream&) const;
80
81 private:
82 friend class SSACalculator;
83
84 Def()
85 : m_variable(nullptr)
86 , m_block(nullptr)
87 , m_value(nullptr)
88 {
89 }
90
91 Def(Variable* variable, BasicBlock* block, Value* value)
92 : m_variable(variable)
93 , m_block(block)
94 , m_value(value)
95 {
96 }
97
98 Variable* m_variable;
99 BasicBlock* m_block;
100 Value* m_value;
101 };
102
103 Variable* newVariable();
104 Def* newDef(Variable*, BasicBlock*, Value*);
105
106 Variable* variable(unsigned index) { return &m_variables[index]; }
107
108 template<typename Functor>
109 void computePhis(const Functor& functor)
110 {
111 m_dominators = &m_proc.dominators();
112 for (Variable& variable : m_variables) {
113 m_dominators->forAllBlocksInPrunedIteratedDominanceFrontierOf(
114 variable.m_blocksWithDefs,
115 [&] (BasicBlock* block) -> bool {
116 Value* phi = functor(&variable, block);
117 if (!phi)
118 return false;
119
120 BlockData& data = m_data[block];
121 Def* phiDef = m_phis.add(Def(&variable, block, phi));
122 data.m_phis.append(phiDef);
123
124 data.m_defs.add(&variable, phiDef);
125 return true;
126 });
127 }
128 }
129
130 const Vector<Def*>& phisForBlock(BasicBlock* block)
131 {
132 return m_data[block].m_phis;
133 }
134
135 // Ignores defs within the given block; it assumes that you've taken care of those
136 // yourself.
137 Def* nonLocalReachingDef(BasicBlock*, Variable*);
138 Def* reachingDefAtHead(BasicBlock* block, Variable* variable)
139 {
140 return nonLocalReachingDef(block, variable);
141 }
142
143 // Considers the def within the given block, but only works at the tail of the block.
144 Def* reachingDefAtTail(BasicBlock*, Variable*);
145
146 void dump(PrintStream&) const;
147
148private:
149 SegmentedVector<Variable> m_variables;
150 Bag<Def> m_defs;
151
152 Bag<Def> m_phis;
153
154 struct BlockData {
155 HashMap<Variable*, Def*> m_defs;
156 Vector<Def*> m_phis;
157 };
158
159 IndexMap<BasicBlock*, BlockData> m_data;
160
161 Dominators* m_dominators { nullptr };
162 Procedure& m_proc;
163};
164
165} } // namespace JSC::B3
166
167#endif // ENABLE(B3_JIT)
168