public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/112900] New: Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)`  (or reversely)
@ 2023-12-07 10:38 xxs_chy at outlook dot com
  2023-12-07 10:51 ` [Bug tree-optimization/112900] " rguenth 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-07 10:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112900
           Summary: Missing optimization: canonicalize `select c, x - 1, x
                    + 1` to `x + (select c, -1, 1)`  (or reversely)
           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: ---

GCC works much better on folding branches into select than llvm for:
https://godbolt.org/z/jWzePjqTs

But GCC seems to generate different X86 assembly for the code
below(https://godbolt.org/z/Erq56Tbjq):

int src(int x, int y, bool cond) {
    return (x > y ? x - 1 : x + 1);
}

int tgt(int x, int y, bool cond) {
    return x + (x > y ? -1 : 1);
}

For `src`, we compute both `x-1` and `x+1` and apply `cmov` to return one of
them.
For `tgt`, we fold `(x > y ? -1 : 1)` to `(x > y) * 2 - 1`.

LLVM prefers the latter, so I think it may be better to canonicalize `select c,
x - 1, x + 1` to `x + (select c, -1, 1)`. Though it's uncanonicalized in
tree-optimization stage, please let me know if there is other backend's factors
like ILP deciding that.

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

* [Bug tree-optimization/112900] Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)`  (or reversely)
  2023-12-07 10:38 [Bug tree-optimization/112900] New: Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)` (or reversely) xxs_chy at outlook dot com
@ 2023-12-07 10:51 ` rguenth at gcc dot gnu.org
  2023-12-07 19:24 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-07 10:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think at least fold has/had some of this folding one variant into the other.

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

* [Bug tree-optimization/112900] Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)`  (or reversely)
  2023-12-07 10:38 [Bug tree-optimization/112900] New: Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)` (or reversely) xxs_chy at outlook dot com
  2023-12-07 10:51 ` [Bug tree-optimization/112900] " rguenth at gcc dot gnu.org
@ 2023-12-07 19:24 ` pinskia at gcc dot gnu.org
  2023-12-07 19:25 ` pinskia at gcc dot gnu.org
  2023-12-07 19:25 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-07 19:24 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|                            |x86_64
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=112403
           Keywords|                            |missed-optimization
           Severity|normal                      |enhancement
                 CC|                            |pinskia at gcc dot gnu.org

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note for aarch64, the src is better than tgt here.  See PR 112403 which I filed
while working on PR 94274 .

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

* [Bug tree-optimization/112900] Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)`  (or reversely)
  2023-12-07 10:38 [Bug tree-optimization/112900] New: Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)` (or reversely) xxs_chy at outlook dot com
  2023-12-07 10:51 ` [Bug tree-optimization/112900] " rguenth at gcc dot gnu.org
  2023-12-07 19:24 ` pinskia at gcc dot gnu.org
@ 2023-12-07 19:25 ` pinskia at gcc dot gnu.org
  2023-12-07 19:25 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-07 19:25 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=111078

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
PR 111078 is also related.

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

* [Bug tree-optimization/112900] Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)`  (or reversely)
  2023-12-07 10:38 [Bug tree-optimization/112900] New: Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)` (or reversely) xxs_chy at outlook dot com
                   ` (2 preceding siblings ...)
  2023-12-07 19:25 ` pinskia at gcc dot gnu.org
@ 2023-12-07 19:25 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-07 19:25 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-12-07
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

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

end of thread, other threads:[~2023-12-07 19:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-07 10:38 [Bug tree-optimization/112900] New: Missing optimization: canonicalize `select c, x - 1, x + 1` to `x + (select c, -1, 1)` (or reversely) xxs_chy at outlook dot com
2023-12-07 10:51 ` [Bug tree-optimization/112900] " rguenth at gcc dot gnu.org
2023-12-07 19:24 ` pinskia at gcc dot gnu.org
2023-12-07 19:25 ` pinskia at gcc dot gnu.org
2023-12-07 19:25 ` pinskia at gcc dot gnu.org

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).