public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/105777] New: Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check
@ 2022-05-30 20:37 gabravier at gmail dot com
  2022-05-30 20:44 ` [Bug middle-end/105777] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gabravier at gmail dot com @ 2022-05-30 20:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105777
           Summary: Failure to optimize __builtin_mul_overflow with
                    constant operand to add+cmp check
           Product: gcc
           Version: 13.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 f17(unsigned x)
{
    int z;
    return __builtin_mul_overflow((int)x, 35, &z);
}

This can be optimized to `return (x + 0xFC57C57C) < 0xF8AF8AF9;` (and I'd
assume the same pattern with other constants than 35 should be optimizable in
the same way). LLVM does this transformation, but GCC does not.

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

* [Bug middle-end/105777] Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check
  2022-05-30 20:37 [Bug tree-optimization/105777] New: Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check gabravier at gmail dot com
@ 2022-05-30 20:44 ` pinskia at gcc dot gnu.org
  2022-05-31 13:36 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-05-30 20:44 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-05-30
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
           Severity|normal                      |enhancement

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
It depends on how fast the multiply is so it is a middle-end issue where expand
should be able to do it if the multiply is slow enough.

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

* [Bug middle-end/105777] Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check
  2022-05-30 20:37 [Bug tree-optimization/105777] New: Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check gabravier at gmail dot com
  2022-05-30 20:44 ` [Bug middle-end/105777] " pinskia at gcc dot gnu.org
@ 2022-05-31 13:36 ` jakub at gcc dot gnu.org
  2022-06-03  9:42 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-05-31 13:36 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Right now, x86, riscv and s390 are the only targets with mulv<mode>4 and the
former 2 with umulv<mode>4 support, so perhaps it might be enough to just
compare  how those sequences compare on those 2/3 arches.

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

* [Bug middle-end/105777] Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check
  2022-05-30 20:37 [Bug tree-optimization/105777] New: Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check gabravier at gmail dot com
  2022-05-30 20:44 ` [Bug middle-end/105777] " pinskia at gcc dot gnu.org
  2022-05-31 13:36 ` jakub at gcc dot gnu.org
@ 2022-06-03  9:42 ` cvs-commit at gcc dot gnu.org
  2022-06-03  9:51 ` jakub at gcc dot gnu.org
  2022-10-24 17:45 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-06-03  9:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 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:1982fe2692b6c3b7f969ffc4edac59f9d4359e91

commit r13-979-g1982fe2692b6c3b7f969ffc4edac59f9d4359e91
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Jun 3 11:41:21 2022 +0200

    match.pd: Optimize __builtin_mul_overflow_p (x, cst, (stype)0) [PR105777]

    The following patch is an incremental change to the PR30314 enhancement,
    this one handles signed types.
    For signed types (but still, the same for 1st and result element type
    and non-zero constant that fits into that type), we actually need to
    watch for overflow in direction to positive and negative infinity
    and it also depends on whether the cst operand is positive or negative.
    For __builtin_mul_overflow_p (x, cst, (stype) 0):
    For cst > 0, we can simplify it to:
    x > INT_MAX / cst || x < INT_MIN / cst
    aka:
    x + (unsigned) (INT_MIN / cst) > (unsigned) (INT_MAX / cst) - (unsigned)
(INT_MIN / cst)
    and for cst < 0 to:
    x < INT_MAX / cst || x > INT_MIN / cst
    aka:
    x + (unsigned) (INT_MAX / cst) > (unsigned) (INT_MIN / cst) - (unsigned)
(INT_MAX / cst)

    Additionally, I've added executable testcases, so we don't just check for
    the optimization to be performed, but also that it is correct (done that
    even for the other PR's testcase).

    2022-06-03  Jakub Jelinek  <jakub@redhat.com>

            PR middle-end/30314
            PR middle-end/105777
            * match.pd (__builtin_mul_overflow_p (x, cst, (stype) 0) ->
            x > stype_max / cst || x < stype_min / cst): New simplification.

            * gcc.dg/tree-ssa/pr30314.c: Add noipa attribute to all functions.
            * gcc.dg/tree-ssa/pr105777.c: New test.
            * gcc.c-torture/execute/pr30314.c: New test.
            * gcc.c-torture/execute/pr105777.c: New test.

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

* [Bug middle-end/105777] Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check
  2022-05-30 20:37 [Bug tree-optimization/105777] New: Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2022-06-03  9:42 ` cvs-commit at gcc dot gnu.org
@ 2022-06-03  9:51 ` jakub at gcc dot gnu.org
  2022-10-24 17:45 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-03  9:51 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

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

* [Bug middle-end/105777] Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check
  2022-05-30 20:37 [Bug tree-optimization/105777] New: Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2022-06-03  9:51 ` jakub at gcc dot gnu.org
@ 2022-10-24 17:45 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-24 17:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.0

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

end of thread, other threads:[~2022-10-24 17:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-30 20:37 [Bug tree-optimization/105777] New: Failure to optimize __builtin_mul_overflow with constant operand to add+cmp check gabravier at gmail dot com
2022-05-30 20:44 ` [Bug middle-end/105777] " pinskia at gcc dot gnu.org
2022-05-31 13:36 ` jakub at gcc dot gnu.org
2022-06-03  9:42 ` cvs-commit at gcc dot gnu.org
2022-06-03  9:51 ` jakub at gcc dot gnu.org
2022-10-24 17:45 ` 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).