1/*
2 * Copyright (C) 2013 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 DFGInPlaceAbstractState_h
27#define DFGInPlaceAbstractState_h
28
29#if ENABLE(DFG_JIT)
30
31#include "DFGAbstractValue.h"
32#include "DFGBranchDirection.h"
33#include "DFGGraph.h"
34#include "DFGMergeMode.h"
35#include "DFGNode.h"
36
37namespace JSC { namespace DFG {
38
39class InPlaceAbstractState {
40public:
41    InPlaceAbstractState(Graph&);
42
43    ~InPlaceAbstractState();
44
45    void createValueForNode(Node*) { }
46
47    AbstractValue& forNode(Node* node)
48    {
49        return node->value;
50    }
51
52    AbstractValue& forNode(Edge edge)
53    {
54        return forNode(edge.node());
55    }
56
57    Operands<AbstractValue>& variables()
58    {
59        return m_variables;
60    }
61
62    // Call this before beginning CFA to initialize the abstract values of
63    // arguments, and to indicate which blocks should be listed for CFA
64    // execution.
65    void initialize();
66
67    // Start abstractly executing the given basic block. Initializes the
68    // notion of abstract state to what we believe it to be at the head
69    // of the basic block, according to the basic block's data structures.
70    // This method also sets cfaShouldRevisit to false.
71    void beginBasicBlock(BasicBlock*);
72
73    BasicBlock* block() const { return m_block; }
74
75    // Finish abstractly executing a basic block. If MergeToTail or
76    // MergeToSuccessors is passed, then this merges everything we have
77    // learned about how the state changes during this block's execution into
78    // the block's data structures. There are three return modes, depending
79    // on the value of mergeMode:
80    //
81    // DontMerge:
82    //    Always returns false.
83    //
84    // MergeToTail:
85    //    Returns true if the state of the block at the tail was changed.
86    //    This means that you must call mergeToSuccessors(), and if that
87    //    returns true, then you must revisit (at least) the successor
88    //    blocks. False will always be returned if the block is terminal
89    //    (i.e. ends in Throw or Return, or has a ForceOSRExit inside it).
90    //
91    // MergeToSuccessors:
92    //    Returns true if the state of the block at the tail was changed,
93    //    and, if the state at the heads of successors was changed.
94    //    A true return means that you must revisit (at least) the successor
95    //    blocks. This also sets cfaShouldRevisit to true for basic blocks
96    //    that must be visited next.
97    bool endBasicBlock(MergeMode);
98
99    // Reset the AbstractState. This throws away any results, and at this point
100    // you can safely call beginBasicBlock() on any basic block.
101    void reset();
102
103    // Did the last executed node clobber the world?
104    bool didClobber() const { return m_didClobber; }
105
106    // Is the execution state still valid? This will be false if execute() has
107    // returned false previously.
108    bool isValid() const { return m_isValid; }
109
110    // Merge the abstract state stored at the first block's tail into the second
111    // block's head. Returns true if the second block's state changed. If so,
112    // that block must be abstractly interpreted again. This also sets
113    // to->cfaShouldRevisit to true, if it returns true, or if to has not been
114    // visited yet.
115    bool merge(BasicBlock* from, BasicBlock* to);
116
117    // Merge the abstract state stored at the block's tail into all of its
118    // successors. Returns true if any of the successors' states changed. Note
119    // that this is automatically called in endBasicBlock() if MergeMode is
120    // MergeToSuccessors.
121    bool mergeToSuccessors(BasicBlock*);
122
123    // Methods intended to be called from AbstractInterpreter.
124    void setDidClobber(bool didClobber) { m_didClobber = didClobber; }
125    void setIsValid(bool isValid) { m_isValid = isValid; }
126    void setBranchDirection(BranchDirection branchDirection) { m_branchDirection = branchDirection; }
127    void setFoundConstants(bool foundConstants) { m_foundConstants = foundConstants; }
128    bool haveStructures() const { return m_haveStructures; } // It's always safe to return true.
129    void setHaveStructures(bool haveStructures) { m_haveStructures = haveStructures; }
130
131private:
132    bool mergeStateAtTail(AbstractValue& destination, AbstractValue& inVariable, Node*);
133
134    static bool mergeVariableBetweenBlocks(AbstractValue& destination, AbstractValue& source, Node* destinationNode, Node* sourceNode);
135
136    Graph& m_graph;
137
138    Operands<AbstractValue> m_variables;
139    BasicBlock* m_block;
140
141    bool m_haveStructures;
142    bool m_foundConstants;
143
144    bool m_isValid;
145    bool m_didClobber;
146
147    BranchDirection m_branchDirection; // This is only set for blocks that end in Branch and that execute to completion (i.e. m_isValid == true).
148};
149
150} } // namespace JSC::DFG
151
152#endif // ENABLE(DFG_JIT)
153
154#endif // DFGInPlaceAbstractState_h
155
156