public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/28045]  New: Bitfield, &&, and optimization => bad code generation
@ 2006-06-15 19:34 Jerry dot James at usu dot edu
  2006-06-15 20:07 ` [Bug c/28045] " rguenth at gcc dot gnu dot org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: Jerry dot James at usu dot edu @ 2006-06-15 19:34 UTC (permalink / raw)
  To: gcc-bugs

This is a stripped-down bit of code representing a bad code generation problem
we've been having with XEmacs 21.5 + gcc 4.X + optimization.  I can reproduce
with Fedora Core 5's packaging of gcc 4.1.1 on the x86_64 platform, and with
Ubuntu's packaging of gcc 4.0.3 on the i386 platform.

Compile the following code without optimization, and it reports that the
negation of 1 is -1, which is in bounds.  Compile with any -O flag (confirmed
for -O, -O2, -O3, and -Os) and the code reports that the negation of 1 is -1,
which is out of bounds.  If I break the && expression up into 2 consecutive if
statements to see which bound is supposedly violated, the optimized code
reports that -1 is within each bound individually.

Things that have no effect: int/long are interchangeable; the size of the "tag"
bitfield doesn't seem to matter, so long as the "tag" size and the "val" size
add up to "INT_BITS".

I also tried compiling with all of the flags turned on by -O, but without -O
itself.  Good code is generated in that case.

#include <stdio.h>

#define INT_BITS (sizeof(int) * 8)
#define MAX_VALUE (int)((1UL << (INT_BITS - 2)) - 1UL)
#define MIN_VALUE (-MAX_VALUE - 1)

struct tagged_int
{
  int tag: 2;
  int val: INT_BITS - 2;
};

static void
negate (struct tagged_int accum)
{
  printf ("min = %d, max = %d\n", MIN_VALUE, MAX_VALUE);
  printf ("The negation of 1 is %d, which is ", -(int)accum.val);
  if (-(int)accum.val <= MAX_VALUE && -(int)accum.val >= MIN_VALUE) {
    puts ("in bounds.");
  } else {
    puts ("out of bounds.");
  }
}

int
main ()
{
  struct tagged_int obj;

  obj.tag = 0;
  obj.val = 1L;
  negate(obj);
  return 0;
}


-- 
           Summary: Bitfield, &&, and optimization => bad code generation
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Jerry dot James at usu dot edu
 GCC build triplet: x86_64-redhat-linux
  GCC host triplet: x86_64-redhat-linux
GCC target triplet: x86_64-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28045


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

end of thread, other threads:[~2006-06-29 21:37 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-15 19:34 [Bug c/28045] New: Bitfield, &&, and optimization => bad code generation Jerry dot James at usu dot edu
2006-06-15 20:07 ` [Bug c/28045] " rguenth at gcc dot gnu dot org
2006-06-17 21:05 ` Jerry dot James at usu dot edu
2006-06-17 22:06 ` Jerry dot James at usu dot edu
2006-06-17 22:07 ` pinskia at gcc dot gnu dot org
2006-06-18 15:19 ` rguenth at gcc dot gnu dot org
2006-06-18 16:35 ` [Bug middle-end/28045] [4.0/4.1/4.2 Regression] " pinskia at gcc dot gnu dot org
2006-06-19 14:49 ` rguenth at gcc dot gnu dot org
2006-06-19 14:57 ` [Bug middle-end/28045] [4.0/4.1 " rguenth at gcc dot gnu dot org
2006-06-19 16:31 ` Jerry dot James at usu dot edu
2006-06-23  9:57 ` rguenth at gcc dot gnu dot org
2006-06-23  9:58 ` rguenth at gcc dot gnu dot org
2006-06-23  9:59 ` rguenth at gcc dot gnu dot org
2006-06-29 21:40 ` patchapp at dberlin dot 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).