public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/95853] New: Failure to optimize add overflow pattern to __builtin_add_overflow
@ 2020-06-23 22:09 gabravier at gmail dot com
  2020-06-24 16:48 ` [Bug tree-optimization/95853] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: gabravier at gmail dot com @ 2020-06-23 22:09 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95853
           Summary: Failure to optimize add overflow pattern to
                    __builtin_add_overflow
           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: ---

struct result_struct
{
    bool p;
    uint64_t r;
};

result_struct size_add_overflow(uint64_t x, uint64_t y)
{
    unsigned __int128 z = (unsigned __int128)x + (unsigned __int128)y;
    return {z > SIZE_MAX, (size_t)z};
}

This can be optimized to 

result_struct size_add_overflow(uint64_t x, uint64_t y)
{ 
    uint64_t result;
    return {__builtin_add_overflow(x, y, &result), result};
}

This transformation is done by LLVM, but not by GCC.

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

* [Bug tree-optimization/95853] Failure to optimize add overflow pattern to __builtin_add_overflow
  2020-06-23 22:09 [Bug tree-optimization/95853] New: Failure to optimize add overflow pattern to __builtin_add_overflow gabravier at gmail dot com
@ 2020-06-24 16:48 ` pinskia at gcc dot gnu.org
  2020-11-20 15:48 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-06-24 16:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

* [Bug tree-optimization/95853] Failure to optimize add overflow pattern to __builtin_add_overflow
  2020-06-23 22:09 [Bug tree-optimization/95853] New: Failure to optimize add overflow pattern to __builtin_add_overflow gabravier at gmail dot com
  2020-06-24 16:48 ` [Bug tree-optimization/95853] " pinskia at gcc dot gnu.org
@ 2020-11-20 15:48 ` jakub at gcc dot gnu.org
  2020-11-22 18:17 ` cvs-commit at gcc dot gnu.org
  2020-11-24 10:41 ` jakub at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-11-20 15:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 49606
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49606&action=edit
gcc11-pr95853-wip.patch

I've tried to implement this, but on:
#if __SIZEOF_INT128__
typedef __uint128_t W;
typedef unsigned long long T;
#else
typedef unsigned long long W;
typedef unsigned int T;
#endif

struct S { int p; T r; };

struct S
foo (T x, T y)
{
  W z = (W) x + y;
  return (struct S) { z > ~(T) 0, (T) z };
}

struct S
bar (T x)
{
  W z = (W) x + 132;
  return (struct S) { z > ~(T) 0, (T) z };
}

struct S
baz (T x, unsigned short y)
{
  W z = (W) x + y;
  return (struct S) { z > ~(T) 0, (T) z };
}

struct S
qux (unsigned short x, T y)
{
  W z = (W) x + y;
  return (struct S) { z > ~(T) 0, (T) z };
}

it actually handles only the baz and qux functions.  The problem is that for
foo and bar forwprop1 (for this optimization unhelpfully) forward propagates
the addition, so we end up with:
  _1 = (__int128 unsigned) x_6(D);
  _2 = (__int128 unsigned) y_7(D);
  z_8 = _1 + _2;
  _3 = z_8 > 18446744073709551615;
  _4 = (int) _3;
  _5 = x_6(D) + y_7(D);
  D.1971.p = _4;
  D.1971.r = _5;
  return D.1971;
and there is no (easy) connection between the two additions.  I guess I could
look through immediate uses of the rhs1's setter in that case.

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

* [Bug tree-optimization/95853] Failure to optimize add overflow pattern to __builtin_add_overflow
  2020-06-23 22:09 [Bug tree-optimization/95853] New: Failure to optimize add overflow pattern to __builtin_add_overflow gabravier at gmail dot com
  2020-06-24 16:48 ` [Bug tree-optimization/95853] " pinskia at gcc dot gnu.org
  2020-11-20 15:48 ` jakub at gcc dot gnu.org
@ 2020-11-22 18:17 ` cvs-commit at gcc dot gnu.org
  2020-11-24 10:41 ` jakub at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-11-22 18:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:c1fb592f2c3c6b5a6616cf882ce24d30e167a646

commit r11-5238-gc1fb592f2c3c6b5a6616cf882ce24d30e167a646
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Sun Nov 22 19:16:34 2020 +0100

    widening_mul: pattern recognize further forms of __builtin_add_overflow
[PR95853]

    The following patch recognizes some further forms of additions with
overflow
    checks as shown in the testcase, in particular where the unsigned addition
is
    performed in a wider mode just to catch overflow with a >
narrower_utype_max
    check.

    2020-11-22  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/95853
            * tree-ssa-math-opts.c (uaddsub_overflow_check_p): Add maxval
            argument, if non-NULL, instead look for r > maxval or r <= maxval
            comparisons.
            (match_uaddsub_overflow): Pattern recognize even other forms of
            __builtin_add_overflow, in particular when addition is performed
            in a wider type and result compared to maximum of the narrower
            type.

            * gcc.dg/pr95853.c: New test.

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

* [Bug tree-optimization/95853] Failure to optimize add overflow pattern to __builtin_add_overflow
  2020-06-23 22:09 [Bug tree-optimization/95853] New: Failure to optimize add overflow pattern to __builtin_add_overflow gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2020-11-22 18:17 ` cvs-commit at gcc dot gnu.org
@ 2020-11-24 10:41 ` jakub at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-11-24 10:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2020-11-24 10:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-23 22:09 [Bug tree-optimization/95853] New: Failure to optimize add overflow pattern to __builtin_add_overflow gabravier at gmail dot com
2020-06-24 16:48 ` [Bug tree-optimization/95853] " pinskia at gcc dot gnu.org
2020-11-20 15:48 ` jakub at gcc dot gnu.org
2020-11-22 18:17 ` cvs-commit at gcc dot gnu.org
2020-11-24 10:41 ` 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).