]>
Commit | Line | Data |
---|---|---|
ed1e77d3 A |
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 | #ifndef DFGSSACalculator_h | |
27 | #define DFGSSACalculator_h | |
28 | ||
29 | #if ENABLE(DFG_JIT) | |
30 | ||
31 | #include "DFGDominators.h" | |
32 | #include "DFGGraph.h" | |
33 | ||
34 | namespace JSC { namespace DFG { | |
35 | ||
36 | // SSACalculator provides a reusable tool for using the Cytron, Ferrante, Rosen, Wegman, and | |
37 | // Zadeck "Efficiently Computing Static Single Assignment Form and the Control Dependence Graph" | |
38 | // (TOPLAS'91) algorithm for computing SSA. SSACalculator doesn't magically do everything for you | |
39 | // but it maintains the major data structures and handles most of the non-local reasoning. Here's | |
40 | // the workflow of using SSACalculator to execute this algorithm: | |
41 | // | |
42 | // 0) Create a fresh SSACalculator instance. You will need this instance only for as long as | |
43 | // you're not yet done computing SSA. | |
44 | // | |
45 | // 1) Create an SSACalculator::Variable for every variable that you want to do Phi insertion | |
46 | // on. SSACalculator::Variable::index() is a dense indexing of the Variables that you | |
47 | // created, so you can easily use a Vector to map the SSACalculator::Variables to your | |
48 | // variables. | |
49 | // | |
50 | // 2) Create a SSACalculator::Def for every assignment to those variables. A Def knows about the | |
51 | // variable, the block, and the DFG::Node* that has the value being put into the variable. | |
52 | // Note that creating a Def in block B for variable V if block B already has a def for variable | |
53 | // V will overwrite the previous Def's DFG::Node* value. This enables you to create Defs by | |
54 | // processing basic blocks in forward order. If a block has multiple Defs of a variable, this | |
55 | // "just works" because each block will then remember the last Def of each variable. | |
56 | // | |
57 | // 3) Call SSACalculator::computePhis(). This takes a functor that will create the Phi nodes. The | |
58 | // functor returns either the Phi node it created, or nullptr, if it chooses to prune. (As an | |
59 | // aside, it's always sound not to prune, and the safest reason for pruning is liveness.) The | |
60 | // computePhis() code will record the created Phi nodes as Defs, and it will separately record | |
61 | // the list of Phis inserted at each block. It's OK for the functor you pass here to modify the | |
62 | // DFG::Graph on the fly, but the easiest way to write this is to just create the Phi nodes by | |
63 | // doing Graph::addNode() and return them. It's then best to insert all Phi nodes for a block | |
64 | // in bulk as part of the pass you do below, in step (4). | |
65 | // | |
66 | // 4) Modify the graph to create the SSA data flow. For each block, this should: | |
67 | // | |
68 | // 4.0) Compute the set of reaching defs (aka available values) for each variable by calling | |
69 | // SSACalculator::reachingDefAtHead() for each variable. Record this in a local table that | |
70 | // will be incrementally updated as you proceed through the block in forward order in the | |
71 | // next steps: | |
72 | // | |
73 | // FIXME: It might be better to compute reaching defs for all live variables in one go, to | |
74 | // avoid doing repeated dom tree traversals. | |
75 | // https://bugs.webkit.org/show_bug.cgi?id=136610 | |
76 | // | |
77 | // 4.1) Insert all of the Phi nodes for the block by using SSACalculator::phisForBlock(), and | |
78 | // record those Phi nodes as being available values. | |
79 | // | |
80 | // 4.2) Process the block in forward order. For each load from a variable, replace it with the | |
81 | // available SSA value for that variable. For each store, delete it and record the stored | |
82 | // value as being available. | |
83 | // | |
84 | // Note that you have two options of how to replace loads with SSA values. You can replace | |
85 | // the load with an Identity node; this will end up working fairly naturally so long as | |
86 | // you run GCSE after your phase. Or, you can replace all uses of the load with the SSA | |
87 | // value yourself (using the Graph::performSubstitution() idiom), but that requires that | |
88 | // your loop over basic blocks proceeds in the appropriate graph order, for example | |
89 | // preorder. | |
90 | // | |
91 | // FIXME: Make it easier to do this, that doesn't involve rerunning GCSE. | |
92 | // https://bugs.webkit.org/show_bug.cgi?id=136639 | |
93 | // | |
94 | // 4.3) Insert Upsilons for each Phi in each successor block. Use the available values table to | |
95 | // decide the source value for each Phi's variable. Note that you could also use | |
96 | // SSACalculator::reachingDefAtTail() instead of the available values table, though your | |
97 | // local available values table is likely to be more efficient. | |
98 | // | |
99 | // The most obvious use of SSACalculator is for the CPS->SSA conversion itself, but it's meant to | |
100 | // also be used for SSA update and for things like the promotion of heap fields to local SSA | |
101 | // variables. | |
102 | ||
103 | class SSACalculator { | |
104 | public: | |
105 | SSACalculator(Graph&); | |
106 | ~SSACalculator(); | |
107 | ||
108 | void reset(); | |
109 | ||
110 | class Variable { | |
111 | public: | |
112 | unsigned index() const { return m_index; } | |
113 | ||
114 | void dump(PrintStream&) const; | |
115 | void dumpVerbose(PrintStream&) const; | |
116 | ||
117 | private: | |
118 | friend class SSACalculator; | |
119 | ||
120 | Variable() | |
121 | : m_index(UINT_MAX) | |
122 | { | |
123 | } | |
124 | ||
125 | Variable(unsigned index) | |
126 | : m_index(index) | |
127 | { | |
128 | } | |
129 | ||
130 | BlockList m_blocksWithDefs; | |
131 | unsigned m_index; | |
132 | }; | |
133 | ||
134 | class Def { | |
135 | public: | |
136 | Variable* variable() const { return m_variable; } | |
137 | BasicBlock* block() const { return m_block; } | |
138 | ||
139 | Node* value() const { return m_value; } | |
140 | ||
141 | void dump(PrintStream&) const; | |
142 | ||
143 | private: | |
144 | friend class SSACalculator; | |
145 | ||
146 | Def() | |
147 | : m_variable(nullptr) | |
148 | , m_block(nullptr) | |
149 | , m_value(nullptr) | |
150 | { | |
151 | } | |
152 | ||
153 | Def(Variable* variable, BasicBlock* block, Node* value) | |
154 | : m_variable(variable) | |
155 | , m_block(block) | |
156 | , m_value(value) | |
157 | { | |
158 | } | |
159 | ||
160 | Variable* m_variable; | |
161 | BasicBlock* m_block; | |
162 | Node* m_value; | |
163 | }; | |
164 | ||
165 | Variable* newVariable(); | |
166 | Def* newDef(Variable*, BasicBlock*, Node*); | |
167 | ||
168 | Variable* variable(unsigned index) { return &m_variables[index]; } | |
169 | ||
170 | // The PhiInsertionFunctor takes a Variable and a BasicBlock and either inserts a Phi and | |
171 | // returns the Node for that Phi, or it decides that it's not worth it to insert a Phi at that | |
172 | // block because of some additional pruning condition (typically liveness) and returns | |
173 | // nullptr. If a non-null Node* is returned, a new Def is created, so that | |
174 | // nonLocalReachingDef() will find it later. Note that it is generally always sound to not | |
175 | // prune any Phis (that is, to always have the functor insert a Phi and never return nullptr). | |
176 | template<typename PhiInsertionFunctor> | |
177 | void computePhis(const PhiInsertionFunctor& functor) | |
178 | { | |
179 | DFG_ASSERT(m_graph, nullptr, m_graph.m_dominators.isValid()); | |
180 | ||
181 | for (Variable& variable : m_variables) { | |
182 | m_graph.m_dominators.forAllBlocksInPrunedIteratedDominanceFrontierOf( | |
183 | variable.m_blocksWithDefs, | |
184 | [&] (BasicBlock* block) -> bool { | |
185 | Node* phiNode = functor(&variable, block); | |
186 | if (!phiNode) | |
187 | return false; | |
188 | ||
189 | BlockData& data = m_data[block]; | |
190 | Def* phiDef = m_phis.add(Def(&variable, block, phiNode)); | |
191 | data.m_phis.append(phiDef); | |
192 | ||
193 | // Note that it's possible to have a block that looks like this before SSA | |
194 | // conversion: | |
195 | // | |
196 | // label: | |
197 | // print(x); | |
198 | // ... | |
199 | // x = 42; | |
200 | // goto label; | |
201 | // | |
202 | // And it may look like this after SSA conversion: | |
203 | // | |
204 | // label: | |
205 | // x1: Phi() | |
206 | // ... | |
207 | // Upsilon(42, ^x1) | |
208 | // goto label; | |
209 | // | |
210 | // In this case, we will want to insert a Phi in this block, and the block | |
211 | // will already have a Def for the variable. When this happens, we don't want | |
212 | // the Phi to override the original Def, since the Phi is at the top, the | |
213 | // original Def in the m_defs table would have been at the bottom, and we want | |
214 | // m_defs to tell us about defs at tail. | |
215 | // | |
216 | // So, we rely on the fact that HashMap::add() does nothing if the key was | |
217 | // already present. | |
218 | data.m_defs.add(&variable, phiDef); | |
219 | return true; | |
220 | }); | |
221 | } | |
222 | } | |
223 | ||
224 | const Vector<Def*>& phisForBlock(BasicBlock* block) | |
225 | { | |
226 | return m_data[block].m_phis; | |
227 | } | |
228 | ||
229 | // Ignores defs within the given block; it assumes that you've taken care of those | |
230 | // yourself. | |
231 | Def* nonLocalReachingDef(BasicBlock*, Variable*); | |
232 | Def* reachingDefAtHead(BasicBlock* block, Variable* variable) | |
233 | { | |
234 | return nonLocalReachingDef(block, variable); | |
235 | } | |
236 | ||
237 | // Considers the def within the given block, but only works at the tail of the block. | |
238 | Def* reachingDefAtTail(BasicBlock*, Variable*); | |
239 | ||
240 | void dump(PrintStream&) const; | |
241 | ||
242 | private: | |
243 | SegmentedVector<Variable> m_variables; | |
244 | Bag<Def> m_defs; | |
245 | ||
246 | Bag<Def> m_phis; | |
247 | ||
248 | struct BlockData { | |
249 | HashMap<Variable*, Def*> m_defs; | |
250 | Vector<Def*> m_phis; | |
251 | }; | |
252 | ||
253 | BlockMap<BlockData> m_data; | |
254 | ||
255 | Graph& m_graph; | |
256 | }; | |
257 | ||
258 | } } // namespace JSC::DFG | |
259 | ||
260 | #endif // ENABLE(DFG_JIT) | |
261 | ||
262 | #endif // DFGSSACalculator_h | |
263 |