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 "DFGBackwardsPropagationPhase.h"
31 #include "DFGBasicBlockInlines.h"
34 #include "JSCInlines.h"
36 namespace JSC
{ namespace DFG
{
38 class BackwardsPropagationPhase
: public Phase
{
40 BackwardsPropagationPhase(Graph
& graph
)
41 : Phase(graph
, "backwards propagation")
50 for (BlockIndex blockIndex
= m_graph
.numBlocks(); blockIndex
--;) {
51 BasicBlock
* block
= m_graph
.block(blockIndex
);
55 // Prevent a tower of overflowing additions from creating a value that is out of the
57 m_allowNestedOverflowingAdditions
= block
->size() < (1 << 16);
59 for (unsigned indexInBlock
= block
->size(); indexInBlock
--;)
60 propagate(block
->at(indexInBlock
));
68 bool isNotNegZero(Node
* node
)
70 if (!node
->isNumberConstant())
72 double value
= node
->asNumber();
73 return (value
|| 1.0 / value
> 0.0);
76 bool isNotPosZero(Node
* node
)
78 if (!node
->isNumberConstant())
80 double value
= node
->asNumber();
81 return (value
|| 1.0 / value
< 0.0);
84 // Tests if the absolute value is strictly less than the power of two.
86 bool isWithinPowerOfTwoForConstant(Node
* node
)
88 JSValue immediateValue
= node
->asJSValue();
89 if (!immediateValue
.isNumber())
91 double immediate
= immediateValue
.asNumber();
92 return immediate
> -(static_cast<int64_t>(1) << power
) && immediate
< (static_cast<int64_t>(1) << power
);
96 bool isWithinPowerOfTwoNonRecursive(Node
* node
)
98 if (!node
->isNumberConstant())
100 return isWithinPowerOfTwoForConstant
<power
>(node
);
104 bool isWithinPowerOfTwo(Node
* node
)
106 switch (node
->op()) {
109 case Int52Constant
: {
110 return isWithinPowerOfTwoForConstant
<power
>(node
);
117 return isWithinPowerOfTwoNonRecursive
<power
>(node
->child1().node())
118 || isWithinPowerOfTwoNonRecursive
<power
>(node
->child2().node());
132 Node
* shiftAmount
= node
->child2().node();
133 if (!node
->isNumberConstant())
135 JSValue immediateValue
= shiftAmount
->asJSValue();
136 if (!immediateValue
.isInt32())
138 return immediateValue
.asInt32() > 32 - power
;
147 bool isWithinPowerOfTwo(Edge edge
)
149 return isWithinPowerOfTwo
<power
>(edge
.node());
152 bool mergeDefaultFlags(Node
* node
)
154 bool changed
= false;
155 if (node
->flags() & NodeHasVarArgs
) {
156 for (unsigned childIdx
= node
->firstChild();
157 childIdx
< node
->firstChild() + node
->numChildren();
159 if (!!m_graph
.m_varArgChildren
[childIdx
])
160 changed
|= m_graph
.m_varArgChildren
[childIdx
]->mergeFlags(NodeBytecodeUsesAsValue
);
165 changed
|= node
->child1()->mergeFlags(NodeBytecodeUsesAsValue
);
168 changed
|= node
->child2()->mergeFlags(NodeBytecodeUsesAsValue
);
171 changed
|= node
->child3()->mergeFlags(NodeBytecodeUsesAsValue
);
176 void propagate(Node
* node
)
178 NodeFlags flags
= node
->flags() & NodeBytecodeBackPropMask
;
180 switch (node
->op()) {
182 VariableAccessData
* variableAccessData
= node
->variableAccessData();
183 flags
&= ~NodeBytecodeUsesAsInt
; // We don't care about cross-block uses-as-int.
184 m_changed
|= variableAccessData
->mergeFlags(flags
);
189 VariableAccessData
* variableAccessData
= node
->variableAccessData();
190 if (!variableAccessData
->isLoadedFrom())
192 flags
= variableAccessData
->flags();
193 RELEASE_ASSERT(!(flags
& ~NodeBytecodeBackPropMask
));
194 flags
|= NodeBytecodeUsesAsNumber
; // Account for the fact that control flow may cause overflows that our modeling can't handle.
195 node
->child1()->mergeFlags(flags
);
200 VariableAccessData
* variableAccessData
= node
->variableAccessData();
201 m_changed
|= variableAccessData
->mergeFlags(NodeBytecodeUsesAsValue
);
216 flags
|= NodeBytecodeUsesAsInt
;
217 flags
&= ~(NodeBytecodeUsesAsNumber
| NodeBytecodeNeedsNegZero
| NodeBytecodeUsesAsOther
);
218 flags
&= ~NodeBytecodeUsesAsArrayIndex
;
219 node
->child1()->mergeFlags(flags
);
220 node
->child2()->mergeFlags(flags
);
224 case StringCharCodeAt
: {
225 node
->child1()->mergeFlags(NodeBytecodeUsesAsValue
);
226 node
->child2()->mergeFlags(NodeBytecodeUsesAsValue
| NodeBytecodeUsesAsInt
| NodeBytecodeUsesAsArrayIndex
);
230 case UInt32ToNumber
: {
231 node
->child1()->mergeFlags(flags
);
236 if (isNotNegZero(node
->child1().node()) || isNotNegZero(node
->child2().node()))
237 flags
&= ~NodeBytecodeNeedsNegZero
;
238 if (node
->child1()->hasNumberResult() || node
->child2()->hasNumberResult())
239 flags
&= ~NodeBytecodeUsesAsOther
;
240 if (!isWithinPowerOfTwo
<32>(node
->child1()) && !isWithinPowerOfTwo
<32>(node
->child2()))
241 flags
|= NodeBytecodeUsesAsNumber
;
242 if (!m_allowNestedOverflowingAdditions
)
243 flags
|= NodeBytecodeUsesAsNumber
;
245 node
->child1()->mergeFlags(flags
);
246 node
->child2()->mergeFlags(flags
);
251 if (isNotNegZero(node
->child1().node()) || isNotNegZero(node
->child2().node()))
252 flags
&= ~NodeBytecodeNeedsNegZero
;
253 if (!isWithinPowerOfTwo
<32>(node
->child1()) && !isWithinPowerOfTwo
<32>(node
->child2()))
254 flags
|= NodeBytecodeUsesAsNumber
;
255 if (!m_allowNestedOverflowingAdditions
)
256 flags
|= NodeBytecodeUsesAsNumber
;
258 node
->child1()->mergeFlags(flags
);
259 node
->child2()->mergeFlags(flags
);
264 flags
&= ~(NodeBytecodeUsesAsNumber
| NodeBytecodeNeedsNegZero
| NodeBytecodeUsesAsOther
| ~NodeBytecodeUsesAsArrayIndex
);
265 flags
|= NodeBytecodeUsesAsInt
;
266 node
->child1()->mergeFlags(flags
);
271 if (isNotNegZero(node
->child1().node()) || isNotPosZero(node
->child2().node()))
272 flags
&= ~NodeBytecodeNeedsNegZero
;
273 if (!isWithinPowerOfTwo
<32>(node
->child1()) && !isWithinPowerOfTwo
<32>(node
->child2()))
274 flags
|= NodeBytecodeUsesAsNumber
;
275 if (!m_allowNestedOverflowingAdditions
)
276 flags
|= NodeBytecodeUsesAsNumber
;
278 node
->child1()->mergeFlags(flags
);
279 node
->child2()->mergeFlags(flags
);
284 flags
&= ~NodeBytecodeUsesAsOther
;
286 node
->child1()->mergeFlags(flags
);
291 // As soon as a multiply happens, we can easily end up in the part
292 // of the double domain where the point at which you do truncation
293 // can change the outcome. So, ArithMul always forces its inputs to
294 // check for overflow. Additionally, it will have to check for overflow
295 // itself unless we can prove that there is no way for the values
296 // produced to cause double rounding.
298 if (!isWithinPowerOfTwo
<22>(node
->child1().node())
299 && !isWithinPowerOfTwo
<22>(node
->child2().node()))
300 flags
|= NodeBytecodeUsesAsNumber
;
302 node
->mergeFlags(flags
);
304 flags
|= NodeBytecodeUsesAsNumber
| NodeBytecodeNeedsNegZero
;
305 flags
&= ~NodeBytecodeUsesAsOther
;
307 node
->child1()->mergeFlags(flags
);
308 node
->child2()->mergeFlags(flags
);
313 flags
|= NodeBytecodeUsesAsNumber
| NodeBytecodeNeedsNegZero
;
314 flags
&= ~NodeBytecodeUsesAsOther
;
316 node
->child1()->mergeFlags(flags
);
317 node
->child2()->mergeFlags(flags
);
322 flags
|= NodeBytecodeUsesAsNumber
;
323 flags
&= ~NodeBytecodeUsesAsOther
;
325 node
->child1()->mergeFlags(flags
);
326 node
->child2()->mergeFlags(flags
& ~NodeBytecodeNeedsNegZero
);
331 node
->child1()->mergeFlags(NodeBytecodeUsesAsValue
);
332 node
->child2()->mergeFlags(NodeBytecodeUsesAsNumber
| NodeBytecodeUsesAsOther
| NodeBytecodeUsesAsInt
| NodeBytecodeUsesAsArrayIndex
);
336 case NewArrayWithSize
: {
337 node
->child1()->mergeFlags(NodeBytecodeUsesAsValue
| NodeBytecodeUsesAsInt
| NodeBytecodeUsesAsArrayIndex
);
341 case NewTypedArray
: {
342 // Negative zero is not observable. NaN versus undefined are only observable
343 // in that you would get a different exception message. So, like, whatever: we
344 // claim here that NaN v. undefined is observable.
345 node
->child1()->mergeFlags(NodeBytecodeUsesAsInt
| NodeBytecodeUsesAsNumber
| NodeBytecodeUsesAsOther
| NodeBytecodeUsesAsArrayIndex
);
350 node
->child1()->mergeFlags(NodeBytecodeUsesAsValue
);
351 node
->child2()->mergeFlags(NodeBytecodeUsesAsValue
| NodeBytecodeUsesAsInt
| NodeBytecodeUsesAsArrayIndex
);
356 case CallStringConstructor
: {
357 node
->child1()->mergeFlags(NodeBytecodeUsesAsNumber
| NodeBytecodeUsesAsOther
);
362 node
->child1()->mergeFlags(flags
);
368 m_graph
.varArgChild(node
, 0)->mergeFlags(NodeBytecodeUsesAsValue
);
369 m_graph
.varArgChild(node
, 1)->mergeFlags(NodeBytecodeUsesAsNumber
| NodeBytecodeUsesAsOther
| NodeBytecodeUsesAsInt
| NodeBytecodeUsesAsArrayIndex
);
370 m_graph
.varArgChild(node
, 2)->mergeFlags(NodeBytecodeUsesAsValue
);
375 SwitchData
* data
= node
->switchData();
376 switch (data
->kind
) {
378 // We don't need NodeBytecodeNeedsNegZero because if the cases are all integers
379 // then -0 and 0 are treated the same. We don't need NodeBytecodeUsesAsOther
380 // because if all of the cases are integers then NaN and undefined are
381 // treated the same (i.e. they will take default).
382 node
->child1()->mergeFlags(NodeBytecodeUsesAsNumber
| NodeBytecodeUsesAsInt
);
385 // We don't need NodeBytecodeNeedsNegZero because if the cases are all strings
386 // then -0 and 0 are treated the same. We don't need NodeBytecodeUsesAsOther
387 // because if all of the cases are single-character strings then NaN
388 // and undefined are treated the same (i.e. they will take default).
389 node
->child1()->mergeFlags(NodeBytecodeUsesAsNumber
);
393 // We don't need NodeBytecodeNeedsNegZero because if the cases are all strings
394 // then -0 and 0 are treated the same.
395 node
->child1()->mergeFlags(NodeBytecodeUsesAsNumber
| NodeBytecodeUsesAsOther
);
398 // There is currently no point to being clever here since this is used for switching
400 mergeDefaultFlags(node
);
407 // This would be trivial to handle but we just assert that we cannot see these yet.
408 RELEASE_ASSERT_NOT_REACHED();
411 // Note: ArithSqrt, ArithSin, and ArithCos and other math intrinsics don't have special
412 // rules in here because they are always followed by Phantoms to signify that if the
413 // method call speculation fails, the bytecode may use the arguments in arbitrary ways.
414 // This corresponds to that possibility of someone doing something like:
415 // Math.sin = function(x) { doArbitraryThingsTo(x); }
418 mergeDefaultFlags(node
);
423 bool m_allowNestedOverflowingAdditions
;
427 bool performBackwardsPropagation(Graph
& graph
)
429 SamplingRegion
samplingRegion("DFG Backwards Propagation Phase");
430 return runPhase
<BackwardsPropagationPhase
>(graph
);
433 } } // namespace JSC::DFG
435 #endif // ENABLE(DFG_JIT)