1 | /*
|
---|
2 | * Copyright (C) 2013-2015 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 "DFGDCEPhase.h"
|
---|
28 |
|
---|
29 | #if ENABLE(DFG_JIT)
|
---|
30 |
|
---|
31 | #include "DFGGraph.h"
|
---|
32 | #include "DFGInsertionSet.h"
|
---|
33 | #include "DFGPhase.h"
|
---|
34 | #include "JSCJSValueInlines.h"
|
---|
35 |
|
---|
36 | namespace JSC { namespace DFG {
|
---|
37 |
|
---|
38 | class DCEPhase : public Phase {
|
---|
39 | public:
|
---|
40 | DCEPhase(Graph& graph)
|
---|
41 | : Phase(graph, "dead code elimination")
|
---|
42 | , m_insertionSet(graph)
|
---|
43 | {
|
---|
44 | }
|
---|
45 |
|
---|
46 | bool run()
|
---|
47 | {
|
---|
48 | ASSERT(m_graph.m_form == ThreadedCPS || m_graph.m_form == SSA);
|
---|
49 |
|
---|
50 | m_graph.computeRefCounts();
|
---|
51 |
|
---|
52 | for (BasicBlock* block : m_graph.blocksInPreOrder())
|
---|
53 | fixupBlock(block);
|
---|
54 |
|
---|
55 | for (auto& argumentsVector : m_graph.m_rootToArguments.values())
|
---|
56 | cleanVariables(argumentsVector);
|
---|
57 |
|
---|
58 | // Just do a basic Phantom/Check clean-up.
|
---|
59 | for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
|
---|
60 | BasicBlock* block = m_graph.block(blockIndex);
|
---|
61 | if (!block)
|
---|
62 | continue;
|
---|
63 | unsigned sourceIndex = 0;
|
---|
64 | unsigned targetIndex = 0;
|
---|
65 | while (sourceIndex < block->size()) {
|
---|
66 | Node* node = block->at(sourceIndex++);
|
---|
67 | switch (node->op()) {
|
---|
68 | case Check:
|
---|
69 | case Phantom:
|
---|
70 | if (node->children.isEmpty())
|
---|
71 | continue;
|
---|
72 | break;
|
---|
73 | case CheckVarargs: {
|
---|
74 | bool isEmpty = true;
|
---|
75 | m_graph.doToChildren(node, [&] (Edge edge) {
|
---|
76 | isEmpty &= !edge;
|
---|
77 | });
|
---|
78 | if (isEmpty)
|
---|
79 | continue;
|
---|
80 | break;
|
---|
81 | }
|
---|
82 | default:
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | block->at(targetIndex++) = node;
|
---|
86 | }
|
---|
87 | block->resize(targetIndex);
|
---|
88 | }
|
---|
89 |
|
---|
90 | m_graph.m_refCountState = ExactRefCount;
|
---|
91 |
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 |
|
---|
95 | private:
|
---|
96 | void fixupBlock(BasicBlock* block)
|
---|
97 | {
|
---|
98 | if (!block)
|
---|
99 | return;
|
---|
100 |
|
---|
101 | if (m_graph.m_form == ThreadedCPS) {
|
---|
102 | for (unsigned phiIndex = 0; phiIndex < block->phis.size(); ++phiIndex) {
|
---|
103 | Node* phi = block->phis[phiIndex];
|
---|
104 | if (!phi->shouldGenerate()) {
|
---|
105 | m_graph.deleteNode(phi);
|
---|
106 | block->phis[phiIndex--] = block->phis.last();
|
---|
107 | block->phis.removeLast();
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | cleanVariables(block->variablesAtHead);
|
---|
112 | cleanVariables(block->variablesAtTail);
|
---|
113 | }
|
---|
114 |
|
---|
115 | // This has to be a forward loop because we are using the insertion set.
|
---|
116 | for (unsigned indexInBlock = 0; indexInBlock < block->size(); ++indexInBlock) {
|
---|
117 | Node* node = block->at(indexInBlock);
|
---|
118 | if (node->shouldGenerate())
|
---|
119 | continue;
|
---|
120 |
|
---|
121 | if (node->flags() & NodeHasVarArgs) {
|
---|
122 | for (unsigned childIdx = node->firstChild(); childIdx < node->firstChild() + node->numChildren(); childIdx++) {
|
---|
123 | Edge edge = m_graph.m_varArgChildren[childIdx];
|
---|
124 |
|
---|
125 | if (!edge || edge.willNotHaveCheck())
|
---|
126 | continue;
|
---|
127 |
|
---|
128 | m_insertionSet.insertNode(indexInBlock, SpecNone, Check, node->origin, edge);
|
---|
129 | }
|
---|
130 |
|
---|
131 | node->setOpAndDefaultFlags(Check);
|
---|
132 | node->children.reset();
|
---|
133 | node->setRefCount(1);
|
---|
134 | continue;
|
---|
135 | }
|
---|
136 |
|
---|
137 | node->remove(m_graph);
|
---|
138 | node->setRefCount(1);
|
---|
139 | }
|
---|
140 |
|
---|
141 | m_insertionSet.execute(block);
|
---|
142 | }
|
---|
143 |
|
---|
144 | template<typename VariablesVectorType>
|
---|
145 | void cleanVariables(VariablesVectorType& variables)
|
---|
146 | {
|
---|
147 | for (unsigned i = variables.size(); i--;) {
|
---|
148 | Node* node = variables[i];
|
---|
149 | if (!node)
|
---|
150 | continue;
|
---|
151 | if (node->op() != Check && node->shouldGenerate())
|
---|
152 | continue;
|
---|
153 | variables[i] = nullptr;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | InsertionSet m_insertionSet;
|
---|
158 | };
|
---|
159 |
|
---|
160 | bool performDCE(Graph& graph)
|
---|
161 | {
|
---|
162 | return runPhase<DCEPhase>(graph);
|
---|
163 | }
|
---|
164 |
|
---|
165 | } } // namespace JSC::DFG
|
---|
166 |
|
---|
167 | #endif // ENABLE(DFG_JIT)
|
---|
168 |
|
---|