public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/112857] New: Missing optimzation: fold (b + ~a) > 0 to a - b < -1
@ 2023-12-05  6:16 xxs_chy at outlook dot com
  2023-12-05  7:10 ` [Bug tree-optimization/112857] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: xxs_chy at outlook dot com @ 2023-12-05  6:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112857

            Bug ID: 112857
           Summary: Missing optimzation: fold (b + ~a) > 0 to a - b < -1
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xxs_chy at outlook dot com
  Target Milestone: ---

Godbolt link example: https://godbolt.org/z/Pba4Y164f
For c code like:


bool src(int a, int b){
    return (b + ~a) > 0;
}

can be folded to:


bool tgt(int a, int b){
    return a - b < -1;
}


But both GCC and LLVM missed it.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/112857] Missing optimzation: fold (b + ~a) > 0 to a - b < -1
  2023-12-05  6:16 [Bug tree-optimization/112857] New: Missing optimzation: fold (b + ~a) > 0 to a - b < -1 xxs_chy at outlook dot com
@ 2023-12-05  7:10 ` pinskia at gcc dot gnu.org
  2023-12-05  7:47 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-05  7:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112857

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
   Last reconfirmed|                            |2023-12-05
     Ever confirmed|0                           |1
                 CC|                            |pinskia at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. I was actually going to file this one but my access to gcc.gnu.org
was not working all day.

Note all of the following testcases should produce the same code too:
```
int g0(int a, int b)
{
        return b > a;
}

int g10(int a, int b)
{
        return (b-a) > 0;
}

int g(int a, int b)
{
  return (b + ~ a) >= 0;
}


int g1(int a, int b)
{
  return (b - a) -1 >= 0;
}

int g2(int a, int b)
{
  return (b +  (-a-1)) >= 0;
}



int g3(int a, int b)
{
  return (b + ~ a) + 1 > 0;
}

int g4(int a, int b)
{
  return (b + (~ a + 1)) > 0;
}
```

For the above, GCC is able to get the best code for g0, g10, g1, and g4 (though
g1 and g4 are still `(b-a) > 0` at the gimple level. While LLVM is able to get
it for g0, g10 and g4 (g3 is close though with `(b-a) > 0`).

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/112857] Missing optimzation: fold (b + ~a) > 0 to a - b < -1
  2023-12-05  6:16 [Bug tree-optimization/112857] New: Missing optimzation: fold (b + ~a) > 0 to a - b < -1 xxs_chy at outlook dot com
  2023-12-05  7:10 ` [Bug tree-optimization/112857] " pinskia at gcc dot gnu.org
@ 2023-12-05  7:47 ` rguenth at gcc dot gnu.org
  2023-12-05 10:19 ` xxs_chy at outlook dot com
  2023-12-05 10:20 ` xxs_chy at outlook dot com
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-05  7:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112857

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
careful about overflow.  Also note compares against zero might be cheaper (but
that's eventually an RTL expansion thing).

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/112857] Missing optimzation: fold (b + ~a) > 0 to a - b < -1
  2023-12-05  6:16 [Bug tree-optimization/112857] New: Missing optimzation: fold (b + ~a) > 0 to a - b < -1 xxs_chy at outlook dot com
  2023-12-05  7:10 ` [Bug tree-optimization/112857] " pinskia at gcc dot gnu.org
  2023-12-05  7:47 ` rguenth at gcc dot gnu.org
@ 2023-12-05 10:19 ` xxs_chy at outlook dot com
  2023-12-05 10:20 ` xxs_chy at outlook dot com
  3 siblings, 0 replies; 5+ messages in thread
From: xxs_chy at outlook dot com @ 2023-12-05 10:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112857

--- Comment #3 from XChy <xxs_chy at outlook dot com> ---
(In reply to Richard Biener from comment #2)
> careful about overflow. 

I'm not a developer of GCC, but for "(b + ~a) > 0 -> a - b < -1", I can say
it's a valid refinement for both signed and unsigned, with SMT verfication of
LLVM IR.

> Also note compares against zero might be cheaper (but that's eventually an RTL 
> expansion thing).

Yes, that depends on specific platform, and may need to handle in backends.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/112857] Missing optimzation: fold (b + ~a) > 0 to a - b < -1
  2023-12-05  6:16 [Bug tree-optimization/112857] New: Missing optimzation: fold (b + ~a) > 0 to a - b < -1 xxs_chy at outlook dot com
                   ` (2 preceding siblings ...)
  2023-12-05 10:19 ` xxs_chy at outlook dot com
@ 2023-12-05 10:20 ` xxs_chy at outlook dot com
  3 siblings, 0 replies; 5+ messages in thread
From: xxs_chy at outlook dot com @ 2023-12-05 10:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112857

--- Comment #4 from XChy <xxs_chy at outlook dot com> ---
(In reply to Andrew Pinski from comment #1)

> For the above, GCC is able to get the best code for g0, g10, g1, and g4
> (though g1 and g4 are still `(b-a) > 0` at the gimple level. While LLVM is
> able to get it for g0, g10 and g4 (g3 is close though with `(b-a) > 0`).

Thanks for generalizing this fold!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-12-05 10:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-05  6:16 [Bug tree-optimization/112857] New: Missing optimzation: fold (b + ~a) > 0 to a - b < -1 xxs_chy at outlook dot com
2023-12-05  7:10 ` [Bug tree-optimization/112857] " pinskia at gcc dot gnu.org
2023-12-05  7:47 ` rguenth at gcc dot gnu.org
2023-12-05 10:19 ` xxs_chy at outlook dot com
2023-12-05 10:20 ` xxs_chy at outlook dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).