public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/94757] New: GCC does not optimise unsigned multiplication known not to overflow
@ 2020-04-25  8:55 felix.von.s at posteo dot de
  2020-04-27  6:35 ` [Bug tree-optimization/94757] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: felix.von.s at posteo dot de @ 2020-04-25  8:55 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94757
           Summary: GCC does not optimise unsigned multiplication known
                    not to overflow
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: felix.von.s at posteo dot de
  Target Milestone: ---

This code:

    #include <limits.h>

    unsigned f(unsigned x) {
        if (x > UINT_MAX / 3)
            __builtin_unreachable();
        return (x * 3) / 3;
    }

should be possible to optimise into the identity function, but (on x86_64) gcc
-O3 generates a multiplication (via LEA) then a division (via multiplication).
GCC knows that the multiplication cannot overflow, because replacing the
returned expression with __builtin_mul_overflow_p(x, 3, x) is makes it optimise
to returning constant 0.

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

* [Bug tree-optimization/94757] GCC does not optimise unsigned multiplication known not to overflow
  2020-04-25  8:55 [Bug tree-optimization/94757] New: GCC does not optimise unsigned multiplication known not to overflow felix.von.s at posteo dot de
@ 2020-04-27  6:35 ` rguenth at gcc dot gnu.org
  2020-06-19  8:09 ` qianjh at cn dot fujitsu.com
  2021-12-15 23:56 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-04-27  6:35 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-04-27
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

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

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

* [Bug tree-optimization/94757] GCC does not optimise unsigned multiplication known not to overflow
  2020-04-25  8:55 [Bug tree-optimization/94757] New: GCC does not optimise unsigned multiplication known not to overflow felix.von.s at posteo dot de
  2020-04-27  6:35 ` [Bug tree-optimization/94757] " rguenth at gcc dot gnu.org
@ 2020-06-19  8:09 ` qianjh at cn dot fujitsu.com
  2021-12-15 23:56 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: qianjh at cn dot fujitsu.com @ 2020-06-19  8:09 UTC (permalink / raw)
  To: gcc-bugs

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

Qian Jianhua <qianjh at cn dot fujitsu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |qianjh at cn dot fujitsu.com

--- Comment #2 from Qian Jianhua <qianjh at cn dot fujitsu.com> ---
>GCC knows that the multiplication cannot overflow, because replacing the 
>returned expression with __builtin_mul_overflow_p(x, 3, x) is makes it optimise 
>to returning constant 0.
__builtin_mul_overflow_p function only check the value range. So it could be
optimised by VRP.

The process like this
   x     range [0, UINT_MAX/3]
   x*3   range [0, UINT_MAX]
   x*3/3 range [0, UINT_MAX/3], but not calculate the result(=x).

I tested some other cases. 
It seems that the optimiser in gcc only check the value range of the
expression, but not track/calculate the result of the expression, if the
variable is not constant int the expression.


So i think such optimisation of expression is not supported in gcc now.

In clang, the expression "(x * 3) / 3" is optimised to "x".

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

* [Bug tree-optimization/94757] GCC does not optimise unsigned multiplication known not to overflow
  2020-04-25  8:55 [Bug tree-optimization/94757] New: GCC does not optimise unsigned multiplication known not to overflow felix.von.s at posteo dot de
  2020-04-27  6:35 ` [Bug tree-optimization/94757] " rguenth at gcc dot gnu.org
  2020-06-19  8:09 ` qianjh at cn dot fujitsu.com
@ 2021-12-15 23:56 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-15 23:56 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |11.0
           Severity|normal                      |enhancement
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=97997
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed for GCC 11 by r11-5444-ga3ebc13492ff23 (PR 97997), this is almost a dup
of bug 97997 but not exactly.

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

end of thread, other threads:[~2021-12-15 23:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-25  8:55 [Bug tree-optimization/94757] New: GCC does not optimise unsigned multiplication known not to overflow felix.von.s at posteo dot de
2020-04-27  6:35 ` [Bug tree-optimization/94757] " rguenth at gcc dot gnu.org
2020-06-19  8:09 ` qianjh at cn dot fujitsu.com
2021-12-15 23:56 ` 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).