public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "eskil at obsession dot se" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c/46899] New: compiler optimization Date: Sun, 12 Dec 2010 00:35:00 -0000 [thread overview] Message-ID: <bug-46899-4@http.gcc.gnu.org/bugzilla/> (raw) 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
next reply other threads:[~2010-12-12 0:35 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2010-12-12 0:35 eskil at obsession dot se [this message] 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
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-46899-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).