public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/46899] New: compiler optimization
@ 2010-12-12  0:35 eskil at obsession dot se
  2010-12-12  1:54 ` [Bug c/46899] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: eskil at obsession dot se @ 2010-12-12  0:35 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: compiler optimization
           Product: gcc
           Version: 4.4.5
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: eskil@obsession.se


This keeps bothering me because I really think there is something wrong with
the gcc compiler. Its a bit of a corner case but still.

Lets consider the following code:

void my_func(short a)
{
    unsigned int b;

    b = (unsigned int)a;

    if(b == 70000)
    {
    /* something */
    }
}

Now my question is, will the "something" code ever run. So lets have a look at
the line:

b = (unsigned int)a;

In no implementation that I am aware of will a 16bit short converted to an
unsigned int ever produce the value 70000. However, reading the spec it says:

An example of undefined behavior is the behavior on integer overflow.

So in theory it could give b the value of 70000, on a particular (although
imaginary) implementation. So according to the spec, the answer is yes.

So if the compiler wants to optimize this code it has two options: Assume b can
be anything (and keep the if statement), or determine the behavior of the
particular implementation of overflow on the target hardware, and conclude that
it could NEVER yield the number 70000. It could then optimize away the if
statement.

Gcc however makes the assumption, that b will be in the range of zero and max
short, disregarding if this is true on the hardware. An assumption neither the
spec, or most hardware implementations backs up.

This doesnt matter if the number is 70000 (because we dont have that weird
hardware), but if it is 4294967294, it will, since most hardware will wrap the
overflowing integer.

This causes problems since users (like me) can read out the value of b, see
that it matched what ever I'm comparing it to, and yet it doesnt trigger the
branch. 

Regards 

E


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

end of thread, other threads:[~2010-12-13 14:09 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-12  0:35 [Bug c/46899] New: compiler optimization eskil at obsession dot se
2010-12-12  1:54 ` [Bug c/46899] " pinskia at gcc dot gnu.org
2010-12-12  1:56 ` pinskia at gcc dot gnu.org
2010-12-12  9:10 ` eskil at obsession dot se
2010-12-12 10:20 ` pinskia at gcc dot gnu.org
2010-12-12 12:30 ` eskil at obsession dot se
2010-12-12 21:03 ` pinskia at gcc dot gnu.org
2010-12-12 21:46 ` eskil at obsession dot se
2010-12-12 21:52 ` pinskia at gcc dot gnu.org
2010-12-12 22:23 ` eskil at obsession dot se
2010-12-13  0:21 ` schwab@linux-m68k.org
2010-12-13 14:09 ` eskil at obsession dot se

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).