]> git.saurik.com Git - apple/javascriptcore.git/blame - dfg/DFGDCEPhase.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / dfg / DFGDCEPhase.cpp
CommitLineData
93a37866 1/*
ed1e77d3 2 * Copyright (C) 2013-2015 Apple Inc. All rights reserved.
93a37866
A
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"
81345200 35#include "JSCInlines.h"
93a37866
A
36
37namespace JSC { namespace DFG {
38
39class DCEPhase : public Phase {
40public:
41 DCEPhase(Graph& graph)
42 : Phase(graph, "dead code elimination")
81345200 43 , m_insertionSet(graph)
93a37866
A
44 {
45 }
46
47 bool run()
48 {
81345200
A
49 ASSERT(m_graph.m_form == ThreadedCPS || m_graph.m_form == SSA);
50
ed1e77d3
A
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--;) {
81345200 60 BasicBlock* block = m_graph.block(blockIndex);
93a37866
A
61 if (!block)
62 continue;
ed1e77d3
A
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())
81345200 71 continue;
ed1e77d3
A
72 break;
73 default:
74 break;
93a37866 75 }
ed1e77d3 76 block->at(targetIndex++) = node;
93a37866 77 }
ed1e77d3 78 block->resize(targetIndex);
93a37866
A
79 }
80
81 m_graph.m_refCountState = ExactRefCount;
82
83 return true;
84 }
85
86private:
81345200
A
87 void fixupBlock(BasicBlock* block)
88 {
89 if (!block)
93a37866 90 return;
ed1e77d3
A
91
92 if (m_graph.m_form == ThreadedCPS) {
81345200 93 for (unsigned phiIndex = 0; phiIndex < block->phis.size(); ++phiIndex) {
ed1e77d3
A
94 Node* phi = block->phis[phiIndex];
95 if (!phi->shouldGenerate()) {
96 m_graph.m_allocator.free(phi);
81345200
A
97 block->phis[phiIndex--] = block->phis.last();
98 block->phis.removeLast();
99 }
100 }
101
102 cleanVariables(block->variablesAtHead);
103 cleanVariables(block->variablesAtTail);
81345200
A
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
ed1e77d3
A
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);
81345200 120 }
81345200 121
ed1e77d3
A
122 node->setOpAndDefaultFlags(Check);
123 node->children.reset();
124 node->setRefCount(1);
125 continue;
81345200
A
126 }
127
ed1e77d3
A
128 node->remove();
129 node->setRefCount(1);
81345200
A
130 }
131
132 m_insertionSet.execute(block);
93a37866
A
133 }
134
81345200
A
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;
ed1e77d3 142 if (node->op() != Check && node->shouldGenerate())
81345200 143 continue;
ed1e77d3 144 variables[i] = nullptr;
81345200
A
145 }
146 }
147
81345200 148 InsertionSet m_insertionSet;
93a37866
A
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