public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/105119] New: the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed
@ 2022-03-31 18:03 ppalka at gcc dot gnu.org
  2022-04-01  6:22 ` [Bug rtl-optimization/105119] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-03-31 18:03 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105119
           Summary: the division in x / (1 << y) is optimized away when x
                    has unsigned type, but not when it's signed
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ppalka at gcc dot gnu.org
  Target Milestone: ---

For the following on x86_64 with -O2

int f(int x, int y) {
  return x / (1 << y);
}

GCC generates an idiv instruction:

        movl    %esi, %ecx
        movl    $1, %edx
        movl    %edi, %eax
        sall    %cl, %edx
        movl    %edx, %ecx
        cltd
        idivl   %ecx
        ret

But I believe this is equivalent to the division-less

int g(int x, int y) {
  return (x + (x < 0) * ((1 << y) - 1)) >> y;
}

(which basically generalizes the existing x / (1 << y) -> x >> y transformation
that we perform for unsigned x).  For this latter function, we generate

        movl    %esi, %ecx
        movl    $1, %eax
        xorl    %edx, %edx
        sall    %cl, %eax
        subl    $1, %eax
        testl   %edi, %edi
        cmovns  %edx, %eax
        addl    %edi, %eax
        sarl    %cl, %eax
        ret

which seems to be significantly faster according to some rough benchmarks.

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

* [Bug rtl-optimization/105119] the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed
  2022-03-31 18:03 [Bug rtl-optimization/105119] New: the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed ppalka at gcc dot gnu.org
@ 2022-04-01  6:22 ` rguenth at gcc dot gnu.org
  2022-11-28 20:04 ` [Bug middle-end/105119] " pinskia at gcc dot gnu.org
  2022-11-28 20:09 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-01  6:22 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think it's because arithmetic right-shift of negative values is undefined?

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

* [Bug middle-end/105119] the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed
  2022-03-31 18:03 [Bug rtl-optimization/105119] New: the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed ppalka at gcc dot gnu.org
  2022-04-01  6:22 ` [Bug rtl-optimization/105119] " rguenth at gcc dot gnu.org
@ 2022-11-28 20:04 ` pinskia at gcc dot gnu.org
  2022-11-28 20:09 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-28 20:04 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
          Component|rtl-optimization            |middle-end

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

* [Bug middle-end/105119] the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed
  2022-03-31 18:03 [Bug rtl-optimization/105119] New: the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed ppalka at gcc dot gnu.org
  2022-04-01  6:22 ` [Bug rtl-optimization/105119] " rguenth at gcc dot gnu.org
  2022-11-28 20:04 ` [Bug middle-end/105119] " pinskia at gcc dot gnu.org
@ 2022-11-28 20:09 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-28 20:09 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-11-28
     Ever confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The unsigned case is handled with r8-1550-g71f82be94fad15 .

The comment in match.pd is now:
/* (A / (1 << B)) -> (A >> B).
   Only for unsigned A.  For signed A, this would not preserve rounding
   toward zero.
   For example: (-1 / ( 1 << B)) !=  -1 >> B.
   Also also [handle] widening conversions, like:
   (A / (unsigned long long) (1U << B)) -> (A >> B)
   or
   (A / (unsigned long long) (1 << B)) -> (A >> B).
   If the left shift is signed, it can be done only if the upper bits
   of A starting from shift's type sign bit are zero, as
   (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
   so it is valid only if A >> 31 is zero.  */

Confirmed.

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

end of thread, other threads:[~2022-11-28 20:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-31 18:03 [Bug rtl-optimization/105119] New: the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed ppalka at gcc dot gnu.org
2022-04-01  6:22 ` [Bug rtl-optimization/105119] " rguenth at gcc dot gnu.org
2022-11-28 20:04 ` [Bug middle-end/105119] " pinskia at gcc dot gnu.org
2022-11-28 20:09 ` 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).