This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Question about GCC 5.2.0 and expression reordering
- From: Andrew Haley <aph at redhat dot com>
- To: David Barto <barto at cambridgesemantics dot com>, gcc-help at gcc dot gnu dot org
- Date: Wed, 14 Jun 2017 17:37:41 +0100
- Subject: Re: Question about GCC 5.2.0 and expression reordering
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=aph at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 414F87F6A4
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 414F87F6A4
- References: <CC40CBD3-1E2F-4620-9A8A-68F17699C030@cambridgesemantics.com>
On 14/06/17 16:58, David Barto wrote:
> I have the following code. It is compiled at -O3 using g++
> with the -std=gnu++14 option.
>
> The code in question is the following:
>
> if ( !testbit(rec_scan0,63) && f20type == URI ) {
>
> Valgrind claims that the "Conditional jump or move depends on uninitialized value(s)"
>
> Well, only if !testbit(rec_scan0,63) should we check the f20type value,
> right? Apparently GCC is reordering the expression. This makes no
> sense to me, from the old school of C coding.
>
> My question is 2 fold:
> 1 - is this legal (and I think it is) and if so would someone point to the relevant
> part of the C++ standard. (I can’t find it)
Yes. By the "as if" rule, as long as your program does what it's
supposed to do, GCC can do whatever it likes under the hood. The
"don't evaluate the RHS of a && operator" rule only actually matters
if the RHS has a side effect. Sure, reading an uninitialized value is
undefined behaviour, bur GCC knows that it will do no harm.
--
Andrew Haley
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671