Bug 36996 - Bad optimization
Summary: Bad optimization
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.2.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 56672 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-08-01 10:07 UTC by Nic Roets
Modified: 2013-03-21 09:59 UTC (History)
1 user (show)

See Also:
Host:
Target: i486-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nic Roets 2008-08-01 10:07:02 UTC
When the program below is compiled with -O2, it goes into an endless loop.

#include <stdio.h>

int main (void)
{
  int mask;
  for (mask = 0; (mask & 7) != 4; mask += 0x55555555) {
    printf ("%x\n", mask & 7);
  }
}

The output of gcc -v is 
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
Comment 1 Andreas Schwab 2008-08-01 10:59:36 UTC
This depends on undefined behaviour when mask + 0x55555555 overflows.  Change mask to unsigned to get defined behaviour.
Comment 2 Richard Biener 2013-03-21 09:59:24 UTC
*** Bug 56672 has been marked as a duplicate of this bug. ***