public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/94789] New: Failure to take advantage of shift operand semantics to turn subtraction into negate
@ 2020-04-27  8:56 gabravier at gmail dot com
  2020-04-27 10:24 ` [Bug target/94789] " rguenth at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: gabravier at gmail dot com @ 2020-04-27  8:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94789
           Summary: Failure to take advantage of shift operand semantics
                    to turn subtraction into negate
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

int r(int x, unsigned b)
{
    int const m = CHAR_BIT * sizeof(x) - b;
    return (x << m);
}

`CHAR_BIT * sizeof(x) - b;` can be optimized to `-b`. LLVM does this
transformation, not GCC.

Comparison here : https://godbolt.org/z/5byJ2E

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

* [Bug target/94789] Failure to take advantage of shift operand semantics to turn subtraction into negate
  2020-04-27  8:56 [Bug tree-optimization/94789] New: Failure to take advantage of shift operand semantics to turn subtraction into negate gabravier at gmail dot com
@ 2020-04-27 10:24 ` rguenth at gcc dot gnu.org
  2020-04-27 10:37 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-04-27 10:24 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-04-27
     Ever confirmed|0                           |1
             Target|                            |x86_64-*-* i?86-*-*
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
          Component|tree-optimization           |target

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Hmm, that looks target dependent to me.  We'd take advanage of the targts
handling of out-of-bound shift operands?

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

* [Bug target/94789] Failure to take advantage of shift operand semantics to turn subtraction into negate
  2020-04-27  8:56 [Bug tree-optimization/94789] New: Failure to take advantage of shift operand semantics to turn subtraction into negate gabravier at gmail dot com
  2020-04-27 10:24 ` [Bug target/94789] " rguenth at gcc dot gnu.org
@ 2020-04-27 10:37 ` pinskia at gcc dot gnu.org
  2020-04-27 12:03 ` gabravier at gmail dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-04-27 10:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Maybe (-b)&31 instead? And then the &31 could optimized out later on?

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

* [Bug target/94789] Failure to take advantage of shift operand semantics to turn subtraction into negate
  2020-04-27  8:56 [Bug tree-optimization/94789] New: Failure to take advantage of shift operand semantics to turn subtraction into negate gabravier at gmail dot com
  2020-04-27 10:24 ` [Bug target/94789] " rguenth at gcc dot gnu.org
  2020-04-27 10:37 ` pinskia at gcc dot gnu.org
@ 2020-04-27 12:03 ` gabravier at gmail dot com
  2020-04-29 13:46 ` wilco at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: gabravier at gmail dot com @ 2020-04-27 12:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Gabriel Ravier <gabravier at gmail dot com> ---
>From what I've seen, this optimisation could be useful on at least these
targets : 
- x86_64
- i686
- aarch64

On other architectures I've looked at, either the optimization can't be done
and/or it's useless because those architectures are capable of doing `32 - b`
in a single instruction

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

* [Bug target/94789] Failure to take advantage of shift operand semantics to turn subtraction into negate
  2020-04-27  8:56 [Bug tree-optimization/94789] New: Failure to take advantage of shift operand semantics to turn subtraction into negate gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2020-04-27 12:03 ` gabravier at gmail dot com
@ 2020-04-29 13:46 ` wilco at gcc dot gnu.org
  2021-09-04 22:21 ` pinskia at gcc dot gnu.org
  2024-02-27  8:05 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: wilco at gcc dot gnu.org @ 2020-04-29 13:46 UTC (permalink / raw)
  To: gcc-bugs

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

Wilco <wilco at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |wilco at gcc dot gnu.org

--- Comment #4 from Wilco <wilco at gcc dot gnu.org> ---
(In reply to Gabriel Ravier from comment #0)
> int r(int x, unsigned b)
> {
>     int const m = CHAR_BIT * sizeof(x) - b;
>     return (x << m);
> }
> 
> `CHAR_BIT * sizeof(x) - b;` can be optimized to `-b`. LLVM does this
> transformation, not GCC.
> 
> Comparison here : https://godbolt.org/z/5byJ2E

AArch64 already generates:

  neg w1, w1
  lsl w0, w0, w1
  ret

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

* [Bug target/94789] Failure to take advantage of shift operand semantics to turn subtraction into negate
  2020-04-27  8:56 [Bug tree-optimization/94789] New: Failure to take advantage of shift operand semantics to turn subtraction into negate gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2020-04-29 13:46 ` wilco at gcc dot gnu.org
@ 2021-09-04 22:21 ` pinskia at gcc dot gnu.org
  2024-02-27  8:05 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-04 22:21 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

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

* [Bug target/94789] Failure to take advantage of shift operand semantics to turn subtraction into negate
  2020-04-27  8:56 [Bug tree-optimization/94789] New: Failure to take advantage of shift operand semantics to turn subtraction into negate gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2021-09-04 22:21 ` pinskia at gcc dot gnu.org
@ 2024-02-27  8:05 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-27  8:05 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|x86_64-*-* i?86-*-* aarch64 |x86_64-*-* i?86-*-*

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Wilco from comment #4)
> AArch64 already generates:
> 
>   neg w1, w1
>   lsl w0, w0, w1
>   ret

aarch64 is because it has a pattern to optimize this explictly:
(insn 14 9 15 2 (set (reg/i:SI 0 x0)
        (ashift:SI (reg:SI 108)
            (minus:QI (const_int 32 [0x20])
                (subreg:QI (reg:SI 109) 0)))) "/app/example.cpp":5:1 744
{*aarch64_ashl_reg_minussi3}
     (expr_list:REG_DEAD (reg:SI 108)
        (expr_list:REG_DEAD (reg:SI 109)
            (nil))))

Which was added in r8-3672-g59abe903987d61 .  Maybe the x86_64 backend do a
similar thing?

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

end of thread, other threads:[~2024-02-27  8:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-27  8:56 [Bug tree-optimization/94789] New: Failure to take advantage of shift operand semantics to turn subtraction into negate gabravier at gmail dot com
2020-04-27 10:24 ` [Bug target/94789] " rguenth at gcc dot gnu.org
2020-04-27 10:37 ` pinskia at gcc dot gnu.org
2020-04-27 12:03 ` gabravier at gmail dot com
2020-04-29 13:46 ` wilco at gcc dot gnu.org
2021-09-04 22:21 ` pinskia at gcc dot gnu.org
2024-02-27  8:05 ` 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).