2 * Copyright (C) 2011, 2013 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.
27 #include "DFGCFAPhase.h"
31 #include "DFGAbstractInterpreterInlines.h"
33 #include "DFGInPlaceAbstractState.h"
35 #include "DFGSafeToExecute.h"
36 #include "OperandsInlines.h"
37 #include "JSCInlines.h"
39 namespace JSC
{ namespace DFG
{
41 class CFAPhase
: public Phase
{
43 CFAPhase(Graph
& graph
)
44 : Phase(graph
, "control flow analysis")
46 , m_interpreter(graph
, m_state
)
47 , m_verbose(Options::verboseCFA())
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
);
59 if (m_verbose
&& !shouldDumpGraphAtEachPhase()) {
60 dataLog("Graph before CFA:\n");
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.
86 void performBlockCFA(BasicBlock
* block
)
90 if (!block
->cfaShouldRevisit
)
93 dataLog(" Block ", *block
, ":\n");
94 m_state
.beginBasicBlock(block
);
96 dataLog(" head vars: ", block
->valuesAtHead
, "\n");
97 for (unsigned i
= 0; i
< block
->size(); ++i
) {
99 Node
* node
= block
->at(i
);
100 dataLogF(" %s @%u: ", Graph::opName(node
->op()), node
->index());
102 if (!safeToExecute(m_state
, m_graph
, node
))
103 dataLog("(UNSAFE) ");
105 m_interpreter
.dump(WTF::dataFile());
107 if (m_state
.haveStructures())
108 dataLog(" (Have Structures)");
111 if (!m_interpreter
.execute(i
)) {
113 dataLogF(" Expect OSR exit.\n");
118 dataLogF(" tail regs: ");
119 m_interpreter
.dump(WTF::dataFile());
122 m_changed
|= m_state
.endBasicBlock(MergeToSuccessors
);
125 dataLog(" tail vars: ", block
->valuesAtTail
, "\n");
128 void performForwardCFA()
132 dataLogF("CFA [%u]\n", ++m_count
);
134 for (BlockIndex blockIndex
= 0; blockIndex
< m_graph
.numBlocks(); ++blockIndex
)
135 performBlockCFA(m_graph
.block(blockIndex
));
139 InPlaceAbstractState m_state
;
140 AbstractInterpreter
<InPlaceAbstractState
> m_interpreter
;
148 bool performCFA(Graph
& graph
)
150 SamplingRegion
samplingRegion("DFG CFA Phase");
151 return runPhase
<CFAPhase
>(graph
);
154 } } // namespace JSC::DFG
156 #endif // ENABLE(DFG_JIT)