2 * Copyright (C) 2011 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 DFGAbstractState_h
27 #define DFGAbstractState_h
29 #include <wtf/Platform.h>
33 #include "DFGAbstractValue.h"
36 #include <wtf/Vector.h>
46 // This implements the notion of an abstract state for flow-sensitive intraprocedural
47 // control flow analysis (CFA), with a focus on the elimination of redundant type checks.
48 // It also implements most of the mechanisms of abstract interpretation that such an
49 // analysis would use. This class should be used in two idioms:
51 // 1) Performing the CFA. In this case, AbstractState should be run over all basic
52 // blocks repeatedly until convergence is reached. Convergence is defined by
53 // endBasicBlock(AbstractState::MergeToSuccessors) returning false for all blocks.
55 // 2) Rematerializing the results of a previously executed CFA. In this case,
56 // AbstractState should be run over whatever basic block you're interested in up
57 // to the point of the node at which you'd like to interrogate the known type
58 // of all other nodes. At this point it's safe to discard the AbstractState entirely,
59 // call reset(), or to run it to the end of the basic block and call
60 // endBasicBlock(AbstractState::DontMerge). The latter option is safest because
61 // it performs some useful integrity checks.
63 // After the CFA is run, the inter-block state is saved at the heads and tails of all
64 // basic blocks. This allows the intra-block state to be rematerialized by just
65 // executing the CFA for that block. If you need to know inter-block state only, then
66 // you only need to examine the BasicBlock::m_valuesAtHead or m_valuesAtTail fields.
68 // Running this analysis involves the following, modulo the inter-block state
69 // merging and convergence fixpoint:
71 // AbstractState state(codeBlock, graph);
72 // state.beginBasicBlock(basicBlock);
73 // bool endReached = true;
74 // for (NodeIndex idx = basicBlock.begin; idx < basicBlock.end; ++idx) {
75 // if (!state.execute(idx))
78 // bool result = state.endBasicBlock(<either Merge or DontMerge>);
83 // Don't merge the state in AbstractState with basic blocks.
86 // Merge the state in AbstractState with the tail of the basic
87 // block being analyzed.
90 // Merge the state in AbstractState with the tail of the basic
91 // block, and with the heads of successor blocks.
95 AbstractState(Graph
&);
99 AbstractValue
& forNode(NodeIndex nodeIndex
)
101 return m_nodes
[nodeIndex
];
104 AbstractValue
& forNode(Edge nodeUse
)
106 return forNode(nodeUse
.index());
109 // Call this before beginning CFA to initialize the abstract values of
110 // arguments, and to indicate which blocks should be listed for CFA
112 static void initialize(Graph
&);
114 // Start abstractly executing the given basic block. Initializes the
115 // notion of abstract state to what we believe it to be at the head
116 // of the basic block, according to the basic block's data structures.
117 // This method also sets cfaShouldRevisit to false.
118 void beginBasicBlock(BasicBlock
*);
120 // Finish abstractly executing a basic block. If MergeToTail or
121 // MergeToSuccessors is passed, then this merges everything we have
122 // learned about how the state changes during this block's execution into
123 // the block's data structures. There are three return modes, depending
124 // on the value of mergeMode:
127 // Always returns false.
130 // Returns true if the state of the block at the tail was changed.
131 // This means that you must call mergeToSuccessors(), and if that
132 // returns true, then you must revisit (at least) the successor
133 // blocks. False will always be returned if the block is terminal
134 // (i.e. ends in Throw or Return, or has a ForceOSRExit inside it).
136 // MergeToSuccessors:
137 // Returns true if the state of the block at the tail was changed,
138 // and, if the state at the heads of successors was changed.
139 // A true return means that you must revisit (at least) the successor
140 // blocks. This also sets cfaShouldRevisit to true for basic blocks
141 // that must be visited next.
142 bool endBasicBlock(MergeMode
);
144 // Reset the AbstractState. This throws away any results, and at this point
145 // you can safely call beginBasicBlock() on any basic block.
148 // Abstractly executes the given node. The new abstract state is stored into an
149 // abstract register file stored in *this. Loads of local variables (that span
150 // basic blocks) interrogate the basic block's notion of the state at the head.
151 // Stores to local variables are handled in endBasicBlock(). This returns true
152 // if execution should continue past this node. Notably, it will return true
153 // for block terminals, so long as those terminals are not Return or variants
155 bool execute(unsigned);
157 // Is the execution state still valid? This will be false if execute() has
158 // returned false previously.
159 bool isValid() const { return m_isValid
; }
161 // Merge the abstract state stored at the first block's tail into the second
162 // block's head. Returns true if the second block's state changed. If so,
163 // that block must be abstractly interpreted again. This also sets
164 // to->cfaShouldRevisit to true, if it returns true, or if to has not been
166 bool merge(BasicBlock
* from
, BasicBlock
* to
);
168 // Merge the abstract state stored at the block's tail into all of its
169 // successors. Returns true if any of the successors' states changed. Note
170 // that this is automatically called in endBasicBlock() if MergeMode is
171 // MergeToSuccessors.
172 bool mergeToSuccessors(Graph
&, BasicBlock
*);
175 void dump(FILE* out
);
179 void clobberStructures(unsigned);
181 bool mergeStateAtTail(AbstractValue
& destination
, AbstractValue
& inVariable
, NodeIndex
);
183 static bool mergeVariableBetweenBlocks(AbstractValue
& destination
, AbstractValue
& source
, NodeIndex destinationNodeIndex
, NodeIndex sourceNodeIndex
);
185 CodeBlock
* m_codeBlock
;
188 Vector
<AbstractValue
, 64> m_nodes
;
189 Operands
<AbstractValue
> m_variables
;
191 bool m_haveStructures
;
196 } } // namespace JSC::DFG
198 #endif // ENABLE(DFG_JIT)
200 #endif // DFGAbstractState_h