2 * Copyright (C) 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 "DFGStrengthReductionPhase.h"
32 #include "DFGInsertionSet.h"
34 #include "DFGPredictionPropagationPhase.h"
35 #include "DFGVariableAccessDataDump.h"
36 #include "JSCInlines.h"
38 namespace JSC
{ namespace DFG
{
40 class StrengthReductionPhase
: public Phase
{
42 StrengthReductionPhase(Graph
& graph
)
43 : Phase(graph
, "strength reduction")
44 , m_insertionSet(graph
)
50 ASSERT(m_graph
.m_fixpointState
== FixpointNotConverged
);
54 for (BlockIndex blockIndex
= m_graph
.numBlocks(); blockIndex
--;) {
55 m_block
= m_graph
.block(blockIndex
);
58 for (m_nodeIndex
= 0; m_nodeIndex
< m_block
->size(); ++m_nodeIndex
) {
59 m_node
= m_block
->at(m_nodeIndex
);
62 m_insertionSet
.execute(m_block
);
71 switch (m_node
->op()) {
73 handleCommutativity();
75 if (m_node
->child2()->isConstant()) {
76 JSValue op2
= m_graph
.valueOfJSConstant(m_node
->child2().node());
77 if (op2
.isInt32() && !op2
.asInt32()) {
78 convertToIdentityOverChild1();
86 handleCommutativity();
92 if (m_node
->child2()->isConstant()) {
93 JSValue op2
= m_graph
.valueOfJSConstant(m_node
->child2().node());
94 if (op2
.isInt32() && !(op2
.asInt32() & 0x1f)) {
95 convertToIdentityOverChild1();
102 if (m_node
->child1()->op() == BitURShift
103 && m_node
->child1()->child2()->isConstant()) {
104 JSValue shiftAmount
= m_graph
.valueOfJSConstant(
105 m_node
->child1()->child2().node());
106 if (shiftAmount
.isInt32() && (shiftAmount
.asInt32() & 0x1f)
107 && m_node
->arithMode() != Arith::DoOverflow
) {
108 m_node
->convertToIdentity();
116 handleCommutativity();
118 if (m_graph
.isInt32Constant(m_node
->child2().node())) {
119 int32_t value
= m_graph
.valueOfInt32Constant(
120 m_node
->child2().node());
122 convertToIdentityOverChild1();
129 handleCommutativity();
133 if (m_graph
.isInt32Constant(m_node
->child2().node())
134 && m_node
->isBinaryUseKind(Int32Use
)) {
135 int32_t value
= m_graph
.valueOfInt32Constant(m_node
->child2().node());
136 if (-value
!= value
) {
137 m_node
->setOp(ArithAdd
);
138 m_node
->child2().setNode(
139 m_insertionSet
.insertConstant(
140 m_nodeIndex
, m_node
->origin
, jsNumber(-value
)));
148 if (JSArrayBufferView
* view
= m_graph
.tryGetFoldableViewForChild1(m_node
))
149 foldTypedArrayPropertyToConstant(view
, jsNumber(view
->length()));
152 case GetTypedArrayByteOffset
:
153 if (JSArrayBufferView
* view
= m_graph
.tryGetFoldableView(m_node
->child1().node()))
154 foldTypedArrayPropertyToConstant(view
, jsNumber(view
->byteOffset()));
157 case GetIndexedPropertyStorage
:
158 if (JSArrayBufferView
* view
= m_graph
.tryGetFoldableViewForChild1(m_node
)) {
159 if (view
->mode() != FastTypedArray
) {
160 prepareToFoldTypedArray(view
);
161 m_node
->convertToConstantStoragePointer(view
->vector());
165 // FIXME: It would be awesome to be able to fold the property storage for
166 // these GC-allocated typed arrays. For now it doesn't matter because the
167 // most common use-cases for constant typed arrays involve large arrays with
168 // aliased buffer views.
169 // https://bugs.webkit.org/show_bug.cgi?id=125425
177 // This short-circuits circuitous conversions, like ValueRep(DoubleRep(value)) or
178 // even more complicated things. Like, it can handle a beast like
179 // ValueRep(DoubleRep(Int52Rep(value))).
181 // The only speculation that we would do beyond validating that we have a type that
182 // can be represented a certain way is an Int32 check that would appear on Int52Rep
183 // nodes. For now, if we see this and the final type we want is an Int52, we use it
184 // as an excuse not to fold. The only thing we would need is a Int52RepInt32Use kind.
185 bool hadInt32Check
= false;
186 if (m_node
->op() == Int52Rep
) {
187 if (m_node
->child1().useKind() != Int32Use
)
189 hadInt32Check
= true;
191 for (Node
* node
= m_node
->child1().node(); ; node
= node
->child1().node()) {
192 if (canonicalResultRepresentation(node
->result()) ==
193 canonicalResultRepresentation(m_node
->result())) {
194 m_insertionSet
.insertNode(
195 m_nodeIndex
, SpecNone
, Phantom
, m_node
->origin
, m_node
->child1());
197 // FIXME: Consider adding Int52RepInt32Use or even DoubleRepInt32Use,
198 // which would be super weird. The latter would only arise in some
199 // seriously circuitous conversions.
200 if (canonicalResultRepresentation(node
->result()) != NodeResultJS
)
203 m_insertionSet
.insertNode(
204 m_nodeIndex
, SpecNone
, Phantom
, m_node
->origin
,
205 Edge(node
, Int32Use
));
207 m_node
->child1() = node
->defaultEdge();
208 m_node
->convertToIdentity();
213 switch (node
->op()) {
215 if (node
->child1().useKind() != Int32Use
)
217 hadInt32Check
= true;
237 void convertToIdentityOverChild(unsigned childIndex
)
239 m_insertionSet
.insertNode(
240 m_nodeIndex
, SpecNone
, Phantom
, m_node
->origin
, m_node
->children
);
241 m_node
->children
.removeEdge(childIndex
^ 1);
242 m_node
->convertToIdentity();
246 void convertToIdentityOverChild1()
248 convertToIdentityOverChild(0);
251 void convertToIdentityOverChild2()
253 convertToIdentityOverChild(1);
256 void foldTypedArrayPropertyToConstant(JSArrayBufferView
* view
, JSValue constant
)
258 prepareToFoldTypedArray(view
);
259 m_graph
.convertToConstant(m_node
, constant
);
263 void prepareToFoldTypedArray(JSArrayBufferView
* view
)
265 m_insertionSet
.insertNode(
266 m_nodeIndex
, SpecNone
, TypedArrayWatchpoint
, m_node
->origin
,
268 m_insertionSet
.insertNode(
269 m_nodeIndex
, SpecNone
, Phantom
, m_node
->origin
, m_node
->children
);
272 void handleCommutativity()
274 // If the right side is a constant then there is nothing left to do.
275 if (m_node
->child2()->hasConstant())
278 // This case ensures that optimizations that look for x + const don't also have
279 // to look for const + x.
280 if (m_node
->child1()->hasConstant()) {
281 std::swap(m_node
->child1(), m_node
->child2());
286 // This case ensures that CSE is commutativity-aware.
287 if (m_node
->child1().node() > m_node
->child2().node()) {
288 std::swap(m_node
->child1(), m_node
->child2());
294 InsertionSet m_insertionSet
;
296 unsigned m_nodeIndex
;
301 bool performStrengthReduction(Graph
& graph
)
303 SamplingRegion
samplingRegion("DFG Strength Reduction Phase");
304 return runPhase
<StrengthReductionPhase
>(graph
);
307 } } // namespace JSC::DFG
309 #endif // ENABLE(DFG_JIT)