Bug 151164

Summary: [JSC] Support Doubles with B3's Add
Product: WebKit Reporter: Benjamin Poulain <benjamin>
Component: New BugsAssignee: Benjamin Poulain <benjamin>
Status: RESOLVED FIXED    
Severity: Normal CC: fpizlo
Priority: P2    
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch fpizlo: review+

Description Benjamin Poulain 2015-11-11 16:25:41 PST
[JSC] Support Doubles with B3's Add
Comment 1 Benjamin Poulain 2015-11-11 16:27:16 PST
Created attachment 265333 [details]
Patch
Comment 2 Geoffrey Garen 2015-11-11 16:32:58 PST
Comment on attachment 265333 [details]
Patch

r=me
Comment 3 Filip Pizlo 2015-11-11 16:58:25 PST
Comment on attachment 265333 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=265333&action=review

> Source/JavaScriptCore/b3/B3ReduceStrength.cpp:172
> +            if (m_value->child(1)->isLikeZero()) {

Does this do the right thing for -0?
Comment 4 Benjamin Poulain 2015-11-11 17:06:24 PST
(In reply to comment #3)
> Comment on attachment 265333 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=265333&action=review
> 
> > Source/JavaScriptCore/b3/B3ReduceStrength.cpp:172
> > +            if (m_value->child(1)->isLikeZero()) {
> 
> Does this do the right thing for -0?

Yep :)
Comment 5 Filip Pizlo 2015-11-11 17:08:58 PST
(In reply to comment #4)
> (In reply to comment #3)
> > Comment on attachment 265333 [details]
> > Patch
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=265333&action=review
> > 
> > > Source/JavaScriptCore/b3/B3ReduceStrength.cpp:172
> > > +            if (m_value->child(1)->isLikeZero()) {
> > 
> > Does this do the right thing for -0?
> 
> Yep :)

Can you add a comment about why? :-)

For example, does isLikeZero return true for -0?  Even if this was documented on the method, it would help to say this here. If it does work for -0, then it would be worth explaining why this handles both -0 and 0 as the other value. A table or something that explains this would help for example if we wanted to try to expand this optimization with some more sophisticated algebra.
Comment 6 Benjamin Poulain 2015-11-11 17:33:11 PST
(In reply to comment #5)
> Can you add a comment about why? :-)
> 
> For example, does isLikeZero return true for -0?  Even if this was
> documented on the method, it would help to say this here. If it does work
> for -0, then it would be worth explaining why this handles both -0 and 0 as
> the other value. A table or something that explains this would help for
> example if we wanted to try to expand this optimization with some more
> sophisticated algebra.

If you prefer something more explicit, what do you think of:
    bool isZero()
    bool isZeroOrMinusZero()
on Value?
Comment 7 Filip Pizlo 2015-11-11 17:40:12 PST
(In reply to comment #6)
> (In reply to comment #5)
> > Can you add a comment about why? :-)
> > 
> > For example, does isLikeZero return true for -0?  Even if this was
> > documented on the method, it would help to say this here. If it does work
> > for -0, then it would be worth explaining why this handles both -0 and 0 as
> > the other value. A table or something that explains this would help for
> > example if we wanted to try to expand this optimization with some more
> > sophisticated algebra.
> 
> If you prefer something more explicit, what do you think of:
>     bool isZero()
>     bool isZeroOrMinusZero()
> on Value?

I think that isLikeZero() essentially means -0, and I'm fine with the name.

I believe that your optimization assumes that the following is true:

0 + 0 = 0
0 + -0 = 0
-0 + 0 = -0
-0 + -0 = -0

But I don't think that this is true.  Here's what I found, in both jsc and lldb:

0 + 0 = 0
0 + -0 = 0
-0 + 0 = 0
-0 + -0 = -0

In other words, when child(1) is 0, this operation acts as a -0 clean-up filter, which is not the same as identity.  When child(1) is -0 then it appears that this is indeed an identity.

So, either your optimization is wrong, or you need some comments to describe why it's right.  That comment should start with pointing out that this optimization applies to both 0 and -0, and you should explain why it behaves like identity in both cases.
Comment 8 Filip Pizlo 2015-11-11 17:40:38 PST
(In reply to comment #7)
> (In reply to comment #6)
> > (In reply to comment #5)
> > > Can you add a comment about why? :-)
> > > 
> > > For example, does isLikeZero return true for -0?  Even if this was
> > > documented on the method, it would help to say this here. If it does work
> > > for -0, then it would be worth explaining why this handles both -0 and 0 as
> > > the other value. A table or something that explains this would help for
> > > example if we wanted to try to expand this optimization with some more
> > > sophisticated algebra.
> > 
> > If you prefer something more explicit, what do you think of:
> >     bool isZero()
> >     bool isZeroOrMinusZero()
> > on Value?
> 
> I think that isLikeZero() essentially means -0, and I'm fine with the name.

*essentially means 0 or -0.
Comment 9 Benjamin Poulain 2015-11-11 17:57:07 PST
Comment on attachment 265333 [details]
Patch

Yeah, looks like I got that wrong.
I just trusted the tests without much thinking.
Comment 10 Benjamin Poulain 2015-11-11 18:43:25 PST
Created attachment 265345 [details]
Patch
Comment 11 Filip Pizlo 2015-11-11 18:46:22 PST
Comment on attachment 265345 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=265345&action=review

> Source/JavaScriptCore/b3/B3ReduceStrength.cpp:172
>              // Turn this: Add(value, zero)
>              // Into an Identity.
> -            if (m_value->child(1)->isInt(0)) {
> +            if (m_value->child(1)->isInt(0) || m_value->child(1)->isNegativeZero()) {

It would be great to say in the comment why this only works for -0, so that somebody doesn't just assume this is a bug.
Comment 12 Benjamin Poulain 2015-11-11 21:12:17 PST
Committed r192347: <http://trac.webkit.org/changeset/192347>