2 * Copyright (C) 2013-2015 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 "DFGDCEPhase.h"
31 #include "DFGBasicBlockInlines.h"
33 #include "DFGInsertionSet.h"
35 #include "JSCInlines.h"
37 namespace JSC
{ namespace DFG
{
39 class DCEPhase
: public Phase
{
41 DCEPhase(Graph
& graph
)
42 : Phase(graph
, "dead code elimination")
43 , m_insertionSet(graph
)
49 ASSERT(m_graph
.m_form
== ThreadedCPS
|| m_graph
.m_form
== SSA
);
51 m_graph
.computeRefCounts();
53 for (BasicBlock
* block
: m_graph
.blocksInPreOrder())
56 cleanVariables(m_graph
.m_arguments
);
58 // Just do a basic Phantom/Check clean-up.
59 for (BlockIndex blockIndex
= m_graph
.numBlocks(); blockIndex
--;) {
60 BasicBlock
* block
= m_graph
.block(blockIndex
);
63 unsigned sourceIndex
= 0;
64 unsigned targetIndex
= 0;
65 while (sourceIndex
< block
->size()) {
66 Node
* node
= block
->at(sourceIndex
++);
70 if (node
->children
.isEmpty())
76 block
->at(targetIndex
++) = node
;
78 block
->resize(targetIndex
);
81 m_graph
.m_refCountState
= ExactRefCount
;
87 void fixupBlock(BasicBlock
* block
)
92 if (m_graph
.m_form
== ThreadedCPS
) {
93 for (unsigned phiIndex
= 0; phiIndex
< block
->phis
.size(); ++phiIndex
) {
94 Node
* phi
= block
->phis
[phiIndex
];
95 if (!phi
->shouldGenerate()) {
96 m_graph
.m_allocator
.free(phi
);
97 block
->phis
[phiIndex
--] = block
->phis
.last();
98 block
->phis
.removeLast();
102 cleanVariables(block
->variablesAtHead
);
103 cleanVariables(block
->variablesAtTail
);
106 // This has to be a forward loop because we are using the insertion set.
107 for (unsigned indexInBlock
= 0; indexInBlock
< block
->size(); ++indexInBlock
) {
108 Node
* node
= block
->at(indexInBlock
);
109 if (node
->shouldGenerate())
112 if (node
->flags() & NodeHasVarArgs
) {
113 for (unsigned childIdx
= node
->firstChild(); childIdx
< node
->firstChild() + node
->numChildren(); childIdx
++) {
114 Edge edge
= m_graph
.m_varArgChildren
[childIdx
];
116 if (!edge
|| edge
.willNotHaveCheck())
119 m_insertionSet
.insertNode(indexInBlock
, SpecNone
, Check
, node
->origin
, edge
);
122 node
->setOpAndDefaultFlags(Check
);
123 node
->children
.reset();
124 node
->setRefCount(1);
129 node
->setRefCount(1);
132 m_insertionSet
.execute(block
);
135 template<typename VariablesVectorType
>
136 void cleanVariables(VariablesVectorType
& variables
)
138 for (unsigned i
= variables
.size(); i
--;) {
139 Node
* node
= variables
[i
];
142 if (node
->op() != Check
&& node
->shouldGenerate())
144 variables
[i
] = nullptr;
148 InsertionSet m_insertionSet
;
151 bool performDCE(Graph
& graph
)
153 SamplingRegion
samplingRegion("DFG DCE Phase");
154 return runPhase
<DCEPhase
>(graph
);
157 } } // namespace JSC::DFG
159 #endif // ENABLE(DFG_JIT)