public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/96697] New: Failure to optimize mod+div to 0
@ 2020-08-19  0:47 gabravier at gmail dot com
  2020-08-25 11:04 ` [Bug tree-optimization/96697] " rguenth at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: gabravier at gmail dot com @ 2020-08-19  0:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96697
           Summary: Failure to optimize mod+div to 0
           Product: gcc
           Version: 11.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 f(int x, int y)
{
    return (x % y) / y;
}

This can be optimized to `return 0;`. This transformation is done by LLVM, but
not by GCC.

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

* [Bug tree-optimization/96697] Failure to optimize mod+div to 0
  2020-08-19  0:47 [Bug tree-optimization/96697] New: Failure to optimize mod+div to 0 gabravier at gmail dot com
@ 2020-08-25 11:04 ` rguenth at gcc dot gnu.org
  2021-01-02  9:01 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-08-25 11:04 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-08-25

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.

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

* [Bug tree-optimization/96697] Failure to optimize mod+div to 0
  2020-08-19  0:47 [Bug tree-optimization/96697] New: Failure to optimize mod+div to 0 gabravier at gmail dot com
  2020-08-25 11:04 ` [Bug tree-optimization/96697] " rguenth at gcc dot gnu.org
@ 2021-01-02  9:01 ` jakub at gcc dot gnu.org
  2021-01-02  9:22 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-02  9:01 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |amacleod at redhat dot com,
                   |                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Shall we do that as a specific matcher or e.g. in the ranger once it gets code
for symbolic comparisons?  I mean, for signed t = x % y note that t is in [-y +
1, y + 1] and on the division use that information to determine the division
result range to be [0, 0] ?
It could then handle even e.g. ((unsigned) x % y) / (y + 32) for signed y etc.

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

* [Bug tree-optimization/96697] Failure to optimize mod+div to 0
  2020-08-19  0:47 [Bug tree-optimization/96697] New: Failure to optimize mod+div to 0 gabravier at gmail dot com
  2020-08-25 11:04 ` [Bug tree-optimization/96697] " rguenth at gcc dot gnu.org
  2021-01-02  9:01 ` jakub at gcc dot gnu.org
@ 2021-01-02  9:22 ` jakub at gcc dot gnu.org
  2021-01-04 15:07 ` amacleod at redhat dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-02  9:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
On the other side, we do have:
/* X % Y is smaller than Y.  */
(for cmp (lt ge)
 (simplify
  (cmp (trunc_mod @0 @1) @1)
  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
   { constant_boolean_node (cmp == LT_EXPR, type); })))
(for cmp (gt le)
 (simplify
  (cmp @1 (trunc_mod @0 @1))
  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
   { constant_boolean_node (cmp == GT_EXPR, type); })))
patterns.

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

* [Bug tree-optimization/96697] Failure to optimize mod+div to 0
  2020-08-19  0:47 [Bug tree-optimization/96697] New: Failure to optimize mod+div to 0 gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2021-01-02  9:22 ` jakub at gcc dot gnu.org
@ 2021-01-04 15:07 ` amacleod at redhat dot com
  2021-07-12 19:56 ` amacleod at redhat dot com
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: amacleod at redhat dot com @ 2021-01-04 15:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Macleod <amacleod at redhat dot com> ---
(In reply to Jakub Jelinek from comment #2)
> Shall we do that as a specific matcher or e.g. in the ranger once it gets
> code for symbolic comparisons?  I mean, for signed t = x % y note that t is
> in [-y + 1, y + 1] and on the division use that information to determine the
> division result range to be [0, 0] ?
> It could then handle even e.g. ((unsigned) x % y) / (y + 32) for signed y
> etc.

IN theory the ranger should handle this when relations are available.

    <bb 2> :
    _1 = x_2(D) % y_3(D);       // Establish relation _1 < y_3
    _4 = _1 / y_3(D);
    return _4;

IT should recognize the relation _1 < y_3 from the first statement and when we
calculate _1 / y_3, it again ought to then apply the relation and determine
that _4 = [0,0]

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

* [Bug tree-optimization/96697] Failure to optimize mod+div to 0
  2020-08-19  0:47 [Bug tree-optimization/96697] New: Failure to optimize mod+div to 0 gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2021-01-04 15:07 ` amacleod at redhat dot com
@ 2021-07-12 19:56 ` amacleod at redhat dot com
  2021-08-15 20:58 ` pinskia at gcc dot gnu.org
  2021-08-15 21:33 ` jakub at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: amacleod at redhat dot com @ 2021-07-12 19:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Macleod <amacleod at redhat dot com> ---
(In reply to Andrew Macleod from comment #4)
> (In reply to Jakub Jelinek from comment #2)
> > Shall we do that as a specific matcher or e.g. in the ranger once it gets
> > code for symbolic comparisons?  I mean, for signed t = x % y note that t is
> > in [-y + 1, y + 1] and on the division use that information to determine the
> > division result range to be [0, 0] ?
> > It could then handle even e.g. ((unsigned) x % y) / (y + 32) for signed y
> > etc.
> 
> IN theory the ranger should handle this when relations are available.
> 
>     <bb 2> :
>     _1 = x_2(D) % y_3(D);       // Establish relation _1 < y_3
>     _4 = _1 / y_3(D);
>     return _4;
> 
> IT should recognize the relation _1 < y_3 from the first statement and when
> we calculate _1 / y_3, it again ought to then apply the relation and
> determine that _4 = [0,0]

hum.  but that'd only work for unsigned I guess?  If y_3 is negative, then _1 >
y_3 I believe.   so if y_3 is VARYING, relations won't solve this issue :-P

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

* [Bug tree-optimization/96697] Failure to optimize mod+div to 0
  2020-08-19  0:47 [Bug tree-optimization/96697] New: Failure to optimize mod+div to 0 gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2021-07-12 19:56 ` amacleod at redhat dot com
@ 2021-08-15 20:58 ` pinskia at gcc dot gnu.org
  2021-08-15 21:33 ` jakub at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-15 20:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |missed-optimization
   Last reconfirmed|2020-08-25 00:00:00         |2021-8-15

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

* [Bug tree-optimization/96697] Failure to optimize mod+div to 0
  2020-08-19  0:47 [Bug tree-optimization/96697] New: Failure to optimize mod+div to 0 gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2021-08-15 20:58 ` pinskia at gcc dot gnu.org
@ 2021-08-15 21:33 ` jakub at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-08-15 21:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
For signed x and y, x % y == x % -y, x % y has the sign of x.  So for x in
non-negative you can use x % y < abs(y) and generally -abs(y) < x % y < abs(y)

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

end of thread, other threads:[~2021-08-15 21:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-19  0:47 [Bug tree-optimization/96697] New: Failure to optimize mod+div to 0 gabravier at gmail dot com
2020-08-25 11:04 ` [Bug tree-optimization/96697] " rguenth at gcc dot gnu.org
2021-01-02  9:01 ` jakub at gcc dot gnu.org
2021-01-02  9:22 ` jakub at gcc dot gnu.org
2021-01-04 15:07 ` amacleod at redhat dot com
2021-07-12 19:56 ` amacleod at redhat dot com
2021-08-15 20:58 ` pinskia at gcc dot gnu.org
2021-08-15 21:33 ` jakub 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).