@@ -164,6 +164,81 @@ class BackwardsPropagationPhase : public Phase {
164164 return (value || 1.0 / value < 0.0 );
165165 }
166166
167+ // Tests if the absolute value is strictly less than the power of two.
168+ template <int power>
169+ bool isWithinPowerOfTwoForConstant (Node* node)
170+ {
171+ JSValue immediateValue = node->asJSValue ();
172+ if (!immediateValue.isNumber ())
173+ return false ;
174+ double immediate = immediateValue.asNumber ();
175+ return isInteger (immediate) && immediate > -(static_cast <int64_t >(1 ) << power) && immediate < (static_cast <int64_t >(1 ) << power);
176+ }
177+
178+ template <int power>
179+ bool isWithinPowerOfTwo (Node* node)
180+ {
181+ switch (node->op ()) {
182+ case DoubleConstant:
183+ case JSConstant:
184+ case Int52Constant: {
185+ return isWithinPowerOfTwoForConstant<power>(node);
186+ }
187+
188+ case ValueBitAnd:
189+ case ArithBitAnd: {
190+ if (power > 31 )
191+ return true ;
192+
193+ // (x & mask) is only bounded by |mask| when mask is non-negative; a negative
194+ // mask preserves the sign bit and the result can span the full int32 range.
195+ auto isNonNegativeMaskWithinPower = [](Node* operand) {
196+ if (!operand->isNumberConstant ())
197+ return false ;
198+ double immediate = operand->asNumber ();
199+ return immediate >= 0 && immediate < (static_cast <int64_t >(1 ) << power);
200+ };
201+ return isNonNegativeMaskWithinPower (node->child1 ().node ())
202+ || isNonNegativeMaskWithinPower (node->child2 ().node ());
203+ }
204+
205+ case ArithBitOr:
206+ case ArithBitXor:
207+ case ValueBitOr:
208+ case ValueBitXor:
209+ case ValueBitLShift:
210+ case ArithBitLShift: {
211+ return power > 31 ;
212+ }
213+
214+ case ArithBitRShift:
215+ case ValueBitRShift:
216+ case ArithBitURShift:
217+ case ValueBitURShift: {
218+ if (power > 31 )
219+ return true ;
220+
221+ Node* shiftAmount = node->child2 ().node ();
222+ if (!shiftAmount->isNumberConstant ())
223+ return false ;
224+ JSValue immediateValue = shiftAmount->asJSValue ();
225+ if (!immediateValue.isInt32 ())
226+ return false ;
227+ // Shifting right by k = (count & 31) leaves |result| < 2^(32 - k), for both >> and >>>.
228+ return (immediateValue.asInt32 () & 31 ) > 31 - power;
229+ }
230+
231+ default :
232+ return false ;
233+ }
234+ }
235+
236+ template <int power>
237+ bool isWithinPowerOfTwo (Edge edge)
238+ {
239+ return isWithinPowerOfTwo<power>(edge.node ());
240+ }
241+
167242 static bool LEGACY_NODELETE mergeFlags (NodeFlags& flagsRef, NodeFlags newFlags)
168243 {
169244 return checkAndSet (flagsRef, flagsRef | newFlags);
@@ -367,7 +442,7 @@ class BackwardsPropagationPhase : public Phase {
367442 flags &= ~NodeBytecodeNeedsNegZero;
368443 if (node->child1 ()->hasNumericResult () || node->child2 ()->hasNumericResult () || node->child1 ()->hasNumberResult () || node->child2 ()->hasNumberResult ())
369444 flags &= ~NodeBytecodeUsesAsOther;
370- if (!m_graph. isWithinPowerOfTwo <32 >(node->child1 ()) && !m_graph. isWithinPowerOfTwo <32 >(node->child2 ()))
445+ if (!isWithinPowerOfTwo<32 >(node->child1 ()) && !isWithinPowerOfTwo<32 >(node->child2 ()))
371446 flags |= NodeBytecodeUsesAsNumber;
372447 if (!m_allowNestedOverflowingAdditions)
373448 flags |= NodeBytecodeUsesAsNumber;
@@ -382,7 +457,7 @@ class BackwardsPropagationPhase : public Phase {
382457 flags &= ~NodeBytecodeUsesAsOther;
383458 if (isNotNegZero (node->child1 ().node ()) || isNotNegZero (node->child2 ().node ()))
384459 flags &= ~NodeBytecodeNeedsNegZero;
385- if (!m_graph. isWithinPowerOfTwo <32 >(node->child1 ()) && !m_graph. isWithinPowerOfTwo <32 >(node->child2 ()))
460+ if (!isWithinPowerOfTwo<32 >(node->child1 ()) && !isWithinPowerOfTwo<32 >(node->child2 ()))
386461 flags |= NodeBytecodeUsesAsNumber;
387462 if (!m_allowNestedOverflowingAdditions)
388463 flags |= NodeBytecodeUsesAsNumber;
@@ -404,7 +479,7 @@ class BackwardsPropagationPhase : public Phase {
404479 flags &= ~NodeBytecodeUsesAsOther;
405480 if (isNotNegZero (node->child1 ().node ()) || isNotPosZero (node->child2 ().node ()))
406481 flags &= ~NodeBytecodeNeedsNegZero;
407- if (!m_graph. isWithinPowerOfTwo <32 >(node->child1 ()) && !m_graph. isWithinPowerOfTwo <32 >(node->child2 ()))
482+ if (!isWithinPowerOfTwo<32 >(node->child1 ()) && !isWithinPowerOfTwo<32 >(node->child2 ()))
408483 flags |= NodeBytecodeUsesAsNumber;
409484 if (!m_allowNestedOverflowingAdditions)
410485 flags |= NodeBytecodeUsesAsNumber;
@@ -427,7 +502,7 @@ class BackwardsPropagationPhase : public Phase {
427502 case Dec: {
428503 flags &= ~NodeBytecodeNeedsNegZero;
429504 flags &= ~NodeBytecodeUsesAsOther;
430- if (!m_graph. isWithinPowerOfTwo <32 >(node->child1 ()))
505+ if (!isWithinPowerOfTwo<32 >(node->child1 ()))
431506 flags |= NodeBytecodeUsesAsNumber;
432507 if (!m_allowNestedOverflowingAdditions)
433508 flags |= NodeBytecodeUsesAsNumber;
@@ -442,9 +517,14 @@ class BackwardsPropagationPhase : public Phase {
442517 // As soon as a multiply happens, we can easily end up in the part
443518 // of the double domain where the point at which you do truncation
444519 // can change the outcome. So, ArithMul always forces its inputs to
445- // check for overflow. Whether the multiply itself can be truncated
446- // also depends on its product being exact, which Graph::mulProductIsExact
447- // decides from the operands when the multiply is speculated.
520+ // check for overflow. Additionally, it will have to check for overflow
521+ // itself unless we can prove that there is no way for the values
522+ // produced to cause double rounding.
523+
524+ if (!isWithinPowerOfTwo<22 >(node->child1 ().node ())
525+ && !isWithinPowerOfTwo<22 >(node->child2 ().node ()))
526+ flags |= NodeBytecodeUsesAsNumber;
527+
448528 node->mergeFlags (flags);
449529
450530 flags |= NodeBytecodeUsesAsNumber | NodeBytecodeNeedsNegZero | NodeBytecodeNeedsNaNOrInfinity;
@@ -593,7 +673,6 @@ class BackwardsPropagationPhase : public Phase {
593673 }
594674
595675 case Identity:
596- case ValueRep:
597676 ASSERT (m_graph.afterFixup ());
598677 node->child1 ()->mergeFlags (flags);
599678 break ;
0 commit comments