]> git.saurik.com Git - apple/javascriptcore.git/blob - dfg/DFGCFAPhase.cpp
5f69de18609c3101f42f87f25db237b141dce6d7
[apple/javascriptcore.git] / dfg / DFGCFAPhase.cpp
1 /*
2 * Copyright (C) 2011, 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 #include "config.h"
27 #include "DFGCFAPhase.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "DFGAbstractInterpreterInlines.h"
32 #include "DFGGraph.h"
33 #include "DFGInPlaceAbstractState.h"
34 #include "DFGPhase.h"
35 #include "DFGSafeToExecute.h"
36 #include "OperandsInlines.h"
37 #include "JSCInlines.h"
38
39 namespace JSC { namespace DFG {
40
41 class CFAPhase : public Phase {
42 public:
43 CFAPhase(Graph& graph)
44 : Phase(graph, "control flow analysis")
45 , m_state(graph)
46 , m_interpreter(graph, m_state)
47 , m_verbose(Options::verboseCFA())
48 {
49 }
50
51 bool run()
52 {
53 ASSERT(m_graph.m_form == ThreadedCPS || m_graph.m_form == SSA);
54 ASSERT(m_graph.m_unificationState == GloballyUnified);
55 ASSERT(m_graph.m_refCountState == EverythingIsLive);
56
57 m_count = 0;
58
59 if (m_verbose && !shouldDumpGraphAtEachPhase()) {
60 dataLog("Graph before CFA:\n");
61 m_graph.dump();
62 }
63
64 // This implements a pseudo-worklist-based forward CFA, except that the visit order
65 // of blocks is the bytecode program order (which is nearly topological), and
66 // instead of a worklist we just walk all basic blocks checking if cfaShouldRevisit
67 // is set to true. This is likely to balance the efficiency properties of both
68 // worklist-based and forward fixpoint-based approaches. Like a worklist-based
69 // approach, it won't visit code if it's meaningless to do so (nothing changed at
70 // the head of the block or the predecessors have not been visited). Like a forward
71 // fixpoint-based approach, it has a high probability of only visiting a block
72 // after all predecessors have been visited. Only loops will cause this analysis to
73 // revisit blocks, and the amount of revisiting is proportional to loop depth.
74
75 m_state.initialize();
76
77 do {
78 m_changed = false;
79 performForwardCFA();
80 } while (m_changed);
81
82 return true;
83 }
84
85 private:
86 void performBlockCFA(BasicBlock* block)
87 {
88 if (!block)
89 return;
90 if (!block->cfaShouldRevisit)
91 return;
92 if (m_verbose)
93 dataLog(" Block ", *block, ":\n");
94 m_state.beginBasicBlock(block);
95 if (m_verbose)
96 dataLog(" head vars: ", block->valuesAtHead, "\n");
97 for (unsigned i = 0; i < block->size(); ++i) {
98 if (m_verbose) {
99 Node* node = block->at(i);
100 dataLogF(" %s @%u: ", Graph::opName(node->op()), node->index());
101
102 if (!safeToExecute(m_state, m_graph, node))
103 dataLog("(UNSAFE) ");
104
105 m_interpreter.dump(WTF::dataFile());
106
107 if (m_state.haveStructures())
108 dataLog(" (Have Structures)");
109 dataLogF("\n");
110 }
111 if (!m_interpreter.execute(i)) {
112 if (m_verbose)
113 dataLogF(" Expect OSR exit.\n");
114 break;
115 }
116 }
117 if (m_verbose) {
118 dataLogF(" tail regs: ");
119 m_interpreter.dump(WTF::dataFile());
120 dataLogF("\n");
121 }
122 m_changed |= m_state.endBasicBlock(MergeToSuccessors);
123
124 if (m_verbose)
125 dataLog(" tail vars: ", block->valuesAtTail, "\n");
126 }
127
128 void performForwardCFA()
129 {
130 ++m_count;
131 if (m_verbose)
132 dataLogF("CFA [%u]\n", ++m_count);
133
134 for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex)
135 performBlockCFA(m_graph.block(blockIndex));
136 }
137
138 private:
139 InPlaceAbstractState m_state;
140 AbstractInterpreter<InPlaceAbstractState> m_interpreter;
141
142 bool m_verbose;
143
144 bool m_changed;
145 unsigned m_count;
146 };
147
148 bool performCFA(Graph& graph)
149 {
150 SamplingRegion samplingRegion("DFG CFA Phase");
151 return runPhase<CFAPhase>(graph);
152 }
153
154 } } // namespace JSC::DFG
155
156 #endif // ENABLE(DFG_JIT)