2 * Copyright (C) 2014 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.
26 #ifndef DFGSSACalculator_h
27 #define DFGSSACalculator_h
31 #include "DFGDominators.h"
34 namespace JSC
{ namespace DFG
{
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:
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.
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
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.
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).
66 // 4) Modify the graph to create the SSA data flow. For each block, this should:
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
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
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.
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.
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
91 // FIXME: Make it easier to do this, that doesn't involve rerunning GCSE.
92 // https://bugs.webkit.org/show_bug.cgi?id=136639
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.
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
103 class SSACalculator
{
105 SSACalculator(Graph
&);
112 unsigned index() const { return m_index
; }
114 void dump(PrintStream
&) const;
115 void dumpVerbose(PrintStream
&) const;
118 friend class SSACalculator
;
125 Variable(unsigned index
)
130 BlockList m_blocksWithDefs
;
136 Variable
* variable() const { return m_variable
; }
137 BasicBlock
* block() const { return m_block
; }
139 Node
* value() const { return m_value
; }
141 void dump(PrintStream
&) const;
144 friend class SSACalculator
;
147 : m_variable(nullptr)
153 Def(Variable
* variable
, BasicBlock
* block
, Node
* value
)
154 : m_variable(variable
)
160 Variable
* m_variable
;
165 Variable
* newVariable();
166 Def
* newDef(Variable
*, BasicBlock
*, Node
*);
168 Variable
* variable(unsigned index
) { return &m_variables
[index
]; }
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
)
179 DFG_ASSERT(m_graph
, nullptr, m_graph
.m_dominators
.isValid());
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
);
189 BlockData
& data
= m_data
[block
];
190 Def
* phiDef
= m_phis
.add(Def(&variable
, block
, phiNode
));
191 data
.m_phis
.append(phiDef
);
193 // Note that it's possible to have a block that looks like this before SSA
202 // And it may look like this after SSA conversion:
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.
216 // So, we rely on the fact that HashMap::add() does nothing if the key was
218 data
.m_defs
.add(&variable
, phiDef
);
224 const Vector
<Def
*>& phisForBlock(BasicBlock
* block
)
226 return m_data
[block
].m_phis
;
229 // Ignores defs within the given block; it assumes that you've taken care of those
231 Def
* nonLocalReachingDef(BasicBlock
*, Variable
*);
232 Def
* reachingDefAtHead(BasicBlock
* block
, Variable
* variable
)
234 return nonLocalReachingDef(block
, variable
);
237 // Considers the def within the given block, but only works at the tail of the block.
238 Def
* reachingDefAtTail(BasicBlock
*, Variable
*);
240 void dump(PrintStream
&) const;
243 SegmentedVector
<Variable
> m_variables
;
249 HashMap
<Variable
*, Def
*> m_defs
;
253 BlockMap
<BlockData
> m_data
;
258 } } // namespace JSC::DFG
260 #endif // ENABLE(DFG_JIT)
262 #endif // DFGSSACalculator_h