]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - dfg/DFGDCEPhase.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / dfg / DFGDCEPhase.cpp
... / ...
CommitLineData
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 "DFGBasicBlockInlines.h"
32#include "DFGGraph.h"
33#include "DFGInsertionSet.h"
34#include "DFGPhase.h"
35#include "JSCInlines.h"
36
37namespace JSC { namespace DFG {
38
39class DCEPhase : public Phase {
40public:
41 DCEPhase(Graph& graph)
42 : Phase(graph, "dead code elimination")
43 , m_insertionSet(graph)
44 {
45 }
46
47 bool run()
48 {
49 ASSERT(m_graph.m_form == ThreadedCPS || m_graph.m_form == SSA);
50
51 m_graph.computeRefCounts();
52
53 for (BasicBlock* block : m_graph.blocksInPreOrder())
54 fixupBlock(block);
55
56 cleanVariables(m_graph.m_arguments);
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 default:
74 break;
75 }
76 block->at(targetIndex++) = node;
77 }
78 block->resize(targetIndex);
79 }
80
81 m_graph.m_refCountState = ExactRefCount;
82
83 return true;
84 }
85
86private:
87 void fixupBlock(BasicBlock* block)
88 {
89 if (!block)
90 return;
91
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();
99 }
100 }
101
102 cleanVariables(block->variablesAtHead);
103 cleanVariables(block->variablesAtTail);
104 }
105
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())
110 continue;
111
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];
115
116 if (!edge || edge.willNotHaveCheck())
117 continue;
118
119 m_insertionSet.insertNode(indexInBlock, SpecNone, Check, node->origin, edge);
120 }
121
122 node->setOpAndDefaultFlags(Check);
123 node->children.reset();
124 node->setRefCount(1);
125 continue;
126 }
127
128 node->remove();
129 node->setRefCount(1);
130 }
131
132 m_insertionSet.execute(block);
133 }
134
135 template<typename VariablesVectorType>
136 void cleanVariables(VariablesVectorType& variables)
137 {
138 for (unsigned i = variables.size(); i--;) {
139 Node* node = variables[i];
140 if (!node)
141 continue;
142 if (node->op() != Check && node->shouldGenerate())
143 continue;
144 variables[i] = nullptr;
145 }
146 }
147
148 InsertionSet m_insertionSet;
149};
150
151bool performDCE(Graph& graph)
152{
153 SamplingRegion samplingRegion("DFG DCE Phase");
154 return runPhase<DCEPhase>(graph);
155}
156
157} } // namespace JSC::DFG
158
159#endif // ENABLE(DFG_JIT)
160