From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23464 invoked by alias); 3 Nov 2011 07:19:24 -0000 Received: (qmail 23450 invoked by uid 22791); 3 Nov 2011 07:19:23 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,TW_OV X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 03 Nov 2011 07:19:08 +0000 From: "gcc.hall at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/50975] New: Logical operators evaluated in wrong order if no side effects Date: Thu, 03 Nov 2011 07:19:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Keywords: X-Bugzilla-Severity: major X-Bugzilla-Who: gcc.hall at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-11/txt/msg00230.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50975 Bug #: 50975 Summary: Logical operators evaluated in wrong order if no side effects Classification: Unclassified Product: gcc Version: 4.6.2 Status: UNCONFIRMED Severity: major Priority: P3 Component: c AssignedTo: unassigned@gcc.gnu.org ReportedBy: gcc.hall@gmail.com gcc 4.6.2 -Os If the expressions either side of a logical operator (|| or &&) have no side effects, gcc sometimes evaluates them in the wrong order. See the example below. Of course the code works, but the standard is clear, logical operators GUARANTEE left to right evaluation. In this case people use the feature as a micro optimization, putting the most likely to succeed case first with || or vice-versa with &&. ----------------------------------- #include int main( int argc, char *argv[] ) { char *last = argv[1]; if( *last == 10 || *last == 13 ) --last; printf("last = %p\n", last ); return 0; } --------------------------------------------- movb (%eax), %dl # *last_3, D.2517 cmpb $13, %dl #, D.2517 je .L4 #, cmpb $10, %dl #, D.2517 jne .L2 #, .L4: decl %eax # last .L2: --------------------------------------------