From mboxrd@z Thu Jan 1 00:00:00 1970 From: keenan@ghs.com To: gcc-gnats@gcc.gnu.org Subject: optimization/4751: g++ -O producing incorrect code for ((global1&255)==255 || global2) Date: Tue, 30 Oct 2001 15:46:00 -0000 Message-id: <20011030233615.28201.qmail@sourceware.cygnus.com> X-SW-Source: 2001-10/msg00706.html List-Id: >Number: 4751 >Category: optimization >Synopsis: g++ -O producing incorrect code for ((global1&255)==255 || global2) >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: wrong-code >Submitter-Id: net >Arrival-Date: Tue Oct 30 15:46:00 PST 2001 >Closed-Date: >Last-Modified: >Originator: keenan@ghs.com >Release: gcc version 3.0.1 (also reproduced on 2.95.2 and 2.95.3) >Organization: >Environment: HP-UX 11.0 (also reproduced on 10.20) >Description: An illegal optimization is being performed, such that the attached file produces incorrect output when compiled with -O (or -O1, -O2, -O3). The simple test case breaks down to code that is of the form (where both globals are type int) if(((global1&255)==global1) || global2) exectute_some_statement(); The statement is _always_ executed, even when both expressions are false. If split into two tests: if(((global1&255)==global1)) exectute_some_statement(); if(global2) exectute_some_statement(); The statment is not executed. I wasn't able to reproduce this bug on non-HP-UX hosts. >How-To-Repeat: g++ -O foo.cc ./a.out >Fix: >Release-Note: >Audit-Trail: >Unformatted: ----gnatsweb-attachment---- Content-Type: text/plain; name="foo.cc" Content-Disposition: inline; filename="foo.cc" #include int global_0 = 0; int global_neg1 = -1; int main() { bool test = ((global_neg1&255)==global_neg1); // bool test = !(global_neg1&~255); if(test || global_0) { printf ("Guess again...\n"); } return 0; }