public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114539] New: `__builtin_add_overflow(unsigned, b, &r); r  < b` where b is a CST is not optimized to using the overflow
@ 2024-04-01  2:45 pinskia at gcc dot gnu.org
  2024-04-01  2:47 ` [Bug tree-optimization/114539] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-01  2:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114539
           Summary: `__builtin_add_overflow(unsigned, b, &r); r  < b`
                    where b is a CST is not optimized to using the
                    overflow
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
bool f(unsigned v, unsigned tt)
{
 unsigned r;
 unsigned t = __builtin_add_overflow(v, tt, &r);
 return (r < tt) == t;
}


bool f1(unsigned v, unsigned tt)
{
 tt = 3;
 unsigned r;
 unsigned t = __builtin_add_overflow(v, tt, &r);
 return (r < tt) == t;
}
```

f is able to be optimized to 1 but f1 is not due to the `r < 3` being Canonical
form being `r <= 2` (or in the case of 1, `r == 0`).

I found this while looking into PR 114538 if we change things slightly.

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

* [Bug tree-optimization/114539] `__builtin_add_overflow(unsigned, b, &r); r  < b` where b is a CST is not optimized to using the overflow
  2024-04-01  2:45 [Bug tree-optimization/114539] New: `__builtin_add_overflow(unsigned, b, &r); r < b` where b is a CST is not optimized to using the overflow pinskia at gcc dot gnu.org
@ 2024-04-01  2:47 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-01  2:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=114538
   Last reconfirmed|                            |2024-04-01
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is the current pattern that matches the non-CST case:
```
/* Testing for overflow is unnecessary if we already know the result.  */
/* A - B > A  */
(for cmp (gt le)
     out (ne eq)
 (simplify
  (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
       && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
   (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
/* A + B < A  */
(for cmp (lt ge)
     out (ne eq)
 (simplify
  (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
       && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
   (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
```

Mine for GCC 15.

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

end of thread, other threads:[~2024-04-01  2:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-01  2:45 [Bug tree-optimization/114539] New: `__builtin_add_overflow(unsigned, b, &r); r < b` where b is a CST is not optimized to using the overflow pinskia at gcc dot gnu.org
2024-04-01  2:47 ` [Bug tree-optimization/114539] " 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).