public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/68131] New: missed optimization and warning for broken overflow check
@ 2015-10-28 11:39 rv at rasmusvillemoes dot dk
  2015-10-28 12:19 ` [Bug tree-optimization/68131] " glisse at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: rv at rasmusvillemoes dot dk @ 2015-10-28 11:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 68131
           Summary: missed optimization and warning for broken overflow
                    check
           Product: gcc
           Version: 5.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rv at rasmusvillemoes dot dk
  Target Milestone: ---

Using "a + b < a" is the standard (and well-defined) way of checking for
overflow when adding unsigned variables a,b. However, due to promotion rules,
this breaks down when a and b have type narrower than int. Consider

struct s {
        unsigned short x;
};

int f(struct s *a, const struct s *b)
{
        if (a->x + b->x < a->x)
                return -1;
        a->x += b->x;
        return 0;
}

The conditional is never true, but neither clang or gcc warns (with -Wall
-Wextra) about what was obviously intended to be an overflow check. clang does
compile this to

   0:   66 8b 06                mov    (%rsi),%ax
   3:   66 01 07                add    %ax,(%rdi)
   6:   31 c0                   xor    %eax,%eax
   8:   c3                      retq   

whereas gcc generates

   0:   0f b7 0f                movzwl (%rdi),%ecx
   3:   0f b7 16                movzwl (%rsi),%edx
   6:   89 d0                   mov    %edx,%eax
   8:   01 ca                   add    %ecx,%edx
   a:   39 d1                   cmp    %edx,%ecx
   c:   7f 12                   jg     20 <f+0x20>
   e:   01 c8                   add    %ecx,%eax
  10:   66 89 07                mov    %ax,(%rdi)
  13:   31 c0                   xor    %eax,%eax
  15:   c3                      retq   
  16:   66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
  1d:   00 00 00 
  20:   b8 ff ff ff ff          mov    $0xffffffff,%eax
  25:   c3                      retq


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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-28 11:39 [Bug rtl-optimization/68131] New: missed optimization and warning for broken overflow check rv at rasmusvillemoes dot dk
2015-10-28 12:19 ` [Bug tree-optimization/68131] " glisse at gcc dot gnu.org
2015-10-28 12:31 ` rguenth at gcc dot gnu.org
2015-10-28 12:59 ` glisse at gcc dot gnu.org
2021-08-29 20:18 ` pinskia at gcc dot gnu.org
2023-10-24 20: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).