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 "DFGStrengthReductionPhase.h"
31 #include "DFGAbstractHeap.h"
32 #include "DFGClobberize.h"
34 #include "DFGInsertionSet.h"
36 #include "DFGPredictionPropagationPhase.h"
37 #include "DFGVariableAccessDataDump.h"
38 #include "JSCInlines.h"
40 namespace JSC
{ namespace DFG
{
42 class StrengthReductionPhase
: public Phase
{
44 StrengthReductionPhase(Graph
& graph
)
45 : Phase(graph
, "strength reduction")
46 , m_insertionSet(graph
)
52 ASSERT(m_graph
.m_fixpointState
== FixpointNotConverged
);
56 for (BlockIndex blockIndex
= m_graph
.numBlocks(); blockIndex
--;) {
57 m_block
= m_graph
.block(blockIndex
);
60 for (m_nodeIndex
= 0; m_nodeIndex
< m_block
->size(); ++m_nodeIndex
) {
61 m_node
= m_block
->at(m_nodeIndex
);
64 m_insertionSet
.execute(m_block
);
73 switch (m_node
->op()) {
75 handleCommutativity();
77 if (m_node
->child2()->isInt32Constant() && !m_node
->child2()->asInt32()) {
78 convertToIdentityOverChild1();
85 handleCommutativity();
91 if (m_node
->child2()->isInt32Constant() && !(m_node
->child2()->asInt32() & 0x1f)) {
92 convertToIdentityOverChild1();
98 if (m_node
->child1()->op() == BitURShift
99 && m_node
->child1()->child2()->isInt32Constant()
100 && (m_node
->child1()->child2()->asInt32() & 0x1f)
101 && m_node
->arithMode() != Arith::DoOverflow
) {
102 m_node
->convertToIdentity();
109 handleCommutativity();
111 if (m_node
->child2()->isInt32Constant() && !m_node
->child2()->asInt32()) {
112 convertToIdentityOverChild1();
118 handleCommutativity();
122 if (m_node
->child2()->isInt32Constant()
123 && m_node
->isBinaryUseKind(Int32Use
)) {
124 int32_t value
= m_node
->child2()->asInt32();
125 if (-value
!= value
) {
126 m_node
->setOp(ArithAdd
);
127 m_node
->child2().setNode(
128 m_insertionSet
.insertConstant(
129 m_nodeIndex
, m_node
->origin
, jsNumber(-value
)));
137 if (m_node
->child2()->isNumberConstant()) {
138 double yOperandValue
= m_node
->child2()->asNumber();
139 if (yOperandValue
== 1) {
140 convertToIdentityOverChild1();
141 } else if (yOperandValue
== 0.5) {
142 m_insertionSet
.insertCheck(m_nodeIndex
, m_node
);
143 m_node
->convertToArithSqrt();
152 // This short-circuits circuitous conversions, like ValueRep(DoubleRep(value)) or
153 // even more complicated things. Like, it can handle a beast like
154 // ValueRep(DoubleRep(Int52Rep(value))).
156 // The only speculation that we would do beyond validating that we have a type that
157 // can be represented a certain way is an Int32 check that would appear on Int52Rep
158 // nodes. For now, if we see this and the final type we want is an Int52, we use it
159 // as an excuse not to fold. The only thing we would need is a Int52RepInt32Use kind.
160 bool hadInt32Check
= false;
161 if (m_node
->op() == Int52Rep
) {
162 if (m_node
->child1().useKind() != Int32Use
)
164 hadInt32Check
= true;
166 for (Node
* node
= m_node
->child1().node(); ; node
= node
->child1().node()) {
167 if (canonicalResultRepresentation(node
->result()) ==
168 canonicalResultRepresentation(m_node
->result())) {
169 m_insertionSet
.insertCheck(m_nodeIndex
, m_node
);
171 // FIXME: Consider adding Int52RepInt32Use or even DoubleRepInt32Use,
172 // which would be super weird. The latter would only arise in some
173 // seriously circuitous conversions.
174 if (canonicalResultRepresentation(node
->result()) != NodeResultJS
)
177 m_insertionSet
.insertCheck(
178 m_nodeIndex
, m_node
->origin
, Edge(node
, Int32Use
));
180 m_node
->child1() = node
->defaultEdge();
181 m_node
->convertToIdentity();
186 switch (node
->op()) {
188 if (node
->child1().useKind() != Int32Use
)
190 hadInt32Check
= true;
206 ASSERT(m_graph
.m_form
!= SSA
);
208 Node
* setLocal
= nullptr;
209 VirtualRegister local
= m_node
->local();
211 for (unsigned i
= m_nodeIndex
; i
--;) {
212 Node
* node
= m_block
->at(i
);
213 if (node
->op() == SetLocal
&& node
->local() == local
) {
217 if (accessesOverlap(m_graph
, node
, AbstractHeap(Stack
, local
)))
224 // The Flush should become a PhantomLocal at this point. This means that we want the
225 // local's value during OSR, but we don't care if the value is stored to the stack. CPS
226 // rethreading can canonicalize PhantomLocals for us.
227 m_node
->convertFlushToPhantomLocal();
238 void convertToIdentityOverChild(unsigned childIndex
)
240 m_insertionSet
.insertCheck(m_nodeIndex
, m_node
);
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 handleCommutativity()
258 // If the right side is a constant then there is nothing left to do.
259 if (m_node
->child2()->hasConstant())
262 // This case ensures that optimizations that look for x + const don't also have
263 // to look for const + x.
264 if (m_node
->child1()->hasConstant()) {
265 std::swap(m_node
->child1(), m_node
->child2());
270 // This case ensures that CSE is commutativity-aware.
271 if (m_node
->child1().node() > m_node
->child2().node()) {
272 std::swap(m_node
->child1(), m_node
->child2());
278 InsertionSet m_insertionSet
;
280 unsigned m_nodeIndex
;
285 bool performStrengthReduction(Graph
& graph
)
287 SamplingRegion
samplingRegion("DFG Strength Reduction Phase");
288 return runPhase
<StrengthReductionPhase
>(graph
);
291 } } // namespace JSC::DFG
293 #endif // ENABLE(DFG_JIT)